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