MTL5 -- Matrix Template Library C++ API
mtl:: -- C++20 header-only linear algebra for mixed-precision algorithm design
Loading...
Searching...
No Matches
mtl::array::ndarray< Value, N, Order > Class Template Reference

N-dimensional array with static rank and runtime shape. More...

#include <ndarray.hpp>

Public Types

using value_type = Value
using reference = Value&
using const_reference = const Value&
using pointer = Value*
using const_pointer = const Value*
using size_type = std::size_t
using shape_type = ::mtl::array::shape<N>
using strides_type = std::array<size_type, N>

Public Member Functions

 ndarray ()=default
 Default: empty array with zero extents.
 ndarray (shape_type sh)
 Construct from shape, zero-initialized.
 ndarray (std::initializer_list< size_type > extents)
 Construct from initializer list of extents.
 ndarray (shape_type sh, const Value &val)
 Construct from shape + fill value.
 ndarray (Value *ptr, shape_type sh, strides_type strides)
 Non-owning view: wraps external memory with given shape and strides.
 ndarray (Value *ptr, shape_type sh)
 Non-owning view: wraps external memory with contiguous layout.
 ndarray (const ndarray &other)
 ndarray (ndarray &&other) noexcept
 The source is left empty: no storage, a zero shape.
ndarray & operator= (const ndarray &other)
ndarray & operator= (ndarray &&other) noexcept
constexpr const shape_type & get_shape () const
constexpr const strides_type & get_strides () const
constexpr size_type extent (size_type dim) const
constexpr size_type size () const
constexpr bool empty () const
bool is_view () const
 Check if this array owns its memory.
bool is_contiguous () const
 True when the elements occupy a dense block of size() values, in EITHER C or F order.
bool is_c_contiguous () const
 True when memory is contiguous in C (row-major) order.
bool is_f_contiguous () const
 True when memory is contiguous in F (column-major) order.
bool is_contiguous_in_own_order () const
 True when memory order matches THIS array's logical iteration order, i.e.
template<typename... Indices>
requires (sizeof...(Indices) == N)
reference operator() (Indices... indices)
 Multi-index access: a(i, j, k, ...).
template<typename... Indices>
requires (sizeof...(Indices) == N)
const_reference operator() (Indices... indices) const
reference operator[] (const std::array< size_type, N > &idx)
 Array-index access: a[{i, j, k}].
const_reference operator[] (const std::array< size_type, N > &idx) const
reference flat (size_type i)
 Flat (linear) index access - only valid for contiguous arrays.
const_reference flat (size_type i) const
pointer data ()
const_pointer data () const
pointer begin ()
const_pointer begin () const
pointer end ()
const_pointer end () const
void fill (const Value &val)
ndarray view ()
 Create a view (non-owning) of this array with the same shape and strides.
template<std::size_t M>
ndarray< Value, M, Order > reshape (::mtl::array::shape< M > new_shape)
 Reshape: a view over the same elements, read in this array's Order.
