|
Boost-Commit : |
From: pbristow_at_[hidden]
Date: 2007-07-30 07:31:49
Author: pbristow
Date: 2007-07-30 07:31:49 EDT (Mon, 30 Jul 2007)
New Revision: 7586
URL: http://svn.boost.org/trac/boost/changeset/7586
Log:
Further examples
Added:
sandbox/math_toolkit/libs/math/example/error_policies_example.cpp (contents, props changed)
sandbox/math_toolkit/libs/math/example/error_policy_example.cpp (contents, props changed)
Added: sandbox/math_toolkit/libs/math/example/error_policies_example.cpp
==============================================================================
--- (empty file)
+++ sandbox/math_toolkit/libs/math/example/error_policies_example.cpp 2007-07-30 07:31:49 EDT (Mon, 30 Jul 2007)
@@ -0,0 +1,97 @@
+// example_policy_normal.cpp
+
+// Copyright Paul A. Bristow 2007.
+// Copyright John Maddock 2007.
+
+// Use, modification and distribution are subject to the
+// Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt
+// or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+#include <boost/math/distributions/normal.hpp>
+ using boost::math::normal_distribution;
+
+#include <boost/math/distributions/students_t.hpp>
+ using boost::math::students_t; // Probability of students_t(df, t).
+ using boost::math::students_t_distribution;
+
+// using namespace boost::math;
+//.\error_policy_normal.cpp(30) : error C2872: 'policy' : ambiguous symbol
+// could be 'I:\Boost-sandbox\math_toolkit\boost/math/policy/policy.hpp(392) : boost::math::policy::policy'
+// or 'boost::math::policy'
+
+ // So can't use this using namespace command.
+// Suppose we want a statistical distribution to return infinities,
+// rather than throw exceptions (the default policy), then we can use:
+
+// std
+#include <iostream>
+ using std::cout;
+ using std::endl;
+
+using boost::math::policy::policy;
+// Possible errors
+using boost::math::policy::overflow_error;
+using boost::math::policy::underflow_error;
+using boost::math::policy::domain_error;
+using boost::math::policy::pole_error;
+using boost::math::policy::denorm_error;
+using boost::math::policy::evaluation_error;
+
+using boost::math::policy::ignore_error;
+
+// Define a custom policy to ignore just overflow:
+typedef policy<
+overflow_error<ignore_error>
+ > my_policy;
+
+// Define another custom policy (perhaps ill-advised?)
+// to ignore all errors: domain, pole, overflow, underflow, denorm & evaluation:
+typedef policy<
+domain_error<ignore_error>,
+pole_error<ignore_error>,
+overflow_error<ignore_error>,
+underflow_error<ignore_error>,
+denorm_error<ignore_error>,
+evaluation_error<ignore_error>
+ > my_ignoreall_policy;
+
+// Define a new distribution with a custom policy to ignore_error
+// (& thus perhaps return infinity for some arguments):
+typedef boost::math::normal_distribution<double, my_policy> my_normal;
+// Note: uses default parameters zero mean and unit standard deviation.
+
+// We could also do the same for another distribution, for example:
+using boost::math::students_t_distribution;
+typedef students_t_distribution<double, my_ignoreall_policy> my_students_t;
+
+int main()
+{
+ cout << "quantile(my_normal(), 0.05); = " << quantile(my_normal(), 0.05) << endl; // 0.05 is argument within normal range.
+ cout << "quantile(my_normal(), 0.); = " << quantile(my_normal(), 0.) << endl; // argument zero, so expect infinity.
+ cout << "quantile(my_normal(), 0.); = " << quantile(my_normal(), 0.F) << endl; // argument zero, so expect infinity.
+
+ cout << "quantile(my_students_t(), 0.); = " << quantile(my_students_t(-1), 0.F) << endl; // 'bad' argument negative, so expect NaN.
+
+ // Construct a (0, 1) normal distribution that ignores all errors,
+ // returning NaN, infinity, zero, or best guess,
+ // and NOT setting errno.
+ normal_distribution<long double, my_ignoreall_policy> my_normal2(0.L, 1.L); // explicit parameters for distribution.
+ cout << "quantile(my_normal2(), 0.); = " << quantile(my_normal2, 0.01) << endl; // argument 0.01, so result finite.
+ cout << "quantile(my_normal2(), 0.); = " << quantile(my_normal2, 0.) << endl; // argument zero, so expect infinity.
+
+ return 0;
+}
+
+/*
+
+Output:
+
+quantile(my_normal(), 0.05); = -1.64485
+quantile(my_normal(), 0.); = -1.#INF
+quantile(my_normal(), 0.); = -1.#INF
+quantile(my_students_t(), 0.); = 1.#QNAN
+quantile(my_normal2(), 0.); = -2.32635
+quantile(my_normal2(), 0.); = -1.#INF
+
+*/
\ No newline at end of file
Added: sandbox/math_toolkit/libs/math/example/error_policy_example.cpp
==============================================================================
--- (empty file)
+++ sandbox/math_toolkit/libs/math/example/error_policy_example.cpp 2007-07-30 07:31:49 EDT (Mon, 30 Jul 2007)
@@ -0,0 +1,76 @@
+// example_policy_handling.cpp
+
+// Copyright Paul A. Bristow 2007.
+// Copyright John Maddock 2007.
+
+// Use, modification and distribution are subject to the
+// Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt
+// or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// See error_handling_example.cpp for use of
+// macro definition to change policy for
+// domain_error - negative degrees of freedom argument
+// for student's t distribution CDF,
+// and catching the exception.
+
+// See error_handling_policies for more examples.
+
+// Boost
+#include <boost/math/distributions/students_t.hpp>
+using boost::math::students_t_distribution; // Probability of students_t(df, t).
+using boost::math::students_t; // Probability of students_t(df, t) convenience typedef.
+
+// std
+#include <iostream>
+ using std::cout;
+ using std::endl;
+
+#include <stdexcept>
+ using std::exception;
+
+using boost::math::policy::policy;
+using boost::math::policy::domain_error;
+using boost::math::policy::ignore_error;
+
+// Define a (bad?) policy to ignore domain errors ('bad' arguments):
+typedef policy<
+ domain_error<ignore_error>
+ > my_policy;
+
+// Define my distribution with this different domain error policy:
+typedef students_t_distribution<double, my_policy> my_students_t;
+
+int main()
+{ // Example of error handling of bad argument(s) to a distribution.
+ cout << "Example error handling using Student's t function. " << endl;
+
+ double degrees_of_freedom = -1; double t = -1.; // Bad arguments!
+
+ try
+ {
+ cout << "Probability of ignore_error Student's t is " << cdf(my_students_t(degrees_of_freedom), t) << endl;
+ cout << "Probability of default error policy Student's t is " << endl;
+ cout << cdf(students_t(-1), -1) << endl;
+ }
+ catch(const std::exception& e)
+ {
+ std::cout <<
+ "\n""Message from thrown exception was:\n " << e.what() << std::endl;
+ }
+
+ return 0;
+} // int main()
+
+/*
+
+Output:
+
+Example error handling using Student's t function.
+Probability of ignore_error Student's t is 1.#QNAN
+Probability of default error policy Student's t is
+Message from thrown exception was:
+ Error in function boost::math::students_t_distribution<double>::students_t_distribution:
+ Degrees of freedom argument is -1, but must be > 0 !
+
+*/
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk