Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / math / test / test_minima.cpp
1 //  Copyright John Maddock 2006.
2 //  Copyright Paul A. Bristow 2007.
3
4 //  Use, modification and distribution are subject to the
5 //  Boost Software License, Version 1.0. (See accompanying file
6 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7
8 #include <pch.hpp>
9
10 #include <boost/math/tools/minima.hpp>
11 #define BOOST_TEST_MAIN
12 #include <boost/test/unit_test.hpp>
13 #include <boost/test/tools/floating_point_comparison.hpp>
14 #include <boost/math/special_functions/gamma.hpp>
15 #include <iostream>
16 #include <iomanip>
17
18 template <class T>
19 struct poly_test
20 {
21    // minima is at (3,4):
22    T operator()(const T& v)
23    {
24       T a = v - 3;
25       return 3 * a * a + 4;
26    }
27 };
28
29 template <class T>
30 void test_minima(T, const char* /* name */)
31 {
32    std::pair<T, T> m = boost::math::tools::brent_find_minima(poly_test<T>(), T(-10), T(10), 50);
33    BOOST_CHECK_CLOSE(m.first, T(3), T(0.001));
34    BOOST_CHECK_CLOSE(m.second, T(4), T(0.001));
35
36    T (*fp)(T);
37 #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
38    fp = boost::math::lgamma<T>;
39 #else
40    fp = boost::math::lgamma;
41 #endif
42
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 #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
46    fp = boost::math::tgamma<T>;
47 #else
48    fp = boost::math::tgamma;
49 #endif
50    m = boost::math::tools::brent_find_minima(fp, T(0.5), T(10), 50);
51    BOOST_CHECK_CLOSE(m.first, T(1.461632), T(0.1));
52 }
53
54 BOOST_AUTO_TEST_CASE( test_main )
55 {
56    test_minima(0.1f, "float");
57    test_minima(0.1, "double");
58 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
59    test_minima(0.1L, "long double");
60 #endif
61    
62 }
63
64