MTL5 -- Matrix Template Library C++ API
mtl:: -- C++20 header-only linear algebra for mixed-precision algorithm design
Loading...
Searching...
No Matches
mtl::array Namespace Reference

Namespaces

namespace  detail_bcast
namespace  ops
namespace  detail_interop
namespace  detail_nd
namespace  detail_ops
namespace  detail_shape
namespace  detail_slice

Classes

class  broadcast_expr
 Lazy expression for element-wise binary operations with broadcasting. More...
class  ndarray
 N-dimensional array with static rank and runtime shape. More...
class  shape
 Static-rank shape: an array of N extents. More...
struct  all_t
 Select all elements along a dimension. More...
struct  range
 Select a contiguous range [start, stop) with optional step. More...

Typedefs

using c_order = tag::row_major
 Layout order for multi-dimensional arrays.
using f_order = tag::col_major

Functions

template<detail_bcast::Array A, detail_bcast::Array B>
auto operator+ (A &&a, B &&b)
template<detail_bcast::Array A, detail_bcast::Array B>
auto operator- (A &&a, B &&b)
template<detail_bcast::Array A, detail_bcast::Array B>
auto operator* (A &&a, B &&b)
template<detail_bcast::Array A, detail_bcast::Array B>
auto operator/ (A &&a, B &&b)
template<typename Value, typename Params>
ndarray< Value, 1, c_order > as_ndarray (vec::dense_vector< Value, Params > &v)
 Create a 1D ndarray view over a dense_vector (zero-copy).
template<typename Value, typename Params>
ndarray< const Value, 1, c_order > as_ndarray (const vec::dense_vector< Value, Params > &v)
template<typename Value, typename Params>
auto as_ndarray (mat::dense2D< Value, Params > &m)
 Create a 2D ndarray view over a dense2D matrix (zero-copy).
template<typename Value, typename Params>
auto as_ndarray (const mat::dense2D< Value, Params > &m)
template<typename Value, typename Order>
vec::dense_vector< Value > as_vector (ndarray< Value, 1, Order > &a)
 Create a dense_vector view over a 1D ndarray (zero-copy).
template<typename Value, typename Order>
vec::dense_vector< const Value > as_vector (const ndarray< Value, 1, Order > &a)
template<typename Value, typename Order>
requires std::is_same_v<Order, c_order>
mat::dense2D< Value > as_matrix (ndarray< Value, 2, Order > &a)
 Create a dense2D view over a 2D ndarray (zero-copy).
template<typename Value, typename Order>
requires std::is_same_v<Order, c_order>
mat::dense2D< const Value > as_matrix (const ndarray< Value, 2, Order > &a)
template<typename V, std::size_t N, typename O, typename F>
ndarray< std::remove_cv_t< V >, N, O > transform (const ndarray< V, N, O > &a, F &&f)
 Element-wise transform: apply f to every element of a, writing to out.
template<typename V, std::size_t N, typename O, typename F>
void transform_inplace (ndarray< V, N, O > &a, F &&f)
 In-place transform: apply f to every element.
template<typename V, std::size_t N, typename O, typename T, typename BinOp>
T reduce (const ndarray< V, N, O > &a, T init, BinOp op)
 Reduce all elements with a binary operation and initial value.
template<typename V, std::size_t N, typename O>
ndarray< std::remove_cv_t< V >, 1, c_order > flatten (const ndarray< V, N, O > &a)
 Flatten any ndarray to a contiguous 1D ndarray (always copies for safety), in the array's own order: the same elements, in the same order, as a.reshape({a.size()}) views when a is contiguous in that order.
template<typename Value, std::size_t N>
 ndarray (Value *, shape< N >) -> ndarray< Value, N, c_order >
template<typename Value, std::size_t N>
 ndarray (Value *, shape< N >, std::array< std::size_t, N >) -> ndarray< Value, N, c_order >
template<typename V, std::size_t N, typename O>
std::remove_cv_t< V > sum (const ndarray< V, N, O > &a)
 Sum all elements; zero for an empty array.
template<typename V, std::size_t N, typename O>
std::remove_cv_t< V > prod (const ndarray< V, N, O > &a)
 Product of all elements; one for an empty array.
template<typename V, std::size_t N, typename O>
requires std::totally_ordered<std::remove_cv_t<V>>
std::remove_cv_t< V > min (const ndarray< V, N, O > &a)
 Minimum element, seeded from the first one. Throws on an empty array.
