123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- // Copyright John Maddock 2006
- // Copyright Paul A. Bristow 2007
- // Use, modification and distribution are subject to the
- // Boost Software License, Version 1.0. (See accompanying file
- // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- #include <pch.hpp>
- #ifdef _MSC_VER
- # pragma warning(disable : 4267) // conversion from 'size_t' to 'const unsigned int', possible loss of data
- # pragma warning(disable : 4180) // qualifier applied to function type has no meaning; ignored
- # pragma warning(disable : 4224) // nonstandard extension used : formal parameter 'function_ptr' was previously defined as a type
- # pragma warning(disable : 4100) // unreferenced formal parameter (in ublas/functional)
- #endif
- #include <boost/math/tools/remez.hpp>
- #define BOOST_TEST_MAIN
- #include <boost/test/unit_test.hpp>
- #include <boost/test/tools/floating_point_comparison.hpp>
- #include <boost/math/special_functions/expm1.hpp>
- #include <iostream>
- #include <iomanip>
- void test_polynomial()
- {
- #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
- double (*f)(double) = boost::math::expm1<double>;
- #else
- double (*f)(double) = boost::math::expm1;
- #endif
- std::cout << "Testing expm1 approximation, pinned to origin, abolute error, 6 term polynomial\n";
- boost::math::tools::remez_minimax<double> approx1(f, 6, 0, -1, 1, true, false);
- std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
- for(unsigned i = 0; i < 7; ++i)
- {
- approx1.iterate();
- std::cout << approx1.error_term() << " " << approx1.max_error() << " " << approx1.max_change() << std::endl;
- }
- std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
- std::cout << "Testing expm1 approximation, pinned to origin, relative error, 6 term polynomial\n";
- boost::math::tools::remez_minimax<double> approx2(f, 6, 0, -1, 1, true, true);
- std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
- for(unsigned i = 0; i < 7; ++i)
- {
- approx2.iterate();
- std::cout << approx2.error_term() << " " << approx2.max_error() << " " << approx2.max_change() << std::endl;
- }
- std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
- f = std::exp;
- std::cout << "Testing exp approximation, not pinned to origin, abolute error, 6 term polynomial\n";
- boost::math::tools::remez_minimax<double> approx3(f, 6, 0, -1, 1, false, false);
- std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
- for(unsigned i = 0; i < 7; ++i)
- {
- approx3.iterate();
- std::cout << approx3.error_term() << " " << approx3.max_error() << " " << approx3.max_change() << std::endl;
- }
- std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
- std::cout << "Testing exp approximation, not pinned to origin, relative error, 6 term polynomial\n";
- boost::math::tools::remez_minimax<double> approx4(f, 6, 0, -1, 1, false, true);
- std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
- for(unsigned i = 0; i < 7; ++i)
- {
- approx4.iterate();
- std::cout << approx4.error_term() << " " << approx4.max_error() << " " << approx4.max_change() << std::endl;
- }
- std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
- f = std::cos;
- std::cout << "Testing cos approximation, not pinned to origin, abolute error, 5 term polynomial\n";
- boost::math::tools::remez_minimax<double> approx5(f, 5, 0, -1, 1, false, false);
- std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
- for(unsigned i = 0; i < 7; ++i)
- {
- approx5.iterate();
- std::cout << approx5.error_term() << " " << approx5.max_error() << " " << approx5.max_change() << std::endl;
- }
- std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
- std::cout << "Testing cos approximation, not pinned to origin, relative error, 5 term polynomial\n";
- boost::math::tools::remez_minimax<double> approx6(f, 5, 0, -1, 1, false, true);
- for(unsigned i = 0; i < 7; ++i)
- {
- approx6.iterate();
- std::cout << approx6.error_term() << " " << approx6.max_error() << " " << approx6.max_change() << std::endl;
- }
- std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
- f = std::sin;
- std::cout << "Testing sin approximation, pinned to origin, abolute error, 4 term polynomial\n";
- boost::math::tools::remez_minimax<double> approx7(f, 4, 0, 0, 1, true, false);
- for(unsigned i = 0; i < 7; ++i)
- {
- approx7.iterate();
- std::cout << approx7.error_term() << " " << approx7.max_error() << " " << approx7.max_change() << std::endl;
- }
- std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
- std::cout << "Testing sin approximation, pinned to origin, relative error, 4 term polynomial\n";
- boost::math::tools::remez_minimax<double> approx8(f, 4, 0, 0, 1, true, true);
- for(unsigned i = 0; i < 7; ++i)
- {
- approx8.iterate();
- std::cout << approx8.error_term() << " " << approx8.max_error() << " " << approx8.max_change() << std::endl;
- }
- std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
- }
- void test_rational()
- {
- #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
- double (*f)(double) = boost::math::expm1<double>;
- #else
- double (*f)(double) = boost::math::expm1;
- #endif
- std::cout << "Testing expm1 approximation, pinned to origin, abolute error, 3+3 term rational\n";
- boost::math::tools::remez_minimax<double> approx1(f, 3, 3, -1, 1, true, false);
- std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
- for(unsigned i = 0; i < 7; ++i)
- {
- approx1.iterate();
- std::cout << approx1.error_term() << " " << approx1.max_error() << " " << approx1.max_change() << std::endl;
- }
- std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
- #if 0
- //
- // This one causes UBLAS to fail on some systems, so disabled for now.
- //
- std::cout << "Testing expm1 approximation, pinned to origin, relative error, 3+3 term rational\n";
- boost::math::tools::remez_minimax<double> approx2(f, 3, 3, -1, 1, true, true);
- std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
- for(unsigned i = 0; i < 7; ++i)
- {
- approx2.iterate();
- std::cout << approx2.error_term() << " " << approx2.max_error() << " " << approx2.max_change() << std::endl;
- }
- std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
- #endif
- f = std::exp;
- std::cout << "Testing exp approximation, not pinned to origin, abolute error, 3+3 term rational\n";
- boost::math::tools::remez_minimax<double> approx3(f, 3, 3, -1, 1, false, false);
- std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
- for(unsigned i = 0; i < 7; ++i)
- {
- approx3.iterate();
- std::cout << approx3.error_term() << " " << approx3.max_error() << " " << approx3.max_change() << std::endl;
- }
- std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
- std::cout << "Testing exp approximation, not pinned to origin, relative error, 3+3 term rational\n";
- boost::math::tools::remez_minimax<double> approx4(f, 3, 3, -1, 1, false, true);
- std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
- for(unsigned i = 0; i < 7; ++i)
- {
- approx4.iterate();
- std::cout << approx4.error_term() << " " << approx4.max_error() << " " << approx4.max_change() << std::endl;
- }
- std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
- f = std::cos;
- std::cout << "Testing cos approximation, not pinned to origin, abolute error, 2+2 term rational\n";
- boost::math::tools::remez_minimax<double> approx5(f, 2, 2, 0, 1, false, false);
- std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
- for(unsigned i = 0; i < 7; ++i)
- {
- approx5.iterate();
- std::cout << approx5.error_term() << " " << approx5.max_error() << " " << approx5.max_change() << std::endl;
- }
- std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
- std::cout << "Testing cos approximation, not pinned to origin, relative error, 2+2 term rational\n";
- boost::math::tools::remez_minimax<double> approx6(f, 2, 2, 0, 1, false, true);
- std::cout << "Interpolation Error: " << approx1.max_error() << std::endl;
- for(unsigned i = 0; i < 7; ++i)
- {
- approx6.iterate();
- std::cout << approx6.error_term() << " " << approx6.max_error() << " " << approx6.max_change() << std::endl;
- }
- std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
- }
- BOOST_AUTO_TEST_CASE( test_main )
- {
- test_polynomial();
- test_rational();
-
- }
|