[BACK]Return to gr.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / builtin

Annotation of OpenXM_contrib2/asir2000/builtin/gr.c, Revision 1.19

1.8       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
1.9       noro       26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.8       noro       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.19    ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/builtin/gr.c,v 1.18 2001/01/12 09:03:33 noro Exp $
1.8       noro       49: */
1.1       noro       50: #include "ca.h"
                     51: #include "parse.h"
                     52: #include "base.h"
                     53: #include "ox.h"
                     54:
                     55: #define ITOS(p) (((unsigned int)(p))&0x7fffffff)
                     56: #define STOI(i) ((P)((unsigned int)(i)|0x80000000))
                     57:
                     58: #define NEXTVL(r,c) \
                     59: if(!(r)){NEWVL(r);(c)=(r);}else{NEWVL(NEXT(c));(c)=NEXT(c);}
                     60:
                     61: #define HMAG(p) (p_mag(BDY(p)->c))
                     62:
                     63: struct dp_pairs {
                     64:        int dp1, dp2;
                     65:        DL lcm;
                     66:        int sugar;
                     67:        struct dp_pairs *next;
                     68: };
                     69:
                     70: typedef struct dp_pairs *DP_pairs;
                     71:
                     72: #define NEWDPP(a) ((a)=(DP_pairs)MALLOC(sizeof(struct dp_pairs)))
                     73:
                     74: #define NEXTDPP(r,c) \
                     75: if(!(r)){NEWDPP(r);(c)=(r);}else{NEWDPP(NEXT(c));(c)=NEXT(c);}
                     76:
1.14      noro       77: struct oEGT eg_nf,eg_nfm;
                     78: struct oEGT eg_znfm,eg_pz,eg_np,eg_ra,eg_mc,eg_gc;
1.1       noro       79: int TP,NBP,NMP,NFP,NDP,ZR,NZR;
                     80:
                     81: #define NEWDP_pairs ((DP_pairs)MALLOC(sizeof(struct dp_pairs)))
                     82:
                     83: extern int (*cmpdl)();
1.5       noro       84: extern int do_weyl;
1.1       noro       85:
1.13      noro       86: extern DP_Print;
                     87:
1.14      noro       88: void dp_imul_d(DP,Q,DP *);
1.1       noro       89: void print_stat(void);
                     90: void init_stat(void);
                     91: int dp_load_t(int,DP *);
                     92: void dp_load(int,DP *);
1.7       noro       93: void dp_save(int,Obj,char *);
1.1       noro       94: void dp_make_flaglist(LIST *);
                     95: void dp_set_flag(Obj,Obj);
                     96: int membercheck(NODE,NODE);
                     97: int gbcheck(NODE);
                     98: int dl_redble(DL,DL);
                     99: NODE remove_reducibles(NODE,int);
                    100: NODE updbase(NODE,int);
                    101: DP_pairs criterion_F(DP_pairs);
                    102: int criterion_2(int,int);
                    103: static DP_pairs collect_pairs_of_hdlcm(DP_pairs,DP_pairs *);
                    104: DP_pairs criterion_M(DP_pairs);
                    105: DP_pairs criterion_B(DP_pairs,int);
                    106: DP_pairs newpairs(NODE,int);
                    107: DP_pairs updpairs(DP_pairs,NODE,int);
1.16      noro      108: void _dp_nf(NODE,DP,DP *,int,DP *);
                    109: void _dp_nf_ptozp(NODE,DP,DP *,int,int,DP *);
1.1       noro      110: NODE gb_mod(NODE,int);
                    111: NODE gbd(NODE,int,NODE,NODE);
                    112: NODE gb(NODE,int,NODE);
                    113: NODE gb_f4(NODE);
                    114: NODE gb_f4_mod(NODE,int);
                    115: DP_pairs minp(DP_pairs, DP_pairs *);
                    116: void minsugar(DP_pairs,DP_pairs *,DP_pairs *);
                    117: NODE append_one(NODE,int);
                    118: void reducebase_dehomo(NODE,NODE *);
                    119: int newps_mod(DP,int);
                    120: int newps_nosave(DP,int,NODE);
                    121: int newps(DP,int,NODE);
                    122: void reduceall_mod(NODE,int,NODE *);
                    123: void reduceall(NODE,NODE *);
                    124: NODE NODE_sortbi(NODE,int);
                    125: NODE NODE_sortbi_insert(int, NODE,int);
                    126: NODE NODE_sortb(NODE,int);
                    127: NODE NODE_sortb_insert(DP,NODE,int);
                    128: void prim_part(DP,int,DP *);
                    129: void setup_arrays(NODE,int,NODE *);
                    130: int validhc(P,int,NODE);
                    131: void vlminus(VL,VL,VL *);
                    132: void printsubst(NODE);
                    133: void makesubst(VL,NODE *);
                    134: void pltovl(LIST,VL *);
                    135: void printdl(DL);
                    136: int DPPlength(DP_pairs);
                    137: void dp_gr_mod_main(LIST,LIST,Num,int,struct order_spec *,LIST *);
                    138: void dp_gr_main(LIST,LIST,Num,int,struct order_spec *,LIST *);
                    139: void dp_f4_main(LIST,LIST,struct order_spec *,LIST *);
                    140: void dp_f4_mod_main(LIST,LIST,int,struct order_spec *,LIST *);
                    141: double get_rtime();
                    142: void _dpmod_to_vect(DP,DL *,int *);
                    143: void dp_to_vect(DP,DL *,Q *);
1.12      noro      144: NODE dp_dllist(DP f);
                    145: NODE symb_merge(NODE,NODE,int),_symb_merge(NODE,NODE,int);
1.1       noro      146: extern int dp_nelim;
                    147: extern int dp_fcoeffs;
                    148: static DP *ps,*psm;
                    149: static DL *psh;
                    150: static P *psc;
                    151:
                    152: static int *pss;
                    153: static int psn,pslen;
1.16      noro      154: static int NVars,CNVars;
1.1       noro      155: static VL VC;
1.14      noro      156:
1.16      noro      157: int PCoeffs;
1.14      noro      158: int DP_Print = 0;
                    159: int DP_Multiple = 0;
1.16      noro      160: int DP_NFStat = 0;
1.14      noro      161: LIST Dist = 0;
                    162: int NoGCD = 0;
                    163: int GenTrace = 0;
                    164: int OXCheck = -1;
                    165:
1.1       noro      166: static int NoSugar = 0;
                    167: static int NoCriB = 0;
                    168: static int NoGC = 0;
                    169: static int NoMC = 0;
                    170: static int NoRA = 0;
1.12      noro      171: static int DP_PrintShort = 0;
1.1       noro      172: static int ShowMag = 0;
                    173: static int Stat = 0;
                    174: static int Denominator = 1;
                    175: static int Top = 0;
                    176: static int Reverse = 0;
                    177: static int Max_mag = 0;
                    178: static char *Demand = 0;
                    179: static int PtozpRA = 0;
1.14      noro      180:
1.1       noro      181: int doing_f4;
1.7       noro      182: NODE TraceList;
1.1       noro      183:
                    184: int eqdl(nv,dl1,dl2)
                    185: int nv;
                    186: DL dl1,dl2;
                    187: {
                    188:        int i;
                    189:        int *b1,*b2;
                    190:
                    191:        if ( dl1->td != dl2->td )
                    192:                return 0;
                    193:        for ( i = 0, b1 = dl1->d, b2 = dl2->d; i < nv; i++ )
                    194:                if ( b1[i] != b2[i] )
                    195:                        break;
                    196:        if ( i == nv )
                    197:                return 1;
                    198:        else
                    199:                return 0;
                    200: }
                    201:
1.5       noro      202: /* b[] should be cleared */
                    203:
1.1       noro      204: void _dpmod_to_vect(f,at,b)
                    205: DP f;
                    206: DL *at;
                    207: int *b;
                    208: {
                    209:        int i,nv;
                    210:        MP m;
                    211:
                    212:        nv = f->nv;
                    213:        for ( m = BDY(f), i = 0; m; m = NEXT(m), i++ ) {
                    214:                for ( ; !eqdl(nv,m->dl,at[i]); i++ );
                    215:                b[i] = ITOS(m->c);
                    216:        }
                    217: }
                    218:
                    219: void dp_to_vect(f,at,b)
                    220: DP f;
                    221: DL *at;
                    222: Q *b;
                    223: {
                    224:        int i,nv;
                    225:        MP m;
                    226:
                    227:        nv = f->nv;
                    228:        for ( m = BDY(f), i = 0; m; m = NEXT(m), i++ ) {
                    229:                for ( ; !eqdl(nv,m->dl,at[i]); i++ );
                    230:                b[i] =(Q)m->c;
                    231:        }
                    232: }
                    233:
1.2       noro      234: NODE dp_dllist(f)
1.1       noro      235: DP f;
                    236: {
1.2       noro      237:        MP m;
                    238:        NODE mp,mp0;
1.1       noro      239:
1.2       noro      240:        if ( !f )
                    241:                return 0;
1.1       noro      242:        mp0 = 0;
                    243:        for ( m = BDY(f); m; m = NEXT(m) ) {
1.2       noro      244:                NEXTNODE(mp0,mp); BDY(mp) = (pointer)m->dl;
1.1       noro      245:        }
                    246:        NEXT(mp) = 0;
1.2       noro      247:        return mp0;
                    248: }
                    249:
                    250: void pdl(f)
                    251: NODE f;
                    252: {
                    253:        while ( f ) {
                    254:                printdl(BDY(f)); f = NEXT(f);
                    255:        }
                    256:        fflush(stdout);
                    257:        printf("\n");
1.1       noro      258: }
                    259:
                    260: void dp_gr_main(f,v,homo,modular,ord,rp)
                    261: LIST f,v;
                    262: Num homo;
                    263: int modular;
                    264: struct order_spec *ord;
                    265: LIST *rp;
                    266: {
                    267:        int i,mindex,m,nochk;
                    268:        struct order_spec ord1;
                    269:        VL fv,vv,vc;
                    270:        NODE fd,fd0,fi,fi0,r,r0,t,subst,x,s,xx;
                    271:
                    272:        mindex = 0; nochk = 0; dp_fcoeffs = 0;
                    273:        get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&vc);
                    274:        NVars = length((NODE)vv); PCoeffs = vc ? 1 : 0; VC = vc;
                    275:        CNVars = homo ? NVars+1 : NVars;
                    276:        if ( ord->id && NVars != ord->nv )
                    277:                error("dp_gr_main : invalid order specification");
                    278:        initd(ord);
                    279:        if ( homo ) {
                    280:                homogenize_order(ord,NVars,&ord1);
                    281:                for ( fd0 = fi0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                    282:                        NEXTNODE(fd0,fd); NEXTNODE(fi0,fi);
                    283:                        ptod(CO,vv,(P)BDY(t),(DP *)&BDY(fi)); dp_homo((DP)BDY(fi),(DP *)&BDY(fd));
                    284:                }
                    285:                if ( fd0 ) NEXT(fd) = 0;
                    286:                if ( fi0 ) NEXT(fi) = 0;
                    287:                initd(&ord1);
                    288:        } else {
                    289:                for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                    290:                        NEXTNODE(fd0,fd); ptod(CO,vv,(P)BDY(t),(DP *)&BDY(fd));
                    291:                }
                    292:                if ( fd0 ) NEXT(fd) = 0;
                    293:                fi0 = fd0;
                    294:        }
                    295:        if ( modular < 0 ) {
                    296:                modular = -modular; nochk = 1;
                    297:        }
                    298:        if ( modular )
1.19    ! noro      299:                m = modular > 1 ? modular : get_lprime(mindex);
1.1       noro      300:        else
                    301:                m = 0;
                    302:        makesubst(vc,&subst);
                    303:        setup_arrays(fd0,0,&s);
                    304:        init_stat();
                    305:        while ( 1 ) {
                    306:                if ( homo ) {
                    307:                        initd(&ord1); CNVars = NVars+1;
                    308:                }
1.12      noro      309:                if ( DP_Print && modular ) {
1.1       noro      310:                        fprintf(asir_out,"mod= %d, eval = ",m); printsubst(subst);
                    311:                }
                    312:                x = gb(s,m,subst);
                    313:                if ( x ) {
                    314:                        if ( homo ) {
                    315:                                reducebase_dehomo(x,&xx); x = xx;
                    316:                                initd(ord); CNVars = NVars;
                    317:                        }
                    318:                        reduceall(x,&xx); x = xx;
                    319:                        if ( modular ) {
                    320:                                if ( nochk || (membercheck(fi0,x) && gbcheck(x)) )
                    321:                                        break;
                    322:                        } else
                    323:                                break;
                    324:                }
                    325:                if ( modular )
                    326:                        if ( modular > 1 ) {
                    327:                                *rp = 0; return;
                    328:                        } else
1.19    ! noro      329:                                m = get_lprime(++mindex);
1.1       noro      330:                makesubst(vc,&subst);
                    331:                psn = length(s);
                    332:                for ( i = psn; i < pslen; i++ ) {
                    333:                        pss[i] = 0; psh[i] = 0; psc[i] = 0; ps[i] = 0;
                    334:                }
                    335:        }
                    336:        for ( r0 = 0; x; x = NEXT(x) ) {
                    337:                NEXTNODE(r0,r); dp_load((int)BDY(x),&ps[(int)BDY(x)]);
                    338:                dtop(CO,vv,ps[(int)BDY(x)],(P *)&BDY(r));
                    339:        }
                    340:        if ( r0 ) NEXT(r) = 0;
                    341:        MKLIST(*rp,r0);
                    342:        print_stat();
                    343:        if ( ShowMag )
                    344:                fprintf(asir_out,"\nMax_mag=%d\n",Max_mag);
                    345: }
                    346:
                    347: void dp_gr_mod_main(f,v,homo,m,ord,rp)
                    348: LIST f,v;
                    349: Num homo;
                    350: int m;
                    351: struct order_spec *ord;
                    352: LIST *rp;
                    353: {
                    354:        struct order_spec ord1;
                    355:        VL fv,vv,vc;
                    356:        NODE fd,fd0,r,r0,t,x,s,xx;
                    357:        DP a,b,c;
1.17      noro      358: extern struct oEGT eg_red_mod;
1.1       noro      359:
                    360:        get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&vc);
                    361:        NVars = length((NODE)vv); PCoeffs = vc ? 1 : 0; VC = vc;
                    362:        CNVars = homo ? NVars+1 : NVars;
                    363:        if ( ord->id && NVars != ord->nv )
                    364:                error("dp_gr_mod_main : invalid order specification");
                    365:        initd(ord);
                    366:        if ( homo ) {
                    367:                for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                    368:                        ptod(CO,vv,(P)BDY(t),&a); dp_homo(a,&b);
                    369:                        if ( PCoeffs )
                    370:                                dp_mod(b,m,0,&c);
                    371:                        else
                    372:                                _dp_mod(b,m,(NODE)0,&c);
                    373:                        if ( c ) {
                    374:                                NEXTNODE(fd0,fd); BDY(fd) = (pointer)c;
                    375:                        }
                    376:                }
                    377:                homogenize_order(ord,NVars,&ord1); initd(&ord1);
                    378:        } else {
                    379:                for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                    380:                        ptod(CO,vv,(P)BDY(t),&b);
                    381:                        if ( PCoeffs )
                    382:                                dp_mod(b,m,0,&c);
                    383:                        else
                    384:                                _dp_mod(b,m,0,&c);
                    385:                        if ( c ) {
                    386:                                NEXTNODE(fd0,fd); BDY(fd) = (pointer)c;
                    387:                        }
                    388:                }
                    389:        }
                    390:        if ( fd0 ) NEXT(fd) = 0;
                    391:        setup_arrays(fd0,m,&s);
                    392:        init_stat();
                    393:        if ( homo ) {
                    394:                initd(&ord1); CNVars = NVars+1;
                    395:        }
