polynomialutils.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2010 Manuel Yguel <manuel.yguel@gmail.com>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #include "main.h"
00026 #include <unsupported/Eigen/Polynomials>
00027 #include <iostream>
00028 
00029 using namespace std;
00030 
00031 namespace Eigen {
00032 namespace internal {
00033 template<int Size>
00034 struct increment_if_fixed_size
00035 {
00036   enum {
00037     ret = (Size == Dynamic) ? Dynamic : Size+1
00038   };
00039 };
00040 }
00041 }
00042 
00043 template<typename _Scalar, int _Deg>
00044 void realRoots_to_monicPolynomial_test(int deg)
00045 {
00046   typedef internal::increment_if_fixed_size<_Deg>            Dim;
00047   typedef Matrix<_Scalar,Dim::ret,1>                  PolynomialType;
00048   typedef Matrix<_Scalar,_Deg,1>                      EvalRootsType;
00049 
00050   PolynomialType pols(deg+1);
00051   EvalRootsType roots = EvalRootsType::Random(deg);
00052   roots_to_monicPolynomial( roots, pols );
00053 
00054   EvalRootsType evr( deg );
00055   for( int i=0; i<roots.size(); ++i ){
00056     evr[i] = std::abs( poly_eval( pols, roots[i] ) ); }
00057 
00058   bool evalToZero = evr.isZero( test_precision<_Scalar>() );
00059   if( !evalToZero ){
00060     cerr << evr.transpose() << endl; }
00061   VERIFY( evalToZero );
00062 }
00063 
00064 template<typename _Scalar> void realRoots_to_monicPolynomial_scalar()
00065 {
00066   CALL_SUBTEST_2( (realRoots_to_monicPolynomial_test<_Scalar,2>(2)) );
00067   CALL_SUBTEST_3( (realRoots_to_monicPolynomial_test<_Scalar,3>(3)) );
00068   CALL_SUBTEST_4( (realRoots_to_monicPolynomial_test<_Scalar,4>(4)) );
00069   CALL_SUBTEST_5( (realRoots_to_monicPolynomial_test<_Scalar,5>(5)) );
00070   CALL_SUBTEST_6( (realRoots_to_monicPolynomial_test<_Scalar,6>(6)) );
00071   CALL_SUBTEST_7( (realRoots_to_monicPolynomial_test<_Scalar,7>(7)) );
00072   CALL_SUBTEST_8( (realRoots_to_monicPolynomial_test<_Scalar,17>(17)) );
00073 
00074   CALL_SUBTEST_9( (realRoots_to_monicPolynomial_test<_Scalar,Dynamic>(
00075           internal::random<int>(18,26) )) );
00076 }
00077 
00078 
00079 
00080 
00081 template<typename _Scalar, int _Deg>
00082 void CauchyBounds(int deg)
00083 {
00084   typedef internal::increment_if_fixed_size<_Deg>            Dim;
00085   typedef Matrix<_Scalar,Dim::ret,1>                  PolynomialType;
00086   typedef Matrix<_Scalar,_Deg,1>                      EvalRootsType;
00087 
00088   PolynomialType pols(deg+1);
00089   EvalRootsType roots = EvalRootsType::Random(deg);
00090   roots_to_monicPolynomial( roots, pols );
00091   _Scalar M = cauchy_max_bound( pols );
00092   _Scalar m = cauchy_min_bound( pols );
00093   _Scalar Max = roots.array().abs().maxCoeff();
00094   _Scalar min = roots.array().abs().minCoeff();
00095   bool eval = (M >= Max) && (m <= min);
00096   if( !eval )
00097   {
00098     cerr << "Roots: " << roots << endl;
00099     cerr << "Bounds: (" << m << ", " << M << ")" << endl;
00100     cerr << "Min,Max: (" << min << ", " << Max << ")" << endl;
00101   }
00102   VERIFY( eval );
00103 }
00104 
00105 template<typename _Scalar> void CauchyBounds_scalar()
00106 {
00107   CALL_SUBTEST_2( (CauchyBounds<_Scalar,2>(2)) );
00108   CALL_SUBTEST_3( (CauchyBounds<_Scalar,3>(3)) );
00109   CALL_SUBTEST_4( (CauchyBounds<_Scalar,4>(4)) );
00110   CALL_SUBTEST_5( (CauchyBounds<_Scalar,5>(5)) );
00111   CALL_SUBTEST_6( (CauchyBounds<_Scalar,6>(6)) );
00112   CALL_SUBTEST_7( (CauchyBounds<_Scalar,7>(7)) );
00113   CALL_SUBTEST_8( (CauchyBounds<_Scalar,17>(17)) );
00114 
00115   CALL_SUBTEST_9( (CauchyBounds<_Scalar,Dynamic>(
00116           internal::random<int>(18,26) )) );
00117 }
00118 
00119 void test_polynomialutils()
00120 {
00121   for(int i = 0; i < g_repeat; i++)
00122   {
00123     realRoots_to_monicPolynomial_scalar<double>();
00124     realRoots_to_monicPolynomial_scalar<float>();
00125     CauchyBounds_scalar<double>();
00126     CauchyBounds_scalar<float>();
00127   }
00128 }


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:32:09