bpp-core3  3.0.0
Functions.h
Go to the documentation of this file.
1 //
2 // File: Functions.h
3 // Authors:
4 // Julien Dutheil
5 // Created: 2003-11-09 23:11:00
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_FUNCTIONS_H
42 #define BPP_NUMERIC_FUNCTION_FUNCTIONS_H
43 
44 
45 #include "../AbstractParametrizable.h"
46 #include "../ParameterExceptions.h"
47 #include "../ParameterList.h"
48 #include "../Parametrizable.h"
49 
50 // From Utils:
51 #include "../../Clonable.h"
52 #include "../../Exceptions.h"
53 
54 // From the STL:
55 #include <cmath>
56 
57 namespace bpp
58 {
87 class Function :
88  public virtual Parametrizable
89 {
90 public:
91  Function() {}
92  virtual ~Function() {}
93 
94 public:
100  virtual void setParameters(const ParameterList& parameters) = 0;
101 
108  virtual double getValue() const = 0;
109 
117  virtual double f(const ParameterList& parameters)
118  {
119  setParameters(parameters);
120  return getValue();
121  }
122 
123  friend class FunctionWrapper;
124 };
125 
132  public virtual Function
133 {
134 public:
136  virtual ~DerivableFirstOrder() {}
137 
139 
140 public:
146  virtual void enableFirstOrderDerivatives(bool yn) = 0;
147 
153  virtual bool enableFirstOrderDerivatives() const = 0;
154 
162  virtual double getFirstOrderDerivative(const std::string& variable) const = 0;
163 
173  virtual double df(const std::string& variable, const ParameterList& parameters)
174  {
175  setParameters(parameters);
176  return getFirstOrderDerivative(variable);
177  }
178 };
179 
187  public virtual DerivableFirstOrder
188 {
189 public:
192 
194 
195 public:
201  virtual void enableSecondOrderDerivatives(bool yn) = 0;
202 
208  virtual bool enableSecondOrderDerivatives() const = 0;
209 
217  virtual double getSecondOrderDerivative(const std::string& variable) const = 0;
218 
228  virtual double d2f(const std::string& variable, const ParameterList& parameters)
229  {
230  setParameters(parameters);
231  return getSecondOrderDerivative(variable);
232  }
233 
243  virtual double getSecondOrderDerivative(const std::string& variable1, const std::string& variable2) const = 0;
244 
255  virtual double d2f(const std::string& variable1, const std::string& variable2, const ParameterList& parameters)
256  {
257  setParameters(parameters);
258  return getSecondOrderDerivative(variable1, variable2);
259  }
260 };
261 
267  public virtual Function
268 {
269 protected:
271 
272 public:
273  FunctionWrapper(Function* function) : function_(function) {}
276  {
277  function_ = fw.function_;
278  return *this;
279  }
280 
281 public:
282  bool hasParameter(const std::string& name) const
283  {
284  return function_->hasParameter(name);
285  }
286 
287  void setParameters(const ParameterList& parameters)
288  {
289  function_->setParameters(parameters);
290  }
291 
293  {
294  return function_->getParameters();
295  }
296 
297  const Parameter& getParameter(const std::string& name) const
298  {
299  return function_->getParameter(name);
300  }
301 
302  double getValue() const
303  {
304  return function_->getValue();
305  }
306 
307  double f(const ParameterList& parameters)
308  {
309  return function_->f(parameters);
310  }
311 
312  double getParameterValue(const std::string& name) const
313  {
314  return function_->getParameterValue(name);
315  }
316 
317  void setAllParametersValues(const ParameterList& parameters)
318  {
319  function_->setAllParametersValues(parameters);
320  }
321 
322  void setParameterValue(const std::string& name, double value)
323  {
324  function_->setParameterValue(name, value);
325  }
326 
327  void setParametersValues(const ParameterList& parameters)
328  {
329  function_->setParametersValues(parameters);
330  }
331 
332  bool matchParametersValues(const ParameterList& parameters)
333  {
334  return function_->matchParametersValues(parameters);
335  }
336 
337  size_t getNumberOfParameters() const
338  {
340  }
341 
342  void setNamespace(const std::string& prefix)
343  {
344  function_->setNamespace(prefix);
345  }
346 
347  std::string getNamespace() const
348  {
349  return function_->getNamespace();
350  }
351 
352  std::string getParameterNameWithoutNamespace(const std::string& name) const
353  {
355  }
356 
357 protected:
359  {
360  return function_->getParameters_();
361  }
362 };
363 
364 
370  public FunctionWrapper,
371  public virtual DerivableFirstOrder
372 {
373 public:
375 
376 public:
378  {
380  }
381 
383  {
385  }
386 
387  double getFirstOrderDerivative(const std::string& variable) const
388  {
389  return dynamic_cast<DerivableFirstOrder*>(function_)->getFirstOrderDerivative(variable);
390  }
391 };
392 
393 
400  public virtual DerivableSecondOrder
401 {
402 public:
404 
405 public:
407  {
409  }
410 
412  {
414  }
415 
416  double getSecondOrderDerivative(const std::string& variable) const
417  {
418  return dynamic_cast<DerivableSecondOrder*>(function_)->getSecondOrderDerivative(variable);
419  }
420 
421  double getSecondOrderDerivative(const std::string& variable1, const std::string& variable2) const
422  {
423  return dynamic_cast<DerivableSecondOrder*>(function_)->getSecondOrderDerivative(variable1, variable2);
424  }
425 };
426 
427 
434  public FunctionWrapper
435 {
436 protected:
437  mutable bool constraintMatch_;
438 
439 public:
441  FunctionWrapper(function),
442  constraintMatch_(false) {}
444 
445  InfinityFunctionWrapper* clone() const { return new InfinityFunctionWrapper(*this); }
446 
447 public:
448  void setParameters(const ParameterList& parameters)
449  {
450  try
451  {
452  function_->setParameters(parameters);
453  constraintMatch_ = false;
454  }
455  catch (ConstraintException& ce)
456  {
457  constraintMatch_ = true;
458  }
459  }
460 
461  double getValue() const
462  {
463  return constraintMatch_ ? -std::log(0.) : function_->getValue();
464  }
465 
466  double f(const ParameterList& parameters)
467  {
468  setParameters(parameters);
469  return getValue();
470  }
471 
472  void setAllParametersValues(const ParameterList& parameters)
473  {
474  try
475  {
476  function_->setAllParametersValues(parameters);
477  constraintMatch_ = false;
478  }
479  catch (ConstraintException& ce)
480  {
481  constraintMatch_ = true;
482  }
483  }
484 
485  void setParameterValue(const std::string& name, double value)
486  {
487  try
488  {
489  function_->setParameterValue(name, value);
490  constraintMatch_ = false;
491  }
492  catch (ConstraintException& ce)
493  {
494  constraintMatch_ = true;
495  }
496  }
497 
498  void setParametersValues(const ParameterList& parameters)
499  {
500  try
501  {
502  function_->setParametersValues(parameters);
503  constraintMatch_ = false;
504  }
505  catch (ConstraintException& ce)
506  {
507  constraintMatch_ = true;
508  }
509  }
510 
511  bool matchParametersValues(const ParameterList& parameters)
512  {
513  try
514  {
515  bool test = function_->matchParametersValues(parameters);
516  constraintMatch_ = false;
517  return test;
518  }
519  catch (ConstraintException& ce)
520  {
521  constraintMatch_ = true;
522  return false;
523  }
524  }
525 };
526 
533  public virtual InfinityFunctionWrapper
534 {
535 public:
538 
540 
541 public:
542  double getFirstOrderDerivative(const std::string& variable) const
543  {
544  return constraintMatch_ ? -std::log(0.) : (dynamic_cast<DerivableFirstOrder*>(function_)->getFirstOrderDerivative(variable));
545  }
546 
547  double df(const std::string& variable, const ParameterList& parameters)
548  {
549  setParameters(parameters);
550  return getFirstOrderDerivative(variable);
551  }
552 };
553 
561 {
562 public:
564  InfinityFunctionWrapper(function),
567 
569 
570 public:
571  double getSecondOrderDerivative(const std::string& variable) const
572  {
573  return constraintMatch_ ? -std::log(0.) : (dynamic_cast<DerivableSecondOrder*>(function_)->getSecondOrderDerivative(variable));
574  }
575 
576  double d2f(const std::string& variable, const ParameterList& parameters)
577  {
578  setParameters(parameters);
579  return getSecondOrderDerivative(variable);
580  }
581 
582  double getSecondOrderDerivative(const std::string& variable1, const std::string& variable2) const
583  {
584  return constraintMatch_ ? -std::log(0.) : (dynamic_cast<DerivableSecondOrder*>(function_)->getSecondOrderDerivative(variable1, variable2));
585  }
586 
587  double d2f(const std::string& variable1, const std::string& variable2, const ParameterList& parameters)
588  {
589  setParameters(parameters);
590  return getSecondOrderDerivative(variable1, variable2);
591  }
592 };
593 
594 
601  public virtual Function,
603 {
604 public:
605  TestFunction(double x = 0, double y = 0) :
607  {
608  addParameter_(new Parameter("x", x));
609  addParameter_(new Parameter("y", y));
610  }
611 
612  Clonable* clone() const { return new TestFunction(*this); }
613 
614  void setParameters(const ParameterList& parameters)
615  {
616  matchParametersValues(parameters);
617  }
618 
619  double getValue() const
620  {
621  double x = getParameter("x").getValue();
622  double y = getParameter("y").getValue();
623  return x * x + y * y;
624  }
625 
626  void fireParameterChanged(const ParameterList& parameters) {}
627 };
628 } // end of namespace bpp.
629 #endif // BPP_NUMERIC_FUNCTION_FUNCTIONS_H
A partial implementation of the Parametrizable interface.
virtual void addParameter_(Parameter *parameter)
const Parameter & getParameter(const std::string &name) const
Get the parameter with specified name.
bool matchParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:103
Exception thrown when a value do not match a given constraint.
General class that wraps a function into another one. This class is meant to be derivated and just pr...
Definition: Functions.h:372
double getFirstOrderDerivative(const std::string &variable) const
Get the derivative of the function at the current point.
Definition: Functions.h:387
bool enableFirstOrderDerivatives() const
Tell if derivatives must be computed.
Definition: Functions.h:382
void enableFirstOrderDerivatives(bool yn)
Tell if derivatives must be computed.
Definition: Functions.h:377
DerivableFirstOrderWrapper(DerivableFirstOrder *function)
Definition: Functions.h:374
This is the abstract class for first order derivable functions.
Definition: Functions.h:133
virtual double getFirstOrderDerivative(const std::string &variable) const =0
Get the derivative of the function at the current point.
virtual void enableFirstOrderDerivatives(bool yn)=0
Tell if derivatives must be computed.
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:173
virtual ~DerivableFirstOrder()
Definition: Functions.h:136
virtual bool enableFirstOrderDerivatives() const =0
Tell if derivatives must be computed.
DerivableFirstOrder * clone() const =0
Create a copy of this object and send a pointer to it.
General class that wraps a function into another one. This class is meant to be derivated and just pr...
Definition: Functions.h:401
bool enableSecondOrderDerivatives() const
Tell if derivatives must be computed.
Definition: Functions.h:411
void enableSecondOrderDerivatives(bool yn)
Tell if derivatives must be computed.
Definition: Functions.h:406
double getSecondOrderDerivative(const std::string &variable) const
Get the second order derivative of the function at the current point.
Definition: Functions.h:416
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:421
DerivableSecondOrderWrapper(DerivableSecondOrder *function)
Definition: Functions.h:403
This is the abstract class for second order derivable functions.
Definition: Functions.h:188
DerivableSecondOrder * clone() const =0
Create a copy of this object and send a pointer to it.
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:228
virtual double getSecondOrderDerivative(const std::string &variable) const =0
Get the second order derivative of the function at the current point.
virtual ~DerivableSecondOrder()
Definition: Functions.h:191
virtual double getSecondOrderDerivative(const std::string &variable1, const std::string &variable2) const =0
Get the value of the cross derivative of the function according to a given set of parameters.
virtual void enableSecondOrderDerivatives(bool yn)=0
Tell if derivatives must be computed.
virtual bool enableSecondOrderDerivatives() const =0
Tell if derivatives must be computed.
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:255
General class that wraps a function into another one. This class is meant to be derivated and just pr...
Definition: Functions.h:268
FunctionWrapper & operator=(const FunctionWrapper &fw)
Definition: Functions.h:275
FunctionWrapper(const FunctionWrapper &fw)
Definition: Functions.h:274
const Parameter & getParameter(const std::string &name) const
Get the parameter with specified name.
Definition: Functions.h:297
Function * function_
Definition: Functions.h:270
void setParameters(const ParameterList &parameters)
Set the point where the function must be computed.
Definition: Functions.h:287
FunctionWrapper(Function *function)
Definition: Functions.h:273
void setNamespace(const std::string &prefix)
Set the namespace for the parameter names.
Definition: Functions.h:342
void setAllParametersValues(const ParameterList &parameters)
Set the parameters values to be equals to those of parameters.
Definition: Functions.h:317
std::string getNamespace() const
Definition: Functions.h:347
double getValue() const
Get the value of the function at the current point.
Definition: Functions.h:302
void setParameterValue(const std::string &name, double value)
Set the value of parameter with name name to be equal to value.
Definition: Functions.h:322
double f(const ParameterList &parameters)
Get the value of the function according to a given set of parameters.
Definition: Functions.h:307
size_t getNumberOfParameters() const
Get the number of parameters.
Definition: Functions.h:337
std::string getParameterNameWithoutNamespace(const std::string &name) const
Resolves a parameter name according to the current namespace.
Definition: Functions.h:352
void setParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
Definition: Functions.h:327
double getParameterValue(const std::string &name) const
Get the value for parameter of name 'name'.
Definition: Functions.h:312
const ParameterList & getParameters() const
Get all parameters available.
Definition: Functions.h:292
bool hasParameter(const std::string &name) const
Tell if there is a parameter with specified name.
Definition: Functions.h:282
ParameterList & getParameters_()
Get all parameters available.
Definition: Functions.h:358
bool matchParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
Definition: Functions.h:332
This is the function abstract class.
Definition: Functions.h:89
virtual ~Function()
Definition: Functions.h:92
virtual double getValue() const =0
Get the value of the function at the current point.
virtual void setParameters(const ParameterList &parameters)=0
Set the point where the function must be computed.
virtual double f(const ParameterList &parameters)
Get the value of the function according to a given set of parameters.
Definition: Functions.h:117
Wrapper class for optimization under constraints.
Definition: Functions.h:534
InfinityDerivableFirstOrderWrapper(DerivableFirstOrder *function)
Definition: Functions.h:536
double getFirstOrderDerivative(const std::string &variable) const
Definition: Functions.h:542
InfinityDerivableFirstOrderWrapper * clone() const
Create a copy of this object and send a pointer to it.
Definition: Functions.h:539
double df(const std::string &variable, const ParameterList &parameters)
Definition: Functions.h:547
Wrapper class for optimization under constraints.
Definition: Functions.h:561
double d2f(const std::string &variable, const ParameterList &parameters)
Definition: Functions.h:576
InfinityDerivableSecondOrderWrapper(DerivableFirstOrder *function)
Definition: Functions.h:563
double getSecondOrderDerivative(const std::string &variable1, const std::string &variable2) const
Definition: Functions.h:582
InfinityDerivableSecondOrderWrapper * clone() const
Create a copy of this object and send a pointer to it.
Definition: Functions.h:568
double d2f(const std::string &variable1, const std::string &variable2, const ParameterList &parameters)
Definition: Functions.h:587
double getSecondOrderDerivative(const std::string &variable) const
Definition: Functions.h:571
Wrapper class for optimization under constraints.
Definition: Functions.h:435
void setParameterValue(const std::string &name, double value)
Set the value of parameter with name name to be equal to value.
Definition: Functions.h:485
InfinityFunctionWrapper * clone() const
Create a copy of this object and send a pointer to it.
Definition: Functions.h:445
virtual ~InfinityFunctionWrapper()
Definition: Functions.h:443
void setParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
Definition: Functions.h:498
double f(const ParameterList &parameters)
Get the value of the function according to a given set of parameters.
Definition: Functions.h:466
bool matchParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
Definition: Functions.h:511
double getValue() const
Get the value of the function at the current point.
Definition: Functions.h:461
InfinityFunctionWrapper(Function *function)
Definition: Functions.h:440
void setAllParametersValues(const ParameterList &parameters)
Set the parameters values to be equals to those of parameters.
Definition: Functions.h:472
void setParameters(const ParameterList &parameters)
Set the point where the function must be computed.
Definition: Functions.h:448
The parameter list object.
Definition: ParameterList.h:65
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
virtual double getValue() const
Get the value of this parameter.
Definition: Parameter.h:218
This is the interface for all objects that imply parameters.
virtual std::string getParameterNameWithoutNamespace(const std::string &name) const =0
Resolves a parameter name according to the current namespace.
virtual size_t getNumberOfParameters() const =0
Get the number of parameters.
virtual bool matchParametersValues(const ParameterList &parameters)=0
Update the parameters from parameters.
virtual std::string getNamespace() const =0
virtual void setAllParametersValues(const ParameterList &parameters)=0
Set the parameters values to be equals to those of parameters.
virtual double getParameterValue(const std::string &name) const =0
Get the value for parameter of name 'name'.
virtual const Parameter & getParameter(const std::string &name) const =0
Get the parameter with specified name.
virtual const ParameterList & getParameters() const =0
Get all parameters available.
virtual ParameterList & getParameters_()=0
Get all parameters available.
virtual bool hasParameter(const std::string &name) const =0
Tell if there is a parameter with specified name.
virtual void setParameterValue(const std::string &name, double value)=0
Set the value of parameter with name name to be equal to value.
virtual void setParametersValues(const ParameterList &parameters)=0
Update the parameters from parameters.
virtual void setNamespace(const std::string &prefix)=0
Set the namespace for the parameter names.
A simple funciton with two parameters, mostly for testing and debugging :)
Definition: Functions.h:603
TestFunction(double x=0, double y=0)
Definition: Functions.h:605
Clonable * clone() const
Create a copy of this object and send a pointer to it.
Definition: Functions.h:612
double getValue() const
Get the value of the function at the current point.
Definition: Functions.h:619
void fireParameterChanged(const ParameterList &parameters)
Notify the class when one or several parameters have changed.
Definition: Functions.h:626
void setParameters(const ParameterList &parameters)
Set the point where the function must be computed.
Definition: Functions.h:614