2010-03-25 10:21:52 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
// for linear algebra.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2010 Manuel Yguel <manuel.yguel@gmail.com>
|
|
|
|
//
|
2012-07-14 02:42:47 +08:00
|
|
|
// 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/.
|
2010-03-25 10:21:52 +08:00
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include <unsupported/Eigen/Polynomials>
|
|
|
|
#include <iostream>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2010-10-25 22:15:22 +08:00
|
|
|
namespace Eigen {
|
|
|
|
namespace internal {
|
2010-03-25 10:21:52 +08:00
|
|
|
template<int Size>
|
2010-10-25 22:15:22 +08:00
|
|
|
struct increment_if_fixed_size
|
2010-03-25 10:21:52 +08:00
|
|
|
{
|
|
|
|
enum {
|
|
|
|
ret = (Size == Dynamic) ? Dynamic : Size+1
|
|
|
|
};
|
|
|
|
};
|
2010-10-25 22:15:22 +08:00
|
|
|
}
|
|
|
|
}
|
2010-03-25 10:21:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
template<int Deg, typename POLYNOMIAL, typename SOLVER>
|
|
|
|
bool aux_evalSolver( const POLYNOMIAL& pols, SOLVER& psolve )
|
|
|
|
{
|
2010-06-21 03:44:25 +08:00
|
|
|
typedef typename POLYNOMIAL::Index Index;
|
2010-03-25 10:21:52 +08:00
|
|
|
typedef typename POLYNOMIAL::Scalar Scalar;
|
|
|
|
|
|
|
|
typedef typename SOLVER::RootsType RootsType;
|
|
|
|
typedef Matrix<Scalar,Deg,1> EvalRootsType;
|
|
|
|
|
2010-06-21 03:44:25 +08:00
|
|
|
const Index deg = pols.size()-1;
|
2010-03-25 10:21:52 +08:00
|
|
|
|
|
|
|
psolve.compute( pols );
|
|
|
|
const RootsType& roots( psolve.roots() );
|
|
|
|
EvalRootsType evr( deg );
|
|
|
|
for( int i=0; i<roots.size(); ++i ){
|
|
|
|
evr[i] = std::abs( poly_eval( pols, roots[i] ) ); }
|
|
|
|
|
|
|
|
bool evalToZero = evr.isZero( test_precision<Scalar>() );
|
|
|
|
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<Scalar> 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<rootModuli.size() && distinctModuli; ++i )
|
|
|
|
{
|
2010-10-25 22:15:22 +08:00
|
|
|
if( internal::isApprox( rootModuli[i], rootModuli[i-1] ) ){
|
2010-03-25 10:21:52 +08:00
|
|
|
distinctModuli = false; }
|
|
|
|
}
|
|
|
|
VERIFY( evalToZero || !distinctModuli );
|
|
|
|
|
|
|
|
return distinctModuli;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<int Deg, typename POLYNOMIAL>
|
|
|
|
void evalSolver( const POLYNOMIAL& pols )
|
|
|
|
{
|
|
|
|
typedef typename POLYNOMIAL::Scalar Scalar;
|
|
|
|
|
|
|
|
typedef PolynomialSolver<Scalar, Deg > PolynomialSolverType;
|
|
|
|
|
|
|
|
PolynomialSolverType psolve;
|
|
|
|
aux_evalSolver<Deg, POLYNOMIAL, PolynomialSolverType>( 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 )
|
|
|
|
{
|
2012-11-06 22:25:50 +08:00
|
|
|
using std::sqrt;
|
2010-03-25 10:21:52 +08:00
|
|
|
typedef typename POLYNOMIAL::Scalar Scalar;
|
|
|
|
|
|
|
|
typedef PolynomialSolver<Scalar, Deg > PolynomialSolverType;
|
|
|
|
|
|
|
|
PolynomialSolverType psolve;
|
|
|
|
if( aux_evalSolver<Deg, POLYNOMIAL, PolynomialSolverType>( 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() );
|
|
|
|
|
2012-11-06 22:25:50 +08:00
|
|
|
const Scalar psPrec = sqrt( test_precision<Scalar>() );
|
2010-03-25 10:21:52 +08:00
|
|
|
|
|
|
|
for( size_t i=0; i<calc_realRoots.size(); ++i )
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
for( size_t j=0; j<calc_realRoots.size()&& !found; ++j )
|
|
|
|
{
|
2014-02-08 18:40:51 +08:00
|
|
|
if( internal::isApprox( calc_realRoots[i], real_roots[j], psPrec ) ){
|
2010-03-25 10:21:52 +08:00
|
|
|
found = true; }
|
|
|
|
}
|
|
|
|
VERIFY( found );
|
|
|
|
}
|
|
|
|
|
|
|
|
//Test greatestRoot
|
2010-10-25 22:15:22 +08:00
|
|
|
VERIFY( internal::isApprox( roots.array().abs().maxCoeff(),
|
2012-11-06 22:25:50 +08:00
|
|
|
abs( psolve.greatestRoot() ), psPrec ) );
|
2010-03-25 10:21:52 +08:00
|
|
|
|
|
|
|
//Test smallestRoot
|
2010-10-25 22:15:22 +08:00
|
|
|
VERIFY( internal::isApprox( roots.array().abs().minCoeff(),
|
2012-11-06 22:25:50 +08:00
|
|
|
abs( psolve.smallestRoot() ), psPrec ) );
|
2010-03-25 10:21:52 +08:00
|
|
|
|
|
|
|
bool hasRealRoot;
|
|
|
|
//Test absGreatestRealRoot
|
|
|
|
Real r = psolve.absGreatestRealRoot( hasRealRoot );
|
|
|
|
VERIFY( hasRealRoot == (real_roots.size() > 0 ) );
|
|
|
|
if( hasRealRoot ){
|
2012-11-06 22:25:50 +08:00
|
|
|
VERIFY( internal::isApprox( real_roots.array().abs().maxCoeff(), abs(r), psPrec ) ); }
|
2010-03-25 10:21:52 +08:00
|
|
|
|
|
|
|
//Test absSmallestRealRoot
|
|
|
|
r = psolve.absSmallestRealRoot( hasRealRoot );
|
|
|
|
VERIFY( hasRealRoot == (real_roots.size() > 0 ) );
|
|
|
|
if( hasRealRoot ){
|
2012-11-06 22:25:50 +08:00
|
|
|
VERIFY( internal::isApprox( real_roots.array().abs().minCoeff(), abs( r ), psPrec ) ); }
|
2010-03-25 10:21:52 +08:00
|
|
|
|
|
|
|
//Test greatestRealRoot
|
|
|
|
r = psolve.greatestRealRoot( hasRealRoot );
|
|
|
|
VERIFY( hasRealRoot == (real_roots.size() > 0 ) );
|
|
|
|
if( hasRealRoot ){
|
2010-10-25 22:15:22 +08:00
|
|
|
VERIFY( internal::isApprox( real_roots.array().maxCoeff(), r, psPrec ) ); }
|
2010-03-25 10:21:52 +08:00
|
|
|
|
|
|
|
//Test smallestRealRoot
|
|
|
|
r = psolve.smallestRealRoot( hasRealRoot );
|
|
|
|
VERIFY( hasRealRoot == (real_roots.size() > 0 ) );
|
|
|
|
if( hasRealRoot ){
|
2010-10-25 22:15:22 +08:00
|
|
|
VERIFY( internal::isApprox( real_roots.array().minCoeff(), r, psPrec ) ); }
|
2010-03-25 10:21:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename _Scalar, int _Deg>
|
|
|
|
void polynomialsolver(int deg)
|
|
|
|
{
|
2010-10-25 22:15:22 +08:00
|
|
|
typedef internal::increment_if_fixed_size<_Deg> Dim;
|
2010-03-25 10:21:52 +08:00
|
|
|
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;
|
2010-10-25 22:15:22 +08:00
|
|
|
_Scalar multipleRoot = internal::random<_Scalar>();
|
2010-03-25 10:21:52 +08:00
|
|
|
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++)
|
|
|
|
{
|
2010-10-25 22:15:22 +08:00
|
|
|
CALL_SUBTEST_1( (polynomialsolver<float,1>(1)) );
|
|
|
|
CALL_SUBTEST_2( (polynomialsolver<double,2>(2)) );
|
|
|
|
CALL_SUBTEST_3( (polynomialsolver<double,3>(3)) );
|
|
|
|
CALL_SUBTEST_4( (polynomialsolver<float,4>(4)) );
|
|
|
|
CALL_SUBTEST_5( (polynomialsolver<double,5>(5)) );
|
|
|
|
CALL_SUBTEST_6( (polynomialsolver<float,6>(6)) );
|
|
|
|
CALL_SUBTEST_7( (polynomialsolver<float,7>(7)) );
|
|
|
|
CALL_SUBTEST_8( (polynomialsolver<double,8>(8)) );
|
|
|
|
|
|
|
|
CALL_SUBTEST_9( (polynomialsolver<float,Dynamic>(
|
2011-01-06 02:49:13 +08:00
|
|
|
internal::random<int>(9,13)
|
2010-10-25 22:15:22 +08:00
|
|
|
)) );
|
|
|
|
CALL_SUBTEST_10((polynomialsolver<double,Dynamic>(
|
2011-01-06 02:49:13 +08:00
|
|
|
internal::random<int>(9,13)
|
2010-10-25 22:15:22 +08:00
|
|
|
)) );
|
2010-03-25 10:21:52 +08:00
|
|
|
}
|
|
|
|
}
|