bpp-core3  3.0.0
MathOperator.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_NUMERIC_FUNCTION_OPERATORS_MATHOPERATOR_H
6 #define BPP_NUMERIC_FUNCTION_OPERATORS_MATHOPERATOR_H
7 
8 #include <cmath>
9 #include <memory>
10 
11 
12 namespace bpp
13 {
20 class MathOperator :
21  public Operator
22 {
23 private:
24  /*
25  * @brief pointer to function.
26  * If null, identity function is used.
27  */
28 
29  double (* func_)(double);
30 
31  std::string name_;
32 
33  std::shared_ptr<Operator> son_;
34 
35 public:
36  MathOperator(double (*func)(double), std::string name, std::shared_ptr<Operator> son) :
37  func_(func), name_(name), son_(son)
38  {}
39 
41  {
42  return new MathOperator(*this);
43  }
44 
45  std::shared_ptr<Operator> getSon()
46  {
47  return son_;
48  }
49 
50  double getValue() const
51  {
52  if (func_)
53  return (*func_)(son_->getValue());
54  else
55  return son_->getValue();
56  }
57 
58  std::string getName() const
59  {
60  return name_;
61  }
62 
67  double getFirstOrderDerivative(const std::string& variable) const
68  {
69  double v = son_->getValue();
70  double d = son_->getFirstOrderDerivative(variable);
71 
72  if (name_ == "exp")
73  return d * exp(v);
74  else if (name_ == "log")
75  return d / v;
76  else
77  throw Exception("MathOperator::getFirstOrderDerivative : unknown function " + name_);
78  }
79 
80  double getSecondOrderDerivative(const std::string& variable) const
81  {
82  double v = son_->getValue();
83  double d = son_->getFirstOrderDerivative(variable);
84  double d2 = son_->getSecondOrderDerivative(variable);
85 
86  if (name_ == "exp")
87  return (d2 + d * d) * exp(v);
88  else if (name_ == "log")
89  return (d2 * v - d * d) / (v * v);
90  else
91  throw Exception("MathOperator::getFirstOrderDerivative : unknown function " + name_);
92  return 0;
93  }
94 
95 
96  std::string output() const
97  {
98  return name_ + "(" + son_->output() + ")";
99  }
100 };
101 } // end of namespace bpp.
102 #endif // BPP_NUMERIC_FUNCTION_OPERATORS_MATHOPERATOR_H
std::string name_
Definition: MathOperator.h:31
double getFirstOrderDerivative(const std::string &variable) const
1st order derivative
Definition: MathOperator.h:67
std::string getName() const
Definition: MathOperator.h:58
double getSecondOrderDerivative(const std::string &variable) const
Definition: MathOperator.h:80
Implements a unary operator that applies a math (described in cmath) operator.
Definition: MathOperator.h:20
std::string output() const
Definition: MathOperator.h:96
double(* func_)(double)
Definition: MathOperator.h:29
MathOperator(double(*func)(double), std::string name, std::shared_ptr< Operator > son)
Definition: MathOperator.h:36
std::shared_ptr< Operator > getSon()
Definition: MathOperator.h:45
std::shared_ptr< Operator > son_
Definition: MathOperator.h:33
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
Definition: Exceptions.h:20
MathOperator * clone() const
Create a copy of this object and send a pointer to it.
Definition: MathOperator.h:40
Interface of operator for numerical computation.
Definition: Operator.h:19
double getValue() const
Definition: MathOperator.h:50