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 | { |
---|
75 | matrix T = id_Module2formatedMatrix(m,1,1,r); |
---|
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 | } |
---|