bpp-core3  3.0.0
Functions.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_FUNCTIONS_H
6 #define BPP_NUMERIC_FUNCTION_FUNCTIONS_H
7 
8 
9 #include "../AbstractParametrizable.h"
10 #include "../ParameterExceptions.h"
11 #include "../ParameterList.h"
12 #include "../Parametrizable.h"
13 
14 // From Utils:
15 #include "../../Clonable.h"
16 #include "../../Exceptions.h"
17 
18 // From the STL:
19 #include <cmath>
20 #include <memory>
21 
22 namespace bpp
23 {
53  public virtual Parametrizable
54 {
55 public:
57  virtual ~FunctionInterface() {}
58 
59 public:
65  virtual void setParameters(const ParameterList& parameters) = 0;
66 
73  virtual double getValue() const = 0;
74 
82  virtual double f(const ParameterList& parameters)
83  {
84  setParameters(parameters);
85  return getValue();
86  }
87 
88  friend class FunctionWrapper;
89 };
90 
97  public virtual FunctionInterface
98 {
99 public:
101  virtual ~FirstOrderDerivable() {}
102 
103  FirstOrderDerivable* clone() const = 0;
104 
105 public:
111  virtual void enableFirstOrderDerivatives(bool yn) = 0;
112 
118  virtual bool enableFirstOrderDerivatives() const = 0;
119 
127  virtual double getFirstOrderDerivative(const std::string& variable) const = 0;
128 
138  virtual double df(const std::string& variable, const ParameterList& parameters)
139  {
140  setParameters(parameters);
141  return getFirstOrderDerivative(variable);
142  }
143 };
144 
152  public virtual FirstOrderDerivable
153 {
154 public:
157 
158  SecondOrderDerivable* clone() const = 0;
159 
160 public:
166  virtual void enableSecondOrderDerivatives(bool yn) = 0;
167 
173  virtual bool enableSecondOrderDerivatives() const = 0;
174 
182  virtual double getSecondOrderDerivative(const std::string& variable) const = 0;
183 
193  virtual double d2f(const std::string& variable, const ParameterList& parameters)
194  {
195  setParameters(parameters);
196  return getSecondOrderDerivative(variable);
197  }
198 
208  virtual double getSecondOrderDerivative(const std::string& variable1, const std::string& variable2) const = 0;
209 
220  virtual double d2f(const std::string& variable1, const std::string& variable2, const ParameterList& parameters)
221  {
222  setParameters(parameters);
223  return getSecondOrderDerivative(variable1, variable2);
224  }
225 };
226 
232  public virtual FunctionInterface
233 {
234 protected:
235  std::shared_ptr<FunctionInterface> function_;
236 
237 public:
238  FunctionWrapper(std::shared_ptr<FunctionInterface> function) : function_(function) {}
239  FunctionWrapper(const FunctionWrapper& fw) : function_(fw.function_) {}
241  {
242  function_ = fw.function_;
243  return *this;
244  }
245 
246 public:
247  bool hasParameter(const std::string& name) const override
248  {
249  return function_->hasParameter(name);
250  }
251 
252  void setParameters(const ParameterList& parameters) override
253  {
254  function_->setParameters(parameters);
255  }
256 
257  const ParameterList& getParameters() const override
258  {
259  return function_->getParameters();
260  }
261 
262  const Parameter& parameter(const std::string& name) const override
263  {
264  return function_->parameter(name);
265  }
266 
267  double getValue() const override
268  {
269  return function_->getValue();
270  }
271 
272  double f(const ParameterList& parameters) override
273  {
274  return function_->f(parameters);
275  }
276 
277  double getParameterValue(const std::string& name) const override
278  {
279  return function_->getParameterValue(name);
280  }
281 
282  void setAllParametersValues(const ParameterList& parameters) override
283  {
284  function_->setAllParametersValues(parameters);
285  }
286 
287  void setParameterValue(const std::string& name, double value) override
288  {
289  function_->setParameterValue(name, value);
290  }
291 
292  void setParametersValues(const ParameterList& parameters) override
293  {
294  function_->setParametersValues(parameters);
295  }
296 
297  bool matchParametersValues(const ParameterList& parameters) override
298  {
299  return function_->matchParametersValues(parameters);
300  }
301 
302  void removeConstraint(const std::string& name) override
303  {
304  return function_->removeConstraint(name);
305  }
306 
307  void setConstraint(const std::string& name, std::shared_ptr<ConstraintInterface> constraint) override
308  {
309  return function_->setConstraint(name, constraint);
310  }
311 
312  size_t getNumberOfParameters() const override
313  {
314  return function_->getNumberOfParameters();
315  }
316 
317  void setNamespace(const std::string& prefix) override
318  {
319  function_->setNamespace(prefix);
320  }
321 
322  std::string getNamespace() const override
323  {
324  return function_->getNamespace();
325  }
326 
327  std::string getParameterNameWithoutNamespace(const std::string& name) const override
328  {
329  return function_->getParameterNameWithoutNamespace(name);
330  }
331 
332 protected:
334  {
335  return function_->getParameters_();
336  }
337 };
338 
339 
345  public FunctionWrapper,
346  public virtual FirstOrderDerivable
347 {
348 public:
349  FirstOrderDerivableWrapper(std::shared_ptr<FirstOrderDerivable> function) :
350  FunctionWrapper(function) {}
351 
352 public:
354  {
355  std::dynamic_pointer_cast<FirstOrderDerivable>(function_)->enableFirstOrderDerivatives(yn);
356  }
357 
359  {
360  return std::dynamic_pointer_cast<FirstOrderDerivable>(function_)->enableFirstOrderDerivatives();
361  }
362 
363  double getFirstOrderDerivative(const std::string& variable) const
364  {
365  return std::dynamic_pointer_cast<FirstOrderDerivable>(function_)->getFirstOrderDerivative(variable);
366  }
367 };
368 
369 
376  public virtual SecondOrderDerivable
377 {
378 public:
379  SecondOrderDerivableWrapper(std::shared_ptr<SecondOrderDerivable> function) :
380  FirstOrderDerivableWrapper(function) {}
381 
382 public:
384  {
385  std::dynamic_pointer_cast<SecondOrderDerivable>(function_)->enableSecondOrderDerivatives(yn);
386  }
387 
389  {
390  return std::dynamic_pointer_cast<SecondOrderDerivable>(function_)->enableSecondOrderDerivatives();
391  }
392 
393  double getSecondOrderDerivative(const std::string& variable) const
394  {
395  return std::dynamic_pointer_cast<SecondOrderDerivable>(function_)->getSecondOrderDerivative(variable);
396  }
397 
398  double getSecondOrderDerivative(const std::string& variable1, const std::string& variable2) const
399  {
400  return std::dynamic_pointer_cast<SecondOrderDerivable>(function_)->getSecondOrderDerivative(variable1, variable2);
401  }
402 };
403 
404 
411  public FunctionWrapper
412 {
413 protected:
414  mutable bool constraintMatch_;
415 
416 public:
417  InfinityFunctionWrapper(std::shared_ptr<FunctionInterface> function) :
418  FunctionWrapper(function),
419  constraintMatch_(false) {}
421 
422  InfinityFunctionWrapper* clone() const override { return new InfinityFunctionWrapper(*this); }
423 
424 public:
425  void setParameters(const ParameterList& parameters) override
426  {
427  try
428  {
429  function_->setParameters(parameters);
430  constraintMatch_ = false;
431  }
432  catch (ConstraintException& ce)
433  {
434  constraintMatch_ = true;
435  }
436  }
437 
438  double getValue() const override
439  {
440  return constraintMatch_ ? -std::log(0.) : function_->getValue();
441  }
442 
443  double f(const ParameterList& parameters) override
444  {
445  setParameters(parameters);
446  return getValue();
447  }
448 
449  void setAllParametersValues(const ParameterList& parameters) override
450  {
451  try
452  {
453  function_->setAllParametersValues(parameters);
454  constraintMatch_ = false;
455  }
456  catch (ConstraintException& ce)
457  {
458  constraintMatch_ = true;
459  }
460  }
461 
462  void setParameterValue(const std::string& name, double value) override
463  {
464  try
465  {
466  function_->setParameterValue(name, value);
467  constraintMatch_ = false;
468  }
469  catch (ConstraintException& ce)
470  {
471  constraintMatch_ = true;
472  }
473  }
474 
475  void setParametersValues(const ParameterList& parameters) override
476  {
477  try
478  {
479  function_->setParametersValues(parameters);
480  constraintMatch_ = false;
481  }
482  catch (ConstraintException& ce)
483  {
484  constraintMatch_ = true;
485  }
486  }
487 
488  bool matchParametersValues(const ParameterList& parameters) override
489  {
490  try
491  {
492  bool test = function_->matchParametersValues(parameters);
493  constraintMatch_ = false;
494  return test;
495  }
496  catch (ConstraintException& ce)
497  {
498  constraintMatch_ = true;
499  return false;
500  }
501  }
502 };
503 
510  public virtual InfinityFunctionWrapper
511 {
512 public:
513  InfinityDerivableFirstOrderWrapper(std::shared_ptr<FirstOrderDerivable> function) :
514  InfinityFunctionWrapper(function) {}
516 
518 
519 public:
520  double getFirstOrderDerivative(const std::string& variable) const
521  {
522  return constraintMatch_ ? -std::log(0.) : (std::dynamic_pointer_cast<FirstOrderDerivable>(function_)->getFirstOrderDerivative(variable));
523  }
524 
525  double df(const std::string& variable, const ParameterList& parameters)
526  {
527  setParameters(parameters);
528  return getFirstOrderDerivative(variable);
529  }
530 };
531 
539 {
540 public:
541  InfinityDerivableSecondOrderWrapper(std::shared_ptr<FirstOrderDerivable> function) :
542  InfinityFunctionWrapper(function),
545 
547 
548 public:
549  double getSecondOrderDerivative(const std::string& variable) const
550  {
551  return constraintMatch_ ? -std::log(0.) : (std::dynamic_pointer_cast<SecondOrderDerivable>(function_)->getSecondOrderDerivative(variable));
552  }
553 
554  double d2f(const std::string& variable, const ParameterList& parameters)
555  {
556  setParameters(parameters);
557  return getSecondOrderDerivative(variable);
558  }
559 
560  double getSecondOrderDerivative(const std::string& variable1, const std::string& variable2) const
561  {
562  return constraintMatch_ ? -std::log(0.) : (std::dynamic_pointer_cast<SecondOrderDerivable>(function_)->getSecondOrderDerivative(variable1, variable2));
563  }
564 
565  double d2f(const std::string& variable1, const std::string& variable2, const ParameterList& parameters)
566  {
567  setParameters(parameters);
568  return getSecondOrderDerivative(variable1, variable2);
569  }
570 };
571 
572 
579  public virtual FunctionInterface,
581 {
582 public:
583  TestFunction(double x = 0, double y = 0) :
585  {
586  addParameter_(new Parameter("x", x));
587  addParameter_(new Parameter("y", y));
588  }
589 
590  Clonable* clone() const { return new TestFunction(*this); }
591 
592  void setParameters(const ParameterList& parameters)
593  {
594  matchParametersValues(parameters);
595  }
596 
597  double getValue() const
598  {
599  double x = parameter("x").getValue();
600  double y = parameter("y").getValue();
601  return x * x + y * y;
602  }
603 
604  void fireParameterChanged(const ParameterList& parameters) {}
605 };
606 } // end of namespace bpp.
607 #endif // BPP_NUMERIC_FUNCTION_FUNCTIONS_H
virtual bool matchParametersValues(const ParameterList &parameters)=0
Update the parameters from parameters.
double getValue() const override
Get the value of the function at the current point.
Definition: Functions.h:438
This is the abstract class for first order derivable functions.
Definition: Functions.h:96
Wrapper class for optimization under constraints.
Definition: Functions.h:537
FirstOrderDerivableWrapper(std::shared_ptr< FirstOrderDerivable > function)
Definition: Functions.h:349
void fireParameterChanged(const ParameterList &parameters)
Notify the class when one or several parameters have changed.
Definition: Functions.h:604
void enableFirstOrderDerivatives(bool yn)
Tell if derivatives must be computed.
Definition: Functions.h:353
double d2f(const std::string &variable, const ParameterList &parameters)
Definition: Functions.h:554
std::string getNamespace() const override
Definition: Functions.h:322
bool enableFirstOrderDerivatives() const
Tell if derivatives must be computed.
Definition: Functions.h:358
double getSecondOrderDerivative(const std::string &variable) const
Get the second order derivative of the function at the current point.
Definition: Functions.h:393
virtual double d2f(const std::string &variable1, const std::string &variable2, const ParameterList &parameters)
Get the value of the cross derivative of the function according to a given set of parameters...
Definition: Functions.h:220
void setAllParametersValues(const ParameterList &parameters) override
Set the parameters values to be equals to those of parameters.
Definition: Functions.h:449
A partial implementation of the Parametrizable interface.
virtual double d2f(const std::string &variable, const ParameterList &parameters)
Get the value of the second order derivative of the function according to a given set of parameters...
Definition: Functions.h:193
double getSecondOrderDerivative(const std::string &variable1, const std::string &variable2) const
Definition: Functions.h:560
virtual double df(const std::string &variable, const ParameterList &parameters)
Get the value of the first derivative of the function according to a given set of parameters...
Definition: Functions.h:138
double getFirstOrderDerivative(const std::string &variable) const
Get the derivative of the function at the current point.
Definition: Functions.h:363
bool matchParametersValues(const ParameterList &parameters) override
Update the parameters from parameters.
Definition: Functions.h:488
std::string getParameterNameWithoutNamespace(const std::string &name) const override
Resolves a parameter name according to the current namespace.
Definition: Functions.h:327
Wrapper class for optimization under constraints.
Definition: Functions.h:509
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:97
Clonable * clone() const
Create a copy of this object and send a pointer to it.
Definition: Functions.h:590
double getFirstOrderDerivative(const std::string &variable) const
Definition: Functions.h:520
virtual const Parameter & parameter(const std::string &name) const =0
Get the parameter with specified name.
bool enableSecondOrderDerivatives() const
Tell if derivatives must be computed.
Definition: Functions.h:388
This is the function abstract class.
Definition: Functions.h:52
SecondOrderDerivableWrapper(std::shared_ptr< SecondOrderDerivable > function)
Definition: Functions.h:379
void setParameters(const ParameterList &parameters) override
Set the point where the function must be computed.
Definition: Functions.h:425
void enableSecondOrderDerivatives(bool yn)
Tell if derivatives must be computed.
Definition: Functions.h:383
This is the interface for all objects that imply parameters.
void removeConstraint(const std::string &name) override
Remove the constraint associated with one parameter, if any.
Definition: Functions.h:302
The parameter list object.
Definition: ParameterList.h:27
void setParameterValue(const std::string &name, double value) override
Set the value of parameter with name name to be equal to value.
Definition: Functions.h:287
FunctionWrapper(const FunctionWrapper &fw)
Definition: Functions.h:239
void setConstraint(const std::string &name, std::shared_ptr< ConstraintInterface > constraint) override
Set/Change the constraint associated with one parameter.
Definition: Functions.h:307
virtual ~FunctionInterface()
Definition: Functions.h:57
void setParametersValues(const ParameterList &parameters) override
Update the parameters from parameters.
Definition: Functions.h:475
double getValue() const override
Get the value of the function at the current point.
Definition: Functions.h:267
Wrapper class for optimization under constraints.
Definition: Functions.h:410
virtual void setParameters(const ParameterList &parameters)=0
Set the point where the function must be computed.
void setParametersValues(const ParameterList &parameters) override
Update the parameters from parameters.
Definition: Functions.h:292
InfinityFunctionWrapper(std::shared_ptr< FunctionInterface > function)
Definition: Functions.h:417
General class that wraps a function into another one. This class is meant to be derivated and just pr...
Definition: Functions.h:344
void setAllParametersValues(const ParameterList &parameters) override
Set the parameters values to be equals to those of parameters.
Definition: Functions.h:282
General class that wraps a function into another one. This class is meant to be derivated and just pr...
Definition: Functions.h:231
TestFunction(double x=0, double y=0)
Definition: Functions.h:583
General class that wraps a function into another one. This class is meant to be derivated and just pr...
Definition: Functions.h:374
virtual ~InfinityFunctionWrapper()
Definition: Functions.h:420
virtual ~FirstOrderDerivable()
Definition: Functions.h:101
void setParameters(const ParameterList &parameters) override
Set the point where the function must be computed.
Definition: Functions.h:252
bool matchParametersValues(const ParameterList &parameters) override
Update the parameters from parameters.
Definition: Functions.h:297
FunctionWrapper(std::shared_ptr< FunctionInterface > function)
Definition: Functions.h:238
virtual Clonable * clone() const =0
Create a copy of this object and send a pointer to it.
InfinityDerivableFirstOrderWrapper(std::shared_ptr< FirstOrderDerivable > function)
Definition: Functions.h:513
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...
Definition: Functions.h:398
bool hasParameter(const std::string &name) const override
Tell if there is a parameter with specified name.
Definition: Functions.h:247
double getParameterValue(const std::string &name) const override
Get the value for parameter of name &#39;name&#39;.
Definition: Functions.h:277
InfinityDerivableSecondOrderWrapper(std::shared_ptr< FirstOrderDerivable > function)
Definition: Functions.h:541
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:63
A simple function with two parameters, mostly for testing and debugging :)
Definition: Functions.h:578
void setParameterValue(const std::string &name, double value) override
Set the value of parameter with name name to be equal to value.
Definition: Functions.h:462
const Parameter & parameter(const std::string &name) const override
Get the parameter with specified name.
Definition: Functions.h:262
InfinityDerivableFirstOrderWrapper * clone() const
Create a copy of this object and send a pointer to it.
Definition: Functions.h:517
size_t getNumberOfParameters() const override
Get the number of parameters.
Definition: Functions.h:312
virtual double getValue() const
Get the value of this parameter.
Definition: Parameter.h:181
InfinityDerivableSecondOrderWrapper * clone() const
Create a copy of this object and send a pointer to it.
Definition: Functions.h:546
double f(const ParameterList &parameters) override
Get the value of the function according to a given set of parameters.
Definition: Functions.h:272
void setParameters(const ParameterList &parameters)
Set the point where the function must be computed.
Definition: Functions.h:592
Exception thrown when a value do not match a given constraint.
double getSecondOrderDerivative(const std::string &variable) const
Definition: Functions.h:549
std::shared_ptr< FunctionInterface > function_
Definition: Functions.h:235
InfinityFunctionWrapper * clone() const override
Create a copy of this object and send a pointer to it.
Definition: Functions.h:422
ParameterList & getParameters_() override
Get all parameters available.
Definition: Functions.h:333
double d2f(const std::string &variable1, const std::string &variable2, const ParameterList &parameters)
Definition: Functions.h:565
void setNamespace(const std::string &prefix) override
Set the namespace for the parameter names.
Definition: Functions.h:317
FunctionWrapper & operator=(const FunctionWrapper &fw)
Definition: Functions.h:240
double getValue() const
Get the value of the function at the current point.
Definition: Functions.h:597
double df(const std::string &variable, const ParameterList &parameters)
Definition: Functions.h:525
double f(const ParameterList &parameters) override
Get the value of the function according to a given set of parameters.
Definition: Functions.h:443
virtual ~SecondOrderDerivable()
Definition: Functions.h:156
virtual double f(const ParameterList &parameters)
Get the value of the function according to a given set of parameters.
Definition: Functions.h:82
This is the abstract class for second order derivable functions.
Definition: Functions.h:151
const ParameterList & getParameters() const override
Get all parameters available.
Definition: Functions.h:257
virtual double getValue() const =0
Get the value of the function at the current point.