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

Detailed Description

All the usual arithmetic operations that can be applied to arrays and views.

Arithmetic operations are (mostly) implemented as lazy expressions. That means they do not return the result right away but one of the following proxy objects instead:

These lazy expressions all satisfy 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:

// create two arrays A and B
auto A = nda::array<int, 2>({{1, 2}, {3, 4}});
auto B = nda::array<int, 2>({{5, 6}, {7, 8}});
// add them elementwise
auto ex = A + B; // C is an nda::expr object
// evaluate the lazy expression by constructing a new array
// evaluate the lazy expression using nda::make_regular
auto D = nda::make_regular(ex);
A generic multi-dimensional array.
decltype(auto) make_regular(A &&a)
Make a given object regular.

Note that the behavior of the arithmetic operations depends on the algebra of the involved array or view types:

// multiply two arrays elementwise
auto A1 = nda::array<int, 2>({{1, 2}, {3, 4}});
auto A2 = nda::array<int, 2>({{5, 6}, {7, 8}});
nda::array<int, 2> A3 = A1 * A2;
std::cout << A3 << std::endl;
// multiply two matrices
auto M1 = nda::matrix<int>({{1, 2}, {3, 4}});
auto M2 = nda::matrix<int>({{5, 6}, {7, 8}});
nda::matrix<int> M3 = M1 * M2;
std::cout << M3 << std::endl;

Output:

[[5,12]
[21,32]]
[[19,22]
[43,50]]

Classes

struct  nda::expr< OP, L, R >
 Lazy binary expression for nda::ArrayOrScalar types. More...
 
struct  nda::expr_unary< OP, A >
 Lazy unary expression for nda::Array types. More...
 

Functions

template<Array A, Scalar S>
Array auto nda::operator* (A &&a, S &&s)
 Multiplication operator for an nda::Array and an nda::Scalar.
 
template<Array L, Array R>
auto nda::operator* (L &&l, R &&r)
 Multiplication operator for two nda::Array types.
 
template<Scalar S, Array A>
Array auto nda::operator* (S &&s, A &&a)
 Multiplication operator for an nda::Scalar and an nda::Array.
 
template<Array A, Scalar S>
Array auto nda::operator+ (A &&a, S &&s)
 Addition operator for an nda::Array and an nda::Scalar.
 
template<Array L, Array R>
Array auto nda::operator+ (L &&l, R &&r)
 Addition operator for two nda::Array types.
 
template<Scalar S, Array A>
Array auto nda::operator+ (S &&s, A &&a)
 Addition operator for an nda::Scalar and an nda::Array.
 
template<Array A>
expr_unary<'-', A > nda::operator- (A &&a)
 Unary minus operator for nda::Array types.
 
template<Array A, Scalar S>
Array auto nda::operator- (A &&a, S &&s)
 Subtraction operator for an nda::Array and an nda::Scalar.
 
template<Array L, Array R>
Array auto nda::operator- (L &&l, R &&r)
 Subtraction operator for two nda::Array types.
 
template<Scalar S, Array A>
Array auto nda::operator- (S &&s, A &&a)
 Subtraction operator for an nda::Scalar and an nda::Array.
 
template<Array A, Scalar S>
Array auto nda::operator/ (A &&a, S &&s)
 Division operator for an nda::Array and an nda::Scalar.
 
template<Array L, Array R>
Array auto nda::operator/ (L &&l, R &&r)
 Division operator for two nda::Array types.
 
template<Scalar S, Array A>
Array auto nda::operator/ (S &&s, A &&a)
 Division operator for an nda::Scalar and an nda::Array.
 

Function Documentation

◆ operator*() [1/3]

template<Array A, Scalar S>
Array auto nda::operator* ( A && a,
S && s )

#include <nda/arithmetic.hpp>

Multiplication operator for an nda::Array and an nda::Scalar.

It performs lazy elementwise multiplication.

