58 template <Array A,
typename F,
typename R>
59 auto fold(F f, A
const &a, R r) {
63 nda::for_each(a.shape(), [&a, &res, &f](
auto &&...args) { res = f(res, a(args...)); });
68 template <Array A,
typename F>
69 auto fold(F f, A
const &a) {
89 bool any(A
const &a) {
90 static_assert(std::is_same_v<get_value_t<A>,
bool>,
"Error in nda::any: Value type of the array must be bool");
91 return fold([](
bool r,
auto const &x) ->
bool {
return r or bool(x); }, a,
false);
111 static_assert(std::is_same_v<get_value_t<A>,
bool>,
"Error in nda::all: Value type of the array must be bool");
112 return fold([](
bool r,
auto const &x) ->
bool {
return r and bool(x); }, a,
true);
127 [](
auto const &x,
auto const &y) {
146 [](
auto const &x,
auto const &y) {
161 template <ArrayOfRank<2> A>
163 return std::sqrt(
fold(
164 [](
double r,
auto const &x) ->
double {
165 auto ab = std::abs(x);
178 template <Array A,
typename Value = get_value_t<A>>
183 return fold(std::plus<>{}, a);
208 template <Array A, std::
integral I,
size_t N>
210 auto sum(A
const &a, std::array<I, N> axes) {
212 if constexpr (N == 0) {
216 std::ranges::sort(axes);
217 EXPECTS(std::ranges::adjacent_find(axes) == axes.end());
218 EXPECTS(axes.front() >= 0 and axes.back() <
get_rank<A>);
220 constexpr int res_rank =
get_rank<A> -
static_cast<int>(N);
222 if constexpr (res_rank == 0) {
226 std::array<long, res_rank> keep_axes, res_shape;
227 for (
int i = 0;
auto ax : nda::range(
get_rank<A>)) {
228 if (!std::ranges::binary_search(axes, ax)) {
230 res_shape[i++] = a.shape()[ax];
239 auto idx_arr = std::array{idxs...};
240 std::apply([&](
auto... keep) { res(idx_arr[keep]...) += a(idxs...); }, keep_axes);
260 auto sum(A
const &a,
int axis) {
261 return sum(a, std::array{axis});
271 template <Array A,
typename Value = get_value_t<A>>
291 template <Array A, Array B>
293 [[nodiscard]]
constexpr auto hadamard(A &&a, B &&b) {
294 return nda::map([](
auto const &x,
auto const &y) {
return x * y; })(std::forward<A>(a), std::forward<B>(b));
307 template <
typename T,
typename U,
size_t R>
308 [[nodiscard]]
constexpr auto hadamard(std::array<T, R>
const &a, std::array<U, R>
const &b) {
321 template <
typename T,
typename U>
322 [[nodiscard]]
constexpr auto hadamard(std::vector<T>
const &a, std::vector<U>
const &b) {
323 using TU =
decltype(std::declval<T>() * std::declval<U>());
324 EXPECTS(a.size() == b.size());
326 std::vector<TU> c(a.size());
327 for (
auto i : range(c.size())) c[i] = a[i] * b[i];
Provides basic functions to create and manipulate arrays and views.
Check if a given type satisfies the array concept.
Check if a given type is either an arithmetic or complex type.
Provides concepts for the nda library.
Provides for_each functions for multi-dimensional arrays/views.
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 sum(A const &a)
Sum all the elements of an nda::Array object.
bool any(A const &a)
Does any of the elements of the array evaluate to true?
auto product(A const &a)
Multiply all the elements of an nda::Array object.
auto min_element(A const &a)
Find the minimum element of an array.
constexpr auto hadamard(A &&a, B &&b)
Hadamard product of two nda::Array objects.
bool all(A const &a)
Do all elements of the array evaluate to true?
decltype(auto) make_regular(A &&a)
Make a given object regular.
auto zeros(std::array< Int, Rank > const &shape)
Make an array of the given shape on the given address space and zero-initialize it.
double frobenius_norm(A const &a)
Calculate the Frobenius norm of a 2-dimensional array.
mapped< F > map(F f)
Create a lazy function call expression on arrays/views.
basic_array< ValueType, Rank, Layout, 'A', ContainerPolicy > array
Alias template of an nda::basic_array with an 'A' algebra.
decltype(auto) get_first_element(A &&a)
Get the first element of an array/view or simply return the scalar if a scalar is given.
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.
__inline__ void for_each(std::array< Int, R > const &shape, F &&f)
Loop over all possible index values of a given shape and apply a function to them.
Macros used in the nda library.
Provides lazy function calls on arrays/views.
Includes the itertools header and provides some additional utilities.
Provides type traits for the nda library.