TRIQS/nda 2.0.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
Mathematical functions

Detailed Description

Various mathematical functions that can be applied to arrays and views.

Mathematical functions are (mostly) implemented as lazy expressions. That means they do not return the result right away but the following proxy object instead:

This lazy function call expression satisfies the nda::Array concept and can therefore be used to assign to or construct nda::basic_array and nda::basic_array_view objects. Another way to evaluate lazy expressions is with nda::make_regular:

#include <nda/nda.hpp>
#include <iostream>
int main() {
// create an array A
auto A = nda::array<int, 2>({{1, 2}, {3, 4}});
// square the elements of A
auto ex = nda::pow(A, 2); // ex is a lazy expression
// evaluate the lazy expression by constructing a new array
nda::array<int, 2> A_sq = ex;
std::cout << A_sq << std::endl;
// evaluate the lazy expression using nda::make_regular
std::cout << nda::make_regular(ex) << std::endl;
}
decltype(auto) make_regular(A &&a)
Make a given object regular.
auto pow(S s, P p)
Function pow for nda::Scalar types (uses std::pow).
basic_array< ValueType, Rank, Layout, 'A', ContainerPolicy > array
Alias template of an nda::basic_array with an 'A' algebra.
Includes all relevant headers for the core nda library.

Output:

[[1,4]
[9,16]]
[[1,4]
[9,16]]

All functions also accept scalars. For the <cmath> functions, the scalar overloads are the std ones, while nda::conj, nda::real, nda::imag, nda::abs2, nda::reciprocal, nda::max and nda::min provide their own. The exception is nda::isnan for complex scalars, which is not covered by std::isnan and is true if either component is NaN.

Classes

struct  nda::expr_call< F, As >
 A lazy function call expression on arrays/views. More...
struct  nda::mapped< F >
 Functor that is returned by the nda::map function. More...

Functions

template<Array A>
auto nda::abs (A &&a)
 Function abs for nda::Array types (lazy and coefficient-wise).
template<Scalar S>
auto nda::abs (S s)
 Function abs for nda::Scalar types (uses std::abs).
template<ArrayOrScalar A>
auto nda::abs2 (A &&a)
 Function abs2 for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).
template<Array A>
requires (get_algebra<A> != 'M')
auto nda::acos (A &&a)
 Function acos for non-matrix nda::Array types (lazy and coefficient-wise).
template<Scalar S>
auto nda::acos (S s)
 Function acos for nda::Scalar types (uses std::acos).
template<Array A>
requires (get_algebra<A> != 'M')
auto nda::asin (A &&a)
 Function asin for non-matrix nda::Array types (lazy and coefficient-wise).
template<Scalar S>
auto nda::asin (S s)
 Function asin for nda::Scalar types (uses std::asin).
template<Array A>
requires (get_algebra<A> != 'M')
auto nda::atan (A &&a)
 Function atan for non-matrix nda::Array types (lazy and coefficient-wise).
template<Scalar S>
auto nda::atan (S s)
 Function atan for nda::Scalar types (uses std::atan).
template<ArrayOrScalar A>
decltype(auto) nda::conj (A &&a)
 Function conj for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types with a complex value type).
template<Array A>
requires (get_algebra<A> != 'M')
auto nda::cos (A &&a)
 Function cos for non-matrix nda::Array types (lazy and coefficient-wise).
template<Scalar S>
auto nda::cos (S s)
 Function cos for nda::Scalar types (uses std::cos).
template<Array A>
requires (get_algebra<A> != 'M')
auto nda::cosh (A &&a)
 Function cosh for non-matrix nda::Array types (lazy and coefficient-wise).
template<Scalar S>
auto nda::cosh (S s)
 Function cosh for nda::Scalar types (uses std::cosh).
template<ArrayOfRank< 2 > M>
ArrayOfRank< 2 > auto nda::dagger (M const &m)
 Get the conjugate transpose of 2-dimensional array/view.
template<Array A>
requires (get_algebra<A> != 'M')
auto nda::exp (A &&a)
 Function exp for non-matrix nda::Array types (lazy and coefficient-wise).
