~ubuntu-branches/debian/sid/boost1.49/sid

« back to all changes in this revision

Viewing changes to libs/math/tools/erf_data.cpp

  • Committer: Package Import Robot
  • Author(s): Steve M. Robbins
  • Date: 2012-02-26 00:31:44 UTC
  • Revision ID: package-import@ubuntu.com-20120226003144-eaytp12cbf6ubpms
Tags: upstream-1.49.0
ImportĀ upstreamĀ versionĀ 1.49.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
5
 
 
6
#include <boost/math/bindings/rr.hpp>
 
7
#include <boost/test/included/test_exec_monitor.hpp>
 
8
#include <boost/math/special_functions/gamma.hpp>
 
9
#include <boost/math/special_functions/erf.hpp> // for inverses
 
10
#include <boost/math/constants/constants.hpp>
 
11
#include <boost/math/tools/test.hpp>
 
12
#include <fstream>
 
13
 
 
14
#include <boost/math/tools/test_data.hpp>
 
15
 
 
16
using namespace boost::math::tools;
 
17
using namespace std;
 
18
 
 
19
float external_f;
 
20
float force_truncate(const float* f)
 
21
{
 
22
   external_f = *f;
 
23
   return external_f;
 
24
}
 
25
 
 
26
float truncate_to_float(boost::math::ntl::RR r)
 
27
{
 
28
   float f = boost::math::tools::real_cast<float>(r);
 
29
   return force_truncate(&f);
 
30
}
 
31
 
 
32
struct erf_data_generator
 
33
{
 
34
   boost::math::tuple<boost::math::ntl::RR, boost::math::ntl::RR> operator()(boost::math::ntl::RR z)
 
35
   {
 
36
      // very naively calculate spots using the gamma function at high precision:
 
37
      int sign = 1;
 
38
      if(z < 0)
 
39
      {
 
40
         sign = -1;
 
41
         z = -z;
 
42
      }
 
43
      boost::math::ntl::RR g1, g2;
 
44
      g1 = boost::math::tgamma_lower(boost::math::ntl::RR(0.5), z * z);
 
45
      g1 /= sqrt(boost::math::constants::pi<boost::math::ntl::RR>());
 
46
      g1 *= sign;
 
47
 
 
48
      if(z < 0.5)
 
49
      {
 
50
         g2 = 1 - (sign * g1);
 
51
      }
 
52
      else
 
53
      {
 
54
         g2 = boost::math::tgamma(boost::math::ntl::RR(0.5), z * z);
 
55
         g2 /= sqrt(boost::math::constants::pi<boost::math::ntl::RR>());
 
56
      }
 
57
      if(sign < 1)
 
58
         g2 = 2 - g2;
 
59
      return boost::math::make_tuple(g1, g2);
 
60
   }
 
61
};
 
62
 
 
63
double double_factorial(int N)
 
64
{
 
65
   double result = 1;
 
66
   while(N > 2)
 
67
   {
 
68
      N -= 2;
 
69
      result *= N;
 
70
   }
 
71
   return result;
 
72
}
 
73
 
 
74
void asymptotic_limit(int Bits)
 
75
{
 
76
   //
 
77
   // The following block of code estimates how large z has
 
78
   // to be before we can use the asymptotic expansion for
 
79
   // erf/erfc and still get convergence: the series becomes
 
80
   // divergent eventually so we have to be careful!
 
81
   //
 
82
   double result = (std::numeric_limits<double>::max)();
 
83
   int terms = 0;
 
84
   for(int n = 1; n < 15; ++n)
 
85
   {
 
86
      double lim = (Bits-n) * log(2.0) - log(sqrt(3.14)) + log(double_factorial(2*n+1));
 
87
      double x = 1;
 
88
      while(x*x + (2*n+1)*log(x) <= lim)
 
89
         x += 0.1;
 
90
      if(x < result)
 
91
      {
 
92
         result = x;
 
93
         terms = n;
 
94
      }
 
95
   }
 
96
 
 
97
   std::cout << "Erf asymptotic limit for " 
 
98
      << Bits << " bit numbers is " 
 
99
      << result << " after approximately " 
 
100
      << terms << " terms." << std::endl;
 
101
 
 
102
   result = (std::numeric_limits<double>::max)();
 
103
   terms = 0;
 
104
   for(int n = 1; n < 30; ++n)
 
105
   {
 
106
      double x = pow(double_factorial(2*n+1)/pow(2.0, n-Bits), 1 / (2.0*n));
 
107
      if(x < result)
 
108
      {
 
109
         result = x;
 
110
         terms = n;
 
111
      }
 
112
   }
 
113
 
 
114
   std::cout << "Erfc asymptotic limit for " 
 
115
      << Bits << " bit numbers is " 
 
116
      << result << " after approximately " 
 
117
      << terms << " terms." << std::endl;
 
118
}
 
119
 
 
120
boost::math::tuple<boost::math::ntl::RR, boost::math::ntl::RR> erfc_inv(boost::math::ntl::RR r)
 
121
{
 
122
   boost::math::ntl::RR x = exp(-r * r);
 
123
   x = NTL::RoundToPrecision(x.value(), 64);
 
124
   std::cout << x << "   ";
 
125
   boost::math::ntl::RR result = boost::math::erfc_inv(x);
 
126
   std::cout << result << std::endl;
 
127
   return boost::math::make_tuple(x, result);
 
128
}
 
129
 
 
130
 
 
131
int test_main(int argc, char*argv [])
 
132
{
 
133
   boost::math::ntl::RR::SetPrecision(1000);
 
134
   boost::math::ntl::RR::SetOutputPrecision(40);
 
135
 
 
136
   parameter_info<boost::math::ntl::RR> arg1;
 
137
   test_data<boost::math::ntl::RR> data;
 
138
 
 
139
   bool cont;
 
140
   std::string line;
 
141
 
 
142
   if(argc >= 2)
 
143
   {
 
144
      if(strcmp(argv[1], "--limits") == 0)
 
145
      {
 
146
         asymptotic_limit(24);
 
147
         asymptotic_limit(53);
 
148
         asymptotic_limit(64);
 
149
         asymptotic_limit(106);
 
150
         asymptotic_limit(113);
 
151
         return 0;
 
152
      }
 
153
      else if(strcmp(argv[1], "--erf_inv") == 0)
 
154
      {
 
155
         boost::math::ntl::RR (*f)(boost::math::ntl::RR);
 
156
         f = boost::math::erf_inv;
 
157
         std::cout << "Welcome.\n"
 
158
            "This program will generate spot tests for the inverse erf function:\n";
 
159
         std::cout << "Enter the number of data points: ";
 
160
         int points;
 
161
         std::cin >> points;
 
162
         data.insert(f, make_random_param(boost::math::ntl::RR(-1), boost::math::ntl::RR(1), points));
 
163
      }
 
164
      else if(strcmp(argv[1], "--erfc_inv") == 0)
 
165
      {
 
166
         boost::math::tuple<boost::math::ntl::RR, boost::math::ntl::RR> (*f)(boost::math::ntl::RR);
 
167
         f = erfc_inv;
 
168
         std::cout << "Welcome.\n"
 
169
            "This program will generate spot tests for the inverse erfc function:\n";
 
170
         std::cout << "Enter the maximum *result* expected from erfc_inv: ";
 
171
         double max_val;
 
172
         std::cin >> max_val;
 
173
         std::cout << "Enter the number of data points: ";
 
174
         int points;
 
175
         std::cin >> points;
 
176
         parameter_info<boost::math::ntl::RR> arg = make_random_param(boost::math::ntl::RR(0), boost::math::ntl::RR(max_val), points);
 
177
         arg.type |= dummy_param;
 
178
         data.insert(f, arg);
 
179
      }
 
180
   }
 
181
   else
 
182
   {
 
183
      std::cout << "Welcome.\n"
 
184
         "This program will generate spot tests for the erf and erfc functions:\n"
 
185
         "  erf(z) and erfc(z)\n\n";
 
186
 
 
187
      do{
 
188
         if(0 == get_user_parameter_info(arg1, "a"))
 
189
            return 1;
 
190
         data.insert(erf_data_generator(), arg1);
 
191
 
 
192
         std::cout << "Any more data [y/n]?";
 
193
         std::getline(std::cin, line);
 
194
         boost::algorithm::trim(line);
 
195
         cont = (line == "y");
 
196
      }while(cont);
 
197
   }
 
198
 
 
199
   std::cout << "Enter name of test data file [default=erf_data.ipp]";
 
200
   std::getline(std::cin, line);
 
201
   boost::algorithm::trim(line);
 
202
   if(line == "")
 
203
      line = "erf_data.ipp";
 
204
   std::ofstream ofs(line.c_str());
 
205
   write_code(ofs, data, "erf_data");
 
206
   
 
207
   return 0;
 
208
}
 
209
 
 
210
/* Output for asymptotic limits:
 
211
 
 
212
Erf asymptotic limit for 24 bit numbers is 2.8 after approximately 6 terms.
 
213
Erfc asymptotic limit for 24 bit numbers is 4.12064 after approximately 17 terms.
 
214
Erf asymptotic limit for 53 bit numbers is 4.3 after approximately 11 terms.
 
215
Erfc asymptotic limit for 53 bit numbers is 6.19035 after approximately 29 terms.
 
216
Erf asymptotic limit for 64 bit numbers is 4.8 after approximately 12 terms.
 
217
Erfc asymptotic limit for 64 bit numbers is 7.06004 after approximately 29 terms.
 
218
Erf asymptotic limit for 106 bit numbers is 6.5 after approximately 14 terms.
 
219
Erfc asymptotic limit for 106 bit numbers is 11.6626 after approximately 29 terms.
 
220
Erf asymptotic limit for 113 bit numbers is 6.8 after approximately 14 terms.
 
221
Erfc asymptotic limit for 113 bit numbers is 12.6802 after approximately 29 terms.
 
222
*/
 
223