bpp-core3  3.0.0
MathOperator.h
Go to the documentation of this file.
1 //
2 // File: MathOperator.h
3 // Authors:
4 // Laurent Guéguen
5 // Created: lundi 5 décembre 2016, à 23h 05
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for numerical calculus.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 #ifndef BPP_NUMERIC_FUNCTION_OPERATORS_MATHOPERATOR_H
42 #define BPP_NUMERIC_FUNCTION_OPERATORS_MATHOPERATOR_H
43 
44 #include <cmath>
45 #include <memory>
46 
47 
48 namespace bpp
49 {
56 class MathOperator :
57  public Operator
58 {
59 private:
60  /*
61  * @brief pointer to function.
62  * If null, identity function is used.
63  */
64 
65  double (* func_)(double);
66 
67  std::string name_;
68 
69  std::shared_ptr<Operator> son_;
70 
71 public:
72  MathOperator(double (*func)(double), std::string name, std::shared_ptr<Operator> son) :
73  func_(func), name_(name), son_(son)
74  {}
75 
77  {
78  return new MathOperator(*this);
79  }
80 
81  std::shared_ptr<Operator> getSon()
82  {
83  return son_;
84  }
85 
86  double getValue() const
87  {
88  if (func_)
89  return (*func_)(son_->getValue());
90  else
91  return son_->getValue();
92  }
93 
94  std::string getName() const
95  {
96  return name_;
97  }
98 
103  double getFirstOrderDerivative(const std::string& variable) const
104  {
105  double v = son_->getValue();
106  double d = son_->getFirstOrderDerivative(variable);
107 
108  if (name_ == "exp")
109  return d * exp(v);
110  else if (name_ == "log")
111  return d / v;
112  else
113  throw Exception("MathOperator::getFirstOrderDerivative : unknown function " + name_);
114  }
115 
116  double getSecondOrderDerivative(const std::string& variable) const
117  {
118  double v = son_->getValue();
119  double d = son_->getFirstOrderDerivative(variable);
120  double d2 = son_->getSecondOrderDerivative(variable);
121 
122  if (name_ == "exp")
123  return (d2 + d * d) * exp(v);
124  else if (name_ == "log")
125  return (d2 * v - d * d) / (v * v);
126  else
127  throw Exception("MathOperator::getFirstOrderDerivative : unknown function " + name_);
128  return 0;
129  }
130 
131 
132  std::string output() const
133  {
134  return name_ + "(" + son_->output() + ")";
135  }
136 };
137 } // end of namespace bpp.
138 #endif // BPP_NUMERIC_FUNCTION_OPERATORS_MATHOPERATOR_H
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Definition: Exceptions.h:59
Implements a unary operator that applies a math (described in cmath) operator.
Definition: MathOperator.h:58
double getValue() const
Definition: MathOperator.h:86
std::string output() const
Definition: MathOperator.h:132
double(* func_)(double)
Definition: MathOperator.h:65
double getFirstOrderDerivative(const std::string &variable) const
1st order derivative
Definition: MathOperator.h:103
std::string getName() const
Definition: MathOperator.h:94
MathOperator(double(*func)(double), std::string name, std::shared_ptr< Operator > son)
Definition: MathOperator.h:72
std::shared_ptr< Operator > getSon()
Definition: MathOperator.h:81
double getSecondOrderDerivative(const std::string &variable) const
Definition: MathOperator.h:116
MathOperator * clone() const
Create a copy of this object and send a pointer to it.
Definition: MathOperator.h:76
std::shared_ptr< Operator > son_
Definition: MathOperator.h:69
std::string name_
Definition: MathOperator.h:67
Interface of operator for numerical computation.
Definition: Operator.h:57