template<std::size_t M>
ndarray< const Value, M, Order > reshape (::mtl::array::shape< M > new_shape) const
 Reshape of a const array: a read-only view (#856: it used to return a mutable view through a const_cast).
ndarray< Value, N, Order > transpose ()
 Transpose: a view with reversed shape and strides.
ndarray< const Value, N, Order > transpose () const
 Transpose of a const array: a read-only view (#856).
ndarray & operator+= (const Value &s)
ndarray & operator-= (const Value &s)
ndarray & operator*= (const Value &s)
ndarray & operator/= (const Value &s)
template<typename F>
void iterate_indices (F &&f) const
 Iterate over all valid multi-indices, calling f(idx) for each, in this array's Order (the last index fastest for c_order, the first for f_order).
template<typename F>
void for_each_element (F &&f)
 Apply f to every element, in this array's Order.
template<typename F>
void for_each_element (F &&f) const

Static Public Attributes

static constexpr size_type rank = N

Detailed Description

template<typename Value, std::size_t N, typename Order = c_order>
class mtl::array::ndarray< Value, N, Order >

N-dimensional array with static rank and runtime shape.

Its logical order – the order of iterate_indices, for_each_element, reduce and flatten – is its own Order: c_order arrays walk the last index fastest, f_order arrays the first, whatever the strides of a view say. reshape reads in the same order (#359), so flatten(a) and a.reshape({a.size()}) agree.

Template Parameters
Valueelement type (must be default-constructible)
Nnumber of dimensions (compile-time rank)
Ordermemory layout: c_order (default) or f_order

Member Typedef Documentation

◆ const_pointer

template<typename Value, std::size_t N, typename Order = c_order>
using mtl::array::ndarray< Value, N, Order >::const_pointer = const Value*

◆ const_reference

template<typename Value, std::size_t N, typename Order = c_order>
using mtl::array::ndarray< Value, N, Order >::const_reference = const Value&

◆ pointer

template<typename Value, std::size_t N, typename Order = c_order>
using mtl::array::ndarray< Value, N, Order >::pointer = Value*

◆ reference

template<typename Value, std::size_t N, typename Order = c_order>
using mtl::array::ndarray< Value, N, Order >::reference = Value&

◆ shape_type

template<typename Value, std::size_t N, typename Order = c_order>
using mtl::array::ndarray< Value, N, Order >::shape_type = ::mtl::array::shape<N>

◆ size_type

template<typename Value, std::size_t N, typename Order = c_order>
using mtl::array::ndarray< Value, N, Order >::size_type = std::size_t

◆ strides_type

template<typename Value, std::size_t N, typename Order = c_order>
using mtl::array::ndarray< Value, N, Order >::strides_type = std::array<size_type, N>

◆ value_type

template<typename Value, std::size_t N, typename Order = c_order>
using mtl::array::ndarray< Value, N, Order >::value_type = Value

Constructor & Destructor Documentation

◆ ndarray() [1/8]

template<typename Value, std::size_t N, typename Order = c_order>
mtl::array::ndarray< Value, N, Order >::ndarray ( )
default

Default: empty array with zero extents.

◆ ndarray() [2/8]

template<typename Value, std::size_t N, typename Order = c_order>
mtl::array::ndarray< Value, N, Order >::ndarray ( shape_type sh)
inlineexplicit

Construct from shape, zero-initialized.

◆ ndarray() [3/8]

template<typename Value, std::size_t N, typename Order = c_order>
mtl::array::ndarray< Value, N, Order >::ndarray ( std::initializer_list< size_type > extents)
inline

Construct from initializer list of extents.

◆ ndarray() [4/8]

template<typename Value, std::size_t N, typename Order = c_order>
mtl::array::ndarray< Value, N, Order >::ndarray ( shape_type sh,
const Value & val )
inline

Construct from shape + fill value.

◆ ndarray() [5/8]

template<typename Value, std::size_t N, typename Order = c_order>
mtl::array::ndarray< Value, N, Order >::ndarray ( Value * ptr,
shape_type sh,
strides_type strides )
inline

Non-owning view: wraps external memory with given shape and strides.

◆ ndarray() [6/8]

template<typename Value, std::size_t N, typename Order = c_order>
mtl::array::ndarray< Value, N, Order >::ndarray ( Value * ptr,
shape_type sh )
inline

Non-owning view: wraps external memory with contiguous layout.

◆ ndarray() [7/8]

template<typename Value, std::size_t N, typename Order = c_order>
mtl::array::ndarray< Value, N, Order >::ndarray ( const ndarray< Value, N, Order > & other)
inline

◆ ndarray() [8/8]

template<typename Value, std::size_t N, typename Order = c_order>
mtl::array::ndarray< Value, N, Order >::ndarray ( ndarray< Value, N, Order > && other)
inlinenoexcept

The source is left empty: no storage, a zero shape.

Member Function Documentation

◆ begin() [1/2]

template<typename Value, std::size_t N, typename Order = c_order>
pointer mtl::array::ndarray< Value, N, Order >::begin ( )
inline

◆ begin() [2/2]

template<typename Value, std::size_t N, typename Order = c_order>
const_pointer mtl::array::ndarray< Value, N, Order >::begin ( ) const
inline

◆ data() [1/2]

template<typename Value, std::size_t N, typename Order = c_order>
pointer mtl::array::ndarray< Value, N, Order >::data ( )
inline

◆ data() [2/2]

template<typename Value, std::size_t N, typename Order = c_order>
const_pointer mtl::array::ndarray< Value, N, Order >::data ( ) const
inline

◆ empty()

template<typename Value, std::size_t N, typename Order = c_order>
bool mtl::array::ndarray< Value, N, Order >::empty ( ) const
inlineconstexpr

◆ end() [1/2]

template<typename Value, std::size_t N, typename Order = c_order>
pointer mtl::array::ndarray< Value, N, Order >::end ( )
inline

◆ end() [2/2]

template<typename Value, std::size_t N, typename Order = c_order>
const_pointer mtl::array::ndarray< Value, N, Order >::end ( ) const
inline

◆ extent()

template<typename Value, std::size_t N, typename Order = c_order>
size_type mtl::array::ndarray< Value, N, Order >::extent ( size_type dim) const
inlineconstexpr

◆ fill()

template<typename Value, std::size_t N, typename Order = c_order>
void mtl::array::ndarray< Value, N, Order >::fill ( const Value & val)
inline

◆ flat() [1/2]

template<typename Value, std::size_t N, typename Order = c_order>
reference mtl::array::ndarray< Value, N, Order >::flat ( size_type i)
inline

Flat (linear) index access - only valid for contiguous arrays.

◆ flat() [2/2]

template<typename Value, std::size_t N, typename Order = c_order>
const_reference mtl::array::ndarray< Value, N, Order >::flat ( size_type i) const
inline

◆ for_each_element() [1/2]

template<typename Value, std::size_t N, typename Order = c_order>
template<typename F>
void mtl::array::ndarray< Value, N, Order >::for_each_element ( F && f)
inline

Apply f to every element, in this array's Order.

Memory that is contiguous in that order is walked directly; anything else – a slice, a transposed view – by strides, the innermost dimension a plain loop.

◆ for_each_element() [2/2]

template<typename Value, std::size_t N, typename Order = c_order>
template<typename F>
void mtl::array::ndarray< Value, N, Order >::for_each_element ( F && f) const
inline

◆ get_shape()

template<typename Value, std::size_t N, typename Order = c_order>
const shape_type & mtl::array::ndarray< Value, N, Order >::get_shape ( ) const
inlineconstexpr

◆ get_strides()

template<typename Value, std::size_t N, typename Order = c_order>
const strides_type & mtl::array::ndarray< Value, N, Order >::get_strides ( ) const
inlineconstexpr

◆ is_c_contiguous()

template<typename Value, std::size_t N, typename Order = c_order>
bool mtl::array::ndarray< Value, N, Order >::is_c_contiguous ( ) const
inline

True when memory is contiguous in C (row-major) order.

◆ is_contiguous()

template<typename Value, std::size_t N, typename Order = c_order>
bool mtl::array::ndarray< Value, N, Order >::is_contiguous ( ) const
inline

True when the elements occupy a dense block of size() values, in EITHER C or F order.

This is the right question for anything order-INdependent – BLAS dispatch, fill, a commutative reduction – and the WRONG one for anything that walks memory as though it were logical order. A transpose of a C-order array is F-contiguous, so this returns true while memory order and logical order disagree (#359). Use is_c_contiguous() / is_f_contiguous() when the distinction matters.

◆ is_contiguous_in_own_order()

template<typename Value, std::size_t N, typename Order = c_order>
bool mtl::array::ndarray< Value, N, Order >::is_contiguous_in_own_order ( ) const
inline

True when memory order matches THIS array's logical iteration order, i.e.

the order its own Order parameter implies. This is the predicate every order-dependent operation wants, and the one whose absence caused reshape and flatten to return elements in memory order (#359).

◆ is_f_contiguous()

template<typename Value, std::size_t N, typename Order = c_order>
bool mtl::array::ndarray< Value, N, Order >::is_f_contiguous ( ) const
inline

True when memory is contiguous in F (column-major) order.

For N <= 1 this coincides with is_c_contiguous().

◆ is_view()

template<typename Value, std::size_t N, typename Order = c_order>
bool mtl::array::ndarray< Value, N, Order >::is_view ( ) const
inline

Check if this array owns its memory.

◆ iterate_indices()

template<typename Value, std::size_t N, typename Order = c_order>
template<typename F>
void mtl::array::ndarray< Value, N, Order >::iterate_indices ( F && f) const
inline

Iterate over all valid multi-indices, calling f(idx) for each, in this array's Order (the last index fastest for c_order, the first for f_order).

◆ operator()() [1/2]

template<typename Value, std::size_t N, typename Order = c_order>
template<typename... Indices>
requires (sizeof...(Indices) == N)
reference mtl::array::ndarray< Value, N, Order >::operator() ( Indices... indices)
inline

Multi-index access: a(i, j, k, ...).

◆ operator()() [2/2]

template<typename Value, std::size_t N, typename Order = c_order>
template<typename... Indices>
requires (sizeof...(Indices) == N)
const_reference mtl::array::ndarray< Value, N, Order >::operator() ( Indices... indices) const
inline

◆ operator*=()

template<typename Value, std::size_t N, typename Order = c_order>
ndarray & mtl::array::ndarray< Value, N, Order >::operator*= ( const Value & s)
inline

◆ operator+=()

template<typename Value, std::size_t N, typename Order = c_order>
ndarray & mtl::array::ndarray< Value, N, Order >::operator+= ( const Value & s)
inline

◆ operator-=()

template<typename Value, std::size_t N, typename Order = c_order>
ndarray & mtl::array::ndarray< Value, N, Order >::operator-= ( const Value & s)
inline

◆ operator/=()

template<typename Value, std::size_t N, typename Order = c_order>
ndarray & mtl::array::ndarray< Value, N, Order >::operator/= ( const Value & s)
inline

◆ operator=() [1/2]

template<typename Value, std::size_t N, typename Order = c_order>
ndarray & mtl::array::ndarray< Value, N, Order >::operator= ( const ndarray< Value, N, Order > & other)
inline

◆ operator=() [2/2]

template<typename Value, std::size_t N, typename Order = c_order>
ndarray & mtl::array::ndarray< Value, N, Order >::operator= ( ndarray< Value, N, Order > && other)
inlinenoexcept

◆ operator[]() [1/2]

template<typename Value, std::size_t N, typename Order = c_order>
reference mtl::array::ndarray< Value, N, Order >::operator[] ( const std::array< size_type, N > & idx)
inline

Array-index access: a[{i, j, k}].

◆ operator[]() [2/2]

template<typename Value, std::size_t N, typename Order = c_order>
const_reference mtl::array::ndarray< Value, N, Order >::operator[] ( const std::array< size_type, N > & idx) const
inline

◆ reshape() [1/2]

template<typename Value, std::size_t N, typename Order = c_order>
template<std::size_t M>
ndarray< Value, M, Order > mtl::array::ndarray< Value, N, Order >::reshape ( ::mtl::array::shape< M > new_shape)
inline

Reshape: a view over the same elements, read in this array's Order.

Throws std::invalid_argument if the size differs (#856: it was an assert, so under NDEBUG a 3-element array reshaped to 100 elements gave a view past its buffer), and std::runtime_error if the array is not contiguous in its own order.

◆ reshape() [2/2]

template<typename Value, std::size_t N, typename Order = c_order>
template<std::size_t M>
ndarray< const Value, M, Order > mtl::array::ndarray< Value, N, Order >::reshape ( ::mtl::array::shape< M > new_shape) const
inline

Reshape of a const array: a read-only view (#856: it used to return a mutable view through a const_cast).

◆ size()

template<typename Value, std::size_t N, typename Order = c_order>
size_type mtl::array::ndarray< Value, N, Order >::size ( ) const
inlineconstexpr

◆ transpose() [1/2]

template<typename Value, std::size_t N, typename Order = c_order>
ndarray< Value, N, Order > mtl::array::ndarray< Value, N, Order >::transpose ( )
inline

Transpose: a view with reversed shape and strides.

◆ transpose() [2/2]

template<typename Value, std::size_t N, typename Order = c_order>
ndarray< const Value, N, Order > mtl::array::ndarray< Value, N, Order >::transpose ( ) const
inline

Transpose of a const array: a read-only view (#856).

◆ view()

template<typename Value, std::size_t N, typename Order = c_order>
ndarray mtl::array::ndarray< Value, N, Order >::view ( )
inline

Create a view (non-owning) of this array with the same shape and strides.

Member Data Documentation

◆ rank

template<typename Value, std::size_t N, typename Order = c_order>
size_type mtl::array::ndarray< Value, N, Order >::rank = N
staticconstexpr

The documentation for this class was generated from the following file:
  • /home/runner/work/mtl5/mtl5/include/mtl/array/ndarray.hpp