template<Scalar S>
auto nda::exp (S s)
 Function exp for nda::Scalar types (uses std::exp).
template<Array A>
auto nda::floor (A &&a)
 Function floor for nda::Array types (lazy and coefficient-wise).
template<Scalar S>
auto nda::floor (S s)
 Function floor for nda::Scalar types (uses std::floor).
template<ArrayOfRank< 2 > A>
auto nda::frobenius_norm (A const &a)
 Calculate the Frobenius norm of a 2-dimensional array.
template<ArrayOrScalar A>
auto nda::imag (A &&a)
 Function imag for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).
template<typename A>
requires (Array<A> || is_complex_v<A>)
auto nda::isnan (A &&a)
 Function isnan for nda::Array types and complex nda::Scalar types (lazy and coefficient-wise for nda::Array types).
template<Scalar S>
requires (not is_complex_v<S>)
auto nda::isnan (S s)
 Function isnan for real nda::Scalar types (uses std::isnan).
template<Array A>
requires (get_algebra<A> != 'M')
auto nda::log (A &&a)
 Function log for non-matrix nda::Array types (lazy and coefficient-wise).
template<Scalar S>
auto nda::log (S s)
 Function log for nda::Scalar types (uses std::log).
template<class F>
mapped< F > nda::map (F f)
 Create a lazy function call expression on arrays/views.
template<ArrayOrScalar A, ArrayOrScalar B>
requires (((Scalar<A> && Scalar<B>) || (Array<A> && Array<B> && get_rank<A> == get_rank<B>)) && !is_complex_v<get_value_t<A>> &&
!is_complex_v<get_value_t<B>>)
auto nda::max (A &&a, B &&b)
 Function max for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).
template<ArrayOrScalar A, ArrayOrScalar B>
requires (((Scalar<A> && Scalar<B>) || (Array<A> && Array<B> && get_rank<A> == get_rank<B>)) && !is_complex_v<get_value_t<A>> &&
!is_complex_v<get_value_t<B>>)
auto nda::min (A &&a, B &&b)
 Function min for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).
template<Array A, Scalar S>
auto nda::pow (A &&a, S p)
 Function pow for nda::Array types (lazy and coefficient-wise).
template<Scalar S, Scalar P>
auto nda::pow (S s, P p)
 Function pow for nda::Scalar types (uses std::pow).
template<ArrayOrScalar A>
auto nda::real (A &&a)
 Function real for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).
template<ArrayOrScalar A>
auto nda::reciprocal (A &&a)
 Reciprocal function for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).
template<Array A>
requires (get_algebra<A> != 'M')
auto nda::sin (A &&a)
 Function sin for non-matrix nda::Array types (lazy and coefficient-wise).
template<Scalar S>
auto nda::sin (S s)
 Function sin for nda::Scalar types (uses std::sin).
template<Array A>
requires (get_algebra<A> != 'M')
auto nda::sinh (A &&a)
 Function sinh for non-matrix nda::Array types (lazy and coefficient-wise).
template<Scalar S>
auto nda::sinh (S s)
 Function sinh for nda::Scalar types (uses std::sinh).
template<Array A>
requires (get_algebra<A> != 'M')
auto nda::sqrt (A &&a)
 Function sqrt for non-matrix nda::Array types (lazy and coefficient-wise).
template<Scalar S>
auto nda::sqrt (S s)
 Function sqrt for nda::Scalar types (uses std::sqrt).
template<Array A>
requires (get_algebra<A> != 'M')
auto nda::tan (A &&a)
 Function tan for non-matrix nda::Array types (lazy and coefficient-wise).
template<Scalar S>
auto nda::tan (S s)
 Function tan for nda::Scalar types (uses std::tan).
template<Array A>
requires (get_algebra<A> != 'M')
auto nda::tanh (A &&a)
 Function tanh for non-matrix nda::Array types (lazy and coefficient-wise).
template<Scalar S>
auto nda::tanh (S s)
 Function tanh for nda::Scalar types (uses std::tanh).
template<ArrayOfRank< 2 > M>
auto nda::trace (M const &m)
 Get the trace of a 2-dimensional square array/view.

