File: plot_1d_errors.cpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (411 lines) | stat: -rw-r--r-- 14,568 bytes parent folder | download | duplicates (2)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//  Copyright John Maddock 2018.
//  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)

#include <map>
#include <boost/config.hpp>
#include <boost/multiprecision/cpp_bin_float.hpp>
#ifdef BOOST_HAS_FLOAT128
#include <boost/multiprecision/float128.hpp>
#endif
#include <boost/svg_plot/svg_2d_plot.hpp>

template <class Real>
Real interval_from_range(Real x)
{
   BOOST_MATH_STD_USING
   Real l = floor(log10(x));
   l = pow(10, l);
   if (x / l < 2)
      l /= 10;
   return l;
}


std::string normalise_filename(std::string name)
{
   for(std::string::size_type i = 0; i < name.size(); ++i)
   {
      if (!std::isalnum(name[i]))
         name[i] = '_';
      else
         name[i] = std::tolower(name[i]);
   }
   return name;
}

template <class F, class Real>
void plot_errors_1d(F f, Real start, Real end, unsigned points, const char* function_name, Real max_y_scale = (std::numeric_limits<Real>::max)(), unsigned num_bins = 200)
{
   BOOST_MATH_STD_USING
   std::cout << "Generating points for " << function_name << std::endl;
   Real pos = start;
   Real interval = (end - start) / points;

   std::map<Real, Real> points_upper, points_lower;

   Real max_distance(0), min_distance(0), max_error(0), max_error_location(0);

   constexpr unsigned limb_bits = (sizeof(boost::multiprecision::limb_type) * CHAR_BIT);
   constexpr unsigned mp_digits = (((std::numeric_limits<Real>::digits * 2) / limb_bits + ((std::numeric_limits<Real>::digits * 2) % limb_bits ? 1 : 0))) * limb_bits;

   typedef boost::multiprecision::number<boost::multiprecision::cpp_bin_float<mp_digits, boost::multiprecision::backends::digit_base_2> > mp_type;

   while (pos <= end)
   {
      try
      {
         Real found_value = f(pos);
         Real exact_value = static_cast<Real>(f(mp_type(pos)));
         Real distance = boost::math::sign(found_value - exact_value) * boost::math::epsilon_difference(found_value, exact_value);
         Real bin = start + ((end - start) / num_bins) * boost::math::itrunc(num_bins * (pos - start) / (end - start));
         if (points_lower.find(bin) == points_lower.end())
            points_lower[bin] = 0;
         if (points_upper.find(bin) == points_upper.end())
            points_upper[bin] = 0;
         if (distance > 0)
         {
            if (points_upper[bin] < distance)
               points_upper[bin] = (std::min)(distance, max_y_scale);
         }
         else
         {
            if (points_lower[bin] > distance)
               points_lower[bin] = (std::max)(distance, -max_y_scale);
         }
         if (max_distance < distance)
            max_distance = (std::min)(distance, max_y_scale);
         if (min_distance > distance)
            min_distance = (std::max)(distance, -max_y_scale);
         if (fabs(distance) > max_error)
         {
            max_error = fabs(distance);
            max_error_location = pos;
         }
         pos += interval;
      }
      catch (const std::exception& e)
      {
         std::cout << "Found exception at point " << pos << " : " << e.what() << std::endl;
         pos += interval;
      }
   }

   std::cout << "Max error was " << std::setprecision(3) << max_error << " at location " << std::setprecision(std::numeric_limits<Real>::max_digits10) << max_error_location << std::endl;

   boost::svg::svg_2d_plot plot;
   Real x_start(start), x_end(end);
   if (end - start > 3)
   {
      x_start = floor(start);
      x_end = ceil(end);
   }
   if (min_distance == 0)
      min_distance = -1;
   if (max_distance == 0)
      max_distance = 1;


   plot.title(std::string("Errors in ") + function_name).x_range((double)x_start, (double)x_end).image_x_size(700).legend_border_color(boost::svg::lightgray).plot_border_color(boost::svg::lightgray).background_border_color(boost::svg::lightgray)
      .y_range((int)floor(min_distance), (int)ceil(max_distance)).x_label("x").y_major_interval((double)interval_from_range(max_distance) * 2).x_major_interval((double)interval_from_range(end - start)).legend_on(true).plot_window_on(true).legend_on(false);
   plot.plot(points_upper).stroke_color(boost::svg::green).fill_color(boost::svg::green).size(1).line_on(true).area_fill(boost::svg::green);
   plot.plot(points_lower).stroke_color(boost::svg::green).fill_color(boost::svg::green).size(1).line_on(true).area_fill(boost::svg::green);

   plot.write(normalise_filename(function_name) + ".svg");

}

