1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* |
---|
5 | * ABSTRACT: ringgb interface |
---|
6 | */ |
---|
7 | //#define HAVE_TAIL_RING |
---|
8 | #define NO_BUCKETS |
---|
9 | |
---|
10 | #include "kernel/mod2.h" |
---|
11 | #include "kernel/GBEngine/kutil.h" |
---|
12 | #include "kernel/structs.h" |
---|
13 | #include "kernel/polys.h" |
---|
14 | #include "polys/monomials/p_polys.h" |
---|
15 | #include "kernel/ideals.h" |
---|
16 | #include "kernel/GBEngine/kstd1.h" |
---|
17 | #include "kernel/GBEngine/khstd.h" |
---|
18 | #include "polys/kbuckets.h" |
---|
19 | #include "polys/weight.h" |
---|
20 | #include "misc/intvec.h" |
---|
21 | #include "kernel/polys.h" |
---|
22 | #ifdef HAVE_PLURAL |
---|
23 | #include "polys/nc/nc.h" |
---|
24 | #endif |
---|
25 | |
---|
26 | #include "kernel/GBEngine/ringgb.h" |
---|
27 | |
---|
28 | #ifdef HAVE_RINGS |
---|
29 | poly reduce_poly_fct(poly p, ring r) |
---|
30 | { |
---|
31 | return kFindZeroPoly(p, r, r); |
---|
32 | } |
---|
33 | |
---|
34 | /* |
---|
35 | * Returns maximal k, such that |
---|
36 | * 2^k | n |
---|
37 | */ |
---|
38 | int indexOf2(number n) |
---|
39 | { |
---|
40 | long test = (long) n; |
---|
41 | int i = 0; |
---|
42 | while (test%2 == 0) |
---|
43 | { |
---|
44 | i++; |
---|
45 | test = test / 2; |
---|
46 | } |
---|
47 | return i; |
---|
48 | } |
---|
49 | |
---|
50 | /*************************************************************** |
---|
51 | * |
---|
52 | * Lcm business |
---|
53 | * |
---|
54 | ***************************************************************/ |
---|
55 | // get m1 = LCM(LM(p1), LM(p2))/LM(p1) |
---|
56 | // m2 = LCM(LM(p1), LM(p2))/LM(p2) |
---|
57 | BOOLEAN ring2toM_GetLeadTerms(const poly p1, const poly p2, const ring p_r, |
---|
58 | poly &m1, poly &m2, const ring m_r) |
---|
59 | { |
---|
60 | int i; |
---|
61 | int x; |
---|
62 | m1 = p_Init(m_r); |
---|
63 | m2 = p_Init(m_r); |
---|
64 | |
---|
65 | for (i = p_r->N; i; i--) |
---|
66 | { |
---|
67 | x = p_GetExpDiff(p1, p2, i, p_r); |
---|
68 | if (x > 0) |
---|
69 | { |
---|
70 | p_SetExp(m2,i,x, m_r); |
---|
71 | p_SetExp(m1,i,0, m_r); |
---|
72 | } |
---|
73 | else |
---|
74 | { |
---|
75 | p_SetExp(m1,i,-x, m_r); |
---|
76 | p_SetExp(m2,i,0, m_r); |
---|
77 | } |
---|
78 | } |
---|
79 | p_Setm(m1, m_r); |
---|
80 | p_Setm(m2, m_r); |
---|
81 | long cp1 = (long) pGetCoeff(p1); |
---|
82 | long cp2 = (long) pGetCoeff(p2); |
---|
83 | if (cp1 != 0 && cp2 != 0) |
---|
84 | { |
---|
85 | while (cp1%2 == 0 && cp2%2 == 0) |
---|
86 | { |
---|
87 | cp1 = cp1 / 2; |
---|
88 | cp2 = cp2 / 2; |
---|
89 | } |
---|
90 | } |
---|
91 | p_SetCoeff(m1, (number) cp2, m_r); |
---|
92 | p_SetCoeff(m2, (number) cp1, m_r); |
---|
93 | return TRUE; |
---|
94 | } |
---|
95 | |
---|
96 | void printPolyMsg(const char * start, poly f, const char * end) |
---|
97 | { |
---|
98 | PrintS(start); |
---|
99 | wrp(f); |
---|
100 | PrintS(end); |
---|
101 | } |
---|
102 | |
---|
103 | poly spolyRing2toM(poly f, poly g, ring r) |
---|
104 | { |
---|
105 | poly m1 = NULL; |
---|
106 | poly m2 = NULL; |
---|
107 | ring2toM_GetLeadTerms(f, g, r, m1, m2, r); |
---|
108 | // printPolyMsg("spoly: m1=", m1, " | "); |
---|
109 | // printPolyMsg("m2=", m2, ""); |
---|
110 | // PrintLn(); |
---|
111 | poly sp = pSub(p_Mult_mm(f, m1, r), pp_Mult_mm(g, m2, r)); |
---|
112 | pDelete(&m1); |
---|
113 | pDelete(&m2); |
---|
114 | return(sp); |
---|
115 | } |
---|
116 | |
---|
117 | poly ringRedNF (poly f, ideal G, ring r) |
---|
118 | { |
---|
119 | // If f = 0, then normal form is also 0 |
---|
120 | if (f == NULL) { return NULL; } |
---|
121 | poly h = NULL; |
---|
122 | poly g = pCopy(f); |
---|
123 | int c = 0; |
---|
124 | while (g != NULL) |
---|
125 | { |
---|
126 | Print("%d-step RedNF - g=", c); |
---|
127 | wrp(g); |
---|
128 | PrintS(" | h="); |
---|
129 | wrp(h); |
---|
130 | PrintLn(); |
---|
131 | g = ringNF(g, G, r); |
---|
132 | if (g != NULL) { |
---|
133 | h = pAdd(h, pHead(g)); |
---|
134 | pLmDelete(&g); |
---|
135 | } |
---|
136 | c++; |
---|
137 | } |
---|
138 | return h; |
---|
139 | } |
---|
140 | |
---|
141 | #endif |
---|
142 | |
---|
143 | #ifdef HAVE_RINGS |
---|
144 | |
---|
145 | /* |
---|
146 | * Find an index i from G, such that |
---|
147 | * LT(rside) = x * LT(G[i]) has a solution |
---|
148 | * or -1 if rside is not in the |
---|
149 | * ideal of the leading coefficients |
---|
150 | * of the suitable g from G. |
---|
151 | */ |
---|
152 | int findRingSolver(poly rside, ideal G, ring r) |
---|
153 | { |
---|
154 | if (rside == NULL) return -1; |
---|
155 | int i; |
---|
156 | // int iO2rside = indexOf2(pGetCoeff(rside)); |
---|
157 | for (i = 0; i < IDELEMS(G); i++) |
---|
158 | { |
---|
159 | if // (indexOf2(pGetCoeff(G->m[i])) <= iO2rside && / should not be necessary any more |
---|
160 | (p_LmDivisibleBy(G->m[i], rside, r)) |
---|
161 | { |
---|
162 | return i; |
---|
163 | } |
---|
164 | } |
---|
165 | return -1; |
---|
166 | } |
---|
167 | |
---|
168 | poly plain_spoly(poly f, poly g) |
---|
169 | { |
---|
170 | number cf = nCopy(pGetCoeff(f)), cg = nCopy(pGetCoeff(g)); |
---|
171 | (void)ksCheckCoeff(&cf, &cg, currRing->cf); // gcd and zero divisors |
---|
172 | poly fm, gm; |
---|
173 | k_GetLeadTerms(f, g, currRing, fm, gm, currRing); |
---|
174 | pSetCoeff0(fm, cg); |
---|
175 | pSetCoeff0(gm, cf); // and now, m1 * LT(p1) == m2 * LT(p2) |
---|
176 | poly sp = pSub(ppMult_mm(f, fm), ppMult_mm(g, gm)); |
---|
177 | pDelete(&fm); |
---|
178 | pDelete(&gm); |
---|
179 | return(sp); |
---|
180 | } |
---|
181 | |
---|
182 | /*2 |
---|
183 | * Generates spoly(0, h) if applicable. Assumes ring in Z/2^n. |
---|
184 | */ |
---|
185 | poly plain_zero_spoly(poly h) |
---|
186 | { |
---|
187 | poly p = NULL; |
---|
188 | number gcd = n_Gcd((number) 0, pGetCoeff(h), currRing->cf); |
---|
189 | if (!n_IsOne( gcd, currRing->cf )) |
---|
190 | { |
---|
191 | number tmp=n_Ann(gcd,currRing->cf); |
---|
192 | p = p_Copy(h->next, currRing); |
---|
193 | p = __p_Mult_nn(p, tmp, currRing); |
---|
194 | n_Delete(&tmp,currRing->cf); |
---|
195 | } |
---|
196 | return p; |
---|
197 | } |
---|
198 | |
---|
199 | poly ringNF(poly f, ideal G, ring r) |
---|
200 | { |
---|
201 | // If f = 0, then normal form is also 0 |
---|
202 | if (f == NULL) { return NULL; } |
---|
203 | poly tmp = NULL; |
---|
204 | poly h = pCopy(f); |
---|
205 | int i = findRingSolver(h, G, r); |
---|
206 | int c = 1; |
---|
207 | while (h != NULL && i >= 0) { |
---|
208 | // Print("%d-step NF - h:", c); |
---|
209 | // wrp(h); |
---|
210 | // PrintS(" "); |
---|
211 | // PrintS("G->m[i]:"); |
---|
212 | // wrp(G->m[i]); |
---|
213 | // PrintLn(); |
---|
214 | tmp = h; |
---|
215 | h = plain_spoly(h, G->m[i]); |
---|
216 | pDelete(&tmp); |
---|
217 | // PrintS("=> h="); |
---|
218 | // wrp(h); |
---|
219 | // PrintLn(); |
---|
220 | i = findRingSolver(h, G, r); |
---|
221 | c++; |
---|
222 | } |
---|
223 | return h; |
---|
224 | } |
---|
225 | |
---|
226 | int testGB(ideal I, ideal GI) { |
---|
227 | poly f, g, h, nf; |
---|
228 | int i = 0; |
---|
229 | int j = 0; |
---|
230 | PrintS("I included?"); |
---|
231 | for (i = 0; i < IDELEMS(I); i++) { |
---|
232 | if (ringNF(I->m[i], GI, currRing) != NULL) { |
---|
233 | PrintS("Not reduced to zero from I: "); |
---|
234 | wrp(I->m[i]); |
---|
235 | PrintS(" --> "); |
---|
236 | wrp(ringNF(I->m[i], GI, currRing)); |
---|
237 | PrintLn(); |
---|
238 | return(0); |
---|
239 | } |
---|
240 | PrintS("-"); |
---|
241 | } |
---|
242 | PrintS(" Yes!\nspoly --> 0?"); |
---|
243 | for (i = 0; i < IDELEMS(GI); i++) |
---|
244 | { |
---|
245 | for (j = i + 1; j < IDELEMS(GI); j++) |
---|
246 | { |
---|
247 | f = pCopy(GI->m[i]); |
---|
248 | g = pCopy(GI->m[j]); |
---|
249 | h = plain_spoly(f, g); |
---|
250 | nf = ringNF(h, GI, currRing); |
---|
251 | if (nf != NULL) |
---|
252 | { |
---|
253 | PrintS("spoly("); |
---|
254 | wrp(GI->m[i]); |
---|
255 | PrintS(", "); |
---|
256 | wrp(GI->m[j]); |
---|
257 | PrintS(") = "); |
---|
258 | wrp(h); |
---|
259 | PrintS(" --> "); |
---|
260 | wrp(nf); |
---|
261 | PrintLn(); |
---|
262 | return(0); |
---|
263 | } |
---|
264 | pDelete(&f); |
---|
265 | pDelete(&g); |
---|
266 | pDelete(&h); |
---|
267 | pDelete(&nf); |
---|
268 | PrintS("-"); |
---|
269 | } |
---|
270 | } |
---|
271 | if (!(rField_is_Domain(currRing))) |
---|
272 | { |
---|
273 | PrintS(" Yes!\nzero-spoly --> 0?"); |
---|
274 | for (i = 0; i < IDELEMS(GI); i++) |
---|
275 | { |
---|
276 | f = plain_zero_spoly(GI->m[i]); |
---|
277 | nf = ringNF(f, GI, currRing); |
---|
278 | if (nf != NULL) { |
---|
279 | PrintS("spoly("); |
---|
280 | wrp(GI->m[i]); |
---|
281 | PrintS(", "); |
---|
282 | wrp(0); |
---|
283 | PrintS(") = "); |
---|
284 | wrp(h); |
---|
285 | PrintS(" --> "); |
---|
286 | wrp(nf); |
---|
287 | PrintLn(); |
---|
288 | return(0); |
---|
289 | } |
---|
290 | pDelete(&f); |
---|
291 | pDelete(&nf); |
---|
292 | PrintS("-"); |
---|
293 | } |
---|
294 | } |
---|
295 | PrintS(" Yes!"); |
---|
296 | PrintLn(); |
---|
297 | return(1); |
---|
298 | } |
---|
299 | |
---|
300 | #endif |
---|