TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
Algorithms

Detailed Description

Various algorithms that can be applied to arrays and views.

We can group the different algorithms into

Functions

template<Array A>
bool nda::all (A const &a)
 Do all elements of the array evaluate to true?
template<Array A>
bool nda::any (A const &a)
 Does any of the elements of the array evaluate to true?
template<Array A, typename F>
auto nda::fold (F f, A const &a)
 The same as nda::fold, except that the initial value is a default constructed value type of the array.
template<Array A, typename F, typename R>
auto nda::fold (F f, A const &a, R r)
 Perform a fold operation on the given nda::Array object.
template<Array A, Array B>
constexpr auto nda::hadamard (A &&a, B &&b)
 Hadamard product of two nda::Array objects.
constexpr auto nda::hadamard (nda::Scalar auto a, nda::Scalar auto b)
 Hadamard product of two arithmetic types.
template<typename T, typename U, size_t R>
constexpr auto nda::hadamard (std::array< T, R > const &a, std::array< U, R > const &b)
 Hadamard product of two std::array objects.
template<typename T, typename U>
constexpr auto nda::hadamard (std::vector< T > const &a, std::vector< U > const &b)
 Hadamard product of two std::vector objects.
template<Array A>
auto nda::max_element (A const &a)
 Find the maximum element of an array.
template<Array A>
auto nda::min_element (A const &a)
 Find the minimum element of an array.
template<Array A, typename Value = get_value_t<A>>
requires (nda::Scalar<Value> or nda::Array<Value>)
auto nda::product (A const &a)
 Multiply all the elements of an nda::Array object.
template<Array A, typename Value = get_value_t<A>>
requires (nda::Scalar<Value> or nda::Array<Value>)
auto nda::sum (A const &a)
 Sum all the elements of an nda::Array object.
template<Array A>
auto nda::sum (A const &a, int axis)
 Sum elements of an nda::Array along a specified axis.
template<Array A, std::integral I, size_t N>
auto nda::sum (A const &a, std::array< I, N > axes)
 Sum elements of an nda::Array along specified axes.

Function Documentation

◆ all()

template<Array A>
bool nda::all ( A const & a)

#include <nda/algorithms.hpp>

Do all elements of the array evaluate to true?

The given nda::Array object can also be some lazy expression that evaluates to a boolean. For example:

auto greater0 = nda::map([](auto x) { return x > 0.0; })(A);
auto res = nda::all(greater0);
static basic_array rand(std::array< Int, Rank > const &shape)
bool all(A const &a)
Do all elements of the array evaluate to true?
mapped< F > map(F f)
Create a lazy function call expression on arrays/views.
Definition map.hpp:206
Template Parameters
Anda::Array type.
Parameters
anda::Array object.
Returns
True if all elements of the array evaluate to true, false otherwise.

Definition at line 110 of file algorithms.hpp.

◆ any()

template<Array A>
bool nda::any ( A const & a)

#include <nda/algorithms.hpp>

Does any of the elements of the array evaluate to true?

The given nda::Array object can also be some lazy expression that evaluates to a boolean. For example:

auto greater05 = nda::map([](auto x) { return x > 0.5; })(A);
auto res = nda::any(greater05);
bool any(A const &a)
Does any of the elements of the array evaluate to true?
Template Parameters
Anda::Array type.
Parameters
anda::Array object.
Returns
True if at least one element of the array evaluates to true, false otherwise.

Definition at line 89 of file algorithms.hpp.

◆ fold()

template<Array A, typename F, typename R>
auto nda::fold ( F f,
A const & a,
R r )

#include <nda/algorithms.hpp>

Perform a fold operation on the given nda::Array object.

It calculates the following (where r is an initial value);

auto res = f(...f(f(f(r, a(0,...,0)), a(0,...,1)), a(0,...,2)), ...);
Note
The array is always traversed in C-order.
Template Parameters
Anda::Array type.
FCallable type.
RType of the initial value.
Parameters
fCallable object taking two arguments compatible with the initial value and the array value type.
anda::Array object.
rInitial value.
Returns
Result of the fold operation.

Definition at line 59 of file algorithms.hpp.

◆ hadamard() [1/4]

template<Array A, Array B>
auto nda::hadamard ( A && a,
B && b )
nodiscardconstexpr

#include <nda/algorithms.hpp>

Hadamard product of two nda::Array objects.

Template Parameters
Anda::Array type.
Bnda::Array type.
Parameters
anda::Array object.
bnda::Array object.
Returns
A lazy nda::expr_call object representing the elementwise product of the two input objects.

