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"
|
2010-03-25 10:31:33 +08:00
|
|
|
#include <unsupported/Eigen/Polynomials>
|
2010-03-25 10:21:52 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-08-05 06:41:52 +08:00
|
|
|
template<typename Scalar_, int _Deg>
|
2010-03-25 10:21:52 +08:00
|
|
|
void realRoots_to_monicPolynomial_test(int deg)
|
|
|
|
{
|
2010-10-25 22:15:22 +08:00
|
|
|
typedef internal::increment_if_fixed_size<_Deg> Dim;
|
2021-08-05 06:41:52 +08:00
|
|
|
typedef Matrix<Scalar_,Dim::ret,1> PolynomialType;
|
|
|
|
typedef Matrix<Scalar_,_Deg,1> EvalRootsType;
|
2010-03-25 10:21:52 +08:00
|
|
|
|
|
|
|
PolynomialType pols(deg+1);
|
|
|
|
EvalRootsType roots = EvalRootsType::Random(deg);
|
2010-03-25 10:31:33 +08:00
|
|
|
roots_to_monicPolynomial( roots, pols );
|
2010-03-25 10:21:52 +08:00
|
|
|
|
|
|
|
EvalRootsType evr( deg );
|
|
|
|
for( int i=0; i<roots.size(); ++i ){
|
|
|
|
evr[i] = std::abs( poly_eval( pols, roots[i] ) ); }
|
|
|
|
|
2021-08-05 06:41:52 +08:00
|
|
|
bool evalToZero = evr.isZero( test_precision<Scalar_>() );
|
2010-03-25 10:21:52 +08:00
|
|
|
if( !evalToZero ){
|
|
|
|
cerr << evr.transpose() << endl; }
|
|
|
|
VERIFY( evalToZero );
|
|
|
|
}
|
|
|
|
|
2021-08-05 06:41:52 +08:00
|
|
|
template<typename Scalar_> void realRoots_to_monicPolynomial_scalar()
|
2010-03-25 10:21:52 +08:00
|
|
|
{
|
2021-08-05 06:41:52 +08:00
|
|
|
CALL_SUBTEST_2( (realRoots_to_monicPolynomial_test<Scalar_,2>(2)) );
|
|
|
|
CALL_SUBTEST_3( (realRoots_to_monicPolynomial_test<Scalar_,3>(3)) );
|
|
|
|
CALL_SUBTEST_4( (realRoots_to_monicPolynomial_test<Scalar_,4>(4)) );
|
|
|
|
CALL_SUBTEST_5( (realRoots_to_monicPolynomial_test<Scalar_,5>(5)) );
|
|
|
|
CALL_SUBTEST_6( (realRoots_to_monicPolynomial_test<Scalar_,6>(6)) );
|
|
|
|
CALL_SUBTEST_7( (realRoots_to_monicPolynomial_test<Scalar_,7>(7)) );
|
|
|
|
CALL_SUBTEST_8( (realRoots_to_monicPolynomial_test<Scalar_,17>(17)) );
|
|
|
|
|
|
|
|
CALL_SUBTEST_9( (realRoots_to_monicPolynomial_test<Scalar_,Dynamic>(
|
2010-10-25 22:15:22 +08:00
|
|
|
internal::random<int>(18,26) )) );
|
2010-03-25 10:21:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-08-05 06:41:52 +08:00
|
|
|
template<typename Scalar_, int _Deg>
|
2010-03-25 10:21:52 +08:00
|
|
|
void CauchyBounds(int deg)
|
|
|
|
{
|
2010-10-25 22:15:22 +08:00
|
|
|
typedef internal::increment_if_fixed_size<_Deg> Dim;
|
2021-08-05 06:41:52 +08:00
|
|
|
typedef Matrix<Scalar_,Dim::ret,1> PolynomialType;
|
|
|
|
typedef Matrix<Scalar_,_Deg,1> EvalRootsType;
|
2010-03-25 10:21:52 +08:00
|
|
|
|
|
|
|
PolynomialType pols(deg+1);
|
|
|
|
EvalRootsType roots = EvalRootsType::Random(deg);
|
2010-03-25 10:31:33 +08:00
|
|
|
roots_to_monicPolynomial( roots, pols );
|
2021-08-05 06:41:52 +08:00
|
|
|
Scalar_ M = cauchy_max_bound( pols );
|
|
|
|
Scalar_ m = cauchy_min_bound( pols );
|
|
|
|
Scalar_ Max = roots.array().abs().maxCoeff();
|
|
|
|
Scalar_ min = roots.array().abs().minCoeff();
|
2010-03-25 10:21:52 +08:00
|
|
|
bool eval = (M >= Max) && (m <= min);
|
|
|
|
if( !eval )
|
|
|
|
{
|
|
|
|
cerr << "Roots: " << roots << endl;
|
|
|
|
cerr << "Bounds: (" << m << ", " << M << ")" << endl;
|
|
|
|
cerr << "Min,Max: (" << min << ", " << Max << ")" << endl;
|
|
|
|
}
|
|
|
|
VERIFY( eval );
|
|
|
|
}
|
|
|
|
|
2021-08-05 06:41:52 +08:00
|
|
|
template<typename Scalar_> void CauchyBounds_scalar()
|
2010-03-25 10:21:52 +08:00
|
|
|
{
|
2021-08-05 06:41:52 +08:00
|
|
|
CALL_SUBTEST_2( (CauchyBounds<Scalar_,2>(2)) );
|
|
|
|
CALL_SUBTEST_3( (CauchyBounds<Scalar_,3>(3)) );
|
|
|
|
CALL_SUBTEST_4( (CauchyBounds<Scalar_,4>(4)) );
|
|
|
|
CALL_SUBTEST_5( (CauchyBounds<Scalar_,5>(5)) );
|
|
|
|
CALL_SUBTEST_6( (CauchyBounds<Scalar_,6>(6)) );
|
|
|
|
CALL_SUBTEST_7( (CauchyBounds<Scalar_,7>(7)) );
|
|
|
|
CALL_SUBTEST_8( (CauchyBounds<Scalar_,17>(17)) );
|
|
|
|
|
|
|
|
CALL_SUBTEST_9( (CauchyBounds<Scalar_,Dynamic>(
|
2010-10-25 22:15:22 +08:00
|
|
|
internal::random<int>(18,26) )) );
|
2010-03-25 10:21:52 +08:00
|
|
|
}
|
|
|
|
|
2018-07-17 20:46:15 +08:00
|
|
|
EIGEN_DECLARE_TEST(polynomialutils)
|
2010-03-25 10:21:52 +08:00
|
|
|
{
|
|
|
|
for(int i = 0; i < g_repeat; i++)
|
|
|
|
{
|
|
|
|
realRoots_to_monicPolynomial_scalar<double>();
|
|
|
|
realRoots_to_monicPolynomial_scalar<float>();
|
|
|
|
CauchyBounds_scalar<double>();
|
|
|
|
CauchyBounds_scalar<float>();
|
|
|
|
}
|
|
|
|
}
|