[BACK]Return to dp-supp.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2018 / builtin

Annotation of OpenXM_contrib2/asir2018/builtin/dp-supp.c, Revision 1.10

1.1       noro        1: /*
                      2:  * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
                      3:  * All rights reserved.
                      4:  *
                      5:  * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
                      6:  * non-exclusive and royalty-free license to use, copy, modify and
                      7:  * redistribute, solely for non-commercial and non-profit purposes, the
                      8:  * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
                      9:  * conditions of this Agreement. For the avoidance of doubt, you acquire
                     10:  * only a limited right to use the SOFTWARE hereunder, and FLL or any
                     11:  * third party developer retains all rights, including but not limited to
                     12:  * copyrights, in and to the SOFTWARE.
                     13:  *
                     14:  * (1) FLL does not grant you a license in any way for commercial
                     15:  * purposes. You may use the SOFTWARE only for non-commercial and
                     16:  * non-profit purposes only, such as academic, research and internal
                     17:  * business use.
                     18:  * (2) The SOFTWARE is protected by the Copyright Law of Japan and
                     19:  * international copyright treaties. If you make copies of the SOFTWARE,
                     20:  * with or without modification, as permitted hereunder, you shall affix
                     21:  * to all such copies of the SOFTWARE the above copyright notice.
                     22:  * (3) An explicit reference to this SOFTWARE and its copyright owner
                     23:  * shall be made on your publication or presentation in any form of the
                     24:  * results obtained by use of the SOFTWARE.
                     25:  * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
                     26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
                     27:  * for such modification or the source code of the modified part of the
                     28:  * SOFTWARE.
                     29:  *
                     30:  * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
                     31:  * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
                     32:  * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
                     33:  * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
                     34:  * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
                     35:  * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
                     36:  * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
                     37:  * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
                     38:  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
                     39:  * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
                     40:  * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
                     41:  * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
                     42:  * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
                     43:  * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
                     44:  * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
                     45:  * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
                     46:  * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
                     47:  *
1.10    ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2018/builtin/dp-supp.c,v 1.9 2019/11/12 07:47:45 noro Exp $
1.1       noro       49: */
                     50: #include "ca.h"
                     51: #include "base.h"
                     52: #include "inline.h"
                     53: #include "parse.h"
                     54: #include "ox.h"
                     55:
                     56: #define HMAG(p) (p_mag((P)BDY(p)->c))
                     57:
                     58: extern int (*cmpdl)();
                     59: extern double pz_t_e,pz_t_d,pz_t_d1,pz_t_c;
                     60: extern int dp_nelim,dp_fcoeffs;
                     61: extern int NoGCD;
                     62: extern int GenTrace;
                     63: extern NODE TraceList;
                     64:
                     65: int show_orderspec;
                     66:
                     67: void print_composite_order_spec(struct order_spec *spec);
                     68: void dpm_rest(DPM,DPM *);
                     69:
                     70: /*
                     71:  * content reduction
                     72:  *
                     73:  */
                     74:
                     75: static NODE RatDenomList;
                     76:
                     77: void init_denomlist()
                     78: {
                     79:   RatDenomList = 0;
                     80: }
                     81:
                     82: void add_denomlist(P f)
                     83: {
                     84:   NODE n;
                     85:
                     86:   if ( OID(f)==O_P ) {
                     87:     MKNODE(n,f,RatDenomList); RatDenomList = n;
                     88:   }
                     89: }
                     90:
                     91: LIST get_denomlist()
                     92: {
                     93:   LIST l;
                     94:
                     95:   MKLIST(l,RatDenomList); RatDenomList = 0;
                     96:   return l;
                     97: }
                     98:
                     99: void dp_ptozp(DP p,DP *rp)
                    100: {
                    101:   MP m,mr,mr0;
                    102:   int i,n;
                    103:   Q *w;
                    104:   Z dvr;
                    105:   P t;
                    106:
                    107:   if ( !p )
                    108:     *rp = 0;
                    109:   else {
                    110:     for ( m =BDY(p), n = 0; m; m = NEXT(m), n++ );
                    111:     w = (Q *)ALLOCA(n*sizeof(Q));
                    112:     for ( m =BDY(p), i = 0; i < n; m = NEXT(m), i++ )
                    113:       if ( NUM(m->c) )
                    114:         w[i] = (Q)m->c;
                    115:       else
                    116:         ptozp((P)m->c,1,&w[i],&t);
                    117:     sortbynm(w,n);
                    118:     qltozl(w,n,&dvr);
                    119:     for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                    120:       NEXTMP(mr0,mr); divsp(CO,(P)m->c,(P)dvr,(P *)&mr->c); mr->dl = m->dl;
                    121:     }
                    122:     NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
                    123:   }
                    124: }
                    125:
                    126: void dp_ptozp2(DP p0,DP p1,DP *hp,DP *rp)
                    127: {
                    128:   DP t,s,h,r;
                    129:   MP m,mr,mr0,m0;
                    130:
                    131:   addd(CO,p0,p1,&t); dp_ptozp(t,&s);
                    132:   if ( !p0 ) {
                    133:     h = 0; r = s;
                    134:   } else if ( !p1 ) {
                    135:     h = s; r = 0;
                    136:   } else {
                    137:     for ( mr0 = 0, m = BDY(s), m0 = BDY(p0); m0;
                    138:       m = NEXT(m), m0 = NEXT(m0) ) {
                    139:       NEXTMP(mr0,mr); mr->c = m->c; mr->dl = m->dl;
                    140:     }
                    141:     NEXT(mr) = 0; MKDP(p0->nv,mr0,h); MKDP(p0->nv,m,r);
                    142:   }
                    143:   if ( h )
                    144:     h->sugar = p0->sugar;
                    145:   if ( r )
                    146:     r->sugar = p1->sugar;
                    147:   *hp = h; *rp = r;
                    148: }
                    149:
1.3       noro      150: void dpm_ptozp(DPM p,Z *cont,DPM *rp)
1.1       noro      151: {
                    152:   DMM m,mr,mr0;
                    153:   int i,n;
                    154:   Q *w;
                    155:   Z dvr;
                    156:   P t;
                    157:
1.3       noro      158:   if ( !p ) {
                    159:     *rp = 0; *cont = ONE;
                    160:   } else {
1.1       noro      161:     for ( m =BDY(p), n = 0; m; m = NEXT(m), n++ );
                    162:     w = (Q *)ALLOCA(n*sizeof(Q));
                    163:     for ( m =BDY(p), i = 0; i < n; m = NEXT(m), i++ )
                    164:       if ( NUM(m->c) )
                    165:         w[i] = (Q)m->c;
                    166:       else
                    167:         ptozp((P)m->c,1,&w[i],&t);
                    168:     sortbynm(w,n);
                    169:     qltozl(w,n,&dvr);
                    170:     for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                    171:       NEXTDMM(mr0,mr); divsp(CO,(P)m->c,(P)dvr,(P *)&mr->c); mr->dl = m->dl; mr->pos = m->pos;
                    172:     }
                    173:     NEXT(mr) = 0; MKDPM(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
1.3       noro      174:     *cont = dvr;
1.1       noro      175:   }
                    176: }
                    177:
                    178: void dpm_ptozp2(DPM p0,DPM p1,DPM *hp,DPM *rp)
                    179: {
                    180:   DPM t,s,h,r;
                    181:   DMM m,mr,mr0,m0;
1.3       noro      182:   Z cont;
1.1       noro      183:
1.3       noro      184:   adddpm(CO,p0,p1,&t); dpm_ptozp(t,&cont,&s);
1.1       noro      185:   if ( !p0 ) {
                    186:     h = 0; r = s;
                    187:   } else if ( !p1 ) {
                    188:     h = s; r = 0;
                    189:   } else {
                    190:     for ( mr0 = 0, m = BDY(s), m0 = BDY(p0); m0;
                    191:       m = NEXT(m), m0 = NEXT(m0) ) {
                    192:       NEXTDMM(mr0,mr); mr->c = m->c; mr->dl = m->dl; mr->pos = m->pos;
                    193:     }
                    194:     NEXT(mr) = 0; MKDPM(p0->nv,mr0,h); MKDPM(p0->nv,m,r);
                    195:   }
                    196:   if ( h )
                    197:     h->sugar = p0->sugar;
                    198:   if ( r )
                    199:     r->sugar = p1->sugar;
                    200:   *hp = h; *rp = r;
                    201: }
                    202:
                    203:
                    204: void dp_ptozp3(DP p,Z *dvr,DP *rp)
                    205: {
                    206:   MP m,mr,mr0;
                    207:   int i,n;
                    208:   Q *w;
                    209:   P t;
                    210:
                    211:   if ( !p ) {
                    212:     *rp = 0; *dvr = 0;
                    213:   }else {
                    214:     for ( m =BDY(p), n = 0; m; m = NEXT(m), n++ );
                    215:     w = (Q *)ALLOCA(n*sizeof(Q));
                    216:     for ( m =BDY(p), i = 0; i < n; m = NEXT(m), i++ )
                    217:       if ( NUM(m->c) )
                    218:         w[i] = (Q)m->c;
                    219:       else
                    220:         ptozp((P)m->c,1,&w[i],&t);
                    221:     sortbynm(w,n);
                    222:     qltozl(w,n,dvr);
                    223:     for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                    224:       NEXTMP(mr0,mr); divsp(CO,(P)m->c,(P)(*dvr),(P *)&mr->c); mr->dl = m->dl;
                    225:     }
                    226:     NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
                    227:   }
                    228: }
                    229:
                    230: void dp_idiv(DP p,Z c,DP *rp)
                    231: {
                    232:   MP mr0,m,mr;
                    233:
                    234:   if ( !p )
                    235:     *rp = 0;
                    236:   else if ( MUNIQ((Q)c) )
                    237:     *rp = p;
                    238:   else if ( MUNIQ((Q)c) )
                    239:     chsgnd(p,rp);
                    240:   else {
                    241:     for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                    242:       NEXTMP(mr0,mr);
1.2       noro      243:       divsz((Z)(m->c),c,(Z *)&mr->c);
1.1       noro      244:       mr->dl = m->dl;
                    245:     }
                    246:     NEXT(mr) = 0; MKDP(p->nv,mr0,*rp);
                    247:     if ( *rp )
                    248:       (*rp)->sugar = p->sugar;
                    249:   }
                    250: }
                    251:
                    252: void dp_mbase(NODE hlist,NODE *mbase)
                    253: {
                    254:   DL *dl;
                    255:   DL d;
                    256:   int *t;
                    257:   int i,j,k,n,nvar,td;
                    258:
                    259:   n = length(hlist); nvar = ((DP)BDY(hlist))->nv;
                    260:   dl = (DL *)MALLOC(n*sizeof(DL));
                    261:   NEWDL(d,nvar); *mbase = 0;
                    262:   for ( i = 0; i < n; i++, hlist = NEXT(hlist) ) {
                    263:     dl[i] = BDY((DP)BDY(hlist))->dl;
                    264:     /* trivial ideal check */
                    265:     if ( (*cmpdl)(nvar,d,dl[i]) == 0 ) {
                    266:       return;
                    267:     }
                    268:   }
                    269:   /* zero-dim. ideal check */
                    270:   for ( i = 0; i < nvar; i++ ) {
                    271:     for ( j = 0; j < n; j++ ) {
                    272:       for ( k = 0, t = dl[j]->d; k < nvar; k++ )
                    273:         if ( k != i && t[k] != 0 ) break;
                    274:       if ( k == nvar ) break;
                    275:     }
                    276:     if ( j == n )
                    277:       error("dp_mbase : input ideal is not zero-dimensional");
                    278:   }
                    279:   while ( 1 ) {
                    280:     insert_to_node(d,mbase,nvar);
                    281:     for ( i = nvar-1; i >= 0; ) {
                    282:       d->d[i]++;
                    283:       d->td += MUL_WEIGHT(1,i);
                    284:       for ( j = 0; j < n; j++ ) {
                    285:         if ( _dl_redble(dl[j],d,nvar) )
                    286:           break;
                    287:       }
                    288:       if ( j < n ) {
                    289:         for ( j = nvar-1; j >= i; j-- )
                    290:           d->d[j] = 0;
                    291:         for ( j = 0, td = 0; j < i; j++ )
                    292:           td += MUL_WEIGHT(d->d[j],j);
                    293:         d->td = td;
                    294:         i--;
                    295:       } else
                    296:         break;
                    297:     }
                    298:     if ( i < 0 )
                    299:       break;
                    300:   }
                    301: }
                    302:
                    303: int _dl_redble(DL d1,DL d2,int nvar)
                    304: {
                    305:   int i;
                    306:
                    307:   if ( d1->td > d2->td )
                    308:     return 0;
                    309:   for ( i = 0; i < nvar; i++ )
                    310:     if ( d1->d[i] > d2->d[i] )
                    311:       break;
                    312:   if ( i < nvar )
                    313:     return 0;
                    314:   else
                    315:     return 1;
                    316: }
                    317:
                    318: void insert_to_node(DL d,NODE *n,int nvar)
                    319: {
                    320:   DL d1;
                    321:   MP m;
                    322:   DP dp;
                    323:   NODE n0,n1,n2;
                    324:
                    325:   NEWDL(d1,nvar); d1->td = d->td;
                    326:   bcopy((char *)d->d,(char *)d1->d,nvar*sizeof(int));
                    327:   NEWMP(m); m->dl = d1; m->c = (Obj)ONE; NEXT(m) = 0;
                    328:   MKDP(nvar,m,dp); dp->sugar = d->td;
                    329:   if ( !(*n) ) {
                    330:     MKNODE(n1,dp,0); *n = n1;
                    331:   } else {
                    332:     for ( n1 = *n, n0 = 0; n1; n0 = n1, n1 = NEXT(n1) )
                    333:       if ( (*cmpdl)(nvar,d,BDY((DP)BDY(n1))->dl) > 0 ) {
                    334:         MKNODE(n2,dp,n1);
                    335:         if ( !n0 )
                    336:           *n = n2;
                    337:         else
                    338:           NEXT(n0) = n2;
                    339:         break;
                    340:       }
                    341:     if ( !n1 ) {
                    342:       MKNODE(n2,dp,0); NEXT(n0) = n2;
                    343:     }
                    344:   }
                    345: }
                    346:
                    347: void dp_vtod(Q *c,DP p,DP *rp)
                    348: {
                    349:   MP mr0,m,mr;
                    350:   int i;
                    351:
                    352:   if ( !p )
                    353:     *rp = 0;
                    354:   else {
                    355:     for ( mr0 = 0, m = BDY(p), i = 0; m; m = NEXT(m), i++ ) {
                    356:       NEXTMP(mr0,mr); mr->c = (Obj)c[i]; mr->dl = m->dl;
                    357:     }
                    358:     NEXT(mr) = 0; MKDP(p->nv,mr0,*rp);
                    359:     (*rp)->sugar = p->sugar;
                    360:   }
                    361: }
                    362:
                    363: int have_sf_coef(P p)
                    364: {
                    365:   DCP dc;
                    366:
                    367:   if ( !p )
                    368:     return 0;
                    369:   else if ( NUM(p) )
                    370:     return NID((Num)p) == N_GFS ? 1 : 0;
                    371:   else {
                    372:     for ( dc = DC(p); dc; dc = NEXT(dc) )
                    373:       if ( have_sf_coef(COEF(dc)) )
                    374:         return 1;
                    375:     return 0;
                    376:   }
                    377: }
                    378:
                    379: void head_coef(P p,Num *c)
                    380: {
                    381:   if ( !p )
                    382:     *c = 0;
                    383:   else if ( NUM(p) )
                    384:     *c = (Num)p;
                    385:   else
                    386:     head_coef(COEF(DC(p)),c);
                    387: }
                    388:
                    389: void dp_monic_sf(DP p,DP *rp)
                    390: {
                    391:   Num c;
                    392:
                    393:   if ( !p )
                    394:     *rp = 0;
                    395:   else {
                    396:     head_coef((P)BDY(p)->c,&c);
                    397:     divsdc(CO,p,(P)c,rp);
                    398:   }
                    399: }
                    400:
                    401: void dp_prim(DP p,DP *rp)
                    402: {
                    403:   P t,g;
                    404:   DP p1;
                    405:   MP m,mr,mr0;
                    406:   int i,n;
                    407:   P *w;
                    408:   Q *c;
                    409:   Z dvr;
                    410:   NODE tn;
                    411:
                    412:   if ( !p )
                    413:     *rp = 0;
                    414:   else if ( dp_fcoeffs == N_GFS ) {
                    415:     for ( m = BDY(p); m; m = NEXT(m) )
                    416:       if ( OID(m->c) == O_N ) {
                    417:         /* GCD of coeffs = 1 */
                    418:         dp_monic_sf(p,rp);
                    419:         return;
                    420:       } else break;
                    421:     /* compute GCD over the finite fieid */
                    422:     for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                    423:     w = (P *)ALLOCA(n*sizeof(P));
                    424:     for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ )
                    425:       w[i] = (P)m->c;
                    426:     gcdsf(CO,w,n,&g);
                    427:     if ( NUM(g) )
                    428:       dp_monic_sf(p,rp);
                    429:     else {
                    430:       for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                    431:         NEXTMP(mr0,mr); divsp(CO,(P)m->c,g,(P *)&mr->c); mr->dl = m->dl;
                    432:       }
                    433:       NEXT(mr) = 0; MKDP(p->nv,mr0,p1); p1->sugar = p->sugar;
                    434:       dp_monic_sf(p1,rp);
                    435:     }
                    436:     return;
                    437:   } else if ( dp_fcoeffs )
                    438:     *rp = p;
                    439:   else if ( NoGCD )
                    440:     dp_ptozp(p,rp);
                    441:   else {
                    442:     dp_ptozp(p,&p1); p = p1;
                    443:     for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                    444:     if ( n == 1 ) {
                    445:       m = BDY(p);
                    446:       NEWMP(mr); mr->dl = m->dl; mr->c = (Obj)ONE; NEXT(mr) = 0;
                    447:       MKDP(p->nv,mr,*rp); (*rp)->sugar = p->sugar;
                    448:       return;
                    449:     }
                    450:     w = (P *)ALLOCA(n*sizeof(P));
                    451:     c = (Q *)ALLOCA(n*sizeof(Q));
                    452:     for ( m =BDY(p), i = 0; i < n; m = NEXT(m), i++ )
                    453:       if ( NUM(m->c) ) {
                    454:         c[i] = (Q)m->c; w[i] = (P)ONE;
                    455:       } else
                    456:         ptozp((P)m->c,1,&c[i],&w[i]);
                    457:     qltozl(c,n,&dvr); heu_nezgcdnpz(CO,w,n,&t); mulp(CO,t,(P)dvr,&g);
                    458:     if ( NUM(g) )
                    459:       *rp = p;
                    460:     else {
                    461:       for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                    462:         NEXTMP(mr0,mr); divsp(CO,(P)m->c,g,(P *)&mr->c); mr->dl = m->dl;
                    463:       }
                    464:       NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
                    465:       add_denomlist(g);
                    466:     }
                    467:   }
                    468: }
                    469:
                    470: void heu_nezgcdnpz(VL vl,P *pl,int m,P *pr)
                    471: {
                    472:   int i,r;
                    473:   P gcd,t,s1,s2,u;
                    474:   Z rq;
                    475:   DCP dc;
                    476:   extern int DP_Print;
                    477:
                    478:   while ( 1 ) {
                    479:     for ( i = 0, s1 = 0; i < m; i++ ) {
1.2       noro      480:       r = random(); UTOZ(r,rq);
1.1       noro      481:       mulp(vl,pl[i],(P)rq,&t); addp(vl,s1,t,&u); s1 = u;
                    482:     }
                    483:     for ( i = 0, s2 = 0; i < m; i++ ) {
1.2       noro      484:       r = random(); UTOZ(r,rq);
1.1       noro      485:       mulp(vl,pl[i],(P)rq,&t); addp(vl,s2,t,&u); s2 = u;
                    486:     }
                    487:     ezgcdp(vl,s1,s2,&gcd);
                    488:     if ( DP_Print > 2 )
                    489:       { fprintf(asir_out,"(%d)",nmonop(gcd)); fflush(asir_out); }
                    490:     for ( i = 0; i < m; i++ ) {
                    491:       if ( !divtpz(vl,pl[i],gcd,&t) )
                    492:         break;
                    493:     }
                    494:     if ( i == m )
                    495:       break;
                    496:   }
                    497:   *pr = gcd;
                    498: }
                    499:
                    500: void dp_prim_mod(DP p,int mod,DP *rp)
                    501: {
                    502:   P t,g;
                    503:   MP m,mr,mr0;
                    504:
                    505:   if ( !p )
                    506:     *rp = 0;
                    507:   else if ( NoGCD )
                    508:     *rp = p;
                    509:   else {
                    510:     for ( m = BDY(p), g = (P)m->c, m = NEXT(m); m; m = NEXT(m) ) {
                    511:       gcdprsmp(CO,mod,g,(P)m->c,&t); g = t;
                    512:     }
                    513:     for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                    514:       NEXTMP(mr0,mr); divsmp(CO,mod,(P)m->c,g,(P *)&mr->c); mr->dl = m->dl;
                    515:     }
                    516:     NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
                    517:   }
                    518: }
                    519:
                    520: void dp_cont(DP p,Z *rp)
                    521: {
                    522:   VECT v;
                    523:
                    524:   dp_dtov(p,&v); gcdvz(v,rp);
                    525: }
                    526:
                    527: void dp_dtov(DP dp,VECT *rp)
                    528: {
                    529:   MP m,t;
                    530:   int i,n;
                    531:   VECT v;
                    532:   pointer *p;
                    533:
                    534:   m = BDY(dp);
                    535:   for ( t = m, n = 0; t; t = NEXT(t), n++ );
                    536:   MKVECT(v,n);
                    537:   for ( i = 0, p = BDY(v), t = m; i < n; t = NEXT(t), i++ )
                    538:     p[i] = (pointer)(t->c);
                    539:   *rp = v;
                    540: }
                    541:
                    542: /*
                    543:  * s-poly computation
                    544:  *
                    545:  */
                    546:
                    547: void dp_sp(DP p1,DP p2,DP *rp)
                    548: {
                    549:   int i,n,td;
                    550:   int *w;
                    551:   DL d1,d2,d;
                    552:   MP m;
                    553:   DP t,s1,s2,u;
                    554:   Z c,c1,c2;
                    555:   Z gn;
                    556:
                    557:   n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
                    558:   w = (int *)ALLOCA(n*sizeof(int));
                    559:   for ( i = 0, td = 0; i < n; i++ ) {
                    560:     w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
                    561:   }
                    562:
                    563:   NEWDL(d,n); d->td = td - d1->td;
                    564:   for ( i = 0; i < n; i++ )
                    565:     d->d[i] = w[i] - d1->d[i];
                    566:   c1 = (Z)BDY(p1)->c; c2 = (Z)BDY(p2)->c;
                    567:   if ( INT(c1) && INT(c2) ) {
                    568:     gcdz(c1,c2,&gn);
                    569:     if ( !UNIQ(gn) ) {
1.2       noro      570:       divsz(c1,gn,&c); c1 = c;
                    571:       divsz(c2,gn,&c);c2 = c;
1.1       noro      572:     }
                    573:   }
                    574:
                    575:   NEWMP(m); m->dl = d; m->c = (Obj)c2; NEXT(m) = 0;
                    576:   MKDP(n,m,s1); s1->sugar = d->td; muld(CO,s1,p1,&t);
                    577:
                    578:   NEWDL(d,n); d->td = td - d2->td;
                    579:   for ( i = 0; i < n; i++ )
                    580:     d->d[i] = w[i] - d2->d[i];
                    581:   NEWMP(m); m->dl = d; m->c = (Obj)c1; NEXT(m) = 0;
                    582:   MKDP(n,m,s2); s2->sugar = d->td; muld(CO,s2,p2,&u);
                    583:
                    584:   subd(CO,t,u,rp);
                    585:   if ( GenTrace ) {
                    586:     LIST hist;
                    587:     NODE node;
                    588:
                    589:     node = mknode(4,ONE,NULLP,s1,ONE);
                    590:     MKLIST(hist,node);
                    591:     MKNODE(TraceList,hist,0);
                    592:
                    593:     node = mknode(4,ONE,NULLP,NULLP,ONE);
                    594:     chsgnd(s2,(DP *)&ARG2(node));
                    595:     MKLIST(hist,node);
                    596:     MKNODE(node,hist,TraceList); TraceList = node;
                    597:   }
                    598: }
                    599:
