param.hpp
Go to the documentation of this file.
1 
15 #ifndef MLPACK_CORE_UTIL_PARAM_HPP
16 #define MLPACK_CORE_UTIL_PARAM_HPP
17 
18 // Required forward declarations.
19 namespace mlpack {
20 namespace data {
21 
22 class IncrementPolicy;
23 
24 template<typename PolicyType, typename InputType>
25 class DatasetMapper;
26 
27 using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
28 
29 } // namespace data
30 } // namespace mlpack
31 
37 // These are ugly, but necessary utility functions we must use to generate a
38 // unique identifier inside of the PARAM() module.
39 #define JOIN(x, y) JOIN_AGAIN(x, y)
40 #define JOIN_AGAIN(x, y) x ## y
41 #define STRINGIFY(x) STRINGIFY_AGAIN(x)
42 #define STRINGIFY_AGAIN(x) #x
43 
50 #define BINDING_FUNCTION(...) BINDING_NAME(__VA_ARGS__)
51 
65 #ifdef __COUNTER__
66  #define BINDING_USER_NAME(NAME) static \
67  mlpack::util::BindingName \
68  JOIN(io_bindingusername_dummy_object, __COUNTER__) = \
69  mlpack::util::BindingName( \
70  STRINGIFY(BINDING_NAME), NAME);
71 #else
72  #define BINDING_USER_NAME(NAME) static \
73  mlpack::util::BindingName \
74  JOIN(JOIN(io_bindingusername_dummy_object, __LINE__), opt) = \
75  mlpack::util::BindingName( \
76  STRINGIFY(BINDING_NAME), NAME);
77 #endif
78 
94 #ifdef __COUNTER__
95  #define BINDING_SHORT_DESC(SHORT_DESC) static \
96  mlpack::util::ShortDescription \
97  JOIN(io_programshort_desc_dummy_object, __COUNTER__) = \
98  mlpack::util::ShortDescription( \
99  STRINGIFY(BINDING_NAME), SHORT_DESC);
100 #else
101  #define BINDING_SHORT_DESC(SHORT_DESC) static \
102  mlpack::util::ShortDescription \
103  JOIN(JOIN(io_programshort_desc_dummy_object, __LINE__), opt) = \
104  mlpack::util::ShortDescription( \
105  STRINGIFY(BINDING_NAME), SHORT_DESC);
106 #endif
107 
128 #ifdef __COUNTER__
129  #define BINDING_LONG_DESC(LONG_DESC) static \
130  mlpack::util::LongDescription \
131  JOIN(io_programlong_desc_dummy_object, __COUNTER__) = \
132  mlpack::util::LongDescription( \
133  STRINGIFY(BINDING_NAME), []() { return std::string(LONG_DESC); });
134 #else
135  #define BINDING_LONG_DESC(LONG_DESC) static \
136  mlpack::util::LongDescription \
137  JOIN(JOIN(io_programlong_desc_dummy_object, __LINE__), opt) = \
138  mlpack::util::LongDescription( \
139  STRINGIFY(BINDING_NAME), []() { return std::string(LONG_DESC); });
140 #endif
141 
159 #ifdef __COUNTER__
160  #define BINDING_EXAMPLE(EXAMPLE) static \
161  mlpack::util::Example \
162  JOIN(io_programexample_dummy_object_, __COUNTER__) = \
163  mlpack::util::Example( \
164  STRINGIFY(BINDING_NAME), []() { return(std::string(EXAMPLE)); });
165 #else
166  #define BINDING_EXAMPLE(EXAMPLE) static \
167  mlpack::util::Example \
168  JOIN(JOIN(io_programexample_dummy_object_, __LINE__), opt) = \
169  mlpack::util::Example( \
170  STRINGIFY(BINDING_NAME), []() { return(std::string(EXAMPLE)); });
171 #endif
172 
196 #ifdef __COUNTER__
197  #define BINDING_SEE_ALSO(DESCRIPTION, LINK) static \
198  mlpack::util::SeeAlso \
199  JOIN(io_programsee_also_dummy_object_, __COUNTER__) = \
200  mlpack::util::SeeAlso(STRINGIFY(BINDING_NAME), DESCRIPTION, LINK);
201 #else
202  #define BINDING_SEE_ALSO(DESCRIPTION, LINK) static \
203  mlpack::util::SeeAlso \
204  JOIN(JOIN(io_programsee_also_dummy_object_, __LINE__), opt) = \
205  mlpack::util::SeeAlso(STRINGIFY(BINDING_NAME), DESCRIPTION, LINK);
206 #endif
207 
228 #define PARAM_FLAG(ID, DESC, ALIAS) \
229  PARAM_IN(bool, ID, DESC, ALIAS, false, false);
230 
256 #define PARAM_INT_IN(ID, DESC, ALIAS, DEF) \
257  PARAM_IN(int, ID, DESC, ALIAS, DEF, false)
258 
287 #define PARAM_INT_OUT(ID, DESC) \
288  PARAM_OUT(int, ID, DESC, "", 0, false)
289 
314 #define PARAM_DOUBLE_IN(ID, DESC, ALIAS, DEF) \
315  PARAM_IN(double, ID, DESC, ALIAS, DEF, false)
316 
345 #define PARAM_DOUBLE_OUT(ID, DESC) \
346  PARAM_OUT(double, ID, DESC, "", 0.0, false)
347 
374 #define PARAM_STRING_IN(ID, DESC, ALIAS, DEF) \
375  PARAM_IN(std::string, ID, DESC, ALIAS, DEF, false)
376 
406 #define PARAM_STRING_OUT(ID, DESC, ALIAS) \
407  PARAM_OUT(std::string, ID, DESC, ALIAS, "", false)
408 
434 #define PARAM_MATRIX_IN(ID, DESC, ALIAS) \
435  PARAM_MATRIX(ID, DESC, ALIAS, false, true, true)
436 
462 #define PARAM_MATRIX_IN_REQ(ID, DESC, ALIAS) \
463  PARAM_MATRIX(ID, DESC, ALIAS, true, true, true)
464 
495 #define PARAM_MATRIX_OUT(ID, DESC, ALIAS) \
496  PARAM_MATRIX(ID, DESC, ALIAS, false, true, false)
497 
524 #define PARAM_TMATRIX_IN(ID, DESC, ALIAS) \
525  PARAM_MATRIX(ID, DESC, ALIAS, false, false, true)
526 
554 #define PARAM_TMATRIX_IN_REQ(ID, DESC, ALIAS) \
555  PARAM_MATRIX(ID, DESC, ALIAS, true, false, true)
556 
589 #define PARAM_TMATRIX_OUT(ID, DESC, ALIAS) \
590  PARAM_MATRIX(ID, DESC, ALIAS, false, false, false)
591 
617 #define PARAM_UMATRIX_IN(ID, DESC, ALIAS) \
618  PARAM_UMATRIX(ID, DESC, ALIAS, false, true, true)
619 
646 #define PARAM_UMATRIX_IN_REQ(ID, DESC, ALIAS) \
647  PARAM_UMATRIX(ID, DESC, ALIAS, true, true, true)
648 
680 #define PARAM_UMATRIX_OUT(ID, DESC, ALIAS) \
681  PARAM_UMATRIX(ID, DESC, ALIAS, false, true, false)
682 
683 
709 #define PARAM_COL_IN(ID, DESC, ALIAS) \
710  PARAM_COL(ID, DESC, ALIAS, false, true, true)
711 
737 #define PARAM_COL_IN_REQ(ID, DESC, ALIAS) \
738  PARAM_COL(ID, DESC, ALIAS, true, true, true)
739 
765 #define PARAM_ROW_IN(ID, DESC, ALIAS) \
766  PARAM_ROW(ID, DESC, ALIAS, false, true, true)
767 
793 #define PARAM_UCOL_IN(ID, DESC, ALIAS) \
794  PARAM_UCOL(ID, DESC, ALIAS, false, true, true)
795 
822 #define PARAM_UROW_IN(ID, DESC, ALIAS) \
823  PARAM_UROW(ID, DESC, ALIAS, false, true, true)
824 
855 #define PARAM_COL_OUT(ID, DESC, ALIAS) \
856  PARAM_COL(ID, DESC, ALIAS, false, true, false)
857 
888 #define PARAM_ROW_OUT(ID, DESC, ALIAS) \
889  PARAM_ROW(ID, DESC, ALIAS, false, true, false)
890 
921 #define PARAM_UCOL_OUT(ID, DESC, ALIAS) \
922  PARAM_UCOL(ID, DESC, ALIAS, false, true, false)
923 
954 #define PARAM_UROW_OUT(ID, DESC, ALIAS) \
955  PARAM_UROW(ID, DESC, ALIAS, false, true, false)
956 
981 #define PARAM_VECTOR_IN(T, ID, DESC, ALIAS) \
982  PARAM_IN(std::vector<T>, ID, DESC, ALIAS, std::vector<T>(), false)
983 
1015 #define PARAM_VECTOR_OUT(T, ID, DESC, ALIAS) \
1016  PARAM_OUT(std::vector<T>, ID, DESC, ALIAS, std::vector<T>(), false)
1017 
1055 #define TUPLE_TYPE std::tuple<mlpack::data::DatasetInfo, arma::mat>
1056 #define PARAM_MATRIX_AND_INFO_IN(ID, DESC, ALIAS) \
1057  PARAM(TUPLE_TYPE, ID, DESC, ALIAS, \
1058  "std::tuple<mlpack::data::DatasetInfo, arma::mat>", false, true, true, \
1059  TUPLE_TYPE())
1060 
1089 #define PARAM_MODEL_IN(TYPE, ID, DESC, ALIAS) \
1090  PARAM_MODEL(TYPE, ID, DESC, ALIAS, false, true)
1091 
1120 #define PARAM_MODEL_IN_REQ(TYPE, ID, DESC, ALIAS) \
1121  PARAM_MODEL(TYPE, ID, DESC, ALIAS, true, true)
1122 
1145 #define PARAM_MODEL_OUT(TYPE, ID, DESC, ALIAS) \
1146  PARAM_MODEL(TYPE, ID, DESC, ALIAS, false, false)
1147 
1170 #define PARAM_INT_IN_REQ(ID, DESC, ALIAS) \
1171  PARAM_IN(int, ID, DESC, ALIAS, 0, true)
1172 
1195 #define PARAM_DOUBLE_IN_REQ(ID, DESC, ALIAS) \
1196  PARAM_IN(double, ID, DESC, ALIAS, 0.0, true)
1197 
1220 #define PARAM_STRING_IN_REQ(ID, DESC, ALIAS) \
1221  PARAM_IN(std::string, ID, DESC, ALIAS, "", true)
1222 
1247 #define PARAM_VECTOR_IN_REQ(T, ID, DESC, ALIAS) \
1248  PARAM_IN(std::vector<T>, ID, DESC, ALIAS, std::vector<T>(), true);
1249 
1253 #define PARAM_IN(T, ID, DESC, ALIAS, DEF, REQ) \
1254  PARAM(T, ID, DESC, ALIAS, #T, REQ, true, false, DEF);
1255 
1256 #define PARAM_OUT(T, ID, DESC, ALIAS, DEF, REQ) \
1257  PARAM(T, ID, DESC, ALIAS, #T, REQ, false, false, DEF);
1258 
1259 #define PARAM_MATRIX(ID, DESC, ALIAS, REQ, TRANS, IN) \
1260  PARAM(arma::mat, ID, DESC, ALIAS, "arma::mat", REQ, IN, \
1261  TRANS, arma::mat());
1262 
1263 #define PARAM_UMATRIX(ID, DESC, ALIAS, REQ, TRANS, IN) \
1264  PARAM(arma::Mat<size_t>, ID, DESC, ALIAS, "arma::Mat<size_t>", \
1265  REQ, IN, TRANS, arma::Mat<size_t>());
1266 
1267 #define PARAM_COL(ID, DESC, ALIAS, REQ, TRANS, IN) \
1268  PARAM(arma::vec, ID, DESC, ALIAS, "arma::vec", REQ, IN, TRANS, \
1269  arma::vec());
1270 
1271 #define PARAM_UCOL(ID, DESC, ALIAS, REQ, TRANS, IN) \
1272  PARAM(arma::Col<size_t>, ID, DESC, ALIAS, "arma::Col<size_t>", \
1273  REQ, IN, TRANS, arma::Col<size_t>());
1274 
1275 #define PARAM_ROW(ID, DESC, ALIAS, REQ, TRANS, IN) \
1276  PARAM(arma::rowvec, ID, DESC, ALIAS, "arma::rowvec", REQ, IN, \
1277  TRANS, arma::rowvec());
1278 
1279 #define PARAM_UROW(ID, DESC, ALIAS, REQ, TRANS, IN) \
1280  PARAM(arma::Row<size_t>, ID, DESC, ALIAS, "arma::Row<size_t>", \
1281  REQ, IN, TRANS, arma::Row<size_t>());
1282 
1299 #ifdef __COUNTER__
1300  #define PARAM(T, ID, DESC, ALIAS, NAME, REQ, IN, TRANS, DEF) \
1301  static mlpack::util::Option<T> \
1302  JOIN(io_option_dummy_object_in_, __COUNTER__) \
1303  (DEF, ID, DESC, ALIAS, NAME, REQ, IN, !TRANS, STRINGIFY(BINDING_NAME));
1304 
1305  #define PARAM_GLOBAL(T, ID, DESC, ALIAS, NAME, REQ, IN, TRANS, DEF) \
1306  static mlpack::util::Option<T> \
1307  JOIN(io_option_global_dummy_object_in_, __COUNTER__) \
1308  (DEF, ID, DESC, ALIAS, NAME, REQ, IN, !TRANS, "");
1309 
1310  // There are no uses of required models, so that is not an option to this
1311  // macro (it would be easy to add).
1312  #define PARAM_MODEL(TYPE, ID, DESC, ALIAS, REQ, IN) \
1313  static mlpack::util::Option<TYPE*> \
1314  JOIN(io_option_dummy_model_, __COUNTER__) \
1315  (nullptr, ID, DESC, ALIAS, #TYPE, REQ, IN, false, \
1316  STRINGIFY(BINDING_NAME));
1317 #else
1318  // We have to do some really bizarre stuff since __COUNTER__ isn't defined. I
1319  // don't think we can absolutely guarantee success, but it should be "good
1320  // enough". We use the __LINE__ macro and the type of the parameter to try
1321  // and get a good guess at something unique.
1322  #define PARAM(T, ID, DESC, ALIAS, NAME, REQ, IN, TRANS, DEF) \
1323  static mlpack::util::Option<T> \
1324  JOIN(JOIN(io_option_dummy_object_in_, __LINE__), opt) \
1325  (DEF, ID, DESC, ALIAS, NAME, REQ, IN, !TRANS, STRINGIFY(BINDING_NAME));
1326 
1327  #define PARAM_GLOBAL(T, ID, DESC, ALIAS, NAME, REQ, IN, TRANS, DEF) \
1328  static mlpack::util::Option<T> \
1329  JOIN(JOIN(io_option_global_dummy_object_in_, __LINE__), opt) \
1330  (DEF, ID, DESC, ALIAS, NAME, REQ, IN, !TRANS, "");
1331 
1332  #define PARAM_MODEL(TYPE, ID, DESC, ALIAS, REQ, IN) \
1333  static mlpack::util::Option<TYPE*> \
1334  JOIN(JOIN(io_option_dummy_object_model_, __LINE__), opt) \
1335  (nullptr, ID, DESC, ALIAS, #TYPE, REQ, IN, false, \
1336  STRINGIFY(BINDING_NAME));
1337 #endif
1338 
1339 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
DatasetMapper< data::IncrementPolicy > DatasetInfo