boost_math.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // boost_math.h
  2. // Copyright John Maddock 2007.
  3. // Copyright Paul A. Bristow 2007.
  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. //#define BOOST_MATH_OVERFLOW_ERROR_POLICY errno_on_error
  9. //#define BOOST_MATH_ASSERT_UNDEFINED_POLICY false
  10. // These are now defined in project properties
  11. // "BOOST_MATH_ASSERT_UNDEFINED_POLICY=0"
  12. // "BOOST_MATH_OVERFLOW_ERROR_POLICY=errno_on_error"
  13. // to avoid complications with pre-compiled headers.
  14. #ifdef _MSC_VER
  15. # pragma once
  16. # pragma warning (disable : 4127)
  17. #endif
  18. using namespace System;
  19. #define TRANSLATE_EXCEPTIONS_BEGIN try{
  20. #define TRANSLATE_EXCEPTIONS_END \
  21. }catch(const std::exception& e){ \
  22. System::String^ s = gcnew System::String(e.what());\
  23. InvalidOperationException^ se = gcnew InvalidOperationException(s);\
  24. throw se; \
  25. }
  26. namespace boost_math {
  27. class any_imp
  28. {
  29. public:
  30. // Distribution properties.
  31. virtual double mean()const = 0;
  32. virtual double mode()const = 0;
  33. virtual double median()const = 0;
  34. virtual double variance()const = 0;
  35. virtual double standard_deviation()const = 0;
  36. virtual double skewness()const = 0;
  37. virtual double kurtosis()const = 0;
  38. virtual double kurtosis_excess()const = 0;
  39. virtual double coefficient_of_variation()const = 0;
  40. // Values computed from random variate x.
  41. virtual double hazard(double x)const = 0;
  42. virtual double chf(double x)const = 0;
  43. virtual double cdf(double x)const = 0;
  44. virtual double ccdf(double x)const = 0;
  45. virtual double pdf(double x)const = 0;
  46. virtual double quantile(double x)const = 0;
  47. virtual double quantile_c(double x)const = 0;
  48. // Range & support of x
  49. virtual double lowest()const = 0;
  50. virtual double uppermost()const = 0;
  51. virtual double lower()const = 0;
  52. virtual double upper()const = 0;
  53. };
  54. template <class Distribution>
  55. class concrete_distribution : public any_imp
  56. {
  57. public:
  58. concrete_distribution(const Distribution& d) : m_dist(d) {}
  59. // Distribution properties.
  60. virtual double mean()const
  61. {
  62. return boost::math::mean(m_dist);
  63. }
  64. virtual double median()const
  65. {
  66. return boost::math::median(m_dist);
  67. }
  68. virtual double mode()const
  69. {
  70. return boost::math::mode(m_dist);
  71. }
  72. virtual double variance()const
  73. {
  74. return boost::math::variance(m_dist);
  75. }
  76. virtual double skewness()const
  77. {
  78. return boost::math::skewness(m_dist);
  79. }
  80. virtual double standard_deviation()const
  81. {
  82. return boost::math::standard_deviation(m_dist);
  83. }
  84. virtual double coefficient_of_variation()const
  85. {
  86. return boost::math::coefficient_of_variation(m_dist);
  87. }
  88. virtual double kurtosis()const
  89. {
  90. return boost::math::kurtosis(m_dist);
  91. }
  92. virtual double kurtosis_excess()const
  93. {
  94. return boost::math::kurtosis_excess(m_dist);
  95. }
  96. // Range of x for the distribution.
  97. virtual double lowest()const
  98. {
  99. return boost::math::range(m_dist).first;
  100. }
  101. virtual double uppermost()const
  102. {
  103. return boost::math::range(m_dist).second;
  104. }
  105. // Support of x for the distribution.
  106. virtual double lower()const
  107. {
  108. return boost::math::support(m_dist).first;
  109. }
  110. virtual double upper()const
  111. {
  112. return boost::math::support(m_dist).second;
  113. }
  114. // Values computed from random variate x.
  115. virtual double hazard(double x)const
  116. {
  117. return boost::math::hazard(m_dist, x);
  118. }
  119. virtual double chf(double x)const
  120. {
  121. return boost::math::chf(m_dist, x);
  122. }
  123. virtual double cdf(double x)const
  124. {
  125. return boost::math::cdf(m_dist, x);
  126. }
  127. virtual double ccdf(double x)const
  128. {
  129. return boost::math::cdf(complement(m_dist, x));
  130. }
  131. virtual double pdf(double x)const
  132. {
  133. return boost::math::pdf(m_dist, x);
  134. }
  135. virtual double quantile(double x)const
  136. {
  137. return boost::math::quantile(m_dist, x);
  138. }
  139. virtual double quantile_c(double x)const
  140. {
  141. return boost::math::quantile(complement(m_dist, x));
  142. }
  143. private:
  144. Distribution m_dist;
  145. };
  146. public ref class any_distribution
  147. {
  148. public:
  149. // Added methods for this class here.
  150. any_distribution(int t, double arg1, double arg2, double arg3);
  151. ~any_distribution()
  152. {
  153. reset(0);
  154. }
  155. // Is it OK for these to be inline?
  156. // Distribution properties as 'pointer-to-implementions'.
  157. double mean()
  158. {
  159. TRANSLATE_EXCEPTIONS_BEGIN
  160. return pimpl->mean();
  161. TRANSLATE_EXCEPTIONS_END
  162. }
  163. double median()
  164. {
  165. TRANSLATE_EXCEPTIONS_BEGIN
  166. return pimpl->median();
  167. TRANSLATE_EXCEPTIONS_END
  168. }
  169. double mode()
  170. {
  171. TRANSLATE_EXCEPTIONS_BEGIN
  172. return pimpl->mode();
  173. TRANSLATE_EXCEPTIONS_END
  174. }
  175. double variance()
  176. {
  177. TRANSLATE_EXCEPTIONS_BEGIN
  178. return pimpl->variance();
  179. TRANSLATE_EXCEPTIONS_END
  180. }
  181. double standard_deviation()
  182. {
  183. TRANSLATE_EXCEPTIONS_BEGIN
  184. return pimpl->standard_deviation();
  185. TRANSLATE_EXCEPTIONS_END
  186. }
  187. double coefficient_of_variation()
  188. { // aka Relative Standard deviation.
  189. TRANSLATE_EXCEPTIONS_BEGIN
  190. return pimpl->coefficient_of_variation();
  191. TRANSLATE_EXCEPTIONS_END
  192. }
  193. double skewness()
  194. {
  195. TRANSLATE_EXCEPTIONS_BEGIN
  196. return pimpl->skewness();
  197. TRANSLATE_EXCEPTIONS_END
  198. }
  199. double kurtosis()
  200. {
  201. TRANSLATE_EXCEPTIONS_BEGIN
  202. return pimpl->kurtosis();
  203. TRANSLATE_EXCEPTIONS_END
  204. }
  205. double kurtosis_excess()
  206. {
  207. TRANSLATE_EXCEPTIONS_BEGIN
  208. return pimpl->kurtosis_excess();
  209. TRANSLATE_EXCEPTIONS_END
  210. }
  211. // Values computed from random variate x.
  212. double hazard(double x)
  213. {
  214. TRANSLATE_EXCEPTIONS_BEGIN
  215. return pimpl->hazard(x);
  216. TRANSLATE_EXCEPTIONS_END
  217. }
  218. double chf(double x)
  219. {
  220. TRANSLATE_EXCEPTIONS_BEGIN
  221. return pimpl->chf(x);
  222. TRANSLATE_EXCEPTIONS_END
  223. }
  224. double cdf(double x)
  225. {
  226. TRANSLATE_EXCEPTIONS_BEGIN
  227. return pimpl->cdf(x);
  228. TRANSLATE_EXCEPTIONS_END
  229. }
  230. double ccdf(double x)
  231. {
  232. TRANSLATE_EXCEPTIONS_BEGIN
  233. return pimpl->ccdf(x);
  234. TRANSLATE_EXCEPTIONS_END
  235. }
  236. double pdf(double x)
  237. {
  238. TRANSLATE_EXCEPTIONS_BEGIN
  239. return pimpl->pdf(x);
  240. TRANSLATE_EXCEPTIONS_END
  241. }
  242. double quantile(double x)
  243. {
  244. TRANSLATE_EXCEPTIONS_BEGIN
  245. return pimpl->quantile(x);
  246. TRANSLATE_EXCEPTIONS_END
  247. }
  248. double quantile_c(double x)
  249. {
  250. TRANSLATE_EXCEPTIONS_BEGIN
  251. return pimpl->quantile_c(x);
  252. TRANSLATE_EXCEPTIONS_END
  253. }
  254. double lowest()
  255. {
  256. TRANSLATE_EXCEPTIONS_BEGIN
  257. return pimpl->lowest();
  258. TRANSLATE_EXCEPTIONS_END
  259. }
  260. double uppermost()
  261. {
  262. TRANSLATE_EXCEPTIONS_BEGIN
  263. return pimpl->uppermost();
  264. TRANSLATE_EXCEPTIONS_END
  265. }
  266. double lower()
  267. {
  268. TRANSLATE_EXCEPTIONS_BEGIN
  269. return pimpl->lower();
  270. TRANSLATE_EXCEPTIONS_END
  271. }
  272. double upper()
  273. {
  274. TRANSLATE_EXCEPTIONS_BEGIN
  275. return pimpl->upper();
  276. TRANSLATE_EXCEPTIONS_END
  277. }
  278. // How many distributions are supported:
  279. static int size();
  280. // Display name of i'th distribution:
  281. static System::String^ distribution_name(int i);
  282. // Name of first distribution parameter, or null if not supported:
  283. static System::String^ first_param_name(int i);
  284. // Name of second distribution parameter, or null if not supported:
  285. static System::String^ second_param_name(int i);
  286. // Name of third distribution parameter, or null if not supported:
  287. static System::String^ third_param_name(int i);
  288. // Default value for first parameter:
  289. static double first_param_default(int i);
  290. // Default value for second parameter:
  291. static double second_param_default(int i);
  292. // Default value for third parameter:
  293. static double third_param_default(int i);
  294. private:
  295. any_distribution(const any_distribution^)
  296. { // Constructor is private.
  297. }
  298. const any_distribution^ operator=(const any_distribution^ d)
  299. { // Copy Constructor is private too.
  300. return d;
  301. }
  302. // We really should use a shared_ptr here,
  303. // but apparently it's not allowed in a managed class like this :-(
  304. void reset(any_imp* p)
  305. {
  306. if(pimpl)
  307. { // Exists already, so
  308. delete pimpl;
  309. }
  310. pimpl = p;
  311. }
  312. any_imp* pimpl;
  313. };
  314. }