Function Documentation

◆ abs()

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

#include <nda/mapped_functions.hpp>

Function abs for nda::Array types (lazy and coefficient-wise).

Template Parameters
Anda::Array type.
Parameters
anda::Array object.
Returns
A lazy nda::expr_call object.

Definition at line 194 of file mapped_functions.hpp.

◆ abs2()

template<ArrayOrScalar A>
auto nda::abs2 ( A && a)

#include <nda/mapped_functions.hpp>

Function abs2 for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).

Template Parameters
Anda::ArrayOrScalar type..
Parameters
anda::ArrayOrScalar object.
Returns
A lazy nda::expr_call object (nda::Array) or the result of nda::detail::abs2 applied to the object (nda::Scalar).

Definition at line 244 of file mapped_functions.hpp.

◆ acos()

template<Array A>
requires (get_algebra<A> != 'M')
auto nda::acos ( A && a)

#include <nda/mapped_functions.hpp>

Function acos for non-matrix nda::Array types (lazy and coefficient-wise).

Template Parameters
Anda::Array type.
Parameters
anda::Array object.
Returns
A lazy nda::expr_call object.

Definition at line 417 of file mapped_functions.hpp.

◆ asin()

template<Array A>
requires (get_algebra<A> != 'M')
auto nda::asin ( A && a)

#include <nda/mapped_functions.hpp>

Function asin for non-matrix nda::Array types (lazy and coefficient-wise).

Template Parameters
Anda::Array type.
Parameters
anda::Array object.
Returns
A lazy nda::expr_call object.

Definition at line 436 of file mapped_functions.hpp.

◆ atan()

template<Array A>
requires (get_algebra<A> != 'M')
auto nda::atan ( A && a)

#include <nda/mapped_functions.hpp>

Function atan for non-matrix nda::Array types (lazy and coefficient-wise).

Template Parameters
Anda::Array type.
Parameters
anda::Array object.
Returns
A lazy nda::expr_call object.

Definition at line 455 of file mapped_functions.hpp.

◆ conj()

template<ArrayOrScalar A>
decltype(auto) nda::conj ( A && a)

#include <nda/mapped_functions.hpp>

Function conj for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types with a complex value type).

Template Parameters
Anda::ArrayOrScalar type.
Parameters
anda::ArrayOrScalar object.
Returns
A lazy nda::expr_call object (nda::Array and complex valued), the forwarded input object (nda::Array and not complex valued) or the complex conjugate of the scalar input.

Definition at line 112 of file mapped_functions.hpp.

◆ cos()

template<Array A>
requires (get_algebra<A> != 'M')
auto nda::cos ( A && a)

#include <nda/mapped_functions.hpp>

Function cos for non-matrix nda::Array types (lazy and coefficient-wise).

Template Parameters
Anda::Array type.
Parameters
anda::Array object.
Returns
A lazy nda::expr_call object.

Definition at line 303 of file mapped_functions.hpp.

◆ cosh()

template<Array A>
requires (get_algebra<A> != 'M')
auto nda::cosh ( A && a)

#include <nda/mapped_functions.hpp>

Function cosh for non-matrix nda::Array types (lazy and coefficient-wise).

Template Parameters
Anda::Array type.
Parameters
anda::Array object.
Returns
A lazy nda::expr_call object.

Definition at line 360 of file mapped_functions.hpp.

◆ dagger()

template<ArrayOfRank< 2 > M>
ArrayOfRank< 2 > auto nda::dagger ( M const & m)

#include <nda/matrix_functions.hpp>

Get the conjugate transpose of 2-dimensional array/view.

It first calls nda::transpose and then the lazy nda::conj function in case the array/view is complex valued.

Template Parameters
Mnda::ArrayOfRank<2> type.
Parameters
m2-dimensional array/view.
Returns
(Lazy) Conjugate transpose of the array/view.

Definition at line 81 of file matrix_functions.hpp.

◆ exp()

template<Array A>
requires (get_algebra<A> != 'M')
auto nda::exp ( A && a)

#include <nda/mapped_functions.hpp>

