Multivariate polynomials
They are represented by a sorted list of monomials.



1  Implementation

mpoly/MPoly.H

template < class R, class O>  class MPoly
Interface for multivariate polynomials. The type R is the representation of the polynomial (eg. a list of monomial). The type O is the class specifying the order on the monomials.
{
public:
  // Types
  typedef typename R::value_type             monom_t;
  typedef typename monom_t::coeff_t          coeff_t;
  typedef O                                  order_t;
  typedef typename R::iterator               iterator;
  typedef typename R::const_iterator         const_iterator;
  typedef typename R::reverse_iterator       reverse_iterator; 
  typedef typename R::const_reverse_iterator const_reverse_iterator; 
  typedef MPoly                         self_t;
  
  // Constructors
  MPoly():rep(){}
  MPoly(const monom_t & m):rep() { rep.push_back(m); }
  MPoly(const coeff_t & c):rep() { if(c !=0) rep.push_back(monom_t(c));}
  MPoly(const int & i): rep()    { if(i !=0) rep.push_back(monom_t(i));}
  MPoly(int, const monom_t *);
  MPoly(const self_t & P) 
    {TRACE(P);MPOLYNOMIAL::copy(rep, P.rep);}
  MPoly(char *);

  template
  MPoly(const VAL& M) {assign(*this,M.rep);}
  
  // Destructor
  // no dynamic allocation at this level.
  
  // Assignment operators
  self_t & operator =(const self_t &);
  self_t & operator =(int n)             {*this=self_t(n); return *this;}
  self_t & operator =(const monom_t & m) {*this=self_t(m); return *this;}
  template 
  self_t & operator =(const VAL & M)  {assign(*this,M.rep);return *this;}
  
  self_t & operator*=(const coeff_t &);
  self_t & operator/=(const coeff_t &);

  self_t & operator*=(const monom_t &);
  self_t & operator/=(const monom_t &);

  self_t & operator+=(const self_t &);
  self_t & operator-=(const self_t &);
  self_t & operator*=(const self_t &);
  self_t & operator/=(const self_t & Q);

  // Boolean operators and functions
  bool operator==(int n) const;
  bool operator!=(int n) const {return !(*this==n);}

  // Input/Output operator
  friend istream & operator >> <>(istream &, self_t &);
  friend ostream & operator << <>(ostream &, const self_t &);
  
  // Iterator functions
  iterator                begin ()       {return rep.begin(); }
  const_iterator          begin () const {return rep.begin(); }
  reverse_iterator       rbegin ()       {return rep.rbegin();}
  const_reverse_iterator rbegin () const {return rep.rbegin();}

  iterator                  end ()       {return rep.end(); }
  const_iterator            end () const {return rep.end(); }
  reverse_iterator         rend ()       {return rep.rend();} 
  const_reverse_iterator   rend () const {return rep.rend();}
 
  int size() const {return rep.size();}

  coeff_t operator()(const array1d & p);
  
  // Data
  R rep;
};


template < class R, class O>  inline VAL< MPoly< R,O> *>  Diff(const MPoly< R,O>  & p, unsigned i)
Derivative of p with respect to ith variable.

mpoly/MPoly.C


2  Containers for multivariate polynomials

It could be list, vector, ...



3  Example

#include "mpoly.H"

typedef Monom<double, dynamicexp<'y'> > Mon;
typedef MPoly<vector<Mon>, Dlex<Mon> > Pol;

int main (int argc, char **argv)
{
  Pol p0("x1+x2+1;");
  Pol p1("x1^2+x2+1;");
 
  cout<<"p0 ="<<p0<<endl<<",  p1        ="<<p1<<endl;
p0 =y1+y2+(1) , p1 =y12+y2+(1)
  
  Pol p2(p0+p1);        cout<<p2<<endl;
y12+y1+(2)*y2+(2)
  p2+=p1-p0;            cout<<p2<<endl;
(2)*y12+(2)*y2+(2)
  p2 =p0-p1;            cout<<p2<<endl;
(-1)*y12+y1
  p2=p0-(p0+p1);        cout<<p2<<endl;
(-1)*y12+(-1)*y2+(-1)
  p2=p0*p1;             cout<<p2<<endl;
y13+y12*y2+y12+y1*y2+y22+y1+(2)*y2+(1)
  p2=p0 +p1*2;          cout<<p2<<endl;
(2)*y12+y1+(3)*y2+(3)
  p2=p0 -p1*Mon(2);     cout<<p2<<endl;
(-2)*y12+y1+(-1)*y2+(-1)
  p2=(p1+p0)*(p1-p0*2); cout<<p2<<endl;
y14+(-1)*y13+y12*y2+(-1)*y12+(-5)*y1*y2+(-2)*y22+(-5)*y1+(-4)*y2+(-2)

  cout<<(p1+p0)*p1<<endl;
((y12+y2+(1))+(y1+y2+(1)))*(y12+y2+(1))
  // Yes, the operations are performed only when assigned to a variable.
}