[BACK]Return to poly4.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / Kan

Annotation of OpenXM/src/kan96xx/Kan/poly4.c, Revision 1.15

1.15    ! takayama    1: /* $OpenXM: OpenXM/src/kan96xx/Kan/poly4.c,v 1.14 2004/07/29 08:13:42 takayama Exp $ */
1.1       maekawa     2: #include <stdio.h>
                      3: #include "datatype.h"
                      4: #include "stackm.h"
                      5: #include "extern.h"
                      6: #include "extern2.h"
                      7: #include "matrix.h"
                      8: static void shell(int v[],int n);
                      9: static int degreeOfPrincipalPart(POLY f);
                     10: static int degreeOfInitW(POLY f,int w[]);
1.10      takayama   11: static int degreeOfInitWS(POLY f,int w[],int s[]);
1.13      takayama   12: static int dDegree(POLY f);
                     13: static POLY dHomogenize(POLY f);
1.1       maekawa    14:
                     15: static void shell(v,n)
1.3       takayama   16:      int v[];
                     17:      int n;
1.1       maekawa    18: {
                     19:   int gap,i,j,temp;
                     20:
                     21:   for (gap = n/2; gap > 0; gap /= 2) {
                     22:     for (i = gap; i<n; i++) {
                     23:       for (j=i-gap ; j>=0 && v[j]<v[j+gap]; j -= gap) {
1.3       takayama   24:         temp = v[j];
                     25:         v[j] = v[j+gap];
                     26:         v[j+gap] = temp;
1.1       maekawa    27:       }
                     28:     }
                     29:   }
                     30: }
                     31:
                     32:
                     33: struct matrixOfPOLY *parts(f,v)
1.3       takayama   34:      POLY f;
                     35:      POLY v;  /* v must be a single variable, e.g. x */
1.1       maekawa    36: {
                     37:   struct matrixOfPOLY *evPoly;
                     38:   int vi = 0;  /* index of v */
                     39:   int vx = 1;  /* x --> 1, D--> 0 */
                     40:   int n,evSize,i,k,e;
                     41:   int *ev;
                     42:   struct object *evList;
                     43:   struct object *list;
1.15    ! takayama   44:   struct object ob = OINIT;
1.1       maekawa    45:   POLY ans;
                     46:   POLY h;
                     47:   extern struct ring *CurrentRingp;
                     48:   POLY ft;
                     49:
                     50:
                     51:   if (f ISZERO || v ISZERO) {
                     52:     evPoly = newMatrixOfPOLY(2,1);
                     53:     getMatrixOfPOLY(evPoly,0,0) = ZERO;
                     54:     getMatrixOfPOLY(evPoly,1,0) = ZERO;
                     55:     return(evPoly);
                     56:   }
                     57:   n = v->m->ringp->n;
                     58:   /* get the index of the variable v */
                     59:   for (i=0; i<n; i++) {
                     60:     if (v->m->e[i].x) {
                     61:       vx = 1; vi = i; break;
                     62:     }else if (v->m->e[i].D) {
                     63:       vx = 0; vi = i; break;
                     64:     }
                     65:   }
                     66:   ft = f;
                     67:   /* get the vector of exponents */
                     68:   evList = NULLLIST;
                     69:   while (ft != POLYNULL) {
                     70:     if (vx) {
                     71:       e = ft->m->e[vi].x;
                     72:     }else{
                     73:       e = ft->m->e[vi].D;
                     74:     }
                     75:     ft = ft->next;
                     76:     ob = KpoInteger(e);
                     77:     if (!memberQ(evList,ob)) {
                     78:       list = newList(&ob);
                     79:       evList = vJoin(evList,list);
                     80:     }
                     81:   }
                     82:   /*printf("evList = "); printObjectList(evList);*/
                     83:   evSize = klength(evList);
                     84:   ev = (int *)sGC_malloc(sizeof(int)*(evSize+1));
                     85:   if (ev == (int *)NULL) errorPoly("No more memory.");
                     86:   for (i=0; i<evSize; i++) {
                     87:     ev[i] = KopInteger(car(evList));
                     88:     evList = cdr(evList);
                     89:   }
                     90:   /* sort ev */
                     91:   shell(ev,evSize);
                     92:
                     93:   /* get coefficients */
                     94:   evPoly = newMatrixOfPOLY(2,evSize);
                     95:   for (i=0; i<evSize; i++) {
                     96:     ans = ZERO;
                     97:     getMatrixOfPOLY(evPoly,0,i) = cxx(ev[i],0,0,CurrentRingp);
                     98:     ft = f;
                     99:     while (ft != POLYNULL) {
                    100:       if (vx) {
1.3       takayama  101:         if (ft->m->e[vi].x == ev[i]) {
                    102:           h = newCell(ft->coeffp,monomialCopy(ft->m));
                    103:           xset0(h,vi); /* touch monomial part, so you need to copy it above. */
                    104:           ans = ppAdd(ans,h);
                    105:         }
1.1       maekawa   106:       }else{
1.3       takayama  107:         if (ft->m->e[vi].D == ev[i]) {
                    108:           h = newCell(ft->coeffp,monomialCopy(ft->m));
                    109:           dset0(h,vi);
                    110:           ans = ppAdd(ans,h);
                    111:         }
1.1       maekawa   112:       }
                    113:       ft = ft->next;
                    114:     }
                    115:     getMatrixOfPOLY(evPoly,1,i) = ans;
                    116:   }
                    117:   return(evPoly);
                    118: }
1.3       takayama  119:
1.1       maekawa   120: struct object parts2(f,v)
1.3       takayama  121:      POLY f;
                    122:      POLY v;  /* v must be a single variable, e.g. x */