Function exp for non-matrix nda::Array types (lazy and coefficient-wise).

Template Parameters
Anda::Array type.
Parameters
anda::Array object.
Returns
A lazy nda::expr_call object.

Definition at line 284 of file mapped_functions.hpp.

◆ floor()

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

#include <nda/mapped_functions.hpp>

Function floor for nda::Array types (lazy and coefficient-wise).

Template Parameters
Anda::Array type.
Parameters
anda::Array object.
Returns
A lazy nda::expr_call object.

Definition at line 224 of file mapped_functions.hpp.

◆ frobenius_norm()

template<ArrayOfRank< 2 > A>
auto nda::frobenius_norm ( A const & a)

#include <nda/algorithms.hpp>

Calculate the Frobenius norm of a 2-dimensional array.

It takes the square root of the sum of squared absolute values. The return type matches std::norm applied to a scalar element.

Template Parameters
Anda::ArrayOfRank<2> type.
Parameters
aArray object.
Returns
Frobenius norm of the array/matrix.

Definition at line 184 of file algorithms.hpp.

◆ imag()

template<ArrayOrScalar A>
auto nda::imag ( A && a)

#include <nda/mapped_functions.hpp>

Function imag for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).

Template Parameters
Anda::ArrayOrScalar type.
Parameters
anda::ArrayOrScalar object.
Returns
A lazy nda::expr_call object (nda::Array) or the result of std::imag applied to the object (nda::Scalar).

Definition at line 204 of file mapped_functions.hpp.

◆ isnan()

template<typename A>
requires (Array<A> || is_complex_v<A>)
auto nda::isnan ( A && a)

#include <nda/mapped_functions.hpp>

Function isnan for nda::Array types and complex nda::Scalar types (lazy and coefficient-wise for nda::Array types).

A complex scalar is NaN if either component is NaN.

Template Parameters
Anda::Array or complex nda::Scalar type.
Parameters
anda::Array or complex nda::Scalar object.
Returns
A lazy nda::expr_call object (nda::Array) or the result of nda::detail::isnan applied to the object (complex nda::Scalar).

Definition at line 267 of file mapped_functions.hpp.

◆ log()

template<Array A>
requires (get_algebra<A> != 'M')
auto nda::log ( A && a)

#include <nda/mapped_functions.hpp>

Function log for non-matrix nda::Array types (lazy and coefficient-wise).

Template Parameters
Anda::Array type.
Parameters
anda::Array object.
Returns
A lazy nda::expr_call object.

Definition at line 474 of file mapped_functions.hpp.

◆ map()

template<class F>
mapped< F > nda::map ( F f)

#include <nda/map.hpp>

Create a lazy function call expression on arrays/views.

The callable should take the array/view elements as arguments.

Template Parameters
FCallable type.
Parameters
fCallable object.
Returns
A lazy nda::mapped object.

Definition at line 235 of file map.hpp.

◆ max()

template<ArrayOrScalar A, ArrayOrScalar B>
requires (((Scalar<A> && Scalar<B>) || (Array<A> && Array<B> && get_rank<A> == get_rank<B>)) && !is_complex_v<get_value_t<A>> &&
!is_complex_v<get_value_t<B>>)
auto nda::max ( A && a,
B && b )
nodiscard

#include <nda/mapped_functions.hpp>

Function max for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).

Template Parameters
Anda::ArrayOrScalar type.
Bnda::ArrayOrScalar type.
Parameters
aFirst operand.
bSecond operand.
Returns
A lazy nda::expr_call object (nda::Array) or the result of std::max applied to the inputs (nda::Scalar).

Definition at line 155 of file mapped_functions.hpp.

◆ min()

template<ArrayOrScalar A, ArrayOrScalar B>
requires (((Scalar<A> && Scalar<B>) || (Array<A> && Array<B> && get_rank<A> == get_rank<B>)) && !is_complex_v<get_value_t<A>> &&
!is_complex_v<get_value_t<B>>)
auto nda::min ( A && a,
B && b )
nodiscard

#include <nda/mapped_functions.hpp>

Function min for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).

