TRIQS/nda 1.3.0
Multi-dimensional array library for C++
|
#include <nda/basic_array.hpp>
A generic multi-dimensional array.
Together with nda::basic_array_view, this class forms the backbone of the nda library. It is templated with the following parameters:
ValueType
: This is the type of the elements stored in the array. Most of the time, this will be a scalar type like an int, double or std::complex<double>, but it can also be a more complex type like a custom class or a another nda::basic_array.Rank
: Integer specifying the number of dimensions of the array. This is a compile-time constant.LayoutPolicy
: The layout policy specifies how the array views the memory it uses and how it accesses its elements. It provides a mapping from multi-dimensional to linear indices and vice versa (see Layout policies).Algebra
: The algebra specifies how an array behaves when it is used in an expression. Possible values are 'A' (array), 'M' (matrix) and 'V' (vector) (see nda::get_algebra).ContainerPolicy
: The container policy specifies how and where the data is stored. It is responsible for allocating/deallocating the memory (see Handles).In contrast to views (see nda::basic_array_view), regular arrays own the memory they use for data storage.
Output:
Arrays and views share a lot of the same operations and functionalities. To turn a view into a regular array, use nda::make_regular.
ValueType | Type stored in the array. |
Rank | Number of dimensions of the array. |
LayoutPolicy | Policy determining the memory layout. |
Algebra | Algebra of the array. |
ContainerPolicy | Policy determining how and where the data is stored. |
Definition at line 113 of file basic_array.hpp.
Public Types | |
using | const_iterator = array_iterator<iterator_rank, ValueType const, typename AccessorPolicy::template accessor<ValueType>::pointer> |
Const iterator type of the view/array. | |
using | container_policy_t = ContainerPolicy |
Type of the container policy (see Memory policies). | |
using | iterator = array_iterator<iterator_rank, ValueType, typename AccessorPolicy::template accessor<ValueType>::pointer> |
Iterator type of the view/array. | |
using | layout_policy_t = LayoutPolicy |
Type of the memory layout policy (see Layout policies). | |
using | layout_t = typename LayoutPolicy::template mapping<Rank> |
Type of the memory layout (an nda::idx_map). | |
using | regular_type = basic_array |
The associated regular type. | |
using | storage_t = typename ContainerPolicy::template handle<ValueType> |
Type of the memory handle (see Handles). | |
using | value_type = ValueType |
Type of the values in the array (can not be const). | |
Public Member Functions | |
basic_array () | |
Default constructor constructs an empty array with a default constructed memory handle and layout. | |
template<ArrayOfRank< Rank > A> requires (HasValueTypeConstructibleFrom<A, value_type>) | |
basic_array (A const &a) | |
Construct an array from an nda::ArrayOfRank object with the same rank by copying each element. | |
basic_array (basic_array &&)=default | |
Default move constructor moves the memory handle and layout. | |
basic_array (basic_array const &a)=default | |
Default copy constructor copies the memory handle and layout. | |
template<char A2> requires (Rank == 2) | |
basic_array (basic_array< ValueType, 2, LayoutPolicy, A2, ContainerPolicy > &&a) noexcept | |
Construct a 2-dimensional array from another 2-dimensional array with a different algebra. | |
template<char A, typename CP > | |
basic_array (basic_array< ValueType, Rank, LayoutPolicy, A, CP > a) noexcept | |
Construct an array from another array with a different algebra and/or container policy. | |
template<ArrayInitializer< basic_array > Initializer> | |
basic_array (Initializer const &initializer) | |
Construct an array from an nda::ArrayInitializer object. | |
template<std::integral Int, typename RHS > requires ((Rank == 1 and is_scalar_for_v<RHS, basic_array>)) | |
basic_array (Int sz, RHS const &val) | |
Construct a 1-dimensional array with the given size and initialize each element to the given scalar value. | |
template<std::integral... Ints> requires (sizeof...(Ints) == Rank) | |
basic_array (Ints... is) | |
Construct an array with the given dimensions. | |
basic_array (layout_t const &layout) | |
Construct an array with the given memory layout. | |
basic_array (layout_t const &layout, storage_t &&storage) noexcept | |
Construct an array with the given memory layout and with an existing memory handle/storage. | |
template<std::integral Int = long> requires (std::is_default_constructible_v<ValueType>) | |
basic_array (std::array< Int, Rank > const &shape) | |
Construct an array with the given shape. | |
basic_array (std::initializer_list< std::initializer_list< std::initializer_list< ValueType > > > const &l3) | |
Construct a 3-dimensional array from a triple nested initializer list. | |
basic_array (std::initializer_list< std::initializer_list< ValueType > > const &l2) | |
Construct a 2-dimensional array from a double nested initializer list. | |
basic_array (std::initializer_list< ValueType > const &l) | |
Construct a 1-dimensional array from an initializer list. | |
auto | as_array_view () |
Convert the current array to a view with an 'A' (array) algebra. | |
auto | as_array_view () const |
Convert the current array to a view with an 'A' (array) algebra. | |
const_iterator | begin () const noexcept |
Get a const iterator to the beginning of the view/array. | |
iterator | begin () noexcept |
Get an iterator to the beginning of the view/array. | |
const_iterator | cbegin () const noexcept |
Get a const iterator to the beginning of the view/array. | |
const_iterator | cend () const noexcept |
Get a const iterator to the end of the view/array. | |
ValueType const * | data () const noexcept |
Get a pointer to the actual data (in general this is not the beginning of the memory block for a view). | |
ValueType * | data () noexcept |
Get a pointer to the actual data (in general this is not the beginning of thr memory block for a view). | |
bool | empty () const |
Is the view/array empty? | |
const_iterator | end () const noexcept |
Get a const iterator to the end of the view/array. | |
iterator | end () noexcept |
Get an iterator to the end of the view/array. | |
long | extent (int i) const noexcept |
Get the extent of the ith dimension. | |
constexpr auto const & | indexmap () const noexcept |
Get the memory layout of the view/array. | |
auto | indices () const noexcept |
Get a range that generates all valid index tuples. | |
long | is_contiguous () const noexcept |
Is the memory layout of the view/array contiguous? | |
bool | is_empty () const noexcept |
decltype(auto) | operator() (_linear_index_t idx) const noexcept |
Access the element of the view/array at the given nda::_linear_index_t. | |
decltype(auto) | operator() (_linear_index_t idx) noexcept |
Non-const overload of nda::basic_array_view::operator()(_linear_index_t) const. | |
template<typename... Ts> | |
__inline__ decltype(auto) | operator() (Ts const &...idxs) &&noexcept(has_no_boundcheck) |
Rvalue overload of nda::basic_array_view::operator()(Ts const &...) const & . | |
template<typename... Ts> | |
__inline__ decltype(auto) | operator() (Ts const &...idxs) &noexcept(has_no_boundcheck) |
Non-const overload of nda::basic_array_view::operator()(Ts const &...) const & . | |
template<typename... Ts> | |
__inline__ decltype(auto) | operator() (Ts const &...idxs) const &noexcept(has_no_boundcheck) |
Function call operator to access the view/array. | |
template<typename RHS > | |
auto & | operator*= (RHS const &rhs) noexcept |
Multiplication assignment operator. | |
template<typename RHS > | |
auto & | operator+= (RHS const &rhs) noexcept |
Addition assignment operator. | |
template<typename RHS > | |
auto & | operator-= (RHS const &rhs) noexcept |
Subtraction assignment operator. | |
template<typename RHS > | |
auto & | operator/= (RHS const &rhs) noexcept |
Division assignment operator. | |
basic_array & | operator= (basic_array &&)=default |
Default move assignment moves the memory handle and layout from the right hand side array. | |
basic_array & | operator= (basic_array const &)=default |
Default copy assignment copies the memory handle and layout from the right hand side array. | |
template<char A, typename CP > | |
basic_array & | operator= (basic_array< ValueType, Rank, LayoutPolicy, A, CP > const &rhs) |
Assignment operator makes a deep copy of another array with a different algebra and/or container policy. | |
template<ArrayInitializer< basic_array > Initializer> | |
basic_array & | operator= (Initializer const &initializer) |
Assignment operator uses an nda::ArrayInitializer to assign to the array. | |
template<std::ranges::contiguous_range R> requires (Rank == 1 and not MemoryArray<R>) | |
auto & | operator= (R const &rhs) noexcept |
Assignment operator makes a deep copy of a general contiguous range and assigns it to the 1-dimensional view/array. | |
template<ArrayOfRank< Rank > RHS> | |
basic_array & | operator= (RHS const &rhs) |
Assignment operator makes a deep copy of an nda::ArrayOfRank object. | |
template<typename RHS > requires (is_scalar_for_v<RHS, basic_array>) | |
basic_array & | operator= (RHS const &rhs) noexcept |
Assignment operator assigns a scalar to the array. | |
template<typename T > | |
decltype(auto) | operator[] (T const &idx) const &noexcept(has_no_boundcheck) |
Subscript operator to access the 1-dimensional view/array. | |
template<typename T > | |
decltype(auto) | operator[] (T const &x) &&noexcept(has_no_boundcheck) |
Rvalue overload of nda::basic_array_view::operator[](T const &) const & . | |
template<typename T > | |
decltype(auto) | operator[] (T const &x) &noexcept(has_no_boundcheck) |
Non-const overload of nda::basic_array_view::operator[](T const &) const & . | |
template<std::integral... Ints> | |
void | resize (Ints const &...is) |
Resize the array to a new shape. | |
void | resize (std::array< long, Rank > const &shape) |
Resize the array to a new shape. | |
auto const & | shape () const noexcept |
Get the shape of the view/array. | |
long | shape (int i) const noexcept |
long | size () const noexcept |
Get the total size of the view/array. | |
storage_t | storage () &&noexcept |
Get the data storage of the view/array. | |
storage_t & | storage () &noexcept |
Get the data storage of the view/array. | |
storage_t const & | storage () const &noexcept |
Get the data storage of the view/array. | |
constexpr auto | stride_order () const noexcept |
Get the stride order of the memory layout of the view/array (see nda::idx_map for more details on how we define stride orders). | |
auto const & | strides () const noexcept |
Get the strides of the view/array (see nda::idx_map for more details on how we define strides). | |
auto | transpose () |
auto | transpose () const |
Static Public Member Functions | |
template<char ResultAlgebra, bool SelfIsRvalue, typename Self , typename... Ts> | |
static __inline__ decltype(auto) | call (Self &&self, Ts const &...idxs) noexcept(has_no_boundcheck) |
Implementation of the function call operator. | |
static constexpr bool | is_stride_order_C () noexcept |
Is the stride order of the view/array in C-order? | |
static constexpr bool | is_stride_order_Fortran () noexcept |
Is the stride order of the view/array in Fortran-order? | |
template<std::integral... Ints> requires (sizeof...(Ints) == Rank) | |
static basic_array | ones (Ints... is) |
Make a one-initialized array with the given dimensions. | |
template<std::integral Int = long> requires (nda::is_scalar_v<ValueType>) | |
static basic_array | ones (std::array< Int, Rank > const &shape) |
Make a one-initialized array with the given shape. | |
template<std::integral... Ints> requires (sizeof...(Ints) == Rank) | |
static basic_array | rand (Ints... is) |
Make a random-initialized array with the given dimensions. | |
template<std::integral Int = long> requires (std::is_floating_point_v<ValueType> or nda::is_complex_v<ValueType>) | |
static basic_array | rand (std::array< Int, Rank > const &shape) |
Make a random-initialized array with the given shape. | |
template<std::integral... Ints> requires (sizeof...(Ints) == Rank) | |
static basic_array | zeros (Ints... is) |
Make a zero-initialized array with the given dimensions. | |
template<std::integral Int = long> requires (std::is_standard_layout_v<ValueType> && std::is_trivially_copyable_v<ValueType>) | |
static basic_array | zeros (std::array< Int, Rank > const &shape) |
Make a zero-initialized array with the given shape. | |
Static Public Attributes | |
static constexpr int | iterator_rank = (has_strided_1d(layout_t::layout_prop) ? 1 : Rank) |
Rank of the nda::array_iterator for the view/array. | |
static constexpr int | rank = Rank |
Number of dimensions of the array. | |
|
inlineexplicitnoexcept |
Construct an array from another array with a different algebra and/or container policy.
It takes the memory layout of the other array and copies the data from the memory handle.
A | Algebra of the other array. |
CP | Container policy of the other array. |
a | Other array. |
Definition at line 217 of file basic_array.hpp.
|
inlineexplicit |
Construct an array with the given dimensions.
The integer type must be convertible to long and there must be exactly Rank
arguments. It depends on the value type and the container policy whether the data is initialized with zeros or not.
Ints | Integer types. |
is | Extent (number of elements) along each dimension. |
Definition at line 230 of file basic_array.hpp.
|
inlineexplicit |
Construct a 1-dimensional array with the given size and initialize each element to the given scalar value.
Int | Integer type. |
RHS | Type of the scalar value to initialize the array with. |
sz | Size of the array. |
val | Value to initialize the array with. |
Definition at line 245 of file basic_array.hpp.
|
inlineexplicit |
Construct an array with the given shape.
It depends on the value type and the container policy whether the data is initialized with zeros or not.
Int | Integer type. |
shape | Shape of the array. |
Definition at line 260 of file basic_array.hpp.
|
inlineexplicit |
Construct an array with the given memory layout.
It depends on the value type and the container policy whether the data is initialized with zeros or not.
layout | Memory layout. |
Definition at line 271 of file basic_array.hpp.
|
inlineexplicitnoexcept |
Construct an array with the given memory layout and with an existing memory handle/storage.
The memory handle is moved and the layout is copied into the array.
layout | Memory layout. |
storage | Memory handle/storage. |
Definition at line 283 of file basic_array.hpp.
|
inline |
Construct an array from an nda::ArrayOfRank object with the same rank by copying each element.
A | nda::ArrayOfRank type. |
a | nda::ArrayOfRank object. |
Definition at line 293 of file basic_array.hpp.
|
inline |
Construct an array from an nda::ArrayInitializer object.
An nda::ArrayInitializer is typically returned by delayed operations (see e.g. nda::mpi_gather). The constructor can then be used to create the resulting array.
Initializer | nda::ArrayInitializer type. |
initializer | nda::ArrayInitializer object. |
Definition at line 314 of file basic_array.hpp.
|
inline |
Construct a 1-dimensional array from an initializer list.
l | Initializer list. |
Definition at line 336 of file basic_array.hpp.
|
inline |
Construct a 2-dimensional array from a double nested initializer list.
l2 | Initializer list. |
Definition at line 347 of file basic_array.hpp.
|
inline |
Construct a 3-dimensional array from a triple nested initializer list.
l3 | Initializer list. |
Definition at line 362 of file basic_array.hpp.
|
inlineexplicitnoexcept |
Construct a 2-dimensional array from another 2-dimensional array with a different algebra.
The given array is moved into the constructed array. Note that for nda::stack_array or other array types, this might still involve a copy of the data.
A2 | Algebra of the given array. |
a | Other array. |
Definition at line 387 of file basic_array.hpp.
|
inline |
Convert the current array to a view with an 'A' (array) algebra.
Definition at line 176 of file basic_array.hpp.
|
inline |
Convert the current array to a view with an 'A' (array) algebra.
Definition at line 182 of file basic_array.hpp.
|
inlinestaticnoexcept |
Implementation of the function call operator.
This function is an implementation detail an should be private. Since the Green's function library in TRIQS uses this function, it is kept public (for now).
ResultAlgebra | Algebra of the resulting view/array. |
SelfIsRvalue | True if the view/array is an rvalue. |
Self | Type of the calling view/array. |
T | Types of the arguments. |
self | Calling view. |
idxs | Multi-dimensional index consisting of long , nda::range , nda::range::all_t , nda::ellipsis or lazy arguments. |
Definition at line 777 of file basic_array.hpp.
|
inlinenodiscardnoexcept |
Get a pointer to the actual data (in general this is not the beginning of the memory block for a view).
Definition at line 647 of file basic_array.hpp.
|
inlinenodiscardnoexcept |
Get a pointer to the actual data (in general this is not the beginning of thr memory block for a view).
Definition at line 653 of file basic_array.hpp.
|
inlinenodiscard |
Is the view/array empty?
Definition at line 683 of file basic_array.hpp.
|
inlinenodiscardnoexcept |
Get the extent of the ith dimension.
Definition at line 692 of file basic_array.hpp.
|
inlinenodiscardconstexprnoexcept |
Get the memory layout of the view/array.
Definition at line 615 of file basic_array.hpp.
|
inlinenodiscardnoexcept |
Get a range that generates all valid index tuples.
itertools::multiplied
range that can be used to iterate over all valid index tuples. Definition at line 709 of file basic_array.hpp.
|
inlinenodiscardnoexcept |
Is the memory layout of the view/array contiguous?
Definition at line 677 of file basic_array.hpp.
|
inlinenodiscardnoexcept |
Definition at line 686 of file basic_array.hpp.
|
inlinestaticconstexprnoexcept |
Is the stride order of the view/array in C-order?
Definition at line 715 of file basic_array.hpp.
|
inlinestaticconstexprnoexcept |
Is the stride order of the view/array in Fortran-order?
Definition at line 721 of file basic_array.hpp.
|
inlinestatic |
Make a one-initialized array with the given dimensions.
Ints | Integer types. |
is | Extent (number of elements) along each dimension. |
Definition at line 443 of file basic_array.hpp.
|
inlinestatic |
Make a one-initialized array with the given shape.
Int | Integer type. |
shape | Shape of the array. |
Definition at line 427 of file basic_array.hpp.
|
inlinenoexcept |
Access the element of the view/array at the given nda::_linear_index_t.
The linear index specifies the position of the element in the view/array and not the position of the element w.r.t. to the data pointer (i.e. any possible strides should not be taken into account).
idx | nda::_linear_index_t object. |
Definition at line 732 of file basic_array.hpp.
|
inlinenoexcept |
Function call operator to access the view/array.
Depending on the type of the calling object and the given arguments, this function call does the following:
long
, a single element is accessed:Ts | Types of the function arguments. |
idxs | Multi-dimensional index consisting of long , nda::range , nda::range::all_t , nda::ellipsis or lazy arguments. |
Definition at line 842 of file basic_array.hpp.
|
inlinenoexcept |
Multiplication assignment operator.
It first performs the (lazy) multiplication with the right hand side operand and then assigns the result to the left hand side view/array.
See nda::operator*(L &&, R &&) and nda::operator*(A &&, S &&) for more details.
RHS | nda::Scalar or nda::Array type. |
rhs | Right hand side operand of the multiplication assignment operation. |
Definition at line 998 of file basic_array.hpp.
|
inlinenoexcept |
Addition assignment operator.
It first performs the (lazy) addition with the right hand side operand and then assigns the result to the left hand side view/array.
See nda::operator+(L &&, R &&) and nda::operator+(A &&, S &&) for more details.
RHS | nda::Scalar or nda::Array type. |
rhs | Right hand side operand of the addition assignment operation. |
Definition at line 962 of file basic_array.hpp.
|
inlinenoexcept |
Subtraction assignment operator.
It first performs the (lazy) subtraction with the right hand side operand and then assigns the result to the left hand side view/array.
See nda::operator-(L &&, R &&) and nda::operator-(A &&, S &&) for more details.
RHS | nda::Scalar or nda::Array type. |
rhs | Right hand side operand of the subtraction assignment operation. |
Definition at line 980 of file basic_array.hpp.
|
inlinenoexcept |
Division assignment operator.
It first performs the (lazy) division with the right hand side operand and then assigns the result to the left hand side view/array.
See nda::operator/(L &&, R &&) and nda::operator/(A &&, S &&) for more details.
RHS | nda::Scalar or nda::Array type. |
rhs | Right hand side operand of the division assignment operation. |
Definition at line 1016 of file basic_array.hpp.
|
inline |
Assignment operator makes a deep copy of another array with a different algebra and/or container policy.
A new array object is constructed from the right hand side array and then moved into the current array. This will invalidate all references/views to the existing storage.
A | Algebra of the other array. |
CP | Container policy of the other array. |
rhs | Right hand side of the assignment operation. |
Definition at line 508 of file basic_array.hpp.
|
inline |
Assignment operator uses an nda::ArrayInitializer to assign to the array.
The array is resized to the shape of the initializer. This might invalidate all references/views to the existing storage.
Initializer | nda::ArrayInitializer type. |
initializer | Initializer object. |
Definition at line 557 of file basic_array.hpp.
|
inlinenoexcept |
Assignment operator makes a deep copy of a general contiguous range and assigns it to the 1-dimensional view/array.
R | Range type. |
rhs | Right hand side range object. |
Definition at line 1030 of file basic_array.hpp.
|
inline |
Assignment operator makes a deep copy of an nda::ArrayOfRank object.
The array is first resized to the shape of the right hand side and the elements are copied. This might invalidate all references/views to the existing storage.
RHS | nda::ArrayOfRank type. |
rhs | Right hand side of the assignment operation. |
Definition at line 523 of file basic_array.hpp.
|
inlinenoexcept |
Assignment operator assigns a scalar to the array.
The behavior depends on the algebra of the array:
RHS | Type of the scalar. |
rhs | Right hand side of the assignment operation. |
Definition at line 540 of file basic_array.hpp.
|
inlinenoexcept |
Subscript operator to access the 1-dimensional view/array.
Depending on the type of the calling object and the given argument, this subscript operation does the following:
long
, a single element is accessed:T | Type of the argument. |
idx | 1-dimensional index that is either a long , nda::range , nda::range::all_t , nda::ellipsis or a lazy argument. |
Definition at line 882 of file basic_array.hpp.
|
inlinestatic |
Make a random-initialized array with the given dimensions.
The random values are take from a uniform distribution over [0, 1). For a complex array, both real and imaginary parts are initialized with random values.
Ints | Integer types. |
is | Extent (number of elements) along each dimension. |
Definition at line 485 of file basic_array.hpp.
|
inlinestatic |
Make a random-initialized array with the given shape.
The random values are take from a uniform distribution over [0, 1). For a complex array, both real and imaginary parts are initialized with random values.
Int | Integer type. |
shape | Shape of the array. |
Definition at line 460 of file basic_array.hpp.
|
inline |
Resize the array to a new shape.
Resizing is only performed if the storage is not null and if the new size is different from the previous size. If resizing is performed, the content of the resulting array is undefined since it makes no copy of the previous data and all references/views to the existing storage will be invalidated.
Ints | Integer types. |
is | New extent (number of elements) along each dimension. |
Definition at line 574 of file basic_array.hpp.
|
inline |
Resize the array to a new shape.
Resizing is only performed if the storage is not null and if the new size is different from the previous size. If resizing is performed, the content of the resulting array is undefined since it makes no copy of the previous data and all references/views to the existing storage will be invalidated.
shape | New shape of the array. |
Definition at line 589 of file basic_array.hpp.
|
inlinenodiscardnoexcept |
Get the shape of the view/array.
std::array<long, Rank>
object specifying the shape of the view/array. Definition at line 659 of file basic_array.hpp.
|
inlinenodiscardnoexcept |
extent(i)
or shape()[i]
instead. Definition at line 703 of file basic_array.hpp.
|
inlinenodiscardnoexcept |
Get the total size of the view/array.
Definition at line 671 of file basic_array.hpp.
|
inlinenodiscardnoexcept |
Get the data storage of the view/array.
Definition at line 633 of file basic_array.hpp.
|
inlinenodiscardnoexcept |
Get the data storage of the view/array.
Definition at line 627 of file basic_array.hpp.
|
inlinenodiscardnoexcept |
Get the data storage of the view/array.
Definition at line 621 of file basic_array.hpp.
|
inlinenodiscardconstexprnoexcept |
Get the stride order of the memory layout of the view/array (see nda::idx_map for more details on how we define stride orders).
std::array<int, Rank>
object specifying the stride order. Definition at line 641 of file basic_array.hpp.
|
inlinenodiscardnoexcept |
Get the strides of the view/array (see nda::idx_map for more details on how we define strides).
std::array<long, Rank>
object specifying the strides of the view/array. Definition at line 665 of file basic_array.hpp.
|
inline |
Definition at line 185 of file basic_array.hpp.
|
inline |
Definition at line 192 of file basic_array.hpp.
|
inlinestatic |
Make a zero-initialized array with the given dimensions.
Ints | Integer types. |
is | Extent (number of elements) along each dimension. |
Definition at line 413 of file basic_array.hpp.
|
inlinestatic |
Make a zero-initialized array with the given shape.
Int | Integer type. |
shape | Shape of the array. |
Definition at line 399 of file basic_array.hpp.