6
6
#include < pch.hpp>
7
7
8
8
#define BOOST_TEST_MAIN
9
+
10
+ // See: https://github.com/boostorg/math/issues/1115
11
+ #if __has_include(<X11/X.h>)
12
+ # include < X11/X.h>
13
+ #endif
14
+
9
15
#include < boost/test/unit_test.hpp>
10
16
#include < boost/test/tools/floating_point_comparison.hpp>
11
17
#include < boost/test/results_collector.hpp>
@@ -438,10 +444,10 @@ void test_beta(T, const char* /* name */)
438
444
}
439
445
440
446
#if !defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) && !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX) && !defined(BOOST_NO_CXX11_LAMBDAS)
441
- template <class Complex >
447
+ template <class ComplexType >
442
448
void test_complex_newton ()
443
449
{
444
- typedef typename Complex ::value_type Real;
450
+ typedef typename ComplexType ::value_type Real;
445
451
std::cout << " Testing complex Newton's Method on type " << boost::typeindex::type_id<Real>().pretty_name () << " \n " ;
446
452
using std::abs ;
447
453
using std::sqrt ;
@@ -451,11 +457,11 @@ void test_complex_newton()
451
457
452
458
Real tol = std::numeric_limits<Real>::epsilon ();
453
459
// p(z) = z^2 + 1, roots: \pm i.
454
- polynomial<Complex > p{{1 ,0 }, {0 , 0 }, {1 ,0 }};
455
- Complex guess{1 ,1 };
456
- polynomial<Complex > p_prime = p.prime ();
457
- auto f = [&](Complex z) { return std::make_pair<Complex, Complex >(p (z), p_prime (z)); };
458
- Complex root = complex_newton (f, guess);
460
+ polynomial<ComplexType > p{{1 ,0 }, {0 , 0 }, {1 ,0 }};
461
+ ComplexType guess{1 ,1 };
462
+ polynomial<ComplexType > p_prime = p.prime ();
463
+ auto f = [&](ComplexType z) { return std::make_pair<ComplexType, ComplexType >(p (z), p_prime (z)); };
464
+ ComplexType root = complex_newton (f, guess);
459
465
460
466
BOOST_CHECK (abs (root.real ()) <= tol);
461
467
BOOST_CHECK_CLOSE (root.imag (), (Real)1 , tol);
@@ -468,44 +474,44 @@ void test_complex_newton()
468
474
// Test that double roots are handled correctly-as correctly as possible.
469
475
// Convergence at a double root is not quadratic.
470
476
// This sets p = (z-i)^2:
471
- p = polynomial<Complex >({{-1 ,0 }, {0 ,-2 }, {1 ,0 }});
477
+ p = polynomial<ComplexType >({{-1 ,0 }, {0 ,-2 }, {1 ,0 }});
472
478
p_prime = p.prime ();
473
479
guess = -guess;
474
- auto g = [&](Complex z) { return std::make_pair<Complex, Complex >(p (z), p_prime (z)); };
480
+ auto g = [&](ComplexType z) { return std::make_pair<ComplexType, ComplexType >(p (z), p_prime (z)); };
475
481
root = complex_newton (g, guess);
476
482
BOOST_CHECK (abs (root.real ()) < 10 *sqrt (tol));
477
483
BOOST_CHECK_CLOSE (root.imag (), (Real)1 , tol);
478
484
479
485
// Test that zero derivatives are handled.
480
486
// p(z) = z^2 + iz + 1
481
- p = polynomial<Complex >({{1 ,0 }, {0 ,1 }, {1 ,0 }});
487
+ p = polynomial<ComplexType >({{1 ,0 }, {0 ,1 }, {1 ,0 }});
482
488
// p'(z) = 2z + i
483
489
p_prime = p.prime ();
484
- guess = Complex (0 ,-boost::math::constants::half<Real>());
485
- auto g2 = [&](Complex z) { return std::make_pair<Complex, Complex >(p (z), p_prime (z)); };
490
+ guess = ComplexType (0 ,-boost::math::constants::half<Real>());
491
+ auto g2 = [&](ComplexType z) { return std::make_pair<ComplexType, ComplexType >(p (z), p_prime (z)); };
486
492
root = complex_newton (g2, guess);
487
493
488
494
// Here's the other root, in case code changes cause it to be found:
489
- // Complex expected_root1{0, half<Real>()*(sqrt(static_cast<Real>(5)) - static_cast<Real>(1))};
490
- Complex expected_root2{0 , -half<Real>()*(sqrt (static_cast <Real>(5 )) + static_cast <Real>(1 ))};
495
+ // ComplexType expected_root1{0, half<Real>()*(sqrt(static_cast<Real>(5)) - static_cast<Real>(1))};
496
+ ComplexType expected_root2{0 , -half<Real>()*(sqrt (static_cast <Real>(5 )) + static_cast <Real>(1 ))};
491
497
492
498
BOOST_CHECK_CLOSE (expected_root2.imag (),root.imag (), tol);
493
499
BOOST_CHECK (abs (root.real ()) < tol);
494
500
495
501
// Does a zero root pass the termination criteria?
496
- p = polynomial<Complex >({{0 ,0 }, {0 ,0 }, {1 ,0 }});
502
+ p = polynomial<ComplexType >({{0 ,0 }, {0 ,0 }, {1 ,0 }});
497
503
p_prime = p.prime ();
498
- guess = Complex (0 , -boost::math::constants::half<Real>());
499
- auto g3 = [&](Complex z) { return std::make_pair<Complex, Complex >(p (z), p_prime (z)); };
504
+ guess = ComplexType (0 , -boost::math::constants::half<Real>());
505
+ auto g3 = [&](ComplexType z) { return std::make_pair<ComplexType, ComplexType >(p (z), p_prime (z)); };
500
506
root = complex_newton (g3, guess);
501
507
BOOST_CHECK (abs (root.real ()) < tol);
502
508
503
509
// Does a monstrous root pass?
504
510
Real x = -pow (static_cast <Real>(10 ), 20 );
505
- p = polynomial<Complex >({{x, x}, {1 ,0 }});
511
+ p = polynomial<ComplexType >({{x, x}, {1 ,0 }});
506
512
p_prime = p.prime ();
507
- guess = Complex (0 , -boost::math::constants::half<Real>());
508
- auto g4 = [&](Complex z) { return std::make_pair<Complex, Complex >(p (z), p_prime (z)); };
513
+ guess = ComplexType (0 , -boost::math::constants::half<Real>());
514
+ auto g4 = [&](ComplexType z) { return std::make_pair<ComplexType, ComplexType >(p (z), p_prime (z)); };
509
515
root = complex_newton (g4, guess);
510
516
BOOST_CHECK (abs (root.real () + x) < tol);
511
517
BOOST_CHECK (abs (root.imag () + x) < tol);
@@ -607,24 +613,24 @@ void test_solve_int_quadratic()
607
613
BOOST_CHECK_CLOSE (p.second , double (7 ), tol);
608
614
}
609
615
610
- template <class Complex >
616
+ template <class ComplexType >
611
617
void test_solve_complex_quadratic ()
612
618
{
613
- using Real = typename Complex ::value_type;
619
+ using Real = typename ComplexType ::value_type;
614
620
Real tol = std::numeric_limits<Real>::epsilon ();
615
621
using boost::math::tools::quadratic_roots;
616
- auto [x0, x1] = quadratic_roots<Complex >({1 ,0 }, {0 ,0 }, {-1 ,0 });
622
+ auto [x0, x1] = quadratic_roots<ComplexType >({1 ,0 }, {0 ,0 }, {-1 ,0 });
617
623
BOOST_CHECK_CLOSE (x0.real (), Real (-1 ), tol);
618
624
BOOST_CHECK_CLOSE (x1.real (), Real (1 ), tol);
619
625
BOOST_CHECK_SMALL (x0.imag (), tol);
620
626
BOOST_CHECK_SMALL (x1.imag (), tol);
621
627
622
- auto p = quadratic_roots<Complex >({7 ,0 }, {0 ,0 }, {0 ,0 });
628
+ auto p = quadratic_roots<ComplexType >({7 ,0 }, {0 ,0 }, {0 ,0 });
623
629
BOOST_CHECK_SMALL (p.first .real (), tol);
624
630
BOOST_CHECK_SMALL (p.second .real (), tol);
625
631
626
632
// (x-7)^2 = x^2 - 14*x + 49:
627
- p = quadratic_roots<Complex >({1 ,0 }, {-14 ,0 }, {49 ,0 });
633
+ p = quadratic_roots<ComplexType >({1 ,0 }, {-14 ,0 }, {49 ,0 });
628
634
BOOST_CHECK_CLOSE (p.first .real (), Real (7 ), tol);
629
635
BOOST_CHECK_CLOSE (p.second .real (), Real (7 ), tol);
630
636
0 commit comments