bpp-core3  3.0.0
BinaryOperator.h
Go to the documentation of this file.
1 //
2 // File: BinaryOperator.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_BINARYOPERATOR_H
42 #define BPP_NUMERIC_FUNCTION_OPERATORS_BINARYOPERATOR_H
43 
44 #include <memory>
45 
46 #include "Operator.h"
47 
48 namespace bpp
49 {
56  public Operator
57 {
58 private:
59  char symb_;
60 
61  std::shared_ptr<Operator> left_, right_;
62 
63 public:
64  BinaryOperator(char symb, std::shared_ptr<Operator> left, std::shared_ptr<Operator> right) :
65  symb_(symb), left_(left), right_(right)
66  {}
67 
69  {
70  return new BinaryOperator(*this);
71  }
72 
73  std::shared_ptr<Operator> getLeftSon()
74  {
75  return left_;
76  }
77 
78  std::shared_ptr<Operator> getRightSon()
79  {
80  return right_;
81  }
82 
83  char getSymbol() const
84  {
85  return symb_;
86  }
87 
88  double getValue() const
89  {
90  switch (symb_)
91  {
92  case '+':
93  return left_->getValue() + right_->getValue();
94  case '-':
95  return left_->getValue() - right_->getValue();
96  case '/':
97  if (right_->getValue() == 0)
98  return 0;
99 
100  return left_->getValue() / right_->getValue();
101  case '*':
102  return left_->getValue() * right_->getValue();
103  default:
104  return 0;
105  }
106  }
107 
108  double getFirstOrderDerivative(const std::string& variable) const
109  {
110  double dl = left_->getFirstOrderDerivative(variable);
111  double dr = right_->getFirstOrderDerivative(variable);
112  double l = left_->getValue();
113  double r = right_->getValue();
114 
115  switch (symb_)
116  {
117  case '+':
118  return dl + dr;
119  case '-':
120  return dl - dl;
121  case '/':
122  if (r == 0)
123  return 0;
124 
125  return (dl * r - dr * l ) / (r * r);
126  case '*':
127  return dl * r + dr * l;
128  default:
129  return 0;
130  }
131  return 0;
132  }
133 
134  double getSecondOrderDerivative(const std::string& variable) const
135  {
136  double d2l = left_->getSecondOrderDerivative(variable);
137  double d2r = right_->getSecondOrderDerivative(variable);
138  double l = left_->getValue();
139  double r = right_->getValue();
140  double dl = left_->getFirstOrderDerivative(variable);
141  double dr = right_->getFirstOrderDerivative(variable);
142 
143  double r2 = r * r;
144  double r3 = r * r2;
145 
146  switch (symb_)
147  {
148  case '+':
149  return d2l + d2r;
150  case '-':
151  return d2l - d2r;
152 
153  case '/':
154  if (r == 0)
155  return 0;
156 
157  return (d2l * r - d2r * l ) / r2 - ( 2 * dl * ( dl * r - dr * l) ) / r3;
158 
159  case '*':
160  return d2l * r + d2r * l + 2 * dr * dl;
161  default:
162  return 0;
163  }
164  }
165 
166  std::string output() const
167  {
168  return "(" + left_->output() + " " + symb_ + " " + right_->output() + ")";
169  }
170 };
171 } // end of namespace bpp.
172 #endif // BPP_NUMERIC_FUNCTION_OPERATORS_BINARYOPERATOR_H
Binary arithmetic operator for numerical computation.
double getFirstOrderDerivative(const std::string &variable) const
std::shared_ptr< Operator > right_
char getSymbol() const
BinaryOperator(char symb, std::shared_ptr< Operator > left, std::shared_ptr< Operator > right)
std::shared_ptr< Operator > left_
std::shared_ptr< Operator > getLeftSon()
BinaryOperator * clone() const
Create a copy of this object and send a pointer to it.
double getValue() const
double getSecondOrderDerivative(const std::string &variable) const
std::string output() const
std::shared_ptr< Operator > getRightSon()
Interface of operator for numerical computation.
Definition: Operator.h:57