/usr/include/Bpp/Numeric/Function/Functions.h is in libbpp-core-dev 2.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 | //
// File: Functions.h
// Created by: Julien Dutheil
// Created on: Sun Nov 9 23:11:00 2003
//
/*
Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
This software is a computer program whose purpose is to provide classes
for numerical calculus.
This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software. You can use,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
As a counterpart to the access to the source code and rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty and the software's author, the holder of the
economic rights, and the successive licensors have only limited
liability.
In this respect, the user's attention is drawn to the risks associated
with loading, using, modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean that it is complicated to manipulate, and that also
therefore means that it is reserved for developers and experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and, more generally, to use and operate it in the
same conditions as regards security.
The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.
*/
#ifndef _FUNCTIONS_H_
#define _FUNCTIONS_H_
#include "../ParameterList.h"
#include "../Parametrizable.h"
#include "../AbstractParametrizable.h"
#include "../ParameterExceptions.h"
// From Utils:
#include "../../Clonable.h"
#include "../../Exceptions.h"
// From the STL:
#include <cmath>
namespace bpp
{
/**
* @brief This is the function abstract class.
*
* This class provides the interface for function objet
* and a default implementation of the f() function.
*
* The f() function sends the value of the function according to a
* given set of parameters.
*
* However for complexe function like likelihood for instance,
* computing the function value takes some time, and one do not want
* to perform several times the computation for an identical set of
* parameters.
* The setParameters() method hence allows to set the parameter value
* for which the function is to be computed, perform the computation
* and store the results.
* The getValue() methods send the result of the computation.
* One may hence access to the result of the computation by calling the
* getvalue() method without re-computating the function.
* The f(parameters) function is a shortcut for
* @code
* setParameters(parameters);
* return getValue();
* @endcode
* for convinience.
*
* @see Parameter, ParameterList
*/
class Function:
public virtual Parametrizable
{
public:
Function() {}
virtual ~Function() {}
public:
/**
* @brief Set the point where the function must be computed.
*
* @param parameters The parameter set to pass to the function.
*/
virtual void setParameters(const ParameterList& parameters) throw (ParameterNotFoundException, ConstraintException, Exception) = 0;
/**
* @brief Get the value of the function at the current point.
*
* @return The value of the function.
* @throw Exception If no point is specified or if an error occured.
*/
virtual double getValue() const throw (Exception) = 0;
/**
* @brief Get the value of the function according to a given set of parameters.
*
* @param parameters The parameter set to pass to the function.
* @return The value of the function with the given parameter set.
* @throw Exception If an error occured.
*/
virtual double f(const ParameterList& parameters) throw (Exception)
{
setParameters(parameters);
return getValue();
}
};
/**
* @brief This is the abstract class for first order derivable functions.
*
* This class adds the getFirstOrderDerivative() and df() shortcut functions.
*/
class DerivableFirstOrder:
public virtual Function
{
public:
DerivableFirstOrder() {}
virtual ~DerivableFirstOrder() {}
#if defined(NO_VIRTUAL_COV)
Clonable* clone() const = 0;
#else
DerivableFirstOrder* clone() const = 0;
#endif
public:
/**
* @brief Tell if derivatives must be computed.
*
* @param yn yes/no
*/
virtual void enableFirstOrderDerivatives(bool yn) = 0;
/**
* @brief Tell if derivatives must be computed.
*
* @return yes/no
*/
virtual bool enableFirstOrderDerivatives() const = 0;
/**
* @brief Get the derivative of the function at the current point.
*
* @param variable The name of the @f$ x @f$ variable in @f$ \frac{df}{dx} @f$.
* @return The value of the function.
* @throw Exception If no point is specified or if an error occured.
*/
virtual double getFirstOrderDerivative(const std::string& variable) const throw (Exception) = 0;
/**
* @brief Get the value of the first derivative of the function
* according to a given set of parameters.
*
* @param variable The name of the @f$ x @f$ variable in @f$ \frac{df}{dx} @f$.
* @param parameters The parameter set to pass to the function.
* @return The value of the function with the given parameter set.
* @throw Exception If an error occured.
*/
virtual double df(const std::string& variable, const ParameterList& parameters) throw (Exception)
{
setParameters(parameters);
return getFirstOrderDerivative(variable);
}
};
/**
* @brief This is the abstract class for second order derivable functions.
*
* This class adds the getSecondOrderDerivative() and d2f() shortcut functions.
* Cross derivative functions are also provided.
*/
class DerivableSecondOrder:
public virtual DerivableFirstOrder
{
public:
DerivableSecondOrder() {}
virtual ~DerivableSecondOrder() {}
#if defined(NO_VIRTUAL_COV)
Clonable* clone() const = 0;
#else
DerivableSecondOrder* clone() const = 0;
#endif
public:
/**
* @brief Tell if derivatives must be computed.
*
* @param yn yes/no
*/
virtual void enableSecondOrderDerivatives(bool yn) = 0;
/**
* @brief Tell if derivatives must be computed.
*
* @return yes/no
*/
virtual bool enableSecondOrderDerivatives() const = 0;
/**
* @brief Get the second order derivative of the function at the current point.
*
* @param variable The name of the @f$ x @f$ variable in @f$ \frac{\partial^2 f}{\partial x^2} @f$.
* @return The value of the function.
* @throw Exception If no point is specified or if an error occured.
*/
virtual double getSecondOrderDerivative(const std::string& variable) const throw (Exception) = 0;
/**
* @brief Get the value of the second order derivative of the function
* according to a given set of parameters.
*
* @param variable The name of the @f$ x @f$ variable in @f$ \frac{\partial^2 f}{\partial x^2} @f$.
* @param parameters The parameter set to pass to the function.
* @return The value of the function with the given parameter set.
* @throw Exception If an error occured.
*/
virtual double d2f(const std::string& variable, const ParameterList& parameters) throw (Exception)
{
setParameters(parameters);
return getSecondOrderDerivative(variable);
}
/**
* @brief Get the value of the cross derivative of the function
* according to a given set of parameters.
*
* @param variable1 The name of the @f$ x @f$ variable in @f$ \frac{\partial^2 f}{\partial x \partial y} @f$.
* @param variable2 The name of the @f$ y @f$ variable in @f$ \frac{\partial^2 f}{\partial x \partial y} @f$.
* @return The value of the function with the given parameter set.
* @throw Exception If an error occured.
*/
virtual double getSecondOrderDerivative(const std::string& variable1, const std::string& variable2) const throw (Exception) = 0;
/**
* @brief Get the value of the cross derivative of the function
* according to a given set of parameters.
*
* @param variable1 The name of the @f$ x @f$ variable in @f$ \frac{\partial^2 f}{\partial x \partial y} @f$.
* @param variable2 The name of the @f$ y @f$ variable in @f$ \frac{\partial^2 f}{\partial x \partial y} @f$.
* @param parameters The parameter set to pass to the function.
* @return The value of the function with the given parameter set.
* @throw Exception If an error occured.
*/
virtual double d2f(const std::string& variable1, const std::string& variable2, const ParameterList& parameters) throw (Exception)
{
setParameters(parameters);
return getSecondOrderDerivative(variable1, variable2);
}
};
/**
* @brief General class that wraps a function into another one.
* This class is meant to be derivated and just provides a general framework.
*/
class FunctionWrapper:
public virtual Function
{
protected:
Function* function_;
public:
FunctionWrapper(Function* function) : function_(function) {}
FunctionWrapper(const FunctionWrapper& fw) : function_(fw.function_) {}
FunctionWrapper& operator=(const FunctionWrapper& fw)
{
function_ = fw.function_;
return *this;
}
public:
bool hasParameter(const std::string& name) const
{
return function_->hasParameter(name);
}
void setParameters(const ParameterList & parameters)
throw (ParameterNotFoundException, ConstraintException)
{
function_->setParameters(parameters);
}
const ParameterList& getParameters() const throw (Exception)
{
return function_->getParameters();
}
const Parameter& getParameter(const std::string & name) const throw (ParameterNotFoundException)
{
return function_->getParameter(name);
}
double getValue() const throw (Exception)
{
return function_->getValue();
}
double f(const ParameterList& parameters) throw (Exception)
{
return function_->f(parameters);
}
double getParameterValue(const std::string& name) const throw (ParameterNotFoundException)
{
return function_->getParameterValue(name);
}
void setAllParametersValues(const ParameterList & parameters)
throw (ParameterNotFoundException, ConstraintException)
{
function_->setAllParametersValues(parameters);
}
void setParameterValue(const std::string& name, double value)
throw (ParameterNotFoundException, ConstraintException)
{
function_->setParameterValue(name, value);
}
void setParametersValues(const ParameterList& parameters)
throw (ParameterNotFoundException, ConstraintException)
{
function_->setParametersValues(parameters);
}
bool matchParametersValues(const ParameterList& parameters)
throw (ConstraintException)
{
return function_->matchParametersValues(parameters);
}
size_t getNumberOfParameters() const
{
return function_->getNumberOfParameters();
}
void setNamespace(const std::string& prefix)
{
function_->setNamespace(prefix);
}
std::string getNamespace() const
{
return function_->getNamespace();
}
std::string getParameterNameWithoutNamespace(const std::string& name) const
{
return function_->getParameterNameWithoutNamespace(name);
}
};
/**
* @brief General class that wraps a function into another one.
* This class is meant to be derivated and just provides a general framework.
*/
class DerivableFirstOrderWrapper:
public FunctionWrapper,
public virtual DerivableFirstOrder
{
public:
DerivableFirstOrderWrapper(DerivableFirstOrder* function) : FunctionWrapper(function) {}
public:
void enableFirstOrderDerivatives(bool yn) {
dynamic_cast<DerivableFirstOrder*>(function_)->enableFirstOrderDerivatives(yn);
}
bool enableFirstOrderDerivatives() const {
return dynamic_cast<DerivableFirstOrder*>(function_)->enableFirstOrderDerivatives();
}
double getFirstOrderDerivative(const std::string& variable) const throw (Exception) {
return dynamic_cast<DerivableFirstOrder*>(function_)->getFirstOrderDerivative(variable);
}
};
/**
* @brief General class that wraps a function into another one.
* This class is meant to be derivated and just provides a general framework.
*/
class DerivableSecondOrderWrapper:
public DerivableFirstOrderWrapper,
public virtual DerivableSecondOrder
{
public:
DerivableSecondOrderWrapper(DerivableSecondOrder* function) : DerivableFirstOrderWrapper(function) {}
public:
void enableSecondOrderDerivatives(bool yn) {
dynamic_cast<DerivableSecondOrder*>(function_)->enableSecondOrderDerivatives(yn);
}
bool enableSecondOrderDerivatives() const {
return dynamic_cast<DerivableSecondOrder*>(function_)->enableSecondOrderDerivatives();
}
double getSecondOrderDerivative(const std::string& variable) const throw (Exception) {
return dynamic_cast<DerivableSecondOrder*>(function_)->getSecondOrderDerivative(variable);
}
double getSecondOrderDerivative(const std::string& variable1, const std::string& variable2) const throw (Exception) {
return dynamic_cast<DerivableSecondOrder*>(function_)->getSecondOrderDerivative(variable1, variable2);
}
};
/**
* @brief Wrapper class for optimization under constraints.
*
* Catch any ConstraintException thrown and send +inf.
*/
class InfinityFunctionWrapper:
public FunctionWrapper
{
protected:
mutable bool constraintMatch_;
public:
InfinityFunctionWrapper(Function* function) :
FunctionWrapper(function),
constraintMatch_(false) {}
virtual ~InfinityFunctionWrapper() {}
#if defined(NO_VIRTUAL_COV)
Clonable* clone() const { return new InfinityFunctionWrapper(*this); }
#else
InfinityFunctionWrapper* clone() const { return new InfinityFunctionWrapper(*this); }
#endif
public:
void setParameters(const ParameterList& parameters)
throw (ParameterNotFoundException, ConstraintException)
{
try
{
function_->setParameters(parameters);
constraintMatch_ = false;
}
catch(ConstraintException& ce)
{
constraintMatch_ = true;
}
}
double getValue() const throw (Exception)
{
return constraintMatch_ ? -log(0.) : function_->getValue();
}
double f(const ParameterList& parameters) throw (Exception)
{
setParameters(parameters);
return getValue();
}
void setAllParametersValues(const ParameterList & parameters)
throw (ParameterNotFoundException, ConstraintException)
{
try
{
function_->setAllParametersValues(parameters);
constraintMatch_ = false;
}
catch(ConstraintException& ce)
{
constraintMatch_ = true;
}
}
void setParameterValue(const std::string& name, double value)
throw (ParameterNotFoundException, ConstraintException)
{
try
{
function_->setParameterValue(name, value);
constraintMatch_ = false;
}
catch(ConstraintException& ce)
{
constraintMatch_ = true;
}
}
void setParametersValues(const ParameterList& parameters)
throw (ParameterNotFoundException, ConstraintException)
{
try
{
function_->setParametersValues(parameters);
constraintMatch_ = false;
}
catch(ConstraintException& ce)
{
constraintMatch_ = true;
}
}
bool matchParametersValues(const ParameterList& parameters)
throw (ConstraintException)
{
try
{
bool test = function_->matchParametersValues(parameters);
constraintMatch_ = false;
return test;
}
catch (ConstraintException& ce)
{
constraintMatch_ = true;
return false;
}
}
};
/**
* @brief Wrapper class for optimization under constraints.
*
* Catch any ConstraintException thrown and send +inf.
*/
class InfinityDerivableFirstOrderWrapper :
public virtual InfinityFunctionWrapper
{
public:
InfinityDerivableFirstOrderWrapper(DerivableFirstOrder* function) : InfinityFunctionWrapper(function) {}
virtual ~InfinityDerivableFirstOrderWrapper() {}
#if defined(NO_VIRTUAL_COV)
Clonable* clone() const { return new InfinityDerivableFirstOrderWrapper(*this); }
#else
InfinityDerivableFirstOrderWrapper* clone() const { return new InfinityDerivableFirstOrderWrapper(*this); }
#endif
public:
double getFirstOrderDerivative(const std::string& variable) const throw (Exception)
{
return constraintMatch_ ? -log(0.) : (dynamic_cast<DerivableFirstOrder *>(function_)->getFirstOrderDerivative(variable));
}
double df(const std::string& variable, const ParameterList& parameters) throw (Exception)
{
setParameters(parameters);
return getFirstOrderDerivative(variable);
}
};
/**
* @brief Wrapper class for optimization under constraints.
*
* Catch any ConstraintException thrown and send +inf.
*/
class InfinityDerivableSecondOrderWrapper :
public virtual InfinityDerivableFirstOrderWrapper
{
public:
InfinityDerivableSecondOrderWrapper(DerivableFirstOrder* function):
InfinityFunctionWrapper(function),
InfinityDerivableFirstOrderWrapper(function) {}
virtual ~InfinityDerivableSecondOrderWrapper() {}
#if defined(NO_VIRTUAL_COV)
Clonable* clone() const { return new InfinityDerivableSecondOrderWrapper(*this); }
#else
InfinityDerivableSecondOrderWrapper* clone() const { return new InfinityDerivableSecondOrderWrapper(*this); }
#endif
public:
double getSecondOrderDerivative(const std::string& variable) const throw (Exception)
{
return constraintMatch_ ? -log(0.) : (dynamic_cast<DerivableSecondOrder *>(function_)->getSecondOrderDerivative(variable));
}
double d2f(const std::string & variable, const ParameterList& parameters) throw (Exception)
{
setParameters(parameters);
return getSecondOrderDerivative(variable);
}
double getSecondOrderDerivative(const std::string& variable1, const std::string& variable2) const throw (Exception)
{
return constraintMatch_ ? -log(0.) : (dynamic_cast<DerivableSecondOrder *>(function_)->getSecondOrderDerivative(variable1, variable2));
}
double d2f(const std::string & variable1, const std::string& variable2, const ParameterList& parameters) throw (Exception)
{
setParameters(parameters);
return getSecondOrderDerivative(variable1, variable2);
}
};
/**
* @brief A simple funciton with two parameters, mostly for testing and debugging :)
*
* @author Julien Dutheil.
*/
class TestFunction :
public virtual Function,
public AbstractParametrizable
{
public:
TestFunction(double x = 0, double y = 0) :
AbstractParametrizable("")
{
addParameter_(new Parameter("x", x));
addParameter_(new Parameter("y", y));
}
Clonable* clone() const { return new TestFunction(*this); }
void setParameters(const ParameterList& parameters) throw (Exception)
{
matchParametersValues(parameters);
}
double getValue() const throw (Exception)
{
double x = getParameter("x").getValue();
double y = getParameter("y").getValue();
return (x*x + y*y);
}
void fireParameterChanged(const ParameterList& parameters) {}
};
} //end of namespace bpp.
#endif //_FUNCTIONS_H_
|