[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.56

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

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