test_minima.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #include <pch.hpp>
  7. #include <boost/math/tools/minima.hpp>
  8. #define BOOST_TEST_MAIN
  9. #include <boost/test/unit_test.hpp>
  10. #include <boost/test/tools/floating_point_comparison.hpp>
  11. #include <boost/math/special_functions/gamma.hpp>
  12. #include <iostream>
  13. #include <iomanip>
  14. template <class T>
  15. struct poly_test
  16. {
  17. // minima is at (3,4):
  18. T operator()(const T& v)
  19. {
  20. T a = v - 3;
  21. return 3 * a * a + 4;
  22. }
  23. };
  24. template <class T>
  25. void test_minima(T, const char* /* name */)
  26. {
  27. std::pair<T, T> m = boost::math::tools::brent_find_minima(poly_test<T>(), T(-10), T(10), 50);
  28. BOOST_CHECK_CLOSE(m.first, T(3), T(0.001));
  29. BOOST_CHECK_CLOSE(m.second, T(4), T(0.001));
  30. T (*fp)(T);
  31. #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  32. fp = boost::math::lgamma<T>;
  33. #else
  34. fp = boost::math::lgamma;
  35. #endif
  36. m = boost::math::tools::brent_find_minima(fp, T(0.5), T(10), 50);
  37. BOOST_CHECK_CLOSE(m.first, T(1.461632), T(0.1));
  38. #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  39. fp = boost::math::tgamma<T>;
  40. #else
  41. fp = boost::math::tgamma;
  42. #endif
  43. m = boost::math::tools::brent_find_minima(fp, T(0.5), T(10), 50);
  44. BOOST_CHECK_CLOSE(m.first, T(1.461632), T(0.1));
  45. }
  46. BOOST_AUTO_TEST_CASE( test_main )
  47. {
  48. test_minima(0.1f, "float");
  49. test_minima(0.1, "double");
  50. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  51. test_minima(0.1L, "long double");
  52. #endif
  53. }