#include <boost/math/special_functions.hpp>

struct digamma_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::digamma(x);
   }
};

struct tgamma_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::tgamma(x);
   }
};

struct lgamma_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::lgamma(x);
   }
};

struct trigamma_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::tgamma(x);
   }
};

struct erf_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::erf(x);
   }
};

struct erfc_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::erfc(x);
   }
};

struct j0_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::cyl_bessel_j(0, x);
   }
};

struct j1_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::cyl_bessel_j(1, x);
   }
};

struct y0_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::cyl_neumann(0, x);
   }
};

struct y1_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::cyl_neumann(1, x);
   }
};

struct i0_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::cyl_bessel_i(0, x);
   }
};

struct i1_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::cyl_bessel_i(1, x);
   }
};

struct k0_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::cyl_bessel_k(0, x);
   }
};

struct k1_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::cyl_bessel_k(1, x);
   }
};

struct ai_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::airy_ai(x);
   }
};

struct aip_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::airy_ai_prime(x);
   }
};

struct bi_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::airy_bi(x);
   }
};

struct bip_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::airy_bi_prime(x);
   }
};

struct ellint_1_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::ellint_1(x);
   }
};

struct ellint_2_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::ellint_2(x);
   }
};

struct ellint_d_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::ellint_d(x);
   }
};

struct zeta_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::zeta(x);
   }
};

struct ei_func
{
   template <class T>
   T operator()(T x)
   {
      return boost::math::expint(x);
   }
};