1.1       maekawa   123: {
                    124:   struct matrixOfPOLY *evPoly;
                    125:   int vi = 0;  /* index of v */
                    126:   int vx = 1;  /* x --> 1, D--> 0 */
                    127:   int n,evSize,i,k,e;
                    128:   int *ev;
                    129:   struct object *evList;
                    130:   struct object *list;
1.15    ! takayama  131:   struct object ob = OINIT;
1.1       maekawa   132:   POLY ans;
                    133:   POLY h;
                    134:   POLY ft;
1.15    ! takayama  135:   struct object ob1 = OINIT;
        !           136:   struct object ob2 = OINIT;
        !           137:   struct object rob = OINIT;
1.1       maekawa   138:
                    139:
                    140:   if (f ISZERO || v ISZERO) {
                    141:     evPoly = newMatrixOfPOLY(2,1);
                    142:     getMatrixOfPOLY(evPoly,0,0) = ZERO;
                    143:     getMatrixOfPOLY(evPoly,1,0) = ZERO;
                    144:     rob = newObjectArray(2);
                    145:     ob1 = newObjectArray(1);
                    146:     ob2 = newObjectArray(1);
                    147:     putoa(ob1,0,KpoInteger(0));
                    148:     putoa(ob2,0,KpoPOLY(POLYNULL));
                    149:     putoa(rob,0,ob1); putoa(rob,1,ob2);
                    150:     return(rob);
                    151:   }
                    152:   n = v->m->ringp->n;
                    153:   /* get the index of the variable v */
                    154:   for (i=0; i<n; i++) {
                    155:     if (v->m->e[i].x) {
                    156:       vx = 1; vi = i; break;
                    157:     }else if (v->m->e[i].D) {
                    158:       vx = 0; vi = i; break;
                    159:     }
                    160:   }
                    161:   ft = f;
                    162:   /* get the vector of exponents */
                    163:   evList = NULLLIST;
                    164:   while (ft != POLYNULL) {
                    165:     if (vx) {
                    166:       e = ft->m->e[vi].x;
                    167:     }else{
                    168:       e = ft->m->e[vi].D;
                    169:     }
                    170:     ft = ft->next;
                    171:     ob = KpoInteger(e);
                    172:     if (!memberQ(evList,ob)) {
                    173:       list = newList(&ob);
                    174:       evList = vJoin(evList,list);
                    175:     }
                    176:   }
                    177:   /*printf("evList = "); printObjectList(evList);*/
                    178:   evSize = klength(evList);
                    179:   ev = (int *)sGC_malloc(sizeof(int)*(evSize+1));
                    180:   if (ev == (int *)NULL) errorPoly("No more memory.");
                    181:   for (i=0; i<evSize; i++) {
                    182:     ev[i] = KopInteger(car(evList));
                    183:     evList = cdr(evList);
                    184:   }
                    185:   /* sort ev */
                    186:   shell(ev,evSize);
                    187:
                    188:   /* get coefficients */
                    189:   evPoly = newMatrixOfPOLY(2,evSize);
                    190:   for (i=0; i<evSize; i++) {
                    191:     ans = ZERO;
                    192:     /* getMatrixOfPOLY(evPoly,0,i) = cxx(ev[i],0,0,CurrentRingp); */
                    193:     getMatrixOfPOLY(evPoly,0,i) = POLYNULL;
                    194:     ft = f;
                    195:     while (ft != POLYNULL) {
                    196:       if (vx) {
1.3       takayama  197:         if (ft->m->e[vi].x == ev[i]) {
                    198:           h = newCell(ft->coeffp,monomialCopy(ft->m));
                    199:           xset0(h,vi); /* touch monomial part, so you need to copy it above. */
                    200:           ans = ppAdd(ans,h);
                    201:         }
1.1       maekawa   202:       }else{
1.3       takayama  203:         if (ft->m->e[vi].D == ev[i]) {
                    204:           h = newCell(ft->coeffp,monomialCopy(ft->m));
                    205:           dset0(h,vi);
                    206:           ans = ppAdd(ans,h);
                    207:         }
1.1       maekawa   208:       }
                    209:       ft = ft->next;
                    210:     }
                    211:     getMatrixOfPOLY(evPoly,1,i) = ans;
                    212:   }
                    213:   rob = newObjectArray(2);
                    214:   ob1 = newObjectArray(evSize);
                    215:   ob2 = newObjectArray(evSize);
                    216:   for (i=0; i<evSize; i++) {
                    217:     putoa(ob2,i,KpoPOLY(getMatrixOfPOLY(evPoly,1,i)));
                    218:     putoa(ob1,i,KpoInteger(ev[i]));
                    219:   }
                    220:   putoa(rob,0,ob1); putoa(rob,1,ob2);
                    221:   return(rob);
                    222: }
1.3       takayama  223:
1.1       maekawa   224: int pDegreeWrtV(f,v)
1.3       takayama  225:      POLY f;
                    226:      POLY v;
1.1       maekawa   227: {
                    228:   int vx = 1;
                    229:   int vi = 0;
                    230:   int i,n;
                    231:   int ans;
                    232:   if (f ISZERO || v ISZERO) return(0);
                    233:   n = f->m->ringp->n;
                    234:   for (i=0; i<n; i++) {
                    235:     if (v->m->e[i].x) {
                    236:       vx = 1; vi = i;
                    237:       break;
                    238:     }else if (v->m->e[i].D) {
                    239:       vx = 0; vi = i;
                    240:       break;
                    241:     }
                    242:   }
                    243:   if (vx) {
                    244:     ans = f->m->e[vi].x;
                    245:   }else{
                    246:     ans = f->m->e[vi].D;
                    247:   }
                    248:   f = f->next;
                    249:   while (f != POLYNULL) {
                    250:     if (vx) {
                    251:       if (f->m->e[vi].x > ans) ans = f->m->e[vi].x;
                    252:     }else{
                    253:       if (f->m->e[vi].D > ans) ans = f->m->e[vi].D;
                    254:     }
                    255:     f = f->next;
                    256:   }
                    257:   return(ans);
                    258: }
                    259:
                    260: int containVectorVariable(POLY f)
                    261: {
                    262:   MONOMIAL tf;
                    263:   static int nn,mm,ll,cc,n,m,l,c;
                    264:   static struct ring *cr = (struct ring *)NULL;
                    265:   int i;
                    266:
                    267:   if (f ISZERO) return(0);
                    268:   tf = f->m;
                    269:   if (tf->ringp != cr) {
                    270:     n = tf->ringp->n;
                    271:     m = tf->ringp->m;
                    272:     l = tf->ringp->l;
                    273:     c = tf->ringp->c;
                    274:     nn = tf->ringp->nn;
                    275:     mm = tf->ringp->mm;
                    276:     ll = tf->ringp->ll;
                    277:     cc = tf->ringp->cc;
                    278:     cr = tf->ringp;
                    279:   }
                    280:
                    281:   while (f != POLYNULL) {
                    282:     tf = f->m;
                    283:     for (i=cc; i<c; i++) {
                    284:       if ( tf->e[i].x ) return(1);
                    285:       if ( tf->e[i].D ) return(1);
                    286:     }
                    287:     for (i=ll; i<l; i++) {
                    288:       if (tf->e[i].x) return(1);
                    289:       if (tf->e[i].D) return(1);
                    290:     }
                    291:     for (i=mm; i<m; i++) {
                    292:       if (tf->e[i].x) return(1);
                    293:       if (tf->e[i].D) return(1);
                    294:     }
                    295:     for (i=nn; i<n; i++) {
                    296:       if (tf->e[i].x) return(1);
                    297:       if (tf->e[i].D) return(1);
                    298:     }
                    299:     f = f->next;
                    300:   }
                    301:   return(0);
                    302:
                    303: }
                    304:
                    305: POLY homogenize(f)
1.3       takayama  306:      POLY f;
                    307:      /* homogenize by using (*grade)(f) */
1.1       maekawa   308: {
                    309:   POLY t;
                    310:   int maxg;
                    311:   int flag,d;
1.13      takayama  312:   extern int Homogenize;
1.1       maekawa   313:
                    314:   if (f == ZERO) return(f);
1.13      takayama  315:   if (Homogenize == 3) { /* double homogenization Dx x = x Dx + h H */
                    316:     return dHomogenize(f);
                    317:   }
1.1       maekawa   318:   t = f; maxg = (*grade)(f); flag = 0;
                    319:   while (t != POLYNULL) {
                    320:     d = (*grade)(t);
                    321:     if (d != maxg) flag = 1;
                    322:     if (d > maxg) {
                    323:       maxg = d;
                    324:     }
                    325:     t = t->next;
                    326:   }
                    327:   if (flag == 0) return(f);
                    328:
                    329:   f = pmCopy(f); /* You can rewrite the monomial parts */
                    330:   t = f;
                    331:   while (t != POLYNULL) {
                    332:     d = (*grade)(t);
                    333:     if (d != maxg) {
                    334:       t->m->e[0].D += maxg-d; /* Multiply h^(maxg-d) */
                    335:     }
                    336:     t = t->next;
                    337:   }
                    338:   return(f);
                    339: }
                    340:
                    341: int isHomogenized(f)
1.3       takayama  342:      POLY f;
1.1       maekawa   343: {
                    344:   POLY t;
                    345:   extern int Homogenize_vec;
                    346:   int maxg;
                    347:   if (!Homogenize_vec) return(isHomogenized_vec(f));
                    348:   if (f == ZERO) return(1);
1.4       takayama  349:   if (f->m->ringp->weightedHomogenization) {
                    350:        return 1; /* BUG: do not chech in case of one-zero homogenization */
                    351:   }
1.1       maekawa   352:   maxg = (*grade)(f);
                    353:   t = f;
                    354:   while (t != POLYNULL) {
                    355:     if (maxg != (*grade)(t)) return(0);
                    356:     t = t->next;
                    357:   }
                    358:   return(1);
                    359: }
                    360:
                    361: int isHomogenized_vec(f)
1.3       takayama  362:      POLY f;
1.1       maekawa   363: {
1.3       takayama  364:   /* This is not efficient version. *grade should be grade_module1v(). */
1.1       maekawa   365:   POLY t;
                    366:   int ggg;
                    367:   if (f == ZERO) return(1);
1.4       takayama  368:   if (f->m->ringp->weightedHomogenization) {
                    369:        return 1; /* BUG: do not chech in case of one-zero homogenization */
                    370:   }
1.1       maekawa   371:   while (f != POLYNULL) {
                    372:     t = f;
                    373:     ggg = (*grade)(f);
                    374:     while (t != POLYNULL) {
                    375:       if ((*isSameComponent)(f,t)) {
1.3       takayama  376:         if (ggg != (*grade)(t)) return(0);
1.1       maekawa   377:       }
                    378:       t = t->next;
                    379:     }
                    380:     f = f->next;
                    381:   }
                    382:   return(1);
                    383: }
                    384:
1.13      takayama  385: static POLY dHomogenize(f)
                    386: POLY f;
                    387: {
                    388:   POLY t;
                    389:   int maxg, maxdg;
                    390:   int flag,d,dd,neg;
                    391:
                    392:   if (f == ZERO) return(f);
1.14      takayama  393:
                    394:   t = f;
                    395:   maxg = (*grade)(f);
                    396:   while (t != POLYNULL) {
                    397:        dd = (*grade)(t);
                    398:        if (maxg < dd) maxg = dd;
                    399:        t = t->next;
                    400:   }
                    401:   /* fprintf(stderr,"maxg=%d\n",maxg); */
                    402:
                    403:   t = f;
                    404:   maxdg = dDegree(f);
                    405:   while (t != POLYNULL) {
                    406:        dd = dDegree(t);
                    407:        if (maxdg < dd) maxdg = dd;
                    408:        t = t->next;
                    409:   }
                    410:   /* fprintf(stderr,"maxdg=%d\n",maxdg); */
                    411:
                    412:   t = f;
                    413:   flag = 0;
1.13      takayama  414:   while (t != POLYNULL) {
                    415:     d = (*grade)(t);
                    416:     if (d != maxg) flag = 1;
                    417:     if (d > maxg) {
                    418:       maxg = d;
                    419:     }
                    420:     d = dDegree(f);
                    421:     if (d > maxdg) {
                    422:       maxdg = d;
                    423:     }
                    424:     t = t->next;
                    425:   }
                    426:   if (flag == 0) return(f);
                    427:
                    428:   t = f; neg = 0;
                    429:   while (t != POLYNULL) {
                    430:     d = (*grade)(t);
                    431:     dd = dDegree(t);
                    432:     if (maxg-d-(maxdg-dd) < neg) {
                    433:       neg = maxg-d-(maxdg-dd);
                    434:     }
                    435:     t = t->next;
                    436:   }
                    437:   neg = -neg;
                    438:
                    439:   f = pmCopy(f); /* You can rewrite the monomial parts */
                    440:   t = f;
                    441:   while (t != POLYNULL) {
                    442:     d = (*grade)(t);
                    443:     dd = dDegree(t);
                    444:     t->m->e[0].D += maxdg-dd; /* h */
                    445:     t->m->e[0].x += maxg-d-(maxdg-dd)+neg; /* Multiply H */
                    446:     /* example Dx^2+Dx+x */
                    447:     t = t->next;
                    448:   }
                    449:   return(f);
                    450: }
1.1       maekawa   451:
                    452: static int degreeOfPrincipalPart(f)
1.3       takayama  453:      POLY f;
1.1       maekawa   454: {
                    455:   int n,i,dd;
                    456:   if (f ISZERO) return(0);
                    457:   n = f->m->ringp->n; dd = 0;
                    458:   /* D[0] is homogenization var */
                    459:   for (i=1; i<n; i++) {
1.13      takayama  460:     dd += f->m->e[i].D;
                    461:   }
                    462:   return(dd);
                    463: }
                    464:
                    465: static int dDegree(f)
                    466:      POLY f;
                    467: {
                    468:   int nn,i,dd,m;
                    469:   if (f ISZERO) return(0);
                    470:   nn = f->m->ringp->nn; dd = 0;
                    471:   m = f->m->ringp->m;
                    472:   for (i=m; i<nn; i++) {
1.1       maekawa   473:     dd += f->m->e[i].D;
                    474:   }
                    475:   return(dd);
                    476: }
                    477:
                    478: POLY POLYToPrincipalPart(f)
1.3       takayama  479:      POLY f;
1.1       maekawa   480: {
                    481:   POLY node;
                    482:   struct listPoly nod;
                    483:   POLY h;
                    484:   POLY g;
                    485:   int maxd = -20000; /* very big negative number */
                    486:   int dd;
                    487:   node = &nod; node->next = POLYNULL; h = node;
                    488:
                    489:   g = pCopy(f); /* shallow copy */
                    490:   while (!(f ISZERO)) {
                    491:     dd = degreeOfPrincipalPart(f);
                    492:     if (dd > maxd) maxd = dd;
                    493:     f = f->next;
                    494:   }
                    495:   while (!(g ISZERO)) {
                    496:     dd = degreeOfPrincipalPart(g);
                    497:     if (dd == maxd) {
                    498:       h->next = g;
                    499:       h = h->next;
                    500:     }
                    501:     g = g->next;
                    502:   }
                    503:   h->next = POLYNULL;
                    504:   return(node->next);
                    505: }
                    506:
                    507: static int degreeOfInitW(f,w)
1.3       takayama  508:      POLY f;
                    509:      int w[];
1.1       maekawa   510: {
                    511:   int n,i,dd;
                    512:   if (f ISZERO) {
                    513:     errorPoly("degreeOfInitW(0,w) ");
                    514:   }
                    515:   n = f->m->ringp->n; dd = 0;
                    516:   for (i=0; i<n; i++) {
                    517:     dd += (f->m->e[i].D)*w[n+i];
                    518:     dd += (f->m->e[i].x)*w[i];
                    519:   }
                    520:   return(dd);
                    521: }
                    522:
                    523: POLY POLYToInitW(f,w)
1.3       takayama  524:      POLY f;
                    525:      int w[]; /* weight vector */
1.1       maekawa   526: {
                    527:   POLY h;
                    528:   POLY g;
                    529:   int maxd;
                    530:   int dd;
1.11      takayama  531:   h = POLYNULL;
1.1       maekawa   532:
1.11      takayama  533:   /*printf("1:%s\n",POLYToString(f,'*',1));*/
1.1       maekawa   534:   if (f ISZERO) return(f);
                    535:   maxd = degreeOfInitW(f,w);
1.11      takayama  536:   g = f;
1.1       maekawa   537:   while (!(f ISZERO)) {
                    538:     dd = degreeOfInitW(f,w);
                    539:     if (dd > maxd) maxd = dd;
                    540:     f = f->next;
                    541:   }
                    542:   while (!(g ISZERO)) {
                    543:     dd = degreeOfInitW(g,w);
                    544:     if (dd == maxd) {
1.11      takayama  545:       h = ppAdd(h,newCell(g->coeffp,g->m)); /* it might be slow. */
1.1       maekawa   546:     }
                    547:     g = g->next;
                    548:   }
1.11      takayama  549:   /*printf("2:%s\n",POLYToString(h,'*',1));*/
                    550:   return(h);
1.10      takayama  551: }
                    552:
                    553: static int degreeOfInitWS(f,w,s)
                    554:      POLY f;
                    555:      int w[];
                    556:         int s[];
                    557: {
                    558:   int n,i,dd;
                    559:   if (f ISZERO) {
                    560:     errorPoly("degreeOfInitWS(0,w) ");
                    561:   }
1.11      takayama  562:   if (s == (int *) NULL) return degreeOfInitW(f,w);
1.10      takayama  563:   n = f->m->ringp->n; dd = 0;
                    564:   for (i=0; i<n-1; i++) {
                    565:     dd += (f->m->e[i].D)*w[n+i];
                    566:     dd += (f->m->e[i].x)*w[i];
                    567:   }
                    568:   dd += s[(f->m->e[n-1].x)];
                    569:   return(dd);
                    570: }
                    571:
                    572: POLY POLYToInitWS(f,w,s)
                    573:      POLY f;
                    574:      int w[]; /* weight vector */
                    575:         int s[]; /* shift vector */
                    576: {
                    577:   POLY h;
                    578:   POLY g;
                    579:   int maxd;
                    580:   int dd;
1.11      takayama  581:   h = POLYNULL;
1.10      takayama  582:
1.11      takayama  583:   /*printf("1s:%s\n",POLYToString(f,'*',1));*/
1.10      takayama  584:   if (f ISZERO) return(f);
                    585:   maxd = degreeOfInitWS(f,w,s);
1.11      takayama  586:   g = f;
1.10      takayama  587:   while (!(f ISZERO)) {
                    588:     dd = degreeOfInitWS(f,w,s);
                    589:     if (dd > maxd) maxd = dd;
                    590:     f = f->next;
                    591:   }
                    592:   while (!(g ISZERO)) {
                    593:     dd = degreeOfInitWS(g,w,s);
                    594:     if (dd == maxd) {
1.11      takayama  595:       h = ppAdd(h,newCell(g->coeffp,g->m)); /* it might be slow. */
1.10      takayama  596:     }
                    597:     g = g->next;
                    598:   }
1.11      takayama  599:   /*printf("2s:%s\n",POLYToString(h,'*',1));*/
                    600:   return(h);
1.10      takayama  601: }
                    602:
                    603: int ordWsAll(f,w,s)
                    604:      POLY f;
                    605:      int w[]; /* weight vector */
                    606:         int s[]; /* shift vector */
                    607: {
                    608:   int maxd;
                    609:   int dd;
                    610:
                    611:   if (f ISZERO)  errorPoly("ordWsAll(0,w,s) ");
                    612:   maxd = degreeOfInitWS(f,w,s);
                    613:   while (!(f ISZERO)) {
                    614:     dd = degreeOfInitWS(f,w,s);
                    615:     if (dd > maxd) maxd = dd;
                    616:     f = f->next;
                    617:   }
                    618:   return maxd;
1.1       maekawa   619: }
                    620:
                    621:
                    622: /*
                    623: 1.The substitution  "ringp->multiplication = ...." is allowed only in
                    624:   KsetUpRing(), so the check in KswitchFunction is not necessary.
                    625: 2.mmLarger != matrix and AvoidTheSameRing==1, then error
                    626: 3.If Schreyer = 1, then the system always generates a new ring.
                    627: 4.The execution of set_order_by_matrix is not allowed when Avoid... == 1.
                    628: 5.When mmLarger == tower  (in tower.sm1, tower-sugar.sm1), we do
                    629:   as follows with our own risk.
                    630: [(AvoidTheSameRing)] pushEnv [ [(AvoidTheSameRing) 0] system_variable (mmLarger) (tower) switch_function ] pop popEnv
                    631: */
                    632: int isTheSameRing(struct ring *rstack[],int rp, struct ring *newRingp)
                    633: {
                    634:   struct ring *rrr;
                    635:   int i,j,k;
                    636:   int a=0;
                    637:   for (k=0; k<rp; k++) {
                    638:     rrr = rstack[k];
                    639:     if (rrr->p != newRingp->p) { a=1; goto bbb ; }
                    640:     if (rrr->n != newRingp->n) { a=2; goto bbb ; }
                    641:     if (rrr->nn != newRingp->nn) { a=3; goto bbb ; }
                    642:     if (rrr->m != newRingp->m) { a=4; goto bbb ; }
                    643:     if (rrr->mm != newRingp->mm) { a=5; goto bbb ; }
                    644:     if (rrr->l != newRingp->l) { a=6; goto bbb ; }
                    645:     if (rrr->ll != newRingp->ll) { a=7; goto bbb ; }
                    646:     if (rrr->c != newRingp->c) { a=8; goto bbb ; }
                    647:     if (rrr->cc != newRingp->cc) { a=9; goto bbb ; }
                    648:     for (i=0; i<rrr->n; i++) {
                    649:       if (strcmp(rrr->x[i],newRingp->x[i])!=0) { a=10; goto bbb ; }
                    650:       if (strcmp(rrr->D[i],newRingp->D[i])!=0) { a=11; goto bbb ; }
                    651:     }
                    652:     if (rrr->orderMatrixSize != newRingp->orderMatrixSize) { a=12; goto bbb ; }
                    653:     for (i=0; i<rrr->orderMatrixSize; i++) {
                    654:       for (j=0; j<2*(rrr->n); j++) {
1.3       takayama  655:         if (rrr->order[i*2*(rrr->n)+j] != newRingp->order[i*2*(rrr->n)+j])
                    656:           { a=13; goto bbb ; }
1.1       maekawa   657:       }
                    658:     }
                    659:     if (rrr->next != newRingp->next) { a=14; goto bbb ; }
                    660:     if (rrr->multiplication != newRingp->multiplication) { a=15; goto bbb ; }
                    661:     /* if (rrr->schreyer != newRingp->schreyer) { a=16; goto bbb ; }*/
                    662:     if (newRingp->schreyer == 1) { a=16; goto bbb; }
1.4       takayama  663:     if (rrr->weightedHomogenization != newRingp->weightedHomogenization) { a=16; goto bbb; }
1.7       takayama  664:     if (rrr->degreeShiftSize != newRingp->degreeShiftSize) {
                    665:       a = 17; goto bbb;
                    666:     }
                    667:     if (rrr->degreeShiftN != newRingp->degreeShiftN) {
                    668:       a = 17; goto bbb;
                    669:     }
                    670:     for (i=0; i < rrr->degreeShiftSize; i++) {
                    671:       for (j=0; j< rrr->degreeShiftN; j++) {
                    672:         if (rrr->degreeShift[i*(rrr->degreeShiftN)+j] !=
                    673:             newRingp->degreeShift[i*(rrr->degreeShiftN)+j]) {
                    674:           a = 17; goto bbb;
                    675:         }
                    676:       }
                    677:     }
                    678:
1.1       maekawa   679:     /* The following fields are ignored.
                    680:        void *gbListTower;
                    681:        int *outputOrder;
                    682:        char *name;
                    683:     */
                    684:     /* All tests are passed. */
                    685:     return(k);
                    686:   bbb: ;
                    687:     /* for debugging. */
                    688:     /* fprintf(stderr," reason=%d, ",a); */
                    689:   }
                    690:   return(-1);
                    691: }
1.6       takayama  692:
                    693: /* s->1 */
                    694: POLY goDeHomogenizeS(POLY f) {
1.9       takayama  695:   POLY lRule[1];
                    696:   POLY rRule[1];
                    697:   struct ring *rp;
                    698:   POLY ans;
                    699:   /* printf("1:[%s]\n",POLYToString(f,'*',1)); */
                    700:   if (f == POLYNULL) return f;
                    701:   rp = f->m->ringp;
                    702:   if (rp->next == NULL) {
                    703:        lRule[0] = cxx(1,0,1,rp);
                    704:        rRule[0] = cxx(1,0,0,rp);
                    705:        ans=replace(f,lRule,rRule,1);
                    706:   }else{
                    707:        struct coeff *cp;
                    708:        POLY t;
                    709:        POLY nc;
                    710:     ans = POLYNULL;
                    711:        while (f != POLYNULL) {
                    712:          cp = f->coeffp;
                    713:          if (cp->tag == POLY_COEFF) {
                    714:                t = goDeHomogenizeS((cp->val).f);
1.11      takayama  715:                nc = newCell(polyToCoeff(t,f->m->ringp),monomialCopy(f->m));
1.9       takayama  716:                ans = ppAddv(ans,nc);
                    717:                f = f->next;
                    718:          }else{
                    719:                ans = f; break;
                    720:          }
                    721:        }
                    722:   }
                    723:   /* printf("2:[%s]\n",POLYToString(ans,'*',1)); */
                    724:   return ans;
                    725: }
                    726:
                    727: POLY goDeHomogenizeS_buggy(POLY f) {
1.6       takayama  728:   POLY node;
                    729:   POLY lastf;
                    730:   struct listPoly nod;
                    731:   POLY h;
                    732:   POLY tf;
                    733:   int gt,first;
                    734:
1.9       takayama  735:   printf("1:[%s]\n",POLYToString(f,'*',1));
1.6       takayama  736:   if (f == POLYNULL) return(POLYNULL);
                    737:   node = &nod;
                    738:   node->next = POLYNULL;
                    739:   lastf = POLYNULL;
                    740:   first = 1;
                    741:   while (f != POLYNULL) {
                    742:     tf = newCell(f->coeffp,monomialCopy(f->m));
                    743:     tf->m->e[0].x = 0;  /* H, s variable in the G-O paper. */
                    744:     if (first) {
                    745:       node->next = tf;
                    746:       lastf = tf;
                    747:       first = 0;
                    748:     }else{
                    749:       gt = (*mmLarger)(lastf,tf);
                    750:       if (gt == 1) {
                    751:         lastf->next = tf;
                    752:         lastf = tf;
                    753:       }else{
                    754:         h = node->next;
                    755:         h = ppAddv(h,tf);
                    756:         node->next = h;
                    757:         lastf = h;
                    758:         while (lastf->next != POLYNULL) {
                    759:           lastf = lastf->next;
                    760:         }
                    761:       }
                    762:     }
                    763:     f = f->next;
                    764:   }
1.9       takayama  765:   printf("2:[%s]\n",POLYToString(node->next,'*',1));
1.6       takayama  766:   return (node->next);
                    767: }
                    768:
1.5       takayama  769: /* Granger-Oaku's homogenization for the ecart tangent cone.
                    770:    Note: 2003.07.10.
                    771:    ds[] is the degree shift.
                    772:    ei ( element index ). If it is < 0, then e[n-1]->x will be used,
                    773:                          else ei is used.
1.6       takayama  774:    if onlyS is set to 1, then input is assumed to be (u,v)-h-homogeneous.
1.5       takayama  775: */
1.6       takayama  776: POLY goHomogenize(POLY f,int u[],int v[],int ds[],int dssize,int ei,int onlyS)
1.5       takayama  777: {
                    778:   POLY node;
                    779:   POLY lastf;
                    780:   struct listPoly nod;
                    781:   POLY h;
                    782:   POLY tf;
                    783:   int gt,first,m,mp,t,tp,dsIdx,message;
1.7       takayama  784:   struct ring *rp;
1.5       takayama  785:
                    786:   message = 1;
                    787:   if (f == POLYNULL) return(POLYNULL);
1.7       takayama  788:   rp = f->m->ringp;
1.12      takayama  789:   /*
1.7       takayama  790:   if ((rp->degreeShiftSize == 0) && (dssize > 0)) {
                    791:        warningPoly("You are trying to homogenize a polynomial with degree shift. However, the polynomial belongs to the ring without degreeShift option. It may cause a trouble in comparison in free module.\n");
                    792:   }
1.12      takayama  793:   */
1.5       takayama  794:   node = &nod;
                    795:   node->next = POLYNULL;
                    796:   lastf = POLYNULL;
                    797:   first = 1;
                    798:   while (f != POLYNULL) {
                    799:     if (first) {
                    800:       t = m = dGrade1(f);
                    801:       tp = mp = uvGrade1(f,u,v,ds,dssize,ei);
                    802:     }else{
                    803:       t =  dGrade1(f);
                    804:       tp = uvGrade1(f,u,v,ds,dssize,ei);
                    805:       if (t > m) m = t;
                    806:       if (tp < mp) mp = tp;
                    807:     }
                    808:
                    809:     tf = newCell(f->coeffp,monomialCopy(f->m));
                    810:        /* Automatic dehomogenize. Not +=  */
                    811:        if (message && ((tf->m->e[0].D != 0) || (tf->m->e[0].x != 0))) {
                    812:       /*go-debug fprintf(stderr,"Automatic dehomogenize and homogenize.\n"); */
                    813:          message = 0;
                    814:        }
1.6       takayama  815:        if (!onlyS) {
                    816:          tf->m->e[0].D = -t;  /* h */
                    817:        }
1.5       takayama  818:     tf->m->e[0].x = tp;  /* H, s variable in the G-O paper. */
                    819:        /*go-debug printf("t(h)=%d, tp(uv+ds)=%d\n",t,tp); */
                    820:     if (first) {
                    821:       node->next = tf;
                    822:       lastf = tf;
                    823:       first = 0;
                    824:     }else{
                    825:       gt = (*mmLarger)(lastf,tf);
                    826:       if (gt == 1) {
                    827:         lastf->next = tf;
                    828:         lastf = tf;
                    829:       }else{
                    830:         /*go-debug printf("?\n"); */
                    831:         h = node->next;
                    832:         h = ppAddv(h,tf);
                    833:         node->next = h;
                    834:         lastf = h;
                    835:         while (lastf->next != POLYNULL) {
                    836:           lastf = lastf->next;
                    837:         }
                    838:       }
                    839:        }
                    840:        f = f->next;
                    841:   }
                    842:   h = node->next;
                    843:   /*go-debug printf("m=%d, mp=%d\n",m,mp); */
                    844:   while (h != POLYNULL) {
1.12      takayama  845:     /*go-debug printf("Old: h=%d, s=%d\n",h->m->e[0].D,h->m->e[0].x);  */
1.6       takayama  846:     if (!onlyS) h->m->e[0].D += m;   /* h */
1.5       takayama  847:     h->m->e[0].x += -mp; /* H, s*/
                    848:     /*go-debug printf("New: h=%d, s=%d\n",h->m->e[0].D,h->m->e[0].x); */
                    849:     h = h->next;
                    850:   }
                    851:   return (node->next);
                    852: }
                    853:
                    854: /* u[] = -1, v[] = 1 */
1.6       takayama  855: POLY goHomogenize11(POLY f,int ds[],int dssize,int ei,int onlyS)
1.5       takayama  856: {
                    857:   int r;
                    858:   int i,t,n,m,nn;
                    859:   MONOMIAL tf;
                    860:   static int *u;
                    861:   static int *v;
                    862:   static struct ring *cr = (struct ring *)NULL;
                    863:
                    864:   if (f == POLYNULL) return POLYNULL;
                    865:
                    866:   tf = f->m;
                    867:   if (tf->ringp != cr) {
                    868:     n = tf->ringp->n;
                    869:     m = tf->ringp->m;
                    870:     nn = tf->ringp->nn;
                    871:     cr = tf->ringp;
                    872:        u = (int *)sGC_malloc(sizeof(int)*n);
                    873:        v = (int *)sGC_malloc(sizeof(int)*n);
                    874:        for (i=0; i<n; i++) u[i]=v[i]=0;
                    875:        for (i=m; i<nn; i++) {
                    876:          u[i] = -1; v[i] = 1;
                    877:        }
                    878:   }
1.6       takayama  879:   return(goHomogenize(f,u,v,ds,dssize,ei,onlyS));
1.5       takayama  880: }
                    881:
1.6       takayama  882: POLY goHomogenize_dsIdx(POLY f,int u[],int v[],int dsIdx,int ei,int onlyS)
1.5       takayama  883: {
                    884:   if (f == POLYNULL) return POLYNULL;
                    885: }
