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