core/vnl/algo/vnl_cpoly_roots.cxx
Go to the documentation of this file.
00001 /*
00002   fsm
00003 */
00004 #include "vnl_cpoly_roots.h"
00005 #include <vcl_cassert.h>
00006 #include <vnl/algo/vnl_complex_eigensystem.h>
00007 
00008 void vnl_cpoly_roots::compute(vnl_vector<vcl_complex<double> > const &a)
00009 {
00010   // construct companion matrix
00011   vnl_matrix<vcl_complex<double> > comp(N, N);
00012   comp.fill(0);
00013   for (unsigned i=0; i<N-1; ++i)
00014     comp(i+1, i) = 1;
00015   for (unsigned i=0; i<N; ++i)
00016     comp(i, N-1) = -a[N-1-i];
00017 
00018   // the eigenvalues of the companion matrix are the roots of the polynomial
00019   solns = vnl_complex_eigensystem(comp,
00020                                   false,    // we only want
00021                                   false).W; // the eigenvalues.
00022 #ifdef DEBUG
00023   vcl_cerr << "s = " << solns << '\n';
00024 #endif
00025 }
00026 
00027 vnl_cpoly_roots::vnl_cpoly_roots(vnl_vector<vcl_complex<double> > const & a)
00028   : solns(a.size())
00029   , N(a.size()) // degree
00030 {
00031   compute(a);
00032 }
00033 
00034 vnl_cpoly_roots::vnl_cpoly_roots(vnl_vector<double> const & a_real,
00035                                  vnl_vector<double> const & a_imag)
00036   : solns(a_real.size())
00037   , N(a_real.size()) // degree
00038 {
00039   assert(a_real.size() == a_imag.size());
00040   vnl_vector<vcl_complex<double> > a(N);
00041   for (unsigned i=0; i<N; ++i)
00042     a[i] = vcl_complex<double>(a_real[i], a_imag[i]);
00043 
00044 #ifdef DEBUG
00045   vcl_cerr << "a = " << a << '\n';
00046 #endif
00047   compute(a);
00048 }