File: test_root_finding_concepts.cpp

package info (click to toggle)
scipy 1.16.0-1exp7
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 234,820 kB
  • sloc: cpp: 503,145; python: 344,611; ansic: 195,638; javascript: 89,566; fortran: 56,210; cs: 3,081; f90: 1,150; sh: 848; makefile: 785; pascal: 284; csh: 135; lisp: 134; xml: 56; perl: 51
file content (226 lines) | stat: -rw-r--r-- 9,409 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright John Maddock 2014

// 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)

#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp> // Boost.Test
#include <boost/test/results_collector.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/test/tools/floating_point_comparison.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/fusion/include/tuple.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <boost/math/tools/toms748_solve.hpp>
#include <tuple>

#include <iostream>
#include <iomanip>
   using std::cout;
   using std::endl;
   using std::setprecision;

#include <boost/math/tools/roots.hpp>

//
// We'll use cbrt as an example:
//
struct cbtr_functor_1
{
   cbtr_functor_1(double x) : m_target(x) {}
   double operator()(double x)
   {
      return x * x * x - m_target;
   }
private:
   double m_target;
};

struct cbtr_functor_2a
{
   cbtr_functor_2a(double x) : m_target(x) {}
   std::pair<double, double> operator()(double x)
   {
      return std::make_pair(x * x * x - m_target, 3 * x * x);
   }
private:
   double m_target;
};

struct cbtr_functor_2b
{
   cbtr_functor_2b(double x) : m_target(x) {}
   std::tuple<double, double> operator()(double x)
   {
      return std::tuple<double, double>(x * x * x - m_target, 3 * x * x);
   }
private:
   double m_target;
};
struct cbtr_functor_2c
{
   cbtr_functor_2c(double x) : m_target(x) {}
   boost::tuple<double, double> operator()(double x)
   {
      return boost::tuple<double, double>(x * x * x - m_target, 3 * x * x);
   }
private:
   double m_target;
};
struct cbtr_functor_2d
{
   cbtr_functor_2d(double x) : m_target(x) {}
   boost::fusion::tuple<double, double> operator()(double x)
   {
      return boost::fusion::tuple<double, double>(x * x * x - m_target, 3 * x * x);
   }
private:
   double m_target;
};

struct cbtr_functor_3b
{
   cbtr_functor_3b(double x) : m_target(x) {}
   std::tuple<double, double, double> operator()(double x)
   {
      return std::tuple<double, double, double>(x * x * x - m_target, 3 * x * x, 6 * x);
   }
private:
   double m_target;
};

struct cbtr_functor_3c
{
   cbtr_functor_3c(double x) : m_target(x) {}
   boost::tuple<double, double, double> operator()(double x)
   {
      return boost::tuple<double, double, double>(x * x * x - m_target, 3 * x * x, 6 * x);
   }
private:
   double m_target;
};
struct cbtr_functor_3d
{
   cbtr_functor_3d(double x) : m_target(x) {}
   boost::fusion::tuple<double, double, double> operator()(double x)
   {
      return boost::fusion::tuple<double, double, double>(x * x * x - m_target, 3 * x * x, 6 * x);
   }
private:
   double m_target;
};