1.17      noro      396: /* init_eg(&eg_red_mod); */
1.1       noro      397:        x = gb_mod(s,m);
1.17      noro      398: /* print_eg("Red_mod",&eg_red_mod); */
1.1       noro      399:        if ( homo ) {
                    400:                reducebase_dehomo(x,&xx); x = xx;
                    401:                initd(ord); CNVars = NVars;
                    402:        }
                    403:        reduceall_mod(x,m,&xx); x = xx;
                    404:        if ( PCoeffs )
                    405:                for ( r0 = 0; x; x = NEXT(x) ) {
                    406:                        NEXTNODE(r0,r); mdtop(CO,m,vv,ps[(int)BDY(x)],(P *)&BDY(r));
                    407:                }
                    408:        else
                    409:                for ( r0 = 0; x; x = NEXT(x) ) {
                    410:                        NEXTNODE(r0,r); _dtop_mod(CO,vv,ps[(int)BDY(x)],(P *)&BDY(r));
                    411:                }
                    412:        print_stat();
                    413:        if ( r0 ) NEXT(r) = 0;
                    414:        MKLIST(*rp,r0);
                    415: }
                    416:
                    417: void dp_f4_main(f,v,ord,rp)
                    418: LIST f,v;
                    419: struct order_spec *ord;
                    420: LIST *rp;
                    421: {
                    422:        int i,mindex,m,nochk;
                    423:        struct order_spec ord1;
                    424:        VL fv,vv,vc;
                    425:        NODE fd,fd0,fi,fi0,r,r0,t,subst,x,s,xx;
                    426:
                    427:        dp_fcoeffs = 0;
                    428:        get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&vc);
                    429:        NVars = length((NODE)vv); PCoeffs = vc ? 1 : 0; VC = vc;
                    430:        CNVars = NVars;
                    431:        if ( ord->id && NVars != ord->nv )
                    432:                error("dp_f4_main : invalid order specification");
                    433:        initd(ord);
                    434:        for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                    435:                NEXTNODE(fd0,fd); ptod(CO,vv,(P)BDY(t),(DP *)&BDY(fd));
                    436:        }
                    437:        if ( fd0 ) NEXT(fd) = 0;
                    438:        setup_arrays(fd0,0,&s);
                    439:        x = gb_f4(s);
                    440:        reduceall(x,&xx); x = xx;
                    441:        for ( r0 = 0; x; x = NEXT(x) ) {
                    442:                NEXTNODE(r0,r); dp_load((int)BDY(x),&ps[(int)BDY(x)]);
                    443:                dtop(CO,vv,ps[(int)BDY(x)],(P *)&BDY(r));
                    444:        }
                    445:        if ( r0 ) NEXT(r) = 0;
                    446:        MKLIST(*rp,r0);
                    447: }
                    448:
                    449: void dp_f4_mod_main(f,v,m,ord,rp)
                    450: LIST f,v;
                    451: int m;
                    452: struct order_spec *ord;
                    453: LIST *rp;
                    454: {
                    455:        int i;
                    456:        struct order_spec ord1;
                    457:        VL fv,vv,vc;
1.5       noro      458:        DP b,c,c1;
1.1       noro      459:        NODE fd,fd0,fi,fi0,r,r0,t,subst,x,s,xx;
                    460:
                    461:        dp_fcoeffs = 0;
                    462:        get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&vc);
                    463:        NVars = length((NODE)vv); PCoeffs = vc ? 1 : 0; VC = vc;
                    464:        CNVars = NVars;
                    465:        if ( ord->id && NVars != ord->nv )
                    466:                error("dp_f4_mod_main : invalid order specification");
                    467:        initd(ord);
                    468:        for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                    469:                ptod(CO,vv,(P)BDY(t),&b);
                    470:                _dp_mod(b,m,0,&c);
1.5       noro      471:                _dp_monic(c,m,&c1);
1.1       noro      472:                if ( c ) {
1.5       noro      473:                        NEXTNODE(fd0,fd); BDY(fd) = (pointer)c1;
1.1       noro      474:                }
                    475:        }
                    476:        if ( fd0 ) NEXT(fd) = 0;
                    477:        setup_arrays(fd0,m,&s);
                    478:        x = gb_f4_mod(s,m);
                    479:        reduceall_mod(x,m,&xx); x = xx;
                    480:        for ( r0 = 0; x; x = NEXT(x) ) {
                    481:                NEXTNODE(r0,r); _dtop_mod(CO,vv,ps[(int)BDY(x)],(P *)&BDY(r));
                    482:        }
                    483:        if ( r0 ) NEXT(r) = 0;
                    484:        MKLIST(*rp,r0);
                    485: }
                    486:
                    487: NODE gb_f4(f)
                    488: NODE f;
                    489: {
                    490:        int i,j,k,nh,row,col,nv;
                    491:        NODE r,g,gall;
1.2       noro      492:        NODE s,s0;
1.1       noro      493:        DP_pairs d,dm,dr,t;
1.2       noro      494:        DP h,nf,nf1,f1,f2,f21,f21r,sp,sp1,sd,sdm,tdp;
1.1       noro      495:        MP mp,mp0;
                    496:        NODE blist,bt,nt;
                    497:        DL *ht,*at;
                    498:        MAT mat,nm;
                    499:        int *colstat;
                    500:        int *rind,*cind;
                    501:        int rank,nred;
                    502:        Q dn;
                    503:        struct oEGT tmp0,tmp1,tmp2,eg_split_symb,eg_split_elim;
                    504:        extern struct oEGT eg_mod,eg_elim,eg_chrem,eg_gschk,eg_intrat,eg_symb;
                    505:
                    506:        init_eg(&eg_mod); init_eg(&eg_elim); init_eg(&eg_chrem);
                    507:        init_eg(&eg_gschk); init_eg(&eg_intrat); init_eg(&eg_symb);
                    508:
                    509:        doing_f4 = 1;
                    510:        for ( gall = g = 0, d = 0, r = f; r; r = NEXT(r) ) {
                    511:                i = (int)BDY(r);
                    512:                d = updpairs(d,g,i);
                    513:                g = updbase(g,i);
                    514:                gall = append_one(gall,i);
                    515:        }
                    516:        if ( gall )
                    517:                nv = ((DP)ps[(int)BDY(gall)])->nv;
                    518:        while ( d ) {
                    519:                get_eg(&tmp0);
                    520:                minsugar(d,&dm,&dr); d = dr;
1.12      noro      521:                if ( DP_Print )
1.1       noro      522:                        fprintf(asir_out,"sugar=%d\n",dm->sugar);
                    523:                blist = 0; s0 = 0;
                    524:                /* asph : sum of all head terms of spoly */
                    525:                for ( t = dm; t; t = NEXT(t) ) {
                    526:                        dp_sp(ps[t->dp1],ps[t->dp2],&sp);
                    527:                        if ( sp ) {
                    528:                                MKNODE(bt,sp,blist); blist = bt;
1.2       noro      529:                                s0 = symb_merge(s0,dp_dllist(sp),nv);
1.1       noro      530:                        }
                    531:                }
                    532:                /* s0 : all the terms appeared in symbolic redunction */
1.2       noro      533:                for ( s = s0, nred = 0; s; s = NEXT(s) ) {
1.1       noro      534:                        for ( r = gall; r; r = NEXT(r) )
1.2       noro      535:                                if ( _dl_redble(BDY(ps[(int)BDY(r)])->dl,BDY(s),nv) )
1.1       noro      536:                                        break;
                    537:                        if ( r ) {
1.2       noro      538:                                dltod(BDY(s),nv,&tdp);
                    539:                                dp_subd(tdp,ps[(int)BDY(r)],&sd);
1.5       noro      540:                                muld(CO,sd,ps[(int)BDY(r)],&f2);
1.1       noro      541:                                MKNODE(bt,f2,blist); blist = bt;
1.2       noro      542:                                s = symb_merge(s,dp_dllist(f2),nv);
1.1       noro      543:                                nred++;
                    544:                        }
                    545:                }
                    546:
                    547:                /* the first nred polys in blist are reducers */
                    548:                /* row = the number of all the polys */
                    549:                for ( r = blist, row = 0; r; r = NEXT(r), row++ );
                    550:                ht = (DL *)MALLOC(nred*sizeof(DL));
                    551:                for ( r = blist, i = 0; i < nred; r = NEXT(r), i++ )
                    552:                        ht[i] = BDY((DP)BDY(r))->dl;
1.2       noro      553:                for ( s = s0, col = 0; s; s = NEXT(s), col++ );
1.1       noro      554:                at = (DL *)MALLOC(col*sizeof(DL));
1.2       noro      555:                for ( s = s0, i = 0; i < col; s = NEXT(s), i++ )
                    556:                        at[i] = (DL)BDY(s);
1.1       noro      557:                MKMAT(mat,row,col);
                    558:                for ( i = 0, r = blist; i < row; r = NEXT(r), i++ )
                    559:                        dp_to_vect(BDY(r),at,(Q *)mat->body[i]);
                    560:                get_eg(&tmp1); add_eg(&eg_symb,&tmp0,&tmp1);
                    561:                init_eg(&eg_split_symb); add_eg(&eg_split_symb,&tmp0,&tmp1);
1.12      noro      562:                if ( DP_Print ) {
1.1       noro      563:                        print_eg("Symb",&eg_split_symb);
                    564:                        fprintf(asir_out,"mat : %d x %d",row,col);
                    565:                        fflush(asir_out);
                    566:                }
1.3       noro      567: #if 0
                    568:                rank = generic_gauss_elim_hensel(mat,&nm,&dn,&rind,&cind);
                    569: #else
1.1       noro      570:                rank = generic_gauss_elim(mat,&nm,&dn,&rind,&cind);
1.3       noro      571: #endif
1.12      noro      572:                if ( DP_Print )
1.1       noro      573:                        fprintf(asir_out,"done rank = %d\n",rank,row,col);
                    574:                for ( i = 0; i < rank; i++ ) {
                    575:                        for ( k = 0; k < nred; k++ )
                    576:                                if ( !cmpdl(nv,at[rind[i]],ht[k]) )
                    577:                                        break;
                    578:                        if ( k == nred ) {
                    579:                                /* this is a new base */
                    580:                                mp0 = 0;
                    581:                                NEXTMP(mp0,mp); mp->dl = at[rind[i]]; mp->c = (P)dn;
                    582:                                for ( k = 0; k < col-rank; k++ )
                    583:                                        if ( nm->body[i][k] ) {
                    584:                                                NEXTMP(mp0,mp); mp->dl = at[cind[k]];
                    585:                                                mp->c = (P)nm->body[i][k];
                    586:                                        }
                    587:                                NEXT(mp) = 0;
                    588:                                MKDP(nv,mp0,nf); nf->sugar = dm->sugar;
                    589:                                dp_ptozp(nf,&nf1);
                    590:                                nh = newps(nf1,0,0);
                    591:                                d = updpairs(d,g,nh);
                    592:                                g = updbase(g,nh);
                    593:                                gall = append_one(gall,nh);
                    594:                        }
                    595:                }
                    596:        }
1.12      noro      597:        if ( DP_Print ) {
1.1       noro      598:                print_eg("Symb",&eg_symb);
                    599:                print_eg("Mod",&eg_mod); print_eg("GaussElim",&eg_elim);
                    600:                print_eg("ChRem",&eg_chrem); print_eg("IntToRat",&eg_intrat);
                    601:                print_eg("Check",&eg_gschk);
                    602:        }
                    603:        return g;
                    604: }
                    605:
1.5       noro      606: /* initial bases are monic */
                    607:
1.1       noro      608: NODE gb_f4_mod(f,m)
                    609: NODE f;
                    610: int m;
                    611: {
                    612:        int i,j,k,nh,row,col,nv;
                    613:        NODE r,g,gall;
1.2       noro      614:        NODE s,s0;
1.1       noro      615:        DP_pairs d,dm,dr,t;
1.2       noro      616:        DP h,nf,f1,f2,f21,f21r,sp,sp1,sd,sdm,tdp;
1.1       noro      617:        MP mp,mp0;
                    618:        NODE blist,bt,nt;
1.5       noro      619:        DL *ht,*at,*st;
                    620:        int **spmat,**redmat;
                    621:        int *colstat,*w;
                    622:        int rank,nred,nsp,nonzero,spcol;
                    623:        int *indred,*isred,*ri;
                    624:        struct oEGT tmp0,tmp1,tmp2,eg_split_symb,eg_split_elim1,eg_split_elim2;
                    625:        extern struct oEGT eg_symb,eg_elim1,eg_elim2;
1.1       noro      626:
1.5       noro      627:        init_eg(&eg_symb); init_eg(&eg_elim1); init_eg(&eg_elim2);
1.1       noro      628:        for ( gall = g = 0, d = 0, r = f; r; r = NEXT(r) ) {
                    629:                i = (int)BDY(r);
                    630:                d = updpairs(d,g,i);
                    631:                g = updbase(g,i);
                    632:                gall = append_one(gall,i);
                    633:        }
                    634:        if ( gall )
                    635:                nv = ((DP)ps[(int)BDY(gall)])->nv;
                    636:        while ( d ) {
                    637:                get_eg(&tmp0);
                    638:                minsugar(d,&dm,&dr); d = dr;
1.12      noro      639:                if ( DP_Print )
1.1       noro      640:                        fprintf(asir_out,"sugar=%d\n",dm->sugar);
                    641:                blist = 0; s0 = 0;
                    642:                /* asph : sum of all head terms of spoly */
                    643:                for ( t = dm; t; t = NEXT(t) ) {
1.13      noro      644:                        _dp_sp_mod(ps[t->dp1],ps[t->dp2],m,&sp);
1.1       noro      645:                        if ( sp ) {
                    646:                                MKNODE(bt,sp,blist); blist = bt;
1.2       noro      647:                                s0 = symb_merge(s0,dp_dllist(sp),nv);
1.1       noro      648:                        }
                    649:                }
                    650:                /* s0 : all the terms appeared in symbolic redunction */
1.2       noro      651:                for ( s = s0, nred = 0; s; s = NEXT(s) ) {
1.1       noro      652:                        for ( r = gall; r; r = NEXT(r) )
1.2       noro      653:                                if ( _dl_redble(BDY(ps[(int)BDY(r)])->dl,BDY(s),nv) )
1.1       noro      654:                                        break;
                    655:                        if ( r ) {
1.2       noro      656:                                dltod(BDY(s),nv,&tdp);
                    657:                                dp_subd(tdp,ps[(int)BDY(r)],&sd);
1.1       noro      658:                                _dp_mod(sd,m,0,&sdm);
1.13      noro      659:                                mulmd_dup(m,sdm,ps[(int)BDY(r)],&f2);
1.1       noro      660:                                MKNODE(bt,f2,blist); blist = bt;
1.2       noro      661:                                s = symb_merge(s,dp_dllist(f2),nv);
1.1       noro      662:                                nred++;
                    663:                        }
                    664:                }
1.5       noro      665:
                    666:                get_eg(&tmp1); add_eg(&eg_symb,&tmp0,&tmp1);
                    667:                init_eg(&eg_split_symb); add_eg(&eg_split_symb,&tmp0,&tmp1);
1.1       noro      668:
                    669:                /* the first nred polys in blist are reducers */
                    670:                /* row = the number of all the polys */
                    671:                for ( r = blist, row = 0; r; r = NEXT(r), row++ );
1.5       noro      672:
                    673:                /* head terms of reducers */
1.1       noro      674:                ht = (DL *)MALLOC(nred*sizeof(DL));
                    675:                for ( r = blist, i = 0; i < nred; r = NEXT(r), i++ )
                    676:                        ht[i] = BDY((DP)BDY(r))->dl;
1.5       noro      677:
                    678:                /* col = number of all terms */
1.2       noro      679:                for ( s = s0, col = 0; s; s = NEXT(s), col++ );
1.5       noro      680:
                    681:                /* head terms of all terms */
1.1       noro      682:                at = (DL *)MALLOC(col*sizeof(DL));
1.2       noro      683:                for ( s = s0, i = 0; i < col; s = NEXT(s), i++ )
                    684:                        at[i] = (DL)BDY(s);
1.5       noro      685:
                    686:                /* store coefficients separately in spmat and redmat */
                    687:                nsp = row-nred;
                    688:
                    689:                /* reducer matrix */
                    690:                redmat = (int **)almat(nred,col);
                    691:                for ( i = 0, r = blist; i < nred; r = NEXT(r), i++ )
                    692:                        _dpmod_to_vect(BDY(r),at,redmat[i]);
                    693:                /* XXX */
                    694: /*             reduce_reducers_mod(redmat,nred,col,m); */
                    695:                /* register the position of the head term */
                    696:                indred = (int *)MALLOC(nred*sizeof(int));
                    697:                bzero(indred,nred*sizeof(int));
                    698:                isred = (int *)MALLOC(col*sizeof(int));
                    699:                bzero(isred,col*sizeof(int));
                    700:                for ( i = 0; i < nred; i++ ) {
                    701:                        ri = redmat[i];
                    702:                        for ( j = 0; j < col && !ri[j]; j++ );
                    703:                        indred[i] = j;
                    704:                        isred[j] = 1;
                    705:                }
                    706:
                    707:                spcol = col-nred;
                    708:                /* head terms not in ht */
                    709:                st = (DL *)MALLOC(spcol*sizeof(DL));
                    710:                for ( j = 0, k = 0; j < col; j++ )
                    711:                        if ( !isred[j] )
                    712:                                st[k++] = at[j];
                    713:
                    714:                /* spoly matrix; stored in reduced form; terms in ht[] are omitted */
                    715:                spmat = almat(nsp,spcol);
                    716:                w = (int *)MALLOC(col*sizeof(int));
                    717:                for ( ; i < row; r = NEXT(r), i++ ) {
                    718:                        bzero(w,col*sizeof(int));
                    719:                        _dpmod_to_vect(BDY(r),at,w);
                    720:                        reduce_sp_by_red_mod(w,redmat,indred,nred,col,m);
                    721:                        for ( j = 0, k = 0; j < col; j++ )
                    722:                                if ( !isred[j] )
                    723:                                        spmat[i-nred][k++] = w[j];
                    724:                }
                    725:
                    726:                get_eg(&tmp0); add_eg(&eg_elim1,&tmp1,&tmp0);
                    727:                init_eg(&eg_split_elim1); add_eg(&eg_split_elim1,&tmp1,&tmp0);
                    728:
                    729:                colstat = (int *)MALLOC_ATOMIC(spcol*sizeof(int));
                    730:                for ( i = 0, nonzero=0; i < nsp; i++ )
                    731:                        for ( j = 0; j < spcol; j++ )
                    732:                                if ( spmat[i][j] )
1.1       noro      733:                                        nonzero++;
1.17      noro      734:                if ( DP_Print && nsp )
1.5       noro      735:                        fprintf(asir_out,"spmat : %d x %d (nonzero=%f%%)...",
                    736:                                nsp,spcol,((double)nonzero*100)/(nsp*spcol));
1.17      noro      737:                if ( nsp )
                    738:                        rank = generic_gauss_elim_mod(spmat,nsp,spcol,m,colstat);
                    739:                else
                    740:                        rank = 0;
1.5       noro      741:                get_eg(&tmp1); add_eg(&eg_elim2,&tmp0,&tmp1);
                    742:                init_eg(&eg_split_elim2); add_eg(&eg_split_elim2,&tmp0,&tmp1);
                    743:
1.12      noro      744:                if ( DP_Print ) {
1.1       noro      745:                        fprintf(asir_out,"done rank = %d\n",rank,row,col);
                    746:                        print_eg("Symb",&eg_split_symb);
1.5       noro      747:                        print_eg("Elim1",&eg_split_elim1);
                    748:                        print_eg("Elim2",&eg_split_elim2);
1.1       noro      749:                        fprintf(asir_out,"\n");
                    750:                }
1.5       noro      751:                for ( j = 0, i = 0; j < spcol; j++ )
1.1       noro      752:                        if ( colstat[j] ) {
1.5       noro      753:                                mp0 = 0;
1.13      noro      754:                                NEXTMP(mp0,mp); mp->dl = st[j]; mp->c = STOI(1);
1.5       noro      755:                                for ( k = j+1; k < spcol; k++ )
                    756:                                        if ( !colstat[k] && spmat[i][k] ) {
1.13      noro      757:                                                NEXTMP(mp0,mp); mp->dl = st[k];
1.5       noro      758:                                                mp->c = STOI(spmat[i][k]);
1.1       noro      759:                                }
1.5       noro      760:                                NEXT(mp) = 0;
                    761:                                MKDP(nv,mp0,nf); nf->sugar = dm->sugar;
                    762:                                nh = newps_mod(nf,m);
                    763:                                d = updpairs(d,g,nh);
                    764:                                g = updbase(g,nh);
                    765:                                gall = append_one(gall,nh);
1.1       noro      766:                                i++;
                    767:                        }
                    768:        }
1.12      noro      769:        if ( DP_Print ) {
1.1       noro      770:                print_eg("Symb",&eg_symb);
1.5       noro      771:                print_eg("Elim1",&eg_elim1);
                    772:                print_eg("Elim2",&eg_elim2);
1.1       noro      773:                fflush(asir_out);
                    774:        }
                    775:        return g;
                    776: }
                    777:
                    778: int DPPlength(n)
                    779: DP_pairs n;
                    780: {
                    781:        int i;
                    782:
                    783:        for ( i = 0; n; n = NEXT(n), i++ );
                    784:        return i;
                    785: }
                    786:
                    787: void printdl(dl)
                    788: DL dl;
                    789: {
                    790:        int i;
                    791:
                    792:        fprintf(asir_out,"<<");
                    793:        for ( i = 0; i < CNVars-1; i++ )
                    794:                fprintf(asir_out,"%d,",dl->d[i]);
                    795:        fprintf(asir_out,"%d>>",dl->d[i]);
                    796: }
                    797:
                    798: void pltovl(l,vl)
                    799: LIST l;
                    800: VL *vl;
                    801: {
                    802:        NODE n;
                    803:        VL r,r0;
                    804:
                    805:        n = BDY(l);
                    806:        for ( r0 = 0; n; n = NEXT(n) ) {
                    807:                NEXTVL(r0,r); r->v = VR((P)BDY(n));
                    808:        }
                    809:        if ( r0 ) NEXT(r) = 0;
                    810:        *vl = r0;
                    811: }
                    812:
                    813: void makesubst(v,s)
                    814: VL v;
                    815: NODE *s;
                    816: {
                    817:        NODE r,r0;
                    818:        Q q;
                    819:        unsigned int n;
                    820:
                    821:        for ( r0 = 0; v; v = NEXT(v) ) {
                    822:                NEXTNODE(r0,r); BDY(r) = (pointer)v->v;
                    823: #if defined(_PA_RISC1_1)
                    824:                n = mrand48()&BMASK; UTOQ(n,q);
                    825: #else
                    826:                n = random(); UTOQ(n,q);
                    827: #endif
                    828:                NEXTNODE(r0,r); BDY(r) = (pointer)q;
                    829:        }
                    830:        if ( r0 ) NEXT(r) = 0;
                    831:        *s = r0;
                    832: }
                    833:
                    834: void printsubst(s)
                    835: NODE s;
                    836: {
                    837:        fputc('[',asir_out);
                    838:        while ( s ) {
                    839:                printv(CO,(V)BDY(s)); s = NEXT(s);
                    840:                fprintf(asir_out,"->%d",QTOS((Q)BDY(s)));
                    841:                if ( NEXT(s) ) {
                    842:                        fputc(',',asir_out); s = NEXT(s);
                    843:                } else
                    844:                        break;
                    845:        }
                    846:        fprintf(asir_out,"]\n"); return;
                    847: }
                    848:
                    849: void vlminus(v,w,d)
                    850: VL v,w,*d;
                    851: {
                    852:        int i,j,n,m;
                    853:        V *va,*wa;
                    854:        V a;
                    855:        VL r,r0;
                    856:        VL t;
                    857:
                    858:        for ( n = 0, t = v; t; t = NEXT(t), n++ );
                    859:        va = (V *)ALLOCA(n*sizeof(V));
                    860:        for ( i = 0, t = v; t; t = NEXT(t), i++ )
                    861:                va[i] = t->v;
                    862:        for ( m = 0, t = w; t; t = NEXT(t), m++ );
                    863:        wa = (V *)ALLOCA(m*sizeof(V));
                    864:        for ( i = 0, t = w; t; t = NEXT(t), i++ )
                    865:                wa[i] = t->v;
                    866:        for ( i = 0; i < n; i++ ) {
                    867:                a = va[i];
                    868:                for ( j = 0; j < m; j++ )
                    869:                        if ( a == wa[j] )
                    870:                                break;
                    871:                if ( j < m )
                    872:                        va[i] = 0;
                    873:        }
                    874:        for ( r0 = 0, i = 0; i < n; i++ )
                    875:                if ( va[i] ) { NEXTVL(r0,r); r->v = va[i];      }
                    876:        if ( r0 ) NEXT(r) = 0;
                    877:        *d = r0;
                    878: }
                    879:
                    880: int validhc(a,m,s)
                    881: P a;
                    882: int m;
                    883: NODE s;
                    884: {
                    885:        P c,c1;
                    886:        V v;
                    887:
                    888:        if ( !a )
                    889:                return 0;
                    890:        for ( c = a; s; s = NEXT(s) ) {
                    891:                v = (V)BDY(s); s = NEXT(s);
                    892:                substp(CO,c,v,(P)BDY(s),&c1); c = c1;
                    893:        }
                    894:        ptomp(m,c,&c1);
                    895:        return c1 ? 1 : 0;
                    896: }
                    897:
                    898: void setup_arrays(f,m,r)
                    899: NODE f,*r;
                    900: int m;
                    901: {
                    902:        int i;
1.7       noro      903:        NODE s,s0,f0;
1.1       noro      904:
1.17      noro      905: #if 1
1.7       noro      906:        f0 = f = NODE_sortb(f,1);
1.17      noro      907: #else
                    908:        f0 = f;
                    909: #endif
1.1       noro      910:        psn = length(f); pslen = 2*psn;
                    911:        ps = (DP *)MALLOC(pslen*sizeof(DP));
                    912:        psh = (DL *)MALLOC(pslen*sizeof(DL));
                    913:        pss = (int *)MALLOC(pslen*sizeof(int));
                    914:        psc = (P *)MALLOC(pslen*sizeof(P));
                    915:        for ( i = 0; i < psn; i++, f = NEXT(f) ) {
                    916:                prim_part((DP)BDY(f),m,&ps[i]);
                    917:                if ( Demand )
1.7       noro      918:                        dp_save(i,(Obj)ps[i],0);
1.1       noro      919:                psh[i] = BDY(ps[i])->dl;
                    920:                pss[i] = ps[i]->sugar;
                    921:                psc[i] = BDY(ps[i])->c;
                    922:        }
1.7       noro      923:        if ( GenTrace && (OXCheck >= 0) ) {
                    924:                Q q;
                    925:                STRING fname;
                    926:                LIST input;
                    927:                NODE arg;
                    928:                Obj dmy;
                    929:
                    930:                STOQ(OXCheck,q);
                    931:                MKSTR(fname,"register_input");
                    932:                MKLIST(input,f0);
                    933:                arg = mknode(3,q,fname,input);
                    934:                Pox_cmo_rpc(arg,&dmy);
                    935:        }
1.1       noro      936:        for ( s0 = 0, i = 0; i < psn; i++ ) {
                    937:                NEXTNODE(s0,s); BDY(s) = (pointer)i;
                    938:        }
                    939:        if ( s0 ) NEXT(s) = 0;
                    940:        *r = s0;
                    941: }
                    942:
                    943: void prim_part(f,m,r)
                    944: DP f,*r;
                    945: int m;
                    946: {
1.7       noro      947:        P d,t;
                    948:
1.1       noro      949:        if ( m > 0 ) {
                    950:                if ( PCoeffs )
                    951:                        dp_prim_mod(f,m,r);
                    952:                else
                    953:                        *r = f;
                    954:        } else {
                    955:                if ( dp_fcoeffs )
                    956:                        *r = f;
                    957:                else if ( PCoeffs )
                    958:                        dp_prim(f,r);
                    959:                else
                    960:                        dp_ptozp(f,r);
1.7       noro      961:                if ( GenTrace && TraceList ) {
                    962:                        divsp(CO,BDY(f)->c,BDY(*r)->c,&d);
                    963:                        mulp(CO,(P)ARG3(BDY((LIST)BDY(TraceList))),d,&t);
                    964:                        ARG3(BDY((LIST)BDY(TraceList))) = t;
                    965:                }
1.1       noro      966:        }
                    967: }
                    968:
                    969: NODE /* of DP */ NODE_sortb_insert( newdp, nd, dec )
                    970: DP newdp;
                    971: NODE /* of DP */ nd;
                    972: int dec;
                    973: {
                    974:        register NODE last, p;
                    975:        register DL newdl = BDY(newdp)->dl;
                    976:        register int (*cmpfun)() = cmpdl, nv = CNVars;
                    977:        NODE newnd;
                    978:        int sgn = dec ? 1 : -1;
                    979:        MKNODE( newnd, newdp, 0 );
                    980:        if ( !(last = nd) || sgn*(*cmpfun)( nv, newdl, BDY((DP) BDY(last))->dl ) > 0 ) {
                    981:                NEXT(newnd) = last;
                    982:                return newnd;
                    983:        }
                    984:        for ( ; p = NEXT(last); last = p )
                    985:                if ( sgn*(*cmpfun)( nv, newdl, BDY((DP) BDY(p))->dl ) > 0 ) break;
                    986:        if ( p ) NEXT(NEXT(last) = newnd) = p;
                    987:        else NEXT(last) = newnd;
                    988:        return nd;
                    989: }
                    990:
                    991: NODE NODE_sortb( node, dec )
                    992: NODE node;
                    993: int dec;
                    994: {
                    995:        register NODE nd, ans;
                    996:
                    997:        for ( ans = 0, nd = node; nd; nd = NEXT(nd) )
                    998:                ans = NODE_sortb_insert( (DP) BDY(nd), ans, dec );
                    999:        return ans;
                   1000: }
                   1001:
                   1002: NODE /* of index */ NODE_sortbi_insert( newdpi, nd, dec )
                   1003: int newdpi;
                   1004: NODE /* of index */ nd;
                   1005: int dec;
                   1006: {
                   1007:        register NODE last, p;
                   1008:        register DL newdl = psh[newdpi];
                   1009:        register int (*cmpfun)() = cmpdl, nv = CNVars;
                   1010:        NODE newnd;
                   1011:        int sgn = dec ? 1 : -1;
                   1012:        MKNODE( newnd, newdpi, 0 );
                   1013:        if ( !(last = nd) || sgn*(*cmpfun)( nv, newdl, psh[(int)BDY(last)] ) > 0 ) {
                   1014:                NEXT(newnd) = last;
                   1015:                return newnd;
                   1016:        }
                   1017:        for ( ; p = NEXT(last); last = p )
                   1018:                if ( sgn*(*cmpfun)( nv, newdl, psh[(int)BDY(p)] ) > 0 ) break;
                   1019:        if ( p ) NEXT(NEXT(last) = newnd) = p;
                   1020:        else NEXT(last) = newnd;
                   1021:        return nd;
                   1022: }
                   1023:
                   1024: NODE NODE_sortbi( node, dec )
                   1025: NODE node;
                   1026: int dec;
                   1027: {
                   1028:        register NODE nd, ans;
                   1029:
                   1030:        for ( ans = 0, nd = node; nd; nd = NEXT(nd) )
                   1031:                ans = NODE_sortbi_insert( (int) BDY(nd), ans, dec );
                   1032:        return ans;
                   1033: }
                   1034:
                   1035: void reduceall(in,h)
                   1036: NODE in;
                   1037: NODE *h;
                   1038: {
                   1039:        NODE r,t,top;
                   1040:        int n,i,j;
                   1041:        int *w;
                   1042:        DP g,g1;
                   1043:        struct oEGT tmp0,tmp1;
                   1044:
                   1045:        if ( NoRA ) {
                   1046:                *h = in; return;
                   1047:        }
1.12      noro     1048:        if ( DP_Print || DP_PrintShort ) {
1.1       noro     1049:                fprintf(asir_out,"reduceall\n"); fflush(asir_out);
                   1050:        }
                   1051:        r = NODE_sortbi(in,0);
                   1052:        n = length(r);
                   1053:        w = (int *)ALLOCA(n*sizeof(int));
                   1054:        for ( i = 0, t = r; i < n; i++, t = NEXT(t) )
                   1055:                w[i] = (int)BDY(t);
                   1056:        for ( i = 0; i < n; i++ ) {
                   1057:                for ( top = 0, j = n-1; j >= 0; j-- )
                   1058:                        if ( j != i ) {
                   1059:                                MKNODE(t,(pointer)w[j],top); top = t;
                   1060:                        }
                   1061:                get_eg(&tmp0);
                   1062:                dp_load(w[i],&ps[w[i]]);
                   1063:
1.7       noro     1064:                if ( GenTrace ) {
                   1065:                        Q q;
                   1066:                        NODE node;
                   1067:                        LIST hist;
                   1068:
                   1069:                        STOQ(w[i],q);
                   1070:                        node = mknode(4,ONE,q,ONE,ONE);
                   1071:                        MKLIST(hist,node);
                   1072:                        MKNODE(TraceList,hist,0);
                   1073:                }
1.16      noro     1074:                _dp_nf(top,ps[w[i]],ps,1,&g);
1.1       noro     1075:                prim_part(g,0,&g1);
                   1076:                get_eg(&tmp1); add_eg(&eg_ra,&tmp0,&tmp1);
1.12      noro     1077:                if ( DP_Print || DP_PrintShort ) {
1.1       noro     1078:                        fprintf(asir_out,"."); fflush(asir_out);
                   1079:                }
                   1080:                w[i] = newps(g1,0,(NODE)0);
                   1081:        }
                   1082:        for ( top = 0, j = n-1; j >= 0; j-- ) {
                   1083:                MKNODE(t,(pointer)w[j],top); top = t;
                   1084:        }
                   1085:        *h = top;
1.12      noro     1086:        if ( DP_Print || DP_PrintShort )
1.1       noro     1087:                fprintf(asir_out,"\n");
                   1088: }
                   1089:
                   1090: void reduceall_mod(in,m,h)
                   1091: NODE in;
                   1092: int m;
                   1093: NODE *h;
                   1094: {
                   1095:        NODE r,t,top;
                   1096:        int n,i,j;
                   1097:        int *w;
1.12      noro     1098:        DP g,p;
1.1       noro     1099:        struct oEGT tmp0,tmp1;
                   1100:
                   1101:        if ( NoRA ) {
                   1102:                *h = in; return;
                   1103:        }
1.12      noro     1104:        if ( DP_Print || DP_PrintShort ) {
1.1       noro     1105:                fprintf(asir_out,"reduceall\n"); fflush(asir_out);
                   1106:        }
                   1107:        r = NODE_sortbi(in,0);
                   1108:        n = length(r);
                   1109:        w = (int *)ALLOCA(n*sizeof(int));
                   1110:        for ( i = 0, t = r; i < n; i++, t = NEXT(t) )
                   1111:                w[i] = (int)BDY(t);
                   1112:        for ( i = 0; i < n; i++ ) {
                   1113:                for ( top = 0, j = n-1; j >= 0; j-- )
                   1114:                        if ( j != i ) {
                   1115:                                MKNODE(t,(pointer)w[j],top); top = t;
                   1116:                        }
                   1117:                get_eg(&tmp0);
                   1118:                if ( PCoeffs )
                   1119:                        dp_nf_mod(top,ps[w[i]],ps,m,1,&g);
1.12      noro     1120:                else {
                   1121:                        dpto_dp(ps[w[i]],&p);
                   1122:                        _dp_nf_mod_destructive(top,p,ps,m,1,&g);
                   1123:                }
1.1       noro     1124:                get_eg(&tmp1); add_eg(&eg_ra,&tmp0,&tmp1);
1.12      noro     1125:                if ( DP_Print || DP_PrintShort ) {
1.1       noro     1126:                        fprintf(asir_out,"."); fflush(asir_out);
                   1127:                }
                   1128:                w[i] = newps_mod(g,m);
                   1129:        }
                   1130:        for ( top = 0, j = n-1; j >= 0; j-- ) {
                   1131:                MKNODE(t,(pointer)w[j],top); top = t;
                   1132:        }
                   1133:        *h = top;
1.12      noro     1134:        if ( DP_Print || DP_PrintShort )
1.1       noro     1135:                fprintf(asir_out,"\n");
                   1136: }
                   1137:
                   1138: int newps(a,m,subst)
                   1139: DP a;
                   1140: int m;
                   1141: NODE subst;
                   1142: {
                   1143:        if ( m && !validhc(!a?0:BDY(a)->c,m,subst) )
                   1144:                return -1;
                   1145:        if ( psn == pslen ) {
                   1146:                pslen *= 2;
                   1147:                ps = (DP *)REALLOC((char *)ps,pslen*sizeof(DP));
                   1148:                psh = (DL *)REALLOC((char *)psh,pslen*sizeof(DL));
                   1149:                pss = (int *)REALLOC((char *)pss,pslen*sizeof(int));
                   1150:                psc = (P *)REALLOC((char *)psc,pslen*sizeof(P));
                   1151:                if ( m )
                   1152:                        psm = (DP *)REALLOC((char *)psm,pslen*sizeof(DP));
                   1153:        }
                   1154:        if ( Demand ) {
                   1155:                if ( doing_f4 )
                   1156:                        ps[psn] = a;
                   1157:                else
                   1158:                        ps[psn] = 0;
1.7       noro     1159:                dp_save(psn,(Obj)a,0);
1.1       noro     1160:        } else
                   1161:                ps[psn] = a;
                   1162:        psh[psn] = BDY(a)->dl;
                   1163:        pss[psn] = a->sugar;
                   1164:        psc[psn] = BDY(a)->c;
                   1165:        if ( m )
                   1166:                _dp_mod(a,m,subst,&psm[psn]);
1.7       noro     1167:        if ( GenTrace ) {
                   1168:                NODE tn,tr,tr1;
                   1169:                LIST trace;
                   1170:
                   1171:                /* reverse the TraceList */
                   1172:                tn = TraceList;
                   1173:                for ( tr = 0; tn; tn = NEXT(tn) ) {
                   1174:                        MKNODE(tr1,BDY(tn),tr); tr = tr1;
                   1175:                }
                   1176:                MKLIST(trace,tr);
                   1177:                if ( OXCheck >= 0 ) {
                   1178:                        NODE arg;
                   1179:                        Q q1,q2;
                   1180:                        STRING fname;
                   1181:                        Obj dmy;
                   1182:
                   1183:                        STOQ(OXCheck,q1);
                   1184:                        MKSTR(fname,"check_trace");
                   1185:                        STOQ(psn,q2);
                   1186:                        arg = mknode(5,q1,fname,a,q2,trace);
                   1187:                        Pox_cmo_rpc(arg,&dmy);
                   1188:                } else
                   1189:                        dp_save(psn,(Obj)trace,"t");
                   1190:                TraceList = 0;
                   1191:        }
1.1       noro     1192:        return psn++;
                   1193: }
                   1194:
                   1195: int newps_nosave(a,m,subst)
                   1196: DP a;
                   1197: int m;
                   1198: NODE subst;
                   1199: {
                   1200:        if ( m && !validhc(!a?0:BDY(a)->c,m,subst) )
                   1201:                return -1;
                   1202:        if ( psn == pslen ) {
                   1203:                pslen *= 2;
                   1204:                ps = (DP *)REALLOC((char *)ps,pslen*sizeof(DP));
                   1205:                psh = (DL *)REALLOC((char *)psh,pslen*sizeof(DL));
                   1206:                pss = (int *)REALLOC((char *)pss,pslen*sizeof(int));
                   1207:                psc = (P *)REALLOC((char *)psc,pslen*sizeof(P));
                   1208:                if ( m )
                   1209:                        psm = (DP *)REALLOC((char *)psm,pslen*sizeof(DP));
                   1210:        }
                   1211:        ps[psn] = 0;
                   1212:        psh[psn] = BDY(a)->dl;
                   1213:        pss[psn] = a->sugar;
                   1214:        psc[psn] = BDY(a)->c;
                   1215:        if ( m )
                   1216:                _dp_mod(a,m,subst,&psm[psn]);
                   1217:        return psn++;
                   1218: }
                   1219:
                   1220: int newps_mod(a,m)
                   1221: DP a;
                   1222: int m;
                   1223: {
                   1224:        if ( psn == pslen ) {
                   1225:                pslen *= 2;
                   1226:                ps = (DP *)REALLOC((char *)ps,pslen*sizeof(DP));
                   1227:                psh = (DL *)REALLOC((char *)psh,pslen*sizeof(DL));
                   1228:                pss = (int *)REALLOC((char *)pss,pslen*sizeof(int));
                   1229:                psc = (P *)REALLOC((char *)psc,pslen*sizeof(P)); /* XXX */
                   1230:        }
                   1231:        ps[psn] = a;
                   1232:        psh[psn] = BDY(ps[psn])->dl;
                   1233:        pss[psn] = ps[psn]->sugar;
                   1234:        return psn++;
                   1235: }
                   1236:
                   1237: void reducebase_dehomo(f,g)
                   1238: NODE f,*g;
                   1239: {
                   1240:        int n,i,j,k;
                   1241:        int *r;
                   1242:        DL *w,d;
                   1243:        DP u;
                   1244:        NODE t,top;
                   1245:
                   1246:        n = length(f);
                   1247:        w = (DL *)ALLOCA(n*sizeof(DL));
                   1248:        r = (int *)ALLOCA(n*sizeof(int));
                   1249:        for ( i = 0, t = f; i < n; i++, t = NEXT(t) ) {
                   1250:                r[i] = (int)BDY(t); w[i] = psh[r[i]];
                   1251:        }
                   1252:        for ( i = 0; i < n; i++ ) {
                   1253:                for ( j = 0, d = w[i]; j < n; j++ ) {
                   1254:                        if ( j != i ) {
                   1255:                                for ( k = 0; k < NVars; k++ )
                   1256:                                        if ( d->d[k] < w[j]->d[k] )
                   1257:                                                break;
                   1258:                                if ( k == NVars )
                   1259:                                        break;
                   1260:                        }
                   1261:                }
                   1262:                if ( j != n )
                   1263:                        r[i] = -1;
                   1264:        }
                   1265:        for ( top = 0, i = n-1; i >= 0; i-- )
                   1266:                if ( r[i] >= 0 ) {
1.7       noro     1267:                        dp_load(r[i],&ps[r[i]]); dp_dehomo(ps[r[i]],&u);
                   1268:                        if ( GenTrace ) {
                   1269:                                Q q;
                   1270:                                LIST hist;
                   1271:                                NODE node;
                   1272:
                   1273:                                STOQ(r[i],q);
                   1274:                                node = mknode(4,0,q,0,0);
                   1275:                                MKLIST(hist,node);
                   1276:                                MKNODE(TraceList,hist,0);
                   1277:                        }
                   1278:                        j = newps(u,0,0);
1.1       noro     1279:                        MKNODE(t,j,top); top = t;
                   1280:                }
                   1281:        *g = top;
                   1282: }
                   1283:
                   1284: NODE append_one(f,n)
                   1285: NODE f;
                   1286: int n;
                   1287: {
                   1288:        NODE t;
                   1289:
                   1290:        if ( Reverse || !f ) {
                   1291:                MKNODE(t,(pointer)n,f); return t;
                   1292:        } else {
                   1293:                for ( t = f; NEXT(t); t = NEXT(t) );
                   1294:                MKNODE(NEXT(t),(pointer)n,0);
                   1295:                return f;
                   1296:        }
                   1297: }
                   1298:
                   1299: DP_pairs minp( d, prest )
                   1300: DP_pairs d, *prest;
                   1301: {
                   1302:        register DP_pairs m, ml, p, l;
                   1303:        register DL lcm;
                   1304:        register int s, nv = CNVars;
                   1305:        register int (*cmpfun)() = cmpdl;
                   1306:
                   1307:        if ( !(p = NEXT(m = d)) ) {
                   1308:                *prest = p;
                   1309:                NEXT(m) = 0;
                   1310:                return m;
                   1311:        }
                   1312:        for ( lcm = m->lcm, s = m->sugar, ml = 0, l = m; p; p = NEXT(l = p) )
                   1313:                if ( NoSugar ? (*cmpfun)( nv, lcm, p->lcm ) >= 0 :
                   1314:                     (s > p->sugar || s == p->sugar && (*cmpfun)( nv, lcm, p->lcm ) >= 0) )
                   1315:                        ml = l,  lcm = (m = p)->lcm,  s = p->sugar;
                   1316:        if ( !ml ) *prest = NEXT(m);
                   1317:        else {
                   1318:                NEXT(ml) = NEXT(m);
                   1319:                *prest = d;
                   1320:        }
                   1321:        NEXT(m) = 0;
                   1322:        return m;
                   1323: }
                   1324:
                   1325: void minsugar(d,dm,dr)
                   1326: DP_pairs d;
                   1327: DP_pairs *dm,*dr;
                   1328: {
                   1329:        int msugar;
                   1330:        DP_pairs t,dm0,dr0,dmt,drt;
                   1331:
                   1332:        for ( msugar = d->sugar, t = NEXT(d); t; t = NEXT(t) )
                   1333:                if ( t->sugar < msugar )
                   1334:                        msugar = t->sugar;
                   1335:        dm0 = 0; dr0 = 0;
                   1336:        for ( t = d; t; t = NEXT(t) ) {
                   1337:                if ( t->sugar == msugar ) {
                   1338:                        NEXTDPP(dm0,dmt);
                   1339:                        dmt->dp1 = t->dp1; dmt->dp2 = t->dp2;
                   1340:                        dmt->lcm = t->lcm; dmt->sugar = t->sugar;
                   1341:                } else {
                   1342:                        NEXTDPP(dr0,drt);
                   1343:                        drt->dp1 = t->dp1; drt->dp2 = t->dp2;
                   1344:                        drt->lcm = t->lcm; drt->sugar = t->sugar;
                   1345:                }
                   1346:        }
                   1347:        if ( dm0 ) NEXT(dmt) = 0;
                   1348:        if ( dr0 ) NEXT(drt) = 0;
                   1349:        *dm = dm0; *dr = dr0;
                   1350: }
                   1351:
                   1352: NODE gb(f,m,subst)
                   1353: NODE f;
                   1354: int m;
                   1355: NODE subst;
                   1356: {
                   1357:        int i,nh,prev,mag;
                   1358:        NODE r,g,gall;
                   1359:        DP_pairs d,d1;
                   1360:        DP_pairs l;
                   1361:        DP h,nf,nfm,dp1,dp2;
                   1362:        MP mp;
                   1363:        struct oEGT tnf0,tnf1,tnfm0,tnfm1,tpz0,tpz1,tsp0,tsp1,tspm0,tspm1,tnp0,tnp1,tmp0,tmp1;
                   1364:        int skip_nf_flag;
                   1365:        double t_0;
1.7       noro     1366:        Q q;
1.6       noro     1367:        int new_sugar;
1.1       noro     1368:        static prev_sugar = -1;
                   1369:
                   1370:        Max_mag = 0;
                   1371:        prev = 1;
                   1372:        doing_f4 = 0;
                   1373:        if ( m ) {
                   1374:                psm = (DP *)MALLOC(pslen*sizeof(DP));
                   1375:                for ( i = 0; i < psn; i++ )
                   1376:                        if ( psh[i] && !validhc(psc[i],m,subst) )
                   1377:                                return 0;
                   1378:                        else
                   1379:                                _dp_mod(ps[i],m,subst,&psm[i]);
                   1380:        }
                   1381:        for ( gall = g = 0, d = 0, r = f; r; r = NEXT(r) ) {
                   1382:                i = (int)BDY(r);
                   1383:                d = updpairs(d,g,i);
                   1384:                g = updbase(g,i);
                   1385:                gall = append_one(gall,i);
                   1386:        }
                   1387:        while ( d ) {
1.14      noro     1388:                l = minp(d,&d);
1.1       noro     1389:                if ( m ) {
1.6       noro     1390:                        _dp_sp_mod_dup(psm[l->dp1],psm[l->dp2],m,&h);
1.10      noro     1391:                        if ( h )
                   1392:                                new_sugar = h->sugar;
1.1       noro     1393:                        get_eg(&tnfm0);
1.6       noro     1394:                        _dp_nf_mod_destructive(gall,h,psm,m,0,&nfm);
1.1       noro     1395:                        get_eg(&tnfm1); add_eg(&eg_nfm,&tnfm0,&tnfm1);
                   1396:                } else
                   1397:                        nfm = (DP)1;
                   1398:                if ( nfm ) {
                   1399:                        if ( Demand ) {
                   1400:                                if ( dp_load_t(psn,&nf) ) {
                   1401:                                        skip_nf_flag = 1;
1.14      noro     1402:                                        tnf1=tsp1;
1.1       noro     1403:                                        goto skip_nf;
                   1404:                                } else {
                   1405:                                        skip_nf_flag = 0;
                   1406:                                        dp_load(l->dp1,&dp1); dp_load(l->dp2,&dp2);
                   1407:                                        dp_sp(dp1,dp2,&h);
                   1408:                                }
                   1409:                        } else
                   1410:                                dp_sp(ps[l->dp1],ps[l->dp2],&h);
1.7       noro     1411:                        if ( GenTrace ) {
                   1412:                                STOQ(l->dp1,q); ARG1(BDY((LIST)BDY(NEXT(TraceList)))) = q;
                   1413:                                STOQ(l->dp2,q); ARG1(BDY((LIST)BDY(TraceList))) = q;
                   1414:                        }
1.10      noro     1415:                        if ( h )
                   1416:                                new_sugar = h->sugar;
1.1       noro     1417:                        get_eg(&tnf0);
                   1418:                        t_0 = get_rtime();
1.18      noro     1419:                        if ( PCoeffs || dp_fcoeffs )
1.16      noro     1420:                                _dp_nf(gall,h,ps,!Top,&nf);
                   1421:                        else
                   1422:                                _dp_nf_ptozp(gall,h,ps,!Top,DP_Multiple,&nf);
1.12      noro     1423:                        if ( DP_Print )
1.1       noro     1424:                                fprintf(asir_out,"(%.3g)",get_rtime()-t_0);
                   1425:                        get_eg(&tnf1); add_eg(&eg_nf,&tnf0,&tnf1);
                   1426:                } else
                   1427:                        nf = 0;
                   1428: skip_nf:
                   1429:                if ( nf ) {
                   1430:                        NZR++;
                   1431:                        get_eg(&tpz0);
                   1432:                        prim_part(nf,0,&h);
                   1433:                        get_eg(&tpz1); add_eg(&eg_pz,&tpz0,&tpz1);
                   1434:                        get_eg(&tnp0);
                   1435:                        if ( Demand && skip_nf_flag )
                   1436:                                nh = newps_nosave(h,m,subst);
                   1437:                        else
                   1438:                                nh = newps(h,m,subst);
                   1439:                        get_eg(&tnp1); add_eg(&eg_np,&tnp0,&tnp1);
                   1440:                        if ( nh < 0 )
                   1441:                                return 0;
                   1442:                        d = updpairs(d,g,nh);
                   1443:                        g = updbase(g,nh);
                   1444:                        gall = append_one(gall,nh);
                   1445:                        if ( !dp_fcoeffs && ShowMag ) {
                   1446:                                for ( mag = 0, mp = BDY(h); mp; mp = NEXT(mp) )
                   1447:                                        mag += p_mag((P)mp->c);
                   1448:                                Max_mag = MAX(Max_mag,mag);
                   1449:                        }
1.12      noro     1450:                        if ( DP_Print ) {
1.1       noro     1451:                                if ( !prev )
                   1452:                                        fprintf(asir_out,"\n");
                   1453:                                print_split_e(&tnf0,&tnf1); print_split_e(&tpz0,&tpz1);
                   1454:                                printdl(psh[nh]);
                   1455:                                fprintf(asir_out,"(%d,%d),nb=%d,nab=%d,rp=%d,sugar=%d",
                   1456:                                        l->dp1,l->dp2,length(g),length(gall),DPPlength(d),
                   1457:                                        pss[nh]);
                   1458:                                if ( ShowMag )
                   1459:                                        fprintf(asir_out,",mag=%d",mag);
                   1460:                                fprintf(asir_out,"\n"); fflush(asir_out);
1.12      noro     1461:                        } else if ( DP_PrintShort ) {
1.1       noro     1462:                                fprintf(asir_out,"+"); fflush(asir_out);
                   1463:                        }
                   1464:                        prev = 1;
                   1465:                } else {
                   1466:                        if ( m )
                   1467:                                add_eg(&eg_znfm,&tnfm0,&tnfm1);
                   1468:                        ZR++;
1.12      noro     1469:                        if ( DP_Print || DP_PrintShort ) {
1.6       noro     1470:                                if ( new_sugar != prev_sugar ) {
                   1471:                                        fprintf(asir_out,"[%d]",new_sugar);
                   1472:                                        prev_sugar = new_sugar;
1.1       noro     1473:                                }
                   1474:                                fprintf(asir_out,"."); fflush(asir_out); prev = 0;
                   1475:                        }
                   1476:                }
                   1477:        }
1.12      noro     1478:        if ( DP_Print || DP_PrintShort )
1.1       noro     1479:                fprintf(asir_out,"gb done\n");
                   1480:        return g;
                   1481: }
                   1482:
                   1483: NODE gb_mod(f,m)
                   1484: NODE f;
                   1485: int m;
                   1486: {
                   1487:        int i,nh,prev;
                   1488:        NODE r,g,gall;
                   1489:        DP_pairs d,d1;
                   1490:        DP_pairs l;
                   1491:        DP h,nf;
                   1492:        struct oEGT tnfm0,tnfm1,tspm0,tspm1,tmp0,tmp1,tpz0,tpz1;
                   1493:
                   1494:        prev = 1;
                   1495:        for ( gall = g = 0, d = 0, r = f; r; r = NEXT(r) ) {
                   1496:                i = (int)BDY(r);
                   1497:                d = updpairs(d,g,i);
                   1498:                g = updbase(g,i);
                   1499:                gall = append_one(gall,i);
                   1500:        }
                   1501:        while ( d ) {
1.14      noro     1502:                l = minp(d,&d);
1.1       noro     1503:                if ( PCoeffs ) {
                   1504:                        dp_sp_mod(ps[l->dp1],ps[l->dp2],m,&h);
1.14      noro     1505:                        get_eg(&tnfm0);
1.1       noro     1506:                        dp_nf_mod(gall,h,ps,m,!Top,&nf);
                   1507:                } else {
1.6       noro     1508:                        _dp_sp_mod_dup(ps[l->dp1],ps[l->dp2],m,&h);
1.14      noro     1509:                        get_eg(&tnfm0);
1.6       noro     1510:                        _dp_nf_mod_destructive(gall,h,ps,m,!Top,&nf);
1.1       noro     1511:                }
                   1512:                get_eg(&tnfm1); add_eg(&eg_nfm,&tnfm0,&tnfm1);
                   1513:                if ( nf ) {
                   1514:                        NZR++;
                   1515:                        get_eg(&tpz0);
                   1516:                        prim_part(nf,m,&h);
                   1517:                        get_eg(&tpz1); add_eg(&eg_pz,&tpz0,&tpz1);
                   1518:                        nh = newps_mod(h,m);
                   1519:                        if ( nh < 0 )
                   1520:                                return 0;
                   1521:                        d = updpairs(d,g,nh);
                   1522:                        g = updbase(g,nh);
                   1523:                        gall = append_one(gall,nh);
1.12      noro     1524:                        if ( DP_Print ) {
1.1       noro     1525:                                if ( !prev )
                   1526:                                        fprintf(asir_out,"\n");
                   1527:                                print_split_eg(&tnfm0,&tnfm1); fflush(asir_out);
                   1528:                                fprintf(asir_out,"(%d,%d),nb=%d,nab=%d,rp=%d,sugar=%d",l->dp1,l->dp2,length(g),length(gall),DPPlength(d),pss[nh]);
                   1529:                                printdl(psh[nh]); fprintf(asir_out,"\n"); fflush(asir_out);
1.12      noro     1530:                        } else if ( DP_PrintShort ) {
1.6       noro     1531:                                fprintf(asir_out,"+"); fflush(asir_out);
1.1       noro     1532:                        }
                   1533:                        prev = 1;
                   1534:                } else {
                   1535:                        add_eg(&eg_znfm,&tnfm0,&tnfm1);
                   1536:                        ZR++;
1.12      noro     1537:                        if ( DP_Print || DP_PrintShort ) {
1.1       noro     1538:                                fprintf(asir_out,"."); fflush(asir_out); prev = 0;
                   1539:                        }
                   1540:                }
                   1541:        }
1.12      noro     1542:        if ( DP_Print || DP_PrintShort )
1.1       noro     1543:                fprintf(asir_out,"gb_mod done\n");
                   1544:        return g;
                   1545: }
                   1546:
1.14      noro     1547: DP_pairs updpairs( d, g, t)
                   1548: DP_pairs d;
                   1549: NODE /* of index */ g;
                   1550: int t;
1.1       noro     1551: {
1.14      noro     1552:        register DP_pairs d1, dd, nd;
                   1553:        struct oEGT tup0,tup1;
                   1554:        int dl,dl1;
1.1       noro     1555:
1.14      noro     1556:        if ( !g ) return d;
                   1557:        if ( !NoCriB && d ) {
                   1558:                dl = DPPlength(d);
                   1559:                d = criterion_B( d, t );
                   1560:                dl -= DPPlength(d); NBP += dl;
1.1       noro     1561:        }
1.14      noro     1562:        d1 = newpairs( g, t );
                   1563:        if ( NEXT(d1) ) {
                   1564:                dl = DPPlength(d1); TP += dl;
                   1565:                d1 = criterion_M( d1 );
                   1566:                dl1 = DPPlength(d1); NMP += (dl-dl1); dl = dl1;
                   1567:                d1 = criterion_F( d1 );
                   1568:                dl1 = DPPlength(d1); NFP += (dl-dl1); dl = dl1;
                   1569:        } else
                   1570:                dl = 1;
                   1571:        if ( !do_weyl )
                   1572:                for ( dd = 0; d1; d1 = nd ) {
                   1573:                        nd = NEXT(d1);
                   1574:                        if ( !criterion_2( d1->dp1, d1->dp2 ) ) {
                   1575:                                NEXT(d1) = dd;
                   1576:                                dd = d1;
1.1       noro     1577:                        }
                   1578:                }
1.14      noro     1579:        else
                   1580:                dd = d1;
                   1581:        dl1 = DPPlength(dd); NDP += (dl-dl1);
                   1582:        if ( !(nd = d) ) return dd;
                   1583:        while ( nd = NEXT(d1 = nd) ) ;
                   1584:        NEXT(d1) = dd;
                   1585:        return d;
1.1       noro     1586: }
                   1587:
1.14      noro     1588: DP_pairs newpairs( g, t )
                   1589: NODE /* of index */ g;
                   1590: register int t;
                   1591: {
                   1592:        register NODE r;
                   1593:        register DL tdl = psh[t];
                   1594:        register int ts;
                   1595:        register DP_pairs p, last;
                   1596:        int dp;
                   1597:        register DL dl;
                   1598:        register int s;
1.1       noro     1599:
1.14      noro     1600:        ts = pss[t] - tdl->td;
                   1601:        for ( last = 0, r = g; r; r = NEXT(r) ) {
                   1602:                NEXT(p = NEWDP_pairs) = last;
                   1603:                last = p;
                   1604:                dp = p->dp1 = (int)BDY(r);  p->dp2 = t;
                   1605:                p->lcm = lcm_of_DL(CNVars, dl = psh[dp], tdl, (DL)0 );
                   1606: #if 0
                   1607:                if ( do_weyl )
                   1608:                        p->sugar = dl_weight(p->lcm);
                   1609:                else
                   1610: #endif
                   1611:                        p->sugar = (ts > (s = pss[dp] - dl->td) ? ts : s) + p->lcm->td;
                   1612:        }
                   1613:        return last;
                   1614: }
1.1       noro     1615:
1.14      noro     1616: DP_pairs criterion_B( d, s )
                   1617: register DP_pairs d;
                   1618: int s;
1.1       noro     1619: {
1.14      noro     1620:        register DP_pairs dd, p;
                   1621:        register DL tij, t = psh[s], dltmp;
1.1       noro     1622:
1.14      noro     1623:        if ( !d ) return 0;
                   1624:        NEWDL( dltmp, CNVars );
                   1625:        for ( dd = 0; d; d = p ) {
                   1626:                p = NEXT(d),
                   1627:                tij = d->lcm;
                   1628:                if ( tij->td != lcm_of_DL(CNVars, tij, t, dltmp )->td
                   1629:                        || !dl_equal(CNVars, tij, dltmp )
                   1630:                        || (tij->td == lcm_of_DL(CNVars, psh[d->dp1], t, dltmp )->td
                   1631:                            && dl_equal(CNVars, dltmp, tij ))
                   1632:                        || (tij->td == lcm_of_DL(CNVars, psh[d->dp2], t, dltmp )->td
                   1633:                            && dl_equal(CNVars, dltmp, tij )) ) {
                   1634:                        NEXT(d) = dd;
                   1635:                        dd = d;
                   1636:                }
1.1       noro     1637:        }
1.14      noro     1638:        return dd;
                   1639: }
1.1       noro     1640:
1.14      noro     1641: DP_pairs criterion_M( d1 )
                   1642: DP_pairs d1;
                   1643: {
                   1644:        register DP_pairs dd, e, d3, d2, p;
                   1645:        register DL itdl, jtdl;
                   1646:        register int itdltd, jtdltd;
1.1       noro     1647:
1.14      noro     1648:        for ( dd = 0, e = d1; e; e = d3 ) {
                   1649:                if ( !(d2 = NEXT(e)) ) {
                   1650:                        NEXT(e) = dd;
                   1651:                        return e;
                   1652:                }
                   1653:                itdltd = (itdl = e->lcm)->td;
                   1654:                for ( d3 = 0; d2; d2 = p ) {
                   1655:                        p = NEXT(d2),
                   1656:                        jtdltd = (jtdl = d2->lcm)->td;
                   1657:                        if ( jtdltd == itdltd  )
                   1658:                                if ( dl_equal(CNVars, itdl, jtdl ) ) ;
                   1659:                                else if ( dl_redble( jtdl, itdl ) ) continue;
                   1660:                                else if ( dl_redble( itdl, jtdl ) ) goto delit;
                   1661:                                else ;
                   1662:                        else if ( jtdltd > itdltd )
                   1663:                                if ( dl_redble( jtdl, itdl ) ) continue;
1.1       noro     1664:                                else ;
                   1665:                        else if ( dl_redble( itdl, jtdl ) ) goto delit;
                   1666:                        NEXT(d2) = d3;
                   1667:                        d3 = d2;
                   1668:                }
                   1669:                NEXT(e) = dd;
                   1670:                dd = e;
                   1671:                continue;
                   1672:                /**/
                   1673:        delit:  NEXT(d2) = d3;
                   1674:                d3 = d2;
                   1675:                for ( ; p; p = d2 ) {
                   1676:                        d2 = NEXT(p);
                   1677:                        NEXT(p) = d3;
                   1678:                        d3 = p;
                   1679:                }
                   1680:        }
                   1681:        return dd;
                   1682: }
                   1683:
                   1684: static DP_pairs collect_pairs_of_hdlcm( d1, prest )
                   1685: DP_pairs d1, *prest;
                   1686: {
                   1687:        register DP_pairs w, p, r, s;
                   1688:        register DL ti;
                   1689:        register int td;
                   1690:
                   1691:        td = (ti = (w = d1)->lcm)->td;
                   1692:        s = NEXT(w);
                   1693:        NEXT(w) = 0;
                   1694:        for ( r = 0; s; s = p ) {
                   1695:                p = NEXT(s);
                   1696:                if ( td == s->lcm->td && dl_equal(CNVars, ti, s->lcm ) )
                   1697:                {
                   1698:                        NEXT(s) = w;
                   1699:                        w = s;
                   1700:                } else {
                   1701:                        NEXT(s) = r;
                   1702:                        r = s;
                   1703:                }
                   1704:        }
                   1705:        *prest = r;
                   1706:        return w;
                   1707: }
                   1708:
                   1709: int criterion_2( dp1, dp2 )
                   1710: int dp1, dp2;
                   1711: {
                   1712:        register int i, *d1, *d2;
                   1713:
                   1714:        d1 = psh[dp1]->d,  d2 = psh[dp2]->d;
                   1715:        for ( i = CNVars; --i >= 0; d1++, d2++ )
                   1716:                if ( (*d1 <= *d2 ? *d1 : *d2) > 0  ) return 0;
                   1717:        return 1;
                   1718: }
                   1719:
                   1720: DP_pairs criterion_F( d1 )
                   1721: DP_pairs d1;
                   1722: {
                   1723:        DP_pairs rest, head;
                   1724:        register DP_pairs last, p, r, w;
                   1725:        register int s;
                   1726:
                   1727:        for ( head = last = 0, p = d1; NEXT(p); ) {
                   1728:                s = (r = w = collect_pairs_of_hdlcm( p, &rest ))->sugar;
                   1729:                while ( w = NEXT(w) )
                   1730:                        if ( criterion_2( w->dp1, w->dp2 ) ) {
                   1731:                                r = w;
                   1732:                                break;
                   1733:                        } else if ( w->sugar < s ) s = (r = w)->sugar;
                   1734:                if ( last ) NEXT(last) = r;
                   1735:                else head = r;
                   1736:                NEXT(last = r) = 0;
                   1737:                if ( !(p = rest) ) return head;
                   1738:        }
                   1739:        if ( !last ) return p;
                   1740:        NEXT(last) = p;
                   1741:        return head;
                   1742: }
                   1743:
                   1744: NODE updbase(g,t)
                   1745: NODE g;
                   1746: int t;
                   1747: {
                   1748:        g = remove_reducibles(g,t);
                   1749:        g = append_one(g,t);
                   1750:        return g;
                   1751: }
                   1752:
                   1753: NODE /* of index */ remove_reducibles( nd, newdp )
                   1754: NODE /* of index */ nd;
                   1755: int newdp;
                   1756: {
                   1757:        register DL dl, dln;
                   1758:        register NODE last, p, head;
                   1759:        register int td;
                   1760:
                   1761:        dl = psh[newdp];
                   1762:        td = dl->td;
                   1763:        for ( head = last = 0, p = nd; p; ) {
                   1764:                dln = psh[(int)BDY(p)];
                   1765:                if ( dln->td >= td && dl_redble( dln, dl ) ) {
                   1766:                        p = NEXT(p);
                   1767:                        if ( last ) NEXT(last) = p;
                   1768:                } else {
                   1769:                        if ( !last ) head = p;
                   1770:                        p = NEXT(last = p);
                   1771:                }
                   1772:        }
                   1773:        return head;
                   1774: }
                   1775:
                   1776: int dl_redble( dl1, dl2 )
                   1777: DL dl1, dl2;
                   1778: {
                   1779:        register int n, *d1, *d2;
                   1780:
                   1781:        for ( d1 = dl1->d, d2 = dl2->d, n = CNVars; --n >= 0; d1++, d2++ )
                   1782:                if ( *d1 < *d2 ) return 0;
                   1783:        return 1;
1.5       noro     1784: }
                   1785:
                   1786: int dl_weight(dl)
                   1787: DL dl;
                   1788: {
                   1789:        int n,w,i;
                   1790:
                   1791:        n = CNVars/2;
                   1792:        for ( i = 0, w = 0; i < n; i++ )
                   1793:                w += (-dl->d[i]+dl->d[n+i]);
                   1794:        return w;
1.1       noro     1795: }
                   1796:
                   1797: int gbcheck(f)
                   1798: NODE f;
                   1799: {
                   1800:        int i;
                   1801:        NODE r,g,gall;
                   1802:        DP_pairs d,l;
                   1803:        DP h,nf,dp1,dp2;
                   1804:        struct oEGT tmp0,tmp1;
                   1805:
                   1806:        if ( NoGC )
                   1807:                return 1;
                   1808:        for ( gall = g = 0, d = 0, r = f; r; r = NEXT(r) ) {
                   1809:                i = (int)BDY(r);
                   1810:                d = updpairs(d,g,i);
                   1811:                g = updbase(g,i);
                   1812:                gall = append_one(gall,i);
                   1813:        }
1.12      noro     1814:        if ( DP_Print || DP_PrintShort ) {
1.1       noro     1815:                fprintf(asir_out,"gbcheck total %d pairs\n",DPPlength(d)); fflush(asir_out);
                   1816:        }
                   1817:        while ( d ) {
                   1818:                l = d; d = NEXT(d);
                   1819:                get_eg(&tmp0);
1.17      noro     1820:                dp_load(l->dp1,&dp1); dp_load(l->dp2,&dp2);
                   1821:                dp_sp(dp1,dp2,&h);
                   1822: /* fprintf(stderr,"{%d,%d}",l->dp1,l->dp2); */
1.16      noro     1823:                _dp_nf(gall,h,ps,1,&nf);
1.1       noro     1824:                get_eg(&tmp1); add_eg(&eg_gc,&tmp0,&tmp1);
1.12      noro     1825:                if ( DP_Print || DP_PrintShort ) {
1.1       noro     1826:                        fprintf(asir_out,"."); fflush(asir_out);
                   1827:                }
                   1828:                if ( nf )
                   1829:                        return 0;
                   1830:        }
1.12      noro     1831:        if ( DP_Print || DP_PrintShort )
1.1       noro     1832:                fprintf(asir_out,"\n");
                   1833:        return 1;
                   1834: }
                   1835:
                   1836: int membercheck(f,x)
                   1837: NODE f,x;
                   1838: {
                   1839:        DP g;
                   1840:        struct oEGT tmp0,tmp1;
                   1841:
                   1842:        if ( NoMC )
                   1843:                return 1;
1.12      noro     1844:        if ( DP_Print || DP_PrintShort ) {
1.1       noro     1845:                fprintf(asir_out,"membercheck\n"); fflush(asir_out);
                   1846:        }
                   1847:        for ( ; f; f = NEXT(f) ) {
                   1848:                get_eg(&tmp0);
1.16      noro     1849:                _dp_nf(x,(DP)BDY(f),ps,1,&g);
1.1       noro     1850:                get_eg(&tmp1); add_eg(&eg_mc,&tmp0,&tmp1);
1.12      noro     1851:                if ( DP_Print ) {
1.1       noro     1852:                        print_split_eg(&tmp0,&tmp1); fflush(asir_out);
1.12      noro     1853:                } else if ( DP_PrintShort ) {
1.1       noro     1854:                        fprintf(asir_out,"."); fflush(asir_out);
                   1855:                }
                   1856:                if ( g )
                   1857:                        return 0;
                   1858:        }
1.12      noro     1859:        if ( DP_Print || DP_PrintShort )
1.1       noro     1860:                fprintf(asir_out,"\n");
                   1861:        return 1;
                   1862: }
                   1863:
                   1864: void dp_set_flag(name,value)
                   1865: Obj name,value;
                   1866: {
                   1867:        char *n;
                   1868:        int v;
                   1869:
                   1870:        if ( OID(name) != O_STR )
                   1871:                return;
                   1872:        n = BDY((STRING)name);
                   1873:        if ( !strcmp(n,"Demand") ) {
                   1874:                Demand = value ? BDY((STRING)value) : 0; return;
                   1875:        }
                   1876:        if ( !strcmp(n,"Dist") ) {
                   1877:                Dist = (LIST)value; return;
                   1878:        }
                   1879:        if ( value && OID(value) != O_N )
                   1880:                return;
                   1881:        v = QTOS((Q)value);
                   1882:        if ( !strcmp(n,"NoSugar") )
                   1883:                NoSugar = v;
                   1884:        else if ( !strcmp(n,"NoCriB") )
                   1885:                NoCriB = v;
                   1886:        else if ( !strcmp(n,"NoGC") )
                   1887:                NoGC = v;
                   1888:        else if ( !strcmp(n,"NoMC") )
                   1889:                NoMC = v;
                   1890:        else if ( !strcmp(n,"NoRA") )
                   1891:                NoRA = v;
                   1892:        else if ( !strcmp(n,"NoGCD") )
                   1893:                NoGCD = v;
                   1894:        else if ( !strcmp(n,"Top") )
                   1895:                Top = v;
                   1896:        else if ( !strcmp(n,"ShowMag") )
                   1897:                ShowMag = v;
1.14      noro     1898:        else if ( !strcmp(n,"PrintShort") )
1.12      noro     1899:                DP_PrintShort = v;
1.14      noro     1900:        else if ( !strcmp(n,"Print") )
1.12      noro     1901:                DP_Print = v;
1.14      noro     1902:        else if ( !strcmp(n,"NFStat") )
                   1903:                DP_NFStat = v;
1.1       noro     1904:        else if ( !strcmp(n,"Stat") )
                   1905:                Stat = v;
                   1906:        else if ( !strcmp(n,"Reverse") )
                   1907:                Reverse = v;
                   1908:        else if ( !strcmp(n,"Multiple") )
1.14      noro     1909:                DP_Multiple = v;
1.1       noro     1910:        else if ( !strcmp(n,"Denominator") )
                   1911:                Denominator = v;
                   1912:        else if ( !strcmp(n,"PtozpRA") )
                   1913:                PtozpRA = v;
1.7       noro     1914:        else if ( !strcmp(n,"GenTrace") )
                   1915:                GenTrace = v;
                   1916:        else if ( !strcmp(n,"OXCheck") )
                   1917:                OXCheck = v;
1.1       noro     1918: }
                   1919:
                   1920: void dp_make_flaglist(list)
                   1921: LIST *list;
                   1922: {
                   1923:        Q v;
                   1924:        STRING name,path;
                   1925:        NODE n,n1;
                   1926:
1.14      noro     1927:        STOQ(DP_Multiple,v); MKNODE(n,v,0); MKSTR(name,"DP_Multiple"); MKNODE(n1,name,n); n = n1;
1.1       noro     1928:        STOQ(Denominator,v); MKNODE(n1,v,n); n = n1; MKSTR(name,"Denominator"); MKNODE(n1,name,n); n = n1;
                   1929:        MKNODE(n1,Dist,n); n = n1; MKSTR(name,"Dist"); MKNODE(n1,name,n); n = n1;
                   1930:        STOQ(Reverse,v); MKNODE(n1,v,n); n = n1; MKSTR(name,"Reverse"); MKNODE(n1,name,n); n = n1;
                   1931:        STOQ(Stat,v); MKNODE(n1,v,n); n = n1; MKSTR(name,"Stat"); MKNODE(n1,name,n); n = n1;
1.14      noro     1932:        STOQ(DP_Print,v); MKNODE(n1,v,n); n = n1; MKSTR(name,"Print"); MKNODE(n1,name,n); n = n1;
                   1933:        STOQ(DP_NFStat,v); MKNODE(n1,v,n); n = n1; MKSTR(name,"NFStat"); MKNODE(n1,name,n); n = n1;
1.7       noro     1934:        STOQ(OXCheck,v); MKNODE(n1,v,n); n = n1; MKSTR(name,"OXCheck"); MKNODE(n1,name,n); n = n1;
                   1935:        STOQ(GenTrace,v); MKNODE(n1,v,n); n = n1; MKSTR(name,"GenTrace"); MKNODE(n1,name,n); n = n1;
1.1       noro     1936:        STOQ(PtozpRA,v); MKNODE(n1,v,n); n = n1; MKSTR(name,"PtozpRA"); MKNODE(n1,name,n); n = n1;
                   1937:        STOQ(ShowMag,v); MKNODE(n1,v,n); n = n1; MKSTR(name,"ShowMag"); MKNODE(n1,name,n); n = n1;
                   1938:        STOQ(Top,v); MKNODE(n1,v,n); n = n1; MKSTR(name,"Top"); MKNODE(n1,name,n); n = n1;
                   1939:        STOQ(NoGCD,v); MKNODE(n1,v,n); n = n1; MKSTR(name,"NoGCD"); MKNODE(n1,name,n); n = n1;
                   1940:        STOQ(NoRA,v); MKNODE(n1,v,n); n = n1; MKSTR(name,"NoRA"); MKNODE(n1,name,n); n = n1;
                   1941:        STOQ(NoMC,v); MKNODE(n1,v,n); n = n1; MKSTR(name,"NoMC"); MKNODE(n1,name,n); n = n1;
                   1942:        STOQ(NoGC,v); MKNODE(n1,v,n); n = n1; MKSTR(name,"NoGC"); MKNODE(n1,name,n); n = n1;
                   1943:        STOQ(NoCriB,v); MKNODE(n1,v,n); n = n1; MKSTR(name,"NoCriB"); MKNODE(n1,name,n); n = n1;
                   1944:        STOQ(NoSugar,v); MKNODE(n1,v,n); n = n1; MKSTR(name,"NoSugar"); MKNODE(n1,name,n); n = n1;
                   1945:        if ( Demand )
                   1946:                MKSTR(path,Demand);
                   1947:        else
                   1948:                path = 0;
                   1949:        MKNODE(n1,path,n); n = n1; MKSTR(name,"Demand"); MKNODE(n1,name,n); n = n1;
                   1950:        MKLIST(*list,n);
                   1951: }
                   1952:
                   1953: #define DELIM '/'
                   1954:
1.7       noro     1955: void dp_save(index,p,prefix)
1.1       noro     1956: int index;
1.7       noro     1957: Obj p;
                   1958: char *prefix;
1.1       noro     1959: {
                   1960:        FILE *fp;
                   1961:        char path[BUFSIZ];
                   1962:
1.7       noro     1963:        if ( prefix )
                   1964:                sprintf(path,"%s%c%s%d",Demand,DELIM,prefix,index);
                   1965:        else
                   1966:                sprintf(path,"%s%c%d",Demand,DELIM,index);
1.1       noro     1967:        if ( !(fp = fopen(path,"wb") ) )
                   1968:                error("dp_save : cannot open a file");
1.7       noro     1969:        savevl(fp,VC); saveobj(fp,p); fclose(fp);
1.1       noro     1970: }
                   1971:
                   1972: void dp_load(index,p)
                   1973: int index;
                   1974: DP *p;
                   1975: {
                   1976:        FILE *fp;
                   1977:        char path[BUFSIZ];
                   1978:
                   1979:        if ( !Demand || ps[index] )
                   1980:                *p = ps[index];
                   1981:        else {
                   1982:                sprintf(path,"%s%c%d",Demand,DELIM,index);
                   1983:                if ( !(fp = fopen(path,"rb") ) )
                   1984:                        error("dp_load : cannot open a file");
                   1985:                skipvl(fp); loadobj(fp,(Obj *)p); fclose(fp);
                   1986:        }
                   1987: }
                   1988:
                   1989: int dp_load_t(index,p)
                   1990: int index;
                   1991: DP *p;
                   1992: {
                   1993:        FILE *fp;
                   1994:        char path[BUFSIZ];
                   1995:
                   1996:        sprintf(path,"%s%c%d",Demand,DELIM,index);
                   1997:        if ( !(fp = fopen(path,"rb") ) )
                   1998:                return 0;
                   1999:        else {
                   2000:                skipvl(fp); loadobj(fp,(Obj *)p); fclose(fp); return 1;
                   2001:        }
                   2002: }
                   2003:
                   2004: void init_stat() {
                   2005:        init_eg(&eg_nf); init_eg(&eg_nfm); init_eg(&eg_znfm);
1.14      noro     2006:        init_eg(&eg_pz); init_eg(&eg_np);
1.1       noro     2007:        init_eg(&eg_ra); init_eg(&eg_mc); init_eg(&eg_gc);
1.14      noro     2008:        ZR = NZR = TP = NBP = NFP = NDP = 0;
1.1       noro     2009: }
                   2010:
                   2011: void print_stat() {
1.12      noro     2012:        if ( !DP_Print && !Stat )
1.1       noro     2013:                return;
                   2014:        print_eg("NF",&eg_nf); print_eg("NFM",&eg_nfm); print_eg("ZNFM",&eg_znfm);
1.14      noro     2015:        print_eg("PZ",&eg_pz); print_eg("NP",&eg_np);
1.1       noro     2016:        print_eg("RA",&eg_ra); print_eg("MC",&eg_mc); print_eg("GC",&eg_gc);
                   2017:        fprintf(asir_out,"T=%d,B=%d M=%d F=%d D=%d ZR=%d NZR=%d\n",TP,NBP,NMP,NFP,NDP,ZR,NZR);
                   2018: }
1.14      noro     2019:
                   2020: /*
                   2021:  * dp_nf used in gb()
                   2022:  *
                   2023:  */
                   2024:
                   2025: double pz_t_e, pz_t_d, pz_t_d1, pz_t_c, im_t_s, im_t_r;
                   2026:
                   2027: extern int GenTrace;
                   2028: extern NODE TraceList;
1.15      noro     2029: extern int mpi_mag;
                   2030:
                   2031: void dp_mulc_d(p,c,r)
                   2032: DP p;
                   2033: P c;
                   2034: DP *r;
                   2035: {
                   2036:        if ( Dist && BDY(Dist)
                   2037:                && HMAG(p) > mpi_mag
                   2038:                && p_mag((P)c) > mpi_mag ) {
                   2039:                if ( DP_NFStat ) fprintf(asir_out,"~");
                   2040:                dp_imul_d(p,(Q)c,r);
                   2041:        } else {
                   2042:                if ( DP_NFStat ) fprintf(asir_out,"_");
                   2043:                muldc(CO,p,c,r);
                   2044:        }
                   2045: }
1.14      noro     2046:
1.16      noro     2047: void _dp_nf(b,g,ps,full,rp)
                   2048: NODE b;
                   2049: DP g;
                   2050: DP *ps;
                   2051: int full;
                   2052: DP *rp;
                   2053: {
                   2054:        DP u,p,d,s,t,mult;
                   2055:        P coef;
                   2056:        NODE l;
                   2057:        MP m,mr;
                   2058:        int sugar,psugar;
                   2059:
                   2060:        if ( !g ) {
                   2061:                *rp = 0; return;
                   2062:        }
                   2063:        sugar = g->sugar;
                   2064:        for ( d = 0; g; ) {
                   2065:                for ( u = 0, l = b; l; l = NEXT(l) ) {
                   2066:                        if ( dl_redble(BDY(g)->dl,psh[(int)BDY(l)]) ) {
                   2067:                                dp_load((int)BDY(l),&p);
                   2068:                                /* t+u = coef*(d+g) - mult*p (t = coef*d) */
                   2069:                                dp_red(d,g,p,&t,&u,&coef,&mult);
                   2070:                                psugar = (BDY(g)->dl->td - BDY(p)->dl->td) + p->sugar;
                   2071:                                sugar = MAX(sugar,psugar);
                   2072:                                if ( GenTrace ) {
                   2073:                                        LIST hist;
                   2074:                                        Q cq;
                   2075:                                        NODE node,node0;
                   2076:
                   2077:                                        STOQ((int)BDY(l),cq);
                   2078:                                        node0 = mknode(4,coef,cq,mult,ONE);
                   2079:                                        MKLIST(hist,node0);
                   2080:                                        MKNODE(node,hist,TraceList); TraceList = node;
                   2081:                                }
                   2082:                                if ( !u ) {
                   2083:                                        if ( d )
                   2084:                                                d->sugar = sugar;
                   2085:                                        *rp = d; return;
                   2086:                                }
                   2087:                                d = t;
                   2088:                                break;
                   2089:                        }
                   2090:                }
                   2091:                if ( u )
                   2092:                        g = u;
                   2093:                else if ( !full ) {
                   2094:                        if ( g ) {
                   2095:                                MKDP(g->nv,BDY(g),t); t->sugar = sugar; g = t;
                   2096:                        }
                   2097:                        *rp = g; return;
                   2098:                } else {
                   2099:                        m = BDY(g); NEWMP(mr); mr->dl = m->dl; mr->c = m->c;
                   2100:                        NEXT(mr) = 0; MKDP(g->nv,mr,t); t->sugar = mr->dl->td;
                   2101:                        addd(CO,d,t,&s); d = s;
                   2102:                        dp_rest(g,&t); g = t;
                   2103:                }
                   2104:        }
                   2105:        if ( d )
                   2106:                d->sugar = sugar;
                   2107:        *rp = d;
                   2108: }
                   2109:
                   2110: void _dp_nf_ptozp(b,g,ps,full,multiple,r)
1.14      noro     2111: NODE b;
                   2112: DP g;
                   2113: DP *ps;
                   2114: int full,multiple;
                   2115: DP *r;
                   2116: {
                   2117:        DP u,dp,rp,t,t1,t2,red,shift;
                   2118:        Q dc,rc,dcq,rcq,cont,hr,hred,cr,cred,mcred,c,gcd,cq;
                   2119:        N gn,tn,cn;
                   2120:        NODE l;
                   2121:        MP m,mr;
                   2122:        int hmag,denom;
                   2123:        int sugar,psugar;
                   2124:        NODE dist;
                   2125:        STRING imul;
                   2126:        int kara_bit;
                   2127:        double get_rtime();
1.15      noro     2128:        double t_0,t_00,tt,ttt,t_p,t_m,t_g,t_a;
1.14      noro     2129:        LIST hist;
                   2130:        NODE node;
                   2131:        Q rcred,mrcred;
                   2132:
                   2133:        if ( !g ) {
                   2134:                *r = 0; return;
                   2135:        }
                   2136:        pz_t_e = pz_t_d = pz_t_d1 = pz_t_c = 0;
1.15      noro     2137:        t_p = t_m = t_g = t_a = 0;
1.14      noro     2138:
                   2139:        denom = Denominator?Denominator:1;
                   2140:        hmag = multiple*HMAG(g)/denom;
                   2141:        sugar = g->sugar;
                   2142:
                   2143:        dc = 0; dp = 0; rc = ONE; rp = g;
                   2144:        MKSTR(imul,"dp_imul_index");
                   2145:
                   2146:        /* g = dc*dp+rc*rp */
                   2147:        for ( ; rp; ) {
                   2148:                for ( u = 0, l = b; l; l = NEXT(l) ) {
                   2149:                        if ( dl_redble(BDY(rp)->dl,psh[(int)BDY(l)]) ) {
                   2150:                                t_0 = get_rtime();
                   2151:                                dp_load((int)BDY(l),&red);
                   2152:                                hr = (Q)BDY(rp)->c; hred = (Q)BDY(red)->c;
                   2153:                                igcd_cofactor((Q)BDY(rp)->c,(Q)BDY(red)->c,&gcd,&cred,&cr);
1.15      noro     2154:                                tt = get_rtime(); t_p += tt-t_0;
1.14      noro     2155:
                   2156:                                dp_subd(rp,red,&shift);
1.15      noro     2157:                                dp_mulc_d(rp,cr,&t);
1.14      noro     2158:                                chsgnp((P)cred,(P *)&mcred);
1.15      noro     2159:                                dp_mulc_d(red,mcred,&t1);
                   2160:                                muld(CO,shift,t1,&t1);
                   2161:                                addd(CO,t,t1,&u);
                   2162:                                t_m += get_rtime()-tt;
1.14      noro     2163:
                   2164:                                psugar = (BDY(rp)->dl->td - BDY(red)->dl->td) + red->sugar;
                   2165:                                sugar = MAX(sugar,psugar);
1.15      noro     2166:
1.14      noro     2167:                                if ( GenTrace ) {
                   2168:                                        /* u = cr*rp + (-cred)*shift*red */
                   2169:                                        STOQ((int)BDY(l),cq);
                   2170:                                        node = mknode(4,cr,cq,0,0);
                   2171:                                        mulq(cred,rc,&rcred);
                   2172:                                        chsgnnum((Num)rcred,(Num *)&mrcred);
                   2173:                                        muldc(CO,shift,(P)mrcred,(DP *)&ARG2(node));
                   2174:                                        MKLIST(hist,node);
                   2175:                                }
1.15      noro     2176:
1.14      noro     2177:                                if ( !u ) {
                   2178:                                        if ( dp )
                   2179:                                                dp->sugar = sugar;
                   2180:                                        *r = dp;
                   2181:                                        if ( GenTrace ) {
                   2182:                                                ARG3(BDY(hist)) = ONE;
                   2183:                                                MKNODE(node,hist,TraceList); TraceList = node;
                   2184:                                        }
                   2185:                                        goto final;
                   2186:                                }
                   2187:                                break;
                   2188:                        }
                   2189:                }
                   2190:                if ( u ) {
                   2191:                        if ( multiple && HMAG(u) > hmag ) {
                   2192:                                t_0 = get_rtime();
1.16      noro     2193:                                dp_ptozp_d(u,&rp);
1.15      noro     2194:                                tt = get_rtime(); t_g += tt-t_0;
                   2195:
                   2196:                                divsq((Q)BDY(u)->c,(Q)BDY(rp)->c,&cont);
1.14      noro     2197:                                if ( !dp_fcoeffs && DP_NFStat ) {
1.15      noro     2198:                                        fprintf(asir_out,
                   2199:                                                "(%d)",p_mag((P)cont)*100/p_mag((P)BDY(u)->c));
1.14      noro     2200:                                        fflush(asir_out);
                   2201:                                }
1.15      noro     2202:                                mulq(cr,dc,&dcq); mulq(cont,rc,&rcq);
1.14      noro     2203:                                igcd_cofactor(dcq,rcq,&gcd,&dc,&rc);
1.15      noro     2204:                                t_a = get_rtime()-tt;
                   2205:
1.14      noro     2206:                                hmag = multiple*HMAG(rp)/denom;
                   2207:                                if ( GenTrace ) {
                   2208:                                        ARG3(BDY(hist)) = (pointer)gcd;
                   2209:                                        MKNODE(node,hist,TraceList); TraceList = node;
                   2210:                                }
                   2211:                        } else {
1.15      noro     2212:                                rp = u;
1.14      noro     2213:                                t_0 = get_rtime();
1.15      noro     2214:                                mulq(cr,dc,&dc);
                   2215:                                t_a += get_rtime()-t_0;
1.14      noro     2216:                                if ( GenTrace ) {
                   2217:                                        ARG3(BDY(hist)) = (pointer)ONE;
                   2218:                                        MKNODE(node,hist,TraceList); TraceList = node;
                   2219:                                }
                   2220:                        }
                   2221:                } else if ( !full ) {
                   2222:                        if ( rp ) {
                   2223:                                MKDP(rp->nv,BDY(rp),t); t->sugar = sugar; rp = t;
                   2224:                        }
                   2225:                        *r = rp;
                   2226:                        goto final;
                   2227:                } else {
                   2228:                        t_0 = get_rtime();
                   2229:                        mulq((Q)BDY(rp)->c,rc,&c);
1.15      noro     2230:                        igcd_cofactor(dc,c,&dc,&dcq,&cq);
                   2231:                        muldc(CO,dp,(P)dcq,&t);
                   2232:                        dp_hm(rp,&t1); BDY(t1)->c = (P)cq;  addd(CO,t,t1,&dp);
                   2233:                        dp_rest(rp,&rp);
                   2234:                        t_a += get_rtime()-t_0;
1.14      noro     2235:                }
                   2236:        }
                   2237:        if ( GenTrace ) {
                   2238:                mulq(ARG3(BDY((LIST)BDY(TraceList))),dc,&cq);
                   2239:                ARG3(BDY((LIST)BDY(TraceList))) = (pointer)cq;
                   2240:        }
                   2241:        if ( dp )
                   2242:                dp->sugar = sugar;
                   2243:        *r = dp;
                   2244: final:
                   2245:        if ( DP_NFStat )
1.15      noro     2246:                fprintf(asir_out,
                   2247:                        "(%.3g %.3g %.3g %.3g %.3g %.3g %.3g %.3g)",
                   2248:                        t_p,t_m,t_g,t_a,
1.14      noro     2249:                        pz_t_e, pz_t_d, pz_t_d1, pz_t_c);
                   2250: }
                   2251:
                   2252: void imulv();
                   2253:
                   2254: void dp_imul_d(p,q,rp)
                   2255: DP p;
                   2256: Q q;
                   2257: DP *rp;
                   2258: {
                   2259:        int nsep,ndist,i,j,k,l,n;
                   2260:        double t0,t1,t2;
                   2261:        Q *s;
                   2262:        pointer *b;
                   2263:        VECT c,cs,ri;
                   2264:        VECT *r;
                   2265:        MP m;
                   2266:        NODE tn,dist,n0,n1,n2;
                   2267:        Obj dmy;
                   2268:        STRING imul;
                   2269:        extern LIST Dist;
                   2270:
                   2271:        if ( !p || !q ) {
                   2272:                *rp = 0; return;
                   2273:        }
                   2274:        dist = BDY(Dist);
                   2275:        for ( tn = dist, ndist = 0; tn; tn = NEXT(tn), ndist++ );
                   2276:        nsep = ndist + 1;
                   2277:        for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   2278:        if ( n <= nsep ) {
                   2279:                muldc(CO,p,(P)q,rp); return;
                   2280:        }
                   2281:        MKSTR(imul,"imulv");
                   2282:        t0 = get_rtime();
                   2283:        dp_dtov(p,&c);
                   2284:        sepvect(c,nsep,&cs);
                   2285:        r = (VECT *)CALLOC(nsep,sizeof(VECT *));
                   2286:        for ( i = 0, tn = dist, b = BDY(cs); i < ndist; i++, tn = NEXT(tn) ) {
                   2287:                n0 = mknode(4,BDY(tn),imul,b[i],q);
                   2288:                Pox_rpc(n0,&dmy);
                   2289:        }
                   2290:        t1 = get_rtime();
                   2291:        im_t_s += t1 - t0;
                   2292:        imulv(b[i],q,&r[i]);
                   2293:        t1 = get_rtime();
                   2294:        for ( i = 0, tn = dist; i < ndist; i++, tn = NEXT(tn) ) {
                   2295:                MKNODE(n0,BDY(tn),0);
                   2296:                Pox_pop_local(n0,&r[i]);
                   2297:                if ( OID(r[i]) == O_ERR ) {
                   2298:                        printexpr(CO,(Obj)r[i]);
                   2299:                        error("dp_imul_d : aborted");
                   2300:                }
                   2301:        }
                   2302:        t2 = get_rtime();
                   2303:        im_t_r += t2 - t1;
                   2304:        s = (Q *)CALLOC(n,sizeof(Q));
                   2305:        for ( i = j = 0; i < nsep; i++ ) {
                   2306:                for ( k = 0, ri = r[i], l = ri->len; k < l; k++, j++ ) {
                   2307:                        s[j] = (Q)BDY(ri)[k];
                   2308:                }
                   2309:        }
                   2310:        dp_vtod(s,p,rp);
                   2311: }
                   2312:
                   2313: void imulv(w,c,rp)
                   2314: VECT w;
                   2315: Q c;
                   2316: VECT *rp;
                   2317: {
                   2318:        int n,i;
                   2319:        VECT r;
                   2320:
                   2321:        n = w->len;
                   2322:        MKVECT(r,n); *rp = r;
                   2323:        for ( i = 0; i < n; i++ )
                   2324:                mulq((Q)BDY(w)[i],(Q)c,(Q *)&BDY(r)[i]);
                   2325: }
                   2326:

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