1.6       takayama  886: POLY goHomogenize11_dsIdx(POLY f,int ds[],int dsIdx,int ei,int onlyS)
1.5       takayama  887: {
                    888:   if (f == POLYNULL) return POLYNULL;
1.8       takayama  889: }
                    890:
                    891: /* cf. KsetUpRing() in kanExport0.c */
                    892: struct ring *newRingOverFp(struct ring *rp,int p) {
                    893:   struct ring *newRingp;
                    894:   char *ringName = NULL;
                    895:   char pstr[64];
                    896:   sprintf(pstr,"_%d",p);
                    897:   ringName = (char *)sGC_malloc(128);
                    898:   newRingp = (struct ring *)sGC_malloc(sizeof(struct ring));
                    899:   if (newRingp == NULL) errorPoly("No more memory.\n");
                    900:   strcpy(ringName,rp->name);
                    901:   strcat(ringName,pstr);
                    902:   *newRingp = *rp;
                    903:   newRingp->p = p;
                    904:   newRingp->name = ringName;
                    905:   return newRingp;
                    906: }
                    907:
                    908: /*
                    909:    P = 3001;
                    910:    L = [ ];
                    911:    while (P<10000) {
                    912:      L=cons(P,L);
                    913:      P = pari(nextprime,P+1);
                    914:        }
                    915:    print(L);
                    916: */
                    917: #define N799  799
                    918: static int nextPrime(void) {
                    919:   static int pt = 0;
                    920:   static int tb[N799] =
                    921: {3001,3011,3019,3023,3037,3041,3049,3061,3067,3079,3083,3089,3109,3119,3121,3137,3163,3167,3169,3181,3187,3191,3203,3209,3217,3221,3229,3251,3253,3257,3259,3271,3299,3301,3307,3313,3319,3323,3329,3331,3343,3347,3359,3361,3371,3373,3389,3391,3407,3413,3433,3449,3457,3461,3463,3467,3469,3491,3499,3511,3517,3527,3529,3533,3539,3541,3547,3557,3559,3571,3581,3583,3593,3607,3613,3617,3623,3631,3637,3643,3659,3671,3673,3677,3691,3697,3701,3709,3719,3727,3733,3739,3761,3767,3769,3779,3793,3797,3803,3821,3823,3833,3847,3851,3853,3863,3877,3881,3889,3907,3911,3917,3919,3923,3929,3931,3943,3947,3967,3989,
                    922:   4001,4003,4007,4013,4019,4021,4027,4049,4051,4057,4073,4079,4091,4093,4099,4111,4127,4129,4133,4139,4153,4157,4159,4177,4201,4211,4217,4219,4229,4231,4241,4243,4253,4259,4261,4271,4273,4283,4289,4297,4327,4337,4339,4349,4357,4363,4373,4391,4397,4409,4421,4423,4441,4447,4451,4457,4463,4481,4483,4493,4507,4513,4517,4519,4523,4547,4549,4561,4567,4583,4591,4597,4603,4621,4637,4639,4643,4649,4651,4657,4663,4673,4679,4691,4703,4721,4723,4729,4733,4751,4759,4783,4787,4789,4793,4799,4801,4813,4817,4831,4861,4871,4877,4889,4903,4909,4919,4931,4933,4937,4943,4951,4957,4967,4969,4973,4987,4993,4999,
                    923:   5003,5009,5011,5021,5023,5039,5051,5059,5077,5081,5087,5099,5101,5107,5113,5119,5147,5153,5167,5171,5179,5189,5197,5209,5227,5231,5233,5237,5261,5273,5279,5281,5297,5303,5309,5323,5333,5347,5351,5381,5387,5393,5399,5407,5413,5417,5419,5431,5437,5441,5443,5449,5471,5477,5479,5483,5501,5503,5507,5519,5521,5527,5531,5557,5563,5569,5573,5581,5591,5623,5639,5641,5647,5651,5653,5657,5659,5669,5683,5689,5693,5701,5711,5717,5737,5741,5743,5749,5779,5783,5791,5801,5807,5813,5821,5827,5839,5843,5849,5851,5857,5861,5867,5869,5879,5881,5897,5903,5923,5927,5939,5953,5981,5987,
                    924:   6007,6011,6029,6037,6043,6047,6053,6067,6073,6079,6089,6091,6101,6113,6121,6131,6133,6143,6151,6163,6173,6197,6199,6203,6211,6217,6221,6229,6247,6257,6263,6269,6271,6277,6287,6299,6301,6311,6317,6323,6329,6337,6343,6353,6359,6361,6367,6373,6379,6389,6397,6421,6427,6449,6451,6469,6473,6481,6491,6521,6529,6547,6551,6553,6563,6569,6571,6577,6581,6599,6607,6619,6637,6653,6659,6661,6673,6679,6689,6691,6701,6703,6709,6719,6733,6737,6761,6763,6779,6781,6791,6793,6803,6823,6827,6829,6833,6841,6857,6863,6869,6871,6883,6899,6907,6911,6917,6947,6949,6959,6961,6967,6971,6977,6983,6991,6997,
                    925:   7001,7013,7019,7027,7039,7043,7057,7069,7079,7103,7109,7121,7127,7129,7151,7159,7177,7187,7193,7207,7211,7213,7219,7229,7237,7243,7247,7253,7283,7297,7307,7309,7321,7331,7333,7349,7351,7369,7393,7411,7417,7433,7451,7457,7459,7477,7481,7487,7489,7499,7507,7517,7523,7529,7537,7541,7547,7549,7559,7561,7573,7577,7583,7589,7591,7603,7607,7621,7639,7643,7649,7669,7673,7681,7687,7691,7699,7703,7717,7723,7727,7741,7753,7757,7759,7789,7793,7817,7823,7829,7841,7853,7867,7873,7877,7879,7883,7901,7907,7919,7927,7933,7937,7949,7951,7963,7993,
                    926: 8009,8011,8017,8039,8053,8059,8069,8081,8087,8089,8093,8101,8111,8117,8123,8147,8161,8167,8171,8179,8191,8209,8219,8221,8231,8233,8237,8243,8263,8269,8273,8287,8291,8293,8297,8311,8317,8329,8353,8363,8369,8377,8387,8389,8419,8423,8429,8431,8443,8447,8461,8467,8501,8513,8521,8527,8537,8539,8543,8563,8573,8581,8597,8599,8609,8623,8627,8629,8641,8647,8663,8669,8677,8681,8689,8693,8699,8707,8713,8719,8731,8737,8741,8747,8753,8761,8779,8783,8803,8807,8819,8821,8831,8837,8839,8849,8861,8863,8867,8887,8893,8923,8929,8933,8941,8951,8963,8969,8971,8999,
                    927:   9001,9007,9011,9013,9029,9041,9043,9049,9059,9067,9091,9103,9109,9127,9133,9137,9151,9157,9161,9173,9181,9187,9199,9203,9209,9221,9227,9239,9241,9257,9277,9281,9283,9293,9311,9319,9323,9337,9341,9343,9349,9371,9377,9391,9397,9403,9413,9419,9421,9431,9433,9437,9439,9461,9463,9467,9473,9479,9491,9497,9511,9521,9533,9539,9547,9551,9587,9601,9613,9619,9623,9629,9631,9643,9649,9661,9677,9679,9689,9697,9719,9721,9733,9739,9743,9749,9767,9769,9781,9787,9791,9803,9811,9817,9829,9833,9839,9851,9857,9859,9871,9883,9887,9901,9907,9923,9929,9931,9941,9949,9967,9973};
                    928:
                    929:   if (pt <N799) {
                    930:        return tb[pt++];
                    931:   }else{
                    932:        pt = 0;
                    933:        return tb[pt++];
                    934:   }
                    935: }
                    936:
                    937: int getPrime(int p) {
                    938:   int i;
                    939:   if (p <= 2) return nextPrime();
                    940:   for (i=2; i<p; i++) {
                    941:        if (p % i == 0) {
                    942:          return nextPrime();
                    943:        }
                    944:   }
                    945:   return p;
1.5       takayama  946: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>