Template Parameters
Anda::Array type.
Snda::Scalar type.
Parameters
anda::Array left hand side operand.
snda::Scalar right hand side operand.
Returns
Lazy binary expression for the multiplication operation.

Definition at line 453 of file arithmetic.hpp.

◆ operator*() [2/3]

template<Array L, Array R>
auto nda::operator* ( L && l,
R && r )

#include <nda/arithmetic.hpp>

Multiplication operator for two nda::Array types.

The input arrays must have one of the following algebras:

  • 'A' * 'A': Elementwise multiplication of two arrays returns a lazy nda::expr object.
  • 'M' * 'M': Matrix-matrix multiplication calls nda::matmul and returns the result.
  • 'M' * 'V': Matrix-vector multiplication calls nda::matvecmul and returns the result.

Obvious restrictions on the ranks and shapes of the input arrays apply.

Template Parameters
Lnda::Array type of left hand side.
Rnda::Array type of right hand side.
Parameters
lnda::Array left hand side operand.
rnda::Array right hand side operand.
Returns
Either a lazy binary expression for the multiplication operation ('A' * 'A') or the result of the matrix-matrix or matrix-vector multiplication.

Definition at line 413 of file arithmetic.hpp.

◆ operator*() [3/3]

template<Scalar S, Array A>
Array auto nda::operator* ( S && s,
A && a )

#include <nda/arithmetic.hpp>

Multiplication operator for an nda::Scalar and an nda::Array.

It performs elementwise multiplication.

Template Parameters
Snda::Scalar type.
Anda::Array type.
Parameters
snda::Scalar left hand side operand.
anda::Array right hand side operand.
Returns
Lazy binary expression for the multiplication operation.

Definition at line 469 of file arithmetic.hpp.

◆ operator+() [1/3]

template<Array A, Scalar S>
Array auto nda::operator+ ( A && a,
S && s )

#include <nda/arithmetic.hpp>

Addition operator for an nda::Array and an nda::Scalar.

Depending on the algebra of the nda::Array, it performs the following lazy operations:

  • 'A': Elementwise addition.
  • 'M': Addition of the nda::Scalar to the elements on the shorter diagonal of the matrix.
Template Parameters
Anda::Array type.
Snda::Scalar type.
Parameters
anda::Array left hand side operand.
snda::Scalar right hand side operand.
Returns
Lazy binary expression for the addition operation.

Definition at line 320 of file arithmetic.hpp.

◆ operator+() [2/3]

template<Array L, Array R>
Array auto nda::operator+ ( L && l,
R && r )

#include <nda/arithmetic.hpp>

Addition operator for two nda::Array types.

It performs lazy elementwise addition.

Template Parameters
Lnda::Array type of left hand side.
Rnda::Array type of right hand side.
Parameters
lnda::Array left hand side operand.
rnda::Array right hand side operand.
Returns
Lazy binary expression for the addition operation.

Definition at line 301 of file arithmetic.hpp.

◆ operator+() [3/3]

template<Scalar S, Array A>
Array auto nda::operator+ ( S && s,
A && a )

#include <nda/arithmetic.hpp>

Addition operator for an nda::Scalar and an nda::Array.

Depending on the algebra of the nda::Array, it performs the following lazy operations:

  • 'A': Elementwise addition.
  • 'M': Addition of the nda::Scalar to the elements on the shorter diagonal of the matrix.
Template Parameters
Snda::Scalar type.
Anda::Array type.
Parameters
snda::Scalar left hand side operand.
anda::Array right hand side operand.
Returns
Lazy binary expression for the addition operation.

Definition at line 338 of file arithmetic.hpp.

◆ operator-() [1/4]

template<Array A>
expr_unary<'-', A > nda::operator- ( A && a)

#include <nda/arithmetic.hpp>

Unary minus operator for nda::Array types.

It performs lazy elementwise negation.

Template Parameters
Anda::Array type.
Parameters
anda::Array operand.
Returns
Lazy unary expression for the negation operation.

Definition at line 285 of file arithmetic.hpp.

