diff --git a/polynomial.cc b/polynomial.cc index 5e0630d..69bb6d6 100644 --- a/polynomial.cc +++ b/polynomial.cc @@ -142,6 +142,22 @@ return p; } +template +T Polynomial::newton(const T x0, const typename LA_traits::normtype thr, const int maxit) const +{ +Polynomial d=derivative(1); +T x=x0; +for(int i=0; i::normtype thr) + void simplify(const typename LA_traits::normtype thr=0) { NOT_GPU(*this); int n=degree(); - while(n>0 && abs((*this)[n])0 && abs((*this)[n])<=thr) --n; resize(n,true); }; void normalize() {if((*this)[degree()]==(T)0) laerror("zero coefficient at highest degree - simplify first"); *this /= (*this)[degree()];}; @@ -103,27 +103,41 @@ public: return r; } } - Polynomial derivative() const + Polynomial derivative(const int d=1) const { + if(d==0) return *this; + if(d<0) return integral(-d); NOT_GPU(*this); int n=degree(); - if(n==0) + if(n-d<0) { Polynomial r(0); r[0]=0; return r; } - Polynomial r(n-1); - for(int i=1; i<=n; ++i) r[i-1] = (*this)[i]* ((T)i); + Polynomial r(n-d); + for(int i=d; i<=n; ++i) + { + int prod=1; + for(int j=i; j>=i-d+1; --j) prod *= j; + r[i-d] = (*this)[i]* ((T)prod); + } return r; }; - Polynomial integral() const + Polynomial integral(const int d=1) const { + if(d==0) return *this; + if(d<0) return derivative(-d); NOT_GPU(*this); int n=degree(); - Polynomial r(n+1); - r[0]=0; - for(int i=0; i<=n; ++i) r[i+1] = (*this)[i]/((T)(i+1)); + Polynomial r(n+d); + for(int i=0; i companion() const; //matrix which has this characteristic polynomial NRVec::complextype> roots() const; //implemented for complex and double only NRVec realroots(const typename LA_traits::normtype thr) const; + T newton(const T x0, const typename LA_traits::normtype thr=1e-14, const int maxit=1000) const; //solve root from the guess //@@@gcd, lcm euler and svd diff --git a/t.cc b/t.cc index 70ae1e5..f426e75 100644 --- a/t.cc +++ b/t.cc @@ -2203,20 +2203,25 @@ cout << (value(p,u)*value(q,u) -value(r,u)).norm()<>n ; NRVec r(n); r.randomize(1.); +double x0=r[0]*0.8+r[1]*0.2; r.sort(0); Polynomial p=polyfromroots(r); cout <>n ; @@ -2227,6 +2232,9 @@ Polynomial p=lagrange_interpolation(x,y); cout < yy=values(p,x); cout<<"interpolation error= "<<(y-yy).norm()<q=p.integral(2); +Polynomialpp=q.derivative(2); +cout<<"test deriv. "<<(pp-p).norm()<