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