RFFGen
 All Classes Namespaces Files Functions Typedefs Enumerations Groups
sum.hh
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the C++-library RFFGen. */
4 /* Copyright 2015 Lars Lubkoll */
5 /* */
6 /* RFFGen is free software: you can redistribute it and/or modify */
7 /* it under the terms of the GNU General Public License as published by */
8 /* the Free Software Foundation, either version 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* RFFGen is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU General Public License */
17 /* along with RFFGen. If not, see <http://www.gnu.org/licenses/>. */
18 /* */
19 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
20 
21 #ifndef RFFGEN_MATHEMATICAL_OPERATIONS_SUM_HH
22 #define RFFGEN_MATHEMATICAL_OPERATIONS_SUM_HH
23 
24 #include <type_traits>
25 #include <utility>
26 
27 #include "../Util/base.hh"
28 #include "../Util/computeSum.hh"
29 #include "../Util/derivativeWrappers.hh"
30 #include "../Util/indexedType.hh"
31 
32 namespace RFFGen
33 {
37  template <class> struct Chainer;
42  namespace MathematicalOperations
43  {
47  template <class> struct FunctionConceptCheck;
57  template <class F, class G,
58  class CheckF = FunctionConceptCheck<F> ,
59  class CheckG = FunctionConceptCheck<G> >
60  struct Sum : Base , Chainer< Sum<F,G,CheckF,CheckG> >
61  {
62  using Chainer< Sum<F,G,CheckF,CheckG> >::operator ();
63  Sum() = default;
64 
70  template <class InitF, class InitG>
71  Sum(const InitF& f_, const InitG& g_)
72  : f(f_), g(g_)
73  {
74  updateResultOfD0();
75  }
76 
78  template <class Arg>
79  void update(Arg const& x)
80  {
81  f.update(x);
82  g.update(x);
83  updateResultOfD0();
84  }
85 
87  template <int index, class Arg>
88  void updateVariable(const Arg& x)
89  {
90  f.template updateVariable<index>(x);
91  g.template updateVariable<index>(x);
92  }
93 
95  const auto& d0() const noexcept
96  {
97  return resultOfD0;
98  }
99 
104  template < int id , class Arg ,
105  class IndexedArg = IndexedType<Arg,id> ,
106  class = std::enable_if_t< ComputeSum< D1<F,IndexedArg>, D1<G,IndexedArg> >::present > >
107  auto d1(const Arg& dx) const
108  {
109  return ComputeSum< D1<F,IndexedArg>, D1<G,IndexedArg> >( f,g,dx )();
110  }
111 
117  template < int idx , int idy , class ArgX , class ArgY ,
118  class IndexedArgX = IndexedType<ArgX,idx> ,
119  class IndexedArgY = IndexedType<ArgY,idy> ,
120  class = std::enable_if_t< ComputeSum< D2<F,IndexedArgX,IndexedArgY>, D2<G,IndexedArgX,IndexedArgY> >::present > >
121  auto d2(const ArgX& dx, const ArgY& dy) const
122  {
123  return ComputeSum< D2<F,IndexedArgX,IndexedArgY>, D2<G,IndexedArgX,IndexedArgY> >( f,g,dx,dy )();
124  }
125 
132  template < int idx , int idy , int idz , class ArgX, class ArgY, class ArgZ ,
133  class IndexedArgX = IndexedType<ArgX,idx> ,
134  class IndexedArgY = IndexedType<ArgY,idy> ,
135  class IndexedArgZ = IndexedType<ArgZ,idz> ,
136  class = std::enable_if_t< ComputeSum< D3<F,IndexedArgX,IndexedArgY,IndexedArgZ>, D3<G,IndexedArgX,IndexedArgY,IndexedArgZ> >::present > >
137  auto d3(const ArgX& dx, const ArgY& dy, const ArgZ& dz) const
138  {
139  return ComputeSum< D3<F,IndexedArgX,IndexedArgY,IndexedArgZ>, D3<G,IndexedArgX,IndexedArgY,IndexedArgZ> >( f,g,dx,dy,dz )();
140  }
141 
142  private:
143  void updateResultOfD0()
144  {
145  resultOfD0 = f.d0() + g.d0();
146  }
147 
148  F f;
149  G g;
150 
151  using type = std::conditional_t<std::is_same<decltype(std::declval<F>().d0()),decltype(std::declval<G>().d0())>::value,
152  decltype(std::declval<F>().d0()),
153  decltype(std::declval<F>().d0() + std::declval<G>().d0())>;
154  std::decay_t<type> resultOfD0;
155  };
156  }
157 }
158 
159 #endif // RFFGEN_MATHEMATICAL_OPERATIONS_SUM_HH
void update(Arg const &x)
Reset point of evaluation.
Definition: sum.hh:79
auto d3(const ArgX &dx, const ArgY &dy, const ArgZ &dz) const
Third directional derivative.
Definition: sum.hh:137
Sum(const InitF &f_, const InitG &g_)
Constructor passing arguments to function constructors.
Definition: sum.hh:71
auto d2(const ArgX &dx, const ArgY &dy) const
Second directional derivative.
Definition: sum.hh:121
auto d1(const Arg &dx) const
First directional derivative.
Definition: sum.hh:107
Base class for functions satisfying FunctionConcept. Required for enabling the operators in generate...
Definition: base.hh:27
Sum of functions of type F and G (F and G must satisfy the requirements of Concepts::FunctionConcept)...
Definition: sum.hh:60
const auto & d0() const noexcept
Function value.
Definition: sum.hh:95
void updateVariable(const Arg &x)
Propagate call to updateVariable() to f and g.
Definition: sum.hh:88