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

1.51    ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.50 2021/03/09 05:20:35 noro Exp $ */
1.1       noro        2:
                      3: #include "nd.h"
                      4:
1.45      noro        5: void print_siglist(NODE l);
                      6:
1.49      noro        7: NODE nd_hpdata;
1.47      noro        8: int Nnd_add,Nf4_red,NcriB,NcriMF,Ncri2,Npairs,Nnewpair;
1.12      noro        9: struct oEGT eg_search,f4_symb,f4_conv,f4_elim1,f4_elim2;
1.1       noro       10:
                     11: int diag_period = 6;
                     12: int weight_check = 1;
                     13: int (*ndl_compare_function)(UINT *a1,UINT *a2);
1.21      noro       14: /* for general module order */
1.19      noro       15: int (*ndl_base_compare_function)(UINT *a1,UINT *a2);
1.21      noro       16: int (*dl_base_compare_function)(int nv,DL a,DL b);
                     17: int nd_base_ordtype;
1.1       noro       18: int nd_dcomp;
                     19: int nd_rref2;
                     20: NM _nm_free_list;
                     21: ND _nd_free_list;
                     22: ND_pairs _ndp_free_list;
                     23: NODE nd_hcf;
1.35      noro       24: int Nsyz,Nsamesig;
1.1       noro       25:
                     26: Obj nd_top_weight;
                     27:
                     28: static NODE nd_subst;
                     29: static VL nd_vc;
                     30: static int nd_ntrans;
                     31: static int nd_nalg;
                     32: #if 0
                     33: static int ndv_alloc;
                     34: #endif
                     35: #if 1
                     36: static int nd_f4_nsp=0x7fffffff;
                     37: #else
                     38: static int nd_f4_nsp=50;
                     39: #endif
                     40: static double nd_scale=2;
                     41: static UINT **nd_bound;
                     42: static struct order_spec *nd_ord;
                     43: static EPOS nd_epos;
                     44: static BlockMask nd_blockmask;
                     45: static int nd_nvar;
                     46: static int nd_isrlex;
                     47: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
                     48: static UINT nd_mask[32];
                     49: static UINT nd_mask0,nd_mask1;
                     50:
                     51: static NDV *nd_ps;
                     52: static NDV *nd_ps_trace;
                     53: static NDV *nd_ps_sym;
                     54: static NDV *nd_ps_trace_sym;
                     55: static RHist *nd_psh;
1.30      noro       56: static int nd_psn,nd_pslen,nd_nbase;
1.1       noro       57: static RHist *nd_red;
                     58: static int *nd_work_vector;
                     59: static int **nd_matrix;
                     60: static int nd_matrix_len;
                     61: static struct weight_or_block *nd_worb;
                     62: static int nd_worb_len;
                     63: static int nd_found,nd_create,nd_notfirst;
                     64: static int nmv_adv;
                     65: static int nd_demand;
1.21      noro       66: static int nd_module,nd_module_ordtype,nd_mpos,nd_pot_nelim;
1.1       noro       67: static int nd_module_rank,nd_poly_weight_len;
                     68: static int *nd_poly_weight,*nd_module_weight;
                     69: static NODE nd_tracelist;
                     70: static NODE nd_alltracelist;
1.41      noro       71: static int nd_gentrace,nd_gensyz,nd_nora,nd_newelim,nd_intersect,nd_lf,nd_norb;
                     72: static int nd_f4_td,nd_sba_f4step,nd_sba_pot,nd_sba_largelcm,nd_sba_dontsort,nd_sba_redundant_check;
1.46      noro       73: static int nd_top,nd_sba_syz,nd_sba_inputisgb;
1.1       noro       74: static int *nd_gbblock;
                     75: static NODE nd_nzlist,nd_check_splist;
                     76: static int nd_splist;
                     77: static int *nd_sugarweight;
                     78: static int nd_f4red,nd_rank0,nd_last_nonzero;
1.27      noro       79: static DL *nd_sba_hm;
1.29      noro       80: static NODE *nd_sba_pos;
1.1       noro       81:
1.46      noro       82: struct comp_sig_spec {
                     83:   int n;
                     84:   // current_i <-> oldv[i]
                     85:   int *oldv;
                     86:   int *weight;
                     87:   struct order_pair *order_pair;
                     88:   int block_length;
                     89:   int **matrix;
                     90:   int row;
                     91:   int (*cmpdl)(int n,DL d1,DL d2);
                     92: };
                     93:
                     94: struct comp_sig_spec *nd_sba_modord;
                     95:
1.49      noro       96: DL ndltodl(int n,UINT *ndl);
1.1       noro       97: NumberField get_numberfield();
                     98: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
                     99: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
                    100: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
                    101: int nd_monic(int m,ND *p);
                    102: NDV plain_vect_to_ndv_q(Z *mat,int col,UINT *s0vect);
                    103: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank);
                    104: NDV pltondv(VL vl,VL dvl,LIST p);
                    105: void pltozpl(LIST l,Q *cont,LIST *pp);
                    106: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
                    107: void nmtodp(int mod,NM m,DP *r);
1.15      noro      108: void ndltodp(UINT *d,DP *r);
1.1       noro      109: NODE reverse_node(NODE n);
                    110: P ndc_div(int mod,union oNDC a,union oNDC b);
                    111: P ndctop(int mod,union oNDC c);
                    112: void finalize_tracelist(int i,P cont);
                    113: void conv_ilist(int demand,int trace,NODE g,int **indp);
1.46      noro      114: void parse_nd_option(VL vl,NODE opt);
1.1       noro      115: void dltondl(int n,DL dl,UINT *r);
                    116: DP ndvtodp(int mod,NDV p);
                    117: DP ndtodp(int mod,ND p);
1.16      noro      118: DPM ndvtodpm(int mod,NDV p);
1.43      noro      119: NDV dptondv(int mod,DP p);
1.16      noro      120: NDV dpmtondv(int mod,DPM p);
1.43      noro      121: int dp_getdeg(DP p);
1.16      noro      122: int dpm_getdeg(DPM p,int *rank);
                    123: void dpm_ptozp(DPM p,Z *cont,DPM *r);
                    124: int compdmm(int nv,DMM a,DMM b);
1.44      noro      125: DPM sigtodpm(SIG s);
                    126: SIG dup_sig(SIG sig);
1.1       noro      127:
                    128: void Pdp_set_weight(NODE,VECT *);
                    129: void Pox_cmo_rpc(NODE,Obj *);
                    130:
                    131: ND nd_add_lf(ND p1,ND p2);
                    132: void nd_mul_c_lf(ND p,Z mul);
                    133: void ndv_mul_c_lf(NDV p,Z mul);
                    134: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                    135:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
                    136: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                    137:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
                    138: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                    139:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
                    140: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
1.40      noro      141: 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      142: NODE nd_f4_lf_trace_main(int m,int **indp);
                    143: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
                    144:
                    145: extern int lf_lazy;
                    146: extern Z current_mod_lf;
                    147:
                    148: extern int Denominator,DP_Multiple,MaxDeg;
                    149:
                    150: #define BLEN (8*sizeof(unsigned long))
                    151:
                    152: typedef struct matrix {
                    153:   int row,col;
                    154:   unsigned long **a;
                    155: } *matrix;
                    156:
                    157:
                    158: void nd_free_private_storage()
                    159: {
                    160:     _nm_free_list = 0;
                    161:     _ndp_free_list = 0;
                    162: #if 0
                    163:     GC_gcollect();
                    164: #endif
                    165: }
                    166:
                    167: void _NM_alloc()
                    168: {
                    169:     NM p;
                    170:     int i;
                    171:
                    172:     for ( i = 0; i < 1024; i++ ) {
                    173:         p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                    174:         p->next = _nm_free_list; _nm_free_list = p;
                    175:     }
                    176: }
                    177:
                    178: matrix alloc_matrix(int row,int col)
                    179: {
                    180:   unsigned long **a;
                    181:   int i,len,blen;
                    182:   matrix mat;
                    183:
                    184:   mat = (matrix)MALLOC(sizeof(struct matrix));
                    185:   mat->row = row;
                    186:   mat->col = col;
                    187:   mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
                    188:   return mat;
                    189: }
                    190:
                    191:
                    192: void _ND_alloc()
                    193: {
                    194:     ND p;
                    195:     int i;
                    196:
                    197:     for ( i = 0; i < 1024; i++ ) {
                    198:         p = (ND)MALLOC(sizeof(struct oND));
                    199:         p->body = (NM)_nd_free_list; _nd_free_list = p;
                    200:     }
                    201: }
                    202:
                    203: void _NDP_alloc()
                    204: {
                    205:     ND_pairs p;
                    206:     int i;
                    207:
                    208:     for ( i = 0; i < 1024; i++ ) {
                    209:         p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
                    210:             +(nd_wpd-1)*sizeof(UINT));
                    211:         p->next = _ndp_free_list; _ndp_free_list = p;
                    212:     }
                    213: }
                    214:
                    215: INLINE int nd_length(ND p)
                    216: {
                    217:     NM m;
                    218:     int i;
                    219:
                    220:     if ( !p )
                    221:         return 0;
                    222:     else {
                    223:         for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
                    224:         return i;
                    225:     }
                    226: }
                    227:
                    228: extern int dp_negative_weight;
                    229:
                    230: INLINE int ndl_reducible(UINT *d1,UINT *d2)
                    231: {
                    232:     UINT u1,u2;
                    233:     int i,j;
                    234:
                    235:     if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
                    236:
                    237:     if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
                    238: #if USE_UNROLL
                    239:     switch ( nd_bpe ) {
                    240:         case 3:
                    241:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    242:                 u1 = d1[i]; u2 = d2[i];
                    243:                 if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
                    244:                 if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
                    245:                 if ( (u1&  0xe00000) < (u2&  0xe00000) ) return 0;
                    246:                 if ( (u1&  0x1c0000) < (u2&  0x1c0000) ) return 0;
                    247:                 if ( (u1&   0x38000) < (u2&   0x38000) ) return 0;
                    248:                 if ( (u1&    0x7000) < (u2&    0x7000) ) return 0;
                    249:                 if ( (u1&     0xe00) < (u2&     0xe00) ) return 0;
                    250:                 if ( (u1&     0x1c0) < (u2&     0x1c0) ) return 0;
                    251:                 if ( (u1&      0x38) < (u2&      0x38) ) return 0;
                    252:                 if ( (u1&       0x7) < (u2&       0x7) ) return 0;
                    253:             }
                    254:             return 1;
                    255:             break;
                    256:         case 4:
                    257:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    258:                 u1 = d1[i]; u2 = d2[i];
                    259:                 if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
                    260:                 if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
                    261:                 if ( (u1&  0xf00000) < (u2&  0xf00000) ) return 0;
                    262:                 if ( (u1&   0xf0000) < (u2&   0xf0000) ) return 0;
                    263:                 if ( (u1&    0xf000) < (u2&    0xf000) ) return 0;
                    264:                 if ( (u1&     0xf00) < (u2&     0xf00) ) return 0;
                    265:                 if ( (u1&      0xf0) < (u2&      0xf0) ) return 0;
                    266:                 if ( (u1&       0xf) < (u2&       0xf) ) return 0;
                    267:             }
                    268:             return 1;
                    269:             break;
                    270:         case 6:
                    271:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    272:                 u1 = d1[i]; u2 = d2[i];
                    273:                 if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
                    274:                 if ( (u1&  0xfc0000) < (u2&  0xfc0000) ) return 0;
                    275:                 if ( (u1&   0x3f000) < (u2&   0x3f000) ) return 0;
                    276:                 if ( (u1&     0xfc0) < (u2&     0xfc0) ) return 0;
                    277:                 if ( (u1&      0x3f) < (u2&      0x3f) ) return 0;
                    278:             }
                    279:             return 1;
                    280:             break;
                    281:         case 8:
                    282:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    283:                 u1 = d1[i]; u2 = d2[i];
                    284:                 if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
                    285:                 if ( (u1&  0xff0000) < (u2&  0xff0000) ) return 0;
                    286:                 if ( (u1&    0xff00) < (u2&    0xff00) ) return 0;
                    287:                 if ( (u1&      0xff) < (u2&      0xff) ) return 0;
                    288:             }
                    289:             return 1;
                    290:             break;
                    291:         case 16:
                    292:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    293:                 u1 = d1[i]; u2 = d2[i];
                    294:                 if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
                    295:                 if ( (u1&    0xffff) < (u2&    0xffff) ) return 0;
                    296:             }
                    297:             return 1;
                    298:             break;
                    299:         case 32:
                    300:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                    301:                 if ( d1[i] < d2[i] ) return 0;
                    302:             return 1;
                    303:             break;
                    304:         default:
                    305:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    306:                 u1 = d1[i]; u2 = d2[i];
                    307:                 for ( j = 0; j < nd_epw; j++ )
                    308:                     if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    309:             }
                    310:             return 1;
                    311:     }
                    312: #else
                    313:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    314:         u1 = d1[i]; u2 = d2[i];
                    315:         for ( j = 0; j < nd_epw; j++ )
                    316:             if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    317:     }
                    318:     return 1;
                    319: #endif
                    320: }
                    321:
                    322: /*
                    323:  * If the current order is a block order,
                    324:  * then the last block is length 1 and contains
                    325:  * the homo variable. Otherwise, the original
                    326:  * order is either 0 or 2.
                    327:  */
                    328:
                    329: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
                    330: {
                    331:     int w,i,e,n,omask0;
                    332:
                    333:     omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
                    334:     n = nd_nvar-1;
                    335:     ndl_zero(r);
                    336:     for ( i = 0; i < n; i++ ) {
                    337:         e = GET_EXP_OLD(d,i);
                    338:         PUT_EXP(r,i,e);
                    339:     }
                    340:     w = TD(d);
                    341:     PUT_EXP(r,nd_nvar-1,weight-w);
                    342:     if ( nd_module ) MPOS(r) = d[ompos];
                    343:     TD(r) = weight;
                    344:     if ( nd_blockmask ) ndl_weight_mask(r);
                    345: }
                    346:
                    347: void ndl_dehomogenize(UINT *d)
                    348: {
                    349:     UINT mask;
                    350:     UINT h;
                    351:     int i,bits;
                    352:
                    353:     if ( nd_blockmask ) {
                    354:         h = GET_EXP(d,nd_nvar-1);
                    355:         XOR_EXP(d,nd_nvar-1,h);
                    356:         TD(d) -= h;
                    357:         ndl_weight_mask(d);
                    358:     } else {
                    359:         if ( nd_isrlex ) {
                    360:             if ( nd_bpe == 32 ) {
                    361:                 h = d[nd_exporigin];
                    362:                 for ( i = nd_exporigin+1; i < nd_wpd; i++ )
                    363:                     d[i-1] = d[i];
                    364:                 d[i-1] = 0;
                    365:                 TD(d) -= h;
                    366:             } else {
                    367:                 bits = nd_epw*nd_bpe;
                    368:                 mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
                    369:                 h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
                    370:                 for ( i = nd_exporigin; i < nd_wpd; i++ )
                    371:                     d[i] = ((d[i]<<nd_bpe)&mask)
                    372:                         |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
                    373:                 TD(d) -= h;
                    374:             }
                    375:         } else {
                    376:             h = GET_EXP(d,nd_nvar-1);
                    377:             XOR_EXP(d,nd_nvar-1,h);
                    378:             TD(d) -= h;
                    379:         }
                    380:     }
                    381: }
                    382:
                    383: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
                    384: {
                    385:     UINT t1,t2,u,u1,u2;
                    386:     int i,j,l;
                    387:
                    388:     if ( nd_module && (MPOS(d1) != MPOS(d2)) )
                    389:         error("ndl_lcm : inconsistent monomials");
                    390: #if USE_UNROLL
                    391:     switch ( nd_bpe ) {
                    392:         case 3:
                    393:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    394:                 u1 = d1[i]; u2 = d2[i];
                    395:                 t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
                    396:                 t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
                    397:                 t1 = (u1&  0xe00000); t2 = (u2&  0xe00000); u |= t1>t2?t1:t2;
                    398:                 t1 = (u1&  0x1c0000); t2 = (u2&  0x1c0000); u |= t1>t2?t1:t2;
                    399:                 t1 = (u1&   0x38000); t2 = (u2&   0x38000); u |= t1>t2?t1:t2;
                    400:                 t1 = (u1&    0x7000); t2 = (u2&    0x7000); u |= t1>t2?t1:t2;
                    401:                 t1 = (u1&     0xe00); t2 = (u2&     0xe00); u |= t1>t2?t1:t2;
                    402:                 t1 = (u1&     0x1c0); t2 = (u2&     0x1c0); u |= t1>t2?t1:t2;
                    403:                 t1 = (u1&      0x38); t2 = (u2&      0x38); u |= t1>t2?t1:t2;
                    404:                 t1 = (u1&       0x7); t2 = (u2&       0x7); u |= t1>t2?t1:t2;
                    405:                 d[i] = u;
                    406:             }
                    407:             break;
                    408:         case 4:
                    409:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    410:                 u1 = d1[i]; u2 = d2[i];
                    411:                 t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
                    412:                 t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
                    413:                 t1 = (u1&  0xf00000); t2 = (u2&  0xf00000); u |= t1>t2?t1:t2;
                    414:                 t1 = (u1&   0xf0000); t2 = (u2&   0xf0000); u |= t1>t2?t1:t2;
                    415:                 t1 = (u1&    0xf000); t2 = (u2&    0xf000); u |= t1>t2?t1:t2;
                    416:                 t1 = (u1&     0xf00); t2 = (u2&     0xf00); u |= t1>t2?t1:t2;
                    417:                 t1 = (u1&      0xf0); t2 = (u2&      0xf0); u |= t1>t2?t1:t2;
                    418:                 t1 = (u1&       0xf); t2 = (u2&       0xf); u |= t1>t2?t1:t2;
                    419:                 d[i] = u;
                    420:             }
                    421:             break;
                    422:         case 6:
                    423:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    424:                 u1 = d1[i]; u2 = d2[i];
                    425:                 t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
                    426:                 t1 = (u1&  0xfc0000); t2 = (u2&  0xfc0000); u |= t1>t2?t1:t2;
                    427:                 t1 = (u1&   0x3f000); t2 = (u2&   0x3f000); u |= t1>t2?t1:t2;
                    428:                 t1 = (u1&     0xfc0); t2 = (u2&     0xfc0); u |= t1>t2?t1:t2;
                    429:                 t1 = (u1&      0x3f); t2 = (u2&      0x3f); u |= t1>t2?t1:t2;
                    430:                 d[i] = u;
                    431:             }
                    432:             break;
                    433:         case 8:
                    434:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    435:                 u1 = d1[i]; u2 = d2[i];
                    436:                 t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
                    437:                 t1 = (u1&  0xff0000); t2 = (u2&  0xff0000); u |= t1>t2?t1:t2;
                    438:                 t1 = (u1&    0xff00); t2 = (u2&    0xff00); u |= t1>t2?t1:t2;
                    439:                 t1 = (u1&      0xff); t2 = (u2&      0xff); u |= t1>t2?t1:t2;
                    440:                 d[i] = u;
                    441:             }
                    442:             break;
                    443:         case 16:
                    444:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    445:                 u1 = d1[i]; u2 = d2[i];
                    446:                 t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
                    447:                 t1 = (u1&    0xffff); t2 = (u2&    0xffff); u |= t1>t2?t1:t2;
                    448:                 d[i] = u;
                    449:             }
                    450:             break;
                    451:         case 32:
                    452:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    453:                 u1 = d1[i]; u2 = d2[i];
                    454:                 d[i] = u1>u2?u1:u2;
                    455:             }
                    456:             break;
                    457:         default:
                    458:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    459:                 u1 = d1[i]; u2 = d2[i];
                    460:                 for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    461:                     t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    462:                 }
                    463:                 d[i] = u;
                    464:             }
                    465:             break;
                    466:     }
                    467: #else
                    468:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    469:         u1 = d1[i]; u2 = d2[i];
                    470:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    471:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    472:         }
                    473:         d[i] = u;
                    474:     }
                    475: #endif
                    476:     if ( nd_module ) MPOS(d) = MPOS(d1);
                    477:     TD(d) = ndl_weight(d);
                    478:     if ( nd_blockmask ) ndl_weight_mask(d);
                    479: }
                    480:
                    481: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
                    482: {
                    483:     UINT t1,t2,u,u1,u2;
                    484:     int i,j,l;
                    485:
                    486:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    487:         u1 = d1[i]; u2 = d2[i];
                    488:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    489:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    490:         }
                    491:         d[i] = u;
                    492:     }
                    493: }
                    494:
                    495: int ndl_weight(UINT *d)
                    496: {
                    497:     UINT t,u;
                    498:     int i,j;
                    499:
                    500:     if ( current_dl_weight_vector )
                    501:         for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    502:             u = GET_EXP(d,i);
                    503:             t += MUL_WEIGHT(u,i);
                    504:         }
                    505:     else
                    506:         for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    507:             u = d[i];
                    508:             for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    509:                 t += (u&nd_mask0);
                    510:         }
1.20      noro      511:     if ( nd_module && nd_module_rank && MPOS(d) )
                    512:         t += nd_module_weight[MPOS(d)-1];
                    513:     for ( i = nd_exporigin; i < nd_wpd; i++ )
                    514:       if ( d[i] && !t )
                    515:         printf("afo\n");
1.1       noro      516:     return t;
                    517: }
                    518:
                    519: /* for sugarweight */
                    520:
                    521: int ndl_weight2(UINT *d)
                    522: {
                    523:     int t,u;
                    524:     int i,j;
                    525:
                    526:     for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    527:         u = GET_EXP(d,i);
                    528:         t += nd_sugarweight[i]*u;
                    529:     }
1.20      noro      530:     if ( nd_module && nd_module_rank && MPOS(d) )
                    531:         t += nd_module_weight[MPOS(d)-1];
1.1       noro      532:     return t;
                    533: }
                    534:
                    535: void ndl_weight_mask(UINT *d)
                    536: {
                    537:     UINT t,u;
                    538:     UINT *mask;
                    539:     int i,j,k,l;
                    540:
                    541:     l = nd_blockmask->n;
                    542:     for ( k = 0; k < l; k++ ) {
                    543:         mask = nd_blockmask->mask[k];
                    544:         if ( current_dl_weight_vector )
                    545:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    546:                 u = GET_EXP_MASK(d,i,mask);
                    547:                 t += MUL_WEIGHT(u,i);
                    548:             }
                    549:         else
                    550:             for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    551:                 u = d[i]&mask[i];
                    552:                 for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    553:                     t += (u&nd_mask0);
                    554:             }
                    555:         d[k+1] = t;
                    556:     }
                    557: }
                    558:
1.21      noro      559: int ndl_glex_compare(UINT *d1,UINT *d2)
                    560: {
                    561:   if ( TD(d1) > TD(d2) ) return 1;
                    562:   else if ( TD(d1) < TD(d2) ) return -1;
                    563:   else return ndl_lex_compare(d1,d2);
                    564: }
                    565:
1.1       noro      566: int ndl_lex_compare(UINT *d1,UINT *d2)
                    567: {
                    568:     int i;
                    569:
                    570:     d1 += nd_exporigin;
                    571:     d2 += nd_exporigin;
                    572:     for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
                    573:         if ( *d1 > *d2 )
                    574:             return nd_isrlex ? -1 : 1;
                    575:         else if ( *d1 < *d2 )
                    576:             return nd_isrlex ? 1 : -1;
                    577:     return 0;
                    578: }
                    579:
                    580: int ndl_block_compare(UINT *d1,UINT *d2)
                    581: {
                    582:     int i,l,j,ord_o,ord_l;
                    583:     struct order_pair *op;
                    584:     UINT t1,t2,m;
                    585:     UINT *mask;
                    586:
                    587:     l = nd_blockmask->n;
                    588:     op = nd_blockmask->order_pair;
                    589:     for ( j = 0; j < l; j++ ) {
                    590:         mask = nd_blockmask->mask[j];
                    591:         ord_o = op[j].order;
                    592:         if ( ord_o < 2 ) {
                    593:             if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
                    594:             else if ( t1 < t2 ) return -1;
                    595:         }
                    596:         for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    597:             m = mask[i];
                    598:             t1 = d1[i]&m;
                    599:             t2 = d2[i]&m;
                    600:             if ( t1 > t2 )
                    601:                 return !ord_o ? -1 : 1;
                    602:             else if ( t1 < t2 )
                    603:                 return !ord_o ? 1 : -1;
                    604:         }
                    605:     }
                    606:     return 0;
                    607: }
                    608:
                    609: int ndl_matrix_compare(UINT *d1,UINT *d2)
                    610: {
                    611:   int i,j,s,row;
                    612:   int *v;
                    613:   Z **mat;
                    614:   Z *w;
                    615:   Z t1;
                    616:   Z t,t2;
                    617:
1.6       noro      618:   for ( j = 0; j < nd_nvar; j++ )
                    619:     nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.1       noro      620:   if ( nd_top_weight ) {
                    621:     if ( OID(nd_top_weight) == O_VECT ) {
1.6       noro      622:       mat = (Z **)&BDY((VECT)nd_top_weight);
                    623:       row = 1;
1.1       noro      624:     } else {
                    625:       mat = (Z **)BDY((MAT)nd_top_weight);
1.6       noro      626:       row = ((MAT)nd_top_weight)->row;
1.1       noro      627:     }
                    628:     for ( i = 0; i < row; i++ ) {
1.6       noro      629:       w = mat[i];
1.1       noro      630:       for ( j = 0, t = 0; j < nd_nvar; j++ ) {
1.6       noro      631:         STOZ(nd_work_vector[j],t1);
1.1       noro      632:         mulz(w[j],t1,&t2);
                    633:         addz(t,t2,&t1);
                    634:         t = t1;
                    635:       }
1.6       noro      636:       if ( t ) {
                    637:         s = sgnz(t);
1.1       noro      638:         if ( s > 0 ) return 1;
                    639:         else if ( s < 0 ) return -1;
1.6       noro      640:       }
1.1       noro      641:     }
1.6       noro      642:   }
                    643:   for ( i = 0; i < nd_matrix_len; i++ ) {
                    644:     v = nd_matrix[i];
                    645:     for ( j = 0, s = 0; j < nd_nvar; j++ )
                    646:       s += v[j]*nd_work_vector[j];
                    647:     if ( s > 0 ) return 1;
                    648:     else if ( s < 0 ) return -1;
                    649:   }
1.1       noro      650:   if ( !ndl_equal(d1,d2) )
1.6       noro      651:     error("ndl_matrix_compare : invalid matrix");
                    652:   return 0;
1.1       noro      653: }
                    654:
                    655: int ndl_composite_compare(UINT *d1,UINT *d2)
                    656: {
                    657:     int i,j,s,start,end,len,o;
                    658:     int *v;
                    659:     struct sparse_weight *sw;
                    660:
                    661:     for ( j = 0; j < nd_nvar; j++ )
                    662:         nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
                    663:     for ( i = 0; i < nd_worb_len; i++ ) {
                    664:         len = nd_worb[i].length;
                    665:         switch ( nd_worb[i].type ) {
                    666:             case IS_DENSE_WEIGHT:
                    667:                 v = nd_worb[i].body.dense_weight;
                    668:                 for ( j = 0, s = 0; j < len; j++ )
                    669:                     s += v[j]*nd_work_vector[j];
                    670:                 if ( s > 0 ) return 1;
                    671:                 else if ( s < 0 ) return -1;
                    672:                 break;
                    673:             case IS_SPARSE_WEIGHT:
                    674:                 sw = nd_worb[i].body.sparse_weight;
                    675:                 for ( j = 0, s = 0; j < len; j++ )
                    676:                     s += sw[j].value*nd_work_vector[sw[j].pos];
                    677:                 if ( s > 0 ) return 1;
                    678:                 else if ( s < 0 ) return -1;
                    679:                 break;
                    680:             case IS_BLOCK:
                    681:                 o = nd_worb[i].body.block.order;
                    682:                 start = nd_worb[i].body.block.start;
                    683:                 switch ( o ) {
                    684:                     case 0:
                    685:                         end = start+len;
                    686:                         for ( j = start, s = 0; j < end; j++ )
                    687:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    688:                         if ( s > 0 ) return 1;
                    689:                         else if ( s < 0 ) return -1;
                    690:                         for ( j = end-1; j >= start; j-- )
                    691:                             if ( nd_work_vector[j] < 0 ) return 1;
                    692:                             else if ( nd_work_vector[j] > 0 ) return -1;
                    693:                         break;
                    694:                     case 1:
                    695:                         end = start+len;
                    696:                         for ( j = start, s = 0; j < end; j++ )
                    697:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    698:                         if ( s > 0 ) return 1;
                    699:                         else if ( s < 0 ) return -1;
                    700:                         for ( j = start; j < end; j++ )
                    701:                             if ( nd_work_vector[j] > 0 ) return 1;
                    702:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    703:                         break;
                    704:                     case 2:
                    705:                         end = start+len;
                    706:                         for ( j = start; j < end; j++ )
                    707:                             if ( nd_work_vector[j] > 0 ) return 1;
                    708:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    709:                         break;
                    710:                 }
                    711:                 break;
                    712:         }
                    713:     }
                    714:     return 0;
                    715: }
                    716:
                    717: /* TDH -> WW -> TD-> RL */
                    718:
                    719: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
                    720: {
                    721:     int i,m,e1,e2;
                    722:
                    723:     if ( TD(d1) > TD(d2) ) return 1;
                    724:     else if ( TD(d1) < TD(d2) ) return -1;
                    725:     m = nd_nvar>>1;
                    726:     for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
                    727:         e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
                    728:         e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
                    729:     }
                    730:     if ( e1 > e2 ) return 1;
                    731:     else if ( e1 < e2 ) return -1;
                    732:     return ndl_lex_compare(d1,d2);
                    733: }
                    734:
1.21      noro      735: // common function for module glex and grlex comparison
                    736: int ndl_module_glex_compare(UINT *d1,UINT *d2)
1.1       noro      737: {
1.21      noro      738:   int c;
1.1       noro      739:
1.21      noro      740:   switch ( nd_module_ordtype ) {
                    741:     case 0:
                    742:       if ( TD(d1) > TD(d2) ) return 1;
                    743:       else if ( TD(d1) < TD(d2) ) return -1;
                    744:       else if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
                    745:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    746:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    747:       else return 0;
                    748:       break;
1.1       noro      749:
1.21      noro      750:     case 1:
1.19      noro      751:       if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
                    752:          if ( TD(d1) > TD(d2) ) return 1;
                    753:          else if ( TD(d1) < TD(d2) ) return -1;
                    754:          if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
                    755:          if ( MPOS(d1) < MPOS(d2) ) return 1;
                    756:          else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    757:       }
                    758:       if ( MPOS(d1) < MPOS(d2) ) return 1;
                    759:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
1.21      noro      760:       else if ( TD(d1) > TD(d2) ) return 1;
                    761:       else if ( TD(d1) < TD(d2) ) return -1;
                    762:       else return ndl_lex_compare(d1,d2);
                    763:       break;
1.1       noro      764:
1.21      noro      765:     case 2: // weight -> POT
                    766:       if ( TD(d1) > TD(d2) ) return 1;
                    767:       else if ( TD(d1) < TD(d2) ) return -1;
                    768:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    769:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    770:       else return ndl_lex_compare(d1,d2);
                    771:       break;
1.1       noro      772:
1.21      noro      773:     default:
                    774:       error("ndl_module_glex_compare : invalid module_ordtype");
1.28      noro      775:       return 0;
1.21      noro      776:   }
1.1       noro      777: }
                    778:
1.21      noro      779: // common  for module comparison
                    780: int ndl_module_compare(UINT *d1,UINT *d2)
1.1       noro      781: {
1.21      noro      782:   int c;
1.1       noro      783:
1.21      noro      784:   switch ( nd_module_ordtype ) {
                    785:     case 0:
1.23      noro      786:       if ( (c = (*ndl_base_compare_function)(d1,d2)) != 0 ) return c;
1.21      noro      787:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    788:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    789:       else return 0;
                    790:       break;
1.1       noro      791:
1.21      noro      792:     case 1:
                    793:       if ( MPOS(d1) < MPOS(d2) ) return 1;
                    794:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    795:       else return (*ndl_base_compare_function)(d1,d2);
                    796:       break;
1.1       noro      797:
1.21      noro      798:     case 2: // weight -> POT
                    799:       if ( TD(d1) > TD(d2) ) return 1;
                    800:       else if ( TD(d1) < TD(d2) ) return -1;
                    801:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    802:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    803:       else return (*ndl_base_compare_function)(d1,d2);
                    804:       break;
1.1       noro      805:
1.21      noro      806:     default:
                    807:       error("ndl_module_compare : invalid module_ordtype");
1.28      noro      808:       return 0;
1.21      noro      809:   }
1.1       noro      810: }
                    811:
1.21      noro      812: extern DMMstack dmm_stack;
                    813: void _addtodl(int n,DL d1,DL d2);
1.31      noro      814: void _adddl(int n,DL d1,DL d2,DL d3);
1.21      noro      815: int _eqdl(int n,DL d1,DL d2);
                    816:
                    817: int ndl_module_schreyer_compare(UINT *m1,UINT *m2)
                    818: {
1.44      noro      819:   int pos1,pos2,t,j,retpot;
1.21      noro      820:   DMM *in;
                    821:   DMMstack s;
                    822:   static DL d1=0;
                    823:   static DL d2=0;
                    824:   static int dlen=0;
1.44      noro      825:   extern int ReversePOT;
1.21      noro      826:
1.44      noro      827:   if ( ReversePOT ) retpot = -1;
                    828:   else retpot = 1;
1.21      noro      829:   pos1 = MPOS(m1); pos2 = MPOS(m2);
                    830:   if ( pos1 == pos2 ) return (*ndl_base_compare_function)(m1,m2);
                    831:   if ( nd_nvar > dlen ) {
                    832:     NEWDL(d1,nd_nvar);
                    833:     NEWDL(d2,nd_nvar);
                    834:     dlen = nd_nvar;
                    835:   }
                    836:   d1->td = TD(m1);
                    837:   for ( j = 0; j < nd_nvar; j++ ) d1->d[j] = GET_EXP(m1,j);
                    838:   d2->td = TD(m2);
                    839:   for ( j = 0; j < nd_nvar; j++ ) d2->d[j] = GET_EXP(m2,j);
                    840:   for ( s = dmm_stack; s; s = NEXT(s) ) {
                    841:     in = s->in;
                    842:     _addtodl(nd_nvar,in[pos1]->dl,d1);
                    843:     _addtodl(nd_nvar,in[pos2]->dl,d2);
                    844:     if ( in[pos1]->pos == in[pos2]->pos && _eqdl(nd_nvar,d1,d2)) {
1.44      noro      845:       if ( pos1 < pos2 ) return retpot;
                    846:       else if ( pos1 > pos2 ) return -retpot;
1.21      noro      847:       else return 0;
                    848:     }
                    849:     pos1 = in[pos1]->pos;
                    850:     pos2 = in[pos2]->pos;
                    851:     if ( pos1 == pos2 ) return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    852:   }
                    853:   // comparison by the bottom order
                    854: LAST:
                    855:   switch ( nd_base_ordtype ) {
                    856:     case 0:
                    857:       t = (*dl_base_compare_function)(nd_nvar,d1,d2);
                    858:       if ( t ) return t;
1.44      noro      859:       else if ( pos1 < pos2 ) return retpot;
                    860:       else if ( pos1 > pos2 ) return -retpot;
1.21      noro      861:       else return 0;
                    862:       break;
                    863:     case 1:
1.44      noro      864:       if ( pos1 < pos2 ) return retpot;
                    865:       else if ( pos1 > pos2 ) return -retpot;
1.21      noro      866:       else return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    867:       break;
                    868:     case 2:
                    869:       if ( d1->td > d2->td  ) return 1;
                    870:       else if ( d1->td < d2->td ) return -1;
1.44      noro      871:       else if ( pos1 < pos2 ) return retpot;
                    872:       else if ( pos1 > pos2 ) return -retpot;
1.21      noro      873:       else return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    874:       break;
                    875:     default:
                    876:       error("ndl_schreyer_compare : invalid base ordtype");
1.28      noro      877:       return 0;
1.21      noro      878:   }
1.1       noro      879: }
                    880:
                    881: INLINE int ndl_equal(UINT *d1,UINT *d2)
                    882: {
                    883:     int i;
                    884:
                    885:     switch ( nd_wpd ) {
                    886:         case 2:
                    887:             if ( TD(d2) != TD(d1) ) return 0;
                    888:             if ( d2[1] != d1[1] ) return 0;
                    889:             return 1;
                    890:             break;
                    891:         case 3:
                    892:             if ( TD(d2) != TD(d1) ) return 0;
                    893:             if ( d2[1] != d1[1] ) return 0;
                    894:             if ( d2[2] != d1[2] ) return 0;
                    895:             return 1;
                    896:             break;
                    897:         default:
                    898:             for ( i = 0; i < nd_wpd; i++ )
                    899:                 if ( *d1++ != *d2++ ) return 0;
                    900:             return 1;
                    901:             break;
                    902:     }
                    903: }
                    904:
                    905: INLINE void ndl_copy(UINT *d1,UINT *d2)
                    906: {
                    907:     int i;
                    908:
                    909:     switch ( nd_wpd ) {
                    910:         case 2:
                    911:             TD(d2) = TD(d1);
                    912:             d2[1] = d1[1];
                    913:             break;
                    914:         case 3:
                    915:             TD(d2) = TD(d1);
                    916:             d2[1] = d1[1];
                    917:             d2[2] = d1[2];
                    918:             break;
                    919:         default:
                    920:             for ( i = 0; i < nd_wpd; i++ )
                    921:                 d2[i] = d1[i];
                    922:             break;
                    923:     }
                    924: }
                    925:
                    926: INLINE void ndl_zero(UINT *d)
                    927: {
                    928:     int i;
                    929:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                    930: }
                    931:
                    932: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
                    933: {
                    934:     int i;
                    935:
                    936:     if ( nd_module ) {
                    937:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
                    938:         error("ndl_add : invalid operation");
                    939:     }
                    940: #if 1
                    941:     switch ( nd_wpd ) {
                    942:         case 2:
                    943:             TD(d) = TD(d1)+TD(d2);
                    944:             d[1] = d1[1]+d2[1];
                    945:             break;
                    946:         case 3:
                    947:             TD(d) = TD(d1)+TD(d2);
                    948:             d[1] = d1[1]+d2[1];
                    949:             d[2] = d1[2]+d2[2];
                    950:             break;
                    951:         default:
                    952:             for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    953:             break;
                    954:     }
                    955: #else
                    956:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    957: #endif
                    958: }
                    959:
                    960: /* d1 += d2 */
                    961: INLINE void ndl_addto(UINT *d1,UINT *d2)
                    962: {
                    963:     int i;
                    964:
                    965:     if ( nd_module ) {
                    966:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
                    967:             error("ndl_addto : invalid operation");
                    968:     }
                    969: #if 1
                    970:     switch ( nd_wpd ) {
                    971:         case 2:
                    972:             TD(d1) += TD(d2);
                    973:             d1[1] += d2[1];
                    974:             break;
                    975:         case 3:
                    976:             TD(d1) += TD(d2);
                    977:             d1[1] += d2[1];
                    978:             d1[2] += d2[2];
                    979:             break;
                    980:         default:
                    981:             for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    982:             break;
                    983:     }
                    984: #else
                    985:     for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    986: #endif
                    987: }
                    988:
                    989: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
                    990: {
                    991:     int i;
                    992:
                    993:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
                    994: }
                    995:
                    996: int ndl_disjoint(UINT *d1,UINT *d2)
                    997: {
                    998:     UINT t1,t2,u,u1,u2;
                    999:     int i,j;
                   1000:
                   1001:     if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
                   1002: #if USE_UNROLL
                   1003:     switch ( nd_bpe ) {
                   1004:         case 3:
                   1005:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1006:                 u1 = d1[i]; u2 = d2[i];
                   1007:                 t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
                   1008:                 t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
                   1009:                 t1 = u1&  0xe00000; t2 = u2&  0xe00000; if ( t1&&t2 ) return 0;
                   1010:                 t1 = u1&  0x1c0000; t2 = u2&  0x1c0000; if ( t1&&t2 ) return 0;
                   1011:                 t1 = u1&   0x38000; t2 = u2&   0x38000; if ( t1&&t2 ) return 0;
                   1012:                 t1 = u1&    0x7000; t2 = u2&    0x7000; if ( t1&&t2 ) return 0;
                   1013:                 t1 = u1&     0xe00; t2 = u2&     0xe00; if ( t1&&t2 ) return 0;
                   1014:                 t1 = u1&     0x1c0; t2 = u2&     0x1c0; if ( t1&&t2 ) return 0;
                   1015:                 t1 = u1&      0x38; t2 = u2&      0x38; if ( t1&&t2 ) return 0;
                   1016:                 t1 = u1&       0x7; t2 = u2&       0x7; if ( t1&&t2 ) return 0;
                   1017:             }
                   1018:             return 1;
                   1019:             break;
                   1020:         case 4:
                   1021:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1022:                 u1 = d1[i]; u2 = d2[i];
                   1023:                 t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
                   1024:                 t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
                   1025:                 t1 = u1&  0xf00000; t2 = u2&  0xf00000; if ( t1&&t2 ) return 0;
                   1026:                 t1 = u1&   0xf0000; t2 = u2&   0xf0000; if ( t1&&t2 ) return 0;
                   1027:                 t1 = u1&    0xf000; t2 = u2&    0xf000; if ( t1&&t2 ) return 0;
                   1028:                 t1 = u1&     0xf00; t2 = u2&     0xf00; if ( t1&&t2 ) return 0;
                   1029:                 t1 = u1&      0xf0; t2 = u2&      0xf0; if ( t1&&t2 ) return 0;
                   1030:                 t1 = u1&       0xf; t2 = u2&       0xf; if ( t1&&t2 ) return 0;
                   1031:             }
                   1032:             return 1;
                   1033:             break;
                   1034:         case 6:
                   1035:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1036:                 u1 = d1[i]; u2 = d2[i];
                   1037:                 t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
                   1038:                 t1 = u1&  0xfc0000; t2 = u2&  0xfc0000; if ( t1&&t2 ) return 0;
                   1039:                 t1 = u1&   0x3f000; t2 = u2&   0x3f000; if ( t1&&t2 ) return 0;
                   1040:                 t1 = u1&     0xfc0; t2 = u2&     0xfc0; if ( t1&&t2 ) return 0;
                   1041:                 t1 = u1&      0x3f; t2 = u2&      0x3f; if ( t1&&t2 ) return 0;
                   1042:             }
                   1043:             return 1;
                   1044:             break;
                   1045:         case 8:
                   1046:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1047:                 u1 = d1[i]; u2 = d2[i];
                   1048:                 t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
                   1049:                 t1 = u1&  0xff0000; t2 = u2&  0xff0000; if ( t1&&t2 ) return 0;
                   1050:                 t1 = u1&    0xff00; t2 = u2&    0xff00; if ( t1&&t2 ) return 0;
                   1051:                 t1 = u1&      0xff; t2 = u2&      0xff; if ( t1&&t2 ) return 0;
                   1052:             }
                   1053:             return 1;
                   1054:             break;
                   1055:         case 16:
                   1056:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1057:                 u1 = d1[i]; u2 = d2[i];
                   1058:                 t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
                   1059:                 t1 = u1&    0xffff; t2 = u2&    0xffff; if ( t1&&t2 ) return 0;
                   1060:             }
                   1061:             return 1;
                   1062:             break;
                   1063:         case 32:
                   1064:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                   1065:                 if ( d1[i] && d2[i] ) return 0;
                   1066:             return 1;
                   1067:             break;
                   1068:         default:
                   1069:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1070:                 u1 = d1[i]; u2 = d2[i];
                   1071:                 for ( j = 0; j < nd_epw; j++ ) {
                   1072:                     if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                   1073:                     u1 >>= nd_bpe; u2 >>= nd_bpe;
                   1074:                 }
                   1075:             }
                   1076:             return 1;
                   1077:             break;
                   1078:     }
                   1079: #else
                   1080:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1081:         u1 = d1[i]; u2 = d2[i];
                   1082:         for ( j = 0; j < nd_epw; j++ ) {
                   1083:             if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                   1084:             u1 >>= nd_bpe; u2 >>= nd_bpe;
                   1085:         }
                   1086:     }
                   1087:     return 1;
                   1088: #endif
                   1089: }
                   1090:
                   1091: int ndl_check_bound(UINT *d1,UINT *d2)
                   1092: {
                   1093:     UINT u2;
                   1094:     int i,j,ind,k;
                   1095:
                   1096:     ind = 0;
                   1097: #if USE_UNROLL
                   1098:     switch ( nd_bpe ) {
                   1099:         case 3:
                   1100:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1101:                 u2 = d2[i];
                   1102:                 if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
                   1103:                 if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
                   1104:                 if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
                   1105:                 if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
                   1106:                 if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
                   1107:                 if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
                   1108:                 if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
                   1109:                 if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
                   1110:                 if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
                   1111:                 if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
                   1112:             }
                   1113:             return 0;
                   1114:             break;
                   1115:         case 4:
                   1116:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1117:                 u2 = d2[i];
                   1118:                 if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
                   1119:                 if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
                   1120:                 if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
                   1121:                 if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
                   1122:                 if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
                   1123:                 if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
                   1124:                 if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
                   1125:                 if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
                   1126:             }
                   1127:             return 0;
                   1128:             break;
                   1129:         case 6:
                   1130:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1131:                 u2 = d2[i];
                   1132:                 if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
                   1133:                 if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
                   1134:                 if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
                   1135:                 if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
                   1136:                 if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
                   1137:             }
                   1138:             return 0;
                   1139:             break;
                   1140:         case 8:
                   1141:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1142:                 u2 = d2[i];
                   1143:                 if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
                   1144:                 if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
                   1145:                 if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
                   1146:                 if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
                   1147:             }
                   1148:             return 0;
                   1149:             break;
                   1150:         case 16:
                   1151:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1152:                 u2 = d2[i];
                   1153:                 if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
                   1154:                 if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
                   1155:             }
                   1156:             return 0;
                   1157:             break;
                   1158:         case 32:
                   1159:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                   1160:                 if ( d1[i]+d2[i]<d1[i] ) return 1;
                   1161:             return 0;
                   1162:             break;
                   1163:         default:
                   1164:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1165:                 u2 = d2[i];
                   1166:                 k = (nd_epw-1)*nd_bpe;
                   1167:                 for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1168:                     if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1169:             }
                   1170:             return 0;
                   1171:             break;
                   1172:     }
                   1173: #else
                   1174:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1175:         u2 = d2[i];
                   1176:         k = (nd_epw-1)*nd_bpe;
                   1177:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1178:             if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1179:     }
                   1180:     return 0;
                   1181: #endif
                   1182: }
                   1183:
                   1184: int ndl_check_bound2(int index,UINT *d2)
                   1185: {
                   1186:     return ndl_check_bound(nd_bound[index],d2);
                   1187: }
                   1188:
                   1189: INLINE int ndl_hash_value(UINT *d)
                   1190: {
                   1191:     int i;
1.11      noro     1192:     UINT r;
1.1       noro     1193:
                   1194:     r = 0;
                   1195:     for ( i = 0; i < nd_wpd; i++ )
1.12      noro     1196:         r = (r*1511+d[i]);
1.11      noro     1197:     r %= REDTAB_LEN;
1.1       noro     1198:     return r;
                   1199: }
                   1200:
                   1201: INLINE int ndl_find_reducer(UINT *dg)
                   1202: {
                   1203:     RHist r;
                   1204:     int d,k,i;
                   1205:
                   1206:     d = ndl_hash_value(dg);
                   1207:     for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   1208:         if ( ndl_equal(dg,DL(r)) ) {
                   1209:             if ( k > 0 ) nd_notfirst++;
                   1210:             nd_found++;
                   1211:             return r->index;
                   1212:         }
                   1213:     }
                   1214:     if ( Reverse )
                   1215:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   1216:             r = nd_psh[i];
                   1217:             if ( ndl_reducible(dg,DL(r)) ) {
                   1218:                 nd_create++;
                   1219:                 nd_append_red(dg,i);
                   1220:                 return i;
                   1221:             }
                   1222:         }
                   1223:     else
                   1224:         for ( i = 0; i < nd_psn; i++ ) {
                   1225:             r = nd_psh[i];
                   1226:             if ( ndl_reducible(dg,DL(r)) ) {
                   1227:                 nd_create++;
                   1228:                 nd_append_red(dg,i);
                   1229:                 return i;
                   1230:             }
                   1231:         }
                   1232:     return -1;
                   1233: }
                   1234:
1.41      noro     1235: INLINE int ndl_find_reducer_nonsig(UINT *dg)
                   1236: {
                   1237:     RHist r;
                   1238:     int i;
                   1239:
                   1240:     for ( i = 0; i < nd_psn; i++ ) {
                   1241:       r = nd_psh[i];
                   1242:       if ( ndl_reducible(dg,DL(r)) ) return i;
                   1243:     }
                   1244:     return -1;
                   1245: }
                   1246:
1.24      noro     1247: // ret=0,...,nd_psn-1 => reducer found
                   1248: // ret=nd_psn => reducer not found
                   1249: // ret=-1 => singular top reducible
                   1250:
                   1251: int comp_sig(SIG s1,SIG s2);
                   1252: void _ndltodl(UINT *ndl,DL dl);
                   1253:
                   1254: void print_sig(SIG s)
                   1255: {
                   1256:   int i;
                   1257:
                   1258:   fprintf(asir_out,"<<");
                   1259:   for ( i = 0; i < nd_nvar; i++ ) {
                   1260:     fprintf(asir_out,"%d",s->dl->d[i]);
                   1261:     if ( i != nd_nvar-1 ) fprintf(asir_out,",");
                   1262:   }
                   1263:   fprintf(asir_out,">>*e%d",s->pos);
                   1264: }
                   1265:
1.45      noro     1266: void print_siglist(NODE l)
                   1267: {
                   1268:   for ( ; l; l = NEXT(l) )
                   1269:    print_sig((SIG)l->body);
                   1270: }
                   1271:
                   1272:
1.35      noro     1273: // assuming increasing order wrt signature
                   1274:
1.24      noro     1275: INLINE int ndl_find_reducer_s(UINT *dg,SIG sig)
                   1276: {
                   1277:   RHist r;
1.27      noro     1278:   int i,singular,ret,d,k;
1.26      noro     1279:   static int wpd,nvar;
1.24      noro     1280:   static SIG quo;
                   1281:   static UINT *tmp;
                   1282:
1.26      noro     1283:   if ( !quo || nvar != nd_nvar ) NEWSIG(quo);
                   1284:   if ( wpd != nd_wpd ) {
1.24      noro     1285:     wpd = nd_wpd;
                   1286:     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
                   1287:   }
1.27      noro     1288:   d = ndl_hash_value(dg);
1.35      noro     1289: #if 1
1.27      noro     1290:   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   1291:     if ( ndl_equal(dg,DL(r)) ) {
                   1292:       return r->index;
                   1293:     }
                   1294:   }
1.35      noro     1295: #endif
1.24      noro     1296:   singular = 0;
                   1297:   for ( i = 0; i < nd_psn; i++ ) {
                   1298:     r = nd_psh[i];
                   1299:     if ( ndl_reducible(dg,DL(r)) ) {
1.27      noro     1300:       ndl_sub(dg,DL(r),tmp);
1.24      noro     1301:       _ndltodl(tmp,DL(quo));
                   1302:       _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
                   1303:       quo->pos = nd_psh[i]->sig->pos;
                   1304:       ret = comp_sig(sig,quo);
                   1305:       if ( ret > 0 ) { singular = 0; break; }
1.38      noro     1306:       if ( ret == 0 ) { /* fprintf(asir_out,"s"); fflush(asir_out); */ singular = 1; }
1.24      noro     1307:     }
                   1308:   }
                   1309:   if ( singular ) return -1;
1.27      noro     1310:   else if ( i < nd_psn )
                   1311:     nd_append_red(dg,i);
                   1312:   return i;
1.24      noro     1313: }
                   1314:
1.1       noro     1315: ND nd_merge(ND p1,ND p2)
                   1316: {
                   1317:     int n,c;
                   1318:     int t,can,td1,td2;
                   1319:     ND r;
                   1320:     NM m1,m2,mr0,mr,s;
                   1321:
                   1322:     if ( !p1 ) return p2;
                   1323:     else if ( !p2 ) return p1;
                   1324:     else {
                   1325:         can = 0;
                   1326:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1327:             c = DL_COMPARE(DL(m1),DL(m2));
                   1328:             switch ( c ) {
                   1329:                 case 0:
                   1330:                     s = m1; m1 = NEXT(m1);
                   1331:                     can++; NEXTNM2(mr0,mr,s);
                   1332:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1333:                     break;
                   1334:                 case 1:
                   1335:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1336:                     break;
                   1337:                 case -1:
                   1338:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1339:                     break;
                   1340:             }
                   1341:         }
                   1342:         if ( !mr0 )
                   1343:             if ( m1 ) mr0 = m1;
                   1344:             else if ( m2 ) mr0 = m2;
                   1345:             else return 0;
                   1346:         else if ( m1 ) NEXT(mr) = m1;
                   1347:         else if ( m2 ) NEXT(mr) = m2;
                   1348:         else NEXT(mr) = 0;
                   1349:         BDY(p1) = mr0;
                   1350:         SG(p1) = MAX(SG(p1),SG(p2));
                   1351:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1352:         FREEND(p2);
                   1353:         return p1;
                   1354:     }
                   1355: }
                   1356:
                   1357: ND nd_add(int mod,ND p1,ND p2)
                   1358: {
                   1359:     int n,c;
                   1360:     int t,can,td1,td2;
                   1361:     ND r;
                   1362:     NM m1,m2,mr0,mr,s;
                   1363:
1.11      noro     1364:     Nnd_add++;
1.1       noro     1365:     if ( !p1 ) return p2;
                   1366:     else if ( !p2 ) return p1;
                   1367:     else if ( mod == -1 ) return nd_add_sf(p1,p2);
                   1368:     else if ( mod == -2 ) return nd_add_lf(p1,p2);
                   1369:     else if ( !mod ) return nd_add_q(p1,p2);
                   1370:     else {
                   1371:         can = 0;
                   1372:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1373:             c = DL_COMPARE(DL(m1),DL(m2));
                   1374:             switch ( c ) {
                   1375:                 case 0:
                   1376:                     t = ((CM(m1))+(CM(m2))) - mod;
                   1377:                     if ( t < 0 ) t += mod;
                   1378:                     s = m1; m1 = NEXT(m1);
                   1379:                     if ( t ) {
                   1380:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1381:                     } else {
                   1382:                         can += 2; FREENM(s);
                   1383:                     }
                   1384:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1385:                     break;
                   1386:                 case 1:
                   1387:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1388:                     break;
                   1389:                 case -1:
                   1390:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1391:                     break;
                   1392:             }
                   1393:         }
                   1394:         if ( !mr0 )
                   1395:             if ( m1 ) mr0 = m1;
                   1396:             else if ( m2 ) mr0 = m2;
                   1397:             else return 0;
                   1398:         else if ( m1 ) NEXT(mr) = m1;
                   1399:         else if ( m2 ) NEXT(mr) = m2;
                   1400:         else NEXT(mr) = 0;
                   1401:         BDY(p1) = mr0;
                   1402:         SG(p1) = MAX(SG(p1),SG(p2));
                   1403:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1404:         FREEND(p2);
                   1405:         return p1;
                   1406:     }
                   1407: }
                   1408:
                   1409: /* XXX on opteron, the inlined manipulation of destructive additon of
                   1410:  * two NM seems to make gcc optimizer get confused, so the part is
                   1411:  * done in a function.
                   1412:  */
                   1413:
                   1414: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
                   1415: {
                   1416:     NM s;
                   1417:     P t;
                   1418:     int can;
                   1419:
                   1420:     addp(nd_vc,CP(*m1),CP(*m2),&t);
                   1421:     s = *m1; *m1 = NEXT(*m1);
                   1422:     if ( t ) {
                   1423:         can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
                   1424:     } else {
                   1425:         can = 2; FREENM(s);
                   1426:     }
                   1427:     s = *m2; *m2 = NEXT(*m2); FREENM(s);
                   1428:     return can;
                   1429: }
                   1430:
                   1431: ND nd_add_q(ND p1,ND p2)
                   1432: {
                   1433:     int n,c,can;
                   1434:     ND r;
                   1435:     NM m1,m2,mr0,mr,s;
                   1436:     P t;
                   1437:
                   1438:     if ( !p1 ) return p2;
                   1439:     else if ( !p2 ) return p1;
                   1440:     else {
                   1441:         can = 0;
                   1442:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1443:             c = DL_COMPARE(DL(m1),DL(m2));
                   1444:             switch ( c ) {
                   1445:                 case 0:
                   1446: #if defined(__x86_64__)
                   1447:                     can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
                   1448: #else
                   1449:                     addp(nd_vc,CP(m1),CP(m2),&t);
                   1450:                     s = m1; m1 = NEXT(m1);
                   1451:                     if ( t ) {
                   1452:                         can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
                   1453:                     } else {
                   1454:                         can += 2; FREENM(s);
                   1455:                     }
                   1456:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1457: #endif
                   1458:                     break;
                   1459:                 case 1:
                   1460:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1461:                     break;
                   1462:                 case -1:
                   1463:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1464:                     break;
                   1465:             }
                   1466:         }
                   1467:         if ( !mr0 )
                   1468:             if ( m1 ) mr0 = m1;
                   1469:             else if ( m2 ) mr0 = m2;
                   1470:             else return 0;
                   1471:         else if ( m1 ) NEXT(mr) = m1;
                   1472:         else if ( m2 ) NEXT(mr) = m2;
                   1473:         else NEXT(mr) = 0;
                   1474:         BDY(p1) = mr0;
                   1475:         SG(p1) = MAX(SG(p1),SG(p2));
                   1476:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1477:         FREEND(p2);
                   1478:         return p1;
                   1479:     }
                   1480: }
                   1481:
                   1482: ND nd_add_sf(ND p1,ND p2)
                   1483: {
                   1484:     int n,c,can;
                   1485:     ND r;
                   1486:     NM m1,m2,mr0,mr,s;
                   1487:     int t;
                   1488:
                   1489:     if ( !p1 ) return p2;
                   1490:     else if ( !p2 ) return p1;
                   1491:     else {
                   1492:         can = 0;
                   1493:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1494:             c = DL_COMPARE(DL(m1),DL(m2));
                   1495:             switch ( c ) {
                   1496:                 case 0:
                   1497:                     t = _addsf(CM(m1),CM(m2));
                   1498:                     s = m1; m1 = NEXT(m1);
                   1499:                     if ( t ) {
                   1500:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1501:                     } else {
                   1502:                         can += 2; FREENM(s);
                   1503:                     }
                   1504:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1505:                     break;
                   1506:                 case 1:
                   1507:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1508:                     break;
                   1509:                 case -1:
                   1510:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1511:                     break;
                   1512:             }
                   1513:         }
                   1514:         if ( !mr0 )
                   1515:             if ( m1 ) mr0 = m1;
                   1516:             else if ( m2 ) mr0 = m2;
                   1517:             else return 0;
                   1518:         else if ( m1 ) NEXT(mr) = m1;
                   1519:         else if ( m2 ) NEXT(mr) = m2;
                   1520:         else NEXT(mr) = 0;
                   1521:         BDY(p1) = mr0;
                   1522:         SG(p1) = MAX(SG(p1),SG(p2));
                   1523:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1524:         FREEND(p2);
                   1525:         return p1;
                   1526:     }
                   1527: }
                   1528:
                   1529:
                   1530: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
                   1531: {
                   1532:     int c,c1,c2;
                   1533:     Z cg,cred,gcd,tq;
                   1534:     P cgp,credp,gcdp;
                   1535:     Obj tr,tr1;
                   1536:
                   1537:     if ( mod == -1 ) {
                   1538:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1539:         *divp = (Obj)ONE;
                   1540:     } else if ( mod == -2 ) {
                   1541:         Z inv,t;
                   1542:         divlf(ONE,HCZ(p),&inv);
                   1543:         chsgnlf(HCZ(g),&t);
                   1544:         mullf(inv,t,&CZ(mul));
                   1545:         *divp = (Obj)ONE;
                   1546:     } else if ( mod ) {
                   1547:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1548:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1549:         *divp = (Obj)ONE;
                   1550:     } else if ( nd_vc ) {
                   1551:         ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
                   1552:         divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
                   1553:         chsgnp(cgp,&CP(mul));
                   1554:         nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
                   1555:         if ( dn ) {
                   1556:             mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
                   1557:             reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
                   1558:         }
                   1559:         *divp = (Obj)credp;
                   1560:     } else {
1.6       noro     1561:         igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1562:         chsgnz(cg,&CZ(mul));
1.1       noro     1563:         nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
                   1564:         if ( dn ) {
                   1565:             mulz(dn->z,cred,&tq); dn->z = tq;
                   1566:         }
                   1567:         *divp = (Obj)cred;
                   1568:     }
                   1569:     return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
                   1570: }
                   1571:
                   1572: /* ret=1 : success, ret=0 : overflow */
1.6       noro     1573: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1       noro     1574: {
                   1575:     NM m,mrd,tail;
                   1576:     NM mul;
                   1577:     int n,sugar,psugar,sugar0,stat,index;
                   1578:     int c,c1,c2,dummy;
                   1579:     RHist h;
                   1580:     NDV p,red;
                   1581:     Q cg,cred,gcd,tq,qq;
                   1582:     Z iq;
                   1583:     DP dmul;
                   1584:     NODE node;
                   1585:     LIST hist;
                   1586:     double hmag;
                   1587:     P tp,tp1;
                   1588:     Obj tr,tr1,div;
                   1589:     union oNDC hg;
                   1590:     P cont;
                   1591:
                   1592:     if ( !g ) {
                   1593:         *rp = d;
                   1594:         return 1;
                   1595:     }
                   1596:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1597:
                   1598:     sugar0 = sugar = SG(g);
                   1599:     n = NV(g);
                   1600:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1601:     if ( d )
                   1602:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1603:     for ( ; g; ) {
                   1604:         index = ndl_find_reducer(HDL(g));
                   1605:         if ( index >= 0 ) {
                   1606:             h = nd_psh[index];
                   1607:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1608:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1609:                 nd_free(g); nd_free(d);
                   1610:                 return 0;
                   1611:             }
                   1612:             p = nd_demand ? ndv_load(index) : ps[index];
                   1613:             /* d+g -> div*(d+g)+mul*p */
1.6       noro     1614:             g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1       noro     1615:             if ( nd_gentrace ) {
                   1616:                 /* Trace=[div,index,mul,ONE] */
1.6       noro     1617:                 STOZ(index,iq);
1.1       noro     1618:                 nmtodp(mod,mul,&dmul);
                   1619:                 node = mknode(4,div,iq,dmul,ONE);
                   1620:             }
                   1621:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
                   1622:             if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
                   1623:                 hg = HCU(g);
                   1624:                 nd_removecont2(d,g);
1.6       noro     1625:                 if ( nd_gentrace ) {
1.1       noro     1626:                     /* overwrite cont : Trace=[div,index,mul,cont] */
1.6       noro     1627:                     /* exact division */
1.1       noro     1628:                     cont = ndc_div(mod,hg,HCU(g));
                   1629:                     if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
                   1630:                 }
                   1631:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1632:             }
                   1633:             MKLIST(hist,node);
                   1634:             MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
                   1635:         } else if ( !full ) {
                   1636:             *rp = g;
                   1637:             return 1;
                   1638:         } else {
                   1639:             m = BDY(g);
                   1640:             if ( NEXT(m) ) {
                   1641:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1642:             } else {
                   1643:                 FREEND(g); g = 0;
                   1644:             }
                   1645:             if ( d ) {
                   1646:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1647:             } else {
                   1648:                 MKND(n,m,1,d); tail = BDY(d);
                   1649:             }
                   1650:         }
                   1651:     }
                   1652:     if ( d ) SG(d) = sugar;
                   1653:     *rp = d;
                   1654:     return 1;
                   1655: }
                   1656:
1.24      noro     1657: // ret=1 => success
                   1658: // ret=0 => overflow
                   1659: // ret=-1 => singular top reducible
                   1660:
                   1661: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
                   1662: {
                   1663:     NM m,mrd,tail;
                   1664:     NM mul;
                   1665:     int n,sugar,psugar,sugar0,stat,index;
                   1666:     int c,c1,c2,dummy;
                   1667:     RHist h;
                   1668:     NDV p,red;
                   1669:     Q cg,cred,gcd,tq,qq;
                   1670:     Z iq;
                   1671:     DP dmul;
                   1672:     NODE node;
                   1673:     LIST hist;
                   1674:     double hmag;
                   1675:     P tp,tp1;
                   1676:     Obj tr,tr1,div;
                   1677:     union oNDC hg;
                   1678:     P cont;
                   1679:     SIG sig;
                   1680:
                   1681:     if ( !g ) {
                   1682:         *rp = d;
                   1683:         return 1;
                   1684:     }
                   1685:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1686:
                   1687:     sugar0 = sugar = SG(g);
                   1688:     n = NV(g);
                   1689:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1690:     if ( d )
                   1691:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1692:     sig = g->sig;
                   1693:     for ( ; g; ) {
                   1694:         index = ndl_find_reducer_s(HDL(g),sig);
                   1695:         if ( index >= 0 && index < nd_psn ) {
                   1696:             // reducer found
                   1697:             h = nd_psh[index];
                   1698:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1699:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1700:                 nd_free(g); nd_free(d);
                   1701:                 return 0;
                   1702:             }
                   1703:             p = ps[index];
                   1704:             /* d+g -> div*(d+g)+mul*p */
                   1705:             g = nd_reduce2(mod,d,g,p,mul,0,&div);
                   1706:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
                   1707:             if ( !mod && g && ((double)(p_mag(HCP(g))) > hmag) ) {
                   1708:                 hg = HCU(g);
                   1709:                 nd_removecont2(d,g);
                   1710:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1711:             }
                   1712:         } else if ( index == -1 ) {
                   1713:           // singular top reducible
                   1714:           return -1;
                   1715:         } else if ( !full ) {
                   1716:             *rp = g;
                   1717:             g->sig = sig;
                   1718:             return 1;
                   1719:         } else {
                   1720:             m = BDY(g);
                   1721:             if ( NEXT(m) ) {
                   1722:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1723:             } else {
                   1724:                 FREEND(g); g = 0;
                   1725:             }
                   1726:             if ( d ) {
                   1727:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1728:             } else {
                   1729:                 MKND(n,m,1,d); tail = BDY(d);
                   1730:             }
                   1731:         }
                   1732:     }
                   1733:     if ( d ) {
                   1734:       SG(d) = sugar;
                   1735:       d->sig = sig;
                   1736:     }
                   1737:     *rp = d;
                   1738:     return 1;
                   1739: }
                   1740:
1.1       noro     1741: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
                   1742: {
                   1743:     int hindex,index;
                   1744:     NDV p;
                   1745:     ND u,d,red;
                   1746:     NODE l;
                   1747:     NM mul,m,mrd,tail;
                   1748:     int sugar,psugar,n,h_reducible;
                   1749:     PGeoBucket bucket;
                   1750:     int c,c1,c2;
                   1751:     Z cg,cred,gcd,zzz;
                   1752:     RHist h;
                   1753:     double hmag,gmag;
                   1754:     int count = 0;
                   1755:     int hcount = 0;
                   1756:
                   1757:     if ( !g ) {
                   1758:         *rp = 0;
                   1759:         return 1;
                   1760:     }
                   1761:     sugar = SG(g);
                   1762:     n = NV(g);
1.6       noro     1763:     if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1       noro     1764:     bucket = create_pbucket();
                   1765:     add_pbucket(mod,bucket,g);
                   1766:     d = 0;
                   1767:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1768:     while ( 1 ) {
                   1769:         if ( mod > 0 || mod == -1 )
                   1770:           hindex = head_pbucket(mod,bucket);
                   1771:         else if ( mod == -2 )
                   1772:           hindex = head_pbucket_lf(bucket);
                   1773:         else
                   1774:           hindex = head_pbucket_q(bucket);
                   1775:         if ( hindex < 0 ) {
                   1776:             if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1777:             if ( d ) SG(d) = sugar;
                   1778:             *rp = d;
                   1779:             return 1;
                   1780:         }
                   1781:         g = bucket->body[hindex];
                   1782:         index = ndl_find_reducer(HDL(g));
                   1783:         if ( index >= 0 ) {
                   1784:             count++;
                   1785:             if ( !d ) hcount++;
                   1786:             h = nd_psh[index];
                   1787:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1788:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1789:                 nd_free(d);
                   1790:                 free_pbucket(bucket);
                   1791:                 *rp = 0;
                   1792:                 return 0;
                   1793:             }
                   1794:             p = ps[index];
                   1795:             if ( mod == -1 )
                   1796:                 CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1797:             else if ( mod == -2 ) {
                   1798:                 Z inv,t;
                   1799:                 divlf(ONE,HCZ(p),&inv);
                   1800:                 chsgnlf(HCZ(g),&t);
                   1801:                 mullf(inv,t,&CZ(mul));
                   1802:             } else if ( mod ) {
                   1803:                 c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1804:                 DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1805:             } else {
1.6       noro     1806:                 igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1807:                 chsgnz(cg,&CZ(mul));
1.1       noro     1808:                 nd_mul_c_q(d,(P)cred);
                   1809:                 mulq_pbucket(bucket,cred);
                   1810:                 g = bucket->body[hindex];
1.6       noro     1811:                 gmag = (double)p_mag((P)HCZ(g));
1.1       noro     1812:             }
                   1813:             red = ndv_mul_nm(mod,mul,p);
                   1814:             bucket->body[hindex] = nd_remove_head(g);
                   1815:             red = nd_remove_head(red);
                   1816:             add_pbucket(mod,bucket,red);
                   1817:             psugar = SG(p)+TD(DL(mul));
                   1818:             sugar = MAX(sugar,psugar);
                   1819:             if ( !mod && hmag && (gmag > hmag) ) {
                   1820:                 g = normalize_pbucket(mod,bucket);
                   1821:                 if ( !g ) {
                   1822:                     if ( d ) SG(d) = sugar;
                   1823:                     *rp = d;
                   1824:                     return 1;
                   1825:                 }
                   1826:                 nd_removecont2(d,g);
1.6       noro     1827:                 hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1       noro     1828:                 add_pbucket(mod,bucket,g);
                   1829:             }
                   1830:         } else if ( !full ) {
                   1831:             g = normalize_pbucket(mod,bucket);
                   1832:             if ( g ) SG(g) = sugar;
                   1833:             *rp = g;
                   1834:             return 1;
                   1835:         } else {
                   1836:             m = BDY(g);
                   1837:             if ( NEXT(m) ) {
                   1838:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1839:             } else {
                   1840:                 FREEND(g); g = 0;
                   1841:             }
                   1842:             bucket->body[hindex] = g;
                   1843:             NEXT(m) = 0;
                   1844:             if ( d ) {
                   1845:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1846:             } else {
                   1847:                 MKND(n,m,1,d); tail = BDY(d);
                   1848:             }
                   1849:         }
                   1850:     }
                   1851: }
                   1852:
1.25      noro     1853: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *rp)
                   1854: {
                   1855:   int hindex,index;
                   1856:   NDV p;
                   1857:   ND u,d,red;
                   1858:   NODE l;
                   1859:   NM mul,m,mrd,tail;
                   1860:   int sugar,psugar,n,h_reducible;
                   1861:   PGeoBucket bucket;
                   1862:   int c,c1,c2;
                   1863:   Z cg,cred,gcd,zzz;
                   1864:   RHist h;
                   1865:   double hmag,gmag;
                   1866:   int count = 0;
                   1867:   int hcount = 0;
                   1868:   SIG sig;
                   1869:
                   1870:   if ( !g ) {
                   1871:     *rp = 0;
                   1872:     return 1;
                   1873:   }
                   1874:   sugar = SG(g);
                   1875:   n = NV(g);
                   1876:   if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
                   1877:   bucket = create_pbucket();
                   1878:   add_pbucket(mod,bucket,g);
                   1879:   d = 0;
                   1880:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1881:   sig = g->sig;
                   1882:   while ( 1 ) {
                   1883:     if ( mod > 0 || mod == -1 )
                   1884:       hindex = head_pbucket(mod,bucket);
                   1885:     else if ( mod == -2 )
                   1886:       hindex = head_pbucket_lf(bucket);
                   1887:     else
                   1888:       hindex = head_pbucket_q(bucket);
                   1889:     if ( hindex < 0 ) {
                   1890:       if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1891:       if ( d ) {
                   1892:         SG(d) = sugar;
                   1893:         d->sig = sig;
                   1894:       }
                   1895:       *rp = d;
                   1896:       return 1;
                   1897:     }
                   1898:     g = bucket->body[hindex];
                   1899:     index = ndl_find_reducer_s(HDL(g),sig);
                   1900:     if ( index >= 0 && index < nd_psn ) {
                   1901:       count++;
                   1902:       if ( !d ) hcount++;
                   1903:       h = nd_psh[index];
                   1904:       ndl_sub(HDL(g),DL(h),DL(mul));
                   1905:       if ( ndl_check_bound2(index,DL(mul)) ) {
                   1906:         nd_free(d);
                   1907:         free_pbucket(bucket);
                   1908:         *rp = 0;
                   1909:         return 0;
                   1910:       }
                   1911:       p = ps[index];
                   1912:       if ( mod == -1 )
                   1913:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1914:       else if ( mod == -2 ) {
                   1915:         Z inv,t;
                   1916:         divlf(ONE,HCZ(p),&inv);
                   1917:         chsgnlf(HCZ(g),&t);
                   1918:         mullf(inv,t,&CZ(mul));
                   1919:       } else if ( mod ) {
                   1920:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1921:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1922:       } else {
                   1923:         igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1924:         chsgnz(cg,&CZ(mul));
                   1925:         nd_mul_c_q(d,(P)cred);
                   1926:         mulq_pbucket(bucket,cred);
                   1927:         g = bucket->body[hindex];
                   1928:         gmag = (double)p_mag((P)HCZ(g));
                   1929:       }
                   1930:       red = ndv_mul_nm(mod,mul,p);
                   1931:       bucket->body[hindex] = nd_remove_head(g);
                   1932:       red = nd_remove_head(red);
                   1933:       add_pbucket(mod,bucket,red);
                   1934:       psugar = SG(p)+TD(DL(mul));
                   1935:       sugar = MAX(sugar,psugar);
                   1936:       if ( !mod && hmag && (gmag > hmag) ) {
                   1937:          g = normalize_pbucket(mod,bucket);
                   1938:          if ( !g ) {
                   1939:            if ( d ) {
                   1940:              SG(d) = sugar;
                   1941:              d->sig = sig;
                   1942:            }
                   1943:            *rp = d;
                   1944:            return 1;
                   1945:          }
                   1946:          nd_removecont2(d,g);
                   1947:          hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
                   1948:          add_pbucket(mod,bucket,g);
                   1949:       }
                   1950:     } else if ( index == -1 ) {
                   1951:       // singular top reducible
                   1952:       return -1;
                   1953:     } else if ( !full ) {
                   1954:       g = normalize_pbucket(mod,bucket);
                   1955:       if ( g ) {
                   1956:         SG(g) = sugar;
                   1957:         g->sig = sig;
                   1958:       }
                   1959:       *rp = g;
                   1960:       return 1;
                   1961:     } else {
                   1962:       m = BDY(g);
                   1963:       if ( NEXT(m) ) {
                   1964:         BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1965:       } else {
                   1966:         FREEND(g); g = 0;
                   1967:       }
                   1968:       bucket->body[hindex] = g;
                   1969:       NEXT(m) = 0;
                   1970:       if ( d ) {
                   1971:         NEXT(tail)=m; tail=m; LEN(d)++;
                   1972:       } else {
                   1973:         MKND(n,m,1,d); tail = BDY(d);
                   1974:       }
                   1975:     }
                   1976:   }
                   1977: }
                   1978:
1.1       noro     1979: /* input : list of NDV, cand : list of NDV */
                   1980:
                   1981: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
                   1982: {
                   1983:     int n,i,stat;
                   1984:     ND nf,d;
                   1985:     NDV r;
                   1986:     NODE t,s;
                   1987:     union oNDC dn;
                   1988:     Z q;
                   1989:     LIST list;
                   1990:
1.24      noro     1991:     ndv_setup(m,0,cand,nd_gentrace?1:0,1,0);
1.1       noro     1992:     n = length(cand);
                   1993:
                   1994:   if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
                   1995:     /* membercheck : list is a subset of Id(cand) ? */
                   1996:     for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
                   1997: again:
                   1998:     nd_tracelist = 0;
                   1999:         if ( nd_bpe > obpe )
                   2000:             r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
                   2001:         else
                   2002:             r = (NDV)BDY(t);
                   2003: #if 0
                   2004:         // moved to nd_f4_lf_trace()
                   2005:         if ( m == -2 ) ndv_mod(m,r);
                   2006: #endif
                   2007:         d = ndvtond(m,r);
1.6       noro     2008:         stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1       noro     2009:         if ( !stat ) {
                   2010:             nd_reconstruct(0,0);
                   2011:             goto again;
                   2012:         } else if ( nf ) return 0;
                   2013:     if ( nd_gentrace ) {
                   2014:       nd_tracelist = reverse_node(nd_tracelist);
                   2015:       MKLIST(list,nd_tracelist);
1.6       noro     2016:       STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1       noro     2017:       MKNODE(s,list,nd_alltracelist);
                   2018:       nd_alltracelist = s; nd_tracelist = 0;
                   2019:     }
                   2020:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   2021:     }
                   2022:     if ( DP_Print ) { printf("\n"); }
                   2023:     return 1;
                   2024: }
                   2025:
                   2026: ND nd_remove_head(ND p)
                   2027: {
                   2028:     NM m;
                   2029:
                   2030:     m = BDY(p);
                   2031:     if ( !NEXT(m) ) {
                   2032:         FREEND(p); p = 0;
                   2033:     } else {
                   2034:         BDY(p) = NEXT(m); LEN(p)--;
                   2035:     }
                   2036:     FREENM(m);
                   2037:     return p;
                   2038: }
                   2039:
                   2040: ND nd_separate_head(ND p,ND *head)
                   2041: {
                   2042:     NM m,m0;
                   2043:     ND r;
                   2044:
                   2045:     m = BDY(p);
                   2046:     if ( !NEXT(m) ) {
                   2047:         *head = p; p = 0;
                   2048:     } else {
                   2049:         m0 = m;
                   2050:         BDY(p) = NEXT(m); LEN(p)--;
                   2051:         NEXT(m0) = 0;
                   2052:         MKND(NV(p),m0,1,r);
                   2053:         *head = r;
                   2054:     }
                   2055:     return p;
                   2056: }
                   2057:
                   2058: PGeoBucket create_pbucket()
                   2059: {
                   2060:     PGeoBucket g;
                   2061:
                   2062:     g = CALLOC(1,sizeof(struct oPGeoBucket));
                   2063:     g->m = -1;
                   2064:     return g;
                   2065: }
                   2066:
                   2067: void free_pbucket(PGeoBucket b) {
                   2068:     int i;
                   2069:
                   2070:     for ( i = 0; i <= b->m; i++ )
                   2071:         if ( b->body[i] ) {
                   2072:             nd_free(b->body[i]);
                   2073:             b->body[i] = 0;
                   2074:         }
                   2075:     GCFREE(b);
                   2076: }
                   2077:
1.39      noro     2078: #if 0
1.1       noro     2079: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   2080: {
                   2081:     int l,i,k,m;
                   2082:
                   2083:     if ( !d )
                   2084:         return;
                   2085:     l = LEN(d);
                   2086:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2087:     /* 2^(k-1) < l <= 2^k (=m) */
                   2088:     d = nd_merge(g->body[k],d);
                   2089:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   2090:         g->body[k] = 0;
                   2091:         d = nd_merge(g->body[k+1],d);
                   2092:     }
                   2093:     g->body[k] = d;
                   2094:     g->m = MAX(g->m,k);
                   2095: }
1.39      noro     2096: #else
                   2097: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   2098: {
                   2099:   int l,i,k,m,m0;
                   2100:
                   2101:   if ( !d )
                   2102:     return;
                   2103:   m0 = g->m;
                   2104:   while ( 1 ) {
                   2105:     l = LEN(d);
                   2106:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2107:     /* 2^(k-1) < l <= 2^k (=m) */
                   2108:     if ( g->body[k] == 0 ) {
                   2109:       g->body[k] = d;
                   2110:       m0 = MAX(k,m0);
                   2111:       break;
                   2112:     } else {
                   2113:       d = nd_merge(g->body[k],d);
                   2114:       g->body[k] = 0;
                   2115:     }
                   2116:   }
                   2117:   g->m = m0;
                   2118: }
                   2119: #endif
1.1       noro     2120:
1.39      noro     2121: #if 0
1.1       noro     2122: void add_pbucket(int mod,PGeoBucket g,ND d)
                   2123: {
                   2124:     int l,i,k,m;
                   2125:
                   2126:     if ( !d )
                   2127:         return;
                   2128:     l = LEN(d);
                   2129:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2130:     /* 2^(k-1) < l <= 2^k (=m) */
                   2131:     d = nd_add(mod,g->body[k],d);
                   2132:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   2133:         g->body[k] = 0;
                   2134:         d = nd_add(mod,g->body[k+1],d);
                   2135:     }
                   2136:     g->body[k] = d;
                   2137:     g->m = MAX(g->m,k);
                   2138: }
1.39      noro     2139: #else
                   2140: void add_pbucket(int mod,PGeoBucket g,ND d)
                   2141: {
                   2142:   int l,i,k,m,m0;
                   2143:
                   2144:   m0 = g->m;
                   2145:   while ( d != 0 ) {
                   2146:     l = LEN(d);
                   2147:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2148:     /* 2^(k-1) < l <= 2^k (=m) */
                   2149:     if ( g->body[k] == 0 ) {
                   2150:       g->body[k] = d;
                   2151:       m0 = MAX(k,m0);
                   2152:       break;
                   2153:     } else {
                   2154:       d = nd_add(mod,g->body[k],d);
                   2155:       g->body[k] = 0;
                   2156:     }
                   2157:   }
                   2158:   g->m = m0;
                   2159: }
                   2160: #endif
1.1       noro     2161:
                   2162: void mulq_pbucket(PGeoBucket g,Z c)
                   2163: {
                   2164:     int k;
                   2165:
                   2166:     for ( k = 0; k <= g->m; k++ )
                   2167:         nd_mul_c_q(g->body[k],(P)c);
                   2168: }
                   2169:
                   2170: NM remove_head_pbucket_symbolic(PGeoBucket g)
                   2171: {
                   2172:     int j,i,k,c;
                   2173:     NM head;
                   2174:
                   2175:     k = g->m;
                   2176:     j = -1;
                   2177:     for ( i = 0; i <= k; i++ ) {
                   2178:         if ( !g->body[i] ) continue;
                   2179:         if ( j < 0 ) j = i;
                   2180:         else {
                   2181:             c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
                   2182:             if ( c > 0 )
                   2183:                 j = i;
                   2184:             else if ( c == 0 )
                   2185:                 g->body[i] = nd_remove_head(g->body[i]);
                   2186:         }
                   2187:     }
                   2188:     if ( j < 0 ) return 0;
                   2189:     else {
                   2190:         head = BDY(g->body[j]);
                   2191:         if ( !NEXT(head) ) {
                   2192:             FREEND(g->body[j]);
                   2193:             g->body[j] = 0;
                   2194:         } else {
                   2195:             BDY(g->body[j]) = NEXT(head);
                   2196:             LEN(g->body[j])--;
                   2197:         }
                   2198:         return head;
                   2199:     }
                   2200: }
                   2201:
                   2202: int head_pbucket(int mod,PGeoBucket g)
                   2203: {
                   2204:     int j,i,c,k,nv,sum;
                   2205:     UINT *di,*dj;
                   2206:     ND gi,gj;
                   2207:
                   2208:     k = g->m;
                   2209:     while ( 1 ) {
                   2210:         j = -1;
                   2211:         for ( i = 0; i <= k; i++ ) {
                   2212:             if ( !(gi = g->body[i]) )
                   2213:                 continue;
                   2214:             if ( j < 0 ) {
                   2215:                 j = i;
                   2216:                 gj = g->body[j];
                   2217:                 dj = HDL(gj);
                   2218:                 sum = HCM(gj);
                   2219:             } else {
                   2220:                 c = DL_COMPARE(HDL(gi),dj);
                   2221:                 if ( c > 0 ) {
                   2222:                     if ( sum ) HCM(gj) = sum;
                   2223:                     else g->body[j] = nd_remove_head(gj);
                   2224:                     j = i;
                   2225:                     gj = g->body[j];
                   2226:                     dj = HDL(gj);
                   2227:                     sum = HCM(gj);
                   2228:                 } else if ( c == 0 ) {
                   2229:                     if ( mod == -1 )
                   2230:                         sum = _addsf(sum,HCM(gi));
                   2231:                     else {
                   2232:                         sum = sum+HCM(gi)-mod;
                   2233:                         if ( sum < 0 ) sum += mod;
                   2234:                     }
                   2235:                     g->body[i] = nd_remove_head(gi);
                   2236:                 }
                   2237:             }
                   2238:         }
                   2239:         if ( j < 0 ) return -1;
                   2240:         else if ( sum ) {
                   2241:             HCM(gj) = sum;
                   2242:             return j;
                   2243:         } else
                   2244:             g->body[j] = nd_remove_head(gj);
                   2245:     }
                   2246: }
                   2247:
                   2248: int head_pbucket_q(PGeoBucket g)
                   2249: {
                   2250:     int j,i,c,k,nv;
                   2251:     Z sum,t;
                   2252:     ND gi,gj;
                   2253:
                   2254:     k = g->m;
                   2255:     while ( 1 ) {
                   2256:         j = -1;
                   2257:         for ( i = 0; i <= k; i++ ) {
                   2258:             if ( !(gi = g->body[i]) ) continue;
                   2259:             if ( j < 0 ) {
                   2260:                 j = i;
                   2261:                 gj = g->body[j];
1.6       noro     2262:                 sum = HCZ(gj);
1.1       noro     2263:             } else {
                   2264:                 nv = NV(gi);
                   2265:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   2266:                 if ( c > 0 ) {
1.6       noro     2267:                     if ( sum ) HCZ(gj) = sum;
1.1       noro     2268:                     else g->body[j] = nd_remove_head(gj);
                   2269:                     j = i;
                   2270:                     gj = g->body[j];
1.6       noro     2271:                     sum = HCZ(gj);
1.1       noro     2272:                 } else if ( c == 0 ) {
1.6       noro     2273:                     addz(sum,HCZ(gi),&t);
1.1       noro     2274:                     sum = t;
                   2275:                     g->body[i] = nd_remove_head(gi);
                   2276:                 }
                   2277:             }
                   2278:         }
                   2279:         if ( j < 0 ) return -1;
                   2280:         else if ( sum ) {
1.6       noro     2281:             HCZ(gj) = sum;
1.1       noro     2282:             return j;
                   2283:         } else
                   2284:             g->body[j] = nd_remove_head(gj);
                   2285:     }
                   2286: }
                   2287:
                   2288: int head_pbucket_lf(PGeoBucket g)
                   2289: {
                   2290:     int j,i,c,k,nv;
                   2291:     Z sum,t;
                   2292:     ND gi,gj;
                   2293:
                   2294:     k = g->m;
                   2295:     while ( 1 ) {
                   2296:         j = -1;
                   2297:         for ( i = 0; i <= k; i++ ) {
                   2298:             if ( !(gi = g->body[i]) ) continue;
                   2299:             if ( j < 0 ) {
                   2300:                 j = i;
                   2301:                 gj = g->body[j];
                   2302:                 sum = HCZ(gj);
                   2303:             } else {
                   2304:                 nv = NV(gi);
                   2305:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   2306:                 if ( c > 0 ) {
                   2307:                     if ( sum ) HCZ(gj) = sum;
                   2308:                     else g->body[j] = nd_remove_head(gj);
                   2309:                     j = i;
                   2310:                     gj = g->body[j];
                   2311:                     sum = HCZ(gj);
                   2312:                 } else if ( c == 0 ) {
                   2313:                     addlf(sum,HCZ(gi),&t);
                   2314:                     sum = t;
                   2315:                     g->body[i] = nd_remove_head(gi);
                   2316:                 }
                   2317:             }
                   2318:         }
                   2319:         if ( j < 0 ) return -1;
                   2320:         else if ( sum ) {
                   2321:             HCZ(gj) = sum;
                   2322:             return j;
                   2323:         } else
                   2324:             g->body[j] = nd_remove_head(gj);
                   2325:     }
                   2326: }
                   2327:
                   2328: ND normalize_pbucket(int mod,PGeoBucket g)
                   2329: {
                   2330:     int i;
                   2331:     ND r,t;
                   2332:
                   2333:     r = 0;
                   2334:     for ( i = 0; i <= g->m; i++ ) {
                   2335:         r = nd_add(mod,r,g->body[i]);
                   2336:         g->body[i] = 0;
                   2337:     }
                   2338:     g->m = -1;
                   2339:     return r;
                   2340: }
                   2341:
                   2342: #if 0
                   2343: void register_hcf(NDV p)
                   2344: {
                   2345:     DCP dc,t;
                   2346:     P hc,h;
                   2347:     int c;
                   2348:     NODE l,l1,prev;
                   2349:
                   2350:     hc = p->body->c.p;
                   2351:     if ( !nd_vc || NUM(hc) ) return;
                   2352:     fctrp(nd_vc,hc,&dc);
                   2353:     for ( t = dc; t; t = NEXT(t) ) {
                   2354:         h = t->c;
                   2355:         if ( NUM(h) ) continue;
                   2356:         for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   2357:             c = compp(nd_vc,h,(P)BDY(l));
                   2358:             if ( c >= 0 ) break;
                   2359:         }
                   2360:         if ( !l || c > 0  ) {
                   2361:             MKNODE(l1,h,l);
                   2362:             if ( !prev )
                   2363:                 nd_hcf = l1;
                   2364:             else
                   2365:                 NEXT(prev) = l1;
                   2366:         }
                   2367:     }
                   2368: }
                   2369: #else
                   2370: void register_hcf(NDV p)
                   2371: {
                   2372:     DCP dc,t;
                   2373:     P hc,h,q;
                   2374:     Q dmy;
                   2375:     int c;
                   2376:     NODE l,l1,prev;
                   2377:
                   2378:     hc = p->body->c.p;
                   2379:     if ( NUM(hc) ) return;
                   2380:     ptozp(hc,1,&dmy,&h);
                   2381: #if 1
                   2382:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   2383:         while ( 1 ) {
                   2384:             if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
                   2385:             else break;
                   2386:         }
                   2387:     }
                   2388:     if ( NUM(h) ) return;
                   2389: #endif
                   2390:     for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   2391:         c = compp(nd_vc,h,(P)BDY(l));
                   2392:         if ( c >= 0 ) break;
                   2393:     }
                   2394:     if ( !l || c > 0  ) {
                   2395:         MKNODE(l1,h,l);
                   2396:         if ( !prev )
                   2397:             nd_hcf = l1;
                   2398:         else
                   2399:             NEXT(prev) = l1;
                   2400:     }
                   2401: }
                   2402: #endif
                   2403:
                   2404: int do_diagonalize(int sugar,int m)
                   2405: {
1.6       noro     2406:   int i,nh,stat;
                   2407:   NODE r,g,t;
                   2408:   ND h,nf,s,head;
                   2409:   NDV nfv;
                   2410:   Q q;
                   2411:   P nm,nmp,dn,mnp,dnp,cont,cont1;
                   2412:   union oNDC hc;
                   2413:   NODE node;
                   2414:   LIST l;
                   2415:   Z iq;
1.1       noro     2416:
1.6       noro     2417:   for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   2418:     if ( nd_gentrace ) {
                   2419:       /* Trace = [1,index,1,1] */
                   2420:       STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2421:       MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2422:     }
                   2423:     if ( nd_demand )
                   2424:       nfv = ndv_load(i);
                   2425:     else
                   2426:       nfv = nd_ps[i];
                   2427:     s = ndvtond(m,nfv);
                   2428:     s = nd_separate_head(s,&head);
                   2429:     stat = nd_nf(m,head,s,nd_ps,1,&nf);
                   2430:     if ( !stat ) return 0;
                   2431:     ndv_free(nfv);
                   2432:     hc = HCU(nf); nd_removecont(m,nf);
                   2433:     /* exact division */
                   2434:     cont = ndc_div(m,hc,HCU(nf));
1.1       noro     2435:     if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6       noro     2436:     nfv = ndtondv(m,nf);
                   2437:     nd_free(nf);
                   2438:     nd_bound[i] = ndv_compute_bound(nfv);
                   2439:     if ( !m ) register_hcf(nfv);
                   2440:     if ( nd_demand ) {
                   2441:       ndv_save(nfv,i);
                   2442:       ndv_free(nfv);
                   2443:     } else
                   2444:       nd_ps[i] = nfv;
                   2445:   }
                   2446:   return 1;
1.1       noro     2447: }
                   2448:
                   2449: LIST compute_splist()
                   2450: {
                   2451:   NODE g,tn0,tn,node;
                   2452:   LIST l0;
                   2453:   ND_pairs d,t;
                   2454:   int i;
                   2455:   Z i1,i2;
                   2456:
                   2457:     g = 0; d = 0;
                   2458:     for ( i = 0; i < nd_psn; i++ ) {
                   2459:         d = update_pairs(d,g,i,0);
                   2460:         g = update_base(g,i);
                   2461:     }
                   2462:   for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
                   2463:     NEXTNODE(tn0,tn);
1.6       noro     2464:         STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1       noro     2465:         node = mknode(2,i1,i2); MKLIST(l0,node);
                   2466:     BDY(tn) = l0;
                   2467:   }
                   2468:   if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
                   2469:   return l0;
                   2470: }
                   2471:
1.49      noro     2472: typedef struct oHPDATA {
                   2473:   P hn; // HP(t)=hn(t)/(1-t)^n
                   2474:   int len;
                   2475:   P *head; // hp(i)=head[i] (i=0,...,len-1)
                   2476:   P hp; // dim Hm(i)=hp(i) (i >= len)
                   2477:   VECT x; // BDY(x)[i] = <<0,...,1,...,0>>
                   2478:   P *plist; // plist[i]=(1-t)^i
                   2479: } *HPDATA;
                   2480:
                   2481: void make_reduced(VECT b,int nv);
                   2482: void mhp_rec(VECT b,VECT x,P t,P *r);
                   2483: P mhp_ctop(P *r,P *plist,int n);
                   2484: void mhp_to_hf(VL vl,P hp,int n,P *plist,VECT *head,P *hf);
                   2485: DL monomial_colon(DL a,DL b,int n);
                   2486: LIST dp_monomial_hilbert_poincare(VECT b,VECT x,P *plist);
                   2487:
                   2488: int hpvalue(HPDATA data,int d)
                   2489: {
                   2490:   P *head;
                   2491:   int len;
                   2492:   P hp,val;
                   2493:   Z dz;
                   2494:
                   2495:   head = data->head;
                   2496:   len = data->len;
                   2497:   hp = data->hp;
                   2498:   if ( d < len )
                   2499:     return ZTOS((Z)head[d]);
                   2500:   else {
                   2501:     STOZ(d,dz);
                   2502:     substp(CO,hp,hp->v,(P)dz,&val);
                   2503:     return ZTOS((Z)val);
                   2504:   }
                   2505: }
                   2506:
                   2507: void setup_hpdata(HPDATA final,HPDATA current)
                   2508: {
                   2509:   int n,i;
                   2510:   P *r;
                   2511:   DL *p;
                   2512:   P tv;
                   2513:   VECT b,x,head;
                   2514:   DL dl;
                   2515:
                   2516:   n = nd_nvar;
                   2517:   final->hn = (P)ARG0(nd_hpdata);
                   2518:   head = (VECT)ARG2(nd_hpdata);
                   2519:   final->len = head->len;
                   2520:   final->head = (P *)BDY(head);
                   2521:   final->hp = (P)ARG3(nd_hpdata);
                   2522:   final->plist = (P *)BDY((VECT)ARG4(nd_hpdata));
                   2523:   MKVECT(x,n);
                   2524:   for ( i = 0; i < n; i++ ) {
                   2525:     NEWDL(dl,n); dl->d[i] = 1; dl->td = 1; BDY(x)[i] = dl;
                   2526:   }
                   2527:   final->x = x;
                   2528:
                   2529:   r = (P *)CALLOC(n+1,sizeof(P));
                   2530:   MKVECT(b,nd_psn); p = (DL *)BDY(b);
                   2531:   for ( i = 0; i < nd_psn; i++ ) {
                   2532:     p[i] = ndltodl(n,nd_psh[i]->dl);
                   2533:   }
                   2534:   make_reduced(b,n);
                   2535:   makevar("t",&tv);
                   2536:   mhp_rec(b,x,tv,r);
                   2537:   current->hn = mhp_ctop(r,final->plist,n);
                   2538:   mhp_to_hf(CO,current->hn,n,final->plist,&head,&current->hp);
                   2539:   current->head = (P *)BDY(head);
                   2540:   current->len = head->len;
                   2541:   current->x = x;
                   2542:   current->plist = final->plist;
                   2543: }
                   2544:
                   2545: void update_hpdata(HPDATA current,int nh,int do_hf)
                   2546: {
                   2547:   NODE data1,nd,t;
                   2548:   DL new,dl;
                   2549:   int len,i,n;
                   2550:   Z dz;
                   2551:   DL *p;
                   2552:   VECT b,head;
                   2553:   P tv,td,s,hn,hpoly;
                   2554:   LIST list1;
                   2555:
                   2556:   n = nd_nvar;
                   2557:   new = ndltodl(n,nd_psh[nh]->dl);
                   2558:   MKVECT(b,nh); p = (DL *)BDY(b);
                   2559:   for ( i = 0; i < nh; i++ ) {
                   2560:     p[i] = monomial_colon(ndltodl(n,nd_psh[i]->dl),new,n);
                   2561:   }
                   2562:   // compute HP(I:new)
                   2563:   list1 = dp_monomial_hilbert_poincare(b,current->x,current->plist);
                   2564:   data1 = BDY((LIST)list1);
                   2565:   // HP(I+<new>) = H(I)-t^d*H(I:new), d=tdeg(new)
                   2566:   makevar("t",&tv); UTOZ(new->td,dz);
                   2567:   pwrp(CO,tv,dz,&td);
                   2568:   mulp(CO,(P)ARG0(data1),td,&s);
                   2569:   subp(CO,current->hn,s,&hn);
                   2570:   current->hn = hn;
                   2571:   if ( do_hf ) {
                   2572:     mhp_to_hf(CO,hn,n,current->plist,&head,&hpoly);
                   2573:     current->head = (P *)BDY(head);
                   2574:     current->len = head->len;
                   2575:     current->hp = hpoly;
                   2576:   }
                   2577: }
                   2578:
                   2579: ND_pairs nd_remove_same_sugar( ND_pairs d, int sugar)
                   2580: {
                   2581:   struct oND_pairs root;
                   2582:   ND_pairs prev,cur;
                   2583:
                   2584:   root.next = d;
                   2585:   prev = &root; cur = d;
                   2586:   while ( cur ) {
                   2587:     if ( SG(cur) == sugar )
                   2588:       prev->next = cur->next;
                   2589:     else
                   2590:       prev = cur;
                   2591:     cur = cur->next;
                   2592:   }
                   2593:   return root.next;
                   2594: }
                   2595:
1.1       noro     2596: /* return value = 0 => input is not a GB */
                   2597:
                   2598: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
                   2599: {
1.6       noro     2600:   int i,nh,sugar,stat;
                   2601:   NODE r,g,t;
                   2602:   ND_pairs d;
                   2603:   ND_pairs l;
                   2604:   ND h,nf,s,head,nf1;
                   2605:   NDV nfv;
                   2606:   Z q;
                   2607:   union oNDC dn,hc;
                   2608:   int diag_count = 0;
1.41      noro     2609:   int Nnfnz = 0,Nnfz = 0;
1.6       noro     2610:   P cont;
                   2611:   LIST list;
1.49      noro     2612:   struct oHPDATA current_hpdata,final_hpdata;
                   2613:   int final_hpvalue;
                   2614:
1.27      noro     2615: struct oEGT eg1,eg2,eg_update;
1.6       noro     2616:
1.27      noro     2617: init_eg(&eg_update);
1.11      noro     2618:   Nnd_add = 0;
1.6       noro     2619:   g = 0; d = 0;
                   2620:   for ( i = 0; i < nd_psn; i++ ) {
                   2621:     d = update_pairs(d,g,i,gensyz);
                   2622:     g = update_base(g,i);
                   2623:   }
                   2624:   sugar = 0;
1.49      noro     2625:   if ( nd_hpdata ) {
                   2626:     setup_hpdata(&final_hpdata,&current_hpdata);
                   2627:   }
1.6       noro     2628:   while ( d ) {
1.1       noro     2629: again:
1.6       noro     2630:     l = nd_minp(d,&d);
                   2631:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   2632:     if ( SG(l) != sugar ) {
                   2633:       if ( ishomo ) {
                   2634:         diag_count = 0;
                   2635:         stat = do_diagonalize(sugar,m);
1.1       noro     2636:         if ( !stat ) {
1.6       noro     2637:           NEXT(l) = d; d = l;
                   2638:           d = nd_reconstruct(0,d);
                   2639:           goto again;
1.1       noro     2640:         }
1.6       noro     2641:       }
                   2642:       sugar = SG(l);
                   2643:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
1.49      noro     2644:       if ( nd_hpdata ) {
                   2645:         if ( !compp(CO,final_hpdata.hn,current_hpdata.hn) )
                   2646:           break;
                   2647:         else {
                   2648:           final_hpvalue = hpvalue(&final_hpdata,sugar);
                   2649:           if ( final_hpvalue == hpvalue(&current_hpdata,sugar) ) {
                   2650:             if ( DP_Print ) fprintf(asir_out,"sugar=%d done.\n",sugar);
                   2651:             d = nd_remove_same_sugar(d,sugar);
                   2652:             continue;
                   2653:           }
                   2654:         }
                   2655:       }
1.6       noro     2656:     }
                   2657:     stat = nd_sp(m,0,l,&h);
                   2658:     if ( !stat ) {
                   2659:       NEXT(l) = d; d = l;
                   2660:       d = nd_reconstruct(0,d);
                   2661:       goto again;
                   2662:     }
1.1       noro     2663: #if USE_GEOBUCKET
1.39      noro     2664:     stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf)
                   2665:       :nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     2666: #else
1.39      noro     2667:     stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     2668: #endif
1.6       noro     2669:     if ( !stat ) {
                   2670:       NEXT(l) = d; d = l;
                   2671:       d = nd_reconstruct(0,d);
                   2672:       goto again;
                   2673:     } else if ( nf ) {
1.41      noro     2674:       Nnfnz++;
1.6       noro     2675:       if ( checkonly || gensyz ) return 0;
1.1       noro     2676:       if ( nd_newelim ) {
                   2677:         if ( nd_module ) {
                   2678:           if ( MPOS(HDL(nf)) > 1 ) return 0;
                   2679:         } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
                   2680:       }
1.6       noro     2681:       if ( DP_Print ) { printf("+"); fflush(stdout); }
                   2682:       hc = HCU(nf);
                   2683:       nd_removecont(m,nf);
                   2684:       if ( !m && nd_nalg ) {
                   2685:         nd_monic(0,&nf);
                   2686:         nd_removecont(m,nf);
                   2687:       }
                   2688:       if ( nd_gentrace ) {
                   2689:         /* exact division */
1.1       noro     2690:         cont = ndc_div(m,hc,HCU(nf));
                   2691:         if ( m || !UNIQ(cont) ) {
1.6       noro     2692:           t = mknode(4,NULLP,NULLP,NULLP,cont);
                   2693:           MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1       noro     2694:           nd_tracelist = t;
                   2695:         }
                   2696:       }
1.6       noro     2697:       nfv = ndtondv(m,nf); nd_free(nf);
1.24      noro     2698:       nh = ndv_newps(m,nfv,0);
1.6       noro     2699:       if ( !m && (ishomo && ++diag_count == diag_period) ) {
                   2700:         diag_count = 0;
                   2701:         stat = do_diagonalize(sugar,m);
                   2702:         if ( !stat ) {
                   2703:           NEXT(l) = d; d = l;
                   2704:           d = nd_reconstruct(1,d);
                   2705:           goto again;
1.1       noro     2706:         }
1.6       noro     2707:       }
1.27      noro     2708: get_eg(&eg1);
1.6       noro     2709:       d = update_pairs(d,g,nh,0);
1.27      noro     2710: get_eg(&eg2); add_eg(&eg_update,&eg1,&eg2);
1.6       noro     2711:       g = update_base(g,nh);
                   2712:       FREENDP(l);
1.49      noro     2713:       if ( nd_hpdata ) {
                   2714:         update_hpdata(&current_hpdata,nh,1);
                   2715:         if ( final_hpvalue == hpvalue(&current_hpdata,sugar) ) {
                   2716:           if ( DP_Print ) fprintf(asir_out,"sugar=%d done.\n",sugar);
                   2717:           d = nd_remove_same_sugar(d,sugar);
                   2718:         }
                   2719:       }
1.41      noro     2720:     } else {
                   2721:       Nnfz++;
                   2722:       if ( nd_gentrace && gensyz ) {
                   2723:         nd_tracelist = reverse_node(nd_tracelist);
                   2724:         MKLIST(list,nd_tracelist);
                   2725:         STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
                   2726:         MKNODE(t,list,nd_alltracelist);
                   2727:         nd_alltracelist = t; nd_tracelist = 0;
                   2728:       }
                   2729:       if ( DP_Print ) { printf("."); fflush(stdout); }
                   2730:         FREENDP(l);
                   2731:     }
1.36      noro     2732:   }
                   2733:   conv_ilist(nd_demand,0,g,indp);
1.41      noro     2734:   if ( !checkonly && DP_Print ) {
                   2735:     printf("\nnd_gb done. Nnd_add=%d,Npairs=%d, Nnfnz=%d,Nnfz=%d,",Nnd_add,Npairs,Nnfnz,Nnfz);
                   2736:     printf("Nremoved=%d\n",NcriB+NcriMF+Ncri2);
                   2737:     fflush(asir_out);
                   2738:   }
                   2739:   if ( DP_Print ) {
                   2740:     print_eg("update",&eg_update); fprintf(asir_out,"\n");
                   2741:   }
1.36      noro     2742:   return g;
1.1       noro     2743: }
                   2744:
1.30      noro     2745: ND_pairs update_pairs_s(ND_pairs d,int t,NODE *syz);
1.47      noro     2746: int update_pairs_array_s(ND_pairs *d,int t,NODE *syz);
1.30      noro     2747: ND_pairs nd_newpairs_s(int t ,NODE *syz);
1.47      noro     2748: ND_pairs *nd_newpairs_array_s(int t ,NODE *syz);
1.24      noro     2749:
                   2750: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *nf);
                   2751: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
                   2752:
                   2753: void _copydl(int n,DL d1,DL d2);
                   2754: void _subfromdl(int n,DL d1,DL d2);
                   2755: extern int (*cmpdl)(int n,DL d1,DL d2);
                   2756:
                   2757: NODE insert_sig(NODE l,SIG s)
                   2758: {
                   2759:   int pos;
                   2760:   DL sig;
                   2761:   struct oNODE root;
                   2762:   NODE p,prev,r;
                   2763:   SIG t;
                   2764:
                   2765:   pos = s->pos; sig = DL(s);
                   2766:   root.next = l; prev = &root;
                   2767:   for ( p = l; p; p = p->next ) {
                   2768:     t = (SIG)p->body;
                   2769:     if ( t->pos == pos ) {
                   2770:       if ( _dl_redble(DL(t),sig,nd_nvar) )
                   2771:         return root.next;
                   2772:       else if ( _dl_redble(sig,DL(t),nd_nvar) )
                   2773:         // remove p
                   2774:         prev->next = p->next;
1.45      noro     2775:       else
                   2776:         prev = p;
1.24      noro     2777:     } else
                   2778:       prev = p;
                   2779:   }
1.27      noro     2780:   NEWNODE(r); r->body = (pointer)s; r->next = 0;
                   2781:   for ( p = &root; p->next; p = p->next );
                   2782:   p->next = r;
                   2783: //  r->next = root.next;
                   2784: //  return r;
                   2785:   return root.next;
1.24      noro     2786: }
                   2787:
                   2788: ND_pairs remove_spair_s(ND_pairs d,SIG sig)
                   2789: {
                   2790:   struct oND_pairs root;
                   2791:   ND_pairs prev,p;
                   2792:   SIG spsig;
                   2793:
                   2794:   root.next = d;
                   2795:   prev = &root; p = d;
                   2796:   while ( p ) {
                   2797:     spsig = p->sig;
1.26      noro     2798:     if ( sig->pos == spsig->pos && _dl_redble(DL(sig),DL(spsig),nd_nvar) ) {
1.24      noro     2799:       // remove p
                   2800:       prev->next = p->next;
1.26      noro     2801:       Nsyz++;
                   2802:     } else
1.24      noro     2803:       prev = p;
                   2804:     p = p->next;
                   2805:   }
                   2806:   return (ND_pairs)root.next;
                   2807: }
                   2808:
1.29      noro     2809: int _dl_redble_ext(DL,DL,DL,int);
                   2810:
1.28      noro     2811: int small_lcm(ND_pairs l)
                   2812: {
                   2813:   SIG sig;
                   2814:   int i;
1.29      noro     2815:   NODE t;
1.28      noro     2816:   static DL lcm,mul,quo;
1.44      noro     2817:   static int nvar = 0;
1.28      noro     2818:
1.38      noro     2819:   if ( nd_sba_largelcm ) return 0;
1.28      noro     2820:   if ( nvar < nd_nvar ) {
                   2821:     nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(quo,nvar); NEWDL(mul,nvar);
                   2822:   }
                   2823:   sig = l->sig;
                   2824:   _ndltodl(l->lcm,lcm);
1.29      noro     2825: #if 0
1.28      noro     2826:   for ( i = 0; i < nd_psn; i++ ) {
                   2827:     if ( sig->pos == nd_psh[i]->sig->pos &&
1.29      noro     2828:       _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
1.28      noro     2829:       _ndltodl(DL(nd_psh[i]),mul);
                   2830:       _addtodl(nd_nvar,quo,mul);
                   2831:       if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
                   2832:         break;
                   2833:     }
                   2834:   }
                   2835:   if ( i < nd_psn ) return 1;
                   2836:   else return 0;
1.29      noro     2837: #else
                   2838:   for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
                   2839:     i = (long)BDY(t);
                   2840:     if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
                   2841:       _ndltodl(DL(nd_psh[i]),mul);
                   2842:       _addtodl(nd_nvar,quo,mul);
                   2843:       if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
                   2844:         break;
                   2845:     }
                   2846:   }
                   2847:   if ( t ) return 1;
                   2848:   else return 0;
                   2849: #endif
1.28      noro     2850: }
                   2851:
1.41      noro     2852: ND_pairs find_smallest_lcm(ND_pairs l)
                   2853: {
                   2854:   SIG sig;
                   2855:   int i,minindex;
                   2856:   NODE t;
                   2857:   ND_pairs r;
                   2858:   struct oSIG sig1;
                   2859:   static DL mul,quo,minlm;
1.44      noro     2860:   static int nvar = 0;
1.41      noro     2861:
                   2862:   if ( nvar < nd_nvar ) {
                   2863:     nvar = nd_nvar;
                   2864:     NEWDL(quo,nvar); NEWDL(mul,nvar);
                   2865:     NEWDL(minlm,nvar);
                   2866:   }
                   2867:   sig = l->sig;
                   2868:   // find mg s.t. m*s(g)=sig and m*lm(g) is minimal
                   2869:   _ndltodl(l->lcm,minlm); minindex = -1;
                   2870:   for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
                   2871:     i = (long)BDY(t);
                   2872:     if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
                   2873:       _ndltodl(DL(nd_psh[i]),mul);
                   2874:       _addtodl(nd_nvar,quo,mul);
                   2875:       if ( (*cmpdl)(nd_nvar,minlm,mul) > 0 ) {
                   2876:         minindex = i;
1.47      noro     2877:         break;
1.41      noro     2878:         _copydl(nd_nvar,mul,minlm);
                   2879:       }
                   2880:     }
                   2881:   }
                   2882:   // l->lcm is minimal; return l itself
                   2883:   if ( minindex < 0 ) return l;
1.47      noro     2884:   else return 0;
1.41      noro     2885:   for ( i = 0; i < nd_psn; i++ ) {
                   2886:     if ( i == minindex ) continue;
                   2887:     _ndltodl(DL(nd_psh[i]),mul);
                   2888:     if ( _dl_redble_ext(mul,minlm,quo,nd_nvar) ) {
                   2889:       _addtodl(nd_nvar,nd_ps[i]->sig->dl,quo);
                   2890:       sig1.pos = nd_ps[i]->sig->pos;
                   2891:       sig1.dl = quo;
                   2892:       if ( comp_sig(sig,&sig1) > 0 ) {
                   2893: //        printf("X");
                   2894:         NEWND_pairs(r);
                   2895:         r->sig = sig;
                   2896:         r->i1 = minindex;
                   2897:         r->i2 = i;
                   2898:         dltondl(nd_nvar,minlm,r->lcm);
                   2899:         r->next = 0;
                   2900:         return r;
                   2901:       }
                   2902:     }
                   2903:   }
                   2904:   // there is no suitable spair
                   2905:   return 0;
                   2906: }
                   2907:
1.28      noro     2908: ND_pairs remove_large_lcm(ND_pairs d)
                   2909: {
                   2910:   struct oND_pairs root;
                   2911:   ND_pairs prev,p;
                   2912:
                   2913:   root.next = d;
                   2914:   prev = &root; p = d;
                   2915:   while ( p ) {
1.41      noro     2916: #if 0
1.28      noro     2917:     if ( small_lcm(p) ) {
                   2918:       // remove p
                   2919:       prev->next = p->next;
                   2920:     } else
1.41      noro     2921: #else
                   2922:     if ( find_smallest_lcm(p) == 0 ) {
                   2923:       // remove p
                   2924:       prev->next = p->next;
                   2925:     } else
                   2926: #endif
1.28      noro     2927:       prev = p;
                   2928:     p = p->next;
                   2929:   }
                   2930:   return (ND_pairs)root.next;
                   2931: }
                   2932:
1.26      noro     2933: struct oEGT eg_create,eg_newpairs,eg_merge;
                   2934:
1.30      noro     2935: NODE conv_ilist_s(int demand,int trace,int **indp);
                   2936:
1.44      noro     2937: // S(fj*ei-fi*ej)
                   2938:
1.45      noro     2939: void _subdl(int,DL,DL,DL);
                   2940:
1.44      noro     2941: SIG trivial_sig(int i,int j)
                   2942: {
1.45      noro     2943:   static DL lcm;
1.44      noro     2944:   static struct oSIG sigi,sigj;
                   2945:   static int nvar = 0;
                   2946:   SIG sig;
                   2947:
                   2948:   if ( nvar != nd_nvar ) {
1.45      noro     2949:     nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(sigi.dl,nvar); NEWDL(sigj.dl,nvar);
                   2950:   }
1.46      noro     2951:   if ( nd_sba_inputisgb != 0 ) {
1.45      noro     2952:     lcm_of_DL(nd_nvar,nd_sba_hm[i],nd_sba_hm[j],lcm);
                   2953:     sigi.pos = i; _subdl(nd_nvar,lcm,nd_sba_hm[i],sigi.dl);
                   2954:     sigj.pos = j; _subdl(nd_nvar,lcm,nd_sba_hm[j],sigj.dl);
                   2955:     if ( comp_sig(&sigi,&sigj) > 0 ) sig = dup_sig(&sigi);
                   2956:     else sig = dup_sig(&sigj);
                   2957:   } else {
                   2958:     sigi.pos = i; _copydl(nd_nvar,nd_sba_hm[j],sigi.dl);
                   2959:     sigj.pos = j; _copydl(nd_nvar,nd_sba_hm[i],sigj.dl);
                   2960:     if ( comp_sig(&sigi,&sigj) > 0 ) sig = dup_sig(&sigi);
                   2961:     else sig = dup_sig(&sigj);
1.44      noro     2962:   }
                   2963:   return sig;
                   2964: }
                   2965:
1.47      noro     2966: int nd_minsig(ND_pairs *d)
                   2967: {
                   2968:   int min,i,ret;
                   2969:
                   2970:   min = -1;
                   2971:   for ( i = 0; i < nd_nbase; i++ ) {
                   2972:     if ( d[i] != 0 ) {
                   2973:       if ( min < 0 ) min = i;
                   2974:       else {
                   2975:         ret = comp_sig(d[i]->sig,d[min]->sig);
                   2976:         if ( ret < 0 ) min = i;
                   2977:       }
                   2978:     }
                   2979:   }
                   2980:   return min;
                   2981: }
                   2982:
                   2983: int dlength(ND_pairs d)
                   2984: {
                   2985:   int i;
                   2986:   for ( i = 0; d; d = d->next, i++ );
                   2987:   return i;
                   2988: }
                   2989:
1.44      noro     2990: NODE nd_sba_buch(int m,int ishomo,int **indp,NODE *syzp)
1.24      noro     2991: {
1.39      noro     2992:   int i,j,nh,sugar,stat,pos;
1.30      noro     2993:   NODE r,t,g;
1.47      noro     2994:   ND_pairs *d;
1.41      noro     2995:   ND_pairs l,l1;
1.24      noro     2996:   ND h,nf,s,head,nf1;
                   2997:   NDV nfv;
                   2998:   Z q;
                   2999:   union oNDC dn,hc;
                   3000:   P cont;
                   3001:   LIST list;
                   3002:   SIG sig;
1.29      noro     3003:   NODE *syzlist;
1.47      noro     3004:   int ngen,ind;
1.41      noro     3005:   int Nnominimal,Nredundant;
1.28      noro     3006:   DL lcm,quo,mul;
1.49      noro     3007:   struct oHPDATA final_hpdata,current_hpdata;
1.50      noro     3008:   struct oEGT eg1,eg2,eg3,eg4,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
                   3009:   struct oEGT eg_minsig,eg_smallest,eg_removecont,eg_hpdata,eg_updatepairs,eg_sbabuch,eg_sp;
1.47      noro     3010:   int Nnfs=0,Nnfz=0,Nnfnz=0,dlen,nsyz;
1.24      noro     3011:
1.27      noro     3012: init_eg(&eg_remove);
1.29      noro     3013:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.47      noro     3014:   d = (ND_pairs *)MALLOC(nd_psn*sizeof(ND_pairs));
                   3015:   nd_nbase = nd_psn;
1.26      noro     3016:   Nsyz = 0;
1.24      noro     3017:   Nnd_add = 0;
1.41      noro     3018:   Nnominimal = 0;
1.26      noro     3019:   Nredundant = 0;
1.44      noro     3020:   ngen = nd_psn;
1.48      noro     3021:   if ( !do_weyl ) {
                   3022:     for ( i = 0; i < nd_psn; i++ )
                   3023:       for ( j = i+1; j < nd_psn; j++ ) {
                   3024:         sig = trivial_sig(i,j);
                   3025:         syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
                   3026:       }
1.29      noro     3027:     }
1.47      noro     3028:   dlen = 0;
1.41      noro     3029:   for ( i = 0; i < nd_psn; i++ ) {
1.47      noro     3030:     dlen += update_pairs_array_s(d,i,syzlist);
1.41      noro     3031:   }
1.24      noro     3032:   sugar = 0;
1.39      noro     3033:   pos = 0;
1.49      noro     3034:   if ( nd_hpdata ) {
                   3035:     setup_hpdata(&final_hpdata,&current_hpdata);
                   3036:   }
1.26      noro     3037:   NEWDL(lcm,nd_nvar); NEWDL(quo,nd_nvar); NEWDL(mul,nd_nvar);
1.50      noro     3038: init_eg(&eg_sp);
1.26      noro     3039: init_eg(&eg_create);
                   3040: init_eg(&eg_merge);
1.47      noro     3041: init_eg(&eg_minsig);
                   3042: init_eg(&eg_smallest);
1.29      noro     3043: init_eg(&eg_large);
1.31      noro     3044: init_eg(&eg_nf);
                   3045: init_eg(&eg_nfzero);
1.50      noro     3046: init_eg(&eg_removecont);
                   3047: init_eg(&eg_updatepairs);
                   3048: init_eg(&eg_hpdata);
                   3049: init_eg(&eg_sbabuch);
                   3050: get_eg(&eg3);
1.47      noro     3051:   while ( 1 ) {
                   3052:     if ( DP_Print && dlen%100 == 0 ) fprintf(asir_out,"(%d)",dlen);
                   3053: again :
                   3054: get_eg(&eg1);
                   3055:     ind = nd_minsig(d);
                   3056: get_eg(&eg2); add_eg(&eg_minsig,&eg1,&eg2);
                   3057:     if ( ind < 0 ) break;
                   3058:     l = d[ind];
                   3059: //    printf("(%d,%d)",l->i1,l->i2); print_sig(l->sig); printf("\n");
                   3060: get_eg(&eg1);
1.41      noro     3061:     l1 = find_smallest_lcm(l);
1.47      noro     3062: get_eg(&eg2); add_eg(&eg_smallest,&eg1,&eg2);
1.41      noro     3063:     if ( l1 == 0 ) {
1.47      noro     3064:       d[ind] = d[ind]->next; dlen--;
1.50      noro     3065:       if ( DP_Print && !nd_hpdata ) fprintf(asir_out,"M");
1.41      noro     3066:       Nnominimal++;
                   3067:       continue;
                   3068:     }
                   3069:     if ( SG(l1) != sugar ) {
                   3070:       sugar = SG(l1);
                   3071:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   3072:     }
                   3073:     sig = l1->sig;
                   3074:     if ( DP_Print && nd_sba_pot ) {
                   3075:       if ( sig->pos != pos ) {
                   3076:         fprintf(asir_out,"[%d]",sig->pos);
                   3077:         pos = sig->pos;
                   3078:       }
                   3079:     }
1.50      noro     3080: get_eg(&eg1);
1.41      noro     3081:     stat = nd_sp(m,0,l1,&h);
1.50      noro     3082: get_eg(&eg2); add_eg(&eg_sp,&eg1,&eg2);
1.24      noro     3083:     if ( !stat ) {
1.47      noro     3084:       nd_reconstruct_s(0,d);
1.24      noro     3085:       goto again;
                   3086:     }
1.31      noro     3087: get_eg(&eg1);
1.25      noro     3088: #if USE_GEOBUCKET
1.39      noro     3089:     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     3090: #else
1.39      noro     3091:     stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.24      noro     3092: #endif
1.31      noro     3093: get_eg(&eg2);
1.24      noro     3094:     if ( !stat ) {
1.47      noro     3095:       nd_reconstruct_s(0,d);
1.24      noro     3096:       goto again;
                   3097:     } else if ( stat == -1 ) {
1.47      noro     3098:       d[ind] = d[ind]->next; dlen--;
1.41      noro     3099:       Nnfs++;
1.24      noro     3100:       if ( DP_Print ) { printf("S"); fflush(stdout); }
                   3101:     } else if ( nf ) {
1.47      noro     3102:       d[ind] = d[ind]->next; dlen--;
1.41      noro     3103:       Nnfnz++;
                   3104:       if ( DP_Print ) {
                   3105:         if ( nd_sba_redundant_check ) {
                   3106:           if ( ndl_find_reducer_nonsig(HDL(nf)) >= 0 ) {
                   3107:             Nredundant++;
                   3108:             printf("R");
                   3109:           } else
                   3110:             printf("+");
                   3111:         } else
                   3112:           printf("+");
                   3113:         fflush(stdout);
                   3114:       }
1.31      noro     3115:       add_eg(&eg_nf,&eg1,&eg2);
1.24      noro     3116:       hc = HCU(nf);
1.50      noro     3117:       get_eg(&eg1);
1.24      noro     3118:       nd_removecont(m,nf);
1.50      noro     3119:       get_eg(&eg2); add_eg(&eg_removecont,&eg1,&eg2);
1.24      noro     3120:       nfv = ndtondv(m,nf); nd_free(nf);
                   3121:       nh = ndv_newps(m,nfv,0);
1.26      noro     3122:
1.50      noro     3123:       get_eg(&eg1);
1.47      noro     3124:       dlen += update_pairs_array_s(d,nh,syzlist);
1.50      noro     3125:       get_eg(&eg2); add_eg(&eg_updatepairs,&eg1,&eg2);
1.29      noro     3126:       nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.49      noro     3127:       if ( nd_hpdata ) {
1.50      noro     3128:         get_eg(&eg1);
1.49      noro     3129:         update_hpdata(&current_hpdata,nh,0);
1.50      noro     3130:         get_eg(&eg2); add_eg(&eg_hpdata,&eg1,&eg2);
1.49      noro     3131:         if ( !compp(CO,final_hpdata.hn,current_hpdata.hn) ) {
                   3132:           if ( DP_Print ) { printf("\nWe found a gb.\n"); }
                   3133:           break;
                   3134:         }
                   3135:       }
1.24      noro     3136:    } else {
1.47      noro     3137:       d[ind] = d[ind]->next; dlen--;
1.41      noro     3138:       Nnfz++;
1.31      noro     3139:       add_eg(&eg_nfzero,&eg1,&eg2);
1.24      noro     3140:      // syzygy
1.27      noro     3141: get_eg(&eg1);
1.47      noro     3142:      nsyz = Nsyz;
                   3143:      d[sig->pos] = remove_spair_s(d[sig->pos],sig);
                   3144:      dlen -= Nsyz-nsyz;
1.27      noro     3145: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29      noro     3146:      syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.24      noro     3147:      if ( DP_Print ) { printf("."); fflush(stdout); }
                   3148:    }
                   3149:  }
1.50      noro     3150:  get_eg(&eg4); add_eg(&eg_sbabuch,&eg3,&eg4);
1.30      noro     3151:  g = conv_ilist_s(nd_demand,0,indp);
1.26      noro     3152:  if ( DP_Print ) {
1.47      noro     3153:    printf("\ndlen=%d,nd_sba done. nd_add=%d,Nsyz=%d,Nsamesig=%d,Nnominimal=%d\n",dlen,Nnd_add,Nsyz,Nsamesig,Nnominimal);
1.41      noro     3154:    printf("Nnfnz=%d,Nnfz=%d,Nnfsingular=%d\n",Nnfnz,Nnfz,Nnfs);
1.50      noro     3155:    fflush(stdout);
1.41      noro     3156:    if ( nd_sba_redundant_check )
                   3157:    printf("Nredundant=%d\n",Nredundant);
1.50      noro     3158:    fflush(stdout);
                   3159:    print_eg("sp",&eg_sp);
1.26      noro     3160:    print_eg("create",&eg_create);
                   3161:    print_eg("merge",&eg_merge);
1.47      noro     3162:    print_eg("minsig",&eg_minsig);
                   3163:    print_eg("smallest",&eg_smallest);
                   3164:    print_eg("remove",&eg_remove);
1.50      noro     3165:    printf("\n");
1.47      noro     3166:    print_eg("nf",&eg_nf);
                   3167:    print_eg("nfzero",&eg_nfzero);
1.50      noro     3168:    print_eg("removecont",&eg_removecont);
                   3169:    print_eg("updatepairs",&eg_updatepairs);
                   3170:    print_eg("hpdata",&eg_hpdata);
                   3171:    print_eg("total",&eg_sbabuch);
1.47      noro     3172:    printf("\n");
                   3173:  }
                   3174:  if ( nd_sba_syz ) {
1.27      noro     3175:    print_eg("remove",&eg_remove);
1.31      noro     3176:    print_eg("nf",&eg_nf);
                   3177:    print_eg("nfzero",&eg_nfzero);
1.27      noro     3178:    printf("\n");
1.26      noro     3179:  }
1.44      noro     3180:  if ( nd_sba_syz ) {
                   3181:    NODE hsyz,tsyz,prev;
                   3182:
                   3183:    hsyz = 0;
                   3184:    for ( i = 0; i < ngen; i++ ) {
                   3185:      tsyz = syzlist[i];
                   3186:      for ( prev = 0; tsyz != 0; prev = tsyz, tsyz = NEXT(tsyz))
                   3187:        BDY(tsyz) = (pointer)sigtodpm((SIG)BDY(tsyz));
                   3188:      if ( prev != 0 ) {
                   3189:        prev->next = hsyz; hsyz = syzlist[i];
                   3190:      }
                   3191:    }
                   3192:    *syzp = hsyz;
                   3193:  } else *syzp = 0;
1.24      noro     3194:  return g;
                   3195: }
                   3196:
1.1       noro     3197: /* splist = [[i1,i2],...] */
                   3198:
                   3199: int check_splist(int m,NODE splist)
                   3200: {
                   3201:   NODE t,p;
                   3202:   ND_pairs d,r,l;
                   3203:   int stat;
                   3204:   ND h,nf;
                   3205:
                   3206:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   3207:     p = BDY((LIST)BDY(t));
1.6       noro     3208:     NEXTND_pairs(d,r);
                   3209:     r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
                   3210:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     3211:     SG(r) = TD(LCM(r)); /* XXX */
                   3212:   }
                   3213:   if ( d ) NEXT(r) = 0;
                   3214:
1.6       noro     3215:   while ( d ) {
1.1       noro     3216: again:
1.6       noro     3217:     l = nd_minp(d,&d);
                   3218:     stat = nd_sp(m,0,l,&h);
                   3219:     if ( !stat ) {
                   3220:       NEXT(l) = d; d = l;
                   3221:       d = nd_reconstruct(0,d);
                   3222:       goto again;
                   3223:     }
1.39      noro     3224:     stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.6       noro     3225:     if ( !stat ) {
                   3226:       NEXT(l) = d; d = l;
                   3227:       d = nd_reconstruct(0,d);
                   3228:       goto again;
                   3229:     } else if ( nf ) return 0;
1.1       noro     3230:     if ( DP_Print) { printf("."); fflush(stdout); }
1.6       noro     3231:   }
1.1       noro     3232:   if ( DP_Print) { printf("done.\n"); fflush(stdout); }
                   3233:   return 1;
                   3234: }
                   3235:
                   3236: int check_splist_f4(int m,NODE splist)
                   3237: {
                   3238:   UINT *s0vect;
1.6       noro     3239:   PGeoBucket bucket;
1.1       noro     3240:   NODE p,rp0,t;
                   3241:   ND_pairs d,r,l,ll;
                   3242:   int col,stat;
                   3243:
                   3244:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   3245:     p = BDY((LIST)BDY(t));
1.6       noro     3246:     NEXTND_pairs(d,r);
                   3247:     r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
                   3248:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     3249:     SG(r) = TD(LCM(r)); /* XXX */
                   3250:   }
                   3251:   if ( d ) NEXT(r) = 0;
                   3252:
1.6       noro     3253:   while ( d ) {
                   3254:     l = nd_minsugarp(d,&d);
                   3255:     bucket = create_pbucket();
                   3256:     stat = nd_sp_f4(m,0,l,bucket);
                   3257:     if ( !stat ) {
                   3258:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   3259:       NEXT(ll) = d; d = l;
                   3260:       d = nd_reconstruct(0,d);
                   3261:       continue;
                   3262:     }
                   3263:     if ( bucket->m < 0 ) continue;
                   3264:     col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   3265:     if ( !col ) {
                   3266:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   3267:       NEXT(ll) = d; d = l;
                   3268:       d = nd_reconstruct(0,d);
                   3269:       continue;
1.1       noro     3270:     }
1.6       noro     3271:     if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
                   3272:   }
                   3273:   return 1;
1.1       noro     3274: }
                   3275:
                   3276: int do_diagonalize_trace(int sugar,int m)
                   3277: {
1.6       noro     3278:   int i,nh,stat;
                   3279:   NODE r,g,t;
                   3280:   ND h,nf,nfq,s,head;
                   3281:   NDV nfv,nfqv;
                   3282:   Q q,den,num;
                   3283:   union oNDC hc;
                   3284:   NODE node;
                   3285:   LIST l;
                   3286:   Z iq;
                   3287:   P cont,cont1;
1.1       noro     3288:
1.6       noro     3289:   for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   3290:     if ( nd_gentrace ) {
                   3291:         /* Trace = [1,index,1,1] */
                   3292:         STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   3293:         MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   3294:     }
                   3295:     /* for nd_ps */
                   3296:     s = ndvtond(m,nd_ps[i]);
                   3297:     s = nd_separate_head(s,&head);
                   3298:     stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
                   3299:     if ( !stat ) return 0;
                   3300:     nf = nd_add(m,head,nf);
                   3301:     ndv_free(nd_ps[i]);
                   3302:     nd_ps[i] = ndtondv(m,nf);
                   3303:     nd_free(nf);
                   3304:
                   3305:     /* for nd_ps_trace */
                   3306:     if ( nd_demand )
                   3307:         nfv = ndv_load(i);
                   3308:     else
                   3309:         nfv = nd_ps_trace[i];
                   3310:     s = ndvtond(0,nfv);
                   3311:     s = nd_separate_head(s,&head);
                   3312:     stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
                   3313:     if ( !stat ) return 0;
                   3314:     ndv_free(nfv);
                   3315:     hc = HCU(nf); nd_removecont(0,nf);
                   3316:     /* exact division */
1.1       noro     3317:     cont = ndc_div(0,hc,HCU(nf));
1.6       noro     3318:     if ( nd_gentrace ) finalize_tracelist(i,cont);
                   3319:     nfv = ndtondv(0,nf);
                   3320:     nd_free(nf);
                   3321:     nd_bound[i] = ndv_compute_bound(nfv);
                   3322:     register_hcf(nfv);
                   3323:     if ( nd_demand ) {
                   3324:     ndv_save(nfv,i);
                   3325:     ndv_free(nfv);
                   3326:     } else
                   3327:     nd_ps_trace[i] = nfv;
                   3328:   }
                   3329:   return 1;
1.1       noro     3330: }
                   3331:
                   3332: static struct oEGT eg_invdalg;
                   3333: struct oEGT eg_le;
                   3334:
                   3335: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
                   3336: {
                   3337:     NODE tn;
                   3338:     P p1;
                   3339:
                   3340:     for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
                   3341:         substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
                   3342:     }
                   3343:     *r = p;
                   3344: }
                   3345:
                   3346: NODE nd_gb_trace(int m,int ishomo,int **indp)
                   3347: {
1.6       noro     3348:   int i,nh,sugar,stat;
                   3349:   NODE r,g,t;
                   3350:   ND_pairs d;
                   3351:   ND_pairs l;
                   3352:   ND h,nf,nfq,s,head;
                   3353:   NDV nfv,nfqv;
                   3354:   Z q,den,num;
                   3355:   P hc;
                   3356:   union oNDC dn,hnfq;
                   3357:   struct oEGT eg_monic,egm0,egm1;
                   3358:   int diag_count = 0;
                   3359:   P cont;
                   3360:   LIST list;
1.49      noro     3361:   struct oHPDATA current_hpdata,final_hpdata;
                   3362:   int final_hpvalue;
1.6       noro     3363:
                   3364:   init_eg(&eg_monic);
                   3365:   init_eg(&eg_invdalg);
                   3366:   init_eg(&eg_le);
                   3367:   g = 0; d = 0;
                   3368:   for ( i = 0; i < nd_psn; i++ ) {
                   3369:     d = update_pairs(d,g,i,0);
                   3370:     g = update_base(g,i);
                   3371:   }
                   3372:   sugar = 0;
1.49      noro     3373:   if ( nd_hpdata ) {
                   3374:     setup_hpdata(&final_hpdata,&current_hpdata);
                   3375:   }
                   3376:
1.6       noro     3377:   while ( d ) {
1.1       noro     3378: again:
1.6       noro     3379:     l = nd_minp(d,&d);
                   3380:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   3381:     if ( SG(l) != sugar ) {
1.1       noro     3382: #if 1
1.6       noro     3383:       if ( ishomo ) {
                   3384:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3385:         stat = do_diagonalize_trace(sugar,m);
                   3386:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3387:         diag_count = 0;
1.1       noro     3388:         if ( !stat ) {
1.6       noro     3389:           NEXT(l) = d; d = l;
                   3390:           d = nd_reconstruct(1,d);
                   3391:           goto again;
1.1       noro     3392:         }
1.6       noro     3393:       }
                   3394: #endif
                   3395:       sugar = SG(l);
                   3396:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
1.49      noro     3397:       if ( nd_hpdata ) {
                   3398:         if ( !compp(CO,final_hpdata.hn,current_hpdata.hn) )
                   3399:           break;
                   3400:         else {
                   3401:           final_hpvalue = hpvalue(&final_hpdata,sugar);
                   3402:           if ( final_hpvalue == hpvalue(&current_hpdata,sugar) ) {
                   3403:             if ( DP_Print ) fprintf(asir_out,"sugar=%d done.\n",sugar);
                   3404:             d = nd_remove_same_sugar(d,sugar);
                   3405:             continue;
                   3406:           }
                   3407:         }
                   3408:       }
1.6       noro     3409:     }
                   3410:     stat = nd_sp(m,0,l,&h);
                   3411:     if ( !stat ) {
                   3412:       NEXT(l) = d; d = l;
                   3413:       d = nd_reconstruct(1,d);
                   3414:       goto again;
                   3415:     }
1.1       noro     3416: #if USE_GEOBUCKET
1.39      noro     3417:     stat = nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     3418: #else
1.39      noro     3419:     stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     3420: #endif
1.6       noro     3421:     if ( !stat ) {
                   3422:       NEXT(l) = d; d = l;
                   3423:       d = nd_reconstruct(1,d);
                   3424:       goto again;
                   3425:     } else if ( nf ) {
                   3426:       if ( nd_demand ) {
                   3427:         nfqv = ndv_load(nd_psn);
                   3428:         nfq = ndvtond(0,nfqv);
                   3429:       } else
                   3430:         nfq = 0;
                   3431:       if ( !nfq ) {
1.39      noro     3432:         if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!nd_top&&!Top,&nfq) ) {
1.6       noro     3433:           NEXT(l) = d; d = l;
                   3434:           d = nd_reconstruct(1,d);
                   3435:           goto again;
                   3436:         }
                   3437:       }
                   3438:       if ( nfq ) {
                   3439:         /* m|HC(nfq) => failure */
                   3440:         if ( nd_vc ) {
                   3441:           nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
                   3442:         } else
                   3443:           q = HCZ(nfq);
                   3444:         if ( !remqi((Q)q,m) ) return 0;
                   3445:
                   3446:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   3447:         hnfq = HCU(nfq);
                   3448:         if ( nd_nalg ) {
                   3449:           /* m|DN(HC(nf)^(-1)) => failure */
                   3450:           get_eg(&egm0);
                   3451:           if ( !nd_monic(m,&nfq) ) return 0;
                   3452:           get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
                   3453:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   3454:           nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
                   3455:         } else {
                   3456:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   3457:           nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
                   3458:         }
                   3459:         if ( nd_gentrace ) {
                   3460:           /* exact division */
                   3461:           cont = ndc_div(0,hnfq,HCU(nfqv));
                   3462:           if ( !UNIQ(cont) ) {
                   3463:             t = mknode(4,NULLP,NULLP,NULLP,cont);
                   3464:             MKLIST(list,t); MKNODE(t,list,nd_tracelist);
                   3465:             nd_tracelist = t;
                   3466:           }
                   3467:         }
1.24      noro     3468:         nh = ndv_newps(0,nfv,nfqv);
1.6       noro     3469:         if ( ishomo && ++diag_count == diag_period ) {
                   3470:           diag_count = 0;
                   3471:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3472:           stat = do_diagonalize_trace(sugar,m);
                   3473:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3474:           if ( !stat ) {
1.1       noro     3475:             NEXT(l) = d; d = l;
                   3476:             d = nd_reconstruct(1,d);
                   3477:             goto again;
1.6       noro     3478:           }
1.1       noro     3479:         }
1.6       noro     3480:         d = update_pairs(d,g,nh,0);
                   3481:         g = update_base(g,nh);
1.49      noro     3482:         if ( nd_hpdata ) {
                   3483:           update_hpdata(&current_hpdata,nh,1);
                   3484:           if ( final_hpvalue == hpvalue(&current_hpdata,sugar) ) {
                   3485:             if ( DP_Print ) fprintf(asir_out,"sugar=%d done.\n",sugar);
                   3486:             d = nd_remove_same_sugar(d,sugar);
                   3487:           }
                   3488:         }
1.6       noro     3489:       } else {
                   3490:         if ( DP_Print ) { printf("*"); fflush(stdout); }
                   3491:       }
                   3492:     } else {
                   3493:       if ( DP_Print ) { printf("."); fflush(stdout); }
1.1       noro     3494:     }
1.6       noro     3495:     FREENDP(l);
                   3496:   }
                   3497:   if ( nd_nalg ) {
                   3498:     if ( DP_Print ) {
                   3499:       print_eg("monic",&eg_monic);
                   3500:       print_eg("invdalg",&eg_invdalg);
                   3501:       print_eg("le",&eg_le);
1.1       noro     3502:     }
1.6       noro     3503:   }
1.1       noro     3504:   conv_ilist(nd_demand,1,g,indp);
1.41      noro     3505:   if ( DP_Print ) { printf("\nnd_gb_trace done.\n"); fflush(stdout); }
1.6       noro     3506:   return g;
1.1       noro     3507: }
                   3508:
                   3509: int ndv_compare(NDV *p1,NDV *p2)
                   3510: {
                   3511:     return DL_COMPARE(HDL(*p1),HDL(*p2));
                   3512: }
                   3513:
                   3514: int ndv_compare_rev(NDV *p1,NDV *p2)
                   3515: {
                   3516:     return -DL_COMPARE(HDL(*p1),HDL(*p2));
                   3517: }
                   3518:
                   3519: int ndvi_compare(NDVI p1,NDVI p2)
                   3520: {
                   3521:     return DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3522: }
                   3523:
                   3524: int ndvi_compare_rev(NDVI p1,NDVI p2)
                   3525: {
                   3526:     return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3527: }
                   3528:
                   3529: NODE ndv_reduceall(int m,NODE f)
                   3530: {
                   3531:   int i,j,n,stat;
                   3532:   ND nf,g,head;
                   3533:   NODE t,a0,a;
                   3534:   union oNDC dn;
                   3535:   Q q,num,den;
                   3536:   NODE node;
                   3537:   LIST l;
                   3538:   Z iq,jq;
                   3539:   int *perm;
                   3540:   union oNDC hc;
                   3541:   P cont,cont1;
                   3542:
                   3543:   if ( nd_nora ) return f;
                   3544:   n = length(f);
1.24      noro     3545:   ndv_setup(m,0,f,0,1,0);
1.1       noro     3546:   perm = (int *)MALLOC(n*sizeof(int));
                   3547:   if ( nd_gentrace ) {
                   3548:     for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6       noro     3549:       perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1       noro     3550:   }
                   3551:   for ( i = 0; i < n; ) {
                   3552:     if ( nd_gentrace ) {
                   3553:       /* Trace = [1,index,1,1] */
1.6       noro     3554:       STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1       noro     3555:       MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   3556:     }
                   3557:     g = ndvtond(m,nd_ps[i]);
                   3558:     g = nd_separate_head(g,&head);
1.6       noro     3559:     stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1       noro     3560:     if ( !stat )
                   3561:       nd_reconstruct(0,0);
                   3562:     else {
                   3563:       if ( DP_Print ) { printf("."); fflush(stdout); }
                   3564:       ndv_free(nd_ps[i]);
                   3565:       hc = HCU(nf); nd_removecont(m,nf);
                   3566:       if ( nd_gentrace ) {
                   3567:         for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6       noro     3568:           jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
                   3569:           STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1       noro     3570:         }
1.6       noro     3571:         /* exact division */
1.1       noro     3572:         cont = ndc_div(m,hc,HCU(nf));
                   3573:         finalize_tracelist(perm[i],cont);
                   3574:       }
                   3575:       nd_ps[i] = ndtondv(m,nf); nd_free(nf);
                   3576:       nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   3577:       i++;
                   3578:     }
                   3579:   }
                   3580:   if ( DP_Print ) { printf("\n"); }
                   3581:   for ( a0 = 0, i = 0; i < n; i++ ) {
                   3582:     NEXTNODE(a0,a);
                   3583:     if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
                   3584:     else {
                   3585:       for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
                   3586:       BDY(a) = (pointer)nd_ps[j];
                   3587:     }
                   3588:   }
                   3589:   NEXT(a) = 0;
                   3590:   return a0;
                   3591: }
                   3592:
1.41      noro     3593: int ndplength(ND_pairs d)
                   3594: {
                   3595:   int i;
                   3596:   for ( i = 0; d; i++ ) d = NEXT(d);
                   3597:   return i;
                   3598: }
                   3599:
1.1       noro     3600: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
                   3601: {
                   3602:   ND_pairs d1,nd,cur,head,prev,remove;
1.41      noro     3603:   int len0;
1.1       noro     3604:
                   3605:   if ( !g ) return d;
                   3606:   /* for testing */
                   3607:   if ( gensyz && nd_gensyz == 2 ) {
                   3608:     d1 = nd_newpairs(g,t);
                   3609:     if ( !d )
                   3610:       return d1;
                   3611:     else {
                   3612:       nd = d;
                   3613:       while ( NEXT(nd) ) nd = NEXT(nd);
                   3614:       NEXT(nd) = d1;
                   3615:       return d;
                   3616:     }
                   3617:   }
                   3618:   d = crit_B(d,t);
                   3619:   d1 = nd_newpairs(g,t);
1.41      noro     3620:   len0 = ndplength(d1);
1.1       noro     3621:   d1 = crit_M(d1);
                   3622:   d1 = crit_F(d1);
1.41      noro     3623:   NcriMF += len0-ndplength(d1);
1.1       noro     3624:   if ( gensyz || do_weyl )
                   3625:     head = d1;
                   3626:   else {
                   3627:     prev = 0; cur = head = d1;
                   3628:     while ( cur ) {
                   3629:       if ( crit_2( cur->i1,cur->i2 ) ) {
                   3630:         remove = cur;
                   3631:         if ( !prev ) head = cur = NEXT(cur);
                   3632:         else cur = NEXT(prev) = NEXT(cur);
1.41      noro     3633:         FREENDP(remove); Ncri2++;
1.1       noro     3634:       } else {
                   3635:         prev = cur; cur = NEXT(cur);
                   3636:       }
                   3637:     }
                   3638:   }
                   3639:   if ( !d )
                   3640:     return head;
                   3641:   else {
                   3642:     nd = d;
                   3643:     while ( NEXT(nd) ) nd = NEXT(nd);
                   3644:     NEXT(nd) = head;
                   3645:     return d;
                   3646:   }
                   3647: }
                   3648:
1.24      noro     3649: ND_pairs merge_pairs_s(ND_pairs d,ND_pairs d1);
                   3650:
1.30      noro     3651: ND_pairs update_pairs_s( ND_pairs d, int t,NODE *syz)
1.24      noro     3652: {
                   3653:   ND_pairs d1;
1.26      noro     3654:   struct oEGT eg1,eg2,eg3;
1.24      noro     3655:
1.30      noro     3656:   if ( !t ) return d;
1.26      noro     3657: get_eg(&eg1);
1.30      noro     3658:   d1 = nd_newpairs_s(t,syz);
1.26      noro     3659: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
1.24      noro     3660:   d = merge_pairs_s(d,d1);
1.26      noro     3661: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
1.24      noro     3662:   return d;
                   3663: }
1.1       noro     3664:
1.47      noro     3665: int update_pairs_array_s( ND_pairs *d, int t,NODE *syz)
                   3666: {
                   3667:   ND_pairs *d1;
                   3668:   struct oEGT eg1,eg2,eg3;
                   3669:   int i;
                   3670:
                   3671:   if ( !t ) return 0;
                   3672: get_eg(&eg1);
                   3673:   Nnewpair = 0;
                   3674:   d1 = nd_newpairs_array_s(t,syz);
                   3675: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
                   3676:   for ( i = 0; i < nd_nbase; i++ )
                   3677:     d[i] = merge_pairs_s(d[i],d1[i]);
                   3678: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
                   3679:   return Nnewpair;
                   3680: }
                   3681:
1.1       noro     3682: ND_pairs nd_newpairs( NODE g, int t )
                   3683: {
                   3684:   NODE h;
                   3685:   UINT *dl;
                   3686:   int ts,s,i,t0,min,max;
                   3687:   ND_pairs r,r0;
                   3688:
                   3689:   dl = DL(nd_psh[t]);
                   3690:   ts = SG(nd_psh[t]) - TD(dl);
1.17      noro     3691:   if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1       noro     3692:   for ( r0 = 0, h = g; h; h = NEXT(h) ) {
                   3693:     if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
                   3694:       continue;
                   3695:     if ( nd_gbblock ) {
                   3696:       t0 = (long)BDY(h);
                   3697:       for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
                   3698:         min = nd_gbblock[i]; max = nd_gbblock[i+1];
                   3699:         if ( t0 >= min && t0 <= max && t >= min && t <= max )
                   3700:           break;
                   3701:       }
                   3702:       if ( nd_gbblock[i] >= 0 )
                   3703:         continue;
                   3704:     }
1.41      noro     3705:     NEXTND_pairs(r0,r); Npairs++;
1.1       noro     3706:     r->i1 = (long)BDY(h);
                   3707:     r->i2 = t;
                   3708:     ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
                   3709:     s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   3710:     SG(r) = MAX(s,ts) + TD(LCM(r));
                   3711:     /* experimental */
                   3712:     if ( nd_sugarweight )
                   3713:       r->sugar2 = ndl_weight2(r->lcm);
                   3714:   }
                   3715:   if ( r0 ) NEXT(r) = 0;
                   3716:   return r0;
                   3717: }
                   3718:
1.46      noro     3719: int sig_cmpdl_op(int n,DL d1,DL d2)
                   3720: {
                   3721:   int e1,e2,i,j,l;
                   3722:   int *t1,*t2;
                   3723:   int len,head;
                   3724:   struct order_pair *pair;
                   3725:
                   3726:   len = nd_sba_modord->block_length;
                   3727:   pair = nd_sba_modord->order_pair;
                   3728:
                   3729:   head = 0;
                   3730:   for ( i = 0, t1 = d1->d, t2 = d2->d; i < len; i++ ) {
                   3731:     l = pair[i].length;
                   3732:     switch ( pair[i].order ) {
                   3733:       case 0:
                   3734:         for ( j = 0, e1 = e2 = 0; j < l; j++ ) {
                   3735:           e1 += t1[j];
                   3736:           e2 += t2[j];
                   3737:         }
                   3738:         if ( e1 > e2 )
                   3739:           return 1;
                   3740:         else if ( e1 < e2 )
                   3741:           return -1;
                   3742:         else {
                   3743:           for ( j = l - 1; j >= 0 && t1[j] == t2[j]; j-- );
                   3744:           if ( j >= 0 )
                   3745:             return t1[j] < t2[j] ? 1 : -1;
                   3746:         }
                   3747:         break;
                   3748:       case 1:
                   3749:         for ( j = 0, e1 = e2 = 0; j < l; j++ ) {
                   3750:           e1 += t1[j];
                   3751:           e2 += t2[j];
                   3752:         }
                   3753:         if ( e1 > e2 )
                   3754:           return 1;
                   3755:         else if ( e1 < e2 )
                   3756:           return -1;
                   3757:         else {
                   3758:           for ( j = 0; j < l && t1[j] == t2[j]; j++ );
                   3759:           if ( j < l )
                   3760:             return t1[j] > t2[j] ? 1 : -1;
                   3761:         }
                   3762:         break;
                   3763:       case 2:
                   3764:         for ( j = 0; j < l && t1[j] == t2[j]; j++ );
                   3765:         if ( j < l )
                   3766:           return t1[j] > t2[j] ? 1 : -1;
                   3767:         break;
                   3768:       default:
                   3769:         error("sig_cmpdl_op : invalid order"); break;
                   3770:     }
                   3771:     t1 += l; t2 += l; head += l;
                   3772:   }
                   3773:   return 0;
                   3774: }
                   3775:
                   3776: int sig_cmpdl_mat(int n,DL d1,DL d2)
                   3777: {
                   3778:   int *v,*t1,*t2;
                   3779:   int s,i,j,len;
                   3780:   int **matrix;
                   3781:   static int *w;
                   3782:   static int nvar = 0;
                   3783:
                   3784:   if ( nvar != n ) {
                   3785:     nvar = n; w = (int *)MALLOC(n*sizeof(int));
                   3786:   }
                   3787:   for ( i = 0, t1 = d1->d, t2 = d2->d; i < n; i++ )
                   3788:     w[i] = t1[i]-t2[i];
                   3789:   len = nd_sba_modord->row;
                   3790:   matrix = nd_sba_modord->matrix;
                   3791:   for ( j = 0; j < len; j++ ) {
                   3792:     v = matrix[j];
                   3793:     for ( i = 0, s = 0; i < n; i++ )
                   3794:       s += v[i]*w[i];
                   3795:     if ( s > 0 )
                   3796:       return 1;
                   3797:     else if ( s < 0 )
                   3798:       return -1;
                   3799:   }
                   3800:   return 0;
                   3801: }
                   3802:
                   3803: struct comp_sig_spec *create_comp_sig_spec(VL current_vl,VL old_vl,Obj ord,Obj weight)
                   3804: {
                   3805:   struct comp_sig_spec *spec;
                   3806:   VL ovl,vl;
                   3807:   V ov;
                   3808:   int i,j,n,nvar,s;
                   3809:   NODE node,t,tn;
                   3810:   struct order_pair *l;
                   3811:   MAT m;
                   3812:   Obj **b;
                   3813:   int **w;
                   3814:   int *a;
                   3815:
                   3816:   spec = (struct comp_sig_spec *)MALLOC(sizeof(struct comp_sig_spec));
                   3817:   for ( i = 0, vl = current_vl; vl; vl = NEXT(vl), i++ );
                   3818:   spec->n = nvar = i;
                   3819:   if ( old_vl != 0 ) {
                   3820:     spec->oldv = (int *)MALLOC(nvar*sizeof(int));
                   3821:     for ( i = 0, ovl = old_vl; i < nvar; ovl = NEXT(ovl), i++ ) {
                   3822:       ov = ovl->v;
                   3823:       for ( j = 0, vl = current_vl; vl; vl = NEXT(vl), j++ )
                   3824:         if ( ov == vl->v ) break;
                   3825:       spec->oldv[i] = j;
                   3826:     }
                   3827:   } else
                   3828:     spec->oldv = 0;
                   3829:   if ( !ord || NUM(ord) ) {
                   3830:     switch ( ZTOS((Z)ord) ) {
                   3831:       case 0:
                   3832:         spec->cmpdl = cmpdl_revgradlex; break;
                   3833:       case 1:
                   3834:         spec->cmpdl = cmpdl_gradlex; break;
                   3835:       case 2:
                   3836:         spec->cmpdl = cmpdl_lex; break;
                   3837:       default:
                   3838:         error("create_comp_sig_spec : invalid spec"); break;
                   3839:     }
                   3840:   } else if ( OID(ord) == O_LIST ) {
                   3841:     node = BDY((LIST)ord);
                   3842:     for ( n = 0, t = node; t; t = NEXT(t), n++ );
                   3843:     l = (struct order_pair *)MALLOC_ATOMIC(n*sizeof(struct order_pair));
                   3844:     for ( i = 0, t = node, s = 0; i < n; t = NEXT(t), i++ ) {
                   3845:       tn = BDY((LIST)BDY(t)); l[i].order = ZTOS((Q)BDY(tn));
                   3846:       tn = NEXT(tn); l[i].length = ZTOS((Q)BDY(tn));
                   3847:       s += l[i].length;
                   3848:     }
                   3849:     if ( s != nvar )
                   3850:       error("create_comp_sig_spec : invalid spec");
                   3851:     spec->order_pair = l;
                   3852:     spec->block_length = n;
                   3853:     spec->cmpdl = sig_cmpdl_op;
                   3854:   } else if ( OID(ord) == O_MAT ) {
                   3855:     m = (MAT)ord; b = (Obj **)BDY(m);
                   3856:     if ( m->col != nvar )
                   3857:       error("create_comp_sig_spec : invalid spec");
                   3858:     w = almat(m->row,m->col);
                   3859:     for ( i = 0; i < m->row; i++ )
                   3860:       for ( j = 0; j < m->col; j++ )
                   3861:         w[i][j] = ZTOS((Q)b[i][j]);
                   3862:     spec->row = m->row;
                   3863:     spec->matrix = w;
                   3864:     spec->cmpdl = sig_cmpdl_mat;
                   3865:   } else
                   3866:     error("create_comp_sig_spec : invalid spec");
                   3867:   if ( weight != 0 ) {
                   3868:     node = BDY((LIST)weight);
                   3869:     a = (int *)MALLOC(nvar*sizeof(int));
                   3870:     for ( i = 0; i < nvar; i++, node = NEXT(node) )
                   3871:       a[i] = ZTOS((Z)BDY(node));
                   3872:     spec->weight = a;
                   3873:   }
                   3874:   return spec;
                   3875: }
                   3876:
                   3877: #define SIG_MUL_WEIGHT(a,i) (weight?(a)*weight[i]:(a))
                   3878:
                   3879: int comp_sig_monomial(int n,DL d1,DL d2)
                   3880: {
                   3881:   static DL m1,m2;
                   3882:   static int nvar = 0;
                   3883:   int *oldv,*weight;
                   3884:   int i,w1,w2;
                   3885:
                   3886:   if ( nvar != n ) {
                   3887:     nvar = n; NEWDL(m1,nvar); NEWDL(m2,nvar);
                   3888:   }
                   3889:   if ( !nd_sba_modord )
                   3890:     return (*cmpdl)(n,d1,d2);
                   3891:   else {
                   3892:     weight = nd_sba_modord->weight;
                   3893:     oldv = nd_sba_modord->oldv;
                   3894:     if ( oldv ) {
                   3895:       for ( i = 0; i < n; i++ ) {
                   3896:         m1->d[i] = d1->d[oldv[i]]; m2->d[i] = d2->d[oldv[i]];
                   3897:       }
                   3898:     } else {
                   3899:       for ( i = 0; i < n; i++ ) {
                   3900:         m1->d[i] = d1->d[i]; m2->d[i] = d2->d[i];
                   3901:       }
                   3902:     }
                   3903:     for ( i = 0, w1 = w2 = 0; i < n; i++ ) {
                   3904:       w1 += SIG_MUL_WEIGHT(m1->d[i],i);
                   3905:       w2 += SIG_MUL_WEIGHT(m2->d[i],i);
                   3906:     }
                   3907:     m1->td = w1; m2->td = w2;
                   3908:     return (*nd_sba_modord->cmpdl)(n,m1,m2);
                   3909:   }
                   3910: }
                   3911:
1.24      noro     3912: int comp_sig(SIG s1,SIG s2)
                   3913: {
1.38      noro     3914:   if ( nd_sba_pot ) {
                   3915:     if ( s1->pos > s2->pos ) return 1;
                   3916:     else if ( s1->pos < s2->pos ) return -1;
1.46      noro     3917:     else return comp_sig_monomial(nd_nvar,s1->dl,s2->dl);
1.38      noro     3918:   } else {
                   3919:     static DL m1,m2;
1.44      noro     3920:     static int nvar = 0;
1.38      noro     3921:     int ret;
                   3922:
                   3923:     if ( nvar != nd_nvar ) {
                   3924:       nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
                   3925:     }
                   3926:     _adddl(nd_nvar,s1->dl,nd_sba_hm[s1->pos],m1);
                   3927:     _adddl(nd_nvar,s2->dl,nd_sba_hm[s2->pos],m2);
1.47      noro     3928:     if ( !nd_sba_modord )
                   3929:       ret = (*cmpdl)(nd_nvar,m1,m2);
                   3930:     else
                   3931:       ret = comp_sig_monomial(nd_nvar,m1,m2);
1.38      noro     3932:     if ( ret != 0 ) return ret;
                   3933:     else if ( s1->pos > s2->pos ) return 1;
                   3934:     else if ( s1->pos < s2->pos ) return -1;
                   3935:     else return 0;
1.24      noro     3936:   }
                   3937: }
                   3938:
                   3939: int _create_spair_s(int i1,int i2,ND_pairs sp,SIG sig1,SIG sig2)
                   3940: {
                   3941:   int ret,s1,s2;
                   3942:   RHist p1,p2;
                   3943:   static int wpd;
                   3944:   static UINT *lcm;
                   3945:
                   3946:   sp->i1 = i1;
                   3947:   sp->i2 = i2;
                   3948:   p1 = nd_psh[i1];
                   3949:   p2 = nd_psh[i2];
                   3950:   ndl_lcm(DL(p1),DL(p2),sp->lcm);
1.49      noro     3951: #if 0
1.24      noro     3952:   s1 = SG(p1)-TD(DL(p1));
                   3953:   s2 = SG(p2)-TD(DL(p2));
                   3954:   SG(sp) = MAX(s1,s2) + TD(sp->lcm);
1.49      noro     3955: #endif
1.24      noro     3956:
1.26      noro     3957:   if ( wpd != nd_wpd ) {
1.24      noro     3958:     wpd = nd_wpd;
                   3959:     lcm = (UINT *)MALLOC(wpd*sizeof(UINT));
                   3960:   }
                   3961:   // DL(sig1) <- sp->lcm
                   3962:   // DL(sig1) -= DL(p1)
                   3963:   // DL(sig1) += DL(p1->sig)
1.27      noro     3964:   ndl_sub(sp->lcm,DL(p1),lcm);
1.24      noro     3965:   _ndltodl(lcm,DL(sig1));
                   3966:   _addtodl(nd_nvar,DL(p1->sig),DL(sig1));
                   3967:   sig1->pos = p1->sig->pos;
                   3968:
                   3969:   // DL(sig2) <- sp->lcm
                   3970:   // DL(sig2) -= DL(p2)
                   3971:   // DL(sig2) += DL(p2->sig)
1.27      noro     3972:   ndl_sub(sp->lcm,DL(p2),lcm);
1.24      noro     3973:   _ndltodl(lcm,DL(sig2));
                   3974:   _addtodl(nd_nvar,DL(p2->sig),DL(sig2));
                   3975:   sig2->pos = p2->sig->pos;
                   3976:
                   3977:   ret = comp_sig(sig1,sig2);
                   3978:   if ( ret == 0 ) return 0;
                   3979:   else if ( ret > 0 ) sp->sig = sig1;
                   3980:   else sp->sig = sig2;
1.49      noro     3981:
                   3982:   s1 = DL(sig1)->td+nd_sba_hm[p1->sig->pos]->td;
                   3983:   s2 = DL(sig2)->td+nd_sba_hm[p2->sig->pos]->td;
                   3984:   SG(sp) = MAX(s1,s2);
                   3985:
1.24      noro     3986:   return 1;
                   3987: }
                   3988:
                   3989: SIG dup_sig(SIG sig)
                   3990: {
                   3991:   SIG r;
                   3992:
                   3993:   if ( !sig ) return 0;
                   3994:   else {
                   3995:     NEWSIG(r);
                   3996:     _copydl(nd_nvar,DL(sig),DL(r));
                   3997:     r->pos = sig->pos;
                   3998:     return r;
                   3999:   }
                   4000: }
                   4001:
                   4002: void dup_ND_pairs(ND_pairs to,ND_pairs from)
                   4003: {
                   4004:   to->i1 = from->i1;
                   4005:   to->i2 = from->i2;
                   4006:   to->sugar = from->sugar;
                   4007:   to->sugar2 = from->sugar2;
                   4008:   ndl_copy(from->lcm,to->lcm);
                   4009:   to->sig = dup_sig(from->sig);
                   4010: }
                   4011:
                   4012: ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
                   4013: {
                   4014:   struct oND_pairs root;
                   4015:   ND_pairs q1,q2,r0,r;
                   4016:   int ret;
                   4017:
                   4018:   r = &root;
                   4019:   for ( q1 = p1, q2 = p2; q1 != 0 && q2 != 0; ) {
                   4020:     ret = comp_sig(q1->sig,q2->sig);
                   4021:     if ( ret < 0 ) {
                   4022:       r->next = q1; r = q1; q1 = q1->next;
                   4023:     } else if ( ret > 0 ) {
                   4024:       r->next = q2; r = q2; q2 = q2->next;
                   4025:     } else {
1.47      noro     4026:       Nnewpair--;
1.24      noro     4027:       ret = DL_COMPARE(q1->lcm,q2->lcm);
1.35      noro     4028:       Nsamesig++;
1.24      noro     4029:       if ( ret < 0 ) {
                   4030:         r->next = q1; r = q1; q1 = q1->next;
                   4031:         q2 = q2->next;
                   4032:       } else {
                   4033:         r->next = q2; r = q2; q2 = q2->next;
                   4034:         q1 = q1->next;
                   4035:       }
                   4036:     }
                   4037:   }
                   4038:   if ( q1 ) {
                   4039:     r->next = q1;
                   4040:   } else {
                   4041:     r->next = q2;
                   4042:   }
                   4043:   return root.next;
                   4044: }
                   4045:
                   4046: ND_pairs insert_pair_s(ND_pairs l,ND_pairs s)
                   4047: {
                   4048:   ND_pairs p,prev;
1.47      noro     4049:   int ret=1;
1.24      noro     4050:
                   4051:   for ( p = l, prev = 0; p != 0; prev = p, p = p->next ) {
                   4052:     if ( (ret = comp_sig(s->sig,p->sig)) <= 0 )
                   4053:       break;
                   4054:   }
                   4055:   if ( ret == 0 ) {
                   4056:     ret = DL_COMPARE(s->lcm,p->lcm);
                   4057:     if ( ret < 0 ) {
                   4058:       // replace p with s
                   4059:       s->next = p->next;
                   4060:       if ( prev == 0 ) {
                   4061:         return s;
                   4062:       } else {
                   4063:         prev->next = s;
                   4064:         return l;
                   4065:       }
                   4066:     } else
                   4067:       return l;
                   4068:   } else {
                   4069:     // insert s between prev and p
1.47      noro     4070:     Nnewpair++;
1.24      noro     4071:     s->next = p;
                   4072:     if ( prev == 0 ) {
                   4073:       return s;
                   4074:     } else {
                   4075:       prev->next = s;
                   4076:       return l;
                   4077:     }
                   4078:   }
                   4079: }
                   4080:
1.29      noro     4081: INLINE int __dl_redble(DL d1,DL d2,int nvar)
                   4082: {
                   4083:   int i;
                   4084:
                   4085:   if ( d1->td > d2->td )
                   4086:     return 0;
                   4087:   for ( i = nvar-1; i >= 0; i-- )
                   4088:     if ( d1->d[i] > d2->d[i] )
                   4089:       break;
                   4090:   if ( i >= 0 )
                   4091:     return 0;
                   4092:   else
                   4093:     return 1;
                   4094: }
                   4095:
1.30      noro     4096: ND_pairs nd_newpairs_s(int t, NODE *syz)
1.24      noro     4097: {
                   4098:   NODE h,s;
                   4099:   UINT *dl;
1.27      noro     4100:   int ts,ret,i;
1.24      noro     4101:   ND_pairs r,r0,_sp,sp;
1.29      noro     4102:   SIG spsig,tsig;
1.44      noro     4103:   static int nvar = 0;
1.29      noro     4104:   static SIG _sig1,_sig2;
1.26      noro     4105:   struct oEGT eg1,eg2,eg3,eg4;
1.24      noro     4106:
                   4107:   NEWND_pairs(_sp);
1.29      noro     4108:   if ( !_sig1 || nvar != nd_nvar ) {
                   4109:     nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
                   4110:   }
1.24      noro     4111:   r0 = 0;
1.27      noro     4112:   for ( i = 0; i < t; i++ ) {
                   4113:     ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
1.24      noro     4114:     if ( ret ) {
                   4115:       spsig = _sp->sig;
1.29      noro     4116:       for ( s = syz[spsig->pos]; s; s = s->next ) {
1.24      noro     4117:         tsig = (SIG)s->body;
1.29      noro     4118:         if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
1.24      noro     4119:           break;
                   4120:       }
                   4121:       if ( s == 0 ) {
                   4122:         NEWND_pairs(sp);
                   4123:         dup_ND_pairs(sp,_sp);
                   4124:         r0 = insert_pair_s(r0,sp);
1.26      noro     4125:       } else
                   4126:         Nsyz++;
1.24      noro     4127:     }
                   4128:   }
                   4129:   return r0;
                   4130: }
                   4131:
1.47      noro     4132: ND_pairs *nd_newpairs_array_s(int t, NODE *syz)
                   4133: {
                   4134:   NODE h,s;
                   4135:   UINT *dl;
                   4136:   int ts,ret,i;
                   4137:   ND_pairs r,r0,_sp,sp;
                   4138:   ND_pairs *d;
                   4139:   SIG spsig,tsig;
                   4140:   static int nvar = 0;
                   4141:   static SIG _sig1,_sig2;
                   4142:   struct oEGT eg1,eg2,eg3,eg4;
                   4143:
                   4144:   NEWND_pairs(_sp);
                   4145:   if ( !_sig1 || nvar != nd_nvar ) {
                   4146:     nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
                   4147:   }
                   4148:   d = (ND_pairs *)MALLOC(nd_nbase*sizeof(ND_pairs));
                   4149:   Nnewpair = 0;
                   4150:   for ( i = 0; i < t; i++ ) {
                   4151:     ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
                   4152:     if ( ret ) {
                   4153:       spsig = _sp->sig;
                   4154:       for ( s = syz[spsig->pos]; s; s = s->next ) {
                   4155:         tsig = (SIG)s->body;
                   4156:         if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
                   4157:           break;
                   4158:       }
                   4159:       if ( s == 0 ) {
                   4160:         NEWND_pairs(sp);
                   4161:         dup_ND_pairs(sp,_sp);
                   4162:         d[spsig->pos] = insert_pair_s(d[spsig->pos],sp);
                   4163:       } else
                   4164:         Nsyz++;
                   4165:     }
                   4166:   }
                   4167:   return d;
                   4168: }
                   4169:
1.1       noro     4170: /* ipair = [i1,i2],[i1,i2],... */
                   4171: ND_pairs nd_ipairtospair(NODE ipair)
                   4172: {
                   4173:   int s1,s2;
                   4174:   NODE tn,t;
                   4175:   ND_pairs r,r0;
                   4176:
                   4177:   for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
                   4178:     NEXTND_pairs(r0,r);
                   4179:     tn = BDY((LIST)BDY(t));
1.6       noro     4180:     r->i1 = ZTOS((Q)ARG0(tn));
                   4181:     r->i2 = ZTOS((Q)ARG1(tn));
1.1       noro     4182:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
                   4183:     s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   4184:     s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
                   4185:     SG(r) = MAX(s1,s2) + TD(LCM(r));
                   4186:     /* experimental */
                   4187:     if ( nd_sugarweight )
                   4188:         r->sugar2 = ndl_weight2(r->lcm);
                   4189:   }
                   4190:   if ( r0 ) NEXT(r) = 0;
                   4191:   return r0;
                   4192: }
                   4193:
                   4194: /* kokokara */
                   4195:
                   4196: ND_pairs crit_B( ND_pairs d, int s )
                   4197: {
                   4198:   ND_pairs cur,head,prev,remove;
                   4199:   UINT *t,*tl,*lcm;
                   4200:   int td,tdl;
                   4201:
                   4202:   if ( !d ) return 0;
                   4203:   t = DL(nd_psh[s]);
                   4204:   prev = 0;
                   4205:   head = cur = d;
                   4206:   lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   4207:   while ( cur ) {
                   4208:     tl = cur->lcm;
                   4209:     if ( ndl_reducible(tl,t) ) {
                   4210:       ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
                   4211:       if ( !ndl_equal(lcm,tl) ) {
                   4212:         ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
                   4213:         if (!ndl_equal(lcm,tl)) {
                   4214:           remove = cur;
                   4215:           if ( !prev ) {
                   4216:             head = cur = NEXT(cur);
                   4217:           } else {
                   4218:             cur = NEXT(prev) = NEXT(cur);
                   4219:           }
1.41      noro     4220:           FREENDP(remove); NcriB++;
1.1       noro     4221:         } else {
                   4222:           prev = cur; cur = NEXT(cur);
                   4223:         }
                   4224:       } else {
                   4225:         prev = cur; cur = NEXT(cur);
                   4226:       }
                   4227:     } else {
                   4228:       prev = cur; cur = NEXT(cur);
                   4229:     }
                   4230:   }
                   4231:   return head;
                   4232: }
                   4233:
                   4234: ND_pairs crit_M( ND_pairs d1 )
                   4235: {
                   4236:     ND_pairs e,d2,d3,dd,p;
                   4237:     UINT *id,*jd;
                   4238:
                   4239:     if ( !d1 ) return d1;
                   4240:     for ( dd = 0, e = d1; e; e = d3 ) {
                   4241:         if ( !(d2 = NEXT(e)) ) {
                   4242:             NEXT(e) = dd;
                   4243:             return e;
                   4244:         }
                   4245:         id = LCM(e);
                   4246:         for ( d3 = 0; d2; d2 = p ) {
                   4247:             p = NEXT(d2);
                   4248:             jd = LCM(d2);
                   4249:             if ( ndl_equal(jd,id) )
                   4250:                 ;
                   4251:             else if ( TD(jd) > TD(id) )
                   4252:                 if ( ndl_reducible(jd,id) ) continue;
                   4253:                 else ;
                   4254:             else if ( ndl_reducible(id,jd) ) goto delit;
                   4255:             NEXT(d2) = d3;
                   4256:             d3 = d2;
                   4257:         }
                   4258:         NEXT(e) = dd;
                   4259:         dd = e;
                   4260:         continue;
                   4261:         /**/
                   4262:     delit:    NEXT(d2) = d3;
                   4263:         d3 = d2;
                   4264:         for ( ; p; p = d2 ) {
                   4265:             d2 = NEXT(p);
                   4266:             NEXT(p) = d3;
                   4267:             d3 = p;
                   4268:         }
                   4269:         FREENDP(e);
                   4270:     }
                   4271:     return dd;
                   4272: }
                   4273:
                   4274: ND_pairs crit_F( ND_pairs d1 )
                   4275: {
                   4276:     ND_pairs rest, head,remove;
                   4277:     ND_pairs last, p, r, w;
                   4278:     int s;
                   4279:
                   4280:     if ( !d1 ) return d1;
                   4281:     for ( head = last = 0, p = d1; NEXT(p); ) {
                   4282:         r = w = equivalent_pairs(p,&rest);
                   4283:         s = SG(r);
                   4284:         w = NEXT(w);
                   4285:         while ( w ) {
                   4286:             if ( crit_2(w->i1,w->i2) ) {
                   4287:                 r = w;
                   4288:                 w = NEXT(w);
                   4289:                 while ( w ) {
                   4290:                     remove = w;
                   4291:                     w = NEXT(w);
                   4292:                     FREENDP(remove);
                   4293:                 }
                   4294:                 break;
                   4295:             } else if ( SG(w) < s ) {
                   4296:                 FREENDP(r);
                   4297:                 r = w;
                   4298:                 s = SG(r);
                   4299:                 w = NEXT(w);
                   4300:             } else {
                   4301:                 remove = w;
                   4302:                 w = NEXT(w);
                   4303:                 FREENDP(remove);
                   4304:             }
                   4305:         }
                   4306:         if ( last ) NEXT(last) = r;
                   4307:         else head = r;
                   4308:         NEXT(last = r) = 0;
                   4309:         p = rest;
                   4310:         if ( !p ) return head;
                   4311:     }
                   4312:     if ( !last ) return p;
                   4313:     NEXT(last) = p;
                   4314:     return head;
                   4315: }
                   4316:
                   4317: int crit_2( int dp1, int dp2 )
                   4318: {
                   4319:     return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
                   4320: }
                   4321:
                   4322: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
                   4323: {
                   4324:     ND_pairs w,p,r,s;
                   4325:     UINT *d;
                   4326:
                   4327:     w = d1;
                   4328:     d = LCM(w);
                   4329:     s = NEXT(w);
                   4330:     NEXT(w) = 0;
                   4331:     for ( r = 0; s; s = p ) {
                   4332:         p = NEXT(s);
                   4333:         if ( ndl_equal(d,LCM(s)) ) {
                   4334:             NEXT(s) = w; w = s;
                   4335:         } else {
                   4336:             NEXT(s) = r; r = s;
                   4337:         }
                   4338:     }
                   4339:     *prest = r;
                   4340:     return w;
                   4341: }
                   4342:
                   4343: NODE update_base(NODE nd,int ndp)
                   4344: {
                   4345:     UINT *dl, *dln;
                   4346:     NODE last, p, head;
                   4347:
                   4348:     dl = DL(nd_psh[ndp]);
                   4349:     for ( head = last = 0, p = nd; p; ) {
                   4350:         dln = DL(nd_psh[(long)BDY(p)]);
                   4351:         if ( ndl_reducible( dln, dl ) ) {
                   4352:             p = NEXT(p);
                   4353:             if ( last ) NEXT(last) = p;
                   4354:         } else {
                   4355:             if ( !last ) head = p;
                   4356:             p = NEXT(last = p);
                   4357:         }
                   4358:     }
                   4359:     head = append_one(head,ndp);
                   4360:     return head;
                   4361: }
                   4362:
                   4363: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   4364: {
                   4365:     ND_pairs m,ml,p,l;
                   4366:     UINT *lcm;
                   4367:     int s,td,len,tlen,c,c1;
                   4368:
                   4369:     if ( !(p = NEXT(m = d)) ) {
                   4370:         *prest = p;
                   4371:         NEXT(m) = 0;
                   4372:         return m;
                   4373:     }
                   4374:     if ( !NoSugar ) {
                   4375:         if ( nd_sugarweight ) {
                   4376:             s = m->sugar2;
                   4377:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   4378:                 if ( (p->sugar2 < s)
                   4379:                     || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   4380:                     ml = l; m = p; s = m->sugar2;
                   4381:                 }
                   4382:         } else {
                   4383:             s = SG(m);
                   4384:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   4385:                 if ( (SG(p) < s)
                   4386:                     || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   4387:                     ml = l; m = p; s = SG(m);
                   4388:                 }
                   4389:         }
                   4390:     } else {
                   4391:         for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   4392:             if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
                   4393:                 ml = l; m = p; s = SG(m);
                   4394:             }
                   4395:     }
                   4396:     if ( !ml ) *prest = NEXT(m);
                   4397:     else {
                   4398:         NEXT(ml) = NEXT(m);
                   4399:         *prest = d;
                   4400:     }
                   4401:     NEXT(m) = 0;
                   4402:     return m;
                   4403: }
                   4404:
                   4405: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   4406: {
                   4407:     int msugar,i;
                   4408:     ND_pairs t,dm0,dm,dr0,dr;
                   4409:
                   4410:     if ( nd_sugarweight ) {
                   4411:         for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
                   4412:             if ( t->sugar2 < msugar ) msugar = t->sugar2;
                   4413:         dm0 = 0; dr0 = 0;
                   4414:         for ( i = 0, t = d; t; t = NEXT(t) )
                   4415:             if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
                   4416:                 if ( dm0 ) NEXT(dm) = t;
                   4417:                 else dm0 = t;
                   4418:                 dm = t;
                   4419:                 i++;
                   4420:             } else {
                   4421:                 if ( dr0 ) NEXT(dr) = t;
                   4422:                 else dr0 = t;
                   4423:                 dr = t;
                   4424:             }
                   4425:     } else {
                   4426:         for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   4427:             if ( SG(t) < msugar ) msugar = SG(t);
                   4428:         dm0 = 0; dr0 = 0;
                   4429:         for ( i = 0, t = d; t; t = NEXT(t) )
                   4430:             if ( i < nd_f4_nsp && SG(t) == msugar ) {
                   4431:                 if ( dm0 ) NEXT(dm) = t;
                   4432:                 else dm0 = t;
                   4433:                 dm = t;
                   4434:                 i++;
                   4435:             } else {
                   4436:                 if ( dr0 ) NEXT(dr) = t;
                   4437:                 else dr0 = t;
                   4438:                 dr = t;
                   4439:             }
                   4440:     }
                   4441:     NEXT(dm) = 0;
                   4442:     if ( dr0 ) NEXT(dr) = 0;
                   4443:     *prest = dr0;
                   4444:     return dm0;
                   4445: }
                   4446:
1.28      noro     4447: ND_pairs nd_minsugarp_s( ND_pairs d, ND_pairs *prest )
                   4448: {
                   4449:   int msugar;
                   4450:   ND_pairs t,last;
                   4451:
                   4452:   for ( msugar = SG(d), t = d; t; t = NEXT(t) )
                   4453:     if ( SG(t) == msugar ) last = t;
                   4454:   *prest = last->next;
                   4455:   last->next = 0;
                   4456:   return d;
                   4457: }
                   4458:
1.1       noro     4459: int nd_tdeg(NDV c)
                   4460: {
                   4461:   int wmax = 0;
                   4462:   int i,len;
                   4463:   NMV a;
                   4464:
                   4465:   len = LEN(c);
                   4466:   for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   4467:     wmax = MAX(TD(DL(a)),wmax);
                   4468:   return wmax;
                   4469: }
                   4470:
1.24      noro     4471: int ndv_newps(int m,NDV a,NDV aq)
1.1       noro     4472: {
                   4473:     int len;
                   4474:     RHist r;
                   4475:     NDV b;
                   4476:     NODE tn;
                   4477:     LIST l;
                   4478:     Z iq;
                   4479:
                   4480:     if ( nd_psn == nd_pslen ) {
                   4481:         nd_pslen *= 2;
                   4482:         nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
                   4483:         nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
                   4484:         nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
                   4485:         nd_bound = (UINT **)
                   4486:             REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
                   4487:         nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
                   4488:         nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
                   4489:     }
                   4490:     NEWRHist(r); nd_psh[nd_psn] = r;
                   4491:     nd_ps[nd_psn] = a;
                   4492:     if ( aq ) {
                   4493:         nd_ps_trace[nd_psn] = aq;
                   4494:         if ( !m ) {
                   4495:           register_hcf(aq);
                   4496:         } else if ( m == -2 ) {
                   4497:           /* do nothing */
                   4498:         } else
                   4499:           error("ndv_newps : invalud modulus");
                   4500:         nd_bound[nd_psn] = ndv_compute_bound(aq);
                   4501: #if 1
                   4502:         SG(r) = SG(aq);
                   4503: #else
                   4504:         SG(r) = nd_tdeg(aq);
                   4505: #endif
                   4506:         ndl_copy(HDL(aq),DL(r));
1.24      noro     4507:         r->sig = dup_sig(aq->sig);
1.1       noro     4508:     } else {
                   4509:         if ( !m ) register_hcf(a);
                   4510:         nd_bound[nd_psn] = ndv_compute_bound(a);
                   4511: #if 1
                   4512:         SG(r) = SG(a);
                   4513: #else
                   4514:         SG(r) = nd_tdeg(a);
                   4515: #endif
                   4516:         ndl_copy(HDL(a),DL(r));
1.24      noro     4517:         r->sig = dup_sig(a->sig);
1.1       noro     4518:     }
                   4519:     if ( nd_demand ) {
                   4520:         if ( aq ) {
                   4521:             ndv_save(nd_ps_trace[nd_psn],nd_psn);
                   4522:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   4523:             nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   4524:             nd_ps_trace[nd_psn] = 0;
                   4525:         } else {
                   4526:             ndv_save(nd_ps[nd_psn],nd_psn);
                   4527:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
                   4528:             nd_ps[nd_psn] = 0;
                   4529:         }
                   4530:     }
                   4531:     if ( nd_gentrace ) {
                   4532:         /* reverse the tracelist and append it to alltracelist */
                   4533:         nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6       noro     4534:         STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1       noro     4535:         MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
                   4536:     }
                   4537:     return nd_psn++;
                   4538: }
                   4539:
1.46      noro     4540: // find LM wrt the specified modord
                   4541: void ndv_lm_modord(NDV p,DL d)
1.45      noro     4542: {
                   4543:   NMV m;
                   4544:   DL tmp;
                   4545:   int len,i,ret;
                   4546:
                   4547:   NEWDL(tmp,nd_nvar);
                   4548:   m = BDY(p); len = LEN(p);
1.46      noro     4549:   _ndltodl(DL(m),d); // printdl(d); printf("->");
1.45      noro     4550:   for ( i = 1, NMV_ADV(m); i < len; i++, NMV_ADV(m) ) {
                   4551:     _ndltodl(DL(m),tmp);
1.46      noro     4552:     ret = comp_sig_monomial(nd_nvar,tmp,d);
1.45      noro     4553:     if ( ret > 0 ) _copydl(nd_nvar,tmp,d);
                   4554:   }
1.46      noro     4555: //   printdl(d); printf("\n");
1.45      noro     4556: }
                   4557:
1.1       noro     4558: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
                   4559: /* return 1 if success, 0 if failure (HC(a mod p)) */
                   4560:
1.24      noro     4561: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont,int sba)
1.1       noro     4562: {
1.6       noro     4563:   int i,j,td,len,max;
                   4564:   NODE s,s0,f0,tn;
                   4565:   UINT *d;
                   4566:   RHist r;
                   4567:   NDVI w;
                   4568:   NDV a,am;
                   4569:   union oNDC hc;
                   4570:   NODE node;
                   4571:   P hcp;
                   4572:   Z iq,jq;
                   4573:   LIST l;
                   4574:
                   4575:   nd_found = 0; nd_notfirst = 0; nd_create = 0;
                   4576:   /* initialize the tracelist */
                   4577:   nd_tracelist = 0;
                   4578:
                   4579:   for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
                   4580:   w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
                   4581:   for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
                   4582:     if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
                   4583:   if ( !dont_sort ) {
                   4584:     /* XXX heuristic */
1.26      noro     4585:     if ( !sba && !nd_ord->id && (nd_ord->ord.simple<2) )
1.6       noro     4586:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   4587:         (int (*)(const void *,const void *))ndvi_compare_rev);
                   4588:     else
                   4589:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   4590:         (int (*)(const void *,const void *))ndvi_compare);
                   4591:   }
                   4592:   nd_pslen = 2*nd_psn;
                   4593:   nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4594:   nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4595:   nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4596:   nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4597:   nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
                   4598:   nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
                   4599:   nd_hcf = 0;
1.1       noro     4600:
1.6       noro     4601:   if ( trace && nd_vc )
                   4602:     makesubst(nd_vc,&nd_subst);
                   4603:   else
                   4604:     nd_subst = 0;
1.1       noro     4605:
1.6       noro     4606:   if ( !nd_red )
                   4607:     nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   4608:   for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
                   4609:   for ( i = 0; i < nd_psn; i++ ) {
                   4610:     hc = HCU(w[i].p);
                   4611:     if ( trace ) {
                   4612:       if ( mod == -2 ) {
                   4613:         /* over a large finite field */
                   4614:         /* trace = small modulus */
                   4615:         a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
                   4616:         ndv_mod(-2,a);
                   4617:         if ( !dont_removecont) ndv_removecont(-2,a);
                   4618:         am = nd_ps[i] = ndv_dup(trace,w[i].p);
                   4619:         ndv_mod(trace,am);
                   4620:       if ( DL_COMPARE(HDL(am),HDL(a)) )
                   4621:         return 0;
                   4622:         ndv_removecont(trace,am);
                   4623:       } else {
                   4624:         a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
                   4625:         if ( !dont_removecont) ndv_removecont(0,a);
                   4626:         register_hcf(a);
                   4627:         am = nd_ps[i] = ndv_dup(mod,a);
                   4628:         ndv_mod(mod,am);
                   4629:       if ( DL_COMPARE(HDL(am),HDL(a)) )
                   4630:         return 0;
                   4631:         ndv_removecont(mod,am);
                   4632:       }
                   4633:     } else {
                   4634:       a = nd_ps[i] = ndv_dup(mod,w[i].p);
                   4635:       if ( mod || !dont_removecont ) ndv_removecont(mod,a);
                   4636:       if ( !mod ) register_hcf(a);
1.1       noro     4637:     }
1.6       noro     4638:     if ( nd_gentrace ) {
                   4639:       STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
                   4640:       /* exact division */
1.1       noro     4641:       if ( !dont_removecont )
1.6       noro     4642:         ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
                   4643:       MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
                   4644:     }
                   4645:     NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
                   4646:     nd_bound[i] = ndv_compute_bound(a);
                   4647:     nd_psh[i] = r;
                   4648:     if ( nd_demand ) {
                   4649:       if ( trace ) {
                   4650:         ndv_save(nd_ps_trace[i],i);
                   4651:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   4652:         nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   4653:         nd_ps_trace[i] = 0;
                   4654:       } else {
                   4655:         ndv_save(nd_ps[i],i);
                   4656:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
                   4657:         nd_ps[i] = 0;
                   4658:       }
1.1       noro     4659:     }
1.6       noro     4660:   }
1.24      noro     4661:   if ( sba ) {
1.27      noro     4662:     nd_sba_hm = (DL *)MALLOC(nd_psn*sizeof(DL));
1.24      noro     4663:    // setup signatures
1.27      noro     4664:     for ( i = 0; i < nd_psn; i++ ) {
                   4665:       SIG sig;
1.24      noro     4666:
1.27      noro     4667:       NEWSIG(sig); sig->pos = i;
                   4668:       nd_ps[i]->sig = sig;
                   4669:       if ( nd_demand ) nd_ps_sym[i]->sig = sig;
                   4670:         nd_psh[i]->sig = sig;
                   4671:       if ( trace ) {
                   4672:         nd_ps_trace[i]->sig = sig;
                   4673:         if ( nd_demand ) nd_ps_trace_sym[i]->sig = sig;
                   4674:       }
                   4675:       NEWDL(nd_sba_hm[i],nd_nvar);
1.46      noro     4676:       if ( nd_sba_modord )
                   4677:         ndv_lm_modord(nd_ps[i],nd_sba_hm[i]);
1.45      noro     4678:       else
                   4679:         _ndltodl(DL(nd_psh[i]),nd_sba_hm[i]);
1.27      noro     4680:     }
1.29      noro     4681:     nd_sba_pos = (NODE *)MALLOC(nd_psn*sizeof(NODE));
                   4682:     for ( i = 0; i < nd_psn; i++ ) {
                   4683:       j = nd_psh[i]->sig->pos;
                   4684:       nd_sba_pos[j] = append_one(nd_sba_pos[j],i);
                   4685:     }
1.24      noro     4686:   }
1.6       noro     4687:   if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
                   4688:   return 1;
1.1       noro     4689: }
                   4690:
                   4691: struct order_spec *append_block(struct order_spec *spec,
                   4692:     int nv,int nalg,int ord);
                   4693:
                   4694: extern VECT current_dl_weight_vector_obj;
                   4695: static VECT prev_weight_vector_obj;
                   4696:
                   4697: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
                   4698:     struct order_spec **ord1p,LIST *f1p,NODE *alistp)
                   4699: {
                   4700:     NODE alist,t,s,r0,r,arg;
                   4701:     VL tv;
                   4702:     P poly;
                   4703:     DP d;
                   4704:     Alg alpha,dp;
                   4705:     DAlg inv,da,hc;
                   4706:     MP m;
                   4707:     int i,nvar,nalg,n;
                   4708:     NumberField nf;
                   4709:     LIST f1,f2;
                   4710:     struct order_spec *current_spec;
                   4711:     VECT obj,obj0;
                   4712:     VECT tmp;
                   4713:
                   4714:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
                   4715:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
                   4716:
                   4717:     for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
                   4718:         NEXTNODE(alist,t); MKV(tv->v,poly);
                   4719:         MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
                   4720:         tv->v = tv->v->priv;
                   4721:     }
                   4722:     NEXT(t) = 0;
                   4723:
                   4724:     /* simplification, making polynomials monic */
                   4725:     setfield_dalg(alist);
                   4726:     obj_algtodalg((Obj)f,(Obj *)&f1);
                   4727:     for ( t = BDY(f); t; t = NEXT(t) ) {
                   4728:         initd(ord); ptod(vv,vv,(P)BDY(t),&d);
                   4729:         hc = (DAlg)BDY(d)->c;
                   4730:         if ( NID(hc) == N_DA ) {
                   4731:             invdalg(hc,&inv);
                   4732:             for ( m = BDY(d); m; m = NEXT(m) ) {
                   4733:                 muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
                   4734:             }
                   4735:         }
                   4736:         initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
                   4737:     }
                   4738:     obj_dalgtoalg((Obj)f1,(Obj *)&f);
                   4739:
                   4740:     /* append alg vars to the var list */
                   4741:     for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
                   4742:     NEXT(tv) = av;
                   4743:
                   4744:     /* append a block to ord */
                   4745:     *ord1p = append_block(ord,nvar,nalg,2);
                   4746:
                   4747:     /* create generator list */
                   4748:     nf = get_numberfield();
                   4749:     for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
                   4750:         MKAlg(nf->defpoly[i],dp);
                   4751:         MKNODE(s,dp,t); t = s;
                   4752:     }
                   4753:     MKLIST(f1,t);
                   4754:     *alistp = alist;
                   4755:     algobjtorat((Obj)f1,(Obj *)f1p);
                   4756:
                   4757:     /* creating a new weight vector */
                   4758:     prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
                   4759:     n = nvar+nalg+1;
                   4760:     MKVECT(obj,n);
                   4761:     if ( obj0 && obj0->len == nvar )
                   4762:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
                   4763:     else
                   4764:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
                   4765:     for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
                   4766:     BDY(obj)[n-1] = (pointer)ONE;
                   4767:     arg = mknode(1,obj);
                   4768:     Pdp_set_weight(arg,&tmp);
                   4769: }
                   4770:
                   4771: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
                   4772: {
                   4773:     NODE s,t,u0,u;
                   4774:     P p;
                   4775:     VL tv;
                   4776:     Obj obj;
                   4777:     VECT tmp;
                   4778:     NODE arg;
                   4779:
                   4780:     u0 = 0;
                   4781:     for ( t = r; t; t = NEXT(t) ) {
                   4782:         p = (P)BDY(t);
                   4783:         for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
                   4784:             substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
                   4785:         }
                   4786:         if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
                   4787:             NEXTNODE(u0,u);
                   4788:             BDY(u) = (pointer)p;
                   4789:         }
                   4790:     }
                   4791:     arg = mknode(1,prev_weight_vector_obj);
                   4792:     Pdp_set_weight(arg,&tmp);
                   4793:
                   4794:     return u0;
                   4795: }
                   4796:
                   4797: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
                   4798: {
                   4799:     VL tv,fv,vv,vc,av;
                   4800:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4801:     int e,max,nvar,i;
                   4802:     NDV b;
                   4803:     int ishomo,nalg,mrank,trank,wmax,len;
                   4804:   NMV a;
                   4805:     Alg alpha,dp;
                   4806:     P p,zp;
                   4807:     Q dmy;
                   4808:     LIST f1,f2,zpl;
                   4809:     Obj obj;
                   4810:     NumberField nf;
                   4811:     struct order_spec *ord1;
                   4812:     NODE tr,tl1,tl2,tl3,tl4,nzlist;
                   4813:     LIST l1,l2,l3,l4,l5;
                   4814:   int j;
                   4815:   Z jq,bpe,last_nonzero;
                   4816:     int *perm;
                   4817:     EPOS oepos;
                   4818:     int obpe,oadv,ompos,cbpe;
1.15      noro     4819:     VECT hvect;
1.1       noro     4820:
1.41      noro     4821:     NcriB = NcriMF = Ncri2 = 0;
1.1       noro     4822:     nd_module = 0;
                   4823:     if ( !m && Demand ) nd_demand = 1;
                   4824:     else nd_demand = 0;
                   4825:
                   4826:     if ( DP_Multiple )
                   4827:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4828: #if 0
                   4829:     ndv_alloc = 0;
                   4830: #endif
                   4831:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     4832:     parse_nd_option(vv,current_option);
1.1       noro     4833:     if ( m && nd_vc )
                   4834:        error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
                   4835:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4836:     switch ( ord->id ) {
                   4837:         case 1:
                   4838:             if ( ord->nv != nvar )
                   4839:                 error("nd_{gr,f4} : invalid order specification");
                   4840:             break;
                   4841:         default:
                   4842:             break;
                   4843:     }
                   4844:     nd_nalg = 0;
                   4845:     av = 0;
                   4846:     if ( !m ) {
                   4847:         get_algtree((Obj)f,&av);
                   4848:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4849:         nd_ntrans = nvar;
                   4850:         nd_nalg = nalg;
                   4851:         /* #i -> t#i */
                   4852:         if ( nalg ) {
                   4853:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4854:             ord = ord1;
                   4855:             f = f1;
                   4856:         }
                   4857:         nvar += nalg;
                   4858:     }
                   4859:     nd_init_ord(ord);
                   4860:     mrank = 0;
                   4861:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4862:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4863:             if ( nd_module ) {
1.16      noro     4864:                 if ( OID(BDY(t)) == O_DPM ) {
                   4865:                   e = dpm_getdeg((DPM)BDY(t),&trank);
                   4866:                   max = MAX(e,max);
                   4867:                   mrank = MAX(mrank,trank);
                   4868:                 } else {
                   4869:                   s = BDY((LIST)BDY(t));
                   4870:                   trank = length(s);
                   4871:                   mrank = MAX(mrank,trank);
                   4872:                   for ( ; s; s = NEXT(s) ) {
                   4873:                       e = getdeg(tv->v,(P)BDY(s));
                   4874:                       max = MAX(e,max);
                   4875:                   }
1.1       noro     4876:                 }
                   4877:             } else {
1.43      noro     4878:                 if ( OID(BDY(t)) == O_DP ) {
                   4879:                   e = dp_getdeg((DP)BDY(t));
                   4880:                   max = MAX(e,max);
                   4881:                 } else {
                   4882:                   e = getdeg(tv->v,(P)BDY(t));
                   4883:                   max = MAX(e,max);
                   4884:                 }
1.1       noro     4885:             }
                   4886:         }
                   4887:     nd_setup_parameters(nvar,nd_nzlist?0:max);
                   4888:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   4889:     ishomo = 1;
                   4890:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4891:       if ( nd_module ) {
1.16      noro     4892:         if ( OID(BDY(t)) == O_DPM ) {
                   4893:           Z cont;
                   4894:           DPM zdpm;
                   4895:
                   4896:           if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
                   4897:           else zdpm = (DPM)BDY(t);
                   4898:           b = (pointer)dpmtondv(m,zdpm);
                   4899:         } else {
                   4900:           if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   4901:           else zpl = (LIST)BDY(t);
1.1       noro     4902:           b = (pointer)pltondv(CO,vv,zpl);
1.16      noro     4903:         }
1.1       noro     4904:       } else {
1.43      noro     4905:         if ( OID(BDY(t)) == O_DP ) {
                   4906:           DP zdp;
                   4907:
                   4908:           if ( !m && !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
                   4909:           else zdp = (DP)BDY(t);
                   4910:           b = (pointer)dptondv(m,zdp);
                   4911:         } else {
                   4912:           if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   4913:           else zp = (P)BDY(t);
                   4914:           b = (pointer)ptondv(CO,vv,zp);
                   4915:         }
1.1       noro     4916:       }
                   4917:       if ( ishomo )
                   4918:         ishomo = ishomo && ndv_ishomo(b);
                   4919:       if ( m ) ndv_mod(m,b);
                   4920:       if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4921:     }
                   4922:     if ( fd0 ) NEXT(fd) = 0;
                   4923:
                   4924:   if ( !ishomo && homo ) {
                   4925:         for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   4926:             b = (NDV)BDY(t); len = LEN(b);
                   4927:             for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   4928:                 wmax = MAX(TD(DL(a)),wmax);
                   4929:         }
                   4930:         homogenize_order(ord,nvar,&ord1);
                   4931:         nd_init_ord(ord1);
                   4932:         nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   4933:         for ( t = fd0; t; t = NEXT(t) )
                   4934:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   4935:     }
                   4936:
1.24      noro     4937:     ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0,0);
1.1       noro     4938:     if ( nd_gentrace ) {
                   4939:         MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   4940:     }
                   4941:   if ( nd_splist ) {
                   4942:     *rp = compute_splist();
                   4943:     return;
                   4944:   }
                   4945:   if ( nd_check_splist ) {
                   4946:         if ( f4 ) {
                   4947:             if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
                   4948:             else *rp = 0;
                   4949:         } else {
                   4950:             if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
                   4951:             else *rp = 0;
                   4952:         }
                   4953:     return;
                   4954:   }
                   4955:     x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
                   4956:   if ( !x ) {
                   4957:     *rp = 0; return;
                   4958:   }
1.15      noro     4959:   if ( nd_gentrace ) {
                   4960:     MKVECT(hvect,nd_psn);
                   4961:     for ( i = 0; i < nd_psn; i++ )
                   4962:        ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   4963:   }
1.1       noro     4964:   if ( !ishomo && homo ) {
                   4965:        /* dehomogenization */
                   4966:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   4967:     nd_init_ord(ord);
                   4968:     nd_setup_parameters(nvar,0);
                   4969:   }
                   4970:     nd_demand = 0;
                   4971:   if ( nd_module && nd_intersect ) {
                   4972:     for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17      noro     4973:       if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1       noro     4974:         MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
                   4975:       }
                   4976:     conv_ilist(nd_demand,0,x,0);
                   4977:     goto FINAL;
                   4978:   }
                   4979:     if ( nd_gentrace  && f4 ) { nzlist = nd_alltracelist; }
                   4980:     x = ndv_reducebase(x,perm);
                   4981:     if ( nd_gentrace  && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
                   4982:     x = ndv_reduceall(m,x);
                   4983:     cbpe = nd_bpe;
                   4984:     if ( nd_gentrace && !f4 ) {
                   4985:         tl2 = nd_alltracelist; nd_alltracelist = 0;
                   4986:         ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
                   4987:         tl3 = nd_alltracelist; nd_alltracelist = 0;
                   4988:         if ( nd_gensyz ) {
                   4989:           nd_gb(m,0,1,1,0);
                   4990:             tl4 = nd_alltracelist; nd_alltracelist = 0;
                   4991:         } else tl4 = 0;
                   4992:     }
                   4993:     nd_bpe = cbpe;
                   4994:     nd_setup_parameters(nd_nvar,0);
                   4995: FINAL:
                   4996:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16      noro     4997:       NEXTNODE(r0,r);
                   4998:       if ( nd_module ) {
                   4999:         if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
                   5000:         else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
                   5001:       } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
                   5002:       else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1       noro     5003:     }
                   5004:     if ( r0 ) NEXT(r) = 0;
                   5005:     if ( !m && nd_nalg )
                   5006:         r0 = postprocess_algcoef(av,alist,r0);
                   5007:     MKLIST(*rp,r0);
                   5008:     if ( nd_gentrace ) {
                   5009:   if ( f4 ) {
1.6       noro     5010:             STOZ(16,bpe);
                   5011:             STOZ(nd_last_nonzero,last_nonzero);
1.15      noro     5012:             tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1       noro     5013:         } else {
                   5014:             tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   5015:             tl3 = reverse_node(tl3);
                   5016:             /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   5017:             for ( t = tl2; t; t = NEXT(t) ) {
                   5018:             /* s = [i,[*,j,*,*],...] */
                   5019:                 s = BDY((LIST)BDY(t));
1.6       noro     5020:                 j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     5021:                 for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     5022:                     j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     5023:                     ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   5024:                 }
                   5025:             }
                   5026:             for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6       noro     5027:                 STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     5028:             }
                   5029:             MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   5030:             MKLIST(l5,tl4);
1.6       noro     5031:             STOZ(nd_bpe,bpe);
1.15      noro     5032:             tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     5033:         }
                   5034:     }
                   5035: #if 0
                   5036:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   5037: #endif
                   5038: }
                   5039:
1.28      noro     5040: NODE nd_sba_f4(int m,int **indp);
                   5041:
                   5042: void nd_sba(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.24      noro     5043: {
                   5044:   VL tv,fv,vv,vc,av;
1.44      noro     5045:   NODE fd,fd0,r,r0,t,x,s,xx,nd,syz;
1.24      noro     5046:   int e,max,nvar,i;
                   5047:   NDV b;
                   5048:   int ishomo,nalg,wmax,len;
                   5049:   NMV a;
                   5050:   P p,zp;
                   5051:   Q dmy;
                   5052:   struct order_spec *ord1;
                   5053:   int j;
                   5054:   int *perm;
                   5055:   EPOS oepos;
                   5056:   int obpe,oadv,ompos,cbpe;
1.51    ! noro     5057:   struct oEGT eg0,eg1,egconv,egintred;
1.24      noro     5058:
                   5059:   nd_module = 0;
                   5060:   nd_demand = 0;
1.35      noro     5061:   Nsamesig = 0;
1.24      noro     5062:   if ( DP_Multiple )
                   5063:     nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   5064:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     5065:   parse_nd_option(vv,current_option);
1.24      noro     5066:   if ( m && nd_vc )
                   5067:     error("nd_sba : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
                   5068:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5069:   switch ( ord->id ) {
                   5070:     case 1:
                   5071:       if ( ord->nv != nvar )
                   5072:         error("nd_sba : invalid order specification");
                   5073:         break;
                   5074:       default:
                   5075:         break;
                   5076:   }
                   5077:   nd_nalg = 0;
                   5078:   nd_init_ord(ord);
                   5079:   // for SIG comparison
                   5080:   initd(ord);
                   5081:   for ( t = BDY(f), max = 1; t; t = NEXT(t) ) {
                   5082:     for ( tv = vv; tv; tv = NEXT(tv) ) {
1.43      noro     5083:       if ( OID(BDY(t)) == O_DP ) {
                   5084:         e = dp_getdeg((DP)BDY(t));
                   5085:         max = MAX(e,max);
                   5086:       } else {
                   5087:         e = getdeg(tv->v,(P)BDY(t));
                   5088:         max = MAX(e,max);
                   5089:       }
1.24      noro     5090:     }
                   5091:   }
1.25      noro     5092:   nd_setup_parameters(nvar,max);
1.24      noro     5093:   obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   5094:   ishomo = 1;
                   5095:   for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.43      noro     5096:     if ( OID(BDY(t)) == O_DP ) {
                   5097:       DP zdp;
                   5098:
                   5099:       if ( !m ) dp_ptozp((DP)BDY(t),&zdp);
                   5100:       else zdp = (DP)BDY(t);
                   5101:       b = (pointer)dptondv(m,zdp);
                   5102:     } else {
                   5103:       if ( !m ) ptozp((P)BDY(t),1,&dmy,&zp);
                   5104:       else zp = (P)BDY(t);
                   5105:       b = (pointer)ptondv(CO,vv,zp);
                   5106:     }
1.24      noro     5107:     if ( ishomo )
                   5108:       ishomo = ishomo && ndv_ishomo(b);
                   5109:     if ( m ) ndv_mod(m,b);
                   5110:     if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   5111:   }
                   5112:   if ( fd0 ) NEXT(fd) = 0;
                   5113:
                   5114:   if ( !ishomo && homo ) {
                   5115:     for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   5116:       b = (NDV)BDY(t); len = LEN(b);
                   5117:       for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   5118:         wmax = MAX(TD(DL(a)),wmax);
                   5119:       }
                   5120:       homogenize_order(ord,nvar,&ord1);
                   5121:       nd_init_ord(ord1);
1.42      noro     5122:       // for SIG comparison
                   5123:       initd(ord1);
1.24      noro     5124:       nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   5125:       for ( t = fd0; t; t = NEXT(t) )
                   5126:         ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   5127:   }
                   5128:
1.39      noro     5129:   ndv_setup(m,0,fd0,nd_sba_dontsort,0,1);
1.44      noro     5130:   x = f4 ? nd_sba_f4(m,&perm) : nd_sba_buch(m,ishomo || homo,&perm,&syz);
1.24      noro     5131:   if ( !x ) {
                   5132:     *rp = 0; return;
                   5133:   }
                   5134:   if ( !ishomo && homo ) {
                   5135:        /* dehomogenization */
                   5136:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   5137:     nd_init_ord(ord);
1.42      noro     5138:     // for SIG comparison
                   5139:     initd(ord);
1.24      noro     5140:     nd_setup_parameters(nvar,0);
                   5141:   }
                   5142:   nd_demand = 0;
1.51    ! noro     5143:   get_eg(&eg0);
1.24      noro     5144:   x = ndv_reducebase(x,perm);
                   5145:   x = ndv_reduceall(m,x);
1.51    ! noro     5146:   get_eg(&eg1); init_eg(&egintred); add_eg(&egintred,&eg0,&eg1);
1.24      noro     5147:   nd_setup_parameters(nd_nvar,0);
1.29      noro     5148:   get_eg(&eg0);
1.24      noro     5149:   for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   5150:     NEXTNODE(r0,r);
                   5151:     if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
1.34      noro     5152:     else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.24      noro     5153:   }
                   5154:   if ( r0 ) NEXT(r) = 0;
1.44      noro     5155:   if ( nd_sba_syz ) {
                   5156:     LIST gb,hsyz;
                   5157:     NODE nd;
                   5158:
                   5159:     MKLIST(gb,r0);
                   5160:     MKLIST(hsyz,syz);
                   5161:     nd = mknode(2,gb,hsyz);
                   5162:     MKLIST(*rp,nd);
                   5163:   } else
                   5164:     MKLIST(*rp,r0);
1.29      noro     5165:   get_eg(&eg1); init_eg(&egconv); add_eg(&egconv,&eg0,&eg1);
1.51    ! noro     5166:   print_eg("intred",&egintred); fprintf(asir_out,"\n");
1.29      noro     5167:   print_eg("conv",&egconv); fprintf(asir_out,"\n");
1.24      noro     5168: }
                   5169:
1.1       noro     5170: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
                   5171: {
                   5172:     VL tv,fv,vv,vc,av;
                   5173:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   5174:     int e,max,nvar,i;
                   5175:     NDV b;
                   5176:     int ishomo,nalg;
                   5177:     Alg alpha,dp;
                   5178:     P p,zp;
                   5179:     Q dmy;
                   5180:     LIST f1,f2;
                   5181:     Obj obj;
                   5182:     NumberField nf;
                   5183:     struct order_spec *ord1;
                   5184:     int *perm;
                   5185:
                   5186:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     5187:     parse_nd_option(vv,current_option);
1.1       noro     5188:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5189:     switch ( ord->id ) {
                   5190:         case 1:
                   5191:             if ( ord->nv != nvar )
                   5192:                 error("nd_check : invalid order specification");
                   5193:             break;
                   5194:         default:
                   5195:             break;
                   5196:     }
                   5197:     nd_nalg = 0;
                   5198:     av = 0;
                   5199:     if ( !m ) {
                   5200:         get_algtree((Obj)f,&av);
                   5201:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   5202:         nd_ntrans = nvar;
                   5203:         nd_nalg = nalg;
                   5204:         /* #i -> t#i */
                   5205:         if ( nalg ) {
                   5206:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   5207:             ord = ord1;
                   5208:             f = f1;
                   5209:         }
                   5210:         nvar += nalg;
                   5211:     }
                   5212:     nd_init_ord(ord);
                   5213:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   5214:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   5215:             e = getdeg(tv->v,(P)BDY(t));
                   5216:             max = MAX(e,max);
                   5217:         }
                   5218:     nd_setup_parameters(nvar,max);
                   5219:     ishomo = 1;
                   5220:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   5221:         ptozp((P)BDY(t),1,&dmy,&zp);
                   5222:         b = (pointer)ptondv(CO,vv,zp);
                   5223:         if ( ishomo )
                   5224:             ishomo = ishomo && ndv_ishomo(b);
                   5225:         if ( m ) ndv_mod(m,b);
                   5226:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   5227:     }
                   5228:     if ( fd0 ) NEXT(fd) = 0;
1.24      noro     5229:     ndv_setup(m,0,fd0,0,1,0);
1.1       noro     5230:     for ( x = 0, i = 0; i < nd_psn; i++ )
                   5231:         x = update_base(x,i);
                   5232:     if ( do_check ) {
                   5233:         x = nd_gb(m,ishomo,1,0,&perm);
                   5234:         if ( !x ) {
                   5235:             *rp = 0;
                   5236:             return;
                   5237:         }
                   5238:     } else {
                   5239: #if 0
                   5240:     /* bug ? */
                   5241:         for ( t = x; t; t = NEXT(t) )
                   5242:             BDY(t) = (pointer)nd_ps[(long)BDY(t)];
                   5243: #else
                   5244:     conv_ilist(0,0,x,&perm);
                   5245: #endif
                   5246:     }
                   5247:     x = ndv_reducebase(x,perm);
                   5248:     x = ndv_reduceall(m,x);
                   5249:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   5250:         NEXTNODE(r0,r);
                   5251:         BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   5252:     }
                   5253:     if ( r0 ) NEXT(r) = 0;
                   5254:     if ( !m && nd_nalg )
                   5255:         r0 = postprocess_algcoef(av,alist,r0);
                   5256:     MKLIST(*rp,r0);
                   5257: }
                   5258:
                   5259: NDV recompute_trace(NODE trace,NDV *p,int m);
                   5260: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
                   5261:
                   5262: NDV recompute_trace(NODE ti,NDV *p,int mod)
                   5263: {
                   5264:   int c,c1,c2,i;
                   5265:   NM mul,m,tail;
                   5266:   ND d,r,rm;
                   5267:   NODE sj;
                   5268:   NDV red;
                   5269:   Obj mj;
                   5270:
                   5271:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   5272:   CM(mul) = 1;
                   5273:   tail = 0;
                   5274:   for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
                   5275:     sj = BDY((LIST)BDY(ti));
                   5276:     if ( ARG0(sj) ) {
1.6       noro     5277:       red = p[ZTOS((Q)ARG1(sj))];
1.1       noro     5278:       mj = (Obj)ARG2(sj);
                   5279:       if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
                   5280:       else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
                   5281:       rm = ndv_mul_nm(mod,mul,red);
                   5282:       if ( !r ) r = rm;
                   5283:       else {
                   5284:         for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
                   5285:           if ( d ) {
                   5286:             NEXT(tail) = m; tail = m; LEN(d)++;
                   5287:           } else {
                   5288:             MKND(nd_nvar,m,1,d); tail = BDY(d);
                   5289:           }
                   5290:         }
                   5291:         if ( !m ) return 0; /* failure */
                   5292:                 else {
                   5293:           BDY(r) = m;
                   5294:                     if ( mod > 0 || mod == -1 ) {
                   5295:             c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
                   5296:             DMAR(c1,c2,0,mod,c);
                   5297:             nd_mul_c(mod,rm,c);
                   5298:                     } else {
                   5299:                       Z t,u;
                   5300:
                   5301:                       chsgnlf(HCZ(r),&t);
                   5302:                       divlf(t,HCZ(rm),&u);
                   5303:                       nd_mul_c_lf(rm,u);
                   5304:                     }
                   5305:           r = nd_add(mod,r,rm);
                   5306:         }
                   5307:           }
                   5308:      }
                   5309:   }
                   5310:   if ( tail ) NEXT(tail) = 0;
                   5311:   d = nd_add(mod,d,r);
                   5312:   nd_mul_c(mod,d,invm(HCM(d),mod));
                   5313:   return ndtondv(mod,d);
                   5314: }
                   5315:
                   5316: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
                   5317: {
                   5318:     VL tv,fv,vv,vc,av;
                   5319:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   5320:     int e,max,nvar,i;
                   5321:     NDV b;
                   5322:     int ishomo,nalg;
                   5323:     Alg alpha,dp;
                   5324:     P p,zp;
                   5325:     Q dmy;
                   5326:     LIST f1,f2;
                   5327:     Obj obj;
                   5328:     NumberField nf;
                   5329:     struct order_spec *ord1;
                   5330:   NODE permtrace,intred,ind,perm,trace,ti;
                   5331:   int len,n,j;
                   5332:   NDV *db,*pb;
                   5333:
                   5334:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     5335:     parse_nd_option(vv,current_option);
1.1       noro     5336:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5337:     switch ( ord->id ) {
                   5338:         case 1:
                   5339:             if ( ord->nv != nvar )
                   5340:                 error("nd_check : invalid order specification");
                   5341:             break;
                   5342:         default:
                   5343:             break;
                   5344:     }
                   5345:     nd_init_ord(ord);
1.6       noro     5346:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     5347:     nd_setup_parameters(nvar,0);
                   5348:
                   5349:   len = length(BDY(f));
                   5350:   db = (NDV *)MALLOC(len*sizeof(NDV *));
                   5351:   for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
                   5352:       ptozp((P)BDY(t),1,&dmy,&zp);
                   5353:       b = ptondv(CO,vv,zp);
                   5354:         ndv_mod(m,b);
                   5355:     ndv_mul_c(m,b,invm(HCM(b),m));
                   5356:     db[i] = b;
                   5357:     }
                   5358:
                   5359:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   5360:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   5361:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   5362:   perm = BDY((LIST)ARG0(permtrace));
                   5363:   trace = NEXT(permtrace);
                   5364:
                   5365:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     5366:     j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1       noro     5367:     if ( j > i ) i = j;
                   5368:   }
                   5369:   n = i+1;
                   5370:   pb = (NDV *)MALLOC(n*sizeof(NDV *));
                   5371:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   5372:     ti = BDY((LIST)BDY(t));
1.6       noro     5373:     pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1       noro     5374:   }
                   5375:   for ( t = trace; t; t = NEXT(t) ) {
                   5376:     ti = BDY((LIST)BDY(t));
1.6       noro     5377:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   5378:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     5379:       if ( DP_Print ) {
                   5380:            fprintf(asir_out,"."); fflush(asir_out);
                   5381:       }
                   5382:   }
                   5383:   for ( t = intred; t; t = NEXT(t) ) {
                   5384:     ti = BDY((LIST)BDY(t));
1.6       noro     5385:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   5386:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     5387:       if ( DP_Print ) {
                   5388:            fprintf(asir_out,"*"); fflush(asir_out);
                   5389:       }
                   5390:   }
                   5391:     for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
                   5392:         NEXTNODE(r0,r);
1.6       noro     5393:     b = pb[ZTOS((Q)BDY(t))];
1.1       noro     5394:         ndv_mul_c(m,b,invm(HCM(b),m));
                   5395: #if 0
1.6       noro     5396:         BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1       noro     5397: #else
1.6       noro     5398:         BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1       noro     5399: #endif
                   5400:     }
                   5401:     if ( r0 ) NEXT(r) = 0;
                   5402:     MKLIST(*rp,r0);
                   5403:     if ( DP_Print ) fprintf(asir_out,"\n");
                   5404: }
                   5405:
1.16      noro     5406: 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     5407: {
                   5408:     VL tv,fv,vv,vc,av;
                   5409:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   5410:     int m,nocheck,nvar,mindex,e,max;
                   5411:     NDV c;
                   5412:     NMV a;
                   5413:     P p,zp;
                   5414:     Q dmy;
                   5415:     EPOS oepos;
                   5416:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   5417:     Alg alpha,dp;
                   5418:     P poly;
                   5419:     LIST f1,f2,zpl;
                   5420:     Obj obj;
                   5421:     NumberField nf;
                   5422:     struct order_spec *ord1;
                   5423:     struct oEGT eg_check,eg0,eg1;
                   5424:     NODE tr,tl1,tl2,tl3,tl4;
                   5425:     LIST l1,l2,l3,l4,l5;
                   5426:     int *perm;
                   5427:     int j,ret;
                   5428:     Z jq,bpe;
1.15      noro     5429:     VECT hvect;
1.1       noro     5430:
1.41      noro     5431:     NcriB = NcriMF = Ncri2 = 0;
1.1       noro     5432:     nd_module = 0;
                   5433:     nd_lf = 0;
1.46      noro     5434:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   5435:     parse_nd_option(vv,current_option);
1.1       noro     5436:     if ( nd_lf ) {
                   5437:       if ( f4 )
                   5438:         nd_f4_lf_trace(f,v,trace,homo,ord,rp);
                   5439:       else
                   5440:         error("nd_gr_trace is not implemented yet over a large finite field");
                   5441:       return;
                   5442:     }
                   5443:     if ( DP_Multiple )
                   5444:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   5445:
                   5446:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5447:     switch ( ord->id ) {
                   5448:         case 1:
                   5449:             if ( ord->nv != nvar )
                   5450:                 error("nd_gr_trace : invalid order specification");
                   5451:             break;
                   5452:         default:
                   5453:             break;
                   5454:     }
                   5455:
                   5456:     get_algtree((Obj)f,&av);
                   5457:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   5458:     nd_ntrans = nvar;
                   5459:     nd_nalg = nalg;
                   5460:     /* #i -> t#i */
                   5461:     if ( nalg ) {
                   5462:         preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   5463:         ord = ord1;
                   5464:         f = f1;
                   5465:     }
                   5466:     nvar += nalg;
                   5467:
                   5468:     nocheck = 0;
                   5469:     mindex = 0;
                   5470:
                   5471:     if ( Demand ) nd_demand = 1;
                   5472:     else nd_demand = 0;
                   5473:
                   5474:     /* setup modulus */
                   5475:     if ( trace < 0 ) {
                   5476:         trace = -trace;
                   5477:         nocheck = 1;
                   5478:     }
                   5479:     m = trace > 1 ? trace : get_lprime(mindex);
                   5480:     nd_init_ord(ord);
                   5481:     mrank = 0;
                   5482:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   5483:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   5484:             if ( nd_module ) {
1.16      noro     5485:               if ( OID(BDY(t)) == O_DPM ) {
                   5486:                 e = dpm_getdeg((DPM)BDY(t),&trank);
                   5487:                 max = MAX(e,max);
                   5488:                 mrank = MAX(mrank,trank);
                   5489:               } else {
1.1       noro     5490:                 s = BDY((LIST)BDY(t));
                   5491:                 trank = length(s);
                   5492:                 mrank = MAX(mrank,trank);
                   5493:                 for ( ; s; s = NEXT(s) ) {
                   5494:                     e = getdeg(tv->v,(P)BDY(s));
                   5495:                     max = MAX(e,max);
                   5496:                 }
1.16      noro     5497:               }
1.1       noro     5498:             } else {
1.43      noro     5499:               if ( OID(BDY(t)) == O_DP ) {
                   5500:                 e = dp_getdeg((DP)BDY(t));
                   5501:                 max = MAX(e,max);
                   5502:               } else {
1.1       noro     5503:                 e = getdeg(tv->v,(P)BDY(t));
                   5504:                 max = MAX(e,max);
1.43      noro     5505:               }
1.1       noro     5506:             }
                   5507:         }
                   5508:     nd_setup_parameters(nvar,max);
                   5509:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   5510:     ishomo = 1;
                   5511:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   5512:         if ( nd_module ) {
1.16      noro     5513:           if ( OID(BDY(t)) == O_DPM ) {
                   5514:             Z cont;
                   5515:             DPM zdpm;
                   5516:
1.17      noro     5517:             if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16      noro     5518:             else zdpm = (DPM)BDY(t);
                   5519:             c = (pointer)dpmtondv(m,zdpm);
                   5520:           } else {
                   5521:             if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   5522:             else zpl = (LIST)BDY(t);
1.1       noro     5523:             c = (pointer)pltondv(CO,vv,zpl);
1.16      noro     5524:           }
1.1       noro     5525:         } else {
1.43      noro     5526:           if ( OID(BDY(t)) == O_DP ) {
                   5527:             DP zdp;
                   5528:
                   5529:             if ( !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
                   5530:             else zdp = (DP)BDY(t);
                   5531:             c = (pointer)dptondv(m,zdp);
                   5532:           } else {
                   5533:             if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   5534:             else zp = (P)BDY(t);
                   5535:             c = (pointer)ptondv(CO,vv,zp);
                   5536:           }
1.1       noro     5537:         }
                   5538:         if ( ishomo )
                   5539:             ishomo = ishomo && ndv_ishomo(c);
                   5540:         if ( c ) {
                   5541:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   5542:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   5543:         }
                   5544:     }
                   5545:     if ( in0 ) NEXT(in) = 0;
                   5546:     if ( fd0 ) NEXT(fd) = 0;
                   5547:     if ( !ishomo && homo ) {
                   5548:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   5549:             c = (NDV)BDY(t); len = LEN(c);
                   5550:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   5551:                 wmax = MAX(TD(DL(a)),wmax);
                   5552:         }
                   5553:         homogenize_order(ord,nvar,&ord1);
                   5554:         nd_init_ord(ord1);
                   5555:         nd_setup_parameters(nvar+1,wmax);
                   5556:         for ( t = fd0; t; t = NEXT(t) )
                   5557:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   5558:     }
                   5559:     if ( MaxDeg > 0 ) nocheck = 1;
                   5560:     while ( 1 ) {
                   5561:     tl1 = tl2 = tl3 = tl4 = 0;
                   5562:         if ( Demand )
                   5563:             nd_demand = 1;
1.24      noro     5564:         ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1       noro     5565:         if ( nd_gentrace ) {
                   5566:             MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   5567:         }
                   5568:         if ( ret )
                   5569:             cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
                   5570:         if ( !ret || !cand ) {
                   5571:             /* failure */
                   5572:             if ( trace > 1 ) { *rp = 0; return; }
                   5573:             else m = get_lprime(++mindex);
                   5574:             continue;
                   5575:         }
1.15      noro     5576:         if ( nd_gentrace ) {
                   5577:           MKVECT(hvect,nd_psn);
                   5578:           for ( i = 0; i < nd_psn; i++ )
                   5579:              ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   5580:         }
1.1       noro     5581:         if ( !ishomo && homo ) {
                   5582:             /* dehomogenization */
                   5583:             for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   5584:             nd_init_ord(ord);
                   5585:             nd_setup_parameters(nvar,0);
                   5586:         }
                   5587:         nd_demand = 0;
                   5588:         cand = ndv_reducebase(cand,perm);
                   5589:         if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
                   5590:         cand = ndv_reduceall(0,cand);
                   5591:         cbpe = nd_bpe;
                   5592:         if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
                   5593:         get_eg(&eg0);
                   5594:         if ( nocheck )
                   5595:             break;
                   5596:         if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   5597:             if ( nd_gentrace ) {
                   5598:           tl3 = nd_alltracelist; nd_alltracelist = 0;
                   5599:         } else tl3 = 0;
                   5600:             /* gbcheck : cand is a GB of Id(cand) ? */
                   5601:             if ( nd_vc || nd_gentrace || nd_gensyz )
                   5602:               ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
                   5603:             else
                   5604:               ret = nd_f4(0,1,0)!=0;
                   5605:             if ( nd_gentrace && nd_gensyz ) {
                   5606:           tl4 = nd_alltracelist; nd_alltracelist = 0;
                   5607:         } else tl4 = 0;
                   5608:     }
                   5609:     if ( ret ) break;
                   5610:         else if ( trace > 1 ) {
                   5611:             /* failure */
                   5612:             *rp = 0; return;
                   5613:         } else {
                   5614:             /* try the next modulus */
                   5615:             m = get_lprime(++mindex);
                   5616:             /* reset the parameters */
                   5617:             if ( !ishomo && homo ) {
                   5618:                 nd_init_ord(ord1);
                   5619:                 nd_setup_parameters(nvar+1,wmax);
                   5620:             } else {
                   5621:                 nd_init_ord(ord);
                   5622:                 nd_setup_parameters(nvar,max);
                   5623:             }
                   5624:         }
                   5625:     }
                   5626:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   5627:     if ( DP_Print )
1.6       noro     5628:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     5629:     /* dp->p */
                   5630:     nd_bpe = cbpe;
                   5631:     nd_setup_parameters(nd_nvar,0);
                   5632:     for ( r = cand; r; r = NEXT(r) ) {
1.16      noro     5633:       if ( nd_module ) {
1.17      noro     5634:         if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16      noro     5635:         else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17      noro     5636:       } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
                   5637:       else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1       noro     5638:     }
                   5639:     if ( nd_nalg )
                   5640:         cand = postprocess_algcoef(av,alist,cand);
                   5641:     MKLIST(*rp,cand);
                   5642:     if ( nd_gentrace ) {
                   5643:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   5644:     tl3 = reverse_node(tl3);
                   5645:     /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   5646:         for ( t = tl2; t; t = NEXT(t) ) {
                   5647:       /* s = [i,[*,j,*,*],...] */
                   5648:             s = BDY((LIST)BDY(t));
1.6       noro     5649:             j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     5650:       for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     5651:                 j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     5652:         ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   5653:             }
                   5654:     }
                   5655:     for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6       noro     5656:         STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     5657:     }
                   5658:         MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   5659:     MKLIST(l5,tl4);
1.6       noro     5660:       STOZ(nd_bpe,bpe);
1.15      noro     5661:         tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     5662:     }
                   5663: }
                   5664:
                   5665: /* XXX : module element is not considered  */
                   5666:
                   5667: void dltondl(int n,DL dl,UINT *r)
                   5668: {
                   5669:     UINT *d;
                   5670:     int i,j,l,s,ord_l;
                   5671:     struct order_pair *op;
                   5672:
                   5673:     d = (unsigned int *)dl->d;
                   5674:     for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
                   5675:     if ( nd_blockmask ) {
                   5676:         l = nd_blockmask->n;
                   5677:         op = nd_blockmask->order_pair;
                   5678:         for ( j = 0, s = 0; j < l; j++ ) {
                   5679:             ord_l = op[j].length;
                   5680:             for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
                   5681:         }
                   5682:         TD(r) = ndl_weight(r);
                   5683:         ndl_weight_mask(r);
                   5684:     } else {
                   5685:         for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
                   5686:         TD(r) = ndl_weight(r);
                   5687:     }
                   5688: }
                   5689:
                   5690: DL ndltodl(int n,UINT *ndl)
                   5691: {
                   5692:     DL dl;
                   5693:     int *d;
                   5694:     int i,j,l,s,ord_l;
                   5695:     struct order_pair *op;
                   5696:
1.33      noro     5697:     NEWDL_NOINIT(dl,n);
1.1       noro     5698:     dl->td = TD(ndl);
                   5699:     d = dl->d;
                   5700:     if ( nd_blockmask ) {
                   5701:         l = nd_blockmask->n;
                   5702:         op = nd_blockmask->order_pair;
                   5703:         for ( j = 0, s = 0; j < l; j++ ) {
                   5704:             ord_l = op[j].length;
                   5705:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   5706:         }
                   5707:     } else {
                   5708:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   5709:     }
                   5710:     return dl;
                   5711: }
                   5712:
1.24      noro     5713: void _ndltodl(UINT *ndl,DL dl)
                   5714: {
                   5715:     int *d;
                   5716:     int i,j,l,s,ord_l,n;
                   5717:     struct order_pair *op;
                   5718:
                   5719:     n = nd_nvar;
                   5720:     dl->td = TD(ndl);
                   5721:     d = dl->d;
                   5722:     if ( nd_blockmask ) {
                   5723:         l = nd_blockmask->n;
                   5724:         op = nd_blockmask->order_pair;
                   5725:         for ( j = 0, s = 0; j < l; j++ ) {
                   5726:             ord_l = op[j].length;
                   5727:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   5728:         }
                   5729:     } else {
                   5730:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   5731:     }
                   5732: }
                   5733:
1.1       noro     5734: void nmtodp(int mod,NM m,DP *r)
                   5735: {
                   5736:     DP dp;
                   5737:     MP mr;
                   5738:
                   5739:     NEWMP(mr);
                   5740:     mr->dl = ndltodl(nd_nvar,DL(m));
                   5741:     mr->c = (Obj)ndctop(mod,m->c);
                   5742:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   5743:     *r = dp;
                   5744: }
                   5745:
1.15      noro     5746: void ndltodp(UINT *d,DP *r)
                   5747: {
                   5748:     DP dp;
                   5749:     MP mr;
                   5750:
                   5751:     NEWMP(mr);
                   5752:     mr->dl = ndltodl(nd_nvar,d);
                   5753:     mr->c = (Obj)ONE;
                   5754:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   5755:     *r = dp;
                   5756: }
                   5757:
1.1       noro     5758: void ndl_print(UINT *dl)
                   5759: {
                   5760:     int n;
                   5761:     int i,j,l,ord_l,s,s0;
                   5762:     struct order_pair *op;
                   5763:
                   5764:     n = nd_nvar;
                   5765:     printf("<<");
                   5766:     if ( nd_blockmask ) {
                   5767:         l = nd_blockmask->n;
                   5768:         op = nd_blockmask->order_pair;
                   5769:         for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   5770:             ord_l = op[j].length;
                   5771:             for ( i = 0; i < ord_l; i++, s++ )
                   5772:                 printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
                   5773:         }
                   5774:     } else {
                   5775:         for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
                   5776:     }
                   5777:     printf(">>");
                   5778:     if ( nd_module && MPOS(dl) )
                   5779:         printf("*e%d",MPOS(dl));
                   5780: }
                   5781:
                   5782: void nd_print(ND p)
                   5783: {
                   5784:     NM m;
                   5785:
                   5786:     if ( !p )
                   5787:         printf("0\n");
                   5788:     else {
                   5789:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5790:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     5791:             else printf("+%ld*",CM(m));
1.1       noro     5792:             ndl_print(DL(m));
                   5793:         }
                   5794:         printf("\n");
                   5795:     }
                   5796: }
                   5797:
                   5798: void nd_print_q(ND p)
                   5799: {
                   5800:     NM m;
                   5801:
                   5802:     if ( !p )
                   5803:         printf("0\n");
                   5804:     else {
                   5805:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5806:             printf("+");
1.6       noro     5807:             printexpr(CO,(Obj)CZ(m));
1.1       noro     5808:             printf("*");
                   5809:             ndl_print(DL(m));
                   5810:         }
                   5811:         printf("\n");
                   5812:     }
                   5813: }
                   5814:
                   5815: void ndp_print(ND_pairs d)
                   5816: {
                   5817:     ND_pairs t;
                   5818:
                   5819:     for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
                   5820:     printf("\n");
                   5821: }
                   5822:
                   5823: void nd_removecont(int mod,ND p)
                   5824: {
                   5825:     int i,n;
                   5826:     Z *w;
                   5827:     NM m;
                   5828:     struct oVECT v;
                   5829:
                   5830:     if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
                   5831:     else if ( mod == -2 ) {
                   5832:       Z inv;
                   5833:       divlf(ONE,HCZ(p),&inv);
                   5834:       nd_mul_c_lf(p,inv);
                   5835:     } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
                   5836:     else {
                   5837:         for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   5838:         w = (Z *)MALLOC(n*sizeof(Q));
                   5839:         v.len = n;
                   5840:         v.body = (pointer *)w;
1.6       noro     5841:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5842:         removecont_array((P *)w,n,1);
1.6       noro     5843:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5844:     }
                   5845: }
                   5846:
                   5847: void nd_removecont2(ND p1,ND p2)
                   5848: {
                   5849:     int i,n1,n2,n;
                   5850:     Z *w;
                   5851:     NM m;
                   5852:     struct oVECT v;
                   5853:
                   5854:     n1 = nd_length(p1);
                   5855:     n2 = nd_length(p2);
                   5856:     n = n1+n2;
                   5857:     w = (Z *)MALLOC(n*sizeof(Q));
                   5858:     v.len = n;
                   5859:     v.body = (pointer *)w;
                   5860:     i = 0;
                   5861:     if ( p1 )
1.6       noro     5862:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5863:     if ( p2 )
1.6       noro     5864:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5865:     removecont_array((P *)w,n,1);
                   5866:     i = 0;
                   5867:     if ( p1 )
1.6       noro     5868:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5869:     if ( p2 )
1.6       noro     5870:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5871: }
                   5872:
                   5873: void ndv_removecont(int mod,NDV p)
                   5874: {
                   5875:     int i,len,all_p;
                   5876:     Z *c;
                   5877:     P *w;
                   5878:     Z dvr,t;
                   5879:     P g,cont,tp;
                   5880:     NMV m;
                   5881:
                   5882:     if ( mod == -1 )
                   5883:         ndv_mul_c(mod,p,_invsf(HCM(p)));
                   5884:     else if ( mod == -2 ) {
                   5885:       Z inv;
                   5886:       divlf(ONE,HCZ(p),&inv);
                   5887:       ndv_mul_c_lf(p,inv);
                   5888:     } else if ( mod )
                   5889:         ndv_mul_c(mod,p,invm(HCM(p),mod));
                   5890:     else {
                   5891:         len = p->len;
                   5892:         w = (P *)MALLOC(len*sizeof(P));
                   5893:         c = (Z *)MALLOC(len*sizeof(Q));
                   5894:         for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
                   5895:             ptozp(CP(m),1,(Q *)&c[i],&w[i]);
                   5896:             all_p = all_p && !NUM(w[i]);
                   5897:         }
                   5898:         if ( all_p ) {
                   5899:             qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
                   5900:             mulp(nd_vc,(P)dvr,g,&cont);
                   5901:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5902:                 divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
                   5903:             }
                   5904:         } else {
                   5905:             sortbynm((Q *)c,len);
                   5906:             qltozl((Q *)c,len,&dvr);
                   5907:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5908:                 divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
                   5909:             }
                   5910:         }
                   5911:     }
                   5912: }
                   5913:
                   5914: /* koko */
                   5915:
                   5916: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
                   5917: {
                   5918:     int len,i,max;
                   5919:     NMV m,mr0,mr,t;
                   5920:
                   5921:     len = p->len;
1.14      noro     5922:     for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1       noro     5923:         max = MAX(max,TD(DL(m)));
                   5924:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   5925:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   5926:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   5927:     t = (NMV)MALLOC(nmv_adv);
                   5928:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   5929:         ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6       noro     5930:         CZ(mr) = CZ(m);
1.1       noro     5931:         ndl_copy(DL(t),DL(mr));
                   5932:     }
                   5933:     NV(p)++;
                   5934:     BDY(p) = mr0;
                   5935: }
                   5936:
                   5937: void ndv_dehomogenize(NDV p,struct order_spec *ord)
                   5938: {
                   5939:     int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
                   5940:     int pos;
                   5941:     Q *w;
                   5942:     Q dvr,t;
                   5943:     NMV m,r;
                   5944:
                   5945:     len = p->len;
                   5946:     newnvar = nd_nvar-1;
                   5947:     newexporigin = nd_get_exporigin(ord);
                   5948:     if ( nd_module ) newmpos = newexporigin-1;
                   5949:     newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
                   5950:     for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
                   5951:         ndl_dehomogenize(DL(m));
                   5952:     if ( newwpd != nd_wpd ) {
                   5953:         newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
                   5954:         for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6       noro     5955:             CZ(r) = CZ(m);
1.1       noro     5956:             if ( nd_module ) pos = MPOS(DL(m));
                   5957:             for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   5958:             adj = nd_exporigin-newexporigin;
                   5959:             for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
                   5960:             if ( nd_module ) {
                   5961:                 DL(r)[newmpos] = pos;
                   5962:             }
                   5963:         }
                   5964:     }
                   5965:     NV(p)--;
                   5966: }
                   5967:
                   5968: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
                   5969: {
                   5970:     int i;
                   5971:     P *tpl,*tpl1;
                   5972:     NODE l;
                   5973:     P h,gcd,t;
                   5974:
                   5975:     tpl = (P *)MALLOC(m*sizeof(P));
                   5976:     tpl1 = (P *)MALLOC(m*sizeof(P));
                   5977:     bcopy(pl,tpl,m*sizeof(P));
                   5978:     gcd = (P)ONE;
                   5979:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   5980:         h = (P)BDY(l);
                   5981:         while ( 1 ) {
                   5982:             for ( i = 0; i < m; i++ )
                   5983:                 if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
                   5984:                     break;
                   5985:             if ( i == m ) {
                   5986:                 bcopy(tpl1,tpl,m*sizeof(P));
                   5987:                 mulp(vl,gcd,h,&t); gcd = t;
                   5988:             } else
                   5989:                 break;
                   5990:         }
                   5991:     }
                   5992:     if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
                   5993:     if ( full ) {
                   5994:         heu_nezgcdnpz(vl,tpl,m,&t);
                   5995:         mulp(vl,gcd,t,pr);
                   5996:     } else
                   5997:         *pr = gcd;
                   5998: }
                   5999:
                   6000: void removecont_array(P *p,int n,int full)
                   6001: {
                   6002:     int all_p,all_q,i;
                   6003:     Z *c;
                   6004:     P *w;
                   6005:     P t,s;
                   6006:
                   6007:     for ( all_q = 1, i = 0; i < n; i++ )
                   6008:         all_q = all_q && NUM(p[i]);
                   6009:     if ( all_q ) {
                   6010:         removecont_array_q((Z *)p,n);
                   6011:     } else {
                   6012:         c = (Z *)MALLOC(n*sizeof(Z));
                   6013:         w = (P *)MALLOC(n*sizeof(P));
                   6014:         for ( i = 0; i < n; i++ ) {
                   6015:             ptozp(p[i],1,(Q *)&c[i],&w[i]);
                   6016:         }
                   6017:         removecont_array_q(c,n);
                   6018:         nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
                   6019:         for ( i = 0; i < n; i++ ) {
                   6020:             divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
                   6021:         }
                   6022:     }
                   6023: }
                   6024:
                   6025: /* c is an int array */
                   6026:
                   6027: void removecont_array_q(Z *c,int n)
                   6028: {
                   6029:   struct oVECT v;
                   6030:   Z d0,d1,a,u,u1,gcd;
                   6031:   int i,j;
                   6032:   Z *q,*r;
                   6033:
                   6034:   q = (Z *)MALLOC(n*sizeof(Z));
                   6035:   r = (Z *)MALLOC(n*sizeof(Z));
                   6036:   v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   6037:   gcdvz_estimate(&v,&d0);
                   6038:   for ( i = 0; i < n; i++ ) {
                   6039:     divqrz(c[i],d0,&q[i],&r[i]);
                   6040:   }
                   6041:   for ( i = 0; i < n; i++ ) if ( r[i] ) break;
                   6042:   if ( i < n ) {
                   6043:     v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   6044:     gcdvz(&v,&d1);
                   6045:     gcdz(d0,d1,&gcd);
1.6       noro     6046:     /* exact division */
                   6047:     divsz(d0,gcd,&a);
1.1       noro     6048:     for ( i = 0; i < n; i++ ) {
                   6049:       mulz(a,q[i],&u);
                   6050:       if ( r[i] ) {
1.6       noro     6051:         /* exact division */
                   6052:         divsz(r[i],gcd,&u1);
1.1       noro     6053:         addz(u,u1,&q[i]);
                   6054:       } else
                   6055:         q[i] = u;
                   6056:     }
                   6057:   }
                   6058:   for ( i = 0; i < n; i++ ) c[i] = q[i];
                   6059: }
                   6060:
1.4       noro     6061: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
                   6062:
                   6063: void mpz_removecont_array(mpz_t *c,int n)
                   6064: {
                   6065:   mpz_t d0,a,u,u1,gcd;
                   6066:   int i,j;
1.13      noro     6067:   static mpz_t *q,*r;
                   6068:   static int c_len = 0;
1.4       noro     6069:
                   6070:   for ( i = 0; i < n; i++ )
                   6071:     if ( mpz_sgn(c[i]) ) break;
                   6072:   if ( i == n ) return;
                   6073:   gcdv_mpz_estimate(d0,c,n);
1.13      noro     6074:   if ( n > c_len ) {
                   6075:     q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   6076:     r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   6077:     c_len = n;
                   6078:   }
1.4       noro     6079:   for ( i = 0; i < n; i++ ) {
                   6080:     mpz_init(q[i]); mpz_init(r[i]);
                   6081:     mpz_fdiv_qr(q[i],r[i],c[i],d0);
                   6082:   }
                   6083:   for ( i = 0; i < n; i++ )
                   6084:     if ( mpz_sgn(r[i]) ) break;
                   6085:   mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
                   6086:   if ( i < n ) {
                   6087:     mpz_gcd(gcd,d0,r[i]);
                   6088:     for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
                   6089:     mpz_div(a,d0,gcd);
                   6090:     for ( i = 0; i < n; i++ ) {
                   6091:       mpz_mul(u,a,q[i]);
                   6092:       if ( mpz_sgn(r[i]) ) {
                   6093:         mpz_div(u1,r[i],gcd);
                   6094:         mpz_add(q[i],u,u1);
                   6095:       } else
                   6096:         mpz_set(q[i],u);
                   6097:     }
                   6098:   }
                   6099:   for ( i = 0; i < n; i++ )
                   6100:     mpz_set(c[i],q[i]);
                   6101: }
                   6102:
1.1       noro     6103: void nd_mul_c(int mod,ND p,int mul)
                   6104: {
                   6105:     NM m;
                   6106:     int c,c1;
                   6107:
                   6108:     if ( !p ) return;
                   6109:     if ( mul == 1 ) return;
                   6110:     if ( mod == -1 )
                   6111:         for ( m = BDY(p); m; m = NEXT(m) )
                   6112:             CM(m) = _mulsf(CM(m),mul);
                   6113:     else
                   6114:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   6115:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   6116:         }
                   6117: }
                   6118:
                   6119: void nd_mul_c_lf(ND p,Z mul)
                   6120: {
                   6121:     NM m;
                   6122:     Z c;
                   6123:
                   6124:     if ( !p ) return;
                   6125:     if ( UNIZ(mul) ) return;
                   6126:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   6127:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   6128:     }
                   6129: }
                   6130:
                   6131: void nd_mul_c_q(ND p,P mul)
                   6132: {
                   6133:     NM m;
                   6134:     P c;
                   6135:
                   6136:     if ( !p ) return;
                   6137:     if ( UNIQ(mul) ) return;
                   6138:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   6139:         mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
                   6140:     }
                   6141: }
                   6142:
                   6143: void nd_mul_c_p(VL vl,ND p,P mul)
                   6144: {
                   6145:     NM m;
                   6146:     P c;
                   6147:
                   6148:     if ( !p ) return;
                   6149:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   6150:         mulp(vl,CP(m),mul,&c); CP(m) = c;
                   6151:     }
                   6152: }
                   6153:
                   6154: void nd_free(ND p)
                   6155: {
                   6156:     NM t,s;
                   6157:
                   6158:     if ( !p ) return;
                   6159:     t = BDY(p);
                   6160:     while ( t ) {
                   6161:         s = NEXT(t);
                   6162:         FREENM(t);
                   6163:         t = s;
                   6164:     }
                   6165:     FREEND(p);
                   6166: }
                   6167:
                   6168: void ndv_free(NDV p)
                   6169: {
                   6170:     GCFREE(BDY(p));
                   6171: }
                   6172:
                   6173: void nd_append_red(UINT *d,int i)
                   6174: {
                   6175:     RHist m,m0;
                   6176:     int h;
                   6177:
                   6178:     NEWRHist(m);
                   6179:     h = ndl_hash_value(d);
                   6180:     m->index = i;
                   6181:     ndl_copy(d,DL(m));
                   6182:     NEXT(m) = nd_red[h];
                   6183:     nd_red[h] = m;
                   6184: }
                   6185:
                   6186: UINT *ndv_compute_bound(NDV p)
                   6187: {
                   6188:     UINT *d1,*d2,*t;
                   6189:     UINT u;
                   6190:     int i,j,k,l,len,ind;
                   6191:     NMV m;
                   6192:
                   6193:     if ( !p )
                   6194:         return 0;
                   6195:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6196:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6197:     len = LEN(p);
                   6198:     m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   6199:     for ( i = 1; i < len; i++, NMV_ADV(m) ) {
                   6200:         ndl_max(DL(m),d1,d2);
                   6201:         t = d1; d1 = d2; d2 = t;
                   6202:     }
                   6203:     l = nd_nvar+31;
                   6204:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   6205:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   6206:         u = d1[i];
                   6207:         k = (nd_epw-1)*nd_bpe;
                   6208:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   6209:             t[ind] = (u>>k)&nd_mask0;
                   6210:     }
                   6211:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   6212:     return t;
                   6213: }
                   6214:
                   6215: UINT *nd_compute_bound(ND p)
                   6216: {
                   6217:     UINT *d1,*d2,*t;
                   6218:     UINT u;
                   6219:     int i,j,k,l,len,ind;
                   6220:     NM m;
                   6221:
                   6222:     if ( !p )
                   6223:         return 0;
                   6224:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6225:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6226:     len = LEN(p);
                   6227:     m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   6228:     for ( m = NEXT(m); m; m = NEXT(m) ) {
                   6229:         ndl_lcm(DL(m),d1,d2);
                   6230:         t = d1; d1 = d2; d2 = t;
                   6231:     }
                   6232:     l = nd_nvar+31;
                   6233:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   6234:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   6235:         u = d1[i];
                   6236:         k = (nd_epw-1)*nd_bpe;
                   6237:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   6238:             t[ind] = (u>>k)&nd_mask0;
                   6239:     }
                   6240:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   6241:     return t;
                   6242: }
                   6243:
                   6244: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
                   6245: /* of a term. In this case we need additional 1 word. */
                   6246:
                   6247: int nd_get_exporigin(struct order_spec *ord)
                   6248: {
                   6249:     switch ( ord->id ) {
1.21      noro     6250:         case 0: case 2: case 256: case 258: case 300:
1.1       noro     6251:             return 1+nd_module;
                   6252:         case 1: case 257:
                   6253:             /* block order */
                   6254:             /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
                   6255:             /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
                   6256:             return ord->ord.block.length+1+nd_module;
                   6257:         case 3: case 259:
                   6258: #if 0
                   6259:             error("nd_get_exporigin : composite order is not supported yet.");
                   6260: #else
                   6261:             return 1+nd_module;
                   6262: #endif
                   6263:         default:
                   6264:             error("nd_get_exporigin : ivalid argument.");
                   6265:             return 0;
                   6266:     }
                   6267: }
                   6268:
                   6269: void nd_setup_parameters(int nvar,int max) {
                   6270:     int i,j,n,elen,ord_o,ord_l,l,s,wpd;
                   6271:     struct order_pair *op;
1.45      noro     6272:     extern int CNVars;
1.1       noro     6273:
                   6274:     nd_nvar = nvar;
1.45      noro     6275:     CNVars = nvar;
1.1       noro     6276:     if ( max ) {
                   6277:         /* XXX */
                   6278:         if ( do_weyl ) nd_bpe = 32;
                   6279:         else if ( max < 2 ) nd_bpe = 1;
                   6280:         else if ( max < 4 ) nd_bpe = 2;
                   6281:         else if ( max < 8 ) nd_bpe = 3;
                   6282:         else if ( max < 16 ) nd_bpe = 4;
                   6283:         else if ( max < 32 ) nd_bpe = 5;
                   6284:         else if ( max < 64 ) nd_bpe = 6;
                   6285:         else if ( max < 256 ) nd_bpe = 8;
                   6286:         else if ( max < 1024 ) nd_bpe = 10;
                   6287:         else if ( max < 65536 ) nd_bpe = 16;
                   6288:         else nd_bpe = 32;
                   6289:     }
                   6290:     if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
                   6291:         UINT t;
                   6292:     int st;
                   6293:         int *v;
                   6294:   /* t = max(weights) */
                   6295:         t = 0;
                   6296:         if ( current_dl_weight_vector )
                   6297:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                   6298:                 if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
                   6299:                 if ( t < st ) t = st;
                   6300:             }
                   6301:         if ( nd_matrix )
                   6302:             for ( i = 0; i < nd_matrix_len; i++ )
                   6303:                 for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
                   6304:                     if ( (st=v[j]) < 0 ) st = -st;
                   6305:                     if ( t < st ) t = st;
                   6306:         }
                   6307:         /* i = bitsize of t */
                   6308:         for ( i = 0; t; t >>=1, i++ );
                   6309:         /* i += bitsize of nd_nvar */
                   6310:         for ( t = nd_nvar; t; t >>=1, i++);
                   6311:         /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
                   6312:         if ( (nd_bpe+i) >= 31 )
                   6313:             error("nd_setup_parameters : too large weight");
                   6314:     }
                   6315:     nd_epw = (sizeof(UINT)*8)/nd_bpe;
                   6316:     elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   6317:     nd_exporigin = nd_get_exporigin(nd_ord);
                   6318:     wpd = nd_exporigin+elen;
                   6319:     if ( nd_module )
                   6320:         nd_mpos = nd_exporigin-1;
                   6321:     else
                   6322:         nd_mpos = -1;
                   6323:     if ( wpd != nd_wpd ) {
                   6324:         nd_free_private_storage();
                   6325:         nd_wpd = wpd;
                   6326:     }
                   6327:     if ( nd_bpe < 32 ) {
                   6328:         nd_mask0 = (1<<nd_bpe)-1;
                   6329:     } else {
                   6330:         nd_mask0 = 0xffffffff;
                   6331:     }
                   6332:     bzero(nd_mask,sizeof(nd_mask));
                   6333:     nd_mask1 = 0;
                   6334:     for ( i = 0; i < nd_epw; i++ ) {
                   6335:         nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   6336:         nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   6337:     }
                   6338:     nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
                   6339:     nd_epos = nd_create_epos(nd_ord);
                   6340:     nd_blockmask = nd_create_blockmask(nd_ord);
                   6341:     nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
                   6342: }
                   6343:
                   6344: ND_pairs nd_reconstruct(int trace,ND_pairs d)
                   6345: {
                   6346:     int i,obpe,oadv,h;
                   6347:     static NM prev_nm_free_list;
                   6348:     static ND_pairs prev_ndp_free_list;
                   6349:     RHist mr0,mr;
                   6350:     RHist r;
                   6351:     RHist *old_red;
                   6352:     ND_pairs s0,s,t;
                   6353:     EPOS oepos;
                   6354:
                   6355:     obpe = nd_bpe;
                   6356:     oadv = nmv_adv;
                   6357:     oepos = nd_epos;
                   6358:     if ( obpe < 2 ) nd_bpe = 2;
                   6359:     else if ( obpe < 3 ) nd_bpe = 3;
                   6360:     else if ( obpe < 4 ) nd_bpe = 4;
                   6361:     else if ( obpe < 5 ) nd_bpe = 5;
                   6362:     else if ( obpe < 6 ) nd_bpe = 6;
                   6363:     else if ( obpe < 8 ) nd_bpe = 8;
                   6364:     else if ( obpe < 10 ) nd_bpe = 10;
                   6365:     else if ( obpe < 16 ) nd_bpe = 16;
                   6366:     else if ( obpe < 32 ) nd_bpe = 32;
                   6367:     else error("nd_reconstruct : exponent too large");
                   6368:
                   6369:     nd_setup_parameters(nd_nvar,0);
                   6370:     prev_nm_free_list = _nm_free_list;
                   6371:     prev_ndp_free_list = _ndp_free_list;
                   6372:     _nm_free_list = 0;
                   6373:     _ndp_free_list = 0;
                   6374:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   6375:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   6376:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   6377:     }
                   6378:     if ( trace )
                   6379:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   6380:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   6381:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   6382:         }
                   6383:     s0 = 0;
                   6384:     for ( t = d; t; t = NEXT(t) ) {
                   6385:         NEXTND_pairs(s0,s);
                   6386:         s->i1 = t->i1;
                   6387:         s->i2 = t->i2;
1.24      noro     6388:         s->sig = t->sig;
1.1       noro     6389:         SG(s) = SG(t);
                   6390:         ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   6391:     }
                   6392:
                   6393:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   6394:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   6395:         old_red[i] = nd_red[i];
                   6396:         nd_red[i] = 0;
                   6397:     }
                   6398:     for ( i = 0; i < REDTAB_LEN; i++ )
                   6399:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   6400:             NEWRHist(mr);
                   6401:             mr->index = r->index;
                   6402:             SG(mr) = SG(r);
                   6403:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   6404:             h = ndl_hash_value(DL(mr));
                   6405:             NEXT(mr) = nd_red[h];
                   6406:             nd_red[h] = mr;
1.24      noro     6407:             mr->sig = r->sig;
1.1       noro     6408:         }
                   6409:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   6410:     old_red = 0;
                   6411:     for ( i = 0; i < nd_psn; i++ ) {
                   6412:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   6413:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24      noro     6414:         r->sig = nd_psh[i]->sig;
1.1       noro     6415:         nd_psh[i] = r;
                   6416:     }
                   6417:     if ( s0 ) NEXT(s) = 0;
                   6418:     prev_nm_free_list = 0;
                   6419:     prev_ndp_free_list = 0;
                   6420: #if 0
                   6421:     GC_gcollect();
                   6422: #endif
                   6423:     return s0;
                   6424: }
                   6425:
1.30      noro     6426: void nd_reconstruct_s(int trace,ND_pairs *d)
                   6427: {
                   6428:     int i,obpe,oadv,h;
                   6429:     static NM prev_nm_free_list;
                   6430:     static ND_pairs prev_ndp_free_list;
                   6431:     RHist mr0,mr;
                   6432:     RHist r;
                   6433:     RHist *old_red;
                   6434:     ND_pairs s0,s,t;
                   6435:     EPOS oepos;
                   6436:
                   6437:     obpe = nd_bpe;
                   6438:     oadv = nmv_adv;
                   6439:     oepos = nd_epos;
                   6440:     if ( obpe < 2 ) nd_bpe = 2;
                   6441:     else if ( obpe < 3 ) nd_bpe = 3;
                   6442:     else if ( obpe < 4 ) nd_bpe = 4;
                   6443:     else if ( obpe < 5 ) nd_bpe = 5;
                   6444:     else if ( obpe < 6 ) nd_bpe = 6;
                   6445:     else if ( obpe < 8 ) nd_bpe = 8;
                   6446:     else if ( obpe < 10 ) nd_bpe = 10;
                   6447:     else if ( obpe < 16 ) nd_bpe = 16;
                   6448:     else if ( obpe < 32 ) nd_bpe = 32;
                   6449:     else error("nd_reconstruct_s : exponent too large");
                   6450:
                   6451:     nd_setup_parameters(nd_nvar,0);
                   6452:     prev_nm_free_list = _nm_free_list;
                   6453:     prev_ndp_free_list = _ndp_free_list;
                   6454:     _nm_free_list = 0;
                   6455:     _ndp_free_list = 0;
                   6456:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   6457:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   6458:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   6459:     }
                   6460:     if ( trace )
                   6461:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   6462:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   6463:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   6464:         }
                   6465:
                   6466:     for ( i = 0; i < nd_nbase; i++ ) {
                   6467:       s0 = 0;
                   6468:       for ( t = d[i]; t; t = NEXT(t) ) {
                   6469:           NEXTND_pairs(s0,s);
                   6470:           s->i1 = t->i1;
                   6471:           s->i2 = t->i2;
                   6472:           s->sig = t->sig;
                   6473:           SG(s) = SG(t);
                   6474:           ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   6475:       }
1.47      noro     6476:       if ( s0 ) NEXT(s) = 0;
1.30      noro     6477:       d[i] = s0;
                   6478:     }
                   6479:
                   6480:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   6481:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   6482:         old_red[i] = nd_red[i];
                   6483:         nd_red[i] = 0;
                   6484:     }
                   6485:     for ( i = 0; i < REDTAB_LEN; i++ )
                   6486:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   6487:             NEWRHist(mr);
                   6488:             mr->index = r->index;
                   6489:             SG(mr) = SG(r);
                   6490:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   6491:             h = ndl_hash_value(DL(mr));
                   6492:             NEXT(mr) = nd_red[h];
                   6493:             nd_red[h] = mr;
                   6494:             mr->sig = r->sig;
                   6495:         }
                   6496:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   6497:     old_red = 0;
                   6498:     for ( i = 0; i < nd_psn; i++ ) {
                   6499:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   6500:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
                   6501:         r->sig = nd_psh[i]->sig;
                   6502:         nd_psh[i] = r;
                   6503:     }
                   6504:     if ( s0 ) NEXT(s) = 0;
                   6505:     prev_nm_free_list = 0;
                   6506:     prev_ndp_free_list = 0;
                   6507: #if 0
                   6508:     GC_gcollect();
                   6509: #endif
                   6510: }
                   6511:
1.1       noro     6512: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
                   6513: {
                   6514:     int n,i,ei,oepw,omask0,j,s,ord_l,l;
                   6515:     struct order_pair *op;
                   6516:
                   6517:     n = nd_nvar;
                   6518:     oepw = (sizeof(UINT)*8)/obpe;
                   6519:     omask0 = (1<<obpe)-1;
                   6520:     TD(r) = TD(d);
                   6521:     for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
                   6522:     if ( nd_blockmask ) {
                   6523:         l = nd_blockmask->n;
                   6524:         op = nd_blockmask->order_pair;
                   6525:         for ( i = 1; i < nd_exporigin; i++ )
                   6526:             r[i] = d[i];
                   6527:         for ( j = 0, s = 0; j < l; j++ ) {
                   6528:             ord_l = op[j].length;
                   6529:             for ( i = 0; i < ord_l; i++, s++ ) {
                   6530:                 ei =  GET_EXP_OLD(d,s);
                   6531:                 PUT_EXP(r,s,ei);
                   6532:             }
                   6533:         }
                   6534:     } else {
                   6535:         for ( i = 0; i < n; i++ ) {
                   6536:             ei = GET_EXP_OLD(d,i);
                   6537:             PUT_EXP(r,i,ei);
                   6538:         }
                   6539:     }
                   6540:     if ( nd_module ) MPOS(r) = MPOS(d);
                   6541: }
                   6542:
                   6543: ND nd_copy(ND p)
                   6544: {
                   6545:     NM m,mr,mr0;
                   6546:     int c,n;
                   6547:     ND r;
                   6548:
                   6549:     if ( !p )
                   6550:         return 0;
                   6551:     else {
                   6552:         for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   6553:             NEXTNM(mr0,mr);
                   6554:             CM(mr) = CM(m);
                   6555:             ndl_copy(DL(m),DL(mr));
                   6556:         }
                   6557:         NEXT(mr) = 0;
                   6558:         MKND(NV(p),mr0,LEN(p),r);
                   6559:         SG(r) = SG(p);
                   6560:         return r;
                   6561:     }
                   6562: }
                   6563:
                   6564: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
                   6565: {
                   6566:     NM m1,m2;
                   6567:     NDV p1,p2;
                   6568:     ND t1,t2;
                   6569:     UINT *lcm;
                   6570:     P gp,tp;
                   6571:     Z g,t;
                   6572:     Z iq;
                   6573:     int td;
                   6574:     LIST hist;
                   6575:     NODE node;
                   6576:     DP d;
                   6577:
                   6578:     if ( !mod && nd_demand ) {
                   6579:         p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
                   6580:     } else {
                   6581:         if ( trace ) {
                   6582:             p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   6583:         } else {
                   6584:             p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   6585:         }
                   6586:     }
                   6587:     lcm = LCM(p);
                   6588:     NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
                   6589:     if ( ndl_check_bound2(p->i1,DL(m1)) ) {
                   6590:         FREENM(m1); return 0;
                   6591:     }
                   6592:     NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
                   6593:     if ( ndl_check_bound2(p->i2,DL(m2)) ) {
                   6594:         FREENM(m1); FREENM(m2); return 0;
                   6595:     }
                   6596:
                   6597:     if ( mod == -1 ) {
                   6598:         CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
                   6599:     } else if ( mod > 0 ) {
                   6600:         CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
                   6601:     } else if ( mod == -2 ) {
                   6602:         CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
                   6603:     } else if ( nd_vc ) {
                   6604:         ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
                   6605:         divsp(nd_vc,HCP(p2),gp,&CP(m1));
                   6606:         divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
                   6607:     } else {
1.6       noro     6608:         igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1       noro     6609:     }
                   6610:     t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
                   6611:     *rp = nd_add(mod,t1,t2);
                   6612:     if ( nd_gentrace ) {
                   6613:         /* nd_tracelist is initialized */
1.6       noro     6614:         STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     6615:         MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6       noro     6616:         STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     6617:         MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
                   6618:         nd_tracelist = node;
                   6619:     }
1.24      noro     6620:     if ( *rp )
                   6621:       (*rp)->sig = p->sig;
1.1       noro     6622:     FREENM(m1); FREENM(m2);
                   6623:     return 1;
                   6624: }
                   6625:
                   6626: void ndv_mul_c(int mod,NDV p,int mul)
                   6627: {
                   6628:     NMV m;
                   6629:     int c,c1,len,i;
                   6630:
                   6631:     if ( !p ) return;
                   6632:     len = LEN(p);
                   6633:     if ( mod == -1 )
                   6634:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   6635:             CM(m) = _mulsf(CM(m),mul);
                   6636:     else
                   6637:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6638:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   6639:         }
                   6640: }
                   6641:
                   6642: void ndv_mul_c_lf(NDV p,Z mul)
                   6643: {
                   6644:     NMV m;
                   6645:     Z c;
                   6646:     int len,i;
                   6647:
                   6648:     if ( !p ) return;
                   6649:     len = LEN(p);
                   6650:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6651:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   6652:     }
                   6653: }
                   6654:
                   6655: /* for nd_det */
                   6656: void ndv_mul_c_q(NDV p,Z mul)
                   6657: {
                   6658:     NMV m;
                   6659:     Z c;
                   6660:     int len,i;
                   6661:
                   6662:     if ( !p ) return;
                   6663:     len = LEN(p);
                   6664:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     6665:         mulz(CZ(m),mul,&c); CZ(m) = c;
1.1       noro     6666:     }
                   6667: }
                   6668:
                   6669: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
                   6670:     int n2,i,j,l,n,tlen;
                   6671:     UINT *d0;
                   6672:     NM *tab,*psum;
                   6673:     ND s,r;
                   6674:     NM t;
                   6675:     NMV m1;
                   6676:
                   6677:     if ( !p ) return 0;
                   6678:     n = NV(p); n2 = n>>1;
                   6679:     d0 = DL(m0);
                   6680:     l = LEN(p);
                   6681:     for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   6682:     tab = (NM *)MALLOC(tlen*sizeof(NM));
                   6683:     psum = (NM *)MALLOC(tlen*sizeof(NM));
                   6684:     for ( i = 0; i < tlen; i++ ) psum[i] = 0;
                   6685:     m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   6686:     for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
                   6687:         /* m0(NM) * m1(NMV) => tab(NM) */
                   6688:         weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
                   6689:         for ( j = 0; j < tlen; j++ ) {
                   6690:             if ( tab[j] ) {
                   6691:                 NEXT(tab[j]) = psum[j];    psum[j] = tab[j];
                   6692:             }
                   6693:         }
                   6694:     }
                   6695:     for ( i = tlen-1, r = 0; i >= 0; i-- )
                   6696:         if ( psum[i] ) {
                   6697:             for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   6698:             MKND(n,psum[i],j,s);
                   6699:             r = nd_add(mod,r,s);
                   6700:         }
                   6701:     if ( r ) SG(r) = SG(p)+TD(d0);
                   6702:     return r;
                   6703: }
                   6704:
                   6705: /* product of monomials */
                   6706: /* XXX block order is not handled correctly */
                   6707:
                   6708: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   6709: {
                   6710:     int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
                   6711:     UINT *d0,*d1,*d,*dt,*ctab;
                   6712:     Z *ctab_q;
                   6713:     Z q,q1;
                   6714:     UINT c0,c1,c;
                   6715:     NM *p;
                   6716:     NM m,t;
                   6717:     int mpos;
                   6718:
                   6719:     for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   6720:     if ( !m0 || !m1 ) return;
                   6721:     d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
                   6722:     if ( nd_module )
                   6723:         if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
                   6724:
                   6725:     NEWNM(m); d = DL(m);
                   6726:     if ( mod ) {
                   6727:         c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
                   6728:     } else if ( nd_vc )
                   6729:         mulp(nd_vc,CP(m0),CP(m1),&CP(m));
                   6730:   else
1.6       noro     6731:         mulz(CZ(m0),CZ(m1),&CZ(m));
1.1       noro     6732:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   6733:     homo = n&1 ? 1 : 0;
                   6734:     if ( homo ) {
                   6735:         /* offset of h-degree */
                   6736:         h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   6737:         PUT_EXP(DL(m),n-1,h);
                   6738:         TD(DL(m)) = h;
                   6739:         if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   6740:     }
                   6741:     tab[0] = m;
                   6742:     NEWNM(m); d = DL(m);
                   6743:     for ( i = 0, curlen = 1; i < n2; i++ ) {
                   6744:         a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   6745:         k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   6746:         /* xi^a*(Di^k*xi^l)*Di^b */
                   6747:         a += l; b += k;
                   6748:         s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
                   6749:         if ( !k || !l ) {
                   6750:             for ( j = 0; j < curlen; j++ )
                   6751:                 if ( (t = tab[j]) != 0 ) {
                   6752:                     dt = DL(t);
                   6753:                     PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
                   6754:                     if ( nd_blockmask ) ndl_weight_mask(dt);
                   6755:                 }
                   6756:             curlen *= k+1;
                   6757:             continue;
                   6758:         }
                   6759:         min = MIN(k,l);
                   6760:         if ( mod ) {
                   6761:             ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
                   6762:             mkwcm(k,l,mod,(int *)ctab);
                   6763:         } else {
                   6764:             ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
                   6765:             mkwc(k,l,ctab_q);
                   6766:         }
                   6767:         for ( j = min; j >= 0; j-- ) {
                   6768:             for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
                   6769:             PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
                   6770:             h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
                   6771:             if ( homo ) {
                   6772:                 TD(d) = s;
                   6773:                 PUT_EXP(d,n-1,s-h);
                   6774:             } else TD(d) = h;
                   6775:             if ( nd_blockmask ) ndl_weight_mask(d);
                   6776:             if ( mod ) c = ctab[j];
                   6777:             else q = ctab_q[j];
                   6778:             p = tab+curlen*j;
                   6779:             if ( j == 0 ) {
                   6780:                 for ( u = 0; u < curlen; u++, p++ ) {
                   6781:                     if ( tab[u] ) {
                   6782:                         ndl_addto(DL(tab[u]),d);
                   6783:                         if ( mod ) {
                   6784:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
                   6785:                         } else if ( nd_vc )
                   6786:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
                   6787:             else {
1.6       noro     6788:                             mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1       noro     6789:                         }
                   6790:                     }
                   6791:                 }
                   6792:             } else {
                   6793:                 for ( u = 0; u < curlen; u++, p++ ) {
                   6794:                     if ( tab[u] ) {
                   6795:                         NEWNM(t);
                   6796:                         ndl_add(DL(tab[u]),d,DL(t));
                   6797:                         if ( mod ) {
                   6798:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
                   6799:                         } else if ( nd_vc )
                   6800:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
                   6801:             else
1.6       noro     6802:                             mulz(CZ(tab[u]),q,&CZ(t));
1.1       noro     6803:                         *p = t;
                   6804:                     }
                   6805:                 }
                   6806:             }
                   6807:         }
                   6808:         curlen *= k+1;
                   6809:     }
                   6810:     FREENM(m);
                   6811:     if ( nd_module ) {
                   6812:         mpos = MPOS(d1);
                   6813:         for ( i = 0; i < tlen; i++ )
                   6814:             if ( tab[i] ) {
                   6815:                 d = DL(tab[i]);
                   6816:                 MPOS(d) = mpos;
                   6817:                 TD(d) = ndl_weight(d);
                   6818:             }
                   6819:     }
                   6820: }
                   6821:
                   6822: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   6823: {
                   6824:     NM mr,mr0;
                   6825:     NMV m;
                   6826:     UINT *d,*dt,*dm;
                   6827:     int c,n,td,i,c1,c2,len;
                   6828:     Q q;
                   6829:     ND r;
                   6830:
                   6831:     if ( !p ) return 0;
                   6832:     else {
                   6833:         n = NV(p); m = BDY(p);
                   6834:         d = DL(m0);
                   6835:         len = LEN(p);
                   6836:         mr0 = 0;
                   6837:         td = TD(d);
                   6838:         c = CM(m0);
                   6839:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6840:             NEXTNM(mr0,mr);
                   6841:             CM(mr) = 1;
                   6842:             ndl_add(DL(m),d,DL(mr));
                   6843:         }
                   6844:         NEXT(mr) = 0;
                   6845:         MKND(NV(p),mr0,len,r);
                   6846:         SG(r) = SG(p) + TD(d);
                   6847:         return r;
                   6848:     }
                   6849: }
                   6850:
                   6851: ND ndv_mul_nm(int mod,NM m0,NDV p)
                   6852: {
                   6853:     NM mr,mr0;
                   6854:     NMV m;
                   6855:     UINT *d,*dt,*dm;
                   6856:     int c,n,td,i,c1,c2,len;
                   6857:     P q;
                   6858:     ND r;
                   6859:
                   6860:     if ( !p ) return 0;
                   6861:     else if ( do_weyl ) {
                   6862:         if ( mod < 0 ) {
                   6863:             error("ndv_mul_nm : not implemented (weyl)");
                   6864:             return 0;
                   6865:         } else
                   6866:             return weyl_ndv_mul_nm(mod,m0,p);
                   6867:     } else {
                   6868:         n = NV(p); m = BDY(p);
                   6869:         d = DL(m0);
                   6870:         len = LEN(p);
                   6871:         mr0 = 0;
                   6872:         td = TD(d);
                   6873:         if ( mod == -1 ) {
                   6874:             c = CM(m0);
                   6875:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6876:                 NEXTNM(mr0,mr);
                   6877:                 CM(mr) = _mulsf(CM(m),c);
                   6878:                 ndl_add(DL(m),d,DL(mr));
                   6879:             }
                   6880:         } else if ( mod == -2 ) {
                   6881:             Z cl;
                   6882:             cl = CZ(m0);
                   6883:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6884:                 NEXTNM(mr0,mr);
                   6885:                 mullf(CZ(m),cl,&CZ(mr));
                   6886:                 ndl_add(DL(m),d,DL(mr));
                   6887:             }
                   6888:         } else if ( mod ) {
                   6889:             c = CM(m0);
                   6890:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6891:                 NEXTNM(mr0,mr);
                   6892:                 c1 = CM(m);
                   6893:                 DMAR(c1,c,0,mod,c2);
                   6894:                 CM(mr) = c2;
                   6895:                 ndl_add(DL(m),d,DL(mr));
                   6896:             }
                   6897:         } else {
                   6898:             q = CP(m0);
                   6899:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6900:                 NEXTNM(mr0,mr);
                   6901:                 mulp(nd_vc,CP(m),q,&CP(mr));
                   6902:                 ndl_add(DL(m),d,DL(mr));
                   6903:             }
                   6904:         }
                   6905:         NEXT(mr) = 0;
                   6906:         MKND(NV(p),mr0,len,r);
                   6907:         SG(r) = SG(p) + TD(d);
                   6908:         return r;
                   6909:     }
                   6910: }
                   6911:
                   6912: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
                   6913: {
                   6914:     NM mq0,mq;
                   6915:     NMV tm;
                   6916:     Q q;
                   6917:     int i,nv,sg,c,c1,c2,hindex;
                   6918:     ND p,t,r;
                   6919:
                   6920:     if ( bucket->m < 0 ) return 0;
                   6921:     else {
                   6922:         nv = NV(d);
                   6923:         mq0 = 0;
                   6924:         tm = (NMV)MALLOC(nmv_adv);
                   6925:         while ( 1 ) {
                   6926:             if ( mod > 0 || mod == -1 )
                   6927:               hindex = head_pbucket(mod,bucket);
                   6928:             else if ( mod == -2 )
                   6929:               hindex = head_pbucket_lf(bucket);
                   6930:             else
                   6931:               hindex = head_pbucket_q(bucket);
                   6932:             if ( hindex < 0 ) break;
                   6933:             p = bucket->body[hindex];
                   6934:             NEXTNM(mq0,mq);
                   6935:             ndl_sub(HDL(p),HDL(d),DL(mq));
                   6936:             ndl_copy(DL(mq),DL(tm));
                   6937:             if ( mod ) {
                   6938:                 c1 = invm(HCM(d),mod); c2 = HCM(p);
                   6939:                 DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   6940:                 CM(tm) = mod-c;
                   6941:             } else {
1.6       noro     6942:                 divsz(HCZ(p),HCZ(d),&CZ(mq));
                   6943:                 chsgnz(CZ(mq),&CZ(tm));
1.1       noro     6944:             }
                   6945:             t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
                   6946:             bucket->body[hindex] = nd_remove_head(p);
                   6947:             t = nd_remove_head(t);
                   6948:             add_pbucket(mod,bucket,t);
                   6949:         }
                   6950:         if ( !mq0 )
                   6951:             r = 0;
                   6952:         else {
                   6953:             NEXT(mq) = 0;
                   6954:             for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   6955:             MKND(nv,mq0,i,r);
                   6956:             /* XXX */
                   6957:             SG(r) = HTD(r);
                   6958:         }
                   6959:         return r;
                   6960:     }
                   6961: }
                   6962:
                   6963: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   6964: {
                   6965:     NMV m,mr,mr0,t;
                   6966:     int len,i,k;
                   6967:
                   6968:     if ( !p ) return;
                   6969:     m = BDY(p); len = LEN(p);
                   6970:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   6971:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   6972:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   6973:     t = (NMV)MALLOC(nmv_adv);
                   6974:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6       noro     6975:         CZ(t) = CZ(m);
1.1       noro     6976:         for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   6977:         ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6       noro     6978:         CZ(mr) = CZ(t);
1.1       noro     6979:         ndl_copy(DL(t),DL(mr));
                   6980:     }
                   6981:     BDY(p) = mr0;
                   6982: }
                   6983:
                   6984: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   6985: {
                   6986:     NMV m,mr,mr0;
                   6987:     int len,i;
                   6988:     NDV r;
                   6989:
                   6990:     if ( !p ) return 0;
                   6991:     m = BDY(p); len = LEN(p);
                   6992:     mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   6993:     for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   6994:         ndl_zero(DL(mr));
                   6995:         ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6       noro     6996:         CZ(mr) = CZ(m);
1.1       noro     6997:     }
                   6998:     MKNDV(NV(p),mr0,len,r);
                   6999:     SG(r) = SG(p);
1.24      noro     7000:     r->sig = p->sig;
1.1       noro     7001:     return r;
                   7002: }
                   7003:
                   7004: /* duplicate p */
                   7005:
                   7006: NDV ndv_dup(int mod,NDV p)
                   7007: {
                   7008:     NDV d;
                   7009:     NMV t,m,m0;
                   7010:     int i,len;
                   7011:
                   7012:     if ( !p ) return 0;
                   7013:     len = LEN(p);
                   7014:     m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   7015:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   7016:         ndl_copy(DL(t),DL(m));
1.6       noro     7017:         CZ(m) = CZ(t);
1.1       noro     7018:     }
                   7019:     MKNDV(NV(p),m0,len,d);
                   7020:     SG(d) = SG(p);
                   7021:     return d;
                   7022: }
                   7023:
                   7024: NDV ndv_symbolic(int mod,NDV p)
                   7025: {
                   7026:     NDV d;
                   7027:     NMV t,m,m0;
                   7028:     int i,len;
                   7029:
                   7030:     if ( !p ) return 0;
                   7031:     len = LEN(p);
                   7032:     m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   7033:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   7034:         ndl_copy(DL(t),DL(m));
1.6       noro     7035:         CZ(m) = ONE;
1.1       noro     7036:     }
                   7037:     MKNDV(NV(p),m0,len,d);
                   7038:     SG(d) = SG(p);
                   7039:     return d;
                   7040: }
                   7041:
                   7042: ND nd_dup(ND p)
                   7043: {
                   7044:     ND d;
                   7045:     NM t,m,m0;
                   7046:
                   7047:     if ( !p ) return 0;
                   7048:     for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   7049:         NEXTNM(m0,m);
                   7050:         ndl_copy(DL(t),DL(m));
1.6       noro     7051:         CZ(m) = CZ(t);
1.1       noro     7052:     }
                   7053:     if ( m0 ) NEXT(m) = 0;
                   7054:     MKND(NV(p),m0,LEN(p),d);
                   7055:     SG(d) = SG(p);
                   7056:     return d;
                   7057: }
                   7058:
                   7059: /* XXX if p->len == 0 then it represents 0 */
                   7060:
                   7061: void ndv_mod(int mod,NDV p)
                   7062: {
                   7063:     NMV t,d;
                   7064:     int r,s,u;
                   7065:     int i,len,dlen;
                   7066:     P cp;
                   7067:     Z c;
                   7068:     Obj gfs;
                   7069:
                   7070:     if ( !p ) return;
                   7071:     len = LEN(p);
                   7072:     dlen = 0;
                   7073:     if ( mod == -1 )
                   7074:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   7075:             simp_ff((Obj)CP(t),&gfs);
                   7076:             if ( gfs ) {
                   7077:               r = FTOIF(CONT((GFS)gfs));
                   7078:               CM(d) = r;
                   7079:               ndl_copy(DL(t),DL(d));
                   7080:               NMV_ADV(d);
                   7081:               dlen++;
                   7082:             }
                   7083:         }
                   7084:     else if ( mod == -2 )
                   7085:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   7086:             simp_ff((Obj)CP(t),&gfs);
                   7087:             if ( gfs ) {
                   7088:               lmtolf((LM)gfs,&CZ(d));
                   7089:               ndl_copy(DL(t),DL(d));
                   7090:               NMV_ADV(d);
                   7091:               dlen++;
                   7092:             }
                   7093:         }
                   7094:     else
                   7095:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   7096:             if ( nd_vc ) {
                   7097:                 nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
                   7098:                 c = (Z)cp;
                   7099:             } else
1.6       noro     7100:                 c = CZ(t);
1.1       noro     7101:             r = remqi((Q)c,mod);
                   7102:             if ( r ) {
                   7103:                 CM(d) = r;
                   7104:                 ndl_copy(DL(t),DL(d));
                   7105:                 NMV_ADV(d);
                   7106:                 dlen++;
                   7107:             }
                   7108:         }
                   7109:     LEN(p) = dlen;
                   7110: }
                   7111:
                   7112: NDV ptondv(VL vl,VL dvl,P p)
                   7113: {
                   7114:     ND nd;
                   7115:
                   7116:     nd = ptond(vl,dvl,p);
                   7117:     return ndtondv(0,nd);
                   7118: }
                   7119:
                   7120: void pltozpl(LIST l,Q *cont,LIST *pp)
                   7121: {
1.16      noro     7122:   NODE nd,nd1;
                   7123:   int n;
                   7124:   P *pl;
                   7125:   Q *cl;
                   7126:   int i;
                   7127:   P dmy;
                   7128:   Z dvr,inv;
                   7129:   LIST r;
                   7130:
                   7131:   nd = BDY(l); n = length(nd);
                   7132:   pl = (P *)MALLOC(n*sizeof(P));
                   7133:   cl = (Q *)MALLOC(n*sizeof(Q));
                   7134:   for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
                   7135:     ptozp((P)BDY(nd),1,&cl[i],&dmy);
                   7136:   }
                   7137:   qltozl(cl,n,&dvr);
                   7138:   divz(ONE,dvr,&inv);
                   7139:   nd = BDY(l);
                   7140:   for ( i = 0; i < n; i++, nd = NEXT(nd) )
                   7141:     divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
                   7142:   nd = 0;
                   7143:   for ( i = n-1; i >= 0; i-- ) {
                   7144:     MKNODE(nd1,pl[i],nd); nd = nd1;
                   7145:   }
                   7146:   MKLIST(r,nd);
                   7147:   *pp = r;
1.1       noro     7148: }
                   7149:
                   7150: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
                   7151:
                   7152: NDV pltondv(VL vl,VL dvl,LIST p)
                   7153: {
                   7154:     int i;
                   7155:     NODE t;
                   7156:     ND r,ri;
                   7157:     NM m;
                   7158:
                   7159:     if ( !nd_module ) error("pltond : module order must be set");
                   7160:     r = 0;
                   7161:     for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
                   7162:         ri = ptond(vl,dvl,(P)BDY(t));
                   7163:         if ( ri )
                   7164:             for ( m = BDY(ri); m; m = NEXT(m) ) {
                   7165:                 MPOS(DL(m)) = i;
                   7166:                 TD(DL(m)) = ndl_weight(DL(m));
                   7167:                 if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   7168:             }
                   7169:         r = nd_add(0,r,ri);
                   7170:     }
                   7171:     return ndtondv(0,r);
                   7172: }
                   7173:
                   7174: ND ptond(VL vl,VL dvl,P p)
                   7175: {
                   7176:     int n,i,j,k,e;
                   7177:     VL tvl;
                   7178:     V v;
                   7179:     DCP dc;
                   7180:     DCP *w;
                   7181:     ND r,s,t,u;
                   7182:     P x;
                   7183:     int c;
                   7184:     UINT *d;
                   7185:     NM m,m0;
                   7186:
                   7187:     if ( !p )
                   7188:         return 0;
                   7189:     else if ( NUM(p) ) {
                   7190:         NEWNM(m);
                   7191:         ndl_zero(DL(m));
1.48      noro     7192:         if ( RATN(p) && !INT((Q)p) )
1.1       noro     7193:           error("ptond : input must be integer-coefficient");
1.6       noro     7194:         CZ(m) = (Z)p;
1.1       noro     7195:         NEXT(m) = 0;
                   7196:         MKND(nd_nvar,m,1,r);
                   7197:         SG(r) = 0;
                   7198:         return r;
                   7199:     } else {
                   7200:         for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   7201:         w = (DCP *)MALLOC(k*sizeof(DCP));
                   7202:         for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   7203:         for ( i = 0, tvl = dvl, v = VR(p);
                   7204:             tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   7205:         if ( !tvl ) {
                   7206:             for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   7207:                 t = ptond(vl,dvl,COEF(w[j]));
                   7208:                 pwrp(vl,x,DEG(w[j]),&p);
                   7209:                 nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   7210:             }
                   7211:             return s;
                   7212:         } else {
                   7213:             NEWNM(m0); d = DL(m0);
                   7214:             for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6       noro     7215:                 ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1       noro     7216:                 TD(d) = MUL_WEIGHT(e,i);
                   7217:                 if ( nd_blockmask) ndl_weight_mask(d);
                   7218:                 if ( nd_module ) MPOS(d) = 0;
                   7219:                 t = ptond(vl,dvl,COEF(w[j]));
                   7220:                 for ( m = BDY(t); m; m = NEXT(m) )
                   7221:                     ndl_addto(DL(m),d);
                   7222:                 SG(t) += TD(d);
                   7223:                 s = nd_add(0,s,t);
                   7224:             }
                   7225:             FREENM(m0);
                   7226:             return s;
                   7227:         }
                   7228:     }
                   7229: }
                   7230:
                   7231: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   7232: {
                   7233:     VL tvl;
                   7234:     int len,n,j,i,e;
                   7235:     NMV m;
                   7236:     Z q;
                   7237:     P c;
                   7238:     UINT *d;
                   7239:     P s,r,u,t,w;
                   7240:     GFS gfs;
                   7241:
                   7242:     if ( !p ) return 0;
                   7243:     else {
                   7244:         len = LEN(p);
                   7245:         n = NV(p);
                   7246:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   7247:         for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   7248:             if ( mod == -1 ) {
                   7249:                e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   7250:             } else if ( mod == -2 ) {
                   7251:                c = (P)CZ(m);
                   7252:             } else if ( mod > 0 ) {
1.6       noro     7253:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     7254:             } else
                   7255:                 c = CP(m);
                   7256:             d = DL(m);
                   7257:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     7258:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     7259:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   7260:             }
                   7261:             addp(vl,s,t,&u); s = u;
                   7262:         }
                   7263:         return s;
                   7264:     }
                   7265: }
                   7266:
                   7267: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
                   7268: {
                   7269:     VL tvl;
                   7270:     int len,n,j,i,e;
                   7271:     NMV m;
                   7272:     Z q;
                   7273:     P c;
                   7274:     UINT *d;
                   7275:     P s,r,u,t,w;
                   7276:     GFS gfs;
                   7277:     P *a;
                   7278:     LIST l;
                   7279:     NODE nd,nd1;
                   7280:
                   7281:     if ( !p ) return 0;
                   7282:     else {
                   7283:         a = (P *)MALLOC((rank+1)*sizeof(P));
                   7284:         for ( i = 0; i <= rank; i++ ) a[i] = 0;
                   7285:         len = LEN(p);
                   7286:         n = NV(p);
                   7287:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   7288:         for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
                   7289:             if ( mod == -1 ) {
                   7290:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   7291:             } else if ( mod ) {
1.6       noro     7292:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     7293:             } else
                   7294:                 c = CP(m);
                   7295:             d = DL(m);
                   7296:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     7297:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     7298:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   7299:             }
                   7300:             addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
                   7301:         }
                   7302:         nd = 0;
                   7303:         for ( i = rank; i > 0; i-- ) {
                   7304:             MKNODE(nd1,a[i],nd); nd = nd1;
                   7305:         }
                   7306:         MKLIST(l,nd);
                   7307:         return l;
                   7308:     }
                   7309: }
                   7310:
                   7311: NDV ndtondv(int mod,ND p)
                   7312: {
                   7313:     NDV d;
                   7314:     NMV m,m0;
                   7315:     NM t;
                   7316:     int i,len;
                   7317:
                   7318:     if ( !p ) return 0;
                   7319:     len = LEN(p);
                   7320:     if ( mod > 0 || mod == -1 )
                   7321:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   7322:     else
                   7323:         m0 = m = MALLOC(len*nmv_adv);
                   7324: #if 0
                   7325:     ndv_alloc += nmv_adv*len;
                   7326: #endif
                   7327:     for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   7328:         ndl_copy(DL(t),DL(m));
1.6       noro     7329:         CZ(m) = CZ(t);
1.1       noro     7330:     }
                   7331:     MKNDV(NV(p),m0,len,d);
                   7332:     SG(d) = SG(p);
1.24      noro     7333:     d->sig = p->sig;
1.1       noro     7334:     return d;
                   7335: }
                   7336:
1.16      noro     7337: static int dmm_comp_nv;
                   7338:
                   7339: int dmm_comp(DMM *a,DMM *b)
                   7340: {
                   7341:    return -compdmm(dmm_comp_nv,*a,*b);
                   7342: }
                   7343:
                   7344: void dmm_sort_by_ord(DMM *a,int len,int nv)
                   7345: {
                   7346:   dmm_comp_nv = nv;
                   7347:   qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
                   7348: }
                   7349:
                   7350: void dpm_sort(DPM p,DPM *rp)
                   7351: {
                   7352:   DMM t,t1;
                   7353:   int len,i,n;
                   7354:   DMM *a;
                   7355:   DPM d;
                   7356:
                   7357:   if ( !p ) *rp = 0;
                   7358:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   7359:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   7360:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   7361:   n = p->nv;
                   7362:   dmm_sort_by_ord(a,len,n);
                   7363:   t = 0;
                   7364:   for ( i = len-1; i >= 0; i-- ) {
                   7365:     NEWDMM(t1);
                   7366:     t1->c = a[i]->c;
                   7367:     t1->dl = a[i]->dl;
                   7368:     t1->pos = a[i]->pos;
                   7369:     t1->next = t;
                   7370:     t = t1;
                   7371:   }
                   7372:   MKDPM(n,t,d);
                   7373:   SG(d) = SG(p);
                   7374:   *rp = d;
                   7375: }
                   7376:
1.18      noro     7377: int dpm_comp(DPM *a,DPM *b)
                   7378: {
1.22      noro     7379:   return -compdpm(CO,*a,*b);
1.18      noro     7380: }
                   7381:
                   7382: NODE dpm_sort_list(NODE l)
                   7383: {
                   7384:   int i,len;
                   7385:   NODE t,t1;
                   7386:   DPM *a;
                   7387:
                   7388:   len = length(l);
                   7389:   a = (DPM *)MALLOC(len*sizeof(DPM));
                   7390:   for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
                   7391:   qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
                   7392:   t = 0;
                   7393:   for ( i = len-1; i >= 0; i-- ) {
                   7394:     MKNODE(t1,(pointer)a[i],t); t = t1;
                   7395:   }
                   7396:   return t;
                   7397: }
                   7398:
1.20      noro     7399: int nmv_comp(NMV a,NMV b)
                   7400: {
1.21      noro     7401:   int t;
                   7402:   t = DL_COMPARE(a->dl,b->dl);
                   7403:   return -t;
1.20      noro     7404: }
                   7405:
1.43      noro     7406: NDV dptondv(int mod,DP p)
                   7407: {
                   7408:   NDV d;
                   7409:   NMV m,m0;
                   7410:   MP t;
                   7411:   MP *a;
                   7412:   int i,len,n;
                   7413:
                   7414:   if ( !p ) return 0;
                   7415:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   7416:   n = p->nv;
                   7417:   if ( mod > 0 || mod == -1 )
                   7418:     m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   7419:   else
                   7420:     m0 = m = MALLOC(len*nmv_adv);
                   7421:   for ( i = 0, t = BDY(p); i < len; i++, NMV_ADV(m), t = NEXT(t) ) {
                   7422:     dltondl(n,t->dl,DL(m));
                   7423:     TD(DL(m)) = ndl_weight(DL(m));
                   7424:     CZ(m) = (Z)t->c;
                   7425:   }
                   7426:   qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
                   7427:   MKNDV(NV(p),m0,len,d);
                   7428:   SG(d) = SG(p);
                   7429:   return d;
                   7430: }
                   7431:
1.16      noro     7432: NDV dpmtondv(int mod,DPM p)
                   7433: {
                   7434:   NDV d;
                   7435:   NMV m,m0;
                   7436:   DMM t;
                   7437:   DMM *a;
                   7438:   int i,len,n;
                   7439:
                   7440:   if ( !p ) return 0;
                   7441:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   7442:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   7443:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   7444:   n = p->nv;
                   7445:   dmm_sort_by_ord(a,len,n);
                   7446:   if ( mod > 0 || mod == -1 )
                   7447:     m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   7448:   else
                   7449:     m0 = m = MALLOC(len*nmv_adv);
                   7450: #if 0
                   7451:   ndv_alloc += nmv_adv*len;
                   7452: #endif
                   7453:   for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   7454:     dltondl(n,a[i]->dl,DL(m));
                   7455:     MPOS(DL(m)) = a[i]->pos;
1.20      noro     7456:     TD(DL(m)) = ndl_weight(DL(m));
1.16      noro     7457:     CZ(m) = (Z)a[i]->c;
                   7458:   }
1.20      noro     7459:   qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16      noro     7460:   MKNDV(NV(p),m0,len,d);
                   7461:   SG(d) = SG(p);
                   7462:   return d;
                   7463: }
                   7464:
1.1       noro     7465: ND ndvtond(int mod,NDV p)
                   7466: {
                   7467:     ND d;
                   7468:     NM m,m0;
                   7469:     NMV t;
                   7470:     int i,len;
                   7471:
                   7472:     if ( !p ) return 0;
                   7473:     m0 = 0;
                   7474:     len = p->len;
                   7475:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   7476:         NEXTNM(m0,m);
                   7477:         ndl_copy(DL(t),DL(m));
1.6       noro     7478:         CZ(m) = CZ(t);
1.1       noro     7479:     }
                   7480:     NEXT(m) = 0;
                   7481:     MKND(NV(p),m0,len,d);
                   7482:     SG(d) = SG(p);
1.28      noro     7483:     d->sig = p->sig;
1.1       noro     7484:     return d;
                   7485: }
                   7486:
                   7487: DP ndvtodp(int mod,NDV p)
                   7488: {
                   7489:     MP m,m0;
                   7490:   DP d;
                   7491:     NMV t;
                   7492:     int i,len;
                   7493:
                   7494:     if ( !p ) return 0;
                   7495:     m0 = 0;
                   7496:     len = p->len;
                   7497:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   7498:         NEXTMP(m0,m);
                   7499:       m->dl = ndltodl(nd_nvar,DL(t));
                   7500:       m->c = (Obj)ndctop(mod,t->c);
                   7501:     }
                   7502:     NEXT(m) = 0;
                   7503:   MKDP(nd_nvar,m0,d);
                   7504:     SG(d) = SG(p);
                   7505:     return d;
                   7506: }
                   7507:
1.44      noro     7508: DPM sigtodpm(SIG s)
                   7509: {
                   7510:   DMM m;
                   7511:   DPM d;
                   7512:
                   7513:   NEWDMM(m);
                   7514:   m->c = (Obj)ONE;
                   7515:   m->dl = s->dl;
                   7516:   m->pos = s->pos+1;
                   7517:   m->next = 0;
                   7518:   MKDPM(nd_nvar,m,d);
                   7519:   SG(d) = s->dl->td;
                   7520:   return d;
                   7521: }
                   7522:
1.16      noro     7523: DPM ndvtodpm(int mod,NDV p)
                   7524: {
                   7525:   DMM m,m0;
                   7526:   DPM d;
                   7527:   NMV t;
                   7528:   int i,len;
                   7529:
                   7530:   if ( !p ) return 0;
                   7531:   m0 = 0;
                   7532:   len = p->len;
                   7533:   for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   7534:     NEXTDMM(m0,m);
                   7535:     m->dl = ndltodl(nd_nvar,DL(t));
                   7536:     m->c = (Obj)ndctop(mod,t->c);
                   7537:     m->pos = MPOS(DL(t));
                   7538:   }
                   7539:   NEXT(m) = 0;
                   7540:   MKDPM(nd_nvar,m0,d);
                   7541:   SG(d) = SG(p);
                   7542:   return d;
                   7543: }
                   7544:
                   7545:
1.1       noro     7546: DP ndtodp(int mod,ND p)
                   7547: {
                   7548:     MP m,m0;
                   7549:   DP d;
                   7550:     NM t;
                   7551:     int i,len;
                   7552:
                   7553:     if ( !p ) return 0;
                   7554:     m0 = 0;
                   7555:     len = p->len;
                   7556:     for ( t = BDY(p); t; t = NEXT(t) ) {
                   7557:         NEXTMP(m0,m);
                   7558:       m->dl = ndltodl(nd_nvar,DL(t));
                   7559:       m->c = (Obj)ndctop(mod,t->c);
                   7560:     }
                   7561:     NEXT(m) = 0;
                   7562:   MKDP(nd_nvar,m0,d);
                   7563:     SG(d) = SG(p);
                   7564:     return d;
                   7565: }
                   7566:
                   7567: void ndv_print(NDV p)
                   7568: {
                   7569:     NMV m;
                   7570:     int i,len;
                   7571:
                   7572:     if ( !p ) printf("0\n");
                   7573:     else {
                   7574:         len = LEN(p);
                   7575:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   7576:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     7577:             else printf("+%ld*",CM(m));
1.1       noro     7578:             ndl_print(DL(m));
                   7579:         }
                   7580:         printf("\n");
                   7581:     }
                   7582: }
                   7583:
                   7584: void ndv_print_q(NDV p)
                   7585: {
                   7586:     NMV m;
                   7587:     int i,len;
                   7588:
                   7589:     if ( !p ) printf("0\n");
                   7590:     else {
                   7591:         len = LEN(p);
                   7592:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   7593:             printf("+");
1.6       noro     7594:             printexpr(CO,(Obj)CZ(m));
1.1       noro     7595:             printf("*");
                   7596:             ndl_print(DL(m));
                   7597:         }
                   7598:         printf("\n");
                   7599:     }
                   7600: }
                   7601:
                   7602: NODE ndv_reducebase(NODE x,int *perm)
                   7603: {
                   7604:     int len,i,j;
                   7605:     NDVI w;
                   7606:     NODE t,t0;
                   7607:
1.41      noro     7608:     if ( nd_norb ) return x;
1.1       noro     7609:     len = length(x);
                   7610:     w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
                   7611:     for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
                   7612:         w[i].p = BDY(t); w[i].i = perm[i];
                   7613:     }
                   7614:     for ( i = 0; i < len; i++ ) {
                   7615:         for ( j = 0; j < i; j++ ) {
                   7616:             if ( w[i].p && w[j].p ) {
                   7617:                 if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
                   7618:                 else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
                   7619:             }
                   7620:         }
                   7621:     }
                   7622:     for ( i = j = 0, t0 = 0; i < len; i++ ) {
                   7623:         if ( w[i].p ) {
                   7624:             NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
                   7625:             perm[j++] = w[i].i;
                   7626:         }
                   7627:     }
                   7628:     NEXT(t) = 0; x = t0;
                   7629:     return x;
                   7630: }
                   7631:
                   7632: /* XXX incomplete */
                   7633:
1.21      noro     7634: extern DMMstack dmm_stack;
                   7635: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16      noro     7636:
1.1       noro     7637: void nd_init_ord(struct order_spec *ord)
                   7638: {
                   7639:   nd_module = (ord->id >= 256);
                   7640:   if ( nd_module ) {
                   7641:     nd_dcomp = -1;
1.21      noro     7642:     nd_module_ordtype = ord->module_ordtype;
1.1       noro     7643:     nd_pot_nelim = ord->pot_nelim;
                   7644:     nd_poly_weight_len = ord->nv;
                   7645:     nd_poly_weight = ord->top_weight;
                   7646:     nd_module_rank = ord->module_rank;
                   7647:     nd_module_weight = ord->module_top_weight;
                   7648:   }
                   7649:   nd_matrix = 0;
                   7650:   nd_matrix_len = 0;
                   7651:     switch ( ord->id ) {
                   7652:         case 0:
                   7653:             switch ( ord->ord.simple ) {
                   7654:                 case 0:
                   7655:                     nd_dcomp = 1;
                   7656:                     nd_isrlex = 1;
                   7657:                     break;
                   7658:                 case 1:
                   7659:                     nd_dcomp = 1;
                   7660:                     nd_isrlex = 0;
                   7661:                     break;
                   7662:                 case 2:
                   7663:                     nd_dcomp = 0;
                   7664:                     nd_isrlex = 0;
                   7665:                     ndl_compare_function = ndl_lex_compare;
                   7666:                     break;
                   7667:                 case 11:
                   7668:                     /* XXX */
                   7669:                     nd_dcomp = 0;
                   7670:                     nd_isrlex = 1;
                   7671:                     ndl_compare_function = ndl_ww_lex_compare;
                   7672:                     break;
                   7673:                 default:
                   7674:                     error("nd_gr : unsupported order");
                   7675:             }
                   7676:             break;
                   7677:         case 1:
                   7678:             /* block order */
                   7679:             /* XXX */
                   7680:             nd_dcomp = -1;
                   7681:             nd_isrlex = 0;
                   7682:             ndl_compare_function = ndl_block_compare;
                   7683:             break;
                   7684:         case 2:
                   7685:             /* matrix order */
                   7686:             /* XXX */
                   7687:             nd_dcomp = -1;
                   7688:             nd_isrlex = 0;
                   7689:             nd_matrix_len = ord->ord.matrix.row;
                   7690:             nd_matrix = ord->ord.matrix.matrix;
                   7691:             ndl_compare_function = ndl_matrix_compare;
                   7692:             break;
                   7693:         case 3:
                   7694:             /* composite order */
                   7695:             nd_dcomp = -1;
                   7696:             nd_isrlex = 0;
                   7697:             nd_worb_len = ord->ord.composite.length;
                   7698:             nd_worb = ord->ord.composite.w_or_b;
                   7699:             ndl_compare_function = ndl_composite_compare;
                   7700:             break;
                   7701:
                   7702:         /* module order */
                   7703:         case 256:
                   7704:             switch ( ord->ord.simple ) {
                   7705:                 case 0:
1.21      noro     7706:                     nd_dcomp = 0;
1.1       noro     7707:                     nd_isrlex = 1;
1.21      noro     7708:                     ndl_compare_function = ndl_module_glex_compare;
1.1       noro     7709:                     break;
                   7710:                 case 1:
1.21      noro     7711:                     nd_dcomp = 0;
1.1       noro     7712:                     nd_isrlex = 0;
                   7713:                     ndl_compare_function = ndl_module_glex_compare;
                   7714:                     break;
                   7715:                 case 2:
1.21      noro     7716:                     nd_dcomp = 0;
1.1       noro     7717:                     nd_isrlex = 0;
1.21      noro     7718:                     ndl_compare_function = ndl_module_compare;
                   7719:                     ndl_base_compare_function = ndl_lex_compare;
1.1       noro     7720:                     break;
                   7721:                 default:
1.21      noro     7722:                     error("nd_init_ord : unsupported order");
1.1       noro     7723:             }
                   7724:             break;
                   7725:         case 257:
                   7726:             /* block order */
                   7727:             nd_isrlex = 0;
1.21      noro     7728:             ndl_compare_function = ndl_module_compare;
                   7729:             ndl_base_compare_function = ndl_block_compare;
1.1       noro     7730:             break;
                   7731:         case 258:
                   7732:             /* matrix order */
                   7733:             nd_isrlex = 0;
                   7734:             nd_matrix_len = ord->ord.matrix.row;
                   7735:             nd_matrix = ord->ord.matrix.matrix;
1.21      noro     7736:             ndl_compare_function = ndl_module_compare;
                   7737:             ndl_base_compare_function = ndl_matrix_compare;
1.1       noro     7738:             break;
                   7739:         case 259:
                   7740:             /* composite order */
                   7741:             nd_isrlex = 0;
                   7742:             nd_worb_len = ord->ord.composite.length;
                   7743:             nd_worb = ord->ord.composite.w_or_b;
1.21      noro     7744:             ndl_compare_function = ndl_module_compare;
                   7745:             ndl_base_compare_function = ndl_composite_compare;
                   7746:             break;
                   7747:         case 300:
                   7748:             /* schreyer order */
                   7749:             if ( ord->base->id != 256 )
                   7750:                error("nd_init_ord : unsupported base order");
                   7751:             ndl_compare_function = ndl_module_schreyer_compare;
                   7752:             dmm_stack = ord->dmmstack;
                   7753:             switch ( ord->base->ord.simple ) {
                   7754:                 case 0:
                   7755:                     nd_isrlex = 1;
                   7756:                     ndl_base_compare_function = ndl_glex_compare;
                   7757:                     dl_base_compare_function = cmpdl_revgradlex;
                   7758:                     break;
                   7759:                 case 1:
                   7760:                     nd_isrlex = 0;
                   7761:                     ndl_base_compare_function = ndl_glex_compare;
                   7762:                     dl_base_compare_function = cmpdl_gradlex;
                   7763:                     break;
                   7764:                 case 2:
                   7765:                     nd_isrlex = 0;
                   7766:                     ndl_base_compare_function = ndl_lex_compare;
                   7767:                     dl_base_compare_function = cmpdl_lex;
                   7768:                     break;
                   7769:                 default:
                   7770:                     error("nd_init_ord : unsupported order");
                   7771:             }
1.1       noro     7772:             break;
                   7773:     }
                   7774:     nd_ord = ord;
                   7775: }
                   7776:
                   7777: BlockMask nd_create_blockmask(struct order_spec *ord)
                   7778: {
                   7779:     int n,i,j,s,l;
                   7780:     UINT *t;
                   7781:     BlockMask bm;
                   7782:
                   7783:     /* we only create mask table for block order */
                   7784:     if ( ord->id != 1 && ord->id != 257 )
                   7785:         return 0;
                   7786:     n = ord->ord.block.length;
                   7787:     bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   7788:     bm->n = n;
                   7789:     bm->order_pair = ord->ord.block.order_pair;
                   7790:     bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
                   7791:     for ( i = 0, s = 0; i < n; i++ ) {
                   7792:         bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
                   7793:         for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   7794:         l = bm->order_pair[i].length;
                   7795:         for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   7796:     }
                   7797:     return bm;
                   7798: }
                   7799:
                   7800: EPOS nd_create_epos(struct order_spec *ord)
                   7801: {
                   7802:     int i,j,l,s,ord_l,ord_o;
                   7803:     EPOS epos;
                   7804:     struct order_pair *op;
                   7805:
                   7806:     epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   7807:     switch ( ord->id ) {
1.21      noro     7808:         case 0: case 256: case 300:
1.1       noro     7809:             if ( nd_isrlex ) {
                   7810:                 for ( i = 0; i < nd_nvar; i++ ) {
                   7811:                     epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   7812:                     epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   7813:                 }
                   7814:             } else {
                   7815:                 for ( i = 0; i < nd_nvar; i++ ) {
                   7816:                     epos[i].i = nd_exporigin + i/nd_epw;
                   7817:                     epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   7818:                 }
                   7819:             }
                   7820:             break;
                   7821:         case 1: case 257:
                   7822:             /* block order */
                   7823:             l = ord->ord.block.length;
                   7824:             op = ord->ord.block.order_pair;
                   7825:             for ( j = 0, s = 0; j < l; j++ ) {
                   7826:                 ord_o = op[j].order;
                   7827:                 ord_l = op[j].length;
                   7828:                 if ( !ord_o )
                   7829:                     for ( i = 0; i < ord_l; i++ ) {
                   7830:                         epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   7831:                         epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   7832:                     }
                   7833:                 else
                   7834:                     for ( i = 0; i < ord_l; i++ ) {
                   7835:                         epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   7836:                         epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   7837:                     }
                   7838:                 s += ord_l;
                   7839:             }
                   7840:             break;
                   7841:         case 2:
                   7842:             /* matrix order */
                   7843:         case 3:
                   7844:             /* composite order */
                   7845:         default:
                   7846:             for ( i = 0; i < nd_nvar; i++ ) {
                   7847:                 epos[i].i = nd_exporigin + i/nd_epw;
                   7848:                 epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   7849:             }
                   7850:             break;
                   7851:     }
                   7852:     return epos;
                   7853: }
                   7854:
                   7855: /* external interface */
                   7856:
                   7857: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
                   7858: {
                   7859:     NODE t,in0,in;
                   7860:     ND ndf,nf;
                   7861:     NDV ndvf;
                   7862:     VL vv,tv;
                   7863:     int stat,nvar,max,mrank;
                   7864:     union oNDC dn;
                   7865:     Q cont;
                   7866:     P pp;
                   7867:     LIST ppl;
                   7868:
                   7869:     if ( !f ) {
                   7870:         *rp = 0;
                   7871:         return;
                   7872:     }
                   7873:     pltovl(v,&vv);
                   7874:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   7875:
                   7876:     /* max=65536 implies nd_bpe=32 */
                   7877:     max = 65536;
                   7878:
                   7879:   nd_module = 0;
                   7880:   /* nd_module will be set if ord is a module ordering */
                   7881:     nd_init_ord(ord);
                   7882:     nd_setup_parameters(nvar,max);
                   7883:     if ( nd_module && OID(f) != O_LIST )
                   7884:         error("nd_nf_p : the first argument must be a list");
                   7885:   if ( nd_module ) mrank = length(BDY((LIST)f));
                   7886:     /* conversion to ndv */
                   7887:     for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   7888:         NEXTNODE(in0,in);
                   7889:         if ( nd_module ) {
                   7890:           if ( !BDY(t) || OID(BDY(t)) != O_LIST
                   7891:                || length(BDY((LIST)BDY(t))) != mrank )
                   7892:               error("nd_nf_p : inconsistent basis element");
                   7893:           if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
                   7894:           else ppl = (LIST)BDY(t);
                   7895:           BDY(in) = (pointer)pltondv(CO,vv,ppl);
                   7896:         } else {
                   7897:           if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
                   7898:           else pp = (P)BDY(t);
                   7899:           BDY(in) = (pointer)ptondv(CO,vv,pp);
                   7900:         }
                   7901:         if ( m ) ndv_mod(m,(NDV)BDY(in));
                   7902:     }
                   7903:     if ( in0 ) NEXT(in) = 0;
                   7904:
                   7905:     if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
                   7906:     else ndvf = ptondv(CO,vv,(P)f);
                   7907:     if ( m ) ndv_mod(m,ndvf);
                   7908:     ndf = (pointer)ndvtond(m,ndvf);
                   7909:
                   7910:     /* dont sort, dont removecont */
1.24      noro     7911:     ndv_setup(m,0,in0,1,1,0);
1.1       noro     7912:     nd_scale=2;
1.6       noro     7913:     stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1       noro     7914:     if ( !stat )
                   7915:         error("nd_nf_p : exponent too large");
                   7916:     if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
                   7917:     else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
                   7918: }
                   7919:
                   7920: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   7921: {
                   7922:     NM m;
                   7923:     UINT *t,*s;
                   7924:     int i;
                   7925:
                   7926:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   7927:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7928:         t = DL(m);
                   7929:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   7930:         r[i] = CM(m);
                   7931:     }
                   7932:     for ( i = 0; !r[i]; i++ );
                   7933:     return i;
                   7934: }
                   7935:
                   7936: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
                   7937: {
                   7938:     NM m;
                   7939:     UINT *t,*s;
                   7940:     int i;
                   7941:
                   7942:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   7943:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7944:         t = DL(m);
                   7945:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     7946:         r[i] = CZ(m);
1.1       noro     7947:     }
                   7948:     for ( i = 0; !r[i]; i++ );
                   7949:     return i;
                   7950: }
                   7951:
                   7952: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
                   7953: {
                   7954:     NM m;
                   7955:     UINT *t,*s;
                   7956:     int i;
                   7957:
                   7958:     for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
                   7959:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7960:         t = DL(m);
                   7961:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   7962:         mpz_set(r[i],BDY(CZ(m)));
                   7963:     }
                   7964:     for ( i = 0; !mpz_sgn(r[i]); i++ );
                   7965:     return i;
                   7966: }
                   7967:
                   7968: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
                   7969: {
                   7970:     NM m;
                   7971:     unsigned long *v;
                   7972:     int i,j,h,size;
                   7973:   UINT *s,*t;
                   7974:
                   7975:   size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
                   7976:     v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
                   7977:     bzero(v,size);
                   7978:     for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
                   7979:     t = DL(m);
                   7980:     h = ndl_hash_value(t);
                   7981:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   7982:       v[i/BLEN] |= 1L <<(i%BLEN);
                   7983:     }
                   7984:     return v;
                   7985: }
                   7986:
                   7987: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
                   7988: {
                   7989:     NMV mr;
                   7990:     UINT *d,*t,*s;
                   7991:     int i,j,len,h,head;
                   7992:
                   7993:     d = DL(m);
                   7994:     len = LEN(p);
                   7995:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   7996:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   7997:         ndl_add(d,DL(mr),t);
                   7998:     h = ndl_hash_value(t);
                   7999:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   8000:     if ( j == 0 ) head = i;
                   8001:       v[i/BLEN] |= 1L <<(i%BLEN);
                   8002:     }
                   8003:     return head;
                   8004: }
                   8005:
                   8006: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   8007: {
                   8008:     NM m;
                   8009:     NMV mr;
                   8010:     UINT *d,*t,*s;
                   8011:     NDV p;
                   8012:     int i,j,len;
                   8013:     Z *r;
                   8014:
                   8015:     m = pair->mul;
                   8016:     d = DL(m);
                   8017:     p = nd_ps[pair->index];
                   8018:     len = LEN(p);
                   8019:     r = (Z *)CALLOC(n,sizeof(Q));
                   8020:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   8021:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   8022:         ndl_add(d,DL(mr),t);
                   8023:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     8024:         r[i] = CZ(mr);
1.1       noro     8025:     }
                   8026:     return r;
                   8027: }
                   8028:
1.11      noro     8029: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1       noro     8030: {
                   8031:     NM m;
                   8032:     NMV mr;
1.11      noro     8033:     UINT *d,*t,*s,*u;
1.1       noro     8034:     NDV p;
                   8035:     unsigned char *ivc;
                   8036:     unsigned short *ivs;
                   8037:     UINT *v,*ivi,*s0v;
1.11      noro     8038:     int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1       noro     8039:     IndArray r;
                   8040:
                   8041:     m = pair->mul;
                   8042:     d = DL(m);
                   8043:     if ( trace )
                   8044:       p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
                   8045:     else
                   8046:       p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
                   8047:
                   8048:     len = LEN(p);
                   8049:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   8050:     v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11      noro     8051:     for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
                   8052:       ndl_add(d,DL(mr),t);
                   8053:       st = prev;
                   8054:       ed = n;
                   8055:       while ( ed > st ) {
                   8056:         md = (st+ed)/2;
                   8057:         u = s0+md*nd_wpd;
                   8058:         c = DL_COMPARE(u,t);
                   8059:         if ( c == 0 ) break;
                   8060:         else if ( c > 0 ) st = md;
                   8061:         else ed = md;
                   8062:       }
                   8063:       prev = v[j] = md;
1.1       noro     8064:     }
                   8065:     r = (IndArray)MALLOC(sizeof(struct oIndArray));
                   8066:     r->head = v[0];
                   8067:     diff = 0;
                   8068:     for ( i = 1; i < len; i++ ) {
                   8069:         cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   8070:     }
                   8071:     if ( diff < 256 ) {
                   8072:         r->width = 1;
                   8073:         ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   8074:         r->index.c = ivc;
                   8075:         for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   8076:     } else if ( diff < 65536 ) {
                   8077:         r->width = 2;
                   8078:         ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   8079:         r->index.s = ivs;
                   8080:         for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   8081:     } else {
                   8082:         r->width = 4;
                   8083:         ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   8084:         r->index.i = ivi;
                   8085:         for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
                   8086:     }
                   8087:     return r;
                   8088: }
                   8089:
                   8090: int compress_array(Z *svect,Z *cvect,int n)
                   8091: {
                   8092:     int i,j;
                   8093:
                   8094:     for ( i = j = 0; i < n; i++ )
                   8095:         if ( svect[i] ) cvect[j++] = svect[i];
                   8096:     return j;
                   8097: }
                   8098:
                   8099: void expand_array(Z *svect,Z *cvect,int n)
                   8100: {
                   8101:     int i,j;
                   8102:
                   8103:     for ( i = j = 0; j < n;  i++  )
                   8104:         if ( svect[i] ) svect[i] = cvect[j++];
                   8105: }
                   8106:
1.8       noro     8107: #if 0
1.1       noro     8108: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8109: {
                   8110:     int i,j,k,len,pos,prev,nz;
                   8111:     Z cs,mcs,c1,c2,cr,gcd,t;
                   8112:     IndArray ivect;
                   8113:     unsigned char *ivc;
                   8114:     unsigned short *ivs;
                   8115:     unsigned int *ivi;
                   8116:     NDV redv;
                   8117:     NMV mr;
                   8118:     NODE rp;
                   8119:     int maxrs;
                   8120:     double hmag;
                   8121:     Z *cvect;
1.3       noro     8122:     int l;
1.1       noro     8123:
                   8124:     maxrs = 0;
                   8125:     for ( i = 0; i < col && !svect[i]; i++ );
                   8126:     if ( i == col ) return maxrs;
                   8127:     hmag = p_mag((P)svect[i])*nd_scale;
                   8128:     cvect = (Z *)MALLOC(col*sizeof(Q));
                   8129:     for ( i = 0; i < nred; i++ ) {
                   8130:         ivect = imat[i];
                   8131:         k = ivect->head;
                   8132:         if ( svect[k] ) {
                   8133:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8134:             redv = nd_demand?ndv_load(rp0[i]->index)
                   8135:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   8136:             len = LEN(redv); mr = BDY(redv);
1.6       noro     8137:             igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1       noro     8138:             chsgnz(cs,&mcs);
                   8139:             if ( !UNIQ(cr) ) {
                   8140:                 for ( j = 0; j < col; j++ ) {
                   8141:                     mulz(svect[j],cr,&c1); svect[j] = c1;
                   8142:                 }
                   8143:             }
                   8144:             svect[k] = 0; prev = k;
                   8145:             switch ( ivect->width ) {
                   8146:                 case 1:
                   8147:                     ivc = ivect->index.c;
                   8148:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8149:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     8150:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     8151:                     }
                   8152:                     break;
                   8153:                 case 2:
                   8154:                     ivs = ivect->index.s;
                   8155:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8156:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     8157:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     8158:                     }
                   8159:                     break;
                   8160:                 case 4:
                   8161:                     ivi = ivect->index.i;
                   8162:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8163:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     8164:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     8165:                     }
                   8166:                     break;
                   8167:             }
                   8168:             for ( j = k+1; j < col && !svect[j]; j++ );
                   8169:             if ( j == col ) break;
                   8170:             if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
                   8171:                 nz = compress_array(svect,cvect,col);
                   8172:                 removecont_array((P *)cvect,nz,1);
                   8173:                 expand_array(svect,cvect,nz);
                   8174:                 hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   8175:             }
                   8176:         }
                   8177:     }
                   8178:     nz = compress_array(svect,cvect,col);
                   8179:     removecont_array((P *)cvect,nz,1);
                   8180:     expand_array(svect,cvect,nz);
                   8181:     if ( DP_Print ) {
                   8182:         fprintf(asir_out,"-"); fflush(asir_out);
                   8183:     }
                   8184:     return maxrs;
                   8185: }
1.4       noro     8186: #else
1.9       noro     8187:
1.4       noro     8188: /* direct mpz version */
                   8189: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8190: {
                   8191:     int i,j,k,len,pos,prev;
                   8192:     mpz_t cs,cr,gcd;
                   8193:     IndArray ivect;
                   8194:     unsigned char *ivc;
                   8195:     unsigned short *ivs;
                   8196:     unsigned int *ivi;
                   8197:     NDV redv;
                   8198:     NMV mr;
                   8199:     NODE rp;
                   8200:     int maxrs;
                   8201:     double hmag;
                   8202:     int l;
1.13      noro     8203:     static mpz_t *svect;
                   8204:     static int svect_len=0;
1.4       noro     8205:
                   8206:     maxrs = 0;
                   8207:     for ( i = 0; i < col && !svect0[i]; i++ );
                   8208:     if ( i == col ) return maxrs;
                   8209:     hmag = p_mag((P)svect0[i])*nd_scale;
1.13      noro     8210:     if ( col > svect_len ) {
                   8211:       svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   8212:       svect_len = col;
                   8213:     }
1.4       noro     8214:     for ( i = 0; i < col; i++ ) {
                   8215:       mpz_init(svect[i]);
                   8216:       if ( svect0[i] )
                   8217:         mpz_set(svect[i],BDY(svect0[i]));
                   8218:       else
                   8219:         mpz_set_ui(svect[i],0);
                   8220:     }
                   8221:     mpz_init(gcd); mpz_init(cs); mpz_init(cr);
                   8222:     for ( i = 0; i < nred; i++ ) {
                   8223:         ivect = imat[i];
                   8224:         k = ivect->head;
                   8225:         if ( mpz_sgn(svect[k]) ) {
                   8226:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8227:             redv = nd_demand?ndv_load(rp0[i]->index)
                   8228:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   8229:             len = LEN(redv); mr = BDY(redv);
1.6       noro     8230:             mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4       noro     8231:             mpz_div(cs,svect[k],gcd);
1.6       noro     8232:             mpz_div(cr,BDY(CZ(mr)),gcd);
1.4       noro     8233:             mpz_neg(cs,cs);
1.9       noro     8234:             if ( MUNIMPZ(cr) )
                   8235:               for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
                   8236:             else if ( !UNIMPZ(cr) )
                   8237:               for ( j = 0; j < col; j++ ) {
                   8238:                 if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
                   8239:               }
1.4       noro     8240:             mpz_set_ui(svect[k],0);
                   8241:             prev = k;
                   8242:             switch ( ivect->width ) {
                   8243:                 case 1:
                   8244:                     ivc = ivect->index.c;
                   8245:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8246:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     8247:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     8248:                     }
                   8249:                     break;
                   8250:                 case 2:
                   8251:                     ivs = ivect->index.s;
                   8252:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8253:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     8254:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     8255:                     }
                   8256:                     break;
                   8257:                 case 4:
                   8258:                     ivi = ivect->index.i;
                   8259:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8260:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     8261:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     8262:                     }
                   8263:                     break;
                   8264:             }
                   8265:             for ( j = k+1; j < col && !svect[j]; j++ );
                   8266:             if ( j == col ) break;
                   8267:             if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
                   8268:                 mpz_removecont_array(svect,col);
                   8269:                 hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
                   8270:             }
                   8271:         }
                   8272:     }
                   8273:     mpz_removecont_array(svect,col);
                   8274:     if ( DP_Print ) {
                   8275:         fprintf(asir_out,"-"); fflush(asir_out);
                   8276:     }
                   8277:     for ( i = 0; i < col; i++ )
                   8278:       if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
                   8279:       else svect0[i] = 0;
                   8280:     return maxrs;
                   8281: }
                   8282: #endif
1.1       noro     8283:
1.40      noro     8284: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred,SIG sig)
1.1       noro     8285: {
                   8286:     int i,j,k,len,pos,prev;
                   8287:     UINT c,c1,c2,c3,up,lo,dmy;
                   8288:     IndArray ivect;
                   8289:     unsigned char *ivc;
                   8290:     unsigned short *ivs;
                   8291:     unsigned int *ivi;
                   8292:     NDV redv;
                   8293:     NMV mr;
                   8294:     NODE rp;
                   8295:     int maxrs;
                   8296:
                   8297:     maxrs = 0;
                   8298:     for ( i = 0; i < nred; i++ ) {
                   8299:         ivect = imat[i];
                   8300:         k = ivect->head; svect[k] %= m;
1.40      noro     8301:         if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.1       noro     8302:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8303:             c = m-c; redv = nd_ps[rp0[i]->index];
                   8304:             len = LEN(redv); mr = BDY(redv);
                   8305:             svect[k] = 0; prev = k;
                   8306:             switch ( ivect->width ) {
                   8307:                 case 1:
                   8308:                     ivc = ivect->index.c;
                   8309:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8310:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.40      noro     8311:                         if ( c1 ) {
                   8312:                           c2 = svect[pos];
1.1       noro     8313:                           DMA(c1,c,c2,up,lo);
                   8314:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   8315:                           } else svect[pos] = lo;
1.40      noro     8316:                         }
1.1       noro     8317:                     }
                   8318:                     break;
                   8319:                 case 2:
                   8320:                     ivs = ivect->index.s;
                   8321:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8322:                         pos = prev+ivs[j]; c1 = CM(mr);
                   8323:                         prev = pos;
1.40      noro     8324:                         if ( c1 ) {
                   8325:                           c2 = svect[pos];
1.1       noro     8326:                           DMA(c1,c,c2,up,lo);
                   8327:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   8328:                           } else svect[pos] = lo;
1.40      noro     8329:                         }
1.1       noro     8330:                     }
                   8331:                     break;
                   8332:                 case 4:
                   8333:                     ivi = ivect->index.i;
                   8334:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8335:                         pos = prev+ivi[j]; c1 = CM(mr);
                   8336:                         prev = pos;
1.40      noro     8337:                         if ( c1 ) {
                   8338:                           c2 = svect[pos];
1.1       noro     8339:                           DMA(c1,c,c2,up,lo);
                   8340:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   8341:                           } else svect[pos] = lo;
1.40      noro     8342:                         }
1.1       noro     8343:                     }
                   8344:                     break;
                   8345:             }
                   8346:         }
                   8347:     }
                   8348:     for ( i = 0; i < col; i++ )
                   8349:         if ( svect[i] >= (UINT)m ) svect[i] %= m;
                   8350:     return maxrs;
                   8351: }
                   8352:
                   8353: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8354: {
                   8355:     int i,j,k,len,pos,prev;
                   8356:     UINT c,c1,c2,c3,up,lo,dmy;
                   8357:     IndArray ivect;
                   8358:     unsigned char *ivc;
                   8359:     unsigned short *ivs;
                   8360:     unsigned int *ivi;
                   8361:     NDV redv;
                   8362:     NMV mr;
                   8363:     NODE rp;
                   8364:     int maxrs;
                   8365:
                   8366:     maxrs = 0;
                   8367:     for ( i = 0; i < nred; i++ ) {
                   8368:         ivect = imat[i];
                   8369:         k = ivect->head;
                   8370:         if ( (c = svect[k]) != 0 ) {
                   8371:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8372:             c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
                   8373:             len = LEN(redv); mr = BDY(redv);
                   8374:             svect[k] = 0; prev = k;
                   8375:             switch ( ivect->width ) {
                   8376:                 case 1:
                   8377:                     ivc = ivect->index.c;
                   8378:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8379:                         pos = prev+ivc[j]; prev = pos;
                   8380:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   8381:                     }
                   8382:                     break;
                   8383:                 case 2:
                   8384:                     ivs = ivect->index.s;
                   8385:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8386:                         pos = prev+ivs[j]; prev = pos;
                   8387:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   8388:                     }
                   8389:                     break;
                   8390:                 case 4:
                   8391:                     ivi = ivect->index.i;
                   8392:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8393:                         pos = prev+ivi[j]; prev = pos;
                   8394:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   8395:                     }
                   8396:                     break;
                   8397:             }
                   8398:         }
                   8399:     }
                   8400:     return maxrs;
                   8401: }
                   8402:
                   8403: ND nd_add_lf(ND p1,ND p2)
                   8404: {
                   8405:     int n,c,can;
                   8406:     ND r;
                   8407:     NM m1,m2,mr0,mr,s;
                   8408:     Z t;
                   8409:
                   8410:     if ( !p1 ) return p2;
                   8411:     else if ( !p2 ) return p1;
                   8412:     else {
                   8413:         can = 0;
                   8414:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   8415:             c = DL_COMPARE(DL(m1),DL(m2));
                   8416:             switch ( c ) {
                   8417:                 case 0:
                   8418:                     addlf(CZ(m1),CZ(m2),&t);
                   8419:                     s = m1; m1 = NEXT(m1);
                   8420:                     if ( t ) {
                   8421:                         can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
                   8422:                     } else {
                   8423:                         can += 2; FREENM(s);
                   8424:                     }
                   8425:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   8426:                     break;
                   8427:                 case 1:
                   8428:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   8429:                     break;
                   8430:                 case -1:
                   8431:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   8432:                     break;
                   8433:             }
                   8434:         }
                   8435:         if ( !mr0 )
                   8436:             if ( m1 ) mr0 = m1;
                   8437:             else if ( m2 ) mr0 = m2;
                   8438:             else return 0;
                   8439:         else if ( m1 ) NEXT(mr) = m1;
                   8440:         else if ( m2 ) NEXT(mr) = m2;
                   8441:         else NEXT(mr) = 0;
                   8442:         BDY(p1) = mr0;
                   8443:         SG(p1) = MAX(SG(p1),SG(p2));
                   8444:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   8445:         FREEND(p2);
                   8446:         return p1;
                   8447:     }
                   8448: }
                   8449:
                   8450: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8451: {
                   8452:     int i,j,k,len,pos,prev;
                   8453:     mpz_t c,mc,c1;
                   8454:     IndArray ivect;
                   8455:     unsigned char *ivc;
                   8456:     unsigned short *ivs;
                   8457:     unsigned int *ivi;
                   8458:     NDV redv;
                   8459:     NMV mr;
                   8460:     NODE rp;
                   8461:     int maxrs;
                   8462:
                   8463:     maxrs = 0;
                   8464:     lf_lazy = 1;
                   8465:     for ( i = 0; i < nred; i++ ) {
                   8466:         ivect = imat[i];
                   8467:         k = ivect->head;
                   8468:         mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
                   8469:         if ( mpz_sgn(svect[k]) ) {
                   8470:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8471:             mpz_neg(svect[k],svect[k]);
                   8472:             redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
                   8473:             len = LEN(redv); mr = BDY(redv);
                   8474:             prev = k;
                   8475:             switch ( ivect->width ) {
                   8476:                 case 1:
                   8477:                     ivc = ivect->index.c;
                   8478:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8479:                         pos = prev+ivc[j]; prev = pos;
                   8480:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   8481:                     }
                   8482:                     break;
                   8483:                 case 2:
                   8484:                     ivs = ivect->index.s;
                   8485:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8486:                         pos = prev+ivs[j]; prev = pos;
                   8487:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   8488:                     }
                   8489:                     break;
                   8490:                 case 4:
                   8491:                     ivi = ivect->index.i;
                   8492:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8493:                         pos = prev+ivi[j]; prev = pos;
                   8494:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   8495:                     }
                   8496:                     break;
                   8497:             }
                   8498:             mpz_set_ui(svect[k],0);
                   8499:         }
                   8500:     }
                   8501:     lf_lazy=0;
                   8502:     for ( i = 0; i < col; i++ ) {
                   8503:         mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
                   8504:     }
                   8505:     return maxrs;
                   8506: }
                   8507:
                   8508: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
                   8509: {
                   8510:     int i,j,k,l,rank,s;
                   8511:     mpz_t a,a1,inv;
                   8512:     mpz_t *t,*pivot,*pk;
                   8513:     mpz_t **mat;
                   8514:     struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
                   8515:     int size,size1;
                   8516:
                   8517:     mpz_init(inv);
                   8518:     mpz_init(a);
                   8519:     mat = (mpz_t **)mat0;
                   8520:         size = 0;
                   8521:     for ( rank = 0, j = 0; j < col; j++ ) {
                   8522:         for ( i = rank; i < row; i++ ) {
                   8523:             mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
                   8524:         }
                   8525:         for ( i = rank; i < row; i++ )
                   8526:             if ( mpz_sgn(mat[i][j]) )
                   8527:                 break;
                   8528:         if ( i == row ) {
                   8529:             colstat[j] = 0;
                   8530:             continue;
                   8531:         } else
                   8532:             colstat[j] = 1;
                   8533:         if ( i != rank ) {
                   8534:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   8535:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   8536:         }
                   8537:         pivot = mat[rank];
                   8538:         s = sugar[rank];
                   8539:         mpz_invert(inv,pivot[j],BDY(current_mod_lf));
                   8540:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   8541:             if ( mpz_sgn(*pk) ) {
                   8542:                 mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
                   8543:             }
                   8544:         for ( i = rank+1; i < row; i++ ) {
                   8545:             t = mat[i];
                   8546:             if ( mpz_sgn(t[j]) ) {
                   8547:                 sugar[i] = MAX(sugar[i],s);
                   8548:                 mpz_neg(a,t[j]);
                   8549:                 red_by_vect_lf(t+j,pivot+j,a,col-j);
                   8550:             }
                   8551:         }
                   8552:         rank++;
                   8553:     }
                   8554:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   8555:         if ( colstat[j] ) {
                   8556:             pivot = mat[l];
                   8557:             s = sugar[l];
                   8558:             for ( k = j; k < col; k++ )
                   8559:               mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
                   8560:             for ( i = 0; i < l; i++ ) {
                   8561:                 t = mat[i];
                   8562:                 if ( mpz_sgn(t[j]) ) {
                   8563:                     sugar[i] = MAX(sugar[i],s);
                   8564:                     mpz_neg(a,t[j]);
                   8565:                     red_by_vect_lf(t+j,pivot+j,a,col-j);
                   8566:                 }
                   8567:             }
                   8568:             l--;
                   8569:         }
                   8570:     for ( j = 0, l = 0; l < rank; j++ )
                   8571:         if ( colstat[j] ) {
                   8572:             t = mat[l];
                   8573:             for ( k = j; k < col; k++ ) {
                   8574:                 mpz_mod(t[k],t[k],BDY(current_mod_lf));
                   8575:             }
                   8576:             l++;
                   8577:         }
                   8578:     return rank;
                   8579: }
                   8580:
                   8581:
                   8582: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   8583: {
                   8584:     int j,k,len;
                   8585:     UINT *p;
                   8586:     UINT c;
                   8587:     NDV r;
                   8588:     NMV mr0,mr;
                   8589:
                   8590:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   8591:     if ( !len ) return 0;
                   8592:     else {
                   8593:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8594: #if 0
                   8595:         ndv_alloc += nmv_adv*len;
                   8596: #endif
                   8597:         mr = mr0;
                   8598:         p = s0vect;
                   8599:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   8600:             if ( !rhead[j] ) {
                   8601:                 if ( (c = vect[k++]) != 0 ) {
                   8602:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   8603:                 }
                   8604:             }
                   8605:         MKNDV(nd_nvar,mr0,len,r);
                   8606:         return r;
                   8607:     }
                   8608: }
                   8609:
1.40      noro     8610: NDV vect_to_ndv_s(UINT *vect,int col,UINT *s0vect)
                   8611: {
                   8612:     int j,k,len;
                   8613:     UINT *p;
                   8614:     UINT c;
                   8615:     NDV r;
                   8616:     NMV mr0,mr;
                   8617:
                   8618:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   8619:     if ( !len ) return 0;
                   8620:     else {
                   8621:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8622:         mr = mr0;
                   8623:         p = s0vect;
                   8624:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   8625:           if ( (c = vect[k++]) != 0 ) {
                   8626:             ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   8627:           }
                   8628:         MKNDV(nd_nvar,mr0,len,r);
                   8629:         return r;
                   8630:     }
                   8631: }
                   8632:
1.1       noro     8633: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
                   8634: {
                   8635:     int j,k,len;
                   8636:     UINT *p;
                   8637:     NDV r;
                   8638:     NMV mr0,mr;
                   8639:
                   8640:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
                   8641:     if ( !len ) return 0;
                   8642:     else {
                   8643:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8644:         mr = mr0;
                   8645:         p = s0vect;
                   8646:         for ( j = 0; j < col; j++, p += nd_wpd )
                   8647:       if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
                   8648:             ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
                   8649:           }
                   8650:         MKNDV(nd_nvar,mr0,len,r);
                   8651:         return r;
                   8652:     }
                   8653: }
                   8654:
                   8655: /* for preprocessed vector */
                   8656:
                   8657: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   8658: {
1.6       noro     8659:   int j,k,len;
                   8660:   UINT *p;
                   8661:   Z c;
                   8662:   NDV r;
                   8663:   NMV mr0,mr;
1.1       noro     8664:
1.6       noro     8665:   for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   8666:   if ( !len ) return 0;
                   8667:   else {
                   8668:     mr0 = (NMV)MALLOC(nmv_adv*len);
1.1       noro     8669: #if 0
1.6       noro     8670:     ndv_alloc += nmv_adv*len;
1.1       noro     8671: #endif
1.6       noro     8672:     mr = mr0;
                   8673:     p = s0vect;
                   8674:     for ( j = k = 0; j < col; j++, p += nd_wpd ) {
                   8675:       if ( !rhead[j] ) {
                   8676:         if ( (c = vect[k++]) != 0 ) {
                   8677:           if ( !INT(c) )
                   8678:             error("vect_to_ndv_q : components must be integers");
                   8679:             ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
                   8680:         }
                   8681:       }
1.1       noro     8682:     }
1.6       noro     8683:     MKNDV(nd_nvar,mr0,len,r);
                   8684:     return r;
                   8685:   }
1.1       noro     8686: }
                   8687:
                   8688: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   8689: {
                   8690:     int j,k,len;
                   8691:     UINT *p;
                   8692:     mpz_t c;
                   8693:     NDV r;
                   8694:     NMV mr0,mr;
                   8695:
                   8696:     for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
                   8697:     if ( !len ) return 0;
                   8698:     else {
                   8699:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   8700: #if 0
                   8701:         ndv_alloc += nmv_adv*len;
                   8702: #endif
                   8703:         mr = mr0;
                   8704:         p = s0vect;
                   8705:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   8706:             if ( !rhead[j] ) {
                   8707:                 c[0] = vect[k++][0];
                   8708:                 if ( mpz_sgn(c) ) {
                   8709:                     ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
                   8710:                 }
                   8711:             }
                   8712:         MKNDV(nd_nvar,mr0,len,r);
                   8713:         return r;
                   8714:     }
                   8715: }
                   8716:
                   8717: /* for plain vector */
                   8718:
                   8719: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
                   8720: {
                   8721:     int j,k,len;
                   8722:     UINT *p;
                   8723:     Z c;
                   8724:     NDV r;
                   8725:     NMV mr0,mr;
                   8726:
                   8727:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   8728:     if ( !len ) return 0;
                   8729:     else {
                   8730:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   8731: #if 0
                   8732:         ndv_alloc += nmv_adv*len;
                   8733: #endif
                   8734:         mr = mr0;
                   8735:         p = s0vect;
                   8736:         for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   8737:             if ( (c = vect[k]) != 0 ) {
                   8738:                 if ( !INT(c) )
1.6       noro     8739:                     error("plain_vect_to_ndv_q : components must be integers");
                   8740:                 ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1       noro     8741:             }
                   8742:         MKNDV(nd_nvar,mr0,len,r);
                   8743:         return r;
                   8744:     }
                   8745: }
                   8746:
                   8747: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
                   8748: {
                   8749:     ND_pairs t;
                   8750:     NODE sp0,sp;
                   8751:     int stat;
                   8752:     ND spol;
                   8753:
                   8754:     for ( t = l; t; t = NEXT(t) ) {
                   8755:         stat = nd_sp(m,trace,t,&spol);
                   8756:         if ( !stat ) return 0;
                   8757:         if ( spol ) {
                   8758:             add_pbucket_symbolic(bucket,spol);
                   8759:         }
                   8760:     }
                   8761:     return 1;
                   8762: }
                   8763:
                   8764: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   8765: {
                   8766:     NODE rp0,rp;
                   8767:     NM mul,head,s0,s;
                   8768:     int index,col,i,sugar;
                   8769:     RHist h;
                   8770:     UINT *s0v,*p;
                   8771:     NM_ind_pair pair;
                   8772:     ND red;
                   8773:     NDV *ps;
                   8774:
                   8775:     s0 = 0; rp0 = 0; col = 0;
                   8776:   if ( nd_demand )
                   8777:       ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   8778:   else
                   8779:       ps = trace?nd_ps_trace:nd_ps;
                   8780:     while ( 1 ) {
                   8781:         head = remove_head_pbucket_symbolic(bucket);
                   8782:         if ( !head ) break;
                   8783:         if ( !s0 ) s0 = head;
                   8784:         else NEXT(s) = head;
                   8785:         s = head;
                   8786:         index = ndl_find_reducer(DL(head));
                   8787:         if ( index >= 0 ) {
                   8788:             h = nd_psh[index];
                   8789:             NEWNM(mul);
                   8790:             ndl_sub(DL(head),DL(h),DL(mul));
                   8791:             if ( ndl_check_bound2(index,DL(mul)) )
                   8792:                 return 0;
                   8793:             sugar = TD(DL(mul))+SG(ps[index]);
1.28      noro     8794:             MKNM_ind_pair(pair,mul,index,sugar,0);
1.1       noro     8795:             red = ndv_mul_nm_symbolic(mul,ps[index]);
                   8796:             add_pbucket_symbolic(bucket,nd_remove_head(red));
                   8797:             NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   8798:         }
                   8799:         col++;
                   8800:     }
                   8801:     if ( rp0 ) NEXT(rp) = 0;
                   8802:     NEXT(s) = 0;
                   8803:     s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   8804:     for ( i = 0, p = s0v, s = s0; i < col;
                   8805:         i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   8806:     *s0vect = s0v;
                   8807:     *r = rp0;
                   8808:     return col;
                   8809: }
                   8810:
                   8811: void print_ndp(ND_pairs l)
                   8812: {
                   8813:   ND_pairs t;
                   8814:
                   8815:   for ( t = l; t; t = NEXT(t) )
                   8816:     printf("[%d,%d] ",t->i1,t->i2);
                   8817:   printf("\n");
                   8818: }
                   8819:
                   8820: NODE nd_f4(int m,int checkonly,int **indp)
                   8821: {
                   8822:     int i,nh,stat,index,f4red;
                   8823:     NODE r,g,tn0,tn,node;
                   8824:     ND_pairs d,l,t,ll0,ll,lh;
                   8825:   LIST l0,l1;
                   8826:     ND spol,red;
                   8827:     NDV nf,redv;
                   8828:     NM s0,s;
                   8829:     NODE rp0,srp0,nflist,nzlist,nzlist_t;
                   8830:     int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
                   8831:     UINT c;
                   8832:     UINT **spmat;
                   8833:     UINT *s0vect,*svect,*p,*v;
                   8834:     int *colstat;
                   8835:     IndArray *imat;
                   8836:     int *rhead;
                   8837:     int spcol,sprow;
                   8838:     int sugar,sugarh;
                   8839:     PGeoBucket bucket;
                   8840:     struct oEGT eg0,eg1,eg_f4;
                   8841:     Z i1,i2,sugarq;
1.12      noro     8842:
                   8843:     init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1       noro     8844: #if 0
                   8845:     ndv_alloc = 0;
                   8846: #endif
1.11      noro     8847:     Nf4_red=0;
1.1       noro     8848:     g = 0; d = 0;
                   8849:     for ( i = 0; i < nd_psn; i++ ) {
                   8850:         d = update_pairs(d,g,i,0);
                   8851:         g = update_base(g,i);
                   8852:     }
                   8853:   nzlist = 0;
                   8854:     nzlist_t = nd_nzlist;
                   8855:     f4red = 1;
                   8856:     nd_last_nonzero = 0;
                   8857:     while ( d ) {
                   8858:         get_eg(&eg0);
                   8859:         l = nd_minsugarp(d,&d);
                   8860:         sugar = nd_sugarweight?l->sugar2:SG(l);
                   8861:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   8862:         if ( nzlist_t ) {
                   8863:             node = BDY((LIST)BDY(nzlist_t));
1.6       noro     8864:             sugarh = ZTOS((Q)ARG0(node));
1.1       noro     8865:             tn = BDY((LIST)ARG1(node));
                   8866:             if ( !tn ) {
                   8867:               nzlist_t = NEXT(nzlist_t);
                   8868:               continue;
                   8869:             }
                   8870:             /* tn = [[i1,i2],...] */
                   8871:             lh = nd_ipairtospair(tn);
                   8872:         }
                   8873:         bucket = create_pbucket();
                   8874:         stat = nd_sp_f4(m,0,l,bucket);
                   8875:         if ( !stat ) {
                   8876:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8877:             NEXT(t) = d; d = l;
                   8878:             d = nd_reconstruct(0,d);
                   8879:             continue;
                   8880:         }
                   8881:         if ( bucket->m < 0 ) continue;
                   8882:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   8883:         if ( !col ) {
                   8884:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8885:             NEXT(t) = d; d = l;
                   8886:             d = nd_reconstruct(0,d);
                   8887:             continue;
                   8888:         }
1.12      noro     8889:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1       noro     8890:         if ( DP_Print )
1.6       noro     8891:             fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5       noro     8892:                 sugar,eg_f4.exectime);
1.1       noro     8893:         nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
                   8894:         if ( checkonly && nflist ) return 0;
                   8895:         /* adding new bases */
                   8896:         if ( nflist ) nd_last_nonzero = f4red;
                   8897:         for ( r = nflist; r; r = NEXT(r) ) {
                   8898:             nf = (NDV)BDY(r);
1.38      noro     8899:             if ( nd_f4_td ) SG(nf) = nd_tdeg(nf);
1.1       noro     8900:             ndv_removecont(m,nf);
                   8901:             if ( !m && nd_nalg ) {
                   8902:                 ND nf1;
                   8903:
                   8904:                 nf1 = ndvtond(m,nf);
                   8905:                 nd_monic(0,&nf1);
                   8906:                 nd_removecont(m,nf1);
                   8907:                 nf = ndtondv(m,nf1);
                   8908:             }
1.24      noro     8909:             nh = ndv_newps(m,nf,0);
1.1       noro     8910:             d = update_pairs(d,g,nh,0);
                   8911:             g = update_base(g,nh);
                   8912:         }
                   8913:         if ( DP_Print ) {
                   8914:           fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
                   8915:         }
                   8916:         if ( nd_gentrace ) {
                   8917:       for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
                   8918:         NEXTNODE(tn0,tn);
1.6       noro     8919:                 STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1       noro     8920:                 node = mknode(2,i1,i2); MKLIST(l0,node);
                   8921:         BDY(tn) = l0;
                   8922:       }
                   8923:       if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6       noro     8924:             STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1       noro     8925:             MKNODE(node,l1,nzlist); nzlist = node;
                   8926:         }
                   8927:         if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
                   8928:         f4red++;
                   8929:         if ( nd_f4red && f4red > nd_f4red ) break;
                   8930:         if ( nd_rank0 && !nflist ) break;
                   8931:     }
                   8932:     if ( nd_gentrace ) {
                   8933:     MKLIST(l0,reverse_node(nzlist));
                   8934:         MKNODE(nd_alltracelist,l0,0);
                   8935:     }
                   8936: #if 0
                   8937:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   8938: #endif
1.12      noro     8939:   if ( DP_Print ) {
                   8940:     fprintf(asir_out,"number of red=%d,",Nf4_red);
                   8941:     fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
                   8942:       f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
1.41      noro     8943:     fprintf(asir_out,"number of removed pairs=%d\n,",NcriB+NcriMF+Ncri2);
1.12      noro     8944:   }
1.1       noro     8945:   conv_ilist(nd_demand,0,g,indp);
                   8946:     return g;
                   8947: }
                   8948:
                   8949: NODE nd_f4_trace(int m,int **indp)
                   8950: {
                   8951:     int i,nh,stat,index;
                   8952:     NODE r,g;
                   8953:     ND_pairs d,l,l0,t;
                   8954:     ND spol,red;
                   8955:     NDV nf,redv,nfqv,nfv;
                   8956:     NM s0,s;
                   8957:     NODE rp0,srp0,nflist;
                   8958:     int nsp,nred,col,rank,len,k,j,a;
                   8959:     UINT c;
                   8960:     UINT **spmat;
                   8961:     UINT *s0vect,*svect,*p,*v;
                   8962:     int *colstat;
                   8963:     IndArray *imat;
                   8964:     int *rhead;
                   8965:     int spcol,sprow;
                   8966:     int sugar;
                   8967:     PGeoBucket bucket;
                   8968:     struct oEGT eg0,eg1,eg_f4;
                   8969:
                   8970:     g = 0; d = 0;
                   8971:     for ( i = 0; i < nd_psn; i++ ) {
                   8972:         d = update_pairs(d,g,i,0);
                   8973:         g = update_base(g,i);
                   8974:     }
                   8975:     while ( d ) {
                   8976:         get_eg(&eg0);
                   8977:         l = nd_minsugarp(d,&d);
                   8978:         sugar = SG(l);
                   8979:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   8980:         bucket = create_pbucket();
                   8981:         stat = nd_sp_f4(m,0,l,bucket);
                   8982:         if ( !stat ) {
                   8983:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8984:             NEXT(t) = d; d = l;
                   8985:             d = nd_reconstruct(1,d);
                   8986:             continue;
                   8987:         }
                   8988:         if ( bucket->m < 0 ) continue;
                   8989:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   8990:         if ( !col ) {
                   8991:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8992:             NEXT(t) = d; d = l;
                   8993:             d = nd_reconstruct(1,d);
                   8994:             continue;
                   8995:         }
                   8996:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   8997:         if ( DP_Print )
1.3       noro     8998:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5       noro     8999:                 sugar,eg_f4.exectime);
1.1       noro     9000:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   9001:         if ( !l0 ) continue;
                   9002:         l = l0;
                   9003:
                   9004:         /* over Q */
                   9005:         bucket = create_pbucket();
                   9006:         stat = nd_sp_f4(0,1,l,bucket);
                   9007:         if ( !stat ) {
                   9008:             for ( t = l; NEXT(t); t = NEXT(t) );
                   9009:             NEXT(t) = d; d = l;
                   9010:             d = nd_reconstruct(1,d);
                   9011:             continue;
                   9012:         }
                   9013:         if ( bucket->m < 0 ) continue;
                   9014:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   9015:         if ( !col ) {
                   9016:             for ( t = l; NEXT(t); t = NEXT(t) );
                   9017:             NEXT(t) = d; d = l;
                   9018:             d = nd_reconstruct(1,d);
                   9019:             continue;
                   9020:         }
                   9021:         nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   9022:         /* adding new bases */
                   9023:         for ( r = nflist; r; r = NEXT(r) ) {
                   9024:             nfqv = (NDV)BDY(r);
                   9025:             ndv_removecont(0,nfqv);
1.6       noro     9026:             if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1       noro     9027:             if ( nd_nalg ) {
                   9028:                 ND nf1;
                   9029:
                   9030:                 nf1 = ndvtond(m,nfqv);
                   9031:                 nd_monic(0,&nf1);
                   9032:                 nd_removecont(0,nf1);
                   9033:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   9034:             }
                   9035:             nfv = ndv_dup(0,nfqv);
                   9036:             ndv_mod(m,nfv);
                   9037:             ndv_removecont(m,nfv);
1.24      noro     9038:             nh = ndv_newps(0,nfv,nfqv);
1.1       noro     9039:             d = update_pairs(d,g,nh,0);
                   9040:             g = update_base(g,nh);
                   9041:         }
                   9042:     }
                   9043: #if 0
                   9044:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   9045: #endif
                   9046:   conv_ilist(nd_demand,1,g,indp);
                   9047:     return g;
                   9048: }
                   9049:
                   9050: int rref(matrix mat,int *sugar)
                   9051: {
                   9052:   int row,col,i,j,k,l,s,wcol,wj;
                   9053:   unsigned long bj;
                   9054:   unsigned long **a;
                   9055:   unsigned long *ai,*ak,*as,*t;
                   9056:   int *pivot;
                   9057:
                   9058:   row = mat->row;
                   9059:   col = mat->col;
                   9060:   a = mat->a;
                   9061:   wcol = (col+BLEN-1)/BLEN;
                   9062:   pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   9063:   i = 0;
                   9064:   for ( j = 0; j < col; j++ ) {
                   9065:   wj = j/BLEN; bj = 1L<<(j%BLEN);
                   9066:     for ( k = i; k < row; k++ )
                   9067:     if ( a[k][wj] & bj ) break;
                   9068:     if ( k == row ) continue;
                   9069:   pivot[i] = j;
                   9070:     if ( k != i ) {
                   9071:    t = a[i]; a[i] = a[k]; a[k] = t;
                   9072:    s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
                   9073:   }
                   9074:   ai = a[i];
                   9075:     for ( k = i+1; k < row; k++ ) {
                   9076:     ak = a[k];
                   9077:     if ( ak[wj] & bj ) {
                   9078:       for ( l = wj; l < wcol; l++ )
                   9079:       ak[l] ^= ai[l];
                   9080:       sugar[k] = MAX(sugar[k],sugar[i]);
                   9081:     }
                   9082:   }
                   9083:   i++;
                   9084:   }
                   9085:   for ( k = i-1; k >= 0; k-- ) {
                   9086:     j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
                   9087:   ak = a[k];
                   9088:     for ( s = 0; s < k; s++ ) {
                   9089:     as = a[s];
                   9090:       if ( as[wj] & bj ) {
                   9091:         for ( l = wj; l < wcol; l++ )
                   9092:       as[l] ^= ak[l];
                   9093:       sugar[s] = MAX(sugar[s],sugar[k]);
                   9094:     }
                   9095:   }
                   9096:   }
                   9097:   return i;
                   9098: }
                   9099:
                   9100: void print_matrix(matrix mat)
                   9101: {
                   9102:   int row,col,i,j;
                   9103:   unsigned long *ai;
                   9104:
                   9105:   row = mat->row;
                   9106:   col = mat->col;
                   9107:   printf("%d x %d\n",row,col);
                   9108:   for ( i = 0; i < row; i++ ) {
                   9109:   ai = mat->a[i];
                   9110:     for ( j = 0; j < col; j++ ) {
                   9111:     if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
                   9112:     else putchar('0');
                   9113:   }
                   9114:   putchar('\n');
                   9115:   }
                   9116: }
                   9117:
                   9118: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
                   9119:
                   9120: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
                   9121: {
                   9122:   int row,col,wcol,wj,i,j;
                   9123:   unsigned long bj;
                   9124:   unsigned long *ai;
                   9125:   unsigned long **a;
                   9126:   int len;
                   9127:   int *pos;
                   9128:
                   9129:   row = mat->row;
                   9130:   col = mat->col;
                   9131:   wcol = (col+BLEN-1)/BLEN;
                   9132:   pos = (int *)MALLOC(wcol*sizeof(int));
                   9133:   bzero(pos,wcol*sizeof(int));
                   9134:   for ( i = j = 0; i < wcol; i++ )
                   9135:     if ( v[i] ) pos[j++] = i;;
                   9136:   len = j;
                   9137:   wj = rhead/BLEN;
                   9138:   bj = 1L<<rhead%BLEN;
                   9139:   a = mat->a;
                   9140:   for ( i = 0; i < row; i++ ) {
                   9141:   ai = a[i];
                   9142:     if ( ai[wj]&bj ) {
                   9143:     for ( j = 0; j < len; j++ )
                   9144:       ai[pos[j]] ^= v[pos[j]];
                   9145:     sugar[i] = MAX(sugar[i],rsugar);
                   9146:   }
                   9147:   }
                   9148: }
                   9149:
                   9150: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   9151: {
                   9152:     int nsp,nred,i,i0,k,rank,row;
                   9153:     NODE r0,rp;
                   9154:     ND_pairs sp;
                   9155:   ND spol;
                   9156:   NM_ind_pair rt;
                   9157:     int *s0hash;
                   9158:   UINT *s;
                   9159:   int *pivot,*sugar,*head;
                   9160:   matrix mat;
                   9161:     NM m;
                   9162:     NODE r;
                   9163:   struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
                   9164:   int rhead,rsugar,size;
                   9165:     unsigned long *v;
                   9166:
                   9167:     get_eg(&eg0);
                   9168:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   9169:     nred = length(rp0);
                   9170:     mat = alloc_matrix(nsp,col);
                   9171:     s0hash = (int *)MALLOC(col*sizeof(int));
                   9172:     for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
                   9173:         s0hash[i] = ndl_hash_value(s);
                   9174:
                   9175:   sugar = (int *)MALLOC(nsp*sizeof(int));
                   9176:   for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
                   9177:     nd_sp(2,0,sp,&spol);
                   9178:     if ( spol ) {
                   9179:         mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
                   9180:       sugar[i] = SG(spol);
                   9181:       i++;
                   9182:     }
                   9183:   }
                   9184:   mat->row = i;
                   9185:     if ( DP_Print ) {
                   9186:       fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
                   9187:     }
                   9188:   size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
                   9189:   v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
                   9190:     for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
                   9191:     rt = (NM_ind_pair)BDY(rp);
                   9192:     bzero(v,size);
                   9193:         rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
                   9194:     rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
                   9195:       red_by_vect_2(mat,sugar,v,rhead,rsugar);
                   9196:   }
                   9197:
                   9198:     get_eg(&eg1);
                   9199:     init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
                   9200:   rank = rref(mat,sugar);
                   9201:
                   9202:     for ( i = 0, r0 = 0; i < rank; i++ ) {
                   9203:       NEXTNODE(r0,r);
                   9204:     BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
                   9205:       SG((NDV)BDY(r)) = sugar[i];
                   9206:     }
                   9207:     if ( r0 ) NEXT(r) = 0;
                   9208:     get_eg(&eg2);
                   9209:     init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
                   9210:     if ( DP_Print ) {
                   9211:         fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5       noro     9212:       eg_elim1.exectime,eg_elim2.exectime);
1.1       noro     9213:         fflush(asir_out);
                   9214:   }
                   9215:     return r0;
                   9216: }
                   9217:
                   9218:
                   9219: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   9220: {
                   9221:     IndArray *imat;
1.11      noro     9222:     int nsp,nred,i,start;
1.1       noro     9223:     int *rhead;
                   9224:     NODE r0,rp;
                   9225:     ND_pairs sp;
                   9226:     NM_ind_pair *rvect;
                   9227:     UINT *s;
                   9228:     int *s0hash;
1.11      noro     9229:     struct oEGT eg0,eg1,eg_conv;
1.1       noro     9230:
                   9231:     if ( m == 2 && nd_rref2 )
                   9232:      return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
                   9233:
                   9234:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   9235:     nred = length(rp0);
                   9236:     imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   9237:     rhead = (int *)MALLOC(col*sizeof(int));
                   9238:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   9239:
                   9240:     /* construction of index arrays */
1.11      noro     9241:     get_eg(&eg0);
1.1       noro     9242:     if ( DP_Print ) {
1.11      noro     9243:       fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   9244:       fflush(asir_out);
1.1       noro     9245:     }
                   9246:     rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11      noro     9247:     for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1       noro     9248:         rvect[i] = (NM_ind_pair)BDY(rp);
1.11      noro     9249:         imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1       noro     9250:         rhead[imat[i]->head] = 1;
1.11      noro     9251:         start = imat[i]->head;
                   9252:     }
1.12      noro     9253:     get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11      noro     9254:     if ( DP_Print ) {
                   9255:       fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   9256:       fflush(asir_out);
1.1       noro     9257:     }
                   9258:     if ( m > 0 )
1.7       noro     9259: #if SIZEOF_LONG==8
1.1       noro     9260:         r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   9261: #else
                   9262:         r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   9263: #endif
                   9264:     else if ( m == -1 )
                   9265:         r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   9266:     else if ( m == -2 )
                   9267:         r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   9268:     else
                   9269:         r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   9270:     return r0;
                   9271: }
                   9272:
                   9273: /* for Fp, 2<=p<2^16 */
                   9274:
                   9275: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   9276:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   9277: {
                   9278:     int spcol,sprow,a;
                   9279:     int i,j,k,l,rank;
                   9280:     NODE r0,r;
                   9281:     ND_pairs sp;
                   9282:     ND spol;
                   9283:     UINT **spmat;
                   9284:     UINT *svect,*v;
                   9285:     int *colstat;
                   9286:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9287:     int maxrs;
                   9288:     int *spsugar;
                   9289:     ND_pairs *spactive;
                   9290:
                   9291:     spcol = col-nred;
                   9292:     get_eg(&eg0);
                   9293:     /* elimination (1st step) */
                   9294:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   9295:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   9296:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9297:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   9298:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9299:         nd_sp(m,0,sp,&spol);
                   9300:         if ( !spol ) continue;
                   9301:         nd_to_vect(m,s0vect,col,spol,svect);
                   9302:         if ( m == -1 )
                   9303:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   9304:         else
1.40      noro     9305:             maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,0);
1.1       noro     9306:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9307:         if ( i < col ) {
                   9308:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   9309:             for ( j = k = 0; j < col; j++ )
                   9310:                 if ( !rhead[j] ) v[k++] = svect[j];
                   9311:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9312:             if ( nz )
                   9313:             spactive[sprow] = sp;
                   9314:             sprow++;
                   9315:         }
                   9316:         nd_free(spol);
                   9317:     }
                   9318:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9319:     if ( DP_Print ) {
1.5       noro     9320:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9321:         fflush(asir_out);
                   9322:     }
                   9323:     /* free index arrays */
                   9324:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   9325:
                   9326:     /* elimination (2nd step) */
                   9327:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9328:     if ( m == -1 )
                   9329:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   9330:     else
                   9331:         rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   9332:     r0 = 0;
                   9333:     for ( i = 0; i < rank; i++ ) {
                   9334:         NEXTNODE(r0,r); BDY(r) =
                   9335:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   9336:         SG((NDV)BDY(r)) = spsugar[i];
                   9337:         GCFREE(spmat[i]);
                   9338:     }
                   9339:     if ( r0 ) NEXT(r) = 0;
                   9340:
                   9341:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   9342:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9343:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9344:     if ( DP_Print ) {
1.5       noro     9345:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9346:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9347:             nsp,nred,sprow,spcol,rank);
1.5       noro     9348:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9349:     }
                   9350:     if ( nz ) {
                   9351:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   9352:         if ( rank > 0 ) {
                   9353:             NEXT(spactive[rank-1]) = 0;
                   9354:             *nz = spactive[0];
                   9355:         } else
                   9356:             *nz = 0;
                   9357:     }
                   9358:     return r0;
                   9359: }
                   9360:
1.40      noro     9361: NODE nd_f4_red_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   9362:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
                   9363: {
                   9364:     int spcol,sprow,a;
                   9365:     int i,j,k,l,rank;
                   9366:     NODE r0,r;
                   9367:     ND_pairs sp;
                   9368:     ND spol;
                   9369:     UINT **spmat;
                   9370:     UINT *svect,*cvect;
                   9371:     UINT *v;
                   9372:     int *colstat;
                   9373:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9374:     int maxrs;
                   9375:     int *spsugar;
                   9376:     ND_pairs *spactive;
                   9377:     SIG *spsig;
                   9378:
                   9379:     get_eg(&eg0);
                   9380:     /* elimination (1st step) */
                   9381:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   9382:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9383:     spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
                   9384:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9385:         nd_sp(m,0,sp,&spol);
                   9386:         if ( !spol ) {
                   9387:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
                   9388:           continue;
                   9389:         }
                   9390:         svect = (UINT *)MALLOC(col*sizeof(UINT));
                   9391:         nd_to_vect(m,s0vect,col,spol,svect);
                   9392:         maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,spol->sig);
                   9393:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9394:         if ( i < col ) {
                   9395:             spmat[sprow] = svect;
                   9396:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9397:             spsig[sprow] = sp->sig;
                   9398:             sprow++;
                   9399:         } else {
                   9400:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
                   9401:         }
                   9402:         nd_free(spol);
                   9403:     }
                   9404:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
                   9405:     if ( DP_Print ) {
                   9406:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   9407:         fflush(asir_out);
                   9408:     }
                   9409:     /* free index arrays */
                   9410:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   9411:
                   9412:     /* elimination (2nd step) */
                   9413:     colstat = (int *)MALLOC(col*sizeof(int));
                   9414:     rank = nd_gauss_elim_mod_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
                   9415:     r0 = 0;
                   9416:     for ( i = 0; i < sprow; i++ ) {
                   9417:         if ( spsugar[i] >= 0 ) {
                   9418:           NEXTNODE(r0,r);
                   9419:           BDY(r) = vect_to_ndv_s(spmat[i],col,s0vect);
                   9420:           SG((NDV)BDY(r)) = spsugar[i];
                   9421:           ((NDV)BDY(r))->sig = spsig[i];
                   9422:         } else
                   9423:           syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
                   9424:         GCFREE(spmat[i]);
                   9425:     }
                   9426:     if ( r0 ) NEXT(r) = 0;
                   9427:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
                   9428:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9429:     if ( DP_Print ) {
                   9430:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   9431:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9432:             nsp,nred,sprow,col,rank);
                   9433:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   9434:     }
                   9435:     return r0;
                   9436: }
                   9437:
1.1       noro     9438:
                   9439: /* for small finite fields */
                   9440:
                   9441: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   9442:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   9443: {
                   9444:     int spcol,sprow,a;
                   9445:     int i,j,k,l,rank;
                   9446:     NODE r0,r;
                   9447:     ND_pairs sp;
                   9448:     ND spol;
                   9449:     UINT **spmat;
                   9450:     UINT *svect,*v;
                   9451:     int *colstat;
                   9452:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9453:     int maxrs;
                   9454:     int *spsugar;
                   9455:     ND_pairs *spactive;
                   9456:
                   9457:     spcol = col-nred;
                   9458:     get_eg(&eg0);
                   9459:     /* elimination (1st step) */
                   9460:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   9461:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   9462:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9463:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   9464:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9465:         nd_sp(m,0,sp,&spol);
                   9466:         if ( !spol ) continue;
                   9467:         nd_to_vect(m,s0vect,col,spol,svect);
                   9468:         maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   9469:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9470:         if ( i < col ) {
                   9471:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   9472:             for ( j = k = 0; j < col; j++ )
                   9473:                 if ( !rhead[j] ) v[k++] = svect[j];
                   9474:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9475:             if ( nz )
                   9476:             spactive[sprow] = sp;
                   9477:             sprow++;
                   9478:         }
                   9479:         nd_free(spol);
                   9480:     }
                   9481:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9482:     if ( DP_Print ) {
1.5       noro     9483:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9484:         fflush(asir_out);
                   9485:     }
                   9486:     /* free index arrays */
                   9487:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   9488:
                   9489:     /* elimination (2nd step) */
                   9490:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9491:     rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   9492:     r0 = 0;
                   9493:     for ( i = 0; i < rank; i++ ) {
                   9494:         NEXTNODE(r0,r); BDY(r) =
                   9495:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   9496:         SG((NDV)BDY(r)) = spsugar[i];
                   9497:         GCFREE(spmat[i]);
                   9498:     }
                   9499:     if ( r0 ) NEXT(r) = 0;
                   9500:
                   9501:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   9502:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9503:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9504:     if ( DP_Print ) {
1.5       noro     9505:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9506:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9507:             nsp,nred,sprow,spcol,rank);
1.5       noro     9508:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9509:     }
                   9510:     if ( nz ) {
                   9511:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   9512:         if ( rank > 0 ) {
                   9513:             NEXT(spactive[rank-1]) = 0;
                   9514:             *nz = spactive[0];
                   9515:         } else
                   9516:             *nz = 0;
                   9517:     }
                   9518:     return r0;
                   9519: }
                   9520:
                   9521: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   9522:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   9523: {
                   9524:     int spcol,sprow,a;
                   9525:     int i,j,k,l,rank;
                   9526:     NODE r0,r;
                   9527:     ND_pairs sp;
                   9528:     ND spol;
                   9529:     mpz_t **spmat;
                   9530:     mpz_t *svect,*v;
                   9531:     int *colstat;
                   9532:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9533:     int maxrs;
                   9534:     int *spsugar;
                   9535:     pointer *w;
                   9536:
                   9537:     spcol = col-nred;
                   9538:     get_eg(&eg0);
                   9539:     /* elimination (1st step) */
                   9540:     spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
                   9541:     svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   9542:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9543:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9544:         nd_sp(m,trace,sp,&spol);
                   9545:         if ( !spol ) continue;
                   9546:         nd_to_vect_lf(s0vect,col,spol,svect);
                   9547:         maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
                   9548:         for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
                   9549:         if ( i < col ) {
                   9550:             spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
                   9551:             for ( j = k = 0; j < col; j++ )
                   9552:                 if ( !rhead[j] ) v[k++][0] = svect[j][0];
                   9553:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9554:             sprow++;
                   9555:         }
                   9556: /*        nd_free(spol); */
                   9557:     }
                   9558:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9559:     if ( DP_Print ) {
1.5       noro     9560:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9561:         fflush(asir_out);
                   9562:     }
                   9563:     /* free index arrays */
                   9564: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   9565:
                   9566:     /* elimination (2nd step) */
                   9567:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9568:     rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
                   9569:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   9570:     for ( i = 0; i < rank; i++ ) {
                   9571: #if 0
                   9572:         w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   9573:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   9574: #else
                   9575:         w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   9576:         SG((NDV)w[i]) = spsugar[i];
                   9577: #endif
                   9578: /*        GCFREE(spmat[i]); */
                   9579:
                   9580:     }
                   9581: #if 0
                   9582:     qsort(w,rank,sizeof(NDV),
                   9583:         (int (*)(const void *,const void *))ndv_compare);
                   9584: #endif
                   9585:     r0 = 0;
                   9586:     for ( i = 0; i < rank; i++ ) {
                   9587:         NEXTNODE(r0,r); BDY(r) = w[i];
                   9588:     }
                   9589:     if ( r0 ) NEXT(r) = 0;
                   9590:
                   9591: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   9592:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9593:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9594:     if ( DP_Print ) {
1.5       noro     9595:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9596:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9597:             nsp,nred,sprow,spcol,rank);
1.5       noro     9598:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9599:     }
                   9600:     return r0;
                   9601: }
                   9602:
                   9603: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   9604:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   9605: {
                   9606:     int spcol,sprow,a;
                   9607:     int i,j,k,l,rank;
                   9608:     NODE r0,r;
                   9609:     ND_pairs sp;
                   9610:     ND spol;
                   9611:     Z **spmat;
                   9612:     Z *svect,*v;
                   9613:     int *colstat;
                   9614:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9615:     int maxrs;
                   9616:     int *spsugar;
                   9617:     pointer *w;
                   9618:
                   9619:     spcol = col-nred;
                   9620:     get_eg(&eg0);
                   9621:     /* elimination (1st step) */
                   9622:     spmat = (Z **)MALLOC(nsp*sizeof(Q *));
                   9623:     svect = (Z *)MALLOC(col*sizeof(Q));
                   9624:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9625:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9626:         nd_sp(0,trace,sp,&spol);
                   9627:         if ( !spol ) continue;
                   9628:         nd_to_vect_q(s0vect,col,spol,svect);
                   9629:         maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
                   9630:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9631:         if ( i < col ) {
                   9632:             spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
                   9633:             for ( j = k = 0; j < col; j++ )
                   9634:                 if ( !rhead[j] ) v[k++] = svect[j];
                   9635:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9636:             sprow++;
                   9637:         }
                   9638: /*        nd_free(spol); */
                   9639:     }
                   9640:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9641:     if ( DP_Print ) {
1.5       noro     9642:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9643:         fflush(asir_out);
                   9644:     }
                   9645:     /* free index arrays */
                   9646: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   9647:
                   9648:     /* elimination (2nd step) */
                   9649:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9650:     rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
                   9651:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   9652:     for ( i = 0; i < rank; i++ ) {
                   9653: #if 0
                   9654:         w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   9655:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   9656: #else
                   9657:         w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   9658:         SG((NDV)w[i]) = spsugar[i];
                   9659: #endif
                   9660: /*        GCFREE(spmat[i]); */
                   9661:     }
                   9662: #if 0
                   9663:     qsort(w,rank,sizeof(NDV),
                   9664:         (int (*)(const void *,const void *))ndv_compare);
                   9665: #endif
                   9666:     r0 = 0;
                   9667:     for ( i = 0; i < rank; i++ ) {
                   9668:         NEXTNODE(r0,r); BDY(r) = w[i];
                   9669:     }
                   9670:     if ( r0 ) NEXT(r) = 0;
                   9671:
                   9672: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   9673:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9674:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9675:     if ( DP_Print ) {
1.5       noro     9676:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9677:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9678:             nsp,nred,sprow,spcol,rank);
1.5       noro     9679:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9680:     }
                   9681:     return r0;
                   9682: }
                   9683:
                   9684: FILE *nd_write,*nd_read;
                   9685:
                   9686: void nd_send_int(int a) {
                   9687:     write_int(nd_write,(unsigned int *)&a);
                   9688: }
                   9689:
                   9690: void nd_send_intarray(int *p,int len) {
                   9691:     write_intarray(nd_write,(unsigned int *)p,len);
                   9692: }
                   9693:
                   9694: int nd_recv_int() {
                   9695:     int a;
                   9696:
                   9697:     read_int(nd_read,(unsigned int *)&a);
                   9698:     return a;
                   9699: }
                   9700:
                   9701: void nd_recv_intarray(int *p,int len) {
                   9702:     read_intarray(nd_read,(unsigned int *)p,len);
                   9703: }
                   9704:
                   9705: void nd_send_ndv(NDV p) {
                   9706:     int len,i;
                   9707:     NMV m;
                   9708:
                   9709:     if ( !p ) nd_send_int(0);
                   9710:     else {
                   9711:         len = LEN(p);
                   9712:         nd_send_int(len);
                   9713:         m = BDY(p);
                   9714:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9715:             nd_send_int(CM(m));
                   9716:             nd_send_intarray((int *)DL(m),nd_wpd);
                   9717:         }
                   9718:     }
                   9719: }
                   9720:
                   9721: void nd_send_nd(ND p) {
                   9722:     int len,i;
                   9723:     NM m;
                   9724:
                   9725:     if ( !p ) nd_send_int(0);
                   9726:     else {
                   9727:         len = LEN(p);
                   9728:         nd_send_int(len);
                   9729:         m = BDY(p);
                   9730:         for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   9731:             nd_send_int(CM(m));
                   9732:             nd_send_intarray((int *)DL(m),nd_wpd);
                   9733:         }
                   9734:     }
                   9735: }
                   9736:
                   9737: NDV nd_recv_ndv()
                   9738: {
                   9739:     int len,i;
                   9740:     NMV m,m0;
                   9741:     NDV r;
                   9742:
                   9743:     len = nd_recv_int();
                   9744:     if ( !len ) return 0;
                   9745:     else {
                   9746:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   9747: #if 0
                   9748:         ndv_alloc += len*nmv_adv;
                   9749: #endif
                   9750:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9751:             CM(m) = nd_recv_int();
                   9752:             nd_recv_intarray((int *)DL(m),nd_wpd);
                   9753:         }
                   9754:         MKNDV(nd_nvar,m0,len,r);
                   9755:         return r;
                   9756:     }
                   9757: }
                   9758:
                   9759: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
                   9760: {
                   9761:     int i,j,t,c,rank,inv;
                   9762:     int *ci,*ri;
                   9763:     Z dn;
                   9764:     MAT m,nm;
                   9765:
                   9766:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
                   9767:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   9768:     for ( i = 0; i < row; i++ )
                   9769:         for ( j = 0; j < col; j++ )
                   9770:             mat0[i][j] = 0;
                   9771:     c = col-rank;
                   9772:     for ( i = 0; i < rank; i++ ) {
                   9773:         mat0[i][ri[i]] = dn;
                   9774:         for ( j = 0; j < c; j++ )
                   9775:             mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
                   9776:     }
                   9777:     return rank;
                   9778: }
                   9779:
                   9780: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   9781: {
                   9782:     int i,j,k,l,inv,a,rank,s;
                   9783:     unsigned int *t,*pivot,*pk;
                   9784:     unsigned int **mat;
                   9785:     ND_pairs pair;
                   9786:
                   9787:     mat = (unsigned int **)mat0;
                   9788:     for ( rank = 0, j = 0; j < col; j++ ) {
                   9789:         for ( i = rank; i < row; i++ )
                   9790:             mat[i][j] %= md;
                   9791:         for ( i = rank; i < row; i++ )
                   9792:             if ( mat[i][j] )
                   9793:                 break;
                   9794:         if ( i == row ) {
                   9795:             colstat[j] = 0;
                   9796:             continue;
                   9797:         } else
                   9798:             colstat[j] = 1;
                   9799:         if ( i != rank ) {
                   9800:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   9801:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   9802:             if ( spactive ) {
                   9803:                 pair = spactive[i]; spactive[i] = spactive[rank];
                   9804:                 spactive[rank] = pair;
                   9805:             }
                   9806:         }
                   9807:         pivot = mat[rank];
                   9808:         s = sugar[rank];
                   9809:         inv = invm(pivot[j],md);
                   9810:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   9811:             if ( *pk ) {
                   9812:                 if ( *pk >= (unsigned int)md )
                   9813:                     *pk %= md;
                   9814:                 DMAR(*pk,inv,0,md,*pk)
                   9815:             }
                   9816:         for ( i = rank+1; i < row; i++ ) {
                   9817:             t = mat[i];
                   9818:             if ( (a = t[j]) != 0 ) {
                   9819:                 sugar[i] = MAX(sugar[i],s);
                   9820:                 red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   9821:             }
                   9822:         }
                   9823:         rank++;
                   9824:     }
                   9825:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   9826:         if ( colstat[j] ) {
                   9827:             pivot = mat[l];
                   9828:             s = sugar[l];
                   9829:             for ( i = 0; i < l; i++ ) {
                   9830:                 t = mat[i];
                   9831:                 t[j] %= md;
                   9832:                 if ( (a = t[j]) != 0 ) {
                   9833:                     sugar[i] = MAX(sugar[i],s);
                   9834:                     red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   9835:                 }
                   9836:             }
                   9837:             l--;
                   9838:         }
                   9839:     for ( j = 0, l = 0; l < rank; j++ )
                   9840:         if ( colstat[j] ) {
                   9841:             t = mat[l];
                   9842:             for ( k = j; k < col; k++ )
                   9843:                 if ( t[k] >= (unsigned int)md )
                   9844:                     t[k] %= md;
                   9845:             l++;
                   9846:         }
                   9847:     return rank;
                   9848: }
                   9849:
1.40      noro     9850: int nd_gauss_elim_mod_s(UINT **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat,SIG *sig)
                   9851: {
                   9852:   int i,j,k,l,rank,s,imin;
                   9853:   UINT inv;
                   9854:   UINT a;
                   9855:   UINT *t,*pivot,*pk;
                   9856:   UINT *ck;
                   9857:   UINT *ct;
                   9858:   ND_pairs pair;
                   9859:   SIG sg;
                   9860:   int *used;
                   9861:
                   9862:   used = (int *)MALLOC(row*sizeof(int));
                   9863:   for ( j = 0; j < col; j++ ) {
                   9864:     for ( i = 0; i < row; i++ )
                   9865:       a = mat[i][j] %= md;
                   9866:     for ( i = 0; i < row; i++ )
                   9867:       if ( !used[i] && mat[i][j] ) break;
                   9868:     if ( i == row ) {
                   9869:       colstat[j] = 0;
                   9870:       continue;
                   9871:     } else {
                   9872:       colstat[j] = 1;
                   9873:       used[i] = 1;
                   9874:     }
                   9875:     /* column j is normalized */
                   9876:     s = sugar[i];
                   9877:     inv = invm(mat[i][j],md);
                   9878:     /* normalize pivot row */
                   9879:     for ( k = j, pk = mat[i]+j; k < col; k++, pk++, ck++ ) {
                   9880:       DMAR(*pk,inv,0,md,*pk);
                   9881:     }
                   9882:     for ( k = i+1; k < row; k++ ) {
                   9883:       if ( (a = mat[k][j]) != 0 ) {
                   9884:         sugar[k] = MAX(sugar[k],s);
                   9885:         red_by_vect(md,mat[k]+j,mat[i]+j,(int)(md-a),col-j);
                   9886:         Nf4_red++;
                   9887:       }
                   9888:     }
                   9889:   }
                   9890:   rank = 0;
                   9891:   for ( i = 0; i < row; i++ ) {
                   9892:     for ( j = 0; j < col; j++ )
                   9893:       if ( mat[i][j] ) break;
                   9894:     if ( j == col ) sugar[i] = -1;
                   9895:     else rank++;
                   9896:   }
                   9897:   return rank;
                   9898: }
                   9899:
1.1       noro     9900:
1.7       noro     9901: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1       noro     9902: {
1.7       noro     9903:     int i,j,k,l,inv,a,rank,s;
                   9904:     unsigned int *t,*pivot,*pk;
                   9905:     unsigned int **mat;
                   9906:
                   9907:     mat = (unsigned int **)mat0;
                   9908:     for ( rank = 0, j = 0; j < col; j++ ) {
                   9909:         for ( i = rank; i < row; i++ )
                   9910:             if ( mat[i][j] )
                   9911:                 break;
                   9912:         if ( i == row ) {
                   9913:             colstat[j] = 0;
                   9914:             continue;
                   9915:         } else
                   9916:             colstat[j] = 1;
                   9917:         if ( i != rank ) {
                   9918:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   9919:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   9920:         }
                   9921:         pivot = mat[rank];
                   9922:         s = sugar[rank];
                   9923:         inv = _invsf(pivot[j]);
                   9924:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   9925:             if ( *pk )
                   9926:                 *pk = _mulsf(*pk,inv);
                   9927:         for ( i = rank+1; i < row; i++ ) {
                   9928:             t = mat[i];
                   9929:             if ( (a = t[j]) != 0 ) {
                   9930:                 sugar[i] = MAX(sugar[i],s);
                   9931:                 red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   9932:             }
                   9933:         }
                   9934:         rank++;
                   9935:     }
                   9936:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   9937:         if ( colstat[j] ) {
                   9938:             pivot = mat[l];
                   9939:             s = sugar[l];
                   9940:             for ( i = 0; i < l; i++ ) {
                   9941:                 t = mat[i];
                   9942:                 if ( (a = t[j]) != 0 ) {
                   9943:                     sugar[i] = MAX(sugar[i],s);
                   9944:                     red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   9945:                 }
                   9946:             }
                   9947:             l--;
                   9948:         }
                   9949:     return rank;
                   9950: }
1.1       noro     9951:
1.7       noro     9952: int ndv_ishomo(NDV p)
                   9953: {
                   9954:     NMV m;
                   9955:     int len,h;
1.1       noro     9956:
                   9957:     if ( !p ) return 1;
                   9958:     len = LEN(p);
                   9959:     m = BDY(p);
                   9960:     h = TD(DL(m));
                   9961:     NMV_ADV(m);
                   9962:     for ( len--; len; len--, NMV_ADV(m) )
1.20      noro     9963:         if ( TD(DL(m)) != h ) {
                   9964:           return 0;
                   9965:         }
1.1       noro     9966:     return 1;
                   9967: }
                   9968:
                   9969: void ndv_save(NDV p,int index)
                   9970: {
                   9971:     FILE *s;
                   9972:     char name[BUFSIZ];
                   9973:     short id;
                   9974:     int nv,sugar,len,n,i,td,e,j;
                   9975:     NMV m;
                   9976:     unsigned int *dl;
                   9977:     int mpos;
                   9978:
                   9979:     sprintf(name,"%s/%d",Demand,index);
                   9980:     s = fopen(name,"w");
                   9981:     savevl(s,0);
                   9982:     if ( !p ) {
                   9983:         saveobj(s,0);
                   9984:         return;
                   9985:     }
                   9986:     id = O_DP;
                   9987:     nv = NV(p);
                   9988:     sugar = SG(p);
                   9989:     len = LEN(p);
                   9990:     write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
                   9991:     write_int(s,(unsigned int *)&len);
                   9992:
                   9993:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     9994:         saveobj(s,(Obj)CZ(m));
1.1       noro     9995:         dl = DL(m);
                   9996:         td = TD(dl);
                   9997:         write_int(s,(unsigned int *)&td);
                   9998:         for ( j = 0; j < nv; j++ ) {
                   9999:             e = GET_EXP(dl,j);
                   10000:             write_int(s,(unsigned int *)&e);
                   10001:         }
                   10002:         if ( nd_module ) {
                   10003:             mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
                   10004:         }
                   10005:     }
                   10006:     fclose(s);
                   10007: }
                   10008:
                   10009: void nd_save_mod(ND p,int index)
                   10010: {
                   10011:     FILE *s;
                   10012:     char name[BUFSIZ];
                   10013:     int nv,sugar,len,c;
                   10014:     NM m;
                   10015:
                   10016:     sprintf(name,"%s/%d",Demand,index);
                   10017:     s = fopen(name,"w");
                   10018:     if ( !p ) {
                   10019:     len = 0;
                   10020:       write_int(s,(unsigned int *)&len);
                   10021:     fclose(s);
                   10022:         return;
                   10023:     }
                   10024:     nv = NV(p);
                   10025:     sugar = SG(p);
                   10026:     len = LEN(p);
                   10027:     write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
                   10028:   for ( m = BDY(p); m; m = NEXT(m) ) {
                   10029:     c = CM(m); write_int(s,(unsigned int *)&c);
                   10030:     write_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   10031:   }
                   10032:     fclose(s);
                   10033: }
                   10034:
                   10035: NDV ndv_load(int index)
                   10036: {
                   10037:     FILE *s;
                   10038:     char name[BUFSIZ];
                   10039:     short id;
                   10040:     int nv,sugar,len,n,i,td,e,j;
                   10041:     NDV d;
                   10042:     NMV m0,m;
                   10043:     unsigned int *dl;
                   10044:     Obj obj;
                   10045:     int mpos;
                   10046:
                   10047:     sprintf(name,"%s/%d",Demand,index);
                   10048:     s = fopen(name,"r");
                   10049:     if ( !s ) return 0;
                   10050:
                   10051:     skipvl(s);
                   10052:     read_short(s,(unsigned short *)&id);
                   10053:     if ( !id ) return 0;
                   10054:     read_int(s,(unsigned int *)&nv);
                   10055:     read_int(s,(unsigned int *)&sugar);
                   10056:     read_int(s,(unsigned int *)&len);
                   10057:
                   10058:     m0 = m = MALLOC(len*nmv_adv);
                   10059:     for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     10060:         loadobj(s,&obj); CZ(m) = (Z)obj;
1.1       noro     10061:         dl = DL(m);
                   10062:         ndl_zero(dl);
                   10063:         read_int(s,(unsigned int *)&td); TD(dl) = td;
                   10064:         for ( j = 0; j < nv; j++ ) {
                   10065:             read_int(s,(unsigned int *)&e);
                   10066:             PUT_EXP(dl,j,e);
                   10067:         }
                   10068:         if ( nd_module ) {
                   10069:             read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
                   10070:         }
                   10071:         if ( nd_blockmask ) ndl_weight_mask(dl);
                   10072:     }
                   10073:     fclose(s);
                   10074:     MKNDV(nv,m0,len,d);
                   10075:     SG(d) = sugar;
                   10076:     return d;
                   10077: }
                   10078:
                   10079: ND nd_load_mod(int index)
                   10080: {
                   10081:     FILE *s;
                   10082:     char name[BUFSIZ];
                   10083:     int nv,sugar,len,i,c;
                   10084:   ND d;
                   10085:     NM m0,m;
                   10086:
                   10087:     sprintf(name,"%s/%d",Demand,index);
                   10088:     s = fopen(name,"r");
                   10089:   /* if the file does not exist, it means p[index]=0 */
                   10090:     if ( !s ) return 0;
                   10091:
                   10092:     read_int(s,(unsigned int *)&nv);
                   10093:   if ( !nv ) { fclose(s); return 0; }
                   10094:
                   10095:     read_int(s,(unsigned int *)&sugar);
                   10096:     read_int(s,(unsigned int *)&len);
                   10097:   for ( m0 = 0, i = 0; i < len; i++ ) {
                   10098:     NEXTNM(m0,m);
                   10099:     read_int(s,(unsigned int *)&c); CM(m) = c;
                   10100:     read_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   10101:   }
                   10102:   NEXT(m) = 0;
                   10103:     MKND(nv,m0,len,d);
                   10104:     SG(d) = sugar;
                   10105:   fclose(s);
                   10106:     return d;
                   10107: }
                   10108:
                   10109: void nd_det(int mod,MAT f,P *rp)
                   10110: {
                   10111:     VL fv,tv;
                   10112:     int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
                   10113:     pointer **m;
                   10114:     P **w;
                   10115:     P mp,r;
                   10116:     NDV **dm;
                   10117:     NDV *t,*mi,*mj;
                   10118:     NDV d,s,mij,mjj;
                   10119:     ND u;
                   10120:     NMV nmv;
                   10121:     UINT *bound;
                   10122:     PGeoBucket bucket;
                   10123:     struct order_spec *ord;
                   10124:     Z dq,dt,ds;
                   10125:     Z mone;
                   10126:     Z gn,qn,dn0,nm,dn;
                   10127:
                   10128:     create_order_spec(0,0,&ord);
                   10129:     nd_init_ord(ord);
                   10130:     get_vars((Obj)f,&fv);
                   10131:     if ( f->row != f->col )
                   10132:         error("nd_det : non-square matrix");
                   10133:     n = f->row;
                   10134:     m = f->body;
                   10135:     for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   10136:
                   10137:     if ( !nvar ) {
                   10138:         if ( !mod )
                   10139:             detp(CO,(P **)m,n,rp);
                   10140:         else {
                   10141:             w = (P **)almat_pointer(n,n);
                   10142:             for ( i = 0; i < n; i++ )
                   10143:                 for ( j = 0; j < n; j++ )
                   10144:                     ptomp(mod,(P)m[i][j],&w[i][j]);
                   10145:             detmp(CO,mod,w,n,&mp);
                   10146:             mptop(mp,rp);
                   10147:         }
                   10148:         return;
                   10149:     }
                   10150:
                   10151:     if ( !mod ) {
                   10152:         w = (P **)almat_pointer(n,n);
                   10153:         dq = ONE;
                   10154:         for ( i = 0; i < n; i++ ) {
                   10155:             dn0 = ONE;
                   10156:             for ( j = 0; j < n; j++ ) {
                   10157:                 if ( !m[i][j] ) continue;
                   10158:                 lgp(m[i][j],&nm,&dn);
1.6       noro     10159:                 gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1       noro     10160:             }
                   10161:             if ( !UNIZ(dn0) ) {
                   10162:                 ds = dn0;
                   10163:                 for ( j = 0; j < n; j++ )
                   10164:                     mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   10165:                 mulz(dq,ds,&dt); dq = dt;
                   10166:             } else
                   10167:                 for ( j = 0; j < n; j++ )
                   10168:                     w[i][j] = (P)m[i][j];
                   10169:         }
                   10170:         m = (pointer **)w;
                   10171:     }
                   10172:
                   10173:     for ( i = 0, max = 1; i < n; i++ )
                   10174:         for ( j = 0; j < n; j++ )
                   10175:             for ( tv = fv; tv; tv = NEXT(tv) ) {
                   10176:                 e = getdeg(tv->v,(P)m[i][j]);
                   10177:                 max = MAX(e,max);
                   10178:             }
                   10179:     nd_setup_parameters(nvar,max);
                   10180:     dm = (NDV **)almat_pointer(n,n);
                   10181:     for ( i = 0, max = 1; i < n; i++ )
                   10182:         for ( j = 0; j < n; j++ ) {
                   10183:             dm[i][j] = ptondv(CO,fv,m[i][j]);
                   10184:             if ( mod ) ndv_mod(mod,dm[i][j]);
                   10185:             if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   10186:         }
                   10187:     d = ptondv(CO,fv,(P)ONE);
                   10188:     if ( mod ) ndv_mod(mod,d);
                   10189:     chsgnz(ONE,&mone);
                   10190:     for ( j = 0, sgn = 1; j < n; j++ ) {
                   10191:       if ( DP_Print ) {
                   10192:         fprintf(asir_out,".");
                   10193:       }
                   10194:         for ( i = j; i < n && !dm[i][j]; i++ );
                   10195:         if ( i == n ) {
                   10196:             *rp = 0;
                   10197:             return;
                   10198:         }
                   10199:         k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
                   10200:         for ( k = j; k < n; k++ )
                   10201:             for ( l = j; l < n; l++ )
                   10202:                 if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   10203:                     k0 = k; l0 = l; len0 = LEN(dm[k][l]);
                   10204:                 }
                   10205:         if ( k0 != j ) {
                   10206:             t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   10207:             sgn = -sgn;
                   10208:         }
                   10209:         if ( l0 != j ) {
                   10210:             for ( k = j; k < n; k++ ) {
                   10211:                 s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   10212:             }
                   10213:             sgn = -sgn;
                   10214:         }
                   10215:         bound = nd_det_compute_bound(dm,n,j);
                   10216:         for ( k = 0; k < nd_nvar; k++ )
                   10217:             if ( bound[k]*2 > nd_mask0 ) break;
                   10218:         if ( k < nd_nvar )
                   10219:             nd_det_reconstruct(dm,n,j,d);
                   10220:
                   10221:         for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
                   10222: /*            if ( DP_Print ) fprintf(asir_out,"    i=%d\n        ",i); */
                   10223:             mi = dm[i]; mij = mi[j];
                   10224:             if ( mod )
                   10225:                 ndv_mul_c(mod,mij,mod-1);
                   10226:             else
                   10227:                 ndv_mul_c_q(mij,mone);
                   10228:             for ( k = j+1; k < n; k++ ) {
                   10229: /*                if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
                   10230:                 bucket = create_pbucket();
                   10231:                 if ( mi[k] ) {
                   10232:                     nmv = BDY(mjj); len = LEN(mjj);
                   10233:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   10234:                         u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   10235:                         add_pbucket(mod,bucket,u);
                   10236:                     }
                   10237:                 }
                   10238:                 if ( mj[k] && mij ) {
                   10239:                     nmv = BDY(mij); len = LEN(mij);
                   10240:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   10241:                         u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   10242:                         add_pbucket(mod,bucket,u);
                   10243:                     }
                   10244:                 }
                   10245:                 u = nd_quo(mod,bucket,d);
                   10246:                 mi[k] = ndtondv(mod,u);
                   10247:             }
                   10248: /*            if ( DP_Print ) fprintf(asir_out,"\n",k); */
                   10249:         }
                   10250:         d = mjj;
                   10251:     }
                   10252:     if ( DP_Print ) {
                   10253:       fprintf(asir_out,"\n");
                   10254:     }
                   10255:     if ( sgn < 0 ) {
                   10256:         if ( mod )
                   10257:             ndv_mul_c(mod,d,mod-1);
                   10258:         else
                   10259:             ndv_mul_c_q(d,mone);
                   10260:     }
                   10261:     r = ndvtop(mod,CO,fv,d);
                   10262:     if ( !mod && !UNIQ(dq) )
                   10263:         divsp(CO,r,(P)dq,rp);
                   10264:     else
                   10265:         *rp = r;
                   10266: }
                   10267:
                   10268: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
                   10269: {
                   10270:     NM mr,mr0;
                   10271:     NM tnm;
                   10272:     NMV m;
                   10273:     UINT *d0,*dt,*dm;
                   10274:     int c,n,td,i,c1,c2,len;
                   10275:     Z q;
                   10276:     ND r;
                   10277:
                   10278:     if ( !p ) return 0;
                   10279:     else {
                   10280:         n = NV(p); m = BDY(p); len = LEN(p);
                   10281:         d0 = DL(m0);
                   10282:         td = TD(d);
                   10283:         mr0 = 0;
                   10284:         NEWNM(tnm);
                   10285:         if ( mod ) {
                   10286:             c = CM(m0);
                   10287:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   10288:                 ndl_add(DL(m),d0,DL(tnm));
                   10289:                 if ( ndl_reducible(DL(tnm),d) ) {
                   10290:                     NEXTNM(mr0,mr);
                   10291:                     c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
                   10292:                     ndl_copy(DL(tnm),DL(mr));
                   10293:                 }
                   10294:             }
                   10295:         } else {
1.6       noro     10296:             q = CZ(m0);
1.1       noro     10297:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   10298:                 ndl_add(DL(m),d0,DL(tnm));
                   10299:                 if ( ndl_reducible(DL(tnm),d) ) {
                   10300:                     NEXTNM(mr0,mr);
1.6       noro     10301:                     mulz(CZ(m),q,&CZ(mr));
1.1       noro     10302:                     ndl_copy(DL(tnm),DL(mr));
                   10303:                 }
                   10304:             }
                   10305:         }
                   10306:         if ( !mr0 )
                   10307:             return 0;
                   10308:         else {
                   10309:             NEXT(mr) = 0;
                   10310:             for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   10311:             MKND(NV(p),mr0,len,r);
                   10312:             SG(r) = SG(p) + TD(d0);
                   10313:             return r;
                   10314:         }
                   10315:     }
                   10316: }
                   10317:
                   10318: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   10319: {
                   10320:     int i,obpe,oadv,h,k,l;
                   10321:     static NM prev_nm_free_list;
                   10322:     EPOS oepos;
                   10323:
                   10324:     obpe = nd_bpe;
                   10325:     oadv = nmv_adv;
                   10326:     oepos = nd_epos;
                   10327:     if ( obpe < 2 ) nd_bpe = 2;
                   10328:     else if ( obpe < 3 ) nd_bpe = 3;
                   10329:     else if ( obpe < 4 ) nd_bpe = 4;
                   10330:     else if ( obpe < 5 ) nd_bpe = 5;
                   10331:     else if ( obpe < 6 ) nd_bpe = 6;
                   10332:     else if ( obpe < 8 ) nd_bpe = 8;
                   10333:     else if ( obpe < 10 ) nd_bpe = 10;
                   10334:     else if ( obpe < 16 ) nd_bpe = 16;
                   10335:     else if ( obpe < 32 ) nd_bpe = 32;
                   10336:     else error("nd_det_reconstruct : exponent too large");
                   10337:
                   10338:     nd_setup_parameters(nd_nvar,0);
                   10339:     prev_nm_free_list = _nm_free_list;
                   10340:     _nm_free_list = 0;
                   10341:     for ( k = j; k < n; k++ )
                   10342:         for (l = j; l < n; l++ )
                   10343:             ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   10344:     ndv_realloc(d,obpe,oadv,oepos);
                   10345:     prev_nm_free_list = 0;
                   10346: #if 0
                   10347:     GC_gcollect();
                   10348: #endif
                   10349: }
                   10350:
                   10351: /* returns a UINT array containing degree bounds */
                   10352:
                   10353: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   10354: {
                   10355:     UINT *d0,*d1,*d,*t,*r;
                   10356:     int k,l,i;
                   10357:
                   10358:     d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
                   10359:     for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
                   10360:     for ( k = j; k < n; k++ )
                   10361:         for ( l = j; l < n; l++ )
                   10362:             if ( dm[k][l] ) {
                   10363:                 d = ndv_compute_bound(dm[k][l]);
                   10364:                 for ( i = 0; i < nd_nvar; i++ )
                   10365:                     d0[i] = MAX(d0[i],d[i]);
                   10366:             }
                   10367:     return d0;
                   10368: }
                   10369:
                   10370: DL nd_separate_d(UINT *d,UINT *trans)
                   10371: {
                   10372:     int n,td,i,e,j;
                   10373:     DL a;
                   10374:
                   10375:     ndl_zero(trans);
                   10376:     td = 0;
                   10377:     for ( i = 0; i < nd_ntrans; i++ ) {
                   10378:         e = GET_EXP(d,i);
                   10379:         PUT_EXP(trans,i,e);
                   10380:         td += MUL_WEIGHT(e,i);
                   10381:     }
                   10382:     if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   10383:         /* homogenized */
                   10384:         i = nd_nvar-1;
                   10385:         e = GET_EXP(d,i);
                   10386:         PUT_EXP(trans,i,e);
                   10387:         td += MUL_WEIGHT(e,i);
                   10388:     }
                   10389:     TD(trans) = td;
                   10390:     if ( nd_blockmask) ndl_weight_mask(trans);
                   10391:     NEWDL(a,nd_nalg);
                   10392:     td = 0;
                   10393:     for ( i = 0; i < nd_nalg; i++ ) {
                   10394:         j = nd_ntrans+i;
                   10395:         e = GET_EXP(d,j);
                   10396:         a->d[i] = e;
                   10397:         td += e;
                   10398:     }
                   10399:     a->td = td;
                   10400:     return a;
                   10401: }
                   10402:
                   10403: int nd_monic(int mod,ND *p)
                   10404: {
                   10405:     UINT *trans,*t;
                   10406:     DL alg;
                   10407:     MP mp0,mp;
                   10408:     NM m,m0,m1,ma0,ma,mb,mr0,mr;
                   10409:     ND r;
                   10410:     DL dl;
                   10411:     DP nm;
                   10412:     NDV ndv;
                   10413:     DAlg inv,cd;
                   10414:     ND s,c;
                   10415:     Z l,mul;
                   10416:     Z ln;
                   10417:     int n,ntrans,i,e,td,is_lc,len;
                   10418:     NumberField nf;
                   10419:     struct oEGT eg0,eg1;
                   10420:
                   10421:     if ( !(nf = get_numberfield()) )
                   10422:         error("nd_monic : current_numberfield is not set");
                   10423:
                   10424:     /* Q coef -> DAlg coef */
                   10425:     NEWNM(ma0); ma = ma0;
                   10426:     m = BDY(*p);
                   10427:     is_lc = 1;
                   10428:     while ( 1 ) {
                   10429:         NEWMP(mp0); mp = mp0;
1.6       noro     10430:         mp->c = (Obj)CZ(m);
1.1       noro     10431:         mp->dl = nd_separate_d(DL(m),DL(ma));
                   10432:         NEWNM(mb);
                   10433:         for ( m = NEXT(m); m; m = NEXT(m) ) {
                   10434:             alg = nd_separate_d(DL(m),DL(mb));
                   10435:             if ( !ndl_equal(DL(ma),DL(mb)) )
                   10436:                 break;
1.6       noro     10437:             NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1       noro     10438:         }
                   10439:         NEXT(mp) = 0;
                   10440:         MKDP(nd_nalg,mp0,nm);
                   10441:         MKDAlg(nm,ONE,cd);
                   10442:         if ( is_lc == 1 ) {
                   10443:             /* if the lc is a rational number, we have nothing to do */
                   10444:             if ( !mp0->dl->td )
                   10445:                 return 1;
                   10446:
                   10447:             get_eg(&eg0);
                   10448:             invdalg(cd,&inv);
                   10449:             get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   10450:             /* check the validity of inv */
                   10451:             if ( mod && !remqi((Q)inv->dn,mod) )
                   10452:                 return 0;
                   10453:             CA(ma) = nf->one;
                   10454:             is_lc = 0;
                   10455:             ln = ONE;
                   10456:         } else {
                   10457:             muldalg(cd,inv,&CA(ma));
                   10458:             lcmz(ln,CA(ma)->dn,&ln);
                   10459:         }
                   10460:         if ( m ) {
                   10461:             NEXT(ma) = mb; ma = mb;
                   10462:         } else {
                   10463:             NEXT(ma) = 0;
                   10464:             break;
                   10465:         }
                   10466:     }
                   10467:     /* l = lcm(denoms) */
                   10468:     l = ln;
                   10469:     for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6       noro     10470:         divsz(l,CA(m)->dn,&mul);
1.1       noro     10471:         for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   10472:             NEXTNM(mr0,mr);
1.6       noro     10473:             mulz((Z)mp->c,mul,&CZ(mr));
1.1       noro     10474:             dl = mp->dl;
                   10475:             td = TD(DL(m));
                   10476:             ndl_copy(DL(m),DL(mr));
                   10477:             for ( i = 0; i < nd_nalg; i++ ) {
                   10478:                 e = dl->d[i];
                   10479:                 PUT_EXP(DL(mr),i+nd_ntrans,e);
                   10480:                 td += MUL_WEIGHT(e,i+nd_ntrans);
                   10481:             }
                   10482:             if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
                   10483:             TD(DL(mr)) = td;
                   10484:             if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   10485:         }
                   10486:     }
                   10487:     NEXT(mr) = 0;
                   10488:     for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   10489:     MKND(NV(*p),mr0,len,r);
                   10490:     /* XXX */
                   10491:     SG(r) = SG(*p);
                   10492:     nd_free(*p);
                   10493:     *p = r;
                   10494:     return 1;
                   10495: }
                   10496:
                   10497: NODE reverse_node(NODE n)
                   10498: {
                   10499:     NODE t,t1;
                   10500:
                   10501:     for ( t = 0; n; n = NEXT(n) ) {
                   10502:         MKNODE(t1,BDY(n),t); t = t1;
                   10503:     }
                   10504:     return t;
                   10505: }
                   10506:
                   10507: P ndc_div(int mod,union oNDC a,union oNDC b)
                   10508: {
                   10509:     union oNDC c;
                   10510:     int inv,t;
                   10511:
                   10512:     if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10      noro     10513:     else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1       noro     10514:     else if ( mod ) {
                   10515:         inv = invm(b.m,mod);
                   10516:         DMAR(a.m,inv,0,mod,t); c.m = t;
                   10517:     } else if ( nd_vc )
                   10518:        divsp(nd_vc,a.p,b.p,&c.p);
                   10519:     else
                   10520:        divsz(a.z,b.z,&c.z);
                   10521:     return ndctop(mod,c);
                   10522: }
                   10523:
                   10524: P ndctop(int mod,union oNDC c)
                   10525: {
                   10526:     Z q;
                   10527:     int e;
                   10528:     GFS gfs;
                   10529:
                   10530:     if ( mod == -1 ) {
                   10531:         e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
                   10532:     } else if ( mod == -2 ) {
1.10      noro     10533:        q = c.z; return (P)q;
1.1       noro     10534:     } else if ( mod > 0 ) {
1.6       noro     10535:         STOZ(c.m,q); return (P)q;
1.1       noro     10536:     } else
                   10537:         return (P)c.p;
                   10538: }
                   10539:
                   10540: /* [0,0,0,cont] = p -> p/cont */
                   10541:
                   10542: void finalize_tracelist(int i,P cont)
                   10543: {
                   10544:  LIST l;
                   10545:  NODE node;
                   10546:  Z iq;
                   10547:
                   10548:   if ( !UNIQ(cont) ) {
                   10549:     node = mknode(4,NULLP,NULLP,NULLP,cont);
                   10550:     MKLIST(l,node); MKNODE(node,l,nd_tracelist);
                   10551:     nd_tracelist = node;
                   10552:   }
1.6       noro     10553:   STOZ(i,iq);
1.1       noro     10554:   nd_tracelist = reverse_node(nd_tracelist);
                   10555:   MKLIST(l,nd_tracelist);
                   10556:   node = mknode(2,iq,l); MKLIST(l,node);
                   10557:   MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
                   10558:   nd_alltracelist = node; nd_tracelist = 0;
                   10559: }
                   10560:
                   10561: void conv_ilist(int demand,int trace,NODE g,int **indp)
                   10562: {
                   10563:     int n,i,j;
                   10564:   int *ind;
                   10565:   NODE t;
                   10566:
                   10567:     n = length(g);
                   10568:   ind = (int *)MALLOC(n*sizeof(int));
                   10569:   for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
                   10570:     j = (long)BDY(t); ind[i] = j;
                   10571:     BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
                   10572:   }
                   10573:   if ( indp ) *indp = ind;
                   10574: }
                   10575:
1.30      noro     10576: NODE conv_ilist_s(int demand,int trace,int **indp)
                   10577: {
                   10578:   int n,i,j;
                   10579:   int *ind;
                   10580:   NODE g0,g;
                   10581:
                   10582:   n = nd_psn;
                   10583:   ind = (int *)MALLOC(n*sizeof(int));
                   10584:   g0 = 0;
                   10585:   for ( i = 0; i < n; i++ ) {
                   10586:     ind[i] = i;
                   10587:     NEXTNODE(g0,g);
                   10588:     BDY(g) = (pointer)(demand?ndv_load(i):(trace?nd_ps_trace[i]:nd_ps[i]));
                   10589:   }
                   10590:   if ( g0 ) NEXT(g) = 0;
                   10591:   if ( indp ) *indp = ind;
                   10592:   return g0;
                   10593: }
                   10594:
1.46      noro     10595: void parse_nd_option(VL vl,NODE opt)
1.1       noro     10596: {
1.38      noro     10597:   NODE t,p,u;
1.1       noro     10598:   int i,s,n;
1.38      noro     10599:   char *key;
                   10600:   Obj value;
1.46      noro     10601:   VL oldvl;
1.1       noro     10602:
1.41      noro     10603:   nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_norb = 0; nd_gbblock = 0;
1.1       noro     10604:   nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
                   10605:   nd_splist = 0; nd_check_splist = 0;
1.38      noro     10606:   nd_sugarweight = 0; nd_f4red =0; nd_rank0 = 0;
                   10607:   nd_f4_td = 0; nd_sba_f4step = 2; nd_sba_pot = 0; nd_sba_largelcm = 0;
1.41      noro     10608:   nd_sba_dontsort = 0; nd_top = 0; nd_sba_redundant_check = 0;
1.46      noro     10609:   nd_sba_syz = 0; nd_sba_modord = 0; nd_sba_inputisgb = 0;
1.49      noro     10610:   nd_hpdata = 0;
1.39      noro     10611:
1.38      noro     10612:   for ( t = opt; t; t = NEXT(t) ) {
                   10613:     p = BDY((LIST)BDY(t));
                   10614:     key = BDY((STRING)BDY(p));
                   10615:     value = (Obj)BDY(NEXT(p));
                   10616:     if ( !strcmp(key,"gentrace") )
                   10617:       nd_gentrace = value?1:0;
                   10618:     else if ( !strcmp(key,"gensyz") )
                   10619:       nd_gensyz = value?1:0;
                   10620:     else if ( !strcmp(key,"nora") )
                   10621:       nd_nora = value?1:0;
1.41      noro     10622:     else if ( !strcmp(key,"norb") )
                   10623:       nd_norb = value?1:0;
1.38      noro     10624:     else if ( !strcmp(key,"gbblock") ) {
                   10625:       if ( value && OID(value) == O_LIST ) {
1.1       noro     10626:         u = BDY((LIST)value);
1.38      noro     10627:         nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.1       noro     10628:         for ( i = 0; u; u = NEXT(u) ) {
                   10629:           p = BDY((LIST)BDY(u));
1.6       noro     10630:           s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
                   10631:           nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1       noro     10632:         }
                   10633:         nd_gbblock[i] = -1;
1.38      noro     10634:       } else
                   10635:         nd_gbblock = 0;
1.1       noro     10636:     } else if ( !strcmp(key,"newelim") )
                   10637:             nd_newelim = value?1:0;
                   10638:     else if ( !strcmp(key,"intersect") )
                   10639:             nd_intersect = value?1:0;
1.17      noro     10640:     else if ( !strcmp(key,"syzgen") )
                   10641:             nd_intersect = ZTOS((Q)value);
1.1       noro     10642:     else if ( !strcmp(key,"lf") )
                   10643:             nd_lf = value?1:0;
                   10644:     else if ( !strcmp(key,"trace") ) {
1.38      noro     10645:       if ( value ) {
                   10646:         u = BDY((LIST)value);
                   10647:         nd_nzlist = BDY((LIST)ARG2(u));
                   10648:         nd_bpe = ZTOS((Q)ARG3(u));
                   10649:       }
1.1       noro     10650:     } else if ( !strcmp(key,"f4red") ) {
1.38      noro     10651:       nd_f4red = ZTOS((Q)value);
1.1       noro     10652:     } else if ( !strcmp(key,"rank0") ) {
1.38      noro     10653:       nd_rank0 = value?1:0;
1.1       noro     10654:     } else if ( !strcmp(key,"splist") ) {
1.38      noro     10655:       nd_splist = value?1:0;
1.1       noro     10656:     } else if ( !strcmp(key,"check_splist") ) {
                   10657:       nd_check_splist = BDY((LIST)value);
1.49      noro     10658:     } else if ( !strcmp(key,"hpdata") ) {
1.50      noro     10659:       if ( value )
                   10660:         nd_hpdata = BDY((LIST)value);
1.1       noro     10661:     } else if ( !strcmp(key,"sugarweight") ) {
                   10662:       u = BDY((LIST)value);
1.38      noro     10663:       n = length(u);
                   10664:       nd_sugarweight = MALLOC(n*sizeof(int));
1.1       noro     10665:       for ( i = 0; i < n; i++, u = NEXT(u) )
1.38      noro     10666:         nd_sugarweight[i] = ZTOS((Q)BDY(u));
                   10667:     } else if ( !strcmp(key,"f4_td") ) {
                   10668:       nd_f4_td = value?1:0;
                   10669:     } else if ( !strcmp(key,"sba_f4step") ) {
                   10670:       nd_sba_f4step = value?ZTOS((Q)value):0;
                   10671:     } else if ( !strcmp(key,"sba_pot") ) {
1.44      noro     10672:       nd_sba_pot = ZTOS((Q)value);
1.38      noro     10673:     } else if ( !strcmp(key,"sba_largelcm") ) {
                   10674:       nd_sba_largelcm = value?1:0;
1.39      noro     10675:     } else if ( !strcmp(key,"sba_dontsort") ) {
                   10676:       nd_sba_dontsort = value?1:0;
1.44      noro     10677:     } else if ( !strcmp(key,"sba_syz") ) {
                   10678:       nd_sba_syz = value?1:0;
1.46      noro     10679:     } else if ( !strcmp(key,"sba_modord") ) {
                   10680:       // value=[vlist,ordspec,weight]
                   10681:       u = BDY((LIST)value);
                   10682:       pltovl((LIST)ARG0(u),&oldvl);
                   10683:       nd_sba_modord = create_comp_sig_spec(vl,oldvl,(Obj)ARG1(u),argc(u)==3?ARG2(u):0);
                   10684:     } else if ( !strcmp(key,"sba_gbinput") ) {
                   10685:       nd_sba_inputisgb = value?1:0;
                   10686:       if ( nd_sba_inputisgb != 0 ) {
                   10687:         // value=[vlist,ordspec,weight]
                   10688:         u = BDY((LIST)value);
                   10689:         pltovl((LIST)ARG0(u),&oldvl);
                   10690:         nd_sba_modord = create_comp_sig_spec(vl,oldvl,(Obj)ARG1(u),argc(u)==3?ARG2(u):0);
                   10691:       }
1.41      noro     10692:     } else if ( !strcmp(key,"sba_redundant_check") ) {
                   10693:       nd_sba_redundant_check = value?1:0;
1.39      noro     10694:     } else if ( !strcmp(key,"top") ) {
                   10695:       nd_top = value?1:0;
1.1       noro     10696:     }
1.38      noro     10697:   }
1.44      noro     10698:   if ( nd_sba_syz ) nd_sba_dontsort = 1;
1.1       noro     10699: }
                   10700:
                   10701: ND mdptond(DP d);
                   10702: ND nd_mul_nm(int mod,NM m0,ND p);
                   10703: ND nd_mul_nm_lf(NM m0,ND p);
                   10704: ND *btog(NODE ti,ND **p,int nb,int mod);
                   10705: ND btog_one(NODE ti,ND *p,int nb,int mod);
                   10706: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
                   10707: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
                   10708:
                   10709: /* d:monomial */
                   10710: ND mdptond(DP d)
                   10711: {
                   10712:   NM m;
                   10713:   ND r;
                   10714:
                   10715:   if ( OID(d) == 1 )
                   10716:   r = ptond(CO,CO,(P)d);
                   10717:   else {
                   10718:     NEWNM(m);
                   10719:     dltondl(NV(d),BDY(d)->dl,DL(m));
1.6       noro     10720:     CZ(m) = (Z)BDY(d)->c;
1.1       noro     10721:     NEXT(m) = 0;
                   10722:     MKND(NV(d),m,1,r);
                   10723:   }
                   10724:     return r;
                   10725: }
                   10726:
                   10727: ND nd_mul_nm(int mod,NM m0,ND p)
                   10728: {
                   10729:   UINT *d0;
                   10730:   int c0,c1,c;
                   10731:   NM tm,mr,mr0;
                   10732:   ND r;
                   10733:
                   10734:   if ( !p ) return 0;
                   10735:   d0 = DL(m0);
                   10736:   c0 = CM(m0);
                   10737:   mr0 = 0;
                   10738:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   10739:     NEXTNM(mr0,mr);
                   10740:   c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
                   10741:   ndl_add(d0,DL(tm),DL(mr));
                   10742:   }
                   10743:   NEXT(mr) = 0;
                   10744:   MKND(NV(p),mr0,LEN(p),r);
                   10745:   return r;
                   10746: }
                   10747:
                   10748: ND nd_mul_nm_lf(NM m0,ND p)
                   10749: {
                   10750:   UINT *d0;
                   10751:   Z c0,c1,c;
                   10752:   NM tm,mr,mr0;
                   10753:   ND r;
                   10754:
                   10755:   if ( !p ) return 0;
                   10756:   d0 = DL(m0);
                   10757:   c0 = CZ(m0);
                   10758:   mr0 = 0;
                   10759:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   10760:     NEXTNM(mr0,mr);
                   10761:     c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
                   10762:     ndl_add(d0,DL(tm),DL(mr));
                   10763:   }
                   10764:   NEXT(mr) = 0;
                   10765:   MKND(NV(p),mr0,LEN(p),r);
                   10766:   return r;
                   10767: }
                   10768:
                   10769: ND *btog(NODE ti,ND **p,int nb,int mod)
                   10770: {
                   10771:   PGeoBucket *r;
                   10772:   int i,ci;
                   10773:   NODE t,s;
                   10774:   ND m,tp;
                   10775:   ND *pi,*rd;
                   10776:   P c;
                   10777:
                   10778:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   10779:   for ( i = 0; i < nb; i++ )
                   10780:     r[i] = create_pbucket();
                   10781:   for ( t = ti; t; t = NEXT(t) ) {
                   10782:   s = BDY((LIST)BDY(t));
                   10783:     if ( ARG0(s) ) {
                   10784:     m = mdptond((DP)ARG2(s));
1.6       noro     10785:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     10786:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   10787:       HCM(m) = ci;
1.6       noro     10788:       pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     10789:       for ( i = 0; i < nb; i++ ) {
                   10790:       tp = nd_mul_nm(mod,BDY(m),pi[i]);
                   10791:         add_pbucket(mod,r[i],tp);
                   10792:       }
                   10793:     }
                   10794:     ci = 1;
                   10795:     } else {
                   10796:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   10797:     ci = invm(ci,mod);
                   10798:   }
                   10799:   }
                   10800:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   10801:   for ( i = 0; i < nb; i++ )
                   10802:   rd[i] = normalize_pbucket(mod,r[i]);
                   10803:   if ( ci != 1 )
                   10804:     for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
                   10805:    return rd;
                   10806: }
                   10807:
                   10808: /* YYY */
                   10809: ND *btog_lf(NODE ti,ND **p,int nb)
                   10810: {
                   10811:   PGeoBucket *r;
                   10812:   int i;
                   10813:   NODE t,s;
                   10814:   ND m,tp;
                   10815:   ND *pi,*rd;
                   10816:   LM lm;
                   10817:   Z lf,c;
                   10818:
                   10819:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   10820:   for ( i = 0; i < nb; i++ )
                   10821:     r[i] = create_pbucket();
                   10822:   for ( t = ti; t; t = NEXT(t) ) {
                   10823:     s = BDY((LIST)BDY(t));
                   10824:     if ( ARG0(s) ) {
                   10825:       m = mdptond((DP)ARG2(s));
1.6       noro     10826:       simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1       noro     10827:       if ( lm ) {
                   10828:         lmtolf(lm,&lf); HCZ(m) = lf;
1.6       noro     10829:         pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     10830:         for ( i = 0; i < nb; i++ ) {
                   10831:           tp = nd_mul_nm_lf(BDY(m),pi[i]);
                   10832:           add_pbucket(-2,r[i],tp);
                   10833:         }
                   10834:       }
                   10835:       c = ONE;
                   10836:     } else {
                   10837:       simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
                   10838:     }
                   10839:   }
                   10840:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   10841:   for ( i = 0; i < nb; i++ )
                   10842:     rd[i] = normalize_pbucket(-2,r[i]);
                   10843:   for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
                   10844:   return rd;
                   10845: }
                   10846:
                   10847: ND btog_one(NODE ti,ND *p,int nb,int mod)
                   10848: {
                   10849:   PGeoBucket r;
                   10850:   int i,ci,j;
                   10851:   NODE t,s;
                   10852:   ND m,tp;
                   10853:   ND pi,rd;
                   10854:   P c;
                   10855:
                   10856:   r = create_pbucket();
                   10857:   for ( t = ti; t; t = NEXT(t) ) {
                   10858:   s = BDY((LIST)BDY(t));
                   10859:     if ( ARG0(s) ) {
                   10860:     m = mdptond((DP)ARG2(s));
1.6       noro     10861:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     10862:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   10863:       HCM(m) = ci;
1.6       noro     10864:       pi = p[j=ZTOS((Q)ARG1(s))];
1.1       noro     10865:     if ( !pi ) {
                   10866:       pi = nd_load_mod(j);
                   10867:       tp = nd_mul_nm(mod,BDY(m),pi);
                   10868:       nd_free(pi);
                   10869:         add_pbucket(mod,r,tp);
                   10870:     } else {
                   10871:       tp = nd_mul_nm(mod,BDY(m),pi);
                   10872:         add_pbucket(mod,r,tp);
                   10873:       }
                   10874:     }
                   10875:     ci = 1;
                   10876:     } else {
                   10877:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   10878:     ci = invm(ci,mod);
                   10879:   }
                   10880:   }
                   10881:   rd = normalize_pbucket(mod,r);
                   10882:   free_pbucket(r);
                   10883:   if ( ci != 1 ) nd_mul_c(mod,rd,ci);
                   10884:   return rd;
                   10885: }
                   10886:
                   10887: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
                   10888:
                   10889: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
                   10890: {
                   10891:   int i,j,n,m,nb,pi0,pi1,nvar;
                   10892:   VL fv,tv,vv;
                   10893:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   10894:   ND **p;
                   10895:   ND *c;
                   10896:   ND u;
                   10897:   P inv;
                   10898:   MAT mat;
                   10899:
                   10900:   if ( mod == -2 )
                   10901:     return nd_btog_lf(f,v,ord,tlist,rp);
                   10902:
                   10903:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     10904:   parse_nd_option(vv,current_option);
1.1       noro     10905:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   10906:   switch ( ord->id ) {
                   10907:     case 1:
                   10908:       if ( ord->nv != nvar )
                   10909:         error("nd_check : invalid order specification");
                   10910:       break;
                   10911:     default:
                   10912:       break;
                   10913:   }
                   10914:   nd_init_ord(ord);
                   10915: #if 0
1.6       noro     10916:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     10917: #else
                   10918:   nd_bpe = 32;
                   10919: #endif
                   10920:   nd_setup_parameters(nvar,0);
                   10921:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   10922:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   10923:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   10924:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   10925:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     10926:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     10927:   if ( j > i ) i = j;
                   10928:   }
                   10929:   n = i+1;
                   10930:   nb = length(BDY(f));
                   10931:   p = (ND **)MALLOC(n*sizeof(ND *));
                   10932:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   10933:     pi = BDY((LIST)BDY(t));
1.6       noro     10934:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     10935:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   10936:     ptomp(mod,(P)ARG2(pi),&inv);
                   10937:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   10938:     u = ptond(CO,vv,(P)ONE);
                   10939:     HCM(u) = ((MQ)inv)->cont;
                   10940:     c[pi1] = u;
                   10941:   }
                   10942:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   10943:     printf("%d ",i); fflush(stdout);
                   10944:     ti = BDY((LIST)BDY(t));
1.6       noro     10945:     p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10946:   }
                   10947:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   10948:     printf("%d ",i); fflush(stdout);
                   10949:     ti = BDY((LIST)BDY(t));
1.6       noro     10950:     p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10951:   }
                   10952:   m = length(ind);
                   10953:   MKMAT(mat,nb,m);
                   10954:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     10955:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     10956:       BDY(mat)[i][j] = ndtodp(mod,c[i]);
                   10957:   return mat;
                   10958: }
                   10959:
                   10960: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
                   10961: {
                   10962:   int i,j,n,m,nb,pi0,pi1,nvar;
                   10963:   VL fv,tv,vv;
                   10964:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   10965:   ND **p;
                   10966:   ND *c;
                   10967:   ND u;
                   10968:   MAT mat;
                   10969:   LM lm;
                   10970:   Z lf,inv;
                   10971:
                   10972:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     10973:   parse_nd_option(vv,current_option);
1.1       noro     10974:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   10975:   switch ( ord->id ) {
                   10976:     case 1:
                   10977:       if ( ord->nv != nvar )
                   10978:         error("nd_check : invalid order specification");
                   10979:       break;
                   10980:     default:
                   10981:       break;
                   10982:   }
                   10983:   nd_init_ord(ord);
                   10984: #if 0
1.6       noro     10985:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     10986: #else
                   10987:   nd_bpe = 32;
                   10988: #endif
                   10989:   nd_setup_parameters(nvar,0);
                   10990:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   10991:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   10992:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   10993:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   10994:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     10995:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     10996:   if ( j > i ) i = j;
                   10997:   }
                   10998:   n = i+1;
                   10999:   nb = length(BDY(f));
                   11000:   p = (ND **)MALLOC(n*sizeof(ND *));
                   11001:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   11002:     pi = BDY((LIST)BDY(t));
1.6       noro     11003:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     11004:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   11005:     simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
                   11006:     u = ptond(CO,vv,(P)ONE);
                   11007:     HCZ(u) = inv;
                   11008:     c[pi1] = u;
                   11009:   }
                   11010:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   11011:     printf("%d ",i); fflush(stdout);
                   11012:     ti = BDY((LIST)BDY(t));
1.6       noro     11013:     p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     11014:   }
                   11015:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   11016:     printf("%d ",i); fflush(stdout);
                   11017:     ti = BDY((LIST)BDY(t));
1.6       noro     11018:     p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     11019:   }
                   11020:   m = length(ind);
                   11021:   MKMAT(mat,nb,m);
                   11022:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     11023:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     11024:       BDY(mat)[i][j] = ndtodp(-2,c[i]);
                   11025:   return mat;
                   11026: }
                   11027:
                   11028: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
                   11029:   LIST tlist,int pos,MAT *rp)
                   11030: {
                   11031:   int i,j,n,m,nb,pi0,pi1,nvar;
                   11032:   VL fv,tv,vv;
                   11033:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   11034:   ND *p;
                   11035:   ND *c;
                   11036:   ND u;
                   11037:   P inv;
                   11038:   VECT vect;
                   11039:
                   11040:   if ( mod == -2 )
                   11041:     error("nd_btog_one : not implemented yet for a large finite field");
                   11042:
                   11043:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     11044:   parse_nd_option(vv,current_option);
1.1       noro     11045:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   11046:   switch ( ord->id ) {
                   11047:     case 1:
                   11048:       if ( ord->nv != nvar )
                   11049:         error("nd_check : invalid order specification");
                   11050:       break;
                   11051:     default:
                   11052:       break;
                   11053:   }
                   11054:   nd_init_ord(ord);
                   11055: #if 0
1.6       noro     11056:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     11057: #else
                   11058:   nd_bpe = 32;
                   11059: #endif
                   11060:   nd_setup_parameters(nvar,0);
                   11061:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   11062:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   11063:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   11064:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   11065:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     11066:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     11067:   if ( j > i ) i = j;
                   11068:   }
                   11069:   n = i+1;
                   11070:   nb = length(BDY(f));
                   11071:   p = (ND *)MALLOC(n*sizeof(ND *));
                   11072:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   11073:     pi = BDY((LIST)BDY(t));
1.6       noro     11074:   pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     11075:   if ( pi1 == pos ) {
                   11076:     ptomp(mod,(P)ARG2(pi),&inv);
                   11077:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   11078:     u = ptond(CO,vv,(P)ONE);
                   11079:     HCM(u) = ((MQ)inv)->cont;
                   11080:     p[pi0] = u;
                   11081:     }
                   11082:   }
                   11083:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   11084:   printf("%d ",i); fflush(stdout);
                   11085:     ti = BDY((LIST)BDY(t));
1.6       noro     11086:     p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     11087:     if ( Demand ) {
                   11088:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   11089:   }
                   11090:   }
                   11091:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   11092:   printf("%d ",i); fflush(stdout);
                   11093:     ti = BDY((LIST)BDY(t));
1.6       noro     11094:     p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     11095:     if ( Demand ) {
                   11096:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   11097:   }
                   11098:   }
                   11099:   m = length(ind);
                   11100:   MKVECT(vect,m);
                   11101:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6       noro     11102:   u = p[ZTOS((Q)BDY(t))];
1.1       noro     11103:   if ( !u ) {
1.6       noro     11104:     u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1       noro     11105:     BDY(vect)[j] = ndtodp(mod,u);
                   11106:     nd_free(u);
                   11107:   } else
                   11108:     BDY(vect)[j] = ndtodp(mod,u);
                   11109:   }
                   11110:   return vect;
                   11111: }
                   11112:
                   11113: void ndv_print_lf(NDV p)
                   11114: {
                   11115:     NMV m;
                   11116:     int i,len;
                   11117:
                   11118:     if ( !p ) printf("0\n");
                   11119:     else {
                   11120:         len = LEN(p);
                   11121:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   11122:             printf("+");
                   11123:             mpz_out_str(asir_out,10,BDY(CZ(m)));
                   11124:             printf("*");
                   11125:             ndl_print(DL(m));
                   11126:         }
                   11127:         printf("\n");
                   11128:     }
                   11129: }
                   11130:
                   11131: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
                   11132: {
                   11133:     VL tv,fv,vv,vc,av;
                   11134:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   11135:     int m,nocheck,nvar,mindex,e,max;
                   11136:     NDV c;
                   11137:     NMV a;
                   11138:     P p,zp;
                   11139:     Q dmy;
                   11140:     EPOS oepos;
                   11141:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   11142:     Alg alpha,dp;
                   11143:     P poly;
                   11144:     LIST f1,f2,zpl;
                   11145:     Obj obj;
                   11146:     NumberField nf;
                   11147:     struct order_spec *ord1;
                   11148:     struct oEGT eg_check,eg0,eg1;
                   11149:     NODE tr,tl1,tl2,tl3,tl4;
                   11150:     LIST l1,l2,l3,l4,l5;
                   11151:     int *perm;
                   11152:     int j,ret;
                   11153:     NODE retn;
                   11154:     Q jq,bpe;
                   11155:
                   11156:     nd_module = 0;
                   11157:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     11158:     parse_nd_option(vv,current_option);
1.1       noro     11159:     if ( nd_vc )
                   11160:       error("nd_f4_lf_trace : computation over a rational function field is not implemented");
                   11161:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   11162:     switch ( ord->id ) {
                   11163:         case 1:
                   11164:             if ( ord->nv != nvar )
                   11165:                 error("nd_f4_lf_trace : invalid order specification");
                   11166:             break;
                   11167:         default:
                   11168:             break;
                   11169:     }
                   11170:
                   11171:     nd_ntrans = nvar;
                   11172:     nd_nalg = 0;
                   11173:
                   11174:     nocheck = 0;
                   11175:     mindex = 0;
                   11176:
                   11177:     /* do not use on-demand load/save */
                   11178:     nd_demand = 0;
                   11179:     m = trace > 1 ? trace : get_lprime(mindex);
                   11180:     nd_init_ord(ord);
                   11181:     mrank = 0;
                   11182:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   11183:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   11184:             if ( nd_module ) {
                   11185:                 s = BDY((LIST)BDY(t));
                   11186:                 trank = length(s);
                   11187:                 mrank = MAX(mrank,trank);
                   11188:                 for ( ; s; s = NEXT(s) ) {
                   11189:                     e = getdeg(tv->v,(P)BDY(s));
                   11190:                     max = MAX(e,max);
                   11191:                 }
                   11192:             } else {
                   11193:                 e = getdeg(tv->v,(P)BDY(t));
                   11194:                 max = MAX(e,max);
                   11195:             }
                   11196:         }
                   11197:     nd_setup_parameters(nvar,max);
                   11198:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   11199:     ishomo = 1;
                   11200:     /* XXX */
                   11201:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   11202:         if ( nd_module ) {
                   11203:             c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
                   11204:         } else {
                   11205:             c = (pointer)ptondv(CO,vv,(P)BDY(t));
                   11206:         }
                   11207:         if ( ishomo )
                   11208:             ishomo = ishomo && ndv_ishomo(c);
                   11209:         if ( c ) {
                   11210:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   11211:             ndv_mod(-2,c);
                   11212:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   11213:         }
                   11214:     }
                   11215:     if ( in0 ) NEXT(in) = 0;
                   11216:     if ( fd0 ) NEXT(fd) = 0;
                   11217:     if ( !ishomo && homo ) {
                   11218:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   11219:             c = (NDV)BDY(t); len = LEN(c);
                   11220:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   11221:                 wmax = MAX(TD(DL(a)),wmax);
                   11222:         }
                   11223:         homogenize_order(ord,nvar,&ord1);
                   11224:         nd_init_ord(ord1);
                   11225:         nd_setup_parameters(nvar+1,wmax);
                   11226:         for ( t = fd0; t; t = NEXT(t) )
                   11227:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   11228:     }
                   11229:     if ( MaxDeg > 0 ) nocheck = 1;
1.24      noro     11230:     ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1       noro     11231:     if ( ret )
                   11232:       cand = nd_f4_lf_trace_main(m,&perm);
                   11233:     if ( !ret || !cand ) {
                   11234:        *rp = 0; return;
                   11235:     }
                   11236:     if ( !ishomo && homo ) {
                   11237:       /* dehomogenization */
                   11238:       for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   11239:       nd_init_ord(ord);
                   11240:       nd_setup_parameters(nvar,0);
                   11241:     }
                   11242:     cand = ndv_reducebase(cand,perm);
                   11243:     cand = ndv_reduceall(-2,cand);
                   11244:     cbpe = nd_bpe;
                   11245:     get_eg(&eg0);
                   11246:     if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   11247:       /* gbcheck : cand is a GB of Id(cand) ? */
                   11248:       retn = nd_f4(-2,0,0);
                   11249:     }
                   11250:   if ( !retn ) {
                   11251:       /* failure */
                   11252:       *rp = 0; return;
                   11253:     }
                   11254:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   11255:     if ( DP_Print )
1.5       noro     11256:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     11257:     /* dp->p */
                   11258:     nd_bpe = cbpe;
                   11259:     nd_setup_parameters(nd_nvar,0);
                   11260:     for ( r = cand; r; r = NEXT(r) ) {
                   11261:       if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
                   11262:       else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
                   11263:     }
                   11264:     MKLIST(*rp,cand);
                   11265: }
                   11266:
                   11267: NODE nd_f4_lf_trace_main(int m,int **indp)
                   11268: {
                   11269:     int i,nh,stat,index;
                   11270:     NODE r,rm,g;
                   11271:     ND_pairs d,l,l0,t;
                   11272:     ND spol,red;
                   11273:     NDV nf,redv,nfqv,nfv;
                   11274:     NM s0,s;
                   11275:     NODE rp0,srp0,nflist,nflist_lf;
                   11276:     int nsp,nred,col,rank,len,k,j,a;
                   11277:     UINT c;
                   11278:     UINT **spmat;
                   11279:     UINT *s0vect,*svect,*p,*v;
                   11280:     int *colstat;
                   11281:     IndArray *imat;
                   11282:     int *rhead;
                   11283:     int spcol,sprow;
                   11284:     int sugar;
                   11285:     PGeoBucket bucket;
                   11286:     struct oEGT eg0,eg1,eg_f4;
                   11287:
                   11288:     g = 0; d = 0;
                   11289:     for ( i = 0; i < nd_psn; i++ ) {
                   11290:         d = update_pairs(d,g,i,0);
                   11291:         g = update_base(g,i);
                   11292:     }
                   11293:     while ( d ) {
                   11294:         get_eg(&eg0);
                   11295:         l = nd_minsugarp(d,&d);
                   11296:         sugar = SG(l);
                   11297:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   11298:         bucket = create_pbucket();
                   11299:         stat = nd_sp_f4(m,0,l,bucket);
                   11300:         if ( !stat ) {
                   11301:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11302:             NEXT(t) = d; d = l;
                   11303:             d = nd_reconstruct(1,d);
                   11304:             continue;
                   11305:         }
                   11306:         if ( bucket->m < 0 ) continue;
                   11307:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   11308:         if ( !col ) {
                   11309:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11310:             NEXT(t) = d; d = l;
                   11311:             d = nd_reconstruct(1,d);
                   11312:             continue;
                   11313:         }
                   11314:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   11315:         if ( DP_Print )
1.5       noro     11316:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1       noro     11317:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   11318:         if ( !l0 ) continue;
                   11319:         l = l0;
                   11320:
                   11321:         /* over LF */
                   11322:         bucket = create_pbucket();
                   11323:         stat = nd_sp_f4(-2,1,l,bucket);
                   11324:         if ( !stat ) {
                   11325:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11326:             NEXT(t) = d; d = l;
                   11327:             d = nd_reconstruct(1,d);
                   11328:             continue;
                   11329:         }
                   11330:         if ( bucket->m < 0 ) continue;
                   11331:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   11332:         if ( !col ) {
                   11333:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11334:             NEXT(t) = d; d = l;
                   11335:             d = nd_reconstruct(1,d);
                   11336:             continue;
                   11337:         }
                   11338:         nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
                   11339:         /* adding new bases */
                   11340:         for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
                   11341:             nfv = (NDV)BDY(rm);
                   11342:             nfqv = (NDV)BDY(r);
                   11343:             if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
                   11344:             ndv_removecont(m,nfv);
                   11345:             ndv_removecont(-2,nfqv);
1.24      noro     11346:             nh = ndv_newps(-2,nfv,nfqv);
1.1       noro     11347:             d = update_pairs(d,g,nh,0);
                   11348:             g = update_base(g,nh);
                   11349:         }
                   11350:         if ( r || rm ) return 0;
                   11351:     }
                   11352:   conv_ilist(nd_demand,1,g,indp);
                   11353:     return g;
                   11354: }
                   11355:
1.7       noro     11356: #if SIZEOF_LONG==8
                   11357:
                   11358: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   11359: {
                   11360:     int j,k,len;
                   11361:     UINT *p;
                   11362:     UINT c;
                   11363:     NDV r;
                   11364:     NMV mr0,mr;
                   11365:
                   11366:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   11367:     if ( !len ) return 0;
                   11368:     else {
                   11369:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   11370: #if 0
                   11371:         ndv_alloc += nmv_adv*len;
                   11372: #endif
                   11373:         mr = mr0;
                   11374:         p = s0vect;
                   11375:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   11376:             if ( !rhead[j] ) {
                   11377:                 if ( (c = (UINT)vect[k++]) != 0 ) {
                   11378:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   11379:                 }
                   11380:             }
                   11381:         MKNDV(nd_nvar,mr0,len,r);
                   11382:         return r;
                   11383:     }
                   11384: }
                   11385:
1.28      noro     11386: NDV vect64_to_ndv_s(mp_limb_t *vect,int col,UINT *s0vect)
                   11387: {
                   11388:     int j,k,len;
                   11389:     UINT *p;
                   11390:     UINT c;
                   11391:     NDV r;
                   11392:     NMV mr0,mr;
                   11393:
                   11394:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   11395:     if ( !len ) return 0;
                   11396:     else {
                   11397:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   11398:         mr = mr0;
                   11399:         p = s0vect;
                   11400:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   11401:           if ( (c = (UINT)vect[k++]) != 0 ) {
                   11402:             ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   11403:           }
                   11404:         MKNDV(nd_nvar,mr0,len,r);
                   11405:         return r;
                   11406:     }
                   11407: }
                   11408:
1.7       noro     11409: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
                   11410: {
                   11411:     NM m;
1.11      noro     11412:     UINT *t,*s,*u;
                   11413:     int i,st,ed,md,prev,c;
1.7       noro     11414:
                   11415:     for ( i = 0; i < n; i++ ) r[i] = 0;
1.11      noro     11416:     prev = 0;
                   11417:     for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
                   11418:       t = DL(m);
                   11419:       st = prev;
                   11420:       ed = n;
                   11421:       while ( ed > st ) {
                   11422:         md = (st+ed)/2;
                   11423:         u = s0+md*nd_wpd;
                   11424:         c = DL_COMPARE(u,t);
                   11425:         if ( c == 0 ) break;
                   11426:         else if ( c > 0 ) st = md;
                   11427:         else ed = md;
                   11428:       }
                   11429:       r[md] = (mp_limb_t)CM(m);
                   11430:       prev = md;
1.7       noro     11431:     }
                   11432:     for ( i = 0; !r[i]; i++ );
                   11433:     return i;
                   11434: }
                   11435:
                   11436: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
                   11437:
1.28      noro     11438: 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     11439: {
                   11440:     int i,j,k,len,pos,prev;
                   11441:     mp_limb_t a,c,c1,c2;
                   11442:     IndArray ivect;
                   11443:     unsigned char *ivc;
                   11444:     unsigned short *ivs;
                   11445:     unsigned int *ivi;
                   11446:     NDV redv;
                   11447:     NMV mr;
                   11448:     NODE rp;
                   11449:     int maxrs;
                   11450:
                   11451:     for ( i = 0; i < col; i++ ) cvect[i] = 0;
                   11452:     maxrs = 0;
                   11453:     for ( i = 0; i < nred; i++ ) {
                   11454:         ivect = imat[i];
                   11455:         k = ivect->head;
                   11456:         a = svect[k]; c = cvect[k];
                   11457:         MOD128(a,c,m);
                   11458:         svect[k] = a; cvect[k] = 0;
1.28      noro     11459:         if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.11      noro     11460:             Nf4_red++;
1.7       noro     11461:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   11462:             c = m-c; redv = nd_ps[rp0[i]->index];
                   11463:             len = LEN(redv); mr = BDY(redv);
                   11464:             svect[k] = 0; prev = k;
                   11465:             switch ( ivect->width ) {
                   11466:                 case 1:
                   11467:                     ivc = ivect->index.c;
                   11468:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   11469:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12      noro     11470:                         c2 = svect[pos]+c1*c;
                   11471:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   11472:                         svect[pos] = c2;
1.7       noro     11473:                     }
                   11474:                     break;
                   11475:                 case 2:
                   11476:                     ivs = ivect->index.s;
                   11477:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   11478:                         pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12      noro     11479:                         c2 = svect[pos]+c1*c;
                   11480:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   11481:                         svect[pos] = c2;
1.7       noro     11482:                     }
                   11483:                     break;
                   11484:                 case 4:
                   11485:                     ivi = ivect->index.i;
                   11486:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   11487:                         pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12      noro     11488:                         c2 = svect[pos]+c1*c;
                   11489:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   11490:                         svect[pos] = c2;
1.7       noro     11491:                     }
                   11492:                     break;
                   11493:             }
                   11494:         }
                   11495:     }
                   11496:     for ( i = 0; i < col; i++ ) {
                   11497:       a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
                   11498:     }
                   11499:     return maxrs;
                   11500: }
                   11501:
                   11502: /* for Fp, 2^15=<p<2^29 */
                   11503:
                   11504: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   11505:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   11506: {
                   11507:     int spcol,sprow,a;
                   11508:     int i,j,k,l,rank;
                   11509:     NODE r0,r;
                   11510:     ND_pairs sp;
                   11511:     ND spol;
                   11512:     mp_limb_t **spmat;
                   11513:     mp_limb_t *svect,*cvect;
                   11514:     mp_limb_t *v;
                   11515:     int *colstat;
                   11516:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   11517:     int maxrs;
                   11518:     int *spsugar;
                   11519:     ND_pairs *spactive;
                   11520:
                   11521:     spcol = col-nred;
                   11522:     get_eg(&eg0);
                   11523:     /* elimination (1st step) */
                   11524:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   11525:     svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11526:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11527:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   11528:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   11529:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   11530:         nd_sp(m,0,sp,&spol);
                   11531:         if ( !spol ) continue;
                   11532:         nd_to_vect64(m,s0vect,col,spol,svect);
1.28      noro     11533:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,0);
1.7       noro     11534:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   11535:         if ( i < col ) {
                   11536:             spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
                   11537:             for ( j = k = 0; j < col; j++ )
                   11538:                 if ( !rhead[j] ) v[k++] = (UINT)svect[j];
                   11539:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   11540:             if ( nz )
                   11541:             spactive[sprow] = sp;
                   11542:             sprow++;
                   11543:         }
                   11544:         nd_free(spol);
                   11545:     }
1.12      noro     11546:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7       noro     11547:     if ( DP_Print ) {
                   11548:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   11549:         fflush(asir_out);
                   11550:     }
                   11551:     /* free index arrays */
                   11552:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   11553:
                   11554:     /* elimination (2nd step) */
                   11555:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   11556:     rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   11557:     r0 = 0;
                   11558:     for ( i = 0; i < rank; i++ ) {
                   11559:         NEXTNODE(r0,r); BDY(r) =
                   11560:           (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   11561:         SG((NDV)BDY(r)) = spsugar[i];
                   11562:         GCFREE(spmat[i]);
                   11563:     }
                   11564:     if ( r0 ) NEXT(r) = 0;
                   11565:
                   11566:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12      noro     11567:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7       noro     11568:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   11569:     if ( DP_Print ) {
                   11570:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   11571:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   11572:             nsp,nred,sprow,spcol,rank);
                   11573:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   11574:     }
                   11575:     if ( nz ) {
                   11576:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   11577:         if ( rank > 0 ) {
                   11578:             NEXT(spactive[rank-1]) = 0;
                   11579:             *nz = spactive[0];
                   11580:         } else
                   11581:             *nz = 0;
                   11582:     }
                   11583:     return r0;
                   11584: }
                   11585:
                   11586: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   11587: {
                   11588:   int i,j,k,l,rank,s;
                   11589:   mp_limb_t inv;
                   11590:   mp_limb_t a;
                   11591:   UINT c;
                   11592:   mp_limb_t *t,*pivot,*pk;
                   11593:   UINT *ck;
                   11594:   UINT **cmat;
                   11595:   UINT *ct;
                   11596:   ND_pairs pair;
                   11597:
                   11598:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   11599:   for ( i = 0; i < row; i++ ) {
                   11600:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   11601:     bzero(cmat[i],col*sizeof(UINT));
                   11602:   }
                   11603:
                   11604:   for ( rank = 0, j = 0; j < col; j++ ) {
                   11605:     for ( i = rank; i < row; i++ ) {
                   11606:       a = mat[i][j]; c = cmat[i][j];
                   11607:       MOD128(a,c,md);
                   11608:       mat[i][j] = a; cmat[i][j] = 0;
                   11609:     }
                   11610:     for ( i = rank; i < row; i++ )
                   11611:       if ( mat[i][j] )
                   11612:         break;
                   11613:     if ( i == row ) {
                   11614:       colstat[j] = 0;
                   11615:       continue;
                   11616:     } else
                   11617:       colstat[j] = 1;
                   11618:     if ( i != rank ) {
                   11619:       t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   11620:       ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
                   11621:       s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   11622:       if ( spactive ) {
                   11623:         pair = spactive[i]; spactive[i] = spactive[rank];
                   11624:         spactive[rank] = pair;
                   11625:       }
                   11626:     }
                   11627:     /* column j is normalized */
                   11628:     s = sugar[rank];
                   11629:     inv = invm((UINT)mat[rank][j],md);
                   11630:     /* normalize pivot row */
                   11631:     for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
                   11632:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   11633:     }
                   11634:     for ( i = rank+1; i < row; i++ ) {
                   11635:       if ( (a = mat[i][j]) != 0 ) {
                   11636:         sugar[i] = MAX(sugar[i],s);
                   11637:         red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11      noro     11638:         Nf4_red++;
1.7       noro     11639:       }
                   11640:     }
                   11641:     rank++;
                   11642:   }
                   11643:   for ( j = col-1, l = rank-1; j >= 0; j-- )
                   11644:     if ( colstat[j] ) {
                   11645:       for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
                   11646:         a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
                   11647:       }
                   11648:       s = sugar[l];
                   11649:       for ( i = 0; i < l; i++ ) {
                   11650:         a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
                   11651:         if ( a ) {
                   11652:           sugar[i] = MAX(sugar[i],s);
                   11653:           red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11      noro     11654:           Nf4_red++;
1.7       noro     11655:         }
                   11656:       }
                   11657:       l--;
                   11658:     }
                   11659:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   11660:   GCFREE(cmat);
                   11661:   return rank;
                   11662: }
                   11663:
1.28      noro     11664: 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)
                   11665: {
                   11666:   int i,j,k,l,rank,s,imin;
                   11667:   mp_limb_t inv;
                   11668:   mp_limb_t a;
                   11669:   UINT c;
                   11670:   mp_limb_t *t,*pivot,*pk;
                   11671:   UINT *ck;
                   11672:   UINT **cmat;
                   11673:   UINT *ct;
                   11674:   ND_pairs pair;
                   11675:   SIG sg;
1.31      noro     11676:   int *used;
1.28      noro     11677:
1.31      noro     11678:   used = (int *)MALLOC(row*sizeof(int));
1.28      noro     11679:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   11680:   for ( i = 0; i < row; i++ ) {
                   11681:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   11682:     bzero(cmat[i],col*sizeof(UINT));
                   11683:   }
                   11684:
1.31      noro     11685:   for ( j = 0; j < col; j++ ) {
                   11686:     for ( i = 0; i < row; i++ ) {
1.28      noro     11687:       a = mat[i][j]; c = cmat[i][j];
                   11688:       MOD128(a,c,md);
                   11689:       mat[i][j] = a; cmat[i][j] = 0;
                   11690:     }
1.31      noro     11691:     for ( i = 0; i < row; i++ )
                   11692:       if ( !used[i] && mat[i][j] ) break;
                   11693:     if ( i == row ) {
1.28      noro     11694:       colstat[j] = 0;
                   11695:       continue;
1.31      noro     11696:     } else {
1.28      noro     11697:       colstat[j] = 1;
1.31      noro     11698:       used[i] = 1;
1.28      noro     11699:     }
                   11700:     /* column j is normalized */
1.31      noro     11701:     s = sugar[i];
                   11702:     inv = invm((UINT)mat[i][j],md);
1.28      noro     11703:     /* normalize pivot row */
1.31      noro     11704:     for ( k = j, pk = mat[i]+j, ck = cmat[i]+j; k < col; k++, pk++, ck++ ) {
1.28      noro     11705:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   11706:     }
1.31      noro     11707:     for ( k = i+1; k < row; k++ ) {
                   11708:       if ( (a = mat[k][j]) != 0 ) {
                   11709:         sugar[k] = MAX(sugar[k],s);
                   11710:         red_by_vect64(md,mat[k]+j,cmat[k]+j,mat[i]+j,(int)(md-a),col-j);
1.28      noro     11711:         Nf4_red++;
                   11712:       }
                   11713:     }
                   11714:   }
1.31      noro     11715:   rank = 0;
                   11716:   for ( i = 0; i < row; i++ ) {
                   11717:     for ( j = 0; j < col; j++ )
                   11718:       if ( mat[i][j] ) break;
                   11719:     if ( j == col ) sugar[i] = -1;
                   11720:     else rank++;
                   11721:   }
1.28      noro     11722:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   11723:   GCFREE(cmat);
                   11724:   return rank;
                   11725: }
                   11726:
                   11727: NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   11728:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
                   11729: {
                   11730:     int spcol,sprow,a;
                   11731:     int i,j,k,l,rank;
                   11732:     NODE r0,r;
                   11733:     ND_pairs sp;
                   11734:     ND spol;
                   11735:     mp_limb_t **spmat;
                   11736:     mp_limb_t *svect,*cvect;
                   11737:     mp_limb_t *v;
                   11738:     int *colstat;
                   11739:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   11740:     int maxrs;
                   11741:     int *spsugar;
                   11742:     ND_pairs *spactive;
                   11743:     SIG *spsig;
                   11744:
                   11745:     get_eg(&eg0);
                   11746:     /* elimination (1st step) */
                   11747:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   11748:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11749:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   11750:     spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
                   11751:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   11752:         nd_sp(m,0,sp,&spol);
                   11753:         if ( !spol ) {
1.29      noro     11754:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     11755:           continue;
                   11756:         }
                   11757:         svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11758:         nd_to_vect64(m,s0vect,col,spol,svect);
                   11759:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,spol->sig);
                   11760:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   11761:         if ( i < col ) {
                   11762:             spmat[sprow] = svect;
                   11763:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   11764:             spsig[sprow] = sp->sig;
                   11765:             sprow++;
                   11766:         } else {
1.29      noro     11767:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     11768:         }
                   11769:         nd_free(spol);
                   11770:     }
                   11771:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
                   11772:     if ( DP_Print ) {
                   11773:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   11774:         fflush(asir_out);
                   11775:     }
                   11776:     /* free index arrays */
                   11777:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   11778:
                   11779:     /* elimination (2nd step) */
                   11780:     colstat = (int *)MALLOC(col*sizeof(int));
                   11781:     rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
                   11782:     r0 = 0;
1.31      noro     11783:     for ( i = 0; i < sprow; i++ ) {
                   11784:         if ( spsugar[i] >= 0 ) {
                   11785:           NEXTNODE(r0,r);
                   11786:           BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
                   11787:           SG((NDV)BDY(r)) = spsugar[i];
                   11788:           ((NDV)BDY(r))->sig = spsig[i];
                   11789:         } else
                   11790:           syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
1.28      noro     11791:         GCFREE(spmat[i]);
                   11792:     }
                   11793:     if ( r0 ) NEXT(r) = 0;
                   11794:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
                   11795:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   11796:     if ( DP_Print ) {
                   11797:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   11798:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   11799:             nsp,nred,sprow,col,rank);
                   11800:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   11801:     }
                   11802:     return r0;
                   11803: }
1.40      noro     11804: #endif
1.28      noro     11805:
                   11806: NODE nd_f4_red_s(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,NODE *syzlistp)
                   11807: {
                   11808:   IndArray *imat;
                   11809:   int nsp,nred,i,start;
                   11810:   int *rhead;
                   11811:   NODE r0,rp;
                   11812:   ND_pairs sp;
                   11813:   NM_ind_pair *rvect;
                   11814:   UINT *s;
                   11815:   int *s0hash;
                   11816:   struct oEGT eg0,eg1,eg_conv;
                   11817:
                   11818:   for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   11819:   nred = length(rp0);
                   11820:   imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   11821:   rhead = (int *)MALLOC(col*sizeof(int));
                   11822:   for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   11823:
                   11824:   /* construction of index arrays */
                   11825:   get_eg(&eg0);
                   11826:   if ( DP_Print ) {
                   11827:     fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   11828:     fflush(asir_out);
                   11829:   }
                   11830:   rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   11831:   for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   11832:     rvect[i] = (NM_ind_pair)BDY(rp);
                   11833:     imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
                   11834:     rhead[imat[i]->head] = 1;
                   11835:     start = imat[i]->head;
                   11836:   }
                   11837:   get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
                   11838:   if ( DP_Print ) {
                   11839:     fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   11840:     fflush(asir_out);
                   11841:   }
                   11842:   if ( m > 0 )
1.40      noro     11843: #if SIZEOF_LONG==8
1.28      noro     11844:     r0 = nd_f4_red_mod64_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
1.40      noro     11845: #else
                   11846:     r0 = nd_f4_red_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
                   11847: #endif
1.28      noro     11848:   else
                   11849: //    r0 = nd_f4_red_q_main_s(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   11850:     error("nd_f4_red_q_main_s : not implemented yet");
                   11851:   return r0;
                   11852: }
                   11853:
                   11854: INLINE int ndl_find_reducer_minsig(UINT *dg)
                   11855: {
                   11856:   RHist r;
                   11857:   int i,singular,ret,d,k,imin;
                   11858:   SIG t;
                   11859:   static int wpd,nvar;
                   11860:   static SIG quo,quomin;
                   11861:   static UINT *tmp;
                   11862:
                   11863:   if ( !quo || nvar != nd_nvar ) { NEWSIG(quo); NEWSIG(quomin); }
                   11864:   if ( wpd != nd_wpd ) {
                   11865:     wpd = nd_wpd;
                   11866:     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
                   11867:   }
                   11868: #if 0
                   11869:   d = ndl_hash_value(dg);
                   11870:   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   11871:     if ( ndl_equal(dg,DL(r)) ) {
                   11872:       return r->index;
                   11873:     }
                   11874:   }
                   11875: #endif
                   11876:   imin = -1;
                   11877:   for ( i = 0; i < nd_psn; i++ ) {
                   11878:     r = nd_psh[i];
                   11879:     if ( ndl_reducible(dg,DL(r)) ) {
                   11880:       ndl_sub(dg,DL(r),tmp);
                   11881:       _ndltodl(tmp,DL(quo));
                   11882:       _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
                   11883:       quo->pos = nd_psh[i]->sig->pos;
                   11884:       if ( imin < 0 || comp_sig(quomin,quo) > 0 ) {
                   11885:         t = quo; quo = quomin; quomin = t;
                   11886:         imin = i;
                   11887:       }
                   11888:     }
                   11889:   }
                   11890:   if ( imin == -1 ) return nd_psn;
                   11891:   else {
                   11892: #if 0
                   11893:     nd_append_red(dg,i);
                   11894: #endif
                   11895:     return imin;
                   11896:   }
                   11897: }
                   11898:
                   11899: int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   11900: {
                   11901:   NODE rp0,rp;
                   11902:   NM mul,head,s0,s;
                   11903:   int index,col,i,sugar;
                   11904:   RHist h;
                   11905:   UINT *s0v,*p;
                   11906:   NM_ind_pair pair;
                   11907:   ND red;
                   11908:   NDV *ps;
                   11909:   SIG sig;
                   11910:
                   11911:   s0 = 0; rp0 = 0; col = 0;
                   11912:   if ( nd_demand )
                   11913:     ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   11914:   else
                   11915:     ps = trace?nd_ps_trace:nd_ps;
                   11916:   while ( 1 ) {
                   11917:     head = remove_head_pbucket_symbolic(bucket);
                   11918:     if ( !head ) break;
                   11919:     if ( !s0 ) s0 = head;
                   11920:     else NEXT(s) = head;
                   11921:     s = head;
                   11922:     index = ndl_find_reducer_minsig(DL(head));
                   11923:     if ( index >= 0 && index < nd_psn ) {
                   11924:       h = nd_psh[index];
                   11925:       NEWNM(mul);
                   11926:       ndl_sub(DL(head),DL(h),DL(mul));
                   11927:       if ( ndl_check_bound2(index,DL(mul)) )
                   11928:         return 0;
                   11929:       sugar = TD(DL(mul))+SG(ps[index]);
                   11930:       NEWSIG(sig);
                   11931:       _ndltodl(DL(mul),DL(sig));
                   11932:       _addtodl(nd_nvar,DL(nd_psh[index]->sig),DL(sig));
                   11933:       sig->pos = nd_psh[index]->sig->pos;
                   11934:       MKNM_ind_pair(pair,mul,index,sugar,sig);
                   11935:       red = ndv_mul_nm_symbolic(mul,ps[index]);
                   11936:       add_pbucket_symbolic(bucket,nd_remove_head(red));
                   11937:       NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   11938:     }
                   11939:     col++;
                   11940:   }
                   11941:   if ( rp0 ) NEXT(rp) = 0;
                   11942:   NEXT(s) = 0;
                   11943:   s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   11944:   for ( i = 0, p = s0v, s = s0; i < col;
                   11945:     i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   11946:   *s0vect = s0v;
                   11947:   *r = rp0;
                   11948:
                   11949:   return col;
                   11950: }
                   11951:
                   11952: NODE nd_sba_f4(int m,int **indp)
                   11953: {
1.34      noro     11954:   int i,nh,stat,index,f4red,f4step;
1.33      noro     11955:   int col,rank,len,k,j,a,sugar,nbase,psugar,ms;
                   11956:   NODE r,g,rp0,nflist;
1.41      noro     11957:   ND_pairs d,l,t,l1;
1.33      noro     11958:   ND h,nf;
                   11959:   NDV nfv;
                   11960:   union oNDC hc;
                   11961:   UINT *s0vect;
1.28      noro     11962:   UINT c;
                   11963:   PGeoBucket bucket;
1.33      noro     11964:   NODE *syzlist;
                   11965:   SIG sig;
1.28      noro     11966:   struct oEGT eg0,eg1,eg_f4;
1.33      noro     11967:   struct oEGT eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.28      noro     11968:
                   11969:   Nf4_red=0;
1.30      noro     11970:   d = 0;
1.29      noro     11971:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.28      noro     11972:   for ( i = 0; i < nd_psn; i++ ) {
1.30      noro     11973:     d = update_pairs_s(d,i,syzlist);
1.28      noro     11974:   }
1.30      noro     11975:   nd_nbase = nd_psn;
1.28      noro     11976:   f4red = 1;
1.33      noro     11977:   psugar = 0;
1.34      noro     11978:   f4step = 0;
1.28      noro     11979:   while ( d ) {
1.33      noro     11980:     for ( t = d, ms = SG(d); t; t = NEXT(t) )
                   11981:       if ( SG(t) < ms ) ms = SG(t);
1.38      noro     11982:     if ( ms == psugar && f4step >= nd_sba_f4step ) {
1.33      noro     11983: again:
                   11984:       l = d; d = d->next;
1.41      noro     11985: #if 0
1.33      noro     11986:       if ( small_lcm(l) ) {
                   11987:         if ( DP_Print ) fprintf(asir_out,"M");
                   11988:         continue;
                   11989:       }
                   11990:       sig = l->sig;
                   11991:       stat = nd_sp(m,0,l,&h);
1.41      noro     11992: #else
                   11993:       l1 = find_smallest_lcm(l);
                   11994:       if ( l1 == 0 ) {
                   11995:         if ( DP_Print ) fprintf(asir_out,"M");
                   11996:         continue;
                   11997:       }
                   11998:       sig = l1->sig;
                   11999:       stat = nd_sp(m,0,l1,&h);
                   12000: #endif
1.33      noro     12001:       if ( !stat ) {
                   12002:         NEXT(l) = d; d = l;
                   12003:         d = nd_reconstruct(0,d);
                   12004:         goto again;
                   12005:       }
                   12006:   get_eg(&eg1);
                   12007:   #if USE_GEOBUCKET
1.39      noro     12008:       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     12009:   #else
1.39      noro     12010:       stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.33      noro     12011:   #endif
                   12012:   get_eg(&eg2);
                   12013:       if ( !stat ) {
                   12014:         NEXT(l) = d; d = l;
                   12015:         d = nd_reconstruct(0,d);
                   12016:         goto again;
                   12017:       } else if ( stat == -1 ) {
                   12018:         if ( DP_Print ) { printf("S"); fflush(stdout); }
                   12019:         FREENDP(l);
                   12020:       } else if ( nf ) {
                   12021:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   12022:         add_eg(&eg_nf,&eg1,&eg2);
                   12023:         hc = HCU(nf);
                   12024:         nd_removecont(m,nf);
                   12025:         nfv = ndtondv(m,nf); nd_free(nf);
                   12026:         nh = ndv_newps(m,nfv,0);
                   12027:
1.30      noro     12028:         d = update_pairs_s(d,nh,syzlist);
                   12029:         nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.33      noro     12030:         FREENDP(l);
1.28      noro     12031:       } else {
1.33      noro     12032:         add_eg(&eg_nfzero,&eg1,&eg2);
                   12033:        // syzygy
                   12034:   get_eg(&eg1);
                   12035:         d = remove_spair_s(d,sig);
                   12036:   get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29      noro     12037:         syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.33      noro     12038:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   12039:         FREENDP(l);
                   12040:       }
                   12041:     } else {
1.34      noro     12042:       if ( ms != psugar ) f4step = 1;
                   12043:       else f4step++;
1.33      noro     12044: again2:
                   12045:       psugar = ms;
                   12046:       l = nd_minsugarp_s(d,&d);
                   12047:       sugar = nd_sugarweight?d->sugar2:SG(d);
                   12048:       bucket = create_pbucket();
                   12049:       stat = nd_sp_f4(m,0,l,bucket);
                   12050:       if ( !stat ) {
                   12051:         for ( t = l; NEXT(t); t = NEXT(t) );
                   12052:         NEXT(t) = d; d = l;
                   12053:         d = nd_reconstruct(0,d);
                   12054:         goto again2;
                   12055:       }
                   12056:       if ( bucket->m < 0 ) continue;
                   12057:       col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
                   12058:       if ( !col ) {
1.37      noro     12059:         for ( t = l; NEXT(t); t = NEXT(t) )
                   12060:           ;
                   12061:         NEXT(t) = d; d = l;
1.33      noro     12062:         d = nd_reconstruct(0,d);
                   12063:         goto again2;
                   12064:       }
1.34      noro     12065:       if ( DP_Print ) fprintf(asir_out,"\nsugar=%d,",psugar);
1.33      noro     12066:       nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
                   12067:       /* adding new bases */
                   12068:       for ( r = nflist; r; r = NEXT(r) ) {
                   12069:         nfv = (NDV)BDY(r);
1.38      noro     12070:         if ( nd_f4_td ) SG(nfv) = nd_tdeg(nfv);
1.33      noro     12071:         ndv_removecont(m,nfv);
                   12072:         nh = ndv_newps(m,nfv,0);
                   12073:         d = update_pairs_s(d,nh,syzlist);
                   12074:         nd_sba_pos[nfv->sig->pos] = append_one(nd_sba_pos[nfv->sig->pos],nh);
                   12075:       }
                   12076:       for ( i = 0; i < nd_nbase; i++ )
                   12077:         for ( r = syzlist[i]; r; r = NEXT(r) )
                   12078:             d = remove_spair_s(d,(SIG)BDY(r));
                   12079:       d = remove_large_lcm(d);
                   12080:       if ( DP_Print ) {
1.34      noro     12081:         fprintf(asir_out,"f4red=%d,gblen=%d",f4red,nd_psn); fflush(asir_out);
1.33      noro     12082:       }
                   12083:       f4red++;
1.28      noro     12084:     }
                   12085:   }
                   12086:   if ( DP_Print ) {
1.34      noro     12087:     fprintf(asir_out,"\nnumber of red=%d,",Nf4_red);
1.28      noro     12088:   }
1.30      noro     12089:   g = conv_ilist_s(nd_demand,0,indp);
1.28      noro     12090:   return g;
                   12091: }

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