File: git_issue_1249.cpp

package info (click to toggle)
scipy 1.17.0-1exp2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 235,340 kB
  • sloc: cpp: 506,914; python: 357,038; ansic: 215,028; javascript: 89,566; fortran: 19,308; cs: 3,081; f90: 1,150; sh: 860; makefile: 519; pascal: 284; lisp: 134; xml: 56; perl: 51
file content (145 lines) | stat: -rw-r--r-- 3,566 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//  (C) Copyright Kilian Kilger 2025.
//  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)

#define BOOST_TEST_MAIN

#include <boost/test/unit_test.hpp>
#include <boost/test/tools/floating_point_comparison.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/test/results_collector.hpp>
#include <boost/math/special_functions/gamma.hpp>
#include <boost/multiprecision/cpp_bin_float.hpp>

using namespace std;
using namespace boost::math;
using namespace boost::math::policies;
using namespace boost::multiprecision;

typedef policy<
   policies::domain_error<errno_on_error>,
   policies::pole_error<errno_on_error>,
   policies::overflow_error<errno_on_error>,
   policies::evaluation_error<errno_on_error>
> c_policy;

template<typename T>
struct test_lower
{
   T operator()(T a, T x) const
   {
      return tgamma_lower(a, x, c_policy());
   }

   T expected(T a) const
   {
      return T(0.0);
   }
};

template<typename T>
struct test_upper
{
   T operator()(T a, T x) const
   {
      return tgamma(a, x, c_policy());
   }
   T expected(T a) const
   {
      return tgamma(a, c_policy());
   }
};

template<typename T>
struct test_gamma_p
{
   T operator()(T a, T x) const
   {
      return gamma_p(a, x, c_policy());
   }
   T expected(T) const
   {
      return T(0.0);
   }
};

template<typename T>
struct test_gamma_q
{
   T operator()(T a, T x) const
   {
      return gamma_q(a, x, c_policy());
   }
   T expected(T) const
   {
      return T(1.0);
   }
};

template<typename T, template<typename> class Fun>
void test_impl(T a)
{
   Fun<T> fn;
   errno = 0;
   T x = T(0.0);
   T result = fn(a, x);
   int saveErrno = errno;

   errno = 0;

   T expected = fn.expected(a);

   BOOST_CHECK(errno == saveErrno);
   BOOST_CHECK_EQUAL(result, expected);
}

template<template<typename> class Fun, typename T>
void test_type_dispatch(T val)
{
   if (val <= (std::numeric_limits<float>::max)())
      test_impl<float, Fun>(static_cast<float>(val));
   if (val <= (std::numeric_limits<double>::max)())
      test_impl<double, Fun>(static_cast<double>(val));
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
   test_impl<long double, Fun>(static_cast<long double>(val));
#endif
   test_impl<cpp_bin_float_50, Fun>(static_cast<cpp_bin_float_50>(val));
}

template<template<typename> class Fun>
void test_impl()
{
   test_type_dispatch<Fun, double>(1.0);
   test_type_dispatch<Fun, double>(0.1);
   test_type_dispatch<Fun, double>(0.5);
   test_type_dispatch<Fun, double>(0.6);
   test_type_dispatch<Fun, double>(1.3);
   test_type_dispatch<Fun, double>(1.5);
   test_type_dispatch<Fun, double>(2);
   test_type_dispatch<Fun, double>(100);
   test_type_dispatch<Fun, double>((std::numeric_limits<float>::max)());
   test_type_dispatch<Fun, double>((std::numeric_limits<double>::max)());
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS   
   test_type_dispatch<Fun, long double>((std::numeric_limits<long double>::max)());
#endif
}

void test_derivative()
{
   using namespace boost::math::detail;
   double derivative = 0;
   double result = gamma_incomplete_imp(1.0, 0.0, true, false, c_policy(), &derivative);
   BOOST_CHECK(errno == 0);
   BOOST_CHECK_EQUAL(derivative, tools::max_value<double>() / 2);
   BOOST_CHECK_EQUAL(result, 0);
}

BOOST_AUTO_TEST_CASE( test_main )
{
   test_impl<test_lower>();
   test_impl<test_upper>();
   test_impl<test_gamma_p>();
   test_impl<test_gamma_q>();
   test_derivative();
}