26#ifdef NDA_ENFORCE_BOUNDCHECK
48 template <
char OP, Array A>
50 static_assert(OP ==
'-',
"Error in nda::expr_unary: Only negation is supported");
65 template <
typename... Args>
67 return -
a(std::forward<Args>(args)...);
74 [[nodiscard]]
constexpr auto shape()
const {
return a.shape(); }
80 [[nodiscard]]
constexpr long size()
const {
return a.size(); }
95 template <
char OP, ArrayOrScalar L, ArrayOrScalar R>
104 using L_t = std::decay_t<L>;
107 using R_t = std::decay_t<R>;
133 [[nodiscard]]
constexpr decltype(
auto)
shape()
const {
139 EXPECTS(
l.shape() ==
r.shape());
148 [[nodiscard]]
constexpr long size()
const {
154 EXPECTS(
l.size() ==
r.size());
169 template <
typename... Args>
172 if constexpr (OP ==
'+') {
177 return (std::equal_to{}(args...) ?
l +
r(args...) :
r(args...));
180 return l +
r(args...);
185 return (std::equal_to{}(args...) ?
l(args...) +
r :
l(args...));
188 return l(args...) +
r;
191 return l(args...) +
r(args...);
195 if constexpr (OP ==
'-') {
200 return (std::equal_to{}(args...) ?
l -
r(args...) : -
r(args...));
203 return l -
r(args...);
208 return (std::equal_to{}(args...) ?
l(args...) -
r :
l(args...));
211 return l(args...) -
r;
214 return l(args...) -
r(args...);
218 if constexpr (OP ==
'*') {
221 return l *
r(args...);
224 return l(args...) *
r;
227 static_assert(
algebra !=
'M',
"Error in nda::expr: Matrix algebra not supported");
228 return l(args...) *
r(args...);
233 if constexpr (OP ==
'/') {
236 static_assert(
algebra !=
'M',
"Error in nda::expr: Matrix algebra not supported");
237 return l /
r(args...);
240 return l(args...) /
r;
243 static_assert(
algebra !=
'M',
"Error in nda::expr: Matrix algebra not supported");
244 return l(args...) /
r(args...);
258 template <
typename Arg>
260 static_assert(
get_rank<expr> == 1,
"Error in nda::expr: Subscript operator only available for expressions of rank 1");
276 return {std::forward<A>(a)};
290 template <Array L, Array R>
293 return expr<
'+', L, R>{std::forward<L>(l), std::forward<R>(r)};
309 template <Array A, Scalar S>
311 return expr<
'+', A, std::decay_t<S>>{std::forward<A>(a), s};
327 template <Scalar S, Array A>
329 return expr<
'+', std::decay_t<S>, A>{s, std::forward<A>(a)};
343 template <Array L, Array R>
346 return expr<
'-', L, R>{std::forward<L>(l), std::forward<R>(r)};
362 template <Array A, Scalar S>
364 return expr<
'-', A, std::decay_t<S>>{std::forward<A>(a), s};
380 template <Scalar S, Array A>
382 return expr<
'-', std::decay_t<S>, A>{s, std::forward<A>(a)};
402 template <Array L, Array R>
407 static_assert(l_algebra !=
'V',
"Error in nda::operator*: Can not multiply vector by an array or a matrix");
410 if constexpr (l_algebra ==
'A') {
411 static_assert(r_algebra ==
'A',
"Error in nda::operator*: Both types need to be arrays");
413#ifdef NDA_ENFORCE_BOUNDCHECK
414 if (l.shape() != r.shape()) NDA_RUNTIME_ERROR <<
"Error in nda::operator*: Dimension mismatch: " << l.shape() <<
" != " << r.shape();
416 return expr<
'*', L, R>{std::forward<L>(l), std::forward<R>(r)};
420 if constexpr (l_algebra ==
'M') {
421 static_assert(r_algebra !=
'A',
"Error in nda::operator*: Can not multiply a matrix by an array");
422 if constexpr (r_algebra ==
'M')
442 template <Array A, Scalar S>
444 return expr<
'*', A, std::decay_t<S>>{std::forward<A>(a), s};
458 template <Scalar S, Array A>
460 return expr<
'*', std::decay_t<S>, A>{s, std::forward<A>(a)};
479 template <Array L, Array R>
484 static_assert(l_algebra !=
'V',
"Error in nda::operator/: Can not divide a vector by an array or a matrix");
487 if constexpr (l_algebra ==
'A') {
488 static_assert(r_algebra ==
'A',
"Error in nda::operator/: Both types need to be arrays");
490#ifdef NDA_ENFORCE_BOUNDCHECK
491 if (l.shape() != r.shape()) NDA_RUNTIME_ERROR <<
"Error in nda::operator/: Dimension mismatch: " << l.shape() <<
" != " << r.shape();
493 return expr<
'/', L, R>{std::forward<L>(l), std::forward<R>(r)};
497 if constexpr (l_algebra ==
'M') {
498 static_assert(r_algebra ==
'M',
"Error in nda::operator*: Can not divide a matrix by an array/vector");
514 template <Array A, Scalar S>
516 return expr<
'/', A, std::decay_t<S>>{std::forward<A>(a), s};
532 template <Scalar S, Array A>
535 if constexpr (algebra ==
'M')
538 return expr<
'/', std::decay_t<S>, A>{s, std::forward<A>(a)};
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 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.
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.
Provides functions to compute the inverse of a matrix.
Macros used in the nda library.
Provides a generic matrix-matrix multiplication.
Provides a generic 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.