RFFGen
 All Classes Namespaces Files Functions Typedefs Enumerations Groups
log.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_CMATH_LOG_HH
22 #define RFFGEN_CMATH_LOG_HH
23 
24 #include <cmath>
25 #include "../Util/base.hh"
26 #include "../Util/chainer.hh"
27 #include "../Util/exceptions.hh"
28 
29 namespace RFFGen
30 {
31  namespace CMath
32  {
41  struct LN : Base , Chainer<LN>
42  {
43  using Chainer<LN>::operator ();
44 
49  explicit LN(double x=1.) { update(x); }
50 
52  void update(double x)
53  {
54 #ifdef RFFGEN_ENABLE_EXCEPTIONS
55  if( x <= 0 ) throw OutOfDomainException("LN","]0,inf[",x,__FILE__,__LINE__);
56 #endif
57  x_inv = 1./x;
58  value = ::log(x);
59  }
60 
62  double d0() const noexcept
63  {
64  return value;
65  }
66 
68  template < int = -1 >
69  double d1(double dx = 1.) const
70  {
71  return x_inv * dx;
72  }
73 
75  template < int = -1 , int = -1 >
76  double d2(double dx = 1., double dy = 1.) const
77  {
78  return - x_inv * x_inv * dx * dy;
79  }
80 
82  template < int = -1 , int = -1 , int = -1 >
83  double d3(double dx = 1., double dy = 1., double dz = 1.) const
84  {
85  return 2 * x_inv * x_inv * x_inv * dx * dy * dz;
86  }
87 
88  private:
89  double value = 0., x_inv = 1.;
90  };
91 
100  struct Log10 : Base , Chainer<Log10>
101  {
102  using Chainer<Log10>::operator ();
103 
108  explicit Log10(double x=1.) { update(x); }
109 
111  void update(double x)
112  {
113 #ifdef RFFGEN_ENABLE_EXCEPTIONS
114  if( x <= 0 ) throw OutOfDomainException("Log10","]0,inf[",x,__FILE__,__LINE__);
115 #endif
116  x_inv = 1./x;
117  value = ::log10(x);
118  }
119 
121  double d0() const noexcept
122  {
123  return value;
124  }
125 
127  template < int = -1 >
128  double d1(double dx = 1.) const
129  {
130  return ln10inv * x_inv * dx;
131  }
132 
134  template < int = -1 , int = -1 >
135  double d2(double dx = 1., double dy = 1.) const
136  {
137  return - ln10inv * x_inv * x_inv * dx * dy;
138  }
139 
141  template < int = -1 , int = -1 , int = -1 >
142  double d3(double dx = 1., double dy = 1., double dz = 1.) const
143  {
144  return 2 * ln10inv * x_inv * x_inv * x_inv * dx * dy * dz;
145  }
146 
147  private:
148  double value = 0., x_inv = 1., ln10inv = 1/log(10.);
149  };
150 
159  struct Log2 : Base , Chainer<Log2>
160  {
161  using Chainer<Log2>::operator ();
162 
167  explicit Log2(double x=1.) { update(x); }
168 
170  void update(double x)
171  {
172 #ifdef RFFGEN_ENABLE_EXCEPTIONS
173  if( x <= 0 ) throw OutOfDomainException("Log2","]0,inf[",x,__FILE__,__LINE__);
174 #endif
175  x_inv = 1./x;
176  value = ::log2(x);
177  }
178 
180  double d0() const noexcept
181  {
182  return value;
183  }
184 
186  template < int = -1 >
187  double d1(double dx = 1.) const
188  {
189  return ln2inv * x_inv * dx;
190  }
191 
193  template < int = -1 , int = -1 >
194  double d2(double dx = 1., double dy = 1.) const
195  {
196  return - ln2inv * x_inv * x_inv * dx * dy;
197  }
198 
200  template < int = -1 , int = -1 , int = -1 >
201  double d3(double dx = 1., double dy = 1., double dz = 1.) const
202  {
203  return 2 * ln2inv * x_inv * x_inv * x_inv * dx * dy * dz;
204  }
205 
206  private:
207  double value = 0., x_inv = 1., ln2inv = 1/log(2.);
208  };
209 
214  template <class Function, class = std::enable_if_t<std::is_base_of<Base,Function>::value> >
215  auto ln(const Function& f)
216  {
217  return LN()(f);
218  }
219 
224  template <class Function, class = std::enable_if_t<std::is_base_of<Base,Function>::value> >
225  auto log10(const Function& f)
226  {
227  return Log10()(f);
228  }
229 
234  template <class Function, class = std::enable_if_t<std::is_base_of<Base,Function>::value> >
235  auto log2(const Function& f)
236  {
237  return Log2()(f);
238  }
239  }
240 }
241 
242 #endif // RFFGEN_CMATH_LOG_HH
Log2(double x=1.)
Constructor.
Definition: log.hh:167
double d3(double dx=1., double dy=1., double dz=1.) const
Third (directional) derivative.
Definition: log.hh:201
double d1(double dx=1.) const
First (directional) derivative.
Definition: log.hh:187
double d3(double dx=1., double dy=1., double dz=1.) const
Third (directional) derivative.
Definition: log.hh:83
double d0() const noexcept
Function value.
Definition: log.hh:62
void update(double x)
Reset point of evaluation.
Definition: log.hh:170
Base class for functions satisfying FunctionConcept. Required for enabling the operators in generate...
Definition: base.hh:27
double d3(double dx=1., double dy=1., double dz=1.) const
Third (directional) derivative.
Definition: log.hh:142
void update(double x)
Reset point of evaluation.
Definition: log.hh:111
double d0() const noexcept
Function value.
Definition: log.hh:121
Natural logarithm including first three derivatives.
Definition: log.hh:41
double d2(double dx=1., double dy=1.) const
Second (directional) derivative.
Definition: log.hh:135
double d2(double dx=1., double dy=1.) const
Second (directional) derivative.
Definition: log.hh:194
Exception for scalar function arguments that are outside the domain of the function.
Definition: exceptions.hh:40
LN(double x=1.)
Constructor.
Definition: log.hh:49
void update(double x)
Reset point of evaluation.
Definition: log.hh:52
Common (base 10) logarithm including first three derivatives.
Definition: log.hh:100
double d1(double dx=1.) const
First (directional) derivative.
Definition: log.hh:69
Base 2 logarithm including first three derivatives.
Definition: log.hh:159
double d1(double dx=1.) const
First (directional) derivative.
Definition: log.hh:128
Log10(double x=1.)
Constructor.
Definition: log.hh:108
double d0() const noexcept
Function value.
Definition: log.hh:180
double d2(double dx=1., double dy=1.) const
Second (directional) derivative.
Definition: log.hh:76