Alexandria 2.31.0
SDC-CH common library for the Euclid project
|
The NdArray module provides a multidimensional container storing its elements contiguously in memory in row-major order. For instance, a 3D array with the shape (3,2,4) contains 24 elements.
\[ \prod_{i=0}^{n}shape[i] \]
The order in memory for that array would be:
The usage of this class is fairly straight forward. You need to define it specifying at least the contained type, and pass to the constructor either a shape, or a shape and initial data.
For convenience, you can pass the shape as a vector of size_t
, or as a initializer list.
The individual elements can be accessed with the method at
. Similarly to the constructor, it accepts either the axes as a list of parameters, or as a vector of size_t.
The version that accepts a list of parameters ultimately generates a call to the second via metaprogramming, so they are fully equivalent.
To get the array shape, just use the shape
method.
You can also get the total size (number of elements), and a reference to the underlying container.
NdArray can be reshaped as long as the new shape matches exactly the number of elements already contained within the array.
Last, there is an overload of the operator <<
for std::ostream
. This can be useful for debugging, but it is also necessary for the implementation of Euclid::Table::AsciiWriter, as it relies on the existence of this operator (technically, boost::lexical_cast
does). The output has the form <shape>values
.
Alexandria 2.17 adds support for numpy array files. For using them, you need to include the header NdArray/io/Npy.h
, and NdArray/io/NpyMmap.h
for memory mapped files. This allows the exchange of data between Alexandria based software and Python.
Note, however, that the support is limited to primitive types - ints of different sizes, floats, doubles - with native endianness. Structured arrays are not supported.
The generated files can be read by numpy
in any architecture, as the metadata is properly initialized.
An example for reading a numpy
array:
For writing:
As you can notice, the type has to be known in advance.
Alexandria also supports memory mapped files both for reading and writing, although for creation the shape has to be known in advance: