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

Annotation of OpenXM_contrib2/asir2000/engine/nd.c, Revision 1.146

1.146   ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.145 2006/08/26 05:38:06 noro Exp $ */
1.2       noro        2:
1.94      noro        3: #include "nd.h"
1.63      noro        4:
1.131     noro        5: int diag_period = 6;
1.61      noro        6: int (*ndl_compare_function)(UINT *a1,UINT *a2);
1.94      noro        7: int nd_dcomp;
                      8: NM _nm_free_list;
                      9: ND _nd_free_list;
                     10: ND_pairs _ndp_free_list;
1.32      noro       11:
1.146   ! noro       12: static NODE nd_subst;
        !            13: static VL nd_vc;
1.121     noro       14: static int nd_ntrans;
1.117     noro       15: static int nd_nalg;
1.103     noro       16: #if 0
1.74      noro       17: static int ndv_alloc;
1.103     noro       18: #endif
1.87      noro       19: #if 1
1.69      noro       20: static int nd_f4_nsp=0x7fffffff;
1.87      noro       21: #else
                     22: static int nd_f4_nsp=50;
                     23: #endif
1.42      noro       24: static double nd_scale=2;
1.61      noro       25: static UINT **nd_bound;
1.42      noro       26: static struct order_spec *nd_ord;
                     27: static EPOS nd_epos;
1.43      noro       28: static BlockMask nd_blockmask;
1.42      noro       29: static int nd_nvar;
                     30: static int nd_isrlex;
                     31: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
1.61      noro       32: static UINT nd_mask[32];
                     33: static UINT nd_mask0,nd_mask1;
1.42      noro       34:
1.20      noro       35: static NDV *nd_ps;
1.53      noro       36: static NDV *nd_ps_trace;
1.42      noro       37: static RHist *nd_psh;
                     38: static int nd_psn,nd_pslen;
                     39: static RHist *nd_red;
1.96      noro       40: static int *nd_work_vector;
                     41: static int **nd_matrix;
                     42: static int nd_matrix_len;
1.97      noro       43: static struct weight_or_block *nd_worb;
                     44: static int nd_worb_len;
1.42      noro       45: static int nd_found,nd_create,nd_notfirst;
                     46: static int nmv_adv;
1.77      noro       47: static int nd_demand;
1.1       noro       48:
1.119     noro       49: NumberField get_numberfield();
1.114     noro       50: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
                     51: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
1.118     noro       52: int nd_monic(int m,ND *p);
1.129     noro       53: NDV plain_vect_to_ndv_q(Q *mat,int col,UINT *s0vect);
1.114     noro       54:
1.1       noro       55: void nd_free_private_storage()
                     56: {
                     57:        _nm_free_list = 0;
1.5       noro       58:        _ndp_free_list = 0;
1.71      noro       59: #if 0
1.1       noro       60:        GC_gcollect();
1.71      noro       61: #endif
1.1       noro       62: }
                     63:
                     64: void _NM_alloc()
                     65: {
                     66:        NM p;
                     67:        int i;
                     68:
1.11      noro       69:        for ( i = 0; i < 1024; i++ ) {
1.61      noro       70:                p = (NM)GC_malloc(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.1       noro       71:                p->next = _nm_free_list; _nm_free_list = p;
                     72:        }
                     73: }
                     74:
                     75: void _ND_alloc()
                     76: {
                     77:        ND p;
                     78:        int i;
                     79:
                     80:        for ( i = 0; i < 1024; i++ ) {
                     81:                p = (ND)GC_malloc(sizeof(struct oND));
                     82:                p->body = (NM)_nd_free_list; _nd_free_list = p;
                     83:        }
                     84: }
                     85:
                     86: void _NDP_alloc()
                     87: {
                     88:        ND_pairs p;
                     89:        int i;
                     90:
1.11      noro       91:        for ( i = 0; i < 1024; i++ ) {
1.1       noro       92:                p = (ND_pairs)GC_malloc(sizeof(struct oND_pairs)
1.61      noro       93:                        +(nd_wpd-1)*sizeof(UINT));
1.1       noro       94:                p->next = _ndp_free_list; _ndp_free_list = p;
                     95:        }
                     96: }
                     97:
1.30      noro       98: INLINE int nd_length(ND p)
1.1       noro       99: {
                    100:        NM m;
                    101:        int i;
                    102:
                    103:        if ( !p )
                    104:                return 0;
                    105:        else {
                    106:                for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
                    107:                return i;
                    108:        }
                    109: }
                    110:
1.61      noro      111: INLINE int ndl_reducible(UINT *d1,UINT *d2)
1.1       noro      112: {
1.61      noro      113:        UINT u1,u2;
1.1       noro      114:        int i,j;
                    115:
1.34      noro      116:        if ( TD(d1) < TD(d2) ) return 0;
1.65      noro      117: #if USE_UNROLL
1.1       noro      118:        switch ( nd_bpe ) {
1.62      noro      119:                case 3:
                    120:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    121:                                u1 = d1[i]; u2 = d2[i];
                    122:                                if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
                    123:                                if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
                    124:                                if ( (u1&  0xe00000) < (u2&  0xe00000) ) return 0;
                    125:                                if ( (u1&  0x1c0000) < (u2&  0x1c0000) ) return 0;
                    126:                                if ( (u1&   0x38000) < (u2&   0x38000) ) return 0;
                    127:                                if ( (u1&    0x7000) < (u2&    0x7000) ) return 0;
                    128:                                if ( (u1&     0xe00) < (u2&     0xe00) ) return 0;
                    129:                                if ( (u1&     0x1c0) < (u2&     0x1c0) ) return 0;
                    130:                                if ( (u1&      0x38) < (u2&      0x38) ) return 0;
                    131:                                if ( (u1&       0x7) < (u2&       0x7) ) return 0;
                    132:                        }
                    133:                        return 1;
                    134:                        break;
1.1       noro      135:                case 4:
1.41      noro      136:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      137:                                u1 = d1[i]; u2 = d2[i];
                    138:                                if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
1.62      noro      139:                                if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
                    140:                                if ( (u1&  0xf00000) < (u2&  0xf00000) ) return 0;
                    141:                                if ( (u1&   0xf0000) < (u2&   0xf0000) ) return 0;
                    142:                                if ( (u1&    0xf000) < (u2&    0xf000) ) return 0;
                    143:                                if ( (u1&     0xf00) < (u2&     0xf00) ) return 0;
                    144:                                if ( (u1&      0xf0) < (u2&      0xf0) ) return 0;
                    145:                                if ( (u1&       0xf) < (u2&       0xf) ) return 0;
1.1       noro      146:                        }
                    147:                        return 1;
                    148:                        break;
                    149:                case 6:
1.41      noro      150:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      151:                                u1 = d1[i]; u2 = d2[i];
                    152:                                if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
1.62      noro      153:                                if ( (u1&  0xfc0000) < (u2&  0xfc0000) ) return 0;
                    154:                                if ( (u1&   0x3f000) < (u2&   0x3f000) ) return 0;
                    155:                                if ( (u1&     0xfc0) < (u2&     0xfc0) ) return 0;
                    156:                                if ( (u1&      0x3f) < (u2&      0x3f) ) return 0;
1.1       noro      157:                        }
                    158:                        return 1;
                    159:                        break;
                    160:                case 8:
1.41      noro      161:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      162:                                u1 = d1[i]; u2 = d2[i];
                    163:                                if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
1.62      noro      164:                                if ( (u1&  0xff0000) < (u2&  0xff0000) ) return 0;
                    165:                                if ( (u1&    0xff00) < (u2&    0xff00) ) return 0;
                    166:                                if ( (u1&      0xff) < (u2&      0xff) ) return 0;
1.1       noro      167:                        }
                    168:                        return 1;
                    169:                        break;
                    170:                case 16:
1.41      noro      171:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      172:                                u1 = d1[i]; u2 = d2[i];
                    173:                                if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
1.62      noro      174:                                if ( (u1&    0xffff) < (u2&    0xffff) ) return 0;
1.1       noro      175:                        }
                    176:                        return 1;
                    177:                        break;
                    178:                case 32:
1.41      noro      179:                        for ( i = nd_exporigin; i < nd_wpd; i++ )
1.1       noro      180:                                if ( d1[i] < d2[i] ) return 0;
                    181:                        return 1;
                    182:                        break;
                    183:                default:
1.41      noro      184:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      185:                                u1 = d1[i]; u2 = d2[i];
                    186:                                for ( j = 0; j < nd_epw; j++ )
                    187:                                        if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    188:                        }
                    189:                        return 1;
                    190:        }
1.65      noro      191: #else
                    192:        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    193:                u1 = d1[i]; u2 = d2[i];
                    194:                for ( j = 0; j < nd_epw; j++ )
                    195:                        if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    196:        }
                    197:        return 1;
                    198: #endif
1.1       noro      199: }
                    200:
1.61      noro      201: /*
                    202:  * If the current order is a block order,
                    203:  * then the last block is length 1 and contains
                    204:  * the homo variable. Otherwise, the original
                    205:  * order is either 0 or 2.
                    206:  */
                    207:
                    208: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int weight)
1.23      noro      209: {
1.61      noro      210:        int w,i,e,n,omask0;
                    211:
                    212:        omask0 = (1<<obpe)-1;
                    213:        n = nd_nvar-1;
                    214:        ndl_zero(r);
                    215:        for ( i = 0; i < n; i++ ) {
                    216:                e = GET_EXP_OLD(d,i);
                    217:                PUT_EXP(r,i,e);
                    218:        }
                    219:        w = TD(d);
                    220:        PUT_EXP(r,nd_nvar-1,weight-w);
                    221:        TD(r) = weight;
                    222:        if ( nd_blockmask ) ndl_weight_mask(r);
                    223: }
                    224:
                    225: void ndl_dehomogenize(UINT *d)
                    226: {
                    227:        UINT mask;
                    228:        UINT h;
1.31      noro      229:        int i,bits;
1.23      noro      230:
1.44      noro      231:        if ( nd_blockmask ) {
                    232:                h = GET_EXP(d,nd_nvar-1);
1.45      noro      233:                XOR_EXP(d,nd_nvar-1,h);
1.44      noro      234:                TD(d) -= h;
                    235:                d[nd_exporigin-1] -= h;
                    236:        } else {
                    237:                if ( nd_isrlex ) {
                    238:                        if ( nd_bpe == 32 ) {
                    239:                                h = d[nd_exporigin];
                    240:                                for ( i = nd_exporigin+1; i < nd_wpd; i++ )
                    241:                                        d[i-1] = d[i];
                    242:                                d[i-1] = 0;
                    243:                                TD(d) -= h;
                    244:                        } else {
                    245:                                bits = nd_epw*nd_bpe;
                    246:                                mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
                    247:                                h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
                    248:                                for ( i = nd_exporigin; i < nd_wpd; i++ )
                    249:                                        d[i] = ((d[i]<<nd_bpe)&mask)
                    250:                                                |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
                    251:                                TD(d) -= h;
                    252:                        }
1.45      noro      253:                } else {
                    254:                        h = GET_EXP(d,nd_nvar-1);
                    255:                        XOR_EXP(d,nd_nvar-1,h);
                    256:                        TD(d) -= h;
                    257:                }
1.44      noro      258:        }
1.23      noro      259: }
                    260:
1.61      noro      261: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
1.1       noro      262: {
1.61      noro      263:        UINT t1,t2,u,u1,u2;
1.43      noro      264:        int i,j,l;
1.1       noro      265:
1.65      noro      266: #if USE_UNROLL
1.1       noro      267:        switch ( nd_bpe ) {
1.62      noro      268:                case 3:
                    269:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    270:                                u1 = d1[i]; u2 = d2[i];
                    271:                                t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
                    272:                                t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
                    273:                                t1 = (u1&  0xe00000); t2 = (u2&  0xe00000); u |= t1>t2?t1:t2;
                    274:                                t1 = (u1&  0x1c0000); t2 = (u2&  0x1c0000); u |= t1>t2?t1:t2;
                    275:                                t1 = (u1&   0x38000); t2 = (u2&   0x38000); u |= t1>t2?t1:t2;
                    276:                                t1 = (u1&    0x7000); t2 = (u2&    0x7000); u |= t1>t2?t1:t2;
                    277:                                t1 = (u1&     0xe00); t2 = (u2&     0xe00); u |= t1>t2?t1:t2;
                    278:                                t1 = (u1&     0x1c0); t2 = (u2&     0x1c0); u |= t1>t2?t1:t2;
                    279:                                t1 = (u1&      0x38); t2 = (u2&      0x38); u |= t1>t2?t1:t2;
                    280:                                t1 = (u1&       0x7); t2 = (u2&       0x7); u |= t1>t2?t1:t2;
                    281:                                d[i] = u;
                    282:                        }
                    283:                        break;
1.1       noro      284:                case 4:
1.41      noro      285:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      286:                                u1 = d1[i]; u2 = d2[i];
                    287:                                t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
1.62      noro      288:                                t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
                    289:                                t1 = (u1&  0xf00000); t2 = (u2&  0xf00000); u |= t1>t2?t1:t2;
                    290:                                t1 = (u1&   0xf0000); t2 = (u2&   0xf0000); u |= t1>t2?t1:t2;
                    291:                                t1 = (u1&    0xf000); t2 = (u2&    0xf000); u |= t1>t2?t1:t2;
                    292:                                t1 = (u1&     0xf00); t2 = (u2&     0xf00); u |= t1>t2?t1:t2;
                    293:                                t1 = (u1&      0xf0); t2 = (u2&      0xf0); u |= t1>t2?t1:t2;
                    294:                                t1 = (u1&       0xf); t2 = (u2&       0xf); u |= t1>t2?t1:t2;
1.1       noro      295:                                d[i] = u;
                    296:                        }
                    297:                        break;
                    298:                case 6:
1.41      noro      299:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      300:                                u1 = d1[i]; u2 = d2[i];
                    301:                                t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
1.62      noro      302:                                t1 = (u1&  0xfc0000); t2 = (u2&  0xfc0000); u |= t1>t2?t1:t2;
                    303:                                t1 = (u1&   0x3f000); t2 = (u2&   0x3f000); u |= t1>t2?t1:t2;
                    304:                                t1 = (u1&     0xfc0); t2 = (u2&     0xfc0); u |= t1>t2?t1:t2;
                    305:                                t1 = (u1&      0x3f); t2 = (u2&      0x3f); u |= t1>t2?t1:t2;
1.1       noro      306:                                d[i] = u;
                    307:                        }
                    308:                        break;
                    309:                case 8:
1.41      noro      310:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      311:                                u1 = d1[i]; u2 = d2[i];
                    312:                                t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
1.62      noro      313:                                t1 = (u1&  0xff0000); t2 = (u2&  0xff0000); u |= t1>t2?t1:t2;
                    314:                                t1 = (u1&    0xff00); t2 = (u2&    0xff00); u |= t1>t2?t1:t2;
                    315:                                t1 = (u1&      0xff); t2 = (u2&      0xff); u |= t1>t2?t1:t2;
1.1       noro      316:                                d[i] = u;
                    317:                        }
                    318:                        break;
                    319:                case 16:
1.41      noro      320:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      321:                                u1 = d1[i]; u2 = d2[i];
                    322:                                t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
1.62      noro      323:                                t1 = (u1&    0xffff); t2 = (u2&    0xffff); u |= t1>t2?t1:t2;
1.1       noro      324:                                d[i] = u;
                    325:                        }
                    326:                        break;
                    327:                case 32:
1.41      noro      328:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      329:                                u1 = d1[i]; u2 = d2[i];
                    330:                                d[i] = u1>u2?u1:u2;
                    331:                        }
                    332:                        break;
                    333:                default:
1.41      noro      334:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      335:                                u1 = d1[i]; u2 = d2[i];
                    336:                                for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    337:                                        t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    338:                                }
                    339:                                d[i] = u;
                    340:                        }
                    341:                        break;
                    342:        }
1.65      noro      343: #else
                    344:        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    345:                u1 = d1[i]; u2 = d2[i];
                    346:                for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    347:                        t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    348:                }
                    349:                d[i] = u;
                    350:        }
                    351: #endif
1.39      noro      352:        TD(d) = ndl_weight(d);
1.61      noro      353:        if ( nd_blockmask ) ndl_weight_mask(d);
1.57      noro      354: }
                    355:
1.61      noro      356: int ndl_weight(UINT *d)
1.1       noro      357: {
1.61      noro      358:        UINT t,u;
1.1       noro      359:        int i,j;
                    360:
1.60      noro      361:        if ( current_dl_weight_vector )
                    362:                for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    363:                        u = GET_EXP(d,i);
                    364:                        t += MUL_WEIGHT(u,i);
                    365:                }
                    366:        else
                    367:                for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    368:                        u = d[i];
                    369:                        for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    370:                                t += (u&nd_mask0);
                    371:                }
1.1       noro      372:        return t;
                    373: }
                    374:
1.61      noro      375: void ndl_weight_mask(UINT *d)
1.43      noro      376: {
1.61      noro      377:        UINT t,u;
                    378:        UINT *mask;
                    379:        int i,j,k,l;
1.43      noro      380:
1.61      noro      381:        l = nd_blockmask->n;
                    382:        for ( k = 0; k < l; k++ ) {
                    383:                mask = nd_blockmask->mask[k];
                    384:                if ( current_dl_weight_vector )
                    385:                        for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    386:                                u = GET_EXP_MASK(d,i,mask);
                    387:                                t += MUL_WEIGHT(u,i);
                    388:                        }
                    389:                else
                    390:                        for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    391:                                u = d[i]&mask[i];
                    392:                                for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    393:                                        t += (u&nd_mask0);
                    394:                        }
                    395:                d[k+1] = t;
                    396:        }
1.43      noro      397: }
                    398:
1.61      noro      399: int ndl_lex_compare(UINT *d1,UINT *d2)
1.1       noro      400: {
                    401:        int i;
                    402:
1.41      noro      403:        d1 += nd_exporigin;
                    404:        d2 += nd_exporigin;
                    405:        for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
1.1       noro      406:                if ( *d1 > *d2 )
1.32      noro      407:                        return nd_isrlex ? -1 : 1;
1.1       noro      408:                else if ( *d1 < *d2 )
1.32      noro      409:                        return nd_isrlex ? 1 : -1;
1.1       noro      410:        return 0;
                    411: }
                    412:
1.61      noro      413: int ndl_block_compare(UINT *d1,UINT *d2)
1.43      noro      414: {
                    415:        int i,l,j,ord_o,ord_l;
                    416:        struct order_pair *op;
1.61      noro      417:        UINT t1,t2,m;
                    418:        UINT *mask;
1.43      noro      419:
                    420:        l = nd_blockmask->n;
                    421:        op = nd_blockmask->order_pair;
                    422:        for ( j = 0; j < l; j++ ) {
                    423:                mask = nd_blockmask->mask[j];
                    424:                ord_o = op[j].order;
                    425:                if ( ord_o < 2 )
1.44      noro      426:                        if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
                    427:                        else if ( t1 < t2 ) return -1;
1.43      noro      428:                for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.44      noro      429:                        m = mask[i];
                    430:                        t1 = d1[i]&m;
                    431:                        t2 = d2[i]&m;
1.43      noro      432:                        if ( t1 > t2 )
                    433:                                return !ord_o ? -1 : 1;
                    434:                        else if ( t1 < t2 )
                    435:                                return !ord_o ? 1 : -1;
                    436:                }
                    437:        }
                    438:        return 0;
                    439: }
                    440:
1.96      noro      441: int ndl_matrix_compare(UINT *d1,UINT *d2)
                    442: {
                    443:        int i,j,s;
                    444:        int *v;
                    445:
                    446:        for ( j = 0; j < nd_nvar; j++ )
                    447:                nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
                    448:        for ( i = 0; i < nd_matrix_len; i++ ) {
                    449:                v = nd_matrix[i];
                    450:                for ( j = 0, s = 0; j < nd_nvar; j++ )
                    451:                        s += v[j]*nd_work_vector[j];
                    452:                if ( s > 0 ) return 1;
                    453:                else if ( s < 0 ) return -1;
                    454:        }
                    455:        return 0;
                    456: }
                    457:
1.97      noro      458: int ndl_composite_compare(UINT *d1,UINT *d2)
                    459: {
                    460:        int i,j,s,start,end,len,o;
                    461:        int *v;
                    462:        struct sparse_weight *sw;
                    463:
                    464:        for ( j = 0; j < nd_nvar; j++ )
                    465:                nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
                    466:        for ( i = 0; i < nd_worb_len; i++ ) {
                    467:                len = nd_worb[i].length;
                    468:                switch ( nd_worb[i].type ) {
                    469:                        case IS_DENSE_WEIGHT:
                    470:                                v = nd_worb[i].body.dense_weight;
                    471:                                for ( j = 0, s = 0; j < len; j++ )
                    472:                                        s += v[j]*nd_work_vector[j];
                    473:                                if ( s > 0 ) return 1;
                    474:                                else if ( s < 0 ) return -1;
                    475:                                break;
                    476:                        case IS_SPARSE_WEIGHT:
                    477:                                sw = nd_worb[i].body.sparse_weight;
                    478:                                for ( j = 0, s = 0; j < len; j++ )
                    479:                                        s += sw[j].value*nd_work_vector[sw[j].pos];
                    480:                                if ( s > 0 ) return 1;
                    481:                                else if ( s < 0 ) return -1;
                    482:                                break;
                    483:                        case IS_BLOCK:
                    484:                                o = nd_worb[i].body.block.order;
                    485:                                start = nd_worb[i].body.block.start;
                    486:                                switch ( o ) {
                    487:                                        case 0:
                    488:                                                end = start+len;
                    489:                                                for ( j = start, s = 0; j < end; j++ )
                    490:                                                        s += MUL_WEIGHT(nd_work_vector[j],j);
                    491:                                                if ( s > 0 ) return 1;
                    492:                                                else if ( s < 0 ) return -1;
                    493:                                                for ( j = end-1; j >= start; j-- )
                    494:                                                        if ( nd_work_vector[j] < 0 ) return 1;
                    495:                                                        else if ( nd_work_vector[j] > 0 ) return -1;
                    496:                                                break;
                    497:                                        case 1:
                    498:                                                end = start+len;
                    499:                                                for ( j = start, s = 0; j < end; j++ )
                    500:                                                        s += MUL_WEIGHT(nd_work_vector[j],j);
                    501:                                                if ( s > 0 ) return 1;
                    502:                                                else if ( s < 0 ) return -1;
                    503:                                                for ( j = start; j < end; j++ )
                    504:                                                        if ( nd_work_vector[j] > 0 ) return 1;
                    505:                                                        else if ( nd_work_vector[j] < 0 ) return -1;
                    506:                                                break;
                    507:                                        case 2:
                    508:                                                for ( j = start; j < end; j++ )
                    509:                                                        if ( nd_work_vector[j] > 0 ) return 1;
                    510:                                                        else if ( nd_work_vector[j] < 0 ) return -1;
                    511:                                                break;
                    512:                                }
                    513:                                break;
                    514:                }
                    515:        }
                    516:        return 0;
                    517: }
                    518:
1.58      noro      519: /* TDH -> WW -> TD-> RL */
                    520:
1.61      noro      521: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
1.58      noro      522: {
                    523:        int i,m,e1,e2;
                    524:
                    525:        if ( TD(d1) > TD(d2) ) return 1;
                    526:        else if ( TD(d1) < TD(d2) ) return -1;
                    527:        m = nd_nvar>>1;
                    528:        for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
                    529:                e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
                    530:                e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
                    531:        }
                    532:        if ( e1 > e2 ) return 1;
                    533:        else if ( e1 < e2 ) return -1;
                    534:        return ndl_lex_compare(d1,d2);
                    535: }
                    536:
1.61      noro      537: INLINE int ndl_equal(UINT *d1,UINT *d2)
1.1       noro      538: {
                    539:        int i;
                    540:
1.81      noro      541:        switch ( nd_wpd ) {
                    542:                case 2:
                    543:                        if ( TD(d2) != TD(d1) ) return 0;
                    544:                        if ( d2[1] != d1[1] ) return 0;
                    545:                        return 1;
                    546:                        break;
                    547:                case 3:
                    548:                        if ( TD(d2) != TD(d1) ) return 0;
                    549:                        if ( d2[1] != d1[1] ) return 0;
                    550:                        if ( d2[2] != d1[2] ) return 0;
                    551:                        return 1;
                    552:                        break;
                    553:                default:
                    554:                        for ( i = 0; i < nd_wpd; i++ )
                    555:                                if ( *d1++ != *d2++ ) return 0;
                    556:                        return 1;
                    557:                        break;
                    558:        }
1.1       noro      559: }
                    560:
1.61      noro      561: INLINE void ndl_copy(UINT *d1,UINT *d2)
1.6       noro      562: {
                    563:        int i;
                    564:
                    565:        switch ( nd_wpd ) {
1.41      noro      566:                case 2:
1.34      noro      567:                        TD(d2) = TD(d1);
                    568:                        d2[1] = d1[1];
1.6       noro      569:                        break;
1.41      noro      570:                case 3:
1.34      noro      571:                        TD(d2) = TD(d1);
1.6       noro      572:                        d2[1] = d1[1];
1.34      noro      573:                        d2[2] = d1[2];
1.6       noro      574:                        break;
                    575:                default:
1.41      noro      576:                        for ( i = 0; i < nd_wpd; i++ )
1.6       noro      577:                                d2[i] = d1[i];
                    578:                        break;
                    579:        }
                    580: }
                    581:
1.61      noro      582: INLINE void ndl_zero(UINT *d)
                    583: {
                    584:        int i;
                    585:        for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                    586: }
                    587:
                    588: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
1.1       noro      589: {
                    590:        int i;
                    591:
1.43      noro      592: #if 1
1.6       noro      593:        switch ( nd_wpd ) {
1.41      noro      594:                case 2:
                    595:                        TD(d) = TD(d1)+TD(d2);
1.34      noro      596:                        d[1] = d1[1]+d2[1];
1.6       noro      597:                        break;
1.41      noro      598:                case 3:
                    599:                        TD(d) = TD(d1)+TD(d2);
1.6       noro      600:                        d[1] = d1[1]+d2[1];
1.34      noro      601:                        d[2] = d1[2]+d2[2];
1.6       noro      602:                        break;
                    603:                default:
1.43      noro      604:                        for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
1.6       noro      605:                        break;
                    606:        }
1.43      noro      607: #else
                    608:        for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    609: #endif
1.6       noro      610: }
                    611:
1.55      noro      612: /* d1 += d2 */
1.61      noro      613: INLINE void ndl_addto(UINT *d1,UINT *d2)
1.55      noro      614: {
                    615:        int i;
                    616:
                    617: #if 1
                    618:        switch ( nd_wpd ) {
                    619:                case 2:
                    620:                        TD(d1) += TD(d2);
                    621:                        d1[1] += d2[1];
                    622:                        break;
                    623:                case 3:
                    624:                        TD(d1) += TD(d2);
                    625:                        d1[1] += d2[1];
                    626:                        d1[2] += d2[2];
                    627:                        break;
                    628:                default:
                    629:                        for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    630:                        break;
                    631:        }
                    632: #else
                    633:        for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    634: #endif
                    635: }
                    636:
1.61      noro      637: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
1.6       noro      638: {
                    639:        int i;
                    640:
1.43      noro      641:        for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
1.1       noro      642: }
                    643:
1.61      noro      644: int ndl_disjoint(UINT *d1,UINT *d2)
1.1       noro      645: {
1.61      noro      646:        UINT t1,t2,u,u1,u2;
1.1       noro      647:        int i,j;
                    648:
1.65      noro      649: #if USE_UNROLL
1.1       noro      650:        switch ( nd_bpe ) {
1.62      noro      651:                case 3:
                    652:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    653:                                u1 = d1[i]; u2 = d2[i];
                    654:                                t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
                    655:                                t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
                    656:                                t1 = u1&  0xe00000; t2 = u2&  0xe00000; if ( t1&&t2 ) return 0;
                    657:                                t1 = u1&  0x1c0000; t2 = u2&  0x1c0000; if ( t1&&t2 ) return 0;
                    658:                                t1 = u1&   0x38000; t2 = u2&   0x38000; if ( t1&&t2 ) return 0;
                    659:                                t1 = u1&    0x7000; t2 = u2&    0x7000; if ( t1&&t2 ) return 0;
                    660:                                t1 = u1&     0xe00; t2 = u2&     0xe00; if ( t1&&t2 ) return 0;
                    661:                                t1 = u1&     0x1c0; t2 = u2&     0x1c0; if ( t1&&t2 ) return 0;
                    662:                                t1 = u1&      0x38; t2 = u2&      0x38; if ( t1&&t2 ) return 0;
                    663:                                t1 = u1&       0x7; t2 = u2&       0x7; if ( t1&&t2 ) return 0;
                    664:                        }
                    665:                        return 1;
                    666:                        break;
1.1       noro      667:                case 4:
1.41      noro      668:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      669:                                u1 = d1[i]; u2 = d2[i];
                    670:                                t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
1.62      noro      671:                                t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
                    672:                                t1 = u1&  0xf00000; t2 = u2&  0xf00000; if ( t1&&t2 ) return 0;
                    673:                                t1 = u1&   0xf0000; t2 = u2&   0xf0000; if ( t1&&t2 ) return 0;
                    674:                                t1 = u1&    0xf000; t2 = u2&    0xf000; if ( t1&&t2 ) return 0;
                    675:                                t1 = u1&     0xf00; t2 = u2&     0xf00; if ( t1&&t2 ) return 0;
                    676:                                t1 = u1&      0xf0; t2 = u2&      0xf0; if ( t1&&t2 ) return 0;
                    677:                                t1 = u1&       0xf; t2 = u2&       0xf; if ( t1&&t2 ) return 0;
1.1       noro      678:                        }
                    679:                        return 1;
                    680:                        break;
                    681:                case 6:
1.41      noro      682:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      683:                                u1 = d1[i]; u2 = d2[i];
                    684:                                t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
1.62      noro      685:                                t1 = u1&  0xfc0000; t2 = u2&  0xfc0000; if ( t1&&t2 ) return 0;
                    686:                                t1 = u1&   0x3f000; t2 = u2&   0x3f000; if ( t1&&t2 ) return 0;
                    687:                                t1 = u1&     0xfc0; t2 = u2&     0xfc0; if ( t1&&t2 ) return 0;
                    688:                                t1 = u1&      0x3f; t2 = u2&      0x3f; if ( t1&&t2 ) return 0;
1.1       noro      689:                        }
                    690:                        return 1;
                    691:                        break;
                    692:                case 8:
1.41      noro      693:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      694:                                u1 = d1[i]; u2 = d2[i];
                    695:                                t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
1.62      noro      696:                                t1 = u1&  0xff0000; t2 = u2&  0xff0000; if ( t1&&t2 ) return 0;
                    697:                                t1 = u1&    0xff00; t2 = u2&    0xff00; if ( t1&&t2 ) return 0;
                    698:                                t1 = u1&      0xff; t2 = u2&      0xff; if ( t1&&t2 ) return 0;
1.1       noro      699:                        }
                    700:                        return 1;
                    701:                        break;
                    702:                case 16:
1.41      noro      703:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      704:                                u1 = d1[i]; u2 = d2[i];
                    705:                                t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
1.62      noro      706:                                t1 = u1&    0xffff; t2 = u2&    0xffff; if ( t1&&t2 ) return 0;
1.1       noro      707:                        }
                    708:                        return 1;
                    709:                        break;
                    710:                case 32:
1.41      noro      711:                        for ( i = nd_exporigin; i < nd_wpd; i++ )
1.1       noro      712:                                if ( d1[i] && d2[i] ) return 0;
                    713:                        return 1;
                    714:                        break;
                    715:                default:
1.41      noro      716:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      717:                                u1 = d1[i]; u2 = d2[i];
                    718:                                for ( j = 0; j < nd_epw; j++ ) {
                    719:                                        if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                    720:                                        u1 >>= nd_bpe; u2 >>= nd_bpe;
                    721:                                }
                    722:                        }
                    723:                        return 1;
                    724:                        break;
                    725:        }
1.65      noro      726: #else
                    727:        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    728:                u1 = d1[i]; u2 = d2[i];
                    729:                for ( j = 0; j < nd_epw; j++ ) {
                    730:                        if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                    731:                        u1 >>= nd_bpe; u2 >>= nd_bpe;
                    732:                }
                    733:        }
                    734:        return 1;
                    735: #endif
1.1       noro      736: }
                    737:
1.114     noro      738: int ndl_check_bound(UINT *d1,UINT *d2)
1.1       noro      739: {
1.61      noro      740:        UINT u2;
1.5       noro      741:        int i,j,ind,k;
1.1       noro      742:
1.5       noro      743:        ind = 0;
1.65      noro      744: #if USE_UNROLL
1.5       noro      745:        switch ( nd_bpe ) {
1.62      noro      746:                case 3:
                    747:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    748:                                u2 = d2[i];
                    749:                                if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
                    750:                                if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
                    751:                                if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
                    752:                                if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
                    753:                                if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
                    754:                                if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
                    755:                                if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
                    756:                                if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
                    757:                                if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
                    758:                                if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
                    759:                        }
                    760:                        return 0;
                    761:                        break;
1.5       noro      762:                case 4:
1.41      noro      763:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5       noro      764:                                u2 = d2[i];
                    765:                                if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
                    766:                                if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
                    767:                                if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
                    768:                                if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
                    769:                                if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
                    770:                                if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
                    771:                                if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
                    772:                                if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
                    773:                        }
                    774:                        return 0;
                    775:                        break;
                    776:                case 6:
1.41      noro      777:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5       noro      778:                                u2 = d2[i];
                    779:                                if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
                    780:                                if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
                    781:                                if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
                    782:                                if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
                    783:                                if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
                    784:                        }
                    785:                        return 0;
                    786:                        break;
                    787:                case 8:
1.41      noro      788:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5       noro      789:                                u2 = d2[i];
                    790:                                if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
                    791:                                if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
                    792:                                if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
                    793:                                if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
                    794:                        }
                    795:                        return 0;
                    796:                        break;
                    797:                case 16:
1.41      noro      798:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5       noro      799:                                u2 = d2[i];
                    800:                                if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
                    801:                                if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
                    802:                        }
                    803:                        return 0;
                    804:                        break;
                    805:                case 32:
1.41      noro      806:                        for ( i = nd_exporigin; i < nd_wpd; i++ )
1.5       noro      807:                                if ( d1[i]+d2[i]<d1[i] ) return 1;
                    808:                        return 0;
                    809:                        break;
                    810:                default:
1.41      noro      811:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5       noro      812:                                u2 = d2[i];
                    813:                                k = (nd_epw-1)*nd_bpe;
                    814:                                for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                    815:                                        if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                    816:                        }
                    817:                        return 0;
                    818:                        break;
                    819:        }
1.65      noro      820: #else
                    821:        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    822:                u2 = d2[i];
                    823:                k = (nd_epw-1)*nd_bpe;
                    824:                for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                    825:                        if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                    826:        }
                    827:        return 0;
                    828: #endif
1.1       noro      829: }
                    830:
1.114     noro      831: int ndl_check_bound2(int index,UINT *d2)
                    832: {
                    833:        return ndl_check_bound(nd_bound[index],d2);
                    834: }
                    835:
1.61      noro      836: INLINE int ndl_hash_value(UINT *d)
1.1       noro      837: {
                    838:        int i;
                    839:        int r;
                    840:
1.34      noro      841:        r = 0;
1.41      noro      842:        for ( i = 0; i < nd_wpd; i++ )
1.1       noro      843:                r = ((r<<16)+d[i])%REDTAB_LEN;
                    844:        return r;
                    845: }
                    846:
1.63      noro      847: INLINE int ndl_find_reducer(UINT *dg)
1.1       noro      848: {
1.13      noro      849:        RHist r;
1.6       noro      850:        int d,k,i;
1.1       noro      851:
1.63      noro      852:        d = ndl_hash_value(dg);
1.13      noro      853:        for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1.34      noro      854:                if ( ndl_equal(dg,DL(r)) ) {
1.1       noro      855:                        if ( k > 0 ) nd_notfirst++;
                    856:                        nd_found++;
1.13      noro      857:                        return r->index;
1.1       noro      858:                }
                    859:        }
1.13      noro      860:        if ( Reverse )
                    861:                for ( i = nd_psn-1; i >= 0; i-- ) {
                    862:                        r = nd_psh[i];
1.34      noro      863:                        if ( ndl_reducible(dg,DL(r)) ) {
1.13      noro      864:                                nd_create++;
1.34      noro      865:                                nd_append_red(dg,i);
1.13      noro      866:                                return i;
                    867:                        }
                    868:                }
                    869:        else
                    870:                for ( i = 0; i < nd_psn; i++ ) {
                    871:                        r = nd_psh[i];
1.34      noro      872:                        if ( ndl_reducible(dg,DL(r)) ) {
1.13      noro      873:                                nd_create++;
1.34      noro      874:                                nd_append_red(dg,i);
1.13      noro      875:                                return i;
                    876:                        }
1.1       noro      877:                }
1.6       noro      878:        return -1;
1.1       noro      879: }
                    880:
1.63      noro      881: ND nd_merge(ND p1,ND p2)
                    882: {
                    883:        int n,c;
                    884:        int t,can,td1,td2;
                    885:        ND r;
                    886:        NM m1,m2,mr0,mr,s;
                    887:
                    888:        if ( !p1 ) return p2;
                    889:        else if ( !p2 ) return p1;
                    890:        else {
                    891:                can = 0;
                    892:                for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                    893:                        c = DL_COMPARE(DL(m1),DL(m2));
                    894:                        switch ( c ) {
                    895:                                case 0:
                    896:                                        s = m1; m1 = NEXT(m1);
                    897:                                        can++; NEXTNM2(mr0,mr,s);
                    898:                                        s = m2; m2 = NEXT(m2); FREENM(s);
                    899:                                        break;
                    900:                                case 1:
                    901:                                        s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                    902:                                        break;
                    903:                                case -1:
                    904:                                        s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                    905:                                        break;
                    906:                        }
                    907:                }
                    908:                if ( !mr0 )
                    909:                        if ( m1 ) mr0 = m1;
                    910:                        else if ( m2 ) mr0 = m2;
                    911:                        else return 0;
                    912:                else if ( m1 ) NEXT(mr) = m1;
                    913:                else if ( m2 ) NEXT(mr) = m2;
                    914:                else NEXT(mr) = 0;
                    915:                BDY(p1) = mr0;
                    916:                SG(p1) = MAX(SG(p1),SG(p2));
                    917:                LEN(p1) = LEN(p1)+LEN(p2)-can;
                    918:                FREEND(p2);
                    919:                return p1;
                    920:        }
                    921: }
                    922:
1.31      noro      923: ND nd_add(int mod,ND p1,ND p2)
1.1       noro      924: {
                    925:        int n,c;
1.34      noro      926:        int t,can,td1,td2;
1.1       noro      927:        ND r;
                    928:        NM m1,m2,mr0,mr,s;
                    929:
1.34      noro      930:        if ( !p1 ) return p2;
                    931:        else if ( !p2 ) return p1;
1.71      noro      932:        else if ( mod == -1 ) return nd_add_sf(p1,p2);
1.113     noro      933:        else if ( !mod ) return nd_add_q(p1,p2);
1.1       noro      934:        else {
1.30      noro      935:                can = 0;
1.1       noro      936:                for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1.34      noro      937:                        c = DL_COMPARE(DL(m1),DL(m2));
1.1       noro      938:                        switch ( c ) {
                    939:                                case 0:
1.19      noro      940:                                        t = ((CM(m1))+(CM(m2))) - mod;
1.34      noro      941:                                        if ( t < 0 ) t += mod;
1.1       noro      942:                                        s = m1; m1 = NEXT(m1);
                    943:                                        if ( t ) {
1.34      noro      944:                                                can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1.17      noro      945:                                        } else {
1.34      noro      946:                                                can += 2; FREENM(s);
1.17      noro      947:                                        }
                    948:                                        s = m2; m2 = NEXT(m2); FREENM(s);
1.95      noro      949:                                        break;
                    950:                                case 1:
                    951:                                        s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                    952:                                        break;
                    953:                                case -1:
                    954:                                        s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                    955:                                        break;
                    956:                        }
                    957:                }
                    958:                if ( !mr0 )
                    959:                        if ( m1 ) mr0 = m1;
                    960:                        else if ( m2 ) mr0 = m2;
                    961:                        else return 0;
                    962:                else if ( m1 ) NEXT(mr) = m1;
                    963:                else if ( m2 ) NEXT(mr) = m2;
                    964:                else NEXT(mr) = 0;
                    965:                BDY(p1) = mr0;
                    966:                SG(p1) = MAX(SG(p1),SG(p2));
                    967:                LEN(p1) = LEN(p1)+LEN(p2)-can;
                    968:                FREEND(p2);
                    969:                return p1;
                    970:        }
                    971: }
                    972:
                    973: /* XXX on opteron, the inlined manipulation of destructive additon of
                    974:  * two NM seems to make gcc optimizer get confused, so the part is
                    975:  * done in a function.
                    976:  */
                    977:
1.113     noro      978: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
1.95      noro      979: {
                    980:        NM s;
1.146   ! noro      981:        P t;
1.95      noro      982:        int can;
                    983:
1.146   ! noro      984:        addp(nd_vc,CP(*m1),CP(*m2),&t);
1.95      noro      985:        s = *m1; *m1 = NEXT(*m1);
                    986:        if ( t ) {
1.146   ! noro      987:                can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
1.95      noro      988:        } else {
                    989:                can = 2; FREENM(s);
                    990:        }
                    991:        s = *m2; *m2 = NEXT(*m2); FREENM(s);
                    992:        return can;
                    993: }
                    994:
1.113     noro      995: ND nd_add_q(ND p1,ND p2)
1.95      noro      996: {
                    997:        int n,c,can;
                    998:        ND r;
                    999:        NM m1,m2,mr0,mr,s;
1.146   ! noro     1000:        P t;
1.95      noro     1001:
                   1002:        if ( !p1 ) return p2;
                   1003:        else if ( !p2 ) return p1;
                   1004:        else {
                   1005:                can = 0;
                   1006:                for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1007:                        c = DL_COMPARE(DL(m1),DL(m2));
                   1008:                        switch ( c ) {
                   1009:                                case 0:
                   1010: #if defined(__x86_64__)
1.113     noro     1011:                                        can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
1.95      noro     1012: #else
1.146   ! noro     1013:                                        addp(nd_vc,CP(m1),CP(m2),&t);
1.95      noro     1014:                                        s = m1; m1 = NEXT(m1);
                   1015:                                        if ( t ) {
1.146   ! noro     1016:                                                can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
1.95      noro     1017:                                        } else {
                   1018:                                                can += 2; FREENM(s);
                   1019:                                        }
                   1020:                                        s = m2; m2 = NEXT(m2); FREENM(s);
                   1021: #endif
1.17      noro     1022:                                        break;
                   1023:                                case 1:
                   1024:                                        s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1025:                                        break;
                   1026:                                case -1:
                   1027:                                        s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1028:                                        break;
                   1029:                        }
                   1030:                }
                   1031:                if ( !mr0 )
1.34      noro     1032:                        if ( m1 ) mr0 = m1;
                   1033:                        else if ( m2 ) mr0 = m2;
                   1034:                        else return 0;
                   1035:                else if ( m1 ) NEXT(mr) = m1;
                   1036:                else if ( m2 ) NEXT(mr) = m2;
                   1037:                else NEXT(mr) = 0;
1.17      noro     1038:                BDY(p1) = mr0;
                   1039:                SG(p1) = MAX(SG(p1),SG(p2));
1.31      noro     1040:                LEN(p1) = LEN(p1)+LEN(p2)-can;
1.17      noro     1041:                FREEND(p2);
                   1042:                return p1;
                   1043:        }
                   1044: }
                   1045:
1.71      noro     1046: ND nd_add_sf(ND p1,ND p2)
                   1047: {
                   1048:        int n,c,can;
                   1049:        ND r;
                   1050:        NM m1,m2,mr0,mr,s;
                   1051:        int t;
                   1052:
                   1053:        if ( !p1 ) return p2;
                   1054:        else if ( !p2 ) return p1;
                   1055:        else {
                   1056:                can = 0;
                   1057:                for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1058:                        c = DL_COMPARE(DL(m1),DL(m2));
                   1059:                        switch ( c ) {
                   1060:                                case 0:
                   1061:                                        t = _addsf(CM(m1),CM(m2));
                   1062:                                        s = m1; m1 = NEXT(m1);
                   1063:                                        if ( t ) {
                   1064:                                                can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1065:                                        } else {
                   1066:                                                can += 2; FREENM(s);
                   1067:                                        }
                   1068:                                        s = m2; m2 = NEXT(m2); FREENM(s);
                   1069:                                        break;
                   1070:                                case 1:
                   1071:                                        s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1072:                                        break;
                   1073:                                case -1:
                   1074:                                        s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1075:                                        break;
                   1076:                        }
                   1077:                }
                   1078:                if ( !mr0 )
                   1079:                        if ( m1 ) mr0 = m1;
                   1080:                        else if ( m2 ) mr0 = m2;
                   1081:                        else return 0;
                   1082:                else if ( m1 ) NEXT(mr) = m1;
                   1083:                else if ( m2 ) NEXT(mr) = m2;
                   1084:                else NEXT(mr) = 0;
                   1085:                BDY(p1) = mr0;
                   1086:                SG(p1) = MAX(SG(p1),SG(p2));
                   1087:                LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1088:                FREEND(p2);
                   1089:                return p1;
                   1090:        }
                   1091: }
                   1092:
1.146   ! noro     1093: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn)
        !          1094: {
        !          1095:        int c,c1,c2;
        !          1096:        Q cg,cred,gcd,tq;
        !          1097:        P cgp,credp,gcdp;
        !          1098:        Obj tr,tr1;
        !          1099:
        !          1100:        if ( mod == -1 )
        !          1101:                CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
        !          1102:        else if ( mod ) {
        !          1103:                c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
        !          1104:                DMAR(c1,c2,0,mod,c); CM(mul) = c;
        !          1105:        } else if ( nd_vc ) {
        !          1106:                ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
        !          1107:                divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
        !          1108:                chsgnp(cgp,&CP(mul));
        !          1109:                nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
        !          1110:                if ( dn ) {
        !          1111:                        mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
        !          1112:                        reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
        !          1113:                }
        !          1114:        } else {
        !          1115:                igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
        !          1116:                chsgnq(cg,&CQ(mul));
        !          1117:                nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
        !          1118:                if ( dn ) {
        !          1119:                        mulq(dn->z,cred,&tq); dn->z = tq;
        !          1120:                }
        !          1121:        }
        !          1122:        return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
        !          1123: }
        !          1124:
1.1       noro     1125: /* ret=1 : success, ret=0 : overflow */
1.146   ! noro     1126: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,NDC dn,ND *rp)
1.1       noro     1127: {
                   1128:        NM m,mrd,tail;
1.7       noro     1129:        NM mul;
1.10      noro     1130:        int n,sugar,psugar,sugar0,stat,index;
1.30      noro     1131:        int c,c1,c2,dummy;
1.17      noro     1132:        RHist h;
1.11      noro     1133:        NDV p,red;
1.113     noro     1134:        Q cg,cred,gcd,tq,qq;
1.21      noro     1135:        double hmag;
1.146   ! noro     1136:        P tp,tp1;
        !          1137:        Obj tr,tr1;
1.1       noro     1138:
1.113     noro     1139:        if ( dn ) {
                   1140:                if ( mod )
                   1141:                        dn->m = 1;
1.146   ! noro     1142:                else if ( nd_vc )
        !          1143:                        dn->r = (R)ONE;
1.113     noro     1144:                else
                   1145:                        dn->z = ONE;
                   1146:        }
1.1       noro     1147:        if ( !g ) {
1.146   ! noro     1148:                *rp = d;
1.1       noro     1149:                return 1;
                   1150:        }
1.146   ! noro     1151:        if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
1.21      noro     1152:
1.14      noro     1153:        sugar0 = sugar = SG(g);
1.1       noro     1154:        n = NV(g);
1.61      noro     1155:        mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.146   ! noro     1156:        if ( d )
        !          1157:                for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
        !          1158:        for ( ; g; ) {
1.63      noro     1159:                index = ndl_find_reducer(HDL(g));
1.6       noro     1160:                if ( index >= 0 ) {
1.17      noro     1161:                        h = nd_psh[index];
                   1162:                        ndl_sub(HDL(g),DL(h),DL(mul));
1.14      noro     1163:                        if ( ndl_check_bound2(index,DL(mul)) ) {
1.6       noro     1164:                                nd_free(g); nd_free(d);
                   1165:                                return 0;
                   1166:                        }
1.146   ! noro     1167:                        p = nd_demand ? ndv_load(index) : ps[index];
        !          1168:                        g = nd_reduce2(mod,d,g,p,mul,dn);
1.34      noro     1169:                        sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1.146   ! noro     1170:                        if ( !mod && g && ((double)(p_mag(HCP(g))) > hmag) ) {
        !          1171:                                tp = HCP(g);
1.21      noro     1172:                                nd_removecont2(d,g);
1.69      noro     1173:                                if ( dn ) {
1.146   ! noro     1174:                                        if ( nd_vc ) {
        !          1175:                                                divsp(nd_vc,tp,HCP(g),&tp1);
        !          1176:                                                divr(nd_vc,(Obj)dn->r,(Obj)tp1,&tr);
        !          1177:                                                reductr(nd_vc,(Obj)tr,&tr1); dn->r = (R)tr1;
        !          1178:                                        } else {
        !          1179:                                                divq((Q)tp,HCQ(g),&qq);
        !          1180:                                                divq(dn->z,qq,&tq); dn->z = tq;
        !          1181:                                        }
1.69      noro     1182:                                }
1.146   ! noro     1183:                                hmag = ((double)p_mag(HCP(g)))*nd_scale;
1.21      noro     1184:                        }
1.1       noro     1185:                } else if ( !full ) {
                   1186:                        *rp = g;
                   1187:                        return 1;
                   1188:                } else {
                   1189:                        m = BDY(g);
                   1190:                        if ( NEXT(m) ) {
1.34      noro     1191:                                BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1.1       noro     1192:                        } else {
                   1193:                                FREEND(g); g = 0;
                   1194:                        }
                   1195:                        if ( d ) {
1.34      noro     1196:                                NEXT(tail)=m; tail=m; LEN(d)++;
1.1       noro     1197:                        } else {
1.34      noro     1198:                                MKND(n,m,1,d); tail = BDY(d);
1.1       noro     1199:                        }
                   1200:                }
                   1201:        }
1.34      noro     1202:        if ( d ) SG(d) = sugar;
1.1       noro     1203:        *rp = d;
                   1204:        return 1;
                   1205: }
1.28      noro     1206:
1.53      noro     1207: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1.25      noro     1208: {
                   1209:        int hindex,index;
                   1210:        NDV p;
                   1211:        ND u,d,red;
                   1212:        NODE l;
1.31      noro     1213:        NM mul,m,mrd,tail;
1.25      noro     1214:        int sugar,psugar,n,h_reducible;
                   1215:        PGeoBucket bucket;
                   1216:        int c,c1,c2;
1.113     noro     1217:        Q cg,cred,gcd,zzz;
1.25      noro     1218:        RHist h;
1.28      noro     1219:        double hmag,gmag;
1.77      noro     1220:        int count = 0;
                   1221:        int hcount = 0;
1.25      noro     1222:
                   1223:        if ( !g ) {
                   1224:                *rp = 0;
                   1225:                return 1;
                   1226:        }
                   1227:        sugar = SG(g);
                   1228:        n = NV(g);
1.113     noro     1229:        if ( !mod ) hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1.25      noro     1230:        bucket = create_pbucket();
1.31      noro     1231:        add_pbucket(mod,bucket,g);
1.25      noro     1232:        d = 0;
1.61      noro     1233:        mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.25      noro     1234:        while ( 1 ) {
1.113     noro     1235:                hindex = mod?head_pbucket(mod,bucket):head_pbucket_q(bucket);
1.25      noro     1236:                if ( hindex < 0 ) {
1.77      noro     1237:                        if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1.34      noro     1238:                        if ( d ) SG(d) = sugar;
1.25      noro     1239:                        *rp = d;
                   1240:                        return 1;
                   1241:                }
                   1242:                g = bucket->body[hindex];
1.63      noro     1243:                index = ndl_find_reducer(HDL(g));
1.25      noro     1244:                if ( index >= 0 ) {
1.77      noro     1245:                        count++;
                   1246:                        if ( !d ) hcount++;
1.25      noro     1247:                        h = nd_psh[index];
                   1248:                        ndl_sub(HDL(g),DL(h),DL(mul));
                   1249:                        if ( ndl_check_bound2(index,DL(mul)) ) {
1.26      noro     1250:                                nd_free(d);
1.25      noro     1251:                                free_pbucket(bucket);
                   1252:                                *rp = 0;
                   1253:                                return 0;
                   1254:                        }
1.53      noro     1255:                        p = ps[index];
1.71      noro     1256:                        if ( mod == -1 )
                   1257:                                CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1258:                        else if ( mod ) {
1.25      noro     1259:                                c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1260:                                DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1261:                        } else {
1.113     noro     1262:                                igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
                   1263:                                chsgnq(cg,&CQ(mul));
1.146   ! noro     1264:                                nd_mul_c_q(d,(P)cred);
1.113     noro     1265:                                mulq_pbucket(bucket,cred);
1.26      noro     1266:                                g = bucket->body[hindex];
1.113     noro     1267:                                gmag = (double)p_mag((P)HCQ(g));
1.25      noro     1268:                        }
1.55      noro     1269:                        red = ndv_mul_nm(mod,mul,p);
1.25      noro     1270:                        bucket->body[hindex] = nd_remove_head(g);
                   1271:                        red = nd_remove_head(red);
1.31      noro     1272:                        add_pbucket(mod,bucket,red);
1.34      noro     1273:                        psugar = SG(p)+TD(DL(mul));
                   1274:                        sugar = MAX(sugar,psugar);
1.28      noro     1275:                        if ( !mod && hmag && (gmag > hmag) ) {
                   1276:                                g = normalize_pbucket(mod,bucket);
                   1277:                                if ( !g ) {
1.34      noro     1278:                                        if ( d ) SG(d) = sugar;
1.28      noro     1279:                                        *rp = d;
                   1280:                                        return 1;
                   1281:                                }
                   1282:                                nd_removecont2(d,g);
1.113     noro     1283:                                hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1.31      noro     1284:                                add_pbucket(mod,bucket,g);
1.28      noro     1285:                        }
1.25      noro     1286:                } else if ( !full ) {
                   1287:                        g = normalize_pbucket(mod,bucket);
1.34      noro     1288:                        if ( g ) SG(g) = sugar;
1.25      noro     1289:                        *rp = g;
                   1290:                        return 1;
                   1291:                } else {
                   1292:                        m = BDY(g);
                   1293:                        if ( NEXT(m) ) {
1.34      noro     1294:                                BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1.25      noro     1295:                        } else {
                   1296:                                FREEND(g); g = 0;
                   1297:                        }
                   1298:                        bucket->body[hindex] = g;
                   1299:                        NEXT(m) = 0;
                   1300:                        if ( d ) {
1.34      noro     1301:                                NEXT(tail)=m; tail=m; LEN(d)++;
1.25      noro     1302:                        } else {
1.34      noro     1303:                                MKND(n,m,1,d); tail = BDY(d);
1.25      noro     1304:                        }
                   1305:                }
                   1306:        }
                   1307: }
1.27      noro     1308:
1.61      noro     1309: /* input : list of NDV, cand : list of NDV */
1.28      noro     1310:
1.61      noro     1311: int ndv_check_candidate(NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1.28      noro     1312: {
                   1313:        int n,i,stat;
                   1314:        ND nf,d;
1.61      noro     1315:        NDV r;
1.45      noro     1316:        NODE t,s;
1.113     noro     1317:        union oNDC dn;
1.45      noro     1318:
1.128     noro     1319:        ndv_setup(0,0,cand,0);
1.31      noro     1320:        n = length(cand);
1.28      noro     1321:
                   1322:        /* membercheck : list is a subset of Id(cand) ? */
                   1323:        for ( t = input; t; t = NEXT(t) ) {
1.45      noro     1324: again:
1.61      noro     1325:                if ( nd_bpe > obpe )
                   1326:                        r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
                   1327:                else
                   1328:                        r = (NDV)BDY(t);
                   1329:                d = ndvtond(0,r);
1.146   ! noro     1330:                stat = nd_nf(0,0,d,nd_ps,0,0,&nf);
1.45      noro     1331:                if ( !stat ) {
1.103     noro     1332:                        nd_reconstruct(0,0);
1.45      noro     1333:                        goto again;
                   1334:                } else if ( nf ) return 0;
1.71      noro     1335:                if ( DP_Print ) { printf("."); fflush(stdout); }
1.28      noro     1336:        }
1.71      noro     1337:        if ( DP_Print ) { printf("\n"); }
1.28      noro     1338:        /* gbcheck : cand is a GB of Id(cand) ? */
1.77      noro     1339:        if ( !nd_gb(0,0,1) ) return 0;
1.28      noro     1340:        /* XXX */
1.23      noro     1341:        return 1;
                   1342: }
1.1       noro     1343:
                   1344: ND nd_remove_head(ND p)
                   1345: {
                   1346:        NM m;
                   1347:
                   1348:        m = BDY(p);
                   1349:        if ( !NEXT(m) ) {
1.34      noro     1350:                FREEND(p); p = 0;
1.31      noro     1351:        } else {
1.34      noro     1352:                BDY(p) = NEXT(m); LEN(p)--;
1.31      noro     1353:        }
1.1       noro     1354:        FREENM(m);
                   1355:        return p;
                   1356: }
                   1357:
1.69      noro     1358: ND nd_separate_head(ND p,ND *head)
                   1359: {
                   1360:        NM m,m0;
                   1361:        ND r;
                   1362:
                   1363:        m = BDY(p);
                   1364:        if ( !NEXT(m) ) {
                   1365:                *head = p; p = 0;
                   1366:        } else {
                   1367:                m0 = m;
                   1368:                BDY(p) = NEXT(m); LEN(p)--;
                   1369:                NEXT(m0) = 0;
                   1370:                MKND(NV(p),m0,1,r);
                   1371:                *head = r;
                   1372:        }
                   1373:        return p;
                   1374: }
                   1375:
1.1       noro     1376: PGeoBucket create_pbucket()
                   1377: {
                   1378:     PGeoBucket g;
                   1379:
                   1380:        g = CALLOC(1,sizeof(struct oPGeoBucket));
                   1381:        g->m = -1;
                   1382:        return g;
                   1383: }
                   1384:
1.25      noro     1385: void free_pbucket(PGeoBucket b) {
                   1386:        int i;
                   1387:
1.26      noro     1388:        for ( i = 0; i <= b->m; i++ )
1.25      noro     1389:                if ( b->body[i] ) {
                   1390:                        nd_free(b->body[i]);
                   1391:                        b->body[i] = 0;
                   1392:                }
                   1393:        GC_free(b);
                   1394: }
                   1395:
1.63      noro     1396: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   1397: {
                   1398:        int l,i,k,m;
                   1399:
                   1400:        if ( !d )
                   1401:                return;
                   1402:        l = LEN(d);
                   1403:        for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   1404:        /* 2^(k-1) < l <= 2^k (=m) */
                   1405:        d = nd_merge(g->body[k],d);
                   1406:        for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   1407:                g->body[k] = 0;
                   1408:                d = nd_merge(g->body[k+1],d);
                   1409:        }
                   1410:        g->body[k] = d;
                   1411:        g->m = MAX(g->m,k);
                   1412: }
                   1413:
1.31      noro     1414: void add_pbucket(int mod,PGeoBucket g,ND d)
1.1       noro     1415: {
1.31      noro     1416:        int l,i,k,m;
1.1       noro     1417:
1.31      noro     1418:        if ( !d )
                   1419:                return;
                   1420:        l = LEN(d);
1.29      noro     1421:        for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   1422:        /* 2^(k-1) < l <= 2^k (=m) */
1.31      noro     1423:        d = nd_add(mod,g->body[k],d);
                   1424:        for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
1.1       noro     1425:                g->body[k] = 0;
1.31      noro     1426:                d = nd_add(mod,g->body[k+1],d);
1.1       noro     1427:        }
                   1428:        g->body[k] = d;
                   1429:        g->m = MAX(g->m,k);
                   1430: }
                   1431:
1.113     noro     1432: void mulq_pbucket(PGeoBucket g,Q c)
1.26      noro     1433: {
                   1434:        int k;
                   1435:
                   1436:        for ( k = 0; k <= g->m; k++ )
1.146   ! noro     1437:                nd_mul_c_q(g->body[k],(P)c);
1.26      noro     1438: }
                   1439:
1.63      noro     1440: NM remove_head_pbucket_symbolic(PGeoBucket g)
                   1441: {
                   1442:        int j,i,k,c;
                   1443:        NM head;
                   1444:
                   1445:        k = g->m;
                   1446:        j = -1;
                   1447:        for ( i = 0; i <= k; i++ ) {
                   1448:                if ( !g->body[i] ) continue;
                   1449:                if ( j < 0 ) j = i;
                   1450:                else {
                   1451:                        c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
                   1452:                        if ( c > 0 )
                   1453:                                j = i;
                   1454:                        else if ( c == 0 )
                   1455:                                g->body[i] = nd_remove_head(g->body[i]);
                   1456:                }
                   1457:        }
                   1458:        if ( j < 0 ) return 0;
                   1459:        else {
                   1460:                head = BDY(g->body[j]);
                   1461:                if ( !NEXT(head) ) {
                   1462:                        FREEND(g->body[j]);
                   1463:                        g->body[j] = 0;
                   1464:                } else {
                   1465:                        BDY(g->body[j]) = NEXT(head);
                   1466:                        LEN(g->body[j])--;
                   1467:                }
                   1468:                return head;
                   1469:        }
                   1470: }
                   1471:
1.19      noro     1472: int head_pbucket(int mod,PGeoBucket g)
1.1       noro     1473: {
                   1474:        int j,i,c,k,nv,sum;
1.61      noro     1475:        UINT *di,*dj;
1.1       noro     1476:        ND gi,gj;
                   1477:
                   1478:        k = g->m;
                   1479:        while ( 1 ) {
                   1480:                j = -1;
                   1481:                for ( i = 0; i <= k; i++ ) {
                   1482:                        if ( !(gi = g->body[i]) )
                   1483:                                continue;
                   1484:                        if ( j < 0 ) {
                   1485:                                j = i;
                   1486:                                gj = g->body[j];
                   1487:                                dj = HDL(gj);
1.14      noro     1488:                                sum = HCM(gj);
1.1       noro     1489:                        } else {
1.34      noro     1490:                                c = DL_COMPARE(HDL(gi),dj);
1.1       noro     1491:                                if ( c > 0 ) {
1.34      noro     1492:                                        if ( sum ) HCM(gj) = sum;
                   1493:                                        else g->body[j] = nd_remove_head(gj);
1.1       noro     1494:                                        j = i;
                   1495:                                        gj = g->body[j];
                   1496:                                        dj = HDL(gj);
1.14      noro     1497:                                        sum = HCM(gj);
1.1       noro     1498:                                } else if ( c == 0 ) {
1.71      noro     1499:                                        if ( mod == -1 )
                   1500:                                                sum = _addsf(sum,HCM(gi));
                   1501:                                        else {
                   1502:                                                sum = sum+HCM(gi)-mod;
                   1503:                                                if ( sum < 0 ) sum += mod;
                   1504:                                        }
1.1       noro     1505:                                        g->body[i] = nd_remove_head(gi);
                   1506:                                }
                   1507:                        }
                   1508:                }
1.34      noro     1509:                if ( j < 0 ) return -1;
1.1       noro     1510:                else if ( sum ) {
1.14      noro     1511:                        HCM(gj) = sum;
1.26      noro     1512:                        return j;
1.31      noro     1513:                } else
1.26      noro     1514:                        g->body[j] = nd_remove_head(gj);
                   1515:        }
                   1516: }
                   1517:
1.113     noro     1518: int head_pbucket_q(PGeoBucket g)
1.26      noro     1519: {
                   1520:        int j,i,c,k,nv;
1.113     noro     1521:        Q sum,t;
1.26      noro     1522:        ND gi,gj;
                   1523:
                   1524:        k = g->m;
                   1525:        while ( 1 ) {
                   1526:                j = -1;
                   1527:                for ( i = 0; i <= k; i++ ) {
1.34      noro     1528:                        if ( !(gi = g->body[i]) ) continue;
1.26      noro     1529:                        if ( j < 0 ) {
                   1530:                                j = i;
                   1531:                                gj = g->body[j];
1.113     noro     1532:                                sum = HCQ(gj);
1.26      noro     1533:                        } else {
                   1534:                                nv = NV(gi);
1.34      noro     1535:                                c = DL_COMPARE(HDL(gi),HDL(gj));
1.26      noro     1536:                                if ( c > 0 ) {
1.113     noro     1537:                                        if ( sum ) HCQ(gj) = sum;
1.34      noro     1538:                                        else g->body[j] = nd_remove_head(gj);
1.26      noro     1539:                                        j = i;
                   1540:                                        gj = g->body[j];
1.113     noro     1541:                                        sum = HCQ(gj);
1.26      noro     1542:                                } else if ( c == 0 ) {
1.113     noro     1543:                                        addq(sum,HCQ(gi),&t);
                   1544:                                        sum = t;
1.26      noro     1545:                                        g->body[i] = nd_remove_head(gi);
                   1546:                                }
                   1547:                        }
                   1548:                }
1.34      noro     1549:                if ( j < 0 ) return -1;
1.26      noro     1550:                else if ( sum ) {
1.113     noro     1551:                        HCQ(gj) = sum;
1.1       noro     1552:                        return j;
1.31      noro     1553:                } else
1.1       noro     1554:                        g->body[j] = nd_remove_head(gj);
                   1555:        }
                   1556: }
                   1557:
1.25      noro     1558: ND normalize_pbucket(int mod,PGeoBucket g)
1.1       noro     1559: {
1.31      noro     1560:        int i;
1.1       noro     1561:        ND r,t;
                   1562:
                   1563:        r = 0;
1.28      noro     1564:        for ( i = 0; i <= g->m; i++ ) {
1.31      noro     1565:                r = nd_add(mod,r,g->body[i]);
1.28      noro     1566:                g->body[i] = 0;
                   1567:        }
                   1568:        g->m = -1;
1.1       noro     1569:        return r;
                   1570: }
                   1571:
1.122     noro     1572: int do_diagonalize(int sugar,int m)
1.92      noro     1573: {
                   1574:        int i,nh,stat;
                   1575:        NODE r,g,t;
                   1576:        ND h,nf,s,head;
                   1577:        NDV nfv;
1.113     noro     1578:        Q q,num,den;
1.146   ! noro     1579:        P nm,nmp,dn,mnp,dnp;
1.92      noro     1580:
1.143     noro     1581:        for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
1.92      noro     1582:                if ( nd_demand )
                   1583:                        nfv = ndv_load(i);
                   1584:                else
                   1585:                        nfv = nd_ps[i];
                   1586:                s = ndvtond(m,nfv);
                   1587:                s = nd_separate_head(s,&head);
1.146   ! noro     1588:                stat = nd_nf(m,head,s,nd_ps,1,0,&nf);
1.122     noro     1589:                if ( !stat ) return 0;
1.92      noro     1590:                ndv_free(nfv);
                   1591:                nd_removecont(m,nf);
                   1592:                nfv = ndtondv(m,nf);
                   1593:                nd_free(nf);
                   1594:                nd_bound[i] = ndv_compute_bound(nfv);
                   1595:                if ( nd_demand ) {
                   1596:                        ndv_save(nfv,i);
                   1597:                        ndv_free(nfv);
                   1598:                } else
                   1599:                        nd_ps[i] = nfv;
                   1600:        }
1.122     noro     1601:        return 1;
1.92      noro     1602: }
                   1603:
1.27      noro     1604: /* return value = 0 => input is not a GB */
                   1605:
1.77      noro     1606: NODE nd_gb(int m,int ishomo,int checkonly)
1.1       noro     1607: {
                   1608:        int i,nh,sugar,stat;
1.23      noro     1609:        NODE r,g,t;
1.1       noro     1610:        ND_pairs d;
                   1611:        ND_pairs l;
1.117     noro     1612:        ND h,nf,s,head,nf1;
1.63      noro     1613:        NDV nfv;
1.113     noro     1614:        Q q,num,den;
                   1615:        union oNDC dn;
1.140     noro     1616:        int diag_count = 0;
1.1       noro     1617:
1.23      noro     1618:        g = 0; d = 0;
                   1619:        for ( i = 0; i < nd_psn; i++ ) {
1.1       noro     1620:                d = update_pairs(d,g,i);
                   1621:                g = update_base(g,i);
                   1622:        }
                   1623:        sugar = 0;
                   1624:        while ( d ) {
                   1625: again:
                   1626:                l = nd_minp(d,&d);
1.14      noro     1627:                if ( SG(l) != sugar ) {
1.122     noro     1628:                        if ( ishomo ) {
1.140     noro     1629:                                diag_count = 0;
1.122     noro     1630:                                stat = do_diagonalize(sugar,m);
                   1631:                                if ( !stat ) {
                   1632:                                        NEXT(l) = d; d = l;
                   1633:                                        d = nd_reconstruct(0,d);
                   1634:                                        goto again;
                   1635:                                }
                   1636:                        }
1.14      noro     1637:                        sugar = SG(l);
1.71      noro     1638:                        if ( DP_Print ) fprintf(asir_out,"%d",sugar);
1.1       noro     1639:                }
1.53      noro     1640:                stat = nd_sp(m,0,l,&h);
1.1       noro     1641:                if ( !stat ) {
                   1642:                        NEXT(l) = d; d = l;
1.103     noro     1643:                        d = nd_reconstruct(0,d);
1.1       noro     1644:                        goto again;
                   1645:                }
1.41      noro     1646: #if USE_GEOBUCKET
1.146   ! noro     1647:                stat = m?nd_nf_pbucket(m,h,nd_ps,!Top,&nf):nd_nf(m,0,h,nd_ps,!Top,0,&nf);
1.41      noro     1648: #else
1.146   ! noro     1649:                stat = nd_nf(m,0,h,nd_ps,!Top,0,&nf);
1.41      noro     1650: #endif
1.1       noro     1651:                if ( !stat ) {
                   1652:                        NEXT(l) = d; d = l;
1.103     noro     1653:                        d = nd_reconstruct(0,d);
1.1       noro     1654:                        goto again;
                   1655:                } else if ( nf ) {
1.27      noro     1656:                        if ( checkonly ) return 0;
1.71      noro     1657:                        if ( DP_Print ) { printf("+"); fflush(stdout); }
1.63      noro     1658:                        nd_removecont(m,nf);
1.118     noro     1659:                        if ( !m && nd_nalg ) {
                   1660:                                nd_monic(0,&nf);
                   1661:                                nd_removecont(m,nf);
1.117     noro     1662:                        }
1.63      noro     1663:                        nfv = ndtondv(m,nf); nd_free(nf);
1.77      noro     1664:                        nh = ndv_newps(m,nfv,0);
1.141     noro     1665:                        if ( !m && (ishomo && ++diag_count == diag_period) ) {
1.140     noro     1666:                                diag_count = 0;
                   1667:                                stat = do_diagonalize(sugar,m);
                   1668:                                if ( !stat ) {
                   1669:                                        NEXT(l) = d; d = l;
                   1670:                                        d = nd_reconstruct(1,d);
                   1671:                                        goto again;
                   1672:                                }
                   1673:                        }
1.1       noro     1674:                        d = update_pairs(d,g,nh);
                   1675:                        g = update_base(g,nh);
                   1676:                        FREENDP(l);
                   1677:                } else {
1.71      noro     1678:                        if ( DP_Print ) { printf("."); fflush(stdout); }
1.1       noro     1679:                        FREENDP(l);
                   1680:                }
                   1681:        }
1.77      noro     1682:        if ( nd_demand )
                   1683:                for ( t = g; t; t = NEXT(t) )
                   1684:                        BDY(t) = (pointer)ndv_load((int)BDY(t));
                   1685:        else
                   1686:                for ( t = g; t; t = NEXT(t) )
                   1687:                        BDY(t) = (pointer)nd_ps[(int)BDY(t)];
1.1       noro     1688:        return g;
                   1689: }
                   1690:
1.122     noro     1691: int do_diagonalize_trace(int sugar,int m)
1.91      noro     1692: {
                   1693:        int i,nh,stat;
                   1694:        NODE r,g,t;
                   1695:        ND h,nf,nfq,s,head;
                   1696:        NDV nfv,nfqv;
1.113     noro     1697:        Q q,den,num;
1.91      noro     1698:
1.143     noro     1699:        for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
1.91      noro     1700:                /* for nd_ps */
                   1701:                s = ndvtond(m,nd_ps[i]);
                   1702:                s = nd_separate_head(s,&head);
1.122     noro     1703:                stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
                   1704:                if ( !stat ) return 0;
1.91      noro     1705:                nf = nd_add(m,head,nf);
                   1706:                ndv_free(nd_ps[i]);
                   1707:                nd_ps[i] = ndtondv(m,nf);
                   1708:                nd_free(nf);
                   1709:
                   1710:                /* for nd_ps_trace */
                   1711:                if ( nd_demand )
                   1712:                        nfv = ndv_load(i);
                   1713:                else
                   1714:                        nfv = nd_ps_trace[i];
                   1715:                s = ndvtond(0,nfv);
                   1716:                s = nd_separate_head(s,&head);
1.146   ! noro     1717:                stat = nd_nf(0,head,s,nd_ps_trace,1,0,&nf);
1.122     noro     1718:                if ( !stat ) return 0;
1.91      noro     1719:                ndv_free(nfv);
                   1720:                nd_removecont(0,nf);
                   1721:                nfv = ndtondv(0,nf);
                   1722:                nd_free(nf);
                   1723:                nd_bound[i] = ndv_compute_bound(nfv);
                   1724:                if ( nd_demand ) {
                   1725:                        ndv_save(nfv,i);
                   1726:                        ndv_free(nfv);
                   1727:                } else
                   1728:                        nd_ps_trace[i] = nfv;
                   1729:        }
1.122     noro     1730:        return 1;
1.91      noro     1731: }
                   1732:
1.118     noro     1733: static struct oEGT eg_invdalg;
                   1734: struct oEGT eg_le;
                   1735:
1.82      noro     1736: NODE nd_gb_trace(int m,int ishomo)
1.20      noro     1737: {
                   1738:        int i,nh,sugar,stat;
1.23      noro     1739:        NODE r,g,t;
1.20      noro     1740:        ND_pairs d;
                   1741:        ND_pairs l;
1.82      noro     1742:        ND h,nf,nfq,s,head;
1.63      noro     1743:        NDV nfv,nfqv;
1.113     noro     1744:        Q q,den,num;
                   1745:        union oNDC dn;
1.118     noro     1746:        struct oEGT eg_monic,egm0,egm1;
1.130     noro     1747:        int diag_count = 0;
1.20      noro     1748:
1.118     noro     1749:        init_eg(&eg_monic);
                   1750:        init_eg(&eg_invdalg);
                   1751:        init_eg(&eg_le);
1.23      noro     1752:        g = 0; d = 0;
                   1753:        for ( i = 0; i < nd_psn; i++ ) {
1.20      noro     1754:                d = update_pairs(d,g,i);
                   1755:                g = update_base(g,i);
                   1756:        }
                   1757:        sugar = 0;
                   1758:        while ( d ) {
                   1759: again:
                   1760:                l = nd_minp(d,&d);
                   1761:                if ( SG(l) != sugar ) {
1.130     noro     1762: #if 1
1.122     noro     1763:                        if ( ishomo ) {
                   1764:                                stat = do_diagonalize_trace(sugar,m);
1.130     noro     1765:                                diag_count = 0;
1.122     noro     1766:                                if ( !stat ) {
                   1767:                                        NEXT(l) = d; d = l;
                   1768:                                        d = nd_reconstruct(1,d);
                   1769:                                        goto again;
                   1770:                                }
                   1771:                        }
1.130     noro     1772: #endif
1.20      noro     1773:                        sugar = SG(l);
1.71      noro     1774:                        if ( DP_Print ) fprintf(asir_out,"%d",sugar);
1.20      noro     1775:                }
1.53      noro     1776:                stat = nd_sp(m,0,l,&h);
1.20      noro     1777:                if ( !stat ) {
                   1778:                        NEXT(l) = d; d = l;
1.103     noro     1779:                        d = nd_reconstruct(1,d);
1.20      noro     1780:                        goto again;
                   1781:                }
1.41      noro     1782: #if USE_GEOBUCKET
1.53      noro     1783:                stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.41      noro     1784: #else
1.146   ! noro     1785:                stat = nd_nf(m,0,h,nd_ps,!Top,0,&nf);
1.41      noro     1786: #endif
1.20      noro     1787:                if ( !stat ) {
                   1788:                        NEXT(l) = d; d = l;
1.103     noro     1789:                        d = nd_reconstruct(1,d);
1.20      noro     1790:                        goto again;
                   1791:                } else if ( nf ) {
1.77      noro     1792:                        if ( nd_demand ) {
                   1793:                                nfqv = ndv_load(nd_psn);
                   1794:                                nfq = ndvtond(0,nfqv);
                   1795:                        } else
                   1796:                                nfq = 0;
                   1797:                        if ( !nfq ) {
1.146   ! noro     1798:                                if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!Top,0,&nfq) ) {
1.77      noro     1799:                                        NEXT(l) = d; d = l;
1.103     noro     1800:                                        d = nd_reconstruct(1,d);
1.77      noro     1801:                                        goto again;
                   1802:                                }
1.70      noro     1803:                        }
1.20      noro     1804:                        if ( nfq ) {
1.70      noro     1805:                                /* m|HC(nfq) => failure */
1.113     noro     1806:                                if ( !rem(NM(HCQ(nfq)),m) ) return 0;
1.63      noro     1807:
1.71      noro     1808:                                if ( DP_Print ) { printf("+"); fflush(stdout); }
1.118     noro     1809:                                if ( nd_nalg ) {
                   1810:                                        /* m|DN(HC(nf)^(-1)) => failure */
                   1811:                                        get_eg(&egm0);
                   1812:                                        if ( !nd_monic(m,&nfq) ) return 0;
                   1813:                                        get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
                   1814:                                        nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   1815:                                        nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
                   1816:                                } else {
                   1817:                                        nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   1818:                                        nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
                   1819:                                }
1.77      noro     1820:                                nh = ndv_newps(0,nfv,nfqv);
1.130     noro     1821:                                if ( ishomo && ++diag_count == diag_period ) {
                   1822:                                        diag_count = 0;
                   1823:                                        stat = do_diagonalize_trace(sugar,m);
                   1824:                                        if ( !stat ) {
                   1825:                                                NEXT(l) = d; d = l;
                   1826:                                                d = nd_reconstruct(1,d);
                   1827:                                                goto again;
                   1828:                                        }
                   1829:                                }
1.20      noro     1830:                                d = update_pairs(d,g,nh);
                   1831:                                g = update_base(g,nh);
                   1832:                        } else {
1.71      noro     1833:                                if ( DP_Print ) { printf("*"); fflush(stdout); }
1.20      noro     1834:                        }
                   1835:                } else {
1.71      noro     1836:                        if ( DP_Print ) { printf("."); fflush(stdout); }
1.20      noro     1837:                }
                   1838:                FREENDP(l);
                   1839:        }
1.77      noro     1840:        if ( nd_demand )
                   1841:                for ( t = g; t; t = NEXT(t) )
                   1842:                        BDY(t) = (pointer)ndv_load((int)BDY(t));
                   1843:        else
                   1844:                for ( t = g; t; t = NEXT(t) )
                   1845:                        BDY(t) = (pointer)nd_ps_trace[(int)BDY(t)];
1.118     noro     1846:        if ( nd_nalg ) {
                   1847:                print_eg("monic",&eg_monic);
                   1848:                print_eg("invdalg",&eg_invdalg);
                   1849:                print_eg("le",&eg_le);
                   1850:        }
1.20      noro     1851:        return g;
                   1852: }
                   1853:
1.23      noro     1854: int ndv_compare(NDV *p1,NDV *p2)
                   1855: {
1.34      noro     1856:        return DL_COMPARE(HDL(*p1),HDL(*p2));
1.23      noro     1857: }
                   1858:
                   1859: int ndv_compare_rev(NDV *p1,NDV *p2)
                   1860: {
1.34      noro     1861:        return -DL_COMPARE(HDL(*p1),HDL(*p2));
1.23      noro     1862: }
                   1863:
1.61      noro     1864: NODE ndv_reduceall(int m,NODE f)
1.23      noro     1865: {
1.69      noro     1866:        int i,n,stat;
                   1867:        ND nf,g,head;
1.23      noro     1868:        NODE t,a0,a;
1.113     noro     1869:        union oNDC dn;
1.69      noro     1870:        NDV *w;
1.113     noro     1871:        Q q,num,den;
1.23      noro     1872:
1.69      noro     1873:        n = length(f);
                   1874: #if 0
                   1875:        w = (NDV *)ALLOCA(n*sizeof(NDV));
                   1876:        for ( i = 0, t = f; i < n; i++, t = NEXT(t) ) w[i] = (NDV)BDY(t);
                   1877:        qsort(w,n,sizeof(NDV),
                   1878:                (int (*)(const void *,const void *))ndv_compare);
                   1879:        for ( t = f, i = 0; t; i++, t = NEXT(t) ) BDY(t) = (pointer)w[i];
                   1880: #endif
1.128     noro     1881:        ndv_setup(m,0,f,0);
1.69      noro     1882:        for ( i = 0; i < n; ) {
                   1883:                g = ndvtond(m,nd_ps[i]);
                   1884:                g = nd_separate_head(g,&head);
1.146   ! noro     1885:                stat = nd_nf(m,head,g,nd_ps,1,0,&nf);
1.50      noro     1886:                if ( !stat )
1.103     noro     1887:                        nd_reconstruct(0,0);
1.69      noro     1888:                else {
1.71      noro     1889:                        if ( DP_Print ) { printf("."); fflush(stdout); }
1.69      noro     1890:                        ndv_free(nd_ps[i]);
1.24      noro     1891:                        nd_removecont(m,nf);
1.69      noro     1892:                        nd_ps[i] = ndtondv(m,nf); nd_free(nf);
                   1893:                        nd_bound[i] = ndv_compute_bound(nd_ps[i]);
1.50      noro     1894:                        i++;
1.23      noro     1895:                }
                   1896:        }
1.71      noro     1897:        if ( DP_Print ) { printf("\n"); }
1.23      noro     1898:        for ( a0 = 0, i = 0; i < n; i++ ) {
                   1899:                NEXTNODE(a0,a);
1.69      noro     1900:                BDY(a) = (pointer)nd_ps[i];
1.23      noro     1901:        }
                   1902:        NEXT(a) = 0;
                   1903:        return a0;
                   1904: }
                   1905:
1.1       noro     1906: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t)
                   1907: {
                   1908:        ND_pairs d1,nd,cur,head,prev,remove;
                   1909:
                   1910:        if ( !g ) return d;
                   1911:        d = crit_B(d,t);
                   1912:        d1 = nd_newpairs(g,t);
                   1913:        d1 = crit_M(d1);
                   1914:        d1 = crit_F(d1);
1.55      noro     1915:        if ( do_weyl )
                   1916:                head = d1;
                   1917:        else {
                   1918:                prev = 0; cur = head = d1;
                   1919:                while ( cur ) {
                   1920:                        if ( crit_2( cur->i1,cur->i2 ) ) {
                   1921:                                remove = cur;
                   1922:                                if ( !prev ) head = cur = NEXT(cur);
                   1923:                                else cur = NEXT(prev) = NEXT(cur);
                   1924:                                FREENDP(remove);
                   1925:                        } else {
                   1926:                                prev = cur; cur = NEXT(cur);
                   1927:                        }
1.1       noro     1928:                }
                   1929:        }
                   1930:        if ( !d )
                   1931:                return head;
                   1932:        else {
                   1933:                nd = d;
1.34      noro     1934:                while ( NEXT(nd) ) nd = NEXT(nd);
1.1       noro     1935:                NEXT(nd) = head;
                   1936:                return d;
                   1937:        }
                   1938: }
                   1939:
                   1940: ND_pairs nd_newpairs( NODE g, int t )
                   1941: {
                   1942:        NODE h;
1.61      noro     1943:        UINT *dl;
1.34      noro     1944:        int ts,s;
1.1       noro     1945:        ND_pairs r,r0;
                   1946:
1.20      noro     1947:        dl = DL(nd_psh[t]);
1.34      noro     1948:        ts = SG(nd_psh[t]) - TD(dl);
1.1       noro     1949:        for ( r0 = 0, h = g; h; h = NEXT(h) ) {
                   1950:                NEXTND_pairs(r0,r);
                   1951:                r->i1 = (int)BDY(h);
                   1952:                r->i2 = t;
1.20      noro     1953:                ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
1.34      noro     1954:                s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   1955:                SG(r) = MAX(s,ts) + TD(LCM(r));
1.1       noro     1956:        }
                   1957:        NEXT(r) = 0;
                   1958:        return r0;
                   1959: }
                   1960:
                   1961: ND_pairs crit_B( ND_pairs d, int s )
                   1962: {
                   1963:        ND_pairs cur,head,prev,remove;
1.61      noro     1964:        UINT *t,*tl,*lcm;
1.1       noro     1965:        int td,tdl;
                   1966:
                   1967:        if ( !d ) return 0;
1.20      noro     1968:        t = DL(nd_psh[s]);
1.1       noro     1969:        prev = 0;
                   1970:        head = cur = d;
1.61      noro     1971:        lcm = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
1.1       noro     1972:        while ( cur ) {
                   1973:                tl = cur->lcm;
                   1974:                if ( ndl_reducible(tl,t)
1.20      noro     1975:                        && (ndl_lcm(DL(nd_psh[cur->i1]),t,lcm),!ndl_equal(lcm,tl))
                   1976:                        && (ndl_lcm(DL(nd_psh[cur->i2]),t,lcm),!ndl_equal(lcm,tl)) ) {
1.1       noro     1977:                        remove = cur;
                   1978:                        if ( !prev ) {
                   1979:                                head = cur = NEXT(cur);
                   1980:                        } else {
                   1981:                                cur = NEXT(prev) = NEXT(cur);
                   1982:                        }
                   1983:                        FREENDP(remove);
                   1984:                } else {
1.34      noro     1985:                        prev = cur; cur = NEXT(cur);
1.1       noro     1986:                }
                   1987:        }
                   1988:        return head;
                   1989: }
                   1990:
                   1991: ND_pairs crit_M( ND_pairs d1 )
                   1992: {
                   1993:        ND_pairs e,d2,d3,dd,p;
1.61      noro     1994:        UINT *id,*jd;
1.1       noro     1995:
                   1996:        for ( dd = 0, e = d1; e; e = d3 ) {
                   1997:                if ( !(d2 = NEXT(e)) ) {
                   1998:                        NEXT(e) = dd;
                   1999:                        return e;
                   2000:                }
1.34      noro     2001:                id = LCM(e);
1.1       noro     2002:                for ( d3 = 0; d2; d2 = p ) {
1.34      noro     2003:                        p = NEXT(d2);
                   2004:                        jd = LCM(d2);
                   2005:                        if ( ndl_equal(jd,id) )
                   2006:                                ;
                   2007:                        else if ( TD(jd) > TD(id) )
1.1       noro     2008:                                if ( ndl_reducible(jd,id) ) continue;
                   2009:                                else ;
1.34      noro     2010:                        else if ( ndl_reducible(id,jd) ) goto delit;
1.1       noro     2011:                        NEXT(d2) = d3;
                   2012:                        d3 = d2;
                   2013:                }
                   2014:                NEXT(e) = dd;
                   2015:                dd = e;
                   2016:                continue;
                   2017:                /**/
                   2018:        delit:  NEXT(d2) = d3;
                   2019:                d3 = d2;
                   2020:                for ( ; p; p = d2 ) {
                   2021:                        d2 = NEXT(p);
                   2022:                        NEXT(p) = d3;
                   2023:                        d3 = p;
                   2024:                }
                   2025:                FREENDP(e);
                   2026:        }
                   2027:        return dd;
                   2028: }
                   2029:
                   2030: ND_pairs crit_F( ND_pairs d1 )
                   2031: {
                   2032:        ND_pairs rest, head,remove;
                   2033:        ND_pairs last, p, r, w;
                   2034:        int s;
                   2035:
                   2036:        for ( head = last = 0, p = d1; NEXT(p); ) {
                   2037:                r = w = equivalent_pairs(p,&rest);
1.14      noro     2038:                s = SG(r);
1.1       noro     2039:                w = NEXT(w);
                   2040:                while ( w ) {
                   2041:                        if ( crit_2(w->i1,w->i2) ) {
                   2042:                                r = w;
                   2043:                                w = NEXT(w);
                   2044:                                while ( w ) {
                   2045:                                        remove = w;
                   2046:                                        w = NEXT(w);
                   2047:                                        FREENDP(remove);
                   2048:                                }
                   2049:                                break;
1.14      noro     2050:                        } else if ( SG(w) < s ) {
1.1       noro     2051:                                FREENDP(r);
                   2052:                                r = w;
1.14      noro     2053:                                s = SG(r);
1.1       noro     2054:                                w = NEXT(w);
                   2055:                        } else {
                   2056:                                remove = w;
                   2057:                                w = NEXT(w);
                   2058:                                FREENDP(remove);
                   2059:                        }
                   2060:                }
                   2061:                if ( last ) NEXT(last) = r;
                   2062:                else head = r;
                   2063:                NEXT(last = r) = 0;
                   2064:                p = rest;
                   2065:                if ( !p ) return head;
                   2066:        }
                   2067:        if ( !last ) return p;
                   2068:        NEXT(last) = p;
                   2069:        return head;
                   2070: }
                   2071:
                   2072: int crit_2( int dp1, int dp2 )
                   2073: {
1.20      noro     2074:        return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
1.1       noro     2075: }
                   2076:
1.40      noro     2077: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
1.1       noro     2078: {
                   2079:        ND_pairs w,p,r,s;
1.61      noro     2080:        UINT *d;
1.1       noro     2081:
                   2082:        w = d1;
1.34      noro     2083:        d = LCM(w);
1.1       noro     2084:        s = NEXT(w);
                   2085:        NEXT(w) = 0;
                   2086:        for ( r = 0; s; s = p ) {
                   2087:                p = NEXT(s);
1.34      noro     2088:                if ( ndl_equal(d,LCM(s)) ) {
1.39      noro     2089:                        NEXT(s) = w; w = s;
1.1       noro     2090:                } else {
1.39      noro     2091:                        NEXT(s) = r; r = s;
1.1       noro     2092:                }
                   2093:        }
                   2094:        *prest = r;
                   2095:        return w;
                   2096: }
                   2097:
                   2098: NODE update_base(NODE nd,int ndp)
                   2099: {
1.61      noro     2100:        UINT *dl, *dln;
1.137     noro     2101:        NODE last, p, head;
1.1       noro     2102:
1.20      noro     2103:        dl = DL(nd_psh[ndp]);
1.1       noro     2104:        for ( head = last = 0, p = nd; p; ) {
1.20      noro     2105:                dln = DL(nd_psh[(int)BDY(p)]);
1.34      noro     2106:                if ( ndl_reducible( dln, dl ) ) {
1.1       noro     2107:                        p = NEXT(p);
                   2108:                        if ( last ) NEXT(last) = p;
                   2109:                } else {
                   2110:                        if ( !last ) head = p;
                   2111:                        p = NEXT(last = p);
                   2112:                }
                   2113:        }
                   2114:        head = append_one(head,ndp);
                   2115:        return head;
                   2116: }
                   2117:
                   2118: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   2119: {
                   2120:        ND_pairs m,ml,p,l;
1.61      noro     2121:        UINT *lcm;
1.33      noro     2122:        int s,td,len,tlen,c,c1;
1.1       noro     2123:
                   2124:        if ( !(p = NEXT(m = d)) ) {
                   2125:                *prest = p;
                   2126:                NEXT(m) = 0;
                   2127:                return m;
                   2128:        }
1.14      noro     2129:        s = SG(m);
1.77      noro     2130:        if ( !NoSugar ) {
                   2131:                for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2132:                        if ( (SG(p) < s)
                   2133:                                || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   2134:                                ml = l; m = p; s = SG(m);
                   2135:                        }
                   2136:        } else {
                   2137:                for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2138:                        if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
                   2139:                                ml = l; m = p; s = SG(m);
                   2140:                        }
                   2141:        }
1.1       noro     2142:        if ( !ml ) *prest = NEXT(m);
                   2143:        else {
                   2144:                NEXT(ml) = NEXT(m);
                   2145:                *prest = d;
                   2146:        }
                   2147:        NEXT(m) = 0;
                   2148:        return m;
                   2149: }
                   2150:
1.63      noro     2151: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   2152: {
1.69      noro     2153:        int msugar,i;
1.63      noro     2154:        ND_pairs t,dm0,dm,dr0,dr;
                   2155:
                   2156:        for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   2157:                if ( SG(t) < msugar ) msugar = SG(t);
                   2158:        dm0 = 0; dr0 = 0;
1.69      noro     2159:        for ( i = 0, t = d; t; t = NEXT(t) )
                   2160:                if ( i < nd_f4_nsp && SG(t) == msugar ) {
1.63      noro     2161:                        if ( dm0 ) NEXT(dm) = t;
                   2162:                        else dm0 = t;
                   2163:                        dm = t;
1.69      noro     2164:                        i++;
1.63      noro     2165:                } else {
                   2166:                        if ( dr0 ) NEXT(dr) = t;
                   2167:                        else dr0 = t;
                   2168:                        dr = t;
                   2169:                }
                   2170:        NEXT(dm) = 0;
                   2171:        if ( dr0 ) NEXT(dr) = 0;
                   2172:        *prest = dr0;
                   2173:        return dm0;
                   2174: }
                   2175:
1.77      noro     2176: int ndv_newps(int m,NDV a,NDV aq)
1.1       noro     2177: {
1.3       noro     2178:        int len;
1.13      noro     2179:        RHist r;
1.20      noro     2180:        NDV b;
1.3       noro     2181:
1.1       noro     2182:        if ( nd_psn == nd_pslen ) {
                   2183:                nd_pslen *= 2;
1.11      noro     2184:                nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
1.53      noro     2185:                nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
1.13      noro     2186:                nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
1.61      noro     2187:                nd_bound = (UINT **)
                   2188:                        REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
1.1       noro     2189:        }
1.39      noro     2190:        NEWRHist(r); nd_psh[nd_psn] = r;
1.63      noro     2191:        nd_ps[nd_psn] = a;
1.39      noro     2192:        if ( aq ) {
1.63      noro     2193:                nd_ps_trace[nd_psn] = aq;
                   2194:                nd_bound[nd_psn] = ndv_compute_bound(aq);
1.39      noro     2195:                SG(r) = SG(aq); ndl_copy(HDL(aq),DL(r));
1.53      noro     2196:        } else {
1.63      noro     2197:                nd_bound[nd_psn] = ndv_compute_bound(a);
1.53      noro     2198:                SG(r) = SG(a); ndl_copy(HDL(a),DL(r));
1.39      noro     2199:        }
1.77      noro     2200:        if ( nd_demand ) {
                   2201:                if ( aq ) {
                   2202:                        ndv_save(nd_ps_trace[nd_psn],nd_psn);
                   2203:                        nd_ps_trace[nd_psn] = 0;
                   2204:                } else {
                   2205:                        ndv_save(nd_ps[nd_psn],nd_psn);
                   2206:                        nd_ps[nd_psn] = 0;
                   2207:                }
                   2208:        }
1.1       noro     2209:        return nd_psn++;
                   2210: }
                   2211:
1.128     noro     2212: void ndv_setup(int mod,int trace,NODE f,int dont_sort)
1.1       noro     2213: {
1.5       noro     2214:        int i,j,td,len,max;
1.1       noro     2215:        NODE s,s0,f0;
1.61      noro     2216:        UINT *d;
1.13      noro     2217:        RHist r;
1.69      noro     2218:        NDV *w;
1.61      noro     2219:        NDV a,am;
1.11      noro     2220:
                   2221:        nd_found = 0; nd_notfirst = 0; nd_create = 0;
1.1       noro     2222:
1.69      noro     2223:        for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
                   2224:        w = (NDV *)ALLOCA(nd_psn*sizeof(NDV));
                   2225:        for ( i = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) w[i++] = BDY(s);
1.139     noro     2226:        if ( !dont_sort ) {
                   2227:                /* XXX heuristic */
                   2228:                if ( !nd_ord->id && (nd_ord->ord.simple<2) )
                   2229:                        qsort(w,nd_psn,sizeof(NDV),
                   2230:                                (int (*)(const void *,const void *))ndv_compare_rev);
                   2231:                else
                   2232:                        qsort(w,nd_psn,sizeof(NDV),
                   2233:                                (int (*)(const void *,const void *))ndv_compare);
                   2234:        }
1.69      noro     2235:        nd_pslen = 2*nd_psn;
1.11      noro     2236:        nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.53      noro     2237:        nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.13      noro     2238:        nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
1.61      noro     2239:        nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
1.57      noro     2240:
1.146   ! noro     2241:        if ( trace && nd_vc )
        !          2242:                makesubst(nd_vc,&nd_subst);
        !          2243:        else
        !          2244:                nd_subst = 0;
        !          2245:
1.11      noro     2246:        if ( !nd_red )
1.13      noro     2247:                nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
1.71      noro     2248:        for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
1.69      noro     2249:        for ( i = 0; i < nd_psn; i++ ) {
1.39      noro     2250:                if ( trace ) {
1.69      noro     2251:                        a = nd_ps_trace[i] = ndv_dup(0,w[i]);
1.61      noro     2252:                        ndv_removecont(0,a);
                   2253:                        am = nd_ps[i] = ndv_dup(mod,a);
                   2254:                        ndv_mod(mod,am);
                   2255:                        ndv_removecont(mod,am);
                   2256:                } else {
1.69      noro     2257:                        a = nd_ps[i] = ndv_dup(mod,w[i]);
1.61      noro     2258:                        ndv_removecont(mod,a);
1.39      noro     2259:                }
1.61      noro     2260:                NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
1.57      noro     2261:                nd_bound[i] = ndv_compute_bound(a);
1.20      noro     2262:                nd_psh[i] = r;
1.77      noro     2263:                if ( nd_demand ) {
                   2264:                        if ( trace ) {
                   2265:                                ndv_save(nd_ps_trace[i],i);
                   2266:                                nd_ps_trace[i] = 0;
                   2267:                        } else {
                   2268:                                ndv_save(nd_ps[i],i);
                   2269:                                nd_ps[i] = 0;
                   2270:                        }
                   2271:                }
1.20      noro     2272:        }
                   2273: }
                   2274:
1.119     noro     2275: struct order_spec *append_block(struct order_spec *spec,
                   2276:     int nv,int nalg,int ord);
                   2277:
1.121     noro     2278: extern VECT current_dl_weight_vector_obj;
                   2279: static VECT prev_weight_vector_obj;
                   2280:
1.120     noro     2281: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
                   2282:        struct order_spec **ord1p,LIST *f1p,NODE *alistp)
                   2283: {
1.121     noro     2284:        NODE alist,t,s,r0,r,arg;
1.120     noro     2285:        VL tv;
                   2286:        P poly;
                   2287:        DP d;
                   2288:        Alg alpha,dp;
                   2289:        DAlg inv,da,hc;
                   2290:        MP m;
1.121     noro     2291:        int i,nvar,nalg,n;
1.120     noro     2292:        NumberField nf;
                   2293:        LIST f1,f2;
                   2294:        struct order_spec *current_spec;
1.121     noro     2295:        VECT obj,obj0;
                   2296:        Obj tmp;
1.120     noro     2297:
                   2298:        for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
                   2299:        for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
                   2300:
                   2301:        for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
                   2302:                NEXTNODE(alist,t); MKV(tv->v,poly);
                   2303:                MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
                   2304:                tv->v = tv->v->priv;
                   2305:        }
                   2306:        NEXT(t) = 0;
                   2307:
                   2308:        /* simplification, makeing polynomials monic */
                   2309:        setfield_dalg(alist);
                   2310:        obj_algtodalg(f,&f1);
                   2311:        for ( t = BDY(f); t; t = NEXT(t) ) {
                   2312:                initd(ord); ptod(vv,vv,(P)BDY(t),&d);
                   2313:                hc = (DAlg)BDY(d)->c;
                   2314:                if ( NID(hc) == N_DA ) {
                   2315:                        invdalg(hc,&inv);
                   2316:                        for ( m = BDY(d); m; m = NEXT(m) ) {
                   2317:                                muldalg(inv,(DAlg)m->c,&da); m->c = (P)da;
                   2318:                        }
                   2319:                }
                   2320:                initd(ord); dtop(vv,vv,d,&poly); BDY(f) = (pointer)poly;
                   2321:        }
                   2322:        obj_dalgtoalg(f1,&f);
                   2323:
                   2324:        /* append alg vars to the var list */
                   2325:        for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
                   2326:        NEXT(tv) = av;
                   2327:
                   2328:        /* append a block to ord */
                   2329:        *ord1p = append_block(ord,nvar,nalg,2);
                   2330:
                   2331:        /* create generator list */
                   2332:        nf = get_numberfield();
                   2333:        for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
                   2334:                MKAlg(nf->defpoly[i],dp);
                   2335:                MKNODE(s,dp,t); t = s;
                   2336:        }
                   2337:        MKLIST(f1,t);
                   2338:        *alistp = alist;
                   2339:        algobjtorat(f1,f1p);
1.121     noro     2340:
                   2341:        /* creating a new weight vector */
                   2342:        prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
                   2343:        n = nvar+nalg+1;
                   2344:        MKVECT(obj,n);
                   2345:        if ( obj0 && obj0->len == nvar )
                   2346:                for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
                   2347:        else
                   2348:                for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
                   2349:        for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
                   2350:        BDY(obj)[n-1] = (pointer)ONE;
                   2351:        arg = mknode(1,obj);
                   2352:        Pdp_set_weight(arg,&tmp);
                   2353: }
                   2354:
                   2355: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
                   2356: {
                   2357:        NODE s,t,u0,u;
                   2358:        P p;
                   2359:        VL tv;
                   2360:        Obj obj,tmp;
                   2361:        NODE arg;
                   2362:
                   2363:        u0 = 0;
                   2364:        for ( t = r; t; t = NEXT(t) ) {
                   2365:                p = (P)BDY(t);
                   2366:                for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
                   2367:                        substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
                   2368:                }
                   2369:                if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
                   2370:                        NEXTNODE(u0,u);
                   2371:                        BDY(u) = (pointer)p;
                   2372:                }
                   2373:        }
                   2374:        arg = mknode(1,prev_weight_vector_obj);
                   2375:        Pdp_set_weight(arg,&tmp);
                   2376:
                   2377:        return u0;
1.120     noro     2378: }
                   2379:
1.63      noro     2380: void nd_gr(LIST f,LIST v,int m,int f4,struct order_spec *ord,LIST *rp)
1.1       noro     2381: {
1.119     noro     2382:        VL tv,fv,vv,vc,av;
                   2383:        NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   2384:        int e,max,nvar,i;
1.69      noro     2385:        NDV b;
1.119     noro     2386:        int ishomo,nalg;
                   2387:        Alg alpha,dp;
1.144     noro     2388:        P p,zp;
                   2389:        Q dmy;
1.119     noro     2390:        LIST f1,f2;
                   2391:        Obj obj;
                   2392:        NumberField nf;
1.120     noro     2393:        struct order_spec *ord1;
1.1       noro     2394:
1.78      noro     2395:        if ( !m && Demand ) nd_demand = 1;
                   2396:        else nd_demand = 0;
                   2397:
1.103     noro     2398: #if 0
1.74      noro     2399:        ndv_alloc = 0;
1.103     noro     2400: #endif
1.146   ! noro     2401:        get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.74      noro     2402:        for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
1.88      noro     2403:        switch ( ord->id ) {
                   2404:                case 1:
                   2405:                        if ( ord->nv != nvar )
                   2406:                                error("nd_{gr,f4} : invalid order specification");
                   2407:                        break;
                   2408:                default:
                   2409:                        break;
                   2410:        }
1.119     noro     2411:        nd_nalg = 0;
1.123     noro     2412:        av = 0;
1.119     noro     2413:        if ( !m ) {
                   2414:                get_algtree((Obj)f,&av);
                   2415:                for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
1.121     noro     2416:                nd_ntrans = nvar;
1.119     noro     2417:                nd_nalg = nalg;
                   2418:                /* #i -> t#i */
                   2419:                if ( nalg ) {
1.120     noro     2420:                        preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   2421:                        ord = ord1;
                   2422:                        f = f1;
1.119     noro     2423:                }
1.120     noro     2424:                nvar += nalg;
1.119     noro     2425:        }
1.32      noro     2426:        nd_init_ord(ord);
1.61      noro     2427:        for ( t = BDY(f), max = 0; t; t = NEXT(t) )
                   2428:                for ( tv = vv; tv; tv = NEXT(tv) ) {
                   2429:                        e = getdeg(tv->v,(P)BDY(t));
                   2430:                        max = MAX(e,max);
                   2431:                }
                   2432:        nd_setup_parameters(nvar,max);
1.77      noro     2433:        ishomo = 1;
1.1       noro     2434:        for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.144     noro     2435:                ptozp((P)BDY(t),1,&dmy,&zp);
                   2436:                b = (pointer)ptondv(CO,vv,zp);
1.77      noro     2437:                if ( ishomo )
                   2438:                        ishomo = ishomo && ndv_ishomo(b);
1.69      noro     2439:                if ( m ) ndv_mod(m,b);
1.61      noro     2440:                if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
1.1       noro     2441:        }
                   2442:        if ( fd0 ) NEXT(fd) = 0;
1.128     noro     2443:        ndv_setup(m,0,fd0,0);
1.77      noro     2444:        x = f4?nd_f4(m):nd_gb(m,ishomo,0);
1.82      noro     2445:        nd_demand = 0;
1.61      noro     2446:        x = ndv_reducebase(x);
                   2447:        x = ndv_reduceall(m,x);
1.23      noro     2448:        for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.1       noro     2449:                NEXTNODE(r0,r);
1.61      noro     2450:                BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.20      noro     2451:        }
                   2452:        if ( r0 ) NEXT(r) = 0;
1.121     noro     2453:        if ( nalg )
                   2454:                r0 = postprocess_algcoef(av,alist,r0);
1.20      noro     2455:        MKLIST(*rp,r0);
1.103     noro     2456: #if 0
1.74      noro     2457:        fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     2458: #endif
1.127     noro     2459: }
                   2460:
                   2461: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
                   2462: {
                   2463:        VL tv,fv,vv,vc,av;
                   2464:        NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   2465:        int e,max,nvar,i;
                   2466:        NDV b;
                   2467:        int ishomo,nalg;
                   2468:        Alg alpha,dp;
1.144     noro     2469:        P p,zp;
                   2470:        Q dmy;
1.127     noro     2471:        LIST f1,f2;
                   2472:        Obj obj;
                   2473:        NumberField nf;
                   2474:        struct order_spec *ord1;
                   2475:
1.146   ! noro     2476:        get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.127     noro     2477:        for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   2478:        switch ( ord->id ) {
                   2479:                case 1:
                   2480:                        if ( ord->nv != nvar )
                   2481:                                error("nd_check : invalid order specification");
                   2482:                        break;
                   2483:                default:
                   2484:                        break;
                   2485:        }
                   2486:        nd_nalg = 0;
                   2487:        av = 0;
                   2488:        if ( !m ) {
                   2489:                get_algtree((Obj)f,&av);
                   2490:                for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   2491:                nd_ntrans = nvar;
                   2492:                nd_nalg = nalg;
                   2493:                /* #i -> t#i */
                   2494:                if ( nalg ) {
                   2495:                        preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   2496:                        ord = ord1;
                   2497:                        f = f1;
                   2498:                }
                   2499:                nvar += nalg;
                   2500:        }
                   2501:        nd_init_ord(ord);
                   2502:        for ( t = BDY(f), max = 0; t; t = NEXT(t) )
                   2503:                for ( tv = vv; tv; tv = NEXT(tv) ) {
                   2504:                        e = getdeg(tv->v,(P)BDY(t));
                   2505:                        max = MAX(e,max);
                   2506:                }
                   2507:        nd_setup_parameters(nvar,max);
                   2508:        ishomo = 1;
                   2509:        for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.144     noro     2510:                ptozp((P)BDY(t),1,&dmy,&zp);
                   2511:                b = (pointer)ptondv(CO,vv,zp);
1.127     noro     2512:                if ( ishomo )
                   2513:                        ishomo = ishomo && ndv_ishomo(b);
                   2514:                if ( m ) ndv_mod(m,b);
                   2515:                if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   2516:        }
                   2517:        if ( fd0 ) NEXT(fd) = 0;
1.128     noro     2518:        ndv_setup(m,0,fd0,0);
1.127     noro     2519:        for ( x = 0, i = 0; i < nd_psn; i++ )
                   2520:                x = update_base(x,i);
                   2521:        if ( do_check ) {
                   2522:                x = nd_gb(m,ishomo,1);
                   2523:                if ( !x ) {
                   2524:                        *rp = 0;
                   2525:                        return;
                   2526:                }
                   2527:        } else {
                   2528:                for ( t = x; t; t = NEXT(t) )
                   2529:                        BDY(t) = (pointer)nd_ps[(int)BDY(t)];
                   2530:        }
                   2531:        x = ndv_reducebase(x);
                   2532:        x = ndv_reduceall(m,x);
                   2533:        for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   2534:                NEXTNODE(r0,r);
                   2535:                BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   2536:        }
                   2537:        if ( r0 ) NEXT(r) = 0;
                   2538:        if ( nalg )
                   2539:                r0 = postprocess_algcoef(av,alist,r0);
                   2540:        MKLIST(*rp,r0);
1.20      noro     2541: }
                   2542:
1.133     noro     2543: void nd_gr_trace(LIST f,LIST v,int trace,int homo,int f4,struct order_spec *ord,LIST *rp)
1.20      noro     2544: {
1.119     noro     2545:        VL tv,fv,vv,vc,av;
                   2546:        NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
1.61      noro     2547:        int m,nocheck,nvar,mindex,e,max;
                   2548:        NDV c;
                   2549:        NMV a;
1.144     noro     2550:        P p,zp;
                   2551:        Q dmy;
1.61      noro     2552:        EPOS oepos;
1.119     noro     2553:        int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg;
                   2554:        Alg alpha,dp;
                   2555:        P poly;
                   2556:        LIST f1,f2;
                   2557:        Obj obj;
                   2558:        NumberField nf;
1.120     noro     2559:        struct order_spec *ord1;
1.145     noro     2560:        struct oEGT eg_check,eg0,eg1;
1.20      noro     2561:
1.146   ! noro     2562:        get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.74      noro     2563:        for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
1.88      noro     2564:        switch ( ord->id ) {
                   2565:                case 1:
                   2566:                        if ( ord->nv != nvar )
                   2567:                                error("nd_gr_trace : invalid order specification");
                   2568:                        break;
                   2569:                default:
                   2570:                        break;
                   2571:        }
1.119     noro     2572:
                   2573:        get_algtree((Obj)f,&av);
                   2574:        for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
1.121     noro     2575:        nd_ntrans = nvar;
1.119     noro     2576:        nd_nalg = nalg;
                   2577:        /* #i -> t#i */
                   2578:        if ( nalg ) {
1.120     noro     2579:                preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   2580:                ord = ord1;
                   2581:                f = f1;
1.119     noro     2582:        }
1.120     noro     2583:        nvar += nalg;
1.119     noro     2584:
1.52      noro     2585:        nocheck = 0;
                   2586:        mindex = 0;
                   2587:
1.78      noro     2588:        if ( Demand ) nd_demand = 1;
                   2589:        else nd_demand = 0;
                   2590:
1.52      noro     2591:        /* setup modulus */
                   2592:        if ( trace < 0 ) {
                   2593:                trace = -trace;
                   2594:                nocheck = 1;
                   2595:        }
                   2596:        m = trace > 1 ? trace : get_lprime(mindex);
1.61      noro     2597:        for ( t = BDY(f), max = 0; t; t = NEXT(t) )
                   2598:                for ( tv = vv; tv; tv = NEXT(tv) ) {
                   2599:                        e = getdeg(tv->v,(P)BDY(t));
                   2600:                        max = MAX(e,max);
1.23      noro     2601:                }
1.61      noro     2602:        nd_init_ord(ord);
                   2603:        nd_setup_parameters(nvar,max);
                   2604:        obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos;
1.82      noro     2605:        ishomo = 1;
1.61      noro     2606:        for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.144     noro     2607:                ptozp((P)BDY(t),1,&dmy,&zp);
                   2608:                c = (pointer)ptondv(CO,vv,zp);
1.82      noro     2609:                if ( ishomo )
                   2610:                        ishomo = ishomo && ndv_ishomo(c);
1.61      noro     2611:                if ( c ) {
                   2612:                        NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   2613:                        NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
1.23      noro     2614:                }
1.61      noro     2615:        }
                   2616:        if ( in0 ) NEXT(in) = 0;
                   2617:        if ( fd0 ) NEXT(fd) = 0;
1.82      noro     2618:        if ( !ishomo && homo ) {
1.122     noro     2619:                for ( t = in0, wmax = max; t; t = NEXT(t) ) {
1.61      noro     2620:                        c = (NDV)BDY(t); len = LEN(c);
                   2621:                        for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   2622:                                wmax = MAX(TD(DL(a)),wmax);
                   2623:                }
                   2624:                homogenize_order(ord,nvar,&ord1);
1.89      noro     2625:                nd_init_ord(ord1);
1.61      noro     2626:                nd_setup_parameters(nvar+1,wmax);
                   2627:                for ( t = fd0; t; t = NEXT(t) )
                   2628:                        ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos);
1.27      noro     2629:        }
1.52      noro     2630:        while ( 1 ) {
1.77      noro     2631:                if ( Demand )
                   2632:                        nd_demand = 1;
1.128     noro     2633:                ndv_setup(m,1,fd0,0);
1.133     noro     2634:                cand = f4?nd_f4_trace(m):nd_gb_trace(m,ishomo || homo);
1.52      noro     2635:                if ( !cand ) {
                   2636:                        /* failure */
1.61      noro     2637:                        if ( trace > 1 ) { *rp = 0; return; }
                   2638:                        else m = get_lprime(++mindex);
1.52      noro     2639:                        continue;
                   2640:                }
1.82      noro     2641:                if ( !ishomo && homo ) {
1.27      noro     2642:                        /* dehomogenization */
1.61      noro     2643:                        for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
1.45      noro     2644:                        nd_init_ord(ord);
1.61      noro     2645:                        nd_setup_parameters(nvar,0);
1.27      noro     2646:                }
1.77      noro     2647:                nd_demand = 0;
1.61      noro     2648:                cand = ndv_reducebase(cand);
                   2649:                cand = ndv_reduceall(0,cand);
1.78      noro     2650:                cbpe = nd_bpe;
1.61      noro     2651:                if ( nocheck )
                   2652:                        break;
1.145     noro     2653:                get_eg(&eg0);
1.61      noro     2654:                if ( ndv_check_candidate(in0,obpe,oadv,oepos,cand) )
1.52      noro     2655:                        /* success */
                   2656:                        break;
                   2657:                else if ( trace > 1 ) {
                   2658:                        /* failure */
                   2659:                        *rp = 0; return;
1.61      noro     2660:                } else {
1.52      noro     2661:                        /* try the next modulus */
                   2662:                        m = get_lprime(++mindex);
1.61      noro     2663:                        /* reset the parameters */
1.82      noro     2664:                        if ( !ishomo && homo ) {
1.89      noro     2665:                                nd_init_ord(ord1);
1.61      noro     2666:                                nd_setup_parameters(nvar+1,wmax);
                   2667:                        } else {
                   2668:                                nd_init_ord(ord);
                   2669:                                nd_setup_parameters(nvar,max);
                   2670:                        }
                   2671:                }
1.52      noro     2672:        }
1.145     noro     2673:        get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   2674:        if ( DP_Print )
                   2675:                fprintf(asir_out,"check=%fsec\n",eg_check.exectime+eg_check.gctime);
1.27      noro     2676:        /* dp->p */
1.61      noro     2677:        nd_bpe = cbpe;
1.73      noro     2678:        nd_setup_parameters(nd_nvar,0);
1.121     noro     2679:        for ( r = cand; r; r = NEXT(r) )
1.119     noro     2680:                BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.121     noro     2681:        if ( nalg )
                   2682:                cand = postprocess_algcoef(av,alist,cand);
1.27      noro     2683:        MKLIST(*rp,cand);
1.1       noro     2684: }
                   2685:
1.61      noro     2686: void dltondl(int n,DL dl,UINT *r)
1.1       noro     2687: {
1.61      noro     2688:        UINT *d;
1.57      noro     2689:        int i,j,l,s,ord_l;
1.43      noro     2690:        struct order_pair *op;
1.1       noro     2691:
                   2692:        d = dl->d;
1.41      noro     2693:        for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
1.43      noro     2694:        if ( nd_blockmask ) {
                   2695:                l = nd_blockmask->n;
                   2696:                op = nd_blockmask->order_pair;
                   2697:                for ( j = 0, s = 0; j < l; j++ ) {
                   2698:                        ord_l = op[j].length;
1.57      noro     2699:                        for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
1.43      noro     2700:                }
                   2701:                TD(r) = ndl_weight(r);
1.61      noro     2702:                ndl_weight_mask(r);
1.43      noro     2703:        } else {
1.56      noro     2704:                for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
1.43      noro     2705:                TD(r) = ndl_weight(r);
                   2706:        }
1.1       noro     2707: }
                   2708:
1.61      noro     2709: DL ndltodl(int n,UINT *ndl)
1.1       noro     2710: {
                   2711:        DL dl;
                   2712:        int *d;
1.57      noro     2713:        int i,j,l,s,ord_l;
1.43      noro     2714:        struct order_pair *op;
1.1       noro     2715:
                   2716:        NEWDL(dl,n);
1.34      noro     2717:        dl->td = TD(ndl);
1.1       noro     2718:        d = dl->d;
1.43      noro     2719:        if ( nd_blockmask ) {
                   2720:                l = nd_blockmask->n;
                   2721:                op = nd_blockmask->order_pair;
                   2722:                for ( j = 0, s = 0; j < l; j++ ) {
                   2723:                        ord_l = op[j].length;
1.57      noro     2724:                        for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
1.43      noro     2725:                }
                   2726:        } else {
1.56      noro     2727:                for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
1.43      noro     2728:        }
1.1       noro     2729:        return dl;
                   2730: }
                   2731:
1.61      noro     2732: void ndl_print(UINT *dl)
1.1       noro     2733: {
                   2734:        int n;
1.57      noro     2735:        int i,j,l,ord_l,s,s0;
1.43      noro     2736:        struct order_pair *op;
1.1       noro     2737:
                   2738:        n = nd_nvar;
                   2739:        printf("<<");
1.43      noro     2740:        if ( nd_blockmask ) {
                   2741:                l = nd_blockmask->n;
                   2742:                op = nd_blockmask->order_pair;
                   2743:                for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   2744:                        ord_l = op[j].length;
1.57      noro     2745:                        for ( i = 0; i < ord_l; i++, s++ )
                   2746:                                printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
1.43      noro     2747:                }
                   2748:        } else {
1.56      noro     2749:                for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
1.43      noro     2750:        }
1.1       noro     2751:        printf(">>");
                   2752: }
                   2753:
                   2754: void nd_print(ND p)
                   2755: {
                   2756:        NM m;
                   2757:
                   2758:        if ( !p )
                   2759:                printf("0\n");
                   2760:        else {
                   2761:                for ( m = BDY(p); m; m = NEXT(m) ) {
1.71      noro     2762:                        if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   2763:                        else printf("+%d*",CM(m));
1.14      noro     2764:                        ndl_print(DL(m));
1.1       noro     2765:                }
                   2766:                printf("\n");
                   2767:        }
                   2768: }
                   2769:
1.113     noro     2770: void nd_print_q(ND p)
1.16      noro     2771: {
                   2772:        NM m;
                   2773:
                   2774:        if ( !p )
                   2775:                printf("0\n");
                   2776:        else {
                   2777:                for ( m = BDY(p); m; m = NEXT(m) ) {
                   2778:                        printf("+");
1.113     noro     2779:                        printexpr(CO,(Obj)CQ(m));
1.16      noro     2780:                        printf("*");
                   2781:                        ndl_print(DL(m));
                   2782:                }
                   2783:                printf("\n");
                   2784:        }
                   2785: }
                   2786:
1.1       noro     2787: void ndp_print(ND_pairs d)
                   2788: {
                   2789:        ND_pairs t;
                   2790:
1.34      noro     2791:        for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
1.1       noro     2792:        printf("\n");
                   2793: }
                   2794:
1.20      noro     2795: void nd_removecont(int mod,ND p)
1.16      noro     2796: {
                   2797:        int i,n;
1.113     noro     2798:        Q *w;
                   2799:        Q dvr,t;
1.16      noro     2800:        NM m;
1.21      noro     2801:        struct oVECT v;
1.113     noro     2802:        N q,r;
1.16      noro     2803:
1.71      noro     2804:        if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
                   2805:        else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
1.20      noro     2806:        else {
                   2807:                for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
1.113     noro     2808:                w = (Q *)ALLOCA(n*sizeof(Q));
1.21      noro     2809:                v.len = n;
                   2810:                v.body = (pointer *)w;
1.113     noro     2811:                for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
1.146   ! noro     2812:                removecont_array((P *)w,n);
1.113     noro     2813:                for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
1.16      noro     2814:        }
                   2815: }
                   2816:
1.21      noro     2817: void nd_removecont2(ND p1,ND p2)
                   2818: {
                   2819:        int i,n1,n2,n;
1.113     noro     2820:        Q *w;
                   2821:        Q dvr,t;
1.21      noro     2822:        NM m;
                   2823:        struct oVECT v;
1.113     noro     2824:        N q,r;
1.21      noro     2825:
                   2826:        if ( !p1 ) {
                   2827:                nd_removecont(0,p2); return;
                   2828:        } else if ( !p2 ) {
                   2829:                nd_removecont(0,p1); return;
                   2830:        }
                   2831:        n1 = nd_length(p1);
                   2832:        n2 = nd_length(p2);
                   2833:        n = n1+n2;
1.113     noro     2834:        w = (Q *)ALLOCA(n*sizeof(Q));
1.21      noro     2835:        v.len = n;
                   2836:        v.body = (pointer *)w;
1.113     noro     2837:        for ( m = BDY(p1), i = 0; i < n1; m = NEXT(m), i++ ) w[i] = CQ(m);
                   2838:        for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
1.146   ! noro     2839:        removecont_array((P *)w,n);
1.113     noro     2840:        for ( m = BDY(p1), i = 0; i < n1; m = NEXT(m), i++ ) CQ(m) = w[i];
                   2841:        for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
1.21      noro     2842: }
                   2843:
1.20      noro     2844: void ndv_removecont(int mod,NDV p)
1.16      noro     2845: {
1.146   ! noro     2846:        int i,len,all_p;
        !          2847:        Q *c;
        !          2848:        P *w;
1.113     noro     2849:        Q dvr,t;
1.146   ! noro     2850:        P g,cont,tp;
1.16      noro     2851:        NMV m;
                   2852:
1.71      noro     2853:        if ( mod == -1 )
                   2854:                ndv_mul_c(mod,p,_invsf(HCM(p)));
                   2855:        else if ( mod )
1.20      noro     2856:                ndv_mul_c(mod,p,invm(HCM(p),mod));
                   2857:        else {
                   2858:                len = p->len;
1.146   ! noro     2859:                w = (P *)ALLOCA(len*sizeof(P));
        !          2860:                c = (Q *)ALLOCA(len*sizeof(Q));
        !          2861:                for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
        !          2862:                        ptozp(CP(m),1,&c[i],&w[i]);
        !          2863:                        all_p = all_p && !NUM(w[i]);
        !          2864:                }
        !          2865:                if ( all_p ) {
        !          2866:                        qltozl(c,len,&dvr); heu_nezgcdnpz(nd_vc,w,len,&g);
        !          2867:                        mulp(nd_vc,(P)dvr,g,&cont);
        !          2868:                        for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
        !          2869:                                divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
        !          2870:                        }
        !          2871:                } else {
        !          2872:                        sortbynm((Q *)c,len);
        !          2873:                        qltozl((Q *)c,len,&dvr);
        !          2874:                        for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
        !          2875:                                divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
        !          2876:                        }
1.113     noro     2877:                }
1.16      noro     2878:        }
1.21      noro     2879: }
                   2880:
1.61      noro     2881: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos)
                   2882: {
                   2883:        int len,i,max;
                   2884:        NMV m,mr0,mr,t;
                   2885:
                   2886:        len = p->len;
                   2887:        for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
                   2888:                max = MAX(max,TD(DL(m)));
                   2889:        mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   2890:        m = (NMV)((char *)mr0+(len-1)*oadv);
                   2891:        mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   2892:        t = (NMV)ALLOCA(nmv_adv);
                   2893:        for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   2894:                ndl_homogenize(DL(m),DL(t),obpe,oepos,max);
1.113     noro     2895:                CQ(mr) = CQ(m);
1.61      noro     2896:                ndl_copy(DL(t),DL(mr));
                   2897:        }
                   2898:        NV(p)++;
                   2899:        BDY(p) = mr0;
                   2900: }
                   2901:
1.45      noro     2902: void ndv_dehomogenize(NDV p,struct order_spec *ord)
1.23      noro     2903: {
1.45      noro     2904:        int i,j,adj,len,newnvar,newwpd,newadv,newexporigin;
1.113     noro     2905:        Q *w;
                   2906:        Q dvr,t;
1.23      noro     2907:        NMV m,r;
                   2908:
                   2909:        len = p->len;
                   2910:        newnvar = nd_nvar-1;
1.48      noro     2911:        newexporigin = nd_get_exporigin(ord);
1.45      noro     2912:        newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
1.23      noro     2913:        for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
1.34      noro     2914:                ndl_dehomogenize(DL(m));
1.23      noro     2915:        if ( newwpd != nd_wpd ) {
1.90      noro     2916:                newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
1.61      noro     2917:                for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.113     noro     2918:                        CQ(r) = CQ(m);
1.45      noro     2919:                        for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   2920:                        adj = nd_exporigin-newexporigin;
                   2921:                        for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
1.23      noro     2922:                }
                   2923:        }
                   2924:        NV(p)--;
                   2925: }
                   2926:
1.146   ! noro     2927: void removecont_array(P *p,int n)
        !          2928: {
        !          2929:        int all_p,all_q,i;
        !          2930:        Q *c;
        !          2931:        P *w;
        !          2932:        P t,s;
        !          2933:
        !          2934:        for ( all_q = 1, i = 0; i < n; i++ )
        !          2935:                all_q = all_q && NUM(p[i]);
        !          2936:        if ( all_q ) {
        !          2937:                removecont_array_q((Q *)p,n);
        !          2938:        } else {
        !          2939:                c = (Q *)ALLOCA(n*sizeof(Q));
        !          2940:                w = (P *)ALLOCA(n*sizeof(P));
        !          2941:                for ( i = 0; i < n; i++ ) {
        !          2942:                        ptozp(p[i],1,&c[i],&w[i]);
        !          2943:                }
        !          2944:                removecont_array_q(c,n);
        !          2945:                heu_nezgcdnpz(nd_vc,w,n,&t);
        !          2946:                for ( i = 0; i < n; i++ ) {
        !          2947:                        divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
        !          2948:                }
        !          2949:        }
        !          2950: }
        !          2951:
        !          2952: void removecont_array_q(Q *c,int n)
1.21      noro     2953: {
                   2954:        struct oVECT v;
1.113     noro     2955:        Q d0,d1,a,u,u1,gcd;
1.110     noro     2956:        int i,j;
1.113     noro     2957:        N qn,rn,gn;
                   2958:        Q *q,*r;
1.21      noro     2959:
1.113     noro     2960:        q = (Q *)ALLOCA(n*sizeof(Q));
                   2961:        r = (Q *)ALLOCA(n*sizeof(Q));
1.21      noro     2962:        v.id = O_VECT; v.len = n; v.body = (pointer *)c;
1.113     noro     2963:        igcdv_estimate(&v,&d0);
                   2964:        for ( i = 0; i < n; i++ ) {
                   2965:                divn(NM(c[i]),NM(d0),&qn,&rn);
                   2966:                NTOQ(qn,SGN(c[i])*SGN(d0),q[i]);
                   2967:                NTOQ(rn,SGN(c[i]),r[i]);
                   2968:        }
1.34      noro     2969:        for ( i = 0; i < n; i++ ) if ( r[i] ) break;
1.21      noro     2970:        if ( i < n ) {
                   2971:                v.id = O_VECT; v.len = n; v.body = (pointer *)r;
1.113     noro     2972:                igcdv(&v,&d1);
                   2973:                gcdn(NM(d0),NM(d1),&gn); NTOQ(gn,1,gcd);
                   2974:                divsn(NM(d0),gn,&qn); NTOQ(qn,1,a);
1.21      noro     2975:                for ( i = 0; i < n; i++ ) {
1.113     noro     2976:                        mulq(a,q[i],&u);
                   2977:                        if ( r[i] ) {
                   2978:                                divsn(NM(r[i]),gn,&qn); NTOQ(qn,SGN(r[i]),u1);
                   2979:                                addq(u,u1,&q[i]);
                   2980:                        } else
                   2981:                                q[i] = u;
1.21      noro     2982:                }
                   2983:        }
1.34      noro     2984:        for ( i = 0; i < n; i++ ) c[i] = q[i];
1.16      noro     2985: }
                   2986:
1.19      noro     2987: void nd_mul_c(int mod,ND p,int mul)
1.1       noro     2988: {
                   2989:        NM m;
                   2990:        int c,c1;
                   2991:
1.34      noro     2992:        if ( !p ) return;
1.115     noro     2993:        if ( mul == 1 ) return;
1.71      noro     2994:        if ( mod == -1 )
                   2995:                for ( m = BDY(p); m; m = NEXT(m) )
                   2996:                        CM(m) = _mulsf(CM(m),mul);
                   2997:        else
                   2998:                for ( m = BDY(p); m; m = NEXT(m) ) {
                   2999:                        c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   3000:                }
1.1       noro     3001: }
                   3002:
1.146   ! noro     3003: void nd_mul_c_q(ND p,P mul)
1.16      noro     3004: {
                   3005:        NM m;
1.146   ! noro     3006:        P c;
1.16      noro     3007:
1.113     noro     3008:        if ( !p ) return;
1.115     noro     3009:        if ( UNIQ(mul) ) return;
1.16      noro     3010:        for ( m = BDY(p); m; m = NEXT(m) ) {
1.146   ! noro     3011:                mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
1.16      noro     3012:        }
                   3013: }
                   3014:
1.61      noro     3015: void nd_mul_c_p(VL vl,ND p,P mul)
                   3016: {
                   3017:        NM m;
                   3018:        P c;
                   3019:
                   3020:        if ( !p ) return;
                   3021:        for ( m = BDY(p); m; m = NEXT(m) ) {
                   3022:                mulp(vl,CP(m),mul,&c); CP(m) = c;
                   3023:        }
                   3024: }
                   3025:
1.1       noro     3026: void nd_free(ND p)
                   3027: {
                   3028:        NM t,s;
                   3029:
1.34      noro     3030:        if ( !p ) return;
1.1       noro     3031:        t = BDY(p);
                   3032:        while ( t ) {
                   3033:                s = NEXT(t);
                   3034:                FREENM(t);
                   3035:                t = s;
                   3036:        }
                   3037:        FREEND(p);
                   3038: }
                   3039:
1.23      noro     3040: void ndv_free(NDV p)
                   3041: {
                   3042:        GC_free(BDY(p));
                   3043: }
                   3044:
1.61      noro     3045: void nd_append_red(UINT *d,int i)
1.1       noro     3046: {
1.13      noro     3047:        RHist m,m0;
1.1       noro     3048:        int h;
                   3049:
1.13      noro     3050:        NEWRHist(m);
1.34      noro     3051:        h = ndl_hash_value(d);
1.13      noro     3052:        m->index = i;
1.14      noro     3053:        ndl_copy(d,DL(m));
1.1       noro     3054:        NEXT(m) = nd_red[h];
                   3055:        nd_red[h] = m;
                   3056: }
                   3057:
1.61      noro     3058: UINT *ndv_compute_bound(NDV p)
1.1       noro     3059: {
1.61      noro     3060:        UINT *d1,*d2,*t;
                   3061:        UINT u;
1.57      noro     3062:        int i,j,k,l,len,ind;
1.45      noro     3063:        NMV m;
1.1       noro     3064:
                   3065:        if ( !p )
                   3066:                return 0;
1.61      noro     3067:        d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3068:        d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
1.45      noro     3069:        len = LEN(p);
                   3070:        m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   3071:        for ( i = 1; i < len; i++, NMV_ADV(m) ) {
1.14      noro     3072:                ndl_lcm(DL(m),d1,d2);
1.1       noro     3073:                t = d1; d1 = d2; d2 = t;
                   3074:        }
1.12      noro     3075:        l = nd_nvar+31;
1.61      noro     3076:        t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
1.57      noro     3077:        for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   3078:                u = d1[i];
                   3079:                k = (nd_epw-1)*nd_bpe;
                   3080:                for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   3081:                        t[ind] = (u>>k)&nd_mask0;
                   3082:        }
                   3083:        for ( ; ind < l; ind++ ) t[ind] = 0;
1.1       noro     3084:        return t;
                   3085: }
                   3086:
1.99      noro     3087: UINT *nd_compute_bound(ND p)
                   3088: {
                   3089:        UINT *d1,*d2,*t;
                   3090:        UINT u;
                   3091:        int i,j,k,l,len,ind;
                   3092:        NM m;
                   3093:
                   3094:        if ( !p )
                   3095:                return 0;
                   3096:        d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3097:        d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3098:        len = LEN(p);
                   3099:        m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   3100:        for ( m = NEXT(m); m; m = NEXT(m) ) {
                   3101:                ndl_lcm(DL(m),d1,d2);
                   3102:                t = d1; d1 = d2; d2 = t;
                   3103:        }
                   3104:        l = nd_nvar+31;
                   3105:        t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   3106:        for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   3107:                u = d1[i];
                   3108:                k = (nd_epw-1)*nd_bpe;
                   3109:                for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   3110:                        t[ind] = (u>>k)&nd_mask0;
                   3111:        }
                   3112:        for ( ; ind < l; ind++ ) t[ind] = 0;
                   3113:        return t;
                   3114: }
                   3115:
1.48      noro     3116: int nd_get_exporigin(struct order_spec *ord)
                   3117: {
1.51      noro     3118:        switch ( ord->id ) {
1.96      noro     3119:                case 0: case 2:
1.48      noro     3120:                        return 1;
1.41      noro     3121:                case 1:
                   3122:                        /* block order */
1.43      noro     3123:                        /* d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
1.48      noro     3124:                        return ord->ord.block.length+1;
1.96      noro     3125:                case 3:
                   3126:                        error("nd_get_exporigin : composite order is not supported yet.");
1.41      noro     3127:        }
1.48      noro     3128: }
                   3129:
1.61      noro     3130: void nd_setup_parameters(int nvar,int max) {
1.79      noro     3131:        int i,j,n,elen,ord_o,ord_l,l,s,wpd;
1.57      noro     3132:        struct order_pair *op;
1.48      noro     3133:
1.73      noro     3134:        nd_nvar = nvar;
1.79      noro     3135:        if ( max ) {
1.83      noro     3136:                /* XXX */
                   3137:                if ( do_weyl ) nd_bpe = 32;
                   3138:                else if ( max < 2 ) nd_bpe = 1;
1.79      noro     3139:                else if ( max < 4 ) nd_bpe = 2;
                   3140:                else if ( max < 8 ) nd_bpe = 3;
                   3141:                else if ( max < 16 ) nd_bpe = 4;
                   3142:                else if ( max < 32 ) nd_bpe = 5;
                   3143:                else if ( max < 64 ) nd_bpe = 6;
                   3144:                else if ( max < 256 ) nd_bpe = 8;
                   3145:                else if ( max < 1024 ) nd_bpe = 10;
                   3146:                else if ( max < 65536 ) nd_bpe = 16;
                   3147:                else nd_bpe = 32;
                   3148:        }
1.61      noro     3149:        nd_epw = (sizeof(UINT)*8)/nd_bpe;
1.48      noro     3150:        elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   3151:        nd_exporigin = nd_get_exporigin(nd_ord);
1.79      noro     3152:        wpd = nd_exporigin+elen;
                   3153:        if ( wpd != nd_wpd ) {
                   3154:                nd_free_private_storage();
                   3155:                nd_wpd = wpd;
                   3156:        }
1.1       noro     3157:        if ( nd_bpe < 32 ) {
                   3158:                nd_mask0 = (1<<nd_bpe)-1;
                   3159:        } else {
                   3160:                nd_mask0 = 0xffffffff;
                   3161:        }
                   3162:        bzero(nd_mask,sizeof(nd_mask));
                   3163:        nd_mask1 = 0;
                   3164:        for ( i = 0; i < nd_epw; i++ ) {
                   3165:                nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   3166:                nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   3167:        }
1.90      noro     3168:        nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
1.57      noro     3169:        nd_epos = nd_create_epos(nd_ord);
1.43      noro     3170:        nd_blockmask = nd_create_blockmask(nd_ord);
1.96      noro     3171:        nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
1.1       noro     3172: }
                   3173:
1.103     noro     3174: ND_pairs nd_reconstruct(int trace,ND_pairs d)
1.1       noro     3175: {
1.37      noro     3176:        int i,obpe,oadv,h;
1.71      noro     3177:        static NM prev_nm_free_list;
                   3178:        static ND_pairs prev_ndp_free_list;
1.13      noro     3179:        RHist mr0,mr;
                   3180:        RHist r;
1.37      noro     3181:        RHist *old_red;
1.71      noro     3182:        ND_pairs s0,s,t;
1.43      noro     3183:        EPOS oepos;
1.15      noro     3184:
1.1       noro     3185:        obpe = nd_bpe;
1.11      noro     3186:        oadv = nmv_adv;
1.43      noro     3187:        oepos = nd_epos;
1.63      noro     3188:        if ( obpe < 2 ) nd_bpe = 2;
                   3189:        else if ( obpe < 3 ) nd_bpe = 3;
1.62      noro     3190:        else if ( obpe < 4 ) nd_bpe = 4;
1.63      noro     3191:        else if ( obpe < 5 ) nd_bpe = 5;
1.34      noro     3192:        else if ( obpe < 6 ) nd_bpe = 6;
                   3193:        else if ( obpe < 8 ) nd_bpe = 8;
1.63      noro     3194:        else if ( obpe < 10 ) nd_bpe = 10;
1.34      noro     3195:        else if ( obpe < 16 ) nd_bpe = 16;
                   3196:        else if ( obpe < 32 ) nd_bpe = 32;
                   3197:        else error("nd_reconstruct : exponent too large");
1.5       noro     3198:
1.73      noro     3199:        nd_setup_parameters(nd_nvar,0);
1.1       noro     3200:        prev_nm_free_list = _nm_free_list;
                   3201:        prev_ndp_free_list = _ndp_free_list;
                   3202:        _nm_free_list = 0;
                   3203:        _ndp_free_list = 0;
1.53      noro     3204:        for ( i = nd_psn-1; i >= 0; i-- ) ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   3205:        if ( trace )
                   3206:                for ( i = nd_psn-1; i >= 0; i-- )
                   3207:                        ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
1.1       noro     3208:        s0 = 0;
                   3209:        for ( t = d; t; t = NEXT(t) ) {
                   3210:                NEXTND_pairs(s0,s);
                   3211:                s->i1 = t->i1;
                   3212:                s->i2 = t->i2;
1.14      noro     3213:                SG(s) = SG(t);
1.61      noro     3214:                ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
1.1       noro     3215:        }
1.37      noro     3216:
                   3217:        old_red = (RHist *)ALLOCA(REDTAB_LEN*sizeof(RHist));
1.6       noro     3218:        for ( i = 0; i < REDTAB_LEN; i++ ) {
1.37      noro     3219:                old_red[i] = nd_red[i];
                   3220:                nd_red[i] = 0;
                   3221:        }
                   3222:        for ( i = 0; i < REDTAB_LEN; i++ )
                   3223:                for ( r = old_red[i]; r; r = NEXT(r) ) {
                   3224:                        NEWRHist(mr);
1.13      noro     3225:                        mr->index = r->index;
1.20      noro     3226:                        SG(mr) = SG(r);
1.61      noro     3227:                        ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
1.37      noro     3228:                        h = ndl_hash_value(DL(mr));
                   3229:                        NEXT(mr) = nd_red[h];
                   3230:                        nd_red[h] = mr;
1.6       noro     3231:                }
1.37      noro     3232:        for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   3233:        old_red = 0;
1.11      noro     3234:        for ( i = 0; i < nd_psn; i++ ) {
1.20      noro     3235:                NEWRHist(r); SG(r) = SG(nd_psh[i]);
1.61      noro     3236:                ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.13      noro     3237:                nd_psh[i] = r;
1.11      noro     3238:        }
1.1       noro     3239:        if ( s0 ) NEXT(s) = 0;
                   3240:        prev_nm_free_list = 0;
                   3241:        prev_ndp_free_list = 0;
1.71      noro     3242: #if 0
1.1       noro     3243:        GC_gcollect();
1.71      noro     3244: #endif
1.1       noro     3245:        return s0;
                   3246: }
                   3247:
1.61      noro     3248: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
1.1       noro     3249: {
1.57      noro     3250:        int n,i,ei,oepw,omask0,j,s,ord_l,l;
1.43      noro     3251:        struct order_pair *op;
1.1       noro     3252:
                   3253:        n = nd_nvar;
1.61      noro     3254:        oepw = (sizeof(UINT)*8)/obpe;
1.43      noro     3255:        omask0 = (1<<obpe)-1;
1.34      noro     3256:        TD(r) = TD(d);
1.41      noro     3257:        for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
1.43      noro     3258:        if ( nd_blockmask ) {
                   3259:                l = nd_blockmask->n;
                   3260:                op = nd_blockmask->order_pair;
                   3261:                for ( i = 1; i < nd_exporigin; i++ )
                   3262:                        r[i] = d[i];
                   3263:                for ( j = 0, s = 0; j < l; j++ ) {
                   3264:                        ord_l = op[j].length;
1.57      noro     3265:                        for ( i = 0; i < ord_l; i++, s++ ) {
                   3266:                                ei =  GET_EXP_OLD(d,s);
                   3267:                                PUT_EXP(r,s,ei);
                   3268:                        }
1.1       noro     3269:                }
1.43      noro     3270:        } else {
1.56      noro     3271:                for ( i = 0; i < n; i++ ) {
                   3272:                        ei = GET_EXP_OLD(d,i);
                   3273:                        PUT_EXP(r,i,ei);
                   3274:                }
1.1       noro     3275:        }
                   3276: }
1.3       noro     3277:
1.6       noro     3278: ND nd_copy(ND p)
                   3279: {
                   3280:        NM m,mr,mr0;
1.41      noro     3281:        int c,n;
1.6       noro     3282:        ND r;
                   3283:
                   3284:        if ( !p )
                   3285:                return 0;
                   3286:        else {
                   3287:                for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   3288:                        NEXTNM(mr0,mr);
1.14      noro     3289:                        CM(mr) = CM(m);
                   3290:                        ndl_copy(DL(m),DL(mr));
1.6       noro     3291:                }
                   3292:                NEXT(mr) = 0;
1.31      noro     3293:                MKND(NV(p),mr0,LEN(p),r);
1.14      noro     3294:                SG(r) = SG(p);
1.6       noro     3295:                return r;
                   3296:        }
                   3297: }
                   3298:
1.53      noro     3299: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
1.11      noro     3300: {
                   3301:        NM m;
                   3302:        NDV p1,p2;
                   3303:        ND t1,t2;
1.61      noro     3304:        UINT *lcm;
1.31      noro     3305:        int td;
1.11      noro     3306:
1.77      noro     3307:        if ( !mod && nd_demand ) {
                   3308:                p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
1.53      noro     3309:        } else {
1.77      noro     3310:                if ( trace ) {
                   3311:                        p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   3312:                } else {
                   3313:                        p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   3314:                }
1.20      noro     3315:        }
1.34      noro     3316:        lcm = LCM(p);
1.11      noro     3317:        NEWNM(m);
1.113     noro     3318:        CQ(m) = HCQ(p2);
1.34      noro     3319:        ndl_sub(lcm,HDL(p1),DL(m));
1.56      noro     3320:        if ( ndl_check_bound2(p->i1,DL(m)) )
                   3321:                return 0;
1.55      noro     3322:        t1 = ndv_mul_nm(mod,m,p1);
1.71      noro     3323:        if ( mod == -1 ) CM(m) = _chsgnsf(HCM(p1));
                   3324:        else if ( mod ) CM(m) = mod-HCM(p1);
1.146   ! noro     3325:        else chsgnp(HCP(p1),&CP(m));
1.34      noro     3326:        ndl_sub(lcm,HDL(p2),DL(m));
1.14      noro     3327:        if ( ndl_check_bound2(p->i2,DL(m)) ) {
1.11      noro     3328:                nd_free(t1);
                   3329:                return 0;
                   3330:        }
1.55      noro     3331:        t2 = ndv_mul_nm(mod,m,p2);
1.31      noro     3332:        *rp = nd_add(mod,t1,t2);
1.11      noro     3333:        FREENM(m);
                   3334:        return 1;
                   3335: }
                   3336:
1.19      noro     3337: void ndv_mul_c(int mod,NDV p,int mul)
1.11      noro     3338: {
                   3339:        NMV m;
                   3340:        int c,c1,len,i;
                   3341:
1.34      noro     3342:        if ( !p ) return;
1.14      noro     3343:        len = LEN(p);
1.71      noro     3344:        if ( mod == -1 )
                   3345:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   3346:                        CM(m) = _mulsf(CM(m),mul);
                   3347:        else
                   3348:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   3349:                        c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   3350:                }
1.11      noro     3351: }
                   3352:
1.113     noro     3353: void ndv_mul_c_q(NDV p,Q mul)
1.16      noro     3354: {
                   3355:        NMV m;
1.113     noro     3356:        Q c;
1.16      noro     3357:        int len,i;
                   3358:
1.34      noro     3359:        if ( !p ) return;
1.16      noro     3360:        len = LEN(p);
                   3361:        for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.113     noro     3362:                mulq(CQ(m),mul,&c); CQ(m) = c;
1.16      noro     3363:        }
                   3364: }
                   3365:
1.55      noro     3366: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
                   3367:        int n2,i,j,l,n,tlen;
1.61      noro     3368:        UINT *d0;
1.55      noro     3369:        NM *tab,*psum;
                   3370:        ND s,r;
                   3371:        NM t;
                   3372:        NMV m1;
                   3373:
                   3374:        if ( !p ) return 0;
                   3375:        n = NV(p); n2 = n>>1;
                   3376:        d0 = DL(m0);
                   3377:        l = LEN(p);
                   3378:        for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   3379:        tab = (NM *)ALLOCA(tlen*sizeof(NM));
                   3380:        psum = (NM *)ALLOCA(tlen*sizeof(NM));
                   3381:        for ( i = 0; i < tlen; i++ ) psum[i] = 0;
1.56      noro     3382:        m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   3383:        for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
1.55      noro     3384:                /* m0(NM) * m1(NMV) => tab(NM) */
1.56      noro     3385:                weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
1.55      noro     3386:                for ( j = 0; j < tlen; j++ ) {
                   3387:                        if ( tab[j] ) {
                   3388:                                NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
                   3389:                        }
                   3390:                }
                   3391:        }
                   3392:        for ( i = tlen-1, r = 0; i >= 0; i-- )
                   3393:                if ( psum[i] ) {
                   3394:                        for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   3395:                        MKND(n,psum[i],j,s);
                   3396:                        r = nd_add(mod,r,s);
                   3397:                }
1.56      noro     3398:        if ( r ) SG(r) = SG(p)+TD(d0);
                   3399:        return r;
1.55      noro     3400: }
                   3401:
1.56      noro     3402: /* product of monomials */
                   3403: /* XXX block order is not handled correctly */
                   3404:
1.55      noro     3405: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   3406: {
1.56      noro     3407:        int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
1.61      noro     3408:        UINT *d0,*d1,*d,*dt,*ctab;
1.113     noro     3409:        Q *ctab_q;
                   3410:        Q q,q1;
1.61      noro     3411:        UINT c0,c1,c;
1.55      noro     3412:        NM *p;
                   3413:        NM m,t;
                   3414:
                   3415:        for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   3416:        if ( !m0 || !m1 ) return;
                   3417:        d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
                   3418:        NEWNM(m); d = DL(m);
1.56      noro     3419:        if ( mod ) {
                   3420:                c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
                   3421:        } else
1.113     noro     3422:                mulq(CQ(m0),CQ(m1),&CQ(m));
1.55      noro     3423:        for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   3424:        homo = n&1 ? 1 : 0;
                   3425:        if ( homo ) {
                   3426:                /* offset of h-degree */
                   3427:                h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   3428:                PUT_EXP(DL(m),n-1,h);
                   3429:                TD(DL(m)) = h;
1.61      noro     3430:                if ( nd_blockmask ) ndl_weight_mask(DL(m));
1.55      noro     3431:        }
                   3432:        tab[0] = m;
                   3433:        NEWNM(m); d = DL(m);
1.57      noro     3434:        for ( i = 0, curlen = 1; i < n2; i++ ) {
1.55      noro     3435:                a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   3436:                k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   3437:                /* xi^a*(Di^k*xi^l)*Di^b */
                   3438:                a += l; b += k;
1.56      noro     3439:                s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
1.55      noro     3440:                if ( !k || !l ) {
                   3441:                        for ( j = 0; j < curlen; j++ )
1.56      noro     3442:                                if ( t = tab[j] ) {
                   3443:                                        dt = DL(t);
                   3444:                                        PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
1.61      noro     3445:                                        if ( nd_blockmask ) ndl_weight_mask(dt);
1.55      noro     3446:                                }
                   3447:                        curlen *= k+1;
                   3448:                        continue;
                   3449:                }
                   3450:                min = MIN(k,l);
1.56      noro     3451:                if ( mod ) {
1.61      noro     3452:                        ctab = (UINT *)ALLOCA((min+1)*sizeof(UINT));
1.56      noro     3453:                        mkwcm(k,l,mod,ctab);
                   3454:                } else {
1.113     noro     3455:                        ctab_q = (Q *)ALLOCA((min+1)*sizeof(Q));
                   3456:                        mkwc(k,l,ctab_q);
1.56      noro     3457:                }
1.57      noro     3458:                for ( j = min; j >= 0; j-- ) {
1.56      noro     3459:                        for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
1.55      noro     3460:                        PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
1.56      noro     3461:                        h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
1.55      noro     3462:                        if ( homo ) {
                   3463:                                TD(d) = s;
1.56      noro     3464:                                PUT_EXP(d,n-1,s-h);
1.55      noro     3465:                        } else TD(d) = h;
1.61      noro     3466:                        if ( nd_blockmask ) ndl_weight_mask(d);
1.56      noro     3467:                        if ( mod ) c = ctab[j];
1.113     noro     3468:                        else q = ctab_q[j];
1.57      noro     3469:                        p = tab+curlen*j;
                   3470:                        if ( j == 0 ) {
                   3471:                                for ( u = 0; u < curlen; u++, p++ ) {
                   3472:                                        if ( tab[u] ) {
                   3473:                                                ndl_addto(DL(tab[u]),d);
                   3474:                                                if ( mod ) {
                   3475:                                                        c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
                   3476:                                                } else {
1.113     noro     3477:                                                        mulq(CQ(tab[u]),q,&q1); CQ(tab[u]) = q1;
1.57      noro     3478:                                                }
                   3479:                                        }
1.56      noro     3480:                                }
1.57      noro     3481:                        } else {
                   3482:                                for ( u = 0; u < curlen; u++, p++ ) {
                   3483:                                        if ( tab[u] ) {
                   3484:                                                NEWNM(t);
                   3485:                                                ndl_add(DL(tab[u]),d,DL(t));
                   3486:                                                if ( mod ) {
                   3487:                                                        c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
                   3488:                                                } else
1.113     noro     3489:                                                        mulq(CQ(tab[u]),q,&CQ(t));
1.57      noro     3490:                                                *p = t;
                   3491:                                        }
1.55      noro     3492:                                }
                   3493:                        }
                   3494:                }
                   3495:                curlen *= k+1;
                   3496:        }
                   3497:        FREENM(m);
                   3498: }
                   3499:
1.63      noro     3500: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   3501: {
                   3502:        NM mr,mr0;
                   3503:        NMV m;
                   3504:        UINT *d,*dt,*dm;
                   3505:        int c,n,td,i,c1,c2,len;
1.113     noro     3506:        Q q;
1.63      noro     3507:        ND r;
                   3508:
                   3509:        if ( !p ) return 0;
                   3510:        else {
                   3511:                n = NV(p); m = BDY(p);
                   3512:                d = DL(m0);
                   3513:                len = LEN(p);
                   3514:                mr0 = 0;
                   3515:                td = TD(d);
                   3516:                c = CM(m0);
                   3517:                for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   3518:                        NEXTNM(mr0,mr);
                   3519:                        CM(mr) = 1;
                   3520:                        ndl_add(DL(m),d,DL(mr));
                   3521:                }
                   3522:                NEXT(mr) = 0;
                   3523:                MKND(NV(p),mr0,len,r);
                   3524:                SG(r) = SG(p) + TD(d);
                   3525:                return r;
                   3526:        }
                   3527: }
                   3528:
1.55      noro     3529: ND ndv_mul_nm(int mod,NM m0,NDV p)
1.9       noro     3530: {
                   3531:        NM mr,mr0;
                   3532:        NMV m;
1.61      noro     3533:        UINT *d,*dt,*dm;
1.9       noro     3534:        int c,n,td,i,c1,c2,len;
1.146   ! noro     3535:        P q;
1.9       noro     3536:        ND r;
                   3537:
1.34      noro     3538:        if ( !p ) return 0;
1.55      noro     3539:        else if ( do_weyl )
1.71      noro     3540:                if ( mod == -1 )
                   3541:                        error("ndv_mul_nm : not implemented (weyl)");
                   3542:                else
                   3543:                        return weyl_ndv_mul_nm(mod,m0,p);
1.9       noro     3544:        else {
                   3545:                n = NV(p); m = BDY(p);
1.34      noro     3546:                d = DL(m0);
1.14      noro     3547:                len = LEN(p);
1.9       noro     3548:                mr0 = 0;
1.34      noro     3549:                td = TD(d);
1.71      noro     3550:                if ( mod == -1 ) {
                   3551:                        c = CM(m0);
                   3552:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   3553:                                NEXTNM(mr0,mr);
                   3554:                                CM(mr) = _mulsf(CM(m),c);
                   3555:                                ndl_add(DL(m),d,DL(mr));
                   3556:                        }
                   3557:                } else if ( mod ) {
1.16      noro     3558:                        c = CM(m0);
                   3559:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   3560:                                NEXTNM(mr0,mr);
                   3561:                                c1 = CM(m);
1.19      noro     3562:                                DMAR(c1,c,0,mod,c2);
1.16      noro     3563:                                CM(mr) = c2;
                   3564:                                ndl_add(DL(m),d,DL(mr));
                   3565:                        }
                   3566:                } else {
1.146   ! noro     3567:                        q = CP(m0);
1.16      noro     3568:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   3569:                                NEXTNM(mr0,mr);
1.146   ! noro     3570:                                mulp(nd_vc,CP(m),q,&CP(mr));
1.16      noro     3571:                                ndl_add(DL(m),d,DL(mr));
                   3572:                        }
1.4       noro     3573:                }
1.9       noro     3574:                NEXT(mr) = 0;
1.31      noro     3575:                MKND(NV(p),mr0,len,r);
1.34      noro     3576:                SG(r) = SG(p) + TD(d);
1.9       noro     3577:                return r;
1.4       noro     3578:        }
                   3579: }
                   3580:
1.104     noro     3581: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
1.99      noro     3582: {
                   3583:        NM mq0,mq;
1.102     noro     3584:        NMV tm;
1.113     noro     3585:        Q q;
1.104     noro     3586:        int i,nv,sg,c,c1,c2,hindex;
                   3587:        ND p,t,r;
                   3588:        N tnm;
                   3589:
1.124     noro     3590:        if ( bucket->m < 0 ) return 0;
1.99      noro     3591:        else {
1.104     noro     3592:                nv = NV(d);
1.99      noro     3593:                mq0 = 0;
1.102     noro     3594:                tm = (NMV)ALLOCA(nmv_adv);
1.104     noro     3595:                while ( 1 ) {
1.113     noro     3596:                        hindex = mod?head_pbucket(mod,bucket):head_pbucket_q(bucket);
1.104     noro     3597:                        if ( hindex < 0 ) break;
                   3598:                        p = bucket->body[hindex];
1.99      noro     3599:                        NEXTNM(mq0,mq);
                   3600:                        ndl_sub(HDL(p),HDL(d),DL(mq));
1.102     noro     3601:                        ndl_copy(DL(mq),DL(tm));
                   3602:                        if ( mod ) {
                   3603:                                c1 = invm(HCM(d),mod); c2 = HCM(p);
                   3604:                                DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   3605:                                CM(tm) = mod-c;
                   3606:                        } else {
1.113     noro     3607:                                divsn(NM(HCQ(p)),NM(HCQ(d)),&tnm);
                   3608:                                NTOQ(tnm,SGN(HCQ(p))*SGN(HCQ(d)),CQ(mq));
                   3609:                                chsgnq(CQ(mq),&CQ(tm));
1.102     noro     3610:                        }
                   3611:                        t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
1.104     noro     3612:                        bucket->body[hindex] = nd_remove_head(p);
                   3613:                        t = nd_remove_head(t);
                   3614:                        add_pbucket(mod,bucket,t);
                   3615:                }
                   3616:                if ( !mq0 )
                   3617:                        r = 0;
                   3618:                else {
                   3619:                        NEXT(mq) = 0;
                   3620:                        for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   3621:                        MKND(nv,mq0,i,r);
                   3622:                        /* XXX */
                   3623:                        SG(r) = HTD(r);
1.99      noro     3624:                }
                   3625:                return r;
                   3626:        }
                   3627: }
                   3628:
1.43      noro     3629: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
1.11      noro     3630: {
1.13      noro     3631:        NMV m,mr,mr0,t;
                   3632:        int len,i,k;
1.11      noro     3633:
1.61      noro     3634:        if ( !p ) return;
                   3635:        m = BDY(p); len = LEN(p);
                   3636:        mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   3637:        m = (NMV)((char *)mr0+(len-1)*oadv);
                   3638:        mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   3639:        t = (NMV)ALLOCA(nmv_adv);
                   3640:        for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.113     noro     3641:                CQ(t) = CQ(m);
1.61      noro     3642:                for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   3643:                ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.113     noro     3644:                CQ(mr) = CQ(t);
1.61      noro     3645:                ndl_copy(DL(t),DL(mr));
                   3646:        }
                   3647:        BDY(p) = mr0;
                   3648: }
                   3649:
                   3650: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   3651: {
                   3652:        NMV m,mr,mr0;
                   3653:        int len,i;
                   3654:        NDV r;
1.11      noro     3655:
1.61      noro     3656:        if ( !p ) return 0;
                   3657:        m = BDY(p); len = LEN(p);
                   3658:        mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   3659:        for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   3660:                ndl_zero(DL(mr));
                   3661:                ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.113     noro     3662:                CQ(mr) = CQ(m);
1.11      noro     3663:        }
1.61      noro     3664:        MKNDV(NV(p),mr0,len,r);
                   3665:        SG(r) = SG(p);
                   3666:        return r;
1.11      noro     3667: }
                   3668:
1.61      noro     3669: /* duplicate p */
                   3670:
                   3671: NDV ndv_dup(int mod,NDV p)
1.3       noro     3672: {
                   3673:        NDV d;
1.61      noro     3674:        NMV t,m,m0;
1.3       noro     3675:        int i,len;
                   3676:
1.34      noro     3677:        if ( !p ) return 0;
1.31      noro     3678:        len = LEN(p);
1.34      noro     3679:        m0 = m = (NMV)(mod?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
1.61      noro     3680:        for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
1.14      noro     3681:                ndl_copy(DL(t),DL(m));
1.113     noro     3682:                CQ(m) = CQ(t);
1.3       noro     3683:        }
                   3684:        MKNDV(NV(p),m0,len,d);
1.23      noro     3685:        SG(d) = SG(p);
                   3686:        return d;
                   3687: }
                   3688:
1.63      noro     3689: ND nd_dup(ND p)
                   3690: {
                   3691:        ND d;
                   3692:        NM t,m,m0;
                   3693:
                   3694:        if ( !p ) return 0;
                   3695:        for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   3696:                NEXTNM(m0,m);
                   3697:                ndl_copy(DL(t),DL(m));
1.113     noro     3698:                CQ(m) = CQ(t);
1.63      noro     3699:        }
                   3700:        if ( m0 ) NEXT(m) = 0;
                   3701:        MKND(NV(p),m0,LEN(p),d);
                   3702:        SG(d) = SG(p);
                   3703:        return d;
                   3704: }
                   3705:
