⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_triangular.cpp

📁 Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// Copyright Paul Bristow 2006, 2007.// Copyright John Maddock 2006, 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)// test_triangular.cpp#ifdef _MSC_VER#  pragma warning(disable: 4127) // conditional expression is constant.#  pragma warning(disable: 4305) // truncation from 'long double' to 'float'#endif#include <boost/math/concepts/real_concept.hpp> // for real_concept#include <boost/test/included/test_exec_monitor.hpp> // Boost.Test#include <boost/test/floating_point_comparison.hpp>#include <boost/math/distributions/triangular.hpp>using boost::math::triangular_distribution;#include <boost/math/tools/test.hpp>#include <boost/math/special_functions/fpclassify.hpp>#include <iostream>using std::cout;using std::endl;using std::scientific;using std::fixed;using std::left;using std::right;using std::setw;using std::setprecision;using std::showpos;#include <limits>using std::numeric_limits;template <class RealType>void check_triangular(RealType lower, RealType mode, RealType upper, RealType x, RealType p, RealType q, RealType tol){  BOOST_CHECK_CLOSE_FRACTION(    ::boost::math::cdf(    triangular_distribution<RealType>(lower, mode, upper),   // distribution.    x),  // random variable.    p,    // probability.    tol);   // tolerance.  BOOST_CHECK_CLOSE_FRACTION(    ::boost::math::cdf(    complement(    triangular_distribution<RealType>(lower, mode, upper), // distribution.    x)),    // random variable.    q,    // probability complement.    tol);  // tolerance.  BOOST_CHECK_CLOSE_FRACTION(    ::boost::math::quantile(    triangular_distribution<RealType>(lower,mode, upper),  // distribution.    p),   // probability.    x,  // random variable.    tol);  // tolerance.  BOOST_CHECK_CLOSE_FRACTION(    ::boost::math::quantile(    complement(    triangular_distribution<RealType>(lower, mode, upper),  // distribution.    q)),     // probability complement.    x,                                             // random variable.    tol);  // tolerance.} // void check_triangulartemplate <class RealType>void test_spots(RealType){  // Basic sanity checks:  //  // Some test values were generated for the triangular distribution  // using the online calculator at   // http://espse.ed.psu.edu/edpsych/faculty/rhale/hale/507Mat/statlets/free/pdist.htm  //  // Tolerance is just over 5 epsilon expressed as a fraction:  RealType tolerance = boost::math::tools::epsilon<RealType>() * 5; // 5 eps as a fraction.  RealType tol5eps = boost::math::tools::epsilon<RealType>() * 5; // 5 eps as a fraction.  cout << "Tolerance for type " << typeid(RealType).name()  << " is " << tolerance << "." << endl;    using namespace std; // for ADL of std::exp;  // Tests on construction  // Default should be 0, 0, 1  BOOST_CHECK_EQUAL(triangular_distribution<RealType>().lower(), -1);  BOOST_CHECK_EQUAL(triangular_distribution<RealType>().mode(), 0);  BOOST_CHECK_EQUAL(triangular_distribution<RealType>().upper(), 1);  BOOST_CHECK_EQUAL(support(triangular_distribution<RealType>()).first, triangular_distribution<RealType>().lower());  BOOST_CHECK_EQUAL(support(triangular_distribution<RealType>()).second, triangular_distribution<RealType>().upper());  if (std::numeric_limits<RealType>::has_quiet_NaN == true)  {  BOOST_CHECK_THROW( // duff parameter lower.    triangular_distribution<RealType>(static_cast<RealType>(std::numeric_limits<RealType>::quiet_NaN()), 0, 0),    std::domain_error);  BOOST_CHECK_THROW( // duff parameter mode.    triangular_distribution<RealType>(0, static_cast<RealType>(std::numeric_limits<RealType>::quiet_NaN()), 0),    std::domain_error);  BOOST_CHECK_THROW( // duff parameter upper.    triangular_distribution<RealType>(0, 0, static_cast<RealType>(std::numeric_limits<RealType>::quiet_NaN())),    std::domain_error);  } // quiet_NaN tests.  BOOST_CHECK_THROW( // duff parameters upper < lower.    triangular_distribution<RealType>(1, 0, -1),    std::domain_error);  BOOST_CHECK_THROW( // duff parameters upper == lower.    triangular_distribution<RealType>(0, 0, 0),    std::domain_error);  BOOST_CHECK_THROW( // duff parameters mode < lower.    triangular_distribution<RealType>(0, -1, 1),    std::domain_error);  BOOST_CHECK_THROW( // duff parameters mode > upper.    triangular_distribution<RealType>(0, 2, 1),    std::domain_error);  // Tests for PDF  // // triangular_distribution<RealType>() default is (0, 0, 1), mode == lower.  BOOST_CHECK_CLOSE_FRACTION( // x == lower == mode    pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(0)),     static_cast<RealType>(2),     tolerance);  BOOST_CHECK_CLOSE_FRACTION( // x == upper    pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(1)),     static_cast<RealType>(0),     tolerance);  BOOST_CHECK_CLOSE_FRACTION( // x > upper    pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(-1)),     static_cast<RealType>(0),     tolerance);   BOOST_CHECK_CLOSE_FRACTION( // x < lower    pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(2)),     static_cast<RealType>(0),     tolerance);  BOOST_CHECK_CLOSE_FRACTION( // x < lower    pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(2)),     static_cast<RealType>(0),     tolerance);  // triangular_distribution<RealType>() (0, 1, 1) mode == upper  BOOST_CHECK_CLOSE_FRACTION( // x == lower    pdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(0)),     static_cast<RealType>(0),     tolerance);  BOOST_CHECK_CLOSE_FRACTION( // x == upper    pdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(1)),     static_cast<RealType>(2),     tolerance);  BOOST_CHECK_CLOSE_FRACTION( // x > upper    pdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(-1)),     static_cast<RealType>(0),     tolerance);   BOOST_CHECK_CLOSE_FRACTION( // x < lower    pdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(2)),     static_cast<RealType>(0),     tolerance);  BOOST_CHECK_CLOSE_FRACTION( // x < middle so Wiki says special case pdf = 2 * x    pdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(0.25)),     static_cast<RealType>(0.5),     tolerance);  BOOST_CHECK_CLOSE_FRACTION( // x < middle so Wiki says special case cdf = x * x    cdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(0.25)),     static_cast<RealType>(0.25 * 0.25),     tolerance);  // triangular_distribution<RealType>() (0, 0.5, 1) mode == middle.  BOOST_CHECK_CLOSE_FRACTION( // x == lower    pdf(triangular_distribution<RealType>(0, 0.5, 1), static_cast<RealType>(0)),     static_cast<RealType>(0),     tolerance);  BOOST_CHECK_CLOSE_FRACTION( // x == upper    pdf(triangular_distribution<RealType>(0, 0.5, 1), static_cast<RealType>(1)),     static_cast<RealType>(0),     tolerance);  BOOST_CHECK_CLOSE_FRACTION( // x > upper    pdf(triangular_distribution<RealType>(0, 0.5, 1), static_cast<RealType>(-1)),     static_cast<RealType>(0),     tolerance);   BOOST_CHECK_CLOSE_FRACTION( // x < lower    pdf(triangular_distribution<RealType>(0, 0.5, 1), static_cast<RealType>(2)),     static_cast<RealType>(0),     tolerance);  BOOST_CHECK_CLOSE_FRACTION( // x == mode    pdf(triangular_distribution<RealType>(0, 0.5, 1), static_cast<RealType>(0.5)),     static_cast<RealType>(2),     tolerance);  BOOST_CHECK_CLOSE_FRACTION( // x == half mode    pdf(triangular_distribution<RealType>(0, 0.5, 1), static_cast<RealType>(0.25)),     static_cast<RealType>(1),     tolerance);  BOOST_CHECK_CLOSE_FRACTION( // x == half mode    pdf(triangular_distribution<RealType>(0, 0.5, 1), static_cast<RealType>(0.75)),     static_cast<RealType>(1),     tolerance);  if(std::numeric_limits<RealType>::has_infinity)  { // BOOST_CHECK tests for infinity using std::numeric_limits<>::infinity()    // Note that infinity is not implemented for real_concept, so these tests    // are only done for types, like built-in float, double.. that have infinity.    // Note that these assume that  BOOST_MATH_OVERFLOW_ERROR_POLICY is NOT throw_on_error.    // #define BOOST_MATH_OVERFLOW_ERROR_POLICY == throw_on_error would give a throw here.    // #define BOOST_MATH_DOMAIN_ERROR_POLICY == throw_on_error IS defined, so the throw path    // of error handling is tested below with BOOST_CHECK_THROW tests.    BOOST_CHECK_THROW( // x == infinity NOT OK.      pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(std::numeric_limits<RealType>::infinity())),       std::domain_error);    BOOST_CHECK_THROW( // x == minus infinity not OK too.      pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(-std::numeric_limits<RealType>::infinity())),       std::domain_error);  }  if(std::numeric_limits<RealType>::has_quiet_NaN)  { // BOOST_CHECK tests for NaN using std::numeric_limits<>::has_quiet_NaN() - should throw.    BOOST_CHECK_THROW(      pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(std::numeric_limits<RealType>::quiet_NaN())),       std::domain_error);    BOOST_CHECK_THROW(

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -