Miscelaneous#

Error handling#

class Error : public runtime_error#

Exception class used for all errors in metatensor.

Public Functions

inline Error(const std::string &message)#

Create a new Error with the given message

N-dimensional arrays#

template<typename T>
class NDArray#

Simple N-dimensional array interface

This class can either be a non-owning view inside some existing memory (for example memory allocated by Rust); or own its memory (in the form of an std::vector<double>). If the array does not own its memory, accessing it is only valid for as long as the memory is kept alive.

The API of this class is very intentionally minimal to keep metatensor as simple as possible. Feel free to wrap the corresponding data inside types with richer API such as Eigen, Boost, etc.

Public Functions

inline NDArray()#

Create a new empty NDArray, with shape [0, 0].

inline NDArray(const T *data, std::vector<size_t> shape)#

Create a new NDArray using a non-owning view in const memory with the given shape.

data must point to contiguous memory containing the right number of elements as described by the shape, which will be interpreted as an N-dimensional array in row-major order. The resulting NDArray is only valid for as long as data is.

inline NDArray(T *data, std::vector<size_t> shape)#

Create a new NDArray using a non-owning view in non-const memory with the given shape.

data must point to contiguous memory containing the right number of elements as described by the shape, which will be interpreted as an N-dimensional array in row-major order. The resulting NDArray is only valid for as long as data is.

inline NDArray(std::vector<T> data, std::vector<size_t> shape)#

Create a new NDArray owning its data with the given shape.

NDArray(const NDArray&) = delete#

NDArray is not copy-constructible.

NDArray &operator=(const NDArray &other) = delete#

NDArray can not be copy-assigned.

inline NDArray(NDArray &&other) noexcept#

NDArray is move-constructible.

inline NDArray &operator=(NDArray &&other) noexcept#

NDArray can be move-assigned.

inline bool is_view() const#

Is this NDArray a view into external data?

template<typename ...Args>
inline T operator()(Args... args) const &#

Get the value inside this NDArray at the given index

auto array = NDArray(...);

double value = array(2, 3, 1);
template<typename ...Args>
inline T &operator()(Args... args) &#

Get a reference to the value inside this NDArray at the given index

auto array = NDArray(...);

array(2, 3, 1) = 5.2;
inline const T *data() const &#

Get the data pointer for this array, i.e. the pointer to the first element.

inline T *data() &#

Get the data pointer for this array, i.e. the pointer to the first element.

inline const std::vector<size_t> &shape() const &#

Get the shape of this array.

inline bool is_empty() const#

Check if this array is empty, i.e. if at least one of the shape element is 0.