18
19
20
24#include "./concepts.hpp"
25#include "./layout/for_each.hpp"
26#include "./traits.hpp"
38
39
40
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 template <Array A,
typename F,
typename R>
63 auto fold(F f, A
const &a, R r) {
65 decltype(f(r, get_value_t<A>{})) r2 = r;
66 nda::for_each(a.shape(), [&a, &r2, &f](
auto &&...args) { r2 = f(r2, a(args...)); });
71 template <Array A,
typename F>
72 auto fold(F f, A
const &a) {
73 return fold(std::move(f), a, get_value_t<A>{});
77
78
79
80
81
82
83
84
85
86
87
88
89
90
92 bool any(A
const &a) {
93 static_assert(std::is_same_v<get_value_t<A>,
bool>,
"Error in nda::any: Value type of the array must be bool");
94 return fold([](
bool r,
auto const &x) ->
bool {
return r
or bool(x); }, a,
false);
98
99
100
101
102
103
104
105
106
107
108
109
110
111
114 static_assert(std::is_same_v<get_value_t<A>,
bool>,
"Error in nda::all: Value type of the array must be bool");
115 return fold([](
bool r,
auto const &x) ->
bool {
return r
and bool(x); }, a,
true);
119
120
121
122
123
124
125
126
130 [](
auto const &x,
auto const &y) {
134 a, get_first_element(a));
138
139
140
141
142
143
144
145
149 [](
auto const &x,
auto const &y) {
153 a, get_first_element(a));
157
158
159
160
161
162
163
164 template <ArrayOfRank<2> A>
166 return std::sqrt(fold(
167 [](
double r,
auto const &x) ->
double {
168 auto ab = std::abs(x);
175
176
177
178
179
180
183 requires(nda::is_scalar_v<get_value_t<A>>)
185 return fold(std::plus<>{}, a);
189
190
191
192
193
194
197 requires(nda::is_scalar_v<get_value_t<A>>)
199 return fold(std::multiplies<>{}, a, get_value_t<A>{1});
auto max_element(A const &a)
Find the maximum element of an array.
auto fold(F f, A const &a, R r)
Perform a fold operation on the given nda::Array object.
auto product(A const &a)
Multiply all the elements of an nda::Array object.
auto fold(F f, A const &a)
The same as nda::fold, except that the initial value is a default constructed value type of the array...
bool any(A const &a)
Does any of the elements of the array evaluate to true?
auto min_element(A const &a)
Find the minimum element of an array.
auto sum(A const &a)
Sum all the elements of an nda::Array object.
bool all(A const &a)
Do all elements of the array evaluate to true?
double frobenius_norm(A const &a)
Calculate the Frobenius norm of a 2-dimensional array.