00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "main.h"
00011 #include <unsupported/Eigen/Polynomials>
00012 #include <iostream>
00013 #include <algorithm>
00014
00015 using namespace std;
00016
00017 namespace Eigen {
00018 namespace internal {
00019 template<int Size>
00020 struct increment_if_fixed_size
00021 {
00022 enum {
00023 ret = (Size == Dynamic) ? Dynamic : Size+1
00024 };
00025 };
00026 }
00027 }
00028
00029
00030 template<int Deg, typename POLYNOMIAL, typename SOLVER>
00031 bool aux_evalSolver( const POLYNOMIAL& pols, SOLVER& psolve )
00032 {
00033 typedef typename POLYNOMIAL::Index Index;
00034 typedef typename POLYNOMIAL::Scalar Scalar;
00035
00036 typedef typename SOLVER::RootsType RootsType;
00037 typedef Matrix<Scalar,Deg,1> EvalRootsType;
00038
00039 const Index deg = pols.size()-1;
00040
00041 psolve.compute( pols );
00042 const RootsType& roots( psolve.roots() );
00043 EvalRootsType evr( deg );
00044 for( int i=0; i<roots.size(); ++i ){
00045 evr[i] = std::abs( poly_eval( pols, roots[i] ) ); }
00046
00047 bool evalToZero = evr.isZero( test_precision<Scalar>() );
00048 if( !evalToZero )
00049 {
00050 cerr << "WRONG root: " << endl;
00051 cerr << "Polynomial: " << pols.transpose() << endl;
00052 cerr << "Roots found: " << roots.transpose() << endl;
00053 cerr << "Abs value of the polynomial at the roots: " << evr.transpose() << endl;
00054 cerr << endl;
00055 }
00056
00057 std::vector<Scalar> rootModuli( roots.size() );
00058 Map< EvalRootsType > aux( &rootModuli[0], roots.size() );
00059 aux = roots.array().abs();
00060 std::sort( rootModuli.begin(), rootModuli.end() );
00061 bool distinctModuli=true;
00062 for( size_t i=1; i<rootModuli.size() && distinctModuli; ++i )
00063 {
00064 if( internal::isApprox( rootModuli[i], rootModuli[i-1] ) ){
00065 distinctModuli = false; }
00066 }
00067 VERIFY( evalToZero || !distinctModuli );
00068
00069 return distinctModuli;
00070 }
00071
00072
00073
00074
00075
00076
00077
00078 template<int Deg, typename POLYNOMIAL>
00079 void evalSolver( const POLYNOMIAL& pols )
00080 {
00081 typedef typename POLYNOMIAL::Scalar Scalar;
00082
00083 typedef PolynomialSolver<Scalar, Deg > PolynomialSolverType;
00084
00085 PolynomialSolverType psolve;
00086 aux_evalSolver<Deg, POLYNOMIAL, PolynomialSolverType>( pols, psolve );
00087 }
00088
00089
00090
00091
00092 template< int Deg, typename POLYNOMIAL, typename ROOTS, typename REAL_ROOTS >
00093 void evalSolverSugarFunction( const POLYNOMIAL& pols, const ROOTS& roots, const REAL_ROOTS& real_roots )
00094 {
00095 using std::sqrt;
00096 typedef typename POLYNOMIAL::Scalar Scalar;
00097
00098 typedef PolynomialSolver<Scalar, Deg > PolynomialSolverType;
00099
00100 PolynomialSolverType psolve;
00101 if( aux_evalSolver<Deg, POLYNOMIAL, PolynomialSolverType>( pols, psolve ) )
00102 {
00103
00104
00105
00106
00107 typedef typename REAL_ROOTS::Scalar Real;
00108
00109
00110 std::vector< Real > calc_realRoots;
00111 psolve.realRoots( calc_realRoots );
00112 VERIFY( calc_realRoots.size() == (size_t)real_roots.size() );
00113
00114 const Scalar psPrec = sqrt( test_precision<Scalar>() );
00115
00116 for( size_t i=0; i<calc_realRoots.size(); ++i )
00117 {
00118 bool found = false;
00119 for( size_t j=0; j<calc_realRoots.size()&& !found; ++j )
00120 {
00121 if( internal::isApprox( calc_realRoots[i], real_roots[j] ), psPrec ){
00122 found = true; }
00123 }
00124 VERIFY( found );
00125 }
00126
00127
00128 VERIFY( internal::isApprox( roots.array().abs().maxCoeff(),
00129 abs( psolve.greatestRoot() ), psPrec ) );
00130
00131
00132 VERIFY( internal::isApprox( roots.array().abs().minCoeff(),
00133 abs( psolve.smallestRoot() ), psPrec ) );
00134
00135 bool hasRealRoot;
00136
00137 Real r = psolve.absGreatestRealRoot( hasRealRoot );
00138 VERIFY( hasRealRoot == (real_roots.size() > 0 ) );
00139 if( hasRealRoot ){
00140 VERIFY( internal::isApprox( real_roots.array().abs().maxCoeff(), abs(r), psPrec ) ); }
00141
00142
00143 r = psolve.absSmallestRealRoot( hasRealRoot );
00144 VERIFY( hasRealRoot == (real_roots.size() > 0 ) );
00145 if( hasRealRoot ){
00146 VERIFY( internal::isApprox( real_roots.array().abs().minCoeff(), abs( r ), psPrec ) ); }
00147
00148
00149 r = psolve.greatestRealRoot( hasRealRoot );
00150 VERIFY( hasRealRoot == (real_roots.size() > 0 ) );
00151 if( hasRealRoot ){
00152 VERIFY( internal::isApprox( real_roots.array().maxCoeff(), r, psPrec ) ); }
00153
00154
00155 r = psolve.smallestRealRoot( hasRealRoot );
00156 VERIFY( hasRealRoot == (real_roots.size() > 0 ) );
00157 if( hasRealRoot ){
00158 VERIFY( internal::isApprox( real_roots.array().minCoeff(), r, psPrec ) ); }
00159 }
00160 }
00161
00162
00163 template<typename _Scalar, int _Deg>
00164 void polynomialsolver(int deg)
00165 {
00166 typedef internal::increment_if_fixed_size<_Deg> Dim;
00167 typedef Matrix<_Scalar,Dim::ret,1> PolynomialType;
00168 typedef Matrix<_Scalar,_Deg,1> EvalRootsType;
00169
00170 cout << "Standard cases" << endl;
00171 PolynomialType pols = PolynomialType::Random(deg+1);
00172 evalSolver<_Deg,PolynomialType>( pols );
00173
00174 cout << "Hard cases" << endl;
00175 _Scalar multipleRoot = internal::random<_Scalar>();
00176 EvalRootsType allRoots = EvalRootsType::Constant(deg,multipleRoot);
00177 roots_to_monicPolynomial( allRoots, pols );
00178 evalSolver<_Deg,PolynomialType>( pols );
00179
00180 cout << "Test sugar" << endl;
00181 EvalRootsType realRoots = EvalRootsType::Random(deg);
00182 roots_to_monicPolynomial( realRoots, pols );
00183 evalSolverSugarFunction<_Deg>(
00184 pols,
00185 realRoots.template cast <
00186 std::complex<
00187 typename NumTraits<_Scalar>::Real
00188 >
00189 >(),
00190 realRoots );
00191 }
00192
00193 void test_polynomialsolver()
00194 {
00195 for(int i = 0; i < g_repeat; i++)
00196 {
00197 CALL_SUBTEST_1( (polynomialsolver<float,1>(1)) );
00198 CALL_SUBTEST_2( (polynomialsolver<double,2>(2)) );
00199 CALL_SUBTEST_3( (polynomialsolver<double,3>(3)) );
00200 CALL_SUBTEST_4( (polynomialsolver<float,4>(4)) );
00201 CALL_SUBTEST_5( (polynomialsolver<double,5>(5)) );
00202 CALL_SUBTEST_6( (polynomialsolver<float,6>(6)) );
00203 CALL_SUBTEST_7( (polynomialsolver<float,7>(7)) );
00204 CALL_SUBTEST_8( (polynomialsolver<double,8>(8)) );
00205
00206 CALL_SUBTEST_9( (polynomialsolver<float,Dynamic>(
00207 internal::random<int>(9,13)
00208 )) );
00209 CALL_SUBTEST_10((polynomialsolver<double,Dynamic>(
00210 internal::random<int>(9,13)
00211 )) );
00212 }
00213 }