1.3       noro      600: void dpm_sp(DPM p1,DPM p2,DPM *rp,DP *mul1,DP *mul2)
1.1       noro      601: {
                    602:   int i,n,td;
                    603:   int *w;
                    604:   DL d1,d2,d;
                    605:   MP m;
                    606:   DP s1,s2;
                    607:   DPM t,u;
                    608:   Z c,c1,c2;
                    609:   Z gn;
                    610:
                    611:   n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
                    612:   if ( BDY(p1)->pos != BDY(p2)->pos ) {
1.3       noro      613:     *mul1 = 0; *mul2 = 0; *rp = 0;
1.1       noro      614:     return;
                    615:   }
                    616:   w = (int *)ALLOCA(n*sizeof(int));
                    617:   for ( i = 0, td = 0; i < n; i++ ) {
                    618:     w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
                    619:   }
                    620:
                    621:   NEWDL(d,n); d->td = td - d1->td;
                    622:   for ( i = 0; i < n; i++ )
                    623:     d->d[i] = w[i] - d1->d[i];
                    624:   c1 = (Z)BDY(p1)->c; c2 = (Z)BDY(p2)->c;
                    625:   if ( INT(c1) && INT(c2) ) {
                    626:     gcdz(c1,c2,&gn);
                    627:     if ( !UNIQ(gn) ) {
1.2       noro      628:       divsz(c1,gn,&c); c1 = c;
                    629:       divsz(c2,gn,&c);c2 = c;
1.1       noro      630:     }
                    631:   }
                    632:
                    633:   NEWMP(m); m->dl = d; m->c = (Obj)c2; NEXT(m) = 0;
                    634:   MKDP(n,m,s1); s1->sugar = d->td; mulobjdpm(CO,(Obj)s1,p1,&t);
1.3       noro      635:   *mul1 = s1;
1.1       noro      636:
                    637:   NEWDL(d,n); d->td = td - d2->td;
                    638:   for ( i = 0; i < n; i++ )
                    639:     d->d[i] = w[i] - d2->d[i];
                    640:   NEWMP(m); m->dl = d; m->c = (Obj)c1; NEXT(m) = 0;
                    641:   MKDP(n,m,s2); s2->sugar = d->td; mulobjdpm(CO,(Obj)s2,p2,&u);
1.3       noro      642:   *mul2 = s2;
1.1       noro      643:
                    644:   subdpm(CO,t,u,rp);
                    645:   if ( GenTrace ) {
                    646:     LIST hist;
                    647:     NODE node;
                    648:
                    649:     node = mknode(4,ONE,NULLP,s1,ONE);
                    650:     MKLIST(hist,node);
                    651:     MKNODE(TraceList,hist,0);
                    652:
                    653:     node = mknode(4,ONE,NULLP,NULLP,ONE);
                    654:     chsgnd(s2,(DP *)&ARG2(node));
                    655:     MKLIST(hist,node);
                    656:     MKNODE(node,hist,TraceList); TraceList = node;
                    657:   }
                    658: }
                    659:
1.4       noro      660: DP dpm_sp_hm(DPM p1,DPM p2)
                    661: {
                    662:   int i,n,td;
                    663:   int *w;
                    664:   DL d1,d2,d;
                    665:   MP m;
                    666:   DP s1;
                    667:
                    668:   n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
                    669:   if ( BDY(p1)->pos != BDY(p2)->pos ) {
                    670:     return 0;
                    671:   }
                    672:   w = (int *)ALLOCA(n*sizeof(int));
                    673:   for ( i = 0, td = 0; i < n; i++ ) {
                    674:     w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
                    675:   }
                    676:
                    677:   NEWDL(d,n); d->td = td - d1->td;
                    678:   for ( i = 0; i < n; i++ )
                    679:     d->d[i] = w[i] - d1->d[i];
                    680:
                    681:   NEWMP(m); m->dl = d; m->c = (Obj)ONE; NEXT(m) = 0;
                    682:   MKDP(n,m,s1); s1->sugar = d->td;
                    683:   return s1;
                    684: }
                    685:
1.1       noro      686: void _dp_sp_dup(DP p1,DP p2,DP *rp)
                    687: {
                    688:   int i,n,td;
                    689:   int *w;
                    690:   DL d1,d2,d;
                    691:   MP m;
                    692:   DP t,s1,s2,u;
                    693:   Z c,c1,c2;
                    694:   Z gn;
                    695:
                    696:   n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
                    697:   w = (int *)ALLOCA(n*sizeof(int));
                    698:   for ( i = 0, td = 0; i < n; i++ ) {
                    699:     w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
                    700:   }
                    701:
                    702:   _NEWDL(d,n); d->td = td - d1->td;
                    703:   for ( i = 0; i < n; i++ )
                    704:     d->d[i] = w[i] - d1->d[i];
                    705:   c1 = (Z)BDY(p1)->c; c2 = (Z)BDY(p2)->c;
                    706:   if ( INT(c1) && INT(c2) ) {
                    707:     gcdz(c1,c2,&gn);
                    708:     if ( !UNIQ(gn) ) {
1.2       noro      709:       divsz(c1,gn,&c); c1 = c;
                    710:       divsz(c2,gn,&c);c2 = c;
1.1       noro      711:     }
                    712:   }
                    713:
                    714:   _NEWMP(m); m->dl = d; m->c = (Obj)c2; NEXT(m) = 0;
                    715:   _MKDP(n,m,s1); s1->sugar = d->td; _muld_dup(CO,s1,p1,&t); _free_dp(s1);
                    716:
                    717:   _NEWDL(d,n); d->td = td - d2->td;
                    718:   for ( i = 0; i < n; i++ )
                    719:     d->d[i] = w[i] - d2->d[i];
                    720:   _NEWMP(m); m->dl = d; chsgnp((P)c1,(P *)&m->c); NEXT(m) = 0;
                    721:   _MKDP(n,m,s2); s2->sugar = d->td; _muld_dup(CO,s2,p2,&u); _free_dp(s2);
                    722:
                    723:   _addd_destructive(CO,t,u,rp);
                    724:   if ( GenTrace ) {
                    725:     LIST hist;
                    726:     NODE node;
                    727:
                    728:     node = mknode(4,ONE,NULLP,s1,ONE);
                    729:     MKLIST(hist,node);
                    730:     MKNODE(TraceList,hist,0);
                    731:
                    732:     node = mknode(4,ONE,NULLP,NULLP,ONE);
                    733:     chsgnd(s2,(DP *)&ARG2(node));
                    734:     MKLIST(hist,node);
                    735:     MKNODE(node,hist,TraceList); TraceList = node;
                    736:   }
                    737: }
                    738:
                    739: void dp_sp_mod(DP p1,DP p2,int mod,DP *rp)
                    740: {
                    741:   int i,n,td;
                    742:   int *w;
                    743:   DL d1,d2,d;
                    744:   MP m;
                    745:   DP t,s,u;
                    746:
                    747:   n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
                    748:   w = (int *)ALLOCA(n*sizeof(int));
                    749:   for ( i = 0, td = 0; i < n; i++ ) {
                    750:     w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
                    751:   }
                    752:   NEWDL_NOINIT(d,n); d->td = td - d1->td;
                    753:   for ( i = 0; i < n; i++ )
                    754:     d->d[i] = w[i] - d1->d[i];
                    755:   NEWMP(m); m->dl = d; m->c = (Obj)BDY(p2)->c; NEXT(m) = 0;
                    756:   MKDP(n,m,s); s->sugar = d->td; mulmd(CO,mod,p1,s,&t);
                    757:   NEWDL_NOINIT(d,n); d->td = td - d2->td;
                    758:   for ( i = 0; i < n; i++ )
                    759:     d->d[i] = w[i] - d2->d[i];
                    760:   NEWMP(m); m->dl = d; m->c = (Obj)BDY(p1)->c; NEXT(m) = 0;
                    761:   MKDP(n,m,s); s->sugar = d->td; mulmd(CO,mod,p2,s,&u);
                    762:   submd(CO,mod,t,u,rp);
                    763: }
                    764:
                    765: void _dp_sp_mod_dup(DP p1,DP p2,int mod,DP *rp)
                    766: {
                    767:   int i,n,td;
                    768:   int *w;
                    769:   DL d1,d2,d;
                    770:   MP m;
                    771:   DP t,s,u;
                    772:
                    773:   n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
                    774:   w = (int *)ALLOCA(n*sizeof(int));
                    775:   for ( i = 0, td = 0; i < n; i++ ) {
                    776:     w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
                    777:   }
                    778:   _NEWDL(d,n); d->td = td - d1->td;
                    779:   for ( i = 0; i < n; i++ )
                    780:     d->d[i] = w[i] - d1->d[i];
                    781:   _NEWMP(m); m->dl = d; m->c = BDY(p2)->c; NEXT(m) = 0;
                    782:   _MKDP(n,m,s); s->sugar = d->td; _mulmd_dup(mod,s,p1,&t); _free_dp(s);
                    783:   _NEWDL(d,n); d->td = td - d2->td;
                    784:   for ( i = 0; i < n; i++ )
                    785:     d->d[i] = w[i] - d2->d[i];
                    786:   _NEWMP(m); m->dl = d; m->c = (Obj)STOI(mod - ITOS(BDY(p1)->c)); NEXT(m) = 0;
                    787:   _MKDP(n,m,s); s->sugar = d->td; _mulmd_dup(mod,s,p2,&u); _free_dp(s);
                    788:   _addmd_destructive(mod,t,u,rp);
                    789: }
                    790:
                    791: void _dp_sp_mod(DP p1,DP p2,int mod,DP *rp)
                    792: {
                    793:   int i,n,td;
                    794:   int *w;
                    795:   DL d1,d2,d;
                    796:   MP m;
                    797:   DP t,s,u;
                    798:
                    799:   n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
                    800:   w = (int *)ALLOCA(n*sizeof(int));
                    801:   for ( i = 0, td = 0; i < n; i++ ) {
                    802:     w[i] = MAX(d1->d[i],d2->d[i]); td += MUL_WEIGHT(w[i],i);
                    803:   }
                    804:   NEWDL(d,n); d->td = td - d1->td;
                    805:   for ( i = 0; i < n; i++ )
                    806:     d->d[i] = w[i] - d1->d[i];
                    807:   NEWMP(m); m->dl = d; m->c = BDY(p2)->c; NEXT(m) = 0;
                    808:   MKDP(n,m,s); s->sugar = d->td; mulmd_dup(mod,s,p1,&t);
                    809:   NEWDL(d,n); d->td = td - d2->td;
                    810:   for ( i = 0; i < n; i++ )
                    811:     d->d[i] = w[i] - d2->d[i];
                    812:   NEWMP(m); m->dl = d; m->c = (Obj)STOI(mod - ITOS(BDY(p1)->c)); NEXT(m) = 0;
                    813:   MKDP(n,m,s); s->sugar = d->td; mulmd_dup(mod,s,p2,&u);
                    814:   addmd_destructive(mod,t,u,rp);
                    815: }
                    816:
                    817: /*
                    818:  * m-reduction
                    819:  * do content reduction over Z or Q(x,...)
                    820:  * do nothing over finite fields
                    821:  *
1.3       noro      822:  * head+rest = dn*(p0+p1)+mult*p2
1.1       noro      823:  */
                    824:
                    825: void dp_red(DP p0,DP p1,DP p2,DP *head,DP *rest,P *dnp,DP *multp)
                    826: {
                    827:   int i,n;
                    828:   DL d1,d2,d;
                    829:   MP m;
                    830:   DP t,s,r,h;
                    831:   Z c,c1,c2,gn;
                    832:   P g,a;
                    833:   P p[2];
                    834:
                    835:   n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
                    836:   NEWDL(d,n); d->td = d1->td - d2->td;
                    837:   for ( i = 0; i < n; i++ )
                    838:     d->d[i] = d1->d[i]-d2->d[i];
                    839:   c1 = (Z)BDY(p1)->c; c2 = (Z)BDY(p2)->c;
                    840:   if ( dp_fcoeffs == N_GFS ) {
                    841:     p[0] = (P)c1; p[1] = (P)c2;
                    842:     gcdsf(CO,p,2,&g);
                    843:     divsp(CO,(P)c1,g,&a); c1 = (Z)a; divsp(CO,(P)c2,g,&a); c2 = (Z)a;
                    844:   } else if ( dp_fcoeffs ) {
                    845:     /* do nothing */
                    846:   } else if ( INT(c1) && INT(c2) ) {
                    847:     gcdz(c1,c2,&gn);
                    848:     if ( !UNIQ(gn) ) {
1.2       noro      849:       divsz(c1,gn,&c); c1 = c;
                    850:       divsz(c2,gn,&c); c2 = c;
1.1       noro      851:     }
                    852:   } else {
                    853:     ezgcdpz(CO,(P)c1,(P)c2,&g);
                    854:     divsp(CO,(P)c1,g,&a); c1 = (Z)a; divsp(CO,(P)c2,g,&a); c2 = (Z)a;
                    855:     add_denomlist(g);
                    856:   }
                    857:   NEWMP(m); m->dl = d; chsgnp((P)c1,(P *)&m->c); NEXT(m) = 0; MKDP(n,m,s); s->sugar = d->td;
                    858:   *multp = s;
                    859:   muld(CO,s,p2,&t); muldc(CO,p1,(Obj)c2,&s); addd(CO,s,t,&r);
                    860:   muldc(CO,p0,(Obj)c2,&h);
                    861:   *head = h; *rest = r; *dnp = (P)c2;
                    862: }
                    863:
1.3       noro      864: // head+rest = dn*(p0+p1)-mult*p2
1.1       noro      865: void dpm_red(DPM p0,DPM p1,DPM p2,DPM *head,DPM *rest,P *dnp,DP *multp)
                    866: {
                    867:   int i,n,pos;
                    868:   DL d1,d2,d;
                    869:   MP m;
1.4       noro      870:   DP s,ms;
1.1       noro      871:   DPM t,r,h,u,w;
                    872:   Z c,c1,c2,gn;
                    873:   P g,a;
                    874:   P p[2];
                    875:
                    876:   n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl; pos = BDY(p1)->pos;
                    877:   if ( pos != BDY(p2)->pos )
                    878:     error("dpm_red : cannot happen");
                    879:   NEWDL(d,n); d->td = d1->td - d2->td;
                    880:   for ( i = 0; i < n; i++ )
                    881:     d->d[i] = d1->d[i]-d2->d[i];
                    882:   c1 = (Z)BDY(p1)->c; c2 = (Z)BDY(p2)->c;
                    883:   if ( dp_fcoeffs == N_GFS ) {
                    884:     p[0] = (P)c1; p[1] = (P)c2;
                    885:     gcdsf(CO,p,2,&g);
                    886:     divsp(CO,(P)c1,g,&a); c1 = (Z)a; divsp(CO,(P)c2,g,&a); c2 = (Z)a;
                    887:   } else if ( dp_fcoeffs ) {
                    888:     /* do nothing */
                    889:   } else if ( INT(c1) && INT(c2) ) {
                    890:     gcdz(c1,c2,&gn);
                    891:     if ( !UNIQ(gn) ) {
1.2       noro      892:       divsz(c1,gn,&c); c1 = c;
                    893:       divsz(c2,gn,&c); c2 = c;
1.1       noro      894:     }
                    895:   } else {
                    896:     ezgcdpz(CO,(P)c1,(P)c2,&g);
                    897:     divsp(CO,(P)c1,g,&a); c1 = (Z)a; divsp(CO,(P)c2,g,&a); c2 = (Z)a;
                    898:     add_denomlist(g);
                    899:   }
1.3       noro      900:   NEWMP(m); m->dl = d; m->c = (Obj)c1; NEXT(m) = 0; MKDP(n,m,s); s->sugar = d->td;
1.1       noro      901:   *multp = s;
1.4       noro      902:   chsgnd(s,&ms); mulobjdpm(CO,(Obj)ms,p2,&u); mulobjdpm(CO,(Obj)c2,p1,&w); adddpm(CO,w,u,&r);
1.1       noro      903:   mulobjdpm(CO,(Obj)c2,p0,&h);
                    904:   *head = h; *rest = r; *dnp = (P)c2;
                    905: }
                    906:
1.5       noro      907: void dpm_red2(DPM p1,DPM p2,DPM *rest,P *dnp,DP *multp)
                    908: {
                    909:   int i,n,pos;
                    910:   DL d1,d2,d;
                    911:   MP m;
                    912:   DP s,ms;
                    913:   DPM t,r,h,u,w;
                    914:   Z c,c1,c2,gn;
                    915:   P g,a;
                    916:   P p[2];
                    917:
                    918:   n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl; pos = BDY(p1)->pos;
                    919:   if ( pos != BDY(p2)->pos )
                    920:     error("dpm_red : cannot happen");
                    921:   NEWDL(d,n); d->td = d1->td - d2->td;
                    922:   for ( i = 0; i < n; i++ )
                    923:     d->d[i] = d1->d[i]-d2->d[i];
                    924:   c1 = (Z)BDY(p1)->c; c2 = (Z)BDY(p2)->c;
                    925:   if ( dp_fcoeffs == N_GFS ) {
                    926:     p[0] = (P)c1; p[1] = (P)c2;
                    927:     gcdsf(CO,p,2,&g);
                    928:     divsp(CO,(P)c1,g,&a); c1 = (Z)a; divsp(CO,(P)c2,g,&a); c2 = (Z)a;
                    929:   } else if ( dp_fcoeffs ) {
                    930:     /* do nothing */
                    931:   } else if ( INT(c1) && INT(c2) ) {
                    932:     gcdz(c1,c2,&gn);
                    933:     if ( !UNIQ(gn) ) {
                    934:       divsz(c1,gn,&c); c1 = c;
                    935:       divsz(c2,gn,&c); c2 = c;
                    936:     }
                    937:   } else {
                    938:     ezgcdpz(CO,(P)c1,(P)c2,&g);
                    939:     divsp(CO,(P)c1,g,&a); c1 = (Z)a; divsp(CO,(P)c2,g,&a); c2 = (Z)a;
                    940:     add_denomlist(g);
                    941:   }
                    942:   NEWMP(m); m->dl = d; m->c = (Obj)c1; NEXT(m) = 0; MKDP(n,m,s); s->sugar = d->td;
                    943:   *multp = s;
                    944:   chsgnd(s,&ms); mulobjdpm(CO,(Obj)ms,p2,&u); mulobjdpm(CO,(Obj)c2,p1,&w); adddpm(CO,w,u,&r);
                    945:   *rest = r; *dnp = (P)c2;
                    946: }
