TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
algorithms.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 "./layout/for_each.hpp"
26#include "./traits.hpp"
27
28#include <algorithm>
29#include <cmath>
30#include <cstdlib>
31#include <functional>
32#include <type_traits>
33#include <utility>
34
35namespace nda {
36
42 // FIXME : CHECK ORDER of the LOOP !
62 template <Array A, typename F, typename R>
63 auto fold(F f, A const &a, R r) {
64 // cast the initial value to the return type of f to avoid narrowing
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...)); });
67 return r2;
68 }
69
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>{});
74 }
75
91 template <Array A>
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);
95 }
96
112 template <Array A>
113 bool all(A const &a) {
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);
116 }
117
127 template <Array A>
128 auto max_element(A const &a) {
129 return fold(
130 [](auto const &x, auto const &y) {
131 using std::max;
132 return max(x, y);
133 },
134 a, get_first_element(a));
135 }
136
146 template <Array A>
147 auto min_element(A const &a) {
148 return fold(
149 [](auto const &x, auto const &y) {
150 using std::min;
151 return min(x, y);
152 },
153 a, get_first_element(a));
154 }
155
164 template <ArrayOfRank<2> A>
165 double frobenius_norm(A const &a) {
166 return std::sqrt(fold(
167 [](double r, auto const &x) -> double {
168 auto ab = std::abs(x);
169 return r + ab * ab;
170 },
171 a, double(0)));
172 }
173
181 template <Array A>
182 auto sum(A const &a)
184 {
185 return fold(std::plus<>{}, a);
186 }
187
195 template <Array A>
196 auto product(A const &a)
198 {
199 return fold(std::multiplies<>{}, a, get_value_t<A>{1});
200 }
201
204} // namespace nda
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 product(A const &a)
Multiply 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 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.
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
decltype(auto) get_first_element(A const &a)
Get the first element of an array/view or simply return the scalar if a scalar is given.
Definition traits.hpp:177
__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.
Definition for_each.hpp:129
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
Provides type traits for the nda library.