1.61      noro     3706: /* XXX if p->len == 0 then it represents 0 */
                   3707:
                   3708: void ndv_mod(int mod,NDV p)
                   3709: {
                   3710:        NMV t,d;
1.125     noro     3711:        int r,s,u;
1.61      noro     3712:        int i,len,dlen;
1.146   ! noro     3713:        Q c;
        !          3714:        P cp,cp1;
        !          3715:        NODE tn;
1.71      noro     3716:        Obj gfs;
1.61      noro     3717:
                   3718:        if ( !p ) return;
                   3719:        len = LEN(p);
                   3720:        dlen = 0;
1.71      noro     3721:        if ( mod == -1 )
                   3722:                for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   3723:                        simp_ff((Obj)CP(t),&gfs);
                   3724:                        r = FTOIF(CONT((GFS)gfs));
1.61      noro     3725:                        CM(d) = r;
                   3726:                        ndl_copy(DL(t),DL(d));
                   3727:                        NMV_ADV(d);
                   3728:                        dlen++;
                   3729:                }
1.71      noro     3730:        else
                   3731:                for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
1.146   ! noro     3732:                        if ( nd_vc ) {
        !          3733:                                for ( tn = nd_subst, cp = CP(t); tn; tn = NEXT(NEXT(tn)) ) {
        !          3734:                                        substp(nd_vc,cp,BDY(tn),BDY(NEXT(tn)),&cp1); cp = cp1;
        !          3735:                                }
        !          3736:                                c = (Q)cp;
        !          3737:                        } else
        !          3738:                                c = CQ(t);
        !          3739:                        r = rem(NM(c),mod);
1.71      noro     3740:                        if ( r ) {
1.146   ! noro     3741:                                if ( SGN(c) < 0 )
1.113     noro     3742:                                        r = mod-r;
1.146   ! noro     3743:                                if ( DN(c) ) {
        !          3744:                                        s = rem(DN(c),mod);
1.125     noro     3745:                                        if ( !s )
                   3746:                                                error("ndv_mod : division by 0");
                   3747:                                        s = invm(s,mod);
                   3748:                                        DMAR(r,s,0,mod,u); r = u;
                   3749:                                }
1.71      noro     3750:                                CM(d) = r;
                   3751:                                ndl_copy(DL(t),DL(d));
                   3752:                                NMV_ADV(d);
                   3753:                                dlen++;
                   3754:                        }
                   3755:                }
1.61      noro     3756:        LEN(p) = dlen;
                   3757: }
                   3758:
                   3759: NDV ptondv(VL vl,VL dvl,P p)
                   3760: {
                   3761:        ND nd;
                   3762:
                   3763:        nd = ptond(vl,dvl,p);
                   3764:        return ndtondv(0,nd);
                   3765: }
                   3766:
                   3767: ND ptond(VL vl,VL dvl,P p)
1.23      noro     3768: {
1.61      noro     3769:        int n,i,j,k,e;
                   3770:        VL tvl;
                   3771:        V v;
                   3772:        DCP dc;
                   3773:        DCP *w;
                   3774:        ND r,s,t,u;
                   3775:        P x;
                   3776:        int c;
                   3777:        UINT *d;
1.23      noro     3778:        NM m,m0;
1.61      noro     3779:
                   3780:        if ( !p )
                   3781:                return 0;
                   3782:        else if ( NUM(p) ) {
                   3783:                NEWNM(m);
                   3784:                ndl_zero(DL(m));
1.113     noro     3785:                CQ(m) = (Q)p;
1.61      noro     3786:                NEXT(m) = 0;
                   3787:                MKND(nd_nvar,m,1,r);
                   3788:                SG(r) = 0;
                   3789:                return r;
                   3790:        } else {
                   3791:                for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   3792:                w = (DCP *)ALLOCA(k*sizeof(DCP));
                   3793:                for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   3794:                for ( i = 0, tvl = dvl, v = VR(p);
1.98      noro     3795:                        tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
1.61      noro     3796:                if ( !tvl ) {
                   3797:                        for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   3798:                                t = ptond(vl,dvl,COEF(w[j]));
                   3799:                                pwrp(vl,x,DEG(w[j]),&p);
                   3800:                                nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   3801:                        }
                   3802:                        return s;
                   3803:                } else {
                   3804:                        NEWNM(m0); d = DL(m0);
                   3805:                        for ( j = k-1, s = 0; j >= 0; j-- ) {
                   3806:                                ndl_zero(d); e = QTOS(DEG(w[j])); PUT_EXP(d,i,e);
                   3807:                                TD(d) = MUL_WEIGHT(e,i);
                   3808:                                if ( nd_blockmask) ndl_weight_mask(d);
                   3809:                                t = ptond(vl,dvl,COEF(w[j]));
                   3810:                                for ( m = BDY(t); m; m = NEXT(m) )
                   3811:                                        ndl_addto(DL(m),d);
                   3812:                                SG(t) += TD(d);
                   3813:                                s = nd_add(0,s,t);
                   3814:                        }
                   3815:                        FREENM(m0);
                   3816:                        return s;
                   3817:                }
                   3818:        }
                   3819: }
                   3820:
                   3821: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   3822: {
                   3823:        VL tvl;
                   3824:        int len,n,j,i,e;
                   3825:        NMV m;
                   3826:        Q q;
                   3827:        P c;
                   3828:        UINT *d;
                   3829:        P s,r,u,t,w;
1.71      noro     3830:        GFS gfs;
1.23      noro     3831:
1.34      noro     3832:        if ( !p ) return 0;
1.61      noro     3833:        else {
                   3834:                len = LEN(p);
                   3835:                n = NV(p);
                   3836:                m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   3837:                for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
1.71      noro     3838:                        if ( mod == -1 ) {
                   3839:                                e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   3840:                        } else if ( mod ) {
1.61      noro     3841:                                STOQ(CM(m),q); c = (P)q;
                   3842:                        } else
1.113     noro     3843:                                c = CP(m);
1.61      noro     3844:                        d = DL(m);
                   3845:                        for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   3846:                                MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   3847:                                pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   3848:                        }
                   3849:                        addp(vl,s,t,&u); s = u;
                   3850:                }
                   3851:                return s;
1.23      noro     3852:        }
1.3       noro     3853: }
                   3854:
1.61      noro     3855: NDV ndtondv(int mod,ND p)
1.11      noro     3856: {
                   3857:        NDV d;
1.61      noro     3858:        NMV m,m0;
                   3859:        NM t;
                   3860:        int i,len;
1.11      noro     3861:
1.34      noro     3862:        if ( !p ) return 0;
1.61      noro     3863:        len = LEN(p);
1.74      noro     3864:        if ( mod )
                   3865:                m0 = m = (NMV)GC_malloc_atomic_ignore_off_page(len*nmv_adv);
                   3866:        else
                   3867:                m0 = m = MALLOC(len*nmv_adv);
1.103     noro     3868: #if 0
1.74      noro     3869:        ndv_alloc += nmv_adv*len;
1.103     noro     3870: #endif
1.61      noro     3871:        for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   3872:                ndl_copy(DL(t),DL(m));
1.113     noro     3873:                CQ(m) = CQ(t);
1.11      noro     3874:        }
1.61      noro     3875:        MKNDV(NV(p),m0,len,d);
1.14      noro     3876:        SG(d) = SG(p);
1.11      noro     3877:        return d;
                   3878: }
                   3879:
1.61      noro     3880: ND ndvtond(int mod,NDV p)
1.11      noro     3881: {
1.61      noro     3882:        ND d;
                   3883:        NM m,m0;
1.11      noro     3884:        NMV t;
1.61      noro     3885:        int i,len;
1.11      noro     3886:
1.34      noro     3887:        if ( !p ) return 0;
1.11      noro     3888:        m0 = 0;
1.61      noro     3889:        len = p->len;
                   3890:        for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   3891:                NEXTNM(m0,m);
                   3892:                ndl_copy(DL(t),DL(m));
1.113     noro     3893:                CQ(m) = CQ(t);
1.11      noro     3894:        }
                   3895:        NEXT(m) = 0;
1.61      noro     3896:        MKND(NV(p),m0,len,d);
1.14      noro     3897:        SG(d) = SG(p);
1.11      noro     3898:        return d;
                   3899: }
                   3900:
1.3       noro     3901: void ndv_print(NDV p)
                   3902: {
                   3903:        NMV m;
                   3904:        int i,len;
                   3905:
1.34      noro     3906:        if ( !p ) printf("0\n");
1.3       noro     3907:        else {
1.14      noro     3908:                len = LEN(p);
1.3       noro     3909:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.71      noro     3910:                        if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   3911:                        else printf("+%d*",CM(m));
1.16      noro     3912:                        ndl_print(DL(m));
                   3913:                }
                   3914:                printf("\n");
                   3915:        }
                   3916: }
                   3917:
1.113     noro     3918: void ndv_print_q(NDV p)
1.16      noro     3919: {
                   3920:        NMV m;
                   3921:        int i,len;
                   3922:
1.34      noro     3923:        if ( !p ) printf("0\n");
1.16      noro     3924:        else {
                   3925:                len = LEN(p);
                   3926:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   3927:                        printf("+");
1.113     noro     3928:                        printexpr(CO,(Obj)CQ(m));
1.16      noro     3929:                        printf("*");
1.14      noro     3930:                        ndl_print(DL(m));
1.3       noro     3931:                }
                   3932:                printf("\n");
                   3933:        }
1.25      noro     3934: }
                   3935:
1.61      noro     3936: NODE ndv_reducebase(NODE x)
1.27      noro     3937: {
                   3938:        int len,i,j;
                   3939:        NDV *w;
                   3940:        NODE t,t0;
                   3941:
                   3942:        len = length(x);
                   3943:        w = (NDV *)ALLOCA(len*sizeof(NDV));
                   3944:        for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) w[i] = BDY(t);
                   3945:        for ( i = 0; i < len; i++ ) {
                   3946:                for ( j = 0; j < i; j++ ) {
                   3947:                        if ( w[i] && w[j] )
                   3948:                                if ( ndl_reducible(HDL(w[i]),HDL(w[j])) ) w[i] = 0;
                   3949:                                else if ( ndl_reducible(HDL(w[j]),HDL(w[i])) ) w[j] = 0;
                   3950:                }
                   3951:        }
                   3952:        for ( i = len-1, t0 = 0; i >= 0; i-- ) {
                   3953:                if ( w[i] ) { NEXTNODE(t0,t); BDY(t) = (pointer)w[i]; }
                   3954:        }
                   3955:        NEXT(t) = 0; x = t0;
                   3956:        return x;
1.11      noro     3957: }
1.32      noro     3958:
1.43      noro     3959: /* XXX incomplete */
                   3960:
1.32      noro     3961: void nd_init_ord(struct order_spec *ord)
                   3962: {
1.43      noro     3963:        switch ( ord->id ) {
1.32      noro     3964:                case 0:
1.43      noro     3965:                        switch ( ord->ord.simple ) {
                   3966:                                case 0:
                   3967:                                        nd_dcomp = 1;
                   3968:                                        nd_isrlex = 1;
                   3969:                                        break;
                   3970:                                case 1:
                   3971:                                        nd_dcomp = 1;
                   3972:                                        nd_isrlex = 0;
                   3973:                                        break;
                   3974:                                case 2:
                   3975:                                        nd_dcomp = 0;
                   3976:                                        nd_isrlex = 0;
1.45      noro     3977:                                        ndl_compare_function = ndl_lex_compare;
1.58      noro     3978:                                        break;
                   3979:                                case 11:
                   3980:                                        /* XXX */
                   3981:                                        nd_dcomp = 0;
                   3982:                                        nd_isrlex = 1;
                   3983:                                        ndl_compare_function = ndl_ww_lex_compare;
1.43      noro     3984:                                        break;
                   3985:                                default:
                   3986:                                        error("nd_gr : unsupported order");
                   3987:                        }
1.32      noro     3988:                        break;
                   3989:                case 1:
1.96      noro     3990:                        /* block order */
1.43      noro     3991:                        /* XXX */
                   3992:                        nd_dcomp = -1;
1.32      noro     3993:                        nd_isrlex = 0;
1.45      noro     3994:                        ndl_compare_function = ndl_block_compare;
1.34      noro     3995:                        break;
1.43      noro     3996:                case 2:
1.96      noro     3997:                        /* matrix order */
                   3998:                        /* XXX */
                   3999:                        nd_dcomp = -1;
                   4000:                        nd_isrlex = 0;
                   4001:                        nd_matrix_len = ord->ord.matrix.row;
                   4002:                        nd_matrix = ord->ord.matrix.matrix;
                   4003:                        ndl_compare_function = ndl_matrix_compare;
                   4004:                        break;
                   4005:                case 3:
1.97      noro     4006:                        /* composite order */
                   4007:                        nd_dcomp = -1;
                   4008:                        nd_isrlex = 0;
                   4009:                        nd_worb_len = ord->ord.composite.length;
                   4010:                        nd_worb = ord->ord.composite.w_or_b;
                   4011:                        ndl_compare_function = ndl_composite_compare;
1.32      noro     4012:                        break;
                   4013:        }
1.41      noro     4014:        nd_ord = ord;
1.32      noro     4015: }
                   4016:
1.43      noro     4017: BlockMask nd_create_blockmask(struct order_spec *ord)
                   4018: {
                   4019:        int n,i,j,s,l;
1.61      noro     4020:        UINT *t;
1.43      noro     4021:        BlockMask bm;
                   4022:
1.96      noro     4023:        /* we only create mask table for block order */
                   4024:        if ( ord->id != 1 )
1.43      noro     4025:                return 0;
                   4026:        n = ord->ord.block.length;
                   4027:        bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   4028:        bm->n = n;
                   4029:        bm->order_pair = ord->ord.block.order_pair;
1.61      noro     4030:        bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
1.43      noro     4031:        for ( i = 0, s = 0; i < n; i++ ) {
1.61      noro     4032:                bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
1.43      noro     4033:                for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   4034:                l = bm->order_pair[i].length;
                   4035:                for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   4036:        }
                   4037:        return bm;
1.57      noro     4038: }
                   4039:
                   4040: EPOS nd_create_epos(struct order_spec *ord)
                   4041: {
                   4042:        int i,j,l,s,ord_l,ord_o;
                   4043:        EPOS epos;
                   4044:        struct order_pair *op;
                   4045:
                   4046:        epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   4047:        switch ( ord->id ) {
                   4048:                case 0:
                   4049:                        if ( nd_isrlex ) {
                   4050:                                for ( i = 0; i < nd_nvar; i++ ) {
                   4051:                                        epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   4052:                                        epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   4053:                                }
                   4054:                        } else {
                   4055:                                for ( i = 0; i < nd_nvar; i++ ) {
                   4056:                                        epos[i].i = nd_exporigin + i/nd_epw;
                   4057:                                        epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   4058:                                }
                   4059:                        }
                   4060:                        break;
                   4061:                case 1:
                   4062:                        /* block order */
                   4063:                        l = ord->ord.block.length;
                   4064:                        op = ord->ord.block.order_pair;
                   4065:                        for ( j = 0, s = 0; j < l; j++ ) {
                   4066:                                ord_o = op[j].order;
                   4067:                                ord_l = op[j].length;
                   4068:                                if ( !ord_o )
                   4069:                                        for ( i = 0; i < ord_l; i++ ) {
                   4070:                                                epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   4071:                                                epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   4072:                                        }
                   4073:                                else
                   4074:                                        for ( i = 0; i < ord_l; i++ ) {
                   4075:                                                epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   4076:                                                epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   4077:                                        }
                   4078:                                s += ord_l;
                   4079:                        }
                   4080:                        break;
                   4081:                case 2:
1.96      noro     4082:                        /* matrix order */
                   4083:                case 3:
                   4084:                        /* composite order */
                   4085:                        for ( i = 0; i < nd_nvar; i++ ) {
                   4086:                                epos[i].i = nd_exporigin + i/nd_epw;
                   4087:                                epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   4088:                        }
                   4089:                        break;
1.57      noro     4090:        }
                   4091:        return epos;
1.43      noro     4092: }
1.59      noro     4093:
                   4094: /* external interface */
                   4095:
                   4096: void nd_nf_p(P f,LIST g,LIST v,int m,struct order_spec *ord,P *rp)
                   4097: {
1.61      noro     4098:        NODE t,in0,in;
1.59      noro     4099:        ND nd,nf;
1.61      noro     4100:        NDV ndv;
                   4101:        VL vv,tv;
                   4102:        int stat,nvar,max,e;
1.113     noro     4103:        union oNDC dn;
1.146   ! noro     4104:        Q cont;
        !          4105:        P pp;
1.59      noro     4106:
1.116     noro     4107:        if ( !f ) {
                   4108:                *rp = 0;
                   4109:                return;
                   4110:        }
1.59      noro     4111:        pltovl(v,&vv);
1.74      noro     4112:        for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
1.61      noro     4113:
                   4114:        /* get the degree bound */
                   4115:        for ( t = BDY(g), max = 0; t; t = NEXT(t) )
                   4116:                for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4117:                        e = getdeg(tv->v,(P)BDY(t));
                   4118:                        max = MAX(e,max);
                   4119:                }
                   4120:        for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4121:                e = getdeg(tv->v,f);
                   4122:                max = MAX(e,max);
                   4123:        }
                   4124:
1.59      noro     4125:        nd_init_ord(ord);
1.61      noro     4126:        nd_setup_parameters(nvar,max);
                   4127:
                   4128:        /* conversion to ndv */
                   4129:        for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   4130:                NEXTNODE(in0,in);
1.146   ! noro     4131:                ptozp((P)BDY(t),1,&cont,&pp);
        !          4132:                BDY(in) = (pointer)ptondv(CO,vv,pp);
1.69      noro     4133:                if ( m ) ndv_mod(m,(NDV)BDY(in));
1.61      noro     4134:        }
                   4135:        NEXTNODE(in0,in);
                   4136:        BDY(in) = (pointer)ptondv(CO,vv,f);
1.69      noro     4137:        if ( m ) ndv_mod(m,(NDV)BDY(in));
1.61      noro     4138:        NEXT(in) = 0;
                   4139:
1.128     noro     4140:        /* dont sort */
                   4141:        ndv_setup(m,0,in0,1);
1.59      noro     4142:        nd_psn--;
                   4143:        nd_scale=2;
                   4144:        while ( 1 ) {
                   4145:                nd = (pointer)ndvtond(m,nd_ps[nd_psn]);
1.146   ! noro     4146:                stat = nd_nf(m,0,nd,nd_ps,1,0,&nf);
1.59      noro     4147:                if ( !stat ) {
                   4148:                        nd_psn++;
1.103     noro     4149:                        nd_reconstruct(0,0);
1.59      noro     4150:                        nd_psn--;
                   4151:                } else
                   4152:                        break;
                   4153:        }
1.61      noro     4154:        *rp = ndvtop(m,CO,vv,ndtondv(m,nf));
1.63      noro     4155: }
                   4156:
                   4157: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   4158: {
                   4159:        NM m;
                   4160:        UINT *t,*s;
                   4161:        int i;
                   4162:
                   4163:        for ( i = 0; i < n; i++ ) r[i] = 0;
                   4164:        for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   4165:                t = DL(m);
                   4166:                for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   4167:                r[i] = CM(m);
                   4168:        }
                   4169:        for ( i = 0; !r[i]; i++ );
                   4170:        return i;
                   4171: }
                   4172:
1.113     noro     4173: int nd_to_vect_q(UINT *s0,int n,ND d,Q *r)
1.74      noro     4174: {
1.107     noro     4175:        NM m;
1.74      noro     4176:        UINT *t,*s;
1.107     noro     4177:        int i;
1.74      noro     4178:
                   4179:        for ( i = 0; i < n; i++ ) r[i] = 0;
1.107     noro     4180:        for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
1.74      noro     4181:                t = DL(m);
                   4182:                for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.113     noro     4183:                r[i] = CQ(m);
1.74      noro     4184:        }
                   4185:        for ( i = 0; !r[i]; i++ );
                   4186:        return i;
                   4187: }
                   4188:
1.129     noro     4189: Q *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   4190: {
                   4191:        NM m;
                   4192:        NMV mr;
                   4193:        UINT *d,*t,*s;
                   4194:        NDV p;
                   4195:        int i,j,len;
                   4196:        Q *r;
                   4197:
                   4198:        m = pair->mul;
                   4199:        d = DL(m);
                   4200:        p = nd_ps[pair->index];
                   4201:        len = LEN(p);
                   4202:        r = (Q *)CALLOC(n,sizeof(Q));
                   4203:        t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   4204:        for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   4205:                ndl_add(d,DL(mr),t);
                   4206:                for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   4207:                r[i] = CQ(mr);
                   4208:        }
                   4209:        return r;
                   4210: }
                   4211:
1.67      noro     4212: IndArray nm_ind_pair_to_vect_compress(int mod,UINT *s0,int n,NM_ind_pair pair)
1.64      noro     4213: {
                   4214:        NM m;
                   4215:        NMV mr;
                   4216:        UINT *d,*t,*s;
                   4217:        NDV p;
1.67      noro     4218:        unsigned char *ivc;
                   4219:        unsigned short *ivs;
1.81      noro     4220:        UINT *v,*ivi,*s0v;
1.67      noro     4221:        int i,j,len,prev,diff,cdiff;
                   4222:        IndArray r;
1.64      noro     4223:
                   4224:        m = pair->mul;
                   4225:        d = DL(m);
                   4226:        p = nd_ps[pair->index];
                   4227:        len = LEN(p);
                   4228:        t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
1.67      noro     4229:        v = (unsigned int *)ALLOCA(len*sizeof(unsigned int));
1.64      noro     4230:        for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   4231:                ndl_add(d,DL(mr),t);
                   4232:                for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.67      noro     4233:                v[j] = i;
                   4234:        }
1.81      noro     4235:        r = (IndArray)MALLOC(sizeof(struct oIndArray));
1.67      noro     4236:        r->head = v[0];
                   4237:        diff = 0;
                   4238:        for ( i = 1; i < len; i++ ) {
                   4239:                cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   4240:        }
                   4241:        if ( diff < 256 ) {
                   4242:                r->width = 1;
                   4243:                ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   4244:                r->index.c = ivc;
                   4245:                for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   4246:        } else if ( diff < 65536 ) {
                   4247:                r->width = 2;
                   4248:                ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   4249:                r->index.s = ivs;
                   4250:                for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   4251:        } else {
                   4252:                r->width = 4;
                   4253:                ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   4254:                r->index.i = ivi;
                   4255:                for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
1.64      noro     4256:        }
1.67      noro     4257:        return r;
1.64      noro     4258: }
                   4259:
1.135     noro     4260: int compress_array(Q *svect,Q *cvect,int n)
                   4261: {
                   4262:        int i,j;
                   4263:
                   4264:        for ( i = j = 0; i < n; i++ )
                   4265:                if ( svect[i] ) cvect[j++] = svect[i];
                   4266:        return j;
                   4267: }
                   4268:
                   4269: void expand_array(Q *svect,Q *cvect,int n)
                   4270: {
                   4271:        int i,j;
                   4272:
                   4273:        for ( i = j = 0; j < n;  i++  )
                   4274:                if ( svect[i] ) svect[i] = cvect[j++];
                   4275: }
                   4276:
1.133     noro     4277: int ndv_reduce_vect_q(Q *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.107     noro     4278: {
1.135     noro     4279:        int i,j,k,len,pos,prev,nz;
1.113     noro     4280:        Q cs,mcs,c1,c2,cr,gcd,t;
1.107     noro     4281:        IndArray ivect;
                   4282:        unsigned char *ivc;
                   4283:        unsigned short *ivs;
                   4284:        unsigned int *ivi;
                   4285:        NDV redv;
                   4286:        NMV mr;
                   4287:        NODE rp;
                   4288:        int maxrs;
1.133     noro     4289:        double hmag;
1.135     noro     4290:        Q *cvect;
1.107     noro     4291:
                   4292:        maxrs = 0;
1.134     noro     4293:        for ( i = 0; i < col && !svect[i]; i++ );
                   4294:        if ( i == col ) return maxrs;
                   4295:        hmag = p_mag((P)svect[i])*nd_scale;
1.135     noro     4296:        cvect = (Q *)ALLOCA(col*sizeof(Q));
1.107     noro     4297:        for ( i = 0; i < nred; i++ ) {
                   4298:                ivect = imat[i];
                   4299:                k = ivect->head;
                   4300:                if ( svect[k] ) {
                   4301:                        maxrs = MAX(maxrs,rp0[i]->sugar);
1.133     noro     4302:                        redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
1.107     noro     4303:                        len = LEN(redv); mr = BDY(redv);
1.113     noro     4304:                        igcd_cofactor(svect[k],CQ(mr),&gcd,&cs,&cr);
                   4305:                        chsgnq(cs,&mcs);
                   4306:                        if ( !UNIQ(cr) ) {
                   4307:                                for ( j = 0; j < col; j++ ) {
                   4308:                                        mulq(svect[j],cr,&c1); svect[j] = c1;
                   4309:                                }
                   4310:                        }
1.107     noro     4311:                        svect[k] = 0; prev = k;
                   4312:                        switch ( ivect->width ) {
                   4313:                                case 1:
                   4314:                                        ivc = ivect->index.c;
                   4315:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   4316:                                                pos = prev+ivc[j]; prev = pos;
1.113     noro     4317:                                                mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
1.107     noro     4318:                                        }
                   4319:                                        break;
                   4320:                                case 2:
                   4321:                                        ivs = ivect->index.s;
                   4322:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   4323:                                                pos = prev+ivs[j]; prev = pos;
1.113     noro     4324:                                                mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
1.107     noro     4325:                                        }
                   4326:                                        break;
                   4327:                                case 4:
                   4328:                                        ivi = ivect->index.i;
                   4329:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   4330:                                                pos = prev+ivi[j]; prev = pos;
1.113     noro     4331:                                                mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
1.107     noro     4332:                                        }
                   4333:                                        break;
                   4334:                        }
1.134     noro     4335:                        for ( j = k+1; j < col && !svect[j]; j++ );
                   4336:                        if ( j == col ) break;
                   4337:                        if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
1.135     noro     4338:                                nz = compress_array(svect,cvect,col);
1.146   ! noro     4339:                                removecont_array((P *)cvect,nz);
1.135     noro     4340:                                expand_array(svect,cvect,nz);
1.134     noro     4341:                                hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   4342:                        }
1.107     noro     4343:                }
1.133     noro     4344:        }
1.135     noro     4345:        nz = compress_array(svect,cvect,col);
1.146   ! noro     4346:        removecont_array((P *)cvect,nz);
1.135     noro     4347:        expand_array(svect,cvect,nz);
1.133     noro     4348:        if ( DP_Print ) {
                   4349:                fprintf(asir_out,"-"); fflush(asir_out);
1.107     noro     4350:        }
                   4351:        return maxrs;
                   4352: }
                   4353:
1.76      noro     4354: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.65      noro     4355: {
1.67      noro     4356:        int i,j,k,len,pos,prev;
1.66      noro     4357:        UINT c,c1,c2,c3,up,lo,dmy;
1.67      noro     4358:        IndArray ivect;
                   4359:        unsigned char *ivc;
                   4360:        unsigned short *ivs;
                   4361:        unsigned int *ivi;
1.65      noro     4362:        NDV redv;
1.67      noro     4363:        NMV mr;
1.65      noro     4364:        NODE rp;
1.76      noro     4365:        int maxrs;
1.65      noro     4366:
1.76      noro     4367:        maxrs = 0;
1.74      noro     4368:        for ( i = 0; i < nred; i++ ) {
1.65      noro     4369:                ivect = imat[i];
1.67      noro     4370:                k = ivect->head; svect[k] %= m;
1.65      noro     4371:                if ( c = svect[k] ) {
1.76      noro     4372:                        maxrs = MAX(maxrs,rp0[i]->sugar);
1.74      noro     4373:                        c = m-c; redv = nd_ps[rp0[i]->index];
1.67      noro     4374:                        len = LEN(redv); mr = BDY(redv);
                   4375:                        svect[k] = 0; prev = k;
                   4376:                        switch ( ivect->width ) {
                   4377:                                case 1:
                   4378:                                        ivc = ivect->index.c;
                   4379:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   4380:                                                pos = prev+ivc[j]; c1 = CM(mr); c2 = svect[pos];
                   4381:                                                prev = pos;
                   4382:                                                DMA(c1,c,c2,up,lo);
                   4383:                                                if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   4384:                                                } else svect[pos] = lo;
                   4385:                                        }
                   4386:                                        break;
                   4387:                                case 2:
                   4388:                                        ivs = ivect->index.s;
                   4389:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   4390:                                                pos = prev+ivs[j]; c1 = CM(mr); c2 = svect[pos];
                   4391:                                                prev = pos;
                   4392:                                                DMA(c1,c,c2,up,lo);
                   4393:                                                if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   4394:                                                } else svect[pos] = lo;
                   4395:                                        }
                   4396:                                        break;
                   4397:                                case 4:
                   4398:                                        ivi = ivect->index.i;
                   4399:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   4400:                                                pos = prev+ivi[j]; c1 = CM(mr); c2 = svect[pos];
                   4401:                                                prev = pos;
                   4402:                                                DMA(c1,c,c2,up,lo);
                   4403:                                                if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   4404:                                                } else svect[pos] = lo;
                   4405:                                        }
                   4406:                                        break;
1.65      noro     4407:                        }
                   4408:                }
                   4409:        }
1.66      noro     4410:        for ( i = 0; i < col; i++ )
                   4411:                if ( svect[i] >= (UINT)m ) svect[i] %= m;
1.76      noro     4412:        return maxrs;
1.65      noro     4413: }
                   4414:
1.76      noro     4415: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.72      noro     4416: {
                   4417:        int i,j,k,len,pos,prev;
                   4418:        UINT c,c1,c2,c3,up,lo,dmy;
                   4419:        IndArray ivect;
                   4420:        unsigned char *ivc;
                   4421:        unsigned short *ivs;
                   4422:        unsigned int *ivi;
                   4423:        NDV redv;
                   4424:        NMV mr;
                   4425:        NODE rp;
1.76      noro     4426:        int maxrs;
1.72      noro     4427:
1.76      noro     4428:        maxrs = 0;
1.74      noro     4429:        for ( i = 0; i < nred; i++ ) {
1.72      noro     4430:                ivect = imat[i];
                   4431:                k = ivect->head; svect[k] %= m;
                   4432:                if ( c = svect[k] ) {
1.76      noro     4433:                        maxrs = MAX(maxrs,rp0[i]->sugar);
1.74      noro     4434:                        c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
1.72      noro     4435:                        len = LEN(redv); mr = BDY(redv);
                   4436:                        svect[k] = 0; prev = k;
                   4437:                        switch ( ivect->width ) {
                   4438:                                case 1:
                   4439:                                        ivc = ivect->index.c;
                   4440:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   4441:                                                pos = prev+ivc[j]; prev = pos;
                   4442:                                                svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   4443:                                        }
                   4444:                                        break;
                   4445:                                case 2:
                   4446:                                        ivs = ivect->index.s;
                   4447:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   4448:                                                pos = prev+ivs[j]; prev = pos;
                   4449:                                                svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   4450:                                        }
                   4451:                                        break;
                   4452:                                case 4:
                   4453:                                        ivi = ivect->index.i;
                   4454:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   4455:                                                pos = prev+ivi[j]; prev = pos;
                   4456:                                                svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   4457:                                        }
                   4458:                                        break;
                   4459:                        }
                   4460:                }
                   4461:        }
1.76      noro     4462:        return maxrs;
1.72      noro     4463: }
                   4464:
1.65      noro     4465: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   4466: {
                   4467:        int j,k,len;
                   4468:        UINT *p;
                   4469:        UINT c;
                   4470:        NDV r;
                   4471:        NMV mr0,mr;
                   4472:
                   4473:        for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   4474:        if ( !len ) return 0;
                   4475:        else {
1.74      noro     4476:                mr0 = (NMV)GC_malloc_atomic_ignore_off_page(nmv_adv*len);
1.103     noro     4477: #if 0
1.74      noro     4478:                ndv_alloc += nmv_adv*len;
1.103     noro     4479: #endif
1.65      noro     4480:                mr = mr0;
                   4481:                p = s0vect;
                   4482:                for ( j = k = 0; j < col; j++, p += nd_wpd )
                   4483:                        if ( !rhead[j] ) {
                   4484:                                if ( c = vect[k++] ) {
                   4485:                                        ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   4486:                                }
                   4487:                        }
                   4488:                MKNDV(nd_nvar,mr0,len,r);
                   4489:                return r;
                   4490:        }
                   4491: }
                   4492:
1.129     noro     4493: /* for preprocessed vector */
                   4494:
1.113     noro     4495: NDV vect_to_ndv_q(Q *vect,int spcol,int col,int *rhead,UINT *s0vect)
1.107     noro     4496: {
                   4497:        int j,k,len;
                   4498:        UINT *p;
1.113     noro     4499:        Q c;
1.107     noro     4500:        NDV r;
                   4501:        NMV mr0,mr;
                   4502:
                   4503:        for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   4504:        if ( !len ) return 0;
                   4505:        else {
1.109     noro     4506:                mr0 = (NMV)GC_malloc(nmv_adv*len);
1.107     noro     4507: #if 0
                   4508:                ndv_alloc += nmv_adv*len;
                   4509: #endif
                   4510:                mr = mr0;
                   4511:                p = s0vect;
                   4512:                for ( j = k = 0; j < col; j++, p += nd_wpd )
                   4513:                        if ( !rhead[j] ) {
                   4514:                                if ( c = vect[k++] ) {
1.113     noro     4515:                                        if ( DN(c) )
                   4516:                                                error("afo");
                   4517:                                        ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
1.107     noro     4518:                                }
                   4519:                        }
                   4520:                MKNDV(nd_nvar,mr0,len,r);
                   4521:                return r;
                   4522:        }
                   4523: }
                   4524:
1.129     noro     4525: /* for plain vector */
                   4526:
                   4527: NDV plain_vect_to_ndv_q(Q *vect,int col,UINT *s0vect)
                   4528: {
                   4529:        int j,k,len;
                   4530:        UINT *p;
                   4531:        Q c;
                   4532:        NDV r;
                   4533:        NMV mr0,mr;
                   4534:
                   4535:        for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   4536:        if ( !len ) return 0;
                   4537:        else {
                   4538:                mr0 = (NMV)GC_malloc(nmv_adv*len);
                   4539: #if 0
                   4540:                ndv_alloc += nmv_adv*len;
                   4541: #endif
                   4542:                mr = mr0;
                   4543:                p = s0vect;
                   4544:                for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   4545:                        if ( c = vect[k] ) {
                   4546:                                if ( DN(c) )
                   4547:                                        error("afo");
                   4548:                                ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
                   4549:                        }
                   4550:                MKNDV(nd_nvar,mr0,len,r);
                   4551:                return r;
                   4552:        }
                   4553: }
                   4554:
1.133     noro     4555: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
1.65      noro     4556: {
                   4557:        ND_pairs t;
                   4558:        NODE sp0,sp;
                   4559:        int stat;
                   4560:        ND spol;
                   4561:
                   4562:        for ( t = l; t; t = NEXT(t) ) {
1.133     noro     4563:                stat = nd_sp(m,trace,t,&spol);
1.65      noro     4564:                if ( !stat ) return 0;
                   4565:                if ( spol ) {
                   4566:                        add_pbucket_symbolic(bucket,spol);
                   4567:                }
                   4568:        }
1.68      noro     4569:        return 1;
1.65      noro     4570: }
                   4571:
1.133     noro     4572: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
1.65      noro     4573: {
                   4574:        NODE rp0,rp;
                   4575:        NM mul,head,s0,s;
1.76      noro     4576:        int index,col,i,sugar;
1.65      noro     4577:        RHist h;
                   4578:        UINT *s0v,*p;
                   4579:        NM_ind_pair pair;
                   4580:        ND red;
1.133     noro     4581:        NDV *ps;
1.65      noro     4582:
                   4583:        s0 = 0; rp0 = 0; col = 0;
1.133     noro     4584:        ps = trace?nd_ps_trace:nd_ps;
1.65      noro     4585:        while ( 1 ) {
                   4586:                head = remove_head_pbucket_symbolic(bucket);
                   4587:                if ( !head ) break;
                   4588:                if ( !s0 ) s0 = head;
                   4589:                else NEXT(s) = head;
                   4590:                s = head;
                   4591:                index = ndl_find_reducer(DL(head));
                   4592:                if ( index >= 0 ) {
                   4593:                        h = nd_psh[index];
                   4594:                        NEWNM(mul);
                   4595:                        ndl_sub(DL(head),DL(h),DL(mul));
                   4596:                        if ( ndl_check_bound2(index,DL(mul)) ) return 0;
1.133     noro     4597:                        sugar = TD(DL(mul))+SG(ps[index]);
1.76      noro     4598:                        MKNM_ind_pair(pair,mul,index,sugar);
1.133     noro     4599:                        red = ndv_mul_nm_symbolic(mul,ps[index]);
1.65      noro     4600:                        add_pbucket_symbolic(bucket,nd_remove_head(red));
                   4601:                        NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   4602:                }
                   4603:                col++;
                   4604:        }
1.72      noro     4605:        if ( rp0 ) NEXT(rp) = 0;
                   4606:        NEXT(s) = 0;
1.65      noro     4607:        s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   4608:        for ( i = 0, p = s0v, s = s0; i < col;
                   4609:                i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   4610:        *s0vect = s0v;
                   4611:        *r = rp0;
                   4612:        return col;
                   4613: }
                   4614:
1.69      noro     4615: NODE nd_f4(int m)
                   4616: {
                   4617:        int i,nh,stat,index;
                   4618:        NODE r,g;
                   4619:        ND_pairs d,l,t;
                   4620:        ND spol,red;
                   4621:        NDV nf,redv;
                   4622:        NM s0,s;
1.84      noro     4623:        NODE rp0,srp0,nflist;
1.69      noro     4624:        int nsp,nred,col,rank,len,k,j,a;
                   4625:        UINT c;
1.74      noro     4626:        UINT **spmat;
1.69      noro     4627:        UINT *s0vect,*svect,*p,*v;
                   4628:        int *colstat;
                   4629:        IndArray *imat;
                   4630:        int *rhead;
                   4631:        int spcol,sprow;
                   4632:        int sugar;
                   4633:        PGeoBucket bucket;
                   4634:        struct oEGT eg0,eg1,eg_f4;
                   4635:
1.103     noro     4636: #if 0
1.74      noro     4637:        ndv_alloc = 0;
1.103     noro     4638: #endif
1.69      noro     4639:        g = 0; d = 0;
                   4640:        for ( i = 0; i < nd_psn; i++ ) {
                   4641:                d = update_pairs(d,g,i);
                   4642:                g = update_base(g,i);
                   4643:        }
                   4644:        while ( d ) {
                   4645:                get_eg(&eg0);
                   4646:                l = nd_minsugarp(d,&d);
                   4647:                sugar = SG(l);
                   4648:                bucket = create_pbucket();
1.133     noro     4649:                stat = nd_sp_f4(m,0,l,bucket);
1.69      noro     4650:                if ( !stat ) {
                   4651:                        for ( t = l; NEXT(t); t = NEXT(t) );
                   4652:                        NEXT(t) = d; d = l;
1.103     noro     4653:                        d = nd_reconstruct(0,d);
1.69      noro     4654:                        continue;
                   4655:                }
1.86      noro     4656:                if ( bucket->m < 0 ) continue;
1.133     noro     4657:                col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
1.69      noro     4658:                if ( !col ) {
                   4659:                        for ( t = l; NEXT(t); t = NEXT(t) );
                   4660:                        NEXT(t) = d; d = l;
1.103     noro     4661:                        d = nd_reconstruct(0,d);
1.69      noro     4662:                        continue;
                   4663:                }
                   4664:                get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
1.74      noro     4665:                if ( DP_Print )
                   4666:                        fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   4667:                                sugar,eg_f4.exectime+eg_f4.gctime);
                   4668:                if ( 1 )
1.133     noro     4669:                        nflist = nd_f4_red(m,l,0,s0vect,col,rp0,0);
1.74      noro     4670:                else
1.133     noro     4671:                        nflist = nd_f4_red_dist(m,l,s0vect,col,rp0,0);
1.69      noro     4672:                /* adding new bases */
1.74      noro     4673:                for ( r = nflist; r; r = NEXT(r) ) {
                   4674:                        nf = (NDV)BDY(r);
1.69      noro     4675:                        ndv_removecont(m,nf);
1.129     noro     4676:                        if ( !m && nd_nalg ) {
                   4677:                                ND nf1;
                   4678:
                   4679:                                nf1 = ndvtond(m,nf);
                   4680:                                nd_monic(0,&nf1);
                   4681:                                nd_removecont(m,nf1);
                   4682:                                nf = ndtondv(m,nf1);
                   4683:                        }
1.77      noro     4684:                        nh = ndv_newps(m,nf,0);
1.69      noro     4685:                        d = update_pairs(d,g,nh);
                   4686:                        g = update_base(g,nh);
                   4687:                }
                   4688:        }
                   4689:        for ( r = g; r; r = NEXT(r) ) BDY(r) = (pointer)nd_ps[(int)BDY(r)];
1.103     noro     4690: #if 0
1.74      noro     4691:        fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     4692: #endif
1.69      noro     4693:        return g;
                   4694: }
1.74      noro     4695:
1.133     noro     4696: NODE nd_f4_trace(int m)
                   4697: {
                   4698:        int i,nh,stat,index;
                   4699:        NODE r,g;
                   4700:        ND_pairs d,l,l0,t;
                   4701:        ND spol,red;
                   4702:        NDV nf,redv,nfqv,nfv;
                   4703:        NM s0,s;
                   4704:        NODE rp0,srp0,nflist;
                   4705:        int nsp,nred,col,rank,len,k,j,a;
                   4706:        UINT c;
                   4707:        UINT **spmat;
                   4708:        UINT *s0vect,*svect,*p,*v;
                   4709:        int *colstat;
                   4710:        IndArray *imat;
                   4711:        int *rhead;
                   4712:        int spcol,sprow;
                   4713:        int sugar;
                   4714:        PGeoBucket bucket;
                   4715:        struct oEGT eg0,eg1,eg_f4;
                   4716:
                   4717:        g = 0; d = 0;
                   4718:        for ( i = 0; i < nd_psn; i++ ) {
                   4719:                d = update_pairs(d,g,i);
                   4720:                g = update_base(g,i);
                   4721:        }
                   4722:        while ( d ) {
                   4723:                get_eg(&eg0);
                   4724:                l = nd_minsugarp(d,&d);
                   4725:                sugar = SG(l);
                   4726:                bucket = create_pbucket();
                   4727:                stat = nd_sp_f4(m,0,l,bucket);
                   4728:                if ( !stat ) {
                   4729:                        for ( t = l; NEXT(t); t = NEXT(t) );
                   4730:                        NEXT(t) = d; d = l;
                   4731:                        d = nd_reconstruct(1,d);
                   4732:                        continue;
                   4733:                }
                   4734:                if ( bucket->m < 0 ) continue;
                   4735:                col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   4736:                if ( !col ) {
                   4737:                        for ( t = l; NEXT(t); t = NEXT(t) );
                   4738:                        NEXT(t) = d; d = l;
                   4739:                        d = nd_reconstruct(1,d);
                   4740:                        continue;
                   4741:                }
                   4742:                get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   4743:                if ( DP_Print )
                   4744:                        fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   4745:                                sugar,eg_f4.exectime+eg_f4.gctime);
                   4746:                nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   4747:                if ( !l0 ) continue;
                   4748:                l = l0;
                   4749:
                   4750:                /* over Q */
                   4751:                bucket = create_pbucket();
                   4752:                stat = nd_sp_f4(0,1,l,bucket);
                   4753:                if ( !stat ) {
                   4754:                        for ( t = l; NEXT(t); t = NEXT(t) );
                   4755:                        NEXT(t) = d; d = l;
                   4756:                        d = nd_reconstruct(1,d);
                   4757:                        continue;
                   4758:                }
                   4759:                if ( bucket->m < 0 ) continue;
                   4760:                col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   4761:                if ( !col ) {
                   4762:                        for ( t = l; NEXT(t); t = NEXT(t) );
                   4763:                        NEXT(t) = d; d = l;
                   4764:                        d = nd_reconstruct(1,d);
                   4765:                        continue;
                   4766:                }
                   4767:                nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   4768:                /* adding new bases */
                   4769:                for ( r = nflist; r; r = NEXT(r) ) {
                   4770:                        nfqv = (NDV)BDY(r);
                   4771:                        ndv_removecont(0,nfqv);
                   4772:                        if ( !rem(NM(HCQ(nfqv)),m) ) return 0;
                   4773:                        if ( nd_nalg ) {
                   4774:                                ND nf1;
                   4775:
                   4776:                                nf1 = ndvtond(m,nfqv);
                   4777:                                nd_monic(0,&nf1);
                   4778:                                nd_removecont(0,nf1);
                   4779:                                nfqv = ndtondv(0,nf1); nd_free(nf1);
                   4780:                        }
                   4781:                        nfv = ndv_dup(0,nfqv);
                   4782:                        ndv_mod(m,nfv);
                   4783:                        ndv_removecont(m,nfv);
                   4784:                        nh = ndv_newps(0,nfv,nfqv);
                   4785:                        d = update_pairs(d,g,nh);
                   4786:                        g = update_base(g,nh);
                   4787:                }
                   4788:        }
                   4789:        for ( r = g; r; r = NEXT(r) ) BDY(r) = (pointer)nd_ps_trace[(int)BDY(r)];
                   4790: #if 0
                   4791:        fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   4792: #endif
                   4793:        return g;
                   4794: }
                   4795:
                   4796: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
1.63      noro     4797: {
1.67      noro     4798:        IndArray *imat;
1.106     noro     4799:        int nsp,nred,i;
1.65      noro     4800:        int *rhead;
1.106     noro     4801:        NODE r0,rp;
1.74      noro     4802:        ND_pairs sp;
                   4803:        NM_ind_pair *rvect;
1.63      noro     4804:
1.74      noro     4805:        for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
1.106     noro     4806:        nred = length(rp0);
1.74      noro     4807:        imat = (IndArray *)ALLOCA(nred*sizeof(IndArray));
                   4808:        rhead = (int *)ALLOCA(col*sizeof(int));
                   4809:        for ( i = 0; i < col; i++ ) rhead[i] = 0;
1.63      noro     4810:
1.74      noro     4811:        /* construction of index arrays */
                   4812:        rvect = (NM_ind_pair *)ALLOCA(nred*sizeof(NM_ind_pair));
                   4813:        for ( rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   4814:                rvect[i] = (NM_ind_pair)BDY(rp);
                   4815:                imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rvect[i]);
                   4816:                rhead[imat[i]->head] = 1;
                   4817:        }
1.107     noro     4818:        if ( m )
1.133     noro     4819:                r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
1.107     noro     4820:        else
1.133     noro     4821:                r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
1.106     noro     4822:        return r0;
                   4823: }
1.74      noro     4824:
1.106     noro     4825: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
1.133     noro     4826:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
1.106     noro     4827: {
                   4828:        int spcol,sprow,a;
                   4829:        int i,j,k,l,rank;
                   4830:        NODE r0,r;
                   4831:        ND_pairs sp;
                   4832:        ND spol;
                   4833:        int **spmat;
                   4834:        UINT *svect,*v;
                   4835:        int *colstat;
                   4836:        struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   4837:        int maxrs;
                   4838:        int *spsugar;
1.133     noro     4839:        ND_pairs *spactive;
1.106     noro     4840:
                   4841:        spcol = col-nred;
                   4842:        get_eg(&eg0);
1.74      noro     4843:        /* elimination (1st step) */
                   4844:        spmat = (int **)ALLOCA(nsp*sizeof(UINT *));
                   4845:        svect = (UINT *)ALLOCA(col*sizeof(UINT));
1.76      noro     4846:        spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
1.133     noro     4847:        spactive = !nz?0:(ND_pairs *)ALLOCA(nsp*sizeof(ND_pairs));
1.74      noro     4848:        for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   4849:                nd_sp(m,0,sp,&spol);
1.75      noro     4850:                if ( !spol ) continue;
1.74      noro     4851:                nd_to_vect(m,s0vect,col,spol,svect);
1.76      noro     4852:                if ( m == -1 )
                   4853:                        maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   4854:                else
                   4855:                        maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
1.74      noro     4856:                for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   4857:                if ( i < col ) {
                   4858:                        spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   4859:                        for ( j = k = 0; j < col; j++ )
                   4860:                                if ( !rhead[j] ) v[k++] = svect[j];
1.76      noro     4861:                        spsugar[sprow] = MAX(maxrs,SG(spol));
1.133     noro     4862:                        if ( nz )
                   4863:                        spactive[sprow] = sp;
1.74      noro     4864:                        sprow++;
                   4865:                }
1.76      noro     4866:                nd_free(spol);
1.74      noro     4867:        }
1.80      noro     4868:        get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   4869:        if ( DP_Print ) {
                   4870:                fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   4871:                fflush(asir_out);
                   4872:        }
1.74      noro     4873:        /* free index arrays */
                   4874:        for ( i = 0; i < nred; i++ ) GC_free(imat[i]->index.c);
                   4875:
                   4876:        /* elimination (2nd step) */
                   4877:        colstat = (int *)ALLOCA(spcol*sizeof(int));
                   4878:        if ( m == -1 )
1.76      noro     4879:                rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
1.74      noro     4880:        else
1.133     noro     4881:                rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
1.74      noro     4882:        r0 = 0;
                   4883:        for ( i = 0; i < rank; i++ ) {
                   4884:                NEXTNODE(r0,r); BDY(r) =
                   4885:                        (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
1.76      noro     4886:                SG((NDV)BDY(r)) = spsugar[i];
1.74      noro     4887:                GC_free(spmat[i]);
                   4888:        }
1.109     noro     4889:        if ( r0 ) NEXT(r) = 0;
1.133     noro     4890:
1.74      noro     4891:        for ( ; i < sprow; i++ ) GC_free(spmat[i]);
1.80      noro     4892:        get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   4893:        init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
1.74      noro     4894:        if ( DP_Print ) {
1.80      noro     4895:                fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
1.74      noro     4896:                fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   4897:                        nsp,nred,sprow,spcol,rank);
                   4898:                fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
1.63      noro     4899:        }
1.133     noro     4900:        if ( nz ) {
                   4901:                for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   4902:                if ( rank > 0 ) {
                   4903:                        NEXT(spactive[rank-1]) = 0;
                   4904:                        *nz = spactive[0];
                   4905:                } else
                   4906:                        *nz = 0;
                   4907:        }
1.74      noro     4908:        return r0;
                   4909: }
                   4910:
1.133     noro     4911: #if 1
                   4912: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
1.107     noro     4913:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   4914: {
                   4915:        int spcol,sprow,a;
                   4916:        int i,j,k,l,rank;
                   4917:        NODE r0,r;
                   4918:        ND_pairs sp;
                   4919:        ND spol;
1.113     noro     4920:        Q **spmat;
                   4921:        Q *svect,*v;
1.107     noro     4922:        int *colstat;
                   4923:        struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   4924:        int maxrs;
                   4925:        int *spsugar;
1.137     noro     4926:        pointer *w;
1.107     noro     4927:
                   4928:        spcol = col-nred;
                   4929:        get_eg(&eg0);
                   4930:        /* elimination (1st step) */
1.113     noro     4931:        spmat = (Q **)ALLOCA(nsp*sizeof(Q *));
                   4932:        svect = (Q *)ALLOCA(col*sizeof(Q));
                   4933:        spsugar = (int *)ALLOCA(nsp*sizeof(Q));
1.107     noro     4934:        for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
1.133     noro     4935:                nd_sp(0,trace,sp,&spol);
1.107     noro     4936:                if ( !spol ) continue;
1.113     noro     4937:                nd_to_vect_q(s0vect,col,spol,svect);
1.133     noro     4938:                maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
1.107     noro     4939:                for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   4940:                if ( i < col ) {
1.113     noro     4941:                        spmat[sprow] = v = (Q *)MALLOC(spcol*sizeof(Q));
1.107     noro     4942:                        for ( j = k = 0; j < col; j++ )
                   4943:                                if ( !rhead[j] ) v[k++] = svect[j];
                   4944:                        spsugar[sprow] = MAX(maxrs,SG(spol));
                   4945:                        sprow++;
                   4946:                }
1.109     noro     4947: /*             nd_free(spol); */
1.107     noro     4948:        }
                   4949:        get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   4950:        if ( DP_Print ) {
                   4951:                fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   4952:                fflush(asir_out);
                   4953:        }
                   4954:        /* free index arrays */
1.109     noro     4955: /*     for ( i = 0; i < nred; i++ ) GC_free(imat[i]->index.c); */
1.107     noro     4956:
                   4957:        /* elimination (2nd step) */
                   4958:        colstat = (int *)ALLOCA(spcol*sizeof(int));
1.113     noro     4959:        rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
1.137     noro     4960:        w = (pointer *)ALLOCA(rank*sizeof(pointer));
                   4961:        for ( i = 0; i < rank; i++ ) {
                   4962:                w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   4963:                SG((NDV)w[rank-i-1]) = spsugar[i];
                   4964: /*             GC_free(spmat[i]); */
                   4965:        }
1.138     noro     4966: #if 0
1.137     noro     4967:        qsort(w,rank,sizeof(NDV),
                   4968:                (int (*)(const void *,const void *))ndv_compare);
                   4969: #endif
1.107     noro     4970:        r0 = 0;
                   4971:        for ( i = 0; i < rank; i++ ) {
1.137     noro     4972:                NEXTNODE(r0,r); BDY(r) = w[i];
1.107     noro     4973:        }
1.137     noro     4974:        if ( r0 ) NEXT(r) = 0;
1.135     noro     4975:
1.109     noro     4976: /*     for ( ; i < sprow; i++ ) GC_free(spmat[i]); */
1.107     noro     4977:        get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   4978:        init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   4979:        if ( DP_Print ) {
                   4980:                fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   4981:                fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   4982:                        nsp,nred,sprow,spcol,rank);
                   4983:                fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   4984:        }
                   4985:        return r0;
                   4986: }
1.129     noro     4987: #else
                   4988: void printm(Q **mat,int row,int col)
                   4989: {
                   4990:        int i,j;
                   4991:        printf("[");
                   4992:        for ( i = 0; i < row; i++ ) {
                   4993:                for ( j = 0; j < col; j++ ) {
                   4994:                        printexpr(CO,mat[i][j]); printf(" ");
                   4995:                }
                   4996:                printf("]\n");
                   4997:        }
                   4998: }
                   4999:
                   5000: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   5001:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   5002: {
                   5003:        int row,a;
                   5004:        int i,j,rank;
                   5005:        NODE r0,r;
                   5006:        ND_pairs sp;
                   5007:        ND spol;
                   5008:        Q **mat;
                   5009:        int *colstat;
                   5010:        int *sugar;
                   5011:
                   5012:        row = nsp+nred;
                   5013:        /* make the matrix */
                   5014:        mat = (Q **)ALLOCA(row*sizeof(Q *));
                   5015:        sugar = (int *)ALLOCA(row*sizeof(int));
                   5016:        for ( row = a = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   5017:                nd_sp(0,0,sp,&spol);
                   5018:                if ( !spol ) continue;
                   5019:                mat[row] = (Q *)MALLOC(col*sizeof(Q));
                   5020:                nd_to_vect_q(s0vect,col,spol,mat[row]);
                   5021:                sugar[row] = SG(spol);
                   5022:                row++;
                   5023:        }
                   5024:        for ( i = 0; i < nred; i++, row++ ) {
                   5025:                mat[row] = nm_ind_pair_to_vect(0,s0vect,col,rvect[i]);
                   5026:                sugar[row] = rvect[i]->sugar;
                   5027:        }
                   5028:        /* elimination */
                   5029:        colstat = (int *)ALLOCA(col*sizeof(int));
                   5030:        rank = nd_gauss_elim_q(mat,sugar,row,col,colstat);
                   5031:        r0 = 0;
                   5032:        for ( i = 0; i < rank; i++ ) {
                   5033:                for ( j = 0; j < col; j++ ) if ( mat[i][j] ) break;
                   5034:                if ( j == col ) error("nd_f4_red_q_main : cannot happen");
                   5035:                if ( rhead[j] ) continue;
                   5036:                NEXTNODE(r0,r); BDY(r) =
                   5037:                        (pointer)plain_vect_to_ndv_q(mat[i],col,s0vect);
                   5038:                SG((NDV)BDY(r)) = sugar[i];
                   5039:        }
                   5040:        if ( r0 ) NEXT(r) = 0;
                   5041:        printf("\n");
                   5042:        return r0;
                   5043: }
                   5044: #endif
1.107     noro     5045:
1.74      noro     5046: FILE *nd_write,*nd_read;
                   5047:
                   5048: void nd_send_int(int a) {
                   5049:        write_int(nd_write,&a);
                   5050: }
                   5051:
                   5052: void nd_send_intarray(int *p,int len) {
                   5053:        write_intarray(nd_write,p,len);
                   5054: }
                   5055:
                   5056: int nd_recv_int() {
                   5057:        int a;
                   5058:
                   5059:        read_int(nd_read,&a);
                   5060:        return a;
                   5061: }
                   5062:
                   5063: void nd_recv_intarray(int *p,int len) {
                   5064:        read_intarray(nd_read,p,len);
                   5065: }
                   5066:
                   5067: void nd_send_ndv(NDV p) {
                   5068:        int len,i;
                   5069:        NMV m;
                   5070:
                   5071:        if ( !p ) nd_send_int(0);
                   5072:        else {
                   5073:                len = LEN(p);
                   5074:                nd_send_int(len);
                   5075:                m = BDY(p);
                   5076:                for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   5077:                        nd_send_int(CM(m));
                   5078:                        nd_send_intarray(DL(m),nd_wpd);
1.68      noro     5079:                }
1.74      noro     5080:        }
                   5081: }
                   5082:
                   5083: void nd_send_nd(ND p) {
                   5084:        int len,i;
                   5085:        NM m;
                   5086:
                   5087:        if ( !p ) nd_send_int(0);
                   5088:        else {
                   5089:                len = LEN(p);
                   5090:                nd_send_int(len);
                   5091:                m = BDY(p);
                   5092:                for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   5093:                        nd_send_int(CM(m));
                   5094:                        nd_send_intarray(DL(m),nd_wpd);
1.65      noro     5095:                }
1.74      noro     5096:        }
                   5097: }
1.65      noro     5098:
1.74      noro     5099: NDV nd_recv_ndv()
                   5100: {
                   5101:        int len,i;
                   5102:        NMV m,m0;
                   5103:        NDV r;
1.65      noro     5104:
1.74      noro     5105:        len = nd_recv_int();
                   5106:        if ( !len ) return 0;
                   5107:        else {
                   5108:                m0 = m = (NMV)GC_malloc_atomic_ignore_off_page(nmv_adv*len);
1.103     noro     5109: #if 0
1.74      noro     5110:                ndv_alloc += len*nmv_adv;
1.103     noro     5111: #endif
1.74      noro     5112:                for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   5113:                        CM(m) = nd_recv_int();
                   5114:                        nd_recv_intarray(DL(m),nd_wpd);
1.71      noro     5115:                }
1.74      noro     5116:                MKNDV(nd_nvar,m0,len,r);
                   5117:                return r;
                   5118:        }
                   5119: }
1.65      noro     5120:
1.74      noro     5121: int ox_exec_f4_red(Q proc)
                   5122: {
                   5123:        Obj obj;
                   5124:        STRING fname;
                   5125:        NODE arg;
                   5126:        int s;
                   5127:        extern int ox_need_conv,ox_file_io;
                   5128:
                   5129:        MKSTR(fname,"nd_exec_f4_red");
                   5130:        arg = mknode(2,proc,fname);
                   5131:        Pox_cmo_rpc(arg,&obj);
                   5132:        s = get_ox_server_id(QTOS(proc));
                   5133:        nd_write = iofp[s].out;
                   5134:        nd_read = iofp[s].in;
                   5135:        ox_need_conv = ox_file_io = 0;
                   5136:        return s;
                   5137: }
                   5138:
1.133     noro     5139: NODE nd_f4_red_dist(int m,ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
1.74      noro     5140: {
                   5141:        int nsp,nred;
                   5142:        int i,rank,s;
                   5143:        NODE rp,r0,r;
                   5144:        ND_pairs sp;
                   5145:        NM_ind_pair pair;
                   5146:        NMV nmv;
                   5147:        NM nm;
                   5148:        NDV nf;
                   5149:        Obj proc,dmy;
                   5150:
                   5151:        ox_launch_main(0,0,&proc);
                   5152:        s = ox_exec_f4_red((Q)proc);
                   5153:
                   5154:        nd_send_int(m);
                   5155:        nd_send_int(nd_nvar);
                   5156:        nd_send_int(nd_bpe);
                   5157:        nd_send_int(nd_wpd);
                   5158:        nd_send_int(nmv_adv);
                   5159:
1.89      noro     5160:        saveobj(nd_write,dp_current_spec->obj); fflush(nd_write);
1.74      noro     5161:
                   5162:        nd_send_int(nd_psn);
                   5163:        for ( i = 0; i < nd_psn; i++ ) nd_send_ndv(nd_ps[i]);
                   5164:
                   5165:        for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   5166:        nd_send_int(nsp);
                   5167:        for ( i = 0, sp = sp0; i < nsp; i++, sp = NEXT(sp) ) {
                   5168:                nd_send_int(sp->i1); nd_send_int(sp->i2);
                   5169:        }
                   5170:
                   5171:        nd_send_int(col); nd_send_intarray(s0vect,col*nd_wpd);
                   5172:
                   5173:        nred = length(rp0); nd_send_int(nred);
                   5174:        for ( i = 0, rp = rp0; i < nred; i++, rp = NEXT(rp) ) {
                   5175:                pair = (NM_ind_pair)BDY(rp);
                   5176:                nd_send_int(pair->index);
                   5177:                nd_send_intarray(pair->mul->dl,nd_wpd);
                   5178:        }
                   5179:        fflush(nd_write);
                   5180:        rank = nd_recv_int();
                   5181:        fprintf(asir_out,"rank=%d\n",rank);
                   5182:        r0 = 0;
                   5183:        for ( i = 0; i < rank; i++ ) {
                   5184:                nf = nd_recv_ndv();
                   5185:                NEXTNODE(r0,r); BDY(r) = (pointer)nf;
                   5186:        }
                   5187:        Pox_shutdown(mknode(1,proc),&dmy);
                   5188:        return r0;
                   5189: }
                   5190:
                   5191: /* server side */
                   5192:
                   5193: void nd_exec_f4_red_dist()
                   5194: {
                   5195:        int m,i,nsp,col,s0size,nred,spcol,j,k;
                   5196:        NM_ind_pair *rp0;
                   5197:        NDV nf;
                   5198:        UINT *s0vect;
                   5199:        IndArray *imat;
                   5200:        int *rhead;
                   5201:        int **spmat;
                   5202:        UINT *svect,*v;
                   5203:        ND_pairs *sp0;
                   5204:        int *colstat;
                   5205:        int a,sprow,rank;
1.89      noro     5206:        struct order_spec *ord;
1.74      noro     5207:        Obj ordspec;
                   5208:        ND spol;
1.76      noro     5209:        int maxrs;
                   5210:        int *spsugar;
1.74      noro     5211:
                   5212:        nd_read = iofp[0].in;
                   5213:        nd_write = iofp[0].out;
                   5214:        m = nd_recv_int();
                   5215:        nd_nvar = nd_recv_int();
                   5216:        nd_bpe = nd_recv_int();
                   5217:        nd_wpd = nd_recv_int();
                   5218:        nmv_adv = nd_recv_int();
                   5219:
                   5220:        loadobj(nd_read,&ordspec);
1.89      noro     5221:        create_order_spec(0,ordspec,&ord);
                   5222:        nd_init_ord(ord);
1.74      noro     5223:        nd_setup_parameters(nd_nvar,0);
                   5224:
                   5225:        nd_psn = nd_recv_int();
                   5226:        nd_ps = (NDV *)MALLOC(nd_psn*sizeof(NDV));
                   5227:        nd_bound = (UINT **)MALLOC(nd_psn*sizeof(UINT *));
                   5228:        for ( i = 0; i < nd_psn; i++ ) {
                   5229:                nd_ps[i] = nd_recv_ndv();
                   5230:                nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   5231:        }
                   5232:
                   5233:        nsp = nd_recv_int();
                   5234:        sp0 = (ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   5235:        for ( i = 0; i < nsp; i++ ) {
                   5236:                NEWND_pairs(sp0[i]);
                   5237:                sp0[i]->i1 = nd_recv_int(); sp0[i]->i2 = nd_recv_int();
                   5238:                ndl_lcm(HDL(nd_ps[sp0[i]->i1]),HDL(nd_ps[sp0[i]->i2]),LCM(sp0[i]));
                   5239:        }
                   5240:
                   5241:        col = nd_recv_int();
                   5242:        s0size = col*nd_wpd;
                   5243:        s0vect = (UINT *)MALLOC(s0size*sizeof(UINT));
                   5244:        nd_recv_intarray(s0vect,s0size);
                   5245:
                   5246:        nred = nd_recv_int();
                   5247:        rp0 = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   5248:        for ( i = 0; i < nred; i++ ) {
                   5249:                rp0[i] = (NM_ind_pair)MALLOC(sizeof(struct oNM_ind_pair));
                   5250:                rp0[i]->index = nd_recv_int();
                   5251:                rp0[i]->mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   5252:                nd_recv_intarray(rp0[i]->mul->dl,nd_wpd);
                   5253:        }
                   5254:
                   5255:        spcol = col-nred;
                   5256:        imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   5257:        rhead = (int *)MALLOC(col*sizeof(int));
                   5258:        for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   5259:
                   5260:        /* construction of index arrays */
                   5261:        for ( i = 0; i < nred; i++ ) {
                   5262:                imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rp0[i]);
                   5263:                rhead[imat[i]->head] = 1;
                   5264:        }
                   5265:
                   5266:        /* elimination (1st step) */
                   5267:        spmat = (int **)MALLOC(nsp*sizeof(UINT *));
                   5268:        svect = (UINT *)MALLOC(col*sizeof(UINT));
1.76      noro     5269:        spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
1.74      noro     5270:        for ( a = sprow = 0; a < nsp; a++ ) {
                   5271:                nd_sp(m,0,sp0[a],&spol);
1.75      noro     5272:                if ( !spol ) continue;
1.74      noro     5273:                nd_to_vect(m,s0vect,col,spol,svect);
1.76      noro     5274:                if ( m == -1 )
                   5275:                        maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rp0,nred);
                   5276:                else
                   5277:                        maxrs = ndv_reduce_vect(m,svect,col,imat,rp0,nred);
1.74      noro     5278:                for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   5279:                if ( i < col ) {
                   5280:                        spmat[sprow] = v = (UINT *)MALLOC(spcol*sizeof(UINT));
                   5281:                        for ( j = k = 0; j < col; j++ )
                   5282:                                if ( !rhead[j] ) v[k++] = svect[j];
1.76      noro     5283:                        spsugar[sprow] = MAX(maxrs,SG(spol));
1.74      noro     5284:                        sprow++;
                   5285:                }
1.76      noro     5286:                nd_free(spol);
1.74      noro     5287:        }
                   5288:        /* elimination (2nd step) */
                   5289:        colstat = (int *)ALLOCA(spcol*sizeof(int));
                   5290:        if ( m == -1 )
1.76      noro     5291:                rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
1.74      noro     5292:        else
1.133     noro     5293:                rank = nd_gauss_elim_mod(spmat,spsugar,0,sprow,spcol,m,colstat);
1.74      noro     5294:        nd_send_int(rank);
                   5295:        for ( i = 0; i < rank; i++ ) {
                   5296:                nf = vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   5297:                nd_send_ndv(nf);
1.63      noro     5298:        }
1.74      noro     5299:        fflush(nd_write);
1.107     noro     5300: }
                   5301:
1.113     noro     5302: int nd_gauss_elim_q(Q **mat0,int *sugar,int row,int col,int *colstat)
1.107     noro     5303: {
1.109     noro     5304:        int mod,i,j,t,c,rank,rank0,inv;
                   5305:        int *ci,*ri;
1.113     noro     5306:        Q dn;
1.109     noro     5307:        MAT m,nm;
1.108     noro     5308:        int **wmat;
                   5309:
                   5310:        /* XXX */
                   5311:        mod = 99999989;
                   5312:        wmat = (int **)ALLOCA(row*sizeof(int *));
                   5313:        for ( i = 0; i < row; i++ ) {
                   5314:                wmat[i] = (int *)ALLOCA(col*sizeof(int));
                   5315:                for ( j = 0; j < col; j++ ) {
1.113     noro     5316:                        if ( mat0[i][j] ) {
                   5317:                                t = rem(NM(mat0[i][j]),mod);
                   5318:                                if ( SGN(mat0[i][j]) < 0 ) t = mod-t;
                   5319:                                wmat[i][j] = t;
                   5320:                        } else
1.108     noro     5321:                                wmat[i][j] = 0;
                   5322:                }
                   5323:        }
1.133     noro     5324:        rank0 = nd_gauss_elim_mod(wmat,sugar,0,row,col,mod,colstat);
1.109     noro     5325:        NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
1.113     noro     5326:        rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
1.109     noro     5327:        if ( rank != rank0 )
                   5328:                error("afo");
                   5329:        for ( i = 0; i < row; i++ )
                   5330:                for ( j = 0; j < col; j++ )
                   5331:                        mat0[i][j] = 0;
                   5332:        c = col-rank;
                   5333:        for ( i = 0; i < rank; i++ ) {
1.113     noro     5334:                mat0[i][ri[i]] = dn;
1.109     noro     5335:                for ( j = 0; j < c; j++ )
1.113     noro     5336:                        mat0[i][ci[j]] = (Q)BDY(nm)[i][j];
1.109     noro     5337:        }
1.113     noro     5338:        inv = invm(rem(NM(dn),mod),mod);
                   5339:        if ( SGN(dn) < 0 ) inv = mod-inv;
1.109     noro     5340:        for ( i = 0; i < row; i++ )
                   5341:                for ( j = 0; j < col; j++ ) {
1.113     noro     5342:                        if ( mat0[i][j] ) {
                   5343:                                t = rem(NM(mat0[i][j]),mod);
                   5344:                                if ( SGN(mat0[i][j]) < 0 ) t = mod-t;
                   5345:                        } else
1.109     noro     5346:                                t = 0;
                   5347:                        c = dmar(t,inv,0,mod);
                   5348:                        if ( wmat[i][j] != c )
                   5349:                                error("afo");
                   5350:                }
                   5351:        return rank;
1.76      noro     5352: }
                   5353:
1.133     noro     5354: int nd_gauss_elim_mod(int **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
1.76      noro     5355: {
                   5356:        int i,j,k,l,inv,a,rank,s;
                   5357:        unsigned int *t,*pivot,*pk;
                   5358:        unsigned int **mat;
1.133     noro     5359:        ND_pairs pair;
1.76      noro     5360:
                   5361:        mat = (unsigned int **)mat0;
                   5362:        for ( rank = 0, j = 0; j < col; j++ ) {
                   5363:                for ( i = rank; i < row; i++ )
                   5364:                        mat[i][j] %= md;
                   5365:                for ( i = rank; i < row; i++ )
                   5366:                        if ( mat[i][j] )
                   5367:                                break;
                   5368:                if ( i == row ) {
                   5369:                        colstat[j] = 0;
                   5370:                        continue;
                   5371:                } else
                   5372:                        colstat[j] = 1;
                   5373:                if ( i != rank ) {
                   5374:                        t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   5375:                        s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
1.133     noro     5376:                        if ( spactive ) {
                   5377:                                pair = spactive[i]; spactive[i] = spactive[rank];
                   5378:                                spactive[rank] = pair;
                   5379:                        }
1.76      noro     5380:                }
                   5381:                pivot = mat[rank];
                   5382:                s = sugar[rank];
                   5383:                inv = invm(pivot[j],md);
                   5384:                for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   5385:                        if ( *pk ) {
                   5386:                                if ( *pk >= (unsigned int)md )
                   5387:                                        *pk %= md;
                   5388:                                DMAR(*pk,inv,0,md,*pk)
                   5389:                        }
                   5390:                for ( i = rank+1; i < row; i++ ) {
                   5391:                        t = mat[i];
                   5392:                        if ( a = t[j] ) {
                   5393:                                sugar[i] = MAX(sugar[i],s);
                   5394:                                red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   5395:                        }
                   5396:                }
                   5397:                rank++;
                   5398:        }
                   5399:        for ( j = col-1, l = rank-1; j >= 0; j-- )
                   5400:                if ( colstat[j] ) {
                   5401:                        pivot = mat[l];
                   5402:                        s = sugar[l];
                   5403:                        for ( i = 0; i < l; i++ ) {
                   5404:                                t = mat[i];
                   5405:                                t[j] %= md;
                   5406:                                if ( a = t[j] ) {
                   5407:                                        sugar[i] = MAX(sugar[i],s);
                   5408:                                        red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   5409:                                }
                   5410:                        }
                   5411:                        l--;
                   5412:                }
                   5413:        for ( j = 0, l = 0; l < rank; j++ )
                   5414:                if ( colstat[j] ) {
                   5415:                        t = mat[l];
                   5416:                        for ( k = j; k < col; k++ )
                   5417:                                if ( t[k] >= (unsigned int)md )
                   5418:                                        t[k] %= md;
                   5419:                        l++;
                   5420:                }
                   5421:        return rank;
                   5422: }
                   5423:
                   5424: int nd_gauss_elim_sf(int **mat0,int *sugar,int row,int col,int md,int *colstat)
                   5425: {
                   5426:        int i,j,k,l,inv,a,rank,s;
                   5427:        unsigned int *t,*pivot,*pk;
                   5428:        unsigned int **mat;
                   5429:
                   5430:        mat = (unsigned int **)mat0;
                   5431:        for ( rank = 0, j = 0; j < col; j++ ) {
                   5432:                for ( i = rank; i < row; i++ )
                   5433:                        if ( mat[i][j] )
                   5434:                                break;
                   5435:                if ( i == row ) {
                   5436:                        colstat[j] = 0;
                   5437:                        continue;
                   5438:                } else
                   5439:                        colstat[j] = 1;
                   5440:                if ( i != rank ) {
                   5441:                        t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   5442:                        s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   5443:                }
                   5444:                pivot = mat[rank];
                   5445:                s = sugar[rank];
                   5446:                inv = _invsf(pivot[j]);
                   5447:                for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   5448:                        if ( *pk )
                   5449:                                *pk = _mulsf(*pk,inv);
                   5450:                for ( i = rank+1; i < row; i++ ) {
                   5451:                        t = mat[i];
                   5452:                        if ( a = t[j] ) {
                   5453:                                sugar[i] = MAX(sugar[i],s);
                   5454:                                red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   5455:                        }
                   5456:                }
                   5457:                rank++;
                   5458:        }
                   5459:        for ( j = col-1, l = rank-1; j >= 0; j-- )
                   5460:                if ( colstat[j] ) {
                   5461:                        pivot = mat[l];
                   5462:                        s = sugar[l];
                   5463:                        for ( i = 0; i < l; i++ ) {
                   5464:                                t = mat[i];
                   5465:                                if ( a = t[j] ) {
                   5466:                                        sugar[i] = MAX(sugar[i],s);
                   5467:                                        red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   5468:                                }
                   5469:                        }
                   5470:                        l--;
                   5471:                }
                   5472:        return rank;
1.77      noro     5473: }
                   5474:
                   5475: int ndv_ishomo(NDV p)
                   5476: {
                   5477:        NMV m;
                   5478:        int len,h;
                   5479:
                   5480:        if ( !p ) return 1;
                   5481:        len = LEN(p);
                   5482:        m = BDY(p);
                   5483:        h = TD(DL(m));
                   5484:        NMV_ADV(m);
                   5485:        for ( len--; len; len--, NMV_ADV(m) )
                   5486:                if ( TD(DL(m)) != h ) return 0;
                   5487:        return 1;
                   5488: }
                   5489:
                   5490: void ndv_save(NDV p,int index)
                   5491: {
                   5492:        FILE *s;
                   5493:        char name[BUFSIZ];
                   5494:        short id;
                   5495:        int nv,sugar,len,n,i,td,e,j;
                   5496:        NMV m;
                   5497:        unsigned int *dl;
                   5498:
                   5499:        sprintf(name,"%s/%d",Demand,index);
                   5500:        s = fopen(name,"w");
                   5501:        savevl(s,0);
                   5502:        if ( !p ) {
                   5503:                saveobj(s,0);
                   5504:                return;
                   5505:        }
                   5506:        id = O_DP;
                   5507:        nv = NV(p);
                   5508:        sugar = SG(p);
                   5509:        len = LEN(p);
                   5510:        write_short(s,&id); write_int(s,&nv); write_int(s,&sugar);
                   5511:        write_int(s,&len);
                   5512:
                   5513:        for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.113     noro     5514:                saveobj(s,(Obj)CQ(m));
1.77      noro     5515:                dl = DL(m);
                   5516:                td = TD(dl);
                   5517:                write_int(s,&td);
                   5518:                for ( j = 0; j < nv; j++ ) {
                   5519:                        e = GET_EXP(dl,j);
                   5520:                        write_int(s,&e);
                   5521:                }
                   5522:        }
                   5523:        fclose(s);
                   5524: }
                   5525:
                   5526: NDV ndv_load(int index)
                   5527: {
                   5528:        FILE *s;
                   5529:        char name[BUFSIZ];
                   5530:        short id;
                   5531:        int nv,sugar,len,n,i,td,e,j;
                   5532:        NDV d;
                   5533:        NMV m0,m;
                   5534:        unsigned int *dl;
                   5535:        Obj obj;
                   5536:
                   5537:        sprintf(name,"%s/%d",Demand,index);
                   5538:        s = fopen(name,"r");
                   5539:        if ( !s ) return 0;
                   5540:
                   5541:        skipvl(s);
                   5542:        read_short(s,&id);
                   5543:        if ( !id ) return 0;
                   5544:        read_int(s,&nv);
                   5545:        read_int(s,&sugar);
                   5546:        read_int(s,&len);
                   5547:
                   5548:        m0 = m = MALLOC(len*nmv_adv);
                   5549:        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.113     noro     5550:                loadobj(s,&obj); CQ(m) = (Q)obj;
1.77      noro     5551:                dl = DL(m);
                   5552:                ndl_zero(dl);
                   5553:                read_int(s,&td); TD(dl) = td;
                   5554:                for ( j = 0; j < nv; j++ ) {
                   5555:                        read_int(s,&e);
                   5556:                        PUT_EXP(dl,j,e);
                   5557:                }
                   5558:                if ( nd_blockmask ) ndl_weight_mask(dl);
                   5559:        }
                   5560:        fclose(s);
                   5561:        MKNDV(nv,m0,len,d);
                   5562:        SG(d) = sugar;
                   5563:        return d;
1.99      noro     5564: }
                   5565:
1.102     noro     5566: void nd_det(int mod,MAT f,P *rp)
1.99      noro     5567: {
                   5568:        VL fv,tv;
1.102     noro     5569:        int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
1.99      noro     5570:        pointer **m;
1.113     noro     5571:        Q mone;
1.125     noro     5572:        P **w;
1.126     noro     5573:        P mp,r;
1.102     noro     5574:        NDV **dm;
                   5575:        NDV *t,*mi,*mj;
                   5576:        NDV d,s,mij,mjj;
                   5577:        ND u;
                   5578:        NMV nmv;
1.114     noro     5579:        UINT *bound;
1.99      noro     5580:        PGeoBucket bucket;
                   5581:        struct order_spec *ord;
1.126     noro     5582:        Q dq,dt,ds;
                   5583:        N gn,qn,dn0,nm,dn;
1.99      noro     5584:
                   5585:        create_order_spec(0,0,&ord);
                   5586:        nd_init_ord(ord);
                   5587:        get_vars((Obj)f,&fv);
                   5588:        if ( f->row != f->col )
                   5589:                error("nd_det : non-square matrix");
                   5590:        n = f->row;
1.125     noro     5591:        m = f->body;
1.99      noro     5592:        for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
1.125     noro     5593:
                   5594:        if ( !nvar ) {
                   5595:                if ( !mod )
                   5596:                        detp(CO,(P **)m,n,rp);
                   5597:                else {
                   5598:                        w = (P **)almat_pointer(n,n);
                   5599:                        for ( i = 0; i < n; i++ )
                   5600:                                for ( j = 0; j < n; j++ )
                   5601:                                        ptomp(mod,(P)m[i][j],&w[i][j]);
                   5602:                        detmp(CO,mod,w,n,&mp);
                   5603:                        mptop(mp,rp);
                   5604:                }
                   5605:                return;
                   5606:        }
1.126     noro     5607:
                   5608:        if ( !mod ) {
                   5609:                w = (P **)almat_pointer(n,n);
                   5610:                dq = ONE;
                   5611:                for ( i = 0; i < n; i++ ) {
                   5612:                        dn0 = ONEN;
                   5613:                        for ( j = 0; j < n; j++ ) {
                   5614:                                if ( !m[i][j] ) continue;
                   5615:                                lgp(m[i][j],&nm,&dn);
                   5616:                                gcdn(dn0,dn,&gn); divsn(dn0,gn,&qn); muln(qn,dn,&dn0);
                   5617:                        }
                   5618:                        if ( !UNIN(dn0) ) {
                   5619:                                NTOQ(dn0,1,ds);
                   5620:                                for ( j = 0; j < n; j++ )
                   5621:                                        mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   5622:                                mulq(dq,ds,&dt); dq = dt;
                   5623:                        } else
                   5624:                                for ( j = 0; j < n; j++ )
                   5625:                                        w[i][j] = (P)m[i][j];
                   5626:                }
                   5627:                m = (pointer **)w;
                   5628:        }
1.125     noro     5629:
1.99      noro     5630:        for ( i = 0, max = 0; i < n; i++ )
                   5631:                for ( j = 0; j < n; j++ )
                   5632:                        for ( tv = fv; tv; tv = NEXT(tv) ) {
                   5633:                                e = getdeg(tv->v,(P)m[i][j]);
                   5634:                                max = MAX(e,max);
                   5635:                        }
1.114     noro     5636:        nd_setup_parameters(nvar,max);
1.102     noro     5637:        dm = (NDV **)almat_pointer(n,n);
1.99      noro     5638:        for ( i = 0, max = 0; i < n; i++ )
1.102     noro     5639:                for ( j = 0; j < n; j++ ) {
                   5640:                        dm[i][j] = ptondv(CO,fv,m[i][j]);
                   5641:                        if ( mod ) ndv_mod(mod,dm[i][j]);
                   5642:                        if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   5643:                }
                   5644:        d = ptondv(CO,fv,(P)ONE);
                   5645:        if ( mod ) ndv_mod(mod,d);
1.113     noro     5646:        chsgnq(ONE,&mone);
1.99      noro     5647:        for ( j = 0, sgn = 1; j < n; j++ ) {
1.125     noro     5648:                if ( DP_Print ) fprintf(stderr,".",j);
1.99      noro     5649:                for ( i = j; i < n && !dm[i][j]; i++ );
                   5650:                if ( i == n ) {
                   5651:                        *rp = 0;
                   5652:                        return;
                   5653:                }
1.102     noro     5654:                k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
1.99      noro     5655:                for ( k = j; k < n; k++ )
                   5656:                        for ( l = j; l < n; l++ )
1.102     noro     5657:                                if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   5658:                                        k0 = k; l0 = l; len0 = LEN(dm[k][l]);
1.99      noro     5659:                                }
                   5660:                if ( k0 != j ) {
                   5661:                        t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   5662:                        sgn = -sgn;
                   5663:                }
                   5664:                if ( l0 != j ) {
                   5665:                        for ( k = j; k < n; k++ ) {
                   5666:                                s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   5667:                        }
                   5668:                        sgn = -sgn;
                   5669:                }
1.114     noro     5670:                bound = nd_det_compute_bound(dm,n,j);
                   5671:                if ( ndl_check_bound(bound,bound) )
                   5672:                        nd_det_reconstruct(dm,n,j,d);
                   5673:
1.99      noro     5674:                for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
1.114     noro     5675: /*                     if ( DP_Print ) fprintf(stderr,"        i=%d\n          ",i); */
1.99      noro     5676:                        mi = dm[i]; mij = mi[j];
1.102     noro     5677:                        if ( mod )
                   5678:                                ndv_mul_c(mod,mij,mod-1);
                   5679:                        else
1.113     noro     5680:                                ndv_mul_c_q(mij,mone);
1.99      noro     5681:                        for ( k = j+1; k < n; k++ ) {
1.114     noro     5682: /*                             if ( DP_Print ) fprintf(stderr,"k=%d ",k); */
1.99      noro     5683:                                bucket = create_pbucket();
1.104     noro     5684:                                if ( mi[k] ) {
1.102     noro     5685:                                        nmv = BDY(mjj); len = LEN(mjj);
                   5686:                                        for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   5687:                                                u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   5688:                                                add_pbucket(mod,bucket,u);
1.99      noro     5689:                                        }
1.104     noro     5690:                                }
1.99      noro     5691:                                if ( mj[k] && mij ) {
1.102     noro     5692:                                        nmv = BDY(mij); len = LEN(mij);
                   5693:                                        for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   5694:                                                u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   5695:                                                add_pbucket(mod,bucket,u);
1.99      noro     5696:                                        }
                   5697:                                }
1.104     noro     5698:                                u = nd_quo(mod,bucket,d);
1.102     noro     5699:                                mi[k] = ndtondv(mod,u);
1.99      noro     5700:                        }
1.114     noro     5701: /*                     if ( DP_Print ) fprintf(stderr,"\n",k); */
1.99      noro     5702:                }
                   5703:                d = mjj;
                   5704:        }
1.125     noro     5705:        if ( DP_Print ) fprintf(stderr,"\n",k);
1.100     noro     5706:        if ( sgn < 0 )
1.102     noro     5707:                if ( mod )
                   5708:                        ndv_mul_c(mod,d,mod-1);
                   5709:                else
1.113     noro     5710:                        ndv_mul_c_q(d,mone);
1.126     noro     5711:        r = ndvtop(mod,CO,fv,d);
                   5712:        if ( !mod && !UNIQ(dq) )
                   5713:                divsp(CO,r,(P)dq,rp);
                   5714:        else
                   5715:                *rp = r;
1.99      noro     5716: }
                   5717:
1.102     noro     5718: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
1.99      noro     5719: {
                   5720:        NM mr,mr0;
1.102     noro     5721:        NM tnm;
                   5722:        NMV m;
1.99      noro     5723:        UINT *d0,*dt,*dm;
                   5724:        int c,n,td,i,c1,c2,len;
1.113     noro     5725:        Q q;
1.99      noro     5726:        ND r;
                   5727:
                   5728:        if ( !p ) return 0;
                   5729:        else {
1.102     noro     5730:                n = NV(p); m = BDY(p); len = LEN(p);
1.99      noro     5731:                d0 = DL(m0);
1.102     noro     5732:                td = TD(d);
1.99      noro     5733:                mr0 = 0;
                   5734:                NEWNM(tnm);
1.102     noro     5735:                if ( mod ) {
                   5736:                        c = CM(m0);
                   5737:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   5738:                                ndl_add(DL(m),d0,DL(tnm));
                   5739:                                if ( ndl_reducible(DL(tnm),d) ) {
                   5740:                                        NEXTNM(mr0,mr);
                   5741:                                        c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
1.104     noro     5742:                                        ndl_copy(DL(tnm),DL(mr));
1.102     noro     5743:                                }
                   5744:                        }
                   5745:                } else {
1.113     noro     5746:                        q = CQ(m0);
1.102     noro     5747:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   5748:                                ndl_add(DL(m),d0,DL(tnm));
                   5749:                                if ( ndl_reducible(DL(tnm),d) ) {
                   5750:                                        NEXTNM(mr0,mr);
1.113     noro     5751:                                        mulq(CQ(m),q,&CQ(mr));
1.104     noro     5752:                                        ndl_copy(DL(tnm),DL(mr));
1.102     noro     5753:                                }
1.99      noro     5754:                        }
                   5755:                }
                   5756:                if ( !mr0 )
                   5757:                        return 0;
                   5758:                else {
                   5759:                        NEXT(mr) = 0;
1.105     noro     5760:                        for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
1.99      noro     5761:                        MKND(NV(p),mr0,len,r);
                   5762:                        SG(r) = SG(p) + TD(d0);
                   5763:                        return r;
                   5764:                }
                   5765:        }
1.114     noro     5766: }
                   5767:
                   5768: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   5769: {
                   5770:        int i,obpe,oadv,h,k,l;
                   5771:        static NM prev_nm_free_list;
                   5772:        EPOS oepos;
                   5773:
                   5774:        obpe = nd_bpe;
                   5775:        oadv = nmv_adv;
                   5776:        oepos = nd_epos;
                   5777:        if ( obpe < 2 ) nd_bpe = 2;
                   5778:        else if ( obpe < 3 ) nd_bpe = 3;
                   5779:        else if ( obpe < 4 ) nd_bpe = 4;
                   5780:        else if ( obpe < 5 ) nd_bpe = 5;
                   5781:        else if ( obpe < 6 ) nd_bpe = 6;
                   5782:        else if ( obpe < 8 ) nd_bpe = 8;
                   5783:        else if ( obpe < 10 ) nd_bpe = 10;
                   5784:        else if ( obpe < 16 ) nd_bpe = 16;
                   5785:        else if ( obpe < 32 ) nd_bpe = 32;
                   5786:        else error("nd_det_reconstruct : exponent too large");
                   5787:
                   5788:        nd_setup_parameters(nd_nvar,0);
                   5789:        prev_nm_free_list = _nm_free_list;
                   5790:        _nm_free_list = 0;
                   5791:        for ( k = j; k < n; k++ )
                   5792:                for (l = j; l < n; l++ )
                   5793:                        ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   5794:        ndv_realloc(d,obpe,oadv,oepos);
                   5795:        prev_nm_free_list = 0;
                   5796: #if 0
                   5797:        GC_gcollect();
                   5798: #endif
                   5799: }
                   5800:
                   5801: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   5802: {
                   5803:        UINT *d0,*d1,*d,*t,*r;
                   5804:        int k,l;
                   5805:
                   5806:        d0 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   5807:        d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   5808:        for ( k = 0; k < nd_wpd; k++ ) d0[k] = 0;
                   5809:        for ( k = j; k < n; k++ )
                   5810:                for ( l = j; l < n; l++ )
                   5811:                        if ( dm[k][l] ) {
                   5812:                                d = ndv_compute_bound(dm[k][l]);
                   5813:                                ndl_lcm(d,d0,d1);
                   5814:                                t = d1; d1 = d0; d0 = t;
                   5815:                        }
                   5816:        r = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   5817:        for ( k = 0; k < nd_wpd; k++ ) r[k] = d0[k];
                   5818:        return r;
1.117     noro     5819: }
                   5820:
                   5821: DL nd_separate_d(UINT *d,UINT *trans)
                   5822: {
1.121     noro     5823:        int n,td,i,e,j;
1.117     noro     5824:        DL a;
                   5825:
                   5826:        ndl_zero(trans);
                   5827:        td = 0;
1.121     noro     5828:        for ( i = 0; i < nd_ntrans; i++ ) {
                   5829:                e = GET_EXP(d,i);
                   5830:                PUT_EXP(trans,i,e);
                   5831:                td += MUL_WEIGHT(e,i);
                   5832:        }
                   5833:        if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   5834:                /* homogenized */
                   5835:                i = nd_nvar-1;
1.117     noro     5836:                e = GET_EXP(d,i);
                   5837:                PUT_EXP(trans,i,e);
                   5838:                td += MUL_WEIGHT(e,i);
                   5839:        }
                   5840:        TD(trans) = td;
                   5841:        if ( nd_blockmask) ndl_weight_mask(trans);
                   5842:        NEWDL(a,nd_nalg);
                   5843:        td = 0;
1.121     noro     5844:        for ( i = 0; i < nd_nalg; i++ ) {
                   5845:                j = nd_ntrans+i;
                   5846:                e = GET_EXP(d,j);
                   5847:                a->d[i] = e;
1.117     noro     5848:                td += e;
                   5849:        }
                   5850:        a->td = td;
                   5851:        return a;
                   5852: }
                   5853:
1.118     noro     5854: int nd_monic(int mod,ND *p)
1.117     noro     5855: {
                   5856:        UINT *trans,*t;
                   5857:        DL alg;
                   5858:        MP mp0,mp;
1.118     noro     5859:        NM m,m0,m1,ma0,ma,mb,mr0,mr;
                   5860:        ND r;
1.117     noro     5861:        DL dl;
                   5862:        DP nm;
                   5863:        NDV ndv;
1.118     noro     5864:        DAlg inv,cd;
1.117     noro     5865:        ND s,c;
1.118     noro     5866:        Q l,mul;
                   5867:        N ln;
                   5868:        int n,ntrans,i,e,td,is_lc,len;
                   5869:        NumberField nf;
                   5870:        struct oEGT eg0,eg1;
                   5871:
                   5872:        if ( !(nf = get_numberfield()) )
                   5873:                error("nd_monic : current_numberfield is not set");
1.117     noro     5874:
1.118     noro     5875:        /* Q coef -> DAlg coef */
                   5876:        NEWNM(ma0); ma = ma0;
                   5877:        m = BDY(*p);
                   5878:        is_lc = 1;
                   5879:        while ( 1 ) {
                   5880:                NEWMP(mp0); mp = mp0;
                   5881:                mp->c = (P)CQ(m);
                   5882:                mp->dl = nd_separate_d(DL(m),DL(ma));
                   5883:                NEWNM(mb);
                   5884:                for ( m = NEXT(m); m; m = NEXT(m) ) {
                   5885:                        alg = nd_separate_d(DL(m),DL(mb));
                   5886:                        if ( !ndl_equal(DL(ma),DL(mb)) )
                   5887:                                break;
                   5888:                        NEXTMP(mp0,mp); mp->c = (P)CQ(m); mp->dl = alg;
                   5889:                }
                   5890:                NEXT(mp) = 0;
                   5891:                MKDP(nd_nalg,mp0,nm);
                   5892:                MKDAlg(nm,ONE,cd);
                   5893:                if ( is_lc == 1 ) {
                   5894:                        /* if the lc is a rational number, we have nothing to do */
                   5895:                        if ( !mp0->dl->td )
                   5896:                                return 1;
                   5897:
                   5898:                        get_eg(&eg0);
                   5899:                        invdalg(cd,&inv);
                   5900:                        get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   5901:                        /* check the validity of inv */
                   5902:                        if ( mod && !rem(NM(inv->dn),mod) )
                   5903:                                return 0;
                   5904:                        CA(ma) = nf->one;
                   5905:                        is_lc = 0;
                   5906:                        ln = ONEN;
                   5907:                } else {
                   5908:                        muldalg(cd,inv,&CA(ma));
                   5909:                        lcmn(ln,NM(CA(ma)->dn),&ln);
                   5910:                }
                   5911:                if ( m ) {
                   5912:                        NEXT(ma) = mb; ma = mb;
                   5913:                } else {
                   5914:                        NEXT(ma) = 0;
                   5915:                        break;
                   5916:                }
                   5917:        }
                   5918:        /* l = lcm(denoms) */
                   5919:        NTOQ(ln,1,l);
                   5920:        for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
                   5921:                divq(l,CA(m)->dn,&mul);
                   5922:                for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   5923:                        NEXTNM(mr0,mr);
                   5924:                        mulq((Q)mp->c,mul,&CQ(mr));
                   5925:                        dl = mp->dl;
                   5926:                        td = TD(DL(m));
                   5927:                        ndl_copy(DL(m),DL(mr));
1.121     noro     5928:                        for ( i = 0; i < nd_nalg; i++ ) {
                   5929:                                e = dl->d[i];
                   5930:                                PUT_EXP(DL(mr),i+nd_ntrans,e);
                   5931:                                td += MUL_WEIGHT(e,i+nd_ntrans);
1.118     noro     5932:                        }
                   5933:                        TD(DL(mr)) = td;
                   5934:                        if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   5935:                }
1.117     noro     5936:        }
1.118     noro     5937:        NEXT(mr) = 0;
                   5938:        for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   5939:        MKND(NV(*p),mr0,len,r);
                   5940:        /* XXX */
                   5941:        SG(r) = SG(*p);
                   5942:        nd_free(*p);
                   5943:        *p = r;
                   5944:        return 1;
1.59      noro     5945: }

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