My Project  UNKNOWN_GIT_VERSION
polys.cc
Go to the documentation of this file.
1 #include "kernel/mod2.h"
2 
3 #include "omalloc/omalloc.h"
4 #include "misc/options.h"
5 
6 #include "polys.h"
7 #include "kernel/ideals.h"
8 #include "kernel/ideals.h"
9 #include "polys/clapsing.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 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(p_GetComp(p,r)==0)
47  {
48  if ((r->cf->convSingNFactoryN!=ndConvSingNFactoryN)
49  &&(!rField_is_Ring(r)))
50  {
51  poly res=singclap_pdivide(p, q, r);
52  p_Delete(&p,r);
53  p_Delete(&q,r);
54  return res;
55  }
56  else
57  {
58  ideal vi=idInit(1,1); vi->m[0]=q;
59  ideal ui=idInit(1,1); ui->m[0]=p;
60  ideal R; matrix U;
61  ring save_ring=currRing;
62  if (r!=currRing) rChangeCurrRing(r);
63  int save_opt;
64  SI_SAVE_OPT1(save_opt);
65  si_opt_1 &= ~(Sy_bit(OPT_PROT));
66  ideal m = idLift(vi,ui,&R, FALSE,TRUE,TRUE,&U);
67  SI_RESTORE_OPT1(save_opt);
68  if (r!=save_ring) rChangeCurrRing(save_ring);
69  if (idIs0(R))
70  {
72  p=MATELEM(T,1,1); MATELEM(T,1,1)=NULL;
73  id_Delete((ideal *)&T,r);
74  }
75  else p=NULL;
76  id_Delete((ideal *)&U,r);
77  id_Delete(&R,r);
78  //vi->m[0]=NULL; ui->m[0]=NULL;
79  id_Delete(&vi,r);
80  id_Delete(&ui,r);
81  return p;
82  }
83  }
84  else
85  {
86  int comps=p_MaxComp(p,r);
87  ideal I=idInit(comps,1);
88  poly h;
89  int i;
90  // conversion to a list of polys:
91  while (p!=NULL)
92  {
93  i=p_GetComp(p,r)-1;
94  h=pNext(p);
95  pNext(p)=NULL;
96  p_SetComp(p,0,r);
97  I->m[i]=p_Add_q(I->m[i],p,r);
98  p=h;
99  }
100  // division and conversion to vector:
101  h=NULL;
102  p=NULL;
103  for(i=comps-1;i>=0;i--)
104  {
105  if (I->m[i]!=NULL)
106  {
107  if ((r->cf->convSingNFactoryN!=ndConvSingNFactoryN)
108  &&(!rField_is_Ring(r)))
109  h=singclap_pdivide(I->m[i],q,r);
110  else
111  {
112  ideal vi=idInit(1,1); vi->m[0]=q;
113  ideal ui=idInit(1,1); ui->m[0]=I->m[i];
114  ideal R; matrix U;
115  ring save_ring=currRing;
116  if (r!=currRing) rChangeCurrRing(r);
117  int save_opt;
118  SI_SAVE_OPT1(save_opt);
119  si_opt_1 &= ~(Sy_bit(OPT_PROT));
120  ideal m = idLift(vi,ui,&R, FALSE,TRUE,TRUE,&U);
121  SI_RESTORE_OPT1(save_opt);
122  if (r!=save_ring) rChangeCurrRing(save_ring);
123  if (idIs0(R))
124  {
126  p=MATELEM(T,1,1); MATELEM(T,1,1)=NULL;
127  id_Delete((ideal *)&T,r);
128  }
129  else p=NULL;
130  id_Delete((ideal*)&U,r);
131  id_Delete(&R,r);
132  vi->m[0]=NULL; ui->m[0]=NULL;
133  id_Delete(&vi,r);
134  id_Delete(&ui,r);
135  }
136  p_SetCompP(h,i+1,r);
137  p=p_Add_q(p,h,r);
138  }
139  }
140  id_Delete(&I,r);
141  p_Delete(&q,r);
142  return p;
143  }
144  }
145  else
146  { /* This means that q != 0 consists of just one term,
147  or that r is over a coefficient ring. */
148 #ifdef HAVE_RINGS
149  if (!rField_is_Domain(r))
150  {
151  WerrorS("division only defined over coefficient domains");
152  return NULL;
153  }
154  if (pNext(q)!=NULL)
155  {
156  WerrorS("division over a coefficient domain only implemented for terms");
157  return NULL;
158  }
159 #endif
160  return p_DivideM(p,q,r);
161  }
162  return FALSE;
163 }
164 
165 poly singclap_gcd ( poly f, poly g, const ring r )
166 {
167  poly res=NULL;
168 
169  if (f!=NULL)
170  {
171  //if (r->cf->has_simple_Inverse) p_Norm(f,r);
172  if (rField_is_Zp(r)) p_Norm(f,r);
173  else p_Cleardenom(f, r);
174  }
175  if (g!=NULL)
176  {
177  //if (r->cf->has_simple_Inverse) p_Norm(g,r);
178  if (rField_is_Zp(r)) p_Norm(g,r);
179  else p_Cleardenom(g, r);
180  }
181  else return f; // g==0 => gcd=f (but do a p_Cleardenom/pNorm)
182  if (f==NULL) return g; // f==0 => gcd=g (but do a p_Cleardenom/pNorm)
185  {
186  res=p_One(currRing);
187  }
188  else if (r->cf->convSingNFactoryN!=ndConvSingNFactoryN)
189  {
190  res=singclap_gcd_r(f,g,r);
191  }
192  else
193  {
194  ideal I=idInit(2,1);
195  I->m[0]=f;
196  I->m[1]=p_Copy(g,r);
197  intvec *w=NULL;
198  ring save_ring=currRing;
199  if (r!=currRing) rChangeCurrRing(r);
200  int save_opt;
201  SI_SAVE_OPT1(save_opt);
202  si_opt_1 &= ~(Sy_bit(OPT_PROT));
203  ideal S1=idSyzygies(I,testHomog,&w);
204  if (w!=NULL) delete w;
205  // expect S1->m[0]=(-g/gcd,f/gcd)
206  if (IDELEMS(S1)!=1) WarnS("error in syzygy computation for GCD");
207  int lp;
208  p_TakeOutComp(&S1->m[0],1,&res,&lp,r);
209  p_Delete(&S1->m[0],r);
210  // GCD is g divided iby (-g/gcd):
211  res=p_Divide(g,res,r);
212  // restore, r, opt:
213  SI_RESTORE_OPT1(save_opt);
214  if (r!=save_ring) rChangeCurrRing(save_ring);
215  // clean the result
216  res=p_Cleardenom(res,r);
217  p_Content(res,r);
218  return res;
219  }
220  p_Delete(&f, r);
221  p_Delete(&g, r);
222  return res;
223 }
#define TRUE
Definition: auxiliary.h:98
#define FALSE
Definition: auxiliary.h:94
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
poly singclap_pdivide(poly f, poly g, const ring r)
Definition: clapsing.cc:557
poly singclap_gcd_r(poly f, poly g, const ring r)
Definition: clapsing.cc:43
Definition: intvec.h:21
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
ideal idSyzygies(ideal h1, tHomog h, intvec **w, BOOLEAN setSyzComp, BOOLEAN setRegularity, int *deg, GbVariant alg)
Definition: ideals.cc:730
ideal idLift(ideal mod, ideal submod, ideal *rest, BOOLEAN goodShape, BOOLEAN isSB, BOOLEAN divide, matrix *unit, GbVariant alg)
Definition: ideals.cc:1113
BOOLEAN idIs0(ideal h)
returns true if h is the zero ideal
static Poly * h
Definition: janet.cc:972
static jList * T
Definition: janet.cc:31
void p_TakeOutComp(poly *p, long comp, poly *q, int *lq, const ring r)
Definition: p_polys.cc:3446
#define MATELEM(mat, i, j)
Definition: matpol.h:28
#define assume(x)
Definition: mod2.h:390
#define p_GetComp(p, r)
Definition: monomials.h:65
#define pNext(p)
Definition: monomials.h:37
CanonicalForm ndConvSingNFactoryN(number, BOOLEAN, const coeffs)
Definition: numbers.cc:273
#define NULL
Definition: omList.c:10
unsigned si_opt_1
Definition: options.c:5
#define OPT_PROT
Definition: options.h:74
#define SI_SAVE_OPT1(A)
Definition: options.h:22
#define SI_RESTORE_OPT1(A)
Definition: options.h:25
#define Sy_bit(x)
Definition: options.h:32
poly p_DivideM(poly a, poly b, const ring r)
Definition: p_polys.cc:1540
void p_Content(poly ph, const ring r)
Definition: p_polys.cc:2238
void p_Norm(poly p1, const ring r)
Definition: p_polys.cc:3670
poly p_Cleardenom(poly p, const ring r)
Definition: p_polys.cc:2782
poly p_One(const ring r)
Definition: p_polys.cc:1305
static poly p_Add_q(poly p, poly q, const ring r)
Definition: p_polys.h:893
static void p_SetCompP(poly p, int i, ring r)
Definition: p_polys.h:255
static unsigned long p_SetComp(poly p, unsigned long c, ring r)
Definition: p_polys.h:248
static BOOLEAN p_IsConstant(const poly p, const ring r)
Definition: p_polys.h:1929
static long p_MaxComp(poly p, ring lmRing, ring tailRing)
Definition: p_polys.h:293
static void p_Delete(poly *p, const ring r)
Definition: p_polys.h:858
static poly p_Copy(poly p, const ring r)
returns a copy of p
Definition: p_polys.h:813
ring currRing
Widely used global variable which specifies the current polynomial ring for Singular interpreter and ...
Definition: polys.cc:13
void rChangeCurrRing(ring r)
Definition: polys.cc:15
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:31
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:165
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:3334
static BOOLEAN rField_is_Ring(const ring r)
Definition: ring.h:475
static BOOLEAN rField_is_Zp(const ring r)
Definition: ring.h:491
static BOOLEAN rField_is_Domain(const ring r)
Definition: ring.h:478
#define rTest(r)
Definition: ring.h:776
ideal idInit(int idsize, int rank)
initialise an ideal / module
Definition: simpleideals.cc:37
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:24
#define R
Definition: sirandom.c:26
@ testHomog
Definition: structs.h:41