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)
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)
16 #include <boost/math/tools/remez.hpp>
17 #define BOOST_TEST_MAIN
18 #include <boost/test/unit_test.hpp>
19 #include <boost/test/tools/floating_point_comparison.hpp>
20 #include <boost/math/special_functions/expm1.hpp>
25 void test_polynomial()
27 #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
28 double (*f)(double) = boost::math::expm1<double>;
30 double (*f)(double) = boost::math::expm1;
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)
38 std::cout << approx1.error_term() << " " << approx1.max_error() << " " << approx1.max_change() << std::endl;
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)
47 std::cout << approx2.error_term() << " " << approx2.max_error() << " " << approx2.max_change() << std::endl;
49 std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
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)
58 std::cout << approx3.error_term() << " " << approx3.max_error() << " " << approx3.max_change() << std::endl;
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)
67 std::cout << approx4.error_term() << " " << approx4.max_error() << " " << approx4.max_change() << std::endl;
69 std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
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)
78 std::cout << approx5.error_term() << " " << approx5.max_error() << " " << approx5.max_change() << std::endl;
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)
86 std::cout << approx6.error_term() << " " << approx6.max_error() << " " << approx6.max_change() << std::endl;
88 std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
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)
96 std::cout << approx7.error_term() << " " << approx7.max_error() << " " << approx7.max_change() << std::endl;
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)
104 std::cout << approx8.error_term() << " " << approx8.max_error() << " " << approx8.max_change() << std::endl;
106 std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
111 #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
112 double (*f)(double) = boost::math::expm1<double>;
114 double (*f)(double) = boost::math::expm1;
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)
122 std::cout << approx1.error_term() << " " << approx1.max_error() << " " << approx1.max_change() << std::endl;
124 std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
127 // This one causes UBLAS to fail on some systems, so disabled for now.
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)
135 std::cout << approx2.error_term() << " " << approx2.max_error() << " " << approx2.max_change() << std::endl;
137 std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
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)
146 std::cout << approx3.error_term() << " " << approx3.max_error() << " " << approx3.max_change() << std::endl;
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)
155 std::cout << approx4.error_term() << " " << approx4.max_error() << " " << approx4.max_change() << std::endl;
157 std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
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)
166 std::cout << approx5.error_term() << " " << approx5.max_error() << " " << approx5.max_change() << std::endl;
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)
175 std::cout << approx6.error_term() << " " << approx6.max_error() << " " << approx6.max_change() << std::endl;
177 std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
180 BOOST_AUTO_TEST_CASE( test_main )