mirror of
https://github.com/boostorg/math.git
synced 2026-01-19 04:22:09 +00:00
Hypergeometric: start testing larger arguments to 1F1 and begin to fix the errors.
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2017 John Maddock
|
||||
// Distributed under 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)
|
||||
//
|
||||
#ifndef BOOST_MATH_HYPERGEOMETRIC_1F1_SCALED_SERIES_HPP
|
||||
#define BOOST_MATH_HYPERGEOMETRIC_1F1_SCALED_SERIES_HPP
|
||||
|
||||
#include <boost/array.hpp>
|
||||
|
||||
namespace boost{ namespace math{ namespace detail{
|
||||
|
||||
template <class T, class Policy>
|
||||
T hypergeometric_1f1_scaled_series(const T& a, const T& b, const T& z, const Policy& pol, const char* function)
|
||||
{
|
||||
BOOST_MATH_STD_USING_CORE
|
||||
//
|
||||
// Result is returned scaled by e^-z.
|
||||
// Whenever the terms start becoming too large, we scale by some factor e^-n
|
||||
// and keep track of the integer scaling factor n. At the end we can perform
|
||||
// an exact subtraction of n from z and scale the result:
|
||||
//
|
||||
T sum(0), term(1), upper_limit(sqrt(boost::math::tools::max_value<T>())), diff;
|
||||
unsigned n = 0;
|
||||
boost::intmax_t log_scaling_factor = -boost::math::itrunc(boost::math::tools::log_max_value<T>());
|
||||
T scaling_factor = exp(log_scaling_factor);
|
||||
boost::intmax_t current_scaling = 0;
|
||||
|
||||
do
|
||||
{
|
||||
sum += term;
|
||||
if (sum >= upper_limit)
|
||||
{
|
||||
sum *= scaling_factor;
|
||||
term *= scaling_factor;
|
||||
current_scaling += log_scaling_factor;
|
||||
}
|
||||
term *= (((a + n) / ((b + n) * (n + 1))) * z);
|
||||
if (n > boost::math::policies::get_max_series_iterations<Policy>())
|
||||
return boost::math::policies::raise_evaluation_error(function, "Series did not converge, best value is %1%", sum, pol);
|
||||
++n;
|
||||
diff = fabs(term / sum);
|
||||
} while (diff > boost::math::policies::get_epsilon<T, Policy>());
|
||||
|
||||
return sum * exp(-z - current_scaling);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} } } // namespaces
|
||||
|
||||
#endif // BOOST_MATH_HYPERGEOMETRIC_1F1_SCALED_SERIES_HPP
|
||||
2412
test/hypergeometric_1f1_big.ipp
Normal file
2412
test/hypergeometric_1f1_big.ipp
Normal file
File diff suppressed because it is too large
Load Diff
160
tools/hyp_1f1_big_data.cpp
Normal file
160
tools/hyp_1f1_big_data.cpp
Normal file
@@ -0,0 +1,160 @@
|
||||
// (C) Copyright John Maddock 2006.
|
||||
// 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_MATH_MAX_SERIES_ITERATION_POLICY 10000000
|
||||
|
||||
#include <boost/math/special_functions/hypergeometric_1f1.hpp>
|
||||
#include <boost/math/constants/constants.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
#include <boost/math/tools/test_data.hpp>
|
||||
#include <boost/random.hpp>
|
||||
#define BOOST_MATH_USE_MPFR
|
||||
#include "mp_t.hpp"
|
||||
|
||||
#include <boost/multiprecision/mpfi.hpp>
|
||||
|
||||
using namespace boost::math::tools;
|
||||
using namespace boost::math;
|
||||
using namespace std;
|
||||
using namespace boost::multiprecision;
|
||||
|
||||
typedef mpfi_float_1000 mpfi_type;
|
||||
|
||||
mp_t hypergeometric_1f1_generic_series(mp_t a_, mp_t b_, mp_t z_)
|
||||
{
|
||||
mpfi_type a(a_), b(b_), z(z_), sum(0), term(1), diff;
|
||||
unsigned n = 0;
|
||||
|
||||
do
|
||||
{
|
||||
sum += term;
|
||||
term *= (((a + n) / ((b + n) * (n + 1))) * z);
|
||||
++n;
|
||||
diff = fabs(term / sum);
|
||||
if (n > 10000000)
|
||||
{
|
||||
std::cout << "Aborting series evaluation due to too many iterations...\n";
|
||||
throw evaluation_error("");
|
||||
}
|
||||
if (fabs(upper(sum)) > std::numeric_limits<double>::max())
|
||||
{
|
||||
std::cout << "Aborting series evaluation due to over large sum...\n";
|
||||
throw evaluation_error("");
|
||||
}
|
||||
} while (upper(diff) > 1e-40);
|
||||
|
||||
mp_t r = mp_t(width(sum) / median(sum));
|
||||
if (fabs(r) > 1e-40)
|
||||
{
|
||||
std::cout << "Aborting to to error in result of " << r << std::endl;
|
||||
throw evaluation_error("");
|
||||
}
|
||||
std::cout << "Found error in sum was " << r << std::endl;
|
||||
|
||||
return mp_t(median(sum));
|
||||
}
|
||||
|
||||
|
||||
struct hypergeometric_1f1_gen
|
||||
{
|
||||
mp_t operator()(mp_t a1, mp_t a2, mp_t z)
|
||||
{
|
||||
mp_t result;
|
||||
try {
|
||||
result = hypergeometric_1f1_generic_series(a1, a2, z);
|
||||
std::cout << a1 << " " << a2 << " " << z << " " << result << std::endl;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
throw std::domain_error("");
|
||||
}
|
||||
if (fabs(result) > std::numeric_limits<double>::max())
|
||||
{
|
||||
std::cout << "Rejecting over large value\n";
|
||||
throw std::domain_error("");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main(int, char* [])
|
||||
{
|
||||
parameter_info<mp_t> arg1, arg2, arg3;
|
||||
test_data<mp_t> data;
|
||||
|
||||
std::cout << "Welcome.\n"
|
||||
"This program will generate spot tests for 1F1 (Yeh!!):\n";
|
||||
|
||||
std::string line;
|
||||
bool cont;
|
||||
|
||||
std::vector<mp_t> v;
|
||||
random_ns::mt19937 rnd;
|
||||
random_ns::uniform_real_distribution<float> ur_a(0, 1);
|
||||
|
||||
mp_t p = ur_a(rnd);
|
||||
p *= 1e6;
|
||||
v.push_back(p);
|
||||
v.push_back(-p);
|
||||
p = ur_a(rnd);
|
||||
p *= 1e5;
|
||||
v.push_back(p);
|
||||
v.push_back(-p);
|
||||
p = ur_a(rnd);
|
||||
p *= 1e4;
|
||||
v.push_back(p);
|
||||
v.push_back(-p);
|
||||
p = ur_a(rnd);
|
||||
p *= 1e3;
|
||||
v.push_back(p);
|
||||
v.push_back(-p);
|
||||
p = ur_a(rnd);
|
||||
p *= 1e2;
|
||||
v.push_back(p);
|
||||
v.push_back(-p);
|
||||
p = ur_a(rnd);
|
||||
p *= 1e-5;
|
||||
v.push_back(p);
|
||||
v.push_back(-p);
|
||||
p = ur_a(rnd);
|
||||
p *= 1e-12;
|
||||
v.push_back(p);
|
||||
v.push_back(-p);
|
||||
p = ur_a(rnd);
|
||||
p *= 1e-30;
|
||||
v.push_back(p);
|
||||
v.push_back(-p);
|
||||
|
||||
for (unsigned i = 0; i < v.size(); ++i)
|
||||
{
|
||||
for (unsigned j = 0; j < v.size(); ++j)
|
||||
{
|
||||
for (unsigned k = 0; k < v.size(); ++k)
|
||||
{
|
||||
arg1 = make_single_param(v[i]);
|
||||
arg2 = make_single_param(mp_t(v[j] * 3 / 2));
|
||||
arg3 = make_single_param(mp_t(v[k] * 5 / 4));
|
||||
data.insert(hypergeometric_1f1_gen(), arg1, arg2, arg3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::cout << "Enter name of test data file [default=hypergeometric_1f1.ipp]";
|
||||
std::getline(std::cin, line);
|
||||
boost::algorithm::trim(line);
|
||||
if(line == "")
|
||||
line = "hypergeometric_1f1.ipp";
|
||||
std::ofstream ofs(line.c_str());
|
||||
ofs << std::scientific << std::setprecision(40);
|
||||
write_code(ofs, data, line.c_str());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user