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

1.35    ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.34 2020/07/10 08:09:05 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.35    ! noro       21: int Nsyz,Nsamesig;
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:
1.35    ! noro     1226: // assuming increasing order wrt signature
        !          1227:
1.24      noro     1228: INLINE int ndl_find_reducer_s(UINT *dg,SIG sig)
                   1229: {
                   1230:   RHist r;
1.27      noro     1231:   int i,singular,ret,d,k;
1.26      noro     1232:   static int wpd,nvar;
1.24      noro     1233:   static SIG quo;
                   1234:   static UINT *tmp;
                   1235:
1.26      noro     1236:   if ( !quo || nvar != nd_nvar ) NEWSIG(quo);
                   1237:   if ( wpd != nd_wpd ) {
1.24      noro     1238:     wpd = nd_wpd;
                   1239:     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
                   1240:   }
1.27      noro     1241:   d = ndl_hash_value(dg);
1.35    ! noro     1242: #if 1
1.27      noro     1243:   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   1244:     if ( ndl_equal(dg,DL(r)) ) {
                   1245:       return r->index;
                   1246:     }
                   1247:   }
1.35    ! noro     1248: #endif
1.24      noro     1249:   singular = 0;
                   1250:   for ( i = 0; i < nd_psn; i++ ) {
                   1251:     r = nd_psh[i];
                   1252:     if ( ndl_reducible(dg,DL(r)) ) {
1.27      noro     1253:       ndl_sub(dg,DL(r),tmp);
1.24      noro     1254:       _ndltodl(tmp,DL(quo));
                   1255:       _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
                   1256:       quo->pos = nd_psh[i]->sig->pos;
                   1257:       ret = comp_sig(sig,quo);
                   1258:       if ( ret > 0 ) { singular = 0; break; }
1.29      noro     1259:       if ( ret == 0 ) { fprintf(asir_out,"s"); fflush(asir_out); singular = 1; }
1.24      noro     1260:     }
                   1261:   }
                   1262:   if ( singular ) return -1;
1.27      noro     1263:   else if ( i < nd_psn )
                   1264:     nd_append_red(dg,i);
                   1265:   return i;
1.24      noro     1266: }
                   1267:
1.1       noro     1268: ND nd_merge(ND p1,ND p2)
                   1269: {
                   1270:     int n,c;
                   1271:     int t,can,td1,td2;
                   1272:     ND r;
                   1273:     NM m1,m2,mr0,mr,s;
                   1274:
                   1275:     if ( !p1 ) return p2;
                   1276:     else if ( !p2 ) return p1;
                   1277:     else {
                   1278:         can = 0;
                   1279:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1280:             c = DL_COMPARE(DL(m1),DL(m2));
                   1281:             switch ( c ) {
                   1282:                 case 0:
                   1283:                     s = m1; m1 = NEXT(m1);
                   1284:                     can++; NEXTNM2(mr0,mr,s);
                   1285:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1286:                     break;
                   1287:                 case 1:
                   1288:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1289:                     break;
                   1290:                 case -1:
                   1291:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1292:                     break;
                   1293:             }
                   1294:         }
                   1295:         if ( !mr0 )
                   1296:             if ( m1 ) mr0 = m1;
                   1297:             else if ( m2 ) mr0 = m2;
                   1298:             else return 0;
                   1299:         else if ( m1 ) NEXT(mr) = m1;
                   1300:         else if ( m2 ) NEXT(mr) = m2;
                   1301:         else NEXT(mr) = 0;
                   1302:         BDY(p1) = mr0;
                   1303:         SG(p1) = MAX(SG(p1),SG(p2));
                   1304:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1305:         FREEND(p2);
                   1306:         return p1;
                   1307:     }
                   1308: }
                   1309:
                   1310: ND nd_add(int mod,ND p1,ND p2)
                   1311: {
                   1312:     int n,c;
                   1313:     int t,can,td1,td2;
                   1314:     ND r;
                   1315:     NM m1,m2,mr0,mr,s;
                   1316:
1.11      noro     1317:     Nnd_add++;
1.1       noro     1318:     if ( !p1 ) return p2;
                   1319:     else if ( !p2 ) return p1;
                   1320:     else if ( mod == -1 ) return nd_add_sf(p1,p2);
                   1321:     else if ( mod == -2 ) return nd_add_lf(p1,p2);
                   1322:     else if ( !mod ) return nd_add_q(p1,p2);
                   1323:     else {
                   1324:         can = 0;
                   1325:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1326:             c = DL_COMPARE(DL(m1),DL(m2));
                   1327:             switch ( c ) {
                   1328:                 case 0:
                   1329:                     t = ((CM(m1))+(CM(m2))) - mod;
                   1330:                     if ( t < 0 ) t += mod;
                   1331:                     s = m1; m1 = NEXT(m1);
                   1332:                     if ( t ) {
                   1333:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1334:                     } else {
                   1335:                         can += 2; FREENM(s);
                   1336:                     }
                   1337:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1338:                     break;
                   1339:                 case 1:
                   1340:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1341:                     break;
                   1342:                 case -1:
                   1343:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1344:                     break;
                   1345:             }
                   1346:         }
                   1347:         if ( !mr0 )
                   1348:             if ( m1 ) mr0 = m1;
                   1349:             else if ( m2 ) mr0 = m2;
                   1350:             else return 0;
                   1351:         else if ( m1 ) NEXT(mr) = m1;
                   1352:         else if ( m2 ) NEXT(mr) = m2;
                   1353:         else NEXT(mr) = 0;
                   1354:         BDY(p1) = mr0;
                   1355:         SG(p1) = MAX(SG(p1),SG(p2));
                   1356:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1357:         FREEND(p2);
                   1358:         return p1;
                   1359:     }
                   1360: }
                   1361:
                   1362: /* XXX on opteron, the inlined manipulation of destructive additon of
                   1363:  * two NM seems to make gcc optimizer get confused, so the part is
                   1364:  * done in a function.
                   1365:  */
                   1366:
                   1367: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
                   1368: {
                   1369:     NM s;
                   1370:     P t;
                   1371:     int can;
                   1372:
                   1373:     addp(nd_vc,CP(*m1),CP(*m2),&t);
                   1374:     s = *m1; *m1 = NEXT(*m1);
                   1375:     if ( t ) {
                   1376:         can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
                   1377:     } else {
                   1378:         can = 2; FREENM(s);
                   1379:     }
                   1380:     s = *m2; *m2 = NEXT(*m2); FREENM(s);
                   1381:     return can;
                   1382: }
                   1383:
                   1384: ND nd_add_q(ND p1,ND p2)
                   1385: {
                   1386:     int n,c,can;
                   1387:     ND r;
                   1388:     NM m1,m2,mr0,mr,s;
                   1389:     P t;
                   1390:
                   1391:     if ( !p1 ) return p2;
                   1392:     else if ( !p2 ) return p1;
                   1393:     else {
                   1394:         can = 0;
                   1395:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1396:             c = DL_COMPARE(DL(m1),DL(m2));
                   1397:             switch ( c ) {
                   1398:                 case 0:
                   1399: #if defined(__x86_64__)
                   1400:                     can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
                   1401: #else
                   1402:                     addp(nd_vc,CP(m1),CP(m2),&t);
                   1403:                     s = m1; m1 = NEXT(m1);
                   1404:                     if ( t ) {
                   1405:                         can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
                   1406:                     } else {
                   1407:                         can += 2; FREENM(s);
                   1408:                     }
                   1409:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1410: #endif
                   1411:                     break;
                   1412:                 case 1:
                   1413:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1414:                     break;
                   1415:                 case -1:
                   1416:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1417:                     break;
                   1418:             }
                   1419:         }
                   1420:         if ( !mr0 )
                   1421:             if ( m1 ) mr0 = m1;
                   1422:             else if ( m2 ) mr0 = m2;
                   1423:             else return 0;
                   1424:         else if ( m1 ) NEXT(mr) = m1;
                   1425:         else if ( m2 ) NEXT(mr) = m2;
                   1426:         else NEXT(mr) = 0;
                   1427:         BDY(p1) = mr0;
                   1428:         SG(p1) = MAX(SG(p1),SG(p2));
                   1429:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1430:         FREEND(p2);
                   1431:         return p1;
                   1432:     }
                   1433: }
                   1434:
                   1435: ND nd_add_sf(ND p1,ND p2)
                   1436: {
                   1437:     int n,c,can;
                   1438:     ND r;
                   1439:     NM m1,m2,mr0,mr,s;
                   1440:     int t;
                   1441:
                   1442:     if ( !p1 ) return p2;
                   1443:     else if ( !p2 ) return p1;
                   1444:     else {
                   1445:         can = 0;
                   1446:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1447:             c = DL_COMPARE(DL(m1),DL(m2));
                   1448:             switch ( c ) {
                   1449:                 case 0:
                   1450:                     t = _addsf(CM(m1),CM(m2));
                   1451:                     s = m1; m1 = NEXT(m1);
                   1452:                     if ( t ) {
                   1453:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1454:                     } else {
                   1455:                         can += 2; FREENM(s);
                   1456:                     }
                   1457:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1458:                     break;
                   1459:                 case 1:
                   1460:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1461:                     break;
                   1462:                 case -1:
                   1463:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1464:                     break;
                   1465:             }
                   1466:         }
                   1467:         if ( !mr0 )
                   1468:             if ( m1 ) mr0 = m1;
                   1469:             else if ( m2 ) mr0 = m2;
                   1470:             else return 0;
                   1471:         else if ( m1 ) NEXT(mr) = m1;
                   1472:         else if ( m2 ) NEXT(mr) = m2;
                   1473:         else NEXT(mr) = 0;
                   1474:         BDY(p1) = mr0;
                   1475:         SG(p1) = MAX(SG(p1),SG(p2));
                   1476:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1477:         FREEND(p2);
                   1478:         return p1;
                   1479:     }
                   1480: }
                   1481:
                   1482:
                   1483: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
                   1484: {
                   1485:     int c,c1,c2;
                   1486:     Z cg,cred,gcd,tq;
                   1487:     P cgp,credp,gcdp;
                   1488:     Obj tr,tr1;
                   1489:
                   1490:     if ( mod == -1 ) {
                   1491:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1492:         *divp = (Obj)ONE;
                   1493:     } else if ( mod == -2 ) {
                   1494:         Z inv,t;
                   1495:         divlf(ONE,HCZ(p),&inv);
                   1496:         chsgnlf(HCZ(g),&t);
                   1497:         mullf(inv,t,&CZ(mul));
                   1498:         *divp = (Obj)ONE;
                   1499:     } else if ( mod ) {
                   1500:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1501:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1502:         *divp = (Obj)ONE;
                   1503:     } else if ( nd_vc ) {
                   1504:         ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
                   1505:         divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
                   1506:         chsgnp(cgp,&CP(mul));
                   1507:         nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
                   1508:         if ( dn ) {
                   1509:             mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
                   1510:             reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
                   1511:         }
                   1512:         *divp = (Obj)credp;
                   1513:     } else {
1.6       noro     1514:         igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1515:         chsgnz(cg,&CZ(mul));
1.1       noro     1516:         nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
                   1517:         if ( dn ) {
                   1518:             mulz(dn->z,cred,&tq); dn->z = tq;
                   1519:         }
                   1520:         *divp = (Obj)cred;
                   1521:     }
                   1522:     return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
                   1523: }
                   1524:
                   1525: /* ret=1 : success, ret=0 : overflow */
1.6       noro     1526: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1       noro     1527: {
                   1528:     NM m,mrd,tail;
                   1529:     NM mul;
                   1530:     int n,sugar,psugar,sugar0,stat,index;
                   1531:     int c,c1,c2,dummy;
                   1532:     RHist h;
                   1533:     NDV p,red;
                   1534:     Q cg,cred,gcd,tq,qq;
                   1535:     Z iq;
                   1536:     DP dmul;
                   1537:     NODE node;
                   1538:     LIST hist;
                   1539:     double hmag;
                   1540:     P tp,tp1;
                   1541:     Obj tr,tr1,div;
                   1542:     union oNDC hg;
                   1543:     P cont;
                   1544:
                   1545:     if ( !g ) {
                   1546:         *rp = d;
                   1547:         return 1;
                   1548:     }
                   1549:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1550:
                   1551:     sugar0 = sugar = SG(g);
                   1552:     n = NV(g);
                   1553:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1554:     if ( d )
                   1555:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1556:     for ( ; g; ) {
                   1557:         index = ndl_find_reducer(HDL(g));
                   1558:         if ( index >= 0 ) {
                   1559:             h = nd_psh[index];
                   1560:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1561:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1562:                 nd_free(g); nd_free(d);
                   1563:                 return 0;
                   1564:             }
                   1565:             p = nd_demand ? ndv_load(index) : ps[index];
                   1566:             /* d+g -> div*(d+g)+mul*p */
1.6       noro     1567:             g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1       noro     1568:             if ( nd_gentrace ) {
                   1569:                 /* Trace=[div,index,mul,ONE] */
1.6       noro     1570:                 STOZ(index,iq);
1.1       noro     1571:                 nmtodp(mod,mul,&dmul);
                   1572:                 node = mknode(4,div,iq,dmul,ONE);
                   1573:             }
                   1574:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
                   1575:             if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
                   1576:                 hg = HCU(g);
                   1577:                 nd_removecont2(d,g);
1.6       noro     1578:                 if ( nd_gentrace ) {
1.1       noro     1579:                     /* overwrite cont : Trace=[div,index,mul,cont] */
1.6       noro     1580:                     /* exact division */
1.1       noro     1581:                     cont = ndc_div(mod,hg,HCU(g));
                   1582:                     if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
                   1583:                 }
                   1584:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1585:             }
                   1586:             MKLIST(hist,node);
                   1587:             MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
                   1588:         } else if ( !full ) {
                   1589:             *rp = g;
                   1590:             return 1;
                   1591:         } else {
                   1592:             m = BDY(g);
                   1593:             if ( NEXT(m) ) {
                   1594:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1595:             } else {
                   1596:                 FREEND(g); g = 0;
                   1597:             }
                   1598:             if ( d ) {
                   1599:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1600:             } else {
                   1601:                 MKND(n,m,1,d); tail = BDY(d);
                   1602:             }
                   1603:         }
                   1604:     }
                   1605:     if ( d ) SG(d) = sugar;
                   1606:     *rp = d;
                   1607:     return 1;
                   1608: }
                   1609:
1.24      noro     1610: // ret=1 => success
                   1611: // ret=0 => overflow
                   1612: // ret=-1 => singular top reducible
                   1613:
                   1614: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
                   1615: {
                   1616:     NM m,mrd,tail;
                   1617:     NM mul;
                   1618:     int n,sugar,psugar,sugar0,stat,index;
                   1619:     int c,c1,c2,dummy;
                   1620:     RHist h;
                   1621:     NDV p,red;
                   1622:     Q cg,cred,gcd,tq,qq;
                   1623:     Z iq;
                   1624:     DP dmul;
                   1625:     NODE node;
                   1626:     LIST hist;
                   1627:     double hmag;
                   1628:     P tp,tp1;
                   1629:     Obj tr,tr1,div;
                   1630:     union oNDC hg;
                   1631:     P cont;
                   1632:     SIG sig;
                   1633:
                   1634:     if ( !g ) {
                   1635:         *rp = d;
                   1636:         return 1;
                   1637:     }
                   1638:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1639:
                   1640:     sugar0 = sugar = SG(g);
                   1641:     n = NV(g);
                   1642:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1643:     if ( d )
                   1644:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1645:     sig = g->sig;
                   1646:     for ( ; g; ) {
                   1647:         index = ndl_find_reducer_s(HDL(g),sig);
                   1648:         if ( index >= 0 && index < nd_psn ) {
                   1649:             // reducer found
                   1650:             h = nd_psh[index];
                   1651:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1652:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1653:                 nd_free(g); nd_free(d);
                   1654:                 return 0;
                   1655:             }
                   1656:             p = ps[index];
                   1657:             /* d+g -> div*(d+g)+mul*p */
                   1658:             g = nd_reduce2(mod,d,g,p,mul,0,&div);
                   1659:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
                   1660:             if ( !mod && g && ((double)(p_mag(HCP(g))) > hmag) ) {
                   1661:                 hg = HCU(g);
                   1662:                 nd_removecont2(d,g);
                   1663:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1664:             }
                   1665:         } else if ( index == -1 ) {
                   1666:           // singular top reducible
                   1667:           return -1;
                   1668:         } else if ( !full ) {
                   1669:             *rp = g;
                   1670:             g->sig = sig;
                   1671:             return 1;
                   1672:         } else {
                   1673:             m = BDY(g);
                   1674:             if ( NEXT(m) ) {
                   1675:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1676:             } else {
                   1677:                 FREEND(g); g = 0;
                   1678:             }
                   1679:             if ( d ) {
                   1680:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1681:             } else {
                   1682:                 MKND(n,m,1,d); tail = BDY(d);
                   1683:             }
                   1684:         }
                   1685:     }
                   1686:     if ( d ) {
                   1687:       SG(d) = sugar;
                   1688:       d->sig = sig;
                   1689:     }
                   1690:     *rp = d;
                   1691:     return 1;
                   1692: }
                   1693:
1.1       noro     1694: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
                   1695: {
                   1696:     int hindex,index;
                   1697:     NDV p;
                   1698:     ND u,d,red;
                   1699:     NODE l;
                   1700:     NM mul,m,mrd,tail;
                   1701:     int sugar,psugar,n,h_reducible;
                   1702:     PGeoBucket bucket;
                   1703:     int c,c1,c2;
                   1704:     Z cg,cred,gcd,zzz;
                   1705:     RHist h;
                   1706:     double hmag,gmag;
                   1707:     int count = 0;
                   1708:     int hcount = 0;
                   1709:
                   1710:     if ( !g ) {
                   1711:         *rp = 0;
                   1712:         return 1;
                   1713:     }
                   1714:     sugar = SG(g);
                   1715:     n = NV(g);
1.6       noro     1716:     if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1       noro     1717:     bucket = create_pbucket();
                   1718:     add_pbucket(mod,bucket,g);
                   1719:     d = 0;
                   1720:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1721:     while ( 1 ) {
                   1722:         if ( mod > 0 || mod == -1 )
                   1723:           hindex = head_pbucket(mod,bucket);
                   1724:         else if ( mod == -2 )
                   1725:           hindex = head_pbucket_lf(bucket);
                   1726:         else
                   1727:           hindex = head_pbucket_q(bucket);
                   1728:         if ( hindex < 0 ) {
                   1729:             if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1730:             if ( d ) SG(d) = sugar;
                   1731:             *rp = d;
                   1732:             return 1;
                   1733:         }
                   1734:         g = bucket->body[hindex];
                   1735:         index = ndl_find_reducer(HDL(g));
                   1736:         if ( index >= 0 ) {
                   1737:             count++;
                   1738:             if ( !d ) hcount++;
                   1739:             h = nd_psh[index];
                   1740:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1741:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1742:                 nd_free(d);
                   1743:                 free_pbucket(bucket);
                   1744:                 *rp = 0;
                   1745:                 return 0;
                   1746:             }
                   1747:             p = ps[index];
                   1748:             if ( mod == -1 )
                   1749:                 CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1750:             else if ( mod == -2 ) {
                   1751:                 Z inv,t;
                   1752:                 divlf(ONE,HCZ(p),&inv);
                   1753:                 chsgnlf(HCZ(g),&t);
                   1754:                 mullf(inv,t,&CZ(mul));
                   1755:             } else if ( mod ) {
                   1756:                 c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1757:                 DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1758:             } else {
1.6       noro     1759:                 igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1760:                 chsgnz(cg,&CZ(mul));
1.1       noro     1761:                 nd_mul_c_q(d,(P)cred);
                   1762:                 mulq_pbucket(bucket,cred);
                   1763:                 g = bucket->body[hindex];
1.6       noro     1764:                 gmag = (double)p_mag((P)HCZ(g));
1.1       noro     1765:             }
                   1766:             red = ndv_mul_nm(mod,mul,p);
                   1767:             bucket->body[hindex] = nd_remove_head(g);
                   1768:             red = nd_remove_head(red);
                   1769:             add_pbucket(mod,bucket,red);
                   1770:             psugar = SG(p)+TD(DL(mul));
                   1771:             sugar = MAX(sugar,psugar);
                   1772:             if ( !mod && hmag && (gmag > hmag) ) {
                   1773:                 g = normalize_pbucket(mod,bucket);
                   1774:                 if ( !g ) {
                   1775:                     if ( d ) SG(d) = sugar;
                   1776:                     *rp = d;
                   1777:                     return 1;
                   1778:                 }
                   1779:                 nd_removecont2(d,g);
1.6       noro     1780:                 hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1       noro     1781:                 add_pbucket(mod,bucket,g);
                   1782:             }
                   1783:         } else if ( !full ) {
                   1784:             g = normalize_pbucket(mod,bucket);
                   1785:             if ( g ) SG(g) = sugar;
                   1786:             *rp = g;
                   1787:             return 1;
                   1788:         } else {
                   1789:             m = BDY(g);
                   1790:             if ( NEXT(m) ) {
                   1791:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1792:             } else {
                   1793:                 FREEND(g); g = 0;
                   1794:             }
                   1795:             bucket->body[hindex] = g;
                   1796:             NEXT(m) = 0;
                   1797:             if ( d ) {
                   1798:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1799:             } else {
                   1800:                 MKND(n,m,1,d); tail = BDY(d);
                   1801:             }
                   1802:         }
                   1803:     }
                   1804: }
                   1805:
1.25      noro     1806: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *rp)
                   1807: {
                   1808:   int hindex,index;
                   1809:   NDV p;
                   1810:   ND u,d,red;
                   1811:   NODE l;
                   1812:   NM mul,m,mrd,tail;
                   1813:   int sugar,psugar,n,h_reducible;
                   1814:   PGeoBucket bucket;
                   1815:   int c,c1,c2;
                   1816:   Z cg,cred,gcd,zzz;
                   1817:   RHist h;
                   1818:   double hmag,gmag;
                   1819:   int count = 0;
                   1820:   int hcount = 0;
                   1821:   SIG sig;
                   1822:
                   1823:   if ( !g ) {
                   1824:     *rp = 0;
                   1825:     return 1;
                   1826:   }
                   1827:   sugar = SG(g);
                   1828:   n = NV(g);
                   1829:   if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
                   1830:   bucket = create_pbucket();
                   1831:   add_pbucket(mod,bucket,g);
                   1832:   d = 0;
                   1833:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1834:   sig = g->sig;
                   1835:   while ( 1 ) {
                   1836:     if ( mod > 0 || mod == -1 )
                   1837:       hindex = head_pbucket(mod,bucket);
                   1838:     else if ( mod == -2 )
                   1839:       hindex = head_pbucket_lf(bucket);
                   1840:     else
                   1841:       hindex = head_pbucket_q(bucket);
                   1842:     if ( hindex < 0 ) {
                   1843:       if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1844:       if ( d ) {
                   1845:         SG(d) = sugar;
                   1846:         d->sig = sig;
                   1847:       }
                   1848:       *rp = d;
                   1849:       return 1;
                   1850:     }
                   1851:     g = bucket->body[hindex];
                   1852:     index = ndl_find_reducer_s(HDL(g),sig);
                   1853:     if ( index >= 0 && index < nd_psn ) {
                   1854:       count++;
                   1855:       if ( !d ) hcount++;
                   1856:       h = nd_psh[index];
                   1857:       ndl_sub(HDL(g),DL(h),DL(mul));
                   1858:       if ( ndl_check_bound2(index,DL(mul)) ) {
                   1859:         nd_free(d);
                   1860:         free_pbucket(bucket);
                   1861:         *rp = 0;
                   1862:         return 0;
                   1863:       }
                   1864:       p = ps[index];
                   1865:       if ( mod == -1 )
                   1866:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1867:       else if ( mod == -2 ) {
                   1868:         Z inv,t;
                   1869:         divlf(ONE,HCZ(p),&inv);
                   1870:         chsgnlf(HCZ(g),&t);
                   1871:         mullf(inv,t,&CZ(mul));
                   1872:       } else if ( mod ) {
                   1873:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1874:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1875:       } else {
                   1876:         igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1877:         chsgnz(cg,&CZ(mul));
                   1878:         nd_mul_c_q(d,(P)cred);
                   1879:         mulq_pbucket(bucket,cred);
                   1880:         g = bucket->body[hindex];
                   1881:         gmag = (double)p_mag((P)HCZ(g));
                   1882:       }
                   1883:       red = ndv_mul_nm(mod,mul,p);
                   1884:       bucket->body[hindex] = nd_remove_head(g);
                   1885:       red = nd_remove_head(red);
                   1886:       add_pbucket(mod,bucket,red);
                   1887:       psugar = SG(p)+TD(DL(mul));
                   1888:       sugar = MAX(sugar,psugar);
                   1889:       if ( !mod && hmag && (gmag > hmag) ) {
                   1890:          g = normalize_pbucket(mod,bucket);
                   1891:          if ( !g ) {
                   1892:            if ( d ) {
                   1893:              SG(d) = sugar;
                   1894:              d->sig = sig;
                   1895:            }
                   1896:            *rp = d;
                   1897:            return 1;
                   1898:          }
                   1899:          nd_removecont2(d,g);
                   1900:          hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
                   1901:          add_pbucket(mod,bucket,g);
                   1902:       }
                   1903:     } else if ( index == -1 ) {
                   1904:       // singular top reducible
                   1905:       return -1;
                   1906:     } else if ( !full ) {
                   1907:       g = normalize_pbucket(mod,bucket);
                   1908:       if ( g ) {
                   1909:         SG(g) = sugar;
                   1910:         g->sig = sig;
                   1911:       }
                   1912:       *rp = g;
                   1913:       return 1;
                   1914:     } else {
                   1915:       m = BDY(g);
                   1916:       if ( NEXT(m) ) {
                   1917:         BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1918:       } else {
                   1919:         FREEND(g); g = 0;
                   1920:       }
                   1921:       bucket->body[hindex] = g;
                   1922:       NEXT(m) = 0;
                   1923:       if ( d ) {
                   1924:         NEXT(tail)=m; tail=m; LEN(d)++;
                   1925:       } else {
                   1926:         MKND(n,m,1,d); tail = BDY(d);
                   1927:       }
                   1928:     }
                   1929:   }
                   1930: }
                   1931:
1.1       noro     1932: /* input : list of NDV, cand : list of NDV */
                   1933:
                   1934: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
                   1935: {
                   1936:     int n,i,stat;
                   1937:     ND nf,d;
                   1938:     NDV r;
                   1939:     NODE t,s;
                   1940:     union oNDC dn;
                   1941:     Z q;
                   1942:     LIST list;
                   1943:
1.24      noro     1944:     ndv_setup(m,0,cand,nd_gentrace?1:0,1,0);
1.1       noro     1945:     n = length(cand);
                   1946:
                   1947:   if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
                   1948:     /* membercheck : list is a subset of Id(cand) ? */
                   1949:     for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
                   1950: again:
                   1951:     nd_tracelist = 0;
                   1952:         if ( nd_bpe > obpe )
                   1953:             r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
                   1954:         else
                   1955:             r = (NDV)BDY(t);
                   1956: #if 0
                   1957:         // moved to nd_f4_lf_trace()
                   1958:         if ( m == -2 ) ndv_mod(m,r);
                   1959: #endif
                   1960:         d = ndvtond(m,r);
1.6       noro     1961:         stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1       noro     1962:         if ( !stat ) {
                   1963:             nd_reconstruct(0,0);
                   1964:             goto again;
                   1965:         } else if ( nf ) return 0;
                   1966:     if ( nd_gentrace ) {
                   1967:       nd_tracelist = reverse_node(nd_tracelist);
                   1968:       MKLIST(list,nd_tracelist);
1.6       noro     1969:       STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1       noro     1970:       MKNODE(s,list,nd_alltracelist);
                   1971:       nd_alltracelist = s; nd_tracelist = 0;
                   1972:     }
                   1973:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   1974:     }
                   1975:     if ( DP_Print ) { printf("\n"); }
                   1976:     return 1;
                   1977: }
                   1978:
                   1979: ND nd_remove_head(ND p)
                   1980: {
                   1981:     NM m;
                   1982:
                   1983:     m = BDY(p);
                   1984:     if ( !NEXT(m) ) {
                   1985:         FREEND(p); p = 0;
                   1986:     } else {
                   1987:         BDY(p) = NEXT(m); LEN(p)--;
                   1988:     }
                   1989:     FREENM(m);
                   1990:     return p;
                   1991: }
                   1992:
                   1993: ND nd_separate_head(ND p,ND *head)
                   1994: {
                   1995:     NM m,m0;
                   1996:     ND r;
                   1997:
                   1998:     m = BDY(p);
                   1999:     if ( !NEXT(m) ) {
                   2000:         *head = p; p = 0;
                   2001:     } else {
                   2002:         m0 = m;
                   2003:         BDY(p) = NEXT(m); LEN(p)--;
                   2004:         NEXT(m0) = 0;
                   2005:         MKND(NV(p),m0,1,r);
                   2006:         *head = r;
                   2007:     }
                   2008:     return p;
                   2009: }
                   2010:
                   2011: PGeoBucket create_pbucket()
                   2012: {
                   2013:     PGeoBucket g;
                   2014:
                   2015:     g = CALLOC(1,sizeof(struct oPGeoBucket));
                   2016:     g->m = -1;
                   2017:     return g;
                   2018: }
                   2019:
                   2020: void free_pbucket(PGeoBucket b) {
                   2021:     int i;
                   2022:
                   2023:     for ( i = 0; i <= b->m; i++ )
                   2024:         if ( b->body[i] ) {
                   2025:             nd_free(b->body[i]);
                   2026:             b->body[i] = 0;
                   2027:         }
                   2028:     GCFREE(b);
                   2029: }
                   2030:
                   2031: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   2032: {
                   2033:     int l,i,k,m;
                   2034:
                   2035:     if ( !d )
                   2036:         return;
                   2037:     l = LEN(d);
                   2038:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2039:     /* 2^(k-1) < l <= 2^k (=m) */
                   2040:     d = nd_merge(g->body[k],d);
                   2041:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   2042:         g->body[k] = 0;
                   2043:         d = nd_merge(g->body[k+1],d);
                   2044:     }
                   2045:     g->body[k] = d;
                   2046:     g->m = MAX(g->m,k);
                   2047: }
                   2048:
                   2049: void add_pbucket(int mod,PGeoBucket g,ND d)
                   2050: {
                   2051:     int l,i,k,m;
                   2052:
                   2053:     if ( !d )
                   2054:         return;
                   2055:     l = LEN(d);
                   2056:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2057:     /* 2^(k-1) < l <= 2^k (=m) */
                   2058:     d = nd_add(mod,g->body[k],d);
                   2059:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   2060:         g->body[k] = 0;
                   2061:         d = nd_add(mod,g->body[k+1],d);
                   2062:     }
                   2063:     g->body[k] = d;
                   2064:     g->m = MAX(g->m,k);
                   2065: }
                   2066:
                   2067: void mulq_pbucket(PGeoBucket g,Z c)
                   2068: {
                   2069:     int k;
                   2070:
                   2071:     for ( k = 0; k <= g->m; k++ )
                   2072:         nd_mul_c_q(g->body[k],(P)c);
                   2073: }
                   2074:
                   2075: NM remove_head_pbucket_symbolic(PGeoBucket g)
                   2076: {
                   2077:     int j,i,k,c;
                   2078:     NM head;
                   2079:
                   2080:     k = g->m;
                   2081:     j = -1;
                   2082:     for ( i = 0; i <= k; i++ ) {
                   2083:         if ( !g->body[i] ) continue;
                   2084:         if ( j < 0 ) j = i;
                   2085:         else {
                   2086:             c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
                   2087:             if ( c > 0 )
                   2088:                 j = i;
                   2089:             else if ( c == 0 )
                   2090:                 g->body[i] = nd_remove_head(g->body[i]);
                   2091:         }
                   2092:     }
                   2093:     if ( j < 0 ) return 0;
                   2094:     else {
                   2095:         head = BDY(g->body[j]);
                   2096:         if ( !NEXT(head) ) {
                   2097:             FREEND(g->body[j]);
                   2098:             g->body[j] = 0;
                   2099:         } else {
                   2100:             BDY(g->body[j]) = NEXT(head);
                   2101:             LEN(g->body[j])--;
                   2102:         }
                   2103:         return head;
                   2104:     }
                   2105: }
                   2106:
                   2107: int head_pbucket(int mod,PGeoBucket g)
                   2108: {
                   2109:     int j,i,c,k,nv,sum;
                   2110:     UINT *di,*dj;
                   2111:     ND gi,gj;
                   2112:
                   2113:     k = g->m;
                   2114:     while ( 1 ) {
                   2115:         j = -1;
                   2116:         for ( i = 0; i <= k; i++ ) {
                   2117:             if ( !(gi = g->body[i]) )
                   2118:                 continue;
                   2119:             if ( j < 0 ) {
                   2120:                 j = i;
                   2121:                 gj = g->body[j];
                   2122:                 dj = HDL(gj);
                   2123:                 sum = HCM(gj);
                   2124:             } else {
                   2125:                 c = DL_COMPARE(HDL(gi),dj);
                   2126:                 if ( c > 0 ) {
                   2127:                     if ( sum ) HCM(gj) = sum;
                   2128:                     else g->body[j] = nd_remove_head(gj);
                   2129:                     j = i;
                   2130:                     gj = g->body[j];
                   2131:                     dj = HDL(gj);
                   2132:                     sum = HCM(gj);
                   2133:                 } else if ( c == 0 ) {
                   2134:                     if ( mod == -1 )
                   2135:                         sum = _addsf(sum,HCM(gi));
                   2136:                     else {
                   2137:                         sum = sum+HCM(gi)-mod;
                   2138:                         if ( sum < 0 ) sum += mod;
                   2139:                     }
                   2140:                     g->body[i] = nd_remove_head(gi);
                   2141:                 }
                   2142:             }
                   2143:         }
                   2144:         if ( j < 0 ) return -1;
                   2145:         else if ( sum ) {
                   2146:             HCM(gj) = sum;
                   2147:             return j;
                   2148:         } else
                   2149:             g->body[j] = nd_remove_head(gj);
                   2150:     }
                   2151: }
                   2152:
                   2153: int head_pbucket_q(PGeoBucket g)
                   2154: {
                   2155:     int j,i,c,k,nv;
                   2156:     Z sum,t;
                   2157:     ND gi,gj;
                   2158:
                   2159:     k = g->m;
                   2160:     while ( 1 ) {
                   2161:         j = -1;
                   2162:         for ( i = 0; i <= k; i++ ) {
                   2163:             if ( !(gi = g->body[i]) ) continue;
                   2164:             if ( j < 0 ) {
                   2165:                 j = i;
                   2166:                 gj = g->body[j];
1.6       noro     2167:                 sum = HCZ(gj);
1.1       noro     2168:             } else {
                   2169:                 nv = NV(gi);
                   2170:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   2171:                 if ( c > 0 ) {
1.6       noro     2172:                     if ( sum ) HCZ(gj) = sum;
1.1       noro     2173:                     else g->body[j] = nd_remove_head(gj);
                   2174:                     j = i;
                   2175:                     gj = g->body[j];
1.6       noro     2176:                     sum = HCZ(gj);
1.1       noro     2177:                 } else if ( c == 0 ) {
1.6       noro     2178:                     addz(sum,HCZ(gi),&t);
1.1       noro     2179:                     sum = t;
                   2180:                     g->body[i] = nd_remove_head(gi);
                   2181:                 }
                   2182:             }
                   2183:         }
                   2184:         if ( j < 0 ) return -1;
                   2185:         else if ( sum ) {
1.6       noro     2186:             HCZ(gj) = sum;
1.1       noro     2187:             return j;
                   2188:         } else
                   2189:             g->body[j] = nd_remove_head(gj);
                   2190:     }
                   2191: }
                   2192:
                   2193: int head_pbucket_lf(PGeoBucket g)
                   2194: {
                   2195:     int j,i,c,k,nv;
                   2196:     Z sum,t;
                   2197:     ND gi,gj;
                   2198:
                   2199:     k = g->m;
                   2200:     while ( 1 ) {
                   2201:         j = -1;
                   2202:         for ( i = 0; i <= k; i++ ) {
                   2203:             if ( !(gi = g->body[i]) ) continue;
                   2204:             if ( j < 0 ) {
                   2205:                 j = i;
                   2206:                 gj = g->body[j];
                   2207:                 sum = HCZ(gj);
                   2208:             } else {
                   2209:                 nv = NV(gi);
                   2210:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   2211:                 if ( c > 0 ) {
                   2212:                     if ( sum ) HCZ(gj) = sum;
                   2213:                     else g->body[j] = nd_remove_head(gj);
                   2214:                     j = i;
                   2215:                     gj = g->body[j];
                   2216:                     sum = HCZ(gj);
                   2217:                 } else if ( c == 0 ) {
                   2218:                     addlf(sum,HCZ(gi),&t);
                   2219:                     sum = t;
                   2220:                     g->body[i] = nd_remove_head(gi);
                   2221:                 }
                   2222:             }
                   2223:         }
                   2224:         if ( j < 0 ) return -1;
                   2225:         else if ( sum ) {
                   2226:             HCZ(gj) = sum;
                   2227:             return j;
                   2228:         } else
                   2229:             g->body[j] = nd_remove_head(gj);
                   2230:     }
                   2231: }
                   2232:
                   2233: ND normalize_pbucket(int mod,PGeoBucket g)
                   2234: {
                   2235:     int i;
                   2236:     ND r,t;
                   2237:
                   2238:     r = 0;
                   2239:     for ( i = 0; i <= g->m; i++ ) {
                   2240:         r = nd_add(mod,r,g->body[i]);
                   2241:         g->body[i] = 0;
                   2242:     }
                   2243:     g->m = -1;
                   2244:     return r;
                   2245: }
                   2246:
                   2247: #if 0
                   2248: void register_hcf(NDV p)
                   2249: {
                   2250:     DCP dc,t;
                   2251:     P hc,h;
                   2252:     int c;
                   2253:     NODE l,l1,prev;
                   2254:
                   2255:     hc = p->body->c.p;
                   2256:     if ( !nd_vc || NUM(hc) ) return;
                   2257:     fctrp(nd_vc,hc,&dc);
                   2258:     for ( t = dc; t; t = NEXT(t) ) {
                   2259:         h = t->c;
                   2260:         if ( NUM(h) ) continue;
                   2261:         for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   2262:             c = compp(nd_vc,h,(P)BDY(l));
                   2263:             if ( c >= 0 ) break;
                   2264:         }
                   2265:         if ( !l || c > 0  ) {
                   2266:             MKNODE(l1,h,l);
                   2267:             if ( !prev )
                   2268:                 nd_hcf = l1;
                   2269:             else
                   2270:                 NEXT(prev) = l1;
                   2271:         }
                   2272:     }
                   2273: }
                   2274: #else
                   2275: void register_hcf(NDV p)
                   2276: {
                   2277:     DCP dc,t;
                   2278:     P hc,h,q;
                   2279:     Q dmy;
                   2280:     int c;
                   2281:     NODE l,l1,prev;
                   2282:
                   2283:     hc = p->body->c.p;
                   2284:     if ( NUM(hc) ) return;
                   2285:     ptozp(hc,1,&dmy,&h);
                   2286: #if 1
                   2287:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   2288:         while ( 1 ) {
                   2289:             if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
                   2290:             else break;
                   2291:         }
                   2292:     }
                   2293:     if ( NUM(h) ) return;
                   2294: #endif
                   2295:     for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   2296:         c = compp(nd_vc,h,(P)BDY(l));
                   2297:         if ( c >= 0 ) break;
                   2298:     }
                   2299:     if ( !l || c > 0  ) {
                   2300:         MKNODE(l1,h,l);
                   2301:         if ( !prev )
                   2302:             nd_hcf = l1;
                   2303:         else
                   2304:             NEXT(prev) = l1;
                   2305:     }
                   2306: }
                   2307: #endif
                   2308:
                   2309: int do_diagonalize(int sugar,int m)
                   2310: {
1.6       noro     2311:   int i,nh,stat;
                   2312:   NODE r,g,t;
                   2313:   ND h,nf,s,head;
                   2314:   NDV nfv;
                   2315:   Q q;
                   2316:   P nm,nmp,dn,mnp,dnp,cont,cont1;
                   2317:   union oNDC hc;
                   2318:   NODE node;
                   2319:   LIST l;
                   2320:   Z iq;
1.1       noro     2321:
1.6       noro     2322:   for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   2323:     if ( nd_gentrace ) {
                   2324:       /* Trace = [1,index,1,1] */
                   2325:       STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2326:       MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2327:     }
                   2328:     if ( nd_demand )
                   2329:       nfv = ndv_load(i);
                   2330:     else
                   2331:       nfv = nd_ps[i];
                   2332:     s = ndvtond(m,nfv);
                   2333:     s = nd_separate_head(s,&head);
                   2334:     stat = nd_nf(m,head,s,nd_ps,1,&nf);
                   2335:     if ( !stat ) return 0;
                   2336:     ndv_free(nfv);
                   2337:     hc = HCU(nf); nd_removecont(m,nf);
                   2338:     /* exact division */
                   2339:     cont = ndc_div(m,hc,HCU(nf));
1.1       noro     2340:     if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6       noro     2341:     nfv = ndtondv(m,nf);
                   2342:     nd_free(nf);
                   2343:     nd_bound[i] = ndv_compute_bound(nfv);
                   2344:     if ( !m ) register_hcf(nfv);
                   2345:     if ( nd_demand ) {
                   2346:       ndv_save(nfv,i);
                   2347:       ndv_free(nfv);
                   2348:     } else
                   2349:       nd_ps[i] = nfv;
                   2350:   }
                   2351:   return 1;
1.1       noro     2352: }
                   2353:
                   2354: LIST compute_splist()
                   2355: {
                   2356:   NODE g,tn0,tn,node;
                   2357:   LIST l0;
                   2358:   ND_pairs d,t;
                   2359:   int i;
                   2360:   Z i1,i2;
                   2361:
                   2362:     g = 0; d = 0;
                   2363:     for ( i = 0; i < nd_psn; i++ ) {
                   2364:         d = update_pairs(d,g,i,0);
                   2365:         g = update_base(g,i);
                   2366:     }
                   2367:   for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
                   2368:     NEXTNODE(tn0,tn);
1.6       noro     2369:         STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1       noro     2370:         node = mknode(2,i1,i2); MKLIST(l0,node);
                   2371:     BDY(tn) = l0;
                   2372:   }
                   2373:   if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
                   2374:   return l0;
                   2375: }
                   2376:
                   2377: /* return value = 0 => input is not a GB */
                   2378:
                   2379: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
                   2380: {
1.6       noro     2381:   int i,nh,sugar,stat;
                   2382:   NODE r,g,t;
                   2383:   ND_pairs d;
                   2384:   ND_pairs l;
                   2385:   ND h,nf,s,head,nf1;
                   2386:   NDV nfv;
                   2387:   Z q;
                   2388:   union oNDC dn,hc;
                   2389:   int diag_count = 0;
                   2390:   P cont;
                   2391:   LIST list;
1.27      noro     2392: struct oEGT eg1,eg2,eg_update;
1.6       noro     2393:
1.27      noro     2394: init_eg(&eg_update);
1.11      noro     2395:   Nnd_add = 0;
1.6       noro     2396:   g = 0; d = 0;
                   2397:   for ( i = 0; i < nd_psn; i++ ) {
                   2398:     d = update_pairs(d,g,i,gensyz);
                   2399:     g = update_base(g,i);
                   2400:   }
                   2401:   sugar = 0;
                   2402:   while ( d ) {
1.1       noro     2403: again:
1.6       noro     2404:     l = nd_minp(d,&d);
                   2405:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   2406:     if ( SG(l) != sugar ) {
                   2407:       if ( ishomo ) {
                   2408:         diag_count = 0;
                   2409:         stat = do_diagonalize(sugar,m);
1.1       noro     2410:         if ( !stat ) {
1.6       noro     2411:           NEXT(l) = d; d = l;
                   2412:           d = nd_reconstruct(0,d);
                   2413:           goto again;
1.1       noro     2414:         }
1.6       noro     2415:       }
                   2416:       sugar = SG(l);
                   2417:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2418:     }
                   2419:     stat = nd_sp(m,0,l,&h);
                   2420:     if ( !stat ) {
                   2421:       NEXT(l) = d; d = l;
                   2422:       d = nd_reconstruct(0,d);
                   2423:       goto again;
                   2424:     }
1.1       noro     2425: #if USE_GEOBUCKET
1.6       noro     2426:     stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!Top,&nf)
                   2427:       :nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1       noro     2428: #else
1.6       noro     2429:     stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1       noro     2430: #endif
1.6       noro     2431:     if ( !stat ) {
                   2432:       NEXT(l) = d; d = l;
                   2433:       d = nd_reconstruct(0,d);
                   2434:       goto again;
                   2435:     } else if ( nf ) {
                   2436:       if ( checkonly || gensyz ) return 0;
1.1       noro     2437:       if ( nd_newelim ) {
                   2438:         if ( nd_module ) {
                   2439:           if ( MPOS(HDL(nf)) > 1 ) return 0;
                   2440:         } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
                   2441:       }
1.6       noro     2442:       if ( DP_Print ) { printf("+"); fflush(stdout); }
                   2443:       hc = HCU(nf);
                   2444:       nd_removecont(m,nf);
                   2445:       if ( !m && nd_nalg ) {
                   2446:         nd_monic(0,&nf);
                   2447:         nd_removecont(m,nf);
                   2448:       }
                   2449:       if ( nd_gentrace ) {
                   2450:         /* exact division */
1.1       noro     2451:         cont = ndc_div(m,hc,HCU(nf));
                   2452:         if ( m || !UNIQ(cont) ) {
1.6       noro     2453:           t = mknode(4,NULLP,NULLP,NULLP,cont);
                   2454:           MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1       noro     2455:           nd_tracelist = t;
                   2456:         }
                   2457:       }
1.6       noro     2458:       nfv = ndtondv(m,nf); nd_free(nf);
1.24      noro     2459:       nh = ndv_newps(m,nfv,0);
1.6       noro     2460:       if ( !m && (ishomo && ++diag_count == diag_period) ) {
                   2461:         diag_count = 0;
                   2462:         stat = do_diagonalize(sugar,m);
                   2463:         if ( !stat ) {
                   2464:           NEXT(l) = d; d = l;
                   2465:           d = nd_reconstruct(1,d);
                   2466:           goto again;
1.1       noro     2467:         }
1.6       noro     2468:       }
1.27      noro     2469: get_eg(&eg1);
1.6       noro     2470:       d = update_pairs(d,g,nh,0);
1.27      noro     2471: get_eg(&eg2); add_eg(&eg_update,&eg1,&eg2);
1.6       noro     2472:       g = update_base(g,nh);
                   2473:       FREENDP(l);
                   2474:    } else {
                   2475:      if ( nd_gentrace && gensyz ) {
                   2476:        nd_tracelist = reverse_node(nd_tracelist);
                   2477:        MKLIST(list,nd_tracelist);
                   2478:        STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
                   2479:        MKNODE(t,list,nd_alltracelist);
                   2480:        nd_alltracelist = t; nd_tracelist = 0;
                   2481:      }
                   2482:      if ( DP_Print ) { printf("."); fflush(stdout); }
                   2483:        FREENDP(l);
                   2484:    }
                   2485:  }
                   2486:  conv_ilist(nd_demand,0,g,indp);
1.11      noro     2487:     if ( !checkonly && DP_Print ) { printf("nd_gb done. Number of nd_add=%d\n",Nnd_add); fflush(stdout); }
1.27      noro     2488: print_eg("update",&eg_update);
1.1       noro     2489:     return g;
                   2490: }
                   2491:
1.30      noro     2492: ND_pairs update_pairs_s(ND_pairs d,int t,NODE *syz);
                   2493: ND_pairs nd_newpairs_s(int t ,NODE *syz);
1.24      noro     2494:
                   2495: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *nf);
                   2496: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
                   2497:
                   2498: void _copydl(int n,DL d1,DL d2);
                   2499: void _subfromdl(int n,DL d1,DL d2);
                   2500: extern int (*cmpdl)(int n,DL d1,DL d2);
                   2501:
                   2502: NODE insert_sig(NODE l,SIG s)
                   2503: {
                   2504:   int pos;
                   2505:   DL sig;
                   2506:   struct oNODE root;
                   2507:   NODE p,prev,r;
                   2508:   SIG t;
                   2509:
                   2510:   pos = s->pos; sig = DL(s);
                   2511:   root.next = l; prev = &root;
                   2512:   for ( p = l; p; p = p->next ) {
                   2513:     t = (SIG)p->body;
                   2514:     if ( t->pos == pos ) {
                   2515:       if ( _dl_redble(DL(t),sig,nd_nvar) )
                   2516:         return root.next;
                   2517:       else if ( _dl_redble(sig,DL(t),nd_nvar) )
                   2518:         // remove p
                   2519:         prev->next = p->next;
                   2520:     } else
                   2521:       prev = p;
                   2522:   }
1.27      noro     2523:   NEWNODE(r); r->body = (pointer)s; r->next = 0;
                   2524:   for ( p = &root; p->next; p = p->next );
                   2525:   p->next = r;
                   2526: //  r->next = root.next;
                   2527: //  return r;
                   2528:   return root.next;
1.24      noro     2529: }
                   2530:
                   2531: ND_pairs remove_spair_s(ND_pairs d,SIG sig)
                   2532: {
                   2533:   struct oND_pairs root;
                   2534:   ND_pairs prev,p;
                   2535:   SIG spsig;
                   2536:
                   2537:   root.next = d;
                   2538:   prev = &root; p = d;
                   2539:   while ( p ) {
                   2540:     spsig = p->sig;
1.26      noro     2541:     if ( sig->pos == spsig->pos && _dl_redble(DL(sig),DL(spsig),nd_nvar) ) {
1.24      noro     2542:       // remove p
                   2543:       prev->next = p->next;
1.26      noro     2544:       Nsyz++;
                   2545:     } else
1.24      noro     2546:       prev = p;
                   2547:     p = p->next;
                   2548:   }
                   2549:   return (ND_pairs)root.next;
                   2550: }
                   2551:
1.29      noro     2552: int _dl_redble_ext(DL,DL,DL,int);
                   2553:
1.28      noro     2554: int small_lcm(ND_pairs l)
                   2555: {
                   2556:   SIG sig;
                   2557:   int i;
1.29      noro     2558:   NODE t;
1.28      noro     2559:   static DL lcm,mul,quo;
                   2560:   static int nvar;
                   2561:
                   2562:   if ( nvar < nd_nvar ) {
                   2563:     nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(quo,nvar); NEWDL(mul,nvar);
                   2564:   }
                   2565:   sig = l->sig;
                   2566:   _ndltodl(l->lcm,lcm);
1.29      noro     2567: #if 0
1.28      noro     2568:   for ( i = 0; i < nd_psn; i++ ) {
                   2569:     if ( sig->pos == nd_psh[i]->sig->pos &&
1.29      noro     2570:       _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
1.28      noro     2571:       _ndltodl(DL(nd_psh[i]),mul);
                   2572:       _addtodl(nd_nvar,quo,mul);
                   2573:       if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
                   2574:         break;
                   2575:     }
                   2576:   }
                   2577:   if ( i < nd_psn ) return 1;
                   2578:   else return 0;
1.29      noro     2579: #else
                   2580:   for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
                   2581:     i = (long)BDY(t);
                   2582:     if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
                   2583:       _ndltodl(DL(nd_psh[i]),mul);
                   2584:       _addtodl(nd_nvar,quo,mul);
                   2585:       if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
                   2586:         break;
                   2587:     }
                   2588:   }
                   2589:   if ( t ) return 1;
                   2590:   else return 0;
                   2591: #endif
1.28      noro     2592: }
                   2593:
                   2594: ND_pairs remove_large_lcm(ND_pairs d)
                   2595: {
                   2596:   struct oND_pairs root;
                   2597:   ND_pairs prev,p;
                   2598:
                   2599:   root.next = d;
                   2600:   prev = &root; p = d;
                   2601:   while ( p ) {
                   2602:     if ( small_lcm(p) ) {
                   2603:       // remove p
                   2604:       prev->next = p->next;
                   2605:     } else
                   2606:       prev = p;
                   2607:     p = p->next;
                   2608:   }
                   2609:   return (ND_pairs)root.next;
                   2610: }
                   2611:
1.26      noro     2612: struct oEGT eg_create,eg_newpairs,eg_merge;
                   2613:
1.30      noro     2614: NODE conv_ilist_s(int demand,int trace,int **indp);
                   2615:
1.24      noro     2616: NODE nd_sba_buch(int m,int ishomo,int **indp)
                   2617: {
1.29      noro     2618:   int i,j,nh,sugar,stat;
1.30      noro     2619:   NODE r,t,g;
1.24      noro     2620:   ND_pairs d;
                   2621:   ND_pairs l;
                   2622:   ND h,nf,s,head,nf1;
                   2623:   NDV nfv;
                   2624:   Z q;
                   2625:   union oNDC dn,hc;
                   2626:   P cont;
                   2627:   LIST list;
                   2628:   SIG sig;
1.29      noro     2629:   NODE *syzlist;
1.26      noro     2630:   int Nredundant;
1.28      noro     2631:   DL lcm,quo,mul;
1.31      noro     2632:   struct oEGT eg1,eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.24      noro     2633:
1.27      noro     2634: init_eg(&eg_remove);
1.29      noro     2635:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.26      noro     2636:   Nsyz = 0;
1.24      noro     2637:   Nnd_add = 0;
1.26      noro     2638:   Nredundant = 0;
1.30      noro     2639:   d = 0;
1.24      noro     2640:   for ( i = 0; i < nd_psn; i++ ) {
1.30      noro     2641:     d = update_pairs_s(d,i,syzlist);
1.24      noro     2642:   }
1.29      noro     2643:   for ( i = 0; i < nd_psn; i++ )
                   2644:     for ( j = i+1; j < nd_psn; j++ ) {
                   2645:       NEWSIG(sig); sig->pos = j;
                   2646:       _copydl(nd_nvar,nd_sba_hm[i],sig->dl);
                   2647:       syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
                   2648:     }
1.24      noro     2649:   sugar = 0;
1.26      noro     2650:   NEWDL(lcm,nd_nvar); NEWDL(quo,nd_nvar); NEWDL(mul,nd_nvar);
                   2651: init_eg(&eg_create);
                   2652: init_eg(&eg_merge);
1.29      noro     2653: init_eg(&eg_large);
1.31      noro     2654: init_eg(&eg_nf);
                   2655: init_eg(&eg_nfzero);
1.24      noro     2656:   while ( d ) {
                   2657: again:
1.26      noro     2658:     if ( DP_Print ) {
                   2659:       int len;
                   2660:       ND_pairs td;
1.28      noro     2661:       for ( td = d, len=0; td; td = td->next, len++)
                   2662:         ;
1.26      noro     2663:        if ( !(len%100) ) fprintf(asir_out,"(%d)",len);
                   2664:       }
1.24      noro     2665:     l = d; d = d->next;
1.28      noro     2666:     if ( small_lcm(l) ) {
1.24      noro     2667:       if ( DP_Print ) fprintf(asir_out,"M");
1.26      noro     2668:       Nredundant++;
1.24      noro     2669:       continue;
                   2670:     }
                   2671:     if ( SG(l) != sugar ) {
                   2672:       sugar = SG(l);
                   2673:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2674:     }
1.28      noro     2675:     sig = l->sig;
1.24      noro     2676:     stat = nd_sp(m,0,l,&h);
                   2677:     if ( !stat ) {
                   2678:       NEXT(l) = d; d = l;
                   2679:       d = nd_reconstruct(0,d);
                   2680:       goto again;
                   2681:     }
1.31      noro     2682: get_eg(&eg1);
1.25      noro     2683: #if USE_GEOBUCKET
1.24      noro     2684:     stat = m?nd_nf_pbucket_s(m,h,nd_ps,!Top,&nf):nd_nf_s(m,0,h,nd_ps,!Top,&nf);
                   2685: #else
                   2686:     stat = nd_nf_s(m,0,h,nd_ps,!Top,&nf);
                   2687: #endif
1.31      noro     2688: get_eg(&eg2);
1.24      noro     2689:     if ( !stat ) {
                   2690:       NEXT(l) = d; d = l;
                   2691:       d = nd_reconstruct(0,d);
                   2692:       goto again;
                   2693:     } else if ( stat == -1 ) {
                   2694:       if ( DP_Print ) { printf("S"); fflush(stdout); }
1.27      noro     2695:       FREENDP(l);
1.24      noro     2696:     } else if ( nf ) {
                   2697:       if ( DP_Print ) { printf("+"); fflush(stdout); }
1.31      noro     2698:       add_eg(&eg_nf,&eg1,&eg2);
1.24      noro     2699:       hc = HCU(nf);
                   2700:       nd_removecont(m,nf);
                   2701:       nfv = ndtondv(m,nf); nd_free(nf);
                   2702:       nh = ndv_newps(m,nfv,0);
1.26      noro     2703:
1.30      noro     2704:       d = update_pairs_s(d,nh,syzlist);
1.29      noro     2705:       nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.24      noro     2706:       FREENDP(l);
                   2707:    } else {
1.31      noro     2708:       add_eg(&eg_nfzero,&eg1,&eg2);
1.24      noro     2709:      // syzygy
1.27      noro     2710: get_eg(&eg1);
1.24      noro     2711:      d = remove_spair_s(d,sig);
1.27      noro     2712: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29      noro     2713:      syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.24      noro     2714:      if ( DP_Print ) { printf("."); fflush(stdout); }
                   2715:      FREENDP(l);
                   2716:    }
                   2717:  }
1.30      noro     2718:  g = conv_ilist_s(nd_demand,0,indp);
1.26      noro     2719:  if ( DP_Print ) {
1.35    ! noro     2720:    printf("\nnd_sba done. nd_add=%d,Nsyz=%d,Nsamesig=%d,Nredundant=%d\n",Nnd_add,Nsyz,Nsamesig,Nredundant);
1.26      noro     2721:    fflush(stdout);
                   2722:    print_eg("create",&eg_create);
                   2723:    print_eg("merge",&eg_merge);
1.27      noro     2724:    print_eg("remove",&eg_remove);
1.31      noro     2725:    print_eg("nf",&eg_nf);
                   2726:    print_eg("nfzero",&eg_nfzero);
1.27      noro     2727:    printf("\n");
1.26      noro     2728:  }
1.24      noro     2729:  return g;
                   2730: }
                   2731:
1.1       noro     2732: /* splist = [[i1,i2],...] */
                   2733:
                   2734: int check_splist(int m,NODE splist)
                   2735: {
                   2736:   NODE t,p;
                   2737:   ND_pairs d,r,l;
                   2738:   int stat;
                   2739:   ND h,nf;
                   2740:
                   2741:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   2742:     p = BDY((LIST)BDY(t));
1.6       noro     2743:     NEXTND_pairs(d,r);
                   2744:     r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
                   2745:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     2746:     SG(r) = TD(LCM(r)); /* XXX */
                   2747:   }
                   2748:   if ( d ) NEXT(r) = 0;
                   2749:
1.6       noro     2750:   while ( d ) {
1.1       noro     2751: again:
1.6       noro     2752:     l = nd_minp(d,&d);
                   2753:     stat = nd_sp(m,0,l,&h);
                   2754:     if ( !stat ) {
                   2755:       NEXT(l) = d; d = l;
                   2756:       d = nd_reconstruct(0,d);
                   2757:       goto again;
                   2758:     }
                   2759:     stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
                   2760:     if ( !stat ) {
                   2761:       NEXT(l) = d; d = l;
                   2762:       d = nd_reconstruct(0,d);
                   2763:       goto again;
                   2764:     } else if ( nf ) return 0;
1.1       noro     2765:     if ( DP_Print) { printf("."); fflush(stdout); }
1.6       noro     2766:   }
1.1       noro     2767:   if ( DP_Print) { printf("done.\n"); fflush(stdout); }
                   2768:   return 1;
                   2769: }
                   2770:
                   2771: int check_splist_f4(int m,NODE splist)
                   2772: {
                   2773:   UINT *s0vect;
1.6       noro     2774:   PGeoBucket bucket;
1.1       noro     2775:   NODE p,rp0,t;
                   2776:   ND_pairs d,r,l,ll;
                   2777:   int col,stat;
                   2778:
                   2779:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   2780:     p = BDY((LIST)BDY(t));
1.6       noro     2781:     NEXTND_pairs(d,r);
                   2782:     r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
                   2783:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     2784:     SG(r) = TD(LCM(r)); /* XXX */
                   2785:   }
                   2786:   if ( d ) NEXT(r) = 0;
                   2787:
1.6       noro     2788:   while ( d ) {
                   2789:     l = nd_minsugarp(d,&d);
                   2790:     bucket = create_pbucket();
                   2791:     stat = nd_sp_f4(m,0,l,bucket);
                   2792:     if ( !stat ) {
                   2793:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   2794:       NEXT(ll) = d; d = l;
                   2795:       d = nd_reconstruct(0,d);
                   2796:       continue;
                   2797:     }
                   2798:     if ( bucket->m < 0 ) continue;
                   2799:     col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   2800:     if ( !col ) {
                   2801:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   2802:       NEXT(ll) = d; d = l;
                   2803:       d = nd_reconstruct(0,d);
                   2804:       continue;
1.1       noro     2805:     }
1.6       noro     2806:     if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
                   2807:   }
                   2808:   return 1;
1.1       noro     2809: }
                   2810:
                   2811: int do_diagonalize_trace(int sugar,int m)
                   2812: {
1.6       noro     2813:   int i,nh,stat;
                   2814:   NODE r,g,t;
                   2815:   ND h,nf,nfq,s,head;
                   2816:   NDV nfv,nfqv;
                   2817:   Q q,den,num;
                   2818:   union oNDC hc;
                   2819:   NODE node;
                   2820:   LIST l;
                   2821:   Z iq;
                   2822:   P cont,cont1;
1.1       noro     2823:
1.6       noro     2824:   for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   2825:     if ( nd_gentrace ) {
                   2826:         /* Trace = [1,index,1,1] */
                   2827:         STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2828:         MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2829:     }
                   2830:     /* for nd_ps */
                   2831:     s = ndvtond(m,nd_ps[i]);
                   2832:     s = nd_separate_head(s,&head);
                   2833:     stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
                   2834:     if ( !stat ) return 0;
                   2835:     nf = nd_add(m,head,nf);
                   2836:     ndv_free(nd_ps[i]);
                   2837:     nd_ps[i] = ndtondv(m,nf);
                   2838:     nd_free(nf);
                   2839:
                   2840:     /* for nd_ps_trace */
                   2841:     if ( nd_demand )
                   2842:         nfv = ndv_load(i);
                   2843:     else
                   2844:         nfv = nd_ps_trace[i];
                   2845:     s = ndvtond(0,nfv);
                   2846:     s = nd_separate_head(s,&head);
                   2847:     stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
                   2848:     if ( !stat ) return 0;
                   2849:     ndv_free(nfv);
                   2850:     hc = HCU(nf); nd_removecont(0,nf);
                   2851:     /* exact division */
1.1       noro     2852:     cont = ndc_div(0,hc,HCU(nf));
1.6       noro     2853:     if ( nd_gentrace ) finalize_tracelist(i,cont);
                   2854:     nfv = ndtondv(0,nf);
                   2855:     nd_free(nf);
                   2856:     nd_bound[i] = ndv_compute_bound(nfv);
                   2857:     register_hcf(nfv);
                   2858:     if ( nd_demand ) {
                   2859:     ndv_save(nfv,i);
                   2860:     ndv_free(nfv);
                   2861:     } else
                   2862:     nd_ps_trace[i] = nfv;
                   2863:   }
                   2864:   return 1;
1.1       noro     2865: }
                   2866:
                   2867: static struct oEGT eg_invdalg;
                   2868: struct oEGT eg_le;
                   2869:
                   2870: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
                   2871: {
                   2872:     NODE tn;
                   2873:     P p1;
                   2874:
                   2875:     for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
                   2876:         substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
                   2877:     }
                   2878:     *r = p;
                   2879: }
                   2880:
                   2881: NODE nd_gb_trace(int m,int ishomo,int **indp)
                   2882: {
1.6       noro     2883:   int i,nh,sugar,stat;
                   2884:   NODE r,g,t;
                   2885:   ND_pairs d;
                   2886:   ND_pairs l;
                   2887:   ND h,nf,nfq,s,head;
                   2888:   NDV nfv,nfqv;
                   2889:   Z q,den,num;
                   2890:   P hc;
                   2891:   union oNDC dn,hnfq;
                   2892:   struct oEGT eg_monic,egm0,egm1;
                   2893:   int diag_count = 0;
                   2894:   P cont;
                   2895:   LIST list;
                   2896:
                   2897:   init_eg(&eg_monic);
                   2898:   init_eg(&eg_invdalg);
                   2899:   init_eg(&eg_le);
                   2900:   g = 0; d = 0;
                   2901:   for ( i = 0; i < nd_psn; i++ ) {
                   2902:     d = update_pairs(d,g,i,0);
                   2903:     g = update_base(g,i);
                   2904:   }
                   2905:   sugar = 0;
                   2906:   while ( d ) {
1.1       noro     2907: again:
1.6       noro     2908:     l = nd_minp(d,&d);
                   2909:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   2910:     if ( SG(l) != sugar ) {
1.1       noro     2911: #if 1
1.6       noro     2912:       if ( ishomo ) {
                   2913:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2914:         stat = do_diagonalize_trace(sugar,m);
                   2915:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2916:         diag_count = 0;
1.1       noro     2917:         if ( !stat ) {
1.6       noro     2918:           NEXT(l) = d; d = l;
                   2919:           d = nd_reconstruct(1,d);
                   2920:           goto again;
1.1       noro     2921:         }
1.6       noro     2922:       }
                   2923: #endif
                   2924:       sugar = SG(l);
                   2925:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2926:     }
                   2927:     stat = nd_sp(m,0,l,&h);
                   2928:     if ( !stat ) {
                   2929:       NEXT(l) = d; d = l;
                   2930:       d = nd_reconstruct(1,d);
                   2931:       goto again;
                   2932:     }
1.1       noro     2933: #if USE_GEOBUCKET
1.6       noro     2934:     stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.1       noro     2935: #else
1.6       noro     2936:     stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1       noro     2937: #endif
1.6       noro     2938:     if ( !stat ) {
                   2939:       NEXT(l) = d; d = l;
                   2940:       d = nd_reconstruct(1,d);
                   2941:       goto again;
                   2942:     } else if ( nf ) {
                   2943:       if ( nd_demand ) {
                   2944:         nfqv = ndv_load(nd_psn);
                   2945:         nfq = ndvtond(0,nfqv);
                   2946:       } else
                   2947:         nfq = 0;
                   2948:       if ( !nfq ) {
                   2949:         if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!Top,&nfq) ) {
                   2950:           NEXT(l) = d; d = l;
                   2951:           d = nd_reconstruct(1,d);
                   2952:           goto again;
                   2953:         }
                   2954:       }
                   2955:       if ( nfq ) {
                   2956:         /* m|HC(nfq) => failure */
                   2957:         if ( nd_vc ) {
                   2958:           nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
                   2959:         } else
                   2960:           q = HCZ(nfq);
                   2961:         if ( !remqi((Q)q,m) ) return 0;
                   2962:
                   2963:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   2964:         hnfq = HCU(nfq);
                   2965:         if ( nd_nalg ) {
                   2966:           /* m|DN(HC(nf)^(-1)) => failure */
                   2967:           get_eg(&egm0);
                   2968:           if ( !nd_monic(m,&nfq) ) return 0;
                   2969:           get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
                   2970:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   2971:           nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
                   2972:         } else {
                   2973:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   2974:           nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
                   2975:         }
                   2976:         if ( nd_gentrace ) {
                   2977:           /* exact division */
                   2978:           cont = ndc_div(0,hnfq,HCU(nfqv));
                   2979:           if ( !UNIQ(cont) ) {
                   2980:             t = mknode(4,NULLP,NULLP,NULLP,cont);
                   2981:             MKLIST(list,t); MKNODE(t,list,nd_tracelist);
                   2982:             nd_tracelist = t;
                   2983:           }
                   2984:         }
1.24      noro     2985:         nh = ndv_newps(0,nfv,nfqv);
1.6       noro     2986:         if ( ishomo && ++diag_count == diag_period ) {
                   2987:           diag_count = 0;
                   2988:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2989:           stat = do_diagonalize_trace(sugar,m);
                   2990:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2991:           if ( !stat ) {
1.1       noro     2992:             NEXT(l) = d; d = l;
                   2993:             d = nd_reconstruct(1,d);
                   2994:             goto again;
1.6       noro     2995:           }
1.1       noro     2996:         }
1.6       noro     2997:         d = update_pairs(d,g,nh,0);
                   2998:         g = update_base(g,nh);
                   2999:       } else {
                   3000:         if ( DP_Print ) { printf("*"); fflush(stdout); }
                   3001:       }
                   3002:     } else {
                   3003:       if ( DP_Print ) { printf("."); fflush(stdout); }
1.1       noro     3004:     }
1.6       noro     3005:     FREENDP(l);
                   3006:   }
                   3007:   if ( nd_nalg ) {
                   3008:     if ( DP_Print ) {
                   3009:       print_eg("monic",&eg_monic);
                   3010:       print_eg("invdalg",&eg_invdalg);
                   3011:       print_eg("le",&eg_le);
1.1       noro     3012:     }
1.6       noro     3013:   }
1.1       noro     3014:   conv_ilist(nd_demand,1,g,indp);
1.6       noro     3015:   if ( DP_Print ) { printf("nd_gb_trace done.\n"); fflush(stdout); }
                   3016:   return g;
1.1       noro     3017: }
                   3018:
                   3019: int ndv_compare(NDV *p1,NDV *p2)
                   3020: {
                   3021:     return DL_COMPARE(HDL(*p1),HDL(*p2));
                   3022: }
                   3023:
                   3024: int ndv_compare_rev(NDV *p1,NDV *p2)
                   3025: {
                   3026:     return -DL_COMPARE(HDL(*p1),HDL(*p2));
                   3027: }
                   3028:
                   3029: int ndvi_compare(NDVI p1,NDVI p2)
                   3030: {
                   3031:     return DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3032: }
                   3033:
                   3034: int ndvi_compare_rev(NDVI p1,NDVI p2)
                   3035: {
                   3036:     return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3037: }
                   3038:
                   3039: NODE ndv_reduceall(int m,NODE f)
                   3040: {
                   3041:   int i,j,n,stat;
                   3042:   ND nf,g,head;
                   3043:   NODE t,a0,a;
                   3044:   union oNDC dn;
                   3045:   Q q,num,den;
                   3046:   NODE node;
                   3047:   LIST l;
                   3048:   Z iq,jq;
                   3049:   int *perm;
                   3050:   union oNDC hc;
                   3051:   P cont,cont1;
                   3052:
                   3053:   if ( nd_nora ) return f;
                   3054:   n = length(f);
1.24      noro     3055:   ndv_setup(m,0,f,0,1,0);
1.1       noro     3056:   perm = (int *)MALLOC(n*sizeof(int));
                   3057:   if ( nd_gentrace ) {
                   3058:     for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6       noro     3059:       perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1       noro     3060:   }
                   3061:   for ( i = 0; i < n; ) {
                   3062:     if ( nd_gentrace ) {
                   3063:       /* Trace = [1,index,1,1] */
1.6       noro     3064:       STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1       noro     3065:       MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   3066:     }
                   3067:     g = ndvtond(m,nd_ps[i]);
                   3068:     g = nd_separate_head(g,&head);
1.6       noro     3069:     stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1       noro     3070:     if ( !stat )
                   3071:       nd_reconstruct(0,0);
                   3072:     else {
                   3073:       if ( DP_Print ) { printf("."); fflush(stdout); }
                   3074:       ndv_free(nd_ps[i]);
                   3075:       hc = HCU(nf); nd_removecont(m,nf);
                   3076:       if ( nd_gentrace ) {
                   3077:         for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6       noro     3078:           jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
                   3079:           STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1       noro     3080:         }
1.6       noro     3081:         /* exact division */
1.1       noro     3082:         cont = ndc_div(m,hc,HCU(nf));
                   3083:         finalize_tracelist(perm[i],cont);
                   3084:       }
                   3085:       nd_ps[i] = ndtondv(m,nf); nd_free(nf);
                   3086:       nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   3087:       i++;
                   3088:     }
                   3089:   }
                   3090:   if ( DP_Print ) { printf("\n"); }
                   3091:   for ( a0 = 0, i = 0; i < n; i++ ) {
                   3092:     NEXTNODE(a0,a);
                   3093:     if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
                   3094:     else {
                   3095:       for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
                   3096:       BDY(a) = (pointer)nd_ps[j];
                   3097:     }
                   3098:   }
                   3099:   NEXT(a) = 0;
                   3100:   return a0;
                   3101: }
                   3102:
                   3103: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
                   3104: {
                   3105:   ND_pairs d1,nd,cur,head,prev,remove;
                   3106:
                   3107:   if ( !g ) return d;
                   3108:   /* for testing */
                   3109:   if ( gensyz && nd_gensyz == 2 ) {
                   3110:     d1 = nd_newpairs(g,t);
                   3111:     if ( !d )
                   3112:       return d1;
                   3113:     else {
                   3114:       nd = d;
                   3115:       while ( NEXT(nd) ) nd = NEXT(nd);
                   3116:       NEXT(nd) = d1;
                   3117:       return d;
                   3118:     }
                   3119:   }
                   3120:   d = crit_B(d,t);
                   3121:   d1 = nd_newpairs(g,t);
                   3122:   d1 = crit_M(d1);
                   3123:   d1 = crit_F(d1);
                   3124:   if ( gensyz || do_weyl )
                   3125:     head = d1;
                   3126:   else {
                   3127:     prev = 0; cur = head = d1;
                   3128:     while ( cur ) {
                   3129:       if ( crit_2( cur->i1,cur->i2 ) ) {
                   3130:         remove = cur;
                   3131:         if ( !prev ) head = cur = NEXT(cur);
                   3132:         else cur = NEXT(prev) = NEXT(cur);
                   3133:         FREENDP(remove);
                   3134:       } else {
                   3135:         prev = cur; cur = NEXT(cur);
                   3136:       }
                   3137:     }
                   3138:   }
                   3139:   if ( !d )
                   3140:     return head;
                   3141:   else {
                   3142:     nd = d;
                   3143:     while ( NEXT(nd) ) nd = NEXT(nd);
                   3144:     NEXT(nd) = head;
                   3145:     return d;
                   3146:   }
                   3147: }
                   3148:
1.24      noro     3149: ND_pairs merge_pairs_s(ND_pairs d,ND_pairs d1);
                   3150:
1.30      noro     3151: ND_pairs update_pairs_s( ND_pairs d, int t,NODE *syz)
1.24      noro     3152: {
                   3153:   ND_pairs d1;
1.26      noro     3154:   struct oEGT eg1,eg2,eg3;
1.24      noro     3155:
1.30      noro     3156:   if ( !t ) return d;
1.26      noro     3157: get_eg(&eg1);
1.30      noro     3158:   d1 = nd_newpairs_s(t,syz);
1.26      noro     3159: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
1.24      noro     3160:   d = merge_pairs_s(d,d1);
1.26      noro     3161: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
1.24      noro     3162:   return d;
                   3163: }
1.1       noro     3164:
                   3165: ND_pairs nd_newpairs( NODE g, int t )
                   3166: {
                   3167:   NODE h;
                   3168:   UINT *dl;
                   3169:   int ts,s,i,t0,min,max;
                   3170:   ND_pairs r,r0;
                   3171:
                   3172:   dl = DL(nd_psh[t]);
                   3173:   ts = SG(nd_psh[t]) - TD(dl);
1.17      noro     3174:   if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1       noro     3175:   for ( r0 = 0, h = g; h; h = NEXT(h) ) {
                   3176:     if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
                   3177:       continue;
                   3178:     if ( nd_gbblock ) {
                   3179:       t0 = (long)BDY(h);
                   3180:       for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
                   3181:         min = nd_gbblock[i]; max = nd_gbblock[i+1];
                   3182:         if ( t0 >= min && t0 <= max && t >= min && t <= max )
                   3183:           break;
                   3184:       }
                   3185:       if ( nd_gbblock[i] >= 0 )
                   3186:         continue;
                   3187:     }
                   3188:     NEXTND_pairs(r0,r);
                   3189:     r->i1 = (long)BDY(h);
                   3190:     r->i2 = t;
                   3191:     ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
                   3192:     s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   3193:     SG(r) = MAX(s,ts) + TD(LCM(r));
                   3194:     /* experimental */
                   3195:     if ( nd_sugarweight )
                   3196:       r->sugar2 = ndl_weight2(r->lcm);
                   3197:   }
                   3198:   if ( r0 ) NEXT(r) = 0;
                   3199:   return r0;
                   3200: }
                   3201:
1.24      noro     3202: int comp_sig(SIG s1,SIG s2)
                   3203: {
                   3204: #if 0
                   3205:   if ( s1->pos > s2->pos ) return 1;
                   3206:   else if ( s1->pos < s2->pos ) return -1;
                   3207:   else return (*cmpdl)(nd_nvar,s1->dl,s2->dl);
                   3208: #else
                   3209:   static DL m1,m2;
                   3210:   static int nvar;
                   3211:   int ret;
                   3212:
1.26      noro     3213:   if ( nvar != nd_nvar ) {
                   3214:     nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
1.24      noro     3215:   }
1.31      noro     3216: #if 0
1.27      noro     3217:   _copydl(nd_nvar,nd_sba_hm[s1->pos],m1);
                   3218:   _copydl(nd_nvar,nd_sba_hm[s2->pos],m2);
1.24      noro     3219:   _addtodl(nd_nvar,s1->dl,m1);
                   3220:   _addtodl(nd_nvar,s2->dl,m2);
1.31      noro     3221: #else
                   3222:   _adddl(nd_nvar,s1->dl,nd_sba_hm[s1->pos],m1);
                   3223:   _adddl(nd_nvar,s2->dl,nd_sba_hm[s2->pos],m2);
                   3224: #endif
1.24      noro     3225:   ret = (*cmpdl)(nd_nvar,m1,m2);
                   3226:   if ( ret != 0 ) return ret;
                   3227:   else if ( s1->pos > s2->pos ) return 1;
                   3228:   else if ( s1->pos < s2->pos ) return -1;
                   3229:   else return 0;
                   3230: #endif
                   3231: }
                   3232:
                   3233: int _create_spair_s(int i1,int i2,ND_pairs sp,SIG sig1,SIG sig2)
                   3234: {
                   3235:   int ret,s1,s2;
                   3236:   RHist p1,p2;
                   3237:   static int wpd;
                   3238:   static UINT *lcm;
                   3239:
                   3240:   sp->i1 = i1;
                   3241:   sp->i2 = i2;
                   3242:   p1 = nd_psh[i1];
                   3243:   p2 = nd_psh[i2];
                   3244:   ndl_lcm(DL(p1),DL(p2),sp->lcm);
                   3245:   s1 = SG(p1)-TD(DL(p1));
                   3246:   s2 = SG(p2)-TD(DL(p2));
                   3247:   SG(sp) = MAX(s1,s2) + TD(sp->lcm);
                   3248:
1.26      noro     3249:   if ( wpd != nd_wpd ) {
1.24      noro     3250:     wpd = nd_wpd;
                   3251:     lcm = (UINT *)MALLOC(wpd*sizeof(UINT));
                   3252:   }
                   3253:   // DL(sig1) <- sp->lcm
                   3254:   // DL(sig1) -= DL(p1)
                   3255:   // DL(sig1) += DL(p1->sig)
1.27      noro     3256:   ndl_sub(sp->lcm,DL(p1),lcm);
1.24      noro     3257:   _ndltodl(lcm,DL(sig1));
                   3258:   _addtodl(nd_nvar,DL(p1->sig),DL(sig1));
                   3259:   sig1->pos = p1->sig->pos;
                   3260:
                   3261:   // DL(sig2) <- sp->lcm
                   3262:   // DL(sig2) -= DL(p2)
                   3263:   // DL(sig2) += DL(p2->sig)
1.27      noro     3264:   ndl_sub(sp->lcm,DL(p2),lcm);
1.24      noro     3265:   _ndltodl(lcm,DL(sig2));
                   3266:   _addtodl(nd_nvar,DL(p2->sig),DL(sig2));
                   3267:   sig2->pos = p2->sig->pos;
                   3268:
                   3269:   ret = comp_sig(sig1,sig2);
                   3270:   if ( ret == 0 ) return 0;
                   3271:   else if ( ret > 0 ) sp->sig = sig1;
                   3272:   else sp->sig = sig2;
                   3273:   return 1;
                   3274: }
                   3275:
                   3276: SIG dup_sig(SIG sig)
                   3277: {
                   3278:   SIG r;
                   3279:
                   3280:   if ( !sig ) return 0;
                   3281:   else {
                   3282:     NEWSIG(r);
                   3283:     _copydl(nd_nvar,DL(sig),DL(r));
                   3284:     r->pos = sig->pos;
                   3285:     return r;
                   3286:   }
                   3287: }
                   3288:
                   3289: void dup_ND_pairs(ND_pairs to,ND_pairs from)
                   3290: {
                   3291:   to->i1 = from->i1;
                   3292:   to->i2 = from->i2;
                   3293:   to->sugar = from->sugar;
                   3294:   to->sugar2 = from->sugar2;
                   3295:   ndl_copy(from->lcm,to->lcm);
                   3296:   to->sig = dup_sig(from->sig);
                   3297: }
                   3298:
                   3299: ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
                   3300: {
                   3301:   struct oND_pairs root;
                   3302:   ND_pairs q1,q2,r0,r;
                   3303:   int ret;
                   3304:
                   3305:   r = &root;
                   3306:   for ( q1 = p1, q2 = p2; q1 != 0 && q2 != 0; ) {
                   3307:     ret = comp_sig(q1->sig,q2->sig);
                   3308:     if ( ret < 0 ) {
                   3309:       r->next = q1; r = q1; q1 = q1->next;
                   3310:     } else if ( ret > 0 ) {
                   3311:       r->next = q2; r = q2; q2 = q2->next;
                   3312:     } else {
                   3313:       ret = DL_COMPARE(q1->lcm,q2->lcm);
1.35    ! noro     3314:       Nsamesig++;
1.24      noro     3315:       if ( ret < 0 ) {
                   3316:         r->next = q1; r = q1; q1 = q1->next;
                   3317:         q2 = q2->next;
                   3318:       } else {
                   3319:         r->next = q2; r = q2; q2 = q2->next;
                   3320:         q1 = q1->next;
                   3321:       }
                   3322:     }
                   3323:   }
                   3324:   if ( q1 ) {
                   3325:     r->next = q1;
                   3326:   } else {
                   3327:     r->next = q2;
                   3328:   }
                   3329:   return root.next;
                   3330: }
                   3331:
                   3332: ND_pairs insert_pair_s(ND_pairs l,ND_pairs s)
                   3333: {
                   3334:   ND_pairs p,prev;
                   3335:   int ret;
                   3336:
                   3337:   for ( p = l, prev = 0; p != 0; prev = p, p = p->next ) {
                   3338:     if ( (ret = comp_sig(s->sig,p->sig)) <= 0 )
                   3339:       break;
                   3340:   }
                   3341:   if ( ret == 0 ) {
                   3342:     ret = DL_COMPARE(s->lcm,p->lcm);
                   3343:     if ( ret < 0 ) {
                   3344:       // replace p with s
                   3345:       s->next = p->next;
                   3346:       if ( prev == 0 ) {
                   3347:         return s;
                   3348:       } else {
                   3349:         prev->next = s;
                   3350:         return l;
                   3351:       }
                   3352:     } else
                   3353:       return l;
                   3354:   } else {
                   3355:     // insert s between prev and p
                   3356:     s->next = p;
                   3357:     if ( prev == 0 ) {
                   3358:       return s;
                   3359:     } else {
                   3360:       prev->next = s;
                   3361:       return l;
                   3362:     }
                   3363:   }
                   3364: }
                   3365:
1.29      noro     3366: INLINE int __dl_redble(DL d1,DL d2,int nvar)
                   3367: {
                   3368:   int i;
                   3369:
                   3370:   if ( d1->td > d2->td )
                   3371:     return 0;
                   3372:   for ( i = nvar-1; i >= 0; i-- )
                   3373:     if ( d1->d[i] > d2->d[i] )
                   3374:       break;
                   3375:   if ( i >= 0 )
                   3376:     return 0;
                   3377:   else
                   3378:     return 1;
                   3379: }
                   3380:
1.30      noro     3381: ND_pairs nd_newpairs_s(int t, NODE *syz)
1.24      noro     3382: {
                   3383:   NODE h,s;
                   3384:   UINT *dl;
1.27      noro     3385:   int ts,ret,i;
1.24      noro     3386:   ND_pairs r,r0,_sp,sp;
1.29      noro     3387:   SIG spsig,tsig;
                   3388:   static int nvar;
                   3389:   static SIG _sig1,_sig2;
1.26      noro     3390:   struct oEGT eg1,eg2,eg3,eg4;
1.24      noro     3391:
                   3392:   NEWND_pairs(_sp);
1.29      noro     3393:   if ( !_sig1 || nvar != nd_nvar ) {
                   3394:     nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
                   3395:   }
1.24      noro     3396:   r0 = 0;
1.27      noro     3397:   for ( i = 0; i < t; i++ ) {
                   3398:     ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
1.24      noro     3399:     if ( ret ) {
                   3400:       spsig = _sp->sig;
1.29      noro     3401:       for ( s = syz[spsig->pos]; s; s = s->next ) {
1.24      noro     3402:         tsig = (SIG)s->body;
1.29      noro     3403:         if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
1.24      noro     3404:           break;
                   3405:       }
                   3406:       if ( s == 0 ) {
                   3407:         NEWND_pairs(sp);
                   3408:         dup_ND_pairs(sp,_sp);
                   3409:         r0 = insert_pair_s(r0,sp);
1.26      noro     3410:       } else
                   3411:         Nsyz++;
1.24      noro     3412:     }
                   3413:   }
                   3414:   return r0;
                   3415: }
                   3416:
1.1       noro     3417: /* ipair = [i1,i2],[i1,i2],... */
                   3418: ND_pairs nd_ipairtospair(NODE ipair)
                   3419: {
                   3420:   int s1,s2;
                   3421:   NODE tn,t;
                   3422:   ND_pairs r,r0;
                   3423:
                   3424:   for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
                   3425:     NEXTND_pairs(r0,r);
                   3426:     tn = BDY((LIST)BDY(t));
1.6       noro     3427:     r->i1 = ZTOS((Q)ARG0(tn));
                   3428:     r->i2 = ZTOS((Q)ARG1(tn));
1.1       noro     3429:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
                   3430:     s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   3431:     s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
                   3432:     SG(r) = MAX(s1,s2) + TD(LCM(r));
                   3433:     /* experimental */
                   3434:     if ( nd_sugarweight )
                   3435:         r->sugar2 = ndl_weight2(r->lcm);
                   3436:   }
                   3437:   if ( r0 ) NEXT(r) = 0;
                   3438:   return r0;
                   3439: }
                   3440:
                   3441: /* kokokara */
                   3442:
                   3443: ND_pairs crit_B( ND_pairs d, int s )
                   3444: {
                   3445:   ND_pairs cur,head,prev,remove;
                   3446:   UINT *t,*tl,*lcm;
                   3447:   int td,tdl;
                   3448:
                   3449:   if ( !d ) return 0;
                   3450:   t = DL(nd_psh[s]);
                   3451:   prev = 0;
                   3452:   head = cur = d;
                   3453:   lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   3454:   while ( cur ) {
                   3455:     tl = cur->lcm;
                   3456:     if ( ndl_reducible(tl,t) ) {
                   3457:       ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
                   3458:       if ( !ndl_equal(lcm,tl) ) {
                   3459:         ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
                   3460:         if (!ndl_equal(lcm,tl)) {
                   3461:           remove = cur;
                   3462:           if ( !prev ) {
                   3463:             head = cur = NEXT(cur);
                   3464:           } else {
                   3465:             cur = NEXT(prev) = NEXT(cur);
                   3466:           }
                   3467:           FREENDP(remove);
                   3468:         } else {
                   3469:           prev = cur; cur = NEXT(cur);
                   3470:         }
                   3471:       } else {
                   3472:         prev = cur; cur = NEXT(cur);
                   3473:       }
                   3474:     } else {
                   3475:       prev = cur; cur = NEXT(cur);
                   3476:     }
                   3477:   }
                   3478:   return head;
                   3479: }
                   3480:
                   3481: ND_pairs crit_M( ND_pairs d1 )
                   3482: {
                   3483:     ND_pairs e,d2,d3,dd,p;
                   3484:     UINT *id,*jd;
                   3485:
                   3486:     if ( !d1 ) return d1;
                   3487:     for ( dd = 0, e = d1; e; e = d3 ) {
                   3488:         if ( !(d2 = NEXT(e)) ) {
                   3489:             NEXT(e) = dd;
                   3490:             return e;
                   3491:         }
                   3492:         id = LCM(e);
                   3493:         for ( d3 = 0; d2; d2 = p ) {
                   3494:             p = NEXT(d2);
                   3495:             jd = LCM(d2);
                   3496:             if ( ndl_equal(jd,id) )
                   3497:                 ;
                   3498:             else if ( TD(jd) > TD(id) )
                   3499:                 if ( ndl_reducible(jd,id) ) continue;
                   3500:                 else ;
                   3501:             else if ( ndl_reducible(id,jd) ) goto delit;
                   3502:             NEXT(d2) = d3;
                   3503:             d3 = d2;
                   3504:         }
                   3505:         NEXT(e) = dd;
                   3506:         dd = e;
                   3507:         continue;
                   3508:         /**/
                   3509:     delit:    NEXT(d2) = d3;
                   3510:         d3 = d2;
                   3511:         for ( ; p; p = d2 ) {
                   3512:             d2 = NEXT(p);
                   3513:             NEXT(p) = d3;
                   3514:             d3 = p;
                   3515:         }
                   3516:         FREENDP(e);
                   3517:     }
                   3518:     return dd;
                   3519: }
                   3520:
                   3521: ND_pairs crit_F( ND_pairs d1 )
                   3522: {
                   3523:     ND_pairs rest, head,remove;
                   3524:     ND_pairs last, p, r, w;
                   3525:     int s;
                   3526:
                   3527:     if ( !d1 ) return d1;
                   3528:     for ( head = last = 0, p = d1; NEXT(p); ) {
                   3529:         r = w = equivalent_pairs(p,&rest);
                   3530:         s = SG(r);
                   3531:         w = NEXT(w);
                   3532:         while ( w ) {
                   3533:             if ( crit_2(w->i1,w->i2) ) {
                   3534:                 r = w;
                   3535:                 w = NEXT(w);
                   3536:                 while ( w ) {
                   3537:                     remove = w;
                   3538:                     w = NEXT(w);
                   3539:                     FREENDP(remove);
                   3540:                 }
                   3541:                 break;
                   3542:             } else if ( SG(w) < s ) {
                   3543:                 FREENDP(r);
                   3544:                 r = w;
                   3545:                 s = SG(r);
                   3546:                 w = NEXT(w);
                   3547:             } else {
                   3548:                 remove = w;
                   3549:                 w = NEXT(w);
                   3550:                 FREENDP(remove);
                   3551:             }
                   3552:         }
                   3553:         if ( last ) NEXT(last) = r;
                   3554:         else head = r;
                   3555:         NEXT(last = r) = 0;
                   3556:         p = rest;
                   3557:         if ( !p ) return head;
                   3558:     }
                   3559:     if ( !last ) return p;
                   3560:     NEXT(last) = p;
                   3561:     return head;
                   3562: }
                   3563:
                   3564: int crit_2( int dp1, int dp2 )
                   3565: {
                   3566:     return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
                   3567: }
                   3568:
                   3569: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
                   3570: {
                   3571:     ND_pairs w,p,r,s;
                   3572:     UINT *d;
                   3573:
                   3574:     w = d1;
                   3575:     d = LCM(w);
                   3576:     s = NEXT(w);
                   3577:     NEXT(w) = 0;
                   3578:     for ( r = 0; s; s = p ) {
                   3579:         p = NEXT(s);
                   3580:         if ( ndl_equal(d,LCM(s)) ) {
                   3581:             NEXT(s) = w; w = s;
                   3582:         } else {
                   3583:             NEXT(s) = r; r = s;
                   3584:         }
                   3585:     }
                   3586:     *prest = r;
                   3587:     return w;
                   3588: }
                   3589:
                   3590: NODE update_base(NODE nd,int ndp)
                   3591: {
                   3592:     UINT *dl, *dln;
                   3593:     NODE last, p, head;
                   3594:
                   3595:     dl = DL(nd_psh[ndp]);
                   3596:     for ( head = last = 0, p = nd; p; ) {
                   3597:         dln = DL(nd_psh[(long)BDY(p)]);
                   3598:         if ( ndl_reducible( dln, dl ) ) {
                   3599:             p = NEXT(p);
                   3600:             if ( last ) NEXT(last) = p;
                   3601:         } else {
                   3602:             if ( !last ) head = p;
                   3603:             p = NEXT(last = p);
                   3604:         }
                   3605:     }
                   3606:     head = append_one(head,ndp);
                   3607:     return head;
                   3608: }
                   3609:
                   3610: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   3611: {
                   3612:     ND_pairs m,ml,p,l;
                   3613:     UINT *lcm;
                   3614:     int s,td,len,tlen,c,c1;
                   3615:
                   3616:     if ( !(p = NEXT(m = d)) ) {
                   3617:         *prest = p;
                   3618:         NEXT(m) = 0;
                   3619:         return m;
                   3620:     }
                   3621:     if ( !NoSugar ) {
                   3622:         if ( nd_sugarweight ) {
                   3623:             s = m->sugar2;
                   3624:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   3625:                 if ( (p->sugar2 < s)
                   3626:                     || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   3627:                     ml = l; m = p; s = m->sugar2;
                   3628:                 }
                   3629:         } else {
                   3630:             s = SG(m);
                   3631:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   3632:                 if ( (SG(p) < s)
                   3633:                     || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   3634:                     ml = l; m = p; s = SG(m);
                   3635:                 }
                   3636:         }
                   3637:     } else {
                   3638:         for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   3639:             if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
                   3640:                 ml = l; m = p; s = SG(m);
                   3641:             }
                   3642:     }
                   3643:     if ( !ml ) *prest = NEXT(m);
                   3644:     else {
                   3645:         NEXT(ml) = NEXT(m);
                   3646:         *prest = d;
                   3647:     }
                   3648:     NEXT(m) = 0;
                   3649:     return m;
                   3650: }
                   3651:
                   3652: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   3653: {
                   3654:     int msugar,i;
                   3655:     ND_pairs t,dm0,dm,dr0,dr;
                   3656:
                   3657:     if ( nd_sugarweight ) {
                   3658:         for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
                   3659:             if ( t->sugar2 < msugar ) msugar = t->sugar2;
                   3660:         dm0 = 0; dr0 = 0;
                   3661:         for ( i = 0, t = d; t; t = NEXT(t) )
                   3662:             if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
                   3663:                 if ( dm0 ) NEXT(dm) = t;
                   3664:                 else dm0 = t;
                   3665:                 dm = t;
                   3666:                 i++;
                   3667:             } else {
                   3668:                 if ( dr0 ) NEXT(dr) = t;
                   3669:                 else dr0 = t;
                   3670:                 dr = t;
                   3671:             }
                   3672:     } else {
                   3673:         for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   3674:             if ( SG(t) < msugar ) msugar = SG(t);
                   3675:         dm0 = 0; dr0 = 0;
                   3676:         for ( i = 0, t = d; t; t = NEXT(t) )
                   3677:             if ( i < nd_f4_nsp && SG(t) == msugar ) {
                   3678:                 if ( dm0 ) NEXT(dm) = t;
                   3679:                 else dm0 = t;
                   3680:                 dm = t;
                   3681:                 i++;
                   3682:             } else {
                   3683:                 if ( dr0 ) NEXT(dr) = t;
                   3684:                 else dr0 = t;
                   3685:                 dr = t;
                   3686:             }
                   3687:     }
                   3688:     NEXT(dm) = 0;
                   3689:     if ( dr0 ) NEXT(dr) = 0;
                   3690:     *prest = dr0;
                   3691:     return dm0;
                   3692: }
                   3693:
1.28      noro     3694: ND_pairs nd_minsugarp_s( ND_pairs d, ND_pairs *prest )
                   3695: {
                   3696:   int msugar;
                   3697:   ND_pairs t,last;
                   3698:
                   3699:   for ( msugar = SG(d), t = d; t; t = NEXT(t) )
                   3700:     if ( SG(t) == msugar ) last = t;
                   3701:   *prest = last->next;
                   3702:   last->next = 0;
                   3703:   return d;
                   3704: }
                   3705:
1.1       noro     3706: int nd_tdeg(NDV c)
                   3707: {
                   3708:   int wmax = 0;
                   3709:   int i,len;
                   3710:   NMV a;
                   3711:
                   3712:   len = LEN(c);
                   3713:   for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   3714:     wmax = MAX(TD(DL(a)),wmax);
                   3715:   return wmax;
                   3716: }
                   3717:
1.24      noro     3718: int ndv_newps(int m,NDV a,NDV aq)
1.1       noro     3719: {
                   3720:     int len;
                   3721:     RHist r;
                   3722:     NDV b;
                   3723:     NODE tn;
                   3724:     LIST l;
                   3725:     Z iq;
                   3726:
                   3727:     if ( nd_psn == nd_pslen ) {
                   3728:         nd_pslen *= 2;
                   3729:         nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
                   3730:         nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
                   3731:         nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
                   3732:         nd_bound = (UINT **)
                   3733:             REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
                   3734:         nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
                   3735:         nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
                   3736:     }
                   3737:     NEWRHist(r); nd_psh[nd_psn] = r;
                   3738:     nd_ps[nd_psn] = a;
                   3739:     if ( aq ) {
                   3740:         nd_ps_trace[nd_psn] = aq;
                   3741:         if ( !m ) {
                   3742:           register_hcf(aq);
                   3743:         } else if ( m == -2 ) {
                   3744:           /* do nothing */
                   3745:         } else
                   3746:           error("ndv_newps : invalud modulus");
                   3747:         nd_bound[nd_psn] = ndv_compute_bound(aq);
                   3748: #if 1
                   3749:         SG(r) = SG(aq);
                   3750: #else
                   3751:         SG(r) = nd_tdeg(aq);
                   3752: #endif
                   3753:         ndl_copy(HDL(aq),DL(r));
1.24      noro     3754:         r->sig = dup_sig(aq->sig);
1.1       noro     3755:     } else {
                   3756:         if ( !m ) register_hcf(a);
                   3757:         nd_bound[nd_psn] = ndv_compute_bound(a);
                   3758: #if 1
                   3759:         SG(r) = SG(a);
                   3760: #else
                   3761:         SG(r) = nd_tdeg(a);
                   3762: #endif
                   3763:         ndl_copy(HDL(a),DL(r));
1.24      noro     3764:         r->sig = dup_sig(a->sig);
1.1       noro     3765:     }
                   3766:     if ( nd_demand ) {
                   3767:         if ( aq ) {
                   3768:             ndv_save(nd_ps_trace[nd_psn],nd_psn);
                   3769:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   3770:             nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   3771:             nd_ps_trace[nd_psn] = 0;
                   3772:         } else {
                   3773:             ndv_save(nd_ps[nd_psn],nd_psn);
                   3774:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
                   3775:             nd_ps[nd_psn] = 0;
                   3776:         }
                   3777:     }
                   3778:     if ( nd_gentrace ) {
                   3779:         /* reverse the tracelist and append it to alltracelist */
                   3780:         nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6       noro     3781:         STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1       noro     3782:         MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
                   3783:     }
                   3784:     return nd_psn++;
                   3785: }
                   3786:
                   3787: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
                   3788: /* return 1 if success, 0 if failure (HC(a mod p)) */
                   3789:
1.24      noro     3790: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont,int sba)
1.1       noro     3791: {
1.6       noro     3792:   int i,j,td,len,max;
                   3793:   NODE s,s0,f0,tn;
                   3794:   UINT *d;
                   3795:   RHist r;
                   3796:   NDVI w;
                   3797:   NDV a,am;
                   3798:   union oNDC hc;
                   3799:   NODE node;
                   3800:   P hcp;
                   3801:   Z iq,jq;
                   3802:   LIST l;
                   3803:
                   3804:   nd_found = 0; nd_notfirst = 0; nd_create = 0;
                   3805:   /* initialize the tracelist */
                   3806:   nd_tracelist = 0;
                   3807:
                   3808:   for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
                   3809:   w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
                   3810:   for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
                   3811:     if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
                   3812:   if ( !dont_sort ) {
                   3813:     /* XXX heuristic */
1.26      noro     3814:     if ( !sba && !nd_ord->id && (nd_ord->ord.simple<2) )
1.6       noro     3815:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   3816:         (int (*)(const void *,const void *))ndvi_compare_rev);
                   3817:     else
                   3818:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   3819:         (int (*)(const void *,const void *))ndvi_compare);
                   3820:   }
                   3821:   nd_pslen = 2*nd_psn;
                   3822:   nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3823:   nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3824:   nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3825:   nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3826:   nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
                   3827:   nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
                   3828:   nd_hcf = 0;
1.1       noro     3829:
1.6       noro     3830:   if ( trace && nd_vc )
                   3831:     makesubst(nd_vc,&nd_subst);
                   3832:   else
                   3833:     nd_subst = 0;
1.1       noro     3834:
1.6       noro     3835:   if ( !nd_red )
                   3836:     nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   3837:   for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
                   3838:   for ( i = 0; i < nd_psn; i++ ) {
                   3839:     hc = HCU(w[i].p);
                   3840:     if ( trace ) {
                   3841:       if ( mod == -2 ) {
                   3842:         /* over a large finite field */
                   3843:         /* trace = small modulus */
                   3844:         a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
                   3845:         ndv_mod(-2,a);
                   3846:         if ( !dont_removecont) ndv_removecont(-2,a);
                   3847:         am = nd_ps[i] = ndv_dup(trace,w[i].p);
                   3848:         ndv_mod(trace,am);
                   3849:       if ( DL_COMPARE(HDL(am),HDL(a)) )
                   3850:         return 0;
                   3851:         ndv_removecont(trace,am);
                   3852:       } else {
                   3853:         a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
                   3854:         if ( !dont_removecont) ndv_removecont(0,a);
                   3855:         register_hcf(a);
                   3856:         am = nd_ps[i] = ndv_dup(mod,a);
                   3857:         ndv_mod(mod,am);
                   3858:       if ( DL_COMPARE(HDL(am),HDL(a)) )
                   3859:         return 0;
                   3860:         ndv_removecont(mod,am);
                   3861:       }
                   3862:     } else {
                   3863:       a = nd_ps[i] = ndv_dup(mod,w[i].p);
                   3864:       if ( mod || !dont_removecont ) ndv_removecont(mod,a);
                   3865:       if ( !mod ) register_hcf(a);
1.1       noro     3866:     }
1.6       noro     3867:     if ( nd_gentrace ) {
                   3868:       STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
                   3869:       /* exact division */
1.1       noro     3870:       if ( !dont_removecont )
1.6       noro     3871:         ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
                   3872:       MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
                   3873:     }
                   3874:     NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
                   3875:     nd_bound[i] = ndv_compute_bound(a);
                   3876:     nd_psh[i] = r;
                   3877:     if ( nd_demand ) {
                   3878:       if ( trace ) {
                   3879:         ndv_save(nd_ps_trace[i],i);
                   3880:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   3881:         nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   3882:         nd_ps_trace[i] = 0;
                   3883:       } else {
                   3884:         ndv_save(nd_ps[i],i);
                   3885:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
                   3886:         nd_ps[i] = 0;
                   3887:       }
1.1       noro     3888:     }
1.6       noro     3889:   }
1.24      noro     3890:   if ( sba ) {
1.27      noro     3891:     nd_sba_hm = (DL *)MALLOC(nd_psn*sizeof(DL));
1.24      noro     3892:    // setup signatures
1.27      noro     3893:     for ( i = 0; i < nd_psn; i++ ) {
                   3894:       SIG sig;
1.24      noro     3895:
1.27      noro     3896:       NEWSIG(sig); sig->pos = i;
                   3897:       nd_ps[i]->sig = sig;
                   3898:       if ( nd_demand ) nd_ps_sym[i]->sig = sig;
                   3899:         nd_psh[i]->sig = sig;
                   3900:       if ( trace ) {
                   3901:         nd_ps_trace[i]->sig = sig;
                   3902:         if ( nd_demand ) nd_ps_trace_sym[i]->sig = sig;
                   3903:       }
                   3904:       NEWDL(nd_sba_hm[i],nd_nvar);
                   3905:       _ndltodl(DL(nd_psh[i]),nd_sba_hm[i]);
                   3906:     }
1.29      noro     3907:     nd_sba_pos = (NODE *)MALLOC(nd_psn*sizeof(NODE));
                   3908:     for ( i = 0; i < nd_psn; i++ ) {
                   3909:       j = nd_psh[i]->sig->pos;
                   3910:       nd_sba_pos[j] = append_one(nd_sba_pos[j],i);
                   3911:     }
1.24      noro     3912:   }
1.6       noro     3913:   if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
                   3914:   return 1;
1.1       noro     3915: }
                   3916:
                   3917: struct order_spec *append_block(struct order_spec *spec,
                   3918:     int nv,int nalg,int ord);
                   3919:
                   3920: extern VECT current_dl_weight_vector_obj;
                   3921: static VECT prev_weight_vector_obj;
                   3922:
                   3923: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
                   3924:     struct order_spec **ord1p,LIST *f1p,NODE *alistp)
                   3925: {
                   3926:     NODE alist,t,s,r0,r,arg;
                   3927:     VL tv;
                   3928:     P poly;
                   3929:     DP d;
                   3930:     Alg alpha,dp;
                   3931:     DAlg inv,da,hc;
                   3932:     MP m;
                   3933:     int i,nvar,nalg,n;
                   3934:     NumberField nf;
                   3935:     LIST f1,f2;
                   3936:     struct order_spec *current_spec;
                   3937:     VECT obj,obj0;
                   3938:     VECT tmp;
                   3939:
                   3940:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
                   3941:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
                   3942:
                   3943:     for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
                   3944:         NEXTNODE(alist,t); MKV(tv->v,poly);
                   3945:         MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
                   3946:         tv->v = tv->v->priv;
                   3947:     }
                   3948:     NEXT(t) = 0;
                   3949:
                   3950:     /* simplification, making polynomials monic */
                   3951:     setfield_dalg(alist);
                   3952:     obj_algtodalg((Obj)f,(Obj *)&f1);
                   3953:     for ( t = BDY(f); t; t = NEXT(t) ) {
                   3954:         initd(ord); ptod(vv,vv,(P)BDY(t),&d);
                   3955:         hc = (DAlg)BDY(d)->c;
                   3956:         if ( NID(hc) == N_DA ) {
                   3957:             invdalg(hc,&inv);
                   3958:             for ( m = BDY(d); m; m = NEXT(m) ) {
                   3959:                 muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
                   3960:             }
                   3961:         }
                   3962:         initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
                   3963:     }
                   3964:     obj_dalgtoalg((Obj)f1,(Obj *)&f);
                   3965:
                   3966:     /* append alg vars to the var list */
                   3967:     for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
                   3968:     NEXT(tv) = av;
                   3969:
                   3970:     /* append a block to ord */
                   3971:     *ord1p = append_block(ord,nvar,nalg,2);
                   3972:
                   3973:     /* create generator list */
                   3974:     nf = get_numberfield();
                   3975:     for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
                   3976:         MKAlg(nf->defpoly[i],dp);
                   3977:         MKNODE(s,dp,t); t = s;
                   3978:     }
                   3979:     MKLIST(f1,t);
                   3980:     *alistp = alist;
                   3981:     algobjtorat((Obj)f1,(Obj *)f1p);
                   3982:
                   3983:     /* creating a new weight vector */
                   3984:     prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
                   3985:     n = nvar+nalg+1;
                   3986:     MKVECT(obj,n);
                   3987:     if ( obj0 && obj0->len == nvar )
                   3988:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
                   3989:     else
                   3990:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
                   3991:     for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
                   3992:     BDY(obj)[n-1] = (pointer)ONE;
                   3993:     arg = mknode(1,obj);
                   3994:     Pdp_set_weight(arg,&tmp);
                   3995: }
                   3996:
                   3997: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
                   3998: {
                   3999:     NODE s,t,u0,u;
                   4000:     P p;
                   4001:     VL tv;
                   4002:     Obj obj;
                   4003:     VECT tmp;
                   4004:     NODE arg;
                   4005:
                   4006:     u0 = 0;
                   4007:     for ( t = r; t; t = NEXT(t) ) {
                   4008:         p = (P)BDY(t);
                   4009:         for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
                   4010:             substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
                   4011:         }
                   4012:         if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
                   4013:             NEXTNODE(u0,u);
                   4014:             BDY(u) = (pointer)p;
                   4015:         }
                   4016:     }
                   4017:     arg = mknode(1,prev_weight_vector_obj);
                   4018:     Pdp_set_weight(arg,&tmp);
                   4019:
                   4020:     return u0;
                   4021: }
                   4022:
                   4023: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
                   4024: {
                   4025:     VL tv,fv,vv,vc,av;
                   4026:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4027:     int e,max,nvar,i;
                   4028:     NDV b;
                   4029:     int ishomo,nalg,mrank,trank,wmax,len;
                   4030:   NMV a;
                   4031:     Alg alpha,dp;
                   4032:     P p,zp;
                   4033:     Q dmy;
                   4034:     LIST f1,f2,zpl;
                   4035:     Obj obj;
                   4036:     NumberField nf;
                   4037:     struct order_spec *ord1;
                   4038:     NODE tr,tl1,tl2,tl3,tl4,nzlist;
                   4039:     LIST l1,l2,l3,l4,l5;
                   4040:   int j;
                   4041:   Z jq,bpe,last_nonzero;
                   4042:     int *perm;
                   4043:     EPOS oepos;
                   4044:     int obpe,oadv,ompos,cbpe;
1.15      noro     4045:     VECT hvect;
1.1       noro     4046:
                   4047:     nd_module = 0;
                   4048:     if ( !m && Demand ) nd_demand = 1;
                   4049:     else nd_demand = 0;
                   4050:     parse_nd_option(current_option);
                   4051:
                   4052:     if ( DP_Multiple )
                   4053:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4054: #if 0
                   4055:     ndv_alloc = 0;
                   4056: #endif
                   4057:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4058:     if ( m && nd_vc )
                   4059:        error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
                   4060:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4061:     switch ( ord->id ) {
                   4062:         case 1:
                   4063:             if ( ord->nv != nvar )
                   4064:                 error("nd_{gr,f4} : invalid order specification");
                   4065:             break;
                   4066:         default:
                   4067:             break;
                   4068:     }
                   4069:     nd_nalg = 0;
                   4070:     av = 0;
                   4071:     if ( !m ) {
                   4072:         get_algtree((Obj)f,&av);
                   4073:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4074:         nd_ntrans = nvar;
                   4075:         nd_nalg = nalg;
                   4076:         /* #i -> t#i */
                   4077:         if ( nalg ) {
                   4078:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4079:             ord = ord1;
                   4080:             f = f1;
                   4081:         }
                   4082:         nvar += nalg;
                   4083:     }
                   4084:     nd_init_ord(ord);
                   4085:     mrank = 0;
                   4086:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4087:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4088:             if ( nd_module ) {
1.16      noro     4089:                 if ( OID(BDY(t)) == O_DPM ) {
                   4090:                   e = dpm_getdeg((DPM)BDY(t),&trank);
                   4091:                   max = MAX(e,max);
                   4092:                   mrank = MAX(mrank,trank);
                   4093:                 } else {
                   4094:                   s = BDY((LIST)BDY(t));
                   4095:                   trank = length(s);
                   4096:                   mrank = MAX(mrank,trank);
                   4097:                   for ( ; s; s = NEXT(s) ) {
                   4098:                       e = getdeg(tv->v,(P)BDY(s));
                   4099:                       max = MAX(e,max);
                   4100:                   }
1.1       noro     4101:                 }
                   4102:             } else {
                   4103:                 e = getdeg(tv->v,(P)BDY(t));
                   4104:                 max = MAX(e,max);
                   4105:             }
                   4106:         }
                   4107:     nd_setup_parameters(nvar,nd_nzlist?0:max);
                   4108:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   4109:     ishomo = 1;
                   4110:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4111:       if ( nd_module ) {
1.16      noro     4112:         if ( OID(BDY(t)) == O_DPM ) {
                   4113:           Z cont;
                   4114:           DPM zdpm;
                   4115:
                   4116:           if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
                   4117:           else zdpm = (DPM)BDY(t);
                   4118:           b = (pointer)dpmtondv(m,zdpm);
                   4119:         } else {
                   4120:           if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   4121:           else zpl = (LIST)BDY(t);
1.1       noro     4122:           b = (pointer)pltondv(CO,vv,zpl);
1.16      noro     4123:         }
1.1       noro     4124:       } else {
                   4125:         if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   4126:         else zp = (P)BDY(t);
                   4127:         b = (pointer)ptondv(CO,vv,zp);
                   4128:       }
                   4129:       if ( ishomo )
                   4130:         ishomo = ishomo && ndv_ishomo(b);
                   4131:       if ( m ) ndv_mod(m,b);
                   4132:       if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4133:     }
                   4134:     if ( fd0 ) NEXT(fd) = 0;
                   4135:
                   4136:   if ( !ishomo && homo ) {
                   4137:         for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   4138:             b = (NDV)BDY(t); len = LEN(b);
                   4139:             for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   4140:                 wmax = MAX(TD(DL(a)),wmax);
                   4141:         }
                   4142:         homogenize_order(ord,nvar,&ord1);
                   4143:         nd_init_ord(ord1);
                   4144:         nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   4145:         for ( t = fd0; t; t = NEXT(t) )
                   4146:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   4147:     }
                   4148:
1.24      noro     4149:     ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0,0);
1.1       noro     4150:     if ( nd_gentrace ) {
                   4151:         MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   4152:     }
                   4153:   if ( nd_splist ) {
                   4154:     *rp = compute_splist();
                   4155:     return;
                   4156:   }
                   4157:   if ( nd_check_splist ) {
                   4158:         if ( f4 ) {
                   4159:             if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
                   4160:             else *rp = 0;
                   4161:         } else {
                   4162:             if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
                   4163:             else *rp = 0;
                   4164:         }
                   4165:     return;
                   4166:   }
                   4167:     x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
                   4168:   if ( !x ) {
                   4169:     *rp = 0; return;
                   4170:   }
1.15      noro     4171:   if ( nd_gentrace ) {
                   4172:     MKVECT(hvect,nd_psn);
                   4173:     for ( i = 0; i < nd_psn; i++ )
                   4174:        ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   4175:   }
1.1       noro     4176:   if ( !ishomo && homo ) {
                   4177:        /* dehomogenization */
                   4178:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   4179:     nd_init_ord(ord);
                   4180:     nd_setup_parameters(nvar,0);
                   4181:   }
                   4182:     nd_demand = 0;
                   4183:   if ( nd_module && nd_intersect ) {
                   4184:     for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17      noro     4185:       if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1       noro     4186:         MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
                   4187:       }
                   4188:     conv_ilist(nd_demand,0,x,0);
                   4189:     goto FINAL;
                   4190:   }
                   4191:     if ( nd_gentrace  && f4 ) { nzlist = nd_alltracelist; }
                   4192:     x = ndv_reducebase(x,perm);
                   4193:     if ( nd_gentrace  && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
                   4194:     x = ndv_reduceall(m,x);
                   4195:     cbpe = nd_bpe;
                   4196:     if ( nd_gentrace && !f4 ) {
                   4197:         tl2 = nd_alltracelist; nd_alltracelist = 0;
                   4198:         ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
                   4199:         tl3 = nd_alltracelist; nd_alltracelist = 0;
                   4200:         if ( nd_gensyz ) {
                   4201:           nd_gb(m,0,1,1,0);
                   4202:             tl4 = nd_alltracelist; nd_alltracelist = 0;
                   4203:         } else tl4 = 0;
                   4204:     }
                   4205:     nd_bpe = cbpe;
                   4206:     nd_setup_parameters(nd_nvar,0);
                   4207: FINAL:
                   4208:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16      noro     4209:       NEXTNODE(r0,r);
                   4210:       if ( nd_module ) {
                   4211:         if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
                   4212:         else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
                   4213:       } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
                   4214:       else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1       noro     4215:     }
                   4216:     if ( r0 ) NEXT(r) = 0;
                   4217:     if ( !m && nd_nalg )
                   4218:         r0 = postprocess_algcoef(av,alist,r0);
                   4219:     MKLIST(*rp,r0);
                   4220:     if ( nd_gentrace ) {
                   4221:   if ( f4 ) {
1.6       noro     4222:             STOZ(16,bpe);
                   4223:             STOZ(nd_last_nonzero,last_nonzero);
1.15      noro     4224:             tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1       noro     4225:         } else {
                   4226:             tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   4227:             tl3 = reverse_node(tl3);
                   4228:             /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   4229:             for ( t = tl2; t; t = NEXT(t) ) {
                   4230:             /* s = [i,[*,j,*,*],...] */
                   4231:                 s = BDY((LIST)BDY(t));
1.6       noro     4232:                 j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     4233:                 for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     4234:                     j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     4235:                     ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   4236:                 }
                   4237:             }
                   4238:             for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6       noro     4239:                 STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     4240:             }
                   4241:             MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   4242:             MKLIST(l5,tl4);
1.6       noro     4243:             STOZ(nd_bpe,bpe);
1.15      noro     4244:             tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     4245:         }
                   4246:     }
                   4247: #if 0
                   4248:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   4249: #endif
                   4250: }
                   4251:
1.28      noro     4252: NODE nd_sba_f4(int m,int **indp);
                   4253:
                   4254: void nd_sba(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.24      noro     4255: {
                   4256:   VL tv,fv,vv,vc,av;
                   4257:   NODE fd,fd0,r,r0,t,x,s,xx;
                   4258:   int e,max,nvar,i;
                   4259:   NDV b;
                   4260:   int ishomo,nalg,wmax,len;
                   4261:   NMV a;
                   4262:   P p,zp;
                   4263:   Q dmy;
                   4264:   struct order_spec *ord1;
                   4265:   int j;
                   4266:   int *perm;
                   4267:   EPOS oepos;
                   4268:   int obpe,oadv,ompos,cbpe;
1.29      noro     4269:   struct oEGT eg0,eg1,egconv;
1.24      noro     4270:
                   4271:   nd_module = 0;
                   4272:   nd_demand = 0;
                   4273:   parse_nd_option(current_option);
1.35    ! noro     4274:   Nsamesig = 0;
1.24      noro     4275:   if ( DP_Multiple )
                   4276:     nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4277:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4278:   if ( m && nd_vc )
                   4279:     error("nd_sba : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
                   4280:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4281:   switch ( ord->id ) {
                   4282:     case 1:
                   4283:       if ( ord->nv != nvar )
                   4284:         error("nd_sba : invalid order specification");
                   4285:         break;
                   4286:       default:
                   4287:         break;
                   4288:   }
                   4289:   nd_nalg = 0;
                   4290:   nd_init_ord(ord);
                   4291:   // for SIG comparison
                   4292:   initd(ord);
                   4293:   for ( t = BDY(f), max = 1; t; t = NEXT(t) ) {
                   4294:     for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4295:       e = getdeg(tv->v,(P)BDY(t));
                   4296:       max = MAX(e,max);
                   4297:     }
                   4298:   }
1.25      noro     4299:   nd_setup_parameters(nvar,max);
1.24      noro     4300:   obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   4301:   ishomo = 1;
                   4302:   for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4303:     if ( !m ) ptozp((P)BDY(t),1,&dmy,&zp);
                   4304:     else zp = (P)BDY(t);
                   4305:     b = (pointer)ptondv(CO,vv,zp);
                   4306:     if ( ishomo )
                   4307:       ishomo = ishomo && ndv_ishomo(b);
                   4308:     if ( m ) ndv_mod(m,b);
                   4309:     if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4310:   }
                   4311:   if ( fd0 ) NEXT(fd) = 0;
                   4312:
                   4313:   if ( !ishomo && homo ) {
                   4314:     for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   4315:       b = (NDV)BDY(t); len = LEN(b);
                   4316:       for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   4317:         wmax = MAX(TD(DL(a)),wmax);
                   4318:       }
                   4319:       homogenize_order(ord,nvar,&ord1);
                   4320:       nd_init_ord(ord1);
                   4321:       nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   4322:       for ( t = fd0; t; t = NEXT(t) )
                   4323:         ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   4324:   }
                   4325:
                   4326:   ndv_setup(m,0,fd0,0,0,1);
1.28      noro     4327:   x = f4 ? nd_sba_f4(m,&perm) : nd_sba_buch(m,ishomo || homo,&perm);
1.24      noro     4328:   if ( !x ) {
                   4329:     *rp = 0; return;
                   4330:   }
                   4331:   if ( !ishomo && homo ) {
                   4332:        /* dehomogenization */
                   4333:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   4334:     nd_init_ord(ord);
                   4335:     nd_setup_parameters(nvar,0);
                   4336:   }
                   4337:   nd_demand = 0;
                   4338:   x = ndv_reducebase(x,perm);
                   4339:   x = ndv_reduceall(m,x);
                   4340:   nd_setup_parameters(nd_nvar,0);
1.29      noro     4341:   get_eg(&eg0);
1.24      noro     4342:   for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   4343:     NEXTNODE(r0,r);
                   4344:     if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
1.34      noro     4345:     else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.24      noro     4346:   }
                   4347:   if ( r0 ) NEXT(r) = 0;
                   4348:   MKLIST(*rp,r0);
1.29      noro     4349:   get_eg(&eg1); init_eg(&egconv); add_eg(&egconv,&eg0,&eg1);
                   4350:   print_eg("conv",&egconv); fprintf(asir_out,"\n");
1.24      noro     4351: }
                   4352:
1.1       noro     4353: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
                   4354: {
                   4355:     VL tv,fv,vv,vc,av;
                   4356:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4357:     int e,max,nvar,i;
                   4358:     NDV b;
                   4359:     int ishomo,nalg;
                   4360:     Alg alpha,dp;
                   4361:     P p,zp;
                   4362:     Q dmy;
                   4363:     LIST f1,f2;
                   4364:     Obj obj;
                   4365:     NumberField nf;
                   4366:     struct order_spec *ord1;
                   4367:     int *perm;
                   4368:
                   4369:     parse_nd_option(current_option);
                   4370:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4371:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4372:     switch ( ord->id ) {
                   4373:         case 1:
                   4374:             if ( ord->nv != nvar )
                   4375:                 error("nd_check : invalid order specification");
                   4376:             break;
                   4377:         default:
                   4378:             break;
                   4379:     }
                   4380:     nd_nalg = 0;
                   4381:     av = 0;
                   4382:     if ( !m ) {
                   4383:         get_algtree((Obj)f,&av);
                   4384:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4385:         nd_ntrans = nvar;
                   4386:         nd_nalg = nalg;
                   4387:         /* #i -> t#i */
                   4388:         if ( nalg ) {
                   4389:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4390:             ord = ord1;
                   4391:             f = f1;
                   4392:         }
                   4393:         nvar += nalg;
                   4394:     }
                   4395:     nd_init_ord(ord);
                   4396:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4397:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4398:             e = getdeg(tv->v,(P)BDY(t));
                   4399:             max = MAX(e,max);
                   4400:         }
                   4401:     nd_setup_parameters(nvar,max);
                   4402:     ishomo = 1;
                   4403:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4404:         ptozp((P)BDY(t),1,&dmy,&zp);
                   4405:         b = (pointer)ptondv(CO,vv,zp);
                   4406:         if ( ishomo )
                   4407:             ishomo = ishomo && ndv_ishomo(b);
                   4408:         if ( m ) ndv_mod(m,b);
                   4409:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4410:     }
                   4411:     if ( fd0 ) NEXT(fd) = 0;
1.24      noro     4412:     ndv_setup(m,0,fd0,0,1,0);
1.1       noro     4413:     for ( x = 0, i = 0; i < nd_psn; i++ )
                   4414:         x = update_base(x,i);
                   4415:     if ( do_check ) {
                   4416:         x = nd_gb(m,ishomo,1,0,&perm);
                   4417:         if ( !x ) {
                   4418:             *rp = 0;
                   4419:             return;
                   4420:         }
                   4421:     } else {
                   4422: #if 0
                   4423:     /* bug ? */
                   4424:         for ( t = x; t; t = NEXT(t) )
                   4425:             BDY(t) = (pointer)nd_ps[(long)BDY(t)];
                   4426: #else
                   4427:     conv_ilist(0,0,x,&perm);
                   4428: #endif
                   4429:     }
                   4430:     x = ndv_reducebase(x,perm);
                   4431:     x = ndv_reduceall(m,x);
                   4432:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   4433:         NEXTNODE(r0,r);
                   4434:         BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   4435:     }
                   4436:     if ( r0 ) NEXT(r) = 0;
                   4437:     if ( !m && nd_nalg )
                   4438:         r0 = postprocess_algcoef(av,alist,r0);
                   4439:     MKLIST(*rp,r0);
                   4440: }
                   4441:
                   4442: NDV recompute_trace(NODE trace,NDV *p,int m);
                   4443: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
                   4444:
                   4445: NDV recompute_trace(NODE ti,NDV *p,int mod)
                   4446: {
                   4447:   int c,c1,c2,i;
                   4448:   NM mul,m,tail;
                   4449:   ND d,r,rm;
                   4450:   NODE sj;
                   4451:   NDV red;
                   4452:   Obj mj;
                   4453:
                   4454:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   4455:   CM(mul) = 1;
                   4456:   tail = 0;
                   4457:   for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
                   4458:     sj = BDY((LIST)BDY(ti));
                   4459:     if ( ARG0(sj) ) {
1.6       noro     4460:       red = p[ZTOS((Q)ARG1(sj))];
1.1       noro     4461:       mj = (Obj)ARG2(sj);
                   4462:       if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
                   4463:       else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
                   4464:       rm = ndv_mul_nm(mod,mul,red);
                   4465:       if ( !r ) r = rm;
                   4466:       else {
                   4467:         for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
                   4468:           if ( d ) {
                   4469:             NEXT(tail) = m; tail = m; LEN(d)++;
                   4470:           } else {
                   4471:             MKND(nd_nvar,m,1,d); tail = BDY(d);
                   4472:           }
                   4473:         }
                   4474:         if ( !m ) return 0; /* failure */
                   4475:                 else {
                   4476:           BDY(r) = m;
                   4477:                     if ( mod > 0 || mod == -1 ) {
                   4478:             c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
                   4479:             DMAR(c1,c2,0,mod,c);
                   4480:             nd_mul_c(mod,rm,c);
                   4481:                     } else {
                   4482:                       Z t,u;
                   4483:
                   4484:                       chsgnlf(HCZ(r),&t);
                   4485:                       divlf(t,HCZ(rm),&u);
                   4486:                       nd_mul_c_lf(rm,u);
                   4487:                     }
                   4488:           r = nd_add(mod,r,rm);
                   4489:         }
                   4490:           }
                   4491:      }
                   4492:   }
                   4493:   if ( tail ) NEXT(tail) = 0;
                   4494:   d = nd_add(mod,d,r);
                   4495:   nd_mul_c(mod,d,invm(HCM(d),mod));
                   4496:   return ndtondv(mod,d);
                   4497: }
                   4498:
                   4499: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
                   4500: {
                   4501:     VL tv,fv,vv,vc,av;
                   4502:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4503:     int e,max,nvar,i;
                   4504:     NDV b;
                   4505:     int ishomo,nalg;
                   4506:     Alg alpha,dp;
                   4507:     P p,zp;
                   4508:     Q dmy;
                   4509:     LIST f1,f2;
                   4510:     Obj obj;
                   4511:     NumberField nf;
                   4512:     struct order_spec *ord1;
                   4513:   NODE permtrace,intred,ind,perm,trace,ti;
                   4514:   int len,n,j;
                   4515:   NDV *db,*pb;
                   4516:
                   4517:     parse_nd_option(current_option);
                   4518:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4519:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4520:     switch ( ord->id ) {
                   4521:         case 1:
                   4522:             if ( ord->nv != nvar )
                   4523:                 error("nd_check : invalid order specification");
                   4524:             break;
                   4525:         default:
                   4526:             break;
                   4527:     }
                   4528:     nd_init_ord(ord);
1.6       noro     4529:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     4530:     nd_setup_parameters(nvar,0);
                   4531:
                   4532:   len = length(BDY(f));
                   4533:   db = (NDV *)MALLOC(len*sizeof(NDV *));
                   4534:   for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
                   4535:       ptozp((P)BDY(t),1,&dmy,&zp);
                   4536:       b = ptondv(CO,vv,zp);
                   4537:         ndv_mod(m,b);
                   4538:     ndv_mul_c(m,b,invm(HCM(b),m));
                   4539:     db[i] = b;
                   4540:     }
                   4541:
                   4542:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   4543:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   4544:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   4545:   perm = BDY((LIST)ARG0(permtrace));
                   4546:   trace = NEXT(permtrace);
                   4547:
                   4548:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     4549:     j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1       noro     4550:     if ( j > i ) i = j;
                   4551:   }
                   4552:   n = i+1;
                   4553:   pb = (NDV *)MALLOC(n*sizeof(NDV *));
                   4554:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   4555:     ti = BDY((LIST)BDY(t));
1.6       noro     4556:     pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1       noro     4557:   }
                   4558:   for ( t = trace; t; t = NEXT(t) ) {
                   4559:     ti = BDY((LIST)BDY(t));
1.6       noro     4560:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   4561:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     4562:       if ( DP_Print ) {
                   4563:            fprintf(asir_out,"."); fflush(asir_out);
                   4564:       }
                   4565:   }
                   4566:   for ( t = intred; t; t = NEXT(t) ) {
                   4567:     ti = BDY((LIST)BDY(t));
1.6       noro     4568:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   4569:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     4570:       if ( DP_Print ) {
                   4571:            fprintf(asir_out,"*"); fflush(asir_out);
                   4572:       }
                   4573:   }
                   4574:     for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
                   4575:         NEXTNODE(r0,r);
1.6       noro     4576:     b = pb[ZTOS((Q)BDY(t))];
1.1       noro     4577:         ndv_mul_c(m,b,invm(HCM(b),m));
                   4578: #if 0
1.6       noro     4579:         BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1       noro     4580: #else
1.6       noro     4581:         BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1       noro     4582: #endif
                   4583:     }
                   4584:     if ( r0 ) NEXT(r) = 0;
                   4585:     MKLIST(*rp,r0);
                   4586:     if ( DP_Print ) fprintf(asir_out,"\n");
                   4587: }
                   4588:
1.16      noro     4589: 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     4590: {
                   4591:     VL tv,fv,vv,vc,av;
                   4592:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   4593:     int m,nocheck,nvar,mindex,e,max;
                   4594:     NDV c;
                   4595:     NMV a;
                   4596:     P p,zp;
                   4597:     Q dmy;
                   4598:     EPOS oepos;
                   4599:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   4600:     Alg alpha,dp;
                   4601:     P poly;
                   4602:     LIST f1,f2,zpl;
                   4603:     Obj obj;
                   4604:     NumberField nf;
                   4605:     struct order_spec *ord1;
                   4606:     struct oEGT eg_check,eg0,eg1;
                   4607:     NODE tr,tl1,tl2,tl3,tl4;
                   4608:     LIST l1,l2,l3,l4,l5;
                   4609:     int *perm;
                   4610:     int j,ret;
                   4611:     Z jq,bpe;
1.15      noro     4612:     VECT hvect;
1.1       noro     4613:
                   4614:     nd_module = 0;
                   4615:     nd_lf = 0;
                   4616:     parse_nd_option(current_option);
                   4617:     if ( nd_lf ) {
                   4618:       if ( f4 )
                   4619:         nd_f4_lf_trace(f,v,trace,homo,ord,rp);
                   4620:       else
                   4621:         error("nd_gr_trace is not implemented yet over a large finite field");
                   4622:       return;
                   4623:     }
                   4624:     if ( DP_Multiple )
                   4625:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4626:
                   4627:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4628:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4629:     switch ( ord->id ) {
                   4630:         case 1:
                   4631:             if ( ord->nv != nvar )
                   4632:                 error("nd_gr_trace : invalid order specification");
                   4633:             break;
                   4634:         default:
                   4635:             break;
                   4636:     }
                   4637:
                   4638:     get_algtree((Obj)f,&av);
                   4639:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4640:     nd_ntrans = nvar;
                   4641:     nd_nalg = nalg;
                   4642:     /* #i -> t#i */
                   4643:     if ( nalg ) {
                   4644:         preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4645:         ord = ord1;
                   4646:         f = f1;
                   4647:     }
                   4648:     nvar += nalg;
                   4649:
                   4650:     nocheck = 0;
                   4651:     mindex = 0;
                   4652:
                   4653:     if ( Demand ) nd_demand = 1;
                   4654:     else nd_demand = 0;
                   4655:
                   4656:     /* setup modulus */
                   4657:     if ( trace < 0 ) {
                   4658:         trace = -trace;
                   4659:         nocheck = 1;
                   4660:     }
                   4661:     m = trace > 1 ? trace : get_lprime(mindex);
                   4662:     nd_init_ord(ord);
                   4663:     mrank = 0;
                   4664:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4665:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4666:             if ( nd_module ) {
1.16      noro     4667:               if ( OID(BDY(t)) == O_DPM ) {
                   4668:                 e = dpm_getdeg((DPM)BDY(t),&trank);
                   4669:                 max = MAX(e,max);
                   4670:                 mrank = MAX(mrank,trank);
                   4671:               } else {
1.1       noro     4672:                 s = BDY((LIST)BDY(t));
                   4673:                 trank = length(s);
                   4674:                 mrank = MAX(mrank,trank);
                   4675:                 for ( ; s; s = NEXT(s) ) {
                   4676:                     e = getdeg(tv->v,(P)BDY(s));
                   4677:                     max = MAX(e,max);
                   4678:                 }
1.16      noro     4679:               }
1.1       noro     4680:             } else {
                   4681:                 e = getdeg(tv->v,(P)BDY(t));
                   4682:                 max = MAX(e,max);
                   4683:             }
                   4684:         }
                   4685:     nd_setup_parameters(nvar,max);
                   4686:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   4687:     ishomo = 1;
                   4688:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4689:         if ( nd_module ) {
1.16      noro     4690:           if ( OID(BDY(t)) == O_DPM ) {
                   4691:             Z cont;
                   4692:             DPM zdpm;
                   4693:
1.17      noro     4694:             if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16      noro     4695:             else zdpm = (DPM)BDY(t);
                   4696:             c = (pointer)dpmtondv(m,zdpm);
                   4697:           } else {
                   4698:             if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   4699:             else zpl = (LIST)BDY(t);
1.1       noro     4700:             c = (pointer)pltondv(CO,vv,zpl);
1.16      noro     4701:           }
1.1       noro     4702:         } else {
1.16      noro     4703:           if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   4704:           else zp = (P)BDY(t);
                   4705:           c = (pointer)ptondv(CO,vv,zp);
1.1       noro     4706:         }
                   4707:         if ( ishomo )
                   4708:             ishomo = ishomo && ndv_ishomo(c);
                   4709:         if ( c ) {
                   4710:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   4711:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   4712:         }
                   4713:     }
                   4714:     if ( in0 ) NEXT(in) = 0;
                   4715:     if ( fd0 ) NEXT(fd) = 0;
                   4716:     if ( !ishomo && homo ) {
                   4717:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   4718:             c = (NDV)BDY(t); len = LEN(c);
                   4719:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   4720:                 wmax = MAX(TD(DL(a)),wmax);
                   4721:         }
                   4722:         homogenize_order(ord,nvar,&ord1);
                   4723:         nd_init_ord(ord1);
                   4724:         nd_setup_parameters(nvar+1,wmax);
                   4725:         for ( t = fd0; t; t = NEXT(t) )
                   4726:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   4727:     }
                   4728:     if ( MaxDeg > 0 ) nocheck = 1;
                   4729:     while ( 1 ) {
                   4730:     tl1 = tl2 = tl3 = tl4 = 0;
                   4731:         if ( Demand )
                   4732:             nd_demand = 1;
1.24      noro     4733:         ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1       noro     4734:         if ( nd_gentrace ) {
                   4735:             MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   4736:         }
                   4737:         if ( ret )
                   4738:             cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
                   4739:         if ( !ret || !cand ) {
                   4740:             /* failure */
                   4741:             if ( trace > 1 ) { *rp = 0; return; }
                   4742:             else m = get_lprime(++mindex);
                   4743:             continue;
                   4744:         }
1.15      noro     4745:         if ( nd_gentrace ) {
                   4746:           MKVECT(hvect,nd_psn);
                   4747:           for ( i = 0; i < nd_psn; i++ )
                   4748:              ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   4749:         }
1.1       noro     4750:         if ( !ishomo && homo ) {
                   4751:             /* dehomogenization */
                   4752:             for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   4753:             nd_init_ord(ord);
                   4754:             nd_setup_parameters(nvar,0);
                   4755:         }
                   4756:         nd_demand = 0;
                   4757:         cand = ndv_reducebase(cand,perm);
                   4758:         if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
                   4759:         cand = ndv_reduceall(0,cand);
                   4760:         cbpe = nd_bpe;
                   4761:         if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
                   4762:         get_eg(&eg0);
                   4763:         if ( nocheck )
                   4764:             break;
                   4765:         if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   4766:             if ( nd_gentrace ) {
                   4767:           tl3 = nd_alltracelist; nd_alltracelist = 0;
                   4768:         } else tl3 = 0;
                   4769:             /* gbcheck : cand is a GB of Id(cand) ? */
                   4770:             if ( nd_vc || nd_gentrace || nd_gensyz )
                   4771:               ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
                   4772:             else
                   4773:               ret = nd_f4(0,1,0)!=0;
                   4774:             if ( nd_gentrace && nd_gensyz ) {
                   4775:           tl4 = nd_alltracelist; nd_alltracelist = 0;
                   4776:         } else tl4 = 0;
                   4777:     }
                   4778:     if ( ret ) break;
                   4779:         else if ( trace > 1 ) {
                   4780:             /* failure */
                   4781:             *rp = 0; return;
                   4782:         } else {
                   4783:             /* try the next modulus */
                   4784:             m = get_lprime(++mindex);
                   4785:             /* reset the parameters */
                   4786:             if ( !ishomo && homo ) {
                   4787:                 nd_init_ord(ord1);
                   4788:                 nd_setup_parameters(nvar+1,wmax);
                   4789:             } else {
                   4790:                 nd_init_ord(ord);
                   4791:                 nd_setup_parameters(nvar,max);
                   4792:             }
                   4793:         }
                   4794:     }
                   4795:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   4796:     if ( DP_Print )
1.6       noro     4797:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     4798:     /* dp->p */
                   4799:     nd_bpe = cbpe;
                   4800:     nd_setup_parameters(nd_nvar,0);
                   4801:     for ( r = cand; r; r = NEXT(r) ) {
1.16      noro     4802:       if ( nd_module ) {
1.17      noro     4803:         if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16      noro     4804:         else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17      noro     4805:       } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
                   4806:       else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1       noro     4807:     }
                   4808:     if ( nd_nalg )
                   4809:         cand = postprocess_algcoef(av,alist,cand);
                   4810:     MKLIST(*rp,cand);
                   4811:     if ( nd_gentrace ) {
                   4812:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   4813:     tl3 = reverse_node(tl3);
                   4814:     /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   4815:         for ( t = tl2; t; t = NEXT(t) ) {
                   4816:       /* s = [i,[*,j,*,*],...] */
                   4817:             s = BDY((LIST)BDY(t));
1.6       noro     4818:             j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     4819:       for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     4820:                 j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     4821:         ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   4822:             }
                   4823:     }
                   4824:     for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6       noro     4825:         STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     4826:     }
                   4827:         MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   4828:     MKLIST(l5,tl4);
1.6       noro     4829:       STOZ(nd_bpe,bpe);
1.15      noro     4830:         tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     4831:     }
                   4832: }
                   4833:
                   4834: /* XXX : module element is not considered  */
                   4835:
                   4836: void dltondl(int n,DL dl,UINT *r)
                   4837: {
                   4838:     UINT *d;
                   4839:     int i,j,l,s,ord_l;
                   4840:     struct order_pair *op;
                   4841:
                   4842:     d = (unsigned int *)dl->d;
                   4843:     for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
                   4844:     if ( nd_blockmask ) {
                   4845:         l = nd_blockmask->n;
                   4846:         op = nd_blockmask->order_pair;
                   4847:         for ( j = 0, s = 0; j < l; j++ ) {
                   4848:             ord_l = op[j].length;
                   4849:             for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
                   4850:         }
                   4851:         TD(r) = ndl_weight(r);
                   4852:         ndl_weight_mask(r);
                   4853:     } else {
                   4854:         for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
                   4855:         TD(r) = ndl_weight(r);
                   4856:     }
                   4857: }
                   4858:
                   4859: DL ndltodl(int n,UINT *ndl)
                   4860: {
                   4861:     DL dl;
                   4862:     int *d;
                   4863:     int i,j,l,s,ord_l;
                   4864:     struct order_pair *op;
                   4865:
1.33      noro     4866:     NEWDL_NOINIT(dl,n);
1.1       noro     4867:     dl->td = TD(ndl);
                   4868:     d = dl->d;
                   4869:     if ( nd_blockmask ) {
                   4870:         l = nd_blockmask->n;
                   4871:         op = nd_blockmask->order_pair;
                   4872:         for ( j = 0, s = 0; j < l; j++ ) {
                   4873:             ord_l = op[j].length;
                   4874:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   4875:         }
                   4876:     } else {
                   4877:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   4878:     }
                   4879:     return dl;
                   4880: }
                   4881:
1.24      noro     4882: void _ndltodl(UINT *ndl,DL dl)
                   4883: {
                   4884:     int *d;
                   4885:     int i,j,l,s,ord_l,n;
                   4886:     struct order_pair *op;
                   4887:
                   4888:     n = nd_nvar;
                   4889:     dl->td = TD(ndl);
                   4890:     d = dl->d;
                   4891:     if ( nd_blockmask ) {
                   4892:         l = nd_blockmask->n;
                   4893:         op = nd_blockmask->order_pair;
                   4894:         for ( j = 0, s = 0; j < l; j++ ) {
                   4895:             ord_l = op[j].length;
                   4896:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   4897:         }
                   4898:     } else {
                   4899:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   4900:     }
                   4901: }
                   4902:
1.1       noro     4903: void nmtodp(int mod,NM m,DP *r)
                   4904: {
                   4905:     DP dp;
                   4906:     MP mr;
                   4907:
                   4908:     NEWMP(mr);
                   4909:     mr->dl = ndltodl(nd_nvar,DL(m));
                   4910:     mr->c = (Obj)ndctop(mod,m->c);
                   4911:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   4912:     *r = dp;
                   4913: }
                   4914:
1.15      noro     4915: void ndltodp(UINT *d,DP *r)
                   4916: {
                   4917:     DP dp;
                   4918:     MP mr;
                   4919:
                   4920:     NEWMP(mr);
                   4921:     mr->dl = ndltodl(nd_nvar,d);
                   4922:     mr->c = (Obj)ONE;
                   4923:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   4924:     *r = dp;
                   4925: }
                   4926:
1.1       noro     4927: void ndl_print(UINT *dl)
                   4928: {
                   4929:     int n;
                   4930:     int i,j,l,ord_l,s,s0;
                   4931:     struct order_pair *op;
                   4932:
                   4933:     n = nd_nvar;
                   4934:     printf("<<");
                   4935:     if ( nd_blockmask ) {
                   4936:         l = nd_blockmask->n;
                   4937:         op = nd_blockmask->order_pair;
                   4938:         for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   4939:             ord_l = op[j].length;
                   4940:             for ( i = 0; i < ord_l; i++, s++ )
                   4941:                 printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
                   4942:         }
                   4943:     } else {
                   4944:         for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
                   4945:     }
                   4946:     printf(">>");
                   4947:     if ( nd_module && MPOS(dl) )
                   4948:         printf("*e%d",MPOS(dl));
                   4949: }
                   4950:
                   4951: void nd_print(ND p)
                   4952: {
                   4953:     NM m;
                   4954:
                   4955:     if ( !p )
                   4956:         printf("0\n");
                   4957:     else {
                   4958:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   4959:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     4960:             else printf("+%ld*",CM(m));
1.1       noro     4961:             ndl_print(DL(m));
                   4962:         }
                   4963:         printf("\n");
                   4964:     }
                   4965: }
                   4966:
                   4967: void nd_print_q(ND p)
                   4968: {
                   4969:     NM m;
                   4970:
                   4971:     if ( !p )
                   4972:         printf("0\n");
                   4973:     else {
                   4974:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   4975:             printf("+");
1.6       noro     4976:             printexpr(CO,(Obj)CZ(m));
1.1       noro     4977:             printf("*");
                   4978:             ndl_print(DL(m));
                   4979:         }
                   4980:         printf("\n");
                   4981:     }
                   4982: }
                   4983:
                   4984: void ndp_print(ND_pairs d)
                   4985: {
                   4986:     ND_pairs t;
                   4987:
                   4988:     for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
                   4989:     printf("\n");
                   4990: }
                   4991:
                   4992: void nd_removecont(int mod,ND p)
                   4993: {
                   4994:     int i,n;
                   4995:     Z *w;
                   4996:     NM m;
                   4997:     struct oVECT v;
                   4998:
                   4999:     if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
                   5000:     else if ( mod == -2 ) {
                   5001:       Z inv;
                   5002:       divlf(ONE,HCZ(p),&inv);
                   5003:       nd_mul_c_lf(p,inv);
                   5004:     } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
                   5005:     else {
                   5006:         for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   5007:         w = (Z *)MALLOC(n*sizeof(Q));
                   5008:         v.len = n;
                   5009:         v.body = (pointer *)w;
1.6       noro     5010:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5011:         removecont_array((P *)w,n,1);
1.6       noro     5012:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5013:     }
                   5014: }
                   5015:
                   5016: void nd_removecont2(ND p1,ND p2)
                   5017: {
                   5018:     int i,n1,n2,n;
                   5019:     Z *w;
                   5020:     NM m;
                   5021:     struct oVECT v;
                   5022:
                   5023:     n1 = nd_length(p1);
                   5024:     n2 = nd_length(p2);
                   5025:     n = n1+n2;
                   5026:     w = (Z *)MALLOC(n*sizeof(Q));
                   5027:     v.len = n;
                   5028:     v.body = (pointer *)w;
                   5029:     i = 0;
                   5030:     if ( p1 )
1.6       noro     5031:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5032:     if ( p2 )
1.6       noro     5033:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5034:     removecont_array((P *)w,n,1);
                   5035:     i = 0;
                   5036:     if ( p1 )
1.6       noro     5037:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5038:     if ( p2 )
1.6       noro     5039:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5040: }
                   5041:
                   5042: void ndv_removecont(int mod,NDV p)
                   5043: {
                   5044:     int i,len,all_p;
                   5045:     Z *c;
                   5046:     P *w;
                   5047:     Z dvr,t;
                   5048:     P g,cont,tp;
                   5049:     NMV m;
                   5050:
                   5051:     if ( mod == -1 )
                   5052:         ndv_mul_c(mod,p,_invsf(HCM(p)));
                   5053:     else if ( mod == -2 ) {
                   5054:       Z inv;
                   5055:       divlf(ONE,HCZ(p),&inv);
                   5056:       ndv_mul_c_lf(p,inv);
                   5057:     } else if ( mod )
                   5058:         ndv_mul_c(mod,p,invm(HCM(p),mod));
                   5059:     else {
                   5060:         len = p->len;
                   5061:         w = (P *)MALLOC(len*sizeof(P));
                   5062:         c = (Z *)MALLOC(len*sizeof(Q));
                   5063:         for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
                   5064:             ptozp(CP(m),1,(Q *)&c[i],&w[i]);
                   5065:             all_p = all_p && !NUM(w[i]);
                   5066:         }
                   5067:         if ( all_p ) {
                   5068:             qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
                   5069:             mulp(nd_vc,(P)dvr,g,&cont);
                   5070:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5071:                 divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
                   5072:             }
                   5073:         } else {
                   5074:             sortbynm((Q *)c,len);
                   5075:             qltozl((Q *)c,len,&dvr);
                   5076:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5077:                 divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
                   5078:             }
                   5079:         }
                   5080:     }
                   5081: }
                   5082:
                   5083: /* koko */
                   5084:
                   5085: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
                   5086: {
                   5087:     int len,i,max;
                   5088:     NMV m,mr0,mr,t;
                   5089:
                   5090:     len = p->len;
1.14      noro     5091:     for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1       noro     5092:         max = MAX(max,TD(DL(m)));
                   5093:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   5094:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   5095:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   5096:     t = (NMV)MALLOC(nmv_adv);
                   5097:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   5098:         ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6       noro     5099:         CZ(mr) = CZ(m);
1.1       noro     5100:         ndl_copy(DL(t),DL(mr));
                   5101:     }
                   5102:     NV(p)++;
                   5103:     BDY(p) = mr0;
                   5104: }
                   5105:
                   5106: void ndv_dehomogenize(NDV p,struct order_spec *ord)
                   5107: {
                   5108:     int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
                   5109:     int pos;
                   5110:     Q *w;
                   5111:     Q dvr,t;
                   5112:     NMV m,r;
                   5113:
                   5114:     len = p->len;
                   5115:     newnvar = nd_nvar-1;
                   5116:     newexporigin = nd_get_exporigin(ord);
                   5117:     if ( nd_module ) newmpos = newexporigin-1;
                   5118:     newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
                   5119:     for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
                   5120:         ndl_dehomogenize(DL(m));
                   5121:     if ( newwpd != nd_wpd ) {
                   5122:         newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
                   5123:         for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6       noro     5124:             CZ(r) = CZ(m);
1.1       noro     5125:             if ( nd_module ) pos = MPOS(DL(m));
                   5126:             for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   5127:             adj = nd_exporigin-newexporigin;
                   5128:             for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
                   5129:             if ( nd_module ) {
                   5130:                 DL(r)[newmpos] = pos;
                   5131:             }
                   5132:         }
                   5133:     }
                   5134:     NV(p)--;
                   5135: }
                   5136:
                   5137: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
                   5138: {
                   5139:     int i;
                   5140:     P *tpl,*tpl1;
                   5141:     NODE l;
                   5142:     P h,gcd,t;
                   5143:
                   5144:     tpl = (P *)MALLOC(m*sizeof(P));
                   5145:     tpl1 = (P *)MALLOC(m*sizeof(P));
                   5146:     bcopy(pl,tpl,m*sizeof(P));
                   5147:     gcd = (P)ONE;
                   5148:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   5149:         h = (P)BDY(l);
                   5150:         while ( 1 ) {
                   5151:             for ( i = 0; i < m; i++ )
                   5152:                 if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
                   5153:                     break;
                   5154:             if ( i == m ) {
                   5155:                 bcopy(tpl1,tpl,m*sizeof(P));
                   5156:                 mulp(vl,gcd,h,&t); gcd = t;
                   5157:             } else
                   5158:                 break;
                   5159:         }
                   5160:     }
                   5161:     if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
                   5162:     if ( full ) {
                   5163:         heu_nezgcdnpz(vl,tpl,m,&t);
                   5164:         mulp(vl,gcd,t,pr);
                   5165:     } else
                   5166:         *pr = gcd;
                   5167: }
                   5168:
                   5169: void removecont_array(P *p,int n,int full)
                   5170: {
                   5171:     int all_p,all_q,i;
                   5172:     Z *c;
                   5173:     P *w;
                   5174:     P t,s;
                   5175:
                   5176:     for ( all_q = 1, i = 0; i < n; i++ )
                   5177:         all_q = all_q && NUM(p[i]);
                   5178:     if ( all_q ) {
                   5179:         removecont_array_q((Z *)p,n);
                   5180:     } else {
                   5181:         c = (Z *)MALLOC(n*sizeof(Z));
                   5182:         w = (P *)MALLOC(n*sizeof(P));
                   5183:         for ( i = 0; i < n; i++ ) {
                   5184:             ptozp(p[i],1,(Q *)&c[i],&w[i]);
                   5185:         }
                   5186:         removecont_array_q(c,n);
                   5187:         nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
                   5188:         for ( i = 0; i < n; i++ ) {
                   5189:             divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
                   5190:         }
                   5191:     }
                   5192: }
                   5193:
                   5194: /* c is an int array */
                   5195:
                   5196: void removecont_array_q(Z *c,int n)
                   5197: {
                   5198:   struct oVECT v;
                   5199:   Z d0,d1,a,u,u1,gcd;
                   5200:   int i,j;
                   5201:   Z *q,*r;
                   5202:
                   5203:   q = (Z *)MALLOC(n*sizeof(Z));
                   5204:   r = (Z *)MALLOC(n*sizeof(Z));
                   5205:   v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   5206:   gcdvz_estimate(&v,&d0);
                   5207:   for ( i = 0; i < n; i++ ) {
                   5208:     divqrz(c[i],d0,&q[i],&r[i]);
                   5209:   }
                   5210:   for ( i = 0; i < n; i++ ) if ( r[i] ) break;
                   5211:   if ( i < n ) {
                   5212:     v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   5213:     gcdvz(&v,&d1);
                   5214:     gcdz(d0,d1,&gcd);
1.6       noro     5215:     /* exact division */
                   5216:     divsz(d0,gcd,&a);
1.1       noro     5217:     for ( i = 0; i < n; i++ ) {
                   5218:       mulz(a,q[i],&u);
                   5219:       if ( r[i] ) {
1.6       noro     5220:         /* exact division */
                   5221:         divsz(r[i],gcd,&u1);
1.1       noro     5222:         addz(u,u1,&q[i]);
                   5223:       } else
                   5224:         q[i] = u;
                   5225:     }
                   5226:   }
                   5227:   for ( i = 0; i < n; i++ ) c[i] = q[i];
                   5228: }
                   5229:
1.4       noro     5230: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
                   5231:
                   5232: void mpz_removecont_array(mpz_t *c,int n)
                   5233: {
                   5234:   mpz_t d0,a,u,u1,gcd;
                   5235:   int i,j;
1.13      noro     5236:   static mpz_t *q,*r;
                   5237:   static int c_len = 0;
1.4       noro     5238:
                   5239:   for ( i = 0; i < n; i++ )
                   5240:     if ( mpz_sgn(c[i]) ) break;
                   5241:   if ( i == n ) return;
                   5242:   gcdv_mpz_estimate(d0,c,n);
1.13      noro     5243:   if ( n > c_len ) {
                   5244:     q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   5245:     r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   5246:     c_len = n;
                   5247:   }
1.4       noro     5248:   for ( i = 0; i < n; i++ ) {
                   5249:     mpz_init(q[i]); mpz_init(r[i]);
                   5250:     mpz_fdiv_qr(q[i],r[i],c[i],d0);
                   5251:   }
                   5252:   for ( i = 0; i < n; i++ )
                   5253:     if ( mpz_sgn(r[i]) ) break;
                   5254:   mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
                   5255:   if ( i < n ) {
                   5256:     mpz_gcd(gcd,d0,r[i]);
                   5257:     for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
                   5258:     mpz_div(a,d0,gcd);
                   5259:     for ( i = 0; i < n; i++ ) {
                   5260:       mpz_mul(u,a,q[i]);
                   5261:       if ( mpz_sgn(r[i]) ) {
                   5262:         mpz_div(u1,r[i],gcd);
                   5263:         mpz_add(q[i],u,u1);
                   5264:       } else
                   5265:         mpz_set(q[i],u);
                   5266:     }
                   5267:   }
                   5268:   for ( i = 0; i < n; i++ )
                   5269:     mpz_set(c[i],q[i]);
                   5270: }
                   5271:
1.1       noro     5272: void nd_mul_c(int mod,ND p,int mul)
                   5273: {
                   5274:     NM m;
                   5275:     int c,c1;
                   5276:
                   5277:     if ( !p ) return;
                   5278:     if ( mul == 1 ) return;
                   5279:     if ( mod == -1 )
                   5280:         for ( m = BDY(p); m; m = NEXT(m) )
                   5281:             CM(m) = _mulsf(CM(m),mul);
                   5282:     else
                   5283:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5284:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   5285:         }
                   5286: }
                   5287:
                   5288: void nd_mul_c_lf(ND p,Z mul)
                   5289: {
                   5290:     NM m;
                   5291:     Z c;
                   5292:
                   5293:     if ( !p ) return;
                   5294:     if ( UNIZ(mul) ) return;
                   5295:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   5296:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   5297:     }
                   5298: }
                   5299:
                   5300: void nd_mul_c_q(ND p,P mul)
                   5301: {
                   5302:     NM m;
                   5303:     P c;
                   5304:
                   5305:     if ( !p ) return;
                   5306:     if ( UNIQ(mul) ) return;
                   5307:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   5308:         mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
                   5309:     }
                   5310: }
                   5311:
                   5312: void nd_mul_c_p(VL vl,ND p,P mul)
                   5313: {
                   5314:     NM m;
                   5315:     P c;
                   5316:
                   5317:     if ( !p ) return;
                   5318:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   5319:         mulp(vl,CP(m),mul,&c); CP(m) = c;
                   5320:     }
                   5321: }
                   5322:
                   5323: void nd_free(ND p)
                   5324: {
                   5325:     NM t,s;
                   5326:
                   5327:     if ( !p ) return;
                   5328:     t = BDY(p);
                   5329:     while ( t ) {
                   5330:         s = NEXT(t);
                   5331:         FREENM(t);
                   5332:         t = s;
                   5333:     }
                   5334:     FREEND(p);
                   5335: }
                   5336:
                   5337: void ndv_free(NDV p)
                   5338: {
                   5339:     GCFREE(BDY(p));
                   5340: }
                   5341:
                   5342: void nd_append_red(UINT *d,int i)
                   5343: {
                   5344:     RHist m,m0;
                   5345:     int h;
                   5346:
                   5347:     NEWRHist(m);
                   5348:     h = ndl_hash_value(d);
                   5349:     m->index = i;
                   5350:     ndl_copy(d,DL(m));
                   5351:     NEXT(m) = nd_red[h];
                   5352:     nd_red[h] = m;
                   5353: }
                   5354:
                   5355: UINT *ndv_compute_bound(NDV p)
                   5356: {
                   5357:     UINT *d1,*d2,*t;
                   5358:     UINT u;
                   5359:     int i,j,k,l,len,ind;
                   5360:     NMV m;
                   5361:
                   5362:     if ( !p )
                   5363:         return 0;
                   5364:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5365:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5366:     len = LEN(p);
                   5367:     m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   5368:     for ( i = 1; i < len; i++, NMV_ADV(m) ) {
                   5369:         ndl_max(DL(m),d1,d2);
                   5370:         t = d1; d1 = d2; d2 = t;
                   5371:     }
                   5372:     l = nd_nvar+31;
                   5373:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   5374:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   5375:         u = d1[i];
                   5376:         k = (nd_epw-1)*nd_bpe;
                   5377:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   5378:             t[ind] = (u>>k)&nd_mask0;
                   5379:     }
                   5380:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   5381:     return t;
                   5382: }
                   5383:
                   5384: UINT *nd_compute_bound(ND p)
                   5385: {
                   5386:     UINT *d1,*d2,*t;
                   5387:     UINT u;
                   5388:     int i,j,k,l,len,ind;
                   5389:     NM m;
                   5390:
                   5391:     if ( !p )
                   5392:         return 0;
                   5393:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5394:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5395:     len = LEN(p);
                   5396:     m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   5397:     for ( m = NEXT(m); m; m = NEXT(m) ) {
                   5398:         ndl_lcm(DL(m),d1,d2);
                   5399:         t = d1; d1 = d2; d2 = t;
                   5400:     }
                   5401:     l = nd_nvar+31;
                   5402:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   5403:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   5404:         u = d1[i];
                   5405:         k = (nd_epw-1)*nd_bpe;
                   5406:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   5407:             t[ind] = (u>>k)&nd_mask0;
                   5408:     }
                   5409:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   5410:     return t;
                   5411: }
                   5412:
                   5413: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
                   5414: /* of a term. In this case we need additional 1 word. */
                   5415:
                   5416: int nd_get_exporigin(struct order_spec *ord)
                   5417: {
                   5418:     switch ( ord->id ) {
1.21      noro     5419:         case 0: case 2: case 256: case 258: case 300:
1.1       noro     5420:             return 1+nd_module;
                   5421:         case 1: case 257:
                   5422:             /* block order */
                   5423:             /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
                   5424:             /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
                   5425:             return ord->ord.block.length+1+nd_module;
                   5426:         case 3: case 259:
                   5427: #if 0
                   5428:             error("nd_get_exporigin : composite order is not supported yet.");
                   5429: #else
                   5430:             return 1+nd_module;
                   5431: #endif
                   5432:         default:
                   5433:             error("nd_get_exporigin : ivalid argument.");
                   5434:             return 0;
                   5435:     }
                   5436: }
                   5437:
                   5438: void nd_setup_parameters(int nvar,int max) {
                   5439:     int i,j,n,elen,ord_o,ord_l,l,s,wpd;
                   5440:     struct order_pair *op;
                   5441:
                   5442:     nd_nvar = nvar;
                   5443:     if ( max ) {
                   5444:         /* XXX */
                   5445:         if ( do_weyl ) nd_bpe = 32;
                   5446:         else if ( max < 2 ) nd_bpe = 1;
                   5447:         else if ( max < 4 ) nd_bpe = 2;
                   5448:         else if ( max < 8 ) nd_bpe = 3;
                   5449:         else if ( max < 16 ) nd_bpe = 4;
                   5450:         else if ( max < 32 ) nd_bpe = 5;
                   5451:         else if ( max < 64 ) nd_bpe = 6;
                   5452:         else if ( max < 256 ) nd_bpe = 8;
                   5453:         else if ( max < 1024 ) nd_bpe = 10;
                   5454:         else if ( max < 65536 ) nd_bpe = 16;
                   5455:         else nd_bpe = 32;
                   5456:     }
                   5457:     if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
                   5458:         UINT t;
                   5459:     int st;
                   5460:         int *v;
                   5461:   /* t = max(weights) */
                   5462:         t = 0;
                   5463:         if ( current_dl_weight_vector )
                   5464:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                   5465:                 if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
                   5466:                 if ( t < st ) t = st;
                   5467:             }
                   5468:         if ( nd_matrix )
                   5469:             for ( i = 0; i < nd_matrix_len; i++ )
                   5470:                 for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
                   5471:                     if ( (st=v[j]) < 0 ) st = -st;
                   5472:                     if ( t < st ) t = st;
                   5473:         }
                   5474:         /* i = bitsize of t */
                   5475:         for ( i = 0; t; t >>=1, i++ );
                   5476:         /* i += bitsize of nd_nvar */
                   5477:         for ( t = nd_nvar; t; t >>=1, i++);
                   5478:         /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
                   5479:         if ( (nd_bpe+i) >= 31 )
                   5480:             error("nd_setup_parameters : too large weight");
                   5481:     }
                   5482:     nd_epw = (sizeof(UINT)*8)/nd_bpe;
                   5483:     elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   5484:     nd_exporigin = nd_get_exporigin(nd_ord);
                   5485:     wpd = nd_exporigin+elen;
                   5486:     if ( nd_module )
                   5487:         nd_mpos = nd_exporigin-1;
                   5488:     else
                   5489:         nd_mpos = -1;
                   5490:     if ( wpd != nd_wpd ) {
                   5491:         nd_free_private_storage();
                   5492:         nd_wpd = wpd;
                   5493:     }
                   5494:     if ( nd_bpe < 32 ) {
                   5495:         nd_mask0 = (1<<nd_bpe)-1;
                   5496:     } else {
                   5497:         nd_mask0 = 0xffffffff;
                   5498:     }
                   5499:     bzero(nd_mask,sizeof(nd_mask));
                   5500:     nd_mask1 = 0;
                   5501:     for ( i = 0; i < nd_epw; i++ ) {
                   5502:         nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   5503:         nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   5504:     }
                   5505:     nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
                   5506:     nd_epos = nd_create_epos(nd_ord);
                   5507:     nd_blockmask = nd_create_blockmask(nd_ord);
                   5508:     nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
                   5509: }
                   5510:
                   5511: ND_pairs nd_reconstruct(int trace,ND_pairs d)
                   5512: {
                   5513:     int i,obpe,oadv,h;
                   5514:     static NM prev_nm_free_list;
                   5515:     static ND_pairs prev_ndp_free_list;
                   5516:     RHist mr0,mr;
                   5517:     RHist r;
                   5518:     RHist *old_red;
                   5519:     ND_pairs s0,s,t;
                   5520:     EPOS oepos;
                   5521:
                   5522:     obpe = nd_bpe;
                   5523:     oadv = nmv_adv;
                   5524:     oepos = nd_epos;
                   5525:     if ( obpe < 2 ) nd_bpe = 2;
                   5526:     else if ( obpe < 3 ) nd_bpe = 3;
                   5527:     else if ( obpe < 4 ) nd_bpe = 4;
                   5528:     else if ( obpe < 5 ) nd_bpe = 5;
                   5529:     else if ( obpe < 6 ) nd_bpe = 6;
                   5530:     else if ( obpe < 8 ) nd_bpe = 8;
                   5531:     else if ( obpe < 10 ) nd_bpe = 10;
                   5532:     else if ( obpe < 16 ) nd_bpe = 16;
                   5533:     else if ( obpe < 32 ) nd_bpe = 32;
                   5534:     else error("nd_reconstruct : exponent too large");
                   5535:
                   5536:     nd_setup_parameters(nd_nvar,0);
                   5537:     prev_nm_free_list = _nm_free_list;
                   5538:     prev_ndp_free_list = _ndp_free_list;
                   5539:     _nm_free_list = 0;
                   5540:     _ndp_free_list = 0;
                   5541:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   5542:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   5543:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   5544:     }
                   5545:     if ( trace )
                   5546:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   5547:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   5548:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   5549:         }
                   5550:     s0 = 0;
                   5551:     for ( t = d; t; t = NEXT(t) ) {
                   5552:         NEXTND_pairs(s0,s);
                   5553:         s->i1 = t->i1;
                   5554:         s->i2 = t->i2;
1.24      noro     5555:         s->sig = t->sig;
1.1       noro     5556:         SG(s) = SG(t);
                   5557:         ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   5558:     }
                   5559:
                   5560:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   5561:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   5562:         old_red[i] = nd_red[i];
                   5563:         nd_red[i] = 0;
                   5564:     }
                   5565:     for ( i = 0; i < REDTAB_LEN; i++ )
                   5566:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   5567:             NEWRHist(mr);
                   5568:             mr->index = r->index;
                   5569:             SG(mr) = SG(r);
                   5570:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   5571:             h = ndl_hash_value(DL(mr));
                   5572:             NEXT(mr) = nd_red[h];
                   5573:             nd_red[h] = mr;
1.24      noro     5574:             mr->sig = r->sig;
1.1       noro     5575:         }
                   5576:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   5577:     old_red = 0;
                   5578:     for ( i = 0; i < nd_psn; i++ ) {
                   5579:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   5580:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24      noro     5581:         r->sig = nd_psh[i]->sig;
1.1       noro     5582:         nd_psh[i] = r;
                   5583:     }
                   5584:     if ( s0 ) NEXT(s) = 0;
                   5585:     prev_nm_free_list = 0;
                   5586:     prev_ndp_free_list = 0;
                   5587: #if 0
                   5588:     GC_gcollect();
                   5589: #endif
                   5590:     return s0;
                   5591: }
                   5592:
1.30      noro     5593: void nd_reconstruct_s(int trace,ND_pairs *d)
                   5594: {
                   5595:     int i,obpe,oadv,h;
                   5596:     static NM prev_nm_free_list;
                   5597:     static ND_pairs prev_ndp_free_list;
                   5598:     RHist mr0,mr;
                   5599:     RHist r;
                   5600:     RHist *old_red;
                   5601:     ND_pairs s0,s,t;
                   5602:     EPOS oepos;
                   5603:
                   5604:     obpe = nd_bpe;
                   5605:     oadv = nmv_adv;
                   5606:     oepos = nd_epos;
                   5607:     if ( obpe < 2 ) nd_bpe = 2;
                   5608:     else if ( obpe < 3 ) nd_bpe = 3;
                   5609:     else if ( obpe < 4 ) nd_bpe = 4;
                   5610:     else if ( obpe < 5 ) nd_bpe = 5;
                   5611:     else if ( obpe < 6 ) nd_bpe = 6;
                   5612:     else if ( obpe < 8 ) nd_bpe = 8;
                   5613:     else if ( obpe < 10 ) nd_bpe = 10;
                   5614:     else if ( obpe < 16 ) nd_bpe = 16;
                   5615:     else if ( obpe < 32 ) nd_bpe = 32;
                   5616:     else error("nd_reconstruct_s : exponent too large");
                   5617:
                   5618:     nd_setup_parameters(nd_nvar,0);
                   5619:     prev_nm_free_list = _nm_free_list;
                   5620:     prev_ndp_free_list = _ndp_free_list;
                   5621:     _nm_free_list = 0;
                   5622:     _ndp_free_list = 0;
                   5623:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   5624:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   5625:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   5626:     }
                   5627:     if ( trace )
                   5628:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   5629:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   5630:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   5631:         }
                   5632:
                   5633:     for ( i = 0; i < nd_nbase; i++ ) {
                   5634:       s0 = 0;
                   5635:       for ( t = d[i]; t; t = NEXT(t) ) {
                   5636:           NEXTND_pairs(s0,s);
                   5637:           s->i1 = t->i1;
                   5638:           s->i2 = t->i2;
                   5639:           s->sig = t->sig;
                   5640:           SG(s) = SG(t);
                   5641:           ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   5642:       }
                   5643:       d[i] = s0;
                   5644:     }
                   5645:
                   5646:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   5647:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   5648:         old_red[i] = nd_red[i];
                   5649:         nd_red[i] = 0;
                   5650:     }
                   5651:     for ( i = 0; i < REDTAB_LEN; i++ )
                   5652:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   5653:             NEWRHist(mr);
                   5654:             mr->index = r->index;
                   5655:             SG(mr) = SG(r);
                   5656:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   5657:             h = ndl_hash_value(DL(mr));
                   5658:             NEXT(mr) = nd_red[h];
                   5659:             nd_red[h] = mr;
                   5660:             mr->sig = r->sig;
                   5661:         }
                   5662:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   5663:     old_red = 0;
                   5664:     for ( i = 0; i < nd_psn; i++ ) {
                   5665:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   5666:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
                   5667:         r->sig = nd_psh[i]->sig;
                   5668:         nd_psh[i] = r;
                   5669:     }
                   5670:     if ( s0 ) NEXT(s) = 0;
                   5671:     prev_nm_free_list = 0;
                   5672:     prev_ndp_free_list = 0;
                   5673: #if 0
                   5674:     GC_gcollect();
                   5675: #endif
                   5676: }
                   5677:
1.1       noro     5678: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
                   5679: {
                   5680:     int n,i,ei,oepw,omask0,j,s,ord_l,l;
                   5681:     struct order_pair *op;
                   5682:
                   5683:     n = nd_nvar;
                   5684:     oepw = (sizeof(UINT)*8)/obpe;
                   5685:     omask0 = (1<<obpe)-1;
                   5686:     TD(r) = TD(d);
                   5687:     for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
                   5688:     if ( nd_blockmask ) {
                   5689:         l = nd_blockmask->n;
                   5690:         op = nd_blockmask->order_pair;
                   5691:         for ( i = 1; i < nd_exporigin; i++ )
                   5692:             r[i] = d[i];
                   5693:         for ( j = 0, s = 0; j < l; j++ ) {
                   5694:             ord_l = op[j].length;
                   5695:             for ( i = 0; i < ord_l; i++, s++ ) {
                   5696:                 ei =  GET_EXP_OLD(d,s);
                   5697:                 PUT_EXP(r,s,ei);
                   5698:             }
                   5699:         }
                   5700:     } else {
                   5701:         for ( i = 0; i < n; i++ ) {
                   5702:             ei = GET_EXP_OLD(d,i);
                   5703:             PUT_EXP(r,i,ei);
                   5704:         }
                   5705:     }
                   5706:     if ( nd_module ) MPOS(r) = MPOS(d);
                   5707: }
                   5708:
                   5709: ND nd_copy(ND p)
                   5710: {
                   5711:     NM m,mr,mr0;
                   5712:     int c,n;
                   5713:     ND r;
                   5714:
                   5715:     if ( !p )
                   5716:         return 0;
                   5717:     else {
                   5718:         for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   5719:             NEXTNM(mr0,mr);
                   5720:             CM(mr) = CM(m);
                   5721:             ndl_copy(DL(m),DL(mr));
                   5722:         }
                   5723:         NEXT(mr) = 0;
                   5724:         MKND(NV(p),mr0,LEN(p),r);
                   5725:         SG(r) = SG(p);
                   5726:         return r;
                   5727:     }
                   5728: }
                   5729:
                   5730: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
                   5731: {
                   5732:     NM m1,m2;
                   5733:     NDV p1,p2;
                   5734:     ND t1,t2;
                   5735:     UINT *lcm;
                   5736:     P gp,tp;
                   5737:     Z g,t;
                   5738:     Z iq;
                   5739:     int td;
                   5740:     LIST hist;
                   5741:     NODE node;
                   5742:     DP d;
                   5743:
                   5744:     if ( !mod && nd_demand ) {
                   5745:         p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
                   5746:     } else {
                   5747:         if ( trace ) {
                   5748:             p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   5749:         } else {
                   5750:             p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   5751:         }
                   5752:     }
                   5753:     lcm = LCM(p);
                   5754:     NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
                   5755:     if ( ndl_check_bound2(p->i1,DL(m1)) ) {
                   5756:         FREENM(m1); return 0;
                   5757:     }
                   5758:     NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
                   5759:     if ( ndl_check_bound2(p->i2,DL(m2)) ) {
                   5760:         FREENM(m1); FREENM(m2); return 0;
                   5761:     }
                   5762:
                   5763:     if ( mod == -1 ) {
                   5764:         CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
                   5765:     } else if ( mod > 0 ) {
                   5766:         CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
                   5767:     } else if ( mod == -2 ) {
                   5768:         CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
                   5769:     } else if ( nd_vc ) {
                   5770:         ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
                   5771:         divsp(nd_vc,HCP(p2),gp,&CP(m1));
                   5772:         divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
                   5773:     } else {
1.6       noro     5774:         igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1       noro     5775:     }
                   5776:     t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
                   5777:     *rp = nd_add(mod,t1,t2);
                   5778:     if ( nd_gentrace ) {
                   5779:         /* nd_tracelist is initialized */
1.6       noro     5780:         STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     5781:         MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6       noro     5782:         STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     5783:         MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
                   5784:         nd_tracelist = node;
                   5785:     }
1.24      noro     5786:     if ( *rp )
                   5787:       (*rp)->sig = p->sig;
1.1       noro     5788:     FREENM(m1); FREENM(m2);
                   5789:     return 1;
                   5790: }
                   5791:
                   5792: void ndv_mul_c(int mod,NDV p,int mul)
                   5793: {
                   5794:     NMV m;
                   5795:     int c,c1,len,i;
                   5796:
                   5797:     if ( !p ) return;
                   5798:     len = LEN(p);
                   5799:     if ( mod == -1 )
                   5800:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   5801:             CM(m) = _mulsf(CM(m),mul);
                   5802:     else
                   5803:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   5804:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   5805:         }
                   5806: }
                   5807:
                   5808: void ndv_mul_c_lf(NDV p,Z mul)
                   5809: {
                   5810:     NMV m;
                   5811:     Z c;
                   5812:     int len,i;
                   5813:
                   5814:     if ( !p ) return;
                   5815:     len = LEN(p);
                   5816:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   5817:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   5818:     }
                   5819: }
                   5820:
                   5821: /* for nd_det */
                   5822: void ndv_mul_c_q(NDV p,Z mul)
                   5823: {
                   5824:     NMV m;
                   5825:     Z c;
                   5826:     int len,i;
                   5827:
                   5828:     if ( !p ) return;
                   5829:     len = LEN(p);
                   5830:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     5831:         mulz(CZ(m),mul,&c); CZ(m) = c;
1.1       noro     5832:     }
                   5833: }
                   5834:
                   5835: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
                   5836:     int n2,i,j,l,n,tlen;
                   5837:     UINT *d0;
                   5838:     NM *tab,*psum;
                   5839:     ND s,r;
                   5840:     NM t;
                   5841:     NMV m1;
                   5842:
                   5843:     if ( !p ) return 0;
                   5844:     n = NV(p); n2 = n>>1;
                   5845:     d0 = DL(m0);
                   5846:     l = LEN(p);
                   5847:     for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   5848:     tab = (NM *)MALLOC(tlen*sizeof(NM));
                   5849:     psum = (NM *)MALLOC(tlen*sizeof(NM));
                   5850:     for ( i = 0; i < tlen; i++ ) psum[i] = 0;
                   5851:     m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   5852:     for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
                   5853:         /* m0(NM) * m1(NMV) => tab(NM) */
                   5854:         weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
                   5855:         for ( j = 0; j < tlen; j++ ) {
                   5856:             if ( tab[j] ) {
                   5857:                 NEXT(tab[j]) = psum[j];    psum[j] = tab[j];
                   5858:             }
                   5859:         }
                   5860:     }
                   5861:     for ( i = tlen-1, r = 0; i >= 0; i-- )
                   5862:         if ( psum[i] ) {
                   5863:             for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   5864:             MKND(n,psum[i],j,s);
                   5865:             r = nd_add(mod,r,s);
                   5866:         }
                   5867:     if ( r ) SG(r) = SG(p)+TD(d0);
                   5868:     return r;
                   5869: }
                   5870:
                   5871: /* product of monomials */
                   5872: /* XXX block order is not handled correctly */
                   5873:
                   5874: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   5875: {
                   5876:     int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
                   5877:     UINT *d0,*d1,*d,*dt,*ctab;
                   5878:     Z *ctab_q;
                   5879:     Z q,q1;
                   5880:     UINT c0,c1,c;
                   5881:     NM *p;
                   5882:     NM m,t;
                   5883:     int mpos;
                   5884:
                   5885:     for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   5886:     if ( !m0 || !m1 ) return;
                   5887:     d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
                   5888:     if ( nd_module )
                   5889:         if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
                   5890:
                   5891:     NEWNM(m); d = DL(m);
                   5892:     if ( mod ) {
                   5893:         c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
                   5894:     } else if ( nd_vc )
                   5895:         mulp(nd_vc,CP(m0),CP(m1),&CP(m));
                   5896:   else
1.6       noro     5897:         mulz(CZ(m0),CZ(m1),&CZ(m));
1.1       noro     5898:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   5899:     homo = n&1 ? 1 : 0;
                   5900:     if ( homo ) {
                   5901:         /* offset of h-degree */
                   5902:         h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   5903:         PUT_EXP(DL(m),n-1,h);
                   5904:         TD(DL(m)) = h;
                   5905:         if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   5906:     }
                   5907:     tab[0] = m;
                   5908:     NEWNM(m); d = DL(m);
                   5909:     for ( i = 0, curlen = 1; i < n2; i++ ) {
                   5910:         a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   5911:         k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   5912:         /* xi^a*(Di^k*xi^l)*Di^b */
                   5913:         a += l; b += k;
                   5914:         s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
                   5915:         if ( !k || !l ) {
                   5916:             for ( j = 0; j < curlen; j++ )
                   5917:                 if ( (t = tab[j]) != 0 ) {
                   5918:                     dt = DL(t);
                   5919:                     PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
                   5920:                     if ( nd_blockmask ) ndl_weight_mask(dt);
                   5921:                 }
                   5922:             curlen *= k+1;
                   5923:             continue;
                   5924:         }
                   5925:         min = MIN(k,l);
                   5926:         if ( mod ) {
                   5927:             ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
                   5928:             mkwcm(k,l,mod,(int *)ctab);
                   5929:         } else {
                   5930:             ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
                   5931:             mkwc(k,l,ctab_q);
                   5932:         }
                   5933:         for ( j = min; j >= 0; j-- ) {
                   5934:             for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
                   5935:             PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
                   5936:             h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
                   5937:             if ( homo ) {
                   5938:                 TD(d) = s;
                   5939:                 PUT_EXP(d,n-1,s-h);
                   5940:             } else TD(d) = h;
                   5941:             if ( nd_blockmask ) ndl_weight_mask(d);
                   5942:             if ( mod ) c = ctab[j];
                   5943:             else q = ctab_q[j];
                   5944:             p = tab+curlen*j;
                   5945:             if ( j == 0 ) {
                   5946:                 for ( u = 0; u < curlen; u++, p++ ) {
                   5947:                     if ( tab[u] ) {
                   5948:                         ndl_addto(DL(tab[u]),d);
                   5949:                         if ( mod ) {
                   5950:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
                   5951:                         } else if ( nd_vc )
                   5952:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
                   5953:             else {
1.6       noro     5954:                             mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1       noro     5955:                         }
                   5956:                     }
                   5957:                 }
                   5958:             } else {
                   5959:                 for ( u = 0; u < curlen; u++, p++ ) {
                   5960:                     if ( tab[u] ) {
                   5961:                         NEWNM(t);
                   5962:                         ndl_add(DL(tab[u]),d,DL(t));
                   5963:                         if ( mod ) {
                   5964:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
                   5965:                         } else if ( nd_vc )
                   5966:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
                   5967:             else
1.6       noro     5968:                             mulz(CZ(tab[u]),q,&CZ(t));
1.1       noro     5969:                         *p = t;
                   5970:                     }
                   5971:                 }
                   5972:             }
                   5973:         }
                   5974:         curlen *= k+1;
                   5975:     }
                   5976:     FREENM(m);
                   5977:     if ( nd_module ) {
                   5978:         mpos = MPOS(d1);
                   5979:         for ( i = 0; i < tlen; i++ )
                   5980:             if ( tab[i] ) {
                   5981:                 d = DL(tab[i]);
                   5982:                 MPOS(d) = mpos;
                   5983:                 TD(d) = ndl_weight(d);
                   5984:             }
                   5985:     }
                   5986: }
                   5987:
                   5988: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   5989: {
                   5990:     NM mr,mr0;
                   5991:     NMV m;
                   5992:     UINT *d,*dt,*dm;
                   5993:     int c,n,td,i,c1,c2,len;
                   5994:     Q q;
                   5995:     ND r;
                   5996:
                   5997:     if ( !p ) return 0;
                   5998:     else {
                   5999:         n = NV(p); m = BDY(p);
                   6000:         d = DL(m0);
                   6001:         len = LEN(p);
                   6002:         mr0 = 0;
                   6003:         td = TD(d);
                   6004:         c = CM(m0);
                   6005:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6006:             NEXTNM(mr0,mr);
                   6007:             CM(mr) = 1;
                   6008:             ndl_add(DL(m),d,DL(mr));
                   6009:         }
                   6010:         NEXT(mr) = 0;
                   6011:         MKND(NV(p),mr0,len,r);
                   6012:         SG(r) = SG(p) + TD(d);
                   6013:         return r;
                   6014:     }
                   6015: }
                   6016:
                   6017: ND ndv_mul_nm(int mod,NM m0,NDV p)
                   6018: {
                   6019:     NM mr,mr0;
                   6020:     NMV m;
                   6021:     UINT *d,*dt,*dm;
                   6022:     int c,n,td,i,c1,c2,len;
                   6023:     P q;
                   6024:     ND r;
                   6025:
                   6026:     if ( !p ) return 0;
                   6027:     else if ( do_weyl ) {
                   6028:         if ( mod < 0 ) {
                   6029:             error("ndv_mul_nm : not implemented (weyl)");
                   6030:             return 0;
                   6031:         } else
                   6032:             return weyl_ndv_mul_nm(mod,m0,p);
                   6033:     } else {
                   6034:         n = NV(p); m = BDY(p);
                   6035:         d = DL(m0);
                   6036:         len = LEN(p);
                   6037:         mr0 = 0;
                   6038:         td = TD(d);
                   6039:         if ( mod == -1 ) {
                   6040:             c = CM(m0);
                   6041:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6042:                 NEXTNM(mr0,mr);
                   6043:                 CM(mr) = _mulsf(CM(m),c);
                   6044:                 ndl_add(DL(m),d,DL(mr));
                   6045:             }
                   6046:         } else if ( mod == -2 ) {
                   6047:             Z cl;
                   6048:             cl = CZ(m0);
                   6049:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6050:                 NEXTNM(mr0,mr);
                   6051:                 mullf(CZ(m),cl,&CZ(mr));
                   6052:                 ndl_add(DL(m),d,DL(mr));
                   6053:             }
                   6054:         } else if ( mod ) {
                   6055:             c = CM(m0);
                   6056:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6057:                 NEXTNM(mr0,mr);
                   6058:                 c1 = CM(m);
                   6059:                 DMAR(c1,c,0,mod,c2);
                   6060:                 CM(mr) = c2;
                   6061:                 ndl_add(DL(m),d,DL(mr));
                   6062:             }
                   6063:         } else {
                   6064:             q = CP(m0);
                   6065:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6066:                 NEXTNM(mr0,mr);
                   6067:                 mulp(nd_vc,CP(m),q,&CP(mr));
                   6068:                 ndl_add(DL(m),d,DL(mr));
                   6069:             }
                   6070:         }
                   6071:         NEXT(mr) = 0;
                   6072:         MKND(NV(p),mr0,len,r);
                   6073:         SG(r) = SG(p) + TD(d);
                   6074:         return r;
                   6075:     }
                   6076: }
                   6077:
                   6078: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
                   6079: {
                   6080:     NM mq0,mq;
                   6081:     NMV tm;
                   6082:     Q q;
                   6083:     int i,nv,sg,c,c1,c2,hindex;
                   6084:     ND p,t,r;
                   6085:
                   6086:     if ( bucket->m < 0 ) return 0;
                   6087:     else {
                   6088:         nv = NV(d);
                   6089:         mq0 = 0;
                   6090:         tm = (NMV)MALLOC(nmv_adv);
                   6091:         while ( 1 ) {
                   6092:             if ( mod > 0 || mod == -1 )
                   6093:               hindex = head_pbucket(mod,bucket);
                   6094:             else if ( mod == -2 )
                   6095:               hindex = head_pbucket_lf(bucket);
                   6096:             else
                   6097:               hindex = head_pbucket_q(bucket);
                   6098:             if ( hindex < 0 ) break;
                   6099:             p = bucket->body[hindex];
                   6100:             NEXTNM(mq0,mq);
                   6101:             ndl_sub(HDL(p),HDL(d),DL(mq));
                   6102:             ndl_copy(DL(mq),DL(tm));
                   6103:             if ( mod ) {
                   6104:                 c1 = invm(HCM(d),mod); c2 = HCM(p);
                   6105:                 DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   6106:                 CM(tm) = mod-c;
                   6107:             } else {
1.6       noro     6108:                 divsz(HCZ(p),HCZ(d),&CZ(mq));
                   6109:                 chsgnz(CZ(mq),&CZ(tm));
1.1       noro     6110:             }
                   6111:             t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
                   6112:             bucket->body[hindex] = nd_remove_head(p);
                   6113:             t = nd_remove_head(t);
                   6114:             add_pbucket(mod,bucket,t);
                   6115:         }
                   6116:         if ( !mq0 )
                   6117:             r = 0;
                   6118:         else {
                   6119:             NEXT(mq) = 0;
                   6120:             for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   6121:             MKND(nv,mq0,i,r);
                   6122:             /* XXX */
                   6123:             SG(r) = HTD(r);
                   6124:         }
                   6125:         return r;
                   6126:     }
                   6127: }
                   6128:
                   6129: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   6130: {
                   6131:     NMV m,mr,mr0,t;
                   6132:     int len,i,k;
                   6133:
                   6134:     if ( !p ) return;
                   6135:     m = BDY(p); len = LEN(p);
                   6136:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   6137:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   6138:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   6139:     t = (NMV)MALLOC(nmv_adv);
                   6140:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6       noro     6141:         CZ(t) = CZ(m);
1.1       noro     6142:         for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   6143:         ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6       noro     6144:         CZ(mr) = CZ(t);
1.1       noro     6145:         ndl_copy(DL(t),DL(mr));
                   6146:     }
                   6147:     BDY(p) = mr0;
                   6148: }
                   6149:
                   6150: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   6151: {
                   6152:     NMV m,mr,mr0;
                   6153:     int len,i;
                   6154:     NDV r;
                   6155:
                   6156:     if ( !p ) return 0;
                   6157:     m = BDY(p); len = LEN(p);
                   6158:     mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   6159:     for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   6160:         ndl_zero(DL(mr));
                   6161:         ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6       noro     6162:         CZ(mr) = CZ(m);
1.1       noro     6163:     }
                   6164:     MKNDV(NV(p),mr0,len,r);
                   6165:     SG(r) = SG(p);
1.24      noro     6166:     r->sig = p->sig;
1.1       noro     6167:     return r;
                   6168: }
                   6169:
                   6170: /* duplicate p */
                   6171:
                   6172: NDV ndv_dup(int mod,NDV p)
                   6173: {
                   6174:     NDV d;
                   6175:     NMV t,m,m0;
                   6176:     int i,len;
                   6177:
                   6178:     if ( !p ) return 0;
                   6179:     len = LEN(p);
                   6180:     m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   6181:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   6182:         ndl_copy(DL(t),DL(m));
1.6       noro     6183:         CZ(m) = CZ(t);
1.1       noro     6184:     }
                   6185:     MKNDV(NV(p),m0,len,d);
                   6186:     SG(d) = SG(p);
                   6187:     return d;
                   6188: }
                   6189:
                   6190: NDV ndv_symbolic(int mod,NDV p)
                   6191: {
                   6192:     NDV d;
                   6193:     NMV t,m,m0;
                   6194:     int i,len;
                   6195:
                   6196:     if ( !p ) return 0;
                   6197:     len = LEN(p);
                   6198:     m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   6199:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   6200:         ndl_copy(DL(t),DL(m));
1.6       noro     6201:         CZ(m) = ONE;
1.1       noro     6202:     }
                   6203:     MKNDV(NV(p),m0,len,d);
                   6204:     SG(d) = SG(p);
                   6205:     return d;
                   6206: }
                   6207:
                   6208: ND nd_dup(ND p)
                   6209: {
                   6210:     ND d;
                   6211:     NM t,m,m0;
                   6212:
                   6213:     if ( !p ) return 0;
                   6214:     for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   6215:         NEXTNM(m0,m);
                   6216:         ndl_copy(DL(t),DL(m));
1.6       noro     6217:         CZ(m) = CZ(t);
1.1       noro     6218:     }
                   6219:     if ( m0 ) NEXT(m) = 0;
                   6220:     MKND(NV(p),m0,LEN(p),d);
                   6221:     SG(d) = SG(p);
                   6222:     return d;
                   6223: }
                   6224:
                   6225: /* XXX if p->len == 0 then it represents 0 */
                   6226:
                   6227: void ndv_mod(int mod,NDV p)
                   6228: {
                   6229:     NMV t,d;
                   6230:     int r,s,u;
                   6231:     int i,len,dlen;
                   6232:     P cp;
                   6233:     Z c;
                   6234:     Obj gfs;
                   6235:
                   6236:     if ( !p ) return;
                   6237:     len = LEN(p);
                   6238:     dlen = 0;
                   6239:     if ( mod == -1 )
                   6240:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   6241:             simp_ff((Obj)CP(t),&gfs);
                   6242:             if ( gfs ) {
                   6243:               r = FTOIF(CONT((GFS)gfs));
                   6244:               CM(d) = r;
                   6245:               ndl_copy(DL(t),DL(d));
                   6246:               NMV_ADV(d);
                   6247:               dlen++;
                   6248:             }
                   6249:         }
                   6250:     else if ( mod == -2 )
                   6251:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   6252:             simp_ff((Obj)CP(t),&gfs);
                   6253:             if ( gfs ) {
                   6254:               lmtolf((LM)gfs,&CZ(d));
                   6255:               ndl_copy(DL(t),DL(d));
                   6256:               NMV_ADV(d);
                   6257:               dlen++;
                   6258:             }
                   6259:         }
                   6260:     else
                   6261:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   6262:             if ( nd_vc ) {
                   6263:                 nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
                   6264:                 c = (Z)cp;
                   6265:             } else
1.6       noro     6266:                 c = CZ(t);
1.1       noro     6267:             r = remqi((Q)c,mod);
                   6268:             if ( r ) {
                   6269:                 CM(d) = r;
                   6270:                 ndl_copy(DL(t),DL(d));
                   6271:                 NMV_ADV(d);
                   6272:                 dlen++;
                   6273:             }
                   6274:         }
                   6275:     LEN(p) = dlen;
                   6276: }
                   6277:
                   6278: NDV ptondv(VL vl,VL dvl,P p)
                   6279: {
                   6280:     ND nd;
                   6281:
                   6282:     nd = ptond(vl,dvl,p);
                   6283:     return ndtondv(0,nd);
                   6284: }
                   6285:
                   6286: void pltozpl(LIST l,Q *cont,LIST *pp)
                   6287: {
1.16      noro     6288:   NODE nd,nd1;
                   6289:   int n;
                   6290:   P *pl;
                   6291:   Q *cl;
                   6292:   int i;
                   6293:   P dmy;
                   6294:   Z dvr,inv;
                   6295:   LIST r;
                   6296:
                   6297:   nd = BDY(l); n = length(nd);
                   6298:   pl = (P *)MALLOC(n*sizeof(P));
                   6299:   cl = (Q *)MALLOC(n*sizeof(Q));
                   6300:   for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
                   6301:     ptozp((P)BDY(nd),1,&cl[i],&dmy);
                   6302:   }
                   6303:   qltozl(cl,n,&dvr);
                   6304:   divz(ONE,dvr,&inv);
                   6305:   nd = BDY(l);
                   6306:   for ( i = 0; i < n; i++, nd = NEXT(nd) )
                   6307:     divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
                   6308:   nd = 0;
                   6309:   for ( i = n-1; i >= 0; i-- ) {
                   6310:     MKNODE(nd1,pl[i],nd); nd = nd1;
                   6311:   }
                   6312:   MKLIST(r,nd);
                   6313:   *pp = r;
1.1       noro     6314: }
                   6315:
                   6316: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
                   6317:
                   6318: NDV pltondv(VL vl,VL dvl,LIST p)
                   6319: {
                   6320:     int i;
                   6321:     NODE t;
                   6322:     ND r,ri;
                   6323:     NM m;
                   6324:
                   6325:     if ( !nd_module ) error("pltond : module order must be set");
                   6326:     r = 0;
                   6327:     for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
                   6328:         ri = ptond(vl,dvl,(P)BDY(t));
                   6329:         if ( ri )
                   6330:             for ( m = BDY(ri); m; m = NEXT(m) ) {
                   6331:                 MPOS(DL(m)) = i;
                   6332:                 TD(DL(m)) = ndl_weight(DL(m));
                   6333:                 if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   6334:             }
                   6335:         r = nd_add(0,r,ri);
                   6336:     }
                   6337:     return ndtondv(0,r);
                   6338: }
                   6339:
                   6340: ND ptond(VL vl,VL dvl,P p)
                   6341: {
                   6342:     int n,i,j,k,e;
                   6343:     VL tvl;
                   6344:     V v;
                   6345:     DCP dc;
                   6346:     DCP *w;
                   6347:     ND r,s,t,u;
                   6348:     P x;
                   6349:     int c;
                   6350:     UINT *d;
                   6351:     NM m,m0;
                   6352:
                   6353:     if ( !p )
                   6354:         return 0;
                   6355:     else if ( NUM(p) ) {
                   6356:         NEWNM(m);
                   6357:         ndl_zero(DL(m));
                   6358:         if ( !INT((Q)p) )
                   6359:           error("ptond : input must be integer-coefficient");
1.6       noro     6360:         CZ(m) = (Z)p;
1.1       noro     6361:         NEXT(m) = 0;
                   6362:         MKND(nd_nvar,m,1,r);
                   6363:         SG(r) = 0;
                   6364:         return r;
                   6365:     } else {
                   6366:         for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   6367:         w = (DCP *)MALLOC(k*sizeof(DCP));
                   6368:         for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   6369:         for ( i = 0, tvl = dvl, v = VR(p);
                   6370:             tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   6371:         if ( !tvl ) {
                   6372:             for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   6373:                 t = ptond(vl,dvl,COEF(w[j]));
                   6374:                 pwrp(vl,x,DEG(w[j]),&p);
                   6375:                 nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   6376:             }
                   6377:             return s;
                   6378:         } else {
                   6379:             NEWNM(m0); d = DL(m0);
                   6380:             for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6       noro     6381:                 ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1       noro     6382:                 TD(d) = MUL_WEIGHT(e,i);
                   6383:                 if ( nd_blockmask) ndl_weight_mask(d);
                   6384:                 if ( nd_module ) MPOS(d) = 0;
                   6385:                 t = ptond(vl,dvl,COEF(w[j]));
                   6386:                 for ( m = BDY(t); m; m = NEXT(m) )
                   6387:                     ndl_addto(DL(m),d);
                   6388:                 SG(t) += TD(d);
                   6389:                 s = nd_add(0,s,t);
                   6390:             }
                   6391:             FREENM(m0);
                   6392:             return s;
                   6393:         }
                   6394:     }
                   6395: }
                   6396:
                   6397: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   6398: {
                   6399:     VL tvl;
                   6400:     int len,n,j,i,e;
                   6401:     NMV m;
                   6402:     Z q;
                   6403:     P c;
                   6404:     UINT *d;
                   6405:     P s,r,u,t,w;
                   6406:     GFS gfs;
                   6407:
                   6408:     if ( !p ) return 0;
                   6409:     else {
                   6410:         len = LEN(p);
                   6411:         n = NV(p);
                   6412:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   6413:         for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   6414:             if ( mod == -1 ) {
                   6415:                e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   6416:             } else if ( mod == -2 ) {
                   6417:                c = (P)CZ(m);
                   6418:             } else if ( mod > 0 ) {
1.6       noro     6419:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     6420:             } else
                   6421:                 c = CP(m);
                   6422:             d = DL(m);
                   6423:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     6424:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     6425:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   6426:             }
                   6427:             addp(vl,s,t,&u); s = u;
                   6428:         }
                   6429:         return s;
                   6430:     }
                   6431: }
                   6432:
                   6433: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
                   6434: {
                   6435:     VL tvl;
                   6436:     int len,n,j,i,e;
                   6437:     NMV m;
                   6438:     Z q;
                   6439:     P c;
                   6440:     UINT *d;
                   6441:     P s,r,u,t,w;
                   6442:     GFS gfs;
                   6443:     P *a;
                   6444:     LIST l;
                   6445:     NODE nd,nd1;
                   6446:
                   6447:     if ( !p ) return 0;
                   6448:     else {
                   6449:         a = (P *)MALLOC((rank+1)*sizeof(P));
                   6450:         for ( i = 0; i <= rank; i++ ) a[i] = 0;
                   6451:         len = LEN(p);
                   6452:         n = NV(p);
                   6453:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   6454:         for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
                   6455:             if ( mod == -1 ) {
                   6456:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   6457:             } else if ( mod ) {
1.6       noro     6458:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     6459:             } else
                   6460:                 c = CP(m);
                   6461:             d = DL(m);
                   6462:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     6463:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     6464:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   6465:             }
                   6466:             addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
                   6467:         }
                   6468:         nd = 0;
                   6469:         for ( i = rank; i > 0; i-- ) {
                   6470:             MKNODE(nd1,a[i],nd); nd = nd1;
                   6471:         }
                   6472:         MKLIST(l,nd);
                   6473:         return l;
                   6474:     }
                   6475: }
                   6476:
                   6477: NDV ndtondv(int mod,ND p)
                   6478: {
                   6479:     NDV d;
                   6480:     NMV m,m0;
                   6481:     NM t;
                   6482:     int i,len;
                   6483:
                   6484:     if ( !p ) return 0;
                   6485:     len = LEN(p);
                   6486:     if ( mod > 0 || mod == -1 )
                   6487:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   6488:     else
                   6489:         m0 = m = MALLOC(len*nmv_adv);
                   6490: #if 0
                   6491:     ndv_alloc += nmv_adv*len;
                   6492: #endif
                   6493:     for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   6494:         ndl_copy(DL(t),DL(m));
1.6       noro     6495:         CZ(m) = CZ(t);
1.1       noro     6496:     }
                   6497:     MKNDV(NV(p),m0,len,d);
                   6498:     SG(d) = SG(p);
1.24      noro     6499:     d->sig = p->sig;
1.1       noro     6500:     return d;
                   6501: }
                   6502:
1.16      noro     6503: static int dmm_comp_nv;
                   6504:
                   6505: int dmm_comp(DMM *a,DMM *b)
                   6506: {
                   6507:    return -compdmm(dmm_comp_nv,*a,*b);
                   6508: }
                   6509:
                   6510: void dmm_sort_by_ord(DMM *a,int len,int nv)
                   6511: {
                   6512:   dmm_comp_nv = nv;
                   6513:   qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
                   6514: }
                   6515:
                   6516: void dpm_sort(DPM p,DPM *rp)
                   6517: {
                   6518:   DMM t,t1;
                   6519:   int len,i,n;
                   6520:   DMM *a;
                   6521:   DPM d;
                   6522:
                   6523:   if ( !p ) *rp = 0;
                   6524:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   6525:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   6526:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   6527:   n = p->nv;
                   6528:   dmm_sort_by_ord(a,len,n);
                   6529:   t = 0;
                   6530:   for ( i = len-1; i >= 0; i-- ) {
                   6531:     NEWDMM(t1);
                   6532:     t1->c = a[i]->c;
                   6533:     t1->dl = a[i]->dl;
                   6534:     t1->pos = a[i]->pos;
                   6535:     t1->next = t;
                   6536:     t = t1;
                   6537:   }
                   6538:   MKDPM(n,t,d);
                   6539:   SG(d) = SG(p);
                   6540:   *rp = d;
                   6541: }
                   6542:
1.18      noro     6543: int dpm_comp(DPM *a,DPM *b)
                   6544: {
1.22      noro     6545:   return -compdpm(CO,*a,*b);
1.18      noro     6546: }
                   6547:
                   6548: NODE dpm_sort_list(NODE l)
                   6549: {
                   6550:   int i,len;
                   6551:   NODE t,t1;
                   6552:   DPM *a;
                   6553:
                   6554:   len = length(l);
                   6555:   a = (DPM *)MALLOC(len*sizeof(DPM));
                   6556:   for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
                   6557:   qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
                   6558:   t = 0;
                   6559:   for ( i = len-1; i >= 0; i-- ) {
                   6560:     MKNODE(t1,(pointer)a[i],t); t = t1;
                   6561:   }
                   6562:   return t;
                   6563: }
                   6564:
1.20      noro     6565: int nmv_comp(NMV a,NMV b)
                   6566: {
1.21      noro     6567:   int t;
                   6568:   t = DL_COMPARE(a->dl,b->dl);
                   6569:   return -t;
1.20      noro     6570: }
                   6571:
1.16      noro     6572: NDV dpmtondv(int mod,DPM p)
                   6573: {
                   6574:   NDV d;
                   6575:   NMV m,m0;
                   6576:   DMM t;
                   6577:   DMM *a;
                   6578:   int i,len,n;
                   6579:
                   6580:   if ( !p ) return 0;
                   6581:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   6582:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   6583:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   6584:   n = p->nv;
                   6585:   dmm_sort_by_ord(a,len,n);
                   6586:   if ( mod > 0 || mod == -1 )
                   6587:     m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   6588:   else
                   6589:     m0 = m = MALLOC(len*nmv_adv);
                   6590: #if 0
                   6591:   ndv_alloc += nmv_adv*len;
                   6592: #endif
                   6593:   for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6594:     dltondl(n,a[i]->dl,DL(m));
                   6595:     MPOS(DL(m)) = a[i]->pos;
1.20      noro     6596:     TD(DL(m)) = ndl_weight(DL(m));
1.16      noro     6597:     CZ(m) = (Z)a[i]->c;
                   6598:   }
1.20      noro     6599:   qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16      noro     6600:   MKNDV(NV(p),m0,len,d);
                   6601:   SG(d) = SG(p);
                   6602:   return d;
                   6603: }
                   6604:
1.1       noro     6605: ND ndvtond(int mod,NDV p)
                   6606: {
                   6607:     ND d;
                   6608:     NM m,m0;
                   6609:     NMV t;
                   6610:     int i,len;
                   6611:
                   6612:     if ( !p ) return 0;
                   6613:     m0 = 0;
                   6614:     len = p->len;
                   6615:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   6616:         NEXTNM(m0,m);
                   6617:         ndl_copy(DL(t),DL(m));
1.6       noro     6618:         CZ(m) = CZ(t);
1.1       noro     6619:     }
                   6620:     NEXT(m) = 0;
                   6621:     MKND(NV(p),m0,len,d);
                   6622:     SG(d) = SG(p);
1.28      noro     6623:     d->sig = p->sig;
1.1       noro     6624:     return d;
                   6625: }
                   6626:
                   6627: DP ndvtodp(int mod,NDV p)
                   6628: {
                   6629:     MP m,m0;
                   6630:   DP d;
                   6631:     NMV t;
                   6632:     int i,len;
                   6633:
                   6634:     if ( !p ) return 0;
                   6635:     m0 = 0;
                   6636:     len = p->len;
                   6637:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   6638:         NEXTMP(m0,m);
                   6639:       m->dl = ndltodl(nd_nvar,DL(t));
                   6640:       m->c = (Obj)ndctop(mod,t->c);
                   6641:     }
                   6642:     NEXT(m) = 0;
                   6643:   MKDP(nd_nvar,m0,d);
                   6644:     SG(d) = SG(p);
                   6645:     return d;
                   6646: }
                   6647:
1.16      noro     6648: DPM ndvtodpm(int mod,NDV p)
                   6649: {
                   6650:   DMM m,m0;
                   6651:   DPM d;
                   6652:   NMV t;
                   6653:   int i,len;
                   6654:
                   6655:   if ( !p ) return 0;
                   6656:   m0 = 0;
                   6657:   len = p->len;
                   6658:   for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   6659:     NEXTDMM(m0,m);
                   6660:     m->dl = ndltodl(nd_nvar,DL(t));
                   6661:     m->c = (Obj)ndctop(mod,t->c);
                   6662:     m->pos = MPOS(DL(t));
                   6663:   }
                   6664:   NEXT(m) = 0;
                   6665:   MKDPM(nd_nvar,m0,d);
                   6666:   SG(d) = SG(p);
                   6667:   return d;
                   6668: }
                   6669:
                   6670:
1.1       noro     6671: DP ndtodp(int mod,ND p)
                   6672: {
                   6673:     MP m,m0;
                   6674:   DP d;
                   6675:     NM t;
                   6676:     int i,len;
                   6677:
                   6678:     if ( !p ) return 0;
                   6679:     m0 = 0;
                   6680:     len = p->len;
                   6681:     for ( t = BDY(p); t; t = NEXT(t) ) {
                   6682:         NEXTMP(m0,m);
                   6683:       m->dl = ndltodl(nd_nvar,DL(t));
                   6684:       m->c = (Obj)ndctop(mod,t->c);
                   6685:     }
                   6686:     NEXT(m) = 0;
                   6687:   MKDP(nd_nvar,m0,d);
                   6688:     SG(d) = SG(p);
                   6689:     return d;
                   6690: }
                   6691:
                   6692: void ndv_print(NDV p)
                   6693: {
                   6694:     NMV m;
                   6695:     int i,len;
                   6696:
                   6697:     if ( !p ) printf("0\n");
                   6698:     else {
                   6699:         len = LEN(p);
                   6700:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6701:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     6702:             else printf("+%ld*",CM(m));
1.1       noro     6703:             ndl_print(DL(m));
                   6704:         }
                   6705:         printf("\n");
                   6706:     }
                   6707: }
                   6708:
                   6709: void ndv_print_q(NDV p)
                   6710: {
                   6711:     NMV m;
                   6712:     int i,len;
                   6713:
                   6714:     if ( !p ) printf("0\n");
                   6715:     else {
                   6716:         len = LEN(p);
                   6717:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6718:             printf("+");
1.6       noro     6719:             printexpr(CO,(Obj)CZ(m));
1.1       noro     6720:             printf("*");
                   6721:             ndl_print(DL(m));
                   6722:         }
                   6723:         printf("\n");
                   6724:     }
                   6725: }
                   6726:
                   6727: NODE ndv_reducebase(NODE x,int *perm)
                   6728: {
                   6729:     int len,i,j;
                   6730:     NDVI w;
                   6731:     NODE t,t0;
                   6732:
                   6733:     len = length(x);
                   6734:     w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
                   6735:     for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
                   6736:         w[i].p = BDY(t); w[i].i = perm[i];
                   6737:     }
                   6738:     for ( i = 0; i < len; i++ ) {
                   6739:         for ( j = 0; j < i; j++ ) {
                   6740:             if ( w[i].p && w[j].p ) {
                   6741:                 if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
                   6742:                 else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
                   6743:             }
                   6744:         }
                   6745:     }
                   6746:     for ( i = j = 0, t0 = 0; i < len; i++ ) {
                   6747:         if ( w[i].p ) {
                   6748:             NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
                   6749:             perm[j++] = w[i].i;
                   6750:         }
                   6751:     }
                   6752:     NEXT(t) = 0; x = t0;
                   6753:     return x;
                   6754: }
                   6755:
                   6756: /* XXX incomplete */
                   6757:
1.21      noro     6758: extern DMMstack dmm_stack;
                   6759: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16      noro     6760:
1.1       noro     6761: void nd_init_ord(struct order_spec *ord)
                   6762: {
                   6763:   nd_module = (ord->id >= 256);
                   6764:   if ( nd_module ) {
                   6765:     nd_dcomp = -1;
1.21      noro     6766:     nd_module_ordtype = ord->module_ordtype;
1.1       noro     6767:     nd_pot_nelim = ord->pot_nelim;
                   6768:     nd_poly_weight_len = ord->nv;
                   6769:     nd_poly_weight = ord->top_weight;
                   6770:     nd_module_rank = ord->module_rank;
                   6771:     nd_module_weight = ord->module_top_weight;
                   6772:   }
                   6773:   nd_matrix = 0;
                   6774:   nd_matrix_len = 0;
                   6775:     switch ( ord->id ) {
                   6776:         case 0:
                   6777:             switch ( ord->ord.simple ) {
                   6778:                 case 0:
                   6779:                     nd_dcomp = 1;
                   6780:                     nd_isrlex = 1;
                   6781:                     break;
                   6782:                 case 1:
                   6783:                     nd_dcomp = 1;
                   6784:                     nd_isrlex = 0;
                   6785:                     break;
                   6786:                 case 2:
                   6787:                     nd_dcomp = 0;
                   6788:                     nd_isrlex = 0;
                   6789:                     ndl_compare_function = ndl_lex_compare;
                   6790:                     break;
                   6791:                 case 11:
                   6792:                     /* XXX */
                   6793:                     nd_dcomp = 0;
                   6794:                     nd_isrlex = 1;
                   6795:                     ndl_compare_function = ndl_ww_lex_compare;
                   6796:                     break;
                   6797:                 default:
                   6798:                     error("nd_gr : unsupported order");
                   6799:             }
                   6800:             break;
                   6801:         case 1:
                   6802:             /* block order */
                   6803:             /* XXX */
                   6804:             nd_dcomp = -1;
                   6805:             nd_isrlex = 0;
                   6806:             ndl_compare_function = ndl_block_compare;
                   6807:             break;
                   6808:         case 2:
                   6809:             /* matrix order */
                   6810:             /* XXX */
                   6811:             nd_dcomp = -1;
                   6812:             nd_isrlex = 0;
                   6813:             nd_matrix_len = ord->ord.matrix.row;
                   6814:             nd_matrix = ord->ord.matrix.matrix;
                   6815:             ndl_compare_function = ndl_matrix_compare;
                   6816:             break;
                   6817:         case 3:
                   6818:             /* composite order */
                   6819:             nd_dcomp = -1;
                   6820:             nd_isrlex = 0;
                   6821:             nd_worb_len = ord->ord.composite.length;
                   6822:             nd_worb = ord->ord.composite.w_or_b;
                   6823:             ndl_compare_function = ndl_composite_compare;
                   6824:             break;
                   6825:
                   6826:         /* module order */
                   6827:         case 256:
                   6828:             switch ( ord->ord.simple ) {
                   6829:                 case 0:
1.21      noro     6830:                     nd_dcomp = 0;
1.1       noro     6831:                     nd_isrlex = 1;
1.21      noro     6832:                     ndl_compare_function = ndl_module_glex_compare;
1.1       noro     6833:                     break;
                   6834:                 case 1:
1.21      noro     6835:                     nd_dcomp = 0;
1.1       noro     6836:                     nd_isrlex = 0;
                   6837:                     ndl_compare_function = ndl_module_glex_compare;
                   6838:                     break;
                   6839:                 case 2:
1.21      noro     6840:                     nd_dcomp = 0;
1.1       noro     6841:                     nd_isrlex = 0;
1.21      noro     6842:                     ndl_compare_function = ndl_module_compare;
                   6843:                     ndl_base_compare_function = ndl_lex_compare;
1.1       noro     6844:                     break;
                   6845:                 default:
1.21      noro     6846:                     error("nd_init_ord : unsupported order");
1.1       noro     6847:             }
                   6848:             break;
                   6849:         case 257:
                   6850:             /* block order */
                   6851:             nd_isrlex = 0;
1.21      noro     6852:             ndl_compare_function = ndl_module_compare;
                   6853:             ndl_base_compare_function = ndl_block_compare;
1.1       noro     6854:             break;
                   6855:         case 258:
                   6856:             /* matrix order */
                   6857:             nd_isrlex = 0;
                   6858:             nd_matrix_len = ord->ord.matrix.row;
                   6859:             nd_matrix = ord->ord.matrix.matrix;
1.21      noro     6860:             ndl_compare_function = ndl_module_compare;
                   6861:             ndl_base_compare_function = ndl_matrix_compare;
1.1       noro     6862:             break;
                   6863:         case 259:
                   6864:             /* composite order */
                   6865:             nd_isrlex = 0;
                   6866:             nd_worb_len = ord->ord.composite.length;
                   6867:             nd_worb = ord->ord.composite.w_or_b;
1.21      noro     6868:             ndl_compare_function = ndl_module_compare;
                   6869:             ndl_base_compare_function = ndl_composite_compare;
                   6870:             break;
                   6871:         case 300:
                   6872:             /* schreyer order */
                   6873:             if ( ord->base->id != 256 )
                   6874:                error("nd_init_ord : unsupported base order");
                   6875:             ndl_compare_function = ndl_module_schreyer_compare;
                   6876:             dmm_stack = ord->dmmstack;
                   6877:             switch ( ord->base->ord.simple ) {
                   6878:                 case 0:
                   6879:                     nd_isrlex = 1;
                   6880:                     ndl_base_compare_function = ndl_glex_compare;
                   6881:                     dl_base_compare_function = cmpdl_revgradlex;
                   6882:                     break;
                   6883:                 case 1:
                   6884:                     nd_isrlex = 0;
                   6885:                     ndl_base_compare_function = ndl_glex_compare;
                   6886:                     dl_base_compare_function = cmpdl_gradlex;
                   6887:                     break;
                   6888:                 case 2:
                   6889:                     nd_isrlex = 0;
                   6890:                     ndl_base_compare_function = ndl_lex_compare;
                   6891:                     dl_base_compare_function = cmpdl_lex;
                   6892:                     break;
                   6893:                 default:
                   6894:                     error("nd_init_ord : unsupported order");
                   6895:             }
1.1       noro     6896:             break;
                   6897:     }
                   6898:     nd_ord = ord;
                   6899: }
                   6900:
                   6901: BlockMask nd_create_blockmask(struct order_spec *ord)
                   6902: {
                   6903:     int n,i,j,s,l;
                   6904:     UINT *t;
                   6905:     BlockMask bm;
                   6906:
                   6907:     /* we only create mask table for block order */
                   6908:     if ( ord->id != 1 && ord->id != 257 )
                   6909:         return 0;
                   6910:     n = ord->ord.block.length;
                   6911:     bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   6912:     bm->n = n;
                   6913:     bm->order_pair = ord->ord.block.order_pair;
                   6914:     bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
                   6915:     for ( i = 0, s = 0; i < n; i++ ) {
                   6916:         bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
                   6917:         for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   6918:         l = bm->order_pair[i].length;
                   6919:         for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   6920:     }
                   6921:     return bm;
                   6922: }
                   6923:
                   6924: EPOS nd_create_epos(struct order_spec *ord)
                   6925: {
                   6926:     int i,j,l,s,ord_l,ord_o;
                   6927:     EPOS epos;
                   6928:     struct order_pair *op;
                   6929:
                   6930:     epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   6931:     switch ( ord->id ) {
1.21      noro     6932:         case 0: case 256: case 300:
1.1       noro     6933:             if ( nd_isrlex ) {
                   6934:                 for ( i = 0; i < nd_nvar; i++ ) {
                   6935:                     epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   6936:                     epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   6937:                 }
                   6938:             } else {
                   6939:                 for ( i = 0; i < nd_nvar; i++ ) {
                   6940:                     epos[i].i = nd_exporigin + i/nd_epw;
                   6941:                     epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   6942:                 }
                   6943:             }
                   6944:             break;
                   6945:         case 1: case 257:
                   6946:             /* block order */
                   6947:             l = ord->ord.block.length;
                   6948:             op = ord->ord.block.order_pair;
                   6949:             for ( j = 0, s = 0; j < l; j++ ) {
                   6950:                 ord_o = op[j].order;
                   6951:                 ord_l = op[j].length;
                   6952:                 if ( !ord_o )
                   6953:                     for ( i = 0; i < ord_l; i++ ) {
                   6954:                         epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   6955:                         epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   6956:                     }
                   6957:                 else
                   6958:                     for ( i = 0; i < ord_l; i++ ) {
                   6959:                         epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   6960:                         epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   6961:                     }
                   6962:                 s += ord_l;
                   6963:             }
                   6964:             break;
                   6965:         case 2:
                   6966:             /* matrix order */
                   6967:         case 3:
                   6968:             /* composite order */
                   6969:         default:
                   6970:             for ( i = 0; i < nd_nvar; i++ ) {
                   6971:                 epos[i].i = nd_exporigin + i/nd_epw;
                   6972:                 epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   6973:             }
                   6974:             break;
                   6975:     }
                   6976:     return epos;
                   6977: }
                   6978:
                   6979: /* external interface */
                   6980:
                   6981: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
                   6982: {
                   6983:     NODE t,in0,in;
                   6984:     ND ndf,nf;
                   6985:     NDV ndvf;
                   6986:     VL vv,tv;
                   6987:     int stat,nvar,max,mrank;
                   6988:     union oNDC dn;
                   6989:     Q cont;
                   6990:     P pp;
                   6991:     LIST ppl;
                   6992:
                   6993:     if ( !f ) {
                   6994:         *rp = 0;
                   6995:         return;
                   6996:     }
                   6997:     pltovl(v,&vv);
                   6998:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   6999:
                   7000:     /* max=65536 implies nd_bpe=32 */
                   7001:     max = 65536;
                   7002:
                   7003:   nd_module = 0;
                   7004:   /* nd_module will be set if ord is a module ordering */
                   7005:     nd_init_ord(ord);
                   7006:     nd_setup_parameters(nvar,max);
                   7007:     if ( nd_module && OID(f) != O_LIST )
                   7008:         error("nd_nf_p : the first argument must be a list");
                   7009:   if ( nd_module ) mrank = length(BDY((LIST)f));
                   7010:     /* conversion to ndv */
                   7011:     for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   7012:         NEXTNODE(in0,in);
                   7013:         if ( nd_module ) {
                   7014:           if ( !BDY(t) || OID(BDY(t)) != O_LIST
                   7015:                || length(BDY((LIST)BDY(t))) != mrank )
                   7016:               error("nd_nf_p : inconsistent basis element");
                   7017:           if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
                   7018:           else ppl = (LIST)BDY(t);
                   7019:           BDY(in) = (pointer)pltondv(CO,vv,ppl);
                   7020:         } else {
                   7021:           if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
                   7022:           else pp = (P)BDY(t);
                   7023:           BDY(in) = (pointer)ptondv(CO,vv,pp);
                   7024:         }
                   7025:         if ( m ) ndv_mod(m,(NDV)BDY(in));
                   7026:     }
                   7027:     if ( in0 ) NEXT(in) = 0;
                   7028:
                   7029:     if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
                   7030:     else ndvf = ptondv(CO,vv,(P)f);
                   7031:     if ( m ) ndv_mod(m,ndvf);
                   7032:     ndf = (pointer)ndvtond(m,ndvf);
                   7033:
                   7034:     /* dont sort, dont removecont */
1.24      noro     7035:     ndv_setup(m,0,in0,1,1,0);
1.1       noro     7036:     nd_scale=2;
1.6       noro     7037:     stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1       noro     7038:     if ( !stat )
                   7039:         error("nd_nf_p : exponent too large");
                   7040:     if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
                   7041:     else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
                   7042: }
                   7043:
                   7044: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   7045: {
                   7046:     NM m;
                   7047:     UINT *t,*s;
                   7048:     int i;
                   7049:
                   7050:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   7051:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7052:         t = DL(m);
                   7053:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   7054:         r[i] = CM(m);
                   7055:     }
                   7056:     for ( i = 0; !r[i]; i++ );
                   7057:     return i;
                   7058: }
                   7059:
                   7060: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
                   7061: {
                   7062:     NM m;
                   7063:     UINT *t,*s;
                   7064:     int i;
                   7065:
                   7066:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   7067:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7068:         t = DL(m);
                   7069:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     7070:         r[i] = CZ(m);
1.1       noro     7071:     }
                   7072:     for ( i = 0; !r[i]; i++ );
                   7073:     return i;
                   7074: }
                   7075:
                   7076: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
                   7077: {
                   7078:     NM m;
                   7079:     UINT *t,*s;
                   7080:     int i;
                   7081:
                   7082:     for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
                   7083:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7084:         t = DL(m);
                   7085:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   7086:         mpz_set(r[i],BDY(CZ(m)));
                   7087:     }
                   7088:     for ( i = 0; !mpz_sgn(r[i]); i++ );
                   7089:     return i;
                   7090: }
                   7091:
                   7092: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
                   7093: {
                   7094:     NM m;
                   7095:     unsigned long *v;
                   7096:     int i,j,h,size;
                   7097:   UINT *s,*t;
                   7098:
                   7099:   size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
                   7100:     v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
                   7101:     bzero(v,size);
                   7102:     for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
                   7103:     t = DL(m);
                   7104:     h = ndl_hash_value(t);
                   7105:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   7106:       v[i/BLEN] |= 1L <<(i%BLEN);
                   7107:     }
                   7108:     return v;
                   7109: }
                   7110:
                   7111: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
                   7112: {
                   7113:     NMV mr;
                   7114:     UINT *d,*t,*s;
                   7115:     int i,j,len,h,head;
                   7116:
                   7117:     d = DL(m);
                   7118:     len = LEN(p);
                   7119:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   7120:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   7121:         ndl_add(d,DL(mr),t);
                   7122:     h = ndl_hash_value(t);
                   7123:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   7124:     if ( j == 0 ) head = i;
                   7125:       v[i/BLEN] |= 1L <<(i%BLEN);
                   7126:     }
                   7127:     return head;
                   7128: }
                   7129:
                   7130: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   7131: {
                   7132:     NM m;
                   7133:     NMV mr;
                   7134:     UINT *d,*t,*s;
                   7135:     NDV p;
                   7136:     int i,j,len;
                   7137:     Z *r;
                   7138:
                   7139:     m = pair->mul;
                   7140:     d = DL(m);
                   7141:     p = nd_ps[pair->index];
                   7142:     len = LEN(p);
                   7143:     r = (Z *)CALLOC(n,sizeof(Q));
                   7144:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   7145:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   7146:         ndl_add(d,DL(mr),t);
                   7147:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     7148:         r[i] = CZ(mr);
1.1       noro     7149:     }
                   7150:     return r;
                   7151: }
                   7152:
1.11      noro     7153: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1       noro     7154: {
                   7155:     NM m;
                   7156:     NMV mr;
1.11      noro     7157:     UINT *d,*t,*s,*u;
1.1       noro     7158:     NDV p;
                   7159:     unsigned char *ivc;
                   7160:     unsigned short *ivs;
                   7161:     UINT *v,*ivi,*s0v;
1.11      noro     7162:     int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1       noro     7163:     IndArray r;
                   7164:
                   7165:     m = pair->mul;
                   7166:     d = DL(m);
                   7167:     if ( trace )
                   7168:       p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
                   7169:     else
                   7170:       p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
                   7171:
                   7172:     len = LEN(p);
                   7173:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   7174:     v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11      noro     7175:     for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
                   7176:       ndl_add(d,DL(mr),t);
                   7177:       st = prev;
                   7178:       ed = n;
                   7179:       while ( ed > st ) {
                   7180:         md = (st+ed)/2;
                   7181:         u = s0+md*nd_wpd;
                   7182:         c = DL_COMPARE(u,t);
                   7183:         if ( c == 0 ) break;
                   7184:         else if ( c > 0 ) st = md;
                   7185:         else ed = md;
                   7186:       }
                   7187:       prev = v[j] = md;
1.1       noro     7188:     }
                   7189:     r = (IndArray)MALLOC(sizeof(struct oIndArray));
                   7190:     r->head = v[0];
                   7191:     diff = 0;
                   7192:     for ( i = 1; i < len; i++ ) {
                   7193:         cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   7194:     }
                   7195:     if ( diff < 256 ) {
                   7196:         r->width = 1;
                   7197:         ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   7198:         r->index.c = ivc;
                   7199:         for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   7200:     } else if ( diff < 65536 ) {
                   7201:         r->width = 2;
                   7202:         ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   7203:         r->index.s = ivs;
                   7204:         for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   7205:     } else {
                   7206:         r->width = 4;
                   7207:         ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   7208:         r->index.i = ivi;
                   7209:         for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
                   7210:     }
                   7211:     return r;
                   7212: }
                   7213:
                   7214: int compress_array(Z *svect,Z *cvect,int n)
                   7215: {
                   7216:     int i,j;
                   7217:
                   7218:     for ( i = j = 0; i < n; i++ )
                   7219:         if ( svect[i] ) cvect[j++] = svect[i];
                   7220:     return j;
                   7221: }
                   7222:
                   7223: void expand_array(Z *svect,Z *cvect,int n)
                   7224: {
                   7225:     int i,j;
                   7226:
                   7227:     for ( i = j = 0; j < n;  i++  )
                   7228:         if ( svect[i] ) svect[i] = cvect[j++];
                   7229: }
                   7230:
1.8       noro     7231: #if 0
1.1       noro     7232: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7233: {
                   7234:     int i,j,k,len,pos,prev,nz;
                   7235:     Z cs,mcs,c1,c2,cr,gcd,t;
                   7236:     IndArray ivect;
                   7237:     unsigned char *ivc;
                   7238:     unsigned short *ivs;
                   7239:     unsigned int *ivi;
                   7240:     NDV redv;
                   7241:     NMV mr;
                   7242:     NODE rp;
                   7243:     int maxrs;
                   7244:     double hmag;
                   7245:     Z *cvect;
1.3       noro     7246:     int l;
1.1       noro     7247:
                   7248:     maxrs = 0;
                   7249:     for ( i = 0; i < col && !svect[i]; i++ );
                   7250:     if ( i == col ) return maxrs;
                   7251:     hmag = p_mag((P)svect[i])*nd_scale;
                   7252:     cvect = (Z *)MALLOC(col*sizeof(Q));
                   7253:     for ( i = 0; i < nred; i++ ) {
                   7254:         ivect = imat[i];
                   7255:         k = ivect->head;
                   7256:         if ( svect[k] ) {
                   7257:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7258:             redv = nd_demand?ndv_load(rp0[i]->index)
                   7259:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   7260:             len = LEN(redv); mr = BDY(redv);
1.6       noro     7261:             igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1       noro     7262:             chsgnz(cs,&mcs);
                   7263:             if ( !UNIQ(cr) ) {
                   7264:                 for ( j = 0; j < col; j++ ) {
                   7265:                     mulz(svect[j],cr,&c1); svect[j] = c1;
                   7266:                 }
                   7267:             }
                   7268:             svect[k] = 0; prev = k;
                   7269:             switch ( ivect->width ) {
                   7270:                 case 1:
                   7271:                     ivc = ivect->index.c;
                   7272:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7273:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     7274:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     7275:                     }
                   7276:                     break;
                   7277:                 case 2:
                   7278:                     ivs = ivect->index.s;
                   7279:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7280:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     7281:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     7282:                     }
                   7283:                     break;
                   7284:                 case 4:
                   7285:                     ivi = ivect->index.i;
                   7286:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7287:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     7288:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     7289:                     }
                   7290:                     break;
                   7291:             }
                   7292:             for ( j = k+1; j < col && !svect[j]; j++ );
                   7293:             if ( j == col ) break;
                   7294:             if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
                   7295:                 nz = compress_array(svect,cvect,col);
                   7296:                 removecont_array((P *)cvect,nz,1);
                   7297:                 expand_array(svect,cvect,nz);
                   7298:                 hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   7299:             }
                   7300:         }
                   7301:     }
                   7302:     nz = compress_array(svect,cvect,col);
                   7303:     removecont_array((P *)cvect,nz,1);
                   7304:     expand_array(svect,cvect,nz);
                   7305:     if ( DP_Print ) {
                   7306:         fprintf(asir_out,"-"); fflush(asir_out);
                   7307:     }
                   7308:     return maxrs;
                   7309: }
1.4       noro     7310: #else
1.9       noro     7311:
1.4       noro     7312: /* direct mpz version */
                   7313: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7314: {
                   7315:     int i,j,k,len,pos,prev;
                   7316:     mpz_t cs,cr,gcd;
                   7317:     IndArray ivect;
                   7318:     unsigned char *ivc;
                   7319:     unsigned short *ivs;
                   7320:     unsigned int *ivi;
                   7321:     NDV redv;
                   7322:     NMV mr;
                   7323:     NODE rp;
                   7324:     int maxrs;
                   7325:     double hmag;
                   7326:     int l;
1.13      noro     7327:     static mpz_t *svect;
                   7328:     static int svect_len=0;
1.4       noro     7329:
                   7330:     maxrs = 0;
                   7331:     for ( i = 0; i < col && !svect0[i]; i++ );
                   7332:     if ( i == col ) return maxrs;
                   7333:     hmag = p_mag((P)svect0[i])*nd_scale;
1.13      noro     7334:     if ( col > svect_len ) {
                   7335:       svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   7336:       svect_len = col;
                   7337:     }
1.4       noro     7338:     for ( i = 0; i < col; i++ ) {
                   7339:       mpz_init(svect[i]);
                   7340:       if ( svect0[i] )
                   7341:         mpz_set(svect[i],BDY(svect0[i]));
                   7342:       else
                   7343:         mpz_set_ui(svect[i],0);
                   7344:     }
                   7345:     mpz_init(gcd); mpz_init(cs); mpz_init(cr);
                   7346:     for ( i = 0; i < nred; i++ ) {
                   7347:         ivect = imat[i];
                   7348:         k = ivect->head;
                   7349:         if ( mpz_sgn(svect[k]) ) {
                   7350:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7351:             redv = nd_demand?ndv_load(rp0[i]->index)
                   7352:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   7353:             len = LEN(redv); mr = BDY(redv);
1.6       noro     7354:             mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4       noro     7355:             mpz_div(cs,svect[k],gcd);
1.6       noro     7356:             mpz_div(cr,BDY(CZ(mr)),gcd);
1.4       noro     7357:             mpz_neg(cs,cs);
1.9       noro     7358:             if ( MUNIMPZ(cr) )
                   7359:               for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
                   7360:             else if ( !UNIMPZ(cr) )
                   7361:               for ( j = 0; j < col; j++ ) {
                   7362:                 if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
                   7363:               }
1.4       noro     7364:             mpz_set_ui(svect[k],0);
                   7365:             prev = k;
                   7366:             switch ( ivect->width ) {
                   7367:                 case 1:
                   7368:                     ivc = ivect->index.c;
                   7369:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7370:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     7371:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     7372:                     }
                   7373:                     break;
                   7374:                 case 2:
                   7375:                     ivs = ivect->index.s;
                   7376:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7377:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     7378:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     7379:                     }
                   7380:                     break;
                   7381:                 case 4:
                   7382:                     ivi = ivect->index.i;
                   7383:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7384:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     7385:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     7386:                     }
                   7387:                     break;
                   7388:             }
                   7389:             for ( j = k+1; j < col && !svect[j]; j++ );
                   7390:             if ( j == col ) break;
                   7391:             if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
                   7392:                 mpz_removecont_array(svect,col);
                   7393:                 hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
                   7394:             }
                   7395:         }
                   7396:     }
                   7397:     mpz_removecont_array(svect,col);
                   7398:     if ( DP_Print ) {
                   7399:         fprintf(asir_out,"-"); fflush(asir_out);
                   7400:     }
                   7401:     for ( i = 0; i < col; i++ )
                   7402:       if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
                   7403:       else svect0[i] = 0;
                   7404:     return maxrs;
                   7405: }
                   7406: #endif
1.1       noro     7407:
                   7408: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7409: {
                   7410:     int i,j,k,len,pos,prev;
                   7411:     UINT c,c1,c2,c3,up,lo,dmy;
                   7412:     IndArray ivect;
                   7413:     unsigned char *ivc;
                   7414:     unsigned short *ivs;
                   7415:     unsigned int *ivi;
                   7416:     NDV redv;
                   7417:     NMV mr;
                   7418:     NODE rp;
                   7419:     int maxrs;
                   7420:
                   7421:     maxrs = 0;
                   7422:     for ( i = 0; i < nred; i++ ) {
                   7423:         ivect = imat[i];
                   7424:         k = ivect->head; svect[k] %= m;
                   7425:         if ( (c = svect[k]) != 0 ) {
                   7426:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7427:             c = m-c; redv = nd_ps[rp0[i]->index];
                   7428:             len = LEN(redv); mr = BDY(redv);
                   7429:             svect[k] = 0; prev = k;
                   7430:             switch ( ivect->width ) {
                   7431:                 case 1:
                   7432:                     ivc = ivect->index.c;
                   7433:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7434:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
                   7435:             if ( c1 ) {
                   7436:               c2 = svect[pos];
                   7437:                           DMA(c1,c,c2,up,lo);
                   7438:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   7439:                           } else svect[pos] = lo;
                   7440:             }
                   7441:                     }
                   7442:                     break;
                   7443:                 case 2:
                   7444:                     ivs = ivect->index.s;
                   7445:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7446:                         pos = prev+ivs[j]; c1 = CM(mr);
                   7447:                         prev = pos;
                   7448:             if ( c1 ) {
                   7449:               c2 = svect[pos];
                   7450:                           DMA(c1,c,c2,up,lo);
                   7451:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   7452:                           } else svect[pos] = lo;
                   7453:             }
                   7454:                     }
                   7455:                     break;
                   7456:                 case 4:
                   7457:                     ivi = ivect->index.i;
                   7458:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7459:                         pos = prev+ivi[j]; c1 = CM(mr);
                   7460:                         prev = pos;
                   7461:             if ( c1 ) {
                   7462:               c2 = svect[pos];
                   7463:                           DMA(c1,c,c2,up,lo);
                   7464:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   7465:                           } else svect[pos] = lo;
                   7466:             }
                   7467:                     }
                   7468:                     break;
                   7469:             }
                   7470:         }
                   7471:     }
                   7472:     for ( i = 0; i < col; i++ )
                   7473:         if ( svect[i] >= (UINT)m ) svect[i] %= m;
                   7474:     return maxrs;
                   7475: }
                   7476:
                   7477: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7478: {
                   7479:     int i,j,k,len,pos,prev;
                   7480:     UINT c,c1,c2,c3,up,lo,dmy;
                   7481:     IndArray ivect;
                   7482:     unsigned char *ivc;
                   7483:     unsigned short *ivs;
                   7484:     unsigned int *ivi;
                   7485:     NDV redv;
                   7486:     NMV mr;
                   7487:     NODE rp;
                   7488:     int maxrs;
                   7489:
                   7490:     maxrs = 0;
                   7491:     for ( i = 0; i < nred; i++ ) {
                   7492:         ivect = imat[i];
                   7493:         k = ivect->head;
                   7494:         if ( (c = svect[k]) != 0 ) {
                   7495:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7496:             c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
                   7497:             len = LEN(redv); mr = BDY(redv);
                   7498:             svect[k] = 0; prev = k;
                   7499:             switch ( ivect->width ) {
                   7500:                 case 1:
                   7501:                     ivc = ivect->index.c;
                   7502:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7503:                         pos = prev+ivc[j]; prev = pos;
                   7504:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   7505:                     }
                   7506:                     break;
                   7507:                 case 2:
                   7508:                     ivs = ivect->index.s;
                   7509:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7510:                         pos = prev+ivs[j]; prev = pos;
                   7511:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   7512:                     }
                   7513:                     break;
                   7514:                 case 4:
                   7515:                     ivi = ivect->index.i;
                   7516:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7517:                         pos = prev+ivi[j]; prev = pos;
                   7518:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   7519:                     }
                   7520:                     break;
                   7521:             }
                   7522:         }
                   7523:     }
                   7524:     return maxrs;
                   7525: }
                   7526:
                   7527: ND nd_add_lf(ND p1,ND p2)
                   7528: {
                   7529:     int n,c,can;
                   7530:     ND r;
                   7531:     NM m1,m2,mr0,mr,s;
                   7532:     Z t;
                   7533:
                   7534:     if ( !p1 ) return p2;
                   7535:     else if ( !p2 ) return p1;
                   7536:     else {
                   7537:         can = 0;
                   7538:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   7539:             c = DL_COMPARE(DL(m1),DL(m2));
                   7540:             switch ( c ) {
                   7541:                 case 0:
                   7542:                     addlf(CZ(m1),CZ(m2),&t);
                   7543:                     s = m1; m1 = NEXT(m1);
                   7544:                     if ( t ) {
                   7545:                         can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
                   7546:                     } else {
                   7547:                         can += 2; FREENM(s);
                   7548:                     }
                   7549:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   7550:                     break;
                   7551:                 case 1:
                   7552:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   7553:                     break;
                   7554:                 case -1:
                   7555:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   7556:                     break;
                   7557:             }
                   7558:         }
                   7559:         if ( !mr0 )
                   7560:             if ( m1 ) mr0 = m1;
                   7561:             else if ( m2 ) mr0 = m2;
                   7562:             else return 0;
                   7563:         else if ( m1 ) NEXT(mr) = m1;
                   7564:         else if ( m2 ) NEXT(mr) = m2;
                   7565:         else NEXT(mr) = 0;
                   7566:         BDY(p1) = mr0;
                   7567:         SG(p1) = MAX(SG(p1),SG(p2));
                   7568:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   7569:         FREEND(p2);
                   7570:         return p1;
                   7571:     }
                   7572: }
                   7573:
                   7574: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7575: {
                   7576:     int i,j,k,len,pos,prev;
                   7577:     mpz_t c,mc,c1;
                   7578:     IndArray ivect;
                   7579:     unsigned char *ivc;
                   7580:     unsigned short *ivs;
                   7581:     unsigned int *ivi;
                   7582:     NDV redv;
                   7583:     NMV mr;
                   7584:     NODE rp;
                   7585:     int maxrs;
                   7586:
                   7587:     maxrs = 0;
                   7588:     lf_lazy = 1;
                   7589:     for ( i = 0; i < nred; i++ ) {
                   7590:         ivect = imat[i];
                   7591:         k = ivect->head;
                   7592:         mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
                   7593:         if ( mpz_sgn(svect[k]) ) {
                   7594:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7595:             mpz_neg(svect[k],svect[k]);
                   7596:             redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
                   7597:             len = LEN(redv); mr = BDY(redv);
                   7598:             prev = k;
                   7599:             switch ( ivect->width ) {
                   7600:                 case 1:
                   7601:                     ivc = ivect->index.c;
                   7602:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7603:                         pos = prev+ivc[j]; prev = pos;
                   7604:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   7605:                     }
                   7606:                     break;
                   7607:                 case 2:
                   7608:                     ivs = ivect->index.s;
                   7609:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7610:                         pos = prev+ivs[j]; prev = pos;
                   7611:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   7612:                     }
                   7613:                     break;
                   7614:                 case 4:
                   7615:                     ivi = ivect->index.i;
                   7616:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7617:                         pos = prev+ivi[j]; prev = pos;
                   7618:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   7619:                     }
                   7620:                     break;
                   7621:             }
                   7622:             mpz_set_ui(svect[k],0);
                   7623:         }
                   7624:     }
                   7625:     lf_lazy=0;
                   7626:     for ( i = 0; i < col; i++ ) {
                   7627:         mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
                   7628:     }
                   7629:     return maxrs;
                   7630: }
                   7631:
                   7632: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
                   7633: {
                   7634:     int i,j,k,l,rank,s;
                   7635:     mpz_t a,a1,inv;
                   7636:     mpz_t *t,*pivot,*pk;
                   7637:     mpz_t **mat;
                   7638:     struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
                   7639:     int size,size1;
                   7640:
                   7641:     mpz_init(inv);
                   7642:     mpz_init(a);
                   7643:     mat = (mpz_t **)mat0;
                   7644:         size = 0;
                   7645:     for ( rank = 0, j = 0; j < col; j++ ) {
                   7646:         for ( i = rank; i < row; i++ ) {
                   7647:             mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
                   7648:         }
                   7649:         for ( i = rank; i < row; i++ )
                   7650:             if ( mpz_sgn(mat[i][j]) )
                   7651:                 break;
                   7652:         if ( i == row ) {
                   7653:             colstat[j] = 0;
                   7654:             continue;
                   7655:         } else
                   7656:             colstat[j] = 1;
                   7657:         if ( i != rank ) {
                   7658:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   7659:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   7660:         }
                   7661:         pivot = mat[rank];
                   7662:         s = sugar[rank];
                   7663:         mpz_invert(inv,pivot[j],BDY(current_mod_lf));
                   7664:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   7665:             if ( mpz_sgn(*pk) ) {
                   7666:                 mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
                   7667:             }
                   7668:         for ( i = rank+1; i < row; i++ ) {
                   7669:             t = mat[i];
                   7670:             if ( mpz_sgn(t[j]) ) {
                   7671:                 sugar[i] = MAX(sugar[i],s);
                   7672:                 mpz_neg(a,t[j]);
                   7673:                 red_by_vect_lf(t+j,pivot+j,a,col-j);
                   7674:             }
                   7675:         }
                   7676:         rank++;
                   7677:     }
                   7678:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   7679:         if ( colstat[j] ) {
                   7680:             pivot = mat[l];
                   7681:             s = sugar[l];
                   7682:             for ( k = j; k < col; k++ )
                   7683:               mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
                   7684:             for ( i = 0; i < l; i++ ) {
                   7685:                 t = mat[i];
                   7686:                 if ( mpz_sgn(t[j]) ) {
                   7687:                     sugar[i] = MAX(sugar[i],s);
                   7688:                     mpz_neg(a,t[j]);
                   7689:                     red_by_vect_lf(t+j,pivot+j,a,col-j);
                   7690:                 }
                   7691:             }
                   7692:             l--;
                   7693:         }
                   7694:     for ( j = 0, l = 0; l < rank; j++ )
                   7695:         if ( colstat[j] ) {
                   7696:             t = mat[l];
                   7697:             for ( k = j; k < col; k++ ) {
                   7698:                 mpz_mod(t[k],t[k],BDY(current_mod_lf));
                   7699:             }
                   7700:             l++;
                   7701:         }
                   7702:     return rank;
                   7703: }
                   7704:
                   7705:
                   7706: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   7707: {
                   7708:     int j,k,len;
                   7709:     UINT *p;
                   7710:     UINT c;
                   7711:     NDV r;
                   7712:     NMV mr0,mr;
                   7713:
                   7714:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   7715:     if ( !len ) return 0;
                   7716:     else {
                   7717:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   7718: #if 0
                   7719:         ndv_alloc += nmv_adv*len;
                   7720: #endif
                   7721:         mr = mr0;
                   7722:         p = s0vect;
                   7723:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   7724:             if ( !rhead[j] ) {
                   7725:                 if ( (c = vect[k++]) != 0 ) {
                   7726:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   7727:                 }
                   7728:             }
                   7729:         MKNDV(nd_nvar,mr0,len,r);
                   7730:         return r;
                   7731:     }
                   7732: }
                   7733:
                   7734: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
                   7735: {
                   7736:     int j,k,len;
                   7737:     UINT *p;
                   7738:     NDV r;
                   7739:     NMV mr0,mr;
                   7740:
                   7741:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
                   7742:     if ( !len ) return 0;
                   7743:     else {
                   7744:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   7745:         mr = mr0;
                   7746:         p = s0vect;
                   7747:         for ( j = 0; j < col; j++, p += nd_wpd )
                   7748:       if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
                   7749:             ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
                   7750:           }
                   7751:         MKNDV(nd_nvar,mr0,len,r);
                   7752:         return r;
                   7753:     }
                   7754: }
                   7755:
                   7756: /* for preprocessed vector */
                   7757:
                   7758: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   7759: {
1.6       noro     7760:   int j,k,len;
                   7761:   UINT *p;
                   7762:   Z c;
                   7763:   NDV r;
                   7764:   NMV mr0,mr;
1.1       noro     7765:
1.6       noro     7766:   for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   7767:   if ( !len ) return 0;
                   7768:   else {
                   7769:     mr0 = (NMV)MALLOC(nmv_adv*len);
1.1       noro     7770: #if 0
1.6       noro     7771:     ndv_alloc += nmv_adv*len;
1.1       noro     7772: #endif
1.6       noro     7773:     mr = mr0;
                   7774:     p = s0vect;
                   7775:     for ( j = k = 0; j < col; j++, p += nd_wpd ) {
                   7776:       if ( !rhead[j] ) {
                   7777:         if ( (c = vect[k++]) != 0 ) {
                   7778:           if ( !INT(c) )
                   7779:             error("vect_to_ndv_q : components must be integers");
                   7780:             ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
                   7781:         }
                   7782:       }
1.1       noro     7783:     }
1.6       noro     7784:     MKNDV(nd_nvar,mr0,len,r);
                   7785:     return r;
                   7786:   }
1.1       noro     7787: }
                   7788:
                   7789: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   7790: {
                   7791:     int j,k,len;
                   7792:     UINT *p;
                   7793:     mpz_t c;
                   7794:     NDV r;
                   7795:     NMV mr0,mr;
                   7796:
                   7797:     for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
                   7798:     if ( !len ) return 0;
                   7799:     else {
                   7800:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   7801: #if 0
                   7802:         ndv_alloc += nmv_adv*len;
                   7803: #endif
                   7804:         mr = mr0;
                   7805:         p = s0vect;
                   7806:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   7807:             if ( !rhead[j] ) {
                   7808:                 c[0] = vect[k++][0];
                   7809:                 if ( mpz_sgn(c) ) {
                   7810:                     ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
                   7811:                 }
                   7812:             }
                   7813:         MKNDV(nd_nvar,mr0,len,r);
                   7814:         return r;
                   7815:     }
                   7816: }
                   7817:
                   7818: /* for plain vector */
                   7819:
                   7820: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
                   7821: {
                   7822:     int j,k,len;
                   7823:     UINT *p;
                   7824:     Z c;
                   7825:     NDV r;
                   7826:     NMV mr0,mr;
                   7827:
                   7828:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   7829:     if ( !len ) return 0;
                   7830:     else {
                   7831:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   7832: #if 0
                   7833:         ndv_alloc += nmv_adv*len;
                   7834: #endif
                   7835:         mr = mr0;
                   7836:         p = s0vect;
                   7837:         for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   7838:             if ( (c = vect[k]) != 0 ) {
                   7839:                 if ( !INT(c) )
1.6       noro     7840:                     error("plain_vect_to_ndv_q : components must be integers");
                   7841:                 ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1       noro     7842:             }
                   7843:         MKNDV(nd_nvar,mr0,len,r);
                   7844:         return r;
                   7845:     }
                   7846: }
                   7847:
                   7848: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
                   7849: {
                   7850:     ND_pairs t;
                   7851:     NODE sp0,sp;
                   7852:     int stat;
                   7853:     ND spol;
                   7854:
                   7855:     for ( t = l; t; t = NEXT(t) ) {
                   7856:         stat = nd_sp(m,trace,t,&spol);
                   7857:         if ( !stat ) return 0;
                   7858:         if ( spol ) {
                   7859:             add_pbucket_symbolic(bucket,spol);
                   7860:         }
                   7861:     }
                   7862:     return 1;
                   7863: }
                   7864:
                   7865: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   7866: {
                   7867:     NODE rp0,rp;
                   7868:     NM mul,head,s0,s;
                   7869:     int index,col,i,sugar;
                   7870:     RHist h;
                   7871:     UINT *s0v,*p;
                   7872:     NM_ind_pair pair;
                   7873:     ND red;
                   7874:     NDV *ps;
                   7875:
                   7876:     s0 = 0; rp0 = 0; col = 0;
                   7877:   if ( nd_demand )
                   7878:       ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   7879:   else
                   7880:       ps = trace?nd_ps_trace:nd_ps;
                   7881:     while ( 1 ) {
                   7882:         head = remove_head_pbucket_symbolic(bucket);
                   7883:         if ( !head ) break;
                   7884:         if ( !s0 ) s0 = head;
                   7885:         else NEXT(s) = head;
                   7886:         s = head;
                   7887:         index = ndl_find_reducer(DL(head));
                   7888:         if ( index >= 0 ) {
                   7889:             h = nd_psh[index];
                   7890:             NEWNM(mul);
                   7891:             ndl_sub(DL(head),DL(h),DL(mul));
                   7892:             if ( ndl_check_bound2(index,DL(mul)) )
                   7893:                 return 0;
                   7894:             sugar = TD(DL(mul))+SG(ps[index]);
1.28      noro     7895:             MKNM_ind_pair(pair,mul,index,sugar,0);
1.1       noro     7896:             red = ndv_mul_nm_symbolic(mul,ps[index]);
                   7897:             add_pbucket_symbolic(bucket,nd_remove_head(red));
                   7898:             NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   7899:         }
                   7900:         col++;
                   7901:     }
                   7902:     if ( rp0 ) NEXT(rp) = 0;
                   7903:     NEXT(s) = 0;
                   7904:     s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   7905:     for ( i = 0, p = s0v, s = s0; i < col;
                   7906:         i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   7907:     *s0vect = s0v;
                   7908:     *r = rp0;
                   7909:     return col;
                   7910: }
                   7911:
                   7912: void print_ndp(ND_pairs l)
                   7913: {
                   7914:   ND_pairs t;
                   7915:
                   7916:   for ( t = l; t; t = NEXT(t) )
                   7917:     printf("[%d,%d] ",t->i1,t->i2);
                   7918:   printf("\n");
                   7919: }
                   7920:
                   7921: NODE nd_f4(int m,int checkonly,int **indp)
                   7922: {
                   7923:     int i,nh,stat,index,f4red;
                   7924:     NODE r,g,tn0,tn,node;
                   7925:     ND_pairs d,l,t,ll0,ll,lh;
                   7926:   LIST l0,l1;
                   7927:     ND spol,red;
                   7928:     NDV nf,redv;
                   7929:     NM s0,s;
                   7930:     NODE rp0,srp0,nflist,nzlist,nzlist_t;
                   7931:     int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
                   7932:     UINT c;
                   7933:     UINT **spmat;
                   7934:     UINT *s0vect,*svect,*p,*v;
                   7935:     int *colstat;
                   7936:     IndArray *imat;
                   7937:     int *rhead;
                   7938:     int spcol,sprow;
                   7939:     int sugar,sugarh;
                   7940:     PGeoBucket bucket;
                   7941:     struct oEGT eg0,eg1,eg_f4;
                   7942:     Z i1,i2,sugarq;
1.12      noro     7943:
                   7944:     init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1       noro     7945: #if 0
                   7946:     ndv_alloc = 0;
                   7947: #endif
1.11      noro     7948:     Nf4_red=0;
1.1       noro     7949:     g = 0; d = 0;
                   7950:     for ( i = 0; i < nd_psn; i++ ) {
                   7951:         d = update_pairs(d,g,i,0);
                   7952:         g = update_base(g,i);
                   7953:     }
                   7954:   nzlist = 0;
                   7955:     nzlist_t = nd_nzlist;
                   7956:     f4red = 1;
                   7957:     nd_last_nonzero = 0;
                   7958:     while ( d ) {
                   7959:         get_eg(&eg0);
                   7960:         l = nd_minsugarp(d,&d);
                   7961:         sugar = nd_sugarweight?l->sugar2:SG(l);
                   7962:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   7963:         if ( nzlist_t ) {
                   7964:             node = BDY((LIST)BDY(nzlist_t));
1.6       noro     7965:             sugarh = ZTOS((Q)ARG0(node));
1.1       noro     7966:             tn = BDY((LIST)ARG1(node));
                   7967:             if ( !tn ) {
                   7968:               nzlist_t = NEXT(nzlist_t);
                   7969:               continue;
                   7970:             }
                   7971:             /* tn = [[i1,i2],...] */
                   7972:             lh = nd_ipairtospair(tn);
                   7973:         }
                   7974:         bucket = create_pbucket();
                   7975:         stat = nd_sp_f4(m,0,l,bucket);
                   7976:         if ( !stat ) {
                   7977:             for ( t = l; NEXT(t); t = NEXT(t) );
                   7978:             NEXT(t) = d; d = l;
                   7979:             d = nd_reconstruct(0,d);
                   7980:             continue;
                   7981:         }
                   7982:         if ( bucket->m < 0 ) continue;
                   7983:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   7984:         if ( !col ) {
                   7985:             for ( t = l; NEXT(t); t = NEXT(t) );
                   7986:             NEXT(t) = d; d = l;
                   7987:             d = nd_reconstruct(0,d);
                   7988:             continue;
                   7989:         }
1.12      noro     7990:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1       noro     7991:         if ( DP_Print )
1.6       noro     7992:             fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5       noro     7993:                 sugar,eg_f4.exectime);
1.1       noro     7994:         nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
                   7995:         if ( checkonly && nflist ) return 0;
                   7996:         /* adding new bases */
                   7997:         if ( nflist ) nd_last_nonzero = f4red;
                   7998:         for ( r = nflist; r; r = NEXT(r) ) {
                   7999:             nf = (NDV)BDY(r);
                   8000:             ndv_removecont(m,nf);
                   8001:             if ( !m && nd_nalg ) {
                   8002:                 ND nf1;
                   8003:
                   8004:                 nf1 = ndvtond(m,nf);
                   8005:                 nd_monic(0,&nf1);
                   8006:                 nd_removecont(m,nf1);
                   8007:                 nf = ndtondv(m,nf1);
                   8008:             }
1.24      noro     8009:             nh = ndv_newps(m,nf,0);
1.1       noro     8010:             d = update_pairs(d,g,nh,0);
                   8011:             g = update_base(g,nh);
                   8012:         }
                   8013:         if ( DP_Print ) {
                   8014:           fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
                   8015:         }
                   8016:         if ( nd_gentrace ) {
                   8017:       for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
                   8018:         NEXTNODE(tn0,tn);
1.6       noro     8019:                 STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1       noro     8020:                 node = mknode(2,i1,i2); MKLIST(l0,node);
                   8021:         BDY(tn) = l0;
                   8022:       }
                   8023:       if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6       noro     8024:             STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1       noro     8025:             MKNODE(node,l1,nzlist); nzlist = node;
                   8026:         }
                   8027:         if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
                   8028:         f4red++;
                   8029:         if ( nd_f4red && f4red > nd_f4red ) break;
                   8030:         if ( nd_rank0 && !nflist ) break;
                   8031:     }
                   8032:     if ( nd_gentrace ) {
                   8033:     MKLIST(l0,reverse_node(nzlist));
                   8034:         MKNODE(nd_alltracelist,l0,0);
                   8035:     }
                   8036: #if 0
                   8037:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   8038: #endif
1.12      noro     8039:   if ( DP_Print ) {
                   8040:     fprintf(asir_out,"number of red=%d,",Nf4_red);
                   8041:     fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
                   8042:       f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
                   8043:   }
1.1       noro     8044:   conv_ilist(nd_demand,0,g,indp);
                   8045:     return g;
                   8046: }
                   8047:
                   8048: NODE nd_f4_trace(int m,int **indp)
                   8049: {
                   8050:     int i,nh,stat,index;
                   8051:     NODE r,g;
                   8052:     ND_pairs d,l,l0,t;
                   8053:     ND spol,red;
                   8054:     NDV nf,redv,nfqv,nfv;
                   8055:     NM s0,s;
                   8056:     NODE rp0,srp0,nflist;
                   8057:     int nsp,nred,col,rank,len,k,j,a;
                   8058:     UINT c;
                   8059:     UINT **spmat;
                   8060:     UINT *s0vect,*svect,*p,*v;
                   8061:     int *colstat;
                   8062:     IndArray *imat;
                   8063:     int *rhead;
                   8064:     int spcol,sprow;
                   8065:     int sugar;
                   8066:     PGeoBucket bucket;
                   8067:     struct oEGT eg0,eg1,eg_f4;
                   8068:
                   8069:     g = 0; d = 0;
                   8070:     for ( i = 0; i < nd_psn; i++ ) {
                   8071:         d = update_pairs(d,g,i,0);
                   8072:         g = update_base(g,i);
                   8073:     }
                   8074:     while ( d ) {
                   8075:         get_eg(&eg0);
                   8076:         l = nd_minsugarp(d,&d);
                   8077:         sugar = SG(l);
                   8078:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   8079:         bucket = create_pbucket();
                   8080:         stat = nd_sp_f4(m,0,l,bucket);
                   8081:         if ( !stat ) {
                   8082:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8083:             NEXT(t) = d; d = l;
                   8084:             d = nd_reconstruct(1,d);
                   8085:             continue;
                   8086:         }
                   8087:         if ( bucket->m < 0 ) continue;
                   8088:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   8089:         if ( !col ) {
                   8090:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8091:             NEXT(t) = d; d = l;
                   8092:             d = nd_reconstruct(1,d);
                   8093:             continue;
                   8094:         }
                   8095:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   8096:         if ( DP_Print )
1.3       noro     8097:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5       noro     8098:                 sugar,eg_f4.exectime);
1.1       noro     8099:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   8100:         if ( !l0 ) continue;
                   8101:         l = l0;
                   8102:
                   8103:         /* over Q */
                   8104:         bucket = create_pbucket();
                   8105:         stat = nd_sp_f4(0,1,l,bucket);
                   8106:         if ( !stat ) {
                   8107:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8108:             NEXT(t) = d; d = l;
                   8109:             d = nd_reconstruct(1,d);
                   8110:             continue;
                   8111:         }
                   8112:         if ( bucket->m < 0 ) continue;
                   8113:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   8114:         if ( !col ) {
                   8115:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8116:             NEXT(t) = d; d = l;
                   8117:             d = nd_reconstruct(1,d);
                   8118:             continue;
                   8119:         }
                   8120:         nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   8121:         /* adding new bases */
                   8122:         for ( r = nflist; r; r = NEXT(r) ) {
                   8123:             nfqv = (NDV)BDY(r);
                   8124:             ndv_removecont(0,nfqv);
1.6       noro     8125:             if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1       noro     8126:             if ( nd_nalg ) {
                   8127:                 ND nf1;
                   8128:
                   8129:                 nf1 = ndvtond(m,nfqv);
                   8130:                 nd_monic(0,&nf1);
                   8131:                 nd_removecont(0,nf1);
                   8132:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   8133:             }
                   8134:             nfv = ndv_dup(0,nfqv);
                   8135:             ndv_mod(m,nfv);
                   8136:             ndv_removecont(m,nfv);
1.24      noro     8137:             nh = ndv_newps(0,nfv,nfqv);
1.1       noro     8138:             d = update_pairs(d,g,nh,0);
                   8139:             g = update_base(g,nh);
                   8140:         }
                   8141:     }
                   8142: #if 0
                   8143:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   8144: #endif
                   8145:   conv_ilist(nd_demand,1,g,indp);
                   8146:     return g;
                   8147: }
                   8148:
                   8149: int rref(matrix mat,int *sugar)
                   8150: {
                   8151:   int row,col,i,j,k,l,s,wcol,wj;
                   8152:   unsigned long bj;
                   8153:   unsigned long **a;
                   8154:   unsigned long *ai,*ak,*as,*t;
                   8155:   int *pivot;
                   8156:
                   8157:   row = mat->row;
                   8158:   col = mat->col;
                   8159:   a = mat->a;
                   8160:   wcol = (col+BLEN-1)/BLEN;
                   8161:   pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   8162:   i = 0;
                   8163:   for ( j = 0; j < col; j++ ) {
                   8164:   wj = j/BLEN; bj = 1L<<(j%BLEN);
                   8165:     for ( k = i; k < row; k++ )
                   8166:     if ( a[k][wj] & bj ) break;
                   8167:     if ( k == row ) continue;
                   8168:   pivot[i] = j;
                   8169:     if ( k != i ) {
                   8170:    t = a[i]; a[i] = a[k]; a[k] = t;
                   8171:    s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
                   8172:   }
                   8173:   ai = a[i];
                   8174:     for ( k = i+1; k < row; k++ ) {
                   8175:     ak = a[k];
                   8176:     if ( ak[wj] & bj ) {
                   8177:       for ( l = wj; l < wcol; l++ )
                   8178:       ak[l] ^= ai[l];
                   8179:       sugar[k] = MAX(sugar[k],sugar[i]);
                   8180:     }
                   8181:   }
                   8182:   i++;
                   8183:   }
                   8184:   for ( k = i-1; k >= 0; k-- ) {
                   8185:     j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
                   8186:   ak = a[k];
                   8187:     for ( s = 0; s < k; s++ ) {
                   8188:     as = a[s];
                   8189:       if ( as[wj] & bj ) {
                   8190:         for ( l = wj; l < wcol; l++ )
                   8191:       as[l] ^= ak[l];
                   8192:       sugar[s] = MAX(sugar[s],sugar[k]);
                   8193:     }
                   8194:   }
                   8195:   }
                   8196:   return i;
                   8197: }
                   8198:
                   8199: void print_matrix(matrix mat)
                   8200: {
                   8201:   int row,col,i,j;
                   8202:   unsigned long *ai;
                   8203:
                   8204:   row = mat->row;
                   8205:   col = mat->col;
                   8206:   printf("%d x %d\n",row,col);
                   8207:   for ( i = 0; i < row; i++ ) {
                   8208:   ai = mat->a[i];
                   8209:     for ( j = 0; j < col; j++ ) {
                   8210:     if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
                   8211:     else putchar('0');
                   8212:   }
                   8213:   putchar('\n');
                   8214:   }
                   8215: }
                   8216:
                   8217: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
                   8218:
                   8219: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
                   8220: {
                   8221:   int row,col,wcol,wj,i,j;
                   8222:   unsigned long bj;
                   8223:   unsigned long *ai;
                   8224:   unsigned long **a;
                   8225:   int len;
                   8226:   int *pos;
                   8227:
                   8228:   row = mat->row;
                   8229:   col = mat->col;
                   8230:   wcol = (col+BLEN-1)/BLEN;
                   8231:   pos = (int *)MALLOC(wcol*sizeof(int));
                   8232:   bzero(pos,wcol*sizeof(int));
                   8233:   for ( i = j = 0; i < wcol; i++ )
                   8234:     if ( v[i] ) pos[j++] = i;;
                   8235:   len = j;
                   8236:   wj = rhead/BLEN;
                   8237:   bj = 1L<<rhead%BLEN;
                   8238:   a = mat->a;
                   8239:   for ( i = 0; i < row; i++ ) {
                   8240:   ai = a[i];
                   8241:     if ( ai[wj]&bj ) {
                   8242:     for ( j = 0; j < len; j++ )
                   8243:       ai[pos[j]] ^= v[pos[j]];
                   8244:     sugar[i] = MAX(sugar[i],rsugar);
                   8245:   }
                   8246:   }
                   8247: }
                   8248:
                   8249: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   8250: {
                   8251:     int nsp,nred,i,i0,k,rank,row;
                   8252:     NODE r0,rp;
                   8253:     ND_pairs sp;
                   8254:   ND spol;
                   8255:   NM_ind_pair rt;
                   8256:     int *s0hash;
                   8257:   UINT *s;
                   8258:   int *pivot,*sugar,*head;
                   8259:   matrix mat;
                   8260:     NM m;
                   8261:     NODE r;
                   8262:   struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
                   8263:   int rhead,rsugar,size;
                   8264:     unsigned long *v;
                   8265:
                   8266:     get_eg(&eg0);
                   8267:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   8268:     nred = length(rp0);
                   8269:     mat = alloc_matrix(nsp,col);
                   8270:     s0hash = (int *)MALLOC(col*sizeof(int));
                   8271:     for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
                   8272:         s0hash[i] = ndl_hash_value(s);
                   8273:
                   8274:   sugar = (int *)MALLOC(nsp*sizeof(int));
                   8275:   for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
                   8276:     nd_sp(2,0,sp,&spol);
                   8277:     if ( spol ) {
                   8278:         mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
                   8279:       sugar[i] = SG(spol);
                   8280:       i++;
                   8281:     }
                   8282:   }
                   8283:   mat->row = i;
                   8284:     if ( DP_Print ) {
                   8285:       fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
                   8286:     }
                   8287:   size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
                   8288:   v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
                   8289:     for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
                   8290:     rt = (NM_ind_pair)BDY(rp);
                   8291:     bzero(v,size);
                   8292:         rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
                   8293:     rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
                   8294:       red_by_vect_2(mat,sugar,v,rhead,rsugar);
                   8295:   }
                   8296:
                   8297:     get_eg(&eg1);
                   8298:     init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
                   8299:   rank = rref(mat,sugar);
                   8300:
                   8301:     for ( i = 0, r0 = 0; i < rank; i++ ) {
                   8302:       NEXTNODE(r0,r);
                   8303:     BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
                   8304:       SG((NDV)BDY(r)) = sugar[i];
                   8305:     }
                   8306:     if ( r0 ) NEXT(r) = 0;
                   8307:     get_eg(&eg2);
                   8308:     init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
                   8309:     if ( DP_Print ) {
                   8310:         fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5       noro     8311:       eg_elim1.exectime,eg_elim2.exectime);
1.1       noro     8312:         fflush(asir_out);
                   8313:   }
                   8314:     return r0;
                   8315: }
                   8316:
                   8317:
                   8318: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   8319: {
                   8320:     IndArray *imat;
1.11      noro     8321:     int nsp,nred,i,start;
1.1       noro     8322:     int *rhead;
                   8323:     NODE r0,rp;
                   8324:     ND_pairs sp;
                   8325:     NM_ind_pair *rvect;
                   8326:     UINT *s;
                   8327:     int *s0hash;
1.11      noro     8328:     struct oEGT eg0,eg1,eg_conv;
1.1       noro     8329:
                   8330:     if ( m == 2 && nd_rref2 )
                   8331:      return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
                   8332:
                   8333:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   8334:     nred = length(rp0);
                   8335:     imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   8336:     rhead = (int *)MALLOC(col*sizeof(int));
                   8337:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   8338:
                   8339:     /* construction of index arrays */
1.11      noro     8340:     get_eg(&eg0);
1.1       noro     8341:     if ( DP_Print ) {
1.11      noro     8342:       fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   8343:       fflush(asir_out);
1.1       noro     8344:     }
                   8345:     rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11      noro     8346:     for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1       noro     8347:         rvect[i] = (NM_ind_pair)BDY(rp);
1.11      noro     8348:         imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1       noro     8349:         rhead[imat[i]->head] = 1;
1.11      noro     8350:         start = imat[i]->head;
                   8351:     }
1.12      noro     8352:     get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11      noro     8353:     if ( DP_Print ) {
                   8354:       fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   8355:       fflush(asir_out);
1.1       noro     8356:     }
                   8357:     if ( m > 0 )
1.7       noro     8358: #if SIZEOF_LONG==8
1.1       noro     8359:         r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   8360: #else
                   8361:         r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   8362: #endif
                   8363:     else if ( m == -1 )
                   8364:         r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   8365:     else if ( m == -2 )
                   8366:         r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   8367:     else
                   8368:         r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   8369:     return r0;
                   8370: }
                   8371:
                   8372: /* for Fp, 2<=p<2^16 */
                   8373:
                   8374: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   8375:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   8376: {
                   8377:     int spcol,sprow,a;
                   8378:     int i,j,k,l,rank;
                   8379:     NODE r0,r;
                   8380:     ND_pairs sp;
                   8381:     ND spol;
                   8382:     UINT **spmat;
                   8383:     UINT *svect,*v;
                   8384:     int *colstat;
                   8385:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8386:     int maxrs;
                   8387:     int *spsugar;
                   8388:     ND_pairs *spactive;
                   8389:
                   8390:     spcol = col-nred;
                   8391:     get_eg(&eg0);
                   8392:     /* elimination (1st step) */
                   8393:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   8394:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   8395:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8396:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   8397:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8398:         nd_sp(m,0,sp,&spol);
                   8399:         if ( !spol ) continue;
                   8400:         nd_to_vect(m,s0vect,col,spol,svect);
                   8401:         if ( m == -1 )
                   8402:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   8403:         else
                   8404:             maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
                   8405:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   8406:         if ( i < col ) {
                   8407:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   8408:             for ( j = k = 0; j < col; j++ )
                   8409:                 if ( !rhead[j] ) v[k++] = svect[j];
                   8410:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8411:             if ( nz )
                   8412:             spactive[sprow] = sp;
                   8413:             sprow++;
                   8414:         }
                   8415:         nd_free(spol);
                   8416:     }
                   8417:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8418:     if ( DP_Print ) {
1.5       noro     8419:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8420:         fflush(asir_out);
                   8421:     }
                   8422:     /* free index arrays */
                   8423:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   8424:
                   8425:     /* elimination (2nd step) */
                   8426:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8427:     if ( m == -1 )
                   8428:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   8429:     else
                   8430:         rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   8431:     r0 = 0;
                   8432:     for ( i = 0; i < rank; i++ ) {
                   8433:         NEXTNODE(r0,r); BDY(r) =
                   8434:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   8435:         SG((NDV)BDY(r)) = spsugar[i];
                   8436:         GCFREE(spmat[i]);
                   8437:     }
                   8438:     if ( r0 ) NEXT(r) = 0;
                   8439:
                   8440:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   8441:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8442:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8443:     if ( DP_Print ) {
1.5       noro     8444:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8445:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8446:             nsp,nred,sprow,spcol,rank);
1.5       noro     8447:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     8448:     }
                   8449:     if ( nz ) {
                   8450:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   8451:         if ( rank > 0 ) {
                   8452:             NEXT(spactive[rank-1]) = 0;
                   8453:             *nz = spactive[0];
                   8454:         } else
                   8455:             *nz = 0;
                   8456:     }
                   8457:     return r0;
                   8458: }
                   8459:
                   8460:
                   8461: /* for small finite fields */
                   8462:
                   8463: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   8464:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   8465: {
                   8466:     int spcol,sprow,a;
                   8467:     int i,j,k,l,rank;
                   8468:     NODE r0,r;
                   8469:     ND_pairs sp;
                   8470:     ND spol;
                   8471:     UINT **spmat;
                   8472:     UINT *svect,*v;
                   8473:     int *colstat;
                   8474:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8475:     int maxrs;
                   8476:     int *spsugar;
                   8477:     ND_pairs *spactive;
                   8478:
                   8479:     spcol = col-nred;
                   8480:     get_eg(&eg0);
                   8481:     /* elimination (1st step) */
                   8482:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   8483:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   8484:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8485:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   8486:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8487:         nd_sp(m,0,sp,&spol);
                   8488:         if ( !spol ) continue;
                   8489:         nd_to_vect(m,s0vect,col,spol,svect);
                   8490:         maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   8491:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   8492:         if ( i < col ) {
                   8493:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   8494:             for ( j = k = 0; j < col; j++ )
                   8495:                 if ( !rhead[j] ) v[k++] = svect[j];
                   8496:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8497:             if ( nz )
                   8498:             spactive[sprow] = sp;
                   8499:             sprow++;
                   8500:         }
                   8501:         nd_free(spol);
                   8502:     }
                   8503:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8504:     if ( DP_Print ) {
1.5       noro     8505:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8506:         fflush(asir_out);
                   8507:     }
                   8508:     /* free index arrays */
                   8509:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   8510:
                   8511:     /* elimination (2nd step) */
                   8512:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8513:     rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   8514:     r0 = 0;
                   8515:     for ( i = 0; i < rank; i++ ) {
                   8516:         NEXTNODE(r0,r); BDY(r) =
                   8517:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   8518:         SG((NDV)BDY(r)) = spsugar[i];
                   8519:         GCFREE(spmat[i]);
                   8520:     }
                   8521:     if ( r0 ) NEXT(r) = 0;
                   8522:
                   8523:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   8524:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8525:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8526:     if ( DP_Print ) {
1.5       noro     8527:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8528:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8529:             nsp,nred,sprow,spcol,rank);
1.5       noro     8530:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     8531:     }
                   8532:     if ( nz ) {
                   8533:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   8534:         if ( rank > 0 ) {
                   8535:             NEXT(spactive[rank-1]) = 0;
                   8536:             *nz = spactive[0];
                   8537:         } else
                   8538:             *nz = 0;
                   8539:     }
                   8540:     return r0;
                   8541: }
                   8542:
                   8543: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   8544:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   8545: {
                   8546:     int spcol,sprow,a;
                   8547:     int i,j,k,l,rank;
                   8548:     NODE r0,r;
                   8549:     ND_pairs sp;
                   8550:     ND spol;
                   8551:     mpz_t **spmat;
                   8552:     mpz_t *svect,*v;
                   8553:     int *colstat;
                   8554:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8555:     int maxrs;
                   8556:     int *spsugar;
                   8557:     pointer *w;
                   8558:
                   8559:     spcol = col-nred;
                   8560:     get_eg(&eg0);
                   8561:     /* elimination (1st step) */
                   8562:     spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
                   8563:     svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   8564:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8565:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8566:         nd_sp(m,trace,sp,&spol);
                   8567:         if ( !spol ) continue;
                   8568:         nd_to_vect_lf(s0vect,col,spol,svect);
                   8569:         maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
                   8570:         for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
                   8571:         if ( i < col ) {
                   8572:             spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
                   8573:             for ( j = k = 0; j < col; j++ )
                   8574:                 if ( !rhead[j] ) v[k++][0] = svect[j][0];
                   8575:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8576:             sprow++;
                   8577:         }
                   8578: /*        nd_free(spol); */
                   8579:     }
                   8580:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8581:     if ( DP_Print ) {
1.5       noro     8582:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8583:         fflush(asir_out);
                   8584:     }
                   8585:     /* free index arrays */
                   8586: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   8587:
                   8588:     /* elimination (2nd step) */
                   8589:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8590:     rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
                   8591:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   8592:     for ( i = 0; i < rank; i++ ) {
                   8593: #if 0
                   8594:         w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   8595:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   8596: #else
                   8597:         w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   8598:         SG((NDV)w[i]) = spsugar[i];
                   8599: #endif
                   8600: /*        GCFREE(spmat[i]); */
                   8601:
                   8602:     }
                   8603: #if 0
                   8604:     qsort(w,rank,sizeof(NDV),
                   8605:         (int (*)(const void *,const void *))ndv_compare);
                   8606: #endif
                   8607:     r0 = 0;
                   8608:     for ( i = 0; i < rank; i++ ) {
                   8609:         NEXTNODE(r0,r); BDY(r) = w[i];
                   8610:     }
                   8611:     if ( r0 ) NEXT(r) = 0;
                   8612:
                   8613: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   8614:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8615:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8616:     if ( DP_Print ) {
1.5       noro     8617:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8618:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8619:             nsp,nred,sprow,spcol,rank);
1.5       noro     8620:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     8621:     }
                   8622:     return r0;
                   8623: }
                   8624:
                   8625: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   8626:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   8627: {
                   8628:     int spcol,sprow,a;
                   8629:     int i,j,k,l,rank;
                   8630:     NODE r0,r;
                   8631:     ND_pairs sp;
                   8632:     ND spol;
                   8633:     Z **spmat;
                   8634:     Z *svect,*v;
                   8635:     int *colstat;
                   8636:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8637:     int maxrs;
                   8638:     int *spsugar;
                   8639:     pointer *w;
                   8640:
                   8641:     spcol = col-nred;
                   8642:     get_eg(&eg0);
                   8643:     /* elimination (1st step) */
                   8644:     spmat = (Z **)MALLOC(nsp*sizeof(Q *));
                   8645:     svect = (Z *)MALLOC(col*sizeof(Q));
                   8646:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8647:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8648:         nd_sp(0,trace,sp,&spol);
                   8649:         if ( !spol ) continue;
                   8650:         nd_to_vect_q(s0vect,col,spol,svect);
                   8651:         maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
                   8652:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   8653:         if ( i < col ) {
                   8654:             spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
                   8655:             for ( j = k = 0; j < col; j++ )
                   8656:                 if ( !rhead[j] ) v[k++] = svect[j];
                   8657:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8658:             sprow++;
                   8659:         }
                   8660: /*        nd_free(spol); */
                   8661:     }
                   8662:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8663:     if ( DP_Print ) {
1.5       noro     8664:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8665:         fflush(asir_out);
                   8666:     }
                   8667:     /* free index arrays */
                   8668: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   8669:
                   8670:     /* elimination (2nd step) */
                   8671:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8672:     rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
                   8673:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   8674:     for ( i = 0; i < rank; i++ ) {
                   8675: #if 0
                   8676:         w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   8677:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   8678: #else
                   8679:         w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   8680:         SG((NDV)w[i]) = spsugar[i];
                   8681: #endif
                   8682: /*        GCFREE(spmat[i]); */
                   8683:     }
                   8684: #if 0
                   8685:     qsort(w,rank,sizeof(NDV),
                   8686:         (int (*)(const void *,const void *))ndv_compare);
                   8687: #endif
                   8688:     r0 = 0;
                   8689:     for ( i = 0; i < rank; i++ ) {
                   8690:         NEXTNODE(r0,r); BDY(r) = w[i];
                   8691:     }
                   8692:     if ( r0 ) NEXT(r) = 0;
                   8693:
                   8694: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   8695:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8696:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8697:     if ( DP_Print ) {
1.5       noro     8698:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8699:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8700:             nsp,nred,sprow,spcol,rank);
1.5       noro     8701:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     8702:     }
                   8703:     return r0;
                   8704: }
                   8705:
                   8706: FILE *nd_write,*nd_read;
                   8707:
                   8708: void nd_send_int(int a) {
                   8709:     write_int(nd_write,(unsigned int *)&a);
                   8710: }
                   8711:
                   8712: void nd_send_intarray(int *p,int len) {
                   8713:     write_intarray(nd_write,(unsigned int *)p,len);
                   8714: }
                   8715:
                   8716: int nd_recv_int() {
                   8717:     int a;
                   8718:
                   8719:     read_int(nd_read,(unsigned int *)&a);
                   8720:     return a;
                   8721: }
                   8722:
                   8723: void nd_recv_intarray(int *p,int len) {
                   8724:     read_intarray(nd_read,(unsigned int *)p,len);
                   8725: }
                   8726:
                   8727: void nd_send_ndv(NDV p) {
                   8728:     int len,i;
                   8729:     NMV m;
                   8730:
                   8731:     if ( !p ) nd_send_int(0);
                   8732:     else {
                   8733:         len = LEN(p);
                   8734:         nd_send_int(len);
                   8735:         m = BDY(p);
                   8736:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   8737:             nd_send_int(CM(m));
                   8738:             nd_send_intarray((int *)DL(m),nd_wpd);
                   8739:         }
                   8740:     }
                   8741: }
                   8742:
                   8743: void nd_send_nd(ND p) {
                   8744:     int len,i;
                   8745:     NM m;
                   8746:
                   8747:     if ( !p ) nd_send_int(0);
                   8748:     else {
                   8749:         len = LEN(p);
                   8750:         nd_send_int(len);
                   8751:         m = BDY(p);
                   8752:         for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   8753:             nd_send_int(CM(m));
                   8754:             nd_send_intarray((int *)DL(m),nd_wpd);
                   8755:         }
                   8756:     }
                   8757: }
                   8758:
                   8759: NDV nd_recv_ndv()
                   8760: {
                   8761:     int len,i;
                   8762:     NMV m,m0;
                   8763:     NDV r;
                   8764:
                   8765:     len = nd_recv_int();
                   8766:     if ( !len ) return 0;
                   8767:     else {
                   8768:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8769: #if 0
                   8770:         ndv_alloc += len*nmv_adv;
                   8771: #endif
                   8772:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   8773:             CM(m) = nd_recv_int();
                   8774:             nd_recv_intarray((int *)DL(m),nd_wpd);
                   8775:         }
                   8776:         MKNDV(nd_nvar,m0,len,r);
                   8777:         return r;
                   8778:     }
                   8779: }
                   8780:
                   8781: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
                   8782: {
                   8783:     int i,j,t,c,rank,inv;
                   8784:     int *ci,*ri;
                   8785:     Z dn;
                   8786:     MAT m,nm;
                   8787:
                   8788:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
                   8789:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   8790:     for ( i = 0; i < row; i++ )
                   8791:         for ( j = 0; j < col; j++ )
                   8792:             mat0[i][j] = 0;
                   8793:     c = col-rank;
                   8794:     for ( i = 0; i < rank; i++ ) {
                   8795:         mat0[i][ri[i]] = dn;
                   8796:         for ( j = 0; j < c; j++ )
                   8797:             mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
                   8798:     }
                   8799:     return rank;
                   8800: }
                   8801:
                   8802: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   8803: {
                   8804:     int i,j,k,l,inv,a,rank,s;
                   8805:     unsigned int *t,*pivot,*pk;
                   8806:     unsigned int **mat;
                   8807:     ND_pairs pair;
                   8808:
                   8809:     mat = (unsigned int **)mat0;
                   8810:     for ( rank = 0, j = 0; j < col; j++ ) {
                   8811:         for ( i = rank; i < row; i++ )
                   8812:             mat[i][j] %= md;
                   8813:         for ( i = rank; i < row; i++ )
                   8814:             if ( mat[i][j] )
                   8815:                 break;
                   8816:         if ( i == row ) {
                   8817:             colstat[j] = 0;
                   8818:             continue;
                   8819:         } else
                   8820:             colstat[j] = 1;
                   8821:         if ( i != rank ) {
                   8822:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   8823:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   8824:             if ( spactive ) {
                   8825:                 pair = spactive[i]; spactive[i] = spactive[rank];
                   8826:                 spactive[rank] = pair;
                   8827:             }
                   8828:         }
                   8829:         pivot = mat[rank];
                   8830:         s = sugar[rank];
                   8831:         inv = invm(pivot[j],md);
                   8832:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   8833:             if ( *pk ) {
                   8834:                 if ( *pk >= (unsigned int)md )
                   8835:                     *pk %= md;
                   8836:                 DMAR(*pk,inv,0,md,*pk)
                   8837:             }
                   8838:         for ( i = rank+1; i < row; i++ ) {
                   8839:             t = mat[i];
                   8840:             if ( (a = t[j]) != 0 ) {
                   8841:                 sugar[i] = MAX(sugar[i],s);
                   8842:                 red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   8843:             }
                   8844:         }
                   8845:         rank++;
                   8846:     }
                   8847:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   8848:         if ( colstat[j] ) {
                   8849:             pivot = mat[l];
                   8850:             s = sugar[l];
                   8851:             for ( i = 0; i < l; i++ ) {
                   8852:                 t = mat[i];
                   8853:                 t[j] %= md;
                   8854:                 if ( (a = t[j]) != 0 ) {
                   8855:                     sugar[i] = MAX(sugar[i],s);
                   8856:                     red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   8857:                 }
                   8858:             }
                   8859:             l--;
                   8860:         }
                   8861:     for ( j = 0, l = 0; l < rank; j++ )
                   8862:         if ( colstat[j] ) {
                   8863:             t = mat[l];
                   8864:             for ( k = j; k < col; k++ )
                   8865:                 if ( t[k] >= (unsigned int)md )
                   8866:                     t[k] %= md;
                   8867:             l++;
                   8868:         }
                   8869:     return rank;
                   8870: }
                   8871:
                   8872:
1.7       noro     8873: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1       noro     8874: {
1.7       noro     8875:     int i,j,k,l,inv,a,rank,s;
                   8876:     unsigned int *t,*pivot,*pk;
                   8877:     unsigned int **mat;
                   8878:
                   8879:     mat = (unsigned int **)mat0;
                   8880:     for ( rank = 0, j = 0; j < col; j++ ) {
                   8881:         for ( i = rank; i < row; i++ )
                   8882:             if ( mat[i][j] )
                   8883:                 break;
                   8884:         if ( i == row ) {
                   8885:             colstat[j] = 0;
                   8886:             continue;
                   8887:         } else
                   8888:             colstat[j] = 1;
                   8889:         if ( i != rank ) {
                   8890:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   8891:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   8892:         }
                   8893:         pivot = mat[rank];
                   8894:         s = sugar[rank];
                   8895:         inv = _invsf(pivot[j]);
                   8896:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   8897:             if ( *pk )
                   8898:                 *pk = _mulsf(*pk,inv);
                   8899:         for ( i = rank+1; i < row; i++ ) {
                   8900:             t = mat[i];
                   8901:             if ( (a = t[j]) != 0 ) {
                   8902:                 sugar[i] = MAX(sugar[i],s);
                   8903:                 red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   8904:             }
                   8905:         }
                   8906:         rank++;
                   8907:     }
                   8908:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   8909:         if ( colstat[j] ) {
                   8910:             pivot = mat[l];
                   8911:             s = sugar[l];
                   8912:             for ( i = 0; i < l; i++ ) {
                   8913:                 t = mat[i];
                   8914:                 if ( (a = t[j]) != 0 ) {
                   8915:                     sugar[i] = MAX(sugar[i],s);
                   8916:                     red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   8917:                 }
                   8918:             }
                   8919:             l--;
                   8920:         }
                   8921:     return rank;
                   8922: }
1.1       noro     8923:
1.7       noro     8924: int ndv_ishomo(NDV p)
                   8925: {
                   8926:     NMV m;
                   8927:     int len,h;
1.1       noro     8928:
                   8929:     if ( !p ) return 1;
                   8930:     len = LEN(p);
                   8931:     m = BDY(p);
                   8932:     h = TD(DL(m));
                   8933:     NMV_ADV(m);
                   8934:     for ( len--; len; len--, NMV_ADV(m) )
1.20      noro     8935:         if ( TD(DL(m)) != h ) {
                   8936:           return 0;
                   8937:         }
1.1       noro     8938:     return 1;
                   8939: }
                   8940:
                   8941: void ndv_save(NDV p,int index)
                   8942: {
                   8943:     FILE *s;
                   8944:     char name[BUFSIZ];
                   8945:     short id;
                   8946:     int nv,sugar,len,n,i,td,e,j;
                   8947:     NMV m;
                   8948:     unsigned int *dl;
                   8949:     int mpos;
                   8950:
                   8951:     sprintf(name,"%s/%d",Demand,index);
                   8952:     s = fopen(name,"w");
                   8953:     savevl(s,0);
                   8954:     if ( !p ) {
                   8955:         saveobj(s,0);
                   8956:         return;
                   8957:     }
                   8958:     id = O_DP;
                   8959:     nv = NV(p);
                   8960:     sugar = SG(p);
                   8961:     len = LEN(p);
                   8962:     write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
                   8963:     write_int(s,(unsigned int *)&len);
                   8964:
                   8965:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     8966:         saveobj(s,(Obj)CZ(m));
1.1       noro     8967:         dl = DL(m);
                   8968:         td = TD(dl);
                   8969:         write_int(s,(unsigned int *)&td);
                   8970:         for ( j = 0; j < nv; j++ ) {
                   8971:             e = GET_EXP(dl,j);
                   8972:             write_int(s,(unsigned int *)&e);
                   8973:         }
                   8974:         if ( nd_module ) {
                   8975:             mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
                   8976:         }
                   8977:     }
                   8978:     fclose(s);
                   8979: }
                   8980:
                   8981: void nd_save_mod(ND p,int index)
                   8982: {
                   8983:     FILE *s;
                   8984:     char name[BUFSIZ];
                   8985:     int nv,sugar,len,c;
                   8986:     NM m;
                   8987:
                   8988:     sprintf(name,"%s/%d",Demand,index);
                   8989:     s = fopen(name,"w");
                   8990:     if ( !p ) {
                   8991:     len = 0;
                   8992:       write_int(s,(unsigned int *)&len);
                   8993:     fclose(s);
                   8994:         return;
                   8995:     }
                   8996:     nv = NV(p);
                   8997:     sugar = SG(p);
                   8998:     len = LEN(p);
                   8999:     write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
                   9000:   for ( m = BDY(p); m; m = NEXT(m) ) {
                   9001:     c = CM(m); write_int(s,(unsigned int *)&c);
                   9002:     write_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   9003:   }
                   9004:     fclose(s);
                   9005: }
                   9006:
                   9007: NDV ndv_load(int index)
                   9008: {
                   9009:     FILE *s;
                   9010:     char name[BUFSIZ];
                   9011:     short id;
                   9012:     int nv,sugar,len,n,i,td,e,j;
                   9013:     NDV d;
                   9014:     NMV m0,m;
                   9015:     unsigned int *dl;
                   9016:     Obj obj;
                   9017:     int mpos;
                   9018:
                   9019:     sprintf(name,"%s/%d",Demand,index);
                   9020:     s = fopen(name,"r");
                   9021:     if ( !s ) return 0;
                   9022:
                   9023:     skipvl(s);
                   9024:     read_short(s,(unsigned short *)&id);
                   9025:     if ( !id ) return 0;
                   9026:     read_int(s,(unsigned int *)&nv);
                   9027:     read_int(s,(unsigned int *)&sugar);
                   9028:     read_int(s,(unsigned int *)&len);
                   9029:
                   9030:     m0 = m = MALLOC(len*nmv_adv);
                   9031:     for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     9032:         loadobj(s,&obj); CZ(m) = (Z)obj;
1.1       noro     9033:         dl = DL(m);
                   9034:         ndl_zero(dl);
                   9035:         read_int(s,(unsigned int *)&td); TD(dl) = td;
                   9036:         for ( j = 0; j < nv; j++ ) {
                   9037:             read_int(s,(unsigned int *)&e);
                   9038:             PUT_EXP(dl,j,e);
                   9039:         }
                   9040:         if ( nd_module ) {
                   9041:             read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
                   9042:         }
                   9043:         if ( nd_blockmask ) ndl_weight_mask(dl);
                   9044:     }
                   9045:     fclose(s);
                   9046:     MKNDV(nv,m0,len,d);
                   9047:     SG(d) = sugar;
                   9048:     return d;
                   9049: }
                   9050:
                   9051: ND nd_load_mod(int index)
                   9052: {
                   9053:     FILE *s;
                   9054:     char name[BUFSIZ];
                   9055:     int nv,sugar,len,i,c;
                   9056:   ND d;
                   9057:     NM m0,m;
                   9058:
                   9059:     sprintf(name,"%s/%d",Demand,index);
                   9060:     s = fopen(name,"r");
                   9061:   /* if the file does not exist, it means p[index]=0 */
                   9062:     if ( !s ) return 0;
                   9063:
                   9064:     read_int(s,(unsigned int *)&nv);
                   9065:   if ( !nv ) { fclose(s); return 0; }
                   9066:
                   9067:     read_int(s,(unsigned int *)&sugar);
                   9068:     read_int(s,(unsigned int *)&len);
                   9069:   for ( m0 = 0, i = 0; i < len; i++ ) {
                   9070:     NEXTNM(m0,m);
                   9071:     read_int(s,(unsigned int *)&c); CM(m) = c;
                   9072:     read_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   9073:   }
                   9074:   NEXT(m) = 0;
                   9075:     MKND(nv,m0,len,d);
                   9076:     SG(d) = sugar;
                   9077:   fclose(s);
                   9078:     return d;
                   9079: }
                   9080:
                   9081: void nd_det(int mod,MAT f,P *rp)
                   9082: {
                   9083:     VL fv,tv;
                   9084:     int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
                   9085:     pointer **m;
                   9086:     P **w;
                   9087:     P mp,r;
                   9088:     NDV **dm;
                   9089:     NDV *t,*mi,*mj;
                   9090:     NDV d,s,mij,mjj;
                   9091:     ND u;
                   9092:     NMV nmv;
                   9093:     UINT *bound;
                   9094:     PGeoBucket bucket;
                   9095:     struct order_spec *ord;
                   9096:     Z dq,dt,ds;
                   9097:     Z mone;
                   9098:     Z gn,qn,dn0,nm,dn;
                   9099:
                   9100:     create_order_spec(0,0,&ord);
                   9101:     nd_init_ord(ord);
                   9102:     get_vars((Obj)f,&fv);
                   9103:     if ( f->row != f->col )
                   9104:         error("nd_det : non-square matrix");
                   9105:     n = f->row;
                   9106:     m = f->body;
                   9107:     for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   9108:
                   9109:     if ( !nvar ) {
                   9110:         if ( !mod )
                   9111:             detp(CO,(P **)m,n,rp);
                   9112:         else {
                   9113:             w = (P **)almat_pointer(n,n);
                   9114:             for ( i = 0; i < n; i++ )
                   9115:                 for ( j = 0; j < n; j++ )
                   9116:                     ptomp(mod,(P)m[i][j],&w[i][j]);
                   9117:             detmp(CO,mod,w,n,&mp);
                   9118:             mptop(mp,rp);
                   9119:         }
                   9120:         return;
                   9121:     }
                   9122:
                   9123:     if ( !mod ) {
                   9124:         w = (P **)almat_pointer(n,n);
                   9125:         dq = ONE;
                   9126:         for ( i = 0; i < n; i++ ) {
                   9127:             dn0 = ONE;
                   9128:             for ( j = 0; j < n; j++ ) {
                   9129:                 if ( !m[i][j] ) continue;
                   9130:                 lgp(m[i][j],&nm,&dn);
1.6       noro     9131:                 gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1       noro     9132:             }
                   9133:             if ( !UNIZ(dn0) ) {
                   9134:                 ds = dn0;
                   9135:                 for ( j = 0; j < n; j++ )
                   9136:                     mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   9137:                 mulz(dq,ds,&dt); dq = dt;
                   9138:             } else
                   9139:                 for ( j = 0; j < n; j++ )
                   9140:                     w[i][j] = (P)m[i][j];
                   9141:         }
                   9142:         m = (pointer **)w;
                   9143:     }
                   9144:
                   9145:     for ( i = 0, max = 1; i < n; i++ )
                   9146:         for ( j = 0; j < n; j++ )
                   9147:             for ( tv = fv; tv; tv = NEXT(tv) ) {
                   9148:                 e = getdeg(tv->v,(P)m[i][j]);
                   9149:                 max = MAX(e,max);
                   9150:             }
                   9151:     nd_setup_parameters(nvar,max);
                   9152:     dm = (NDV **)almat_pointer(n,n);
                   9153:     for ( i = 0, max = 1; i < n; i++ )
                   9154:         for ( j = 0; j < n; j++ ) {
                   9155:             dm[i][j] = ptondv(CO,fv,m[i][j]);
                   9156:             if ( mod ) ndv_mod(mod,dm[i][j]);
                   9157:             if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   9158:         }
                   9159:     d = ptondv(CO,fv,(P)ONE);
                   9160:     if ( mod ) ndv_mod(mod,d);
                   9161:     chsgnz(ONE,&mone);
                   9162:     for ( j = 0, sgn = 1; j < n; j++ ) {
                   9163:       if ( DP_Print ) {
                   9164:         fprintf(asir_out,".");
                   9165:       }
                   9166:         for ( i = j; i < n && !dm[i][j]; i++ );
                   9167:         if ( i == n ) {
                   9168:             *rp = 0;
                   9169:             return;
                   9170:         }
                   9171:         k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
                   9172:         for ( k = j; k < n; k++ )
                   9173:             for ( l = j; l < n; l++ )
                   9174:                 if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   9175:                     k0 = k; l0 = l; len0 = LEN(dm[k][l]);
                   9176:                 }
                   9177:         if ( k0 != j ) {
                   9178:             t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   9179:             sgn = -sgn;
                   9180:         }
                   9181:         if ( l0 != j ) {
                   9182:             for ( k = j; k < n; k++ ) {
                   9183:                 s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   9184:             }
                   9185:             sgn = -sgn;
                   9186:         }
                   9187:         bound = nd_det_compute_bound(dm,n,j);
                   9188:         for ( k = 0; k < nd_nvar; k++ )
                   9189:             if ( bound[k]*2 > nd_mask0 ) break;
                   9190:         if ( k < nd_nvar )
                   9191:             nd_det_reconstruct(dm,n,j,d);
                   9192:
                   9193:         for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
                   9194: /*            if ( DP_Print ) fprintf(asir_out,"    i=%d\n        ",i); */
                   9195:             mi = dm[i]; mij = mi[j];
                   9196:             if ( mod )
                   9197:                 ndv_mul_c(mod,mij,mod-1);
                   9198:             else
                   9199:                 ndv_mul_c_q(mij,mone);
                   9200:             for ( k = j+1; k < n; k++ ) {
                   9201: /*                if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
                   9202:                 bucket = create_pbucket();
                   9203:                 if ( mi[k] ) {
                   9204:                     nmv = BDY(mjj); len = LEN(mjj);
                   9205:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   9206:                         u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   9207:                         add_pbucket(mod,bucket,u);
                   9208:                     }
                   9209:                 }
                   9210:                 if ( mj[k] && mij ) {
                   9211:                     nmv = BDY(mij); len = LEN(mij);
                   9212:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   9213:                         u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   9214:                         add_pbucket(mod,bucket,u);
                   9215:                     }
                   9216:                 }
                   9217:                 u = nd_quo(mod,bucket,d);
                   9218:                 mi[k] = ndtondv(mod,u);
                   9219:             }
                   9220: /*            if ( DP_Print ) fprintf(asir_out,"\n",k); */
                   9221:         }
                   9222:         d = mjj;
                   9223:     }
                   9224:     if ( DP_Print ) {
                   9225:       fprintf(asir_out,"\n");
                   9226:     }
                   9227:     if ( sgn < 0 ) {
                   9228:         if ( mod )
                   9229:             ndv_mul_c(mod,d,mod-1);
                   9230:         else
                   9231:             ndv_mul_c_q(d,mone);
                   9232:     }
                   9233:     r = ndvtop(mod,CO,fv,d);
                   9234:     if ( !mod && !UNIQ(dq) )
                   9235:         divsp(CO,r,(P)dq,rp);
                   9236:     else
                   9237:         *rp = r;
                   9238: }
                   9239:
                   9240: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
                   9241: {
                   9242:     NM mr,mr0;
                   9243:     NM tnm;
                   9244:     NMV m;
                   9245:     UINT *d0,*dt,*dm;
                   9246:     int c,n,td,i,c1,c2,len;
                   9247:     Z q;
                   9248:     ND r;
                   9249:
                   9250:     if ( !p ) return 0;
                   9251:     else {
                   9252:         n = NV(p); m = BDY(p); len = LEN(p);
                   9253:         d0 = DL(m0);
                   9254:         td = TD(d);
                   9255:         mr0 = 0;
                   9256:         NEWNM(tnm);
                   9257:         if ( mod ) {
                   9258:             c = CM(m0);
                   9259:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9260:                 ndl_add(DL(m),d0,DL(tnm));
                   9261:                 if ( ndl_reducible(DL(tnm),d) ) {
                   9262:                     NEXTNM(mr0,mr);
                   9263:                     c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
                   9264:                     ndl_copy(DL(tnm),DL(mr));
                   9265:                 }
                   9266:             }
                   9267:         } else {
1.6       noro     9268:             q = CZ(m0);
1.1       noro     9269:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9270:                 ndl_add(DL(m),d0,DL(tnm));
                   9271:                 if ( ndl_reducible(DL(tnm),d) ) {
                   9272:                     NEXTNM(mr0,mr);
1.6       noro     9273:                     mulz(CZ(m),q,&CZ(mr));
1.1       noro     9274:                     ndl_copy(DL(tnm),DL(mr));
                   9275:                 }
                   9276:             }
                   9277:         }
                   9278:         if ( !mr0 )
                   9279:             return 0;
                   9280:         else {
                   9281:             NEXT(mr) = 0;
                   9282:             for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   9283:             MKND(NV(p),mr0,len,r);
                   9284:             SG(r) = SG(p) + TD(d0);
                   9285:             return r;
                   9286:         }
                   9287:     }
                   9288: }
                   9289:
                   9290: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   9291: {
                   9292:     int i,obpe,oadv,h,k,l;
                   9293:     static NM prev_nm_free_list;
                   9294:     EPOS oepos;
                   9295:
                   9296:     obpe = nd_bpe;
                   9297:     oadv = nmv_adv;
                   9298:     oepos = nd_epos;
                   9299:     if ( obpe < 2 ) nd_bpe = 2;
                   9300:     else if ( obpe < 3 ) nd_bpe = 3;
                   9301:     else if ( obpe < 4 ) nd_bpe = 4;
                   9302:     else if ( obpe < 5 ) nd_bpe = 5;
                   9303:     else if ( obpe < 6 ) nd_bpe = 6;
                   9304:     else if ( obpe < 8 ) nd_bpe = 8;
                   9305:     else if ( obpe < 10 ) nd_bpe = 10;
                   9306:     else if ( obpe < 16 ) nd_bpe = 16;
                   9307:     else if ( obpe < 32 ) nd_bpe = 32;
                   9308:     else error("nd_det_reconstruct : exponent too large");
                   9309:
                   9310:     nd_setup_parameters(nd_nvar,0);
                   9311:     prev_nm_free_list = _nm_free_list;
                   9312:     _nm_free_list = 0;
                   9313:     for ( k = j; k < n; k++ )
                   9314:         for (l = j; l < n; l++ )
                   9315:             ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   9316:     ndv_realloc(d,obpe,oadv,oepos);
                   9317:     prev_nm_free_list = 0;
                   9318: #if 0
                   9319:     GC_gcollect();
                   9320: #endif
                   9321: }
                   9322:
                   9323: /* returns a UINT array containing degree bounds */
                   9324:
                   9325: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   9326: {
                   9327:     UINT *d0,*d1,*d,*t,*r;
                   9328:     int k,l,i;
                   9329:
                   9330:     d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
                   9331:     for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
                   9332:     for ( k = j; k < n; k++ )
                   9333:         for ( l = j; l < n; l++ )
                   9334:             if ( dm[k][l] ) {
                   9335:                 d = ndv_compute_bound(dm[k][l]);
                   9336:                 for ( i = 0; i < nd_nvar; i++ )
                   9337:                     d0[i] = MAX(d0[i],d[i]);
                   9338:             }
                   9339:     return d0;
                   9340: }
                   9341:
                   9342: DL nd_separate_d(UINT *d,UINT *trans)
                   9343: {
                   9344:     int n,td,i,e,j;
                   9345:     DL a;
                   9346:
                   9347:     ndl_zero(trans);
                   9348:     td = 0;
                   9349:     for ( i = 0; i < nd_ntrans; i++ ) {
                   9350:         e = GET_EXP(d,i);
                   9351:         PUT_EXP(trans,i,e);
                   9352:         td += MUL_WEIGHT(e,i);
                   9353:     }
                   9354:     if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   9355:         /* homogenized */
                   9356:         i = nd_nvar-1;
                   9357:         e = GET_EXP(d,i);
                   9358:         PUT_EXP(trans,i,e);
                   9359:         td += MUL_WEIGHT(e,i);
                   9360:     }
                   9361:     TD(trans) = td;
                   9362:     if ( nd_blockmask) ndl_weight_mask(trans);
                   9363:     NEWDL(a,nd_nalg);
                   9364:     td = 0;
                   9365:     for ( i = 0; i < nd_nalg; i++ ) {
                   9366:         j = nd_ntrans+i;
                   9367:         e = GET_EXP(d,j);
                   9368:         a->d[i] = e;
                   9369:         td += e;
                   9370:     }
                   9371:     a->td = td;
                   9372:     return a;
                   9373: }
                   9374:
                   9375: int nd_monic(int mod,ND *p)
                   9376: {
                   9377:     UINT *trans,*t;
                   9378:     DL alg;
                   9379:     MP mp0,mp;
                   9380:     NM m,m0,m1,ma0,ma,mb,mr0,mr;
                   9381:     ND r;
                   9382:     DL dl;
                   9383:     DP nm;
                   9384:     NDV ndv;
                   9385:     DAlg inv,cd;
                   9386:     ND s,c;
                   9387:     Z l,mul;
                   9388:     Z ln;
                   9389:     int n,ntrans,i,e,td,is_lc,len;
                   9390:     NumberField nf;
                   9391:     struct oEGT eg0,eg1;
                   9392:
                   9393:     if ( !(nf = get_numberfield()) )
                   9394:         error("nd_monic : current_numberfield is not set");
                   9395:
                   9396:     /* Q coef -> DAlg coef */
                   9397:     NEWNM(ma0); ma = ma0;
                   9398:     m = BDY(*p);
                   9399:     is_lc = 1;
                   9400:     while ( 1 ) {
                   9401:         NEWMP(mp0); mp = mp0;
1.6       noro     9402:         mp->c = (Obj)CZ(m);
1.1       noro     9403:         mp->dl = nd_separate_d(DL(m),DL(ma));
                   9404:         NEWNM(mb);
                   9405:         for ( m = NEXT(m); m; m = NEXT(m) ) {
                   9406:             alg = nd_separate_d(DL(m),DL(mb));
                   9407:             if ( !ndl_equal(DL(ma),DL(mb)) )
                   9408:                 break;
1.6       noro     9409:             NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1       noro     9410:         }
                   9411:         NEXT(mp) = 0;
                   9412:         MKDP(nd_nalg,mp0,nm);
                   9413:         MKDAlg(nm,ONE,cd);
                   9414:         if ( is_lc == 1 ) {
                   9415:             /* if the lc is a rational number, we have nothing to do */
                   9416:             if ( !mp0->dl->td )
                   9417:                 return 1;
                   9418:
                   9419:             get_eg(&eg0);
                   9420:             invdalg(cd,&inv);
                   9421:             get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   9422:             /* check the validity of inv */
                   9423:             if ( mod && !remqi((Q)inv->dn,mod) )
                   9424:                 return 0;
                   9425:             CA(ma) = nf->one;
                   9426:             is_lc = 0;
                   9427:             ln = ONE;
                   9428:         } else {
                   9429:             muldalg(cd,inv,&CA(ma));
                   9430:             lcmz(ln,CA(ma)->dn,&ln);
                   9431:         }
                   9432:         if ( m ) {
                   9433:             NEXT(ma) = mb; ma = mb;
                   9434:         } else {
                   9435:             NEXT(ma) = 0;
                   9436:             break;
                   9437:         }
                   9438:     }
                   9439:     /* l = lcm(denoms) */
                   9440:     l = ln;
                   9441:     for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6       noro     9442:         divsz(l,CA(m)->dn,&mul);
1.1       noro     9443:         for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   9444:             NEXTNM(mr0,mr);
1.6       noro     9445:             mulz((Z)mp->c,mul,&CZ(mr));
1.1       noro     9446:             dl = mp->dl;
                   9447:             td = TD(DL(m));
                   9448:             ndl_copy(DL(m),DL(mr));
                   9449:             for ( i = 0; i < nd_nalg; i++ ) {
                   9450:                 e = dl->d[i];
                   9451:                 PUT_EXP(DL(mr),i+nd_ntrans,e);
                   9452:                 td += MUL_WEIGHT(e,i+nd_ntrans);
                   9453:             }
                   9454:             if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
                   9455:             TD(DL(mr)) = td;
                   9456:             if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   9457:         }
                   9458:     }
                   9459:     NEXT(mr) = 0;
                   9460:     for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   9461:     MKND(NV(*p),mr0,len,r);
                   9462:     /* XXX */
                   9463:     SG(r) = SG(*p);
                   9464:     nd_free(*p);
                   9465:     *p = r;
                   9466:     return 1;
                   9467: }
                   9468:
                   9469: NODE reverse_node(NODE n)
                   9470: {
                   9471:     NODE t,t1;
                   9472:
                   9473:     for ( t = 0; n; n = NEXT(n) ) {
                   9474:         MKNODE(t1,BDY(n),t); t = t1;
                   9475:     }
                   9476:     return t;
                   9477: }
                   9478:
                   9479: P ndc_div(int mod,union oNDC a,union oNDC b)
                   9480: {
                   9481:     union oNDC c;
                   9482:     int inv,t;
                   9483:
                   9484:     if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10      noro     9485:     else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1       noro     9486:     else if ( mod ) {
                   9487:         inv = invm(b.m,mod);
                   9488:         DMAR(a.m,inv,0,mod,t); c.m = t;
                   9489:     } else if ( nd_vc )
                   9490:        divsp(nd_vc,a.p,b.p,&c.p);
                   9491:     else
                   9492:        divsz(a.z,b.z,&c.z);
                   9493:     return ndctop(mod,c);
                   9494: }
                   9495:
                   9496: P ndctop(int mod,union oNDC c)
                   9497: {
                   9498:     Z q;
                   9499:     int e;
                   9500:     GFS gfs;
                   9501:
                   9502:     if ( mod == -1 ) {
                   9503:         e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
                   9504:     } else if ( mod == -2 ) {
1.10      noro     9505:        q = c.z; return (P)q;
1.1       noro     9506:     } else if ( mod > 0 ) {
1.6       noro     9507:         STOZ(c.m,q); return (P)q;
1.1       noro     9508:     } else
                   9509:         return (P)c.p;
                   9510: }
                   9511:
                   9512: /* [0,0,0,cont] = p -> p/cont */
                   9513:
                   9514: void finalize_tracelist(int i,P cont)
                   9515: {
                   9516:  LIST l;
                   9517:  NODE node;
                   9518:  Z iq;
                   9519:
                   9520:   if ( !UNIQ(cont) ) {
                   9521:     node = mknode(4,NULLP,NULLP,NULLP,cont);
                   9522:     MKLIST(l,node); MKNODE(node,l,nd_tracelist);
                   9523:     nd_tracelist = node;
                   9524:   }
1.6       noro     9525:   STOZ(i,iq);
1.1       noro     9526:   nd_tracelist = reverse_node(nd_tracelist);
                   9527:   MKLIST(l,nd_tracelist);
                   9528:   node = mknode(2,iq,l); MKLIST(l,node);
                   9529:   MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
                   9530:   nd_alltracelist = node; nd_tracelist = 0;
                   9531: }
                   9532:
                   9533: void conv_ilist(int demand,int trace,NODE g,int **indp)
                   9534: {
                   9535:     int n,i,j;
                   9536:   int *ind;
                   9537:   NODE t;
                   9538:
                   9539:     n = length(g);
                   9540:   ind = (int *)MALLOC(n*sizeof(int));
                   9541:   for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
                   9542:     j = (long)BDY(t); ind[i] = j;
                   9543:     BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
                   9544:   }
                   9545:   if ( indp ) *indp = ind;
                   9546: }
                   9547:
1.30      noro     9548: NODE conv_ilist_s(int demand,int trace,int **indp)
                   9549: {
                   9550:   int n,i,j;
                   9551:   int *ind;
                   9552:   NODE g0,g;
                   9553:
                   9554:   n = nd_psn;
                   9555:   ind = (int *)MALLOC(n*sizeof(int));
                   9556:   g0 = 0;
                   9557:   for ( i = 0; i < n; i++ ) {
                   9558:     ind[i] = i;
                   9559:     NEXTNODE(g0,g);
                   9560:     BDY(g) = (pointer)(demand?ndv_load(i):(trace?nd_ps_trace[i]:nd_ps[i]));
                   9561:   }
                   9562:   if ( g0 ) NEXT(g) = 0;
                   9563:   if ( indp ) *indp = ind;
                   9564:   return g0;
                   9565: }
                   9566:
1.1       noro     9567: void parse_nd_option(NODE opt)
                   9568: {
                   9569:     NODE t,p,u;
                   9570:   int i,s,n;
                   9571:     char *key;
                   9572:     Obj value;
                   9573:
                   9574:     nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
                   9575:   nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
                   9576:   nd_splist = 0; nd_check_splist = 0;
                   9577:     nd_sugarweight = 0;
                   9578:     nd_f4red =0;
                   9579:     nd_rank0 = 0;
                   9580:     for ( t = opt; t; t = NEXT(t) ) {
                   9581:         p = BDY((LIST)BDY(t));
                   9582:         key = BDY((STRING)BDY(p));
                   9583:         value = (Obj)BDY(NEXT(p));
                   9584:         if ( !strcmp(key,"gentrace") )
                   9585:             nd_gentrace = value?1:0;
                   9586:         else if ( !strcmp(key,"gensyz") )
                   9587:             nd_gensyz = value?1:0;
                   9588:         else if ( !strcmp(key,"nora") )
                   9589:             nd_nora = value?1:0;
                   9590:         else if ( !strcmp(key,"gbblock") ) {
                   9591:             if ( value && OID(value) == O_LIST ) {
                   9592:         u = BDY((LIST)value);
                   9593:               nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
                   9594:         for ( i = 0; u; u = NEXT(u) ) {
                   9595:           p = BDY((LIST)BDY(u));
1.6       noro     9596:           s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
                   9597:           nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1       noro     9598:         }
                   9599:         nd_gbblock[i] = -1;
                   9600:             } else
                   9601:               nd_gbblock = 0;
                   9602:     } else if ( !strcmp(key,"newelim") )
                   9603:             nd_newelim = value?1:0;
                   9604:     else if ( !strcmp(key,"intersect") )
                   9605:             nd_intersect = value?1:0;
1.17      noro     9606:     else if ( !strcmp(key,"syzgen") )
                   9607:             nd_intersect = ZTOS((Q)value);
1.1       noro     9608:     else if ( !strcmp(key,"lf") )
                   9609:             nd_lf = value?1:0;
                   9610:     else if ( !strcmp(key,"trace") ) {
                   9611:            if ( value ) {
                   9612:                u = BDY((LIST)value);
                   9613:            nd_nzlist = BDY((LIST)ARG2(u));
1.6       noro     9614:            nd_bpe = ZTOS((Q)ARG3(u));
1.1       noro     9615:            }
                   9616:     } else if ( !strcmp(key,"f4red") ) {
1.6       noro     9617:        nd_f4red = ZTOS((Q)value);
1.1       noro     9618:     } else if ( !strcmp(key,"rank0") ) {
                   9619:             nd_rank0 = value?1:0;
                   9620:     } else if ( !strcmp(key,"splist") ) {
                   9621:             nd_splist = value?1:0;
                   9622:     } else if ( !strcmp(key,"check_splist") ) {
                   9623:       nd_check_splist = BDY((LIST)value);
                   9624:     } else if ( !strcmp(key,"sugarweight") ) {
                   9625:       u = BDY((LIST)value);
                   9626:             n = length(u);
                   9627:             nd_sugarweight = MALLOC(n*sizeof(int));
                   9628:       for ( i = 0; i < n; i++, u = NEXT(u) )
1.6       noro     9629:                 nd_sugarweight[i] = ZTOS((Q)BDY(u));
1.1       noro     9630:     }
                   9631:     }
                   9632: }
                   9633:
                   9634: ND mdptond(DP d);
                   9635: ND nd_mul_nm(int mod,NM m0,ND p);
                   9636: ND nd_mul_nm_lf(NM m0,ND p);
                   9637: ND *btog(NODE ti,ND **p,int nb,int mod);
                   9638: ND btog_one(NODE ti,ND *p,int nb,int mod);
                   9639: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
                   9640: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
                   9641:
                   9642: /* d:monomial */
                   9643: ND mdptond(DP d)
                   9644: {
                   9645:   NM m;
                   9646:   ND r;
                   9647:
                   9648:   if ( OID(d) == 1 )
                   9649:   r = ptond(CO,CO,(P)d);
                   9650:   else {
                   9651:     NEWNM(m);
                   9652:     dltondl(NV(d),BDY(d)->dl,DL(m));
1.6       noro     9653:     CZ(m) = (Z)BDY(d)->c;
1.1       noro     9654:     NEXT(m) = 0;
                   9655:     MKND(NV(d),m,1,r);
                   9656:   }
                   9657:     return r;
                   9658: }
                   9659:
                   9660: ND nd_mul_nm(int mod,NM m0,ND p)
                   9661: {
                   9662:   UINT *d0;
                   9663:   int c0,c1,c;
                   9664:   NM tm,mr,mr0;
                   9665:   ND r;
                   9666:
                   9667:   if ( !p ) return 0;
                   9668:   d0 = DL(m0);
                   9669:   c0 = CM(m0);
                   9670:   mr0 = 0;
                   9671:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   9672:     NEXTNM(mr0,mr);
                   9673:   c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
                   9674:   ndl_add(d0,DL(tm),DL(mr));
                   9675:   }
                   9676:   NEXT(mr) = 0;
                   9677:   MKND(NV(p),mr0,LEN(p),r);
                   9678:   return r;
                   9679: }
                   9680:
                   9681: ND nd_mul_nm_lf(NM m0,ND p)
                   9682: {
                   9683:   UINT *d0;
                   9684:   Z c0,c1,c;
                   9685:   NM tm,mr,mr0;
                   9686:   ND r;
                   9687:
                   9688:   if ( !p ) return 0;
                   9689:   d0 = DL(m0);
                   9690:   c0 = CZ(m0);
                   9691:   mr0 = 0;
                   9692:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   9693:     NEXTNM(mr0,mr);
                   9694:     c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
                   9695:     ndl_add(d0,DL(tm),DL(mr));
                   9696:   }
                   9697:   NEXT(mr) = 0;
                   9698:   MKND(NV(p),mr0,LEN(p),r);
                   9699:   return r;
                   9700: }
                   9701:
                   9702: ND *btog(NODE ti,ND **p,int nb,int mod)
                   9703: {
                   9704:   PGeoBucket *r;
                   9705:   int i,ci;
                   9706:   NODE t,s;
                   9707:   ND m,tp;
                   9708:   ND *pi,*rd;
                   9709:   P c;
                   9710:
                   9711:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   9712:   for ( i = 0; i < nb; i++ )
                   9713:     r[i] = create_pbucket();
                   9714:   for ( t = ti; t; t = NEXT(t) ) {
                   9715:   s = BDY((LIST)BDY(t));
                   9716:     if ( ARG0(s) ) {
                   9717:     m = mdptond((DP)ARG2(s));
1.6       noro     9718:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     9719:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   9720:       HCM(m) = ci;
1.6       noro     9721:       pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     9722:       for ( i = 0; i < nb; i++ ) {
                   9723:       tp = nd_mul_nm(mod,BDY(m),pi[i]);
                   9724:         add_pbucket(mod,r[i],tp);
                   9725:       }
                   9726:     }
                   9727:     ci = 1;
                   9728:     } else {
                   9729:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   9730:     ci = invm(ci,mod);
                   9731:   }
                   9732:   }
                   9733:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   9734:   for ( i = 0; i < nb; i++ )
                   9735:   rd[i] = normalize_pbucket(mod,r[i]);
                   9736:   if ( ci != 1 )
                   9737:     for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
                   9738:    return rd;
                   9739: }
                   9740:
                   9741: /* YYY */
                   9742: ND *btog_lf(NODE ti,ND **p,int nb)
                   9743: {
                   9744:   PGeoBucket *r;
                   9745:   int i;
                   9746:   NODE t,s;
                   9747:   ND m,tp;
                   9748:   ND *pi,*rd;
                   9749:   LM lm;
                   9750:   Z lf,c;
                   9751:
                   9752:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   9753:   for ( i = 0; i < nb; i++ )
                   9754:     r[i] = create_pbucket();
                   9755:   for ( t = ti; t; t = NEXT(t) ) {
                   9756:     s = BDY((LIST)BDY(t));
                   9757:     if ( ARG0(s) ) {
                   9758:       m = mdptond((DP)ARG2(s));
1.6       noro     9759:       simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1       noro     9760:       if ( lm ) {
                   9761:         lmtolf(lm,&lf); HCZ(m) = lf;
1.6       noro     9762:         pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     9763:         for ( i = 0; i < nb; i++ ) {
                   9764:           tp = nd_mul_nm_lf(BDY(m),pi[i]);
                   9765:           add_pbucket(-2,r[i],tp);
                   9766:         }
                   9767:       }
                   9768:       c = ONE;
                   9769:     } else {
                   9770:       simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
                   9771:     }
                   9772:   }
                   9773:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   9774:   for ( i = 0; i < nb; i++ )
                   9775:     rd[i] = normalize_pbucket(-2,r[i]);
                   9776:   for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
                   9777:   return rd;
                   9778: }
                   9779:
                   9780: ND btog_one(NODE ti,ND *p,int nb,int mod)
                   9781: {
                   9782:   PGeoBucket r;
                   9783:   int i,ci,j;
                   9784:   NODE t,s;
                   9785:   ND m,tp;
                   9786:   ND pi,rd;
                   9787:   P c;
                   9788:
                   9789:   r = create_pbucket();
                   9790:   for ( t = ti; t; t = NEXT(t) ) {
                   9791:   s = BDY((LIST)BDY(t));
                   9792:     if ( ARG0(s) ) {
                   9793:     m = mdptond((DP)ARG2(s));
1.6       noro     9794:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     9795:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   9796:       HCM(m) = ci;
1.6       noro     9797:       pi = p[j=ZTOS((Q)ARG1(s))];
1.1       noro     9798:     if ( !pi ) {
                   9799:       pi = nd_load_mod(j);
                   9800:       tp = nd_mul_nm(mod,BDY(m),pi);
                   9801:       nd_free(pi);
                   9802:         add_pbucket(mod,r,tp);
                   9803:     } else {
                   9804:       tp = nd_mul_nm(mod,BDY(m),pi);
                   9805:         add_pbucket(mod,r,tp);
                   9806:       }
                   9807:     }
                   9808:     ci = 1;
                   9809:     } else {
                   9810:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   9811:     ci = invm(ci,mod);
                   9812:   }
                   9813:   }
                   9814:   rd = normalize_pbucket(mod,r);
                   9815:   free_pbucket(r);
                   9816:   if ( ci != 1 ) nd_mul_c(mod,rd,ci);
                   9817:   return rd;
                   9818: }
                   9819:
                   9820: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
                   9821:
                   9822: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
                   9823: {
                   9824:   int i,j,n,m,nb,pi0,pi1,nvar;
                   9825:   VL fv,tv,vv;
                   9826:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   9827:   ND **p;
                   9828:   ND *c;
                   9829:   ND u;
                   9830:   P inv;
                   9831:   MAT mat;
                   9832:
                   9833:   if ( mod == -2 )
                   9834:     return nd_btog_lf(f,v,ord,tlist,rp);
                   9835:
                   9836:   parse_nd_option(current_option);
                   9837:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   9838:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   9839:   switch ( ord->id ) {
                   9840:     case 1:
                   9841:       if ( ord->nv != nvar )
                   9842:         error("nd_check : invalid order specification");
                   9843:       break;
                   9844:     default:
                   9845:       break;
                   9846:   }
                   9847:   nd_init_ord(ord);
                   9848: #if 0
1.6       noro     9849:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     9850: #else
                   9851:   nd_bpe = 32;
                   9852: #endif
                   9853:   nd_setup_parameters(nvar,0);
                   9854:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   9855:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   9856:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   9857:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   9858:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     9859:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     9860:   if ( j > i ) i = j;
                   9861:   }
                   9862:   n = i+1;
                   9863:   nb = length(BDY(f));
                   9864:   p = (ND **)MALLOC(n*sizeof(ND *));
                   9865:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   9866:     pi = BDY((LIST)BDY(t));
1.6       noro     9867:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     9868:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   9869:     ptomp(mod,(P)ARG2(pi),&inv);
                   9870:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   9871:     u = ptond(CO,vv,(P)ONE);
                   9872:     HCM(u) = ((MQ)inv)->cont;
                   9873:     c[pi1] = u;
                   9874:   }
                   9875:   for ( t = trace,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:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   9881:     printf("%d ",i); fflush(stdout);
                   9882:     ti = BDY((LIST)BDY(t));
1.6       noro     9883:     p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     9884:   }
                   9885:   m = length(ind);
                   9886:   MKMAT(mat,nb,m);
                   9887:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     9888:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     9889:       BDY(mat)[i][j] = ndtodp(mod,c[i]);
                   9890:   return mat;
                   9891: }
                   9892:
                   9893: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
                   9894: {
                   9895:   int i,j,n,m,nb,pi0,pi1,nvar;
                   9896:   VL fv,tv,vv;
                   9897:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   9898:   ND **p;
                   9899:   ND *c;
                   9900:   ND u;
                   9901:   MAT mat;
                   9902:   LM lm;
                   9903:   Z lf,inv;
                   9904:
                   9905:   parse_nd_option(current_option);
                   9906:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   9907:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   9908:   switch ( ord->id ) {
                   9909:     case 1:
                   9910:       if ( ord->nv != nvar )
                   9911:         error("nd_check : invalid order specification");
                   9912:       break;
                   9913:     default:
                   9914:       break;
                   9915:   }
                   9916:   nd_init_ord(ord);
                   9917: #if 0
1.6       noro     9918:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     9919: #else
                   9920:   nd_bpe = 32;
                   9921: #endif
                   9922:   nd_setup_parameters(nvar,0);
                   9923:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   9924:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   9925:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   9926:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   9927:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     9928:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     9929:   if ( j > i ) i = j;
                   9930:   }
                   9931:   n = i+1;
                   9932:   nb = length(BDY(f));
                   9933:   p = (ND **)MALLOC(n*sizeof(ND *));
                   9934:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   9935:     pi = BDY((LIST)BDY(t));
1.6       noro     9936:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     9937:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   9938:     simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
                   9939:     u = ptond(CO,vv,(P)ONE);
                   9940:     HCZ(u) = inv;
                   9941:     c[pi1] = u;
                   9942:   }
                   9943:   for ( t = trace,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:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   9949:     printf("%d ",i); fflush(stdout);
                   9950:     ti = BDY((LIST)BDY(t));
1.6       noro     9951:     p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     9952:   }
                   9953:   m = length(ind);
                   9954:   MKMAT(mat,nb,m);
                   9955:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     9956:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     9957:       BDY(mat)[i][j] = ndtodp(-2,c[i]);
                   9958:   return mat;
                   9959: }
                   9960:
                   9961: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
                   9962:   LIST tlist,int pos,MAT *rp)
                   9963: {
                   9964:   int i,j,n,m,nb,pi0,pi1,nvar;
                   9965:   VL fv,tv,vv;
                   9966:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   9967:   ND *p;
                   9968:   ND *c;
                   9969:   ND u;
                   9970:   P inv;
                   9971:   VECT vect;
                   9972:
                   9973:   if ( mod == -2 )
                   9974:     error("nd_btog_one : not implemented yet for a large finite field");
                   9975:
                   9976:   parse_nd_option(current_option);
                   9977:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   9978:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   9979:   switch ( ord->id ) {
                   9980:     case 1:
                   9981:       if ( ord->nv != nvar )
                   9982:         error("nd_check : invalid order specification");
                   9983:       break;
                   9984:     default:
                   9985:       break;
                   9986:   }
                   9987:   nd_init_ord(ord);
                   9988: #if 0
1.6       noro     9989:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     9990: #else
                   9991:   nd_bpe = 32;
                   9992: #endif
                   9993:   nd_setup_parameters(nvar,0);
                   9994:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   9995:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   9996:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   9997:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   9998:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     9999:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     10000:   if ( j > i ) i = j;
                   10001:   }
                   10002:   n = i+1;
                   10003:   nb = length(BDY(f));
                   10004:   p = (ND *)MALLOC(n*sizeof(ND *));
                   10005:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   10006:     pi = BDY((LIST)BDY(t));
1.6       noro     10007:   pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     10008:   if ( pi1 == pos ) {
                   10009:     ptomp(mod,(P)ARG2(pi),&inv);
                   10010:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   10011:     u = ptond(CO,vv,(P)ONE);
                   10012:     HCM(u) = ((MQ)inv)->cont;
                   10013:     p[pi0] = u;
                   10014:     }
                   10015:   }
                   10016:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   10017:   printf("%d ",i); fflush(stdout);
                   10018:     ti = BDY((LIST)BDY(t));
1.6       noro     10019:     p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10020:     if ( Demand ) {
                   10021:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   10022:   }
                   10023:   }
                   10024:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   10025:   printf("%d ",i); fflush(stdout);
                   10026:     ti = BDY((LIST)BDY(t));
1.6       noro     10027:     p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10028:     if ( Demand ) {
                   10029:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   10030:   }
                   10031:   }
                   10032:   m = length(ind);
                   10033:   MKVECT(vect,m);
                   10034:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6       noro     10035:   u = p[ZTOS((Q)BDY(t))];
1.1       noro     10036:   if ( !u ) {
1.6       noro     10037:     u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1       noro     10038:     BDY(vect)[j] = ndtodp(mod,u);
                   10039:     nd_free(u);
                   10040:   } else
                   10041:     BDY(vect)[j] = ndtodp(mod,u);
                   10042:   }
                   10043:   return vect;
                   10044: }
                   10045:
                   10046: void ndv_print_lf(NDV p)
                   10047: {
                   10048:     NMV m;
                   10049:     int i,len;
                   10050:
                   10051:     if ( !p ) printf("0\n");
                   10052:     else {
                   10053:         len = LEN(p);
                   10054:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   10055:             printf("+");
                   10056:             mpz_out_str(asir_out,10,BDY(CZ(m)));
                   10057:             printf("*");
                   10058:             ndl_print(DL(m));
                   10059:         }
                   10060:         printf("\n");
                   10061:     }
                   10062: }
                   10063:
                   10064: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
                   10065: {
                   10066:     VL tv,fv,vv,vc,av;
                   10067:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   10068:     int m,nocheck,nvar,mindex,e,max;
                   10069:     NDV c;
                   10070:     NMV a;
                   10071:     P p,zp;
                   10072:     Q dmy;
                   10073:     EPOS oepos;
                   10074:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   10075:     Alg alpha,dp;
                   10076:     P poly;
                   10077:     LIST f1,f2,zpl;
                   10078:     Obj obj;
                   10079:     NumberField nf;
                   10080:     struct order_spec *ord1;
                   10081:     struct oEGT eg_check,eg0,eg1;
                   10082:     NODE tr,tl1,tl2,tl3,tl4;
                   10083:     LIST l1,l2,l3,l4,l5;
                   10084:     int *perm;
                   10085:     int j,ret;
                   10086:     NODE retn;
                   10087:     Q jq,bpe;
                   10088:
                   10089:     nd_module = 0;
                   10090:     parse_nd_option(current_option);
                   10091:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   10092:     if ( nd_vc )
                   10093:       error("nd_f4_lf_trace : computation over a rational function field is not implemented");
                   10094:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   10095:     switch ( ord->id ) {
                   10096:         case 1:
                   10097:             if ( ord->nv != nvar )
                   10098:                 error("nd_f4_lf_trace : invalid order specification");
                   10099:             break;
                   10100:         default:
                   10101:             break;
                   10102:     }
                   10103:
                   10104:     nd_ntrans = nvar;
                   10105:     nd_nalg = 0;
                   10106:
                   10107:     nocheck = 0;
                   10108:     mindex = 0;
                   10109:
                   10110:     /* do not use on-demand load/save */
                   10111:     nd_demand = 0;
                   10112:     m = trace > 1 ? trace : get_lprime(mindex);
                   10113:     nd_init_ord(ord);
                   10114:     mrank = 0;
                   10115:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   10116:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   10117:             if ( nd_module ) {
                   10118:                 s = BDY((LIST)BDY(t));
                   10119:                 trank = length(s);
                   10120:                 mrank = MAX(mrank,trank);
                   10121:                 for ( ; s; s = NEXT(s) ) {
                   10122:                     e = getdeg(tv->v,(P)BDY(s));
                   10123:                     max = MAX(e,max);
                   10124:                 }
                   10125:             } else {
                   10126:                 e = getdeg(tv->v,(P)BDY(t));
                   10127:                 max = MAX(e,max);
                   10128:             }
                   10129:         }
                   10130:     nd_setup_parameters(nvar,max);
                   10131:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   10132:     ishomo = 1;
                   10133:     /* XXX */
                   10134:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   10135:         if ( nd_module ) {
                   10136:             c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
                   10137:         } else {
                   10138:             c = (pointer)ptondv(CO,vv,(P)BDY(t));
                   10139:         }
                   10140:         if ( ishomo )
                   10141:             ishomo = ishomo && ndv_ishomo(c);
                   10142:         if ( c ) {
                   10143:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   10144:             ndv_mod(-2,c);
                   10145:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   10146:         }
                   10147:     }
                   10148:     if ( in0 ) NEXT(in) = 0;
                   10149:     if ( fd0 ) NEXT(fd) = 0;
                   10150:     if ( !ishomo && homo ) {
                   10151:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   10152:             c = (NDV)BDY(t); len = LEN(c);
                   10153:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   10154:                 wmax = MAX(TD(DL(a)),wmax);
                   10155:         }
                   10156:         homogenize_order(ord,nvar,&ord1);
                   10157:         nd_init_ord(ord1);
                   10158:         nd_setup_parameters(nvar+1,wmax);
                   10159:         for ( t = fd0; t; t = NEXT(t) )
                   10160:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   10161:     }
                   10162:     if ( MaxDeg > 0 ) nocheck = 1;
1.24      noro     10163:     ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1       noro     10164:     if ( ret )
                   10165:       cand = nd_f4_lf_trace_main(m,&perm);
                   10166:     if ( !ret || !cand ) {
                   10167:        *rp = 0; return;
                   10168:     }
                   10169:     if ( !ishomo && homo ) {
                   10170:       /* dehomogenization */
                   10171:       for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   10172:       nd_init_ord(ord);
                   10173:       nd_setup_parameters(nvar,0);
                   10174:     }
                   10175:     cand = ndv_reducebase(cand,perm);
                   10176:     cand = ndv_reduceall(-2,cand);
                   10177:     cbpe = nd_bpe;
                   10178:     get_eg(&eg0);
                   10179:     if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   10180:       /* gbcheck : cand is a GB of Id(cand) ? */
                   10181:       retn = nd_f4(-2,0,0);
                   10182:     }
                   10183:   if ( !retn ) {
                   10184:       /* failure */
                   10185:       *rp = 0; return;
                   10186:     }
                   10187:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   10188:     if ( DP_Print )
1.5       noro     10189:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     10190:     /* dp->p */
                   10191:     nd_bpe = cbpe;
                   10192:     nd_setup_parameters(nd_nvar,0);
                   10193:     for ( r = cand; r; r = NEXT(r) ) {
                   10194:       if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
                   10195:       else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
                   10196:     }
                   10197:     MKLIST(*rp,cand);
                   10198: }
                   10199:
                   10200: NODE nd_f4_lf_trace_main(int m,int **indp)
                   10201: {
                   10202:     int i,nh,stat,index;
                   10203:     NODE r,rm,g;
                   10204:     ND_pairs d,l,l0,t;
                   10205:     ND spol,red;
                   10206:     NDV nf,redv,nfqv,nfv;
                   10207:     NM s0,s;
                   10208:     NODE rp0,srp0,nflist,nflist_lf;
                   10209:     int nsp,nred,col,rank,len,k,j,a;
                   10210:     UINT c;
                   10211:     UINT **spmat;
                   10212:     UINT *s0vect,*svect,*p,*v;
                   10213:     int *colstat;
                   10214:     IndArray *imat;
                   10215:     int *rhead;
                   10216:     int spcol,sprow;
                   10217:     int sugar;
                   10218:     PGeoBucket bucket;
                   10219:     struct oEGT eg0,eg1,eg_f4;
                   10220:
                   10221:     g = 0; d = 0;
                   10222:     for ( i = 0; i < nd_psn; i++ ) {
                   10223:         d = update_pairs(d,g,i,0);
                   10224:         g = update_base(g,i);
                   10225:     }
                   10226:     while ( d ) {
                   10227:         get_eg(&eg0);
                   10228:         l = nd_minsugarp(d,&d);
                   10229:         sugar = SG(l);
                   10230:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   10231:         bucket = create_pbucket();
                   10232:         stat = nd_sp_f4(m,0,l,bucket);
                   10233:         if ( !stat ) {
                   10234:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10235:             NEXT(t) = d; d = l;
                   10236:             d = nd_reconstruct(1,d);
                   10237:             continue;
                   10238:         }
                   10239:         if ( bucket->m < 0 ) continue;
                   10240:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   10241:         if ( !col ) {
                   10242:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10243:             NEXT(t) = d; d = l;
                   10244:             d = nd_reconstruct(1,d);
                   10245:             continue;
                   10246:         }
                   10247:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   10248:         if ( DP_Print )
1.5       noro     10249:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1       noro     10250:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   10251:         if ( !l0 ) continue;
                   10252:         l = l0;
                   10253:
                   10254:         /* over LF */
                   10255:         bucket = create_pbucket();
                   10256:         stat = nd_sp_f4(-2,1,l,bucket);
                   10257:         if ( !stat ) {
                   10258:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10259:             NEXT(t) = d; d = l;
                   10260:             d = nd_reconstruct(1,d);
                   10261:             continue;
                   10262:         }
                   10263:         if ( bucket->m < 0 ) continue;
                   10264:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   10265:         if ( !col ) {
                   10266:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10267:             NEXT(t) = d; d = l;
                   10268:             d = nd_reconstruct(1,d);
                   10269:             continue;
                   10270:         }
                   10271:         nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
                   10272:         /* adding new bases */
                   10273:         for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
                   10274:             nfv = (NDV)BDY(rm);
                   10275:             nfqv = (NDV)BDY(r);
                   10276:             if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
                   10277:             ndv_removecont(m,nfv);
                   10278:             ndv_removecont(-2,nfqv);
1.24      noro     10279:             nh = ndv_newps(-2,nfv,nfqv);
1.1       noro     10280:             d = update_pairs(d,g,nh,0);
                   10281:             g = update_base(g,nh);
                   10282:         }
                   10283:         if ( r || rm ) return 0;
                   10284:     }
                   10285:   conv_ilist(nd_demand,1,g,indp);
                   10286:     return g;
                   10287: }
                   10288:
1.7       noro     10289: #if SIZEOF_LONG==8
                   10290:
                   10291: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   10292: {
                   10293:     int j,k,len;
                   10294:     UINT *p;
                   10295:     UINT c;
                   10296:     NDV r;
                   10297:     NMV mr0,mr;
                   10298:
                   10299:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   10300:     if ( !len ) return 0;
                   10301:     else {
                   10302:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   10303: #if 0
                   10304:         ndv_alloc += nmv_adv*len;
                   10305: #endif
                   10306:         mr = mr0;
                   10307:         p = s0vect;
                   10308:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   10309:             if ( !rhead[j] ) {
                   10310:                 if ( (c = (UINT)vect[k++]) != 0 ) {
                   10311:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   10312:                 }
                   10313:             }
                   10314:         MKNDV(nd_nvar,mr0,len,r);
                   10315:         return r;
                   10316:     }
                   10317: }
                   10318:
1.28      noro     10319: NDV vect64_to_ndv_s(mp_limb_t *vect,int col,UINT *s0vect)
                   10320: {
                   10321:     int j,k,len;
                   10322:     UINT *p;
                   10323:     UINT c;
                   10324:     NDV r;
                   10325:     NMV mr0,mr;
                   10326:
                   10327:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   10328:     if ( !len ) return 0;
                   10329:     else {
                   10330:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   10331:         mr = mr0;
                   10332:         p = s0vect;
                   10333:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   10334:           if ( (c = (UINT)vect[k++]) != 0 ) {
                   10335:             ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   10336:           }
                   10337:         MKNDV(nd_nvar,mr0,len,r);
                   10338:         return r;
                   10339:     }
                   10340: }
                   10341:
1.7       noro     10342: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
                   10343: {
                   10344:     NM m;
1.11      noro     10345:     UINT *t,*s,*u;
                   10346:     int i,st,ed,md,prev,c;
1.7       noro     10347:
                   10348:     for ( i = 0; i < n; i++ ) r[i] = 0;
1.11      noro     10349:     prev = 0;
                   10350:     for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
                   10351:       t = DL(m);
                   10352:       st = prev;
                   10353:       ed = n;
                   10354:       while ( ed > st ) {
                   10355:         md = (st+ed)/2;
                   10356:         u = s0+md*nd_wpd;
                   10357:         c = DL_COMPARE(u,t);
                   10358:         if ( c == 0 ) break;
                   10359:         else if ( c > 0 ) st = md;
                   10360:         else ed = md;
                   10361:       }
                   10362:       r[md] = (mp_limb_t)CM(m);
                   10363:       prev = md;
1.7       noro     10364:     }
                   10365:     for ( i = 0; !r[i]; i++ );
                   10366:     return i;
                   10367: }
                   10368:
                   10369: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
                   10370:
1.28      noro     10371: 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     10372: {
                   10373:     int i,j,k,len,pos,prev;
                   10374:     mp_limb_t a,c,c1,c2;
                   10375:     IndArray ivect;
                   10376:     unsigned char *ivc;
                   10377:     unsigned short *ivs;
                   10378:     unsigned int *ivi;
                   10379:     NDV redv;
                   10380:     NMV mr;
                   10381:     NODE rp;
                   10382:     int maxrs;
                   10383:
                   10384:     for ( i = 0; i < col; i++ ) cvect[i] = 0;
                   10385:     maxrs = 0;
                   10386:     for ( i = 0; i < nred; i++ ) {
                   10387:         ivect = imat[i];
                   10388:         k = ivect->head;
                   10389:         a = svect[k]; c = cvect[k];
                   10390:         MOD128(a,c,m);
                   10391:         svect[k] = a; cvect[k] = 0;
1.28      noro     10392:         if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.11      noro     10393:             Nf4_red++;
1.7       noro     10394:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   10395:             c = m-c; redv = nd_ps[rp0[i]->index];
                   10396:             len = LEN(redv); mr = BDY(redv);
                   10397:             svect[k] = 0; prev = k;
                   10398:             switch ( ivect->width ) {
                   10399:                 case 1:
                   10400:                     ivc = ivect->index.c;
                   10401:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   10402:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12      noro     10403:                         c2 = svect[pos]+c1*c;
                   10404:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   10405:                         svect[pos] = c2;
1.7       noro     10406:                     }
                   10407:                     break;
                   10408:                 case 2:
                   10409:                     ivs = ivect->index.s;
                   10410:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   10411:                         pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12      noro     10412:                         c2 = svect[pos]+c1*c;
                   10413:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   10414:                         svect[pos] = c2;
1.7       noro     10415:                     }
                   10416:                     break;
                   10417:                 case 4:
                   10418:                     ivi = ivect->index.i;
                   10419:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   10420:                         pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12      noro     10421:                         c2 = svect[pos]+c1*c;
                   10422:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   10423:                         svect[pos] = c2;
1.7       noro     10424:                     }
                   10425:                     break;
                   10426:             }
                   10427:         }
                   10428:     }
                   10429:     for ( i = 0; i < col; i++ ) {
                   10430:       a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
                   10431:     }
                   10432:     return maxrs;
                   10433: }
                   10434:
                   10435: /* for Fp, 2^15=<p<2^29 */
                   10436:
                   10437: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   10438:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   10439: {
                   10440:     int spcol,sprow,a;
                   10441:     int i,j,k,l,rank;
                   10442:     NODE r0,r;
                   10443:     ND_pairs sp;
                   10444:     ND spol;
                   10445:     mp_limb_t **spmat;
                   10446:     mp_limb_t *svect,*cvect;
                   10447:     mp_limb_t *v;
                   10448:     int *colstat;
                   10449:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   10450:     int maxrs;
                   10451:     int *spsugar;
                   10452:     ND_pairs *spactive;
                   10453:
                   10454:     spcol = col-nred;
                   10455:     get_eg(&eg0);
                   10456:     /* elimination (1st step) */
                   10457:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   10458:     svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   10459:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   10460:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   10461:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   10462:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   10463:         nd_sp(m,0,sp,&spol);
                   10464:         if ( !spol ) continue;
                   10465:         nd_to_vect64(m,s0vect,col,spol,svect);
1.28      noro     10466:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,0);
1.7       noro     10467:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   10468:         if ( i < col ) {
                   10469:             spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
                   10470:             for ( j = k = 0; j < col; j++ )
                   10471:                 if ( !rhead[j] ) v[k++] = (UINT)svect[j];
                   10472:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   10473:             if ( nz )
                   10474:             spactive[sprow] = sp;
                   10475:             sprow++;
                   10476:         }
                   10477:         nd_free(spol);
                   10478:     }
1.12      noro     10479:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7       noro     10480:     if ( DP_Print ) {
                   10481:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   10482:         fflush(asir_out);
                   10483:     }
                   10484:     /* free index arrays */
                   10485:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   10486:
                   10487:     /* elimination (2nd step) */
                   10488:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   10489:     rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   10490:     r0 = 0;
                   10491:     for ( i = 0; i < rank; i++ ) {
                   10492:         NEXTNODE(r0,r); BDY(r) =
                   10493:           (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   10494:         SG((NDV)BDY(r)) = spsugar[i];
                   10495:         GCFREE(spmat[i]);
                   10496:     }
                   10497:     if ( r0 ) NEXT(r) = 0;
                   10498:
                   10499:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12      noro     10500:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7       noro     10501:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   10502:     if ( DP_Print ) {
                   10503:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   10504:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   10505:             nsp,nred,sprow,spcol,rank);
                   10506:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   10507:     }
                   10508:     if ( nz ) {
                   10509:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   10510:         if ( rank > 0 ) {
                   10511:             NEXT(spactive[rank-1]) = 0;
                   10512:             *nz = spactive[0];
                   10513:         } else
                   10514:             *nz = 0;
                   10515:     }
                   10516:     return r0;
                   10517: }
                   10518:
                   10519: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   10520: {
                   10521:   int i,j,k,l,rank,s;
                   10522:   mp_limb_t inv;
                   10523:   mp_limb_t a;
                   10524:   UINT c;
                   10525:   mp_limb_t *t,*pivot,*pk;
                   10526:   UINT *ck;
                   10527:   UINT **cmat;
                   10528:   UINT *ct;
                   10529:   ND_pairs pair;
                   10530:
                   10531:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   10532:   for ( i = 0; i < row; i++ ) {
                   10533:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   10534:     bzero(cmat[i],col*sizeof(UINT));
                   10535:   }
                   10536:
                   10537:   for ( rank = 0, j = 0; j < col; j++ ) {
                   10538:     for ( i = rank; i < row; i++ ) {
                   10539:       a = mat[i][j]; c = cmat[i][j];
                   10540:       MOD128(a,c,md);
                   10541:       mat[i][j] = a; cmat[i][j] = 0;
                   10542:     }
                   10543:     for ( i = rank; i < row; i++ )
                   10544:       if ( mat[i][j] )
                   10545:         break;
                   10546:     if ( i == row ) {
                   10547:       colstat[j] = 0;
                   10548:       continue;
                   10549:     } else
                   10550:       colstat[j] = 1;
                   10551:     if ( i != rank ) {
                   10552:       t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   10553:       ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
                   10554:       s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   10555:       if ( spactive ) {
                   10556:         pair = spactive[i]; spactive[i] = spactive[rank];
                   10557:         spactive[rank] = pair;
                   10558:       }
                   10559:     }
                   10560:     /* column j is normalized */
                   10561:     s = sugar[rank];
                   10562:     inv = invm((UINT)mat[rank][j],md);
                   10563:     /* normalize pivot row */
                   10564:     for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
                   10565:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   10566:     }
                   10567:     for ( i = rank+1; i < row; i++ ) {
                   10568:       if ( (a = mat[i][j]) != 0 ) {
                   10569:         sugar[i] = MAX(sugar[i],s);
                   10570:         red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11      noro     10571:         Nf4_red++;
1.7       noro     10572:       }
                   10573:     }
                   10574:     rank++;
                   10575:   }
                   10576:   for ( j = col-1, l = rank-1; j >= 0; j-- )
                   10577:     if ( colstat[j] ) {
                   10578:       for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
                   10579:         a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
                   10580:       }
                   10581:       s = sugar[l];
                   10582:       for ( i = 0; i < l; i++ ) {
                   10583:         a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
                   10584:         if ( a ) {
                   10585:           sugar[i] = MAX(sugar[i],s);
                   10586:           red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11      noro     10587:           Nf4_red++;
1.7       noro     10588:         }
                   10589:       }
                   10590:       l--;
                   10591:     }
                   10592:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   10593:   GCFREE(cmat);
                   10594:   return rank;
                   10595: }
                   10596: #endif
                   10597:
1.28      noro     10598: 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)
                   10599: {
                   10600:   int i,j,k,l,rank,s,imin;
                   10601:   mp_limb_t inv;
                   10602:   mp_limb_t a;
                   10603:   UINT c;
                   10604:   mp_limb_t *t,*pivot,*pk;
                   10605:   UINT *ck;
                   10606:   UINT **cmat;
                   10607:   UINT *ct;
                   10608:   ND_pairs pair;
                   10609:   SIG sg;
1.31      noro     10610:   int *used;
1.28      noro     10611:
1.31      noro     10612:   used = (int *)MALLOC(row*sizeof(int));
1.28      noro     10613:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   10614:   for ( i = 0; i < row; i++ ) {
                   10615:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   10616:     bzero(cmat[i],col*sizeof(UINT));
                   10617:   }
                   10618:
1.31      noro     10619:   for ( j = 0; j < col; j++ ) {
                   10620:     for ( i = 0; i < row; i++ ) {
1.28      noro     10621:       a = mat[i][j]; c = cmat[i][j];
                   10622:       MOD128(a,c,md);
                   10623:       mat[i][j] = a; cmat[i][j] = 0;
                   10624:     }
1.31      noro     10625:     for ( i = 0; i < row; i++ )
                   10626:       if ( !used[i] && mat[i][j] ) break;
                   10627:     if ( i == row ) {
1.28      noro     10628:       colstat[j] = 0;
                   10629:       continue;
1.31      noro     10630:     } else {
1.28      noro     10631:       colstat[j] = 1;
1.31      noro     10632:       used[i] = 1;
1.28      noro     10633:     }
                   10634:     /* column j is normalized */
1.31      noro     10635:     s = sugar[i];
                   10636:     inv = invm((UINT)mat[i][j],md);
1.28      noro     10637:     /* normalize pivot row */
1.31      noro     10638:     for ( k = j, pk = mat[i]+j, ck = cmat[i]+j; k < col; k++, pk++, ck++ ) {
1.28      noro     10639:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   10640:     }
1.31      noro     10641:     for ( k = i+1; k < row; k++ ) {
                   10642:       if ( (a = mat[k][j]) != 0 ) {
                   10643:         sugar[k] = MAX(sugar[k],s);
                   10644:         red_by_vect64(md,mat[k]+j,cmat[k]+j,mat[i]+j,(int)(md-a),col-j);
1.28      noro     10645:         Nf4_red++;
                   10646:       }
                   10647:     }
                   10648:   }
1.31      noro     10649:   rank = 0;
                   10650:   for ( i = 0; i < row; i++ ) {
                   10651:     for ( j = 0; j < col; j++ )
                   10652:       if ( mat[i][j] ) break;
                   10653:     if ( j == col ) sugar[i] = -1;
                   10654:     else rank++;
                   10655:   }
1.28      noro     10656:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   10657:   GCFREE(cmat);
                   10658:   return rank;
                   10659: }
                   10660:
                   10661: NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   10662:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
                   10663: {
                   10664:     int spcol,sprow,a;
                   10665:     int i,j,k,l,rank;
                   10666:     NODE r0,r;
                   10667:     ND_pairs sp;
                   10668:     ND spol;
                   10669:     mp_limb_t **spmat;
                   10670:     mp_limb_t *svect,*cvect;
                   10671:     mp_limb_t *v;
                   10672:     int *colstat;
                   10673:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   10674:     int maxrs;
                   10675:     int *spsugar;
                   10676:     ND_pairs *spactive;
                   10677:     SIG *spsig;
                   10678:
                   10679:     get_eg(&eg0);
                   10680:     /* elimination (1st step) */
                   10681:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   10682:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   10683:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   10684:     spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
                   10685:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   10686:         nd_sp(m,0,sp,&spol);
                   10687:         if ( !spol ) {
1.29      noro     10688:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     10689:           continue;
                   10690:         }
                   10691:         svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   10692:         nd_to_vect64(m,s0vect,col,spol,svect);
                   10693:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,spol->sig);
                   10694:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   10695:         if ( i < col ) {
                   10696:             spmat[sprow] = svect;
                   10697:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   10698:             spsig[sprow] = sp->sig;
                   10699:             sprow++;
                   10700:         } else {
1.29      noro     10701:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     10702:         }
                   10703:         nd_free(spol);
                   10704:     }
                   10705:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
                   10706:     if ( DP_Print ) {
                   10707:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   10708:         fflush(asir_out);
                   10709:     }
                   10710:     /* free index arrays */
                   10711:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   10712:
                   10713:     /* elimination (2nd step) */
                   10714:     colstat = (int *)MALLOC(col*sizeof(int));
                   10715:     rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
                   10716:     r0 = 0;
1.31      noro     10717:     for ( i = 0; i < sprow; i++ ) {
                   10718:         if ( spsugar[i] >= 0 ) {
                   10719:           NEXTNODE(r0,r);
                   10720:           BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
                   10721:           SG((NDV)BDY(r)) = spsugar[i];
                   10722:           ((NDV)BDY(r))->sig = spsig[i];
                   10723:         } else
                   10724:           syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
1.28      noro     10725:         GCFREE(spmat[i]);
                   10726:     }
                   10727:     if ( r0 ) NEXT(r) = 0;
                   10728:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
                   10729:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   10730:     if ( DP_Print ) {
                   10731:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   10732:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   10733:             nsp,nred,sprow,col,rank);
                   10734:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   10735:     }
                   10736:     return r0;
                   10737: }
                   10738:
                   10739: NODE nd_f4_red_s(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,NODE *syzlistp)
                   10740: {
                   10741:   IndArray *imat;
                   10742:   int nsp,nred,i,start;
                   10743:   int *rhead;
                   10744:   NODE r0,rp;
                   10745:   ND_pairs sp;
                   10746:   NM_ind_pair *rvect;
                   10747:   UINT *s;
                   10748:   int *s0hash;
                   10749:   struct oEGT eg0,eg1,eg_conv;
                   10750:
                   10751:   for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   10752:   nred = length(rp0);
                   10753:   imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   10754:   rhead = (int *)MALLOC(col*sizeof(int));
                   10755:   for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   10756:
                   10757:   /* construction of index arrays */
                   10758:   get_eg(&eg0);
                   10759:   if ( DP_Print ) {
                   10760:     fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   10761:     fflush(asir_out);
                   10762:   }
                   10763:   rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   10764:   for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   10765:     rvect[i] = (NM_ind_pair)BDY(rp);
                   10766:     imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
                   10767:     rhead[imat[i]->head] = 1;
                   10768:     start = imat[i]->head;
                   10769:   }
                   10770:   get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
                   10771:   if ( DP_Print ) {
                   10772:     fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   10773:     fflush(asir_out);
                   10774:   }
                   10775:   if ( m > 0 )
                   10776:     r0 = nd_f4_red_mod64_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
                   10777:   else
                   10778: //    r0 = nd_f4_red_q_main_s(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   10779:     error("nd_f4_red_q_main_s : not implemented yet");
                   10780:   return r0;
                   10781: }
                   10782:
                   10783: INLINE int ndl_find_reducer_minsig(UINT *dg)
                   10784: {
                   10785:   RHist r;
                   10786:   int i,singular,ret,d,k,imin;
                   10787:   SIG t;
                   10788:   static int wpd,nvar;
                   10789:   static SIG quo,quomin;
                   10790:   static UINT *tmp;
                   10791:
                   10792:   if ( !quo || nvar != nd_nvar ) { NEWSIG(quo); NEWSIG(quomin); }
                   10793:   if ( wpd != nd_wpd ) {
                   10794:     wpd = nd_wpd;
                   10795:     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
                   10796:   }
                   10797: #if 0
                   10798:   d = ndl_hash_value(dg);
                   10799:   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   10800:     if ( ndl_equal(dg,DL(r)) ) {
                   10801:       return r->index;
                   10802:     }
                   10803:   }
                   10804: #endif
                   10805:   imin = -1;
                   10806:   for ( i = 0; i < nd_psn; i++ ) {
                   10807:     r = nd_psh[i];
                   10808:     if ( ndl_reducible(dg,DL(r)) ) {
                   10809:       ndl_sub(dg,DL(r),tmp);
                   10810:       _ndltodl(tmp,DL(quo));
                   10811:       _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
                   10812:       quo->pos = nd_psh[i]->sig->pos;
                   10813:       if ( imin < 0 || comp_sig(quomin,quo) > 0 ) {
                   10814:         t = quo; quo = quomin; quomin = t;
                   10815:         imin = i;
                   10816:       }
                   10817:     }
                   10818:   }
                   10819:   if ( imin == -1 ) return nd_psn;
                   10820:   else {
                   10821: #if 0
                   10822:     nd_append_red(dg,i);
                   10823: #endif
                   10824:     return imin;
                   10825:   }
                   10826: }
                   10827:
                   10828: int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   10829: {
                   10830:   NODE rp0,rp;
                   10831:   NM mul,head,s0,s;
                   10832:   int index,col,i,sugar;
                   10833:   RHist h;
                   10834:   UINT *s0v,*p;
                   10835:   NM_ind_pair pair;
                   10836:   ND red;
                   10837:   NDV *ps;
                   10838:   SIG sig;
                   10839:
                   10840:   s0 = 0; rp0 = 0; col = 0;
                   10841:   if ( nd_demand )
                   10842:     ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   10843:   else
                   10844:     ps = trace?nd_ps_trace:nd_ps;
                   10845:   while ( 1 ) {
                   10846:     head = remove_head_pbucket_symbolic(bucket);
                   10847:     if ( !head ) break;
                   10848:     if ( !s0 ) s0 = head;
                   10849:     else NEXT(s) = head;
                   10850:     s = head;
                   10851:     index = ndl_find_reducer_minsig(DL(head));
                   10852:     if ( index >= 0 && index < nd_psn ) {
                   10853:       h = nd_psh[index];
                   10854:       NEWNM(mul);
                   10855:       ndl_sub(DL(head),DL(h),DL(mul));
                   10856:       if ( ndl_check_bound2(index,DL(mul)) )
                   10857:         return 0;
                   10858:       sugar = TD(DL(mul))+SG(ps[index]);
                   10859:       NEWSIG(sig);
                   10860:       _ndltodl(DL(mul),DL(sig));
                   10861:       _addtodl(nd_nvar,DL(nd_psh[index]->sig),DL(sig));
                   10862:       sig->pos = nd_psh[index]->sig->pos;
                   10863:       MKNM_ind_pair(pair,mul,index,sugar,sig);
                   10864:       red = ndv_mul_nm_symbolic(mul,ps[index]);
                   10865:       add_pbucket_symbolic(bucket,nd_remove_head(red));
                   10866:       NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   10867:     }
                   10868:     col++;
                   10869:   }
                   10870:   if ( rp0 ) NEXT(rp) = 0;
                   10871:   NEXT(s) = 0;
                   10872:   s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   10873:   for ( i = 0, p = s0v, s = s0; i < col;
                   10874:     i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   10875:   *s0vect = s0v;
                   10876:   *r = rp0;
                   10877:
                   10878:   return col;
                   10879: }
                   10880:
                   10881: NODE nd_sba_f4(int m,int **indp)
                   10882: {
1.34      noro     10883:   int i,nh,stat,index,f4red,f4step;
1.33      noro     10884:   int col,rank,len,k,j,a,sugar,nbase,psugar,ms;
                   10885:   NODE r,g,rp0,nflist;
                   10886:   ND_pairs d,l,t;
                   10887:   ND h,nf;
                   10888:   NDV nfv;
                   10889:   union oNDC hc;
                   10890:   UINT *s0vect;
1.28      noro     10891:   UINT c;
                   10892:   PGeoBucket bucket;
1.33      noro     10893:   NODE *syzlist;
                   10894:   SIG sig;
1.28      noro     10895:   struct oEGT eg0,eg1,eg_f4;
1.33      noro     10896:   struct oEGT eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.28      noro     10897:
                   10898:   Nf4_red=0;
1.30      noro     10899:   d = 0;
1.29      noro     10900:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.28      noro     10901:   for ( i = 0; i < nd_psn; i++ ) {
1.30      noro     10902:     d = update_pairs_s(d,i,syzlist);
1.28      noro     10903:   }
1.30      noro     10904:   nd_nbase = nd_psn;
1.28      noro     10905:   f4red = 1;
1.33      noro     10906:   psugar = 0;
1.34      noro     10907:   f4step = 0;
1.28      noro     10908:   while ( d ) {
1.33      noro     10909:     for ( t = d, ms = SG(d); t; t = NEXT(t) )
                   10910:       if ( SG(t) < ms ) ms = SG(t);
1.34      noro     10911:     if ( ms == psugar && f4step >= 2 ) {
1.33      noro     10912: again:
                   10913:       l = d; d = d->next;
                   10914:       if ( small_lcm(l) ) {
                   10915:         if ( DP_Print ) fprintf(asir_out,"M");
                   10916:         continue;
                   10917:       }
                   10918:       sig = l->sig;
                   10919:       stat = nd_sp(m,0,l,&h);
                   10920:       if ( !stat ) {
                   10921:         NEXT(l) = d; d = l;
                   10922:         d = nd_reconstruct(0,d);
                   10923:         goto again;
                   10924:       }
                   10925:   get_eg(&eg1);
                   10926:   #if USE_GEOBUCKET
                   10927:       stat = m?nd_nf_pbucket_s(m,h,nd_ps,!Top,&nf):nd_nf_s(m,0,h,nd_ps,!Top,&nf);
                   10928:   #else
                   10929:       stat = nd_nf_s(m,0,h,nd_ps,!Top,&nf);
                   10930:   #endif
                   10931:   get_eg(&eg2);
                   10932:       if ( !stat ) {
                   10933:         NEXT(l) = d; d = l;
                   10934:         d = nd_reconstruct(0,d);
                   10935:         goto again;
                   10936:       } else if ( stat == -1 ) {
                   10937:         if ( DP_Print ) { printf("S"); fflush(stdout); }
                   10938:         FREENDP(l);
                   10939:       } else if ( nf ) {
                   10940:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   10941:         add_eg(&eg_nf,&eg1,&eg2);
                   10942:         hc = HCU(nf);
                   10943:         nd_removecont(m,nf);
                   10944:         nfv = ndtondv(m,nf); nd_free(nf);
                   10945:         nh = ndv_newps(m,nfv,0);
                   10946:
1.30      noro     10947:         d = update_pairs_s(d,nh,syzlist);
                   10948:         nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.33      noro     10949:         FREENDP(l);
1.28      noro     10950:       } else {
1.33      noro     10951:         add_eg(&eg_nfzero,&eg1,&eg2);
                   10952:        // syzygy
                   10953:   get_eg(&eg1);
                   10954:         d = remove_spair_s(d,sig);
                   10955:   get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29      noro     10956:         syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.33      noro     10957:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   10958:         FREENDP(l);
                   10959:       }
                   10960:     } else {
1.34      noro     10961:       if ( ms != psugar ) f4step = 1;
                   10962:       else f4step++;
1.33      noro     10963: again2:
                   10964:       psugar = ms;
                   10965:       l = nd_minsugarp_s(d,&d);
                   10966:       sugar = nd_sugarweight?d->sugar2:SG(d);
                   10967:       bucket = create_pbucket();
                   10968:       stat = nd_sp_f4(m,0,l,bucket);
                   10969:       if ( !stat ) {
                   10970:         for ( t = l; NEXT(t); t = NEXT(t) );
                   10971:         NEXT(t) = d; d = l;
                   10972:         d = nd_reconstruct(0,d);
                   10973:         goto again2;
                   10974:       }
                   10975:       if ( bucket->m < 0 ) continue;
                   10976:       col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
                   10977:       if ( !col ) {
                   10978:         for ( t = l; NEXT(t); t = NEXT(t) );
                   10979:           NEXT(t) = d; d = l;
                   10980:         d = nd_reconstruct(0,d);
                   10981:         goto again2;
                   10982:       }
1.34      noro     10983:       if ( DP_Print ) fprintf(asir_out,"\nsugar=%d,",psugar);
1.33      noro     10984:       nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
                   10985:       /* adding new bases */
                   10986:       for ( r = nflist; r; r = NEXT(r) ) {
                   10987:         nfv = (NDV)BDY(r);
                   10988:         ndv_removecont(m,nfv);
                   10989:         nh = ndv_newps(m,nfv,0);
                   10990:         d = update_pairs_s(d,nh,syzlist);
                   10991:         nd_sba_pos[nfv->sig->pos] = append_one(nd_sba_pos[nfv->sig->pos],nh);
                   10992:       }
                   10993:       for ( i = 0; i < nd_nbase; i++ )
                   10994:         for ( r = syzlist[i]; r; r = NEXT(r) )
                   10995:             d = remove_spair_s(d,(SIG)BDY(r));
                   10996:       d = remove_large_lcm(d);
                   10997:       if ( DP_Print ) {
1.34      noro     10998:         fprintf(asir_out,"f4red=%d,gblen=%d",f4red,nd_psn); fflush(asir_out);
1.33      noro     10999:       }
                   11000:       f4red++;
1.28      noro     11001:     }
                   11002:   }
                   11003:   if ( DP_Print ) {
1.34      noro     11004:     fprintf(asir_out,"\nnumber of red=%d,",Nf4_red);
1.28      noro     11005:   }
1.30      noro     11006:   g = conv_ilist_s(nd_demand,0,indp);
1.28      noro     11007:   return g;
                   11008: }

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