bpp-core  2.2.0
Functions.h
Go to the documentation of this file.
1 //
2 // File: Functions.h
3 // Created by: Julien Dutheil
4 // Created on: Sun Nov 9 23:11:00 2003
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
9 
10 This software is a computer program whose purpose is to provide classes
11 for numerical calculus.
12 
13 This software is governed by the CeCILL license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's author, the holder of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #ifndef _FUNCTIONS_H_
41 #define _FUNCTIONS_H_
42 
43 #include "../ParameterList.h"
44 #include "../Parametrizable.h"
45 #include "../AbstractParametrizable.h"
46 #include "../ParameterExceptions.h"
47 
48 // From Utils:
49 #include "../../Clonable.h"
50 #include "../../Exceptions.h"
51 
52 // From the STL:
53 #include <cmath>
54 
55 namespace bpp
56 {
57 
86 class Function:
87  public virtual Parametrizable
88 {
89  public:
90  Function() {}
91  virtual ~Function() {}
92 
93  public:
94 
100  virtual void setParameters(const ParameterList& parameters) throw (ParameterNotFoundException, ConstraintException, Exception) = 0;
101 
108  virtual double getValue() const throw (Exception) = 0;
109 
117  virtual double f(const ParameterList& parameters) throw (Exception)
118  {
119  setParameters(parameters);
120  return getValue();
121  }
122 };
123 
130  public virtual Function
131 {
132  public:
134  virtual ~DerivableFirstOrder() {}
135 
136 #if defined(NO_VIRTUAL_COV)
137  Clonable* clone() const = 0;
138 #else
139  DerivableFirstOrder* clone() const = 0;
140 #endif
141 
142  public:
143 
149  virtual void enableFirstOrderDerivatives(bool yn) = 0;
150 
156  virtual bool enableFirstOrderDerivatives() const = 0;
157 
165  virtual double getFirstOrderDerivative(const std::string& variable) const throw (Exception) = 0;
166 
176  virtual double df(const std::string& variable, const ParameterList& parameters) throw (Exception)
177  {
178  setParameters(parameters);
179  return getFirstOrderDerivative(variable);
180  }
181 };
182 
190  public virtual DerivableFirstOrder
191 {
192  public:
195 
196 #if defined(NO_VIRTUAL_COV)
197  Clonable* clone() const = 0;
198 #else
199  DerivableSecondOrder* clone() const = 0;
200 #endif
201 
202  public:
203 
209  virtual void enableSecondOrderDerivatives(bool yn) = 0;
210 
216  virtual bool enableSecondOrderDerivatives() const = 0;
217 
225  virtual double getSecondOrderDerivative(const std::string& variable) const throw (Exception) = 0;
226 
236  virtual double d2f(const std::string& variable, const ParameterList& parameters) throw (Exception)
237  {
238  setParameters(parameters);
239  return getSecondOrderDerivative(variable);
240  }
241 
251  virtual double getSecondOrderDerivative(const std::string& variable1, const std::string& variable2) const throw (Exception) = 0;
252 
263  virtual double d2f(const std::string& variable1, const std::string& variable2, const ParameterList& parameters) throw (Exception)
264  {
265  setParameters(parameters);
266  return getSecondOrderDerivative(variable1, variable2);
267  }
268 };
269 
275  public virtual Function
276 {
277  protected:
279 
280  public:
281  FunctionWrapper(Function* function) : function_(function) {}
284  {
285  function_ = fw.function_;
286  return *this;
287  }
288 
289  public:
290  bool hasParameter(const std::string& name) const
291  {
292  return function_->hasParameter(name);
293  }
294 
295  void setParameters(const ParameterList & parameters)
297  {
298  function_->setParameters(parameters);
299  }
300 
301  const ParameterList& getParameters() const throw (Exception)
302  {
303  return function_->getParameters();
304  }
305 
306  const Parameter& getParameter(const std::string & name) const throw (ParameterNotFoundException)
307  {
308  return function_->getParameter(name);
309  }
310 
311  double getValue() const throw (Exception)
312  {
313  return function_->getValue();
314  }
315 
316  double f(const ParameterList& parameters) throw (Exception)
317  {
318  return function_->f(parameters);
319  }
320 
321  double getParameterValue(const std::string& name) const throw (ParameterNotFoundException)
322  {
323  return function_->getParameterValue(name);
324  }
325 
326  void setAllParametersValues(const ParameterList & parameters)
328  {
329  function_->setAllParametersValues(parameters);
330  }
331 
332  void setParameterValue(const std::string& name, double value)
334  {
335  function_->setParameterValue(name, value);
336  }
337 
338  void setParametersValues(const ParameterList& parameters)
340  {
341  function_->setParametersValues(parameters);
342  }
343 
344  bool matchParametersValues(const ParameterList& parameters)
345  throw (ConstraintException)
346  {
347  return function_->matchParametersValues(parameters);
348  }
349 
350  size_t getNumberOfParameters() const
351  {
353  }
354 
355  void setNamespace(const std::string& prefix)
356  {
357  function_->setNamespace(prefix);
358  }
359 
360  std::string getNamespace() const
361  {
362  return function_->getNamespace();
363  }
364 
365  std::string getParameterNameWithoutNamespace(const std::string& name) const
366  {
368  }
369 
370 };
371 
372 
373 
379  public FunctionWrapper,
380  public virtual DerivableFirstOrder
381 {
382  public:
384 
385  public:
388  }
389 
392  }
393 
394  double getFirstOrderDerivative(const std::string& variable) const throw (Exception) {
395  return dynamic_cast<DerivableFirstOrder*>(function_)->getFirstOrderDerivative(variable);
396  }
397 
398 };
399 
400 
401 
408  public virtual DerivableSecondOrder
409 {
410  public:
412 
413  public:
416  }
417 
420  }
421 
422  double getSecondOrderDerivative(const std::string& variable) const throw (Exception) {
423  return dynamic_cast<DerivableSecondOrder*>(function_)->getSecondOrderDerivative(variable);
424  }
425 
426  double getSecondOrderDerivative(const std::string& variable1, const std::string& variable2) const throw (Exception) {
427  return dynamic_cast<DerivableSecondOrder*>(function_)->getSecondOrderDerivative(variable1, variable2);
428  }
429 
430 };
431 
432 
433 
440  public FunctionWrapper
441 {
442  protected:
443  mutable bool constraintMatch_;
444 
445  public:
447  FunctionWrapper(function),
448  constraintMatch_(false) {}
450 
451 #if defined(NO_VIRTUAL_COV)
452  Clonable* clone() const { return new InfinityFunctionWrapper(*this); }
453 #else
454  InfinityFunctionWrapper* clone() const { return new InfinityFunctionWrapper(*this); }
455 #endif
456 
457  public:
458 
459  void setParameters(const ParameterList& parameters)
461  {
462  try
463  {
464  function_->setParameters(parameters);
465  constraintMatch_ = false;
466  }
467  catch(ConstraintException& ce)
468  {
469  constraintMatch_ = true;
470  }
471  }
472 
473  double getValue() const throw (Exception)
474  {
475  return constraintMatch_ ? -log(0.) : function_->getValue();
476  }
477 
478  double f(const ParameterList& parameters) throw (Exception)
479  {
480  setParameters(parameters);
481  return getValue();
482  }
483 
484  void setAllParametersValues(const ParameterList & parameters)
486  {
487  try
488  {
489  function_->setAllParametersValues(parameters);
490  constraintMatch_ = false;
491  }
492  catch(ConstraintException& ce)
493  {
494  constraintMatch_ = true;
495  }
496  }
497 
498  void setParameterValue(const std::string& name, double value)
500  {
501  try
502  {
503  function_->setParameterValue(name, value);
504  constraintMatch_ = false;
505  }
506  catch(ConstraintException& ce)
507  {
508  constraintMatch_ = true;
509  }
510  }
511 
512  void setParametersValues(const ParameterList& parameters)
514  {
515  try
516  {
517  function_->setParametersValues(parameters);
518  constraintMatch_ = false;
519  }
520  catch(ConstraintException& ce)
521  {
522  constraintMatch_ = true;
523  }
524  }
525 
526  bool matchParametersValues(const ParameterList& parameters)
527  throw (ConstraintException)
528  {
529  try
530  {
531  bool test = function_->matchParametersValues(parameters);
532  constraintMatch_ = false;
533  return test;
534  }
535  catch (ConstraintException& ce)
536  {
537  constraintMatch_ = true;
538  return false;
539  }
540  }
541 
542 };
543 
550  public virtual InfinityFunctionWrapper
551 {
552  public:
555 
556 #if defined(NO_VIRTUAL_COV)
557  Clonable* clone() const { return new InfinityDerivableFirstOrderWrapper(*this); }
558 #else
560 #endif
561 
562  public:
563 
564  double getFirstOrderDerivative(const std::string& variable) const throw (Exception)
565  {
566  return constraintMatch_ ? -log(0.) : (dynamic_cast<DerivableFirstOrder *>(function_)->getFirstOrderDerivative(variable));
567  }
568 
569  double df(const std::string& variable, const ParameterList& parameters) throw (Exception)
570  {
571  setParameters(parameters);
572  return getFirstOrderDerivative(variable);
573  }
574 };
575 
583 {
584  public:
586  InfinityFunctionWrapper(function),
589 
590 #if defined(NO_VIRTUAL_COV)
591  Clonable* clone() const { return new InfinityDerivableSecondOrderWrapper(*this); }
592 #else
594 #endif
595 
596  public:
597 
598  double getSecondOrderDerivative(const std::string& variable) const throw (Exception)
599  {
600  return constraintMatch_ ? -log(0.) : (dynamic_cast<DerivableSecondOrder *>(function_)->getSecondOrderDerivative(variable));
601  }
602 
603  double d2f(const std::string & variable, const ParameterList& parameters) throw (Exception)
604  {
605  setParameters(parameters);
606  return getSecondOrderDerivative(variable);
607  }
608 
609  double getSecondOrderDerivative(const std::string& variable1, const std::string& variable2) const throw (Exception)
610  {
611  return constraintMatch_ ? -log(0.) : (dynamic_cast<DerivableSecondOrder *>(function_)->getSecondOrderDerivative(variable1, variable2));
612  }
613 
614  double d2f(const std::string & variable1, const std::string& variable2, const ParameterList& parameters) throw (Exception)
615  {
616  setParameters(parameters);
617  return getSecondOrderDerivative(variable1, variable2);
618  }
619 };
620 
621 
628  public virtual Function,
630 {
631  public:
632  TestFunction(double x = 0, double y = 0) :
634  {
635  addParameter_(new Parameter("x", x));
636  addParameter_(new Parameter("y", y));
637  }
638 
639  Clonable* clone() const { return new TestFunction(*this); }
640 
641  void setParameters(const ParameterList& parameters) throw (Exception)
642  {
643  matchParametersValues(parameters);
644  }
645 
646  double getValue() const throw (Exception)
647  {
648  double x = getParameter("x").getValue();
649  double y = getParameter("y").getValue();
650  return (x*x + y*y);
651  }
652 
653  void fireParameterChanged(const ParameterList& parameters) {}
654 };
655 
656 } //end of namespace bpp.
657 
658 #endif //_FUNCTIONS_H_
659 
bool enableFirstOrderDerivatives() const
Tell if derivatives must be computed.
Definition: Functions.h:390
bool enableSecondOrderDerivatives() const
Tell if derivatives must be computed.
Definition: Functions.h:418
virtual bool matchParametersValues(const ParameterList &parameters)=0
Update the parameters from parameters.
Exception thrown when a parameter is not found, for instance in a ParameterList.
Wrapper class for optimization under constraints.
Definition: Functions.h:581
virtual ~DerivableSecondOrder()
Definition: Functions.h:194
void fireParameterChanged(const ParameterList &parameters)
Notify the class when one or several parameters have changed.
Definition: Functions.h:653
double d2f(const std::string &variable, const ParameterList &parameters)
Definition: Functions.h:603
size_t getNumberOfParameters() const
Get the number of parameters.
Definition: Functions.h:350
FunctionWrapper(Function *function)
Definition: Functions.h:281
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
void setParameterValue(const std::string &name, double value)
Set the value of parameter with name name to be equal to value.
Definition: Functions.h:498
void setAllParametersValues(const ParameterList &parameters)
Set the parameters values to be equals to those of parameters.
Definition: Functions.h:326
InfinityDerivableFirstOrderWrapper(DerivableFirstOrder *function)
Definition: Functions.h:553
virtual void setParameterValue(const std::string &name, double value)=0
Set the value of parameter with name name to be equal to value.
This class allows to perform a correspondence analysis.
double getFirstOrderDerivative(const std::string &variable) const
Get the derivative of the function at the current point.
Definition: Functions.h:394
virtual double getParameterValue(const std::string &name) const =0
Get the value for parameter of name &#39;name&#39;.
A partial implementation of the Parametrizable interface.
double getSecondOrderDerivative(const std::string &variable1, const std::string &variable2) const
Definition: Functions.h:609
This is the function abstract class.
Definition: Functions.h:86
void setParameters(const ParameterList &parameters)
Set the point where the function must be computed.
Definition: Functions.h:295
std::string getNamespace() const
Definition: Functions.h:360
Function * function_
Definition: Functions.h:278
virtual std::string getNamespace() const =0
double getValue() const
Get the value of the function at the current point.
Definition: Functions.h:311
virtual const ParameterList & getParameters() const =0
Get all parameters available.
General class that wraps a function into another one. This class is meant to be derivated and just pr...
Definition: Functions.h:406
Wrapper class for optimization under constraints.
Definition: Functions.h:549
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
Clonable * clone() const
Create a copy of this object and send a pointer to it.
Definition: Functions.h:639
virtual void setParametersValues(const ParameterList &parameters)=0
Update the parameters from parameters.
virtual bool enableSecondOrderDerivatives() const =0
Tell if derivatives must be computed.
double getFirstOrderDerivative(const std::string &variable) const
Definition: Functions.h:564
virtual bool enableFirstOrderDerivatives() const =0
Tell if derivatives must be computed.
void setNamespace(const std::string &prefix)
Set the namespace for the parameter names.
Definition: Functions.h:355
virtual double getSecondOrderDerivative(const std::string &variable) const =0
Get the second order derivative of the function at the current point.
This is the interface for all objects that imply parameters.
virtual double getFirstOrderDerivative(const std::string &variable) const =0
Get the derivative of the function at the current point.
The parameter list object.
Definition: ParameterList.h:61
DerivableSecondOrderWrapper(DerivableSecondOrder *function)
Definition: Functions.h:411
void setParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
Definition: Functions.h:512
DerivableFirstOrderWrapper(DerivableFirstOrder *function)
Definition: Functions.h:383
bool matchParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
Definition: Functions.h:526
FunctionWrapper(const FunctionWrapper &fw)
Definition: Functions.h:282
std::string getParameterNameWithoutNamespace(const std::string &name) const
Resolves a parameter name according to the current namespace.
Definition: Functions.h:365
DerivableSecondOrder * clone() const =0
Create a copy of this object and send a pointer to it.
void setParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
Definition: Functions.h:338
Wrapper class for optimization under constraints.
Definition: Functions.h:439
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:263
virtual void setAllParametersValues(const ParameterList &parameters)=0
Set the parameters values to be equals to those of parameters.
General class that wraps a function into another one. This class is meant to be derivated and just pr...
Definition: Functions.h:274
double getSecondOrderDerivative(const std::string &variable) const
Get the second order derivative of the function at the current point.
Definition: Functions.h:422
bool matchParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
TestFunction(double x=0, double y=0)
Definition: Functions.h:632
const Parameter & getParameter(const std::string &name) const
Get the parameter with specified name.
virtual ~InfinityFunctionWrapper()
Definition: Functions.h:449
InfinityFunctionWrapper(Function *function)
Definition: Functions.h:446
double f(const ParameterList &parameters)
Get the value of the function according to a given set of parameters.
Definition: Functions.h:478
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:176
virtual ~Function()
Definition: Functions.h:91
InfinityDerivableSecondOrderWrapper(DerivableFirstOrder *function)
Definition: Functions.h:585
DerivableFirstOrder * clone() const =0
Create a copy of this object and send a pointer to it.
double f(const ParameterList &parameters)
Get the value of the function according to a given set of parameters.
Definition: Functions.h:316
void setParameterValue(const std::string &name, double value)
Set the value of parameter with name name to be equal to value.
Definition: Functions.h:332
void addParameter_(Parameter *parameter)
virtual std::string getParameterNameWithoutNamespace(const std::string &name) const =0
Resolves a parameter name according to the current namespace.
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:236
virtual ~DerivableFirstOrder()
Definition: Functions.h:134
Exception base class.
Definition: Exceptions.h:57
bool matchParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
Definition: Functions.h:344
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:99
A simple funciton with two parameters, mostly for testing and debugging :)
Definition: Functions.h:627
This is the abstract class for second order derivable functions.
Definition: Functions.h:189
const ParameterList & getParameters() const
Get all parameters available.
Definition: Functions.h:301
double getParameterValue(const std::string &name) const
Get the value for parameter of name &#39;name&#39;.
Definition: Functions.h:321
bool hasParameter(const std::string &name) const
Tell if there is a parameter with specified name.
Definition: Functions.h:290
InfinityDerivableFirstOrderWrapper * clone() const
Create a copy of this object and send a pointer to it.
Definition: Functions.h:559
This is the abstract class for first order derivable functions.
Definition: Functions.h:129
virtual double getValue() const
Get the value of this parameter.
Definition: Parameter.h:241
InfinityDerivableSecondOrderWrapper * clone() const
Create a copy of this object and send a pointer to it.
Definition: Functions.h:593
virtual const Parameter & getParameter(const std::string &name) const =0
Get the parameter with specified name.
void setParameters(const ParameterList &parameters)
Set the point where the function must be computed.
Definition: Functions.h:641
Exception thrown when a value do not match a given constraint.
virtual void setNamespace(const std::string &prefix)=0
Set the namespace for the parameter names.
double getSecondOrderDerivative(const std::string &variable) const
Definition: Functions.h:598
virtual bool hasParameter(const std::string &name) const =0
Tell if there is a parameter with specified name.
double d2f(const std::string &variable1, const std::string &variable2, const ParameterList &parameters)
Definition: Functions.h:614
FunctionWrapper & operator=(const FunctionWrapper &fw)
Definition: Functions.h:283
double getValue() const
Get the value of the function at the current point.
Definition: Functions.h:646
double df(const std::string &variable, const ParameterList &parameters)
Definition: Functions.h:569
virtual size_t getNumberOfParameters() const =0
Get the number of parameters.
void enableSecondOrderDerivatives(bool yn)
Tell if derivatives must be computed.
Definition: Functions.h:414
void setAllParametersValues(const ParameterList &parameters)
Set the parameters values to be equals to those of parameters.
Definition: Functions.h:484
void setParameters(const ParameterList &parameters)
Set the point where the function must be computed.
Definition: Functions.h:459
void enableFirstOrderDerivatives(bool yn)
Tell if derivatives must be computed.
Definition: Functions.h:386
General class that wraps a function into another one. This class is meant to be derivated and just pr...
Definition: Functions.h:378
double getValue() const
Get the value of the function at the current point.
Definition: Functions.h:473
const Parameter & getParameter(const std::string &name) const
Get the parameter with specified name.
Definition: Functions.h:306
virtual double getValue() const =0
Get the value of the function at the current point.
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:426
InfinityFunctionWrapper * clone() const
Create a copy of this object and send a pointer to it.
Definition: Functions.h:454