template<typename V, std::size_t N, typename O>
requires std::totally_ordered<std::remove_cv_t<V>>
std::remove_cv_t< V > max (const ndarray< V, N, O > &a)
 Maximum element, seeded from the first one. Throws on an empty array.
template<typename V, std::size_t N, typename O>
std::remove_cv_t< V > mean (const ndarray< V, N, O > &a)
 Mean of all elements. Throws on an empty array.
template<typename V, std::size_t N, typename O>
requires (N > 1)
ndarray< std::remove_cv_t< V >, N - 1, O > sum_axis (const ndarray< V, N, O > &a, std::size_t axis)
 Sum along a specified axis, producing an ndarray of rank N-1.
template<typename V, std::size_t N, typename O>
requires (N > 1)
ndarray< std::remove_cv_t< V >, N - 1, O > mean_axis (const ndarray< V, N, O > &a, std::size_t axis)
 Mean along a specified axis.
template<std::size_t N>
constexpr std::array< std::size_t, N > c_order_strides (const shape< N > &sh)
 Compute C-order (row-major) strides: last dimension has stride 1.
template<std::size_t N>
constexpr std::array< std::size_t, N > f_order_strides (const shape< N > &sh)
 Compute F-order (column-major) strides: first dimension has stride 1.
template<typename Order, std::size_t N>
constexpr std::array< std::size_t, N > compute_strides (const shape< N > &sh)
 Compute strides for a given layout order.
template<std::size_t N>
constexpr std::size_t compute_offset (const std::array< std::size_t, N > &indices, const std::array< std::size_t, N > &strides)
 Compute flat offset from multi-index and strides.
template<std::size_t N, typename... Indices>
constexpr std::size_t offset_from (const std::array< std::size_t, N > &strides, Indices... indices)
 Variadic offset computation - converts (i, j, k, ...) to flat index.
template<std::size_t N>
constexpr bool broadcast_shape (const shape< N > &a, const shape< N > &b, shape< N > &result)
 Compute the broadcast shape of two shapes.
template<std::size_t N>
constexpr std::array< std::size_t, N > broadcast_strides (const shape< N > &original, const std::array< std::size_t, N > &strides, const shape< N > &target)
 Compute broadcast strides: dimension with extent 1 gets stride 0 (repeat).
template<std::size_t N>
constexpr bool is_contiguous_c (const shape< N > &sh, const std::array< std::size_t, N > &strides)
 Check if strides correspond to a contiguous C-order layout.
template<std::size_t N>
constexpr bool is_contiguous_f (const shape< N > &sh, const std::array< std::size_t, N > &strides)
 Check if strides correspond to a contiguous F-order layout.
template<std::size_t N>
constexpr bool is_contiguous (const shape< N > &sh, const std::array< std::size_t, N > &strides)
 Check if the array is contiguous in any order.
template<typename Value, std::size_t N, typename Order, typename... Args>
requires (sizeof...(Args) == N)
auto slice (ndarray< Value, N, Order > &arr, Args... args)
 Slice an ndarray, returning a view with potentially reduced rank.
template<typename Value, std::size_t N, typename Order, typename... Args>
requires (sizeof...(Args) == N)
auto slice (const ndarray< Value, N, Order > &arr, Args... args)
 Const overload: returns a view over const data.

Variables

constexpr all_t all {}

Typedef Documentation

◆ c_order

Layout order for multi-dimensional arrays.

◆ f_order

Function Documentation

◆ as_matrix() [1/2]

template<typename Value, typename Order>
requires std::is_same_v<Order, c_order>
mat::dense2D< const Value > mtl::array::as_matrix ( const ndarray< Value, 2, Order > & a)

◆ as_matrix() [2/2]

template<typename Value, typename Order>
requires std::is_same_v<Order, c_order>
mat::dense2D< Value > mtl::array::as_matrix ( ndarray< Value, 2, Order > & a)

Create a dense2D view over a 2D ndarray (zero-copy).

