Public Member Functions

TRTK::Optimization::Gradient< Function, ValueType > Class Template Reference

Generates a functor which is the gradient of the given function. More...

#include <Optimization.hpp>

List of all members.

Public Member Functions

 Gradient (Function &function, Options< ValueType > options=Options< ValueType >())
 Constructs a gradient function of the given function.
virtual ~Gradient ()
 Destructs the instance of Gradient.
Coordinate< ValueType > operator() (const Coordinate< ValueType > &value)
 Returns the gradient.

Detailed Description

template<class Function, class ValueType>
class TRTK::Optimization::Gradient< Function, ValueType >

Generates a functor which is the gradient of the given function.

Parameters:
[in]functionScalar-valued multivariate function or functor.
[in]optionsOptions to control the operation of this function.

The gradient is computed using the five-point stencil method. The spacing of the finit differences can be set via options.

Note:
Unfortunately C++98 does not deduce the template arguments of classes. Use makeGradient() to circumvent that.

Example:

 #include <cassert>
 #include <cmath>
 #include <iostream>

 #include <TRTK/Coordinate.hpp>
 #include <TRTK/Optimization.hpp>

 using namespace std;
 using namespace TRTK;
 using namespace TRTK::Optimization;

 double f(const TRTK::Coordinate<double> & arg)
 {
     assert(arg.size() == 2);

     const double x = arg.x();
     const double y = arg.y();

     return (x - 2) * (x - 2)  - (y - 1) * (y - 1) - 9;
 }

 int main()
 {
     // Generate the gradient from f.
     // YOU MIGHT WANT TO HAVE A LOOK AT "makeGradient".

     Gradient<double (const Coordinate<double> &), double> gradient(f);

     Coordinate<double> arg(100, -2);
     cout << "Gradient(100, -2) = " << gradient(arg) << endl << endl;

     // Find the minimum of f (i.e. the zero of grad f).

     Result<double> result = solveNewtonRaphsonMethod(gradient, arg);

     cout << "Result:    " << endl
          << "Error      " << result.error << endl
          << "Iterations " << result.number_of_iterations << endl
          << "Root       " << result.root << endl;

     return 0;
 }

Output

 Gradient(100, -2) = (196, 6)

 Result:
 Error      0
 Iterations 3
 Root       (2, 1)
Macros:
If TRTK_PARALLELIZE_GRADIENT is defined Gradient will make use of OpenMP. Be aware that function needs to be reentrant (i.e. it can be safely executed concurrently).
Author:
Christoph Haenisch
Version:
0.1.1
Date:
last changed on 2013-08-15

Definition at line 366 of file Optimization.hpp.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines