source: git/kernel/Poly.h @ 90be05

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