test_roots.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. // (C) Copyright John Maddock 2006.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <pch.hpp>
  6. #define BOOST_TEST_MAIN
  7. #include <boost/test/unit_test.hpp>
  8. #include <boost/test/tools/floating_point_comparison.hpp>
  9. #include <boost/test/results_collector.hpp>
  10. #include <boost/math/special_functions/beta.hpp>
  11. #include <boost/math/distributions/skew_normal.hpp>
  12. #include <boost/math/tools/polynomial.hpp>
  13. #include <boost/math/tools/roots.hpp>
  14. #include <boost/math/constants/constants.hpp>
  15. #include <boost/test/results_collector.hpp>
  16. #include <boost/test/unit_test.hpp>
  17. #include <boost/array.hpp>
  18. #include <boost/type_index.hpp>
  19. #include "table_type.hpp"
  20. #include <iostream>
  21. #include <iomanip>
  22. #include <boost/multiprecision/cpp_bin_float.hpp>
  23. #include <boost/multiprecision/cpp_complex.hpp>
  24. #define BOOST_CHECK_CLOSE_EX(a, b, prec, i) \
  25. {\
  26. unsigned int failures = boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed;\
  27. BOOST_CHECK_CLOSE(a, b, prec); \
  28. if(failures != boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed)\
  29. {\
  30. std::cerr << "Failure was at row " << i << std::endl;\
  31. std::cerr << std::setprecision(35); \
  32. std::cerr << "{ " << data[i][0] << " , " << data[i][1] << " , " << data[i][2];\
  33. std::cerr << " , " << data[i][3] << " , " << data[i][4] << " , " << data[i][5] << " } " << std::endl;\
  34. }\
  35. }
  36. //
  37. // Implement various versions of inverse of the incomplete beta
  38. // using different root finding algorithms, and deliberately "bad"
  39. // starting conditions: that way we get all the pathological cases
  40. // we could ever wish for!!!
  41. //
  42. template <class T, class Policy>
  43. struct ibeta_roots_1 // for first order algorithms
  44. {
  45. ibeta_roots_1(T _a, T _b, T t, bool inv = false, bool neg = false)
  46. : a(_a), b(_b), target(t), invert(inv), neg(neg) {}
  47. T operator()(const T& x)
  48. {
  49. return boost::math::detail::ibeta_imp(a, b, (neg ? -x : x), Policy(), invert, true) - target;
  50. }
  51. private:
  52. T a, b, target;
  53. bool invert, neg;
  54. };
  55. template <class T, class Policy>
  56. struct ibeta_roots_2 // for second order algorithms
  57. {
  58. ibeta_roots_2(T _a, T _b, T t, bool inv = false, bool neg = false)
  59. : a(_a), b(_b), target(t), invert(inv), neg(neg) {}
  60. boost::math::tuple<T, T> operator()(const T& xx)
  61. {
  62. typedef typename boost::math::lanczos::lanczos<T, Policy>::type L;
  63. T x = neg ? -xx : xx;
  64. T f = boost::math::detail::ibeta_imp(a, b, x, Policy(), invert, true) - target;
  65. T f1 = invert ?
  66. -boost::math::detail::ibeta_power_terms(b, a, 1 - x, x, L(), true, Policy())
  67. : boost::math::detail::ibeta_power_terms(a, b, x, 1 - x, L(), true, Policy());
  68. T y = 1 - x;
  69. if(y == 0)
  70. y = boost::math::tools::min_value<T>() * 8;
  71. f1 /= y * x;
  72. // make sure we don't have a zero derivative:
  73. if(f1 == 0)
  74. f1 = (invert ? -1 : 1) * boost::math::tools::min_value<T>() * 64;
  75. return boost::math::make_tuple(f, neg ? -f1 : f1);
  76. }
  77. private:
  78. T a, b, target;
  79. bool invert, neg;
  80. };
  81. template <class T, class Policy>
  82. struct ibeta_roots_3 // for third order algorithms
  83. {
  84. ibeta_roots_3(T _a, T _b, T t, bool inv = false, bool neg = false)
  85. : a(_a), b(_b), target(t), invert(inv), neg(neg) {}
  86. boost::math::tuple<T, T, T> operator()(const T& xx)
  87. {
  88. typedef typename boost::math::lanczos::lanczos<T, Policy>::type L;
  89. T x = neg ? -xx : xx;
  90. T f = boost::math::detail::ibeta_imp(a, b, x, Policy(), invert, true) - target;
  91. T f1 = invert ?
  92. -boost::math::detail::ibeta_power_terms(b, a, 1 - x, x, L(), true, Policy())
  93. : boost::math::detail::ibeta_power_terms(a, b, x, 1 - x, L(), true, Policy());
  94. T y = 1 - x;
  95. if(y == 0)
  96. y = boost::math::tools::min_value<T>() * 8;
  97. f1 /= y * x;
  98. T f2 = f1 * (-y * a + (b - 2) * x + 1) / (y * x);
  99. if(invert)
  100. f2 = -f2;
  101. // make sure we don't have a zero derivative:
  102. if(f1 == 0)
  103. f1 = (invert ? -1 : 1) * boost::math::tools::min_value<T>() * 64;
  104. if (neg)
  105. {
  106. f1 = -f1;
  107. }
  108. return boost::math::make_tuple(f, f1, f2);
  109. }
  110. private:
  111. T a, b, target;
  112. bool invert, neg;
  113. };
  114. double inverse_ibeta_bisect(double a, double b, double z)
  115. {
  116. typedef boost::math::policies::policy<> pol;
  117. bool invert = false;
  118. int bits = std::numeric_limits<double>::digits;
  119. //
  120. // special cases, we need to have these because there may be other
  121. // possible answers:
  122. //
  123. if(z == 1) return 1;
  124. if(z == 0) return 0;
  125. //
  126. // We need a good estimate of the error in the incomplete beta function
  127. // so that we don't set the desired precision too high. Assume that 3-bits
  128. // are lost each time the arguments increase by a factor of 10:
  129. //
  130. using namespace std;
  131. int bits_lost = static_cast<int>(ceil(log10((std::max)(a, b)) * 3));
  132. if(bits_lost < 0)
  133. bits_lost = 3;
  134. else
  135. bits_lost += 3;
  136. int precision = bits - bits_lost;
  137. double min = 0;
  138. double max = 1;
  139. boost::math::tools::eps_tolerance<double> tol(precision);
  140. return boost::math::tools::bisect(ibeta_roots_1<double, pol>(a, b, z, invert), min, max, tol).first;
  141. }
  142. double inverse_ibeta_bisect_neg(double a, double b, double z)
  143. {
  144. typedef boost::math::policies::policy<> pol;
  145. bool invert = false;
  146. int bits = std::numeric_limits<double>::digits;
  147. //
  148. // special cases, we need to have these because there may be other
  149. // possible answers:
  150. //
  151. if(z == 1) return 1;
  152. if(z == 0) return 0;
  153. //
  154. // We need a good estimate of the error in the incomplete beta function
  155. // so that we don't set the desired precision too high. Assume that 3-bits
  156. // are lost each time the arguments increase by a factor of 10:
  157. //
  158. using namespace std;
  159. int bits_lost = static_cast<int>(ceil(log10((std::max)(a, b)) * 3));
  160. if(bits_lost < 0)
  161. bits_lost = 3;
  162. else
  163. bits_lost += 3;
  164. int precision = bits - bits_lost;
  165. double min = -1;
  166. double max = 0;
  167. boost::math::tools::eps_tolerance<double> tol(precision);
  168. return -boost::math::tools::bisect(ibeta_roots_1<double, pol>(a, b, z, invert, true), min, max, tol).first;
  169. }
  170. double inverse_ibeta_newton(double a, double b, double z)
  171. {
  172. double guess = 0.5;
  173. bool invert = false;
  174. int bits = std::numeric_limits<double>::digits;
  175. //
  176. // special cases, we need to have these because there may be other
  177. // possible answers:
  178. //
  179. if(z == 1) return 1;
  180. if(z == 0) return 0;
  181. //
  182. // We need a good estimate of the error in the incomplete beta function
  183. // so that we don't set the desired precision too high. Assume that 3-bits
  184. // are lost each time the arguments increase by a factor of 10:
  185. //
  186. using namespace std;
  187. int bits_lost = static_cast<int>(ceil(log10((std::max)(a, b)) * 3));
  188. if(bits_lost < 0)
  189. bits_lost = 3;
  190. else
  191. bits_lost += 3;
  192. int precision = bits - bits_lost;
  193. double min = 0;
  194. double max = 1;
  195. return boost::math::tools::newton_raphson_iterate(ibeta_roots_2<double, boost::math::policies::policy<> >(a, b, z, invert), guess, min, max, precision);
  196. }
  197. double inverse_ibeta_newton_neg(double a, double b, double z)
  198. {
  199. double guess = 0.5;
  200. bool invert = false;
  201. int bits = std::numeric_limits<double>::digits;
  202. //
  203. // special cases, we need to have these because there may be other
  204. // possible answers:
  205. //
  206. if(z == 1) return 1;
  207. if(z == 0) return 0;
  208. //
  209. // We need a good estimate of the error in the incomplete beta function
  210. // so that we don't set the desired precision too high. Assume that 3-bits
  211. // are lost each time the arguments increase by a factor of 10:
  212. //
  213. using namespace std;
  214. int bits_lost = static_cast<int>(ceil(log10((std::max)(a, b)) * 3));
  215. if(bits_lost < 0)
  216. bits_lost = 3;
  217. else
  218. bits_lost += 3;
  219. int precision = bits - bits_lost;
  220. double min = -1;
  221. double max = 0;
  222. return -boost::math::tools::newton_raphson_iterate(ibeta_roots_2<double, boost::math::policies::policy<> >(a, b, z, invert, true), -guess, min, max, precision);
  223. }
  224. double inverse_ibeta_halley(double a, double b, double z)
  225. {
  226. double guess = 0.5;
  227. bool invert = false;
  228. int bits = std::numeric_limits<double>::digits;
  229. //
  230. // special cases, we need to have these because there may be other
  231. // possible answers:
  232. //
  233. if(z == 1) return 1;
  234. if(z == 0) return 0;
  235. //
  236. // We need a good estimate of the error in the incomplete beta function
  237. // so that we don't set the desired precision too high. Assume that 3-bits
  238. // are lost each time the arguments increase by a factor of 10:
  239. //
  240. using namespace std;
  241. int bits_lost = static_cast<int>(ceil(log10((std::max)(a, b)) * 3));
  242. if(bits_lost < 0)
  243. bits_lost = 3;
  244. else
  245. bits_lost += 3;
  246. int precision = bits - bits_lost;
  247. double min = 0;
  248. double max = 1;
  249. return boost::math::tools::halley_iterate(ibeta_roots_3<double, boost::math::policies::policy<> >(a, b, z, invert), guess, min, max, precision);
  250. }
  251. double inverse_ibeta_halley_neg(double a, double b, double z)
  252. {
  253. double guess = -0.5;
  254. bool invert = false;
  255. int bits = std::numeric_limits<double>::digits;
  256. //
  257. // special cases, we need to have these because there may be other
  258. // possible answers:
  259. //
  260. if(z == 1) return 1;
  261. if(z == 0) return 0;
  262. //
  263. // We need a good estimate of the error in the incomplete beta function
  264. // so that we don't set the desired precision too high. Assume that 3-bits
  265. // are lost each time the arguments increase by a factor of 10:
  266. //
  267. using namespace std;
  268. int bits_lost = static_cast<int>(ceil(log10((std::max)(a, b)) * 3));
  269. if(bits_lost < 0)
  270. bits_lost = 3;
  271. else
  272. bits_lost += 3;
  273. int precision = bits - bits_lost;
  274. double min = -1;
  275. double max = 0;
  276. return -boost::math::tools::halley_iterate(ibeta_roots_3<double, boost::math::policies::policy<> >(a, b, z, invert, true), guess, min, max, precision);
  277. }
  278. double inverse_ibeta_schroder(double a, double b, double z)
  279. {
  280. double guess = 0.5;
  281. bool invert = false;
  282. int bits = std::numeric_limits<double>::digits;
  283. //
  284. // special cases, we need to have these because there may be other
  285. // possible answers:
  286. //
  287. if(z == 1) return 1;
  288. if(z == 0) return 0;
  289. //
  290. // We need a good estimate of the error in the incomplete beta function
  291. // so that we don't set the desired precision too high. Assume that 3-bits
  292. // are lost each time the arguments increase by a factor of 10:
  293. //
  294. using namespace std;
  295. int bits_lost = static_cast<int>(ceil(log10((std::max)(a, b)) * 3));
  296. if(bits_lost < 0)
  297. bits_lost = 3;
  298. else
  299. bits_lost += 3;
  300. int precision = bits - bits_lost;
  301. double min = 0;
  302. double max = 1;
  303. return boost::math::tools::schroder_iterate(ibeta_roots_3<double, boost::math::policies::policy<> >(a, b, z, invert), guess, min, max, precision);
  304. }
  305. template <class Real, class T>
  306. void test_inverses(const T& data)
  307. {
  308. using namespace std;
  309. typedef Real value_type;
  310. value_type precision = static_cast<value_type>(ldexp(1.0, 1-boost::math::policies::digits<value_type, boost::math::policies::policy<> >()/2)) * 150;
  311. if(boost::math::policies::digits<value_type, boost::math::policies::policy<> >() < 50)
  312. precision = 1; // 1% or two decimal digits, all we can hope for when the input is truncated
  313. for(unsigned i = 0; i < data.size(); ++i)
  314. {
  315. //
  316. // These inverse tests are thrown off if the output of the
  317. // incomplete beta is too close to 1: basically there is insuffient
  318. // information left in the value we're using as input to the inverse
  319. // to be able to get back to the original value.
  320. //
  321. if(data[i][5] == 0)
  322. {
  323. BOOST_CHECK_EQUAL(inverse_ibeta_halley(Real(data[i][0]), Real(data[i][1]), Real(data[i][5])), value_type(0));
  324. BOOST_CHECK_EQUAL(inverse_ibeta_halley_neg(Real(data[i][0]), Real(data[i][1]), Real(data[i][5])), value_type(0));
  325. BOOST_CHECK_EQUAL(inverse_ibeta_schroder(Real(data[i][0]), Real(data[i][1]), Real(data[i][5])), value_type(0));
  326. BOOST_CHECK_EQUAL(inverse_ibeta_newton(Real(data[i][0]), Real(data[i][1]), Real(data[i][5])), value_type(0));
  327. BOOST_CHECK_EQUAL(inverse_ibeta_newton_neg(Real(data[i][0]), Real(data[i][1]), Real(data[i][5])), value_type(0));
  328. BOOST_CHECK_EQUAL(inverse_ibeta_bisect(Real(data[i][0]), Real(data[i][1]), Real(data[i][5])), value_type(0));
  329. BOOST_CHECK_EQUAL(inverse_ibeta_bisect_neg(Real(data[i][0]), Real(data[i][1]), Real(data[i][5])), value_type(0));
  330. }
  331. else if((1 - data[i][5] > 0.001)
  332. && (fabs(data[i][5]) > 2 * boost::math::tools::min_value<value_type>())
  333. && (fabs(data[i][5]) > 2 * boost::math::tools::min_value<double>()))
  334. {
  335. value_type inv = inverse_ibeta_halley(Real(data[i][0]), Real(data[i][1]), Real(data[i][5]));
  336. BOOST_CHECK_CLOSE_EX(Real(data[i][2]), inv, precision, i);
  337. inv = inverse_ibeta_halley_neg(Real(data[i][0]), Real(data[i][1]), Real(data[i][5]));
  338. BOOST_ASSERT(boost::math::isfinite(inv));
  339. BOOST_CHECK_CLOSE_EX(Real(data[i][2]), inv, precision, i);
  340. inv = inverse_ibeta_schroder(Real(data[i][0]), Real(data[i][1]), Real(data[i][5]));
  341. BOOST_CHECK_CLOSE_EX(Real(data[i][2]), inv, precision, i);
  342. inv = inverse_ibeta_newton(Real(data[i][0]), Real(data[i][1]), Real(data[i][5]));
  343. BOOST_CHECK_CLOSE_EX(Real(data[i][2]), inv, precision, i);
  344. inv = inverse_ibeta_newton_neg(Real(data[i][0]), Real(data[i][1]), Real(data[i][5]));
  345. BOOST_CHECK_CLOSE_EX(Real(data[i][2]), inv, precision, i);
  346. inv = inverse_ibeta_bisect(Real(data[i][0]), Real(data[i][1]), Real(data[i][5]));
  347. BOOST_CHECK_CLOSE_EX(Real(data[i][2]), inv, precision, i);
  348. inv = inverse_ibeta_bisect_neg(Real(data[i][0]), Real(data[i][1]), Real(data[i][5]));
  349. BOOST_CHECK_CLOSE_EX(Real(data[i][2]), inv, precision, i);
  350. }
  351. else if(1 == data[i][5])
  352. {
  353. BOOST_CHECK_EQUAL(inverse_ibeta_halley(Real(data[i][0]), Real(data[i][1]), Real(data[i][5])), value_type(1));
  354. BOOST_CHECK_EQUAL(inverse_ibeta_halley_neg(Real(data[i][0]), Real(data[i][1]), Real(data[i][5])), value_type(1));
  355. BOOST_CHECK_EQUAL(inverse_ibeta_schroder(Real(data[i][0]), Real(data[i][1]), Real(data[i][5])), value_type(1));
  356. BOOST_CHECK_EQUAL(inverse_ibeta_newton(Real(data[i][0]), Real(data[i][1]), Real(data[i][5])), value_type(1));
  357. BOOST_CHECK_EQUAL(inverse_ibeta_newton_neg(Real(data[i][0]), Real(data[i][1]), Real(data[i][5])), value_type(1));
  358. BOOST_CHECK_EQUAL(inverse_ibeta_bisect(Real(data[i][0]), Real(data[i][1]), Real(data[i][5])), value_type(1));
  359. BOOST_CHECK_EQUAL(inverse_ibeta_bisect_neg(Real(data[i][0]), Real(data[i][1]), Real(data[i][5])), value_type(1));
  360. }
  361. }
  362. }
  363. #ifndef SC_
  364. #define SC_(x) static_cast<typename table_type<T>::type>(BOOST_JOIN(x, L))
  365. #endif
  366. template <class T>
  367. void test_beta(T, const char* /* name */)
  368. {
  369. //
  370. // The actual test data is rather verbose, so it's in a separate file
  371. //
  372. // The contents are as follows, each row of data contains
  373. // five items, input value a, input value b, integration limits x, beta(a, b, x) and ibeta(a, b, x):
  374. //
  375. # include "ibeta_small_data.ipp"
  376. test_inverses<T>(ibeta_small_data);
  377. # include "ibeta_data.ipp"
  378. test_inverses<T>(ibeta_data);
  379. # include "ibeta_large_data.ipp"
  380. test_inverses<T>(ibeta_large_data);
  381. }
  382. #if !defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) && !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX) && !defined(BOOST_NO_CXX11_LAMBDAS)
  383. template <class Complex>
  384. void test_complex_newton()
  385. {
  386. typedef typename Complex::value_type Real;
  387. std::cout << "Testing complex Newton's Method on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
  388. using std::abs;
  389. using std::sqrt;
  390. using boost::math::tools::complex_newton;
  391. using boost::math::tools::polynomial;
  392. using boost::math::constants::half;
  393. Real tol = std::numeric_limits<Real>::epsilon();
  394. // p(z) = z^2 + 1, roots: \pm i.
  395. polynomial<Complex> p{{1,0}, {0, 0}, {1,0}};
  396. Complex guess{1,1};
  397. polynomial<Complex> p_prime = p.prime();
  398. auto f = [&](Complex z) { return std::make_pair<Complex, Complex>(p(z), p_prime(z)); };
  399. Complex root = complex_newton(f, guess);
  400. BOOST_CHECK(abs(root.real()) <= tol);
  401. BOOST_CHECK_CLOSE(root.imag(), (Real)1, tol);
  402. guess = -guess;
  403. root = complex_newton(f, guess);
  404. BOOST_CHECK(abs(root.real()) <= tol);
  405. BOOST_CHECK_CLOSE(root.imag(), (Real)-1, tol);
  406. // Test that double roots are handled correctly-as correctly as possible.
  407. // Convergence at a double root is not quadratic.
  408. // This sets p = (z-i)^2:
  409. p = polynomial<Complex>({{-1,0}, {0,-2}, {1,0}});
  410. p_prime = p.prime();
  411. guess = -guess;
  412. auto g = [&](Complex z) { return std::make_pair<Complex, Complex>(p(z), p_prime(z)); };
  413. root = complex_newton(g, guess);
  414. BOOST_CHECK(abs(root.real()) < 10*sqrt(tol));
  415. BOOST_CHECK_CLOSE(root.imag(), (Real)1, tol);
  416. // Test that zero derivatives are handled.
  417. // p(z) = z^2 + iz + 1
  418. p = polynomial<Complex>({{1,0}, {0,1}, {1,0}});
  419. // p'(z) = 2z + i
  420. p_prime = p.prime();
  421. guess = Complex(0,-boost::math::constants::half<Real>());
  422. auto g2 = [&](Complex z) { return std::make_pair<Complex, Complex>(p(z), p_prime(z)); };
  423. root = complex_newton(g2, guess);
  424. // Here's the other root, in case code changes cause it to be found:
  425. //Complex expected_root1{0, half<Real>()*(sqrt(static_cast<Real>(5)) - static_cast<Real>(1))};
  426. Complex expected_root2{0, -half<Real>()*(sqrt(static_cast<Real>(5)) + static_cast<Real>(1))};
  427. BOOST_CHECK_CLOSE(expected_root2.imag(),root.imag(), tol);
  428. BOOST_CHECK(abs(root.real()) < tol);
  429. // Does a zero root pass the termination criteria?
  430. p = polynomial<Complex>({{0,0}, {0,0}, {1,0}});
  431. p_prime = p.prime();
  432. guess = Complex(0, -boost::math::constants::half<Real>());
  433. auto g3 = [&](Complex z) { return std::make_pair<Complex, Complex>(p(z), p_prime(z)); };
  434. root = complex_newton(g3, guess);
  435. BOOST_CHECK(abs(root.real()) < tol);
  436. // Does a monstrous root pass?
  437. Real x = -pow(static_cast<Real>(10), 20);
  438. p = polynomial<Complex>({{x, x}, {1,0}});
  439. p_prime = p.prime();
  440. guess = Complex(0, -boost::math::constants::half<Real>());
  441. auto g4 = [&](Complex z) { return std::make_pair<Complex, Complex>(p(z), p_prime(z)); };
  442. root = complex_newton(g4, guess);
  443. BOOST_CHECK(abs(root.real() + x) < tol);
  444. BOOST_CHECK(abs(root.imag() + x) < tol);
  445. }
  446. // Polynomials which didn't factorize using Newton's method at first:
  447. void test_daubechies_fails()
  448. {
  449. std::cout << "Testing failures from Daubechies filter computation.\n";
  450. using std::abs;
  451. using std::sqrt;
  452. using boost::math::tools::complex_newton;
  453. using boost::math::tools::polynomial;
  454. using boost::math::constants::half;
  455. double tol = 500*std::numeric_limits<double>::epsilon();
  456. polynomial<std::complex<double>> p{{-185961388.136908293,141732493.98435241}, {601080390,0}};
  457. std::complex<double> guess{1,1};
  458. polynomial<std::complex<double>> p_prime = p.prime();
  459. auto f = [&](std::complex<double> z) { return std::make_pair<std::complex<double>, std::complex<double>>(p(z), p_prime(z)); };
  460. std::complex<double> root = complex_newton(f, guess);
  461. std::complex<double> expected_root = -p.data()[0]/p.data()[1];
  462. BOOST_CHECK_CLOSE(expected_root.imag(), root.imag(), tol);
  463. BOOST_CHECK_CLOSE(expected_root.real(), root.real(), tol);
  464. }
  465. #endif
  466. #if !defined(BOOST_NO_CXX17_IF_CONSTEXPR)
  467. template<class Real>
  468. void test_solve_real_quadratic()
  469. {
  470. Real tol = std::numeric_limits<Real>::epsilon();
  471. using boost::math::tools::quadratic_roots;
  472. auto [x0, x1] = quadratic_roots<Real>(1, 0, -1);
  473. BOOST_CHECK_CLOSE(x0, Real(-1), tol);
  474. BOOST_CHECK_CLOSE(x1, Real(1), tol);
  475. auto p = quadratic_roots((Real)7, (Real)0, (Real)0);
  476. BOOST_CHECK_SMALL(p.first, tol);
  477. BOOST_CHECK_SMALL(p.second, tol);
  478. // (x-7)^2 = x^2 - 14*x + 49:
  479. p = quadratic_roots((Real)1, (Real)-14, (Real)49);
  480. BOOST_CHECK_CLOSE(p.first, Real(7), tol);
  481. BOOST_CHECK_CLOSE(p.second, Real(7), tol);
  482. // This test does not pass in multiprecision,
  483. // due to the fact it does not have an fma:
  484. if (std::is_floating_point<Real>::value)
  485. {
  486. // (x-1)(x-1-eps) = x^2 + (-eps - 2)x + (1)(1+eps)
  487. Real eps = 2*std::numeric_limits<Real>::epsilon();
  488. Real b = 256 * (-2 - eps);
  489. Real c = 256 * (1 + eps);
  490. p = quadratic_roots((Real)256, b, c);
  491. BOOST_CHECK_CLOSE(p.first, Real(1), tol);
  492. BOOST_CHECK_CLOSE(p.second, Real(1) + eps, tol);
  493. }
  494. if (std::is_same<Real, double>::value)
  495. {
  496. // Kahan's example: This is the test that demonstrates the necessity of the fma instruction.
  497. // https://en.wikipedia.org/wiki/Loss_of_significance#Instability_of_the_quadratic_equation
  498. p = quadratic_roots<Real>((Real)94906265.625, (Real )-189812534, (Real)94906268.375);
  499. BOOST_CHECK_CLOSE_FRACTION(p.first, Real(1), tol);
  500. BOOST_CHECK_CLOSE_FRACTION(p.second, 1.000000028975958, 4*tol);
  501. }
  502. }
  503. template<class Z>
  504. void test_solve_int_quadratic()
  505. {
  506. double tol = std::numeric_limits<double>::epsilon();
  507. using boost::math::tools::quadratic_roots;
  508. auto [x0, x1] = quadratic_roots(1, 0, -1);
  509. BOOST_CHECK_CLOSE(x0, double(-1), tol);
  510. BOOST_CHECK_CLOSE(x1, double(1), tol);
  511. auto p = quadratic_roots(7, 0, 0);
  512. BOOST_CHECK_SMALL(p.first, tol);
  513. BOOST_CHECK_SMALL(p.second, tol);
  514. // (x-7)^2 = x^2 - 14*x + 49:
  515. p = quadratic_roots(1, -14, 49);
  516. BOOST_CHECK_CLOSE(p.first, double(7), tol);
  517. BOOST_CHECK_CLOSE(p.second, double(7), tol);
  518. }
  519. template<class Complex>
  520. void test_solve_complex_quadratic()
  521. {
  522. using Real = typename Complex::value_type;
  523. Real tol = std::numeric_limits<Real>::epsilon();
  524. using boost::math::tools::quadratic_roots;
  525. auto [x0, x1] = quadratic_roots<Complex>({1,0}, {0,0}, {-1,0});
  526. BOOST_CHECK_CLOSE(x0.real(), Real(-1), tol);
  527. BOOST_CHECK_CLOSE(x1.real(), Real(1), tol);
  528. BOOST_CHECK_SMALL(x0.imag(), tol);
  529. BOOST_CHECK_SMALL(x1.imag(), tol);
  530. auto p = quadratic_roots<Complex>({7,0}, {0,0}, {0,0});
  531. BOOST_CHECK_SMALL(p.first.real(), tol);
  532. BOOST_CHECK_SMALL(p.second.real(), tol);
  533. // (x-7)^2 = x^2 - 14*x + 49:
  534. p = quadratic_roots<Complex>({1,0}, {-14,0}, {49,0});
  535. BOOST_CHECK_CLOSE(p.first.real(), Real(7), tol);
  536. BOOST_CHECK_CLOSE(p.second.real(), Real(7), tol);
  537. }
  538. #endif
  539. void test_failures()
  540. {
  541. #if !defined(BOOST_NO_CXX11_LAMBDAS)
  542. // There is no root:
  543. BOOST_CHECK_THROW(boost::math::tools::newton_raphson_iterate([](double x) { return std::make_pair(x * x + 1, 2 * x); }, 10.0, -12.0, 12.0, 52), boost::math::evaluation_error);
  544. BOOST_CHECK_THROW(boost::math::tools::newton_raphson_iterate([](double x) { return std::make_pair(x * x + 1, 2 * x); }, -10.0, -12.0, 12.0, 52), boost::math::evaluation_error);
  545. // There is a root, but a bad guess takes us into a local minima:
  546. BOOST_CHECK_THROW(boost::math::tools::newton_raphson_iterate([](double x) { return std::make_pair(boost::math::pow<6>(x) - 2 * boost::math::pow<4>(x) + x + 0.5, 6 * boost::math::pow<5>(x) - 8 * boost::math::pow<3>(x) + 1); }, 0.75, -20., 20., 52), boost::math::evaluation_error);
  547. // There is no root:
  548. BOOST_CHECK_THROW(boost::math::tools::halley_iterate([](double x) { return std::make_tuple(x * x + 1, 2 * x, 2); }, 10.0, -12.0, 12.0, 52), boost::math::evaluation_error);
  549. BOOST_CHECK_THROW(boost::math::tools::halley_iterate([](double x) { return std::make_tuple(x * x + 1, 2 * x, 2); }, -10.0, -12.0, 12.0, 52), boost::math::evaluation_error);
  550. // There is a root, but a bad guess takes us into a local minima:
  551. BOOST_CHECK_THROW(boost::math::tools::halley_iterate([](double x) { return std::make_tuple(boost::math::pow<6>(x) - 2 * boost::math::pow<4>(x) + x + 0.5, 6 * boost::math::pow<5>(x) - 8 * boost::math::pow<3>(x) + 1, 30 * boost::math::pow<4>(x) - 24 * boost::math::pow<2>(x)); }, 0.75, -20., 20., 52), boost::math::evaluation_error);
  552. #endif
  553. }
  554. BOOST_AUTO_TEST_CASE( test_main )
  555. {
  556. test_beta(0.1, "double");
  557. #if !defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) && !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX) && !defined(BOOST_NO_CXX11_LAMBDAS)
  558. test_complex_newton<std::complex<float>>();
  559. test_complex_newton<std::complex<double>>();
  560. test_complex_newton<boost::multiprecision::cpp_complex_100>();
  561. test_daubechies_fails();
  562. #endif
  563. #if !defined(BOOST_NO_CXX17_IF_CONSTEXPR)
  564. test_solve_real_quadratic<float>();
  565. test_solve_real_quadratic<double>();
  566. test_solve_real_quadratic<long double>();
  567. test_solve_real_quadratic<boost::multiprecision::cpp_bin_float_50>();
  568. test_solve_int_quadratic<int>();
  569. test_solve_complex_quadratic<std::complex<double>>();
  570. #endif
  571. test_failures();
  572. }