bpp-core3  3.0.0
ReparametrizationFunctionWrapper.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_REPARAMETRIZATIONFUNCTIONWRAPPER_H
6 #define BPP_NUMERIC_FUNCTION_REPARAMETRIZATIONFUNCTIONWRAPPER_H
7 
8 
9 #include "../AbstractParametrizable.h"
10 #include "../TransformedParameter.h"
11 #include "Functions.h"
12 
13 namespace bpp
14 {
22  public virtual FunctionInterface,
24 {
25 protected:
26  std::shared_ptr<FunctionInterface> function_;
28 
29 public:
36  ReparametrizationFunctionWrapper(std::shared_ptr<FunctionInterface> function, bool verbose = true) :
38  function_(function),
39  functionParameters_(function->getParameters())
40  {
41  init_(verbose);
42  }
43 
53  ReparametrizationFunctionWrapper(std::shared_ptr<FunctionInterface> function, const ParameterList& parameters, bool verbose = true) :
55  function_(function),
56  functionParameters_(function->getParameters().getCommonParametersWith(parameters))
57  {
58  init_(verbose);
59  }
60 
63  function_(rfw.function_),
64  functionParameters_(rfw.functionParameters_) {}
65 
67  {
68  AbstractParametrizable::operator=(rfw),
69  function_ = rfw.function_;
70  functionParameters_ = rfw.functionParameters_;
71  return *this;
72  }
73 
75 
77 
78 private:
79  void init_(bool verbose);
80 
81 public:
82  virtual const FunctionInterface& function() const { return *function_; }
83 
84  virtual FunctionInterface& function() { return *function_; }
85 
86  virtual std::shared_ptr<const FunctionInterface> getFunction() const { return function_; }
87 
88  virtual std::shared_ptr<FunctionInterface> getFunction() { return function_; }
89 
90  void setParameters(const ParameterList& parameters)
91  {
92 // parameters.printParameters(std::cout);
93  matchParametersValues(parameters);
94  // We only set parameters that have been changed:
95 // functionParameters_.printParameters(std::cout);
96  function_->setParameters(functionParameters_.createSubList(parameters.getParameterNames()));
97  }
98 
99  double getValue() const
100  {
101  return function_->getValue();
102  }
103 
104  void fireParameterChanged (const ParameterList& parameters);
105 };
106 
114  public virtual FirstOrderDerivable,
116 {
117 public:
124  ReparametrizationDerivableFirstOrderWrapper(std::shared_ptr<FirstOrderDerivable> function, bool verbose = true) :
126  {}
127 
137  ReparametrizationDerivableFirstOrderWrapper(std::shared_ptr<FirstOrderDerivable> function, const ParameterList& parameters, bool verbose = true) :
138  ReparametrizationFunctionWrapper(function, parameters, verbose)
139  {}
140 
142 
144 
145 private:
146  void init_(bool verbose);
147 
148 public:
149  void enableFirstOrderDerivatives(bool yn) { std::dynamic_pointer_cast<FirstOrderDerivable>(function_)->enableFirstOrderDerivatives(yn); }
150 
151  bool enableFirstOrderDerivatives() const { return std::dynamic_pointer_cast<const FirstOrderDerivable>(function_)->enableFirstOrderDerivatives(); }
152 
153  double getFirstOrderDerivative(const std::string& variable) const
154  {
155  return std::dynamic_pointer_cast<const FirstOrderDerivable>(function_)->getFirstOrderDerivative(variable)
156  * dynamic_cast<const TransformedParameter&>(parameter(variable)).getFirstOrderDerivative();
157  }
158 };
159 
160 
168  public virtual SecondOrderDerivable,
170 {
171 public:
178  ReparametrizationDerivableSecondOrderWrapper(std::shared_ptr<SecondOrderDerivable> function, bool verbose = true) :
180  {}
181 
191  ReparametrizationDerivableSecondOrderWrapper(std::shared_ptr<SecondOrderDerivable> function, const ParameterList& parameters, bool verbose = true) :
193  {}
194 
196 
198 
199 private:
200  void init_(bool verbose);
201 
202 public:
204 
205  bool enableSecondOrderDerivatives() const { return std::dynamic_pointer_cast<const SecondOrderDerivable>(function_)->enableSecondOrderDerivatives(); }
206 
207  double getSecondOrderDerivative(const std::string& variable) const
208  {
209  return std::dynamic_pointer_cast<const SecondOrderDerivable>(function_)->getSecondOrderDerivative(variable)
210  * std::pow(dynamic_cast<const TransformedParameter&>(parameter(variable)).getFirstOrderDerivative(), 2)
211  + std::dynamic_pointer_cast<const SecondOrderDerivable>(function_)->getFirstOrderDerivative(variable)
212  * dynamic_cast<const TransformedParameter&>(parameter(variable)).getSecondOrderDerivative();
213  }
214 
215  double getSecondOrderDerivative(const std::string& variable1, const std::string& variable2) const
216  {
217  return std::dynamic_pointer_cast<const SecondOrderDerivable>(function_)->getSecondOrderDerivative(variable1, variable2)
218  * dynamic_cast<const TransformedParameter&>(parameter(variable1)).getFirstOrderDerivative()
219  * dynamic_cast<const TransformedParameter&>(parameter(variable2)).getFirstOrderDerivative();
220  }
221 };
222 } // end of namespace bpp.
223 #endif // BPP_NUMERIC_FUNCTION_REPARAMETRIZATIONFUNCTIONWRAPPER_H
ReparametrizationFunctionWrapper(const ReparametrizationFunctionWrapper &rfw)
This is the abstract class for first order derivable functions.
Definition: Functions.h:96
double getSecondOrderDerivative(const std::string &variable1, const std::string &variable2) const
Get the value of the cross derivative of the function according to a given set of parameters...
bool enableSecondOrderDerivatives() const
Tell if derivatives must be computed.
std::string getNamespace() const override
ReparametrizationFunctionWrapper & operator=(const ReparametrizationFunctionWrapper &rfw)
virtual ParameterList createSubList(const std::vector< std::string > &names) const
Get given parameters as a sublist.
ReparametrizationFunctionWrapper(std::shared_ptr< FunctionInterface > function, bool verbose=true)
Build a new reparametrization wrapper for the given function, using all available parameters...
void fireParameterChanged(const ParameterList &parameters)
Notify the class when one or several parameters have changed.
A partial implementation of the Parametrizable interface.
std::shared_ptr< FunctionInterface > function_
ReparametrizationDerivableSecondOrderWrapper(std::shared_ptr< SecondOrderDerivable > function, bool verbose=true)
Build a new reparametrization wrapper for the given function, using all available parameters...
double getFirstOrderDerivative(const std::string &variable) const
Get the derivative of the function at the current point.
This is the function abstract class.
Definition: Functions.h:52
ReparametrizationDerivableFirstOrderWrapper(std::shared_ptr< FirstOrderDerivable > function, bool verbose=true)
Build a new reparametrization wrapper for the given function, using all available parameters...
void enableFirstOrderDerivatives(bool yn)
Tell if derivatives must be computed.
The parameter list object.
Definition: ParameterList.h:27
virtual const FunctionInterface & function() const
virtual std::shared_ptr< const FunctionInterface > getFunction() const
double getValue() const
Get the value of the function at the current point.
Function wrapper that remove simple constraints on parameters. Also transform first and second order ...
ReparametrizationDerivableSecondOrderWrapper(std::shared_ptr< SecondOrderDerivable > function, const ParameterList &parameters, bool verbose=true)
Build a new reparametrization wrapper for the given function, using only the specified parameters...
double getSecondOrderDerivative(const std::string &variable) const
Get the second order derivative of the function at the current point.
bool matchParametersValues(const ParameterList &parameters) override
Update the parameters from parameters.
virtual std::vector< std::string > getParameterNames() const
Get all parameter names in the list.
bool enableFirstOrderDerivatives() const
Tell if derivatives must be computed.
ReparametrizationDerivableSecondOrderWrapper * clone() const
Create a copy of this object and send a pointer to it.
ReparametrizationDerivableFirstOrderWrapper * clone() const
Create a copy of this object and send a pointer to it.
void enableSecondOrderDerivatives(bool yn)
Tell if derivatives must be computed.
virtual std::shared_ptr< FunctionInterface > getFunction()
ReparametrizationFunctionWrapper(std::shared_ptr< FunctionInterface > function, const ParameterList &parameters, bool verbose=true)
Build a new reparametrization wrapper for the given function, using only the specified parameters...
Function wrapper that remove simple constraints on parameters.
Function wrapper that remove simple constraints on parameters. Also transform first order derivatives...
ReparametrizationFunctionWrapper * clone() const
Create a copy of this object and send a pointer to it.
void setParameters(const ParameterList &parameters)
Set the point where the function must be computed.
The TransformedParameter abstract class.
ReparametrizationDerivableFirstOrderWrapper(std::shared_ptr< FirstOrderDerivable > function, const ParameterList &parameters, bool verbose=true)
Build a new reparametrization wrapper for the given function, using only the specified parameters...
const Parameter & parameter(const std::string &name) const override
Get the parameter with specified name.
const ParameterList & getParameters() const override
Get all parameters available.
This is the abstract class for second order derivable functions.
Definition: Functions.h:151