TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
arithmetic.hpp
Go to the documentation of this file.
1// Copyright (c) 2019--present, The Simons Foundation
2// This file is part of TRIQS/nda and is licensed under the Apache License, Version 2.0.
3// SPDX-License-Identifier: Apache-2.0
4// See LICENSE in the root of this distribution for details.
5
10
11#pragma once
12
13#include "./concepts.hpp"
14#include "./declarations.hpp"
15#include "./linalg/matmul.hpp"
17#include "./macros.hpp"
18#include "./stdutil/complex.hpp"
19#include "./traits.hpp"
20
21#include <functional>
22#include <type_traits>
23#include <utility>
24
25#ifdef NDA_ENFORCE_BOUNDCHECK
26#include "./exceptions.hpp"
27#endif // NDA_ENFORCE_BOUNDCHECK
28
29namespace nda {
30
35
47 template <char OP, Array A>
48 struct expr_unary {
49 static_assert(OP == '-', "Error in nda::expr_unary: Only negation is supported");
50
52 A a;
53
64 template <typename... Args>
65 auto operator()(Args &&...args) const {
66 return -a(std::forward<Args>(args)...);
67 }
68
73 [[nodiscard]] constexpr auto shape() const { return a.shape(); }
74
79 [[nodiscard]] constexpr long size() const { return a.size(); }
80 };
81
94 template <char OP, ArrayOrScalar L, ArrayOrScalar R>
95 struct expr {
97 L l;
98
100 R r;
101
103 using L_t = std::decay_t<L>;
104
106 using R_t = std::decay_t<R>;
107
108 // FIXME : we should use is_scalar_for_v but the trait needs work to accommodate scalar L or R
110 static constexpr bool l_is_scalar = nda::is_scalar_v<L>;
111
113 static constexpr bool r_is_scalar = nda::is_scalar_v<R>;
114
116 static constexpr char algebra = (l_is_scalar ? get_algebra<R> : get_algebra<L>);
117
123 if (l_is_scalar) return (algebra == 'A' ? get_layout_info<R> : layout_info_t{}); // 1 as an array has all flags, it is just 1
124 if (r_is_scalar) return (algebra == 'A' ? get_layout_info<L> : layout_info_t{}); // 1 as a matrix does not, as it is diagonal only.
125 return get_layout_info<R> & get_layout_info<L>; // default case. Take the logical and of all flags
126 }
127
132 [[nodiscard]] constexpr decltype(auto) shape() const {
133 if constexpr (l_is_scalar) {
134 return r.shape();
135 } else if constexpr (r_is_scalar) {
136 return l.shape();
137 } else {
138 EXPECTS(l.shape() == r.shape());
139 return l.shape();
140 }
141 }
142
147 [[nodiscard]] constexpr long size() const {
148 if constexpr (l_is_scalar) {
149 return r.size();
150 } else if constexpr (r_is_scalar) {
151 return l.size();
152 } else {
153 EXPECTS(l.size() == r.size());
154 return l.size();
155 }
156 }
157
168 template <typename... Args>
169 auto operator()(Args const &...args) const {
170 // addition
171 if constexpr (OP == '+') {
172 if constexpr (l_is_scalar) {
173 // lhs is a scalar
174 if constexpr (algebra == 'M')
175 // rhs is a matrix
176 return (std::equal_to{}(args...) ? l + r(args...) : r(args...));
177 else
178 // rhs is an array
179 return l + r(args...);
180 } else if constexpr (r_is_scalar) {
181 // rhs is a scalar
182 if constexpr (algebra == 'M')
183 // lhs is a matrix
184 return (std::equal_to{}(args...) ? l(args...) + r : l(args...));
185 else
186 // lhs is an array
187 return l(args...) + r;
188 } else
189 // both are arrays or matrices
190 return l(args...) + r(args...);
191 }
192
193 // subtraction
194 if constexpr (OP == '-') {
195 if constexpr (l_is_scalar) {
196 // lhs is a scalar
197 if constexpr (algebra == 'M')
198 // rhs is a matrix
199 return (std::equal_to{}(args...) ? l - r(args...) : -r(args...));
200 else
201 // rhs is an array
202 return l - r(args...);
203 } else if constexpr (r_is_scalar) {
204 // rhs is a scalar
205 if constexpr (algebra == 'M')
206 // lhs is a matrix
207 return (std::equal_to{}(args...) ? l(args...) - r : l(args...));
208 else
209 // lhs is an array
210 return l(args...) - r;
211 } else
212 // both are arrays or matrices
213 return l(args...) - r(args...);
214 }
215
216 // multiplication
217 if constexpr (OP == '*') {
218 if constexpr (l_is_scalar)
219 // lhs is a scalar
220 return l * r(args...);
221 else if constexpr (r_is_scalar)
222 // rhs is a scalar
223 return l(args...) * r;
224 else {
225 // both are arrays (matrix product is not supported here)
226 static_assert(algebra != 'M', "Error in nda::expr: Matrix algebra not supported");
227 return l(args...) * r(args...);
228 }
229 }
230
231 // division
232 if constexpr (OP == '/') {
233 if constexpr (l_is_scalar) {
234 // lhs is a scalar
235 static_assert(algebra != 'M', "Error in nda::expr: Matrix algebra not supported");
236 return l / r(args...);
237 } else if constexpr (r_is_scalar)
238 // rhs is a scalar
239 return l(args...) / r;
240 else {
241 // both are arrays (matrix division is not supported here)
242 static_assert(algebra != 'M', "Error in nda::expr: Matrix algebra not supported");
243 return l(args...) / r(args...);
244 }
245 }
246 }
247
257 template <typename Arg>
258 auto operator[](Arg &&arg) const {
259 static_assert(get_rank<expr> == 1, "Error in nda::expr: Subscript operator only available for expressions of rank 1");
260 return operator()(std::forward<Arg>(arg));
261 }
262 };
263
273 template <Array A>
274 expr_unary<'-', A> operator-(A &&a) {
275 return {std::forward<A>(a)};
276 }
277
289 template <Array L, Array R>
290 Array auto operator+(L &&l, R &&r) {
291 static_assert(get_rank<L> == get_rank<R>, "Error in lazy nda::operator+: Rank mismatch");
292 return expr<'+', L, R>{std::forward<L>(l), std::forward<R>(r)};
293 }
294
308 template <Array A, Scalar S>
309 Array auto operator+(A &&a, S &&s) { // NOLINT (S&& is mandatory for proper concept Array <: typename to work)
310 return expr<'+', A, std::decay_t<S>>{std::forward<A>(a), s};
311 }
312
326 template <Scalar S, Array A>
327 Array auto operator+(S &&s, A &&a) { // NOLINT (S&& is mandatory for proper concept Array <: typename to work)
328 return expr<'+', std::decay_t<S>, A>{s, std::forward<A>(a)};
329 }
330
342 template <Array L, Array R>
343 Array auto operator-(L &&l, R &&r) {
344 static_assert(get_rank<L> == get_rank<R>, "Error in lazy nda::operator-: Rank mismatch");
345 return expr<'-', L, R>{std::forward<L>(l), std::forward<R>(r)};
346 }
347
361 template <Array A, Scalar S>
362 Array auto operator-(A &&a, S &&s) { // NOLINT (S&& is mandatory for proper concept Array <: typename to work)
363 return expr<'-', A, std::decay_t<S>>{std::forward<A>(a), s};
364 }
365
379 template <Scalar S, Array A>
380 Array auto operator-(S &&s, A &&a) { // NOLINT (S&& is mandatory for proper concept Array <: typename to work)
381 return expr<'-', std::decay_t<S>, A>{s, std::forward<A>(a)};
382 }
383
401 template <Array L, Array R>
402 auto operator*(L &&l, R &&r) {
403 // allowed algebras: A * A or M * M or M * V
404 static constexpr char l_algebra = get_algebra<L>;
405 static constexpr char r_algebra = get_algebra<R>;
406 static_assert(l_algebra != 'V', "Error in nda::operator*: Can not multiply vector by an array or a matrix");
407
408 // two arrays: A * A
409 if constexpr (l_algebra == 'A') {
410 static_assert(r_algebra == 'A', "Error in nda::operator*: Both types need to be arrays");
411 static_assert(get_rank<L> == get_rank<R>, "Error in nda::operator*: Rank mismatch");
412#ifdef NDA_ENFORCE_BOUNDCHECK
413 if (l.shape() != r.shape()) NDA_RUNTIME_ERROR << "Error in nda::operator*: Dimension mismatch: " << l.shape() << " != " << r.shape();
414#endif
415 return expr<'*', L, R>{std::forward<L>(l), std::forward<R>(r)};
416 }
417
418 // two matrices: M * M
419 if constexpr (l_algebra == 'M') {
420 static_assert(r_algebra != 'A', "Error in nda::operator*: Can not multiply a matrix by an array");
421 if constexpr (r_algebra == 'M')
422 // matrix * matrix
423 return matmul(std::forward<L>(l), std::forward<R>(r));
424 else
425 // matrix * vector
426 return matvecmul(std::forward<L>(l), std::forward<R>(r));
427 }
428 }
429
441 template <Array A, Scalar S>
442 Array auto operator*(A &&a, S &&s) { // NOLINT (S&& is mandatory for proper concept Array <: typename to work)
443 return expr<'*', A, std::decay_t<S>>{std::forward<A>(a), s};
444 }
445
457 template <Scalar S, Array A>
458 Array auto operator*(S &&s, A &&a) { // NOLINT (S&& is mandatory for proper concept Array <: typename to work)
459 return expr<'*', std::decay_t<S>, A>{s, std::forward<A>(a)};
460 }
461
478 template <Array L, Array R>
479 Array auto operator/(L &&l, R &&r) {
480 // allowed algebras: A / A or M / M
481 static constexpr char l_algebra = get_algebra<L>;
482 static constexpr char r_algebra = get_algebra<R>;
483 static_assert(l_algebra != 'V', "Error in nda::operator/: Can not divide a vector by an array or a matrix");
484
485 // two arrays: A / A
486 if constexpr (l_algebra == 'A') {
487 static_assert(r_algebra == 'A', "Error in nda::operator/: Both types need to be arrays");
488 static_assert(get_rank<L> == get_rank<R>, "Error in nda::operator/: Rank mismatch");
489#ifdef NDA_ENFORCE_BOUNDCHECK
490 if (l.shape() != r.shape()) NDA_RUNTIME_ERROR << "Error in nda::operator/: Dimension mismatch: " << l.shape() << " != " << r.shape();
491#endif
492 return expr<'/', L, R>{std::forward<L>(l), std::forward<R>(r)};
493 }
494
495 // two matrices: M / M
496 if constexpr (l_algebra == 'M') {
497 static_assert(r_algebra == 'M', "Error in nda::operator*: Can not divide a matrix by an array/vector");
498 return std::forward<L>(l) * inverse(matrix<get_value_t<R>>{std::forward<R>(r)});
499 }
500 }
501
513 template <Array A, Scalar S>
514 Array auto operator/(A &&a, S &&s) { // NOLINT (S&& is mandatory for proper concept Array <: typename to work)
515 return expr<'/', A, std::decay_t<S>>{std::forward<A>(a), s};
516 }
517
531 template <Scalar S, Array A>
532 Array auto operator/(S &&s, A &&a) { // NOLINT (S&& is mandatory for proper concept Array <: typename to work)
533 static constexpr char algebra = get_algebra<A>;
534 if constexpr (algebra == 'M')
535 return s * inverse(matrix<get_value_t<A>>{std::forward<A>(a)});
536 else
537 return expr<'/', std::decay_t<S>, A>{s, std::forward<A>(a)};
538 }
539
541
542} // namespace nda
Provides additional operators for std::complex and other arithmetic types.
Check if a given type satisfies the array concept.
Definition concepts.hpp:230
Provides concepts for the nda library.
Provides various convenient aliases and helper functions for nda::basic_array and nda::basic_array_vi...
Provides functions to compute the determinant and inverse of a matrix.
Provides a custom runtime error class and macros to assert conditions and throw exceptions.
Array auto operator+(L &&l, R &&r)
Addition operator for two nda::Array types.
Array auto operator/(L &&l, R &&r)
Division operator for two nda::Array types.
auto operator*(L &&l, R &&r)
Multiplication operator for two nda::Array types.
expr_unary<'-', A > operator-(A &&a)
Unary minus operator for nda::Array types.
basic_array< ValueType, 2, Layout, 'M', ContainerPolicy > matrix
Alias template of an nda::basic_array with rank 2 and an 'M' algebra.
constexpr char get_algebra
Constexpr variable that specifies the algebra of a type.
Definition traits.hpp:115
constexpr int get_rank
Constexpr variable that specifies the rank of an nda::Array or of a contiguous 1-dimensional range.
Definition traits.hpp:125
std::decay_t< decltype(get_first_element(std::declval< A const >()))> get_value_t
Get the value type of an array/view or a scalar type.
Definition traits.hpp:181
constexpr layout_info_t get_layout_info
Constexpr variable that specifies the nda::layout_info_t of type A.
Definition traits.hpp:310
auto matvecmul(A &&a, X &&x)
Perform a matrix-vector multiplication.
Definition matmul.hpp:141
auto matmul(A &&a, B &&b)
Perform a matrix-matrix multiplication.
Definition matmul.hpp:76
auto inverse(M const &m)
Compute the inverse of an n-by-n matrix.
constexpr bool is_scalar_v
Constexpr variable that is true if type S is a scalar type, i.e. arithmetic or complex.
Definition traits.hpp:68
Macros used in the nda library.
Provides matrix-matrix an matrix-vector multiplication.
Lazy unary expression for nda::Array types.
A a
nda::Array object.
auto operator()(Args &&...args) const
Function call operator.
constexpr long size() const
Get the total size of the nda::Array operand.
constexpr auto shape() const
Get the shape of the nda::Array operand.
Lazy binary expression for nda::ArrayOrScalar types.
L l
nda::ArrayOrScalar left hand side operand.
constexpr long size() const
Get the total size of the expression (result of the operation).
static constexpr bool r_is_scalar
Constexpr variable that is true if the right hand side operand is a scalar.
std::decay_t< L > L_t
Decay type of the left hand side operand.
constexpr decltype(auto) shape() const
Get the shape of the expression (result of the operation).
auto operator[](Arg &&arg) const
Subscript operator.
static constexpr layout_info_t compute_layout_info()
Compute the layout information of the expression.
static constexpr char algebra
Constexpr variable specifying the algebra of one of the non-scalar operands.
R r
nda::ArrayOrScalar right hand side operand.
auto operator()(Args const &...args) const
Function call operator.
static constexpr bool l_is_scalar
Constexpr variable that is true if the left hand side operand is a scalar.
std::decay_t< R > R_t
Decay type of the right hand side operand.
Stores information about the memory layout and the stride order of an array/view.
Definition traits.hpp:284
Provides type traits for the nda library.