]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/tools/erf_data.cpp
1 // (C) Copyright John Maddock 2006.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #include <boost/math/special_functions/gamma.hpp>
7 #include <boost/math/special_functions/erf.hpp> // for inverses
8 #include <boost/math/constants/constants.hpp>
10 #include <boost/math/tools/test_data.hpp>
13 using namespace boost::math::tools
;
17 float force_truncate(const float* f
)
23 float truncate_to_float(mp_t r
)
25 float f
= boost::math::tools::real_cast
<float>(r
);
26 return force_truncate(&f
);
29 struct erf_data_generator
31 boost::math::tuple
<mp_t
, mp_t
> operator()(mp_t z
)
33 // very naively calculate spots using the gamma function at high precision:
41 g1
= boost::math::tgamma_lower(mp_t(0.5), z
* z
);
42 g1
/= sqrt(boost::math::constants::pi
<mp_t
>());
51 g2
= boost::math::tgamma(mp_t(0.5), z
* z
);
52 g2
/= sqrt(boost::math::constants::pi
<mp_t
>());
56 return boost::math::make_tuple(g1
, g2
);
60 double double_factorial(int N
)
71 void asymptotic_limit(int Bits
)
74 // The following block of code estimates how large z has
75 // to be before we can use the asymptotic expansion for
76 // erf/erfc and still get convergence: the series becomes
77 // divergent eventually so we have to be careful!
79 double result
= (std::numeric_limits
<double>::max
)();
81 for(int n
= 1; n
< 15; ++n
)
83 double lim
= (Bits
-n
) * log(2.0) - log(sqrt(3.14)) + log(double_factorial(2*n
+1));
85 while(x
*x
+ (2*n
+1)*log(x
) <= lim
)
94 std::cout
<< "Erf asymptotic limit for "
95 << Bits
<< " bit numbers is "
96 << result
<< " after approximately "
97 << terms
<< " terms." << std::endl
;
99 result
= (std::numeric_limits
<double>::max
)();
101 for(int n
= 1; n
< 30; ++n
)
103 double x
= pow(double_factorial(2*n
+1)/pow(2.0, n
-Bits
), 1 / (2.0*n
));
111 std::cout
<< "Erfc asymptotic limit for "
112 << Bits
<< " bit numbers is "
113 << result
<< " after approximately "
114 << terms
<< " terms." << std::endl
;
117 boost::math::tuple
<mp_t
, mp_t
> erfc_inv(mp_t r
)
119 mp_t x
= exp(-r
* r
);
120 x
= x
.convert_to
<double>();
121 std::cout
<< x
<< " ";
122 mp_t result
= boost::math::erfc_inv(x
);
123 std::cout
<< result
<< std::endl
;
124 return boost::math::make_tuple(x
, result
);
128 int main(int argc
, char*argv
[])
130 parameter_info
<mp_t
> arg1
;
131 test_data
<mp_t
> data
;
138 if(strcmp(argv
[1], "--limits") == 0)
140 asymptotic_limit(24);
141 asymptotic_limit(53);
142 asymptotic_limit(64);
143 asymptotic_limit(106);
144 asymptotic_limit(113);
147 else if(strcmp(argv
[1], "--erf_inv") == 0)
150 f
= boost::math::erf_inv
;
151 std::cout
<< "Welcome.\n"
152 "This program will generate spot tests for the inverse erf function:\n";
153 std::cout
<< "Enter the number of data points: ";
156 data
.insert(f
, make_random_param(mp_t(-1), mp_t(1), points
));
158 else if(strcmp(argv
[1], "--erfc_inv") == 0)
160 boost::math::tuple
<mp_t
, mp_t
> (*f
)(mp_t
);
162 std::cout
<< "Welcome.\n"
163 "This program will generate spot tests for the inverse erfc function:\n";
164 std::cout
<< "Enter the maximum *result* expected from erfc_inv: ";
167 std::cout
<< "Enter the number of data points: ";
170 parameter_info
<mp_t
> arg
= make_random_param(mp_t(0), mp_t(max_val
), points
);
171 arg
.type
|= dummy_param
;
177 std::cout
<< "Welcome.\n"
178 "This program will generate spot tests for the erf and erfc functions:\n"
179 " erf(z) and erfc(z)\n\n";
182 if(0 == get_user_parameter_info(arg1
, "a"))
184 data
.insert(erf_data_generator(), arg1
);
186 std::cout
<< "Any more data [y/n]?";
187 std::getline(std::cin
, line
);
188 boost::algorithm::trim(line
);
189 cont
= (line
== "y");
193 std::cout
<< "Enter name of test data file [default=erf_data.ipp]";
194 std::getline(std::cin
, line
);
195 boost::algorithm::trim(line
);
197 line
= "erf_data.ipp";
198 std::ofstream
ofs(line
.c_str());
199 ofs
<< std::scientific
<< std::setprecision(40);
200 write_code(ofs
, data
, "erf_data");
205 /* Output for asymptotic limits:
207 Erf asymptotic limit for 24 bit numbers is 2.8 after approximately 6 terms.
208 Erfc asymptotic limit for 24 bit numbers is 4.12064 after approximately 17 terms.
209 Erf asymptotic limit for 53 bit numbers is 4.3 after approximately 11 terms.
210 Erfc asymptotic limit for 53 bit numbers is 6.19035 after approximately 29 terms.
211 Erf asymptotic limit for 64 bit numbers is 4.8 after approximately 12 terms.
212 Erfc asymptotic limit for 64 bit numbers is 7.06004 after approximately 29 terms.
213 Erf asymptotic limit for 106 bit numbers is 6.5 after approximately 14 terms.
214 Erfc asymptotic limit for 106 bit numbers is 11.6626 after approximately 29 terms.
215 Erf asymptotic limit for 113 bit numbers is 6.8 after approximately 14 terms.
216 Erfc asymptotic limit for 113 bit numbers is 12.6802 after approximately 29 terms.