Template Parameters
Anda::ArrayOrScalar type.
Bnda::ArrayOrScalar type.
Parameters
aFirst operand.
bSecond operand.
Returns
A lazy nda::expr_call object (nda::Array) or the result of std::min applied to the inputs (nda::Scalar).

Definition at line 174 of file mapped_functions.hpp.

◆ pow()

template<Array A, Scalar S>
auto nda::pow ( A && a,
S p )

#include <nda/mapped_functions.hpp>

Function pow for nda::Array types (lazy and coefficient-wise).

Template Parameters
Anda::Array type.
Snda::Scalar exponent type.
Parameters
anda::Array object.
pExponent value.
Returns
A lazy nda::expr_call object.

Definition at line 98 of file mapped_functions.hpp.

◆ real()

template<ArrayOrScalar A>
auto nda::real ( A && a)

#include <nda/mapped_functions.hpp>

Function real for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).

Template Parameters
Anda::ArrayOrScalar type..
Parameters
anda::ArrayOrScalar object.
Returns
A lazy nda::expr_call object (nda::Array) or the result of nda::detail::real applied to the object (nda::Scalar).

Definition at line 234 of file mapped_functions.hpp.

◆ reciprocal()

template<ArrayOrScalar A>
auto nda::reciprocal ( A && a)

#include <nda/mapped_functions.hpp>

Reciprocal function for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).

Template Parameters
Anda::ArrayOrScalar type.
Parameters
anda::ArrayOrScalar object.
Returns
A lazy nda::expr_call object (nda::Array) or the result of \( 1.0 / x \) applied to the object (nda::Scalar).

Definition at line 128 of file mapped_functions.hpp.

◆ sin()

template<Array A>
requires (get_algebra<A> != 'M')
auto nda::sin ( A && a)

#include <nda/mapped_functions.hpp>

Function sin for non-matrix nda::Array types (lazy and coefficient-wise).

Template Parameters
Anda::Array type.
Parameters
anda::Array object.
Returns
A lazy nda::expr_call object.

Definition at line 322 of file mapped_functions.hpp.

◆ sinh()

template<Array A>
requires (get_algebra<A> != 'M')
auto nda::sinh ( A && a)

#include <nda/mapped_functions.hpp>

Function sinh for non-matrix nda::Array types (lazy and coefficient-wise).

Template Parameters
Anda::Array type.
Parameters
anda::Array object.
Returns
A lazy nda::expr_call object.

Definition at line 379 of file mapped_functions.hpp.

◆ sqrt()

template<Array A>
requires (get_algebra<A> != 'M')
auto nda::sqrt ( A && a)

#include <nda/mapped_functions.hpp>

Function sqrt for non-matrix nda::Array types (lazy and coefficient-wise).

Template Parameters
Anda::Array type.
Parameters
anda::Array object.
Returns
A lazy nda::expr_call object.

Definition at line 493 of file mapped_functions.hpp.

◆ tan()

template<Array A>
requires (get_algebra<A> != 'M')
auto nda::tan ( A && a)

#include <nda/mapped_functions.hpp>

Function tan for non-matrix nda::Array types (lazy and coefficient-wise).

Template Parameters
Anda::Array type.
Parameters
anda::Array object.
Returns
A lazy nda::expr_call object.

Definition at line 341 of file mapped_functions.hpp.

◆ tanh()

template<Array A>
requires (get_algebra<A> != 'M')
auto nda::tanh ( A && a)

#include <nda/mapped_functions.hpp>

Function tanh for non-matrix nda::Array types (lazy and coefficient-wise).

Template Parameters
Anda::Array type.
Parameters
anda::Array object.
Returns
A lazy nda::expr_call object.

Definition at line 398 of file mapped_functions.hpp.

◆ trace()

template<ArrayOfRank< 2 > M>
auto nda::trace ( M const & m)

#include <nda/matrix_functions.hpp>

Get the trace of a 2-dimensional square array/view.

Template Parameters
Mnda::ArrayOfRank<2> type.
Parameters
m2-dimensional array/view.
Returns
Sum of the diagonal elements of the array/view.

Definition at line 60 of file matrix_functions.hpp.