My Project
Loading...
Searching...
No Matches
ringgb.cc
Go to the documentation of this file.
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"
12#include "kernel/structs.h"
13#include "kernel/polys.h"
15#include "kernel/ideals.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
27
28poly reduce_poly_fct(poly p, ring r)
29{
30 return kFindZeroPoly(p, r, r);
31}
32
33/*
34 * Returns maximal k, such that
35 * 2^k | n
36 */
37int indexOf2(number n)
38{
39 long test = (long) n;
40 int i = 0;
41 while (test%2 == 0)
42 {
43 i++;
44 test = test / 2;
45 }
46 return i;
47}
48
49/***************************************************************
50 *
51 * Lcm business
52 *
53 ***************************************************************/
54// get m1 = LCM(LM(p1), LM(p2))/LM(p1)
55// m2 = LCM(LM(p1), LM(p2))/LM(p2)
56BOOLEAN ring2toM_GetLeadTerms(const poly p1, const poly p2, const ring p_r,
57 poly &m1, poly &m2, const ring m_r)
58{
59 int i;
60 int x;
61 m1 = p_Init(m_r);
62 m2 = p_Init(m_r);
63
64 for (i = p_r->N; i; i--)
65 {
66 x = p_GetExpDiff(p1, p2, i, p_r);
67 if (x > 0)
68 {
69 p_SetExp(m2,i,x, m_r);
70 p_SetExp(m1,i,0, m_r);
71 }
72 else
73 {
74 p_SetExp(m1,i,-x, m_r);
75 p_SetExp(m2,i,0, m_r);
76 }
77 }
78 p_Setm(m1, m_r);
79 p_Setm(m2, m_r);
80 long cp1 = (long) pGetCoeff(p1);
81 long cp2 = (long) pGetCoeff(p2);
82 if (cp1 != 0 && cp2 != 0)
83 {
84 while (cp1%2 == 0 && cp2%2 == 0)
85 {
86 cp1 = cp1 / 2;
87 cp2 = cp2 / 2;
88 }
89 }
90 p_SetCoeff(m1, (number) cp2, m_r);
91 p_SetCoeff(m2, (number) cp1, m_r);
92 return TRUE;
93}
94
95void printPolyMsg(const char * start, poly f, const char * end)
96{
97 PrintS(start);
98 wrp(f);
99 PrintS(end);
100}
101
102poly spolyRing2toM(poly f, poly g, ring r)
103{
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
116poly ringRedNF (poly f, ideal G, ring r)
117{
118 // If f = 0, then normal form is also 0
119 if (f == NULL) { return NULL; }
120 poly h = NULL;
121 poly g = pCopy(f);
122 int c = 0;
123 while (g != NULL)
124 {
125 Print("%d-step RedNF - g=", c);
126 wrp(g);
127 PrintS(" | h=");
128 wrp(h);
129 PrintLn();
130 g = ringNF(g, G, r);
131 if (g != NULL) {
132 h = pAdd(h, pHead(g));
133 pLmDelete(&g);
134 }
135 c++;
136 }
137 return h;
138}
139
140/*
141 * Find an index i from G, such that
142 * LT(rside) = x * LT(G[i]) has a solution
143 * or -1 if rside is not in the
144 * ideal of the leading coefficients
145 * of the suitable g from G.
146 */
147int findRingSolver(poly rside, ideal G, ring r)
148{
149 if (rside == NULL) return -1;
150 int i;
151// int iO2rside = indexOf2(pGetCoeff(rside));
152 for (i = 0; i < IDELEMS(G); i++)
153 {
154 if // (indexOf2(pGetCoeff(G->m[i])) <= iO2rside && / should not be necessary any more
155 (p_LmDivisibleBy(G->m[i], rside, r))
156 {
157 return i;
158 }
159 }
160 return -1;
161}
162
163poly plain_spoly(poly f, poly g)
164{
165 number cf = nCopy(pGetCoeff(f)), cg = nCopy(pGetCoeff(g));
166 (void)ksCheckCoeff(&cf, &cg, currRing->cf); // gcd and zero divisors
167 poly fm, gm;
168 k_GetLeadTerms(f, g, currRing, fm, gm, currRing);
169 pSetCoeff0(fm, cg);
170 pSetCoeff0(gm, cf); // and now, m1 * LT(p1) == m2 * LT(p2)
171 poly sp = pSub(ppMult_mm(f, fm), ppMult_mm(g, gm));
172 pDelete(&fm);
173 pDelete(&gm);
174 return(sp);
175}
176
177/*2
178* Generates spoly(0, h) if applicable. Assumes ring has zero divisors
179*/
181{
182 poly p = NULL;
183 number zero=n_Init(0,currRing->cf);
184 number gcd = n_Gcd(zero, pGetCoeff(h), currRing->cf);
185 if (!n_IsOne( gcd, currRing->cf ))
186 {
187 number tmp=n_Ann(gcd,currRing->cf);
188 p = p_Copy(h->next, currRing);
189 p = __p_Mult_nn(p, tmp, currRing);
190 n_Delete(&tmp,currRing->cf);
191 }
192 n_Delete(&zero,currRing->cf);
193 return p;
194}
195
196poly ringNF(poly f, ideal G, ring r)
197{
198 // If f = 0, then normal form is also 0
199 if (f == NULL) { return NULL; }
200 poly tmp = NULL;
201 poly h = pCopy(f);
202 int i = findRingSolver(h, G, r);
203 int c = 1;
204 while (h != NULL && i >= 0) {
205// Print("%d-step NF - h:", c);
206// wrp(h);
207// PrintS(" ");
208// PrintS("G->m[i]:");
209// wrp(G->m[i]);
210// PrintLn();
211 tmp = h;
212 h = plain_spoly(h, G->m[i]);
213 pDelete(&tmp);
214// PrintS("=> h=");
215// wrp(h);
216// PrintLn();
217 i = findRingSolver(h, G, r);
218 c++;
219 }
220 return h;
221}
222
223int testGB(ideal I, ideal GI) {
224 poly f, g, h, nf;
225 int i = 0;
226 int j = 0;
227 PrintS("I included?");
228 for (i = 0; i < IDELEMS(I); i++) {
229 if (ringNF(I->m[i], GI, currRing) != NULL) {
230 PrintS("Not reduced to zero from I: ");
231 wrp(I->m[i]);
232 PrintS(" --> ");
233 wrp(ringNF(I->m[i], GI, currRing));
234 PrintLn();
235 return(0);
236 }
237 PrintS("-");
238 }
239 PrintS(" Yes!\nspoly --> 0?");
240 for (i = 0; i < IDELEMS(GI); i++)
241 {
242 for (j = i + 1; j < IDELEMS(GI); j++)
243 {
244 f = pCopy(GI->m[i]);
245 g = pCopy(GI->m[j]);
246 h = plain_spoly(f, g);
247 nf = ringNF(h, GI, currRing);
248 if (nf != NULL)
249 {
250 PrintS("spoly(");
251 wrp(GI->m[i]);
252 PrintS(", ");
253 wrp(GI->m[j]);
254 PrintS(") = ");
255 wrp(h);
256 PrintS(" --> ");
257 wrp(nf);
258 PrintLn();
259 return(0);
260 }
261 pDelete(&f);
262 pDelete(&g);
263 pDelete(&h);
264 pDelete(&nf);
265 PrintS("-");
266 }
267 }
269 {
270 PrintS(" Yes!\nzero-spoly --> 0?");
271 for (i = 0; i < IDELEMS(GI); i++)
272 {
273 f = plain_zero_spoly(GI->m[i]);
274 nf = ringNF(f, GI, currRing);
275 if (nf != NULL) {
276 PrintS("spoly(");
277 wrp(GI->m[i]);
278 PrintS(", ");
279 wrp(0);
280 PrintS(") = ");
281 wrp(h);
282 PrintS(" --> ");
283 wrp(nf);
284 PrintLn();
285 return(0);
286 }
287 pDelete(&f);
288 pDelete(&nf);
289 PrintS("-");
290 }
291 }
292 PrintS(" Yes!");
293 PrintLn();
294 return(1);
295}
int BOOLEAN
Definition auxiliary.h:88
#define TRUE
Definition auxiliary.h:101
int i
Definition cfEzgcd.cc:132
Variable x
Definition cfModGcd.cc:4090
int p
Definition cfModGcd.cc:4086
g
Definition cfModGcd.cc:4098
CanonicalForm cf
Definition cfModGcd.cc:4091
CanonicalForm test
Definition cfModGcd.cc:4104
CanonicalForm cg
Definition cfModGcd.cc:4091
FILE * f
Definition checklibs.c:9
static FORCE_INLINE number n_Gcd(number a, number b, const coeffs r)
in Z: return the gcd of 'a' and 'b' in Z/nZ, Z/2^kZ: computed as in the case Z in Z/pZ,...
Definition coeffs.h:665
static FORCE_INLINE number n_Ann(number a, const coeffs r)
if r is a ring with zero divisors, return an annihilator!=0 of b otherwise return NULL
Definition coeffs.h:680
static FORCE_INLINE void n_Delete(number *p, const coeffs r)
delete 'p'
Definition coeffs.h:459
static FORCE_INLINE number n_Init(long i, const coeffs r)
a number representing i in the given coeff field/ring r
Definition coeffs.h:539
static FORCE_INLINE BOOLEAN n_IsOne(number n, const coeffs r)
TRUE iff 'n' represents the one element.
Definition coeffs.h:472
#define Print
Definition emacs.cc:80
int j
Definition facHensel.cc:110
STATIC_VAR TreeM * G
Definition janet.cc:31
STATIC_VAR Poly * h
Definition janet.cc:971
KINLINE BOOLEAN k_GetLeadTerms(const poly p1, const poly p2, const ring p_r, poly &m1, poly &m2, const ring m_r)
Definition kInline.h:1015
int ksCheckCoeff(number *a, number *b, const coeffs r)
Definition kbuckets.cc:1504
poly kFindZeroPoly(poly input_p, ring leadRing, ring tailRing)
Definition kstd2.cc:613
#define pSetCoeff0(p, n)
Definition monomials.h:59
static number & pGetCoeff(poly p)
return an alias to the leading coefficient of p assumes that p != NULL NOTE: not copy
Definition monomials.h:44
#define nCopy(n)
Definition numbers.h:15
#define NULL
Definition omList.c:12
static long p_GetExpDiff(poly p1, poly p2, int i, ring r)
Definition p_polys.h:637
static poly pp_Mult_mm(poly p, poly m, const ring r)
Definition p_polys.h:1033
static unsigned long p_SetExp(poly p, const unsigned long e, const unsigned long iBitmask, const int VarOffset)
set a single variable exponent @Note: VarOffset encodes the position in p->exp
Definition p_polys.h:490
static void p_Setm(poly p, const ring r)
Definition p_polys.h:235
static number p_SetCoeff(poly p, number n, ring r)
Definition p_polys.h:414
static BOOLEAN p_LmDivisibleBy(poly a, poly b, const ring r)
Definition p_polys.h:1907
static poly p_Mult_mm(poly p, poly m, const ring r)
Definition p_polys.h:1053
static poly p_Init(const ring r, omBin bin)
Definition p_polys.h:1336
static poly p_Copy(poly p, const ring r)
returns a copy of p
Definition p_polys.h:848
#define __p_Mult_nn(p, n, r)
Definition p_polys.h:973
VAR ring currRing
Widely used global variable which specifies the current polynomial ring for Singular interpreter and ...
Definition polys.cc:13
Compatibility layer for legacy polynomial operations (over currRing)
#define pAdd(p, q)
Definition polys.h:204
#define pDelete(p_ptr)
Definition polys.h:187
#define pHead(p)
returns newly allocated copy of Lm(p), coef is copied, next=NULL, p might be NULL
Definition polys.h:68
#define ppMult_mm(p, m)
Definition polys.h:202
#define pSub(a, b)
Definition polys.h:288
#define pLmDelete(p)
assume p != NULL, deletes Lm(p)->coef and Lm(p)
Definition polys.h:77
void wrp(poly p)
Definition polys.h:311
#define pCopy(p)
return a copy of the poly
Definition polys.h:186
void PrintS(const char *s)
Definition reporter.cc:284
void PrintLn()
Definition reporter.cc:310
static BOOLEAN rField_is_Domain(const ring r)
Definition ring.h:493
poly reduce_poly_fct(poly p, ring r)
Definition ringgb.cc:28
poly ringNF(poly f, ideal G, ring r)
Definition ringgb.cc:196
int findRingSolver(poly rside, ideal G, ring r)
Definition ringgb.cc:147
void printPolyMsg(const char *start, poly f, const char *end)
Definition ringgb.cc:95
poly plain_spoly(poly f, poly g)
Definition ringgb.cc:163
poly spolyRing2toM(poly f, poly g, ring r)
Definition ringgb.cc:102
BOOLEAN ring2toM_GetLeadTerms(const poly p1, const poly p2, const ring p_r, poly &m1, poly &m2, const ring m_r)
Definition ringgb.cc:56
poly plain_zero_spoly(poly h)
Definition ringgb.cc:180
poly ringRedNF(poly f, ideal G, ring r)
Definition ringgb.cc:116
int indexOf2(number n)
Definition ringgb.cc:37
int testGB(ideal I, ideal GI)
Definition ringgb.cc:223
#define IDELEMS(i)
Definition gnumpfl.cc:25
int gcd(int a, int b)