table_helper.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // Copyright John Maddock 2015.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef TABLE_HELPER_HPP
  6. #define TABLE_HELPER_HPP
  7. #include <vector>
  8. #include <string>
  9. #include <boost/version.hpp>
  10. #include <boost/lexical_cast.hpp>
  11. //
  12. // Also include headers for whatever else we may be testing:
  13. //
  14. #ifdef TEST_LIBSTDCXX
  15. #include <tr1/cmath>
  16. #include <stdexcept>
  17. #endif
  18. #ifdef TEST_GSL
  19. #include <gsl/gsl_sf.h>
  20. #include <gsl/gsl_errno.h>
  21. #include <gsl/gsl_version.h>
  22. void gsl_handler(const char * reason, const char * file, int line, int gsl_errno)
  23. {
  24. if(gsl_errno == GSL_ERANGE) return; // handle zero or infinity in our test code.
  25. #ifdef DISTRIBUTIONS_TEST
  26. return;
  27. #else
  28. throw std::domain_error(reason);
  29. #endif
  30. }
  31. struct gsl_error_handler_setter
  32. {
  33. gsl_error_handler_t * old_handler;
  34. gsl_error_handler_setter()
  35. {
  36. old_handler = gsl_set_error_handler(gsl_handler);
  37. }
  38. ~gsl_error_handler_setter()
  39. {
  40. gsl_set_error_handler(old_handler);
  41. }
  42. };
  43. static const gsl_error_handler_setter handler;
  44. #endif
  45. #ifdef TEST_RMATH
  46. // Rmath overloads ftrunc, leading to strange errors from GCC unless we include this:
  47. #include <boost/math/special_functions.hpp>
  48. #define MATHLIB_STANDALONE
  49. #include <Rmath.h>
  50. #endif
  51. #ifdef TEST_DCDFLIB
  52. extern "C" {
  53. extern void cdfbet(int*, double*, double*, double*, double*, double*, double*, int*, double*);
  54. extern void cdfbin(int*, double*, double*, double*, double*, double*, double*, int*, double*);
  55. extern void cdfchi(int*, double*, double*, double*, double*, int*, double*);
  56. extern void cdfchn(int*, double*, double*, double*, double*, double*, int*, double*);
  57. extern void cdff(int*, double*, double*, double*, double*, double*, int*, double*);
  58. extern void cdffnc(int*, double*, double*, double*, double*, double*, double*, int*s, double*);
  59. extern void cdfgam(int*, double*, double*, double*, double*, double*, int*, double*);
  60. extern void cdfnbn(int*, double*, double*, double*, double*, double*, double*, int*, double*);
  61. extern void cdfnor(int*, double*, double*, double*, double*, double*, int*, double*);
  62. extern void cdfpoi(int*, double*, double*, double*, double*, int*, double*);
  63. extern void cdft(int*, double*, double*, double*, double*, int*, double*);
  64. extern void cdftnc(int*, double*, double*, double*, double*, double*, int*, double*);
  65. }
  66. inline double dcdflib_beta_cdf(double x, double a, double b)
  67. {
  68. int what = 1;
  69. int status = 0;
  70. double p, q, bound, y(1-x);
  71. cdfbet(&what, &p, &q, &x, &y, &a, &b, &status, &bound);
  72. return p;
  73. }
  74. inline double dcdflib_beta_quantile(double p, double a, double b)
  75. {
  76. int what = 2;
  77. int status = 0;
  78. double x, y, bound, q(1 - p);
  79. cdfbet(&what, &p, &q, &x, &y, &a, &b, &status, &bound);
  80. return x;
  81. }
  82. inline double dcdflib_binomial_cdf(double x, double s, double sf)
  83. {
  84. int what = 1;
  85. int status = 0;
  86. double p, q, bound, sfc(1-sf);
  87. cdfbin(&what, &p, &q, &x, &s, &sf, &sfc, &status, &bound);
  88. return p;
  89. }
  90. inline double dcdflib_binomial_quantile(double p, double s, double sf)
  91. {
  92. int what = 2;
  93. int status = 0;
  94. double x, bound, q(1 - p), sfc(1-sf);
  95. cdfbin(&what, &p, &q, &x, &s, &sf, &sfc, &status, &bound);
  96. return x;
  97. }
  98. inline double dcdflib_chi_cdf(double x, double df)
  99. {
  100. int what = 1;
  101. int status = 0;
  102. double p, q, bound;
  103. cdfchi(&what, &p, &q, &x, &df, &status, &bound);
  104. return p;
  105. }
  106. inline double dcdflib_chi_quantile(double p, double df)
  107. {
  108. int what = 2;
  109. int status = 0;
  110. double x, bound, q(1 - p);
  111. cdfchi(&what, &p, &q, &x, &df, &status, &bound);
  112. return x;
  113. }
  114. inline double dcdflib_chi_n_cdf(double x, double df, double nc)
  115. {
  116. int what = 1;
  117. int status = 0;
  118. double p, q, bound;
  119. cdfchn(&what, &p, &q, &x, &df, &nc, &status, &bound);
  120. return p;
  121. }
  122. inline double dcdflib_chi_n_quantile(double p, double df, double nc)
  123. {
  124. int what = 2;
  125. int status = 0;
  126. double x, bound, q(1 - p);
  127. cdfchn(&what, &p, &q, &x, &df, &nc, &status, &bound);
  128. return x;
  129. }
  130. inline double dcdflib_f_cdf(double x, double df1, double df2)
  131. {
  132. int what = 1;
  133. int status = 0;
  134. double p, q, bound;
  135. cdff(&what, &p, &q, &x, &df1, &df2, &status, &bound);
  136. return p;
  137. }
  138. inline double dcdflib_f_quantile(double p, double df1, double df2)
  139. {
  140. int what = 2;
  141. int status = 0;
  142. double x, bound, q(1 - p);
  143. cdff(&what, &p, &q, &x, &df1, &df2, &status, &bound);
  144. return x;
  145. }
  146. inline double dcdflib_f_n_cdf(double x, double df1, double df2, double nc)
  147. {
  148. int what = 1;
  149. int status = 0;
  150. double p, q, bound;
  151. cdffnc(&what, &p, &q, &x, &df1, &df2, &nc, &status, &bound);
  152. return p;
  153. }
  154. inline double dcdflib_f_n_quantile(double p, double df1, double df2, double nc)
  155. {
  156. int what = 2;
  157. int status = 0;
  158. double x, bound, q(1 - p);
  159. cdffnc(&what, &p, &q, &x, &df1, &df2, &nc, &status, &bound);
  160. return x;
  161. }
  162. inline double dcdflib_gamma_cdf(double x, double shape, double scale)
  163. {
  164. int what = 1;
  165. int status = 0;
  166. double p, q, bound;
  167. scale = 1 / scale;
  168. cdfgam(&what, &p, &q, &x, &shape, &scale, &status, &bound);
  169. return p;
  170. }
  171. inline double dcdflib_gamma_quantile(double p, double shape, double scale)
  172. {
  173. int what = 2;
  174. int status = 0;
  175. double x, bound, q(1 - p);
  176. scale = 1 / scale;
  177. cdfgam(&what, &p, &q, &x, &shape, &scale, &status, &bound);
  178. return x;
  179. }
  180. inline double dcdflib_nbin_cdf(double x, double r, double sf)
  181. {
  182. int what = 1;
  183. int status = 0;
  184. double p, q, bound, sfc(1 - sf);
  185. cdfnbn(&what, &p, &q, &x, &r, &sf, &sfc, &status, &bound);
  186. return p;
  187. }
  188. inline double dcdflib_nbin_quantile(double p, double r, double sf)
  189. {
  190. int what = 2;
  191. int status = 0;
  192. double x, bound, q(1 - p), sfc(1 - sf);
  193. cdfnbn(&what, &p, &q, &x, &r, &sf, &sfc, &status, &bound);
  194. return x;
  195. }
  196. inline double dcdflib_norm_cdf(double x, double mean, double sd)
  197. {
  198. int what = 1;
  199. int status = 0;
  200. double p, q, bound;
  201. cdfnor(&what, &p, &q, &x, &mean, &sd, &status, &bound);
  202. return p;
  203. }
  204. inline double dcdflib_norm_quantile(double p, double mean, double sd)
  205. {
  206. int what = 2;
  207. int status = 0;
  208. double x, bound, q(1 - p);
  209. cdfnor(&what, &p, &q, &x, &mean, &sd, &status, &bound);
  210. return x;
  211. }
  212. inline double dcdflib_poisson_cdf(double x, double param)
  213. {
  214. int what = 1;
  215. int status = 0;
  216. double p, q, bound;
  217. cdfpoi(&what, &p, &q, &x, &param, &status, &bound);
  218. return p;
  219. }
  220. inline double dcdflib_poisson_quantile(double p, double param)
  221. {
  222. int what = 2;
  223. int status = 0;
  224. double x, bound, q(1 - p);
  225. cdfpoi(&what, &p, &q, &x, &param, &status, &bound);
  226. return x;
  227. }
  228. inline double dcdflib_t_cdf(double x, double param)
  229. {
  230. int what = 1;
  231. int status = 0;
  232. double p, q, bound;
  233. cdft(&what, &p, &q, &x, &param, &status, &bound);
  234. return p;
  235. }
  236. inline double dcdflib_t_quantile(double p, double param)
  237. {
  238. int what = 2;
  239. int status = 0;
  240. double x, bound, q(1 - p);
  241. cdft(&what, &p, &q, &x, &param, &status, &bound);
  242. return x;
  243. }
  244. inline double dcdflib_t_n_cdf(double x, double param, double nc)
  245. {
  246. int what = 1;
  247. int status = 0;
  248. double p, q, bound;
  249. cdftnc(&what, &p, &q, &x, &param, &nc, &status, &bound);
  250. return p;
  251. }
  252. inline double dcdflib_t_n_quantile(double p, double param, double nc)
  253. {
  254. int what = 2;
  255. int status = 0;
  256. double x, bound, q(1 - p);
  257. cdftnc(&what, &p, &q, &x, &param, &nc, &status, &bound);
  258. return x;
  259. }
  260. #endif
  261. extern std::vector<std::vector<double> > data;
  262. void report_execution_time(double t, std::string table, std::string row, std::string heading);
  263. std::string get_compiler_options_name();
  264. inline std::string boost_name()
  265. {
  266. return "boost " + boost::lexical_cast<std::string>(BOOST_VERSION / 100000) + "." + boost::lexical_cast<std::string>((BOOST_VERSION / 100) % 1000);
  267. }
  268. inline std::string compiler_name()
  269. {
  270. #ifdef COMPILER_NAME
  271. return COMPILER_NAME;
  272. #else
  273. return BOOST_COMPILER;
  274. #endif
  275. }
  276. inline std::string platform_name()
  277. {
  278. #ifdef _WIN32
  279. return "Windows x64";
  280. #else
  281. return BOOST_PLATFORM;
  282. #endif
  283. }
  284. #endif // TABLE_HELPER_HPP