Annotation of OpenXM/src/kan96xx/Kan/gb.c, Revision 1.11
1.11 ! takayama 1: /* $OpenXM: OpenXM/src/kan96xx/Kan/gb.c,v 1.10 2005/06/16 06:54:55 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.11 ! takayama 220: if (reduceOnly) g->gb = 2; /* unknown */
1.1 maekawa 221: d = newGradedPairs(INITGRADE*2);
222: for (i=0; i<g->lim; i++) {
223: g->polys[i] = newPolySet(INITSIZE);
224: }
225:
1.8 takayama 226: first = 1;
1.1 maekawa 227: for (i=0; i<r; i++) {
228: gt = getArrayOfPOLY(f,i);
229: if (gt ISZERO) { rp = CurrentRingp; } else { rp = gt->m->ringp; }
1.8 takayama 230: if (TraceLift && (!(gt ISZERO)) && first) {
231: TraceLift_ringmod = newRingOverFp(rp,getPrime(TraceLift)); first = 0;
232: if (KanGBmessage) printf("Prime number for the trace lift is %d.\n",
233: TraceLift_ringmod->p);
234: }
1.1 maekawa 235: grade = -1; whereInG(g,gt,&grade,&indx,Sugar);
236: if (KanGBmessage == 2) {
237: printf("init=%s, ",POLYToString(head(gt),'*',1));
238: printf("(gr,indx)=(%d,%d).\n",grade,indx);
239: }
240: d = updatePairs(d,gt,grade,indx,g);
241: serial = i;
242: if (!needBack) {
243: g = putPolyInG(g,gt,grade,indx,
1.5 takayama 244: (struct syz0 *)NULL,1,serial);
1.1 maekawa 245: }else {
246: syzp = newSyz0();
247: syzp->cf = cxx(1,0,0,rp);
248: syzp->syz = toSyzPoly(cxx(1,0,0,rp),grade,indx);
249: g = putPolyInG(g,gt,grade,indx,syzp,1,serial);
250: }
251:
1.7 takayama 252: /* markRedundant0(g,grade,indx); ?*/
253: markGeneratorInG(g,grade,indx); /*?*/
1.1 maekawa 254: if (Debug) {
255: outputGradedPairs(d); outputGradedPolySet(g,needSyz);
256: }
257: }
258: if (needSyz) {
259: *grP = newPair((struct pair *)NULL);
260: listP = *grP;
261: }
262:
263: while ((top = getPair(d)) != (struct pair *)NULL) {
264: ig = top->ig; ii = top->ii; /* [ig,ii] */
265: jg = top->jg; ji = top->ji; /* [jg,ji] */
1.7 takayama 266: /*
267: if (g->polys[ig]->del[ii] || g->polys[jg]->del[ji]) {
268: if (KanGBmessage) printf("p");
269: continue;
270: } Don't do this.
271: */
1.1 maekawa 272: gi = g->polys[ig]->g[ii];
273: gj = g->polys[jg]->g[ji];
274:
275: Spairs++;
1.9 takayama 276: if (reduceOnly && (!needSyz) && (!needBack)) h = spZero(); /* rd = 0 */
277: else h = (*sp)(gi,gj);
1.1 maekawa 278: rd = ppAddv(ppMult(h.a,gi),ppMult(h.b,gj));
1.6 takayama 279:
280: if (Statistics) {
281: if (top->grade >=0 && top->grade < DMAX) {
282: statisticsPL = pLength(rd);
283: SpNumber[top->grade]++;
284: if (MaxLength[top->grade] < statisticsPL) {
285: MaxLength[top->grade] = statisticsPL;
286: }
287: }
288: }
289:
1.1 maekawa 290: if (!Sugar || forceReduction) {
291: rd = (*reduction)(rd,g,needBack,&syz);
292: }else{
293: rd = reduction_sugar(rd,g,needBack,&syz,top->grade);
294: }
295: syzPoly = syz.syz;
296: syzCf = syz.cf;
297:
298: if (KanGBmessage) {
299: if (KanGBmessage == 2) {
1.5 takayama 300: printf("sp([%d,%d],[%d,%d]), ",ig,ii,jg,ji);
301: if (rd ISZERO) {
302: printf(" 0 \n");
303: } else{
304: printf(" terms=%d, grade=%d, ",pLength(rd),top->grade);
305: printf(" init=%s, ",POLYToString(head(rd),'*',1));
306: }
1.1 maekawa 307: }else{
1.5 takayama 308: if (pgrade != top->grade) {
309: pgrade = top->grade;
310: printf(" %d",pgrade);
311: fflush(stdout);
312: }
313: if (rd ISZERO) {
314: printf("o"); fflush(stdout);
315: }else{
316: printf("."); fflush(stdout);
317: }
1.1 maekawa 318: }
319: }
320:
321: if (!(rd ISZERO)) {
1.10 takayama 322: if (gbCheck) {
323: /* Abort the calculation. */
324: g->gb = 0;
325: if (KanGBmessage) {
326: printf("gbCheck failed. \n");
327: printf("Note that the result is NOT groebner basis.\n");
328: }
329: break;
330: }
1.1 maekawa 331: if (needBack || needSyz) {
1.5 takayama 332: syzp = newSyz0();
333: syzp->cf = syzCf; /* no meaning */
334: syzp->syz = ppAdd(toSyzPoly(h.a,ig,ii),toSyzPoly(h.b,jg,ji));
335: syzp->syz = cpMult(toSyzCoeff(syzCf),syzp->syz);
336: syzp->syz = ppAdd(syzp->syz,syzPoly);
1.1 maekawa 337: }
338:
339: if (ReduceLowerTerms && !(Sugar)) {
1.5 takayama 340: rd = (*reductionCdr)(rd,g,needBack,&syz);
341: if (needBack || needSyz) {
342: /* syzp->cf = ppMult(syz.cf,syzp->cf); no meaning */
343: syzp->syz = ppAdd(syz.syz,
344: cpMult(toSyzCoeff(syz.cf),syzp->syz));
345: }
1.1 maekawa 346: }
347:
348: if(Sugar && (!forceReduction)){grade=top->grade;}else{grade=-1;}whereInG(g,rd,&grade,&indx,Sugar);
349: if (KanGBmessage == 2) {
1.5 takayama 350: printf("(gr,indx)=(%d,%d).\n",grade,indx);
351: /*
352: printf("sp(%s,%s)-->%s\n",POLYToString(gi,' ',1),
353: POLYToString(gj,' ',1),
354: POLYToString(rd,' ',1));
355: */
1.1 maekawa 356: }
1.5 takayama 357:
1.1 maekawa 358: d = updatePairs(d,rd,grade,indx,g);
359: g = putPolyInG(g,rd,grade,indx,syzp,0,-1);
360: if (Sugar) { markRedundant0(g,grade,indx);}
361: else {markRedundant(g,rd,grade,indx,Sugar);}
362:
363: if (countDown) {
1.5 takayama 364: if (eliminated(rd) == 1) {
365: --countDown;
366: printf("x"); fflush(stdout);
367: if (countDown == 0) {
368: printf("\nThe computation of the Groebner basis is suspended because of countDown==0.\n");
369: printf("Note that the result is NOT groebner basis.\n");
1.10 takayama 370: g->gb = 0;
1.5 takayama 371: break;
372: }
373: }
1.1 maekawa 374: }
375: if (Debug) {
1.5 takayama 376: outputGradedPairs(d); outputGradedPolySet(g,needSyz);
1.1 maekawa 377: }
378: }else{
379: if (needSyz) {
1.5 takayama 380: top->syz = ppAdd(toSyzPoly(h.a,ig,ii),toSyzPoly(h.b,jg,ji));
381: top->syz = cpMult(toSyzCoeff(syzCf),top->syz);
382: top->syz = ppAdd(top->syz,syzPoly);
383: listP->next = top; top->prev = listP; listP = listP->next;
384: }
385: }
386: if (StopDegree < pgrade) {
387: fprintf(stderr,"Obtained a partial GB (StopDegree=%d)\n",StopDegree);
388: if (KanGBmessage) {
389: printf("Computation of the Groebner basis is suspended bacause of StopDegree < computing grade.\n");
390: printf("Note that the result is NOT groebner basis.\n");
1.1 maekawa 391: }
1.10 takayama 392: g->gb = 0;
1.5 takayama 393: break;
1.1 maekawa 394: }
395: }
396:
397: if (KanGBmessage == 2) {
398: outputGradedPolySet(g,needSyz);
399: }
400: if (KanGBmessage) {
401: if (Sugar) {
402: printf("\nCompleted (GB with sugar).\n");
403: }else{
404: printf("\nCompleted.\n");
405: }
406: }
407:
408: if (Statistics) {
409: printf("\nThe number of s-pairs is %d.\n",Spairs);
410: printf("Criterion1 is applied %d times.\n",Criterion1);
411: printf("Criterions M,F and B are applied M=%d, F=%d, B=%d times.\n",Criterion2M,Criterion2F,Criterion2B);
412: Spairs = Criterion1 = Criterion2M = Criterion2F = Criterion2B = 0;
1.6 takayama 413:
414: printf("degree(number of spolys): maximal polynomial size\n");
415: statisticsCount = 0;
416: for (i=0; i<DMAX; i++) {
417: if (SpNumber[i] > 0) {
418: printf("%3d(%3d): %5d, ",i,SpNumber[i],MaxLength[i]);
419: if (statisticsCount % 4 == 3) {
420: printf("\n");
421: statisticsCount = 0;
422: }else{ statisticsCount++; }
423: }
424: }
425: printf("\n");
1.1 maekawa 426: }
427:
1.9 takayama 428: if (AutoReduce || reduceOnly) {
1.3 takayama 429: toReducedBasis(g,needBack,needSyz);
430: }
1.1 maekawa 431:
432: return(g);
433: }
434:
435:
436: static int sugarGrade(struct pair *inode,struct gradedPolySet *grG,
1.5 takayama 437: POLY gt,int gtSugarGrade)
1.1 maekawa 438: {
439: int a,b,ans;
440: int debug = 0;
441: a = grade_gen(inode->lcm)-grade_gen(grG->polys[inode->ig]->g[inode->ii]);
442: b = grade_gen(inode->lcm)-grade_gen(gt);
443: /* inode = lcm(f_i, gt) = p f_i = q gt modulo lower order terms.
444: a = tdeg(p), b = tdeg(gt);
1.5 takayama 445: */
1.1 maekawa 446: if (debug) {
447: printf("Sugar grade of sp([%d,%d],[%d,%d]) ",inode->ig,inode->ii,
1.5 takayama 448: inode->jg,inode->ji);
1.1 maekawa 449: printf("is max(%d+%d,%d+%d)\n",a,inode->ig,b,gtSugarGrade);
450: }
451: a = a+(inode->ig); /* new sugar degree of p f_i. */
452: b = b + gtSugarGrade; /* new sugar degree of q gt */
453: return( a > b ? a : b);
454: }
455:
1.3 takayama 456: void toReducedBasis(struct gradedPolySet *grP,int needBack, int needSyz)
457: {
458: int changed, grd, i, reduced, grade,indx;
459: struct syz0 syz;
460: struct syz0 *syzp;
461: POLY f;
462: POLY rd;
463: struct polySet *set;
464:
465: /* KanGBmessage=1; */
466: do {
467: if (KanGBmessage) {
468: printf("s"); fflush(stdout);
469: }
470: changed = 0;
471: grd = 0;
472: while (grd < grP->maxGrade) {
473: set = grP->polys[grd];
474: for (i=0; i<set->size; i++) {
1.5 takayama 475: if (set->del[i] == 0) {
476: f = set->g[i];
477: if (KanGBmessage) {
478: /* printf("(%d,%d)",grd,i); */
479: fflush(stdout);
480: }
481: rd = reductionCdr_except_grd_i(f,grP,needBack,&syz,grd,i,&reduced);
482: if (KanGBmessage) {
483: if (reduced) {
484: printf(".");
485: }else{
486: printf("o");
487: }
488: fflush(stdout);
489: }
490: if (reduced) {
491: changed = 1;
492: set->del[i] = 1;
493: if (rd != ZERO) {
494: if (needSyz) {
495: syzp = newSyz0();
496: syzp->cf = syz.cf; /* no meaning */
497: syzp->syz = toSyzPoly(cxx(1,0,0,rd->m->ringp),grd,i);
498: syzp->syz = cpMult(toSyzCoeff(syz.cf),syzp->syz);
499: syzp->syz = ppAdd(syzp->syz,syz.syz);
500: /* rd = c*f + \sum c_{d,i} g_{d,i}
501: c : syz.cf, \sum c_{d,j} g_{d,j} : syz.syz.
502: c*grade^grd*index^i + \sum c_{d,j} grade^d*index^j
503: grP is a set of polynomials. Polynomials are indexed by
504: grade and index.
505: */
506: /* printf("%s, ",POLYToString(syzp->cf,' ',1));
507: printf("%s\n",POLYToString(syzp->syz,' ',1)); */
508: }else{
509: syzp = NULL;
510: }
511: grade = -1; whereInG(grP,rd,&grade,&indx,Sugar);
512: /* Do not forget to set grade to -1 */
513: /* printf("grade=%d, indx=%d, ",grade,indx); */
514: putPolyInG(grP,rd,grade,indx,syzp,0,-1);
515: }
516: }
517: }
1.3 takayama 518: }
519: grd++;
520: }
521: } while(changed);
522: if (KanGBmessage) {
523: printf("Done(reduced basis)\n");
524: }
1.10 takayama 525: grP->reduced = 1;
1.3 takayama 526: }
1.1 maekawa 527:
528:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>