36#ifdef NDA_ENFORCE_BOUNDCHECK
58 template <
char OP, Array A>
60 static_assert(OP ==
'-',
"Error in nda::expr_unary: Only negation is supported");
75 template <
typename... Args>
77 return -
a(std::forward<Args>(args)...);
84 [[nodiscard]]
constexpr auto shape()
const {
return a.shape(); }
90 [[nodiscard]]
constexpr long size()
const {
return a.size(); }
105 template <
char OP, ArrayOrScalar L, ArrayOrScalar R>
114 using L_t = std::decay_t<L>;
117 using R_t = std::decay_t<R>;
143 [[nodiscard]]
constexpr decltype(
auto)
shape()
const {
149 EXPECTS(
l.shape() ==
r.shape());
158 [[nodiscard]]
constexpr long size()
const {
164 EXPECTS(
l.size() ==
r.size());
179 template <
typename... Args>
182 if constexpr (OP ==
'+') {
187 return (std::equal_to{}(args...) ?
l +
r(args...) :
r(args...));
190 return l +
r(args...);
195 return (std::equal_to{}(args...) ?
l(args...) +
r :
l(args...));
198 return l(args...) +
r;
201 return l(args...) +
r(args...);
205 if constexpr (OP ==
'-') {
210 return (std::equal_to{}(args...) ?
l -
r(args...) : -
r(args...));
213 return l -
r(args...);
218 return (std::equal_to{}(args...) ?
l(args...) -
r :
l(args...));
221 return l(args...) -
r;
224 return l(args...) -
r(args...);
228 if constexpr (OP ==
'*') {
231 return l *
r(args...);
234 return l(args...) *
r;
237 static_assert(
algebra !=
'M',
"Error in nda::expr: Matrix algebra not supported");
238 return l(args...) *
r(args...);
243 if constexpr (OP ==
'/') {
246 static_assert(
algebra !=
'M',
"Error in nda::expr: Matrix algebra not supported");
247 return l /
r(args...);
250 return l(args...) /
r;
253 static_assert(
algebra !=
'M',
"Error in nda::expr: Matrix algebra not supported");
254 return l(args...) /
r(args...);
268 template <
typename Arg>
270 static_assert(
get_rank<expr> == 1,
"Error in nda::expr: Subscript operator only available for expressions of rank 1");
286 return {std::forward<A>(a)};
300 template <Array L, Array R>
303 return expr<
'+', L, R>{std::forward<L>(l), std::forward<R>(r)};
319 template <Array A, Scalar S>
321 return expr<
'+', A, std::decay_t<S>>{std::forward<A>(a), s};
337 template <Scalar S, Array A>
339 return expr<
'+', std::decay_t<S>, A>{s, std::forward<A>(a)};
353 template <Array L, Array R>
356 return expr<
'-', L, R>{std::forward<L>(l), std::forward<R>(r)};
372 template <Array A, Scalar S>
374 return expr<
'-', A, std::decay_t<S>>{std::forward<A>(a), s};
390 template <Scalar S, Array A>
392 return expr<
'-', std::decay_t<S>, A>{s, std::forward<A>(a)};
412 template <Array L, Array R>
417 static_assert(l_algebra !=
'V',
"Error in nda::operator*: Can not multiply vector by an array or a matrix");
420 if constexpr (l_algebra ==
'A') {
421 static_assert(r_algebra ==
'A',
"Error in nda::operator*: Both types need to be arrays");
423#ifdef NDA_ENFORCE_BOUNDCHECK
424 if (l.shape() != r.shape()) NDA_RUNTIME_ERROR <<
"Error in nda::operator*: Dimension mismatch: " << l.shape() <<
" != " << r.shape();
426 return expr<
'*', L, R>{std::forward<L>(l), std::forward<R>(r)};
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')
434 return matmul(std::forward<L>(l), std::forward<R>(r));
437 return matvecmul(std::forward<L>(l), std::forward<R>(r));
452 template <Array A, Scalar S>
454 return expr<
'*', A, std::decay_t<S>>{std::forward<A>(a), s};
468 template <Scalar S, Array A>
470 return expr<
'*', std::decay_t<S>, A>{s, std::forward<A>(a)};
489 template <Array L, Array R>
494 static_assert(l_algebra !=
'V',
"Error in nda::operator/: Can not divide a vector by an array or a matrix");
497 if constexpr (l_algebra ==
'A') {
498 static_assert(r_algebra ==
'A',
"Error in nda::operator/: Both types need to be arrays");
500#ifdef NDA_ENFORCE_BOUNDCHECK
501 if (l.shape() != r.shape()) NDA_RUNTIME_ERROR <<
"Error in nda::operator/: Dimension mismatch: " << l.shape() <<
" != " << r.shape();
503 return expr<
'/', L, R>{std::forward<L>(l), std::forward<R>(r)};
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");
524 template <Array A, Scalar S>
526 return expr<
'/', A, std::decay_t<S>>{std::forward<A>(a), s};
542 template <Scalar S, Array A>
544 static constexpr char algebra = get_algebra<A>;
545 if constexpr (algebra ==
'M')
548 return expr<
'/', std::decay_t<S>, A>{s, std::forward<A>(a)};
A generic multi-dimensional array.
Provides additional operators for std::complex and other arithmetic types.
Check if a given type satisfies the array concept.
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.
constexpr int get_rank
Constexpr variable that specifies the rank of an nda::Array or of a contiguous 1-dimensional range.
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.
constexpr layout_info_t get_layout_info
Constexpr variable that specifies the nda::layout_info_t of type A.
constexpr bool is_scalar_v
Constexpr variable that is true if type S is a scalar type, i.e. arithmetic or complex.
Macros used in the nda library.
Provides matrix-matrix an matrix-vector multiplication.
Lazy unary expression for nda::Array types.
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.
Provides type traits for the nda library.