1c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This file is part of Eigen, a lightweight C++ template library
2c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// for linear algebra.
3c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
4c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Copyright (C) 2010 Manuel Yguel <manuel.yguel@gmail.com>
5c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
6c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This Source Code Form is subject to the terms of the Mozilla
7c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Public License v. 2.0. If a copy of the MPL was not distributed
8c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
10c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include "main.h"
11c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include <unsupported/Eigen/Polynomials>
12c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include <iostream>
13c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
14c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathusing namespace std;
15c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
16c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathnamespace Eigen {
17c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathnamespace internal {
18c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<int Size>
19c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct increment_if_fixed_size
20c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
21c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  enum {
22c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    ret = (Size == Dynamic) ? Dynamic : Size+1
23c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  };
24c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
25c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
26c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
27c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
28c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename _Scalar, int _Deg>
29c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathvoid realRoots_to_monicPolynomial_test(int deg)
30c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
31c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef internal::increment_if_fixed_size<_Deg>            Dim;
32c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef Matrix<_Scalar,Dim::ret,1>                  PolynomialType;
33c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef Matrix<_Scalar,_Deg,1>                      EvalRootsType;
34c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
35c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  PolynomialType pols(deg+1);
36c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  EvalRootsType roots = EvalRootsType::Random(deg);
37c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  roots_to_monicPolynomial( roots, pols );
38c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
39c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  EvalRootsType evr( deg );
40c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  for( int i=0; i<roots.size(); ++i ){
41c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    evr[i] = std::abs( poly_eval( pols, roots[i] ) ); }
42c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
43c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  bool evalToZero = evr.isZero( test_precision<_Scalar>() );
44c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  if( !evalToZero ){
45c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    cerr << evr.transpose() << endl; }
46c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VERIFY( evalToZero );
47c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
48c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
49c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename _Scalar> void realRoots_to_monicPolynomial_scalar()
50c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
51c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  CALL_SUBTEST_2( (realRoots_to_monicPolynomial_test<_Scalar,2>(2)) );
52c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  CALL_SUBTEST_3( (realRoots_to_monicPolynomial_test<_Scalar,3>(3)) );
53c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  CALL_SUBTEST_4( (realRoots_to_monicPolynomial_test<_Scalar,4>(4)) );
54c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  CALL_SUBTEST_5( (realRoots_to_monicPolynomial_test<_Scalar,5>(5)) );
55c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  CALL_SUBTEST_6( (realRoots_to_monicPolynomial_test<_Scalar,6>(6)) );
56c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  CALL_SUBTEST_7( (realRoots_to_monicPolynomial_test<_Scalar,7>(7)) );
57c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  CALL_SUBTEST_8( (realRoots_to_monicPolynomial_test<_Scalar,17>(17)) );
58c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
59c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  CALL_SUBTEST_9( (realRoots_to_monicPolynomial_test<_Scalar,Dynamic>(
60c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath          internal::random<int>(18,26) )) );
61c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
62c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
63c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
64c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
65c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
66c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename _Scalar, int _Deg>
67c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathvoid CauchyBounds(int deg)
68c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
69c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef internal::increment_if_fixed_size<_Deg>            Dim;
70c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef Matrix<_Scalar,Dim::ret,1>                  PolynomialType;
71c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef Matrix<_Scalar,_Deg,1>                      EvalRootsType;
72c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
73c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  PolynomialType pols(deg+1);
74c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  EvalRootsType roots = EvalRootsType::Random(deg);
75c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  roots_to_monicPolynomial( roots, pols );
76c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  _Scalar M = cauchy_max_bound( pols );
77c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  _Scalar m = cauchy_min_bound( pols );
78c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  _Scalar Max = roots.array().abs().maxCoeff();
79c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  _Scalar min = roots.array().abs().minCoeff();
80c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  bool eval = (M >= Max) && (m <= min);
81c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  if( !eval )
82c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
83c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    cerr << "Roots: " << roots << endl;
84c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    cerr << "Bounds: (" << m << ", " << M << ")" << endl;
85c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    cerr << "Min,Max: (" << min << ", " << Max << ")" << endl;
86c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
87c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VERIFY( eval );
88c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
89c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
90c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename _Scalar> void CauchyBounds_scalar()
91c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
92c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  CALL_SUBTEST_2( (CauchyBounds<_Scalar,2>(2)) );
93c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  CALL_SUBTEST_3( (CauchyBounds<_Scalar,3>(3)) );
94c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  CALL_SUBTEST_4( (CauchyBounds<_Scalar,4>(4)) );
95c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  CALL_SUBTEST_5( (CauchyBounds<_Scalar,5>(5)) );
96c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  CALL_SUBTEST_6( (CauchyBounds<_Scalar,6>(6)) );
97c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  CALL_SUBTEST_7( (CauchyBounds<_Scalar,7>(7)) );
98c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  CALL_SUBTEST_8( (CauchyBounds<_Scalar,17>(17)) );
99c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
100c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  CALL_SUBTEST_9( (CauchyBounds<_Scalar,Dynamic>(
101c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath          internal::random<int>(18,26) )) );
102c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
103c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
104c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathvoid test_polynomialutils()
105c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
106c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  for(int i = 0; i < g_repeat; i++)
107c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
108c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    realRoots_to_monicPolynomial_scalar<double>();
109c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    realRoots_to_monicPolynomial_scalar<float>();
110c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CauchyBounds_scalar<double>();
111c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CauchyBounds_scalar<float>();
112c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
113c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
114