Requires contiguous C-order (row-major) storage, and now CHECKS for it. The precondition used to be spelled is_contiguous(), which is true for an F-contiguous array too – so any transpose of a C-order array passed here aliased its memory as row-major and silently produced the TRANSPOSE (#359). dense2D is row-major, so C-contiguity is the only layout that can be aliased without a copy; pass flattened or explicitly copied data otherwise.

◆ as_ndarray() [1/4]

template<typename Value, typename Params>
auto mtl::array::as_ndarray ( const mat::dense2D< Value, Params > & m)

◆ as_ndarray() [2/4]

template<typename Value, typename Params>
ndarray< const Value, 1, c_order > mtl::array::as_ndarray ( const vec::dense_vector< Value, Params > & v)

◆ as_ndarray() [3/4]

template<typename Value, typename Params>
auto mtl::array::as_ndarray ( mat::dense2D< Value, Params > & m)

Create a 2D ndarray view over a dense2D matrix (zero-copy).

Works for row-major matrices; column-major uses F-order strides.

◆ as_ndarray() [4/4]

template<typename Value, typename Params>
ndarray< Value, 1, c_order > mtl::array::as_ndarray ( vec::dense_vector< Value, Params > & v)

Create a 1D ndarray view over a dense_vector (zero-copy).

◆ as_vector() [1/2]

template<typename Value, typename Order>
vec::dense_vector< const Value > mtl::array::as_vector ( const ndarray< Value, 1, Order > & a)

◆ as_vector() [2/2]

template<typename Value, typename Order>
vec::dense_vector< Value > mtl::array::as_vector ( ndarray< Value, 1, Order > & a)

Create a dense_vector view over a 1D ndarray (zero-copy).

Requires contiguous storage, and throws std::invalid_argument otherwise (#856). The either-order predicate is correct here: for N == 1 the C and F strides are both {1}, so the distinction that broke as_matrix and reshape (#359) does not exist at rank 1.

◆ broadcast_shape()

template<std::size_t N>
bool mtl::array::broadcast_shape ( const shape< N > & a,
const shape< N > & b,
shape< N > & result )
constexpr

Compute the broadcast shape of two shapes.

Follows NumPy rules: trailing dimensions aligned, size-1 axes stretch. Returns false if shapes are incompatible.

◆ broadcast_strides()

template<std::size_t N>
std::array< std::size_t, N > mtl::array::broadcast_strides ( const shape< N > & original,
const std::array< std::size_t, N > & strides,
const shape< N > & target )
constexpr

Compute broadcast strides: dimension with extent 1 gets stride 0 (repeat).

◆ c_order_strides()

template<std::size_t N>
std::array< std::size_t, N > mtl::array::c_order_strides ( const shape< N > & sh)
constexpr

Compute C-order (row-major) strides: last dimension has stride 1.

◆ compute_offset()

template<std::size_t N>
std::size_t mtl::array::compute_offset ( const std::array< std::size_t, N > & indices,
const std::array< std::size_t, N > & strides )
constexpr

Compute flat offset from multi-index and strides.

◆ compute_strides()

template<typename Order, std::size_t N>
std::array< std::size_t, N > mtl::array::compute_strides ( const shape< N > & sh)
constexpr

Compute strides for a given layout order.

◆ f_order_strides()

template<std::size_t N>
std::array< std::size_t, N > mtl::array::f_order_strides ( const shape< N > & sh)
constexpr

Compute F-order (column-major) strides: first dimension has stride 1.

◆ flatten()

template<typename V, std::size_t N, typename O>
ndarray< std::remove_cv_t< V >, 1, c_order > mtl::array::flatten ( const ndarray< V, N, O > & a)

Flatten any ndarray to a contiguous 1D ndarray (always copies for safety), in the array's own order: the same elements, in the same order, as a.reshape({a.size()}) views when a is contiguous in that order.

◆ is_contiguous()

template<std::size_t N>
bool mtl::array::is_contiguous ( const shape< N > & sh,
const std::array< std::size_t, N > & strides )
constexpr

Check if the array is contiguous in any order.

◆ is_contiguous_c()

template<std::size_t N>
bool mtl::array::is_contiguous_c ( const shape< N > & sh,
const std::array< std::size_t, N > & strides )
constexpr

Check if strides correspond to a contiguous C-order layout.

◆ is_contiguous_f()

template<std::size_t N>
bool mtl::array::is_contiguous_f ( const shape< N > & sh,
const std::array< std::size_t, N > & strides )
constexpr

Check if strides correspond to a contiguous F-order layout.

◆ max()

template<typename V, std::size_t N, typename O>
requires std::totally_ordered<std::remove_cv_t<V>>
std::remove_cv_t< V > mtl::array::max ( const ndarray< V, N, O > & a)

Maximum element, seeded from the first one. Throws on an empty array.

◆ mean()

template<typename V, std::size_t N, typename O>
std::remove_cv_t< V > mtl::array::mean ( const ndarray< V, N, O > & a)

Mean of all elements. Throws on an empty array.

◆ mean_axis()

template<typename V, std::size_t N, typename O>
requires (N > 1)
ndarray< std::remove_cv_t< V >, N - 1, O > mtl::array::mean_axis ( const ndarray< V, N, O > & a,
std::size_t axis )

Mean along a specified axis.

Throws std::invalid_argument when the axis has extent 0 (there is nothing to average).

◆ min()

template<typename V, std::size_t N, typename O>
requires std::totally_ordered<std::remove_cv_t<V>>
std::remove_cv_t< V > mtl::array::min ( const ndarray< V, N, O > & a)

Minimum element, seeded from the first one. Throws on an empty array.

◆ ndarray() [1/2]

template<typename Value, std::size_t N>
mtl::array::ndarray ( Value * ,
shape< N >  )->ndarray< Value, N, c_order >

◆ ndarray() [2/2]

template<typename Value, std::size_t N>
mtl::array::ndarray ( Value * ,
shape< N > ,
std::array< std::size_t, N >  )->ndarray< Value, N, c_order >

◆ offset_from()

template<std::size_t N, typename... Indices>
std::size_t mtl::array::offset_from ( const std::array< std::size_t, N > & strides,
Indices... indices )
constexpr

Variadic offset computation - converts (i, j, k, ...) to flat index.

◆ operator*()

template<detail_bcast::Array A, detail_bcast::Array B>
auto mtl::array::operator* ( A && a,
B && b )

◆ operator+()

template<detail_bcast::Array A, detail_bcast::Array B>
auto mtl::array::operator+ ( A && a,
B && b )

◆ operator-()

template<detail_bcast::Array A, detail_bcast::Array B>
auto mtl::array::operator- ( A && a,
B && b )

◆ operator/()

template<detail_bcast::Array A, detail_bcast::Array B>
auto mtl::array::operator/ ( A && a,
B && b )

◆ prod()

template<typename V, std::size_t N, typename O>
std::remove_cv_t< V > mtl::array::prod ( const ndarray< V, N, O > & a)

Product of all elements; one for an empty array.

◆ reduce()

template<typename V, std::size_t N, typename O, typename T, typename BinOp>
T mtl::array::reduce ( const ndarray< V, N, O > & a,
T init,
BinOp op )

Reduce all elements with a binary operation and initial value.

◆ slice() [1/2]

template<typename Value, std::size_t N, typename Order, typename... Args>
requires (sizeof...(Args) == N)
auto mtl::array::slice ( const ndarray< Value, N, Order > & arr,
Args... args )

Const overload: returns a view over const data.

◆ slice() [2/2]

template<typename Value, std::size_t N, typename Order, typename... Args>
requires (sizeof...(Args) == N)
auto mtl::array::slice ( ndarray< Value, N, Order > & arr,
Args... args )

Slice an ndarray, returning a view with potentially reduced rank.

Each argument is one of:

  • all -> keep the full dimension
  • range -> select a sub-range (adjusts shape + offset), clamped to the extent; step 0 throws
  • integer -> fix an index (reduces rank by 1); out of range throws

Returns an ndarray view with rank = N - (number of integer args). A rank-0 array takes no arguments and gives a view of itself.

◆ sum()

template<typename V, std::size_t N, typename O>
std::remove_cv_t< V > mtl::array::sum ( const ndarray< V, N, O > & a)

Sum all elements; zero for an empty array.

◆ sum_axis()

template<typename V, std::size_t N, typename O>
requires (N > 1)
ndarray< std::remove_cv_t< V >, N - 1, O > mtl::array::sum_axis ( const ndarray< V, N, O > & a,
std::size_t axis )

Sum along a specified axis, producing an ndarray of rank N-1.

An axis of extent 0 gives zeros. Throws std::out_of_range for an axis >= N.

◆ transform()

template<typename V, std::size_t N, typename O, typename F>
ndarray< std::remove_cv_t< V >, N, O > mtl::array::transform ( const ndarray< V, N, O > & a,
F && f )

Element-wise transform: apply f to every element of a, writing to out.

Works on any NdArray (ndarray, or adapted vector/matrix views).

◆ transform_inplace()

template<typename V, std::size_t N, typename O, typename F>
void mtl::array::transform_inplace ( ndarray< V, N, O > & a,
F && f )

In-place transform: apply f to every element.

Variable Documentation

◆ all

all_t mtl::array::all {}
inlineconstexpr