Miscellaneous¶
Version number¶
The same functions and macros as the C API are available in C++.
Error handling¶
-
class Error : public runtime_error¶
Exception class used for all errors in metatensor.
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(const T *data, std::vector<size_t> shape)¶
Create a new
NDArrayusing a non-owning view inconstmemory with the givenshape.datamust point to contiguous memory containing the right number of elements as described by theshape, which will be interpreted as an N-dimensional array in row-major order. The resultingNDArrayis only valid for as long asdatais.
-
inline NDArray(T *data, std::vector<size_t> shape)¶
Create a new
NDArrayusing a non-owning view in non-constmemory with the givenshape.datamust point to contiguous memory containing the right number of elements as described by theshape, which will be interpreted as an N-dimensional array in row-major order. The resultingNDArrayis only valid for as long asdatais.
-
inline NDArray(std::vector<T> data, std::vector<size_t> shape)¶
Create a new
NDArrayowning itsdatawith the givenshape.
-
template<typename ...Args>
inline T operator()(Args... args) const &¶ Get the value inside this
NDArrayat the given indexauto array = NDArray(...); double value = array(2, 3, 1);
-
template<typename ...Args>
inline T &operator()(Args... args) &¶ Get a reference to the value inside this
NDArrayat the given indexauto 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 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.
-
inline NDArray(const T *data, std::vector<size_t> shape)¶
TensorMap serialization¶
-
inline void metatensor::io::save(const std::string &path, const TensorMap &tensor)¶
Save a
TensorMapto the file atpath.If the file exists, it will be overwritten.
TensorMapare serialized using numpy’s.npzformat, i.e. a ZIP file without compression (storage method isSTORED), where each file is stored as a.npyarray. See the C API documentation for more information on the format.
-
template<typename Buffer = std::vector<uint8_t>>
Buffer metatensor::io::save_buffer(const TensorMap &tensor)¶ Save a
TensorMapto an in-memory buffer.The
Buffertemplate parameter can be set to any type that can be constructed from a pair of iterator overstd::vector<uint8_t>.
-
inline TensorMap metatensor::io::load(const std::string &path, mts_create_array_callback_t create_array = details::default_create_array)¶
Load a previously saved
TensorMapfrom the given path.create_arraywill be used to create new arrays when constructing the blocks and gradients, the default version will create data usingSimpleDataArray. Seemts_create_array_callback_t()for more information.TensorMapare serialized using numpy’s.npzformat, i.e. a ZIP file without compression (storage method isSTORED), where each file is stored as a.npyarray. See the C API documentation for more information on the format.
-
inline TensorMap metatensor::io::load_buffer(const uint8_t *buffer, size_t buffer_count, mts_create_array_callback_t create_array = details::default_create_array)¶
Load a previously saved
TensorMapfrom the givenbuffer, containingbuffer_countelements.create_arraywill be used to create new arrays when constructing the blocks and gradients, the default version will create data usingSimpleDataArray. Seemts_create_array_callback_t()for more information.
-
template<typename Buffer>
TensorMap metatensor::io::load_buffer(const Buffer &buffer, mts_create_array_callback_t create_array = details::default_create_array)¶ Load a previously saved
TensorMapfrom the givenbuffer.The
Buffertemplate parameter would typically be astd::vector<uint8_t>or astd::string, but any container with contiguous data and anitem_typewith the same size as auint8_tcan work.
-
inline mts_status_t metatensor::details::default_create_array(const uintptr_t *shape_ptr, uintptr_t shape_count, mts_array_t *array)¶
Default callback for data array creating in
TensorMap::load, which will create aSimpleDataArray.
Labels serialization¶
-
inline void metatensor::io::save(const std::string &path, const Labels &labels)¶
Save
Labelsto the file atpath.If the file exists, it will be overwritten.
-
template<typename Buffer = std::vector<uint8_t>>
Buffer metatensor::io::save_buffer(const Labels &labels)¶ Save
Labelsto an in-memory buffer.The
Buffertemplate parameter can be set to any type that can be constructed from a pair of iterator overstd::vector<uint8_t>.
-
inline Labels metatensor::io::load_labels(const std::string &path)¶
Load previously saved
Labelsfrom the given path.
-
inline Labels metatensor::io::load_labels_buffer(const uint8_t *buffer, size_t buffer_count)¶
Load previously saved
Labelsfrom the givenbuffer, containingbuffer_countelements.
-
template<typename Buffer>
Labels metatensor::io::load_labels_buffer(const Buffer &buffer)¶ Load a previously saved
Labelsfrom the givenbuffer.The
Buffertemplate parameter would typically be astd::vector<uint8_t>or astd::string, but any container with contiguous data and anitem_typewith the same size as auint8_tcan work.