5 #ifndef BPP_NUMERIC_FUNCTION_NUMERICALDERIVATIVE_H 6 #define BPP_NUMERIC_FUNCTION_NUMERICALDERIVATIVE_H 9 #include "../Matrix/Matrix.h" 73 mutable std::map<std::string, size_t>
index_;
82 h_(0.0001), variables_(), index_(), der1_(), der2_(), crossDer2_(),
83 computeD1_(true), computeD2_(true), computeCrossD2_(false) {}
87 h_(0.0001), variables_(), index_(), der1_(), der2_(), crossDer2_(),
88 computeD1_(true), computeD2_(true), computeCrossD2_(false) {}
92 h_(0.0001), variables_(), index_(), der1_(), der2_(), crossDer2_(),
93 computeD1_(true), computeD2_(true), computeCrossD2_(false) {}
96 FunctionWrapper(ad), function1_(ad.function1_), function2_(ad.function2_),
97 h_(ad.h_), variables_(ad.variables_), index_(ad.index_), der1_(ad.der1_), der2_(ad.der2_), crossDer2_(ad.crossDer2_),
98 computeD1_(ad.computeD1_), computeD2_(ad.computeD2_), computeCrossD2_(ad.computeCrossD2_) {}
128 variables_ = variables;
130 for (
size_t i = 0; i < variables_.size(); i++)
132 index_[variables_[i]] = i;
134 der1_.resize(variables_.size());
135 der2_.resize(variables_.size());
136 crossDer2_.
resize(variables_.size(), variables_.size());
149 std::map<std::string, size_t>::iterator it = index_.find(variable);
150 if (computeD1_ && it != index_.end())
151 return der1_[it->second];
154 return function1_->getFirstOrderDerivative(variable);
156 throw Exception(
"First order derivative not computed for variable " + variable +
".");
170 std::map<std::string, size_t>::iterator it = index_.find(variable);
171 if (computeD2_ && it != index_.end())
172 return der2_[it->second];
175 return function2_->getSecondOrderDerivative(variable);
177 throw Exception(
"Second order derivative not computed for variable " + variable +
".");
182 std::map<std::string, size_t>::iterator it1 = index_.find(variable1);
183 std::map<std::string, size_t>::iterator it2 = index_.find(variable2);
184 if (computeCrossD2_ && it1 != index_.end() && it2 != index_.end())
return crossDer2_(it1->second, it2->second);
187 return function2_->getSecondOrderDerivative(variable1, variable2);
189 throw Exception(
"Cross second order derivative not computed for variables " + variable1 +
" and " + variable2 +
".");
206 function_->setParameters(parameters);
207 updateDerivatives(parameters);
211 function_->setAllParametersValues(parameters);
212 updateDerivatives(parameters);
217 function_->setParameterValue(name, value);
218 updateDerivatives(function_->getParameters().createSubList(name));
223 function_->setParametersValues(parameters);
224 updateDerivatives(parameters);
229 bool test = function_->matchParametersValues(parameters);
230 updateDerivatives(parameters);
245 virtual void updateDerivatives(
const ParameterList& parameters) = 0;
248 #endif // BPP_NUMERIC_FUNCTION_NUMERICALDERIVATIVE_H
void enableSecondOrderCrossDerivatives(bool yn)
virtual void setParametersToDerivate(const std::vector< std::string > &variables)=0
Set the list of parameters to derivate numerically.
double getSecondOrderDerivative(const std::string &variable) const override
Get the second order derivative of the function at the current point.
void setParameterValue(const std::string &name, double value) override
Set the value of parameter with name name to be equal to value.
bool enableFirstOrderDerivatives() const override
Tell if derivatives must be computed.
virtual double getInterval() const =0
std::shared_ptr< SecondOrderDerivable > function2_
bool enableSecondOrderCrossDerivatives() const
void setParametersToDerivate(const std::vector< std::string > &variables) override
Set the list of parameters to derivate numerically.
void enableSecondOrderDerivatives(bool yn) override
Tell if derivatives must be computed.
RowMatrix< double > crossDer2_
std::vector< std::string > variables_
bool matchParametersValues(const ParameterList ¶meters) override
Update the parameters from parameters.
Numerical derivative function wrapper, partial implementation.
The parameter list object.
bool enableSecondOrderDerivatives() const override
Tell if derivatives must be computed.
AbstractNumericalDerivative(const AbstractNumericalDerivative &ad)
double getFirstOrderDerivative(const std::string &variable) const override
Get the derivative of the function at the current point.
AbstractNumericalDerivative(std::shared_ptr< SecondOrderDerivable > function)
double getInterval() const override
void enableFirstOrderDerivatives(bool yn) override
Tell if derivatives must be computed.
virtual void setParameters(const ParameterList ¶meters)=0
Set the point where the function must be computed.
AbstractNumericalDerivative(std::shared_ptr< FunctionInterface > function)
void resize(size_t nRows, size_t nCols)
Resize the matrix.
General class that wraps a function into another one. This class is meant to be derivated and just pr...
std::shared_ptr< FirstOrderDerivable > function1_
double getSecondOrderDerivative(const std::string &variable1, const std::string &variable2) const override
Get the value of the cross derivative of the function according to a given set of parameters...
virtual ~AbstractNumericalDerivative()
AbstractNumericalDerivative(std::shared_ptr< FirstOrderDerivable > function)
void setParametersValues(const ParameterList ¶meters) override
Update the parameters from parameters.
void setAllParametersValues(const ParameterList ¶meters) override
Set the parameters values to be equals to those of parameters.
Numerical derivative function wrapper.
std::map< std::string, size_t > index_
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
void setInterval(double h) override
Set the interval value used in numerical approximation.
void setParameters(const ParameterList ¶meters) override
Set the point where the function must be computed.
std::vector< double > der2_
AbstractNumericalDerivative & operator=(const AbstractNumericalDerivative &ad)
SecondOrderDerivable * clone() const =0
Create a copy of this object and send a pointer to it.
std::vector< double > der1_
FunctionWrapper & operator=(const FunctionWrapper &fw)
virtual void setInterval(double h)=0
Set the interval value used in numerical approximation.
This is the abstract class for second order derivable functions.
double f(const ParameterList ¶meters) override
Get the value of the function according to a given set of parameters.
virtual double getValue() const =0
Get the value of the function at the current point.