1848b8605Smrg/**************************************************************************
2848b8605Smrg *
3848b8605Smrg * (C) Copyright VMware, Inc 2010.
4848b8605Smrg * (C) Copyright John Maddock 2006.
5848b8605Smrg * Use, modification and distribution are subject to the
6848b8605Smrg * Boost Software License, Version 1.0. (See accompanying file
7848b8605Smrg * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8848b8605Smrg *
9848b8605Smrg **************************************************************************/
10848b8605Smrg
11848b8605Smrg
12848b8605Smrg/*
13848b8605Smrg * This file allows to compute the minimax polynomial coefficients we use
14848b8605Smrg * for fast exp2/log2.
15848b8605Smrg *
16848b8605Smrg * How to use this source:
17848b8605Smrg *
18848b8605Smrg * - Download and build the NTL library from
19848b8605Smrg *   http://shoup.net/ntl/download.html , or install libntl-dev package if on
20848b8605Smrg *   Debian.
21848b8605Smrg *
22848b8605Smrg * - Download boost source code matching to your distro.
23848b8605Smrg *
24848b8605Smrg * - Goto libs/math/minimax and replace f.cpp with this file.
25848b8605Smrg *
26848b8605Smrg * - Build as
27848b8605Smrg *
28848b8605Smrg *   g++ -o minimax -I /path/to/ntl/include main.cpp f.cpp /path/to/ntl/src/ntl.a
29848b8605Smrg *
30848b8605Smrg * - Run as
31848b8605Smrg *
32848b8605Smrg *    ./minimax
33848b8605Smrg *
34848b8605Smrg * - For example, to compute exp2 5th order polynomial between [0, 1] do:
35848b8605Smrg *
36848b8605Smrg *    variant 0
37848b8605Smrg *    range 0 1
38848b8605Smrg *    order 5 0
39848b8605Smrg *    step 200
40848b8605Smrg *    info
41848b8605Smrg *
42848b8605Smrg *  and take the coefficients from the P = { ... } array.
43848b8605Smrg *
44848b8605Smrg * - To compute log2 4th order polynomial between [0, 1/9] do:
45848b8605Smrg *
46848b8605Smrg *    variant 1
47848b8605Smrg *    range 0 0.111111112
48848b8605Smrg *    order 4 0
49848b8605Smrg *    step 200
50848b8605Smrg *    info
51848b8605Smrg *
52848b8605Smrg * - For more info see
53848b8605Smrg * http://www.boost.org/doc/libs/1_47_0/libs/math/doc/sf_and_dist/html/math_toolkit/toolkit/internals2/minimax.html
54848b8605Smrg */
55848b8605Smrg
56848b8605Smrg#define L22
57848b8605Smrg#include <boost/math/bindings/rr.hpp>
58848b8605Smrg#include <boost/math/tools/polynomial.hpp>
59848b8605Smrg
60848b8605Smrg#include <cmath>
61848b8605Smrg
62848b8605Smrgboost::math::ntl::RR exp2(const boost::math::ntl::RR& x)
63848b8605Smrg{
64848b8605Smrg      return exp(x*log(2.0));
65848b8605Smrg}
66848b8605Smrg
67848b8605Smrgboost::math::ntl::RR log2(const boost::math::ntl::RR& x)
68848b8605Smrg{
69848b8605Smrg      return log(x)/log(2.0);
70848b8605Smrg}
71848b8605Smrg
72848b8605Smrgboost::math::ntl::RR f(const boost::math::ntl::RR& x, int variant)
73848b8605Smrg{
74848b8605Smrg   switch(variant)
75848b8605Smrg   {
76848b8605Smrg   case 0:
77848b8605Smrg      return exp2(x);
78848b8605Smrg
79848b8605Smrg   case 1:
80848b8605Smrg      return log2((1.0 + sqrt(x))/(1.0 - sqrt(x)))/sqrt(x);
81848b8605Smrg   }
82848b8605Smrg
83848b8605Smrg   return 0;
84848b8605Smrg}
85848b8605Smrg
86848b8605Smrg
87848b8605Smrgvoid show_extra(
88848b8605Smrg   const boost::math::tools::polynomial<boost::math::ntl::RR>& n,
89848b8605Smrg   const boost::math::tools::polynomial<boost::math::ntl::RR>& d,
90848b8605Smrg   const boost::math::ntl::RR& x_offset,
91848b8605Smrg   const boost::math::ntl::RR& y_offset,
92848b8605Smrg   int variant)
93848b8605Smrg{
94848b8605Smrg   switch(variant)
95848b8605Smrg   {
96848b8605Smrg   default:
97848b8605Smrg      // do nothing here...
98848b8605Smrg      ;
99848b8605Smrg   }
100848b8605Smrg}
101848b8605Smrg
102