Optimo  0.1.0
A C++ header library for optimization
 All Classes Functions Variables Pages
numerical_gradient.h
1 // Copyright (C) 2013 Victor Fragoso <vfragoso@cs.ucsb.edu>
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following
13 // disclaimer in the documentation and/or other materials provided
14 // with the distribution.
15 //
16 // * Neither the name of the University of California, Santa Barbara nor the
17 // names of its contributors may be used to endorse or promote products
18 // derived from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 // ARE DISCLAIMED. IN NO EVENT SHALL VICTOR FRAGOSO BE LIABLE FOR ANY DIRECT,
24 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 //
31 
32 #ifndef OPTIMO_CORE_NUMERICAL_GRADIENT_H_
33 #define OPTIMO_CORE_NUMERICAL_GRADIENT_H_
34 
35 #include <Eigen/Core>
36 #include "optimo/core/objects.h"
37 #include "optimo/core/objects_ls.h"
38 
39 namespace optimo {
41 
58 template <typename Scalar, uint n>
59 class SecantGradientFunctor : public GradientFunctor<Scalar, n> {
60  public:
61  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
62 
65  const Scalar h) :
66  GradientFunctor<Scalar, n>(), objective_(obj), h_(h) { }
67 
68  // Destructor
69  virtual ~SecantGradientFunctor(void) { }
70 
72  virtual void
73  operator()(const Eigen::Matrix<Scalar, n, 1>& x,
74  Eigen::Matrix<Scalar, n, 1>* g) const {
75  Scalar den = 2*h_;
76  Eigen::Matrix<Scalar, n, 1> xl, xr;
77  for (unsigned int i = 0; i < n; i++) {
78  xl = x;
79  xr = x;
80  xr(i, 0) += h_;
81  xl(i, 0) -= h_;
82  (*g)(i, 0) = (objective_(xr) - objective_(xl))/den;
83  }
84  }
85 
86  protected:
87  const ObjectiveFunctor<Scalar, n>& objective_;
88  const Scalar h_;
89 };
91 
105 template <typename Scalar>
107  public:
110  const Scalar h) :
111  GradientFunctorLS<Scalar>(), objective_(obj), h_(h) { }
112 
113  virtual ~SecantGradientFunctorLS(void) { }
114 
116  virtual void
117  operator()(const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>& x,
118  Eigen::Matrix<Scalar, Eigen::Dynamic, 1>* g) const {
119  const int n = x.rows();
120  Scalar den = 2*h_;
121  Eigen::Matrix<Scalar, Eigen::Dynamic, 1> xl(n), xr(n);
122  for (unsigned int i = 0; i < n; i++) {
123  xl = x;
124  xr = x;
125  xr(i, 0) += h_;
126  xl(i, 0) -= h_;
127  (*g)(i, 0) = (objective_(xr) - objective_(xl))/den;
128  }
129  }
130 
131  protected:
132  const ObjectiveFunctorLS<Scalar>& objective_;
133  const Scalar h_;
134 };
135 } // optimo
136 
137 #endif // OPTIMO_CORE_NUMERICAL_GRADIENT_H_
Objective functor.
Definition: objects_ls.h:51
Objective functor.
Definition: objects.h:54
EIGEN_MAKE_ALIGNED_OPERATOR_NEW SecantGradientFunctor(const ObjectiveFunctor< Scalar, n > &obj, const Scalar h)
Constructor.
Definition: numerical_gradient.h:64
Numerical Gradient Computation using the Secant Method.
Definition: numerical_gradient.h:59
SecantGradientFunctorLS(const ObjectiveFunctorLS< Scalar > &obj, const Scalar h)
Constructor.
Definition: numerical_gradient.h:109
Gradient functor.
Definition: objects_ls.h:88
virtual void operator()(const Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &x, Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > *g) const
Compute gradient.
Definition: numerical_gradient.h:117
Numerical Gradient Computation using the Secant Method.
Definition: numerical_gradient.h:106
Gradient functor.
Definition: objects.h:88
virtual void operator()(const Eigen::Matrix< Scalar, n, 1 > &x, Eigen::Matrix< Scalar, n, 1 > *g) const
Compute gradient.
Definition: numerical_gradient.h:73