00001
00022
00023
00024
00025
00026 #ifndef __SYNFIG_POLYNOMIAL_ROOT_H
00027 #define __SYNFIG_POLYNOMIAL_ROOT_H
00028
00029
00030
00031 #include <complex>
00032 #include <vector>
00033
00034
00035
00036
00037
00038
00039 template < typename T = float, typename F = float >
00040 class Polynomial : public std::vector<T>
00041 {
00042 public:
00043
00044
00045 void degree(unsigned int d, const T & def = (T)0) { resize(d+1,def); }
00046 unsigned int degree()const { return this->size() - 1; }
00047
00048 const Polynomial & operator+=(const Polynomial &p)
00049 {
00050 if(p.size() > this->size())
00051 resize(p.size(), (T)0);
00052
00053 for(int i = 0; i < p.size(); ++i)
00054 {
00055 (*this)[i] += p[i];
00056 }
00057 return *this;
00058 }
00059
00060 const Polynomial & operator-=(const Polynomial &p)
00061 {
00062 if(p.size() > this->size())
00063 resize(p.size(), (T)0);
00064
00065 for(int i = 0; i < p.size(); ++i)
00066 {
00067 (*this)[i] -= p[i];
00068 }
00069 return *this;
00070 }
00071
00072 const Polynomial & operator*=(const Polynomial &p)
00073 {
00074 if(p.size() < 1)
00075 {
00076 this->resize(0);
00077 return *this;
00078 }
00079
00080 unsigned int i,j;
00081 std::vector<T> nc(*this);
00082
00083
00084 for(i = 0; i < nc.size(); ++i)
00085 {
00086 (*this)[i] *= p[0];
00087 }
00088
00089 if(p.size() < 2) return *this;
00090
00091 this->resize(this->size() + p.degree());
00092 for(int i = 0; i < nc.size(); ++i)
00093 {
00094 for(int j = 1; j < p.size(); ++j)
00095 {
00096 nc[i+j] += nc[i]*p[j];
00097 }
00098 }
00099
00100 return *this;
00101 }
00102 };
00103
00104 class RootFinder
00105 {
00106 std::vector< std::complex<float> > workcoefs;
00107 int its;
00108
00109 public:
00110 std::vector< std::complex<float> > coefs;
00111
00112 std::vector< std::complex<float> > roots;
00113
00114 void find_all_roots(bool polish);
00115 };
00116
00117
00118
00119
00120
00121 #endif