1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: ringgb.cc,v 1.9 2006-06-08 22:33:36 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 | * Find an index i from G, such that |
---|
53 | * LT(rside) = x * LT(G[i]) has a solution |
---|
54 | * or -1 if rside is not in the |
---|
55 | * ideal of the leading coefficients |
---|
56 | * of the suitable g from G. |
---|
57 | */ |
---|
58 | int findRing2toMsolver(poly rside, ideal G, ring r) { |
---|
59 | if (rside == NULL) return -1; |
---|
60 | int i; |
---|
61 | int iO2rside = indexOf2(pGetCoeff(rside)); |
---|
62 | for (i = 0; i < IDELEMS(G); i++) { |
---|
63 | if (indexOf2(pGetCoeff(G->m[i])) <= iO2rside && p_LmDivisibleBy(G->m[i], rside, r)) { |
---|
64 | return i; |
---|
65 | } |
---|
66 | } |
---|
67 | return -1; |
---|
68 | } |
---|
69 | |
---|
70 | /*************************************************************** |
---|
71 | * |
---|
72 | * Lcm business |
---|
73 | * |
---|
74 | ***************************************************************/ |
---|
75 | // get m1 = LCM(LM(p1), LM(p2))/LM(p1) |
---|
76 | // m2 = LCM(LM(p1), LM(p2))/LM(p2) |
---|
77 | BOOLEAN ring2toM_GetLeadTerms(const poly p1, const poly p2, const ring p_r, |
---|
78 | poly &m1, poly &m2, const ring m_r) |
---|
79 | { |
---|
80 | |
---|
81 | int i; |
---|
82 | Exponent_t x; |
---|
83 | m1 = p_Init(m_r); |
---|
84 | m2 = p_Init(m_r); |
---|
85 | |
---|
86 | for (i = p_r->N; i; i--) |
---|
87 | { |
---|
88 | x = p_GetExpDiff(p1, p2, i, p_r); |
---|
89 | if (x > 0) |
---|
90 | { |
---|
91 | p_SetExp(m2,i,x, m_r); |
---|
92 | p_SetExp(m1,i,0, m_r); |
---|
93 | } |
---|
94 | else |
---|
95 | { |
---|
96 | p_SetExp(m1,i,-x, m_r); |
---|
97 | p_SetExp(m2,i,0, m_r); |
---|
98 | } |
---|
99 | } |
---|
100 | p_Setm(m1, m_r); |
---|
101 | p_Setm(m2, m_r); |
---|
102 | long cp1 = (long) pGetCoeff(p1); |
---|
103 | long cp2 = (long) pGetCoeff(p2); |
---|
104 | if (cp1 != 0 && cp2 != 0) { |
---|
105 | while (cp1%2 == 0 && cp2%2 == 0) { |
---|
106 | cp1 = cp1 / 2; |
---|
107 | cp2 = cp2 / 2; |
---|
108 | } |
---|
109 | } |
---|
110 | p_SetCoeff(m1, (number) cp2, m_r); |
---|
111 | p_SetCoeff(m2, (number) cp1, m_r); |
---|
112 | return TRUE; |
---|
113 | } |
---|
114 | |
---|
115 | void printPolyMsg(const char * start, poly f, const char * end) |
---|
116 | { |
---|
117 | PrintS(start); |
---|
118 | wrp(f); |
---|
119 | PrintS(end); |
---|
120 | } |
---|
121 | |
---|
122 | poly plain_spoly(poly f, poly g) { |
---|
123 | number cf = pGetCoeff(f), cg = pGetCoeff(g); |
---|
124 | int ct = ksCheckCoeff(&cf, &cg); // gcd and zero divisors |
---|
125 | poly fm, gm; |
---|
126 | k_GetLeadTerms(f, g, currRing, fm, gm, currRing); |
---|
127 | pSetCoeff0(fm, cg); |
---|
128 | pSetCoeff0(gm, cf); // and now, m1 * LT(p1) == m2 * LT(p2) |
---|
129 | poly sp = pSub(pMult_mm(f, fm), pMult_mm(g, gm)); |
---|
130 | pDelete(&fm); |
---|
131 | pDelete(&gm); |
---|
132 | return(sp); |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | poly spolyRing2toM(poly f, poly g, ring r) { |
---|
137 | poly m1 = NULL; |
---|
138 | poly m2 = NULL; |
---|
139 | ring2toM_GetLeadTerms(f, g, r, m1, m2, r); |
---|
140 | // printPolyMsg("spoly: m1=", m1, " | "); |
---|
141 | // printPolyMsg("m2=", m2, ""); |
---|
142 | // PrintLn(); |
---|
143 | poly sp = pSub(p_Mult_mm(f, m1, r), pp_Mult_mm(g, m2, r)); |
---|
144 | pDelete(&m1); |
---|
145 | pDelete(&m2); |
---|
146 | return(sp); |
---|
147 | } |
---|
148 | |
---|
149 | poly ringNF(poly f, ideal G, ring r) { |
---|
150 | // If f = 0, then normal form is also 0 |
---|
151 | if (f == NULL) { return NULL; } |
---|
152 | poly h = pCopy(f); |
---|
153 | int i = findRing2toMsolver(h, G, r); |
---|
154 | int c = 1; |
---|
155 | while (h != NULL && i >= 0) { |
---|
156 | // Print("%d-step NF - h:", c); |
---|
157 | // wrp(h); |
---|
158 | // PrintS(" "); |
---|
159 | // PrintS("G->m[i]:"); |
---|
160 | // wrp(G->m[i]); |
---|
161 | // PrintLn(); |
---|
162 | h = spolyRing2toM(h, G->m[i], r); |
---|
163 | // PrintS("=> h="); |
---|
164 | // wrp(h); |
---|
165 | // PrintLn(); |
---|
166 | i = findRing2toMsolver(h, G, r); |
---|
167 | c++; |
---|
168 | } |
---|
169 | return h; |
---|
170 | } |
---|
171 | |
---|
172 | poly ringRedNF (poly f, ideal G, ring r) { |
---|
173 | // If f = 0, then normal form is also 0 |
---|
174 | if (f == NULL) { return NULL; } |
---|
175 | poly h = NULL; |
---|
176 | poly g = pCopy(f); |
---|
177 | int c = 0; |
---|
178 | while (g != NULL) { |
---|
179 | Print("%d-step RedNF - g=", c); |
---|
180 | wrp(g); |
---|
181 | PrintS(" | h="); |
---|
182 | wrp(h); |
---|
183 | PrintLn(); |
---|
184 | g = ringNF(g, G, r); |
---|
185 | if (g != NULL) { |
---|
186 | h = pAdd(h, pHead(g)); |
---|
187 | pLmDelete(&g); |
---|
188 | } |
---|
189 | c++; |
---|
190 | } |
---|
191 | return h; |
---|
192 | } |
---|
193 | |
---|
194 | int testGB(ideal GI) { |
---|
195 | poly f, g, h; |
---|
196 | int i = 0; |
---|
197 | int j = 0; |
---|
198 | for (i = 0; i < IDELEMS(GI) - 1; i++) { |
---|
199 | for (j = i + 1; j < IDELEMS(GI); j++) { |
---|
200 | f = pCopy(GI->m[i]); |
---|
201 | g = pCopy(GI->m[j]); |
---|
202 | h = plain_spoly(f, g); |
---|
203 | if (ringNF(h, GI, currRing) != NULL) { |
---|
204 | wrp(GI->m[i]); |
---|
205 | PrintLn(); |
---|
206 | wrp(GI->m[j]); |
---|
207 | PrintLn(); |
---|
208 | wrp(h); |
---|
209 | PrintLn(); |
---|
210 | wrp(ringNF(h, GI, currRing)); |
---|
211 | PrintLn(); |
---|
212 | return(0); |
---|
213 | } |
---|
214 | pDelete(&h); |
---|
215 | } |
---|
216 | } |
---|
217 | return(1); |
---|
218 | } |
---|
219 | |
---|
220 | #endif |
---|