Definition at line 293 of file algorithms.hpp.

◆ hadamard() [2/4]

auto nda::hadamard ( nda::Scalar auto a,
nda::Scalar auto b )
constexpr

#include <nda/algorithms.hpp>

Hadamard product of two arithmetic types.

Template Parameters
Tnda::Scalar type of the first input.
Unda::Scalar type of the second input.
Parameters
aFirst input.
bSecond input.
Returns
Product of the two inputs.

Definition at line 340 of file algorithms.hpp.

◆ hadamard() [3/4]

template<typename T, typename U, size_t R>
auto nda::hadamard ( std::array< T, R > const & a,
std::array< U, R > const & b )
nodiscardconstexpr

#include <nda/algorithms.hpp>

Hadamard product of two std::array objects.

Template Parameters
TValue type of the first array.
UValue type of the second array.
RSize of the arrays.
Parameters
astd::array object.
bstd::array object.
Returns
std::array containing the elementwise product of the two input arrays.

Definition at line 308 of file algorithms.hpp.

◆ hadamard() [4/4]

template<typename T, typename U>
auto nda::hadamard ( std::vector< T > const & a,
std::vector< U > const & b )
nodiscardconstexpr

#include <nda/algorithms.hpp>

Hadamard product of two std::vector objects.

Template Parameters
TValue type of the first input vector.
UValue type of the second input vector.
Parameters
astd::vector object.
bstd::vector object.
Returns
std::vector containing the elementwise product of the two input vectors.

Definition at line 322 of file algorithms.hpp.

◆ max_element()

template<Array A>
auto nda::max_element ( A const & a)

#include <nda/algorithms.hpp>

Find the maximum element of an array.

It uses nda::fold and std::max.

Template Parameters
Anda::Array type.
Parameters
anda::Array object.
Returns
Maximum element of the array.

Definition at line 125 of file algorithms.hpp.

◆ min_element()

template<Array A>
auto nda::min_element ( A const & a)

#include <nda/algorithms.hpp>

Find the minimum element of an array.

It uses nda::fold and std::min.

Template Parameters
Anda::Array type.
Parameters
anda::Array object.
Returns
Minimum element of the array.

Definition at line 144 of file algorithms.hpp.

◆ product()

template<Array A, typename Value = get_value_t<A>>
requires (nda::Scalar<Value> or nda::Array<Value>)
auto nda::product ( A const & a)

#include <nda/algorithms.hpp>

Multiply all the elements of an nda::Array object.

Template Parameters
Anda::Array type.
Parameters
anda::Array object.
Returns
Product of all elements.

Definition at line 272 of file algorithms.hpp.

◆ sum() [1/3]

template<Array A, typename Value = get_value_t<A>>
requires (nda::Scalar<Value> or nda::Array<Value>)
auto nda::sum ( A const & a)

#include <nda/algorithms.hpp>

Sum all the elements of an nda::Array object.

Template Parameters
Anda::Array type.
Parameters
anda::Array object.
Returns
Sum of all elements.

Definition at line 179 of file algorithms.hpp.

◆ sum() [2/3]

template<Array A>
auto nda::sum ( A const & a,
int axis )

#include <nda/algorithms.hpp>

Sum elements of an nda::Array along a specified axis.

It simply calls nda::sum(A const &, std::array<I, N>).

Template Parameters
Anda::Array type.
Parameters
anda::Array object.
axisThe axis along which to sum.
Returns
An nda::array with rank reduced by 1 or a scalar if the original array has rank 1.

Definition at line 260 of file algorithms.hpp.

◆ sum() [3/3]

template<Array A, std::integral I, size_t N>
auto nda::sum ( A const & a,
std::array< I, N > axes )

#include <nda/algorithms.hpp>

Sum elements of an nda::Array along specified axes.

This function behaves similar to numpy.sum. The result is an array with rank reduced by the number of axes summed over, i.e. if the original array has rank \( R \) and we sum over \( N \) axes, the resulting array has rank \( R - N \).

If all axes are summed over, \( R = N \), the call is dispatched to nda::sum.

If no axes are specified, \( N = 0 \), a copy of the input array is returned.

The given axes are expected to be unique and in the range \( [0, R-1] \).

Template Parameters
Anda::Array type.
NNumber of axes to sum over.
Parameters
anda::Array object.
axesArray of axis indices to sum over.
Returns
An nda::basic_array with rank reduced by \( N \), or a scalar if all axes are summed.

Definition at line 210 of file algorithms.hpp.