// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2010 Manuel Yguel // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. #include "main.h" #include #include #include using namespace std; namespace StormEigen { namespace internal { template struct increment_if_fixed_size { enum { ret = (Size == Dynamic) ? Dynamic : Size+1 }; }; } } template bool aux_evalSolver( const POLYNOMIAL& pols, SOLVER& psolve ) { typedef typename POLYNOMIAL::Index Index; typedef typename POLYNOMIAL::Scalar Scalar; typedef typename SOLVER::RootsType RootsType; typedef Matrix EvalRootsType; const Index deg = pols.size()-1; // Test template constructor from coefficient vector SOLVER solve_constr (pols); psolve.compute( pols ); const RootsType& roots( psolve.roots() ); EvalRootsType evr( deg ); for( int i=0; i() ); if( !evalToZero ) { cerr << "WRONG root: " << endl; cerr << "Polynomial: " << pols.transpose() << endl; cerr << "Roots found: " << roots.transpose() << endl; cerr << "Abs value of the polynomial at the roots: " << evr.transpose() << endl; cerr << endl; } std::vector rootModuli( roots.size() ); Map< EvalRootsType > aux( &rootModuli[0], roots.size() ); aux = roots.array().abs(); std::sort( rootModuli.begin(), rootModuli.end() ); bool distinctModuli=true; for( size_t i=1; i void evalSolver( const POLYNOMIAL& pols ) { typedef typename POLYNOMIAL::Scalar Scalar; typedef PolynomialSolver PolynomialSolverType; PolynomialSolverType psolve; aux_evalSolver( pols, psolve ); } template< int Deg, typename POLYNOMIAL, typename ROOTS, typename REAL_ROOTS > void evalSolverSugarFunction( const POLYNOMIAL& pols, const ROOTS& roots, const REAL_ROOTS& real_roots ) { using std::sqrt; typedef typename POLYNOMIAL::Scalar Scalar; typedef PolynomialSolver PolynomialSolverType; PolynomialSolverType psolve; if( aux_evalSolver( pols, psolve ) ) { //It is supposed that // 1) the roots found are correct // 2) the roots have distinct moduli typedef typename POLYNOMIAL::Scalar Scalar; typedef typename REAL_ROOTS::Scalar Real; //Test realRoots std::vector< Real > calc_realRoots; psolve.realRoots( calc_realRoots ); VERIFY( calc_realRoots.size() == (size_t)real_roots.size() ); const Scalar psPrec = sqrt( test_precision() ); for( size_t i=0; i 0 ) ); if( hasRealRoot ){ VERIFY( internal::isApprox( real_roots.array().abs().maxCoeff(), abs(r), psPrec ) ); } //Test absSmallestRealRoot r = psolve.absSmallestRealRoot( hasRealRoot ); VERIFY( hasRealRoot == (real_roots.size() > 0 ) ); if( hasRealRoot ){ VERIFY( internal::isApprox( real_roots.array().abs().minCoeff(), abs( r ), psPrec ) ); } //Test greatestRealRoot r = psolve.greatestRealRoot( hasRealRoot ); VERIFY( hasRealRoot == (real_roots.size() > 0 ) ); if( hasRealRoot ){ VERIFY( internal::isApprox( real_roots.array().maxCoeff(), r, psPrec ) ); } //Test smallestRealRoot r = psolve.smallestRealRoot( hasRealRoot ); VERIFY( hasRealRoot == (real_roots.size() > 0 ) ); if( hasRealRoot ){ VERIFY( internal::isApprox( real_roots.array().minCoeff(), r, psPrec ) ); } } } template void polynomialsolver(int deg) { typedef internal::increment_if_fixed_size<_Deg> Dim; typedef Matrix<_Scalar,Dim::ret,1> PolynomialType; typedef Matrix<_Scalar,_Deg,1> EvalRootsType; cout << "Standard cases" << endl; PolynomialType pols = PolynomialType::Random(deg+1); evalSolver<_Deg,PolynomialType>( pols ); cout << "Hard cases" << endl; _Scalar multipleRoot = internal::random<_Scalar>(); EvalRootsType allRoots = EvalRootsType::Constant(deg,multipleRoot); roots_to_monicPolynomial( allRoots, pols ); evalSolver<_Deg,PolynomialType>( pols ); cout << "Test sugar" << endl; EvalRootsType realRoots = EvalRootsType::Random(deg); roots_to_monicPolynomial( realRoots, pols ); evalSolverSugarFunction<_Deg>( pols, realRoots.template cast < std::complex< typename NumTraits<_Scalar>::Real > >(), realRoots ); } void test_polynomialsolver() { for(int i = 0; i < g_repeat; i++) { CALL_SUBTEST_1( (polynomialsolver(1)) ); CALL_SUBTEST_2( (polynomialsolver(2)) ); CALL_SUBTEST_3( (polynomialsolver(3)) ); CALL_SUBTEST_4( (polynomialsolver(4)) ); CALL_SUBTEST_5( (polynomialsolver(5)) ); CALL_SUBTEST_6( (polynomialsolver(6)) ); CALL_SUBTEST_7( (polynomialsolver(7)) ); CALL_SUBTEST_8( (polynomialsolver(8)) ); CALL_SUBTEST_9( (polynomialsolver( internal::random(9,13) )) ); CALL_SUBTEST_10((polynomialsolver( internal::random(9,13) )) ); CALL_SUBTEST_11((polynomialsolver(1)) ); } }