◆ operator-() [2/4]

template<Array A, Scalar S>
Array auto nda::operator- ( A && a,
S && s )

#include <nda/arithmetic.hpp>

Subtraction operator for an nda::Array and an nda::Scalar.

Depending on the algebra of the nda::Array, it performs the following lazy operations:

  • 'A': Elementwise subtraction.
  • 'M': Subtraction of the nda::Scalar from the elements on the shorter diagonal of the matrix.
Template Parameters
Anda::Array type.
Snda::Scalar type.
Parameters
anda::Array left hand side operand.
snda::Scalar right hand side operand.
Returns
Lazy binary expression for the subtraction operation.

Definition at line 373 of file arithmetic.hpp.

◆ operator-() [3/4]

template<Array L, Array R>
Array auto nda::operator- ( L && l,
R && r )

#include <nda/arithmetic.hpp>

Subtraction operator for two nda::Array types.

It performs lazy elementwise subtraction.

Template Parameters
Lnda::Array type of left hand side.
Rnda::Array type of right hand side.
Parameters
lnda::Array left hand side operand.
rnda::Array right hand side operand.
Returns
Lazy binary expression for the subtraction operation.

Definition at line 354 of file arithmetic.hpp.

◆ operator-() [4/4]

template<Scalar S, Array A>
Array auto nda::operator- ( S && s,
A && a )

#include <nda/arithmetic.hpp>

Subtraction operator for an nda::Scalar and an nda::Array.

Depending on the algebra of the nda::Array, it performs the following lazy operations:

  • 'A': Elementwise subtraction.
  • 'M': Subtraction of the elements on the shorter diagonal of the matrix from the nda::Scalar.
Template Parameters
Snda::Scalar type.
Anda::Array type.
Parameters
snda::Scalar left hand side operand.
anda::Array right hand side operand.
Returns
Lazy binary expression for the subtraction operation.

Definition at line 391 of file arithmetic.hpp.

◆ operator/() [1/3]

template<Array A, Scalar S>
Array auto nda::operator/ ( A && a,
S && s )

#include <nda/arithmetic.hpp>

Division operator for an nda::Array and an nda::Scalar.

It performs lazy elementwise division.

Template Parameters
Anda::Array type.
Snda::Scalar type.
Parameters
anda::Array left hand side operand.
snda::Scalar right hand side operand.
Returns
Lazy binary expression for the division operation.

Definition at line 525 of file arithmetic.hpp.

◆ operator/() [2/3]

template<Array L, Array R>
Array auto nda::operator/ ( L && l,
R && r )

#include <nda/arithmetic.hpp>

Division operator for two nda::Array types.

The input arrays must have one of the following algebras:

  • 'A' / 'A': Elementwise division of two arrays returns a lazy nda::expr object.
  • 'M' / 'M': Multiplies the lhs matrix with the inverse of the rhs matrix and returns the result.

Obvious restrictions on the ranks and shapes of the input arrays apply.

Template Parameters
Lnda::Array type of left hand side.
Rnda::Array type of right hand side.
Parameters
lnda::Array left hand side operand.
rnda::Array right hand side operand.
Returns
Either a lazy binary expression for the division operation ('A' * 'A') or the result of the matrix-inverse matrix multiplication.

Definition at line 490 of file arithmetic.hpp.

◆ operator/() [3/3]

template<Scalar S, Array A>
Array auto nda::operator/ ( S && s,
A && a )

#include <nda/arithmetic.hpp>

Division operator for an nda::Scalar and an nda::Array.

Depending on the algebra of the nda::Array, it performs the following lazy operations:

  • 'A': Elementwise division.
  • 'M': Multiplication of the nda::Scalar with the inverse of the matrix.
Template Parameters
Snda::Scalar type.
Anda::Array type.
Parameters
snda::Scalar left hand side operand.
anda::Array right hand side operand.
Returns
Lazy binary expression for the division operation (multiplication in case of a matrix).

Definition at line 543 of file arithmetic.hpp.