#ifndef CLASS_DEF_HPP #define CLASS_DEF_HPP #include #include #include "boost/math/distributions.hpp" #include "boost/format.hpp" // Round up to achieve correct ppf(cdf) round-trips for discrete distributions typedef boost::math::policies::policy< boost::math::policies::discrete_quantile< boost::math::policies::integer_round_up > > Policy; // Run user_error function when evaluation_errors and overflow_errors are encountered typedef boost::math::policies::policy< boost::math::policies::evaluation_error, boost::math::policies::overflow_error > user_error_policy; BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS(user_error_policy) // Raise a RuntimeWarning making users aware that something went wrong during // evaluation of the function, but return the best guess template RealType boost::math::policies::user_evaluation_error(const char* function, const char* message, const RealType& val) { std::string msg("Error in function "); msg += (boost::format(function) % typeid(RealType).name()).str() + ": "; // "message" may have %1%, but arguments don't always contain all // required information, so don't call boost::format for now msg += message; PyGILState_STATE save = PyGILState_Ensure(); PyErr_WarnEx(PyExc_RuntimeWarning, msg.c_str(), 1); PyGILState_Release(save); return val; } template RealType boost::math::policies::user_overflow_error(const char* function, const char* message, const RealType& val) { std::string msg("Error in function "); msg += (boost::format(function) % typeid(RealType).name()).str() + ": "; // From Boost docs: "overflow and underflow messages do not contain this %1% specifier // (since the value of value is immaterial in these cases)." msg += message; PyGILState_STATE save = PyGILState_Ensure(); PyErr_SetString(PyExc_OverflowError, msg.c_str()); PyGILState_Release(save); return 0; } template