1.1       noro      947:
                    948: /*
                    949:  * m-reduction by a marked poly
                    950:  * do content reduction over Z or Q(x,...)
                    951:  * do nothing over finite fields
                    952:  *
                    953:  */
                    954:
                    955:
                    956: void dp_red_marked(DP p0,DP p1,DP p2,DP hp2,DP *head,DP *rest,P *dnp,DP *multp)
                    957: {
                    958:   int i,n;
                    959:   DL d1,d2,d;
                    960:   MP m;
                    961:   DP t,s,r,h;
                    962:   Z c,c1,c2,gn;
                    963:   P g,a;
                    964:   P p[2];
                    965:
                    966:   n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(hp2)->dl;
                    967:   NEWDL(d,n); d->td = d1->td - d2->td;
                    968:   for ( i = 0; i < n; i++ )
                    969:     d->d[i] = d1->d[i]-d2->d[i];
                    970:   c1 = (Z)BDY(p1)->c; c2 = (Z)BDY(hp2)->c;
                    971:   if ( dp_fcoeffs == N_GFS ) {
                    972:     p[0] = (P)c1; p[1] = (P)c2;
                    973:     gcdsf(CO,p,2,&g);
                    974:     divsp(CO,(P)c1,g,&a); c1 = (Z)a; divsp(CO,(P)c2,g,&a); c2 = (Z)a;
                    975:   } else if ( dp_fcoeffs ) {
                    976:     /* do nothing */
                    977:   } else if ( INT(c1) && INT(c2) ) {
                    978:     gcdz(c1,c2,&gn);
                    979:     if ( !UNIQ(gn) ) {
1.2       noro      980:       divsz(c1,gn,&c); c1 = c;
                    981:       divsz(c2,gn,&c); c2 = c;
1.1       noro      982:     }
                    983:   } else {
                    984:     ezgcdpz(CO,(P)c1,(P)c2,&g);
                    985:     divsp(CO,(P)c1,g,&a); c1 = (Z)a; divsp(CO,(P)c2,g,&a); c2 = (Z)a;
                    986:   }
                    987:   NEWMP(m); m->dl = d; m->c = (Obj)c1; NEXT(m) = 0; MKDP(n,m,s); s->sugar = d->td;
                    988:   *multp = s;
                    989:   muld(CO,s,p2,&t); muldc(CO,p1,(Obj)c2,&s); subd(CO,s,t,&r);
                    990:   muldc(CO,p0,(Obj)c2,&h);
                    991:   *head = h; *rest = r; *dnp = (P)c2;
                    992: }
                    993:
                    994: void dp_red_marked_mod(DP p0,DP p1,DP p2,DP hp2,int mod,DP *head,DP *rest,P *dnp,DP *multp)
                    995: {
                    996:   int i,n;
                    997:   DL d1,d2,d;
                    998:   MP m;
                    999:   DP t,s,r,h;
                   1000:   P c1,c2,g,u;
                   1001:
                   1002:   n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(hp2)->dl;
                   1003:   NEWDL(d,n); d->td = d1->td - d2->td;
                   1004:   for ( i = 0; i < n; i++ )
                   1005:     d->d[i] = d1->d[i]-d2->d[i];
                   1006:   c1 = (P)BDY(p1)->c; c2 = (P)BDY(hp2)->c;
                   1007:   gcdprsmp(CO,mod,c1,c2,&g);
                   1008:   divsmp(CO,mod,c1,g,&u); c1 = u; divsmp(CO,mod,c2,g,&u); c2 = u;
                   1009:   if ( NUM(c2) ) {
                   1010:     divsmp(CO,mod,c1,c2,&u); c1 = u; c2 = (P)ONEM;
                   1011:   }
                   1012:   NEWMP(m); m->dl = d; m->c = (Obj)c1; NEXT(m) = 0;
                   1013:   MKDP(n,m,s); s->sugar = d->td;
                   1014:   *multp = s;
                   1015:   mulmd(CO,mod,s,p2,&t);
                   1016:   if ( NUM(c2) ) {
                   1017:     submd(CO,mod,p1,t,&r); h = p0;
                   1018:   } else {
                   1019:     mulmdc(CO,mod,p1,c2,&s); submd(CO,mod,s,t,&r); mulmdc(CO,mod,p0,c2,&h);
                   1020:   }
                   1021:   *head = h; *rest = r; *dnp = c2;
                   1022: }
                   1023:
                   1024: /* m-reduction over a field */
                   1025:
                   1026: void dp_red_f(DP p1,DP p2,DP *rest)
                   1027: {
                   1028:   int i,n;
                   1029:   DL d1,d2,d;
                   1030:   MP m;
                   1031:   DP t,s;
                   1032:   Obj a,b;
                   1033:
                   1034:   n = p1->nv;
                   1035:   d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
                   1036:
                   1037:   NEWDL(d,n); d->td = d1->td - d2->td;
                   1038:   for ( i = 0; i < n; i++ )
                   1039:     d->d[i] = d1->d[i]-d2->d[i];
                   1040:
                   1041:   NEWMP(m); m->dl = d;
                   1042:   divr(CO,(Obj)BDY(p1)->c,(Obj)BDY(p2)->c,&a); chsgnr(a,&b);
                   1043:   C(m) = (Obj)b;
                   1044:   NEXT(m) = 0; MKDP(n,m,s); s->sugar = d->td;
                   1045:
                   1046:   muld(CO,s,p2,&t); addd(CO,p1,t,rest);
                   1047: }
                   1048:
                   1049: void dpm_red_f(DPM p1,DPM p2,DPM *rest)
                   1050: {
                   1051:   int i,n;
                   1052:   DL d1,d2,d;
                   1053:   MP m;
                   1054:   DPM t;
                   1055:   DP s;
                   1056:   Obj a,b;
                   1057:
                   1058:   n = p1->nv;
                   1059:   d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
                   1060:
                   1061:   NEWDL(d,n); d->td = d1->td - d2->td;
                   1062:   for ( i = 0; i < n; i++ )
                   1063:     d->d[i] = d1->d[i]-d2->d[i];
                   1064:
                   1065:   NEWMP(m); m->dl = d;
                   1066:   arf_div(CO,(Obj)BDY(p1)->c,(Obj)BDY(p2)->c,&a); arf_chsgn(a,&b);
                   1067:   C(m) = b;
                   1068:   NEXT(m) = 0; MKDP(n,m,s); s->sugar = d->td;
                   1069:
                   1070:   mulobjdpm(CO,(Obj)s,p2,&t); adddpm(CO,p1,t,rest);
                   1071: }
                   1072:
                   1073:
                   1074: void dp_red_mod(DP p0,DP p1,DP p2,int mod,DP *head,DP *rest,P *dnp)
                   1075: {
                   1076:   int i,n;
                   1077:   DL d1,d2,d;
                   1078:   MP m;
                   1079:   DP t,s,r,h;
                   1080:   P c1,c2,g,u;
                   1081:
                   1082:   n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
                   1083:   NEWDL(d,n); d->td = d1->td - d2->td;
                   1084:   for ( i = 0; i < n; i++ )
                   1085:     d->d[i] = d1->d[i]-d2->d[i];
                   1086:   c1 = (P)BDY(p1)->c; c2 = (P)BDY(p2)->c;
                   1087:   gcdprsmp(CO,mod,c1,c2,&g);
                   1088:   divsmp(CO,mod,c1,g,&u); c1 = u; divsmp(CO,mod,c2,g,&u); c2 = u;
                   1089:   if ( NUM(c2) ) {
                   1090:     divsmp(CO,mod,c1,c2,&u); c1 = u; c2 = (P)ONEM;
                   1091:   }
                   1092:   NEWMP(m); m->dl = d; chsgnmp(mod,(P)c1,(P *)&m->c); NEXT(m) = 0;
                   1093:   MKDP(n,m,s); s->sugar = d->td; mulmd(CO,mod,s,p2,&t);
                   1094:   if ( NUM(c2) ) {
                   1095:     addmd(CO,mod,p1,t,&r); h = p0;
                   1096:   } else {
                   1097:     mulmdc(CO,mod,p1,c2,&s); addmd(CO,mod,s,t,&r); mulmdc(CO,mod,p0,c2,&h);
                   1098:   }
                   1099:   *head = h; *rest = r; *dnp = c2;
                   1100: }
                   1101:
                   1102: struct oEGT eg_red_mod;
                   1103:
                   1104: void _dp_red_mod_destructive(DP p1,DP p2,int mod,DP *rp)
                   1105: {
                   1106:   int i,n;
                   1107:   DL d1,d2,d;
                   1108:   MP m;
                   1109:   DP t,s;
                   1110:   int c,c1,c2;
                   1111:   extern int do_weyl;
                   1112:
                   1113:   n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
                   1114:   _NEWDL(d,n); d->td = d1->td - d2->td;
                   1115:   for ( i = 0; i < n; i++ )
                   1116:     d->d[i] = d1->d[i]-d2->d[i];
                   1117:   c = invm(ITOS(BDY(p2)->c),mod);
                   1118:   c2 = ITOS(BDY(p1)->c);
                   1119:   DMAR(c,c2,0,mod,c1);
                   1120:   _NEWMP(m); m->dl = d; m->c = (Obj)STOI(mod-c1); NEXT(m) = 0;
                   1121: #if 0
                   1122:   _MKDP(n,m,s); s->sugar = d->td;
                   1123:   _mulmd_dup(mod,s,p2,&t); _free_dp(s);
                   1124: #else
                   1125:   if ( do_weyl ) {
                   1126:     _MKDP(n,m,s); s->sugar = d->td;
                   1127:     _mulmd_dup(mod,s,p2,&t); _free_dp(s);
                   1128:   } else {
                   1129:     _mulmdm_dup(mod,p2,m,&t); _FREEMP(m);
                   1130:   }
                   1131: #endif
                   1132: /* get_eg(&t0); */
                   1133:   _addmd_destructive(mod,p1,t,rp);
                   1134: /* get_eg(&t1); add_eg(&eg_red_mod,&t0,&t1); */
                   1135: }
                   1136:
                   1137: /*
                   1138:  * normal form computation
                   1139:  *
                   1140:  */
                   1141:
                   1142: void dp_true_nf(NODE b,DP g,DP *ps,int full,DP *rp,P *dnp)
                   1143: {
                   1144:   DP u,p,d,s,t,dmy;
                   1145:   NODE l;
                   1146:   MP m,mr;
                   1147:   int i,n;
                   1148:   int *wb;
                   1149:   int sugar,psugar;
                   1150:   P dn,tdn,tdn1;
                   1151:
                   1152:   dn = (P)ONE;
                   1153:   if ( !g ) {
                   1154:     *rp = 0; *dnp = dn; return;
                   1155:   }
                   1156:   for ( n = 0, l = b; l; l = NEXT(l), n++ );
                   1157:   wb = (int *)ALLOCA(n*sizeof(int));
                   1158:   for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1.2       noro     1159:     wb[i] = ZTOS((Q)BDY(l));
1.1       noro     1160:   sugar = g->sugar;
                   1161:   for ( d = 0; g; ) {
                   1162:     for ( u = 0, i = 0; i < n; i++ ) {
                   1163:       if ( dp_redble(g,p = ps[wb[i]]) ) {
                   1164:         dp_red(d,g,p,&t,&u,&tdn,&dmy);
                   1165:         psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
                   1166:         sugar = MAX(sugar,psugar);
                   1167:         if ( !u ) {
                   1168:           if ( d )
                   1169:             d->sugar = sugar;
                   1170:           *rp = d; *dnp = dn; return;
                   1171:         } else {
                   1172:           d = t;
                   1173:           mulp(CO,dn,tdn,&tdn1); dn = tdn1;
                   1174:         }
                   1175:         break;
                   1176:       }
                   1177:     }
                   1178:     if ( u )
                   1179:       g = u;
                   1180:     else if ( !full ) {
                   1181:       if ( g ) {
                   1182:         MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
                   1183:       }
                   1184:       *rp = g; *dnp = dn; return;
                   1185:     } else {
                   1186:       m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
                   1187:       NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
                   1188:       addd(CO,d,t,&s); d = s;
                   1189:       dp_rest(g,&t); g = t;
                   1190:     }
                   1191:   }
                   1192:   if ( d )
                   1193:     d->sugar = sugar;
                   1194:   *rp = d; *dnp = dn;
                   1195: }
                   1196:
                   1197: void dp_removecont2(DP p1,DP p2,DP *r1p,DP *r2p,Z *contp)
                   1198: {
                   1199:   struct oVECT v;
                   1200:   int i,n1,n2,n;
                   1201:   MP m,m0,t;
                   1202:   Z *w;
                   1203:   Z h;
                   1204:
                   1205:   if ( p1 ) {
                   1206:     for ( i = 0, m = BDY(p1); m; m = NEXT(m), i++ );
                   1207:     n1 = i;
                   1208:   } else
                   1209:     n1 = 0;
                   1210:   if ( p2 ) {
                   1211:     for ( i = 0, m = BDY(p2); m; m = NEXT(m), i++ );
                   1212:     n2 = i;
                   1213:   } else
                   1214:     n2 = 0;
                   1215:   n = n1+n2;
                   1216:   if ( !n ) {
                   1217:     *r1p = 0; *r2p = 0; *contp = ONE; return;
                   1218:   }
                   1219:   w = (Z *)ALLOCA(n*sizeof(Q));
                   1220:   v.len = n;
                   1221:   v.body = (pointer *)w;
                   1222:   i = 0;
                   1223:   if ( p1 )
                   1224:     for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = (Z)m->c;
                   1225:   if ( p2 )
                   1226:     for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = (Z)m->c;
1.2       noro     1227:   h = w[0]; removecont_array((P *)w,n,1); divsz(h,w[0],contp);
1.1       noro     1228:   i = 0;
                   1229:   if ( p1 ) {
                   1230:     for ( m0 = 0, t = BDY(p1); i < n1; i++, t = NEXT(t) ) {
                   1231:       NEXTMP(m0,m); m->c = (Obj)w[i]; m->dl = t->dl;
                   1232:     }
                   1233:     NEXT(m) = 0;
                   1234:     MKDP(p1->nv,m0,*r1p); (*r1p)->sugar = p1->sugar;
                   1235:   } else
                   1236:     *r1p = 0;
                   1237:   if ( p2 ) {
                   1238:     for ( m0 = 0, t = BDY(p2); i < n; i++, t = NEXT(t) ) {
                   1239:       NEXTMP(m0,m); m->c = (Obj)w[i]; m->dl = t->dl;
                   1240:     }
                   1241:     NEXT(m) = 0;
                   1242:     MKDP(p2->nv,m0,*r2p); (*r2p)->sugar = p2->sugar;
                   1243:   } else
                   1244:     *r2p = 0;
                   1245: }
                   1246:
1.8       noro     1247: void dpm_removecont2(DPM p1,DPM p2,DPM *r1p,DPM *r2p,Z *contp)
                   1248: {
                   1249:   struct oVECT v;
                   1250:   int i,n1,n2,n;
                   1251:   DMM m,m0,t;
                   1252:   Z *w;
                   1253:   Z h;
                   1254:
                   1255:   if ( p1 ) {
                   1256:     for ( i = 0, m = BDY(p1); m; m = NEXT(m), i++ );
                   1257:     n1 = i;
                   1258:   } else
                   1259:     n1 = 0;
                   1260:   if ( p2 ) {
                   1261:     for ( i = 0, m = BDY(p2); m; m = NEXT(m), i++ );
                   1262:     n2 = i;
                   1263:   } else
                   1264:     n2 = 0;
                   1265:   n = n1+n2;
                   1266:   if ( !n ) {
                   1267:     *r1p = 0; *r2p = 0; *contp = ONE; return;
                   1268:   }
                   1269:   w = (Z *)ALLOCA(n*sizeof(Q));
                   1270:   v.len = n;
                   1271:   v.body = (pointer *)w;
                   1272:   i = 0;
                   1273:   if ( p1 )
                   1274:     for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = (Z)m->c;
                   1275:   if ( p2 )
                   1276:     for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = (Z)m->c;
                   1277:   h = w[0]; removecont_array((P *)w,n,1); divsz(h,w[0],contp);
                   1278:   i = 0;
                   1279:   if ( p1 ) {
                   1280:     for ( m0 = 0, t = BDY(p1); i < n1; i++, t = NEXT(t) ) {
                   1281:       NEXTDMM(m0,m); m->c = (Obj)w[i]; m->dl = t->dl; m->pos = t->pos;
                   1282:     }
                   1283:     NEXT(m) = 0;
                   1284:     MKDPM(p1->nv,m0,*r1p); (*r1p)->sugar = p1->sugar;
                   1285:   } else
                   1286:     *r1p = 0;
                   1287:   if ( p2 ) {
                   1288:     for ( m0 = 0, t = BDY(p2); i < n; i++, t = NEXT(t) ) {
                   1289:       NEXTDMM(m0,m); m->c = (Obj)w[i]; m->dl = t->dl; m->pos = t->pos;
                   1290:     }
                   1291:     NEXT(m) = 0;
                   1292:     MKDPM(p2->nv,m0,*r2p); (*r2p)->sugar = p2->sugar;
                   1293:   } else
                   1294:     *r2p = 0;
                   1295: }
                   1296:
1.1       noro     1297: /* true nf by a marked GB */
                   1298:
                   1299: void dp_true_nf_marked(NODE b,DP g,DP *ps,DP *hps,DP *rp,P *nmp,P *dnp)
                   1300: {
                   1301:   DP u,p,d,s,t,dmy,hp;
                   1302:   NODE l;
                   1303:   MP m,mr;
                   1304:   int i,n,hmag;
                   1305:   int *wb;
                   1306:   int sugar,psugar,multiple;
                   1307:   P nm,tnm1,dn,tdn,tdn1;
                   1308:   Z cont;
                   1309:
                   1310:   multiple = 0;
                   1311:   hmag = multiple*HMAG(g);
                   1312:   nm = (P)ONE;
                   1313:   dn = (P)ONE;
                   1314:   if ( !g ) {
                   1315:     *rp = 0; *dnp = dn; return;
                   1316:   }
                   1317:   for ( n = 0, l = b; l; l = NEXT(l), n++ );
                   1318:   wb = (int *)ALLOCA(n*sizeof(int));
                   1319:   for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1.2       noro     1320:     wb[i] = ZTOS((Z)BDY(l));
1.1       noro     1321:   sugar = g->sugar;
                   1322:   for ( d = 0; g; ) {
                   1323:     for ( u = 0, i = 0; i < n; i++ ) {
                   1324:       if ( dp_redble(g,hp = hps[wb[i]]) ) {
                   1325:         p = ps[wb[i]];
                   1326:         dp_red_marked(d,g,p,hp,&t,&u,&tdn,&dmy);
                   1327:         psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
                   1328:         sugar = MAX(sugar,psugar);
                   1329:         if ( !u ) {
                   1330:           goto last;
                   1331:         } else {
                   1332:           d = t;
                   1333:           mulp(CO,dn,tdn,&tdn1); dn = tdn1;
                   1334:         }
                   1335:         break;
                   1336:       }
                   1337:     }
                   1338:     if ( u ) {
                   1339:       g = u;
                   1340:       if ( multiple && ((d && HMAG(d)>hmag) || (HMAG(g)>hmag)) ) {
                   1341:         dp_removecont2(d,g,&t,&u,&cont); d = t; g = u;
                   1342:         mulp(CO,nm,(P)cont,&tnm1); nm = tnm1;
                   1343:         if ( d )
                   1344:           hmag = multiple*HMAG(d);
                   1345:         else
                   1346:           hmag = multiple*HMAG(g);
                   1347:       }
                   1348:     } else {
                   1349:       m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
                   1350:       NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
                   1351:       addd(CO,d,t,&s); d = s;
                   1352:       dp_rest(g,&t); g = t;
                   1353:     }
                   1354:   }
                   1355: last:
                   1356:   if ( d ) {
                   1357:     dp_removecont2(d,0,&t,&u,&cont); d = t;
                   1358:     mulp(CO,nm,(P)cont,&tnm1); nm = tnm1;
                   1359:     d->sugar = sugar;
                   1360:   }
                   1361:   *rp = d; *nmp = nm; *dnp = dn;
                   1362: }
                   1363:
                   1364: void dp_true_nf_marked_mod(NODE b,DP g,DP *ps,DP *hps,int mod,DP *rp,P *dnp)
                   1365: {
                   1366:   DP hp,u,p,d,s,t,dmy;
                   1367:   NODE l;
                   1368:   MP m,mr;
                   1369:   int i,n;
                   1370:   int *wb;
                   1371:   int sugar,psugar;
                   1372:   P dn,tdn,tdn1;
                   1373:
                   1374:   dn = (P)ONEM;
                   1375:   if ( !g ) {
                   1376:     *rp = 0; *dnp = dn; return;
                   1377:   }
1.3       noro     1378:   for ( n = 0, l = b; l; l = NEXT(l), n++ )
                   1379:     ;
                   1380:   wb = (int *)ALLOCA(n*sizeof(int));
1.1       noro     1381:   for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1.2       noro     1382:     wb[i] = ZTOS((Q)BDY(l));
1.1       noro     1383:   sugar = g->sugar;
                   1384:   for ( d = 0; g; ) {
                   1385:     for ( u = 0, i = 0; i < n; i++ ) {
                   1386:       if ( dp_redble(g,hp = hps[wb[i]]) ) {
                   1387:         p = ps[wb[i]];
                   1388:         dp_red_marked_mod(d,g,p,hp,mod,&t,&u,&tdn,&dmy);
                   1389:         psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
                   1390:         sugar = MAX(sugar,psugar);
                   1391:         if ( !u ) {
                   1392:           if ( d )
                   1393:             d->sugar = sugar;
                   1394:           *rp = d; *dnp = dn; return;
                   1395:         } else {
                   1396:           d = t;
                   1397:           mulmp(CO,mod,dn,tdn,&tdn1); dn = tdn1;
                   1398:         }
                   1399:         break;
                   1400:       }
                   1401:     }
                   1402:     if ( u )
                   1403:       g = u;
                   1404:     else {
                   1405:       m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
                   1406:       NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
                   1407:       addmd(CO,mod,d,t,&s); d = s;
                   1408:       dp_rest(g,&t); g = t;
                   1409:     }
                   1410:   }
                   1411:   if ( d )
                   1412:     d->sugar = sugar;
                   1413:   *rp = d; *dnp = dn;
                   1414: }
                   1415:
                   1416: /* true nf by a marked GB and collect quotients */
                   1417:
                   1418: DP *dp_true_nf_and_quotient_marked (NODE b,DP g,DP *ps,DP *hps,DP *rp,P *dnp)
                   1419: {
                   1420:   DP u,p,d,s,t,dmy,hp,mult;
                   1421:   DP *q;
                   1422:   NODE l;
                   1423:   MP m,mr;
                   1424:   int i,n,j;
                   1425:   int *wb;
                   1426:   int sugar,psugar,multiple;
                   1427:   P nm,tnm1,dn,tdn,tdn1;
                   1428:   Q cont;
                   1429:
                   1430:   dn = (P)ONE;
                   1431:   if ( !g ) {
                   1432:     *rp = 0; *dnp = dn; return 0;
                   1433:   }
                   1434:   for ( n = 0, l = b; l; l = NEXT(l), n++ );
                   1435:   wb = (int *)ALLOCA(n*sizeof(int));
                   1436:   for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1.2       noro     1437:     wb[i] = ZTOS((Q)BDY(l));
1.1       noro     1438:   q = (DP *)MALLOC(n*sizeof(DP));
                   1439:   for ( i = 0; i < n; i++ ) q[i] = 0;
                   1440:   sugar = g->sugar;
                   1441:   for ( d = 0; g; ) {
                   1442:     for ( u = 0, i = 0; i < n; i++ ) {
                   1443:       if ( dp_redble(g,hp = hps[wb[i]]) ) {
                   1444:         p = ps[wb[i]];
                   1445:         dp_red_marked(d,g,p,hp,&t,&u,&tdn,&mult);
                   1446:         psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
                   1447:         sugar = MAX(sugar,psugar);
                   1448:         for ( j = 0; j < n; j++ ) {
                   1449:           muldc(CO,q[j],(Obj)tdn,&dmy); q[j] = dmy;
                   1450:         }
                   1451:         addd(CO,q[wb[i]],mult,&dmy); q[wb[i]] = dmy;
                   1452:         mulp(CO,dn,tdn,&tdn1); dn = tdn1;
                   1453:         d = t;
                   1454:         if ( !u ) goto last;
                   1455:         break;
                   1456:       }
                   1457:     }
                   1458:     if ( u ) {
                   1459:       g = u;
                   1460:     } else {
                   1461:       m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
                   1462:       NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
                   1463:       addd(CO,d,t,&s); d = s;
                   1464:       dp_rest(g,&t); g = t;
                   1465:     }
                   1466:   }
                   1467: last:
                   1468:   if ( d ) d->sugar = sugar;
                   1469:   *rp = d; *dnp = dn;
                   1470:   return q;
                   1471: }
                   1472:
1.5       noro     1473: struct oEGT egred;
                   1474:
                   1475: void mulcmp(Obj c,MP m);
                   1476: void mulcdmm(Obj c,DMM m);
                   1477:
                   1478: DP appendd(DP d,DP m)
                   1479: {
                   1480:   MP t;
                   1481:
                   1482:   if ( !d ) return m;
                   1483:   for ( t = BDY(d); NEXT(t); t = NEXT(t) );
                   1484:   NEXT(t) = BDY(m);
                   1485:   return d;
                   1486: }
                   1487:
                   1488: DPM appenddpm(DPM d,DPM m)
                   1489: {
                   1490:   DMM t;
                   1491:
                   1492:   if ( !d ) return m;
                   1493:   for ( t = BDY(d); NEXT(t); t = NEXT(t) );
                   1494:   NEXT(t) = BDY(m);
                   1495:   return d;
                   1496: }
1.4       noro     1497:
1.3       noro     1498: DP *dpm_nf_and_quotient(NODE b,DPM g,VECT psv,DPM *rp,P *dnp)
                   1499: {
1.5       noro     1500:   DPM u,p,s,t,d;
                   1501:   DP dmy,mult,zzz;
1.3       noro     1502:   DPM *ps;
                   1503:   DP *q;
                   1504:   NODE l;
                   1505:   DMM m,mr;
1.5       noro     1506:   MP mp;
                   1507:   int i,n,j,len,nv;
1.3       noro     1508:   int *wb;
                   1509:   int sugar,psugar,multiple;
                   1510:   P nm,tnm1,dn,tdn,tdn1;
                   1511:   Q cont;
1.4       noro     1512:   struct oEGT eg0,eg1;
1.3       noro     1513:
                   1514:   dn = (P)ONE;
                   1515:   if ( !g ) {
                   1516:     *rp = 0; *dnp = dn; return 0;
                   1517:   }
1.5       noro     1518:   nv = NV(g);
1.3       noro     1519:   ps = (DPM *)BDY(psv);
                   1520:   len = psv->len;
                   1521:   if ( b ) {
                   1522:     for ( n = 0, l = b; l; l = NEXT(l), n++ )
                   1523:       ;
                   1524:     wb = (int *)ALLOCA(n*sizeof(int));
                   1525:     for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
                   1526:       wb[i] = ZTOS((Q)BDY(l));
                   1527:   } else {
                   1528:     wb = (int *)ALLOCA(len*sizeof(int));
                   1529:     for ( i = j = 0; i < len; i++ )
                   1530:       if ( ps[i] ) wb[j++] = i;
                   1531:     n = j;
                   1532:   }
                   1533:   q = (DP *)MALLOC(len*sizeof(DP));
                   1534:   for ( i = 0; i < len; i++ ) q[i] = 0;
                   1535:   sugar = g->sugar;
                   1536:   for ( d = 0; g; ) {
                   1537:     for ( u = 0, i = 0; i < n; i++ ) {
                   1538:       if ( dpm_redble(g,p = ps[wb[i]]) ) {
1.5       noro     1539: // get_eg(&eg0);
                   1540:         dpm_red2(g,p,&u,&tdn,&mult);
                   1541: // get_eg(&eg1); add_eg(&egred,&eg0,&eg1);
1.3       noro     1542:         psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
                   1543:         sugar = MAX(sugar,psugar);
                   1544:         for ( j = 0; j < len; j++ ) {
1.5       noro     1545:           if ( q[j] ) { mulcmp((Obj)tdn,BDY(q[j])); }
1.3       noro     1546:         }
1.5       noro     1547:         q[wb[i]] = appendd(q[wb[i]],mult);
1.3       noro     1548:         mulp(CO,dn,tdn,&tdn1); dn = tdn1;
1.5       noro     1549:         if ( d ) mulcdmm((Obj)tdn,BDY(d));
1.3       noro     1550:         if ( !u ) goto last;
                   1551:         break;
                   1552:       }
                   1553:     }
                   1554:     if ( u ) {
                   1555:       g = u;
                   1556:     } else {
                   1557:       m = BDY(g); NEWDMM(mr); mr->dl = m->dl; mr->c = m->c; mr->pos = m->pos;
                   1558:       NEXT(mr) = 0; MKDPM(g->nv,mr,t); t->sugar = mr->dl->td;
1.5       noro     1559:       d = appenddpm(d,t);
1.3       noro     1560:       dpm_rest(g,&t); g = t;
                   1561:     }
                   1562:   }
                   1563: last:
                   1564:   if ( d ) d->sugar = sugar;
                   1565:   *rp = d; *dnp = dn;
                   1566:   return q;
                   1567: }
                   1568:
1.9       noro     1569: DPM dpm_nf_and_quotient2(NODE b,DPM g,VECT psv,DPM *rp,P *dnp)
                   1570: {
                   1571:   DPM u,p,s,t,d,q;
                   1572:   DP dmy,mult,zzz;
                   1573:   DPM *ps;
                   1574:   NODE l;
                   1575:   DMM mr0,mq0,mr,mq,m;
                   1576:   MP mp;
                   1577:   int i,n,j,len,nv;
                   1578:   int *wb;
                   1579:   int sugar,psugar,multiple;
                   1580:   P nm,tnm1,dn,tdn,tdn1;
                   1581:   Q cont;
                   1582:   Obj c1;
                   1583:   struct oEGT eg0,eg1;
                   1584:
                   1585:   dn = (P)ONE;
                   1586:   if ( !g ) {
                   1587:     *rp = 0; *dnp = dn; return 0;
                   1588:   }
                   1589:   nv = NV(g);
                   1590:   ps = (DPM *)BDY(psv);
                   1591:   len = psv->len;
                   1592:   if ( b ) {
                   1593:     for ( n = 0, l = b; l; l = NEXT(l), n++ )
                   1594:       ;
                   1595:     wb = (int *)ALLOCA(n*sizeof(int));
                   1596:     for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
                   1597:       wb[i] = ZTOS((Q)BDY(l));
                   1598:   } else {
                   1599:     wb = (int *)ALLOCA(len*sizeof(int));
                   1600:     for ( i = j = 0; i < len; i++ )
                   1601:       if ( ps[i] ) wb[j++] = i;
                   1602:     n = j;
                   1603:   }
                   1604:   sugar = g->sugar;
                   1605:   mq0 = 0;
                   1606:   mr0 = 0;
                   1607:   for ( ; g; ) {
                   1608:     for ( u = 0, i = 0; i < n; i++ ) {
                   1609:       if ( dpm_redble(g,p = ps[wb[i]]) ) {
                   1610:         dpm_red2(g,p,&u,&tdn,&mult);
                   1611:         psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
                   1612:         sugar = MAX(sugar,psugar);
                   1613:         for ( m = mq0; m; m = NEXT(m) ) {
                   1614:           arf_mul(CO,(Obj)tdn,m->c,&c1); m->c = c1;
                   1615:         }
                   1616:         for ( m = mr0; m; m = NEXT(m) ) {
                   1617:           arf_mul(CO,(Obj)tdn,m->c,&c1); m->c = c1;
                   1618:         }
                   1619:         NEXTDMM(mq0,mq);
                   1620:         mq->c = BDY(mult)->c; mq->dl = BDY(mult)->dl; mq->pos = wb[i]+1;
                   1621:         mulp(CO,dn,tdn,&tdn1); dn = tdn1;
                   1622:         if ( !u ) goto last;
                   1623:         break;
                   1624:       }
                   1625:     }
                   1626:     if ( u ) {
                   1627:       g = u;
                   1628:     } else {
                   1629:       m = BDY(g);
                   1630:       NEXTDMM(mr0,mr);
                   1631:       mr->dl = m->dl; mr->c = m->c; mr->pos = m->pos;
                   1632:       dpm_rest(g,&t); g = t;
                   1633:     }
                   1634:   }
                   1635: last:
                   1636:   if ( mr0 ) {
                   1637:     NEXT(mr) = 0;
                   1638:     MKDPM(nv,mr0,d); d->sugar = sugar;
                   1639:   } else
                   1640:     d = 0;
                   1641:   if ( mq0 ) {
                   1642:     NEXT(mq) = 0;
                   1643:     MKDPM(nv,mq0,q); q->sugar = sugar;
                   1644:   } else
                   1645:     q = 0;
                   1646:   *rp = d; *dnp = dn;
                   1647:   return q;
                   1648: }
                   1649:
                   1650: DPM dpm_nf_and_quotient3(DPM g,VECT psv,DPM *rp,P *dnp)
                   1651: {
                   1652:   DPM u,p,s,t,d,q;
                   1653:   DP dmy,mult,zzz;
                   1654:   DPM *ps;
                   1655:   NODE2 nd;
                   1656:   DMM mr0,mq0,mr,mq,m;
                   1657:   MP mp;
                   1658:   int i,n,j,len,nv,pos,max;
                   1659:   int *wb;
                   1660:   int sugar,psugar,multiple;
                   1661:   P nm,tnm1,dn,tdn,tdn1;
                   1662:   Q cont;
                   1663:   Obj c1;
                   1664:   struct oEGT eg0,eg1;
                   1665:
                   1666:   dn = (P)ONE;
                   1667:   if ( !g ) {
                   1668:     *rp = 0; *dnp = dn; return 0;
                   1669:   }
                   1670:   nv = NV(g);
                   1671:   sugar = g->sugar;
                   1672:   mq0 = 0;
                   1673:   mr0 = 0;
                   1674:   max = psv->len;
                   1675:   for ( ; g; ) {
                   1676:     pos = BDY(g)->pos;
                   1677:     u = 0;
                   1678:     if ( pos < max ) {
                   1679:       nd = (NODE2)BDY(psv)[pos];
                   1680:       for ( u = 0; nd; nd = NEXT(nd) ) {
                   1681:         if ( dpm_redble(g,p = (DPM)(nd->body1)) ) {
                   1682:           dpm_red2(g,p,&u,&tdn,&mult);
                   1683:           psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
                   1684:           sugar = MAX(sugar,psugar);
                   1685:           if ( !UNIZ(tdn) ) {
                   1686:             for ( m = mq0; m; m = NEXT(m) ) {
                   1687:               arf_mul(CO,(Obj)tdn,m->c,&c1); m->c = c1;
                   1688:             }
                   1689:             for ( m = mr0; m; m = NEXT(m) ) {
                   1690:               arf_mul(CO,(Obj)tdn,m->c,&c1); m->c = c1;
                   1691:             }
                   1692:           }
                   1693:           NEXTDMM(mq0,mq);
                   1694:           mq->c = BDY(mult)->c; mq->dl = BDY(mult)->dl; mq->pos = (long)nd->body2;
                   1695:           mulp(CO,dn,tdn,&tdn1); dn = tdn1;
                   1696:           if ( !u ) goto last;
                   1697:           break;
                   1698:         }
                   1699:       }
                   1700:     }
                   1701:     if ( u ) {
                   1702:       g = u;
                   1703:     } else {
                   1704:       m = BDY(g);
                   1705:       NEXTDMM(mr0,mr);
                   1706:       mr->dl = m->dl; mr->c = m->c; mr->pos = m->pos;
                   1707:       dpm_rest(g,&t); g = t;
                   1708:     }
                   1709:   }
                   1710: last:
                   1711:   if ( mr0 ) {
                   1712:     NEXT(mr) = 0;
                   1713:     MKDPM(nv,mr0,d); d->sugar = sugar;
                   1714:   } else
                   1715:     d = 0;
                   1716:   if ( mq0 ) {
                   1717:     NEXT(mq) = 0;
                   1718:     MKDPM(nv,mq0,q); q->sugar = sugar;
                   1719:   } else
                   1720:     q = 0;
                   1721:   *rp = d; *dnp = dn;
                   1722:   return q;
                   1723: }
                   1724:
                   1725: DPM dpm_nf_and_quotient4(DPM g,DPM *ps,VECT psiv,DPM head,DPM *rp,P *dnp)
                   1726: {
                   1727:   DPM u,p,s,t,d,q;
                   1728:   DP dmy,mult,zzz;
                   1729:   NODE nd;
                   1730:   DMM mr0,mq0,mr,mq,m;
                   1731:   MP mp;
                   1732:   int i,n,j,len,nv,pos,max;
                   1733:   int *wb;
                   1734:   int sugar,psugar,multiple;
                   1735:   P nm,tnm1,dn,tdn,tdn1,c;
                   1736:   Q cont;
                   1737:   Obj c1;
                   1738:   struct oEGT eg0,eg1;
                   1739:
                   1740:   dn = (P)ONE;
                   1741:   if ( !g ) {
                   1742:     *rp = 0; *dnp = dn; return 0;
                   1743:   }
                   1744:   nv = NV(g);
                   1745:   sugar = g->sugar;
                   1746:   mq0 = 0;
                   1747:   if ( head ) {
                   1748:     for ( m = BDY(head); m; m = NEXT(m) ) {
                   1749:       NEXTDMM(mq0,mq);
                   1750:       mq->c = m->c; mq->dl = m->dl; mq->pos = m->pos;
                   1751:     }
                   1752:   }
                   1753:   mr0 = 0;
                   1754:   max = psiv->len;
                   1755:   for ( ; g; ) {
                   1756:     pos = BDY(g)->pos;
                   1757:     u = 0;
                   1758:     if ( pos < max ) {
                   1759:       nd = (NODE)BDY(psiv)[pos];
                   1760:       for ( u = 0; nd; nd = NEXT(nd) ) {
                   1761:         if ( dpm_redble(g,p = ps[(long)(BDY(nd))-1]) ) {
                   1762:           dpm_red2(g,p,&u,&tdn,&mult);
                   1763:           psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
                   1764:           sugar = MAX(sugar,psugar);
                   1765:           if ( !UNIZ(tdn) ) {
                   1766:             for ( m = mq0; m; m = NEXT(m) ) {
                   1767:               arf_mul(CO,(Obj)tdn,m->c,&c1); m->c = c1;
                   1768:             }
                   1769:             for ( m = mr0; m; m = NEXT(m) ) {
                   1770:               arf_mul(CO,(Obj)tdn,m->c,&c1); m->c = c1;
                   1771:             }
                   1772:           }
                   1773:           NEXTDMM(mq0,mq);
                   1774:           mq->c = BDY(mult)->c;
                   1775:           mq->dl = BDY(mult)->dl; mq->pos = (long)BDY(nd);
                   1776:           mulp(CO,dn,tdn,&tdn1); dn = tdn1;
                   1777:           if ( !u ) goto last;
                   1778:           break;
                   1779:         }
                   1780:       }
                   1781:     }
                   1782:     if ( u ) {
                   1783:       g = u;
                   1784:     } else {
                   1785:       m = BDY(g);
                   1786:       NEXTDMM(mr0,mr);
                   1787:       mr->dl = m->dl; mr->c = m->c; mr->pos = m->pos;
                   1788:       dpm_rest(g,&t); g = t;
                   1789:     }
                   1790:   }
                   1791: last:
                   1792:   if ( mr0 ) {
                   1793:     NEXT(mr) = 0;
                   1794:     MKDPM(nv,mr0,d); d->sugar = sugar;
                   1795:   } else
                   1796:     d = 0;
                   1797:   if ( mq0 ) {
                   1798:     NEXT(mq) = 0;
                   1799:     MKDPM(nv,mq0,q); q->sugar = sugar;
                   1800:   } else
                   1801:     q = 0;
                   1802:   *rp = d; *dnp = dn;
                   1803:   return q;
                   1804: }
                   1805:
1.10    ! noro     1806: /* an intermediate version for calling from the user language */
        !          1807:
        !          1808: DPM dpm_sp_nf_asir(VECT psv,int i,int j,DPM *nf)
        !          1809: {
        !          1810:   DPM *ps;
        !          1811:   int n,nv,s1,s2,sugar,max,pos,psugar;
        !          1812:   DPM g,u,p,d,q,t;
        !          1813:   DMM mq0,mq,mr0,mr,m;
        !          1814:   DP mult,t1,t2;
        !          1815:   P dn,tdn,tdn1;
        !          1816:   NODE nd;
        !          1817:   Obj c1;
        !          1818:
        !          1819:   ps = (DPM *)BDY(psv);
        !          1820:   n = psv->len;
        !          1821:   nv = ps[1]->nv;
        !          1822:   dpm_sp(ps[i],ps[j],&g,&t1,&t2);
        !          1823:   mq0 = 0;
        !          1824:   NEXTDMM(mq0,mq); mq->c = BDY(t1)->c; mq->pos = i; mq->dl = BDY(t1)->dl;
        !          1825:   NEXTDMM(mq0,mq); chsgnp((P)BDY(t2)->c,(P *)&mq->c); mq->pos = j; mq->dl = BDY(t2)->dl;
        !          1826:
        !          1827:   if ( !g ) {
        !          1828:     NEXT(mq) = 0;
        !          1829:     MKDPM(nv,mq0,d);
        !          1830:     s1 = BDY(t1)->dl->td + ps[i]->sugar;
        !          1831:     s2 = BDY(t2)->dl->td + ps[j]->sugar;
        !          1832:     d->sugar = MAX(s1,s2);
        !          1833:     *nf = 0;
        !          1834:     return d;
        !          1835:   }
        !          1836:
        !          1837:   dn = (P)ONE;
        !          1838:   sugar = g->sugar;
        !          1839:   mr0 = 0;
        !          1840:   while ( g ) {
        !          1841:     for ( u = 0, i = 1; i < n; i++ ) {
        !          1842:       if ( (p=ps[i])!=0 && dpm_redble(g,p) ) {
        !          1843:         dpm_red2(g,p,&u,&tdn,&mult);
        !          1844:         psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
        !          1845:         sugar = MAX(sugar,psugar);
        !          1846:         if ( !UNIZ(tdn) ) {
        !          1847:           for ( m = mq0; m; m = NEXT(m) ) {
        !          1848:             arf_mul(CO,(Obj)tdn,m->c,&c1); m->c = c1;
        !          1849:           }
        !          1850:           for ( m = mr0; m; m = NEXT(m) ) {
        !          1851:             arf_mul(CO,(Obj)tdn,m->c,&c1); m->c = c1;
        !          1852:           }
        !          1853:         }
        !          1854:         NEXTDMM(mq0,mq);
        !          1855:         chsgnp((P)BDY(mult)->c,(P *)&mq->c);
        !          1856:         mq->dl = BDY(mult)->dl; mq->pos = i;
        !          1857:         mulp(CO,dn,tdn,&tdn1); dn = tdn1;
        !          1858:         if ( !u ) goto last;
        !          1859:         break;
        !          1860:       }
        !          1861:     }
        !          1862:     if ( u ) {
        !          1863:       g = u;
        !          1864:     } else {
        !          1865:       m = BDY(g);
        !          1866:       NEXTDMM(mr0,mr);
        !          1867:       mr->dl = m->dl; mr->c = m->c; mr->pos = m->pos;
        !          1868:       dpm_rest(g,&t); g = t;
        !          1869:     }
        !          1870:   }
        !          1871: last:
        !          1872:   if ( mr0 ) {
        !          1873:     NEXT(mr) = 0; MKDPM(nv,mr0,d); d->sugar = sugar;
        !          1874:   } else
        !          1875:     d = 0;
        !          1876:   NEXT(mq) = 0; MKDPM(nv,mq0,q); q->sugar = sugar;
        !          1877:   *nf = d;
        !          1878:   return q;
        !          1879: }
        !          1880:
1.9       noro     1881: DPM dpm_sp_nf(VECT psv,VECT psiv,int i,int j,DPM *nf)
                   1882: {
                   1883:   DPM *ps;
                   1884:   int n,nv,s1,s2,sugar,max,pos,psugar;
                   1885:   DPM g,u,p,d,q,t;
                   1886:   DMM mq0,mq,mr0,mr,m;
                   1887:   DP mult,t1,t2;
                   1888:   P dn,tdn,tdn1;
                   1889:   NODE nd;
                   1890:   Obj c1;
                   1891:
                   1892:   ps = (DPM *)BDY(psv);
                   1893:   n = psv->len;
                   1894:   nv = ps[1]->nv;
                   1895:   dpm_sp(ps[i],ps[j],&g,&t1,&t2);
                   1896:   mq0 = 0;
                   1897:   NEXTDMM(mq0,mq); mq->c = BDY(t1)->c; mq->pos = i; mq->dl = BDY(t1)->dl;
                   1898:   NEXTDMM(mq0,mq); chsgnp((P)BDY(t2)->c,(P *)&mq->c); mq->pos = j; mq->dl = BDY(t2)->dl;
                   1899:
                   1900:   if ( !g ) {
                   1901:     NEXT(mq) = 0;
                   1902:     MKDPM(nv,mq0,d);
                   1903:     s1 = BDY(t1)->dl->td + ps[i]->sugar;
                   1904:     s2 = BDY(t2)->dl->td + ps[j]->sugar;
                   1905:     d->sugar = MAX(s1,s2);
                   1906:     *nf = 0;
                   1907:     return d;
                   1908:   }
                   1909:
                   1910:   dn = (P)ONE;
                   1911:   sugar = g->sugar;
                   1912:   mr0 = 0;
                   1913:   max = psiv->len;
                   1914:   while ( g ) {
                   1915:     pos = BDY(g)->pos;
                   1916:     u = 0;
                   1917:     if ( pos < max ) {
                   1918:       nd = (NODE)BDY(psiv)[pos];
                   1919:       for ( u = 0; nd; nd = NEXT(nd) ) {
                   1920:         if ( dpm_redble(g,p = ps[(long)(BDY(nd))]) ) {
                   1921:           dpm_red2(g,p,&u,&tdn,&mult);
                   1922:           psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
                   1923:           sugar = MAX(sugar,psugar);
                   1924:           if ( !UNIZ(tdn) ) {
                   1925:             for ( m = mq0; m; m = NEXT(m) ) {
                   1926:               arf_mul(CO,(Obj)tdn,m->c,&c1); m->c = c1;
                   1927:             }
                   1928:             for ( m = mr0; m; m = NEXT(m) ) {
                   1929:               arf_mul(CO,(Obj)tdn,m->c,&c1); m->c = c1;
                   1930:             }
                   1931:           }
                   1932:           NEXTDMM(mq0,mq);
                   1933:           chsgnp((P)BDY(mult)->c,(P *)&mq->c);
                   1934:           mq->dl = BDY(mult)->dl; mq->pos = (long)BDY(nd);
                   1935:           mulp(CO,dn,tdn,&tdn1); dn = tdn1;
                   1936:           if ( !u ) goto last;
                   1937:           break;
                   1938:         }
                   1939:       }
                   1940:     }
                   1941:     if ( u ) {
                   1942:       g = u;
                   1943:     } else {
                   1944:       m = BDY(g);
                   1945:       NEXTDMM(mr0,mr);
                   1946:       mr->dl = m->dl; mr->c = m->c; mr->pos = m->pos;
                   1947:       dpm_rest(g,&t); g = t;
                   1948:     }
                   1949:   }
                   1950: last:
                   1951:   if ( mr0 ) {
                   1952:     NEXT(mr) = 0; MKDPM(nv,mr0,d); d->sugar = sugar;
                   1953:   } else
                   1954:     d = 0;
                   1955:   NEXT(mq) = 0; MKDPM(nv,mq0,q); q->sugar = sugar;
                   1956:   *nf = d;
                   1957:   return q;
                   1958: }
                   1959:
1.1       noro     1960: DP *dp_true_nf_and_quotient_marked_mod(NODE b,DP g,DP *ps,DP *hps,int mod,DP *rp,P *dnp)
                   1961: {
                   1962:   DP u,p,d,s,t,dmy,hp,mult;
                   1963:   DP *q;
                   1964:   NODE l;
                   1965:   MP m,mr;
                   1966:   int i,n,j;
                   1967:   int *wb;
                   1968:   int sugar,psugar;
                   1969:   P dn,tdn,tdn1;
                   1970:
                   1971:   for ( n = 0, l = b; l; l = NEXT(l), n++ );
                   1972:   q = (DP *)MALLOC(n*sizeof(DP));
                   1973:   for ( i = 0; i < n; i++ ) q[i] = 0;
                   1974:   dn = (P)ONEM;
                   1975:   if ( !g ) {
                   1976:     *rp = 0; *dnp = dn; return 0;
                   1977:   }
                   1978:   wb = (int *)ALLOCA(n*sizeof(int));
                   1979:   for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1.2       noro     1980:     wb[i] = ZTOS((Q)BDY(l));
1.1       noro     1981:   sugar = g->sugar;
                   1982:   for ( d = 0; g; ) {
                   1983:     for ( u = 0, i = 0; i < n; i++ ) {
                   1984:       if ( dp_redble(g,hp = hps[wb[i]]) ) {
                   1985:         p = ps[wb[i]];
                   1986:         dp_red_marked_mod(d,g,p,hp,mod,&t,&u,&tdn,&mult);
                   1987:         psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
                   1988:         sugar = MAX(sugar,psugar);
                   1989:         for ( j = 0; j < n; j++ ) {
                   1990:           mulmdc(CO,mod,q[j],(P)tdn,&dmy); q[j] = dmy;
                   1991:         }
                   1992:         addmd(CO,mod,q[wb[i]],mult,&dmy); q[wb[i]] = dmy;
                   1993:         mulmp(CO,mod,dn,tdn,&tdn1); dn = tdn1;
                   1994:         d = t;
                   1995:         if ( !u ) goto last;
                   1996:         break;
                   1997:       }
                   1998:     }
                   1999:     if ( u )
                   2000:       g = u;
                   2001:     else {
                   2002:       m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
                   2003:       NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
                   2004:       addmd(CO,mod,d,t,&s); d = s;
                   2005:       dp_rest(g,&t); g = t;
                   2006:     }
                   2007:   }
                   2008: last:
                   2009:   if ( d )
                   2010:     d->sugar = sugar;
                   2011:   *rp = d; *dnp = dn;
                   2012:   return q;
                   2013: }
                   2014:
                   2015: /* nf computation over Z */
                   2016:
                   2017: void dp_nf_z(NODE b,DP g,DP *ps,int full,int multiple,DP *rp)
                   2018: {
                   2019:   DP u,p,d,s,t,dmy1;
                   2020:   P dmy;
                   2021:   NODE l;
                   2022:   MP m,mr;
                   2023:   int i,n;
                   2024:   int *wb;
                   2025:   int hmag;
                   2026:   int sugar,psugar;
                   2027:
                   2028:   if ( !g ) {
                   2029:     *rp = 0; return;
                   2030:   }
                   2031:   for ( n = 0, l = b; l; l = NEXT(l), n++ );
                   2032:   wb = (int *)ALLOCA(n*sizeof(int));
                   2033:   for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1.2       noro     2034:     wb[i] = ZTOS((Q)BDY(l));
1.1       noro     2035:
                   2036:   hmag = multiple*HMAG(g);
                   2037:   sugar = g->sugar;
                   2038:
                   2039:   for ( d = 0; g; ) {
                   2040:     for ( u = 0, i = 0; i < n; i++ ) {
                   2041:       if ( dp_redble(g,p = ps[wb[i]]) ) {
                   2042:         dp_red(d,g,p,&t,&u,&dmy,&dmy1);
                   2043:         psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
                   2044:         sugar = MAX(sugar,psugar);
                   2045:         if ( !u ) {
                   2046:           if ( d )
                   2047:             d->sugar = sugar;
                   2048:           *rp = d; return;
                   2049:         }
                   2050:         d = t;
                   2051:         break;
                   2052:       }
                   2053:     }
                   2054:     if ( u ) {
                   2055:       g = u;
                   2056:       if ( d ) {
                   2057:         if ( multiple && HMAG(d) > hmag ) {
                   2058:           dp_ptozp2(d,g,&t,&u); d = t; g = u;
                   2059:           hmag = multiple*HMAG(d);
                   2060:         }
                   2061:       } else {
                   2062:         if ( multiple && HMAG(g) > hmag ) {
                   2063:           dp_ptozp(g,&t); g = t;
                   2064:           hmag = multiple*HMAG(g);
                   2065:         }
                   2066:       }
                   2067:     }
                   2068:     else if ( !full ) {
                   2069:       if ( g ) {
                   2070:         MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
                   2071:       }
                   2072:       *rp = g; return;
                   2073:     } else {
                   2074:       m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
                   2075:       NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
                   2076:       addd(CO,d,t,&s); d = s;
                   2077:       dp_rest(g,&t); g = t;
                   2078:
                   2079:     }
                   2080:   }
                   2081:   if ( d )
                   2082:     d->sugar = sugar;
                   2083:   *rp = d;
                   2084: }
                   2085:
1.4       noro     2086: void dpm_nf_z(NODE b,DPM g,VECT psv,int full,int multiple,DPM *rp)
1.1       noro     2087: {
1.4       noro     2088:   DPM *ps;
1.1       noro     2089:   DPM u,p,d,s,t;
                   2090:   DP dmy1;
                   2091:   P dmy;
1.3       noro     2092:   Z cont;
1.1       noro     2093:   NODE l;
                   2094:   DMM m,mr;
                   2095:   int i,n;
                   2096:   int *wb;
                   2097:   int hmag;
                   2098:   int sugar,psugar;
                   2099:
                   2100:   if ( !g ) {
                   2101:     *rp = 0; return;
                   2102:   }
1.4       noro     2103:   if ( b ) {
                   2104:     for ( n = 0, l = b; l; l = NEXT(l), n++ );
                   2105:     wb = (int *)ALLOCA(n*sizeof(int));
                   2106:     for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
                   2107:       wb[i] = ZTOS((Q)BDY(l));
                   2108:     ps = (DPM *)BDY(psv);
                   2109:   } else {
                   2110:     n = psv->len;
                   2111:     wb = (int *)MALLOC(n*sizeof(int));
                   2112:     for ( i = 0; i < n; i++ ) wb[i] = i;
                   2113:     ps = (DPM *)BDY(psv);
                   2114:   }
1.1       noro     2115:
                   2116:   hmag = multiple*HMAG(g);
                   2117:   sugar = g->sugar;
                   2118:
                   2119:   for ( d = 0; g; ) {
                   2120:     for ( u = 0, i = 0; i < n; i++ ) {
1.4       noro     2121:       if ( (p=ps[wb[i]])!=0 && dpm_redble(g,p) ) {
1.5       noro     2122:         dpm_red2(g,p,&u,&dmy,&dmy1);
1.1       noro     2123:         psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
                   2124:         sugar = MAX(sugar,psugar);
1.5       noro     2125:         if ( d ) mulcdmm((Obj)dmy,BDY(d));
1.1       noro     2126:         if ( !u ) {
                   2127:           if ( d )
                   2128:             d->sugar = sugar;
                   2129:           *rp = d; return;
                   2130:         }
                   2131:         break;
                   2132:       }
                   2133:     }
                   2134:     if ( u ) {
                   2135:       g = u;
                   2136:       if ( d ) {
                   2137:         if ( multiple && HMAG(d) > hmag ) {
                   2138:           dpm_ptozp2(d,g,&t,&u); d = t; g = u;
                   2139:           hmag = multiple*HMAG(d);
                   2140:         }
                   2141:       } else {
                   2142:         if ( multiple && HMAG(g) > hmag ) {
1.3       noro     2143:           dpm_ptozp(g,&cont,&t); g = t;
1.1       noro     2144:           hmag = multiple*HMAG(g);
                   2145:         }
                   2146:       }
                   2147:     }
                   2148:     else if ( !full ) {
                   2149:       if ( g ) {
                   2150:         MKDPM(g->nv,BDY(g),t); t->sugar = sugar; g = t;
                   2151:       }
                   2152:       *rp = g; return;
                   2153:     } else {
                   2154:       m = BDY(g); NEWDMM(mr); mr->dl = m->dl; mr->c = m->c; mr->pos = m->pos;
                   2155:       NEXT(mr) = 0; MKDPM(g->nv,mr,t); t->sugar = mr->dl->td;
1.5       noro     2156:       d = appenddpm(d,t);
1.1       noro     2157:       dpm_rest(g,&t); g = t;
                   2158:     }
                   2159:   }
                   2160:   if ( d )
                   2161:     d->sugar = sugar;
                   2162:   *rp = d;
                   2163: }
                   2164:
1.3       noro     2165: void dpm_shift(DPM p,int s,DPM *r)
                   2166: {
                   2167:   DMM m,mr0,mr;
                   2168:   DPM t;
                   2169:
                   2170:   if ( !p ) *r = 0;
                   2171:   else {
                   2172:     for ( m = BDY(p), mr0 = 0; m; m = NEXT(m) ) {
                   2173:       NEXTDMM(mr0,mr);
                   2174:       mr->dl = m->dl; mr->c = m->c; mr->pos = m->pos-s;
                   2175:       if ( mr->pos <= 0 )
                   2176:         error("dpm_shift : too large shift value");
                   2177:     }
                   2178:     NEXT(mr) = 0;
                   2179:     MKDPM(p->nv,mr0,t); t->sugar = p->sugar;
                   2180:     *r = t;
                   2181:   }
                   2182: }
                   2183:
                   2184: // up=sum{c*<<...:i>>|i<=s}, lo=sum{c*<<...:i>>|i>s}
                   2185:
                   2186: void dpm_split(DPM p,int s,DPM *up,DPM *lo)
                   2187: {
                   2188:   DMM m,mu0,mu,ml0,ml;
                   2189:   DPM t;
                   2190:
                   2191:   if ( !p ) {
                   2192:     *up = 0; *lo = 0;
                   2193:   } else {
                   2194:     for ( m = BDY(p), mu0 = ml0 = 0; m; m = NEXT(m) ) {
                   2195:       if ( m->pos <= s ) {
                   2196:         NEXTDMM(mu0,mu);
                   2197:         mu->dl = m->dl; mu->c = m->c; mu->pos = m->pos;
                   2198:       } else {
                   2199:         NEXTDMM(ml0,ml);
                   2200:         ml->dl = m->dl; ml->c = m->c; ml->pos = m->pos;
                   2201:       }
                   2202:     }
                   2203:     if ( mu0 ) {
                   2204:       NEXT(mu) = 0; MKDPM(p->nv,mu0,t); t->sugar = p->sugar;
                   2205:       *up = t;
                   2206:     } else
                   2207:       *up = 0;
                   2208:     if ( ml0 ) {
                   2209:       NEXT(ml) = 0; MKDPM(p->nv,ml0,t); t->sugar = p->sugar;
                   2210:       *lo = t;
                   2211:     } else
                   2212:       *lo = 0;
                   2213:   }
                   2214: }
                   2215:
1.1       noro     2216: /* nf computation over a field */
                   2217:
                   2218: void dp_nf_f(NODE b,DP g,DP *ps,int full,DP *rp)
                   2219: {
                   2220:   DP u,p,d,s,t;
                   2221:   NODE l;
                   2222:   MP m,mr;
                   2223:   int i,n;
                   2224:   int *wb;
                   2225:   int sugar,psugar;
                   2226:
                   2227:   if ( !g ) {
                   2228:     *rp = 0; return;
                   2229:   }
                   2230:   for ( n = 0, l = b; l; l = NEXT(l), n++ );
                   2231:   wb = (int *)ALLOCA(n*sizeof(int));
                   2232:   for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1.2       noro     2233:     wb[i] = ZTOS((Q)BDY(l));
1.1       noro     2234:
                   2235:   sugar = g->sugar;
                   2236:   for ( d = 0; g; ) {
                   2237:     for ( u = 0, i = 0; i < n; i++ ) {
                   2238:       if ( dp_redble(g,p = ps[wb[i]]) ) {
                   2239:         dp_red_f(g,p,&u);
                   2240:         psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
                   2241:         sugar = MAX(sugar,psugar);
                   2242:         if ( !u ) {
                   2243:           if ( d )
                   2244:             d->sugar = sugar;
                   2245:           *rp = d; return;
                   2246:         }
                   2247:         break;
                   2248:       }
                   2249:     }
                   2250:     if ( u )
                   2251:       g = u;
                   2252:     else if ( !full ) {
                   2253:       if ( g ) {
                   2254:         MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
                   2255:       }
                   2256:       *rp = g; return;
                   2257:     } else {
                   2258:       m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
                   2259:       NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
                   2260:       addd(CO,d,t,&s); d = s;
                   2261:       dp_rest(g,&t); g = t;
                   2262:     }
                   2263:   }
                   2264:   if ( d )
                   2265:     d->sugar = sugar;
                   2266:   *rp = d;
                   2267: }
                   2268:
1.4       noro     2269: void dpm_nf_f(NODE b,DPM g,VECT psv,int full,DPM *rp)
1.1       noro     2270: {
1.4       noro     2271:   DPM *ps;
1.1       noro     2272:   DPM u,p,d,s,t;
                   2273:   NODE l;
                   2274:   DMM m,mr;
                   2275:   int i,n;
                   2276:   int *wb;
                   2277:   int sugar,psugar;
                   2278:
                   2279:   if ( !g ) {
                   2280:     *rp = 0; return;
                   2281:   }
1.4       noro     2282:   if ( b ) {
                   2283:     for ( n = 0, l = b; l; l = NEXT(l), n++ );
                   2284:     wb = (int *)ALLOCA(n*sizeof(int));
                   2285:     for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
                   2286:       wb[i] = ZTOS((Q)BDY(l));
                   2287:     ps = (DPM *)BDY(psv);
                   2288:   } else {
                   2289:     n = psv->len;
                   2290:     wb = (int *)MALLOC(n*sizeof(int));
                   2291:     for ( i = 0; i < n; i++ ) wb[i] = i;
                   2292:     ps = (DPM *)BDY(psv);
                   2293:   }
1.1       noro     2294:
                   2295:   sugar = g->sugar;
                   2296:   for ( d = 0; g; ) {
                   2297:     for ( u = 0, i = 0; i < n; i++ ) {
1.4       noro     2298:       if ( ( (p=ps[wb[i]]) != 0 ) && dpm_redble(g,p) ) {
1.1       noro     2299:         dpm_red_f(g,p,&u);
                   2300:         psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
                   2301:         sugar = MAX(sugar,psugar);
                   2302:         if ( !u ) {
                   2303:           if ( d )
                   2304:             d->sugar = sugar;
                   2305:           *rp = d; return;
                   2306:         }
                   2307:         break;
                   2308:       }
                   2309:     }
                   2310:     if ( u )
                   2311:       g = u;
                   2312:     else if ( !full ) {
                   2313:       if ( g ) {
                   2314:         MKDPM(g->nv,BDY(g),t); t->sugar = sugar; g = t;
                   2315:       }
                   2316:       *rp = g; return;
                   2317:     } else {
                   2318:       m = BDY(g); NEWDMM(mr); mr->dl = m->dl; mr->c = m->c; mr->pos = m->pos;
                   2319:       NEXT(mr) = 0; MKDPM(g->nv,mr,t); t->sugar = mr->dl->td;
                   2320:       adddpm(CO,d,t,&s); d = s;
                   2321:       dpm_rest(g,&t); g = t;
                   2322:     }
                   2323:   }
                   2324:   if ( d )
                   2325:     d->sugar = sugar;
                   2326:   *rp = d;
                   2327: }
                   2328:
                   2329: /* nf computation over GF(mod) (only for internal use) */
                   2330:
                   2331: void dp_nf_mod(NODE b,DP g,DP *ps,int mod,int full,DP *rp)
                   2332: {
                   2333:   DP u,p,d,s,t;
                   2334:   P dmy;
                   2335:   NODE l;
                   2336:   MP m,mr;
                   2337:   int sugar,psugar;
                   2338:
                   2339:   if ( !g ) {
                   2340:     *rp = 0; return;
                   2341:   }
                   2342:   sugar = g->sugar;
                   2343:   for ( d = 0; g; ) {
                   2344:     for ( u = 0, l = b; l; l = NEXT(l) ) {
                   2345:       if ( dp_redble(g,p = ps[(long)BDY(l)]) ) {
                   2346:         dp_red_mod(d,g,p,mod,&t,&u,&dmy);
                   2347:         psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
                   2348:         sugar = MAX(sugar,psugar);
                   2349:         if ( !u ) {
                   2350:           if ( d )
                   2351:             d->sugar = sugar;
                   2352:           *rp = d; return;
                   2353:         }
                   2354:         d = t;
                   2355:         break;
                   2356:       }
                   2357:     }
                   2358:     if ( u )
                   2359:       g = u;
                   2360:     else if ( !full ) {
                   2361:       if ( g ) {
                   2362:         MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
                   2363:       }
                   2364:       *rp = g; return;
                   2365:     } else {
                   2366:       m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
                   2367:       NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
                   2368:       addmd(CO,mod,d,t,&s); d = s;
                   2369:       dp_rest(g,&t); g = t;
                   2370:     }
                   2371:   }
                   2372:   if ( d )
                   2373:     d->sugar = sugar;
                   2374:   *rp = d;
                   2375: }
                   2376:
                   2377: void dp_true_nf_mod(NODE b,DP g,DP *ps,int mod,int full,DP *rp,P *dnp)
                   2378: {
                   2379:   DP u,p,d,s,t;
                   2380:   NODE l;
                   2381:   MP m,mr;
                   2382:   int i,n;
                   2383:   int *wb;
                   2384:   int sugar,psugar;
                   2385:   P dn,tdn,tdn1;
                   2386:
                   2387:   dn = (P)ONEM;
                   2388:   if ( !g ) {
                   2389:     *rp = 0; *dnp = dn; return;
                   2390:   }
1.3       noro     2391:   for ( n = 0, l = b; l; l = NEXT(l), n++ )
                   2392:     ;
                   2393:   wb = (int *)ALLOCA(n*sizeof(int));
1.1       noro     2394:   for ( i = 0, l = b; i < n; l = NEXT(l), i++ )
1.2       noro     2395:     wb[i] = ZTOS((Q)BDY(l));
1.1       noro     2396:   sugar = g->sugar;
                   2397:   for ( d = 0; g; ) {
                   2398:     for ( u = 0, i = 0; i < n; i++ ) {
                   2399:       if ( dp_redble(g,p = ps[wb[i]]) ) {
                   2400:         dp_red_mod(d,g,p,mod,&t,&u,&tdn);
                   2401:         psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
                   2402:         sugar = MAX(sugar,psugar);
                   2403:         if ( !u ) {
                   2404:           if ( d )
                   2405:             d->sugar = sugar;
                   2406:           *rp = d; *dnp = dn; return;
                   2407:         } else {
                   2408:           d = t;
                   2409:           mulmp(CO,mod,dn,tdn,&tdn1); dn = tdn1;
                   2410:         }
                   2411:         break;
                   2412:       }
                   2413:     }
                   2414:     if ( u )
                   2415:       g = u;
                   2416:     else if ( !full ) {
                   2417:       if ( g ) {
                   2418:         MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
                   2419:       }
                   2420:       *rp = g; *dnp = dn; return;
                   2421:     } else {
                   2422:       m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
                   2423:       NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
                   2424:       addmd(CO,mod,d,t,&s); d = s;
                   2425:       dp_rest(g,&t); g = t;
                   2426:     }
                   2427:   }
                   2428:   if ( d )
                   2429:     d->sugar = sugar;
                   2430:   *rp = d; *dnp = dn;
                   2431: }
                   2432:
                   2433: void _dp_nf_mod_destructive(NODE b,DP g,DP *ps,int mod,int full,DP *rp)
                   2434: {
                   2435:   DP u,p,d;
                   2436:   NODE l;
                   2437:   MP m,mrd;
                   2438:   int sugar,psugar,n,h_reducible;
                   2439:
                   2440:   if ( !g ) {
                   2441:     *rp = 0; return;
                   2442:   }
                   2443:   sugar = g->sugar;
                   2444:   n = g->nv;
                   2445:   for ( d = 0; g; ) {
                   2446:     for ( h_reducible = 0, l = b; l; l = NEXT(l) ) {
                   2447:       if ( dp_redble(g,p = ps[(long)BDY(l)]) ) {
                   2448:         h_reducible = 1;
                   2449:         psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
                   2450:         _dp_red_mod_destructive(g,p,mod,&u); g = u;
                   2451:         sugar = MAX(sugar,psugar);
                   2452:         if ( !g ) {
                   2453:           if ( d )
                   2454:             d->sugar = sugar;
                   2455:           _dptodp(d,rp); _free_dp(d); return;
                   2456:         }
                   2457:         break;
                   2458:       }
                   2459:     }
                   2460:     if ( !h_reducible ) {
                   2461:       /* head term is not reducible */
                   2462:       if ( !full ) {
                   2463:         if ( g )
                   2464:           g->sugar = sugar;
                   2465:         _dptodp(g,rp); _free_dp(g); return;
                   2466:       } else {
                   2467:         m = BDY(g);
                   2468:         if ( NEXT(m) ) {
                   2469:           BDY(g) = NEXT(m); NEXT(m) = 0;
                   2470:         } else {
                   2471:           _FREEDP(g); g = 0;
                   2472:         }
                   2473:         if ( d ) {
                   2474:           for ( mrd = BDY(d); NEXT(mrd); mrd = NEXT(mrd) );
                   2475:           NEXT(mrd) = m;
                   2476:         } else {
                   2477:           _MKDP(n,m,d);
                   2478:         }
                   2479:       }
                   2480:     }
                   2481:   }
                   2482:   if ( d )
                   2483:     d->sugar = sugar;
                   2484:   _dptodp(d,rp); _free_dp(d);
                   2485: }
                   2486:
                   2487: /* reduction by linear base over a field */
                   2488:
                   2489: void dp_lnf_f(DP p1,DP p2,NODE g,DP *r1p,DP *r2p)
                   2490: {
                   2491:   DP r1,r2,b1,b2,t,s;
                   2492:   Obj c,c1,c2;
                   2493:   NODE l,b;
                   2494:   int n;
                   2495:
                   2496:   if ( !p1 ) {
                   2497:     *r1p = p1; *r2p = p2; return;
                   2498:   }
                   2499:   n = p1->nv;
                   2500:   for ( l = g, r1 = p1, r2 = p2; l; l = NEXT(l) ) {
                   2501:       if ( !r1 ) {
                   2502:         *r1p = r1; *r2p = r2; return;
                   2503:       }
                   2504:       b = BDY((LIST)BDY(l)); b1 = (DP)BDY(b);
                   2505:       if ( dl_equal(n,BDY(r1)->dl,BDY(b1)->dl) ) {
                   2506:         b2 = (DP)BDY(NEXT(b));
                   2507:         divr(CO,(Obj)ONE,(Obj)BDY(b1)->c,&c1);
                   2508:         mulr(CO,c1,(Obj)BDY(r1)->c,&c2); chsgnr(c2,&c);
                   2509:         muldc(CO,b1,(Obj)c,&t); addd(CO,r1,t,&s); r1 = s;
                   2510:         muldc(CO,b2,(Obj)c,&t); addd(CO,r2,t,&s); r2 = s;
                   2511:       }
                   2512:   }
                   2513:   *r1p = r1; *r2p = r2;
                   2514: }
                   2515:
                   2516: /* reduction by linear base over GF(mod) */
                   2517:
                   2518: void dp_lnf_mod(DP p1,DP p2,NODE g,int mod,DP *r1p,DP *r2p)
                   2519: {
                   2520:   DP r1,r2,b1,b2,t,s;
                   2521:   P c;
                   2522:   MQ c1,c2;
                   2523:   NODE l,b;
                   2524:   int n;
                   2525:
                   2526:   if ( !p1 ) {
                   2527:     *r1p = p1; *r2p = p2; return;
                   2528:   }
                   2529:   n = p1->nv;
                   2530:   for ( l = g, r1 = p1, r2 = p2; l; l = NEXT(l) ) {
                   2531:       if ( !r1 ) {
                   2532:         *r1p = r1; *r2p = r2; return;
                   2533:       }
                   2534:       b = BDY((LIST)BDY(l)); b1 = (DP)BDY(b);
                   2535:       if ( dl_equal(n,BDY(r1)->dl,BDY(b1)->dl) ) {
                   2536:         b2 = (DP)BDY(NEXT(b));
                   2537:         invmq(mod,(MQ)BDY(b1)->c,&c1);
                   2538:         mulmq(mod,c1,(MQ)BDY(r1)->c,&c2); chsgnmp(mod,(P)c2,&c);
                   2539:         mulmdc(CO,mod,b1,c,&t); addmd(CO,mod,r1,t,&s); r1 = s;
                   2540:         mulmdc(CO,mod,b2,c,&t); addmd(CO,mod,r2,t,&s); r2 = s;
                   2541:       }
                   2542:   }
                   2543:   *r1p = r1; *r2p = r2;
                   2544: }
                   2545:
                   2546: void dp_nf_tab_mod(DP p,LIST *tab,int mod,DP *rp)
                   2547: {
                   2548:   DP s,t,u;
                   2549:   MP m;
                   2550:   DL h;
                   2551:   int i,n;
                   2552:
                   2553:   if ( !p ) {
                   2554:     *rp = p; return;
                   2555:   }
                   2556:   n = p->nv;
                   2557:   for ( s = 0, i = 0, m = BDY(p); m; m = NEXT(m) ) {
                   2558:     h = m->dl;
                   2559:     while ( !dl_equal(n,h,BDY((DP)BDY(BDY(tab[i])))->dl ) )
                   2560:       i++;
                   2561:     mulmdc(CO,mod,(DP)BDY(NEXT(BDY(tab[i]))),(P)m->c,&t);
                   2562:     addmd(CO,mod,s,t,&u); s = u;
                   2563:   }
                   2564:   *rp = s;
                   2565: }
                   2566:
                   2567: void dp_nf_tab_f(DP p,LIST *tab,DP *rp)
                   2568: {
                   2569:   DP s,t,u;
                   2570:   MP m;
                   2571:   DL h;
                   2572:   int i,n;
                   2573:
                   2574:   if ( !p ) {
                   2575:     *rp = p; return;
                   2576:   }
                   2577:   n = p->nv;
                   2578:   for ( s = 0, i = 0, m = BDY(p); m; m = NEXT(m) ) {
                   2579:     h = m->dl;
                   2580:     while ( !dl_equal(n,h,BDY((DP)BDY(BDY(tab[i])))->dl ) )
                   2581:       i++;
                   2582:     muldc(CO,(DP)BDY(NEXT(BDY(tab[i]))),m->c,&t);
                   2583:     addd(CO,s,t,&u); s = u;
                   2584:   }
                   2585:   *rp = s;
                   2586: }
                   2587:
                   2588: /*
                   2589:  * setting flags
                   2590:  * call create_order_spec with vl=0 to set old type order.
                   2591:  *
                   2592:  */
                   2593:
                   2594: int create_order_spec(VL vl,Obj obj,struct order_spec **specp)
                   2595: {
                   2596:   int i,j,n,s,row,col,ret,wlen;
                   2597:   struct order_spec *spec;
                   2598:   struct order_pair *l;
                   2599:   Obj wp,wm;
                   2600:   NODE node,t,tn,wpair;
                   2601:   MAT m;
                   2602:   VECT v;
                   2603:   pointer **b,*bv;
                   2604:   int **w;
                   2605:
                   2606:   if ( vl && obj && OID(obj) == O_LIST ) {
                   2607:     ret = create_composite_order_spec(vl,(LIST)obj,specp);
                   2608:     if ( show_orderspec )
                   2609:       print_composite_order_spec(*specp);
                   2610:     return ret;
                   2611:   }
                   2612:
                   2613:   *specp = spec = (struct order_spec *)MALLOC(sizeof(struct order_spec));
                   2614:   if ( !obj || NUM(obj) ) {
                   2615:     spec->id = 0; spec->obj = obj;
1.2       noro     2616:     spec->ord.simple = ZTOS((Q)obj);
1.1       noro     2617:     return 1;
                   2618:   } else if ( OID(obj) == O_LIST ) {
1.6       noro     2619:     /* module order */
1.1       noro     2620:     node = BDY((LIST)obj);
                   2621:     if ( !BDY(node) || NUM(BDY(node)) ) {
                   2622:       switch ( length(node) ) {
1.6       noro     2623:       case 2: /* [n,ord] */
1.1       noro     2624:         create_order_spec(0,(Obj)BDY(NEXT(node)),&spec);
                   2625:         spec->id += 256; spec->obj = obj;
                   2626:         spec->top_weight = 0;
                   2627:         spec->module_rank = 0;
                   2628:         spec->module_top_weight = 0;
1.6       noro     2629:         spec->module_ordtype = ZTOS((Z)BDY(node));
                   2630:         if ( spec->module_ordtype < 0  ) {
                   2631:             spec->pot_nelim = -spec->module_ordtype;
                   2632:             spec->module_ordtype = 1;
                   2633:         } else
                   2634:             spec->pot_nelim = 0;
                   2635:         break;
                   2636:
                   2637:       case 3: /* [n,[wv,wm],ord] */
                   2638:         spec->module_ordtype = ZTOS((Z)BDY(node));
                   2639:         if ( spec->module_ordtype < 0  ) {
                   2640:             spec->pot_nelim = -spec->module_ordtype;
                   2641:             spec->module_ordtype = 1;
                   2642:         } else
1.1       noro     2643:             spec->pot_nelim = 0;
1.6       noro     2644:
                   2645:         if ( spec->module_ordtype == 3 ) { /* schreyer order */
                   2646:           Obj baseobj;
                   2647:           struct order_spec *basespec;
                   2648:           int len;
                   2649:           NODE in;
                   2650:           LIST *la;
                   2651:           DMMstack stack;
                   2652:           DMMstack push_schreyer_order(LIST l,DMMstack s);
                   2653:
                   2654:           spec->id = 300; spec->obj = obj;
                   2655:           node = NEXT(node);
                   2656:           if ( !BDY(node) || OID(BDY(node)) != O_LIST )
                   2657:             error("create_order_spec : [mlist1,mlist,...] must be specified for defining a schreyer order");
                   2658:           stack = 0;
                   2659:           in = BDY((LIST)BDY(node));
                   2660:           len = length(in);
                   2661:           la = (LIST *)MALLOC(len*sizeof(LIST));
                   2662:           for ( i = 0; i < len; i++, in = NEXT(in) ) la[i] = (LIST)(BDY(in));
                   2663:           for ( i = len-1; i >= 0; i-- ) stack = push_schreyer_order(la[i],stack);
                   2664:           spec->dmmstack = stack;
                   2665:
                   2666:           node = NEXT(node);
                   2667:           baseobj = (Obj)BDY(node);
                   2668:           create_order_spec(0,baseobj,&basespec);
                   2669:           basespec->obj = baseobj;
                   2670:           spec->base = basespec;
                   2671:         } else {  /* weighted order */
1.7       noro     2672:           int ordtype;
                   2673:
                   2674:           ordtype = spec->module_ordtype;
1.6       noro     2675:           create_order_spec(0,(Obj)BDY(NEXT(NEXT(node))),&spec);
1.7       noro     2676:           spec->module_ordtype = ordtype;
                   2677:           spec->id += 256; spec->obj = obj;
1.6       noro     2678:           node = NEXT(node);
                   2679:           if ( !BDY(node) || OID(BDY(node)) != O_LIST )
                   2680:             error("create_order_spec : [weight_for_poly,weight_for_modlue] must be specified as a module topweight");
                   2681:           wpair = BDY((LIST)BDY(node));
                   2682:           if ( length(wpair) != 2 )
                   2683:             error("create_order_spec : [weight_for_poly,weight_for_modlue] must be specified as a module topweight");
                   2684:
                   2685:           wp = BDY(wpair);
                   2686:           wm = BDY(NEXT(wpair));
                   2687:           if ( !wp || OID(wp) != O_LIST || !wm || OID(wm) != O_LIST )
                   2688:             error("create_order_spec : [weight_for_poly,weight_for_modlue] must be specified as a module topweight");
                   2689:           spec->nv = length(BDY((LIST)wp));
                   2690:           spec->top_weight = (int *)MALLOC_ATOMIC(spec->nv*sizeof(int));
                   2691:           for ( i = 0, t = BDY((LIST)wp); i < spec->nv; t = NEXT(t), i++ )
                   2692:             spec->top_weight[i] = ZTOS((Q)BDY(t));
                   2693:
                   2694:           spec->module_rank = length(BDY((LIST)wm));
                   2695:           spec->module_top_weight = (int *)MALLOC_ATOMIC(spec->module_rank*sizeof(int));
                   2696:           for ( i = 0, t = BDY((LIST)wm); i < spec->module_rank; t = NEXT(t), i++ )
                   2697:             spec->module_top_weight[i] = ZTOS((Q)BDY(t));
1.1       noro     2698:         }
                   2699:         break;
                   2700:
                   2701:       default:
                   2702:         error("create_order_spec : invalid arguments for module order");
                   2703:       }
                   2704:
                   2705:       *specp = spec;
                   2706:       return 1;
                   2707:     } else {
                   2708:       /* block order in polynomial ring */
                   2709:       for ( n = 0, t = node; t; t = NEXT(t), n++ );
                   2710:       l = (struct order_pair *)MALLOC_ATOMIC(n*sizeof(struct order_pair));
                   2711:       for ( i = 0, t = node, s = 0; i < n; t = NEXT(t), i++ ) {
1.2       noro     2712:         tn = BDY((LIST)BDY(t)); l[i].order = ZTOS((Q)BDY(tn));
                   2713:         tn = NEXT(tn); l[i].length = ZTOS((Q)BDY(tn));
1.1       noro     2714:         s += l[i].length;
                   2715:       }
                   2716:       spec->id = 1; spec->obj = obj;
                   2717:       spec->ord.block.order_pair = l;
                   2718:       spec->ord.block.length = n; spec->nv = s;
                   2719:       return 1;
                   2720:     }
                   2721:   } else if ( OID(obj) == O_MAT ) {
                   2722:     m = (MAT)obj; row = m->row; col = m->col; b = BDY(m);
                   2723:     w = almat(row,col);
                   2724:     for ( i = 0; i < row; i++ )
                   2725:       for ( j = 0; j < col; j++ )
1.2       noro     2726:         w[i][j] = ZTOS((Q)b[i][j]);
1.1       noro     2727:     spec->id = 2; spec->obj = obj;
                   2728:     spec->nv = col; spec->ord.matrix.row = row;
                   2729:     spec->ord.matrix.matrix = w;
                   2730:     return 1;
                   2731:   } else
                   2732:     return 0;
                   2733: }
                   2734:
                   2735: void print_composite_order_spec(struct order_spec *spec)
                   2736: {
                   2737:   int nv,n,len,i,j,k,start;
                   2738:   struct weight_or_block *worb;
                   2739:
                   2740:   nv = spec->nv;
                   2741:   n = spec->ord.composite.length;
                   2742:   worb = spec->ord.composite.w_or_b;
                   2743:   for ( i = 0; i < n; i++, worb++ ) {
                   2744:     len = worb->length;
                   2745:     printf("[ ");
                   2746:     switch ( worb->type ) {
                   2747:       case IS_DENSE_WEIGHT:
                   2748:         for ( j = 0; j < len; j++ )
                   2749:           printf("%d ",worb->body.dense_weight[j]);
                   2750:         for ( ; j < nv; j++ )
                   2751:           printf("0 ");
                   2752:         break;
                   2753:       case IS_SPARSE_WEIGHT:
                   2754:         for ( j = 0, k = 0; j < nv; j++ )
                   2755:           if ( j == worb->body.sparse_weight[k].pos )
                   2756:             printf("%d ",worb->body.sparse_weight[k++].value);
                   2757:           else
                   2758:             printf("0 ");
                   2759:         break;
                   2760:       case IS_BLOCK:
                   2761:         start = worb->body.block.start;
                   2762:         for ( j = 0; j < start; j++ ) printf("0 ");
                   2763:         switch ( worb->body.block.order ) {
                   2764:           case 0:
                   2765:             for ( k = 0; k < len; k++, j++ ) printf("R ");
                   2766:             break;
                   2767:           case 1:
                   2768:             for ( k = 0; k < len; k++, j++ ) printf("G ");
                   2769:             break;
                   2770:           case 2:
                   2771:             for ( k = 0; k < len; k++, j++ ) printf("L ");
                   2772:             break;
                   2773:         }
                   2774:         for ( ; j < nv; j++ ) printf("0 ");
                   2775:         break;
                   2776:     }
                   2777:     printf("]\n");
                   2778:   }
                   2779: }
                   2780:
                   2781: struct order_spec *append_block(struct order_spec *spec,
                   2782:   int nv,int nalg,int ord)
                   2783: {
                   2784:   MAT m,mat;
                   2785:   int i,j,row,col,n;
                   2786:   Z **b,**wp;
                   2787:   int **w;
                   2788:   NODE t,s,s0;
                   2789:   struct order_pair *l,*l0;
                   2790:   int n0,nv0;
                   2791:   LIST list0,list1,list;
                   2792:   Z oq,nq;
                   2793:   struct order_spec *r;
                   2794:
                   2795:   r = (struct order_spec *)MALLOC(sizeof(struct order_spec));
                   2796:   switch ( spec->id ) {
                   2797:     case 0:
1.2       noro     2798:       STOZ(spec->ord.simple,oq); STOZ(nv,nq);
1.1       noro     2799:       t = mknode(2,oq,nq); MKLIST(list0,t);
1.2       noro     2800:       STOZ(ord,oq); STOZ(nalg,nq);
1.1       noro     2801:       t = mknode(2,oq,nq); MKLIST(list1,t);
                   2802:       t = mknode(2,list0,list1); MKLIST(list,t);
                   2803:       l = (struct order_pair *)MALLOC_ATOMIC(2*sizeof(struct order_pair));
                   2804:       l[0].order = spec->ord.simple; l[0].length = nv;
                   2805:       l[1].order = ord; l[1].length = nalg;
                   2806:       r->id = 1;  r->obj = (Obj)list;
                   2807:       r->ord.block.order_pair = l;
                   2808:       r->ord.block.length = 2;
                   2809:       r->nv = nv+nalg;
                   2810:       break;
                   2811:     case 1:
                   2812:       if ( spec->nv != nv )
                   2813:         error("append_block : number of variables mismatch");
                   2814:       l0 = spec->ord.block.order_pair;
                   2815:       n0 = spec->ord.block.length;
                   2816:       nv0 = spec->nv;
                   2817:       list0 = (LIST)spec->obj;
                   2818:       n = n0+1;
                   2819:       l = (struct order_pair *)MALLOC_ATOMIC(n*sizeof(struct order_pair));
                   2820:       for ( i = 0; i < n0; i++ )
                   2821:         l[i] = l0[i];
                   2822:       l[i].order = ord; l[i].length = nalg;
                   2823:        for ( t = BDY(list0), s0 = 0; t; t = NEXT(t) ) {
                   2824:         NEXTNODE(s0,s); BDY(s) = BDY(t);
                   2825:       }
1.2       noro     2826:       STOZ(ord,oq); STOZ(nalg,nq);
1.1       noro     2827:       t = mknode(2,oq,nq); MKLIST(list,t);
                   2828:       NEXTNODE(s0,s); BDY(s) = (pointer)list; NEXT(s) = 0;
                   2829:       MKLIST(list,s0);
                   2830:       r->id = 1;  r->obj = (Obj)list;
                   2831:       r->ord.block.order_pair = l;
                   2832:       r->ord.block.length = n;
                   2833:       r->nv = nv+nalg;
                   2834:       break;
                   2835:     case 2:
                   2836:       if ( spec->nv != nv )
                   2837:         error("append_block : number of variables mismatch");
                   2838:       m = (MAT)spec->obj;
                   2839:       row = m->row; col = m->col; b = (Z **)BDY(m);
                   2840:       w = almat(row+nalg,col+nalg);
                   2841:       MKMAT(mat,row+nalg,col+nalg); wp = (Z **)BDY(mat);
                   2842:       for ( i = 0; i < row; i++ )
                   2843:         for ( j = 0; j < col; j++ ) {
1.2       noro     2844:           w[i][j] = ZTOS(b[i][j]);
1.1       noro     2845:           wp[i][j] = b[i][j];
                   2846:         }
                   2847:       for ( i = 0; i < nalg; i++ ) {
                   2848:         w[i+row][i+col] = 1;
                   2849:         wp[i+row][i+col] = ONE;
                   2850:       }
                   2851:       r->id = 2; r->obj = (Obj)mat;
                   2852:       r->nv = col+nalg; r->ord.matrix.row = row+nalg;
                   2853:       r->ord.matrix.matrix = w;
                   2854:       break;
                   2855:     case 3:
                   2856:     default:
                   2857:       /* XXX */
                   2858:       error("append_block : not implemented yet");
                   2859:   }
                   2860:   return r;
                   2861: }
                   2862:
                   2863: int comp_sw(struct sparse_weight *a, struct sparse_weight *b)
                   2864: {
                   2865:   if ( a->pos > b->pos ) return 1;
                   2866:   else if ( a->pos < b->pos ) return -1;
                   2867:   else return 0;
                   2868: }
                   2869:
                   2870: /* order = [w_or_b, w_or_b, ... ] */
                   2871: /* w_or_b = w or b                */
                   2872: /* w = [1,2,...] or [x,1,y,2,...] */
                   2873: /* b = [@lex,x,y,...,z] etc       */
                   2874:
                   2875: int create_composite_order_spec(VL vl,LIST order,struct order_spec **specp)
                   2876: {
                   2877:   NODE wb,t,p;
                   2878:   struct order_spec *spec;
                   2879:   VL tvl;
                   2880:   int n,i,j,k,l,start,end,len,w;
                   2881:   int *dw;
                   2882:   struct sparse_weight *sw;
                   2883:   struct weight_or_block *w_or_b;
                   2884:   Obj a0;
                   2885:   NODE a;
                   2886:   V v,sv,ev;
                   2887:   SYMBOL sym;
                   2888:   int *top;
                   2889:
                   2890:   /* l = number of vars in vl */
                   2891:   for ( l = 0, tvl = vl; tvl; tvl = NEXT(tvl), l++ );
                   2892:   /* n = number of primitives in order */
                   2893:   wb = BDY(order);
                   2894:   n = length(wb);
                   2895:   *specp = spec = (struct order_spec *)MALLOC(sizeof(struct order_spec));
                   2896:   spec->id = 3;
                   2897:   spec->obj = (Obj)order;
                   2898:   spec->nv = l;
                   2899:   spec->ord.composite.length = n;
                   2900:   w_or_b = spec->ord.composite.w_or_b = (struct weight_or_block *)
                   2901:     MALLOC(sizeof(struct weight_or_block)*(n+1));
                   2902:
                   2903:   /* top : register the top variable in each w_or_b specification */
                   2904:   top = (int *)ALLOCA(l*sizeof(int));
                   2905:   for ( i = 0; i < l; i++ ) top[i] = 0;
                   2906:
                   2907:   for ( t = wb, i = 0; t; t = NEXT(t), i++ ) {
                   2908:     if ( !BDY(t) || OID((Obj)BDY(t)) != O_LIST )
                   2909:       error("a list of lists must be specified for the key \"order\"");
                   2910:     a = BDY((LIST)BDY(t));
                   2911:     len = length(a);
                   2912:     a0 = (Obj)BDY(a);
                   2913:     if ( !a0 || OID(a0) == O_N ) {
                   2914:       /* a is a dense weight vector */
                   2915:       dw = (int *)MALLOC(sizeof(int)*len);
                   2916:       for ( j = 0, p = a; j < len; p = NEXT(p), j++ ) {
                   2917:         if ( !INT((Q)BDY(p)) )
                   2918:           error("a dense weight vector must be specified as a list of integers");
1.2       noro     2919:         dw[j] = ZTOS((Q)BDY(p));
1.1       noro     2920:       }
                   2921:       w_or_b[i].type = IS_DENSE_WEIGHT;
                   2922:       w_or_b[i].length = len;
                   2923:       w_or_b[i].body.dense_weight = dw;
                   2924:
                   2925:       /* find the top */
                   2926:       for ( k = 0; k < len && !dw[k]; k++ );
                   2927:       if ( k < len ) top[k] = 1;
                   2928:
                   2929:     } else if ( OID(a0) == O_P ) {
                   2930:       /* a is a sparse weight vector */
                   2931:       len >>= 1;
                   2932:       sw = (struct sparse_weight *)
                   2933:         MALLOC(sizeof(struct sparse_weight)*len);
                   2934:       for ( j = 0, p = a; j < len; j++ ) {
                   2935:         if ( !BDY(p) || OID((P)BDY(p)) != O_P )
                   2936:           error("a sparse weight vector must be specified as [var1,weight1,...]");
                   2937:         v = VR((P)BDY(p)); p = NEXT(p);
                   2938:         for ( tvl = vl, k = 0; tvl && tvl->v != v;
                   2939:           k++, tvl = NEXT(tvl) );
                   2940:         if ( !tvl )
                   2941:           error("invalid variable name in a sparse weight vector");
                   2942:         sw[j].pos = k;
                   2943:         if ( !INT((Q)BDY(p)) )
                   2944:           error("a sparse weight vector must be specified as [var1,weight1,...]");
1.2       noro     2945:         sw[j].value = ZTOS((Q)BDY(p)); p = NEXT(p);
1.1       noro     2946:       }
                   2947:       qsort(sw,len,sizeof(struct sparse_weight),
                   2948:         (int (*)(const void *,const void *))comp_sw);
                   2949:       w_or_b[i].type = IS_SPARSE_WEIGHT;
                   2950:       w_or_b[i].length = len;
                   2951:       w_or_b[i].body.sparse_weight = sw;
                   2952:
                   2953:       /* find the top */
                   2954:       for ( k = 0; k < len && !sw[k].value; k++ );
                   2955:       if ( k < len ) top[sw[k].pos] = 1;
                   2956:     } else if ( OID(a0) == O_RANGE ) {
                   2957:       /* [range(v1,v2),w] */
                   2958:       sv = VR((P)(((RANGE)a0)->start));
                   2959:       ev = VR((P)(((RANGE)a0)->end));
                   2960:       for ( tvl = vl, start = 0; tvl && tvl->v != sv; start++, tvl = NEXT(tvl) );
                   2961:       if ( !tvl )
                   2962:         error("invalid range");
                   2963:       for ( end = start; tvl && tvl->v != ev; end++, tvl = NEXT(tvl) );
                   2964:       if ( !tvl )
                   2965:         error("invalid range");
                   2966:       len = end-start+1;
                   2967:       sw = (struct sparse_weight *)
                   2968:         MALLOC(sizeof(struct sparse_weight)*len);
1.2       noro     2969:       w = ZTOS((Q)BDY(NEXT(a)));
1.1       noro     2970:       for ( tvl = vl, k = 0; k < start; k++, tvl = NEXT(tvl) );
                   2971:       for ( j = 0 ; k <= end; k++, tvl = NEXT(tvl), j++ ) {
                   2972:         sw[j].pos = k;
                   2973:         sw[j].value = w;
                   2974:       }
                   2975:       w_or_b[i].type = IS_SPARSE_WEIGHT;
                   2976:       w_or_b[i].length = len;
                   2977:       w_or_b[i].body.sparse_weight = sw;
                   2978:
                   2979:       /* register the top */
                   2980:       if ( w ) top[start] = 1;
                   2981:     } else if ( OID(a0) == O_SYMBOL ) {
                   2982:       /* a is a block */
                   2983:       sym = (SYMBOL)a0; a = NEXT(a); len--;
                   2984:       if ( OID((Obj)BDY(a)) == O_RANGE ) {
                   2985:         sv = VR((P)(((RANGE)BDY(a))->start));
                   2986:         ev = VR((P)(((RANGE)BDY(a))->end));
                   2987:         for ( tvl = vl, start = 0; tvl && tvl->v != sv; start++, tvl = NEXT(tvl) );
                   2988:         if ( !tvl )
                   2989:           error("invalid range");
                   2990:         for ( end = start; tvl && tvl->v != ev; end++, tvl = NEXT(tvl) );
                   2991:         if ( !tvl )
                   2992:           error("invalid range");
                   2993:         len = end-start+1;
                   2994:       } else {
                   2995:         for ( start = 0, tvl = vl; tvl->v != VR((P)BDY(a));
                   2996:         tvl = NEXT(tvl), start++ );
                   2997:         for ( p = NEXT(a), tvl = NEXT(tvl); p;
                   2998:           p = NEXT(p), tvl = NEXT(tvl) ) {
                   2999:           if ( !BDY(p) || OID((P)BDY(p)) != O_P )
                   3000:             error("a block must be specified as [ordsymbol,var1,var2,...]");
                   3001:           if ( tvl->v != VR((P)BDY(p)) ) break;
                   3002:         }
                   3003:         if ( p )
                   3004:           error("a block must be contiguous in the variable list");
                   3005:       }
                   3006:       w_or_b[i].type = IS_BLOCK;
                   3007:       w_or_b[i].length = len;
                   3008:       w_or_b[i].body.block.start = start;
                   3009:       if ( !strcmp(sym->name,"@grlex") )
                   3010:         w_or_b[i].body.block.order = 0;
                   3011:       else if ( !strcmp(sym->name,"@glex") )
                   3012:         w_or_b[i].body.block.order = 1;
                   3013:       else if ( !strcmp(sym->name,"@lex") )
                   3014:         w_or_b[i].body.block.order = 2;
                   3015:       else
                   3016:         error("invalid ordername");
                   3017:       /* register the tops */
                   3018:       for ( j = 0, k = start; j < len; j++, k++ )
                   3019:         top[k] = 1;
                   3020:     }
                   3021:   }
                   3022:   for ( k = 0; k < l && top[k]; k++ );
                   3023:   if ( k < l ) {
                   3024:     /* incomplete order specification; add @grlex */
                   3025:     w_or_b[n].type = IS_BLOCK;
                   3026:     w_or_b[n].length = l;
                   3027:     w_or_b[n].body.block.start = 0;
                   3028:     w_or_b[n].body.block.order = 0;
                   3029:     spec->ord.composite.length = n+1;
                   3030:   }
1.3       noro     3031:   return 1;
1.1       noro     3032: }
                   3033:
                   3034: /* module order spec */
                   3035:
                   3036: void create_modorder_spec(int id,LIST shift,struct modorder_spec **s)
                   3037: {
                   3038:   struct modorder_spec *spec;
                   3039:   NODE n,t;
                   3040:   LIST list;
                   3041:   int *ds;
                   3042:   int i,l;
                   3043:   Z q;
                   3044:
                   3045:   *s = spec = (struct modorder_spec *)MALLOC(sizeof(struct modorder_spec));
                   3046:   spec->id = id;
                   3047:   if ( shift ) {
                   3048:     n = BDY(shift);
                   3049:     spec->len = l = length(n);
                   3050:     spec->degree_shift = ds = (int *)MALLOC_ATOMIC(l*sizeof(int));
                   3051:     for ( t = n, i = 0; t; t = NEXT(t), i++ )
1.2       noro     3052:       ds[i] = ZTOS((Q)BDY(t));
1.1       noro     3053:   } else {
                   3054:     spec->len = 0;
                   3055:     spec->degree_shift = 0;
                   3056:   }
1.2       noro     3057:   STOZ(id,q);
1.1       noro     3058:   n = mknode(2,q,shift);
                   3059:   MKLIST(list,n);
                   3060:   spec->obj = (Obj)list;
                   3061: }
                   3062:
                   3063: /*
                   3064:  * converters
                   3065:  *
                   3066:  */
                   3067:
1.7       noro     3068: void dpm_homo(DPM p,DPM *rp)
                   3069: {
                   3070:   DMM m,mr,mr0,t;
                   3071:   int i,n,nv,td;
                   3072:   DL dl,dlh;
                   3073:
                   3074:   if ( !p )
                   3075:     *rp = 0;
                   3076:   else {
                   3077:     n = p->nv; nv = n + 1;
                   3078:     m = BDY(p);
                   3079:     td = 0;
                   3080:     for ( t = m; t; t = NEXT(t) )
                   3081:       if ( m->dl->td > td ) td = m->dl->td;
                   3082:     for ( mr0 = 0; m; m = NEXT(m) ) {
                   3083:       NEXTDMM(mr0,mr); mr->c = m->c; mr->pos = m->pos;
                   3084:       dl = m->dl;
                   3085:       mr->dl = dlh = (DL)MALLOC_ATOMIC((nv+1)*sizeof(int));
                   3086:       dlh->td = td;
                   3087:       for ( i = 0; i < n; i++ )
                   3088:         dlh->d[i] = dl->d[i];
                   3089:       dlh->d[n] = td - dl->td;
                   3090:     }
                   3091:     NEXT(mr) = 0; MKDPM(nv,mr0,*rp); (*rp)->sugar = p->sugar;
                   3092:   }
                   3093: }
                   3094:
                   3095: void dpm_dehomo(DPM p,DPM *rp)
                   3096: {
                   3097:   DMM m,mr,mr0;
                   3098:   int i,n,nv;
                   3099:   DL dl,dlh;
                   3100:
                   3101:   if ( !p )
                   3102:     *rp = 0;
                   3103:   else {
                   3104:     n = p->nv; nv = n - 1;
                   3105:     m = BDY(p);
                   3106:     for ( mr0 = 0; m; m = NEXT(m) ) {
                   3107:       NEXTDMM(mr0,mr); mr->c = m->c; mr->pos = m->pos;
                   3108:       dlh = m->dl;
                   3109:       mr->dl = dl = (DL)MALLOC_ATOMIC((nv+1)*sizeof(int));
                   3110:       dl->td = dlh->td - dlh->d[nv];
                   3111:       for ( i = 0; i < nv; i++ )
                   3112:         dl->d[i] = dlh->d[i];
                   3113:     }
                   3114:     NEXT(mr) = 0; MKDPM(nv,mr0,*rp); (*rp)->sugar = p->sugar;
                   3115:   }
                   3116: }
                   3117:
1.1       noro     3118: void dp_homo(DP p,DP *rp)
                   3119: {
                   3120:   MP m,mr,mr0;
                   3121:   int i,n,nv,td;
                   3122:   DL dl,dlh;
                   3123:
                   3124:   if ( !p )
                   3125:     *rp = 0;
                   3126:   else {
                   3127:     n = p->nv; nv = n + 1;
                   3128:     m = BDY(p); td = sugard(m);
                   3129:     for ( mr0 = 0; m; m = NEXT(m) ) {
                   3130:       NEXTMP(mr0,mr); mr->c = m->c;
                   3131:       dl = m->dl;
                   3132:       mr->dl = dlh = (DL)MALLOC_ATOMIC((nv+1)*sizeof(int));
                   3133:       dlh->td = td;
                   3134:       for ( i = 0; i < n; i++ )
                   3135:         dlh->d[i] = dl->d[i];
                   3136:       dlh->d[n] = td - dl->td;
                   3137:     }
                   3138:     NEXT(mr) = 0; MKDP(nv,mr0,*rp); (*rp)->sugar = p->sugar;
                   3139:   }
                   3140: }
                   3141:
                   3142: void dp_dehomo(DP p,DP *rp)
                   3143: {
                   3144:   MP m,mr,mr0;
                   3145:   int i,n,nv;
                   3146:   DL dl,dlh;
                   3147:
                   3148:   if ( !p )
                   3149:     *rp = 0;
                   3150:   else {
                   3151:     n = p->nv; nv = n - 1;
                   3152:     m = BDY(p);
                   3153:     for ( mr0 = 0; m; m = NEXT(m) ) {
                   3154:       NEXTMP(mr0,mr); mr->c = m->c;
                   3155:       dlh = m->dl;
                   3156:       mr->dl = dl = (DL)MALLOC_ATOMIC((nv+1)*sizeof(int));
                   3157:       dl->td = dlh->td - dlh->d[nv];
                   3158:       for ( i = 0; i < nv; i++ )
                   3159:         dl->d[i] = dlh->d[i];
                   3160:     }
                   3161:     NEXT(mr) = 0; MKDP(nv,mr0,*rp); (*rp)->sugar = p->sugar;
                   3162:   }
                   3163: }
                   3164:
1.7       noro     3165:
1.1       noro     3166: void dp_mod(DP p,int mod,NODE subst,DP *rp)
                   3167: {
                   3168:   MP m,mr,mr0;
                   3169:   P t,s,s1;
                   3170:   V v;
                   3171:   NODE tn;
                   3172:
                   3173:   if ( !p )
                   3174:     *rp = 0;
                   3175:   else {
                   3176:     for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   3177:       for ( tn = subst, s = (P)m->c; tn; tn = NEXT(tn) ) {
                   3178:         v = VR((P)BDY(tn)); tn = NEXT(tn);
                   3179:         substp(CO,s,v,(P)BDY(tn),&s1); s = s1;
                   3180:       }
                   3181:       ptomp(mod,s,&t);
                   3182:       if ( t ) {
                   3183:         NEXTMP(mr0,mr); mr->c = (Obj)t; mr->dl = m->dl;
                   3184:       }
                   3185:     }
                   3186:     if ( mr0 ) {
                   3187:       NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
                   3188:     } else
                   3189:       *rp = 0;
                   3190:   }
                   3191: }
                   3192:
                   3193: void dp_rat(DP p,DP *rp)
                   3194: {
                   3195:   MP m,mr,mr0;
                   3196:
                   3197:   if ( !p )
                   3198:     *rp = 0;
                   3199:   else {
                   3200:     for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   3201:       NEXTMP(mr0,mr); mptop((P)m->c,(P *)&mr->c); mr->dl = m->dl;
                   3202:     }
                   3203:     if ( mr0 ) {
                   3204:       NEXT(mr) = 0; MKDP(p->nv,mr0,*rp); (*rp)->sugar = p->sugar;
                   3205:     } else
                   3206:       *rp = 0;
                   3207:   }
                   3208: }
                   3209:
                   3210:
                   3211: void homogenize_order(struct order_spec *old,int n,struct order_spec **newp)
                   3212: {
                   3213:   struct order_pair *l;
                   3214:   int length,nv,row,i,j;
                   3215:   int **newm,**oldm;
                   3216:   struct order_spec *new;
                   3217:   int onv,nnv,nlen,olen,owlen;
                   3218:   struct weight_or_block *owb,*nwb;
                   3219:
                   3220:   *newp = new = (struct order_spec *)MALLOC(sizeof(struct order_spec));
1.6       noro     3221:   bcopy((char *)old,(char *)new,sizeof(struct order_spec));
1.1       noro     3222:   switch ( old->id ) {
                   3223:     case 0:
                   3224:       switch ( old->ord.simple ) {
                   3225:         case 0:
1.6       noro     3226:           break;
1.1       noro     3227:         case 1:
                   3228:           l = (struct order_pair *)
                   3229:             MALLOC_ATOMIC(2*sizeof(struct order_pair));
                   3230:           l[0].length = n; l[0].order = 1;
                   3231:           l[1].length = 1; l[1].order = 2;
                   3232:           new->id = 1;
                   3233:           new->ord.block.order_pair = l;
                   3234:           new->ord.block.length = 2; new->nv = n+1;
                   3235:           break;
                   3236:         case 2:
1.6       noro     3237:           new->ord.simple = 1; break;
1.1       noro     3238:         case 3: case 4: case 5:
1.6       noro     3239:           new->ord.simple = old->ord.simple+3;
1.1       noro     3240:           dp_nelim = n-1; break;
                   3241:         case 6: case 7: case 8: case 9:
1.6       noro     3242:           break;
1.1       noro     3243:         default:
                   3244:           error("homogenize_order : invalid input");
                   3245:       }
                   3246:       break;
                   3247:     case 1: case 257:
                   3248:       length = old->ord.block.length;
                   3249:       l = (struct order_pair *)
                   3250:         MALLOC_ATOMIC((length+1)*sizeof(struct order_pair));
                   3251:       bcopy((char *)old->ord.block.order_pair,(char *)l,length*sizeof(struct order_pair));
                   3252:       l[length].order = 2; l[length].length = 1;
1.6       noro     3253:       new->nv = n+1;
1.1       noro     3254:       new->ord.block.order_pair = l;
                   3255:       new->ord.block.length = length+1;
                   3256:       break;
                   3257:     case 2: case 258:
                   3258:       nv = old->nv; row = old->ord.matrix.row;
                   3259:       oldm = old->ord.matrix.matrix; newm = almat(row+1,nv+1);
                   3260:       for ( i = 0; i <= nv; i++ )
                   3261:         newm[0][i] = 1;
                   3262:       for ( i = 0; i < row; i++ ) {
                   3263:         for ( j = 0; j < nv; j++ )
                   3264:           newm[i+1][j] = oldm[i][j];
                   3265:         newm[i+1][j] = 0;
                   3266:       }
1.6       noro     3267:       new->nv = nv+1;
1.1       noro     3268:       new->ord.matrix.row = row+1; new->ord.matrix.matrix = newm;
                   3269:       break;
                   3270:     case 3: case 259:
                   3271:       onv = old->nv;
                   3272:       nnv = onv+1;
                   3273:       olen = old->ord.composite.length;
                   3274:       nlen = olen+1;
                   3275:       owb = old->ord.composite.w_or_b;
                   3276:       nwb = (struct weight_or_block *)
                   3277:         MALLOC(nlen*sizeof(struct weight_or_block));
                   3278:       for ( i = 0; i < olen; i++ ) {
                   3279:         nwb[i].type = owb[i].type;
                   3280:         switch ( owb[i].type ) {
                   3281:           case IS_DENSE_WEIGHT:
                   3282:             owlen = owb[i].length;
                   3283:             nwb[i].length = owlen+1;
                   3284:             nwb[i].body.dense_weight = (int *)MALLOC((owlen+1)*sizeof(int));
                   3285:             for ( j = 0; j < owlen; j++ )
                   3286:               nwb[i].body.dense_weight[j] = owb[i].body.dense_weight[j];
                   3287:             nwb[i].body.dense_weight[owlen] = 0;
                   3288:             break;
                   3289:           case IS_SPARSE_WEIGHT:
                   3290:             nwb[i].length = owb[i].length;
                   3291:             nwb[i].body.sparse_weight = owb[i].body.sparse_weight;
                   3292:             break;
                   3293:           case IS_BLOCK:
                   3294:             nwb[i].length = owb[i].length;
                   3295:             nwb[i].body.block = owb[i].body.block;
                   3296:             break;
                   3297:         }
                   3298:       }
                   3299:       nwb[i].type = IS_SPARSE_WEIGHT;
                   3300:       nwb[i].body.sparse_weight =
                   3301:         (struct sparse_weight *)MALLOC(sizeof(struct sparse_weight));
                   3302:       nwb[i].body.sparse_weight[0].pos = onv;
                   3303:       nwb[i].body.sparse_weight[0].value = 1;
                   3304:       new->nv = nnv;
                   3305:       new->ord.composite.length = nlen;
                   3306:       new->ord.composite.w_or_b = nwb;
                   3307:       print_composite_order_spec(new);
                   3308:       break;
                   3309:     case 256: /* simple module order */
                   3310:       switch ( old->ord.simple ) {
                   3311:         case 0:
1.6       noro     3312:           break;
1.1       noro     3313:         case 1:
                   3314:           l = (struct order_pair *)
                   3315:             MALLOC_ATOMIC(2*sizeof(struct order_pair));
                   3316:           l[0].length = n; l[0].order = old->ord.simple;
                   3317:           l[1].length = 1; l[1].order = 2;
                   3318:           new->id = 257;
                   3319:           new->ord.block.order_pair = l;
                   3320:           new->ord.block.length = 2; new->nv = n+1;
                   3321:           break;
                   3322:         case 2:
1.6       noro     3323:           new->ord.simple = 1; break;
1.1       noro     3324:         default:
                   3325:           error("homogenize_order : invalid input");
                   3326:       }
                   3327:       break;
                   3328:     default:
                   3329:       error("homogenize_order : invalid input");
                   3330:   }
                   3331: }
                   3332:
                   3333: int comp_nm(Q *a,Q *b)
                   3334: {
                   3335:   Z z,nma,nmb;
                   3336:
                   3337:   nmq(*a,&z); absz(z,&nma);
                   3338:   nmq(*b,&z); absz(z,&nmb);
                   3339:   return cmpz(nma,nmb);
                   3340: }
                   3341:
                   3342: void sortbynm(Q *w,int n)
                   3343: {
                   3344:   qsort(w,n,sizeof(Q),(int (*)(const void *,const void *))comp_nm);
                   3345: }
                   3346:
                   3347:
                   3348: /*
                   3349:  * simple operations
                   3350:  *
                   3351:  */
                   3352:
                   3353: int dp_redble(DP p1,DP p2)
                   3354: {
                   3355:   int i,n;
                   3356:   DL d1,d2;
                   3357:
                   3358:   d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
                   3359:   if ( d1->td < d2->td )
                   3360:     return 0;
                   3361:   else {
                   3362:     for ( i = 0, n = p1->nv; i < n; i++ )
                   3363:       if ( d1->d[i] < d2->d[i] )
                   3364:         return 0;
                   3365:     return 1;
                   3366:   }
                   3367: }
                   3368:
                   3369: int dpm_redble(DPM p1,DPM p2)
                   3370: {
                   3371:   int i,n;
                   3372:   DL d1,d2;
                   3373:
                   3374:   if ( BDY(p1)->pos != BDY(p2)->pos ) return 0;
                   3375:   d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
                   3376:   if ( d1->td < d2->td )
                   3377:     return 0;
                   3378:   else {
                   3379:     for ( i = 0, n = p1->nv; i < n; i++ )
                   3380:       if ( d1->d[i] < d2->d[i] )
                   3381:         return 0;
                   3382:     return 1;
                   3383:   }
                   3384: }
                   3385:
                   3386:
                   3387: void dp_subd(DP p1,DP p2,DP *rp)
                   3388: {
                   3389:   int i,n;
                   3390:   DL d1,d2,d;
                   3391:   MP m;
                   3392:   DP s;
                   3393:
                   3394:   n = p1->nv; d1 = BDY(p1)->dl; d2 = BDY(p2)->dl;
                   3395:   NEWDL(d,n); d->td = d1->td - d2->td;
                   3396:   for ( i = 0; i < n; i++ )
                   3397:     d->d[i] = d1->d[i]-d2->d[i];
                   3398:   NEWMP(m); m->dl = d; m->c = (Obj)ONE; NEXT(m) = 0;
                   3399:   MKDP(n,m,s); s->sugar = d->td;
                   3400:   *rp = s;
                   3401: }
                   3402:
                   3403: void dltod(DL d,int n,DP *rp)
                   3404: {
                   3405:   MP m;
                   3406:   DP s;
                   3407:
                   3408:   NEWMP(m); m->dl = d; m->c = (Obj)ONE; NEXT(m) = 0;
                   3409:   MKDP(n,m,s); s->sugar = d->td;
                   3410:   *rp = s;
                   3411: }
                   3412:
                   3413: void dp_hm(DP p,DP *rp)
                   3414: {
                   3415:   MP m,mr;
                   3416:
                   3417:   if ( !p )
                   3418:     *rp = 0;
                   3419:   else {
                   3420:     m = BDY(p);
                   3421:     NEWMP(mr); mr->dl = m->dl; mr->c = m->c; NEXT(mr) = 0;
                   3422:     MKDP(p->nv,mr,*rp); (*rp)->sugar = mr->dl->td;   /* XXX */
                   3423:   }
                   3424: }
                   3425:
                   3426: void dp_ht(DP p,DP *rp)
                   3427: {
                   3428:   MP m,mr;
                   3429:
                   3430:   if ( !p )
                   3431:     *rp = 0;
                   3432:   else {
                   3433:     m = BDY(p);
                   3434:     NEWMP(mr); mr->dl = m->dl; mr->c = (Obj)ONE; NEXT(mr) = 0;
                   3435:     MKDP(p->nv,mr,*rp); (*rp)->sugar = mr->dl->td;   /* XXX */
                   3436:   }
                   3437: }
                   3438:
                   3439: void dpm_hm(DPM p,DPM *rp)
                   3440: {
                   3441:   DMM m,mr;
                   3442:
                   3443:   if ( !p )
                   3444:     *rp = 0;
                   3445:   else {
                   3446:     m = BDY(p);
                   3447:     NEWDMM(mr); mr->dl = m->dl; mr->c = m->c; mr->pos = m->pos; NEXT(mr) = 0;
                   3448:     MKDPM(p->nv,mr,*rp); (*rp)->sugar = mr->dl->td;   /* XXX */
                   3449:   }
                   3450: }
                   3451:
                   3452: void dpm_ht(DPM p,DPM *rp)
                   3453: {
                   3454:   DMM m,mr;
                   3455:
                   3456:   if ( !p )
                   3457:     *rp = 0;
                   3458:   else {
                   3459:     m = BDY(p);
                   3460:     NEWDMM(mr); mr->dl = m->dl; mr->pos = m->pos; mr->c = (Obj)ONE; NEXT(mr) = 0;
                   3461:     MKDPM(p->nv,mr,*rp); (*rp)->sugar = mr->dl->td;   /* XXX */
                   3462:   }
                   3463: }
                   3464:
                   3465:
                   3466: void dp_rest(DP p,DP *rp)
                   3467: {
                   3468:   MP m;
                   3469:
                   3470:   m = BDY(p);
                   3471:   if ( !NEXT(m) )
                   3472:     *rp = 0;
                   3473:   else {
                   3474:     MKDP(p->nv,NEXT(m),*rp);
                   3475:     if ( *rp )
                   3476:       (*rp)->sugar = p->sugar;
                   3477:   }
                   3478: }
                   3479:
                   3480: void dpm_rest(DPM p,DPM *rp)
                   3481: {
                   3482:   DMM m;
                   3483:
                   3484:   m = BDY(p);
                   3485:   if ( !NEXT(m) )
                   3486:     *rp = 0;
                   3487:   else {
                   3488:     MKDPM(p->nv,NEXT(m),*rp);
                   3489:     if ( *rp )
                   3490:       (*rp)->sugar = p->sugar;
                   3491:   }
                   3492: }
                   3493:
1.3       noro     3494: int dpm_getdeg(DPM p,int *r)
                   3495: {
                   3496:   int max,n,i,rank;
                   3497:   DMM m;
                   3498:   int *d;
                   3499:
                   3500:   if ( !p ) return 0;
                   3501:   n = p->nv;
                   3502:   max = 0;
                   3503:   rank = 0;
                   3504:   for ( m = BDY(p); m; m = NEXT(m) ) {
                   3505:     d = m->dl->d;
                   3506:     for ( i = 0; i < n; i++ )
                   3507:       if ( d[i] > max ) max = d[i];
                   3508:     rank = MAX(rank,m->pos);
                   3509:   }
                   3510:   *r = rank;
                   3511:   return max;
                   3512: }
                   3513:
1.1       noro     3514: DL lcm_of_DL(int nv,DL dl1,DL dl2,DL dl)
                   3515: {
                   3516:   register int i, *d1, *d2, *d, td;
                   3517:
                   3518:   if ( !dl ) NEWDL(dl,nv);
                   3519:   d = dl->d,  d1 = dl1->d,  d2 = dl2->d;
                   3520:   for ( td = 0, i = 0; i < nv; d1++, d2++, d++, i++ ) {
                   3521:     *d = *d1 > *d2 ? *d1 : *d2;
                   3522:     td += MUL_WEIGHT(*d,i);
                   3523:   }
                   3524:   dl->td = td;
                   3525:   return dl;
                   3526: }
                   3527:
                   3528: int dl_equal(int nv,DL dl1,DL dl2)
                   3529: {
                   3530:     register int *d1, *d2, n;
                   3531:
                   3532:     if ( dl1->td != dl2->td ) return 0;
                   3533:     for ( d1 = dl1->d, d2 = dl2->d, n = nv; --n >= 0; d1++, d2++ )
                   3534:         if ( *d1 != *d2 ) return 0;
                   3535:     return 1;
                   3536: }
                   3537:
                   3538: int dp_nt(DP p)
                   3539: {
                   3540:   int i;
                   3541:   MP m;
                   3542:
                   3543:   if ( !p )
                   3544:     return 0;
                   3545:   else {
                   3546:     for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
                   3547:     return i;
                   3548:   }
                   3549: }
                   3550:
                   3551: int dp_homogeneous(DP p)
                   3552: {
                   3553:   MP m;
                   3554:   int d;
                   3555:
                   3556:   if ( !p )
                   3557:     return 1;
                   3558:   else {
                   3559:     m = BDY(p);
                   3560:     d = m->dl->td;
                   3561:     m = NEXT(m);
                   3562:     for ( ; m; m = NEXT(m) ) {
                   3563:       if ( m->dl->td != d )
                   3564:         return 0;
                   3565:     }
                   3566:     return 1;
                   3567:   }
                   3568: }
                   3569:
                   3570: void _print_mp(int nv,MP m)
                   3571: {
                   3572:   int i;
                   3573:
                   3574:   if ( !m )
                   3575:     return;
                   3576:   for ( ; m; m = NEXT(m) ) {
1.3       noro     3577:     fprintf(stderr,"%ld<",ITOS(C(m)));
1.1       noro     3578:     for ( i = 0; i < nv; i++ ) {
                   3579:       fprintf(stderr,"%d",m->dl->d[i]);
                   3580:       if ( i != nv-1 )
                   3581:         fprintf(stderr," ");
                   3582:     }
1.3       noro     3583:     fprintf(stderr,">");
1.1       noro     3584:   }
                   3585:   fprintf(stderr,"\n");
                   3586: }
                   3587:
                   3588: static int cmp_mp_nvar;
                   3589:
                   3590: int comp_mp(MP *a,MP *b)
                   3591: {
                   3592:   return -(*cmpdl)(cmp_mp_nvar,(*a)->dl,(*b)->dl);
                   3593: }
                   3594:
                   3595: void dp_sort(DP p,DP *rp)
                   3596: {
                   3597:   MP t,mp,mp0;
                   3598:   int i,n;
                   3599:   DP r;
                   3600:   MP *w;
                   3601:
                   3602:   if ( !p ) {
                   3603:     *rp = 0;
                   3604:     return;
                   3605:   }
                   3606:   for ( t = BDY(p), n = 0; t; t = NEXT(t), n++ );
                   3607:   w = (MP *)ALLOCA(n*sizeof(MP));
                   3608:   for ( t = BDY(p), i = 0; i < n; t = NEXT(t), i++ )
                   3609:     w[i] = t;
                   3610:   cmp_mp_nvar = NV(p);
                   3611:   qsort(w,n,sizeof(MP),(int (*)(const void *,const void *))comp_mp);
                   3612:   mp0 = 0;
                   3613:   for ( i = n-1; i >= 0; i-- ) {
                   3614:     NEWMP(mp); mp->dl = w[i]->dl; C(mp) = C(w[i]);
                   3615:     NEXT(mp) = mp0; mp0 = mp;
                   3616:   }
                   3617:   MKDP(p->nv,mp0,r);
                   3618:   r->sugar = p->sugar;
                   3619:   *rp = r;
                   3620: }
                   3621:
                   3622: DP extract_initial_term_from_dp(DP p,int *weight,int n);
                   3623: LIST extract_initial_term(LIST f,int *weight,int n);
                   3624:
                   3625: DP extract_initial_term_from_dp(DP p,int *weight,int n)
                   3626: {
                   3627:   int w,t,i,top;
                   3628:   MP m,r0,r;
                   3629:   DP dp;
                   3630:
                   3631:   if ( !p ) return 0;
                   3632:   top = 1;
                   3633:   for ( m = BDY(p); m; m = NEXT(m) ) {
                   3634:     for ( i = 0, t = 0; i < n; i++ )
                   3635:       t += weight[i]*m->dl->d[i];
                   3636:     if ( top || t > w ) {
                   3637:       r0 = 0;
                   3638:       w = t;
                   3639:       top = 0;
                   3640:     }
                   3641:     if ( t == w ) {
                   3642:       NEXTMP(r0,r);
                   3643:       r->dl = m->dl;
                   3644:       r->c = m->c;
                   3645:     }
                   3646:   }
                   3647:   NEXT(r) = 0;
                   3648:   MKDP(p->nv,r0,dp);
                   3649:   return dp;
                   3650: }
                   3651:
                   3652: LIST extract_initial_term(LIST f,int *weight,int n)
                   3653: {
                   3654:   NODE nd,r0,r;
                   3655:   Obj p;
                   3656:   LIST l;
                   3657:
                   3658:   nd = BDY(f);
                   3659:   for ( r0 = 0; nd; nd = NEXT(nd) ) {
                   3660:     NEXTNODE(r0,r);
                   3661:     p = (Obj)BDY(nd);
                   3662:     BDY(r) = (pointer)extract_initial_term_from_dp((DP)p,weight,n);
                   3663:   }
                   3664:   if ( r0 ) NEXT(r) = 0;
                   3665:   MKLIST(l,r0);
                   3666:   return l;
                   3667: }
                   3668:
                   3669: LIST dp_initial_term(LIST f,struct order_spec *ord)
                   3670: {
                   3671:   int n,l,i;
                   3672:   struct weight_or_block *worb;
                   3673:   int *weight;
                   3674:
                   3675:   switch ( ord->id ) {
                   3676:     case 2: /* matrix order */
                   3677:       /* extract the first row */
                   3678:       n = ord->nv;
                   3679:       weight = ord->ord.matrix.matrix[0];
                   3680:       return extract_initial_term(f,weight,n);
                   3681:     case 3: /* composite order */
                   3682:       /* the first w_or_b */
                   3683:       worb = ord->ord.composite.w_or_b;
                   3684:       switch ( worb->type ) {
                   3685:         case IS_DENSE_WEIGHT:
                   3686:           n = worb->length;
                   3687:           weight = worb->body.dense_weight;
                   3688:           return extract_initial_term(f,weight,n);
                   3689:         case IS_SPARSE_WEIGHT:
                   3690:           n = ord->nv;
                   3691:           weight = (int *)ALLOCA(n*sizeof(int));
                   3692:           for ( i = 0; i < n; i++ ) weight[i] = 0;
                   3693:           l = worb->length;
                   3694:           for ( i = 0; i < l; i++ )
                   3695:             weight[worb->body.sparse_weight[i].pos]
                   3696:               =  worb->body.sparse_weight[i].value;
                   3697:           return extract_initial_term(f,weight,n);
                   3698:         default:
                   3699:           error("dp_initial_term : unsupported order");
                   3700:       }
                   3701:     default:
                   3702:       error("dp_initial_term : unsupported order");
                   3703:   }
1.3       noro     3704:   return 0;
1.1       noro     3705: }
                   3706:
                   3707: int highest_order_dp(DP p,int *weight,int n);
                   3708: LIST highest_order(LIST f,int *weight,int n);
                   3709:
                   3710: int highest_order_dp(DP p,int *weight,int n)
                   3711: {
                   3712:   int w,t,i,top;
                   3713:   MP m;
                   3714:
                   3715:   if ( !p ) return -1;
                   3716:   top = 1;
                   3717:   for ( m = BDY(p); m; m = NEXT(m) ) {
                   3718:     for ( i = 0, t = 0; i < n; i++ )
                   3719:       t += weight[i]*m->dl->d[i];
                   3720:     if ( top || t > w ) {
                   3721:       w = t;
                   3722:       top = 0;
                   3723:     }
                   3724:   }
                   3725:   return w;
                   3726: }
                   3727:
                   3728: LIST highest_order(LIST f,int *weight,int n)
                   3729: {
                   3730:   int h;
                   3731:   NODE nd,r0,r;
                   3732:   Obj p;
                   3733:   LIST l;
                   3734:   Z q;
                   3735:
                   3736:   nd = BDY(f);
                   3737:   for ( r0 = 0; nd; nd = NEXT(nd) ) {
                   3738:     NEXTNODE(r0,r);
                   3739:     p = (Obj)BDY(nd);
                   3740:     h = highest_order_dp((DP)p,weight,n);
1.2       noro     3741:     STOZ(h,q);
1.1       noro     3742:     BDY(r) = (pointer)q;
                   3743:   }
                   3744:   if ( r0 ) NEXT(r) = 0;
                   3745:   MKLIST(l,r0);
                   3746:   return l;
                   3747: }
                   3748:
                   3749: LIST dp_order(LIST f,struct order_spec *ord)
                   3750: {
                   3751:   int n,l,i;
                   3752:   struct weight_or_block *worb;
                   3753:   int *weight;
                   3754:
                   3755:   switch ( ord->id ) {
                   3756:     case 2: /* matrix order */
                   3757:       /* extract the first row */
                   3758:       n = ord->nv;
                   3759:       weight = ord->ord.matrix.matrix[0];
                   3760:       return highest_order(f,weight,n);
                   3761:     case 3: /* composite order */
                   3762:       /* the first w_or_b */
                   3763:       worb = ord->ord.composite.w_or_b;
                   3764:       switch ( worb->type ) {
                   3765:         case IS_DENSE_WEIGHT:
                   3766:           n = worb->length;
                   3767:           weight = worb->body.dense_weight;
                   3768:           return highest_order(f,weight,n);
                   3769:         case IS_SPARSE_WEIGHT:
                   3770:           n = ord->nv;
                   3771:           weight = (int *)ALLOCA(n*sizeof(int));
                   3772:           for ( i = 0; i < n; i++ ) weight[i] = 0;
                   3773:           l = worb->length;
                   3774:           for ( i = 0; i < l; i++ )
                   3775:             weight[worb->body.sparse_weight[i].pos]
                   3776:               =  worb->body.sparse_weight[i].value;
                   3777:           return highest_order(f,weight,n);
                   3778:         default:
                   3779:           error("dp_initial_term : unsupported order");
                   3780:       }
                   3781:     default:
                   3782:       error("dp_initial_term : unsupported order");
                   3783:   }
1.3       noro     3784:   return 0;
1.1       noro     3785: }
                   3786:
                   3787: int dpv_ht(DPV p,DP *h)
                   3788: {
                   3789:   int len,max,maxi,i,t;
                   3790:   DP *e;
                   3791:   MP m,mr;
                   3792:
                   3793:   len = p->len;
                   3794:   e = p->body;
                   3795:   max = -1;
                   3796:   maxi = -1;
                   3797:   for ( i = 0; i < len; i++ )
                   3798:     if ( e[i] && (t = BDY(e[i])->dl->td) > max ) {
                   3799:       max = t;
                   3800:       maxi = i;
                   3801:     }
                   3802:   if ( max < 0 ) {
                   3803:     *h = 0;
                   3804:     return -1;
                   3805:   } else {
                   3806:     m = BDY(e[maxi]);
                   3807:     NEWMP(mr); mr->dl = m->dl; mr->c = (Obj)ONE; NEXT(mr) = 0;
                   3808:     MKDP(e[maxi]->nv,mr,*h); (*h)->sugar = mr->dl->td;  /* XXX */
                   3809:     return maxi;
                   3810:   }
                   3811: }
                   3812:
                   3813: /* return 1 if 0 <_w1 v && v <_w2 0 */
                   3814:
                   3815: int in_c12(int n,int *v,int row1,int **w1,int row2, int **w2)
                   3816: {
                   3817:   int t1,t2;
                   3818:
                   3819:   t1 = compare_zero(n,v,row1,w1);
                   3820:   t2 = compare_zero(n,v,row2,w2);
                   3821:   if ( t1 > 0 && t2 < 0 ) return 1;
                   3822:   else return 0;
                   3823: }
                   3824:
                   3825: /* 0 < u => 1, 0 > u => -1 */
                   3826:
                   3827: int compare_zero(int n,int *u,int row,int **w)
                   3828: {
                   3829:   int i,j,t;
                   3830:   int *wi;
                   3831:
                   3832:   for ( i = 0; i < row; i++ ) {
                   3833:     wi = w[i];
                   3834:     for ( j = 0, t = 0; j < n; j++ ) t += u[j]*wi[j];
                   3835:     if ( t > 0 ) return 1;
                   3836:     else if ( t < 0 ) return -1;
                   3837:   }
                   3838:   return 0;
                   3839: }
                   3840:
                   3841: /* functions for generic groebner walk */
                   3842: /* u=0 means u=-infty */
                   3843:
                   3844: int compare_facet_preorder(int n,int *u,int *v,
                   3845:   int row1,int **w1,int row2,int **w2)
                   3846: {
                   3847:   int i,j,s,t,tu,tv;
                   3848:   int *w2i,*uv;
                   3849:
                   3850:   if ( !u ) return 1;
                   3851:   uv = W_ALLOC(n);
                   3852:   for ( i = 0; i < row2; i++ ) {
                   3853:     w2i = w2[i];
                   3854:     for ( j = 0, tu = tv = 0; j < n; j++ )
1.3       noro     3855:       if ( (s = w2i[j]) != 0 ) {
1.1       noro     3856:         tu += s*u[j]; tv += s*v[j];
                   3857:       }
                   3858:     for ( j = 0; j < n; j++ ) uv[j] = u[j]*tv-v[j]*tu;
                   3859:     t = compare_zero(n,uv,row1,w1);
                   3860:     if ( t > 0 ) return 1;
                   3861:     else if ( t < 0 ) return 0;
                   3862:   }
                   3863:   return 1;
                   3864: }
                   3865:
                   3866: Q inner_product_with_small_vector(VECT w,int *v)
                   3867: {
                   3868:   int n,i;
                   3869:   Z q;
                   3870:   Q s,t,u;
                   3871:
                   3872:   n = w->len;
                   3873:   s = 0;
                   3874:   for ( i = 0; i < n; i++ ) {
1.2       noro     3875:     STOZ(v[i],q); mulq((Q)w->body[i],(Q)q,&t); addq(t,s,&u); s = u;
1.1       noro     3876:   }
                   3877:   return s;
                   3878: }
                   3879:
                   3880: Q compute_last_t(NODE g,NODE gh,Q t,VECT w1,VECT w2,NODE *homo,VECT *wp)
                   3881: {
                   3882:   int n,i;
                   3883:   int *wt;
                   3884:   Q last,d1,d2,dn,nm,s,t1;
                   3885:   VECT wd,wt1,wt2,w;
                   3886:   NODE tg,tgh;
                   3887:   MP f;
                   3888:   int *h;
                   3889:   NODE r0,r;
                   3890:   MP m0,m;
                   3891:   DP d;
                   3892:
                   3893:   n = w1->len;
                   3894:   wt = W_ALLOC(n);
                   3895:   last = (Q)ONE;
                   3896:   /* t1 = 1-t */
                   3897:   for ( tg = g, tgh = gh; tg; tg = NEXT(tg), tgh = NEXT(tgh ) ) {
                   3898:     f = BDY((DP)BDY(tg));
                   3899:     h = BDY((DP)BDY(tgh))->dl->d;
                   3900:     for ( ; f; f = NEXT(f) ) {
                   3901:       for ( i = 0; i < n; i++ ) wt[i] = h[i]-f->dl->d[i];
                   3902:       for ( i = 0; i < n && !wt[i]; i++ );
                   3903:       if ( i == n ) continue;
                   3904:       d1 = inner_product_with_small_vector(w1,wt);
                   3905:       d2 = inner_product_with_small_vector(w2,wt);
                   3906:       nm = d1; subq(d1,d2,&dn);
                   3907:       /* if d1=d2 then nothing happens */
                   3908:       if ( !dn ) continue;
                   3909:       /* s satisfies ds = 0*/
                   3910:       divq(nm,dn,&s);
                   3911:
                   3912:       if ( cmpq(s,t) > 0 && cmpq(s,last) < 0 )
                   3913:         last = s;
                   3914:       else if ( !cmpq(s,t) ) {
                   3915:         if ( cmpq(d2,0) < 0 ) {
                   3916:           last = t;
                   3917:           break;
                   3918:         }
                   3919:       }
                   3920:     }
                   3921:   }
                   3922:   nmq(last,(Z *)&nm);
                   3923:   dnq(last,(Z *)&dn);
                   3924:   /* (1-n/d)*w1+n/d*w2 -> w=(d-n)*w1+n*w2 */
                   3925:   subq(dn,nm,&t1); mulvect(CO,(Obj)w1,(Obj)t1,(Obj *)&wt1);
                   3926:   mulvect(CO,(Obj)w2,(Obj)nm,(Obj *)&wt2); addvect(CO,wt1,wt2,&w);
                   3927:
                   3928:   r0 = 0;
                   3929:   for ( tg = g, tgh = gh; tg; tg = NEXT(tg), tgh = NEXT(tgh ) ) {
                   3930:     f = BDY((DP)BDY(tg));
                   3931:     h = BDY((DP)BDY(tgh))->dl->d;
                   3932:     for ( m0 = 0; f; f = NEXT(f) ) {
                   3933:       for ( i = 0; i < n; i++ ) wt[i] = h[i]-f->dl->d[i];
                   3934:       for ( i = 0; i < n && !wt[i]; i++ );
                   3935:       if ( !inner_product_with_small_vector(w,wt) ) {
                   3936:         NEXTMP(m0,m); m->c = f->c; m->dl = f->dl;
                   3937:       }
                   3938:     }
                   3939:     NEXT(m) = 0;
                   3940:     MKDP(((DP)BDY(tg))->nv,m0,d);  d->sugar = ((DP)BDY(tg))->sugar;
                   3941:     NEXTNODE(r0,r); BDY(r) = (pointer)d;
                   3942:   }
                   3943:   NEXT(r) = 0;
                   3944:   *homo = r0;
                   3945:   *wp = w;
                   3946:   return last;
                   3947: }
                   3948:
                   3949: /* return 0 if last_w = infty */
                   3950:
                   3951: NODE compute_last_w(NODE g,NODE gh,int n,int **w,
                   3952:   int row1,int **w1,int row2,int **w2)
                   3953: {
                   3954:   DP d;
                   3955:   MP f,m0,m;
                   3956:   int *wt,*v,*h;
                   3957:   NODE t,s,n0,tn,n1,r0,r;
                   3958:   int i;
                   3959:
                   3960:   wt = W_ALLOC(n);
                   3961:   n0 = 0;
                   3962:   for ( t = g, s = gh; t; t = NEXT(t), s = NEXT(s) ) {
                   3963:     f = BDY((DP)BDY(t));
                   3964:     h = BDY((DP)BDY(s))->dl->d;
                   3965:     for ( ; f; f = NEXT(f) ) {
                   3966:       for ( i = 0; i < n; i++ ) wt[i] = h[i]-f->dl->d[i];
                   3967:       for ( i = 0; i < n && !wt[i]; i++ );
                   3968:       if ( i == n ) continue;
                   3969:
                   3970:       if ( in_c12(n,wt,row1,w1,row2,w2) &&
                   3971:         compare_facet_preorder(n,*w,wt,row1,w1,row2,w2) ) {
                   3972:         v = (int *)MALLOC_ATOMIC(n*sizeof(int));
                   3973:         for ( i = 0; i < n; i++ ) v[i] = wt[i];
                   3974:         MKNODE(n1,v,n0); n0 = n1;
                   3975:       }
                   3976:     }
                   3977:   }
                   3978:   if ( !n0 ) return 0;
                   3979:   for ( t = n0; t; t = NEXT(t) ) {
                   3980:     v = (int *)BDY(t);
                   3981:     for ( s = n0; s; s = NEXT(s) )
                   3982:       if ( !compare_facet_preorder(n,v,(int *)BDY(s),row1,w1,row2,w2) )
                   3983:         break;
                   3984:     if ( !s ) {
                   3985:       *w = v;
                   3986:       break;
                   3987:     }
                   3988:   }
                   3989:   if ( !t )
                   3990:     error("compute_last_w : cannot happen");
                   3991:   r0 = 0;
                   3992:   for ( t = g, s = gh; t; t = NEXT(t), s = NEXT(s) ) {
                   3993:     f = BDY((DP)BDY(t));
                   3994:     h = BDY((DP)BDY(s))->dl->d;
                   3995:     for ( m0 = 0; f; f = NEXT(f) ) {
                   3996:       for ( i = 0; i < n; i++ ) wt[i] = h[i]-f->dl->d[i];
                   3997:       for ( i = 0; i < n && !wt[i]; i++ );
                   3998:       if ( i == n  ||
                   3999:         (compare_facet_preorder(n,wt,*w,row1,w1,row2,w2)
                   4000:         && compare_facet_preorder(n,*w,wt,row1,w1,row2,w2)) ) {
                   4001:         NEXTMP(m0,m); m->c = f->c; m->dl = f->dl;
                   4002:       }
                   4003:     }
                   4004:     NEXT(m) = 0;
                   4005:     MKDP(((DP)BDY(t))->nv,m0,d);  d->sugar = ((DP)BDY(t))->sugar;
                   4006:     NEXTNODE(r0,r); BDY(r) = (pointer)d;
                   4007:   }
                   4008:   NEXT(r) = 0;
                   4009:   return r0;
                   4010: }
                   4011:
                   4012: /* compute a sufficient set of d(f)=u-v */
                   4013:
                   4014: NODE compute_essential_df(DP *g,DP *gh,int ng)
                   4015: {
                   4016:   int nv,i,j,k,t,lj;
                   4017:   NODE r,r1,ri,rt,r0;
                   4018:   MP m;
                   4019:   MP *mj;
                   4020:   DL di,hj,dl,dlt;
                   4021:   int *d,*dt;
                   4022:   LIST l;
                   4023:   Z q;
                   4024:
                   4025:   nv = g[0]->nv;
                   4026:   r = 0;
                   4027:   for ( j = 0; j < ng; j++ ) {
                   4028:     for ( m = BDY(g[j]), lj = 0; m; m = NEXT(m), lj++ );
                   4029:     mj = (MP *)ALLOCA(lj*sizeof(MP));
                   4030:     for ( m = BDY(g[j]), k = 0; m; m = NEXT(m), k++ )
                   4031:       mj[k] = m;
                   4032:     for ( i = 0; i < lj; i++ ) {
                   4033:       for ( di = mj[i]->dl, k = i+1; k < lj; k++ )
                   4034:         if ( _dl_redble(di,mj[k]->dl,nv) ) break;
                   4035:       if ( k < lj ) mj[i] = 0;
                   4036:     }
                   4037:     hj = BDY(gh[j])->dl;
                   4038:     _NEWDL(dl,nv); d = dl->d;
                   4039:     r0 = r;
                   4040:     for ( i = 0; i < lj; i++ ) {
                   4041:       if ( mj[i] && !dl_equal(nv,di=mj[i]->dl,hj) ) {
                   4042:         for ( k = 0, t = 0; k < nv; k++ ) {
                   4043:           d[k] = hj->d[k]-di->d[k];
                   4044:           t += d[k];
                   4045:         }
                   4046:         dl->td = t;
                   4047: #if 1
                   4048:         for ( rt = r0; rt; rt = NEXT(rt) ) {
                   4049:           dlt = (DL)BDY(rt);
                   4050:           if ( dlt->td != dl->td ) continue;
                   4051:           for ( dt = dlt->d, k = 0; k < nv; k++ )
                   4052:             if ( d[k] != dt[k] ) break;
                   4053:           if ( k == nv ) break;
                   4054:         }
                   4055: #else
                   4056:         rt = 0;
                   4057: #endif
                   4058:         if ( !rt ) {
                   4059:           MKNODE(r1,dl,r); r = r1;
                   4060:           _NEWDL(dl,nv); d = dl->d;
                   4061:         }
                   4062:       }
                   4063:     }
                   4064:   }
                   4065:   for ( rt = r; rt; rt = NEXT(rt) ) {
                   4066:     dl = (DL)BDY(rt); d = dl->d;
                   4067:     ri = 0;
                   4068:     for ( k = nv-1; k >= 0; k-- ) {
1.2       noro     4069:       STOZ(d[k],q);
1.1       noro     4070:       MKNODE(r1,q,ri); ri = r1;
                   4071:     }
                   4072:     MKNODE(r1,0,ri); MKLIST(l,r1);
                   4073:     BDY(rt) = (pointer)l;
                   4074:   }
                   4075:   return r;
                   4076: }
                   4077:
                   4078: int comp_bits_divisible(int *a,int *b,int n)
                   4079: {
                   4080:   int bpi,i,wi,bi;
                   4081:
                   4082:   bpi = (sizeof(int)/sizeof(char))*8;
                   4083:   for ( i = 0; i < n; i++ ) {
                   4084:     wi = i/bpi; bi = i%bpi;
                   4085:     if ( !(a[wi]&(1<<bi)) && (b[wi]&(1<<bi)) ) return 0;
                   4086:   }
                   4087:   return 1;
                   4088: }
                   4089:
                   4090: int comp_bits_lex(int *a,int *b,int n)
                   4091: {
                   4092:   int bpi,i,wi,ba,bb,bi;
                   4093:
                   4094:   bpi = (sizeof(int)/sizeof(char))*8;
                   4095:   for ( i = 0; i < n; i++ ) {
                   4096:     wi = i/bpi; bi = i%bpi;
                   4097:     ba = (a[wi]&(1<<bi))?1:0;
                   4098:     bb = (b[wi]&(1<<bi))?1:0;
                   4099:     if ( ba > bb ) return 1;
                   4100:     else if ( ba < bb ) return -1;
                   4101:   }
                   4102:   return 0;
                   4103: }
                   4104:
                   4105: NODE mono_raddec(NODE ideal)
                   4106: {
                   4107:   DP p;
                   4108:   int nv,w,i,bpi,di,c,len;
                   4109:   int *d,*s,*u,*new;
                   4110:   NODE t,t1,v,r,rem,prev;
                   4111:
                   4112:   if( !ideal ) return 0;
                   4113:   p = (DP)BDY(ideal);
                   4114:   nv = NV(p);
                   4115:   bpi = (sizeof(int)/sizeof(char))*8;
                   4116:   w = (nv+(bpi-1))/bpi;
                   4117:   d = p->body->dl->d;
                   4118:   if ( !NEXT(ideal) )  {
                   4119:     for ( t = 0, i = nv-1; i >= 0; i-- ) {
                   4120:       if ( d[i] ) {
                   4121:         s = (int *)CALLOC(w,sizeof(int));
                   4122:         s[i/bpi] |= 1<<(i%bpi);
                   4123:         MKNODE(t1,s,t);
                   4124:         t = t1;
                   4125:       }
                   4126:     }
                   4127:     return t;
                   4128:   }
                   4129:   rem = mono_raddec(NEXT(ideal));
                   4130:   r = 0;
                   4131:   len = w*sizeof(int);
                   4132:   u = (int *)CALLOC(w,sizeof(int));
                   4133:   for ( i = nv-1; i >= 0; i-- ) {
                   4134:     if ( d[i] ) {
                   4135:       for ( t = rem; t; t = NEXT(t) ) {
                   4136:         bcopy((char *)BDY(t),(char *)u,len);
                   4137:         u[i/bpi] |= 1<<(i%bpi);
                   4138:         for ( v = r; v; v = NEXT(v) ) {
                   4139:           if ( comp_bits_divisible(u,(int *)BDY(v),nv) ) break;
                   4140:         }
                   4141:         if ( v ) continue;
                   4142:         for ( v = r, prev = 0; v; v = NEXT(v) ) {
                   4143:           if ( comp_bits_divisible((int *)BDY(v),u,nv) ) {
                   4144:             if ( prev ) NEXT(prev) = NEXT(v);
                   4145:             else r = NEXT(r);
                   4146:           } else prev =v;
                   4147:         }
                   4148:         for ( v = r, prev = 0; v; prev = v, v = NEXT(v) ) {
                   4149:           if ( comp_bits_lex(u,(int *)BDY(v),nv) < 0 ) break;
                   4150:         }
                   4151:         new = (int *)CALLOC(w,sizeof(int));
                   4152:         bcopy((char *)u,(char *)new,len);
                   4153:         MKNODE(t1,new,v);
                   4154:         if ( prev ) NEXT(prev) = t1;
                   4155:         else r = t1;
                   4156:       }
                   4157:     }
                   4158:   }
                   4159:   return r;
                   4160: }

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