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-2024 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: Thomas Hahn, Olivier Parcollet, Nils Wentzell
16
22#pragma once
23
24#include "./clef.hpp"
26#include "../exceptions.hpp"
27
28namespace nda::clef {
29
35 namespace detail {
36
37 // Helper function to sum a callable object over a domain using a simple for loop.
38 template <typename F, typename D>
39 auto sum_f_domain_impl(F const &f, D const &d)
40 requires(not is_clef_expression<F, D>)
41 {
42 auto it = d.begin();
43 auto ite = d.end();
44 if (it == ite) NDA_RUNTIME_ERROR << "Error in nda::clef::sum_f_domain_impl: Sum over an empty domain";
45 auto res = make_regular(f(*it));
46 ++it;
47 for (; it != ite; ++it) res = res + f(*it);
48 return res;
49 }
50
51 // Make sum_f_domain_impl lazy.
52 CLEF_MAKE_FNT_LAZY(sum_f_domain_impl);
53
54 } // namespace detail
55
75 template <typename Expr, int N, typename D>
76 decltype(auto) sum(Expr const &ex, clef::pair<N, D> d) {
77 if constexpr (std::is_lvalue_reference_v<D>) {
78 return detail::sum_f_domain_impl(make_function(ex, clef::placeholder<N>()), d.rhs);
79 } else {
80 return detail::sum_f_domain_impl(make_function(ex, clef::placeholder<N>()), std::move(d.rhs));
81 }
82 }
83
108 template <typename Expr, typename D0, typename D1, typename... Ds>
109 auto sum(Expr const &ex, D0 &&d0, D1 &&d1, Ds &&...ds) {
110 return sum(sum(ex, std::forward<D0>(d0)), std::forward<D1>(d1), std::forward<Ds>(ds)...);
111 }
112
115} // 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:100
#define CLEF_MAKE_FNT_LAZY(name)
Macro to make any function lazy, i.e. accept lazy arguments and return a function call expression nod...
decltype(auto) sum(Expr const &ex, clef::pair< N, D > d)
Sum an expression over a 1-dimensional domain.
Definition sum.hpp:76
constexpr bool is_clef_expression
Alias template for nda::clef::is_any_lazy.
Definition utils.hpp:160
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.