1 //  Copyright John Maddock 2006
2 //  Copyright Paul A. Bristow 2007
3 //  Use, modification and distribution are subject to the
4 //  Boost Software License, Version 1.0. (See accompanying file
5 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 
7 #include <pch.hpp>
8 
9 #ifdef _MSC_VER
10 #  pragma warning(disable : 4267) // conversion from 'size_t' to 'const unsigned int', possible loss of data
11 #  pragma warning(disable : 4180) // qualifier applied to function type has no meaning; ignored
12 #  pragma warning(disable : 4224) // nonstandard extension used : formal parameter 'function_ptr' was previously defined as a type
13 #  pragma warning(disable : 4100) // unreferenced formal parameter (in ublas/functional)
14 #endif
15 
16 #include <boost/math/tools/remez.hpp>
17 #define BOOST_TEST_MAIN
18 #include <boost/test/unit_test.hpp>
19 #include <boost/test/floating_point_comparison.hpp>
20 #include <boost/math/special_functions/expm1.hpp>
21 #include <iostream>
22 #include <iomanip>
23 
24 
test_polynomial()25 void test_polynomial()
26 {
27 #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
28    double (*f)(double) = boost::math::expm1<double>;
29 #else
30    double (*f)(double) = boost::math::expm1;
31 #endif
32    std::cout << "Testing expm1 approximation, pinned to origin, abolute error, 6 term polynomial\n";
33    boost::math::tools::remez_minimax<double> approx1(f, 6, 0, -1, 1, true, false);
34    std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
35    for(unsigned i = 0; i < 7; ++i)
36    {
37       approx1.iterate();
38       std::cout << approx1.error_term() << " " << approx1.max_error() << " " << approx1.max_change() << std::endl;
39    }
40    std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
41    std::cout << "Testing expm1 approximation, pinned to origin, relative error, 6 term polynomial\n";
42    boost::math::tools::remez_minimax<double> approx2(f, 6, 0, -1, 1, true, true);
43    std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
44    for(unsigned i = 0; i < 7; ++i)
45    {
46       approx2.iterate();
47       std::cout << approx2.error_term() << " " << approx2.max_error() << " " << approx2.max_change() << std::endl;
48    }
49    std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
50 
51    f = std::exp;
52    std::cout << "Testing exp approximation, not pinned to origin, abolute error, 6 term polynomial\n";
53    boost::math::tools::remez_minimax<double> approx3(f, 6, 0, -1, 1, false, false);
54    std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
55    for(unsigned i = 0; i < 7; ++i)
56    {
57       approx3.iterate();
58       std::cout << approx3.error_term() << " " << approx3.max_error() << " " << approx3.max_change() << std::endl;
59    }
60    std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
61    std::cout << "Testing exp approximation, not pinned to origin, relative error, 6 term polynomial\n";
62    boost::math::tools::remez_minimax<double> approx4(f, 6, 0, -1, 1, false, true);
63    std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
64    for(unsigned i = 0; i < 7; ++i)
65    {
66       approx4.iterate();
67       std::cout << approx4.error_term() << " " << approx4.max_error() << " " << approx4.max_change() << std::endl;
68    }
69    std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
70 
71    f = std::cos;
72    std::cout << "Testing cos approximation, not pinned to origin, abolute error, 5 term polynomial\n";
73    boost::math::tools::remez_minimax<double> approx5(f, 5, 0, -1, 1, false, false);
74    std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
75    for(unsigned i = 0; i < 7; ++i)
76    {
77       approx5.iterate();
78       std::cout << approx5.error_term() << " " << approx5.max_error() << " " << approx5.max_change() << std::endl;
79    }
80    std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
81    std::cout << "Testing cos approximation, not pinned to origin, relative error, 5 term polynomial\n";
82    boost::math::tools::remez_minimax<double> approx6(f, 5, 0, -1, 1, false, true);
83    for(unsigned i = 0; i < 7; ++i)
84    {
85       approx6.iterate();
86       std::cout << approx6.error_term() << " " << approx6.max_error() << " " << approx6.max_change() << std::endl;
87    }
88    std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
89 
90    f = std::sin;
91    std::cout << "Testing sin approximation, pinned to origin, abolute error, 4 term polynomial\n";
92    boost::math::tools::remez_minimax<double> approx7(f, 4, 0, 0, 1, true, false);
93    for(unsigned i = 0; i < 7; ++i)
94    {
95       approx7.iterate();
96       std::cout << approx7.error_term() << " " << approx7.max_error() << " " << approx7.max_change() << std::endl;
97    }
98    std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
99    std::cout << "Testing sin approximation, pinned to origin, relative error, 4 term polynomial\n";
100    boost::math::tools::remez_minimax<double> approx8(f, 4, 0, 0, 1, true, true);
101    for(unsigned i = 0; i < 7; ++i)
102    {
103       approx8.iterate();
104       std::cout << approx8.error_term() << " " << approx8.max_error() << " " << approx8.max_change() << std::endl;
105    }
106    std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
107 }
108 
test_rational()109 void test_rational()
110 {
111 #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
112    double (*f)(double) = boost::math::expm1<double>;
113 #else
114    double (*f)(double) = boost::math::expm1;
115 #endif
116    std::cout << "Testing expm1 approximation, pinned to origin, abolute error, 3+3 term rational\n";
117    boost::math::tools::remez_minimax<double> approx1(f, 3, 3, -1, 1, true, false);
118    std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
119    for(unsigned i = 0; i < 7; ++i)
120    {
121       approx1.iterate();
122       std::cout << approx1.error_term() << " " << approx1.max_error() << " " << approx1.max_change() << std::endl;
123    }
124    std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
125 #if 0
126    //
127    // This one causes UBLAS to fail on some systems, so disabled for now.
128    //
129    std::cout << "Testing expm1 approximation, pinned to origin, relative error, 3+3 term rational\n";
130    boost::math::tools::remez_minimax<double> approx2(f, 3, 3, -1, 1, true, true);
131    std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
132    for(unsigned i = 0; i < 7; ++i)
133    {
134       approx2.iterate();
135       std::cout << approx2.error_term() << " " << approx2.max_error() << " " << approx2.max_change() << std::endl;
136    }
137    std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
138 #endif
139    f = std::exp;
140    std::cout << "Testing exp approximation, not pinned to origin, abolute error, 3+3 term rational\n";
141    boost::math::tools::remez_minimax<double> approx3(f, 3, 3, -1, 1, false, false);
142    std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
143    for(unsigned i = 0; i < 7; ++i)
144    {
145       approx3.iterate();
146       std::cout << approx3.error_term() << " " << approx3.max_error() << " " << approx3.max_change() << std::endl;
147    }
148    std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
149    std::cout << "Testing exp approximation, not pinned to origin, relative error, 3+3 term rational\n";
150    boost::math::tools::remez_minimax<double> approx4(f, 3, 3, -1, 1, false, true);
151    std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
152    for(unsigned i = 0; i < 7; ++i)
153    {
154       approx4.iterate();
155       std::cout << approx4.error_term() << " " << approx4.max_error() << " " << approx4.max_change() << std::endl;
156    }
157    std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
158 
159    f = std::cos;
160    std::cout << "Testing cos approximation, not pinned to origin, abolute error, 2+2 term rational\n";
161    boost::math::tools::remez_minimax<double> approx5(f, 2, 2, 0, 1, false, false);
162    std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
163    for(unsigned i = 0; i < 7; ++i)
164    {
165       approx5.iterate();
166       std::cout << approx5.error_term() << " " << approx5.max_error() << " " << approx5.max_change() << std::endl;
167    }
168    std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
169    std::cout << "Testing cos approximation, not pinned to origin, relative error, 2+2 term rational\n";
170    boost::math::tools::remez_minimax<double> approx6(f, 2, 2, 0, 1, false, true);
171    std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
172    for(unsigned i = 0; i < 7; ++i)
173    {
174       approx6.iterate();
175       std::cout << approx6.error_term() << " " << approx6.max_error() << " " << approx6.max_change() << std::endl;
176    }
177    std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
178 }
179 
BOOST_AUTO_TEST_CASE(test_main)180 BOOST_AUTO_TEST_CASE( test_main )
181 {
182    test_polynomial();
183    test_rational();
184 
185 }
186 
187 
188