Generates a functor which is the gradient of the given function. More...
#include <Optimization.hpp>
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. |
Generates a functor which is the gradient of the given function.
[in] | function | Scalar-valued multivariate function or functor. |
[in] | options | Options 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
.
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
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).Definition at line 366 of file Optimization.hpp.
Documentation generated by Doxygen