add_gradient.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_CORE_OPTIMIZERS_FUNCTION_ADD_GRADIENT_HPP
14 #define MLPACK_CORE_OPTIMIZERS_FUNCTION_ADD_GRADIENT_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 #include "traits.hpp"
18 
19 namespace mlpack {
20 namespace optimization {
21 
26 template<typename FunctionType,
27  bool HasEvaluateWithGradient =
28  traits::HasEvaluateWithGradient<FunctionType,
30  bool HasGradient = traits::HasGradient<FunctionType,
31  traits::GradientForm>::value>
33 {
34  public:
35  // Provide a dummy overload so the name 'Gradient' exists for this object.
37 };
38 
42 template<typename FunctionType, bool HasEvaluateWithGradient>
43 class AddGradient<FunctionType, HasEvaluateWithGradient, true>
44 {
45  public:
46  // Reflect the existing Gradient().
47  void Gradient(const arma::mat& coordinates, arma::mat& gradient)
48  {
49  static_cast<FunctionType*>(static_cast<Function<FunctionType>*>(
50  this))->Gradient(coordinates, gradient);
51  }
52 };
53 
58 template<typename FunctionType>
59 class AddGradient<FunctionType, true, false>
60 {
61  public:
68  void Gradient(const arma::mat& coordinates, arma::mat& gradient)
69  {
70  // The returned objective value will be ignored.
71  (void) static_cast<Function<FunctionType>*>(this)->EvaluateWithGradient(
72  coordinates, gradient);
73  }
74 };
75 
80 template<typename FunctionType,
81  bool HasEvaluateWithGradient =
82  traits::HasEvaluateWithGradient<FunctionType,
84  bool HasGradient = traits::HasGradient<FunctionType,
87 {
88  public:
89  // Provide a dummy overload so the name 'Gradient' exists for this object.
91 };
92 
96 template<typename FunctionType, bool HasEvaluateWithGradient>
97 class AddGradientConst<FunctionType, HasEvaluateWithGradient, true>
98 {
99  public:
100  // Reflect the existing Gradient().
101  void Gradient(const arma::mat& coordinates, arma::mat& gradient) const
102  {
103  static_cast<const FunctionType*>(static_cast<const
104 Function<FunctionType>*>(this))->Gradient(coordinates,
105  gradient);
106  }
107 };
108 
113 template<typename FunctionType>
114 class AddGradientConst<FunctionType, true, false>
115 {
116  public:
123  void Gradient(const arma::mat& coordinates, arma::mat& gradient) const
124  {
125  // The returned objective value will be ignored.
126  (void) static_cast<
127  const Function<FunctionType>*>(this)->EvaluateWithGradient(coordinates,
128  gradient);
129  }
130 };
131 
136 template<typename FunctionType,
137  bool HasEvaluateWithGradient =
138  traits::HasEvaluateWithGradient<FunctionType,
140  bool HasGradient = traits::HasGradient<FunctionType,
143 {
144  public:
145  // Provide a dummy overload so the name 'Gradient' exists for this object.
147 };
148 
152 template<typename FunctionType, bool HasEvaluateWithGradient>
153 class AddGradientStatic<FunctionType, HasEvaluateWithGradient, true>
154 {
155  public:
156  // Reflect the existing Gradient().
157  static void Gradient(const arma::mat& coordinates, arma::mat& gradient)
158  {
159  FunctionType::Gradient(coordinates, gradient);
160  }
161 };
162 
167 template<typename FunctionType>
168 class AddGradientStatic<FunctionType, true, false>
169 {
170  public:
177  static void Gradient(const arma::mat& coordinates, arma::mat& gradient)
178  {
179  // The returned objective value will be ignored.
180  (void) FunctionType::EvaluateWithGradient(coordinates, gradient);
181  }
182 };
183 
184 } // namespace optimization
185 } // namespace mlpack
186 
187 #endif
double(*)(const arma::mat &, arma::mat &) EvaluateWithGradientStaticForm
This is the form of a static EvaluateWithGradient() method.
Definition: traits.hpp:83
void(*)(const arma::mat &, arma::mat &) GradientStaticForm
This is the form of a static Gradient() method.
Definition: traits.hpp:68
static double EvaluateWithGradient(traits::UnconstructableType &, const size_t, const size_t)
void(FunctionType::*)(const arma::mat &, arma::mat &) const GradientConstForm
This is the form of a const Gradient() method.
Definition: traits.hpp:64
.hpp
Definition: add_to_po.hpp:21
double(FunctionType::*)(const arma::mat &, arma::mat &) const EvaluateWithGradientConstForm
This is the form of a const EvaluateWithGradient() method.
Definition: traits.hpp:78
void Gradient(const arma::mat &coordinates, arma::mat &gradient)
Calculate the gradient and store it in the given matrix.
The AddGradient mixin class will provide a const Gradient() method if the given FunctionType has Eval...
The AddGradient mixin class will provide a static Gradient() method if the given FunctionType has sta...
The core includes that mlpack expects; standard C++ includes and Armadillo.
void Gradient(traits::UnconstructableType &) const
void(FunctionType::*)(const arma::mat &, arma::mat &) GradientForm
This is the form of a non-const Gradient() method.
Definition: traits.hpp:59
static void Gradient(traits::UnconstructableType &)
void Gradient(const arma::mat &coordinates, arma::mat &gradient)
static void Gradient(const arma::mat &coordinates, arma::mat &gradient)
void Gradient(const arma::mat &coordinates, arma::mat &gradient) const
Calculate the gradient and store it in the given matrix.
void Gradient(const arma::mat &coordinates, arma::mat &gradient) const
This is a utility type used to provide unusable overloads from each of the mixin classes.
Definition: traits.hpp:255
void Gradient(traits::UnconstructableType &)
The AddGradient mixin class will provide a Gradient() method if the given FunctionType has EvaluateWi...
static void Gradient(const arma::mat &coordinates, arma::mat &gradient)
Calculate the gradient and store it in the given matrix.
double(FunctionType::*)(const arma::mat &, arma::mat &) EvaluateWithGradientForm
This is the form of a non-const EvaluateWithGradient() method.
Definition: traits.hpp:73
The Function class is a wrapper class for any FunctionType that will add any possible derived methods...
Definition: function.hpp:22