TRIQS/nda 1.3.0
Multi-dimensional array library for C++
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"
15#include "../exceptions.hpp"
16
17namespace 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
64 template <typename Expr, int N, typename D>
65 decltype(auto) sum(Expr const &ex, clef::pair<N, D> d) {
66 if constexpr (std::is_lvalue_reference_v<D>) {
67 return detail::sum_f_domain_impl(make_function(ex, clef::placeholder<N>()), d.rhs);
68 } else {
69 return detail::sum_f_domain_impl(make_function(ex, clef::placeholder<N>()), std::move(d.rhs));
70 }
71 }
72
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
Provides basic functions to create and manipulate arrays and views.
Includes all relevant headers for the core clef library.
Provides a custom runtime error class and macros to assert conditions and throw exceptions.
decltype(auto) make_regular(A &&a)
Make a given object regular.
__inline__ auto make_function(T &&obj, Phs...)
Factory function for nda::clef::make_fun_impl objects.
Definition function.hpp:89
#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
decltype(auto) sum(Expr const &ex, clef::pair< N, D > d)
Sum an expression over a 1-dimensional domain.
Definition sum.hpp:65
constexpr bool is_clef_expression
Alias template for nda::clef::is_any_lazy.
Definition utils.hpp:149
A pair consisting of a placeholder and its assigned value.
T rhs
Value assigned to the placeholder (can be an lvalue reference).
A placeholder is an empty struct, labelled by an int.