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