flexiblesusy is hosted by Hepforge, IPPP Durham
FlexibleSUSY
sum.hpp
Go to the documentation of this file.
1// ====================================================================
2// This file is part of FlexibleSUSY.
3//
4// FlexibleSUSY is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published
6// by the Free Software Foundation, either version 3 of the License,
7// or (at your option) any later version.
8//
9// FlexibleSUSY is distributed in the hope that it will be useful, but
10// WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with FlexibleSUSY. If not, see
16// <http://www.gnu.org/licenses/>.
17// ====================================================================
18
19#ifndef SUM_H
20#define SUM_H
21
22#include <type_traits>
23#include <cstddef>
24#include <Eigen/Core>
25
26namespace flexiblesusy {
27
28#define SUM(...) (get_sum(__VA_ARGS__)(__VA_ARGS__))
29
30#define get_sum(...) get_sum_macro(__VA_ARGS__, sum_user_t, sum_ptrdiff_t,)
31
32#define get_sum_macro(_1, _2, _3, _4, _5, name, ...) name
33
34#define sum_ptrdiff_t(idx, ini, fin, expr) \
35 sum<std::ptrdiff_t>((ini), (fin), [&](std::ptrdiff_t (idx)) { return (expr); })
36
37#define sum_user_t(type, idx, ini, fin, expr) \
38 sum<type>((ini), (fin), [&](type (idx)) { return (expr); })
39
40template<typename T>
42{
43 static constexpr auto value =
44 std::is_base_of<Eigen::EigenBase<T>, T>::value;
45};
46
47template<typename Idx, typename Function, bool isEigenType>
49 static auto eval(Idx i, Function f) -> decltype(f(i)) {
50 return f(i);
51 }
52};
53
54template<typename Idx, typename Function>
55struct EvalEigenXprImpl<Idx, Function, true> {
56 static auto eval(Idx i, Function f) ->
57 typename std::remove_reference<decltype(f(i).eval())>::type
58 {
59 return f(i).eval();
60 }
61};
62
63template<typename Idx, typename Function>
64auto EvalEigenXpr(Idx i, Function f) ->
65 decltype(
66 EvalEigenXprImpl<Idx, Function, is_eigen_type<decltype(f(i))>::value>::
67 eval(i, f))
68{
69 return
70 EvalEigenXprImpl<Idx, Function, is_eigen_type<decltype(f(i))>::value>::
71 eval(i, f);
72}
73
74template<typename T, bool isEigenType>
76 static const T zero() {
77 return T();
78 }
79};
80
81template<typename T>
82struct create_zero<T, true> {
83 static const T zero() {
84 T z;
85 z.setZero();
86 return z;
87 }
88};
89
90template<class Idx, class Function>
91auto sum(Idx ini, Idx fin, Function f) -> decltype(EvalEigenXpr<Idx>(ini, f))
92{
93 using Evaled = decltype(EvalEigenXpr<Idx>(ini, f));
94 using Acc = typename std::remove_cv<Evaled>::type;
96 for (Idx i = ini; i <= fin; i++) s += f(i);
97 return s;
98}
99
100} // namespace flexiblesusy
101
102#endif // sum_hpp
auto EvalEigenXpr(Idx i, Function f) -> decltype(EvalEigenXprImpl< Idx, Function, is_eigen_type< decltype(f(i))>::value >::eval(i, f))
Definition: sum.hpp:64
std::complex< double > f(double tau) noexcept
auto sum(Idx ini, Idx fin, Function f) -> decltype(EvalEigenXpr< Idx >(ini, f))
Definition: sum.hpp:91
static auto eval(Idx i, Function f) -> typename std::remove_reference< decltype(f(i).eval())>::type
Definition: sum.hpp:56
static auto eval(Idx i, Function f) -> decltype(f(i))
Definition: sum.hpp:49
static const T zero()
Definition: sum.hpp:76
static constexpr auto value
Definition: sum.hpp:43