mlpack  git-master
traits.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_CORE_OPTIMIZERS_FUNCTION_TRAITS_HPP
14 #define MLPACK_CORE_OPTIMIZERS_FUNCTION_TRAITS_HPP
15 
16 #include <mlpack/prereqs.hpp>
18 
19 namespace mlpack {
20 namespace optimization {
21 namespace traits {
22 
24 HAS_EXACT_METHOD_FORM(Evaluate, HasEvaluate);
26 HAS_EXACT_METHOD_FORM(Gradient, HasGradient);
28 HAS_EXACT_METHOD_FORM(EvaluateWithGradient, HasEvaluateWithGradient);
30 HAS_EXACT_METHOD_FORM(NumFunctions, HasNumFunctions);
32 HAS_EXACT_METHOD_FORM(Shuffle, HasShuffle);
34 HAS_EXACT_METHOD_FORM(NumConstraints, HasNumConstraints);
36 HAS_EXACT_METHOD_FORM(EvaluateConstraint, HasEvaluateConstraint);
38 HAS_EXACT_METHOD_FORM(GradientConstraint, HasGradientConstraint);
40 HAS_EXACT_METHOD_FORM(NumFeatures, HasNumFeatures);
42 HAS_EXACT_METHOD_FORM(PartialGradient, HasPartialGradient);
43 
45 template<typename FunctionType>
46 using EvaluateForm = double(FunctionType::*)(const arma::mat&);
47 
49 template<typename FunctionType>
50 using EvaluateConstForm =
51  double(FunctionType::*)(const arma::mat&) const;
52 
54 template<typename FunctionType>
55 using EvaluateStaticForm = double(*)(const arma::mat&);
56 
58 template<typename FunctionType>
59 using GradientForm = void(FunctionType::*)(const arma::mat&, arma::mat&);
60 
62 template<typename FunctionType>
63 using GradientConstForm =
64  void(FunctionType::*)(const arma::mat&, arma::mat&) const;
65 
67 template<typename FunctionType>
68 using GradientStaticForm = void(*)(const arma::mat&, arma::mat&);
69 
71 template<typename FunctionType>
73  double(FunctionType::*)(const arma::mat&, arma::mat&);
74 
76 template<typename FunctionType>
78  double(FunctionType::*)(const arma::mat&, arma::mat&) const;
79 
81 template<typename FunctionType>
83  double(*)(const arma::mat&, arma::mat&);
84 
86 template <typename FunctionType>
87 using NumFunctionsForm = size_t(FunctionType::*)();
88 
90 template <typename FunctionType>
91 using NumFunctionsConstForm = size_t(FunctionType::*)() const;
92 
94 template<typename FunctionType>
95 using NumFunctionsStaticForm = size_t(*)();
96 
98 template<typename FunctionType>
99 using ShuffleForm = void(FunctionType::*)();
100 
102 template<typename FunctionType>
103 using ShuffleConstForm = void(FunctionType::*)() const;
104 
106 template<typename FunctionType>
107 using ShuffleStaticForm = void(*)();
108 
110 template<typename FunctionType>
111 using DecomposableEvaluateForm = double(FunctionType::*)(
112  const arma::mat&, const size_t, const size_t);
113 
115 template<typename FunctionType>
116 using DecomposableEvaluateConstForm = double(FunctionType::*)(
117  const arma::mat&, const size_t, const size_t) const;
118 
120 template<typename FunctionType>
121 using DecomposableEvaluateStaticForm = double(*)(
122  const arma::mat&, const size_t, const size_t);
123 
125 template<typename FunctionType>
126 using DecomposableGradientForm = void(FunctionType::*)(
127  const arma::mat&, const size_t, arma::mat&, const size_t);
128 
130 template<typename FunctionType>
131 using DecomposableGradientConstForm = void(FunctionType::*)(
132  const arma::mat&, const size_t, arma::mat&, const size_t) const;
133 
135 template<typename FunctionType>
136 using DecomposableGradientStaticForm = void(*)(
137  const arma::mat&, const size_t, arma::mat&, const size_t);
138 
140 template<typename FunctionType>
141 using DecomposableEvaluateWithGradientForm = double(FunctionType::*)(
142  const arma::mat&, const size_t, arma::mat&, const size_t);
143 
145 template<typename FunctionType>
146 using DecomposableEvaluateWithGradientConstForm = double(FunctionType::*)(
147  const arma::mat&, const size_t, arma::mat&, const size_t) const;
148 
150 template<typename FunctionType>
152  const arma::mat&, const size_t, arma::mat&, const size_t);
153 
155 template<typename FunctionType>
156 using NumConstraintsForm = size_t(FunctionType::*)();
157 
159 template<typename FunctionType>
160 using NumConstraintsConstForm = size_t(FunctionType::*)() const;
161 
163 template<typename FunctionType>
164 using NumConstraintsStaticForm = size_t(*)();
165 
167 template <typename FunctionType>
168 using EvaluateConstraintForm = double(FunctionType::*)(
169  const size_t, const arma::mat&);
170 
172 template<typename FunctionType>
173 using EvaluateConstraintConstForm = double(FunctionType::*)(
174  const size_t, const arma::mat&) const;
175 
177 template<typename FunctionType>
178 using EvaluateConstraintStaticForm = double(*)(const size_t, const arma::mat&);
179 
181 template <typename FunctionType>
182 using GradientConstraintForm = void(FunctionType::*)(
183  const size_t, const arma::mat&, arma::mat&);
184 
186 template<typename FunctionType>
187 using GradientConstraintConstForm = void(FunctionType::*)(
188  const size_t, const arma::mat&, arma::mat&) const;
189 
191 template<typename Class, typename... Ts>
192 using GradientConstraintStaticForm = void(*)(
193  const size_t, const arma::mat&, arma::mat&);
194 
196 template<typename FunctionType>
197 using SparseGradientForm = void(FunctionType::*)(
198  const arma::mat&, const size_t, arma::sp_mat&, const size_t);
199 
201 template<typename FunctionType>
202 using SparseGradientConstForm = void(FunctionType::*)(
203  const arma::mat&, const size_t, arma::sp_mat&, const size_t) const;
204 
206 template<typename FunctionType>
207 using SparseGradientStaticForm = void(*)(
208  const arma::mat&, const size_t, arma::sp_mat&, const size_t);
209 
211 template<typename FunctionType>
212 using NumFeaturesForm = size_t(FunctionType::*)();
213 
215 template<typename FunctionType>
216 using NumFeaturesConstForm = size_t(FunctionType::*)() const;
217 
219 template<typename FunctionType>
220 using NumFeaturesStaticForm = size_t(*)();
221 
223 template<typename FunctionType>
224 using PartialGradientForm = void(FunctionType::*)(
225  const arma::mat&, const size_t, arma::sp_mat&);
226 
228 template<typename FunctionType>
229 using PartialGradientConstForm = void(FunctionType::*)(
230  const arma::mat&, const size_t, arma::sp_mat&) const;
231 
233 template<typename FunctionType>
234 using PartialGradientStaticForm = void(*)(
235  const arma::mat&, const size_t, arma::sp_mat&);
236 
238 template<typename FunctionType, typename... Ts>
239 using OtherForm = double(FunctionType::*)(Ts...);
240 
242 template<typename FunctionType, typename... Ts>
243 using OtherConstForm = double(FunctionType::*)(Ts...) const;
244 
246 template<typename FunctionType, typename... Ts>
247 using OtherStaticForm = double(*)(Ts...);
248 
256 {
257  private:
258  UnconstructableType() { }
259 };
260 
271 template<typename ClassType,
272  template<typename, template<typename...> class, size_t> class CheckerA,
273  template<typename...> class SignatureA,
274  template<typename...> class ConstSignatureA,
275  template<typename...> class StaticSignatureA,
276  template<typename, template<typename...> class, size_t> class CheckerB,
277  template<typename...> class SignatureB,
278  template<typename...> class ConstSignatureB,
279  template<typename...> class StaticSignatureB>
281 {
282  // Check if any const or static version of method A exists.
283  const static bool HasAnyFormA =
284  CheckerA<ClassType, SignatureA, 0>::value ||
285  CheckerA<ClassType, ConstSignatureA, 0>::value ||
286  CheckerA<ClassType, StaticSignatureA, 0>::value;
287  // Check if any const or static versino of method B exists.
288  const static bool HasAnyFormB =
289  CheckerB<ClassType, SignatureB, 0>::value ||
290  CheckerB<ClassType, ConstSignatureB, 0>::value ||
291  CheckerB<ClassType, StaticSignatureB, 0>::value;
292 
293  // Make sure at least one const version exists.
294  const static bool HasEitherNonConstForm =
295  CheckerA<ClassType, SignatureA, 0>::value ||
296  CheckerB<ClassType, SignatureB, 0>::value;
297 
298  const static bool value = HasEitherNonConstForm && HasAnyFormA && HasAnyFormB;
299 };
300 
311 template<typename ClassType,
312  template<typename, template<typename...> class, size_t> class CheckerA,
313  template<typename...> class ConstSignatureA,
314  template<typename...> class StaticSignatureA,
315  template<typename, template<typename...> class, size_t> class CheckerB,
316  template<typename...> class ConstSignatureB,
317  template<typename...> class StaticSignatureB>
319 {
320  // Check if any const or static version of method A exists.
321  const static bool HasAnyFormA =
322  CheckerA<ClassType, ConstSignatureA, 0>::value ||
323  CheckerA<ClassType, StaticSignatureA, 0>::value;
324  // Check if any const or static version of method B exists.
325  const static bool HasAnyFormB =
326  CheckerB<ClassType, ConstSignatureB, 0>::value ||
327  CheckerB<ClassType, StaticSignatureB, 0>::value;
328 
329  // Make sure at least one const version exists.
330  const static bool HasEitherConstForm =
331  CheckerA<ClassType, ConstSignatureA, 0>::value ||
332  CheckerB<ClassType, ConstSignatureB, 0>::value;
333 
334  const static bool value = HasEitherConstForm && HasAnyFormA && HasAnyFormB;
335 };
336 
337 } // namespace traits
338 } // namespace optimization
339 } // namespace mlpack
340 
341 #endif
double(*)(const arma::mat &) EvaluateStaticForm
This is the form of a static Evaluate() method.
Definition: traits.hpp:55
double(*)(const arma::mat &, arma::mat &) EvaluateWithGradientStaticForm
This is the form of a static EvaluateWithGradient() method.
Definition: traits.hpp:83
void(FunctionType::*)(const arma::mat &, const size_t, arma::sp_mat &, const size_t) SparseGradientForm
This is the form of a non-const sparse Gradient() method.
Definition: traits.hpp:198
void(*)() ShuffleStaticForm
This is the form of a static Shuffle() method.
Definition: traits.hpp:107
void(*)(const arma::mat &, arma::mat &) GradientStaticForm
This is the form of a static Gradient() method.
Definition: traits.hpp:68
double(FunctionType::*)(const arma::mat &) const EvaluateConstForm
This is the form of a const Evaluate() method.
Definition: traits.hpp:51
size_t(*)() NumFeaturesStaticForm
This is the form of a static NumFeatures() method.
Definition: traits.hpp:220
size_t(FunctionType::*)() const NumConstraintsConstForm
This is the form of a const NumConstraints() method.
Definition: traits.hpp:160
void(FunctionType::*)(const arma::mat &, arma::mat &) const GradientConstForm
This is the form of a const Gradient() method.
Definition: traits.hpp:64
void(*)(const arma::mat &, const size_t, arma::sp_mat &, const size_t) SparseGradientStaticForm
This is the form of a static sparse Gradient() method.
Definition: traits.hpp:208
.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(FunctionType::*)(const size_t, const arma::mat &, arma::mat &) GradientConstraintForm
This is the form of a non-const GradientConstraint() method.
Definition: traits.hpp:183
double(FunctionType::*)(Ts...) const OtherConstForm
This is a utility struct that will match any const form.
Definition: traits.hpp:243
void(FunctionType::*)(const arma::mat &, const size_t, arma::sp_mat &) PartialGradientForm
This is the form of a non-const PartialGradient() method.
Definition: traits.hpp:225
The core includes that mlpack expects; standard C++ includes and Armadillo.
void(FunctionType::*)(const arma::mat &, const size_t, arma::sp_mat &, const size_t) const SparseGradientConstForm
This is the form of a const sparse Gradient() method.
Definition: traits.hpp:203
double(FunctionType::*)(const arma::mat &) EvaluateForm
This is the form of a non-const Evaluate() method.
Definition: traits.hpp:46
size_t(FunctionType::*)() const NumFunctionsConstForm
This is the form of a const NumFunctions() method.
Definition: traits.hpp:91
double(FunctionType::*)(const size_t, const arma::mat &) EvaluateConstraintForm
This is the form of a non-const EvaluateConstraint() method.
Definition: traits.hpp:169
void(*)(const arma::mat &, const size_t, arma::sp_mat &) PartialGradientStaticForm
This is the form of a static PartialGradient() method.
Definition: traits.hpp:235
double(FunctionType::*)(const arma::mat &, const size_t, const size_t) const DecomposableEvaluateConstForm
This is the form of a decomposable const Evaluate() method.
Definition: traits.hpp:117
void(FunctionType::*)(const arma::mat &, arma::mat &) GradientForm
This is the form of a non-const Gradient() method.
Definition: traits.hpp:59
size_t(FunctionType::*)() NumFeaturesForm
This is the form of a non-const NumFeatures() method.
Definition: traits.hpp:212
double(*)(const arma::mat &, const size_t, arma::mat &, const size_t) DecomposableEvaluateWithGradientStaticForm
This is the form of a decomposable static EvaluateWithGradient() method.
Definition: traits.hpp:152
double(FunctionType::*)(const arma::mat &, const size_t, arma::mat &, const size_t) const DecomposableEvaluateWithGradientConstForm
This is the form of a decomposable const EvaluateWithGradient() method.
Definition: traits.hpp:147
HAS_EXACT_METHOD_FORM(Evaluate, HasEvaluate)
Detect an Evaluate() method.
size_t(*)() NumFunctionsStaticForm
This is the form of a static NumFunctions() method.
Definition: traits.hpp:95
void(FunctionType::*)() const ShuffleConstForm
This is the form of a const Shuffle() method.
Definition: traits.hpp:103
double(*)(const size_t, const arma::mat &) EvaluateConstraintStaticForm
This is the form of a static EvaluateConstraint() method.
Definition: traits.hpp:178
void(FunctionType::*)(const arma::mat &, const size_t, arma::mat &, const size_t) DecomposableGradientForm
This is the form of a decomposable non-const Gradient() method.
Definition: traits.hpp:127
This is a utility type used to provide unusable overloads from each of the mixin classes.
Definition: traits.hpp:255
void(FunctionType::*)(const arma::mat &, const size_t, arma::sp_mat &) const PartialGradientConstForm
This is the form of a const PartialGradient() method.
Definition: traits.hpp:230
double(*)(Ts...) OtherStaticForm
This is a utility struct that will match any static form.
Definition: traits.hpp:247
size_t(FunctionType::*)() NumConstraintsForm
This is the form of a non-const NumConstraints() method.
Definition: traits.hpp:156
void(FunctionType::*)(const arma::mat &, const size_t, arma::mat &, const size_t) const DecomposableGradientConstForm
This the form of a decomposable const Gradient() method.
Definition: traits.hpp:132
void(FunctionType::*)() ShuffleForm
This is the form of a non-const Shuffle() method.
Definition: traits.hpp:99
size_t(FunctionType::*)() NumFunctionsForm
This is the form of a non-const NumFunctions() method.
Definition: traits.hpp:87
void(*)(const size_t, const arma::mat &, arma::mat &) GradientConstraintStaticForm
This is the form of a static GradientConstraint() method.
Definition: traits.hpp:193
double(FunctionType::*)(const arma::mat &, arma::mat &) EvaluateWithGradientForm
This is the form of a non-const EvaluateWithGradient() method.
Definition: traits.hpp:73
double(FunctionType::*)(const arma::mat &, const size_t, arma::mat &, const size_t) DecomposableEvaluateWithGradientForm
This is the form of a decomposable non-const EvaluateWithGradient() method.
Definition: traits.hpp:142
Utility struct: sometimes we want to know if we have two functions available, and that at least one o...
Definition: traits.hpp:318
Utility struct: sometimes we want to know if we have two functions available, and that at least one o...
Definition: traits.hpp:280
size_t(*)() NumConstraintsStaticForm
This is the form of a static NumConstraints() method.
Definition: traits.hpp:164
double(*)(const arma::mat &, const size_t, const size_t) DecomposableEvaluateStaticForm
This is the form of a decomposable static Evaluate() method.
Definition: traits.hpp:122
void(FunctionType::*)(const size_t, const arma::mat &, arma::mat &) const GradientConstraintConstForm
This is the form of a const GradientConstraint() method.
Definition: traits.hpp:188
double(FunctionType::*)(const arma::mat &, const size_t, const size_t) DecomposableEvaluateForm
This is the form of a decomposable Evaluate() method.
Definition: traits.hpp:112
double(FunctionType::*)(Ts...) OtherForm
This is a utility struct that will match any non-const form.
Definition: traits.hpp:239
size_t(FunctionType::*)() const NumFeaturesConstForm
This is the form of a const NumFeatures() method.
Definition: traits.hpp:216
double(FunctionType::*)(const size_t, const arma::mat &) const EvaluateConstraintConstForm
This is the form of a const EvaluateConstraint() method.
Definition: traits.hpp:174
void(*)(const arma::mat &, const size_t, arma::mat &, const size_t) DecomposableGradientStaticForm
This is the form of a decomposable static Gradient() method.
Definition: traits.hpp:137