Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / math / dot_net_example / boost_math / boost_math.cpp
1 // Copyright John Maddock 2007.
2 // Copyright Paul A. Bristow 2007, 2009, 2012.
3
4 // Use, modification and distribution are subject to the
5 // Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt
7 // or copy at http://www.boost.org/LICENSE_1_0.txt)
8
9 // boost_math.cpp  This is the main DLL file.
10
11 //#define BOOST_MATH_OVERFLOW_ERROR_POLICY errno_on_error
12 //#define BOOST_MATH_ASSERT_UNDEFINED_POLICY false
13 // These are now defined in project properties
14 // to avoid complications with pre-compiled headers:
15 // "BOOST_MATH_ASSERT_UNDEFINED_POLICY=0"
16 // "BOOST_MATH_OVERFLOW_ERROR_POLICY="errno_on_error""
17 // so command line shows:
18 // /D "BOOST_MATH_ASSERT_UNDEFINED_POLICY=0"
19 // /D "BOOST_MATH_OVERFLOW_ERROR_POLICY="errno_on_error""
20
21 #include "stdafx.h"
22
23 #ifdef _MSC_VER
24 #  pragma warning(disable: 4400) // 'const boost_math::any_distribution ^' : const/volatile qualifiers on this type are not supported
25 #  pragma warning(disable: 4244) // 'argument' : conversion from 'double' to 'unsigned int', possible loss of data
26 #  pragma warning(disable: 4512) // assignment operator could not be generated
27 // hypergeometric expects integer parameters.
28 #  pragma warning(disable: 4127) // constant
29 #endif
30
31 #include "boost_math.h"
32
33 namespace boost_math
34 {
35
36 any_distribution::any_distribution(int t, double arg1, double arg2, double arg3)
37 {
38    TRANSLATE_EXCEPTIONS_BEGIN
39    // This is where all the work gets done:
40    switch(t) // index of distribution to distribution_info distributions[]
41    {  // New entries must match distribution names, parameter name(s) and defaults defined below.
42    case 0:
43       this->reset(new concrete_distribution<boost::math::bernoulli>(boost::math::bernoulli(arg1)));
44       break;
45    case 1:
46       this->reset(new concrete_distribution<boost::math::beta_distribution<> >(boost::math::beta_distribution<>(arg1, arg2)));
47       break; // Note - no typedef, so need explicit type <> but rely on default = double.
48    case 2:
49       this->reset(new concrete_distribution<boost::math::binomial_distribution<> >(boost::math::binomial_distribution<>(arg1, arg2)));
50       break; // Note - no typedef, so need explicit type <> but rely on default = double.
51    case 3:
52       this->reset(new concrete_distribution<boost::math::cauchy>(boost::math::cauchy(arg1, arg2)));
53       break;
54    case 4:
55       this->reset(new concrete_distribution<boost::math::chi_squared>(boost::math::chi_squared(arg1)));
56       break;
57    case 5:
58       this->reset(new concrete_distribution<boost::math::exponential>(boost::math::exponential(arg1)));
59       break;
60    case 6:
61       this->reset(new concrete_distribution<boost::math::extreme_value>(boost::math::extreme_value(arg1)));
62       break;
63    case 7:
64       this->reset(new concrete_distribution<boost::math::fisher_f >(boost::math::fisher_f(arg1, arg2)));
65       break;
66    case 8:
67       this->reset(new concrete_distribution<boost::math::gamma_distribution<> >(boost::math::gamma_distribution<>(arg1, arg2)));
68       break;
69    case 9:
70       this->reset(new concrete_distribution<boost::math::geometric_distribution<> >(boost::math::geometric_distribution<>(arg1)));
71       break;
72    case 10:
73       this->reset(new concrete_distribution<boost::math::hypergeometric_distribution<> >(boost::math::hypergeometric_distribution<>(arg1, arg2, arg3)));
74       break;
75    case 11:
76       this->reset(new concrete_distribution<boost::math::inverse_chi_squared_distribution<> >(boost::math::inverse_chi_squared_distribution<>(arg1, arg2)));
77       break;
78    case 12:
79       this->reset(new concrete_distribution<boost::math::inverse_gamma_distribution<> >(boost::math::inverse_gamma_distribution<>(arg1, arg2)));
80       break;
81    case 13:
82       this->reset(new concrete_distribution<boost::math::inverse_gaussian_distribution<> >(boost::math::inverse_gaussian_distribution<>(arg1, arg2)));
83       break;
84    case 14:
85       this->reset(new concrete_distribution<boost::math::laplace_distribution<> >(boost::math::laplace_distribution<>(arg1, arg2)));
86       break;
87    case 15:
88       this->reset(new concrete_distribution<boost::math::logistic_distribution<> >(boost::math::logistic_distribution<>(arg1, arg2)));
89       break;
90    case 16:
91       this->reset(new concrete_distribution<boost::math::lognormal_distribution<> >(boost::math::lognormal_distribution<>(arg1, arg2)));
92       break;
93    case 17:
94       this->reset(new concrete_distribution<boost::math::negative_binomial_distribution<> >(boost::math::negative_binomial_distribution<>(arg1, arg2)));
95       break;
96    case 18:
97       this->reset(new concrete_distribution<boost::math::non_central_beta_distribution<> >(boost::math::non_central_beta_distribution<>(arg1, arg2, arg3)));
98       break;
99    case 19:
100       this->reset(new concrete_distribution<boost::math::non_central_chi_squared_distribution<> >(boost::math::non_central_chi_squared_distribution<>(arg1, arg2)));
101       break;
102    case 20:
103       this->reset(new concrete_distribution<boost::math::non_central_f_distribution<> >(boost::math::non_central_f_distribution<>(arg1, arg2, arg3)));
104       break;
105    case 21:
106       this->reset(new concrete_distribution<boost::math::non_central_t_distribution<> >(boost::math::non_central_t_distribution<>(arg1, arg2)));
107       break;
108    case 22:
109       this->reset(new concrete_distribution<boost::math::normal_distribution<> >(boost::math::normal_distribution<>(arg1, arg2)));
110       break;
111    case 23:
112       this->reset(new concrete_distribution<boost::math::pareto>(boost::math::pareto(arg1, arg2)));
113       break;
114    case 24:
115       this->reset(new concrete_distribution<boost::math::poisson>(boost::math::poisson(arg1)));
116       break;
117    case 25:
118       this->reset(new concrete_distribution<boost::math::rayleigh>(boost::math::rayleigh(arg1)));
119       break;
120    case 26:
121       this->reset(new concrete_distribution<boost::math::skew_normal>(boost::math::skew_normal(arg1, arg2, arg3)));
122       break;
123    case 27:
124       this->reset(new concrete_distribution<boost::math::students_t>(boost::math::students_t(arg1)));
125       break;
126    case 28:
127       this->reset(new concrete_distribution<boost::math::triangular>(boost::math::triangular(arg1, arg2, arg3)));
128       break;
129    case 29:
130       this->reset(new concrete_distribution<boost::math::uniform>(boost::math::uniform(arg1, arg2)));
131       break;
132    case 30:
133       this->reset(new concrete_distribution<boost::math::weibull>(boost::math::weibull(arg1, arg2)));
134       break;
135
136
137    default:
138       // TODO  Need some proper error handling here?
139       BOOST_ASSERT(0);
140    }
141    TRANSLATE_EXCEPTIONS_END
142 } // any_distribution constructor.
143
144 struct distribution_info
145 {
146    const char* name; // of distribution.
147    const char* first_param; // Parameters' name like "degrees of freedom",
148    const char* second_param; // if required, else "",
149    const char* third_param; // if required, else "".
150    // triangular and non-centrals need 3 parameters.
151    // (Only the Bi-Weibull would need 5 parameters?)
152    double first_default; // distribution parameter value, often 0, 0.5 or 1.
153    double second_default; // 0 if there isn't a second argument.
154    // Note that defaults below follow default argument in constructors,
155    // if any, but need not be the same.
156    double third_default; // 0 if there isn't a third argument.
157 };
158
159 distribution_info distributions[] =
160 { // distribution name, parameter name(s) and default(s)
161   // Order must match any_distribution constructor above!
162   // Null string "" and zero default for un-used arguments.
163    { "Bernoulli", "Probability", "", "",0.5, 0, 0}, // case 0
164    { "Beta", "Alpha", "Beta", "", 1, 1, 0}, // case 1
165    { "Binomial", "Trials", "Probability of success", "", 1, 0.5, 0}, // case 2
166    { "Cauchy", "Location", "Scale", "", 0, 1, 0}, // case 3
167    { "Chi_squared", "Degrees of freedom", "", "", 1, 0, 0}, // case 4
168    { "Exponential", "lambda", "", "", 1, 0, 0}, // case 5
169    { "Extreme value", "Location", "Scale", "", 0, 1, 0}, // case 6
170    { "Fisher-F", "Degrees of freedom 1", "Degrees of freedom 2", "", 1, 1, 0}, // case 7
171    { "Gamma (Erlang)", "Shape", "Scale", "", 1, 1, 0}, // case 8
172    { "Geometric", "Probability", "", "", 1, 0, 0}, // case 9
173    { "HyperGeometric", "Defects", "Samples", "Objects", 1, 0, 1}, // case 10
174    { "InverseChiSq", "Degrees of Freedom", "Scale", "", 1, 1, 0}, // case 11
175    { "InverseGamma", "Shape", "Scale", "", 1, 1, 0}, // case 12
176    { "InverseGaussian", "Mean", "Scale", "", 1, 1, 0}, // case 13
177    { "Laplace", "Location", "Scale", "", 0, 1, 0}, // case 14
178    { "Logistic", "Location", "Scale", "", 0, 1, 0}, // case 15
179    { "LogNormal", "Location", "Scale", "", 0, 1, 0}, // case 16
180    { "Negative Binomial", "Successes", "Probability of success", "", 1, 0.5, 0}, // case 17
181    { "Noncentral Beta", "Shape alpha", "Shape beta", "Non-centrality", 1, 1, 0}, // case 18
182    { "Noncentral ChiSquare", "Degrees of Freedom", "Non-centrality", "", 1, 0, 0}, // case 19
183    { "Noncentral F", "Degrees of Freedom 1", "Degrees of Freedom 2", "Non-centrality", 1, 1, 0}, // case 20
184    { "Noncentral t", "Degrees of Freedom", "Non-centrality", "", 1, 0, 0}, // case 21
185    { "Normal (Gaussian)", "Mean", "Standard Deviation", "", 0, 1, 0}, // case 22
186    { "Pareto", "Location", "Shape","", 1, 1, 0}, // case 23
187    { "Poisson", "Mean", "", "", 1, 0, 0}, // case 24
188    { "Rayleigh", "Shape", "", "", 1, 0, 0}, // case 25
189    { "Skew Normal", "Location", "Shape", "Skew", 0, 1, 0}, // case 27  (defaults to Gaussian).
190    { "Student's t", "Degrees of Freedom", "", "", 1, 0, 0}, // case 28
191    { "Triangular", "Lower", "Mode", "Upper", -1, 0, +1 }, // case 29 3rd parameter!
192    // 0, 0.5, 1 also said to be 'standard' but this is most like an approximation to Gaussian distribution.
193    { "Uniform", "Lower", "Upper", "", 0, 1, 0}, // case 30
194    { "Weibull", "Shape", "Scale", "", 1, 1, 0}, // case 31
195 };
196
197 // How many distributions are supported:
198 int any_distribution::size()
199 {
200    return sizeof(distributions) / sizeof(distributions[0]);
201 }
202
203 // Display name of i'th distribution:
204 System::String^ any_distribution::distribution_name(int i)
205 {
206    if(i >= size())
207       return "";
208    return gcnew System::String(distributions[i].name);
209 }
210 // Name of first distribution parameter, or null if not supported:
211 System::String^ any_distribution::first_param_name(int i)
212 {
213    if(i >= size())
214       return "";
215    return gcnew System::String(distributions[i].first_param);
216 }
217 // Name of second distribution parameter, or null if not supported:
218 System::String^ any_distribution::second_param_name(int i)
219 {
220    if(i >= size())
221       return "";
222    return gcnew System::String(distributions[i].second_param);
223 }
224 // Name of third distribution parameter, or null if not supported:
225 System::String^ any_distribution::third_param_name(int i)
226 {
227    if(i >= size())
228       return "";
229    return gcnew System::String(distributions[i].third_param);
230 }
231 // default value for first parameter:
232 double any_distribution::first_param_default(int i)
233 {
234    if(i >= size())
235       return 0;
236    return distributions[i].first_default;
237 }
238 // default value for second parameter:
239 double any_distribution::second_param_default(int i)
240 {
241    if(i >= size())
242       return 0;
243    return distributions[i].second_default;
244 }
245 // default value for third parameter:
246 double any_distribution::third_param_default(int i)
247 {
248    if(i >= size())
249       return 0;
250    return distributions[i].third_default;
251 }
252
253 } // namespace boost_math
254
255