My Project  debian-1:4.1.2-p1+ds-2
polys.cc
Go to the documentation of this file.
1 #include "kernel/mod2.h"
2 
3 #include "misc/options.h"
4 
5 #include "polys.h"
6 #include "kernel/ideals.h"
7 #include "kernel/ideals.h"
8 #include "polys/clapsing.h"
9 #include "polys/clapconv.h"
10 
11 /// Widely used global variable which specifies the current polynomial ring for Singular interpreter and legacy implementatins.
12 /// @Note: one should avoid using it in newer designs, for example due to possible problems in parallelization with threads.
13 VAR ring currRing = NULL;
14 
15 void rChangeCurrRing(ring r)
16 {
17  //------------ set global ring vars --------------------------------
18  currRing = r;
19  if( r != NULL )
20  {
21  rTest(r);
22  //------------ global variables related to coefficients ------------
23  assume( r->cf!= NULL );
24  nSetChar(r->cf);
25  //------------ global variables related to polys
26  p_SetGlobals(r); // also setting TEST_RINGDEP_OPTS
27  //------------ global variables related to factory -----------------
28  }
29 }
30 
31 poly p_Divide(poly p, poly q, const ring r)
32 {
33  assume(q!=NULL);
34  if (q==NULL)
35  {
36  WerrorS("div. by 0");
37  return NULL;
38  }
39  if (p==NULL)
40  {
41  p_Delete(&q,r);
42  return NULL;
43  }
44  if (pNext(q)!=NULL)
45  { /* This means that q != 0 consists of at least two terms*/
46  if (rIsLPRing(r))
47  {
48  WerrorS("not implemented for letterplace rings");
49  return NULL;
50  }
51  if(p_GetComp(p,r)==0)
52  {
53  if((rFieldType(r)==n_transExt)
54  &&(convSingTrP(p,r))
55  &&(convSingTrP(q,r)))
56  {
57  poly res=singclap_pdivide(p, q, r);
58  p_Delete(&p,r);
59  p_Delete(&q,r);
60  return res;
61  }
62  else if ((r->cf->convSingNFactoryN!=ndConvSingNFactoryN)
63  &&(!rField_is_Ring(r)))
64  {
65  poly res=singclap_pdivide(p, q, r);
66  p_Delete(&p,r);
67  p_Delete(&q,r);
68  return res;
69  }
70  else
71  {
72  ideal vi=idInit(1,1); vi->m[0]=q;
73  ideal ui=idInit(1,1); ui->m[0]=p;
74  ideal R; matrix U;
75  ring save_ring=currRing;
76  if (r!=currRing) rChangeCurrRing(r);
77  int save_opt;
78  SI_SAVE_OPT1(save_opt);
79  si_opt_1 &= ~(Sy_bit(OPT_PROT));
80  ideal m = idLift(vi,ui,&R, FALSE,TRUE,TRUE,&U);
81  SI_RESTORE_OPT1(save_opt);
82  if (r!=save_ring) rChangeCurrRing(save_ring);
84  p=MATELEM(T,1,1); MATELEM(T,1,1)=NULL;
85  id_Delete((ideal *)&T,r);
86  id_Delete((ideal *)&U,r);
87  id_Delete(&R,r);
88  //vi->m[0]=NULL; ui->m[0]=NULL;
89  id_Delete(&vi,r);
90  id_Delete(&ui,r);
91  return p;
92  }
93  }
94  else
95  {
96  int comps=p_MaxComp(p,r);
97  ideal I=idInit(comps,1);
98  poly h;
99  int i;
100  // conversion to a list of polys:
101  while (p!=NULL)
102  {
103  i=p_GetComp(p,r)-1;
104  h=pNext(p);
105  pNext(p)=NULL;
106  p_SetComp(p,0,r);
107  I->m[i]=p_Add_q(I->m[i],p,r);
108  p=h;
109  }
110  // division and conversion to vector:
111  h=NULL;
112  p=NULL;
113  for(i=comps-1;i>=0;i--)
114  {
115  if (I->m[i]!=NULL)
116  {
117  if((rFieldType(r)==n_transExt)
118  &&(convSingTrP(I->m[i],r))
119  &&(convSingTrP(q,r)))
120  {
121  h=singclap_pdivide(I->m[i],q,r);
122  }
123  else if ((r->cf->convSingNFactoryN!=ndConvSingNFactoryN)
124  &&(!rField_is_Ring(r)))
125  h=singclap_pdivide(I->m[i],q,r);
126  else
127  {
128  ideal vi=idInit(1,1); vi->m[0]=q;
129  ideal ui=idInit(1,1); ui->m[0]=I->m[i];
130  ideal R; matrix U;
131  ring save_ring=currRing;
132  if (r!=currRing) rChangeCurrRing(r);
133  int save_opt;
134  SI_SAVE_OPT1(save_opt);
135  si_opt_1 &= ~(Sy_bit(OPT_PROT));
136  ideal m = idLift(vi,ui,&R, FALSE,TRUE,TRUE,&U);
137  SI_RESTORE_OPT1(save_opt);
138  if (r!=save_ring) rChangeCurrRing(save_ring);
139  if (idIs0(R))
140  {
142  p=MATELEM(T,1,1); MATELEM(T,1,1)=NULL;
143  id_Delete((ideal *)&T,r);
144  }
145  else p=NULL;
146  id_Delete((ideal*)&U,r);
147  id_Delete(&R,r);
148  vi->m[0]=NULL; ui->m[0]=NULL;
149  id_Delete(&vi,r);
150  id_Delete(&ui,r);
151  }
152  p_SetCompP(h,i+1,r);
153  p=p_Add_q(p,h,r);
154  }
155  }
156  id_Delete(&I,r);
157  p_Delete(&q,r);
158  return p;
159  }
160  }
161  else
162  { /* This means that q != 0 consists of just one term,
163  or that r is over a coefficient ring. */
164 #ifdef HAVE_RINGS
165  if (!rField_is_Domain(r))
166  {
167  WerrorS("division only defined over coefficient domains");
168  return NULL;
169  }
170  if (pNext(q)!=NULL)
171  {
172  WerrorS("division over a coefficient domain only implemented for terms");
173  return NULL;
174  }
175 #endif
176  return p_DivideM(p,q,r);
177  }
178  return NULL;
179 }
180 
181 poly p_DivRem(poly p, poly q, poly &rest, const ring r)
182 {
183  assume(q!=NULL);
184  rest=NULL;
185  if (q==NULL)
186  {
187  WerrorS("div. by 0");
188  return NULL;
189  }
190  if (p==NULL)
191  {
192  p_Delete(&q,r);
193  return NULL;
194  }
195  if (rIsLPRing(r))
196  {
197  WerrorS("not implemented for letterplace rings");
198  return NULL;
199  }
200  if(p_GetComp(p,r)==0)
201  {
202  if((rFieldType(r)==n_transExt)
203  &&(convSingTrP(p,r))
204  &&(convSingTrP(q,r)))
205  {
206  poly res=singclap_pdivide(p, q, r);
207  rest=singclap_pmod(p,q,r);
208  p_Delete(&p,r);
209  p_Delete(&q,r);
210  return res;
211  }
212  else if ((r->cf->convSingNFactoryN!=ndConvSingNFactoryN)
213  &&(!rField_is_Ring(r)))
214  {
215  poly res=singclap_pdivide(p, q, r);
216  rest=singclap_pmod(p,q,r);
217  p_Delete(&p,r);
218  p_Delete(&q,r);
219  return res;
220  }
221  else
222  {
223  ideal vi=idInit(1,1); vi->m[0]=q;
224  ideal ui=idInit(1,1); ui->m[0]=p;
225  ideal R; matrix U;
226  ring save_ring=currRing;
227  if (r!=currRing) rChangeCurrRing(r);
228  int save_opt;
229  SI_SAVE_OPT1(save_opt);
230  si_opt_1 &= ~(Sy_bit(OPT_PROT));
231  ideal m = idLift(vi,ui,&R, FALSE,TRUE,TRUE,&U);
232  SI_RESTORE_OPT1(save_opt);
233  if (r!=save_ring) rChangeCurrRing(save_ring);
235  p=MATELEM(T,1,1); MATELEM(T,1,1)=NULL;
236  id_Delete((ideal *)&T,r);
237  T = id_Module2formatedMatrix(R,1,1,r);
238  rest=MATELEM(T,1,1); MATELEM(T,1,1)=NULL;
239  id_Delete((ideal *)&T,r);
240  id_Delete((ideal *)&U,r);
241  id_Delete(&R,r);
242  //vi->m[0]=NULL; ui->m[0]=NULL;
243  id_Delete(&vi,r);
244  id_Delete(&ui,r);
245  return p;
246  }
247  }
248  return NULL;
249 }
250 
251 poly singclap_gcd ( poly f, poly g, const ring r )
252 {
253  poly res=NULL;
254 
255  if (f!=NULL)
256  {
257  //if (r->cf->has_simple_Inverse) p_Norm(f,r);
258  if (rField_is_Zp(r)) p_Norm(f,r);
259  else p_Cleardenom(f, r);
260  }
261  if (g!=NULL)
262  {
263  //if (r->cf->has_simple_Inverse) p_Norm(g,r);
264  if (rField_is_Zp(r)) p_Norm(g,r);
265  else p_Cleardenom(g, r);
266  }
267  else return f; // g==0 => gcd=f (but do a p_Cleardenom/pNorm)
268  if (f==NULL) return g; // f==0 => gcd=g (but do a p_Cleardenom/pNorm)
269  if(!rField_is_Ring(r)
270  && (p_IsConstant(f,r)
271  ||p_IsConstant(g,r)))
272  {
273  res=p_One(r);
274  }
275  else if (r->cf->convSingNFactoryN!=ndConvSingNFactoryN)
276  {
277  res=singclap_gcd_r(f,g,r);
278  }
279  else
280  {
281  ideal I=idInit(2,1);
282  I->m[0]=f;
283  I->m[1]=p_Copy(g,r);
284  intvec *w=NULL;
285  ring save_ring=currRing;
286  if (r!=currRing) rChangeCurrRing(r);
287  int save_opt;
288  SI_SAVE_OPT1(save_opt);
289  si_opt_1 &= ~(Sy_bit(OPT_PROT));
290  ideal S1=idSyzygies(I,testHomog,&w);
291  if (w!=NULL) delete w;
292  // expect S1->m[0]=(-g/gcd,f/gcd)
293  if (IDELEMS(S1)!=1) WarnS("error in syzygy computation for GCD");
294  int lp;
295  p_TakeOutComp(&S1->m[0],1,&res,&lp,r);
296  p_Delete(&S1->m[0],r);
297  // GCD is g divided iby (-g/gcd):
298  res=p_Divide(g,res,r);
299  // restore, r, opt:
300  SI_RESTORE_OPT1(save_opt);
301  if (r!=save_ring) rChangeCurrRing(save_ring);
302  // clean the result
303  res=p_Cleardenom(res,r);
304  p_Content(res,r);
305  return res;
306  }
307  p_Delete(&f, r);
308  p_Delete(&g, r);
309  return res;
310 }
#define TRUE
Definition: auxiliary.h:100
#define FALSE
Definition: auxiliary.h:96
int m
Definition: cfEzgcd.cc:121
int i
Definition: cfEzgcd.cc:125
int p
Definition: cfModGcd.cc:4019
g
Definition: cfModGcd.cc:4031
FILE * f
Definition: checklibs.c:9
BOOLEAN convSingTrP(poly p, const ring r)
Definition: clapconv.cc:320
poly singclap_pmod(poly f, poly g, const ring r)
Definition: clapsing.cc:605
poly singclap_pdivide(poly f, poly g, const ring r)
Definition: clapsing.cc:558
poly singclap_gcd_r(poly f, poly g, const ring r)
Definition: clapsing.cc:42
Definition: intvec.h:23
@ n_transExt
used for all transcendental extensions, i.e., the top-most extension in an extension tower is transce...
Definition: coeffs.h:39
static FORCE_INLINE void nSetChar(const coeffs r)
initialisations after each ring change
Definition: coeffs.h:436
#define WarnS
Definition: emacs.cc:78
CanonicalForm res
Definition: facAbsFact.cc:64
const CanonicalForm & w
Definition: facAbsFact.cc:55
void WerrorS(const char *s)
Definition: feFopen.cc:24
#define VAR
Definition: globaldefs.h:5
ideal idSyzygies(ideal h1, tHomog h, intvec **w, BOOLEAN setSyzComp, BOOLEAN setRegularity, int *deg, GbVariant alg)
Definition: ideals.cc:728
ideal idLift(ideal mod, ideal submod, ideal *rest, BOOLEAN goodShape, BOOLEAN isSB, BOOLEAN divide, matrix *unit, GbVariant alg)
Definition: ideals.cc:1111
BOOLEAN idIs0(ideal h)
returns true if h is the zero ideal
STATIC_VAR jList * T
Definition: janet.cc:30
STATIC_VAR Poly * h
Definition: janet.cc:971
void p_TakeOutComp(poly *p, long comp, poly *q, int *lq, const ring r)
Definition: p_polys.cc:3455
#define MATELEM(mat, i, j)
1-based access to matrix
Definition: matpol.h:29
#define assume(x)
Definition: mod2.h:390
#define p_GetComp(p, r)
Definition: monomials.h:64
#define pNext(p)
Definition: monomials.h:36
CanonicalForm ndConvSingNFactoryN(number, BOOLEAN, const coeffs)
Definition: numbers.cc:272
#define NULL
Definition: omList.c:12
VAR unsigned si_opt_1
Definition: options.c:5
#define OPT_PROT
Definition: options.h:73
#define SI_SAVE_OPT1(A)
Definition: options.h:21
#define SI_RESTORE_OPT1(A)
Definition: options.h:24
#define Sy_bit(x)
Definition: options.h:31
poly p_DivideM(poly a, poly b, const ring r)
Definition: p_polys.cc:1560
void p_Content(poly ph, const ring r)
Definition: p_polys.cc:2270
void p_Norm(poly p1, const ring r)
Definition: p_polys.cc:3679
poly p_Cleardenom(poly p, const ring r)
Definition: p_polys.cc:2791
poly p_One(const ring r)
Definition: p_polys.cc:1303
static poly p_Add_q(poly p, poly q, const ring r)
Definition: p_polys.h:895
static void p_SetCompP(poly p, int i, ring r)
Definition: p_polys.h:253
static unsigned long p_SetComp(poly p, unsigned long c, ring r)
Definition: p_polys.h:246
static BOOLEAN p_IsConstant(const poly p, const ring r)
Definition: p_polys.h:1931
static long p_MaxComp(poly p, ring lmRing, ring tailRing)
Definition: p_polys.h:291
static void p_Delete(poly *p, const ring r)
Definition: p_polys.h:860
static poly p_Copy(poly p, const ring r)
returns a copy of p
Definition: p_polys.h:811
void rChangeCurrRing(ring r)
Definition: polys.cc:15
poly p_Divide(poly p, poly q, const ring r)
polynomial division a/b, ignoring the rest via singclap_pdivide resp. idLift destroyes a,...
Definition: polys.cc:31
VAR ring currRing
Widely used global variable which specifies the current polynomial ring for Singular interpreter and ...
Definition: polys.cc:13
poly singclap_gcd(poly f, poly g, const ring r)
polynomial gcd via singclap_gcd_r resp. idSyzygies destroys f and g
Definition: polys.cc:251
poly p_DivRem(poly p, poly q, poly &rest, const ring r)
Definition: polys.cc:181
Compatiblity layer for legacy polynomial operations (over currRing)
void p_SetGlobals(const ring r, BOOLEAN complete)
set all properties of a new ring - also called by rComplete
Definition: ring.cc:3363
static BOOLEAN rField_is_Ring(const ring r)
Definition: ring.h:479
static BOOLEAN rField_is_Zp(const ring r)
Definition: ring.h:495
static n_coeffType rFieldType(const ring r)
the type of the coefficient filed of r (n_Zp, n_Q, etc)
Definition: ring.h:551
static BOOLEAN rField_is_Domain(const ring r)
Definition: ring.h:482
static BOOLEAN rIsLPRing(const ring r)
Definition: ring.h:408
#define rTest(r)
Definition: ring.h:780
ideal idInit(int idsize, int rank)
initialise an ideal / module
Definition: simpleideals.cc:35
void id_Delete(ideal *h, ring r)
deletes an ideal/module/matrix
matrix id_Module2formatedMatrix(ideal mod, int rows, int cols, const ring R)
#define IDELEMS(i)
Definition: simpleideals.h:23
#define R
Definition: sirandom.c:27
@ testHomog
Definition: structs.h:43