int main()
{
   plot_errors_1d(digamma_func(), 1e-200, 10.0, 10000, "digamma, double");
   plot_errors_1d(tgamma_func(), 1e-200, 150.0, 10000, "tgamma, double");
   plot_errors_1d(lgamma_func(), 1e-200, 1000.0, 10000, "lgamma, double");
   plot_errors_1d(trigamma_func(), 1e-200, 10.0, 10000, "trigamma, double");
   plot_errors_1d(erf_func(), -5.0, 5.0, 10000, "erf, double");
   plot_errors_1d(erfc_func(), -5.0, 30.0, 10000, "erfc, double");
   plot_errors_1d(j0_func(), 0.0, 50.0, 10000, "j0, double", 50.0);
   plot_errors_1d(j1_func(), 0.0, 50.0, 10000, "j1, double", 50.0);
   plot_errors_1d(y0_func(), 1e-100, 50.0, 10000, "y0, double", 50.0);
   plot_errors_1d(y1_func(), 1e-100, 50.0, 10000, "y1, double", 50.0);
   plot_errors_1d(i0_func(), 0.0, 50.0, 10000, "i0, double");
   plot_errors_1d(i1_func(), 0.0, 50.0, 10000, "i1, double");
   plot_errors_1d(k0_func(), 1e-100, 50.0, 10000, "k0, double");
   plot_errors_1d(k1_func(), 1e-100, 50.0, 10000, "k1, double");
   plot_errors_1d(ai_func(), -20.0, 20.0, 10000, "Ai, double", 100.0);
   plot_errors_1d(bi_func(), -20.0, 20.0, 10000, "Bi, double", 100.0);
   plot_errors_1d(aip_func(), -20.0, 20.0, 10000, "Ai Prime, double", 100.0);
   plot_errors_1d(bip_func(), -20.0, 20.0, 10000, "Bi Prime, double", 100.0);

   plot_errors_1d(ellint_1_func(), -1.0, 1.0, 10000, "Elliptic Integral K, double");
   plot_errors_1d(ellint_2_func(), -1.0, 1.0, 10000, "Elliptic Integral E, double");
   plot_errors_1d(ellint_d_func(), -1.0, 1.0, 10000, "Elliptic Integral D, double");

   plot_errors_1d(zeta_func(), -20.0, 20.0, 10000, "Zeta, double");
   plot_errors_1d(ei_func(), -20.0, 20.0, 10000, "Exponential Integral Ei, double");

#if LDBL_MANT_DIG == 64
   plot_errors_1d(digamma_func(), 1e-200L, 10.0L, 10000, "digamma, 80-bit long double");
   plot_errors_1d(tgamma_func(), 1e-200L, 150.0L, 10000, "tgamma, 80-bit long double");
   plot_errors_1d(lgamma_func(), 1e-200L, 1000.0L, 10000, "lgamma, 80-bit long double");
   plot_errors_1d(trigamma_func(), 1e-200L, 10.0L, 10000, "trigamma, 80-bit long double");
   plot_errors_1d(erf_func(), -5.0L, 5.0L, 10000, "erf, 80-bit long double");
   plot_errors_1d(erfc_func(), -5.0L, 120.0L, 10000, "erfc, 80-bit long double");
   plot_errors_1d(j0_func(), 0.0L, 50.0L, 10000, "j0, 80 bit long double", 50.0L);
   plot_errors_1d(j1_func(), 0.0L, 50.0L, 10000, "j1, 80 bit long double", 50.0L);
   plot_errors_1d(y0_func(), 1e-100L, 50.0L, 10000, "y0, 80 bit long double", 50.0L);
   plot_errors_1d(y1_func(), 1e-100L, 50.0L, 10000, "y1, 80 bit long double", 50.0L);
   plot_errors_1d(i0_func(), 0.0L, 50.0L, 10000, "i0, 80 bit long double");
   plot_errors_1d(i1_func(), 0.0L, 50.0L, 10000, "i1, 80 bit long double");
   plot_errors_1d(k0_func(), 1e-100L, 50.0L, 10000, "k0, 80 bit long double");
   plot_errors_1d(k1_func(), 1e-100L, 50.0L, 10000, "k1, 80 bit long double");
   plot_errors_1d(ai_func(), -20.0L, 20.0L, 10000, "Ai, 80 bit long double", 100.0L);
   plot_errors_1d(bi_func(), -20.0L, 20.0L, 10000, "Bi, 80 bit long double", 100.0L);
   plot_errors_1d(aip_func(), -20.0L, 20.0L, 10000, "Ai Prime, 80 bit long double", 100.0L);
   plot_errors_1d(bip_func(), -20.0L, 20.0L, 10000, "Bi Prime, 80 bit long double", 100.0L);

   plot_errors_1d(ellint_1_func(), -1.0L, 1.0L, 10000, "Elliptic Integral K, 80 bit long double");
   plot_errors_1d(ellint_2_func(), -1.0L, 1.0L, 10000, "Elliptic Integral E, 80 bit long double");
   plot_errors_1d(ellint_d_func(), -1.0L, 1.0L, 10000, "Elliptic Integral D, 80 bit long double");

   plot_errors_1d(zeta_func(), -20.0L, 20.0L, 10000, "Zeta, 80 bit long double");
   plot_errors_1d(ei_func(), -20.0L, 20.0L, 10000, "Exponential Integral Ei, 80 bit long double");
#endif
#ifdef BOOST_HAS_FLOAT128
   plot_errors_1d(digamma_func(), boost::multiprecision::float128(1e-200), boost::multiprecision::float128(10.0), 10000, "digamma, __float128");
   plot_errors_1d(tgamma_func(), boost::multiprecision::float128(1e-200), boost::multiprecision::float128(150.0), 10000, "tgamma, __float128");
   plot_errors_1d(lgamma_func(), boost::multiprecision::float128(1e-200), boost::multiprecision::float128(1000.0), 10000, "lgamma, __float128");
   plot_errors_1d(trigamma_func(), boost::multiprecision::float128(1e-200), boost::multiprecision::float128(10.0), 10000, "trigamma, __float128");
   plot_errors_1d(erf_func(), -boost::multiprecision::float128(5.0), boost::multiprecision::float128(5.0), 10000, "erf, __float128");
   plot_errors_1d(erfc_func(), -boost::multiprecision::float128(5.0), boost::multiprecision::float128(120.0), 10000, "erfc, __float128");
   plot_errors_1d(j0_func(), boost::multiprecision::float128(0.0), boost::multiprecision::float128(50.0), 10000, "j0, __float128", boost::multiprecision::float128(50.0));
   plot_errors_1d(j1_func(), boost::multiprecision::float128(0.0), boost::multiprecision::float128(50.0), 10000, "j1, __float128", boost::multiprecision::float128(50.0));
   plot_errors_1d(y0_func(), boost::multiprecision::float128(1e-100), boost::multiprecision::float128(50.0), 10000, "y0, __float128", boost::multiprecision::float128(50.0));
   plot_errors_1d(y1_func(), boost::multiprecision::float128(1e-100), boost::multiprecision::float128(50.0), 10000, "y1, __float128", boost::multiprecision::float128(50.0));
   plot_errors_1d(i0_func(), boost::multiprecision::float128(0.0), boost::multiprecision::float128(50.0), 10000, "i0, __float128");
   plot_errors_1d(i1_func(), boost::multiprecision::float128(0.0), boost::multiprecision::float128(50.0), 10000, "i1, __float128");
   plot_errors_1d(k0_func(), boost::multiprecision::float128(1e-100), boost::multiprecision::float128(50.0), 10000, "k0, __float128");
   plot_errors_1d(k1_func(), boost::multiprecision::float128(1e-100), boost::multiprecision::float128(50.0), 10000, "k1, __float128");
   plot_errors_1d(ai_func(), -boost::multiprecision::float128(20.0), boost::multiprecision::float128(20.0), 10000, "Ai, __float128", boost::multiprecision::float128(100.0));
   plot_errors_1d(bi_func(), -boost::multiprecision::float128(20.0), boost::multiprecision::float128(20.0), 10000, "Bi, __float128", boost::multiprecision::float128(100.0));
   plot_errors_1d(aip_func(), -boost::multiprecision::float128(20.0), boost::multiprecision::float128(20.0), 10000, "Ai Prime, __float128", boost::multiprecision::float128(100.0));
   plot_errors_1d(bip_func(), -boost::multiprecision::float128(20.0), boost::multiprecision::float128(20.0), 10000, "Bi Prime, __float128", boost::multiprecision::float128(100.0));

   plot_errors_1d(ellint_1_func(), -boost::multiprecision::float128(1.0), boost::multiprecision::float128(1.0), 10000, "Elliptic Integral K, __float128");
   plot_errors_1d(ellint_2_func(), -boost::multiprecision::float128(1.0), boost::multiprecision::float128(1.0), 10000, "Elliptic Integral E, __float128");
   plot_errors_1d(ellint_d_func(), -boost::multiprecision::float128(1.0), boost::multiprecision::float128(1.0), 10000, "Elliptic Integral D, __float128");

   plot_errors_1d(zeta_func(), -boost::multiprecision::float128(20.0), boost::multiprecision::float128(20.0), 10000, "Zeta, __float128");
   plot_errors_1d(ei_func(), -boost::multiprecision::float128(20.0), boost::multiprecision::float128(20.0), 10000, "Exponential Integral Ei, __float128");
#endif
   return 0;
}