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-2023 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Olivier Parcollet, Nils Wentzell
16
22#pragma once
23
24#include "./concepts.hpp"
25#include "./declarations.hpp"
26#include "./linalg/matmul.hpp"
28#include "./macros.hpp"
29#include "./stdutil/complex.hpp"
30#include "./traits.hpp"
31
32#include <functional>
33#include <type_traits>
34#include <utility>
35
36#ifdef NDA_ENFORCE_BOUNDCHECK
37#include "./exceptions.hpp"
38#endif // NDA_ENFORCE_BOUNDCHECK
39
40namespace nda {
41
58 template <char OP, Array A>
59 struct expr_unary {
60 static_assert(OP == '-', "Error in nda::expr_unary: Only negation is supported");
61
63 A a;
64
75 template <typename... Args>
76 auto operator()(Args &&...args) const {
77 return -a(std::forward<Args>(args)...);
78 }
79
84 [[nodiscard]] constexpr auto shape() const { return a.shape(); }
85
90 [[nodiscard]] constexpr long size() const { return a.size(); }
91 };
92
105 template <char OP, ArrayOrScalar L, ArrayOrScalar R>
106 struct expr {
108 L l;
109
111 R r;
112
114 using L_t = std::decay_t<L>;
115
117 using R_t = std::decay_t<R>;
118
119 // FIXME : we should use is_scalar_for_v but the trait needs work to accommodate scalar L or R
121 static constexpr bool l_is_scalar = nda::is_scalar_v<L>;
122
124 static constexpr bool r_is_scalar = nda::is_scalar_v<R>;
125
127 static constexpr char algebra = (l_is_scalar ? get_algebra<R> : get_algebra<L>);
128
134 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
135 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.
136 return get_layout_info<R> & get_layout_info<L>; // default case. Take the logical and of all flags
137 }
138
143 [[nodiscard]] constexpr decltype(auto) shape() const {
144 if constexpr (l_is_scalar) {
145 return r.shape();
146 } else if constexpr (r_is_scalar) {
147 return l.shape();
148 } else {
149 EXPECTS(l.shape() == r.shape());
150 return l.shape();
151 }
152 }
153
158 [[nodiscard]] constexpr long size() const {
159 if constexpr (l_is_scalar) {
160 return r.size();
161 } else if constexpr (r_is_scalar) {
162 return l.size();
163 } else {
164 EXPECTS(l.size() == r.size());
165 return l.size();
166 }
167 }
168
179 template <typename... Args>
180 auto operator()(Args const &...args) const {
181 // addition
182 if constexpr (OP == '+') {
183 if constexpr (l_is_scalar) {
184 // lhs is a scalar
185 if constexpr (algebra == 'M')
186 // rhs is a matrix
187 return (std::equal_to{}(args...) ? l + r(args...) : r(args...));
188 else
189 // rhs is an array
190 return l + r(args...);
191 } else if constexpr (r_is_scalar) {
192 // rhs is a scalar
193 if constexpr (algebra == 'M')
194 // lhs is a matrix
195 return (std::equal_to{}(args...) ? l(args...) + r : l(args...));
196 else
197 // lhs is an array
198 return l(args...) + r;
199 } else
200 // both are arrays or matrices
201 return l(args...) + r(args...);
202 }
203
204 // subtraction
205 if constexpr (OP == '-') {
206 if constexpr (l_is_scalar) {
207 // lhs is a scalar
208 if constexpr (algebra == 'M')
209 // rhs is a matrix
210 return (std::equal_to{}(args...) ? l - r(args...) : -r(args...));
211 else
212 // rhs is an array
213 return l - r(args...);
214 } else if constexpr (r_is_scalar) {
215 // rhs is a scalar
216 if constexpr (algebra == 'M')
217 // lhs is a matrix
218 return (std::equal_to{}(args...) ? l(args...) - r : l(args...));
219 else
220 // lhs is an array
221 return l(args...) - r;
222 } else
223 // both are arrays or matrices
224 return l(args...) - r(args...);
225 }
226
227 // multiplication
228 if constexpr (OP == '*') {
229 if constexpr (l_is_scalar)
230 // lhs is a scalar
231 return l * r(args...);
232 else if constexpr (r_is_scalar)
233 // rhs is a scalar
234 return l(args...) * r;
235 else {
236 // both are arrays (matrix product is not supported here)
237 static_assert(algebra != 'M', "Error in nda::expr: Matrix algebra not supported");
238 return l(args...) * r(args...);
239 }
240 }
241
242 // division
243 if constexpr (OP == '/') {
244 if constexpr (l_is_scalar) {
245 // lhs is a scalar
246 static_assert(algebra != 'M', "Error in nda::expr: Matrix algebra not supported");
247 return l / r(args...);
248 } else if constexpr (r_is_scalar)
249 // rhs is a scalar
250 return l(args...) / r;
251 else {
252 // both are arrays (matrix division is not supported here)
253 static_assert(algebra != 'M', "Error in nda::expr: Matrix algebra not supported");
254 return l(args...) / r(args...);
255 }
256 }
257 }
258
268 template <typename Arg>
269 auto operator[](Arg &&arg) const {
270 static_assert(get_rank<expr> == 1, "Error in nda::expr: Subscript operator only available for expressions of rank 1");
271 return operator()(std::forward<Arg>(arg));
272 }
273 };
274
284 template <Array A>
285 expr_unary<'-', A> operator-(A &&a) {
286 return {std::forward<A>(a)};
287 }
288
300 template <Array L, Array R>
301 Array auto operator+(L &&l, R &&r) {
302 static_assert(get_rank<L> == get_rank<R>, "Error in lazy nda::operator+: Rank mismatch");
303 return expr<'+', L, R>{std::forward<L>(l), std::forward<R>(r)};
304 }
305
319 template <Array A, Scalar S>
320 Array auto operator+(A &&a, S &&s) { // NOLINT (S&& is mandatory for proper concept Array <: typename to work)
321 return expr<'+', A, std::decay_t<S>>{std::forward<A>(a), s};
322 }
323
337 template <Scalar S, Array A>
338 Array auto operator+(S &&s, A &&a) { // NOLINT (S&& is mandatory for proper concept Array <: typename to work)
339 return expr<'+', std::decay_t<S>, A>{s, std::forward<A>(a)};
340 }
341
353 template <Array L, Array R>
354 Array auto operator-(L &&l, R &&r) {
355 static_assert(get_rank<L> == get_rank<R>, "Error in lazy nda::operator-: Rank mismatch");
356 return expr<'-', L, R>{std::forward<L>(l), std::forward<R>(r)};
357 }
358
372 template <Array A, Scalar S>
373 Array auto operator-(A &&a, S &&s) { // NOLINT (S&& is mandatory for proper concept Array <: typename to work)
374 return expr<'-', A, std::decay_t<S>>{std::forward<A>(a), s};
375 }
376
390 template <Scalar S, Array A>
391 Array auto operator-(S &&s, A &&a) { // NOLINT (S&& is mandatory for proper concept Array <: typename to work)
392 return expr<'-', std::decay_t<S>, A>{s, std::forward<A>(a)};
393 }
394
412 template <Array L, Array R>
413 auto operator*(L &&l, R &&r) {
414 // allowed algebras: A * A or M * M or M * V
415 static constexpr char l_algebra = get_algebra<L>;
416 static constexpr char r_algebra = get_algebra<R>;
417 static_assert(l_algebra != 'V', "Error in nda::operator*: Can not multiply vector by an array or a matrix");
418
419 // two arrays: A * A
420 if constexpr (l_algebra == 'A') {
421 static_assert(r_algebra == 'A', "Error in nda::operator*: Both types need to be arrays");
422 static_assert(get_rank<L> == get_rank<R>, "Error in nda::operator*: Rank mismatch");
423#ifdef NDA_ENFORCE_BOUNDCHECK
424 if (l.shape() != r.shape()) NDA_RUNTIME_ERROR << "Error in nda::operator*: Dimension mismatch: " << l.shape() << " != " << r.shape();
425#endif
426 return expr<'*', L, R>{std::forward<L>(l), std::forward<R>(r)};
427 }
428
429 // two matrices: M * M
430 if constexpr (l_algebra == 'M') {
431 static_assert(r_algebra != 'A', "Error in nda::operator*: Can not multiply a matrix by an array");
432 if constexpr (r_algebra == 'M')
433 // matrix * matrix
434 return matmul(std::forward<L>(l), std::forward<R>(r));
435 else
436 // matrix * vector
437 return matvecmul(std::forward<L>(l), std::forward<R>(r));
438 }
439 }
440
452 template <Array A, Scalar S>
453 Array auto operator*(A &&a, S &&s) { // NOLINT (S&& is mandatory for proper concept Array <: typename to work)
454 return expr<'*', A, std::decay_t<S>>{std::forward<A>(a), s};
455 }
456
468 template <Scalar S, Array A>
469 Array auto operator*(S &&s, A &&a) { // NOLINT (S&& is mandatory for proper concept Array <: typename to work)
470 return expr<'*', std::decay_t<S>, A>{s, std::forward<A>(a)};
471 }
472
489 template <Array L, Array R>
490 Array auto operator/(L &&l, R &&r) {
491 // allowed algebras: A / A or M / M
492 static constexpr char l_algebra = get_algebra<L>;
493 static constexpr char r_algebra = get_algebra<R>;
494 static_assert(l_algebra != 'V', "Error in nda::operator/: Can not divide a vector by an array or a matrix");
495
496 // two arrays: A / A
497 if constexpr (l_algebra == 'A') {
498 static_assert(r_algebra == 'A', "Error in nda::operator/: Both types need to be arrays");
499 static_assert(get_rank<L> == get_rank<R>, "Error in nda::operator/: Rank mismatch");
500#ifdef NDA_ENFORCE_BOUNDCHECK
501 if (l.shape() != r.shape()) NDA_RUNTIME_ERROR << "Error in nda::operator/: Dimension mismatch: " << l.shape() << " != " << r.shape();
502#endif
503 return expr<'/', L, R>{std::forward<L>(l), std::forward<R>(r)};
504 }
505
506 // two matrices: M / M
507 if constexpr (l_algebra == 'M') {
508 static_assert(r_algebra == 'M', "Error in nda::operator*: Can not divide a matrix by an array/vector");
509 return std::forward<L>(l) * inverse(matrix<get_value_t<R>>{std::forward<R>(r)});
510 }
511 }
512
524 template <Array A, Scalar S>
525 Array auto operator/(A &&a, S &&s) { // NOLINT (S&& is mandatory for proper concept Array <: typename to work)
526 return expr<'/', A, std::decay_t<S>>{std::forward<A>(a), s};
527 }
528
542 template <Scalar S, Array A>
543 Array auto operator/(S &&s, A &&a) { // NOLINT (S&& is mandatory for proper concept Array <: typename to work)
544 static constexpr char algebra = get_algebra<A>;
545 if constexpr (algebra == 'M')
546 return s * inverse(matrix<get_value_t<A>>{std::forward<A>(a)});
547 else
548 return expr<'/', std::decay_t<S>, A>{s, std::forward<A>(a)};
549 }
550
553} // namespace nda
A generic multi-dimensional array.
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.
constexpr char get_algebra
Constexpr variable that specifies the algebra of a type.
Definition traits.hpp:126
constexpr int get_rank
Constexpr variable that specifies the rank of an nda::Array or of a contiguous 1-dimensional range.
Definition traits.hpp:136
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:192
constexpr layout_info_t get_layout_info
Constexpr variable that specifies the nda::layout_info_t of type A.
Definition traits.hpp:321
auto matvecmul(A &&a, X &&x)
Perform a matrix-vector multiplication.
Definition matmul.hpp:152
auto matmul(A &&a, B &&b)
Perform a matrix-matrix multiplication.
Definition matmul.hpp:87
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:79
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:295
Provides type traits for the nda library.