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

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

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