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

1.45    ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.44 2020/12/15 07:40:09 noro Exp $ */
1.1       noro        2:
                      3: #include "nd.h"
                      4:
1.45    ! noro        5: void print_siglist(NODE l);
        !             6:
1.41      noro        7: int Nnd_add,Nf4_red,NcriB,NcriMF,Ncri2,Npairs;
1.12      noro        8: struct oEGT eg_search,f4_symb,f4_conv,f4_elim1,f4_elim2;
1.1       noro        9:
                     10: int diag_period = 6;
                     11: int weight_check = 1;
                     12: int (*ndl_compare_function)(UINT *a1,UINT *a2);
1.21      noro       13: /* for general module order */
1.19      noro       14: int (*ndl_base_compare_function)(UINT *a1,UINT *a2);
1.21      noro       15: int (*dl_base_compare_function)(int nv,DL a,DL b);
                     16: int nd_base_ordtype;
1.1       noro       17: int nd_dcomp;
                     18: int nd_rref2;
                     19: NM _nm_free_list;
                     20: ND _nd_free_list;
                     21: ND_pairs _ndp_free_list;
                     22: NODE nd_hcf;
1.35      noro       23: int Nsyz,Nsamesig;
1.1       noro       24:
                     25: Obj nd_top_weight;
                     26:
                     27: static NODE nd_subst;
                     28: static VL nd_vc;
                     29: static int nd_ntrans;
                     30: static int nd_nalg;
                     31: #if 0
                     32: static int ndv_alloc;
                     33: #endif
                     34: #if 1
                     35: static int nd_f4_nsp=0x7fffffff;
                     36: #else
                     37: static int nd_f4_nsp=50;
                     38: #endif
                     39: static double nd_scale=2;
                     40: static UINT **nd_bound;
                     41: static struct order_spec *nd_ord;
                     42: static EPOS nd_epos;
                     43: static BlockMask nd_blockmask;
                     44: static int nd_nvar;
                     45: static int nd_isrlex;
                     46: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
                     47: static UINT nd_mask[32];
                     48: static UINT nd_mask0,nd_mask1;
                     49:
                     50: static NDV *nd_ps;
                     51: static NDV *nd_ps_trace;
                     52: static NDV *nd_ps_sym;
                     53: static NDV *nd_ps_trace_sym;
                     54: static RHist *nd_psh;
1.30      noro       55: static int nd_psn,nd_pslen,nd_nbase;
1.1       noro       56: static RHist *nd_red;
                     57: static int *nd_work_vector;
                     58: static int **nd_matrix;
                     59: static int nd_matrix_len;
                     60: static struct weight_or_block *nd_worb;
                     61: static int nd_worb_len;
                     62: static int nd_found,nd_create,nd_notfirst;
                     63: static int nmv_adv;
                     64: static int nd_demand;
1.21      noro       65: static int nd_module,nd_module_ordtype,nd_mpos,nd_pot_nelim;
1.1       noro       66: static int nd_module_rank,nd_poly_weight_len;
                     67: static int *nd_poly_weight,*nd_module_weight;
                     68: static NODE nd_tracelist;
                     69: static NODE nd_alltracelist;
1.41      noro       70: static int nd_gentrace,nd_gensyz,nd_nora,nd_newelim,nd_intersect,nd_lf,nd_norb;
                     71: static int nd_f4_td,nd_sba_f4step,nd_sba_pot,nd_sba_largelcm,nd_sba_dontsort,nd_sba_redundant_check;
1.45    ! noro       72: static int nd_top,nd_sba_syz,nd_sba_fixord,nd_sba_grevlexgb;
1.1       noro       73: static int *nd_gbblock;
                     74: static NODE nd_nzlist,nd_check_splist;
                     75: static int nd_splist;
                     76: static int *nd_sugarweight;
                     77: static int nd_f4red,nd_rank0,nd_last_nonzero;
1.27      noro       78: static DL *nd_sba_hm;
1.29      noro       79: static NODE *nd_sba_pos;
1.1       noro       80:
                     81: NumberField get_numberfield();
                     82: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
                     83: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
                     84: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
                     85: int nd_monic(int m,ND *p);
                     86: NDV plain_vect_to_ndv_q(Z *mat,int col,UINT *s0vect);
                     87: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank);
                     88: NDV pltondv(VL vl,VL dvl,LIST p);
                     89: void pltozpl(LIST l,Q *cont,LIST *pp);
                     90: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
                     91: void nmtodp(int mod,NM m,DP *r);
1.15      noro       92: void ndltodp(UINT *d,DP *r);
1.1       noro       93: NODE reverse_node(NODE n);
                     94: P ndc_div(int mod,union oNDC a,union oNDC b);
                     95: P ndctop(int mod,union oNDC c);
                     96: void finalize_tracelist(int i,P cont);
                     97: void conv_ilist(int demand,int trace,NODE g,int **indp);
                     98: void parse_nd_option(NODE opt);
                     99: void dltondl(int n,DL dl,UINT *r);
                    100: DP ndvtodp(int mod,NDV p);
                    101: DP ndtodp(int mod,ND p);
1.16      noro      102: DPM ndvtodpm(int mod,NDV p);
1.43      noro      103: NDV dptondv(int mod,DP p);
1.16      noro      104: NDV dpmtondv(int mod,DPM p);
1.43      noro      105: int dp_getdeg(DP p);
1.16      noro      106: int dpm_getdeg(DPM p,int *rank);
                    107: void dpm_ptozp(DPM p,Z *cont,DPM *r);
                    108: int compdmm(int nv,DMM a,DMM b);
1.44      noro      109: DPM sigtodpm(SIG s);
                    110: SIG dup_sig(SIG sig);
1.1       noro      111:
                    112: void Pdp_set_weight(NODE,VECT *);
                    113: void Pox_cmo_rpc(NODE,Obj *);
                    114:
                    115: ND nd_add_lf(ND p1,ND p2);
                    116: void nd_mul_c_lf(ND p,Z mul);
                    117: void ndv_mul_c_lf(NDV p,Z mul);
                    118: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                    119:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
                    120: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                    121:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
                    122: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                    123:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
                    124: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
1.40      noro      125: int nd_gauss_elim_mod_s(UINT **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat,SIG *sig);
1.1       noro      126: NODE nd_f4_lf_trace_main(int m,int **indp);
                    127: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
                    128:
                    129: extern int lf_lazy;
                    130: extern Z current_mod_lf;
                    131:
                    132: extern int Denominator,DP_Multiple,MaxDeg;
                    133:
                    134: #define BLEN (8*sizeof(unsigned long))
                    135:
                    136: typedef struct matrix {
                    137:   int row,col;
                    138:   unsigned long **a;
                    139: } *matrix;
                    140:
                    141:
                    142: void nd_free_private_storage()
                    143: {
                    144:     _nm_free_list = 0;
                    145:     _ndp_free_list = 0;
                    146: #if 0
                    147:     GC_gcollect();
                    148: #endif
                    149: }
                    150:
                    151: void _NM_alloc()
                    152: {
                    153:     NM p;
                    154:     int i;
                    155:
                    156:     for ( i = 0; i < 1024; i++ ) {
                    157:         p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                    158:         p->next = _nm_free_list; _nm_free_list = p;
                    159:     }
                    160: }
                    161:
                    162: matrix alloc_matrix(int row,int col)
                    163: {
                    164:   unsigned long **a;
                    165:   int i,len,blen;
                    166:   matrix mat;
                    167:
                    168:   mat = (matrix)MALLOC(sizeof(struct matrix));
                    169:   mat->row = row;
                    170:   mat->col = col;
                    171:   mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
                    172:   return mat;
                    173: }
                    174:
                    175:
                    176: void _ND_alloc()
                    177: {
                    178:     ND p;
                    179:     int i;
                    180:
                    181:     for ( i = 0; i < 1024; i++ ) {
                    182:         p = (ND)MALLOC(sizeof(struct oND));
                    183:         p->body = (NM)_nd_free_list; _nd_free_list = p;
                    184:     }
                    185: }
                    186:
                    187: void _NDP_alloc()
                    188: {
                    189:     ND_pairs p;
                    190:     int i;
                    191:
                    192:     for ( i = 0; i < 1024; i++ ) {
                    193:         p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
                    194:             +(nd_wpd-1)*sizeof(UINT));
                    195:         p->next = _ndp_free_list; _ndp_free_list = p;
                    196:     }
                    197: }
                    198:
                    199: INLINE int nd_length(ND p)
                    200: {
                    201:     NM m;
                    202:     int i;
                    203:
                    204:     if ( !p )
                    205:         return 0;
                    206:     else {
                    207:         for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
                    208:         return i;
                    209:     }
                    210: }
                    211:
                    212: extern int dp_negative_weight;
                    213:
                    214: INLINE int ndl_reducible(UINT *d1,UINT *d2)
                    215: {
                    216:     UINT u1,u2;
                    217:     int i,j;
                    218:
                    219:     if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
                    220:
                    221:     if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
                    222: #if USE_UNROLL
                    223:     switch ( nd_bpe ) {
                    224:         case 3:
                    225:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    226:                 u1 = d1[i]; u2 = d2[i];
                    227:                 if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
                    228:                 if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
                    229:                 if ( (u1&  0xe00000) < (u2&  0xe00000) ) return 0;
                    230:                 if ( (u1&  0x1c0000) < (u2&  0x1c0000) ) return 0;
                    231:                 if ( (u1&   0x38000) < (u2&   0x38000) ) return 0;
                    232:                 if ( (u1&    0x7000) < (u2&    0x7000) ) return 0;
                    233:                 if ( (u1&     0xe00) < (u2&     0xe00) ) return 0;
                    234:                 if ( (u1&     0x1c0) < (u2&     0x1c0) ) return 0;
                    235:                 if ( (u1&      0x38) < (u2&      0x38) ) return 0;
                    236:                 if ( (u1&       0x7) < (u2&       0x7) ) return 0;
                    237:             }
                    238:             return 1;
                    239:             break;
                    240:         case 4:
                    241:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    242:                 u1 = d1[i]; u2 = d2[i];
                    243:                 if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
                    244:                 if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
                    245:                 if ( (u1&  0xf00000) < (u2&  0xf00000) ) return 0;
                    246:                 if ( (u1&   0xf0000) < (u2&   0xf0000) ) return 0;
                    247:                 if ( (u1&    0xf000) < (u2&    0xf000) ) return 0;
                    248:                 if ( (u1&     0xf00) < (u2&     0xf00) ) return 0;
                    249:                 if ( (u1&      0xf0) < (u2&      0xf0) ) return 0;
                    250:                 if ( (u1&       0xf) < (u2&       0xf) ) return 0;
                    251:             }
                    252:             return 1;
                    253:             break;
                    254:         case 6:
                    255:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    256:                 u1 = d1[i]; u2 = d2[i];
                    257:                 if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
                    258:                 if ( (u1&  0xfc0000) < (u2&  0xfc0000) ) return 0;
                    259:                 if ( (u1&   0x3f000) < (u2&   0x3f000) ) return 0;
                    260:                 if ( (u1&     0xfc0) < (u2&     0xfc0) ) return 0;
                    261:                 if ( (u1&      0x3f) < (u2&      0x3f) ) return 0;
                    262:             }
                    263:             return 1;
                    264:             break;
                    265:         case 8:
                    266:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    267:                 u1 = d1[i]; u2 = d2[i];
                    268:                 if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
                    269:                 if ( (u1&  0xff0000) < (u2&  0xff0000) ) return 0;
                    270:                 if ( (u1&    0xff00) < (u2&    0xff00) ) return 0;
                    271:                 if ( (u1&      0xff) < (u2&      0xff) ) return 0;
                    272:             }
                    273:             return 1;
                    274:             break;
                    275:         case 16:
                    276:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    277:                 u1 = d1[i]; u2 = d2[i];
                    278:                 if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
                    279:                 if ( (u1&    0xffff) < (u2&    0xffff) ) return 0;
                    280:             }
                    281:             return 1;
                    282:             break;
                    283:         case 32:
                    284:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                    285:                 if ( d1[i] < d2[i] ) return 0;
                    286:             return 1;
                    287:             break;
                    288:         default:
                    289:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    290:                 u1 = d1[i]; u2 = d2[i];
                    291:                 for ( j = 0; j < nd_epw; j++ )
                    292:                     if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    293:             }
                    294:             return 1;
                    295:     }
                    296: #else
                    297:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    298:         u1 = d1[i]; u2 = d2[i];
                    299:         for ( j = 0; j < nd_epw; j++ )
                    300:             if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    301:     }
                    302:     return 1;
                    303: #endif
                    304: }
                    305:
                    306: /*
                    307:  * If the current order is a block order,
                    308:  * then the last block is length 1 and contains
                    309:  * the homo variable. Otherwise, the original
                    310:  * order is either 0 or 2.
                    311:  */
                    312:
                    313: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
                    314: {
                    315:     int w,i,e,n,omask0;
                    316:
                    317:     omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
                    318:     n = nd_nvar-1;
                    319:     ndl_zero(r);
                    320:     for ( i = 0; i < n; i++ ) {
                    321:         e = GET_EXP_OLD(d,i);
                    322:         PUT_EXP(r,i,e);
                    323:     }
                    324:     w = TD(d);
                    325:     PUT_EXP(r,nd_nvar-1,weight-w);
                    326:     if ( nd_module ) MPOS(r) = d[ompos];
                    327:     TD(r) = weight;
                    328:     if ( nd_blockmask ) ndl_weight_mask(r);
                    329: }
                    330:
                    331: void ndl_dehomogenize(UINT *d)
                    332: {
                    333:     UINT mask;
                    334:     UINT h;
                    335:     int i,bits;
                    336:
                    337:     if ( nd_blockmask ) {
                    338:         h = GET_EXP(d,nd_nvar-1);
                    339:         XOR_EXP(d,nd_nvar-1,h);
                    340:         TD(d) -= h;
                    341:         ndl_weight_mask(d);
                    342:     } else {
                    343:         if ( nd_isrlex ) {
                    344:             if ( nd_bpe == 32 ) {
                    345:                 h = d[nd_exporigin];
                    346:                 for ( i = nd_exporigin+1; i < nd_wpd; i++ )
                    347:                     d[i-1] = d[i];
                    348:                 d[i-1] = 0;
                    349:                 TD(d) -= h;
                    350:             } else {
                    351:                 bits = nd_epw*nd_bpe;
                    352:                 mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
                    353:                 h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
                    354:                 for ( i = nd_exporigin; i < nd_wpd; i++ )
                    355:                     d[i] = ((d[i]<<nd_bpe)&mask)
                    356:                         |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
                    357:                 TD(d) -= h;
                    358:             }
                    359:         } else {
                    360:             h = GET_EXP(d,nd_nvar-1);
                    361:             XOR_EXP(d,nd_nvar-1,h);
                    362:             TD(d) -= h;
                    363:         }
                    364:     }
                    365: }
                    366:
                    367: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
                    368: {
                    369:     UINT t1,t2,u,u1,u2;
                    370:     int i,j,l;
                    371:
                    372:     if ( nd_module && (MPOS(d1) != MPOS(d2)) )
                    373:         error("ndl_lcm : inconsistent monomials");
                    374: #if USE_UNROLL
                    375:     switch ( nd_bpe ) {
                    376:         case 3:
                    377:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    378:                 u1 = d1[i]; u2 = d2[i];
                    379:                 t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
                    380:                 t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
                    381:                 t1 = (u1&  0xe00000); t2 = (u2&  0xe00000); u |= t1>t2?t1:t2;
                    382:                 t1 = (u1&  0x1c0000); t2 = (u2&  0x1c0000); u |= t1>t2?t1:t2;
                    383:                 t1 = (u1&   0x38000); t2 = (u2&   0x38000); u |= t1>t2?t1:t2;
                    384:                 t1 = (u1&    0x7000); t2 = (u2&    0x7000); u |= t1>t2?t1:t2;
                    385:                 t1 = (u1&     0xe00); t2 = (u2&     0xe00); u |= t1>t2?t1:t2;
                    386:                 t1 = (u1&     0x1c0); t2 = (u2&     0x1c0); u |= t1>t2?t1:t2;
                    387:                 t1 = (u1&      0x38); t2 = (u2&      0x38); u |= t1>t2?t1:t2;
                    388:                 t1 = (u1&       0x7); t2 = (u2&       0x7); u |= t1>t2?t1:t2;
                    389:                 d[i] = u;
                    390:             }
                    391:             break;
                    392:         case 4:
                    393:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    394:                 u1 = d1[i]; u2 = d2[i];
                    395:                 t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
                    396:                 t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
                    397:                 t1 = (u1&  0xf00000); t2 = (u2&  0xf00000); u |= t1>t2?t1:t2;
                    398:                 t1 = (u1&   0xf0000); t2 = (u2&   0xf0000); u |= t1>t2?t1:t2;
                    399:                 t1 = (u1&    0xf000); t2 = (u2&    0xf000); u |= t1>t2?t1:t2;
                    400:                 t1 = (u1&     0xf00); t2 = (u2&     0xf00); u |= t1>t2?t1:t2;
                    401:                 t1 = (u1&      0xf0); t2 = (u2&      0xf0); u |= t1>t2?t1:t2;
                    402:                 t1 = (u1&       0xf); t2 = (u2&       0xf); u |= t1>t2?t1:t2;
                    403:                 d[i] = u;
                    404:             }
                    405:             break;
                    406:         case 6:
                    407:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    408:                 u1 = d1[i]; u2 = d2[i];
                    409:                 t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
                    410:                 t1 = (u1&  0xfc0000); t2 = (u2&  0xfc0000); u |= t1>t2?t1:t2;
                    411:                 t1 = (u1&   0x3f000); t2 = (u2&   0x3f000); u |= t1>t2?t1:t2;
                    412:                 t1 = (u1&     0xfc0); t2 = (u2&     0xfc0); u |= t1>t2?t1:t2;
                    413:                 t1 = (u1&      0x3f); t2 = (u2&      0x3f); u |= t1>t2?t1:t2;
                    414:                 d[i] = u;
                    415:             }
                    416:             break;
                    417:         case 8:
                    418:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    419:                 u1 = d1[i]; u2 = d2[i];
                    420:                 t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
                    421:                 t1 = (u1&  0xff0000); t2 = (u2&  0xff0000); u |= t1>t2?t1:t2;
                    422:                 t1 = (u1&    0xff00); t2 = (u2&    0xff00); u |= t1>t2?t1:t2;
                    423:                 t1 = (u1&      0xff); t2 = (u2&      0xff); u |= t1>t2?t1:t2;
                    424:                 d[i] = u;
                    425:             }
                    426:             break;
                    427:         case 16:
                    428:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    429:                 u1 = d1[i]; u2 = d2[i];
                    430:                 t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
                    431:                 t1 = (u1&    0xffff); t2 = (u2&    0xffff); u |= t1>t2?t1:t2;
                    432:                 d[i] = u;
                    433:             }
                    434:             break;
                    435:         case 32:
                    436:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    437:                 u1 = d1[i]; u2 = d2[i];
                    438:                 d[i] = u1>u2?u1:u2;
                    439:             }
                    440:             break;
                    441:         default:
                    442:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    443:                 u1 = d1[i]; u2 = d2[i];
                    444:                 for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    445:                     t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    446:                 }
                    447:                 d[i] = u;
                    448:             }
                    449:             break;
                    450:     }
                    451: #else
                    452:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    453:         u1 = d1[i]; u2 = d2[i];
                    454:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    455:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    456:         }
                    457:         d[i] = u;
                    458:     }
                    459: #endif
                    460:     if ( nd_module ) MPOS(d) = MPOS(d1);
                    461:     TD(d) = ndl_weight(d);
                    462:     if ( nd_blockmask ) ndl_weight_mask(d);
                    463: }
                    464:
                    465: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
                    466: {
                    467:     UINT t1,t2,u,u1,u2;
                    468:     int i,j,l;
                    469:
                    470:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    471:         u1 = d1[i]; u2 = d2[i];
                    472:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    473:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    474:         }
                    475:         d[i] = u;
                    476:     }
                    477: }
                    478:
                    479: int ndl_weight(UINT *d)
                    480: {
                    481:     UINT t,u;
                    482:     int i,j;
                    483:
                    484:     if ( current_dl_weight_vector )
                    485:         for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    486:             u = GET_EXP(d,i);
                    487:             t += MUL_WEIGHT(u,i);
                    488:         }
                    489:     else
                    490:         for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    491:             u = d[i];
                    492:             for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    493:                 t += (u&nd_mask0);
                    494:         }
1.20      noro      495:     if ( nd_module && nd_module_rank && MPOS(d) )
                    496:         t += nd_module_weight[MPOS(d)-1];
                    497:     for ( i = nd_exporigin; i < nd_wpd; i++ )
                    498:       if ( d[i] && !t )
                    499:         printf("afo\n");
1.1       noro      500:     return t;
                    501: }
                    502:
                    503: /* for sugarweight */
                    504:
                    505: int ndl_weight2(UINT *d)
                    506: {
                    507:     int t,u;
                    508:     int i,j;
                    509:
                    510:     for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    511:         u = GET_EXP(d,i);
                    512:         t += nd_sugarweight[i]*u;
                    513:     }
1.20      noro      514:     if ( nd_module && nd_module_rank && MPOS(d) )
                    515:         t += nd_module_weight[MPOS(d)-1];
1.1       noro      516:     return t;
                    517: }
                    518:
                    519: void ndl_weight_mask(UINT *d)
                    520: {
                    521:     UINT t,u;
                    522:     UINT *mask;
                    523:     int i,j,k,l;
                    524:
                    525:     l = nd_blockmask->n;
                    526:     for ( k = 0; k < l; k++ ) {
                    527:         mask = nd_blockmask->mask[k];
                    528:         if ( current_dl_weight_vector )
                    529:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    530:                 u = GET_EXP_MASK(d,i,mask);
                    531:                 t += MUL_WEIGHT(u,i);
                    532:             }
                    533:         else
                    534:             for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    535:                 u = d[i]&mask[i];
                    536:                 for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    537:                     t += (u&nd_mask0);
                    538:             }
                    539:         d[k+1] = t;
                    540:     }
                    541: }
                    542:
1.21      noro      543: int ndl_glex_compare(UINT *d1,UINT *d2)
                    544: {
                    545:   if ( TD(d1) > TD(d2) ) return 1;
                    546:   else if ( TD(d1) < TD(d2) ) return -1;
                    547:   else return ndl_lex_compare(d1,d2);
                    548: }
                    549:
1.1       noro      550: int ndl_lex_compare(UINT *d1,UINT *d2)
                    551: {
                    552:     int i;
                    553:
                    554:     d1 += nd_exporigin;
                    555:     d2 += nd_exporigin;
                    556:     for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
                    557:         if ( *d1 > *d2 )
                    558:             return nd_isrlex ? -1 : 1;
                    559:         else if ( *d1 < *d2 )
                    560:             return nd_isrlex ? 1 : -1;
                    561:     return 0;
                    562: }
                    563:
                    564: int ndl_block_compare(UINT *d1,UINT *d2)
                    565: {
                    566:     int i,l,j,ord_o,ord_l;
                    567:     struct order_pair *op;
                    568:     UINT t1,t2,m;
                    569:     UINT *mask;
                    570:
                    571:     l = nd_blockmask->n;
                    572:     op = nd_blockmask->order_pair;
                    573:     for ( j = 0; j < l; j++ ) {
                    574:         mask = nd_blockmask->mask[j];
                    575:         ord_o = op[j].order;
                    576:         if ( ord_o < 2 ) {
                    577:             if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
                    578:             else if ( t1 < t2 ) return -1;
                    579:         }
                    580:         for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    581:             m = mask[i];
                    582:             t1 = d1[i]&m;
                    583:             t2 = d2[i]&m;
                    584:             if ( t1 > t2 )
                    585:                 return !ord_o ? -1 : 1;
                    586:             else if ( t1 < t2 )
                    587:                 return !ord_o ? 1 : -1;
                    588:         }
                    589:     }
                    590:     return 0;
                    591: }
                    592:
                    593: int ndl_matrix_compare(UINT *d1,UINT *d2)
                    594: {
                    595:   int i,j,s,row;
                    596:   int *v;
                    597:   Z **mat;
                    598:   Z *w;
                    599:   Z t1;
                    600:   Z t,t2;
                    601:
1.6       noro      602:   for ( j = 0; j < nd_nvar; j++ )
                    603:     nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.1       noro      604:   if ( nd_top_weight ) {
                    605:     if ( OID(nd_top_weight) == O_VECT ) {
1.6       noro      606:       mat = (Z **)&BDY((VECT)nd_top_weight);
                    607:       row = 1;
1.1       noro      608:     } else {
                    609:       mat = (Z **)BDY((MAT)nd_top_weight);
1.6       noro      610:       row = ((MAT)nd_top_weight)->row;
1.1       noro      611:     }
                    612:     for ( i = 0; i < row; i++ ) {
1.6       noro      613:       w = mat[i];
1.1       noro      614:       for ( j = 0, t = 0; j < nd_nvar; j++ ) {
1.6       noro      615:         STOZ(nd_work_vector[j],t1);
1.1       noro      616:         mulz(w[j],t1,&t2);
                    617:         addz(t,t2,&t1);
                    618:         t = t1;
                    619:       }
1.6       noro      620:       if ( t ) {
                    621:         s = sgnz(t);
1.1       noro      622:         if ( s > 0 ) return 1;
                    623:         else if ( s < 0 ) return -1;
1.6       noro      624:       }
1.1       noro      625:     }
1.6       noro      626:   }
                    627:   for ( i = 0; i < nd_matrix_len; i++ ) {
                    628:     v = nd_matrix[i];
                    629:     for ( j = 0, s = 0; j < nd_nvar; j++ )
                    630:       s += v[j]*nd_work_vector[j];
                    631:     if ( s > 0 ) return 1;
                    632:     else if ( s < 0 ) return -1;
                    633:   }
1.1       noro      634:   if ( !ndl_equal(d1,d2) )
1.6       noro      635:     error("ndl_matrix_compare : invalid matrix");
                    636:   return 0;
1.1       noro      637: }
                    638:
                    639: int ndl_composite_compare(UINT *d1,UINT *d2)
                    640: {
                    641:     int i,j,s,start,end,len,o;
                    642:     int *v;
                    643:     struct sparse_weight *sw;
                    644:
                    645:     for ( j = 0; j < nd_nvar; j++ )
                    646:         nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
                    647:     for ( i = 0; i < nd_worb_len; i++ ) {
                    648:         len = nd_worb[i].length;
                    649:         switch ( nd_worb[i].type ) {
                    650:             case IS_DENSE_WEIGHT:
                    651:                 v = nd_worb[i].body.dense_weight;
                    652:                 for ( j = 0, s = 0; j < len; j++ )
                    653:                     s += v[j]*nd_work_vector[j];
                    654:                 if ( s > 0 ) return 1;
                    655:                 else if ( s < 0 ) return -1;
                    656:                 break;
                    657:             case IS_SPARSE_WEIGHT:
                    658:                 sw = nd_worb[i].body.sparse_weight;
                    659:                 for ( j = 0, s = 0; j < len; j++ )
                    660:                     s += sw[j].value*nd_work_vector[sw[j].pos];
                    661:                 if ( s > 0 ) return 1;
                    662:                 else if ( s < 0 ) return -1;
                    663:                 break;
                    664:             case IS_BLOCK:
                    665:                 o = nd_worb[i].body.block.order;
                    666:                 start = nd_worb[i].body.block.start;
                    667:                 switch ( o ) {
                    668:                     case 0:
                    669:                         end = start+len;
                    670:                         for ( j = start, s = 0; j < end; j++ )
                    671:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    672:                         if ( s > 0 ) return 1;
                    673:                         else if ( s < 0 ) return -1;
                    674:                         for ( j = end-1; j >= start; j-- )
                    675:                             if ( nd_work_vector[j] < 0 ) return 1;
                    676:                             else if ( nd_work_vector[j] > 0 ) return -1;
                    677:                         break;
                    678:                     case 1:
                    679:                         end = start+len;
                    680:                         for ( j = start, s = 0; j < end; j++ )
                    681:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    682:                         if ( s > 0 ) return 1;
                    683:                         else if ( s < 0 ) return -1;
                    684:                         for ( j = start; j < end; j++ )
                    685:                             if ( nd_work_vector[j] > 0 ) return 1;
                    686:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    687:                         break;
                    688:                     case 2:
                    689:                         end = start+len;
                    690:                         for ( j = start; j < end; j++ )
                    691:                             if ( nd_work_vector[j] > 0 ) return 1;
                    692:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    693:                         break;
                    694:                 }
                    695:                 break;
                    696:         }
                    697:     }
                    698:     return 0;
                    699: }
                    700:
                    701: /* TDH -> WW -> TD-> RL */
                    702:
                    703: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
                    704: {
                    705:     int i,m,e1,e2;
                    706:
                    707:     if ( TD(d1) > TD(d2) ) return 1;
                    708:     else if ( TD(d1) < TD(d2) ) return -1;
                    709:     m = nd_nvar>>1;
                    710:     for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
                    711:         e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
                    712:         e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
                    713:     }
                    714:     if ( e1 > e2 ) return 1;
                    715:     else if ( e1 < e2 ) return -1;
                    716:     return ndl_lex_compare(d1,d2);
                    717: }
                    718:
1.21      noro      719: // common function for module glex and grlex comparison
                    720: int ndl_module_glex_compare(UINT *d1,UINT *d2)
1.1       noro      721: {
1.21      noro      722:   int c;
1.1       noro      723:
1.21      noro      724:   switch ( nd_module_ordtype ) {
                    725:     case 0:
                    726:       if ( TD(d1) > TD(d2) ) return 1;
                    727:       else if ( TD(d1) < TD(d2) ) return -1;
                    728:       else if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
                    729:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    730:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    731:       else return 0;
                    732:       break;
1.1       noro      733:
1.21      noro      734:     case 1:
1.19      noro      735:       if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
                    736:          if ( TD(d1) > TD(d2) ) return 1;
                    737:          else if ( TD(d1) < TD(d2) ) return -1;
                    738:          if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
                    739:          if ( MPOS(d1) < MPOS(d2) ) return 1;
                    740:          else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    741:       }
                    742:       if ( MPOS(d1) < MPOS(d2) ) return 1;
                    743:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
1.21      noro      744:       else if ( TD(d1) > TD(d2) ) return 1;
                    745:       else if ( TD(d1) < TD(d2) ) return -1;
                    746:       else return ndl_lex_compare(d1,d2);
                    747:       break;
1.1       noro      748:
1.21      noro      749:     case 2: // weight -> POT
                    750:       if ( TD(d1) > TD(d2) ) return 1;
                    751:       else if ( TD(d1) < TD(d2) ) return -1;
                    752:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    753:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    754:       else return ndl_lex_compare(d1,d2);
                    755:       break;
1.1       noro      756:
1.21      noro      757:     default:
                    758:       error("ndl_module_glex_compare : invalid module_ordtype");
1.28      noro      759:       return 0;
1.21      noro      760:   }
1.1       noro      761: }
                    762:
1.21      noro      763: // common  for module comparison
                    764: int ndl_module_compare(UINT *d1,UINT *d2)
1.1       noro      765: {
1.21      noro      766:   int c;
1.1       noro      767:
1.21      noro      768:   switch ( nd_module_ordtype ) {
                    769:     case 0:
1.23      noro      770:       if ( (c = (*ndl_base_compare_function)(d1,d2)) != 0 ) return c;
1.21      noro      771:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    772:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    773:       else return 0;
                    774:       break;
1.1       noro      775:
1.21      noro      776:     case 1:
                    777:       if ( MPOS(d1) < MPOS(d2) ) return 1;
                    778:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    779:       else return (*ndl_base_compare_function)(d1,d2);
                    780:       break;
1.1       noro      781:
1.21      noro      782:     case 2: // weight -> POT
                    783:       if ( TD(d1) > TD(d2) ) return 1;
                    784:       else if ( TD(d1) < TD(d2) ) return -1;
                    785:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    786:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    787:       else return (*ndl_base_compare_function)(d1,d2);
                    788:       break;
1.1       noro      789:
1.21      noro      790:     default:
                    791:       error("ndl_module_compare : invalid module_ordtype");
1.28      noro      792:       return 0;
1.21      noro      793:   }
1.1       noro      794: }
                    795:
1.21      noro      796: extern DMMstack dmm_stack;
                    797: void _addtodl(int n,DL d1,DL d2);
1.31      noro      798: void _adddl(int n,DL d1,DL d2,DL d3);
1.21      noro      799: int _eqdl(int n,DL d1,DL d2);
                    800:
                    801: int ndl_module_schreyer_compare(UINT *m1,UINT *m2)
                    802: {
1.44      noro      803:   int pos1,pos2,t,j,retpot;
1.21      noro      804:   DMM *in;
                    805:   DMMstack s;
                    806:   static DL d1=0;
                    807:   static DL d2=0;
                    808:   static int dlen=0;
1.44      noro      809:   extern int ReversePOT;
1.21      noro      810:
1.44      noro      811:   if ( ReversePOT ) retpot = -1;
                    812:   else retpot = 1;
1.21      noro      813:   pos1 = MPOS(m1); pos2 = MPOS(m2);
                    814:   if ( pos1 == pos2 ) return (*ndl_base_compare_function)(m1,m2);
                    815:   if ( nd_nvar > dlen ) {
                    816:     NEWDL(d1,nd_nvar);
                    817:     NEWDL(d2,nd_nvar);
                    818:     dlen = nd_nvar;
                    819:   }
                    820:   d1->td = TD(m1);
                    821:   for ( j = 0; j < nd_nvar; j++ ) d1->d[j] = GET_EXP(m1,j);
                    822:   d2->td = TD(m2);
                    823:   for ( j = 0; j < nd_nvar; j++ ) d2->d[j] = GET_EXP(m2,j);
                    824:   for ( s = dmm_stack; s; s = NEXT(s) ) {
                    825:     in = s->in;
                    826:     _addtodl(nd_nvar,in[pos1]->dl,d1);
                    827:     _addtodl(nd_nvar,in[pos2]->dl,d2);
                    828:     if ( in[pos1]->pos == in[pos2]->pos && _eqdl(nd_nvar,d1,d2)) {
1.44      noro      829:       if ( pos1 < pos2 ) return retpot;
                    830:       else if ( pos1 > pos2 ) return -retpot;
1.21      noro      831:       else return 0;
                    832:     }
                    833:     pos1 = in[pos1]->pos;
                    834:     pos2 = in[pos2]->pos;
                    835:     if ( pos1 == pos2 ) return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    836:   }
                    837:   // comparison by the bottom order
                    838: LAST:
                    839:   switch ( nd_base_ordtype ) {
                    840:     case 0:
                    841:       t = (*dl_base_compare_function)(nd_nvar,d1,d2);
                    842:       if ( t ) return t;
1.44      noro      843:       else if ( pos1 < pos2 ) return retpot;
                    844:       else if ( pos1 > pos2 ) return -retpot;
1.21      noro      845:       else return 0;
                    846:       break;
                    847:     case 1:
1.44      noro      848:       if ( pos1 < pos2 ) return retpot;
                    849:       else if ( pos1 > pos2 ) return -retpot;
1.21      noro      850:       else return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    851:       break;
                    852:     case 2:
                    853:       if ( d1->td > d2->td  ) return 1;
                    854:       else if ( d1->td < d2->td ) return -1;
1.44      noro      855:       else if ( pos1 < pos2 ) return retpot;
                    856:       else if ( pos1 > pos2 ) return -retpot;
1.21      noro      857:       else return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    858:       break;
                    859:     default:
                    860:       error("ndl_schreyer_compare : invalid base ordtype");
1.28      noro      861:       return 0;
1.21      noro      862:   }
1.1       noro      863: }
                    864:
                    865: INLINE int ndl_equal(UINT *d1,UINT *d2)
                    866: {
                    867:     int i;
                    868:
                    869:     switch ( nd_wpd ) {
                    870:         case 2:
                    871:             if ( TD(d2) != TD(d1) ) return 0;
                    872:             if ( d2[1] != d1[1] ) return 0;
                    873:             return 1;
                    874:             break;
                    875:         case 3:
                    876:             if ( TD(d2) != TD(d1) ) return 0;
                    877:             if ( d2[1] != d1[1] ) return 0;
                    878:             if ( d2[2] != d1[2] ) return 0;
                    879:             return 1;
                    880:             break;
                    881:         default:
                    882:             for ( i = 0; i < nd_wpd; i++ )
                    883:                 if ( *d1++ != *d2++ ) return 0;
                    884:             return 1;
                    885:             break;
                    886:     }
                    887: }
                    888:
                    889: INLINE void ndl_copy(UINT *d1,UINT *d2)
                    890: {
                    891:     int i;
                    892:
                    893:     switch ( nd_wpd ) {
                    894:         case 2:
                    895:             TD(d2) = TD(d1);
                    896:             d2[1] = d1[1];
                    897:             break;
                    898:         case 3:
                    899:             TD(d2) = TD(d1);
                    900:             d2[1] = d1[1];
                    901:             d2[2] = d1[2];
                    902:             break;
                    903:         default:
                    904:             for ( i = 0; i < nd_wpd; i++ )
                    905:                 d2[i] = d1[i];
                    906:             break;
                    907:     }
                    908: }
                    909:
                    910: INLINE void ndl_zero(UINT *d)
                    911: {
                    912:     int i;
                    913:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                    914: }
                    915:
                    916: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
                    917: {
                    918:     int i;
                    919:
                    920:     if ( nd_module ) {
                    921:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
                    922:         error("ndl_add : invalid operation");
                    923:     }
                    924: #if 1
                    925:     switch ( nd_wpd ) {
                    926:         case 2:
                    927:             TD(d) = TD(d1)+TD(d2);
                    928:             d[1] = d1[1]+d2[1];
                    929:             break;
                    930:         case 3:
                    931:             TD(d) = TD(d1)+TD(d2);
                    932:             d[1] = d1[1]+d2[1];
                    933:             d[2] = d1[2]+d2[2];
                    934:             break;
                    935:         default:
                    936:             for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    937:             break;
                    938:     }
                    939: #else
                    940:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    941: #endif
                    942: }
                    943:
                    944: /* d1 += d2 */
                    945: INLINE void ndl_addto(UINT *d1,UINT *d2)
                    946: {
                    947:     int i;
                    948:
                    949:     if ( nd_module ) {
                    950:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
                    951:             error("ndl_addto : invalid operation");
                    952:     }
                    953: #if 1
                    954:     switch ( nd_wpd ) {
                    955:         case 2:
                    956:             TD(d1) += TD(d2);
                    957:             d1[1] += d2[1];
                    958:             break;
                    959:         case 3:
                    960:             TD(d1) += TD(d2);
                    961:             d1[1] += d2[1];
                    962:             d1[2] += d2[2];
                    963:             break;
                    964:         default:
                    965:             for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    966:             break;
                    967:     }
                    968: #else
                    969:     for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    970: #endif
                    971: }
                    972:
                    973: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
                    974: {
                    975:     int i;
                    976:
                    977:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
                    978: }
                    979:
                    980: int ndl_disjoint(UINT *d1,UINT *d2)
                    981: {
                    982:     UINT t1,t2,u,u1,u2;
                    983:     int i,j;
                    984:
                    985:     if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
                    986: #if USE_UNROLL
                    987:     switch ( nd_bpe ) {
                    988:         case 3:
                    989:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    990:                 u1 = d1[i]; u2 = d2[i];
                    991:                 t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
                    992:                 t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
                    993:                 t1 = u1&  0xe00000; t2 = u2&  0xe00000; if ( t1&&t2 ) return 0;
                    994:                 t1 = u1&  0x1c0000; t2 = u2&  0x1c0000; if ( t1&&t2 ) return 0;
                    995:                 t1 = u1&   0x38000; t2 = u2&   0x38000; if ( t1&&t2 ) return 0;
                    996:                 t1 = u1&    0x7000; t2 = u2&    0x7000; if ( t1&&t2 ) return 0;
                    997:                 t1 = u1&     0xe00; t2 = u2&     0xe00; if ( t1&&t2 ) return 0;
                    998:                 t1 = u1&     0x1c0; t2 = u2&     0x1c0; if ( t1&&t2 ) return 0;
                    999:                 t1 = u1&      0x38; t2 = u2&      0x38; if ( t1&&t2 ) return 0;
                   1000:                 t1 = u1&       0x7; t2 = u2&       0x7; if ( t1&&t2 ) return 0;
                   1001:             }
                   1002:             return 1;
                   1003:             break;
                   1004:         case 4:
                   1005:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1006:                 u1 = d1[i]; u2 = d2[i];
                   1007:                 t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
                   1008:                 t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
                   1009:                 t1 = u1&  0xf00000; t2 = u2&  0xf00000; if ( t1&&t2 ) return 0;
                   1010:                 t1 = u1&   0xf0000; t2 = u2&   0xf0000; if ( t1&&t2 ) return 0;
                   1011:                 t1 = u1&    0xf000; t2 = u2&    0xf000; if ( t1&&t2 ) return 0;
                   1012:                 t1 = u1&     0xf00; t2 = u2&     0xf00; if ( t1&&t2 ) return 0;
                   1013:                 t1 = u1&      0xf0; t2 = u2&      0xf0; if ( t1&&t2 ) return 0;
                   1014:                 t1 = u1&       0xf; t2 = u2&       0xf; if ( t1&&t2 ) return 0;
                   1015:             }
                   1016:             return 1;
                   1017:             break;
                   1018:         case 6:
                   1019:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1020:                 u1 = d1[i]; u2 = d2[i];
                   1021:                 t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
                   1022:                 t1 = u1&  0xfc0000; t2 = u2&  0xfc0000; if ( t1&&t2 ) return 0;
                   1023:                 t1 = u1&   0x3f000; t2 = u2&   0x3f000; if ( t1&&t2 ) return 0;
                   1024:                 t1 = u1&     0xfc0; t2 = u2&     0xfc0; if ( t1&&t2 ) return 0;
                   1025:                 t1 = u1&      0x3f; t2 = u2&      0x3f; if ( t1&&t2 ) return 0;
                   1026:             }
                   1027:             return 1;
                   1028:             break;
                   1029:         case 8:
                   1030:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1031:                 u1 = d1[i]; u2 = d2[i];
                   1032:                 t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
                   1033:                 t1 = u1&  0xff0000; t2 = u2&  0xff0000; if ( t1&&t2 ) return 0;
                   1034:                 t1 = u1&    0xff00; t2 = u2&    0xff00; if ( t1&&t2 ) return 0;
                   1035:                 t1 = u1&      0xff; t2 = u2&      0xff; if ( t1&&t2 ) return 0;
                   1036:             }
                   1037:             return 1;
                   1038:             break;
                   1039:         case 16:
                   1040:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1041:                 u1 = d1[i]; u2 = d2[i];
                   1042:                 t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
                   1043:                 t1 = u1&    0xffff; t2 = u2&    0xffff; if ( t1&&t2 ) return 0;
                   1044:             }
                   1045:             return 1;
                   1046:             break;
                   1047:         case 32:
                   1048:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                   1049:                 if ( d1[i] && d2[i] ) return 0;
                   1050:             return 1;
                   1051:             break;
                   1052:         default:
                   1053:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1054:                 u1 = d1[i]; u2 = d2[i];
                   1055:                 for ( j = 0; j < nd_epw; j++ ) {
                   1056:                     if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                   1057:                     u1 >>= nd_bpe; u2 >>= nd_bpe;
                   1058:                 }
                   1059:             }
                   1060:             return 1;
                   1061:             break;
                   1062:     }
                   1063: #else
                   1064:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1065:         u1 = d1[i]; u2 = d2[i];
                   1066:         for ( j = 0; j < nd_epw; j++ ) {
                   1067:             if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                   1068:             u1 >>= nd_bpe; u2 >>= nd_bpe;
                   1069:         }
                   1070:     }
                   1071:     return 1;
                   1072: #endif
                   1073: }
                   1074:
                   1075: int ndl_check_bound(UINT *d1,UINT *d2)
                   1076: {
                   1077:     UINT u2;
                   1078:     int i,j,ind,k;
                   1079:
                   1080:     ind = 0;
                   1081: #if USE_UNROLL
                   1082:     switch ( nd_bpe ) {
                   1083:         case 3:
                   1084:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1085:                 u2 = d2[i];
                   1086:                 if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
                   1087:                 if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
                   1088:                 if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
                   1089:                 if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
                   1090:                 if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
                   1091:                 if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
                   1092:                 if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
                   1093:                 if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
                   1094:                 if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
                   1095:                 if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
                   1096:             }
                   1097:             return 0;
                   1098:             break;
                   1099:         case 4:
                   1100:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1101:                 u2 = d2[i];
                   1102:                 if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
                   1103:                 if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
                   1104:                 if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
                   1105:                 if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
                   1106:                 if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
                   1107:                 if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
                   1108:                 if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
                   1109:                 if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
                   1110:             }
                   1111:             return 0;
                   1112:             break;
                   1113:         case 6:
                   1114:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1115:                 u2 = d2[i];
                   1116:                 if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
                   1117:                 if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
                   1118:                 if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
                   1119:                 if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
                   1120:                 if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
                   1121:             }
                   1122:             return 0;
                   1123:             break;
                   1124:         case 8:
                   1125:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1126:                 u2 = d2[i];
                   1127:                 if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
                   1128:                 if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
                   1129:                 if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
                   1130:                 if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
                   1131:             }
                   1132:             return 0;
                   1133:             break;
                   1134:         case 16:
                   1135:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1136:                 u2 = d2[i];
                   1137:                 if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
                   1138:                 if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
                   1139:             }
                   1140:             return 0;
                   1141:             break;
                   1142:         case 32:
                   1143:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                   1144:                 if ( d1[i]+d2[i]<d1[i] ) return 1;
                   1145:             return 0;
                   1146:             break;
                   1147:         default:
                   1148:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1149:                 u2 = d2[i];
                   1150:                 k = (nd_epw-1)*nd_bpe;
                   1151:                 for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1152:                     if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1153:             }
                   1154:             return 0;
                   1155:             break;
                   1156:     }
                   1157: #else
                   1158:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1159:         u2 = d2[i];
                   1160:         k = (nd_epw-1)*nd_bpe;
                   1161:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1162:             if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1163:     }
                   1164:     return 0;
                   1165: #endif
                   1166: }
                   1167:
                   1168: int ndl_check_bound2(int index,UINT *d2)
                   1169: {
                   1170:     return ndl_check_bound(nd_bound[index],d2);
                   1171: }
                   1172:
                   1173: INLINE int ndl_hash_value(UINT *d)
                   1174: {
                   1175:     int i;
1.11      noro     1176:     UINT r;
1.1       noro     1177:
                   1178:     r = 0;
                   1179:     for ( i = 0; i < nd_wpd; i++ )
1.12      noro     1180:         r = (r*1511+d[i]);
1.11      noro     1181:     r %= REDTAB_LEN;
1.1       noro     1182:     return r;
                   1183: }
                   1184:
                   1185: INLINE int ndl_find_reducer(UINT *dg)
                   1186: {
                   1187:     RHist r;
                   1188:     int d,k,i;
                   1189:
                   1190:     d = ndl_hash_value(dg);
                   1191:     for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   1192:         if ( ndl_equal(dg,DL(r)) ) {
                   1193:             if ( k > 0 ) nd_notfirst++;
                   1194:             nd_found++;
                   1195:             return r->index;
                   1196:         }
                   1197:     }
                   1198:     if ( Reverse )
                   1199:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   1200:             r = nd_psh[i];
                   1201:             if ( ndl_reducible(dg,DL(r)) ) {
                   1202:                 nd_create++;
                   1203:                 nd_append_red(dg,i);
                   1204:                 return i;
                   1205:             }
                   1206:         }
                   1207:     else
                   1208:         for ( i = 0; i < nd_psn; i++ ) {
                   1209:             r = nd_psh[i];
                   1210:             if ( ndl_reducible(dg,DL(r)) ) {
                   1211:                 nd_create++;
                   1212:                 nd_append_red(dg,i);
                   1213:                 return i;
                   1214:             }
                   1215:         }
                   1216:     return -1;
                   1217: }
                   1218:
1.41      noro     1219: INLINE int ndl_find_reducer_nonsig(UINT *dg)
                   1220: {
                   1221:     RHist r;
                   1222:     int i;
                   1223:
                   1224:     for ( i = 0; i < nd_psn; i++ ) {
                   1225:       r = nd_psh[i];
                   1226:       if ( ndl_reducible(dg,DL(r)) ) return i;
                   1227:     }
                   1228:     return -1;
                   1229: }
                   1230:
1.24      noro     1231: // ret=0,...,nd_psn-1 => reducer found
                   1232: // ret=nd_psn => reducer not found
                   1233: // ret=-1 => singular top reducible
                   1234:
                   1235: int comp_sig(SIG s1,SIG s2);
                   1236: void _ndltodl(UINT *ndl,DL dl);
                   1237:
                   1238: void print_sig(SIG s)
                   1239: {
                   1240:   int i;
                   1241:
                   1242:   fprintf(asir_out,"<<");
                   1243:   for ( i = 0; i < nd_nvar; i++ ) {
                   1244:     fprintf(asir_out,"%d",s->dl->d[i]);
                   1245:     if ( i != nd_nvar-1 ) fprintf(asir_out,",");
                   1246:   }
                   1247:   fprintf(asir_out,">>*e%d",s->pos);
                   1248: }
                   1249:
1.45    ! noro     1250: void print_siglist(NODE l)
        !          1251: {
        !          1252:   for ( ; l; l = NEXT(l) )
        !          1253:    print_sig((SIG)l->body);
        !          1254: }
        !          1255:
        !          1256:
1.35      noro     1257: // assuming increasing order wrt signature
                   1258:
1.24      noro     1259: INLINE int ndl_find_reducer_s(UINT *dg,SIG sig)
                   1260: {
                   1261:   RHist r;
1.27      noro     1262:   int i,singular,ret,d,k;
1.26      noro     1263:   static int wpd,nvar;
1.24      noro     1264:   static SIG quo;
                   1265:   static UINT *tmp;
                   1266:
1.26      noro     1267:   if ( !quo || nvar != nd_nvar ) NEWSIG(quo);
                   1268:   if ( wpd != nd_wpd ) {
1.24      noro     1269:     wpd = nd_wpd;
                   1270:     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
                   1271:   }
1.27      noro     1272:   d = ndl_hash_value(dg);
1.35      noro     1273: #if 1
1.27      noro     1274:   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   1275:     if ( ndl_equal(dg,DL(r)) ) {
                   1276:       return r->index;
                   1277:     }
                   1278:   }
1.35      noro     1279: #endif
1.24      noro     1280:   singular = 0;
                   1281:   for ( i = 0; i < nd_psn; i++ ) {
                   1282:     r = nd_psh[i];
                   1283:     if ( ndl_reducible(dg,DL(r)) ) {
1.27      noro     1284:       ndl_sub(dg,DL(r),tmp);
1.24      noro     1285:       _ndltodl(tmp,DL(quo));
                   1286:       _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
                   1287:       quo->pos = nd_psh[i]->sig->pos;
                   1288:       ret = comp_sig(sig,quo);
                   1289:       if ( ret > 0 ) { singular = 0; break; }
1.38      noro     1290:       if ( ret == 0 ) { /* fprintf(asir_out,"s"); fflush(asir_out); */ singular = 1; }
1.24      noro     1291:     }
                   1292:   }
                   1293:   if ( singular ) return -1;
1.27      noro     1294:   else if ( i < nd_psn )
                   1295:     nd_append_red(dg,i);
                   1296:   return i;
1.24      noro     1297: }
                   1298:
1.1       noro     1299: ND nd_merge(ND p1,ND p2)
                   1300: {
                   1301:     int n,c;
                   1302:     int t,can,td1,td2;
                   1303:     ND r;
                   1304:     NM m1,m2,mr0,mr,s;
                   1305:
                   1306:     if ( !p1 ) return p2;
                   1307:     else if ( !p2 ) return p1;
                   1308:     else {
                   1309:         can = 0;
                   1310:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1311:             c = DL_COMPARE(DL(m1),DL(m2));
                   1312:             switch ( c ) {
                   1313:                 case 0:
                   1314:                     s = m1; m1 = NEXT(m1);
                   1315:                     can++; NEXTNM2(mr0,mr,s);
                   1316:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1317:                     break;
                   1318:                 case 1:
                   1319:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1320:                     break;
                   1321:                 case -1:
                   1322:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1323:                     break;
                   1324:             }
                   1325:         }
                   1326:         if ( !mr0 )
                   1327:             if ( m1 ) mr0 = m1;
                   1328:             else if ( m2 ) mr0 = m2;
                   1329:             else return 0;
                   1330:         else if ( m1 ) NEXT(mr) = m1;
                   1331:         else if ( m2 ) NEXT(mr) = m2;
                   1332:         else NEXT(mr) = 0;
                   1333:         BDY(p1) = mr0;
                   1334:         SG(p1) = MAX(SG(p1),SG(p2));
                   1335:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1336:         FREEND(p2);
                   1337:         return p1;
                   1338:     }
                   1339: }
                   1340:
                   1341: ND nd_add(int mod,ND p1,ND p2)
                   1342: {
                   1343:     int n,c;
                   1344:     int t,can,td1,td2;
                   1345:     ND r;
                   1346:     NM m1,m2,mr0,mr,s;
                   1347:
1.11      noro     1348:     Nnd_add++;
1.1       noro     1349:     if ( !p1 ) return p2;
                   1350:     else if ( !p2 ) return p1;
                   1351:     else if ( mod == -1 ) return nd_add_sf(p1,p2);
                   1352:     else if ( mod == -2 ) return nd_add_lf(p1,p2);
                   1353:     else if ( !mod ) return nd_add_q(p1,p2);
                   1354:     else {
                   1355:         can = 0;
                   1356:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1357:             c = DL_COMPARE(DL(m1),DL(m2));
                   1358:             switch ( c ) {
                   1359:                 case 0:
                   1360:                     t = ((CM(m1))+(CM(m2))) - mod;
                   1361:                     if ( t < 0 ) t += mod;
                   1362:                     s = m1; m1 = NEXT(m1);
                   1363:                     if ( t ) {
                   1364:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1365:                     } else {
                   1366:                         can += 2; FREENM(s);
                   1367:                     }
                   1368:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1369:                     break;
                   1370:                 case 1:
                   1371:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1372:                     break;
                   1373:                 case -1:
                   1374:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1375:                     break;
                   1376:             }
                   1377:         }
                   1378:         if ( !mr0 )
                   1379:             if ( m1 ) mr0 = m1;
                   1380:             else if ( m2 ) mr0 = m2;
                   1381:             else return 0;
                   1382:         else if ( m1 ) NEXT(mr) = m1;
                   1383:         else if ( m2 ) NEXT(mr) = m2;
                   1384:         else NEXT(mr) = 0;
                   1385:         BDY(p1) = mr0;
                   1386:         SG(p1) = MAX(SG(p1),SG(p2));
                   1387:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1388:         FREEND(p2);
                   1389:         return p1;
                   1390:     }
                   1391: }
                   1392:
                   1393: /* XXX on opteron, the inlined manipulation of destructive additon of
                   1394:  * two NM seems to make gcc optimizer get confused, so the part is
                   1395:  * done in a function.
                   1396:  */
                   1397:
                   1398: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
                   1399: {
                   1400:     NM s;
                   1401:     P t;
                   1402:     int can;
                   1403:
                   1404:     addp(nd_vc,CP(*m1),CP(*m2),&t);
                   1405:     s = *m1; *m1 = NEXT(*m1);
                   1406:     if ( t ) {
                   1407:         can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
                   1408:     } else {
                   1409:         can = 2; FREENM(s);
                   1410:     }
                   1411:     s = *m2; *m2 = NEXT(*m2); FREENM(s);
                   1412:     return can;
                   1413: }
                   1414:
                   1415: ND nd_add_q(ND p1,ND p2)
                   1416: {
                   1417:     int n,c,can;
                   1418:     ND r;
                   1419:     NM m1,m2,mr0,mr,s;
                   1420:     P t;
                   1421:
                   1422:     if ( !p1 ) return p2;
                   1423:     else if ( !p2 ) return p1;
                   1424:     else {
                   1425:         can = 0;
                   1426:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1427:             c = DL_COMPARE(DL(m1),DL(m2));
                   1428:             switch ( c ) {
                   1429:                 case 0:
                   1430: #if defined(__x86_64__)
                   1431:                     can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
                   1432: #else
                   1433:                     addp(nd_vc,CP(m1),CP(m2),&t);
                   1434:                     s = m1; m1 = NEXT(m1);
                   1435:                     if ( t ) {
                   1436:                         can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
                   1437:                     } else {
                   1438:                         can += 2; FREENM(s);
                   1439:                     }
                   1440:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1441: #endif
                   1442:                     break;
                   1443:                 case 1:
                   1444:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1445:                     break;
                   1446:                 case -1:
                   1447:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1448:                     break;
                   1449:             }
                   1450:         }
                   1451:         if ( !mr0 )
                   1452:             if ( m1 ) mr0 = m1;
                   1453:             else if ( m2 ) mr0 = m2;
                   1454:             else return 0;
                   1455:         else if ( m1 ) NEXT(mr) = m1;
                   1456:         else if ( m2 ) NEXT(mr) = m2;
                   1457:         else NEXT(mr) = 0;
                   1458:         BDY(p1) = mr0;
                   1459:         SG(p1) = MAX(SG(p1),SG(p2));
                   1460:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1461:         FREEND(p2);
                   1462:         return p1;
                   1463:     }
                   1464: }
                   1465:
                   1466: ND nd_add_sf(ND p1,ND p2)
                   1467: {
                   1468:     int n,c,can;
                   1469:     ND r;
                   1470:     NM m1,m2,mr0,mr,s;
                   1471:     int t;
                   1472:
                   1473:     if ( !p1 ) return p2;
                   1474:     else if ( !p2 ) return p1;
                   1475:     else {
                   1476:         can = 0;
                   1477:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1478:             c = DL_COMPARE(DL(m1),DL(m2));
                   1479:             switch ( c ) {
                   1480:                 case 0:
                   1481:                     t = _addsf(CM(m1),CM(m2));
                   1482:                     s = m1; m1 = NEXT(m1);
                   1483:                     if ( t ) {
                   1484:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1485:                     } else {
                   1486:                         can += 2; FREENM(s);
                   1487:                     }
                   1488:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1489:                     break;
                   1490:                 case 1:
                   1491:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1492:                     break;
                   1493:                 case -1:
                   1494:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1495:                     break;
                   1496:             }
                   1497:         }
                   1498:         if ( !mr0 )
                   1499:             if ( m1 ) mr0 = m1;
                   1500:             else if ( m2 ) mr0 = m2;
                   1501:             else return 0;
                   1502:         else if ( m1 ) NEXT(mr) = m1;
                   1503:         else if ( m2 ) NEXT(mr) = m2;
                   1504:         else NEXT(mr) = 0;
                   1505:         BDY(p1) = mr0;
                   1506:         SG(p1) = MAX(SG(p1),SG(p2));
                   1507:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1508:         FREEND(p2);
                   1509:         return p1;
                   1510:     }
                   1511: }
                   1512:
                   1513:
                   1514: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
                   1515: {
                   1516:     int c,c1,c2;
                   1517:     Z cg,cred,gcd,tq;
                   1518:     P cgp,credp,gcdp;
                   1519:     Obj tr,tr1;
                   1520:
                   1521:     if ( mod == -1 ) {
                   1522:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1523:         *divp = (Obj)ONE;
                   1524:     } else if ( mod == -2 ) {
                   1525:         Z inv,t;
                   1526:         divlf(ONE,HCZ(p),&inv);
                   1527:         chsgnlf(HCZ(g),&t);
                   1528:         mullf(inv,t,&CZ(mul));
                   1529:         *divp = (Obj)ONE;
                   1530:     } else if ( mod ) {
                   1531:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1532:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1533:         *divp = (Obj)ONE;
                   1534:     } else if ( nd_vc ) {
                   1535:         ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
                   1536:         divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
                   1537:         chsgnp(cgp,&CP(mul));
                   1538:         nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
                   1539:         if ( dn ) {
                   1540:             mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
                   1541:             reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
                   1542:         }
                   1543:         *divp = (Obj)credp;
                   1544:     } else {
1.6       noro     1545:         igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1546:         chsgnz(cg,&CZ(mul));
1.1       noro     1547:         nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
                   1548:         if ( dn ) {
                   1549:             mulz(dn->z,cred,&tq); dn->z = tq;
                   1550:         }
                   1551:         *divp = (Obj)cred;
                   1552:     }
                   1553:     return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
                   1554: }
                   1555:
                   1556: /* ret=1 : success, ret=0 : overflow */
1.6       noro     1557: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1       noro     1558: {
                   1559:     NM m,mrd,tail;
                   1560:     NM mul;
                   1561:     int n,sugar,psugar,sugar0,stat,index;
                   1562:     int c,c1,c2,dummy;
                   1563:     RHist h;
                   1564:     NDV p,red;
                   1565:     Q cg,cred,gcd,tq,qq;
                   1566:     Z iq;
                   1567:     DP dmul;
                   1568:     NODE node;
                   1569:     LIST hist;
                   1570:     double hmag;
                   1571:     P tp,tp1;
                   1572:     Obj tr,tr1,div;
                   1573:     union oNDC hg;
                   1574:     P cont;
                   1575:
                   1576:     if ( !g ) {
                   1577:         *rp = d;
                   1578:         return 1;
                   1579:     }
                   1580:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1581:
                   1582:     sugar0 = sugar = SG(g);
                   1583:     n = NV(g);
                   1584:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1585:     if ( d )
                   1586:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1587:     for ( ; g; ) {
                   1588:         index = ndl_find_reducer(HDL(g));
                   1589:         if ( index >= 0 ) {
                   1590:             h = nd_psh[index];
                   1591:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1592:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1593:                 nd_free(g); nd_free(d);
                   1594:                 return 0;
                   1595:             }
                   1596:             p = nd_demand ? ndv_load(index) : ps[index];
                   1597:             /* d+g -> div*(d+g)+mul*p */
1.6       noro     1598:             g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1       noro     1599:             if ( nd_gentrace ) {
                   1600:                 /* Trace=[div,index,mul,ONE] */
1.6       noro     1601:                 STOZ(index,iq);
1.1       noro     1602:                 nmtodp(mod,mul,&dmul);
                   1603:                 node = mknode(4,div,iq,dmul,ONE);
                   1604:             }
                   1605:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
                   1606:             if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
                   1607:                 hg = HCU(g);
                   1608:                 nd_removecont2(d,g);
1.6       noro     1609:                 if ( nd_gentrace ) {
1.1       noro     1610:                     /* overwrite cont : Trace=[div,index,mul,cont] */
1.6       noro     1611:                     /* exact division */
1.1       noro     1612:                     cont = ndc_div(mod,hg,HCU(g));
                   1613:                     if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
                   1614:                 }
                   1615:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1616:             }
                   1617:             MKLIST(hist,node);
                   1618:             MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
                   1619:         } else if ( !full ) {
                   1620:             *rp = g;
                   1621:             return 1;
                   1622:         } else {
                   1623:             m = BDY(g);
                   1624:             if ( NEXT(m) ) {
                   1625:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1626:             } else {
                   1627:                 FREEND(g); g = 0;
                   1628:             }
                   1629:             if ( d ) {
                   1630:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1631:             } else {
                   1632:                 MKND(n,m,1,d); tail = BDY(d);
                   1633:             }
                   1634:         }
                   1635:     }
                   1636:     if ( d ) SG(d) = sugar;
                   1637:     *rp = d;
                   1638:     return 1;
                   1639: }
                   1640:
1.24      noro     1641: // ret=1 => success
                   1642: // ret=0 => overflow
                   1643: // ret=-1 => singular top reducible
                   1644:
                   1645: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
                   1646: {
                   1647:     NM m,mrd,tail;
                   1648:     NM mul;
                   1649:     int n,sugar,psugar,sugar0,stat,index;
                   1650:     int c,c1,c2,dummy;
                   1651:     RHist h;
                   1652:     NDV p,red;
                   1653:     Q cg,cred,gcd,tq,qq;
                   1654:     Z iq;
                   1655:     DP dmul;
                   1656:     NODE node;
                   1657:     LIST hist;
                   1658:     double hmag;
                   1659:     P tp,tp1;
                   1660:     Obj tr,tr1,div;
                   1661:     union oNDC hg;
                   1662:     P cont;
                   1663:     SIG sig;
                   1664:
                   1665:     if ( !g ) {
                   1666:         *rp = d;
                   1667:         return 1;
                   1668:     }
                   1669:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1670:
                   1671:     sugar0 = sugar = SG(g);
                   1672:     n = NV(g);
                   1673:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1674:     if ( d )
                   1675:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1676:     sig = g->sig;
                   1677:     for ( ; g; ) {
                   1678:         index = ndl_find_reducer_s(HDL(g),sig);
                   1679:         if ( index >= 0 && index < nd_psn ) {
                   1680:             // reducer found
                   1681:             h = nd_psh[index];
                   1682:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1683:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1684:                 nd_free(g); nd_free(d);
                   1685:                 return 0;
                   1686:             }
                   1687:             p = ps[index];
                   1688:             /* d+g -> div*(d+g)+mul*p */
                   1689:             g = nd_reduce2(mod,d,g,p,mul,0,&div);
                   1690:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
                   1691:             if ( !mod && g && ((double)(p_mag(HCP(g))) > hmag) ) {
                   1692:                 hg = HCU(g);
                   1693:                 nd_removecont2(d,g);
                   1694:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1695:             }
                   1696:         } else if ( index == -1 ) {
                   1697:           // singular top reducible
                   1698:           return -1;
                   1699:         } else if ( !full ) {
                   1700:             *rp = g;
                   1701:             g->sig = sig;
                   1702:             return 1;
                   1703:         } else {
                   1704:             m = BDY(g);
                   1705:             if ( NEXT(m) ) {
                   1706:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1707:             } else {
                   1708:                 FREEND(g); g = 0;
                   1709:             }
                   1710:             if ( d ) {
                   1711:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1712:             } else {
                   1713:                 MKND(n,m,1,d); tail = BDY(d);
                   1714:             }
                   1715:         }
                   1716:     }
                   1717:     if ( d ) {
                   1718:       SG(d) = sugar;
                   1719:       d->sig = sig;
                   1720:     }
                   1721:     *rp = d;
                   1722:     return 1;
                   1723: }
                   1724:
1.1       noro     1725: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
                   1726: {
                   1727:     int hindex,index;
                   1728:     NDV p;
                   1729:     ND u,d,red;
                   1730:     NODE l;
                   1731:     NM mul,m,mrd,tail;
                   1732:     int sugar,psugar,n,h_reducible;
                   1733:     PGeoBucket bucket;
                   1734:     int c,c1,c2;
                   1735:     Z cg,cred,gcd,zzz;
                   1736:     RHist h;
                   1737:     double hmag,gmag;
                   1738:     int count = 0;
                   1739:     int hcount = 0;
                   1740:
                   1741:     if ( !g ) {
                   1742:         *rp = 0;
                   1743:         return 1;
                   1744:     }
                   1745:     sugar = SG(g);
                   1746:     n = NV(g);
1.6       noro     1747:     if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1       noro     1748:     bucket = create_pbucket();
                   1749:     add_pbucket(mod,bucket,g);
                   1750:     d = 0;
                   1751:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1752:     while ( 1 ) {
                   1753:         if ( mod > 0 || mod == -1 )
                   1754:           hindex = head_pbucket(mod,bucket);
                   1755:         else if ( mod == -2 )
                   1756:           hindex = head_pbucket_lf(bucket);
                   1757:         else
                   1758:           hindex = head_pbucket_q(bucket);
                   1759:         if ( hindex < 0 ) {
                   1760:             if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1761:             if ( d ) SG(d) = sugar;
                   1762:             *rp = d;
                   1763:             return 1;
                   1764:         }
                   1765:         g = bucket->body[hindex];
                   1766:         index = ndl_find_reducer(HDL(g));
                   1767:         if ( index >= 0 ) {
                   1768:             count++;
                   1769:             if ( !d ) hcount++;
                   1770:             h = nd_psh[index];
                   1771:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1772:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1773:                 nd_free(d);
                   1774:                 free_pbucket(bucket);
                   1775:                 *rp = 0;
                   1776:                 return 0;
                   1777:             }
                   1778:             p = ps[index];
                   1779:             if ( mod == -1 )
                   1780:                 CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1781:             else if ( mod == -2 ) {
                   1782:                 Z inv,t;
                   1783:                 divlf(ONE,HCZ(p),&inv);
                   1784:                 chsgnlf(HCZ(g),&t);
                   1785:                 mullf(inv,t,&CZ(mul));
                   1786:             } else if ( mod ) {
                   1787:                 c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1788:                 DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1789:             } else {
1.6       noro     1790:                 igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1791:                 chsgnz(cg,&CZ(mul));
1.1       noro     1792:                 nd_mul_c_q(d,(P)cred);
                   1793:                 mulq_pbucket(bucket,cred);
                   1794:                 g = bucket->body[hindex];
1.6       noro     1795:                 gmag = (double)p_mag((P)HCZ(g));
1.1       noro     1796:             }
                   1797:             red = ndv_mul_nm(mod,mul,p);
                   1798:             bucket->body[hindex] = nd_remove_head(g);
                   1799:             red = nd_remove_head(red);
                   1800:             add_pbucket(mod,bucket,red);
                   1801:             psugar = SG(p)+TD(DL(mul));
                   1802:             sugar = MAX(sugar,psugar);
                   1803:             if ( !mod && hmag && (gmag > hmag) ) {
                   1804:                 g = normalize_pbucket(mod,bucket);
                   1805:                 if ( !g ) {
                   1806:                     if ( d ) SG(d) = sugar;
                   1807:                     *rp = d;
                   1808:                     return 1;
                   1809:                 }
                   1810:                 nd_removecont2(d,g);
1.6       noro     1811:                 hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1       noro     1812:                 add_pbucket(mod,bucket,g);
                   1813:             }
                   1814:         } else if ( !full ) {
                   1815:             g = normalize_pbucket(mod,bucket);
                   1816:             if ( g ) SG(g) = sugar;
                   1817:             *rp = g;
                   1818:             return 1;
                   1819:         } else {
                   1820:             m = BDY(g);
                   1821:             if ( NEXT(m) ) {
                   1822:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1823:             } else {
                   1824:                 FREEND(g); g = 0;
                   1825:             }
                   1826:             bucket->body[hindex] = g;
                   1827:             NEXT(m) = 0;
                   1828:             if ( d ) {
                   1829:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1830:             } else {
                   1831:                 MKND(n,m,1,d); tail = BDY(d);
                   1832:             }
                   1833:         }
                   1834:     }
                   1835: }
                   1836:
1.25      noro     1837: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *rp)
                   1838: {
                   1839:   int hindex,index;
                   1840:   NDV p;
                   1841:   ND u,d,red;
                   1842:   NODE l;
                   1843:   NM mul,m,mrd,tail;
                   1844:   int sugar,psugar,n,h_reducible;
                   1845:   PGeoBucket bucket;
                   1846:   int c,c1,c2;
                   1847:   Z cg,cred,gcd,zzz;
                   1848:   RHist h;
                   1849:   double hmag,gmag;
                   1850:   int count = 0;
                   1851:   int hcount = 0;
                   1852:   SIG sig;
                   1853:
                   1854:   if ( !g ) {
                   1855:     *rp = 0;
                   1856:     return 1;
                   1857:   }
                   1858:   sugar = SG(g);
                   1859:   n = NV(g);
                   1860:   if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
                   1861:   bucket = create_pbucket();
                   1862:   add_pbucket(mod,bucket,g);
                   1863:   d = 0;
                   1864:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1865:   sig = g->sig;
                   1866:   while ( 1 ) {
                   1867:     if ( mod > 0 || mod == -1 )
                   1868:       hindex = head_pbucket(mod,bucket);
                   1869:     else if ( mod == -2 )
                   1870:       hindex = head_pbucket_lf(bucket);
                   1871:     else
                   1872:       hindex = head_pbucket_q(bucket);
                   1873:     if ( hindex < 0 ) {
                   1874:       if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1875:       if ( d ) {
                   1876:         SG(d) = sugar;
                   1877:         d->sig = sig;
                   1878:       }
                   1879:       *rp = d;
                   1880:       return 1;
                   1881:     }
                   1882:     g = bucket->body[hindex];
                   1883:     index = ndl_find_reducer_s(HDL(g),sig);
                   1884:     if ( index >= 0 && index < nd_psn ) {
                   1885:       count++;
                   1886:       if ( !d ) hcount++;
                   1887:       h = nd_psh[index];
                   1888:       ndl_sub(HDL(g),DL(h),DL(mul));
                   1889:       if ( ndl_check_bound2(index,DL(mul)) ) {
                   1890:         nd_free(d);
                   1891:         free_pbucket(bucket);
                   1892:         *rp = 0;
                   1893:         return 0;
                   1894:       }
                   1895:       p = ps[index];
                   1896:       if ( mod == -1 )
                   1897:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1898:       else if ( mod == -2 ) {
                   1899:         Z inv,t;
                   1900:         divlf(ONE,HCZ(p),&inv);
                   1901:         chsgnlf(HCZ(g),&t);
                   1902:         mullf(inv,t,&CZ(mul));
                   1903:       } else if ( mod ) {
                   1904:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1905:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1906:       } else {
                   1907:         igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1908:         chsgnz(cg,&CZ(mul));
                   1909:         nd_mul_c_q(d,(P)cred);
                   1910:         mulq_pbucket(bucket,cred);
                   1911:         g = bucket->body[hindex];
                   1912:         gmag = (double)p_mag((P)HCZ(g));
                   1913:       }
                   1914:       red = ndv_mul_nm(mod,mul,p);
                   1915:       bucket->body[hindex] = nd_remove_head(g);
                   1916:       red = nd_remove_head(red);
                   1917:       add_pbucket(mod,bucket,red);
                   1918:       psugar = SG(p)+TD(DL(mul));
                   1919:       sugar = MAX(sugar,psugar);
                   1920:       if ( !mod && hmag && (gmag > hmag) ) {
                   1921:          g = normalize_pbucket(mod,bucket);
                   1922:          if ( !g ) {
                   1923:            if ( d ) {
                   1924:              SG(d) = sugar;
                   1925:              d->sig = sig;
                   1926:            }
                   1927:            *rp = d;
                   1928:            return 1;
                   1929:          }
                   1930:          nd_removecont2(d,g);
                   1931:          hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
                   1932:          add_pbucket(mod,bucket,g);
                   1933:       }
                   1934:     } else if ( index == -1 ) {
                   1935:       // singular top reducible
                   1936:       return -1;
                   1937:     } else if ( !full ) {
                   1938:       g = normalize_pbucket(mod,bucket);
                   1939:       if ( g ) {
                   1940:         SG(g) = sugar;
                   1941:         g->sig = sig;
                   1942:       }
                   1943:       *rp = g;
                   1944:       return 1;
                   1945:     } else {
                   1946:       m = BDY(g);
                   1947:       if ( NEXT(m) ) {
                   1948:         BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1949:       } else {
                   1950:         FREEND(g); g = 0;
                   1951:       }
                   1952:       bucket->body[hindex] = g;
                   1953:       NEXT(m) = 0;
                   1954:       if ( d ) {
                   1955:         NEXT(tail)=m; tail=m; LEN(d)++;
                   1956:       } else {
                   1957:         MKND(n,m,1,d); tail = BDY(d);
                   1958:       }
                   1959:     }
                   1960:   }
                   1961: }
                   1962:
1.1       noro     1963: /* input : list of NDV, cand : list of NDV */
                   1964:
                   1965: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
                   1966: {
                   1967:     int n,i,stat;
                   1968:     ND nf,d;
                   1969:     NDV r;
                   1970:     NODE t,s;
                   1971:     union oNDC dn;
                   1972:     Z q;
                   1973:     LIST list;
                   1974:
1.24      noro     1975:     ndv_setup(m,0,cand,nd_gentrace?1:0,1,0);
1.1       noro     1976:     n = length(cand);
                   1977:
                   1978:   if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
                   1979:     /* membercheck : list is a subset of Id(cand) ? */
                   1980:     for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
                   1981: again:
                   1982:     nd_tracelist = 0;
                   1983:         if ( nd_bpe > obpe )
                   1984:             r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
                   1985:         else
                   1986:             r = (NDV)BDY(t);
                   1987: #if 0
                   1988:         // moved to nd_f4_lf_trace()
                   1989:         if ( m == -2 ) ndv_mod(m,r);
                   1990: #endif
                   1991:         d = ndvtond(m,r);
1.6       noro     1992:         stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1       noro     1993:         if ( !stat ) {
                   1994:             nd_reconstruct(0,0);
                   1995:             goto again;
                   1996:         } else if ( nf ) return 0;
                   1997:     if ( nd_gentrace ) {
                   1998:       nd_tracelist = reverse_node(nd_tracelist);
                   1999:       MKLIST(list,nd_tracelist);
1.6       noro     2000:       STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1       noro     2001:       MKNODE(s,list,nd_alltracelist);
                   2002:       nd_alltracelist = s; nd_tracelist = 0;
                   2003:     }
                   2004:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   2005:     }
                   2006:     if ( DP_Print ) { printf("\n"); }
                   2007:     return 1;
                   2008: }
                   2009:
                   2010: ND nd_remove_head(ND p)
                   2011: {
                   2012:     NM m;
                   2013:
                   2014:     m = BDY(p);
                   2015:     if ( !NEXT(m) ) {
                   2016:         FREEND(p); p = 0;
                   2017:     } else {
                   2018:         BDY(p) = NEXT(m); LEN(p)--;
                   2019:     }
                   2020:     FREENM(m);
                   2021:     return p;
                   2022: }
                   2023:
                   2024: ND nd_separate_head(ND p,ND *head)
                   2025: {
                   2026:     NM m,m0;
                   2027:     ND r;
                   2028:
                   2029:     m = BDY(p);
                   2030:     if ( !NEXT(m) ) {
                   2031:         *head = p; p = 0;
                   2032:     } else {
                   2033:         m0 = m;
                   2034:         BDY(p) = NEXT(m); LEN(p)--;
                   2035:         NEXT(m0) = 0;
                   2036:         MKND(NV(p),m0,1,r);
                   2037:         *head = r;
                   2038:     }
                   2039:     return p;
                   2040: }
                   2041:
                   2042: PGeoBucket create_pbucket()
                   2043: {
                   2044:     PGeoBucket g;
                   2045:
                   2046:     g = CALLOC(1,sizeof(struct oPGeoBucket));
                   2047:     g->m = -1;
                   2048:     return g;
                   2049: }
                   2050:
                   2051: void free_pbucket(PGeoBucket b) {
                   2052:     int i;
                   2053:
                   2054:     for ( i = 0; i <= b->m; i++ )
                   2055:         if ( b->body[i] ) {
                   2056:             nd_free(b->body[i]);
                   2057:             b->body[i] = 0;
                   2058:         }
                   2059:     GCFREE(b);
                   2060: }
                   2061:
1.39      noro     2062: #if 0
1.1       noro     2063: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   2064: {
                   2065:     int l,i,k,m;
                   2066:
                   2067:     if ( !d )
                   2068:         return;
                   2069:     l = LEN(d);
                   2070:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2071:     /* 2^(k-1) < l <= 2^k (=m) */
                   2072:     d = nd_merge(g->body[k],d);
                   2073:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   2074:         g->body[k] = 0;
                   2075:         d = nd_merge(g->body[k+1],d);
                   2076:     }
                   2077:     g->body[k] = d;
                   2078:     g->m = MAX(g->m,k);
                   2079: }
1.39      noro     2080: #else
                   2081: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   2082: {
                   2083:   int l,i,k,m,m0;
                   2084:
                   2085:   if ( !d )
                   2086:     return;
                   2087:   m0 = g->m;
                   2088:   while ( 1 ) {
                   2089:     l = LEN(d);
                   2090:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2091:     /* 2^(k-1) < l <= 2^k (=m) */
                   2092:     if ( g->body[k] == 0 ) {
                   2093:       g->body[k] = d;
                   2094:       m0 = MAX(k,m0);
                   2095:       break;
                   2096:     } else {
                   2097:       d = nd_merge(g->body[k],d);
                   2098:       g->body[k] = 0;
                   2099:     }
                   2100:   }
                   2101:   g->m = m0;
                   2102: }
                   2103: #endif
1.1       noro     2104:
1.39      noro     2105: #if 0
1.1       noro     2106: void add_pbucket(int mod,PGeoBucket g,ND d)
                   2107: {
                   2108:     int l,i,k,m;
                   2109:
                   2110:     if ( !d )
                   2111:         return;
                   2112:     l = LEN(d);
                   2113:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2114:     /* 2^(k-1) < l <= 2^k (=m) */
                   2115:     d = nd_add(mod,g->body[k],d);
                   2116:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   2117:         g->body[k] = 0;
                   2118:         d = nd_add(mod,g->body[k+1],d);
                   2119:     }
                   2120:     g->body[k] = d;
                   2121:     g->m = MAX(g->m,k);
                   2122: }
1.39      noro     2123: #else
                   2124: void add_pbucket(int mod,PGeoBucket g,ND d)
                   2125: {
                   2126:   int l,i,k,m,m0;
                   2127:
                   2128:   m0 = g->m;
                   2129:   while ( d != 0 ) {
                   2130:     l = LEN(d);
                   2131:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2132:     /* 2^(k-1) < l <= 2^k (=m) */
                   2133:     if ( g->body[k] == 0 ) {
                   2134:       g->body[k] = d;
                   2135:       m0 = MAX(k,m0);
                   2136:       break;
                   2137:     } else {
                   2138:       d = nd_add(mod,g->body[k],d);
                   2139:       g->body[k] = 0;
                   2140:     }
                   2141:   }
                   2142:   g->m = m0;
                   2143: }
                   2144: #endif
1.1       noro     2145:
                   2146: void mulq_pbucket(PGeoBucket g,Z c)
                   2147: {
                   2148:     int k;
                   2149:
                   2150:     for ( k = 0; k <= g->m; k++ )
                   2151:         nd_mul_c_q(g->body[k],(P)c);
                   2152: }
                   2153:
                   2154: NM remove_head_pbucket_symbolic(PGeoBucket g)
                   2155: {
                   2156:     int j,i,k,c;
                   2157:     NM head;
                   2158:
                   2159:     k = g->m;
                   2160:     j = -1;
                   2161:     for ( i = 0; i <= k; i++ ) {
                   2162:         if ( !g->body[i] ) continue;
                   2163:         if ( j < 0 ) j = i;
                   2164:         else {
                   2165:             c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
                   2166:             if ( c > 0 )
                   2167:                 j = i;
                   2168:             else if ( c == 0 )
                   2169:                 g->body[i] = nd_remove_head(g->body[i]);
                   2170:         }
                   2171:     }
                   2172:     if ( j < 0 ) return 0;
                   2173:     else {
                   2174:         head = BDY(g->body[j]);
                   2175:         if ( !NEXT(head) ) {
                   2176:             FREEND(g->body[j]);
                   2177:             g->body[j] = 0;
                   2178:         } else {
                   2179:             BDY(g->body[j]) = NEXT(head);
                   2180:             LEN(g->body[j])--;
                   2181:         }
                   2182:         return head;
                   2183:     }
                   2184: }
                   2185:
                   2186: int head_pbucket(int mod,PGeoBucket g)
                   2187: {
                   2188:     int j,i,c,k,nv,sum;
                   2189:     UINT *di,*dj;
                   2190:     ND gi,gj;
                   2191:
                   2192:     k = g->m;
                   2193:     while ( 1 ) {
                   2194:         j = -1;
                   2195:         for ( i = 0; i <= k; i++ ) {
                   2196:             if ( !(gi = g->body[i]) )
                   2197:                 continue;
                   2198:             if ( j < 0 ) {
                   2199:                 j = i;
                   2200:                 gj = g->body[j];
                   2201:                 dj = HDL(gj);
                   2202:                 sum = HCM(gj);
                   2203:             } else {
                   2204:                 c = DL_COMPARE(HDL(gi),dj);
                   2205:                 if ( c > 0 ) {
                   2206:                     if ( sum ) HCM(gj) = sum;
                   2207:                     else g->body[j] = nd_remove_head(gj);
                   2208:                     j = i;
                   2209:                     gj = g->body[j];
                   2210:                     dj = HDL(gj);
                   2211:                     sum = HCM(gj);
                   2212:                 } else if ( c == 0 ) {
                   2213:                     if ( mod == -1 )
                   2214:                         sum = _addsf(sum,HCM(gi));
                   2215:                     else {
                   2216:                         sum = sum+HCM(gi)-mod;
                   2217:                         if ( sum < 0 ) sum += mod;
                   2218:                     }
                   2219:                     g->body[i] = nd_remove_head(gi);
                   2220:                 }
                   2221:             }
                   2222:         }
                   2223:         if ( j < 0 ) return -1;
                   2224:         else if ( sum ) {
                   2225:             HCM(gj) = sum;
                   2226:             return j;
                   2227:         } else
                   2228:             g->body[j] = nd_remove_head(gj);
                   2229:     }
                   2230: }
                   2231:
                   2232: int head_pbucket_q(PGeoBucket g)
                   2233: {
                   2234:     int j,i,c,k,nv;
                   2235:     Z sum,t;
                   2236:     ND gi,gj;
                   2237:
                   2238:     k = g->m;
                   2239:     while ( 1 ) {
                   2240:         j = -1;
                   2241:         for ( i = 0; i <= k; i++ ) {
                   2242:             if ( !(gi = g->body[i]) ) continue;
                   2243:             if ( j < 0 ) {
                   2244:                 j = i;
                   2245:                 gj = g->body[j];
1.6       noro     2246:                 sum = HCZ(gj);
1.1       noro     2247:             } else {
                   2248:                 nv = NV(gi);
                   2249:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   2250:                 if ( c > 0 ) {
1.6       noro     2251:                     if ( sum ) HCZ(gj) = sum;
1.1       noro     2252:                     else g->body[j] = nd_remove_head(gj);
                   2253:                     j = i;
                   2254:                     gj = g->body[j];
1.6       noro     2255:                     sum = HCZ(gj);
1.1       noro     2256:                 } else if ( c == 0 ) {
1.6       noro     2257:                     addz(sum,HCZ(gi),&t);
1.1       noro     2258:                     sum = t;
                   2259:                     g->body[i] = nd_remove_head(gi);
                   2260:                 }
                   2261:             }
                   2262:         }
                   2263:         if ( j < 0 ) return -1;
                   2264:         else if ( sum ) {
1.6       noro     2265:             HCZ(gj) = sum;
1.1       noro     2266:             return j;
                   2267:         } else
                   2268:             g->body[j] = nd_remove_head(gj);
                   2269:     }
                   2270: }
                   2271:
                   2272: int head_pbucket_lf(PGeoBucket g)
                   2273: {
                   2274:     int j,i,c,k,nv;
                   2275:     Z sum,t;
                   2276:     ND gi,gj;
                   2277:
                   2278:     k = g->m;
                   2279:     while ( 1 ) {
                   2280:         j = -1;
                   2281:         for ( i = 0; i <= k; i++ ) {
                   2282:             if ( !(gi = g->body[i]) ) continue;
                   2283:             if ( j < 0 ) {
                   2284:                 j = i;
                   2285:                 gj = g->body[j];
                   2286:                 sum = HCZ(gj);
                   2287:             } else {
                   2288:                 nv = NV(gi);
                   2289:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   2290:                 if ( c > 0 ) {
                   2291:                     if ( sum ) HCZ(gj) = sum;
                   2292:                     else g->body[j] = nd_remove_head(gj);
                   2293:                     j = i;
                   2294:                     gj = g->body[j];
                   2295:                     sum = HCZ(gj);
                   2296:                 } else if ( c == 0 ) {
                   2297:                     addlf(sum,HCZ(gi),&t);
                   2298:                     sum = t;
                   2299:                     g->body[i] = nd_remove_head(gi);
                   2300:                 }
                   2301:             }
                   2302:         }
                   2303:         if ( j < 0 ) return -1;
                   2304:         else if ( sum ) {
                   2305:             HCZ(gj) = sum;
                   2306:             return j;
                   2307:         } else
                   2308:             g->body[j] = nd_remove_head(gj);
                   2309:     }
                   2310: }
                   2311:
                   2312: ND normalize_pbucket(int mod,PGeoBucket g)
                   2313: {
                   2314:     int i;
                   2315:     ND r,t;
                   2316:
                   2317:     r = 0;
                   2318:     for ( i = 0; i <= g->m; i++ ) {
                   2319:         r = nd_add(mod,r,g->body[i]);
                   2320:         g->body[i] = 0;
                   2321:     }
                   2322:     g->m = -1;
                   2323:     return r;
                   2324: }
                   2325:
                   2326: #if 0
                   2327: void register_hcf(NDV p)
                   2328: {
                   2329:     DCP dc,t;
                   2330:     P hc,h;
                   2331:     int c;
                   2332:     NODE l,l1,prev;
                   2333:
                   2334:     hc = p->body->c.p;
                   2335:     if ( !nd_vc || NUM(hc) ) return;
                   2336:     fctrp(nd_vc,hc,&dc);
                   2337:     for ( t = dc; t; t = NEXT(t) ) {
                   2338:         h = t->c;
                   2339:         if ( NUM(h) ) continue;
                   2340:         for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   2341:             c = compp(nd_vc,h,(P)BDY(l));
                   2342:             if ( c >= 0 ) break;
                   2343:         }
                   2344:         if ( !l || c > 0  ) {
                   2345:             MKNODE(l1,h,l);
                   2346:             if ( !prev )
                   2347:                 nd_hcf = l1;
                   2348:             else
                   2349:                 NEXT(prev) = l1;
                   2350:         }
                   2351:     }
                   2352: }
                   2353: #else
                   2354: void register_hcf(NDV p)
                   2355: {
                   2356:     DCP dc,t;
                   2357:     P hc,h,q;
                   2358:     Q dmy;
                   2359:     int c;
                   2360:     NODE l,l1,prev;
                   2361:
                   2362:     hc = p->body->c.p;
                   2363:     if ( NUM(hc) ) return;
                   2364:     ptozp(hc,1,&dmy,&h);
                   2365: #if 1
                   2366:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   2367:         while ( 1 ) {
                   2368:             if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
                   2369:             else break;
                   2370:         }
                   2371:     }
                   2372:     if ( NUM(h) ) return;
                   2373: #endif
                   2374:     for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   2375:         c = compp(nd_vc,h,(P)BDY(l));
                   2376:         if ( c >= 0 ) break;
                   2377:     }
                   2378:     if ( !l || c > 0  ) {
                   2379:         MKNODE(l1,h,l);
                   2380:         if ( !prev )
                   2381:             nd_hcf = l1;
                   2382:         else
                   2383:             NEXT(prev) = l1;
                   2384:     }
                   2385: }
                   2386: #endif
                   2387:
                   2388: int do_diagonalize(int sugar,int m)
                   2389: {
1.6       noro     2390:   int i,nh,stat;
                   2391:   NODE r,g,t;
                   2392:   ND h,nf,s,head;
                   2393:   NDV nfv;
                   2394:   Q q;
                   2395:   P nm,nmp,dn,mnp,dnp,cont,cont1;
                   2396:   union oNDC hc;
                   2397:   NODE node;
                   2398:   LIST l;
                   2399:   Z iq;
1.1       noro     2400:
1.6       noro     2401:   for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   2402:     if ( nd_gentrace ) {
                   2403:       /* Trace = [1,index,1,1] */
                   2404:       STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2405:       MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2406:     }
                   2407:     if ( nd_demand )
                   2408:       nfv = ndv_load(i);
                   2409:     else
                   2410:       nfv = nd_ps[i];
                   2411:     s = ndvtond(m,nfv);
                   2412:     s = nd_separate_head(s,&head);
                   2413:     stat = nd_nf(m,head,s,nd_ps,1,&nf);
                   2414:     if ( !stat ) return 0;
                   2415:     ndv_free(nfv);
                   2416:     hc = HCU(nf); nd_removecont(m,nf);
                   2417:     /* exact division */
                   2418:     cont = ndc_div(m,hc,HCU(nf));
1.1       noro     2419:     if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6       noro     2420:     nfv = ndtondv(m,nf);
                   2421:     nd_free(nf);
                   2422:     nd_bound[i] = ndv_compute_bound(nfv);
                   2423:     if ( !m ) register_hcf(nfv);
                   2424:     if ( nd_demand ) {
                   2425:       ndv_save(nfv,i);
                   2426:       ndv_free(nfv);
                   2427:     } else
                   2428:       nd_ps[i] = nfv;
                   2429:   }
                   2430:   return 1;
1.1       noro     2431: }
                   2432:
                   2433: LIST compute_splist()
                   2434: {
                   2435:   NODE g,tn0,tn,node;
                   2436:   LIST l0;
                   2437:   ND_pairs d,t;
                   2438:   int i;
                   2439:   Z i1,i2;
                   2440:
                   2441:     g = 0; d = 0;
                   2442:     for ( i = 0; i < nd_psn; i++ ) {
                   2443:         d = update_pairs(d,g,i,0);
                   2444:         g = update_base(g,i);
                   2445:     }
                   2446:   for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
                   2447:     NEXTNODE(tn0,tn);
1.6       noro     2448:         STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1       noro     2449:         node = mknode(2,i1,i2); MKLIST(l0,node);
                   2450:     BDY(tn) = l0;
                   2451:   }
                   2452:   if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
                   2453:   return l0;
                   2454: }
                   2455:
                   2456: /* return value = 0 => input is not a GB */
                   2457:
                   2458: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
                   2459: {
1.6       noro     2460:   int i,nh,sugar,stat;
                   2461:   NODE r,g,t;
                   2462:   ND_pairs d;
                   2463:   ND_pairs l;
                   2464:   ND h,nf,s,head,nf1;
                   2465:   NDV nfv;
                   2466:   Z q;
                   2467:   union oNDC dn,hc;
                   2468:   int diag_count = 0;
1.41      noro     2469:   int Nnfnz = 0,Nnfz = 0;
1.6       noro     2470:   P cont;
                   2471:   LIST list;
1.27      noro     2472: struct oEGT eg1,eg2,eg_update;
1.6       noro     2473:
1.27      noro     2474: init_eg(&eg_update);
1.11      noro     2475:   Nnd_add = 0;
1.6       noro     2476:   g = 0; d = 0;
                   2477:   for ( i = 0; i < nd_psn; i++ ) {
                   2478:     d = update_pairs(d,g,i,gensyz);
                   2479:     g = update_base(g,i);
                   2480:   }
                   2481:   sugar = 0;
                   2482:   while ( d ) {
1.1       noro     2483: again:
1.6       noro     2484:     l = nd_minp(d,&d);
                   2485:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   2486:     if ( SG(l) != sugar ) {
                   2487:       if ( ishomo ) {
                   2488:         diag_count = 0;
                   2489:         stat = do_diagonalize(sugar,m);
1.1       noro     2490:         if ( !stat ) {
1.6       noro     2491:           NEXT(l) = d; d = l;
                   2492:           d = nd_reconstruct(0,d);
                   2493:           goto again;
1.1       noro     2494:         }
1.6       noro     2495:       }
                   2496:       sugar = SG(l);
                   2497:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2498:     }
                   2499:     stat = nd_sp(m,0,l,&h);
                   2500:     if ( !stat ) {
                   2501:       NEXT(l) = d; d = l;
                   2502:       d = nd_reconstruct(0,d);
                   2503:       goto again;
                   2504:     }
1.1       noro     2505: #if USE_GEOBUCKET
1.39      noro     2506:     stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf)
                   2507:       :nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     2508: #else
1.39      noro     2509:     stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     2510: #endif
1.6       noro     2511:     if ( !stat ) {
                   2512:       NEXT(l) = d; d = l;
                   2513:       d = nd_reconstruct(0,d);
                   2514:       goto again;
                   2515:     } else if ( nf ) {
1.41      noro     2516:       Nnfnz++;
1.6       noro     2517:       if ( checkonly || gensyz ) return 0;
1.1       noro     2518:       if ( nd_newelim ) {
                   2519:         if ( nd_module ) {
                   2520:           if ( MPOS(HDL(nf)) > 1 ) return 0;
                   2521:         } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
                   2522:       }
1.6       noro     2523:       if ( DP_Print ) { printf("+"); fflush(stdout); }
                   2524:       hc = HCU(nf);
                   2525:       nd_removecont(m,nf);
                   2526:       if ( !m && nd_nalg ) {
                   2527:         nd_monic(0,&nf);
                   2528:         nd_removecont(m,nf);
                   2529:       }
                   2530:       if ( nd_gentrace ) {
                   2531:         /* exact division */
1.1       noro     2532:         cont = ndc_div(m,hc,HCU(nf));
                   2533:         if ( m || !UNIQ(cont) ) {
1.6       noro     2534:           t = mknode(4,NULLP,NULLP,NULLP,cont);
                   2535:           MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1       noro     2536:           nd_tracelist = t;
                   2537:         }
                   2538:       }
1.6       noro     2539:       nfv = ndtondv(m,nf); nd_free(nf);
1.24      noro     2540:       nh = ndv_newps(m,nfv,0);
1.6       noro     2541:       if ( !m && (ishomo && ++diag_count == diag_period) ) {
                   2542:         diag_count = 0;
                   2543:         stat = do_diagonalize(sugar,m);
                   2544:         if ( !stat ) {
                   2545:           NEXT(l) = d; d = l;
                   2546:           d = nd_reconstruct(1,d);
                   2547:           goto again;
1.1       noro     2548:         }
1.6       noro     2549:       }
1.27      noro     2550: get_eg(&eg1);
1.6       noro     2551:       d = update_pairs(d,g,nh,0);
1.27      noro     2552: get_eg(&eg2); add_eg(&eg_update,&eg1,&eg2);
1.6       noro     2553:       g = update_base(g,nh);
                   2554:       FREENDP(l);
1.41      noro     2555:     } else {
                   2556:       Nnfz++;
                   2557:       if ( nd_gentrace && gensyz ) {
                   2558:         nd_tracelist = reverse_node(nd_tracelist);
                   2559:         MKLIST(list,nd_tracelist);
                   2560:         STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
                   2561:         MKNODE(t,list,nd_alltracelist);
                   2562:         nd_alltracelist = t; nd_tracelist = 0;
                   2563:       }
                   2564:       if ( DP_Print ) { printf("."); fflush(stdout); }
                   2565:         FREENDP(l);
                   2566:     }
1.36      noro     2567:   }
                   2568:   conv_ilist(nd_demand,0,g,indp);
1.41      noro     2569:   if ( !checkonly && DP_Print ) {
                   2570:     printf("\nnd_gb done. Nnd_add=%d,Npairs=%d, Nnfnz=%d,Nnfz=%d,",Nnd_add,Npairs,Nnfnz,Nnfz);
                   2571:     printf("Nremoved=%d\n",NcriB+NcriMF+Ncri2);
                   2572:     fflush(asir_out);
                   2573:   }
                   2574:   if ( DP_Print ) {
                   2575:     print_eg("update",&eg_update); fprintf(asir_out,"\n");
                   2576:   }
1.36      noro     2577:   return g;
1.1       noro     2578: }
                   2579:
1.30      noro     2580: ND_pairs update_pairs_s(ND_pairs d,int t,NODE *syz);
                   2581: ND_pairs nd_newpairs_s(int t ,NODE *syz);
1.24      noro     2582:
                   2583: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *nf);
                   2584: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
                   2585:
                   2586: void _copydl(int n,DL d1,DL d2);
                   2587: void _subfromdl(int n,DL d1,DL d2);
                   2588: extern int (*cmpdl)(int n,DL d1,DL d2);
                   2589:
                   2590: NODE insert_sig(NODE l,SIG s)
                   2591: {
                   2592:   int pos;
                   2593:   DL sig;
                   2594:   struct oNODE root;
                   2595:   NODE p,prev,r;
                   2596:   SIG t;
                   2597:
                   2598:   pos = s->pos; sig = DL(s);
                   2599:   root.next = l; prev = &root;
                   2600:   for ( p = l; p; p = p->next ) {
                   2601:     t = (SIG)p->body;
                   2602:     if ( t->pos == pos ) {
                   2603:       if ( _dl_redble(DL(t),sig,nd_nvar) )
                   2604:         return root.next;
                   2605:       else if ( _dl_redble(sig,DL(t),nd_nvar) )
                   2606:         // remove p
                   2607:         prev->next = p->next;
1.45    ! noro     2608:       else
        !          2609:         prev = p;
1.24      noro     2610:     } else
                   2611:       prev = p;
                   2612:   }
1.27      noro     2613:   NEWNODE(r); r->body = (pointer)s; r->next = 0;
                   2614:   for ( p = &root; p->next; p = p->next );
                   2615:   p->next = r;
                   2616: //  r->next = root.next;
                   2617: //  return r;
                   2618:   return root.next;
1.24      noro     2619: }
                   2620:
                   2621: ND_pairs remove_spair_s(ND_pairs d,SIG sig)
                   2622: {
                   2623:   struct oND_pairs root;
                   2624:   ND_pairs prev,p;
                   2625:   SIG spsig;
                   2626:
                   2627:   root.next = d;
                   2628:   prev = &root; p = d;
                   2629:   while ( p ) {
                   2630:     spsig = p->sig;
1.26      noro     2631:     if ( sig->pos == spsig->pos && _dl_redble(DL(sig),DL(spsig),nd_nvar) ) {
1.24      noro     2632:       // remove p
                   2633:       prev->next = p->next;
1.26      noro     2634:       Nsyz++;
                   2635:     } else
1.24      noro     2636:       prev = p;
                   2637:     p = p->next;
                   2638:   }
                   2639:   return (ND_pairs)root.next;
                   2640: }
                   2641:
1.29      noro     2642: int _dl_redble_ext(DL,DL,DL,int);
                   2643:
1.28      noro     2644: int small_lcm(ND_pairs l)
                   2645: {
                   2646:   SIG sig;
                   2647:   int i;
1.29      noro     2648:   NODE t;
1.28      noro     2649:   static DL lcm,mul,quo;
1.44      noro     2650:   static int nvar = 0;
1.28      noro     2651:
1.38      noro     2652:   if ( nd_sba_largelcm ) return 0;
1.28      noro     2653:   if ( nvar < nd_nvar ) {
                   2654:     nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(quo,nvar); NEWDL(mul,nvar);
                   2655:   }
                   2656:   sig = l->sig;
                   2657:   _ndltodl(l->lcm,lcm);
1.29      noro     2658: #if 0
1.28      noro     2659:   for ( i = 0; i < nd_psn; i++ ) {
                   2660:     if ( sig->pos == nd_psh[i]->sig->pos &&
1.29      noro     2661:       _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
1.28      noro     2662:       _ndltodl(DL(nd_psh[i]),mul);
                   2663:       _addtodl(nd_nvar,quo,mul);
                   2664:       if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
                   2665:         break;
                   2666:     }
                   2667:   }
                   2668:   if ( i < nd_psn ) return 1;
                   2669:   else return 0;
1.29      noro     2670: #else
                   2671:   for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
                   2672:     i = (long)BDY(t);
                   2673:     if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
                   2674:       _ndltodl(DL(nd_psh[i]),mul);
                   2675:       _addtodl(nd_nvar,quo,mul);
                   2676:       if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
                   2677:         break;
                   2678:     }
                   2679:   }
                   2680:   if ( t ) return 1;
                   2681:   else return 0;
                   2682: #endif
1.28      noro     2683: }
                   2684:
1.41      noro     2685: ND_pairs find_smallest_lcm(ND_pairs l)
                   2686: {
                   2687:   SIG sig;
                   2688:   int i,minindex;
                   2689:   NODE t;
                   2690:   ND_pairs r;
                   2691:   struct oSIG sig1;
                   2692:   static DL mul,quo,minlm;
1.44      noro     2693:   static int nvar = 0;
1.41      noro     2694:
                   2695:   if ( nvar < nd_nvar ) {
                   2696:     nvar = nd_nvar;
                   2697:     NEWDL(quo,nvar); NEWDL(mul,nvar);
                   2698:     NEWDL(minlm,nvar);
                   2699:   }
                   2700:   sig = l->sig;
                   2701:   // find mg s.t. m*s(g)=sig and m*lm(g) is minimal
                   2702:   _ndltodl(l->lcm,minlm); minindex = -1;
                   2703:   for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
                   2704:     i = (long)BDY(t);
                   2705:     if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
                   2706:       _ndltodl(DL(nd_psh[i]),mul);
                   2707:       _addtodl(nd_nvar,quo,mul);
                   2708:       if ( (*cmpdl)(nd_nvar,minlm,mul) > 0 ) {
                   2709:         minindex = i;
                   2710:         _copydl(nd_nvar,mul,minlm);
                   2711:       }
                   2712:     }
                   2713:   }
                   2714:   // l->lcm is minimal; return l itself
                   2715:   if ( minindex < 0 ) return l;
                   2716:   for ( i = 0; i < nd_psn; i++ ) {
                   2717:     if ( i == minindex ) continue;
                   2718:     _ndltodl(DL(nd_psh[i]),mul);
                   2719:     if ( _dl_redble_ext(mul,minlm,quo,nd_nvar) ) {
                   2720:       _addtodl(nd_nvar,nd_ps[i]->sig->dl,quo);
                   2721:       sig1.pos = nd_ps[i]->sig->pos;
                   2722:       sig1.dl = quo;
                   2723:       if ( comp_sig(sig,&sig1) > 0 ) {
                   2724: //        printf("X");
                   2725:         NEWND_pairs(r);
                   2726:         r->sig = sig;
                   2727:         r->i1 = minindex;
                   2728:         r->i2 = i;
                   2729:         dltondl(nd_nvar,minlm,r->lcm);
                   2730:         r->next = 0;
                   2731:         return r;
                   2732:       }
                   2733:     }
                   2734:   }
                   2735:   // there is no suitable spair
                   2736:   return 0;
                   2737: }
                   2738:
1.28      noro     2739: ND_pairs remove_large_lcm(ND_pairs d)
                   2740: {
                   2741:   struct oND_pairs root;
                   2742:   ND_pairs prev,p;
                   2743:
                   2744:   root.next = d;
                   2745:   prev = &root; p = d;
                   2746:   while ( p ) {
1.41      noro     2747: #if 0
1.28      noro     2748:     if ( small_lcm(p) ) {
                   2749:       // remove p
                   2750:       prev->next = p->next;
                   2751:     } else
1.41      noro     2752: #else
                   2753:     if ( find_smallest_lcm(p) == 0 ) {
                   2754:       // remove p
                   2755:       prev->next = p->next;
                   2756:     } else
                   2757: #endif
1.28      noro     2758:       prev = p;
                   2759:     p = p->next;
                   2760:   }
                   2761:   return (ND_pairs)root.next;
                   2762: }
                   2763:
1.26      noro     2764: struct oEGT eg_create,eg_newpairs,eg_merge;
                   2765:
1.30      noro     2766: NODE conv_ilist_s(int demand,int trace,int **indp);
                   2767:
1.44      noro     2768: // S(fj*ei-fi*ej)
                   2769:
1.45    ! noro     2770: void _subdl(int,DL,DL,DL);
        !          2771:
1.44      noro     2772: SIG trivial_sig(int i,int j)
                   2773: {
1.45    ! noro     2774:   static DL lcm;
1.44      noro     2775:   static struct oSIG sigi,sigj;
                   2776:   static int nvar = 0;
                   2777:   SIG sig;
                   2778:
                   2779:   if ( nvar != nd_nvar ) {
1.45    ! noro     2780:     nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(sigi.dl,nvar); NEWDL(sigj.dl,nvar);
        !          2781:   }
        !          2782:   if ( nd_sba_grevlexgb != 0 ) {
        !          2783:     lcm_of_DL(nd_nvar,nd_sba_hm[i],nd_sba_hm[j],lcm);
        !          2784:     sigi.pos = i; _subdl(nd_nvar,lcm,nd_sba_hm[i],sigi.dl);
        !          2785:     sigj.pos = j; _subdl(nd_nvar,lcm,nd_sba_hm[j],sigj.dl);
        !          2786:     if ( comp_sig(&sigi,&sigj) > 0 ) sig = dup_sig(&sigi);
        !          2787:     else sig = dup_sig(&sigj);
        !          2788:   } else {
        !          2789:     sigi.pos = i; _copydl(nd_nvar,nd_sba_hm[j],sigi.dl);
        !          2790:     sigj.pos = j; _copydl(nd_nvar,nd_sba_hm[i],sigj.dl);
        !          2791:     if ( comp_sig(&sigi,&sigj) > 0 ) sig = dup_sig(&sigi);
        !          2792:     else sig = dup_sig(&sigj);
1.44      noro     2793:   }
                   2794:   return sig;
                   2795: }
                   2796:
                   2797: NODE nd_sba_buch(int m,int ishomo,int **indp,NODE *syzp)
1.24      noro     2798: {
1.39      noro     2799:   int i,j,nh,sugar,stat,pos;
1.30      noro     2800:   NODE r,t,g;
1.24      noro     2801:   ND_pairs d;
1.41      noro     2802:   ND_pairs l,l1;
1.24      noro     2803:   ND h,nf,s,head,nf1;
                   2804:   NDV nfv;
                   2805:   Z q;
                   2806:   union oNDC dn,hc;
                   2807:   P cont;
                   2808:   LIST list;
                   2809:   SIG sig;
1.29      noro     2810:   NODE *syzlist;
1.44      noro     2811:   int ngen;
1.41      noro     2812:   int Nnominimal,Nredundant;
1.28      noro     2813:   DL lcm,quo,mul;
1.31      noro     2814:   struct oEGT eg1,eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.41      noro     2815:   int Nnfs=0,Nnfz=0,Nnfnz=0;
1.24      noro     2816:
1.27      noro     2817: init_eg(&eg_remove);
1.29      noro     2818:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.26      noro     2819:   Nsyz = 0;
1.24      noro     2820:   Nnd_add = 0;
1.41      noro     2821:   Nnominimal = 0;
1.26      noro     2822:   Nredundant = 0;
1.30      noro     2823:   d = 0;
1.44      noro     2824:   ngen = nd_psn;
1.29      noro     2825:   for ( i = 0; i < nd_psn; i++ )
                   2826:     for ( j = i+1; j < nd_psn; j++ ) {
1.44      noro     2827:       sig = trivial_sig(i,j);
1.29      noro     2828:       syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
                   2829:     }
1.41      noro     2830:   for ( i = 0; i < nd_psn; i++ ) {
                   2831:     d = update_pairs_s(d,i,syzlist);
                   2832:   }
1.24      noro     2833:   sugar = 0;
1.39      noro     2834:   pos = 0;
1.26      noro     2835:   NEWDL(lcm,nd_nvar); NEWDL(quo,nd_nvar); NEWDL(mul,nd_nvar);
                   2836: init_eg(&eg_create);
                   2837: init_eg(&eg_merge);
1.29      noro     2838: init_eg(&eg_large);
1.31      noro     2839: init_eg(&eg_nf);
                   2840: init_eg(&eg_nfzero);
1.24      noro     2841:   while ( d ) {
                   2842: again:
1.26      noro     2843:     if ( DP_Print ) {
                   2844:       int len;
                   2845:       ND_pairs td;
1.28      noro     2846:       for ( td = d, len=0; td; td = td->next, len++)
                   2847:         ;
1.26      noro     2848:        if ( !(len%100) ) fprintf(asir_out,"(%d)",len);
                   2849:       }
1.24      noro     2850:     l = d; d = d->next;
1.41      noro     2851: #if 0
1.28      noro     2852:     if ( small_lcm(l) ) {
1.24      noro     2853:       if ( DP_Print ) fprintf(asir_out,"M");
1.41      noro     2854:       Nnominimal++;
1.24      noro     2855:       continue;
                   2856:     }
                   2857:     if ( SG(l) != sugar ) {
                   2858:       sugar = SG(l);
                   2859:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2860:     }
1.28      noro     2861:     sig = l->sig;
1.39      noro     2862:     if ( DP_Print && nd_sba_pot ) {
                   2863:       if ( sig->pos != pos ) {
                   2864:         fprintf(asir_out,"[%d]",sig->pos);
                   2865:         pos = sig->pos;
                   2866:       }
                   2867:     }
1.24      noro     2868:     stat = nd_sp(m,0,l,&h);
1.41      noro     2869: #else
                   2870:     l1 = find_smallest_lcm(l);
                   2871:     if ( l1 == 0 ) {
                   2872:       if ( DP_Print ) fprintf(asir_out,"M");
                   2873:       Nnominimal++;
                   2874:       continue;
                   2875:     }
                   2876:     if ( SG(l1) != sugar ) {
                   2877:       sugar = SG(l1);
                   2878:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2879:     }
                   2880:     sig = l1->sig;
                   2881:     if ( DP_Print && nd_sba_pot ) {
                   2882:       if ( sig->pos != pos ) {
                   2883:         fprintf(asir_out,"[%d]",sig->pos);
                   2884:         pos = sig->pos;
                   2885:       }
                   2886:     }
                   2887:     stat = nd_sp(m,0,l1,&h);
                   2888: #endif
1.24      noro     2889:     if ( !stat ) {
                   2890:       NEXT(l) = d; d = l;
                   2891:       d = nd_reconstruct(0,d);
                   2892:       goto again;
                   2893:     }
1.31      noro     2894: get_eg(&eg1);
1.25      noro     2895: #if USE_GEOBUCKET
1.39      noro     2896:     stat = m?nd_nf_pbucket_s(m,h,nd_ps,!nd_top&&!Top,&nf):nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.24      noro     2897: #else
1.39      noro     2898:     stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.24      noro     2899: #endif
1.31      noro     2900: get_eg(&eg2);
1.24      noro     2901:     if ( !stat ) {
                   2902:       NEXT(l) = d; d = l;
                   2903:       d = nd_reconstruct(0,d);
                   2904:       goto again;
                   2905:     } else if ( stat == -1 ) {
1.41      noro     2906:       Nnfs++;
1.24      noro     2907:       if ( DP_Print ) { printf("S"); fflush(stdout); }
1.27      noro     2908:       FREENDP(l);
1.24      noro     2909:     } else if ( nf ) {
1.41      noro     2910:       Nnfnz++;
                   2911:       if ( DP_Print ) {
                   2912:         if ( nd_sba_redundant_check ) {
                   2913:           if ( ndl_find_reducer_nonsig(HDL(nf)) >= 0 ) {
                   2914:             Nredundant++;
                   2915:             printf("R");
                   2916:           } else
                   2917:             printf("+");
                   2918:         } else
                   2919:           printf("+");
                   2920:         fflush(stdout);
                   2921:       }
1.31      noro     2922:       add_eg(&eg_nf,&eg1,&eg2);
1.24      noro     2923:       hc = HCU(nf);
                   2924:       nd_removecont(m,nf);
                   2925:       nfv = ndtondv(m,nf); nd_free(nf);
                   2926:       nh = ndv_newps(m,nfv,0);
1.26      noro     2927:
1.30      noro     2928:       d = update_pairs_s(d,nh,syzlist);
1.29      noro     2929:       nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.24      noro     2930:       FREENDP(l);
                   2931:    } else {
1.41      noro     2932:       Nnfz++;
1.31      noro     2933:       add_eg(&eg_nfzero,&eg1,&eg2);
1.24      noro     2934:      // syzygy
1.27      noro     2935: get_eg(&eg1);
1.24      noro     2936:      d = remove_spair_s(d,sig);
1.27      noro     2937: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29      noro     2938:      syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.24      noro     2939:      if ( DP_Print ) { printf("."); fflush(stdout); }
                   2940:      FREENDP(l);
                   2941:    }
                   2942:  }
1.30      noro     2943:  g = conv_ilist_s(nd_demand,0,indp);
1.26      noro     2944:  if ( DP_Print ) {
1.41      noro     2945:    printf("\nnd_sba done. nd_add=%d,Nsyz=%d,Nsamesig=%d,Nnominimal=%d\n",Nnd_add,Nsyz,Nsamesig,Nnominimal);
                   2946:    printf("Nnfnz=%d,Nnfz=%d,Nnfsingular=%d\n",Nnfnz,Nnfz,Nnfs);
                   2947:    fflush(stdout);
                   2948:    if ( nd_sba_redundant_check )
                   2949:    printf("Nredundant=%d\n",Nredundant);
1.26      noro     2950:    fflush(stdout);
                   2951:    print_eg("create",&eg_create);
                   2952:    print_eg("merge",&eg_merge);
1.27      noro     2953:    print_eg("remove",&eg_remove);
1.31      noro     2954:    print_eg("nf",&eg_nf);
                   2955:    print_eg("nfzero",&eg_nfzero);
1.27      noro     2956:    printf("\n");
1.26      noro     2957:  }
1.44      noro     2958:  if ( nd_sba_syz ) {
                   2959:    NODE hsyz,tsyz,prev;
                   2960:
                   2961:    hsyz = 0;
                   2962:    for ( i = 0; i < ngen; i++ ) {
                   2963:      tsyz = syzlist[i];
                   2964:      for ( prev = 0; tsyz != 0; prev = tsyz, tsyz = NEXT(tsyz))
                   2965:        BDY(tsyz) = (pointer)sigtodpm((SIG)BDY(tsyz));
                   2966:      if ( prev != 0 ) {
                   2967:        prev->next = hsyz; hsyz = syzlist[i];
                   2968:      }
                   2969:    }
                   2970:    *syzp = hsyz;
                   2971:  } else *syzp = 0;
1.24      noro     2972:  return g;
                   2973: }
                   2974:
1.1       noro     2975: /* splist = [[i1,i2],...] */
                   2976:
                   2977: int check_splist(int m,NODE splist)
                   2978: {
                   2979:   NODE t,p;
                   2980:   ND_pairs d,r,l;
                   2981:   int stat;
                   2982:   ND h,nf;
                   2983:
                   2984:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   2985:     p = BDY((LIST)BDY(t));
1.6       noro     2986:     NEXTND_pairs(d,r);
                   2987:     r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
                   2988:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     2989:     SG(r) = TD(LCM(r)); /* XXX */
                   2990:   }
                   2991:   if ( d ) NEXT(r) = 0;
                   2992:
1.6       noro     2993:   while ( d ) {
1.1       noro     2994: again:
1.6       noro     2995:     l = nd_minp(d,&d);
                   2996:     stat = nd_sp(m,0,l,&h);
                   2997:     if ( !stat ) {
                   2998:       NEXT(l) = d; d = l;
                   2999:       d = nd_reconstruct(0,d);
                   3000:       goto again;
                   3001:     }
1.39      noro     3002:     stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.6       noro     3003:     if ( !stat ) {
                   3004:       NEXT(l) = d; d = l;
                   3005:       d = nd_reconstruct(0,d);
                   3006:       goto again;
                   3007:     } else if ( nf ) return 0;
1.1       noro     3008:     if ( DP_Print) { printf("."); fflush(stdout); }
1.6       noro     3009:   }
1.1       noro     3010:   if ( DP_Print) { printf("done.\n"); fflush(stdout); }
                   3011:   return 1;
                   3012: }
                   3013:
                   3014: int check_splist_f4(int m,NODE splist)
                   3015: {
                   3016:   UINT *s0vect;
1.6       noro     3017:   PGeoBucket bucket;
1.1       noro     3018:   NODE p,rp0,t;
                   3019:   ND_pairs d,r,l,ll;
                   3020:   int col,stat;
                   3021:
                   3022:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   3023:     p = BDY((LIST)BDY(t));
1.6       noro     3024:     NEXTND_pairs(d,r);
                   3025:     r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
                   3026:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     3027:     SG(r) = TD(LCM(r)); /* XXX */
                   3028:   }
                   3029:   if ( d ) NEXT(r) = 0;
                   3030:
1.6       noro     3031:   while ( d ) {
                   3032:     l = nd_minsugarp(d,&d);
                   3033:     bucket = create_pbucket();
                   3034:     stat = nd_sp_f4(m,0,l,bucket);
                   3035:     if ( !stat ) {
                   3036:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   3037:       NEXT(ll) = d; d = l;
                   3038:       d = nd_reconstruct(0,d);
                   3039:       continue;
                   3040:     }
                   3041:     if ( bucket->m < 0 ) continue;
                   3042:     col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   3043:     if ( !col ) {
                   3044:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   3045:       NEXT(ll) = d; d = l;
                   3046:       d = nd_reconstruct(0,d);
                   3047:       continue;
1.1       noro     3048:     }
1.6       noro     3049:     if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
                   3050:   }
                   3051:   return 1;
1.1       noro     3052: }
                   3053:
                   3054: int do_diagonalize_trace(int sugar,int m)
                   3055: {
1.6       noro     3056:   int i,nh,stat;
                   3057:   NODE r,g,t;
                   3058:   ND h,nf,nfq,s,head;
                   3059:   NDV nfv,nfqv;
                   3060:   Q q,den,num;
                   3061:   union oNDC hc;
                   3062:   NODE node;
                   3063:   LIST l;
                   3064:   Z iq;
                   3065:   P cont,cont1;
1.1       noro     3066:
1.6       noro     3067:   for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   3068:     if ( nd_gentrace ) {
                   3069:         /* Trace = [1,index,1,1] */
                   3070:         STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   3071:         MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   3072:     }
                   3073:     /* for nd_ps */
                   3074:     s = ndvtond(m,nd_ps[i]);
                   3075:     s = nd_separate_head(s,&head);
                   3076:     stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
                   3077:     if ( !stat ) return 0;
                   3078:     nf = nd_add(m,head,nf);
                   3079:     ndv_free(nd_ps[i]);
                   3080:     nd_ps[i] = ndtondv(m,nf);
                   3081:     nd_free(nf);
                   3082:
                   3083:     /* for nd_ps_trace */
                   3084:     if ( nd_demand )
                   3085:         nfv = ndv_load(i);
                   3086:     else
                   3087:         nfv = nd_ps_trace[i];
                   3088:     s = ndvtond(0,nfv);
                   3089:     s = nd_separate_head(s,&head);
                   3090:     stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
                   3091:     if ( !stat ) return 0;
                   3092:     ndv_free(nfv);
                   3093:     hc = HCU(nf); nd_removecont(0,nf);
                   3094:     /* exact division */
1.1       noro     3095:     cont = ndc_div(0,hc,HCU(nf));
1.6       noro     3096:     if ( nd_gentrace ) finalize_tracelist(i,cont);
                   3097:     nfv = ndtondv(0,nf);
                   3098:     nd_free(nf);
                   3099:     nd_bound[i] = ndv_compute_bound(nfv);
                   3100:     register_hcf(nfv);
                   3101:     if ( nd_demand ) {
                   3102:     ndv_save(nfv,i);
                   3103:     ndv_free(nfv);
                   3104:     } else
                   3105:     nd_ps_trace[i] = nfv;
                   3106:   }
                   3107:   return 1;
1.1       noro     3108: }
                   3109:
                   3110: static struct oEGT eg_invdalg;
                   3111: struct oEGT eg_le;
                   3112:
                   3113: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
                   3114: {
                   3115:     NODE tn;
                   3116:     P p1;
                   3117:
                   3118:     for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
                   3119:         substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
                   3120:     }
                   3121:     *r = p;
                   3122: }
                   3123:
                   3124: NODE nd_gb_trace(int m,int ishomo,int **indp)
                   3125: {
1.6       noro     3126:   int i,nh,sugar,stat;
                   3127:   NODE r,g,t;
                   3128:   ND_pairs d;
                   3129:   ND_pairs l;
                   3130:   ND h,nf,nfq,s,head;
                   3131:   NDV nfv,nfqv;
                   3132:   Z q,den,num;
                   3133:   P hc;
                   3134:   union oNDC dn,hnfq;
                   3135:   struct oEGT eg_monic,egm0,egm1;
                   3136:   int diag_count = 0;
                   3137:   P cont;
                   3138:   LIST list;
                   3139:
                   3140:   init_eg(&eg_monic);
                   3141:   init_eg(&eg_invdalg);
                   3142:   init_eg(&eg_le);
                   3143:   g = 0; d = 0;
                   3144:   for ( i = 0; i < nd_psn; i++ ) {
                   3145:     d = update_pairs(d,g,i,0);
                   3146:     g = update_base(g,i);
                   3147:   }
                   3148:   sugar = 0;
                   3149:   while ( d ) {
1.1       noro     3150: again:
1.6       noro     3151:     l = nd_minp(d,&d);
                   3152:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   3153:     if ( SG(l) != sugar ) {
1.1       noro     3154: #if 1
1.6       noro     3155:       if ( ishomo ) {
                   3156:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3157:         stat = do_diagonalize_trace(sugar,m);
                   3158:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3159:         diag_count = 0;
1.1       noro     3160:         if ( !stat ) {
1.6       noro     3161:           NEXT(l) = d; d = l;
                   3162:           d = nd_reconstruct(1,d);
                   3163:           goto again;
1.1       noro     3164:         }
1.6       noro     3165:       }
                   3166: #endif
                   3167:       sugar = SG(l);
                   3168:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   3169:     }
                   3170:     stat = nd_sp(m,0,l,&h);
                   3171:     if ( !stat ) {
                   3172:       NEXT(l) = d; d = l;
                   3173:       d = nd_reconstruct(1,d);
                   3174:       goto again;
                   3175:     }
1.1       noro     3176: #if USE_GEOBUCKET
1.39      noro     3177:     stat = nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     3178: #else
1.39      noro     3179:     stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     3180: #endif
1.6       noro     3181:     if ( !stat ) {
                   3182:       NEXT(l) = d; d = l;
                   3183:       d = nd_reconstruct(1,d);
                   3184:       goto again;
                   3185:     } else if ( nf ) {
                   3186:       if ( nd_demand ) {
                   3187:         nfqv = ndv_load(nd_psn);
                   3188:         nfq = ndvtond(0,nfqv);
                   3189:       } else
                   3190:         nfq = 0;
                   3191:       if ( !nfq ) {
1.39      noro     3192:         if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!nd_top&&!Top,&nfq) ) {
1.6       noro     3193:           NEXT(l) = d; d = l;
                   3194:           d = nd_reconstruct(1,d);
                   3195:           goto again;
                   3196:         }
                   3197:       }
                   3198:       if ( nfq ) {
                   3199:         /* m|HC(nfq) => failure */
                   3200:         if ( nd_vc ) {
                   3201:           nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
                   3202:         } else
                   3203:           q = HCZ(nfq);
                   3204:         if ( !remqi((Q)q,m) ) return 0;
                   3205:
                   3206:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   3207:         hnfq = HCU(nfq);
                   3208:         if ( nd_nalg ) {
                   3209:           /* m|DN(HC(nf)^(-1)) => failure */
                   3210:           get_eg(&egm0);
                   3211:           if ( !nd_monic(m,&nfq) ) return 0;
                   3212:           get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
                   3213:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   3214:           nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
                   3215:         } else {
                   3216:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   3217:           nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
                   3218:         }
                   3219:         if ( nd_gentrace ) {
                   3220:           /* exact division */
                   3221:           cont = ndc_div(0,hnfq,HCU(nfqv));
                   3222:           if ( !UNIQ(cont) ) {
                   3223:             t = mknode(4,NULLP,NULLP,NULLP,cont);
                   3224:             MKLIST(list,t); MKNODE(t,list,nd_tracelist);
                   3225:             nd_tracelist = t;
                   3226:           }
                   3227:         }
1.24      noro     3228:         nh = ndv_newps(0,nfv,nfqv);
1.6       noro     3229:         if ( ishomo && ++diag_count == diag_period ) {
                   3230:           diag_count = 0;
                   3231:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3232:           stat = do_diagonalize_trace(sugar,m);
                   3233:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3234:           if ( !stat ) {
1.1       noro     3235:             NEXT(l) = d; d = l;
                   3236:             d = nd_reconstruct(1,d);
                   3237:             goto again;
1.6       noro     3238:           }
1.1       noro     3239:         }
1.6       noro     3240:         d = update_pairs(d,g,nh,0);
                   3241:         g = update_base(g,nh);
                   3242:       } else {
                   3243:         if ( DP_Print ) { printf("*"); fflush(stdout); }
                   3244:       }
                   3245:     } else {
                   3246:       if ( DP_Print ) { printf("."); fflush(stdout); }
1.1       noro     3247:     }
1.6       noro     3248:     FREENDP(l);
                   3249:   }
                   3250:   if ( nd_nalg ) {
                   3251:     if ( DP_Print ) {
                   3252:       print_eg("monic",&eg_monic);
                   3253:       print_eg("invdalg",&eg_invdalg);
                   3254:       print_eg("le",&eg_le);
1.1       noro     3255:     }
1.6       noro     3256:   }
1.1       noro     3257:   conv_ilist(nd_demand,1,g,indp);
1.41      noro     3258:   if ( DP_Print ) { printf("\nnd_gb_trace done.\n"); fflush(stdout); }
1.6       noro     3259:   return g;
1.1       noro     3260: }
                   3261:
                   3262: int ndv_compare(NDV *p1,NDV *p2)
                   3263: {
                   3264:     return DL_COMPARE(HDL(*p1),HDL(*p2));
                   3265: }
                   3266:
                   3267: int ndv_compare_rev(NDV *p1,NDV *p2)
                   3268: {
                   3269:     return -DL_COMPARE(HDL(*p1),HDL(*p2));
                   3270: }
                   3271:
                   3272: int ndvi_compare(NDVI p1,NDVI p2)
                   3273: {
                   3274:     return DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3275: }
                   3276:
                   3277: int ndvi_compare_rev(NDVI p1,NDVI p2)
                   3278: {
                   3279:     return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3280: }
                   3281:
                   3282: NODE ndv_reduceall(int m,NODE f)
                   3283: {
                   3284:   int i,j,n,stat;
                   3285:   ND nf,g,head;
                   3286:   NODE t,a0,a;
                   3287:   union oNDC dn;
                   3288:   Q q,num,den;
                   3289:   NODE node;
                   3290:   LIST l;
                   3291:   Z iq,jq;
                   3292:   int *perm;
                   3293:   union oNDC hc;
                   3294:   P cont,cont1;
                   3295:
                   3296:   if ( nd_nora ) return f;
                   3297:   n = length(f);
1.24      noro     3298:   ndv_setup(m,0,f,0,1,0);
1.1       noro     3299:   perm = (int *)MALLOC(n*sizeof(int));
                   3300:   if ( nd_gentrace ) {
                   3301:     for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6       noro     3302:       perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1       noro     3303:   }
                   3304:   for ( i = 0; i < n; ) {
                   3305:     if ( nd_gentrace ) {
                   3306:       /* Trace = [1,index,1,1] */
1.6       noro     3307:       STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1       noro     3308:       MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   3309:     }
                   3310:     g = ndvtond(m,nd_ps[i]);
                   3311:     g = nd_separate_head(g,&head);
1.6       noro     3312:     stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1       noro     3313:     if ( !stat )
                   3314:       nd_reconstruct(0,0);
                   3315:     else {
                   3316:       if ( DP_Print ) { printf("."); fflush(stdout); }
                   3317:       ndv_free(nd_ps[i]);
                   3318:       hc = HCU(nf); nd_removecont(m,nf);
                   3319:       if ( nd_gentrace ) {
                   3320:         for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6       noro     3321:           jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
                   3322:           STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1       noro     3323:         }
1.6       noro     3324:         /* exact division */
1.1       noro     3325:         cont = ndc_div(m,hc,HCU(nf));
                   3326:         finalize_tracelist(perm[i],cont);
                   3327:       }
                   3328:       nd_ps[i] = ndtondv(m,nf); nd_free(nf);
                   3329:       nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   3330:       i++;
                   3331:     }
                   3332:   }
                   3333:   if ( DP_Print ) { printf("\n"); }
                   3334:   for ( a0 = 0, i = 0; i < n; i++ ) {
                   3335:     NEXTNODE(a0,a);
                   3336:     if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
                   3337:     else {
                   3338:       for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
                   3339:       BDY(a) = (pointer)nd_ps[j];
                   3340:     }
                   3341:   }
                   3342:   NEXT(a) = 0;
                   3343:   return a0;
                   3344: }
                   3345:
1.41      noro     3346: int ndplength(ND_pairs d)
                   3347: {
                   3348:   int i;
                   3349:   for ( i = 0; d; i++ ) d = NEXT(d);
                   3350:   return i;
                   3351: }
                   3352:
1.1       noro     3353: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
                   3354: {
                   3355:   ND_pairs d1,nd,cur,head,prev,remove;
1.41      noro     3356:   int len0;
1.1       noro     3357:
                   3358:   if ( !g ) return d;
                   3359:   /* for testing */
                   3360:   if ( gensyz && nd_gensyz == 2 ) {
                   3361:     d1 = nd_newpairs(g,t);
                   3362:     if ( !d )
                   3363:       return d1;
                   3364:     else {
                   3365:       nd = d;
                   3366:       while ( NEXT(nd) ) nd = NEXT(nd);
                   3367:       NEXT(nd) = d1;
                   3368:       return d;
                   3369:     }
                   3370:   }
                   3371:   d = crit_B(d,t);
                   3372:   d1 = nd_newpairs(g,t);
1.41      noro     3373:   len0 = ndplength(d1);
1.1       noro     3374:   d1 = crit_M(d1);
                   3375:   d1 = crit_F(d1);
1.41      noro     3376:   NcriMF += len0-ndplength(d1);
1.1       noro     3377:   if ( gensyz || do_weyl )
                   3378:     head = d1;
                   3379:   else {
                   3380:     prev = 0; cur = head = d1;
                   3381:     while ( cur ) {
                   3382:       if ( crit_2( cur->i1,cur->i2 ) ) {
                   3383:         remove = cur;
                   3384:         if ( !prev ) head = cur = NEXT(cur);
                   3385:         else cur = NEXT(prev) = NEXT(cur);
1.41      noro     3386:         FREENDP(remove); Ncri2++;
1.1       noro     3387:       } else {
                   3388:         prev = cur; cur = NEXT(cur);
                   3389:       }
                   3390:     }
                   3391:   }
                   3392:   if ( !d )
                   3393:     return head;
                   3394:   else {
                   3395:     nd = d;
                   3396:     while ( NEXT(nd) ) nd = NEXT(nd);
                   3397:     NEXT(nd) = head;
                   3398:     return d;
                   3399:   }
                   3400: }
                   3401:
1.24      noro     3402: ND_pairs merge_pairs_s(ND_pairs d,ND_pairs d1);
                   3403:
1.30      noro     3404: ND_pairs update_pairs_s( ND_pairs d, int t,NODE *syz)
1.24      noro     3405: {
                   3406:   ND_pairs d1;
1.26      noro     3407:   struct oEGT eg1,eg2,eg3;
1.24      noro     3408:
1.30      noro     3409:   if ( !t ) return d;
1.26      noro     3410: get_eg(&eg1);
1.30      noro     3411:   d1 = nd_newpairs_s(t,syz);
1.26      noro     3412: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
1.24      noro     3413:   d = merge_pairs_s(d,d1);
1.26      noro     3414: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
1.24      noro     3415:   return d;
                   3416: }
1.1       noro     3417:
                   3418: ND_pairs nd_newpairs( NODE g, int t )
                   3419: {
                   3420:   NODE h;
                   3421:   UINT *dl;
                   3422:   int ts,s,i,t0,min,max;
                   3423:   ND_pairs r,r0;
                   3424:
                   3425:   dl = DL(nd_psh[t]);
                   3426:   ts = SG(nd_psh[t]) - TD(dl);
1.17      noro     3427:   if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1       noro     3428:   for ( r0 = 0, h = g; h; h = NEXT(h) ) {
                   3429:     if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
                   3430:       continue;
                   3431:     if ( nd_gbblock ) {
                   3432:       t0 = (long)BDY(h);
                   3433:       for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
                   3434:         min = nd_gbblock[i]; max = nd_gbblock[i+1];
                   3435:         if ( t0 >= min && t0 <= max && t >= min && t <= max )
                   3436:           break;
                   3437:       }
                   3438:       if ( nd_gbblock[i] >= 0 )
                   3439:         continue;
                   3440:     }
1.41      noro     3441:     NEXTND_pairs(r0,r); Npairs++;
1.1       noro     3442:     r->i1 = (long)BDY(h);
                   3443:     r->i2 = t;
                   3444:     ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
                   3445:     s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   3446:     SG(r) = MAX(s,ts) + TD(LCM(r));
                   3447:     /* experimental */
                   3448:     if ( nd_sugarweight )
                   3449:       r->sugar2 = ndl_weight2(r->lcm);
                   3450:   }
                   3451:   if ( r0 ) NEXT(r) = 0;
                   3452:   return r0;
                   3453: }
                   3454:
1.24      noro     3455: int comp_sig(SIG s1,SIG s2)
                   3456: {
1.38      noro     3457:   if ( nd_sba_pot ) {
                   3458:     if ( s1->pos > s2->pos ) return 1;
                   3459:     else if ( s1->pos < s2->pos ) return -1;
                   3460:     else return (*cmpdl)(nd_nvar,s1->dl,s2->dl);
                   3461:   } else {
                   3462:     static DL m1,m2;
1.44      noro     3463:     static int nvar = 0;
1.38      noro     3464:     int ret;
                   3465:
                   3466:     if ( nvar != nd_nvar ) {
                   3467:       nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
                   3468:     }
                   3469:     _adddl(nd_nvar,s1->dl,nd_sba_hm[s1->pos],m1);
                   3470:     _adddl(nd_nvar,s2->dl,nd_sba_hm[s2->pos],m2);
1.45    ! noro     3471:     if ( nd_sba_fixord )
        !          3472:       ret = cmpdl_revgradlex(nd_nvar,m1,m2);
        !          3473:     else
        !          3474:       ret = (*cmpdl)(nd_nvar,m1,m2);
1.38      noro     3475:     if ( ret != 0 ) return ret;
                   3476:     else if ( s1->pos > s2->pos ) return 1;
                   3477:     else if ( s1->pos < s2->pos ) return -1;
                   3478:     else return 0;
1.24      noro     3479:   }
                   3480: }
                   3481:
                   3482: int _create_spair_s(int i1,int i2,ND_pairs sp,SIG sig1,SIG sig2)
                   3483: {
                   3484:   int ret,s1,s2;
                   3485:   RHist p1,p2;
                   3486:   static int wpd;
                   3487:   static UINT *lcm;
                   3488:
                   3489:   sp->i1 = i1;
                   3490:   sp->i2 = i2;
                   3491:   p1 = nd_psh[i1];
                   3492:   p2 = nd_psh[i2];
                   3493:   ndl_lcm(DL(p1),DL(p2),sp->lcm);
                   3494:   s1 = SG(p1)-TD(DL(p1));
                   3495:   s2 = SG(p2)-TD(DL(p2));
                   3496:   SG(sp) = MAX(s1,s2) + TD(sp->lcm);
                   3497:
1.26      noro     3498:   if ( wpd != nd_wpd ) {
1.24      noro     3499:     wpd = nd_wpd;
                   3500:     lcm = (UINT *)MALLOC(wpd*sizeof(UINT));
                   3501:   }
                   3502:   // DL(sig1) <- sp->lcm
                   3503:   // DL(sig1) -= DL(p1)
                   3504:   // DL(sig1) += DL(p1->sig)
1.27      noro     3505:   ndl_sub(sp->lcm,DL(p1),lcm);
1.24      noro     3506:   _ndltodl(lcm,DL(sig1));
                   3507:   _addtodl(nd_nvar,DL(p1->sig),DL(sig1));
                   3508:   sig1->pos = p1->sig->pos;
                   3509:
                   3510:   // DL(sig2) <- sp->lcm
                   3511:   // DL(sig2) -= DL(p2)
                   3512:   // DL(sig2) += DL(p2->sig)
1.27      noro     3513:   ndl_sub(sp->lcm,DL(p2),lcm);
1.24      noro     3514:   _ndltodl(lcm,DL(sig2));
                   3515:   _addtodl(nd_nvar,DL(p2->sig),DL(sig2));
                   3516:   sig2->pos = p2->sig->pos;
                   3517:
                   3518:   ret = comp_sig(sig1,sig2);
                   3519:   if ( ret == 0 ) return 0;
                   3520:   else if ( ret > 0 ) sp->sig = sig1;
                   3521:   else sp->sig = sig2;
                   3522:   return 1;
                   3523: }
                   3524:
                   3525: SIG dup_sig(SIG sig)
                   3526: {
                   3527:   SIG r;
                   3528:
                   3529:   if ( !sig ) return 0;
                   3530:   else {
                   3531:     NEWSIG(r);
                   3532:     _copydl(nd_nvar,DL(sig),DL(r));
                   3533:     r->pos = sig->pos;
                   3534:     return r;
                   3535:   }
                   3536: }
                   3537:
                   3538: void dup_ND_pairs(ND_pairs to,ND_pairs from)
                   3539: {
                   3540:   to->i1 = from->i1;
                   3541:   to->i2 = from->i2;
                   3542:   to->sugar = from->sugar;
                   3543:   to->sugar2 = from->sugar2;
                   3544:   ndl_copy(from->lcm,to->lcm);
                   3545:   to->sig = dup_sig(from->sig);
                   3546: }
                   3547:
                   3548: ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
                   3549: {
                   3550:   struct oND_pairs root;
                   3551:   ND_pairs q1,q2,r0,r;
                   3552:   int ret;
                   3553:
                   3554:   r = &root;
                   3555:   for ( q1 = p1, q2 = p2; q1 != 0 && q2 != 0; ) {
                   3556:     ret = comp_sig(q1->sig,q2->sig);
                   3557:     if ( ret < 0 ) {
                   3558:       r->next = q1; r = q1; q1 = q1->next;
                   3559:     } else if ( ret > 0 ) {
                   3560:       r->next = q2; r = q2; q2 = q2->next;
                   3561:     } else {
                   3562:       ret = DL_COMPARE(q1->lcm,q2->lcm);
1.35      noro     3563:       Nsamesig++;
1.24      noro     3564:       if ( ret < 0 ) {
                   3565:         r->next = q1; r = q1; q1 = q1->next;
                   3566:         q2 = q2->next;
                   3567:       } else {
                   3568:         r->next = q2; r = q2; q2 = q2->next;
                   3569:         q1 = q1->next;
                   3570:       }
                   3571:     }
                   3572:   }
                   3573:   if ( q1 ) {
                   3574:     r->next = q1;
                   3575:   } else {
                   3576:     r->next = q2;
                   3577:   }
                   3578:   return root.next;
                   3579: }
                   3580:
                   3581: ND_pairs insert_pair_s(ND_pairs l,ND_pairs s)
                   3582: {
                   3583:   ND_pairs p,prev;
                   3584:   int ret;
                   3585:
                   3586:   for ( p = l, prev = 0; p != 0; prev = p, p = p->next ) {
                   3587:     if ( (ret = comp_sig(s->sig,p->sig)) <= 0 )
                   3588:       break;
                   3589:   }
                   3590:   if ( ret == 0 ) {
                   3591:     ret = DL_COMPARE(s->lcm,p->lcm);
                   3592:     if ( ret < 0 ) {
                   3593:       // replace p with s
                   3594:       s->next = p->next;
                   3595:       if ( prev == 0 ) {
                   3596:         return s;
                   3597:       } else {
                   3598:         prev->next = s;
                   3599:         return l;
                   3600:       }
                   3601:     } else
                   3602:       return l;
                   3603:   } else {
                   3604:     // insert s between prev and p
                   3605:     s->next = p;
                   3606:     if ( prev == 0 ) {
                   3607:       return s;
                   3608:     } else {
                   3609:       prev->next = s;
                   3610:       return l;
                   3611:     }
                   3612:   }
                   3613: }
                   3614:
1.29      noro     3615: INLINE int __dl_redble(DL d1,DL d2,int nvar)
                   3616: {
                   3617:   int i;
                   3618:
                   3619:   if ( d1->td > d2->td )
                   3620:     return 0;
                   3621:   for ( i = nvar-1; i >= 0; i-- )
                   3622:     if ( d1->d[i] > d2->d[i] )
                   3623:       break;
                   3624:   if ( i >= 0 )
                   3625:     return 0;
                   3626:   else
                   3627:     return 1;
                   3628: }
                   3629:
1.30      noro     3630: ND_pairs nd_newpairs_s(int t, NODE *syz)
1.24      noro     3631: {
                   3632:   NODE h,s;
                   3633:   UINT *dl;
1.27      noro     3634:   int ts,ret,i;
1.24      noro     3635:   ND_pairs r,r0,_sp,sp;
1.29      noro     3636:   SIG spsig,tsig;
1.44      noro     3637:   static int nvar = 0;
1.29      noro     3638:   static SIG _sig1,_sig2;
1.26      noro     3639:   struct oEGT eg1,eg2,eg3,eg4;
1.24      noro     3640:
                   3641:   NEWND_pairs(_sp);
1.29      noro     3642:   if ( !_sig1 || nvar != nd_nvar ) {
                   3643:     nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
                   3644:   }
1.24      noro     3645:   r0 = 0;
1.27      noro     3646:   for ( i = 0; i < t; i++ ) {
                   3647:     ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
1.24      noro     3648:     if ( ret ) {
                   3649:       spsig = _sp->sig;
1.29      noro     3650:       for ( s = syz[spsig->pos]; s; s = s->next ) {
1.24      noro     3651:         tsig = (SIG)s->body;
1.29      noro     3652:         if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
1.24      noro     3653:           break;
                   3654:       }
                   3655:       if ( s == 0 ) {
                   3656:         NEWND_pairs(sp);
                   3657:         dup_ND_pairs(sp,_sp);
                   3658:         r0 = insert_pair_s(r0,sp);
1.26      noro     3659:       } else
                   3660:         Nsyz++;
1.24      noro     3661:     }
                   3662:   }
                   3663:   return r0;
                   3664: }
                   3665:
1.1       noro     3666: /* ipair = [i1,i2],[i1,i2],... */
                   3667: ND_pairs nd_ipairtospair(NODE ipair)
                   3668: {
                   3669:   int s1,s2;
                   3670:   NODE tn,t;
                   3671:   ND_pairs r,r0;
                   3672:
                   3673:   for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
                   3674:     NEXTND_pairs(r0,r);
                   3675:     tn = BDY((LIST)BDY(t));
1.6       noro     3676:     r->i1 = ZTOS((Q)ARG0(tn));
                   3677:     r->i2 = ZTOS((Q)ARG1(tn));
1.1       noro     3678:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
                   3679:     s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   3680:     s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
                   3681:     SG(r) = MAX(s1,s2) + TD(LCM(r));
                   3682:     /* experimental */
                   3683:     if ( nd_sugarweight )
                   3684:         r->sugar2 = ndl_weight2(r->lcm);
                   3685:   }
                   3686:   if ( r0 ) NEXT(r) = 0;
                   3687:   return r0;
                   3688: }
                   3689:
                   3690: /* kokokara */
                   3691:
                   3692: ND_pairs crit_B( ND_pairs d, int s )
                   3693: {
                   3694:   ND_pairs cur,head,prev,remove;
                   3695:   UINT *t,*tl,*lcm;
                   3696:   int td,tdl;
                   3697:
                   3698:   if ( !d ) return 0;
                   3699:   t = DL(nd_psh[s]);
                   3700:   prev = 0;
                   3701:   head = cur = d;
                   3702:   lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   3703:   while ( cur ) {
                   3704:     tl = cur->lcm;
                   3705:     if ( ndl_reducible(tl,t) ) {
                   3706:       ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
                   3707:       if ( !ndl_equal(lcm,tl) ) {
                   3708:         ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
                   3709:         if (!ndl_equal(lcm,tl)) {
                   3710:           remove = cur;
                   3711:           if ( !prev ) {
                   3712:             head = cur = NEXT(cur);
                   3713:           } else {
                   3714:             cur = NEXT(prev) = NEXT(cur);
                   3715:           }
1.41      noro     3716:           FREENDP(remove); NcriB++;
1.1       noro     3717:         } else {
                   3718:           prev = cur; cur = NEXT(cur);
                   3719:         }
                   3720:       } else {
                   3721:         prev = cur; cur = NEXT(cur);
                   3722:       }
                   3723:     } else {
                   3724:       prev = cur; cur = NEXT(cur);
                   3725:     }
                   3726:   }
                   3727:   return head;
                   3728: }
                   3729:
                   3730: ND_pairs crit_M( ND_pairs d1 )
                   3731: {
                   3732:     ND_pairs e,d2,d3,dd,p;
                   3733:     UINT *id,*jd;
                   3734:
                   3735:     if ( !d1 ) return d1;
                   3736:     for ( dd = 0, e = d1; e; e = d3 ) {
                   3737:         if ( !(d2 = NEXT(e)) ) {
                   3738:             NEXT(e) = dd;
                   3739:             return e;
                   3740:         }
                   3741:         id = LCM(e);
                   3742:         for ( d3 = 0; d2; d2 = p ) {
                   3743:             p = NEXT(d2);
                   3744:             jd = LCM(d2);
                   3745:             if ( ndl_equal(jd,id) )
                   3746:                 ;
                   3747:             else if ( TD(jd) > TD(id) )
                   3748:                 if ( ndl_reducible(jd,id) ) continue;
                   3749:                 else ;
                   3750:             else if ( ndl_reducible(id,jd) ) goto delit;
                   3751:             NEXT(d2) = d3;
                   3752:             d3 = d2;
                   3753:         }
                   3754:         NEXT(e) = dd;
                   3755:         dd = e;
                   3756:         continue;
                   3757:         /**/
                   3758:     delit:    NEXT(d2) = d3;
                   3759:         d3 = d2;
                   3760:         for ( ; p; p = d2 ) {
                   3761:             d2 = NEXT(p);
                   3762:             NEXT(p) = d3;
                   3763:             d3 = p;
                   3764:         }
                   3765:         FREENDP(e);
                   3766:     }
                   3767:     return dd;
                   3768: }
                   3769:
                   3770: ND_pairs crit_F( ND_pairs d1 )
                   3771: {
                   3772:     ND_pairs rest, head,remove;
                   3773:     ND_pairs last, p, r, w;
                   3774:     int s;
                   3775:
                   3776:     if ( !d1 ) return d1;
                   3777:     for ( head = last = 0, p = d1; NEXT(p); ) {
                   3778:         r = w = equivalent_pairs(p,&rest);
                   3779:         s = SG(r);
                   3780:         w = NEXT(w);
                   3781:         while ( w ) {
                   3782:             if ( crit_2(w->i1,w->i2) ) {
                   3783:                 r = w;
                   3784:                 w = NEXT(w);
                   3785:                 while ( w ) {
                   3786:                     remove = w;
                   3787:                     w = NEXT(w);
                   3788:                     FREENDP(remove);
                   3789:                 }
                   3790:                 break;
                   3791:             } else if ( SG(w) < s ) {
                   3792:                 FREENDP(r);
                   3793:                 r = w;
                   3794:                 s = SG(r);
                   3795:                 w = NEXT(w);
                   3796:             } else {
                   3797:                 remove = w;
                   3798:                 w = NEXT(w);
                   3799:                 FREENDP(remove);
                   3800:             }
                   3801:         }
                   3802:         if ( last ) NEXT(last) = r;
                   3803:         else head = r;
                   3804:         NEXT(last = r) = 0;
                   3805:         p = rest;
                   3806:         if ( !p ) return head;
                   3807:     }
                   3808:     if ( !last ) return p;
                   3809:     NEXT(last) = p;
                   3810:     return head;
                   3811: }
                   3812:
                   3813: int crit_2( int dp1, int dp2 )
                   3814: {
                   3815:     return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
                   3816: }
                   3817:
                   3818: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
                   3819: {
                   3820:     ND_pairs w,p,r,s;
                   3821:     UINT *d;
                   3822:
                   3823:     w = d1;
                   3824:     d = LCM(w);
                   3825:     s = NEXT(w);
                   3826:     NEXT(w) = 0;
                   3827:     for ( r = 0; s; s = p ) {
                   3828:         p = NEXT(s);
                   3829:         if ( ndl_equal(d,LCM(s)) ) {
                   3830:             NEXT(s) = w; w = s;
                   3831:         } else {
                   3832:             NEXT(s) = r; r = s;
                   3833:         }
                   3834:     }
                   3835:     *prest = r;
                   3836:     return w;
                   3837: }
                   3838:
                   3839: NODE update_base(NODE nd,int ndp)
                   3840: {
                   3841:     UINT *dl, *dln;
                   3842:     NODE last, p, head;
                   3843:
                   3844:     dl = DL(nd_psh[ndp]);
                   3845:     for ( head = last = 0, p = nd; p; ) {
                   3846:         dln = DL(nd_psh[(long)BDY(p)]);
                   3847:         if ( ndl_reducible( dln, dl ) ) {
                   3848:             p = NEXT(p);
                   3849:             if ( last ) NEXT(last) = p;
                   3850:         } else {
                   3851:             if ( !last ) head = p;
                   3852:             p = NEXT(last = p);
                   3853:         }
                   3854:     }
                   3855:     head = append_one(head,ndp);
                   3856:     return head;
                   3857: }
                   3858:
                   3859: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   3860: {
                   3861:     ND_pairs m,ml,p,l;
                   3862:     UINT *lcm;
                   3863:     int s,td,len,tlen,c,c1;
                   3864:
                   3865:     if ( !(p = NEXT(m = d)) ) {
                   3866:         *prest = p;
                   3867:         NEXT(m) = 0;
                   3868:         return m;
                   3869:     }
                   3870:     if ( !NoSugar ) {
                   3871:         if ( nd_sugarweight ) {
                   3872:             s = m->sugar2;
                   3873:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   3874:                 if ( (p->sugar2 < s)
                   3875:                     || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   3876:                     ml = l; m = p; s = m->sugar2;
                   3877:                 }
                   3878:         } else {
                   3879:             s = SG(m);
                   3880:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   3881:                 if ( (SG(p) < s)
                   3882:                     || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   3883:                     ml = l; m = p; s = SG(m);
                   3884:                 }
                   3885:         }
                   3886:     } else {
                   3887:         for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   3888:             if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
                   3889:                 ml = l; m = p; s = SG(m);
                   3890:             }
                   3891:     }
                   3892:     if ( !ml ) *prest = NEXT(m);
                   3893:     else {
                   3894:         NEXT(ml) = NEXT(m);
                   3895:         *prest = d;
                   3896:     }
                   3897:     NEXT(m) = 0;
                   3898:     return m;
                   3899: }
                   3900:
                   3901: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   3902: {
                   3903:     int msugar,i;
                   3904:     ND_pairs t,dm0,dm,dr0,dr;
                   3905:
                   3906:     if ( nd_sugarweight ) {
                   3907:         for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
                   3908:             if ( t->sugar2 < msugar ) msugar = t->sugar2;
                   3909:         dm0 = 0; dr0 = 0;
                   3910:         for ( i = 0, t = d; t; t = NEXT(t) )
                   3911:             if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
                   3912:                 if ( dm0 ) NEXT(dm) = t;
                   3913:                 else dm0 = t;
                   3914:                 dm = t;
                   3915:                 i++;
                   3916:             } else {
                   3917:                 if ( dr0 ) NEXT(dr) = t;
                   3918:                 else dr0 = t;
                   3919:                 dr = t;
                   3920:             }
                   3921:     } else {
                   3922:         for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   3923:             if ( SG(t) < msugar ) msugar = SG(t);
                   3924:         dm0 = 0; dr0 = 0;
                   3925:         for ( i = 0, t = d; t; t = NEXT(t) )
                   3926:             if ( i < nd_f4_nsp && SG(t) == msugar ) {
                   3927:                 if ( dm0 ) NEXT(dm) = t;
                   3928:                 else dm0 = t;
                   3929:                 dm = t;
                   3930:                 i++;
                   3931:             } else {
                   3932:                 if ( dr0 ) NEXT(dr) = t;
                   3933:                 else dr0 = t;
                   3934:                 dr = t;
                   3935:             }
                   3936:     }
                   3937:     NEXT(dm) = 0;
                   3938:     if ( dr0 ) NEXT(dr) = 0;
                   3939:     *prest = dr0;
                   3940:     return dm0;
                   3941: }
                   3942:
1.28      noro     3943: ND_pairs nd_minsugarp_s( ND_pairs d, ND_pairs *prest )
                   3944: {
                   3945:   int msugar;
                   3946:   ND_pairs t,last;
                   3947:
                   3948:   for ( msugar = SG(d), t = d; t; t = NEXT(t) )
                   3949:     if ( SG(t) == msugar ) last = t;
                   3950:   *prest = last->next;
                   3951:   last->next = 0;
                   3952:   return d;
                   3953: }
                   3954:
1.1       noro     3955: int nd_tdeg(NDV c)
                   3956: {
                   3957:   int wmax = 0;
                   3958:   int i,len;
                   3959:   NMV a;
                   3960:
                   3961:   len = LEN(c);
                   3962:   for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   3963:     wmax = MAX(TD(DL(a)),wmax);
                   3964:   return wmax;
                   3965: }
                   3966:
1.24      noro     3967: int ndv_newps(int m,NDV a,NDV aq)
1.1       noro     3968: {
                   3969:     int len;
                   3970:     RHist r;
                   3971:     NDV b;
                   3972:     NODE tn;
                   3973:     LIST l;
                   3974:     Z iq;
                   3975:
                   3976:     if ( nd_psn == nd_pslen ) {
                   3977:         nd_pslen *= 2;
                   3978:         nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
                   3979:         nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
                   3980:         nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
                   3981:         nd_bound = (UINT **)
                   3982:             REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
                   3983:         nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
                   3984:         nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
                   3985:     }
                   3986:     NEWRHist(r); nd_psh[nd_psn] = r;
                   3987:     nd_ps[nd_psn] = a;
                   3988:     if ( aq ) {
                   3989:         nd_ps_trace[nd_psn] = aq;
                   3990:         if ( !m ) {
                   3991:           register_hcf(aq);
                   3992:         } else if ( m == -2 ) {
                   3993:           /* do nothing */
                   3994:         } else
                   3995:           error("ndv_newps : invalud modulus");
                   3996:         nd_bound[nd_psn] = ndv_compute_bound(aq);
                   3997: #if 1
                   3998:         SG(r) = SG(aq);
                   3999: #else
                   4000:         SG(r) = nd_tdeg(aq);
                   4001: #endif
                   4002:         ndl_copy(HDL(aq),DL(r));
1.24      noro     4003:         r->sig = dup_sig(aq->sig);
1.1       noro     4004:     } else {
                   4005:         if ( !m ) register_hcf(a);
                   4006:         nd_bound[nd_psn] = ndv_compute_bound(a);
                   4007: #if 1
                   4008:         SG(r) = SG(a);
                   4009: #else
                   4010:         SG(r) = nd_tdeg(a);
                   4011: #endif
                   4012:         ndl_copy(HDL(a),DL(r));
1.24      noro     4013:         r->sig = dup_sig(a->sig);
1.1       noro     4014:     }
                   4015:     if ( nd_demand ) {
                   4016:         if ( aq ) {
                   4017:             ndv_save(nd_ps_trace[nd_psn],nd_psn);
                   4018:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   4019:             nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   4020:             nd_ps_trace[nd_psn] = 0;
                   4021:         } else {
                   4022:             ndv_save(nd_ps[nd_psn],nd_psn);
                   4023:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
                   4024:             nd_ps[nd_psn] = 0;
                   4025:         }
                   4026:     }
                   4027:     if ( nd_gentrace ) {
                   4028:         /* reverse the tracelist and append it to alltracelist */
                   4029:         nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6       noro     4030:         STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1       noro     4031:         MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
                   4032:     }
                   4033:     return nd_psn++;
                   4034: }
                   4035:
1.45    ! noro     4036: // find LM wrt grevlex
        !          4037: void ndv_lm_fixord(NDV p,DL d)
        !          4038: {
        !          4039:   NMV m;
        !          4040:   DL tmp;
        !          4041:   int len,i,ret;
        !          4042:
        !          4043:   NEWDL(tmp,nd_nvar);
        !          4044:   m = BDY(p); len = LEN(p);
        !          4045:   _ndltodl(DL(m),d); printdl(d); printf("->");
        !          4046:   for ( i = 1, NMV_ADV(m); i < len; i++, NMV_ADV(m) ) {
        !          4047:     _ndltodl(DL(m),tmp);
        !          4048:     ret = cmpdl_revgradlex(nd_nvar,tmp,d);
        !          4049:     if ( ret > 0 ) _copydl(nd_nvar,tmp,d);
        !          4050:   }
        !          4051:   printdl(d); printf("\n");
        !          4052: }
        !          4053:
1.1       noro     4054: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
                   4055: /* return 1 if success, 0 if failure (HC(a mod p)) */
                   4056:
1.24      noro     4057: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont,int sba)
1.1       noro     4058: {
1.6       noro     4059:   int i,j,td,len,max;
                   4060:   NODE s,s0,f0,tn;
                   4061:   UINT *d;
                   4062:   RHist r;
                   4063:   NDVI w;
                   4064:   NDV a,am;
                   4065:   union oNDC hc;
                   4066:   NODE node;
                   4067:   P hcp;
                   4068:   Z iq,jq;
                   4069:   LIST l;
                   4070:
                   4071:   nd_found = 0; nd_notfirst = 0; nd_create = 0;
                   4072:   /* initialize the tracelist */
                   4073:   nd_tracelist = 0;
                   4074:
                   4075:   for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
                   4076:   w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
                   4077:   for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
                   4078:     if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
                   4079:   if ( !dont_sort ) {
                   4080:     /* XXX heuristic */
1.26      noro     4081:     if ( !sba && !nd_ord->id && (nd_ord->ord.simple<2) )
1.6       noro     4082:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   4083:         (int (*)(const void *,const void *))ndvi_compare_rev);
                   4084:     else
                   4085:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   4086:         (int (*)(const void *,const void *))ndvi_compare);
                   4087:   }
                   4088:   nd_pslen = 2*nd_psn;
                   4089:   nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4090:   nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4091:   nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4092:   nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4093:   nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
                   4094:   nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
                   4095:   nd_hcf = 0;
1.1       noro     4096:
1.6       noro     4097:   if ( trace && nd_vc )
                   4098:     makesubst(nd_vc,&nd_subst);
                   4099:   else
                   4100:     nd_subst = 0;
1.1       noro     4101:
1.6       noro     4102:   if ( !nd_red )
                   4103:     nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   4104:   for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
                   4105:   for ( i = 0; i < nd_psn; i++ ) {
                   4106:     hc = HCU(w[i].p);
                   4107:     if ( trace ) {
                   4108:       if ( mod == -2 ) {
                   4109:         /* over a large finite field */
                   4110:         /* trace = small modulus */
                   4111:         a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
                   4112:         ndv_mod(-2,a);
                   4113:         if ( !dont_removecont) ndv_removecont(-2,a);
                   4114:         am = nd_ps[i] = ndv_dup(trace,w[i].p);
                   4115:         ndv_mod(trace,am);
                   4116:       if ( DL_COMPARE(HDL(am),HDL(a)) )
                   4117:         return 0;
                   4118:         ndv_removecont(trace,am);
                   4119:       } else {
                   4120:         a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
                   4121:         if ( !dont_removecont) ndv_removecont(0,a);
                   4122:         register_hcf(a);
                   4123:         am = nd_ps[i] = ndv_dup(mod,a);
                   4124:         ndv_mod(mod,am);
                   4125:       if ( DL_COMPARE(HDL(am),HDL(a)) )
                   4126:         return 0;
                   4127:         ndv_removecont(mod,am);
                   4128:       }
                   4129:     } else {
                   4130:       a = nd_ps[i] = ndv_dup(mod,w[i].p);
                   4131:       if ( mod || !dont_removecont ) ndv_removecont(mod,a);
                   4132:       if ( !mod ) register_hcf(a);
1.1       noro     4133:     }
1.6       noro     4134:     if ( nd_gentrace ) {
                   4135:       STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
                   4136:       /* exact division */
1.1       noro     4137:       if ( !dont_removecont )
1.6       noro     4138:         ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
                   4139:       MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
                   4140:     }
                   4141:     NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
                   4142:     nd_bound[i] = ndv_compute_bound(a);
                   4143:     nd_psh[i] = r;
                   4144:     if ( nd_demand ) {
                   4145:       if ( trace ) {
                   4146:         ndv_save(nd_ps_trace[i],i);
                   4147:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   4148:         nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   4149:         nd_ps_trace[i] = 0;
                   4150:       } else {
                   4151:         ndv_save(nd_ps[i],i);
                   4152:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
                   4153:         nd_ps[i] = 0;
                   4154:       }
1.1       noro     4155:     }
1.6       noro     4156:   }
1.24      noro     4157:   if ( sba ) {
1.27      noro     4158:     nd_sba_hm = (DL *)MALLOC(nd_psn*sizeof(DL));
1.24      noro     4159:    // setup signatures
1.27      noro     4160:     for ( i = 0; i < nd_psn; i++ ) {
                   4161:       SIG sig;
1.24      noro     4162:
1.27      noro     4163:       NEWSIG(sig); sig->pos = i;
                   4164:       nd_ps[i]->sig = sig;
                   4165:       if ( nd_demand ) nd_ps_sym[i]->sig = sig;
                   4166:         nd_psh[i]->sig = sig;
                   4167:       if ( trace ) {
                   4168:         nd_ps_trace[i]->sig = sig;
                   4169:         if ( nd_demand ) nd_ps_trace_sym[i]->sig = sig;
                   4170:       }
                   4171:       NEWDL(nd_sba_hm[i],nd_nvar);
1.45    ! noro     4172:       if ( nd_sba_fixord )
        !          4173:         ndv_lm_fixord(nd_ps[i],nd_sba_hm[i]);
        !          4174:       else
        !          4175:         _ndltodl(DL(nd_psh[i]),nd_sba_hm[i]);
1.27      noro     4176:     }
1.29      noro     4177:     nd_sba_pos = (NODE *)MALLOC(nd_psn*sizeof(NODE));
                   4178:     for ( i = 0; i < nd_psn; i++ ) {
                   4179:       j = nd_psh[i]->sig->pos;
                   4180:       nd_sba_pos[j] = append_one(nd_sba_pos[j],i);
                   4181:     }
1.24      noro     4182:   }
1.6       noro     4183:   if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
                   4184:   return 1;
1.1       noro     4185: }
                   4186:
                   4187: struct order_spec *append_block(struct order_spec *spec,
                   4188:     int nv,int nalg,int ord);
                   4189:
                   4190: extern VECT current_dl_weight_vector_obj;
                   4191: static VECT prev_weight_vector_obj;
                   4192:
                   4193: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
                   4194:     struct order_spec **ord1p,LIST *f1p,NODE *alistp)
                   4195: {
                   4196:     NODE alist,t,s,r0,r,arg;
                   4197:     VL tv;
                   4198:     P poly;
                   4199:     DP d;
                   4200:     Alg alpha,dp;
                   4201:     DAlg inv,da,hc;
                   4202:     MP m;
                   4203:     int i,nvar,nalg,n;
                   4204:     NumberField nf;
                   4205:     LIST f1,f2;
                   4206:     struct order_spec *current_spec;
                   4207:     VECT obj,obj0;
                   4208:     VECT tmp;
                   4209:
                   4210:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
                   4211:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
                   4212:
                   4213:     for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
                   4214:         NEXTNODE(alist,t); MKV(tv->v,poly);
                   4215:         MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
                   4216:         tv->v = tv->v->priv;
                   4217:     }
                   4218:     NEXT(t) = 0;
                   4219:
                   4220:     /* simplification, making polynomials monic */
                   4221:     setfield_dalg(alist);
                   4222:     obj_algtodalg((Obj)f,(Obj *)&f1);
                   4223:     for ( t = BDY(f); t; t = NEXT(t) ) {
                   4224:         initd(ord); ptod(vv,vv,(P)BDY(t),&d);
                   4225:         hc = (DAlg)BDY(d)->c;
                   4226:         if ( NID(hc) == N_DA ) {
                   4227:             invdalg(hc,&inv);
                   4228:             for ( m = BDY(d); m; m = NEXT(m) ) {
                   4229:                 muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
                   4230:             }
                   4231:         }
                   4232:         initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
                   4233:     }
                   4234:     obj_dalgtoalg((Obj)f1,(Obj *)&f);
                   4235:
                   4236:     /* append alg vars to the var list */
                   4237:     for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
                   4238:     NEXT(tv) = av;
                   4239:
                   4240:     /* append a block to ord */
                   4241:     *ord1p = append_block(ord,nvar,nalg,2);
                   4242:
                   4243:     /* create generator list */
                   4244:     nf = get_numberfield();
                   4245:     for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
                   4246:         MKAlg(nf->defpoly[i],dp);
                   4247:         MKNODE(s,dp,t); t = s;
                   4248:     }
                   4249:     MKLIST(f1,t);
                   4250:     *alistp = alist;
                   4251:     algobjtorat((Obj)f1,(Obj *)f1p);
                   4252:
                   4253:     /* creating a new weight vector */
                   4254:     prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
                   4255:     n = nvar+nalg+1;
                   4256:     MKVECT(obj,n);
                   4257:     if ( obj0 && obj0->len == nvar )
                   4258:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
                   4259:     else
                   4260:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
                   4261:     for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
                   4262:     BDY(obj)[n-1] = (pointer)ONE;
                   4263:     arg = mknode(1,obj);
                   4264:     Pdp_set_weight(arg,&tmp);
                   4265: }
                   4266:
                   4267: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
                   4268: {
                   4269:     NODE s,t,u0,u;
                   4270:     P p;
                   4271:     VL tv;
                   4272:     Obj obj;
                   4273:     VECT tmp;
                   4274:     NODE arg;
                   4275:
                   4276:     u0 = 0;
                   4277:     for ( t = r; t; t = NEXT(t) ) {
                   4278:         p = (P)BDY(t);
                   4279:         for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
                   4280:             substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
                   4281:         }
                   4282:         if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
                   4283:             NEXTNODE(u0,u);
                   4284:             BDY(u) = (pointer)p;
                   4285:         }
                   4286:     }
                   4287:     arg = mknode(1,prev_weight_vector_obj);
                   4288:     Pdp_set_weight(arg,&tmp);
                   4289:
                   4290:     return u0;
                   4291: }
                   4292:
                   4293: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
                   4294: {
                   4295:     VL tv,fv,vv,vc,av;
                   4296:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4297:     int e,max,nvar,i;
                   4298:     NDV b;
                   4299:     int ishomo,nalg,mrank,trank,wmax,len;
                   4300:   NMV a;
                   4301:     Alg alpha,dp;
                   4302:     P p,zp;
                   4303:     Q dmy;
                   4304:     LIST f1,f2,zpl;
                   4305:     Obj obj;
                   4306:     NumberField nf;
                   4307:     struct order_spec *ord1;
                   4308:     NODE tr,tl1,tl2,tl3,tl4,nzlist;
                   4309:     LIST l1,l2,l3,l4,l5;
                   4310:   int j;
                   4311:   Z jq,bpe,last_nonzero;
                   4312:     int *perm;
                   4313:     EPOS oepos;
                   4314:     int obpe,oadv,ompos,cbpe;
1.15      noro     4315:     VECT hvect;
1.1       noro     4316:
1.41      noro     4317:     NcriB = NcriMF = Ncri2 = 0;
1.1       noro     4318:     nd_module = 0;
                   4319:     if ( !m && Demand ) nd_demand = 1;
                   4320:     else nd_demand = 0;
                   4321:     parse_nd_option(current_option);
                   4322:
                   4323:     if ( DP_Multiple )
                   4324:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4325: #if 0
                   4326:     ndv_alloc = 0;
                   4327: #endif
                   4328:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4329:     if ( m && nd_vc )
                   4330:        error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
                   4331:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4332:     switch ( ord->id ) {
                   4333:         case 1:
                   4334:             if ( ord->nv != nvar )
                   4335:                 error("nd_{gr,f4} : invalid order specification");
                   4336:             break;
                   4337:         default:
                   4338:             break;
                   4339:     }
                   4340:     nd_nalg = 0;
                   4341:     av = 0;
                   4342:     if ( !m ) {
                   4343:         get_algtree((Obj)f,&av);
                   4344:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4345:         nd_ntrans = nvar;
                   4346:         nd_nalg = nalg;
                   4347:         /* #i -> t#i */
                   4348:         if ( nalg ) {
                   4349:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4350:             ord = ord1;
                   4351:             f = f1;
                   4352:         }
                   4353:         nvar += nalg;
                   4354:     }
                   4355:     nd_init_ord(ord);
                   4356:     mrank = 0;
                   4357:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4358:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4359:             if ( nd_module ) {
1.16      noro     4360:                 if ( OID(BDY(t)) == O_DPM ) {
                   4361:                   e = dpm_getdeg((DPM)BDY(t),&trank);
                   4362:                   max = MAX(e,max);
                   4363:                   mrank = MAX(mrank,trank);
                   4364:                 } else {
                   4365:                   s = BDY((LIST)BDY(t));
                   4366:                   trank = length(s);
                   4367:                   mrank = MAX(mrank,trank);
                   4368:                   for ( ; s; s = NEXT(s) ) {
                   4369:                       e = getdeg(tv->v,(P)BDY(s));
                   4370:                       max = MAX(e,max);
                   4371:                   }
1.1       noro     4372:                 }
                   4373:             } else {
1.43      noro     4374:                 if ( OID(BDY(t)) == O_DP ) {
                   4375:                   e = dp_getdeg((DP)BDY(t));
                   4376:                   max = MAX(e,max);
                   4377:                 } else {
                   4378:                   e = getdeg(tv->v,(P)BDY(t));
                   4379:                   max = MAX(e,max);
                   4380:                 }
1.1       noro     4381:             }
                   4382:         }
                   4383:     nd_setup_parameters(nvar,nd_nzlist?0:max);
                   4384:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   4385:     ishomo = 1;
                   4386:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4387:       if ( nd_module ) {
1.16      noro     4388:         if ( OID(BDY(t)) == O_DPM ) {
                   4389:           Z cont;
                   4390:           DPM zdpm;
                   4391:
                   4392:           if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
                   4393:           else zdpm = (DPM)BDY(t);
                   4394:           b = (pointer)dpmtondv(m,zdpm);
                   4395:         } else {
                   4396:           if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   4397:           else zpl = (LIST)BDY(t);
1.1       noro     4398:           b = (pointer)pltondv(CO,vv,zpl);
1.16      noro     4399:         }
1.1       noro     4400:       } else {
1.43      noro     4401:         if ( OID(BDY(t)) == O_DP ) {
                   4402:           DP zdp;
                   4403:
                   4404:           if ( !m && !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
                   4405:           else zdp = (DP)BDY(t);
                   4406:           b = (pointer)dptondv(m,zdp);
                   4407:         } else {
                   4408:           if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   4409:           else zp = (P)BDY(t);
                   4410:           b = (pointer)ptondv(CO,vv,zp);
                   4411:         }
1.1       noro     4412:       }
                   4413:       if ( ishomo )
                   4414:         ishomo = ishomo && ndv_ishomo(b);
                   4415:       if ( m ) ndv_mod(m,b);
                   4416:       if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4417:     }
                   4418:     if ( fd0 ) NEXT(fd) = 0;
                   4419:
                   4420:   if ( !ishomo && homo ) {
                   4421:         for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   4422:             b = (NDV)BDY(t); len = LEN(b);
                   4423:             for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   4424:                 wmax = MAX(TD(DL(a)),wmax);
                   4425:         }
                   4426:         homogenize_order(ord,nvar,&ord1);
                   4427:         nd_init_ord(ord1);
                   4428:         nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   4429:         for ( t = fd0; t; t = NEXT(t) )
                   4430:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   4431:     }
                   4432:
1.24      noro     4433:     ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0,0);
1.1       noro     4434:     if ( nd_gentrace ) {
                   4435:         MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   4436:     }
                   4437:   if ( nd_splist ) {
                   4438:     *rp = compute_splist();
                   4439:     return;
                   4440:   }
                   4441:   if ( nd_check_splist ) {
                   4442:         if ( f4 ) {
                   4443:             if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
                   4444:             else *rp = 0;
                   4445:         } else {
                   4446:             if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
                   4447:             else *rp = 0;
                   4448:         }
                   4449:     return;
                   4450:   }
                   4451:     x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
                   4452:   if ( !x ) {
                   4453:     *rp = 0; return;
                   4454:   }
1.15      noro     4455:   if ( nd_gentrace ) {
                   4456:     MKVECT(hvect,nd_psn);
                   4457:     for ( i = 0; i < nd_psn; i++ )
                   4458:        ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   4459:   }
1.1       noro     4460:   if ( !ishomo && homo ) {
                   4461:        /* dehomogenization */
                   4462:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   4463:     nd_init_ord(ord);
                   4464:     nd_setup_parameters(nvar,0);
                   4465:   }
                   4466:     nd_demand = 0;
                   4467:   if ( nd_module && nd_intersect ) {
                   4468:     for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17      noro     4469:       if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1       noro     4470:         MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
                   4471:       }
                   4472:     conv_ilist(nd_demand,0,x,0);
                   4473:     goto FINAL;
                   4474:   }
                   4475:     if ( nd_gentrace  && f4 ) { nzlist = nd_alltracelist; }
                   4476:     x = ndv_reducebase(x,perm);
                   4477:     if ( nd_gentrace  && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
                   4478:     x = ndv_reduceall(m,x);
                   4479:     cbpe = nd_bpe;
                   4480:     if ( nd_gentrace && !f4 ) {
                   4481:         tl2 = nd_alltracelist; nd_alltracelist = 0;
                   4482:         ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
                   4483:         tl3 = nd_alltracelist; nd_alltracelist = 0;
                   4484:         if ( nd_gensyz ) {
                   4485:           nd_gb(m,0,1,1,0);
                   4486:             tl4 = nd_alltracelist; nd_alltracelist = 0;
                   4487:         } else tl4 = 0;
                   4488:     }
                   4489:     nd_bpe = cbpe;
                   4490:     nd_setup_parameters(nd_nvar,0);
                   4491: FINAL:
                   4492:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16      noro     4493:       NEXTNODE(r0,r);
                   4494:       if ( nd_module ) {
                   4495:         if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
                   4496:         else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
                   4497:       } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
                   4498:       else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1       noro     4499:     }
                   4500:     if ( r0 ) NEXT(r) = 0;
                   4501:     if ( !m && nd_nalg )
                   4502:         r0 = postprocess_algcoef(av,alist,r0);
                   4503:     MKLIST(*rp,r0);
                   4504:     if ( nd_gentrace ) {
                   4505:   if ( f4 ) {
1.6       noro     4506:             STOZ(16,bpe);
                   4507:             STOZ(nd_last_nonzero,last_nonzero);
1.15      noro     4508:             tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1       noro     4509:         } else {
                   4510:             tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   4511:             tl3 = reverse_node(tl3);
                   4512:             /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   4513:             for ( t = tl2; t; t = NEXT(t) ) {
                   4514:             /* s = [i,[*,j,*,*],...] */
                   4515:                 s = BDY((LIST)BDY(t));
1.6       noro     4516:                 j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     4517:                 for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     4518:                     j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     4519:                     ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   4520:                 }
                   4521:             }
                   4522:             for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6       noro     4523:                 STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     4524:             }
                   4525:             MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   4526:             MKLIST(l5,tl4);
1.6       noro     4527:             STOZ(nd_bpe,bpe);
1.15      noro     4528:             tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     4529:         }
                   4530:     }
                   4531: #if 0
                   4532:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   4533: #endif
                   4534: }
                   4535:
1.28      noro     4536: NODE nd_sba_f4(int m,int **indp);
                   4537:
                   4538: void nd_sba(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.24      noro     4539: {
                   4540:   VL tv,fv,vv,vc,av;
1.44      noro     4541:   NODE fd,fd0,r,r0,t,x,s,xx,nd,syz;
1.24      noro     4542:   int e,max,nvar,i;
                   4543:   NDV b;
                   4544:   int ishomo,nalg,wmax,len;
                   4545:   NMV a;
                   4546:   P p,zp;
                   4547:   Q dmy;
                   4548:   struct order_spec *ord1;
                   4549:   int j;
                   4550:   int *perm;
                   4551:   EPOS oepos;
                   4552:   int obpe,oadv,ompos,cbpe;
1.29      noro     4553:   struct oEGT eg0,eg1,egconv;
1.24      noro     4554:
                   4555:   nd_module = 0;
                   4556:   nd_demand = 0;
                   4557:   parse_nd_option(current_option);
1.35      noro     4558:   Nsamesig = 0;
1.24      noro     4559:   if ( DP_Multiple )
                   4560:     nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4561:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4562:   if ( m && nd_vc )
                   4563:     error("nd_sba : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
                   4564:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4565:   switch ( ord->id ) {
                   4566:     case 1:
                   4567:       if ( ord->nv != nvar )
                   4568:         error("nd_sba : invalid order specification");
                   4569:         break;
                   4570:       default:
                   4571:         break;
                   4572:   }
                   4573:   nd_nalg = 0;
                   4574:   nd_init_ord(ord);
                   4575:   // for SIG comparison
                   4576:   initd(ord);
                   4577:   for ( t = BDY(f), max = 1; t; t = NEXT(t) ) {
                   4578:     for ( tv = vv; tv; tv = NEXT(tv) ) {
1.43      noro     4579:       if ( OID(BDY(t)) == O_DP ) {
                   4580:         e = dp_getdeg((DP)BDY(t));
                   4581:         max = MAX(e,max);
                   4582:       } else {
                   4583:         e = getdeg(tv->v,(P)BDY(t));
                   4584:         max = MAX(e,max);
                   4585:       }
1.24      noro     4586:     }
                   4587:   }
1.25      noro     4588:   nd_setup_parameters(nvar,max);
1.24      noro     4589:   obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   4590:   ishomo = 1;
                   4591:   for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.43      noro     4592:     if ( OID(BDY(t)) == O_DP ) {
                   4593:       DP zdp;
                   4594:
                   4595:       if ( !m ) dp_ptozp((DP)BDY(t),&zdp);
                   4596:       else zdp = (DP)BDY(t);
                   4597:       b = (pointer)dptondv(m,zdp);
                   4598:     } else {
                   4599:       if ( !m ) ptozp((P)BDY(t),1,&dmy,&zp);
                   4600:       else zp = (P)BDY(t);
                   4601:       b = (pointer)ptondv(CO,vv,zp);
                   4602:     }
1.24      noro     4603:     if ( ishomo )
                   4604:       ishomo = ishomo && ndv_ishomo(b);
                   4605:     if ( m ) ndv_mod(m,b);
                   4606:     if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4607:   }
                   4608:   if ( fd0 ) NEXT(fd) = 0;
                   4609:
                   4610:   if ( !ishomo && homo ) {
                   4611:     for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   4612:       b = (NDV)BDY(t); len = LEN(b);
                   4613:       for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   4614:         wmax = MAX(TD(DL(a)),wmax);
                   4615:       }
                   4616:       homogenize_order(ord,nvar,&ord1);
                   4617:       nd_init_ord(ord1);
1.42      noro     4618:       // for SIG comparison
                   4619:       initd(ord1);
1.24      noro     4620:       nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   4621:       for ( t = fd0; t; t = NEXT(t) )
                   4622:         ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   4623:   }
                   4624:
1.39      noro     4625:   ndv_setup(m,0,fd0,nd_sba_dontsort,0,1);
1.44      noro     4626:   x = f4 ? nd_sba_f4(m,&perm) : nd_sba_buch(m,ishomo || homo,&perm,&syz);
1.24      noro     4627:   if ( !x ) {
                   4628:     *rp = 0; return;
                   4629:   }
                   4630:   if ( !ishomo && homo ) {
                   4631:        /* dehomogenization */
                   4632:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   4633:     nd_init_ord(ord);
1.42      noro     4634:     // for SIG comparison
                   4635:     initd(ord);
1.24      noro     4636:     nd_setup_parameters(nvar,0);
                   4637:   }
                   4638:   nd_demand = 0;
                   4639:   x = ndv_reducebase(x,perm);
                   4640:   x = ndv_reduceall(m,x);
                   4641:   nd_setup_parameters(nd_nvar,0);
1.29      noro     4642:   get_eg(&eg0);
1.24      noro     4643:   for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   4644:     NEXTNODE(r0,r);
                   4645:     if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
1.34      noro     4646:     else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.24      noro     4647:   }
                   4648:   if ( r0 ) NEXT(r) = 0;
1.44      noro     4649:   if ( nd_sba_syz ) {
                   4650:     LIST gb,hsyz;
                   4651:     NODE nd;
                   4652:
                   4653:     MKLIST(gb,r0);
                   4654:     MKLIST(hsyz,syz);
                   4655:     nd = mknode(2,gb,hsyz);
                   4656:     MKLIST(*rp,nd);
                   4657:   } else
                   4658:     MKLIST(*rp,r0);
1.29      noro     4659:   get_eg(&eg1); init_eg(&egconv); add_eg(&egconv,&eg0,&eg1);
                   4660:   print_eg("conv",&egconv); fprintf(asir_out,"\n");
1.24      noro     4661: }
                   4662:
1.1       noro     4663: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
                   4664: {
                   4665:     VL tv,fv,vv,vc,av;
                   4666:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4667:     int e,max,nvar,i;
                   4668:     NDV b;
                   4669:     int ishomo,nalg;
                   4670:     Alg alpha,dp;
                   4671:     P p,zp;
                   4672:     Q dmy;
                   4673:     LIST f1,f2;
                   4674:     Obj obj;
                   4675:     NumberField nf;
                   4676:     struct order_spec *ord1;
                   4677:     int *perm;
                   4678:
                   4679:     parse_nd_option(current_option);
                   4680:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4681:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4682:     switch ( ord->id ) {
                   4683:         case 1:
                   4684:             if ( ord->nv != nvar )
                   4685:                 error("nd_check : invalid order specification");
                   4686:             break;
                   4687:         default:
                   4688:             break;
                   4689:     }
                   4690:     nd_nalg = 0;
                   4691:     av = 0;
                   4692:     if ( !m ) {
                   4693:         get_algtree((Obj)f,&av);
                   4694:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4695:         nd_ntrans = nvar;
                   4696:         nd_nalg = nalg;
                   4697:         /* #i -> t#i */
                   4698:         if ( nalg ) {
                   4699:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4700:             ord = ord1;
                   4701:             f = f1;
                   4702:         }
                   4703:         nvar += nalg;
                   4704:     }
                   4705:     nd_init_ord(ord);
                   4706:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4707:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4708:             e = getdeg(tv->v,(P)BDY(t));
                   4709:             max = MAX(e,max);
                   4710:         }
                   4711:     nd_setup_parameters(nvar,max);
                   4712:     ishomo = 1;
                   4713:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4714:         ptozp((P)BDY(t),1,&dmy,&zp);
                   4715:         b = (pointer)ptondv(CO,vv,zp);
                   4716:         if ( ishomo )
                   4717:             ishomo = ishomo && ndv_ishomo(b);
                   4718:         if ( m ) ndv_mod(m,b);
                   4719:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4720:     }
                   4721:     if ( fd0 ) NEXT(fd) = 0;
1.24      noro     4722:     ndv_setup(m,0,fd0,0,1,0);
1.1       noro     4723:     for ( x = 0, i = 0; i < nd_psn; i++ )
                   4724:         x = update_base(x,i);
                   4725:     if ( do_check ) {
                   4726:         x = nd_gb(m,ishomo,1,0,&perm);
                   4727:         if ( !x ) {
                   4728:             *rp = 0;
                   4729:             return;
                   4730:         }
                   4731:     } else {
                   4732: #if 0
                   4733:     /* bug ? */
                   4734:         for ( t = x; t; t = NEXT(t) )
                   4735:             BDY(t) = (pointer)nd_ps[(long)BDY(t)];
                   4736: #else
                   4737:     conv_ilist(0,0,x,&perm);
                   4738: #endif
                   4739:     }
                   4740:     x = ndv_reducebase(x,perm);
                   4741:     x = ndv_reduceall(m,x);
                   4742:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   4743:         NEXTNODE(r0,r);
                   4744:         BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   4745:     }
                   4746:     if ( r0 ) NEXT(r) = 0;
                   4747:     if ( !m && nd_nalg )
                   4748:         r0 = postprocess_algcoef(av,alist,r0);
                   4749:     MKLIST(*rp,r0);
                   4750: }
                   4751:
                   4752: NDV recompute_trace(NODE trace,NDV *p,int m);
                   4753: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
                   4754:
                   4755: NDV recompute_trace(NODE ti,NDV *p,int mod)
                   4756: {
                   4757:   int c,c1,c2,i;
                   4758:   NM mul,m,tail;
                   4759:   ND d,r,rm;
                   4760:   NODE sj;
                   4761:   NDV red;
                   4762:   Obj mj;
                   4763:
                   4764:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   4765:   CM(mul) = 1;
                   4766:   tail = 0;
                   4767:   for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
                   4768:     sj = BDY((LIST)BDY(ti));
                   4769:     if ( ARG0(sj) ) {
1.6       noro     4770:       red = p[ZTOS((Q)ARG1(sj))];
1.1       noro     4771:       mj = (Obj)ARG2(sj);
                   4772:       if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
                   4773:       else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
                   4774:       rm = ndv_mul_nm(mod,mul,red);
                   4775:       if ( !r ) r = rm;
                   4776:       else {
                   4777:         for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
                   4778:           if ( d ) {
                   4779:             NEXT(tail) = m; tail = m; LEN(d)++;
                   4780:           } else {
                   4781:             MKND(nd_nvar,m,1,d); tail = BDY(d);
                   4782:           }
                   4783:         }
                   4784:         if ( !m ) return 0; /* failure */
                   4785:                 else {
                   4786:           BDY(r) = m;
                   4787:                     if ( mod > 0 || mod == -1 ) {
                   4788:             c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
                   4789:             DMAR(c1,c2,0,mod,c);
                   4790:             nd_mul_c(mod,rm,c);
                   4791:                     } else {
                   4792:                       Z t,u;
                   4793:
                   4794:                       chsgnlf(HCZ(r),&t);
                   4795:                       divlf(t,HCZ(rm),&u);
                   4796:                       nd_mul_c_lf(rm,u);
                   4797:                     }
                   4798:           r = nd_add(mod,r,rm);
                   4799:         }
                   4800:           }
                   4801:      }
                   4802:   }
                   4803:   if ( tail ) NEXT(tail) = 0;
                   4804:   d = nd_add(mod,d,r);
                   4805:   nd_mul_c(mod,d,invm(HCM(d),mod));
                   4806:   return ndtondv(mod,d);
                   4807: }
                   4808:
                   4809: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
                   4810: {
                   4811:     VL tv,fv,vv,vc,av;
                   4812:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4813:     int e,max,nvar,i;
                   4814:     NDV b;
                   4815:     int ishomo,nalg;
                   4816:     Alg alpha,dp;
                   4817:     P p,zp;
                   4818:     Q dmy;
                   4819:     LIST f1,f2;
                   4820:     Obj obj;
                   4821:     NumberField nf;
                   4822:     struct order_spec *ord1;
                   4823:   NODE permtrace,intred,ind,perm,trace,ti;
                   4824:   int len,n,j;
                   4825:   NDV *db,*pb;
                   4826:
                   4827:     parse_nd_option(current_option);
                   4828:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4829:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4830:     switch ( ord->id ) {
                   4831:         case 1:
                   4832:             if ( ord->nv != nvar )
                   4833:                 error("nd_check : invalid order specification");
                   4834:             break;
                   4835:         default:
                   4836:             break;
                   4837:     }
                   4838:     nd_init_ord(ord);
1.6       noro     4839:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     4840:     nd_setup_parameters(nvar,0);
                   4841:
                   4842:   len = length(BDY(f));
                   4843:   db = (NDV *)MALLOC(len*sizeof(NDV *));
                   4844:   for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
                   4845:       ptozp((P)BDY(t),1,&dmy,&zp);
                   4846:       b = ptondv(CO,vv,zp);
                   4847:         ndv_mod(m,b);
                   4848:     ndv_mul_c(m,b,invm(HCM(b),m));
                   4849:     db[i] = b;
                   4850:     }
                   4851:
                   4852:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   4853:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   4854:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   4855:   perm = BDY((LIST)ARG0(permtrace));
                   4856:   trace = NEXT(permtrace);
                   4857:
                   4858:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     4859:     j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1       noro     4860:     if ( j > i ) i = j;
                   4861:   }
                   4862:   n = i+1;
                   4863:   pb = (NDV *)MALLOC(n*sizeof(NDV *));
                   4864:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   4865:     ti = BDY((LIST)BDY(t));
1.6       noro     4866:     pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1       noro     4867:   }
                   4868:   for ( t = trace; t; t = NEXT(t) ) {
                   4869:     ti = BDY((LIST)BDY(t));
1.6       noro     4870:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   4871:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     4872:       if ( DP_Print ) {
                   4873:            fprintf(asir_out,"."); fflush(asir_out);
                   4874:       }
                   4875:   }
                   4876:   for ( t = intred; t; t = NEXT(t) ) {
                   4877:     ti = BDY((LIST)BDY(t));
1.6       noro     4878:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   4879:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     4880:       if ( DP_Print ) {
                   4881:            fprintf(asir_out,"*"); fflush(asir_out);
                   4882:       }
                   4883:   }
                   4884:     for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
                   4885:         NEXTNODE(r0,r);
1.6       noro     4886:     b = pb[ZTOS((Q)BDY(t))];
1.1       noro     4887:         ndv_mul_c(m,b,invm(HCM(b),m));
                   4888: #if 0
1.6       noro     4889:         BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1       noro     4890: #else
1.6       noro     4891:         BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1       noro     4892: #endif
                   4893:     }
                   4894:     if ( r0 ) NEXT(r) = 0;
                   4895:     MKLIST(*rp,r0);
                   4896:     if ( DP_Print ) fprintf(asir_out,"\n");
                   4897: }
                   4898:
1.16      noro     4899: 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     4900: {
                   4901:     VL tv,fv,vv,vc,av;
                   4902:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   4903:     int m,nocheck,nvar,mindex,e,max;
                   4904:     NDV c;
                   4905:     NMV a;
                   4906:     P p,zp;
                   4907:     Q dmy;
                   4908:     EPOS oepos;
                   4909:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   4910:     Alg alpha,dp;
                   4911:     P poly;
                   4912:     LIST f1,f2,zpl;
                   4913:     Obj obj;
                   4914:     NumberField nf;
                   4915:     struct order_spec *ord1;
                   4916:     struct oEGT eg_check,eg0,eg1;
                   4917:     NODE tr,tl1,tl2,tl3,tl4;
                   4918:     LIST l1,l2,l3,l4,l5;
                   4919:     int *perm;
                   4920:     int j,ret;
                   4921:     Z jq,bpe;
1.15      noro     4922:     VECT hvect;
1.1       noro     4923:
1.41      noro     4924:     NcriB = NcriMF = Ncri2 = 0;
1.1       noro     4925:     nd_module = 0;
                   4926:     nd_lf = 0;
                   4927:     parse_nd_option(current_option);
                   4928:     if ( nd_lf ) {
                   4929:       if ( f4 )
                   4930:         nd_f4_lf_trace(f,v,trace,homo,ord,rp);
                   4931:       else
                   4932:         error("nd_gr_trace is not implemented yet over a large finite field");
                   4933:       return;
                   4934:     }
                   4935:     if ( DP_Multiple )
                   4936:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4937:
                   4938:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4939:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4940:     switch ( ord->id ) {
                   4941:         case 1:
                   4942:             if ( ord->nv != nvar )
                   4943:                 error("nd_gr_trace : invalid order specification");
                   4944:             break;
                   4945:         default:
                   4946:             break;
                   4947:     }
                   4948:
                   4949:     get_algtree((Obj)f,&av);
                   4950:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4951:     nd_ntrans = nvar;
                   4952:     nd_nalg = nalg;
                   4953:     /* #i -> t#i */
                   4954:     if ( nalg ) {
                   4955:         preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4956:         ord = ord1;
                   4957:         f = f1;
                   4958:     }
                   4959:     nvar += nalg;
                   4960:
                   4961:     nocheck = 0;
                   4962:     mindex = 0;
                   4963:
                   4964:     if ( Demand ) nd_demand = 1;
                   4965:     else nd_demand = 0;
                   4966:
                   4967:     /* setup modulus */
                   4968:     if ( trace < 0 ) {
                   4969:         trace = -trace;
                   4970:         nocheck = 1;
                   4971:     }
                   4972:     m = trace > 1 ? trace : get_lprime(mindex);
                   4973:     nd_init_ord(ord);
                   4974:     mrank = 0;
                   4975:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4976:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4977:             if ( nd_module ) {
1.16      noro     4978:               if ( OID(BDY(t)) == O_DPM ) {
                   4979:                 e = dpm_getdeg((DPM)BDY(t),&trank);
                   4980:                 max = MAX(e,max);
                   4981:                 mrank = MAX(mrank,trank);
                   4982:               } else {
1.1       noro     4983:                 s = BDY((LIST)BDY(t));
                   4984:                 trank = length(s);
                   4985:                 mrank = MAX(mrank,trank);
                   4986:                 for ( ; s; s = NEXT(s) ) {
                   4987:                     e = getdeg(tv->v,(P)BDY(s));
                   4988:                     max = MAX(e,max);
                   4989:                 }
1.16      noro     4990:               }
1.1       noro     4991:             } else {
1.43      noro     4992:               if ( OID(BDY(t)) == O_DP ) {
                   4993:                 e = dp_getdeg((DP)BDY(t));
                   4994:                 max = MAX(e,max);
                   4995:               } else {
1.1       noro     4996:                 e = getdeg(tv->v,(P)BDY(t));
                   4997:                 max = MAX(e,max);
1.43      noro     4998:               }
1.1       noro     4999:             }
                   5000:         }
                   5001:     nd_setup_parameters(nvar,max);
                   5002:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   5003:     ishomo = 1;
                   5004:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   5005:         if ( nd_module ) {
1.16      noro     5006:           if ( OID(BDY(t)) == O_DPM ) {
                   5007:             Z cont;
                   5008:             DPM zdpm;
                   5009:
1.17      noro     5010:             if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16      noro     5011:             else zdpm = (DPM)BDY(t);
                   5012:             c = (pointer)dpmtondv(m,zdpm);
                   5013:           } else {
                   5014:             if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   5015:             else zpl = (LIST)BDY(t);
1.1       noro     5016:             c = (pointer)pltondv(CO,vv,zpl);
1.16      noro     5017:           }
1.1       noro     5018:         } else {
1.43      noro     5019:           if ( OID(BDY(t)) == O_DP ) {
                   5020:             DP zdp;
                   5021:
                   5022:             if ( !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
                   5023:             else zdp = (DP)BDY(t);
                   5024:             c = (pointer)dptondv(m,zdp);
                   5025:           } else {
                   5026:             if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   5027:             else zp = (P)BDY(t);
                   5028:             c = (pointer)ptondv(CO,vv,zp);
                   5029:           }
1.1       noro     5030:         }
                   5031:         if ( ishomo )
                   5032:             ishomo = ishomo && ndv_ishomo(c);
                   5033:         if ( c ) {
                   5034:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   5035:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   5036:         }
                   5037:     }
                   5038:     if ( in0 ) NEXT(in) = 0;
                   5039:     if ( fd0 ) NEXT(fd) = 0;
                   5040:     if ( !ishomo && homo ) {
                   5041:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   5042:             c = (NDV)BDY(t); len = LEN(c);
                   5043:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   5044:                 wmax = MAX(TD(DL(a)),wmax);
                   5045:         }
                   5046:         homogenize_order(ord,nvar,&ord1);
                   5047:         nd_init_ord(ord1);
                   5048:         nd_setup_parameters(nvar+1,wmax);
                   5049:         for ( t = fd0; t; t = NEXT(t) )
                   5050:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   5051:     }
                   5052:     if ( MaxDeg > 0 ) nocheck = 1;
                   5053:     while ( 1 ) {
                   5054:     tl1 = tl2 = tl3 = tl4 = 0;
                   5055:         if ( Demand )
                   5056:             nd_demand = 1;
1.24      noro     5057:         ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1       noro     5058:         if ( nd_gentrace ) {
                   5059:             MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   5060:         }
                   5061:         if ( ret )
                   5062:             cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
                   5063:         if ( !ret || !cand ) {
                   5064:             /* failure */
                   5065:             if ( trace > 1 ) { *rp = 0; return; }
                   5066:             else m = get_lprime(++mindex);
                   5067:             continue;
                   5068:         }
1.15      noro     5069:         if ( nd_gentrace ) {
                   5070:           MKVECT(hvect,nd_psn);
                   5071:           for ( i = 0; i < nd_psn; i++ )
                   5072:              ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   5073:         }
1.1       noro     5074:         if ( !ishomo && homo ) {
                   5075:             /* dehomogenization */
                   5076:             for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   5077:             nd_init_ord(ord);
                   5078:             nd_setup_parameters(nvar,0);
                   5079:         }
                   5080:         nd_demand = 0;
                   5081:         cand = ndv_reducebase(cand,perm);
                   5082:         if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
                   5083:         cand = ndv_reduceall(0,cand);
                   5084:         cbpe = nd_bpe;
                   5085:         if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
                   5086:         get_eg(&eg0);
                   5087:         if ( nocheck )
                   5088:             break;
                   5089:         if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   5090:             if ( nd_gentrace ) {
                   5091:           tl3 = nd_alltracelist; nd_alltracelist = 0;
                   5092:         } else tl3 = 0;
                   5093:             /* gbcheck : cand is a GB of Id(cand) ? */
                   5094:             if ( nd_vc || nd_gentrace || nd_gensyz )
                   5095:               ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
                   5096:             else
                   5097:               ret = nd_f4(0,1,0)!=0;
                   5098:             if ( nd_gentrace && nd_gensyz ) {
                   5099:           tl4 = nd_alltracelist; nd_alltracelist = 0;
                   5100:         } else tl4 = 0;
                   5101:     }
                   5102:     if ( ret ) break;
                   5103:         else if ( trace > 1 ) {
                   5104:             /* failure */
                   5105:             *rp = 0; return;
                   5106:         } else {
                   5107:             /* try the next modulus */
                   5108:             m = get_lprime(++mindex);
                   5109:             /* reset the parameters */
                   5110:             if ( !ishomo && homo ) {
                   5111:                 nd_init_ord(ord1);
                   5112:                 nd_setup_parameters(nvar+1,wmax);
                   5113:             } else {
                   5114:                 nd_init_ord(ord);
                   5115:                 nd_setup_parameters(nvar,max);
                   5116:             }
                   5117:         }
                   5118:     }
                   5119:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   5120:     if ( DP_Print )
1.6       noro     5121:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     5122:     /* dp->p */
                   5123:     nd_bpe = cbpe;
                   5124:     nd_setup_parameters(nd_nvar,0);
                   5125:     for ( r = cand; r; r = NEXT(r) ) {
1.16      noro     5126:       if ( nd_module ) {
1.17      noro     5127:         if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16      noro     5128:         else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17      noro     5129:       } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
                   5130:       else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1       noro     5131:     }
                   5132:     if ( nd_nalg )
                   5133:         cand = postprocess_algcoef(av,alist,cand);
                   5134:     MKLIST(*rp,cand);
                   5135:     if ( nd_gentrace ) {
                   5136:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   5137:     tl3 = reverse_node(tl3);
                   5138:     /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   5139:         for ( t = tl2; t; t = NEXT(t) ) {
                   5140:       /* s = [i,[*,j,*,*],...] */
                   5141:             s = BDY((LIST)BDY(t));
1.6       noro     5142:             j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     5143:       for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     5144:                 j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     5145:         ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   5146:             }
                   5147:     }
                   5148:     for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6       noro     5149:         STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     5150:     }
                   5151:         MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   5152:     MKLIST(l5,tl4);
1.6       noro     5153:       STOZ(nd_bpe,bpe);
1.15      noro     5154:         tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     5155:     }
                   5156: }
                   5157:
                   5158: /* XXX : module element is not considered  */
                   5159:
                   5160: void dltondl(int n,DL dl,UINT *r)
                   5161: {
                   5162:     UINT *d;
                   5163:     int i,j,l,s,ord_l;
                   5164:     struct order_pair *op;
                   5165:
                   5166:     d = (unsigned int *)dl->d;
                   5167:     for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
                   5168:     if ( nd_blockmask ) {
                   5169:         l = nd_blockmask->n;
                   5170:         op = nd_blockmask->order_pair;
                   5171:         for ( j = 0, s = 0; j < l; j++ ) {
                   5172:             ord_l = op[j].length;
                   5173:             for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
                   5174:         }
                   5175:         TD(r) = ndl_weight(r);
                   5176:         ndl_weight_mask(r);
                   5177:     } else {
                   5178:         for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
                   5179:         TD(r) = ndl_weight(r);
                   5180:     }
                   5181: }
                   5182:
                   5183: DL ndltodl(int n,UINT *ndl)
                   5184: {
                   5185:     DL dl;
                   5186:     int *d;
                   5187:     int i,j,l,s,ord_l;
                   5188:     struct order_pair *op;
                   5189:
1.33      noro     5190:     NEWDL_NOINIT(dl,n);
1.1       noro     5191:     dl->td = TD(ndl);
                   5192:     d = dl->d;
                   5193:     if ( nd_blockmask ) {
                   5194:         l = nd_blockmask->n;
                   5195:         op = nd_blockmask->order_pair;
                   5196:         for ( j = 0, s = 0; j < l; j++ ) {
                   5197:             ord_l = op[j].length;
                   5198:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   5199:         }
                   5200:     } else {
                   5201:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   5202:     }
                   5203:     return dl;
                   5204: }
                   5205:
1.24      noro     5206: void _ndltodl(UINT *ndl,DL dl)
                   5207: {
                   5208:     int *d;
                   5209:     int i,j,l,s,ord_l,n;
                   5210:     struct order_pair *op;
                   5211:
                   5212:     n = nd_nvar;
                   5213:     dl->td = TD(ndl);
                   5214:     d = dl->d;
                   5215:     if ( nd_blockmask ) {
                   5216:         l = nd_blockmask->n;
                   5217:         op = nd_blockmask->order_pair;
                   5218:         for ( j = 0, s = 0; j < l; j++ ) {
                   5219:             ord_l = op[j].length;
                   5220:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   5221:         }
                   5222:     } else {
                   5223:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   5224:     }
                   5225: }
                   5226:
1.1       noro     5227: void nmtodp(int mod,NM m,DP *r)
                   5228: {
                   5229:     DP dp;
                   5230:     MP mr;
                   5231:
                   5232:     NEWMP(mr);
                   5233:     mr->dl = ndltodl(nd_nvar,DL(m));
                   5234:     mr->c = (Obj)ndctop(mod,m->c);
                   5235:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   5236:     *r = dp;
                   5237: }
                   5238:
1.15      noro     5239: void ndltodp(UINT *d,DP *r)
                   5240: {
                   5241:     DP dp;
                   5242:     MP mr;
                   5243:
                   5244:     NEWMP(mr);
                   5245:     mr->dl = ndltodl(nd_nvar,d);
                   5246:     mr->c = (Obj)ONE;
                   5247:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   5248:     *r = dp;
                   5249: }
                   5250:
1.1       noro     5251: void ndl_print(UINT *dl)
                   5252: {
                   5253:     int n;
                   5254:     int i,j,l,ord_l,s,s0;
                   5255:     struct order_pair *op;
                   5256:
                   5257:     n = nd_nvar;
                   5258:     printf("<<");
                   5259:     if ( nd_blockmask ) {
                   5260:         l = nd_blockmask->n;
                   5261:         op = nd_blockmask->order_pair;
                   5262:         for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   5263:             ord_l = op[j].length;
                   5264:             for ( i = 0; i < ord_l; i++, s++ )
                   5265:                 printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
                   5266:         }
                   5267:     } else {
                   5268:         for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
                   5269:     }
                   5270:     printf(">>");
                   5271:     if ( nd_module && MPOS(dl) )
                   5272:         printf("*e%d",MPOS(dl));
                   5273: }
                   5274:
                   5275: void nd_print(ND p)
                   5276: {
                   5277:     NM m;
                   5278:
                   5279:     if ( !p )
                   5280:         printf("0\n");
                   5281:     else {
                   5282:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5283:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     5284:             else printf("+%ld*",CM(m));
1.1       noro     5285:             ndl_print(DL(m));
                   5286:         }
                   5287:         printf("\n");
                   5288:     }
                   5289: }
                   5290:
                   5291: void nd_print_q(ND p)
                   5292: {
                   5293:     NM m;
                   5294:
                   5295:     if ( !p )
                   5296:         printf("0\n");
                   5297:     else {
                   5298:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5299:             printf("+");
1.6       noro     5300:             printexpr(CO,(Obj)CZ(m));
1.1       noro     5301:             printf("*");
                   5302:             ndl_print(DL(m));
                   5303:         }
                   5304:         printf("\n");
                   5305:     }
                   5306: }
                   5307:
                   5308: void ndp_print(ND_pairs d)
                   5309: {
                   5310:     ND_pairs t;
                   5311:
                   5312:     for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
                   5313:     printf("\n");
                   5314: }
                   5315:
                   5316: void nd_removecont(int mod,ND p)
                   5317: {
                   5318:     int i,n;
                   5319:     Z *w;
                   5320:     NM m;
                   5321:     struct oVECT v;
                   5322:
                   5323:     if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
                   5324:     else if ( mod == -2 ) {
                   5325:       Z inv;
                   5326:       divlf(ONE,HCZ(p),&inv);
                   5327:       nd_mul_c_lf(p,inv);
                   5328:     } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
                   5329:     else {
                   5330:         for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   5331:         w = (Z *)MALLOC(n*sizeof(Q));
                   5332:         v.len = n;
                   5333:         v.body = (pointer *)w;
1.6       noro     5334:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5335:         removecont_array((P *)w,n,1);
1.6       noro     5336:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5337:     }
                   5338: }
                   5339:
                   5340: void nd_removecont2(ND p1,ND p2)
                   5341: {
                   5342:     int i,n1,n2,n;
                   5343:     Z *w;
                   5344:     NM m;
                   5345:     struct oVECT v;
                   5346:
                   5347:     n1 = nd_length(p1);
                   5348:     n2 = nd_length(p2);
                   5349:     n = n1+n2;
                   5350:     w = (Z *)MALLOC(n*sizeof(Q));
                   5351:     v.len = n;
                   5352:     v.body = (pointer *)w;
                   5353:     i = 0;
                   5354:     if ( p1 )
1.6       noro     5355:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5356:     if ( p2 )
1.6       noro     5357:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5358:     removecont_array((P *)w,n,1);
                   5359:     i = 0;
                   5360:     if ( p1 )
1.6       noro     5361:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5362:     if ( p2 )
1.6       noro     5363:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5364: }
                   5365:
                   5366: void ndv_removecont(int mod,NDV p)
                   5367: {
                   5368:     int i,len,all_p;
                   5369:     Z *c;
                   5370:     P *w;
                   5371:     Z dvr,t;
                   5372:     P g,cont,tp;
                   5373:     NMV m;
                   5374:
                   5375:     if ( mod == -1 )
                   5376:         ndv_mul_c(mod,p,_invsf(HCM(p)));
                   5377:     else if ( mod == -2 ) {
                   5378:       Z inv;
                   5379:       divlf(ONE,HCZ(p),&inv);
                   5380:       ndv_mul_c_lf(p,inv);
                   5381:     } else if ( mod )
                   5382:         ndv_mul_c(mod,p,invm(HCM(p),mod));
                   5383:     else {
                   5384:         len = p->len;
                   5385:         w = (P *)MALLOC(len*sizeof(P));
                   5386:         c = (Z *)MALLOC(len*sizeof(Q));
                   5387:         for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
                   5388:             ptozp(CP(m),1,(Q *)&c[i],&w[i]);
                   5389:             all_p = all_p && !NUM(w[i]);
                   5390:         }
                   5391:         if ( all_p ) {
                   5392:             qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
                   5393:             mulp(nd_vc,(P)dvr,g,&cont);
                   5394:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5395:                 divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
                   5396:             }
                   5397:         } else {
                   5398:             sortbynm((Q *)c,len);
                   5399:             qltozl((Q *)c,len,&dvr);
                   5400:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5401:                 divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
                   5402:             }
                   5403:         }
                   5404:     }
                   5405: }
                   5406:
                   5407: /* koko */
                   5408:
                   5409: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
                   5410: {
                   5411:     int len,i,max;
                   5412:     NMV m,mr0,mr,t;
                   5413:
                   5414:     len = p->len;
1.14      noro     5415:     for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1       noro     5416:         max = MAX(max,TD(DL(m)));
                   5417:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   5418:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   5419:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   5420:     t = (NMV)MALLOC(nmv_adv);
                   5421:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   5422:         ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6       noro     5423:         CZ(mr) = CZ(m);
1.1       noro     5424:         ndl_copy(DL(t),DL(mr));
                   5425:     }
                   5426:     NV(p)++;
                   5427:     BDY(p) = mr0;
                   5428: }
                   5429:
                   5430: void ndv_dehomogenize(NDV p,struct order_spec *ord)
                   5431: {
                   5432:     int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
                   5433:     int pos;
                   5434:     Q *w;
                   5435:     Q dvr,t;
                   5436:     NMV m,r;
                   5437:
                   5438:     len = p->len;
                   5439:     newnvar = nd_nvar-1;
                   5440:     newexporigin = nd_get_exporigin(ord);
                   5441:     if ( nd_module ) newmpos = newexporigin-1;
                   5442:     newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
                   5443:     for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
                   5444:         ndl_dehomogenize(DL(m));
                   5445:     if ( newwpd != nd_wpd ) {
                   5446:         newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
                   5447:         for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6       noro     5448:             CZ(r) = CZ(m);
1.1       noro     5449:             if ( nd_module ) pos = MPOS(DL(m));
                   5450:             for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   5451:             adj = nd_exporigin-newexporigin;
                   5452:             for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
                   5453:             if ( nd_module ) {
                   5454:                 DL(r)[newmpos] = pos;
                   5455:             }
                   5456:         }
                   5457:     }
                   5458:     NV(p)--;
                   5459: }
                   5460:
                   5461: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
                   5462: {
                   5463:     int i;
                   5464:     P *tpl,*tpl1;
                   5465:     NODE l;
                   5466:     P h,gcd,t;
                   5467:
                   5468:     tpl = (P *)MALLOC(m*sizeof(P));
                   5469:     tpl1 = (P *)MALLOC(m*sizeof(P));
                   5470:     bcopy(pl,tpl,m*sizeof(P));
                   5471:     gcd = (P)ONE;
                   5472:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   5473:         h = (P)BDY(l);
                   5474:         while ( 1 ) {
                   5475:             for ( i = 0; i < m; i++ )
                   5476:                 if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
                   5477:                     break;
                   5478:             if ( i == m ) {
                   5479:                 bcopy(tpl1,tpl,m*sizeof(P));
                   5480:                 mulp(vl,gcd,h,&t); gcd = t;
                   5481:             } else
                   5482:                 break;
                   5483:         }
                   5484:     }
                   5485:     if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
                   5486:     if ( full ) {
                   5487:         heu_nezgcdnpz(vl,tpl,m,&t);
                   5488:         mulp(vl,gcd,t,pr);
                   5489:     } else
                   5490:         *pr = gcd;
                   5491: }
                   5492:
                   5493: void removecont_array(P *p,int n,int full)
                   5494: {
                   5495:     int all_p,all_q,i;
                   5496:     Z *c;
                   5497:     P *w;
                   5498:     P t,s;
                   5499:
                   5500:     for ( all_q = 1, i = 0; i < n; i++ )
                   5501:         all_q = all_q && NUM(p[i]);
                   5502:     if ( all_q ) {
                   5503:         removecont_array_q((Z *)p,n);
                   5504:     } else {
                   5505:         c = (Z *)MALLOC(n*sizeof(Z));
                   5506:         w = (P *)MALLOC(n*sizeof(P));
                   5507:         for ( i = 0; i < n; i++ ) {
                   5508:             ptozp(p[i],1,(Q *)&c[i],&w[i]);
                   5509:         }
                   5510:         removecont_array_q(c,n);
                   5511:         nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
                   5512:         for ( i = 0; i < n; i++ ) {
                   5513:             divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
                   5514:         }
                   5515:     }
                   5516: }
                   5517:
                   5518: /* c is an int array */
                   5519:
                   5520: void removecont_array_q(Z *c,int n)
                   5521: {
                   5522:   struct oVECT v;
                   5523:   Z d0,d1,a,u,u1,gcd;
                   5524:   int i,j;
                   5525:   Z *q,*r;
                   5526:
                   5527:   q = (Z *)MALLOC(n*sizeof(Z));
                   5528:   r = (Z *)MALLOC(n*sizeof(Z));
                   5529:   v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   5530:   gcdvz_estimate(&v,&d0);
                   5531:   for ( i = 0; i < n; i++ ) {
                   5532:     divqrz(c[i],d0,&q[i],&r[i]);
                   5533:   }
                   5534:   for ( i = 0; i < n; i++ ) if ( r[i] ) break;
                   5535:   if ( i < n ) {
                   5536:     v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   5537:     gcdvz(&v,&d1);
                   5538:     gcdz(d0,d1,&gcd);
1.6       noro     5539:     /* exact division */
                   5540:     divsz(d0,gcd,&a);
1.1       noro     5541:     for ( i = 0; i < n; i++ ) {
                   5542:       mulz(a,q[i],&u);
                   5543:       if ( r[i] ) {
1.6       noro     5544:         /* exact division */
                   5545:         divsz(r[i],gcd,&u1);
1.1       noro     5546:         addz(u,u1,&q[i]);
                   5547:       } else
                   5548:         q[i] = u;
                   5549:     }
                   5550:   }
                   5551:   for ( i = 0; i < n; i++ ) c[i] = q[i];
                   5552: }
                   5553:
1.4       noro     5554: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
                   5555:
                   5556: void mpz_removecont_array(mpz_t *c,int n)
                   5557: {
                   5558:   mpz_t d0,a,u,u1,gcd;
                   5559:   int i,j;
1.13      noro     5560:   static mpz_t *q,*r;
                   5561:   static int c_len = 0;
1.4       noro     5562:
                   5563:   for ( i = 0; i < n; i++ )
                   5564:     if ( mpz_sgn(c[i]) ) break;
                   5565:   if ( i == n ) return;
                   5566:   gcdv_mpz_estimate(d0,c,n);
1.13      noro     5567:   if ( n > c_len ) {
                   5568:     q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   5569:     r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   5570:     c_len = n;
                   5571:   }
1.4       noro     5572:   for ( i = 0; i < n; i++ ) {
                   5573:     mpz_init(q[i]); mpz_init(r[i]);
                   5574:     mpz_fdiv_qr(q[i],r[i],c[i],d0);
                   5575:   }
                   5576:   for ( i = 0; i < n; i++ )
                   5577:     if ( mpz_sgn(r[i]) ) break;
                   5578:   mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
                   5579:   if ( i < n ) {
                   5580:     mpz_gcd(gcd,d0,r[i]);
                   5581:     for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
                   5582:     mpz_div(a,d0,gcd);
                   5583:     for ( i = 0; i < n; i++ ) {
                   5584:       mpz_mul(u,a,q[i]);
                   5585:       if ( mpz_sgn(r[i]) ) {
                   5586:         mpz_div(u1,r[i],gcd);
                   5587:         mpz_add(q[i],u,u1);
                   5588:       } else
                   5589:         mpz_set(q[i],u);
                   5590:     }
                   5591:   }
                   5592:   for ( i = 0; i < n; i++ )
                   5593:     mpz_set(c[i],q[i]);
                   5594: }
                   5595:
1.1       noro     5596: void nd_mul_c(int mod,ND p,int mul)
                   5597: {
                   5598:     NM m;
                   5599:     int c,c1;
                   5600:
                   5601:     if ( !p ) return;
                   5602:     if ( mul == 1 ) return;
                   5603:     if ( mod == -1 )
                   5604:         for ( m = BDY(p); m; m = NEXT(m) )
                   5605:             CM(m) = _mulsf(CM(m),mul);
                   5606:     else
                   5607:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5608:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   5609:         }
                   5610: }
                   5611:
                   5612: void nd_mul_c_lf(ND p,Z mul)
                   5613: {
                   5614:     NM m;
                   5615:     Z c;
                   5616:
                   5617:     if ( !p ) return;
                   5618:     if ( UNIZ(mul) ) return;
                   5619:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   5620:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   5621:     }
                   5622: }
                   5623:
                   5624: void nd_mul_c_q(ND p,P mul)
                   5625: {
                   5626:     NM m;
                   5627:     P c;
                   5628:
                   5629:     if ( !p ) return;
                   5630:     if ( UNIQ(mul) ) return;
                   5631:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   5632:         mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
                   5633:     }
                   5634: }
                   5635:
                   5636: void nd_mul_c_p(VL vl,ND p,P mul)
                   5637: {
                   5638:     NM m;
                   5639:     P c;
                   5640:
                   5641:     if ( !p ) return;
                   5642:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   5643:         mulp(vl,CP(m),mul,&c); CP(m) = c;
                   5644:     }
                   5645: }
                   5646:
                   5647: void nd_free(ND p)
                   5648: {
                   5649:     NM t,s;
                   5650:
                   5651:     if ( !p ) return;
                   5652:     t = BDY(p);
                   5653:     while ( t ) {
                   5654:         s = NEXT(t);
                   5655:         FREENM(t);
                   5656:         t = s;
                   5657:     }
                   5658:     FREEND(p);
                   5659: }
                   5660:
                   5661: void ndv_free(NDV p)
                   5662: {
                   5663:     GCFREE(BDY(p));
                   5664: }
                   5665:
                   5666: void nd_append_red(UINT *d,int i)
                   5667: {
                   5668:     RHist m,m0;
                   5669:     int h;
                   5670:
                   5671:     NEWRHist(m);
                   5672:     h = ndl_hash_value(d);
                   5673:     m->index = i;
                   5674:     ndl_copy(d,DL(m));
                   5675:     NEXT(m) = nd_red[h];
                   5676:     nd_red[h] = m;
                   5677: }
                   5678:
                   5679: UINT *ndv_compute_bound(NDV p)
                   5680: {
                   5681:     UINT *d1,*d2,*t;
                   5682:     UINT u;
                   5683:     int i,j,k,l,len,ind;
                   5684:     NMV m;
                   5685:
                   5686:     if ( !p )
                   5687:         return 0;
                   5688:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5689:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5690:     len = LEN(p);
                   5691:     m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   5692:     for ( i = 1; i < len; i++, NMV_ADV(m) ) {
                   5693:         ndl_max(DL(m),d1,d2);
                   5694:         t = d1; d1 = d2; d2 = t;
                   5695:     }
                   5696:     l = nd_nvar+31;
                   5697:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   5698:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   5699:         u = d1[i];
                   5700:         k = (nd_epw-1)*nd_bpe;
                   5701:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   5702:             t[ind] = (u>>k)&nd_mask0;
                   5703:     }
                   5704:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   5705:     return t;
                   5706: }
                   5707:
                   5708: UINT *nd_compute_bound(ND p)
                   5709: {
                   5710:     UINT *d1,*d2,*t;
                   5711:     UINT u;
                   5712:     int i,j,k,l,len,ind;
                   5713:     NM m;
                   5714:
                   5715:     if ( !p )
                   5716:         return 0;
                   5717:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5718:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5719:     len = LEN(p);
                   5720:     m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   5721:     for ( m = NEXT(m); m; m = NEXT(m) ) {
                   5722:         ndl_lcm(DL(m),d1,d2);
                   5723:         t = d1; d1 = d2; d2 = t;
                   5724:     }
                   5725:     l = nd_nvar+31;
                   5726:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   5727:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   5728:         u = d1[i];
                   5729:         k = (nd_epw-1)*nd_bpe;
                   5730:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   5731:             t[ind] = (u>>k)&nd_mask0;
                   5732:     }
                   5733:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   5734:     return t;
                   5735: }
                   5736:
                   5737: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
                   5738: /* of a term. In this case we need additional 1 word. */
                   5739:
                   5740: int nd_get_exporigin(struct order_spec *ord)
                   5741: {
                   5742:     switch ( ord->id ) {
1.21      noro     5743:         case 0: case 2: case 256: case 258: case 300:
1.1       noro     5744:             return 1+nd_module;
                   5745:         case 1: case 257:
                   5746:             /* block order */
                   5747:             /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
                   5748:             /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
                   5749:             return ord->ord.block.length+1+nd_module;
                   5750:         case 3: case 259:
                   5751: #if 0
                   5752:             error("nd_get_exporigin : composite order is not supported yet.");
                   5753: #else
                   5754:             return 1+nd_module;
                   5755: #endif
                   5756:         default:
                   5757:             error("nd_get_exporigin : ivalid argument.");
                   5758:             return 0;
                   5759:     }
                   5760: }
                   5761:
                   5762: void nd_setup_parameters(int nvar,int max) {
                   5763:     int i,j,n,elen,ord_o,ord_l,l,s,wpd;
                   5764:     struct order_pair *op;
1.45    ! noro     5765:     extern int CNVars;
1.1       noro     5766:
                   5767:     nd_nvar = nvar;
1.45    ! noro     5768:     CNVars = nvar;
1.1       noro     5769:     if ( max ) {
                   5770:         /* XXX */
                   5771:         if ( do_weyl ) nd_bpe = 32;
                   5772:         else if ( max < 2 ) nd_bpe = 1;
                   5773:         else if ( max < 4 ) nd_bpe = 2;
                   5774:         else if ( max < 8 ) nd_bpe = 3;
                   5775:         else if ( max < 16 ) nd_bpe = 4;
                   5776:         else if ( max < 32 ) nd_bpe = 5;
                   5777:         else if ( max < 64 ) nd_bpe = 6;
                   5778:         else if ( max < 256 ) nd_bpe = 8;
                   5779:         else if ( max < 1024 ) nd_bpe = 10;
                   5780:         else if ( max < 65536 ) nd_bpe = 16;
                   5781:         else nd_bpe = 32;
                   5782:     }
                   5783:     if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
                   5784:         UINT t;
                   5785:     int st;
                   5786:         int *v;
                   5787:   /* t = max(weights) */
                   5788:         t = 0;
                   5789:         if ( current_dl_weight_vector )
                   5790:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                   5791:                 if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
                   5792:                 if ( t < st ) t = st;
                   5793:             }
                   5794:         if ( nd_matrix )
                   5795:             for ( i = 0; i < nd_matrix_len; i++ )
                   5796:                 for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
                   5797:                     if ( (st=v[j]) < 0 ) st = -st;
                   5798:                     if ( t < st ) t = st;
                   5799:         }
                   5800:         /* i = bitsize of t */
                   5801:         for ( i = 0; t; t >>=1, i++ );
                   5802:         /* i += bitsize of nd_nvar */
                   5803:         for ( t = nd_nvar; t; t >>=1, i++);
                   5804:         /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
                   5805:         if ( (nd_bpe+i) >= 31 )
                   5806:             error("nd_setup_parameters : too large weight");
                   5807:     }
                   5808:     nd_epw = (sizeof(UINT)*8)/nd_bpe;
                   5809:     elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   5810:     nd_exporigin = nd_get_exporigin(nd_ord);
                   5811:     wpd = nd_exporigin+elen;
                   5812:     if ( nd_module )
                   5813:         nd_mpos = nd_exporigin-1;
                   5814:     else
                   5815:         nd_mpos = -1;
                   5816:     if ( wpd != nd_wpd ) {
                   5817:         nd_free_private_storage();
                   5818:         nd_wpd = wpd;
                   5819:     }
                   5820:     if ( nd_bpe < 32 ) {
                   5821:         nd_mask0 = (1<<nd_bpe)-1;
                   5822:     } else {
                   5823:         nd_mask0 = 0xffffffff;
                   5824:     }
                   5825:     bzero(nd_mask,sizeof(nd_mask));
                   5826:     nd_mask1 = 0;
                   5827:     for ( i = 0; i < nd_epw; i++ ) {
                   5828:         nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   5829:         nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   5830:     }
                   5831:     nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
                   5832:     nd_epos = nd_create_epos(nd_ord);
                   5833:     nd_blockmask = nd_create_blockmask(nd_ord);
                   5834:     nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
                   5835: }
                   5836:
                   5837: ND_pairs nd_reconstruct(int trace,ND_pairs d)
                   5838: {
                   5839:     int i,obpe,oadv,h;
                   5840:     static NM prev_nm_free_list;
                   5841:     static ND_pairs prev_ndp_free_list;
                   5842:     RHist mr0,mr;
                   5843:     RHist r;
                   5844:     RHist *old_red;
                   5845:     ND_pairs s0,s,t;
                   5846:     EPOS oepos;
                   5847:
                   5848:     obpe = nd_bpe;
                   5849:     oadv = nmv_adv;
                   5850:     oepos = nd_epos;
                   5851:     if ( obpe < 2 ) nd_bpe = 2;
                   5852:     else if ( obpe < 3 ) nd_bpe = 3;
                   5853:     else if ( obpe < 4 ) nd_bpe = 4;
                   5854:     else if ( obpe < 5 ) nd_bpe = 5;
                   5855:     else if ( obpe < 6 ) nd_bpe = 6;
                   5856:     else if ( obpe < 8 ) nd_bpe = 8;
                   5857:     else if ( obpe < 10 ) nd_bpe = 10;
                   5858:     else if ( obpe < 16 ) nd_bpe = 16;
                   5859:     else if ( obpe < 32 ) nd_bpe = 32;
                   5860:     else error("nd_reconstruct : exponent too large");
                   5861:
                   5862:     nd_setup_parameters(nd_nvar,0);
                   5863:     prev_nm_free_list = _nm_free_list;
                   5864:     prev_ndp_free_list = _ndp_free_list;
                   5865:     _nm_free_list = 0;
                   5866:     _ndp_free_list = 0;
                   5867:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   5868:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   5869:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   5870:     }
                   5871:     if ( trace )
                   5872:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   5873:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   5874:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   5875:         }
                   5876:     s0 = 0;
                   5877:     for ( t = d; t; t = NEXT(t) ) {
                   5878:         NEXTND_pairs(s0,s);
                   5879:         s->i1 = t->i1;
                   5880:         s->i2 = t->i2;
1.24      noro     5881:         s->sig = t->sig;
1.1       noro     5882:         SG(s) = SG(t);
                   5883:         ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   5884:     }
                   5885:
                   5886:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   5887:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   5888:         old_red[i] = nd_red[i];
                   5889:         nd_red[i] = 0;
                   5890:     }
                   5891:     for ( i = 0; i < REDTAB_LEN; i++ )
                   5892:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   5893:             NEWRHist(mr);
                   5894:             mr->index = r->index;
                   5895:             SG(mr) = SG(r);
                   5896:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   5897:             h = ndl_hash_value(DL(mr));
                   5898:             NEXT(mr) = nd_red[h];
                   5899:             nd_red[h] = mr;
1.24      noro     5900:             mr->sig = r->sig;
1.1       noro     5901:         }
                   5902:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   5903:     old_red = 0;
                   5904:     for ( i = 0; i < nd_psn; i++ ) {
                   5905:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   5906:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24      noro     5907:         r->sig = nd_psh[i]->sig;
1.1       noro     5908:         nd_psh[i] = r;
                   5909:     }
                   5910:     if ( s0 ) NEXT(s) = 0;
                   5911:     prev_nm_free_list = 0;
                   5912:     prev_ndp_free_list = 0;
                   5913: #if 0
                   5914:     GC_gcollect();
                   5915: #endif
                   5916:     return s0;
                   5917: }
                   5918:
1.30      noro     5919: void nd_reconstruct_s(int trace,ND_pairs *d)
                   5920: {
                   5921:     int i,obpe,oadv,h;
                   5922:     static NM prev_nm_free_list;
                   5923:     static ND_pairs prev_ndp_free_list;
                   5924:     RHist mr0,mr;
                   5925:     RHist r;
                   5926:     RHist *old_red;
                   5927:     ND_pairs s0,s,t;
                   5928:     EPOS oepos;
                   5929:
                   5930:     obpe = nd_bpe;
                   5931:     oadv = nmv_adv;
                   5932:     oepos = nd_epos;
                   5933:     if ( obpe < 2 ) nd_bpe = 2;
                   5934:     else if ( obpe < 3 ) nd_bpe = 3;
                   5935:     else if ( obpe < 4 ) nd_bpe = 4;
                   5936:     else if ( obpe < 5 ) nd_bpe = 5;
                   5937:     else if ( obpe < 6 ) nd_bpe = 6;
                   5938:     else if ( obpe < 8 ) nd_bpe = 8;
                   5939:     else if ( obpe < 10 ) nd_bpe = 10;
                   5940:     else if ( obpe < 16 ) nd_bpe = 16;
                   5941:     else if ( obpe < 32 ) nd_bpe = 32;
                   5942:     else error("nd_reconstruct_s : exponent too large");
                   5943:
                   5944:     nd_setup_parameters(nd_nvar,0);
                   5945:     prev_nm_free_list = _nm_free_list;
                   5946:     prev_ndp_free_list = _ndp_free_list;
                   5947:     _nm_free_list = 0;
                   5948:     _ndp_free_list = 0;
                   5949:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   5950:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   5951:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   5952:     }
                   5953:     if ( trace )
                   5954:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   5955:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   5956:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   5957:         }
                   5958:
                   5959:     for ( i = 0; i < nd_nbase; i++ ) {
                   5960:       s0 = 0;
                   5961:       for ( t = d[i]; t; t = NEXT(t) ) {
                   5962:           NEXTND_pairs(s0,s);
                   5963:           s->i1 = t->i1;
                   5964:           s->i2 = t->i2;
                   5965:           s->sig = t->sig;
                   5966:           SG(s) = SG(t);
                   5967:           ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   5968:       }
                   5969:       d[i] = s0;
                   5970:     }
                   5971:
                   5972:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   5973:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   5974:         old_red[i] = nd_red[i];
                   5975:         nd_red[i] = 0;
                   5976:     }
                   5977:     for ( i = 0; i < REDTAB_LEN; i++ )
                   5978:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   5979:             NEWRHist(mr);
                   5980:             mr->index = r->index;
                   5981:             SG(mr) = SG(r);
                   5982:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   5983:             h = ndl_hash_value(DL(mr));
                   5984:             NEXT(mr) = nd_red[h];
                   5985:             nd_red[h] = mr;
                   5986:             mr->sig = r->sig;
                   5987:         }
                   5988:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   5989:     old_red = 0;
                   5990:     for ( i = 0; i < nd_psn; i++ ) {
                   5991:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   5992:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
                   5993:         r->sig = nd_psh[i]->sig;
                   5994:         nd_psh[i] = r;
                   5995:     }
                   5996:     if ( s0 ) NEXT(s) = 0;
                   5997:     prev_nm_free_list = 0;
                   5998:     prev_ndp_free_list = 0;
                   5999: #if 0
                   6000:     GC_gcollect();
                   6001: #endif
                   6002: }
                   6003:
1.1       noro     6004: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
                   6005: {
                   6006:     int n,i,ei,oepw,omask0,j,s,ord_l,l;
                   6007:     struct order_pair *op;
                   6008:
                   6009:     n = nd_nvar;
                   6010:     oepw = (sizeof(UINT)*8)/obpe;
                   6011:     omask0 = (1<<obpe)-1;
                   6012:     TD(r) = TD(d);
                   6013:     for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
                   6014:     if ( nd_blockmask ) {
                   6015:         l = nd_blockmask->n;
                   6016:         op = nd_blockmask->order_pair;
                   6017:         for ( i = 1; i < nd_exporigin; i++ )
                   6018:             r[i] = d[i];
                   6019:         for ( j = 0, s = 0; j < l; j++ ) {
                   6020:             ord_l = op[j].length;
                   6021:             for ( i = 0; i < ord_l; i++, s++ ) {
                   6022:                 ei =  GET_EXP_OLD(d,s);
                   6023:                 PUT_EXP(r,s,ei);
                   6024:             }
                   6025:         }
                   6026:     } else {
                   6027:         for ( i = 0; i < n; i++ ) {
                   6028:             ei = GET_EXP_OLD(d,i);
                   6029:             PUT_EXP(r,i,ei);
                   6030:         }
                   6031:     }
                   6032:     if ( nd_module ) MPOS(r) = MPOS(d);
                   6033: }
                   6034:
                   6035: ND nd_copy(ND p)
                   6036: {
                   6037:     NM m,mr,mr0;
                   6038:     int c,n;
                   6039:     ND r;
                   6040:
                   6041:     if ( !p )
                   6042:         return 0;
                   6043:     else {
                   6044:         for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   6045:             NEXTNM(mr0,mr);
                   6046:             CM(mr) = CM(m);
                   6047:             ndl_copy(DL(m),DL(mr));
                   6048:         }
                   6049:         NEXT(mr) = 0;
                   6050:         MKND(NV(p),mr0,LEN(p),r);
                   6051:         SG(r) = SG(p);
                   6052:         return r;
                   6053:     }
                   6054: }
                   6055:
                   6056: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
                   6057: {
                   6058:     NM m1,m2;
                   6059:     NDV p1,p2;
                   6060:     ND t1,t2;
                   6061:     UINT *lcm;
                   6062:     P gp,tp;
                   6063:     Z g,t;
                   6064:     Z iq;
                   6065:     int td;
                   6066:     LIST hist;
                   6067:     NODE node;
                   6068:     DP d;
                   6069:
                   6070:     if ( !mod && nd_demand ) {
                   6071:         p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
                   6072:     } else {
                   6073:         if ( trace ) {
                   6074:             p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   6075:         } else {
                   6076:             p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   6077:         }
                   6078:     }
                   6079:     lcm = LCM(p);
                   6080:     NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
                   6081:     if ( ndl_check_bound2(p->i1,DL(m1)) ) {
                   6082:         FREENM(m1); return 0;
                   6083:     }
                   6084:     NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
                   6085:     if ( ndl_check_bound2(p->i2,DL(m2)) ) {
                   6086:         FREENM(m1); FREENM(m2); return 0;
                   6087:     }
                   6088:
                   6089:     if ( mod == -1 ) {
                   6090:         CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
                   6091:     } else if ( mod > 0 ) {
                   6092:         CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
                   6093:     } else if ( mod == -2 ) {
                   6094:         CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
                   6095:     } else if ( nd_vc ) {
                   6096:         ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
                   6097:         divsp(nd_vc,HCP(p2),gp,&CP(m1));
                   6098:         divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
                   6099:     } else {
1.6       noro     6100:         igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1       noro     6101:     }
                   6102:     t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
                   6103:     *rp = nd_add(mod,t1,t2);
                   6104:     if ( nd_gentrace ) {
                   6105:         /* nd_tracelist is initialized */
1.6       noro     6106:         STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     6107:         MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6       noro     6108:         STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     6109:         MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
                   6110:         nd_tracelist = node;
                   6111:     }
1.24      noro     6112:     if ( *rp )
                   6113:       (*rp)->sig = p->sig;
1.1       noro     6114:     FREENM(m1); FREENM(m2);
                   6115:     return 1;
                   6116: }
                   6117:
                   6118: void ndv_mul_c(int mod,NDV p,int mul)
                   6119: {
                   6120:     NMV m;
                   6121:     int c,c1,len,i;
                   6122:
                   6123:     if ( !p ) return;
                   6124:     len = LEN(p);
                   6125:     if ( mod == -1 )
                   6126:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   6127:             CM(m) = _mulsf(CM(m),mul);
                   6128:     else
                   6129:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6130:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   6131:         }
                   6132: }
                   6133:
                   6134: void ndv_mul_c_lf(NDV p,Z mul)
                   6135: {
                   6136:     NMV m;
                   6137:     Z c;
                   6138:     int len,i;
                   6139:
                   6140:     if ( !p ) return;
                   6141:     len = LEN(p);
                   6142:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6143:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   6144:     }
                   6145: }
                   6146:
                   6147: /* for nd_det */
                   6148: void ndv_mul_c_q(NDV p,Z mul)
                   6149: {
                   6150:     NMV m;
                   6151:     Z c;
                   6152:     int len,i;
                   6153:
                   6154:     if ( !p ) return;
                   6155:     len = LEN(p);
                   6156:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     6157:         mulz(CZ(m),mul,&c); CZ(m) = c;
1.1       noro     6158:     }
                   6159: }
                   6160:
                   6161: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
                   6162:     int n2,i,j,l,n,tlen;
                   6163:     UINT *d0;
                   6164:     NM *tab,*psum;
                   6165:     ND s,r;
                   6166:     NM t;
                   6167:     NMV m1;
                   6168:
                   6169:     if ( !p ) return 0;
                   6170:     n = NV(p); n2 = n>>1;
                   6171:     d0 = DL(m0);
                   6172:     l = LEN(p);
                   6173:     for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   6174:     tab = (NM *)MALLOC(tlen*sizeof(NM));
                   6175:     psum = (NM *)MALLOC(tlen*sizeof(NM));
                   6176:     for ( i = 0; i < tlen; i++ ) psum[i] = 0;
                   6177:     m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   6178:     for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
                   6179:         /* m0(NM) * m1(NMV) => tab(NM) */
                   6180:         weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
                   6181:         for ( j = 0; j < tlen; j++ ) {
                   6182:             if ( tab[j] ) {
                   6183:                 NEXT(tab[j]) = psum[j];    psum[j] = tab[j];
                   6184:             }
                   6185:         }
                   6186:     }
                   6187:     for ( i = tlen-1, r = 0; i >= 0; i-- )
                   6188:         if ( psum[i] ) {
                   6189:             for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   6190:             MKND(n,psum[i],j,s);
                   6191:             r = nd_add(mod,r,s);
                   6192:         }
                   6193:     if ( r ) SG(r) = SG(p)+TD(d0);
                   6194:     return r;
                   6195: }
                   6196:
                   6197: /* product of monomials */
                   6198: /* XXX block order is not handled correctly */
                   6199:
                   6200: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   6201: {
                   6202:     int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
                   6203:     UINT *d0,*d1,*d,*dt,*ctab;
                   6204:     Z *ctab_q;
                   6205:     Z q,q1;
                   6206:     UINT c0,c1,c;
                   6207:     NM *p;
                   6208:     NM m,t;
                   6209:     int mpos;
                   6210:
                   6211:     for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   6212:     if ( !m0 || !m1 ) return;
                   6213:     d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
                   6214:     if ( nd_module )
                   6215:         if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
                   6216:
                   6217:     NEWNM(m); d = DL(m);
                   6218:     if ( mod ) {
                   6219:         c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
                   6220:     } else if ( nd_vc )
                   6221:         mulp(nd_vc,CP(m0),CP(m1),&CP(m));
                   6222:   else
1.6       noro     6223:         mulz(CZ(m0),CZ(m1),&CZ(m));
1.1       noro     6224:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   6225:     homo = n&1 ? 1 : 0;
                   6226:     if ( homo ) {
                   6227:         /* offset of h-degree */
                   6228:         h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   6229:         PUT_EXP(DL(m),n-1,h);
                   6230:         TD(DL(m)) = h;
                   6231:         if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   6232:     }
                   6233:     tab[0] = m;
                   6234:     NEWNM(m); d = DL(m);
                   6235:     for ( i = 0, curlen = 1; i < n2; i++ ) {
                   6236:         a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   6237:         k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   6238:         /* xi^a*(Di^k*xi^l)*Di^b */
                   6239:         a += l; b += k;
                   6240:         s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
                   6241:         if ( !k || !l ) {
                   6242:             for ( j = 0; j < curlen; j++ )
                   6243:                 if ( (t = tab[j]) != 0 ) {
                   6244:                     dt = DL(t);
                   6245:                     PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
                   6246:                     if ( nd_blockmask ) ndl_weight_mask(dt);
                   6247:                 }
                   6248:             curlen *= k+1;
                   6249:             continue;
                   6250:         }
                   6251:         min = MIN(k,l);
                   6252:         if ( mod ) {
                   6253:             ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
                   6254:             mkwcm(k,l,mod,(int *)ctab);
                   6255:         } else {
                   6256:             ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
                   6257:             mkwc(k,l,ctab_q);
                   6258:         }
                   6259:         for ( j = min; j >= 0; j-- ) {
                   6260:             for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
                   6261:             PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
                   6262:             h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
                   6263:             if ( homo ) {
                   6264:                 TD(d) = s;
                   6265:                 PUT_EXP(d,n-1,s-h);
                   6266:             } else TD(d) = h;
                   6267:             if ( nd_blockmask ) ndl_weight_mask(d);
                   6268:             if ( mod ) c = ctab[j];
                   6269:             else q = ctab_q[j];
                   6270:             p = tab+curlen*j;
                   6271:             if ( j == 0 ) {
                   6272:                 for ( u = 0; u < curlen; u++, p++ ) {
                   6273:                     if ( tab[u] ) {
                   6274:                         ndl_addto(DL(tab[u]),d);
                   6275:                         if ( mod ) {
                   6276:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
                   6277:                         } else if ( nd_vc )
                   6278:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
                   6279:             else {
1.6       noro     6280:                             mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1       noro     6281:                         }
                   6282:                     }
                   6283:                 }
                   6284:             } else {
                   6285:                 for ( u = 0; u < curlen; u++, p++ ) {
                   6286:                     if ( tab[u] ) {
                   6287:                         NEWNM(t);
                   6288:                         ndl_add(DL(tab[u]),d,DL(t));
                   6289:                         if ( mod ) {
                   6290:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
                   6291:                         } else if ( nd_vc )
                   6292:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
                   6293:             else
1.6       noro     6294:                             mulz(CZ(tab[u]),q,&CZ(t));
1.1       noro     6295:                         *p = t;
                   6296:                     }
                   6297:                 }
                   6298:             }
                   6299:         }
                   6300:         curlen *= k+1;
                   6301:     }
                   6302:     FREENM(m);
                   6303:     if ( nd_module ) {
                   6304:         mpos = MPOS(d1);
                   6305:         for ( i = 0; i < tlen; i++ )
                   6306:             if ( tab[i] ) {
                   6307:                 d = DL(tab[i]);
                   6308:                 MPOS(d) = mpos;
                   6309:                 TD(d) = ndl_weight(d);
                   6310:             }
                   6311:     }
                   6312: }
                   6313:
                   6314: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   6315: {
                   6316:     NM mr,mr0;
                   6317:     NMV m;
                   6318:     UINT *d,*dt,*dm;
                   6319:     int c,n,td,i,c1,c2,len;
                   6320:     Q q;
                   6321:     ND r;
                   6322:
                   6323:     if ( !p ) return 0;
                   6324:     else {
                   6325:         n = NV(p); m = BDY(p);
                   6326:         d = DL(m0);
                   6327:         len = LEN(p);
                   6328:         mr0 = 0;
                   6329:         td = TD(d);
                   6330:         c = CM(m0);
                   6331:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6332:             NEXTNM(mr0,mr);
                   6333:             CM(mr) = 1;
                   6334:             ndl_add(DL(m),d,DL(mr));
                   6335:         }
                   6336:         NEXT(mr) = 0;
                   6337:         MKND(NV(p),mr0,len,r);
                   6338:         SG(r) = SG(p) + TD(d);
                   6339:         return r;
                   6340:     }
                   6341: }
                   6342:
                   6343: ND ndv_mul_nm(int mod,NM m0,NDV p)
                   6344: {
                   6345:     NM mr,mr0;
                   6346:     NMV m;
                   6347:     UINT *d,*dt,*dm;
                   6348:     int c,n,td,i,c1,c2,len;
                   6349:     P q;
                   6350:     ND r;
                   6351:
                   6352:     if ( !p ) return 0;
                   6353:     else if ( do_weyl ) {
                   6354:         if ( mod < 0 ) {
                   6355:             error("ndv_mul_nm : not implemented (weyl)");
                   6356:             return 0;
                   6357:         } else
                   6358:             return weyl_ndv_mul_nm(mod,m0,p);
                   6359:     } else {
                   6360:         n = NV(p); m = BDY(p);
                   6361:         d = DL(m0);
                   6362:         len = LEN(p);
                   6363:         mr0 = 0;
                   6364:         td = TD(d);
                   6365:         if ( mod == -1 ) {
                   6366:             c = CM(m0);
                   6367:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6368:                 NEXTNM(mr0,mr);
                   6369:                 CM(mr) = _mulsf(CM(m),c);
                   6370:                 ndl_add(DL(m),d,DL(mr));
                   6371:             }
                   6372:         } else if ( mod == -2 ) {
                   6373:             Z cl;
                   6374:             cl = CZ(m0);
                   6375:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6376:                 NEXTNM(mr0,mr);
                   6377:                 mullf(CZ(m),cl,&CZ(mr));
                   6378:                 ndl_add(DL(m),d,DL(mr));
                   6379:             }
                   6380:         } else if ( mod ) {
                   6381:             c = CM(m0);
                   6382:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6383:                 NEXTNM(mr0,mr);
                   6384:                 c1 = CM(m);
                   6385:                 DMAR(c1,c,0,mod,c2);
                   6386:                 CM(mr) = c2;
                   6387:                 ndl_add(DL(m),d,DL(mr));
                   6388:             }
                   6389:         } else {
                   6390:             q = CP(m0);
                   6391:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6392:                 NEXTNM(mr0,mr);
                   6393:                 mulp(nd_vc,CP(m),q,&CP(mr));
                   6394:                 ndl_add(DL(m),d,DL(mr));
                   6395:             }
                   6396:         }
                   6397:         NEXT(mr) = 0;
                   6398:         MKND(NV(p),mr0,len,r);
                   6399:         SG(r) = SG(p) + TD(d);
                   6400:         return r;
                   6401:     }
                   6402: }
                   6403:
                   6404: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
                   6405: {
                   6406:     NM mq0,mq;
                   6407:     NMV tm;
                   6408:     Q q;
                   6409:     int i,nv,sg,c,c1,c2,hindex;
                   6410:     ND p,t,r;
                   6411:
                   6412:     if ( bucket->m < 0 ) return 0;
                   6413:     else {
                   6414:         nv = NV(d);
                   6415:         mq0 = 0;
                   6416:         tm = (NMV)MALLOC(nmv_adv);
                   6417:         while ( 1 ) {
                   6418:             if ( mod > 0 || mod == -1 )
                   6419:               hindex = head_pbucket(mod,bucket);
                   6420:             else if ( mod == -2 )
                   6421:               hindex = head_pbucket_lf(bucket);
                   6422:             else
                   6423:               hindex = head_pbucket_q(bucket);
                   6424:             if ( hindex < 0 ) break;
                   6425:             p = bucket->body[hindex];
                   6426:             NEXTNM(mq0,mq);
                   6427:             ndl_sub(HDL(p),HDL(d),DL(mq));
                   6428:             ndl_copy(DL(mq),DL(tm));
                   6429:             if ( mod ) {
                   6430:                 c1 = invm(HCM(d),mod); c2 = HCM(p);
                   6431:                 DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   6432:                 CM(tm) = mod-c;
                   6433:             } else {
1.6       noro     6434:                 divsz(HCZ(p),HCZ(d),&CZ(mq));
                   6435:                 chsgnz(CZ(mq),&CZ(tm));
1.1       noro     6436:             }
                   6437:             t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
                   6438:             bucket->body[hindex] = nd_remove_head(p);
                   6439:             t = nd_remove_head(t);
                   6440:             add_pbucket(mod,bucket,t);
                   6441:         }
                   6442:         if ( !mq0 )
                   6443:             r = 0;
                   6444:         else {
                   6445:             NEXT(mq) = 0;
                   6446:             for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   6447:             MKND(nv,mq0,i,r);
                   6448:             /* XXX */
                   6449:             SG(r) = HTD(r);
                   6450:         }
                   6451:         return r;
                   6452:     }
                   6453: }
                   6454:
                   6455: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   6456: {
                   6457:     NMV m,mr,mr0,t;
                   6458:     int len,i,k;
                   6459:
                   6460:     if ( !p ) return;
                   6461:     m = BDY(p); len = LEN(p);
                   6462:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   6463:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   6464:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   6465:     t = (NMV)MALLOC(nmv_adv);
                   6466:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6       noro     6467:         CZ(t) = CZ(m);
1.1       noro     6468:         for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   6469:         ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6       noro     6470:         CZ(mr) = CZ(t);
1.1       noro     6471:         ndl_copy(DL(t),DL(mr));
                   6472:     }
                   6473:     BDY(p) = mr0;
                   6474: }
                   6475:
                   6476: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   6477: {
                   6478:     NMV m,mr,mr0;
                   6479:     int len,i;
                   6480:     NDV r;
                   6481:
                   6482:     if ( !p ) return 0;
                   6483:     m = BDY(p); len = LEN(p);
                   6484:     mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   6485:     for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   6486:         ndl_zero(DL(mr));
                   6487:         ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6       noro     6488:         CZ(mr) = CZ(m);
1.1       noro     6489:     }
                   6490:     MKNDV(NV(p),mr0,len,r);
                   6491:     SG(r) = SG(p);
1.24      noro     6492:     r->sig = p->sig;
1.1       noro     6493:     return r;
                   6494: }
                   6495:
                   6496: /* duplicate p */
                   6497:
                   6498: NDV ndv_dup(int mod,NDV p)
                   6499: {
                   6500:     NDV d;
                   6501:     NMV t,m,m0;
                   6502:     int i,len;
                   6503:
                   6504:     if ( !p ) return 0;
                   6505:     len = LEN(p);
                   6506:     m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   6507:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   6508:         ndl_copy(DL(t),DL(m));
1.6       noro     6509:         CZ(m) = CZ(t);
1.1       noro     6510:     }
                   6511:     MKNDV(NV(p),m0,len,d);
                   6512:     SG(d) = SG(p);
                   6513:     return d;
                   6514: }
                   6515:
                   6516: NDV ndv_symbolic(int mod,NDV p)
                   6517: {
                   6518:     NDV d;
                   6519:     NMV t,m,m0;
                   6520:     int i,len;
                   6521:
                   6522:     if ( !p ) return 0;
                   6523:     len = LEN(p);
                   6524:     m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   6525:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   6526:         ndl_copy(DL(t),DL(m));
1.6       noro     6527:         CZ(m) = ONE;
1.1       noro     6528:     }
                   6529:     MKNDV(NV(p),m0,len,d);
                   6530:     SG(d) = SG(p);
                   6531:     return d;
                   6532: }
                   6533:
                   6534: ND nd_dup(ND p)
                   6535: {
                   6536:     ND d;
                   6537:     NM t,m,m0;
                   6538:
                   6539:     if ( !p ) return 0;
                   6540:     for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   6541:         NEXTNM(m0,m);
                   6542:         ndl_copy(DL(t),DL(m));
1.6       noro     6543:         CZ(m) = CZ(t);
1.1       noro     6544:     }
                   6545:     if ( m0 ) NEXT(m) = 0;
                   6546:     MKND(NV(p),m0,LEN(p),d);
                   6547:     SG(d) = SG(p);
                   6548:     return d;
                   6549: }
                   6550:
                   6551: /* XXX if p->len == 0 then it represents 0 */
                   6552:
                   6553: void ndv_mod(int mod,NDV p)
                   6554: {
                   6555:     NMV t,d;
                   6556:     int r,s,u;
                   6557:     int i,len,dlen;
                   6558:     P cp;
                   6559:     Z c;
                   6560:     Obj gfs;
                   6561:
                   6562:     if ( !p ) return;
                   6563:     len = LEN(p);
                   6564:     dlen = 0;
                   6565:     if ( mod == -1 )
                   6566:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   6567:             simp_ff((Obj)CP(t),&gfs);
                   6568:             if ( gfs ) {
                   6569:               r = FTOIF(CONT((GFS)gfs));
                   6570:               CM(d) = r;
                   6571:               ndl_copy(DL(t),DL(d));
                   6572:               NMV_ADV(d);
                   6573:               dlen++;
                   6574:             }
                   6575:         }
                   6576:     else if ( mod == -2 )
                   6577:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   6578:             simp_ff((Obj)CP(t),&gfs);
                   6579:             if ( gfs ) {
                   6580:               lmtolf((LM)gfs,&CZ(d));
                   6581:               ndl_copy(DL(t),DL(d));
                   6582:               NMV_ADV(d);
                   6583:               dlen++;
                   6584:             }
                   6585:         }
                   6586:     else
                   6587:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   6588:             if ( nd_vc ) {
                   6589:                 nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
                   6590:                 c = (Z)cp;
                   6591:             } else
1.6       noro     6592:                 c = CZ(t);
1.1       noro     6593:             r = remqi((Q)c,mod);
                   6594:             if ( r ) {
                   6595:                 CM(d) = r;
                   6596:                 ndl_copy(DL(t),DL(d));
                   6597:                 NMV_ADV(d);
                   6598:                 dlen++;
                   6599:             }
                   6600:         }
                   6601:     LEN(p) = dlen;
                   6602: }
                   6603:
                   6604: NDV ptondv(VL vl,VL dvl,P p)
                   6605: {
                   6606:     ND nd;
                   6607:
                   6608:     nd = ptond(vl,dvl,p);
                   6609:     return ndtondv(0,nd);
                   6610: }
                   6611:
                   6612: void pltozpl(LIST l,Q *cont,LIST *pp)
                   6613: {
1.16      noro     6614:   NODE nd,nd1;
                   6615:   int n;
                   6616:   P *pl;
                   6617:   Q *cl;
                   6618:   int i;
                   6619:   P dmy;
                   6620:   Z dvr,inv;
                   6621:   LIST r;
                   6622:
                   6623:   nd = BDY(l); n = length(nd);
                   6624:   pl = (P *)MALLOC(n*sizeof(P));
                   6625:   cl = (Q *)MALLOC(n*sizeof(Q));
                   6626:   for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
                   6627:     ptozp((P)BDY(nd),1,&cl[i],&dmy);
                   6628:   }
                   6629:   qltozl(cl,n,&dvr);
                   6630:   divz(ONE,dvr,&inv);
                   6631:   nd = BDY(l);
                   6632:   for ( i = 0; i < n; i++, nd = NEXT(nd) )
                   6633:     divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
                   6634:   nd = 0;
                   6635:   for ( i = n-1; i >= 0; i-- ) {
                   6636:     MKNODE(nd1,pl[i],nd); nd = nd1;
                   6637:   }
                   6638:   MKLIST(r,nd);
                   6639:   *pp = r;
1.1       noro     6640: }
                   6641:
                   6642: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
                   6643:
                   6644: NDV pltondv(VL vl,VL dvl,LIST p)
                   6645: {
                   6646:     int i;
                   6647:     NODE t;
                   6648:     ND r,ri;
                   6649:     NM m;
                   6650:
                   6651:     if ( !nd_module ) error("pltond : module order must be set");
                   6652:     r = 0;
                   6653:     for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
                   6654:         ri = ptond(vl,dvl,(P)BDY(t));
                   6655:         if ( ri )
                   6656:             for ( m = BDY(ri); m; m = NEXT(m) ) {
                   6657:                 MPOS(DL(m)) = i;
                   6658:                 TD(DL(m)) = ndl_weight(DL(m));
                   6659:                 if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   6660:             }
                   6661:         r = nd_add(0,r,ri);
                   6662:     }
                   6663:     return ndtondv(0,r);
                   6664: }
                   6665:
                   6666: ND ptond(VL vl,VL dvl,P p)
                   6667: {
                   6668:     int n,i,j,k,e;
                   6669:     VL tvl;
                   6670:     V v;
                   6671:     DCP dc;
                   6672:     DCP *w;
                   6673:     ND r,s,t,u;
                   6674:     P x;
                   6675:     int c;
                   6676:     UINT *d;
                   6677:     NM m,m0;
                   6678:
                   6679:     if ( !p )
                   6680:         return 0;
                   6681:     else if ( NUM(p) ) {
                   6682:         NEWNM(m);
                   6683:         ndl_zero(DL(m));
                   6684:         if ( !INT((Q)p) )
                   6685:           error("ptond : input must be integer-coefficient");
1.6       noro     6686:         CZ(m) = (Z)p;
1.1       noro     6687:         NEXT(m) = 0;
                   6688:         MKND(nd_nvar,m,1,r);
                   6689:         SG(r) = 0;
                   6690:         return r;
                   6691:     } else {
                   6692:         for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   6693:         w = (DCP *)MALLOC(k*sizeof(DCP));
                   6694:         for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   6695:         for ( i = 0, tvl = dvl, v = VR(p);
                   6696:             tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   6697:         if ( !tvl ) {
                   6698:             for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   6699:                 t = ptond(vl,dvl,COEF(w[j]));
                   6700:                 pwrp(vl,x,DEG(w[j]),&p);
                   6701:                 nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   6702:             }
                   6703:             return s;
                   6704:         } else {
                   6705:             NEWNM(m0); d = DL(m0);
                   6706:             for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6       noro     6707:                 ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1       noro     6708:                 TD(d) = MUL_WEIGHT(e,i);
                   6709:                 if ( nd_blockmask) ndl_weight_mask(d);
                   6710:                 if ( nd_module ) MPOS(d) = 0;
                   6711:                 t = ptond(vl,dvl,COEF(w[j]));
                   6712:                 for ( m = BDY(t); m; m = NEXT(m) )
                   6713:                     ndl_addto(DL(m),d);
                   6714:                 SG(t) += TD(d);
                   6715:                 s = nd_add(0,s,t);
                   6716:             }
                   6717:             FREENM(m0);
                   6718:             return s;
                   6719:         }
                   6720:     }
                   6721: }
                   6722:
                   6723: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   6724: {
                   6725:     VL tvl;
                   6726:     int len,n,j,i,e;
                   6727:     NMV m;
                   6728:     Z q;
                   6729:     P c;
                   6730:     UINT *d;
                   6731:     P s,r,u,t,w;
                   6732:     GFS gfs;
                   6733:
                   6734:     if ( !p ) return 0;
                   6735:     else {
                   6736:         len = LEN(p);
                   6737:         n = NV(p);
                   6738:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   6739:         for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   6740:             if ( mod == -1 ) {
                   6741:                e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   6742:             } else if ( mod == -2 ) {
                   6743:                c = (P)CZ(m);
                   6744:             } else if ( mod > 0 ) {
1.6       noro     6745:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     6746:             } else
                   6747:                 c = CP(m);
                   6748:             d = DL(m);
                   6749:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     6750:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     6751:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   6752:             }
                   6753:             addp(vl,s,t,&u); s = u;
                   6754:         }
                   6755:         return s;
                   6756:     }
                   6757: }
                   6758:
                   6759: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
                   6760: {
                   6761:     VL tvl;
                   6762:     int len,n,j,i,e;
                   6763:     NMV m;
                   6764:     Z q;
                   6765:     P c;
                   6766:     UINT *d;
                   6767:     P s,r,u,t,w;
                   6768:     GFS gfs;
                   6769:     P *a;
                   6770:     LIST l;
                   6771:     NODE nd,nd1;
                   6772:
                   6773:     if ( !p ) return 0;
                   6774:     else {
                   6775:         a = (P *)MALLOC((rank+1)*sizeof(P));
                   6776:         for ( i = 0; i <= rank; i++ ) a[i] = 0;
                   6777:         len = LEN(p);
                   6778:         n = NV(p);
                   6779:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   6780:         for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
                   6781:             if ( mod == -1 ) {
                   6782:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   6783:             } else if ( mod ) {
1.6       noro     6784:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     6785:             } else
                   6786:                 c = CP(m);
                   6787:             d = DL(m);
                   6788:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     6789:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     6790:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   6791:             }
                   6792:             addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
                   6793:         }
                   6794:         nd = 0;
                   6795:         for ( i = rank; i > 0; i-- ) {
                   6796:             MKNODE(nd1,a[i],nd); nd = nd1;
                   6797:         }
                   6798:         MKLIST(l,nd);
                   6799:         return l;
                   6800:     }
                   6801: }
                   6802:
                   6803: NDV ndtondv(int mod,ND p)
                   6804: {
                   6805:     NDV d;
                   6806:     NMV m,m0;
                   6807:     NM t;
                   6808:     int i,len;
                   6809:
                   6810:     if ( !p ) return 0;
                   6811:     len = LEN(p);
                   6812:     if ( mod > 0 || mod == -1 )
                   6813:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   6814:     else
                   6815:         m0 = m = MALLOC(len*nmv_adv);
                   6816: #if 0
                   6817:     ndv_alloc += nmv_adv*len;
                   6818: #endif
                   6819:     for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   6820:         ndl_copy(DL(t),DL(m));
1.6       noro     6821:         CZ(m) = CZ(t);
1.1       noro     6822:     }
                   6823:     MKNDV(NV(p),m0,len,d);
                   6824:     SG(d) = SG(p);
1.24      noro     6825:     d->sig = p->sig;
1.1       noro     6826:     return d;
                   6827: }
                   6828:
1.16      noro     6829: static int dmm_comp_nv;
                   6830:
                   6831: int dmm_comp(DMM *a,DMM *b)
                   6832: {
                   6833:    return -compdmm(dmm_comp_nv,*a,*b);
                   6834: }
                   6835:
                   6836: void dmm_sort_by_ord(DMM *a,int len,int nv)
                   6837: {
                   6838:   dmm_comp_nv = nv;
                   6839:   qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
                   6840: }
                   6841:
                   6842: void dpm_sort(DPM p,DPM *rp)
                   6843: {
                   6844:   DMM t,t1;
                   6845:   int len,i,n;
                   6846:   DMM *a;
                   6847:   DPM d;
                   6848:
                   6849:   if ( !p ) *rp = 0;
                   6850:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   6851:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   6852:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   6853:   n = p->nv;
                   6854:   dmm_sort_by_ord(a,len,n);
                   6855:   t = 0;
                   6856:   for ( i = len-1; i >= 0; i-- ) {
                   6857:     NEWDMM(t1);
                   6858:     t1->c = a[i]->c;
                   6859:     t1->dl = a[i]->dl;
                   6860:     t1->pos = a[i]->pos;
                   6861:     t1->next = t;
                   6862:     t = t1;
                   6863:   }
                   6864:   MKDPM(n,t,d);
                   6865:   SG(d) = SG(p);
                   6866:   *rp = d;
                   6867: }
                   6868:
1.18      noro     6869: int dpm_comp(DPM *a,DPM *b)
                   6870: {
1.22      noro     6871:   return -compdpm(CO,*a,*b);
1.18      noro     6872: }
                   6873:
                   6874: NODE dpm_sort_list(NODE l)
                   6875: {
                   6876:   int i,len;
                   6877:   NODE t,t1;
                   6878:   DPM *a;
                   6879:
                   6880:   len = length(l);
                   6881:   a = (DPM *)MALLOC(len*sizeof(DPM));
                   6882:   for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
                   6883:   qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
                   6884:   t = 0;
                   6885:   for ( i = len-1; i >= 0; i-- ) {
                   6886:     MKNODE(t1,(pointer)a[i],t); t = t1;
                   6887:   }
                   6888:   return t;
                   6889: }
                   6890:
1.20      noro     6891: int nmv_comp(NMV a,NMV b)
                   6892: {
1.21      noro     6893:   int t;
                   6894:   t = DL_COMPARE(a->dl,b->dl);
                   6895:   return -t;
1.20      noro     6896: }
                   6897:
1.43      noro     6898: NDV dptondv(int mod,DP p)
                   6899: {
                   6900:   NDV d;
                   6901:   NMV m,m0;
                   6902:   MP t;
                   6903:   MP *a;
                   6904:   int i,len,n;
                   6905:
                   6906:   if ( !p ) return 0;
                   6907:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   6908:   n = p->nv;
                   6909:   if ( mod > 0 || mod == -1 )
                   6910:     m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   6911:   else
                   6912:     m0 = m = MALLOC(len*nmv_adv);
                   6913:   for ( i = 0, t = BDY(p); i < len; i++, NMV_ADV(m), t = NEXT(t) ) {
                   6914:     dltondl(n,t->dl,DL(m));
                   6915:     TD(DL(m)) = ndl_weight(DL(m));
                   6916:     CZ(m) = (Z)t->c;
                   6917:   }
                   6918:   qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
                   6919:   MKNDV(NV(p),m0,len,d);
                   6920:   SG(d) = SG(p);
                   6921:   return d;
                   6922: }
                   6923:
1.16      noro     6924: NDV dpmtondv(int mod,DPM p)
                   6925: {
                   6926:   NDV d;
                   6927:   NMV m,m0;
                   6928:   DMM t;
                   6929:   DMM *a;
                   6930:   int i,len,n;
                   6931:
                   6932:   if ( !p ) return 0;
                   6933:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   6934:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   6935:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   6936:   n = p->nv;
                   6937:   dmm_sort_by_ord(a,len,n);
                   6938:   if ( mod > 0 || mod == -1 )
                   6939:     m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   6940:   else
                   6941:     m0 = m = MALLOC(len*nmv_adv);
                   6942: #if 0
                   6943:   ndv_alloc += nmv_adv*len;
                   6944: #endif
                   6945:   for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6946:     dltondl(n,a[i]->dl,DL(m));
                   6947:     MPOS(DL(m)) = a[i]->pos;
1.20      noro     6948:     TD(DL(m)) = ndl_weight(DL(m));
1.16      noro     6949:     CZ(m) = (Z)a[i]->c;
                   6950:   }
1.20      noro     6951:   qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16      noro     6952:   MKNDV(NV(p),m0,len,d);
                   6953:   SG(d) = SG(p);
                   6954:   return d;
                   6955: }
                   6956:
1.1       noro     6957: ND ndvtond(int mod,NDV p)
                   6958: {
                   6959:     ND d;
                   6960:     NM m,m0;
                   6961:     NMV t;
                   6962:     int i,len;
                   6963:
                   6964:     if ( !p ) return 0;
                   6965:     m0 = 0;
                   6966:     len = p->len;
                   6967:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   6968:         NEXTNM(m0,m);
                   6969:         ndl_copy(DL(t),DL(m));
1.6       noro     6970:         CZ(m) = CZ(t);
1.1       noro     6971:     }
                   6972:     NEXT(m) = 0;
                   6973:     MKND(NV(p),m0,len,d);
                   6974:     SG(d) = SG(p);
1.28      noro     6975:     d->sig = p->sig;
1.1       noro     6976:     return d;
                   6977: }
                   6978:
                   6979: DP ndvtodp(int mod,NDV p)
                   6980: {
                   6981:     MP m,m0;
                   6982:   DP d;
                   6983:     NMV t;
                   6984:     int i,len;
                   6985:
                   6986:     if ( !p ) return 0;
                   6987:     m0 = 0;
                   6988:     len = p->len;
                   6989:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   6990:         NEXTMP(m0,m);
                   6991:       m->dl = ndltodl(nd_nvar,DL(t));
                   6992:       m->c = (Obj)ndctop(mod,t->c);
                   6993:     }
                   6994:     NEXT(m) = 0;
                   6995:   MKDP(nd_nvar,m0,d);
                   6996:     SG(d) = SG(p);
                   6997:     return d;
                   6998: }
                   6999:
1.44      noro     7000: DPM sigtodpm(SIG s)
                   7001: {
                   7002:   DMM m;
                   7003:   DPM d;
                   7004:
                   7005:   NEWDMM(m);
                   7006:   m->c = (Obj)ONE;
                   7007:   m->dl = s->dl;
                   7008:   m->pos = s->pos+1;
                   7009:   m->next = 0;
                   7010:   MKDPM(nd_nvar,m,d);
                   7011:   SG(d) = s->dl->td;
                   7012:   return d;
                   7013: }
                   7014:
1.16      noro     7015: DPM ndvtodpm(int mod,NDV p)
                   7016: {
                   7017:   DMM m,m0;
                   7018:   DPM d;
                   7019:   NMV t;
                   7020:   int i,len;
                   7021:
                   7022:   if ( !p ) return 0;
                   7023:   m0 = 0;
                   7024:   len = p->len;
                   7025:   for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   7026:     NEXTDMM(m0,m);
                   7027:     m->dl = ndltodl(nd_nvar,DL(t));
                   7028:     m->c = (Obj)ndctop(mod,t->c);
                   7029:     m->pos = MPOS(DL(t));
                   7030:   }
                   7031:   NEXT(m) = 0;
                   7032:   MKDPM(nd_nvar,m0,d);
                   7033:   SG(d) = SG(p);
                   7034:   return d;
                   7035: }
                   7036:
                   7037:
1.1       noro     7038: DP ndtodp(int mod,ND p)
                   7039: {
                   7040:     MP m,m0;
                   7041:   DP d;
                   7042:     NM t;
                   7043:     int i,len;
                   7044:
                   7045:     if ( !p ) return 0;
                   7046:     m0 = 0;
                   7047:     len = p->len;
                   7048:     for ( t = BDY(p); t; t = NEXT(t) ) {
                   7049:         NEXTMP(m0,m);
                   7050:       m->dl = ndltodl(nd_nvar,DL(t));
                   7051:       m->c = (Obj)ndctop(mod,t->c);
                   7052:     }
                   7053:     NEXT(m) = 0;
                   7054:   MKDP(nd_nvar,m0,d);
                   7055:     SG(d) = SG(p);
                   7056:     return d;
                   7057: }
                   7058:
                   7059: void ndv_print(NDV p)
                   7060: {
                   7061:     NMV m;
                   7062:     int i,len;
                   7063:
                   7064:     if ( !p ) printf("0\n");
                   7065:     else {
                   7066:         len = LEN(p);
                   7067:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   7068:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     7069:             else printf("+%ld*",CM(m));
1.1       noro     7070:             ndl_print(DL(m));
                   7071:         }
                   7072:         printf("\n");
                   7073:     }
                   7074: }
                   7075:
                   7076: void ndv_print_q(NDV p)
                   7077: {
                   7078:     NMV m;
                   7079:     int i,len;
                   7080:
                   7081:     if ( !p ) printf("0\n");
                   7082:     else {
                   7083:         len = LEN(p);
                   7084:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   7085:             printf("+");
1.6       noro     7086:             printexpr(CO,(Obj)CZ(m));
1.1       noro     7087:             printf("*");
                   7088:             ndl_print(DL(m));
                   7089:         }
                   7090:         printf("\n");
                   7091:     }
                   7092: }
                   7093:
                   7094: NODE ndv_reducebase(NODE x,int *perm)
                   7095: {
                   7096:     int len,i,j;
                   7097:     NDVI w;
                   7098:     NODE t,t0;
                   7099:
1.41      noro     7100:     if ( nd_norb ) return x;
1.1       noro     7101:     len = length(x);
                   7102:     w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
                   7103:     for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
                   7104:         w[i].p = BDY(t); w[i].i = perm[i];
                   7105:     }
                   7106:     for ( i = 0; i < len; i++ ) {
                   7107:         for ( j = 0; j < i; j++ ) {
                   7108:             if ( w[i].p && w[j].p ) {
                   7109:                 if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
                   7110:                 else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
                   7111:             }
                   7112:         }
                   7113:     }
                   7114:     for ( i = j = 0, t0 = 0; i < len; i++ ) {
                   7115:         if ( w[i].p ) {
                   7116:             NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
                   7117:             perm[j++] = w[i].i;
                   7118:         }
                   7119:     }
                   7120:     NEXT(t) = 0; x = t0;
                   7121:     return x;
                   7122: }
                   7123:
                   7124: /* XXX incomplete */
                   7125:
1.21      noro     7126: extern DMMstack dmm_stack;
                   7127: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16      noro     7128:
1.1       noro     7129: void nd_init_ord(struct order_spec *ord)
                   7130: {
                   7131:   nd_module = (ord->id >= 256);
                   7132:   if ( nd_module ) {
                   7133:     nd_dcomp = -1;
1.21      noro     7134:     nd_module_ordtype = ord->module_ordtype;
1.1       noro     7135:     nd_pot_nelim = ord->pot_nelim;
                   7136:     nd_poly_weight_len = ord->nv;
                   7137:     nd_poly_weight = ord->top_weight;
                   7138:     nd_module_rank = ord->module_rank;
                   7139:     nd_module_weight = ord->module_top_weight;
                   7140:   }
                   7141:   nd_matrix = 0;
                   7142:   nd_matrix_len = 0;
                   7143:     switch ( ord->id ) {
                   7144:         case 0:
                   7145:             switch ( ord->ord.simple ) {
                   7146:                 case 0:
                   7147:                     nd_dcomp = 1;
                   7148:                     nd_isrlex = 1;
                   7149:                     break;
                   7150:                 case 1:
                   7151:                     nd_dcomp = 1;
                   7152:                     nd_isrlex = 0;
                   7153:                     break;
                   7154:                 case 2:
                   7155:                     nd_dcomp = 0;
                   7156:                     nd_isrlex = 0;
                   7157:                     ndl_compare_function = ndl_lex_compare;
                   7158:                     break;
                   7159:                 case 11:
                   7160:                     /* XXX */
                   7161:                     nd_dcomp = 0;
                   7162:                     nd_isrlex = 1;
                   7163:                     ndl_compare_function = ndl_ww_lex_compare;
                   7164:                     break;
                   7165:                 default:
                   7166:                     error("nd_gr : unsupported order");
                   7167:             }
                   7168:             break;
                   7169:         case 1:
                   7170:             /* block order */
                   7171:             /* XXX */
                   7172:             nd_dcomp = -1;
                   7173:             nd_isrlex = 0;
                   7174:             ndl_compare_function = ndl_block_compare;
                   7175:             break;
                   7176:         case 2:
                   7177:             /* matrix order */
                   7178:             /* XXX */
                   7179:             nd_dcomp = -1;
                   7180:             nd_isrlex = 0;
                   7181:             nd_matrix_len = ord->ord.matrix.row;
                   7182:             nd_matrix = ord->ord.matrix.matrix;
                   7183:             ndl_compare_function = ndl_matrix_compare;
                   7184:             break;
                   7185:         case 3:
                   7186:             /* composite order */
                   7187:             nd_dcomp = -1;
                   7188:             nd_isrlex = 0;
                   7189:             nd_worb_len = ord->ord.composite.length;
                   7190:             nd_worb = ord->ord.composite.w_or_b;
                   7191:             ndl_compare_function = ndl_composite_compare;
                   7192:             break;
                   7193:
                   7194:         /* module order */
                   7195:         case 256:
                   7196:             switch ( ord->ord.simple ) {
                   7197:                 case 0:
1.21      noro     7198:                     nd_dcomp = 0;
1.1       noro     7199:                     nd_isrlex = 1;
1.21      noro     7200:                     ndl_compare_function = ndl_module_glex_compare;
1.1       noro     7201:                     break;
                   7202:                 case 1:
1.21      noro     7203:                     nd_dcomp = 0;
1.1       noro     7204:                     nd_isrlex = 0;
                   7205:                     ndl_compare_function = ndl_module_glex_compare;
                   7206:                     break;
                   7207:                 case 2:
1.21      noro     7208:                     nd_dcomp = 0;
1.1       noro     7209:                     nd_isrlex = 0;
1.21      noro     7210:                     ndl_compare_function = ndl_module_compare;
                   7211:                     ndl_base_compare_function = ndl_lex_compare;
1.1       noro     7212:                     break;
                   7213:                 default:
1.21      noro     7214:                     error("nd_init_ord : unsupported order");
1.1       noro     7215:             }
                   7216:             break;
                   7217:         case 257:
                   7218:             /* block order */
                   7219:             nd_isrlex = 0;
1.21      noro     7220:             ndl_compare_function = ndl_module_compare;
                   7221:             ndl_base_compare_function = ndl_block_compare;
1.1       noro     7222:             break;
                   7223:         case 258:
                   7224:             /* matrix order */
                   7225:             nd_isrlex = 0;
                   7226:             nd_matrix_len = ord->ord.matrix.row;
                   7227:             nd_matrix = ord->ord.matrix.matrix;
1.21      noro     7228:             ndl_compare_function = ndl_module_compare;
                   7229:             ndl_base_compare_function = ndl_matrix_compare;
1.1       noro     7230:             break;
                   7231:         case 259:
                   7232:             /* composite order */
                   7233:             nd_isrlex = 0;
                   7234:             nd_worb_len = ord->ord.composite.length;
                   7235:             nd_worb = ord->ord.composite.w_or_b;
1.21      noro     7236:             ndl_compare_function = ndl_module_compare;
                   7237:             ndl_base_compare_function = ndl_composite_compare;
                   7238:             break;
                   7239:         case 300:
                   7240:             /* schreyer order */
                   7241:             if ( ord->base->id != 256 )
                   7242:                error("nd_init_ord : unsupported base order");
                   7243:             ndl_compare_function = ndl_module_schreyer_compare;
                   7244:             dmm_stack = ord->dmmstack;
                   7245:             switch ( ord->base->ord.simple ) {
                   7246:                 case 0:
                   7247:                     nd_isrlex = 1;
                   7248:                     ndl_base_compare_function = ndl_glex_compare;
                   7249:                     dl_base_compare_function = cmpdl_revgradlex;
                   7250:                     break;
                   7251:                 case 1:
                   7252:                     nd_isrlex = 0;
                   7253:                     ndl_base_compare_function = ndl_glex_compare;
                   7254:                     dl_base_compare_function = cmpdl_gradlex;
                   7255:                     break;
                   7256:                 case 2:
                   7257:                     nd_isrlex = 0;
                   7258:                     ndl_base_compare_function = ndl_lex_compare;
                   7259:                     dl_base_compare_function = cmpdl_lex;
                   7260:                     break;
                   7261:                 default:
                   7262:                     error("nd_init_ord : unsupported order");
                   7263:             }
1.1       noro     7264:             break;
                   7265:     }
                   7266:     nd_ord = ord;
                   7267: }
                   7268:
                   7269: BlockMask nd_create_blockmask(struct order_spec *ord)
                   7270: {
                   7271:     int n,i,j,s,l;
                   7272:     UINT *t;
                   7273:     BlockMask bm;
                   7274:
                   7275:     /* we only create mask table for block order */
                   7276:     if ( ord->id != 1 && ord->id != 257 )
                   7277:         return 0;
                   7278:     n = ord->ord.block.length;
                   7279:     bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   7280:     bm->n = n;
                   7281:     bm->order_pair = ord->ord.block.order_pair;
                   7282:     bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
                   7283:     for ( i = 0, s = 0; i < n; i++ ) {
                   7284:         bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
                   7285:         for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   7286:         l = bm->order_pair[i].length;
                   7287:         for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   7288:     }
                   7289:     return bm;
                   7290: }
                   7291:
                   7292: EPOS nd_create_epos(struct order_spec *ord)
                   7293: {
                   7294:     int i,j,l,s,ord_l,ord_o;
                   7295:     EPOS epos;
                   7296:     struct order_pair *op;
                   7297:
                   7298:     epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   7299:     switch ( ord->id ) {
1.21      noro     7300:         case 0: case 256: case 300:
1.1       noro     7301:             if ( nd_isrlex ) {
                   7302:                 for ( i = 0; i < nd_nvar; i++ ) {
                   7303:                     epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   7304:                     epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   7305:                 }
                   7306:             } else {
                   7307:                 for ( i = 0; i < nd_nvar; i++ ) {
                   7308:                     epos[i].i = nd_exporigin + i/nd_epw;
                   7309:                     epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   7310:                 }
                   7311:             }
                   7312:             break;
                   7313:         case 1: case 257:
                   7314:             /* block order */
                   7315:             l = ord->ord.block.length;
                   7316:             op = ord->ord.block.order_pair;
                   7317:             for ( j = 0, s = 0; j < l; j++ ) {
                   7318:                 ord_o = op[j].order;
                   7319:                 ord_l = op[j].length;
                   7320:                 if ( !ord_o )
                   7321:                     for ( i = 0; i < ord_l; i++ ) {
                   7322:                         epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   7323:                         epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   7324:                     }
                   7325:                 else
                   7326:                     for ( i = 0; i < ord_l; i++ ) {
                   7327:                         epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   7328:                         epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   7329:                     }
                   7330:                 s += ord_l;
                   7331:             }
                   7332:             break;
                   7333:         case 2:
                   7334:             /* matrix order */
                   7335:         case 3:
                   7336:             /* composite order */
                   7337:         default:
                   7338:             for ( i = 0; i < nd_nvar; i++ ) {
                   7339:                 epos[i].i = nd_exporigin + i/nd_epw;
                   7340:                 epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   7341:             }
                   7342:             break;
                   7343:     }
                   7344:     return epos;
                   7345: }
                   7346:
                   7347: /* external interface */
                   7348:
                   7349: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
                   7350: {
                   7351:     NODE t,in0,in;
                   7352:     ND ndf,nf;
                   7353:     NDV ndvf;
                   7354:     VL vv,tv;
                   7355:     int stat,nvar,max,mrank;
                   7356:     union oNDC dn;
                   7357:     Q cont;
                   7358:     P pp;
                   7359:     LIST ppl;
                   7360:
                   7361:     if ( !f ) {
                   7362:         *rp = 0;
                   7363:         return;
                   7364:     }
                   7365:     pltovl(v,&vv);
                   7366:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   7367:
                   7368:     /* max=65536 implies nd_bpe=32 */
                   7369:     max = 65536;
                   7370:
                   7371:   nd_module = 0;
                   7372:   /* nd_module will be set if ord is a module ordering */
                   7373:     nd_init_ord(ord);
                   7374:     nd_setup_parameters(nvar,max);
                   7375:     if ( nd_module && OID(f) != O_LIST )
                   7376:         error("nd_nf_p : the first argument must be a list");
                   7377:   if ( nd_module ) mrank = length(BDY((LIST)f));
                   7378:     /* conversion to ndv */
                   7379:     for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   7380:         NEXTNODE(in0,in);
                   7381:         if ( nd_module ) {
                   7382:           if ( !BDY(t) || OID(BDY(t)) != O_LIST
                   7383:                || length(BDY((LIST)BDY(t))) != mrank )
                   7384:               error("nd_nf_p : inconsistent basis element");
                   7385:           if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
                   7386:           else ppl = (LIST)BDY(t);
                   7387:           BDY(in) = (pointer)pltondv(CO,vv,ppl);
                   7388:         } else {
                   7389:           if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
                   7390:           else pp = (P)BDY(t);
                   7391:           BDY(in) = (pointer)ptondv(CO,vv,pp);
                   7392:         }
                   7393:         if ( m ) ndv_mod(m,(NDV)BDY(in));
                   7394:     }
                   7395:     if ( in0 ) NEXT(in) = 0;
                   7396:
                   7397:     if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
                   7398:     else ndvf = ptondv(CO,vv,(P)f);
                   7399:     if ( m ) ndv_mod(m,ndvf);
                   7400:     ndf = (pointer)ndvtond(m,ndvf);
                   7401:
                   7402:     /* dont sort, dont removecont */
1.24      noro     7403:     ndv_setup(m,0,in0,1,1,0);
1.1       noro     7404:     nd_scale=2;
1.6       noro     7405:     stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1       noro     7406:     if ( !stat )
                   7407:         error("nd_nf_p : exponent too large");
                   7408:     if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
                   7409:     else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
                   7410: }
                   7411:
                   7412: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   7413: {
                   7414:     NM m;
                   7415:     UINT *t,*s;
                   7416:     int i;
                   7417:
                   7418:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   7419:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7420:         t = DL(m);
                   7421:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   7422:         r[i] = CM(m);
                   7423:     }
                   7424:     for ( i = 0; !r[i]; i++ );
                   7425:     return i;
                   7426: }
                   7427:
                   7428: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
                   7429: {
                   7430:     NM m;
                   7431:     UINT *t,*s;
                   7432:     int i;
                   7433:
                   7434:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   7435:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7436:         t = DL(m);
                   7437:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     7438:         r[i] = CZ(m);
1.1       noro     7439:     }
                   7440:     for ( i = 0; !r[i]; i++ );
                   7441:     return i;
                   7442: }
                   7443:
                   7444: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
                   7445: {
                   7446:     NM m;
                   7447:     UINT *t,*s;
                   7448:     int i;
                   7449:
                   7450:     for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
                   7451:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7452:         t = DL(m);
                   7453:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   7454:         mpz_set(r[i],BDY(CZ(m)));
                   7455:     }
                   7456:     for ( i = 0; !mpz_sgn(r[i]); i++ );
                   7457:     return i;
                   7458: }
                   7459:
                   7460: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
                   7461: {
                   7462:     NM m;
                   7463:     unsigned long *v;
                   7464:     int i,j,h,size;
                   7465:   UINT *s,*t;
                   7466:
                   7467:   size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
                   7468:     v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
                   7469:     bzero(v,size);
                   7470:     for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
                   7471:     t = DL(m);
                   7472:     h = ndl_hash_value(t);
                   7473:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   7474:       v[i/BLEN] |= 1L <<(i%BLEN);
                   7475:     }
                   7476:     return v;
                   7477: }
                   7478:
                   7479: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
                   7480: {
                   7481:     NMV mr;
                   7482:     UINT *d,*t,*s;
                   7483:     int i,j,len,h,head;
                   7484:
                   7485:     d = DL(m);
                   7486:     len = LEN(p);
                   7487:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   7488:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   7489:         ndl_add(d,DL(mr),t);
                   7490:     h = ndl_hash_value(t);
                   7491:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   7492:     if ( j == 0 ) head = i;
                   7493:       v[i/BLEN] |= 1L <<(i%BLEN);
                   7494:     }
                   7495:     return head;
                   7496: }
                   7497:
                   7498: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   7499: {
                   7500:     NM m;
                   7501:     NMV mr;
                   7502:     UINT *d,*t,*s;
                   7503:     NDV p;
                   7504:     int i,j,len;
                   7505:     Z *r;
                   7506:
                   7507:     m = pair->mul;
                   7508:     d = DL(m);
                   7509:     p = nd_ps[pair->index];
                   7510:     len = LEN(p);
                   7511:     r = (Z *)CALLOC(n,sizeof(Q));
                   7512:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   7513:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   7514:         ndl_add(d,DL(mr),t);
                   7515:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     7516:         r[i] = CZ(mr);
1.1       noro     7517:     }
                   7518:     return r;
                   7519: }
                   7520:
1.11      noro     7521: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1       noro     7522: {
                   7523:     NM m;
                   7524:     NMV mr;
1.11      noro     7525:     UINT *d,*t,*s,*u;
1.1       noro     7526:     NDV p;
                   7527:     unsigned char *ivc;
                   7528:     unsigned short *ivs;
                   7529:     UINT *v,*ivi,*s0v;
1.11      noro     7530:     int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1       noro     7531:     IndArray r;
                   7532:
                   7533:     m = pair->mul;
                   7534:     d = DL(m);
                   7535:     if ( trace )
                   7536:       p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
                   7537:     else
                   7538:       p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
                   7539:
                   7540:     len = LEN(p);
                   7541:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   7542:     v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11      noro     7543:     for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
                   7544:       ndl_add(d,DL(mr),t);
                   7545:       st = prev;
                   7546:       ed = n;
                   7547:       while ( ed > st ) {
                   7548:         md = (st+ed)/2;
                   7549:         u = s0+md*nd_wpd;
                   7550:         c = DL_COMPARE(u,t);
                   7551:         if ( c == 0 ) break;
                   7552:         else if ( c > 0 ) st = md;
                   7553:         else ed = md;
                   7554:       }
                   7555:       prev = v[j] = md;
1.1       noro     7556:     }
                   7557:     r = (IndArray)MALLOC(sizeof(struct oIndArray));
                   7558:     r->head = v[0];
                   7559:     diff = 0;
                   7560:     for ( i = 1; i < len; i++ ) {
                   7561:         cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   7562:     }
                   7563:     if ( diff < 256 ) {
                   7564:         r->width = 1;
                   7565:         ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   7566:         r->index.c = ivc;
                   7567:         for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   7568:     } else if ( diff < 65536 ) {
                   7569:         r->width = 2;
                   7570:         ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   7571:         r->index.s = ivs;
                   7572:         for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   7573:     } else {
                   7574:         r->width = 4;
                   7575:         ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   7576:         r->index.i = ivi;
                   7577:         for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
                   7578:     }
                   7579:     return r;
                   7580: }
                   7581:
                   7582: int compress_array(Z *svect,Z *cvect,int n)
                   7583: {
                   7584:     int i,j;
                   7585:
                   7586:     for ( i = j = 0; i < n; i++ )
                   7587:         if ( svect[i] ) cvect[j++] = svect[i];
                   7588:     return j;
                   7589: }
                   7590:
                   7591: void expand_array(Z *svect,Z *cvect,int n)
                   7592: {
                   7593:     int i,j;
                   7594:
                   7595:     for ( i = j = 0; j < n;  i++  )
                   7596:         if ( svect[i] ) svect[i] = cvect[j++];
                   7597: }
                   7598:
1.8       noro     7599: #if 0
1.1       noro     7600: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7601: {
                   7602:     int i,j,k,len,pos,prev,nz;
                   7603:     Z cs,mcs,c1,c2,cr,gcd,t;
                   7604:     IndArray ivect;
                   7605:     unsigned char *ivc;
                   7606:     unsigned short *ivs;
                   7607:     unsigned int *ivi;
                   7608:     NDV redv;
                   7609:     NMV mr;
                   7610:     NODE rp;
                   7611:     int maxrs;
                   7612:     double hmag;
                   7613:     Z *cvect;
1.3       noro     7614:     int l;
1.1       noro     7615:
                   7616:     maxrs = 0;
                   7617:     for ( i = 0; i < col && !svect[i]; i++ );
                   7618:     if ( i == col ) return maxrs;
                   7619:     hmag = p_mag((P)svect[i])*nd_scale;
                   7620:     cvect = (Z *)MALLOC(col*sizeof(Q));
                   7621:     for ( i = 0; i < nred; i++ ) {
                   7622:         ivect = imat[i];
                   7623:         k = ivect->head;
                   7624:         if ( svect[k] ) {
                   7625:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7626:             redv = nd_demand?ndv_load(rp0[i]->index)
                   7627:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   7628:             len = LEN(redv); mr = BDY(redv);
1.6       noro     7629:             igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1       noro     7630:             chsgnz(cs,&mcs);
                   7631:             if ( !UNIQ(cr) ) {
                   7632:                 for ( j = 0; j < col; j++ ) {
                   7633:                     mulz(svect[j],cr,&c1); svect[j] = c1;
                   7634:                 }
                   7635:             }
                   7636:             svect[k] = 0; prev = k;
                   7637:             switch ( ivect->width ) {
                   7638:                 case 1:
                   7639:                     ivc = ivect->index.c;
                   7640:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7641:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     7642:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     7643:                     }
                   7644:                     break;
                   7645:                 case 2:
                   7646:                     ivs = ivect->index.s;
                   7647:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7648:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     7649:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     7650:                     }
                   7651:                     break;
                   7652:                 case 4:
                   7653:                     ivi = ivect->index.i;
                   7654:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7655:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     7656:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     7657:                     }
                   7658:                     break;
                   7659:             }
                   7660:             for ( j = k+1; j < col && !svect[j]; j++ );
                   7661:             if ( j == col ) break;
                   7662:             if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
                   7663:                 nz = compress_array(svect,cvect,col);
                   7664:                 removecont_array((P *)cvect,nz,1);
                   7665:                 expand_array(svect,cvect,nz);
                   7666:                 hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   7667:             }
                   7668:         }
                   7669:     }
                   7670:     nz = compress_array(svect,cvect,col);
                   7671:     removecont_array((P *)cvect,nz,1);
                   7672:     expand_array(svect,cvect,nz);
                   7673:     if ( DP_Print ) {
                   7674:         fprintf(asir_out,"-"); fflush(asir_out);
                   7675:     }
                   7676:     return maxrs;
                   7677: }
1.4       noro     7678: #else
1.9       noro     7679:
1.4       noro     7680: /* direct mpz version */
                   7681: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7682: {
                   7683:     int i,j,k,len,pos,prev;
                   7684:     mpz_t cs,cr,gcd;
                   7685:     IndArray ivect;
                   7686:     unsigned char *ivc;
                   7687:     unsigned short *ivs;
                   7688:     unsigned int *ivi;
                   7689:     NDV redv;
                   7690:     NMV mr;
                   7691:     NODE rp;
                   7692:     int maxrs;
                   7693:     double hmag;
                   7694:     int l;
1.13      noro     7695:     static mpz_t *svect;
                   7696:     static int svect_len=0;
1.4       noro     7697:
                   7698:     maxrs = 0;
                   7699:     for ( i = 0; i < col && !svect0[i]; i++ );
                   7700:     if ( i == col ) return maxrs;
                   7701:     hmag = p_mag((P)svect0[i])*nd_scale;
1.13      noro     7702:     if ( col > svect_len ) {
                   7703:       svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   7704:       svect_len = col;
                   7705:     }
1.4       noro     7706:     for ( i = 0; i < col; i++ ) {
                   7707:       mpz_init(svect[i]);
                   7708:       if ( svect0[i] )
                   7709:         mpz_set(svect[i],BDY(svect0[i]));
                   7710:       else
                   7711:         mpz_set_ui(svect[i],0);
                   7712:     }
                   7713:     mpz_init(gcd); mpz_init(cs); mpz_init(cr);
                   7714:     for ( i = 0; i < nred; i++ ) {
                   7715:         ivect = imat[i];
                   7716:         k = ivect->head;
                   7717:         if ( mpz_sgn(svect[k]) ) {
                   7718:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7719:             redv = nd_demand?ndv_load(rp0[i]->index)
                   7720:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   7721:             len = LEN(redv); mr = BDY(redv);
1.6       noro     7722:             mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4       noro     7723:             mpz_div(cs,svect[k],gcd);
1.6       noro     7724:             mpz_div(cr,BDY(CZ(mr)),gcd);
1.4       noro     7725:             mpz_neg(cs,cs);
1.9       noro     7726:             if ( MUNIMPZ(cr) )
                   7727:               for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
                   7728:             else if ( !UNIMPZ(cr) )
                   7729:               for ( j = 0; j < col; j++ ) {
                   7730:                 if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
                   7731:               }
1.4       noro     7732:             mpz_set_ui(svect[k],0);
                   7733:             prev = k;
                   7734:             switch ( ivect->width ) {
                   7735:                 case 1:
                   7736:                     ivc = ivect->index.c;
                   7737:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7738:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     7739:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     7740:                     }
                   7741:                     break;
                   7742:                 case 2:
                   7743:                     ivs = ivect->index.s;
                   7744:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7745:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     7746:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     7747:                     }
                   7748:                     break;
                   7749:                 case 4:
                   7750:                     ivi = ivect->index.i;
                   7751:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7752:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     7753:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     7754:                     }
                   7755:                     break;
                   7756:             }
                   7757:             for ( j = k+1; j < col && !svect[j]; j++ );
                   7758:             if ( j == col ) break;
                   7759:             if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
                   7760:                 mpz_removecont_array(svect,col);
                   7761:                 hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
                   7762:             }
                   7763:         }
                   7764:     }
                   7765:     mpz_removecont_array(svect,col);
                   7766:     if ( DP_Print ) {
                   7767:         fprintf(asir_out,"-"); fflush(asir_out);
                   7768:     }
                   7769:     for ( i = 0; i < col; i++ )
                   7770:       if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
                   7771:       else svect0[i] = 0;
                   7772:     return maxrs;
                   7773: }
                   7774: #endif
1.1       noro     7775:
1.40      noro     7776: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred,SIG sig)
1.1       noro     7777: {
                   7778:     int i,j,k,len,pos,prev;
                   7779:     UINT c,c1,c2,c3,up,lo,dmy;
                   7780:     IndArray ivect;
                   7781:     unsigned char *ivc;
                   7782:     unsigned short *ivs;
                   7783:     unsigned int *ivi;
                   7784:     NDV redv;
                   7785:     NMV mr;
                   7786:     NODE rp;
                   7787:     int maxrs;
                   7788:
                   7789:     maxrs = 0;
                   7790:     for ( i = 0; i < nred; i++ ) {
                   7791:         ivect = imat[i];
                   7792:         k = ivect->head; svect[k] %= m;
1.40      noro     7793:         if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.1       noro     7794:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7795:             c = m-c; redv = nd_ps[rp0[i]->index];
                   7796:             len = LEN(redv); mr = BDY(redv);
                   7797:             svect[k] = 0; prev = k;
                   7798:             switch ( ivect->width ) {
                   7799:                 case 1:
                   7800:                     ivc = ivect->index.c;
                   7801:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7802:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.40      noro     7803:                         if ( c1 ) {
                   7804:                           c2 = svect[pos];
1.1       noro     7805:                           DMA(c1,c,c2,up,lo);
                   7806:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   7807:                           } else svect[pos] = lo;
1.40      noro     7808:                         }
1.1       noro     7809:                     }
                   7810:                     break;
                   7811:                 case 2:
                   7812:                     ivs = ivect->index.s;
                   7813:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7814:                         pos = prev+ivs[j]; c1 = CM(mr);
                   7815:                         prev = pos;
1.40      noro     7816:                         if ( c1 ) {
                   7817:                           c2 = svect[pos];
1.1       noro     7818:                           DMA(c1,c,c2,up,lo);
                   7819:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   7820:                           } else svect[pos] = lo;
1.40      noro     7821:                         }
1.1       noro     7822:                     }
                   7823:                     break;
                   7824:                 case 4:
                   7825:                     ivi = ivect->index.i;
                   7826:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7827:                         pos = prev+ivi[j]; c1 = CM(mr);
                   7828:                         prev = pos;
1.40      noro     7829:                         if ( c1 ) {
                   7830:                           c2 = svect[pos];
1.1       noro     7831:                           DMA(c1,c,c2,up,lo);
                   7832:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   7833:                           } else svect[pos] = lo;
1.40      noro     7834:                         }
1.1       noro     7835:                     }
                   7836:                     break;
                   7837:             }
                   7838:         }
                   7839:     }
                   7840:     for ( i = 0; i < col; i++ )
                   7841:         if ( svect[i] >= (UINT)m ) svect[i] %= m;
                   7842:     return maxrs;
                   7843: }
                   7844:
                   7845: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7846: {
                   7847:     int i,j,k,len,pos,prev;
                   7848:     UINT c,c1,c2,c3,up,lo,dmy;
                   7849:     IndArray ivect;
                   7850:     unsigned char *ivc;
                   7851:     unsigned short *ivs;
                   7852:     unsigned int *ivi;
                   7853:     NDV redv;
                   7854:     NMV mr;
                   7855:     NODE rp;
                   7856:     int maxrs;
                   7857:
                   7858:     maxrs = 0;
                   7859:     for ( i = 0; i < nred; i++ ) {
                   7860:         ivect = imat[i];
                   7861:         k = ivect->head;
                   7862:         if ( (c = svect[k]) != 0 ) {
                   7863:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7864:             c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
                   7865:             len = LEN(redv); mr = BDY(redv);
                   7866:             svect[k] = 0; prev = k;
                   7867:             switch ( ivect->width ) {
                   7868:                 case 1:
                   7869:                     ivc = ivect->index.c;
                   7870:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7871:                         pos = prev+ivc[j]; prev = pos;
                   7872:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   7873:                     }
                   7874:                     break;
                   7875:                 case 2:
                   7876:                     ivs = ivect->index.s;
                   7877:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7878:                         pos = prev+ivs[j]; prev = pos;
                   7879:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   7880:                     }
                   7881:                     break;
                   7882:                 case 4:
                   7883:                     ivi = ivect->index.i;
                   7884:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7885:                         pos = prev+ivi[j]; prev = pos;
                   7886:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   7887:                     }
                   7888:                     break;
                   7889:             }
                   7890:         }
                   7891:     }
                   7892:     return maxrs;
                   7893: }
                   7894:
                   7895: ND nd_add_lf(ND p1,ND p2)
                   7896: {
                   7897:     int n,c,can;
                   7898:     ND r;
                   7899:     NM m1,m2,mr0,mr,s;
                   7900:     Z t;
                   7901:
                   7902:     if ( !p1 ) return p2;
                   7903:     else if ( !p2 ) return p1;
                   7904:     else {
                   7905:         can = 0;
                   7906:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   7907:             c = DL_COMPARE(DL(m1),DL(m2));
                   7908:             switch ( c ) {
                   7909:                 case 0:
                   7910:                     addlf(CZ(m1),CZ(m2),&t);
                   7911:                     s = m1; m1 = NEXT(m1);
                   7912:                     if ( t ) {
                   7913:                         can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
                   7914:                     } else {
                   7915:                         can += 2; FREENM(s);
                   7916:                     }
                   7917:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   7918:                     break;
                   7919:                 case 1:
                   7920:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   7921:                     break;
                   7922:                 case -1:
                   7923:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   7924:                     break;
                   7925:             }
                   7926:         }
                   7927:         if ( !mr0 )
                   7928:             if ( m1 ) mr0 = m1;
                   7929:             else if ( m2 ) mr0 = m2;
                   7930:             else return 0;
                   7931:         else if ( m1 ) NEXT(mr) = m1;
                   7932:         else if ( m2 ) NEXT(mr) = m2;
                   7933:         else NEXT(mr) = 0;
                   7934:         BDY(p1) = mr0;
                   7935:         SG(p1) = MAX(SG(p1),SG(p2));
                   7936:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   7937:         FREEND(p2);
                   7938:         return p1;
                   7939:     }
                   7940: }
                   7941:
                   7942: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7943: {
                   7944:     int i,j,k,len,pos,prev;
                   7945:     mpz_t c,mc,c1;
                   7946:     IndArray ivect;
                   7947:     unsigned char *ivc;
                   7948:     unsigned short *ivs;
                   7949:     unsigned int *ivi;
                   7950:     NDV redv;
                   7951:     NMV mr;
                   7952:     NODE rp;
                   7953:     int maxrs;
                   7954:
                   7955:     maxrs = 0;
                   7956:     lf_lazy = 1;
                   7957:     for ( i = 0; i < nred; i++ ) {
                   7958:         ivect = imat[i];
                   7959:         k = ivect->head;
                   7960:         mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
                   7961:         if ( mpz_sgn(svect[k]) ) {
                   7962:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7963:             mpz_neg(svect[k],svect[k]);
                   7964:             redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
                   7965:             len = LEN(redv); mr = BDY(redv);
                   7966:             prev = k;
                   7967:             switch ( ivect->width ) {
                   7968:                 case 1:
                   7969:                     ivc = ivect->index.c;
                   7970:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7971:                         pos = prev+ivc[j]; prev = pos;
                   7972:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   7973:                     }
                   7974:                     break;
                   7975:                 case 2:
                   7976:                     ivs = ivect->index.s;
                   7977:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7978:                         pos = prev+ivs[j]; prev = pos;
                   7979:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   7980:                     }
                   7981:                     break;
                   7982:                 case 4:
                   7983:                     ivi = ivect->index.i;
                   7984:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7985:                         pos = prev+ivi[j]; prev = pos;
                   7986:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   7987:                     }
                   7988:                     break;
                   7989:             }
                   7990:             mpz_set_ui(svect[k],0);
                   7991:         }
                   7992:     }
                   7993:     lf_lazy=0;
                   7994:     for ( i = 0; i < col; i++ ) {
                   7995:         mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
                   7996:     }
                   7997:     return maxrs;
                   7998: }
                   7999:
                   8000: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
                   8001: {
                   8002:     int i,j,k,l,rank,s;
                   8003:     mpz_t a,a1,inv;
                   8004:     mpz_t *t,*pivot,*pk;
                   8005:     mpz_t **mat;
                   8006:     struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
                   8007:     int size,size1;
                   8008:
                   8009:     mpz_init(inv);
                   8010:     mpz_init(a);
                   8011:     mat = (mpz_t **)mat0;
                   8012:         size = 0;
                   8013:     for ( rank = 0, j = 0; j < col; j++ ) {
                   8014:         for ( i = rank; i < row; i++ ) {
                   8015:             mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
                   8016:         }
                   8017:         for ( i = rank; i < row; i++ )
                   8018:             if ( mpz_sgn(mat[i][j]) )
                   8019:                 break;
                   8020:         if ( i == row ) {
                   8021:             colstat[j] = 0;
                   8022:             continue;
                   8023:         } else
                   8024:             colstat[j] = 1;
                   8025:         if ( i != rank ) {
                   8026:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   8027:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   8028:         }
                   8029:         pivot = mat[rank];
                   8030:         s = sugar[rank];
                   8031:         mpz_invert(inv,pivot[j],BDY(current_mod_lf));
                   8032:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   8033:             if ( mpz_sgn(*pk) ) {
                   8034:                 mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
                   8035:             }
                   8036:         for ( i = rank+1; i < row; i++ ) {
                   8037:             t = mat[i];
                   8038:             if ( mpz_sgn(t[j]) ) {
                   8039:                 sugar[i] = MAX(sugar[i],s);
                   8040:                 mpz_neg(a,t[j]);
                   8041:                 red_by_vect_lf(t+j,pivot+j,a,col-j);
                   8042:             }
                   8043:         }
                   8044:         rank++;
                   8045:     }
                   8046:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   8047:         if ( colstat[j] ) {
                   8048:             pivot = mat[l];
                   8049:             s = sugar[l];
                   8050:             for ( k = j; k < col; k++ )
                   8051:               mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
                   8052:             for ( i = 0; i < l; i++ ) {
                   8053:                 t = mat[i];
                   8054:                 if ( mpz_sgn(t[j]) ) {
                   8055:                     sugar[i] = MAX(sugar[i],s);
                   8056:                     mpz_neg(a,t[j]);
                   8057:                     red_by_vect_lf(t+j,pivot+j,a,col-j);
                   8058:                 }
                   8059:             }
                   8060:             l--;
                   8061:         }
                   8062:     for ( j = 0, l = 0; l < rank; j++ )
                   8063:         if ( colstat[j] ) {
                   8064:             t = mat[l];
                   8065:             for ( k = j; k < col; k++ ) {
                   8066:                 mpz_mod(t[k],t[k],BDY(current_mod_lf));
                   8067:             }
                   8068:             l++;
                   8069:         }
                   8070:     return rank;
                   8071: }
                   8072:
                   8073:
                   8074: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   8075: {
                   8076:     int j,k,len;
                   8077:     UINT *p;
                   8078:     UINT c;
                   8079:     NDV r;
                   8080:     NMV mr0,mr;
                   8081:
                   8082:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   8083:     if ( !len ) return 0;
                   8084:     else {
                   8085:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8086: #if 0
                   8087:         ndv_alloc += nmv_adv*len;
                   8088: #endif
                   8089:         mr = mr0;
                   8090:         p = s0vect;
                   8091:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   8092:             if ( !rhead[j] ) {
                   8093:                 if ( (c = vect[k++]) != 0 ) {
                   8094:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   8095:                 }
                   8096:             }
                   8097:         MKNDV(nd_nvar,mr0,len,r);
                   8098:         return r;
                   8099:     }
                   8100: }
                   8101:
1.40      noro     8102: NDV vect_to_ndv_s(UINT *vect,int col,UINT *s0vect)
                   8103: {
                   8104:     int j,k,len;
                   8105:     UINT *p;
                   8106:     UINT c;
                   8107:     NDV r;
                   8108:     NMV mr0,mr;
                   8109:
                   8110:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   8111:     if ( !len ) return 0;
                   8112:     else {
                   8113:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8114:         mr = mr0;
                   8115:         p = s0vect;
                   8116:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   8117:           if ( (c = vect[k++]) != 0 ) {
                   8118:             ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   8119:           }
                   8120:         MKNDV(nd_nvar,mr0,len,r);
                   8121:         return r;
                   8122:     }
                   8123: }
                   8124:
1.1       noro     8125: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
                   8126: {
                   8127:     int j,k,len;
                   8128:     UINT *p;
                   8129:     NDV r;
                   8130:     NMV mr0,mr;
                   8131:
                   8132:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
                   8133:     if ( !len ) return 0;
                   8134:     else {
                   8135:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8136:         mr = mr0;
                   8137:         p = s0vect;
                   8138:         for ( j = 0; j < col; j++, p += nd_wpd )
                   8139:       if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
                   8140:             ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
                   8141:           }
                   8142:         MKNDV(nd_nvar,mr0,len,r);
                   8143:         return r;
                   8144:     }
                   8145: }
                   8146:
                   8147: /* for preprocessed vector */
                   8148:
                   8149: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   8150: {
1.6       noro     8151:   int j,k,len;
                   8152:   UINT *p;
                   8153:   Z c;
                   8154:   NDV r;
                   8155:   NMV mr0,mr;
1.1       noro     8156:
1.6       noro     8157:   for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   8158:   if ( !len ) return 0;
                   8159:   else {
                   8160:     mr0 = (NMV)MALLOC(nmv_adv*len);
1.1       noro     8161: #if 0
1.6       noro     8162:     ndv_alloc += nmv_adv*len;
1.1       noro     8163: #endif
1.6       noro     8164:     mr = mr0;
                   8165:     p = s0vect;
                   8166:     for ( j = k = 0; j < col; j++, p += nd_wpd ) {
                   8167:       if ( !rhead[j] ) {
                   8168:         if ( (c = vect[k++]) != 0 ) {
                   8169:           if ( !INT(c) )
                   8170:             error("vect_to_ndv_q : components must be integers");
                   8171:             ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
                   8172:         }
                   8173:       }
1.1       noro     8174:     }
1.6       noro     8175:     MKNDV(nd_nvar,mr0,len,r);
                   8176:     return r;
                   8177:   }
1.1       noro     8178: }
                   8179:
                   8180: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   8181: {
                   8182:     int j,k,len;
                   8183:     UINT *p;
                   8184:     mpz_t c;
                   8185:     NDV r;
                   8186:     NMV mr0,mr;
                   8187:
                   8188:     for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
                   8189:     if ( !len ) return 0;
                   8190:     else {
                   8191:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   8192: #if 0
                   8193:         ndv_alloc += nmv_adv*len;
                   8194: #endif
                   8195:         mr = mr0;
                   8196:         p = s0vect;
                   8197:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   8198:             if ( !rhead[j] ) {
                   8199:                 c[0] = vect[k++][0];
                   8200:                 if ( mpz_sgn(c) ) {
                   8201:                     ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
                   8202:                 }
                   8203:             }
                   8204:         MKNDV(nd_nvar,mr0,len,r);
                   8205:         return r;
                   8206:     }
                   8207: }
                   8208:
                   8209: /* for plain vector */
                   8210:
                   8211: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
                   8212: {
                   8213:     int j,k,len;
                   8214:     UINT *p;
                   8215:     Z c;
                   8216:     NDV r;
                   8217:     NMV mr0,mr;
                   8218:
                   8219:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   8220:     if ( !len ) return 0;
                   8221:     else {
                   8222:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   8223: #if 0
                   8224:         ndv_alloc += nmv_adv*len;
                   8225: #endif
                   8226:         mr = mr0;
                   8227:         p = s0vect;
                   8228:         for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   8229:             if ( (c = vect[k]) != 0 ) {
                   8230:                 if ( !INT(c) )
1.6       noro     8231:                     error("plain_vect_to_ndv_q : components must be integers");
                   8232:                 ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1       noro     8233:             }
                   8234:         MKNDV(nd_nvar,mr0,len,r);
                   8235:         return r;
                   8236:     }
                   8237: }
                   8238:
                   8239: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
                   8240: {
                   8241:     ND_pairs t;
                   8242:     NODE sp0,sp;
                   8243:     int stat;
                   8244:     ND spol;
                   8245:
                   8246:     for ( t = l; t; t = NEXT(t) ) {
                   8247:         stat = nd_sp(m,trace,t,&spol);
                   8248:         if ( !stat ) return 0;
                   8249:         if ( spol ) {
                   8250:             add_pbucket_symbolic(bucket,spol);
                   8251:         }
                   8252:     }
                   8253:     return 1;
                   8254: }
                   8255:
                   8256: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   8257: {
                   8258:     NODE rp0,rp;
                   8259:     NM mul,head,s0,s;
                   8260:     int index,col,i,sugar;
                   8261:     RHist h;
                   8262:     UINT *s0v,*p;
                   8263:     NM_ind_pair pair;
                   8264:     ND red;
                   8265:     NDV *ps;
                   8266:
                   8267:     s0 = 0; rp0 = 0; col = 0;
                   8268:   if ( nd_demand )
                   8269:       ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   8270:   else
                   8271:       ps = trace?nd_ps_trace:nd_ps;
                   8272:     while ( 1 ) {
                   8273:         head = remove_head_pbucket_symbolic(bucket);
                   8274:         if ( !head ) break;
                   8275:         if ( !s0 ) s0 = head;
                   8276:         else NEXT(s) = head;
                   8277:         s = head;
                   8278:         index = ndl_find_reducer(DL(head));
                   8279:         if ( index >= 0 ) {
                   8280:             h = nd_psh[index];
                   8281:             NEWNM(mul);
                   8282:             ndl_sub(DL(head),DL(h),DL(mul));
                   8283:             if ( ndl_check_bound2(index,DL(mul)) )
                   8284:                 return 0;
                   8285:             sugar = TD(DL(mul))+SG(ps[index]);
1.28      noro     8286:             MKNM_ind_pair(pair,mul,index,sugar,0);
1.1       noro     8287:             red = ndv_mul_nm_symbolic(mul,ps[index]);
                   8288:             add_pbucket_symbolic(bucket,nd_remove_head(red));
                   8289:             NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   8290:         }
                   8291:         col++;
                   8292:     }
                   8293:     if ( rp0 ) NEXT(rp) = 0;
                   8294:     NEXT(s) = 0;
                   8295:     s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   8296:     for ( i = 0, p = s0v, s = s0; i < col;
                   8297:         i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   8298:     *s0vect = s0v;
                   8299:     *r = rp0;
                   8300:     return col;
                   8301: }
                   8302:
                   8303: void print_ndp(ND_pairs l)
                   8304: {
                   8305:   ND_pairs t;
                   8306:
                   8307:   for ( t = l; t; t = NEXT(t) )
                   8308:     printf("[%d,%d] ",t->i1,t->i2);
                   8309:   printf("\n");
                   8310: }
                   8311:
                   8312: NODE nd_f4(int m,int checkonly,int **indp)
                   8313: {
                   8314:     int i,nh,stat,index,f4red;
                   8315:     NODE r,g,tn0,tn,node;
                   8316:     ND_pairs d,l,t,ll0,ll,lh;
                   8317:   LIST l0,l1;
                   8318:     ND spol,red;
                   8319:     NDV nf,redv;
                   8320:     NM s0,s;
                   8321:     NODE rp0,srp0,nflist,nzlist,nzlist_t;
                   8322:     int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
                   8323:     UINT c;
                   8324:     UINT **spmat;
                   8325:     UINT *s0vect,*svect,*p,*v;
                   8326:     int *colstat;
                   8327:     IndArray *imat;
                   8328:     int *rhead;
                   8329:     int spcol,sprow;
                   8330:     int sugar,sugarh;
                   8331:     PGeoBucket bucket;
                   8332:     struct oEGT eg0,eg1,eg_f4;
                   8333:     Z i1,i2,sugarq;
1.12      noro     8334:
                   8335:     init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1       noro     8336: #if 0
                   8337:     ndv_alloc = 0;
                   8338: #endif
1.11      noro     8339:     Nf4_red=0;
1.1       noro     8340:     g = 0; d = 0;
                   8341:     for ( i = 0; i < nd_psn; i++ ) {
                   8342:         d = update_pairs(d,g,i,0);
                   8343:         g = update_base(g,i);
                   8344:     }
                   8345:   nzlist = 0;
                   8346:     nzlist_t = nd_nzlist;
                   8347:     f4red = 1;
                   8348:     nd_last_nonzero = 0;
                   8349:     while ( d ) {
                   8350:         get_eg(&eg0);
                   8351:         l = nd_minsugarp(d,&d);
                   8352:         sugar = nd_sugarweight?l->sugar2:SG(l);
                   8353:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   8354:         if ( nzlist_t ) {
                   8355:             node = BDY((LIST)BDY(nzlist_t));
1.6       noro     8356:             sugarh = ZTOS((Q)ARG0(node));
1.1       noro     8357:             tn = BDY((LIST)ARG1(node));
                   8358:             if ( !tn ) {
                   8359:               nzlist_t = NEXT(nzlist_t);
                   8360:               continue;
                   8361:             }
                   8362:             /* tn = [[i1,i2],...] */
                   8363:             lh = nd_ipairtospair(tn);
                   8364:         }
                   8365:         bucket = create_pbucket();
                   8366:         stat = nd_sp_f4(m,0,l,bucket);
                   8367:         if ( !stat ) {
                   8368:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8369:             NEXT(t) = d; d = l;
                   8370:             d = nd_reconstruct(0,d);
                   8371:             continue;
                   8372:         }
                   8373:         if ( bucket->m < 0 ) continue;
                   8374:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   8375:         if ( !col ) {
                   8376:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8377:             NEXT(t) = d; d = l;
                   8378:             d = nd_reconstruct(0,d);
                   8379:             continue;
                   8380:         }
1.12      noro     8381:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1       noro     8382:         if ( DP_Print )
1.6       noro     8383:             fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5       noro     8384:                 sugar,eg_f4.exectime);
1.1       noro     8385:         nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
                   8386:         if ( checkonly && nflist ) return 0;
                   8387:         /* adding new bases */
                   8388:         if ( nflist ) nd_last_nonzero = f4red;
                   8389:         for ( r = nflist; r; r = NEXT(r) ) {
                   8390:             nf = (NDV)BDY(r);
1.38      noro     8391:             if ( nd_f4_td ) SG(nf) = nd_tdeg(nf);
1.1       noro     8392:             ndv_removecont(m,nf);
                   8393:             if ( !m && nd_nalg ) {
                   8394:                 ND nf1;
                   8395:
                   8396:                 nf1 = ndvtond(m,nf);
                   8397:                 nd_monic(0,&nf1);
                   8398:                 nd_removecont(m,nf1);
                   8399:                 nf = ndtondv(m,nf1);
                   8400:             }
1.24      noro     8401:             nh = ndv_newps(m,nf,0);
1.1       noro     8402:             d = update_pairs(d,g,nh,0);
                   8403:             g = update_base(g,nh);
                   8404:         }
                   8405:         if ( DP_Print ) {
                   8406:           fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
                   8407:         }
                   8408:         if ( nd_gentrace ) {
                   8409:       for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
                   8410:         NEXTNODE(tn0,tn);
1.6       noro     8411:                 STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1       noro     8412:                 node = mknode(2,i1,i2); MKLIST(l0,node);
                   8413:         BDY(tn) = l0;
                   8414:       }
                   8415:       if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6       noro     8416:             STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1       noro     8417:             MKNODE(node,l1,nzlist); nzlist = node;
                   8418:         }
                   8419:         if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
                   8420:         f4red++;
                   8421:         if ( nd_f4red && f4red > nd_f4red ) break;
                   8422:         if ( nd_rank0 && !nflist ) break;
                   8423:     }
                   8424:     if ( nd_gentrace ) {
                   8425:     MKLIST(l0,reverse_node(nzlist));
                   8426:         MKNODE(nd_alltracelist,l0,0);
                   8427:     }
                   8428: #if 0
                   8429:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   8430: #endif
1.12      noro     8431:   if ( DP_Print ) {
                   8432:     fprintf(asir_out,"number of red=%d,",Nf4_red);
                   8433:     fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
                   8434:       f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
1.41      noro     8435:     fprintf(asir_out,"number of removed pairs=%d\n,",NcriB+NcriMF+Ncri2);
1.12      noro     8436:   }
1.1       noro     8437:   conv_ilist(nd_demand,0,g,indp);
                   8438:     return g;
                   8439: }
                   8440:
                   8441: NODE nd_f4_trace(int m,int **indp)
                   8442: {
                   8443:     int i,nh,stat,index;
                   8444:     NODE r,g;
                   8445:     ND_pairs d,l,l0,t;
                   8446:     ND spol,red;
                   8447:     NDV nf,redv,nfqv,nfv;
                   8448:     NM s0,s;
                   8449:     NODE rp0,srp0,nflist;
                   8450:     int nsp,nred,col,rank,len,k,j,a;
                   8451:     UINT c;
                   8452:     UINT **spmat;
                   8453:     UINT *s0vect,*svect,*p,*v;
                   8454:     int *colstat;
                   8455:     IndArray *imat;
                   8456:     int *rhead;
                   8457:     int spcol,sprow;
                   8458:     int sugar;
                   8459:     PGeoBucket bucket;
                   8460:     struct oEGT eg0,eg1,eg_f4;
                   8461:
                   8462:     g = 0; d = 0;
                   8463:     for ( i = 0; i < nd_psn; i++ ) {
                   8464:         d = update_pairs(d,g,i,0);
                   8465:         g = update_base(g,i);
                   8466:     }
                   8467:     while ( d ) {
                   8468:         get_eg(&eg0);
                   8469:         l = nd_minsugarp(d,&d);
                   8470:         sugar = SG(l);
                   8471:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   8472:         bucket = create_pbucket();
                   8473:         stat = nd_sp_f4(m,0,l,bucket);
                   8474:         if ( !stat ) {
                   8475:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8476:             NEXT(t) = d; d = l;
                   8477:             d = nd_reconstruct(1,d);
                   8478:             continue;
                   8479:         }
                   8480:         if ( bucket->m < 0 ) continue;
                   8481:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   8482:         if ( !col ) {
                   8483:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8484:             NEXT(t) = d; d = l;
                   8485:             d = nd_reconstruct(1,d);
                   8486:             continue;
                   8487:         }
                   8488:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   8489:         if ( DP_Print )
1.3       noro     8490:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5       noro     8491:                 sugar,eg_f4.exectime);
1.1       noro     8492:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   8493:         if ( !l0 ) continue;
                   8494:         l = l0;
                   8495:
                   8496:         /* over Q */
                   8497:         bucket = create_pbucket();
                   8498:         stat = nd_sp_f4(0,1,l,bucket);
                   8499:         if ( !stat ) {
                   8500:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8501:             NEXT(t) = d; d = l;
                   8502:             d = nd_reconstruct(1,d);
                   8503:             continue;
                   8504:         }
                   8505:         if ( bucket->m < 0 ) continue;
                   8506:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   8507:         if ( !col ) {
                   8508:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8509:             NEXT(t) = d; d = l;
                   8510:             d = nd_reconstruct(1,d);
                   8511:             continue;
                   8512:         }
                   8513:         nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   8514:         /* adding new bases */
                   8515:         for ( r = nflist; r; r = NEXT(r) ) {
                   8516:             nfqv = (NDV)BDY(r);
                   8517:             ndv_removecont(0,nfqv);
1.6       noro     8518:             if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1       noro     8519:             if ( nd_nalg ) {
                   8520:                 ND nf1;
                   8521:
                   8522:                 nf1 = ndvtond(m,nfqv);
                   8523:                 nd_monic(0,&nf1);
                   8524:                 nd_removecont(0,nf1);
                   8525:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   8526:             }
                   8527:             nfv = ndv_dup(0,nfqv);
                   8528:             ndv_mod(m,nfv);
                   8529:             ndv_removecont(m,nfv);
1.24      noro     8530:             nh = ndv_newps(0,nfv,nfqv);
1.1       noro     8531:             d = update_pairs(d,g,nh,0);
                   8532:             g = update_base(g,nh);
                   8533:         }
                   8534:     }
                   8535: #if 0
                   8536:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   8537: #endif
                   8538:   conv_ilist(nd_demand,1,g,indp);
                   8539:     return g;
                   8540: }
                   8541:
                   8542: int rref(matrix mat,int *sugar)
                   8543: {
                   8544:   int row,col,i,j,k,l,s,wcol,wj;
                   8545:   unsigned long bj;
                   8546:   unsigned long **a;
                   8547:   unsigned long *ai,*ak,*as,*t;
                   8548:   int *pivot;
                   8549:
                   8550:   row = mat->row;
                   8551:   col = mat->col;
                   8552:   a = mat->a;
                   8553:   wcol = (col+BLEN-1)/BLEN;
                   8554:   pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   8555:   i = 0;
                   8556:   for ( j = 0; j < col; j++ ) {
                   8557:   wj = j/BLEN; bj = 1L<<(j%BLEN);
                   8558:     for ( k = i; k < row; k++ )
                   8559:     if ( a[k][wj] & bj ) break;
                   8560:     if ( k == row ) continue;
                   8561:   pivot[i] = j;
                   8562:     if ( k != i ) {
                   8563:    t = a[i]; a[i] = a[k]; a[k] = t;
                   8564:    s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
                   8565:   }
                   8566:   ai = a[i];
                   8567:     for ( k = i+1; k < row; k++ ) {
                   8568:     ak = a[k];
                   8569:     if ( ak[wj] & bj ) {
                   8570:       for ( l = wj; l < wcol; l++ )
                   8571:       ak[l] ^= ai[l];
                   8572:       sugar[k] = MAX(sugar[k],sugar[i]);
                   8573:     }
                   8574:   }
                   8575:   i++;
                   8576:   }
                   8577:   for ( k = i-1; k >= 0; k-- ) {
                   8578:     j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
                   8579:   ak = a[k];
                   8580:     for ( s = 0; s < k; s++ ) {
                   8581:     as = a[s];
                   8582:       if ( as[wj] & bj ) {
                   8583:         for ( l = wj; l < wcol; l++ )
                   8584:       as[l] ^= ak[l];
                   8585:       sugar[s] = MAX(sugar[s],sugar[k]);
                   8586:     }
                   8587:   }
                   8588:   }
                   8589:   return i;
                   8590: }
                   8591:
                   8592: void print_matrix(matrix mat)
                   8593: {
                   8594:   int row,col,i,j;
                   8595:   unsigned long *ai;
                   8596:
                   8597:   row = mat->row;
                   8598:   col = mat->col;
                   8599:   printf("%d x %d\n",row,col);
                   8600:   for ( i = 0; i < row; i++ ) {
                   8601:   ai = mat->a[i];
                   8602:     for ( j = 0; j < col; j++ ) {
                   8603:     if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
                   8604:     else putchar('0');
                   8605:   }
                   8606:   putchar('\n');
                   8607:   }
                   8608: }
                   8609:
                   8610: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
                   8611:
                   8612: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
                   8613: {
                   8614:   int row,col,wcol,wj,i,j;
                   8615:   unsigned long bj;
                   8616:   unsigned long *ai;
                   8617:   unsigned long **a;
                   8618:   int len;
                   8619:   int *pos;
                   8620:
                   8621:   row = mat->row;
                   8622:   col = mat->col;
                   8623:   wcol = (col+BLEN-1)/BLEN;
                   8624:   pos = (int *)MALLOC(wcol*sizeof(int));
                   8625:   bzero(pos,wcol*sizeof(int));
                   8626:   for ( i = j = 0; i < wcol; i++ )
                   8627:     if ( v[i] ) pos[j++] = i;;
                   8628:   len = j;
                   8629:   wj = rhead/BLEN;
                   8630:   bj = 1L<<rhead%BLEN;
                   8631:   a = mat->a;
                   8632:   for ( i = 0; i < row; i++ ) {
                   8633:   ai = a[i];
                   8634:     if ( ai[wj]&bj ) {
                   8635:     for ( j = 0; j < len; j++ )
                   8636:       ai[pos[j]] ^= v[pos[j]];
                   8637:     sugar[i] = MAX(sugar[i],rsugar);
                   8638:   }
                   8639:   }
                   8640: }
                   8641:
                   8642: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   8643: {
                   8644:     int nsp,nred,i,i0,k,rank,row;
                   8645:     NODE r0,rp;
                   8646:     ND_pairs sp;
                   8647:   ND spol;
                   8648:   NM_ind_pair rt;
                   8649:     int *s0hash;
                   8650:   UINT *s;
                   8651:   int *pivot,*sugar,*head;
                   8652:   matrix mat;
                   8653:     NM m;
                   8654:     NODE r;
                   8655:   struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
                   8656:   int rhead,rsugar,size;
                   8657:     unsigned long *v;
                   8658:
                   8659:     get_eg(&eg0);
                   8660:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   8661:     nred = length(rp0);
                   8662:     mat = alloc_matrix(nsp,col);
                   8663:     s0hash = (int *)MALLOC(col*sizeof(int));
                   8664:     for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
                   8665:         s0hash[i] = ndl_hash_value(s);
                   8666:
                   8667:   sugar = (int *)MALLOC(nsp*sizeof(int));
                   8668:   for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
                   8669:     nd_sp(2,0,sp,&spol);
                   8670:     if ( spol ) {
                   8671:         mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
                   8672:       sugar[i] = SG(spol);
                   8673:       i++;
                   8674:     }
                   8675:   }
                   8676:   mat->row = i;
                   8677:     if ( DP_Print ) {
                   8678:       fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
                   8679:     }
                   8680:   size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
                   8681:   v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
                   8682:     for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
                   8683:     rt = (NM_ind_pair)BDY(rp);
                   8684:     bzero(v,size);
                   8685:         rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
                   8686:     rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
                   8687:       red_by_vect_2(mat,sugar,v,rhead,rsugar);
                   8688:   }
                   8689:
                   8690:     get_eg(&eg1);
                   8691:     init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
                   8692:   rank = rref(mat,sugar);
                   8693:
                   8694:     for ( i = 0, r0 = 0; i < rank; i++ ) {
                   8695:       NEXTNODE(r0,r);
                   8696:     BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
                   8697:       SG((NDV)BDY(r)) = sugar[i];
                   8698:     }
                   8699:     if ( r0 ) NEXT(r) = 0;
                   8700:     get_eg(&eg2);
                   8701:     init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
                   8702:     if ( DP_Print ) {
                   8703:         fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5       noro     8704:       eg_elim1.exectime,eg_elim2.exectime);
1.1       noro     8705:         fflush(asir_out);
                   8706:   }
                   8707:     return r0;
                   8708: }
                   8709:
                   8710:
                   8711: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   8712: {
                   8713:     IndArray *imat;
1.11      noro     8714:     int nsp,nred,i,start;
1.1       noro     8715:     int *rhead;
                   8716:     NODE r0,rp;
                   8717:     ND_pairs sp;
                   8718:     NM_ind_pair *rvect;
                   8719:     UINT *s;
                   8720:     int *s0hash;
1.11      noro     8721:     struct oEGT eg0,eg1,eg_conv;
1.1       noro     8722:
                   8723:     if ( m == 2 && nd_rref2 )
                   8724:      return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
                   8725:
                   8726:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   8727:     nred = length(rp0);
                   8728:     imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   8729:     rhead = (int *)MALLOC(col*sizeof(int));
                   8730:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   8731:
                   8732:     /* construction of index arrays */
1.11      noro     8733:     get_eg(&eg0);
1.1       noro     8734:     if ( DP_Print ) {
1.11      noro     8735:       fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   8736:       fflush(asir_out);
1.1       noro     8737:     }
                   8738:     rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11      noro     8739:     for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1       noro     8740:         rvect[i] = (NM_ind_pair)BDY(rp);
1.11      noro     8741:         imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1       noro     8742:         rhead[imat[i]->head] = 1;
1.11      noro     8743:         start = imat[i]->head;
                   8744:     }
1.12      noro     8745:     get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11      noro     8746:     if ( DP_Print ) {
                   8747:       fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   8748:       fflush(asir_out);
1.1       noro     8749:     }
                   8750:     if ( m > 0 )
1.7       noro     8751: #if SIZEOF_LONG==8
1.1       noro     8752:         r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   8753: #else
                   8754:         r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   8755: #endif
                   8756:     else if ( m == -1 )
                   8757:         r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   8758:     else if ( m == -2 )
                   8759:         r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   8760:     else
                   8761:         r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   8762:     return r0;
                   8763: }
                   8764:
                   8765: /* for Fp, 2<=p<2^16 */
                   8766:
                   8767: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   8768:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   8769: {
                   8770:     int spcol,sprow,a;
                   8771:     int i,j,k,l,rank;
                   8772:     NODE r0,r;
                   8773:     ND_pairs sp;
                   8774:     ND spol;
                   8775:     UINT **spmat;
                   8776:     UINT *svect,*v;
                   8777:     int *colstat;
                   8778:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8779:     int maxrs;
                   8780:     int *spsugar;
                   8781:     ND_pairs *spactive;
                   8782:
                   8783:     spcol = col-nred;
                   8784:     get_eg(&eg0);
                   8785:     /* elimination (1st step) */
                   8786:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   8787:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   8788:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8789:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   8790:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8791:         nd_sp(m,0,sp,&spol);
                   8792:         if ( !spol ) continue;
                   8793:         nd_to_vect(m,s0vect,col,spol,svect);
                   8794:         if ( m == -1 )
                   8795:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   8796:         else
1.40      noro     8797:             maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,0);
1.1       noro     8798:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   8799:         if ( i < col ) {
                   8800:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   8801:             for ( j = k = 0; j < col; j++ )
                   8802:                 if ( !rhead[j] ) v[k++] = svect[j];
                   8803:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8804:             if ( nz )
                   8805:             spactive[sprow] = sp;
                   8806:             sprow++;
                   8807:         }
                   8808:         nd_free(spol);
                   8809:     }
                   8810:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8811:     if ( DP_Print ) {
1.5       noro     8812:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8813:         fflush(asir_out);
                   8814:     }
                   8815:     /* free index arrays */
                   8816:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   8817:
                   8818:     /* elimination (2nd step) */
                   8819:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8820:     if ( m == -1 )
                   8821:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   8822:     else
                   8823:         rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   8824:     r0 = 0;
                   8825:     for ( i = 0; i < rank; i++ ) {
                   8826:         NEXTNODE(r0,r); BDY(r) =
                   8827:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   8828:         SG((NDV)BDY(r)) = spsugar[i];
                   8829:         GCFREE(spmat[i]);
                   8830:     }
                   8831:     if ( r0 ) NEXT(r) = 0;
                   8832:
                   8833:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   8834:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8835:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8836:     if ( DP_Print ) {
1.5       noro     8837:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8838:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8839:             nsp,nred,sprow,spcol,rank);
1.5       noro     8840:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     8841:     }
                   8842:     if ( nz ) {
                   8843:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   8844:         if ( rank > 0 ) {
                   8845:             NEXT(spactive[rank-1]) = 0;
                   8846:             *nz = spactive[0];
                   8847:         } else
                   8848:             *nz = 0;
                   8849:     }
                   8850:     return r0;
                   8851: }
                   8852:
1.40      noro     8853: NODE nd_f4_red_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   8854:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
                   8855: {
                   8856:     int spcol,sprow,a;
                   8857:     int i,j,k,l,rank;
                   8858:     NODE r0,r;
                   8859:     ND_pairs sp;
                   8860:     ND spol;
                   8861:     UINT **spmat;
                   8862:     UINT *svect,*cvect;
                   8863:     UINT *v;
                   8864:     int *colstat;
                   8865:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8866:     int maxrs;
                   8867:     int *spsugar;
                   8868:     ND_pairs *spactive;
                   8869:     SIG *spsig;
                   8870:
                   8871:     get_eg(&eg0);
                   8872:     /* elimination (1st step) */
                   8873:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   8874:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8875:     spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
                   8876:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8877:         nd_sp(m,0,sp,&spol);
                   8878:         if ( !spol ) {
                   8879:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
                   8880:           continue;
                   8881:         }
                   8882:         svect = (UINT *)MALLOC(col*sizeof(UINT));
                   8883:         nd_to_vect(m,s0vect,col,spol,svect);
                   8884:         maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,spol->sig);
                   8885:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   8886:         if ( i < col ) {
                   8887:             spmat[sprow] = svect;
                   8888:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8889:             spsig[sprow] = sp->sig;
                   8890:             sprow++;
                   8891:         } else {
                   8892:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
                   8893:         }
                   8894:         nd_free(spol);
                   8895:     }
                   8896:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
                   8897:     if ( DP_Print ) {
                   8898:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   8899:         fflush(asir_out);
                   8900:     }
                   8901:     /* free index arrays */
                   8902:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   8903:
                   8904:     /* elimination (2nd step) */
                   8905:     colstat = (int *)MALLOC(col*sizeof(int));
                   8906:     rank = nd_gauss_elim_mod_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
                   8907:     r0 = 0;
                   8908:     for ( i = 0; i < sprow; i++ ) {
                   8909:         if ( spsugar[i] >= 0 ) {
                   8910:           NEXTNODE(r0,r);
                   8911:           BDY(r) = vect_to_ndv_s(spmat[i],col,s0vect);
                   8912:           SG((NDV)BDY(r)) = spsugar[i];
                   8913:           ((NDV)BDY(r))->sig = spsig[i];
                   8914:         } else
                   8915:           syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
                   8916:         GCFREE(spmat[i]);
                   8917:     }
                   8918:     if ( r0 ) NEXT(r) = 0;
                   8919:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
                   8920:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8921:     if ( DP_Print ) {
                   8922:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   8923:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8924:             nsp,nred,sprow,col,rank);
                   8925:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   8926:     }
                   8927:     return r0;
                   8928: }
                   8929:
1.1       noro     8930:
                   8931: /* for small finite fields */
                   8932:
                   8933: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   8934:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   8935: {
                   8936:     int spcol,sprow,a;
                   8937:     int i,j,k,l,rank;
                   8938:     NODE r0,r;
                   8939:     ND_pairs sp;
                   8940:     ND spol;
                   8941:     UINT **spmat;
                   8942:     UINT *svect,*v;
                   8943:     int *colstat;
                   8944:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8945:     int maxrs;
                   8946:     int *spsugar;
                   8947:     ND_pairs *spactive;
                   8948:
                   8949:     spcol = col-nred;
                   8950:     get_eg(&eg0);
                   8951:     /* elimination (1st step) */
                   8952:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   8953:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   8954:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8955:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   8956:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8957:         nd_sp(m,0,sp,&spol);
                   8958:         if ( !spol ) continue;
                   8959:         nd_to_vect(m,s0vect,col,spol,svect);
                   8960:         maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   8961:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   8962:         if ( i < col ) {
                   8963:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   8964:             for ( j = k = 0; j < col; j++ )
                   8965:                 if ( !rhead[j] ) v[k++] = svect[j];
                   8966:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8967:             if ( nz )
                   8968:             spactive[sprow] = sp;
                   8969:             sprow++;
                   8970:         }
                   8971:         nd_free(spol);
                   8972:     }
                   8973:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8974:     if ( DP_Print ) {
1.5       noro     8975:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8976:         fflush(asir_out);
                   8977:     }
                   8978:     /* free index arrays */
                   8979:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   8980:
                   8981:     /* elimination (2nd step) */
                   8982:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8983:     rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   8984:     r0 = 0;
                   8985:     for ( i = 0; i < rank; i++ ) {
                   8986:         NEXTNODE(r0,r); BDY(r) =
                   8987:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   8988:         SG((NDV)BDY(r)) = spsugar[i];
                   8989:         GCFREE(spmat[i]);
                   8990:     }
                   8991:     if ( r0 ) NEXT(r) = 0;
                   8992:
                   8993:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   8994:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8995:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8996:     if ( DP_Print ) {
1.5       noro     8997:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8998:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8999:             nsp,nred,sprow,spcol,rank);
1.5       noro     9000:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9001:     }
                   9002:     if ( nz ) {
                   9003:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   9004:         if ( rank > 0 ) {
                   9005:             NEXT(spactive[rank-1]) = 0;
                   9006:             *nz = spactive[0];
                   9007:         } else
                   9008:             *nz = 0;
                   9009:     }
                   9010:     return r0;
                   9011: }
                   9012:
                   9013: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   9014:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   9015: {
                   9016:     int spcol,sprow,a;
                   9017:     int i,j,k,l,rank;
                   9018:     NODE r0,r;
                   9019:     ND_pairs sp;
                   9020:     ND spol;
                   9021:     mpz_t **spmat;
                   9022:     mpz_t *svect,*v;
                   9023:     int *colstat;
                   9024:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9025:     int maxrs;
                   9026:     int *spsugar;
                   9027:     pointer *w;
                   9028:
                   9029:     spcol = col-nred;
                   9030:     get_eg(&eg0);
                   9031:     /* elimination (1st step) */
                   9032:     spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
                   9033:     svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   9034:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9035:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9036:         nd_sp(m,trace,sp,&spol);
                   9037:         if ( !spol ) continue;
                   9038:         nd_to_vect_lf(s0vect,col,spol,svect);
                   9039:         maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
                   9040:         for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
                   9041:         if ( i < col ) {
                   9042:             spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
                   9043:             for ( j = k = 0; j < col; j++ )
                   9044:                 if ( !rhead[j] ) v[k++][0] = svect[j][0];
                   9045:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9046:             sprow++;
                   9047:         }
                   9048: /*        nd_free(spol); */
                   9049:     }
                   9050:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9051:     if ( DP_Print ) {
1.5       noro     9052:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9053:         fflush(asir_out);
                   9054:     }
                   9055:     /* free index arrays */
                   9056: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   9057:
                   9058:     /* elimination (2nd step) */
                   9059:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9060:     rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
                   9061:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   9062:     for ( i = 0; i < rank; i++ ) {
                   9063: #if 0
                   9064:         w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   9065:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   9066: #else
                   9067:         w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   9068:         SG((NDV)w[i]) = spsugar[i];
                   9069: #endif
                   9070: /*        GCFREE(spmat[i]); */
                   9071:
                   9072:     }
                   9073: #if 0
                   9074:     qsort(w,rank,sizeof(NDV),
                   9075:         (int (*)(const void *,const void *))ndv_compare);
                   9076: #endif
                   9077:     r0 = 0;
                   9078:     for ( i = 0; i < rank; i++ ) {
                   9079:         NEXTNODE(r0,r); BDY(r) = w[i];
                   9080:     }
                   9081:     if ( r0 ) NEXT(r) = 0;
                   9082:
                   9083: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   9084:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9085:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9086:     if ( DP_Print ) {
1.5       noro     9087:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9088:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9089:             nsp,nred,sprow,spcol,rank);
1.5       noro     9090:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9091:     }
                   9092:     return r0;
                   9093: }
                   9094:
                   9095: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   9096:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   9097: {
                   9098:     int spcol,sprow,a;
                   9099:     int i,j,k,l,rank;
                   9100:     NODE r0,r;
                   9101:     ND_pairs sp;
                   9102:     ND spol;
                   9103:     Z **spmat;
                   9104:     Z *svect,*v;
                   9105:     int *colstat;
                   9106:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9107:     int maxrs;
                   9108:     int *spsugar;
                   9109:     pointer *w;
                   9110:
                   9111:     spcol = col-nred;
                   9112:     get_eg(&eg0);
                   9113:     /* elimination (1st step) */
                   9114:     spmat = (Z **)MALLOC(nsp*sizeof(Q *));
                   9115:     svect = (Z *)MALLOC(col*sizeof(Q));
                   9116:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9117:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9118:         nd_sp(0,trace,sp,&spol);
                   9119:         if ( !spol ) continue;
                   9120:         nd_to_vect_q(s0vect,col,spol,svect);
                   9121:         maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
                   9122:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9123:         if ( i < col ) {
                   9124:             spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
                   9125:             for ( j = k = 0; j < col; j++ )
                   9126:                 if ( !rhead[j] ) v[k++] = svect[j];
                   9127:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9128:             sprow++;
                   9129:         }
                   9130: /*        nd_free(spol); */
                   9131:     }
                   9132:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9133:     if ( DP_Print ) {
1.5       noro     9134:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9135:         fflush(asir_out);
                   9136:     }
                   9137:     /* free index arrays */
                   9138: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   9139:
                   9140:     /* elimination (2nd step) */
                   9141:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9142:     rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
                   9143:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   9144:     for ( i = 0; i < rank; i++ ) {
                   9145: #if 0
                   9146:         w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   9147:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   9148: #else
                   9149:         w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   9150:         SG((NDV)w[i]) = spsugar[i];
                   9151: #endif
                   9152: /*        GCFREE(spmat[i]); */
                   9153:     }
                   9154: #if 0
                   9155:     qsort(w,rank,sizeof(NDV),
                   9156:         (int (*)(const void *,const void *))ndv_compare);
                   9157: #endif
                   9158:     r0 = 0;
                   9159:     for ( i = 0; i < rank; i++ ) {
                   9160:         NEXTNODE(r0,r); BDY(r) = w[i];
                   9161:     }
                   9162:     if ( r0 ) NEXT(r) = 0;
                   9163:
                   9164: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   9165:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9166:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9167:     if ( DP_Print ) {
1.5       noro     9168:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9169:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9170:             nsp,nred,sprow,spcol,rank);
1.5       noro     9171:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9172:     }
                   9173:     return r0;
                   9174: }
                   9175:
                   9176: FILE *nd_write,*nd_read;
                   9177:
                   9178: void nd_send_int(int a) {
                   9179:     write_int(nd_write,(unsigned int *)&a);
                   9180: }
                   9181:
                   9182: void nd_send_intarray(int *p,int len) {
                   9183:     write_intarray(nd_write,(unsigned int *)p,len);
                   9184: }
                   9185:
                   9186: int nd_recv_int() {
                   9187:     int a;
                   9188:
                   9189:     read_int(nd_read,(unsigned int *)&a);
                   9190:     return a;
                   9191: }
                   9192:
                   9193: void nd_recv_intarray(int *p,int len) {
                   9194:     read_intarray(nd_read,(unsigned int *)p,len);
                   9195: }
                   9196:
                   9197: void nd_send_ndv(NDV p) {
                   9198:     int len,i;
                   9199:     NMV m;
                   9200:
                   9201:     if ( !p ) nd_send_int(0);
                   9202:     else {
                   9203:         len = LEN(p);
                   9204:         nd_send_int(len);
                   9205:         m = BDY(p);
                   9206:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9207:             nd_send_int(CM(m));
                   9208:             nd_send_intarray((int *)DL(m),nd_wpd);
                   9209:         }
                   9210:     }
                   9211: }
                   9212:
                   9213: void nd_send_nd(ND p) {
                   9214:     int len,i;
                   9215:     NM m;
                   9216:
                   9217:     if ( !p ) nd_send_int(0);
                   9218:     else {
                   9219:         len = LEN(p);
                   9220:         nd_send_int(len);
                   9221:         m = BDY(p);
                   9222:         for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   9223:             nd_send_int(CM(m));
                   9224:             nd_send_intarray((int *)DL(m),nd_wpd);
                   9225:         }
                   9226:     }
                   9227: }
                   9228:
                   9229: NDV nd_recv_ndv()
                   9230: {
                   9231:     int len,i;
                   9232:     NMV m,m0;
                   9233:     NDV r;
                   9234:
                   9235:     len = nd_recv_int();
                   9236:     if ( !len ) return 0;
                   9237:     else {
                   9238:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   9239: #if 0
                   9240:         ndv_alloc += len*nmv_adv;
                   9241: #endif
                   9242:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9243:             CM(m) = nd_recv_int();
                   9244:             nd_recv_intarray((int *)DL(m),nd_wpd);
                   9245:         }
                   9246:         MKNDV(nd_nvar,m0,len,r);
                   9247:         return r;
                   9248:     }
                   9249: }
                   9250:
                   9251: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
                   9252: {
                   9253:     int i,j,t,c,rank,inv;
                   9254:     int *ci,*ri;
                   9255:     Z dn;
                   9256:     MAT m,nm;
                   9257:
                   9258:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
                   9259:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   9260:     for ( i = 0; i < row; i++ )
                   9261:         for ( j = 0; j < col; j++ )
                   9262:             mat0[i][j] = 0;
                   9263:     c = col-rank;
                   9264:     for ( i = 0; i < rank; i++ ) {
                   9265:         mat0[i][ri[i]] = dn;
                   9266:         for ( j = 0; j < c; j++ )
                   9267:             mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
                   9268:     }
                   9269:     return rank;
                   9270: }
                   9271:
                   9272: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   9273: {
                   9274:     int i,j,k,l,inv,a,rank,s;
                   9275:     unsigned int *t,*pivot,*pk;
                   9276:     unsigned int **mat;
                   9277:     ND_pairs pair;
                   9278:
                   9279:     mat = (unsigned int **)mat0;
                   9280:     for ( rank = 0, j = 0; j < col; j++ ) {
                   9281:         for ( i = rank; i < row; i++ )
                   9282:             mat[i][j] %= md;
                   9283:         for ( i = rank; i < row; i++ )
                   9284:             if ( mat[i][j] )
                   9285:                 break;
                   9286:         if ( i == row ) {
                   9287:             colstat[j] = 0;
                   9288:             continue;
                   9289:         } else
                   9290:             colstat[j] = 1;
                   9291:         if ( i != rank ) {
                   9292:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   9293:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   9294:             if ( spactive ) {
                   9295:                 pair = spactive[i]; spactive[i] = spactive[rank];
                   9296:                 spactive[rank] = pair;
                   9297:             }
                   9298:         }
                   9299:         pivot = mat[rank];
                   9300:         s = sugar[rank];
                   9301:         inv = invm(pivot[j],md);
                   9302:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   9303:             if ( *pk ) {
                   9304:                 if ( *pk >= (unsigned int)md )
                   9305:                     *pk %= md;
                   9306:                 DMAR(*pk,inv,0,md,*pk)
                   9307:             }
                   9308:         for ( i = rank+1; i < row; i++ ) {
                   9309:             t = mat[i];
                   9310:             if ( (a = t[j]) != 0 ) {
                   9311:                 sugar[i] = MAX(sugar[i],s);
                   9312:                 red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   9313:             }
                   9314:         }
                   9315:         rank++;
                   9316:     }
                   9317:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   9318:         if ( colstat[j] ) {
                   9319:             pivot = mat[l];
                   9320:             s = sugar[l];
                   9321:             for ( i = 0; i < l; i++ ) {
                   9322:                 t = mat[i];
                   9323:                 t[j] %= md;
                   9324:                 if ( (a = t[j]) != 0 ) {
                   9325:                     sugar[i] = MAX(sugar[i],s);
                   9326:                     red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   9327:                 }
                   9328:             }
                   9329:             l--;
                   9330:         }
                   9331:     for ( j = 0, l = 0; l < rank; j++ )
                   9332:         if ( colstat[j] ) {
                   9333:             t = mat[l];
                   9334:             for ( k = j; k < col; k++ )
                   9335:                 if ( t[k] >= (unsigned int)md )
                   9336:                     t[k] %= md;
                   9337:             l++;
                   9338:         }
                   9339:     return rank;
                   9340: }
                   9341:
1.40      noro     9342: int nd_gauss_elim_mod_s(UINT **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat,SIG *sig)
                   9343: {
                   9344:   int i,j,k,l,rank,s,imin;
                   9345:   UINT inv;
                   9346:   UINT a;
                   9347:   UINT *t,*pivot,*pk;
                   9348:   UINT *ck;
                   9349:   UINT *ct;
                   9350:   ND_pairs pair;
                   9351:   SIG sg;
                   9352:   int *used;
                   9353:
                   9354:   used = (int *)MALLOC(row*sizeof(int));
                   9355:   for ( j = 0; j < col; j++ ) {
                   9356:     for ( i = 0; i < row; i++ )
                   9357:       a = mat[i][j] %= md;
                   9358:     for ( i = 0; i < row; i++ )
                   9359:       if ( !used[i] && mat[i][j] ) break;
                   9360:     if ( i == row ) {
                   9361:       colstat[j] = 0;
                   9362:       continue;
                   9363:     } else {
                   9364:       colstat[j] = 1;
                   9365:       used[i] = 1;
                   9366:     }
                   9367:     /* column j is normalized */
                   9368:     s = sugar[i];
                   9369:     inv = invm(mat[i][j],md);
                   9370:     /* normalize pivot row */
                   9371:     for ( k = j, pk = mat[i]+j; k < col; k++, pk++, ck++ ) {
                   9372:       DMAR(*pk,inv,0,md,*pk);
                   9373:     }
                   9374:     for ( k = i+1; k < row; k++ ) {
                   9375:       if ( (a = mat[k][j]) != 0 ) {
                   9376:         sugar[k] = MAX(sugar[k],s);
                   9377:         red_by_vect(md,mat[k]+j,mat[i]+j,(int)(md-a),col-j);
                   9378:         Nf4_red++;
                   9379:       }
                   9380:     }
                   9381:   }
                   9382:   rank = 0;
                   9383:   for ( i = 0; i < row; i++ ) {
                   9384:     for ( j = 0; j < col; j++ )
                   9385:       if ( mat[i][j] ) break;
                   9386:     if ( j == col ) sugar[i] = -1;
                   9387:     else rank++;
                   9388:   }
                   9389:   return rank;
                   9390: }
                   9391:
1.1       noro     9392:
1.7       noro     9393: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1       noro     9394: {
1.7       noro     9395:     int i,j,k,l,inv,a,rank,s;
                   9396:     unsigned int *t,*pivot,*pk;
                   9397:     unsigned int **mat;
                   9398:
                   9399:     mat = (unsigned int **)mat0;
                   9400:     for ( rank = 0, j = 0; j < col; j++ ) {
                   9401:         for ( i = rank; i < row; i++ )
                   9402:             if ( mat[i][j] )
                   9403:                 break;
                   9404:         if ( i == row ) {
                   9405:             colstat[j] = 0;
                   9406:             continue;
                   9407:         } else
                   9408:             colstat[j] = 1;
                   9409:         if ( i != rank ) {
                   9410:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   9411:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   9412:         }
                   9413:         pivot = mat[rank];
                   9414:         s = sugar[rank];
                   9415:         inv = _invsf(pivot[j]);
                   9416:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   9417:             if ( *pk )
                   9418:                 *pk = _mulsf(*pk,inv);
                   9419:         for ( i = rank+1; i < row; i++ ) {
                   9420:             t = mat[i];
                   9421:             if ( (a = t[j]) != 0 ) {
                   9422:                 sugar[i] = MAX(sugar[i],s);
                   9423:                 red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   9424:             }
                   9425:         }
                   9426:         rank++;
                   9427:     }
                   9428:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   9429:         if ( colstat[j] ) {
                   9430:             pivot = mat[l];
                   9431:             s = sugar[l];
                   9432:             for ( i = 0; i < l; i++ ) {
                   9433:                 t = mat[i];
                   9434:                 if ( (a = t[j]) != 0 ) {
                   9435:                     sugar[i] = MAX(sugar[i],s);
                   9436:                     red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   9437:                 }
                   9438:             }
                   9439:             l--;
                   9440:         }
                   9441:     return rank;
                   9442: }
1.1       noro     9443:
1.7       noro     9444: int ndv_ishomo(NDV p)
                   9445: {
                   9446:     NMV m;
                   9447:     int len,h;
1.1       noro     9448:
                   9449:     if ( !p ) return 1;
                   9450:     len = LEN(p);
                   9451:     m = BDY(p);
                   9452:     h = TD(DL(m));
                   9453:     NMV_ADV(m);
                   9454:     for ( len--; len; len--, NMV_ADV(m) )
1.20      noro     9455:         if ( TD(DL(m)) != h ) {
                   9456:           return 0;
                   9457:         }
1.1       noro     9458:     return 1;
                   9459: }
                   9460:
                   9461: void ndv_save(NDV p,int index)
                   9462: {
                   9463:     FILE *s;
                   9464:     char name[BUFSIZ];
                   9465:     short id;
                   9466:     int nv,sugar,len,n,i,td,e,j;
                   9467:     NMV m;
                   9468:     unsigned int *dl;
                   9469:     int mpos;
                   9470:
                   9471:     sprintf(name,"%s/%d",Demand,index);
                   9472:     s = fopen(name,"w");
                   9473:     savevl(s,0);
                   9474:     if ( !p ) {
                   9475:         saveobj(s,0);
                   9476:         return;
                   9477:     }
                   9478:     id = O_DP;
                   9479:     nv = NV(p);
                   9480:     sugar = SG(p);
                   9481:     len = LEN(p);
                   9482:     write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
                   9483:     write_int(s,(unsigned int *)&len);
                   9484:
                   9485:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     9486:         saveobj(s,(Obj)CZ(m));
1.1       noro     9487:         dl = DL(m);
                   9488:         td = TD(dl);
                   9489:         write_int(s,(unsigned int *)&td);
                   9490:         for ( j = 0; j < nv; j++ ) {
                   9491:             e = GET_EXP(dl,j);
                   9492:             write_int(s,(unsigned int *)&e);
                   9493:         }
                   9494:         if ( nd_module ) {
                   9495:             mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
                   9496:         }
                   9497:     }
                   9498:     fclose(s);
                   9499: }
                   9500:
                   9501: void nd_save_mod(ND p,int index)
                   9502: {
                   9503:     FILE *s;
                   9504:     char name[BUFSIZ];
                   9505:     int nv,sugar,len,c;
                   9506:     NM m;
                   9507:
                   9508:     sprintf(name,"%s/%d",Demand,index);
                   9509:     s = fopen(name,"w");
                   9510:     if ( !p ) {
                   9511:     len = 0;
                   9512:       write_int(s,(unsigned int *)&len);
                   9513:     fclose(s);
                   9514:         return;
                   9515:     }
                   9516:     nv = NV(p);
                   9517:     sugar = SG(p);
                   9518:     len = LEN(p);
                   9519:     write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
                   9520:   for ( m = BDY(p); m; m = NEXT(m) ) {
                   9521:     c = CM(m); write_int(s,(unsigned int *)&c);
                   9522:     write_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   9523:   }
                   9524:     fclose(s);
                   9525: }
                   9526:
                   9527: NDV ndv_load(int index)
                   9528: {
                   9529:     FILE *s;
                   9530:     char name[BUFSIZ];
                   9531:     short id;
                   9532:     int nv,sugar,len,n,i,td,e,j;
                   9533:     NDV d;
                   9534:     NMV m0,m;
                   9535:     unsigned int *dl;
                   9536:     Obj obj;
                   9537:     int mpos;
                   9538:
                   9539:     sprintf(name,"%s/%d",Demand,index);
                   9540:     s = fopen(name,"r");
                   9541:     if ( !s ) return 0;
                   9542:
                   9543:     skipvl(s);
                   9544:     read_short(s,(unsigned short *)&id);
                   9545:     if ( !id ) return 0;
                   9546:     read_int(s,(unsigned int *)&nv);
                   9547:     read_int(s,(unsigned int *)&sugar);
                   9548:     read_int(s,(unsigned int *)&len);
                   9549:
                   9550:     m0 = m = MALLOC(len*nmv_adv);
                   9551:     for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     9552:         loadobj(s,&obj); CZ(m) = (Z)obj;
1.1       noro     9553:         dl = DL(m);
                   9554:         ndl_zero(dl);
                   9555:         read_int(s,(unsigned int *)&td); TD(dl) = td;
                   9556:         for ( j = 0; j < nv; j++ ) {
                   9557:             read_int(s,(unsigned int *)&e);
                   9558:             PUT_EXP(dl,j,e);
                   9559:         }
                   9560:         if ( nd_module ) {
                   9561:             read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
                   9562:         }
                   9563:         if ( nd_blockmask ) ndl_weight_mask(dl);
                   9564:     }
                   9565:     fclose(s);
                   9566:     MKNDV(nv,m0,len,d);
                   9567:     SG(d) = sugar;
                   9568:     return d;
                   9569: }
                   9570:
                   9571: ND nd_load_mod(int index)
                   9572: {
                   9573:     FILE *s;
                   9574:     char name[BUFSIZ];
                   9575:     int nv,sugar,len,i,c;
                   9576:   ND d;
                   9577:     NM m0,m;
                   9578:
                   9579:     sprintf(name,"%s/%d",Demand,index);
                   9580:     s = fopen(name,"r");
                   9581:   /* if the file does not exist, it means p[index]=0 */
                   9582:     if ( !s ) return 0;
                   9583:
                   9584:     read_int(s,(unsigned int *)&nv);
                   9585:   if ( !nv ) { fclose(s); return 0; }
                   9586:
                   9587:     read_int(s,(unsigned int *)&sugar);
                   9588:     read_int(s,(unsigned int *)&len);
                   9589:   for ( m0 = 0, i = 0; i < len; i++ ) {
                   9590:     NEXTNM(m0,m);
                   9591:     read_int(s,(unsigned int *)&c); CM(m) = c;
                   9592:     read_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   9593:   }
                   9594:   NEXT(m) = 0;
                   9595:     MKND(nv,m0,len,d);
                   9596:     SG(d) = sugar;
                   9597:   fclose(s);
                   9598:     return d;
                   9599: }
                   9600:
                   9601: void nd_det(int mod,MAT f,P *rp)
                   9602: {
                   9603:     VL fv,tv;
                   9604:     int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
                   9605:     pointer **m;
                   9606:     P **w;
                   9607:     P mp,r;
                   9608:     NDV **dm;
                   9609:     NDV *t,*mi,*mj;
                   9610:     NDV d,s,mij,mjj;
                   9611:     ND u;
                   9612:     NMV nmv;
                   9613:     UINT *bound;
                   9614:     PGeoBucket bucket;
                   9615:     struct order_spec *ord;
                   9616:     Z dq,dt,ds;
                   9617:     Z mone;
                   9618:     Z gn,qn,dn0,nm,dn;
                   9619:
                   9620:     create_order_spec(0,0,&ord);
                   9621:     nd_init_ord(ord);
                   9622:     get_vars((Obj)f,&fv);
                   9623:     if ( f->row != f->col )
                   9624:         error("nd_det : non-square matrix");
                   9625:     n = f->row;
                   9626:     m = f->body;
                   9627:     for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   9628:
                   9629:     if ( !nvar ) {
                   9630:         if ( !mod )
                   9631:             detp(CO,(P **)m,n,rp);
                   9632:         else {
                   9633:             w = (P **)almat_pointer(n,n);
                   9634:             for ( i = 0; i < n; i++ )
                   9635:                 for ( j = 0; j < n; j++ )
                   9636:                     ptomp(mod,(P)m[i][j],&w[i][j]);
                   9637:             detmp(CO,mod,w,n,&mp);
                   9638:             mptop(mp,rp);
                   9639:         }
                   9640:         return;
                   9641:     }
                   9642:
                   9643:     if ( !mod ) {
                   9644:         w = (P **)almat_pointer(n,n);
                   9645:         dq = ONE;
                   9646:         for ( i = 0; i < n; i++ ) {
                   9647:             dn0 = ONE;
                   9648:             for ( j = 0; j < n; j++ ) {
                   9649:                 if ( !m[i][j] ) continue;
                   9650:                 lgp(m[i][j],&nm,&dn);
1.6       noro     9651:                 gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1       noro     9652:             }
                   9653:             if ( !UNIZ(dn0) ) {
                   9654:                 ds = dn0;
                   9655:                 for ( j = 0; j < n; j++ )
                   9656:                     mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   9657:                 mulz(dq,ds,&dt); dq = dt;
                   9658:             } else
                   9659:                 for ( j = 0; j < n; j++ )
                   9660:                     w[i][j] = (P)m[i][j];
                   9661:         }
                   9662:         m = (pointer **)w;
                   9663:     }
                   9664:
                   9665:     for ( i = 0, max = 1; i < n; i++ )
                   9666:         for ( j = 0; j < n; j++ )
                   9667:             for ( tv = fv; tv; tv = NEXT(tv) ) {
                   9668:                 e = getdeg(tv->v,(P)m[i][j]);
                   9669:                 max = MAX(e,max);
                   9670:             }
                   9671:     nd_setup_parameters(nvar,max);
                   9672:     dm = (NDV **)almat_pointer(n,n);
                   9673:     for ( i = 0, max = 1; i < n; i++ )
                   9674:         for ( j = 0; j < n; j++ ) {
                   9675:             dm[i][j] = ptondv(CO,fv,m[i][j]);
                   9676:             if ( mod ) ndv_mod(mod,dm[i][j]);
                   9677:             if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   9678:         }
                   9679:     d = ptondv(CO,fv,(P)ONE);
                   9680:     if ( mod ) ndv_mod(mod,d);
                   9681:     chsgnz(ONE,&mone);
                   9682:     for ( j = 0, sgn = 1; j < n; j++ ) {
                   9683:       if ( DP_Print ) {
                   9684:         fprintf(asir_out,".");
                   9685:       }
                   9686:         for ( i = j; i < n && !dm[i][j]; i++ );
                   9687:         if ( i == n ) {
                   9688:             *rp = 0;
                   9689:             return;
                   9690:         }
                   9691:         k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
                   9692:         for ( k = j; k < n; k++ )
                   9693:             for ( l = j; l < n; l++ )
                   9694:                 if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   9695:                     k0 = k; l0 = l; len0 = LEN(dm[k][l]);
                   9696:                 }
                   9697:         if ( k0 != j ) {
                   9698:             t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   9699:             sgn = -sgn;
                   9700:         }
                   9701:         if ( l0 != j ) {
                   9702:             for ( k = j; k < n; k++ ) {
                   9703:                 s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   9704:             }
                   9705:             sgn = -sgn;
                   9706:         }
                   9707:         bound = nd_det_compute_bound(dm,n,j);
                   9708:         for ( k = 0; k < nd_nvar; k++ )
                   9709:             if ( bound[k]*2 > nd_mask0 ) break;
                   9710:         if ( k < nd_nvar )
                   9711:             nd_det_reconstruct(dm,n,j,d);
                   9712:
                   9713:         for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
                   9714: /*            if ( DP_Print ) fprintf(asir_out,"    i=%d\n        ",i); */
                   9715:             mi = dm[i]; mij = mi[j];
                   9716:             if ( mod )
                   9717:                 ndv_mul_c(mod,mij,mod-1);
                   9718:             else
                   9719:                 ndv_mul_c_q(mij,mone);
                   9720:             for ( k = j+1; k < n; k++ ) {
                   9721: /*                if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
                   9722:                 bucket = create_pbucket();
                   9723:                 if ( mi[k] ) {
                   9724:                     nmv = BDY(mjj); len = LEN(mjj);
                   9725:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   9726:                         u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   9727:                         add_pbucket(mod,bucket,u);
                   9728:                     }
                   9729:                 }
                   9730:                 if ( mj[k] && mij ) {
                   9731:                     nmv = BDY(mij); len = LEN(mij);
                   9732:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   9733:                         u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   9734:                         add_pbucket(mod,bucket,u);
                   9735:                     }
                   9736:                 }
                   9737:                 u = nd_quo(mod,bucket,d);
                   9738:                 mi[k] = ndtondv(mod,u);
                   9739:             }
                   9740: /*            if ( DP_Print ) fprintf(asir_out,"\n",k); */
                   9741:         }
                   9742:         d = mjj;
                   9743:     }
                   9744:     if ( DP_Print ) {
                   9745:       fprintf(asir_out,"\n");
                   9746:     }
                   9747:     if ( sgn < 0 ) {
                   9748:         if ( mod )
                   9749:             ndv_mul_c(mod,d,mod-1);
                   9750:         else
                   9751:             ndv_mul_c_q(d,mone);
                   9752:     }
                   9753:     r = ndvtop(mod,CO,fv,d);
                   9754:     if ( !mod && !UNIQ(dq) )
                   9755:         divsp(CO,r,(P)dq,rp);
                   9756:     else
                   9757:         *rp = r;
                   9758: }
                   9759:
                   9760: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
                   9761: {
                   9762:     NM mr,mr0;
                   9763:     NM tnm;
                   9764:     NMV m;
                   9765:     UINT *d0,*dt,*dm;
                   9766:     int c,n,td,i,c1,c2,len;
                   9767:     Z q;
                   9768:     ND r;
                   9769:
                   9770:     if ( !p ) return 0;
                   9771:     else {
                   9772:         n = NV(p); m = BDY(p); len = LEN(p);
                   9773:         d0 = DL(m0);
                   9774:         td = TD(d);
                   9775:         mr0 = 0;
                   9776:         NEWNM(tnm);
                   9777:         if ( mod ) {
                   9778:             c = CM(m0);
                   9779:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9780:                 ndl_add(DL(m),d0,DL(tnm));
                   9781:                 if ( ndl_reducible(DL(tnm),d) ) {
                   9782:                     NEXTNM(mr0,mr);
                   9783:                     c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
                   9784:                     ndl_copy(DL(tnm),DL(mr));
                   9785:                 }
                   9786:             }
                   9787:         } else {
1.6       noro     9788:             q = CZ(m0);
1.1       noro     9789:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9790:                 ndl_add(DL(m),d0,DL(tnm));
                   9791:                 if ( ndl_reducible(DL(tnm),d) ) {
                   9792:                     NEXTNM(mr0,mr);
1.6       noro     9793:                     mulz(CZ(m),q,&CZ(mr));
1.1       noro     9794:                     ndl_copy(DL(tnm),DL(mr));
                   9795:                 }
                   9796:             }
                   9797:         }
                   9798:         if ( !mr0 )
                   9799:             return 0;
                   9800:         else {
                   9801:             NEXT(mr) = 0;
                   9802:             for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   9803:             MKND(NV(p),mr0,len,r);
                   9804:             SG(r) = SG(p) + TD(d0);
                   9805:             return r;
                   9806:         }
                   9807:     }
                   9808: }
                   9809:
                   9810: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   9811: {
                   9812:     int i,obpe,oadv,h,k,l;
                   9813:     static NM prev_nm_free_list;
                   9814:     EPOS oepos;
                   9815:
                   9816:     obpe = nd_bpe;
                   9817:     oadv = nmv_adv;
                   9818:     oepos = nd_epos;
                   9819:     if ( obpe < 2 ) nd_bpe = 2;
                   9820:     else if ( obpe < 3 ) nd_bpe = 3;
                   9821:     else if ( obpe < 4 ) nd_bpe = 4;
                   9822:     else if ( obpe < 5 ) nd_bpe = 5;
                   9823:     else if ( obpe < 6 ) nd_bpe = 6;
                   9824:     else if ( obpe < 8 ) nd_bpe = 8;
                   9825:     else if ( obpe < 10 ) nd_bpe = 10;
                   9826:     else if ( obpe < 16 ) nd_bpe = 16;
                   9827:     else if ( obpe < 32 ) nd_bpe = 32;
                   9828:     else error("nd_det_reconstruct : exponent too large");
                   9829:
                   9830:     nd_setup_parameters(nd_nvar,0);
                   9831:     prev_nm_free_list = _nm_free_list;
                   9832:     _nm_free_list = 0;
                   9833:     for ( k = j; k < n; k++ )
                   9834:         for (l = j; l < n; l++ )
                   9835:             ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   9836:     ndv_realloc(d,obpe,oadv,oepos);
                   9837:     prev_nm_free_list = 0;
                   9838: #if 0
                   9839:     GC_gcollect();
                   9840: #endif
                   9841: }
                   9842:
                   9843: /* returns a UINT array containing degree bounds */
                   9844:
                   9845: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   9846: {
                   9847:     UINT *d0,*d1,*d,*t,*r;
                   9848:     int k,l,i;
                   9849:
                   9850:     d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
                   9851:     for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
                   9852:     for ( k = j; k < n; k++ )
                   9853:         for ( l = j; l < n; l++ )
                   9854:             if ( dm[k][l] ) {
                   9855:                 d = ndv_compute_bound(dm[k][l]);
                   9856:                 for ( i = 0; i < nd_nvar; i++ )
                   9857:                     d0[i] = MAX(d0[i],d[i]);
                   9858:             }
                   9859:     return d0;
                   9860: }
                   9861:
                   9862: DL nd_separate_d(UINT *d,UINT *trans)
                   9863: {
                   9864:     int n,td,i,e,j;
                   9865:     DL a;
                   9866:
                   9867:     ndl_zero(trans);
                   9868:     td = 0;
                   9869:     for ( i = 0; i < nd_ntrans; i++ ) {
                   9870:         e = GET_EXP(d,i);
                   9871:         PUT_EXP(trans,i,e);
                   9872:         td += MUL_WEIGHT(e,i);
                   9873:     }
                   9874:     if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   9875:         /* homogenized */
                   9876:         i = nd_nvar-1;
                   9877:         e = GET_EXP(d,i);
                   9878:         PUT_EXP(trans,i,e);
                   9879:         td += MUL_WEIGHT(e,i);
                   9880:     }
                   9881:     TD(trans) = td;
                   9882:     if ( nd_blockmask) ndl_weight_mask(trans);
                   9883:     NEWDL(a,nd_nalg);
                   9884:     td = 0;
                   9885:     for ( i = 0; i < nd_nalg; i++ ) {
                   9886:         j = nd_ntrans+i;
                   9887:         e = GET_EXP(d,j);
                   9888:         a->d[i] = e;
                   9889:         td += e;
                   9890:     }
                   9891:     a->td = td;
                   9892:     return a;
                   9893: }
                   9894:
                   9895: int nd_monic(int mod,ND *p)
                   9896: {
                   9897:     UINT *trans,*t;
                   9898:     DL alg;
                   9899:     MP mp0,mp;
                   9900:     NM m,m0,m1,ma0,ma,mb,mr0,mr;
                   9901:     ND r;
                   9902:     DL dl;
                   9903:     DP nm;
                   9904:     NDV ndv;
                   9905:     DAlg inv,cd;
                   9906:     ND s,c;
                   9907:     Z l,mul;
                   9908:     Z ln;
                   9909:     int n,ntrans,i,e,td,is_lc,len;
                   9910:     NumberField nf;
                   9911:     struct oEGT eg0,eg1;
                   9912:
                   9913:     if ( !(nf = get_numberfield()) )
                   9914:         error("nd_monic : current_numberfield is not set");
                   9915:
                   9916:     /* Q coef -> DAlg coef */
                   9917:     NEWNM(ma0); ma = ma0;
                   9918:     m = BDY(*p);
                   9919:     is_lc = 1;
                   9920:     while ( 1 ) {
                   9921:         NEWMP(mp0); mp = mp0;
1.6       noro     9922:         mp->c = (Obj)CZ(m);
1.1       noro     9923:         mp->dl = nd_separate_d(DL(m),DL(ma));
                   9924:         NEWNM(mb);
                   9925:         for ( m = NEXT(m); m; m = NEXT(m) ) {
                   9926:             alg = nd_separate_d(DL(m),DL(mb));
                   9927:             if ( !ndl_equal(DL(ma),DL(mb)) )
                   9928:                 break;
1.6       noro     9929:             NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1       noro     9930:         }
                   9931:         NEXT(mp) = 0;
                   9932:         MKDP(nd_nalg,mp0,nm);
                   9933:         MKDAlg(nm,ONE,cd);
                   9934:         if ( is_lc == 1 ) {
                   9935:             /* if the lc is a rational number, we have nothing to do */
                   9936:             if ( !mp0->dl->td )
                   9937:                 return 1;
                   9938:
                   9939:             get_eg(&eg0);
                   9940:             invdalg(cd,&inv);
                   9941:             get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   9942:             /* check the validity of inv */
                   9943:             if ( mod && !remqi((Q)inv->dn,mod) )
                   9944:                 return 0;
                   9945:             CA(ma) = nf->one;
                   9946:             is_lc = 0;
                   9947:             ln = ONE;
                   9948:         } else {
                   9949:             muldalg(cd,inv,&CA(ma));
                   9950:             lcmz(ln,CA(ma)->dn,&ln);
                   9951:         }
                   9952:         if ( m ) {
                   9953:             NEXT(ma) = mb; ma = mb;
                   9954:         } else {
                   9955:             NEXT(ma) = 0;
                   9956:             break;
                   9957:         }
                   9958:     }
                   9959:     /* l = lcm(denoms) */
                   9960:     l = ln;
                   9961:     for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6       noro     9962:         divsz(l,CA(m)->dn,&mul);
1.1       noro     9963:         for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   9964:             NEXTNM(mr0,mr);
1.6       noro     9965:             mulz((Z)mp->c,mul,&CZ(mr));
1.1       noro     9966:             dl = mp->dl;
                   9967:             td = TD(DL(m));
                   9968:             ndl_copy(DL(m),DL(mr));
                   9969:             for ( i = 0; i < nd_nalg; i++ ) {
                   9970:                 e = dl->d[i];
                   9971:                 PUT_EXP(DL(mr),i+nd_ntrans,e);
                   9972:                 td += MUL_WEIGHT(e,i+nd_ntrans);
                   9973:             }
                   9974:             if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
                   9975:             TD(DL(mr)) = td;
                   9976:             if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   9977:         }
                   9978:     }
                   9979:     NEXT(mr) = 0;
                   9980:     for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   9981:     MKND(NV(*p),mr0,len,r);
                   9982:     /* XXX */
                   9983:     SG(r) = SG(*p);
                   9984:     nd_free(*p);
                   9985:     *p = r;
                   9986:     return 1;
                   9987: }
                   9988:
                   9989: NODE reverse_node(NODE n)
                   9990: {
                   9991:     NODE t,t1;
                   9992:
                   9993:     for ( t = 0; n; n = NEXT(n) ) {
                   9994:         MKNODE(t1,BDY(n),t); t = t1;
                   9995:     }
                   9996:     return t;
                   9997: }
                   9998:
                   9999: P ndc_div(int mod,union oNDC a,union oNDC b)
                   10000: {
                   10001:     union oNDC c;
                   10002:     int inv,t;
                   10003:
                   10004:     if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10      noro     10005:     else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1       noro     10006:     else if ( mod ) {
                   10007:         inv = invm(b.m,mod);
                   10008:         DMAR(a.m,inv,0,mod,t); c.m = t;
                   10009:     } else if ( nd_vc )
                   10010:        divsp(nd_vc,a.p,b.p,&c.p);
                   10011:     else
                   10012:        divsz(a.z,b.z,&c.z);
                   10013:     return ndctop(mod,c);
                   10014: }
                   10015:
                   10016: P ndctop(int mod,union oNDC c)
                   10017: {
                   10018:     Z q;
                   10019:     int e;
                   10020:     GFS gfs;
                   10021:
                   10022:     if ( mod == -1 ) {
                   10023:         e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
                   10024:     } else if ( mod == -2 ) {
1.10      noro     10025:        q = c.z; return (P)q;
1.1       noro     10026:     } else if ( mod > 0 ) {
1.6       noro     10027:         STOZ(c.m,q); return (P)q;
1.1       noro     10028:     } else
                   10029:         return (P)c.p;
                   10030: }
                   10031:
                   10032: /* [0,0,0,cont] = p -> p/cont */
                   10033:
                   10034: void finalize_tracelist(int i,P cont)
                   10035: {
                   10036:  LIST l;
                   10037:  NODE node;
                   10038:  Z iq;
                   10039:
                   10040:   if ( !UNIQ(cont) ) {
                   10041:     node = mknode(4,NULLP,NULLP,NULLP,cont);
                   10042:     MKLIST(l,node); MKNODE(node,l,nd_tracelist);
                   10043:     nd_tracelist = node;
                   10044:   }
1.6       noro     10045:   STOZ(i,iq);
1.1       noro     10046:   nd_tracelist = reverse_node(nd_tracelist);
                   10047:   MKLIST(l,nd_tracelist);
                   10048:   node = mknode(2,iq,l); MKLIST(l,node);
                   10049:   MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
                   10050:   nd_alltracelist = node; nd_tracelist = 0;
                   10051: }
                   10052:
                   10053: void conv_ilist(int demand,int trace,NODE g,int **indp)
                   10054: {
                   10055:     int n,i,j;
                   10056:   int *ind;
                   10057:   NODE t;
                   10058:
                   10059:     n = length(g);
                   10060:   ind = (int *)MALLOC(n*sizeof(int));
                   10061:   for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
                   10062:     j = (long)BDY(t); ind[i] = j;
                   10063:     BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
                   10064:   }
                   10065:   if ( indp ) *indp = ind;
                   10066: }
                   10067:
1.30      noro     10068: NODE conv_ilist_s(int demand,int trace,int **indp)
                   10069: {
                   10070:   int n,i,j;
                   10071:   int *ind;
                   10072:   NODE g0,g;
                   10073:
                   10074:   n = nd_psn;
                   10075:   ind = (int *)MALLOC(n*sizeof(int));
                   10076:   g0 = 0;
                   10077:   for ( i = 0; i < n; i++ ) {
                   10078:     ind[i] = i;
                   10079:     NEXTNODE(g0,g);
                   10080:     BDY(g) = (pointer)(demand?ndv_load(i):(trace?nd_ps_trace[i]:nd_ps[i]));
                   10081:   }
                   10082:   if ( g0 ) NEXT(g) = 0;
                   10083:   if ( indp ) *indp = ind;
                   10084:   return g0;
                   10085: }
                   10086:
1.1       noro     10087: void parse_nd_option(NODE opt)
                   10088: {
1.38      noro     10089:   NODE t,p,u;
1.1       noro     10090:   int i,s,n;
1.38      noro     10091:   char *key;
                   10092:   Obj value;
1.1       noro     10093:
1.41      noro     10094:   nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_norb = 0; nd_gbblock = 0;
1.1       noro     10095:   nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
                   10096:   nd_splist = 0; nd_check_splist = 0;
1.38      noro     10097:   nd_sugarweight = 0; nd_f4red =0; nd_rank0 = 0;
                   10098:   nd_f4_td = 0; nd_sba_f4step = 2; nd_sba_pot = 0; nd_sba_largelcm = 0;
1.41      noro     10099:   nd_sba_dontsort = 0; nd_top = 0; nd_sba_redundant_check = 0;
1.45    ! noro     10100:   nd_sba_syz = 0; nd_sba_fixord = 0; nd_sba_grevlexgb = 0;
1.39      noro     10101:
1.38      noro     10102:   for ( t = opt; t; t = NEXT(t) ) {
                   10103:     p = BDY((LIST)BDY(t));
                   10104:     key = BDY((STRING)BDY(p));
                   10105:     value = (Obj)BDY(NEXT(p));
                   10106:     if ( !strcmp(key,"gentrace") )
                   10107:       nd_gentrace = value?1:0;
                   10108:     else if ( !strcmp(key,"gensyz") )
                   10109:       nd_gensyz = value?1:0;
                   10110:     else if ( !strcmp(key,"nora") )
                   10111:       nd_nora = value?1:0;
1.41      noro     10112:     else if ( !strcmp(key,"norb") )
                   10113:       nd_norb = value?1:0;
1.38      noro     10114:     else if ( !strcmp(key,"gbblock") ) {
                   10115:       if ( value && OID(value) == O_LIST ) {
1.1       noro     10116:         u = BDY((LIST)value);
1.38      noro     10117:         nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.1       noro     10118:         for ( i = 0; u; u = NEXT(u) ) {
                   10119:           p = BDY((LIST)BDY(u));
1.6       noro     10120:           s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
                   10121:           nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1       noro     10122:         }
                   10123:         nd_gbblock[i] = -1;
1.38      noro     10124:       } else
                   10125:         nd_gbblock = 0;
1.1       noro     10126:     } else if ( !strcmp(key,"newelim") )
                   10127:             nd_newelim = value?1:0;
                   10128:     else if ( !strcmp(key,"intersect") )
                   10129:             nd_intersect = value?1:0;
1.17      noro     10130:     else if ( !strcmp(key,"syzgen") )
                   10131:             nd_intersect = ZTOS((Q)value);
1.1       noro     10132:     else if ( !strcmp(key,"lf") )
                   10133:             nd_lf = value?1:0;
                   10134:     else if ( !strcmp(key,"trace") ) {
1.38      noro     10135:       if ( value ) {
                   10136:         u = BDY((LIST)value);
                   10137:         nd_nzlist = BDY((LIST)ARG2(u));
                   10138:         nd_bpe = ZTOS((Q)ARG3(u));
                   10139:       }
1.1       noro     10140:     } else if ( !strcmp(key,"f4red") ) {
1.38      noro     10141:       nd_f4red = ZTOS((Q)value);
1.1       noro     10142:     } else if ( !strcmp(key,"rank0") ) {
1.38      noro     10143:       nd_rank0 = value?1:0;
1.1       noro     10144:     } else if ( !strcmp(key,"splist") ) {
1.38      noro     10145:       nd_splist = value?1:0;
1.1       noro     10146:     } else if ( !strcmp(key,"check_splist") ) {
                   10147:       nd_check_splist = BDY((LIST)value);
                   10148:     } else if ( !strcmp(key,"sugarweight") ) {
                   10149:       u = BDY((LIST)value);
1.38      noro     10150:       n = length(u);
                   10151:       nd_sugarweight = MALLOC(n*sizeof(int));
1.1       noro     10152:       for ( i = 0; i < n; i++, u = NEXT(u) )
1.38      noro     10153:         nd_sugarweight[i] = ZTOS((Q)BDY(u));
                   10154:     } else if ( !strcmp(key,"f4_td") ) {
                   10155:       nd_f4_td = value?1:0;
                   10156:     } else if ( !strcmp(key,"sba_f4step") ) {
                   10157:       nd_sba_f4step = value?ZTOS((Q)value):0;
                   10158:     } else if ( !strcmp(key,"sba_pot") ) {
1.44      noro     10159:       nd_sba_pot = ZTOS((Q)value);
1.38      noro     10160:     } else if ( !strcmp(key,"sba_largelcm") ) {
                   10161:       nd_sba_largelcm = value?1:0;
1.39      noro     10162:     } else if ( !strcmp(key,"sba_dontsort") ) {
                   10163:       nd_sba_dontsort = value?1:0;
1.44      noro     10164:     } else if ( !strcmp(key,"sba_syz") ) {
                   10165:       nd_sba_syz = value?1:0;
1.45    ! noro     10166:     } else if ( !strcmp(key,"sba_fixord") ) {
        !          10167:       nd_sba_fixord = value?1:0;
        !          10168:     } else if ( !strcmp(key,"sba_grevlexgb") ) {
        !          10169:       nd_sba_grevlexgb = value?1:0;
1.41      noro     10170:     } else if ( !strcmp(key,"sba_redundant_check") ) {
                   10171:       nd_sba_redundant_check = value?1:0;
1.39      noro     10172:     } else if ( !strcmp(key,"top") ) {
                   10173:       nd_top = value?1:0;
1.1       noro     10174:     }
1.38      noro     10175:   }
1.44      noro     10176:   if ( nd_sba_syz ) nd_sba_dontsort = 1;
1.1       noro     10177: }
                   10178:
                   10179: ND mdptond(DP d);
                   10180: ND nd_mul_nm(int mod,NM m0,ND p);
                   10181: ND nd_mul_nm_lf(NM m0,ND p);
                   10182: ND *btog(NODE ti,ND **p,int nb,int mod);
                   10183: ND btog_one(NODE ti,ND *p,int nb,int mod);
                   10184: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
                   10185: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
                   10186:
                   10187: /* d:monomial */
                   10188: ND mdptond(DP d)
                   10189: {
                   10190:   NM m;
                   10191:   ND r;
                   10192:
                   10193:   if ( OID(d) == 1 )
                   10194:   r = ptond(CO,CO,(P)d);
                   10195:   else {
                   10196:     NEWNM(m);
                   10197:     dltondl(NV(d),BDY(d)->dl,DL(m));
1.6       noro     10198:     CZ(m) = (Z)BDY(d)->c;
1.1       noro     10199:     NEXT(m) = 0;
                   10200:     MKND(NV(d),m,1,r);
                   10201:   }
                   10202:     return r;
                   10203: }
                   10204:
                   10205: ND nd_mul_nm(int mod,NM m0,ND p)
                   10206: {
                   10207:   UINT *d0;
                   10208:   int c0,c1,c;
                   10209:   NM tm,mr,mr0;
                   10210:   ND r;
                   10211:
                   10212:   if ( !p ) return 0;
                   10213:   d0 = DL(m0);
                   10214:   c0 = CM(m0);
                   10215:   mr0 = 0;
                   10216:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   10217:     NEXTNM(mr0,mr);
                   10218:   c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
                   10219:   ndl_add(d0,DL(tm),DL(mr));
                   10220:   }
                   10221:   NEXT(mr) = 0;
                   10222:   MKND(NV(p),mr0,LEN(p),r);
                   10223:   return r;
                   10224: }
                   10225:
                   10226: ND nd_mul_nm_lf(NM m0,ND p)
                   10227: {
                   10228:   UINT *d0;
                   10229:   Z c0,c1,c;
                   10230:   NM tm,mr,mr0;
                   10231:   ND r;
                   10232:
                   10233:   if ( !p ) return 0;
                   10234:   d0 = DL(m0);
                   10235:   c0 = CZ(m0);
                   10236:   mr0 = 0;
                   10237:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   10238:     NEXTNM(mr0,mr);
                   10239:     c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
                   10240:     ndl_add(d0,DL(tm),DL(mr));
                   10241:   }
                   10242:   NEXT(mr) = 0;
                   10243:   MKND(NV(p),mr0,LEN(p),r);
                   10244:   return r;
                   10245: }
                   10246:
                   10247: ND *btog(NODE ti,ND **p,int nb,int mod)
                   10248: {
                   10249:   PGeoBucket *r;
                   10250:   int i,ci;
                   10251:   NODE t,s;
                   10252:   ND m,tp;
                   10253:   ND *pi,*rd;
                   10254:   P c;
                   10255:
                   10256:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   10257:   for ( i = 0; i < nb; i++ )
                   10258:     r[i] = create_pbucket();
                   10259:   for ( t = ti; t; t = NEXT(t) ) {
                   10260:   s = BDY((LIST)BDY(t));
                   10261:     if ( ARG0(s) ) {
                   10262:     m = mdptond((DP)ARG2(s));
1.6       noro     10263:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     10264:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   10265:       HCM(m) = ci;
1.6       noro     10266:       pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     10267:       for ( i = 0; i < nb; i++ ) {
                   10268:       tp = nd_mul_nm(mod,BDY(m),pi[i]);
                   10269:         add_pbucket(mod,r[i],tp);
                   10270:       }
                   10271:     }
                   10272:     ci = 1;
                   10273:     } else {
                   10274:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   10275:     ci = invm(ci,mod);
                   10276:   }
                   10277:   }
                   10278:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   10279:   for ( i = 0; i < nb; i++ )
                   10280:   rd[i] = normalize_pbucket(mod,r[i]);
                   10281:   if ( ci != 1 )
                   10282:     for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
                   10283:    return rd;
                   10284: }
                   10285:
                   10286: /* YYY */
                   10287: ND *btog_lf(NODE ti,ND **p,int nb)
                   10288: {
                   10289:   PGeoBucket *r;
                   10290:   int i;
                   10291:   NODE t,s;
                   10292:   ND m,tp;
                   10293:   ND *pi,*rd;
                   10294:   LM lm;
                   10295:   Z lf,c;
                   10296:
                   10297:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   10298:   for ( i = 0; i < nb; i++ )
                   10299:     r[i] = create_pbucket();
                   10300:   for ( t = ti; t; t = NEXT(t) ) {
                   10301:     s = BDY((LIST)BDY(t));
                   10302:     if ( ARG0(s) ) {
                   10303:       m = mdptond((DP)ARG2(s));
1.6       noro     10304:       simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1       noro     10305:       if ( lm ) {
                   10306:         lmtolf(lm,&lf); HCZ(m) = lf;
1.6       noro     10307:         pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     10308:         for ( i = 0; i < nb; i++ ) {
                   10309:           tp = nd_mul_nm_lf(BDY(m),pi[i]);
                   10310:           add_pbucket(-2,r[i],tp);
                   10311:         }
                   10312:       }
                   10313:       c = ONE;
                   10314:     } else {
                   10315:       simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
                   10316:     }
                   10317:   }
                   10318:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   10319:   for ( i = 0; i < nb; i++ )
                   10320:     rd[i] = normalize_pbucket(-2,r[i]);
                   10321:   for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
                   10322:   return rd;
                   10323: }
                   10324:
                   10325: ND btog_one(NODE ti,ND *p,int nb,int mod)
                   10326: {
                   10327:   PGeoBucket r;
                   10328:   int i,ci,j;
                   10329:   NODE t,s;
                   10330:   ND m,tp;
                   10331:   ND pi,rd;
                   10332:   P c;
                   10333:
                   10334:   r = create_pbucket();
                   10335:   for ( t = ti; t; t = NEXT(t) ) {
                   10336:   s = BDY((LIST)BDY(t));
                   10337:     if ( ARG0(s) ) {
                   10338:     m = mdptond((DP)ARG2(s));
1.6       noro     10339:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     10340:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   10341:       HCM(m) = ci;
1.6       noro     10342:       pi = p[j=ZTOS((Q)ARG1(s))];
1.1       noro     10343:     if ( !pi ) {
                   10344:       pi = nd_load_mod(j);
                   10345:       tp = nd_mul_nm(mod,BDY(m),pi);
                   10346:       nd_free(pi);
                   10347:         add_pbucket(mod,r,tp);
                   10348:     } else {
                   10349:       tp = nd_mul_nm(mod,BDY(m),pi);
                   10350:         add_pbucket(mod,r,tp);
                   10351:       }
                   10352:     }
                   10353:     ci = 1;
                   10354:     } else {
                   10355:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   10356:     ci = invm(ci,mod);
                   10357:   }
                   10358:   }
                   10359:   rd = normalize_pbucket(mod,r);
                   10360:   free_pbucket(r);
                   10361:   if ( ci != 1 ) nd_mul_c(mod,rd,ci);
                   10362:   return rd;
                   10363: }
                   10364:
                   10365: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
                   10366:
                   10367: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
                   10368: {
                   10369:   int i,j,n,m,nb,pi0,pi1,nvar;
                   10370:   VL fv,tv,vv;
                   10371:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   10372:   ND **p;
                   10373:   ND *c;
                   10374:   ND u;
                   10375:   P inv;
                   10376:   MAT mat;
                   10377:
                   10378:   if ( mod == -2 )
                   10379:     return nd_btog_lf(f,v,ord,tlist,rp);
                   10380:
                   10381:   parse_nd_option(current_option);
                   10382:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   10383:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   10384:   switch ( ord->id ) {
                   10385:     case 1:
                   10386:       if ( ord->nv != nvar )
                   10387:         error("nd_check : invalid order specification");
                   10388:       break;
                   10389:     default:
                   10390:       break;
                   10391:   }
                   10392:   nd_init_ord(ord);
                   10393: #if 0
1.6       noro     10394:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     10395: #else
                   10396:   nd_bpe = 32;
                   10397: #endif
                   10398:   nd_setup_parameters(nvar,0);
                   10399:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   10400:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   10401:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   10402:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   10403:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     10404:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     10405:   if ( j > i ) i = j;
                   10406:   }
                   10407:   n = i+1;
                   10408:   nb = length(BDY(f));
                   10409:   p = (ND **)MALLOC(n*sizeof(ND *));
                   10410:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   10411:     pi = BDY((LIST)BDY(t));
1.6       noro     10412:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     10413:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   10414:     ptomp(mod,(P)ARG2(pi),&inv);
                   10415:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   10416:     u = ptond(CO,vv,(P)ONE);
                   10417:     HCM(u) = ((MQ)inv)->cont;
                   10418:     c[pi1] = u;
                   10419:   }
                   10420:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   10421:     printf("%d ",i); fflush(stdout);
                   10422:     ti = BDY((LIST)BDY(t));
1.6       noro     10423:     p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10424:   }
                   10425:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   10426:     printf("%d ",i); fflush(stdout);
                   10427:     ti = BDY((LIST)BDY(t));
1.6       noro     10428:     p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10429:   }
                   10430:   m = length(ind);
                   10431:   MKMAT(mat,nb,m);
                   10432:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     10433:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     10434:       BDY(mat)[i][j] = ndtodp(mod,c[i]);
                   10435:   return mat;
                   10436: }
                   10437:
                   10438: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
                   10439: {
                   10440:   int i,j,n,m,nb,pi0,pi1,nvar;
                   10441:   VL fv,tv,vv;
                   10442:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   10443:   ND **p;
                   10444:   ND *c;
                   10445:   ND u;
                   10446:   MAT mat;
                   10447:   LM lm;
                   10448:   Z lf,inv;
                   10449:
                   10450:   parse_nd_option(current_option);
                   10451:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   10452:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   10453:   switch ( ord->id ) {
                   10454:     case 1:
                   10455:       if ( ord->nv != nvar )
                   10456:         error("nd_check : invalid order specification");
                   10457:       break;
                   10458:     default:
                   10459:       break;
                   10460:   }
                   10461:   nd_init_ord(ord);
                   10462: #if 0
1.6       noro     10463:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     10464: #else
                   10465:   nd_bpe = 32;
                   10466: #endif
                   10467:   nd_setup_parameters(nvar,0);
                   10468:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   10469:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   10470:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   10471:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   10472:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     10473:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     10474:   if ( j > i ) i = j;
                   10475:   }
                   10476:   n = i+1;
                   10477:   nb = length(BDY(f));
                   10478:   p = (ND **)MALLOC(n*sizeof(ND *));
                   10479:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   10480:     pi = BDY((LIST)BDY(t));
1.6       noro     10481:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     10482:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   10483:     simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
                   10484:     u = ptond(CO,vv,(P)ONE);
                   10485:     HCZ(u) = inv;
                   10486:     c[pi1] = u;
                   10487:   }
                   10488:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   10489:     printf("%d ",i); fflush(stdout);
                   10490:     ti = BDY((LIST)BDY(t));
1.6       noro     10491:     p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     10492:   }
                   10493:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   10494:     printf("%d ",i); fflush(stdout);
                   10495:     ti = BDY((LIST)BDY(t));
1.6       noro     10496:     p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     10497:   }
                   10498:   m = length(ind);
                   10499:   MKMAT(mat,nb,m);
                   10500:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     10501:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     10502:       BDY(mat)[i][j] = ndtodp(-2,c[i]);
                   10503:   return mat;
                   10504: }
                   10505:
                   10506: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
                   10507:   LIST tlist,int pos,MAT *rp)
                   10508: {
                   10509:   int i,j,n,m,nb,pi0,pi1,nvar;
                   10510:   VL fv,tv,vv;
                   10511:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   10512:   ND *p;
                   10513:   ND *c;
                   10514:   ND u;
                   10515:   P inv;
                   10516:   VECT vect;
                   10517:
                   10518:   if ( mod == -2 )
                   10519:     error("nd_btog_one : not implemented yet for a large finite field");
                   10520:
                   10521:   parse_nd_option(current_option);
                   10522:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   10523:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   10524:   switch ( ord->id ) {
                   10525:     case 1:
                   10526:       if ( ord->nv != nvar )
                   10527:         error("nd_check : invalid order specification");
                   10528:       break;
                   10529:     default:
                   10530:       break;
                   10531:   }
                   10532:   nd_init_ord(ord);
                   10533: #if 0
1.6       noro     10534:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     10535: #else
                   10536:   nd_bpe = 32;
                   10537: #endif
                   10538:   nd_setup_parameters(nvar,0);
                   10539:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   10540:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   10541:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   10542:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   10543:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     10544:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     10545:   if ( j > i ) i = j;
                   10546:   }
                   10547:   n = i+1;
                   10548:   nb = length(BDY(f));
                   10549:   p = (ND *)MALLOC(n*sizeof(ND *));
                   10550:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   10551:     pi = BDY((LIST)BDY(t));
1.6       noro     10552:   pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     10553:   if ( pi1 == pos ) {
                   10554:     ptomp(mod,(P)ARG2(pi),&inv);
                   10555:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   10556:     u = ptond(CO,vv,(P)ONE);
                   10557:     HCM(u) = ((MQ)inv)->cont;
                   10558:     p[pi0] = u;
                   10559:     }
                   10560:   }
                   10561:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   10562:   printf("%d ",i); fflush(stdout);
                   10563:     ti = BDY((LIST)BDY(t));
1.6       noro     10564:     p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10565:     if ( Demand ) {
                   10566:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   10567:   }
                   10568:   }
                   10569:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   10570:   printf("%d ",i); fflush(stdout);
                   10571:     ti = BDY((LIST)BDY(t));
1.6       noro     10572:     p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10573:     if ( Demand ) {
                   10574:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   10575:   }
                   10576:   }
                   10577:   m = length(ind);
                   10578:   MKVECT(vect,m);
                   10579:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6       noro     10580:   u = p[ZTOS((Q)BDY(t))];
1.1       noro     10581:   if ( !u ) {
1.6       noro     10582:     u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1       noro     10583:     BDY(vect)[j] = ndtodp(mod,u);
                   10584:     nd_free(u);
                   10585:   } else
                   10586:     BDY(vect)[j] = ndtodp(mod,u);
                   10587:   }
                   10588:   return vect;
                   10589: }
                   10590:
                   10591: void ndv_print_lf(NDV p)
                   10592: {
                   10593:     NMV m;
                   10594:     int i,len;
                   10595:
                   10596:     if ( !p ) printf("0\n");
                   10597:     else {
                   10598:         len = LEN(p);
                   10599:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   10600:             printf("+");
                   10601:             mpz_out_str(asir_out,10,BDY(CZ(m)));
                   10602:             printf("*");
                   10603:             ndl_print(DL(m));
                   10604:         }
                   10605:         printf("\n");
                   10606:     }
                   10607: }
                   10608:
                   10609: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
                   10610: {
                   10611:     VL tv,fv,vv,vc,av;
                   10612:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   10613:     int m,nocheck,nvar,mindex,e,max;
                   10614:     NDV c;
                   10615:     NMV a;
                   10616:     P p,zp;
                   10617:     Q dmy;
                   10618:     EPOS oepos;
                   10619:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   10620:     Alg alpha,dp;
                   10621:     P poly;
                   10622:     LIST f1,f2,zpl;
                   10623:     Obj obj;
                   10624:     NumberField nf;
                   10625:     struct order_spec *ord1;
                   10626:     struct oEGT eg_check,eg0,eg1;
                   10627:     NODE tr,tl1,tl2,tl3,tl4;
                   10628:     LIST l1,l2,l3,l4,l5;
                   10629:     int *perm;
                   10630:     int j,ret;
                   10631:     NODE retn;
                   10632:     Q jq,bpe;
                   10633:
                   10634:     nd_module = 0;
                   10635:     parse_nd_option(current_option);
                   10636:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   10637:     if ( nd_vc )
                   10638:       error("nd_f4_lf_trace : computation over a rational function field is not implemented");
                   10639:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   10640:     switch ( ord->id ) {
                   10641:         case 1:
                   10642:             if ( ord->nv != nvar )
                   10643:                 error("nd_f4_lf_trace : invalid order specification");
                   10644:             break;
                   10645:         default:
                   10646:             break;
                   10647:     }
                   10648:
                   10649:     nd_ntrans = nvar;
                   10650:     nd_nalg = 0;
                   10651:
                   10652:     nocheck = 0;
                   10653:     mindex = 0;
                   10654:
                   10655:     /* do not use on-demand load/save */
                   10656:     nd_demand = 0;
                   10657:     m = trace > 1 ? trace : get_lprime(mindex);
                   10658:     nd_init_ord(ord);
                   10659:     mrank = 0;
                   10660:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   10661:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   10662:             if ( nd_module ) {
                   10663:                 s = BDY((LIST)BDY(t));
                   10664:                 trank = length(s);
                   10665:                 mrank = MAX(mrank,trank);
                   10666:                 for ( ; s; s = NEXT(s) ) {
                   10667:                     e = getdeg(tv->v,(P)BDY(s));
                   10668:                     max = MAX(e,max);
                   10669:                 }
                   10670:             } else {
                   10671:                 e = getdeg(tv->v,(P)BDY(t));
                   10672:                 max = MAX(e,max);
                   10673:             }
                   10674:         }
                   10675:     nd_setup_parameters(nvar,max);
                   10676:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   10677:     ishomo = 1;
                   10678:     /* XXX */
                   10679:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   10680:         if ( nd_module ) {
                   10681:             c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
                   10682:         } else {
                   10683:             c = (pointer)ptondv(CO,vv,(P)BDY(t));
                   10684:         }
                   10685:         if ( ishomo )
                   10686:             ishomo = ishomo && ndv_ishomo(c);
                   10687:         if ( c ) {
                   10688:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   10689:             ndv_mod(-2,c);
                   10690:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   10691:         }
                   10692:     }
                   10693:     if ( in0 ) NEXT(in) = 0;
                   10694:     if ( fd0 ) NEXT(fd) = 0;
                   10695:     if ( !ishomo && homo ) {
                   10696:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   10697:             c = (NDV)BDY(t); len = LEN(c);
                   10698:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   10699:                 wmax = MAX(TD(DL(a)),wmax);
                   10700:         }
                   10701:         homogenize_order(ord,nvar,&ord1);
                   10702:         nd_init_ord(ord1);
                   10703:         nd_setup_parameters(nvar+1,wmax);
                   10704:         for ( t = fd0; t; t = NEXT(t) )
                   10705:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   10706:     }
                   10707:     if ( MaxDeg > 0 ) nocheck = 1;
1.24      noro     10708:     ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1       noro     10709:     if ( ret )
                   10710:       cand = nd_f4_lf_trace_main(m,&perm);
                   10711:     if ( !ret || !cand ) {
                   10712:        *rp = 0; return;
                   10713:     }
                   10714:     if ( !ishomo && homo ) {
                   10715:       /* dehomogenization */
                   10716:       for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   10717:       nd_init_ord(ord);
                   10718:       nd_setup_parameters(nvar,0);
                   10719:     }
                   10720:     cand = ndv_reducebase(cand,perm);
                   10721:     cand = ndv_reduceall(-2,cand);
                   10722:     cbpe = nd_bpe;
                   10723:     get_eg(&eg0);
                   10724:     if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   10725:       /* gbcheck : cand is a GB of Id(cand) ? */
                   10726:       retn = nd_f4(-2,0,0);
                   10727:     }
                   10728:   if ( !retn ) {
                   10729:       /* failure */
                   10730:       *rp = 0; return;
                   10731:     }
                   10732:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   10733:     if ( DP_Print )
1.5       noro     10734:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     10735:     /* dp->p */
                   10736:     nd_bpe = cbpe;
                   10737:     nd_setup_parameters(nd_nvar,0);
                   10738:     for ( r = cand; r; r = NEXT(r) ) {
                   10739:       if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
                   10740:       else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
                   10741:     }
                   10742:     MKLIST(*rp,cand);
                   10743: }
                   10744:
                   10745: NODE nd_f4_lf_trace_main(int m,int **indp)
                   10746: {
                   10747:     int i,nh,stat,index;
                   10748:     NODE r,rm,g;
                   10749:     ND_pairs d,l,l0,t;
                   10750:     ND spol,red;
                   10751:     NDV nf,redv,nfqv,nfv;
                   10752:     NM s0,s;
                   10753:     NODE rp0,srp0,nflist,nflist_lf;
                   10754:     int nsp,nred,col,rank,len,k,j,a;
                   10755:     UINT c;
                   10756:     UINT **spmat;
                   10757:     UINT *s0vect,*svect,*p,*v;
                   10758:     int *colstat;
                   10759:     IndArray *imat;
                   10760:     int *rhead;
                   10761:     int spcol,sprow;
                   10762:     int sugar;
                   10763:     PGeoBucket bucket;
                   10764:     struct oEGT eg0,eg1,eg_f4;
                   10765:
                   10766:     g = 0; d = 0;
                   10767:     for ( i = 0; i < nd_psn; i++ ) {
                   10768:         d = update_pairs(d,g,i,0);
                   10769:         g = update_base(g,i);
                   10770:     }
                   10771:     while ( d ) {
                   10772:         get_eg(&eg0);
                   10773:         l = nd_minsugarp(d,&d);
                   10774:         sugar = SG(l);
                   10775:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   10776:         bucket = create_pbucket();
                   10777:         stat = nd_sp_f4(m,0,l,bucket);
                   10778:         if ( !stat ) {
                   10779:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10780:             NEXT(t) = d; d = l;
                   10781:             d = nd_reconstruct(1,d);
                   10782:             continue;
                   10783:         }
                   10784:         if ( bucket->m < 0 ) continue;
                   10785:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   10786:         if ( !col ) {
                   10787:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10788:             NEXT(t) = d; d = l;
                   10789:             d = nd_reconstruct(1,d);
                   10790:             continue;
                   10791:         }
                   10792:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   10793:         if ( DP_Print )
1.5       noro     10794:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1       noro     10795:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   10796:         if ( !l0 ) continue;
                   10797:         l = l0;
                   10798:
                   10799:         /* over LF */
                   10800:         bucket = create_pbucket();
                   10801:         stat = nd_sp_f4(-2,1,l,bucket);
                   10802:         if ( !stat ) {
                   10803:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10804:             NEXT(t) = d; d = l;
                   10805:             d = nd_reconstruct(1,d);
                   10806:             continue;
                   10807:         }
                   10808:         if ( bucket->m < 0 ) continue;
                   10809:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   10810:         if ( !col ) {
                   10811:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10812:             NEXT(t) = d; d = l;
                   10813:             d = nd_reconstruct(1,d);
                   10814:             continue;
                   10815:         }
                   10816:         nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
                   10817:         /* adding new bases */
                   10818:         for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
                   10819:             nfv = (NDV)BDY(rm);
                   10820:             nfqv = (NDV)BDY(r);
                   10821:             if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
                   10822:             ndv_removecont(m,nfv);
                   10823:             ndv_removecont(-2,nfqv);
1.24      noro     10824:             nh = ndv_newps(-2,nfv,nfqv);
1.1       noro     10825:             d = update_pairs(d,g,nh,0);
                   10826:             g = update_base(g,nh);
                   10827:         }
                   10828:         if ( r || rm ) return 0;
                   10829:     }
                   10830:   conv_ilist(nd_demand,1,g,indp);
                   10831:     return g;
                   10832: }
                   10833:
1.7       noro     10834: #if SIZEOF_LONG==8
                   10835:
                   10836: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   10837: {
                   10838:     int j,k,len;
                   10839:     UINT *p;
                   10840:     UINT c;
                   10841:     NDV r;
                   10842:     NMV mr0,mr;
                   10843:
                   10844:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   10845:     if ( !len ) return 0;
                   10846:     else {
                   10847:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   10848: #if 0
                   10849:         ndv_alloc += nmv_adv*len;
                   10850: #endif
                   10851:         mr = mr0;
                   10852:         p = s0vect;
                   10853:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   10854:             if ( !rhead[j] ) {
                   10855:                 if ( (c = (UINT)vect[k++]) != 0 ) {
                   10856:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   10857:                 }
                   10858:             }
                   10859:         MKNDV(nd_nvar,mr0,len,r);
                   10860:         return r;
                   10861:     }
                   10862: }
                   10863:
1.28      noro     10864: NDV vect64_to_ndv_s(mp_limb_t *vect,int col,UINT *s0vect)
                   10865: {
                   10866:     int j,k,len;
                   10867:     UINT *p;
                   10868:     UINT c;
                   10869:     NDV r;
                   10870:     NMV mr0,mr;
                   10871:
                   10872:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   10873:     if ( !len ) return 0;
                   10874:     else {
                   10875:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   10876:         mr = mr0;
                   10877:         p = s0vect;
                   10878:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   10879:           if ( (c = (UINT)vect[k++]) != 0 ) {
                   10880:             ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   10881:           }
                   10882:         MKNDV(nd_nvar,mr0,len,r);
                   10883:         return r;
                   10884:     }
                   10885: }
                   10886:
1.7       noro     10887: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
                   10888: {
                   10889:     NM m;
1.11      noro     10890:     UINT *t,*s,*u;
                   10891:     int i,st,ed,md,prev,c;
1.7       noro     10892:
                   10893:     for ( i = 0; i < n; i++ ) r[i] = 0;
1.11      noro     10894:     prev = 0;
                   10895:     for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
                   10896:       t = DL(m);
                   10897:       st = prev;
                   10898:       ed = n;
                   10899:       while ( ed > st ) {
                   10900:         md = (st+ed)/2;
                   10901:         u = s0+md*nd_wpd;
                   10902:         c = DL_COMPARE(u,t);
                   10903:         if ( c == 0 ) break;
                   10904:         else if ( c > 0 ) st = md;
                   10905:         else ed = md;
                   10906:       }
                   10907:       r[md] = (mp_limb_t)CM(m);
                   10908:       prev = md;
1.7       noro     10909:     }
                   10910:     for ( i = 0; !r[i]; i++ );
                   10911:     return i;
                   10912: }
                   10913:
                   10914: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
                   10915:
1.28      noro     10916: 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     10917: {
                   10918:     int i,j,k,len,pos,prev;
                   10919:     mp_limb_t a,c,c1,c2;
                   10920:     IndArray ivect;
                   10921:     unsigned char *ivc;
                   10922:     unsigned short *ivs;
                   10923:     unsigned int *ivi;
                   10924:     NDV redv;
                   10925:     NMV mr;
                   10926:     NODE rp;
                   10927:     int maxrs;
                   10928:
                   10929:     for ( i = 0; i < col; i++ ) cvect[i] = 0;
                   10930:     maxrs = 0;
                   10931:     for ( i = 0; i < nred; i++ ) {
                   10932:         ivect = imat[i];
                   10933:         k = ivect->head;
                   10934:         a = svect[k]; c = cvect[k];
                   10935:         MOD128(a,c,m);
                   10936:         svect[k] = a; cvect[k] = 0;
1.28      noro     10937:         if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.11      noro     10938:             Nf4_red++;
1.7       noro     10939:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   10940:             c = m-c; redv = nd_ps[rp0[i]->index];
                   10941:             len = LEN(redv); mr = BDY(redv);
                   10942:             svect[k] = 0; prev = k;
                   10943:             switch ( ivect->width ) {
                   10944:                 case 1:
                   10945:                     ivc = ivect->index.c;
                   10946:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   10947:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12      noro     10948:                         c2 = svect[pos]+c1*c;
                   10949:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   10950:                         svect[pos] = c2;
1.7       noro     10951:                     }
                   10952:                     break;
                   10953:                 case 2:
                   10954:                     ivs = ivect->index.s;
                   10955:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   10956:                         pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12      noro     10957:                         c2 = svect[pos]+c1*c;
                   10958:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   10959:                         svect[pos] = c2;
1.7       noro     10960:                     }
                   10961:                     break;
                   10962:                 case 4:
                   10963:                     ivi = ivect->index.i;
                   10964:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   10965:                         pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12      noro     10966:                         c2 = svect[pos]+c1*c;
                   10967:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   10968:                         svect[pos] = c2;
1.7       noro     10969:                     }
                   10970:                     break;
                   10971:             }
                   10972:         }
                   10973:     }
                   10974:     for ( i = 0; i < col; i++ ) {
                   10975:       a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
                   10976:     }
                   10977:     return maxrs;
                   10978: }
                   10979:
                   10980: /* for Fp, 2^15=<p<2^29 */
                   10981:
                   10982: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   10983:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   10984: {
                   10985:     int spcol,sprow,a;
                   10986:     int i,j,k,l,rank;
                   10987:     NODE r0,r;
                   10988:     ND_pairs sp;
                   10989:     ND spol;
                   10990:     mp_limb_t **spmat;
                   10991:     mp_limb_t *svect,*cvect;
                   10992:     mp_limb_t *v;
                   10993:     int *colstat;
                   10994:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   10995:     int maxrs;
                   10996:     int *spsugar;
                   10997:     ND_pairs *spactive;
                   10998:
                   10999:     spcol = col-nred;
                   11000:     get_eg(&eg0);
                   11001:     /* elimination (1st step) */
                   11002:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   11003:     svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11004:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11005:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   11006:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   11007:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   11008:         nd_sp(m,0,sp,&spol);
                   11009:         if ( !spol ) continue;
                   11010:         nd_to_vect64(m,s0vect,col,spol,svect);
1.28      noro     11011:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,0);
1.7       noro     11012:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   11013:         if ( i < col ) {
                   11014:             spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
                   11015:             for ( j = k = 0; j < col; j++ )
                   11016:                 if ( !rhead[j] ) v[k++] = (UINT)svect[j];
                   11017:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   11018:             if ( nz )
                   11019:             spactive[sprow] = sp;
                   11020:             sprow++;
                   11021:         }
                   11022:         nd_free(spol);
                   11023:     }
1.12      noro     11024:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7       noro     11025:     if ( DP_Print ) {
                   11026:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   11027:         fflush(asir_out);
                   11028:     }
                   11029:     /* free index arrays */
                   11030:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   11031:
                   11032:     /* elimination (2nd step) */
                   11033:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   11034:     rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   11035:     r0 = 0;
                   11036:     for ( i = 0; i < rank; i++ ) {
                   11037:         NEXTNODE(r0,r); BDY(r) =
                   11038:           (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   11039:         SG((NDV)BDY(r)) = spsugar[i];
                   11040:         GCFREE(spmat[i]);
                   11041:     }
                   11042:     if ( r0 ) NEXT(r) = 0;
                   11043:
                   11044:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12      noro     11045:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7       noro     11046:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   11047:     if ( DP_Print ) {
                   11048:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   11049:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   11050:             nsp,nred,sprow,spcol,rank);
                   11051:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   11052:     }
                   11053:     if ( nz ) {
                   11054:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   11055:         if ( rank > 0 ) {
                   11056:             NEXT(spactive[rank-1]) = 0;
                   11057:             *nz = spactive[0];
                   11058:         } else
                   11059:             *nz = 0;
                   11060:     }
                   11061:     return r0;
                   11062: }
                   11063:
                   11064: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   11065: {
                   11066:   int i,j,k,l,rank,s;
                   11067:   mp_limb_t inv;
                   11068:   mp_limb_t a;
                   11069:   UINT c;
                   11070:   mp_limb_t *t,*pivot,*pk;
                   11071:   UINT *ck;
                   11072:   UINT **cmat;
                   11073:   UINT *ct;
                   11074:   ND_pairs pair;
                   11075:
                   11076:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   11077:   for ( i = 0; i < row; i++ ) {
                   11078:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   11079:     bzero(cmat[i],col*sizeof(UINT));
                   11080:   }
                   11081:
                   11082:   for ( rank = 0, j = 0; j < col; j++ ) {
                   11083:     for ( i = rank; i < row; i++ ) {
                   11084:       a = mat[i][j]; c = cmat[i][j];
                   11085:       MOD128(a,c,md);
                   11086:       mat[i][j] = a; cmat[i][j] = 0;
                   11087:     }
                   11088:     for ( i = rank; i < row; i++ )
                   11089:       if ( mat[i][j] )
                   11090:         break;
                   11091:     if ( i == row ) {
                   11092:       colstat[j] = 0;
                   11093:       continue;
                   11094:     } else
                   11095:       colstat[j] = 1;
                   11096:     if ( i != rank ) {
                   11097:       t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   11098:       ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
                   11099:       s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   11100:       if ( spactive ) {
                   11101:         pair = spactive[i]; spactive[i] = spactive[rank];
                   11102:         spactive[rank] = pair;
                   11103:       }
                   11104:     }
                   11105:     /* column j is normalized */
                   11106:     s = sugar[rank];
                   11107:     inv = invm((UINT)mat[rank][j],md);
                   11108:     /* normalize pivot row */
                   11109:     for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
                   11110:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   11111:     }
                   11112:     for ( i = rank+1; i < row; i++ ) {
                   11113:       if ( (a = mat[i][j]) != 0 ) {
                   11114:         sugar[i] = MAX(sugar[i],s);
                   11115:         red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11      noro     11116:         Nf4_red++;
1.7       noro     11117:       }
                   11118:     }
                   11119:     rank++;
                   11120:   }
                   11121:   for ( j = col-1, l = rank-1; j >= 0; j-- )
                   11122:     if ( colstat[j] ) {
                   11123:       for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
                   11124:         a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
                   11125:       }
                   11126:       s = sugar[l];
                   11127:       for ( i = 0; i < l; i++ ) {
                   11128:         a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
                   11129:         if ( a ) {
                   11130:           sugar[i] = MAX(sugar[i],s);
                   11131:           red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11      noro     11132:           Nf4_red++;
1.7       noro     11133:         }
                   11134:       }
                   11135:       l--;
                   11136:     }
                   11137:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   11138:   GCFREE(cmat);
                   11139:   return rank;
                   11140: }
                   11141:
1.28      noro     11142: 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)
                   11143: {
                   11144:   int i,j,k,l,rank,s,imin;
                   11145:   mp_limb_t inv;
                   11146:   mp_limb_t a;
                   11147:   UINT c;
                   11148:   mp_limb_t *t,*pivot,*pk;
                   11149:   UINT *ck;
                   11150:   UINT **cmat;
                   11151:   UINT *ct;
                   11152:   ND_pairs pair;
                   11153:   SIG sg;
1.31      noro     11154:   int *used;
1.28      noro     11155:
1.31      noro     11156:   used = (int *)MALLOC(row*sizeof(int));
1.28      noro     11157:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   11158:   for ( i = 0; i < row; i++ ) {
                   11159:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   11160:     bzero(cmat[i],col*sizeof(UINT));
                   11161:   }
                   11162:
1.31      noro     11163:   for ( j = 0; j < col; j++ ) {
                   11164:     for ( i = 0; i < row; i++ ) {
1.28      noro     11165:       a = mat[i][j]; c = cmat[i][j];
                   11166:       MOD128(a,c,md);
                   11167:       mat[i][j] = a; cmat[i][j] = 0;
                   11168:     }
1.31      noro     11169:     for ( i = 0; i < row; i++ )
                   11170:       if ( !used[i] && mat[i][j] ) break;
                   11171:     if ( i == row ) {
1.28      noro     11172:       colstat[j] = 0;
                   11173:       continue;
1.31      noro     11174:     } else {
1.28      noro     11175:       colstat[j] = 1;
1.31      noro     11176:       used[i] = 1;
1.28      noro     11177:     }
                   11178:     /* column j is normalized */
1.31      noro     11179:     s = sugar[i];
                   11180:     inv = invm((UINT)mat[i][j],md);
1.28      noro     11181:     /* normalize pivot row */
1.31      noro     11182:     for ( k = j, pk = mat[i]+j, ck = cmat[i]+j; k < col; k++, pk++, ck++ ) {
1.28      noro     11183:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   11184:     }
1.31      noro     11185:     for ( k = i+1; k < row; k++ ) {
                   11186:       if ( (a = mat[k][j]) != 0 ) {
                   11187:         sugar[k] = MAX(sugar[k],s);
                   11188:         red_by_vect64(md,mat[k]+j,cmat[k]+j,mat[i]+j,(int)(md-a),col-j);
1.28      noro     11189:         Nf4_red++;
                   11190:       }
                   11191:     }
                   11192:   }
1.31      noro     11193:   rank = 0;
                   11194:   for ( i = 0; i < row; i++ ) {
                   11195:     for ( j = 0; j < col; j++ )
                   11196:       if ( mat[i][j] ) break;
                   11197:     if ( j == col ) sugar[i] = -1;
                   11198:     else rank++;
                   11199:   }
1.28      noro     11200:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   11201:   GCFREE(cmat);
                   11202:   return rank;
                   11203: }
                   11204:
                   11205: NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   11206:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
                   11207: {
                   11208:     int spcol,sprow,a;
                   11209:     int i,j,k,l,rank;
                   11210:     NODE r0,r;
                   11211:     ND_pairs sp;
                   11212:     ND spol;
                   11213:     mp_limb_t **spmat;
                   11214:     mp_limb_t *svect,*cvect;
                   11215:     mp_limb_t *v;
                   11216:     int *colstat;
                   11217:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   11218:     int maxrs;
                   11219:     int *spsugar;
                   11220:     ND_pairs *spactive;
                   11221:     SIG *spsig;
                   11222:
                   11223:     get_eg(&eg0);
                   11224:     /* elimination (1st step) */
                   11225:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   11226:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11227:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   11228:     spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
                   11229:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   11230:         nd_sp(m,0,sp,&spol);
                   11231:         if ( !spol ) {
1.29      noro     11232:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     11233:           continue;
                   11234:         }
                   11235:         svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11236:         nd_to_vect64(m,s0vect,col,spol,svect);
                   11237:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,spol->sig);
                   11238:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   11239:         if ( i < col ) {
                   11240:             spmat[sprow] = svect;
                   11241:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   11242:             spsig[sprow] = sp->sig;
                   11243:             sprow++;
                   11244:         } else {
1.29      noro     11245:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     11246:         }
                   11247:         nd_free(spol);
                   11248:     }
                   11249:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
                   11250:     if ( DP_Print ) {
                   11251:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   11252:         fflush(asir_out);
                   11253:     }
                   11254:     /* free index arrays */
                   11255:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   11256:
                   11257:     /* elimination (2nd step) */
                   11258:     colstat = (int *)MALLOC(col*sizeof(int));
                   11259:     rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
                   11260:     r0 = 0;
1.31      noro     11261:     for ( i = 0; i < sprow; i++ ) {
                   11262:         if ( spsugar[i] >= 0 ) {
                   11263:           NEXTNODE(r0,r);
                   11264:           BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
                   11265:           SG((NDV)BDY(r)) = spsugar[i];
                   11266:           ((NDV)BDY(r))->sig = spsig[i];
                   11267:         } else
                   11268:           syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
1.28      noro     11269:         GCFREE(spmat[i]);
                   11270:     }
                   11271:     if ( r0 ) NEXT(r) = 0;
                   11272:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
                   11273:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   11274:     if ( DP_Print ) {
                   11275:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   11276:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   11277:             nsp,nred,sprow,col,rank);
                   11278:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   11279:     }
                   11280:     return r0;
                   11281: }
1.40      noro     11282: #endif
1.28      noro     11283:
                   11284: NODE nd_f4_red_s(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,NODE *syzlistp)
                   11285: {
                   11286:   IndArray *imat;
                   11287:   int nsp,nred,i,start;
                   11288:   int *rhead;
                   11289:   NODE r0,rp;
                   11290:   ND_pairs sp;
                   11291:   NM_ind_pair *rvect;
                   11292:   UINT *s;
                   11293:   int *s0hash;
                   11294:   struct oEGT eg0,eg1,eg_conv;
                   11295:
                   11296:   for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   11297:   nred = length(rp0);
                   11298:   imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   11299:   rhead = (int *)MALLOC(col*sizeof(int));
                   11300:   for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   11301:
                   11302:   /* construction of index arrays */
                   11303:   get_eg(&eg0);
                   11304:   if ( DP_Print ) {
                   11305:     fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   11306:     fflush(asir_out);
                   11307:   }
                   11308:   rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   11309:   for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   11310:     rvect[i] = (NM_ind_pair)BDY(rp);
                   11311:     imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
                   11312:     rhead[imat[i]->head] = 1;
                   11313:     start = imat[i]->head;
                   11314:   }
                   11315:   get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
                   11316:   if ( DP_Print ) {
                   11317:     fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   11318:     fflush(asir_out);
                   11319:   }
                   11320:   if ( m > 0 )
1.40      noro     11321: #if SIZEOF_LONG==8
1.28      noro     11322:     r0 = nd_f4_red_mod64_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
1.40      noro     11323: #else
                   11324:     r0 = nd_f4_red_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
                   11325: #endif
1.28      noro     11326:   else
                   11327: //    r0 = nd_f4_red_q_main_s(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   11328:     error("nd_f4_red_q_main_s : not implemented yet");
                   11329:   return r0;
                   11330: }
                   11331:
                   11332: INLINE int ndl_find_reducer_minsig(UINT *dg)
                   11333: {
                   11334:   RHist r;
                   11335:   int i,singular,ret,d,k,imin;
                   11336:   SIG t;
                   11337:   static int wpd,nvar;
                   11338:   static SIG quo,quomin;
                   11339:   static UINT *tmp;
                   11340:
                   11341:   if ( !quo || nvar != nd_nvar ) { NEWSIG(quo); NEWSIG(quomin); }
                   11342:   if ( wpd != nd_wpd ) {
                   11343:     wpd = nd_wpd;
                   11344:     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
                   11345:   }
                   11346: #if 0
                   11347:   d = ndl_hash_value(dg);
                   11348:   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   11349:     if ( ndl_equal(dg,DL(r)) ) {
                   11350:       return r->index;
                   11351:     }
                   11352:   }
                   11353: #endif
                   11354:   imin = -1;
                   11355:   for ( i = 0; i < nd_psn; i++ ) {
                   11356:     r = nd_psh[i];
                   11357:     if ( ndl_reducible(dg,DL(r)) ) {
                   11358:       ndl_sub(dg,DL(r),tmp);
                   11359:       _ndltodl(tmp,DL(quo));
                   11360:       _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
                   11361:       quo->pos = nd_psh[i]->sig->pos;
                   11362:       if ( imin < 0 || comp_sig(quomin,quo) > 0 ) {
                   11363:         t = quo; quo = quomin; quomin = t;
                   11364:         imin = i;
                   11365:       }
                   11366:     }
                   11367:   }
                   11368:   if ( imin == -1 ) return nd_psn;
                   11369:   else {
                   11370: #if 0
                   11371:     nd_append_red(dg,i);
                   11372: #endif
                   11373:     return imin;
                   11374:   }
                   11375: }
                   11376:
                   11377: int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   11378: {
                   11379:   NODE rp0,rp;
                   11380:   NM mul,head,s0,s;
                   11381:   int index,col,i,sugar;
                   11382:   RHist h;
                   11383:   UINT *s0v,*p;
                   11384:   NM_ind_pair pair;
                   11385:   ND red;
                   11386:   NDV *ps;
                   11387:   SIG sig;
                   11388:
                   11389:   s0 = 0; rp0 = 0; col = 0;
                   11390:   if ( nd_demand )
                   11391:     ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   11392:   else
                   11393:     ps = trace?nd_ps_trace:nd_ps;
                   11394:   while ( 1 ) {
                   11395:     head = remove_head_pbucket_symbolic(bucket);
                   11396:     if ( !head ) break;
                   11397:     if ( !s0 ) s0 = head;
                   11398:     else NEXT(s) = head;
                   11399:     s = head;
                   11400:     index = ndl_find_reducer_minsig(DL(head));
                   11401:     if ( index >= 0 && index < nd_psn ) {
                   11402:       h = nd_psh[index];
                   11403:       NEWNM(mul);
                   11404:       ndl_sub(DL(head),DL(h),DL(mul));
                   11405:       if ( ndl_check_bound2(index,DL(mul)) )
                   11406:         return 0;
                   11407:       sugar = TD(DL(mul))+SG(ps[index]);
                   11408:       NEWSIG(sig);
                   11409:       _ndltodl(DL(mul),DL(sig));
                   11410:       _addtodl(nd_nvar,DL(nd_psh[index]->sig),DL(sig));
                   11411:       sig->pos = nd_psh[index]->sig->pos;
                   11412:       MKNM_ind_pair(pair,mul,index,sugar,sig);
                   11413:       red = ndv_mul_nm_symbolic(mul,ps[index]);
                   11414:       add_pbucket_symbolic(bucket,nd_remove_head(red));
                   11415:       NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   11416:     }
                   11417:     col++;
                   11418:   }
                   11419:   if ( rp0 ) NEXT(rp) = 0;
                   11420:   NEXT(s) = 0;
                   11421:   s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   11422:   for ( i = 0, p = s0v, s = s0; i < col;
                   11423:     i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   11424:   *s0vect = s0v;
                   11425:   *r = rp0;
                   11426:
                   11427:   return col;
                   11428: }
                   11429:
                   11430: NODE nd_sba_f4(int m,int **indp)
                   11431: {
1.34      noro     11432:   int i,nh,stat,index,f4red,f4step;
1.33      noro     11433:   int col,rank,len,k,j,a,sugar,nbase,psugar,ms;
                   11434:   NODE r,g,rp0,nflist;
1.41      noro     11435:   ND_pairs d,l,t,l1;
1.33      noro     11436:   ND h,nf;
                   11437:   NDV nfv;
                   11438:   union oNDC hc;
                   11439:   UINT *s0vect;
1.28      noro     11440:   UINT c;
                   11441:   PGeoBucket bucket;
1.33      noro     11442:   NODE *syzlist;
                   11443:   SIG sig;
1.28      noro     11444:   struct oEGT eg0,eg1,eg_f4;
1.33      noro     11445:   struct oEGT eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.28      noro     11446:
                   11447:   Nf4_red=0;
1.30      noro     11448:   d = 0;
1.29      noro     11449:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.28      noro     11450:   for ( i = 0; i < nd_psn; i++ ) {
1.30      noro     11451:     d = update_pairs_s(d,i,syzlist);
1.28      noro     11452:   }
1.30      noro     11453:   nd_nbase = nd_psn;
1.28      noro     11454:   f4red = 1;
1.33      noro     11455:   psugar = 0;
1.34      noro     11456:   f4step = 0;
1.28      noro     11457:   while ( d ) {
1.33      noro     11458:     for ( t = d, ms = SG(d); t; t = NEXT(t) )
                   11459:       if ( SG(t) < ms ) ms = SG(t);
1.38      noro     11460:     if ( ms == psugar && f4step >= nd_sba_f4step ) {
1.33      noro     11461: again:
                   11462:       l = d; d = d->next;
1.41      noro     11463: #if 0
1.33      noro     11464:       if ( small_lcm(l) ) {
                   11465:         if ( DP_Print ) fprintf(asir_out,"M");
                   11466:         continue;
                   11467:       }
                   11468:       sig = l->sig;
                   11469:       stat = nd_sp(m,0,l,&h);
1.41      noro     11470: #else
                   11471:       l1 = find_smallest_lcm(l);
                   11472:       if ( l1 == 0 ) {
                   11473:         if ( DP_Print ) fprintf(asir_out,"M");
                   11474:         continue;
                   11475:       }
                   11476:       sig = l1->sig;
                   11477:       stat = nd_sp(m,0,l1,&h);
                   11478: #endif
1.33      noro     11479:       if ( !stat ) {
                   11480:         NEXT(l) = d; d = l;
                   11481:         d = nd_reconstruct(0,d);
                   11482:         goto again;
                   11483:       }
                   11484:   get_eg(&eg1);
                   11485:   #if USE_GEOBUCKET
1.39      noro     11486:       stat = m?nd_nf_pbucket_s(m,h,nd_ps,!nd_top&&!Top,&nf):nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.33      noro     11487:   #else
1.39      noro     11488:       stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.33      noro     11489:   #endif
                   11490:   get_eg(&eg2);
                   11491:       if ( !stat ) {
                   11492:         NEXT(l) = d; d = l;
                   11493:         d = nd_reconstruct(0,d);
                   11494:         goto again;
                   11495:       } else if ( stat == -1 ) {
                   11496:         if ( DP_Print ) { printf("S"); fflush(stdout); }
                   11497:         FREENDP(l);
                   11498:       } else if ( nf ) {
                   11499:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   11500:         add_eg(&eg_nf,&eg1,&eg2);
                   11501:         hc = HCU(nf);
                   11502:         nd_removecont(m,nf);
                   11503:         nfv = ndtondv(m,nf); nd_free(nf);
                   11504:         nh = ndv_newps(m,nfv,0);
                   11505:
1.30      noro     11506:         d = update_pairs_s(d,nh,syzlist);
                   11507:         nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.33      noro     11508:         FREENDP(l);
1.28      noro     11509:       } else {
1.33      noro     11510:         add_eg(&eg_nfzero,&eg1,&eg2);
                   11511:        // syzygy
                   11512:   get_eg(&eg1);
                   11513:         d = remove_spair_s(d,sig);
                   11514:   get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29      noro     11515:         syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.33      noro     11516:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   11517:         FREENDP(l);
                   11518:       }
                   11519:     } else {
1.34      noro     11520:       if ( ms != psugar ) f4step = 1;
                   11521:       else f4step++;
1.33      noro     11522: again2:
                   11523:       psugar = ms;
                   11524:       l = nd_minsugarp_s(d,&d);
                   11525:       sugar = nd_sugarweight?d->sugar2:SG(d);
                   11526:       bucket = create_pbucket();
                   11527:       stat = nd_sp_f4(m,0,l,bucket);
                   11528:       if ( !stat ) {
                   11529:         for ( t = l; NEXT(t); t = NEXT(t) );
                   11530:         NEXT(t) = d; d = l;
                   11531:         d = nd_reconstruct(0,d);
                   11532:         goto again2;
                   11533:       }
                   11534:       if ( bucket->m < 0 ) continue;
                   11535:       col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
                   11536:       if ( !col ) {
1.37      noro     11537:         for ( t = l; NEXT(t); t = NEXT(t) )
                   11538:           ;
                   11539:         NEXT(t) = d; d = l;
1.33      noro     11540:         d = nd_reconstruct(0,d);
                   11541:         goto again2;
                   11542:       }
1.34      noro     11543:       if ( DP_Print ) fprintf(asir_out,"\nsugar=%d,",psugar);
1.33      noro     11544:       nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
                   11545:       /* adding new bases */
                   11546:       for ( r = nflist; r; r = NEXT(r) ) {
                   11547:         nfv = (NDV)BDY(r);
1.38      noro     11548:         if ( nd_f4_td ) SG(nfv) = nd_tdeg(nfv);
1.33      noro     11549:         ndv_removecont(m,nfv);
                   11550:         nh = ndv_newps(m,nfv,0);
                   11551:         d = update_pairs_s(d,nh,syzlist);
                   11552:         nd_sba_pos[nfv->sig->pos] = append_one(nd_sba_pos[nfv->sig->pos],nh);
                   11553:       }
                   11554:       for ( i = 0; i < nd_nbase; i++ )
                   11555:         for ( r = syzlist[i]; r; r = NEXT(r) )
                   11556:             d = remove_spair_s(d,(SIG)BDY(r));
                   11557:       d = remove_large_lcm(d);
                   11558:       if ( DP_Print ) {
1.34      noro     11559:         fprintf(asir_out,"f4red=%d,gblen=%d",f4red,nd_psn); fflush(asir_out);
1.33      noro     11560:       }
                   11561:       f4red++;
1.28      noro     11562:     }
                   11563:   }
                   11564:   if ( DP_Print ) {
1.34      noro     11565:     fprintf(asir_out,"\nnumber of red=%d,",Nf4_red);
1.28      noro     11566:   }
1.30      noro     11567:   g = conv_ilist_s(nd_demand,0,indp);
1.28      noro     11568:   return g;
                   11569: }

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