NDArray

template<typename T, ssize_t Ndim = 2>
class NDArray : public aare::ArrayExpr<NDArray<T, 2>, 2>

Public Types

using value_type = T

Public Functions

inline NDArray()

Default constructor. Constructs an empty NDArray.

inline explicit NDArray(std::array<ssize_t, Ndim> shape)

Construct a new NDArray object with a given shape.

Note

The data is uninitialized.

Parameters:

shape – shape of the new NDArray

inline NDArray(std::array<ssize_t, Ndim> shape, T value)

Construct a new NDArray object with a shape and value.

Parameters:
  • shape – shape of the new array

  • value – value to initialize the array with

inline explicit NDArray(NDView<const T, Ndim> v)

Construct a new NDArray object from a NDView.

Note

The data is copied from the view to the NDArray. The view is only read, so both NDView<T, Ndim> and NDView<const T, Ndim> are accepted.

Parameters:

v – view of data to initialize the NDArray with

inline explicit NDArray(NDView<T, Ndim> v)
template<size_t Size>
inline NDArray(const std::array<T, Size> &arr)

Construct a new NDArray object from an std::array.

inline NDArray(NDArray &&other) noexcept

Move construct a new NDArray object. Cheap since it just reassigns the pointer and copy size/strides.

Parameters:

other –

template<ssize_t M, typename = std::enable_if_t<(M == Ndim + 1)>>
inline NDArray(NDArray<T, M> &&other)

Move construct a new NDArray object from an array with Ndim + 1. Can be used to drop a dimension cheaply.

Parameters:

other –

inline NDArray(const NDArray &other)

Copy construct a new NDArray object from another NDArray.

Parameters:

other –

template<typename E>
inline NDArray(ArrayExpr<E, Ndim> &&expr)

Conversion from a ArrayExpr to an actual NDArray. Used when the expression is evaluated and data needed.

Template Parameters:

E –

Parameters:

expr –

inline ~NDArray()

Destroy the NDArray object. Frees the allocated memory.

inline void copy_from(NDView<const T, Ndim> v)

Copy data from a view of matching shape into this array without reallocating.

Use this instead of assigning a new NDArray when the buffer needs to be kept, for example when the array is part of a preallocated pool.

Parameters:

v – view to copy from, must have the same shape as this array. Accepts NDView<T, Ndim> and NDView<const T, Ndim>.

Throws:

std::runtime_error – if the shapes differ

inline auto *begin()
inline const auto *begin() const
inline auto *end()
inline const auto *end() const
template<typename ...Ix>
inline std::enable_if_t<sizeof...(Ix) == Ndim, T&> operator()(Ix... index)
template<typename ...Ix>
inline std::enable_if_t<sizeof...(Ix) == Ndim, const T&> operator()(Ix... index) const
inline T &operator()(ssize_t i)
inline const T &operator()(ssize_t i) const
inline T &operator[](ssize_t i)
inline const T &operator[](ssize_t i) const
inline T *data()
inline const T *data() const
inline std::byte *buffer()
inline ssize_t size() const

Return the total number of elements in the array as a signed integer.

inline size_t total_bytes() const

Return the total number of bytes in the array.

inline Shape<Ndim> shape() const noexcept

Return the shape of the array.

inline ssize_t shape(ssize_t i) const noexcept

Return the size of dimension i.

inline std::array<ssize_t, Ndim> strides() const noexcept

Return the strides of the array.

inline size_t bitdepth() const noexcept

Return the bitdepth of the array. Useful for checking that detector data can fit in the array type.

inline std::array<ssize_t, Ndim> byte_strides() const noexcept

Return the number of bytes to step in each dimension when traversing the array.

template<size_t Size>
inline NDArray<T, 1> &operator=(const std::array<T, Size> &other)

Copy to the NDArray from an std::array. If the size of the array is different we reallocate the data.

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

Move assignment operator.

inline NDArray &operator=(const NDArray &other)

Copy assignment operator.

template<typename E>
inline NDArray &operator=(const ArrayExpr<E, Ndim> &expr)

Evaluate an ArrayExpr into the NDArray. Reuses the existing buffer if the shape matches.

inline NDArray &operator+=(const NDArray &other)

Add elementwise from another NDArray.

inline NDArray &operator-=(const NDArray &other)

Subtract elementwise with another NDArray.

inline NDArray &operator*=(const NDArray &other)

Multiply elementwise with another NDArray.

template<typename V>
inline NDArray &operator/=(const NDArray<V, Ndim> &other)

Divide elementwise by another NDArray. Templated to allow division with different types.

TODO! Why is this templated when the others are not?

inline NDArray &operator=(const T &value)

Assign a scalar value to all elements in the NDArray.

inline NDArray &operator+=(const T &value)

Add a scalar value to all elements in the NDArray.

inline NDArray &operator-=(const T &value)

Subtract a scalar value to all elements in the NDArray.

inline NDArray &operator*=(const T &value)

Multiply all elements in the NDArray with a scalar value.

inline NDArray &operator/=(const T &value)

Divide all elements in the NDArray with a scalar value.

inline NDArray &operator&=(const T &mask)

Bitwise AND all elements in the NDArray with a scalar mask. Used for example to mask out gain bits for Jungfrau detectors.

inline bool operator==(const NDArray &other) const

Compare two NDArrays elementwise for equality.

inline bool operator!=(const NDArray &other) const

Compare two NDArrays elementwise for non-equality.

inline void sqrt()

Compute the square root of all elements in the NDArray.

inline NDArray &operator++()
inline NDView<T, Ndim> view()

Create a mutable view of the NDArray.

inline NDView<const T, Ndim> view() const

Create a read-only view of a const NDArray.

Public Static Attributes

static constexpr bool is_leaf = true

Friends

friend class NDArray