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

Annotation of OpenXM_contrib2/asir2018/engine/nd.c, Revision 1.34

1.34    ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.33 2020/07/09 02:33:38 noro Exp $ */
1.1       noro        2:
                      3: #include "nd.h"
                      4:
1.11      noro        5: int Nnd_add,Nf4_red;
1.12      noro        6: struct oEGT eg_search,f4_symb,f4_conv,f4_elim1,f4_elim2;
1.1       noro        7:
                      8: int diag_period = 6;
                      9: int weight_check = 1;
                     10: int (*ndl_compare_function)(UINT *a1,UINT *a2);
1.21      noro       11: /* for general module order */
1.19      noro       12: int (*ndl_base_compare_function)(UINT *a1,UINT *a2);
1.21      noro       13: int (*dl_base_compare_function)(int nv,DL a,DL b);
                     14: int nd_base_ordtype;
1.1       noro       15: int nd_dcomp;
                     16: int nd_rref2;
                     17: NM _nm_free_list;
                     18: ND _nd_free_list;
                     19: ND_pairs _ndp_free_list;
                     20: NODE nd_hcf;
1.26      noro       21: int Nsyz;
1.1       noro       22:
                     23: Obj nd_top_weight;
                     24:
                     25: static NODE nd_subst;
                     26: static VL nd_vc;
                     27: static int nd_ntrans;
                     28: static int nd_nalg;
                     29: #if 0
                     30: static int ndv_alloc;
                     31: #endif
                     32: #if 1
                     33: static int nd_f4_nsp=0x7fffffff;
                     34: #else
                     35: static int nd_f4_nsp=50;
                     36: #endif
                     37: static double nd_scale=2;
                     38: static UINT **nd_bound;
                     39: static struct order_spec *nd_ord;
                     40: static EPOS nd_epos;
                     41: static BlockMask nd_blockmask;
                     42: static int nd_nvar;
                     43: static int nd_isrlex;
                     44: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
                     45: static UINT nd_mask[32];
                     46: static UINT nd_mask0,nd_mask1;
                     47:
                     48: static NDV *nd_ps;
                     49: static NDV *nd_ps_trace;
                     50: static NDV *nd_ps_sym;
                     51: static NDV *nd_ps_trace_sym;
                     52: static RHist *nd_psh;
1.30      noro       53: static int nd_psn,nd_pslen,nd_nbase;
1.1       noro       54: static RHist *nd_red;
                     55: static int *nd_work_vector;
                     56: static int **nd_matrix;
                     57: static int nd_matrix_len;
                     58: static struct weight_or_block *nd_worb;
                     59: static int nd_worb_len;
                     60: static int nd_found,nd_create,nd_notfirst;
                     61: static int nmv_adv;
                     62: static int nd_demand;
1.21      noro       63: static int nd_module,nd_module_ordtype,nd_mpos,nd_pot_nelim;
1.1       noro       64: static int nd_module_rank,nd_poly_weight_len;
                     65: static int *nd_poly_weight,*nd_module_weight;
                     66: static NODE nd_tracelist;
                     67: static NODE nd_alltracelist;
                     68: static int nd_gentrace,nd_gensyz,nd_nora,nd_newelim,nd_intersect,nd_lf;
                     69: static int *nd_gbblock;
                     70: static NODE nd_nzlist,nd_check_splist;
                     71: static int nd_splist;
                     72: static int *nd_sugarweight;
                     73: static int nd_f4red,nd_rank0,nd_last_nonzero;
1.27      noro       74: static DL *nd_sba_hm;
1.29      noro       75: static NODE *nd_sba_pos;
1.1       noro       76:
                     77: NumberField get_numberfield();
                     78: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
                     79: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
                     80: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
                     81: int nd_monic(int m,ND *p);
                     82: NDV plain_vect_to_ndv_q(Z *mat,int col,UINT *s0vect);
                     83: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank);
                     84: NDV pltondv(VL vl,VL dvl,LIST p);
                     85: void pltozpl(LIST l,Q *cont,LIST *pp);
                     86: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
                     87: void nmtodp(int mod,NM m,DP *r);
1.15      noro       88: void ndltodp(UINT *d,DP *r);
1.1       noro       89: NODE reverse_node(NODE n);
                     90: P ndc_div(int mod,union oNDC a,union oNDC b);
                     91: P ndctop(int mod,union oNDC c);
                     92: void finalize_tracelist(int i,P cont);
                     93: void conv_ilist(int demand,int trace,NODE g,int **indp);
                     94: void parse_nd_option(NODE opt);
                     95: void dltondl(int n,DL dl,UINT *r);
                     96: DP ndvtodp(int mod,NDV p);
                     97: DP ndtodp(int mod,ND p);
1.16      noro       98: DPM ndvtodpm(int mod,NDV p);
                     99: NDV dpmtondv(int mod,DPM p);
                    100: int dpm_getdeg(DPM p,int *rank);
                    101: void dpm_ptozp(DPM p,Z *cont,DPM *r);
                    102: int compdmm(int nv,DMM a,DMM b);
1.1       noro      103:
                    104: void Pdp_set_weight(NODE,VECT *);
                    105: void Pox_cmo_rpc(NODE,Obj *);
                    106:
                    107: ND nd_add_lf(ND p1,ND p2);
                    108: void nd_mul_c_lf(ND p,Z mul);
                    109: void ndv_mul_c_lf(NDV p,Z mul);
                    110: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                    111:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
                    112: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                    113:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
                    114: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                    115:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
                    116: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
                    117: NODE nd_f4_lf_trace_main(int m,int **indp);
                    118: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
                    119:
                    120: extern int lf_lazy;
                    121: extern Z current_mod_lf;
                    122:
                    123: extern int Denominator,DP_Multiple,MaxDeg;
                    124:
                    125: #define BLEN (8*sizeof(unsigned long))
                    126:
                    127: typedef struct matrix {
                    128:   int row,col;
                    129:   unsigned long **a;
                    130: } *matrix;
                    131:
                    132:
                    133: void nd_free_private_storage()
                    134: {
                    135:     _nm_free_list = 0;
                    136:     _ndp_free_list = 0;
                    137: #if 0
                    138:     GC_gcollect();
                    139: #endif
                    140: }
                    141:
                    142: void _NM_alloc()
                    143: {
                    144:     NM p;
                    145:     int i;
                    146:
                    147:     for ( i = 0; i < 1024; i++ ) {
                    148:         p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                    149:         p->next = _nm_free_list; _nm_free_list = p;
                    150:     }
                    151: }
                    152:
                    153: matrix alloc_matrix(int row,int col)
                    154: {
                    155:   unsigned long **a;
                    156:   int i,len,blen;
                    157:   matrix mat;
                    158:
                    159:   mat = (matrix)MALLOC(sizeof(struct matrix));
                    160:   mat->row = row;
                    161:   mat->col = col;
                    162:   mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
                    163:   return mat;
                    164: }
                    165:
                    166:
                    167: void _ND_alloc()
                    168: {
                    169:     ND p;
                    170:     int i;
                    171:
                    172:     for ( i = 0; i < 1024; i++ ) {
                    173:         p = (ND)MALLOC(sizeof(struct oND));
                    174:         p->body = (NM)_nd_free_list; _nd_free_list = p;
                    175:     }
                    176: }
                    177:
                    178: void _NDP_alloc()
                    179: {
                    180:     ND_pairs p;
                    181:     int i;
                    182:
                    183:     for ( i = 0; i < 1024; i++ ) {
                    184:         p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
                    185:             +(nd_wpd-1)*sizeof(UINT));
                    186:         p->next = _ndp_free_list; _ndp_free_list = p;
                    187:     }
                    188: }
                    189:
                    190: INLINE int nd_length(ND p)
                    191: {
                    192:     NM m;
                    193:     int i;
                    194:
                    195:     if ( !p )
                    196:         return 0;
                    197:     else {
                    198:         for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
                    199:         return i;
                    200:     }
                    201: }
                    202:
                    203: extern int dp_negative_weight;
                    204:
                    205: INLINE int ndl_reducible(UINT *d1,UINT *d2)
                    206: {
                    207:     UINT u1,u2;
                    208:     int i,j;
                    209:
                    210:     if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
                    211:
                    212:     if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
                    213: #if USE_UNROLL
                    214:     switch ( nd_bpe ) {
                    215:         case 3:
                    216:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    217:                 u1 = d1[i]; u2 = d2[i];
                    218:                 if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
                    219:                 if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
                    220:                 if ( (u1&  0xe00000) < (u2&  0xe00000) ) return 0;
                    221:                 if ( (u1&  0x1c0000) < (u2&  0x1c0000) ) return 0;
                    222:                 if ( (u1&   0x38000) < (u2&   0x38000) ) return 0;
                    223:                 if ( (u1&    0x7000) < (u2&    0x7000) ) return 0;
                    224:                 if ( (u1&     0xe00) < (u2&     0xe00) ) return 0;
                    225:                 if ( (u1&     0x1c0) < (u2&     0x1c0) ) return 0;
                    226:                 if ( (u1&      0x38) < (u2&      0x38) ) return 0;
                    227:                 if ( (u1&       0x7) < (u2&       0x7) ) return 0;
                    228:             }
                    229:             return 1;
                    230:             break;
                    231:         case 4:
                    232:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    233:                 u1 = d1[i]; u2 = d2[i];
                    234:                 if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
                    235:                 if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
                    236:                 if ( (u1&  0xf00000) < (u2&  0xf00000) ) return 0;
                    237:                 if ( (u1&   0xf0000) < (u2&   0xf0000) ) return 0;
                    238:                 if ( (u1&    0xf000) < (u2&    0xf000) ) return 0;
                    239:                 if ( (u1&     0xf00) < (u2&     0xf00) ) return 0;
                    240:                 if ( (u1&      0xf0) < (u2&      0xf0) ) return 0;
                    241:                 if ( (u1&       0xf) < (u2&       0xf) ) return 0;
                    242:             }
                    243:             return 1;
                    244:             break;
                    245:         case 6:
                    246:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    247:                 u1 = d1[i]; u2 = d2[i];
                    248:                 if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
                    249:                 if ( (u1&  0xfc0000) < (u2&  0xfc0000) ) return 0;
                    250:                 if ( (u1&   0x3f000) < (u2&   0x3f000) ) return 0;
                    251:                 if ( (u1&     0xfc0) < (u2&     0xfc0) ) return 0;
                    252:                 if ( (u1&      0x3f) < (u2&      0x3f) ) return 0;
                    253:             }
                    254:             return 1;
                    255:             break;
                    256:         case 8:
                    257:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    258:                 u1 = d1[i]; u2 = d2[i];
                    259:                 if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
                    260:                 if ( (u1&  0xff0000) < (u2&  0xff0000) ) return 0;
                    261:                 if ( (u1&    0xff00) < (u2&    0xff00) ) return 0;
                    262:                 if ( (u1&      0xff) < (u2&      0xff) ) return 0;
                    263:             }
                    264:             return 1;
                    265:             break;
                    266:         case 16:
                    267:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    268:                 u1 = d1[i]; u2 = d2[i];
                    269:                 if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
                    270:                 if ( (u1&    0xffff) < (u2&    0xffff) ) return 0;
                    271:             }
                    272:             return 1;
                    273:             break;
                    274:         case 32:
                    275:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                    276:                 if ( d1[i] < d2[i] ) return 0;
                    277:             return 1;
                    278:             break;
                    279:         default:
                    280:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    281:                 u1 = d1[i]; u2 = d2[i];
                    282:                 for ( j = 0; j < nd_epw; j++ )
                    283:                     if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    284:             }
                    285:             return 1;
                    286:     }
                    287: #else
                    288:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    289:         u1 = d1[i]; u2 = d2[i];
                    290:         for ( j = 0; j < nd_epw; j++ )
                    291:             if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    292:     }
                    293:     return 1;
                    294: #endif
                    295: }
                    296:
                    297: /*
                    298:  * If the current order is a block order,
                    299:  * then the last block is length 1 and contains
                    300:  * the homo variable. Otherwise, the original
                    301:  * order is either 0 or 2.
                    302:  */
                    303:
                    304: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
                    305: {
                    306:     int w,i,e,n,omask0;
                    307:
                    308:     omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
                    309:     n = nd_nvar-1;
                    310:     ndl_zero(r);
                    311:     for ( i = 0; i < n; i++ ) {
                    312:         e = GET_EXP_OLD(d,i);
                    313:         PUT_EXP(r,i,e);
                    314:     }
                    315:     w = TD(d);
                    316:     PUT_EXP(r,nd_nvar-1,weight-w);
                    317:     if ( nd_module ) MPOS(r) = d[ompos];
                    318:     TD(r) = weight;
                    319:     if ( nd_blockmask ) ndl_weight_mask(r);
                    320: }
                    321:
                    322: void ndl_dehomogenize(UINT *d)
                    323: {
                    324:     UINT mask;
                    325:     UINT h;
                    326:     int i,bits;
                    327:
                    328:     if ( nd_blockmask ) {
                    329:         h = GET_EXP(d,nd_nvar-1);
                    330:         XOR_EXP(d,nd_nvar-1,h);
                    331:         TD(d) -= h;
                    332:         ndl_weight_mask(d);
                    333:     } else {
                    334:         if ( nd_isrlex ) {
                    335:             if ( nd_bpe == 32 ) {
                    336:                 h = d[nd_exporigin];
                    337:                 for ( i = nd_exporigin+1; i < nd_wpd; i++ )
                    338:                     d[i-1] = d[i];
                    339:                 d[i-1] = 0;
                    340:                 TD(d) -= h;
                    341:             } else {
                    342:                 bits = nd_epw*nd_bpe;
                    343:                 mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
                    344:                 h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
                    345:                 for ( i = nd_exporigin; i < nd_wpd; i++ )
                    346:                     d[i] = ((d[i]<<nd_bpe)&mask)
                    347:                         |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
                    348:                 TD(d) -= h;
                    349:             }
                    350:         } else {
                    351:             h = GET_EXP(d,nd_nvar-1);
                    352:             XOR_EXP(d,nd_nvar-1,h);
                    353:             TD(d) -= h;
                    354:         }
                    355:     }
                    356: }
                    357:
                    358: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
                    359: {
                    360:     UINT t1,t2,u,u1,u2;
                    361:     int i,j,l;
                    362:
                    363:     if ( nd_module && (MPOS(d1) != MPOS(d2)) )
                    364:         error("ndl_lcm : inconsistent monomials");
                    365: #if USE_UNROLL
                    366:     switch ( nd_bpe ) {
                    367:         case 3:
                    368:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    369:                 u1 = d1[i]; u2 = d2[i];
                    370:                 t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
                    371:                 t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
                    372:                 t1 = (u1&  0xe00000); t2 = (u2&  0xe00000); u |= t1>t2?t1:t2;
                    373:                 t1 = (u1&  0x1c0000); t2 = (u2&  0x1c0000); u |= t1>t2?t1:t2;
                    374:                 t1 = (u1&   0x38000); t2 = (u2&   0x38000); u |= t1>t2?t1:t2;
                    375:                 t1 = (u1&    0x7000); t2 = (u2&    0x7000); u |= t1>t2?t1:t2;
                    376:                 t1 = (u1&     0xe00); t2 = (u2&     0xe00); u |= t1>t2?t1:t2;
                    377:                 t1 = (u1&     0x1c0); t2 = (u2&     0x1c0); u |= t1>t2?t1:t2;
                    378:                 t1 = (u1&      0x38); t2 = (u2&      0x38); u |= t1>t2?t1:t2;
                    379:                 t1 = (u1&       0x7); t2 = (u2&       0x7); u |= t1>t2?t1:t2;
                    380:                 d[i] = u;
                    381:             }
                    382:             break;
                    383:         case 4:
                    384:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    385:                 u1 = d1[i]; u2 = d2[i];
                    386:                 t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
                    387:                 t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
                    388:                 t1 = (u1&  0xf00000); t2 = (u2&  0xf00000); u |= t1>t2?t1:t2;
                    389:                 t1 = (u1&   0xf0000); t2 = (u2&   0xf0000); u |= t1>t2?t1:t2;
                    390:                 t1 = (u1&    0xf000); t2 = (u2&    0xf000); u |= t1>t2?t1:t2;
                    391:                 t1 = (u1&     0xf00); t2 = (u2&     0xf00); u |= t1>t2?t1:t2;
                    392:                 t1 = (u1&      0xf0); t2 = (u2&      0xf0); u |= t1>t2?t1:t2;
                    393:                 t1 = (u1&       0xf); t2 = (u2&       0xf); u |= t1>t2?t1:t2;
                    394:                 d[i] = u;
                    395:             }
                    396:             break;
                    397:         case 6:
                    398:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    399:                 u1 = d1[i]; u2 = d2[i];
                    400:                 t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
                    401:                 t1 = (u1&  0xfc0000); t2 = (u2&  0xfc0000); u |= t1>t2?t1:t2;
                    402:                 t1 = (u1&   0x3f000); t2 = (u2&   0x3f000); u |= t1>t2?t1:t2;
                    403:                 t1 = (u1&     0xfc0); t2 = (u2&     0xfc0); u |= t1>t2?t1:t2;
                    404:                 t1 = (u1&      0x3f); t2 = (u2&      0x3f); u |= t1>t2?t1:t2;
                    405:                 d[i] = u;
                    406:             }
                    407:             break;
                    408:         case 8:
                    409:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    410:                 u1 = d1[i]; u2 = d2[i];
                    411:                 t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
                    412:                 t1 = (u1&  0xff0000); t2 = (u2&  0xff0000); u |= t1>t2?t1:t2;
                    413:                 t1 = (u1&    0xff00); t2 = (u2&    0xff00); u |= t1>t2?t1:t2;
                    414:                 t1 = (u1&      0xff); t2 = (u2&      0xff); u |= t1>t2?t1:t2;
                    415:                 d[i] = u;
                    416:             }
                    417:             break;
                    418:         case 16:
                    419:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    420:                 u1 = d1[i]; u2 = d2[i];
                    421:                 t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
                    422:                 t1 = (u1&    0xffff); t2 = (u2&    0xffff); u |= t1>t2?t1:t2;
                    423:                 d[i] = u;
                    424:             }
                    425:             break;
                    426:         case 32:
                    427:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    428:                 u1 = d1[i]; u2 = d2[i];
                    429:                 d[i] = u1>u2?u1:u2;
                    430:             }
                    431:             break;
                    432:         default:
                    433:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    434:                 u1 = d1[i]; u2 = d2[i];
                    435:                 for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    436:                     t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    437:                 }
                    438:                 d[i] = u;
                    439:             }
                    440:             break;
                    441:     }
                    442: #else
                    443:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    444:         u1 = d1[i]; u2 = d2[i];
                    445:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    446:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    447:         }
                    448:         d[i] = u;
                    449:     }
                    450: #endif
                    451:     if ( nd_module ) MPOS(d) = MPOS(d1);
                    452:     TD(d) = ndl_weight(d);
                    453:     if ( nd_blockmask ) ndl_weight_mask(d);
                    454: }
                    455:
                    456: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
                    457: {
                    458:     UINT t1,t2,u,u1,u2;
                    459:     int i,j,l;
                    460:
                    461:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    462:         u1 = d1[i]; u2 = d2[i];
                    463:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    464:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    465:         }
                    466:         d[i] = u;
                    467:     }
                    468: }
                    469:
                    470: int ndl_weight(UINT *d)
                    471: {
                    472:     UINT t,u;
                    473:     int i,j;
                    474:
                    475:     if ( current_dl_weight_vector )
                    476:         for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    477:             u = GET_EXP(d,i);
                    478:             t += MUL_WEIGHT(u,i);
                    479:         }
                    480:     else
                    481:         for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    482:             u = d[i];
                    483:             for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    484:                 t += (u&nd_mask0);
                    485:         }
1.20      noro      486:     if ( nd_module && nd_module_rank && MPOS(d) )
                    487:         t += nd_module_weight[MPOS(d)-1];
                    488:     for ( i = nd_exporigin; i < nd_wpd; i++ )
                    489:       if ( d[i] && !t )
                    490:         printf("afo\n");
1.1       noro      491:     return t;
                    492: }
                    493:
                    494: /* for sugarweight */
                    495:
                    496: int ndl_weight2(UINT *d)
                    497: {
                    498:     int t,u;
                    499:     int i,j;
                    500:
                    501:     for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    502:         u = GET_EXP(d,i);
                    503:         t += nd_sugarweight[i]*u;
                    504:     }
1.20      noro      505:     if ( nd_module && nd_module_rank && MPOS(d) )
                    506:         t += nd_module_weight[MPOS(d)-1];
1.1       noro      507:     return t;
                    508: }
                    509:
                    510: void ndl_weight_mask(UINT *d)
                    511: {
                    512:     UINT t,u;
                    513:     UINT *mask;
                    514:     int i,j,k,l;
                    515:
                    516:     l = nd_blockmask->n;
                    517:     for ( k = 0; k < l; k++ ) {
                    518:         mask = nd_blockmask->mask[k];
                    519:         if ( current_dl_weight_vector )
                    520:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    521:                 u = GET_EXP_MASK(d,i,mask);
                    522:                 t += MUL_WEIGHT(u,i);
                    523:             }
                    524:         else
                    525:             for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    526:                 u = d[i]&mask[i];
                    527:                 for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    528:                     t += (u&nd_mask0);
                    529:             }
                    530:         d[k+1] = t;
                    531:     }
                    532: }
                    533:
1.21      noro      534: int ndl_glex_compare(UINT *d1,UINT *d2)
                    535: {
                    536:   if ( TD(d1) > TD(d2) ) return 1;
                    537:   else if ( TD(d1) < TD(d2) ) return -1;
                    538:   else return ndl_lex_compare(d1,d2);
                    539: }
                    540:
1.1       noro      541: int ndl_lex_compare(UINT *d1,UINT *d2)
                    542: {
                    543:     int i;
                    544:
                    545:     d1 += nd_exporigin;
                    546:     d2 += nd_exporigin;
                    547:     for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
                    548:         if ( *d1 > *d2 )
                    549:             return nd_isrlex ? -1 : 1;
                    550:         else if ( *d1 < *d2 )
                    551:             return nd_isrlex ? 1 : -1;
                    552:     return 0;
                    553: }
                    554:
                    555: int ndl_block_compare(UINT *d1,UINT *d2)
                    556: {
                    557:     int i,l,j,ord_o,ord_l;
                    558:     struct order_pair *op;
                    559:     UINT t1,t2,m;
                    560:     UINT *mask;
                    561:
                    562:     l = nd_blockmask->n;
                    563:     op = nd_blockmask->order_pair;
                    564:     for ( j = 0; j < l; j++ ) {
                    565:         mask = nd_blockmask->mask[j];
                    566:         ord_o = op[j].order;
                    567:         if ( ord_o < 2 ) {
                    568:             if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
                    569:             else if ( t1 < t2 ) return -1;
                    570:         }
                    571:         for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    572:             m = mask[i];
                    573:             t1 = d1[i]&m;
                    574:             t2 = d2[i]&m;
                    575:             if ( t1 > t2 )
                    576:                 return !ord_o ? -1 : 1;
                    577:             else if ( t1 < t2 )
                    578:                 return !ord_o ? 1 : -1;
                    579:         }
                    580:     }
                    581:     return 0;
                    582: }
                    583:
                    584: int ndl_matrix_compare(UINT *d1,UINT *d2)
                    585: {
                    586:   int i,j,s,row;
                    587:   int *v;
                    588:   Z **mat;
                    589:   Z *w;
                    590:   Z t1;
                    591:   Z t,t2;
                    592:
1.6       noro      593:   for ( j = 0; j < nd_nvar; j++ )
                    594:     nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.1       noro      595:   if ( nd_top_weight ) {
                    596:     if ( OID(nd_top_weight) == O_VECT ) {
1.6       noro      597:       mat = (Z **)&BDY((VECT)nd_top_weight);
                    598:       row = 1;
1.1       noro      599:     } else {
                    600:       mat = (Z **)BDY((MAT)nd_top_weight);
1.6       noro      601:       row = ((MAT)nd_top_weight)->row;
1.1       noro      602:     }
                    603:     for ( i = 0; i < row; i++ ) {
1.6       noro      604:       w = mat[i];
1.1       noro      605:       for ( j = 0, t = 0; j < nd_nvar; j++ ) {
1.6       noro      606:         STOZ(nd_work_vector[j],t1);
1.1       noro      607:         mulz(w[j],t1,&t2);
                    608:         addz(t,t2,&t1);
                    609:         t = t1;
                    610:       }
1.6       noro      611:       if ( t ) {
                    612:         s = sgnz(t);
1.1       noro      613:         if ( s > 0 ) return 1;
                    614:         else if ( s < 0 ) return -1;
1.6       noro      615:       }
1.1       noro      616:     }
1.6       noro      617:   }
                    618:   for ( i = 0; i < nd_matrix_len; i++ ) {
                    619:     v = nd_matrix[i];
                    620:     for ( j = 0, s = 0; j < nd_nvar; j++ )
                    621:       s += v[j]*nd_work_vector[j];
                    622:     if ( s > 0 ) return 1;
                    623:     else if ( s < 0 ) return -1;
                    624:   }
1.1       noro      625:   if ( !ndl_equal(d1,d2) )
1.6       noro      626:     error("ndl_matrix_compare : invalid matrix");
                    627:   return 0;
1.1       noro      628: }
                    629:
                    630: int ndl_composite_compare(UINT *d1,UINT *d2)
                    631: {
                    632:     int i,j,s,start,end,len,o;
                    633:     int *v;
                    634:     struct sparse_weight *sw;
                    635:
                    636:     for ( j = 0; j < nd_nvar; j++ )
                    637:         nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
                    638:     for ( i = 0; i < nd_worb_len; i++ ) {
                    639:         len = nd_worb[i].length;
                    640:         switch ( nd_worb[i].type ) {
                    641:             case IS_DENSE_WEIGHT:
                    642:                 v = nd_worb[i].body.dense_weight;
                    643:                 for ( j = 0, s = 0; j < len; j++ )
                    644:                     s += v[j]*nd_work_vector[j];
                    645:                 if ( s > 0 ) return 1;
                    646:                 else if ( s < 0 ) return -1;
                    647:                 break;
                    648:             case IS_SPARSE_WEIGHT:
                    649:                 sw = nd_worb[i].body.sparse_weight;
                    650:                 for ( j = 0, s = 0; j < len; j++ )
                    651:                     s += sw[j].value*nd_work_vector[sw[j].pos];
                    652:                 if ( s > 0 ) return 1;
                    653:                 else if ( s < 0 ) return -1;
                    654:                 break;
                    655:             case IS_BLOCK:
                    656:                 o = nd_worb[i].body.block.order;
                    657:                 start = nd_worb[i].body.block.start;
                    658:                 switch ( o ) {
                    659:                     case 0:
                    660:                         end = start+len;
                    661:                         for ( j = start, s = 0; j < end; j++ )
                    662:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    663:                         if ( s > 0 ) return 1;
                    664:                         else if ( s < 0 ) return -1;
                    665:                         for ( j = end-1; j >= start; j-- )
                    666:                             if ( nd_work_vector[j] < 0 ) return 1;
                    667:                             else if ( nd_work_vector[j] > 0 ) return -1;
                    668:                         break;
                    669:                     case 1:
                    670:                         end = start+len;
                    671:                         for ( j = start, s = 0; j < end; j++ )
                    672:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    673:                         if ( s > 0 ) return 1;
                    674:                         else if ( s < 0 ) return -1;
                    675:                         for ( j = start; j < end; j++ )
                    676:                             if ( nd_work_vector[j] > 0 ) return 1;
                    677:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    678:                         break;
                    679:                     case 2:
                    680:                         end = start+len;
                    681:                         for ( j = start; j < end; j++ )
                    682:                             if ( nd_work_vector[j] > 0 ) return 1;
                    683:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    684:                         break;
                    685:                 }
                    686:                 break;
                    687:         }
                    688:     }
                    689:     return 0;
                    690: }
                    691:
                    692: /* TDH -> WW -> TD-> RL */
                    693:
                    694: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
                    695: {
                    696:     int i,m,e1,e2;
                    697:
                    698:     if ( TD(d1) > TD(d2) ) return 1;
                    699:     else if ( TD(d1) < TD(d2) ) return -1;
                    700:     m = nd_nvar>>1;
                    701:     for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
                    702:         e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
                    703:         e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
                    704:     }
                    705:     if ( e1 > e2 ) return 1;
                    706:     else if ( e1 < e2 ) return -1;
                    707:     return ndl_lex_compare(d1,d2);
                    708: }
                    709:
1.21      noro      710: // common function for module glex and grlex comparison
                    711: int ndl_module_glex_compare(UINT *d1,UINT *d2)
1.1       noro      712: {
1.21      noro      713:   int c;
1.1       noro      714:
1.21      noro      715:   switch ( nd_module_ordtype ) {
                    716:     case 0:
                    717:       if ( TD(d1) > TD(d2) ) return 1;
                    718:       else if ( TD(d1) < TD(d2) ) return -1;
                    719:       else if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
                    720:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    721:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    722:       else return 0;
                    723:       break;
1.1       noro      724:
1.21      noro      725:     case 1:
1.19      noro      726:       if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
                    727:          if ( TD(d1) > TD(d2) ) return 1;
                    728:          else if ( TD(d1) < TD(d2) ) return -1;
                    729:          if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
                    730:          if ( MPOS(d1) < MPOS(d2) ) return 1;
                    731:          else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    732:       }
                    733:       if ( MPOS(d1) < MPOS(d2) ) return 1;
                    734:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
1.21      noro      735:       else if ( TD(d1) > TD(d2) ) return 1;
                    736:       else if ( TD(d1) < TD(d2) ) return -1;
                    737:       else return ndl_lex_compare(d1,d2);
                    738:       break;
1.1       noro      739:
1.21      noro      740:     case 2: // weight -> POT
                    741:       if ( TD(d1) > TD(d2) ) return 1;
                    742:       else if ( TD(d1) < TD(d2) ) return -1;
                    743:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    744:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    745:       else return ndl_lex_compare(d1,d2);
                    746:       break;
1.1       noro      747:
1.21      noro      748:     default:
                    749:       error("ndl_module_glex_compare : invalid module_ordtype");
1.28      noro      750:       return 0;
1.21      noro      751:   }
1.1       noro      752: }
                    753:
1.21      noro      754: // common  for module comparison
                    755: int ndl_module_compare(UINT *d1,UINT *d2)
1.1       noro      756: {
1.21      noro      757:   int c;
1.1       noro      758:
1.21      noro      759:   switch ( nd_module_ordtype ) {
                    760:     case 0:
1.23      noro      761:       if ( (c = (*ndl_base_compare_function)(d1,d2)) != 0 ) return c;
1.21      noro      762:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    763:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    764:       else return 0;
                    765:       break;
1.1       noro      766:
1.21      noro      767:     case 1:
                    768:       if ( MPOS(d1) < MPOS(d2) ) return 1;
                    769:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    770:       else return (*ndl_base_compare_function)(d1,d2);
                    771:       break;
1.1       noro      772:
1.21      noro      773:     case 2: // weight -> POT
                    774:       if ( TD(d1) > TD(d2) ) return 1;
                    775:       else if ( TD(d1) < TD(d2) ) return -1;
                    776:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    777:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    778:       else return (*ndl_base_compare_function)(d1,d2);
                    779:       break;
1.1       noro      780:
1.21      noro      781:     default:
                    782:       error("ndl_module_compare : invalid module_ordtype");
1.28      noro      783:       return 0;
1.21      noro      784:   }
1.1       noro      785: }
                    786:
1.21      noro      787: extern DMMstack dmm_stack;
                    788: void _addtodl(int n,DL d1,DL d2);
1.31      noro      789: void _adddl(int n,DL d1,DL d2,DL d3);
1.21      noro      790: int _eqdl(int n,DL d1,DL d2);
                    791:
                    792: int ndl_module_schreyer_compare(UINT *m1,UINT *m2)
                    793: {
                    794:   int pos1,pos2,t,j;
                    795:   DMM *in;
                    796:   DMMstack s;
                    797:   static DL d1=0;
                    798:   static DL d2=0;
                    799:   static int dlen=0;
                    800:
                    801:   pos1 = MPOS(m1); pos2 = MPOS(m2);
                    802:   if ( pos1 == pos2 ) return (*ndl_base_compare_function)(m1,m2);
                    803:   if ( nd_nvar > dlen ) {
                    804:     NEWDL(d1,nd_nvar);
                    805:     NEWDL(d2,nd_nvar);
                    806:     dlen = nd_nvar;
                    807:   }
                    808:   d1->td = TD(m1);
                    809:   for ( j = 0; j < nd_nvar; j++ ) d1->d[j] = GET_EXP(m1,j);
                    810:   d2->td = TD(m2);
                    811:   for ( j = 0; j < nd_nvar; j++ ) d2->d[j] = GET_EXP(m2,j);
                    812:   for ( s = dmm_stack; s; s = NEXT(s) ) {
                    813:     in = s->in;
                    814:     _addtodl(nd_nvar,in[pos1]->dl,d1);
                    815:     _addtodl(nd_nvar,in[pos2]->dl,d2);
                    816:     if ( in[pos1]->pos == in[pos2]->pos && _eqdl(nd_nvar,d1,d2)) {
                    817:       if ( pos1 < pos2 ) return 1;
                    818:       else if ( pos1 > pos2 ) return -1;
                    819:       else return 0;
                    820:     }
                    821:     pos1 = in[pos1]->pos;
                    822:     pos2 = in[pos2]->pos;
                    823:     if ( pos1 == pos2 ) return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    824:   }
                    825:   // comparison by the bottom order
                    826: LAST:
                    827:   switch ( nd_base_ordtype ) {
                    828:     case 0:
                    829:       t = (*dl_base_compare_function)(nd_nvar,d1,d2);
                    830:       if ( t ) return t;
                    831:       else if ( pos1 < pos2 ) return 1;
                    832:       else if ( pos1 > pos2 ) return -1;
                    833:       else return 0;
                    834:       break;
                    835:     case 1:
                    836:       if ( pos1 < pos2 ) return 1;
                    837:       else if ( pos1 > pos2 ) return -1;
                    838:       else return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    839:       break;
                    840:     case 2:
                    841:       if ( d1->td > d2->td  ) return 1;
                    842:       else if ( d1->td < d2->td ) return -1;
                    843:       else if ( pos1 < pos2 ) return 1;
                    844:       else if ( pos1 > pos2 ) return -1;
                    845:       else return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    846:       break;
                    847:     default:
                    848:       error("ndl_schreyer_compare : invalid base ordtype");
1.28      noro      849:       return 0;
1.21      noro      850:   }
1.1       noro      851: }
                    852:
                    853: INLINE int ndl_equal(UINT *d1,UINT *d2)
                    854: {
                    855:     int i;
                    856:
                    857:     switch ( nd_wpd ) {
                    858:         case 2:
                    859:             if ( TD(d2) != TD(d1) ) return 0;
                    860:             if ( d2[1] != d1[1] ) return 0;
                    861:             return 1;
                    862:             break;
                    863:         case 3:
                    864:             if ( TD(d2) != TD(d1) ) return 0;
                    865:             if ( d2[1] != d1[1] ) return 0;
                    866:             if ( d2[2] != d1[2] ) return 0;
                    867:             return 1;
                    868:             break;
                    869:         default:
                    870:             for ( i = 0; i < nd_wpd; i++ )
                    871:                 if ( *d1++ != *d2++ ) return 0;
                    872:             return 1;
                    873:             break;
                    874:     }
                    875: }
                    876:
                    877: INLINE void ndl_copy(UINT *d1,UINT *d2)
                    878: {
                    879:     int i;
                    880:
                    881:     switch ( nd_wpd ) {
                    882:         case 2:
                    883:             TD(d2) = TD(d1);
                    884:             d2[1] = d1[1];
                    885:             break;
                    886:         case 3:
                    887:             TD(d2) = TD(d1);
                    888:             d2[1] = d1[1];
                    889:             d2[2] = d1[2];
                    890:             break;
                    891:         default:
                    892:             for ( i = 0; i < nd_wpd; i++ )
                    893:                 d2[i] = d1[i];
                    894:             break;
                    895:     }
                    896: }
                    897:
                    898: INLINE void ndl_zero(UINT *d)
                    899: {
                    900:     int i;
                    901:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                    902: }
                    903:
                    904: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
                    905: {
                    906:     int i;
                    907:
                    908:     if ( nd_module ) {
                    909:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
                    910:         error("ndl_add : invalid operation");
                    911:     }
                    912: #if 1
                    913:     switch ( nd_wpd ) {
                    914:         case 2:
                    915:             TD(d) = TD(d1)+TD(d2);
                    916:             d[1] = d1[1]+d2[1];
                    917:             break;
                    918:         case 3:
                    919:             TD(d) = TD(d1)+TD(d2);
                    920:             d[1] = d1[1]+d2[1];
                    921:             d[2] = d1[2]+d2[2];
                    922:             break;
                    923:         default:
                    924:             for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    925:             break;
                    926:     }
                    927: #else
                    928:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    929: #endif
                    930: }
                    931:
                    932: /* d1 += d2 */
                    933: INLINE void ndl_addto(UINT *d1,UINT *d2)
                    934: {
                    935:     int i;
                    936:
                    937:     if ( nd_module ) {
                    938:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
                    939:             error("ndl_addto : invalid operation");
                    940:     }
                    941: #if 1
                    942:     switch ( nd_wpd ) {
                    943:         case 2:
                    944:             TD(d1) += TD(d2);
                    945:             d1[1] += d2[1];
                    946:             break;
                    947:         case 3:
                    948:             TD(d1) += TD(d2);
                    949:             d1[1] += d2[1];
                    950:             d1[2] += d2[2];
                    951:             break;
                    952:         default:
                    953:             for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    954:             break;
                    955:     }
                    956: #else
                    957:     for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    958: #endif
                    959: }
                    960:
                    961: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
                    962: {
                    963:     int i;
                    964:
                    965:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
                    966: }
                    967:
                    968: int ndl_disjoint(UINT *d1,UINT *d2)
                    969: {
                    970:     UINT t1,t2,u,u1,u2;
                    971:     int i,j;
                    972:
                    973:     if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
                    974: #if USE_UNROLL
                    975:     switch ( nd_bpe ) {
                    976:         case 3:
                    977:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    978:                 u1 = d1[i]; u2 = d2[i];
                    979:                 t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
                    980:                 t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
                    981:                 t1 = u1&  0xe00000; t2 = u2&  0xe00000; if ( t1&&t2 ) return 0;
                    982:                 t1 = u1&  0x1c0000; t2 = u2&  0x1c0000; if ( t1&&t2 ) return 0;
                    983:                 t1 = u1&   0x38000; t2 = u2&   0x38000; if ( t1&&t2 ) return 0;
                    984:                 t1 = u1&    0x7000; t2 = u2&    0x7000; if ( t1&&t2 ) return 0;
                    985:                 t1 = u1&     0xe00; t2 = u2&     0xe00; if ( t1&&t2 ) return 0;
                    986:                 t1 = u1&     0x1c0; t2 = u2&     0x1c0; if ( t1&&t2 ) return 0;
                    987:                 t1 = u1&      0x38; t2 = u2&      0x38; if ( t1&&t2 ) return 0;
                    988:                 t1 = u1&       0x7; t2 = u2&       0x7; if ( t1&&t2 ) return 0;
                    989:             }
                    990:             return 1;
                    991:             break;
                    992:         case 4:
                    993:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    994:                 u1 = d1[i]; u2 = d2[i];
                    995:                 t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
                    996:                 t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
                    997:                 t1 = u1&  0xf00000; t2 = u2&  0xf00000; if ( t1&&t2 ) return 0;
                    998:                 t1 = u1&   0xf0000; t2 = u2&   0xf0000; if ( t1&&t2 ) return 0;
                    999:                 t1 = u1&    0xf000; t2 = u2&    0xf000; if ( t1&&t2 ) return 0;
                   1000:                 t1 = u1&     0xf00; t2 = u2&     0xf00; if ( t1&&t2 ) return 0;
                   1001:                 t1 = u1&      0xf0; t2 = u2&      0xf0; if ( t1&&t2 ) return 0;
                   1002:                 t1 = u1&       0xf; t2 = u2&       0xf; if ( t1&&t2 ) return 0;
                   1003:             }
                   1004:             return 1;
                   1005:             break;
                   1006:         case 6:
                   1007:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1008:                 u1 = d1[i]; u2 = d2[i];
                   1009:                 t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
                   1010:                 t1 = u1&  0xfc0000; t2 = u2&  0xfc0000; if ( t1&&t2 ) return 0;
                   1011:                 t1 = u1&   0x3f000; t2 = u2&   0x3f000; if ( t1&&t2 ) return 0;
                   1012:                 t1 = u1&     0xfc0; t2 = u2&     0xfc0; if ( t1&&t2 ) return 0;
                   1013:                 t1 = u1&      0x3f; t2 = u2&      0x3f; if ( t1&&t2 ) return 0;
                   1014:             }
                   1015:             return 1;
                   1016:             break;
                   1017:         case 8:
                   1018:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1019:                 u1 = d1[i]; u2 = d2[i];
                   1020:                 t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
                   1021:                 t1 = u1&  0xff0000; t2 = u2&  0xff0000; if ( t1&&t2 ) return 0;
                   1022:                 t1 = u1&    0xff00; t2 = u2&    0xff00; if ( t1&&t2 ) return 0;
                   1023:                 t1 = u1&      0xff; t2 = u2&      0xff; if ( t1&&t2 ) return 0;
                   1024:             }
                   1025:             return 1;
                   1026:             break;
                   1027:         case 16:
                   1028:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1029:                 u1 = d1[i]; u2 = d2[i];
                   1030:                 t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
                   1031:                 t1 = u1&    0xffff; t2 = u2&    0xffff; if ( t1&&t2 ) return 0;
                   1032:             }
                   1033:             return 1;
                   1034:             break;
                   1035:         case 32:
                   1036:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                   1037:                 if ( d1[i] && d2[i] ) return 0;
                   1038:             return 1;
                   1039:             break;
                   1040:         default:
                   1041:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1042:                 u1 = d1[i]; u2 = d2[i];
                   1043:                 for ( j = 0; j < nd_epw; j++ ) {
                   1044:                     if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                   1045:                     u1 >>= nd_bpe; u2 >>= nd_bpe;
                   1046:                 }
                   1047:             }
                   1048:             return 1;
                   1049:             break;
                   1050:     }
                   1051: #else
                   1052:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1053:         u1 = d1[i]; u2 = d2[i];
                   1054:         for ( j = 0; j < nd_epw; j++ ) {
                   1055:             if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                   1056:             u1 >>= nd_bpe; u2 >>= nd_bpe;
                   1057:         }
                   1058:     }
                   1059:     return 1;
                   1060: #endif
                   1061: }
                   1062:
                   1063: int ndl_check_bound(UINT *d1,UINT *d2)
                   1064: {
                   1065:     UINT u2;
                   1066:     int i,j,ind,k;
                   1067:
                   1068:     ind = 0;
                   1069: #if USE_UNROLL
                   1070:     switch ( nd_bpe ) {
                   1071:         case 3:
                   1072:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1073:                 u2 = d2[i];
                   1074:                 if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
                   1075:                 if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
                   1076:                 if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
                   1077:                 if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
                   1078:                 if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
                   1079:                 if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
                   1080:                 if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
                   1081:                 if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
                   1082:                 if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
                   1083:                 if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
                   1084:             }
                   1085:             return 0;
                   1086:             break;
                   1087:         case 4:
                   1088:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1089:                 u2 = d2[i];
                   1090:                 if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
                   1091:                 if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
                   1092:                 if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
                   1093:                 if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
                   1094:                 if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
                   1095:                 if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
                   1096:                 if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
                   1097:                 if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
                   1098:             }
                   1099:             return 0;
                   1100:             break;
                   1101:         case 6:
                   1102:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1103:                 u2 = d2[i];
                   1104:                 if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
                   1105:                 if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
                   1106:                 if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
                   1107:                 if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
                   1108:                 if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
                   1109:             }
                   1110:             return 0;
                   1111:             break;
                   1112:         case 8:
                   1113:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1114:                 u2 = d2[i];
                   1115:                 if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
                   1116:                 if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
                   1117:                 if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
                   1118:                 if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
                   1119:             }
                   1120:             return 0;
                   1121:             break;
                   1122:         case 16:
                   1123:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1124:                 u2 = d2[i];
                   1125:                 if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
                   1126:                 if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
                   1127:             }
                   1128:             return 0;
                   1129:             break;
                   1130:         case 32:
                   1131:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                   1132:                 if ( d1[i]+d2[i]<d1[i] ) return 1;
                   1133:             return 0;
                   1134:             break;
                   1135:         default:
                   1136:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1137:                 u2 = d2[i];
                   1138:                 k = (nd_epw-1)*nd_bpe;
                   1139:                 for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1140:                     if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1141:             }
                   1142:             return 0;
                   1143:             break;
                   1144:     }
                   1145: #else
                   1146:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1147:         u2 = d2[i];
                   1148:         k = (nd_epw-1)*nd_bpe;
                   1149:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1150:             if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1151:     }
                   1152:     return 0;
                   1153: #endif
                   1154: }
                   1155:
                   1156: int ndl_check_bound2(int index,UINT *d2)
                   1157: {
                   1158:     return ndl_check_bound(nd_bound[index],d2);
                   1159: }
                   1160:
                   1161: INLINE int ndl_hash_value(UINT *d)
                   1162: {
                   1163:     int i;
1.11      noro     1164:     UINT r;
1.1       noro     1165:
                   1166:     r = 0;
                   1167:     for ( i = 0; i < nd_wpd; i++ )
1.12      noro     1168:         r = (r*1511+d[i]);
1.11      noro     1169:     r %= REDTAB_LEN;
1.1       noro     1170:     return r;
                   1171: }
                   1172:
                   1173: INLINE int ndl_find_reducer(UINT *dg)
                   1174: {
                   1175:     RHist r;
                   1176:     int d,k,i;
                   1177:
                   1178:     d = ndl_hash_value(dg);
                   1179:     for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   1180:         if ( ndl_equal(dg,DL(r)) ) {
                   1181:             if ( k > 0 ) nd_notfirst++;
                   1182:             nd_found++;
                   1183:             return r->index;
                   1184:         }
                   1185:     }
                   1186:     if ( Reverse )
                   1187:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   1188:             r = nd_psh[i];
                   1189:             if ( ndl_reducible(dg,DL(r)) ) {
                   1190:                 nd_create++;
                   1191:                 nd_append_red(dg,i);
                   1192:                 return i;
                   1193:             }
                   1194:         }
                   1195:     else
                   1196:         for ( i = 0; i < nd_psn; i++ ) {
                   1197:             r = nd_psh[i];
                   1198:             if ( ndl_reducible(dg,DL(r)) ) {
                   1199:                 nd_create++;
                   1200:                 nd_append_red(dg,i);
                   1201:                 return i;
                   1202:             }
                   1203:         }
                   1204:     return -1;
                   1205: }
                   1206:
1.24      noro     1207: // ret=0,...,nd_psn-1 => reducer found
                   1208: // ret=nd_psn => reducer not found
                   1209: // ret=-1 => singular top reducible
                   1210:
                   1211: int comp_sig(SIG s1,SIG s2);
                   1212: void _ndltodl(UINT *ndl,DL dl);
                   1213:
                   1214: void print_sig(SIG s)
                   1215: {
                   1216:   int i;
                   1217:
                   1218:   fprintf(asir_out,"<<");
                   1219:   for ( i = 0; i < nd_nvar; i++ ) {
                   1220:     fprintf(asir_out,"%d",s->dl->d[i]);
                   1221:     if ( i != nd_nvar-1 ) fprintf(asir_out,",");
                   1222:   }
                   1223:   fprintf(asir_out,">>*e%d",s->pos);
                   1224: }
                   1225:
                   1226: INLINE int ndl_find_reducer_s(UINT *dg,SIG sig)
                   1227: {
                   1228:   RHist r;
1.27      noro     1229:   int i,singular,ret,d,k;
1.26      noro     1230:   static int wpd,nvar;
1.24      noro     1231:   static SIG quo;
                   1232:   static UINT *tmp;
                   1233:
1.26      noro     1234:   if ( !quo || nvar != nd_nvar ) NEWSIG(quo);
                   1235:   if ( wpd != nd_wpd ) {
1.24      noro     1236:     wpd = nd_wpd;
                   1237:     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
                   1238:   }
1.27      noro     1239:   d = ndl_hash_value(dg);
                   1240:   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   1241:     if ( ndl_equal(dg,DL(r)) ) {
                   1242:       return r->index;
                   1243:     }
                   1244:   }
1.24      noro     1245:   singular = 0;
                   1246:   for ( i = 0; i < nd_psn; i++ ) {
                   1247:     r = nd_psh[i];
                   1248:     if ( ndl_reducible(dg,DL(r)) ) {
1.27      noro     1249:       ndl_sub(dg,DL(r),tmp);
1.24      noro     1250:       _ndltodl(tmp,DL(quo));
                   1251:       _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
                   1252:       quo->pos = nd_psh[i]->sig->pos;
                   1253:       ret = comp_sig(sig,quo);
                   1254:       if ( ret > 0 ) { singular = 0; break; }
1.29      noro     1255:       if ( ret == 0 ) { fprintf(asir_out,"s"); fflush(asir_out); singular = 1; }
1.24      noro     1256:     }
                   1257:   }
                   1258:   if ( singular ) return -1;
1.27      noro     1259:   else if ( i < nd_psn )
                   1260:     nd_append_red(dg,i);
                   1261:   return i;
1.24      noro     1262: }
                   1263:
1.1       noro     1264: ND nd_merge(ND p1,ND p2)
                   1265: {
                   1266:     int n,c;
                   1267:     int t,can,td1,td2;
                   1268:     ND r;
                   1269:     NM m1,m2,mr0,mr,s;
                   1270:
                   1271:     if ( !p1 ) return p2;
                   1272:     else if ( !p2 ) return p1;
                   1273:     else {
                   1274:         can = 0;
                   1275:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1276:             c = DL_COMPARE(DL(m1),DL(m2));
                   1277:             switch ( c ) {
                   1278:                 case 0:
                   1279:                     s = m1; m1 = NEXT(m1);
                   1280:                     can++; NEXTNM2(mr0,mr,s);
                   1281:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1282:                     break;
                   1283:                 case 1:
                   1284:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1285:                     break;
                   1286:                 case -1:
                   1287:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1288:                     break;
                   1289:             }
                   1290:         }
                   1291:         if ( !mr0 )
                   1292:             if ( m1 ) mr0 = m1;
                   1293:             else if ( m2 ) mr0 = m2;
                   1294:             else return 0;
                   1295:         else if ( m1 ) NEXT(mr) = m1;
                   1296:         else if ( m2 ) NEXT(mr) = m2;
                   1297:         else NEXT(mr) = 0;
                   1298:         BDY(p1) = mr0;
                   1299:         SG(p1) = MAX(SG(p1),SG(p2));
                   1300:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1301:         FREEND(p2);
                   1302:         return p1;
                   1303:     }
                   1304: }
                   1305:
                   1306: ND nd_add(int mod,ND p1,ND p2)
                   1307: {
                   1308:     int n,c;
                   1309:     int t,can,td1,td2;
                   1310:     ND r;
                   1311:     NM m1,m2,mr0,mr,s;
                   1312:
1.11      noro     1313:     Nnd_add++;
1.1       noro     1314:     if ( !p1 ) return p2;
                   1315:     else if ( !p2 ) return p1;
                   1316:     else if ( mod == -1 ) return nd_add_sf(p1,p2);
                   1317:     else if ( mod == -2 ) return nd_add_lf(p1,p2);
                   1318:     else if ( !mod ) return nd_add_q(p1,p2);
                   1319:     else {
                   1320:         can = 0;
                   1321:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1322:             c = DL_COMPARE(DL(m1),DL(m2));
                   1323:             switch ( c ) {
                   1324:                 case 0:
                   1325:                     t = ((CM(m1))+(CM(m2))) - mod;
                   1326:                     if ( t < 0 ) t += mod;
                   1327:                     s = m1; m1 = NEXT(m1);
                   1328:                     if ( t ) {
                   1329:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1330:                     } else {
                   1331:                         can += 2; FREENM(s);
                   1332:                     }
                   1333:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1334:                     break;
                   1335:                 case 1:
                   1336:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1337:                     break;
                   1338:                 case -1:
                   1339:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1340:                     break;
                   1341:             }
                   1342:         }
                   1343:         if ( !mr0 )
                   1344:             if ( m1 ) mr0 = m1;
                   1345:             else if ( m2 ) mr0 = m2;
                   1346:             else return 0;
                   1347:         else if ( m1 ) NEXT(mr) = m1;
                   1348:         else if ( m2 ) NEXT(mr) = m2;
                   1349:         else NEXT(mr) = 0;
                   1350:         BDY(p1) = mr0;
                   1351:         SG(p1) = MAX(SG(p1),SG(p2));
                   1352:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1353:         FREEND(p2);
                   1354:         return p1;
                   1355:     }
                   1356: }
                   1357:
                   1358: /* XXX on opteron, the inlined manipulation of destructive additon of
                   1359:  * two NM seems to make gcc optimizer get confused, so the part is
                   1360:  * done in a function.
                   1361:  */
                   1362:
                   1363: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
                   1364: {
                   1365:     NM s;
                   1366:     P t;
                   1367:     int can;
                   1368:
                   1369:     addp(nd_vc,CP(*m1),CP(*m2),&t);
                   1370:     s = *m1; *m1 = NEXT(*m1);
                   1371:     if ( t ) {
                   1372:         can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
                   1373:     } else {
                   1374:         can = 2; FREENM(s);
                   1375:     }
                   1376:     s = *m2; *m2 = NEXT(*m2); FREENM(s);
                   1377:     return can;
                   1378: }
                   1379:
                   1380: ND nd_add_q(ND p1,ND p2)
                   1381: {
                   1382:     int n,c,can;
                   1383:     ND r;
                   1384:     NM m1,m2,mr0,mr,s;
                   1385:     P t;
                   1386:
                   1387:     if ( !p1 ) return p2;
                   1388:     else if ( !p2 ) return p1;
                   1389:     else {
                   1390:         can = 0;
                   1391:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1392:             c = DL_COMPARE(DL(m1),DL(m2));
                   1393:             switch ( c ) {
                   1394:                 case 0:
                   1395: #if defined(__x86_64__)
                   1396:                     can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
                   1397: #else
                   1398:                     addp(nd_vc,CP(m1),CP(m2),&t);
                   1399:                     s = m1; m1 = NEXT(m1);
                   1400:                     if ( t ) {
                   1401:                         can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
                   1402:                     } else {
                   1403:                         can += 2; FREENM(s);
                   1404:                     }
                   1405:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1406: #endif
                   1407:                     break;
                   1408:                 case 1:
                   1409:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1410:                     break;
                   1411:                 case -1:
                   1412:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1413:                     break;
                   1414:             }
                   1415:         }
                   1416:         if ( !mr0 )
                   1417:             if ( m1 ) mr0 = m1;
                   1418:             else if ( m2 ) mr0 = m2;
                   1419:             else return 0;
                   1420:         else if ( m1 ) NEXT(mr) = m1;
                   1421:         else if ( m2 ) NEXT(mr) = m2;
                   1422:         else NEXT(mr) = 0;
                   1423:         BDY(p1) = mr0;
                   1424:         SG(p1) = MAX(SG(p1),SG(p2));
                   1425:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1426:         FREEND(p2);
                   1427:         return p1;
                   1428:     }
                   1429: }
                   1430:
                   1431: ND nd_add_sf(ND p1,ND p2)
                   1432: {
                   1433:     int n,c,can;
                   1434:     ND r;
                   1435:     NM m1,m2,mr0,mr,s;
                   1436:     int t;
                   1437:
                   1438:     if ( !p1 ) return p2;
                   1439:     else if ( !p2 ) return p1;
                   1440:     else {
                   1441:         can = 0;
                   1442:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1443:             c = DL_COMPARE(DL(m1),DL(m2));
                   1444:             switch ( c ) {
                   1445:                 case 0:
                   1446:                     t = _addsf(CM(m1),CM(m2));
                   1447:                     s = m1; m1 = NEXT(m1);
                   1448:                     if ( t ) {
                   1449:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1450:                     } else {
                   1451:                         can += 2; FREENM(s);
                   1452:                     }
                   1453:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1454:                     break;
                   1455:                 case 1:
                   1456:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1457:                     break;
                   1458:                 case -1:
                   1459:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1460:                     break;
                   1461:             }
                   1462:         }
                   1463:         if ( !mr0 )
                   1464:             if ( m1 ) mr0 = m1;
                   1465:             else if ( m2 ) mr0 = m2;
                   1466:             else return 0;
                   1467:         else if ( m1 ) NEXT(mr) = m1;
                   1468:         else if ( m2 ) NEXT(mr) = m2;
                   1469:         else NEXT(mr) = 0;
                   1470:         BDY(p1) = mr0;
                   1471:         SG(p1) = MAX(SG(p1),SG(p2));
                   1472:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1473:         FREEND(p2);
                   1474:         return p1;
                   1475:     }
                   1476: }
                   1477:
                   1478:
                   1479: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
                   1480: {
                   1481:     int c,c1,c2;
                   1482:     Z cg,cred,gcd,tq;
                   1483:     P cgp,credp,gcdp;
                   1484:     Obj tr,tr1;
                   1485:
                   1486:     if ( mod == -1 ) {
                   1487:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1488:         *divp = (Obj)ONE;
                   1489:     } else if ( mod == -2 ) {
                   1490:         Z inv,t;
                   1491:         divlf(ONE,HCZ(p),&inv);
                   1492:         chsgnlf(HCZ(g),&t);
                   1493:         mullf(inv,t,&CZ(mul));
                   1494:         *divp = (Obj)ONE;
                   1495:     } else if ( mod ) {
                   1496:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1497:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1498:         *divp = (Obj)ONE;
                   1499:     } else if ( nd_vc ) {
                   1500:         ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
                   1501:         divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
                   1502:         chsgnp(cgp,&CP(mul));
                   1503:         nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
                   1504:         if ( dn ) {
                   1505:             mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
                   1506:             reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
                   1507:         }
                   1508:         *divp = (Obj)credp;
                   1509:     } else {
1.6       noro     1510:         igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1511:         chsgnz(cg,&CZ(mul));
1.1       noro     1512:         nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
                   1513:         if ( dn ) {
                   1514:             mulz(dn->z,cred,&tq); dn->z = tq;
                   1515:         }
                   1516:         *divp = (Obj)cred;
                   1517:     }
                   1518:     return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
                   1519: }
                   1520:
                   1521: /* ret=1 : success, ret=0 : overflow */
1.6       noro     1522: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1       noro     1523: {
                   1524:     NM m,mrd,tail;
                   1525:     NM mul;
                   1526:     int n,sugar,psugar,sugar0,stat,index;
                   1527:     int c,c1,c2,dummy;
                   1528:     RHist h;
                   1529:     NDV p,red;
                   1530:     Q cg,cred,gcd,tq,qq;
                   1531:     Z iq;
                   1532:     DP dmul;
                   1533:     NODE node;
                   1534:     LIST hist;
                   1535:     double hmag;
                   1536:     P tp,tp1;
                   1537:     Obj tr,tr1,div;
                   1538:     union oNDC hg;
                   1539:     P cont;
                   1540:
                   1541:     if ( !g ) {
                   1542:         *rp = d;
                   1543:         return 1;
                   1544:     }
                   1545:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1546:
                   1547:     sugar0 = sugar = SG(g);
                   1548:     n = NV(g);
                   1549:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1550:     if ( d )
                   1551:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1552:     for ( ; g; ) {
                   1553:         index = ndl_find_reducer(HDL(g));
                   1554:         if ( index >= 0 ) {
                   1555:             h = nd_psh[index];
                   1556:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1557:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1558:                 nd_free(g); nd_free(d);
                   1559:                 return 0;
                   1560:             }
                   1561:             p = nd_demand ? ndv_load(index) : ps[index];
                   1562:             /* d+g -> div*(d+g)+mul*p */
1.6       noro     1563:             g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1       noro     1564:             if ( nd_gentrace ) {
                   1565:                 /* Trace=[div,index,mul,ONE] */
1.6       noro     1566:                 STOZ(index,iq);
1.1       noro     1567:                 nmtodp(mod,mul,&dmul);
                   1568:                 node = mknode(4,div,iq,dmul,ONE);
                   1569:             }
                   1570:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
                   1571:             if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
                   1572:                 hg = HCU(g);
                   1573:                 nd_removecont2(d,g);
1.6       noro     1574:                 if ( nd_gentrace ) {
1.1       noro     1575:                     /* overwrite cont : Trace=[div,index,mul,cont] */
1.6       noro     1576:                     /* exact division */
1.1       noro     1577:                     cont = ndc_div(mod,hg,HCU(g));
                   1578:                     if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
                   1579:                 }
                   1580:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1581:             }
                   1582:             MKLIST(hist,node);
                   1583:             MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
                   1584:         } else if ( !full ) {
                   1585:             *rp = g;
                   1586:             return 1;
                   1587:         } else {
                   1588:             m = BDY(g);
                   1589:             if ( NEXT(m) ) {
                   1590:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1591:             } else {
                   1592:                 FREEND(g); g = 0;
                   1593:             }
                   1594:             if ( d ) {
                   1595:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1596:             } else {
                   1597:                 MKND(n,m,1,d); tail = BDY(d);
                   1598:             }
                   1599:         }
                   1600:     }
                   1601:     if ( d ) SG(d) = sugar;
                   1602:     *rp = d;
                   1603:     return 1;
                   1604: }
                   1605:
1.24      noro     1606: // ret=1 => success
                   1607: // ret=0 => overflow
                   1608: // ret=-1 => singular top reducible
                   1609:
                   1610: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
                   1611: {
                   1612:     NM m,mrd,tail;
                   1613:     NM mul;
                   1614:     int n,sugar,psugar,sugar0,stat,index;
                   1615:     int c,c1,c2,dummy;
                   1616:     RHist h;
                   1617:     NDV p,red;
                   1618:     Q cg,cred,gcd,tq,qq;
                   1619:     Z iq;
                   1620:     DP dmul;
                   1621:     NODE node;
                   1622:     LIST hist;
                   1623:     double hmag;
                   1624:     P tp,tp1;
                   1625:     Obj tr,tr1,div;
                   1626:     union oNDC hg;
                   1627:     P cont;
                   1628:     SIG sig;
                   1629:
                   1630:     if ( !g ) {
                   1631:         *rp = d;
                   1632:         return 1;
                   1633:     }
                   1634:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1635:
                   1636:     sugar0 = sugar = SG(g);
                   1637:     n = NV(g);
                   1638:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1639:     if ( d )
                   1640:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1641:     sig = g->sig;
                   1642:     for ( ; g; ) {
                   1643:         index = ndl_find_reducer_s(HDL(g),sig);
                   1644:         if ( index >= 0 && index < nd_psn ) {
                   1645:             // reducer found
                   1646:             h = nd_psh[index];
                   1647:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1648:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1649:                 nd_free(g); nd_free(d);
                   1650:                 return 0;
                   1651:             }
                   1652:             p = ps[index];
                   1653:             /* d+g -> div*(d+g)+mul*p */
                   1654:             g = nd_reduce2(mod,d,g,p,mul,0,&div);
                   1655:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
                   1656:             if ( !mod && g && ((double)(p_mag(HCP(g))) > hmag) ) {
                   1657:                 hg = HCU(g);
                   1658:                 nd_removecont2(d,g);
                   1659:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1660:             }
                   1661:         } else if ( index == -1 ) {
                   1662:           // singular top reducible
                   1663:           return -1;
                   1664:         } else if ( !full ) {
                   1665:             *rp = g;
                   1666:             g->sig = sig;
                   1667:             return 1;
                   1668:         } else {
                   1669:             m = BDY(g);
                   1670:             if ( NEXT(m) ) {
                   1671:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1672:             } else {
                   1673:                 FREEND(g); g = 0;
                   1674:             }
                   1675:             if ( d ) {
                   1676:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1677:             } else {
                   1678:                 MKND(n,m,1,d); tail = BDY(d);
                   1679:             }
                   1680:         }
                   1681:     }
                   1682:     if ( d ) {
                   1683:       SG(d) = sugar;
                   1684:       d->sig = sig;
                   1685:     }
                   1686:     *rp = d;
                   1687:     return 1;
                   1688: }
                   1689:
1.1       noro     1690: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
                   1691: {
                   1692:     int hindex,index;
                   1693:     NDV p;
                   1694:     ND u,d,red;
                   1695:     NODE l;
                   1696:     NM mul,m,mrd,tail;
                   1697:     int sugar,psugar,n,h_reducible;
                   1698:     PGeoBucket bucket;
                   1699:     int c,c1,c2;
                   1700:     Z cg,cred,gcd,zzz;
                   1701:     RHist h;
                   1702:     double hmag,gmag;
                   1703:     int count = 0;
                   1704:     int hcount = 0;
                   1705:
                   1706:     if ( !g ) {
                   1707:         *rp = 0;
                   1708:         return 1;
                   1709:     }
                   1710:     sugar = SG(g);
                   1711:     n = NV(g);
1.6       noro     1712:     if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1       noro     1713:     bucket = create_pbucket();
                   1714:     add_pbucket(mod,bucket,g);
                   1715:     d = 0;
                   1716:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1717:     while ( 1 ) {
                   1718:         if ( mod > 0 || mod == -1 )
                   1719:           hindex = head_pbucket(mod,bucket);
                   1720:         else if ( mod == -2 )
                   1721:           hindex = head_pbucket_lf(bucket);
                   1722:         else
                   1723:           hindex = head_pbucket_q(bucket);
                   1724:         if ( hindex < 0 ) {
                   1725:             if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1726:             if ( d ) SG(d) = sugar;
                   1727:             *rp = d;
                   1728:             return 1;
                   1729:         }
                   1730:         g = bucket->body[hindex];
                   1731:         index = ndl_find_reducer(HDL(g));
                   1732:         if ( index >= 0 ) {
                   1733:             count++;
                   1734:             if ( !d ) hcount++;
                   1735:             h = nd_psh[index];
                   1736:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1737:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1738:                 nd_free(d);
                   1739:                 free_pbucket(bucket);
                   1740:                 *rp = 0;
                   1741:                 return 0;
                   1742:             }
                   1743:             p = ps[index];
                   1744:             if ( mod == -1 )
                   1745:                 CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1746:             else if ( mod == -2 ) {
                   1747:                 Z inv,t;
                   1748:                 divlf(ONE,HCZ(p),&inv);
                   1749:                 chsgnlf(HCZ(g),&t);
                   1750:                 mullf(inv,t,&CZ(mul));
                   1751:             } else if ( mod ) {
                   1752:                 c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1753:                 DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1754:             } else {
1.6       noro     1755:                 igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1756:                 chsgnz(cg,&CZ(mul));
1.1       noro     1757:                 nd_mul_c_q(d,(P)cred);
                   1758:                 mulq_pbucket(bucket,cred);
                   1759:                 g = bucket->body[hindex];
1.6       noro     1760:                 gmag = (double)p_mag((P)HCZ(g));
1.1       noro     1761:             }
                   1762:             red = ndv_mul_nm(mod,mul,p);
                   1763:             bucket->body[hindex] = nd_remove_head(g);
                   1764:             red = nd_remove_head(red);
                   1765:             add_pbucket(mod,bucket,red);
                   1766:             psugar = SG(p)+TD(DL(mul));
                   1767:             sugar = MAX(sugar,psugar);
                   1768:             if ( !mod && hmag && (gmag > hmag) ) {
                   1769:                 g = normalize_pbucket(mod,bucket);
                   1770:                 if ( !g ) {
                   1771:                     if ( d ) SG(d) = sugar;
                   1772:                     *rp = d;
                   1773:                     return 1;
                   1774:                 }
                   1775:                 nd_removecont2(d,g);
1.6       noro     1776:                 hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1       noro     1777:                 add_pbucket(mod,bucket,g);
                   1778:             }
                   1779:         } else if ( !full ) {
                   1780:             g = normalize_pbucket(mod,bucket);
                   1781:             if ( g ) SG(g) = sugar;
                   1782:             *rp = g;
                   1783:             return 1;
                   1784:         } else {
                   1785:             m = BDY(g);
                   1786:             if ( NEXT(m) ) {
                   1787:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1788:             } else {
                   1789:                 FREEND(g); g = 0;
                   1790:             }
                   1791:             bucket->body[hindex] = g;
                   1792:             NEXT(m) = 0;
                   1793:             if ( d ) {
                   1794:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1795:             } else {
                   1796:                 MKND(n,m,1,d); tail = BDY(d);
                   1797:             }
                   1798:         }
                   1799:     }
                   1800: }
                   1801:
1.25      noro     1802: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *rp)
                   1803: {
                   1804:   int hindex,index;
                   1805:   NDV p;
                   1806:   ND u,d,red;
                   1807:   NODE l;
                   1808:   NM mul,m,mrd,tail;
                   1809:   int sugar,psugar,n,h_reducible;
                   1810:   PGeoBucket bucket;
                   1811:   int c,c1,c2;
                   1812:   Z cg,cred,gcd,zzz;
                   1813:   RHist h;
                   1814:   double hmag,gmag;
                   1815:   int count = 0;
                   1816:   int hcount = 0;
                   1817:   SIG sig;
                   1818:
                   1819:   if ( !g ) {
                   1820:     *rp = 0;
                   1821:     return 1;
                   1822:   }
                   1823:   sugar = SG(g);
                   1824:   n = NV(g);
                   1825:   if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
                   1826:   bucket = create_pbucket();
                   1827:   add_pbucket(mod,bucket,g);
                   1828:   d = 0;
                   1829:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1830:   sig = g->sig;
                   1831:   while ( 1 ) {
                   1832:     if ( mod > 0 || mod == -1 )
                   1833:       hindex = head_pbucket(mod,bucket);
                   1834:     else if ( mod == -2 )
                   1835:       hindex = head_pbucket_lf(bucket);
                   1836:     else
                   1837:       hindex = head_pbucket_q(bucket);
                   1838:     if ( hindex < 0 ) {
                   1839:       if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1840:       if ( d ) {
                   1841:         SG(d) = sugar;
                   1842:         d->sig = sig;
                   1843:       }
                   1844:       *rp = d;
                   1845:       return 1;
                   1846:     }
                   1847:     g = bucket->body[hindex];
                   1848:     index = ndl_find_reducer_s(HDL(g),sig);
                   1849:     if ( index >= 0 && index < nd_psn ) {
                   1850:       count++;
                   1851:       if ( !d ) hcount++;
                   1852:       h = nd_psh[index];
                   1853:       ndl_sub(HDL(g),DL(h),DL(mul));
                   1854:       if ( ndl_check_bound2(index,DL(mul)) ) {
                   1855:         nd_free(d);
                   1856:         free_pbucket(bucket);
                   1857:         *rp = 0;
                   1858:         return 0;
                   1859:       }
                   1860:       p = ps[index];
                   1861:       if ( mod == -1 )
                   1862:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1863:       else if ( mod == -2 ) {
                   1864:         Z inv,t;
                   1865:         divlf(ONE,HCZ(p),&inv);
                   1866:         chsgnlf(HCZ(g),&t);
                   1867:         mullf(inv,t,&CZ(mul));
                   1868:       } else if ( mod ) {
                   1869:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1870:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1871:       } else {
                   1872:         igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1873:         chsgnz(cg,&CZ(mul));
                   1874:         nd_mul_c_q(d,(P)cred);
                   1875:         mulq_pbucket(bucket,cred);
                   1876:         g = bucket->body[hindex];
                   1877:         gmag = (double)p_mag((P)HCZ(g));
                   1878:       }
                   1879:       red = ndv_mul_nm(mod,mul,p);
                   1880:       bucket->body[hindex] = nd_remove_head(g);
                   1881:       red = nd_remove_head(red);
                   1882:       add_pbucket(mod,bucket,red);
                   1883:       psugar = SG(p)+TD(DL(mul));
                   1884:       sugar = MAX(sugar,psugar);
                   1885:       if ( !mod && hmag && (gmag > hmag) ) {
                   1886:          g = normalize_pbucket(mod,bucket);
                   1887:          if ( !g ) {
                   1888:            if ( d ) {
                   1889:              SG(d) = sugar;
                   1890:              d->sig = sig;
                   1891:            }
                   1892:            *rp = d;
                   1893:            return 1;
                   1894:          }
                   1895:          nd_removecont2(d,g);
                   1896:          hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
                   1897:          add_pbucket(mod,bucket,g);
                   1898:       }
                   1899:     } else if ( index == -1 ) {
                   1900:       // singular top reducible
                   1901:       return -1;
                   1902:     } else if ( !full ) {
                   1903:       g = normalize_pbucket(mod,bucket);
                   1904:       if ( g ) {
                   1905:         SG(g) = sugar;
                   1906:         g->sig = sig;
                   1907:       }
                   1908:       *rp = g;
                   1909:       return 1;
                   1910:     } else {
                   1911:       m = BDY(g);
                   1912:       if ( NEXT(m) ) {
                   1913:         BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1914:       } else {
                   1915:         FREEND(g); g = 0;
                   1916:       }
                   1917:       bucket->body[hindex] = g;
                   1918:       NEXT(m) = 0;
                   1919:       if ( d ) {
                   1920:         NEXT(tail)=m; tail=m; LEN(d)++;
                   1921:       } else {
                   1922:         MKND(n,m,1,d); tail = BDY(d);
                   1923:       }
                   1924:     }
                   1925:   }
                   1926: }
                   1927:
1.1       noro     1928: /* input : list of NDV, cand : list of NDV */
                   1929:
                   1930: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
                   1931: {
                   1932:     int n,i,stat;
                   1933:     ND nf,d;
                   1934:     NDV r;
                   1935:     NODE t,s;
                   1936:     union oNDC dn;
                   1937:     Z q;
                   1938:     LIST list;
                   1939:
1.24      noro     1940:     ndv_setup(m,0,cand,nd_gentrace?1:0,1,0);
1.1       noro     1941:     n = length(cand);
                   1942:
                   1943:   if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
                   1944:     /* membercheck : list is a subset of Id(cand) ? */
                   1945:     for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
                   1946: again:
                   1947:     nd_tracelist = 0;
                   1948:         if ( nd_bpe > obpe )
                   1949:             r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
                   1950:         else
                   1951:             r = (NDV)BDY(t);
                   1952: #if 0
                   1953:         // moved to nd_f4_lf_trace()
                   1954:         if ( m == -2 ) ndv_mod(m,r);
                   1955: #endif
                   1956:         d = ndvtond(m,r);
1.6       noro     1957:         stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1       noro     1958:         if ( !stat ) {
                   1959:             nd_reconstruct(0,0);
                   1960:             goto again;
                   1961:         } else if ( nf ) return 0;
                   1962:     if ( nd_gentrace ) {
                   1963:       nd_tracelist = reverse_node(nd_tracelist);
                   1964:       MKLIST(list,nd_tracelist);
1.6       noro     1965:       STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1       noro     1966:       MKNODE(s,list,nd_alltracelist);
                   1967:       nd_alltracelist = s; nd_tracelist = 0;
                   1968:     }
                   1969:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   1970:     }
                   1971:     if ( DP_Print ) { printf("\n"); }
                   1972:     return 1;
                   1973: }
                   1974:
                   1975: ND nd_remove_head(ND p)
                   1976: {
                   1977:     NM m;
                   1978:
                   1979:     m = BDY(p);
                   1980:     if ( !NEXT(m) ) {
                   1981:         FREEND(p); p = 0;
                   1982:     } else {
                   1983:         BDY(p) = NEXT(m); LEN(p)--;
                   1984:     }
                   1985:     FREENM(m);
                   1986:     return p;
                   1987: }
                   1988:
                   1989: ND nd_separate_head(ND p,ND *head)
                   1990: {
                   1991:     NM m,m0;
                   1992:     ND r;
                   1993:
                   1994:     m = BDY(p);
                   1995:     if ( !NEXT(m) ) {
                   1996:         *head = p; p = 0;
                   1997:     } else {
                   1998:         m0 = m;
                   1999:         BDY(p) = NEXT(m); LEN(p)--;
                   2000:         NEXT(m0) = 0;
                   2001:         MKND(NV(p),m0,1,r);
                   2002:         *head = r;
                   2003:     }
                   2004:     return p;
                   2005: }
                   2006:
                   2007: PGeoBucket create_pbucket()
                   2008: {
                   2009:     PGeoBucket g;
                   2010:
                   2011:     g = CALLOC(1,sizeof(struct oPGeoBucket));
                   2012:     g->m = -1;
                   2013:     return g;
                   2014: }
                   2015:
                   2016: void free_pbucket(PGeoBucket b) {
                   2017:     int i;
                   2018:
                   2019:     for ( i = 0; i <= b->m; i++ )
                   2020:         if ( b->body[i] ) {
                   2021:             nd_free(b->body[i]);
                   2022:             b->body[i] = 0;
                   2023:         }
                   2024:     GCFREE(b);
                   2025: }
                   2026:
                   2027: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   2028: {
                   2029:     int l,i,k,m;
                   2030:
                   2031:     if ( !d )
                   2032:         return;
                   2033:     l = LEN(d);
                   2034:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2035:     /* 2^(k-1) < l <= 2^k (=m) */
                   2036:     d = nd_merge(g->body[k],d);
                   2037:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   2038:         g->body[k] = 0;
                   2039:         d = nd_merge(g->body[k+1],d);
                   2040:     }
                   2041:     g->body[k] = d;
                   2042:     g->m = MAX(g->m,k);
                   2043: }
                   2044:
                   2045: void add_pbucket(int mod,PGeoBucket g,ND d)
                   2046: {
                   2047:     int l,i,k,m;
                   2048:
                   2049:     if ( !d )
                   2050:         return;
                   2051:     l = LEN(d);
                   2052:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2053:     /* 2^(k-1) < l <= 2^k (=m) */
                   2054:     d = nd_add(mod,g->body[k],d);
                   2055:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   2056:         g->body[k] = 0;
                   2057:         d = nd_add(mod,g->body[k+1],d);
                   2058:     }
                   2059:     g->body[k] = d;
                   2060:     g->m = MAX(g->m,k);
                   2061: }
                   2062:
                   2063: void mulq_pbucket(PGeoBucket g,Z c)
                   2064: {
                   2065:     int k;
                   2066:
                   2067:     for ( k = 0; k <= g->m; k++ )
                   2068:         nd_mul_c_q(g->body[k],(P)c);
                   2069: }
                   2070:
                   2071: NM remove_head_pbucket_symbolic(PGeoBucket g)
                   2072: {
                   2073:     int j,i,k,c;
                   2074:     NM head;
                   2075:
                   2076:     k = g->m;
                   2077:     j = -1;
                   2078:     for ( i = 0; i <= k; i++ ) {
                   2079:         if ( !g->body[i] ) continue;
                   2080:         if ( j < 0 ) j = i;
                   2081:         else {
                   2082:             c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
                   2083:             if ( c > 0 )
                   2084:                 j = i;
                   2085:             else if ( c == 0 )
                   2086:                 g->body[i] = nd_remove_head(g->body[i]);
                   2087:         }
                   2088:     }
                   2089:     if ( j < 0 ) return 0;
                   2090:     else {
                   2091:         head = BDY(g->body[j]);
                   2092:         if ( !NEXT(head) ) {
                   2093:             FREEND(g->body[j]);
                   2094:             g->body[j] = 0;
                   2095:         } else {
                   2096:             BDY(g->body[j]) = NEXT(head);
                   2097:             LEN(g->body[j])--;
                   2098:         }
                   2099:         return head;
                   2100:     }
                   2101: }
                   2102:
                   2103: int head_pbucket(int mod,PGeoBucket g)
                   2104: {
                   2105:     int j,i,c,k,nv,sum;
                   2106:     UINT *di,*dj;
                   2107:     ND gi,gj;
                   2108:
                   2109:     k = g->m;
                   2110:     while ( 1 ) {
                   2111:         j = -1;
                   2112:         for ( i = 0; i <= k; i++ ) {
                   2113:             if ( !(gi = g->body[i]) )
                   2114:                 continue;
                   2115:             if ( j < 0 ) {
                   2116:                 j = i;
                   2117:                 gj = g->body[j];
                   2118:                 dj = HDL(gj);
                   2119:                 sum = HCM(gj);
                   2120:             } else {
                   2121:                 c = DL_COMPARE(HDL(gi),dj);
                   2122:                 if ( c > 0 ) {
                   2123:                     if ( sum ) HCM(gj) = sum;
                   2124:                     else g->body[j] = nd_remove_head(gj);
                   2125:                     j = i;
                   2126:                     gj = g->body[j];
                   2127:                     dj = HDL(gj);
                   2128:                     sum = HCM(gj);
                   2129:                 } else if ( c == 0 ) {
                   2130:                     if ( mod == -1 )
                   2131:                         sum = _addsf(sum,HCM(gi));
                   2132:                     else {
                   2133:                         sum = sum+HCM(gi)-mod;
                   2134:                         if ( sum < 0 ) sum += mod;
                   2135:                     }
                   2136:                     g->body[i] = nd_remove_head(gi);
                   2137:                 }
                   2138:             }
                   2139:         }
                   2140:         if ( j < 0 ) return -1;
                   2141:         else if ( sum ) {
                   2142:             HCM(gj) = sum;
                   2143:             return j;
                   2144:         } else
                   2145:             g->body[j] = nd_remove_head(gj);
                   2146:     }
                   2147: }
                   2148:
                   2149: int head_pbucket_q(PGeoBucket g)
                   2150: {
                   2151:     int j,i,c,k,nv;
                   2152:     Z sum,t;
                   2153:     ND gi,gj;
                   2154:
                   2155:     k = g->m;
                   2156:     while ( 1 ) {
                   2157:         j = -1;
                   2158:         for ( i = 0; i <= k; i++ ) {
                   2159:             if ( !(gi = g->body[i]) ) continue;
                   2160:             if ( j < 0 ) {
                   2161:                 j = i;
                   2162:                 gj = g->body[j];
1.6       noro     2163:                 sum = HCZ(gj);
1.1       noro     2164:             } else {
                   2165:                 nv = NV(gi);
                   2166:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   2167:                 if ( c > 0 ) {
1.6       noro     2168:                     if ( sum ) HCZ(gj) = sum;
1.1       noro     2169:                     else g->body[j] = nd_remove_head(gj);
                   2170:                     j = i;
                   2171:                     gj = g->body[j];
1.6       noro     2172:                     sum = HCZ(gj);
1.1       noro     2173:                 } else if ( c == 0 ) {
1.6       noro     2174:                     addz(sum,HCZ(gi),&t);
1.1       noro     2175:                     sum = t;
                   2176:                     g->body[i] = nd_remove_head(gi);
                   2177:                 }
                   2178:             }
                   2179:         }
                   2180:         if ( j < 0 ) return -1;
                   2181:         else if ( sum ) {
1.6       noro     2182:             HCZ(gj) = sum;
1.1       noro     2183:             return j;
                   2184:         } else
                   2185:             g->body[j] = nd_remove_head(gj);
                   2186:     }
                   2187: }
                   2188:
                   2189: int head_pbucket_lf(PGeoBucket g)
                   2190: {
                   2191:     int j,i,c,k,nv;
                   2192:     Z sum,t;
                   2193:     ND gi,gj;
                   2194:
                   2195:     k = g->m;
                   2196:     while ( 1 ) {
                   2197:         j = -1;
                   2198:         for ( i = 0; i <= k; i++ ) {
                   2199:             if ( !(gi = g->body[i]) ) continue;
                   2200:             if ( j < 0 ) {
                   2201:                 j = i;
                   2202:                 gj = g->body[j];
                   2203:                 sum = HCZ(gj);
                   2204:             } else {
                   2205:                 nv = NV(gi);
                   2206:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   2207:                 if ( c > 0 ) {
                   2208:                     if ( sum ) HCZ(gj) = sum;
                   2209:                     else g->body[j] = nd_remove_head(gj);
                   2210:                     j = i;
                   2211:                     gj = g->body[j];
                   2212:                     sum = HCZ(gj);
                   2213:                 } else if ( c == 0 ) {
                   2214:                     addlf(sum,HCZ(gi),&t);
                   2215:                     sum = t;
                   2216:                     g->body[i] = nd_remove_head(gi);
                   2217:                 }
                   2218:             }
                   2219:         }
                   2220:         if ( j < 0 ) return -1;
                   2221:         else if ( sum ) {
                   2222:             HCZ(gj) = sum;
                   2223:             return j;
                   2224:         } else
                   2225:             g->body[j] = nd_remove_head(gj);
                   2226:     }
                   2227: }
                   2228:
                   2229: ND normalize_pbucket(int mod,PGeoBucket g)
                   2230: {
                   2231:     int i;
                   2232:     ND r,t;
                   2233:
                   2234:     r = 0;
                   2235:     for ( i = 0; i <= g->m; i++ ) {
                   2236:         r = nd_add(mod,r,g->body[i]);
                   2237:         g->body[i] = 0;
                   2238:     }
                   2239:     g->m = -1;
                   2240:     return r;
                   2241: }
                   2242:
                   2243: #if 0
                   2244: void register_hcf(NDV p)
                   2245: {
                   2246:     DCP dc,t;
                   2247:     P hc,h;
                   2248:     int c;
                   2249:     NODE l,l1,prev;
                   2250:
                   2251:     hc = p->body->c.p;
                   2252:     if ( !nd_vc || NUM(hc) ) return;
                   2253:     fctrp(nd_vc,hc,&dc);
                   2254:     for ( t = dc; t; t = NEXT(t) ) {
                   2255:         h = t->c;
                   2256:         if ( NUM(h) ) continue;
                   2257:         for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   2258:             c = compp(nd_vc,h,(P)BDY(l));
                   2259:             if ( c >= 0 ) break;
                   2260:         }
                   2261:         if ( !l || c > 0  ) {
                   2262:             MKNODE(l1,h,l);
                   2263:             if ( !prev )
                   2264:                 nd_hcf = l1;
                   2265:             else
                   2266:                 NEXT(prev) = l1;
                   2267:         }
                   2268:     }
                   2269: }
                   2270: #else
                   2271: void register_hcf(NDV p)
                   2272: {
                   2273:     DCP dc,t;
                   2274:     P hc,h,q;
                   2275:     Q dmy;
                   2276:     int c;
                   2277:     NODE l,l1,prev;
                   2278:
                   2279:     hc = p->body->c.p;
                   2280:     if ( NUM(hc) ) return;
                   2281:     ptozp(hc,1,&dmy,&h);
                   2282: #if 1
                   2283:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   2284:         while ( 1 ) {
                   2285:             if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
                   2286:             else break;
                   2287:         }
                   2288:     }
                   2289:     if ( NUM(h) ) return;
                   2290: #endif
                   2291:     for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   2292:         c = compp(nd_vc,h,(P)BDY(l));
                   2293:         if ( c >= 0 ) break;
                   2294:     }
                   2295:     if ( !l || c > 0  ) {
                   2296:         MKNODE(l1,h,l);
                   2297:         if ( !prev )
                   2298:             nd_hcf = l1;
                   2299:         else
                   2300:             NEXT(prev) = l1;
                   2301:     }
                   2302: }
                   2303: #endif
                   2304:
                   2305: int do_diagonalize(int sugar,int m)
                   2306: {
1.6       noro     2307:   int i,nh,stat;
                   2308:   NODE r,g,t;
                   2309:   ND h,nf,s,head;
                   2310:   NDV nfv;
                   2311:   Q q;
                   2312:   P nm,nmp,dn,mnp,dnp,cont,cont1;
                   2313:   union oNDC hc;
                   2314:   NODE node;
                   2315:   LIST l;
                   2316:   Z iq;
1.1       noro     2317:
1.6       noro     2318:   for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   2319:     if ( nd_gentrace ) {
                   2320:       /* Trace = [1,index,1,1] */
                   2321:       STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2322:       MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2323:     }
                   2324:     if ( nd_demand )
                   2325:       nfv = ndv_load(i);
                   2326:     else
                   2327:       nfv = nd_ps[i];
                   2328:     s = ndvtond(m,nfv);
                   2329:     s = nd_separate_head(s,&head);
                   2330:     stat = nd_nf(m,head,s,nd_ps,1,&nf);
                   2331:     if ( !stat ) return 0;
                   2332:     ndv_free(nfv);
                   2333:     hc = HCU(nf); nd_removecont(m,nf);
                   2334:     /* exact division */
                   2335:     cont = ndc_div(m,hc,HCU(nf));
1.1       noro     2336:     if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6       noro     2337:     nfv = ndtondv(m,nf);
                   2338:     nd_free(nf);
                   2339:     nd_bound[i] = ndv_compute_bound(nfv);
                   2340:     if ( !m ) register_hcf(nfv);
                   2341:     if ( nd_demand ) {
                   2342:       ndv_save(nfv,i);
                   2343:       ndv_free(nfv);
                   2344:     } else
                   2345:       nd_ps[i] = nfv;
                   2346:   }
                   2347:   return 1;
1.1       noro     2348: }
                   2349:
                   2350: LIST compute_splist()
                   2351: {
                   2352:   NODE g,tn0,tn,node;
                   2353:   LIST l0;
                   2354:   ND_pairs d,t;
                   2355:   int i;
                   2356:   Z i1,i2;
                   2357:
                   2358:     g = 0; d = 0;
                   2359:     for ( i = 0; i < nd_psn; i++ ) {
                   2360:         d = update_pairs(d,g,i,0);
                   2361:         g = update_base(g,i);
                   2362:     }
                   2363:   for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
                   2364:     NEXTNODE(tn0,tn);
1.6       noro     2365:         STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1       noro     2366:         node = mknode(2,i1,i2); MKLIST(l0,node);
                   2367:     BDY(tn) = l0;
                   2368:   }
                   2369:   if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
                   2370:   return l0;
                   2371: }
                   2372:
                   2373: /* return value = 0 => input is not a GB */
                   2374:
                   2375: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
                   2376: {
1.6       noro     2377:   int i,nh,sugar,stat;
                   2378:   NODE r,g,t;
                   2379:   ND_pairs d;
                   2380:   ND_pairs l;
                   2381:   ND h,nf,s,head,nf1;
                   2382:   NDV nfv;
                   2383:   Z q;
                   2384:   union oNDC dn,hc;
                   2385:   int diag_count = 0;
                   2386:   P cont;
                   2387:   LIST list;
1.27      noro     2388: struct oEGT eg1,eg2,eg_update;
1.6       noro     2389:
1.27      noro     2390: init_eg(&eg_update);
1.11      noro     2391:   Nnd_add = 0;
1.6       noro     2392:   g = 0; d = 0;
                   2393:   for ( i = 0; i < nd_psn; i++ ) {
                   2394:     d = update_pairs(d,g,i,gensyz);
                   2395:     g = update_base(g,i);
                   2396:   }
                   2397:   sugar = 0;
                   2398:   while ( d ) {
1.1       noro     2399: again:
1.6       noro     2400:     l = nd_minp(d,&d);
                   2401:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   2402:     if ( SG(l) != sugar ) {
                   2403:       if ( ishomo ) {
                   2404:         diag_count = 0;
                   2405:         stat = do_diagonalize(sugar,m);
1.1       noro     2406:         if ( !stat ) {
1.6       noro     2407:           NEXT(l) = d; d = l;
                   2408:           d = nd_reconstruct(0,d);
                   2409:           goto again;
1.1       noro     2410:         }
1.6       noro     2411:       }
                   2412:       sugar = SG(l);
                   2413:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2414:     }
                   2415:     stat = nd_sp(m,0,l,&h);
                   2416:     if ( !stat ) {
                   2417:       NEXT(l) = d; d = l;
                   2418:       d = nd_reconstruct(0,d);
                   2419:       goto again;
                   2420:     }
1.1       noro     2421: #if USE_GEOBUCKET
1.6       noro     2422:     stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!Top,&nf)
                   2423:       :nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1       noro     2424: #else
1.6       noro     2425:     stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1       noro     2426: #endif
1.6       noro     2427:     if ( !stat ) {
                   2428:       NEXT(l) = d; d = l;
                   2429:       d = nd_reconstruct(0,d);
                   2430:       goto again;
                   2431:     } else if ( nf ) {
                   2432:       if ( checkonly || gensyz ) return 0;
1.1       noro     2433:       if ( nd_newelim ) {
                   2434:         if ( nd_module ) {
                   2435:           if ( MPOS(HDL(nf)) > 1 ) return 0;
                   2436:         } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
                   2437:       }
1.6       noro     2438:       if ( DP_Print ) { printf("+"); fflush(stdout); }
                   2439:       hc = HCU(nf);
                   2440:       nd_removecont(m,nf);
                   2441:       if ( !m && nd_nalg ) {
                   2442:         nd_monic(0,&nf);
                   2443:         nd_removecont(m,nf);
                   2444:       }
                   2445:       if ( nd_gentrace ) {
                   2446:         /* exact division */
1.1       noro     2447:         cont = ndc_div(m,hc,HCU(nf));
                   2448:         if ( m || !UNIQ(cont) ) {
1.6       noro     2449:           t = mknode(4,NULLP,NULLP,NULLP,cont);
                   2450:           MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1       noro     2451:           nd_tracelist = t;
                   2452:         }
                   2453:       }
1.6       noro     2454:       nfv = ndtondv(m,nf); nd_free(nf);
1.24      noro     2455:       nh = ndv_newps(m,nfv,0);
1.6       noro     2456:       if ( !m && (ishomo && ++diag_count == diag_period) ) {
                   2457:         diag_count = 0;
                   2458:         stat = do_diagonalize(sugar,m);
                   2459:         if ( !stat ) {
                   2460:           NEXT(l) = d; d = l;
                   2461:           d = nd_reconstruct(1,d);
                   2462:           goto again;
1.1       noro     2463:         }
1.6       noro     2464:       }
1.27      noro     2465: get_eg(&eg1);
1.6       noro     2466:       d = update_pairs(d,g,nh,0);
1.27      noro     2467: get_eg(&eg2); add_eg(&eg_update,&eg1,&eg2);
1.6       noro     2468:       g = update_base(g,nh);
                   2469:       FREENDP(l);
                   2470:    } else {
                   2471:      if ( nd_gentrace && gensyz ) {
                   2472:        nd_tracelist = reverse_node(nd_tracelist);
                   2473:        MKLIST(list,nd_tracelist);
                   2474:        STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
                   2475:        MKNODE(t,list,nd_alltracelist);
                   2476:        nd_alltracelist = t; nd_tracelist = 0;
                   2477:      }
                   2478:      if ( DP_Print ) { printf("."); fflush(stdout); }
                   2479:        FREENDP(l);
                   2480:    }
                   2481:  }
                   2482:  conv_ilist(nd_demand,0,g,indp);
1.11      noro     2483:     if ( !checkonly && DP_Print ) { printf("nd_gb done. Number of nd_add=%d\n",Nnd_add); fflush(stdout); }
1.27      noro     2484: print_eg("update",&eg_update);
1.1       noro     2485:     return g;
                   2486: }
                   2487:
1.30      noro     2488: ND_pairs update_pairs_s(ND_pairs d,int t,NODE *syz);
                   2489: ND_pairs nd_newpairs_s(int t ,NODE *syz);
1.24      noro     2490:
                   2491: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *nf);
                   2492: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
                   2493:
                   2494: void _copydl(int n,DL d1,DL d2);
                   2495: void _subfromdl(int n,DL d1,DL d2);
                   2496: extern int (*cmpdl)(int n,DL d1,DL d2);
                   2497:
                   2498: NODE insert_sig(NODE l,SIG s)
                   2499: {
                   2500:   int pos;
                   2501:   DL sig;
                   2502:   struct oNODE root;
                   2503:   NODE p,prev,r;
                   2504:   SIG t;
                   2505:
                   2506:   pos = s->pos; sig = DL(s);
                   2507:   root.next = l; prev = &root;
                   2508:   for ( p = l; p; p = p->next ) {
                   2509:     t = (SIG)p->body;
                   2510:     if ( t->pos == pos ) {
                   2511:       if ( _dl_redble(DL(t),sig,nd_nvar) )
                   2512:         return root.next;
                   2513:       else if ( _dl_redble(sig,DL(t),nd_nvar) )
                   2514:         // remove p
                   2515:         prev->next = p->next;
                   2516:     } else
                   2517:       prev = p;
                   2518:   }
1.27      noro     2519:   NEWNODE(r); r->body = (pointer)s; r->next = 0;
                   2520:   for ( p = &root; p->next; p = p->next );
                   2521:   p->next = r;
                   2522: //  r->next = root.next;
                   2523: //  return r;
                   2524:   return root.next;
1.24      noro     2525: }
                   2526:
                   2527: ND_pairs remove_spair_s(ND_pairs d,SIG sig)
                   2528: {
                   2529:   struct oND_pairs root;
                   2530:   ND_pairs prev,p;
                   2531:   SIG spsig;
                   2532:
                   2533:   root.next = d;
                   2534:   prev = &root; p = d;
                   2535:   while ( p ) {
                   2536:     spsig = p->sig;
1.26      noro     2537:     if ( sig->pos == spsig->pos && _dl_redble(DL(sig),DL(spsig),nd_nvar) ) {
1.24      noro     2538:       // remove p
                   2539:       prev->next = p->next;
1.26      noro     2540:       Nsyz++;
                   2541:     } else
1.24      noro     2542:       prev = p;
                   2543:     p = p->next;
                   2544:   }
                   2545:   return (ND_pairs)root.next;
                   2546: }
                   2547:
1.29      noro     2548: int _dl_redble_ext(DL,DL,DL,int);
                   2549:
1.28      noro     2550: int small_lcm(ND_pairs l)
                   2551: {
                   2552:   SIG sig;
                   2553:   int i;
1.29      noro     2554:   NODE t;
1.28      noro     2555:   static DL lcm,mul,quo;
                   2556:   static int nvar;
                   2557:
                   2558:   if ( nvar < nd_nvar ) {
                   2559:     nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(quo,nvar); NEWDL(mul,nvar);
                   2560:   }
                   2561:   sig = l->sig;
                   2562:   _ndltodl(l->lcm,lcm);
1.29      noro     2563: #if 0
1.28      noro     2564:   for ( i = 0; i < nd_psn; i++ ) {
                   2565:     if ( sig->pos == nd_psh[i]->sig->pos &&
1.29      noro     2566:       _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
1.28      noro     2567:       _ndltodl(DL(nd_psh[i]),mul);
                   2568:       _addtodl(nd_nvar,quo,mul);
                   2569:       if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
                   2570:         break;
                   2571:     }
                   2572:   }
                   2573:   if ( i < nd_psn ) return 1;
                   2574:   else return 0;
1.29      noro     2575: #else
                   2576:   for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
                   2577:     i = (long)BDY(t);
                   2578:     if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
                   2579:       _ndltodl(DL(nd_psh[i]),mul);
                   2580:       _addtodl(nd_nvar,quo,mul);
                   2581:       if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
                   2582:         break;
                   2583:     }
                   2584:   }
                   2585:   if ( t ) return 1;
                   2586:   else return 0;
                   2587: #endif
1.28      noro     2588: }
                   2589:
                   2590: ND_pairs remove_large_lcm(ND_pairs d)
                   2591: {
                   2592:   struct oND_pairs root;
                   2593:   ND_pairs prev,p;
                   2594:
                   2595:   root.next = d;
                   2596:   prev = &root; p = d;
                   2597:   while ( p ) {
                   2598:     if ( small_lcm(p) ) {
                   2599:       // remove p
                   2600:       prev->next = p->next;
                   2601:     } else
                   2602:       prev = p;
                   2603:     p = p->next;
                   2604:   }
                   2605:   return (ND_pairs)root.next;
                   2606: }
                   2607:
1.26      noro     2608: struct oEGT eg_create,eg_newpairs,eg_merge;
                   2609:
1.30      noro     2610: NODE conv_ilist_s(int demand,int trace,int **indp);
                   2611:
1.24      noro     2612: NODE nd_sba_buch(int m,int ishomo,int **indp)
                   2613: {
1.29      noro     2614:   int i,j,nh,sugar,stat;
1.30      noro     2615:   NODE r,t,g;
1.24      noro     2616:   ND_pairs d;
                   2617:   ND_pairs l;
                   2618:   ND h,nf,s,head,nf1;
                   2619:   NDV nfv;
                   2620:   Z q;
                   2621:   union oNDC dn,hc;
                   2622:   P cont;
                   2623:   LIST list;
                   2624:   SIG sig;
1.29      noro     2625:   NODE *syzlist;
1.26      noro     2626:   int Nredundant;
1.28      noro     2627:   DL lcm,quo,mul;
1.31      noro     2628:   struct oEGT eg1,eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.24      noro     2629:
1.27      noro     2630: init_eg(&eg_remove);
1.29      noro     2631:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.26      noro     2632:   Nsyz = 0;
1.24      noro     2633:   Nnd_add = 0;
1.26      noro     2634:   Nredundant = 0;
1.30      noro     2635:   d = 0;
1.24      noro     2636:   for ( i = 0; i < nd_psn; i++ ) {
1.30      noro     2637:     d = update_pairs_s(d,i,syzlist);
1.24      noro     2638:   }
1.29      noro     2639:   for ( i = 0; i < nd_psn; i++ )
                   2640:     for ( j = i+1; j < nd_psn; j++ ) {
                   2641:       NEWSIG(sig); sig->pos = j;
                   2642:       _copydl(nd_nvar,nd_sba_hm[i],sig->dl);
                   2643:       syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
                   2644:     }
1.24      noro     2645:   sugar = 0;
1.26      noro     2646:   NEWDL(lcm,nd_nvar); NEWDL(quo,nd_nvar); NEWDL(mul,nd_nvar);
                   2647: init_eg(&eg_create);
                   2648: init_eg(&eg_merge);
1.29      noro     2649: init_eg(&eg_large);
1.31      noro     2650: init_eg(&eg_nf);
                   2651: init_eg(&eg_nfzero);
1.24      noro     2652:   while ( d ) {
                   2653: again:
1.26      noro     2654:     if ( DP_Print ) {
                   2655:       int len;
                   2656:       ND_pairs td;
1.28      noro     2657:       for ( td = d, len=0; td; td = td->next, len++)
                   2658:         ;
1.26      noro     2659:        if ( !(len%100) ) fprintf(asir_out,"(%d)",len);
                   2660:       }
1.24      noro     2661:     l = d; d = d->next;
1.28      noro     2662:     if ( small_lcm(l) ) {
1.24      noro     2663:       if ( DP_Print ) fprintf(asir_out,"M");
1.26      noro     2664:       Nredundant++;
1.24      noro     2665:       continue;
                   2666:     }
                   2667:     if ( SG(l) != sugar ) {
                   2668:       sugar = SG(l);
                   2669:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2670:     }
1.28      noro     2671:     sig = l->sig;
1.24      noro     2672:     stat = nd_sp(m,0,l,&h);
                   2673:     if ( !stat ) {
                   2674:       NEXT(l) = d; d = l;
                   2675:       d = nd_reconstruct(0,d);
                   2676:       goto again;
                   2677:     }
1.31      noro     2678: get_eg(&eg1);
1.25      noro     2679: #if USE_GEOBUCKET
1.24      noro     2680:     stat = m?nd_nf_pbucket_s(m,h,nd_ps,!Top,&nf):nd_nf_s(m,0,h,nd_ps,!Top,&nf);
                   2681: #else
                   2682:     stat = nd_nf_s(m,0,h,nd_ps,!Top,&nf);
                   2683: #endif
1.31      noro     2684: get_eg(&eg2);
1.24      noro     2685:     if ( !stat ) {
                   2686:       NEXT(l) = d; d = l;
                   2687:       d = nd_reconstruct(0,d);
                   2688:       goto again;
                   2689:     } else if ( stat == -1 ) {
                   2690:       if ( DP_Print ) { printf("S"); fflush(stdout); }
1.27      noro     2691:       FREENDP(l);
1.24      noro     2692:     } else if ( nf ) {
                   2693:       if ( DP_Print ) { printf("+"); fflush(stdout); }
1.31      noro     2694:       add_eg(&eg_nf,&eg1,&eg2);
1.24      noro     2695:       hc = HCU(nf);
                   2696:       nd_removecont(m,nf);
                   2697:       nfv = ndtondv(m,nf); nd_free(nf);
                   2698:       nh = ndv_newps(m,nfv,0);
1.26      noro     2699:
1.30      noro     2700:       d = update_pairs_s(d,nh,syzlist);
1.29      noro     2701:       nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.24      noro     2702:       FREENDP(l);
                   2703:    } else {
1.31      noro     2704:       add_eg(&eg_nfzero,&eg1,&eg2);
1.24      noro     2705:      // syzygy
1.27      noro     2706: get_eg(&eg1);
1.24      noro     2707:      d = remove_spair_s(d,sig);
1.27      noro     2708: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29      noro     2709:      syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.24      noro     2710:      if ( DP_Print ) { printf("."); fflush(stdout); }
                   2711:      FREENDP(l);
                   2712:    }
                   2713:  }
1.30      noro     2714:  g = conv_ilist_s(nd_demand,0,indp);
1.26      noro     2715:  if ( DP_Print ) {
                   2716:    printf("\nnd_sba done. nd_add=%d,Nsyz=%d,Nredundant=%d\n",Nnd_add,Nsyz,Nredundant);
                   2717:    fflush(stdout);
                   2718:    print_eg("create",&eg_create);
                   2719:    print_eg("merge",&eg_merge);
1.27      noro     2720:    print_eg("remove",&eg_remove);
1.31      noro     2721:    print_eg("nf",&eg_nf);
                   2722:    print_eg("nfzero",&eg_nfzero);
1.27      noro     2723:    printf("\n");
1.26      noro     2724:  }
1.24      noro     2725:  return g;
                   2726: }
                   2727:
1.1       noro     2728: /* splist = [[i1,i2],...] */
                   2729:
                   2730: int check_splist(int m,NODE splist)
                   2731: {
                   2732:   NODE t,p;
                   2733:   ND_pairs d,r,l;
                   2734:   int stat;
                   2735:   ND h,nf;
                   2736:
                   2737:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   2738:     p = BDY((LIST)BDY(t));
1.6       noro     2739:     NEXTND_pairs(d,r);
                   2740:     r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
                   2741:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     2742:     SG(r) = TD(LCM(r)); /* XXX */
                   2743:   }
                   2744:   if ( d ) NEXT(r) = 0;
                   2745:
1.6       noro     2746:   while ( d ) {
1.1       noro     2747: again:
1.6       noro     2748:     l = nd_minp(d,&d);
                   2749:     stat = nd_sp(m,0,l,&h);
                   2750:     if ( !stat ) {
                   2751:       NEXT(l) = d; d = l;
                   2752:       d = nd_reconstruct(0,d);
                   2753:       goto again;
                   2754:     }
                   2755:     stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
                   2756:     if ( !stat ) {
                   2757:       NEXT(l) = d; d = l;
                   2758:       d = nd_reconstruct(0,d);
                   2759:       goto again;
                   2760:     } else if ( nf ) return 0;
1.1       noro     2761:     if ( DP_Print) { printf("."); fflush(stdout); }
1.6       noro     2762:   }
1.1       noro     2763:   if ( DP_Print) { printf("done.\n"); fflush(stdout); }
                   2764:   return 1;
                   2765: }
                   2766:
                   2767: int check_splist_f4(int m,NODE splist)
                   2768: {
                   2769:   UINT *s0vect;
1.6       noro     2770:   PGeoBucket bucket;
1.1       noro     2771:   NODE p,rp0,t;
                   2772:   ND_pairs d,r,l,ll;
                   2773:   int col,stat;
                   2774:
                   2775:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   2776:     p = BDY((LIST)BDY(t));
1.6       noro     2777:     NEXTND_pairs(d,r);
                   2778:     r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
                   2779:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     2780:     SG(r) = TD(LCM(r)); /* XXX */
                   2781:   }
                   2782:   if ( d ) NEXT(r) = 0;
                   2783:
1.6       noro     2784:   while ( d ) {
                   2785:     l = nd_minsugarp(d,&d);
                   2786:     bucket = create_pbucket();
                   2787:     stat = nd_sp_f4(m,0,l,bucket);
                   2788:     if ( !stat ) {
                   2789:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   2790:       NEXT(ll) = d; d = l;
                   2791:       d = nd_reconstruct(0,d);
                   2792:       continue;
                   2793:     }
                   2794:     if ( bucket->m < 0 ) continue;
                   2795:     col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   2796:     if ( !col ) {
                   2797:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   2798:       NEXT(ll) = d; d = l;
                   2799:       d = nd_reconstruct(0,d);
                   2800:       continue;
1.1       noro     2801:     }
1.6       noro     2802:     if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
                   2803:   }
                   2804:   return 1;
1.1       noro     2805: }
                   2806:
                   2807: int do_diagonalize_trace(int sugar,int m)
                   2808: {
1.6       noro     2809:   int i,nh,stat;
                   2810:   NODE r,g,t;
                   2811:   ND h,nf,nfq,s,head;
                   2812:   NDV nfv,nfqv;
                   2813:   Q q,den,num;
                   2814:   union oNDC hc;
                   2815:   NODE node;
                   2816:   LIST l;
                   2817:   Z iq;
                   2818:   P cont,cont1;
1.1       noro     2819:
1.6       noro     2820:   for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   2821:     if ( nd_gentrace ) {
                   2822:         /* Trace = [1,index,1,1] */
                   2823:         STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2824:         MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2825:     }
                   2826:     /* for nd_ps */
                   2827:     s = ndvtond(m,nd_ps[i]);
                   2828:     s = nd_separate_head(s,&head);
                   2829:     stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
                   2830:     if ( !stat ) return 0;
                   2831:     nf = nd_add(m,head,nf);
                   2832:     ndv_free(nd_ps[i]);
                   2833:     nd_ps[i] = ndtondv(m,nf);
                   2834:     nd_free(nf);
                   2835:
                   2836:     /* for nd_ps_trace */
                   2837:     if ( nd_demand )
                   2838:         nfv = ndv_load(i);
                   2839:     else
                   2840:         nfv = nd_ps_trace[i];
                   2841:     s = ndvtond(0,nfv);
                   2842:     s = nd_separate_head(s,&head);
                   2843:     stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
                   2844:     if ( !stat ) return 0;
                   2845:     ndv_free(nfv);
                   2846:     hc = HCU(nf); nd_removecont(0,nf);
                   2847:     /* exact division */
1.1       noro     2848:     cont = ndc_div(0,hc,HCU(nf));
1.6       noro     2849:     if ( nd_gentrace ) finalize_tracelist(i,cont);
                   2850:     nfv = ndtondv(0,nf);
                   2851:     nd_free(nf);
                   2852:     nd_bound[i] = ndv_compute_bound(nfv);
                   2853:     register_hcf(nfv);
                   2854:     if ( nd_demand ) {
                   2855:     ndv_save(nfv,i);
                   2856:     ndv_free(nfv);
                   2857:     } else
                   2858:     nd_ps_trace[i] = nfv;
                   2859:   }
                   2860:   return 1;
1.1       noro     2861: }
                   2862:
                   2863: static struct oEGT eg_invdalg;
                   2864: struct oEGT eg_le;
                   2865:
                   2866: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
                   2867: {
                   2868:     NODE tn;
                   2869:     P p1;
                   2870:
                   2871:     for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
                   2872:         substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
                   2873:     }
                   2874:     *r = p;
                   2875: }
                   2876:
                   2877: NODE nd_gb_trace(int m,int ishomo,int **indp)
                   2878: {
1.6       noro     2879:   int i,nh,sugar,stat;
                   2880:   NODE r,g,t;
                   2881:   ND_pairs d;
                   2882:   ND_pairs l;
                   2883:   ND h,nf,nfq,s,head;
                   2884:   NDV nfv,nfqv;
                   2885:   Z q,den,num;
                   2886:   P hc;
                   2887:   union oNDC dn,hnfq;
                   2888:   struct oEGT eg_monic,egm0,egm1;
                   2889:   int diag_count = 0;
                   2890:   P cont;
                   2891:   LIST list;
                   2892:
                   2893:   init_eg(&eg_monic);
                   2894:   init_eg(&eg_invdalg);
                   2895:   init_eg(&eg_le);
                   2896:   g = 0; d = 0;
                   2897:   for ( i = 0; i < nd_psn; i++ ) {
                   2898:     d = update_pairs(d,g,i,0);
                   2899:     g = update_base(g,i);
                   2900:   }
                   2901:   sugar = 0;
                   2902:   while ( d ) {
1.1       noro     2903: again:
1.6       noro     2904:     l = nd_minp(d,&d);
                   2905:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   2906:     if ( SG(l) != sugar ) {
1.1       noro     2907: #if 1
1.6       noro     2908:       if ( ishomo ) {
                   2909:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2910:         stat = do_diagonalize_trace(sugar,m);
                   2911:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2912:         diag_count = 0;
1.1       noro     2913:         if ( !stat ) {
1.6       noro     2914:           NEXT(l) = d; d = l;
                   2915:           d = nd_reconstruct(1,d);
                   2916:           goto again;
1.1       noro     2917:         }
1.6       noro     2918:       }
                   2919: #endif
                   2920:       sugar = SG(l);
                   2921:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2922:     }
                   2923:     stat = nd_sp(m,0,l,&h);
                   2924:     if ( !stat ) {
                   2925:       NEXT(l) = d; d = l;
                   2926:       d = nd_reconstruct(1,d);
                   2927:       goto again;
                   2928:     }
1.1       noro     2929: #if USE_GEOBUCKET
1.6       noro     2930:     stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.1       noro     2931: #else
1.6       noro     2932:     stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1       noro     2933: #endif
1.6       noro     2934:     if ( !stat ) {
                   2935:       NEXT(l) = d; d = l;
                   2936:       d = nd_reconstruct(1,d);
                   2937:       goto again;
                   2938:     } else if ( nf ) {
                   2939:       if ( nd_demand ) {
                   2940:         nfqv = ndv_load(nd_psn);
                   2941:         nfq = ndvtond(0,nfqv);
                   2942:       } else
                   2943:         nfq = 0;
                   2944:       if ( !nfq ) {
                   2945:         if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!Top,&nfq) ) {
                   2946:           NEXT(l) = d; d = l;
                   2947:           d = nd_reconstruct(1,d);
                   2948:           goto again;
                   2949:         }
                   2950:       }
                   2951:       if ( nfq ) {
                   2952:         /* m|HC(nfq) => failure */
                   2953:         if ( nd_vc ) {
                   2954:           nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
                   2955:         } else
                   2956:           q = HCZ(nfq);
                   2957:         if ( !remqi((Q)q,m) ) return 0;
                   2958:
                   2959:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   2960:         hnfq = HCU(nfq);
                   2961:         if ( nd_nalg ) {
                   2962:           /* m|DN(HC(nf)^(-1)) => failure */
                   2963:           get_eg(&egm0);
                   2964:           if ( !nd_monic(m,&nfq) ) return 0;
                   2965:           get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
                   2966:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   2967:           nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
                   2968:         } else {
                   2969:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   2970:           nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
                   2971:         }
                   2972:         if ( nd_gentrace ) {
                   2973:           /* exact division */
                   2974:           cont = ndc_div(0,hnfq,HCU(nfqv));
                   2975:           if ( !UNIQ(cont) ) {
                   2976:             t = mknode(4,NULLP,NULLP,NULLP,cont);
                   2977:             MKLIST(list,t); MKNODE(t,list,nd_tracelist);
                   2978:             nd_tracelist = t;
                   2979:           }
                   2980:         }
1.24      noro     2981:         nh = ndv_newps(0,nfv,nfqv);
1.6       noro     2982:         if ( ishomo && ++diag_count == diag_period ) {
                   2983:           diag_count = 0;
                   2984:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2985:           stat = do_diagonalize_trace(sugar,m);
                   2986:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2987:           if ( !stat ) {
1.1       noro     2988:             NEXT(l) = d; d = l;
                   2989:             d = nd_reconstruct(1,d);
                   2990:             goto again;
1.6       noro     2991:           }
1.1       noro     2992:         }
1.6       noro     2993:         d = update_pairs(d,g,nh,0);
                   2994:         g = update_base(g,nh);
                   2995:       } else {
                   2996:         if ( DP_Print ) { printf("*"); fflush(stdout); }
                   2997:       }
                   2998:     } else {
                   2999:       if ( DP_Print ) { printf("."); fflush(stdout); }
1.1       noro     3000:     }
1.6       noro     3001:     FREENDP(l);
                   3002:   }
                   3003:   if ( nd_nalg ) {
                   3004:     if ( DP_Print ) {
                   3005:       print_eg("monic",&eg_monic);
                   3006:       print_eg("invdalg",&eg_invdalg);
                   3007:       print_eg("le",&eg_le);
1.1       noro     3008:     }
1.6       noro     3009:   }
1.1       noro     3010:   conv_ilist(nd_demand,1,g,indp);
1.6       noro     3011:   if ( DP_Print ) { printf("nd_gb_trace done.\n"); fflush(stdout); }
                   3012:   return g;
1.1       noro     3013: }
                   3014:
                   3015: int ndv_compare(NDV *p1,NDV *p2)
                   3016: {
                   3017:     return DL_COMPARE(HDL(*p1),HDL(*p2));
                   3018: }
                   3019:
                   3020: int ndv_compare_rev(NDV *p1,NDV *p2)
                   3021: {
                   3022:     return -DL_COMPARE(HDL(*p1),HDL(*p2));
                   3023: }
                   3024:
                   3025: int ndvi_compare(NDVI p1,NDVI p2)
                   3026: {
                   3027:     return DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3028: }
                   3029:
                   3030: int ndvi_compare_rev(NDVI p1,NDVI p2)
                   3031: {
                   3032:     return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3033: }
                   3034:
                   3035: NODE ndv_reduceall(int m,NODE f)
                   3036: {
                   3037:   int i,j,n,stat;
                   3038:   ND nf,g,head;
                   3039:   NODE t,a0,a;
                   3040:   union oNDC dn;
                   3041:   Q q,num,den;
                   3042:   NODE node;
                   3043:   LIST l;
                   3044:   Z iq,jq;
                   3045:   int *perm;
                   3046:   union oNDC hc;
                   3047:   P cont,cont1;
                   3048:
                   3049:   if ( nd_nora ) return f;
                   3050:   n = length(f);
1.24      noro     3051:   ndv_setup(m,0,f,0,1,0);
1.1       noro     3052:   perm = (int *)MALLOC(n*sizeof(int));
                   3053:   if ( nd_gentrace ) {
                   3054:     for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6       noro     3055:       perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1       noro     3056:   }
                   3057:   for ( i = 0; i < n; ) {
                   3058:     if ( nd_gentrace ) {
                   3059:       /* Trace = [1,index,1,1] */
1.6       noro     3060:       STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1       noro     3061:       MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   3062:     }
                   3063:     g = ndvtond(m,nd_ps[i]);
                   3064:     g = nd_separate_head(g,&head);
1.6       noro     3065:     stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1       noro     3066:     if ( !stat )
                   3067:       nd_reconstruct(0,0);
                   3068:     else {
                   3069:       if ( DP_Print ) { printf("."); fflush(stdout); }
                   3070:       ndv_free(nd_ps[i]);
                   3071:       hc = HCU(nf); nd_removecont(m,nf);
                   3072:       if ( nd_gentrace ) {
                   3073:         for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6       noro     3074:           jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
                   3075:           STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1       noro     3076:         }
1.6       noro     3077:         /* exact division */
1.1       noro     3078:         cont = ndc_div(m,hc,HCU(nf));
                   3079:         finalize_tracelist(perm[i],cont);
                   3080:       }
                   3081:       nd_ps[i] = ndtondv(m,nf); nd_free(nf);
                   3082:       nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   3083:       i++;
                   3084:     }
                   3085:   }
                   3086:   if ( DP_Print ) { printf("\n"); }
                   3087:   for ( a0 = 0, i = 0; i < n; i++ ) {
                   3088:     NEXTNODE(a0,a);
                   3089:     if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
                   3090:     else {
                   3091:       for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
                   3092:       BDY(a) = (pointer)nd_ps[j];
                   3093:     }
                   3094:   }
                   3095:   NEXT(a) = 0;
                   3096:   return a0;
                   3097: }
                   3098:
                   3099: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
                   3100: {
                   3101:   ND_pairs d1,nd,cur,head,prev,remove;
                   3102:
                   3103:   if ( !g ) return d;
                   3104:   /* for testing */
                   3105:   if ( gensyz && nd_gensyz == 2 ) {
                   3106:     d1 = nd_newpairs(g,t);
                   3107:     if ( !d )
                   3108:       return d1;
                   3109:     else {
                   3110:       nd = d;
                   3111:       while ( NEXT(nd) ) nd = NEXT(nd);
                   3112:       NEXT(nd) = d1;
                   3113:       return d;
                   3114:     }
                   3115:   }
                   3116:   d = crit_B(d,t);
                   3117:   d1 = nd_newpairs(g,t);
                   3118:   d1 = crit_M(d1);
                   3119:   d1 = crit_F(d1);
                   3120:   if ( gensyz || do_weyl )
                   3121:     head = d1;
                   3122:   else {
                   3123:     prev = 0; cur = head = d1;
                   3124:     while ( cur ) {
                   3125:       if ( crit_2( cur->i1,cur->i2 ) ) {
                   3126:         remove = cur;
                   3127:         if ( !prev ) head = cur = NEXT(cur);
                   3128:         else cur = NEXT(prev) = NEXT(cur);
                   3129:         FREENDP(remove);
                   3130:       } else {
                   3131:         prev = cur; cur = NEXT(cur);
                   3132:       }
                   3133:     }
                   3134:   }
                   3135:   if ( !d )
                   3136:     return head;
                   3137:   else {
                   3138:     nd = d;
                   3139:     while ( NEXT(nd) ) nd = NEXT(nd);
                   3140:     NEXT(nd) = head;
                   3141:     return d;
                   3142:   }
                   3143: }
                   3144:
1.24      noro     3145: ND_pairs merge_pairs_s(ND_pairs d,ND_pairs d1);
                   3146:
1.30      noro     3147: ND_pairs update_pairs_s( ND_pairs d, int t,NODE *syz)
1.24      noro     3148: {
                   3149:   ND_pairs d1;
1.26      noro     3150:   struct oEGT eg1,eg2,eg3;
1.24      noro     3151:
1.30      noro     3152:   if ( !t ) return d;
1.26      noro     3153: get_eg(&eg1);
1.30      noro     3154:   d1 = nd_newpairs_s(t,syz);
1.26      noro     3155: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
1.24      noro     3156:   d = merge_pairs_s(d,d1);
1.26      noro     3157: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
1.24      noro     3158:   return d;
                   3159: }
1.1       noro     3160:
                   3161: ND_pairs nd_newpairs( NODE g, int t )
                   3162: {
                   3163:   NODE h;
                   3164:   UINT *dl;
                   3165:   int ts,s,i,t0,min,max;
                   3166:   ND_pairs r,r0;
                   3167:
                   3168:   dl = DL(nd_psh[t]);
                   3169:   ts = SG(nd_psh[t]) - TD(dl);
1.17      noro     3170:   if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1       noro     3171:   for ( r0 = 0, h = g; h; h = NEXT(h) ) {
                   3172:     if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
                   3173:       continue;
                   3174:     if ( nd_gbblock ) {
                   3175:       t0 = (long)BDY(h);
                   3176:       for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
                   3177:         min = nd_gbblock[i]; max = nd_gbblock[i+1];
                   3178:         if ( t0 >= min && t0 <= max && t >= min && t <= max )
                   3179:           break;
                   3180:       }
                   3181:       if ( nd_gbblock[i] >= 0 )
                   3182:         continue;
                   3183:     }
                   3184:     NEXTND_pairs(r0,r);
                   3185:     r->i1 = (long)BDY(h);
                   3186:     r->i2 = t;
                   3187:     ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
                   3188:     s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   3189:     SG(r) = MAX(s,ts) + TD(LCM(r));
                   3190:     /* experimental */
                   3191:     if ( nd_sugarweight )
                   3192:       r->sugar2 = ndl_weight2(r->lcm);
                   3193:   }
                   3194:   if ( r0 ) NEXT(r) = 0;
                   3195:   return r0;
                   3196: }
                   3197:
1.24      noro     3198: int comp_sig(SIG s1,SIG s2)
                   3199: {
                   3200: #if 0
                   3201:   if ( s1->pos > s2->pos ) return 1;
                   3202:   else if ( s1->pos < s2->pos ) return -1;
                   3203:   else return (*cmpdl)(nd_nvar,s1->dl,s2->dl);
                   3204: #else
                   3205:   static DL m1,m2;
                   3206:   static int nvar;
                   3207:   int ret;
                   3208:
1.26      noro     3209:   if ( nvar != nd_nvar ) {
                   3210:     nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
1.24      noro     3211:   }
1.31      noro     3212: #if 0
1.27      noro     3213:   _copydl(nd_nvar,nd_sba_hm[s1->pos],m1);
                   3214:   _copydl(nd_nvar,nd_sba_hm[s2->pos],m2);
1.24      noro     3215:   _addtodl(nd_nvar,s1->dl,m1);
                   3216:   _addtodl(nd_nvar,s2->dl,m2);
1.31      noro     3217: #else
                   3218:   _adddl(nd_nvar,s1->dl,nd_sba_hm[s1->pos],m1);
                   3219:   _adddl(nd_nvar,s2->dl,nd_sba_hm[s2->pos],m2);
                   3220: #endif
1.24      noro     3221:   ret = (*cmpdl)(nd_nvar,m1,m2);
                   3222:   if ( ret != 0 ) return ret;
                   3223:   else if ( s1->pos > s2->pos ) return 1;
                   3224:   else if ( s1->pos < s2->pos ) return -1;
                   3225:   else return 0;
                   3226: #endif
                   3227: }
                   3228:
                   3229: int _create_spair_s(int i1,int i2,ND_pairs sp,SIG sig1,SIG sig2)
                   3230: {
                   3231:   int ret,s1,s2;
                   3232:   RHist p1,p2;
                   3233:   static int wpd;
                   3234:   static UINT *lcm;
                   3235:
                   3236:   sp->i1 = i1;
                   3237:   sp->i2 = i2;
                   3238:   p1 = nd_psh[i1];
                   3239:   p2 = nd_psh[i2];
                   3240:   ndl_lcm(DL(p1),DL(p2),sp->lcm);
                   3241:   s1 = SG(p1)-TD(DL(p1));
                   3242:   s2 = SG(p2)-TD(DL(p2));
                   3243:   SG(sp) = MAX(s1,s2) + TD(sp->lcm);
                   3244:
1.26      noro     3245:   if ( wpd != nd_wpd ) {
1.24      noro     3246:     wpd = nd_wpd;
                   3247:     lcm = (UINT *)MALLOC(wpd*sizeof(UINT));
                   3248:   }
                   3249:   // DL(sig1) <- sp->lcm
                   3250:   // DL(sig1) -= DL(p1)
                   3251:   // DL(sig1) += DL(p1->sig)
1.27      noro     3252:   ndl_sub(sp->lcm,DL(p1),lcm);
1.24      noro     3253:   _ndltodl(lcm,DL(sig1));
                   3254:   _addtodl(nd_nvar,DL(p1->sig),DL(sig1));
                   3255:   sig1->pos = p1->sig->pos;
                   3256:
                   3257:   // DL(sig2) <- sp->lcm
                   3258:   // DL(sig2) -= DL(p2)
                   3259:   // DL(sig2) += DL(p2->sig)
1.27      noro     3260:   ndl_sub(sp->lcm,DL(p2),lcm);
1.24      noro     3261:   _ndltodl(lcm,DL(sig2));
                   3262:   _addtodl(nd_nvar,DL(p2->sig),DL(sig2));
                   3263:   sig2->pos = p2->sig->pos;
                   3264:
                   3265:   ret = comp_sig(sig1,sig2);
                   3266:   if ( ret == 0 ) return 0;
                   3267:   else if ( ret > 0 ) sp->sig = sig1;
                   3268:   else sp->sig = sig2;
                   3269:   return 1;
                   3270: }
                   3271:
                   3272: SIG dup_sig(SIG sig)
                   3273: {
                   3274:   SIG r;
                   3275:
                   3276:   if ( !sig ) return 0;
                   3277:   else {
                   3278:     NEWSIG(r);
                   3279:     _copydl(nd_nvar,DL(sig),DL(r));
                   3280:     r->pos = sig->pos;
                   3281:     return r;
                   3282:   }
                   3283: }
                   3284:
                   3285: void dup_ND_pairs(ND_pairs to,ND_pairs from)
                   3286: {
                   3287:   to->i1 = from->i1;
                   3288:   to->i2 = from->i2;
                   3289:   to->sugar = from->sugar;
                   3290:   to->sugar2 = from->sugar2;
                   3291:   ndl_copy(from->lcm,to->lcm);
                   3292:   to->sig = dup_sig(from->sig);
                   3293: }
                   3294:
                   3295: ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
                   3296: {
                   3297:   struct oND_pairs root;
                   3298:   ND_pairs q1,q2,r0,r;
                   3299:   int ret;
                   3300:
                   3301:   r = &root;
                   3302:   for ( q1 = p1, q2 = p2; q1 != 0 && q2 != 0; ) {
                   3303:     ret = comp_sig(q1->sig,q2->sig);
                   3304:     if ( ret < 0 ) {
                   3305:       r->next = q1; r = q1; q1 = q1->next;
                   3306:     } else if ( ret > 0 ) {
                   3307:       r->next = q2; r = q2; q2 = q2->next;
                   3308:     } else {
                   3309:       ret = DL_COMPARE(q1->lcm,q2->lcm);
                   3310:       if ( ret < 0 ) {
                   3311:         r->next = q1; r = q1; q1 = q1->next;
                   3312:         q2 = q2->next;
                   3313:       } else {
                   3314:         r->next = q2; r = q2; q2 = q2->next;
                   3315:         q1 = q1->next;
                   3316:       }
                   3317:     }
                   3318:   }
                   3319:   if ( q1 ) {
                   3320:     r->next = q1;
                   3321:   } else {
                   3322:     r->next = q2;
                   3323:   }
                   3324:   return root.next;
                   3325: }
                   3326:
                   3327: ND_pairs insert_pair_s(ND_pairs l,ND_pairs s)
                   3328: {
                   3329:   ND_pairs p,prev;
                   3330:   int ret;
                   3331:
                   3332:   for ( p = l, prev = 0; p != 0; prev = p, p = p->next ) {
                   3333:     if ( (ret = comp_sig(s->sig,p->sig)) <= 0 )
                   3334:       break;
                   3335:   }
                   3336:   if ( ret == 0 ) {
                   3337:     ret = DL_COMPARE(s->lcm,p->lcm);
                   3338:     if ( ret < 0 ) {
                   3339:       // replace p with s
                   3340:       s->next = p->next;
                   3341:       if ( prev == 0 ) {
                   3342:         return s;
                   3343:       } else {
                   3344:         prev->next = s;
                   3345:         return l;
                   3346:       }
                   3347:     } else
                   3348:       return l;
                   3349:   } else {
                   3350:     // insert s between prev and p
                   3351:     s->next = p;
                   3352:     if ( prev == 0 ) {
                   3353:       return s;
                   3354:     } else {
                   3355:       prev->next = s;
                   3356:       return l;
                   3357:     }
                   3358:   }
                   3359: }
                   3360:
1.29      noro     3361: INLINE int __dl_redble(DL d1,DL d2,int nvar)
                   3362: {
                   3363:   int i;
                   3364:
                   3365:   if ( d1->td > d2->td )
                   3366:     return 0;
                   3367:   for ( i = nvar-1; i >= 0; i-- )
                   3368:     if ( d1->d[i] > d2->d[i] )
                   3369:       break;
                   3370:   if ( i >= 0 )
                   3371:     return 0;
                   3372:   else
                   3373:     return 1;
                   3374: }
                   3375:
1.30      noro     3376: ND_pairs nd_newpairs_s(int t, NODE *syz)
1.24      noro     3377: {
                   3378:   NODE h,s;
                   3379:   UINT *dl;
1.27      noro     3380:   int ts,ret,i;
1.24      noro     3381:   ND_pairs r,r0,_sp,sp;
1.29      noro     3382:   SIG spsig,tsig;
                   3383:   static int nvar;
                   3384:   static SIG _sig1,_sig2;
1.26      noro     3385:   struct oEGT eg1,eg2,eg3,eg4;
1.24      noro     3386:
                   3387:   NEWND_pairs(_sp);
1.29      noro     3388:   if ( !_sig1 || nvar != nd_nvar ) {
                   3389:     nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
                   3390:   }
1.24      noro     3391:   r0 = 0;
1.27      noro     3392:   for ( i = 0; i < t; i++ ) {
                   3393:     ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
1.24      noro     3394:     if ( ret ) {
                   3395:       spsig = _sp->sig;
1.29      noro     3396:       for ( s = syz[spsig->pos]; s; s = s->next ) {
1.24      noro     3397:         tsig = (SIG)s->body;
1.29      noro     3398:         if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
1.24      noro     3399:           break;
                   3400:       }
                   3401:       if ( s == 0 ) {
                   3402:         NEWND_pairs(sp);
                   3403:         dup_ND_pairs(sp,_sp);
                   3404:         r0 = insert_pair_s(r0,sp);
1.26      noro     3405:       } else
                   3406:         Nsyz++;
1.24      noro     3407:     }
                   3408:   }
                   3409:   return r0;
                   3410: }
                   3411:
1.1       noro     3412: /* ipair = [i1,i2],[i1,i2],... */
                   3413: ND_pairs nd_ipairtospair(NODE ipair)
                   3414: {
                   3415:   int s1,s2;
                   3416:   NODE tn,t;
                   3417:   ND_pairs r,r0;
                   3418:
                   3419:   for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
                   3420:     NEXTND_pairs(r0,r);
                   3421:     tn = BDY((LIST)BDY(t));
1.6       noro     3422:     r->i1 = ZTOS((Q)ARG0(tn));
                   3423:     r->i2 = ZTOS((Q)ARG1(tn));
1.1       noro     3424:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
                   3425:     s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   3426:     s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
                   3427:     SG(r) = MAX(s1,s2) + TD(LCM(r));
                   3428:     /* experimental */
                   3429:     if ( nd_sugarweight )
                   3430:         r->sugar2 = ndl_weight2(r->lcm);
                   3431:   }
                   3432:   if ( r0 ) NEXT(r) = 0;
                   3433:   return r0;
                   3434: }
                   3435:
                   3436: /* kokokara */
                   3437:
                   3438: ND_pairs crit_B( ND_pairs d, int s )
                   3439: {
                   3440:   ND_pairs cur,head,prev,remove;
                   3441:   UINT *t,*tl,*lcm;
                   3442:   int td,tdl;
                   3443:
                   3444:   if ( !d ) return 0;
                   3445:   t = DL(nd_psh[s]);
                   3446:   prev = 0;
                   3447:   head = cur = d;
                   3448:   lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   3449:   while ( cur ) {
                   3450:     tl = cur->lcm;
                   3451:     if ( ndl_reducible(tl,t) ) {
                   3452:       ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
                   3453:       if ( !ndl_equal(lcm,tl) ) {
                   3454:         ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
                   3455:         if (!ndl_equal(lcm,tl)) {
                   3456:           remove = cur;
                   3457:           if ( !prev ) {
                   3458:             head = cur = NEXT(cur);
                   3459:           } else {
                   3460:             cur = NEXT(prev) = NEXT(cur);
                   3461:           }
                   3462:           FREENDP(remove);
                   3463:         } else {
                   3464:           prev = cur; cur = NEXT(cur);
                   3465:         }
                   3466:       } else {
                   3467:         prev = cur; cur = NEXT(cur);
                   3468:       }
                   3469:     } else {
                   3470:       prev = cur; cur = NEXT(cur);
                   3471:     }
                   3472:   }
                   3473:   return head;
                   3474: }
                   3475:
                   3476: ND_pairs crit_M( ND_pairs d1 )
                   3477: {
                   3478:     ND_pairs e,d2,d3,dd,p;
                   3479:     UINT *id,*jd;
                   3480:
                   3481:     if ( !d1 ) return d1;
                   3482:     for ( dd = 0, e = d1; e; e = d3 ) {
                   3483:         if ( !(d2 = NEXT(e)) ) {
                   3484:             NEXT(e) = dd;
                   3485:             return e;
                   3486:         }
                   3487:         id = LCM(e);
                   3488:         for ( d3 = 0; d2; d2 = p ) {
                   3489:             p = NEXT(d2);
                   3490:             jd = LCM(d2);
                   3491:             if ( ndl_equal(jd,id) )
                   3492:                 ;
                   3493:             else if ( TD(jd) > TD(id) )
                   3494:                 if ( ndl_reducible(jd,id) ) continue;
                   3495:                 else ;
                   3496:             else if ( ndl_reducible(id,jd) ) goto delit;
                   3497:             NEXT(d2) = d3;
                   3498:             d3 = d2;
                   3499:         }
                   3500:         NEXT(e) = dd;
                   3501:         dd = e;
                   3502:         continue;
                   3503:         /**/
                   3504:     delit:    NEXT(d2) = d3;
                   3505:         d3 = d2;
                   3506:         for ( ; p; p = d2 ) {
                   3507:             d2 = NEXT(p);
                   3508:             NEXT(p) = d3;
                   3509:             d3 = p;
                   3510:         }
                   3511:         FREENDP(e);
                   3512:     }
                   3513:     return dd;
                   3514: }
                   3515:
                   3516: ND_pairs crit_F( ND_pairs d1 )
                   3517: {
                   3518:     ND_pairs rest, head,remove;
                   3519:     ND_pairs last, p, r, w;
                   3520:     int s;
                   3521:
                   3522:     if ( !d1 ) return d1;
                   3523:     for ( head = last = 0, p = d1; NEXT(p); ) {
                   3524:         r = w = equivalent_pairs(p,&rest);
                   3525:         s = SG(r);
                   3526:         w = NEXT(w);
                   3527:         while ( w ) {
                   3528:             if ( crit_2(w->i1,w->i2) ) {
                   3529:                 r = w;
                   3530:                 w = NEXT(w);
                   3531:                 while ( w ) {
                   3532:                     remove = w;
                   3533:                     w = NEXT(w);
                   3534:                     FREENDP(remove);
                   3535:                 }
                   3536:                 break;
                   3537:             } else if ( SG(w) < s ) {
                   3538:                 FREENDP(r);
                   3539:                 r = w;
                   3540:                 s = SG(r);
                   3541:                 w = NEXT(w);
                   3542:             } else {
                   3543:                 remove = w;
                   3544:                 w = NEXT(w);
                   3545:                 FREENDP(remove);
                   3546:             }
                   3547:         }
                   3548:         if ( last ) NEXT(last) = r;
                   3549:         else head = r;
                   3550:         NEXT(last = r) = 0;
                   3551:         p = rest;
                   3552:         if ( !p ) return head;
                   3553:     }
                   3554:     if ( !last ) return p;
                   3555:     NEXT(last) = p;
                   3556:     return head;
                   3557: }
                   3558:
                   3559: int crit_2( int dp1, int dp2 )
                   3560: {
                   3561:     return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
                   3562: }
                   3563:
                   3564: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
                   3565: {
                   3566:     ND_pairs w,p,r,s;
                   3567:     UINT *d;
                   3568:
                   3569:     w = d1;
                   3570:     d = LCM(w);
                   3571:     s = NEXT(w);
                   3572:     NEXT(w) = 0;
                   3573:     for ( r = 0; s; s = p ) {
                   3574:         p = NEXT(s);
                   3575:         if ( ndl_equal(d,LCM(s)) ) {
                   3576:             NEXT(s) = w; w = s;
                   3577:         } else {
                   3578:             NEXT(s) = r; r = s;
                   3579:         }
                   3580:     }
                   3581:     *prest = r;
                   3582:     return w;
                   3583: }
                   3584:
                   3585: NODE update_base(NODE nd,int ndp)
                   3586: {
                   3587:     UINT *dl, *dln;
                   3588:     NODE last, p, head;
                   3589:
                   3590:     dl = DL(nd_psh[ndp]);
                   3591:     for ( head = last = 0, p = nd; p; ) {
                   3592:         dln = DL(nd_psh[(long)BDY(p)]);
                   3593:         if ( ndl_reducible( dln, dl ) ) {
                   3594:             p = NEXT(p);
                   3595:             if ( last ) NEXT(last) = p;
                   3596:         } else {
                   3597:             if ( !last ) head = p;
                   3598:             p = NEXT(last = p);
                   3599:         }
                   3600:     }
                   3601:     head = append_one(head,ndp);
                   3602:     return head;
                   3603: }
                   3604:
                   3605: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   3606: {
                   3607:     ND_pairs m,ml,p,l;
                   3608:     UINT *lcm;
                   3609:     int s,td,len,tlen,c,c1;
                   3610:
                   3611:     if ( !(p = NEXT(m = d)) ) {
                   3612:         *prest = p;
                   3613:         NEXT(m) = 0;
                   3614:         return m;
                   3615:     }
                   3616:     if ( !NoSugar ) {
                   3617:         if ( nd_sugarweight ) {
                   3618:             s = m->sugar2;
                   3619:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   3620:                 if ( (p->sugar2 < s)
                   3621:                     || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   3622:                     ml = l; m = p; s = m->sugar2;
                   3623:                 }
                   3624:         } else {
                   3625:             s = SG(m);
                   3626:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   3627:                 if ( (SG(p) < s)
                   3628:                     || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   3629:                     ml = l; m = p; s = SG(m);
                   3630:                 }
                   3631:         }
                   3632:     } else {
                   3633:         for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   3634:             if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
                   3635:                 ml = l; m = p; s = SG(m);
                   3636:             }
                   3637:     }
                   3638:     if ( !ml ) *prest = NEXT(m);
                   3639:     else {
                   3640:         NEXT(ml) = NEXT(m);
                   3641:         *prest = d;
                   3642:     }
                   3643:     NEXT(m) = 0;
                   3644:     return m;
                   3645: }
                   3646:
                   3647: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   3648: {
                   3649:     int msugar,i;
                   3650:     ND_pairs t,dm0,dm,dr0,dr;
                   3651:
                   3652:     if ( nd_sugarweight ) {
                   3653:         for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
                   3654:             if ( t->sugar2 < msugar ) msugar = t->sugar2;
                   3655:         dm0 = 0; dr0 = 0;
                   3656:         for ( i = 0, t = d; t; t = NEXT(t) )
                   3657:             if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
                   3658:                 if ( dm0 ) NEXT(dm) = t;
                   3659:                 else dm0 = t;
                   3660:                 dm = t;
                   3661:                 i++;
                   3662:             } else {
                   3663:                 if ( dr0 ) NEXT(dr) = t;
                   3664:                 else dr0 = t;
                   3665:                 dr = t;
                   3666:             }
                   3667:     } else {
                   3668:         for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   3669:             if ( SG(t) < msugar ) msugar = SG(t);
                   3670:         dm0 = 0; dr0 = 0;
                   3671:         for ( i = 0, t = d; t; t = NEXT(t) )
                   3672:             if ( i < nd_f4_nsp && SG(t) == msugar ) {
                   3673:                 if ( dm0 ) NEXT(dm) = t;
                   3674:                 else dm0 = t;
                   3675:                 dm = t;
                   3676:                 i++;
                   3677:             } else {
                   3678:                 if ( dr0 ) NEXT(dr) = t;
                   3679:                 else dr0 = t;
                   3680:                 dr = t;
                   3681:             }
                   3682:     }
                   3683:     NEXT(dm) = 0;
                   3684:     if ( dr0 ) NEXT(dr) = 0;
                   3685:     *prest = dr0;
                   3686:     return dm0;
                   3687: }
                   3688:
1.28      noro     3689: ND_pairs nd_minsugarp_s( ND_pairs d, ND_pairs *prest )
                   3690: {
                   3691:   int msugar;
                   3692:   ND_pairs t,last;
                   3693:
                   3694:   for ( msugar = SG(d), t = d; t; t = NEXT(t) )
                   3695:     if ( SG(t) == msugar ) last = t;
                   3696:   *prest = last->next;
                   3697:   last->next = 0;
                   3698:   return d;
                   3699: }
                   3700:
1.1       noro     3701: int nd_tdeg(NDV c)
                   3702: {
                   3703:   int wmax = 0;
                   3704:   int i,len;
                   3705:   NMV a;
                   3706:
                   3707:   len = LEN(c);
                   3708:   for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   3709:     wmax = MAX(TD(DL(a)),wmax);
                   3710:   return wmax;
                   3711: }
                   3712:
1.24      noro     3713: int ndv_newps(int m,NDV a,NDV aq)
1.1       noro     3714: {
                   3715:     int len;
                   3716:     RHist r;
                   3717:     NDV b;
                   3718:     NODE tn;
                   3719:     LIST l;
                   3720:     Z iq;
                   3721:
                   3722:     if ( nd_psn == nd_pslen ) {
                   3723:         nd_pslen *= 2;
                   3724:         nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
                   3725:         nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
                   3726:         nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
                   3727:         nd_bound = (UINT **)
                   3728:             REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
                   3729:         nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
                   3730:         nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
                   3731:     }
                   3732:     NEWRHist(r); nd_psh[nd_psn] = r;
                   3733:     nd_ps[nd_psn] = a;
                   3734:     if ( aq ) {
                   3735:         nd_ps_trace[nd_psn] = aq;
                   3736:         if ( !m ) {
                   3737:           register_hcf(aq);
                   3738:         } else if ( m == -2 ) {
                   3739:           /* do nothing */
                   3740:         } else
                   3741:           error("ndv_newps : invalud modulus");
                   3742:         nd_bound[nd_psn] = ndv_compute_bound(aq);
                   3743: #if 1
                   3744:         SG(r) = SG(aq);
                   3745: #else
                   3746:         SG(r) = nd_tdeg(aq);
                   3747: #endif
                   3748:         ndl_copy(HDL(aq),DL(r));
1.24      noro     3749:         r->sig = dup_sig(aq->sig);
1.1       noro     3750:     } else {
                   3751:         if ( !m ) register_hcf(a);
                   3752:         nd_bound[nd_psn] = ndv_compute_bound(a);
                   3753: #if 1
                   3754:         SG(r) = SG(a);
                   3755: #else
                   3756:         SG(r) = nd_tdeg(a);
                   3757: #endif
                   3758:         ndl_copy(HDL(a),DL(r));
1.24      noro     3759:         r->sig = dup_sig(a->sig);
1.1       noro     3760:     }
                   3761:     if ( nd_demand ) {
                   3762:         if ( aq ) {
                   3763:             ndv_save(nd_ps_trace[nd_psn],nd_psn);
                   3764:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   3765:             nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   3766:             nd_ps_trace[nd_psn] = 0;
                   3767:         } else {
                   3768:             ndv_save(nd_ps[nd_psn],nd_psn);
                   3769:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
                   3770:             nd_ps[nd_psn] = 0;
                   3771:         }
                   3772:     }
                   3773:     if ( nd_gentrace ) {
                   3774:         /* reverse the tracelist and append it to alltracelist */
                   3775:         nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6       noro     3776:         STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1       noro     3777:         MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
                   3778:     }
                   3779:     return nd_psn++;
                   3780: }
                   3781:
                   3782: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
                   3783: /* return 1 if success, 0 if failure (HC(a mod p)) */
                   3784:
1.24      noro     3785: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont,int sba)
1.1       noro     3786: {
1.6       noro     3787:   int i,j,td,len,max;
                   3788:   NODE s,s0,f0,tn;
                   3789:   UINT *d;
                   3790:   RHist r;
                   3791:   NDVI w;
                   3792:   NDV a,am;
                   3793:   union oNDC hc;
                   3794:   NODE node;
                   3795:   P hcp;
                   3796:   Z iq,jq;
                   3797:   LIST l;
                   3798:
                   3799:   nd_found = 0; nd_notfirst = 0; nd_create = 0;
                   3800:   /* initialize the tracelist */
                   3801:   nd_tracelist = 0;
                   3802:
                   3803:   for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
                   3804:   w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
                   3805:   for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
                   3806:     if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
                   3807:   if ( !dont_sort ) {
                   3808:     /* XXX heuristic */
1.26      noro     3809:     if ( !sba && !nd_ord->id && (nd_ord->ord.simple<2) )
1.6       noro     3810:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   3811:         (int (*)(const void *,const void *))ndvi_compare_rev);
                   3812:     else
                   3813:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   3814:         (int (*)(const void *,const void *))ndvi_compare);
                   3815:   }
                   3816:   nd_pslen = 2*nd_psn;
                   3817:   nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3818:   nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3819:   nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3820:   nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3821:   nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
                   3822:   nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
                   3823:   nd_hcf = 0;
1.1       noro     3824:
1.6       noro     3825:   if ( trace && nd_vc )
                   3826:     makesubst(nd_vc,&nd_subst);
                   3827:   else
                   3828:     nd_subst = 0;
1.1       noro     3829:
1.6       noro     3830:   if ( !nd_red )
                   3831:     nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   3832:   for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
                   3833:   for ( i = 0; i < nd_psn; i++ ) {
                   3834:     hc = HCU(w[i].p);
                   3835:     if ( trace ) {
                   3836:       if ( mod == -2 ) {
                   3837:         /* over a large finite field */
                   3838:         /* trace = small modulus */
                   3839:         a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
                   3840:         ndv_mod(-2,a);
                   3841:         if ( !dont_removecont) ndv_removecont(-2,a);
                   3842:         am = nd_ps[i] = ndv_dup(trace,w[i].p);
                   3843:         ndv_mod(trace,am);
                   3844:       if ( DL_COMPARE(HDL(am),HDL(a)) )
                   3845:         return 0;
                   3846:         ndv_removecont(trace,am);
                   3847:       } else {
                   3848:         a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
                   3849:         if ( !dont_removecont) ndv_removecont(0,a);
                   3850:         register_hcf(a);
                   3851:         am = nd_ps[i] = ndv_dup(mod,a);
                   3852:         ndv_mod(mod,am);
                   3853:       if ( DL_COMPARE(HDL(am),HDL(a)) )
                   3854:         return 0;
                   3855:         ndv_removecont(mod,am);
                   3856:       }
                   3857:     } else {
                   3858:       a = nd_ps[i] = ndv_dup(mod,w[i].p);
                   3859:       if ( mod || !dont_removecont ) ndv_removecont(mod,a);
                   3860:       if ( !mod ) register_hcf(a);
1.1       noro     3861:     }
1.6       noro     3862:     if ( nd_gentrace ) {
                   3863:       STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
                   3864:       /* exact division */
1.1       noro     3865:       if ( !dont_removecont )
1.6       noro     3866:         ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
                   3867:       MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
                   3868:     }
                   3869:     NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
                   3870:     nd_bound[i] = ndv_compute_bound(a);
                   3871:     nd_psh[i] = r;
                   3872:     if ( nd_demand ) {
                   3873:       if ( trace ) {
                   3874:         ndv_save(nd_ps_trace[i],i);
                   3875:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   3876:         nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   3877:         nd_ps_trace[i] = 0;
                   3878:       } else {
                   3879:         ndv_save(nd_ps[i],i);
                   3880:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
                   3881:         nd_ps[i] = 0;
                   3882:       }
1.1       noro     3883:     }
1.6       noro     3884:   }
1.24      noro     3885:   if ( sba ) {
1.27      noro     3886:     nd_sba_hm = (DL *)MALLOC(nd_psn*sizeof(DL));
1.24      noro     3887:    // setup signatures
1.27      noro     3888:     for ( i = 0; i < nd_psn; i++ ) {
                   3889:       SIG sig;
1.24      noro     3890:
1.27      noro     3891:       NEWSIG(sig); sig->pos = i;
                   3892:       nd_ps[i]->sig = sig;
                   3893:       if ( nd_demand ) nd_ps_sym[i]->sig = sig;
                   3894:         nd_psh[i]->sig = sig;
                   3895:       if ( trace ) {
                   3896:         nd_ps_trace[i]->sig = sig;
                   3897:         if ( nd_demand ) nd_ps_trace_sym[i]->sig = sig;
                   3898:       }
                   3899:       NEWDL(nd_sba_hm[i],nd_nvar);
                   3900:       _ndltodl(DL(nd_psh[i]),nd_sba_hm[i]);
                   3901:     }
1.29      noro     3902:     nd_sba_pos = (NODE *)MALLOC(nd_psn*sizeof(NODE));
                   3903:     for ( i = 0; i < nd_psn; i++ ) {
                   3904:       j = nd_psh[i]->sig->pos;
                   3905:       nd_sba_pos[j] = append_one(nd_sba_pos[j],i);
                   3906:     }
1.24      noro     3907:   }
1.6       noro     3908:   if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
                   3909:   return 1;
1.1       noro     3910: }
                   3911:
                   3912: struct order_spec *append_block(struct order_spec *spec,
                   3913:     int nv,int nalg,int ord);
                   3914:
                   3915: extern VECT current_dl_weight_vector_obj;
                   3916: static VECT prev_weight_vector_obj;
                   3917:
                   3918: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
                   3919:     struct order_spec **ord1p,LIST *f1p,NODE *alistp)
                   3920: {
                   3921:     NODE alist,t,s,r0,r,arg;
                   3922:     VL tv;
                   3923:     P poly;
                   3924:     DP d;
                   3925:     Alg alpha,dp;
                   3926:     DAlg inv,da,hc;
                   3927:     MP m;
                   3928:     int i,nvar,nalg,n;
                   3929:     NumberField nf;
                   3930:     LIST f1,f2;
                   3931:     struct order_spec *current_spec;
                   3932:     VECT obj,obj0;
                   3933:     VECT tmp;
                   3934:
                   3935:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
                   3936:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
                   3937:
                   3938:     for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
                   3939:         NEXTNODE(alist,t); MKV(tv->v,poly);
                   3940:         MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
                   3941:         tv->v = tv->v->priv;
                   3942:     }
                   3943:     NEXT(t) = 0;
                   3944:
                   3945:     /* simplification, making polynomials monic */
                   3946:     setfield_dalg(alist);
                   3947:     obj_algtodalg((Obj)f,(Obj *)&f1);
                   3948:     for ( t = BDY(f); t; t = NEXT(t) ) {
                   3949:         initd(ord); ptod(vv,vv,(P)BDY(t),&d);
                   3950:         hc = (DAlg)BDY(d)->c;
                   3951:         if ( NID(hc) == N_DA ) {
                   3952:             invdalg(hc,&inv);
                   3953:             for ( m = BDY(d); m; m = NEXT(m) ) {
                   3954:                 muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
                   3955:             }
                   3956:         }
                   3957:         initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
                   3958:     }
                   3959:     obj_dalgtoalg((Obj)f1,(Obj *)&f);
                   3960:
                   3961:     /* append alg vars to the var list */
                   3962:     for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
                   3963:     NEXT(tv) = av;
                   3964:
                   3965:     /* append a block to ord */
                   3966:     *ord1p = append_block(ord,nvar,nalg,2);
                   3967:
                   3968:     /* create generator list */
                   3969:     nf = get_numberfield();
                   3970:     for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
                   3971:         MKAlg(nf->defpoly[i],dp);
                   3972:         MKNODE(s,dp,t); t = s;
                   3973:     }
                   3974:     MKLIST(f1,t);
                   3975:     *alistp = alist;
                   3976:     algobjtorat((Obj)f1,(Obj *)f1p);
                   3977:
                   3978:     /* creating a new weight vector */
                   3979:     prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
                   3980:     n = nvar+nalg+1;
                   3981:     MKVECT(obj,n);
                   3982:     if ( obj0 && obj0->len == nvar )
                   3983:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
                   3984:     else
                   3985:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
                   3986:     for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
                   3987:     BDY(obj)[n-1] = (pointer)ONE;
                   3988:     arg = mknode(1,obj);
                   3989:     Pdp_set_weight(arg,&tmp);
                   3990: }
                   3991:
                   3992: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
                   3993: {
                   3994:     NODE s,t,u0,u;
                   3995:     P p;
                   3996:     VL tv;
                   3997:     Obj obj;
                   3998:     VECT tmp;
                   3999:     NODE arg;
                   4000:
                   4001:     u0 = 0;
                   4002:     for ( t = r; t; t = NEXT(t) ) {
                   4003:         p = (P)BDY(t);
                   4004:         for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
                   4005:             substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
                   4006:         }
                   4007:         if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
                   4008:             NEXTNODE(u0,u);
                   4009:             BDY(u) = (pointer)p;
                   4010:         }
                   4011:     }
                   4012:     arg = mknode(1,prev_weight_vector_obj);
                   4013:     Pdp_set_weight(arg,&tmp);
                   4014:
                   4015:     return u0;
                   4016: }
                   4017:
                   4018: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
                   4019: {
                   4020:     VL tv,fv,vv,vc,av;
                   4021:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4022:     int e,max,nvar,i;
                   4023:     NDV b;
                   4024:     int ishomo,nalg,mrank,trank,wmax,len;
                   4025:   NMV a;
                   4026:     Alg alpha,dp;
                   4027:     P p,zp;
                   4028:     Q dmy;
                   4029:     LIST f1,f2,zpl;
                   4030:     Obj obj;
                   4031:     NumberField nf;
                   4032:     struct order_spec *ord1;
                   4033:     NODE tr,tl1,tl2,tl3,tl4,nzlist;
                   4034:     LIST l1,l2,l3,l4,l5;
                   4035:   int j;
                   4036:   Z jq,bpe,last_nonzero;
                   4037:     int *perm;
                   4038:     EPOS oepos;
                   4039:     int obpe,oadv,ompos,cbpe;
1.15      noro     4040:     VECT hvect;
1.1       noro     4041:
                   4042:     nd_module = 0;
                   4043:     if ( !m && Demand ) nd_demand = 1;
                   4044:     else nd_demand = 0;
                   4045:     parse_nd_option(current_option);
                   4046:
                   4047:     if ( DP_Multiple )
                   4048:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4049: #if 0
                   4050:     ndv_alloc = 0;
                   4051: #endif
                   4052:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4053:     if ( m && nd_vc )
                   4054:        error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
                   4055:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4056:     switch ( ord->id ) {
                   4057:         case 1:
                   4058:             if ( ord->nv != nvar )
                   4059:                 error("nd_{gr,f4} : invalid order specification");
                   4060:             break;
                   4061:         default:
                   4062:             break;
                   4063:     }
                   4064:     nd_nalg = 0;
                   4065:     av = 0;
                   4066:     if ( !m ) {
                   4067:         get_algtree((Obj)f,&av);
                   4068:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4069:         nd_ntrans = nvar;
                   4070:         nd_nalg = nalg;
                   4071:         /* #i -> t#i */
                   4072:         if ( nalg ) {
                   4073:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4074:             ord = ord1;
                   4075:             f = f1;
                   4076:         }
                   4077:         nvar += nalg;
                   4078:     }
                   4079:     nd_init_ord(ord);
                   4080:     mrank = 0;
                   4081:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4082:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4083:             if ( nd_module ) {
1.16      noro     4084:                 if ( OID(BDY(t)) == O_DPM ) {
                   4085:                   e = dpm_getdeg((DPM)BDY(t),&trank);
                   4086:                   max = MAX(e,max);
                   4087:                   mrank = MAX(mrank,trank);
                   4088:                 } else {
                   4089:                   s = BDY((LIST)BDY(t));
                   4090:                   trank = length(s);
                   4091:                   mrank = MAX(mrank,trank);
                   4092:                   for ( ; s; s = NEXT(s) ) {
                   4093:                       e = getdeg(tv->v,(P)BDY(s));
                   4094:                       max = MAX(e,max);
                   4095:                   }
1.1       noro     4096:                 }
                   4097:             } else {
                   4098:                 e = getdeg(tv->v,(P)BDY(t));
                   4099:                 max = MAX(e,max);
                   4100:             }
                   4101:         }
                   4102:     nd_setup_parameters(nvar,nd_nzlist?0:max);
                   4103:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   4104:     ishomo = 1;
                   4105:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4106:       if ( nd_module ) {
1.16      noro     4107:         if ( OID(BDY(t)) == O_DPM ) {
                   4108:           Z cont;
                   4109:           DPM zdpm;
                   4110:
                   4111:           if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
                   4112:           else zdpm = (DPM)BDY(t);
                   4113:           b = (pointer)dpmtondv(m,zdpm);
                   4114:         } else {
                   4115:           if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   4116:           else zpl = (LIST)BDY(t);
1.1       noro     4117:           b = (pointer)pltondv(CO,vv,zpl);
1.16      noro     4118:         }
1.1       noro     4119:       } else {
                   4120:         if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   4121:         else zp = (P)BDY(t);
                   4122:         b = (pointer)ptondv(CO,vv,zp);
                   4123:       }
                   4124:       if ( ishomo )
                   4125:         ishomo = ishomo && ndv_ishomo(b);
                   4126:       if ( m ) ndv_mod(m,b);
                   4127:       if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4128:     }
                   4129:     if ( fd0 ) NEXT(fd) = 0;
                   4130:
                   4131:   if ( !ishomo && homo ) {
                   4132:         for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   4133:             b = (NDV)BDY(t); len = LEN(b);
                   4134:             for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   4135:                 wmax = MAX(TD(DL(a)),wmax);
                   4136:         }
                   4137:         homogenize_order(ord,nvar,&ord1);
                   4138:         nd_init_ord(ord1);
                   4139:         nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   4140:         for ( t = fd0; t; t = NEXT(t) )
                   4141:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   4142:     }
                   4143:
1.24      noro     4144:     ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0,0);
1.1       noro     4145:     if ( nd_gentrace ) {
                   4146:         MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   4147:     }
                   4148:   if ( nd_splist ) {
                   4149:     *rp = compute_splist();
                   4150:     return;
                   4151:   }
                   4152:   if ( nd_check_splist ) {
                   4153:         if ( f4 ) {
                   4154:             if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
                   4155:             else *rp = 0;
                   4156:         } else {
                   4157:             if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
                   4158:             else *rp = 0;
                   4159:         }
                   4160:     return;
                   4161:   }
                   4162:     x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
                   4163:   if ( !x ) {
                   4164:     *rp = 0; return;
                   4165:   }
1.15      noro     4166:   if ( nd_gentrace ) {
                   4167:     MKVECT(hvect,nd_psn);
                   4168:     for ( i = 0; i < nd_psn; i++ )
                   4169:        ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   4170:   }
1.1       noro     4171:   if ( !ishomo && homo ) {
                   4172:        /* dehomogenization */
                   4173:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   4174:     nd_init_ord(ord);
                   4175:     nd_setup_parameters(nvar,0);
                   4176:   }
                   4177:     nd_demand = 0;
                   4178:   if ( nd_module && nd_intersect ) {
                   4179:     for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17      noro     4180:       if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1       noro     4181:         MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
                   4182:       }
                   4183:     conv_ilist(nd_demand,0,x,0);
                   4184:     goto FINAL;
                   4185:   }
                   4186:     if ( nd_gentrace  && f4 ) { nzlist = nd_alltracelist; }
                   4187:     x = ndv_reducebase(x,perm);
                   4188:     if ( nd_gentrace  && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
                   4189:     x = ndv_reduceall(m,x);
                   4190:     cbpe = nd_bpe;
                   4191:     if ( nd_gentrace && !f4 ) {
                   4192:         tl2 = nd_alltracelist; nd_alltracelist = 0;
                   4193:         ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
                   4194:         tl3 = nd_alltracelist; nd_alltracelist = 0;
                   4195:         if ( nd_gensyz ) {
                   4196:           nd_gb(m,0,1,1,0);
                   4197:             tl4 = nd_alltracelist; nd_alltracelist = 0;
                   4198:         } else tl4 = 0;
                   4199:     }
                   4200:     nd_bpe = cbpe;
                   4201:     nd_setup_parameters(nd_nvar,0);
                   4202: FINAL:
                   4203:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16      noro     4204:       NEXTNODE(r0,r);
                   4205:       if ( nd_module ) {
                   4206:         if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
                   4207:         else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
                   4208:       } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
                   4209:       else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1       noro     4210:     }
                   4211:     if ( r0 ) NEXT(r) = 0;
                   4212:     if ( !m && nd_nalg )
                   4213:         r0 = postprocess_algcoef(av,alist,r0);
                   4214:     MKLIST(*rp,r0);
                   4215:     if ( nd_gentrace ) {
                   4216:   if ( f4 ) {
1.6       noro     4217:             STOZ(16,bpe);
                   4218:             STOZ(nd_last_nonzero,last_nonzero);
1.15      noro     4219:             tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1       noro     4220:         } else {
                   4221:             tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   4222:             tl3 = reverse_node(tl3);
                   4223:             /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   4224:             for ( t = tl2; t; t = NEXT(t) ) {
                   4225:             /* s = [i,[*,j,*,*],...] */
                   4226:                 s = BDY((LIST)BDY(t));
1.6       noro     4227:                 j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     4228:                 for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     4229:                     j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     4230:                     ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   4231:                 }
                   4232:             }
                   4233:             for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6       noro     4234:                 STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     4235:             }
                   4236:             MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   4237:             MKLIST(l5,tl4);
1.6       noro     4238:             STOZ(nd_bpe,bpe);
1.15      noro     4239:             tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     4240:         }
                   4241:     }
                   4242: #if 0
                   4243:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   4244: #endif
                   4245: }
                   4246:
1.28      noro     4247: NODE nd_sba_f4(int m,int **indp);
                   4248:
                   4249: void nd_sba(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.24      noro     4250: {
                   4251:   VL tv,fv,vv,vc,av;
                   4252:   NODE fd,fd0,r,r0,t,x,s,xx;
                   4253:   int e,max,nvar,i;
                   4254:   NDV b;
                   4255:   int ishomo,nalg,wmax,len;
                   4256:   NMV a;
                   4257:   P p,zp;
                   4258:   Q dmy;
                   4259:   struct order_spec *ord1;
                   4260:   int j;
                   4261:   int *perm;
                   4262:   EPOS oepos;
                   4263:   int obpe,oadv,ompos,cbpe;
1.29      noro     4264:   struct oEGT eg0,eg1,egconv;
1.24      noro     4265:
                   4266:   nd_module = 0;
                   4267:   nd_demand = 0;
                   4268:   parse_nd_option(current_option);
                   4269:
                   4270:   if ( DP_Multiple )
                   4271:     nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4272:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4273:   if ( m && nd_vc )
                   4274:     error("nd_sba : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
                   4275:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4276:   switch ( ord->id ) {
                   4277:     case 1:
                   4278:       if ( ord->nv != nvar )
                   4279:         error("nd_sba : invalid order specification");
                   4280:         break;
                   4281:       default:
                   4282:         break;
                   4283:   }
                   4284:   nd_nalg = 0;
                   4285:   nd_init_ord(ord);
                   4286:   // for SIG comparison
                   4287:   initd(ord);
                   4288:   for ( t = BDY(f), max = 1; t; t = NEXT(t) ) {
                   4289:     for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4290:       e = getdeg(tv->v,(P)BDY(t));
                   4291:       max = MAX(e,max);
                   4292:     }
                   4293:   }
1.25      noro     4294:   nd_setup_parameters(nvar,max);
1.24      noro     4295:   obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   4296:   ishomo = 1;
                   4297:   for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4298:     if ( !m ) ptozp((P)BDY(t),1,&dmy,&zp);
                   4299:     else zp = (P)BDY(t);
                   4300:     b = (pointer)ptondv(CO,vv,zp);
                   4301:     if ( ishomo )
                   4302:       ishomo = ishomo && ndv_ishomo(b);
                   4303:     if ( m ) ndv_mod(m,b);
                   4304:     if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4305:   }
                   4306:   if ( fd0 ) NEXT(fd) = 0;
                   4307:
                   4308:   if ( !ishomo && homo ) {
                   4309:     for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   4310:       b = (NDV)BDY(t); len = LEN(b);
                   4311:       for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   4312:         wmax = MAX(TD(DL(a)),wmax);
                   4313:       }
                   4314:       homogenize_order(ord,nvar,&ord1);
                   4315:       nd_init_ord(ord1);
                   4316:       nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   4317:       for ( t = fd0; t; t = NEXT(t) )
                   4318:         ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   4319:   }
                   4320:
                   4321:   ndv_setup(m,0,fd0,0,0,1);
1.28      noro     4322:   x = f4 ? nd_sba_f4(m,&perm) : nd_sba_buch(m,ishomo || homo,&perm);
1.24      noro     4323:   if ( !x ) {
                   4324:     *rp = 0; return;
                   4325:   }
                   4326:   if ( !ishomo && homo ) {
                   4327:        /* dehomogenization */
                   4328:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   4329:     nd_init_ord(ord);
                   4330:     nd_setup_parameters(nvar,0);
                   4331:   }
                   4332:   nd_demand = 0;
                   4333:   x = ndv_reducebase(x,perm);
                   4334:   x = ndv_reduceall(m,x);
                   4335:   nd_setup_parameters(nd_nvar,0);
1.29      noro     4336:   get_eg(&eg0);
1.24      noro     4337:   for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   4338:     NEXTNODE(r0,r);
                   4339:     if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
1.34    ! noro     4340:     else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.24      noro     4341:   }
                   4342:   if ( r0 ) NEXT(r) = 0;
                   4343:   MKLIST(*rp,r0);
1.29      noro     4344:   get_eg(&eg1); init_eg(&egconv); add_eg(&egconv,&eg0,&eg1);
                   4345:   print_eg("conv",&egconv); fprintf(asir_out,"\n");
1.24      noro     4346: }
                   4347:
1.1       noro     4348: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
                   4349: {
                   4350:     VL tv,fv,vv,vc,av;
                   4351:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4352:     int e,max,nvar,i;
                   4353:     NDV b;
                   4354:     int ishomo,nalg;
                   4355:     Alg alpha,dp;
                   4356:     P p,zp;
                   4357:     Q dmy;
                   4358:     LIST f1,f2;
                   4359:     Obj obj;
                   4360:     NumberField nf;
                   4361:     struct order_spec *ord1;
                   4362:     int *perm;
                   4363:
                   4364:     parse_nd_option(current_option);
                   4365:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4366:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4367:     switch ( ord->id ) {
                   4368:         case 1:
                   4369:             if ( ord->nv != nvar )
                   4370:                 error("nd_check : invalid order specification");
                   4371:             break;
                   4372:         default:
                   4373:             break;
                   4374:     }
                   4375:     nd_nalg = 0;
                   4376:     av = 0;
                   4377:     if ( !m ) {
                   4378:         get_algtree((Obj)f,&av);
                   4379:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4380:         nd_ntrans = nvar;
                   4381:         nd_nalg = nalg;
                   4382:         /* #i -> t#i */
                   4383:         if ( nalg ) {
                   4384:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4385:             ord = ord1;
                   4386:             f = f1;
                   4387:         }
                   4388:         nvar += nalg;
                   4389:     }
                   4390:     nd_init_ord(ord);
                   4391:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4392:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4393:             e = getdeg(tv->v,(P)BDY(t));
                   4394:             max = MAX(e,max);
                   4395:         }
                   4396:     nd_setup_parameters(nvar,max);
                   4397:     ishomo = 1;
                   4398:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4399:         ptozp((P)BDY(t),1,&dmy,&zp);
                   4400:         b = (pointer)ptondv(CO,vv,zp);
                   4401:         if ( ishomo )
                   4402:             ishomo = ishomo && ndv_ishomo(b);
                   4403:         if ( m ) ndv_mod(m,b);
                   4404:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4405:     }
                   4406:     if ( fd0 ) NEXT(fd) = 0;
1.24      noro     4407:     ndv_setup(m,0,fd0,0,1,0);
1.1       noro     4408:     for ( x = 0, i = 0; i < nd_psn; i++ )
                   4409:         x = update_base(x,i);
                   4410:     if ( do_check ) {
                   4411:         x = nd_gb(m,ishomo,1,0,&perm);
                   4412:         if ( !x ) {
                   4413:             *rp = 0;
                   4414:             return;
                   4415:         }
                   4416:     } else {
                   4417: #if 0
                   4418:     /* bug ? */
                   4419:         for ( t = x; t; t = NEXT(t) )
                   4420:             BDY(t) = (pointer)nd_ps[(long)BDY(t)];
                   4421: #else
                   4422:     conv_ilist(0,0,x,&perm);
                   4423: #endif
                   4424:     }
                   4425:     x = ndv_reducebase(x,perm);
                   4426:     x = ndv_reduceall(m,x);
                   4427:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   4428:         NEXTNODE(r0,r);
                   4429:         BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   4430:     }
                   4431:     if ( r0 ) NEXT(r) = 0;
                   4432:     if ( !m && nd_nalg )
                   4433:         r0 = postprocess_algcoef(av,alist,r0);
                   4434:     MKLIST(*rp,r0);
                   4435: }
                   4436:
                   4437: NDV recompute_trace(NODE trace,NDV *p,int m);
                   4438: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
                   4439:
                   4440: NDV recompute_trace(NODE ti,NDV *p,int mod)
                   4441: {
                   4442:   int c,c1,c2,i;
                   4443:   NM mul,m,tail;
                   4444:   ND d,r,rm;
                   4445:   NODE sj;
                   4446:   NDV red;
                   4447:   Obj mj;
                   4448:
                   4449:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   4450:   CM(mul) = 1;
                   4451:   tail = 0;
                   4452:   for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
                   4453:     sj = BDY((LIST)BDY(ti));
                   4454:     if ( ARG0(sj) ) {
1.6       noro     4455:       red = p[ZTOS((Q)ARG1(sj))];
1.1       noro     4456:       mj = (Obj)ARG2(sj);
                   4457:       if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
                   4458:       else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
                   4459:       rm = ndv_mul_nm(mod,mul,red);
                   4460:       if ( !r ) r = rm;
                   4461:       else {
                   4462:         for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
                   4463:           if ( d ) {
                   4464:             NEXT(tail) = m; tail = m; LEN(d)++;
                   4465:           } else {
                   4466:             MKND(nd_nvar,m,1,d); tail = BDY(d);
                   4467:           }
                   4468:         }
                   4469:         if ( !m ) return 0; /* failure */
                   4470:                 else {
                   4471:           BDY(r) = m;
                   4472:                     if ( mod > 0 || mod == -1 ) {
                   4473:             c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
                   4474:             DMAR(c1,c2,0,mod,c);
                   4475:             nd_mul_c(mod,rm,c);
                   4476:                     } else {
                   4477:                       Z t,u;
                   4478:
                   4479:                       chsgnlf(HCZ(r),&t);
                   4480:                       divlf(t,HCZ(rm),&u);
                   4481:                       nd_mul_c_lf(rm,u);
                   4482:                     }
                   4483:           r = nd_add(mod,r,rm);
                   4484:         }
                   4485:           }
                   4486:      }
                   4487:   }
                   4488:   if ( tail ) NEXT(tail) = 0;
                   4489:   d = nd_add(mod,d,r);
                   4490:   nd_mul_c(mod,d,invm(HCM(d),mod));
                   4491:   return ndtondv(mod,d);
                   4492: }
                   4493:
                   4494: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
                   4495: {
                   4496:     VL tv,fv,vv,vc,av;
                   4497:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4498:     int e,max,nvar,i;
                   4499:     NDV b;
                   4500:     int ishomo,nalg;
                   4501:     Alg alpha,dp;
                   4502:     P p,zp;
                   4503:     Q dmy;
                   4504:     LIST f1,f2;
                   4505:     Obj obj;
                   4506:     NumberField nf;
                   4507:     struct order_spec *ord1;
                   4508:   NODE permtrace,intred,ind,perm,trace,ti;
                   4509:   int len,n,j;
                   4510:   NDV *db,*pb;
                   4511:
                   4512:     parse_nd_option(current_option);
                   4513:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4514:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4515:     switch ( ord->id ) {
                   4516:         case 1:
                   4517:             if ( ord->nv != nvar )
                   4518:                 error("nd_check : invalid order specification");
                   4519:             break;
                   4520:         default:
                   4521:             break;
                   4522:     }
                   4523:     nd_init_ord(ord);
1.6       noro     4524:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     4525:     nd_setup_parameters(nvar,0);
                   4526:
                   4527:   len = length(BDY(f));
                   4528:   db = (NDV *)MALLOC(len*sizeof(NDV *));
                   4529:   for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
                   4530:       ptozp((P)BDY(t),1,&dmy,&zp);
                   4531:       b = ptondv(CO,vv,zp);
                   4532:         ndv_mod(m,b);
                   4533:     ndv_mul_c(m,b,invm(HCM(b),m));
                   4534:     db[i] = b;
                   4535:     }
                   4536:
                   4537:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   4538:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   4539:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   4540:   perm = BDY((LIST)ARG0(permtrace));
                   4541:   trace = NEXT(permtrace);
                   4542:
                   4543:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     4544:     j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1       noro     4545:     if ( j > i ) i = j;
                   4546:   }
                   4547:   n = i+1;
                   4548:   pb = (NDV *)MALLOC(n*sizeof(NDV *));
                   4549:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   4550:     ti = BDY((LIST)BDY(t));
1.6       noro     4551:     pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1       noro     4552:   }
                   4553:   for ( t = trace; t; t = NEXT(t) ) {
                   4554:     ti = BDY((LIST)BDY(t));
1.6       noro     4555:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   4556:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     4557:       if ( DP_Print ) {
                   4558:            fprintf(asir_out,"."); fflush(asir_out);
                   4559:       }
                   4560:   }
                   4561:   for ( t = intred; t; t = NEXT(t) ) {
                   4562:     ti = BDY((LIST)BDY(t));
1.6       noro     4563:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   4564:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     4565:       if ( DP_Print ) {
                   4566:            fprintf(asir_out,"*"); fflush(asir_out);
                   4567:       }
                   4568:   }
                   4569:     for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
                   4570:         NEXTNODE(r0,r);
1.6       noro     4571:     b = pb[ZTOS((Q)BDY(t))];
1.1       noro     4572:         ndv_mul_c(m,b,invm(HCM(b),m));
                   4573: #if 0
1.6       noro     4574:         BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1       noro     4575: #else
1.6       noro     4576:         BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1       noro     4577: #endif
                   4578:     }
                   4579:     if ( r0 ) NEXT(r) = 0;
                   4580:     MKLIST(*rp,r0);
                   4581:     if ( DP_Print ) fprintf(asir_out,"\n");
                   4582: }
                   4583:
1.16      noro     4584: void nd_gr_trace(LIST f,LIST v,int trace,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.1       noro     4585: {
                   4586:     VL tv,fv,vv,vc,av;
                   4587:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   4588:     int m,nocheck,nvar,mindex,e,max;
                   4589:     NDV c;
                   4590:     NMV a;
                   4591:     P p,zp;
                   4592:     Q dmy;
                   4593:     EPOS oepos;
                   4594:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   4595:     Alg alpha,dp;
                   4596:     P poly;
                   4597:     LIST f1,f2,zpl;
                   4598:     Obj obj;
                   4599:     NumberField nf;
                   4600:     struct order_spec *ord1;
                   4601:     struct oEGT eg_check,eg0,eg1;
                   4602:     NODE tr,tl1,tl2,tl3,tl4;
                   4603:     LIST l1,l2,l3,l4,l5;
                   4604:     int *perm;
                   4605:     int j,ret;
                   4606:     Z jq,bpe;
1.15      noro     4607:     VECT hvect;
1.1       noro     4608:
                   4609:     nd_module = 0;
                   4610:     nd_lf = 0;
                   4611:     parse_nd_option(current_option);
                   4612:     if ( nd_lf ) {
                   4613:       if ( f4 )
                   4614:         nd_f4_lf_trace(f,v,trace,homo,ord,rp);
                   4615:       else
                   4616:         error("nd_gr_trace is not implemented yet over a large finite field");
                   4617:       return;
                   4618:     }
                   4619:     if ( DP_Multiple )
                   4620:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4621:
                   4622:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4623:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4624:     switch ( ord->id ) {
                   4625:         case 1:
                   4626:             if ( ord->nv != nvar )
                   4627:                 error("nd_gr_trace : invalid order specification");
                   4628:             break;
                   4629:         default:
                   4630:             break;
                   4631:     }
                   4632:
                   4633:     get_algtree((Obj)f,&av);
                   4634:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4635:     nd_ntrans = nvar;
                   4636:     nd_nalg = nalg;
                   4637:     /* #i -> t#i */
                   4638:     if ( nalg ) {
                   4639:         preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4640:         ord = ord1;
                   4641:         f = f1;
                   4642:     }
                   4643:     nvar += nalg;
                   4644:
                   4645:     nocheck = 0;
                   4646:     mindex = 0;
                   4647:
                   4648:     if ( Demand ) nd_demand = 1;
                   4649:     else nd_demand = 0;
                   4650:
                   4651:     /* setup modulus */
                   4652:     if ( trace < 0 ) {
                   4653:         trace = -trace;
                   4654:         nocheck = 1;
                   4655:     }
                   4656:     m = trace > 1 ? trace : get_lprime(mindex);
                   4657:     nd_init_ord(ord);
                   4658:     mrank = 0;
                   4659:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4660:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4661:             if ( nd_module ) {
1.16      noro     4662:               if ( OID(BDY(t)) == O_DPM ) {
                   4663:                 e = dpm_getdeg((DPM)BDY(t),&trank);
                   4664:                 max = MAX(e,max);
                   4665:                 mrank = MAX(mrank,trank);
                   4666:               } else {
1.1       noro     4667:                 s = BDY((LIST)BDY(t));
                   4668:                 trank = length(s);
                   4669:                 mrank = MAX(mrank,trank);
                   4670:                 for ( ; s; s = NEXT(s) ) {
                   4671:                     e = getdeg(tv->v,(P)BDY(s));
                   4672:                     max = MAX(e,max);
                   4673:                 }
1.16      noro     4674:               }
1.1       noro     4675:             } else {
                   4676:                 e = getdeg(tv->v,(P)BDY(t));
                   4677:                 max = MAX(e,max);
                   4678:             }
                   4679:         }
                   4680:     nd_setup_parameters(nvar,max);
                   4681:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   4682:     ishomo = 1;
                   4683:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4684:         if ( nd_module ) {
1.16      noro     4685:           if ( OID(BDY(t)) == O_DPM ) {
                   4686:             Z cont;
                   4687:             DPM zdpm;
                   4688:
1.17      noro     4689:             if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16      noro     4690:             else zdpm = (DPM)BDY(t);
                   4691:             c = (pointer)dpmtondv(m,zdpm);
                   4692:           } else {
                   4693:             if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   4694:             else zpl = (LIST)BDY(t);
1.1       noro     4695:             c = (pointer)pltondv(CO,vv,zpl);
1.16      noro     4696:           }
1.1       noro     4697:         } else {
1.16      noro     4698:           if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   4699:           else zp = (P)BDY(t);
                   4700:           c = (pointer)ptondv(CO,vv,zp);
1.1       noro     4701:         }
                   4702:         if ( ishomo )
                   4703:             ishomo = ishomo && ndv_ishomo(c);
                   4704:         if ( c ) {
                   4705:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   4706:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   4707:         }
                   4708:     }
                   4709:     if ( in0 ) NEXT(in) = 0;
                   4710:     if ( fd0 ) NEXT(fd) = 0;
                   4711:     if ( !ishomo && homo ) {
                   4712:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   4713:             c = (NDV)BDY(t); len = LEN(c);
                   4714:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   4715:                 wmax = MAX(TD(DL(a)),wmax);
                   4716:         }
                   4717:         homogenize_order(ord,nvar,&ord1);
                   4718:         nd_init_ord(ord1);
                   4719:         nd_setup_parameters(nvar+1,wmax);
                   4720:         for ( t = fd0; t; t = NEXT(t) )
                   4721:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   4722:     }
                   4723:     if ( MaxDeg > 0 ) nocheck = 1;
                   4724:     while ( 1 ) {
                   4725:     tl1 = tl2 = tl3 = tl4 = 0;
                   4726:         if ( Demand )
                   4727:             nd_demand = 1;
1.24      noro     4728:         ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1       noro     4729:         if ( nd_gentrace ) {
                   4730:             MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   4731:         }
                   4732:         if ( ret )
                   4733:             cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
                   4734:         if ( !ret || !cand ) {
                   4735:             /* failure */
                   4736:             if ( trace > 1 ) { *rp = 0; return; }
                   4737:             else m = get_lprime(++mindex);
                   4738:             continue;
                   4739:         }
1.15      noro     4740:         if ( nd_gentrace ) {
                   4741:           MKVECT(hvect,nd_psn);
                   4742:           for ( i = 0; i < nd_psn; i++ )
                   4743:              ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   4744:         }
1.1       noro     4745:         if ( !ishomo && homo ) {
                   4746:             /* dehomogenization */
                   4747:             for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   4748:             nd_init_ord(ord);
                   4749:             nd_setup_parameters(nvar,0);
                   4750:         }
                   4751:         nd_demand = 0;
                   4752:         cand = ndv_reducebase(cand,perm);
                   4753:         if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
                   4754:         cand = ndv_reduceall(0,cand);
                   4755:         cbpe = nd_bpe;
                   4756:         if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
                   4757:         get_eg(&eg0);
                   4758:         if ( nocheck )
                   4759:             break;
                   4760:         if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   4761:             if ( nd_gentrace ) {
                   4762:           tl3 = nd_alltracelist; nd_alltracelist = 0;
                   4763:         } else tl3 = 0;
                   4764:             /* gbcheck : cand is a GB of Id(cand) ? */
                   4765:             if ( nd_vc || nd_gentrace || nd_gensyz )
                   4766:               ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
                   4767:             else
                   4768:               ret = nd_f4(0,1,0)!=0;
                   4769:             if ( nd_gentrace && nd_gensyz ) {
                   4770:           tl4 = nd_alltracelist; nd_alltracelist = 0;
                   4771:         } else tl4 = 0;
                   4772:     }
                   4773:     if ( ret ) break;
                   4774:         else if ( trace > 1 ) {
                   4775:             /* failure */
                   4776:             *rp = 0; return;
                   4777:         } else {
                   4778:             /* try the next modulus */
                   4779:             m = get_lprime(++mindex);
                   4780:             /* reset the parameters */
                   4781:             if ( !ishomo && homo ) {
                   4782:                 nd_init_ord(ord1);
                   4783:                 nd_setup_parameters(nvar+1,wmax);
                   4784:             } else {
                   4785:                 nd_init_ord(ord);
                   4786:                 nd_setup_parameters(nvar,max);
                   4787:             }
                   4788:         }
                   4789:     }
                   4790:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   4791:     if ( DP_Print )
1.6       noro     4792:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     4793:     /* dp->p */
                   4794:     nd_bpe = cbpe;
                   4795:     nd_setup_parameters(nd_nvar,0);
                   4796:     for ( r = cand; r; r = NEXT(r) ) {
1.16      noro     4797:       if ( nd_module ) {
1.17      noro     4798:         if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16      noro     4799:         else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17      noro     4800:       } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
                   4801:       else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1       noro     4802:     }
                   4803:     if ( nd_nalg )
                   4804:         cand = postprocess_algcoef(av,alist,cand);
                   4805:     MKLIST(*rp,cand);
                   4806:     if ( nd_gentrace ) {
                   4807:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   4808:     tl3 = reverse_node(tl3);
                   4809:     /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   4810:         for ( t = tl2; t; t = NEXT(t) ) {
                   4811:       /* s = [i,[*,j,*,*],...] */
                   4812:             s = BDY((LIST)BDY(t));
1.6       noro     4813:             j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     4814:       for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     4815:                 j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     4816:         ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   4817:             }
                   4818:     }
                   4819:     for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6       noro     4820:         STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     4821:     }
                   4822:         MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   4823:     MKLIST(l5,tl4);
1.6       noro     4824:       STOZ(nd_bpe,bpe);
1.15      noro     4825:         tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     4826:     }
                   4827: }
                   4828:
                   4829: /* XXX : module element is not considered  */
                   4830:
                   4831: void dltondl(int n,DL dl,UINT *r)
                   4832: {
                   4833:     UINT *d;
                   4834:     int i,j,l,s,ord_l;
                   4835:     struct order_pair *op;
                   4836:
                   4837:     d = (unsigned int *)dl->d;
                   4838:     for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
                   4839:     if ( nd_blockmask ) {
                   4840:         l = nd_blockmask->n;
                   4841:         op = nd_blockmask->order_pair;
                   4842:         for ( j = 0, s = 0; j < l; j++ ) {
                   4843:             ord_l = op[j].length;
                   4844:             for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
                   4845:         }
                   4846:         TD(r) = ndl_weight(r);
                   4847:         ndl_weight_mask(r);
                   4848:     } else {
                   4849:         for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
                   4850:         TD(r) = ndl_weight(r);
                   4851:     }
                   4852: }
                   4853:
                   4854: DL ndltodl(int n,UINT *ndl)
                   4855: {
                   4856:     DL dl;
                   4857:     int *d;
                   4858:     int i,j,l,s,ord_l;
                   4859:     struct order_pair *op;
                   4860:
1.33      noro     4861:     NEWDL_NOINIT(dl,n);
1.1       noro     4862:     dl->td = TD(ndl);
                   4863:     d = dl->d;
                   4864:     if ( nd_blockmask ) {
                   4865:         l = nd_blockmask->n;
                   4866:         op = nd_blockmask->order_pair;
                   4867:         for ( j = 0, s = 0; j < l; j++ ) {
                   4868:             ord_l = op[j].length;
                   4869:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   4870:         }
                   4871:     } else {
                   4872:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   4873:     }
                   4874:     return dl;
                   4875: }
                   4876:
1.24      noro     4877: void _ndltodl(UINT *ndl,DL dl)
                   4878: {
                   4879:     int *d;
                   4880:     int i,j,l,s,ord_l,n;
                   4881:     struct order_pair *op;
                   4882:
                   4883:     n = nd_nvar;
                   4884:     dl->td = TD(ndl);
                   4885:     d = dl->d;
                   4886:     if ( nd_blockmask ) {
                   4887:         l = nd_blockmask->n;
                   4888:         op = nd_blockmask->order_pair;
                   4889:         for ( j = 0, s = 0; j < l; j++ ) {
                   4890:             ord_l = op[j].length;
                   4891:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   4892:         }
                   4893:     } else {
                   4894:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   4895:     }
                   4896: }
                   4897:
1.1       noro     4898: void nmtodp(int mod,NM m,DP *r)
                   4899: {
                   4900:     DP dp;
                   4901:     MP mr;
                   4902:
                   4903:     NEWMP(mr);
                   4904:     mr->dl = ndltodl(nd_nvar,DL(m));
                   4905:     mr->c = (Obj)ndctop(mod,m->c);
                   4906:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   4907:     *r = dp;
                   4908: }
                   4909:
1.15      noro     4910: void ndltodp(UINT *d,DP *r)
                   4911: {
                   4912:     DP dp;
                   4913:     MP mr;
                   4914:
                   4915:     NEWMP(mr);
                   4916:     mr->dl = ndltodl(nd_nvar,d);
                   4917:     mr->c = (Obj)ONE;
                   4918:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   4919:     *r = dp;
                   4920: }
                   4921:
1.1       noro     4922: void ndl_print(UINT *dl)
                   4923: {
                   4924:     int n;
                   4925:     int i,j,l,ord_l,s,s0;
                   4926:     struct order_pair *op;
                   4927:
                   4928:     n = nd_nvar;
                   4929:     printf("<<");
                   4930:     if ( nd_blockmask ) {
                   4931:         l = nd_blockmask->n;
                   4932:         op = nd_blockmask->order_pair;
                   4933:         for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   4934:             ord_l = op[j].length;
                   4935:             for ( i = 0; i < ord_l; i++, s++ )
                   4936:                 printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
                   4937:         }
                   4938:     } else {
                   4939:         for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
                   4940:     }
                   4941:     printf(">>");
                   4942:     if ( nd_module && MPOS(dl) )
                   4943:         printf("*e%d",MPOS(dl));
                   4944: }
                   4945:
                   4946: void nd_print(ND p)
                   4947: {
                   4948:     NM m;
                   4949:
                   4950:     if ( !p )
                   4951:         printf("0\n");
                   4952:     else {
                   4953:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   4954:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     4955:             else printf("+%ld*",CM(m));
1.1       noro     4956:             ndl_print(DL(m));
                   4957:         }
                   4958:         printf("\n");
                   4959:     }
                   4960: }
                   4961:
                   4962: void nd_print_q(ND p)
                   4963: {
                   4964:     NM m;
                   4965:
                   4966:     if ( !p )
                   4967:         printf("0\n");
                   4968:     else {
                   4969:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   4970:             printf("+");
1.6       noro     4971:             printexpr(CO,(Obj)CZ(m));
1.1       noro     4972:             printf("*");
                   4973:             ndl_print(DL(m));
                   4974:         }
                   4975:         printf("\n");
                   4976:     }
                   4977: }
                   4978:
                   4979: void ndp_print(ND_pairs d)
                   4980: {
                   4981:     ND_pairs t;
                   4982:
                   4983:     for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
                   4984:     printf("\n");
                   4985: }
                   4986:
                   4987: void nd_removecont(int mod,ND p)
                   4988: {
                   4989:     int i,n;
                   4990:     Z *w;
                   4991:     NM m;
                   4992:     struct oVECT v;
                   4993:
                   4994:     if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
                   4995:     else if ( mod == -2 ) {
                   4996:       Z inv;
                   4997:       divlf(ONE,HCZ(p),&inv);
                   4998:       nd_mul_c_lf(p,inv);
                   4999:     } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
                   5000:     else {
                   5001:         for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   5002:         w = (Z *)MALLOC(n*sizeof(Q));
                   5003:         v.len = n;
                   5004:         v.body = (pointer *)w;
1.6       noro     5005:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5006:         removecont_array((P *)w,n,1);
1.6       noro     5007:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5008:     }
                   5009: }
                   5010:
                   5011: void nd_removecont2(ND p1,ND p2)
                   5012: {
                   5013:     int i,n1,n2,n;
                   5014:     Z *w;
                   5015:     NM m;
                   5016:     struct oVECT v;
                   5017:
                   5018:     n1 = nd_length(p1);
                   5019:     n2 = nd_length(p2);
                   5020:     n = n1+n2;
                   5021:     w = (Z *)MALLOC(n*sizeof(Q));
                   5022:     v.len = n;
                   5023:     v.body = (pointer *)w;
                   5024:     i = 0;
                   5025:     if ( p1 )
1.6       noro     5026:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5027:     if ( p2 )
1.6       noro     5028:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5029:     removecont_array((P *)w,n,1);
                   5030:     i = 0;
                   5031:     if ( p1 )
1.6       noro     5032:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5033:     if ( p2 )
1.6       noro     5034:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5035: }
                   5036:
                   5037: void ndv_removecont(int mod,NDV p)
                   5038: {
                   5039:     int i,len,all_p;
                   5040:     Z *c;
                   5041:     P *w;
                   5042:     Z dvr,t;
                   5043:     P g,cont,tp;
                   5044:     NMV m;
                   5045:
                   5046:     if ( mod == -1 )
                   5047:         ndv_mul_c(mod,p,_invsf(HCM(p)));
                   5048:     else if ( mod == -2 ) {
                   5049:       Z inv;
                   5050:       divlf(ONE,HCZ(p),&inv);
                   5051:       ndv_mul_c_lf(p,inv);
                   5052:     } else if ( mod )
                   5053:         ndv_mul_c(mod,p,invm(HCM(p),mod));
                   5054:     else {
                   5055:         len = p->len;
                   5056:         w = (P *)MALLOC(len*sizeof(P));
                   5057:         c = (Z *)MALLOC(len*sizeof(Q));
                   5058:         for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
                   5059:             ptozp(CP(m),1,(Q *)&c[i],&w[i]);
                   5060:             all_p = all_p && !NUM(w[i]);
                   5061:         }
                   5062:         if ( all_p ) {
                   5063:             qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
                   5064:             mulp(nd_vc,(P)dvr,g,&cont);
                   5065:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5066:                 divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
                   5067:             }
                   5068:         } else {
                   5069:             sortbynm((Q *)c,len);
                   5070:             qltozl((Q *)c,len,&dvr);
                   5071:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5072:                 divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
                   5073:             }
                   5074:         }
                   5075:     }
                   5076: }
                   5077:
                   5078: /* koko */
                   5079:
                   5080: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
                   5081: {
                   5082:     int len,i,max;
                   5083:     NMV m,mr0,mr,t;
                   5084:
                   5085:     len = p->len;
1.14      noro     5086:     for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1       noro     5087:         max = MAX(max,TD(DL(m)));
                   5088:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   5089:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   5090:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   5091:     t = (NMV)MALLOC(nmv_adv);
                   5092:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   5093:         ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6       noro     5094:         CZ(mr) = CZ(m);
1.1       noro     5095:         ndl_copy(DL(t),DL(mr));
                   5096:     }
                   5097:     NV(p)++;
                   5098:     BDY(p) = mr0;
                   5099: }
                   5100:
                   5101: void ndv_dehomogenize(NDV p,struct order_spec *ord)
                   5102: {
                   5103:     int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
                   5104:     int pos;
                   5105:     Q *w;
                   5106:     Q dvr,t;
                   5107:     NMV m,r;
                   5108:
                   5109:     len = p->len;
                   5110:     newnvar = nd_nvar-1;
                   5111:     newexporigin = nd_get_exporigin(ord);
                   5112:     if ( nd_module ) newmpos = newexporigin-1;
                   5113:     newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
                   5114:     for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
                   5115:         ndl_dehomogenize(DL(m));
                   5116:     if ( newwpd != nd_wpd ) {
                   5117:         newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
                   5118:         for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6       noro     5119:             CZ(r) = CZ(m);
1.1       noro     5120:             if ( nd_module ) pos = MPOS(DL(m));
                   5121:             for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   5122:             adj = nd_exporigin-newexporigin;
                   5123:             for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
                   5124:             if ( nd_module ) {
                   5125:                 DL(r)[newmpos] = pos;
                   5126:             }
                   5127:         }
                   5128:     }
                   5129:     NV(p)--;
                   5130: }
                   5131:
                   5132: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
                   5133: {
                   5134:     int i;
                   5135:     P *tpl,*tpl1;
                   5136:     NODE l;
                   5137:     P h,gcd,t;
                   5138:
                   5139:     tpl = (P *)MALLOC(m*sizeof(P));
                   5140:     tpl1 = (P *)MALLOC(m*sizeof(P));
                   5141:     bcopy(pl,tpl,m*sizeof(P));
                   5142:     gcd = (P)ONE;
                   5143:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   5144:         h = (P)BDY(l);
                   5145:         while ( 1 ) {
                   5146:             for ( i = 0; i < m; i++ )
                   5147:                 if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
                   5148:                     break;
                   5149:             if ( i == m ) {
                   5150:                 bcopy(tpl1,tpl,m*sizeof(P));
                   5151:                 mulp(vl,gcd,h,&t); gcd = t;
                   5152:             } else
                   5153:                 break;
                   5154:         }
                   5155:     }
                   5156:     if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
                   5157:     if ( full ) {
                   5158:         heu_nezgcdnpz(vl,tpl,m,&t);
                   5159:         mulp(vl,gcd,t,pr);
                   5160:     } else
                   5161:         *pr = gcd;
                   5162: }
                   5163:
                   5164: void removecont_array(P *p,int n,int full)
                   5165: {
                   5166:     int all_p,all_q,i;
                   5167:     Z *c;
                   5168:     P *w;
                   5169:     P t,s;
                   5170:
                   5171:     for ( all_q = 1, i = 0; i < n; i++ )
                   5172:         all_q = all_q && NUM(p[i]);
                   5173:     if ( all_q ) {
                   5174:         removecont_array_q((Z *)p,n);
                   5175:     } else {
                   5176:         c = (Z *)MALLOC(n*sizeof(Z));
                   5177:         w = (P *)MALLOC(n*sizeof(P));
                   5178:         for ( i = 0; i < n; i++ ) {
                   5179:             ptozp(p[i],1,(Q *)&c[i],&w[i]);
                   5180:         }
                   5181:         removecont_array_q(c,n);
                   5182:         nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
                   5183:         for ( i = 0; i < n; i++ ) {
                   5184:             divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
                   5185:         }
                   5186:     }
                   5187: }
                   5188:
                   5189: /* c is an int array */
                   5190:
                   5191: void removecont_array_q(Z *c,int n)
                   5192: {
                   5193:   struct oVECT v;
                   5194:   Z d0,d1,a,u,u1,gcd;
                   5195:   int i,j;
                   5196:   Z *q,*r;
                   5197:
                   5198:   q = (Z *)MALLOC(n*sizeof(Z));
                   5199:   r = (Z *)MALLOC(n*sizeof(Z));
                   5200:   v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   5201:   gcdvz_estimate(&v,&d0);
                   5202:   for ( i = 0; i < n; i++ ) {
                   5203:     divqrz(c[i],d0,&q[i],&r[i]);
                   5204:   }
                   5205:   for ( i = 0; i < n; i++ ) if ( r[i] ) break;
                   5206:   if ( i < n ) {
                   5207:     v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   5208:     gcdvz(&v,&d1);
                   5209:     gcdz(d0,d1,&gcd);
1.6       noro     5210:     /* exact division */
                   5211:     divsz(d0,gcd,&a);
1.1       noro     5212:     for ( i = 0; i < n; i++ ) {
                   5213:       mulz(a,q[i],&u);
                   5214:       if ( r[i] ) {
1.6       noro     5215:         /* exact division */
                   5216:         divsz(r[i],gcd,&u1);
1.1       noro     5217:         addz(u,u1,&q[i]);
                   5218:       } else
                   5219:         q[i] = u;
                   5220:     }
                   5221:   }
                   5222:   for ( i = 0; i < n; i++ ) c[i] = q[i];
                   5223: }
                   5224:
1.4       noro     5225: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
                   5226:
                   5227: void mpz_removecont_array(mpz_t *c,int n)
                   5228: {
                   5229:   mpz_t d0,a,u,u1,gcd;
                   5230:   int i,j;
1.13      noro     5231:   static mpz_t *q,*r;
                   5232:   static int c_len = 0;
1.4       noro     5233:
                   5234:   for ( i = 0; i < n; i++ )
                   5235:     if ( mpz_sgn(c[i]) ) break;
                   5236:   if ( i == n ) return;
                   5237:   gcdv_mpz_estimate(d0,c,n);
1.13      noro     5238:   if ( n > c_len ) {
                   5239:     q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   5240:     r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   5241:     c_len = n;
                   5242:   }
1.4       noro     5243:   for ( i = 0; i < n; i++ ) {
                   5244:     mpz_init(q[i]); mpz_init(r[i]);
                   5245:     mpz_fdiv_qr(q[i],r[i],c[i],d0);
                   5246:   }
                   5247:   for ( i = 0; i < n; i++ )
                   5248:     if ( mpz_sgn(r[i]) ) break;
                   5249:   mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
                   5250:   if ( i < n ) {
                   5251:     mpz_gcd(gcd,d0,r[i]);
                   5252:     for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
                   5253:     mpz_div(a,d0,gcd);
                   5254:     for ( i = 0; i < n; i++ ) {
                   5255:       mpz_mul(u,a,q[i]);
                   5256:       if ( mpz_sgn(r[i]) ) {
                   5257:         mpz_div(u1,r[i],gcd);
                   5258:         mpz_add(q[i],u,u1);
                   5259:       } else
                   5260:         mpz_set(q[i],u);
                   5261:     }
                   5262:   }
                   5263:   for ( i = 0; i < n; i++ )
                   5264:     mpz_set(c[i],q[i]);
                   5265: }
                   5266:
1.1       noro     5267: void nd_mul_c(int mod,ND p,int mul)
                   5268: {
                   5269:     NM m;
                   5270:     int c,c1;
                   5271:
                   5272:     if ( !p ) return;
                   5273:     if ( mul == 1 ) return;
                   5274:     if ( mod == -1 )
                   5275:         for ( m = BDY(p); m; m = NEXT(m) )
                   5276:             CM(m) = _mulsf(CM(m),mul);
                   5277:     else
                   5278:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5279:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   5280:         }
                   5281: }
                   5282:
                   5283: void nd_mul_c_lf(ND p,Z mul)
                   5284: {
                   5285:     NM m;
                   5286:     Z c;
                   5287:
                   5288:     if ( !p ) return;
                   5289:     if ( UNIZ(mul) ) return;
                   5290:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   5291:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   5292:     }
                   5293: }
                   5294:
                   5295: void nd_mul_c_q(ND p,P mul)
                   5296: {
                   5297:     NM m;
                   5298:     P c;
                   5299:
                   5300:     if ( !p ) return;
                   5301:     if ( UNIQ(mul) ) return;
                   5302:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   5303:         mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
                   5304:     }
                   5305: }
                   5306:
                   5307: void nd_mul_c_p(VL vl,ND p,P mul)
                   5308: {
                   5309:     NM m;
                   5310:     P c;
                   5311:
                   5312:     if ( !p ) return;
                   5313:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   5314:         mulp(vl,CP(m),mul,&c); CP(m) = c;
                   5315:     }
                   5316: }
                   5317:
                   5318: void nd_free(ND p)
                   5319: {
                   5320:     NM t,s;
                   5321:
                   5322:     if ( !p ) return;
                   5323:     t = BDY(p);
                   5324:     while ( t ) {
                   5325:         s = NEXT(t);
                   5326:         FREENM(t);
                   5327:         t = s;
                   5328:     }
                   5329:     FREEND(p);
                   5330: }
                   5331:
                   5332: void ndv_free(NDV p)
                   5333: {
                   5334:     GCFREE(BDY(p));
                   5335: }
                   5336:
                   5337: void nd_append_red(UINT *d,int i)
                   5338: {
                   5339:     RHist m,m0;
                   5340:     int h;
                   5341:
                   5342:     NEWRHist(m);
                   5343:     h = ndl_hash_value(d);
                   5344:     m->index = i;
                   5345:     ndl_copy(d,DL(m));
                   5346:     NEXT(m) = nd_red[h];
                   5347:     nd_red[h] = m;
                   5348: }
                   5349:
                   5350: UINT *ndv_compute_bound(NDV p)
                   5351: {
                   5352:     UINT *d1,*d2,*t;
                   5353:     UINT u;
                   5354:     int i,j,k,l,len,ind;
                   5355:     NMV m;
                   5356:
                   5357:     if ( !p )
                   5358:         return 0;
                   5359:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5360:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5361:     len = LEN(p);
                   5362:     m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   5363:     for ( i = 1; i < len; i++, NMV_ADV(m) ) {
                   5364:         ndl_max(DL(m),d1,d2);
                   5365:         t = d1; d1 = d2; d2 = t;
                   5366:     }
                   5367:     l = nd_nvar+31;
                   5368:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   5369:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   5370:         u = d1[i];
                   5371:         k = (nd_epw-1)*nd_bpe;
                   5372:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   5373:             t[ind] = (u>>k)&nd_mask0;
                   5374:     }
                   5375:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   5376:     return t;
                   5377: }
                   5378:
                   5379: UINT *nd_compute_bound(ND p)
                   5380: {
                   5381:     UINT *d1,*d2,*t;
                   5382:     UINT u;
                   5383:     int i,j,k,l,len,ind;
                   5384:     NM m;
                   5385:
                   5386:     if ( !p )
                   5387:         return 0;
                   5388:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5389:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5390:     len = LEN(p);
                   5391:     m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   5392:     for ( m = NEXT(m); m; m = NEXT(m) ) {
                   5393:         ndl_lcm(DL(m),d1,d2);
                   5394:         t = d1; d1 = d2; d2 = t;
                   5395:     }
                   5396:     l = nd_nvar+31;
                   5397:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   5398:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   5399:         u = d1[i];
                   5400:         k = (nd_epw-1)*nd_bpe;
                   5401:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   5402:             t[ind] = (u>>k)&nd_mask0;
                   5403:     }
                   5404:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   5405:     return t;
                   5406: }
                   5407:
                   5408: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
                   5409: /* of a term. In this case we need additional 1 word. */
                   5410:
                   5411: int nd_get_exporigin(struct order_spec *ord)
                   5412: {
                   5413:     switch ( ord->id ) {
1.21      noro     5414:         case 0: case 2: case 256: case 258: case 300:
1.1       noro     5415:             return 1+nd_module;
                   5416:         case 1: case 257:
                   5417:             /* block order */
                   5418:             /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
                   5419:             /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
                   5420:             return ord->ord.block.length+1+nd_module;
                   5421:         case 3: case 259:
                   5422: #if 0
                   5423:             error("nd_get_exporigin : composite order is not supported yet.");
                   5424: #else
                   5425:             return 1+nd_module;
                   5426: #endif
                   5427:         default:
                   5428:             error("nd_get_exporigin : ivalid argument.");
                   5429:             return 0;
                   5430:     }
                   5431: }
                   5432:
                   5433: void nd_setup_parameters(int nvar,int max) {
                   5434:     int i,j,n,elen,ord_o,ord_l,l,s,wpd;
                   5435:     struct order_pair *op;
                   5436:
                   5437:     nd_nvar = nvar;
                   5438:     if ( max ) {
                   5439:         /* XXX */
                   5440:         if ( do_weyl ) nd_bpe = 32;
                   5441:         else if ( max < 2 ) nd_bpe = 1;
                   5442:         else if ( max < 4 ) nd_bpe = 2;
                   5443:         else if ( max < 8 ) nd_bpe = 3;
                   5444:         else if ( max < 16 ) nd_bpe = 4;
                   5445:         else if ( max < 32 ) nd_bpe = 5;
                   5446:         else if ( max < 64 ) nd_bpe = 6;
                   5447:         else if ( max < 256 ) nd_bpe = 8;
                   5448:         else if ( max < 1024 ) nd_bpe = 10;
                   5449:         else if ( max < 65536 ) nd_bpe = 16;
                   5450:         else nd_bpe = 32;
                   5451:     }
                   5452:     if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
                   5453:         UINT t;
                   5454:     int st;
                   5455:         int *v;
                   5456:   /* t = max(weights) */
                   5457:         t = 0;
                   5458:         if ( current_dl_weight_vector )
                   5459:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                   5460:                 if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
                   5461:                 if ( t < st ) t = st;
                   5462:             }
                   5463:         if ( nd_matrix )
                   5464:             for ( i = 0; i < nd_matrix_len; i++ )
                   5465:                 for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
                   5466:                     if ( (st=v[j]) < 0 ) st = -st;
                   5467:                     if ( t < st ) t = st;
                   5468:         }
                   5469:         /* i = bitsize of t */
                   5470:         for ( i = 0; t; t >>=1, i++ );
                   5471:         /* i += bitsize of nd_nvar */
                   5472:         for ( t = nd_nvar; t; t >>=1, i++);
                   5473:         /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
                   5474:         if ( (nd_bpe+i) >= 31 )
                   5475:             error("nd_setup_parameters : too large weight");
                   5476:     }
                   5477:     nd_epw = (sizeof(UINT)*8)/nd_bpe;
                   5478:     elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   5479:     nd_exporigin = nd_get_exporigin(nd_ord);
                   5480:     wpd = nd_exporigin+elen;
                   5481:     if ( nd_module )
                   5482:         nd_mpos = nd_exporigin-1;
                   5483:     else
                   5484:         nd_mpos = -1;
                   5485:     if ( wpd != nd_wpd ) {
                   5486:         nd_free_private_storage();
                   5487:         nd_wpd = wpd;
                   5488:     }
                   5489:     if ( nd_bpe < 32 ) {
                   5490:         nd_mask0 = (1<<nd_bpe)-1;
                   5491:     } else {
                   5492:         nd_mask0 = 0xffffffff;
                   5493:     }
                   5494:     bzero(nd_mask,sizeof(nd_mask));
                   5495:     nd_mask1 = 0;
                   5496:     for ( i = 0; i < nd_epw; i++ ) {
                   5497:         nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   5498:         nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   5499:     }
                   5500:     nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
                   5501:     nd_epos = nd_create_epos(nd_ord);
                   5502:     nd_blockmask = nd_create_blockmask(nd_ord);
                   5503:     nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
                   5504: }
                   5505:
                   5506: ND_pairs nd_reconstruct(int trace,ND_pairs d)
                   5507: {
                   5508:     int i,obpe,oadv,h;
                   5509:     static NM prev_nm_free_list;
                   5510:     static ND_pairs prev_ndp_free_list;
                   5511:     RHist mr0,mr;
                   5512:     RHist r;
                   5513:     RHist *old_red;
                   5514:     ND_pairs s0,s,t;
                   5515:     EPOS oepos;
                   5516:
                   5517:     obpe = nd_bpe;
                   5518:     oadv = nmv_adv;
                   5519:     oepos = nd_epos;
                   5520:     if ( obpe < 2 ) nd_bpe = 2;
                   5521:     else if ( obpe < 3 ) nd_bpe = 3;
                   5522:     else if ( obpe < 4 ) nd_bpe = 4;
                   5523:     else if ( obpe < 5 ) nd_bpe = 5;
                   5524:     else if ( obpe < 6 ) nd_bpe = 6;
                   5525:     else if ( obpe < 8 ) nd_bpe = 8;
                   5526:     else if ( obpe < 10 ) nd_bpe = 10;
                   5527:     else if ( obpe < 16 ) nd_bpe = 16;
                   5528:     else if ( obpe < 32 ) nd_bpe = 32;
                   5529:     else error("nd_reconstruct : exponent too large");
                   5530:
                   5531:     nd_setup_parameters(nd_nvar,0);
                   5532:     prev_nm_free_list = _nm_free_list;
                   5533:     prev_ndp_free_list = _ndp_free_list;
                   5534:     _nm_free_list = 0;
                   5535:     _ndp_free_list = 0;
                   5536:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   5537:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   5538:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   5539:     }
                   5540:     if ( trace )
                   5541:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   5542:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   5543:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   5544:         }
                   5545:     s0 = 0;
                   5546:     for ( t = d; t; t = NEXT(t) ) {
                   5547:         NEXTND_pairs(s0,s);
                   5548:         s->i1 = t->i1;
                   5549:         s->i2 = t->i2;
1.24      noro     5550:         s->sig = t->sig;
1.1       noro     5551:         SG(s) = SG(t);
                   5552:         ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   5553:     }
                   5554:
                   5555:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   5556:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   5557:         old_red[i] = nd_red[i];
                   5558:         nd_red[i] = 0;
                   5559:     }
                   5560:     for ( i = 0; i < REDTAB_LEN; i++ )
                   5561:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   5562:             NEWRHist(mr);
                   5563:             mr->index = r->index;
                   5564:             SG(mr) = SG(r);
                   5565:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   5566:             h = ndl_hash_value(DL(mr));
                   5567:             NEXT(mr) = nd_red[h];
                   5568:             nd_red[h] = mr;
1.24      noro     5569:             mr->sig = r->sig;
1.1       noro     5570:         }
                   5571:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   5572:     old_red = 0;
                   5573:     for ( i = 0; i < nd_psn; i++ ) {
                   5574:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   5575:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24      noro     5576:         r->sig = nd_psh[i]->sig;
1.1       noro     5577:         nd_psh[i] = r;
                   5578:     }
                   5579:     if ( s0 ) NEXT(s) = 0;
                   5580:     prev_nm_free_list = 0;
                   5581:     prev_ndp_free_list = 0;
                   5582: #if 0
                   5583:     GC_gcollect();
                   5584: #endif
                   5585:     return s0;
                   5586: }
                   5587:
1.30      noro     5588: void nd_reconstruct_s(int trace,ND_pairs *d)
                   5589: {
                   5590:     int i,obpe,oadv,h;
                   5591:     static NM prev_nm_free_list;
                   5592:     static ND_pairs prev_ndp_free_list;
                   5593:     RHist mr0,mr;
                   5594:     RHist r;
                   5595:     RHist *old_red;
                   5596:     ND_pairs s0,s,t;
                   5597:     EPOS oepos;
                   5598:
                   5599:     obpe = nd_bpe;
                   5600:     oadv = nmv_adv;
                   5601:     oepos = nd_epos;
                   5602:     if ( obpe < 2 ) nd_bpe = 2;
                   5603:     else if ( obpe < 3 ) nd_bpe = 3;
                   5604:     else if ( obpe < 4 ) nd_bpe = 4;
                   5605:     else if ( obpe < 5 ) nd_bpe = 5;
                   5606:     else if ( obpe < 6 ) nd_bpe = 6;
                   5607:     else if ( obpe < 8 ) nd_bpe = 8;
                   5608:     else if ( obpe < 10 ) nd_bpe = 10;
                   5609:     else if ( obpe < 16 ) nd_bpe = 16;
                   5610:     else if ( obpe < 32 ) nd_bpe = 32;
                   5611:     else error("nd_reconstruct_s : exponent too large");
                   5612:
                   5613:     nd_setup_parameters(nd_nvar,0);
                   5614:     prev_nm_free_list = _nm_free_list;
                   5615:     prev_ndp_free_list = _ndp_free_list;
                   5616:     _nm_free_list = 0;
                   5617:     _ndp_free_list = 0;
                   5618:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   5619:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   5620:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   5621:     }
                   5622:     if ( trace )
                   5623:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   5624:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   5625:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   5626:         }
                   5627:
                   5628:     for ( i = 0; i < nd_nbase; i++ ) {
                   5629:       s0 = 0;
                   5630:       for ( t = d[i]; t; t = NEXT(t) ) {
                   5631:           NEXTND_pairs(s0,s);
                   5632:           s->i1 = t->i1;
                   5633:           s->i2 = t->i2;
                   5634:           s->sig = t->sig;
                   5635:           SG(s) = SG(t);
                   5636:           ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   5637:       }
                   5638:       d[i] = s0;
                   5639:     }
                   5640:
                   5641:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   5642:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   5643:         old_red[i] = nd_red[i];
                   5644:         nd_red[i] = 0;
                   5645:     }
                   5646:     for ( i = 0; i < REDTAB_LEN; i++ )
                   5647:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   5648:             NEWRHist(mr);
                   5649:             mr->index = r->index;
                   5650:             SG(mr) = SG(r);
                   5651:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   5652:             h = ndl_hash_value(DL(mr));
                   5653:             NEXT(mr) = nd_red[h];
                   5654:             nd_red[h] = mr;
                   5655:             mr->sig = r->sig;
                   5656:         }
                   5657:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   5658:     old_red = 0;
                   5659:     for ( i = 0; i < nd_psn; i++ ) {
                   5660:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   5661:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
                   5662:         r->sig = nd_psh[i]->sig;
                   5663:         nd_psh[i] = r;
                   5664:     }
                   5665:     if ( s0 ) NEXT(s) = 0;
                   5666:     prev_nm_free_list = 0;
                   5667:     prev_ndp_free_list = 0;
                   5668: #if 0
                   5669:     GC_gcollect();
                   5670: #endif
                   5671: }
                   5672:
1.1       noro     5673: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
                   5674: {
                   5675:     int n,i,ei,oepw,omask0,j,s,ord_l,l;
                   5676:     struct order_pair *op;
                   5677:
                   5678:     n = nd_nvar;
                   5679:     oepw = (sizeof(UINT)*8)/obpe;
                   5680:     omask0 = (1<<obpe)-1;
                   5681:     TD(r) = TD(d);
                   5682:     for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
                   5683:     if ( nd_blockmask ) {
                   5684:         l = nd_blockmask->n;
                   5685:         op = nd_blockmask->order_pair;
                   5686:         for ( i = 1; i < nd_exporigin; i++ )
                   5687:             r[i] = d[i];
                   5688:         for ( j = 0, s = 0; j < l; j++ ) {
                   5689:             ord_l = op[j].length;
                   5690:             for ( i = 0; i < ord_l; i++, s++ ) {
                   5691:                 ei =  GET_EXP_OLD(d,s);
                   5692:                 PUT_EXP(r,s,ei);
                   5693:             }
                   5694:         }
                   5695:     } else {
                   5696:         for ( i = 0; i < n; i++ ) {
                   5697:             ei = GET_EXP_OLD(d,i);
                   5698:             PUT_EXP(r,i,ei);
                   5699:         }
                   5700:     }
                   5701:     if ( nd_module ) MPOS(r) = MPOS(d);
                   5702: }
                   5703:
                   5704: ND nd_copy(ND p)
                   5705: {
                   5706:     NM m,mr,mr0;
                   5707:     int c,n;
                   5708:     ND r;
                   5709:
                   5710:     if ( !p )
                   5711:         return 0;
                   5712:     else {
                   5713:         for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   5714:             NEXTNM(mr0,mr);
                   5715:             CM(mr) = CM(m);
                   5716:             ndl_copy(DL(m),DL(mr));
                   5717:         }
                   5718:         NEXT(mr) = 0;
                   5719:         MKND(NV(p),mr0,LEN(p),r);
                   5720:         SG(r) = SG(p);
                   5721:         return r;
                   5722:     }
                   5723: }
                   5724:
                   5725: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
                   5726: {
                   5727:     NM m1,m2;
                   5728:     NDV p1,p2;
                   5729:     ND t1,t2;
                   5730:     UINT *lcm;
                   5731:     P gp,tp;
                   5732:     Z g,t;
                   5733:     Z iq;
                   5734:     int td;
                   5735:     LIST hist;
                   5736:     NODE node;
                   5737:     DP d;
                   5738:
                   5739:     if ( !mod && nd_demand ) {
                   5740:         p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
                   5741:     } else {
                   5742:         if ( trace ) {
                   5743:             p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   5744:         } else {
                   5745:             p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   5746:         }
                   5747:     }
                   5748:     lcm = LCM(p);
                   5749:     NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
                   5750:     if ( ndl_check_bound2(p->i1,DL(m1)) ) {
                   5751:         FREENM(m1); return 0;
                   5752:     }
                   5753:     NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
                   5754:     if ( ndl_check_bound2(p->i2,DL(m2)) ) {
                   5755:         FREENM(m1); FREENM(m2); return 0;
                   5756:     }
                   5757:
                   5758:     if ( mod == -1 ) {
                   5759:         CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
                   5760:     } else if ( mod > 0 ) {
                   5761:         CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
                   5762:     } else if ( mod == -2 ) {
                   5763:         CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
                   5764:     } else if ( nd_vc ) {
                   5765:         ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
                   5766:         divsp(nd_vc,HCP(p2),gp,&CP(m1));
                   5767:         divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
                   5768:     } else {
1.6       noro     5769:         igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1       noro     5770:     }
                   5771:     t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
                   5772:     *rp = nd_add(mod,t1,t2);
                   5773:     if ( nd_gentrace ) {
                   5774:         /* nd_tracelist is initialized */
1.6       noro     5775:         STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     5776:         MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6       noro     5777:         STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     5778:         MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
                   5779:         nd_tracelist = node;
                   5780:     }
1.24      noro     5781:     if ( *rp )
                   5782:       (*rp)->sig = p->sig;
1.1       noro     5783:     FREENM(m1); FREENM(m2);
                   5784:     return 1;
                   5785: }
                   5786:
                   5787: void ndv_mul_c(int mod,NDV p,int mul)
                   5788: {
                   5789:     NMV m;
                   5790:     int c,c1,len,i;
                   5791:
                   5792:     if ( !p ) return;
                   5793:     len = LEN(p);
                   5794:     if ( mod == -1 )
                   5795:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   5796:             CM(m) = _mulsf(CM(m),mul);
                   5797:     else
                   5798:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   5799:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   5800:         }
                   5801: }
                   5802:
                   5803: void ndv_mul_c_lf(NDV p,Z mul)
                   5804: {
                   5805:     NMV m;
                   5806:     Z c;
                   5807:     int len,i;
                   5808:
                   5809:     if ( !p ) return;
                   5810:     len = LEN(p);
                   5811:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   5812:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   5813:     }
                   5814: }
                   5815:
                   5816: /* for nd_det */
                   5817: void ndv_mul_c_q(NDV p,Z mul)
                   5818: {
                   5819:     NMV m;
                   5820:     Z c;
                   5821:     int len,i;
                   5822:
                   5823:     if ( !p ) return;
                   5824:     len = LEN(p);
                   5825:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     5826:         mulz(CZ(m),mul,&c); CZ(m) = c;
1.1       noro     5827:     }
                   5828: }
                   5829:
                   5830: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
                   5831:     int n2,i,j,l,n,tlen;
                   5832:     UINT *d0;
                   5833:     NM *tab,*psum;
                   5834:     ND s,r;
                   5835:     NM t;
                   5836:     NMV m1;
                   5837:
                   5838:     if ( !p ) return 0;
                   5839:     n = NV(p); n2 = n>>1;
                   5840:     d0 = DL(m0);
                   5841:     l = LEN(p);
                   5842:     for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   5843:     tab = (NM *)MALLOC(tlen*sizeof(NM));
                   5844:     psum = (NM *)MALLOC(tlen*sizeof(NM));
                   5845:     for ( i = 0; i < tlen; i++ ) psum[i] = 0;
                   5846:     m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   5847:     for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
                   5848:         /* m0(NM) * m1(NMV) => tab(NM) */
                   5849:         weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
                   5850:         for ( j = 0; j < tlen; j++ ) {
                   5851:             if ( tab[j] ) {
                   5852:                 NEXT(tab[j]) = psum[j];    psum[j] = tab[j];
                   5853:             }
                   5854:         }
                   5855:     }
                   5856:     for ( i = tlen-1, r = 0; i >= 0; i-- )
                   5857:         if ( psum[i] ) {
                   5858:             for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   5859:             MKND(n,psum[i],j,s);
                   5860:             r = nd_add(mod,r,s);
                   5861:         }
                   5862:     if ( r ) SG(r) = SG(p)+TD(d0);
                   5863:     return r;
                   5864: }
                   5865:
                   5866: /* product of monomials */
                   5867: /* XXX block order is not handled correctly */
                   5868:
                   5869: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   5870: {
                   5871:     int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
                   5872:     UINT *d0,*d1,*d,*dt,*ctab;
                   5873:     Z *ctab_q;
                   5874:     Z q,q1;
                   5875:     UINT c0,c1,c;
                   5876:     NM *p;
                   5877:     NM m,t;
                   5878:     int mpos;
                   5879:
                   5880:     for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   5881:     if ( !m0 || !m1 ) return;
                   5882:     d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
                   5883:     if ( nd_module )
                   5884:         if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
                   5885:
                   5886:     NEWNM(m); d = DL(m);
                   5887:     if ( mod ) {
                   5888:         c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
                   5889:     } else if ( nd_vc )
                   5890:         mulp(nd_vc,CP(m0),CP(m1),&CP(m));
                   5891:   else
1.6       noro     5892:         mulz(CZ(m0),CZ(m1),&CZ(m));
1.1       noro     5893:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   5894:     homo = n&1 ? 1 : 0;
                   5895:     if ( homo ) {
                   5896:         /* offset of h-degree */
                   5897:         h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   5898:         PUT_EXP(DL(m),n-1,h);
                   5899:         TD(DL(m)) = h;
                   5900:         if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   5901:     }
                   5902:     tab[0] = m;
                   5903:     NEWNM(m); d = DL(m);
                   5904:     for ( i = 0, curlen = 1; i < n2; i++ ) {
                   5905:         a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   5906:         k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   5907:         /* xi^a*(Di^k*xi^l)*Di^b */
                   5908:         a += l; b += k;
                   5909:         s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
                   5910:         if ( !k || !l ) {
                   5911:             for ( j = 0; j < curlen; j++ )
                   5912:                 if ( (t = tab[j]) != 0 ) {
                   5913:                     dt = DL(t);
                   5914:                     PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
                   5915:                     if ( nd_blockmask ) ndl_weight_mask(dt);
                   5916:                 }
                   5917:             curlen *= k+1;
                   5918:             continue;
                   5919:         }
                   5920:         min = MIN(k,l);
                   5921:         if ( mod ) {
                   5922:             ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
                   5923:             mkwcm(k,l,mod,(int *)ctab);
                   5924:         } else {
                   5925:             ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
                   5926:             mkwc(k,l,ctab_q);
                   5927:         }
                   5928:         for ( j = min; j >= 0; j-- ) {
                   5929:             for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
                   5930:             PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
                   5931:             h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
                   5932:             if ( homo ) {
                   5933:                 TD(d) = s;
                   5934:                 PUT_EXP(d,n-1,s-h);
                   5935:             } else TD(d) = h;
                   5936:             if ( nd_blockmask ) ndl_weight_mask(d);
                   5937:             if ( mod ) c = ctab[j];
                   5938:             else q = ctab_q[j];
                   5939:             p = tab+curlen*j;
                   5940:             if ( j == 0 ) {
                   5941:                 for ( u = 0; u < curlen; u++, p++ ) {
                   5942:                     if ( tab[u] ) {
                   5943:                         ndl_addto(DL(tab[u]),d);
                   5944:                         if ( mod ) {
                   5945:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
                   5946:                         } else if ( nd_vc )
                   5947:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
                   5948:             else {
1.6       noro     5949:                             mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1       noro     5950:                         }
                   5951:                     }
                   5952:                 }
                   5953:             } else {
                   5954:                 for ( u = 0; u < curlen; u++, p++ ) {
                   5955:                     if ( tab[u] ) {
                   5956:                         NEWNM(t);
                   5957:                         ndl_add(DL(tab[u]),d,DL(t));
                   5958:                         if ( mod ) {
                   5959:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
                   5960:                         } else if ( nd_vc )
                   5961:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
                   5962:             else
1.6       noro     5963:                             mulz(CZ(tab[u]),q,&CZ(t));
1.1       noro     5964:                         *p = t;
                   5965:                     }
                   5966:                 }
                   5967:             }
                   5968:         }
                   5969:         curlen *= k+1;
                   5970:     }
                   5971:     FREENM(m);
                   5972:     if ( nd_module ) {
                   5973:         mpos = MPOS(d1);
                   5974:         for ( i = 0; i < tlen; i++ )
                   5975:             if ( tab[i] ) {
                   5976:                 d = DL(tab[i]);
                   5977:                 MPOS(d) = mpos;
                   5978:                 TD(d) = ndl_weight(d);
                   5979:             }
                   5980:     }
                   5981: }
                   5982:
                   5983: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   5984: {
                   5985:     NM mr,mr0;
                   5986:     NMV m;
                   5987:     UINT *d,*dt,*dm;
                   5988:     int c,n,td,i,c1,c2,len;
                   5989:     Q q;
                   5990:     ND r;
                   5991:
                   5992:     if ( !p ) return 0;
                   5993:     else {
                   5994:         n = NV(p); m = BDY(p);
                   5995:         d = DL(m0);
                   5996:         len = LEN(p);
                   5997:         mr0 = 0;
                   5998:         td = TD(d);
                   5999:         c = CM(m0);
                   6000:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6001:             NEXTNM(mr0,mr);
                   6002:             CM(mr) = 1;
                   6003:             ndl_add(DL(m),d,DL(mr));
                   6004:         }
                   6005:         NEXT(mr) = 0;
                   6006:         MKND(NV(p),mr0,len,r);
                   6007:         SG(r) = SG(p) + TD(d);
                   6008:         return r;
                   6009:     }
                   6010: }
                   6011:
                   6012: ND ndv_mul_nm(int mod,NM m0,NDV p)
                   6013: {
                   6014:     NM mr,mr0;
                   6015:     NMV m;
                   6016:     UINT *d,*dt,*dm;
                   6017:     int c,n,td,i,c1,c2,len;
                   6018:     P q;
                   6019:     ND r;
                   6020:
                   6021:     if ( !p ) return 0;
                   6022:     else if ( do_weyl ) {
                   6023:         if ( mod < 0 ) {
                   6024:             error("ndv_mul_nm : not implemented (weyl)");
                   6025:             return 0;
                   6026:         } else
                   6027:             return weyl_ndv_mul_nm(mod,m0,p);
                   6028:     } else {
                   6029:         n = NV(p); m = BDY(p);
                   6030:         d = DL(m0);
                   6031:         len = LEN(p);
                   6032:         mr0 = 0;
                   6033:         td = TD(d);
                   6034:         if ( mod == -1 ) {
                   6035:             c = CM(m0);
                   6036:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6037:                 NEXTNM(mr0,mr);
                   6038:                 CM(mr) = _mulsf(CM(m),c);
                   6039:                 ndl_add(DL(m),d,DL(mr));
                   6040:             }
                   6041:         } else if ( mod == -2 ) {
                   6042:             Z cl;
                   6043:             cl = CZ(m0);
                   6044:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6045:                 NEXTNM(mr0,mr);
                   6046:                 mullf(CZ(m),cl,&CZ(mr));
                   6047:                 ndl_add(DL(m),d,DL(mr));
                   6048:             }
                   6049:         } else if ( mod ) {
                   6050:             c = CM(m0);
                   6051:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6052:                 NEXTNM(mr0,mr);
                   6053:                 c1 = CM(m);
                   6054:                 DMAR(c1,c,0,mod,c2);
                   6055:                 CM(mr) = c2;
                   6056:                 ndl_add(DL(m),d,DL(mr));
                   6057:             }
                   6058:         } else {
                   6059:             q = CP(m0);
                   6060:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6061:                 NEXTNM(mr0,mr);
                   6062:                 mulp(nd_vc,CP(m),q,&CP(mr));
                   6063:                 ndl_add(DL(m),d,DL(mr));
                   6064:             }
                   6065:         }
                   6066:         NEXT(mr) = 0;
                   6067:         MKND(NV(p),mr0,len,r);
                   6068:         SG(r) = SG(p) + TD(d);
                   6069:         return r;
                   6070:     }
                   6071: }
                   6072:
                   6073: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
                   6074: {
                   6075:     NM mq0,mq;
                   6076:     NMV tm;
                   6077:     Q q;
                   6078:     int i,nv,sg,c,c1,c2,hindex;
                   6079:     ND p,t,r;
                   6080:
                   6081:     if ( bucket->m < 0 ) return 0;
                   6082:     else {
                   6083:         nv = NV(d);
                   6084:         mq0 = 0;
                   6085:         tm = (NMV)MALLOC(nmv_adv);
                   6086:         while ( 1 ) {
                   6087:             if ( mod > 0 || mod == -1 )
                   6088:               hindex = head_pbucket(mod,bucket);
                   6089:             else if ( mod == -2 )
                   6090:               hindex = head_pbucket_lf(bucket);
                   6091:             else
                   6092:               hindex = head_pbucket_q(bucket);
                   6093:             if ( hindex < 0 ) break;
                   6094:             p = bucket->body[hindex];
                   6095:             NEXTNM(mq0,mq);
                   6096:             ndl_sub(HDL(p),HDL(d),DL(mq));
                   6097:             ndl_copy(DL(mq),DL(tm));
                   6098:             if ( mod ) {
                   6099:                 c1 = invm(HCM(d),mod); c2 = HCM(p);
                   6100:                 DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   6101:                 CM(tm) = mod-c;
                   6102:             } else {
1.6       noro     6103:                 divsz(HCZ(p),HCZ(d),&CZ(mq));
                   6104:                 chsgnz(CZ(mq),&CZ(tm));
1.1       noro     6105:             }
                   6106:             t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
                   6107:             bucket->body[hindex] = nd_remove_head(p);
                   6108:             t = nd_remove_head(t);
                   6109:             add_pbucket(mod,bucket,t);
                   6110:         }
                   6111:         if ( !mq0 )
                   6112:             r = 0;
                   6113:         else {
                   6114:             NEXT(mq) = 0;
                   6115:             for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   6116:             MKND(nv,mq0,i,r);
                   6117:             /* XXX */
                   6118:             SG(r) = HTD(r);
                   6119:         }
                   6120:         return r;
                   6121:     }
                   6122: }
                   6123:
                   6124: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   6125: {
                   6126:     NMV m,mr,mr0,t;
                   6127:     int len,i,k;
                   6128:
                   6129:     if ( !p ) return;
                   6130:     m = BDY(p); len = LEN(p);
                   6131:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   6132:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   6133:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   6134:     t = (NMV)MALLOC(nmv_adv);
                   6135:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6       noro     6136:         CZ(t) = CZ(m);
1.1       noro     6137:         for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   6138:         ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6       noro     6139:         CZ(mr) = CZ(t);
1.1       noro     6140:         ndl_copy(DL(t),DL(mr));
                   6141:     }
                   6142:     BDY(p) = mr0;
                   6143: }
                   6144:
                   6145: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   6146: {
                   6147:     NMV m,mr,mr0;
                   6148:     int len,i;
                   6149:     NDV r;
                   6150:
                   6151:     if ( !p ) return 0;
                   6152:     m = BDY(p); len = LEN(p);
                   6153:     mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   6154:     for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   6155:         ndl_zero(DL(mr));
                   6156:         ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6       noro     6157:         CZ(mr) = CZ(m);
1.1       noro     6158:     }
                   6159:     MKNDV(NV(p),mr0,len,r);
                   6160:     SG(r) = SG(p);
1.24      noro     6161:     r->sig = p->sig;
1.1       noro     6162:     return r;
                   6163: }
                   6164:
                   6165: /* duplicate p */
                   6166:
                   6167: NDV ndv_dup(int mod,NDV p)
                   6168: {
                   6169:     NDV d;
                   6170:     NMV t,m,m0;
                   6171:     int i,len;
                   6172:
                   6173:     if ( !p ) return 0;
                   6174:     len = LEN(p);
                   6175:     m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   6176:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   6177:         ndl_copy(DL(t),DL(m));
1.6       noro     6178:         CZ(m) = CZ(t);
1.1       noro     6179:     }
                   6180:     MKNDV(NV(p),m0,len,d);
                   6181:     SG(d) = SG(p);
                   6182:     return d;
                   6183: }
                   6184:
                   6185: NDV ndv_symbolic(int mod,NDV p)
                   6186: {
                   6187:     NDV d;
                   6188:     NMV t,m,m0;
                   6189:     int i,len;
                   6190:
                   6191:     if ( !p ) return 0;
                   6192:     len = LEN(p);
                   6193:     m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   6194:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   6195:         ndl_copy(DL(t),DL(m));
1.6       noro     6196:         CZ(m) = ONE;
1.1       noro     6197:     }
                   6198:     MKNDV(NV(p),m0,len,d);
                   6199:     SG(d) = SG(p);
                   6200:     return d;
                   6201: }
                   6202:
                   6203: ND nd_dup(ND p)
                   6204: {
                   6205:     ND d;
                   6206:     NM t,m,m0;
                   6207:
                   6208:     if ( !p ) return 0;
                   6209:     for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   6210:         NEXTNM(m0,m);
                   6211:         ndl_copy(DL(t),DL(m));
1.6       noro     6212:         CZ(m) = CZ(t);
1.1       noro     6213:     }
                   6214:     if ( m0 ) NEXT(m) = 0;
                   6215:     MKND(NV(p),m0,LEN(p),d);
                   6216:     SG(d) = SG(p);
                   6217:     return d;
                   6218: }
                   6219:
                   6220: /* XXX if p->len == 0 then it represents 0 */
                   6221:
                   6222: void ndv_mod(int mod,NDV p)
                   6223: {
                   6224:     NMV t,d;
                   6225:     int r,s,u;
                   6226:     int i,len,dlen;
                   6227:     P cp;
                   6228:     Z c;
                   6229:     Obj gfs;
                   6230:
                   6231:     if ( !p ) return;
                   6232:     len = LEN(p);
                   6233:     dlen = 0;
                   6234:     if ( mod == -1 )
                   6235:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   6236:             simp_ff((Obj)CP(t),&gfs);
                   6237:             if ( gfs ) {
                   6238:               r = FTOIF(CONT((GFS)gfs));
                   6239:               CM(d) = r;
                   6240:               ndl_copy(DL(t),DL(d));
                   6241:               NMV_ADV(d);
                   6242:               dlen++;
                   6243:             }
                   6244:         }
                   6245:     else if ( mod == -2 )
                   6246:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   6247:             simp_ff((Obj)CP(t),&gfs);
                   6248:             if ( gfs ) {
                   6249:               lmtolf((LM)gfs,&CZ(d));
                   6250:               ndl_copy(DL(t),DL(d));
                   6251:               NMV_ADV(d);
                   6252:               dlen++;
                   6253:             }
                   6254:         }
                   6255:     else
                   6256:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   6257:             if ( nd_vc ) {
                   6258:                 nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
                   6259:                 c = (Z)cp;
                   6260:             } else
1.6       noro     6261:                 c = CZ(t);
1.1       noro     6262:             r = remqi((Q)c,mod);
                   6263:             if ( r ) {
                   6264:                 CM(d) = r;
                   6265:                 ndl_copy(DL(t),DL(d));
                   6266:                 NMV_ADV(d);
                   6267:                 dlen++;
                   6268:             }
                   6269:         }
                   6270:     LEN(p) = dlen;
                   6271: }
                   6272:
                   6273: NDV ptondv(VL vl,VL dvl,P p)
                   6274: {
                   6275:     ND nd;
                   6276:
                   6277:     nd = ptond(vl,dvl,p);
                   6278:     return ndtondv(0,nd);
                   6279: }
                   6280:
                   6281: void pltozpl(LIST l,Q *cont,LIST *pp)
                   6282: {
1.16      noro     6283:   NODE nd,nd1;
                   6284:   int n;
                   6285:   P *pl;
                   6286:   Q *cl;
                   6287:   int i;
                   6288:   P dmy;
                   6289:   Z dvr,inv;
                   6290:   LIST r;
                   6291:
                   6292:   nd = BDY(l); n = length(nd);
                   6293:   pl = (P *)MALLOC(n*sizeof(P));
                   6294:   cl = (Q *)MALLOC(n*sizeof(Q));
                   6295:   for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
                   6296:     ptozp((P)BDY(nd),1,&cl[i],&dmy);
                   6297:   }
                   6298:   qltozl(cl,n,&dvr);
                   6299:   divz(ONE,dvr,&inv);
                   6300:   nd = BDY(l);
                   6301:   for ( i = 0; i < n; i++, nd = NEXT(nd) )
                   6302:     divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
                   6303:   nd = 0;
                   6304:   for ( i = n-1; i >= 0; i-- ) {
                   6305:     MKNODE(nd1,pl[i],nd); nd = nd1;
                   6306:   }
                   6307:   MKLIST(r,nd);
                   6308:   *pp = r;
1.1       noro     6309: }
                   6310:
                   6311: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
                   6312:
                   6313: NDV pltondv(VL vl,VL dvl,LIST p)
                   6314: {
                   6315:     int i;
                   6316:     NODE t;
                   6317:     ND r,ri;
                   6318:     NM m;
                   6319:
                   6320:     if ( !nd_module ) error("pltond : module order must be set");
                   6321:     r = 0;
                   6322:     for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
                   6323:         ri = ptond(vl,dvl,(P)BDY(t));
                   6324:         if ( ri )
                   6325:             for ( m = BDY(ri); m; m = NEXT(m) ) {
                   6326:                 MPOS(DL(m)) = i;
                   6327:                 TD(DL(m)) = ndl_weight(DL(m));
                   6328:                 if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   6329:             }
                   6330:         r = nd_add(0,r,ri);
                   6331:     }
                   6332:     return ndtondv(0,r);
                   6333: }
                   6334:
                   6335: ND ptond(VL vl,VL dvl,P p)
                   6336: {
                   6337:     int n,i,j,k,e;
                   6338:     VL tvl;
                   6339:     V v;
                   6340:     DCP dc;
                   6341:     DCP *w;
                   6342:     ND r,s,t,u;
                   6343:     P x;
                   6344:     int c;
                   6345:     UINT *d;
                   6346:     NM m,m0;
                   6347:
                   6348:     if ( !p )
                   6349:         return 0;
                   6350:     else if ( NUM(p) ) {
                   6351:         NEWNM(m);
                   6352:         ndl_zero(DL(m));
                   6353:         if ( !INT((Q)p) )
                   6354:           error("ptond : input must be integer-coefficient");
1.6       noro     6355:         CZ(m) = (Z)p;
1.1       noro     6356:         NEXT(m) = 0;
                   6357:         MKND(nd_nvar,m,1,r);
                   6358:         SG(r) = 0;
                   6359:         return r;
                   6360:     } else {
                   6361:         for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   6362:         w = (DCP *)MALLOC(k*sizeof(DCP));
                   6363:         for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   6364:         for ( i = 0, tvl = dvl, v = VR(p);
                   6365:             tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   6366:         if ( !tvl ) {
                   6367:             for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   6368:                 t = ptond(vl,dvl,COEF(w[j]));
                   6369:                 pwrp(vl,x,DEG(w[j]),&p);
                   6370:                 nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   6371:             }
                   6372:             return s;
                   6373:         } else {
                   6374:             NEWNM(m0); d = DL(m0);
                   6375:             for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6       noro     6376:                 ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1       noro     6377:                 TD(d) = MUL_WEIGHT(e,i);
                   6378:                 if ( nd_blockmask) ndl_weight_mask(d);
                   6379:                 if ( nd_module ) MPOS(d) = 0;
                   6380:                 t = ptond(vl,dvl,COEF(w[j]));
                   6381:                 for ( m = BDY(t); m; m = NEXT(m) )
                   6382:                     ndl_addto(DL(m),d);
                   6383:                 SG(t) += TD(d);
                   6384:                 s = nd_add(0,s,t);
                   6385:             }
                   6386:             FREENM(m0);
                   6387:             return s;
                   6388:         }
                   6389:     }
                   6390: }
                   6391:
                   6392: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   6393: {
                   6394:     VL tvl;
                   6395:     int len,n,j,i,e;
                   6396:     NMV m;
                   6397:     Z q;
                   6398:     P c;
                   6399:     UINT *d;
                   6400:     P s,r,u,t,w;
                   6401:     GFS gfs;
                   6402:
                   6403:     if ( !p ) return 0;
                   6404:     else {
                   6405:         len = LEN(p);
                   6406:         n = NV(p);
                   6407:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   6408:         for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   6409:             if ( mod == -1 ) {
                   6410:                e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   6411:             } else if ( mod == -2 ) {
                   6412:                c = (P)CZ(m);
                   6413:             } else if ( mod > 0 ) {
1.6       noro     6414:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     6415:             } else
                   6416:                 c = CP(m);
                   6417:             d = DL(m);
                   6418:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     6419:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     6420:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   6421:             }
                   6422:             addp(vl,s,t,&u); s = u;
                   6423:         }
                   6424:         return s;
                   6425:     }
                   6426: }
                   6427:
                   6428: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
                   6429: {
                   6430:     VL tvl;
                   6431:     int len,n,j,i,e;
                   6432:     NMV m;
                   6433:     Z q;
                   6434:     P c;
                   6435:     UINT *d;
                   6436:     P s,r,u,t,w;
                   6437:     GFS gfs;
                   6438:     P *a;
                   6439:     LIST l;
                   6440:     NODE nd,nd1;
                   6441:
                   6442:     if ( !p ) return 0;
                   6443:     else {
                   6444:         a = (P *)MALLOC((rank+1)*sizeof(P));
                   6445:         for ( i = 0; i <= rank; i++ ) a[i] = 0;
                   6446:         len = LEN(p);
                   6447:         n = NV(p);
                   6448:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   6449:         for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
                   6450:             if ( mod == -1 ) {
                   6451:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   6452:             } else if ( mod ) {
1.6       noro     6453:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     6454:             } else
                   6455:                 c = CP(m);
                   6456:             d = DL(m);
                   6457:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     6458:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     6459:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   6460:             }
                   6461:             addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
                   6462:         }
                   6463:         nd = 0;
                   6464:         for ( i = rank; i > 0; i-- ) {
                   6465:             MKNODE(nd1,a[i],nd); nd = nd1;
                   6466:         }
                   6467:         MKLIST(l,nd);
                   6468:         return l;
                   6469:     }
                   6470: }
                   6471:
                   6472: NDV ndtondv(int mod,ND p)
                   6473: {
                   6474:     NDV d;
                   6475:     NMV m,m0;
                   6476:     NM t;
                   6477:     int i,len;
                   6478:
                   6479:     if ( !p ) return 0;
                   6480:     len = LEN(p);
                   6481:     if ( mod > 0 || mod == -1 )
                   6482:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   6483:     else
                   6484:         m0 = m = MALLOC(len*nmv_adv);
                   6485: #if 0
                   6486:     ndv_alloc += nmv_adv*len;
                   6487: #endif
                   6488:     for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   6489:         ndl_copy(DL(t),DL(m));
1.6       noro     6490:         CZ(m) = CZ(t);
1.1       noro     6491:     }
                   6492:     MKNDV(NV(p),m0,len,d);
                   6493:     SG(d) = SG(p);
1.24      noro     6494:     d->sig = p->sig;
1.1       noro     6495:     return d;
                   6496: }
                   6497:
1.16      noro     6498: static int dmm_comp_nv;
                   6499:
                   6500: int dmm_comp(DMM *a,DMM *b)
                   6501: {
                   6502:    return -compdmm(dmm_comp_nv,*a,*b);
                   6503: }
                   6504:
                   6505: void dmm_sort_by_ord(DMM *a,int len,int nv)
                   6506: {
                   6507:   dmm_comp_nv = nv;
                   6508:   qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
                   6509: }
                   6510:
                   6511: void dpm_sort(DPM p,DPM *rp)
                   6512: {
                   6513:   DMM t,t1;
                   6514:   int len,i,n;
                   6515:   DMM *a;
                   6516:   DPM d;
                   6517:
                   6518:   if ( !p ) *rp = 0;
                   6519:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   6520:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   6521:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   6522:   n = p->nv;
                   6523:   dmm_sort_by_ord(a,len,n);
                   6524:   t = 0;
                   6525:   for ( i = len-1; i >= 0; i-- ) {
                   6526:     NEWDMM(t1);
                   6527:     t1->c = a[i]->c;
                   6528:     t1->dl = a[i]->dl;
                   6529:     t1->pos = a[i]->pos;
                   6530:     t1->next = t;
                   6531:     t = t1;
                   6532:   }
                   6533:   MKDPM(n,t,d);
                   6534:   SG(d) = SG(p);
                   6535:   *rp = d;
                   6536: }
                   6537:
1.18      noro     6538: int dpm_comp(DPM *a,DPM *b)
                   6539: {
1.22      noro     6540:   return -compdpm(CO,*a,*b);
1.18      noro     6541: }
                   6542:
                   6543: NODE dpm_sort_list(NODE l)
                   6544: {
                   6545:   int i,len;
                   6546:   NODE t,t1;
                   6547:   DPM *a;
                   6548:
                   6549:   len = length(l);
                   6550:   a = (DPM *)MALLOC(len*sizeof(DPM));
                   6551:   for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
                   6552:   qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
                   6553:   t = 0;
                   6554:   for ( i = len-1; i >= 0; i-- ) {
                   6555:     MKNODE(t1,(pointer)a[i],t); t = t1;
                   6556:   }
                   6557:   return t;
                   6558: }
                   6559:
1.20      noro     6560: int nmv_comp(NMV a,NMV b)
                   6561: {
1.21      noro     6562:   int t;
                   6563:   t = DL_COMPARE(a->dl,b->dl);
                   6564:   return -t;
1.20      noro     6565: }
                   6566:
1.16      noro     6567: NDV dpmtondv(int mod,DPM p)
                   6568: {
                   6569:   NDV d;
                   6570:   NMV m,m0;
                   6571:   DMM t;
                   6572:   DMM *a;
                   6573:   int i,len,n;
                   6574:
                   6575:   if ( !p ) return 0;
                   6576:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   6577:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   6578:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   6579:   n = p->nv;
                   6580:   dmm_sort_by_ord(a,len,n);
                   6581:   if ( mod > 0 || mod == -1 )
                   6582:     m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   6583:   else
                   6584:     m0 = m = MALLOC(len*nmv_adv);
                   6585: #if 0
                   6586:   ndv_alloc += nmv_adv*len;
                   6587: #endif
                   6588:   for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6589:     dltondl(n,a[i]->dl,DL(m));
                   6590:     MPOS(DL(m)) = a[i]->pos;
1.20      noro     6591:     TD(DL(m)) = ndl_weight(DL(m));
1.16      noro     6592:     CZ(m) = (Z)a[i]->c;
                   6593:   }
1.20      noro     6594:   qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16      noro     6595:   MKNDV(NV(p),m0,len,d);
                   6596:   SG(d) = SG(p);
                   6597:   return d;
                   6598: }
                   6599:
1.1       noro     6600: ND ndvtond(int mod,NDV p)
                   6601: {
                   6602:     ND d;
                   6603:     NM m,m0;
                   6604:     NMV t;
                   6605:     int i,len;
                   6606:
                   6607:     if ( !p ) return 0;
                   6608:     m0 = 0;
                   6609:     len = p->len;
                   6610:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   6611:         NEXTNM(m0,m);
                   6612:         ndl_copy(DL(t),DL(m));
1.6       noro     6613:         CZ(m) = CZ(t);
1.1       noro     6614:     }
                   6615:     NEXT(m) = 0;
                   6616:     MKND(NV(p),m0,len,d);
                   6617:     SG(d) = SG(p);
1.28      noro     6618:     d->sig = p->sig;
1.1       noro     6619:     return d;
                   6620: }
                   6621:
                   6622: DP ndvtodp(int mod,NDV p)
                   6623: {
                   6624:     MP m,m0;
                   6625:   DP d;
                   6626:     NMV t;
                   6627:     int i,len;
                   6628:
                   6629:     if ( !p ) return 0;
                   6630:     m0 = 0;
                   6631:     len = p->len;
                   6632:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   6633:         NEXTMP(m0,m);
                   6634:       m->dl = ndltodl(nd_nvar,DL(t));
                   6635:       m->c = (Obj)ndctop(mod,t->c);
                   6636:     }
                   6637:     NEXT(m) = 0;
                   6638:   MKDP(nd_nvar,m0,d);
                   6639:     SG(d) = SG(p);
                   6640:     return d;
                   6641: }
                   6642:
1.16      noro     6643: DPM ndvtodpm(int mod,NDV p)
                   6644: {
                   6645:   DMM m,m0;
                   6646:   DPM d;
                   6647:   NMV t;
                   6648:   int i,len;
                   6649:
                   6650:   if ( !p ) return 0;
                   6651:   m0 = 0;
                   6652:   len = p->len;
                   6653:   for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   6654:     NEXTDMM(m0,m);
                   6655:     m->dl = ndltodl(nd_nvar,DL(t));
                   6656:     m->c = (Obj)ndctop(mod,t->c);
                   6657:     m->pos = MPOS(DL(t));
                   6658:   }
                   6659:   NEXT(m) = 0;
                   6660:   MKDPM(nd_nvar,m0,d);
                   6661:   SG(d) = SG(p);
                   6662:   return d;
                   6663: }
                   6664:
                   6665:
1.1       noro     6666: DP ndtodp(int mod,ND p)
                   6667: {
                   6668:     MP m,m0;
                   6669:   DP d;
                   6670:     NM t;
                   6671:     int i,len;
                   6672:
                   6673:     if ( !p ) return 0;
                   6674:     m0 = 0;
                   6675:     len = p->len;
                   6676:     for ( t = BDY(p); t; t = NEXT(t) ) {
                   6677:         NEXTMP(m0,m);
                   6678:       m->dl = ndltodl(nd_nvar,DL(t));
                   6679:       m->c = (Obj)ndctop(mod,t->c);
                   6680:     }
                   6681:     NEXT(m) = 0;
                   6682:   MKDP(nd_nvar,m0,d);
                   6683:     SG(d) = SG(p);
                   6684:     return d;
                   6685: }
                   6686:
                   6687: void ndv_print(NDV p)
                   6688: {
                   6689:     NMV m;
                   6690:     int i,len;
                   6691:
                   6692:     if ( !p ) printf("0\n");
                   6693:     else {
                   6694:         len = LEN(p);
                   6695:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6696:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     6697:             else printf("+%ld*",CM(m));
1.1       noro     6698:             ndl_print(DL(m));
                   6699:         }
                   6700:         printf("\n");
                   6701:     }
                   6702: }
                   6703:
                   6704: void ndv_print_q(NDV p)
                   6705: {
                   6706:     NMV m;
                   6707:     int i,len;
                   6708:
                   6709:     if ( !p ) printf("0\n");
                   6710:     else {
                   6711:         len = LEN(p);
                   6712:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6713:             printf("+");
1.6       noro     6714:             printexpr(CO,(Obj)CZ(m));
1.1       noro     6715:             printf("*");
                   6716:             ndl_print(DL(m));
                   6717:         }
                   6718:         printf("\n");
                   6719:     }
                   6720: }
                   6721:
                   6722: NODE ndv_reducebase(NODE x,int *perm)
                   6723: {
                   6724:     int len,i,j;
                   6725:     NDVI w;
                   6726:     NODE t,t0;
                   6727:
                   6728:     len = length(x);
                   6729:     w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
                   6730:     for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
                   6731:         w[i].p = BDY(t); w[i].i = perm[i];
                   6732:     }
                   6733:     for ( i = 0; i < len; i++ ) {
                   6734:         for ( j = 0; j < i; j++ ) {
                   6735:             if ( w[i].p && w[j].p ) {
                   6736:                 if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
                   6737:                 else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
                   6738:             }
                   6739:         }
                   6740:     }
                   6741:     for ( i = j = 0, t0 = 0; i < len; i++ ) {
                   6742:         if ( w[i].p ) {
                   6743:             NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
                   6744:             perm[j++] = w[i].i;
                   6745:         }
                   6746:     }
                   6747:     NEXT(t) = 0; x = t0;
                   6748:     return x;
                   6749: }
                   6750:
                   6751: /* XXX incomplete */
                   6752:
1.21      noro     6753: extern DMMstack dmm_stack;
                   6754: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16      noro     6755:
1.1       noro     6756: void nd_init_ord(struct order_spec *ord)
                   6757: {
                   6758:   nd_module = (ord->id >= 256);
                   6759:   if ( nd_module ) {
                   6760:     nd_dcomp = -1;
1.21      noro     6761:     nd_module_ordtype = ord->module_ordtype;
1.1       noro     6762:     nd_pot_nelim = ord->pot_nelim;
                   6763:     nd_poly_weight_len = ord->nv;
                   6764:     nd_poly_weight = ord->top_weight;
                   6765:     nd_module_rank = ord->module_rank;
                   6766:     nd_module_weight = ord->module_top_weight;
                   6767:   }
                   6768:   nd_matrix = 0;
                   6769:   nd_matrix_len = 0;
                   6770:     switch ( ord->id ) {
                   6771:         case 0:
                   6772:             switch ( ord->ord.simple ) {
                   6773:                 case 0:
                   6774:                     nd_dcomp = 1;
                   6775:                     nd_isrlex = 1;
                   6776:                     break;
                   6777:                 case 1:
                   6778:                     nd_dcomp = 1;
                   6779:                     nd_isrlex = 0;
                   6780:                     break;
                   6781:                 case 2:
                   6782:                     nd_dcomp = 0;
                   6783:                     nd_isrlex = 0;
                   6784:                     ndl_compare_function = ndl_lex_compare;
                   6785:                     break;
                   6786:                 case 11:
                   6787:                     /* XXX */
                   6788:                     nd_dcomp = 0;
                   6789:                     nd_isrlex = 1;
                   6790:                     ndl_compare_function = ndl_ww_lex_compare;
                   6791:                     break;
                   6792:                 default:
                   6793:                     error("nd_gr : unsupported order");
                   6794:             }
                   6795:             break;
                   6796:         case 1:
                   6797:             /* block order */
                   6798:             /* XXX */
                   6799:             nd_dcomp = -1;
                   6800:             nd_isrlex = 0;
                   6801:             ndl_compare_function = ndl_block_compare;
                   6802:             break;
                   6803:         case 2:
                   6804:             /* matrix order */
                   6805:             /* XXX */
                   6806:             nd_dcomp = -1;
                   6807:             nd_isrlex = 0;
                   6808:             nd_matrix_len = ord->ord.matrix.row;
                   6809:             nd_matrix = ord->ord.matrix.matrix;
                   6810:             ndl_compare_function = ndl_matrix_compare;
                   6811:             break;
                   6812:         case 3:
                   6813:             /* composite order */
                   6814:             nd_dcomp = -1;
                   6815:             nd_isrlex = 0;
                   6816:             nd_worb_len = ord->ord.composite.length;
                   6817:             nd_worb = ord->ord.composite.w_or_b;
                   6818:             ndl_compare_function = ndl_composite_compare;
                   6819:             break;
                   6820:
                   6821:         /* module order */
                   6822:         case 256:
                   6823:             switch ( ord->ord.simple ) {
                   6824:                 case 0:
1.21      noro     6825:                     nd_dcomp = 0;
1.1       noro     6826:                     nd_isrlex = 1;
1.21      noro     6827:                     ndl_compare_function = ndl_module_glex_compare;
1.1       noro     6828:                     break;
                   6829:                 case 1:
1.21      noro     6830:                     nd_dcomp = 0;
1.1       noro     6831:                     nd_isrlex = 0;
                   6832:                     ndl_compare_function = ndl_module_glex_compare;
                   6833:                     break;
                   6834:                 case 2:
1.21      noro     6835:                     nd_dcomp = 0;
1.1       noro     6836:                     nd_isrlex = 0;
1.21      noro     6837:                     ndl_compare_function = ndl_module_compare;
                   6838:                     ndl_base_compare_function = ndl_lex_compare;
1.1       noro     6839:                     break;
                   6840:                 default:
1.21      noro     6841:                     error("nd_init_ord : unsupported order");
1.1       noro     6842:             }
                   6843:             break;
                   6844:         case 257:
                   6845:             /* block order */
                   6846:             nd_isrlex = 0;
1.21      noro     6847:             ndl_compare_function = ndl_module_compare;
                   6848:             ndl_base_compare_function = ndl_block_compare;
1.1       noro     6849:             break;
                   6850:         case 258:
                   6851:             /* matrix order */
                   6852:             nd_isrlex = 0;
                   6853:             nd_matrix_len = ord->ord.matrix.row;
                   6854:             nd_matrix = ord->ord.matrix.matrix;
1.21      noro     6855:             ndl_compare_function = ndl_module_compare;
                   6856:             ndl_base_compare_function = ndl_matrix_compare;
1.1       noro     6857:             break;
                   6858:         case 259:
                   6859:             /* composite order */
                   6860:             nd_isrlex = 0;
                   6861:             nd_worb_len = ord->ord.composite.length;
                   6862:             nd_worb = ord->ord.composite.w_or_b;
1.21      noro     6863:             ndl_compare_function = ndl_module_compare;
                   6864:             ndl_base_compare_function = ndl_composite_compare;
                   6865:             break;
                   6866:         case 300:
                   6867:             /* schreyer order */
                   6868:             if ( ord->base->id != 256 )
                   6869:                error("nd_init_ord : unsupported base order");
                   6870:             ndl_compare_function = ndl_module_schreyer_compare;
                   6871:             dmm_stack = ord->dmmstack;
                   6872:             switch ( ord->base->ord.simple ) {
                   6873:                 case 0:
                   6874:                     nd_isrlex = 1;
                   6875:                     ndl_base_compare_function = ndl_glex_compare;
                   6876:                     dl_base_compare_function = cmpdl_revgradlex;
                   6877:                     break;
                   6878:                 case 1:
                   6879:                     nd_isrlex = 0;
                   6880:                     ndl_base_compare_function = ndl_glex_compare;
                   6881:                     dl_base_compare_function = cmpdl_gradlex;
                   6882:                     break;
                   6883:                 case 2:
                   6884:                     nd_isrlex = 0;
                   6885:                     ndl_base_compare_function = ndl_lex_compare;
                   6886:                     dl_base_compare_function = cmpdl_lex;
                   6887:                     break;
                   6888:                 default:
                   6889:                     error("nd_init_ord : unsupported order");
                   6890:             }
1.1       noro     6891:             break;
                   6892:     }
                   6893:     nd_ord = ord;
                   6894: }
                   6895:
                   6896: BlockMask nd_create_blockmask(struct order_spec *ord)
                   6897: {
                   6898:     int n,i,j,s,l;
                   6899:     UINT *t;
                   6900:     BlockMask bm;
                   6901:
                   6902:     /* we only create mask table for block order */
                   6903:     if ( ord->id != 1 && ord->id != 257 )
                   6904:         return 0;
                   6905:     n = ord->ord.block.length;
                   6906:     bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   6907:     bm->n = n;
                   6908:     bm->order_pair = ord->ord.block.order_pair;
                   6909:     bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
                   6910:     for ( i = 0, s = 0; i < n; i++ ) {
                   6911:         bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
                   6912:         for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   6913:         l = bm->order_pair[i].length;
                   6914:         for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   6915:     }
                   6916:     return bm;
                   6917: }
                   6918:
                   6919: EPOS nd_create_epos(struct order_spec *ord)
                   6920: {
                   6921:     int i,j,l,s,ord_l,ord_o;
                   6922:     EPOS epos;
                   6923:     struct order_pair *op;
                   6924:
                   6925:     epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   6926:     switch ( ord->id ) {
1.21      noro     6927:         case 0: case 256: case 300:
1.1       noro     6928:             if ( nd_isrlex ) {
                   6929:                 for ( i = 0; i < nd_nvar; i++ ) {
                   6930:                     epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   6931:                     epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   6932:                 }
                   6933:             } else {
                   6934:                 for ( i = 0; i < nd_nvar; i++ ) {
                   6935:                     epos[i].i = nd_exporigin + i/nd_epw;
                   6936:                     epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   6937:                 }
                   6938:             }
                   6939:             break;
                   6940:         case 1: case 257:
                   6941:             /* block order */
                   6942:             l = ord->ord.block.length;
                   6943:             op = ord->ord.block.order_pair;
                   6944:             for ( j = 0, s = 0; j < l; j++ ) {
                   6945:                 ord_o = op[j].order;
                   6946:                 ord_l = op[j].length;
                   6947:                 if ( !ord_o )
                   6948:                     for ( i = 0; i < ord_l; i++ ) {
                   6949:                         epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   6950:                         epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   6951:                     }
                   6952:                 else
                   6953:                     for ( i = 0; i < ord_l; i++ ) {
                   6954:                         epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   6955:                         epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   6956:                     }
                   6957:                 s += ord_l;
                   6958:             }
                   6959:             break;
                   6960:         case 2:
                   6961:             /* matrix order */
                   6962:         case 3:
                   6963:             /* composite order */
                   6964:         default:
                   6965:             for ( i = 0; i < nd_nvar; i++ ) {
                   6966:                 epos[i].i = nd_exporigin + i/nd_epw;
                   6967:                 epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   6968:             }
                   6969:             break;
                   6970:     }
                   6971:     return epos;
                   6972: }
                   6973:
                   6974: /* external interface */
                   6975:
                   6976: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
                   6977: {
                   6978:     NODE t,in0,in;
                   6979:     ND ndf,nf;
                   6980:     NDV ndvf;
                   6981:     VL vv,tv;
                   6982:     int stat,nvar,max,mrank;
                   6983:     union oNDC dn;
                   6984:     Q cont;
                   6985:     P pp;
                   6986:     LIST ppl;
                   6987:
                   6988:     if ( !f ) {
                   6989:         *rp = 0;
                   6990:         return;
                   6991:     }
                   6992:     pltovl(v,&vv);
                   6993:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   6994:
                   6995:     /* max=65536 implies nd_bpe=32 */
                   6996:     max = 65536;
                   6997:
                   6998:   nd_module = 0;
                   6999:   /* nd_module will be set if ord is a module ordering */
                   7000:     nd_init_ord(ord);
                   7001:     nd_setup_parameters(nvar,max);
                   7002:     if ( nd_module && OID(f) != O_LIST )
                   7003:         error("nd_nf_p : the first argument must be a list");
                   7004:   if ( nd_module ) mrank = length(BDY((LIST)f));
                   7005:     /* conversion to ndv */
                   7006:     for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   7007:         NEXTNODE(in0,in);
                   7008:         if ( nd_module ) {
                   7009:           if ( !BDY(t) || OID(BDY(t)) != O_LIST
                   7010:                || length(BDY((LIST)BDY(t))) != mrank )
                   7011:               error("nd_nf_p : inconsistent basis element");
                   7012:           if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
                   7013:           else ppl = (LIST)BDY(t);
                   7014:           BDY(in) = (pointer)pltondv(CO,vv,ppl);
                   7015:         } else {
                   7016:           if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
                   7017:           else pp = (P)BDY(t);
                   7018:           BDY(in) = (pointer)ptondv(CO,vv,pp);
                   7019:         }
                   7020:         if ( m ) ndv_mod(m,(NDV)BDY(in));
                   7021:     }
                   7022:     if ( in0 ) NEXT(in) = 0;
                   7023:
                   7024:     if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
                   7025:     else ndvf = ptondv(CO,vv,(P)f);
                   7026:     if ( m ) ndv_mod(m,ndvf);
                   7027:     ndf = (pointer)ndvtond(m,ndvf);
                   7028:
                   7029:     /* dont sort, dont removecont */
1.24      noro     7030:     ndv_setup(m,0,in0,1,1,0);
1.1       noro     7031:     nd_scale=2;
1.6       noro     7032:     stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1       noro     7033:     if ( !stat )
                   7034:         error("nd_nf_p : exponent too large");
                   7035:     if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
                   7036:     else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
                   7037: }
                   7038:
                   7039: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   7040: {
                   7041:     NM m;
                   7042:     UINT *t,*s;
                   7043:     int i;
                   7044:
                   7045:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   7046:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7047:         t = DL(m);
                   7048:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   7049:         r[i] = CM(m);
                   7050:     }
                   7051:     for ( i = 0; !r[i]; i++ );
                   7052:     return i;
                   7053: }
                   7054:
                   7055: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
                   7056: {
                   7057:     NM m;
                   7058:     UINT *t,*s;
                   7059:     int i;
                   7060:
                   7061:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   7062:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7063:         t = DL(m);
                   7064:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     7065:         r[i] = CZ(m);
1.1       noro     7066:     }
                   7067:     for ( i = 0; !r[i]; i++ );
                   7068:     return i;
                   7069: }
                   7070:
                   7071: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
                   7072: {
                   7073:     NM m;
                   7074:     UINT *t,*s;
                   7075:     int i;
                   7076:
                   7077:     for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
                   7078:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7079:         t = DL(m);
                   7080:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   7081:         mpz_set(r[i],BDY(CZ(m)));
                   7082:     }
                   7083:     for ( i = 0; !mpz_sgn(r[i]); i++ );
                   7084:     return i;
                   7085: }
                   7086:
                   7087: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
                   7088: {
                   7089:     NM m;
                   7090:     unsigned long *v;
                   7091:     int i,j,h,size;
                   7092:   UINT *s,*t;
                   7093:
                   7094:   size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
                   7095:     v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
                   7096:     bzero(v,size);
                   7097:     for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
                   7098:     t = DL(m);
                   7099:     h = ndl_hash_value(t);
                   7100:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   7101:       v[i/BLEN] |= 1L <<(i%BLEN);
                   7102:     }
                   7103:     return v;
                   7104: }
                   7105:
                   7106: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
                   7107: {
                   7108:     NMV mr;
                   7109:     UINT *d,*t,*s;
                   7110:     int i,j,len,h,head;
                   7111:
                   7112:     d = DL(m);
                   7113:     len = LEN(p);
                   7114:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   7115:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   7116:         ndl_add(d,DL(mr),t);
                   7117:     h = ndl_hash_value(t);
                   7118:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   7119:     if ( j == 0 ) head = i;
                   7120:       v[i/BLEN] |= 1L <<(i%BLEN);
                   7121:     }
                   7122:     return head;
                   7123: }
                   7124:
                   7125: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   7126: {
                   7127:     NM m;
                   7128:     NMV mr;
                   7129:     UINT *d,*t,*s;
                   7130:     NDV p;
                   7131:     int i,j,len;
                   7132:     Z *r;
                   7133:
                   7134:     m = pair->mul;
                   7135:     d = DL(m);
                   7136:     p = nd_ps[pair->index];
                   7137:     len = LEN(p);
                   7138:     r = (Z *)CALLOC(n,sizeof(Q));
                   7139:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   7140:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   7141:         ndl_add(d,DL(mr),t);
                   7142:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     7143:         r[i] = CZ(mr);
1.1       noro     7144:     }
                   7145:     return r;
                   7146: }
                   7147:
1.11      noro     7148: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1       noro     7149: {
                   7150:     NM m;
                   7151:     NMV mr;
1.11      noro     7152:     UINT *d,*t,*s,*u;
1.1       noro     7153:     NDV p;
                   7154:     unsigned char *ivc;
                   7155:     unsigned short *ivs;
                   7156:     UINT *v,*ivi,*s0v;
1.11      noro     7157:     int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1       noro     7158:     IndArray r;
                   7159:
                   7160:     m = pair->mul;
                   7161:     d = DL(m);
                   7162:     if ( trace )
                   7163:       p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
                   7164:     else
                   7165:       p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
                   7166:
                   7167:     len = LEN(p);
                   7168:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   7169:     v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11      noro     7170:     for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
                   7171:       ndl_add(d,DL(mr),t);
                   7172:       st = prev;
                   7173:       ed = n;
                   7174:       while ( ed > st ) {
                   7175:         md = (st+ed)/2;
                   7176:         u = s0+md*nd_wpd;
                   7177:         c = DL_COMPARE(u,t);
                   7178:         if ( c == 0 ) break;
                   7179:         else if ( c > 0 ) st = md;
                   7180:         else ed = md;
                   7181:       }
                   7182:       prev = v[j] = md;
1.1       noro     7183:     }
                   7184:     r = (IndArray)MALLOC(sizeof(struct oIndArray));
                   7185:     r->head = v[0];
                   7186:     diff = 0;
                   7187:     for ( i = 1; i < len; i++ ) {
                   7188:         cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   7189:     }
                   7190:     if ( diff < 256 ) {
                   7191:         r->width = 1;
                   7192:         ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   7193:         r->index.c = ivc;
                   7194:         for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   7195:     } else if ( diff < 65536 ) {
                   7196:         r->width = 2;
                   7197:         ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   7198:         r->index.s = ivs;
                   7199:         for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   7200:     } else {
                   7201:         r->width = 4;
                   7202:         ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   7203:         r->index.i = ivi;
                   7204:         for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
                   7205:     }
                   7206:     return r;
                   7207: }
                   7208:
                   7209: int compress_array(Z *svect,Z *cvect,int n)
                   7210: {
                   7211:     int i,j;
                   7212:
                   7213:     for ( i = j = 0; i < n; i++ )
                   7214:         if ( svect[i] ) cvect[j++] = svect[i];
                   7215:     return j;
                   7216: }
                   7217:
                   7218: void expand_array(Z *svect,Z *cvect,int n)
                   7219: {
                   7220:     int i,j;
                   7221:
                   7222:     for ( i = j = 0; j < n;  i++  )
                   7223:         if ( svect[i] ) svect[i] = cvect[j++];
                   7224: }
                   7225:
1.8       noro     7226: #if 0
1.1       noro     7227: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7228: {
                   7229:     int i,j,k,len,pos,prev,nz;
                   7230:     Z cs,mcs,c1,c2,cr,gcd,t;
                   7231:     IndArray ivect;
                   7232:     unsigned char *ivc;
                   7233:     unsigned short *ivs;
                   7234:     unsigned int *ivi;
                   7235:     NDV redv;
                   7236:     NMV mr;
                   7237:     NODE rp;
                   7238:     int maxrs;
                   7239:     double hmag;
                   7240:     Z *cvect;
1.3       noro     7241:     int l;
1.1       noro     7242:
                   7243:     maxrs = 0;
                   7244:     for ( i = 0; i < col && !svect[i]; i++ );
                   7245:     if ( i == col ) return maxrs;
                   7246:     hmag = p_mag((P)svect[i])*nd_scale;
                   7247:     cvect = (Z *)MALLOC(col*sizeof(Q));
                   7248:     for ( i = 0; i < nred; i++ ) {
                   7249:         ivect = imat[i];
                   7250:         k = ivect->head;
                   7251:         if ( svect[k] ) {
                   7252:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7253:             redv = nd_demand?ndv_load(rp0[i]->index)
                   7254:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   7255:             len = LEN(redv); mr = BDY(redv);
1.6       noro     7256:             igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1       noro     7257:             chsgnz(cs,&mcs);
                   7258:             if ( !UNIQ(cr) ) {
                   7259:                 for ( j = 0; j < col; j++ ) {
                   7260:                     mulz(svect[j],cr,&c1); svect[j] = c1;
                   7261:                 }
                   7262:             }
                   7263:             svect[k] = 0; prev = k;
                   7264:             switch ( ivect->width ) {
                   7265:                 case 1:
                   7266:                     ivc = ivect->index.c;
                   7267:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7268:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     7269:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     7270:                     }
                   7271:                     break;
                   7272:                 case 2:
                   7273:                     ivs = ivect->index.s;
                   7274:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7275:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     7276:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     7277:                     }
                   7278:                     break;
                   7279:                 case 4:
                   7280:                     ivi = ivect->index.i;
                   7281:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7282:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     7283:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     7284:                     }
                   7285:                     break;
                   7286:             }
                   7287:             for ( j = k+1; j < col && !svect[j]; j++ );
                   7288:             if ( j == col ) break;
                   7289:             if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
                   7290:                 nz = compress_array(svect,cvect,col);
                   7291:                 removecont_array((P *)cvect,nz,1);
                   7292:                 expand_array(svect,cvect,nz);
                   7293:                 hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   7294:             }
                   7295:         }
                   7296:     }
                   7297:     nz = compress_array(svect,cvect,col);
                   7298:     removecont_array((P *)cvect,nz,1);
                   7299:     expand_array(svect,cvect,nz);
                   7300:     if ( DP_Print ) {
                   7301:         fprintf(asir_out,"-"); fflush(asir_out);
                   7302:     }
                   7303:     return maxrs;
                   7304: }
1.4       noro     7305: #else
1.9       noro     7306:
1.4       noro     7307: /* direct mpz version */
                   7308: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7309: {
                   7310:     int i,j,k,len,pos,prev;
                   7311:     mpz_t cs,cr,gcd;
                   7312:     IndArray ivect;
                   7313:     unsigned char *ivc;
                   7314:     unsigned short *ivs;
                   7315:     unsigned int *ivi;
                   7316:     NDV redv;
                   7317:     NMV mr;
                   7318:     NODE rp;
                   7319:     int maxrs;
                   7320:     double hmag;
                   7321:     int l;
1.13      noro     7322:     static mpz_t *svect;
                   7323:     static int svect_len=0;
1.4       noro     7324:
                   7325:     maxrs = 0;
                   7326:     for ( i = 0; i < col && !svect0[i]; i++ );
                   7327:     if ( i == col ) return maxrs;
                   7328:     hmag = p_mag((P)svect0[i])*nd_scale;
1.13      noro     7329:     if ( col > svect_len ) {
                   7330:       svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   7331:       svect_len = col;
                   7332:     }
1.4       noro     7333:     for ( i = 0; i < col; i++ ) {
                   7334:       mpz_init(svect[i]);
                   7335:       if ( svect0[i] )
                   7336:         mpz_set(svect[i],BDY(svect0[i]));
                   7337:       else
                   7338:         mpz_set_ui(svect[i],0);
                   7339:     }
                   7340:     mpz_init(gcd); mpz_init(cs); mpz_init(cr);
                   7341:     for ( i = 0; i < nred; i++ ) {
                   7342:         ivect = imat[i];
                   7343:         k = ivect->head;
                   7344:         if ( mpz_sgn(svect[k]) ) {
                   7345:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7346:             redv = nd_demand?ndv_load(rp0[i]->index)
                   7347:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   7348:             len = LEN(redv); mr = BDY(redv);
1.6       noro     7349:             mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4       noro     7350:             mpz_div(cs,svect[k],gcd);
1.6       noro     7351:             mpz_div(cr,BDY(CZ(mr)),gcd);
1.4       noro     7352:             mpz_neg(cs,cs);
1.9       noro     7353:             if ( MUNIMPZ(cr) )
                   7354:               for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
                   7355:             else if ( !UNIMPZ(cr) )
                   7356:               for ( j = 0; j < col; j++ ) {
                   7357:                 if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
                   7358:               }
1.4       noro     7359:             mpz_set_ui(svect[k],0);
                   7360:             prev = k;
                   7361:             switch ( ivect->width ) {
                   7362:                 case 1:
                   7363:                     ivc = ivect->index.c;
                   7364:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7365:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     7366:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     7367:                     }
                   7368:                     break;
                   7369:                 case 2:
                   7370:                     ivs = ivect->index.s;
                   7371:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7372:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     7373:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     7374:                     }
                   7375:                     break;
                   7376:                 case 4:
                   7377:                     ivi = ivect->index.i;
                   7378:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7379:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     7380:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     7381:                     }
                   7382:                     break;
                   7383:             }
                   7384:             for ( j = k+1; j < col && !svect[j]; j++ );
                   7385:             if ( j == col ) break;
                   7386:             if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
                   7387:                 mpz_removecont_array(svect,col);
                   7388:                 hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
                   7389:             }
                   7390:         }
                   7391:     }
                   7392:     mpz_removecont_array(svect,col);
                   7393:     if ( DP_Print ) {
                   7394:         fprintf(asir_out,"-"); fflush(asir_out);
                   7395:     }
                   7396:     for ( i = 0; i < col; i++ )
                   7397:       if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
                   7398:       else svect0[i] = 0;
                   7399:     return maxrs;
                   7400: }
                   7401: #endif
1.1       noro     7402:
                   7403: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7404: {
                   7405:     int i,j,k,len,pos,prev;
                   7406:     UINT c,c1,c2,c3,up,lo,dmy;
                   7407:     IndArray ivect;
                   7408:     unsigned char *ivc;
                   7409:     unsigned short *ivs;
                   7410:     unsigned int *ivi;
                   7411:     NDV redv;
                   7412:     NMV mr;
                   7413:     NODE rp;
                   7414:     int maxrs;
                   7415:
                   7416:     maxrs = 0;
                   7417:     for ( i = 0; i < nred; i++ ) {
                   7418:         ivect = imat[i];
                   7419:         k = ivect->head; svect[k] %= m;
                   7420:         if ( (c = svect[k]) != 0 ) {
                   7421:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7422:             c = m-c; redv = nd_ps[rp0[i]->index];
                   7423:             len = LEN(redv); mr = BDY(redv);
                   7424:             svect[k] = 0; prev = k;
                   7425:             switch ( ivect->width ) {
                   7426:                 case 1:
                   7427:                     ivc = ivect->index.c;
                   7428:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7429:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
                   7430:             if ( c1 ) {
                   7431:               c2 = svect[pos];
                   7432:                           DMA(c1,c,c2,up,lo);
                   7433:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   7434:                           } else svect[pos] = lo;
                   7435:             }
                   7436:                     }
                   7437:                     break;
                   7438:                 case 2:
                   7439:                     ivs = ivect->index.s;
                   7440:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7441:                         pos = prev+ivs[j]; c1 = CM(mr);
                   7442:                         prev = pos;
                   7443:             if ( c1 ) {
                   7444:               c2 = svect[pos];
                   7445:                           DMA(c1,c,c2,up,lo);
                   7446:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   7447:                           } else svect[pos] = lo;
                   7448:             }
                   7449:                     }
                   7450:                     break;
                   7451:                 case 4:
                   7452:                     ivi = ivect->index.i;
                   7453:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7454:                         pos = prev+ivi[j]; c1 = CM(mr);
                   7455:                         prev = pos;
                   7456:             if ( c1 ) {
                   7457:               c2 = svect[pos];
                   7458:                           DMA(c1,c,c2,up,lo);
                   7459:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   7460:                           } else svect[pos] = lo;
                   7461:             }
                   7462:                     }
                   7463:                     break;
                   7464:             }
                   7465:         }
                   7466:     }
                   7467:     for ( i = 0; i < col; i++ )
                   7468:         if ( svect[i] >= (UINT)m ) svect[i] %= m;
                   7469:     return maxrs;
                   7470: }
                   7471:
                   7472: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7473: {
                   7474:     int i,j,k,len,pos,prev;
                   7475:     UINT c,c1,c2,c3,up,lo,dmy;
                   7476:     IndArray ivect;
                   7477:     unsigned char *ivc;
                   7478:     unsigned short *ivs;
                   7479:     unsigned int *ivi;
                   7480:     NDV redv;
                   7481:     NMV mr;
                   7482:     NODE rp;
                   7483:     int maxrs;
                   7484:
                   7485:     maxrs = 0;
                   7486:     for ( i = 0; i < nred; i++ ) {
                   7487:         ivect = imat[i];
                   7488:         k = ivect->head;
                   7489:         if ( (c = svect[k]) != 0 ) {
                   7490:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7491:             c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
                   7492:             len = LEN(redv); mr = BDY(redv);
                   7493:             svect[k] = 0; prev = k;
                   7494:             switch ( ivect->width ) {
                   7495:                 case 1:
                   7496:                     ivc = ivect->index.c;
                   7497:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7498:                         pos = prev+ivc[j]; prev = pos;
                   7499:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   7500:                     }
                   7501:                     break;
                   7502:                 case 2:
                   7503:                     ivs = ivect->index.s;
                   7504:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7505:                         pos = prev+ivs[j]; prev = pos;
                   7506:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   7507:                     }
                   7508:                     break;
                   7509:                 case 4:
                   7510:                     ivi = ivect->index.i;
                   7511:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7512:                         pos = prev+ivi[j]; prev = pos;
                   7513:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   7514:                     }
                   7515:                     break;
                   7516:             }
                   7517:         }
                   7518:     }
                   7519:     return maxrs;
                   7520: }
                   7521:
                   7522: ND nd_add_lf(ND p1,ND p2)
                   7523: {
                   7524:     int n,c,can;
                   7525:     ND r;
                   7526:     NM m1,m2,mr0,mr,s;
                   7527:     Z t;
                   7528:
                   7529:     if ( !p1 ) return p2;
                   7530:     else if ( !p2 ) return p1;
                   7531:     else {
                   7532:         can = 0;
                   7533:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   7534:             c = DL_COMPARE(DL(m1),DL(m2));
                   7535:             switch ( c ) {
                   7536:                 case 0:
                   7537:                     addlf(CZ(m1),CZ(m2),&t);
                   7538:                     s = m1; m1 = NEXT(m1);
                   7539:                     if ( t ) {
                   7540:                         can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
                   7541:                     } else {
                   7542:                         can += 2; FREENM(s);
                   7543:                     }
                   7544:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   7545:                     break;
                   7546:                 case 1:
                   7547:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   7548:                     break;
                   7549:                 case -1:
                   7550:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   7551:                     break;
                   7552:             }
                   7553:         }
                   7554:         if ( !mr0 )
                   7555:             if ( m1 ) mr0 = m1;
                   7556:             else if ( m2 ) mr0 = m2;
                   7557:             else return 0;
                   7558:         else if ( m1 ) NEXT(mr) = m1;
                   7559:         else if ( m2 ) NEXT(mr) = m2;
                   7560:         else NEXT(mr) = 0;
                   7561:         BDY(p1) = mr0;
                   7562:         SG(p1) = MAX(SG(p1),SG(p2));
                   7563:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   7564:         FREEND(p2);
                   7565:         return p1;
                   7566:     }
                   7567: }
                   7568:
                   7569: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7570: {
                   7571:     int i,j,k,len,pos,prev;
                   7572:     mpz_t c,mc,c1;
                   7573:     IndArray ivect;
                   7574:     unsigned char *ivc;
                   7575:     unsigned short *ivs;
                   7576:     unsigned int *ivi;
                   7577:     NDV redv;
                   7578:     NMV mr;
                   7579:     NODE rp;
                   7580:     int maxrs;
                   7581:
                   7582:     maxrs = 0;
                   7583:     lf_lazy = 1;
                   7584:     for ( i = 0; i < nred; i++ ) {
                   7585:         ivect = imat[i];
                   7586:         k = ivect->head;
                   7587:         mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
                   7588:         if ( mpz_sgn(svect[k]) ) {
                   7589:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7590:             mpz_neg(svect[k],svect[k]);
                   7591:             redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
                   7592:             len = LEN(redv); mr = BDY(redv);
                   7593:             prev = k;
                   7594:             switch ( ivect->width ) {
                   7595:                 case 1:
                   7596:                     ivc = ivect->index.c;
                   7597:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7598:                         pos = prev+ivc[j]; prev = pos;
                   7599:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   7600:                     }
                   7601:                     break;
                   7602:                 case 2:
                   7603:                     ivs = ivect->index.s;
                   7604:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7605:                         pos = prev+ivs[j]; prev = pos;
                   7606:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   7607:                     }
                   7608:                     break;
                   7609:                 case 4:
                   7610:                     ivi = ivect->index.i;
                   7611:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7612:                         pos = prev+ivi[j]; prev = pos;
                   7613:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   7614:                     }
                   7615:                     break;
                   7616:             }
                   7617:             mpz_set_ui(svect[k],0);
                   7618:         }
                   7619:     }
                   7620:     lf_lazy=0;
                   7621:     for ( i = 0; i < col; i++ ) {
                   7622:         mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
                   7623:     }
                   7624:     return maxrs;
                   7625: }
                   7626:
                   7627: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
                   7628: {
                   7629:     int i,j,k,l,rank,s;
                   7630:     mpz_t a,a1,inv;
                   7631:     mpz_t *t,*pivot,*pk;
                   7632:     mpz_t **mat;
                   7633:     struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
                   7634:     int size,size1;
                   7635:
                   7636:     mpz_init(inv);
                   7637:     mpz_init(a);
                   7638:     mat = (mpz_t **)mat0;
                   7639:         size = 0;
                   7640:     for ( rank = 0, j = 0; j < col; j++ ) {
                   7641:         for ( i = rank; i < row; i++ ) {
                   7642:             mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
                   7643:         }
                   7644:         for ( i = rank; i < row; i++ )
                   7645:             if ( mpz_sgn(mat[i][j]) )
                   7646:                 break;
                   7647:         if ( i == row ) {
                   7648:             colstat[j] = 0;
                   7649:             continue;
                   7650:         } else
                   7651:             colstat[j] = 1;
                   7652:         if ( i != rank ) {
                   7653:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   7654:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   7655:         }
                   7656:         pivot = mat[rank];
                   7657:         s = sugar[rank];
                   7658:         mpz_invert(inv,pivot[j],BDY(current_mod_lf));
                   7659:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   7660:             if ( mpz_sgn(*pk) ) {
                   7661:                 mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
                   7662:             }
                   7663:         for ( i = rank+1; i < row; i++ ) {
                   7664:             t = mat[i];
                   7665:             if ( mpz_sgn(t[j]) ) {
                   7666:                 sugar[i] = MAX(sugar[i],s);
                   7667:                 mpz_neg(a,t[j]);
                   7668:                 red_by_vect_lf(t+j,pivot+j,a,col-j);
                   7669:             }
                   7670:         }
                   7671:         rank++;
                   7672:     }
                   7673:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   7674:         if ( colstat[j] ) {
                   7675:             pivot = mat[l];
                   7676:             s = sugar[l];
                   7677:             for ( k = j; k < col; k++ )
                   7678:               mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
                   7679:             for ( i = 0; i < l; i++ ) {
                   7680:                 t = mat[i];
                   7681:                 if ( mpz_sgn(t[j]) ) {
                   7682:                     sugar[i] = MAX(sugar[i],s);
                   7683:                     mpz_neg(a,t[j]);
                   7684:                     red_by_vect_lf(t+j,pivot+j,a,col-j);
                   7685:                 }
                   7686:             }
                   7687:             l--;
                   7688:         }
                   7689:     for ( j = 0, l = 0; l < rank; j++ )
                   7690:         if ( colstat[j] ) {
                   7691:             t = mat[l];
                   7692:             for ( k = j; k < col; k++ ) {
                   7693:                 mpz_mod(t[k],t[k],BDY(current_mod_lf));
                   7694:             }
                   7695:             l++;
                   7696:         }
                   7697:     return rank;
                   7698: }
                   7699:
                   7700:
                   7701: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   7702: {
                   7703:     int j,k,len;
                   7704:     UINT *p;
                   7705:     UINT c;
                   7706:     NDV r;
                   7707:     NMV mr0,mr;
                   7708:
                   7709:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   7710:     if ( !len ) return 0;
                   7711:     else {
                   7712:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   7713: #if 0
                   7714:         ndv_alloc += nmv_adv*len;
                   7715: #endif
                   7716:         mr = mr0;
                   7717:         p = s0vect;
                   7718:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   7719:             if ( !rhead[j] ) {
                   7720:                 if ( (c = vect[k++]) != 0 ) {
                   7721:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   7722:                 }
                   7723:             }
                   7724:         MKNDV(nd_nvar,mr0,len,r);
                   7725:         return r;
                   7726:     }
                   7727: }
                   7728:
                   7729: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
                   7730: {
                   7731:     int j,k,len;
                   7732:     UINT *p;
                   7733:     NDV r;
                   7734:     NMV mr0,mr;
                   7735:
                   7736:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
                   7737:     if ( !len ) return 0;
                   7738:     else {
                   7739:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   7740:         mr = mr0;
                   7741:         p = s0vect;
                   7742:         for ( j = 0; j < col; j++, p += nd_wpd )
                   7743:       if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
                   7744:             ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
                   7745:           }
                   7746:         MKNDV(nd_nvar,mr0,len,r);
                   7747:         return r;
                   7748:     }
                   7749: }
                   7750:
                   7751: /* for preprocessed vector */
                   7752:
                   7753: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   7754: {
1.6       noro     7755:   int j,k,len;
                   7756:   UINT *p;
                   7757:   Z c;
                   7758:   NDV r;
                   7759:   NMV mr0,mr;
1.1       noro     7760:
1.6       noro     7761:   for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   7762:   if ( !len ) return 0;
                   7763:   else {
                   7764:     mr0 = (NMV)MALLOC(nmv_adv*len);
1.1       noro     7765: #if 0
1.6       noro     7766:     ndv_alloc += nmv_adv*len;
1.1       noro     7767: #endif
1.6       noro     7768:     mr = mr0;
                   7769:     p = s0vect;
                   7770:     for ( j = k = 0; j < col; j++, p += nd_wpd ) {
                   7771:       if ( !rhead[j] ) {
                   7772:         if ( (c = vect[k++]) != 0 ) {
                   7773:           if ( !INT(c) )
                   7774:             error("vect_to_ndv_q : components must be integers");
                   7775:             ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
                   7776:         }
                   7777:       }
1.1       noro     7778:     }
1.6       noro     7779:     MKNDV(nd_nvar,mr0,len,r);
                   7780:     return r;
                   7781:   }
1.1       noro     7782: }
                   7783:
                   7784: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   7785: {
                   7786:     int j,k,len;
                   7787:     UINT *p;
                   7788:     mpz_t c;
                   7789:     NDV r;
                   7790:     NMV mr0,mr;
                   7791:
                   7792:     for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
                   7793:     if ( !len ) return 0;
                   7794:     else {
                   7795:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   7796: #if 0
                   7797:         ndv_alloc += nmv_adv*len;
                   7798: #endif
                   7799:         mr = mr0;
                   7800:         p = s0vect;
                   7801:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   7802:             if ( !rhead[j] ) {
                   7803:                 c[0] = vect[k++][0];
                   7804:                 if ( mpz_sgn(c) ) {
                   7805:                     ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
                   7806:                 }
                   7807:             }
                   7808:         MKNDV(nd_nvar,mr0,len,r);
                   7809:         return r;
                   7810:     }
                   7811: }
                   7812:
                   7813: /* for plain vector */
                   7814:
                   7815: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
                   7816: {
                   7817:     int j,k,len;
                   7818:     UINT *p;
                   7819:     Z c;
                   7820:     NDV r;
                   7821:     NMV mr0,mr;
                   7822:
                   7823:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   7824:     if ( !len ) return 0;
                   7825:     else {
                   7826:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   7827: #if 0
                   7828:         ndv_alloc += nmv_adv*len;
                   7829: #endif
                   7830:         mr = mr0;
                   7831:         p = s0vect;
                   7832:         for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   7833:             if ( (c = vect[k]) != 0 ) {
                   7834:                 if ( !INT(c) )
1.6       noro     7835:                     error("plain_vect_to_ndv_q : components must be integers");
                   7836:                 ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1       noro     7837:             }
                   7838:         MKNDV(nd_nvar,mr0,len,r);
                   7839:         return r;
                   7840:     }
                   7841: }
                   7842:
                   7843: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
                   7844: {
                   7845:     ND_pairs t;
                   7846:     NODE sp0,sp;
                   7847:     int stat;
                   7848:     ND spol;
                   7849:
                   7850:     for ( t = l; t; t = NEXT(t) ) {
                   7851:         stat = nd_sp(m,trace,t,&spol);
                   7852:         if ( !stat ) return 0;
                   7853:         if ( spol ) {
                   7854:             add_pbucket_symbolic(bucket,spol);
                   7855:         }
                   7856:     }
                   7857:     return 1;
                   7858: }
                   7859:
                   7860: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   7861: {
                   7862:     NODE rp0,rp;
                   7863:     NM mul,head,s0,s;
                   7864:     int index,col,i,sugar;
                   7865:     RHist h;
                   7866:     UINT *s0v,*p;
                   7867:     NM_ind_pair pair;
                   7868:     ND red;
                   7869:     NDV *ps;
                   7870:
                   7871:     s0 = 0; rp0 = 0; col = 0;
                   7872:   if ( nd_demand )
                   7873:       ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   7874:   else
                   7875:       ps = trace?nd_ps_trace:nd_ps;
                   7876:     while ( 1 ) {
                   7877:         head = remove_head_pbucket_symbolic(bucket);
                   7878:         if ( !head ) break;
                   7879:         if ( !s0 ) s0 = head;
                   7880:         else NEXT(s) = head;
                   7881:         s = head;
                   7882:         index = ndl_find_reducer(DL(head));
                   7883:         if ( index >= 0 ) {
                   7884:             h = nd_psh[index];
                   7885:             NEWNM(mul);
                   7886:             ndl_sub(DL(head),DL(h),DL(mul));
                   7887:             if ( ndl_check_bound2(index,DL(mul)) )
                   7888:                 return 0;
                   7889:             sugar = TD(DL(mul))+SG(ps[index]);
1.28      noro     7890:             MKNM_ind_pair(pair,mul,index,sugar,0);
1.1       noro     7891:             red = ndv_mul_nm_symbolic(mul,ps[index]);
                   7892:             add_pbucket_symbolic(bucket,nd_remove_head(red));
                   7893:             NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   7894:         }
                   7895:         col++;
                   7896:     }
                   7897:     if ( rp0 ) NEXT(rp) = 0;
                   7898:     NEXT(s) = 0;
                   7899:     s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   7900:     for ( i = 0, p = s0v, s = s0; i < col;
                   7901:         i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   7902:     *s0vect = s0v;
                   7903:     *r = rp0;
                   7904:     return col;
                   7905: }
                   7906:
                   7907: void print_ndp(ND_pairs l)
                   7908: {
                   7909:   ND_pairs t;
                   7910:
                   7911:   for ( t = l; t; t = NEXT(t) )
                   7912:     printf("[%d,%d] ",t->i1,t->i2);
                   7913:   printf("\n");
                   7914: }
                   7915:
                   7916: NODE nd_f4(int m,int checkonly,int **indp)
                   7917: {
                   7918:     int i,nh,stat,index,f4red;
                   7919:     NODE r,g,tn0,tn,node;
                   7920:     ND_pairs d,l,t,ll0,ll,lh;
                   7921:   LIST l0,l1;
                   7922:     ND spol,red;
                   7923:     NDV nf,redv;
                   7924:     NM s0,s;
                   7925:     NODE rp0,srp0,nflist,nzlist,nzlist_t;
                   7926:     int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
                   7927:     UINT c;
                   7928:     UINT **spmat;
                   7929:     UINT *s0vect,*svect,*p,*v;
                   7930:     int *colstat;
                   7931:     IndArray *imat;
                   7932:     int *rhead;
                   7933:     int spcol,sprow;
                   7934:     int sugar,sugarh;
                   7935:     PGeoBucket bucket;
                   7936:     struct oEGT eg0,eg1,eg_f4;
                   7937:     Z i1,i2,sugarq;
1.12      noro     7938:
                   7939:     init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1       noro     7940: #if 0
                   7941:     ndv_alloc = 0;
                   7942: #endif
1.11      noro     7943:     Nf4_red=0;
1.1       noro     7944:     g = 0; d = 0;
                   7945:     for ( i = 0; i < nd_psn; i++ ) {
                   7946:         d = update_pairs(d,g,i,0);
                   7947:         g = update_base(g,i);
                   7948:     }
                   7949:   nzlist = 0;
                   7950:     nzlist_t = nd_nzlist;
                   7951:     f4red = 1;
                   7952:     nd_last_nonzero = 0;
                   7953:     while ( d ) {
                   7954:         get_eg(&eg0);
                   7955:         l = nd_minsugarp(d,&d);
                   7956:         sugar = nd_sugarweight?l->sugar2:SG(l);
                   7957:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   7958:         if ( nzlist_t ) {
                   7959:             node = BDY((LIST)BDY(nzlist_t));
1.6       noro     7960:             sugarh = ZTOS((Q)ARG0(node));
1.1       noro     7961:             tn = BDY((LIST)ARG1(node));
                   7962:             if ( !tn ) {
                   7963:               nzlist_t = NEXT(nzlist_t);
                   7964:               continue;
                   7965:             }
                   7966:             /* tn = [[i1,i2],...] */
                   7967:             lh = nd_ipairtospair(tn);
                   7968:         }
                   7969:         bucket = create_pbucket();
                   7970:         stat = nd_sp_f4(m,0,l,bucket);
                   7971:         if ( !stat ) {
                   7972:             for ( t = l; NEXT(t); t = NEXT(t) );
                   7973:             NEXT(t) = d; d = l;
                   7974:             d = nd_reconstruct(0,d);
                   7975:             continue;
                   7976:         }
                   7977:         if ( bucket->m < 0 ) continue;
                   7978:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   7979:         if ( !col ) {
                   7980:             for ( t = l; NEXT(t); t = NEXT(t) );
                   7981:             NEXT(t) = d; d = l;
                   7982:             d = nd_reconstruct(0,d);
                   7983:             continue;
                   7984:         }
1.12      noro     7985:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1       noro     7986:         if ( DP_Print )
1.6       noro     7987:             fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5       noro     7988:                 sugar,eg_f4.exectime);
1.1       noro     7989:         nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
                   7990:         if ( checkonly && nflist ) return 0;
                   7991:         /* adding new bases */
                   7992:         if ( nflist ) nd_last_nonzero = f4red;
                   7993:         for ( r = nflist; r; r = NEXT(r) ) {
                   7994:             nf = (NDV)BDY(r);
                   7995:             ndv_removecont(m,nf);
                   7996:             if ( !m && nd_nalg ) {
                   7997:                 ND nf1;
                   7998:
                   7999:                 nf1 = ndvtond(m,nf);
                   8000:                 nd_monic(0,&nf1);
                   8001:                 nd_removecont(m,nf1);
                   8002:                 nf = ndtondv(m,nf1);
                   8003:             }
1.24      noro     8004:             nh = ndv_newps(m,nf,0);
1.1       noro     8005:             d = update_pairs(d,g,nh,0);
                   8006:             g = update_base(g,nh);
                   8007:         }
                   8008:         if ( DP_Print ) {
                   8009:           fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
                   8010:         }
                   8011:         if ( nd_gentrace ) {
                   8012:       for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
                   8013:         NEXTNODE(tn0,tn);
1.6       noro     8014:                 STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1       noro     8015:                 node = mknode(2,i1,i2); MKLIST(l0,node);
                   8016:         BDY(tn) = l0;
                   8017:       }
                   8018:       if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6       noro     8019:             STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1       noro     8020:             MKNODE(node,l1,nzlist); nzlist = node;
                   8021:         }
                   8022:         if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
                   8023:         f4red++;
                   8024:         if ( nd_f4red && f4red > nd_f4red ) break;
                   8025:         if ( nd_rank0 && !nflist ) break;
                   8026:     }
                   8027:     if ( nd_gentrace ) {
                   8028:     MKLIST(l0,reverse_node(nzlist));
                   8029:         MKNODE(nd_alltracelist,l0,0);
                   8030:     }
                   8031: #if 0
                   8032:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   8033: #endif
1.12      noro     8034:   if ( DP_Print ) {
                   8035:     fprintf(asir_out,"number of red=%d,",Nf4_red);
                   8036:     fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
                   8037:       f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
                   8038:   }
1.1       noro     8039:   conv_ilist(nd_demand,0,g,indp);
                   8040:     return g;
                   8041: }
                   8042:
                   8043: NODE nd_f4_trace(int m,int **indp)
                   8044: {
                   8045:     int i,nh,stat,index;
                   8046:     NODE r,g;
                   8047:     ND_pairs d,l,l0,t;
                   8048:     ND spol,red;
                   8049:     NDV nf,redv,nfqv,nfv;
                   8050:     NM s0,s;
                   8051:     NODE rp0,srp0,nflist;
                   8052:     int nsp,nred,col,rank,len,k,j,a;
                   8053:     UINT c;
                   8054:     UINT **spmat;
                   8055:     UINT *s0vect,*svect,*p,*v;
                   8056:     int *colstat;
                   8057:     IndArray *imat;
                   8058:     int *rhead;
                   8059:     int spcol,sprow;
                   8060:     int sugar;
                   8061:     PGeoBucket bucket;
                   8062:     struct oEGT eg0,eg1,eg_f4;
                   8063:
                   8064:     g = 0; d = 0;
                   8065:     for ( i = 0; i < nd_psn; i++ ) {
                   8066:         d = update_pairs(d,g,i,0);
                   8067:         g = update_base(g,i);
                   8068:     }
                   8069:     while ( d ) {
                   8070:         get_eg(&eg0);
                   8071:         l = nd_minsugarp(d,&d);
                   8072:         sugar = SG(l);
                   8073:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   8074:         bucket = create_pbucket();
                   8075:         stat = nd_sp_f4(m,0,l,bucket);
                   8076:         if ( !stat ) {
                   8077:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8078:             NEXT(t) = d; d = l;
                   8079:             d = nd_reconstruct(1,d);
                   8080:             continue;
                   8081:         }
                   8082:         if ( bucket->m < 0 ) continue;
                   8083:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   8084:         if ( !col ) {
                   8085:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8086:             NEXT(t) = d; d = l;
                   8087:             d = nd_reconstruct(1,d);
                   8088:             continue;
                   8089:         }
                   8090:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   8091:         if ( DP_Print )
1.3       noro     8092:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5       noro     8093:                 sugar,eg_f4.exectime);
1.1       noro     8094:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   8095:         if ( !l0 ) continue;
                   8096:         l = l0;
                   8097:
                   8098:         /* over Q */
                   8099:         bucket = create_pbucket();
                   8100:         stat = nd_sp_f4(0,1,l,bucket);
                   8101:         if ( !stat ) {
                   8102:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8103:             NEXT(t) = d; d = l;
                   8104:             d = nd_reconstruct(1,d);
                   8105:             continue;
                   8106:         }
                   8107:         if ( bucket->m < 0 ) continue;
                   8108:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   8109:         if ( !col ) {
                   8110:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8111:             NEXT(t) = d; d = l;
                   8112:             d = nd_reconstruct(1,d);
                   8113:             continue;
                   8114:         }
                   8115:         nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   8116:         /* adding new bases */
                   8117:         for ( r = nflist; r; r = NEXT(r) ) {
                   8118:             nfqv = (NDV)BDY(r);
                   8119:             ndv_removecont(0,nfqv);
1.6       noro     8120:             if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1       noro     8121:             if ( nd_nalg ) {
                   8122:                 ND nf1;
                   8123:
                   8124:                 nf1 = ndvtond(m,nfqv);
                   8125:                 nd_monic(0,&nf1);
                   8126:                 nd_removecont(0,nf1);
                   8127:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   8128:             }
                   8129:             nfv = ndv_dup(0,nfqv);
                   8130:             ndv_mod(m,nfv);
                   8131:             ndv_removecont(m,nfv);
1.24      noro     8132:             nh = ndv_newps(0,nfv,nfqv);
1.1       noro     8133:             d = update_pairs(d,g,nh,0);
                   8134:             g = update_base(g,nh);
                   8135:         }
                   8136:     }
                   8137: #if 0
                   8138:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   8139: #endif
                   8140:   conv_ilist(nd_demand,1,g,indp);
                   8141:     return g;
                   8142: }
                   8143:
                   8144: int rref(matrix mat,int *sugar)
                   8145: {
                   8146:   int row,col,i,j,k,l,s,wcol,wj;
                   8147:   unsigned long bj;
                   8148:   unsigned long **a;
                   8149:   unsigned long *ai,*ak,*as,*t;
                   8150:   int *pivot;
                   8151:
                   8152:   row = mat->row;
                   8153:   col = mat->col;
                   8154:   a = mat->a;
                   8155:   wcol = (col+BLEN-1)/BLEN;
                   8156:   pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   8157:   i = 0;
                   8158:   for ( j = 0; j < col; j++ ) {
                   8159:   wj = j/BLEN; bj = 1L<<(j%BLEN);
                   8160:     for ( k = i; k < row; k++ )
                   8161:     if ( a[k][wj] & bj ) break;
                   8162:     if ( k == row ) continue;
                   8163:   pivot[i] = j;
                   8164:     if ( k != i ) {
                   8165:    t = a[i]; a[i] = a[k]; a[k] = t;
                   8166:    s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
                   8167:   }
                   8168:   ai = a[i];
                   8169:     for ( k = i+1; k < row; k++ ) {
                   8170:     ak = a[k];
                   8171:     if ( ak[wj] & bj ) {
                   8172:       for ( l = wj; l < wcol; l++ )
                   8173:       ak[l] ^= ai[l];
                   8174:       sugar[k] = MAX(sugar[k],sugar[i]);
                   8175:     }
                   8176:   }
                   8177:   i++;
                   8178:   }
                   8179:   for ( k = i-1; k >= 0; k-- ) {
                   8180:     j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
                   8181:   ak = a[k];
                   8182:     for ( s = 0; s < k; s++ ) {
                   8183:     as = a[s];
                   8184:       if ( as[wj] & bj ) {
                   8185:         for ( l = wj; l < wcol; l++ )
                   8186:       as[l] ^= ak[l];
                   8187:       sugar[s] = MAX(sugar[s],sugar[k]);
                   8188:     }
                   8189:   }
                   8190:   }
                   8191:   return i;
                   8192: }
                   8193:
                   8194: void print_matrix(matrix mat)
                   8195: {
                   8196:   int row,col,i,j;
                   8197:   unsigned long *ai;
                   8198:
                   8199:   row = mat->row;
                   8200:   col = mat->col;
                   8201:   printf("%d x %d\n",row,col);
                   8202:   for ( i = 0; i < row; i++ ) {
                   8203:   ai = mat->a[i];
                   8204:     for ( j = 0; j < col; j++ ) {
                   8205:     if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
                   8206:     else putchar('0');
                   8207:   }
                   8208:   putchar('\n');
                   8209:   }
                   8210: }
                   8211:
                   8212: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
                   8213:
                   8214: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
                   8215: {
                   8216:   int row,col,wcol,wj,i,j;
                   8217:   unsigned long bj;
                   8218:   unsigned long *ai;
                   8219:   unsigned long **a;
                   8220:   int len;
                   8221:   int *pos;
                   8222:
                   8223:   row = mat->row;
                   8224:   col = mat->col;
                   8225:   wcol = (col+BLEN-1)/BLEN;
                   8226:   pos = (int *)MALLOC(wcol*sizeof(int));
                   8227:   bzero(pos,wcol*sizeof(int));
                   8228:   for ( i = j = 0; i < wcol; i++ )
                   8229:     if ( v[i] ) pos[j++] = i;;
                   8230:   len = j;
                   8231:   wj = rhead/BLEN;
                   8232:   bj = 1L<<rhead%BLEN;
                   8233:   a = mat->a;
                   8234:   for ( i = 0; i < row; i++ ) {
                   8235:   ai = a[i];
                   8236:     if ( ai[wj]&bj ) {
                   8237:     for ( j = 0; j < len; j++ )
                   8238:       ai[pos[j]] ^= v[pos[j]];
                   8239:     sugar[i] = MAX(sugar[i],rsugar);
                   8240:   }
                   8241:   }
                   8242: }
                   8243:
                   8244: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   8245: {
                   8246:     int nsp,nred,i,i0,k,rank,row;
                   8247:     NODE r0,rp;
                   8248:     ND_pairs sp;
                   8249:   ND spol;
                   8250:   NM_ind_pair rt;
                   8251:     int *s0hash;
                   8252:   UINT *s;
                   8253:   int *pivot,*sugar,*head;
                   8254:   matrix mat;
                   8255:     NM m;
                   8256:     NODE r;
                   8257:   struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
                   8258:   int rhead,rsugar,size;
                   8259:     unsigned long *v;
                   8260:
                   8261:     get_eg(&eg0);
                   8262:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   8263:     nred = length(rp0);
                   8264:     mat = alloc_matrix(nsp,col);
                   8265:     s0hash = (int *)MALLOC(col*sizeof(int));
                   8266:     for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
                   8267:         s0hash[i] = ndl_hash_value(s);
                   8268:
                   8269:   sugar = (int *)MALLOC(nsp*sizeof(int));
                   8270:   for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
                   8271:     nd_sp(2,0,sp,&spol);
                   8272:     if ( spol ) {
                   8273:         mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
                   8274:       sugar[i] = SG(spol);
                   8275:       i++;
                   8276:     }
                   8277:   }
                   8278:   mat->row = i;
                   8279:     if ( DP_Print ) {
                   8280:       fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
                   8281:     }
                   8282:   size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
                   8283:   v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
                   8284:     for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
                   8285:     rt = (NM_ind_pair)BDY(rp);
                   8286:     bzero(v,size);
                   8287:         rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
                   8288:     rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
                   8289:       red_by_vect_2(mat,sugar,v,rhead,rsugar);
                   8290:   }
                   8291:
                   8292:     get_eg(&eg1);
                   8293:     init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
                   8294:   rank = rref(mat,sugar);
                   8295:
                   8296:     for ( i = 0, r0 = 0; i < rank; i++ ) {
                   8297:       NEXTNODE(r0,r);
                   8298:     BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
                   8299:       SG((NDV)BDY(r)) = sugar[i];
                   8300:     }
                   8301:     if ( r0 ) NEXT(r) = 0;
                   8302:     get_eg(&eg2);
                   8303:     init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
                   8304:     if ( DP_Print ) {
                   8305:         fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5       noro     8306:       eg_elim1.exectime,eg_elim2.exectime);
1.1       noro     8307:         fflush(asir_out);
                   8308:   }
                   8309:     return r0;
                   8310: }
                   8311:
                   8312:
                   8313: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   8314: {
                   8315:     IndArray *imat;
1.11      noro     8316:     int nsp,nred,i,start;
1.1       noro     8317:     int *rhead;
                   8318:     NODE r0,rp;
                   8319:     ND_pairs sp;
                   8320:     NM_ind_pair *rvect;
                   8321:     UINT *s;
                   8322:     int *s0hash;
1.11      noro     8323:     struct oEGT eg0,eg1,eg_conv;
1.1       noro     8324:
                   8325:     if ( m == 2 && nd_rref2 )
                   8326:      return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
                   8327:
                   8328:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   8329:     nred = length(rp0);
                   8330:     imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   8331:     rhead = (int *)MALLOC(col*sizeof(int));
                   8332:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   8333:
                   8334:     /* construction of index arrays */
1.11      noro     8335:     get_eg(&eg0);
1.1       noro     8336:     if ( DP_Print ) {
1.11      noro     8337:       fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   8338:       fflush(asir_out);
1.1       noro     8339:     }
                   8340:     rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11      noro     8341:     for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1       noro     8342:         rvect[i] = (NM_ind_pair)BDY(rp);
1.11      noro     8343:         imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1       noro     8344:         rhead[imat[i]->head] = 1;
1.11      noro     8345:         start = imat[i]->head;
                   8346:     }
1.12      noro     8347:     get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11      noro     8348:     if ( DP_Print ) {
                   8349:       fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   8350:       fflush(asir_out);
1.1       noro     8351:     }
                   8352:     if ( m > 0 )
1.7       noro     8353: #if SIZEOF_LONG==8
1.1       noro     8354:         r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   8355: #else
                   8356:         r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   8357: #endif
                   8358:     else if ( m == -1 )
                   8359:         r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   8360:     else if ( m == -2 )
                   8361:         r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   8362:     else
                   8363:         r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   8364:     return r0;
                   8365: }
                   8366:
                   8367: /* for Fp, 2<=p<2^16 */
                   8368:
                   8369: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   8370:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   8371: {
                   8372:     int spcol,sprow,a;
                   8373:     int i,j,k,l,rank;
                   8374:     NODE r0,r;
                   8375:     ND_pairs sp;
                   8376:     ND spol;
                   8377:     UINT **spmat;
                   8378:     UINT *svect,*v;
                   8379:     int *colstat;
                   8380:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8381:     int maxrs;
                   8382:     int *spsugar;
                   8383:     ND_pairs *spactive;
                   8384:
                   8385:     spcol = col-nred;
                   8386:     get_eg(&eg0);
                   8387:     /* elimination (1st step) */
                   8388:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   8389:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   8390:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8391:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   8392:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8393:         nd_sp(m,0,sp,&spol);
                   8394:         if ( !spol ) continue;
                   8395:         nd_to_vect(m,s0vect,col,spol,svect);
                   8396:         if ( m == -1 )
                   8397:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   8398:         else
                   8399:             maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
                   8400:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   8401:         if ( i < col ) {
                   8402:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   8403:             for ( j = k = 0; j < col; j++ )
                   8404:                 if ( !rhead[j] ) v[k++] = svect[j];
                   8405:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8406:             if ( nz )
                   8407:             spactive[sprow] = sp;
                   8408:             sprow++;
                   8409:         }
                   8410:         nd_free(spol);
                   8411:     }
                   8412:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8413:     if ( DP_Print ) {
1.5       noro     8414:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8415:         fflush(asir_out);
                   8416:     }
                   8417:     /* free index arrays */
                   8418:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   8419:
                   8420:     /* elimination (2nd step) */
                   8421:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8422:     if ( m == -1 )
                   8423:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   8424:     else
                   8425:         rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   8426:     r0 = 0;
                   8427:     for ( i = 0; i < rank; i++ ) {
                   8428:         NEXTNODE(r0,r); BDY(r) =
                   8429:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   8430:         SG((NDV)BDY(r)) = spsugar[i];
                   8431:         GCFREE(spmat[i]);
                   8432:     }
                   8433:     if ( r0 ) NEXT(r) = 0;
                   8434:
                   8435:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   8436:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8437:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8438:     if ( DP_Print ) {
1.5       noro     8439:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8440:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8441:             nsp,nred,sprow,spcol,rank);
1.5       noro     8442:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     8443:     }
                   8444:     if ( nz ) {
                   8445:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   8446:         if ( rank > 0 ) {
                   8447:             NEXT(spactive[rank-1]) = 0;
                   8448:             *nz = spactive[0];
                   8449:         } else
                   8450:             *nz = 0;
                   8451:     }
                   8452:     return r0;
                   8453: }
                   8454:
                   8455:
                   8456: /* for small finite fields */
                   8457:
                   8458: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   8459:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   8460: {
                   8461:     int spcol,sprow,a;
                   8462:     int i,j,k,l,rank;
                   8463:     NODE r0,r;
                   8464:     ND_pairs sp;
                   8465:     ND spol;
                   8466:     UINT **spmat;
                   8467:     UINT *svect,*v;
                   8468:     int *colstat;
                   8469:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8470:     int maxrs;
                   8471:     int *spsugar;
                   8472:     ND_pairs *spactive;
                   8473:
                   8474:     spcol = col-nred;
                   8475:     get_eg(&eg0);
                   8476:     /* elimination (1st step) */
                   8477:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   8478:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   8479:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8480:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   8481:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8482:         nd_sp(m,0,sp,&spol);
                   8483:         if ( !spol ) continue;
                   8484:         nd_to_vect(m,s0vect,col,spol,svect);
                   8485:         maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   8486:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   8487:         if ( i < col ) {
                   8488:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   8489:             for ( j = k = 0; j < col; j++ )
                   8490:                 if ( !rhead[j] ) v[k++] = svect[j];
                   8491:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8492:             if ( nz )
                   8493:             spactive[sprow] = sp;
                   8494:             sprow++;
                   8495:         }
                   8496:         nd_free(spol);
                   8497:     }
                   8498:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8499:     if ( DP_Print ) {
1.5       noro     8500:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8501:         fflush(asir_out);
                   8502:     }
                   8503:     /* free index arrays */
                   8504:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   8505:
                   8506:     /* elimination (2nd step) */
                   8507:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8508:     rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   8509:     r0 = 0;
                   8510:     for ( i = 0; i < rank; i++ ) {
                   8511:         NEXTNODE(r0,r); BDY(r) =
                   8512:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   8513:         SG((NDV)BDY(r)) = spsugar[i];
                   8514:         GCFREE(spmat[i]);
                   8515:     }
                   8516:     if ( r0 ) NEXT(r) = 0;
                   8517:
                   8518:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   8519:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8520:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8521:     if ( DP_Print ) {
1.5       noro     8522:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8523:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8524:             nsp,nred,sprow,spcol,rank);
1.5       noro     8525:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     8526:     }
                   8527:     if ( nz ) {
                   8528:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   8529:         if ( rank > 0 ) {
                   8530:             NEXT(spactive[rank-1]) = 0;
                   8531:             *nz = spactive[0];
                   8532:         } else
                   8533:             *nz = 0;
                   8534:     }
                   8535:     return r0;
                   8536: }
                   8537:
                   8538: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   8539:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   8540: {
                   8541:     int spcol,sprow,a;
                   8542:     int i,j,k,l,rank;
                   8543:     NODE r0,r;
                   8544:     ND_pairs sp;
                   8545:     ND spol;
                   8546:     mpz_t **spmat;
                   8547:     mpz_t *svect,*v;
                   8548:     int *colstat;
                   8549:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8550:     int maxrs;
                   8551:     int *spsugar;
                   8552:     pointer *w;
                   8553:
                   8554:     spcol = col-nred;
                   8555:     get_eg(&eg0);
                   8556:     /* elimination (1st step) */
                   8557:     spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
                   8558:     svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   8559:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8560:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8561:         nd_sp(m,trace,sp,&spol);
                   8562:         if ( !spol ) continue;
                   8563:         nd_to_vect_lf(s0vect,col,spol,svect);
                   8564:         maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
                   8565:         for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
                   8566:         if ( i < col ) {
                   8567:             spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
                   8568:             for ( j = k = 0; j < col; j++ )
                   8569:                 if ( !rhead[j] ) v[k++][0] = svect[j][0];
                   8570:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8571:             sprow++;
                   8572:         }
                   8573: /*        nd_free(spol); */
                   8574:     }
                   8575:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8576:     if ( DP_Print ) {
1.5       noro     8577:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8578:         fflush(asir_out);
                   8579:     }
                   8580:     /* free index arrays */
                   8581: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   8582:
                   8583:     /* elimination (2nd step) */
                   8584:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8585:     rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
                   8586:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   8587:     for ( i = 0; i < rank; i++ ) {
                   8588: #if 0
                   8589:         w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   8590:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   8591: #else
                   8592:         w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   8593:         SG((NDV)w[i]) = spsugar[i];
                   8594: #endif
                   8595: /*        GCFREE(spmat[i]); */
                   8596:
                   8597:     }
                   8598: #if 0
                   8599:     qsort(w,rank,sizeof(NDV),
                   8600:         (int (*)(const void *,const void *))ndv_compare);
                   8601: #endif
                   8602:     r0 = 0;
                   8603:     for ( i = 0; i < rank; i++ ) {
                   8604:         NEXTNODE(r0,r); BDY(r) = w[i];
                   8605:     }
                   8606:     if ( r0 ) NEXT(r) = 0;
                   8607:
                   8608: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   8609:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8610:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8611:     if ( DP_Print ) {
1.5       noro     8612:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8613:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8614:             nsp,nred,sprow,spcol,rank);
1.5       noro     8615:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     8616:     }
                   8617:     return r0;
                   8618: }
                   8619:
                   8620: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   8621:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   8622: {
                   8623:     int spcol,sprow,a;
                   8624:     int i,j,k,l,rank;
                   8625:     NODE r0,r;
                   8626:     ND_pairs sp;
                   8627:     ND spol;
                   8628:     Z **spmat;
                   8629:     Z *svect,*v;
                   8630:     int *colstat;
                   8631:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8632:     int maxrs;
                   8633:     int *spsugar;
                   8634:     pointer *w;
                   8635:
                   8636:     spcol = col-nred;
                   8637:     get_eg(&eg0);
                   8638:     /* elimination (1st step) */
                   8639:     spmat = (Z **)MALLOC(nsp*sizeof(Q *));
                   8640:     svect = (Z *)MALLOC(col*sizeof(Q));
                   8641:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8642:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8643:         nd_sp(0,trace,sp,&spol);
                   8644:         if ( !spol ) continue;
                   8645:         nd_to_vect_q(s0vect,col,spol,svect);
                   8646:         maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
                   8647:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   8648:         if ( i < col ) {
                   8649:             spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
                   8650:             for ( j = k = 0; j < col; j++ )
                   8651:                 if ( !rhead[j] ) v[k++] = svect[j];
                   8652:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8653:             sprow++;
                   8654:         }
                   8655: /*        nd_free(spol); */
                   8656:     }
                   8657:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8658:     if ( DP_Print ) {
1.5       noro     8659:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8660:         fflush(asir_out);
                   8661:     }
                   8662:     /* free index arrays */
                   8663: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   8664:
                   8665:     /* elimination (2nd step) */
                   8666:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8667:     rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
                   8668:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   8669:     for ( i = 0; i < rank; i++ ) {
                   8670: #if 0
                   8671:         w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   8672:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   8673: #else
                   8674:         w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   8675:         SG((NDV)w[i]) = spsugar[i];
                   8676: #endif
                   8677: /*        GCFREE(spmat[i]); */
                   8678:     }
                   8679: #if 0
                   8680:     qsort(w,rank,sizeof(NDV),
                   8681:         (int (*)(const void *,const void *))ndv_compare);
                   8682: #endif
                   8683:     r0 = 0;
                   8684:     for ( i = 0; i < rank; i++ ) {
                   8685:         NEXTNODE(r0,r); BDY(r) = w[i];
                   8686:     }
                   8687:     if ( r0 ) NEXT(r) = 0;
                   8688:
                   8689: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   8690:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8691:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8692:     if ( DP_Print ) {
1.5       noro     8693:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8694:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8695:             nsp,nred,sprow,spcol,rank);
1.5       noro     8696:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     8697:     }
                   8698:     return r0;
                   8699: }
                   8700:
                   8701: FILE *nd_write,*nd_read;
                   8702:
                   8703: void nd_send_int(int a) {
                   8704:     write_int(nd_write,(unsigned int *)&a);
                   8705: }
                   8706:
                   8707: void nd_send_intarray(int *p,int len) {
                   8708:     write_intarray(nd_write,(unsigned int *)p,len);
                   8709: }
                   8710:
                   8711: int nd_recv_int() {
                   8712:     int a;
                   8713:
                   8714:     read_int(nd_read,(unsigned int *)&a);
                   8715:     return a;
                   8716: }
                   8717:
                   8718: void nd_recv_intarray(int *p,int len) {
                   8719:     read_intarray(nd_read,(unsigned int *)p,len);
                   8720: }
                   8721:
                   8722: void nd_send_ndv(NDV p) {
                   8723:     int len,i;
                   8724:     NMV m;
                   8725:
                   8726:     if ( !p ) nd_send_int(0);
                   8727:     else {
                   8728:         len = LEN(p);
                   8729:         nd_send_int(len);
                   8730:         m = BDY(p);
                   8731:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   8732:             nd_send_int(CM(m));
                   8733:             nd_send_intarray((int *)DL(m),nd_wpd);
                   8734:         }
                   8735:     }
                   8736: }
                   8737:
                   8738: void nd_send_nd(ND p) {
                   8739:     int len,i;
                   8740:     NM m;
                   8741:
                   8742:     if ( !p ) nd_send_int(0);
                   8743:     else {
                   8744:         len = LEN(p);
                   8745:         nd_send_int(len);
                   8746:         m = BDY(p);
                   8747:         for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   8748:             nd_send_int(CM(m));
                   8749:             nd_send_intarray((int *)DL(m),nd_wpd);
                   8750:         }
                   8751:     }
                   8752: }
                   8753:
                   8754: NDV nd_recv_ndv()
                   8755: {
                   8756:     int len,i;
                   8757:     NMV m,m0;
                   8758:     NDV r;
                   8759:
                   8760:     len = nd_recv_int();
                   8761:     if ( !len ) return 0;
                   8762:     else {
                   8763:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8764: #if 0
                   8765:         ndv_alloc += len*nmv_adv;
                   8766: #endif
                   8767:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   8768:             CM(m) = nd_recv_int();
                   8769:             nd_recv_intarray((int *)DL(m),nd_wpd);
                   8770:         }
                   8771:         MKNDV(nd_nvar,m0,len,r);
                   8772:         return r;
                   8773:     }
                   8774: }
                   8775:
                   8776: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
                   8777: {
                   8778:     int i,j,t,c,rank,inv;
                   8779:     int *ci,*ri;
                   8780:     Z dn;
                   8781:     MAT m,nm;
                   8782:
                   8783:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
                   8784:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   8785:     for ( i = 0; i < row; i++ )
                   8786:         for ( j = 0; j < col; j++ )
                   8787:             mat0[i][j] = 0;
                   8788:     c = col-rank;
                   8789:     for ( i = 0; i < rank; i++ ) {
                   8790:         mat0[i][ri[i]] = dn;
                   8791:         for ( j = 0; j < c; j++ )
                   8792:             mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
                   8793:     }
                   8794:     return rank;
                   8795: }
                   8796:
                   8797: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   8798: {
                   8799:     int i,j,k,l,inv,a,rank,s;
                   8800:     unsigned int *t,*pivot,*pk;
                   8801:     unsigned int **mat;
                   8802:     ND_pairs pair;
                   8803:
                   8804:     mat = (unsigned int **)mat0;
                   8805:     for ( rank = 0, j = 0; j < col; j++ ) {
                   8806:         for ( i = rank; i < row; i++ )
                   8807:             mat[i][j] %= md;
                   8808:         for ( i = rank; i < row; i++ )
                   8809:             if ( mat[i][j] )
                   8810:                 break;
                   8811:         if ( i == row ) {
                   8812:             colstat[j] = 0;
                   8813:             continue;
                   8814:         } else
                   8815:             colstat[j] = 1;
                   8816:         if ( i != rank ) {
                   8817:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   8818:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   8819:             if ( spactive ) {
                   8820:                 pair = spactive[i]; spactive[i] = spactive[rank];
                   8821:                 spactive[rank] = pair;
                   8822:             }
                   8823:         }
                   8824:         pivot = mat[rank];
                   8825:         s = sugar[rank];
                   8826:         inv = invm(pivot[j],md);
                   8827:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   8828:             if ( *pk ) {
                   8829:                 if ( *pk >= (unsigned int)md )
                   8830:                     *pk %= md;
                   8831:                 DMAR(*pk,inv,0,md,*pk)
                   8832:             }
                   8833:         for ( i = rank+1; i < row; i++ ) {
                   8834:             t = mat[i];
                   8835:             if ( (a = t[j]) != 0 ) {
                   8836:                 sugar[i] = MAX(sugar[i],s);
                   8837:                 red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   8838:             }
                   8839:         }
                   8840:         rank++;
                   8841:     }
                   8842:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   8843:         if ( colstat[j] ) {
                   8844:             pivot = mat[l];
                   8845:             s = sugar[l];
                   8846:             for ( i = 0; i < l; i++ ) {
                   8847:                 t = mat[i];
                   8848:                 t[j] %= md;
                   8849:                 if ( (a = t[j]) != 0 ) {
                   8850:                     sugar[i] = MAX(sugar[i],s);
                   8851:                     red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   8852:                 }
                   8853:             }
                   8854:             l--;
                   8855:         }
                   8856:     for ( j = 0, l = 0; l < rank; j++ )
                   8857:         if ( colstat[j] ) {
                   8858:             t = mat[l];
                   8859:             for ( k = j; k < col; k++ )
                   8860:                 if ( t[k] >= (unsigned int)md )
                   8861:                     t[k] %= md;
                   8862:             l++;
                   8863:         }
                   8864:     return rank;
                   8865: }
                   8866:
                   8867:
1.7       noro     8868: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1       noro     8869: {
1.7       noro     8870:     int i,j,k,l,inv,a,rank,s;
                   8871:     unsigned int *t,*pivot,*pk;
                   8872:     unsigned int **mat;
                   8873:
                   8874:     mat = (unsigned int **)mat0;
                   8875:     for ( rank = 0, j = 0; j < col; j++ ) {
                   8876:         for ( i = rank; i < row; i++ )
                   8877:             if ( mat[i][j] )
                   8878:                 break;
                   8879:         if ( i == row ) {
                   8880:             colstat[j] = 0;
                   8881:             continue;
                   8882:         } else
                   8883:             colstat[j] = 1;
                   8884:         if ( i != rank ) {
                   8885:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   8886:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   8887:         }
                   8888:         pivot = mat[rank];
                   8889:         s = sugar[rank];
                   8890:         inv = _invsf(pivot[j]);
                   8891:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   8892:             if ( *pk )
                   8893:                 *pk = _mulsf(*pk,inv);
                   8894:         for ( i = rank+1; i < row; i++ ) {
                   8895:             t = mat[i];
                   8896:             if ( (a = t[j]) != 0 ) {
                   8897:                 sugar[i] = MAX(sugar[i],s);
                   8898:                 red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   8899:             }
                   8900:         }
                   8901:         rank++;
                   8902:     }
                   8903:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   8904:         if ( colstat[j] ) {
                   8905:             pivot = mat[l];
                   8906:             s = sugar[l];
                   8907:             for ( i = 0; i < l; i++ ) {
                   8908:                 t = mat[i];
                   8909:                 if ( (a = t[j]) != 0 ) {
                   8910:                     sugar[i] = MAX(sugar[i],s);
                   8911:                     red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   8912:                 }
                   8913:             }
                   8914:             l--;
                   8915:         }
                   8916:     return rank;
                   8917: }
1.1       noro     8918:
1.7       noro     8919: int ndv_ishomo(NDV p)
                   8920: {
                   8921:     NMV m;
                   8922:     int len,h;
1.1       noro     8923:
                   8924:     if ( !p ) return 1;
                   8925:     len = LEN(p);
                   8926:     m = BDY(p);
                   8927:     h = TD(DL(m));
                   8928:     NMV_ADV(m);
                   8929:     for ( len--; len; len--, NMV_ADV(m) )
1.20      noro     8930:         if ( TD(DL(m)) != h ) {
                   8931:           return 0;
                   8932:         }
1.1       noro     8933:     return 1;
                   8934: }
                   8935:
                   8936: void ndv_save(NDV p,int index)
                   8937: {
                   8938:     FILE *s;
                   8939:     char name[BUFSIZ];
                   8940:     short id;
                   8941:     int nv,sugar,len,n,i,td,e,j;
                   8942:     NMV m;
                   8943:     unsigned int *dl;
                   8944:     int mpos;
                   8945:
                   8946:     sprintf(name,"%s/%d",Demand,index);
                   8947:     s = fopen(name,"w");
                   8948:     savevl(s,0);
                   8949:     if ( !p ) {
                   8950:         saveobj(s,0);
                   8951:         return;
                   8952:     }
                   8953:     id = O_DP;
                   8954:     nv = NV(p);
                   8955:     sugar = SG(p);
                   8956:     len = LEN(p);
                   8957:     write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
                   8958:     write_int(s,(unsigned int *)&len);
                   8959:
                   8960:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     8961:         saveobj(s,(Obj)CZ(m));
1.1       noro     8962:         dl = DL(m);
                   8963:         td = TD(dl);
                   8964:         write_int(s,(unsigned int *)&td);
                   8965:         for ( j = 0; j < nv; j++ ) {
                   8966:             e = GET_EXP(dl,j);
                   8967:             write_int(s,(unsigned int *)&e);
                   8968:         }
                   8969:         if ( nd_module ) {
                   8970:             mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
                   8971:         }
                   8972:     }
                   8973:     fclose(s);
                   8974: }
                   8975:
                   8976: void nd_save_mod(ND p,int index)
                   8977: {
                   8978:     FILE *s;
                   8979:     char name[BUFSIZ];
                   8980:     int nv,sugar,len,c;
                   8981:     NM m;
                   8982:
                   8983:     sprintf(name,"%s/%d",Demand,index);
                   8984:     s = fopen(name,"w");
                   8985:     if ( !p ) {
                   8986:     len = 0;
                   8987:       write_int(s,(unsigned int *)&len);
                   8988:     fclose(s);
                   8989:         return;
                   8990:     }
                   8991:     nv = NV(p);
                   8992:     sugar = SG(p);
                   8993:     len = LEN(p);
                   8994:     write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
                   8995:   for ( m = BDY(p); m; m = NEXT(m) ) {
                   8996:     c = CM(m); write_int(s,(unsigned int *)&c);
                   8997:     write_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   8998:   }
                   8999:     fclose(s);
                   9000: }
                   9001:
                   9002: NDV ndv_load(int index)
                   9003: {
                   9004:     FILE *s;
                   9005:     char name[BUFSIZ];
                   9006:     short id;
                   9007:     int nv,sugar,len,n,i,td,e,j;
                   9008:     NDV d;
                   9009:     NMV m0,m;
                   9010:     unsigned int *dl;
                   9011:     Obj obj;
                   9012:     int mpos;
                   9013:
                   9014:     sprintf(name,"%s/%d",Demand,index);
                   9015:     s = fopen(name,"r");
                   9016:     if ( !s ) return 0;
                   9017:
                   9018:     skipvl(s);
                   9019:     read_short(s,(unsigned short *)&id);
                   9020:     if ( !id ) return 0;
                   9021:     read_int(s,(unsigned int *)&nv);
                   9022:     read_int(s,(unsigned int *)&sugar);
                   9023:     read_int(s,(unsigned int *)&len);
                   9024:
                   9025:     m0 = m = MALLOC(len*nmv_adv);
                   9026:     for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     9027:         loadobj(s,&obj); CZ(m) = (Z)obj;
1.1       noro     9028:         dl = DL(m);
                   9029:         ndl_zero(dl);
                   9030:         read_int(s,(unsigned int *)&td); TD(dl) = td;
                   9031:         for ( j = 0; j < nv; j++ ) {
                   9032:             read_int(s,(unsigned int *)&e);
                   9033:             PUT_EXP(dl,j,e);
                   9034:         }
                   9035:         if ( nd_module ) {
                   9036:             read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
                   9037:         }
                   9038:         if ( nd_blockmask ) ndl_weight_mask(dl);
                   9039:     }
                   9040:     fclose(s);
                   9041:     MKNDV(nv,m0,len,d);
                   9042:     SG(d) = sugar;
                   9043:     return d;
                   9044: }
                   9045:
                   9046: ND nd_load_mod(int index)
                   9047: {
                   9048:     FILE *s;
                   9049:     char name[BUFSIZ];
                   9050:     int nv,sugar,len,i,c;
                   9051:   ND d;
                   9052:     NM m0,m;
                   9053:
                   9054:     sprintf(name,"%s/%d",Demand,index);
                   9055:     s = fopen(name,"r");
                   9056:   /* if the file does not exist, it means p[index]=0 */
                   9057:     if ( !s ) return 0;
                   9058:
                   9059:     read_int(s,(unsigned int *)&nv);
                   9060:   if ( !nv ) { fclose(s); return 0; }
                   9061:
                   9062:     read_int(s,(unsigned int *)&sugar);
                   9063:     read_int(s,(unsigned int *)&len);
                   9064:   for ( m0 = 0, i = 0; i < len; i++ ) {
                   9065:     NEXTNM(m0,m);
                   9066:     read_int(s,(unsigned int *)&c); CM(m) = c;
                   9067:     read_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   9068:   }
                   9069:   NEXT(m) = 0;
                   9070:     MKND(nv,m0,len,d);
                   9071:     SG(d) = sugar;
                   9072:   fclose(s);
                   9073:     return d;
                   9074: }
                   9075:
                   9076: void nd_det(int mod,MAT f,P *rp)
                   9077: {
                   9078:     VL fv,tv;
                   9079:     int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
                   9080:     pointer **m;
                   9081:     P **w;
                   9082:     P mp,r;
                   9083:     NDV **dm;
                   9084:     NDV *t,*mi,*mj;
                   9085:     NDV d,s,mij,mjj;
                   9086:     ND u;
                   9087:     NMV nmv;
                   9088:     UINT *bound;
                   9089:     PGeoBucket bucket;
                   9090:     struct order_spec *ord;
                   9091:     Z dq,dt,ds;
                   9092:     Z mone;
                   9093:     Z gn,qn,dn0,nm,dn;
                   9094:
                   9095:     create_order_spec(0,0,&ord);
                   9096:     nd_init_ord(ord);
                   9097:     get_vars((Obj)f,&fv);
                   9098:     if ( f->row != f->col )
                   9099:         error("nd_det : non-square matrix");
                   9100:     n = f->row;
                   9101:     m = f->body;
                   9102:     for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   9103:
                   9104:     if ( !nvar ) {
                   9105:         if ( !mod )
                   9106:             detp(CO,(P **)m,n,rp);
                   9107:         else {
                   9108:             w = (P **)almat_pointer(n,n);
                   9109:             for ( i = 0; i < n; i++ )
                   9110:                 for ( j = 0; j < n; j++ )
                   9111:                     ptomp(mod,(P)m[i][j],&w[i][j]);
                   9112:             detmp(CO,mod,w,n,&mp);
                   9113:             mptop(mp,rp);
                   9114:         }
                   9115:         return;
                   9116:     }
                   9117:
                   9118:     if ( !mod ) {
                   9119:         w = (P **)almat_pointer(n,n);
                   9120:         dq = ONE;
                   9121:         for ( i = 0; i < n; i++ ) {
                   9122:             dn0 = ONE;
                   9123:             for ( j = 0; j < n; j++ ) {
                   9124:                 if ( !m[i][j] ) continue;
                   9125:                 lgp(m[i][j],&nm,&dn);
1.6       noro     9126:                 gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1       noro     9127:             }
                   9128:             if ( !UNIZ(dn0) ) {
                   9129:                 ds = dn0;
                   9130:                 for ( j = 0; j < n; j++ )
                   9131:                     mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   9132:                 mulz(dq,ds,&dt); dq = dt;
                   9133:             } else
                   9134:                 for ( j = 0; j < n; j++ )
                   9135:                     w[i][j] = (P)m[i][j];
                   9136:         }
                   9137:         m = (pointer **)w;
                   9138:     }
                   9139:
                   9140:     for ( i = 0, max = 1; i < n; i++ )
                   9141:         for ( j = 0; j < n; j++ )
                   9142:             for ( tv = fv; tv; tv = NEXT(tv) ) {
                   9143:                 e = getdeg(tv->v,(P)m[i][j]);
                   9144:                 max = MAX(e,max);
                   9145:             }
                   9146:     nd_setup_parameters(nvar,max);
                   9147:     dm = (NDV **)almat_pointer(n,n);
                   9148:     for ( i = 0, max = 1; i < n; i++ )
                   9149:         for ( j = 0; j < n; j++ ) {
                   9150:             dm[i][j] = ptondv(CO,fv,m[i][j]);
                   9151:             if ( mod ) ndv_mod(mod,dm[i][j]);
                   9152:             if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   9153:         }
                   9154:     d = ptondv(CO,fv,(P)ONE);
                   9155:     if ( mod ) ndv_mod(mod,d);
                   9156:     chsgnz(ONE,&mone);
                   9157:     for ( j = 0, sgn = 1; j < n; j++ ) {
                   9158:       if ( DP_Print ) {
                   9159:         fprintf(asir_out,".");
                   9160:       }
                   9161:         for ( i = j; i < n && !dm[i][j]; i++ );
                   9162:         if ( i == n ) {
                   9163:             *rp = 0;
                   9164:             return;
                   9165:         }
                   9166:         k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
                   9167:         for ( k = j; k < n; k++ )
                   9168:             for ( l = j; l < n; l++ )
                   9169:                 if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   9170:                     k0 = k; l0 = l; len0 = LEN(dm[k][l]);
                   9171:                 }
                   9172:         if ( k0 != j ) {
                   9173:             t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   9174:             sgn = -sgn;
                   9175:         }
                   9176:         if ( l0 != j ) {
                   9177:             for ( k = j; k < n; k++ ) {
                   9178:                 s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   9179:             }
                   9180:             sgn = -sgn;
                   9181:         }
                   9182:         bound = nd_det_compute_bound(dm,n,j);
                   9183:         for ( k = 0; k < nd_nvar; k++ )
                   9184:             if ( bound[k]*2 > nd_mask0 ) break;
                   9185:         if ( k < nd_nvar )
                   9186:             nd_det_reconstruct(dm,n,j,d);
                   9187:
                   9188:         for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
                   9189: /*            if ( DP_Print ) fprintf(asir_out,"    i=%d\n        ",i); */
                   9190:             mi = dm[i]; mij = mi[j];
                   9191:             if ( mod )
                   9192:                 ndv_mul_c(mod,mij,mod-1);
                   9193:             else
                   9194:                 ndv_mul_c_q(mij,mone);
                   9195:             for ( k = j+1; k < n; k++ ) {
                   9196: /*                if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
                   9197:                 bucket = create_pbucket();
                   9198:                 if ( mi[k] ) {
                   9199:                     nmv = BDY(mjj); len = LEN(mjj);
                   9200:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   9201:                         u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   9202:                         add_pbucket(mod,bucket,u);
                   9203:                     }
                   9204:                 }
                   9205:                 if ( mj[k] && mij ) {
                   9206:                     nmv = BDY(mij); len = LEN(mij);
                   9207:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   9208:                         u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   9209:                         add_pbucket(mod,bucket,u);
                   9210:                     }
                   9211:                 }
                   9212:                 u = nd_quo(mod,bucket,d);
                   9213:                 mi[k] = ndtondv(mod,u);
                   9214:             }
                   9215: /*            if ( DP_Print ) fprintf(asir_out,"\n",k); */
                   9216:         }
                   9217:         d = mjj;
                   9218:     }
                   9219:     if ( DP_Print ) {
                   9220:       fprintf(asir_out,"\n");
                   9221:     }
                   9222:     if ( sgn < 0 ) {
                   9223:         if ( mod )
                   9224:             ndv_mul_c(mod,d,mod-1);
                   9225:         else
                   9226:             ndv_mul_c_q(d,mone);
                   9227:     }
                   9228:     r = ndvtop(mod,CO,fv,d);
                   9229:     if ( !mod && !UNIQ(dq) )
                   9230:         divsp(CO,r,(P)dq,rp);
                   9231:     else
                   9232:         *rp = r;
                   9233: }
                   9234:
                   9235: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
                   9236: {
                   9237:     NM mr,mr0;
                   9238:     NM tnm;
                   9239:     NMV m;
                   9240:     UINT *d0,*dt,*dm;
                   9241:     int c,n,td,i,c1,c2,len;
                   9242:     Z q;
                   9243:     ND r;
                   9244:
                   9245:     if ( !p ) return 0;
                   9246:     else {
                   9247:         n = NV(p); m = BDY(p); len = LEN(p);
                   9248:         d0 = DL(m0);
                   9249:         td = TD(d);
                   9250:         mr0 = 0;
                   9251:         NEWNM(tnm);
                   9252:         if ( mod ) {
                   9253:             c = CM(m0);
                   9254:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9255:                 ndl_add(DL(m),d0,DL(tnm));
                   9256:                 if ( ndl_reducible(DL(tnm),d) ) {
                   9257:                     NEXTNM(mr0,mr);
                   9258:                     c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
                   9259:                     ndl_copy(DL(tnm),DL(mr));
                   9260:                 }
                   9261:             }
                   9262:         } else {
1.6       noro     9263:             q = CZ(m0);
1.1       noro     9264:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9265:                 ndl_add(DL(m),d0,DL(tnm));
                   9266:                 if ( ndl_reducible(DL(tnm),d) ) {
                   9267:                     NEXTNM(mr0,mr);
1.6       noro     9268:                     mulz(CZ(m),q,&CZ(mr));
1.1       noro     9269:                     ndl_copy(DL(tnm),DL(mr));
                   9270:                 }
                   9271:             }
                   9272:         }
                   9273:         if ( !mr0 )
                   9274:             return 0;
                   9275:         else {
                   9276:             NEXT(mr) = 0;
                   9277:             for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   9278:             MKND(NV(p),mr0,len,r);
                   9279:             SG(r) = SG(p) + TD(d0);
                   9280:             return r;
                   9281:         }
                   9282:     }
                   9283: }
                   9284:
                   9285: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   9286: {
                   9287:     int i,obpe,oadv,h,k,l;
                   9288:     static NM prev_nm_free_list;
                   9289:     EPOS oepos;
                   9290:
                   9291:     obpe = nd_bpe;
                   9292:     oadv = nmv_adv;
                   9293:     oepos = nd_epos;
                   9294:     if ( obpe < 2 ) nd_bpe = 2;
                   9295:     else if ( obpe < 3 ) nd_bpe = 3;
                   9296:     else if ( obpe < 4 ) nd_bpe = 4;
                   9297:     else if ( obpe < 5 ) nd_bpe = 5;
                   9298:     else if ( obpe < 6 ) nd_bpe = 6;
                   9299:     else if ( obpe < 8 ) nd_bpe = 8;
                   9300:     else if ( obpe < 10 ) nd_bpe = 10;
                   9301:     else if ( obpe < 16 ) nd_bpe = 16;
                   9302:     else if ( obpe < 32 ) nd_bpe = 32;
                   9303:     else error("nd_det_reconstruct : exponent too large");
                   9304:
                   9305:     nd_setup_parameters(nd_nvar,0);
                   9306:     prev_nm_free_list = _nm_free_list;
                   9307:     _nm_free_list = 0;
                   9308:     for ( k = j; k < n; k++ )
                   9309:         for (l = j; l < n; l++ )
                   9310:             ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   9311:     ndv_realloc(d,obpe,oadv,oepos);
                   9312:     prev_nm_free_list = 0;
                   9313: #if 0
                   9314:     GC_gcollect();
                   9315: #endif
                   9316: }
                   9317:
                   9318: /* returns a UINT array containing degree bounds */
                   9319:
                   9320: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   9321: {
                   9322:     UINT *d0,*d1,*d,*t,*r;
                   9323:     int k,l,i;
                   9324:
                   9325:     d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
                   9326:     for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
                   9327:     for ( k = j; k < n; k++ )
                   9328:         for ( l = j; l < n; l++ )
                   9329:             if ( dm[k][l] ) {
                   9330:                 d = ndv_compute_bound(dm[k][l]);
                   9331:                 for ( i = 0; i < nd_nvar; i++ )
                   9332:                     d0[i] = MAX(d0[i],d[i]);
                   9333:             }
                   9334:     return d0;
                   9335: }
                   9336:
                   9337: DL nd_separate_d(UINT *d,UINT *trans)
                   9338: {
                   9339:     int n,td,i,e,j;
                   9340:     DL a;
                   9341:
                   9342:     ndl_zero(trans);
                   9343:     td = 0;
                   9344:     for ( i = 0; i < nd_ntrans; i++ ) {
                   9345:         e = GET_EXP(d,i);
                   9346:         PUT_EXP(trans,i,e);
                   9347:         td += MUL_WEIGHT(e,i);
                   9348:     }
                   9349:     if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   9350:         /* homogenized */
                   9351:         i = nd_nvar-1;
                   9352:         e = GET_EXP(d,i);
                   9353:         PUT_EXP(trans,i,e);
                   9354:         td += MUL_WEIGHT(e,i);
                   9355:     }
                   9356:     TD(trans) = td;
                   9357:     if ( nd_blockmask) ndl_weight_mask(trans);
                   9358:     NEWDL(a,nd_nalg);
                   9359:     td = 0;
                   9360:     for ( i = 0; i < nd_nalg; i++ ) {
                   9361:         j = nd_ntrans+i;
                   9362:         e = GET_EXP(d,j);
                   9363:         a->d[i] = e;
                   9364:         td += e;
                   9365:     }
                   9366:     a->td = td;
                   9367:     return a;
                   9368: }
                   9369:
                   9370: int nd_monic(int mod,ND *p)
                   9371: {
                   9372:     UINT *trans,*t;
                   9373:     DL alg;
                   9374:     MP mp0,mp;
                   9375:     NM m,m0,m1,ma0,ma,mb,mr0,mr;
                   9376:     ND r;
                   9377:     DL dl;
                   9378:     DP nm;
                   9379:     NDV ndv;
                   9380:     DAlg inv,cd;
                   9381:     ND s,c;
                   9382:     Z l,mul;
                   9383:     Z ln;
                   9384:     int n,ntrans,i,e,td,is_lc,len;
                   9385:     NumberField nf;
                   9386:     struct oEGT eg0,eg1;
                   9387:
                   9388:     if ( !(nf = get_numberfield()) )
                   9389:         error("nd_monic : current_numberfield is not set");
                   9390:
                   9391:     /* Q coef -> DAlg coef */
                   9392:     NEWNM(ma0); ma = ma0;
                   9393:     m = BDY(*p);
                   9394:     is_lc = 1;
                   9395:     while ( 1 ) {
                   9396:         NEWMP(mp0); mp = mp0;
1.6       noro     9397:         mp->c = (Obj)CZ(m);
1.1       noro     9398:         mp->dl = nd_separate_d(DL(m),DL(ma));
                   9399:         NEWNM(mb);
                   9400:         for ( m = NEXT(m); m; m = NEXT(m) ) {
                   9401:             alg = nd_separate_d(DL(m),DL(mb));
                   9402:             if ( !ndl_equal(DL(ma),DL(mb)) )
                   9403:                 break;
1.6       noro     9404:             NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1       noro     9405:         }
                   9406:         NEXT(mp) = 0;
                   9407:         MKDP(nd_nalg,mp0,nm);
                   9408:         MKDAlg(nm,ONE,cd);
                   9409:         if ( is_lc == 1 ) {
                   9410:             /* if the lc is a rational number, we have nothing to do */
                   9411:             if ( !mp0->dl->td )
                   9412:                 return 1;
                   9413:
                   9414:             get_eg(&eg0);
                   9415:             invdalg(cd,&inv);
                   9416:             get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   9417:             /* check the validity of inv */
                   9418:             if ( mod && !remqi((Q)inv->dn,mod) )
                   9419:                 return 0;
                   9420:             CA(ma) = nf->one;
                   9421:             is_lc = 0;
                   9422:             ln = ONE;
                   9423:         } else {
                   9424:             muldalg(cd,inv,&CA(ma));
                   9425:             lcmz(ln,CA(ma)->dn,&ln);
                   9426:         }
                   9427:         if ( m ) {
                   9428:             NEXT(ma) = mb; ma = mb;
                   9429:         } else {
                   9430:             NEXT(ma) = 0;
                   9431:             break;
                   9432:         }
                   9433:     }
                   9434:     /* l = lcm(denoms) */
                   9435:     l = ln;
                   9436:     for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6       noro     9437:         divsz(l,CA(m)->dn,&mul);
1.1       noro     9438:         for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   9439:             NEXTNM(mr0,mr);
1.6       noro     9440:             mulz((Z)mp->c,mul,&CZ(mr));
1.1       noro     9441:             dl = mp->dl;
                   9442:             td = TD(DL(m));
                   9443:             ndl_copy(DL(m),DL(mr));
                   9444:             for ( i = 0; i < nd_nalg; i++ ) {
                   9445:                 e = dl->d[i];
                   9446:                 PUT_EXP(DL(mr),i+nd_ntrans,e);
                   9447:                 td += MUL_WEIGHT(e,i+nd_ntrans);
                   9448:             }
                   9449:             if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
                   9450:             TD(DL(mr)) = td;
                   9451:             if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   9452:         }
                   9453:     }
                   9454:     NEXT(mr) = 0;
                   9455:     for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   9456:     MKND(NV(*p),mr0,len,r);
                   9457:     /* XXX */
                   9458:     SG(r) = SG(*p);
                   9459:     nd_free(*p);
                   9460:     *p = r;
                   9461:     return 1;
                   9462: }
                   9463:
                   9464: NODE reverse_node(NODE n)
                   9465: {
                   9466:     NODE t,t1;
                   9467:
                   9468:     for ( t = 0; n; n = NEXT(n) ) {
                   9469:         MKNODE(t1,BDY(n),t); t = t1;
                   9470:     }
                   9471:     return t;
                   9472: }
                   9473:
                   9474: P ndc_div(int mod,union oNDC a,union oNDC b)
                   9475: {
                   9476:     union oNDC c;
                   9477:     int inv,t;
                   9478:
                   9479:     if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10      noro     9480:     else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1       noro     9481:     else if ( mod ) {
                   9482:         inv = invm(b.m,mod);
                   9483:         DMAR(a.m,inv,0,mod,t); c.m = t;
                   9484:     } else if ( nd_vc )
                   9485:        divsp(nd_vc,a.p,b.p,&c.p);
                   9486:     else
                   9487:        divsz(a.z,b.z,&c.z);
                   9488:     return ndctop(mod,c);
                   9489: }
                   9490:
                   9491: P ndctop(int mod,union oNDC c)
                   9492: {
                   9493:     Z q;
                   9494:     int e;
                   9495:     GFS gfs;
                   9496:
                   9497:     if ( mod == -1 ) {
                   9498:         e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
                   9499:     } else if ( mod == -2 ) {
1.10      noro     9500:        q = c.z; return (P)q;
1.1       noro     9501:     } else if ( mod > 0 ) {
1.6       noro     9502:         STOZ(c.m,q); return (P)q;
1.1       noro     9503:     } else
                   9504:         return (P)c.p;
                   9505: }
                   9506:
                   9507: /* [0,0,0,cont] = p -> p/cont */
                   9508:
                   9509: void finalize_tracelist(int i,P cont)
                   9510: {
                   9511:  LIST l;
                   9512:  NODE node;
                   9513:  Z iq;
                   9514:
                   9515:   if ( !UNIQ(cont) ) {
                   9516:     node = mknode(4,NULLP,NULLP,NULLP,cont);
                   9517:     MKLIST(l,node); MKNODE(node,l,nd_tracelist);
                   9518:     nd_tracelist = node;
                   9519:   }
1.6       noro     9520:   STOZ(i,iq);
1.1       noro     9521:   nd_tracelist = reverse_node(nd_tracelist);
                   9522:   MKLIST(l,nd_tracelist);
                   9523:   node = mknode(2,iq,l); MKLIST(l,node);
                   9524:   MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
                   9525:   nd_alltracelist = node; nd_tracelist = 0;
                   9526: }
                   9527:
                   9528: void conv_ilist(int demand,int trace,NODE g,int **indp)
                   9529: {
                   9530:     int n,i,j;
                   9531:   int *ind;
                   9532:   NODE t;
                   9533:
                   9534:     n = length(g);
                   9535:   ind = (int *)MALLOC(n*sizeof(int));
                   9536:   for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
                   9537:     j = (long)BDY(t); ind[i] = j;
                   9538:     BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
                   9539:   }
                   9540:   if ( indp ) *indp = ind;
                   9541: }
                   9542:
1.30      noro     9543: NODE conv_ilist_s(int demand,int trace,int **indp)
                   9544: {
                   9545:   int n,i,j;
                   9546:   int *ind;
                   9547:   NODE g0,g;
                   9548:
                   9549:   n = nd_psn;
                   9550:   ind = (int *)MALLOC(n*sizeof(int));
                   9551:   g0 = 0;
                   9552:   for ( i = 0; i < n; i++ ) {
                   9553:     ind[i] = i;
                   9554:     NEXTNODE(g0,g);
                   9555:     BDY(g) = (pointer)(demand?ndv_load(i):(trace?nd_ps_trace[i]:nd_ps[i]));
                   9556:   }
                   9557:   if ( g0 ) NEXT(g) = 0;
                   9558:   if ( indp ) *indp = ind;
                   9559:   return g0;
                   9560: }
                   9561:
1.1       noro     9562: void parse_nd_option(NODE opt)
                   9563: {
                   9564:     NODE t,p,u;
                   9565:   int i,s,n;
                   9566:     char *key;
                   9567:     Obj value;
                   9568:
                   9569:     nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
                   9570:   nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
                   9571:   nd_splist = 0; nd_check_splist = 0;
                   9572:     nd_sugarweight = 0;
                   9573:     nd_f4red =0;
                   9574:     nd_rank0 = 0;
                   9575:     for ( t = opt; t; t = NEXT(t) ) {
                   9576:         p = BDY((LIST)BDY(t));
                   9577:         key = BDY((STRING)BDY(p));
                   9578:         value = (Obj)BDY(NEXT(p));
                   9579:         if ( !strcmp(key,"gentrace") )
                   9580:             nd_gentrace = value?1:0;
                   9581:         else if ( !strcmp(key,"gensyz") )
                   9582:             nd_gensyz = value?1:0;
                   9583:         else if ( !strcmp(key,"nora") )
                   9584:             nd_nora = value?1:0;
                   9585:         else if ( !strcmp(key,"gbblock") ) {
                   9586:             if ( value && OID(value) == O_LIST ) {
                   9587:         u = BDY((LIST)value);
                   9588:               nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
                   9589:         for ( i = 0; u; u = NEXT(u) ) {
                   9590:           p = BDY((LIST)BDY(u));
1.6       noro     9591:           s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
                   9592:           nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1       noro     9593:         }
                   9594:         nd_gbblock[i] = -1;
                   9595:             } else
                   9596:               nd_gbblock = 0;
                   9597:     } else if ( !strcmp(key,"newelim") )
                   9598:             nd_newelim = value?1:0;
                   9599:     else if ( !strcmp(key,"intersect") )
                   9600:             nd_intersect = value?1:0;
1.17      noro     9601:     else if ( !strcmp(key,"syzgen") )
                   9602:             nd_intersect = ZTOS((Q)value);
1.1       noro     9603:     else if ( !strcmp(key,"lf") )
                   9604:             nd_lf = value?1:0;
                   9605:     else if ( !strcmp(key,"trace") ) {
                   9606:            if ( value ) {
                   9607:                u = BDY((LIST)value);
                   9608:            nd_nzlist = BDY((LIST)ARG2(u));
1.6       noro     9609:            nd_bpe = ZTOS((Q)ARG3(u));
1.1       noro     9610:            }
                   9611:     } else if ( !strcmp(key,"f4red") ) {
1.6       noro     9612:        nd_f4red = ZTOS((Q)value);
1.1       noro     9613:     } else if ( !strcmp(key,"rank0") ) {
                   9614:             nd_rank0 = value?1:0;
                   9615:     } else if ( !strcmp(key,"splist") ) {
                   9616:             nd_splist = value?1:0;
                   9617:     } else if ( !strcmp(key,"check_splist") ) {
                   9618:       nd_check_splist = BDY((LIST)value);
                   9619:     } else if ( !strcmp(key,"sugarweight") ) {
                   9620:       u = BDY((LIST)value);
                   9621:             n = length(u);
                   9622:             nd_sugarweight = MALLOC(n*sizeof(int));
                   9623:       for ( i = 0; i < n; i++, u = NEXT(u) )
1.6       noro     9624:                 nd_sugarweight[i] = ZTOS((Q)BDY(u));
1.1       noro     9625:     }
                   9626:     }
                   9627: }
                   9628:
                   9629: ND mdptond(DP d);
                   9630: ND nd_mul_nm(int mod,NM m0,ND p);
                   9631: ND nd_mul_nm_lf(NM m0,ND p);
                   9632: ND *btog(NODE ti,ND **p,int nb,int mod);
                   9633: ND btog_one(NODE ti,ND *p,int nb,int mod);
                   9634: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
                   9635: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
                   9636:
                   9637: /* d:monomial */
                   9638: ND mdptond(DP d)
                   9639: {
                   9640:   NM m;
                   9641:   ND r;
                   9642:
                   9643:   if ( OID(d) == 1 )
                   9644:   r = ptond(CO,CO,(P)d);
                   9645:   else {
                   9646:     NEWNM(m);
                   9647:     dltondl(NV(d),BDY(d)->dl,DL(m));
1.6       noro     9648:     CZ(m) = (Z)BDY(d)->c;
1.1       noro     9649:     NEXT(m) = 0;
                   9650:     MKND(NV(d),m,1,r);
                   9651:   }
                   9652:     return r;
                   9653: }
                   9654:
                   9655: ND nd_mul_nm(int mod,NM m0,ND p)
                   9656: {
                   9657:   UINT *d0;
                   9658:   int c0,c1,c;
                   9659:   NM tm,mr,mr0;
                   9660:   ND r;
                   9661:
                   9662:   if ( !p ) return 0;
                   9663:   d0 = DL(m0);
                   9664:   c0 = CM(m0);
                   9665:   mr0 = 0;
                   9666:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   9667:     NEXTNM(mr0,mr);
                   9668:   c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
                   9669:   ndl_add(d0,DL(tm),DL(mr));
                   9670:   }
                   9671:   NEXT(mr) = 0;
                   9672:   MKND(NV(p),mr0,LEN(p),r);
                   9673:   return r;
                   9674: }
                   9675:
                   9676: ND nd_mul_nm_lf(NM m0,ND p)
                   9677: {
                   9678:   UINT *d0;
                   9679:   Z c0,c1,c;
                   9680:   NM tm,mr,mr0;
                   9681:   ND r;
                   9682:
                   9683:   if ( !p ) return 0;
                   9684:   d0 = DL(m0);
                   9685:   c0 = CZ(m0);
                   9686:   mr0 = 0;
                   9687:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   9688:     NEXTNM(mr0,mr);
                   9689:     c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
                   9690:     ndl_add(d0,DL(tm),DL(mr));
                   9691:   }
                   9692:   NEXT(mr) = 0;
                   9693:   MKND(NV(p),mr0,LEN(p),r);
                   9694:   return r;
                   9695: }
                   9696:
                   9697: ND *btog(NODE ti,ND **p,int nb,int mod)
                   9698: {
                   9699:   PGeoBucket *r;
                   9700:   int i,ci;
                   9701:   NODE t,s;
                   9702:   ND m,tp;
                   9703:   ND *pi,*rd;
                   9704:   P c;
                   9705:
                   9706:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   9707:   for ( i = 0; i < nb; i++ )
                   9708:     r[i] = create_pbucket();
                   9709:   for ( t = ti; t; t = NEXT(t) ) {
                   9710:   s = BDY((LIST)BDY(t));
                   9711:     if ( ARG0(s) ) {
                   9712:     m = mdptond((DP)ARG2(s));
1.6       noro     9713:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     9714:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   9715:       HCM(m) = ci;
1.6       noro     9716:       pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     9717:       for ( i = 0; i < nb; i++ ) {
                   9718:       tp = nd_mul_nm(mod,BDY(m),pi[i]);
                   9719:         add_pbucket(mod,r[i],tp);
                   9720:       }
                   9721:     }
                   9722:     ci = 1;
                   9723:     } else {
                   9724:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   9725:     ci = invm(ci,mod);
                   9726:   }
                   9727:   }
                   9728:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   9729:   for ( i = 0; i < nb; i++ )
                   9730:   rd[i] = normalize_pbucket(mod,r[i]);
                   9731:   if ( ci != 1 )
                   9732:     for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
                   9733:    return rd;
                   9734: }
                   9735:
                   9736: /* YYY */
                   9737: ND *btog_lf(NODE ti,ND **p,int nb)
                   9738: {
                   9739:   PGeoBucket *r;
                   9740:   int i;
                   9741:   NODE t,s;
                   9742:   ND m,tp;
                   9743:   ND *pi,*rd;
                   9744:   LM lm;
                   9745:   Z lf,c;
                   9746:
                   9747:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   9748:   for ( i = 0; i < nb; i++ )
                   9749:     r[i] = create_pbucket();
                   9750:   for ( t = ti; t; t = NEXT(t) ) {
                   9751:     s = BDY((LIST)BDY(t));
                   9752:     if ( ARG0(s) ) {
                   9753:       m = mdptond((DP)ARG2(s));
1.6       noro     9754:       simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1       noro     9755:       if ( lm ) {
                   9756:         lmtolf(lm,&lf); HCZ(m) = lf;
1.6       noro     9757:         pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     9758:         for ( i = 0; i < nb; i++ ) {
                   9759:           tp = nd_mul_nm_lf(BDY(m),pi[i]);
                   9760:           add_pbucket(-2,r[i],tp);
                   9761:         }
                   9762:       }
                   9763:       c = ONE;
                   9764:     } else {
                   9765:       simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
                   9766:     }
                   9767:   }
                   9768:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   9769:   for ( i = 0; i < nb; i++ )
                   9770:     rd[i] = normalize_pbucket(-2,r[i]);
                   9771:   for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
                   9772:   return rd;
                   9773: }
                   9774:
                   9775: ND btog_one(NODE ti,ND *p,int nb,int mod)
                   9776: {
                   9777:   PGeoBucket r;
                   9778:   int i,ci,j;
                   9779:   NODE t,s;
                   9780:   ND m,tp;
                   9781:   ND pi,rd;
                   9782:   P c;
                   9783:
                   9784:   r = create_pbucket();
                   9785:   for ( t = ti; t; t = NEXT(t) ) {
                   9786:   s = BDY((LIST)BDY(t));
                   9787:     if ( ARG0(s) ) {
                   9788:     m = mdptond((DP)ARG2(s));
1.6       noro     9789:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     9790:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   9791:       HCM(m) = ci;
1.6       noro     9792:       pi = p[j=ZTOS((Q)ARG1(s))];
1.1       noro     9793:     if ( !pi ) {
                   9794:       pi = nd_load_mod(j);
                   9795:       tp = nd_mul_nm(mod,BDY(m),pi);
                   9796:       nd_free(pi);
                   9797:         add_pbucket(mod,r,tp);
                   9798:     } else {
                   9799:       tp = nd_mul_nm(mod,BDY(m),pi);
                   9800:         add_pbucket(mod,r,tp);
                   9801:       }
                   9802:     }
                   9803:     ci = 1;
                   9804:     } else {
                   9805:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   9806:     ci = invm(ci,mod);
                   9807:   }
                   9808:   }
                   9809:   rd = normalize_pbucket(mod,r);
                   9810:   free_pbucket(r);
                   9811:   if ( ci != 1 ) nd_mul_c(mod,rd,ci);
                   9812:   return rd;
                   9813: }
                   9814:
                   9815: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
                   9816:
                   9817: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
                   9818: {
                   9819:   int i,j,n,m,nb,pi0,pi1,nvar;
                   9820:   VL fv,tv,vv;
                   9821:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   9822:   ND **p;
                   9823:   ND *c;
                   9824:   ND u;
                   9825:   P inv;
                   9826:   MAT mat;
                   9827:
                   9828:   if ( mod == -2 )
                   9829:     return nd_btog_lf(f,v,ord,tlist,rp);
                   9830:
                   9831:   parse_nd_option(current_option);
                   9832:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   9833:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   9834:   switch ( ord->id ) {
                   9835:     case 1:
                   9836:       if ( ord->nv != nvar )
                   9837:         error("nd_check : invalid order specification");
                   9838:       break;
                   9839:     default:
                   9840:       break;
                   9841:   }
                   9842:   nd_init_ord(ord);
                   9843: #if 0
1.6       noro     9844:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     9845: #else
                   9846:   nd_bpe = 32;
                   9847: #endif
                   9848:   nd_setup_parameters(nvar,0);
                   9849:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   9850:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   9851:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   9852:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   9853:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     9854:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     9855:   if ( j > i ) i = j;
                   9856:   }
                   9857:   n = i+1;
                   9858:   nb = length(BDY(f));
                   9859:   p = (ND **)MALLOC(n*sizeof(ND *));
                   9860:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   9861:     pi = BDY((LIST)BDY(t));
1.6       noro     9862:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     9863:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   9864:     ptomp(mod,(P)ARG2(pi),&inv);
                   9865:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   9866:     u = ptond(CO,vv,(P)ONE);
                   9867:     HCM(u) = ((MQ)inv)->cont;
                   9868:     c[pi1] = u;
                   9869:   }
                   9870:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   9871:     printf("%d ",i); fflush(stdout);
                   9872:     ti = BDY((LIST)BDY(t));
1.6       noro     9873:     p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     9874:   }
                   9875:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   9876:     printf("%d ",i); fflush(stdout);
                   9877:     ti = BDY((LIST)BDY(t));
1.6       noro     9878:     p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     9879:   }
                   9880:   m = length(ind);
                   9881:   MKMAT(mat,nb,m);
                   9882:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     9883:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     9884:       BDY(mat)[i][j] = ndtodp(mod,c[i]);
                   9885:   return mat;
                   9886: }
                   9887:
                   9888: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
                   9889: {
                   9890:   int i,j,n,m,nb,pi0,pi1,nvar;
                   9891:   VL fv,tv,vv;
                   9892:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   9893:   ND **p;
                   9894:   ND *c;
                   9895:   ND u;
                   9896:   MAT mat;
                   9897:   LM lm;
                   9898:   Z lf,inv;
                   9899:
                   9900:   parse_nd_option(current_option);
                   9901:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   9902:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   9903:   switch ( ord->id ) {
                   9904:     case 1:
                   9905:       if ( ord->nv != nvar )
                   9906:         error("nd_check : invalid order specification");
                   9907:       break;
                   9908:     default:
                   9909:       break;
                   9910:   }
                   9911:   nd_init_ord(ord);
                   9912: #if 0
1.6       noro     9913:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     9914: #else
                   9915:   nd_bpe = 32;
                   9916: #endif
                   9917:   nd_setup_parameters(nvar,0);
                   9918:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   9919:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   9920:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   9921:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   9922:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     9923:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     9924:   if ( j > i ) i = j;
                   9925:   }
                   9926:   n = i+1;
                   9927:   nb = length(BDY(f));
                   9928:   p = (ND **)MALLOC(n*sizeof(ND *));
                   9929:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   9930:     pi = BDY((LIST)BDY(t));
1.6       noro     9931:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     9932:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   9933:     simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
                   9934:     u = ptond(CO,vv,(P)ONE);
                   9935:     HCZ(u) = inv;
                   9936:     c[pi1] = u;
                   9937:   }
                   9938:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   9939:     printf("%d ",i); fflush(stdout);
                   9940:     ti = BDY((LIST)BDY(t));
1.6       noro     9941:     p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     9942:   }
                   9943:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   9944:     printf("%d ",i); fflush(stdout);
                   9945:     ti = BDY((LIST)BDY(t));
1.6       noro     9946:     p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     9947:   }
                   9948:   m = length(ind);
                   9949:   MKMAT(mat,nb,m);
                   9950:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     9951:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     9952:       BDY(mat)[i][j] = ndtodp(-2,c[i]);
                   9953:   return mat;
                   9954: }
                   9955:
                   9956: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
                   9957:   LIST tlist,int pos,MAT *rp)
                   9958: {
                   9959:   int i,j,n,m,nb,pi0,pi1,nvar;
                   9960:   VL fv,tv,vv;
                   9961:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   9962:   ND *p;
                   9963:   ND *c;
                   9964:   ND u;
                   9965:   P inv;
                   9966:   VECT vect;
                   9967:
                   9968:   if ( mod == -2 )
                   9969:     error("nd_btog_one : not implemented yet for a large finite field");
                   9970:
                   9971:   parse_nd_option(current_option);
                   9972:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   9973:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   9974:   switch ( ord->id ) {
                   9975:     case 1:
                   9976:       if ( ord->nv != nvar )
                   9977:         error("nd_check : invalid order specification");
                   9978:       break;
                   9979:     default:
                   9980:       break;
                   9981:   }
                   9982:   nd_init_ord(ord);
                   9983: #if 0
1.6       noro     9984:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     9985: #else
                   9986:   nd_bpe = 32;
                   9987: #endif
                   9988:   nd_setup_parameters(nvar,0);
                   9989:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   9990:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   9991:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   9992:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   9993:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     9994:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     9995:   if ( j > i ) i = j;
                   9996:   }
                   9997:   n = i+1;
                   9998:   nb = length(BDY(f));
                   9999:   p = (ND *)MALLOC(n*sizeof(ND *));
                   10000:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   10001:     pi = BDY((LIST)BDY(t));
1.6       noro     10002:   pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     10003:   if ( pi1 == pos ) {
                   10004:     ptomp(mod,(P)ARG2(pi),&inv);
                   10005:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   10006:     u = ptond(CO,vv,(P)ONE);
                   10007:     HCM(u) = ((MQ)inv)->cont;
                   10008:     p[pi0] = u;
                   10009:     }
                   10010:   }
                   10011:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   10012:   printf("%d ",i); fflush(stdout);
                   10013:     ti = BDY((LIST)BDY(t));
1.6       noro     10014:     p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10015:     if ( Demand ) {
                   10016:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   10017:   }
                   10018:   }
                   10019:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   10020:   printf("%d ",i); fflush(stdout);
                   10021:     ti = BDY((LIST)BDY(t));
1.6       noro     10022:     p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10023:     if ( Demand ) {
                   10024:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   10025:   }
                   10026:   }
                   10027:   m = length(ind);
                   10028:   MKVECT(vect,m);
                   10029:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6       noro     10030:   u = p[ZTOS((Q)BDY(t))];
1.1       noro     10031:   if ( !u ) {
1.6       noro     10032:     u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1       noro     10033:     BDY(vect)[j] = ndtodp(mod,u);
                   10034:     nd_free(u);
                   10035:   } else
                   10036:     BDY(vect)[j] = ndtodp(mod,u);
                   10037:   }
                   10038:   return vect;
                   10039: }
                   10040:
                   10041: void ndv_print_lf(NDV p)
                   10042: {
                   10043:     NMV m;
                   10044:     int i,len;
                   10045:
                   10046:     if ( !p ) printf("0\n");
                   10047:     else {
                   10048:         len = LEN(p);
                   10049:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   10050:             printf("+");
                   10051:             mpz_out_str(asir_out,10,BDY(CZ(m)));
                   10052:             printf("*");
                   10053:             ndl_print(DL(m));
                   10054:         }
                   10055:         printf("\n");
                   10056:     }
                   10057: }
                   10058:
                   10059: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
                   10060: {
                   10061:     VL tv,fv,vv,vc,av;
                   10062:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   10063:     int m,nocheck,nvar,mindex,e,max;
                   10064:     NDV c;
                   10065:     NMV a;
                   10066:     P p,zp;
                   10067:     Q dmy;
                   10068:     EPOS oepos;
                   10069:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   10070:     Alg alpha,dp;
                   10071:     P poly;
                   10072:     LIST f1,f2,zpl;
                   10073:     Obj obj;
                   10074:     NumberField nf;
                   10075:     struct order_spec *ord1;
                   10076:     struct oEGT eg_check,eg0,eg1;
                   10077:     NODE tr,tl1,tl2,tl3,tl4;
                   10078:     LIST l1,l2,l3,l4,l5;
                   10079:     int *perm;
                   10080:     int j,ret;
                   10081:     NODE retn;
                   10082:     Q jq,bpe;
                   10083:
                   10084:     nd_module = 0;
                   10085:     parse_nd_option(current_option);
                   10086:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   10087:     if ( nd_vc )
                   10088:       error("nd_f4_lf_trace : computation over a rational function field is not implemented");
                   10089:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   10090:     switch ( ord->id ) {
                   10091:         case 1:
                   10092:             if ( ord->nv != nvar )
                   10093:                 error("nd_f4_lf_trace : invalid order specification");
                   10094:             break;
                   10095:         default:
                   10096:             break;
                   10097:     }
                   10098:
                   10099:     nd_ntrans = nvar;
                   10100:     nd_nalg = 0;
                   10101:
                   10102:     nocheck = 0;
                   10103:     mindex = 0;
                   10104:
                   10105:     /* do not use on-demand load/save */
                   10106:     nd_demand = 0;
                   10107:     m = trace > 1 ? trace : get_lprime(mindex);
                   10108:     nd_init_ord(ord);
                   10109:     mrank = 0;
                   10110:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   10111:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   10112:             if ( nd_module ) {
                   10113:                 s = BDY((LIST)BDY(t));
                   10114:                 trank = length(s);
                   10115:                 mrank = MAX(mrank,trank);
                   10116:                 for ( ; s; s = NEXT(s) ) {
                   10117:                     e = getdeg(tv->v,(P)BDY(s));
                   10118:                     max = MAX(e,max);
                   10119:                 }
                   10120:             } else {
                   10121:                 e = getdeg(tv->v,(P)BDY(t));
                   10122:                 max = MAX(e,max);
                   10123:             }
                   10124:         }
                   10125:     nd_setup_parameters(nvar,max);
                   10126:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   10127:     ishomo = 1;
                   10128:     /* XXX */
                   10129:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   10130:         if ( nd_module ) {
                   10131:             c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
                   10132:         } else {
                   10133:             c = (pointer)ptondv(CO,vv,(P)BDY(t));
                   10134:         }
                   10135:         if ( ishomo )
                   10136:             ishomo = ishomo && ndv_ishomo(c);
                   10137:         if ( c ) {
                   10138:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   10139:             ndv_mod(-2,c);
                   10140:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   10141:         }
                   10142:     }
                   10143:     if ( in0 ) NEXT(in) = 0;
                   10144:     if ( fd0 ) NEXT(fd) = 0;
                   10145:     if ( !ishomo && homo ) {
                   10146:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   10147:             c = (NDV)BDY(t); len = LEN(c);
                   10148:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   10149:                 wmax = MAX(TD(DL(a)),wmax);
                   10150:         }
                   10151:         homogenize_order(ord,nvar,&ord1);
                   10152:         nd_init_ord(ord1);
                   10153:         nd_setup_parameters(nvar+1,wmax);
                   10154:         for ( t = fd0; t; t = NEXT(t) )
                   10155:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   10156:     }
                   10157:     if ( MaxDeg > 0 ) nocheck = 1;
1.24      noro     10158:     ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1       noro     10159:     if ( ret )
                   10160:       cand = nd_f4_lf_trace_main(m,&perm);
                   10161:     if ( !ret || !cand ) {
                   10162:        *rp = 0; return;
                   10163:     }
                   10164:     if ( !ishomo && homo ) {
                   10165:       /* dehomogenization */
                   10166:       for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   10167:       nd_init_ord(ord);
                   10168:       nd_setup_parameters(nvar,0);
                   10169:     }
                   10170:     cand = ndv_reducebase(cand,perm);
                   10171:     cand = ndv_reduceall(-2,cand);
                   10172:     cbpe = nd_bpe;
                   10173:     get_eg(&eg0);
                   10174:     if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   10175:       /* gbcheck : cand is a GB of Id(cand) ? */
                   10176:       retn = nd_f4(-2,0,0);
                   10177:     }
                   10178:   if ( !retn ) {
                   10179:       /* failure */
                   10180:       *rp = 0; return;
                   10181:     }
                   10182:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   10183:     if ( DP_Print )
1.5       noro     10184:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     10185:     /* dp->p */
                   10186:     nd_bpe = cbpe;
                   10187:     nd_setup_parameters(nd_nvar,0);
                   10188:     for ( r = cand; r; r = NEXT(r) ) {
                   10189:       if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
                   10190:       else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
                   10191:     }
                   10192:     MKLIST(*rp,cand);
                   10193: }
                   10194:
                   10195: NODE nd_f4_lf_trace_main(int m,int **indp)
                   10196: {
                   10197:     int i,nh,stat,index;
                   10198:     NODE r,rm,g;
                   10199:     ND_pairs d,l,l0,t;
                   10200:     ND spol,red;
                   10201:     NDV nf,redv,nfqv,nfv;
                   10202:     NM s0,s;
                   10203:     NODE rp0,srp0,nflist,nflist_lf;
                   10204:     int nsp,nred,col,rank,len,k,j,a;
                   10205:     UINT c;
                   10206:     UINT **spmat;
                   10207:     UINT *s0vect,*svect,*p,*v;
                   10208:     int *colstat;
                   10209:     IndArray *imat;
                   10210:     int *rhead;
                   10211:     int spcol,sprow;
                   10212:     int sugar;
                   10213:     PGeoBucket bucket;
                   10214:     struct oEGT eg0,eg1,eg_f4;
                   10215:
                   10216:     g = 0; d = 0;
                   10217:     for ( i = 0; i < nd_psn; i++ ) {
                   10218:         d = update_pairs(d,g,i,0);
                   10219:         g = update_base(g,i);
                   10220:     }
                   10221:     while ( d ) {
                   10222:         get_eg(&eg0);
                   10223:         l = nd_minsugarp(d,&d);
                   10224:         sugar = SG(l);
                   10225:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   10226:         bucket = create_pbucket();
                   10227:         stat = nd_sp_f4(m,0,l,bucket);
                   10228:         if ( !stat ) {
                   10229:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10230:             NEXT(t) = d; d = l;
                   10231:             d = nd_reconstruct(1,d);
                   10232:             continue;
                   10233:         }
                   10234:         if ( bucket->m < 0 ) continue;
                   10235:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   10236:         if ( !col ) {
                   10237:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10238:             NEXT(t) = d; d = l;
                   10239:             d = nd_reconstruct(1,d);
                   10240:             continue;
                   10241:         }
                   10242:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   10243:         if ( DP_Print )
1.5       noro     10244:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1       noro     10245:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   10246:         if ( !l0 ) continue;
                   10247:         l = l0;
                   10248:
                   10249:         /* over LF */
                   10250:         bucket = create_pbucket();
                   10251:         stat = nd_sp_f4(-2,1,l,bucket);
                   10252:         if ( !stat ) {
                   10253:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10254:             NEXT(t) = d; d = l;
                   10255:             d = nd_reconstruct(1,d);
                   10256:             continue;
                   10257:         }
                   10258:         if ( bucket->m < 0 ) continue;
                   10259:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   10260:         if ( !col ) {
                   10261:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10262:             NEXT(t) = d; d = l;
                   10263:             d = nd_reconstruct(1,d);
                   10264:             continue;
                   10265:         }
                   10266:         nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
                   10267:         /* adding new bases */
                   10268:         for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
                   10269:             nfv = (NDV)BDY(rm);
                   10270:             nfqv = (NDV)BDY(r);
                   10271:             if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
                   10272:             ndv_removecont(m,nfv);
                   10273:             ndv_removecont(-2,nfqv);
1.24      noro     10274:             nh = ndv_newps(-2,nfv,nfqv);
1.1       noro     10275:             d = update_pairs(d,g,nh,0);
                   10276:             g = update_base(g,nh);
                   10277:         }
                   10278:         if ( r || rm ) return 0;
                   10279:     }
                   10280:   conv_ilist(nd_demand,1,g,indp);
                   10281:     return g;
                   10282: }
                   10283:
1.7       noro     10284: #if SIZEOF_LONG==8
                   10285:
                   10286: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   10287: {
                   10288:     int j,k,len;
                   10289:     UINT *p;
                   10290:     UINT c;
                   10291:     NDV r;
                   10292:     NMV mr0,mr;
                   10293:
                   10294:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   10295:     if ( !len ) return 0;
                   10296:     else {
                   10297:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   10298: #if 0
                   10299:         ndv_alloc += nmv_adv*len;
                   10300: #endif
                   10301:         mr = mr0;
                   10302:         p = s0vect;
                   10303:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   10304:             if ( !rhead[j] ) {
                   10305:                 if ( (c = (UINT)vect[k++]) != 0 ) {
                   10306:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   10307:                 }
                   10308:             }
                   10309:         MKNDV(nd_nvar,mr0,len,r);
                   10310:         return r;
                   10311:     }
                   10312: }
                   10313:
1.28      noro     10314: NDV vect64_to_ndv_s(mp_limb_t *vect,int col,UINT *s0vect)
                   10315: {
                   10316:     int j,k,len;
                   10317:     UINT *p;
                   10318:     UINT c;
                   10319:     NDV r;
                   10320:     NMV mr0,mr;
                   10321:
                   10322:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   10323:     if ( !len ) return 0;
                   10324:     else {
                   10325:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   10326:         mr = mr0;
                   10327:         p = s0vect;
                   10328:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   10329:           if ( (c = (UINT)vect[k++]) != 0 ) {
                   10330:             ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   10331:           }
                   10332:         MKNDV(nd_nvar,mr0,len,r);
                   10333:         return r;
                   10334:     }
                   10335: }
                   10336:
1.7       noro     10337: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
                   10338: {
                   10339:     NM m;
1.11      noro     10340:     UINT *t,*s,*u;
                   10341:     int i,st,ed,md,prev,c;
1.7       noro     10342:
                   10343:     for ( i = 0; i < n; i++ ) r[i] = 0;
1.11      noro     10344:     prev = 0;
                   10345:     for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
                   10346:       t = DL(m);
                   10347:       st = prev;
                   10348:       ed = n;
                   10349:       while ( ed > st ) {
                   10350:         md = (st+ed)/2;
                   10351:         u = s0+md*nd_wpd;
                   10352:         c = DL_COMPARE(u,t);
                   10353:         if ( c == 0 ) break;
                   10354:         else if ( c > 0 ) st = md;
                   10355:         else ed = md;
                   10356:       }
                   10357:       r[md] = (mp_limb_t)CM(m);
                   10358:       prev = md;
1.7       noro     10359:     }
                   10360:     for ( i = 0; !r[i]; i++ );
                   10361:     return i;
                   10362: }
                   10363:
                   10364: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
                   10365:
1.28      noro     10366: int ndv_reduce_vect64(int m,mp_limb_t *svect,mp_limb_t *cvect,int col,IndArray *imat,NM_ind_pair *rp0,int nred,SIG sig)
1.7       noro     10367: {
                   10368:     int i,j,k,len,pos,prev;
                   10369:     mp_limb_t a,c,c1,c2;
                   10370:     IndArray ivect;
                   10371:     unsigned char *ivc;
                   10372:     unsigned short *ivs;
                   10373:     unsigned int *ivi;
                   10374:     NDV redv;
                   10375:     NMV mr;
                   10376:     NODE rp;
                   10377:     int maxrs;
                   10378:
                   10379:     for ( i = 0; i < col; i++ ) cvect[i] = 0;
                   10380:     maxrs = 0;
                   10381:     for ( i = 0; i < nred; i++ ) {
                   10382:         ivect = imat[i];
                   10383:         k = ivect->head;
                   10384:         a = svect[k]; c = cvect[k];
                   10385:         MOD128(a,c,m);
                   10386:         svect[k] = a; cvect[k] = 0;
1.28      noro     10387:         if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.11      noro     10388:             Nf4_red++;
1.7       noro     10389:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   10390:             c = m-c; redv = nd_ps[rp0[i]->index];
                   10391:             len = LEN(redv); mr = BDY(redv);
                   10392:             svect[k] = 0; prev = k;
                   10393:             switch ( ivect->width ) {
                   10394:                 case 1:
                   10395:                     ivc = ivect->index.c;
                   10396:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   10397:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12      noro     10398:                         c2 = svect[pos]+c1*c;
                   10399:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   10400:                         svect[pos] = c2;
1.7       noro     10401:                     }
                   10402:                     break;
                   10403:                 case 2:
                   10404:                     ivs = ivect->index.s;
                   10405:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   10406:                         pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12      noro     10407:                         c2 = svect[pos]+c1*c;
                   10408:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   10409:                         svect[pos] = c2;
1.7       noro     10410:                     }
                   10411:                     break;
                   10412:                 case 4:
                   10413:                     ivi = ivect->index.i;
                   10414:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   10415:                         pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12      noro     10416:                         c2 = svect[pos]+c1*c;
                   10417:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   10418:                         svect[pos] = c2;
1.7       noro     10419:                     }
                   10420:                     break;
                   10421:             }
                   10422:         }
                   10423:     }
                   10424:     for ( i = 0; i < col; i++ ) {
                   10425:       a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
                   10426:     }
                   10427:     return maxrs;
                   10428: }
                   10429:
                   10430: /* for Fp, 2^15=<p<2^29 */
                   10431:
                   10432: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   10433:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   10434: {
                   10435:     int spcol,sprow,a;
                   10436:     int i,j,k,l,rank;
                   10437:     NODE r0,r;
                   10438:     ND_pairs sp;
                   10439:     ND spol;
                   10440:     mp_limb_t **spmat;
                   10441:     mp_limb_t *svect,*cvect;
                   10442:     mp_limb_t *v;
                   10443:     int *colstat;
                   10444:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   10445:     int maxrs;
                   10446:     int *spsugar;
                   10447:     ND_pairs *spactive;
                   10448:
                   10449:     spcol = col-nred;
                   10450:     get_eg(&eg0);
                   10451:     /* elimination (1st step) */
                   10452:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   10453:     svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   10454:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   10455:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   10456:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   10457:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   10458:         nd_sp(m,0,sp,&spol);
                   10459:         if ( !spol ) continue;
                   10460:         nd_to_vect64(m,s0vect,col,spol,svect);
1.28      noro     10461:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,0);
1.7       noro     10462:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   10463:         if ( i < col ) {
                   10464:             spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
                   10465:             for ( j = k = 0; j < col; j++ )
                   10466:                 if ( !rhead[j] ) v[k++] = (UINT)svect[j];
                   10467:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   10468:             if ( nz )
                   10469:             spactive[sprow] = sp;
                   10470:             sprow++;
                   10471:         }
                   10472:         nd_free(spol);
                   10473:     }
1.12      noro     10474:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7       noro     10475:     if ( DP_Print ) {
                   10476:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   10477:         fflush(asir_out);
                   10478:     }
                   10479:     /* free index arrays */
                   10480:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   10481:
                   10482:     /* elimination (2nd step) */
                   10483:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   10484:     rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   10485:     r0 = 0;
                   10486:     for ( i = 0; i < rank; i++ ) {
                   10487:         NEXTNODE(r0,r); BDY(r) =
                   10488:           (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   10489:         SG((NDV)BDY(r)) = spsugar[i];
                   10490:         GCFREE(spmat[i]);
                   10491:     }
                   10492:     if ( r0 ) NEXT(r) = 0;
                   10493:
                   10494:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12      noro     10495:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7       noro     10496:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   10497:     if ( DP_Print ) {
                   10498:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   10499:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   10500:             nsp,nred,sprow,spcol,rank);
                   10501:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   10502:     }
                   10503:     if ( nz ) {
                   10504:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   10505:         if ( rank > 0 ) {
                   10506:             NEXT(spactive[rank-1]) = 0;
                   10507:             *nz = spactive[0];
                   10508:         } else
                   10509:             *nz = 0;
                   10510:     }
                   10511:     return r0;
                   10512: }
                   10513:
                   10514: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   10515: {
                   10516:   int i,j,k,l,rank,s;
                   10517:   mp_limb_t inv;
                   10518:   mp_limb_t a;
                   10519:   UINT c;
                   10520:   mp_limb_t *t,*pivot,*pk;
                   10521:   UINT *ck;
                   10522:   UINT **cmat;
                   10523:   UINT *ct;
                   10524:   ND_pairs pair;
                   10525:
                   10526:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   10527:   for ( i = 0; i < row; i++ ) {
                   10528:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   10529:     bzero(cmat[i],col*sizeof(UINT));
                   10530:   }
                   10531:
                   10532:   for ( rank = 0, j = 0; j < col; j++ ) {
                   10533:     for ( i = rank; i < row; i++ ) {
                   10534:       a = mat[i][j]; c = cmat[i][j];
                   10535:       MOD128(a,c,md);
                   10536:       mat[i][j] = a; cmat[i][j] = 0;
                   10537:     }
                   10538:     for ( i = rank; i < row; i++ )
                   10539:       if ( mat[i][j] )
                   10540:         break;
                   10541:     if ( i == row ) {
                   10542:       colstat[j] = 0;
                   10543:       continue;
                   10544:     } else
                   10545:       colstat[j] = 1;
                   10546:     if ( i != rank ) {
                   10547:       t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   10548:       ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
                   10549:       s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   10550:       if ( spactive ) {
                   10551:         pair = spactive[i]; spactive[i] = spactive[rank];
                   10552:         spactive[rank] = pair;
                   10553:       }
                   10554:     }
                   10555:     /* column j is normalized */
                   10556:     s = sugar[rank];
                   10557:     inv = invm((UINT)mat[rank][j],md);
                   10558:     /* normalize pivot row */
                   10559:     for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
                   10560:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   10561:     }
                   10562:     for ( i = rank+1; i < row; i++ ) {
                   10563:       if ( (a = mat[i][j]) != 0 ) {
                   10564:         sugar[i] = MAX(sugar[i],s);
                   10565:         red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11      noro     10566:         Nf4_red++;
1.7       noro     10567:       }
                   10568:     }
                   10569:     rank++;
                   10570:   }
                   10571:   for ( j = col-1, l = rank-1; j >= 0; j-- )
                   10572:     if ( colstat[j] ) {
                   10573:       for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
                   10574:         a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
                   10575:       }
                   10576:       s = sugar[l];
                   10577:       for ( i = 0; i < l; i++ ) {
                   10578:         a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
                   10579:         if ( a ) {
                   10580:           sugar[i] = MAX(sugar[i],s);
                   10581:           red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11      noro     10582:           Nf4_red++;
1.7       noro     10583:         }
                   10584:       }
                   10585:       l--;
                   10586:     }
                   10587:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   10588:   GCFREE(cmat);
                   10589:   return rank;
                   10590: }
                   10591: #endif
                   10592:
1.28      noro     10593: int nd_gauss_elim_mod64_s(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat,SIG *sig)
                   10594: {
                   10595:   int i,j,k,l,rank,s,imin;
                   10596:   mp_limb_t inv;
                   10597:   mp_limb_t a;
                   10598:   UINT c;
                   10599:   mp_limb_t *t,*pivot,*pk;
                   10600:   UINT *ck;
                   10601:   UINT **cmat;
                   10602:   UINT *ct;
                   10603:   ND_pairs pair;
                   10604:   SIG sg;
1.31      noro     10605:   int *used;
1.28      noro     10606:
1.31      noro     10607:   used = (int *)MALLOC(row*sizeof(int));
1.28      noro     10608:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   10609:   for ( i = 0; i < row; i++ ) {
                   10610:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   10611:     bzero(cmat[i],col*sizeof(UINT));
                   10612:   }
                   10613:
1.31      noro     10614:   for ( j = 0; j < col; j++ ) {
                   10615:     for ( i = 0; i < row; i++ ) {
1.28      noro     10616:       a = mat[i][j]; c = cmat[i][j];
                   10617:       MOD128(a,c,md);
                   10618:       mat[i][j] = a; cmat[i][j] = 0;
                   10619:     }
1.31      noro     10620:     for ( i = 0; i < row; i++ )
                   10621:       if ( !used[i] && mat[i][j] ) break;
                   10622:     if ( i == row ) {
1.28      noro     10623:       colstat[j] = 0;
                   10624:       continue;
1.31      noro     10625:     } else {
1.28      noro     10626:       colstat[j] = 1;
1.31      noro     10627:       used[i] = 1;
1.28      noro     10628:     }
                   10629:     /* column j is normalized */
1.31      noro     10630:     s = sugar[i];
                   10631:     inv = invm((UINT)mat[i][j],md);
1.28      noro     10632:     /* normalize pivot row */
1.31      noro     10633:     for ( k = j, pk = mat[i]+j, ck = cmat[i]+j; k < col; k++, pk++, ck++ ) {
1.28      noro     10634:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   10635:     }
1.31      noro     10636:     for ( k = i+1; k < row; k++ ) {
                   10637:       if ( (a = mat[k][j]) != 0 ) {
                   10638:         sugar[k] = MAX(sugar[k],s);
                   10639:         red_by_vect64(md,mat[k]+j,cmat[k]+j,mat[i]+j,(int)(md-a),col-j);
1.28      noro     10640:         Nf4_red++;
                   10641:       }
                   10642:     }
                   10643:   }
1.31      noro     10644:   rank = 0;
                   10645:   for ( i = 0; i < row; i++ ) {
                   10646:     for ( j = 0; j < col; j++ )
                   10647:       if ( mat[i][j] ) break;
                   10648:     if ( j == col ) sugar[i] = -1;
                   10649:     else rank++;
                   10650:   }
1.28      noro     10651:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   10652:   GCFREE(cmat);
                   10653:   return rank;
                   10654: }
                   10655:
                   10656: NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   10657:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
                   10658: {
                   10659:     int spcol,sprow,a;
                   10660:     int i,j,k,l,rank;
                   10661:     NODE r0,r;
                   10662:     ND_pairs sp;
                   10663:     ND spol;
                   10664:     mp_limb_t **spmat;
                   10665:     mp_limb_t *svect,*cvect;
                   10666:     mp_limb_t *v;
                   10667:     int *colstat;
                   10668:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   10669:     int maxrs;
                   10670:     int *spsugar;
                   10671:     ND_pairs *spactive;
                   10672:     SIG *spsig;
                   10673:
                   10674:     get_eg(&eg0);
                   10675:     /* elimination (1st step) */
                   10676:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   10677:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   10678:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   10679:     spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
                   10680:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   10681:         nd_sp(m,0,sp,&spol);
                   10682:         if ( !spol ) {
1.29      noro     10683:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     10684:           continue;
                   10685:         }
                   10686:         svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   10687:         nd_to_vect64(m,s0vect,col,spol,svect);
                   10688:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,spol->sig);
                   10689:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   10690:         if ( i < col ) {
                   10691:             spmat[sprow] = svect;
                   10692:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   10693:             spsig[sprow] = sp->sig;
                   10694:             sprow++;
                   10695:         } else {
1.29      noro     10696:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     10697:         }
                   10698:         nd_free(spol);
                   10699:     }
                   10700:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
                   10701:     if ( DP_Print ) {
                   10702:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   10703:         fflush(asir_out);
                   10704:     }
                   10705:     /* free index arrays */
                   10706:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   10707:
                   10708:     /* elimination (2nd step) */
                   10709:     colstat = (int *)MALLOC(col*sizeof(int));
                   10710:     rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
                   10711:     r0 = 0;
1.31      noro     10712:     for ( i = 0; i < sprow; i++ ) {
                   10713:         if ( spsugar[i] >= 0 ) {
                   10714:           NEXTNODE(r0,r);
                   10715:           BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
                   10716:           SG((NDV)BDY(r)) = spsugar[i];
                   10717:           ((NDV)BDY(r))->sig = spsig[i];
                   10718:         } else
                   10719:           syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
1.28      noro     10720:         GCFREE(spmat[i]);
                   10721:     }
                   10722:     if ( r0 ) NEXT(r) = 0;
                   10723:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
                   10724:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   10725:     if ( DP_Print ) {
                   10726:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   10727:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   10728:             nsp,nred,sprow,col,rank);
                   10729:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   10730:     }
                   10731:     return r0;
                   10732: }
                   10733:
                   10734: NODE nd_f4_red_s(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,NODE *syzlistp)
                   10735: {
                   10736:   IndArray *imat;
                   10737:   int nsp,nred,i,start;
                   10738:   int *rhead;
                   10739:   NODE r0,rp;
                   10740:   ND_pairs sp;
                   10741:   NM_ind_pair *rvect;
                   10742:   UINT *s;
                   10743:   int *s0hash;
                   10744:   struct oEGT eg0,eg1,eg_conv;
                   10745:
                   10746:   for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   10747:   nred = length(rp0);
                   10748:   imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   10749:   rhead = (int *)MALLOC(col*sizeof(int));
                   10750:   for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   10751:
                   10752:   /* construction of index arrays */
                   10753:   get_eg(&eg0);
                   10754:   if ( DP_Print ) {
                   10755:     fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   10756:     fflush(asir_out);
                   10757:   }
                   10758:   rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   10759:   for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   10760:     rvect[i] = (NM_ind_pair)BDY(rp);
                   10761:     imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
                   10762:     rhead[imat[i]->head] = 1;
                   10763:     start = imat[i]->head;
                   10764:   }
                   10765:   get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
                   10766:   if ( DP_Print ) {
                   10767:     fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   10768:     fflush(asir_out);
                   10769:   }
                   10770:   if ( m > 0 )
                   10771:     r0 = nd_f4_red_mod64_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
                   10772:   else
                   10773: //    r0 = nd_f4_red_q_main_s(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   10774:     error("nd_f4_red_q_main_s : not implemented yet");
                   10775:   return r0;
                   10776: }
                   10777:
                   10778: INLINE int ndl_find_reducer_minsig(UINT *dg)
                   10779: {
                   10780:   RHist r;
                   10781:   int i,singular,ret,d,k,imin;
                   10782:   SIG t;
                   10783:   static int wpd,nvar;
                   10784:   static SIG quo,quomin;
                   10785:   static UINT *tmp;
                   10786:
                   10787:   if ( !quo || nvar != nd_nvar ) { NEWSIG(quo); NEWSIG(quomin); }
                   10788:   if ( wpd != nd_wpd ) {
                   10789:     wpd = nd_wpd;
                   10790:     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
                   10791:   }
                   10792: #if 0
                   10793:   d = ndl_hash_value(dg);
                   10794:   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   10795:     if ( ndl_equal(dg,DL(r)) ) {
                   10796:       return r->index;
                   10797:     }
                   10798:   }
                   10799: #endif
                   10800:   imin = -1;
                   10801:   for ( i = 0; i < nd_psn; i++ ) {
                   10802:     r = nd_psh[i];
                   10803:     if ( ndl_reducible(dg,DL(r)) ) {
                   10804:       ndl_sub(dg,DL(r),tmp);
                   10805:       _ndltodl(tmp,DL(quo));
                   10806:       _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
                   10807:       quo->pos = nd_psh[i]->sig->pos;
                   10808:       if ( imin < 0 || comp_sig(quomin,quo) > 0 ) {
                   10809:         t = quo; quo = quomin; quomin = t;
                   10810:         imin = i;
                   10811:       }
                   10812:     }
                   10813:   }
                   10814:   if ( imin == -1 ) return nd_psn;
                   10815:   else {
                   10816: #if 0
                   10817:     nd_append_red(dg,i);
                   10818: #endif
                   10819:     return imin;
                   10820:   }
                   10821: }
                   10822:
                   10823: int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   10824: {
                   10825:   NODE rp0,rp;
                   10826:   NM mul,head,s0,s;
                   10827:   int index,col,i,sugar;
                   10828:   RHist h;
                   10829:   UINT *s0v,*p;
                   10830:   NM_ind_pair pair;
                   10831:   ND red;
                   10832:   NDV *ps;
                   10833:   SIG sig;
                   10834:
                   10835:   s0 = 0; rp0 = 0; col = 0;
                   10836:   if ( nd_demand )
                   10837:     ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   10838:   else
                   10839:     ps = trace?nd_ps_trace:nd_ps;
                   10840:   while ( 1 ) {
                   10841:     head = remove_head_pbucket_symbolic(bucket);
                   10842:     if ( !head ) break;
                   10843:     if ( !s0 ) s0 = head;
                   10844:     else NEXT(s) = head;
                   10845:     s = head;
                   10846:     index = ndl_find_reducer_minsig(DL(head));
                   10847:     if ( index >= 0 && index < nd_psn ) {
                   10848:       h = nd_psh[index];
                   10849:       NEWNM(mul);
                   10850:       ndl_sub(DL(head),DL(h),DL(mul));
                   10851:       if ( ndl_check_bound2(index,DL(mul)) )
                   10852:         return 0;
                   10853:       sugar = TD(DL(mul))+SG(ps[index]);
                   10854:       NEWSIG(sig);
                   10855:       _ndltodl(DL(mul),DL(sig));
                   10856:       _addtodl(nd_nvar,DL(nd_psh[index]->sig),DL(sig));
                   10857:       sig->pos = nd_psh[index]->sig->pos;
                   10858:       MKNM_ind_pair(pair,mul,index,sugar,sig);
                   10859:       red = ndv_mul_nm_symbolic(mul,ps[index]);
                   10860:       add_pbucket_symbolic(bucket,nd_remove_head(red));
                   10861:       NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   10862:     }
                   10863:     col++;
                   10864:   }
                   10865:   if ( rp0 ) NEXT(rp) = 0;
                   10866:   NEXT(s) = 0;
                   10867:   s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   10868:   for ( i = 0, p = s0v, s = s0; i < col;
                   10869:     i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   10870:   *s0vect = s0v;
                   10871:   *r = rp0;
                   10872:
                   10873:   return col;
                   10874: }
                   10875:
                   10876: NODE nd_sba_f4(int m,int **indp)
                   10877: {
1.34    ! noro     10878:   int i,nh,stat,index,f4red,f4step;
1.33      noro     10879:   int col,rank,len,k,j,a,sugar,nbase,psugar,ms;
                   10880:   NODE r,g,rp0,nflist;
                   10881:   ND_pairs d,l,t;
                   10882:   ND h,nf;
                   10883:   NDV nfv;
                   10884:   union oNDC hc;
                   10885:   UINT *s0vect;
1.28      noro     10886:   UINT c;
                   10887:   PGeoBucket bucket;
1.33      noro     10888:   NODE *syzlist;
                   10889:   SIG sig;
1.28      noro     10890:   struct oEGT eg0,eg1,eg_f4;
1.33      noro     10891:   struct oEGT eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.28      noro     10892:
                   10893:   Nf4_red=0;
1.30      noro     10894:   d = 0;
1.29      noro     10895:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.28      noro     10896:   for ( i = 0; i < nd_psn; i++ ) {
1.30      noro     10897:     d = update_pairs_s(d,i,syzlist);
1.28      noro     10898:   }
1.30      noro     10899:   nd_nbase = nd_psn;
1.28      noro     10900:   f4red = 1;
1.33      noro     10901:   psugar = 0;
1.34    ! noro     10902:   f4step = 0;
1.28      noro     10903:   while ( d ) {
1.33      noro     10904:     for ( t = d, ms = SG(d); t; t = NEXT(t) )
                   10905:       if ( SG(t) < ms ) ms = SG(t);
1.34    ! noro     10906:     if ( ms == psugar && f4step >= 2 ) {
1.33      noro     10907: again:
                   10908:       l = d; d = d->next;
                   10909:       if ( small_lcm(l) ) {
                   10910:         if ( DP_Print ) fprintf(asir_out,"M");
                   10911:         continue;
                   10912:       }
                   10913:       sig = l->sig;
                   10914:       stat = nd_sp(m,0,l,&h);
                   10915:       if ( !stat ) {
                   10916:         NEXT(l) = d; d = l;
                   10917:         d = nd_reconstruct(0,d);
                   10918:         goto again;
                   10919:       }
                   10920:   get_eg(&eg1);
                   10921:   #if USE_GEOBUCKET
                   10922:       stat = m?nd_nf_pbucket_s(m,h,nd_ps,!Top,&nf):nd_nf_s(m,0,h,nd_ps,!Top,&nf);
                   10923:   #else
                   10924:       stat = nd_nf_s(m,0,h,nd_ps,!Top,&nf);
                   10925:   #endif
                   10926:   get_eg(&eg2);
                   10927:       if ( !stat ) {
                   10928:         NEXT(l) = d; d = l;
                   10929:         d = nd_reconstruct(0,d);
                   10930:         goto again;
                   10931:       } else if ( stat == -1 ) {
                   10932:         if ( DP_Print ) { printf("S"); fflush(stdout); }
                   10933:         FREENDP(l);
                   10934:       } else if ( nf ) {
                   10935:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   10936:         add_eg(&eg_nf,&eg1,&eg2);
                   10937:         hc = HCU(nf);
                   10938:         nd_removecont(m,nf);
                   10939:         nfv = ndtondv(m,nf); nd_free(nf);
                   10940:         nh = ndv_newps(m,nfv,0);
                   10941:
1.30      noro     10942:         d = update_pairs_s(d,nh,syzlist);
                   10943:         nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.33      noro     10944:         FREENDP(l);
1.28      noro     10945:       } else {
1.33      noro     10946:         add_eg(&eg_nfzero,&eg1,&eg2);
                   10947:        // syzygy
                   10948:   get_eg(&eg1);
                   10949:         d = remove_spair_s(d,sig);
                   10950:   get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29      noro     10951:         syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.33      noro     10952:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   10953:         FREENDP(l);
                   10954:       }
                   10955:     } else {
1.34    ! noro     10956:       if ( ms != psugar ) f4step = 1;
        !          10957:       else f4step++;
1.33      noro     10958: again2:
                   10959:       psugar = ms;
                   10960:       l = nd_minsugarp_s(d,&d);
                   10961:       sugar = nd_sugarweight?d->sugar2:SG(d);
                   10962:       bucket = create_pbucket();
                   10963:       stat = nd_sp_f4(m,0,l,bucket);
                   10964:       if ( !stat ) {
                   10965:         for ( t = l; NEXT(t); t = NEXT(t) );
                   10966:         NEXT(t) = d; d = l;
                   10967:         d = nd_reconstruct(0,d);
                   10968:         goto again2;
                   10969:       }
                   10970:       if ( bucket->m < 0 ) continue;
                   10971:       col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
                   10972:       if ( !col ) {
                   10973:         for ( t = l; NEXT(t); t = NEXT(t) );
                   10974:           NEXT(t) = d; d = l;
                   10975:         d = nd_reconstruct(0,d);
                   10976:         goto again2;
                   10977:       }
1.34    ! noro     10978:       if ( DP_Print ) fprintf(asir_out,"\nsugar=%d,",psugar);
1.33      noro     10979:       nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
                   10980:       /* adding new bases */
                   10981:       for ( r = nflist; r; r = NEXT(r) ) {
                   10982:         nfv = (NDV)BDY(r);
                   10983:         ndv_removecont(m,nfv);
                   10984:         nh = ndv_newps(m,nfv,0);
                   10985:         d = update_pairs_s(d,nh,syzlist);
                   10986:         nd_sba_pos[nfv->sig->pos] = append_one(nd_sba_pos[nfv->sig->pos],nh);
                   10987:       }
                   10988:       for ( i = 0; i < nd_nbase; i++ )
                   10989:         for ( r = syzlist[i]; r; r = NEXT(r) )
                   10990:             d = remove_spair_s(d,(SIG)BDY(r));
                   10991:       d = remove_large_lcm(d);
                   10992:       if ( DP_Print ) {
1.34    ! noro     10993:         fprintf(asir_out,"f4red=%d,gblen=%d",f4red,nd_psn); fflush(asir_out);
1.33      noro     10994:       }
                   10995:       f4red++;
1.28      noro     10996:     }
                   10997:   }
                   10998:   if ( DP_Print ) {
1.34    ! noro     10999:     fprintf(asir_out,"\nnumber of red=%d,",Nf4_red);
1.28      noro     11000:   }
1.30      noro     11001:   g = conv_ilist_s(nd_demand,0,indp);
1.28      noro     11002:   return g;
                   11003: }

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