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 
10 /// Widely used global variable which specifies the current polynomial ring for Singular interpreter and legacy implementatins.
11 /// @Note: one should avoid using it in newer designs, for example due to possible problems in parallelization with threads.
12 ring currRing = NULL;
13 
14 void rChangeCurrRing(ring r)
15 {
16  //------------ set global ring vars --------------------------------
17  currRing = r;
18  if( r != NULL )
19  {
20  rTest(r);
21  //------------ global variables related to coefficients ------------
22  assume( r->cf!= NULL );
23  nSetChar(r->cf);
24  //------------ global variables related to polys
25  p_SetGlobals(r); // also setting TEST_RINGDEP_OPTS
26  //------------ global variables related to factory -----------------
27  }
28 }
29 
30 poly p_Divide(poly p, poly q, const ring r)
31 {
32  assume(q!=NULL);
33  if (q==NULL)
34  {
35  WerrorS("div. by 0");
36  return NULL;
37  }
38  if (p==NULL)
39  {
40  p_Delete(&q,r);
41  return NULL;
42  }
43  if (pNext(q)!=NULL)
44  { /* This means that q != 0 consists of at least two terms*/
45  if (rIsLPRing(r))
46  {
47  WerrorS("not implemented for letterplace rings");
48  return NULL;
49  }
50  if(p_GetComp(p,r)==0)
51  {
52  if ((r->cf->convSingNFactoryN!=ndConvSingNFactoryN)
53  &&(!rField_is_Ring(r)))
54  {
55  poly res=singclap_pdivide(p, q, r);
56  p_Delete(&p,r);
57  p_Delete(&q,r);
58  return res;
59  }
60  else
61  {
62  ideal vi=idInit(1,1); vi->m[0]=q;
63  ideal ui=idInit(1,1); ui->m[0]=p;
64  ideal R; matrix U;
65  ring save_ring=currRing;
66  if (r!=currRing) rChangeCurrRing(r);
67  int save_opt;
68  SI_SAVE_OPT1(save_opt);
69  si_opt_1 &= ~(Sy_bit(OPT_PROT));
70  ideal m = idLift(vi,ui,&R, FALSE,TRUE,TRUE,&U);
71  SI_RESTORE_OPT1(save_opt);
72  if (r!=save_ring) rChangeCurrRing(save_ring);
73  if (idIs0(R))
74  {
76  p=MATELEM(T,1,1); MATELEM(T,1,1)=NULL;
77  id_Delete((ideal *)&T,r);
78  }
79  else p=NULL;
80  id_Delete((ideal *)&U,r);
81  id_Delete(&R,r);
82  //vi->m[0]=NULL; ui->m[0]=NULL;
83  id_Delete(&vi,r);
84  id_Delete(&ui,r);
85  return p;
86  }
87  }
88  else
89  {
90  int comps=p_MaxComp(p,r);
91  ideal I=idInit(comps,1);
92  poly h;
93  int i;
94  // conversion to a list of polys:
95  while (p!=NULL)
96  {
97  i=p_GetComp(p,r)-1;
98  h=pNext(p);
99  pNext(p)=NULL;
100  p_SetComp(p,0,r);
101  I->m[i]=p_Add_q(I->m[i],p,r);
102  p=h;
103  }
104  // division and conversion to vector:
105  h=NULL;
106  p=NULL;
107  for(i=comps-1;i>=0;i--)
108  {
109  if (I->m[i]!=NULL)
110  {
111  if ((r->cf->convSingNFactoryN!=ndConvSingNFactoryN)
112  &&(!rField_is_Ring(r)))
113  h=singclap_pdivide(I->m[i],q,r);
114  else
115  {
116  ideal vi=idInit(1,1); vi->m[0]=q;
117  ideal ui=idInit(1,1); ui->m[0]=I->m[i];
118  ideal R; matrix U;
119  ring save_ring=currRing;
120  if (r!=currRing) rChangeCurrRing(r);
121  int save_opt;
122  SI_SAVE_OPT1(save_opt);
123  si_opt_1 &= ~(Sy_bit(OPT_PROT));
124  ideal m = idLift(vi,ui,&R, FALSE,TRUE,TRUE,&U);
125  SI_RESTORE_OPT1(save_opt);
126  if (r!=save_ring) rChangeCurrRing(save_ring);
127  if (idIs0(R))
128  {
129  matrix T = id_Module2formatedMatrix(m,1,1,r);
130  p=MATELEM(T,1,1); MATELEM(T,1,1)=NULL;
131  id_Delete((ideal *)&T,r);
132  }
133  else p=NULL;
134  id_Delete((ideal*)&U,r);
135  id_Delete(&R,r);
136  vi->m[0]=NULL; ui->m[0]=NULL;
137  id_Delete(&vi,r);
138  id_Delete(&ui,r);
139  }
140  p_SetCompP(h,i+1,r);
141  p=p_Add_q(p,h,r);
142  }
143  }
144  id_Delete(&I,r);
145  p_Delete(&q,r);
146  return p;
147  }
148  }
149  else
150  { /* This means that q != 0 consists of just one term,
151  or that r is over a coefficient ring. */
152 #ifdef HAVE_RINGS
153  if (!rField_is_Domain(r))
154  {
155  WerrorS("division only defined over coefficient domains");
156  return NULL;
157  }
158  if (pNext(q)!=NULL)
159  {
160  WerrorS("division over a coefficient domain only implemented for terms");
161  return NULL;
162  }
163 #endif
164  return p_DivideM(p,q,r);
165  }
166  return FALSE;
167 }
168 
169 poly singclap_gcd ( poly f, poly g, const ring r )
170 {
171  poly res=NULL;
172 
173  if (f!=NULL)
174  {
175  //if (r->cf->has_simple_Inverse) p_Norm(f,r);
176  if (rField_is_Zp(r)) p_Norm(f,r);
177  else p_Cleardenom(f, r);
178  }
179  if (g!=NULL)
180  {
181  //if (r->cf->has_simple_Inverse) p_Norm(g,r);
182  if (rField_is_Zp(r)) p_Norm(g,r);
183  else p_Cleardenom(g, r);
184  }
185  else return f; // g==0 => gcd=f (but do a p_Cleardenom/pNorm)
186  if (f==NULL) return g; // f==0 => gcd=g (but do a p_Cleardenom/pNorm)
187  if(!rField_is_Ring(r)
188  && (p_IsConstant(f,r)
189  ||p_IsConstant(g,r)))
190  {
191  res=p_One(r);
192  }
193  else if (r->cf->convSingNFactoryN!=ndConvSingNFactoryN)
194  {
195  res=singclap_gcd_r(f,g,r);
196  }
197  else
198  {
199  ideal I=idInit(2,1);
200  I->m[0]=f;
201  I->m[1]=p_Copy(g,r);
202  intvec *w=NULL;
203  ring save_ring=currRing;
204  if (r!=currRing) rChangeCurrRing(r);
205  int save_opt;
206  SI_SAVE_OPT1(save_opt);
207  si_opt_1 &= ~(Sy_bit(OPT_PROT));
208  ideal S1=idSyzygies(I,testHomog,&w);
209  if (w!=NULL) delete w;
210  // expect S1->m[0]=(-g/gcd,f/gcd)
211  if (IDELEMS(S1)!=1) WarnS("error in syzygy computation for GCD");
212  int lp;
213  p_TakeOutComp(&S1->m[0],1,&res,&lp,r);
214  p_Delete(&S1->m[0],r);
215  // GCD is g divided iby (-g/gcd):
216  res=p_Divide(g,res,r);
217  // restore, r, opt:
218  SI_RESTORE_OPT1(save_opt);
219  if (r!=save_ring) rChangeCurrRing(save_ring);
220  // clean the result
221  res=p_Cleardenom(res,r);
222  p_Content(res,r);
223  return res;
224  }
225  p_Delete(&f, r);
226  p_Delete(&g, r);
227  return res;
228 }
unsigned si_opt_1
Definition: options.c:5
poly singclap_gcd_r(poly f, poly g, const ring r)
Definition: clapsing.cc:42
#define OPT_PROT
Definition: options.h:74
#define FALSE
Definition: auxiliary.h:94
Compatiblity layer for legacy polynomial operations (over currRing)
void p_TakeOutComp(poly *p, long comp, poly *q, int *lq, const ring r)
Definition: p_polys.cc:3455
static unsigned long p_SetComp(poly p, unsigned long c, ring r)
Definition: p_polys.h:246
#define p_GetComp(p, r)
Definition: monomials.h:64
static FORCE_INLINE void nSetChar(const coeffs r)
initialisations after each ring change
Definition: coeffs.h:436
void id_Delete(ideal *h, ring r)
deletes an ideal/module/matrix
#define TRUE
Definition: auxiliary.h:98
static BOOLEAN rField_is_Domain(const ring r)
Definition: ring.h:482
#define SI_SAVE_OPT1(A)
Definition: options.h:22
g
Definition: cfModGcd.cc:4031
void WerrorS(const char *s)
Definition: feFopen.cc:24
void p_Norm(poly p1, const ring r)
Definition: p_polys.cc:3679
#define WarnS
Definition: emacs.cc:78
static BOOLEAN rIsLPRing(const ring r)
Definition: ring.h:408
poly singclap_pdivide(poly f, poly g, const ring r)
Definition: clapsing.cc:556
#define Sy_bit(x)
Definition: options.h:32
static poly p_Copy(poly p, const ring r)
returns a copy of p
Definition: p_polys.h:811
CanonicalForm ndConvSingNFactoryN(number, BOOLEAN, const coeffs)
Definition: numbers.cc:272
static void p_SetCompP(poly p, int i, ring r)
Definition: p_polys.h:253
Definition: intvec.h:19
CanonicalForm res
Definition: facAbsFact.cc:64
poly p_One(const ring r)
Definition: p_polys.cc:1303
poly p_Divide(poly p, poly q, const ring r)
polynomial division, ignoring the rest via singclap_pdivide resp. idLift destroyes a...
Definition: polys.cc:30
#define assume(x)
Definition: mod2.h:390
static BOOLEAN p_IsConstant(const poly p, const ring r)
Definition: p_polys.h:1927
#define rTest(r)
Definition: ring.h:780
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:169
int m
Definition: cfEzgcd.cc:121
FILE * f
Definition: checklibs.c:9
int i
Definition: cfEzgcd.cc:125
void p_Content(poly ph, const ring r)
Definition: p_polys.cc:2270
#define IDELEMS(i)
Definition: simpleideals.h:23
void rChangeCurrRing(ring r)
Definition: polys.cc:14
static BOOLEAN rField_is_Zp(const ring r)
Definition: ring.h:495
ideal idSyzygies(ideal h1, tHomog h, intvec **w, BOOLEAN setSyzComp, BOOLEAN setRegularity, int *deg, GbVariant alg)
Definition: ideals.cc:728
static void p_Delete(poly *p, const ring r)
Definition: p_polys.h:856
ideal idInit(int idsize, int rank)
initialise an ideal / module
Definition: simpleideals.cc:35
poly p_DivideM(poly a, poly b, const ring r)
Definition: p_polys.cc:1560
static BOOLEAN rField_is_Ring(const ring r)
Definition: ring.h:479
#define NULL
Definition: omList.c:12
matrix id_Module2formatedMatrix(ideal mod, int rows, int cols, const ring R)
#define R
Definition: sirandom.c:26
const CanonicalForm & w
Definition: facAbsFact.cc:55
#define pNext(p)
Definition: monomials.h:36
ring currRing
Widely used global variable which specifies the current polynomial ring for Singular interpreter and ...
Definition: polys.cc:12
ideal idLift(ideal mod, ideal submod, ideal *rest, BOOLEAN goodShape, BOOLEAN isSB, BOOLEAN divide, matrix *unit, GbVariant alg)
Definition: ideals.cc:1111
int p
Definition: cfModGcd.cc:4019
static jList * T
Definition: janet.cc:30
static poly p_Add_q(poly p, poly q, const ring r)
Definition: p_polys.h:891
void p_SetGlobals(const ring r, BOOLEAN complete)
set all properties of a new ring - also called by rComplete
Definition: ring.cc:3359
static Poly * h
Definition: janet.cc:971
BOOLEAN idIs0(ideal h)
returns true if h is the zero ideal
poly p_Cleardenom(poly p, const ring r)
Definition: p_polys.cc:2791
#define SI_RESTORE_OPT1(A)
Definition: options.h:25
static long p_MaxComp(poly p, ring lmRing, ring tailRing)
Definition: p_polys.h:291
#define MATELEM(mat, i, j)
1-based access to matrix
Definition: matpol.h:29