BOOST_AUTO_TEST_CASE( test_main )
{
   double x = 27;
   double expected = 3;
   double result;
   double tolerance = std::numeric_limits<double>::epsilon() * 5;
   std::pair<double, double> p;
   //
   // Start by trying the unary functors, bisect first:
   //
   cbtr_functor_1 f1(x);
   boost::math::tools::eps_tolerance<double> t(std::numeric_limits<double>::digits - 1);
   p = boost::math::tools::bisect(f1, 0.0, x, t);
   result = (p.first + p.second) / 2;
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   //
   // bracket_and_solve_root:
   //
   std::uintmax_t max_iter = boost::math::policies::get_max_root_iterations<boost::math::policies::policy<> >();
   p = boost::math::tools::bracket_and_solve_root(f1, x, 2.0, true, t, max_iter);
   result = (p.first + p.second) / 2;
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   //
   // toms748_solve:
   //
   max_iter = boost::math::policies::get_max_root_iterations<boost::math::policies::policy<> >();
   p = boost::math::tools::toms748_solve(f1, 0.0, x, t, max_iter);
   result = (p.first + p.second) / 2;
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);

   //
   // Now try again with C++11 lambda's
   //
   p = boost::math::tools::bisect([x](double z){ return z * z * z - x; }, 0.0, x, t);
   result = (p.first + p.second) / 2;
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   //
   // bracket_and_solve_root:
   //
   max_iter = boost::math::policies::get_max_root_iterations<boost::math::policies::policy<> >();
   p = boost::math::tools::bracket_and_solve_root([x](double z){ return z * z * z - x; }, x, 2.0, true, t, max_iter);
   result = (p.first + p.second) / 2;
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   //
   // toms748_solve:
   //
   max_iter = boost::math::policies::get_max_root_iterations<boost::math::policies::policy<> >();
   p = boost::math::tools::toms748_solve([x](double z){ return z * z * z - x; }, 0.0, x, t, max_iter);
   result = (p.first + p.second) / 2;
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);

   cbtr_functor_2a f2(x);
   cbtr_functor_2b f3(x);
   cbtr_functor_2c f4(x);
   cbtr_functor_2d f5(x);

   //
   // Binary Functors - newton_raphson_iterate - test each possible tuple type:
   //
   result = boost::math::tools::newton_raphson_iterate(f2, x, 0.0, x, std::numeric_limits<double>::digits - 1);
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   result = boost::math::tools::newton_raphson_iterate(f3, x, 0.0, x, std::numeric_limits<double>::digits - 1);
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   result = boost::math::tools::newton_raphson_iterate(f4, x, 0.0, x, std::numeric_limits<double>::digits - 1);
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   result = boost::math::tools::newton_raphson_iterate(f5, x, 0.0, x, std::numeric_limits<double>::digits - 1);
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   //
   // And again but with lambdas:
   //
   result = boost::math::tools::newton_raphson_iterate([x](double z){ return std::make_pair(z * z * z - x, 3 * z * z); }, x, 0.0, x, std::numeric_limits<double>::digits - 1);
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   result = boost::math::tools::newton_raphson_iterate([x](double z){ return std::make_tuple(z * z * z - x, 3 * z * z); }, x, 0.0, x, std::numeric_limits<double>::digits - 1);
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   result = boost::math::tools::newton_raphson_iterate([x](double z){ return boost::tuple<double, double>(z * z * z - x, 3 * z * z); }, x, 0.0, x, std::numeric_limits<double>::digits - 1);
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   result = boost::math::tools::newton_raphson_iterate([x](double z){ return boost::fusion::tuple<double, double>(z * z * z - x, 3 * z * z); }, x, 0.0, x, std::numeric_limits<double>::digits - 1);
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   cbtr_functor_3b f6(x);
   cbtr_functor_3c f7(x);
   cbtr_functor_3d f8(x);

   //
   // Ternary functors:
   //
   result = boost::math::tools::halley_iterate(f6, x, 0.0, x, std::numeric_limits<double>::digits - 1);
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   result = boost::math::tools::halley_iterate(f7, x, 0.0, x, std::numeric_limits<double>::digits - 1);
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   result = boost::math::tools::halley_iterate(f8, x, 0.0, x, std::numeric_limits<double>::digits - 1);
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   result = boost::math::tools::halley_iterate([x](double z){ return std::make_tuple(z * z * z - x, 3 * z * z, 6 * z); }, x, 0.0, x, std::numeric_limits<double>::digits - 1);
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   result = boost::math::tools::halley_iterate([x](double z){ return boost::tuple<double, double, double>(z * z * z - x, 3 * z * z, 6 * z); }, x, 0.0, x, std::numeric_limits<double>::digits - 1);
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   result = boost::math::tools::halley_iterate([x](double z){ return boost::fusion::tuple<double, double, double>(z * z * z - x, 3 * z * z, 6 * z); }, x, 0.0, x, std::numeric_limits<double>::digits - 1);
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   result = boost::math::tools::schroder_iterate(f6, x, 0.0, x, std::numeric_limits<double>::digits - 1);
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   result = boost::math::tools::schroder_iterate(f7, x, 0.0, x, std::numeric_limits<double>::digits - 1);
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   result = boost::math::tools::schroder_iterate(f8, x, 0.0, x, std::numeric_limits<double>::digits - 1);
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   result = boost::math::tools::schroder_iterate([x](double z){ return std::make_tuple(z * z * z - x, 3 * z * z, 6 * z); }, x, 0.0, x, std::numeric_limits<double>::digits - 1);
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   result = boost::math::tools::schroder_iterate([x](double z){ return boost::tuple<double, double, double>(z * z * z - x, 3 * z * z, 6 * z); }, x, 0.0, x, std::numeric_limits<double>::digits - 1);
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
   result = boost::math::tools::schroder_iterate([x](double z){ return boost::fusion::tuple<double, double, double>(z * z * z - x, 3 * z * z, 6 * z); }, x, 0.0, x, std::numeric_limits<double>::digits - 1);
   BOOST_CHECK_CLOSE_FRACTION(expected, result, tolerance);
} // BOOST_AUTO_TEST_CASE( test_main )