Annotation of OpenXM/src/kan96xx/Kan/gb.c, Revision 1.10
1.10 ! takayama 1: /* $OpenXM: OpenXM/src/kan96xx/Kan/gb.c,v 1.9 2005/06/09 04:09:22 takayama Exp $ */
1.5 takayama 2: /* untabify on May 4, 2001 */
1.1 maekawa 3: #include <stdio.h>
4: #include "datatype.h"
5: #include "extern2.h"
6: #include "matrix.h"
7: #include "gradedset.h"
8:
9: #define INITGRADE 4
10: #define INITSIZE 2
1.6 takayama 11: #define DMAX 100
1.1 maekawa 12:
13: int KanGBmessage = 1;
14:
15: static int Debug = 0;
16: static int sugarGrade(struct pair *inode,struct gradedPolySet *grG,
1.5 takayama 17: POLY gt,int gtSugarGrade);
1.1 maekawa 18: extern int Sugar;
19: extern int Statistics;
20: extern int Criterion1;
21: extern int UseCriterion1;
22: extern int UseCriterion2B;
23: extern int Spairs;
24: extern int Criterion2B, Criterion2F, Criterion2M;
1.3 takayama 25: extern int AutoReduce;
1.8 takayama 26: extern int TraceLift;
27: extern struct ring *TraceLift_ringmod;
1.6 takayama 28: static int MaxLength[DMAX];
29: static int SpNumber[DMAX];
1.1 maekawa 30:
31: struct gradedPairs *updatePairs(grD,gt,gtGrade,t,grG)
1.5 takayama 32: struct gradedPairs *grD; /* set of pairs */
33: POLY gt; /* new polynomial */
34: int gtGrade;
35: int t;
36: struct gradedPolySet *grG; /* (f,gt), f \in grG, should be added to grD. */
1.1 maekawa 37: {
38: int gmax,newGrade;
39: struct pair *node,*new,*inode,*jnode;
40: int i,k;
41: struct polySet *g;
42: POLY lcmp;
43: POLY it,jt;
44: extern int Verbose;
45:
46:
47: if (Verbose) {
48: printf("\nupdatePairs(): ");
49: }
50:
51: gmax = grG->maxGrade;
52: node = new = newPair((struct pair *)NULL);
53: /* (f,gt) are stored in new as a list. */
54: for (k=0; k<gmax; k++) {
55: g = grG->polys[k];
56: for (i=0; i<g->size; i++) {
57: if (g->del[i] == 0 && (gtGrade != k || t != i)) {
1.5 takayama 58: lcmp = (*lcm)(g->g[i],gt);
59: if (lcmp == ZERO) {
1.1 maekawa 60:
1.5 takayama 61: }else {
62: new->del = 0;
63: new->next = newPair(new);
64: new = new->next;
65: new->lcm = lcmp;
66: new->ig = k; new->ii = i; /* g->g[i] */
67: new->jg = gtGrade; new->ji = t; /* gt */
68: new->grade = -1; /* one do not need to set grade here. */
69: }
1.1 maekawa 70: }
71: }
72: }
73:
74:
75: /* Check the criterion */
76: /* See Gebauer and Mora, 1988, Journal of Symbolic Computation
77: 279. We must not use the test T(i)T(j) = T(i,j) because it
78: does not hold in the ring of diff op.
1.5 takayama 79: */
1.1 maekawa 80:
81: inode = node->next;
82: while (inode != (struct pair *)NULL) {
83: jnode = inode->next;
84: while (jnode != (struct pair *)NULL) {
85: it = inode->lcm;
86: jt = jnode->lcm;
87: if ((*mmLarger)(it,jt) == 2) {
1.5 takayama 88: /* F(j,t), i<j */
89: jnode->del = 1;
90: if (Verbose) printf("F[%d,%d]([%d,%d],[%d,%d]) ",
91: inode->ig,inode->ii,
92: jnode->ig,jnode->ii,
93: gtGrade,t);
94: Criterion2F++;
1.1 maekawa 95: }else {
1.5 takayama 96: /* g[i] > g[j] ?, M(i,t) */
97: if ((*isReducible)(it,jt)) {
98: inode->del = 1;
99: if (Verbose) printf("M[%d,%d]([%d,%d],[%d,%d]) ",
100: jnode->ig,jnode->ii,
101: inode->ig,inode->ii,
102: gtGrade,t);
103: Criterion2M++;
104: }
1.1 maekawa 105: /* M(j,t) */
1.5 takayama 106: if ((*isReducible)(jt,it)) {
107: jnode->del = 1;
108: if (Verbose) printf("M[%d,%d]([%d,%d],[%d,%d]) ",
109: inode->ig,inode->ii,
110: jnode->ig,jnode->ii,
111: gtGrade,t);
112: Criterion2M++;
113: }
1.1 maekawa 114: }
115: jnode = jnode->next;
116: }
117: inode = inode->next;
118: }
1.5 takayama 119:
1.1 maekawa 120: if (UseCriterion1) {
121: inode = node->next;
122: while (inode != NULL) {
123: if (inode->del == 0) {
1.5 takayama 124: if (criterion1(gt,grG->polys[inode->ig]->g[inode->ii],inode->lcm)) {
125: inode->del = 1;
126: if (Verbose) printf("Criterion1([%d,%d],[%d,%d]) ",
127: inode->ig,inode->ii,
128: gtGrade,t);
129: Criterion1++;
130: }
1.1 maekawa 131: }
132: inode = inode->next;
133: }
134: }
135:
136: if (UseCriterion2B) {
137: Criterion2B += deletePairByCriterion2B(grD,gt,grG);
138: }
139:
140: /* Merge to grD */
141: inode = node->next;
142: if (Debug) outputNode(inode);
143: while (inode != (struct pair *)NULL) {
144: if (inode->del == 0) {
145: if (Sugar) {
1.5 takayama 146: inode->grade = sugarGrade(inode,grG,gt,gtGrade);
1.1 maekawa 147: }else{
1.5 takayama 148: inode->grade = (*grade)(inode->lcm);
1.1 maekawa 149: }
150: if (grD->lim <= inode->grade) {
1.5 takayama 151: grD = enlargeGradedPairs(2*(inode->grade)+1,grD);
1.1 maekawa 152: }
153: insertPair(inode,grD->pairs[inode->grade]);
154: grD->maxGrade = max(grD->maxGrade,inode->grade+1); /* don't forget */
155: }
156: inode = inode->next;
157: }
158: if (Debug) printf("OK.\n");
159: if (Verbose) {
160: printf(" Remaining pairs are %d. maxGrade=%d\n",countPairs(grD),grD->maxGrade);
161: }
162: return(grD);
163: }
164:
1.10 ! takayama 165: struct gradedPolySet *groebner_gen(f,needBack,needSyz,grP,countDown,forceReduction,reduceOnly,gbCheck)
1.5 takayama 166: struct arrayOfPOLY *f;
167: int needBack;
168: int needSyz;
169: struct pair **grP; /* if (needSyz), it is set. */
170: int countDown;
171: int forceReduction;
1.9 takayama 172: int reduceOnly;
1.10 ! takayama 173: int gbCheck;
1.1 maekawa 174: {
175: int r;
176: struct gradedPolySet *g;
177: struct gradedPairs *d;
178: int i;
179: int grade,indx;
180: POLY gt;
181: struct pair *top;
182: int ig,ii,jg,ji;
183: POLY gi,gj;
184: struct spValue h;
185: struct syz0 syz;
186: int pgrade = 0;
187: POLY rd;
188: POLY syzPoly;
189: POLY syzCf;
190: struct syz0 *syzp;
191: int serial;
192: struct pair *listP;
193: extern int Verbose;
194: extern int ReduceLowerTerms;
195: extern int StopDegree;
196: extern struct ring *CurrentRingp;
197: extern char *F_mpMult;
198: struct ring *rp;
1.8 takayama 199: int first;
1.6 takayama 200: int statisticsPL, statisticsCount;
1.1 maekawa 201:
1.9 takayama 202:
1.6 takayama 203: if (Statistics) {
204: for (i=0; i<DMAX; i++) MaxLength[i] = SpNumber[i] = 0;
205: }
1.1 maekawa 206: r = f->n;
207: if (r<=0) return((struct gradedPolySet *)NULL);
208: if (UseCriterion1) {
209: if (needSyz) {
210: warningGradedSet("You cannot use both the options needSyz and UseCriterion1.\nInput [(UseCriterion1) 0] system_variable to turn off the use of the Criterion 1.");
211: }
212: if (strcmp(F_mpMult,"poly")!=0) {
213: warningGradedSet("You can use the option UseCriterion1 only for the computation in the ring of polynomials.\nInput [(UseCriterion1) 0] system_variable to turn off the use of the Criterion 1.");
214: }
215: }
216:
217: Spairs = Criterion1 = Criterion2B = Criterion2F = Criterion2M = 0;
218:
1.10 ! takayama 219: g = newGradedPolySet(INITGRADE); g->gb = 1;
1.1 maekawa 220: d = newGradedPairs(INITGRADE*2);
221: for (i=0; i<g->lim; i++) {
222: g->polys[i] = newPolySet(INITSIZE);
223: }
224:
1.8 takayama 225: first = 1;
1.1 maekawa 226: for (i=0; i<r; i++) {
227: gt = getArrayOfPOLY(f,i);
228: if (gt ISZERO) { rp = CurrentRingp; } else { rp = gt->m->ringp; }
1.8 takayama 229: if (TraceLift && (!(gt ISZERO)) && first) {
230: TraceLift_ringmod = newRingOverFp(rp,getPrime(TraceLift)); first = 0;
231: if (KanGBmessage) printf("Prime number for the trace lift is %d.\n",
232: TraceLift_ringmod->p);
233: }
1.1 maekawa 234: grade = -1; whereInG(g,gt,&grade,&indx,Sugar);
235: if (KanGBmessage == 2) {
236: printf("init=%s, ",POLYToString(head(gt),'*',1));
237: printf("(gr,indx)=(%d,%d).\n",grade,indx);
238: }
239: d = updatePairs(d,gt,grade,indx,g);
240: serial = i;
241: if (!needBack) {
242: g = putPolyInG(g,gt,grade,indx,
1.5 takayama 243: (struct syz0 *)NULL,1,serial);
1.1 maekawa 244: }else {
245: syzp = newSyz0();
246: syzp->cf = cxx(1,0,0,rp);
247: syzp->syz = toSyzPoly(cxx(1,0,0,rp),grade,indx);
248: g = putPolyInG(g,gt,grade,indx,syzp,1,serial);
249: }
250:
1.7 takayama 251: /* markRedundant0(g,grade,indx); ?*/
252: markGeneratorInG(g,grade,indx); /*?*/
1.1 maekawa 253: if (Debug) {
254: outputGradedPairs(d); outputGradedPolySet(g,needSyz);
255: }
256: }
257: if (needSyz) {
258: *grP = newPair((struct pair *)NULL);
259: listP = *grP;
260: }
261:
262: while ((top = getPair(d)) != (struct pair *)NULL) {
263: ig = top->ig; ii = top->ii; /* [ig,ii] */
264: jg = top->jg; ji = top->ji; /* [jg,ji] */
1.7 takayama 265: /*
266: if (g->polys[ig]->del[ii] || g->polys[jg]->del[ji]) {
267: if (KanGBmessage) printf("p");
268: continue;
269: } Don't do this.
270: */
1.1 maekawa 271: gi = g->polys[ig]->g[ii];
272: gj = g->polys[jg]->g[ji];
273:
274: Spairs++;
1.9 takayama 275: if (reduceOnly && (!needSyz) && (!needBack)) h = spZero(); /* rd = 0 */
276: else h = (*sp)(gi,gj);
1.1 maekawa 277: rd = ppAddv(ppMult(h.a,gi),ppMult(h.b,gj));
1.6 takayama 278:
279: if (Statistics) {
280: if (top->grade >=0 && top->grade < DMAX) {
281: statisticsPL = pLength(rd);
282: SpNumber[top->grade]++;
283: if (MaxLength[top->grade] < statisticsPL) {
284: MaxLength[top->grade] = statisticsPL;
285: }
286: }
287: }
288:
1.1 maekawa 289: if (!Sugar || forceReduction) {
290: rd = (*reduction)(rd,g,needBack,&syz);
291: }else{
292: rd = reduction_sugar(rd,g,needBack,&syz,top->grade);
293: }
294: syzPoly = syz.syz;
295: syzCf = syz.cf;
296:
297: if (KanGBmessage) {
298: if (KanGBmessage == 2) {
1.5 takayama 299: printf("sp([%d,%d],[%d,%d]), ",ig,ii,jg,ji);
300: if (rd ISZERO) {
301: printf(" 0 \n");
302: } else{
303: printf(" terms=%d, grade=%d, ",pLength(rd),top->grade);
304: printf(" init=%s, ",POLYToString(head(rd),'*',1));
305: }
1.1 maekawa 306: }else{
1.5 takayama 307: if (pgrade != top->grade) {
308: pgrade = top->grade;
309: printf(" %d",pgrade);
310: fflush(stdout);
311: }
312: if (rd ISZERO) {
313: printf("o"); fflush(stdout);
314: }else{
315: printf("."); fflush(stdout);
316: }
1.1 maekawa 317: }
318: }
319:
320: if (!(rd ISZERO)) {
1.10 ! takayama 321: if (gbCheck) {
! 322: /* Abort the calculation. */
! 323: g->gb = 0;
! 324: if (KanGBmessage) {
! 325: printf("gbCheck failed. \n");
! 326: printf("Note that the result is NOT groebner basis.\n");
! 327: }
! 328: break;
! 329: }
1.1 maekawa 330: if (needBack || needSyz) {
1.5 takayama 331: syzp = newSyz0();
332: syzp->cf = syzCf; /* no meaning */
333: syzp->syz = ppAdd(toSyzPoly(h.a,ig,ii),toSyzPoly(h.b,jg,ji));
334: syzp->syz = cpMult(toSyzCoeff(syzCf),syzp->syz);
335: syzp->syz = ppAdd(syzp->syz,syzPoly);
1.1 maekawa 336: }
337:
338: if (ReduceLowerTerms && !(Sugar)) {
1.5 takayama 339: rd = (*reductionCdr)(rd,g,needBack,&syz);
340: if (needBack || needSyz) {
341: /* syzp->cf = ppMult(syz.cf,syzp->cf); no meaning */
342: syzp->syz = ppAdd(syz.syz,
343: cpMult(toSyzCoeff(syz.cf),syzp->syz));
344: }
1.1 maekawa 345: }
346:
347: if(Sugar && (!forceReduction)){grade=top->grade;}else{grade=-1;}whereInG(g,rd,&grade,&indx,Sugar);
348: if (KanGBmessage == 2) {
1.5 takayama 349: printf("(gr,indx)=(%d,%d).\n",grade,indx);
350: /*
351: printf("sp(%s,%s)-->%s\n",POLYToString(gi,' ',1),
352: POLYToString(gj,' ',1),
353: POLYToString(rd,' ',1));
354: */
1.1 maekawa 355: }
1.5 takayama 356:
1.1 maekawa 357: d = updatePairs(d,rd,grade,indx,g);
358: g = putPolyInG(g,rd,grade,indx,syzp,0,-1);
359: if (Sugar) { markRedundant0(g,grade,indx);}
360: else {markRedundant(g,rd,grade,indx,Sugar);}
361:
362: if (countDown) {
1.5 takayama 363: if (eliminated(rd) == 1) {
364: --countDown;
365: printf("x"); fflush(stdout);
366: if (countDown == 0) {
367: printf("\nThe computation of the Groebner basis is suspended because of countDown==0.\n");
368: printf("Note that the result is NOT groebner basis.\n");
1.10 ! takayama 369: g->gb = 0;
1.5 takayama 370: break;
371: }
372: }
1.1 maekawa 373: }
374: if (Debug) {
1.5 takayama 375: outputGradedPairs(d); outputGradedPolySet(g,needSyz);
1.1 maekawa 376: }
377: }else{
378: if (needSyz) {
1.5 takayama 379: top->syz = ppAdd(toSyzPoly(h.a,ig,ii),toSyzPoly(h.b,jg,ji));
380: top->syz = cpMult(toSyzCoeff(syzCf),top->syz);
381: top->syz = ppAdd(top->syz,syzPoly);
382: listP->next = top; top->prev = listP; listP = listP->next;
383: }
384: }
385: if (StopDegree < pgrade) {
386: fprintf(stderr,"Obtained a partial GB (StopDegree=%d)\n",StopDegree);
387: if (KanGBmessage) {
388: printf("Computation of the Groebner basis is suspended bacause of StopDegree < computing grade.\n");
389: printf("Note that the result is NOT groebner basis.\n");
1.1 maekawa 390: }
1.10 ! takayama 391: g->gb = 0;
1.5 takayama 392: break;
1.1 maekawa 393: }
394: }
395:
396: if (KanGBmessage == 2) {
397: outputGradedPolySet(g,needSyz);
398: }
399: if (KanGBmessage) {
400: if (Sugar) {
401: printf("\nCompleted (GB with sugar).\n");
402: }else{
403: printf("\nCompleted.\n");
404: }
405: }
406:
407: if (Statistics) {
408: printf("\nThe number of s-pairs is %d.\n",Spairs);
409: printf("Criterion1 is applied %d times.\n",Criterion1);
410: printf("Criterions M,F and B are applied M=%d, F=%d, B=%d times.\n",Criterion2M,Criterion2F,Criterion2B);
411: Spairs = Criterion1 = Criterion2M = Criterion2F = Criterion2B = 0;
1.6 takayama 412:
413: printf("degree(number of spolys): maximal polynomial size\n");
414: statisticsCount = 0;
415: for (i=0; i<DMAX; i++) {
416: if (SpNumber[i] > 0) {
417: printf("%3d(%3d): %5d, ",i,SpNumber[i],MaxLength[i]);
418: if (statisticsCount % 4 == 3) {
419: printf("\n");
420: statisticsCount = 0;
421: }else{ statisticsCount++; }
422: }
423: }
424: printf("\n");
1.1 maekawa 425: }
426:
1.9 takayama 427: if (AutoReduce || reduceOnly) {
1.3 takayama 428: toReducedBasis(g,needBack,needSyz);
429: }
1.1 maekawa 430:
431: return(g);
432: }
433:
434:
435: static int sugarGrade(struct pair *inode,struct gradedPolySet *grG,
1.5 takayama 436: POLY gt,int gtSugarGrade)
1.1 maekawa 437: {
438: int a,b,ans;
439: int debug = 0;
440: a = grade_gen(inode->lcm)-grade_gen(grG->polys[inode->ig]->g[inode->ii]);
441: b = grade_gen(inode->lcm)-grade_gen(gt);
442: /* inode = lcm(f_i, gt) = p f_i = q gt modulo lower order terms.
443: a = tdeg(p), b = tdeg(gt);
1.5 takayama 444: */
1.1 maekawa 445: if (debug) {
446: printf("Sugar grade of sp([%d,%d],[%d,%d]) ",inode->ig,inode->ii,
1.5 takayama 447: inode->jg,inode->ji);
1.1 maekawa 448: printf("is max(%d+%d,%d+%d)\n",a,inode->ig,b,gtSugarGrade);
449: }
450: a = a+(inode->ig); /* new sugar degree of p f_i. */
451: b = b + gtSugarGrade; /* new sugar degree of q gt */
452: return( a > b ? a : b);
453: }
454:
1.3 takayama 455: void toReducedBasis(struct gradedPolySet *grP,int needBack, int needSyz)
456: {
457: int changed, grd, i, reduced, grade,indx;
458: struct syz0 syz;
459: struct syz0 *syzp;
460: POLY f;
461: POLY rd;
462: struct polySet *set;
463:
464: /* KanGBmessage=1; */
465: do {
466: if (KanGBmessage) {
467: printf("s"); fflush(stdout);
468: }
469: changed = 0;
470: grd = 0;
471: while (grd < grP->maxGrade) {
472: set = grP->polys[grd];
473: for (i=0; i<set->size; i++) {
1.5 takayama 474: if (set->del[i] == 0) {
475: f = set->g[i];
476: if (KanGBmessage) {
477: /* printf("(%d,%d)",grd,i); */
478: fflush(stdout);
479: }
480: rd = reductionCdr_except_grd_i(f,grP,needBack,&syz,grd,i,&reduced);
481: if (KanGBmessage) {
482: if (reduced) {
483: printf(".");
484: }else{
485: printf("o");
486: }
487: fflush(stdout);
488: }
489: if (reduced) {
490: changed = 1;
491: set->del[i] = 1;
492: if (rd != ZERO) {
493: if (needSyz) {
494: syzp = newSyz0();
495: syzp->cf = syz.cf; /* no meaning */
496: syzp->syz = toSyzPoly(cxx(1,0,0,rd->m->ringp),grd,i);
497: syzp->syz = cpMult(toSyzCoeff(syz.cf),syzp->syz);
498: syzp->syz = ppAdd(syzp->syz,syz.syz);
499: /* rd = c*f + \sum c_{d,i} g_{d,i}
500: c : syz.cf, \sum c_{d,j} g_{d,j} : syz.syz.
501: c*grade^grd*index^i + \sum c_{d,j} grade^d*index^j
502: grP is a set of polynomials. Polynomials are indexed by
503: grade and index.
504: */
505: /* printf("%s, ",POLYToString(syzp->cf,' ',1));
506: printf("%s\n",POLYToString(syzp->syz,' ',1)); */
507: }else{
508: syzp = NULL;
509: }
510: grade = -1; whereInG(grP,rd,&grade,&indx,Sugar);
511: /* Do not forget to set grade to -1 */
512: /* printf("grade=%d, indx=%d, ",grade,indx); */
513: putPolyInG(grP,rd,grade,indx,syzp,0,-1);
514: }
515: }
516: }
1.3 takayama 517: }
518: grd++;
519: }
520: } while(changed);
521: if (KanGBmessage) {
522: printf("Done(reduced basis)\n");
523: }
1.10 ! takayama 524: grP->reduced = 1;
1.3 takayama 525: }
1.1 maekawa 526:
527:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>