1 /**************************************************************************
2 *
3 * (C) Copyright VMware, Inc 2010.
4 * (C) Copyright John Maddock 2006.
5 * Use, modification and distribution are subject to the
6 * Boost Software License, Version 1.0. (See accompanying file
7 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 *
9 **************************************************************************/
10
11
12 /*
13 * This file allows to compute the minimax polynomial coefficients we use
14 * for fast exp2/log2.
15 *
16 * How to use this source:
17 *
18 * - Download and build the NTL library from
19 * http://shoup.net/ntl/download.html , or install libntl-dev package if on
20 * Debian.
21 *
22 * - Download boost source code matching to your distro.
23 *
24 * - Goto libs/math/minimax and replace f.cpp with this file.
25 *
26 * - Build as
27 *
28 * g++ -o minimax -I /path/to/ntl/include main.cpp f.cpp /path/to/ntl/src/ntl.a
29 *
30 * - Run as
31 *
32 * ./minimax
33 *
34 * - For example, to compute exp2 5th order polynomial between [0, 1] do:
35 *
36 * variant 0
37 * range 0 1
38 * order 5 0
39 * step 200
40 * info
41 *
42 * and take the coefficients from the P = { ... } array.
43 *
44 * - To compute log2 4th order polynomial between [0, 1/9] do:
45 *
46 * variant 1
47 * range 0 0.111111112
48 * order 4 0
49 * step 200
50 * info
51 *
52 * - For more info see
53 * http://www.boost.org/doc/libs/1_47_0/libs/math/doc/sf_and_dist/html/math_toolkit/toolkit/internals2/minimax.html
54 */
55
56 #define L22
57 #include <boost/math/bindings/rr.hpp>
58 #include <boost/math/tools/polynomial.hpp>
59
60 #include <cmath>
61
exp2(const boost::math::ntl::RR & x)62 boost::math::ntl::RR exp2(const boost::math::ntl::RR& x)
63 {
64 return exp(x*log(2.0));
65 }
66
log2(const boost::math::ntl::RR & x)67 boost::math::ntl::RR log2(const boost::math::ntl::RR& x)
68 {
69 return log(x)/log(2.0);
70 }
71
f(const boost::math::ntl::RR & x,int variant)72 boost::math::ntl::RR f(const boost::math::ntl::RR& x, int variant)
73 {
74 switch(variant)
75 {
76 case 0:
77 return exp2(x);
78
79 case 1:
80 return log2((1.0 + sqrt(x))/(1.0 - sqrt(x)))/sqrt(x);
81 }
82
83 return 0;
84 }
85
86
show_extra(const boost::math::tools::polynomial<boost::math::ntl::RR> & n,const boost::math::tools::polynomial<boost::math::ntl::RR> & d,const boost::math::ntl::RR & x_offset,const boost::math::ntl::RR & y_offset,int variant)87 void show_extra(
88 const boost::math::tools::polynomial<boost::math::ntl::RR>& n,
89 const boost::math::tools::polynomial<boost::math::ntl::RR>& d,
90 const boost::math::ntl::RR& x_offset,
91 const boost::math::ntl::RR& y_offset,
92 int variant)
93 {
94 switch(variant)
95 {
96 default:
97 // do nothing here...
98 ;
99 }
100 }
101
102