// Rascal, the Advanced Scientific CALculator // Copyright (C) 2001, 2002, Sebastian Ritterbusch (Rascal@Ritterbusch.de) // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more detauls. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // #ifndef POLY_HPP_INCLUDED #define POLY_HPP_INCLUDED #include template class mpoly { public: T *a; int N; mpoly(void) { a=0;N=0; } mpoly(const T & c) { a=new T[N=1];a[0]=c; } mpoly(const T & c, const T & x) { a=new T[N=2];a[0]=c;a[1]=x; } mpoly(const mpoly & b) { a=new T[N=b.N]; int i; for(i=0;i0 && a[N-1]==T(0);N--); } ~mpoly(void) { delete [] a; } mpoly & operator =(const mpoly & c) { T *n; if(c.N==N) n=a; else n=new T[N=c.N]; int i; for(i=0;i0 && a[N-1]==T(0);N--); return *this; } int deg(void) const { int n=N; for(;n>0 && a[n-1]==T(0);n--); return n>0?n-1:0; } int deg(void) { for(;N>0 && a[N-1]==T(0);N--); return N>0?N-1:0; } mpoly & foreach(T (*f)(const T &)) { int i; for(i=0;i & foreach(T (*f)(T)) { int i; for(i=0;i foreach(T (*f)(const T &)) const { int i; mpoly temp(*this); for(i=0;i foreach(T (*f)(T)) const { int i; mpoly temp(*this); for(i=0;i & operator +=(const mpoly & b) { ensure(b.N); int i; for(i=0;i0 && a[N-1]==T(0);N--); return *this; } mpoly & operator -=(const mpoly & b) { ensure(b.N); int i; for(i=0;i0 && a[N-1]==T(0);N--); return *this; } mpoly & operator *=(const mpoly & b) { int n; T *re=new T[n=b.N+N-1]; int i,j; for(i=0;i=0;i--) for(j=b.N-1;j>=0;j--) re[i+j]=re[i+j]+a[i]*b.a[j]; delete [] a; a=re; N=n; for(;N>0 && a[N-1]==T(0);N--); return *this; } T operator()(const value & x) const { T res=a[N-1]; int i; for(i=N-2;i>=0;i--) res=res*x+a[i]; return res; } friend mpoly operator +(mpoly a,const mpoly &b) { return a+=b; } friend mpoly operator -(mpoly a,const mpoly &b) { return a-=b; } friend mpoly operator *(mpoly a,const mpoly &b) { return a*=b; } friend bool operator ==(const mpoly & a,const mpoly & b) { int i; for(i=0;i & a,const mpoly & b) { int i; for(i=0;i power(mpoly a,int e) { mpoly res(T(1)); while(e) { if(e&1) res*=a; e>>=1; if(e) a*=a; } return res; } friend mpoly deriv(mpoly a) { int i; for(i=1;i0) a.N--; return a; } }; #endif