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

1.50    ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.49 2021/02/28 02:33:16 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.29      noro     5057:   struct oEGT eg0,eg1,egconv;
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;
                   5143:   x = ndv_reducebase(x,perm);
                   5144:   x = ndv_reduceall(m,x);
                   5145:   nd_setup_parameters(nd_nvar,0);
1.29      noro     5146:   get_eg(&eg0);
1.24      noro     5147:   for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   5148:     NEXTNODE(r0,r);
                   5149:     if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
1.34      noro     5150:     else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.24      noro     5151:   }
                   5152:   if ( r0 ) NEXT(r) = 0;
1.44      noro     5153:   if ( nd_sba_syz ) {
                   5154:     LIST gb,hsyz;
                   5155:     NODE nd;
                   5156:
                   5157:     MKLIST(gb,r0);
                   5158:     MKLIST(hsyz,syz);
                   5159:     nd = mknode(2,gb,hsyz);
                   5160:     MKLIST(*rp,nd);
                   5161:   } else
                   5162:     MKLIST(*rp,r0);
1.29      noro     5163:   get_eg(&eg1); init_eg(&egconv); add_eg(&egconv,&eg0,&eg1);
                   5164:   print_eg("conv",&egconv); fprintf(asir_out,"\n");
1.24      noro     5165: }
                   5166:
1.1       noro     5167: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
                   5168: {
                   5169:     VL tv,fv,vv,vc,av;
                   5170:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   5171:     int e,max,nvar,i;
                   5172:     NDV b;
                   5173:     int ishomo,nalg;
                   5174:     Alg alpha,dp;
                   5175:     P p,zp;
                   5176:     Q dmy;
                   5177:     LIST f1,f2;
                   5178:     Obj obj;
                   5179:     NumberField nf;
                   5180:     struct order_spec *ord1;
                   5181:     int *perm;
                   5182:
                   5183:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     5184:     parse_nd_option(vv,current_option);
1.1       noro     5185:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5186:     switch ( ord->id ) {
                   5187:         case 1:
                   5188:             if ( ord->nv != nvar )
                   5189:                 error("nd_check : invalid order specification");
                   5190:             break;
                   5191:         default:
                   5192:             break;
                   5193:     }
                   5194:     nd_nalg = 0;
                   5195:     av = 0;
                   5196:     if ( !m ) {
                   5197:         get_algtree((Obj)f,&av);
                   5198:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   5199:         nd_ntrans = nvar;
                   5200:         nd_nalg = nalg;
                   5201:         /* #i -> t#i */
                   5202:         if ( nalg ) {
                   5203:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   5204:             ord = ord1;
                   5205:             f = f1;
                   5206:         }
                   5207:         nvar += nalg;
                   5208:     }
                   5209:     nd_init_ord(ord);
                   5210:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   5211:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   5212:             e = getdeg(tv->v,(P)BDY(t));
                   5213:             max = MAX(e,max);
                   5214:         }
                   5215:     nd_setup_parameters(nvar,max);
                   5216:     ishomo = 1;
                   5217:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   5218:         ptozp((P)BDY(t),1,&dmy,&zp);
                   5219:         b = (pointer)ptondv(CO,vv,zp);
                   5220:         if ( ishomo )
                   5221:             ishomo = ishomo && ndv_ishomo(b);
                   5222:         if ( m ) ndv_mod(m,b);
                   5223:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   5224:     }
                   5225:     if ( fd0 ) NEXT(fd) = 0;
1.24      noro     5226:     ndv_setup(m,0,fd0,0,1,0);
1.1       noro     5227:     for ( x = 0, i = 0; i < nd_psn; i++ )
                   5228:         x = update_base(x,i);
                   5229:     if ( do_check ) {
                   5230:         x = nd_gb(m,ishomo,1,0,&perm);
                   5231:         if ( !x ) {
                   5232:             *rp = 0;
                   5233:             return;
                   5234:         }
                   5235:     } else {
                   5236: #if 0
                   5237:     /* bug ? */
                   5238:         for ( t = x; t; t = NEXT(t) )
                   5239:             BDY(t) = (pointer)nd_ps[(long)BDY(t)];
                   5240: #else
                   5241:     conv_ilist(0,0,x,&perm);
                   5242: #endif
                   5243:     }
                   5244:     x = ndv_reducebase(x,perm);
                   5245:     x = ndv_reduceall(m,x);
                   5246:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   5247:         NEXTNODE(r0,r);
                   5248:         BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   5249:     }
                   5250:     if ( r0 ) NEXT(r) = 0;
                   5251:     if ( !m && nd_nalg )
                   5252:         r0 = postprocess_algcoef(av,alist,r0);
                   5253:     MKLIST(*rp,r0);
                   5254: }
                   5255:
                   5256: NDV recompute_trace(NODE trace,NDV *p,int m);
                   5257: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
                   5258:
                   5259: NDV recompute_trace(NODE ti,NDV *p,int mod)
                   5260: {
                   5261:   int c,c1,c2,i;
                   5262:   NM mul,m,tail;
                   5263:   ND d,r,rm;
                   5264:   NODE sj;
                   5265:   NDV red;
                   5266:   Obj mj;
                   5267:
                   5268:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   5269:   CM(mul) = 1;
                   5270:   tail = 0;
                   5271:   for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
                   5272:     sj = BDY((LIST)BDY(ti));
                   5273:     if ( ARG0(sj) ) {
1.6       noro     5274:       red = p[ZTOS((Q)ARG1(sj))];
1.1       noro     5275:       mj = (Obj)ARG2(sj);
                   5276:       if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
                   5277:       else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
                   5278:       rm = ndv_mul_nm(mod,mul,red);
                   5279:       if ( !r ) r = rm;
                   5280:       else {
                   5281:         for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
                   5282:           if ( d ) {
                   5283:             NEXT(tail) = m; tail = m; LEN(d)++;
                   5284:           } else {
                   5285:             MKND(nd_nvar,m,1,d); tail = BDY(d);
                   5286:           }
                   5287:         }
                   5288:         if ( !m ) return 0; /* failure */
                   5289:                 else {
                   5290:           BDY(r) = m;
                   5291:                     if ( mod > 0 || mod == -1 ) {
                   5292:             c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
                   5293:             DMAR(c1,c2,0,mod,c);
                   5294:             nd_mul_c(mod,rm,c);
                   5295:                     } else {
                   5296:                       Z t,u;
                   5297:
                   5298:                       chsgnlf(HCZ(r),&t);
                   5299:                       divlf(t,HCZ(rm),&u);
                   5300:                       nd_mul_c_lf(rm,u);
                   5301:                     }
                   5302:           r = nd_add(mod,r,rm);
                   5303:         }
                   5304:           }
                   5305:      }
                   5306:   }
                   5307:   if ( tail ) NEXT(tail) = 0;
                   5308:   d = nd_add(mod,d,r);
                   5309:   nd_mul_c(mod,d,invm(HCM(d),mod));
                   5310:   return ndtondv(mod,d);
                   5311: }
                   5312:
                   5313: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
                   5314: {
                   5315:     VL tv,fv,vv,vc,av;
                   5316:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   5317:     int e,max,nvar,i;
                   5318:     NDV b;
                   5319:     int ishomo,nalg;
                   5320:     Alg alpha,dp;
                   5321:     P p,zp;
                   5322:     Q dmy;
                   5323:     LIST f1,f2;
                   5324:     Obj obj;
                   5325:     NumberField nf;
                   5326:     struct order_spec *ord1;
                   5327:   NODE permtrace,intred,ind,perm,trace,ti;
                   5328:   int len,n,j;
                   5329:   NDV *db,*pb;
                   5330:
                   5331:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     5332:     parse_nd_option(vv,current_option);
1.1       noro     5333:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5334:     switch ( ord->id ) {
                   5335:         case 1:
                   5336:             if ( ord->nv != nvar )
                   5337:                 error("nd_check : invalid order specification");
                   5338:             break;
                   5339:         default:
                   5340:             break;
                   5341:     }
                   5342:     nd_init_ord(ord);
1.6       noro     5343:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     5344:     nd_setup_parameters(nvar,0);
                   5345:
                   5346:   len = length(BDY(f));
                   5347:   db = (NDV *)MALLOC(len*sizeof(NDV *));
                   5348:   for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
                   5349:       ptozp((P)BDY(t),1,&dmy,&zp);
                   5350:       b = ptondv(CO,vv,zp);
                   5351:         ndv_mod(m,b);
                   5352:     ndv_mul_c(m,b,invm(HCM(b),m));
                   5353:     db[i] = b;
                   5354:     }
                   5355:
                   5356:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   5357:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   5358:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   5359:   perm = BDY((LIST)ARG0(permtrace));
                   5360:   trace = NEXT(permtrace);
                   5361:
                   5362:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     5363:     j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1       noro     5364:     if ( j > i ) i = j;
                   5365:   }
                   5366:   n = i+1;
                   5367:   pb = (NDV *)MALLOC(n*sizeof(NDV *));
                   5368:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   5369:     ti = BDY((LIST)BDY(t));
1.6       noro     5370:     pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1       noro     5371:   }
                   5372:   for ( t = trace; t; t = NEXT(t) ) {
                   5373:     ti = BDY((LIST)BDY(t));
1.6       noro     5374:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   5375:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     5376:       if ( DP_Print ) {
                   5377:            fprintf(asir_out,"."); fflush(asir_out);
                   5378:       }
                   5379:   }
                   5380:   for ( t = intred; t; t = NEXT(t) ) {
                   5381:     ti = BDY((LIST)BDY(t));
1.6       noro     5382:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   5383:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     5384:       if ( DP_Print ) {
                   5385:            fprintf(asir_out,"*"); fflush(asir_out);
                   5386:       }
                   5387:   }
                   5388:     for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
                   5389:         NEXTNODE(r0,r);
1.6       noro     5390:     b = pb[ZTOS((Q)BDY(t))];
1.1       noro     5391:         ndv_mul_c(m,b,invm(HCM(b),m));
                   5392: #if 0
1.6       noro     5393:         BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1       noro     5394: #else
1.6       noro     5395:         BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1       noro     5396: #endif
                   5397:     }
                   5398:     if ( r0 ) NEXT(r) = 0;
                   5399:     MKLIST(*rp,r0);
                   5400:     if ( DP_Print ) fprintf(asir_out,"\n");
                   5401: }
                   5402:
1.16      noro     5403: 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     5404: {
                   5405:     VL tv,fv,vv,vc,av;
                   5406:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   5407:     int m,nocheck,nvar,mindex,e,max;
                   5408:     NDV c;
                   5409:     NMV a;
                   5410:     P p,zp;
                   5411:     Q dmy;
                   5412:     EPOS oepos;
                   5413:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   5414:     Alg alpha,dp;
                   5415:     P poly;
                   5416:     LIST f1,f2,zpl;
                   5417:     Obj obj;
                   5418:     NumberField nf;
                   5419:     struct order_spec *ord1;
                   5420:     struct oEGT eg_check,eg0,eg1;
                   5421:     NODE tr,tl1,tl2,tl3,tl4;
                   5422:     LIST l1,l2,l3,l4,l5;
                   5423:     int *perm;
                   5424:     int j,ret;
                   5425:     Z jq,bpe;
1.15      noro     5426:     VECT hvect;
1.1       noro     5427:
1.41      noro     5428:     NcriB = NcriMF = Ncri2 = 0;
1.1       noro     5429:     nd_module = 0;
                   5430:     nd_lf = 0;
1.46      noro     5431:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   5432:     parse_nd_option(vv,current_option);
1.1       noro     5433:     if ( nd_lf ) {
                   5434:       if ( f4 )
                   5435:         nd_f4_lf_trace(f,v,trace,homo,ord,rp);
                   5436:       else
                   5437:         error("nd_gr_trace is not implemented yet over a large finite field");
                   5438:       return;
                   5439:     }
                   5440:     if ( DP_Multiple )
                   5441:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   5442:
                   5443:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5444:     switch ( ord->id ) {
                   5445:         case 1:
                   5446:             if ( ord->nv != nvar )
                   5447:                 error("nd_gr_trace : invalid order specification");
                   5448:             break;
                   5449:         default:
                   5450:             break;
                   5451:     }
                   5452:
                   5453:     get_algtree((Obj)f,&av);
                   5454:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   5455:     nd_ntrans = nvar;
                   5456:     nd_nalg = nalg;
                   5457:     /* #i -> t#i */
                   5458:     if ( nalg ) {
                   5459:         preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   5460:         ord = ord1;
                   5461:         f = f1;
                   5462:     }
                   5463:     nvar += nalg;
                   5464:
                   5465:     nocheck = 0;
                   5466:     mindex = 0;
                   5467:
                   5468:     if ( Demand ) nd_demand = 1;
                   5469:     else nd_demand = 0;
                   5470:
                   5471:     /* setup modulus */
                   5472:     if ( trace < 0 ) {
                   5473:         trace = -trace;
                   5474:         nocheck = 1;
                   5475:     }
                   5476:     m = trace > 1 ? trace : get_lprime(mindex);
                   5477:     nd_init_ord(ord);
                   5478:     mrank = 0;
                   5479:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   5480:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   5481:             if ( nd_module ) {
1.16      noro     5482:               if ( OID(BDY(t)) == O_DPM ) {
                   5483:                 e = dpm_getdeg((DPM)BDY(t),&trank);
                   5484:                 max = MAX(e,max);
                   5485:                 mrank = MAX(mrank,trank);
                   5486:               } else {
1.1       noro     5487:                 s = BDY((LIST)BDY(t));
                   5488:                 trank = length(s);
                   5489:                 mrank = MAX(mrank,trank);
                   5490:                 for ( ; s; s = NEXT(s) ) {
                   5491:                     e = getdeg(tv->v,(P)BDY(s));
                   5492:                     max = MAX(e,max);
                   5493:                 }
1.16      noro     5494:               }
1.1       noro     5495:             } else {
1.43      noro     5496:               if ( OID(BDY(t)) == O_DP ) {
                   5497:                 e = dp_getdeg((DP)BDY(t));
                   5498:                 max = MAX(e,max);
                   5499:               } else {
1.1       noro     5500:                 e = getdeg(tv->v,(P)BDY(t));
                   5501:                 max = MAX(e,max);
1.43      noro     5502:               }
1.1       noro     5503:             }
                   5504:         }
                   5505:     nd_setup_parameters(nvar,max);
                   5506:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   5507:     ishomo = 1;
                   5508:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   5509:         if ( nd_module ) {
1.16      noro     5510:           if ( OID(BDY(t)) == O_DPM ) {
                   5511:             Z cont;
                   5512:             DPM zdpm;
                   5513:
1.17      noro     5514:             if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16      noro     5515:             else zdpm = (DPM)BDY(t);
                   5516:             c = (pointer)dpmtondv(m,zdpm);
                   5517:           } else {
                   5518:             if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   5519:             else zpl = (LIST)BDY(t);
1.1       noro     5520:             c = (pointer)pltondv(CO,vv,zpl);
1.16      noro     5521:           }
1.1       noro     5522:         } else {
1.43      noro     5523:           if ( OID(BDY(t)) == O_DP ) {
                   5524:             DP zdp;
                   5525:
                   5526:             if ( !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
                   5527:             else zdp = (DP)BDY(t);
                   5528:             c = (pointer)dptondv(m,zdp);
                   5529:           } else {
                   5530:             if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   5531:             else zp = (P)BDY(t);
                   5532:             c = (pointer)ptondv(CO,vv,zp);
                   5533:           }
1.1       noro     5534:         }
                   5535:         if ( ishomo )
                   5536:             ishomo = ishomo && ndv_ishomo(c);
                   5537:         if ( c ) {
                   5538:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   5539:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   5540:         }
                   5541:     }
                   5542:     if ( in0 ) NEXT(in) = 0;
                   5543:     if ( fd0 ) NEXT(fd) = 0;
                   5544:     if ( !ishomo && homo ) {
                   5545:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   5546:             c = (NDV)BDY(t); len = LEN(c);
                   5547:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   5548:                 wmax = MAX(TD(DL(a)),wmax);
                   5549:         }
                   5550:         homogenize_order(ord,nvar,&ord1);
                   5551:         nd_init_ord(ord1);
                   5552:         nd_setup_parameters(nvar+1,wmax);
                   5553:         for ( t = fd0; t; t = NEXT(t) )
                   5554:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   5555:     }
                   5556:     if ( MaxDeg > 0 ) nocheck = 1;
                   5557:     while ( 1 ) {
                   5558:     tl1 = tl2 = tl3 = tl4 = 0;
                   5559:         if ( Demand )
                   5560:             nd_demand = 1;
1.24      noro     5561:         ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1       noro     5562:         if ( nd_gentrace ) {
                   5563:             MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   5564:         }
                   5565:         if ( ret )
                   5566:             cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
                   5567:         if ( !ret || !cand ) {
                   5568:             /* failure */
                   5569:             if ( trace > 1 ) { *rp = 0; return; }
                   5570:             else m = get_lprime(++mindex);
                   5571:             continue;
                   5572:         }
1.15      noro     5573:         if ( nd_gentrace ) {
                   5574:           MKVECT(hvect,nd_psn);
                   5575:           for ( i = 0; i < nd_psn; i++ )
                   5576:              ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   5577:         }
1.1       noro     5578:         if ( !ishomo && homo ) {
                   5579:             /* dehomogenization */
                   5580:             for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   5581:             nd_init_ord(ord);
                   5582:             nd_setup_parameters(nvar,0);
                   5583:         }
                   5584:         nd_demand = 0;
                   5585:         cand = ndv_reducebase(cand,perm);
                   5586:         if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
                   5587:         cand = ndv_reduceall(0,cand);
                   5588:         cbpe = nd_bpe;
                   5589:         if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
                   5590:         get_eg(&eg0);
                   5591:         if ( nocheck )
                   5592:             break;
                   5593:         if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   5594:             if ( nd_gentrace ) {
                   5595:           tl3 = nd_alltracelist; nd_alltracelist = 0;
                   5596:         } else tl3 = 0;
                   5597:             /* gbcheck : cand is a GB of Id(cand) ? */
                   5598:             if ( nd_vc || nd_gentrace || nd_gensyz )
                   5599:               ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
                   5600:             else
                   5601:               ret = nd_f4(0,1,0)!=0;
                   5602:             if ( nd_gentrace && nd_gensyz ) {
                   5603:           tl4 = nd_alltracelist; nd_alltracelist = 0;
                   5604:         } else tl4 = 0;
                   5605:     }
                   5606:     if ( ret ) break;
                   5607:         else if ( trace > 1 ) {
                   5608:             /* failure */
                   5609:             *rp = 0; return;
                   5610:         } else {
                   5611:             /* try the next modulus */
                   5612:             m = get_lprime(++mindex);
                   5613:             /* reset the parameters */
                   5614:             if ( !ishomo && homo ) {
                   5615:                 nd_init_ord(ord1);
                   5616:                 nd_setup_parameters(nvar+1,wmax);
                   5617:             } else {
                   5618:                 nd_init_ord(ord);
                   5619:                 nd_setup_parameters(nvar,max);
                   5620:             }
                   5621:         }
                   5622:     }
                   5623:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   5624:     if ( DP_Print )
1.6       noro     5625:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     5626:     /* dp->p */
                   5627:     nd_bpe = cbpe;
                   5628:     nd_setup_parameters(nd_nvar,0);
                   5629:     for ( r = cand; r; r = NEXT(r) ) {
1.16      noro     5630:       if ( nd_module ) {
1.17      noro     5631:         if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16      noro     5632:         else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17      noro     5633:       } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
                   5634:       else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1       noro     5635:     }
                   5636:     if ( nd_nalg )
                   5637:         cand = postprocess_algcoef(av,alist,cand);
                   5638:     MKLIST(*rp,cand);
                   5639:     if ( nd_gentrace ) {
                   5640:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   5641:     tl3 = reverse_node(tl3);
                   5642:     /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   5643:         for ( t = tl2; t; t = NEXT(t) ) {
                   5644:       /* s = [i,[*,j,*,*],...] */
                   5645:             s = BDY((LIST)BDY(t));
1.6       noro     5646:             j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     5647:       for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     5648:                 j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     5649:         ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   5650:             }
                   5651:     }
                   5652:     for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6       noro     5653:         STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     5654:     }
                   5655:         MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   5656:     MKLIST(l5,tl4);
1.6       noro     5657:       STOZ(nd_bpe,bpe);
1.15      noro     5658:         tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     5659:     }
                   5660: }
                   5661:
                   5662: /* XXX : module element is not considered  */
                   5663:
                   5664: void dltondl(int n,DL dl,UINT *r)
                   5665: {
                   5666:     UINT *d;
                   5667:     int i,j,l,s,ord_l;
                   5668:     struct order_pair *op;
                   5669:
                   5670:     d = (unsigned int *)dl->d;
                   5671:     for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
                   5672:     if ( nd_blockmask ) {
                   5673:         l = nd_blockmask->n;
                   5674:         op = nd_blockmask->order_pair;
                   5675:         for ( j = 0, s = 0; j < l; j++ ) {
                   5676:             ord_l = op[j].length;
                   5677:             for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
                   5678:         }
                   5679:         TD(r) = ndl_weight(r);
                   5680:         ndl_weight_mask(r);
                   5681:     } else {
                   5682:         for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
                   5683:         TD(r) = ndl_weight(r);
                   5684:     }
                   5685: }
                   5686:
                   5687: DL ndltodl(int n,UINT *ndl)
                   5688: {
                   5689:     DL dl;
                   5690:     int *d;
                   5691:     int i,j,l,s,ord_l;
                   5692:     struct order_pair *op;
                   5693:
1.33      noro     5694:     NEWDL_NOINIT(dl,n);
1.1       noro     5695:     dl->td = TD(ndl);
                   5696:     d = dl->d;
                   5697:     if ( nd_blockmask ) {
                   5698:         l = nd_blockmask->n;
                   5699:         op = nd_blockmask->order_pair;
                   5700:         for ( j = 0, s = 0; j < l; j++ ) {
                   5701:             ord_l = op[j].length;
                   5702:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   5703:         }
                   5704:     } else {
                   5705:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   5706:     }
                   5707:     return dl;
                   5708: }
                   5709:
1.24      noro     5710: void _ndltodl(UINT *ndl,DL dl)
                   5711: {
                   5712:     int *d;
                   5713:     int i,j,l,s,ord_l,n;
                   5714:     struct order_pair *op;
                   5715:
                   5716:     n = nd_nvar;
                   5717:     dl->td = TD(ndl);
                   5718:     d = dl->d;
                   5719:     if ( nd_blockmask ) {
                   5720:         l = nd_blockmask->n;
                   5721:         op = nd_blockmask->order_pair;
                   5722:         for ( j = 0, s = 0; j < l; j++ ) {
                   5723:             ord_l = op[j].length;
                   5724:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   5725:         }
                   5726:     } else {
                   5727:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   5728:     }
                   5729: }
                   5730:
1.1       noro     5731: void nmtodp(int mod,NM m,DP *r)
                   5732: {
                   5733:     DP dp;
                   5734:     MP mr;
                   5735:
                   5736:     NEWMP(mr);
                   5737:     mr->dl = ndltodl(nd_nvar,DL(m));
                   5738:     mr->c = (Obj)ndctop(mod,m->c);
                   5739:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   5740:     *r = dp;
                   5741: }
                   5742:
1.15      noro     5743: void ndltodp(UINT *d,DP *r)
                   5744: {
                   5745:     DP dp;
                   5746:     MP mr;
                   5747:
                   5748:     NEWMP(mr);
                   5749:     mr->dl = ndltodl(nd_nvar,d);
                   5750:     mr->c = (Obj)ONE;
                   5751:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   5752:     *r = dp;
                   5753: }
                   5754:
1.1       noro     5755: void ndl_print(UINT *dl)
                   5756: {
                   5757:     int n;
                   5758:     int i,j,l,ord_l,s,s0;
                   5759:     struct order_pair *op;
                   5760:
                   5761:     n = nd_nvar;
                   5762:     printf("<<");
                   5763:     if ( nd_blockmask ) {
                   5764:         l = nd_blockmask->n;
                   5765:         op = nd_blockmask->order_pair;
                   5766:         for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   5767:             ord_l = op[j].length;
                   5768:             for ( i = 0; i < ord_l; i++, s++ )
                   5769:                 printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
                   5770:         }
                   5771:     } else {
                   5772:         for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
                   5773:     }
                   5774:     printf(">>");
                   5775:     if ( nd_module && MPOS(dl) )
                   5776:         printf("*e%d",MPOS(dl));
                   5777: }
                   5778:
                   5779: void nd_print(ND p)
                   5780: {
                   5781:     NM m;
                   5782:
                   5783:     if ( !p )
                   5784:         printf("0\n");
                   5785:     else {
                   5786:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5787:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     5788:             else printf("+%ld*",CM(m));
1.1       noro     5789:             ndl_print(DL(m));
                   5790:         }
                   5791:         printf("\n");
                   5792:     }
                   5793: }
                   5794:
                   5795: void nd_print_q(ND p)
                   5796: {
                   5797:     NM m;
                   5798:
                   5799:     if ( !p )
                   5800:         printf("0\n");
                   5801:     else {
                   5802:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5803:             printf("+");
1.6       noro     5804:             printexpr(CO,(Obj)CZ(m));
1.1       noro     5805:             printf("*");
                   5806:             ndl_print(DL(m));
                   5807:         }
                   5808:         printf("\n");
                   5809:     }
                   5810: }
                   5811:
                   5812: void ndp_print(ND_pairs d)
                   5813: {
                   5814:     ND_pairs t;
                   5815:
                   5816:     for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
                   5817:     printf("\n");
                   5818: }
                   5819:
                   5820: void nd_removecont(int mod,ND p)
                   5821: {
                   5822:     int i,n;
                   5823:     Z *w;
                   5824:     NM m;
                   5825:     struct oVECT v;
                   5826:
                   5827:     if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
                   5828:     else if ( mod == -2 ) {
                   5829:       Z inv;
                   5830:       divlf(ONE,HCZ(p),&inv);
                   5831:       nd_mul_c_lf(p,inv);
                   5832:     } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
                   5833:     else {
                   5834:         for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   5835:         w = (Z *)MALLOC(n*sizeof(Q));
                   5836:         v.len = n;
                   5837:         v.body = (pointer *)w;
1.6       noro     5838:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5839:         removecont_array((P *)w,n,1);
1.6       noro     5840:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5841:     }
                   5842: }
                   5843:
                   5844: void nd_removecont2(ND p1,ND p2)
                   5845: {
                   5846:     int i,n1,n2,n;
                   5847:     Z *w;
                   5848:     NM m;
                   5849:     struct oVECT v;
                   5850:
                   5851:     n1 = nd_length(p1);
                   5852:     n2 = nd_length(p2);
                   5853:     n = n1+n2;
                   5854:     w = (Z *)MALLOC(n*sizeof(Q));
                   5855:     v.len = n;
                   5856:     v.body = (pointer *)w;
                   5857:     i = 0;
                   5858:     if ( p1 )
1.6       noro     5859:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5860:     if ( p2 )
1.6       noro     5861:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5862:     removecont_array((P *)w,n,1);
                   5863:     i = 0;
                   5864:     if ( p1 )
1.6       noro     5865:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5866:     if ( p2 )
1.6       noro     5867:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5868: }
                   5869:
                   5870: void ndv_removecont(int mod,NDV p)
                   5871: {
                   5872:     int i,len,all_p;
                   5873:     Z *c;
                   5874:     P *w;
                   5875:     Z dvr,t;
                   5876:     P g,cont,tp;
                   5877:     NMV m;
                   5878:
                   5879:     if ( mod == -1 )
                   5880:         ndv_mul_c(mod,p,_invsf(HCM(p)));
                   5881:     else if ( mod == -2 ) {
                   5882:       Z inv;
                   5883:       divlf(ONE,HCZ(p),&inv);
                   5884:       ndv_mul_c_lf(p,inv);
                   5885:     } else if ( mod )
                   5886:         ndv_mul_c(mod,p,invm(HCM(p),mod));
                   5887:     else {
                   5888:         len = p->len;
                   5889:         w = (P *)MALLOC(len*sizeof(P));
                   5890:         c = (Z *)MALLOC(len*sizeof(Q));
                   5891:         for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
                   5892:             ptozp(CP(m),1,(Q *)&c[i],&w[i]);
                   5893:             all_p = all_p && !NUM(w[i]);
                   5894:         }
                   5895:         if ( all_p ) {
                   5896:             qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
                   5897:             mulp(nd_vc,(P)dvr,g,&cont);
                   5898:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5899:                 divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
                   5900:             }
                   5901:         } else {
                   5902:             sortbynm((Q *)c,len);
                   5903:             qltozl((Q *)c,len,&dvr);
                   5904:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5905:                 divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
                   5906:             }
                   5907:         }
                   5908:     }
                   5909: }
                   5910:
                   5911: /* koko */
                   5912:
                   5913: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
                   5914: {
                   5915:     int len,i,max;
                   5916:     NMV m,mr0,mr,t;
                   5917:
                   5918:     len = p->len;
1.14      noro     5919:     for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1       noro     5920:         max = MAX(max,TD(DL(m)));
                   5921:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   5922:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   5923:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   5924:     t = (NMV)MALLOC(nmv_adv);
                   5925:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   5926:         ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6       noro     5927:         CZ(mr) = CZ(m);
1.1       noro     5928:         ndl_copy(DL(t),DL(mr));
                   5929:     }
                   5930:     NV(p)++;
                   5931:     BDY(p) = mr0;
                   5932: }
                   5933:
                   5934: void ndv_dehomogenize(NDV p,struct order_spec *ord)
                   5935: {
                   5936:     int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
                   5937:     int pos;
                   5938:     Q *w;
                   5939:     Q dvr,t;
                   5940:     NMV m,r;
                   5941:
                   5942:     len = p->len;
                   5943:     newnvar = nd_nvar-1;
                   5944:     newexporigin = nd_get_exporigin(ord);
                   5945:     if ( nd_module ) newmpos = newexporigin-1;
                   5946:     newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
                   5947:     for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
                   5948:         ndl_dehomogenize(DL(m));
                   5949:     if ( newwpd != nd_wpd ) {
                   5950:         newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
                   5951:         for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6       noro     5952:             CZ(r) = CZ(m);
1.1       noro     5953:             if ( nd_module ) pos = MPOS(DL(m));
                   5954:             for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   5955:             adj = nd_exporigin-newexporigin;
                   5956:             for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
                   5957:             if ( nd_module ) {
                   5958:                 DL(r)[newmpos] = pos;
                   5959:             }
                   5960:         }
                   5961:     }
                   5962:     NV(p)--;
                   5963: }
                   5964:
                   5965: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
                   5966: {
                   5967:     int i;
                   5968:     P *tpl,*tpl1;
                   5969:     NODE l;
                   5970:     P h,gcd,t;
                   5971:
                   5972:     tpl = (P *)MALLOC(m*sizeof(P));
                   5973:     tpl1 = (P *)MALLOC(m*sizeof(P));
                   5974:     bcopy(pl,tpl,m*sizeof(P));
                   5975:     gcd = (P)ONE;
                   5976:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   5977:         h = (P)BDY(l);
                   5978:         while ( 1 ) {
                   5979:             for ( i = 0; i < m; i++ )
                   5980:                 if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
                   5981:                     break;
                   5982:             if ( i == m ) {
                   5983:                 bcopy(tpl1,tpl,m*sizeof(P));
                   5984:                 mulp(vl,gcd,h,&t); gcd = t;
                   5985:             } else
                   5986:                 break;
                   5987:         }
                   5988:     }
                   5989:     if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
                   5990:     if ( full ) {
                   5991:         heu_nezgcdnpz(vl,tpl,m,&t);
                   5992:         mulp(vl,gcd,t,pr);
                   5993:     } else
                   5994:         *pr = gcd;
                   5995: }
                   5996:
                   5997: void removecont_array(P *p,int n,int full)
                   5998: {
                   5999:     int all_p,all_q,i;
                   6000:     Z *c;
                   6001:     P *w;
                   6002:     P t,s;
                   6003:
                   6004:     for ( all_q = 1, i = 0; i < n; i++ )
                   6005:         all_q = all_q && NUM(p[i]);
                   6006:     if ( all_q ) {
                   6007:         removecont_array_q((Z *)p,n);
                   6008:     } else {
                   6009:         c = (Z *)MALLOC(n*sizeof(Z));
                   6010:         w = (P *)MALLOC(n*sizeof(P));
                   6011:         for ( i = 0; i < n; i++ ) {
                   6012:             ptozp(p[i],1,(Q *)&c[i],&w[i]);
                   6013:         }
                   6014:         removecont_array_q(c,n);
                   6015:         nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
                   6016:         for ( i = 0; i < n; i++ ) {
                   6017:             divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
                   6018:         }
                   6019:     }
                   6020: }
                   6021:
                   6022: /* c is an int array */
                   6023:
                   6024: void removecont_array_q(Z *c,int n)
                   6025: {
                   6026:   struct oVECT v;
                   6027:   Z d0,d1,a,u,u1,gcd;
                   6028:   int i,j;
                   6029:   Z *q,*r;
                   6030:
                   6031:   q = (Z *)MALLOC(n*sizeof(Z));
                   6032:   r = (Z *)MALLOC(n*sizeof(Z));
                   6033:   v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   6034:   gcdvz_estimate(&v,&d0);
                   6035:   for ( i = 0; i < n; i++ ) {
                   6036:     divqrz(c[i],d0,&q[i],&r[i]);
                   6037:   }
                   6038:   for ( i = 0; i < n; i++ ) if ( r[i] ) break;
                   6039:   if ( i < n ) {
                   6040:     v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   6041:     gcdvz(&v,&d1);
                   6042:     gcdz(d0,d1,&gcd);
1.6       noro     6043:     /* exact division */
                   6044:     divsz(d0,gcd,&a);
1.1       noro     6045:     for ( i = 0; i < n; i++ ) {
                   6046:       mulz(a,q[i],&u);
                   6047:       if ( r[i] ) {
1.6       noro     6048:         /* exact division */
                   6049:         divsz(r[i],gcd,&u1);
1.1       noro     6050:         addz(u,u1,&q[i]);
                   6051:       } else
                   6052:         q[i] = u;
                   6053:     }
                   6054:   }
                   6055:   for ( i = 0; i < n; i++ ) c[i] = q[i];
                   6056: }
                   6057:
1.4       noro     6058: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
                   6059:
                   6060: void mpz_removecont_array(mpz_t *c,int n)
                   6061: {
                   6062:   mpz_t d0,a,u,u1,gcd;
                   6063:   int i,j;
1.13      noro     6064:   static mpz_t *q,*r;
                   6065:   static int c_len = 0;
1.4       noro     6066:
                   6067:   for ( i = 0; i < n; i++ )
                   6068:     if ( mpz_sgn(c[i]) ) break;
                   6069:   if ( i == n ) return;
                   6070:   gcdv_mpz_estimate(d0,c,n);
1.13      noro     6071:   if ( n > c_len ) {
                   6072:     q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   6073:     r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   6074:     c_len = n;
                   6075:   }
1.4       noro     6076:   for ( i = 0; i < n; i++ ) {
                   6077:     mpz_init(q[i]); mpz_init(r[i]);
                   6078:     mpz_fdiv_qr(q[i],r[i],c[i],d0);
                   6079:   }
                   6080:   for ( i = 0; i < n; i++ )
                   6081:     if ( mpz_sgn(r[i]) ) break;
                   6082:   mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
                   6083:   if ( i < n ) {
                   6084:     mpz_gcd(gcd,d0,r[i]);
                   6085:     for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
                   6086:     mpz_div(a,d0,gcd);
                   6087:     for ( i = 0; i < n; i++ ) {
                   6088:       mpz_mul(u,a,q[i]);
                   6089:       if ( mpz_sgn(r[i]) ) {
                   6090:         mpz_div(u1,r[i],gcd);
                   6091:         mpz_add(q[i],u,u1);
                   6092:       } else
                   6093:         mpz_set(q[i],u);
                   6094:     }
                   6095:   }
                   6096:   for ( i = 0; i < n; i++ )
                   6097:     mpz_set(c[i],q[i]);
                   6098: }
                   6099:
1.1       noro     6100: void nd_mul_c(int mod,ND p,int mul)
                   6101: {
                   6102:     NM m;
                   6103:     int c,c1;
                   6104:
                   6105:     if ( !p ) return;
                   6106:     if ( mul == 1 ) return;
                   6107:     if ( mod == -1 )
                   6108:         for ( m = BDY(p); m; m = NEXT(m) )
                   6109:             CM(m) = _mulsf(CM(m),mul);
                   6110:     else
                   6111:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   6112:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   6113:         }
                   6114: }
                   6115:
                   6116: void nd_mul_c_lf(ND p,Z mul)
                   6117: {
                   6118:     NM m;
                   6119:     Z c;
                   6120:
                   6121:     if ( !p ) return;
                   6122:     if ( UNIZ(mul) ) return;
                   6123:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   6124:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   6125:     }
                   6126: }
                   6127:
                   6128: void nd_mul_c_q(ND p,P mul)
                   6129: {
                   6130:     NM m;
                   6131:     P c;
                   6132:
                   6133:     if ( !p ) return;
                   6134:     if ( UNIQ(mul) ) return;
                   6135:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   6136:         mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
                   6137:     }
                   6138: }
                   6139:
                   6140: void nd_mul_c_p(VL vl,ND p,P mul)
                   6141: {
                   6142:     NM m;
                   6143:     P c;
                   6144:
                   6145:     if ( !p ) return;
                   6146:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   6147:         mulp(vl,CP(m),mul,&c); CP(m) = c;
                   6148:     }
                   6149: }
                   6150:
                   6151: void nd_free(ND p)
                   6152: {
                   6153:     NM t,s;
                   6154:
                   6155:     if ( !p ) return;
                   6156:     t = BDY(p);
                   6157:     while ( t ) {
                   6158:         s = NEXT(t);
                   6159:         FREENM(t);
                   6160:         t = s;
                   6161:     }
                   6162:     FREEND(p);
                   6163: }
                   6164:
                   6165: void ndv_free(NDV p)
                   6166: {
                   6167:     GCFREE(BDY(p));
                   6168: }
                   6169:
                   6170: void nd_append_red(UINT *d,int i)
                   6171: {
                   6172:     RHist m,m0;
                   6173:     int h;
                   6174:
                   6175:     NEWRHist(m);
                   6176:     h = ndl_hash_value(d);
                   6177:     m->index = i;
                   6178:     ndl_copy(d,DL(m));
                   6179:     NEXT(m) = nd_red[h];
                   6180:     nd_red[h] = m;
                   6181: }
                   6182:
                   6183: UINT *ndv_compute_bound(NDV p)
                   6184: {
                   6185:     UINT *d1,*d2,*t;
                   6186:     UINT u;
                   6187:     int i,j,k,l,len,ind;
                   6188:     NMV m;
                   6189:
                   6190:     if ( !p )
                   6191:         return 0;
                   6192:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6193:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6194:     len = LEN(p);
                   6195:     m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   6196:     for ( i = 1; i < len; i++, NMV_ADV(m) ) {
                   6197:         ndl_max(DL(m),d1,d2);
                   6198:         t = d1; d1 = d2; d2 = t;
                   6199:     }
                   6200:     l = nd_nvar+31;
                   6201:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   6202:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   6203:         u = d1[i];
                   6204:         k = (nd_epw-1)*nd_bpe;
                   6205:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   6206:             t[ind] = (u>>k)&nd_mask0;
                   6207:     }
                   6208:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   6209:     return t;
                   6210: }
                   6211:
                   6212: UINT *nd_compute_bound(ND p)
                   6213: {
                   6214:     UINT *d1,*d2,*t;
                   6215:     UINT u;
                   6216:     int i,j,k,l,len,ind;
                   6217:     NM m;
                   6218:
                   6219:     if ( !p )
                   6220:         return 0;
                   6221:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6222:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6223:     len = LEN(p);
                   6224:     m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   6225:     for ( m = NEXT(m); m; m = NEXT(m) ) {
                   6226:         ndl_lcm(DL(m),d1,d2);
                   6227:         t = d1; d1 = d2; d2 = t;
                   6228:     }
                   6229:     l = nd_nvar+31;
                   6230:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   6231:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   6232:         u = d1[i];
                   6233:         k = (nd_epw-1)*nd_bpe;
                   6234:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   6235:             t[ind] = (u>>k)&nd_mask0;
                   6236:     }
                   6237:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   6238:     return t;
                   6239: }
                   6240:
                   6241: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
                   6242: /* of a term. In this case we need additional 1 word. */
                   6243:
                   6244: int nd_get_exporigin(struct order_spec *ord)
                   6245: {
                   6246:     switch ( ord->id ) {
1.21      noro     6247:         case 0: case 2: case 256: case 258: case 300:
1.1       noro     6248:             return 1+nd_module;
                   6249:         case 1: case 257:
                   6250:             /* block order */
                   6251:             /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
                   6252:             /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
                   6253:             return ord->ord.block.length+1+nd_module;
                   6254:         case 3: case 259:
                   6255: #if 0
                   6256:             error("nd_get_exporigin : composite order is not supported yet.");
                   6257: #else
                   6258:             return 1+nd_module;
                   6259: #endif
                   6260:         default:
                   6261:             error("nd_get_exporigin : ivalid argument.");
                   6262:             return 0;
                   6263:     }
                   6264: }
                   6265:
                   6266: void nd_setup_parameters(int nvar,int max) {
                   6267:     int i,j,n,elen,ord_o,ord_l,l,s,wpd;
                   6268:     struct order_pair *op;
1.45      noro     6269:     extern int CNVars;
1.1       noro     6270:
                   6271:     nd_nvar = nvar;
1.45      noro     6272:     CNVars = nvar;
1.1       noro     6273:     if ( max ) {
                   6274:         /* XXX */
                   6275:         if ( do_weyl ) nd_bpe = 32;
                   6276:         else if ( max < 2 ) nd_bpe = 1;
                   6277:         else if ( max < 4 ) nd_bpe = 2;
                   6278:         else if ( max < 8 ) nd_bpe = 3;
                   6279:         else if ( max < 16 ) nd_bpe = 4;
                   6280:         else if ( max < 32 ) nd_bpe = 5;
                   6281:         else if ( max < 64 ) nd_bpe = 6;
                   6282:         else if ( max < 256 ) nd_bpe = 8;
                   6283:         else if ( max < 1024 ) nd_bpe = 10;
                   6284:         else if ( max < 65536 ) nd_bpe = 16;
                   6285:         else nd_bpe = 32;
                   6286:     }
                   6287:     if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
                   6288:         UINT t;
                   6289:     int st;
                   6290:         int *v;
                   6291:   /* t = max(weights) */
                   6292:         t = 0;
                   6293:         if ( current_dl_weight_vector )
                   6294:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                   6295:                 if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
                   6296:                 if ( t < st ) t = st;
                   6297:             }
                   6298:         if ( nd_matrix )
                   6299:             for ( i = 0; i < nd_matrix_len; i++ )
                   6300:                 for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
                   6301:                     if ( (st=v[j]) < 0 ) st = -st;
                   6302:                     if ( t < st ) t = st;
                   6303:         }
                   6304:         /* i = bitsize of t */
                   6305:         for ( i = 0; t; t >>=1, i++ );
                   6306:         /* i += bitsize of nd_nvar */
                   6307:         for ( t = nd_nvar; t; t >>=1, i++);
                   6308:         /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
                   6309:         if ( (nd_bpe+i) >= 31 )
                   6310:             error("nd_setup_parameters : too large weight");
                   6311:     }
                   6312:     nd_epw = (sizeof(UINT)*8)/nd_bpe;
                   6313:     elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   6314:     nd_exporigin = nd_get_exporigin(nd_ord);
                   6315:     wpd = nd_exporigin+elen;
                   6316:     if ( nd_module )
                   6317:         nd_mpos = nd_exporigin-1;
                   6318:     else
                   6319:         nd_mpos = -1;
                   6320:     if ( wpd != nd_wpd ) {
                   6321:         nd_free_private_storage();
                   6322:         nd_wpd = wpd;
                   6323:     }
                   6324:     if ( nd_bpe < 32 ) {
                   6325:         nd_mask0 = (1<<nd_bpe)-1;
                   6326:     } else {
                   6327:         nd_mask0 = 0xffffffff;
                   6328:     }
                   6329:     bzero(nd_mask,sizeof(nd_mask));
                   6330:     nd_mask1 = 0;
                   6331:     for ( i = 0; i < nd_epw; i++ ) {
                   6332:         nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   6333:         nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   6334:     }
                   6335:     nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
                   6336:     nd_epos = nd_create_epos(nd_ord);
                   6337:     nd_blockmask = nd_create_blockmask(nd_ord);
                   6338:     nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
                   6339: }
                   6340:
                   6341: ND_pairs nd_reconstruct(int trace,ND_pairs d)
                   6342: {
                   6343:     int i,obpe,oadv,h;
                   6344:     static NM prev_nm_free_list;
                   6345:     static ND_pairs prev_ndp_free_list;
                   6346:     RHist mr0,mr;
                   6347:     RHist r;
                   6348:     RHist *old_red;
                   6349:     ND_pairs s0,s,t;
                   6350:     EPOS oepos;
                   6351:
                   6352:     obpe = nd_bpe;
                   6353:     oadv = nmv_adv;
                   6354:     oepos = nd_epos;
                   6355:     if ( obpe < 2 ) nd_bpe = 2;
                   6356:     else if ( obpe < 3 ) nd_bpe = 3;
                   6357:     else if ( obpe < 4 ) nd_bpe = 4;
                   6358:     else if ( obpe < 5 ) nd_bpe = 5;
                   6359:     else if ( obpe < 6 ) nd_bpe = 6;
                   6360:     else if ( obpe < 8 ) nd_bpe = 8;
                   6361:     else if ( obpe < 10 ) nd_bpe = 10;
                   6362:     else if ( obpe < 16 ) nd_bpe = 16;
                   6363:     else if ( obpe < 32 ) nd_bpe = 32;
                   6364:     else error("nd_reconstruct : exponent too large");
                   6365:
                   6366:     nd_setup_parameters(nd_nvar,0);
                   6367:     prev_nm_free_list = _nm_free_list;
                   6368:     prev_ndp_free_list = _ndp_free_list;
                   6369:     _nm_free_list = 0;
                   6370:     _ndp_free_list = 0;
                   6371:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   6372:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   6373:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   6374:     }
                   6375:     if ( trace )
                   6376:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   6377:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   6378:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   6379:         }
                   6380:     s0 = 0;
                   6381:     for ( t = d; t; t = NEXT(t) ) {
                   6382:         NEXTND_pairs(s0,s);
                   6383:         s->i1 = t->i1;
                   6384:         s->i2 = t->i2;
1.24      noro     6385:         s->sig = t->sig;
1.1       noro     6386:         SG(s) = SG(t);
                   6387:         ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   6388:     }
                   6389:
                   6390:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   6391:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   6392:         old_red[i] = nd_red[i];
                   6393:         nd_red[i] = 0;
                   6394:     }
                   6395:     for ( i = 0; i < REDTAB_LEN; i++ )
                   6396:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   6397:             NEWRHist(mr);
                   6398:             mr->index = r->index;
                   6399:             SG(mr) = SG(r);
                   6400:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   6401:             h = ndl_hash_value(DL(mr));
                   6402:             NEXT(mr) = nd_red[h];
                   6403:             nd_red[h] = mr;
1.24      noro     6404:             mr->sig = r->sig;
1.1       noro     6405:         }
                   6406:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   6407:     old_red = 0;
                   6408:     for ( i = 0; i < nd_psn; i++ ) {
                   6409:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   6410:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24      noro     6411:         r->sig = nd_psh[i]->sig;
1.1       noro     6412:         nd_psh[i] = r;
                   6413:     }
                   6414:     if ( s0 ) NEXT(s) = 0;
                   6415:     prev_nm_free_list = 0;
                   6416:     prev_ndp_free_list = 0;
                   6417: #if 0
                   6418:     GC_gcollect();
                   6419: #endif
                   6420:     return s0;
                   6421: }
                   6422:
1.30      noro     6423: void nd_reconstruct_s(int trace,ND_pairs *d)
                   6424: {
                   6425:     int i,obpe,oadv,h;
                   6426:     static NM prev_nm_free_list;
                   6427:     static ND_pairs prev_ndp_free_list;
                   6428:     RHist mr0,mr;
                   6429:     RHist r;
                   6430:     RHist *old_red;
                   6431:     ND_pairs s0,s,t;
                   6432:     EPOS oepos;
                   6433:
                   6434:     obpe = nd_bpe;
                   6435:     oadv = nmv_adv;
                   6436:     oepos = nd_epos;
                   6437:     if ( obpe < 2 ) nd_bpe = 2;
                   6438:     else if ( obpe < 3 ) nd_bpe = 3;
                   6439:     else if ( obpe < 4 ) nd_bpe = 4;
                   6440:     else if ( obpe < 5 ) nd_bpe = 5;
                   6441:     else if ( obpe < 6 ) nd_bpe = 6;
                   6442:     else if ( obpe < 8 ) nd_bpe = 8;
                   6443:     else if ( obpe < 10 ) nd_bpe = 10;
                   6444:     else if ( obpe < 16 ) nd_bpe = 16;
                   6445:     else if ( obpe < 32 ) nd_bpe = 32;
                   6446:     else error("nd_reconstruct_s : exponent too large");
                   6447:
                   6448:     nd_setup_parameters(nd_nvar,0);
                   6449:     prev_nm_free_list = _nm_free_list;
                   6450:     prev_ndp_free_list = _ndp_free_list;
                   6451:     _nm_free_list = 0;
                   6452:     _ndp_free_list = 0;
                   6453:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   6454:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   6455:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   6456:     }
                   6457:     if ( trace )
                   6458:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   6459:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   6460:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   6461:         }
                   6462:
                   6463:     for ( i = 0; i < nd_nbase; i++ ) {
                   6464:       s0 = 0;
                   6465:       for ( t = d[i]; t; t = NEXT(t) ) {
                   6466:           NEXTND_pairs(s0,s);
                   6467:           s->i1 = t->i1;
                   6468:           s->i2 = t->i2;
                   6469:           s->sig = t->sig;
                   6470:           SG(s) = SG(t);
                   6471:           ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   6472:       }
1.47      noro     6473:       if ( s0 ) NEXT(s) = 0;
1.30      noro     6474:       d[i] = s0;
                   6475:     }
                   6476:
                   6477:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   6478:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   6479:         old_red[i] = nd_red[i];
                   6480:         nd_red[i] = 0;
                   6481:     }
                   6482:     for ( i = 0; i < REDTAB_LEN; i++ )
                   6483:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   6484:             NEWRHist(mr);
                   6485:             mr->index = r->index;
                   6486:             SG(mr) = SG(r);
                   6487:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   6488:             h = ndl_hash_value(DL(mr));
                   6489:             NEXT(mr) = nd_red[h];
                   6490:             nd_red[h] = mr;
                   6491:             mr->sig = r->sig;
                   6492:         }
                   6493:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   6494:     old_red = 0;
                   6495:     for ( i = 0; i < nd_psn; i++ ) {
                   6496:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   6497:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
                   6498:         r->sig = nd_psh[i]->sig;
                   6499:         nd_psh[i] = r;
                   6500:     }
                   6501:     if ( s0 ) NEXT(s) = 0;
                   6502:     prev_nm_free_list = 0;
                   6503:     prev_ndp_free_list = 0;
                   6504: #if 0
                   6505:     GC_gcollect();
                   6506: #endif
                   6507: }
                   6508:
1.1       noro     6509: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
                   6510: {
                   6511:     int n,i,ei,oepw,omask0,j,s,ord_l,l;
                   6512:     struct order_pair *op;
                   6513:
                   6514:     n = nd_nvar;
                   6515:     oepw = (sizeof(UINT)*8)/obpe;
                   6516:     omask0 = (1<<obpe)-1;
                   6517:     TD(r) = TD(d);
                   6518:     for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
                   6519:     if ( nd_blockmask ) {
                   6520:         l = nd_blockmask->n;
                   6521:         op = nd_blockmask->order_pair;
                   6522:         for ( i = 1; i < nd_exporigin; i++ )
                   6523:             r[i] = d[i];
                   6524:         for ( j = 0, s = 0; j < l; j++ ) {
                   6525:             ord_l = op[j].length;
                   6526:             for ( i = 0; i < ord_l; i++, s++ ) {
                   6527:                 ei =  GET_EXP_OLD(d,s);
                   6528:                 PUT_EXP(r,s,ei);
                   6529:             }
                   6530:         }
                   6531:     } else {
                   6532:         for ( i = 0; i < n; i++ ) {
                   6533:             ei = GET_EXP_OLD(d,i);
                   6534:             PUT_EXP(r,i,ei);
                   6535:         }
                   6536:     }
                   6537:     if ( nd_module ) MPOS(r) = MPOS(d);
                   6538: }
                   6539:
                   6540: ND nd_copy(ND p)
                   6541: {
                   6542:     NM m,mr,mr0;
                   6543:     int c,n;
                   6544:     ND r;
                   6545:
                   6546:     if ( !p )
                   6547:         return 0;
                   6548:     else {
                   6549:         for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   6550:             NEXTNM(mr0,mr);
                   6551:             CM(mr) = CM(m);
                   6552:             ndl_copy(DL(m),DL(mr));
                   6553:         }
                   6554:         NEXT(mr) = 0;
                   6555:         MKND(NV(p),mr0,LEN(p),r);
                   6556:         SG(r) = SG(p);
                   6557:         return r;
                   6558:     }
                   6559: }
                   6560:
                   6561: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
                   6562: {
                   6563:     NM m1,m2;
                   6564:     NDV p1,p2;
                   6565:     ND t1,t2;
                   6566:     UINT *lcm;
                   6567:     P gp,tp;
                   6568:     Z g,t;
                   6569:     Z iq;
                   6570:     int td;
                   6571:     LIST hist;
                   6572:     NODE node;
                   6573:     DP d;
                   6574:
                   6575:     if ( !mod && nd_demand ) {
                   6576:         p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
                   6577:     } else {
                   6578:         if ( trace ) {
                   6579:             p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   6580:         } else {
                   6581:             p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   6582:         }
                   6583:     }
                   6584:     lcm = LCM(p);
                   6585:     NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
                   6586:     if ( ndl_check_bound2(p->i1,DL(m1)) ) {
                   6587:         FREENM(m1); return 0;
                   6588:     }
                   6589:     NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
                   6590:     if ( ndl_check_bound2(p->i2,DL(m2)) ) {
                   6591:         FREENM(m1); FREENM(m2); return 0;
                   6592:     }
                   6593:
                   6594:     if ( mod == -1 ) {
                   6595:         CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
                   6596:     } else if ( mod > 0 ) {
                   6597:         CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
                   6598:     } else if ( mod == -2 ) {
                   6599:         CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
                   6600:     } else if ( nd_vc ) {
                   6601:         ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
                   6602:         divsp(nd_vc,HCP(p2),gp,&CP(m1));
                   6603:         divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
                   6604:     } else {
1.6       noro     6605:         igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1       noro     6606:     }
                   6607:     t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
                   6608:     *rp = nd_add(mod,t1,t2);
                   6609:     if ( nd_gentrace ) {
                   6610:         /* nd_tracelist is initialized */
1.6       noro     6611:         STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     6612:         MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6       noro     6613:         STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     6614:         MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
                   6615:         nd_tracelist = node;
                   6616:     }
1.24      noro     6617:     if ( *rp )
                   6618:       (*rp)->sig = p->sig;
1.1       noro     6619:     FREENM(m1); FREENM(m2);
                   6620:     return 1;
                   6621: }
                   6622:
                   6623: void ndv_mul_c(int mod,NDV p,int mul)
                   6624: {
                   6625:     NMV m;
                   6626:     int c,c1,len,i;
                   6627:
                   6628:     if ( !p ) return;
                   6629:     len = LEN(p);
                   6630:     if ( mod == -1 )
                   6631:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   6632:             CM(m) = _mulsf(CM(m),mul);
                   6633:     else
                   6634:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6635:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   6636:         }
                   6637: }
                   6638:
                   6639: void ndv_mul_c_lf(NDV p,Z mul)
                   6640: {
                   6641:     NMV m;
                   6642:     Z c;
                   6643:     int len,i;
                   6644:
                   6645:     if ( !p ) return;
                   6646:     len = LEN(p);
                   6647:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6648:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   6649:     }
                   6650: }
                   6651:
                   6652: /* for nd_det */
                   6653: void ndv_mul_c_q(NDV p,Z mul)
                   6654: {
                   6655:     NMV m;
                   6656:     Z c;
                   6657:     int len,i;
                   6658:
                   6659:     if ( !p ) return;
                   6660:     len = LEN(p);
                   6661:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     6662:         mulz(CZ(m),mul,&c); CZ(m) = c;
1.1       noro     6663:     }
                   6664: }
                   6665:
                   6666: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
                   6667:     int n2,i,j,l,n,tlen;
                   6668:     UINT *d0;
                   6669:     NM *tab,*psum;
                   6670:     ND s,r;
                   6671:     NM t;
                   6672:     NMV m1;
                   6673:
                   6674:     if ( !p ) return 0;
                   6675:     n = NV(p); n2 = n>>1;
                   6676:     d0 = DL(m0);
                   6677:     l = LEN(p);
                   6678:     for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   6679:     tab = (NM *)MALLOC(tlen*sizeof(NM));
                   6680:     psum = (NM *)MALLOC(tlen*sizeof(NM));
                   6681:     for ( i = 0; i < tlen; i++ ) psum[i] = 0;
                   6682:     m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   6683:     for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
                   6684:         /* m0(NM) * m1(NMV) => tab(NM) */
                   6685:         weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
                   6686:         for ( j = 0; j < tlen; j++ ) {
                   6687:             if ( tab[j] ) {
                   6688:                 NEXT(tab[j]) = psum[j];    psum[j] = tab[j];
                   6689:             }
                   6690:         }
                   6691:     }
                   6692:     for ( i = tlen-1, r = 0; i >= 0; i-- )
                   6693:         if ( psum[i] ) {
                   6694:             for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   6695:             MKND(n,psum[i],j,s);
                   6696:             r = nd_add(mod,r,s);
                   6697:         }
                   6698:     if ( r ) SG(r) = SG(p)+TD(d0);
                   6699:     return r;
                   6700: }
                   6701:
                   6702: /* product of monomials */
                   6703: /* XXX block order is not handled correctly */
                   6704:
                   6705: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   6706: {
                   6707:     int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
                   6708:     UINT *d0,*d1,*d,*dt,*ctab;
                   6709:     Z *ctab_q;
                   6710:     Z q,q1;
                   6711:     UINT c0,c1,c;
                   6712:     NM *p;
                   6713:     NM m,t;
                   6714:     int mpos;
                   6715:
                   6716:     for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   6717:     if ( !m0 || !m1 ) return;
                   6718:     d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
                   6719:     if ( nd_module )
                   6720:         if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
                   6721:
                   6722:     NEWNM(m); d = DL(m);
                   6723:     if ( mod ) {
                   6724:         c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
                   6725:     } else if ( nd_vc )
                   6726:         mulp(nd_vc,CP(m0),CP(m1),&CP(m));
                   6727:   else
1.6       noro     6728:         mulz(CZ(m0),CZ(m1),&CZ(m));
1.1       noro     6729:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   6730:     homo = n&1 ? 1 : 0;
                   6731:     if ( homo ) {
                   6732:         /* offset of h-degree */
                   6733:         h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   6734:         PUT_EXP(DL(m),n-1,h);
                   6735:         TD(DL(m)) = h;
                   6736:         if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   6737:     }
                   6738:     tab[0] = m;
                   6739:     NEWNM(m); d = DL(m);
                   6740:     for ( i = 0, curlen = 1; i < n2; i++ ) {
                   6741:         a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   6742:         k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   6743:         /* xi^a*(Di^k*xi^l)*Di^b */
                   6744:         a += l; b += k;
                   6745:         s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
                   6746:         if ( !k || !l ) {
                   6747:             for ( j = 0; j < curlen; j++ )
                   6748:                 if ( (t = tab[j]) != 0 ) {
                   6749:                     dt = DL(t);
                   6750:                     PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
                   6751:                     if ( nd_blockmask ) ndl_weight_mask(dt);
                   6752:                 }
                   6753:             curlen *= k+1;
                   6754:             continue;
                   6755:         }
                   6756:         min = MIN(k,l);
                   6757:         if ( mod ) {
                   6758:             ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
                   6759:             mkwcm(k,l,mod,(int *)ctab);
                   6760:         } else {
                   6761:             ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
                   6762:             mkwc(k,l,ctab_q);
                   6763:         }
                   6764:         for ( j = min; j >= 0; j-- ) {
                   6765:             for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
                   6766:             PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
                   6767:             h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
                   6768:             if ( homo ) {
                   6769:                 TD(d) = s;
                   6770:                 PUT_EXP(d,n-1,s-h);
                   6771:             } else TD(d) = h;
                   6772:             if ( nd_blockmask ) ndl_weight_mask(d);
                   6773:             if ( mod ) c = ctab[j];
                   6774:             else q = ctab_q[j];
                   6775:             p = tab+curlen*j;
                   6776:             if ( j == 0 ) {
                   6777:                 for ( u = 0; u < curlen; u++, p++ ) {
                   6778:                     if ( tab[u] ) {
                   6779:                         ndl_addto(DL(tab[u]),d);
                   6780:                         if ( mod ) {
                   6781:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
                   6782:                         } else if ( nd_vc )
                   6783:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
                   6784:             else {
1.6       noro     6785:                             mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1       noro     6786:                         }
                   6787:                     }
                   6788:                 }
                   6789:             } else {
                   6790:                 for ( u = 0; u < curlen; u++, p++ ) {
                   6791:                     if ( tab[u] ) {
                   6792:                         NEWNM(t);
                   6793:                         ndl_add(DL(tab[u]),d,DL(t));
                   6794:                         if ( mod ) {
                   6795:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
                   6796:                         } else if ( nd_vc )
                   6797:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
                   6798:             else
1.6       noro     6799:                             mulz(CZ(tab[u]),q,&CZ(t));
1.1       noro     6800:                         *p = t;
                   6801:                     }
                   6802:                 }
                   6803:             }
                   6804:         }
                   6805:         curlen *= k+1;
                   6806:     }
                   6807:     FREENM(m);
                   6808:     if ( nd_module ) {
                   6809:         mpos = MPOS(d1);
                   6810:         for ( i = 0; i < tlen; i++ )
                   6811:             if ( tab[i] ) {
                   6812:                 d = DL(tab[i]);
                   6813:                 MPOS(d) = mpos;
                   6814:                 TD(d) = ndl_weight(d);
                   6815:             }
                   6816:     }
                   6817: }
                   6818:
                   6819: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   6820: {
                   6821:     NM mr,mr0;
                   6822:     NMV m;
                   6823:     UINT *d,*dt,*dm;
                   6824:     int c,n,td,i,c1,c2,len;
                   6825:     Q q;
                   6826:     ND r;
                   6827:
                   6828:     if ( !p ) return 0;
                   6829:     else {
                   6830:         n = NV(p); m = BDY(p);
                   6831:         d = DL(m0);
                   6832:         len = LEN(p);
                   6833:         mr0 = 0;
                   6834:         td = TD(d);
                   6835:         c = CM(m0);
                   6836:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6837:             NEXTNM(mr0,mr);
                   6838:             CM(mr) = 1;
                   6839:             ndl_add(DL(m),d,DL(mr));
                   6840:         }
                   6841:         NEXT(mr) = 0;
                   6842:         MKND(NV(p),mr0,len,r);
                   6843:         SG(r) = SG(p) + TD(d);
                   6844:         return r;
                   6845:     }
                   6846: }
                   6847:
                   6848: ND ndv_mul_nm(int mod,NM m0,NDV p)
                   6849: {
                   6850:     NM mr,mr0;
                   6851:     NMV m;
                   6852:     UINT *d,*dt,*dm;
                   6853:     int c,n,td,i,c1,c2,len;
                   6854:     P q;
                   6855:     ND r;
                   6856:
                   6857:     if ( !p ) return 0;
                   6858:     else if ( do_weyl ) {
                   6859:         if ( mod < 0 ) {
                   6860:             error("ndv_mul_nm : not implemented (weyl)");
                   6861:             return 0;
                   6862:         } else
                   6863:             return weyl_ndv_mul_nm(mod,m0,p);
                   6864:     } else {
                   6865:         n = NV(p); m = BDY(p);
                   6866:         d = DL(m0);
                   6867:         len = LEN(p);
                   6868:         mr0 = 0;
                   6869:         td = TD(d);
                   6870:         if ( mod == -1 ) {
                   6871:             c = CM(m0);
                   6872:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6873:                 NEXTNM(mr0,mr);
                   6874:                 CM(mr) = _mulsf(CM(m),c);
                   6875:                 ndl_add(DL(m),d,DL(mr));
                   6876:             }
                   6877:         } else if ( mod == -2 ) {
                   6878:             Z cl;
                   6879:             cl = CZ(m0);
                   6880:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6881:                 NEXTNM(mr0,mr);
                   6882:                 mullf(CZ(m),cl,&CZ(mr));
                   6883:                 ndl_add(DL(m),d,DL(mr));
                   6884:             }
                   6885:         } else if ( mod ) {
                   6886:             c = CM(m0);
                   6887:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6888:                 NEXTNM(mr0,mr);
                   6889:                 c1 = CM(m);
                   6890:                 DMAR(c1,c,0,mod,c2);
                   6891:                 CM(mr) = c2;
                   6892:                 ndl_add(DL(m),d,DL(mr));
                   6893:             }
                   6894:         } else {
                   6895:             q = CP(m0);
                   6896:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6897:                 NEXTNM(mr0,mr);
                   6898:                 mulp(nd_vc,CP(m),q,&CP(mr));
                   6899:                 ndl_add(DL(m),d,DL(mr));
                   6900:             }
                   6901:         }
                   6902:         NEXT(mr) = 0;
                   6903:         MKND(NV(p),mr0,len,r);
                   6904:         SG(r) = SG(p) + TD(d);
                   6905:         return r;
                   6906:     }
                   6907: }
                   6908:
                   6909: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
                   6910: {
                   6911:     NM mq0,mq;
                   6912:     NMV tm;
                   6913:     Q q;
                   6914:     int i,nv,sg,c,c1,c2,hindex;
                   6915:     ND p,t,r;
                   6916:
                   6917:     if ( bucket->m < 0 ) return 0;
                   6918:     else {
                   6919:         nv = NV(d);
                   6920:         mq0 = 0;
                   6921:         tm = (NMV)MALLOC(nmv_adv);
                   6922:         while ( 1 ) {
                   6923:             if ( mod > 0 || mod == -1 )
                   6924:               hindex = head_pbucket(mod,bucket);
                   6925:             else if ( mod == -2 )
                   6926:               hindex = head_pbucket_lf(bucket);
                   6927:             else
                   6928:               hindex = head_pbucket_q(bucket);
                   6929:             if ( hindex < 0 ) break;
                   6930:             p = bucket->body[hindex];
                   6931:             NEXTNM(mq0,mq);
                   6932:             ndl_sub(HDL(p),HDL(d),DL(mq));
                   6933:             ndl_copy(DL(mq),DL(tm));
                   6934:             if ( mod ) {
                   6935:                 c1 = invm(HCM(d),mod); c2 = HCM(p);
                   6936:                 DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   6937:                 CM(tm) = mod-c;
                   6938:             } else {
1.6       noro     6939:                 divsz(HCZ(p),HCZ(d),&CZ(mq));
                   6940:                 chsgnz(CZ(mq),&CZ(tm));
1.1       noro     6941:             }
                   6942:             t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
                   6943:             bucket->body[hindex] = nd_remove_head(p);
                   6944:             t = nd_remove_head(t);
                   6945:             add_pbucket(mod,bucket,t);
                   6946:         }
                   6947:         if ( !mq0 )
                   6948:             r = 0;
                   6949:         else {
                   6950:             NEXT(mq) = 0;
                   6951:             for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   6952:             MKND(nv,mq0,i,r);
                   6953:             /* XXX */
                   6954:             SG(r) = HTD(r);
                   6955:         }
                   6956:         return r;
                   6957:     }
                   6958: }
                   6959:
                   6960: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   6961: {
                   6962:     NMV m,mr,mr0,t;
                   6963:     int len,i,k;
                   6964:
                   6965:     if ( !p ) return;
                   6966:     m = BDY(p); len = LEN(p);
                   6967:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   6968:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   6969:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   6970:     t = (NMV)MALLOC(nmv_adv);
                   6971:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6       noro     6972:         CZ(t) = CZ(m);
1.1       noro     6973:         for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   6974:         ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6       noro     6975:         CZ(mr) = CZ(t);
1.1       noro     6976:         ndl_copy(DL(t),DL(mr));
                   6977:     }
                   6978:     BDY(p) = mr0;
                   6979: }
                   6980:
                   6981: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   6982: {
                   6983:     NMV m,mr,mr0;
                   6984:     int len,i;
                   6985:     NDV r;
                   6986:
                   6987:     if ( !p ) return 0;
                   6988:     m = BDY(p); len = LEN(p);
                   6989:     mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   6990:     for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   6991:         ndl_zero(DL(mr));
                   6992:         ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6       noro     6993:         CZ(mr) = CZ(m);
1.1       noro     6994:     }
                   6995:     MKNDV(NV(p),mr0,len,r);
                   6996:     SG(r) = SG(p);
1.24      noro     6997:     r->sig = p->sig;
1.1       noro     6998:     return r;
                   6999: }
                   7000:
                   7001: /* duplicate p */
                   7002:
                   7003: NDV ndv_dup(int mod,NDV p)
                   7004: {
                   7005:     NDV d;
                   7006:     NMV t,m,m0;
                   7007:     int i,len;
                   7008:
                   7009:     if ( !p ) return 0;
                   7010:     len = LEN(p);
                   7011:     m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   7012:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   7013:         ndl_copy(DL(t),DL(m));
1.6       noro     7014:         CZ(m) = CZ(t);
1.1       noro     7015:     }
                   7016:     MKNDV(NV(p),m0,len,d);
                   7017:     SG(d) = SG(p);
                   7018:     return d;
                   7019: }
                   7020:
                   7021: NDV ndv_symbolic(int mod,NDV p)
                   7022: {
                   7023:     NDV d;
                   7024:     NMV t,m,m0;
                   7025:     int i,len;
                   7026:
                   7027:     if ( !p ) return 0;
                   7028:     len = LEN(p);
                   7029:     m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   7030:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   7031:         ndl_copy(DL(t),DL(m));
1.6       noro     7032:         CZ(m) = ONE;
1.1       noro     7033:     }
                   7034:     MKNDV(NV(p),m0,len,d);
                   7035:     SG(d) = SG(p);
                   7036:     return d;
                   7037: }
                   7038:
                   7039: ND nd_dup(ND p)
                   7040: {
                   7041:     ND d;
                   7042:     NM t,m,m0;
                   7043:
                   7044:     if ( !p ) return 0;
                   7045:     for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   7046:         NEXTNM(m0,m);
                   7047:         ndl_copy(DL(t),DL(m));
1.6       noro     7048:         CZ(m) = CZ(t);
1.1       noro     7049:     }
                   7050:     if ( m0 ) NEXT(m) = 0;
                   7051:     MKND(NV(p),m0,LEN(p),d);
                   7052:     SG(d) = SG(p);
                   7053:     return d;
                   7054: }
                   7055:
                   7056: /* XXX if p->len == 0 then it represents 0 */
                   7057:
                   7058: void ndv_mod(int mod,NDV p)
                   7059: {
                   7060:     NMV t,d;
                   7061:     int r,s,u;
                   7062:     int i,len,dlen;
                   7063:     P cp;
                   7064:     Z c;
                   7065:     Obj gfs;
                   7066:
                   7067:     if ( !p ) return;
                   7068:     len = LEN(p);
                   7069:     dlen = 0;
                   7070:     if ( mod == -1 )
                   7071:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   7072:             simp_ff((Obj)CP(t),&gfs);
                   7073:             if ( gfs ) {
                   7074:               r = FTOIF(CONT((GFS)gfs));
                   7075:               CM(d) = r;
                   7076:               ndl_copy(DL(t),DL(d));
                   7077:               NMV_ADV(d);
                   7078:               dlen++;
                   7079:             }
                   7080:         }
                   7081:     else if ( mod == -2 )
                   7082:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   7083:             simp_ff((Obj)CP(t),&gfs);
                   7084:             if ( gfs ) {
                   7085:               lmtolf((LM)gfs,&CZ(d));
                   7086:               ndl_copy(DL(t),DL(d));
                   7087:               NMV_ADV(d);
                   7088:               dlen++;
                   7089:             }
                   7090:         }
                   7091:     else
                   7092:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   7093:             if ( nd_vc ) {
                   7094:                 nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
                   7095:                 c = (Z)cp;
                   7096:             } else
1.6       noro     7097:                 c = CZ(t);
1.1       noro     7098:             r = remqi((Q)c,mod);
                   7099:             if ( r ) {
                   7100:                 CM(d) = r;
                   7101:                 ndl_copy(DL(t),DL(d));
                   7102:                 NMV_ADV(d);
                   7103:                 dlen++;
                   7104:             }
                   7105:         }
                   7106:     LEN(p) = dlen;
                   7107: }
                   7108:
                   7109: NDV ptondv(VL vl,VL dvl,P p)
                   7110: {
                   7111:     ND nd;
                   7112:
                   7113:     nd = ptond(vl,dvl,p);
                   7114:     return ndtondv(0,nd);
                   7115: }
                   7116:
                   7117: void pltozpl(LIST l,Q *cont,LIST *pp)
                   7118: {
1.16      noro     7119:   NODE nd,nd1;
                   7120:   int n;
                   7121:   P *pl;
                   7122:   Q *cl;
                   7123:   int i;
                   7124:   P dmy;
                   7125:   Z dvr,inv;
                   7126:   LIST r;
                   7127:
                   7128:   nd = BDY(l); n = length(nd);
                   7129:   pl = (P *)MALLOC(n*sizeof(P));
                   7130:   cl = (Q *)MALLOC(n*sizeof(Q));
                   7131:   for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
                   7132:     ptozp((P)BDY(nd),1,&cl[i],&dmy);
                   7133:   }
                   7134:   qltozl(cl,n,&dvr);
                   7135:   divz(ONE,dvr,&inv);
                   7136:   nd = BDY(l);
                   7137:   for ( i = 0; i < n; i++, nd = NEXT(nd) )
                   7138:     divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
                   7139:   nd = 0;
                   7140:   for ( i = n-1; i >= 0; i-- ) {
                   7141:     MKNODE(nd1,pl[i],nd); nd = nd1;
                   7142:   }
                   7143:   MKLIST(r,nd);
                   7144:   *pp = r;
1.1       noro     7145: }
                   7146:
                   7147: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
                   7148:
                   7149: NDV pltondv(VL vl,VL dvl,LIST p)
                   7150: {
                   7151:     int i;
                   7152:     NODE t;
                   7153:     ND r,ri;
                   7154:     NM m;
                   7155:
                   7156:     if ( !nd_module ) error("pltond : module order must be set");
                   7157:     r = 0;
                   7158:     for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
                   7159:         ri = ptond(vl,dvl,(P)BDY(t));
                   7160:         if ( ri )
                   7161:             for ( m = BDY(ri); m; m = NEXT(m) ) {
                   7162:                 MPOS(DL(m)) = i;
                   7163:                 TD(DL(m)) = ndl_weight(DL(m));
                   7164:                 if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   7165:             }
                   7166:         r = nd_add(0,r,ri);
                   7167:     }
                   7168:     return ndtondv(0,r);
                   7169: }
                   7170:
                   7171: ND ptond(VL vl,VL dvl,P p)
                   7172: {
                   7173:     int n,i,j,k,e;
                   7174:     VL tvl;
                   7175:     V v;
                   7176:     DCP dc;
                   7177:     DCP *w;
                   7178:     ND r,s,t,u;
                   7179:     P x;
                   7180:     int c;
                   7181:     UINT *d;
                   7182:     NM m,m0;
                   7183:
                   7184:     if ( !p )
                   7185:         return 0;
                   7186:     else if ( NUM(p) ) {
                   7187:         NEWNM(m);
                   7188:         ndl_zero(DL(m));
1.48      noro     7189:         if ( RATN(p) && !INT((Q)p) )
1.1       noro     7190:           error("ptond : input must be integer-coefficient");
1.6       noro     7191:         CZ(m) = (Z)p;
1.1       noro     7192:         NEXT(m) = 0;
                   7193:         MKND(nd_nvar,m,1,r);
                   7194:         SG(r) = 0;
                   7195:         return r;
                   7196:     } else {
                   7197:         for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   7198:         w = (DCP *)MALLOC(k*sizeof(DCP));
                   7199:         for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   7200:         for ( i = 0, tvl = dvl, v = VR(p);
                   7201:             tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   7202:         if ( !tvl ) {
                   7203:             for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   7204:                 t = ptond(vl,dvl,COEF(w[j]));
                   7205:                 pwrp(vl,x,DEG(w[j]),&p);
                   7206:                 nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   7207:             }
                   7208:             return s;
                   7209:         } else {
                   7210:             NEWNM(m0); d = DL(m0);
                   7211:             for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6       noro     7212:                 ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1       noro     7213:                 TD(d) = MUL_WEIGHT(e,i);
                   7214:                 if ( nd_blockmask) ndl_weight_mask(d);
                   7215:                 if ( nd_module ) MPOS(d) = 0;
                   7216:                 t = ptond(vl,dvl,COEF(w[j]));
                   7217:                 for ( m = BDY(t); m; m = NEXT(m) )
                   7218:                     ndl_addto(DL(m),d);
                   7219:                 SG(t) += TD(d);
                   7220:                 s = nd_add(0,s,t);
                   7221:             }
                   7222:             FREENM(m0);
                   7223:             return s;
                   7224:         }
                   7225:     }
                   7226: }
                   7227:
                   7228: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   7229: {
                   7230:     VL tvl;
                   7231:     int len,n,j,i,e;
                   7232:     NMV m;
                   7233:     Z q;
                   7234:     P c;
                   7235:     UINT *d;
                   7236:     P s,r,u,t,w;
                   7237:     GFS gfs;
                   7238:
                   7239:     if ( !p ) return 0;
                   7240:     else {
                   7241:         len = LEN(p);
                   7242:         n = NV(p);
                   7243:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   7244:         for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   7245:             if ( mod == -1 ) {
                   7246:                e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   7247:             } else if ( mod == -2 ) {
                   7248:                c = (P)CZ(m);
                   7249:             } else if ( mod > 0 ) {
1.6       noro     7250:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     7251:             } else
                   7252:                 c = CP(m);
                   7253:             d = DL(m);
                   7254:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     7255:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     7256:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   7257:             }
                   7258:             addp(vl,s,t,&u); s = u;
                   7259:         }
                   7260:         return s;
                   7261:     }
                   7262: }
                   7263:
                   7264: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
                   7265: {
                   7266:     VL tvl;
                   7267:     int len,n,j,i,e;
                   7268:     NMV m;
                   7269:     Z q;
                   7270:     P c;
                   7271:     UINT *d;
                   7272:     P s,r,u,t,w;
                   7273:     GFS gfs;
                   7274:     P *a;
                   7275:     LIST l;
                   7276:     NODE nd,nd1;
                   7277:
                   7278:     if ( !p ) return 0;
                   7279:     else {
                   7280:         a = (P *)MALLOC((rank+1)*sizeof(P));
                   7281:         for ( i = 0; i <= rank; i++ ) a[i] = 0;
                   7282:         len = LEN(p);
                   7283:         n = NV(p);
                   7284:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   7285:         for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
                   7286:             if ( mod == -1 ) {
                   7287:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   7288:             } else if ( mod ) {
1.6       noro     7289:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     7290:             } else
                   7291:                 c = CP(m);
                   7292:             d = DL(m);
                   7293:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     7294:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     7295:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   7296:             }
                   7297:             addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
                   7298:         }
                   7299:         nd = 0;
                   7300:         for ( i = rank; i > 0; i-- ) {
                   7301:             MKNODE(nd1,a[i],nd); nd = nd1;
                   7302:         }
                   7303:         MKLIST(l,nd);
                   7304:         return l;
                   7305:     }
                   7306: }
                   7307:
                   7308: NDV ndtondv(int mod,ND p)
                   7309: {
                   7310:     NDV d;
                   7311:     NMV m,m0;
                   7312:     NM t;
                   7313:     int i,len;
                   7314:
                   7315:     if ( !p ) return 0;
                   7316:     len = LEN(p);
                   7317:     if ( mod > 0 || mod == -1 )
                   7318:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   7319:     else
                   7320:         m0 = m = MALLOC(len*nmv_adv);
                   7321: #if 0
                   7322:     ndv_alloc += nmv_adv*len;
                   7323: #endif
                   7324:     for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   7325:         ndl_copy(DL(t),DL(m));
1.6       noro     7326:         CZ(m) = CZ(t);
1.1       noro     7327:     }
                   7328:     MKNDV(NV(p),m0,len,d);
                   7329:     SG(d) = SG(p);
1.24      noro     7330:     d->sig = p->sig;
1.1       noro     7331:     return d;
                   7332: }
                   7333:
1.16      noro     7334: static int dmm_comp_nv;
                   7335:
                   7336: int dmm_comp(DMM *a,DMM *b)
                   7337: {
                   7338:    return -compdmm(dmm_comp_nv,*a,*b);
                   7339: }
                   7340:
                   7341: void dmm_sort_by_ord(DMM *a,int len,int nv)
                   7342: {
                   7343:   dmm_comp_nv = nv;
                   7344:   qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
                   7345: }
                   7346:
                   7347: void dpm_sort(DPM p,DPM *rp)
                   7348: {
                   7349:   DMM t,t1;
                   7350:   int len,i,n;
                   7351:   DMM *a;
                   7352:   DPM d;
                   7353:
                   7354:   if ( !p ) *rp = 0;
                   7355:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   7356:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   7357:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   7358:   n = p->nv;
                   7359:   dmm_sort_by_ord(a,len,n);
                   7360:   t = 0;
                   7361:   for ( i = len-1; i >= 0; i-- ) {
                   7362:     NEWDMM(t1);
                   7363:     t1->c = a[i]->c;
                   7364:     t1->dl = a[i]->dl;
                   7365:     t1->pos = a[i]->pos;
                   7366:     t1->next = t;
                   7367:     t = t1;
                   7368:   }
                   7369:   MKDPM(n,t,d);
                   7370:   SG(d) = SG(p);
                   7371:   *rp = d;
                   7372: }
                   7373:
1.18      noro     7374: int dpm_comp(DPM *a,DPM *b)
                   7375: {
1.22      noro     7376:   return -compdpm(CO,*a,*b);
1.18      noro     7377: }
                   7378:
                   7379: NODE dpm_sort_list(NODE l)
                   7380: {
                   7381:   int i,len;
                   7382:   NODE t,t1;
                   7383:   DPM *a;
                   7384:
                   7385:   len = length(l);
                   7386:   a = (DPM *)MALLOC(len*sizeof(DPM));
                   7387:   for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
                   7388:   qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
                   7389:   t = 0;
                   7390:   for ( i = len-1; i >= 0; i-- ) {
                   7391:     MKNODE(t1,(pointer)a[i],t); t = t1;
                   7392:   }
                   7393:   return t;
                   7394: }
                   7395:
1.20      noro     7396: int nmv_comp(NMV a,NMV b)
                   7397: {
1.21      noro     7398:   int t;
                   7399:   t = DL_COMPARE(a->dl,b->dl);
                   7400:   return -t;
1.20      noro     7401: }
                   7402:
1.43      noro     7403: NDV dptondv(int mod,DP p)
                   7404: {
                   7405:   NDV d;
                   7406:   NMV m,m0;
                   7407:   MP t;
                   7408:   MP *a;
                   7409:   int i,len,n;
                   7410:
                   7411:   if ( !p ) return 0;
                   7412:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   7413:   n = p->nv;
                   7414:   if ( mod > 0 || mod == -1 )
                   7415:     m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   7416:   else
                   7417:     m0 = m = MALLOC(len*nmv_adv);
                   7418:   for ( i = 0, t = BDY(p); i < len; i++, NMV_ADV(m), t = NEXT(t) ) {
                   7419:     dltondl(n,t->dl,DL(m));
                   7420:     TD(DL(m)) = ndl_weight(DL(m));
                   7421:     CZ(m) = (Z)t->c;
                   7422:   }
                   7423:   qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
                   7424:   MKNDV(NV(p),m0,len,d);
                   7425:   SG(d) = SG(p);
                   7426:   return d;
                   7427: }
                   7428:
1.16      noro     7429: NDV dpmtondv(int mod,DPM p)
                   7430: {
                   7431:   NDV d;
                   7432:   NMV m,m0;
                   7433:   DMM t;
                   7434:   DMM *a;
                   7435:   int i,len,n;
                   7436:
                   7437:   if ( !p ) return 0;
                   7438:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   7439:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   7440:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   7441:   n = p->nv;
                   7442:   dmm_sort_by_ord(a,len,n);
                   7443:   if ( mod > 0 || mod == -1 )
                   7444:     m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   7445:   else
                   7446:     m0 = m = MALLOC(len*nmv_adv);
                   7447: #if 0
                   7448:   ndv_alloc += nmv_adv*len;
                   7449: #endif
                   7450:   for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   7451:     dltondl(n,a[i]->dl,DL(m));
                   7452:     MPOS(DL(m)) = a[i]->pos;
1.20      noro     7453:     TD(DL(m)) = ndl_weight(DL(m));
1.16      noro     7454:     CZ(m) = (Z)a[i]->c;
                   7455:   }
1.20      noro     7456:   qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16      noro     7457:   MKNDV(NV(p),m0,len,d);
                   7458:   SG(d) = SG(p);
                   7459:   return d;
                   7460: }
                   7461:
1.1       noro     7462: ND ndvtond(int mod,NDV p)
                   7463: {
                   7464:     ND d;
                   7465:     NM m,m0;
                   7466:     NMV t;
                   7467:     int i,len;
                   7468:
                   7469:     if ( !p ) return 0;
                   7470:     m0 = 0;
                   7471:     len = p->len;
                   7472:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   7473:         NEXTNM(m0,m);
                   7474:         ndl_copy(DL(t),DL(m));
1.6       noro     7475:         CZ(m) = CZ(t);
1.1       noro     7476:     }
                   7477:     NEXT(m) = 0;
                   7478:     MKND(NV(p),m0,len,d);
                   7479:     SG(d) = SG(p);
1.28      noro     7480:     d->sig = p->sig;
1.1       noro     7481:     return d;
                   7482: }
                   7483:
                   7484: DP ndvtodp(int mod,NDV p)
                   7485: {
                   7486:     MP m,m0;
                   7487:   DP d;
                   7488:     NMV t;
                   7489:     int i,len;
                   7490:
                   7491:     if ( !p ) return 0;
                   7492:     m0 = 0;
                   7493:     len = p->len;
                   7494:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   7495:         NEXTMP(m0,m);
                   7496:       m->dl = ndltodl(nd_nvar,DL(t));
                   7497:       m->c = (Obj)ndctop(mod,t->c);
                   7498:     }
                   7499:     NEXT(m) = 0;
                   7500:   MKDP(nd_nvar,m0,d);
                   7501:     SG(d) = SG(p);
                   7502:     return d;
                   7503: }
                   7504:
1.44      noro     7505: DPM sigtodpm(SIG s)
                   7506: {
                   7507:   DMM m;
                   7508:   DPM d;
                   7509:
                   7510:   NEWDMM(m);
                   7511:   m->c = (Obj)ONE;
                   7512:   m->dl = s->dl;
                   7513:   m->pos = s->pos+1;
                   7514:   m->next = 0;
                   7515:   MKDPM(nd_nvar,m,d);
                   7516:   SG(d) = s->dl->td;
                   7517:   return d;
                   7518: }
                   7519:
1.16      noro     7520: DPM ndvtodpm(int mod,NDV p)
                   7521: {
                   7522:   DMM m,m0;
                   7523:   DPM d;
                   7524:   NMV t;
                   7525:   int i,len;
                   7526:
                   7527:   if ( !p ) return 0;
                   7528:   m0 = 0;
                   7529:   len = p->len;
                   7530:   for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   7531:     NEXTDMM(m0,m);
                   7532:     m->dl = ndltodl(nd_nvar,DL(t));
                   7533:     m->c = (Obj)ndctop(mod,t->c);
                   7534:     m->pos = MPOS(DL(t));
                   7535:   }
                   7536:   NEXT(m) = 0;
                   7537:   MKDPM(nd_nvar,m0,d);
                   7538:   SG(d) = SG(p);
                   7539:   return d;
                   7540: }
                   7541:
                   7542:
1.1       noro     7543: DP ndtodp(int mod,ND p)
                   7544: {
                   7545:     MP m,m0;
                   7546:   DP d;
                   7547:     NM t;
                   7548:     int i,len;
                   7549:
                   7550:     if ( !p ) return 0;
                   7551:     m0 = 0;
                   7552:     len = p->len;
                   7553:     for ( t = BDY(p); t; t = NEXT(t) ) {
                   7554:         NEXTMP(m0,m);
                   7555:       m->dl = ndltodl(nd_nvar,DL(t));
                   7556:       m->c = (Obj)ndctop(mod,t->c);
                   7557:     }
                   7558:     NEXT(m) = 0;
                   7559:   MKDP(nd_nvar,m0,d);
                   7560:     SG(d) = SG(p);
                   7561:     return d;
                   7562: }
                   7563:
                   7564: void ndv_print(NDV p)
                   7565: {
                   7566:     NMV m;
                   7567:     int i,len;
                   7568:
                   7569:     if ( !p ) printf("0\n");
                   7570:     else {
                   7571:         len = LEN(p);
                   7572:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   7573:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     7574:             else printf("+%ld*",CM(m));
1.1       noro     7575:             ndl_print(DL(m));
                   7576:         }
                   7577:         printf("\n");
                   7578:     }
                   7579: }
                   7580:
                   7581: void ndv_print_q(NDV p)
                   7582: {
                   7583:     NMV m;
                   7584:     int i,len;
                   7585:
                   7586:     if ( !p ) printf("0\n");
                   7587:     else {
                   7588:         len = LEN(p);
                   7589:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   7590:             printf("+");
1.6       noro     7591:             printexpr(CO,(Obj)CZ(m));
1.1       noro     7592:             printf("*");
                   7593:             ndl_print(DL(m));
                   7594:         }
                   7595:         printf("\n");
                   7596:     }
                   7597: }
                   7598:
                   7599: NODE ndv_reducebase(NODE x,int *perm)
                   7600: {
                   7601:     int len,i,j;
                   7602:     NDVI w;
                   7603:     NODE t,t0;
                   7604:
1.41      noro     7605:     if ( nd_norb ) return x;
1.1       noro     7606:     len = length(x);
                   7607:     w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
                   7608:     for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
                   7609:         w[i].p = BDY(t); w[i].i = perm[i];
                   7610:     }
                   7611:     for ( i = 0; i < len; i++ ) {
                   7612:         for ( j = 0; j < i; j++ ) {
                   7613:             if ( w[i].p && w[j].p ) {
                   7614:                 if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
                   7615:                 else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
                   7616:             }
                   7617:         }
                   7618:     }
                   7619:     for ( i = j = 0, t0 = 0; i < len; i++ ) {
                   7620:         if ( w[i].p ) {
                   7621:             NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
                   7622:             perm[j++] = w[i].i;
                   7623:         }
                   7624:     }
                   7625:     NEXT(t) = 0; x = t0;
                   7626:     return x;
                   7627: }
                   7628:
                   7629: /* XXX incomplete */
                   7630:
1.21      noro     7631: extern DMMstack dmm_stack;
                   7632: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16      noro     7633:
1.1       noro     7634: void nd_init_ord(struct order_spec *ord)
                   7635: {
                   7636:   nd_module = (ord->id >= 256);
                   7637:   if ( nd_module ) {
                   7638:     nd_dcomp = -1;
1.21      noro     7639:     nd_module_ordtype = ord->module_ordtype;
1.1       noro     7640:     nd_pot_nelim = ord->pot_nelim;
                   7641:     nd_poly_weight_len = ord->nv;
                   7642:     nd_poly_weight = ord->top_weight;
                   7643:     nd_module_rank = ord->module_rank;
                   7644:     nd_module_weight = ord->module_top_weight;
                   7645:   }
                   7646:   nd_matrix = 0;
                   7647:   nd_matrix_len = 0;
                   7648:     switch ( ord->id ) {
                   7649:         case 0:
                   7650:             switch ( ord->ord.simple ) {
                   7651:                 case 0:
                   7652:                     nd_dcomp = 1;
                   7653:                     nd_isrlex = 1;
                   7654:                     break;
                   7655:                 case 1:
                   7656:                     nd_dcomp = 1;
                   7657:                     nd_isrlex = 0;
                   7658:                     break;
                   7659:                 case 2:
                   7660:                     nd_dcomp = 0;
                   7661:                     nd_isrlex = 0;
                   7662:                     ndl_compare_function = ndl_lex_compare;
                   7663:                     break;
                   7664:                 case 11:
                   7665:                     /* XXX */
                   7666:                     nd_dcomp = 0;
                   7667:                     nd_isrlex = 1;
                   7668:                     ndl_compare_function = ndl_ww_lex_compare;
                   7669:                     break;
                   7670:                 default:
                   7671:                     error("nd_gr : unsupported order");
                   7672:             }
                   7673:             break;
                   7674:         case 1:
                   7675:             /* block order */
                   7676:             /* XXX */
                   7677:             nd_dcomp = -1;
                   7678:             nd_isrlex = 0;
                   7679:             ndl_compare_function = ndl_block_compare;
                   7680:             break;
                   7681:         case 2:
                   7682:             /* matrix order */
                   7683:             /* XXX */
                   7684:             nd_dcomp = -1;
                   7685:             nd_isrlex = 0;
                   7686:             nd_matrix_len = ord->ord.matrix.row;
                   7687:             nd_matrix = ord->ord.matrix.matrix;
                   7688:             ndl_compare_function = ndl_matrix_compare;
                   7689:             break;
                   7690:         case 3:
                   7691:             /* composite order */
                   7692:             nd_dcomp = -1;
                   7693:             nd_isrlex = 0;
                   7694:             nd_worb_len = ord->ord.composite.length;
                   7695:             nd_worb = ord->ord.composite.w_or_b;
                   7696:             ndl_compare_function = ndl_composite_compare;
                   7697:             break;
                   7698:
                   7699:         /* module order */
                   7700:         case 256:
                   7701:             switch ( ord->ord.simple ) {
                   7702:                 case 0:
1.21      noro     7703:                     nd_dcomp = 0;
1.1       noro     7704:                     nd_isrlex = 1;
1.21      noro     7705:                     ndl_compare_function = ndl_module_glex_compare;
1.1       noro     7706:                     break;
                   7707:                 case 1:
1.21      noro     7708:                     nd_dcomp = 0;
1.1       noro     7709:                     nd_isrlex = 0;
                   7710:                     ndl_compare_function = ndl_module_glex_compare;
                   7711:                     break;
                   7712:                 case 2:
1.21      noro     7713:                     nd_dcomp = 0;
1.1       noro     7714:                     nd_isrlex = 0;
1.21      noro     7715:                     ndl_compare_function = ndl_module_compare;
                   7716:                     ndl_base_compare_function = ndl_lex_compare;
1.1       noro     7717:                     break;
                   7718:                 default:
1.21      noro     7719:                     error("nd_init_ord : unsupported order");
1.1       noro     7720:             }
                   7721:             break;
                   7722:         case 257:
                   7723:             /* block order */
                   7724:             nd_isrlex = 0;
1.21      noro     7725:             ndl_compare_function = ndl_module_compare;
                   7726:             ndl_base_compare_function = ndl_block_compare;
1.1       noro     7727:             break;
                   7728:         case 258:
                   7729:             /* matrix order */
                   7730:             nd_isrlex = 0;
                   7731:             nd_matrix_len = ord->ord.matrix.row;
                   7732:             nd_matrix = ord->ord.matrix.matrix;
1.21      noro     7733:             ndl_compare_function = ndl_module_compare;
                   7734:             ndl_base_compare_function = ndl_matrix_compare;
1.1       noro     7735:             break;
                   7736:         case 259:
                   7737:             /* composite order */
                   7738:             nd_isrlex = 0;
                   7739:             nd_worb_len = ord->ord.composite.length;
                   7740:             nd_worb = ord->ord.composite.w_or_b;
1.21      noro     7741:             ndl_compare_function = ndl_module_compare;
                   7742:             ndl_base_compare_function = ndl_composite_compare;
                   7743:             break;
                   7744:         case 300:
                   7745:             /* schreyer order */
                   7746:             if ( ord->base->id != 256 )
                   7747:                error("nd_init_ord : unsupported base order");
                   7748:             ndl_compare_function = ndl_module_schreyer_compare;
                   7749:             dmm_stack = ord->dmmstack;
                   7750:             switch ( ord->base->ord.simple ) {
                   7751:                 case 0:
                   7752:                     nd_isrlex = 1;
                   7753:                     ndl_base_compare_function = ndl_glex_compare;
                   7754:                     dl_base_compare_function = cmpdl_revgradlex;
                   7755:                     break;
                   7756:                 case 1:
                   7757:                     nd_isrlex = 0;
                   7758:                     ndl_base_compare_function = ndl_glex_compare;
                   7759:                     dl_base_compare_function = cmpdl_gradlex;
                   7760:                     break;
                   7761:                 case 2:
                   7762:                     nd_isrlex = 0;
                   7763:                     ndl_base_compare_function = ndl_lex_compare;
                   7764:                     dl_base_compare_function = cmpdl_lex;
                   7765:                     break;
                   7766:                 default:
                   7767:                     error("nd_init_ord : unsupported order");
                   7768:             }
1.1       noro     7769:             break;
                   7770:     }
                   7771:     nd_ord = ord;
                   7772: }
                   7773:
                   7774: BlockMask nd_create_blockmask(struct order_spec *ord)
                   7775: {
                   7776:     int n,i,j,s,l;
                   7777:     UINT *t;
                   7778:     BlockMask bm;
                   7779:
                   7780:     /* we only create mask table for block order */
                   7781:     if ( ord->id != 1 && ord->id != 257 )
                   7782:         return 0;
                   7783:     n = ord->ord.block.length;
                   7784:     bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   7785:     bm->n = n;
                   7786:     bm->order_pair = ord->ord.block.order_pair;
                   7787:     bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
                   7788:     for ( i = 0, s = 0; i < n; i++ ) {
                   7789:         bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
                   7790:         for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   7791:         l = bm->order_pair[i].length;
                   7792:         for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   7793:     }
                   7794:     return bm;
                   7795: }
                   7796:
                   7797: EPOS nd_create_epos(struct order_spec *ord)
                   7798: {
                   7799:     int i,j,l,s,ord_l,ord_o;
                   7800:     EPOS epos;
                   7801:     struct order_pair *op;
                   7802:
                   7803:     epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   7804:     switch ( ord->id ) {
1.21      noro     7805:         case 0: case 256: case 300:
1.1       noro     7806:             if ( nd_isrlex ) {
                   7807:                 for ( i = 0; i < nd_nvar; i++ ) {
                   7808:                     epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   7809:                     epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   7810:                 }
                   7811:             } else {
                   7812:                 for ( i = 0; i < nd_nvar; i++ ) {
                   7813:                     epos[i].i = nd_exporigin + i/nd_epw;
                   7814:                     epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   7815:                 }
                   7816:             }
                   7817:             break;
                   7818:         case 1: case 257:
                   7819:             /* block order */
                   7820:             l = ord->ord.block.length;
                   7821:             op = ord->ord.block.order_pair;
                   7822:             for ( j = 0, s = 0; j < l; j++ ) {
                   7823:                 ord_o = op[j].order;
                   7824:                 ord_l = op[j].length;
                   7825:                 if ( !ord_o )
                   7826:                     for ( i = 0; i < ord_l; i++ ) {
                   7827:                         epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   7828:                         epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   7829:                     }
                   7830:                 else
                   7831:                     for ( i = 0; i < ord_l; i++ ) {
                   7832:                         epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   7833:                         epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   7834:                     }
                   7835:                 s += ord_l;
                   7836:             }
                   7837:             break;
                   7838:         case 2:
                   7839:             /* matrix order */
                   7840:         case 3:
                   7841:             /* composite order */
                   7842:         default:
                   7843:             for ( i = 0; i < nd_nvar; i++ ) {
                   7844:                 epos[i].i = nd_exporigin + i/nd_epw;
                   7845:                 epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   7846:             }
                   7847:             break;
                   7848:     }
                   7849:     return epos;
                   7850: }
                   7851:
                   7852: /* external interface */
                   7853:
                   7854: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
                   7855: {
                   7856:     NODE t,in0,in;
                   7857:     ND ndf,nf;
                   7858:     NDV ndvf;
                   7859:     VL vv,tv;
                   7860:     int stat,nvar,max,mrank;
                   7861:     union oNDC dn;
                   7862:     Q cont;
                   7863:     P pp;
                   7864:     LIST ppl;
                   7865:
                   7866:     if ( !f ) {
                   7867:         *rp = 0;
                   7868:         return;
                   7869:     }
                   7870:     pltovl(v,&vv);
                   7871:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   7872:
                   7873:     /* max=65536 implies nd_bpe=32 */
                   7874:     max = 65536;
                   7875:
                   7876:   nd_module = 0;
                   7877:   /* nd_module will be set if ord is a module ordering */
                   7878:     nd_init_ord(ord);
                   7879:     nd_setup_parameters(nvar,max);
                   7880:     if ( nd_module && OID(f) != O_LIST )
                   7881:         error("nd_nf_p : the first argument must be a list");
                   7882:   if ( nd_module ) mrank = length(BDY((LIST)f));
                   7883:     /* conversion to ndv */
                   7884:     for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   7885:         NEXTNODE(in0,in);
                   7886:         if ( nd_module ) {
                   7887:           if ( !BDY(t) || OID(BDY(t)) != O_LIST
                   7888:                || length(BDY((LIST)BDY(t))) != mrank )
                   7889:               error("nd_nf_p : inconsistent basis element");
                   7890:           if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
                   7891:           else ppl = (LIST)BDY(t);
                   7892:           BDY(in) = (pointer)pltondv(CO,vv,ppl);
                   7893:         } else {
                   7894:           if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
                   7895:           else pp = (P)BDY(t);
                   7896:           BDY(in) = (pointer)ptondv(CO,vv,pp);
                   7897:         }
                   7898:         if ( m ) ndv_mod(m,(NDV)BDY(in));
                   7899:     }
                   7900:     if ( in0 ) NEXT(in) = 0;
                   7901:
                   7902:     if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
                   7903:     else ndvf = ptondv(CO,vv,(P)f);
                   7904:     if ( m ) ndv_mod(m,ndvf);
                   7905:     ndf = (pointer)ndvtond(m,ndvf);
                   7906:
                   7907:     /* dont sort, dont removecont */
1.24      noro     7908:     ndv_setup(m,0,in0,1,1,0);
1.1       noro     7909:     nd_scale=2;
1.6       noro     7910:     stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1       noro     7911:     if ( !stat )
                   7912:         error("nd_nf_p : exponent too large");
                   7913:     if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
                   7914:     else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
                   7915: }
                   7916:
                   7917: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   7918: {
                   7919:     NM m;
                   7920:     UINT *t,*s;
                   7921:     int i;
                   7922:
                   7923:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   7924:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7925:         t = DL(m);
                   7926:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   7927:         r[i] = CM(m);
                   7928:     }
                   7929:     for ( i = 0; !r[i]; i++ );
                   7930:     return i;
                   7931: }
                   7932:
                   7933: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
                   7934: {
                   7935:     NM m;
                   7936:     UINT *t,*s;
                   7937:     int i;
                   7938:
                   7939:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   7940:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7941:         t = DL(m);
                   7942:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     7943:         r[i] = CZ(m);
1.1       noro     7944:     }
                   7945:     for ( i = 0; !r[i]; i++ );
                   7946:     return i;
                   7947: }
                   7948:
                   7949: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
                   7950: {
                   7951:     NM m;
                   7952:     UINT *t,*s;
                   7953:     int i;
                   7954:
                   7955:     for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
                   7956:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7957:         t = DL(m);
                   7958:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   7959:         mpz_set(r[i],BDY(CZ(m)));
                   7960:     }
                   7961:     for ( i = 0; !mpz_sgn(r[i]); i++ );
                   7962:     return i;
                   7963: }
                   7964:
                   7965: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
                   7966: {
                   7967:     NM m;
                   7968:     unsigned long *v;
                   7969:     int i,j,h,size;
                   7970:   UINT *s,*t;
                   7971:
                   7972:   size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
                   7973:     v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
                   7974:     bzero(v,size);
                   7975:     for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
                   7976:     t = DL(m);
                   7977:     h = ndl_hash_value(t);
                   7978:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   7979:       v[i/BLEN] |= 1L <<(i%BLEN);
                   7980:     }
                   7981:     return v;
                   7982: }
                   7983:
                   7984: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
                   7985: {
                   7986:     NMV mr;
                   7987:     UINT *d,*t,*s;
                   7988:     int i,j,len,h,head;
                   7989:
                   7990:     d = DL(m);
                   7991:     len = LEN(p);
                   7992:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   7993:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   7994:         ndl_add(d,DL(mr),t);
                   7995:     h = ndl_hash_value(t);
                   7996:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   7997:     if ( j == 0 ) head = i;
                   7998:       v[i/BLEN] |= 1L <<(i%BLEN);
                   7999:     }
                   8000:     return head;
                   8001: }
                   8002:
                   8003: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   8004: {
                   8005:     NM m;
                   8006:     NMV mr;
                   8007:     UINT *d,*t,*s;
                   8008:     NDV p;
                   8009:     int i,j,len;
                   8010:     Z *r;
                   8011:
                   8012:     m = pair->mul;
                   8013:     d = DL(m);
                   8014:     p = nd_ps[pair->index];
                   8015:     len = LEN(p);
                   8016:     r = (Z *)CALLOC(n,sizeof(Q));
                   8017:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   8018:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   8019:         ndl_add(d,DL(mr),t);
                   8020:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     8021:         r[i] = CZ(mr);
1.1       noro     8022:     }
                   8023:     return r;
                   8024: }
                   8025:
1.11      noro     8026: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1       noro     8027: {
                   8028:     NM m;
                   8029:     NMV mr;
1.11      noro     8030:     UINT *d,*t,*s,*u;
1.1       noro     8031:     NDV p;
                   8032:     unsigned char *ivc;
                   8033:     unsigned short *ivs;
                   8034:     UINT *v,*ivi,*s0v;
1.11      noro     8035:     int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1       noro     8036:     IndArray r;
                   8037:
                   8038:     m = pair->mul;
                   8039:     d = DL(m);
                   8040:     if ( trace )
                   8041:       p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
                   8042:     else
                   8043:       p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
                   8044:
                   8045:     len = LEN(p);
                   8046:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   8047:     v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11      noro     8048:     for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
                   8049:       ndl_add(d,DL(mr),t);
                   8050:       st = prev;
                   8051:       ed = n;
                   8052:       while ( ed > st ) {
                   8053:         md = (st+ed)/2;
                   8054:         u = s0+md*nd_wpd;
                   8055:         c = DL_COMPARE(u,t);
                   8056:         if ( c == 0 ) break;
                   8057:         else if ( c > 0 ) st = md;
                   8058:         else ed = md;
                   8059:       }
                   8060:       prev = v[j] = md;
1.1       noro     8061:     }
                   8062:     r = (IndArray)MALLOC(sizeof(struct oIndArray));
                   8063:     r->head = v[0];
                   8064:     diff = 0;
                   8065:     for ( i = 1; i < len; i++ ) {
                   8066:         cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   8067:     }
                   8068:     if ( diff < 256 ) {
                   8069:         r->width = 1;
                   8070:         ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   8071:         r->index.c = ivc;
                   8072:         for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   8073:     } else if ( diff < 65536 ) {
                   8074:         r->width = 2;
                   8075:         ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   8076:         r->index.s = ivs;
                   8077:         for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   8078:     } else {
                   8079:         r->width = 4;
                   8080:         ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   8081:         r->index.i = ivi;
                   8082:         for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
                   8083:     }
                   8084:     return r;
                   8085: }
                   8086:
                   8087: int compress_array(Z *svect,Z *cvect,int n)
                   8088: {
                   8089:     int i,j;
                   8090:
                   8091:     for ( i = j = 0; i < n; i++ )
                   8092:         if ( svect[i] ) cvect[j++] = svect[i];
                   8093:     return j;
                   8094: }
                   8095:
                   8096: void expand_array(Z *svect,Z *cvect,int n)
                   8097: {
                   8098:     int i,j;
                   8099:
                   8100:     for ( i = j = 0; j < n;  i++  )
                   8101:         if ( svect[i] ) svect[i] = cvect[j++];
                   8102: }
                   8103:
1.8       noro     8104: #if 0
1.1       noro     8105: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8106: {
                   8107:     int i,j,k,len,pos,prev,nz;
                   8108:     Z cs,mcs,c1,c2,cr,gcd,t;
                   8109:     IndArray ivect;
                   8110:     unsigned char *ivc;
                   8111:     unsigned short *ivs;
                   8112:     unsigned int *ivi;
                   8113:     NDV redv;
                   8114:     NMV mr;
                   8115:     NODE rp;
                   8116:     int maxrs;
                   8117:     double hmag;
                   8118:     Z *cvect;
1.3       noro     8119:     int l;
1.1       noro     8120:
                   8121:     maxrs = 0;
                   8122:     for ( i = 0; i < col && !svect[i]; i++ );
                   8123:     if ( i == col ) return maxrs;
                   8124:     hmag = p_mag((P)svect[i])*nd_scale;
                   8125:     cvect = (Z *)MALLOC(col*sizeof(Q));
                   8126:     for ( i = 0; i < nred; i++ ) {
                   8127:         ivect = imat[i];
                   8128:         k = ivect->head;
                   8129:         if ( svect[k] ) {
                   8130:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8131:             redv = nd_demand?ndv_load(rp0[i]->index)
                   8132:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   8133:             len = LEN(redv); mr = BDY(redv);
1.6       noro     8134:             igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1       noro     8135:             chsgnz(cs,&mcs);
                   8136:             if ( !UNIQ(cr) ) {
                   8137:                 for ( j = 0; j < col; j++ ) {
                   8138:                     mulz(svect[j],cr,&c1); svect[j] = c1;
                   8139:                 }
                   8140:             }
                   8141:             svect[k] = 0; prev = k;
                   8142:             switch ( ivect->width ) {
                   8143:                 case 1:
                   8144:                     ivc = ivect->index.c;
                   8145:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8146:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     8147:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     8148:                     }
                   8149:                     break;
                   8150:                 case 2:
                   8151:                     ivs = ivect->index.s;
                   8152:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8153:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     8154:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     8155:                     }
                   8156:                     break;
                   8157:                 case 4:
                   8158:                     ivi = ivect->index.i;
                   8159:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8160:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     8161:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     8162:                     }
                   8163:                     break;
                   8164:             }
                   8165:             for ( j = k+1; j < col && !svect[j]; j++ );
                   8166:             if ( j == col ) break;
                   8167:             if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
                   8168:                 nz = compress_array(svect,cvect,col);
                   8169:                 removecont_array((P *)cvect,nz,1);
                   8170:                 expand_array(svect,cvect,nz);
                   8171:                 hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   8172:             }
                   8173:         }
                   8174:     }
                   8175:     nz = compress_array(svect,cvect,col);
                   8176:     removecont_array((P *)cvect,nz,1);
                   8177:     expand_array(svect,cvect,nz);
                   8178:     if ( DP_Print ) {
                   8179:         fprintf(asir_out,"-"); fflush(asir_out);
                   8180:     }
                   8181:     return maxrs;
                   8182: }
1.4       noro     8183: #else
1.9       noro     8184:
1.4       noro     8185: /* direct mpz version */
                   8186: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8187: {
                   8188:     int i,j,k,len,pos,prev;
                   8189:     mpz_t cs,cr,gcd;
                   8190:     IndArray ivect;
                   8191:     unsigned char *ivc;
                   8192:     unsigned short *ivs;
                   8193:     unsigned int *ivi;
                   8194:     NDV redv;
                   8195:     NMV mr;
                   8196:     NODE rp;
                   8197:     int maxrs;
                   8198:     double hmag;
                   8199:     int l;
1.13      noro     8200:     static mpz_t *svect;
                   8201:     static int svect_len=0;
1.4       noro     8202:
                   8203:     maxrs = 0;
                   8204:     for ( i = 0; i < col && !svect0[i]; i++ );
                   8205:     if ( i == col ) return maxrs;
                   8206:     hmag = p_mag((P)svect0[i])*nd_scale;
1.13      noro     8207:     if ( col > svect_len ) {
                   8208:       svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   8209:       svect_len = col;
                   8210:     }
1.4       noro     8211:     for ( i = 0; i < col; i++ ) {
                   8212:       mpz_init(svect[i]);
                   8213:       if ( svect0[i] )
                   8214:         mpz_set(svect[i],BDY(svect0[i]));
                   8215:       else
                   8216:         mpz_set_ui(svect[i],0);
                   8217:     }
                   8218:     mpz_init(gcd); mpz_init(cs); mpz_init(cr);
                   8219:     for ( i = 0; i < nred; i++ ) {
                   8220:         ivect = imat[i];
                   8221:         k = ivect->head;
                   8222:         if ( mpz_sgn(svect[k]) ) {
                   8223:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8224:             redv = nd_demand?ndv_load(rp0[i]->index)
                   8225:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   8226:             len = LEN(redv); mr = BDY(redv);
1.6       noro     8227:             mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4       noro     8228:             mpz_div(cs,svect[k],gcd);
1.6       noro     8229:             mpz_div(cr,BDY(CZ(mr)),gcd);
1.4       noro     8230:             mpz_neg(cs,cs);
1.9       noro     8231:             if ( MUNIMPZ(cr) )
                   8232:               for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
                   8233:             else if ( !UNIMPZ(cr) )
                   8234:               for ( j = 0; j < col; j++ ) {
                   8235:                 if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
                   8236:               }
1.4       noro     8237:             mpz_set_ui(svect[k],0);
                   8238:             prev = k;
                   8239:             switch ( ivect->width ) {
                   8240:                 case 1:
                   8241:                     ivc = ivect->index.c;
                   8242:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8243:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     8244:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     8245:                     }
                   8246:                     break;
                   8247:                 case 2:
                   8248:                     ivs = ivect->index.s;
                   8249:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8250:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     8251:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     8252:                     }
                   8253:                     break;
                   8254:                 case 4:
                   8255:                     ivi = ivect->index.i;
                   8256:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8257:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     8258:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     8259:                     }
                   8260:                     break;
                   8261:             }
                   8262:             for ( j = k+1; j < col && !svect[j]; j++ );
                   8263:             if ( j == col ) break;
                   8264:             if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
                   8265:                 mpz_removecont_array(svect,col);
                   8266:                 hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
                   8267:             }
                   8268:         }
                   8269:     }
                   8270:     mpz_removecont_array(svect,col);
                   8271:     if ( DP_Print ) {
                   8272:         fprintf(asir_out,"-"); fflush(asir_out);
                   8273:     }
                   8274:     for ( i = 0; i < col; i++ )
                   8275:       if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
                   8276:       else svect0[i] = 0;
                   8277:     return maxrs;
                   8278: }
                   8279: #endif
1.1       noro     8280:
1.40      noro     8281: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred,SIG sig)
1.1       noro     8282: {
                   8283:     int i,j,k,len,pos,prev;
                   8284:     UINT c,c1,c2,c3,up,lo,dmy;
                   8285:     IndArray ivect;
                   8286:     unsigned char *ivc;
                   8287:     unsigned short *ivs;
                   8288:     unsigned int *ivi;
                   8289:     NDV redv;
                   8290:     NMV mr;
                   8291:     NODE rp;
                   8292:     int maxrs;
                   8293:
                   8294:     maxrs = 0;
                   8295:     for ( i = 0; i < nred; i++ ) {
                   8296:         ivect = imat[i];
                   8297:         k = ivect->head; svect[k] %= m;
1.40      noro     8298:         if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.1       noro     8299:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8300:             c = m-c; redv = nd_ps[rp0[i]->index];
                   8301:             len = LEN(redv); mr = BDY(redv);
                   8302:             svect[k] = 0; prev = k;
                   8303:             switch ( ivect->width ) {
                   8304:                 case 1:
                   8305:                     ivc = ivect->index.c;
                   8306:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8307:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.40      noro     8308:                         if ( c1 ) {
                   8309:                           c2 = svect[pos];
1.1       noro     8310:                           DMA(c1,c,c2,up,lo);
                   8311:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   8312:                           } else svect[pos] = lo;
1.40      noro     8313:                         }
1.1       noro     8314:                     }
                   8315:                     break;
                   8316:                 case 2:
                   8317:                     ivs = ivect->index.s;
                   8318:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8319:                         pos = prev+ivs[j]; c1 = CM(mr);
                   8320:                         prev = pos;
1.40      noro     8321:                         if ( c1 ) {
                   8322:                           c2 = svect[pos];
1.1       noro     8323:                           DMA(c1,c,c2,up,lo);
                   8324:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   8325:                           } else svect[pos] = lo;
1.40      noro     8326:                         }
1.1       noro     8327:                     }
                   8328:                     break;
                   8329:                 case 4:
                   8330:                     ivi = ivect->index.i;
                   8331:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8332:                         pos = prev+ivi[j]; c1 = CM(mr);
                   8333:                         prev = pos;
1.40      noro     8334:                         if ( c1 ) {
                   8335:                           c2 = svect[pos];
1.1       noro     8336:                           DMA(c1,c,c2,up,lo);
                   8337:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   8338:                           } else svect[pos] = lo;
1.40      noro     8339:                         }
1.1       noro     8340:                     }
                   8341:                     break;
                   8342:             }
                   8343:         }
                   8344:     }
                   8345:     for ( i = 0; i < col; i++ )
                   8346:         if ( svect[i] >= (UINT)m ) svect[i] %= m;
                   8347:     return maxrs;
                   8348: }
                   8349:
                   8350: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8351: {
                   8352:     int i,j,k,len,pos,prev;
                   8353:     UINT c,c1,c2,c3,up,lo,dmy;
                   8354:     IndArray ivect;
                   8355:     unsigned char *ivc;
                   8356:     unsigned short *ivs;
                   8357:     unsigned int *ivi;
                   8358:     NDV redv;
                   8359:     NMV mr;
                   8360:     NODE rp;
                   8361:     int maxrs;
                   8362:
                   8363:     maxrs = 0;
                   8364:     for ( i = 0; i < nred; i++ ) {
                   8365:         ivect = imat[i];
                   8366:         k = ivect->head;
                   8367:         if ( (c = svect[k]) != 0 ) {
                   8368:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8369:             c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
                   8370:             len = LEN(redv); mr = BDY(redv);
                   8371:             svect[k] = 0; prev = k;
                   8372:             switch ( ivect->width ) {
                   8373:                 case 1:
                   8374:                     ivc = ivect->index.c;
                   8375:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8376:                         pos = prev+ivc[j]; prev = pos;
                   8377:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   8378:                     }
                   8379:                     break;
                   8380:                 case 2:
                   8381:                     ivs = ivect->index.s;
                   8382:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8383:                         pos = prev+ivs[j]; prev = pos;
                   8384:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   8385:                     }
                   8386:                     break;
                   8387:                 case 4:
                   8388:                     ivi = ivect->index.i;
                   8389:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8390:                         pos = prev+ivi[j]; prev = pos;
                   8391:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   8392:                     }
                   8393:                     break;
                   8394:             }
                   8395:         }
                   8396:     }
                   8397:     return maxrs;
                   8398: }
                   8399:
                   8400: ND nd_add_lf(ND p1,ND p2)
                   8401: {
                   8402:     int n,c,can;
                   8403:     ND r;
                   8404:     NM m1,m2,mr0,mr,s;
                   8405:     Z t;
                   8406:
                   8407:     if ( !p1 ) return p2;
                   8408:     else if ( !p2 ) return p1;
                   8409:     else {
                   8410:         can = 0;
                   8411:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   8412:             c = DL_COMPARE(DL(m1),DL(m2));
                   8413:             switch ( c ) {
                   8414:                 case 0:
                   8415:                     addlf(CZ(m1),CZ(m2),&t);
                   8416:                     s = m1; m1 = NEXT(m1);
                   8417:                     if ( t ) {
                   8418:                         can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
                   8419:                     } else {
                   8420:                         can += 2; FREENM(s);
                   8421:                     }
                   8422:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   8423:                     break;
                   8424:                 case 1:
                   8425:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   8426:                     break;
                   8427:                 case -1:
                   8428:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   8429:                     break;
                   8430:             }
                   8431:         }
                   8432:         if ( !mr0 )
                   8433:             if ( m1 ) mr0 = m1;
                   8434:             else if ( m2 ) mr0 = m2;
                   8435:             else return 0;
                   8436:         else if ( m1 ) NEXT(mr) = m1;
                   8437:         else if ( m2 ) NEXT(mr) = m2;
                   8438:         else NEXT(mr) = 0;
                   8439:         BDY(p1) = mr0;
                   8440:         SG(p1) = MAX(SG(p1),SG(p2));
                   8441:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   8442:         FREEND(p2);
                   8443:         return p1;
                   8444:     }
                   8445: }
                   8446:
                   8447: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8448: {
                   8449:     int i,j,k,len,pos,prev;
                   8450:     mpz_t c,mc,c1;
                   8451:     IndArray ivect;
                   8452:     unsigned char *ivc;
                   8453:     unsigned short *ivs;
                   8454:     unsigned int *ivi;
                   8455:     NDV redv;
                   8456:     NMV mr;
                   8457:     NODE rp;
                   8458:     int maxrs;
                   8459:
                   8460:     maxrs = 0;
                   8461:     lf_lazy = 1;
                   8462:     for ( i = 0; i < nred; i++ ) {
                   8463:         ivect = imat[i];
                   8464:         k = ivect->head;
                   8465:         mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
                   8466:         if ( mpz_sgn(svect[k]) ) {
                   8467:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8468:             mpz_neg(svect[k],svect[k]);
                   8469:             redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
                   8470:             len = LEN(redv); mr = BDY(redv);
                   8471:             prev = k;
                   8472:             switch ( ivect->width ) {
                   8473:                 case 1:
                   8474:                     ivc = ivect->index.c;
                   8475:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8476:                         pos = prev+ivc[j]; prev = pos;
                   8477:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   8478:                     }
                   8479:                     break;
                   8480:                 case 2:
                   8481:                     ivs = ivect->index.s;
                   8482:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8483:                         pos = prev+ivs[j]; prev = pos;
                   8484:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   8485:                     }
                   8486:                     break;
                   8487:                 case 4:
                   8488:                     ivi = ivect->index.i;
                   8489:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8490:                         pos = prev+ivi[j]; prev = pos;
                   8491:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   8492:                     }
                   8493:                     break;
                   8494:             }
                   8495:             mpz_set_ui(svect[k],0);
                   8496:         }
                   8497:     }
                   8498:     lf_lazy=0;
                   8499:     for ( i = 0; i < col; i++ ) {
                   8500:         mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
                   8501:     }
                   8502:     return maxrs;
                   8503: }
                   8504:
                   8505: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
                   8506: {
                   8507:     int i,j,k,l,rank,s;
                   8508:     mpz_t a,a1,inv;
                   8509:     mpz_t *t,*pivot,*pk;
                   8510:     mpz_t **mat;
                   8511:     struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
                   8512:     int size,size1;
                   8513:
                   8514:     mpz_init(inv);
                   8515:     mpz_init(a);
                   8516:     mat = (mpz_t **)mat0;
                   8517:         size = 0;
                   8518:     for ( rank = 0, j = 0; j < col; j++ ) {
                   8519:         for ( i = rank; i < row; i++ ) {
                   8520:             mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
                   8521:         }
                   8522:         for ( i = rank; i < row; i++ )
                   8523:             if ( mpz_sgn(mat[i][j]) )
                   8524:                 break;
                   8525:         if ( i == row ) {
                   8526:             colstat[j] = 0;
                   8527:             continue;
                   8528:         } else
                   8529:             colstat[j] = 1;
                   8530:         if ( i != rank ) {
                   8531:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   8532:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   8533:         }
                   8534:         pivot = mat[rank];
                   8535:         s = sugar[rank];
                   8536:         mpz_invert(inv,pivot[j],BDY(current_mod_lf));
                   8537:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   8538:             if ( mpz_sgn(*pk) ) {
                   8539:                 mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
                   8540:             }
                   8541:         for ( i = rank+1; i < row; i++ ) {
                   8542:             t = mat[i];
                   8543:             if ( mpz_sgn(t[j]) ) {
                   8544:                 sugar[i] = MAX(sugar[i],s);
                   8545:                 mpz_neg(a,t[j]);
                   8546:                 red_by_vect_lf(t+j,pivot+j,a,col-j);
                   8547:             }
                   8548:         }
                   8549:         rank++;
                   8550:     }
                   8551:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   8552:         if ( colstat[j] ) {
                   8553:             pivot = mat[l];
                   8554:             s = sugar[l];
                   8555:             for ( k = j; k < col; k++ )
                   8556:               mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
                   8557:             for ( i = 0; i < l; i++ ) {
                   8558:                 t = mat[i];
                   8559:                 if ( mpz_sgn(t[j]) ) {
                   8560:                     sugar[i] = MAX(sugar[i],s);
                   8561:                     mpz_neg(a,t[j]);
                   8562:                     red_by_vect_lf(t+j,pivot+j,a,col-j);
                   8563:                 }
                   8564:             }
                   8565:             l--;
                   8566:         }
                   8567:     for ( j = 0, l = 0; l < rank; j++ )
                   8568:         if ( colstat[j] ) {
                   8569:             t = mat[l];
                   8570:             for ( k = j; k < col; k++ ) {
                   8571:                 mpz_mod(t[k],t[k],BDY(current_mod_lf));
                   8572:             }
                   8573:             l++;
                   8574:         }
                   8575:     return rank;
                   8576: }
                   8577:
                   8578:
                   8579: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   8580: {
                   8581:     int j,k,len;
                   8582:     UINT *p;
                   8583:     UINT c;
                   8584:     NDV r;
                   8585:     NMV mr0,mr;
                   8586:
                   8587:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   8588:     if ( !len ) return 0;
                   8589:     else {
                   8590:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8591: #if 0
                   8592:         ndv_alloc += nmv_adv*len;
                   8593: #endif
                   8594:         mr = mr0;
                   8595:         p = s0vect;
                   8596:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   8597:             if ( !rhead[j] ) {
                   8598:                 if ( (c = vect[k++]) != 0 ) {
                   8599:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   8600:                 }
                   8601:             }
                   8602:         MKNDV(nd_nvar,mr0,len,r);
                   8603:         return r;
                   8604:     }
                   8605: }
                   8606:
1.40      noro     8607: NDV vect_to_ndv_s(UINT *vect,int col,UINT *s0vect)
                   8608: {
                   8609:     int j,k,len;
                   8610:     UINT *p;
                   8611:     UINT c;
                   8612:     NDV r;
                   8613:     NMV mr0,mr;
                   8614:
                   8615:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   8616:     if ( !len ) return 0;
                   8617:     else {
                   8618:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8619:         mr = mr0;
                   8620:         p = s0vect;
                   8621:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   8622:           if ( (c = vect[k++]) != 0 ) {
                   8623:             ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   8624:           }
                   8625:         MKNDV(nd_nvar,mr0,len,r);
                   8626:         return r;
                   8627:     }
                   8628: }
                   8629:
1.1       noro     8630: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
                   8631: {
                   8632:     int j,k,len;
                   8633:     UINT *p;
                   8634:     NDV r;
                   8635:     NMV mr0,mr;
                   8636:
                   8637:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
                   8638:     if ( !len ) return 0;
                   8639:     else {
                   8640:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8641:         mr = mr0;
                   8642:         p = s0vect;
                   8643:         for ( j = 0; j < col; j++, p += nd_wpd )
                   8644:       if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
                   8645:             ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
                   8646:           }
                   8647:         MKNDV(nd_nvar,mr0,len,r);
                   8648:         return r;
                   8649:     }
                   8650: }
                   8651:
                   8652: /* for preprocessed vector */
                   8653:
                   8654: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   8655: {
1.6       noro     8656:   int j,k,len;
                   8657:   UINT *p;
                   8658:   Z c;
                   8659:   NDV r;
                   8660:   NMV mr0,mr;
1.1       noro     8661:
1.6       noro     8662:   for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   8663:   if ( !len ) return 0;
                   8664:   else {
                   8665:     mr0 = (NMV)MALLOC(nmv_adv*len);
1.1       noro     8666: #if 0
1.6       noro     8667:     ndv_alloc += nmv_adv*len;
1.1       noro     8668: #endif
1.6       noro     8669:     mr = mr0;
                   8670:     p = s0vect;
                   8671:     for ( j = k = 0; j < col; j++, p += nd_wpd ) {
                   8672:       if ( !rhead[j] ) {
                   8673:         if ( (c = vect[k++]) != 0 ) {
                   8674:           if ( !INT(c) )
                   8675:             error("vect_to_ndv_q : components must be integers");
                   8676:             ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
                   8677:         }
                   8678:       }
1.1       noro     8679:     }
1.6       noro     8680:     MKNDV(nd_nvar,mr0,len,r);
                   8681:     return r;
                   8682:   }
1.1       noro     8683: }
                   8684:
                   8685: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   8686: {
                   8687:     int j,k,len;
                   8688:     UINT *p;
                   8689:     mpz_t c;
                   8690:     NDV r;
                   8691:     NMV mr0,mr;
                   8692:
                   8693:     for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
                   8694:     if ( !len ) return 0;
                   8695:     else {
                   8696:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   8697: #if 0
                   8698:         ndv_alloc += nmv_adv*len;
                   8699: #endif
                   8700:         mr = mr0;
                   8701:         p = s0vect;
                   8702:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   8703:             if ( !rhead[j] ) {
                   8704:                 c[0] = vect[k++][0];
                   8705:                 if ( mpz_sgn(c) ) {
                   8706:                     ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
                   8707:                 }
                   8708:             }
                   8709:         MKNDV(nd_nvar,mr0,len,r);
                   8710:         return r;
                   8711:     }
                   8712: }
                   8713:
                   8714: /* for plain vector */
                   8715:
                   8716: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
                   8717: {
                   8718:     int j,k,len;
                   8719:     UINT *p;
                   8720:     Z c;
                   8721:     NDV r;
                   8722:     NMV mr0,mr;
                   8723:
                   8724:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   8725:     if ( !len ) return 0;
                   8726:     else {
                   8727:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   8728: #if 0
                   8729:         ndv_alloc += nmv_adv*len;
                   8730: #endif
                   8731:         mr = mr0;
                   8732:         p = s0vect;
                   8733:         for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   8734:             if ( (c = vect[k]) != 0 ) {
                   8735:                 if ( !INT(c) )
1.6       noro     8736:                     error("plain_vect_to_ndv_q : components must be integers");
                   8737:                 ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1       noro     8738:             }
                   8739:         MKNDV(nd_nvar,mr0,len,r);
                   8740:         return r;
                   8741:     }
                   8742: }
                   8743:
                   8744: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
                   8745: {
                   8746:     ND_pairs t;
                   8747:     NODE sp0,sp;
                   8748:     int stat;
                   8749:     ND spol;
                   8750:
                   8751:     for ( t = l; t; t = NEXT(t) ) {
                   8752:         stat = nd_sp(m,trace,t,&spol);
                   8753:         if ( !stat ) return 0;
                   8754:         if ( spol ) {
                   8755:             add_pbucket_symbolic(bucket,spol);
                   8756:         }
                   8757:     }
                   8758:     return 1;
                   8759: }
                   8760:
                   8761: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   8762: {
                   8763:     NODE rp0,rp;
                   8764:     NM mul,head,s0,s;
                   8765:     int index,col,i,sugar;
                   8766:     RHist h;
                   8767:     UINT *s0v,*p;
                   8768:     NM_ind_pair pair;
                   8769:     ND red;
                   8770:     NDV *ps;
                   8771:
                   8772:     s0 = 0; rp0 = 0; col = 0;
                   8773:   if ( nd_demand )
                   8774:       ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   8775:   else
                   8776:       ps = trace?nd_ps_trace:nd_ps;
                   8777:     while ( 1 ) {
                   8778:         head = remove_head_pbucket_symbolic(bucket);
                   8779:         if ( !head ) break;
                   8780:         if ( !s0 ) s0 = head;
                   8781:         else NEXT(s) = head;
                   8782:         s = head;
                   8783:         index = ndl_find_reducer(DL(head));
                   8784:         if ( index >= 0 ) {
                   8785:             h = nd_psh[index];
                   8786:             NEWNM(mul);
                   8787:             ndl_sub(DL(head),DL(h),DL(mul));
                   8788:             if ( ndl_check_bound2(index,DL(mul)) )
                   8789:                 return 0;
                   8790:             sugar = TD(DL(mul))+SG(ps[index]);
1.28      noro     8791:             MKNM_ind_pair(pair,mul,index,sugar,0);
1.1       noro     8792:             red = ndv_mul_nm_symbolic(mul,ps[index]);
                   8793:             add_pbucket_symbolic(bucket,nd_remove_head(red));
                   8794:             NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   8795:         }
                   8796:         col++;
                   8797:     }
                   8798:     if ( rp0 ) NEXT(rp) = 0;
                   8799:     NEXT(s) = 0;
                   8800:     s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   8801:     for ( i = 0, p = s0v, s = s0; i < col;
                   8802:         i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   8803:     *s0vect = s0v;
                   8804:     *r = rp0;
                   8805:     return col;
                   8806: }
                   8807:
                   8808: void print_ndp(ND_pairs l)
                   8809: {
                   8810:   ND_pairs t;
                   8811:
                   8812:   for ( t = l; t; t = NEXT(t) )
                   8813:     printf("[%d,%d] ",t->i1,t->i2);
                   8814:   printf("\n");
                   8815: }
                   8816:
                   8817: NODE nd_f4(int m,int checkonly,int **indp)
                   8818: {
                   8819:     int i,nh,stat,index,f4red;
                   8820:     NODE r,g,tn0,tn,node;
                   8821:     ND_pairs d,l,t,ll0,ll,lh;
                   8822:   LIST l0,l1;
                   8823:     ND spol,red;
                   8824:     NDV nf,redv;
                   8825:     NM s0,s;
                   8826:     NODE rp0,srp0,nflist,nzlist,nzlist_t;
                   8827:     int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
                   8828:     UINT c;
                   8829:     UINT **spmat;
                   8830:     UINT *s0vect,*svect,*p,*v;
                   8831:     int *colstat;
                   8832:     IndArray *imat;
                   8833:     int *rhead;
                   8834:     int spcol,sprow;
                   8835:     int sugar,sugarh;
                   8836:     PGeoBucket bucket;
                   8837:     struct oEGT eg0,eg1,eg_f4;
                   8838:     Z i1,i2,sugarq;
1.12      noro     8839:
                   8840:     init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1       noro     8841: #if 0
                   8842:     ndv_alloc = 0;
                   8843: #endif
1.11      noro     8844:     Nf4_red=0;
1.1       noro     8845:     g = 0; d = 0;
                   8846:     for ( i = 0; i < nd_psn; i++ ) {
                   8847:         d = update_pairs(d,g,i,0);
                   8848:         g = update_base(g,i);
                   8849:     }
                   8850:   nzlist = 0;
                   8851:     nzlist_t = nd_nzlist;
                   8852:     f4red = 1;
                   8853:     nd_last_nonzero = 0;
                   8854:     while ( d ) {
                   8855:         get_eg(&eg0);
                   8856:         l = nd_minsugarp(d,&d);
                   8857:         sugar = nd_sugarweight?l->sugar2:SG(l);
                   8858:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   8859:         if ( nzlist_t ) {
                   8860:             node = BDY((LIST)BDY(nzlist_t));
1.6       noro     8861:             sugarh = ZTOS((Q)ARG0(node));
1.1       noro     8862:             tn = BDY((LIST)ARG1(node));
                   8863:             if ( !tn ) {
                   8864:               nzlist_t = NEXT(nzlist_t);
                   8865:               continue;
                   8866:             }
                   8867:             /* tn = [[i1,i2],...] */
                   8868:             lh = nd_ipairtospair(tn);
                   8869:         }
                   8870:         bucket = create_pbucket();
                   8871:         stat = nd_sp_f4(m,0,l,bucket);
                   8872:         if ( !stat ) {
                   8873:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8874:             NEXT(t) = d; d = l;
                   8875:             d = nd_reconstruct(0,d);
                   8876:             continue;
                   8877:         }
                   8878:         if ( bucket->m < 0 ) continue;
                   8879:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   8880:         if ( !col ) {
                   8881:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8882:             NEXT(t) = d; d = l;
                   8883:             d = nd_reconstruct(0,d);
                   8884:             continue;
                   8885:         }
1.12      noro     8886:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1       noro     8887:         if ( DP_Print )
1.6       noro     8888:             fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5       noro     8889:                 sugar,eg_f4.exectime);
1.1       noro     8890:         nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
                   8891:         if ( checkonly && nflist ) return 0;
                   8892:         /* adding new bases */
                   8893:         if ( nflist ) nd_last_nonzero = f4red;
                   8894:         for ( r = nflist; r; r = NEXT(r) ) {
                   8895:             nf = (NDV)BDY(r);
1.38      noro     8896:             if ( nd_f4_td ) SG(nf) = nd_tdeg(nf);
1.1       noro     8897:             ndv_removecont(m,nf);
                   8898:             if ( !m && nd_nalg ) {
                   8899:                 ND nf1;
                   8900:
                   8901:                 nf1 = ndvtond(m,nf);
                   8902:                 nd_monic(0,&nf1);
                   8903:                 nd_removecont(m,nf1);
                   8904:                 nf = ndtondv(m,nf1);
                   8905:             }
1.24      noro     8906:             nh = ndv_newps(m,nf,0);
1.1       noro     8907:             d = update_pairs(d,g,nh,0);
                   8908:             g = update_base(g,nh);
                   8909:         }
                   8910:         if ( DP_Print ) {
                   8911:           fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
                   8912:         }
                   8913:         if ( nd_gentrace ) {
                   8914:       for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
                   8915:         NEXTNODE(tn0,tn);
1.6       noro     8916:                 STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1       noro     8917:                 node = mknode(2,i1,i2); MKLIST(l0,node);
                   8918:         BDY(tn) = l0;
                   8919:       }
                   8920:       if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6       noro     8921:             STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1       noro     8922:             MKNODE(node,l1,nzlist); nzlist = node;
                   8923:         }
                   8924:         if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
                   8925:         f4red++;
                   8926:         if ( nd_f4red && f4red > nd_f4red ) break;
                   8927:         if ( nd_rank0 && !nflist ) break;
                   8928:     }
                   8929:     if ( nd_gentrace ) {
                   8930:     MKLIST(l0,reverse_node(nzlist));
                   8931:         MKNODE(nd_alltracelist,l0,0);
                   8932:     }
                   8933: #if 0
                   8934:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   8935: #endif
1.12      noro     8936:   if ( DP_Print ) {
                   8937:     fprintf(asir_out,"number of red=%d,",Nf4_red);
                   8938:     fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
                   8939:       f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
1.41      noro     8940:     fprintf(asir_out,"number of removed pairs=%d\n,",NcriB+NcriMF+Ncri2);
1.12      noro     8941:   }
1.1       noro     8942:   conv_ilist(nd_demand,0,g,indp);
                   8943:     return g;
                   8944: }
                   8945:
                   8946: NODE nd_f4_trace(int m,int **indp)
                   8947: {
                   8948:     int i,nh,stat,index;
                   8949:     NODE r,g;
                   8950:     ND_pairs d,l,l0,t;
                   8951:     ND spol,red;
                   8952:     NDV nf,redv,nfqv,nfv;
                   8953:     NM s0,s;
                   8954:     NODE rp0,srp0,nflist;
                   8955:     int nsp,nred,col,rank,len,k,j,a;
                   8956:     UINT c;
                   8957:     UINT **spmat;
                   8958:     UINT *s0vect,*svect,*p,*v;
                   8959:     int *colstat;
                   8960:     IndArray *imat;
                   8961:     int *rhead;
                   8962:     int spcol,sprow;
                   8963:     int sugar;
                   8964:     PGeoBucket bucket;
                   8965:     struct oEGT eg0,eg1,eg_f4;
                   8966:
                   8967:     g = 0; d = 0;
                   8968:     for ( i = 0; i < nd_psn; i++ ) {
                   8969:         d = update_pairs(d,g,i,0);
                   8970:         g = update_base(g,i);
                   8971:     }
                   8972:     while ( d ) {
                   8973:         get_eg(&eg0);
                   8974:         l = nd_minsugarp(d,&d);
                   8975:         sugar = SG(l);
                   8976:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   8977:         bucket = create_pbucket();
                   8978:         stat = nd_sp_f4(m,0,l,bucket);
                   8979:         if ( !stat ) {
                   8980:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8981:             NEXT(t) = d; d = l;
                   8982:             d = nd_reconstruct(1,d);
                   8983:             continue;
                   8984:         }
                   8985:         if ( bucket->m < 0 ) continue;
                   8986:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   8987:         if ( !col ) {
                   8988:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8989:             NEXT(t) = d; d = l;
                   8990:             d = nd_reconstruct(1,d);
                   8991:             continue;
                   8992:         }
                   8993:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   8994:         if ( DP_Print )
1.3       noro     8995:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5       noro     8996:                 sugar,eg_f4.exectime);
1.1       noro     8997:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   8998:         if ( !l0 ) continue;
                   8999:         l = l0;
                   9000:
                   9001:         /* over Q */
                   9002:         bucket = create_pbucket();
                   9003:         stat = nd_sp_f4(0,1,l,bucket);
                   9004:         if ( !stat ) {
                   9005:             for ( t = l; NEXT(t); t = NEXT(t) );
                   9006:             NEXT(t) = d; d = l;
                   9007:             d = nd_reconstruct(1,d);
                   9008:             continue;
                   9009:         }
                   9010:         if ( bucket->m < 0 ) continue;
                   9011:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   9012:         if ( !col ) {
                   9013:             for ( t = l; NEXT(t); t = NEXT(t) );
                   9014:             NEXT(t) = d; d = l;
                   9015:             d = nd_reconstruct(1,d);
                   9016:             continue;
                   9017:         }
                   9018:         nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   9019:         /* adding new bases */
                   9020:         for ( r = nflist; r; r = NEXT(r) ) {
                   9021:             nfqv = (NDV)BDY(r);
                   9022:             ndv_removecont(0,nfqv);
1.6       noro     9023:             if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1       noro     9024:             if ( nd_nalg ) {
                   9025:                 ND nf1;
                   9026:
                   9027:                 nf1 = ndvtond(m,nfqv);
                   9028:                 nd_monic(0,&nf1);
                   9029:                 nd_removecont(0,nf1);
                   9030:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   9031:             }
                   9032:             nfv = ndv_dup(0,nfqv);
                   9033:             ndv_mod(m,nfv);
                   9034:             ndv_removecont(m,nfv);
1.24      noro     9035:             nh = ndv_newps(0,nfv,nfqv);
1.1       noro     9036:             d = update_pairs(d,g,nh,0);
                   9037:             g = update_base(g,nh);
                   9038:         }
                   9039:     }
                   9040: #if 0
                   9041:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   9042: #endif
                   9043:   conv_ilist(nd_demand,1,g,indp);
                   9044:     return g;
                   9045: }
                   9046:
                   9047: int rref(matrix mat,int *sugar)
                   9048: {
                   9049:   int row,col,i,j,k,l,s,wcol,wj;
                   9050:   unsigned long bj;
                   9051:   unsigned long **a;
                   9052:   unsigned long *ai,*ak,*as,*t;
                   9053:   int *pivot;
                   9054:
                   9055:   row = mat->row;
                   9056:   col = mat->col;
                   9057:   a = mat->a;
                   9058:   wcol = (col+BLEN-1)/BLEN;
                   9059:   pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   9060:   i = 0;
                   9061:   for ( j = 0; j < col; j++ ) {
                   9062:   wj = j/BLEN; bj = 1L<<(j%BLEN);
                   9063:     for ( k = i; k < row; k++ )
                   9064:     if ( a[k][wj] & bj ) break;
                   9065:     if ( k == row ) continue;
                   9066:   pivot[i] = j;
                   9067:     if ( k != i ) {
                   9068:    t = a[i]; a[i] = a[k]; a[k] = t;
                   9069:    s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
                   9070:   }
                   9071:   ai = a[i];
                   9072:     for ( k = i+1; k < row; k++ ) {
                   9073:     ak = a[k];
                   9074:     if ( ak[wj] & bj ) {
                   9075:       for ( l = wj; l < wcol; l++ )
                   9076:       ak[l] ^= ai[l];
                   9077:       sugar[k] = MAX(sugar[k],sugar[i]);
                   9078:     }
                   9079:   }
                   9080:   i++;
                   9081:   }
                   9082:   for ( k = i-1; k >= 0; k-- ) {
                   9083:     j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
                   9084:   ak = a[k];
                   9085:     for ( s = 0; s < k; s++ ) {
                   9086:     as = a[s];
                   9087:       if ( as[wj] & bj ) {
                   9088:         for ( l = wj; l < wcol; l++ )
                   9089:       as[l] ^= ak[l];
                   9090:       sugar[s] = MAX(sugar[s],sugar[k]);
                   9091:     }
                   9092:   }
                   9093:   }
                   9094:   return i;
                   9095: }
                   9096:
                   9097: void print_matrix(matrix mat)
                   9098: {
                   9099:   int row,col,i,j;
                   9100:   unsigned long *ai;
                   9101:
                   9102:   row = mat->row;
                   9103:   col = mat->col;
                   9104:   printf("%d x %d\n",row,col);
                   9105:   for ( i = 0; i < row; i++ ) {
                   9106:   ai = mat->a[i];
                   9107:     for ( j = 0; j < col; j++ ) {
                   9108:     if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
                   9109:     else putchar('0');
                   9110:   }
                   9111:   putchar('\n');
                   9112:   }
                   9113: }
                   9114:
                   9115: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
                   9116:
                   9117: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
                   9118: {
                   9119:   int row,col,wcol,wj,i,j;
                   9120:   unsigned long bj;
                   9121:   unsigned long *ai;
                   9122:   unsigned long **a;
                   9123:   int len;
                   9124:   int *pos;
                   9125:
                   9126:   row = mat->row;
                   9127:   col = mat->col;
                   9128:   wcol = (col+BLEN-1)/BLEN;
                   9129:   pos = (int *)MALLOC(wcol*sizeof(int));
                   9130:   bzero(pos,wcol*sizeof(int));
                   9131:   for ( i = j = 0; i < wcol; i++ )
                   9132:     if ( v[i] ) pos[j++] = i;;
                   9133:   len = j;
                   9134:   wj = rhead/BLEN;
                   9135:   bj = 1L<<rhead%BLEN;
                   9136:   a = mat->a;
                   9137:   for ( i = 0; i < row; i++ ) {
                   9138:   ai = a[i];
                   9139:     if ( ai[wj]&bj ) {
                   9140:     for ( j = 0; j < len; j++ )
                   9141:       ai[pos[j]] ^= v[pos[j]];
                   9142:     sugar[i] = MAX(sugar[i],rsugar);
                   9143:   }
                   9144:   }
                   9145: }
                   9146:
                   9147: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   9148: {
                   9149:     int nsp,nred,i,i0,k,rank,row;
                   9150:     NODE r0,rp;
                   9151:     ND_pairs sp;
                   9152:   ND spol;
                   9153:   NM_ind_pair rt;
                   9154:     int *s0hash;
                   9155:   UINT *s;
                   9156:   int *pivot,*sugar,*head;
                   9157:   matrix mat;
                   9158:     NM m;
                   9159:     NODE r;
                   9160:   struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
                   9161:   int rhead,rsugar,size;
                   9162:     unsigned long *v;
                   9163:
                   9164:     get_eg(&eg0);
                   9165:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   9166:     nred = length(rp0);
                   9167:     mat = alloc_matrix(nsp,col);
                   9168:     s0hash = (int *)MALLOC(col*sizeof(int));
                   9169:     for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
                   9170:         s0hash[i] = ndl_hash_value(s);
                   9171:
                   9172:   sugar = (int *)MALLOC(nsp*sizeof(int));
                   9173:   for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
                   9174:     nd_sp(2,0,sp,&spol);
                   9175:     if ( spol ) {
                   9176:         mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
                   9177:       sugar[i] = SG(spol);
                   9178:       i++;
                   9179:     }
                   9180:   }
                   9181:   mat->row = i;
                   9182:     if ( DP_Print ) {
                   9183:       fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
                   9184:     }
                   9185:   size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
                   9186:   v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
                   9187:     for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
                   9188:     rt = (NM_ind_pair)BDY(rp);
                   9189:     bzero(v,size);
                   9190:         rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
                   9191:     rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
                   9192:       red_by_vect_2(mat,sugar,v,rhead,rsugar);
                   9193:   }
                   9194:
                   9195:     get_eg(&eg1);
                   9196:     init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
                   9197:   rank = rref(mat,sugar);
                   9198:
                   9199:     for ( i = 0, r0 = 0; i < rank; i++ ) {
                   9200:       NEXTNODE(r0,r);
                   9201:     BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
                   9202:       SG((NDV)BDY(r)) = sugar[i];
                   9203:     }
                   9204:     if ( r0 ) NEXT(r) = 0;
                   9205:     get_eg(&eg2);
                   9206:     init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
                   9207:     if ( DP_Print ) {
                   9208:         fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5       noro     9209:       eg_elim1.exectime,eg_elim2.exectime);
1.1       noro     9210:         fflush(asir_out);
                   9211:   }
                   9212:     return r0;
                   9213: }
                   9214:
                   9215:
                   9216: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   9217: {
                   9218:     IndArray *imat;
1.11      noro     9219:     int nsp,nred,i,start;
1.1       noro     9220:     int *rhead;
                   9221:     NODE r0,rp;
                   9222:     ND_pairs sp;
                   9223:     NM_ind_pair *rvect;
                   9224:     UINT *s;
                   9225:     int *s0hash;
1.11      noro     9226:     struct oEGT eg0,eg1,eg_conv;
1.1       noro     9227:
                   9228:     if ( m == 2 && nd_rref2 )
                   9229:      return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
                   9230:
                   9231:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   9232:     nred = length(rp0);
                   9233:     imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   9234:     rhead = (int *)MALLOC(col*sizeof(int));
                   9235:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   9236:
                   9237:     /* construction of index arrays */
1.11      noro     9238:     get_eg(&eg0);
1.1       noro     9239:     if ( DP_Print ) {
1.11      noro     9240:       fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   9241:       fflush(asir_out);
1.1       noro     9242:     }
                   9243:     rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11      noro     9244:     for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1       noro     9245:         rvect[i] = (NM_ind_pair)BDY(rp);
1.11      noro     9246:         imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1       noro     9247:         rhead[imat[i]->head] = 1;
1.11      noro     9248:         start = imat[i]->head;
                   9249:     }
1.12      noro     9250:     get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11      noro     9251:     if ( DP_Print ) {
                   9252:       fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   9253:       fflush(asir_out);
1.1       noro     9254:     }
                   9255:     if ( m > 0 )
1.7       noro     9256: #if SIZEOF_LONG==8
1.1       noro     9257:         r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   9258: #else
                   9259:         r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   9260: #endif
                   9261:     else if ( m == -1 )
                   9262:         r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   9263:     else if ( m == -2 )
                   9264:         r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   9265:     else
                   9266:         r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   9267:     return r0;
                   9268: }
                   9269:
                   9270: /* for Fp, 2<=p<2^16 */
                   9271:
                   9272: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   9273:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   9274: {
                   9275:     int spcol,sprow,a;
                   9276:     int i,j,k,l,rank;
                   9277:     NODE r0,r;
                   9278:     ND_pairs sp;
                   9279:     ND spol;
                   9280:     UINT **spmat;
                   9281:     UINT *svect,*v;
                   9282:     int *colstat;
                   9283:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9284:     int maxrs;
                   9285:     int *spsugar;
                   9286:     ND_pairs *spactive;
                   9287:
                   9288:     spcol = col-nred;
                   9289:     get_eg(&eg0);
                   9290:     /* elimination (1st step) */
                   9291:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   9292:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   9293:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9294:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   9295:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9296:         nd_sp(m,0,sp,&spol);
                   9297:         if ( !spol ) continue;
                   9298:         nd_to_vect(m,s0vect,col,spol,svect);
                   9299:         if ( m == -1 )
                   9300:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   9301:         else
1.40      noro     9302:             maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,0);
1.1       noro     9303:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9304:         if ( i < col ) {
                   9305:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   9306:             for ( j = k = 0; j < col; j++ )
                   9307:                 if ( !rhead[j] ) v[k++] = svect[j];
                   9308:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9309:             if ( nz )
                   9310:             spactive[sprow] = sp;
                   9311:             sprow++;
                   9312:         }
                   9313:         nd_free(spol);
                   9314:     }
                   9315:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9316:     if ( DP_Print ) {
1.5       noro     9317:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9318:         fflush(asir_out);
                   9319:     }
                   9320:     /* free index arrays */
                   9321:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   9322:
                   9323:     /* elimination (2nd step) */
                   9324:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9325:     if ( m == -1 )
                   9326:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   9327:     else
                   9328:         rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   9329:     r0 = 0;
                   9330:     for ( i = 0; i < rank; i++ ) {
                   9331:         NEXTNODE(r0,r); BDY(r) =
                   9332:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   9333:         SG((NDV)BDY(r)) = spsugar[i];
                   9334:         GCFREE(spmat[i]);
                   9335:     }
                   9336:     if ( r0 ) NEXT(r) = 0;
                   9337:
                   9338:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   9339:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9340:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9341:     if ( DP_Print ) {
1.5       noro     9342:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9343:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9344:             nsp,nred,sprow,spcol,rank);
1.5       noro     9345:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9346:     }
                   9347:     if ( nz ) {
                   9348:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   9349:         if ( rank > 0 ) {
                   9350:             NEXT(spactive[rank-1]) = 0;
                   9351:             *nz = spactive[0];
                   9352:         } else
                   9353:             *nz = 0;
                   9354:     }
                   9355:     return r0;
                   9356: }
                   9357:
1.40      noro     9358: NODE nd_f4_red_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   9359:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
                   9360: {
                   9361:     int spcol,sprow,a;
                   9362:     int i,j,k,l,rank;
                   9363:     NODE r0,r;
                   9364:     ND_pairs sp;
                   9365:     ND spol;
                   9366:     UINT **spmat;
                   9367:     UINT *svect,*cvect;
                   9368:     UINT *v;
                   9369:     int *colstat;
                   9370:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9371:     int maxrs;
                   9372:     int *spsugar;
                   9373:     ND_pairs *spactive;
                   9374:     SIG *spsig;
                   9375:
                   9376:     get_eg(&eg0);
                   9377:     /* elimination (1st step) */
                   9378:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   9379:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9380:     spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
                   9381:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9382:         nd_sp(m,0,sp,&spol);
                   9383:         if ( !spol ) {
                   9384:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
                   9385:           continue;
                   9386:         }
                   9387:         svect = (UINT *)MALLOC(col*sizeof(UINT));
                   9388:         nd_to_vect(m,s0vect,col,spol,svect);
                   9389:         maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,spol->sig);
                   9390:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9391:         if ( i < col ) {
                   9392:             spmat[sprow] = svect;
                   9393:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9394:             spsig[sprow] = sp->sig;
                   9395:             sprow++;
                   9396:         } else {
                   9397:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
                   9398:         }
                   9399:         nd_free(spol);
                   9400:     }
                   9401:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
                   9402:     if ( DP_Print ) {
                   9403:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   9404:         fflush(asir_out);
                   9405:     }
                   9406:     /* free index arrays */
                   9407:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   9408:
                   9409:     /* elimination (2nd step) */
                   9410:     colstat = (int *)MALLOC(col*sizeof(int));
                   9411:     rank = nd_gauss_elim_mod_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
                   9412:     r0 = 0;
                   9413:     for ( i = 0; i < sprow; i++ ) {
                   9414:         if ( spsugar[i] >= 0 ) {
                   9415:           NEXTNODE(r0,r);
                   9416:           BDY(r) = vect_to_ndv_s(spmat[i],col,s0vect);
                   9417:           SG((NDV)BDY(r)) = spsugar[i];
                   9418:           ((NDV)BDY(r))->sig = spsig[i];
                   9419:         } else
                   9420:           syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
                   9421:         GCFREE(spmat[i]);
                   9422:     }
                   9423:     if ( r0 ) NEXT(r) = 0;
                   9424:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
                   9425:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9426:     if ( DP_Print ) {
                   9427:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   9428:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9429:             nsp,nred,sprow,col,rank);
                   9430:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   9431:     }
                   9432:     return r0;
                   9433: }
                   9434:
1.1       noro     9435:
                   9436: /* for small finite fields */
                   9437:
                   9438: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   9439:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   9440: {
                   9441:     int spcol,sprow,a;
                   9442:     int i,j,k,l,rank;
                   9443:     NODE r0,r;
                   9444:     ND_pairs sp;
                   9445:     ND spol;
                   9446:     UINT **spmat;
                   9447:     UINT *svect,*v;
                   9448:     int *colstat;
                   9449:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9450:     int maxrs;
                   9451:     int *spsugar;
                   9452:     ND_pairs *spactive;
                   9453:
                   9454:     spcol = col-nred;
                   9455:     get_eg(&eg0);
                   9456:     /* elimination (1st step) */
                   9457:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   9458:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   9459:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9460:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   9461:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9462:         nd_sp(m,0,sp,&spol);
                   9463:         if ( !spol ) continue;
                   9464:         nd_to_vect(m,s0vect,col,spol,svect);
                   9465:         maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   9466:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9467:         if ( i < col ) {
                   9468:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   9469:             for ( j = k = 0; j < col; j++ )
                   9470:                 if ( !rhead[j] ) v[k++] = svect[j];
                   9471:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9472:             if ( nz )
                   9473:             spactive[sprow] = sp;
                   9474:             sprow++;
                   9475:         }
                   9476:         nd_free(spol);
                   9477:     }
                   9478:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9479:     if ( DP_Print ) {
1.5       noro     9480:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9481:         fflush(asir_out);
                   9482:     }
                   9483:     /* free index arrays */
                   9484:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   9485:
                   9486:     /* elimination (2nd step) */
                   9487:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9488:     rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   9489:     r0 = 0;
                   9490:     for ( i = 0; i < rank; i++ ) {
                   9491:         NEXTNODE(r0,r); BDY(r) =
                   9492:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   9493:         SG((NDV)BDY(r)) = spsugar[i];
                   9494:         GCFREE(spmat[i]);
                   9495:     }
                   9496:     if ( r0 ) NEXT(r) = 0;
                   9497:
                   9498:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   9499:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9500:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9501:     if ( DP_Print ) {
1.5       noro     9502:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9503:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9504:             nsp,nred,sprow,spcol,rank);
1.5       noro     9505:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9506:     }
                   9507:     if ( nz ) {
                   9508:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   9509:         if ( rank > 0 ) {
                   9510:             NEXT(spactive[rank-1]) = 0;
                   9511:             *nz = spactive[0];
                   9512:         } else
                   9513:             *nz = 0;
                   9514:     }
                   9515:     return r0;
                   9516: }
                   9517:
                   9518: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   9519:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   9520: {
                   9521:     int spcol,sprow,a;
                   9522:     int i,j,k,l,rank;
                   9523:     NODE r0,r;
                   9524:     ND_pairs sp;
                   9525:     ND spol;
                   9526:     mpz_t **spmat;
                   9527:     mpz_t *svect,*v;
                   9528:     int *colstat;
                   9529:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9530:     int maxrs;
                   9531:     int *spsugar;
                   9532:     pointer *w;
                   9533:
                   9534:     spcol = col-nred;
                   9535:     get_eg(&eg0);
                   9536:     /* elimination (1st step) */
                   9537:     spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
                   9538:     svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   9539:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9540:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9541:         nd_sp(m,trace,sp,&spol);
                   9542:         if ( !spol ) continue;
                   9543:         nd_to_vect_lf(s0vect,col,spol,svect);
                   9544:         maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
                   9545:         for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
                   9546:         if ( i < col ) {
                   9547:             spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
                   9548:             for ( j = k = 0; j < col; j++ )
                   9549:                 if ( !rhead[j] ) v[k++][0] = svect[j][0];
                   9550:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9551:             sprow++;
                   9552:         }
                   9553: /*        nd_free(spol); */
                   9554:     }
                   9555:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9556:     if ( DP_Print ) {
1.5       noro     9557:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9558:         fflush(asir_out);
                   9559:     }
                   9560:     /* free index arrays */
                   9561: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   9562:
                   9563:     /* elimination (2nd step) */
                   9564:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9565:     rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
                   9566:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   9567:     for ( i = 0; i < rank; i++ ) {
                   9568: #if 0
                   9569:         w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   9570:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   9571: #else
                   9572:         w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   9573:         SG((NDV)w[i]) = spsugar[i];
                   9574: #endif
                   9575: /*        GCFREE(spmat[i]); */
                   9576:
                   9577:     }
                   9578: #if 0
                   9579:     qsort(w,rank,sizeof(NDV),
                   9580:         (int (*)(const void *,const void *))ndv_compare);
                   9581: #endif
                   9582:     r0 = 0;
                   9583:     for ( i = 0; i < rank; i++ ) {
                   9584:         NEXTNODE(r0,r); BDY(r) = w[i];
                   9585:     }
                   9586:     if ( r0 ) NEXT(r) = 0;
                   9587:
                   9588: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   9589:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9590:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9591:     if ( DP_Print ) {
1.5       noro     9592:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9593:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9594:             nsp,nred,sprow,spcol,rank);
1.5       noro     9595:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9596:     }
                   9597:     return r0;
                   9598: }
                   9599:
                   9600: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   9601:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   9602: {
                   9603:     int spcol,sprow,a;
                   9604:     int i,j,k,l,rank;
                   9605:     NODE r0,r;
                   9606:     ND_pairs sp;
                   9607:     ND spol;
                   9608:     Z **spmat;
                   9609:     Z *svect,*v;
                   9610:     int *colstat;
                   9611:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9612:     int maxrs;
                   9613:     int *spsugar;
                   9614:     pointer *w;
                   9615:
                   9616:     spcol = col-nred;
                   9617:     get_eg(&eg0);
                   9618:     /* elimination (1st step) */
                   9619:     spmat = (Z **)MALLOC(nsp*sizeof(Q *));
                   9620:     svect = (Z *)MALLOC(col*sizeof(Q));
                   9621:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9622:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9623:         nd_sp(0,trace,sp,&spol);
                   9624:         if ( !spol ) continue;
                   9625:         nd_to_vect_q(s0vect,col,spol,svect);
                   9626:         maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
                   9627:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9628:         if ( i < col ) {
                   9629:             spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
                   9630:             for ( j = k = 0; j < col; j++ )
                   9631:                 if ( !rhead[j] ) v[k++] = svect[j];
                   9632:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9633:             sprow++;
                   9634:         }
                   9635: /*        nd_free(spol); */
                   9636:     }
                   9637:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9638:     if ( DP_Print ) {
1.5       noro     9639:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9640:         fflush(asir_out);
                   9641:     }
                   9642:     /* free index arrays */
                   9643: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   9644:
                   9645:     /* elimination (2nd step) */
                   9646:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9647:     rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
                   9648:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   9649:     for ( i = 0; i < rank; i++ ) {
                   9650: #if 0
                   9651:         w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   9652:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   9653: #else
                   9654:         w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   9655:         SG((NDV)w[i]) = spsugar[i];
                   9656: #endif
                   9657: /*        GCFREE(spmat[i]); */
                   9658:     }
                   9659: #if 0
                   9660:     qsort(w,rank,sizeof(NDV),
                   9661:         (int (*)(const void *,const void *))ndv_compare);
                   9662: #endif
                   9663:     r0 = 0;
                   9664:     for ( i = 0; i < rank; i++ ) {
                   9665:         NEXTNODE(r0,r); BDY(r) = w[i];
                   9666:     }
                   9667:     if ( r0 ) NEXT(r) = 0;
                   9668:
                   9669: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   9670:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9671:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9672:     if ( DP_Print ) {
1.5       noro     9673:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9674:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9675:             nsp,nred,sprow,spcol,rank);
1.5       noro     9676:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9677:     }
                   9678:     return r0;
                   9679: }
                   9680:
                   9681: FILE *nd_write,*nd_read;
                   9682:
                   9683: void nd_send_int(int a) {
                   9684:     write_int(nd_write,(unsigned int *)&a);
                   9685: }
                   9686:
                   9687: void nd_send_intarray(int *p,int len) {
                   9688:     write_intarray(nd_write,(unsigned int *)p,len);
                   9689: }
                   9690:
                   9691: int nd_recv_int() {
                   9692:     int a;
                   9693:
                   9694:     read_int(nd_read,(unsigned int *)&a);
                   9695:     return a;
                   9696: }
                   9697:
                   9698: void nd_recv_intarray(int *p,int len) {
                   9699:     read_intarray(nd_read,(unsigned int *)p,len);
                   9700: }
                   9701:
                   9702: void nd_send_ndv(NDV p) {
                   9703:     int len,i;
                   9704:     NMV m;
                   9705:
                   9706:     if ( !p ) nd_send_int(0);
                   9707:     else {
                   9708:         len = LEN(p);
                   9709:         nd_send_int(len);
                   9710:         m = BDY(p);
                   9711:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9712:             nd_send_int(CM(m));
                   9713:             nd_send_intarray((int *)DL(m),nd_wpd);
                   9714:         }
                   9715:     }
                   9716: }
                   9717:
                   9718: void nd_send_nd(ND p) {
                   9719:     int len,i;
                   9720:     NM m;
                   9721:
                   9722:     if ( !p ) nd_send_int(0);
                   9723:     else {
                   9724:         len = LEN(p);
                   9725:         nd_send_int(len);
                   9726:         m = BDY(p);
                   9727:         for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   9728:             nd_send_int(CM(m));
                   9729:             nd_send_intarray((int *)DL(m),nd_wpd);
                   9730:         }
                   9731:     }
                   9732: }
                   9733:
                   9734: NDV nd_recv_ndv()
                   9735: {
                   9736:     int len,i;
                   9737:     NMV m,m0;
                   9738:     NDV r;
                   9739:
                   9740:     len = nd_recv_int();
                   9741:     if ( !len ) return 0;
                   9742:     else {
                   9743:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   9744: #if 0
                   9745:         ndv_alloc += len*nmv_adv;
                   9746: #endif
                   9747:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9748:             CM(m) = nd_recv_int();
                   9749:             nd_recv_intarray((int *)DL(m),nd_wpd);
                   9750:         }
                   9751:         MKNDV(nd_nvar,m0,len,r);
                   9752:         return r;
                   9753:     }
                   9754: }
                   9755:
                   9756: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
                   9757: {
                   9758:     int i,j,t,c,rank,inv;
                   9759:     int *ci,*ri;
                   9760:     Z dn;
                   9761:     MAT m,nm;
                   9762:
                   9763:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
                   9764:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   9765:     for ( i = 0; i < row; i++ )
                   9766:         for ( j = 0; j < col; j++ )
                   9767:             mat0[i][j] = 0;
                   9768:     c = col-rank;
                   9769:     for ( i = 0; i < rank; i++ ) {
                   9770:         mat0[i][ri[i]] = dn;
                   9771:         for ( j = 0; j < c; j++ )
                   9772:             mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
                   9773:     }
                   9774:     return rank;
                   9775: }
                   9776:
                   9777: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   9778: {
                   9779:     int i,j,k,l,inv,a,rank,s;
                   9780:     unsigned int *t,*pivot,*pk;
                   9781:     unsigned int **mat;
                   9782:     ND_pairs pair;
                   9783:
                   9784:     mat = (unsigned int **)mat0;
                   9785:     for ( rank = 0, j = 0; j < col; j++ ) {
                   9786:         for ( i = rank; i < row; i++ )
                   9787:             mat[i][j] %= md;
                   9788:         for ( i = rank; i < row; i++ )
                   9789:             if ( mat[i][j] )
                   9790:                 break;
                   9791:         if ( i == row ) {
                   9792:             colstat[j] = 0;
                   9793:             continue;
                   9794:         } else
                   9795:             colstat[j] = 1;
                   9796:         if ( i != rank ) {
                   9797:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   9798:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   9799:             if ( spactive ) {
                   9800:                 pair = spactive[i]; spactive[i] = spactive[rank];
                   9801:                 spactive[rank] = pair;
                   9802:             }
                   9803:         }
                   9804:         pivot = mat[rank];
                   9805:         s = sugar[rank];
                   9806:         inv = invm(pivot[j],md);
                   9807:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   9808:             if ( *pk ) {
                   9809:                 if ( *pk >= (unsigned int)md )
                   9810:                     *pk %= md;
                   9811:                 DMAR(*pk,inv,0,md,*pk)
                   9812:             }
                   9813:         for ( i = rank+1; i < row; i++ ) {
                   9814:             t = mat[i];
                   9815:             if ( (a = t[j]) != 0 ) {
                   9816:                 sugar[i] = MAX(sugar[i],s);
                   9817:                 red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   9818:             }
                   9819:         }
                   9820:         rank++;
                   9821:     }
                   9822:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   9823:         if ( colstat[j] ) {
                   9824:             pivot = mat[l];
                   9825:             s = sugar[l];
                   9826:             for ( i = 0; i < l; i++ ) {
                   9827:                 t = mat[i];
                   9828:                 t[j] %= md;
                   9829:                 if ( (a = t[j]) != 0 ) {
                   9830:                     sugar[i] = MAX(sugar[i],s);
                   9831:                     red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   9832:                 }
                   9833:             }
                   9834:             l--;
                   9835:         }
                   9836:     for ( j = 0, l = 0; l < rank; j++ )
                   9837:         if ( colstat[j] ) {
                   9838:             t = mat[l];
                   9839:             for ( k = j; k < col; k++ )
                   9840:                 if ( t[k] >= (unsigned int)md )
                   9841:                     t[k] %= md;
                   9842:             l++;
                   9843:         }
                   9844:     return rank;
                   9845: }
                   9846:
1.40      noro     9847: int nd_gauss_elim_mod_s(UINT **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat,SIG *sig)
                   9848: {
                   9849:   int i,j,k,l,rank,s,imin;
                   9850:   UINT inv;
                   9851:   UINT a;
                   9852:   UINT *t,*pivot,*pk;
                   9853:   UINT *ck;
                   9854:   UINT *ct;
                   9855:   ND_pairs pair;
                   9856:   SIG sg;
                   9857:   int *used;
                   9858:
                   9859:   used = (int *)MALLOC(row*sizeof(int));
                   9860:   for ( j = 0; j < col; j++ ) {
                   9861:     for ( i = 0; i < row; i++ )
                   9862:       a = mat[i][j] %= md;
                   9863:     for ( i = 0; i < row; i++ )
                   9864:       if ( !used[i] && mat[i][j] ) break;
                   9865:     if ( i == row ) {
                   9866:       colstat[j] = 0;
                   9867:       continue;
                   9868:     } else {
                   9869:       colstat[j] = 1;
                   9870:       used[i] = 1;
                   9871:     }
                   9872:     /* column j is normalized */
                   9873:     s = sugar[i];
                   9874:     inv = invm(mat[i][j],md);
                   9875:     /* normalize pivot row */
                   9876:     for ( k = j, pk = mat[i]+j; k < col; k++, pk++, ck++ ) {
                   9877:       DMAR(*pk,inv,0,md,*pk);
                   9878:     }
                   9879:     for ( k = i+1; k < row; k++ ) {
                   9880:       if ( (a = mat[k][j]) != 0 ) {
                   9881:         sugar[k] = MAX(sugar[k],s);
                   9882:         red_by_vect(md,mat[k]+j,mat[i]+j,(int)(md-a),col-j);
                   9883:         Nf4_red++;
                   9884:       }
                   9885:     }
                   9886:   }
                   9887:   rank = 0;
                   9888:   for ( i = 0; i < row; i++ ) {
                   9889:     for ( j = 0; j < col; j++ )
                   9890:       if ( mat[i][j] ) break;
                   9891:     if ( j == col ) sugar[i] = -1;
                   9892:     else rank++;
                   9893:   }
                   9894:   return rank;
                   9895: }
                   9896:
1.1       noro     9897:
1.7       noro     9898: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1       noro     9899: {
1.7       noro     9900:     int i,j,k,l,inv,a,rank,s;
                   9901:     unsigned int *t,*pivot,*pk;
                   9902:     unsigned int **mat;
                   9903:
                   9904:     mat = (unsigned int **)mat0;
                   9905:     for ( rank = 0, j = 0; j < col; j++ ) {
                   9906:         for ( i = rank; i < row; i++ )
                   9907:             if ( mat[i][j] )
                   9908:                 break;
                   9909:         if ( i == row ) {
                   9910:             colstat[j] = 0;
                   9911:             continue;
                   9912:         } else
                   9913:             colstat[j] = 1;
                   9914:         if ( i != rank ) {
                   9915:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   9916:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   9917:         }
                   9918:         pivot = mat[rank];
                   9919:         s = sugar[rank];
                   9920:         inv = _invsf(pivot[j]);
                   9921:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   9922:             if ( *pk )
                   9923:                 *pk = _mulsf(*pk,inv);
                   9924:         for ( i = rank+1; i < row; i++ ) {
                   9925:             t = mat[i];
                   9926:             if ( (a = t[j]) != 0 ) {
                   9927:                 sugar[i] = MAX(sugar[i],s);
                   9928:                 red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   9929:             }
                   9930:         }
                   9931:         rank++;
                   9932:     }
                   9933:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   9934:         if ( colstat[j] ) {
                   9935:             pivot = mat[l];
                   9936:             s = sugar[l];
                   9937:             for ( i = 0; i < l; i++ ) {
                   9938:                 t = mat[i];
                   9939:                 if ( (a = t[j]) != 0 ) {
                   9940:                     sugar[i] = MAX(sugar[i],s);
                   9941:                     red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   9942:                 }
                   9943:             }
                   9944:             l--;
                   9945:         }
                   9946:     return rank;
                   9947: }
1.1       noro     9948:
1.7       noro     9949: int ndv_ishomo(NDV p)
                   9950: {
                   9951:     NMV m;
                   9952:     int len,h;
1.1       noro     9953:
                   9954:     if ( !p ) return 1;
                   9955:     len = LEN(p);
                   9956:     m = BDY(p);
                   9957:     h = TD(DL(m));
                   9958:     NMV_ADV(m);
                   9959:     for ( len--; len; len--, NMV_ADV(m) )
1.20      noro     9960:         if ( TD(DL(m)) != h ) {
                   9961:           return 0;
                   9962:         }
1.1       noro     9963:     return 1;
                   9964: }
                   9965:
                   9966: void ndv_save(NDV p,int index)
                   9967: {
                   9968:     FILE *s;
                   9969:     char name[BUFSIZ];
                   9970:     short id;
                   9971:     int nv,sugar,len,n,i,td,e,j;
                   9972:     NMV m;
                   9973:     unsigned int *dl;
                   9974:     int mpos;
                   9975:
                   9976:     sprintf(name,"%s/%d",Demand,index);
                   9977:     s = fopen(name,"w");
                   9978:     savevl(s,0);
                   9979:     if ( !p ) {
                   9980:         saveobj(s,0);
                   9981:         return;
                   9982:     }
                   9983:     id = O_DP;
                   9984:     nv = NV(p);
                   9985:     sugar = SG(p);
                   9986:     len = LEN(p);
                   9987:     write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
                   9988:     write_int(s,(unsigned int *)&len);
                   9989:
                   9990:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     9991:         saveobj(s,(Obj)CZ(m));
1.1       noro     9992:         dl = DL(m);
                   9993:         td = TD(dl);
                   9994:         write_int(s,(unsigned int *)&td);
                   9995:         for ( j = 0; j < nv; j++ ) {
                   9996:             e = GET_EXP(dl,j);
                   9997:             write_int(s,(unsigned int *)&e);
                   9998:         }
                   9999:         if ( nd_module ) {
                   10000:             mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
                   10001:         }
                   10002:     }
                   10003:     fclose(s);
                   10004: }
                   10005:
                   10006: void nd_save_mod(ND p,int index)
                   10007: {
                   10008:     FILE *s;
                   10009:     char name[BUFSIZ];
                   10010:     int nv,sugar,len,c;
                   10011:     NM m;
                   10012:
                   10013:     sprintf(name,"%s/%d",Demand,index);
                   10014:     s = fopen(name,"w");
                   10015:     if ( !p ) {
                   10016:     len = 0;
                   10017:       write_int(s,(unsigned int *)&len);
                   10018:     fclose(s);
                   10019:         return;
                   10020:     }
                   10021:     nv = NV(p);
                   10022:     sugar = SG(p);
                   10023:     len = LEN(p);
                   10024:     write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
                   10025:   for ( m = BDY(p); m; m = NEXT(m) ) {
                   10026:     c = CM(m); write_int(s,(unsigned int *)&c);
                   10027:     write_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   10028:   }
                   10029:     fclose(s);
                   10030: }
                   10031:
                   10032: NDV ndv_load(int index)
                   10033: {
                   10034:     FILE *s;
                   10035:     char name[BUFSIZ];
                   10036:     short id;
                   10037:     int nv,sugar,len,n,i,td,e,j;
                   10038:     NDV d;
                   10039:     NMV m0,m;
                   10040:     unsigned int *dl;
                   10041:     Obj obj;
                   10042:     int mpos;
                   10043:
                   10044:     sprintf(name,"%s/%d",Demand,index);
                   10045:     s = fopen(name,"r");
                   10046:     if ( !s ) return 0;
                   10047:
                   10048:     skipvl(s);
                   10049:     read_short(s,(unsigned short *)&id);
                   10050:     if ( !id ) return 0;
                   10051:     read_int(s,(unsigned int *)&nv);
                   10052:     read_int(s,(unsigned int *)&sugar);
                   10053:     read_int(s,(unsigned int *)&len);
                   10054:
                   10055:     m0 = m = MALLOC(len*nmv_adv);
                   10056:     for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     10057:         loadobj(s,&obj); CZ(m) = (Z)obj;
1.1       noro     10058:         dl = DL(m);
                   10059:         ndl_zero(dl);
                   10060:         read_int(s,(unsigned int *)&td); TD(dl) = td;
                   10061:         for ( j = 0; j < nv; j++ ) {
                   10062:             read_int(s,(unsigned int *)&e);
                   10063:             PUT_EXP(dl,j,e);
                   10064:         }
                   10065:         if ( nd_module ) {
                   10066:             read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
                   10067:         }
                   10068:         if ( nd_blockmask ) ndl_weight_mask(dl);
                   10069:     }
                   10070:     fclose(s);
                   10071:     MKNDV(nv,m0,len,d);
                   10072:     SG(d) = sugar;
                   10073:     return d;
                   10074: }
                   10075:
                   10076: ND nd_load_mod(int index)
                   10077: {
                   10078:     FILE *s;
                   10079:     char name[BUFSIZ];
                   10080:     int nv,sugar,len,i,c;
                   10081:   ND d;
                   10082:     NM m0,m;
                   10083:
                   10084:     sprintf(name,"%s/%d",Demand,index);
                   10085:     s = fopen(name,"r");
                   10086:   /* if the file does not exist, it means p[index]=0 */
                   10087:     if ( !s ) return 0;
                   10088:
                   10089:     read_int(s,(unsigned int *)&nv);
                   10090:   if ( !nv ) { fclose(s); return 0; }
                   10091:
                   10092:     read_int(s,(unsigned int *)&sugar);
                   10093:     read_int(s,(unsigned int *)&len);
                   10094:   for ( m0 = 0, i = 0; i < len; i++ ) {
                   10095:     NEXTNM(m0,m);
                   10096:     read_int(s,(unsigned int *)&c); CM(m) = c;
                   10097:     read_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   10098:   }
                   10099:   NEXT(m) = 0;
                   10100:     MKND(nv,m0,len,d);
                   10101:     SG(d) = sugar;
                   10102:   fclose(s);
                   10103:     return d;
                   10104: }
                   10105:
                   10106: void nd_det(int mod,MAT f,P *rp)
                   10107: {
                   10108:     VL fv,tv;
                   10109:     int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
                   10110:     pointer **m;
                   10111:     P **w;
                   10112:     P mp,r;
                   10113:     NDV **dm;
                   10114:     NDV *t,*mi,*mj;
                   10115:     NDV d,s,mij,mjj;
                   10116:     ND u;
                   10117:     NMV nmv;
                   10118:     UINT *bound;
                   10119:     PGeoBucket bucket;
                   10120:     struct order_spec *ord;
                   10121:     Z dq,dt,ds;
                   10122:     Z mone;
                   10123:     Z gn,qn,dn0,nm,dn;
                   10124:
                   10125:     create_order_spec(0,0,&ord);
                   10126:     nd_init_ord(ord);
                   10127:     get_vars((Obj)f,&fv);
                   10128:     if ( f->row != f->col )
                   10129:         error("nd_det : non-square matrix");
                   10130:     n = f->row;
                   10131:     m = f->body;
                   10132:     for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   10133:
                   10134:     if ( !nvar ) {
                   10135:         if ( !mod )
                   10136:             detp(CO,(P **)m,n,rp);
                   10137:         else {
                   10138:             w = (P **)almat_pointer(n,n);
                   10139:             for ( i = 0; i < n; i++ )
                   10140:                 for ( j = 0; j < n; j++ )
                   10141:                     ptomp(mod,(P)m[i][j],&w[i][j]);
                   10142:             detmp(CO,mod,w,n,&mp);
                   10143:             mptop(mp,rp);
                   10144:         }
                   10145:         return;
                   10146:     }
                   10147:
                   10148:     if ( !mod ) {
                   10149:         w = (P **)almat_pointer(n,n);
                   10150:         dq = ONE;
                   10151:         for ( i = 0; i < n; i++ ) {
                   10152:             dn0 = ONE;
                   10153:             for ( j = 0; j < n; j++ ) {
                   10154:                 if ( !m[i][j] ) continue;
                   10155:                 lgp(m[i][j],&nm,&dn);
1.6       noro     10156:                 gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1       noro     10157:             }
                   10158:             if ( !UNIZ(dn0) ) {
                   10159:                 ds = dn0;
                   10160:                 for ( j = 0; j < n; j++ )
                   10161:                     mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   10162:                 mulz(dq,ds,&dt); dq = dt;
                   10163:             } else
                   10164:                 for ( j = 0; j < n; j++ )
                   10165:                     w[i][j] = (P)m[i][j];
                   10166:         }
                   10167:         m = (pointer **)w;
                   10168:     }
                   10169:
                   10170:     for ( i = 0, max = 1; i < n; i++ )
                   10171:         for ( j = 0; j < n; j++ )
                   10172:             for ( tv = fv; tv; tv = NEXT(tv) ) {
                   10173:                 e = getdeg(tv->v,(P)m[i][j]);
                   10174:                 max = MAX(e,max);
                   10175:             }
                   10176:     nd_setup_parameters(nvar,max);
                   10177:     dm = (NDV **)almat_pointer(n,n);
                   10178:     for ( i = 0, max = 1; i < n; i++ )
                   10179:         for ( j = 0; j < n; j++ ) {
                   10180:             dm[i][j] = ptondv(CO,fv,m[i][j]);
                   10181:             if ( mod ) ndv_mod(mod,dm[i][j]);
                   10182:             if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   10183:         }
                   10184:     d = ptondv(CO,fv,(P)ONE);
                   10185:     if ( mod ) ndv_mod(mod,d);
                   10186:     chsgnz(ONE,&mone);
                   10187:     for ( j = 0, sgn = 1; j < n; j++ ) {
                   10188:       if ( DP_Print ) {
                   10189:         fprintf(asir_out,".");
                   10190:       }
                   10191:         for ( i = j; i < n && !dm[i][j]; i++ );
                   10192:         if ( i == n ) {
                   10193:             *rp = 0;
                   10194:             return;
                   10195:         }
                   10196:         k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
                   10197:         for ( k = j; k < n; k++ )
                   10198:             for ( l = j; l < n; l++ )
                   10199:                 if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   10200:                     k0 = k; l0 = l; len0 = LEN(dm[k][l]);
                   10201:                 }
                   10202:         if ( k0 != j ) {
                   10203:             t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   10204:             sgn = -sgn;
                   10205:         }
                   10206:         if ( l0 != j ) {
                   10207:             for ( k = j; k < n; k++ ) {
                   10208:                 s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   10209:             }
                   10210:             sgn = -sgn;
                   10211:         }
                   10212:         bound = nd_det_compute_bound(dm,n,j);
                   10213:         for ( k = 0; k < nd_nvar; k++ )
                   10214:             if ( bound[k]*2 > nd_mask0 ) break;
                   10215:         if ( k < nd_nvar )
                   10216:             nd_det_reconstruct(dm,n,j,d);
                   10217:
                   10218:         for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
                   10219: /*            if ( DP_Print ) fprintf(asir_out,"    i=%d\n        ",i); */
                   10220:             mi = dm[i]; mij = mi[j];
                   10221:             if ( mod )
                   10222:                 ndv_mul_c(mod,mij,mod-1);
                   10223:             else
                   10224:                 ndv_mul_c_q(mij,mone);
                   10225:             for ( k = j+1; k < n; k++ ) {
                   10226: /*                if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
                   10227:                 bucket = create_pbucket();
                   10228:                 if ( mi[k] ) {
                   10229:                     nmv = BDY(mjj); len = LEN(mjj);
                   10230:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   10231:                         u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   10232:                         add_pbucket(mod,bucket,u);
                   10233:                     }
                   10234:                 }
                   10235:                 if ( mj[k] && mij ) {
                   10236:                     nmv = BDY(mij); len = LEN(mij);
                   10237:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   10238:                         u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   10239:                         add_pbucket(mod,bucket,u);
                   10240:                     }
                   10241:                 }
                   10242:                 u = nd_quo(mod,bucket,d);
                   10243:                 mi[k] = ndtondv(mod,u);
                   10244:             }
                   10245: /*            if ( DP_Print ) fprintf(asir_out,"\n",k); */
                   10246:         }
                   10247:         d = mjj;
                   10248:     }
                   10249:     if ( DP_Print ) {
                   10250:       fprintf(asir_out,"\n");
                   10251:     }
                   10252:     if ( sgn < 0 ) {
                   10253:         if ( mod )
                   10254:             ndv_mul_c(mod,d,mod-1);
                   10255:         else
                   10256:             ndv_mul_c_q(d,mone);
                   10257:     }
                   10258:     r = ndvtop(mod,CO,fv,d);
                   10259:     if ( !mod && !UNIQ(dq) )
                   10260:         divsp(CO,r,(P)dq,rp);
                   10261:     else
                   10262:         *rp = r;
                   10263: }
                   10264:
                   10265: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
                   10266: {
                   10267:     NM mr,mr0;
                   10268:     NM tnm;
                   10269:     NMV m;
                   10270:     UINT *d0,*dt,*dm;
                   10271:     int c,n,td,i,c1,c2,len;
                   10272:     Z q;
                   10273:     ND r;
                   10274:
                   10275:     if ( !p ) return 0;
                   10276:     else {
                   10277:         n = NV(p); m = BDY(p); len = LEN(p);
                   10278:         d0 = DL(m0);
                   10279:         td = TD(d);
                   10280:         mr0 = 0;
                   10281:         NEWNM(tnm);
                   10282:         if ( mod ) {
                   10283:             c = CM(m0);
                   10284:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   10285:                 ndl_add(DL(m),d0,DL(tnm));
                   10286:                 if ( ndl_reducible(DL(tnm),d) ) {
                   10287:                     NEXTNM(mr0,mr);
                   10288:                     c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
                   10289:                     ndl_copy(DL(tnm),DL(mr));
                   10290:                 }
                   10291:             }
                   10292:         } else {
1.6       noro     10293:             q = CZ(m0);
1.1       noro     10294:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   10295:                 ndl_add(DL(m),d0,DL(tnm));
                   10296:                 if ( ndl_reducible(DL(tnm),d) ) {
                   10297:                     NEXTNM(mr0,mr);
1.6       noro     10298:                     mulz(CZ(m),q,&CZ(mr));
1.1       noro     10299:                     ndl_copy(DL(tnm),DL(mr));
                   10300:                 }
                   10301:             }
                   10302:         }
                   10303:         if ( !mr0 )
                   10304:             return 0;
                   10305:         else {
                   10306:             NEXT(mr) = 0;
                   10307:             for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   10308:             MKND(NV(p),mr0,len,r);
                   10309:             SG(r) = SG(p) + TD(d0);
                   10310:             return r;
                   10311:         }
                   10312:     }
                   10313: }
                   10314:
                   10315: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   10316: {
                   10317:     int i,obpe,oadv,h,k,l;
                   10318:     static NM prev_nm_free_list;
                   10319:     EPOS oepos;
                   10320:
                   10321:     obpe = nd_bpe;
                   10322:     oadv = nmv_adv;
                   10323:     oepos = nd_epos;
                   10324:     if ( obpe < 2 ) nd_bpe = 2;
                   10325:     else if ( obpe < 3 ) nd_bpe = 3;
                   10326:     else if ( obpe < 4 ) nd_bpe = 4;
                   10327:     else if ( obpe < 5 ) nd_bpe = 5;
                   10328:     else if ( obpe < 6 ) nd_bpe = 6;
                   10329:     else if ( obpe < 8 ) nd_bpe = 8;
                   10330:     else if ( obpe < 10 ) nd_bpe = 10;
                   10331:     else if ( obpe < 16 ) nd_bpe = 16;
                   10332:     else if ( obpe < 32 ) nd_bpe = 32;
                   10333:     else error("nd_det_reconstruct : exponent too large");
                   10334:
                   10335:     nd_setup_parameters(nd_nvar,0);
                   10336:     prev_nm_free_list = _nm_free_list;
                   10337:     _nm_free_list = 0;
                   10338:     for ( k = j; k < n; k++ )
                   10339:         for (l = j; l < n; l++ )
                   10340:             ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   10341:     ndv_realloc(d,obpe,oadv,oepos);
                   10342:     prev_nm_free_list = 0;
                   10343: #if 0
                   10344:     GC_gcollect();
                   10345: #endif
                   10346: }
                   10347:
                   10348: /* returns a UINT array containing degree bounds */
                   10349:
                   10350: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   10351: {
                   10352:     UINT *d0,*d1,*d,*t,*r;
                   10353:     int k,l,i;
                   10354:
                   10355:     d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
                   10356:     for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
                   10357:     for ( k = j; k < n; k++ )
                   10358:         for ( l = j; l < n; l++ )
                   10359:             if ( dm[k][l] ) {
                   10360:                 d = ndv_compute_bound(dm[k][l]);
                   10361:                 for ( i = 0; i < nd_nvar; i++ )
                   10362:                     d0[i] = MAX(d0[i],d[i]);
                   10363:             }
                   10364:     return d0;
                   10365: }
                   10366:
                   10367: DL nd_separate_d(UINT *d,UINT *trans)
                   10368: {
                   10369:     int n,td,i,e,j;
                   10370:     DL a;
                   10371:
                   10372:     ndl_zero(trans);
                   10373:     td = 0;
                   10374:     for ( i = 0; i < nd_ntrans; i++ ) {
                   10375:         e = GET_EXP(d,i);
                   10376:         PUT_EXP(trans,i,e);
                   10377:         td += MUL_WEIGHT(e,i);
                   10378:     }
                   10379:     if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   10380:         /* homogenized */
                   10381:         i = nd_nvar-1;
                   10382:         e = GET_EXP(d,i);
                   10383:         PUT_EXP(trans,i,e);
                   10384:         td += MUL_WEIGHT(e,i);
                   10385:     }
                   10386:     TD(trans) = td;
                   10387:     if ( nd_blockmask) ndl_weight_mask(trans);
                   10388:     NEWDL(a,nd_nalg);
                   10389:     td = 0;
                   10390:     for ( i = 0; i < nd_nalg; i++ ) {
                   10391:         j = nd_ntrans+i;
                   10392:         e = GET_EXP(d,j);
                   10393:         a->d[i] = e;
                   10394:         td += e;
                   10395:     }
                   10396:     a->td = td;
                   10397:     return a;
                   10398: }
                   10399:
                   10400: int nd_monic(int mod,ND *p)
                   10401: {
                   10402:     UINT *trans,*t;
                   10403:     DL alg;
                   10404:     MP mp0,mp;
                   10405:     NM m,m0,m1,ma0,ma,mb,mr0,mr;
                   10406:     ND r;
                   10407:     DL dl;
                   10408:     DP nm;
                   10409:     NDV ndv;
                   10410:     DAlg inv,cd;
                   10411:     ND s,c;
                   10412:     Z l,mul;
                   10413:     Z ln;
                   10414:     int n,ntrans,i,e,td,is_lc,len;
                   10415:     NumberField nf;
                   10416:     struct oEGT eg0,eg1;
                   10417:
                   10418:     if ( !(nf = get_numberfield()) )
                   10419:         error("nd_monic : current_numberfield is not set");
                   10420:
                   10421:     /* Q coef -> DAlg coef */
                   10422:     NEWNM(ma0); ma = ma0;
                   10423:     m = BDY(*p);
                   10424:     is_lc = 1;
                   10425:     while ( 1 ) {
                   10426:         NEWMP(mp0); mp = mp0;
1.6       noro     10427:         mp->c = (Obj)CZ(m);
1.1       noro     10428:         mp->dl = nd_separate_d(DL(m),DL(ma));
                   10429:         NEWNM(mb);
                   10430:         for ( m = NEXT(m); m; m = NEXT(m) ) {
                   10431:             alg = nd_separate_d(DL(m),DL(mb));
                   10432:             if ( !ndl_equal(DL(ma),DL(mb)) )
                   10433:                 break;
1.6       noro     10434:             NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1       noro     10435:         }
                   10436:         NEXT(mp) = 0;
                   10437:         MKDP(nd_nalg,mp0,nm);
                   10438:         MKDAlg(nm,ONE,cd);
                   10439:         if ( is_lc == 1 ) {
                   10440:             /* if the lc is a rational number, we have nothing to do */
                   10441:             if ( !mp0->dl->td )
                   10442:                 return 1;
                   10443:
                   10444:             get_eg(&eg0);
                   10445:             invdalg(cd,&inv);
                   10446:             get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   10447:             /* check the validity of inv */
                   10448:             if ( mod && !remqi((Q)inv->dn,mod) )
                   10449:                 return 0;
                   10450:             CA(ma) = nf->one;
                   10451:             is_lc = 0;
                   10452:             ln = ONE;
                   10453:         } else {
                   10454:             muldalg(cd,inv,&CA(ma));
                   10455:             lcmz(ln,CA(ma)->dn,&ln);
                   10456:         }
                   10457:         if ( m ) {
                   10458:             NEXT(ma) = mb; ma = mb;
                   10459:         } else {
                   10460:             NEXT(ma) = 0;
                   10461:             break;
                   10462:         }
                   10463:     }
                   10464:     /* l = lcm(denoms) */
                   10465:     l = ln;
                   10466:     for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6       noro     10467:         divsz(l,CA(m)->dn,&mul);
1.1       noro     10468:         for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   10469:             NEXTNM(mr0,mr);
1.6       noro     10470:             mulz((Z)mp->c,mul,&CZ(mr));
1.1       noro     10471:             dl = mp->dl;
                   10472:             td = TD(DL(m));
                   10473:             ndl_copy(DL(m),DL(mr));
                   10474:             for ( i = 0; i < nd_nalg; i++ ) {
                   10475:                 e = dl->d[i];
                   10476:                 PUT_EXP(DL(mr),i+nd_ntrans,e);
                   10477:                 td += MUL_WEIGHT(e,i+nd_ntrans);
                   10478:             }
                   10479:             if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
                   10480:             TD(DL(mr)) = td;
                   10481:             if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   10482:         }
                   10483:     }
                   10484:     NEXT(mr) = 0;
                   10485:     for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   10486:     MKND(NV(*p),mr0,len,r);
                   10487:     /* XXX */
                   10488:     SG(r) = SG(*p);
                   10489:     nd_free(*p);
                   10490:     *p = r;
                   10491:     return 1;
                   10492: }
                   10493:
                   10494: NODE reverse_node(NODE n)
                   10495: {
                   10496:     NODE t,t1;
                   10497:
                   10498:     for ( t = 0; n; n = NEXT(n) ) {
                   10499:         MKNODE(t1,BDY(n),t); t = t1;
                   10500:     }
                   10501:     return t;
                   10502: }
                   10503:
                   10504: P ndc_div(int mod,union oNDC a,union oNDC b)
                   10505: {
                   10506:     union oNDC c;
                   10507:     int inv,t;
                   10508:
                   10509:     if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10      noro     10510:     else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1       noro     10511:     else if ( mod ) {
                   10512:         inv = invm(b.m,mod);
                   10513:         DMAR(a.m,inv,0,mod,t); c.m = t;
                   10514:     } else if ( nd_vc )
                   10515:        divsp(nd_vc,a.p,b.p,&c.p);
                   10516:     else
                   10517:        divsz(a.z,b.z,&c.z);
                   10518:     return ndctop(mod,c);
                   10519: }
                   10520:
                   10521: P ndctop(int mod,union oNDC c)
                   10522: {
                   10523:     Z q;
                   10524:     int e;
                   10525:     GFS gfs;
                   10526:
                   10527:     if ( mod == -1 ) {
                   10528:         e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
                   10529:     } else if ( mod == -2 ) {
1.10      noro     10530:        q = c.z; return (P)q;
1.1       noro     10531:     } else if ( mod > 0 ) {
1.6       noro     10532:         STOZ(c.m,q); return (P)q;
1.1       noro     10533:     } else
                   10534:         return (P)c.p;
                   10535: }
                   10536:
                   10537: /* [0,0,0,cont] = p -> p/cont */
                   10538:
                   10539: void finalize_tracelist(int i,P cont)
                   10540: {
                   10541:  LIST l;
                   10542:  NODE node;
                   10543:  Z iq;
                   10544:
                   10545:   if ( !UNIQ(cont) ) {
                   10546:     node = mknode(4,NULLP,NULLP,NULLP,cont);
                   10547:     MKLIST(l,node); MKNODE(node,l,nd_tracelist);
                   10548:     nd_tracelist = node;
                   10549:   }
1.6       noro     10550:   STOZ(i,iq);
1.1       noro     10551:   nd_tracelist = reverse_node(nd_tracelist);
                   10552:   MKLIST(l,nd_tracelist);
                   10553:   node = mknode(2,iq,l); MKLIST(l,node);
                   10554:   MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
                   10555:   nd_alltracelist = node; nd_tracelist = 0;
                   10556: }
                   10557:
                   10558: void conv_ilist(int demand,int trace,NODE g,int **indp)
                   10559: {
                   10560:     int n,i,j;
                   10561:   int *ind;
                   10562:   NODE t;
                   10563:
                   10564:     n = length(g);
                   10565:   ind = (int *)MALLOC(n*sizeof(int));
                   10566:   for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
                   10567:     j = (long)BDY(t); ind[i] = j;
                   10568:     BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
                   10569:   }
                   10570:   if ( indp ) *indp = ind;
                   10571: }
                   10572:
1.30      noro     10573: NODE conv_ilist_s(int demand,int trace,int **indp)
                   10574: {
                   10575:   int n,i,j;
                   10576:   int *ind;
                   10577:   NODE g0,g;
                   10578:
                   10579:   n = nd_psn;
                   10580:   ind = (int *)MALLOC(n*sizeof(int));
                   10581:   g0 = 0;
                   10582:   for ( i = 0; i < n; i++ ) {
                   10583:     ind[i] = i;
                   10584:     NEXTNODE(g0,g);
                   10585:     BDY(g) = (pointer)(demand?ndv_load(i):(trace?nd_ps_trace[i]:nd_ps[i]));
                   10586:   }
                   10587:   if ( g0 ) NEXT(g) = 0;
                   10588:   if ( indp ) *indp = ind;
                   10589:   return g0;
                   10590: }
                   10591:
1.46      noro     10592: void parse_nd_option(VL vl,NODE opt)
1.1       noro     10593: {
1.38      noro     10594:   NODE t,p,u;
1.1       noro     10595:   int i,s,n;
1.38      noro     10596:   char *key;
                   10597:   Obj value;
1.46      noro     10598:   VL oldvl;
1.1       noro     10599:
1.41      noro     10600:   nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_norb = 0; nd_gbblock = 0;
1.1       noro     10601:   nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
                   10602:   nd_splist = 0; nd_check_splist = 0;
1.38      noro     10603:   nd_sugarweight = 0; nd_f4red =0; nd_rank0 = 0;
                   10604:   nd_f4_td = 0; nd_sba_f4step = 2; nd_sba_pot = 0; nd_sba_largelcm = 0;
1.41      noro     10605:   nd_sba_dontsort = 0; nd_top = 0; nd_sba_redundant_check = 0;
1.46      noro     10606:   nd_sba_syz = 0; nd_sba_modord = 0; nd_sba_inputisgb = 0;
1.49      noro     10607:   nd_hpdata = 0;
1.39      noro     10608:
1.38      noro     10609:   for ( t = opt; t; t = NEXT(t) ) {
                   10610:     p = BDY((LIST)BDY(t));
                   10611:     key = BDY((STRING)BDY(p));
                   10612:     value = (Obj)BDY(NEXT(p));
                   10613:     if ( !strcmp(key,"gentrace") )
                   10614:       nd_gentrace = value?1:0;
                   10615:     else if ( !strcmp(key,"gensyz") )
                   10616:       nd_gensyz = value?1:0;
                   10617:     else if ( !strcmp(key,"nora") )
                   10618:       nd_nora = value?1:0;
1.41      noro     10619:     else if ( !strcmp(key,"norb") )
                   10620:       nd_norb = value?1:0;
1.38      noro     10621:     else if ( !strcmp(key,"gbblock") ) {
                   10622:       if ( value && OID(value) == O_LIST ) {
1.1       noro     10623:         u = BDY((LIST)value);
1.38      noro     10624:         nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.1       noro     10625:         for ( i = 0; u; u = NEXT(u) ) {
                   10626:           p = BDY((LIST)BDY(u));
1.6       noro     10627:           s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
                   10628:           nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1       noro     10629:         }
                   10630:         nd_gbblock[i] = -1;
1.38      noro     10631:       } else
                   10632:         nd_gbblock = 0;
1.1       noro     10633:     } else if ( !strcmp(key,"newelim") )
                   10634:             nd_newelim = value?1:0;
                   10635:     else if ( !strcmp(key,"intersect") )
                   10636:             nd_intersect = value?1:0;
1.17      noro     10637:     else if ( !strcmp(key,"syzgen") )
                   10638:             nd_intersect = ZTOS((Q)value);
1.1       noro     10639:     else if ( !strcmp(key,"lf") )
                   10640:             nd_lf = value?1:0;
                   10641:     else if ( !strcmp(key,"trace") ) {
1.38      noro     10642:       if ( value ) {
                   10643:         u = BDY((LIST)value);
                   10644:         nd_nzlist = BDY((LIST)ARG2(u));
                   10645:         nd_bpe = ZTOS((Q)ARG3(u));
                   10646:       }
1.1       noro     10647:     } else if ( !strcmp(key,"f4red") ) {
1.38      noro     10648:       nd_f4red = ZTOS((Q)value);
1.1       noro     10649:     } else if ( !strcmp(key,"rank0") ) {
1.38      noro     10650:       nd_rank0 = value?1:0;
1.1       noro     10651:     } else if ( !strcmp(key,"splist") ) {
1.38      noro     10652:       nd_splist = value?1:0;
1.1       noro     10653:     } else if ( !strcmp(key,"check_splist") ) {
                   10654:       nd_check_splist = BDY((LIST)value);
1.49      noro     10655:     } else if ( !strcmp(key,"hpdata") ) {
1.50    ! noro     10656:       if ( value )
        !          10657:         nd_hpdata = BDY((LIST)value);
1.1       noro     10658:     } else if ( !strcmp(key,"sugarweight") ) {
                   10659:       u = BDY((LIST)value);
1.38      noro     10660:       n = length(u);
                   10661:       nd_sugarweight = MALLOC(n*sizeof(int));
1.1       noro     10662:       for ( i = 0; i < n; i++, u = NEXT(u) )
1.38      noro     10663:         nd_sugarweight[i] = ZTOS((Q)BDY(u));
                   10664:     } else if ( !strcmp(key,"f4_td") ) {
                   10665:       nd_f4_td = value?1:0;
                   10666:     } else if ( !strcmp(key,"sba_f4step") ) {
                   10667:       nd_sba_f4step = value?ZTOS((Q)value):0;
                   10668:     } else if ( !strcmp(key,"sba_pot") ) {
1.44      noro     10669:       nd_sba_pot = ZTOS((Q)value);
1.38      noro     10670:     } else if ( !strcmp(key,"sba_largelcm") ) {
                   10671:       nd_sba_largelcm = value?1:0;
1.39      noro     10672:     } else if ( !strcmp(key,"sba_dontsort") ) {
                   10673:       nd_sba_dontsort = value?1:0;
1.44      noro     10674:     } else if ( !strcmp(key,"sba_syz") ) {
                   10675:       nd_sba_syz = value?1:0;
1.46      noro     10676:     } else if ( !strcmp(key,"sba_modord") ) {
                   10677:       // value=[vlist,ordspec,weight]
                   10678:       u = BDY((LIST)value);
                   10679:       pltovl((LIST)ARG0(u),&oldvl);
                   10680:       nd_sba_modord = create_comp_sig_spec(vl,oldvl,(Obj)ARG1(u),argc(u)==3?ARG2(u):0);
                   10681:     } else if ( !strcmp(key,"sba_gbinput") ) {
                   10682:       nd_sba_inputisgb = value?1:0;
                   10683:       if ( nd_sba_inputisgb != 0 ) {
                   10684:         // value=[vlist,ordspec,weight]
                   10685:         u = BDY((LIST)value);
                   10686:         pltovl((LIST)ARG0(u),&oldvl);
                   10687:         nd_sba_modord = create_comp_sig_spec(vl,oldvl,(Obj)ARG1(u),argc(u)==3?ARG2(u):0);
                   10688:       }
1.41      noro     10689:     } else if ( !strcmp(key,"sba_redundant_check") ) {
                   10690:       nd_sba_redundant_check = value?1:0;
1.39      noro     10691:     } else if ( !strcmp(key,"top") ) {
                   10692:       nd_top = value?1:0;
1.1       noro     10693:     }
1.38      noro     10694:   }
1.44      noro     10695:   if ( nd_sba_syz ) nd_sba_dontsort = 1;
1.1       noro     10696: }
                   10697:
                   10698: ND mdptond(DP d);
                   10699: ND nd_mul_nm(int mod,NM m0,ND p);
                   10700: ND nd_mul_nm_lf(NM m0,ND p);
                   10701: ND *btog(NODE ti,ND **p,int nb,int mod);
                   10702: ND btog_one(NODE ti,ND *p,int nb,int mod);
                   10703: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
                   10704: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
                   10705:
                   10706: /* d:monomial */
                   10707: ND mdptond(DP d)
                   10708: {
                   10709:   NM m;
                   10710:   ND r;
                   10711:
                   10712:   if ( OID(d) == 1 )
                   10713:   r = ptond(CO,CO,(P)d);
                   10714:   else {
                   10715:     NEWNM(m);
                   10716:     dltondl(NV(d),BDY(d)->dl,DL(m));
1.6       noro     10717:     CZ(m) = (Z)BDY(d)->c;
1.1       noro     10718:     NEXT(m) = 0;
                   10719:     MKND(NV(d),m,1,r);
                   10720:   }
                   10721:     return r;
                   10722: }
                   10723:
                   10724: ND nd_mul_nm(int mod,NM m0,ND p)
                   10725: {
                   10726:   UINT *d0;
                   10727:   int c0,c1,c;
                   10728:   NM tm,mr,mr0;
                   10729:   ND r;
                   10730:
                   10731:   if ( !p ) return 0;
                   10732:   d0 = DL(m0);
                   10733:   c0 = CM(m0);
                   10734:   mr0 = 0;
                   10735:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   10736:     NEXTNM(mr0,mr);
                   10737:   c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
                   10738:   ndl_add(d0,DL(tm),DL(mr));
                   10739:   }
                   10740:   NEXT(mr) = 0;
                   10741:   MKND(NV(p),mr0,LEN(p),r);
                   10742:   return r;
                   10743: }
                   10744:
                   10745: ND nd_mul_nm_lf(NM m0,ND p)
                   10746: {
                   10747:   UINT *d0;
                   10748:   Z c0,c1,c;
                   10749:   NM tm,mr,mr0;
                   10750:   ND r;
                   10751:
                   10752:   if ( !p ) return 0;
                   10753:   d0 = DL(m0);
                   10754:   c0 = CZ(m0);
                   10755:   mr0 = 0;
                   10756:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   10757:     NEXTNM(mr0,mr);
                   10758:     c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
                   10759:     ndl_add(d0,DL(tm),DL(mr));
                   10760:   }
                   10761:   NEXT(mr) = 0;
                   10762:   MKND(NV(p),mr0,LEN(p),r);
                   10763:   return r;
                   10764: }
                   10765:
                   10766: ND *btog(NODE ti,ND **p,int nb,int mod)
                   10767: {
                   10768:   PGeoBucket *r;
                   10769:   int i,ci;
                   10770:   NODE t,s;
                   10771:   ND m,tp;
                   10772:   ND *pi,*rd;
                   10773:   P c;
                   10774:
                   10775:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   10776:   for ( i = 0; i < nb; i++ )
                   10777:     r[i] = create_pbucket();
                   10778:   for ( t = ti; t; t = NEXT(t) ) {
                   10779:   s = BDY((LIST)BDY(t));
                   10780:     if ( ARG0(s) ) {
                   10781:     m = mdptond((DP)ARG2(s));
1.6       noro     10782:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     10783:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   10784:       HCM(m) = ci;
1.6       noro     10785:       pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     10786:       for ( i = 0; i < nb; i++ ) {
                   10787:       tp = nd_mul_nm(mod,BDY(m),pi[i]);
                   10788:         add_pbucket(mod,r[i],tp);
                   10789:       }
                   10790:     }
                   10791:     ci = 1;
                   10792:     } else {
                   10793:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   10794:     ci = invm(ci,mod);
                   10795:   }
                   10796:   }
                   10797:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   10798:   for ( i = 0; i < nb; i++ )
                   10799:   rd[i] = normalize_pbucket(mod,r[i]);
                   10800:   if ( ci != 1 )
                   10801:     for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
                   10802:    return rd;
                   10803: }
                   10804:
                   10805: /* YYY */
                   10806: ND *btog_lf(NODE ti,ND **p,int nb)
                   10807: {
                   10808:   PGeoBucket *r;
                   10809:   int i;
                   10810:   NODE t,s;
                   10811:   ND m,tp;
                   10812:   ND *pi,*rd;
                   10813:   LM lm;
                   10814:   Z lf,c;
                   10815:
                   10816:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   10817:   for ( i = 0; i < nb; i++ )
                   10818:     r[i] = create_pbucket();
                   10819:   for ( t = ti; t; t = NEXT(t) ) {
                   10820:     s = BDY((LIST)BDY(t));
                   10821:     if ( ARG0(s) ) {
                   10822:       m = mdptond((DP)ARG2(s));
1.6       noro     10823:       simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1       noro     10824:       if ( lm ) {
                   10825:         lmtolf(lm,&lf); HCZ(m) = lf;
1.6       noro     10826:         pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     10827:         for ( i = 0; i < nb; i++ ) {
                   10828:           tp = nd_mul_nm_lf(BDY(m),pi[i]);
                   10829:           add_pbucket(-2,r[i],tp);
                   10830:         }
                   10831:       }
                   10832:       c = ONE;
                   10833:     } else {
                   10834:       simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
                   10835:     }
                   10836:   }
                   10837:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   10838:   for ( i = 0; i < nb; i++ )
                   10839:     rd[i] = normalize_pbucket(-2,r[i]);
                   10840:   for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
                   10841:   return rd;
                   10842: }
                   10843:
                   10844: ND btog_one(NODE ti,ND *p,int nb,int mod)
                   10845: {
                   10846:   PGeoBucket r;
                   10847:   int i,ci,j;
                   10848:   NODE t,s;
                   10849:   ND m,tp;
                   10850:   ND pi,rd;
                   10851:   P c;
                   10852:
                   10853:   r = create_pbucket();
                   10854:   for ( t = ti; t; t = NEXT(t) ) {
                   10855:   s = BDY((LIST)BDY(t));
                   10856:     if ( ARG0(s) ) {
                   10857:     m = mdptond((DP)ARG2(s));
1.6       noro     10858:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     10859:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   10860:       HCM(m) = ci;
1.6       noro     10861:       pi = p[j=ZTOS((Q)ARG1(s))];
1.1       noro     10862:     if ( !pi ) {
                   10863:       pi = nd_load_mod(j);
                   10864:       tp = nd_mul_nm(mod,BDY(m),pi);
                   10865:       nd_free(pi);
                   10866:         add_pbucket(mod,r,tp);
                   10867:     } else {
                   10868:       tp = nd_mul_nm(mod,BDY(m),pi);
                   10869:         add_pbucket(mod,r,tp);
                   10870:       }
                   10871:     }
                   10872:     ci = 1;
                   10873:     } else {
                   10874:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   10875:     ci = invm(ci,mod);
                   10876:   }
                   10877:   }
                   10878:   rd = normalize_pbucket(mod,r);
                   10879:   free_pbucket(r);
                   10880:   if ( ci != 1 ) nd_mul_c(mod,rd,ci);
                   10881:   return rd;
                   10882: }
                   10883:
                   10884: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
                   10885:
                   10886: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
                   10887: {
                   10888:   int i,j,n,m,nb,pi0,pi1,nvar;
                   10889:   VL fv,tv,vv;
                   10890:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   10891:   ND **p;
                   10892:   ND *c;
                   10893:   ND u;
                   10894:   P inv;
                   10895:   MAT mat;
                   10896:
                   10897:   if ( mod == -2 )
                   10898:     return nd_btog_lf(f,v,ord,tlist,rp);
                   10899:
                   10900:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     10901:   parse_nd_option(vv,current_option);
1.1       noro     10902:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   10903:   switch ( ord->id ) {
                   10904:     case 1:
                   10905:       if ( ord->nv != nvar )
                   10906:         error("nd_check : invalid order specification");
                   10907:       break;
                   10908:     default:
                   10909:       break;
                   10910:   }
                   10911:   nd_init_ord(ord);
                   10912: #if 0
1.6       noro     10913:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     10914: #else
                   10915:   nd_bpe = 32;
                   10916: #endif
                   10917:   nd_setup_parameters(nvar,0);
                   10918:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   10919:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   10920:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   10921:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   10922:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     10923:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     10924:   if ( j > i ) i = j;
                   10925:   }
                   10926:   n = i+1;
                   10927:   nb = length(BDY(f));
                   10928:   p = (ND **)MALLOC(n*sizeof(ND *));
                   10929:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   10930:     pi = BDY((LIST)BDY(t));
1.6       noro     10931:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     10932:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   10933:     ptomp(mod,(P)ARG2(pi),&inv);
                   10934:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   10935:     u = ptond(CO,vv,(P)ONE);
                   10936:     HCM(u) = ((MQ)inv)->cont;
                   10937:     c[pi1] = u;
                   10938:   }
                   10939:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   10940:     printf("%d ",i); fflush(stdout);
                   10941:     ti = BDY((LIST)BDY(t));
1.6       noro     10942:     p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10943:   }
                   10944:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   10945:     printf("%d ",i); fflush(stdout);
                   10946:     ti = BDY((LIST)BDY(t));
1.6       noro     10947:     p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10948:   }
                   10949:   m = length(ind);
                   10950:   MKMAT(mat,nb,m);
                   10951:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     10952:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     10953:       BDY(mat)[i][j] = ndtodp(mod,c[i]);
                   10954:   return mat;
                   10955: }
                   10956:
                   10957: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
                   10958: {
                   10959:   int i,j,n,m,nb,pi0,pi1,nvar;
                   10960:   VL fv,tv,vv;
                   10961:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   10962:   ND **p;
                   10963:   ND *c;
                   10964:   ND u;
                   10965:   MAT mat;
                   10966:   LM lm;
                   10967:   Z lf,inv;
                   10968:
                   10969:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     10970:   parse_nd_option(vv,current_option);
1.1       noro     10971:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   10972:   switch ( ord->id ) {
                   10973:     case 1:
                   10974:       if ( ord->nv != nvar )
                   10975:         error("nd_check : invalid order specification");
                   10976:       break;
                   10977:     default:
                   10978:       break;
                   10979:   }
                   10980:   nd_init_ord(ord);
                   10981: #if 0
1.6       noro     10982:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     10983: #else
                   10984:   nd_bpe = 32;
                   10985: #endif
                   10986:   nd_setup_parameters(nvar,0);
                   10987:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   10988:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   10989:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   10990:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   10991:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     10992:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     10993:   if ( j > i ) i = j;
                   10994:   }
                   10995:   n = i+1;
                   10996:   nb = length(BDY(f));
                   10997:   p = (ND **)MALLOC(n*sizeof(ND *));
                   10998:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   10999:     pi = BDY((LIST)BDY(t));
1.6       noro     11000:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     11001:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   11002:     simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
                   11003:     u = ptond(CO,vv,(P)ONE);
                   11004:     HCZ(u) = inv;
                   11005:     c[pi1] = u;
                   11006:   }
                   11007:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   11008:     printf("%d ",i); fflush(stdout);
                   11009:     ti = BDY((LIST)BDY(t));
1.6       noro     11010:     p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     11011:   }
                   11012:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   11013:     printf("%d ",i); fflush(stdout);
                   11014:     ti = BDY((LIST)BDY(t));
1.6       noro     11015:     p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     11016:   }
                   11017:   m = length(ind);
                   11018:   MKMAT(mat,nb,m);
                   11019:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     11020:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     11021:       BDY(mat)[i][j] = ndtodp(-2,c[i]);
                   11022:   return mat;
                   11023: }
                   11024:
                   11025: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
                   11026:   LIST tlist,int pos,MAT *rp)
                   11027: {
                   11028:   int i,j,n,m,nb,pi0,pi1,nvar;
                   11029:   VL fv,tv,vv;
                   11030:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   11031:   ND *p;
                   11032:   ND *c;
                   11033:   ND u;
                   11034:   P inv;
                   11035:   VECT vect;
                   11036:
                   11037:   if ( mod == -2 )
                   11038:     error("nd_btog_one : not implemented yet for a large finite field");
                   11039:
                   11040:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     11041:   parse_nd_option(vv,current_option);
1.1       noro     11042:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   11043:   switch ( ord->id ) {
                   11044:     case 1:
                   11045:       if ( ord->nv != nvar )
                   11046:         error("nd_check : invalid order specification");
                   11047:       break;
                   11048:     default:
                   11049:       break;
                   11050:   }
                   11051:   nd_init_ord(ord);
                   11052: #if 0
1.6       noro     11053:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     11054: #else
                   11055:   nd_bpe = 32;
                   11056: #endif
                   11057:   nd_setup_parameters(nvar,0);
                   11058:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   11059:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   11060:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   11061:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   11062:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     11063:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     11064:   if ( j > i ) i = j;
                   11065:   }
                   11066:   n = i+1;
                   11067:   nb = length(BDY(f));
                   11068:   p = (ND *)MALLOC(n*sizeof(ND *));
                   11069:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   11070:     pi = BDY((LIST)BDY(t));
1.6       noro     11071:   pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     11072:   if ( pi1 == pos ) {
                   11073:     ptomp(mod,(P)ARG2(pi),&inv);
                   11074:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   11075:     u = ptond(CO,vv,(P)ONE);
                   11076:     HCM(u) = ((MQ)inv)->cont;
                   11077:     p[pi0] = u;
                   11078:     }
                   11079:   }
                   11080:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   11081:   printf("%d ",i); fflush(stdout);
                   11082:     ti = BDY((LIST)BDY(t));
1.6       noro     11083:     p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     11084:     if ( Demand ) {
                   11085:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   11086:   }
                   11087:   }
                   11088:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   11089:   printf("%d ",i); fflush(stdout);
                   11090:     ti = BDY((LIST)BDY(t));
1.6       noro     11091:     p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     11092:     if ( Demand ) {
                   11093:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   11094:   }
                   11095:   }
                   11096:   m = length(ind);
                   11097:   MKVECT(vect,m);
                   11098:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6       noro     11099:   u = p[ZTOS((Q)BDY(t))];
1.1       noro     11100:   if ( !u ) {
1.6       noro     11101:     u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1       noro     11102:     BDY(vect)[j] = ndtodp(mod,u);
                   11103:     nd_free(u);
                   11104:   } else
                   11105:     BDY(vect)[j] = ndtodp(mod,u);
                   11106:   }
                   11107:   return vect;
                   11108: }
                   11109:
                   11110: void ndv_print_lf(NDV p)
                   11111: {
                   11112:     NMV m;
                   11113:     int i,len;
                   11114:
                   11115:     if ( !p ) printf("0\n");
                   11116:     else {
                   11117:         len = LEN(p);
                   11118:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   11119:             printf("+");
                   11120:             mpz_out_str(asir_out,10,BDY(CZ(m)));
                   11121:             printf("*");
                   11122:             ndl_print(DL(m));
                   11123:         }
                   11124:         printf("\n");
                   11125:     }
                   11126: }
                   11127:
                   11128: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
                   11129: {
                   11130:     VL tv,fv,vv,vc,av;
                   11131:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   11132:     int m,nocheck,nvar,mindex,e,max;
                   11133:     NDV c;
                   11134:     NMV a;
                   11135:     P p,zp;
                   11136:     Q dmy;
                   11137:     EPOS oepos;
                   11138:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   11139:     Alg alpha,dp;
                   11140:     P poly;
                   11141:     LIST f1,f2,zpl;
                   11142:     Obj obj;
                   11143:     NumberField nf;
                   11144:     struct order_spec *ord1;
                   11145:     struct oEGT eg_check,eg0,eg1;
                   11146:     NODE tr,tl1,tl2,tl3,tl4;
                   11147:     LIST l1,l2,l3,l4,l5;
                   11148:     int *perm;
                   11149:     int j,ret;
                   11150:     NODE retn;
                   11151:     Q jq,bpe;
                   11152:
                   11153:     nd_module = 0;
                   11154:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     11155:     parse_nd_option(vv,current_option);
1.1       noro     11156:     if ( nd_vc )
                   11157:       error("nd_f4_lf_trace : computation over a rational function field is not implemented");
                   11158:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   11159:     switch ( ord->id ) {
                   11160:         case 1:
                   11161:             if ( ord->nv != nvar )
                   11162:                 error("nd_f4_lf_trace : invalid order specification");
                   11163:             break;
                   11164:         default:
                   11165:             break;
                   11166:     }
                   11167:
                   11168:     nd_ntrans = nvar;
                   11169:     nd_nalg = 0;
                   11170:
                   11171:     nocheck = 0;
                   11172:     mindex = 0;
                   11173:
                   11174:     /* do not use on-demand load/save */
                   11175:     nd_demand = 0;
                   11176:     m = trace > 1 ? trace : get_lprime(mindex);
                   11177:     nd_init_ord(ord);
                   11178:     mrank = 0;
                   11179:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   11180:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   11181:             if ( nd_module ) {
                   11182:                 s = BDY((LIST)BDY(t));
                   11183:                 trank = length(s);
                   11184:                 mrank = MAX(mrank,trank);
                   11185:                 for ( ; s; s = NEXT(s) ) {
                   11186:                     e = getdeg(tv->v,(P)BDY(s));
                   11187:                     max = MAX(e,max);
                   11188:                 }
                   11189:             } else {
                   11190:                 e = getdeg(tv->v,(P)BDY(t));
                   11191:                 max = MAX(e,max);
                   11192:             }
                   11193:         }
                   11194:     nd_setup_parameters(nvar,max);
                   11195:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   11196:     ishomo = 1;
                   11197:     /* XXX */
                   11198:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   11199:         if ( nd_module ) {
                   11200:             c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
                   11201:         } else {
                   11202:             c = (pointer)ptondv(CO,vv,(P)BDY(t));
                   11203:         }
                   11204:         if ( ishomo )
                   11205:             ishomo = ishomo && ndv_ishomo(c);
                   11206:         if ( c ) {
                   11207:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   11208:             ndv_mod(-2,c);
                   11209:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   11210:         }
                   11211:     }
                   11212:     if ( in0 ) NEXT(in) = 0;
                   11213:     if ( fd0 ) NEXT(fd) = 0;
                   11214:     if ( !ishomo && homo ) {
                   11215:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   11216:             c = (NDV)BDY(t); len = LEN(c);
                   11217:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   11218:                 wmax = MAX(TD(DL(a)),wmax);
                   11219:         }
                   11220:         homogenize_order(ord,nvar,&ord1);
                   11221:         nd_init_ord(ord1);
                   11222:         nd_setup_parameters(nvar+1,wmax);
                   11223:         for ( t = fd0; t; t = NEXT(t) )
                   11224:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   11225:     }
                   11226:     if ( MaxDeg > 0 ) nocheck = 1;
1.24      noro     11227:     ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1       noro     11228:     if ( ret )
                   11229:       cand = nd_f4_lf_trace_main(m,&perm);
                   11230:     if ( !ret || !cand ) {
                   11231:        *rp = 0; return;
                   11232:     }
                   11233:     if ( !ishomo && homo ) {
                   11234:       /* dehomogenization */
                   11235:       for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   11236:       nd_init_ord(ord);
                   11237:       nd_setup_parameters(nvar,0);
                   11238:     }
                   11239:     cand = ndv_reducebase(cand,perm);
                   11240:     cand = ndv_reduceall(-2,cand);
                   11241:     cbpe = nd_bpe;
                   11242:     get_eg(&eg0);
                   11243:     if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   11244:       /* gbcheck : cand is a GB of Id(cand) ? */
                   11245:       retn = nd_f4(-2,0,0);
                   11246:     }
                   11247:   if ( !retn ) {
                   11248:       /* failure */
                   11249:       *rp = 0; return;
                   11250:     }
                   11251:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   11252:     if ( DP_Print )
1.5       noro     11253:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     11254:     /* dp->p */
                   11255:     nd_bpe = cbpe;
                   11256:     nd_setup_parameters(nd_nvar,0);
                   11257:     for ( r = cand; r; r = NEXT(r) ) {
                   11258:       if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
                   11259:       else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
                   11260:     }
                   11261:     MKLIST(*rp,cand);
                   11262: }
                   11263:
                   11264: NODE nd_f4_lf_trace_main(int m,int **indp)
                   11265: {
                   11266:     int i,nh,stat,index;
                   11267:     NODE r,rm,g;
                   11268:     ND_pairs d,l,l0,t;
                   11269:     ND spol,red;
                   11270:     NDV nf,redv,nfqv,nfv;
                   11271:     NM s0,s;
                   11272:     NODE rp0,srp0,nflist,nflist_lf;
                   11273:     int nsp,nred,col,rank,len,k,j,a;
                   11274:     UINT c;
                   11275:     UINT **spmat;
                   11276:     UINT *s0vect,*svect,*p,*v;
                   11277:     int *colstat;
                   11278:     IndArray *imat;
                   11279:     int *rhead;
                   11280:     int spcol,sprow;
                   11281:     int sugar;
                   11282:     PGeoBucket bucket;
                   11283:     struct oEGT eg0,eg1,eg_f4;
                   11284:
                   11285:     g = 0; d = 0;
                   11286:     for ( i = 0; i < nd_psn; i++ ) {
                   11287:         d = update_pairs(d,g,i,0);
                   11288:         g = update_base(g,i);
                   11289:     }
                   11290:     while ( d ) {
                   11291:         get_eg(&eg0);
                   11292:         l = nd_minsugarp(d,&d);
                   11293:         sugar = SG(l);
                   11294:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   11295:         bucket = create_pbucket();
                   11296:         stat = nd_sp_f4(m,0,l,bucket);
                   11297:         if ( !stat ) {
                   11298:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11299:             NEXT(t) = d; d = l;
                   11300:             d = nd_reconstruct(1,d);
                   11301:             continue;
                   11302:         }
                   11303:         if ( bucket->m < 0 ) continue;
                   11304:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   11305:         if ( !col ) {
                   11306:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11307:             NEXT(t) = d; d = l;
                   11308:             d = nd_reconstruct(1,d);
                   11309:             continue;
                   11310:         }
                   11311:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   11312:         if ( DP_Print )
1.5       noro     11313:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1       noro     11314:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   11315:         if ( !l0 ) continue;
                   11316:         l = l0;
                   11317:
                   11318:         /* over LF */
                   11319:         bucket = create_pbucket();
                   11320:         stat = nd_sp_f4(-2,1,l,bucket);
                   11321:         if ( !stat ) {
                   11322:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11323:             NEXT(t) = d; d = l;
                   11324:             d = nd_reconstruct(1,d);
                   11325:             continue;
                   11326:         }
                   11327:         if ( bucket->m < 0 ) continue;
                   11328:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   11329:         if ( !col ) {
                   11330:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11331:             NEXT(t) = d; d = l;
                   11332:             d = nd_reconstruct(1,d);
                   11333:             continue;
                   11334:         }
                   11335:         nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
                   11336:         /* adding new bases */
                   11337:         for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
                   11338:             nfv = (NDV)BDY(rm);
                   11339:             nfqv = (NDV)BDY(r);
                   11340:             if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
                   11341:             ndv_removecont(m,nfv);
                   11342:             ndv_removecont(-2,nfqv);
1.24      noro     11343:             nh = ndv_newps(-2,nfv,nfqv);
1.1       noro     11344:             d = update_pairs(d,g,nh,0);
                   11345:             g = update_base(g,nh);
                   11346:         }
                   11347:         if ( r || rm ) return 0;
                   11348:     }
                   11349:   conv_ilist(nd_demand,1,g,indp);
                   11350:     return g;
                   11351: }
                   11352:
1.7       noro     11353: #if SIZEOF_LONG==8
                   11354:
                   11355: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   11356: {
                   11357:     int j,k,len;
                   11358:     UINT *p;
                   11359:     UINT c;
                   11360:     NDV r;
                   11361:     NMV mr0,mr;
                   11362:
                   11363:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   11364:     if ( !len ) return 0;
                   11365:     else {
                   11366:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   11367: #if 0
                   11368:         ndv_alloc += nmv_adv*len;
                   11369: #endif
                   11370:         mr = mr0;
                   11371:         p = s0vect;
                   11372:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   11373:             if ( !rhead[j] ) {
                   11374:                 if ( (c = (UINT)vect[k++]) != 0 ) {
                   11375:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   11376:                 }
                   11377:             }
                   11378:         MKNDV(nd_nvar,mr0,len,r);
                   11379:         return r;
                   11380:     }
                   11381: }
                   11382:
1.28      noro     11383: NDV vect64_to_ndv_s(mp_limb_t *vect,int col,UINT *s0vect)
                   11384: {
                   11385:     int j,k,len;
                   11386:     UINT *p;
                   11387:     UINT c;
                   11388:     NDV r;
                   11389:     NMV mr0,mr;
                   11390:
                   11391:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   11392:     if ( !len ) return 0;
                   11393:     else {
                   11394:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   11395:         mr = mr0;
                   11396:         p = s0vect;
                   11397:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   11398:           if ( (c = (UINT)vect[k++]) != 0 ) {
                   11399:             ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   11400:           }
                   11401:         MKNDV(nd_nvar,mr0,len,r);
                   11402:         return r;
                   11403:     }
                   11404: }
                   11405:
1.7       noro     11406: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
                   11407: {
                   11408:     NM m;
1.11      noro     11409:     UINT *t,*s,*u;
                   11410:     int i,st,ed,md,prev,c;
1.7       noro     11411:
                   11412:     for ( i = 0; i < n; i++ ) r[i] = 0;
1.11      noro     11413:     prev = 0;
                   11414:     for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
                   11415:       t = DL(m);
                   11416:       st = prev;
                   11417:       ed = n;
                   11418:       while ( ed > st ) {
                   11419:         md = (st+ed)/2;
                   11420:         u = s0+md*nd_wpd;
                   11421:         c = DL_COMPARE(u,t);
                   11422:         if ( c == 0 ) break;
                   11423:         else if ( c > 0 ) st = md;
                   11424:         else ed = md;
                   11425:       }
                   11426:       r[md] = (mp_limb_t)CM(m);
                   11427:       prev = md;
1.7       noro     11428:     }
                   11429:     for ( i = 0; !r[i]; i++ );
                   11430:     return i;
                   11431: }
                   11432:
                   11433: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
                   11434:
1.28      noro     11435: 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     11436: {
                   11437:     int i,j,k,len,pos,prev;
                   11438:     mp_limb_t a,c,c1,c2;
                   11439:     IndArray ivect;
                   11440:     unsigned char *ivc;
                   11441:     unsigned short *ivs;
                   11442:     unsigned int *ivi;
                   11443:     NDV redv;
                   11444:     NMV mr;
                   11445:     NODE rp;
                   11446:     int maxrs;
                   11447:
                   11448:     for ( i = 0; i < col; i++ ) cvect[i] = 0;
                   11449:     maxrs = 0;
                   11450:     for ( i = 0; i < nred; i++ ) {
                   11451:         ivect = imat[i];
                   11452:         k = ivect->head;
                   11453:         a = svect[k]; c = cvect[k];
                   11454:         MOD128(a,c,m);
                   11455:         svect[k] = a; cvect[k] = 0;
1.28      noro     11456:         if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.11      noro     11457:             Nf4_red++;
1.7       noro     11458:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   11459:             c = m-c; redv = nd_ps[rp0[i]->index];
                   11460:             len = LEN(redv); mr = BDY(redv);
                   11461:             svect[k] = 0; prev = k;
                   11462:             switch ( ivect->width ) {
                   11463:                 case 1:
                   11464:                     ivc = ivect->index.c;
                   11465:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   11466:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12      noro     11467:                         c2 = svect[pos]+c1*c;
                   11468:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   11469:                         svect[pos] = c2;
1.7       noro     11470:                     }
                   11471:                     break;
                   11472:                 case 2:
                   11473:                     ivs = ivect->index.s;
                   11474:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   11475:                         pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12      noro     11476:                         c2 = svect[pos]+c1*c;
                   11477:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   11478:                         svect[pos] = c2;
1.7       noro     11479:                     }
                   11480:                     break;
                   11481:                 case 4:
                   11482:                     ivi = ivect->index.i;
                   11483:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   11484:                         pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12      noro     11485:                         c2 = svect[pos]+c1*c;
                   11486:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   11487:                         svect[pos] = c2;
1.7       noro     11488:                     }
                   11489:                     break;
                   11490:             }
                   11491:         }
                   11492:     }
                   11493:     for ( i = 0; i < col; i++ ) {
                   11494:       a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
                   11495:     }
                   11496:     return maxrs;
                   11497: }
                   11498:
                   11499: /* for Fp, 2^15=<p<2^29 */
                   11500:
                   11501: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   11502:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   11503: {
                   11504:     int spcol,sprow,a;
                   11505:     int i,j,k,l,rank;
                   11506:     NODE r0,r;
                   11507:     ND_pairs sp;
                   11508:     ND spol;
                   11509:     mp_limb_t **spmat;
                   11510:     mp_limb_t *svect,*cvect;
                   11511:     mp_limb_t *v;
                   11512:     int *colstat;
                   11513:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   11514:     int maxrs;
                   11515:     int *spsugar;
                   11516:     ND_pairs *spactive;
                   11517:
                   11518:     spcol = col-nred;
                   11519:     get_eg(&eg0);
                   11520:     /* elimination (1st step) */
                   11521:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   11522:     svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11523:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11524:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   11525:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   11526:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   11527:         nd_sp(m,0,sp,&spol);
                   11528:         if ( !spol ) continue;
                   11529:         nd_to_vect64(m,s0vect,col,spol,svect);
1.28      noro     11530:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,0);
1.7       noro     11531:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   11532:         if ( i < col ) {
                   11533:             spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
                   11534:             for ( j = k = 0; j < col; j++ )
                   11535:                 if ( !rhead[j] ) v[k++] = (UINT)svect[j];
                   11536:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   11537:             if ( nz )
                   11538:             spactive[sprow] = sp;
                   11539:             sprow++;
                   11540:         }
                   11541:         nd_free(spol);
                   11542:     }
1.12      noro     11543:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7       noro     11544:     if ( DP_Print ) {
                   11545:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   11546:         fflush(asir_out);
                   11547:     }
                   11548:     /* free index arrays */
                   11549:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   11550:
                   11551:     /* elimination (2nd step) */
                   11552:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   11553:     rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   11554:     r0 = 0;
                   11555:     for ( i = 0; i < rank; i++ ) {
                   11556:         NEXTNODE(r0,r); BDY(r) =
                   11557:           (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   11558:         SG((NDV)BDY(r)) = spsugar[i];
                   11559:         GCFREE(spmat[i]);
                   11560:     }
                   11561:     if ( r0 ) NEXT(r) = 0;
                   11562:
                   11563:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12      noro     11564:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7       noro     11565:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   11566:     if ( DP_Print ) {
                   11567:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   11568:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   11569:             nsp,nred,sprow,spcol,rank);
                   11570:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   11571:     }
                   11572:     if ( nz ) {
                   11573:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   11574:         if ( rank > 0 ) {
                   11575:             NEXT(spactive[rank-1]) = 0;
                   11576:             *nz = spactive[0];
                   11577:         } else
                   11578:             *nz = 0;
                   11579:     }
                   11580:     return r0;
                   11581: }
                   11582:
                   11583: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   11584: {
                   11585:   int i,j,k,l,rank,s;
                   11586:   mp_limb_t inv;
                   11587:   mp_limb_t a;
                   11588:   UINT c;
                   11589:   mp_limb_t *t,*pivot,*pk;
                   11590:   UINT *ck;
                   11591:   UINT **cmat;
                   11592:   UINT *ct;
                   11593:   ND_pairs pair;
                   11594:
                   11595:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   11596:   for ( i = 0; i < row; i++ ) {
                   11597:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   11598:     bzero(cmat[i],col*sizeof(UINT));
                   11599:   }
                   11600:
                   11601:   for ( rank = 0, j = 0; j < col; j++ ) {
                   11602:     for ( i = rank; i < row; i++ ) {
                   11603:       a = mat[i][j]; c = cmat[i][j];
                   11604:       MOD128(a,c,md);
                   11605:       mat[i][j] = a; cmat[i][j] = 0;
                   11606:     }
                   11607:     for ( i = rank; i < row; i++ )
                   11608:       if ( mat[i][j] )
                   11609:         break;
                   11610:     if ( i == row ) {
                   11611:       colstat[j] = 0;
                   11612:       continue;
                   11613:     } else
                   11614:       colstat[j] = 1;
                   11615:     if ( i != rank ) {
                   11616:       t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   11617:       ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
                   11618:       s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   11619:       if ( spactive ) {
                   11620:         pair = spactive[i]; spactive[i] = spactive[rank];
                   11621:         spactive[rank] = pair;
                   11622:       }
                   11623:     }
                   11624:     /* column j is normalized */
                   11625:     s = sugar[rank];
                   11626:     inv = invm((UINT)mat[rank][j],md);
                   11627:     /* normalize pivot row */
                   11628:     for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
                   11629:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   11630:     }
                   11631:     for ( i = rank+1; i < row; i++ ) {
                   11632:       if ( (a = mat[i][j]) != 0 ) {
                   11633:         sugar[i] = MAX(sugar[i],s);
                   11634:         red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11      noro     11635:         Nf4_red++;
1.7       noro     11636:       }
                   11637:     }
                   11638:     rank++;
                   11639:   }
                   11640:   for ( j = col-1, l = rank-1; j >= 0; j-- )
                   11641:     if ( colstat[j] ) {
                   11642:       for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
                   11643:         a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
                   11644:       }
                   11645:       s = sugar[l];
                   11646:       for ( i = 0; i < l; i++ ) {
                   11647:         a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
                   11648:         if ( a ) {
                   11649:           sugar[i] = MAX(sugar[i],s);
                   11650:           red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11      noro     11651:           Nf4_red++;
1.7       noro     11652:         }
                   11653:       }
                   11654:       l--;
                   11655:     }
                   11656:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   11657:   GCFREE(cmat);
                   11658:   return rank;
                   11659: }
                   11660:
1.28      noro     11661: 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)
                   11662: {
                   11663:   int i,j,k,l,rank,s,imin;
                   11664:   mp_limb_t inv;
                   11665:   mp_limb_t a;
                   11666:   UINT c;
                   11667:   mp_limb_t *t,*pivot,*pk;
                   11668:   UINT *ck;
                   11669:   UINT **cmat;
                   11670:   UINT *ct;
                   11671:   ND_pairs pair;
                   11672:   SIG sg;
1.31      noro     11673:   int *used;
1.28      noro     11674:
1.31      noro     11675:   used = (int *)MALLOC(row*sizeof(int));
1.28      noro     11676:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   11677:   for ( i = 0; i < row; i++ ) {
                   11678:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   11679:     bzero(cmat[i],col*sizeof(UINT));
                   11680:   }
                   11681:
1.31      noro     11682:   for ( j = 0; j < col; j++ ) {
                   11683:     for ( i = 0; i < row; i++ ) {
1.28      noro     11684:       a = mat[i][j]; c = cmat[i][j];
                   11685:       MOD128(a,c,md);
                   11686:       mat[i][j] = a; cmat[i][j] = 0;
                   11687:     }
1.31      noro     11688:     for ( i = 0; i < row; i++ )
                   11689:       if ( !used[i] && mat[i][j] ) break;
                   11690:     if ( i == row ) {
1.28      noro     11691:       colstat[j] = 0;
                   11692:       continue;
1.31      noro     11693:     } else {
1.28      noro     11694:       colstat[j] = 1;
1.31      noro     11695:       used[i] = 1;
1.28      noro     11696:     }
                   11697:     /* column j is normalized */
1.31      noro     11698:     s = sugar[i];
                   11699:     inv = invm((UINT)mat[i][j],md);
1.28      noro     11700:     /* normalize pivot row */
1.31      noro     11701:     for ( k = j, pk = mat[i]+j, ck = cmat[i]+j; k < col; k++, pk++, ck++ ) {
1.28      noro     11702:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   11703:     }
1.31      noro     11704:     for ( k = i+1; k < row; k++ ) {
                   11705:       if ( (a = mat[k][j]) != 0 ) {
                   11706:         sugar[k] = MAX(sugar[k],s);
                   11707:         red_by_vect64(md,mat[k]+j,cmat[k]+j,mat[i]+j,(int)(md-a),col-j);
1.28      noro     11708:         Nf4_red++;
                   11709:       }
                   11710:     }
                   11711:   }
1.31      noro     11712:   rank = 0;
                   11713:   for ( i = 0; i < row; i++ ) {
                   11714:     for ( j = 0; j < col; j++ )
                   11715:       if ( mat[i][j] ) break;
                   11716:     if ( j == col ) sugar[i] = -1;
                   11717:     else rank++;
                   11718:   }
1.28      noro     11719:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   11720:   GCFREE(cmat);
                   11721:   return rank;
                   11722: }
                   11723:
                   11724: NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   11725:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
                   11726: {
                   11727:     int spcol,sprow,a;
                   11728:     int i,j,k,l,rank;
                   11729:     NODE r0,r;
                   11730:     ND_pairs sp;
                   11731:     ND spol;
                   11732:     mp_limb_t **spmat;
                   11733:     mp_limb_t *svect,*cvect;
                   11734:     mp_limb_t *v;
                   11735:     int *colstat;
                   11736:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   11737:     int maxrs;
                   11738:     int *spsugar;
                   11739:     ND_pairs *spactive;
                   11740:     SIG *spsig;
                   11741:
                   11742:     get_eg(&eg0);
                   11743:     /* elimination (1st step) */
                   11744:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   11745:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11746:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   11747:     spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
                   11748:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   11749:         nd_sp(m,0,sp,&spol);
                   11750:         if ( !spol ) {
1.29      noro     11751:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     11752:           continue;
                   11753:         }
                   11754:         svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11755:         nd_to_vect64(m,s0vect,col,spol,svect);
                   11756:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,spol->sig);
                   11757:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   11758:         if ( i < col ) {
                   11759:             spmat[sprow] = svect;
                   11760:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   11761:             spsig[sprow] = sp->sig;
                   11762:             sprow++;
                   11763:         } else {
1.29      noro     11764:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     11765:         }
                   11766:         nd_free(spol);
                   11767:     }
                   11768:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
                   11769:     if ( DP_Print ) {
                   11770:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   11771:         fflush(asir_out);
                   11772:     }
                   11773:     /* free index arrays */
                   11774:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   11775:
                   11776:     /* elimination (2nd step) */
                   11777:     colstat = (int *)MALLOC(col*sizeof(int));
                   11778:     rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
                   11779:     r0 = 0;
1.31      noro     11780:     for ( i = 0; i < sprow; i++ ) {
                   11781:         if ( spsugar[i] >= 0 ) {
                   11782:           NEXTNODE(r0,r);
                   11783:           BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
                   11784:           SG((NDV)BDY(r)) = spsugar[i];
                   11785:           ((NDV)BDY(r))->sig = spsig[i];
                   11786:         } else
                   11787:           syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
1.28      noro     11788:         GCFREE(spmat[i]);
                   11789:     }
                   11790:     if ( r0 ) NEXT(r) = 0;
                   11791:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
                   11792:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   11793:     if ( DP_Print ) {
                   11794:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   11795:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   11796:             nsp,nred,sprow,col,rank);
                   11797:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   11798:     }
                   11799:     return r0;
                   11800: }
1.40      noro     11801: #endif
1.28      noro     11802:
                   11803: NODE nd_f4_red_s(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,NODE *syzlistp)
                   11804: {
                   11805:   IndArray *imat;
                   11806:   int nsp,nred,i,start;
                   11807:   int *rhead;
                   11808:   NODE r0,rp;
                   11809:   ND_pairs sp;
                   11810:   NM_ind_pair *rvect;
                   11811:   UINT *s;
                   11812:   int *s0hash;
                   11813:   struct oEGT eg0,eg1,eg_conv;
                   11814:
                   11815:   for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   11816:   nred = length(rp0);
                   11817:   imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   11818:   rhead = (int *)MALLOC(col*sizeof(int));
                   11819:   for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   11820:
                   11821:   /* construction of index arrays */
                   11822:   get_eg(&eg0);
                   11823:   if ( DP_Print ) {
                   11824:     fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   11825:     fflush(asir_out);
                   11826:   }
                   11827:   rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   11828:   for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   11829:     rvect[i] = (NM_ind_pair)BDY(rp);
                   11830:     imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
                   11831:     rhead[imat[i]->head] = 1;
                   11832:     start = imat[i]->head;
                   11833:   }
                   11834:   get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
                   11835:   if ( DP_Print ) {
                   11836:     fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   11837:     fflush(asir_out);
                   11838:   }
                   11839:   if ( m > 0 )
1.40      noro     11840: #if SIZEOF_LONG==8
1.28      noro     11841:     r0 = nd_f4_red_mod64_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
1.40      noro     11842: #else
                   11843:     r0 = nd_f4_red_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
                   11844: #endif
1.28      noro     11845:   else
                   11846: //    r0 = nd_f4_red_q_main_s(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   11847:     error("nd_f4_red_q_main_s : not implemented yet");
                   11848:   return r0;
                   11849: }
                   11850:
                   11851: INLINE int ndl_find_reducer_minsig(UINT *dg)
                   11852: {
                   11853:   RHist r;
                   11854:   int i,singular,ret,d,k,imin;
                   11855:   SIG t;
                   11856:   static int wpd,nvar;
                   11857:   static SIG quo,quomin;
                   11858:   static UINT *tmp;
                   11859:
                   11860:   if ( !quo || nvar != nd_nvar ) { NEWSIG(quo); NEWSIG(quomin); }
                   11861:   if ( wpd != nd_wpd ) {
                   11862:     wpd = nd_wpd;
                   11863:     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
                   11864:   }
                   11865: #if 0
                   11866:   d = ndl_hash_value(dg);
                   11867:   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   11868:     if ( ndl_equal(dg,DL(r)) ) {
                   11869:       return r->index;
                   11870:     }
                   11871:   }
                   11872: #endif
                   11873:   imin = -1;
                   11874:   for ( i = 0; i < nd_psn; i++ ) {
                   11875:     r = nd_psh[i];
                   11876:     if ( ndl_reducible(dg,DL(r)) ) {
                   11877:       ndl_sub(dg,DL(r),tmp);
                   11878:       _ndltodl(tmp,DL(quo));
                   11879:       _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
                   11880:       quo->pos = nd_psh[i]->sig->pos;
                   11881:       if ( imin < 0 || comp_sig(quomin,quo) > 0 ) {
                   11882:         t = quo; quo = quomin; quomin = t;
                   11883:         imin = i;
                   11884:       }
                   11885:     }
                   11886:   }
                   11887:   if ( imin == -1 ) return nd_psn;
                   11888:   else {
                   11889: #if 0
                   11890:     nd_append_red(dg,i);
                   11891: #endif
                   11892:     return imin;
                   11893:   }
                   11894: }
                   11895:
                   11896: int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   11897: {
                   11898:   NODE rp0,rp;
                   11899:   NM mul,head,s0,s;
                   11900:   int index,col,i,sugar;
                   11901:   RHist h;
                   11902:   UINT *s0v,*p;
                   11903:   NM_ind_pair pair;
                   11904:   ND red;
                   11905:   NDV *ps;
                   11906:   SIG sig;
                   11907:
                   11908:   s0 = 0; rp0 = 0; col = 0;
                   11909:   if ( nd_demand )
                   11910:     ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   11911:   else
                   11912:     ps = trace?nd_ps_trace:nd_ps;
                   11913:   while ( 1 ) {
                   11914:     head = remove_head_pbucket_symbolic(bucket);
                   11915:     if ( !head ) break;
                   11916:     if ( !s0 ) s0 = head;
                   11917:     else NEXT(s) = head;
                   11918:     s = head;
                   11919:     index = ndl_find_reducer_minsig(DL(head));
                   11920:     if ( index >= 0 && index < nd_psn ) {
                   11921:       h = nd_psh[index];
                   11922:       NEWNM(mul);
                   11923:       ndl_sub(DL(head),DL(h),DL(mul));
                   11924:       if ( ndl_check_bound2(index,DL(mul)) )
                   11925:         return 0;
                   11926:       sugar = TD(DL(mul))+SG(ps[index]);
                   11927:       NEWSIG(sig);
                   11928:       _ndltodl(DL(mul),DL(sig));
                   11929:       _addtodl(nd_nvar,DL(nd_psh[index]->sig),DL(sig));
                   11930:       sig->pos = nd_psh[index]->sig->pos;
                   11931:       MKNM_ind_pair(pair,mul,index,sugar,sig);
                   11932:       red = ndv_mul_nm_symbolic(mul,ps[index]);
                   11933:       add_pbucket_symbolic(bucket,nd_remove_head(red));
                   11934:       NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   11935:     }
                   11936:     col++;
                   11937:   }
                   11938:   if ( rp0 ) NEXT(rp) = 0;
                   11939:   NEXT(s) = 0;
                   11940:   s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   11941:   for ( i = 0, p = s0v, s = s0; i < col;
                   11942:     i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   11943:   *s0vect = s0v;
                   11944:   *r = rp0;
                   11945:
                   11946:   return col;
                   11947: }
                   11948:
                   11949: NODE nd_sba_f4(int m,int **indp)
                   11950: {
1.34      noro     11951:   int i,nh,stat,index,f4red,f4step;
1.33      noro     11952:   int col,rank,len,k,j,a,sugar,nbase,psugar,ms;
                   11953:   NODE r,g,rp0,nflist;
1.41      noro     11954:   ND_pairs d,l,t,l1;
1.33      noro     11955:   ND h,nf;
                   11956:   NDV nfv;
                   11957:   union oNDC hc;
                   11958:   UINT *s0vect;
1.28      noro     11959:   UINT c;
                   11960:   PGeoBucket bucket;
1.33      noro     11961:   NODE *syzlist;
                   11962:   SIG sig;
1.28      noro     11963:   struct oEGT eg0,eg1,eg_f4;
1.33      noro     11964:   struct oEGT eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.28      noro     11965:
                   11966:   Nf4_red=0;
1.30      noro     11967:   d = 0;
1.29      noro     11968:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.28      noro     11969:   for ( i = 0; i < nd_psn; i++ ) {
1.30      noro     11970:     d = update_pairs_s(d,i,syzlist);
1.28      noro     11971:   }
1.30      noro     11972:   nd_nbase = nd_psn;
1.28      noro     11973:   f4red = 1;
1.33      noro     11974:   psugar = 0;
1.34      noro     11975:   f4step = 0;
1.28      noro     11976:   while ( d ) {
1.33      noro     11977:     for ( t = d, ms = SG(d); t; t = NEXT(t) )
                   11978:       if ( SG(t) < ms ) ms = SG(t);
1.38      noro     11979:     if ( ms == psugar && f4step >= nd_sba_f4step ) {
1.33      noro     11980: again:
                   11981:       l = d; d = d->next;
1.41      noro     11982: #if 0
1.33      noro     11983:       if ( small_lcm(l) ) {
                   11984:         if ( DP_Print ) fprintf(asir_out,"M");
                   11985:         continue;
                   11986:       }
                   11987:       sig = l->sig;
                   11988:       stat = nd_sp(m,0,l,&h);
1.41      noro     11989: #else
                   11990:       l1 = find_smallest_lcm(l);
                   11991:       if ( l1 == 0 ) {
                   11992:         if ( DP_Print ) fprintf(asir_out,"M");
                   11993:         continue;
                   11994:       }
                   11995:       sig = l1->sig;
                   11996:       stat = nd_sp(m,0,l1,&h);
                   11997: #endif
1.33      noro     11998:       if ( !stat ) {
                   11999:         NEXT(l) = d; d = l;
                   12000:         d = nd_reconstruct(0,d);
                   12001:         goto again;
                   12002:       }
                   12003:   get_eg(&eg1);
                   12004:   #if USE_GEOBUCKET
1.39      noro     12005:       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     12006:   #else
1.39      noro     12007:       stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.33      noro     12008:   #endif
                   12009:   get_eg(&eg2);
                   12010:       if ( !stat ) {
                   12011:         NEXT(l) = d; d = l;
                   12012:         d = nd_reconstruct(0,d);
                   12013:         goto again;
                   12014:       } else if ( stat == -1 ) {
                   12015:         if ( DP_Print ) { printf("S"); fflush(stdout); }
                   12016:         FREENDP(l);
                   12017:       } else if ( nf ) {
                   12018:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   12019:         add_eg(&eg_nf,&eg1,&eg2);
                   12020:         hc = HCU(nf);
                   12021:         nd_removecont(m,nf);
                   12022:         nfv = ndtondv(m,nf); nd_free(nf);
                   12023:         nh = ndv_newps(m,nfv,0);
                   12024:
1.30      noro     12025:         d = update_pairs_s(d,nh,syzlist);
                   12026:         nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.33      noro     12027:         FREENDP(l);
1.28      noro     12028:       } else {
1.33      noro     12029:         add_eg(&eg_nfzero,&eg1,&eg2);
                   12030:        // syzygy
                   12031:   get_eg(&eg1);
                   12032:         d = remove_spair_s(d,sig);
                   12033:   get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29      noro     12034:         syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.33      noro     12035:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   12036:         FREENDP(l);
                   12037:       }
                   12038:     } else {
1.34      noro     12039:       if ( ms != psugar ) f4step = 1;
                   12040:       else f4step++;
1.33      noro     12041: again2:
                   12042:       psugar = ms;
                   12043:       l = nd_minsugarp_s(d,&d);
                   12044:       sugar = nd_sugarweight?d->sugar2:SG(d);
                   12045:       bucket = create_pbucket();
                   12046:       stat = nd_sp_f4(m,0,l,bucket);
                   12047:       if ( !stat ) {
                   12048:         for ( t = l; NEXT(t); t = NEXT(t) );
                   12049:         NEXT(t) = d; d = l;
                   12050:         d = nd_reconstruct(0,d);
                   12051:         goto again2;
                   12052:       }
                   12053:       if ( bucket->m < 0 ) continue;
                   12054:       col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
                   12055:       if ( !col ) {
1.37      noro     12056:         for ( t = l; NEXT(t); t = NEXT(t) )
                   12057:           ;
                   12058:         NEXT(t) = d; d = l;
1.33      noro     12059:         d = nd_reconstruct(0,d);
                   12060:         goto again2;
                   12061:       }
1.34      noro     12062:       if ( DP_Print ) fprintf(asir_out,"\nsugar=%d,",psugar);
1.33      noro     12063:       nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
                   12064:       /* adding new bases */
                   12065:       for ( r = nflist; r; r = NEXT(r) ) {
                   12066:         nfv = (NDV)BDY(r);
1.38      noro     12067:         if ( nd_f4_td ) SG(nfv) = nd_tdeg(nfv);
1.33      noro     12068:         ndv_removecont(m,nfv);
                   12069:         nh = ndv_newps(m,nfv,0);
                   12070:         d = update_pairs_s(d,nh,syzlist);
                   12071:         nd_sba_pos[nfv->sig->pos] = append_one(nd_sba_pos[nfv->sig->pos],nh);
                   12072:       }
                   12073:       for ( i = 0; i < nd_nbase; i++ )
                   12074:         for ( r = syzlist[i]; r; r = NEXT(r) )
                   12075:             d = remove_spair_s(d,(SIG)BDY(r));
                   12076:       d = remove_large_lcm(d);
                   12077:       if ( DP_Print ) {
1.34      noro     12078:         fprintf(asir_out,"f4red=%d,gblen=%d",f4red,nd_psn); fflush(asir_out);
1.33      noro     12079:       }
                   12080:       f4red++;
1.28      noro     12081:     }
                   12082:   }
                   12083:   if ( DP_Print ) {
1.34      noro     12084:     fprintf(asir_out,"\nnumber of red=%d,",Nf4_red);
1.28      noro     12085:   }
1.30      noro     12086:   g = conv_ilist_s(nd_demand,0,indp);
1.28      noro     12087:   return g;
                   12088: }

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