source: git/kernel/Poly.h @ ef1452

spielwiese
Last change on this file since ef1452 was ef1452, checked in by Michael Brickenstein <bricken@…>, 19 years ago
*bricken: +iterators git-svn-id: file:///usr/local/Singular/svn/trunk@8508 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 9.0 KB
Line 
1//$Id: Poly.h,v 1.7 2005-08-16 14:36:10 bricken Exp $
2
3
4
5#ifndef POLYCPP_HEADER
6#define POLYCPP_HEADER
7#include "mod2.h"
8
9#include "numbers.h"
10#include "Number.h"
11#include "febase.h"
12#include "polys.h"
13#include "ring.h"
14
15
16#include <boost/shared_ptr.hpp>
17
18#include <vector>
19
20#define BOOST_DISABLE_THREADS
21
22
23//PolyImpl is a 08/15 poly wrapper
24//Poly wraps around PolyImpl with reference counting using boost
25class PolyImpl{
26  friend class Poly;
27  //friend class Number;
28 protected:
29  poly getInternalReference() const{
30    return p;
31  }
32 public:
33  ring getRing() const{
34    return r;
35  }
36  friend PolyImpl operator+(const PolyImpl& p1, const PolyImpl& n2);
37  friend PolyImpl operator-(const PolyImpl& p1, const PolyImpl& n2);
38  friend PolyImpl operator/(const PolyImpl& p1, const PolyImpl& n2);
39  friend PolyImpl operator*(const PolyImpl& p1, const PolyImpl& n2);
40  friend bool operator==(const PolyImpl& p1, const PolyImpl& n2);
41  friend PolyImpl operator+(const PolyImpl& p1, int n2);
42  friend PolyImpl operator-(const PolyImpl& p1, int n2);
43  friend PolyImpl operator/(const PolyImpl& p1, int n2);
44  friend PolyImpl operator*(const PolyImpl& p1, int n2);
45  friend bool operator==(const PolyImpl& p1, int n2);
46  PolyImpl& operator=(const PolyImpl& p2){
47    //durch Reihenfolge Selbstzuweisungen berücksichtigt
48    if (this==&p2) return *this;
49    poly pc=p_Copy(p2.p,p2.r);
50    p_Delete(&p,r);
51    r=p2.r;
52    p=pc;
53    return *this;
54  }
55  PolyImpl operator-(){
56    PolyImpl t(*this);
57    t.p=p_Copy(p,r);
58    t.p=p_Neg(t.p,r);
59    return t;
60  }
61  PolyImpl& operator+=(const PolyImpl & p2){
62    if (r!=p2.r){
63      Werror("not the same ring");
64      return *this;
65    }
66    if (this==&p2){
67      number two=n_Init(2,r);
68      p_Mult_nn(p,two,r);
69      return *this;
70    }
71    p=p_Add_q(p,p_Copy(p2.p,p2.r),r);
72   
73    return *this;
74  }
75  PolyImpl& operator*=(const PolyImpl & p2){
76    if (r!=p2.r){
77      Werror("not the same ring");
78      return *this;
79    }
80    if (this==&p2){
81      poly pc=p_Copy(p,r);
82      p=p_Mult_q(p,p2.p,r);
83      return *this;
84    }
85    p=p_Mult_q(p,p_Copy(p2.p,p2.r),r);
86    return *this;
87  }
88  PolyImpl& operator*=(const Number & n){
89    if (r!=n.r){
90      Werror("not the same ring");
91      return *this;
92    }
93   
94    p=p_Mult_nn(p,n.n,r);
95    return *this;
96  }
97  PolyImpl& operator-=(const PolyImpl & p2){
98    if (r!=p2.r){
99      Werror("not the same ring");
100      return *this;
101    }
102    if (this==&p2){
103      p_Delete(&p,r);
104      p=NULL;
105      return *this;
106    }
107
108    poly pc=p_Copy(p2.p,p2.r);
109    pc=p_Neg(pc,r);
110    p=p_Add_q(p,pc,r);
111
112   
113    return *this;
114  }
115  //Div not available for rings other than currRing
116/*   PolyImpl& operator/=(const PolyImpl & p2){ */
117/*     if (r!=p2.r){ */
118/*       Werror("not the same ring"); */
119/*       return *this; */
120/*     } */
121/*     if (this==&p2){ */
122/*       poly one=p_ISet(1,r); */
123/*       p_Delete(&p,r); */
124/*       p=one; */
125/*       return *this; */
126/*     } */
127/*     number nv=n_Div(n,p2.n,r); */
128/*     n_Delete(&n,r); */
129/*     n=nv; */
130/*     return *this; */
131/*   } */
132
133
134
135
136
137
138
139
140
141
142  PolyImpl& operator=(int n){
143 
144    p_Delete(&p,r);
145    p=p_ISet(n,r);
146    return *this;
147 
148  }
149 
150
151  PolyImpl(){
152    r=currRing;
153    p=NULL;
154  }
155  PolyImpl(const PolyImpl & p){
156    r=p.r;
157    this->p=p_Copy(p.p,r);
158  }
159  PolyImpl(poly p, ring r){
160    this->p=p_Copy(p,r);
161    this->r=r;
162  }
163  PolyImpl(poly p, ring r,int){
164    this->p=p;
165    this->r=r;
166  }
167  PolyImpl(int n, ring r){
168    this->p=p_ISet(n,r);
169    this->r=r;
170  }
171  PolyImpl(const Number & n){
172   
173    r=n.r;
174    this->p=p_NSet(n_Copy(n.n,r),r);
175   
176  }
177  explicit PolyImpl(int n){
178    r=currRing;
179    this->p=p_ISet(n,r);
180  }
181  void print(){
182    p_Write(p,r,r);
183  }
184
185  virtual ~PolyImpl(){
186    p_Delete(&p,r);
187  }
188
189 protected:
190  poly p;
191  ring r;
192
193};
194
195PolyImpl operator+(const PolyImpl &p1, const PolyImpl& p2){
196  PolyImpl erg(p1);
197  erg+=p2;
198  return erg;
199}
200PolyImpl operator*(const PolyImpl &p1, const PolyImpl& p2){
201  PolyImpl erg(p1);
202  erg*=p2;
203  return erg;
204}
205PolyImpl operator-(const PolyImpl &p1, const PolyImpl& p2){
206  PolyImpl erg(p1);
207  erg-=p2;
208  return erg;
209}
210/*PolyImpl operator/(const PolyImpl &p1, const PolyImpl& p2){
211  PolyImpl erg(p1);
212  erg/=p2;
213  return erg;
214  }*/
215/*
216bool operator==(const PolyImpl &p1, const PolyImpl& p2){
217  if(p1.r!=p2.r)
218    return false;
219  return n_Equal(p1.n,p2.n,p1.r);
220  }*/
221//Equal Polys not available for oth. rings than currRing
222
223
224PolyImpl operator+(const PolyImpl &p1, int p2){
225  PolyImpl erg(p1);
226  erg+=PolyImpl(p2,p1.r);
227  return erg;
228}
229PolyImpl operator*(const PolyImpl &p1, int p2){
230  PolyImpl erg(p1);
231  erg*=PolyImpl(p2,p1.r);
232  return erg;
233}
234PolyImpl operator-(const PolyImpl &p1, int p2){
235  PolyImpl erg(p1);
236  erg-=PolyImpl(p2,p1.r);
237  return erg;
238}
239/*PolyImpl operator/(const PolyImpl &p1, int p2){
240  PolyImpl erg(p1);
241  erg/=PolyImpl(p2,p1.r);
242  return erg;
243  }*/
244
245/*bool operator==(const PolyImpl &p1, int p2){
246  return n_Equal(p1.n,PolyImpl(p2,p1.r).n,p1.r);
247  }*/
248PolyImpl operator+(int p1, const PolyImpl& p2){
249  PolyImpl erg(p2);
250  return erg+=PolyImpl(p1,p2.getRing());
251}
252/*PolyImpl operator-(int p1, const PolyImpl& p2){
253
254  PolyImpl erg(p1,p2.r);
255  return erg-=p2;
256  }*/
257/*PolyImpl operator/(int p1, const PolyImpl& p2){
258  PolyImpl erg(p1,p2.r);
259  return erg/=p2;
260  }*/
261
262PolyImpl operator*(int p1, const PolyImpl& p2){
263  PolyImpl erg(p2);
264  return erg*=PolyImpl(p1,p2.getRing());
265}
266/*bool operator==(int p1, const PolyImpl& p2){
267  return p2==PolyImpl(p1,p2.r);
268  }*/
269using namespace boost;
270
271
272template<class T> class ConstTermReference{
273 private:
274  ring r;
275  poly t;
276 public:
277  operator T() const {
278    return T(p_Head(t,r),r);
279  }
280  ConstTermReference(poly p, ring r){
281    this->t=p;
282    this->r=r;
283  }
284 
285};
286template<class T> class PolyInputIterator:
287public std::iterator<std::input_iterator_tag,T,int, shared_ptr<const T>,ConstTermReference<T> >
288{
289
290 
291 private:
292  poly t;
293  ring r;
294  public:
295  bool operator==(const PolyInputIterator& t2){
296    return t2.t==t;
297  }
298  bool operator!=(const PolyInputIterator& t2){
299    return t2.t!=t;
300  }
301  PolyInputIterator(poly p, ring r){
302    t=p;
303    this->r=r;
304  }
305  PolyInputIterator(const PolyInputIterator& it){
306    t=it.t;
307    r=it.r;
308  }
309  PolyInputIterator& operator++(){
310    t=t->next;
311    return *this;
312  }
313  PolyInputIterator operator++(int){
314    PolyInputIterator it(*this);
315    ++(*this);
316    return it;
317  }
318  const ConstTermReference<T> operator*(){
319    return ConstTermReference<T> (t,r);
320  }
321  shared_ptr<const T> operator->(){
322    return shared_ptr<const T>(new T(p_Head(t,r),r,0));
323  }
324
325};
326class Poly{
327 
328 public:
329  typedef PolyInputIterator<Poly> iterator;
330  void copy_on_write(){
331    if (!ptr.unique()){
332      ptr.reset(new PolyImpl(*ptr));
333    }
334  }
335  void print() const {
336    ptr->print();
337  }
338  //* ressource managed by Singular
339  char* c_string(){
340
341    return p_String(ptr->p,ptr->r,ptr->r);
342  }
343
344  Poly(ring r=currRing):ptr(new PolyImpl((poly) NULL,r)){
345  }
346  Poly(int n, ring r=currRing):ptr(new PolyImpl(n,r)){
347   
348  }
349  Poly(const char* c, ring r=currRing):ptr(new PolyImpl((poly)NULL,r)){
350    //p_Read takes no const so do
351    char* cp=(char*) omalloc((strlen(c)+1)*sizeof(char));
352    strcpy(cp,c);
353    p_Read(cp,ptr->p,r);
354    omfree(cp);
355  }
356  Poly(const Poly&p):ptr(p.ptr){
357  }
358  Poly(const Number& n):ptr(new PolyImpl(n)){
359   
360  }
361  Poly(std::vector<int> v, ring r=currRing):ptr(new PolyImpl((poly) NULL,r)){
362    unsigned int i;
363    int s=v.size();
364    poly p=p_ISet(1,r);
365    for(i=0;i<v.size();i++){
366      pSetExp(p,i+1,v[i]);
367    }
368    pSetm(p);
369    ptr->p=p;
370  }
371  Poly& operator+=(Poly p2){
372    copy_on_write();
373    *ptr += *p2.ptr;
374   
375    return *this;
376  }
377  Poly& operator*=(Poly p2){
378    copy_on_write();
379    *ptr *= *p2.ptr;
380   
381    return *this;
382  }
383  Poly& operator*=(Number n){
384    copy_on_write();
385    *ptr *=n;
386   
387    return *this;
388  }
389  /*  void print(){
390     StringSetS("");
391     write();
392     Print(StringAppendS(""));
393     }*/
394  virtual ~Poly(){}
395  Poly(poly p, ring r):ptr(new PolyImpl(p,r)){
396  }
397  Poly(poly p, ring r,int):ptr(new PolyImpl(p,r,0)){
398  }
399  /*Poly(Poly& p){
400    ptr=p.ptr;
401    }*/
402
403  PolyInputIterator<Poly> begin(){
404    return PolyInputIterator<Poly>(ptr->p,ptr->r);
405  }
406  PolyInputIterator<Poly> end(){
407    return PolyInputIterator<Poly>(NULL, ptr->r);
408  }
409  ring getRing(){
410    return ptr->getRing();
411  }
412 protected:
413  Poly(PolyImpl& impl):ptr(&impl){
414   
415  }
416  poly getInternalReference(){
417    return ptr->getInternalReference();
418  }
419 private:
420  shared_ptr<PolyImpl> ptr;
421  friend inline Poly operator+(const Poly& p1, const Poly& p2);
422  friend inline Poly operator*(const Poly& p1, const Poly& p2);
423  friend inline Poly operator*(const Poly& p1, const Number& n);
424  // friend inline Poly operator*(const Poly& p1, const Number& n);
425 
426};
427
428inline Poly operator+(const Poly& p1, const Poly& p2){
429    PolyImpl* res=new PolyImpl(*p1.ptr);
430    *res+=*p2.ptr;
431    return(Poly(*res));
432}
433inline Poly operator*(const Poly& p1, const Poly& p2){
434    PolyImpl* res=new PolyImpl(*p1.ptr);
435    *res *= *p2.ptr;
436    return(Poly(*res));
437}
438inline Poly operator*(const Poly& p1, const Number& n){
439    PolyImpl* res=new PolyImpl(*p1.ptr);
440    *res *= n;
441    return(Poly(*res));
442}
443
444
445#endif
Note: See TracBrowser for help on using the repository browser.