TRIQS/nda
2.0.0
Multi-dimensional array library for C++
Toggle main menu visibility
Loading...
Searching...
No Matches
sum.hpp
Go to the documentation of this file.
1
// Copyright (c) 2019--present, The Simons Foundation
2
// This file is part of TRIQS/nda and is licensed under the Apache License, Version 2.0.
3
// SPDX-License-Identifier: Apache-2.0
4
// See LICENSE in the root of this distribution for details.
5
10
11
#pragma once
12
13
#include "
./clef.hpp
"
14
#include "
../basic_functions.hpp
"
15
#include "
../exceptions.hpp
"
16
17
namespace
nda::clef {
18
23
24
namespace
detail {
25
26
// Helper function to sum a callable object over a domain using a simple for loop.
27
template
<
typename
F,
typename
D>
28
auto
sum_f_domain_impl(F
const
&f, D
const
&d)
29
requires
(not
is_clef_expression<F, D>
)
30
{
31
auto
it = d.begin();
32
auto
ite = d.end();
33
if
(it == ite) NDA_RUNTIME_ERROR <<
"Error in nda::clef::sum_f_domain_impl: Sum over an empty domain"
;
34
auto
res =
make_regular
(f(*it));
35
++it;
36
for
(; it != ite; ++it) res = res + f(*it);
37
return
res;
38
}
39
40
// Make sum_f_domain_impl lazy.
41
CLEF_MAKE_FNT_LAZY
(sum_f_domain_impl);
42
43
}
// namespace detail
44
65
template
<
typename
Expr,
int
N,
typename
D>
66
decltype
(
auto
)
sum
(Expr
const
&ex,
clef::pair<N, D>
d) {
67
if
constexpr
(std::is_lvalue_reference_v<D>) {
68
return
detail::sum_f_domain_impl(
make_function
(ex,
clef::placeholder<N>
()), d.
rhs
);
69
}
else
{
70
return
detail::sum_f_domain_impl(
make_function
(ex,
clef::placeholder<N>
()), std::move(d.
rhs
));
71
}
72
}
73
97
template
<
typename
Expr,
typename
D0,
typename
D1,
typename
... Ds>
98
auto
sum
(Expr
const
&ex, D0 &&d0, D1 &&d1, Ds &&...ds) {
99
return
sum
(
sum
(ex, std::forward<D0>(d0)), std::forward<D1>(d1), std::forward<Ds>(ds)...);
100
}
101
103
104
}
// namespace nda::clef
basic_functions.hpp
Provides basic functions to create and manipulate arrays and views.
clef.hpp
Includes all relevant headers for the core clef library.
exceptions.hpp
Provides a custom runtime error class and macros to assert conditions and throw exceptions.
nda::make_regular
decltype(auto) make_regular(A &&a)
Make a given object regular.
Definition
basic_functions.hpp:225
nda::clef::make_function
__inline__ auto make_function(T &&obj, Phs...)
Factory function for nda::clef::make_fun_impl objects.
Definition
function.hpp:89
CLEF_MAKE_FNT_LAZY
#define CLEF_MAKE_FNT_LAZY(name)
Macro to make any function lazy, i.e. accept lazy arguments and return a function call expression nod...
Definition
make_lazy.hpp:89
nda::clef::sum
decltype(auto) sum(Expr const &ex, clef::pair< N, D > d)
Sum an expression over a 1-dimensional domain.
Definition
sum.hpp:66
nda::clef::is_clef_expression
constexpr bool is_clef_expression
Alias template for nda::clef::is_any_lazy.
Definition
utils.hpp:148
nda::clef::pair
A pair consisting of a placeholder and its assigned value.
Definition
placeholder.hpp:107
nda::clef::pair::rhs
T rhs
Value assigned to the placeholder (can be an lvalue reference).
Definition
placeholder.hpp:109
nda::clef::placeholder
A placeholder is an empty struct, labelled by an int.
Definition
placeholder.hpp:51
nda
clef
sum.hpp
Generated by
1.17.0