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

1.54    ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.53 2021/03/12 01:18:33 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 ) {
1.52      noro     2626:     if ( DP_Print ) fprintf(asir_out,"Hilbert driven algorithm.\n");
1.49      noro     2627:     setup_hpdata(&final_hpdata,&current_hpdata);
                   2628:   }
1.6       noro     2629:   while ( d ) {
1.1       noro     2630: again:
1.6       noro     2631:     l = nd_minp(d,&d);
                   2632:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   2633:     if ( SG(l) != sugar ) {
                   2634:       if ( ishomo ) {
                   2635:         diag_count = 0;
                   2636:         stat = do_diagonalize(sugar,m);
1.1       noro     2637:         if ( !stat ) {
1.6       noro     2638:           NEXT(l) = d; d = l;
                   2639:           d = nd_reconstruct(0,d);
                   2640:           goto again;
1.1       noro     2641:         }
1.6       noro     2642:       }
                   2643:       sugar = SG(l);
                   2644:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
1.49      noro     2645:       if ( nd_hpdata ) {
                   2646:         if ( !compp(CO,final_hpdata.hn,current_hpdata.hn) )
                   2647:           break;
                   2648:         else {
                   2649:           final_hpvalue = hpvalue(&final_hpdata,sugar);
                   2650:           if ( final_hpvalue == hpvalue(&current_hpdata,sugar) ) {
1.52      noro     2651: //            if ( DP_Print ) fprintf(asir_out,"done.\n",sugar);
1.49      noro     2652:             d = nd_remove_same_sugar(d,sugar);
                   2653:             continue;
                   2654:           }
                   2655:         }
                   2656:       }
1.6       noro     2657:     }
                   2658:     stat = nd_sp(m,0,l,&h);
                   2659:     if ( !stat ) {
                   2660:       NEXT(l) = d; d = l;
                   2661:       d = nd_reconstruct(0,d);
                   2662:       goto again;
                   2663:     }
1.1       noro     2664: #if USE_GEOBUCKET
1.39      noro     2665:     stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf)
                   2666:       :nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     2667: #else
1.39      noro     2668:     stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     2669: #endif
1.6       noro     2670:     if ( !stat ) {
                   2671:       NEXT(l) = d; d = l;
                   2672:       d = nd_reconstruct(0,d);
                   2673:       goto again;
                   2674:     } else if ( nf ) {
1.41      noro     2675:       Nnfnz++;
1.6       noro     2676:       if ( checkonly || gensyz ) return 0;
1.1       noro     2677:       if ( nd_newelim ) {
                   2678:         if ( nd_module ) {
                   2679:           if ( MPOS(HDL(nf)) > 1 ) return 0;
                   2680:         } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
                   2681:       }
1.6       noro     2682:       if ( DP_Print ) { printf("+"); fflush(stdout); }
                   2683:       hc = HCU(nf);
                   2684:       nd_removecont(m,nf);
                   2685:       if ( !m && nd_nalg ) {
                   2686:         nd_monic(0,&nf);
                   2687:         nd_removecont(m,nf);
                   2688:       }
                   2689:       if ( nd_gentrace ) {
                   2690:         /* exact division */
1.1       noro     2691:         cont = ndc_div(m,hc,HCU(nf));
                   2692:         if ( m || !UNIQ(cont) ) {
1.6       noro     2693:           t = mknode(4,NULLP,NULLP,NULLP,cont);
                   2694:           MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1       noro     2695:           nd_tracelist = t;
                   2696:         }
                   2697:       }
1.6       noro     2698:       nfv = ndtondv(m,nf); nd_free(nf);
1.24      noro     2699:       nh = ndv_newps(m,nfv,0);
1.6       noro     2700:       if ( !m && (ishomo && ++diag_count == diag_period) ) {
                   2701:         diag_count = 0;
                   2702:         stat = do_diagonalize(sugar,m);
                   2703:         if ( !stat ) {
                   2704:           NEXT(l) = d; d = l;
                   2705:           d = nd_reconstruct(1,d);
                   2706:           goto again;
1.1       noro     2707:         }
1.6       noro     2708:       }
1.27      noro     2709: get_eg(&eg1);
1.6       noro     2710:       d = update_pairs(d,g,nh,0);
1.27      noro     2711: get_eg(&eg2); add_eg(&eg_update,&eg1,&eg2);
1.6       noro     2712:       g = update_base(g,nh);
                   2713:       FREENDP(l);
1.49      noro     2714:       if ( nd_hpdata ) {
                   2715:         update_hpdata(&current_hpdata,nh,1);
                   2716:         if ( final_hpvalue == hpvalue(&current_hpdata,sugar) ) {
1.52      noro     2717: //          if ( DP_Print ) fprintf(asir_out,"sugar=%d done.\n",sugar);
1.49      noro     2718:           d = nd_remove_same_sugar(d,sugar);
                   2719:         }
                   2720:       }
1.41      noro     2721:     } else {
                   2722:       Nnfz++;
                   2723:       if ( nd_gentrace && gensyz ) {
                   2724:         nd_tracelist = reverse_node(nd_tracelist);
                   2725:         MKLIST(list,nd_tracelist);
                   2726:         STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
                   2727:         MKNODE(t,list,nd_alltracelist);
                   2728:         nd_alltracelist = t; nd_tracelist = 0;
                   2729:       }
                   2730:       if ( DP_Print ) { printf("."); fflush(stdout); }
                   2731:         FREENDP(l);
                   2732:     }
1.36      noro     2733:   }
                   2734:   conv_ilist(nd_demand,0,g,indp);
1.41      noro     2735:   if ( !checkonly && DP_Print ) {
                   2736:     printf("\nnd_gb done. Nnd_add=%d,Npairs=%d, Nnfnz=%d,Nnfz=%d,",Nnd_add,Npairs,Nnfnz,Nnfz);
                   2737:     printf("Nremoved=%d\n",NcriB+NcriMF+Ncri2);
                   2738:     fflush(asir_out);
                   2739:   }
                   2740:   if ( DP_Print ) {
                   2741:     print_eg("update",&eg_update); fprintf(asir_out,"\n");
                   2742:   }
1.36      noro     2743:   return g;
1.1       noro     2744: }
                   2745:
1.30      noro     2746: ND_pairs update_pairs_s(ND_pairs d,int t,NODE *syz);
1.47      noro     2747: int update_pairs_array_s(ND_pairs *d,int t,NODE *syz);
1.30      noro     2748: ND_pairs nd_newpairs_s(int t ,NODE *syz);
1.47      noro     2749: ND_pairs *nd_newpairs_array_s(int t ,NODE *syz);
1.24      noro     2750:
                   2751: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *nf);
                   2752: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
                   2753:
                   2754: void _copydl(int n,DL d1,DL d2);
                   2755: void _subfromdl(int n,DL d1,DL d2);
                   2756: extern int (*cmpdl)(int n,DL d1,DL d2);
1.53      noro     2757: int _dl_redble_ext(DL,DL,DL,int);
                   2758:
                   2759: int primitive_irred(ND p,SIG sig)
                   2760: {
                   2761:   static int wpd=0,dlen=0;
                   2762:   static DL dquo,squo;
                   2763:   static UINT *quo;
                   2764:   int i;
                   2765:
                   2766:   if ( dlen < nd_nvar ) {
                   2767:     NEWDL(dquo,nd_nvar);
                   2768:     NEWDL(squo,nd_nvar);
                   2769:     dlen = nd_nvar;
                   2770:   }
                   2771:   if ( wpd != nd_wpd ) {
                   2772:     wpd = nd_wpd;
                   2773:     quo = (UINT *)MALLOC(wpd*sizeof(UINT));
                   2774:   }
                   2775:   for ( i = 0; i < nd_psn; i++ ) {
                   2776:     if ( sig->pos == nd_psh[i]->sig->pos &&
                   2777:       _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),squo,nd_nvar) )
                   2778:       if ( ndl_reducible(HDL(p),DL(nd_psh[i])) ) {
                   2779:         if ( DP_Print ) fprintf(asir_out,"D");
                   2780:         ndl_sub(HDL(p),DL(nd_psh[i]),quo);
                   2781:         _ndltodl(quo,dquo);
                   2782:         if ( _eqdl(nd_nvar,squo,dquo) )
                   2783:           return 0;
                   2784:       }
                   2785:   }
                   2786:   return 1;
                   2787: }
1.24      noro     2788:
                   2789: NODE insert_sig(NODE l,SIG s)
                   2790: {
                   2791:   int pos;
                   2792:   DL sig;
                   2793:   struct oNODE root;
                   2794:   NODE p,prev,r;
                   2795:   SIG t;
                   2796:
                   2797:   pos = s->pos; sig = DL(s);
                   2798:   root.next = l; prev = &root;
                   2799:   for ( p = l; p; p = p->next ) {
                   2800:     t = (SIG)p->body;
                   2801:     if ( t->pos == pos ) {
                   2802:       if ( _dl_redble(DL(t),sig,nd_nvar) )
                   2803:         return root.next;
                   2804:       else if ( _dl_redble(sig,DL(t),nd_nvar) )
                   2805:         // remove p
                   2806:         prev->next = p->next;
1.45      noro     2807:       else
                   2808:         prev = p;
1.24      noro     2809:     } else
                   2810:       prev = p;
                   2811:   }
1.27      noro     2812:   NEWNODE(r); r->body = (pointer)s; r->next = 0;
                   2813:   for ( p = &root; p->next; p = p->next );
                   2814:   p->next = r;
                   2815: //  r->next = root.next;
                   2816: //  return r;
                   2817:   return root.next;
1.24      noro     2818: }
                   2819:
                   2820: ND_pairs remove_spair_s(ND_pairs d,SIG sig)
                   2821: {
                   2822:   struct oND_pairs root;
                   2823:   ND_pairs prev,p;
                   2824:   SIG spsig;
                   2825:
                   2826:   root.next = d;
                   2827:   prev = &root; p = d;
                   2828:   while ( p ) {
                   2829:     spsig = p->sig;
1.26      noro     2830:     if ( sig->pos == spsig->pos && _dl_redble(DL(sig),DL(spsig),nd_nvar) ) {
1.24      noro     2831:       // remove p
                   2832:       prev->next = p->next;
1.26      noro     2833:       Nsyz++;
                   2834:     } else
1.24      noro     2835:       prev = p;
                   2836:     p = p->next;
                   2837:   }
                   2838:   return (ND_pairs)root.next;
                   2839: }
                   2840:
1.28      noro     2841: int small_lcm(ND_pairs l)
                   2842: {
                   2843:   SIG sig;
                   2844:   int i;
1.29      noro     2845:   NODE t;
1.28      noro     2846:   static DL lcm,mul,quo;
1.44      noro     2847:   static int nvar = 0;
1.28      noro     2848:
1.38      noro     2849:   if ( nd_sba_largelcm ) return 0;
1.28      noro     2850:   if ( nvar < nd_nvar ) {
                   2851:     nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(quo,nvar); NEWDL(mul,nvar);
                   2852:   }
                   2853:   sig = l->sig;
                   2854:   _ndltodl(l->lcm,lcm);
1.29      noro     2855: #if 0
1.28      noro     2856:   for ( i = 0; i < nd_psn; i++ ) {
                   2857:     if ( sig->pos == nd_psh[i]->sig->pos &&
1.29      noro     2858:       _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
1.28      noro     2859:       _ndltodl(DL(nd_psh[i]),mul);
                   2860:       _addtodl(nd_nvar,quo,mul);
                   2861:       if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
                   2862:         break;
                   2863:     }
                   2864:   }
                   2865:   if ( i < nd_psn ) return 1;
                   2866:   else return 0;
1.29      noro     2867: #else
                   2868:   for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
                   2869:     i = (long)BDY(t);
                   2870:     if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
                   2871:       _ndltodl(DL(nd_psh[i]),mul);
                   2872:       _addtodl(nd_nvar,quo,mul);
                   2873:       if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
                   2874:         break;
                   2875:     }
                   2876:   }
                   2877:   if ( t ) return 1;
                   2878:   else return 0;
                   2879: #endif
1.28      noro     2880: }
                   2881:
1.41      noro     2882: ND_pairs find_smallest_lcm(ND_pairs l)
                   2883: {
                   2884:   SIG sig;
                   2885:   int i,minindex;
                   2886:   NODE t;
                   2887:   ND_pairs r;
                   2888:   struct oSIG sig1;
                   2889:   static DL mul,quo,minlm;
1.44      noro     2890:   static int nvar = 0;
1.41      noro     2891:
                   2892:   if ( nvar < nd_nvar ) {
                   2893:     nvar = nd_nvar;
                   2894:     NEWDL(quo,nvar); NEWDL(mul,nvar);
                   2895:     NEWDL(minlm,nvar);
                   2896:   }
                   2897:   sig = l->sig;
                   2898:   // find mg s.t. m*s(g)=sig and m*lm(g) is minimal
                   2899:   _ndltodl(l->lcm,minlm); minindex = -1;
                   2900:   for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
                   2901:     i = (long)BDY(t);
                   2902:     if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
                   2903:       _ndltodl(DL(nd_psh[i]),mul);
                   2904:       _addtodl(nd_nvar,quo,mul);
                   2905:       if ( (*cmpdl)(nd_nvar,minlm,mul) > 0 ) {
                   2906:         minindex = i;
1.47      noro     2907:         break;
1.41      noro     2908:         _copydl(nd_nvar,mul,minlm);
                   2909:       }
                   2910:     }
                   2911:   }
                   2912:   // l->lcm is minimal; return l itself
                   2913:   if ( minindex < 0 ) return l;
1.47      noro     2914:   else return 0;
1.41      noro     2915:   for ( i = 0; i < nd_psn; i++ ) {
                   2916:     if ( i == minindex ) continue;
                   2917:     _ndltodl(DL(nd_psh[i]),mul);
                   2918:     if ( _dl_redble_ext(mul,minlm,quo,nd_nvar) ) {
                   2919:       _addtodl(nd_nvar,nd_ps[i]->sig->dl,quo);
                   2920:       sig1.pos = nd_ps[i]->sig->pos;
                   2921:       sig1.dl = quo;
                   2922:       if ( comp_sig(sig,&sig1) > 0 ) {
                   2923: //        printf("X");
                   2924:         NEWND_pairs(r);
                   2925:         r->sig = sig;
                   2926:         r->i1 = minindex;
                   2927:         r->i2 = i;
                   2928:         dltondl(nd_nvar,minlm,r->lcm);
                   2929:         r->next = 0;
                   2930:         return r;
                   2931:       }
                   2932:     }
                   2933:   }
                   2934:   // there is no suitable spair
                   2935:   return 0;
                   2936: }
                   2937:
1.28      noro     2938: ND_pairs remove_large_lcm(ND_pairs d)
                   2939: {
                   2940:   struct oND_pairs root;
                   2941:   ND_pairs prev,p;
                   2942:
                   2943:   root.next = d;
                   2944:   prev = &root; p = d;
                   2945:   while ( p ) {
1.41      noro     2946: #if 0
1.28      noro     2947:     if ( small_lcm(p) ) {
                   2948:       // remove p
                   2949:       prev->next = p->next;
                   2950:     } else
1.41      noro     2951: #else
                   2952:     if ( find_smallest_lcm(p) == 0 ) {
                   2953:       // remove p
                   2954:       prev->next = p->next;
                   2955:     } else
                   2956: #endif
1.28      noro     2957:       prev = p;
                   2958:     p = p->next;
                   2959:   }
                   2960:   return (ND_pairs)root.next;
                   2961: }
                   2962:
1.26      noro     2963: struct oEGT eg_create,eg_newpairs,eg_merge;
                   2964:
1.30      noro     2965: NODE conv_ilist_s(int demand,int trace,int **indp);
                   2966:
1.44      noro     2967: // S(fj*ei-fi*ej)
                   2968:
1.45      noro     2969: void _subdl(int,DL,DL,DL);
                   2970:
1.44      noro     2971: SIG trivial_sig(int i,int j)
                   2972: {
1.45      noro     2973:   static DL lcm;
1.44      noro     2974:   static struct oSIG sigi,sigj;
                   2975:   static int nvar = 0;
                   2976:   SIG sig;
                   2977:
                   2978:   if ( nvar != nd_nvar ) {
1.45      noro     2979:     nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(sigi.dl,nvar); NEWDL(sigj.dl,nvar);
                   2980:   }
1.46      noro     2981:   if ( nd_sba_inputisgb != 0 ) {
1.45      noro     2982:     lcm_of_DL(nd_nvar,nd_sba_hm[i],nd_sba_hm[j],lcm);
                   2983:     sigi.pos = i; _subdl(nd_nvar,lcm,nd_sba_hm[i],sigi.dl);
                   2984:     sigj.pos = j; _subdl(nd_nvar,lcm,nd_sba_hm[j],sigj.dl);
                   2985:     if ( comp_sig(&sigi,&sigj) > 0 ) sig = dup_sig(&sigi);
                   2986:     else sig = dup_sig(&sigj);
                   2987:   } else {
                   2988:     sigi.pos = i; _copydl(nd_nvar,nd_sba_hm[j],sigi.dl);
                   2989:     sigj.pos = j; _copydl(nd_nvar,nd_sba_hm[i],sigj.dl);
                   2990:     if ( comp_sig(&sigi,&sigj) > 0 ) sig = dup_sig(&sigi);
                   2991:     else sig = dup_sig(&sigj);
1.44      noro     2992:   }
                   2993:   return sig;
                   2994: }
                   2995:
1.47      noro     2996: int nd_minsig(ND_pairs *d)
                   2997: {
                   2998:   int min,i,ret;
                   2999:
                   3000:   min = -1;
                   3001:   for ( i = 0; i < nd_nbase; i++ ) {
                   3002:     if ( d[i] != 0 ) {
                   3003:       if ( min < 0 ) min = i;
                   3004:       else {
                   3005:         ret = comp_sig(d[i]->sig,d[min]->sig);
                   3006:         if ( ret < 0 ) min = i;
                   3007:       }
                   3008:     }
                   3009:   }
                   3010:   return min;
                   3011: }
                   3012:
                   3013: int dlength(ND_pairs d)
                   3014: {
                   3015:   int i;
                   3016:   for ( i = 0; d; d = d->next, i++ );
                   3017:   return i;
                   3018: }
                   3019:
1.44      noro     3020: NODE nd_sba_buch(int m,int ishomo,int **indp,NODE *syzp)
1.24      noro     3021: {
1.39      noro     3022:   int i,j,nh,sugar,stat,pos;
1.30      noro     3023:   NODE r,t,g;
1.47      noro     3024:   ND_pairs *d;
1.41      noro     3025:   ND_pairs l,l1;
1.24      noro     3026:   ND h,nf,s,head,nf1;
                   3027:   NDV nfv;
                   3028:   Z q;
                   3029:   union oNDC dn,hc;
                   3030:   P cont;
                   3031:   LIST list;
                   3032:   SIG sig;
1.29      noro     3033:   NODE *syzlist;
1.47      noro     3034:   int ngen,ind;
1.41      noro     3035:   int Nnominimal,Nredundant;
1.28      noro     3036:   DL lcm,quo,mul;
1.49      noro     3037:   struct oHPDATA final_hpdata,current_hpdata;
1.50      noro     3038:   struct oEGT eg1,eg2,eg3,eg4,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
                   3039:   struct oEGT eg_minsig,eg_smallest,eg_removecont,eg_hpdata,eg_updatepairs,eg_sbabuch,eg_sp;
1.47      noro     3040:   int Nnfs=0,Nnfz=0,Nnfnz=0,dlen,nsyz;
1.24      noro     3041:
1.27      noro     3042: init_eg(&eg_remove);
1.29      noro     3043:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.47      noro     3044:   d = (ND_pairs *)MALLOC(nd_psn*sizeof(ND_pairs));
                   3045:   nd_nbase = nd_psn;
1.26      noro     3046:   Nsyz = 0;
1.24      noro     3047:   Nnd_add = 0;
1.41      noro     3048:   Nnominimal = 0;
1.26      noro     3049:   Nredundant = 0;
1.44      noro     3050:   ngen = nd_psn;
1.48      noro     3051:   if ( !do_weyl ) {
                   3052:     for ( i = 0; i < nd_psn; i++ )
                   3053:       for ( j = i+1; j < nd_psn; j++ ) {
                   3054:         sig = trivial_sig(i,j);
                   3055:         syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
                   3056:       }
1.29      noro     3057:     }
1.47      noro     3058:   dlen = 0;
1.41      noro     3059:   for ( i = 0; i < nd_psn; i++ ) {
1.47      noro     3060:     dlen += update_pairs_array_s(d,i,syzlist);
1.41      noro     3061:   }
1.24      noro     3062:   sugar = 0;
1.39      noro     3063:   pos = 0;
1.49      noro     3064:   if ( nd_hpdata ) {
                   3065:     setup_hpdata(&final_hpdata,&current_hpdata);
                   3066:   }
1.26      noro     3067:   NEWDL(lcm,nd_nvar); NEWDL(quo,nd_nvar); NEWDL(mul,nd_nvar);
1.50      noro     3068: init_eg(&eg_sp);
1.26      noro     3069: init_eg(&eg_create);
                   3070: init_eg(&eg_merge);
1.47      noro     3071: init_eg(&eg_minsig);
                   3072: init_eg(&eg_smallest);
1.29      noro     3073: init_eg(&eg_large);
1.31      noro     3074: init_eg(&eg_nf);
                   3075: init_eg(&eg_nfzero);
1.50      noro     3076: init_eg(&eg_removecont);
                   3077: init_eg(&eg_updatepairs);
                   3078: init_eg(&eg_hpdata);
                   3079: init_eg(&eg_sbabuch);
                   3080: get_eg(&eg3);
1.47      noro     3081:   while ( 1 ) {
1.53      noro     3082:     if ( DP_Print && !nd_hpdata && dlen%1000 == 0 ) fprintf(asir_out,"(%d)",dlen);
1.47      noro     3083: again :
                   3084: get_eg(&eg1);
                   3085:     ind = nd_minsig(d);
                   3086: get_eg(&eg2); add_eg(&eg_minsig,&eg1,&eg2);
                   3087:     if ( ind < 0 ) break;
                   3088:     l = d[ind];
                   3089: //    printf("(%d,%d)",l->i1,l->i2); print_sig(l->sig); printf("\n");
                   3090: get_eg(&eg1);
1.41      noro     3091:     l1 = find_smallest_lcm(l);
1.47      noro     3092: get_eg(&eg2); add_eg(&eg_smallest,&eg1,&eg2);
1.41      noro     3093:     if ( l1 == 0 ) {
1.47      noro     3094:       d[ind] = d[ind]->next; dlen--;
1.53      noro     3095: //      if ( DP_Print && !nd_hpdata ) fprintf(asir_out,"M");
1.41      noro     3096:       Nnominimal++;
                   3097:       continue;
                   3098:     }
                   3099:     if ( SG(l1) != sugar ) {
                   3100:       sugar = SG(l1);
                   3101:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   3102:     }
                   3103:     sig = l1->sig;
                   3104:     if ( DP_Print && nd_sba_pot ) {
                   3105:       if ( sig->pos != pos ) {
                   3106:         fprintf(asir_out,"[%d]",sig->pos);
                   3107:         pos = sig->pos;
                   3108:       }
                   3109:     }
1.50      noro     3110: get_eg(&eg1);
1.41      noro     3111:     stat = nd_sp(m,0,l1,&h);
1.50      noro     3112: get_eg(&eg2); add_eg(&eg_sp,&eg1,&eg2);
1.24      noro     3113:     if ( !stat ) {
1.47      noro     3114:       nd_reconstruct_s(0,d);
1.24      noro     3115:       goto again;
                   3116:     }
1.31      noro     3117: get_eg(&eg1);
1.25      noro     3118: #if USE_GEOBUCKET
1.39      noro     3119:     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     3120: #else
1.39      noro     3121:     stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.24      noro     3122: #endif
1.31      noro     3123: get_eg(&eg2);
1.24      noro     3124:     if ( !stat ) {
1.47      noro     3125:       nd_reconstruct_s(0,d);
1.24      noro     3126:       goto again;
                   3127:     } else if ( stat == -1 ) {
1.47      noro     3128:       d[ind] = d[ind]->next; dlen--;
1.41      noro     3129:       Nnfs++;
1.24      noro     3130:       if ( DP_Print ) { printf("S"); fflush(stdout); }
                   3131:     } else if ( nf ) {
1.47      noro     3132:       d[ind] = d[ind]->next; dlen--;
1.41      noro     3133:       Nnfnz++;
                   3134:       if ( DP_Print ) {
                   3135:         if ( nd_sba_redundant_check ) {
                   3136:           if ( ndl_find_reducer_nonsig(HDL(nf)) >= 0 ) {
                   3137:             Nredundant++;
                   3138:             printf("R");
                   3139:           } else
                   3140:             printf("+");
                   3141:         } else
                   3142:           printf("+");
                   3143:         fflush(stdout);
                   3144:       }
1.31      noro     3145:       add_eg(&eg_nf,&eg1,&eg2);
1.24      noro     3146:       hc = HCU(nf);
1.50      noro     3147:       get_eg(&eg1);
1.24      noro     3148:       nd_removecont(m,nf);
1.50      noro     3149:       get_eg(&eg2); add_eg(&eg_removecont,&eg1,&eg2);
1.24      noro     3150:       nfv = ndtondv(m,nf); nd_free(nf);
                   3151:       nh = ndv_newps(m,nfv,0);
1.53      noro     3152:
1.50      noro     3153:       get_eg(&eg1);
1.47      noro     3154:       dlen += update_pairs_array_s(d,nh,syzlist);
1.50      noro     3155:       get_eg(&eg2); add_eg(&eg_updatepairs,&eg1,&eg2);
1.29      noro     3156:       nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.49      noro     3157:       if ( nd_hpdata ) {
1.50      noro     3158:         get_eg(&eg1);
1.49      noro     3159:         update_hpdata(&current_hpdata,nh,0);
1.50      noro     3160:         get_eg(&eg2); add_eg(&eg_hpdata,&eg1,&eg2);
1.49      noro     3161:         if ( !compp(CO,final_hpdata.hn,current_hpdata.hn) ) {
                   3162:           if ( DP_Print ) { printf("\nWe found a gb.\n"); }
1.53      noro     3163:             break;
1.49      noro     3164:         }
                   3165:       }
1.24      noro     3166:    } else {
1.47      noro     3167:       d[ind] = d[ind]->next; dlen--;
1.41      noro     3168:       Nnfz++;
1.31      noro     3169:       add_eg(&eg_nfzero,&eg1,&eg2);
1.24      noro     3170:      // syzygy
1.27      noro     3171: get_eg(&eg1);
1.47      noro     3172:      nsyz = Nsyz;
                   3173:      d[sig->pos] = remove_spair_s(d[sig->pos],sig);
                   3174:      dlen -= Nsyz-nsyz;
1.27      noro     3175: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29      noro     3176:      syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.24      noro     3177:      if ( DP_Print ) { printf("."); fflush(stdout); }
                   3178:    }
                   3179:  }
1.50      noro     3180:  get_eg(&eg4); add_eg(&eg_sbabuch,&eg3,&eg4);
1.30      noro     3181:  g = conv_ilist_s(nd_demand,0,indp);
1.26      noro     3182:  if ( DP_Print ) {
1.47      noro     3183:    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     3184:    printf("Nnfnz=%d,Nnfz=%d,Nnfsingular=%d\n",Nnfnz,Nnfz,Nnfs);
1.50      noro     3185:    fflush(stdout);
1.41      noro     3186:    if ( nd_sba_redundant_check )
                   3187:    printf("Nredundant=%d\n",Nredundant);
1.50      noro     3188:    fflush(stdout);
                   3189:    print_eg("sp",&eg_sp);
1.26      noro     3190:    print_eg("create",&eg_create);
                   3191:    print_eg("merge",&eg_merge);
1.47      noro     3192:    print_eg("minsig",&eg_minsig);
                   3193:    print_eg("smallest",&eg_smallest);
                   3194:    print_eg("remove",&eg_remove);
1.50      noro     3195:    printf("\n");
1.47      noro     3196:    print_eg("nf",&eg_nf);
                   3197:    print_eg("nfzero",&eg_nfzero);
1.50      noro     3198:    print_eg("removecont",&eg_removecont);
                   3199:    print_eg("updatepairs",&eg_updatepairs);
                   3200:    print_eg("hpdata",&eg_hpdata);
                   3201:    print_eg("total",&eg_sbabuch);
1.47      noro     3202:    printf("\n");
                   3203:  }
                   3204:  if ( nd_sba_syz ) {
1.27      noro     3205:    print_eg("remove",&eg_remove);
1.31      noro     3206:    print_eg("nf",&eg_nf);
                   3207:    print_eg("nfzero",&eg_nfzero);
1.27      noro     3208:    printf("\n");
1.26      noro     3209:  }
1.44      noro     3210:  if ( nd_sba_syz ) {
                   3211:    NODE hsyz,tsyz,prev;
                   3212:
                   3213:    hsyz = 0;
                   3214:    for ( i = 0; i < ngen; i++ ) {
                   3215:      tsyz = syzlist[i];
                   3216:      for ( prev = 0; tsyz != 0; prev = tsyz, tsyz = NEXT(tsyz))
                   3217:        BDY(tsyz) = (pointer)sigtodpm((SIG)BDY(tsyz));
                   3218:      if ( prev != 0 ) {
                   3219:        prev->next = hsyz; hsyz = syzlist[i];
                   3220:      }
                   3221:    }
                   3222:    *syzp = hsyz;
                   3223:  } else *syzp = 0;
1.24      noro     3224:  return g;
                   3225: }
                   3226:
1.1       noro     3227: /* splist = [[i1,i2],...] */
                   3228:
                   3229: int check_splist(int m,NODE splist)
                   3230: {
                   3231:   NODE t,p;
                   3232:   ND_pairs d,r,l;
                   3233:   int stat;
                   3234:   ND h,nf;
                   3235:
                   3236:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   3237:     p = BDY((LIST)BDY(t));
1.6       noro     3238:     NEXTND_pairs(d,r);
                   3239:     r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
                   3240:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     3241:     SG(r) = TD(LCM(r)); /* XXX */
                   3242:   }
                   3243:   if ( d ) NEXT(r) = 0;
                   3244:
1.6       noro     3245:   while ( d ) {
1.1       noro     3246: again:
1.6       noro     3247:     l = nd_minp(d,&d);
                   3248:     stat = nd_sp(m,0,l,&h);
                   3249:     if ( !stat ) {
                   3250:       NEXT(l) = d; d = l;
                   3251:       d = nd_reconstruct(0,d);
                   3252:       goto again;
                   3253:     }
1.39      noro     3254:     stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.6       noro     3255:     if ( !stat ) {
                   3256:       NEXT(l) = d; d = l;
                   3257:       d = nd_reconstruct(0,d);
                   3258:       goto again;
                   3259:     } else if ( nf ) return 0;
1.1       noro     3260:     if ( DP_Print) { printf("."); fflush(stdout); }
1.6       noro     3261:   }
1.1       noro     3262:   if ( DP_Print) { printf("done.\n"); fflush(stdout); }
                   3263:   return 1;
                   3264: }
                   3265:
                   3266: int check_splist_f4(int m,NODE splist)
                   3267: {
                   3268:   UINT *s0vect;
1.6       noro     3269:   PGeoBucket bucket;
1.1       noro     3270:   NODE p,rp0,t;
                   3271:   ND_pairs d,r,l,ll;
                   3272:   int col,stat;
                   3273:
                   3274:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   3275:     p = BDY((LIST)BDY(t));
1.6       noro     3276:     NEXTND_pairs(d,r);
                   3277:     r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
                   3278:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     3279:     SG(r) = TD(LCM(r)); /* XXX */
                   3280:   }
                   3281:   if ( d ) NEXT(r) = 0;
                   3282:
1.6       noro     3283:   while ( d ) {
                   3284:     l = nd_minsugarp(d,&d);
                   3285:     bucket = create_pbucket();
                   3286:     stat = nd_sp_f4(m,0,l,bucket);
                   3287:     if ( !stat ) {
                   3288:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   3289:       NEXT(ll) = d; d = l;
                   3290:       d = nd_reconstruct(0,d);
                   3291:       continue;
                   3292:     }
                   3293:     if ( bucket->m < 0 ) continue;
                   3294:     col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   3295:     if ( !col ) {
                   3296:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   3297:       NEXT(ll) = d; d = l;
                   3298:       d = nd_reconstruct(0,d);
                   3299:       continue;
1.1       noro     3300:     }
1.6       noro     3301:     if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
                   3302:   }
                   3303:   return 1;
1.1       noro     3304: }
                   3305:
                   3306: int do_diagonalize_trace(int sugar,int m)
                   3307: {
1.6       noro     3308:   int i,nh,stat;
                   3309:   NODE r,g,t;
                   3310:   ND h,nf,nfq,s,head;
                   3311:   NDV nfv,nfqv;
                   3312:   Q q,den,num;
                   3313:   union oNDC hc;
                   3314:   NODE node;
                   3315:   LIST l;
                   3316:   Z iq;
                   3317:   P cont,cont1;
1.1       noro     3318:
1.6       noro     3319:   for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   3320:     if ( nd_gentrace ) {
                   3321:         /* Trace = [1,index,1,1] */
                   3322:         STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   3323:         MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   3324:     }
                   3325:     /* for nd_ps */
                   3326:     s = ndvtond(m,nd_ps[i]);
                   3327:     s = nd_separate_head(s,&head);
                   3328:     stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
                   3329:     if ( !stat ) return 0;
                   3330:     nf = nd_add(m,head,nf);
                   3331:     ndv_free(nd_ps[i]);
                   3332:     nd_ps[i] = ndtondv(m,nf);
                   3333:     nd_free(nf);
                   3334:
                   3335:     /* for nd_ps_trace */
                   3336:     if ( nd_demand )
                   3337:         nfv = ndv_load(i);
                   3338:     else
                   3339:         nfv = nd_ps_trace[i];
                   3340:     s = ndvtond(0,nfv);
                   3341:     s = nd_separate_head(s,&head);
                   3342:     stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
                   3343:     if ( !stat ) return 0;
                   3344:     ndv_free(nfv);
                   3345:     hc = HCU(nf); nd_removecont(0,nf);
                   3346:     /* exact division */
1.1       noro     3347:     cont = ndc_div(0,hc,HCU(nf));
1.6       noro     3348:     if ( nd_gentrace ) finalize_tracelist(i,cont);
                   3349:     nfv = ndtondv(0,nf);
                   3350:     nd_free(nf);
                   3351:     nd_bound[i] = ndv_compute_bound(nfv);
                   3352:     register_hcf(nfv);
                   3353:     if ( nd_demand ) {
                   3354:     ndv_save(nfv,i);
                   3355:     ndv_free(nfv);
                   3356:     } else
                   3357:     nd_ps_trace[i] = nfv;
                   3358:   }
                   3359:   return 1;
1.1       noro     3360: }
                   3361:
                   3362: static struct oEGT eg_invdalg;
                   3363: struct oEGT eg_le;
                   3364:
                   3365: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
                   3366: {
                   3367:     NODE tn;
                   3368:     P p1;
                   3369:
                   3370:     for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
                   3371:         substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
                   3372:     }
                   3373:     *r = p;
                   3374: }
                   3375:
                   3376: NODE nd_gb_trace(int m,int ishomo,int **indp)
                   3377: {
1.6       noro     3378:   int i,nh,sugar,stat;
                   3379:   NODE r,g,t;
                   3380:   ND_pairs d;
                   3381:   ND_pairs l;
                   3382:   ND h,nf,nfq,s,head;
                   3383:   NDV nfv,nfqv;
                   3384:   Z q,den,num;
                   3385:   P hc;
                   3386:   union oNDC dn,hnfq;
                   3387:   struct oEGT eg_monic,egm0,egm1;
                   3388:   int diag_count = 0;
                   3389:   P cont;
                   3390:   LIST list;
1.49      noro     3391:   struct oHPDATA current_hpdata,final_hpdata;
                   3392:   int final_hpvalue;
1.6       noro     3393:
                   3394:   init_eg(&eg_monic);
                   3395:   init_eg(&eg_invdalg);
                   3396:   init_eg(&eg_le);
                   3397:   g = 0; d = 0;
                   3398:   for ( i = 0; i < nd_psn; i++ ) {
                   3399:     d = update_pairs(d,g,i,0);
                   3400:     g = update_base(g,i);
                   3401:   }
                   3402:   sugar = 0;
1.49      noro     3403:   if ( nd_hpdata ) {
1.52      noro     3404:     if ( DP_Print ) fprintf(asir_out,"Hilbert driven algorithm.\n");
1.49      noro     3405:     setup_hpdata(&final_hpdata,&current_hpdata);
                   3406:   }
                   3407:
1.6       noro     3408:   while ( d ) {
1.1       noro     3409: again:
1.6       noro     3410:     l = nd_minp(d,&d);
                   3411:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   3412:     if ( SG(l) != sugar ) {
1.1       noro     3413: #if 1
1.6       noro     3414:       if ( ishomo ) {
                   3415:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3416:         stat = do_diagonalize_trace(sugar,m);
                   3417:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3418:         diag_count = 0;
1.1       noro     3419:         if ( !stat ) {
1.6       noro     3420:           NEXT(l) = d; d = l;
                   3421:           d = nd_reconstruct(1,d);
                   3422:           goto again;
1.1       noro     3423:         }
1.6       noro     3424:       }
                   3425: #endif
                   3426:       sugar = SG(l);
                   3427:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
1.49      noro     3428:       if ( nd_hpdata ) {
                   3429:         if ( !compp(CO,final_hpdata.hn,current_hpdata.hn) )
                   3430:           break;
                   3431:         else {
                   3432:           final_hpvalue = hpvalue(&final_hpdata,sugar);
                   3433:           if ( final_hpvalue == hpvalue(&current_hpdata,sugar) ) {
1.52      noro     3434: //            if ( DP_Print ) fprintf(asir_out,"sugar=%d done.\n",sugar);
1.49      noro     3435:             d = nd_remove_same_sugar(d,sugar);
                   3436:             continue;
                   3437:           }
                   3438:         }
                   3439:       }
1.6       noro     3440:     }
                   3441:     stat = nd_sp(m,0,l,&h);
                   3442:     if ( !stat ) {
                   3443:       NEXT(l) = d; d = l;
                   3444:       d = nd_reconstruct(1,d);
                   3445:       goto again;
                   3446:     }
1.1       noro     3447: #if USE_GEOBUCKET
1.39      noro     3448:     stat = nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     3449: #else
1.39      noro     3450:     stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     3451: #endif
1.6       noro     3452:     if ( !stat ) {
                   3453:       NEXT(l) = d; d = l;
                   3454:       d = nd_reconstruct(1,d);
                   3455:       goto again;
                   3456:     } else if ( nf ) {
                   3457:       if ( nd_demand ) {
                   3458:         nfqv = ndv_load(nd_psn);
                   3459:         nfq = ndvtond(0,nfqv);
                   3460:       } else
                   3461:         nfq = 0;
                   3462:       if ( !nfq ) {
1.39      noro     3463:         if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!nd_top&&!Top,&nfq) ) {
1.6       noro     3464:           NEXT(l) = d; d = l;
                   3465:           d = nd_reconstruct(1,d);
                   3466:           goto again;
                   3467:         }
                   3468:       }
                   3469:       if ( nfq ) {
                   3470:         /* m|HC(nfq) => failure */
                   3471:         if ( nd_vc ) {
                   3472:           nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
                   3473:         } else
                   3474:           q = HCZ(nfq);
                   3475:         if ( !remqi((Q)q,m) ) return 0;
                   3476:
                   3477:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   3478:         hnfq = HCU(nfq);
                   3479:         if ( nd_nalg ) {
                   3480:           /* m|DN(HC(nf)^(-1)) => failure */
                   3481:           get_eg(&egm0);
                   3482:           if ( !nd_monic(m,&nfq) ) return 0;
                   3483:           get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
                   3484:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   3485:           nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
                   3486:         } else {
                   3487:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   3488:           nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
                   3489:         }
                   3490:         if ( nd_gentrace ) {
                   3491:           /* exact division */
                   3492:           cont = ndc_div(0,hnfq,HCU(nfqv));
                   3493:           if ( !UNIQ(cont) ) {
                   3494:             t = mknode(4,NULLP,NULLP,NULLP,cont);
                   3495:             MKLIST(list,t); MKNODE(t,list,nd_tracelist);
                   3496:             nd_tracelist = t;
                   3497:           }
                   3498:         }
1.24      noro     3499:         nh = ndv_newps(0,nfv,nfqv);
1.6       noro     3500:         if ( ishomo && ++diag_count == diag_period ) {
                   3501:           diag_count = 0;
                   3502:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3503:           stat = do_diagonalize_trace(sugar,m);
                   3504:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3505:           if ( !stat ) {
1.1       noro     3506:             NEXT(l) = d; d = l;
                   3507:             d = nd_reconstruct(1,d);
                   3508:             goto again;
1.6       noro     3509:           }
1.1       noro     3510:         }
1.6       noro     3511:         d = update_pairs(d,g,nh,0);
                   3512:         g = update_base(g,nh);
1.49      noro     3513:         if ( nd_hpdata ) {
                   3514:           update_hpdata(&current_hpdata,nh,1);
                   3515:           if ( final_hpvalue == hpvalue(&current_hpdata,sugar) ) {
1.52      noro     3516: //            if ( DP_Print ) fprintf(asir_out,"sugar=%d done.\n",sugar);
1.49      noro     3517:             d = nd_remove_same_sugar(d,sugar);
                   3518:           }
                   3519:         }
1.6       noro     3520:       } else {
                   3521:         if ( DP_Print ) { printf("*"); fflush(stdout); }
                   3522:       }
                   3523:     } else {
                   3524:       if ( DP_Print ) { printf("."); fflush(stdout); }
1.1       noro     3525:     }
1.6       noro     3526:     FREENDP(l);
                   3527:   }
                   3528:   if ( nd_nalg ) {
                   3529:     if ( DP_Print ) {
                   3530:       print_eg("monic",&eg_monic);
                   3531:       print_eg("invdalg",&eg_invdalg);
                   3532:       print_eg("le",&eg_le);
1.1       noro     3533:     }
1.6       noro     3534:   }
1.1       noro     3535:   conv_ilist(nd_demand,1,g,indp);
1.41      noro     3536:   if ( DP_Print ) { printf("\nnd_gb_trace done.\n"); fflush(stdout); }
1.6       noro     3537:   return g;
1.1       noro     3538: }
                   3539:
                   3540: int ndv_compare(NDV *p1,NDV *p2)
                   3541: {
                   3542:     return DL_COMPARE(HDL(*p1),HDL(*p2));
                   3543: }
                   3544:
                   3545: int ndv_compare_rev(NDV *p1,NDV *p2)
                   3546: {
                   3547:     return -DL_COMPARE(HDL(*p1),HDL(*p2));
                   3548: }
                   3549:
                   3550: int ndvi_compare(NDVI p1,NDVI p2)
                   3551: {
                   3552:     return DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3553: }
                   3554:
                   3555: int ndvi_compare_rev(NDVI p1,NDVI p2)
                   3556: {
                   3557:     return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3558: }
                   3559:
                   3560: NODE ndv_reduceall(int m,NODE f)
                   3561: {
                   3562:   int i,j,n,stat;
                   3563:   ND nf,g,head;
                   3564:   NODE t,a0,a;
                   3565:   union oNDC dn;
                   3566:   Q q,num,den;
                   3567:   NODE node;
                   3568:   LIST l;
                   3569:   Z iq,jq;
                   3570:   int *perm;
                   3571:   union oNDC hc;
                   3572:   P cont,cont1;
                   3573:
                   3574:   if ( nd_nora ) return f;
                   3575:   n = length(f);
1.24      noro     3576:   ndv_setup(m,0,f,0,1,0);
1.1       noro     3577:   perm = (int *)MALLOC(n*sizeof(int));
                   3578:   if ( nd_gentrace ) {
                   3579:     for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6       noro     3580:       perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1       noro     3581:   }
                   3582:   for ( i = 0; i < n; ) {
                   3583:     if ( nd_gentrace ) {
                   3584:       /* Trace = [1,index,1,1] */
1.6       noro     3585:       STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1       noro     3586:       MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   3587:     }
                   3588:     g = ndvtond(m,nd_ps[i]);
                   3589:     g = nd_separate_head(g,&head);
1.6       noro     3590:     stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1       noro     3591:     if ( !stat )
                   3592:       nd_reconstruct(0,0);
                   3593:     else {
                   3594:       if ( DP_Print ) { printf("."); fflush(stdout); }
                   3595:       ndv_free(nd_ps[i]);
                   3596:       hc = HCU(nf); nd_removecont(m,nf);
                   3597:       if ( nd_gentrace ) {
                   3598:         for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6       noro     3599:           jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
                   3600:           STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1       noro     3601:         }
1.6       noro     3602:         /* exact division */
1.1       noro     3603:         cont = ndc_div(m,hc,HCU(nf));
                   3604:         finalize_tracelist(perm[i],cont);
                   3605:       }
                   3606:       nd_ps[i] = ndtondv(m,nf); nd_free(nf);
                   3607:       nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   3608:       i++;
                   3609:     }
                   3610:   }
                   3611:   if ( DP_Print ) { printf("\n"); }
                   3612:   for ( a0 = 0, i = 0; i < n; i++ ) {
                   3613:     NEXTNODE(a0,a);
                   3614:     if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
                   3615:     else {
                   3616:       for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
                   3617:       BDY(a) = (pointer)nd_ps[j];
                   3618:     }
                   3619:   }
                   3620:   NEXT(a) = 0;
                   3621:   return a0;
                   3622: }
                   3623:
1.41      noro     3624: int ndplength(ND_pairs d)
                   3625: {
                   3626:   int i;
                   3627:   for ( i = 0; d; i++ ) d = NEXT(d);
                   3628:   return i;
                   3629: }
                   3630:
1.1       noro     3631: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
                   3632: {
                   3633:   ND_pairs d1,nd,cur,head,prev,remove;
1.41      noro     3634:   int len0;
1.1       noro     3635:
                   3636:   if ( !g ) return d;
                   3637:   /* for testing */
                   3638:   if ( gensyz && nd_gensyz == 2 ) {
                   3639:     d1 = nd_newpairs(g,t);
                   3640:     if ( !d )
                   3641:       return d1;
                   3642:     else {
                   3643:       nd = d;
                   3644:       while ( NEXT(nd) ) nd = NEXT(nd);
                   3645:       NEXT(nd) = d1;
                   3646:       return d;
                   3647:     }
                   3648:   }
                   3649:   d = crit_B(d,t);
                   3650:   d1 = nd_newpairs(g,t);
1.41      noro     3651:   len0 = ndplength(d1);
1.1       noro     3652:   d1 = crit_M(d1);
                   3653:   d1 = crit_F(d1);
1.41      noro     3654:   NcriMF += len0-ndplength(d1);
1.1       noro     3655:   if ( gensyz || do_weyl )
                   3656:     head = d1;
                   3657:   else {
                   3658:     prev = 0; cur = head = d1;
                   3659:     while ( cur ) {
                   3660:       if ( crit_2( cur->i1,cur->i2 ) ) {
                   3661:         remove = cur;
                   3662:         if ( !prev ) head = cur = NEXT(cur);
                   3663:         else cur = NEXT(prev) = NEXT(cur);
1.41      noro     3664:         FREENDP(remove); Ncri2++;
1.1       noro     3665:       } else {
                   3666:         prev = cur; cur = NEXT(cur);
                   3667:       }
                   3668:     }
                   3669:   }
                   3670:   if ( !d )
                   3671:     return head;
                   3672:   else {
                   3673:     nd = d;
                   3674:     while ( NEXT(nd) ) nd = NEXT(nd);
                   3675:     NEXT(nd) = head;
                   3676:     return d;
                   3677:   }
                   3678: }
                   3679:
1.24      noro     3680: ND_pairs merge_pairs_s(ND_pairs d,ND_pairs d1);
                   3681:
1.30      noro     3682: ND_pairs update_pairs_s( ND_pairs d, int t,NODE *syz)
1.24      noro     3683: {
                   3684:   ND_pairs d1;
1.26      noro     3685:   struct oEGT eg1,eg2,eg3;
1.24      noro     3686:
1.30      noro     3687:   if ( !t ) return d;
1.26      noro     3688: get_eg(&eg1);
1.30      noro     3689:   d1 = nd_newpairs_s(t,syz);
1.26      noro     3690: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
1.24      noro     3691:   d = merge_pairs_s(d,d1);
1.26      noro     3692: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
1.24      noro     3693:   return d;
                   3694: }
1.1       noro     3695:
1.47      noro     3696: int update_pairs_array_s( ND_pairs *d, int t,NODE *syz)
                   3697: {
                   3698:   ND_pairs *d1;
                   3699:   struct oEGT eg1,eg2,eg3;
                   3700:   int i;
                   3701:
                   3702:   if ( !t ) return 0;
                   3703: get_eg(&eg1);
                   3704:   Nnewpair = 0;
                   3705:   d1 = nd_newpairs_array_s(t,syz);
                   3706: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
                   3707:   for ( i = 0; i < nd_nbase; i++ )
                   3708:     d[i] = merge_pairs_s(d[i],d1[i]);
                   3709: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
                   3710:   return Nnewpair;
                   3711: }
                   3712:
1.1       noro     3713: ND_pairs nd_newpairs( NODE g, int t )
                   3714: {
                   3715:   NODE h;
                   3716:   UINT *dl;
                   3717:   int ts,s,i,t0,min,max;
                   3718:   ND_pairs r,r0;
                   3719:
                   3720:   dl = DL(nd_psh[t]);
                   3721:   ts = SG(nd_psh[t]) - TD(dl);
1.17      noro     3722:   if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1       noro     3723:   for ( r0 = 0, h = g; h; h = NEXT(h) ) {
                   3724:     if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
                   3725:       continue;
                   3726:     if ( nd_gbblock ) {
                   3727:       t0 = (long)BDY(h);
                   3728:       for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
                   3729:         min = nd_gbblock[i]; max = nd_gbblock[i+1];
                   3730:         if ( t0 >= min && t0 <= max && t >= min && t <= max )
                   3731:           break;
                   3732:       }
                   3733:       if ( nd_gbblock[i] >= 0 )
                   3734:         continue;
                   3735:     }
1.41      noro     3736:     NEXTND_pairs(r0,r); Npairs++;
1.1       noro     3737:     r->i1 = (long)BDY(h);
                   3738:     r->i2 = t;
                   3739:     ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
                   3740:     s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   3741:     SG(r) = MAX(s,ts) + TD(LCM(r));
                   3742:     /* experimental */
                   3743:     if ( nd_sugarweight )
                   3744:       r->sugar2 = ndl_weight2(r->lcm);
                   3745:   }
                   3746:   if ( r0 ) NEXT(r) = 0;
                   3747:   return r0;
                   3748: }
                   3749:
1.46      noro     3750: int sig_cmpdl_op(int n,DL d1,DL d2)
                   3751: {
                   3752:   int e1,e2,i,j,l;
                   3753:   int *t1,*t2;
                   3754:   int len,head;
                   3755:   struct order_pair *pair;
                   3756:
                   3757:   len = nd_sba_modord->block_length;
                   3758:   pair = nd_sba_modord->order_pair;
                   3759:
                   3760:   head = 0;
                   3761:   for ( i = 0, t1 = d1->d, t2 = d2->d; i < len; i++ ) {
                   3762:     l = pair[i].length;
                   3763:     switch ( pair[i].order ) {
                   3764:       case 0:
                   3765:         for ( j = 0, e1 = e2 = 0; j < l; j++ ) {
                   3766:           e1 += t1[j];
                   3767:           e2 += t2[j];
                   3768:         }
                   3769:         if ( e1 > e2 )
                   3770:           return 1;
                   3771:         else if ( e1 < e2 )
                   3772:           return -1;
                   3773:         else {
                   3774:           for ( j = l - 1; j >= 0 && t1[j] == t2[j]; j-- );
                   3775:           if ( j >= 0 )
                   3776:             return t1[j] < t2[j] ? 1 : -1;
                   3777:         }
                   3778:         break;
                   3779:       case 1:
                   3780:         for ( j = 0, e1 = e2 = 0; j < l; j++ ) {
                   3781:           e1 += t1[j];
                   3782:           e2 += t2[j];
                   3783:         }
                   3784:         if ( e1 > e2 )
                   3785:           return 1;
                   3786:         else if ( e1 < e2 )
                   3787:           return -1;
                   3788:         else {
                   3789:           for ( j = 0; j < l && t1[j] == t2[j]; j++ );
                   3790:           if ( j < l )
                   3791:             return t1[j] > t2[j] ? 1 : -1;
                   3792:         }
                   3793:         break;
                   3794:       case 2:
                   3795:         for ( j = 0; j < l && t1[j] == t2[j]; j++ );
                   3796:         if ( j < l )
                   3797:           return t1[j] > t2[j] ? 1 : -1;
                   3798:         break;
                   3799:       default:
                   3800:         error("sig_cmpdl_op : invalid order"); break;
                   3801:     }
                   3802:     t1 += l; t2 += l; head += l;
                   3803:   }
                   3804:   return 0;
                   3805: }
                   3806:
                   3807: int sig_cmpdl_mat(int n,DL d1,DL d2)
                   3808: {
                   3809:   int *v,*t1,*t2;
                   3810:   int s,i,j,len;
                   3811:   int **matrix;
                   3812:   static int *w;
                   3813:   static int nvar = 0;
                   3814:
                   3815:   if ( nvar != n ) {
                   3816:     nvar = n; w = (int *)MALLOC(n*sizeof(int));
                   3817:   }
                   3818:   for ( i = 0, t1 = d1->d, t2 = d2->d; i < n; i++ )
                   3819:     w[i] = t1[i]-t2[i];
                   3820:   len = nd_sba_modord->row;
                   3821:   matrix = nd_sba_modord->matrix;
                   3822:   for ( j = 0; j < len; j++ ) {
                   3823:     v = matrix[j];
                   3824:     for ( i = 0, s = 0; i < n; i++ )
                   3825:       s += v[i]*w[i];
                   3826:     if ( s > 0 )
                   3827:       return 1;
                   3828:     else if ( s < 0 )
                   3829:       return -1;
                   3830:   }
                   3831:   return 0;
                   3832: }
                   3833:
                   3834: struct comp_sig_spec *create_comp_sig_spec(VL current_vl,VL old_vl,Obj ord,Obj weight)
                   3835: {
                   3836:   struct comp_sig_spec *spec;
                   3837:   VL ovl,vl;
                   3838:   V ov;
                   3839:   int i,j,n,nvar,s;
                   3840:   NODE node,t,tn;
                   3841:   struct order_pair *l;
                   3842:   MAT m;
                   3843:   Obj **b;
                   3844:   int **w;
                   3845:   int *a;
                   3846:
                   3847:   spec = (struct comp_sig_spec *)MALLOC(sizeof(struct comp_sig_spec));
                   3848:   for ( i = 0, vl = current_vl; vl; vl = NEXT(vl), i++ );
                   3849:   spec->n = nvar = i;
                   3850:   if ( old_vl != 0 ) {
                   3851:     spec->oldv = (int *)MALLOC(nvar*sizeof(int));
                   3852:     for ( i = 0, ovl = old_vl; i < nvar; ovl = NEXT(ovl), i++ ) {
                   3853:       ov = ovl->v;
                   3854:       for ( j = 0, vl = current_vl; vl; vl = NEXT(vl), j++ )
                   3855:         if ( ov == vl->v ) break;
                   3856:       spec->oldv[i] = j;
                   3857:     }
                   3858:   } else
                   3859:     spec->oldv = 0;
                   3860:   if ( !ord || NUM(ord) ) {
                   3861:     switch ( ZTOS((Z)ord) ) {
                   3862:       case 0:
                   3863:         spec->cmpdl = cmpdl_revgradlex; break;
                   3864:       case 1:
                   3865:         spec->cmpdl = cmpdl_gradlex; break;
                   3866:       case 2:
                   3867:         spec->cmpdl = cmpdl_lex; break;
                   3868:       default:
                   3869:         error("create_comp_sig_spec : invalid spec"); break;
                   3870:     }
                   3871:   } else if ( OID(ord) == O_LIST ) {
                   3872:     node = BDY((LIST)ord);
                   3873:     for ( n = 0, t = node; t; t = NEXT(t), n++ );
                   3874:     l = (struct order_pair *)MALLOC_ATOMIC(n*sizeof(struct order_pair));
                   3875:     for ( i = 0, t = node, s = 0; i < n; t = NEXT(t), i++ ) {
                   3876:       tn = BDY((LIST)BDY(t)); l[i].order = ZTOS((Q)BDY(tn));
                   3877:       tn = NEXT(tn); l[i].length = ZTOS((Q)BDY(tn));
                   3878:       s += l[i].length;
                   3879:     }
                   3880:     if ( s != nvar )
                   3881:       error("create_comp_sig_spec : invalid spec");
                   3882:     spec->order_pair = l;
                   3883:     spec->block_length = n;
                   3884:     spec->cmpdl = sig_cmpdl_op;
                   3885:   } else if ( OID(ord) == O_MAT ) {
                   3886:     m = (MAT)ord; b = (Obj **)BDY(m);
                   3887:     if ( m->col != nvar )
                   3888:       error("create_comp_sig_spec : invalid spec");
                   3889:     w = almat(m->row,m->col);
                   3890:     for ( i = 0; i < m->row; i++ )
                   3891:       for ( j = 0; j < m->col; j++ )
                   3892:         w[i][j] = ZTOS((Q)b[i][j]);
                   3893:     spec->row = m->row;
                   3894:     spec->matrix = w;
                   3895:     spec->cmpdl = sig_cmpdl_mat;
                   3896:   } else
                   3897:     error("create_comp_sig_spec : invalid spec");
                   3898:   if ( weight != 0 ) {
                   3899:     node = BDY((LIST)weight);
                   3900:     a = (int *)MALLOC(nvar*sizeof(int));
                   3901:     for ( i = 0; i < nvar; i++, node = NEXT(node) )
                   3902:       a[i] = ZTOS((Z)BDY(node));
                   3903:     spec->weight = a;
                   3904:   }
                   3905:   return spec;
                   3906: }
                   3907:
                   3908: #define SIG_MUL_WEIGHT(a,i) (weight?(a)*weight[i]:(a))
                   3909:
                   3910: int comp_sig_monomial(int n,DL d1,DL d2)
                   3911: {
                   3912:   static DL m1,m2;
                   3913:   static int nvar = 0;
                   3914:   int *oldv,*weight;
                   3915:   int i,w1,w2;
                   3916:
                   3917:   if ( nvar != n ) {
                   3918:     nvar = n; NEWDL(m1,nvar); NEWDL(m2,nvar);
                   3919:   }
                   3920:   if ( !nd_sba_modord )
                   3921:     return (*cmpdl)(n,d1,d2);
                   3922:   else {
                   3923:     weight = nd_sba_modord->weight;
                   3924:     oldv = nd_sba_modord->oldv;
                   3925:     if ( oldv ) {
                   3926:       for ( i = 0; i < n; i++ ) {
                   3927:         m1->d[i] = d1->d[oldv[i]]; m2->d[i] = d2->d[oldv[i]];
                   3928:       }
                   3929:     } else {
                   3930:       for ( i = 0; i < n; i++ ) {
                   3931:         m1->d[i] = d1->d[i]; m2->d[i] = d2->d[i];
                   3932:       }
                   3933:     }
                   3934:     for ( i = 0, w1 = w2 = 0; i < n; i++ ) {
                   3935:       w1 += SIG_MUL_WEIGHT(m1->d[i],i);
                   3936:       w2 += SIG_MUL_WEIGHT(m2->d[i],i);
                   3937:     }
                   3938:     m1->td = w1; m2->td = w2;
                   3939:     return (*nd_sba_modord->cmpdl)(n,m1,m2);
                   3940:   }
                   3941: }
                   3942:
1.24      noro     3943: int comp_sig(SIG s1,SIG s2)
                   3944: {
1.38      noro     3945:   if ( nd_sba_pot ) {
                   3946:     if ( s1->pos > s2->pos ) return 1;
                   3947:     else if ( s1->pos < s2->pos ) return -1;
1.46      noro     3948:     else return comp_sig_monomial(nd_nvar,s1->dl,s2->dl);
1.38      noro     3949:   } else {
                   3950:     static DL m1,m2;
1.44      noro     3951:     static int nvar = 0;
1.38      noro     3952:     int ret;
                   3953:
                   3954:     if ( nvar != nd_nvar ) {
                   3955:       nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
                   3956:     }
                   3957:     _adddl(nd_nvar,s1->dl,nd_sba_hm[s1->pos],m1);
                   3958:     _adddl(nd_nvar,s2->dl,nd_sba_hm[s2->pos],m2);
1.47      noro     3959:     if ( !nd_sba_modord )
                   3960:       ret = (*cmpdl)(nd_nvar,m1,m2);
                   3961:     else
                   3962:       ret = comp_sig_monomial(nd_nvar,m1,m2);
1.38      noro     3963:     if ( ret != 0 ) return ret;
                   3964:     else if ( s1->pos > s2->pos ) return 1;
                   3965:     else if ( s1->pos < s2->pos ) return -1;
                   3966:     else return 0;
1.24      noro     3967:   }
                   3968: }
                   3969:
                   3970: int _create_spair_s(int i1,int i2,ND_pairs sp,SIG sig1,SIG sig2)
                   3971: {
                   3972:   int ret,s1,s2;
                   3973:   RHist p1,p2;
                   3974:   static int wpd;
                   3975:   static UINT *lcm;
                   3976:
                   3977:   sp->i1 = i1;
                   3978:   sp->i2 = i2;
                   3979:   p1 = nd_psh[i1];
                   3980:   p2 = nd_psh[i2];
                   3981:   ndl_lcm(DL(p1),DL(p2),sp->lcm);
1.49      noro     3982: #if 0
1.24      noro     3983:   s1 = SG(p1)-TD(DL(p1));
                   3984:   s2 = SG(p2)-TD(DL(p2));
                   3985:   SG(sp) = MAX(s1,s2) + TD(sp->lcm);
1.49      noro     3986: #endif
1.24      noro     3987:
1.26      noro     3988:   if ( wpd != nd_wpd ) {
1.24      noro     3989:     wpd = nd_wpd;
                   3990:     lcm = (UINT *)MALLOC(wpd*sizeof(UINT));
                   3991:   }
                   3992:   // DL(sig1) <- sp->lcm
                   3993:   // DL(sig1) -= DL(p1)
                   3994:   // DL(sig1) += DL(p1->sig)
1.27      noro     3995:   ndl_sub(sp->lcm,DL(p1),lcm);
1.24      noro     3996:   _ndltodl(lcm,DL(sig1));
                   3997:   _addtodl(nd_nvar,DL(p1->sig),DL(sig1));
                   3998:   sig1->pos = p1->sig->pos;
                   3999:
                   4000:   // DL(sig2) <- sp->lcm
                   4001:   // DL(sig2) -= DL(p2)
                   4002:   // DL(sig2) += DL(p2->sig)
1.27      noro     4003:   ndl_sub(sp->lcm,DL(p2),lcm);
1.24      noro     4004:   _ndltodl(lcm,DL(sig2));
                   4005:   _addtodl(nd_nvar,DL(p2->sig),DL(sig2));
                   4006:   sig2->pos = p2->sig->pos;
                   4007:
                   4008:   ret = comp_sig(sig1,sig2);
                   4009:   if ( ret == 0 ) return 0;
                   4010:   else if ( ret > 0 ) sp->sig = sig1;
                   4011:   else sp->sig = sig2;
1.49      noro     4012:
                   4013:   s1 = DL(sig1)->td+nd_sba_hm[p1->sig->pos]->td;
                   4014:   s2 = DL(sig2)->td+nd_sba_hm[p2->sig->pos]->td;
                   4015:   SG(sp) = MAX(s1,s2);
                   4016:
1.24      noro     4017:   return 1;
                   4018: }
                   4019:
                   4020: SIG dup_sig(SIG sig)
                   4021: {
                   4022:   SIG r;
                   4023:
                   4024:   if ( !sig ) return 0;
                   4025:   else {
                   4026:     NEWSIG(r);
                   4027:     _copydl(nd_nvar,DL(sig),DL(r));
                   4028:     r->pos = sig->pos;
                   4029:     return r;
                   4030:   }
                   4031: }
                   4032:
                   4033: void dup_ND_pairs(ND_pairs to,ND_pairs from)
                   4034: {
                   4035:   to->i1 = from->i1;
                   4036:   to->i2 = from->i2;
                   4037:   to->sugar = from->sugar;
                   4038:   to->sugar2 = from->sugar2;
                   4039:   ndl_copy(from->lcm,to->lcm);
                   4040:   to->sig = dup_sig(from->sig);
                   4041: }
                   4042:
                   4043: ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
                   4044: {
                   4045:   struct oND_pairs root;
                   4046:   ND_pairs q1,q2,r0,r;
                   4047:   int ret;
                   4048:
                   4049:   r = &root;
                   4050:   for ( q1 = p1, q2 = p2; q1 != 0 && q2 != 0; ) {
                   4051:     ret = comp_sig(q1->sig,q2->sig);
                   4052:     if ( ret < 0 ) {
                   4053:       r->next = q1; r = q1; q1 = q1->next;
                   4054:     } else if ( ret > 0 ) {
                   4055:       r->next = q2; r = q2; q2 = q2->next;
                   4056:     } else {
1.47      noro     4057:       Nnewpair--;
1.24      noro     4058:       ret = DL_COMPARE(q1->lcm,q2->lcm);
1.35      noro     4059:       Nsamesig++;
1.24      noro     4060:       if ( ret < 0 ) {
                   4061:         r->next = q1; r = q1; q1 = q1->next;
                   4062:         q2 = q2->next;
                   4063:       } else {
                   4064:         r->next = q2; r = q2; q2 = q2->next;
                   4065:         q1 = q1->next;
                   4066:       }
                   4067:     }
                   4068:   }
                   4069:   if ( q1 ) {
                   4070:     r->next = q1;
                   4071:   } else {
                   4072:     r->next = q2;
                   4073:   }
                   4074:   return root.next;
                   4075: }
                   4076:
                   4077: ND_pairs insert_pair_s(ND_pairs l,ND_pairs s)
                   4078: {
                   4079:   ND_pairs p,prev;
1.47      noro     4080:   int ret=1;
1.24      noro     4081:
                   4082:   for ( p = l, prev = 0; p != 0; prev = p, p = p->next ) {
                   4083:     if ( (ret = comp_sig(s->sig,p->sig)) <= 0 )
                   4084:       break;
                   4085:   }
                   4086:   if ( ret == 0 ) {
                   4087:     ret = DL_COMPARE(s->lcm,p->lcm);
                   4088:     if ( ret < 0 ) {
                   4089:       // replace p with s
                   4090:       s->next = p->next;
                   4091:       if ( prev == 0 ) {
                   4092:         return s;
                   4093:       } else {
                   4094:         prev->next = s;
                   4095:         return l;
                   4096:       }
                   4097:     } else
                   4098:       return l;
                   4099:   } else {
                   4100:     // insert s between prev and p
1.47      noro     4101:     Nnewpair++;
1.24      noro     4102:     s->next = p;
                   4103:     if ( prev == 0 ) {
                   4104:       return s;
                   4105:     } else {
                   4106:       prev->next = s;
                   4107:       return l;
                   4108:     }
                   4109:   }
                   4110: }
                   4111:
1.29      noro     4112: INLINE int __dl_redble(DL d1,DL d2,int nvar)
                   4113: {
                   4114:   int i;
                   4115:
                   4116:   if ( d1->td > d2->td )
                   4117:     return 0;
                   4118:   for ( i = nvar-1; i >= 0; i-- )
                   4119:     if ( d1->d[i] > d2->d[i] )
                   4120:       break;
                   4121:   if ( i >= 0 )
                   4122:     return 0;
                   4123:   else
                   4124:     return 1;
                   4125: }
                   4126:
1.30      noro     4127: ND_pairs nd_newpairs_s(int t, NODE *syz)
1.24      noro     4128: {
                   4129:   NODE h,s;
                   4130:   UINT *dl;
1.27      noro     4131:   int ts,ret,i;
1.24      noro     4132:   ND_pairs r,r0,_sp,sp;
1.29      noro     4133:   SIG spsig,tsig;
1.44      noro     4134:   static int nvar = 0;
1.29      noro     4135:   static SIG _sig1,_sig2;
1.26      noro     4136:   struct oEGT eg1,eg2,eg3,eg4;
1.24      noro     4137:
                   4138:   NEWND_pairs(_sp);
1.29      noro     4139:   if ( !_sig1 || nvar != nd_nvar ) {
                   4140:     nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
                   4141:   }
1.24      noro     4142:   r0 = 0;
1.27      noro     4143:   for ( i = 0; i < t; i++ ) {
                   4144:     ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
1.24      noro     4145:     if ( ret ) {
                   4146:       spsig = _sp->sig;
1.29      noro     4147:       for ( s = syz[spsig->pos]; s; s = s->next ) {
1.24      noro     4148:         tsig = (SIG)s->body;
1.29      noro     4149:         if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
1.24      noro     4150:           break;
                   4151:       }
                   4152:       if ( s == 0 ) {
                   4153:         NEWND_pairs(sp);
                   4154:         dup_ND_pairs(sp,_sp);
                   4155:         r0 = insert_pair_s(r0,sp);
1.26      noro     4156:       } else
                   4157:         Nsyz++;
1.24      noro     4158:     }
                   4159:   }
                   4160:   return r0;
                   4161: }
                   4162:
1.47      noro     4163: ND_pairs *nd_newpairs_array_s(int t, NODE *syz)
                   4164: {
                   4165:   NODE h,s;
                   4166:   UINT *dl;
                   4167:   int ts,ret,i;
                   4168:   ND_pairs r,r0,_sp,sp;
                   4169:   ND_pairs *d;
                   4170:   SIG spsig,tsig;
                   4171:   static int nvar = 0;
                   4172:   static SIG _sig1,_sig2;
                   4173:   struct oEGT eg1,eg2,eg3,eg4;
                   4174:
                   4175:   NEWND_pairs(_sp);
                   4176:   if ( !_sig1 || nvar != nd_nvar ) {
                   4177:     nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
                   4178:   }
                   4179:   d = (ND_pairs *)MALLOC(nd_nbase*sizeof(ND_pairs));
                   4180:   Nnewpair = 0;
                   4181:   for ( i = 0; i < t; i++ ) {
                   4182:     ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
                   4183:     if ( ret ) {
                   4184:       spsig = _sp->sig;
                   4185:       for ( s = syz[spsig->pos]; s; s = s->next ) {
                   4186:         tsig = (SIG)s->body;
                   4187:         if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
                   4188:           break;
                   4189:       }
                   4190:       if ( s == 0 ) {
                   4191:         NEWND_pairs(sp);
                   4192:         dup_ND_pairs(sp,_sp);
                   4193:         d[spsig->pos] = insert_pair_s(d[spsig->pos],sp);
                   4194:       } else
                   4195:         Nsyz++;
                   4196:     }
                   4197:   }
                   4198:   return d;
                   4199: }
                   4200:
1.1       noro     4201: /* ipair = [i1,i2],[i1,i2],... */
                   4202: ND_pairs nd_ipairtospair(NODE ipair)
                   4203: {
                   4204:   int s1,s2;
                   4205:   NODE tn,t;
                   4206:   ND_pairs r,r0;
                   4207:
                   4208:   for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
                   4209:     NEXTND_pairs(r0,r);
                   4210:     tn = BDY((LIST)BDY(t));
1.6       noro     4211:     r->i1 = ZTOS((Q)ARG0(tn));
                   4212:     r->i2 = ZTOS((Q)ARG1(tn));
1.1       noro     4213:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
                   4214:     s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   4215:     s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
                   4216:     SG(r) = MAX(s1,s2) + TD(LCM(r));
                   4217:     /* experimental */
                   4218:     if ( nd_sugarweight )
                   4219:         r->sugar2 = ndl_weight2(r->lcm);
                   4220:   }
                   4221:   if ( r0 ) NEXT(r) = 0;
                   4222:   return r0;
                   4223: }
                   4224:
                   4225: /* kokokara */
                   4226:
                   4227: ND_pairs crit_B( ND_pairs d, int s )
                   4228: {
                   4229:   ND_pairs cur,head,prev,remove;
                   4230:   UINT *t,*tl,*lcm;
                   4231:   int td,tdl;
                   4232:
                   4233:   if ( !d ) return 0;
                   4234:   t = DL(nd_psh[s]);
                   4235:   prev = 0;
                   4236:   head = cur = d;
                   4237:   lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   4238:   while ( cur ) {
                   4239:     tl = cur->lcm;
                   4240:     if ( ndl_reducible(tl,t) ) {
                   4241:       ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
                   4242:       if ( !ndl_equal(lcm,tl) ) {
                   4243:         ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
                   4244:         if (!ndl_equal(lcm,tl)) {
                   4245:           remove = cur;
                   4246:           if ( !prev ) {
                   4247:             head = cur = NEXT(cur);
                   4248:           } else {
                   4249:             cur = NEXT(prev) = NEXT(cur);
                   4250:           }
1.41      noro     4251:           FREENDP(remove); NcriB++;
1.1       noro     4252:         } else {
                   4253:           prev = cur; cur = NEXT(cur);
                   4254:         }
                   4255:       } else {
                   4256:         prev = cur; cur = NEXT(cur);
                   4257:       }
                   4258:     } else {
                   4259:       prev = cur; cur = NEXT(cur);
                   4260:     }
                   4261:   }
                   4262:   return head;
                   4263: }
                   4264:
                   4265: ND_pairs crit_M( ND_pairs d1 )
                   4266: {
                   4267:     ND_pairs e,d2,d3,dd,p;
                   4268:     UINT *id,*jd;
                   4269:
                   4270:     if ( !d1 ) return d1;
                   4271:     for ( dd = 0, e = d1; e; e = d3 ) {
                   4272:         if ( !(d2 = NEXT(e)) ) {
                   4273:             NEXT(e) = dd;
                   4274:             return e;
                   4275:         }
                   4276:         id = LCM(e);
                   4277:         for ( d3 = 0; d2; d2 = p ) {
                   4278:             p = NEXT(d2);
                   4279:             jd = LCM(d2);
                   4280:             if ( ndl_equal(jd,id) )
                   4281:                 ;
                   4282:             else if ( TD(jd) > TD(id) )
                   4283:                 if ( ndl_reducible(jd,id) ) continue;
                   4284:                 else ;
                   4285:             else if ( ndl_reducible(id,jd) ) goto delit;
                   4286:             NEXT(d2) = d3;
                   4287:             d3 = d2;
                   4288:         }
                   4289:         NEXT(e) = dd;
                   4290:         dd = e;
                   4291:         continue;
                   4292:         /**/
                   4293:     delit:    NEXT(d2) = d3;
                   4294:         d3 = d2;
                   4295:         for ( ; p; p = d2 ) {
                   4296:             d2 = NEXT(p);
                   4297:             NEXT(p) = d3;
                   4298:             d3 = p;
                   4299:         }
                   4300:         FREENDP(e);
                   4301:     }
                   4302:     return dd;
                   4303: }
                   4304:
                   4305: ND_pairs crit_F( ND_pairs d1 )
                   4306: {
                   4307:     ND_pairs rest, head,remove;
                   4308:     ND_pairs last, p, r, w;
                   4309:     int s;
                   4310:
                   4311:     if ( !d1 ) return d1;
                   4312:     for ( head = last = 0, p = d1; NEXT(p); ) {
                   4313:         r = w = equivalent_pairs(p,&rest);
                   4314:         s = SG(r);
                   4315:         w = NEXT(w);
                   4316:         while ( w ) {
                   4317:             if ( crit_2(w->i1,w->i2) ) {
                   4318:                 r = w;
                   4319:                 w = NEXT(w);
                   4320:                 while ( w ) {
                   4321:                     remove = w;
                   4322:                     w = NEXT(w);
                   4323:                     FREENDP(remove);
                   4324:                 }
                   4325:                 break;
                   4326:             } else if ( SG(w) < s ) {
                   4327:                 FREENDP(r);
                   4328:                 r = w;
                   4329:                 s = SG(r);
                   4330:                 w = NEXT(w);
                   4331:             } else {
                   4332:                 remove = w;
                   4333:                 w = NEXT(w);
                   4334:                 FREENDP(remove);
                   4335:             }
                   4336:         }
                   4337:         if ( last ) NEXT(last) = r;
                   4338:         else head = r;
                   4339:         NEXT(last = r) = 0;
                   4340:         p = rest;
                   4341:         if ( !p ) return head;
                   4342:     }
                   4343:     if ( !last ) return p;
                   4344:     NEXT(last) = p;
                   4345:     return head;
                   4346: }
                   4347:
                   4348: int crit_2( int dp1, int dp2 )
                   4349: {
                   4350:     return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
                   4351: }
                   4352:
                   4353: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
                   4354: {
                   4355:     ND_pairs w,p,r,s;
                   4356:     UINT *d;
                   4357:
                   4358:     w = d1;
                   4359:     d = LCM(w);
                   4360:     s = NEXT(w);
                   4361:     NEXT(w) = 0;
                   4362:     for ( r = 0; s; s = p ) {
                   4363:         p = NEXT(s);
                   4364:         if ( ndl_equal(d,LCM(s)) ) {
                   4365:             NEXT(s) = w; w = s;
                   4366:         } else {
                   4367:             NEXT(s) = r; r = s;
                   4368:         }
                   4369:     }
                   4370:     *prest = r;
                   4371:     return w;
                   4372: }
                   4373:
                   4374: NODE update_base(NODE nd,int ndp)
                   4375: {
                   4376:     UINT *dl, *dln;
                   4377:     NODE last, p, head;
                   4378:
                   4379:     dl = DL(nd_psh[ndp]);
                   4380:     for ( head = last = 0, p = nd; p; ) {
                   4381:         dln = DL(nd_psh[(long)BDY(p)]);
                   4382:         if ( ndl_reducible( dln, dl ) ) {
                   4383:             p = NEXT(p);
                   4384:             if ( last ) NEXT(last) = p;
                   4385:         } else {
                   4386:             if ( !last ) head = p;
                   4387:             p = NEXT(last = p);
                   4388:         }
                   4389:     }
                   4390:     head = append_one(head,ndp);
                   4391:     return head;
                   4392: }
                   4393:
                   4394: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   4395: {
                   4396:     ND_pairs m,ml,p,l;
                   4397:     UINT *lcm;
                   4398:     int s,td,len,tlen,c,c1;
                   4399:
                   4400:     if ( !(p = NEXT(m = d)) ) {
                   4401:         *prest = p;
                   4402:         NEXT(m) = 0;
                   4403:         return m;
                   4404:     }
                   4405:     if ( !NoSugar ) {
                   4406:         if ( nd_sugarweight ) {
                   4407:             s = m->sugar2;
                   4408:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   4409:                 if ( (p->sugar2 < s)
                   4410:                     || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   4411:                     ml = l; m = p; s = m->sugar2;
                   4412:                 }
                   4413:         } else {
                   4414:             s = SG(m);
                   4415:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   4416:                 if ( (SG(p) < s)
                   4417:                     || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   4418:                     ml = l; m = p; s = SG(m);
                   4419:                 }
                   4420:         }
                   4421:     } else {
                   4422:         for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   4423:             if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
                   4424:                 ml = l; m = p; s = SG(m);
                   4425:             }
                   4426:     }
                   4427:     if ( !ml ) *prest = NEXT(m);
                   4428:     else {
                   4429:         NEXT(ml) = NEXT(m);
                   4430:         *prest = d;
                   4431:     }
                   4432:     NEXT(m) = 0;
                   4433:     return m;
                   4434: }
                   4435:
                   4436: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   4437: {
                   4438:     int msugar,i;
                   4439:     ND_pairs t,dm0,dm,dr0,dr;
                   4440:
                   4441:     if ( nd_sugarweight ) {
                   4442:         for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
                   4443:             if ( t->sugar2 < msugar ) msugar = t->sugar2;
                   4444:         dm0 = 0; dr0 = 0;
                   4445:         for ( i = 0, t = d; t; t = NEXT(t) )
                   4446:             if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
                   4447:                 if ( dm0 ) NEXT(dm) = t;
                   4448:                 else dm0 = t;
                   4449:                 dm = t;
                   4450:                 i++;
                   4451:             } else {
                   4452:                 if ( dr0 ) NEXT(dr) = t;
                   4453:                 else dr0 = t;
                   4454:                 dr = t;
                   4455:             }
                   4456:     } else {
                   4457:         for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   4458:             if ( SG(t) < msugar ) msugar = SG(t);
                   4459:         dm0 = 0; dr0 = 0;
                   4460:         for ( i = 0, t = d; t; t = NEXT(t) )
                   4461:             if ( i < nd_f4_nsp && SG(t) == msugar ) {
                   4462:                 if ( dm0 ) NEXT(dm) = t;
                   4463:                 else dm0 = t;
                   4464:                 dm = t;
                   4465:                 i++;
                   4466:             } else {
                   4467:                 if ( dr0 ) NEXT(dr) = t;
                   4468:                 else dr0 = t;
                   4469:                 dr = t;
                   4470:             }
                   4471:     }
                   4472:     NEXT(dm) = 0;
                   4473:     if ( dr0 ) NEXT(dr) = 0;
                   4474:     *prest = dr0;
                   4475:     return dm0;
                   4476: }
                   4477:
1.28      noro     4478: ND_pairs nd_minsugarp_s( ND_pairs d, ND_pairs *prest )
                   4479: {
                   4480:   int msugar;
                   4481:   ND_pairs t,last;
                   4482:
                   4483:   for ( msugar = SG(d), t = d; t; t = NEXT(t) )
                   4484:     if ( SG(t) == msugar ) last = t;
                   4485:   *prest = last->next;
                   4486:   last->next = 0;
                   4487:   return d;
                   4488: }
                   4489:
1.1       noro     4490: int nd_tdeg(NDV c)
                   4491: {
                   4492:   int wmax = 0;
                   4493:   int i,len;
                   4494:   NMV a;
                   4495:
                   4496:   len = LEN(c);
                   4497:   for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   4498:     wmax = MAX(TD(DL(a)),wmax);
                   4499:   return wmax;
                   4500: }
                   4501:
1.24      noro     4502: int ndv_newps(int m,NDV a,NDV aq)
1.1       noro     4503: {
                   4504:     int len;
                   4505:     RHist r;
                   4506:     NDV b;
                   4507:     NODE tn;
                   4508:     LIST l;
                   4509:     Z iq;
                   4510:
                   4511:     if ( nd_psn == nd_pslen ) {
                   4512:         nd_pslen *= 2;
                   4513:         nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
                   4514:         nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
                   4515:         nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
                   4516:         nd_bound = (UINT **)
                   4517:             REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
                   4518:         nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
                   4519:         nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
                   4520:     }
                   4521:     NEWRHist(r); nd_psh[nd_psn] = r;
                   4522:     nd_ps[nd_psn] = a;
                   4523:     if ( aq ) {
                   4524:         nd_ps_trace[nd_psn] = aq;
                   4525:         if ( !m ) {
                   4526:           register_hcf(aq);
                   4527:         } else if ( m == -2 ) {
                   4528:           /* do nothing */
                   4529:         } else
                   4530:           error("ndv_newps : invalud modulus");
                   4531:         nd_bound[nd_psn] = ndv_compute_bound(aq);
                   4532: #if 1
                   4533:         SG(r) = SG(aq);
                   4534: #else
                   4535:         SG(r) = nd_tdeg(aq);
                   4536: #endif
                   4537:         ndl_copy(HDL(aq),DL(r));
1.24      noro     4538:         r->sig = dup_sig(aq->sig);
1.1       noro     4539:     } else {
                   4540:         if ( !m ) register_hcf(a);
                   4541:         nd_bound[nd_psn] = ndv_compute_bound(a);
                   4542: #if 1
                   4543:         SG(r) = SG(a);
                   4544: #else
                   4545:         SG(r) = nd_tdeg(a);
                   4546: #endif
                   4547:         ndl_copy(HDL(a),DL(r));
1.24      noro     4548:         r->sig = dup_sig(a->sig);
1.1       noro     4549:     }
                   4550:     if ( nd_demand ) {
                   4551:         if ( aq ) {
                   4552:             ndv_save(nd_ps_trace[nd_psn],nd_psn);
                   4553:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   4554:             nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   4555:             nd_ps_trace[nd_psn] = 0;
                   4556:         } else {
                   4557:             ndv_save(nd_ps[nd_psn],nd_psn);
                   4558:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
                   4559:             nd_ps[nd_psn] = 0;
                   4560:         }
                   4561:     }
                   4562:     if ( nd_gentrace ) {
                   4563:         /* reverse the tracelist and append it to alltracelist */
                   4564:         nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6       noro     4565:         STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1       noro     4566:         MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
                   4567:     }
                   4568:     return nd_psn++;
                   4569: }
                   4570:
1.46      noro     4571: // find LM wrt the specified modord
                   4572: void ndv_lm_modord(NDV p,DL d)
1.45      noro     4573: {
                   4574:   NMV m;
                   4575:   DL tmp;
                   4576:   int len,i,ret;
                   4577:
                   4578:   NEWDL(tmp,nd_nvar);
                   4579:   m = BDY(p); len = LEN(p);
1.46      noro     4580:   _ndltodl(DL(m),d); // printdl(d); printf("->");
1.45      noro     4581:   for ( i = 1, NMV_ADV(m); i < len; i++, NMV_ADV(m) ) {
                   4582:     _ndltodl(DL(m),tmp);
1.46      noro     4583:     ret = comp_sig_monomial(nd_nvar,tmp,d);
1.45      noro     4584:     if ( ret > 0 ) _copydl(nd_nvar,tmp,d);
                   4585:   }
1.46      noro     4586: //   printdl(d); printf("\n");
1.45      noro     4587: }
                   4588:
1.1       noro     4589: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
                   4590: /* return 1 if success, 0 if failure (HC(a mod p)) */
                   4591:
1.24      noro     4592: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont,int sba)
1.1       noro     4593: {
1.6       noro     4594:   int i,j,td,len,max;
                   4595:   NODE s,s0,f0,tn;
                   4596:   UINT *d;
                   4597:   RHist r;
                   4598:   NDVI w;
                   4599:   NDV a,am;
                   4600:   union oNDC hc;
                   4601:   NODE node;
                   4602:   P hcp;
                   4603:   Z iq,jq;
                   4604:   LIST l;
                   4605:
                   4606:   nd_found = 0; nd_notfirst = 0; nd_create = 0;
                   4607:   /* initialize the tracelist */
                   4608:   nd_tracelist = 0;
                   4609:
                   4610:   for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
                   4611:   w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
                   4612:   for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
                   4613:     if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
                   4614:   if ( !dont_sort ) {
                   4615:     /* XXX heuristic */
1.26      noro     4616:     if ( !sba && !nd_ord->id && (nd_ord->ord.simple<2) )
1.6       noro     4617:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   4618:         (int (*)(const void *,const void *))ndvi_compare_rev);
                   4619:     else
                   4620:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   4621:         (int (*)(const void *,const void *))ndvi_compare);
                   4622:   }
                   4623:   nd_pslen = 2*nd_psn;
                   4624:   nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4625:   nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4626:   nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4627:   nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4628:   nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
                   4629:   nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
                   4630:   nd_hcf = 0;
1.1       noro     4631:
1.6       noro     4632:   if ( trace && nd_vc )
                   4633:     makesubst(nd_vc,&nd_subst);
                   4634:   else
                   4635:     nd_subst = 0;
1.1       noro     4636:
1.6       noro     4637:   if ( !nd_red )
                   4638:     nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   4639:   for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
                   4640:   for ( i = 0; i < nd_psn; i++ ) {
                   4641:     hc = HCU(w[i].p);
                   4642:     if ( trace ) {
                   4643:       if ( mod == -2 ) {
                   4644:         /* over a large finite field */
                   4645:         /* trace = small modulus */
                   4646:         a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
                   4647:         ndv_mod(-2,a);
                   4648:         if ( !dont_removecont) ndv_removecont(-2,a);
                   4649:         am = nd_ps[i] = ndv_dup(trace,w[i].p);
                   4650:         ndv_mod(trace,am);
                   4651:       if ( DL_COMPARE(HDL(am),HDL(a)) )
                   4652:         return 0;
                   4653:         ndv_removecont(trace,am);
                   4654:       } else {
                   4655:         a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
                   4656:         if ( !dont_removecont) ndv_removecont(0,a);
                   4657:         register_hcf(a);
                   4658:         am = nd_ps[i] = ndv_dup(mod,a);
                   4659:         ndv_mod(mod,am);
                   4660:       if ( DL_COMPARE(HDL(am),HDL(a)) )
                   4661:         return 0;
                   4662:         ndv_removecont(mod,am);
                   4663:       }
                   4664:     } else {
                   4665:       a = nd_ps[i] = ndv_dup(mod,w[i].p);
                   4666:       if ( mod || !dont_removecont ) ndv_removecont(mod,a);
                   4667:       if ( !mod ) register_hcf(a);
1.1       noro     4668:     }
1.6       noro     4669:     if ( nd_gentrace ) {
                   4670:       STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
                   4671:       /* exact division */
1.1       noro     4672:       if ( !dont_removecont )
1.6       noro     4673:         ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
                   4674:       MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
                   4675:     }
                   4676:     NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
                   4677:     nd_bound[i] = ndv_compute_bound(a);
                   4678:     nd_psh[i] = r;
                   4679:     if ( nd_demand ) {
                   4680:       if ( trace ) {
                   4681:         ndv_save(nd_ps_trace[i],i);
                   4682:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   4683:         nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   4684:         nd_ps_trace[i] = 0;
                   4685:       } else {
                   4686:         ndv_save(nd_ps[i],i);
                   4687:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
                   4688:         nd_ps[i] = 0;
                   4689:       }
1.1       noro     4690:     }
1.6       noro     4691:   }
1.24      noro     4692:   if ( sba ) {
1.27      noro     4693:     nd_sba_hm = (DL *)MALLOC(nd_psn*sizeof(DL));
1.24      noro     4694:    // setup signatures
1.27      noro     4695:     for ( i = 0; i < nd_psn; i++ ) {
                   4696:       SIG sig;
1.24      noro     4697:
1.27      noro     4698:       NEWSIG(sig); sig->pos = i;
                   4699:       nd_ps[i]->sig = sig;
                   4700:       if ( nd_demand ) nd_ps_sym[i]->sig = sig;
                   4701:         nd_psh[i]->sig = sig;
                   4702:       if ( trace ) {
                   4703:         nd_ps_trace[i]->sig = sig;
                   4704:         if ( nd_demand ) nd_ps_trace_sym[i]->sig = sig;
                   4705:       }
                   4706:       NEWDL(nd_sba_hm[i],nd_nvar);
1.46      noro     4707:       if ( nd_sba_modord )
                   4708:         ndv_lm_modord(nd_ps[i],nd_sba_hm[i]);
1.45      noro     4709:       else
                   4710:         _ndltodl(DL(nd_psh[i]),nd_sba_hm[i]);
1.27      noro     4711:     }
1.29      noro     4712:     nd_sba_pos = (NODE *)MALLOC(nd_psn*sizeof(NODE));
                   4713:     for ( i = 0; i < nd_psn; i++ ) {
                   4714:       j = nd_psh[i]->sig->pos;
                   4715:       nd_sba_pos[j] = append_one(nd_sba_pos[j],i);
                   4716:     }
1.24      noro     4717:   }
1.6       noro     4718:   if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
                   4719:   return 1;
1.1       noro     4720: }
                   4721:
                   4722: struct order_spec *append_block(struct order_spec *spec,
                   4723:     int nv,int nalg,int ord);
                   4724:
                   4725: extern VECT current_dl_weight_vector_obj;
                   4726: static VECT prev_weight_vector_obj;
                   4727:
                   4728: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
                   4729:     struct order_spec **ord1p,LIST *f1p,NODE *alistp)
                   4730: {
                   4731:     NODE alist,t,s,r0,r,arg;
                   4732:     VL tv;
                   4733:     P poly;
                   4734:     DP d;
                   4735:     Alg alpha,dp;
                   4736:     DAlg inv,da,hc;
                   4737:     MP m;
                   4738:     int i,nvar,nalg,n;
                   4739:     NumberField nf;
                   4740:     LIST f1,f2;
                   4741:     struct order_spec *current_spec;
                   4742:     VECT obj,obj0;
                   4743:     VECT tmp;
                   4744:
                   4745:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
                   4746:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
                   4747:
                   4748:     for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
                   4749:         NEXTNODE(alist,t); MKV(tv->v,poly);
                   4750:         MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
                   4751:         tv->v = tv->v->priv;
                   4752:     }
                   4753:     NEXT(t) = 0;
                   4754:
                   4755:     /* simplification, making polynomials monic */
                   4756:     setfield_dalg(alist);
                   4757:     obj_algtodalg((Obj)f,(Obj *)&f1);
                   4758:     for ( t = BDY(f); t; t = NEXT(t) ) {
                   4759:         initd(ord); ptod(vv,vv,(P)BDY(t),&d);
                   4760:         hc = (DAlg)BDY(d)->c;
                   4761:         if ( NID(hc) == N_DA ) {
                   4762:             invdalg(hc,&inv);
                   4763:             for ( m = BDY(d); m; m = NEXT(m) ) {
                   4764:                 muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
                   4765:             }
                   4766:         }
                   4767:         initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
                   4768:     }
                   4769:     obj_dalgtoalg((Obj)f1,(Obj *)&f);
                   4770:
                   4771:     /* append alg vars to the var list */
                   4772:     for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
                   4773:     NEXT(tv) = av;
                   4774:
                   4775:     /* append a block to ord */
                   4776:     *ord1p = append_block(ord,nvar,nalg,2);
                   4777:
                   4778:     /* create generator list */
                   4779:     nf = get_numberfield();
                   4780:     for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
                   4781:         MKAlg(nf->defpoly[i],dp);
                   4782:         MKNODE(s,dp,t); t = s;
                   4783:     }
                   4784:     MKLIST(f1,t);
                   4785:     *alistp = alist;
                   4786:     algobjtorat((Obj)f1,(Obj *)f1p);
                   4787:
                   4788:     /* creating a new weight vector */
                   4789:     prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
                   4790:     n = nvar+nalg+1;
                   4791:     MKVECT(obj,n);
                   4792:     if ( obj0 && obj0->len == nvar )
                   4793:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
                   4794:     else
                   4795:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
                   4796:     for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
                   4797:     BDY(obj)[n-1] = (pointer)ONE;
                   4798:     arg = mknode(1,obj);
                   4799:     Pdp_set_weight(arg,&tmp);
                   4800: }
                   4801:
                   4802: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
                   4803: {
                   4804:     NODE s,t,u0,u;
                   4805:     P p;
                   4806:     VL tv;
                   4807:     Obj obj;
                   4808:     VECT tmp;
                   4809:     NODE arg;
                   4810:
                   4811:     u0 = 0;
                   4812:     for ( t = r; t; t = NEXT(t) ) {
                   4813:         p = (P)BDY(t);
                   4814:         for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
                   4815:             substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
                   4816:         }
                   4817:         if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
                   4818:             NEXTNODE(u0,u);
                   4819:             BDY(u) = (pointer)p;
                   4820:         }
                   4821:     }
                   4822:     arg = mknode(1,prev_weight_vector_obj);
                   4823:     Pdp_set_weight(arg,&tmp);
                   4824:
                   4825:     return u0;
                   4826: }
                   4827:
                   4828: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
                   4829: {
                   4830:     VL tv,fv,vv,vc,av;
                   4831:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4832:     int e,max,nvar,i;
                   4833:     NDV b;
                   4834:     int ishomo,nalg,mrank,trank,wmax,len;
                   4835:   NMV a;
                   4836:     Alg alpha,dp;
                   4837:     P p,zp;
                   4838:     Q dmy;
                   4839:     LIST f1,f2,zpl;
                   4840:     Obj obj;
                   4841:     NumberField nf;
                   4842:     struct order_spec *ord1;
                   4843:     NODE tr,tl1,tl2,tl3,tl4,nzlist;
                   4844:     LIST l1,l2,l3,l4,l5;
                   4845:   int j;
                   4846:   Z jq,bpe,last_nonzero;
                   4847:     int *perm;
                   4848:     EPOS oepos;
                   4849:     int obpe,oadv,ompos,cbpe;
1.15      noro     4850:     VECT hvect;
1.1       noro     4851:
1.41      noro     4852:     NcriB = NcriMF = Ncri2 = 0;
1.1       noro     4853:     nd_module = 0;
                   4854:     if ( !m && Demand ) nd_demand = 1;
                   4855:     else nd_demand = 0;
                   4856:
                   4857:     if ( DP_Multiple )
                   4858:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4859: #if 0
                   4860:     ndv_alloc = 0;
                   4861: #endif
                   4862:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     4863:     parse_nd_option(vv,current_option);
1.1       noro     4864:     if ( m && nd_vc )
                   4865:        error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
                   4866:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4867:     switch ( ord->id ) {
                   4868:         case 1:
                   4869:             if ( ord->nv != nvar )
                   4870:                 error("nd_{gr,f4} : invalid order specification");
                   4871:             break;
                   4872:         default:
                   4873:             break;
                   4874:     }
                   4875:     nd_nalg = 0;
                   4876:     av = 0;
                   4877:     if ( !m ) {
                   4878:         get_algtree((Obj)f,&av);
                   4879:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4880:         nd_ntrans = nvar;
                   4881:         nd_nalg = nalg;
                   4882:         /* #i -> t#i */
                   4883:         if ( nalg ) {
                   4884:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4885:             ord = ord1;
                   4886:             f = f1;
                   4887:         }
                   4888:         nvar += nalg;
                   4889:     }
                   4890:     nd_init_ord(ord);
                   4891:     mrank = 0;
                   4892:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4893:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4894:             if ( nd_module ) {
1.16      noro     4895:                 if ( OID(BDY(t)) == O_DPM ) {
                   4896:                   e = dpm_getdeg((DPM)BDY(t),&trank);
                   4897:                   max = MAX(e,max);
                   4898:                   mrank = MAX(mrank,trank);
                   4899:                 } else {
                   4900:                   s = BDY((LIST)BDY(t));
                   4901:                   trank = length(s);
                   4902:                   mrank = MAX(mrank,trank);
                   4903:                   for ( ; s; s = NEXT(s) ) {
                   4904:                       e = getdeg(tv->v,(P)BDY(s));
                   4905:                       max = MAX(e,max);
                   4906:                   }
1.1       noro     4907:                 }
                   4908:             } else {
1.43      noro     4909:                 if ( OID(BDY(t)) == O_DP ) {
                   4910:                   e = dp_getdeg((DP)BDY(t));
                   4911:                   max = MAX(e,max);
                   4912:                 } else {
                   4913:                   e = getdeg(tv->v,(P)BDY(t));
                   4914:                   max = MAX(e,max);
                   4915:                 }
1.1       noro     4916:             }
                   4917:         }
                   4918:     nd_setup_parameters(nvar,nd_nzlist?0:max);
                   4919:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   4920:     ishomo = 1;
                   4921:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4922:       if ( nd_module ) {
1.16      noro     4923:         if ( OID(BDY(t)) == O_DPM ) {
                   4924:           Z cont;
                   4925:           DPM zdpm;
                   4926:
                   4927:           if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
                   4928:           else zdpm = (DPM)BDY(t);
                   4929:           b = (pointer)dpmtondv(m,zdpm);
                   4930:         } else {
                   4931:           if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   4932:           else zpl = (LIST)BDY(t);
1.1       noro     4933:           b = (pointer)pltondv(CO,vv,zpl);
1.16      noro     4934:         }
1.1       noro     4935:       } else {
1.43      noro     4936:         if ( OID(BDY(t)) == O_DP ) {
                   4937:           DP zdp;
                   4938:
                   4939:           if ( !m && !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
                   4940:           else zdp = (DP)BDY(t);
                   4941:           b = (pointer)dptondv(m,zdp);
                   4942:         } else {
                   4943:           if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   4944:           else zp = (P)BDY(t);
                   4945:           b = (pointer)ptondv(CO,vv,zp);
                   4946:         }
1.1       noro     4947:       }
                   4948:       if ( ishomo )
                   4949:         ishomo = ishomo && ndv_ishomo(b);
                   4950:       if ( m ) ndv_mod(m,b);
                   4951:       if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4952:     }
                   4953:     if ( fd0 ) NEXT(fd) = 0;
                   4954:
                   4955:   if ( !ishomo && homo ) {
                   4956:         for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   4957:             b = (NDV)BDY(t); len = LEN(b);
                   4958:             for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   4959:                 wmax = MAX(TD(DL(a)),wmax);
                   4960:         }
                   4961:         homogenize_order(ord,nvar,&ord1);
                   4962:         nd_init_ord(ord1);
                   4963:         nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   4964:         for ( t = fd0; t; t = NEXT(t) )
                   4965:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   4966:     }
                   4967:
1.24      noro     4968:     ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0,0);
1.1       noro     4969:     if ( nd_gentrace ) {
                   4970:         MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   4971:     }
                   4972:   if ( nd_splist ) {
                   4973:     *rp = compute_splist();
                   4974:     return;
                   4975:   }
                   4976:   if ( nd_check_splist ) {
                   4977:         if ( f4 ) {
                   4978:             if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
                   4979:             else *rp = 0;
                   4980:         } else {
                   4981:             if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
                   4982:             else *rp = 0;
                   4983:         }
                   4984:     return;
                   4985:   }
                   4986:     x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
                   4987:   if ( !x ) {
                   4988:     *rp = 0; return;
                   4989:   }
1.15      noro     4990:   if ( nd_gentrace ) {
                   4991:     MKVECT(hvect,nd_psn);
                   4992:     for ( i = 0; i < nd_psn; i++ )
                   4993:        ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   4994:   }
1.1       noro     4995:   if ( !ishomo && homo ) {
                   4996:        /* dehomogenization */
                   4997:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   4998:     nd_init_ord(ord);
                   4999:     nd_setup_parameters(nvar,0);
                   5000:   }
                   5001:     nd_demand = 0;
                   5002:   if ( nd_module && nd_intersect ) {
                   5003:     for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17      noro     5004:       if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1       noro     5005:         MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
                   5006:       }
                   5007:     conv_ilist(nd_demand,0,x,0);
                   5008:     goto FINAL;
                   5009:   }
                   5010:     if ( nd_gentrace  && f4 ) { nzlist = nd_alltracelist; }
                   5011:     x = ndv_reducebase(x,perm);
                   5012:     if ( nd_gentrace  && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
                   5013:     x = ndv_reduceall(m,x);
                   5014:     cbpe = nd_bpe;
                   5015:     if ( nd_gentrace && !f4 ) {
                   5016:         tl2 = nd_alltracelist; nd_alltracelist = 0;
                   5017:         ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
                   5018:         tl3 = nd_alltracelist; nd_alltracelist = 0;
                   5019:         if ( nd_gensyz ) {
                   5020:           nd_gb(m,0,1,1,0);
                   5021:             tl4 = nd_alltracelist; nd_alltracelist = 0;
                   5022:         } else tl4 = 0;
                   5023:     }
                   5024:     nd_bpe = cbpe;
                   5025:     nd_setup_parameters(nd_nvar,0);
                   5026: FINAL:
                   5027:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16      noro     5028:       NEXTNODE(r0,r);
                   5029:       if ( nd_module ) {
                   5030:         if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
                   5031:         else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
                   5032:       } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
                   5033:       else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1       noro     5034:     }
                   5035:     if ( r0 ) NEXT(r) = 0;
                   5036:     if ( !m && nd_nalg )
                   5037:         r0 = postprocess_algcoef(av,alist,r0);
                   5038:     MKLIST(*rp,r0);
                   5039:     if ( nd_gentrace ) {
                   5040:   if ( f4 ) {
1.6       noro     5041:             STOZ(16,bpe);
                   5042:             STOZ(nd_last_nonzero,last_nonzero);
1.15      noro     5043:             tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1       noro     5044:         } else {
                   5045:             tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   5046:             tl3 = reverse_node(tl3);
                   5047:             /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   5048:             for ( t = tl2; t; t = NEXT(t) ) {
                   5049:             /* s = [i,[*,j,*,*],...] */
                   5050:                 s = BDY((LIST)BDY(t));
1.6       noro     5051:                 j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     5052:                 for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     5053:                     j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     5054:                     ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   5055:                 }
                   5056:             }
                   5057:             for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6       noro     5058:                 STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     5059:             }
                   5060:             MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   5061:             MKLIST(l5,tl4);
1.6       noro     5062:             STOZ(nd_bpe,bpe);
1.15      noro     5063:             tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     5064:         }
                   5065:     }
                   5066: #if 0
                   5067:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   5068: #endif
                   5069: }
                   5070:
1.28      noro     5071: NODE nd_sba_f4(int m,int **indp);
                   5072:
                   5073: void nd_sba(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.24      noro     5074: {
                   5075:   VL tv,fv,vv,vc,av;
1.44      noro     5076:   NODE fd,fd0,r,r0,t,x,s,xx,nd,syz;
1.24      noro     5077:   int e,max,nvar,i;
                   5078:   NDV b;
                   5079:   int ishomo,nalg,wmax,len;
                   5080:   NMV a;
                   5081:   P p,zp;
                   5082:   Q dmy;
                   5083:   struct order_spec *ord1;
                   5084:   int j;
                   5085:   int *perm;
                   5086:   EPOS oepos;
                   5087:   int obpe,oadv,ompos,cbpe;
1.51      noro     5088:   struct oEGT eg0,eg1,egconv,egintred;
1.24      noro     5089:
                   5090:   nd_module = 0;
                   5091:   nd_demand = 0;
1.35      noro     5092:   Nsamesig = 0;
1.24      noro     5093:   if ( DP_Multiple )
                   5094:     nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   5095:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     5096:   parse_nd_option(vv,current_option);
1.24      noro     5097:   if ( m && nd_vc )
                   5098:     error("nd_sba : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
                   5099:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5100:   switch ( ord->id ) {
                   5101:     case 1:
                   5102:       if ( ord->nv != nvar )
                   5103:         error("nd_sba : invalid order specification");
                   5104:         break;
                   5105:       default:
                   5106:         break;
                   5107:   }
                   5108:   nd_nalg = 0;
                   5109:   nd_init_ord(ord);
                   5110:   // for SIG comparison
                   5111:   initd(ord);
                   5112:   for ( t = BDY(f), max = 1; t; t = NEXT(t) ) {
                   5113:     for ( tv = vv; tv; tv = NEXT(tv) ) {
1.43      noro     5114:       if ( OID(BDY(t)) == O_DP ) {
                   5115:         e = dp_getdeg((DP)BDY(t));
                   5116:         max = MAX(e,max);
                   5117:       } else {
                   5118:         e = getdeg(tv->v,(P)BDY(t));
                   5119:         max = MAX(e,max);
                   5120:       }
1.24      noro     5121:     }
                   5122:   }
1.25      noro     5123:   nd_setup_parameters(nvar,max);
1.24      noro     5124:   obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   5125:   ishomo = 1;
                   5126:   for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.43      noro     5127:     if ( OID(BDY(t)) == O_DP ) {
                   5128:       DP zdp;
                   5129:
                   5130:       if ( !m ) dp_ptozp((DP)BDY(t),&zdp);
                   5131:       else zdp = (DP)BDY(t);
                   5132:       b = (pointer)dptondv(m,zdp);
                   5133:     } else {
                   5134:       if ( !m ) ptozp((P)BDY(t),1,&dmy,&zp);
                   5135:       else zp = (P)BDY(t);
                   5136:       b = (pointer)ptondv(CO,vv,zp);
                   5137:     }
1.24      noro     5138:     if ( ishomo )
                   5139:       ishomo = ishomo && ndv_ishomo(b);
                   5140:     if ( m ) ndv_mod(m,b);
                   5141:     if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   5142:   }
                   5143:   if ( fd0 ) NEXT(fd) = 0;
                   5144:
                   5145:   if ( !ishomo && homo ) {
                   5146:     for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   5147:       b = (NDV)BDY(t); len = LEN(b);
                   5148:       for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   5149:         wmax = MAX(TD(DL(a)),wmax);
                   5150:       }
                   5151:       homogenize_order(ord,nvar,&ord1);
                   5152:       nd_init_ord(ord1);
1.42      noro     5153:       // for SIG comparison
                   5154:       initd(ord1);
1.24      noro     5155:       nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   5156:       for ( t = fd0; t; t = NEXT(t) )
                   5157:         ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   5158:   }
                   5159:
1.39      noro     5160:   ndv_setup(m,0,fd0,nd_sba_dontsort,0,1);
1.44      noro     5161:   x = f4 ? nd_sba_f4(m,&perm) : nd_sba_buch(m,ishomo || homo,&perm,&syz);
1.24      noro     5162:   if ( !x ) {
                   5163:     *rp = 0; return;
                   5164:   }
                   5165:   if ( !ishomo && homo ) {
                   5166:        /* dehomogenization */
                   5167:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   5168:     nd_init_ord(ord);
1.42      noro     5169:     // for SIG comparison
                   5170:     initd(ord);
1.24      noro     5171:     nd_setup_parameters(nvar,0);
                   5172:   }
                   5173:   nd_demand = 0;
1.51      noro     5174:   get_eg(&eg0);
1.24      noro     5175:   x = ndv_reducebase(x,perm);
                   5176:   x = ndv_reduceall(m,x);
1.51      noro     5177:   get_eg(&eg1); init_eg(&egintred); add_eg(&egintred,&eg0,&eg1);
1.24      noro     5178:   nd_setup_parameters(nd_nvar,0);
1.29      noro     5179:   get_eg(&eg0);
1.24      noro     5180:   for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   5181:     NEXTNODE(r0,r);
                   5182:     if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
1.34      noro     5183:     else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.24      noro     5184:   }
                   5185:   if ( r0 ) NEXT(r) = 0;
1.44      noro     5186:   if ( nd_sba_syz ) {
                   5187:     LIST gb,hsyz;
                   5188:     NODE nd;
                   5189:
                   5190:     MKLIST(gb,r0);
                   5191:     MKLIST(hsyz,syz);
                   5192:     nd = mknode(2,gb,hsyz);
                   5193:     MKLIST(*rp,nd);
                   5194:   } else
                   5195:     MKLIST(*rp,r0);
1.29      noro     5196:   get_eg(&eg1); init_eg(&egconv); add_eg(&egconv,&eg0,&eg1);
1.51      noro     5197:   print_eg("intred",&egintred); fprintf(asir_out,"\n");
1.29      noro     5198:   print_eg("conv",&egconv); fprintf(asir_out,"\n");
1.24      noro     5199: }
                   5200:
1.1       noro     5201: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
                   5202: {
                   5203:     VL tv,fv,vv,vc,av;
                   5204:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   5205:     int e,max,nvar,i;
                   5206:     NDV b;
                   5207:     int ishomo,nalg;
                   5208:     Alg alpha,dp;
                   5209:     P p,zp;
                   5210:     Q dmy;
                   5211:     LIST f1,f2;
                   5212:     Obj obj;
                   5213:     NumberField nf;
                   5214:     struct order_spec *ord1;
                   5215:     int *perm;
                   5216:
                   5217:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     5218:     parse_nd_option(vv,current_option);
1.1       noro     5219:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5220:     switch ( ord->id ) {
                   5221:         case 1:
                   5222:             if ( ord->nv != nvar )
                   5223:                 error("nd_check : invalid order specification");
                   5224:             break;
                   5225:         default:
                   5226:             break;
                   5227:     }
                   5228:     nd_nalg = 0;
                   5229:     av = 0;
                   5230:     if ( !m ) {
                   5231:         get_algtree((Obj)f,&av);
                   5232:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   5233:         nd_ntrans = nvar;
                   5234:         nd_nalg = nalg;
                   5235:         /* #i -> t#i */
                   5236:         if ( nalg ) {
                   5237:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   5238:             ord = ord1;
                   5239:             f = f1;
                   5240:         }
                   5241:         nvar += nalg;
                   5242:     }
                   5243:     nd_init_ord(ord);
                   5244:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   5245:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   5246:             e = getdeg(tv->v,(P)BDY(t));
                   5247:             max = MAX(e,max);
                   5248:         }
                   5249:     nd_setup_parameters(nvar,max);
                   5250:     ishomo = 1;
                   5251:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   5252:         ptozp((P)BDY(t),1,&dmy,&zp);
                   5253:         b = (pointer)ptondv(CO,vv,zp);
                   5254:         if ( ishomo )
                   5255:             ishomo = ishomo && ndv_ishomo(b);
                   5256:         if ( m ) ndv_mod(m,b);
                   5257:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   5258:     }
                   5259:     if ( fd0 ) NEXT(fd) = 0;
1.24      noro     5260:     ndv_setup(m,0,fd0,0,1,0);
1.1       noro     5261:     for ( x = 0, i = 0; i < nd_psn; i++ )
                   5262:         x = update_base(x,i);
                   5263:     if ( do_check ) {
                   5264:         x = nd_gb(m,ishomo,1,0,&perm);
                   5265:         if ( !x ) {
                   5266:             *rp = 0;
                   5267:             return;
                   5268:         }
                   5269:     } else {
                   5270: #if 0
                   5271:     /* bug ? */
                   5272:         for ( t = x; t; t = NEXT(t) )
                   5273:             BDY(t) = (pointer)nd_ps[(long)BDY(t)];
                   5274: #else
                   5275:     conv_ilist(0,0,x,&perm);
                   5276: #endif
                   5277:     }
                   5278:     x = ndv_reducebase(x,perm);
                   5279:     x = ndv_reduceall(m,x);
                   5280:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   5281:         NEXTNODE(r0,r);
                   5282:         BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   5283:     }
                   5284:     if ( r0 ) NEXT(r) = 0;
                   5285:     if ( !m && nd_nalg )
                   5286:         r0 = postprocess_algcoef(av,alist,r0);
                   5287:     MKLIST(*rp,r0);
                   5288: }
                   5289:
                   5290: NDV recompute_trace(NODE trace,NDV *p,int m);
                   5291: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
                   5292:
                   5293: NDV recompute_trace(NODE ti,NDV *p,int mod)
                   5294: {
                   5295:   int c,c1,c2,i;
                   5296:   NM mul,m,tail;
                   5297:   ND d,r,rm;
                   5298:   NODE sj;
                   5299:   NDV red;
                   5300:   Obj mj;
                   5301:
                   5302:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   5303:   CM(mul) = 1;
                   5304:   tail = 0;
                   5305:   for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
                   5306:     sj = BDY((LIST)BDY(ti));
                   5307:     if ( ARG0(sj) ) {
1.6       noro     5308:       red = p[ZTOS((Q)ARG1(sj))];
1.1       noro     5309:       mj = (Obj)ARG2(sj);
                   5310:       if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
                   5311:       else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
                   5312:       rm = ndv_mul_nm(mod,mul,red);
                   5313:       if ( !r ) r = rm;
                   5314:       else {
                   5315:         for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
                   5316:           if ( d ) {
                   5317:             NEXT(tail) = m; tail = m; LEN(d)++;
                   5318:           } else {
                   5319:             MKND(nd_nvar,m,1,d); tail = BDY(d);
                   5320:           }
                   5321:         }
                   5322:         if ( !m ) return 0; /* failure */
                   5323:                 else {
                   5324:           BDY(r) = m;
                   5325:                     if ( mod > 0 || mod == -1 ) {
                   5326:             c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
                   5327:             DMAR(c1,c2,0,mod,c);
                   5328:             nd_mul_c(mod,rm,c);
                   5329:                     } else {
                   5330:                       Z t,u;
                   5331:
                   5332:                       chsgnlf(HCZ(r),&t);
                   5333:                       divlf(t,HCZ(rm),&u);
                   5334:                       nd_mul_c_lf(rm,u);
                   5335:                     }
                   5336:           r = nd_add(mod,r,rm);
                   5337:         }
                   5338:           }
                   5339:      }
                   5340:   }
                   5341:   if ( tail ) NEXT(tail) = 0;
                   5342:   d = nd_add(mod,d,r);
                   5343:   nd_mul_c(mod,d,invm(HCM(d),mod));
                   5344:   return ndtondv(mod,d);
                   5345: }
                   5346:
                   5347: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
                   5348: {
                   5349:     VL tv,fv,vv,vc,av;
                   5350:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   5351:     int e,max,nvar,i;
                   5352:     NDV b;
                   5353:     int ishomo,nalg;
                   5354:     Alg alpha,dp;
                   5355:     P p,zp;
                   5356:     Q dmy;
                   5357:     LIST f1,f2;
                   5358:     Obj obj;
                   5359:     NumberField nf;
                   5360:     struct order_spec *ord1;
                   5361:   NODE permtrace,intred,ind,perm,trace,ti;
                   5362:   int len,n,j;
                   5363:   NDV *db,*pb;
                   5364:
                   5365:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     5366:     parse_nd_option(vv,current_option);
1.1       noro     5367:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5368:     switch ( ord->id ) {
                   5369:         case 1:
                   5370:             if ( ord->nv != nvar )
                   5371:                 error("nd_check : invalid order specification");
                   5372:             break;
                   5373:         default:
                   5374:             break;
                   5375:     }
                   5376:     nd_init_ord(ord);
1.6       noro     5377:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     5378:     nd_setup_parameters(nvar,0);
                   5379:
                   5380:   len = length(BDY(f));
                   5381:   db = (NDV *)MALLOC(len*sizeof(NDV *));
                   5382:   for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
                   5383:       ptozp((P)BDY(t),1,&dmy,&zp);
                   5384:       b = ptondv(CO,vv,zp);
                   5385:         ndv_mod(m,b);
                   5386:     ndv_mul_c(m,b,invm(HCM(b),m));
                   5387:     db[i] = b;
                   5388:     }
                   5389:
                   5390:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   5391:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   5392:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   5393:   perm = BDY((LIST)ARG0(permtrace));
                   5394:   trace = NEXT(permtrace);
                   5395:
                   5396:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     5397:     j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1       noro     5398:     if ( j > i ) i = j;
                   5399:   }
                   5400:   n = i+1;
                   5401:   pb = (NDV *)MALLOC(n*sizeof(NDV *));
                   5402:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   5403:     ti = BDY((LIST)BDY(t));
1.6       noro     5404:     pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1       noro     5405:   }
                   5406:   for ( t = trace; t; t = NEXT(t) ) {
                   5407:     ti = BDY((LIST)BDY(t));
1.6       noro     5408:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   5409:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     5410:       if ( DP_Print ) {
                   5411:            fprintf(asir_out,"."); fflush(asir_out);
                   5412:       }
                   5413:   }
                   5414:   for ( t = intred; t; t = NEXT(t) ) {
                   5415:     ti = BDY((LIST)BDY(t));
1.6       noro     5416:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   5417:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     5418:       if ( DP_Print ) {
                   5419:            fprintf(asir_out,"*"); fflush(asir_out);
                   5420:       }
                   5421:   }
                   5422:     for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
                   5423:         NEXTNODE(r0,r);
1.6       noro     5424:     b = pb[ZTOS((Q)BDY(t))];
1.1       noro     5425:         ndv_mul_c(m,b,invm(HCM(b),m));
                   5426: #if 0
1.6       noro     5427:         BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1       noro     5428: #else
1.6       noro     5429:         BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1       noro     5430: #endif
                   5431:     }
                   5432:     if ( r0 ) NEXT(r) = 0;
                   5433:     MKLIST(*rp,r0);
                   5434:     if ( DP_Print ) fprintf(asir_out,"\n");
                   5435: }
                   5436:
1.16      noro     5437: 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     5438: {
                   5439:     VL tv,fv,vv,vc,av;
                   5440:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   5441:     int m,nocheck,nvar,mindex,e,max;
                   5442:     NDV c;
                   5443:     NMV a;
                   5444:     P p,zp;
                   5445:     Q dmy;
                   5446:     EPOS oepos;
                   5447:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   5448:     Alg alpha,dp;
                   5449:     P poly;
                   5450:     LIST f1,f2,zpl;
                   5451:     Obj obj;
                   5452:     NumberField nf;
                   5453:     struct order_spec *ord1;
                   5454:     struct oEGT eg_check,eg0,eg1;
                   5455:     NODE tr,tl1,tl2,tl3,tl4;
                   5456:     LIST l1,l2,l3,l4,l5;
                   5457:     int *perm;
                   5458:     int j,ret;
                   5459:     Z jq,bpe;
1.15      noro     5460:     VECT hvect;
1.1       noro     5461:
1.41      noro     5462:     NcriB = NcriMF = Ncri2 = 0;
1.1       noro     5463:     nd_module = 0;
                   5464:     nd_lf = 0;
1.46      noro     5465:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   5466:     parse_nd_option(vv,current_option);
1.1       noro     5467:     if ( nd_lf ) {
                   5468:       if ( f4 )
                   5469:         nd_f4_lf_trace(f,v,trace,homo,ord,rp);
                   5470:       else
                   5471:         error("nd_gr_trace is not implemented yet over a large finite field");
                   5472:       return;
                   5473:     }
                   5474:     if ( DP_Multiple )
                   5475:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   5476:
                   5477:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5478:     switch ( ord->id ) {
                   5479:         case 1:
                   5480:             if ( ord->nv != nvar )
                   5481:                 error("nd_gr_trace : invalid order specification");
                   5482:             break;
                   5483:         default:
                   5484:             break;
                   5485:     }
                   5486:
                   5487:     get_algtree((Obj)f,&av);
                   5488:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   5489:     nd_ntrans = nvar;
                   5490:     nd_nalg = nalg;
                   5491:     /* #i -> t#i */
                   5492:     if ( nalg ) {
                   5493:         preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   5494:         ord = ord1;
                   5495:         f = f1;
                   5496:     }
                   5497:     nvar += nalg;
                   5498:
                   5499:     nocheck = 0;
                   5500:     mindex = 0;
                   5501:
                   5502:     if ( Demand ) nd_demand = 1;
                   5503:     else nd_demand = 0;
                   5504:
                   5505:     /* setup modulus */
                   5506:     if ( trace < 0 ) {
                   5507:         trace = -trace;
                   5508:         nocheck = 1;
                   5509:     }
                   5510:     m = trace > 1 ? trace : get_lprime(mindex);
                   5511:     nd_init_ord(ord);
                   5512:     mrank = 0;
                   5513:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   5514:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   5515:             if ( nd_module ) {
1.16      noro     5516:               if ( OID(BDY(t)) == O_DPM ) {
                   5517:                 e = dpm_getdeg((DPM)BDY(t),&trank);
                   5518:                 max = MAX(e,max);
                   5519:                 mrank = MAX(mrank,trank);
                   5520:               } else {
1.1       noro     5521:                 s = BDY((LIST)BDY(t));
                   5522:                 trank = length(s);
                   5523:                 mrank = MAX(mrank,trank);
                   5524:                 for ( ; s; s = NEXT(s) ) {
                   5525:                     e = getdeg(tv->v,(P)BDY(s));
                   5526:                     max = MAX(e,max);
                   5527:                 }
1.16      noro     5528:               }
1.1       noro     5529:             } else {
1.43      noro     5530:               if ( OID(BDY(t)) == O_DP ) {
                   5531:                 e = dp_getdeg((DP)BDY(t));
                   5532:                 max = MAX(e,max);
                   5533:               } else {
1.1       noro     5534:                 e = getdeg(tv->v,(P)BDY(t));
                   5535:                 max = MAX(e,max);
1.43      noro     5536:               }
1.1       noro     5537:             }
                   5538:         }
                   5539:     nd_setup_parameters(nvar,max);
                   5540:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   5541:     ishomo = 1;
                   5542:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   5543:         if ( nd_module ) {
1.16      noro     5544:           if ( OID(BDY(t)) == O_DPM ) {
                   5545:             Z cont;
                   5546:             DPM zdpm;
                   5547:
1.17      noro     5548:             if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16      noro     5549:             else zdpm = (DPM)BDY(t);
                   5550:             c = (pointer)dpmtondv(m,zdpm);
                   5551:           } else {
                   5552:             if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   5553:             else zpl = (LIST)BDY(t);
1.1       noro     5554:             c = (pointer)pltondv(CO,vv,zpl);
1.16      noro     5555:           }
1.1       noro     5556:         } else {
1.43      noro     5557:           if ( OID(BDY(t)) == O_DP ) {
                   5558:             DP zdp;
                   5559:
                   5560:             if ( !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
                   5561:             else zdp = (DP)BDY(t);
                   5562:             c = (pointer)dptondv(m,zdp);
                   5563:           } else {
                   5564:             if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   5565:             else zp = (P)BDY(t);
                   5566:             c = (pointer)ptondv(CO,vv,zp);
                   5567:           }
1.1       noro     5568:         }
                   5569:         if ( ishomo )
                   5570:             ishomo = ishomo && ndv_ishomo(c);
                   5571:         if ( c ) {
                   5572:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   5573:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   5574:         }
                   5575:     }
                   5576:     if ( in0 ) NEXT(in) = 0;
                   5577:     if ( fd0 ) NEXT(fd) = 0;
                   5578:     if ( !ishomo && homo ) {
                   5579:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   5580:             c = (NDV)BDY(t); len = LEN(c);
                   5581:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   5582:                 wmax = MAX(TD(DL(a)),wmax);
                   5583:         }
                   5584:         homogenize_order(ord,nvar,&ord1);
                   5585:         nd_init_ord(ord1);
                   5586:         nd_setup_parameters(nvar+1,wmax);
                   5587:         for ( t = fd0; t; t = NEXT(t) )
                   5588:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   5589:     }
                   5590:     if ( MaxDeg > 0 ) nocheck = 1;
                   5591:     while ( 1 ) {
                   5592:     tl1 = tl2 = tl3 = tl4 = 0;
                   5593:         if ( Demand )
                   5594:             nd_demand = 1;
1.24      noro     5595:         ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1       noro     5596:         if ( nd_gentrace ) {
                   5597:             MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   5598:         }
                   5599:         if ( ret )
                   5600:             cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
                   5601:         if ( !ret || !cand ) {
                   5602:             /* failure */
                   5603:             if ( trace > 1 ) { *rp = 0; return; }
                   5604:             else m = get_lprime(++mindex);
                   5605:             continue;
                   5606:         }
1.15      noro     5607:         if ( nd_gentrace ) {
                   5608:           MKVECT(hvect,nd_psn);
                   5609:           for ( i = 0; i < nd_psn; i++ )
                   5610:              ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   5611:         }
1.1       noro     5612:         if ( !ishomo && homo ) {
                   5613:             /* dehomogenization */
                   5614:             for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   5615:             nd_init_ord(ord);
                   5616:             nd_setup_parameters(nvar,0);
                   5617:         }
                   5618:         nd_demand = 0;
                   5619:         cand = ndv_reducebase(cand,perm);
                   5620:         if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
                   5621:         cand = ndv_reduceall(0,cand);
                   5622:         cbpe = nd_bpe;
                   5623:         if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
                   5624:         get_eg(&eg0);
                   5625:         if ( nocheck )
                   5626:             break;
                   5627:         if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   5628:             if ( nd_gentrace ) {
                   5629:           tl3 = nd_alltracelist; nd_alltracelist = 0;
                   5630:         } else tl3 = 0;
                   5631:             /* gbcheck : cand is a GB of Id(cand) ? */
1.54    ! noro     5632:             if ( nd_vc || nd_gentrace || nd_gensyz || do_weyl )
1.1       noro     5633:               ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
                   5634:             else
                   5635:               ret = nd_f4(0,1,0)!=0;
                   5636:             if ( nd_gentrace && nd_gensyz ) {
                   5637:           tl4 = nd_alltracelist; nd_alltracelist = 0;
                   5638:         } else tl4 = 0;
                   5639:     }
                   5640:     if ( ret ) break;
                   5641:         else if ( trace > 1 ) {
                   5642:             /* failure */
                   5643:             *rp = 0; return;
                   5644:         } else {
                   5645:             /* try the next modulus */
                   5646:             m = get_lprime(++mindex);
                   5647:             /* reset the parameters */
                   5648:             if ( !ishomo && homo ) {
                   5649:                 nd_init_ord(ord1);
                   5650:                 nd_setup_parameters(nvar+1,wmax);
                   5651:             } else {
                   5652:                 nd_init_ord(ord);
                   5653:                 nd_setup_parameters(nvar,max);
                   5654:             }
                   5655:         }
                   5656:     }
                   5657:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   5658:     if ( DP_Print )
1.6       noro     5659:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     5660:     /* dp->p */
                   5661:     nd_bpe = cbpe;
                   5662:     nd_setup_parameters(nd_nvar,0);
                   5663:     for ( r = cand; r; r = NEXT(r) ) {
1.16      noro     5664:       if ( nd_module ) {
1.17      noro     5665:         if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16      noro     5666:         else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17      noro     5667:       } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
                   5668:       else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1       noro     5669:     }
                   5670:     if ( nd_nalg )
                   5671:         cand = postprocess_algcoef(av,alist,cand);
                   5672:     MKLIST(*rp,cand);
                   5673:     if ( nd_gentrace ) {
                   5674:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   5675:     tl3 = reverse_node(tl3);
                   5676:     /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   5677:         for ( t = tl2; t; t = NEXT(t) ) {
                   5678:       /* s = [i,[*,j,*,*],...] */
                   5679:             s = BDY((LIST)BDY(t));
1.6       noro     5680:             j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     5681:       for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     5682:                 j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     5683:         ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   5684:             }
                   5685:     }
                   5686:     for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6       noro     5687:         STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     5688:     }
                   5689:         MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   5690:     MKLIST(l5,tl4);
1.6       noro     5691:       STOZ(nd_bpe,bpe);
1.15      noro     5692:         tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     5693:     }
                   5694: }
                   5695:
                   5696: /* XXX : module element is not considered  */
                   5697:
                   5698: void dltondl(int n,DL dl,UINT *r)
                   5699: {
                   5700:     UINT *d;
                   5701:     int i,j,l,s,ord_l;
                   5702:     struct order_pair *op;
                   5703:
                   5704:     d = (unsigned int *)dl->d;
                   5705:     for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
                   5706:     if ( nd_blockmask ) {
                   5707:         l = nd_blockmask->n;
                   5708:         op = nd_blockmask->order_pair;
                   5709:         for ( j = 0, s = 0; j < l; j++ ) {
                   5710:             ord_l = op[j].length;
                   5711:             for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
                   5712:         }
                   5713:         TD(r) = ndl_weight(r);
                   5714:         ndl_weight_mask(r);
                   5715:     } else {
                   5716:         for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
                   5717:         TD(r) = ndl_weight(r);
                   5718:     }
                   5719: }
                   5720:
                   5721: DL ndltodl(int n,UINT *ndl)
                   5722: {
                   5723:     DL dl;
                   5724:     int *d;
                   5725:     int i,j,l,s,ord_l;
                   5726:     struct order_pair *op;
                   5727:
1.33      noro     5728:     NEWDL_NOINIT(dl,n);
1.1       noro     5729:     dl->td = TD(ndl);
                   5730:     d = dl->d;
                   5731:     if ( nd_blockmask ) {
                   5732:         l = nd_blockmask->n;
                   5733:         op = nd_blockmask->order_pair;
                   5734:         for ( j = 0, s = 0; j < l; j++ ) {
                   5735:             ord_l = op[j].length;
                   5736:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   5737:         }
                   5738:     } else {
                   5739:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   5740:     }
                   5741:     return dl;
                   5742: }
                   5743:
1.24      noro     5744: void _ndltodl(UINT *ndl,DL dl)
                   5745: {
                   5746:     int *d;
                   5747:     int i,j,l,s,ord_l,n;
                   5748:     struct order_pair *op;
                   5749:
                   5750:     n = nd_nvar;
                   5751:     dl->td = TD(ndl);
                   5752:     d = dl->d;
                   5753:     if ( nd_blockmask ) {
                   5754:         l = nd_blockmask->n;
                   5755:         op = nd_blockmask->order_pair;
                   5756:         for ( j = 0, s = 0; j < l; j++ ) {
                   5757:             ord_l = op[j].length;
                   5758:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   5759:         }
                   5760:     } else {
                   5761:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   5762:     }
                   5763: }
                   5764:
1.1       noro     5765: void nmtodp(int mod,NM m,DP *r)
                   5766: {
                   5767:     DP dp;
                   5768:     MP mr;
                   5769:
                   5770:     NEWMP(mr);
                   5771:     mr->dl = ndltodl(nd_nvar,DL(m));
                   5772:     mr->c = (Obj)ndctop(mod,m->c);
                   5773:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   5774:     *r = dp;
                   5775: }
                   5776:
1.15      noro     5777: void ndltodp(UINT *d,DP *r)
                   5778: {
                   5779:     DP dp;
                   5780:     MP mr;
                   5781:
                   5782:     NEWMP(mr);
                   5783:     mr->dl = ndltodl(nd_nvar,d);
                   5784:     mr->c = (Obj)ONE;
                   5785:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   5786:     *r = dp;
                   5787: }
                   5788:
1.1       noro     5789: void ndl_print(UINT *dl)
                   5790: {
                   5791:     int n;
                   5792:     int i,j,l,ord_l,s,s0;
                   5793:     struct order_pair *op;
                   5794:
                   5795:     n = nd_nvar;
                   5796:     printf("<<");
                   5797:     if ( nd_blockmask ) {
                   5798:         l = nd_blockmask->n;
                   5799:         op = nd_blockmask->order_pair;
                   5800:         for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   5801:             ord_l = op[j].length;
                   5802:             for ( i = 0; i < ord_l; i++, s++ )
                   5803:                 printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
                   5804:         }
                   5805:     } else {
                   5806:         for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
                   5807:     }
                   5808:     printf(">>");
                   5809:     if ( nd_module && MPOS(dl) )
                   5810:         printf("*e%d",MPOS(dl));
                   5811: }
                   5812:
                   5813: void nd_print(ND p)
                   5814: {
                   5815:     NM m;
                   5816:
                   5817:     if ( !p )
                   5818:         printf("0\n");
                   5819:     else {
                   5820:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5821:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     5822:             else printf("+%ld*",CM(m));
1.1       noro     5823:             ndl_print(DL(m));
                   5824:         }
                   5825:         printf("\n");
                   5826:     }
                   5827: }
                   5828:
                   5829: void nd_print_q(ND p)
                   5830: {
                   5831:     NM m;
                   5832:
                   5833:     if ( !p )
                   5834:         printf("0\n");
                   5835:     else {
                   5836:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5837:             printf("+");
1.6       noro     5838:             printexpr(CO,(Obj)CZ(m));
1.1       noro     5839:             printf("*");
                   5840:             ndl_print(DL(m));
                   5841:         }
                   5842:         printf("\n");
                   5843:     }
                   5844: }
                   5845:
                   5846: void ndp_print(ND_pairs d)
                   5847: {
                   5848:     ND_pairs t;
                   5849:
                   5850:     for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
                   5851:     printf("\n");
                   5852: }
                   5853:
                   5854: void nd_removecont(int mod,ND p)
                   5855: {
                   5856:     int i,n;
                   5857:     Z *w;
                   5858:     NM m;
                   5859:     struct oVECT v;
                   5860:
                   5861:     if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
                   5862:     else if ( mod == -2 ) {
                   5863:       Z inv;
                   5864:       divlf(ONE,HCZ(p),&inv);
                   5865:       nd_mul_c_lf(p,inv);
                   5866:     } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
                   5867:     else {
                   5868:         for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   5869:         w = (Z *)MALLOC(n*sizeof(Q));
                   5870:         v.len = n;
                   5871:         v.body = (pointer *)w;
1.6       noro     5872:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5873:         removecont_array((P *)w,n,1);
1.6       noro     5874:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5875:     }
                   5876: }
                   5877:
                   5878: void nd_removecont2(ND p1,ND p2)
                   5879: {
                   5880:     int i,n1,n2,n;
                   5881:     Z *w;
                   5882:     NM m;
                   5883:     struct oVECT v;
                   5884:
                   5885:     n1 = nd_length(p1);
                   5886:     n2 = nd_length(p2);
                   5887:     n = n1+n2;
                   5888:     w = (Z *)MALLOC(n*sizeof(Q));
                   5889:     v.len = n;
                   5890:     v.body = (pointer *)w;
                   5891:     i = 0;
                   5892:     if ( p1 )
1.6       noro     5893:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5894:     if ( p2 )
1.6       noro     5895:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5896:     removecont_array((P *)w,n,1);
                   5897:     i = 0;
                   5898:     if ( p1 )
1.6       noro     5899:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5900:     if ( p2 )
1.6       noro     5901:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5902: }
                   5903:
                   5904: void ndv_removecont(int mod,NDV p)
                   5905: {
                   5906:     int i,len,all_p;
                   5907:     Z *c;
                   5908:     P *w;
                   5909:     Z dvr,t;
                   5910:     P g,cont,tp;
                   5911:     NMV m;
                   5912:
                   5913:     if ( mod == -1 )
                   5914:         ndv_mul_c(mod,p,_invsf(HCM(p)));
                   5915:     else if ( mod == -2 ) {
                   5916:       Z inv;
                   5917:       divlf(ONE,HCZ(p),&inv);
                   5918:       ndv_mul_c_lf(p,inv);
                   5919:     } else if ( mod )
                   5920:         ndv_mul_c(mod,p,invm(HCM(p),mod));
                   5921:     else {
                   5922:         len = p->len;
                   5923:         w = (P *)MALLOC(len*sizeof(P));
                   5924:         c = (Z *)MALLOC(len*sizeof(Q));
                   5925:         for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
                   5926:             ptozp(CP(m),1,(Q *)&c[i],&w[i]);
                   5927:             all_p = all_p && !NUM(w[i]);
                   5928:         }
                   5929:         if ( all_p ) {
                   5930:             qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
                   5931:             mulp(nd_vc,(P)dvr,g,&cont);
                   5932:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5933:                 divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
                   5934:             }
                   5935:         } else {
                   5936:             sortbynm((Q *)c,len);
                   5937:             qltozl((Q *)c,len,&dvr);
                   5938:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5939:                 divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
                   5940:             }
                   5941:         }
                   5942:     }
                   5943: }
                   5944:
                   5945: /* koko */
                   5946:
                   5947: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
                   5948: {
                   5949:     int len,i,max;
                   5950:     NMV m,mr0,mr,t;
                   5951:
                   5952:     len = p->len;
1.14      noro     5953:     for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1       noro     5954:         max = MAX(max,TD(DL(m)));
                   5955:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   5956:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   5957:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   5958:     t = (NMV)MALLOC(nmv_adv);
                   5959:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   5960:         ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6       noro     5961:         CZ(mr) = CZ(m);
1.1       noro     5962:         ndl_copy(DL(t),DL(mr));
                   5963:     }
                   5964:     NV(p)++;
                   5965:     BDY(p) = mr0;
                   5966: }
                   5967:
                   5968: void ndv_dehomogenize(NDV p,struct order_spec *ord)
                   5969: {
                   5970:     int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
                   5971:     int pos;
                   5972:     Q *w;
                   5973:     Q dvr,t;
                   5974:     NMV m,r;
                   5975:
                   5976:     len = p->len;
                   5977:     newnvar = nd_nvar-1;
                   5978:     newexporigin = nd_get_exporigin(ord);
                   5979:     if ( nd_module ) newmpos = newexporigin-1;
                   5980:     newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
                   5981:     for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
                   5982:         ndl_dehomogenize(DL(m));
                   5983:     if ( newwpd != nd_wpd ) {
                   5984:         newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
                   5985:         for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6       noro     5986:             CZ(r) = CZ(m);
1.1       noro     5987:             if ( nd_module ) pos = MPOS(DL(m));
                   5988:             for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   5989:             adj = nd_exporigin-newexporigin;
                   5990:             for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
                   5991:             if ( nd_module ) {
                   5992:                 DL(r)[newmpos] = pos;
                   5993:             }
                   5994:         }
                   5995:     }
                   5996:     NV(p)--;
                   5997: }
                   5998:
                   5999: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
                   6000: {
                   6001:     int i;
                   6002:     P *tpl,*tpl1;
                   6003:     NODE l;
                   6004:     P h,gcd,t;
                   6005:
                   6006:     tpl = (P *)MALLOC(m*sizeof(P));
                   6007:     tpl1 = (P *)MALLOC(m*sizeof(P));
                   6008:     bcopy(pl,tpl,m*sizeof(P));
                   6009:     gcd = (P)ONE;
                   6010:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   6011:         h = (P)BDY(l);
                   6012:         while ( 1 ) {
                   6013:             for ( i = 0; i < m; i++ )
                   6014:                 if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
                   6015:                     break;
                   6016:             if ( i == m ) {
                   6017:                 bcopy(tpl1,tpl,m*sizeof(P));
                   6018:                 mulp(vl,gcd,h,&t); gcd = t;
                   6019:             } else
                   6020:                 break;
                   6021:         }
                   6022:     }
                   6023:     if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
                   6024:     if ( full ) {
                   6025:         heu_nezgcdnpz(vl,tpl,m,&t);
                   6026:         mulp(vl,gcd,t,pr);
                   6027:     } else
                   6028:         *pr = gcd;
                   6029: }
                   6030:
                   6031: void removecont_array(P *p,int n,int full)
                   6032: {
                   6033:     int all_p,all_q,i;
                   6034:     Z *c;
                   6035:     P *w;
                   6036:     P t,s;
                   6037:
                   6038:     for ( all_q = 1, i = 0; i < n; i++ )
                   6039:         all_q = all_q && NUM(p[i]);
                   6040:     if ( all_q ) {
                   6041:         removecont_array_q((Z *)p,n);
                   6042:     } else {
                   6043:         c = (Z *)MALLOC(n*sizeof(Z));
                   6044:         w = (P *)MALLOC(n*sizeof(P));
                   6045:         for ( i = 0; i < n; i++ ) {
                   6046:             ptozp(p[i],1,(Q *)&c[i],&w[i]);
                   6047:         }
                   6048:         removecont_array_q(c,n);
                   6049:         nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
                   6050:         for ( i = 0; i < n; i++ ) {
                   6051:             divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
                   6052:         }
                   6053:     }
                   6054: }
                   6055:
                   6056: /* c is an int array */
                   6057:
                   6058: void removecont_array_q(Z *c,int n)
                   6059: {
                   6060:   struct oVECT v;
                   6061:   Z d0,d1,a,u,u1,gcd;
                   6062:   int i,j;
                   6063:   Z *q,*r;
                   6064:
                   6065:   q = (Z *)MALLOC(n*sizeof(Z));
                   6066:   r = (Z *)MALLOC(n*sizeof(Z));
                   6067:   v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   6068:   gcdvz_estimate(&v,&d0);
                   6069:   for ( i = 0; i < n; i++ ) {
                   6070:     divqrz(c[i],d0,&q[i],&r[i]);
                   6071:   }
                   6072:   for ( i = 0; i < n; i++ ) if ( r[i] ) break;
                   6073:   if ( i < n ) {
                   6074:     v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   6075:     gcdvz(&v,&d1);
                   6076:     gcdz(d0,d1,&gcd);
1.6       noro     6077:     /* exact division */
                   6078:     divsz(d0,gcd,&a);
1.1       noro     6079:     for ( i = 0; i < n; i++ ) {
                   6080:       mulz(a,q[i],&u);
                   6081:       if ( r[i] ) {
1.6       noro     6082:         /* exact division */
                   6083:         divsz(r[i],gcd,&u1);
1.1       noro     6084:         addz(u,u1,&q[i]);
                   6085:       } else
                   6086:         q[i] = u;
                   6087:     }
                   6088:   }
                   6089:   for ( i = 0; i < n; i++ ) c[i] = q[i];
                   6090: }
                   6091:
1.4       noro     6092: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
                   6093:
                   6094: void mpz_removecont_array(mpz_t *c,int n)
                   6095: {
                   6096:   mpz_t d0,a,u,u1,gcd;
                   6097:   int i,j;
1.13      noro     6098:   static mpz_t *q,*r;
                   6099:   static int c_len = 0;
1.4       noro     6100:
                   6101:   for ( i = 0; i < n; i++ )
                   6102:     if ( mpz_sgn(c[i]) ) break;
                   6103:   if ( i == n ) return;
                   6104:   gcdv_mpz_estimate(d0,c,n);
1.13      noro     6105:   if ( n > c_len ) {
                   6106:     q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   6107:     r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   6108:     c_len = n;
                   6109:   }
1.4       noro     6110:   for ( i = 0; i < n; i++ ) {
                   6111:     mpz_init(q[i]); mpz_init(r[i]);
                   6112:     mpz_fdiv_qr(q[i],r[i],c[i],d0);
                   6113:   }
                   6114:   for ( i = 0; i < n; i++ )
                   6115:     if ( mpz_sgn(r[i]) ) break;
                   6116:   mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
                   6117:   if ( i < n ) {
                   6118:     mpz_gcd(gcd,d0,r[i]);
                   6119:     for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
                   6120:     mpz_div(a,d0,gcd);
                   6121:     for ( i = 0; i < n; i++ ) {
                   6122:       mpz_mul(u,a,q[i]);
                   6123:       if ( mpz_sgn(r[i]) ) {
                   6124:         mpz_div(u1,r[i],gcd);
                   6125:         mpz_add(q[i],u,u1);
                   6126:       } else
                   6127:         mpz_set(q[i],u);
                   6128:     }
                   6129:   }
                   6130:   for ( i = 0; i < n; i++ )
                   6131:     mpz_set(c[i],q[i]);
                   6132: }
                   6133:
1.1       noro     6134: void nd_mul_c(int mod,ND p,int mul)
                   6135: {
                   6136:     NM m;
                   6137:     int c,c1;
                   6138:
                   6139:     if ( !p ) return;
                   6140:     if ( mul == 1 ) return;
                   6141:     if ( mod == -1 )
                   6142:         for ( m = BDY(p); m; m = NEXT(m) )
                   6143:             CM(m) = _mulsf(CM(m),mul);
                   6144:     else
                   6145:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   6146:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   6147:         }
                   6148: }
                   6149:
                   6150: void nd_mul_c_lf(ND p,Z mul)
                   6151: {
                   6152:     NM m;
                   6153:     Z c;
                   6154:
                   6155:     if ( !p ) return;
                   6156:     if ( UNIZ(mul) ) return;
                   6157:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   6158:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   6159:     }
                   6160: }
                   6161:
                   6162: void nd_mul_c_q(ND p,P mul)
                   6163: {
                   6164:     NM m;
                   6165:     P c;
                   6166:
                   6167:     if ( !p ) return;
                   6168:     if ( UNIQ(mul) ) return;
                   6169:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   6170:         mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
                   6171:     }
                   6172: }
                   6173:
                   6174: void nd_mul_c_p(VL vl,ND p,P mul)
                   6175: {
                   6176:     NM m;
                   6177:     P c;
                   6178:
                   6179:     if ( !p ) return;
                   6180:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   6181:         mulp(vl,CP(m),mul,&c); CP(m) = c;
                   6182:     }
                   6183: }
                   6184:
                   6185: void nd_free(ND p)
                   6186: {
                   6187:     NM t,s;
                   6188:
                   6189:     if ( !p ) return;
                   6190:     t = BDY(p);
                   6191:     while ( t ) {
                   6192:         s = NEXT(t);
                   6193:         FREENM(t);
                   6194:         t = s;
                   6195:     }
                   6196:     FREEND(p);
                   6197: }
                   6198:
                   6199: void ndv_free(NDV p)
                   6200: {
                   6201:     GCFREE(BDY(p));
                   6202: }
                   6203:
                   6204: void nd_append_red(UINT *d,int i)
                   6205: {
                   6206:     RHist m,m0;
                   6207:     int h;
                   6208:
                   6209:     NEWRHist(m);
                   6210:     h = ndl_hash_value(d);
                   6211:     m->index = i;
                   6212:     ndl_copy(d,DL(m));
                   6213:     NEXT(m) = nd_red[h];
                   6214:     nd_red[h] = m;
                   6215: }
                   6216:
                   6217: UINT *ndv_compute_bound(NDV p)
                   6218: {
                   6219:     UINT *d1,*d2,*t;
                   6220:     UINT u;
                   6221:     int i,j,k,l,len,ind;
                   6222:     NMV m;
                   6223:
                   6224:     if ( !p )
                   6225:         return 0;
                   6226:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6227:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6228:     len = LEN(p);
                   6229:     m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   6230:     for ( i = 1; i < len; i++, NMV_ADV(m) ) {
                   6231:         ndl_max(DL(m),d1,d2);
                   6232:         t = d1; d1 = d2; d2 = t;
                   6233:     }
                   6234:     l = nd_nvar+31;
                   6235:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   6236:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   6237:         u = d1[i];
                   6238:         k = (nd_epw-1)*nd_bpe;
                   6239:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   6240:             t[ind] = (u>>k)&nd_mask0;
                   6241:     }
                   6242:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   6243:     return t;
                   6244: }
                   6245:
                   6246: UINT *nd_compute_bound(ND p)
                   6247: {
                   6248:     UINT *d1,*d2,*t;
                   6249:     UINT u;
                   6250:     int i,j,k,l,len,ind;
                   6251:     NM m;
                   6252:
                   6253:     if ( !p )
                   6254:         return 0;
                   6255:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6256:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6257:     len = LEN(p);
                   6258:     m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   6259:     for ( m = NEXT(m); m; m = NEXT(m) ) {
                   6260:         ndl_lcm(DL(m),d1,d2);
                   6261:         t = d1; d1 = d2; d2 = t;
                   6262:     }
                   6263:     l = nd_nvar+31;
                   6264:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   6265:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   6266:         u = d1[i];
                   6267:         k = (nd_epw-1)*nd_bpe;
                   6268:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   6269:             t[ind] = (u>>k)&nd_mask0;
                   6270:     }
                   6271:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   6272:     return t;
                   6273: }
                   6274:
                   6275: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
                   6276: /* of a term. In this case we need additional 1 word. */
                   6277:
                   6278: int nd_get_exporigin(struct order_spec *ord)
                   6279: {
                   6280:     switch ( ord->id ) {
1.21      noro     6281:         case 0: case 2: case 256: case 258: case 300:
1.1       noro     6282:             return 1+nd_module;
                   6283:         case 1: case 257:
                   6284:             /* block order */
                   6285:             /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
                   6286:             /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
                   6287:             return ord->ord.block.length+1+nd_module;
                   6288:         case 3: case 259:
                   6289: #if 0
                   6290:             error("nd_get_exporigin : composite order is not supported yet.");
                   6291: #else
                   6292:             return 1+nd_module;
                   6293: #endif
                   6294:         default:
                   6295:             error("nd_get_exporigin : ivalid argument.");
                   6296:             return 0;
                   6297:     }
                   6298: }
                   6299:
                   6300: void nd_setup_parameters(int nvar,int max) {
                   6301:     int i,j,n,elen,ord_o,ord_l,l,s,wpd;
                   6302:     struct order_pair *op;
1.45      noro     6303:     extern int CNVars;
1.1       noro     6304:
                   6305:     nd_nvar = nvar;
1.45      noro     6306:     CNVars = nvar;
1.1       noro     6307:     if ( max ) {
                   6308:         /* XXX */
                   6309:         if ( do_weyl ) nd_bpe = 32;
                   6310:         else if ( max < 2 ) nd_bpe = 1;
                   6311:         else if ( max < 4 ) nd_bpe = 2;
                   6312:         else if ( max < 8 ) nd_bpe = 3;
                   6313:         else if ( max < 16 ) nd_bpe = 4;
                   6314:         else if ( max < 32 ) nd_bpe = 5;
                   6315:         else if ( max < 64 ) nd_bpe = 6;
                   6316:         else if ( max < 256 ) nd_bpe = 8;
                   6317:         else if ( max < 1024 ) nd_bpe = 10;
                   6318:         else if ( max < 65536 ) nd_bpe = 16;
                   6319:         else nd_bpe = 32;
                   6320:     }
                   6321:     if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
                   6322:         UINT t;
                   6323:     int st;
                   6324:         int *v;
                   6325:   /* t = max(weights) */
                   6326:         t = 0;
                   6327:         if ( current_dl_weight_vector )
                   6328:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                   6329:                 if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
                   6330:                 if ( t < st ) t = st;
                   6331:             }
                   6332:         if ( nd_matrix )
                   6333:             for ( i = 0; i < nd_matrix_len; i++ )
                   6334:                 for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
                   6335:                     if ( (st=v[j]) < 0 ) st = -st;
                   6336:                     if ( t < st ) t = st;
                   6337:         }
                   6338:         /* i = bitsize of t */
                   6339:         for ( i = 0; t; t >>=1, i++ );
                   6340:         /* i += bitsize of nd_nvar */
                   6341:         for ( t = nd_nvar; t; t >>=1, i++);
                   6342:         /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
                   6343:         if ( (nd_bpe+i) >= 31 )
                   6344:             error("nd_setup_parameters : too large weight");
                   6345:     }
                   6346:     nd_epw = (sizeof(UINT)*8)/nd_bpe;
                   6347:     elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   6348:     nd_exporigin = nd_get_exporigin(nd_ord);
                   6349:     wpd = nd_exporigin+elen;
                   6350:     if ( nd_module )
                   6351:         nd_mpos = nd_exporigin-1;
                   6352:     else
                   6353:         nd_mpos = -1;
                   6354:     if ( wpd != nd_wpd ) {
                   6355:         nd_free_private_storage();
                   6356:         nd_wpd = wpd;
                   6357:     }
                   6358:     if ( nd_bpe < 32 ) {
                   6359:         nd_mask0 = (1<<nd_bpe)-1;
                   6360:     } else {
                   6361:         nd_mask0 = 0xffffffff;
                   6362:     }
                   6363:     bzero(nd_mask,sizeof(nd_mask));
                   6364:     nd_mask1 = 0;
                   6365:     for ( i = 0; i < nd_epw; i++ ) {
                   6366:         nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   6367:         nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   6368:     }
                   6369:     nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
                   6370:     nd_epos = nd_create_epos(nd_ord);
                   6371:     nd_blockmask = nd_create_blockmask(nd_ord);
                   6372:     nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
                   6373: }
                   6374:
                   6375: ND_pairs nd_reconstruct(int trace,ND_pairs d)
                   6376: {
                   6377:     int i,obpe,oadv,h;
                   6378:     static NM prev_nm_free_list;
                   6379:     static ND_pairs prev_ndp_free_list;
                   6380:     RHist mr0,mr;
                   6381:     RHist r;
                   6382:     RHist *old_red;
                   6383:     ND_pairs s0,s,t;
                   6384:     EPOS oepos;
                   6385:
                   6386:     obpe = nd_bpe;
                   6387:     oadv = nmv_adv;
                   6388:     oepos = nd_epos;
                   6389:     if ( obpe < 2 ) nd_bpe = 2;
                   6390:     else if ( obpe < 3 ) nd_bpe = 3;
                   6391:     else if ( obpe < 4 ) nd_bpe = 4;
                   6392:     else if ( obpe < 5 ) nd_bpe = 5;
                   6393:     else if ( obpe < 6 ) nd_bpe = 6;
                   6394:     else if ( obpe < 8 ) nd_bpe = 8;
                   6395:     else if ( obpe < 10 ) nd_bpe = 10;
                   6396:     else if ( obpe < 16 ) nd_bpe = 16;
                   6397:     else if ( obpe < 32 ) nd_bpe = 32;
                   6398:     else error("nd_reconstruct : exponent too large");
                   6399:
                   6400:     nd_setup_parameters(nd_nvar,0);
                   6401:     prev_nm_free_list = _nm_free_list;
                   6402:     prev_ndp_free_list = _ndp_free_list;
                   6403:     _nm_free_list = 0;
                   6404:     _ndp_free_list = 0;
                   6405:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   6406:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   6407:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   6408:     }
                   6409:     if ( trace )
                   6410:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   6411:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   6412:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   6413:         }
                   6414:     s0 = 0;
                   6415:     for ( t = d; t; t = NEXT(t) ) {
                   6416:         NEXTND_pairs(s0,s);
                   6417:         s->i1 = t->i1;
                   6418:         s->i2 = t->i2;
1.24      noro     6419:         s->sig = t->sig;
1.1       noro     6420:         SG(s) = SG(t);
                   6421:         ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   6422:     }
                   6423:
                   6424:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   6425:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   6426:         old_red[i] = nd_red[i];
                   6427:         nd_red[i] = 0;
                   6428:     }
                   6429:     for ( i = 0; i < REDTAB_LEN; i++ )
                   6430:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   6431:             NEWRHist(mr);
                   6432:             mr->index = r->index;
                   6433:             SG(mr) = SG(r);
                   6434:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   6435:             h = ndl_hash_value(DL(mr));
                   6436:             NEXT(mr) = nd_red[h];
                   6437:             nd_red[h] = mr;
1.24      noro     6438:             mr->sig = r->sig;
1.1       noro     6439:         }
                   6440:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   6441:     old_red = 0;
                   6442:     for ( i = 0; i < nd_psn; i++ ) {
                   6443:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   6444:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24      noro     6445:         r->sig = nd_psh[i]->sig;
1.1       noro     6446:         nd_psh[i] = r;
                   6447:     }
                   6448:     if ( s0 ) NEXT(s) = 0;
                   6449:     prev_nm_free_list = 0;
                   6450:     prev_ndp_free_list = 0;
                   6451: #if 0
                   6452:     GC_gcollect();
                   6453: #endif
                   6454:     return s0;
                   6455: }
                   6456:
1.30      noro     6457: void nd_reconstruct_s(int trace,ND_pairs *d)
                   6458: {
                   6459:     int i,obpe,oadv,h;
                   6460:     static NM prev_nm_free_list;
                   6461:     static ND_pairs prev_ndp_free_list;
                   6462:     RHist mr0,mr;
                   6463:     RHist r;
                   6464:     RHist *old_red;
                   6465:     ND_pairs s0,s,t;
                   6466:     EPOS oepos;
                   6467:
                   6468:     obpe = nd_bpe;
                   6469:     oadv = nmv_adv;
                   6470:     oepos = nd_epos;
                   6471:     if ( obpe < 2 ) nd_bpe = 2;
                   6472:     else if ( obpe < 3 ) nd_bpe = 3;
                   6473:     else if ( obpe < 4 ) nd_bpe = 4;
                   6474:     else if ( obpe < 5 ) nd_bpe = 5;
                   6475:     else if ( obpe < 6 ) nd_bpe = 6;
                   6476:     else if ( obpe < 8 ) nd_bpe = 8;
                   6477:     else if ( obpe < 10 ) nd_bpe = 10;
                   6478:     else if ( obpe < 16 ) nd_bpe = 16;
                   6479:     else if ( obpe < 32 ) nd_bpe = 32;
                   6480:     else error("nd_reconstruct_s : exponent too large");
                   6481:
                   6482:     nd_setup_parameters(nd_nvar,0);
                   6483:     prev_nm_free_list = _nm_free_list;
                   6484:     prev_ndp_free_list = _ndp_free_list;
                   6485:     _nm_free_list = 0;
                   6486:     _ndp_free_list = 0;
                   6487:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   6488:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   6489:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   6490:     }
                   6491:     if ( trace )
                   6492:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   6493:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   6494:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   6495:         }
                   6496:
                   6497:     for ( i = 0; i < nd_nbase; i++ ) {
                   6498:       s0 = 0;
                   6499:       for ( t = d[i]; t; t = NEXT(t) ) {
                   6500:           NEXTND_pairs(s0,s);
                   6501:           s->i1 = t->i1;
                   6502:           s->i2 = t->i2;
                   6503:           s->sig = t->sig;
                   6504:           SG(s) = SG(t);
                   6505:           ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   6506:       }
1.47      noro     6507:       if ( s0 ) NEXT(s) = 0;
1.30      noro     6508:       d[i] = s0;
                   6509:     }
                   6510:
                   6511:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   6512:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   6513:         old_red[i] = nd_red[i];
                   6514:         nd_red[i] = 0;
                   6515:     }
                   6516:     for ( i = 0; i < REDTAB_LEN; i++ )
                   6517:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   6518:             NEWRHist(mr);
                   6519:             mr->index = r->index;
                   6520:             SG(mr) = SG(r);
                   6521:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   6522:             h = ndl_hash_value(DL(mr));
                   6523:             NEXT(mr) = nd_red[h];
                   6524:             nd_red[h] = mr;
                   6525:             mr->sig = r->sig;
                   6526:         }
                   6527:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   6528:     old_red = 0;
                   6529:     for ( i = 0; i < nd_psn; i++ ) {
                   6530:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   6531:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
                   6532:         r->sig = nd_psh[i]->sig;
                   6533:         nd_psh[i] = r;
                   6534:     }
                   6535:     if ( s0 ) NEXT(s) = 0;
                   6536:     prev_nm_free_list = 0;
                   6537:     prev_ndp_free_list = 0;
                   6538: #if 0
                   6539:     GC_gcollect();
                   6540: #endif
                   6541: }
                   6542:
1.1       noro     6543: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
                   6544: {
                   6545:     int n,i,ei,oepw,omask0,j,s,ord_l,l;
                   6546:     struct order_pair *op;
                   6547:
                   6548:     n = nd_nvar;
                   6549:     oepw = (sizeof(UINT)*8)/obpe;
                   6550:     omask0 = (1<<obpe)-1;
                   6551:     TD(r) = TD(d);
                   6552:     for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
                   6553:     if ( nd_blockmask ) {
                   6554:         l = nd_blockmask->n;
                   6555:         op = nd_blockmask->order_pair;
                   6556:         for ( i = 1; i < nd_exporigin; i++ )
                   6557:             r[i] = d[i];
                   6558:         for ( j = 0, s = 0; j < l; j++ ) {
                   6559:             ord_l = op[j].length;
                   6560:             for ( i = 0; i < ord_l; i++, s++ ) {
                   6561:                 ei =  GET_EXP_OLD(d,s);
                   6562:                 PUT_EXP(r,s,ei);
                   6563:             }
                   6564:         }
                   6565:     } else {
                   6566:         for ( i = 0; i < n; i++ ) {
                   6567:             ei = GET_EXP_OLD(d,i);
                   6568:             PUT_EXP(r,i,ei);
                   6569:         }
                   6570:     }
                   6571:     if ( nd_module ) MPOS(r) = MPOS(d);
                   6572: }
                   6573:
                   6574: ND nd_copy(ND p)
                   6575: {
                   6576:     NM m,mr,mr0;
                   6577:     int c,n;
                   6578:     ND r;
                   6579:
                   6580:     if ( !p )
                   6581:         return 0;
                   6582:     else {
                   6583:         for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   6584:             NEXTNM(mr0,mr);
                   6585:             CM(mr) = CM(m);
                   6586:             ndl_copy(DL(m),DL(mr));
                   6587:         }
                   6588:         NEXT(mr) = 0;
                   6589:         MKND(NV(p),mr0,LEN(p),r);
                   6590:         SG(r) = SG(p);
                   6591:         return r;
                   6592:     }
                   6593: }
                   6594:
                   6595: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
                   6596: {
                   6597:     NM m1,m2;
                   6598:     NDV p1,p2;
                   6599:     ND t1,t2;
                   6600:     UINT *lcm;
                   6601:     P gp,tp;
                   6602:     Z g,t;
                   6603:     Z iq;
                   6604:     int td;
                   6605:     LIST hist;
                   6606:     NODE node;
                   6607:     DP d;
                   6608:
                   6609:     if ( !mod && nd_demand ) {
                   6610:         p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
                   6611:     } else {
                   6612:         if ( trace ) {
                   6613:             p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   6614:         } else {
                   6615:             p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   6616:         }
                   6617:     }
                   6618:     lcm = LCM(p);
                   6619:     NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
                   6620:     if ( ndl_check_bound2(p->i1,DL(m1)) ) {
                   6621:         FREENM(m1); return 0;
                   6622:     }
                   6623:     NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
                   6624:     if ( ndl_check_bound2(p->i2,DL(m2)) ) {
                   6625:         FREENM(m1); FREENM(m2); return 0;
                   6626:     }
                   6627:
                   6628:     if ( mod == -1 ) {
                   6629:         CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
                   6630:     } else if ( mod > 0 ) {
                   6631:         CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
                   6632:     } else if ( mod == -2 ) {
                   6633:         CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
                   6634:     } else if ( nd_vc ) {
                   6635:         ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
                   6636:         divsp(nd_vc,HCP(p2),gp,&CP(m1));
                   6637:         divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
                   6638:     } else {
1.6       noro     6639:         igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1       noro     6640:     }
                   6641:     t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
                   6642:     *rp = nd_add(mod,t1,t2);
                   6643:     if ( nd_gentrace ) {
                   6644:         /* nd_tracelist is initialized */
1.6       noro     6645:         STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     6646:         MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6       noro     6647:         STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     6648:         MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
                   6649:         nd_tracelist = node;
                   6650:     }
1.24      noro     6651:     if ( *rp )
                   6652:       (*rp)->sig = p->sig;
1.1       noro     6653:     FREENM(m1); FREENM(m2);
                   6654:     return 1;
                   6655: }
                   6656:
                   6657: void ndv_mul_c(int mod,NDV p,int mul)
                   6658: {
                   6659:     NMV m;
                   6660:     int c,c1,len,i;
                   6661:
                   6662:     if ( !p ) return;
                   6663:     len = LEN(p);
                   6664:     if ( mod == -1 )
                   6665:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   6666:             CM(m) = _mulsf(CM(m),mul);
                   6667:     else
                   6668:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6669:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   6670:         }
                   6671: }
                   6672:
                   6673: void ndv_mul_c_lf(NDV p,Z mul)
                   6674: {
                   6675:     NMV m;
                   6676:     Z c;
                   6677:     int len,i;
                   6678:
                   6679:     if ( !p ) return;
                   6680:     len = LEN(p);
                   6681:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6682:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   6683:     }
                   6684: }
                   6685:
                   6686: /* for nd_det */
                   6687: void ndv_mul_c_q(NDV p,Z mul)
                   6688: {
                   6689:     NMV m;
                   6690:     Z c;
                   6691:     int len,i;
                   6692:
                   6693:     if ( !p ) return;
                   6694:     len = LEN(p);
                   6695:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     6696:         mulz(CZ(m),mul,&c); CZ(m) = c;
1.1       noro     6697:     }
                   6698: }
                   6699:
                   6700: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
                   6701:     int n2,i,j,l,n,tlen;
                   6702:     UINT *d0;
                   6703:     NM *tab,*psum;
                   6704:     ND s,r;
                   6705:     NM t;
                   6706:     NMV m1;
                   6707:
                   6708:     if ( !p ) return 0;
                   6709:     n = NV(p); n2 = n>>1;
                   6710:     d0 = DL(m0);
                   6711:     l = LEN(p);
                   6712:     for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   6713:     tab = (NM *)MALLOC(tlen*sizeof(NM));
                   6714:     psum = (NM *)MALLOC(tlen*sizeof(NM));
                   6715:     for ( i = 0; i < tlen; i++ ) psum[i] = 0;
                   6716:     m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   6717:     for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
                   6718:         /* m0(NM) * m1(NMV) => tab(NM) */
                   6719:         weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
                   6720:         for ( j = 0; j < tlen; j++ ) {
                   6721:             if ( tab[j] ) {
                   6722:                 NEXT(tab[j]) = psum[j];    psum[j] = tab[j];
                   6723:             }
                   6724:         }
                   6725:     }
                   6726:     for ( i = tlen-1, r = 0; i >= 0; i-- )
                   6727:         if ( psum[i] ) {
                   6728:             for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   6729:             MKND(n,psum[i],j,s);
                   6730:             r = nd_add(mod,r,s);
                   6731:         }
                   6732:     if ( r ) SG(r) = SG(p)+TD(d0);
                   6733:     return r;
                   6734: }
                   6735:
                   6736: /* product of monomials */
                   6737: /* XXX block order is not handled correctly */
                   6738:
                   6739: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   6740: {
                   6741:     int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
                   6742:     UINT *d0,*d1,*d,*dt,*ctab;
                   6743:     Z *ctab_q;
                   6744:     Z q,q1;
                   6745:     UINT c0,c1,c;
                   6746:     NM *p;
                   6747:     NM m,t;
                   6748:     int mpos;
                   6749:
                   6750:     for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   6751:     if ( !m0 || !m1 ) return;
                   6752:     d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
                   6753:     if ( nd_module )
                   6754:         if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
                   6755:
                   6756:     NEWNM(m); d = DL(m);
                   6757:     if ( mod ) {
                   6758:         c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
                   6759:     } else if ( nd_vc )
                   6760:         mulp(nd_vc,CP(m0),CP(m1),&CP(m));
                   6761:   else
1.6       noro     6762:         mulz(CZ(m0),CZ(m1),&CZ(m));
1.1       noro     6763:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   6764:     homo = n&1 ? 1 : 0;
                   6765:     if ( homo ) {
                   6766:         /* offset of h-degree */
                   6767:         h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   6768:         PUT_EXP(DL(m),n-1,h);
                   6769:         TD(DL(m)) = h;
                   6770:         if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   6771:     }
                   6772:     tab[0] = m;
                   6773:     NEWNM(m); d = DL(m);
                   6774:     for ( i = 0, curlen = 1; i < n2; i++ ) {
                   6775:         a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   6776:         k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   6777:         /* xi^a*(Di^k*xi^l)*Di^b */
                   6778:         a += l; b += k;
                   6779:         s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
                   6780:         if ( !k || !l ) {
                   6781:             for ( j = 0; j < curlen; j++ )
                   6782:                 if ( (t = tab[j]) != 0 ) {
                   6783:                     dt = DL(t);
                   6784:                     PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
                   6785:                     if ( nd_blockmask ) ndl_weight_mask(dt);
                   6786:                 }
                   6787:             curlen *= k+1;
                   6788:             continue;
                   6789:         }
                   6790:         min = MIN(k,l);
                   6791:         if ( mod ) {
                   6792:             ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
                   6793:             mkwcm(k,l,mod,(int *)ctab);
                   6794:         } else {
                   6795:             ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
                   6796:             mkwc(k,l,ctab_q);
                   6797:         }
                   6798:         for ( j = min; j >= 0; j-- ) {
                   6799:             for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
                   6800:             PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
                   6801:             h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
                   6802:             if ( homo ) {
                   6803:                 TD(d) = s;
                   6804:                 PUT_EXP(d,n-1,s-h);
                   6805:             } else TD(d) = h;
                   6806:             if ( nd_blockmask ) ndl_weight_mask(d);
                   6807:             if ( mod ) c = ctab[j];
                   6808:             else q = ctab_q[j];
                   6809:             p = tab+curlen*j;
                   6810:             if ( j == 0 ) {
                   6811:                 for ( u = 0; u < curlen; u++, p++ ) {
                   6812:                     if ( tab[u] ) {
                   6813:                         ndl_addto(DL(tab[u]),d);
                   6814:                         if ( mod ) {
                   6815:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
                   6816:                         } else if ( nd_vc )
                   6817:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
                   6818:             else {
1.6       noro     6819:                             mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1       noro     6820:                         }
                   6821:                     }
                   6822:                 }
                   6823:             } else {
                   6824:                 for ( u = 0; u < curlen; u++, p++ ) {
                   6825:                     if ( tab[u] ) {
                   6826:                         NEWNM(t);
                   6827:                         ndl_add(DL(tab[u]),d,DL(t));
                   6828:                         if ( mod ) {
                   6829:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
                   6830:                         } else if ( nd_vc )
                   6831:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
                   6832:             else
1.6       noro     6833:                             mulz(CZ(tab[u]),q,&CZ(t));
1.1       noro     6834:                         *p = t;
                   6835:                     }
                   6836:                 }
                   6837:             }
                   6838:         }
                   6839:         curlen *= k+1;
                   6840:     }
                   6841:     FREENM(m);
                   6842:     if ( nd_module ) {
                   6843:         mpos = MPOS(d1);
                   6844:         for ( i = 0; i < tlen; i++ )
                   6845:             if ( tab[i] ) {
                   6846:                 d = DL(tab[i]);
                   6847:                 MPOS(d) = mpos;
                   6848:                 TD(d) = ndl_weight(d);
                   6849:             }
                   6850:     }
                   6851: }
                   6852:
                   6853: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   6854: {
                   6855:     NM mr,mr0;
                   6856:     NMV m;
                   6857:     UINT *d,*dt,*dm;
                   6858:     int c,n,td,i,c1,c2,len;
                   6859:     Q q;
                   6860:     ND r;
                   6861:
                   6862:     if ( !p ) return 0;
                   6863:     else {
                   6864:         n = NV(p); m = BDY(p);
                   6865:         d = DL(m0);
                   6866:         len = LEN(p);
                   6867:         mr0 = 0;
                   6868:         td = TD(d);
                   6869:         c = CM(m0);
                   6870:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6871:             NEXTNM(mr0,mr);
                   6872:             CM(mr) = 1;
                   6873:             ndl_add(DL(m),d,DL(mr));
                   6874:         }
                   6875:         NEXT(mr) = 0;
                   6876:         MKND(NV(p),mr0,len,r);
                   6877:         SG(r) = SG(p) + TD(d);
                   6878:         return r;
                   6879:     }
                   6880: }
                   6881:
                   6882: ND ndv_mul_nm(int mod,NM m0,NDV p)
                   6883: {
                   6884:     NM mr,mr0;
                   6885:     NMV m;
                   6886:     UINT *d,*dt,*dm;
                   6887:     int c,n,td,i,c1,c2,len;
                   6888:     P q;
                   6889:     ND r;
                   6890:
                   6891:     if ( !p ) return 0;
                   6892:     else if ( do_weyl ) {
                   6893:         if ( mod < 0 ) {
                   6894:             error("ndv_mul_nm : not implemented (weyl)");
                   6895:             return 0;
                   6896:         } else
                   6897:             return weyl_ndv_mul_nm(mod,m0,p);
                   6898:     } else {
                   6899:         n = NV(p); m = BDY(p);
                   6900:         d = DL(m0);
                   6901:         len = LEN(p);
                   6902:         mr0 = 0;
                   6903:         td = TD(d);
                   6904:         if ( mod == -1 ) {
                   6905:             c = CM(m0);
                   6906:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6907:                 NEXTNM(mr0,mr);
                   6908:                 CM(mr) = _mulsf(CM(m),c);
                   6909:                 ndl_add(DL(m),d,DL(mr));
                   6910:             }
                   6911:         } else if ( mod == -2 ) {
                   6912:             Z cl;
                   6913:             cl = CZ(m0);
                   6914:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6915:                 NEXTNM(mr0,mr);
                   6916:                 mullf(CZ(m),cl,&CZ(mr));
                   6917:                 ndl_add(DL(m),d,DL(mr));
                   6918:             }
                   6919:         } else if ( mod ) {
                   6920:             c = CM(m0);
                   6921:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6922:                 NEXTNM(mr0,mr);
                   6923:                 c1 = CM(m);
                   6924:                 DMAR(c1,c,0,mod,c2);
                   6925:                 CM(mr) = c2;
                   6926:                 ndl_add(DL(m),d,DL(mr));
                   6927:             }
                   6928:         } else {
                   6929:             q = CP(m0);
                   6930:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6931:                 NEXTNM(mr0,mr);
                   6932:                 mulp(nd_vc,CP(m),q,&CP(mr));
                   6933:                 ndl_add(DL(m),d,DL(mr));
                   6934:             }
                   6935:         }
                   6936:         NEXT(mr) = 0;
                   6937:         MKND(NV(p),mr0,len,r);
                   6938:         SG(r) = SG(p) + TD(d);
                   6939:         return r;
                   6940:     }
                   6941: }
                   6942:
                   6943: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
                   6944: {
                   6945:     NM mq0,mq;
                   6946:     NMV tm;
                   6947:     Q q;
                   6948:     int i,nv,sg,c,c1,c2,hindex;
                   6949:     ND p,t,r;
                   6950:
                   6951:     if ( bucket->m < 0 ) return 0;
                   6952:     else {
                   6953:         nv = NV(d);
                   6954:         mq0 = 0;
                   6955:         tm = (NMV)MALLOC(nmv_adv);
                   6956:         while ( 1 ) {
                   6957:             if ( mod > 0 || mod == -1 )
                   6958:               hindex = head_pbucket(mod,bucket);
                   6959:             else if ( mod == -2 )
                   6960:               hindex = head_pbucket_lf(bucket);
                   6961:             else
                   6962:               hindex = head_pbucket_q(bucket);
                   6963:             if ( hindex < 0 ) break;
                   6964:             p = bucket->body[hindex];
                   6965:             NEXTNM(mq0,mq);
                   6966:             ndl_sub(HDL(p),HDL(d),DL(mq));
                   6967:             ndl_copy(DL(mq),DL(tm));
                   6968:             if ( mod ) {
                   6969:                 c1 = invm(HCM(d),mod); c2 = HCM(p);
                   6970:                 DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   6971:                 CM(tm) = mod-c;
                   6972:             } else {
1.6       noro     6973:                 divsz(HCZ(p),HCZ(d),&CZ(mq));
                   6974:                 chsgnz(CZ(mq),&CZ(tm));
1.1       noro     6975:             }
                   6976:             t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
                   6977:             bucket->body[hindex] = nd_remove_head(p);
                   6978:             t = nd_remove_head(t);
                   6979:             add_pbucket(mod,bucket,t);
                   6980:         }
                   6981:         if ( !mq0 )
                   6982:             r = 0;
                   6983:         else {
                   6984:             NEXT(mq) = 0;
                   6985:             for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   6986:             MKND(nv,mq0,i,r);
                   6987:             /* XXX */
                   6988:             SG(r) = HTD(r);
                   6989:         }
                   6990:         return r;
                   6991:     }
                   6992: }
                   6993:
                   6994: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   6995: {
                   6996:     NMV m,mr,mr0,t;
                   6997:     int len,i,k;
                   6998:
                   6999:     if ( !p ) return;
                   7000:     m = BDY(p); len = LEN(p);
                   7001:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   7002:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   7003:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   7004:     t = (NMV)MALLOC(nmv_adv);
                   7005:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6       noro     7006:         CZ(t) = CZ(m);
1.1       noro     7007:         for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   7008:         ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6       noro     7009:         CZ(mr) = CZ(t);
1.1       noro     7010:         ndl_copy(DL(t),DL(mr));
                   7011:     }
                   7012:     BDY(p) = mr0;
                   7013: }
                   7014:
                   7015: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   7016: {
                   7017:     NMV m,mr,mr0;
                   7018:     int len,i;
                   7019:     NDV r;
                   7020:
                   7021:     if ( !p ) return 0;
                   7022:     m = BDY(p); len = LEN(p);
                   7023:     mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   7024:     for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   7025:         ndl_zero(DL(mr));
                   7026:         ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6       noro     7027:         CZ(mr) = CZ(m);
1.1       noro     7028:     }
                   7029:     MKNDV(NV(p),mr0,len,r);
                   7030:     SG(r) = SG(p);
1.24      noro     7031:     r->sig = p->sig;
1.1       noro     7032:     return r;
                   7033: }
                   7034:
                   7035: /* duplicate p */
                   7036:
                   7037: NDV ndv_dup(int mod,NDV p)
                   7038: {
                   7039:     NDV d;
                   7040:     NMV t,m,m0;
                   7041:     int i,len;
                   7042:
                   7043:     if ( !p ) return 0;
                   7044:     len = LEN(p);
                   7045:     m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   7046:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   7047:         ndl_copy(DL(t),DL(m));
1.6       noro     7048:         CZ(m) = CZ(t);
1.1       noro     7049:     }
                   7050:     MKNDV(NV(p),m0,len,d);
                   7051:     SG(d) = SG(p);
                   7052:     return d;
                   7053: }
                   7054:
                   7055: NDV ndv_symbolic(int mod,NDV p)
                   7056: {
                   7057:     NDV d;
                   7058:     NMV t,m,m0;
                   7059:     int i,len;
                   7060:
                   7061:     if ( !p ) return 0;
                   7062:     len = LEN(p);
                   7063:     m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   7064:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   7065:         ndl_copy(DL(t),DL(m));
1.6       noro     7066:         CZ(m) = ONE;
1.1       noro     7067:     }
                   7068:     MKNDV(NV(p),m0,len,d);
                   7069:     SG(d) = SG(p);
                   7070:     return d;
                   7071: }
                   7072:
                   7073: ND nd_dup(ND p)
                   7074: {
                   7075:     ND d;
                   7076:     NM t,m,m0;
                   7077:
                   7078:     if ( !p ) return 0;
                   7079:     for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   7080:         NEXTNM(m0,m);
                   7081:         ndl_copy(DL(t),DL(m));
1.6       noro     7082:         CZ(m) = CZ(t);
1.1       noro     7083:     }
                   7084:     if ( m0 ) NEXT(m) = 0;
                   7085:     MKND(NV(p),m0,LEN(p),d);
                   7086:     SG(d) = SG(p);
                   7087:     return d;
                   7088: }
                   7089:
                   7090: /* XXX if p->len == 0 then it represents 0 */
                   7091:
                   7092: void ndv_mod(int mod,NDV p)
                   7093: {
                   7094:     NMV t,d;
                   7095:     int r,s,u;
                   7096:     int i,len,dlen;
                   7097:     P cp;
                   7098:     Z c;
                   7099:     Obj gfs;
                   7100:
                   7101:     if ( !p ) return;
                   7102:     len = LEN(p);
                   7103:     dlen = 0;
                   7104:     if ( mod == -1 )
                   7105:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   7106:             simp_ff((Obj)CP(t),&gfs);
                   7107:             if ( gfs ) {
                   7108:               r = FTOIF(CONT((GFS)gfs));
                   7109:               CM(d) = r;
                   7110:               ndl_copy(DL(t),DL(d));
                   7111:               NMV_ADV(d);
                   7112:               dlen++;
                   7113:             }
                   7114:         }
                   7115:     else if ( mod == -2 )
                   7116:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   7117:             simp_ff((Obj)CP(t),&gfs);
                   7118:             if ( gfs ) {
                   7119:               lmtolf((LM)gfs,&CZ(d));
                   7120:               ndl_copy(DL(t),DL(d));
                   7121:               NMV_ADV(d);
                   7122:               dlen++;
                   7123:             }
                   7124:         }
                   7125:     else
                   7126:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   7127:             if ( nd_vc ) {
                   7128:                 nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
                   7129:                 c = (Z)cp;
                   7130:             } else
1.6       noro     7131:                 c = CZ(t);
1.1       noro     7132:             r = remqi((Q)c,mod);
                   7133:             if ( r ) {
                   7134:                 CM(d) = r;
                   7135:                 ndl_copy(DL(t),DL(d));
                   7136:                 NMV_ADV(d);
                   7137:                 dlen++;
                   7138:             }
                   7139:         }
                   7140:     LEN(p) = dlen;
                   7141: }
                   7142:
                   7143: NDV ptondv(VL vl,VL dvl,P p)
                   7144: {
                   7145:     ND nd;
                   7146:
                   7147:     nd = ptond(vl,dvl,p);
                   7148:     return ndtondv(0,nd);
                   7149: }
                   7150:
                   7151: void pltozpl(LIST l,Q *cont,LIST *pp)
                   7152: {
1.16      noro     7153:   NODE nd,nd1;
                   7154:   int n;
                   7155:   P *pl;
                   7156:   Q *cl;
                   7157:   int i;
                   7158:   P dmy;
                   7159:   Z dvr,inv;
                   7160:   LIST r;
                   7161:
                   7162:   nd = BDY(l); n = length(nd);
                   7163:   pl = (P *)MALLOC(n*sizeof(P));
                   7164:   cl = (Q *)MALLOC(n*sizeof(Q));
                   7165:   for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
                   7166:     ptozp((P)BDY(nd),1,&cl[i],&dmy);
                   7167:   }
                   7168:   qltozl(cl,n,&dvr);
                   7169:   divz(ONE,dvr,&inv);
                   7170:   nd = BDY(l);
                   7171:   for ( i = 0; i < n; i++, nd = NEXT(nd) )
                   7172:     divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
                   7173:   nd = 0;
                   7174:   for ( i = n-1; i >= 0; i-- ) {
                   7175:     MKNODE(nd1,pl[i],nd); nd = nd1;
                   7176:   }
                   7177:   MKLIST(r,nd);
                   7178:   *pp = r;
1.1       noro     7179: }
                   7180:
                   7181: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
                   7182:
                   7183: NDV pltondv(VL vl,VL dvl,LIST p)
                   7184: {
                   7185:     int i;
                   7186:     NODE t;
                   7187:     ND r,ri;
                   7188:     NM m;
                   7189:
                   7190:     if ( !nd_module ) error("pltond : module order must be set");
                   7191:     r = 0;
                   7192:     for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
                   7193:         ri = ptond(vl,dvl,(P)BDY(t));
                   7194:         if ( ri )
                   7195:             for ( m = BDY(ri); m; m = NEXT(m) ) {
                   7196:                 MPOS(DL(m)) = i;
                   7197:                 TD(DL(m)) = ndl_weight(DL(m));
                   7198:                 if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   7199:             }
                   7200:         r = nd_add(0,r,ri);
                   7201:     }
                   7202:     return ndtondv(0,r);
                   7203: }
                   7204:
                   7205: ND ptond(VL vl,VL dvl,P p)
                   7206: {
                   7207:     int n,i,j,k,e;
                   7208:     VL tvl;
                   7209:     V v;
                   7210:     DCP dc;
                   7211:     DCP *w;
                   7212:     ND r,s,t,u;
                   7213:     P x;
                   7214:     int c;
                   7215:     UINT *d;
                   7216:     NM m,m0;
                   7217:
                   7218:     if ( !p )
                   7219:         return 0;
                   7220:     else if ( NUM(p) ) {
                   7221:         NEWNM(m);
                   7222:         ndl_zero(DL(m));
1.48      noro     7223:         if ( RATN(p) && !INT((Q)p) )
1.1       noro     7224:           error("ptond : input must be integer-coefficient");
1.6       noro     7225:         CZ(m) = (Z)p;
1.1       noro     7226:         NEXT(m) = 0;
                   7227:         MKND(nd_nvar,m,1,r);
                   7228:         SG(r) = 0;
                   7229:         return r;
                   7230:     } else {
                   7231:         for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   7232:         w = (DCP *)MALLOC(k*sizeof(DCP));
                   7233:         for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   7234:         for ( i = 0, tvl = dvl, v = VR(p);
                   7235:             tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   7236:         if ( !tvl ) {
                   7237:             for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   7238:                 t = ptond(vl,dvl,COEF(w[j]));
                   7239:                 pwrp(vl,x,DEG(w[j]),&p);
                   7240:                 nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   7241:             }
                   7242:             return s;
                   7243:         } else {
                   7244:             NEWNM(m0); d = DL(m0);
                   7245:             for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6       noro     7246:                 ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1       noro     7247:                 TD(d) = MUL_WEIGHT(e,i);
                   7248:                 if ( nd_blockmask) ndl_weight_mask(d);
                   7249:                 if ( nd_module ) MPOS(d) = 0;
                   7250:                 t = ptond(vl,dvl,COEF(w[j]));
                   7251:                 for ( m = BDY(t); m; m = NEXT(m) )
                   7252:                     ndl_addto(DL(m),d);
                   7253:                 SG(t) += TD(d);
                   7254:                 s = nd_add(0,s,t);
                   7255:             }
                   7256:             FREENM(m0);
                   7257:             return s;
                   7258:         }
                   7259:     }
                   7260: }
                   7261:
                   7262: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   7263: {
                   7264:     VL tvl;
                   7265:     int len,n,j,i,e;
                   7266:     NMV m;
                   7267:     Z q;
                   7268:     P c;
                   7269:     UINT *d;
                   7270:     P s,r,u,t,w;
                   7271:     GFS gfs;
                   7272:
                   7273:     if ( !p ) return 0;
                   7274:     else {
                   7275:         len = LEN(p);
                   7276:         n = NV(p);
                   7277:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   7278:         for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   7279:             if ( mod == -1 ) {
                   7280:                e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   7281:             } else if ( mod == -2 ) {
                   7282:                c = (P)CZ(m);
                   7283:             } else if ( mod > 0 ) {
1.6       noro     7284:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     7285:             } else
                   7286:                 c = CP(m);
                   7287:             d = DL(m);
                   7288:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     7289:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     7290:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   7291:             }
                   7292:             addp(vl,s,t,&u); s = u;
                   7293:         }
                   7294:         return s;
                   7295:     }
                   7296: }
                   7297:
                   7298: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
                   7299: {
                   7300:     VL tvl;
                   7301:     int len,n,j,i,e;
                   7302:     NMV m;
                   7303:     Z q;
                   7304:     P c;
                   7305:     UINT *d;
                   7306:     P s,r,u,t,w;
                   7307:     GFS gfs;
                   7308:     P *a;
                   7309:     LIST l;
                   7310:     NODE nd,nd1;
                   7311:
                   7312:     if ( !p ) return 0;
                   7313:     else {
                   7314:         a = (P *)MALLOC((rank+1)*sizeof(P));
                   7315:         for ( i = 0; i <= rank; i++ ) a[i] = 0;
                   7316:         len = LEN(p);
                   7317:         n = NV(p);
                   7318:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   7319:         for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
                   7320:             if ( mod == -1 ) {
                   7321:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   7322:             } else if ( mod ) {
1.6       noro     7323:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     7324:             } else
                   7325:                 c = CP(m);
                   7326:             d = DL(m);
                   7327:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     7328:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     7329:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   7330:             }
                   7331:             addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
                   7332:         }
                   7333:         nd = 0;
                   7334:         for ( i = rank; i > 0; i-- ) {
                   7335:             MKNODE(nd1,a[i],nd); nd = nd1;
                   7336:         }
                   7337:         MKLIST(l,nd);
                   7338:         return l;
                   7339:     }
                   7340: }
                   7341:
                   7342: NDV ndtondv(int mod,ND p)
                   7343: {
                   7344:     NDV d;
                   7345:     NMV m,m0;
                   7346:     NM t;
                   7347:     int i,len;
                   7348:
                   7349:     if ( !p ) return 0;
                   7350:     len = LEN(p);
                   7351:     if ( mod > 0 || mod == -1 )
                   7352:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   7353:     else
                   7354:         m0 = m = MALLOC(len*nmv_adv);
                   7355: #if 0
                   7356:     ndv_alloc += nmv_adv*len;
                   7357: #endif
                   7358:     for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   7359:         ndl_copy(DL(t),DL(m));
1.6       noro     7360:         CZ(m) = CZ(t);
1.1       noro     7361:     }
                   7362:     MKNDV(NV(p),m0,len,d);
                   7363:     SG(d) = SG(p);
1.24      noro     7364:     d->sig = p->sig;
1.1       noro     7365:     return d;
                   7366: }
                   7367:
1.16      noro     7368: static int dmm_comp_nv;
                   7369:
                   7370: int dmm_comp(DMM *a,DMM *b)
                   7371: {
                   7372:    return -compdmm(dmm_comp_nv,*a,*b);
                   7373: }
                   7374:
                   7375: void dmm_sort_by_ord(DMM *a,int len,int nv)
                   7376: {
                   7377:   dmm_comp_nv = nv;
                   7378:   qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
                   7379: }
                   7380:
                   7381: void dpm_sort(DPM p,DPM *rp)
                   7382: {
                   7383:   DMM t,t1;
                   7384:   int len,i,n;
                   7385:   DMM *a;
                   7386:   DPM d;
                   7387:
                   7388:   if ( !p ) *rp = 0;
                   7389:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   7390:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   7391:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   7392:   n = p->nv;
                   7393:   dmm_sort_by_ord(a,len,n);
                   7394:   t = 0;
                   7395:   for ( i = len-1; i >= 0; i-- ) {
                   7396:     NEWDMM(t1);
                   7397:     t1->c = a[i]->c;
                   7398:     t1->dl = a[i]->dl;
                   7399:     t1->pos = a[i]->pos;
                   7400:     t1->next = t;
                   7401:     t = t1;
                   7402:   }
                   7403:   MKDPM(n,t,d);
                   7404:   SG(d) = SG(p);
                   7405:   *rp = d;
                   7406: }
                   7407:
1.18      noro     7408: int dpm_comp(DPM *a,DPM *b)
                   7409: {
1.22      noro     7410:   return -compdpm(CO,*a,*b);
1.18      noro     7411: }
                   7412:
                   7413: NODE dpm_sort_list(NODE l)
                   7414: {
                   7415:   int i,len;
                   7416:   NODE t,t1;
                   7417:   DPM *a;
                   7418:
                   7419:   len = length(l);
                   7420:   a = (DPM *)MALLOC(len*sizeof(DPM));
                   7421:   for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
                   7422:   qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
                   7423:   t = 0;
                   7424:   for ( i = len-1; i >= 0; i-- ) {
                   7425:     MKNODE(t1,(pointer)a[i],t); t = t1;
                   7426:   }
                   7427:   return t;
                   7428: }
                   7429:
1.20      noro     7430: int nmv_comp(NMV a,NMV b)
                   7431: {
1.21      noro     7432:   int t;
                   7433:   t = DL_COMPARE(a->dl,b->dl);
                   7434:   return -t;
1.20      noro     7435: }
                   7436:
1.43      noro     7437: NDV dptondv(int mod,DP p)
                   7438: {
                   7439:   NDV d;
                   7440:   NMV m,m0;
                   7441:   MP t;
                   7442:   MP *a;
                   7443:   int i,len,n;
                   7444:
                   7445:   if ( !p ) return 0;
                   7446:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   7447:   n = p->nv;
                   7448:   if ( mod > 0 || mod == -1 )
                   7449:     m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   7450:   else
                   7451:     m0 = m = MALLOC(len*nmv_adv);
                   7452:   for ( i = 0, t = BDY(p); i < len; i++, NMV_ADV(m), t = NEXT(t) ) {
                   7453:     dltondl(n,t->dl,DL(m));
                   7454:     TD(DL(m)) = ndl_weight(DL(m));
                   7455:     CZ(m) = (Z)t->c;
                   7456:   }
                   7457:   qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
                   7458:   MKNDV(NV(p),m0,len,d);
                   7459:   SG(d) = SG(p);
                   7460:   return d;
                   7461: }
                   7462:
1.16      noro     7463: NDV dpmtondv(int mod,DPM p)
                   7464: {
                   7465:   NDV d;
                   7466:   NMV m,m0;
                   7467:   DMM t;
                   7468:   DMM *a;
                   7469:   int i,len,n;
                   7470:
                   7471:   if ( !p ) return 0;
                   7472:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   7473:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   7474:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   7475:   n = p->nv;
                   7476:   dmm_sort_by_ord(a,len,n);
                   7477:   if ( mod > 0 || mod == -1 )
                   7478:     m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   7479:   else
                   7480:     m0 = m = MALLOC(len*nmv_adv);
                   7481: #if 0
                   7482:   ndv_alloc += nmv_adv*len;
                   7483: #endif
                   7484:   for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   7485:     dltondl(n,a[i]->dl,DL(m));
                   7486:     MPOS(DL(m)) = a[i]->pos;
1.20      noro     7487:     TD(DL(m)) = ndl_weight(DL(m));
1.16      noro     7488:     CZ(m) = (Z)a[i]->c;
                   7489:   }
1.20      noro     7490:   qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16      noro     7491:   MKNDV(NV(p),m0,len,d);
                   7492:   SG(d) = SG(p);
                   7493:   return d;
                   7494: }
                   7495:
1.1       noro     7496: ND ndvtond(int mod,NDV p)
                   7497: {
                   7498:     ND d;
                   7499:     NM m,m0;
                   7500:     NMV t;
                   7501:     int i,len;
                   7502:
                   7503:     if ( !p ) return 0;
                   7504:     m0 = 0;
                   7505:     len = p->len;
                   7506:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   7507:         NEXTNM(m0,m);
                   7508:         ndl_copy(DL(t),DL(m));
1.6       noro     7509:         CZ(m) = CZ(t);
1.1       noro     7510:     }
                   7511:     NEXT(m) = 0;
                   7512:     MKND(NV(p),m0,len,d);
                   7513:     SG(d) = SG(p);
1.28      noro     7514:     d->sig = p->sig;
1.1       noro     7515:     return d;
                   7516: }
                   7517:
                   7518: DP ndvtodp(int mod,NDV p)
                   7519: {
                   7520:     MP m,m0;
                   7521:   DP d;
                   7522:     NMV t;
                   7523:     int i,len;
                   7524:
                   7525:     if ( !p ) return 0;
                   7526:     m0 = 0;
                   7527:     len = p->len;
                   7528:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   7529:         NEXTMP(m0,m);
                   7530:       m->dl = ndltodl(nd_nvar,DL(t));
                   7531:       m->c = (Obj)ndctop(mod,t->c);
                   7532:     }
                   7533:     NEXT(m) = 0;
                   7534:   MKDP(nd_nvar,m0,d);
                   7535:     SG(d) = SG(p);
                   7536:     return d;
                   7537: }
                   7538:
1.44      noro     7539: DPM sigtodpm(SIG s)
                   7540: {
                   7541:   DMM m;
                   7542:   DPM d;
                   7543:
                   7544:   NEWDMM(m);
                   7545:   m->c = (Obj)ONE;
                   7546:   m->dl = s->dl;
                   7547:   m->pos = s->pos+1;
                   7548:   m->next = 0;
                   7549:   MKDPM(nd_nvar,m,d);
                   7550:   SG(d) = s->dl->td;
                   7551:   return d;
                   7552: }
                   7553:
1.16      noro     7554: DPM ndvtodpm(int mod,NDV p)
                   7555: {
                   7556:   DMM m,m0;
                   7557:   DPM d;
                   7558:   NMV t;
                   7559:   int i,len;
                   7560:
                   7561:   if ( !p ) return 0;
                   7562:   m0 = 0;
                   7563:   len = p->len;
                   7564:   for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   7565:     NEXTDMM(m0,m);
                   7566:     m->dl = ndltodl(nd_nvar,DL(t));
                   7567:     m->c = (Obj)ndctop(mod,t->c);
                   7568:     m->pos = MPOS(DL(t));
                   7569:   }
                   7570:   NEXT(m) = 0;
                   7571:   MKDPM(nd_nvar,m0,d);
                   7572:   SG(d) = SG(p);
                   7573:   return d;
                   7574: }
                   7575:
                   7576:
1.1       noro     7577: DP ndtodp(int mod,ND p)
                   7578: {
                   7579:     MP m,m0;
                   7580:   DP d;
                   7581:     NM t;
                   7582:     int i,len;
                   7583:
                   7584:     if ( !p ) return 0;
                   7585:     m0 = 0;
                   7586:     len = p->len;
                   7587:     for ( t = BDY(p); t; t = NEXT(t) ) {
                   7588:         NEXTMP(m0,m);
                   7589:       m->dl = ndltodl(nd_nvar,DL(t));
                   7590:       m->c = (Obj)ndctop(mod,t->c);
                   7591:     }
                   7592:     NEXT(m) = 0;
                   7593:   MKDP(nd_nvar,m0,d);
                   7594:     SG(d) = SG(p);
                   7595:     return d;
                   7596: }
                   7597:
                   7598: void ndv_print(NDV p)
                   7599: {
                   7600:     NMV m;
                   7601:     int i,len;
                   7602:
                   7603:     if ( !p ) printf("0\n");
                   7604:     else {
                   7605:         len = LEN(p);
                   7606:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   7607:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     7608:             else printf("+%ld*",CM(m));
1.1       noro     7609:             ndl_print(DL(m));
                   7610:         }
                   7611:         printf("\n");
                   7612:     }
                   7613: }
                   7614:
                   7615: void ndv_print_q(NDV p)
                   7616: {
                   7617:     NMV m;
                   7618:     int i,len;
                   7619:
                   7620:     if ( !p ) printf("0\n");
                   7621:     else {
                   7622:         len = LEN(p);
                   7623:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   7624:             printf("+");
1.6       noro     7625:             printexpr(CO,(Obj)CZ(m));
1.1       noro     7626:             printf("*");
                   7627:             ndl_print(DL(m));
                   7628:         }
                   7629:         printf("\n");
                   7630:     }
                   7631: }
                   7632:
                   7633: NODE ndv_reducebase(NODE x,int *perm)
                   7634: {
                   7635:     int len,i,j;
                   7636:     NDVI w;
                   7637:     NODE t,t0;
                   7638:
1.41      noro     7639:     if ( nd_norb ) return x;
1.1       noro     7640:     len = length(x);
                   7641:     w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
                   7642:     for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
                   7643:         w[i].p = BDY(t); w[i].i = perm[i];
                   7644:     }
                   7645:     for ( i = 0; i < len; i++ ) {
                   7646:         for ( j = 0; j < i; j++ ) {
                   7647:             if ( w[i].p && w[j].p ) {
                   7648:                 if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
                   7649:                 else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
                   7650:             }
                   7651:         }
                   7652:     }
                   7653:     for ( i = j = 0, t0 = 0; i < len; i++ ) {
                   7654:         if ( w[i].p ) {
                   7655:             NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
                   7656:             perm[j++] = w[i].i;
                   7657:         }
                   7658:     }
                   7659:     NEXT(t) = 0; x = t0;
                   7660:     return x;
                   7661: }
                   7662:
                   7663: /* XXX incomplete */
                   7664:
1.21      noro     7665: extern DMMstack dmm_stack;
                   7666: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16      noro     7667:
1.1       noro     7668: void nd_init_ord(struct order_spec *ord)
                   7669: {
                   7670:   nd_module = (ord->id >= 256);
                   7671:   if ( nd_module ) {
                   7672:     nd_dcomp = -1;
1.21      noro     7673:     nd_module_ordtype = ord->module_ordtype;
1.1       noro     7674:     nd_pot_nelim = ord->pot_nelim;
                   7675:     nd_poly_weight_len = ord->nv;
                   7676:     nd_poly_weight = ord->top_weight;
                   7677:     nd_module_rank = ord->module_rank;
                   7678:     nd_module_weight = ord->module_top_weight;
                   7679:   }
                   7680:   nd_matrix = 0;
                   7681:   nd_matrix_len = 0;
                   7682:     switch ( ord->id ) {
                   7683:         case 0:
                   7684:             switch ( ord->ord.simple ) {
                   7685:                 case 0:
                   7686:                     nd_dcomp = 1;
                   7687:                     nd_isrlex = 1;
                   7688:                     break;
                   7689:                 case 1:
                   7690:                     nd_dcomp = 1;
                   7691:                     nd_isrlex = 0;
                   7692:                     break;
                   7693:                 case 2:
                   7694:                     nd_dcomp = 0;
                   7695:                     nd_isrlex = 0;
                   7696:                     ndl_compare_function = ndl_lex_compare;
                   7697:                     break;
                   7698:                 case 11:
                   7699:                     /* XXX */
                   7700:                     nd_dcomp = 0;
                   7701:                     nd_isrlex = 1;
                   7702:                     ndl_compare_function = ndl_ww_lex_compare;
                   7703:                     break;
                   7704:                 default:
                   7705:                     error("nd_gr : unsupported order");
                   7706:             }
                   7707:             break;
                   7708:         case 1:
                   7709:             /* block order */
                   7710:             /* XXX */
                   7711:             nd_dcomp = -1;
                   7712:             nd_isrlex = 0;
                   7713:             ndl_compare_function = ndl_block_compare;
                   7714:             break;
                   7715:         case 2:
                   7716:             /* matrix order */
                   7717:             /* XXX */
                   7718:             nd_dcomp = -1;
                   7719:             nd_isrlex = 0;
                   7720:             nd_matrix_len = ord->ord.matrix.row;
                   7721:             nd_matrix = ord->ord.matrix.matrix;
                   7722:             ndl_compare_function = ndl_matrix_compare;
                   7723:             break;
                   7724:         case 3:
                   7725:             /* composite order */
                   7726:             nd_dcomp = -1;
                   7727:             nd_isrlex = 0;
                   7728:             nd_worb_len = ord->ord.composite.length;
                   7729:             nd_worb = ord->ord.composite.w_or_b;
                   7730:             ndl_compare_function = ndl_composite_compare;
                   7731:             break;
                   7732:
                   7733:         /* module order */
                   7734:         case 256:
                   7735:             switch ( ord->ord.simple ) {
                   7736:                 case 0:
1.21      noro     7737:                     nd_dcomp = 0;
1.1       noro     7738:                     nd_isrlex = 1;
1.21      noro     7739:                     ndl_compare_function = ndl_module_glex_compare;
1.1       noro     7740:                     break;
                   7741:                 case 1:
1.21      noro     7742:                     nd_dcomp = 0;
1.1       noro     7743:                     nd_isrlex = 0;
                   7744:                     ndl_compare_function = ndl_module_glex_compare;
                   7745:                     break;
                   7746:                 case 2:
1.21      noro     7747:                     nd_dcomp = 0;
1.1       noro     7748:                     nd_isrlex = 0;
1.21      noro     7749:                     ndl_compare_function = ndl_module_compare;
                   7750:                     ndl_base_compare_function = ndl_lex_compare;
1.1       noro     7751:                     break;
                   7752:                 default:
1.21      noro     7753:                     error("nd_init_ord : unsupported order");
1.1       noro     7754:             }
                   7755:             break;
                   7756:         case 257:
                   7757:             /* block order */
                   7758:             nd_isrlex = 0;
1.21      noro     7759:             ndl_compare_function = ndl_module_compare;
                   7760:             ndl_base_compare_function = ndl_block_compare;
1.1       noro     7761:             break;
                   7762:         case 258:
                   7763:             /* matrix order */
                   7764:             nd_isrlex = 0;
                   7765:             nd_matrix_len = ord->ord.matrix.row;
                   7766:             nd_matrix = ord->ord.matrix.matrix;
1.21      noro     7767:             ndl_compare_function = ndl_module_compare;
                   7768:             ndl_base_compare_function = ndl_matrix_compare;
1.1       noro     7769:             break;
                   7770:         case 259:
                   7771:             /* composite order */
                   7772:             nd_isrlex = 0;
                   7773:             nd_worb_len = ord->ord.composite.length;
                   7774:             nd_worb = ord->ord.composite.w_or_b;
1.21      noro     7775:             ndl_compare_function = ndl_module_compare;
                   7776:             ndl_base_compare_function = ndl_composite_compare;
                   7777:             break;
                   7778:         case 300:
                   7779:             /* schreyer order */
                   7780:             if ( ord->base->id != 256 )
                   7781:                error("nd_init_ord : unsupported base order");
                   7782:             ndl_compare_function = ndl_module_schreyer_compare;
                   7783:             dmm_stack = ord->dmmstack;
                   7784:             switch ( ord->base->ord.simple ) {
                   7785:                 case 0:
                   7786:                     nd_isrlex = 1;
                   7787:                     ndl_base_compare_function = ndl_glex_compare;
                   7788:                     dl_base_compare_function = cmpdl_revgradlex;
                   7789:                     break;
                   7790:                 case 1:
                   7791:                     nd_isrlex = 0;
                   7792:                     ndl_base_compare_function = ndl_glex_compare;
                   7793:                     dl_base_compare_function = cmpdl_gradlex;
                   7794:                     break;
                   7795:                 case 2:
                   7796:                     nd_isrlex = 0;
                   7797:                     ndl_base_compare_function = ndl_lex_compare;
                   7798:                     dl_base_compare_function = cmpdl_lex;
                   7799:                     break;
                   7800:                 default:
                   7801:                     error("nd_init_ord : unsupported order");
                   7802:             }
1.1       noro     7803:             break;
                   7804:     }
                   7805:     nd_ord = ord;
                   7806: }
                   7807:
                   7808: BlockMask nd_create_blockmask(struct order_spec *ord)
                   7809: {
                   7810:     int n,i,j,s,l;
                   7811:     UINT *t;
                   7812:     BlockMask bm;
                   7813:
                   7814:     /* we only create mask table for block order */
                   7815:     if ( ord->id != 1 && ord->id != 257 )
                   7816:         return 0;
                   7817:     n = ord->ord.block.length;
                   7818:     bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   7819:     bm->n = n;
                   7820:     bm->order_pair = ord->ord.block.order_pair;
                   7821:     bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
                   7822:     for ( i = 0, s = 0; i < n; i++ ) {
                   7823:         bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
                   7824:         for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   7825:         l = bm->order_pair[i].length;
                   7826:         for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   7827:     }
                   7828:     return bm;
                   7829: }
                   7830:
                   7831: EPOS nd_create_epos(struct order_spec *ord)
                   7832: {
                   7833:     int i,j,l,s,ord_l,ord_o;
                   7834:     EPOS epos;
                   7835:     struct order_pair *op;
                   7836:
                   7837:     epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   7838:     switch ( ord->id ) {
1.21      noro     7839:         case 0: case 256: case 300:
1.1       noro     7840:             if ( nd_isrlex ) {
                   7841:                 for ( i = 0; i < nd_nvar; i++ ) {
                   7842:                     epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   7843:                     epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   7844:                 }
                   7845:             } else {
                   7846:                 for ( i = 0; i < nd_nvar; i++ ) {
                   7847:                     epos[i].i = nd_exporigin + i/nd_epw;
                   7848:                     epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   7849:                 }
                   7850:             }
                   7851:             break;
                   7852:         case 1: case 257:
                   7853:             /* block order */
                   7854:             l = ord->ord.block.length;
                   7855:             op = ord->ord.block.order_pair;
                   7856:             for ( j = 0, s = 0; j < l; j++ ) {
                   7857:                 ord_o = op[j].order;
                   7858:                 ord_l = op[j].length;
                   7859:                 if ( !ord_o )
                   7860:                     for ( i = 0; i < ord_l; i++ ) {
                   7861:                         epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   7862:                         epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   7863:                     }
                   7864:                 else
                   7865:                     for ( i = 0; i < ord_l; i++ ) {
                   7866:                         epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   7867:                         epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   7868:                     }
                   7869:                 s += ord_l;
                   7870:             }
                   7871:             break;
                   7872:         case 2:
                   7873:             /* matrix order */
                   7874:         case 3:
                   7875:             /* composite order */
                   7876:         default:
                   7877:             for ( i = 0; i < nd_nvar; i++ ) {
                   7878:                 epos[i].i = nd_exporigin + i/nd_epw;
                   7879:                 epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   7880:             }
                   7881:             break;
                   7882:     }
                   7883:     return epos;
                   7884: }
                   7885:
                   7886: /* external interface */
                   7887:
                   7888: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
                   7889: {
                   7890:     NODE t,in0,in;
                   7891:     ND ndf,nf;
                   7892:     NDV ndvf;
                   7893:     VL vv,tv;
                   7894:     int stat,nvar,max,mrank;
                   7895:     union oNDC dn;
                   7896:     Q cont;
                   7897:     P pp;
                   7898:     LIST ppl;
                   7899:
                   7900:     if ( !f ) {
                   7901:         *rp = 0;
                   7902:         return;
                   7903:     }
                   7904:     pltovl(v,&vv);
                   7905:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   7906:
                   7907:     /* max=65536 implies nd_bpe=32 */
                   7908:     max = 65536;
                   7909:
                   7910:   nd_module = 0;
                   7911:   /* nd_module will be set if ord is a module ordering */
                   7912:     nd_init_ord(ord);
                   7913:     nd_setup_parameters(nvar,max);
                   7914:     if ( nd_module && OID(f) != O_LIST )
                   7915:         error("nd_nf_p : the first argument must be a list");
                   7916:   if ( nd_module ) mrank = length(BDY((LIST)f));
                   7917:     /* conversion to ndv */
                   7918:     for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   7919:         NEXTNODE(in0,in);
                   7920:         if ( nd_module ) {
                   7921:           if ( !BDY(t) || OID(BDY(t)) != O_LIST
                   7922:                || length(BDY((LIST)BDY(t))) != mrank )
                   7923:               error("nd_nf_p : inconsistent basis element");
                   7924:           if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
                   7925:           else ppl = (LIST)BDY(t);
                   7926:           BDY(in) = (pointer)pltondv(CO,vv,ppl);
                   7927:         } else {
                   7928:           if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
                   7929:           else pp = (P)BDY(t);
                   7930:           BDY(in) = (pointer)ptondv(CO,vv,pp);
                   7931:         }
                   7932:         if ( m ) ndv_mod(m,(NDV)BDY(in));
                   7933:     }
                   7934:     if ( in0 ) NEXT(in) = 0;
                   7935:
                   7936:     if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
                   7937:     else ndvf = ptondv(CO,vv,(P)f);
                   7938:     if ( m ) ndv_mod(m,ndvf);
                   7939:     ndf = (pointer)ndvtond(m,ndvf);
                   7940:
                   7941:     /* dont sort, dont removecont */
1.24      noro     7942:     ndv_setup(m,0,in0,1,1,0);
1.1       noro     7943:     nd_scale=2;
1.6       noro     7944:     stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1       noro     7945:     if ( !stat )
                   7946:         error("nd_nf_p : exponent too large");
                   7947:     if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
                   7948:     else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
                   7949: }
                   7950:
                   7951: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   7952: {
                   7953:     NM m;
                   7954:     UINT *t,*s;
                   7955:     int i;
                   7956:
                   7957:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   7958:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7959:         t = DL(m);
                   7960:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   7961:         r[i] = CM(m);
                   7962:     }
                   7963:     for ( i = 0; !r[i]; i++ );
                   7964:     return i;
                   7965: }
                   7966:
                   7967: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
                   7968: {
                   7969:     NM m;
                   7970:     UINT *t,*s;
                   7971:     int i;
                   7972:
                   7973:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   7974:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7975:         t = DL(m);
                   7976:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     7977:         r[i] = CZ(m);
1.1       noro     7978:     }
                   7979:     for ( i = 0; !r[i]; i++ );
                   7980:     return i;
                   7981: }
                   7982:
                   7983: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
                   7984: {
                   7985:     NM m;
                   7986:     UINT *t,*s;
                   7987:     int i;
                   7988:
                   7989:     for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
                   7990:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7991:         t = DL(m);
                   7992:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   7993:         mpz_set(r[i],BDY(CZ(m)));
                   7994:     }
                   7995:     for ( i = 0; !mpz_sgn(r[i]); i++ );
                   7996:     return i;
                   7997: }
                   7998:
                   7999: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
                   8000: {
                   8001:     NM m;
                   8002:     unsigned long *v;
                   8003:     int i,j,h,size;
                   8004:   UINT *s,*t;
                   8005:
                   8006:   size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
                   8007:     v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
                   8008:     bzero(v,size);
                   8009:     for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
                   8010:     t = DL(m);
                   8011:     h = ndl_hash_value(t);
                   8012:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   8013:       v[i/BLEN] |= 1L <<(i%BLEN);
                   8014:     }
                   8015:     return v;
                   8016: }
                   8017:
                   8018: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
                   8019: {
                   8020:     NMV mr;
                   8021:     UINT *d,*t,*s;
                   8022:     int i,j,len,h,head;
                   8023:
                   8024:     d = DL(m);
                   8025:     len = LEN(p);
                   8026:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   8027:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   8028:         ndl_add(d,DL(mr),t);
                   8029:     h = ndl_hash_value(t);
                   8030:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   8031:     if ( j == 0 ) head = i;
                   8032:       v[i/BLEN] |= 1L <<(i%BLEN);
                   8033:     }
                   8034:     return head;
                   8035: }
                   8036:
                   8037: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   8038: {
                   8039:     NM m;
                   8040:     NMV mr;
                   8041:     UINT *d,*t,*s;
                   8042:     NDV p;
                   8043:     int i,j,len;
                   8044:     Z *r;
                   8045:
                   8046:     m = pair->mul;
                   8047:     d = DL(m);
                   8048:     p = nd_ps[pair->index];
                   8049:     len = LEN(p);
                   8050:     r = (Z *)CALLOC(n,sizeof(Q));
                   8051:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   8052:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   8053:         ndl_add(d,DL(mr),t);
                   8054:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     8055:         r[i] = CZ(mr);
1.1       noro     8056:     }
                   8057:     return r;
                   8058: }
                   8059:
1.11      noro     8060: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1       noro     8061: {
                   8062:     NM m;
                   8063:     NMV mr;
1.11      noro     8064:     UINT *d,*t,*s,*u;
1.1       noro     8065:     NDV p;
                   8066:     unsigned char *ivc;
                   8067:     unsigned short *ivs;
                   8068:     UINT *v,*ivi,*s0v;
1.11      noro     8069:     int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1       noro     8070:     IndArray r;
                   8071:
                   8072:     m = pair->mul;
                   8073:     d = DL(m);
                   8074:     if ( trace )
                   8075:       p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
                   8076:     else
                   8077:       p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
                   8078:
                   8079:     len = LEN(p);
                   8080:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   8081:     v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11      noro     8082:     for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
                   8083:       ndl_add(d,DL(mr),t);
                   8084:       st = prev;
                   8085:       ed = n;
                   8086:       while ( ed > st ) {
                   8087:         md = (st+ed)/2;
                   8088:         u = s0+md*nd_wpd;
                   8089:         c = DL_COMPARE(u,t);
                   8090:         if ( c == 0 ) break;
                   8091:         else if ( c > 0 ) st = md;
                   8092:         else ed = md;
                   8093:       }
                   8094:       prev = v[j] = md;
1.1       noro     8095:     }
                   8096:     r = (IndArray)MALLOC(sizeof(struct oIndArray));
                   8097:     r->head = v[0];
                   8098:     diff = 0;
                   8099:     for ( i = 1; i < len; i++ ) {
                   8100:         cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   8101:     }
                   8102:     if ( diff < 256 ) {
                   8103:         r->width = 1;
                   8104:         ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   8105:         r->index.c = ivc;
                   8106:         for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   8107:     } else if ( diff < 65536 ) {
                   8108:         r->width = 2;
                   8109:         ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   8110:         r->index.s = ivs;
                   8111:         for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   8112:     } else {
                   8113:         r->width = 4;
                   8114:         ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   8115:         r->index.i = ivi;
                   8116:         for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
                   8117:     }
                   8118:     return r;
                   8119: }
                   8120:
                   8121: int compress_array(Z *svect,Z *cvect,int n)
                   8122: {
                   8123:     int i,j;
                   8124:
                   8125:     for ( i = j = 0; i < n; i++ )
                   8126:         if ( svect[i] ) cvect[j++] = svect[i];
                   8127:     return j;
                   8128: }
                   8129:
                   8130: void expand_array(Z *svect,Z *cvect,int n)
                   8131: {
                   8132:     int i,j;
                   8133:
                   8134:     for ( i = j = 0; j < n;  i++  )
                   8135:         if ( svect[i] ) svect[i] = cvect[j++];
                   8136: }
                   8137:
1.8       noro     8138: #if 0
1.1       noro     8139: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8140: {
                   8141:     int i,j,k,len,pos,prev,nz;
                   8142:     Z cs,mcs,c1,c2,cr,gcd,t;
                   8143:     IndArray ivect;
                   8144:     unsigned char *ivc;
                   8145:     unsigned short *ivs;
                   8146:     unsigned int *ivi;
                   8147:     NDV redv;
                   8148:     NMV mr;
                   8149:     NODE rp;
                   8150:     int maxrs;
                   8151:     double hmag;
                   8152:     Z *cvect;
1.3       noro     8153:     int l;
1.1       noro     8154:
                   8155:     maxrs = 0;
                   8156:     for ( i = 0; i < col && !svect[i]; i++ );
                   8157:     if ( i == col ) return maxrs;
                   8158:     hmag = p_mag((P)svect[i])*nd_scale;
                   8159:     cvect = (Z *)MALLOC(col*sizeof(Q));
                   8160:     for ( i = 0; i < nred; i++ ) {
                   8161:         ivect = imat[i];
                   8162:         k = ivect->head;
                   8163:         if ( svect[k] ) {
                   8164:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8165:             redv = nd_demand?ndv_load(rp0[i]->index)
                   8166:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   8167:             len = LEN(redv); mr = BDY(redv);
1.6       noro     8168:             igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1       noro     8169:             chsgnz(cs,&mcs);
                   8170:             if ( !UNIQ(cr) ) {
                   8171:                 for ( j = 0; j < col; j++ ) {
                   8172:                     mulz(svect[j],cr,&c1); svect[j] = c1;
                   8173:                 }
                   8174:             }
                   8175:             svect[k] = 0; prev = k;
                   8176:             switch ( ivect->width ) {
                   8177:                 case 1:
                   8178:                     ivc = ivect->index.c;
                   8179:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8180:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     8181:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     8182:                     }
                   8183:                     break;
                   8184:                 case 2:
                   8185:                     ivs = ivect->index.s;
                   8186:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8187:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     8188:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     8189:                     }
                   8190:                     break;
                   8191:                 case 4:
                   8192:                     ivi = ivect->index.i;
                   8193:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8194:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     8195:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     8196:                     }
                   8197:                     break;
                   8198:             }
                   8199:             for ( j = k+1; j < col && !svect[j]; j++ );
                   8200:             if ( j == col ) break;
                   8201:             if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
                   8202:                 nz = compress_array(svect,cvect,col);
                   8203:                 removecont_array((P *)cvect,nz,1);
                   8204:                 expand_array(svect,cvect,nz);
                   8205:                 hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   8206:             }
                   8207:         }
                   8208:     }
                   8209:     nz = compress_array(svect,cvect,col);
                   8210:     removecont_array((P *)cvect,nz,1);
                   8211:     expand_array(svect,cvect,nz);
                   8212:     if ( DP_Print ) {
                   8213:         fprintf(asir_out,"-"); fflush(asir_out);
                   8214:     }
                   8215:     return maxrs;
                   8216: }
1.4       noro     8217: #else
1.9       noro     8218:
1.4       noro     8219: /* direct mpz version */
                   8220: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8221: {
                   8222:     int i,j,k,len,pos,prev;
                   8223:     mpz_t cs,cr,gcd;
                   8224:     IndArray ivect;
                   8225:     unsigned char *ivc;
                   8226:     unsigned short *ivs;
                   8227:     unsigned int *ivi;
                   8228:     NDV redv;
                   8229:     NMV mr;
                   8230:     NODE rp;
                   8231:     int maxrs;
                   8232:     double hmag;
                   8233:     int l;
1.13      noro     8234:     static mpz_t *svect;
                   8235:     static int svect_len=0;
1.4       noro     8236:
                   8237:     maxrs = 0;
                   8238:     for ( i = 0; i < col && !svect0[i]; i++ );
                   8239:     if ( i == col ) return maxrs;
                   8240:     hmag = p_mag((P)svect0[i])*nd_scale;
1.13      noro     8241:     if ( col > svect_len ) {
                   8242:       svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   8243:       svect_len = col;
                   8244:     }
1.4       noro     8245:     for ( i = 0; i < col; i++ ) {
                   8246:       mpz_init(svect[i]);
                   8247:       if ( svect0[i] )
                   8248:         mpz_set(svect[i],BDY(svect0[i]));
                   8249:       else
                   8250:         mpz_set_ui(svect[i],0);
                   8251:     }
                   8252:     mpz_init(gcd); mpz_init(cs); mpz_init(cr);
                   8253:     for ( i = 0; i < nred; i++ ) {
                   8254:         ivect = imat[i];
                   8255:         k = ivect->head;
                   8256:         if ( mpz_sgn(svect[k]) ) {
                   8257:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8258:             redv = nd_demand?ndv_load(rp0[i]->index)
                   8259:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   8260:             len = LEN(redv); mr = BDY(redv);
1.6       noro     8261:             mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4       noro     8262:             mpz_div(cs,svect[k],gcd);
1.6       noro     8263:             mpz_div(cr,BDY(CZ(mr)),gcd);
1.4       noro     8264:             mpz_neg(cs,cs);
1.9       noro     8265:             if ( MUNIMPZ(cr) )
                   8266:               for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
                   8267:             else if ( !UNIMPZ(cr) )
                   8268:               for ( j = 0; j < col; j++ ) {
                   8269:                 if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
                   8270:               }
1.4       noro     8271:             mpz_set_ui(svect[k],0);
                   8272:             prev = k;
                   8273:             switch ( ivect->width ) {
                   8274:                 case 1:
                   8275:                     ivc = ivect->index.c;
                   8276:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8277:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     8278:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     8279:                     }
                   8280:                     break;
                   8281:                 case 2:
                   8282:                     ivs = ivect->index.s;
                   8283:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8284:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     8285:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     8286:                     }
                   8287:                     break;
                   8288:                 case 4:
                   8289:                     ivi = ivect->index.i;
                   8290:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8291:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     8292:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     8293:                     }
                   8294:                     break;
                   8295:             }
                   8296:             for ( j = k+1; j < col && !svect[j]; j++ );
                   8297:             if ( j == col ) break;
                   8298:             if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
                   8299:                 mpz_removecont_array(svect,col);
                   8300:                 hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
                   8301:             }
                   8302:         }
                   8303:     }
                   8304:     mpz_removecont_array(svect,col);
                   8305:     if ( DP_Print ) {
                   8306:         fprintf(asir_out,"-"); fflush(asir_out);
                   8307:     }
                   8308:     for ( i = 0; i < col; i++ )
                   8309:       if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
                   8310:       else svect0[i] = 0;
                   8311:     return maxrs;
                   8312: }
                   8313: #endif
1.1       noro     8314:
1.40      noro     8315: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred,SIG sig)
1.1       noro     8316: {
                   8317:     int i,j,k,len,pos,prev;
                   8318:     UINT c,c1,c2,c3,up,lo,dmy;
                   8319:     IndArray ivect;
                   8320:     unsigned char *ivc;
                   8321:     unsigned short *ivs;
                   8322:     unsigned int *ivi;
                   8323:     NDV redv;
                   8324:     NMV mr;
                   8325:     NODE rp;
                   8326:     int maxrs;
                   8327:
                   8328:     maxrs = 0;
                   8329:     for ( i = 0; i < nred; i++ ) {
                   8330:         ivect = imat[i];
                   8331:         k = ivect->head; svect[k] %= m;
1.40      noro     8332:         if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.1       noro     8333:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8334:             c = m-c; redv = nd_ps[rp0[i]->index];
                   8335:             len = LEN(redv); mr = BDY(redv);
                   8336:             svect[k] = 0; prev = k;
                   8337:             switch ( ivect->width ) {
                   8338:                 case 1:
                   8339:                     ivc = ivect->index.c;
                   8340:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8341:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.40      noro     8342:                         if ( c1 ) {
                   8343:                           c2 = svect[pos];
1.1       noro     8344:                           DMA(c1,c,c2,up,lo);
                   8345:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   8346:                           } else svect[pos] = lo;
1.40      noro     8347:                         }
1.1       noro     8348:                     }
                   8349:                     break;
                   8350:                 case 2:
                   8351:                     ivs = ivect->index.s;
                   8352:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8353:                         pos = prev+ivs[j]; c1 = CM(mr);
                   8354:                         prev = pos;
1.40      noro     8355:                         if ( c1 ) {
                   8356:                           c2 = svect[pos];
1.1       noro     8357:                           DMA(c1,c,c2,up,lo);
                   8358:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   8359:                           } else svect[pos] = lo;
1.40      noro     8360:                         }
1.1       noro     8361:                     }
                   8362:                     break;
                   8363:                 case 4:
                   8364:                     ivi = ivect->index.i;
                   8365:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8366:                         pos = prev+ivi[j]; c1 = CM(mr);
                   8367:                         prev = pos;
1.40      noro     8368:                         if ( c1 ) {
                   8369:                           c2 = svect[pos];
1.1       noro     8370:                           DMA(c1,c,c2,up,lo);
                   8371:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   8372:                           } else svect[pos] = lo;
1.40      noro     8373:                         }
1.1       noro     8374:                     }
                   8375:                     break;
                   8376:             }
                   8377:         }
                   8378:     }
                   8379:     for ( i = 0; i < col; i++ )
                   8380:         if ( svect[i] >= (UINT)m ) svect[i] %= m;
                   8381:     return maxrs;
                   8382: }
                   8383:
                   8384: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8385: {
                   8386:     int i,j,k,len,pos,prev;
                   8387:     UINT c,c1,c2,c3,up,lo,dmy;
                   8388:     IndArray ivect;
                   8389:     unsigned char *ivc;
                   8390:     unsigned short *ivs;
                   8391:     unsigned int *ivi;
                   8392:     NDV redv;
                   8393:     NMV mr;
                   8394:     NODE rp;
                   8395:     int maxrs;
                   8396:
                   8397:     maxrs = 0;
                   8398:     for ( i = 0; i < nred; i++ ) {
                   8399:         ivect = imat[i];
                   8400:         k = ivect->head;
                   8401:         if ( (c = svect[k]) != 0 ) {
                   8402:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8403:             c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
                   8404:             len = LEN(redv); mr = BDY(redv);
                   8405:             svect[k] = 0; prev = k;
                   8406:             switch ( ivect->width ) {
                   8407:                 case 1:
                   8408:                     ivc = ivect->index.c;
                   8409:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8410:                         pos = prev+ivc[j]; prev = pos;
                   8411:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   8412:                     }
                   8413:                     break;
                   8414:                 case 2:
                   8415:                     ivs = ivect->index.s;
                   8416:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8417:                         pos = prev+ivs[j]; prev = pos;
                   8418:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   8419:                     }
                   8420:                     break;
                   8421:                 case 4:
                   8422:                     ivi = ivect->index.i;
                   8423:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8424:                         pos = prev+ivi[j]; prev = pos;
                   8425:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   8426:                     }
                   8427:                     break;
                   8428:             }
                   8429:         }
                   8430:     }
                   8431:     return maxrs;
                   8432: }
                   8433:
                   8434: ND nd_add_lf(ND p1,ND p2)
                   8435: {
                   8436:     int n,c,can;
                   8437:     ND r;
                   8438:     NM m1,m2,mr0,mr,s;
                   8439:     Z t;
                   8440:
                   8441:     if ( !p1 ) return p2;
                   8442:     else if ( !p2 ) return p1;
                   8443:     else {
                   8444:         can = 0;
                   8445:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   8446:             c = DL_COMPARE(DL(m1),DL(m2));
                   8447:             switch ( c ) {
                   8448:                 case 0:
                   8449:                     addlf(CZ(m1),CZ(m2),&t);
                   8450:                     s = m1; m1 = NEXT(m1);
                   8451:                     if ( t ) {
                   8452:                         can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
                   8453:                     } else {
                   8454:                         can += 2; FREENM(s);
                   8455:                     }
                   8456:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   8457:                     break;
                   8458:                 case 1:
                   8459:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   8460:                     break;
                   8461:                 case -1:
                   8462:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   8463:                     break;
                   8464:             }
                   8465:         }
                   8466:         if ( !mr0 )
                   8467:             if ( m1 ) mr0 = m1;
                   8468:             else if ( m2 ) mr0 = m2;
                   8469:             else return 0;
                   8470:         else if ( m1 ) NEXT(mr) = m1;
                   8471:         else if ( m2 ) NEXT(mr) = m2;
                   8472:         else NEXT(mr) = 0;
                   8473:         BDY(p1) = mr0;
                   8474:         SG(p1) = MAX(SG(p1),SG(p2));
                   8475:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   8476:         FREEND(p2);
                   8477:         return p1;
                   8478:     }
                   8479: }
                   8480:
                   8481: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8482: {
                   8483:     int i,j,k,len,pos,prev;
                   8484:     mpz_t c,mc,c1;
                   8485:     IndArray ivect;
                   8486:     unsigned char *ivc;
                   8487:     unsigned short *ivs;
                   8488:     unsigned int *ivi;
                   8489:     NDV redv;
                   8490:     NMV mr;
                   8491:     NODE rp;
                   8492:     int maxrs;
                   8493:
                   8494:     maxrs = 0;
                   8495:     lf_lazy = 1;
                   8496:     for ( i = 0; i < nred; i++ ) {
                   8497:         ivect = imat[i];
                   8498:         k = ivect->head;
                   8499:         mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
                   8500:         if ( mpz_sgn(svect[k]) ) {
                   8501:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8502:             mpz_neg(svect[k],svect[k]);
                   8503:             redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
                   8504:             len = LEN(redv); mr = BDY(redv);
                   8505:             prev = k;
                   8506:             switch ( ivect->width ) {
                   8507:                 case 1:
                   8508:                     ivc = ivect->index.c;
                   8509:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8510:                         pos = prev+ivc[j]; prev = pos;
                   8511:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   8512:                     }
                   8513:                     break;
                   8514:                 case 2:
                   8515:                     ivs = ivect->index.s;
                   8516:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8517:                         pos = prev+ivs[j]; prev = pos;
                   8518:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   8519:                     }
                   8520:                     break;
                   8521:                 case 4:
                   8522:                     ivi = ivect->index.i;
                   8523:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8524:                         pos = prev+ivi[j]; prev = pos;
                   8525:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   8526:                     }
                   8527:                     break;
                   8528:             }
                   8529:             mpz_set_ui(svect[k],0);
                   8530:         }
                   8531:     }
                   8532:     lf_lazy=0;
                   8533:     for ( i = 0; i < col; i++ ) {
                   8534:         mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
                   8535:     }
                   8536:     return maxrs;
                   8537: }
                   8538:
                   8539: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
                   8540: {
                   8541:     int i,j,k,l,rank,s;
                   8542:     mpz_t a,a1,inv;
                   8543:     mpz_t *t,*pivot,*pk;
                   8544:     mpz_t **mat;
                   8545:     struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
                   8546:     int size,size1;
                   8547:
                   8548:     mpz_init(inv);
                   8549:     mpz_init(a);
                   8550:     mat = (mpz_t **)mat0;
                   8551:         size = 0;
                   8552:     for ( rank = 0, j = 0; j < col; j++ ) {
                   8553:         for ( i = rank; i < row; i++ ) {
                   8554:             mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
                   8555:         }
                   8556:         for ( i = rank; i < row; i++ )
                   8557:             if ( mpz_sgn(mat[i][j]) )
                   8558:                 break;
                   8559:         if ( i == row ) {
                   8560:             colstat[j] = 0;
                   8561:             continue;
                   8562:         } else
                   8563:             colstat[j] = 1;
                   8564:         if ( i != rank ) {
                   8565:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   8566:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   8567:         }
                   8568:         pivot = mat[rank];
                   8569:         s = sugar[rank];
                   8570:         mpz_invert(inv,pivot[j],BDY(current_mod_lf));
                   8571:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   8572:             if ( mpz_sgn(*pk) ) {
                   8573:                 mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
                   8574:             }
                   8575:         for ( i = rank+1; i < row; i++ ) {
                   8576:             t = mat[i];
                   8577:             if ( mpz_sgn(t[j]) ) {
                   8578:                 sugar[i] = MAX(sugar[i],s);
                   8579:                 mpz_neg(a,t[j]);
                   8580:                 red_by_vect_lf(t+j,pivot+j,a,col-j);
                   8581:             }
                   8582:         }
                   8583:         rank++;
                   8584:     }
                   8585:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   8586:         if ( colstat[j] ) {
                   8587:             pivot = mat[l];
                   8588:             s = sugar[l];
                   8589:             for ( k = j; k < col; k++ )
                   8590:               mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
                   8591:             for ( i = 0; i < l; i++ ) {
                   8592:                 t = mat[i];
                   8593:                 if ( mpz_sgn(t[j]) ) {
                   8594:                     sugar[i] = MAX(sugar[i],s);
                   8595:                     mpz_neg(a,t[j]);
                   8596:                     red_by_vect_lf(t+j,pivot+j,a,col-j);
                   8597:                 }
                   8598:             }
                   8599:             l--;
                   8600:         }
                   8601:     for ( j = 0, l = 0; l < rank; j++ )
                   8602:         if ( colstat[j] ) {
                   8603:             t = mat[l];
                   8604:             for ( k = j; k < col; k++ ) {
                   8605:                 mpz_mod(t[k],t[k],BDY(current_mod_lf));
                   8606:             }
                   8607:             l++;
                   8608:         }
                   8609:     return rank;
                   8610: }
                   8611:
                   8612:
                   8613: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   8614: {
                   8615:     int j,k,len;
                   8616:     UINT *p;
                   8617:     UINT c;
                   8618:     NDV r;
                   8619:     NMV mr0,mr;
                   8620:
                   8621:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   8622:     if ( !len ) return 0;
                   8623:     else {
                   8624:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8625: #if 0
                   8626:         ndv_alloc += nmv_adv*len;
                   8627: #endif
                   8628:         mr = mr0;
                   8629:         p = s0vect;
                   8630:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   8631:             if ( !rhead[j] ) {
                   8632:                 if ( (c = vect[k++]) != 0 ) {
                   8633:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   8634:                 }
                   8635:             }
                   8636:         MKNDV(nd_nvar,mr0,len,r);
                   8637:         return r;
                   8638:     }
                   8639: }
                   8640:
1.40      noro     8641: NDV vect_to_ndv_s(UINT *vect,int col,UINT *s0vect)
                   8642: {
                   8643:     int j,k,len;
                   8644:     UINT *p;
                   8645:     UINT c;
                   8646:     NDV r;
                   8647:     NMV mr0,mr;
                   8648:
                   8649:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   8650:     if ( !len ) return 0;
                   8651:     else {
                   8652:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8653:         mr = mr0;
                   8654:         p = s0vect;
                   8655:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   8656:           if ( (c = vect[k++]) != 0 ) {
                   8657:             ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   8658:           }
                   8659:         MKNDV(nd_nvar,mr0,len,r);
                   8660:         return r;
                   8661:     }
                   8662: }
                   8663:
1.1       noro     8664: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
                   8665: {
                   8666:     int j,k,len;
                   8667:     UINT *p;
                   8668:     NDV r;
                   8669:     NMV mr0,mr;
                   8670:
                   8671:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
                   8672:     if ( !len ) return 0;
                   8673:     else {
                   8674:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8675:         mr = mr0;
                   8676:         p = s0vect;
                   8677:         for ( j = 0; j < col; j++, p += nd_wpd )
                   8678:       if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
                   8679:             ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
                   8680:           }
                   8681:         MKNDV(nd_nvar,mr0,len,r);
                   8682:         return r;
                   8683:     }
                   8684: }
                   8685:
                   8686: /* for preprocessed vector */
                   8687:
                   8688: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   8689: {
1.6       noro     8690:   int j,k,len;
                   8691:   UINT *p;
                   8692:   Z c;
                   8693:   NDV r;
                   8694:   NMV mr0,mr;
1.1       noro     8695:
1.6       noro     8696:   for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   8697:   if ( !len ) return 0;
                   8698:   else {
                   8699:     mr0 = (NMV)MALLOC(nmv_adv*len);
1.1       noro     8700: #if 0
1.6       noro     8701:     ndv_alloc += nmv_adv*len;
1.1       noro     8702: #endif
1.6       noro     8703:     mr = mr0;
                   8704:     p = s0vect;
                   8705:     for ( j = k = 0; j < col; j++, p += nd_wpd ) {
                   8706:       if ( !rhead[j] ) {
                   8707:         if ( (c = vect[k++]) != 0 ) {
                   8708:           if ( !INT(c) )
                   8709:             error("vect_to_ndv_q : components must be integers");
                   8710:             ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
                   8711:         }
                   8712:       }
1.1       noro     8713:     }
1.6       noro     8714:     MKNDV(nd_nvar,mr0,len,r);
                   8715:     return r;
                   8716:   }
1.1       noro     8717: }
                   8718:
                   8719: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   8720: {
                   8721:     int j,k,len;
                   8722:     UINT *p;
                   8723:     mpz_t c;
                   8724:     NDV r;
                   8725:     NMV mr0,mr;
                   8726:
                   8727:     for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
                   8728:     if ( !len ) return 0;
                   8729:     else {
                   8730:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   8731: #if 0
                   8732:         ndv_alloc += nmv_adv*len;
                   8733: #endif
                   8734:         mr = mr0;
                   8735:         p = s0vect;
                   8736:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   8737:             if ( !rhead[j] ) {
                   8738:                 c[0] = vect[k++][0];
                   8739:                 if ( mpz_sgn(c) ) {
                   8740:                     ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
                   8741:                 }
                   8742:             }
                   8743:         MKNDV(nd_nvar,mr0,len,r);
                   8744:         return r;
                   8745:     }
                   8746: }
                   8747:
                   8748: /* for plain vector */
                   8749:
                   8750: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
                   8751: {
                   8752:     int j,k,len;
                   8753:     UINT *p;
                   8754:     Z c;
                   8755:     NDV r;
                   8756:     NMV mr0,mr;
                   8757:
                   8758:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   8759:     if ( !len ) return 0;
                   8760:     else {
                   8761:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   8762: #if 0
                   8763:         ndv_alloc += nmv_adv*len;
                   8764: #endif
                   8765:         mr = mr0;
                   8766:         p = s0vect;
                   8767:         for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   8768:             if ( (c = vect[k]) != 0 ) {
                   8769:                 if ( !INT(c) )
1.6       noro     8770:                     error("plain_vect_to_ndv_q : components must be integers");
                   8771:                 ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1       noro     8772:             }
                   8773:         MKNDV(nd_nvar,mr0,len,r);
                   8774:         return r;
                   8775:     }
                   8776: }
                   8777:
                   8778: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
                   8779: {
                   8780:     ND_pairs t;
                   8781:     NODE sp0,sp;
                   8782:     int stat;
                   8783:     ND spol;
                   8784:
                   8785:     for ( t = l; t; t = NEXT(t) ) {
                   8786:         stat = nd_sp(m,trace,t,&spol);
                   8787:         if ( !stat ) return 0;
                   8788:         if ( spol ) {
                   8789:             add_pbucket_symbolic(bucket,spol);
                   8790:         }
                   8791:     }
                   8792:     return 1;
                   8793: }
                   8794:
                   8795: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   8796: {
                   8797:     NODE rp0,rp;
                   8798:     NM mul,head,s0,s;
                   8799:     int index,col,i,sugar;
                   8800:     RHist h;
                   8801:     UINT *s0v,*p;
                   8802:     NM_ind_pair pair;
                   8803:     ND red;
                   8804:     NDV *ps;
                   8805:
                   8806:     s0 = 0; rp0 = 0; col = 0;
                   8807:   if ( nd_demand )
                   8808:       ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   8809:   else
                   8810:       ps = trace?nd_ps_trace:nd_ps;
                   8811:     while ( 1 ) {
                   8812:         head = remove_head_pbucket_symbolic(bucket);
                   8813:         if ( !head ) break;
                   8814:         if ( !s0 ) s0 = head;
                   8815:         else NEXT(s) = head;
                   8816:         s = head;
                   8817:         index = ndl_find_reducer(DL(head));
                   8818:         if ( index >= 0 ) {
                   8819:             h = nd_psh[index];
                   8820:             NEWNM(mul);
                   8821:             ndl_sub(DL(head),DL(h),DL(mul));
                   8822:             if ( ndl_check_bound2(index,DL(mul)) )
                   8823:                 return 0;
                   8824:             sugar = TD(DL(mul))+SG(ps[index]);
1.28      noro     8825:             MKNM_ind_pair(pair,mul,index,sugar,0);
1.1       noro     8826:             red = ndv_mul_nm_symbolic(mul,ps[index]);
                   8827:             add_pbucket_symbolic(bucket,nd_remove_head(red));
                   8828:             NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   8829:         }
                   8830:         col++;
                   8831:     }
                   8832:     if ( rp0 ) NEXT(rp) = 0;
                   8833:     NEXT(s) = 0;
                   8834:     s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   8835:     for ( i = 0, p = s0v, s = s0; i < col;
                   8836:         i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   8837:     *s0vect = s0v;
                   8838:     *r = rp0;
                   8839:     return col;
                   8840: }
                   8841:
                   8842: void print_ndp(ND_pairs l)
                   8843: {
                   8844:   ND_pairs t;
                   8845:
                   8846:   for ( t = l; t; t = NEXT(t) )
                   8847:     printf("[%d,%d] ",t->i1,t->i2);
                   8848:   printf("\n");
                   8849: }
                   8850:
                   8851: NODE nd_f4(int m,int checkonly,int **indp)
                   8852: {
                   8853:     int i,nh,stat,index,f4red;
                   8854:     NODE r,g,tn0,tn,node;
                   8855:     ND_pairs d,l,t,ll0,ll,lh;
                   8856:   LIST l0,l1;
                   8857:     ND spol,red;
                   8858:     NDV nf,redv;
                   8859:     NM s0,s;
                   8860:     NODE rp0,srp0,nflist,nzlist,nzlist_t;
                   8861:     int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
                   8862:     UINT c;
                   8863:     UINT **spmat;
                   8864:     UINT *s0vect,*svect,*p,*v;
                   8865:     int *colstat;
                   8866:     IndArray *imat;
                   8867:     int *rhead;
                   8868:     int spcol,sprow;
                   8869:     int sugar,sugarh;
                   8870:     PGeoBucket bucket;
                   8871:     struct oEGT eg0,eg1,eg_f4;
                   8872:     Z i1,i2,sugarq;
1.12      noro     8873:
                   8874:     init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1       noro     8875: #if 0
                   8876:     ndv_alloc = 0;
                   8877: #endif
1.11      noro     8878:     Nf4_red=0;
1.1       noro     8879:     g = 0; d = 0;
                   8880:     for ( i = 0; i < nd_psn; i++ ) {
                   8881:         d = update_pairs(d,g,i,0);
                   8882:         g = update_base(g,i);
                   8883:     }
                   8884:   nzlist = 0;
                   8885:     nzlist_t = nd_nzlist;
                   8886:     f4red = 1;
                   8887:     nd_last_nonzero = 0;
                   8888:     while ( d ) {
                   8889:         get_eg(&eg0);
                   8890:         l = nd_minsugarp(d,&d);
                   8891:         sugar = nd_sugarweight?l->sugar2:SG(l);
                   8892:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   8893:         if ( nzlist_t ) {
                   8894:             node = BDY((LIST)BDY(nzlist_t));
1.6       noro     8895:             sugarh = ZTOS((Q)ARG0(node));
1.1       noro     8896:             tn = BDY((LIST)ARG1(node));
                   8897:             if ( !tn ) {
                   8898:               nzlist_t = NEXT(nzlist_t);
                   8899:               continue;
                   8900:             }
                   8901:             /* tn = [[i1,i2],...] */
                   8902:             lh = nd_ipairtospair(tn);
                   8903:         }
                   8904:         bucket = create_pbucket();
                   8905:         stat = nd_sp_f4(m,0,l,bucket);
                   8906:         if ( !stat ) {
                   8907:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8908:             NEXT(t) = d; d = l;
                   8909:             d = nd_reconstruct(0,d);
                   8910:             continue;
                   8911:         }
                   8912:         if ( bucket->m < 0 ) continue;
                   8913:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   8914:         if ( !col ) {
                   8915:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8916:             NEXT(t) = d; d = l;
                   8917:             d = nd_reconstruct(0,d);
                   8918:             continue;
                   8919:         }
1.12      noro     8920:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1       noro     8921:         if ( DP_Print )
1.6       noro     8922:             fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5       noro     8923:                 sugar,eg_f4.exectime);
1.1       noro     8924:         nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
                   8925:         if ( checkonly && nflist ) return 0;
                   8926:         /* adding new bases */
                   8927:         if ( nflist ) nd_last_nonzero = f4red;
                   8928:         for ( r = nflist; r; r = NEXT(r) ) {
                   8929:             nf = (NDV)BDY(r);
1.38      noro     8930:             if ( nd_f4_td ) SG(nf) = nd_tdeg(nf);
1.1       noro     8931:             ndv_removecont(m,nf);
                   8932:             if ( !m && nd_nalg ) {
                   8933:                 ND nf1;
                   8934:
                   8935:                 nf1 = ndvtond(m,nf);
                   8936:                 nd_monic(0,&nf1);
                   8937:                 nd_removecont(m,nf1);
                   8938:                 nf = ndtondv(m,nf1);
                   8939:             }
1.24      noro     8940:             nh = ndv_newps(m,nf,0);
1.1       noro     8941:             d = update_pairs(d,g,nh,0);
                   8942:             g = update_base(g,nh);
                   8943:         }
                   8944:         if ( DP_Print ) {
                   8945:           fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
                   8946:         }
                   8947:         if ( nd_gentrace ) {
                   8948:       for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
                   8949:         NEXTNODE(tn0,tn);
1.6       noro     8950:                 STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1       noro     8951:                 node = mknode(2,i1,i2); MKLIST(l0,node);
                   8952:         BDY(tn) = l0;
                   8953:       }
                   8954:       if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6       noro     8955:             STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1       noro     8956:             MKNODE(node,l1,nzlist); nzlist = node;
                   8957:         }
                   8958:         if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
                   8959:         f4red++;
                   8960:         if ( nd_f4red && f4red > nd_f4red ) break;
                   8961:         if ( nd_rank0 && !nflist ) break;
                   8962:     }
                   8963:     if ( nd_gentrace ) {
                   8964:     MKLIST(l0,reverse_node(nzlist));
                   8965:         MKNODE(nd_alltracelist,l0,0);
                   8966:     }
                   8967: #if 0
                   8968:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   8969: #endif
1.12      noro     8970:   if ( DP_Print ) {
                   8971:     fprintf(asir_out,"number of red=%d,",Nf4_red);
                   8972:     fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
                   8973:       f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
1.41      noro     8974:     fprintf(asir_out,"number of removed pairs=%d\n,",NcriB+NcriMF+Ncri2);
1.12      noro     8975:   }
1.1       noro     8976:   conv_ilist(nd_demand,0,g,indp);
                   8977:     return g;
                   8978: }
                   8979:
                   8980: NODE nd_f4_trace(int m,int **indp)
                   8981: {
                   8982:     int i,nh,stat,index;
                   8983:     NODE r,g;
                   8984:     ND_pairs d,l,l0,t;
                   8985:     ND spol,red;
                   8986:     NDV nf,redv,nfqv,nfv;
                   8987:     NM s0,s;
                   8988:     NODE rp0,srp0,nflist;
                   8989:     int nsp,nred,col,rank,len,k,j,a;
                   8990:     UINT c;
                   8991:     UINT **spmat;
                   8992:     UINT *s0vect,*svect,*p,*v;
                   8993:     int *colstat;
                   8994:     IndArray *imat;
                   8995:     int *rhead;
                   8996:     int spcol,sprow;
                   8997:     int sugar;
                   8998:     PGeoBucket bucket;
                   8999:     struct oEGT eg0,eg1,eg_f4;
                   9000:
                   9001:     g = 0; d = 0;
                   9002:     for ( i = 0; i < nd_psn; i++ ) {
                   9003:         d = update_pairs(d,g,i,0);
                   9004:         g = update_base(g,i);
                   9005:     }
                   9006:     while ( d ) {
                   9007:         get_eg(&eg0);
                   9008:         l = nd_minsugarp(d,&d);
                   9009:         sugar = SG(l);
                   9010:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   9011:         bucket = create_pbucket();
                   9012:         stat = nd_sp_f4(m,0,l,bucket);
                   9013:         if ( !stat ) {
                   9014:             for ( t = l; NEXT(t); t = NEXT(t) );
                   9015:             NEXT(t) = d; d = l;
                   9016:             d = nd_reconstruct(1,d);
                   9017:             continue;
                   9018:         }
                   9019:         if ( bucket->m < 0 ) continue;
                   9020:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   9021:         if ( !col ) {
                   9022:             for ( t = l; NEXT(t); t = NEXT(t) );
                   9023:             NEXT(t) = d; d = l;
                   9024:             d = nd_reconstruct(1,d);
                   9025:             continue;
                   9026:         }
                   9027:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   9028:         if ( DP_Print )
1.3       noro     9029:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5       noro     9030:                 sugar,eg_f4.exectime);
1.1       noro     9031:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   9032:         if ( !l0 ) continue;
                   9033:         l = l0;
                   9034:
                   9035:         /* over Q */
                   9036:         bucket = create_pbucket();
                   9037:         stat = nd_sp_f4(0,1,l,bucket);
                   9038:         if ( !stat ) {
                   9039:             for ( t = l; NEXT(t); t = NEXT(t) );
                   9040:             NEXT(t) = d; d = l;
                   9041:             d = nd_reconstruct(1,d);
                   9042:             continue;
                   9043:         }
                   9044:         if ( bucket->m < 0 ) continue;
                   9045:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   9046:         if ( !col ) {
                   9047:             for ( t = l; NEXT(t); t = NEXT(t) );
                   9048:             NEXT(t) = d; d = l;
                   9049:             d = nd_reconstruct(1,d);
                   9050:             continue;
                   9051:         }
                   9052:         nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   9053:         /* adding new bases */
                   9054:         for ( r = nflist; r; r = NEXT(r) ) {
                   9055:             nfqv = (NDV)BDY(r);
                   9056:             ndv_removecont(0,nfqv);
1.6       noro     9057:             if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1       noro     9058:             if ( nd_nalg ) {
                   9059:                 ND nf1;
                   9060:
                   9061:                 nf1 = ndvtond(m,nfqv);
                   9062:                 nd_monic(0,&nf1);
                   9063:                 nd_removecont(0,nf1);
                   9064:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   9065:             }
                   9066:             nfv = ndv_dup(0,nfqv);
                   9067:             ndv_mod(m,nfv);
                   9068:             ndv_removecont(m,nfv);
1.24      noro     9069:             nh = ndv_newps(0,nfv,nfqv);
1.1       noro     9070:             d = update_pairs(d,g,nh,0);
                   9071:             g = update_base(g,nh);
                   9072:         }
                   9073:     }
                   9074: #if 0
                   9075:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   9076: #endif
                   9077:   conv_ilist(nd_demand,1,g,indp);
                   9078:     return g;
                   9079: }
                   9080:
                   9081: int rref(matrix mat,int *sugar)
                   9082: {
                   9083:   int row,col,i,j,k,l,s,wcol,wj;
                   9084:   unsigned long bj;
                   9085:   unsigned long **a;
                   9086:   unsigned long *ai,*ak,*as,*t;
                   9087:   int *pivot;
                   9088:
                   9089:   row = mat->row;
                   9090:   col = mat->col;
                   9091:   a = mat->a;
                   9092:   wcol = (col+BLEN-1)/BLEN;
                   9093:   pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   9094:   i = 0;
                   9095:   for ( j = 0; j < col; j++ ) {
                   9096:   wj = j/BLEN; bj = 1L<<(j%BLEN);
                   9097:     for ( k = i; k < row; k++ )
                   9098:     if ( a[k][wj] & bj ) break;
                   9099:     if ( k == row ) continue;
                   9100:   pivot[i] = j;
                   9101:     if ( k != i ) {
                   9102:    t = a[i]; a[i] = a[k]; a[k] = t;
                   9103:    s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
                   9104:   }
                   9105:   ai = a[i];
                   9106:     for ( k = i+1; k < row; k++ ) {
                   9107:     ak = a[k];
                   9108:     if ( ak[wj] & bj ) {
                   9109:       for ( l = wj; l < wcol; l++ )
                   9110:       ak[l] ^= ai[l];
                   9111:       sugar[k] = MAX(sugar[k],sugar[i]);
                   9112:     }
                   9113:   }
                   9114:   i++;
                   9115:   }
                   9116:   for ( k = i-1; k >= 0; k-- ) {
                   9117:     j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
                   9118:   ak = a[k];
                   9119:     for ( s = 0; s < k; s++ ) {
                   9120:     as = a[s];
                   9121:       if ( as[wj] & bj ) {
                   9122:         for ( l = wj; l < wcol; l++ )
                   9123:       as[l] ^= ak[l];
                   9124:       sugar[s] = MAX(sugar[s],sugar[k]);
                   9125:     }
                   9126:   }
                   9127:   }
                   9128:   return i;
                   9129: }
                   9130:
                   9131: void print_matrix(matrix mat)
                   9132: {
                   9133:   int row,col,i,j;
                   9134:   unsigned long *ai;
                   9135:
                   9136:   row = mat->row;
                   9137:   col = mat->col;
                   9138:   printf("%d x %d\n",row,col);
                   9139:   for ( i = 0; i < row; i++ ) {
                   9140:   ai = mat->a[i];
                   9141:     for ( j = 0; j < col; j++ ) {
                   9142:     if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
                   9143:     else putchar('0');
                   9144:   }
                   9145:   putchar('\n');
                   9146:   }
                   9147: }
                   9148:
                   9149: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
                   9150:
                   9151: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
                   9152: {
                   9153:   int row,col,wcol,wj,i,j;
                   9154:   unsigned long bj;
                   9155:   unsigned long *ai;
                   9156:   unsigned long **a;
                   9157:   int len;
                   9158:   int *pos;
                   9159:
                   9160:   row = mat->row;
                   9161:   col = mat->col;
                   9162:   wcol = (col+BLEN-1)/BLEN;
                   9163:   pos = (int *)MALLOC(wcol*sizeof(int));
                   9164:   bzero(pos,wcol*sizeof(int));
                   9165:   for ( i = j = 0; i < wcol; i++ )
                   9166:     if ( v[i] ) pos[j++] = i;;
                   9167:   len = j;
                   9168:   wj = rhead/BLEN;
                   9169:   bj = 1L<<rhead%BLEN;
                   9170:   a = mat->a;
                   9171:   for ( i = 0; i < row; i++ ) {
                   9172:   ai = a[i];
                   9173:     if ( ai[wj]&bj ) {
                   9174:     for ( j = 0; j < len; j++ )
                   9175:       ai[pos[j]] ^= v[pos[j]];
                   9176:     sugar[i] = MAX(sugar[i],rsugar);
                   9177:   }
                   9178:   }
                   9179: }
                   9180:
                   9181: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   9182: {
                   9183:     int nsp,nred,i,i0,k,rank,row;
                   9184:     NODE r0,rp;
                   9185:     ND_pairs sp;
                   9186:   ND spol;
                   9187:   NM_ind_pair rt;
                   9188:     int *s0hash;
                   9189:   UINT *s;
                   9190:   int *pivot,*sugar,*head;
                   9191:   matrix mat;
                   9192:     NM m;
                   9193:     NODE r;
                   9194:   struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
                   9195:   int rhead,rsugar,size;
                   9196:     unsigned long *v;
                   9197:
                   9198:     get_eg(&eg0);
                   9199:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   9200:     nred = length(rp0);
                   9201:     mat = alloc_matrix(nsp,col);
                   9202:     s0hash = (int *)MALLOC(col*sizeof(int));
                   9203:     for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
                   9204:         s0hash[i] = ndl_hash_value(s);
                   9205:
                   9206:   sugar = (int *)MALLOC(nsp*sizeof(int));
                   9207:   for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
                   9208:     nd_sp(2,0,sp,&spol);
                   9209:     if ( spol ) {
                   9210:         mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
                   9211:       sugar[i] = SG(spol);
                   9212:       i++;
                   9213:     }
                   9214:   }
                   9215:   mat->row = i;
                   9216:     if ( DP_Print ) {
                   9217:       fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
                   9218:     }
                   9219:   size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
                   9220:   v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
                   9221:     for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
                   9222:     rt = (NM_ind_pair)BDY(rp);
                   9223:     bzero(v,size);
                   9224:         rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
                   9225:     rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
                   9226:       red_by_vect_2(mat,sugar,v,rhead,rsugar);
                   9227:   }
                   9228:
                   9229:     get_eg(&eg1);
                   9230:     init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
                   9231:   rank = rref(mat,sugar);
                   9232:
                   9233:     for ( i = 0, r0 = 0; i < rank; i++ ) {
                   9234:       NEXTNODE(r0,r);
                   9235:     BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
                   9236:       SG((NDV)BDY(r)) = sugar[i];
                   9237:     }
                   9238:     if ( r0 ) NEXT(r) = 0;
                   9239:     get_eg(&eg2);
                   9240:     init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
                   9241:     if ( DP_Print ) {
                   9242:         fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5       noro     9243:       eg_elim1.exectime,eg_elim2.exectime);
1.1       noro     9244:         fflush(asir_out);
                   9245:   }
                   9246:     return r0;
                   9247: }
                   9248:
                   9249:
                   9250: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   9251: {
                   9252:     IndArray *imat;
1.11      noro     9253:     int nsp,nred,i,start;
1.1       noro     9254:     int *rhead;
                   9255:     NODE r0,rp;
                   9256:     ND_pairs sp;
                   9257:     NM_ind_pair *rvect;
                   9258:     UINT *s;
                   9259:     int *s0hash;
1.11      noro     9260:     struct oEGT eg0,eg1,eg_conv;
1.1       noro     9261:
                   9262:     if ( m == 2 && nd_rref2 )
                   9263:      return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
                   9264:
                   9265:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   9266:     nred = length(rp0);
                   9267:     imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   9268:     rhead = (int *)MALLOC(col*sizeof(int));
                   9269:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   9270:
                   9271:     /* construction of index arrays */
1.11      noro     9272:     get_eg(&eg0);
1.1       noro     9273:     if ( DP_Print ) {
1.11      noro     9274:       fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   9275:       fflush(asir_out);
1.1       noro     9276:     }
                   9277:     rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11      noro     9278:     for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1       noro     9279:         rvect[i] = (NM_ind_pair)BDY(rp);
1.11      noro     9280:         imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1       noro     9281:         rhead[imat[i]->head] = 1;
1.11      noro     9282:         start = imat[i]->head;
                   9283:     }
1.12      noro     9284:     get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11      noro     9285:     if ( DP_Print ) {
                   9286:       fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   9287:       fflush(asir_out);
1.1       noro     9288:     }
                   9289:     if ( m > 0 )
1.7       noro     9290: #if SIZEOF_LONG==8
1.1       noro     9291:         r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   9292: #else
                   9293:         r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   9294: #endif
                   9295:     else if ( m == -1 )
                   9296:         r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   9297:     else if ( m == -2 )
                   9298:         r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   9299:     else
                   9300:         r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   9301:     return r0;
                   9302: }
                   9303:
                   9304: /* for Fp, 2<=p<2^16 */
                   9305:
                   9306: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   9307:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   9308: {
                   9309:     int spcol,sprow,a;
                   9310:     int i,j,k,l,rank;
                   9311:     NODE r0,r;
                   9312:     ND_pairs sp;
                   9313:     ND spol;
                   9314:     UINT **spmat;
                   9315:     UINT *svect,*v;
                   9316:     int *colstat;
                   9317:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9318:     int maxrs;
                   9319:     int *spsugar;
                   9320:     ND_pairs *spactive;
                   9321:
                   9322:     spcol = col-nred;
                   9323:     get_eg(&eg0);
                   9324:     /* elimination (1st step) */
                   9325:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   9326:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   9327:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9328:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   9329:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9330:         nd_sp(m,0,sp,&spol);
                   9331:         if ( !spol ) continue;
                   9332:         nd_to_vect(m,s0vect,col,spol,svect);
                   9333:         if ( m == -1 )
                   9334:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   9335:         else
1.40      noro     9336:             maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,0);
1.1       noro     9337:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9338:         if ( i < col ) {
                   9339:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   9340:             for ( j = k = 0; j < col; j++ )
                   9341:                 if ( !rhead[j] ) v[k++] = svect[j];
                   9342:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9343:             if ( nz )
                   9344:             spactive[sprow] = sp;
                   9345:             sprow++;
                   9346:         }
                   9347:         nd_free(spol);
                   9348:     }
                   9349:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9350:     if ( DP_Print ) {
1.5       noro     9351:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9352:         fflush(asir_out);
                   9353:     }
                   9354:     /* free index arrays */
                   9355:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   9356:
                   9357:     /* elimination (2nd step) */
                   9358:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9359:     if ( m == -1 )
                   9360:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   9361:     else
                   9362:         rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   9363:     r0 = 0;
                   9364:     for ( i = 0; i < rank; i++ ) {
                   9365:         NEXTNODE(r0,r); BDY(r) =
                   9366:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   9367:         SG((NDV)BDY(r)) = spsugar[i];
                   9368:         GCFREE(spmat[i]);
                   9369:     }
                   9370:     if ( r0 ) NEXT(r) = 0;
                   9371:
                   9372:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   9373:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9374:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9375:     if ( DP_Print ) {
1.5       noro     9376:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9377:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9378:             nsp,nred,sprow,spcol,rank);
1.5       noro     9379:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9380:     }
                   9381:     if ( nz ) {
                   9382:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   9383:         if ( rank > 0 ) {
                   9384:             NEXT(spactive[rank-1]) = 0;
                   9385:             *nz = spactive[0];
                   9386:         } else
                   9387:             *nz = 0;
                   9388:     }
                   9389:     return r0;
                   9390: }
                   9391:
1.40      noro     9392: NODE nd_f4_red_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   9393:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
                   9394: {
                   9395:     int spcol,sprow,a;
                   9396:     int i,j,k,l,rank;
                   9397:     NODE r0,r;
                   9398:     ND_pairs sp;
                   9399:     ND spol;
                   9400:     UINT **spmat;
                   9401:     UINT *svect,*cvect;
                   9402:     UINT *v;
                   9403:     int *colstat;
                   9404:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9405:     int maxrs;
                   9406:     int *spsugar;
                   9407:     ND_pairs *spactive;
                   9408:     SIG *spsig;
                   9409:
                   9410:     get_eg(&eg0);
                   9411:     /* elimination (1st step) */
                   9412:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   9413:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9414:     spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
                   9415:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9416:         nd_sp(m,0,sp,&spol);
                   9417:         if ( !spol ) {
                   9418:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
                   9419:           continue;
                   9420:         }
                   9421:         svect = (UINT *)MALLOC(col*sizeof(UINT));
                   9422:         nd_to_vect(m,s0vect,col,spol,svect);
                   9423:         maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,spol->sig);
                   9424:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9425:         if ( i < col ) {
                   9426:             spmat[sprow] = svect;
                   9427:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9428:             spsig[sprow] = sp->sig;
                   9429:             sprow++;
                   9430:         } else {
                   9431:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
                   9432:         }
                   9433:         nd_free(spol);
                   9434:     }
                   9435:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
                   9436:     if ( DP_Print ) {
                   9437:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   9438:         fflush(asir_out);
                   9439:     }
                   9440:     /* free index arrays */
                   9441:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   9442:
                   9443:     /* elimination (2nd step) */
                   9444:     colstat = (int *)MALLOC(col*sizeof(int));
                   9445:     rank = nd_gauss_elim_mod_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
                   9446:     r0 = 0;
                   9447:     for ( i = 0; i < sprow; i++ ) {
                   9448:         if ( spsugar[i] >= 0 ) {
                   9449:           NEXTNODE(r0,r);
                   9450:           BDY(r) = vect_to_ndv_s(spmat[i],col,s0vect);
                   9451:           SG((NDV)BDY(r)) = spsugar[i];
                   9452:           ((NDV)BDY(r))->sig = spsig[i];
                   9453:         } else
                   9454:           syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
                   9455:         GCFREE(spmat[i]);
                   9456:     }
                   9457:     if ( r0 ) NEXT(r) = 0;
                   9458:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
                   9459:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9460:     if ( DP_Print ) {
                   9461:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   9462:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9463:             nsp,nred,sprow,col,rank);
                   9464:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   9465:     }
                   9466:     return r0;
                   9467: }
                   9468:
1.1       noro     9469:
                   9470: /* for small finite fields */
                   9471:
                   9472: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   9473:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   9474: {
                   9475:     int spcol,sprow,a;
                   9476:     int i,j,k,l,rank;
                   9477:     NODE r0,r;
                   9478:     ND_pairs sp;
                   9479:     ND spol;
                   9480:     UINT **spmat;
                   9481:     UINT *svect,*v;
                   9482:     int *colstat;
                   9483:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9484:     int maxrs;
                   9485:     int *spsugar;
                   9486:     ND_pairs *spactive;
                   9487:
                   9488:     spcol = col-nred;
                   9489:     get_eg(&eg0);
                   9490:     /* elimination (1st step) */
                   9491:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   9492:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   9493:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9494:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   9495:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9496:         nd_sp(m,0,sp,&spol);
                   9497:         if ( !spol ) continue;
                   9498:         nd_to_vect(m,s0vect,col,spol,svect);
                   9499:         maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   9500:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9501:         if ( i < col ) {
                   9502:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   9503:             for ( j = k = 0; j < col; j++ )
                   9504:                 if ( !rhead[j] ) v[k++] = svect[j];
                   9505:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9506:             if ( nz )
                   9507:             spactive[sprow] = sp;
                   9508:             sprow++;
                   9509:         }
                   9510:         nd_free(spol);
                   9511:     }
                   9512:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9513:     if ( DP_Print ) {
1.5       noro     9514:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9515:         fflush(asir_out);
                   9516:     }
                   9517:     /* free index arrays */
                   9518:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   9519:
                   9520:     /* elimination (2nd step) */
                   9521:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9522:     rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   9523:     r0 = 0;
                   9524:     for ( i = 0; i < rank; i++ ) {
                   9525:         NEXTNODE(r0,r); BDY(r) =
                   9526:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   9527:         SG((NDV)BDY(r)) = spsugar[i];
                   9528:         GCFREE(spmat[i]);
                   9529:     }
                   9530:     if ( r0 ) NEXT(r) = 0;
                   9531:
                   9532:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   9533:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9534:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9535:     if ( DP_Print ) {
1.5       noro     9536:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9537:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9538:             nsp,nred,sprow,spcol,rank);
1.5       noro     9539:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9540:     }
                   9541:     if ( nz ) {
                   9542:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   9543:         if ( rank > 0 ) {
                   9544:             NEXT(spactive[rank-1]) = 0;
                   9545:             *nz = spactive[0];
                   9546:         } else
                   9547:             *nz = 0;
                   9548:     }
                   9549:     return r0;
                   9550: }
                   9551:
                   9552: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   9553:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   9554: {
                   9555:     int spcol,sprow,a;
                   9556:     int i,j,k,l,rank;
                   9557:     NODE r0,r;
                   9558:     ND_pairs sp;
                   9559:     ND spol;
                   9560:     mpz_t **spmat;
                   9561:     mpz_t *svect,*v;
                   9562:     int *colstat;
                   9563:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9564:     int maxrs;
                   9565:     int *spsugar;
                   9566:     pointer *w;
                   9567:
                   9568:     spcol = col-nred;
                   9569:     get_eg(&eg0);
                   9570:     /* elimination (1st step) */
                   9571:     spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
                   9572:     svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   9573:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9574:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9575:         nd_sp(m,trace,sp,&spol);
                   9576:         if ( !spol ) continue;
                   9577:         nd_to_vect_lf(s0vect,col,spol,svect);
                   9578:         maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
                   9579:         for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
                   9580:         if ( i < col ) {
                   9581:             spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
                   9582:             for ( j = k = 0; j < col; j++ )
                   9583:                 if ( !rhead[j] ) v[k++][0] = svect[j][0];
                   9584:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9585:             sprow++;
                   9586:         }
                   9587: /*        nd_free(spol); */
                   9588:     }
                   9589:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9590:     if ( DP_Print ) {
1.5       noro     9591:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9592:         fflush(asir_out);
                   9593:     }
                   9594:     /* free index arrays */
                   9595: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   9596:
                   9597:     /* elimination (2nd step) */
                   9598:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9599:     rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
                   9600:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   9601:     for ( i = 0; i < rank; i++ ) {
                   9602: #if 0
                   9603:         w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   9604:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   9605: #else
                   9606:         w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   9607:         SG((NDV)w[i]) = spsugar[i];
                   9608: #endif
                   9609: /*        GCFREE(spmat[i]); */
                   9610:
                   9611:     }
                   9612: #if 0
                   9613:     qsort(w,rank,sizeof(NDV),
                   9614:         (int (*)(const void *,const void *))ndv_compare);
                   9615: #endif
                   9616:     r0 = 0;
                   9617:     for ( i = 0; i < rank; i++ ) {
                   9618:         NEXTNODE(r0,r); BDY(r) = w[i];
                   9619:     }
                   9620:     if ( r0 ) NEXT(r) = 0;
                   9621:
                   9622: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   9623:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9624:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9625:     if ( DP_Print ) {
1.5       noro     9626:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9627:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9628:             nsp,nred,sprow,spcol,rank);
1.5       noro     9629:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9630:     }
                   9631:     return r0;
                   9632: }
                   9633:
                   9634: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   9635:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   9636: {
                   9637:     int spcol,sprow,a;
                   9638:     int i,j,k,l,rank;
                   9639:     NODE r0,r;
                   9640:     ND_pairs sp;
                   9641:     ND spol;
                   9642:     Z **spmat;
                   9643:     Z *svect,*v;
                   9644:     int *colstat;
                   9645:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9646:     int maxrs;
                   9647:     int *spsugar;
                   9648:     pointer *w;
                   9649:
                   9650:     spcol = col-nred;
                   9651:     get_eg(&eg0);
                   9652:     /* elimination (1st step) */
                   9653:     spmat = (Z **)MALLOC(nsp*sizeof(Q *));
                   9654:     svect = (Z *)MALLOC(col*sizeof(Q));
                   9655:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9656:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9657:         nd_sp(0,trace,sp,&spol);
                   9658:         if ( !spol ) continue;
                   9659:         nd_to_vect_q(s0vect,col,spol,svect);
                   9660:         maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
                   9661:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9662:         if ( i < col ) {
                   9663:             spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
                   9664:             for ( j = k = 0; j < col; j++ )
                   9665:                 if ( !rhead[j] ) v[k++] = svect[j];
                   9666:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9667:             sprow++;
                   9668:         }
                   9669: /*        nd_free(spol); */
                   9670:     }
                   9671:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9672:     if ( DP_Print ) {
1.5       noro     9673:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9674:         fflush(asir_out);
                   9675:     }
                   9676:     /* free index arrays */
                   9677: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   9678:
                   9679:     /* elimination (2nd step) */
                   9680:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9681:     rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
                   9682:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   9683:     for ( i = 0; i < rank; i++ ) {
                   9684: #if 0
                   9685:         w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   9686:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   9687: #else
                   9688:         w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   9689:         SG((NDV)w[i]) = spsugar[i];
                   9690: #endif
                   9691: /*        GCFREE(spmat[i]); */
                   9692:     }
                   9693: #if 0
                   9694:     qsort(w,rank,sizeof(NDV),
                   9695:         (int (*)(const void *,const void *))ndv_compare);
                   9696: #endif
                   9697:     r0 = 0;
                   9698:     for ( i = 0; i < rank; i++ ) {
                   9699:         NEXTNODE(r0,r); BDY(r) = w[i];
                   9700:     }
                   9701:     if ( r0 ) NEXT(r) = 0;
                   9702:
                   9703: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   9704:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9705:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9706:     if ( DP_Print ) {
1.5       noro     9707:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9708:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9709:             nsp,nred,sprow,spcol,rank);
1.5       noro     9710:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9711:     }
                   9712:     return r0;
                   9713: }
                   9714:
                   9715: FILE *nd_write,*nd_read;
                   9716:
                   9717: void nd_send_int(int a) {
                   9718:     write_int(nd_write,(unsigned int *)&a);
                   9719: }
                   9720:
                   9721: void nd_send_intarray(int *p,int len) {
                   9722:     write_intarray(nd_write,(unsigned int *)p,len);
                   9723: }
                   9724:
                   9725: int nd_recv_int() {
                   9726:     int a;
                   9727:
                   9728:     read_int(nd_read,(unsigned int *)&a);
                   9729:     return a;
                   9730: }
                   9731:
                   9732: void nd_recv_intarray(int *p,int len) {
                   9733:     read_intarray(nd_read,(unsigned int *)p,len);
                   9734: }
                   9735:
                   9736: void nd_send_ndv(NDV p) {
                   9737:     int len,i;
                   9738:     NMV m;
                   9739:
                   9740:     if ( !p ) nd_send_int(0);
                   9741:     else {
                   9742:         len = LEN(p);
                   9743:         nd_send_int(len);
                   9744:         m = BDY(p);
                   9745:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9746:             nd_send_int(CM(m));
                   9747:             nd_send_intarray((int *)DL(m),nd_wpd);
                   9748:         }
                   9749:     }
                   9750: }
                   9751:
                   9752: void nd_send_nd(ND p) {
                   9753:     int len,i;
                   9754:     NM m;
                   9755:
                   9756:     if ( !p ) nd_send_int(0);
                   9757:     else {
                   9758:         len = LEN(p);
                   9759:         nd_send_int(len);
                   9760:         m = BDY(p);
                   9761:         for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   9762:             nd_send_int(CM(m));
                   9763:             nd_send_intarray((int *)DL(m),nd_wpd);
                   9764:         }
                   9765:     }
                   9766: }
                   9767:
                   9768: NDV nd_recv_ndv()
                   9769: {
                   9770:     int len,i;
                   9771:     NMV m,m0;
                   9772:     NDV r;
                   9773:
                   9774:     len = nd_recv_int();
                   9775:     if ( !len ) return 0;
                   9776:     else {
                   9777:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   9778: #if 0
                   9779:         ndv_alloc += len*nmv_adv;
                   9780: #endif
                   9781:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9782:             CM(m) = nd_recv_int();
                   9783:             nd_recv_intarray((int *)DL(m),nd_wpd);
                   9784:         }
                   9785:         MKNDV(nd_nvar,m0,len,r);
                   9786:         return r;
                   9787:     }
                   9788: }
                   9789:
                   9790: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
                   9791: {
                   9792:     int i,j,t,c,rank,inv;
                   9793:     int *ci,*ri;
                   9794:     Z dn;
                   9795:     MAT m,nm;
                   9796:
                   9797:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
                   9798:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   9799:     for ( i = 0; i < row; i++ )
                   9800:         for ( j = 0; j < col; j++ )
                   9801:             mat0[i][j] = 0;
                   9802:     c = col-rank;
                   9803:     for ( i = 0; i < rank; i++ ) {
                   9804:         mat0[i][ri[i]] = dn;
                   9805:         for ( j = 0; j < c; j++ )
                   9806:             mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
                   9807:     }
                   9808:     return rank;
                   9809: }
                   9810:
                   9811: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   9812: {
                   9813:     int i,j,k,l,inv,a,rank,s;
                   9814:     unsigned int *t,*pivot,*pk;
                   9815:     unsigned int **mat;
                   9816:     ND_pairs pair;
                   9817:
                   9818:     mat = (unsigned int **)mat0;
                   9819:     for ( rank = 0, j = 0; j < col; j++ ) {
                   9820:         for ( i = rank; i < row; i++ )
                   9821:             mat[i][j] %= md;
                   9822:         for ( i = rank; i < row; i++ )
                   9823:             if ( mat[i][j] )
                   9824:                 break;
                   9825:         if ( i == row ) {
                   9826:             colstat[j] = 0;
                   9827:             continue;
                   9828:         } else
                   9829:             colstat[j] = 1;
                   9830:         if ( i != rank ) {
                   9831:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   9832:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   9833:             if ( spactive ) {
                   9834:                 pair = spactive[i]; spactive[i] = spactive[rank];
                   9835:                 spactive[rank] = pair;
                   9836:             }
                   9837:         }
                   9838:         pivot = mat[rank];
                   9839:         s = sugar[rank];
                   9840:         inv = invm(pivot[j],md);
                   9841:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   9842:             if ( *pk ) {
                   9843:                 if ( *pk >= (unsigned int)md )
                   9844:                     *pk %= md;
                   9845:                 DMAR(*pk,inv,0,md,*pk)
                   9846:             }
                   9847:         for ( i = rank+1; i < row; i++ ) {
                   9848:             t = mat[i];
                   9849:             if ( (a = t[j]) != 0 ) {
                   9850:                 sugar[i] = MAX(sugar[i],s);
                   9851:                 red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   9852:             }
                   9853:         }
                   9854:         rank++;
                   9855:     }
                   9856:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   9857:         if ( colstat[j] ) {
                   9858:             pivot = mat[l];
                   9859:             s = sugar[l];
                   9860:             for ( i = 0; i < l; i++ ) {
                   9861:                 t = mat[i];
                   9862:                 t[j] %= md;
                   9863:                 if ( (a = t[j]) != 0 ) {
                   9864:                     sugar[i] = MAX(sugar[i],s);
                   9865:                     red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   9866:                 }
                   9867:             }
                   9868:             l--;
                   9869:         }
                   9870:     for ( j = 0, l = 0; l < rank; j++ )
                   9871:         if ( colstat[j] ) {
                   9872:             t = mat[l];
                   9873:             for ( k = j; k < col; k++ )
                   9874:                 if ( t[k] >= (unsigned int)md )
                   9875:                     t[k] %= md;
                   9876:             l++;
                   9877:         }
                   9878:     return rank;
                   9879: }
                   9880:
1.40      noro     9881: int nd_gauss_elim_mod_s(UINT **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat,SIG *sig)
                   9882: {
                   9883:   int i,j,k,l,rank,s,imin;
                   9884:   UINT inv;
                   9885:   UINT a;
                   9886:   UINT *t,*pivot,*pk;
                   9887:   UINT *ck;
                   9888:   UINT *ct;
                   9889:   ND_pairs pair;
                   9890:   SIG sg;
                   9891:   int *used;
                   9892:
                   9893:   used = (int *)MALLOC(row*sizeof(int));
                   9894:   for ( j = 0; j < col; j++ ) {
                   9895:     for ( i = 0; i < row; i++ )
                   9896:       a = mat[i][j] %= md;
                   9897:     for ( i = 0; i < row; i++ )
                   9898:       if ( !used[i] && mat[i][j] ) break;
                   9899:     if ( i == row ) {
                   9900:       colstat[j] = 0;
                   9901:       continue;
                   9902:     } else {
                   9903:       colstat[j] = 1;
                   9904:       used[i] = 1;
                   9905:     }
                   9906:     /* column j is normalized */
                   9907:     s = sugar[i];
                   9908:     inv = invm(mat[i][j],md);
                   9909:     /* normalize pivot row */
                   9910:     for ( k = j, pk = mat[i]+j; k < col; k++, pk++, ck++ ) {
                   9911:       DMAR(*pk,inv,0,md,*pk);
                   9912:     }
                   9913:     for ( k = i+1; k < row; k++ ) {
                   9914:       if ( (a = mat[k][j]) != 0 ) {
                   9915:         sugar[k] = MAX(sugar[k],s);
                   9916:         red_by_vect(md,mat[k]+j,mat[i]+j,(int)(md-a),col-j);
                   9917:         Nf4_red++;
                   9918:       }
                   9919:     }
                   9920:   }
                   9921:   rank = 0;
                   9922:   for ( i = 0; i < row; i++ ) {
                   9923:     for ( j = 0; j < col; j++ )
                   9924:       if ( mat[i][j] ) break;
                   9925:     if ( j == col ) sugar[i] = -1;
                   9926:     else rank++;
                   9927:   }
                   9928:   return rank;
                   9929: }
                   9930:
1.1       noro     9931:
1.7       noro     9932: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1       noro     9933: {
1.7       noro     9934:     int i,j,k,l,inv,a,rank,s;
                   9935:     unsigned int *t,*pivot,*pk;
                   9936:     unsigned int **mat;
                   9937:
                   9938:     mat = (unsigned int **)mat0;
                   9939:     for ( rank = 0, j = 0; j < col; j++ ) {
                   9940:         for ( i = rank; i < row; i++ )
                   9941:             if ( mat[i][j] )
                   9942:                 break;
                   9943:         if ( i == row ) {
                   9944:             colstat[j] = 0;
                   9945:             continue;
                   9946:         } else
                   9947:             colstat[j] = 1;
                   9948:         if ( i != rank ) {
                   9949:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   9950:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   9951:         }
                   9952:         pivot = mat[rank];
                   9953:         s = sugar[rank];
                   9954:         inv = _invsf(pivot[j]);
                   9955:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   9956:             if ( *pk )
                   9957:                 *pk = _mulsf(*pk,inv);
                   9958:         for ( i = rank+1; i < row; i++ ) {
                   9959:             t = mat[i];
                   9960:             if ( (a = t[j]) != 0 ) {
                   9961:                 sugar[i] = MAX(sugar[i],s);
                   9962:                 red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   9963:             }
                   9964:         }
                   9965:         rank++;
                   9966:     }
                   9967:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   9968:         if ( colstat[j] ) {
                   9969:             pivot = mat[l];
                   9970:             s = sugar[l];
                   9971:             for ( i = 0; i < l; i++ ) {
                   9972:                 t = mat[i];
                   9973:                 if ( (a = t[j]) != 0 ) {
                   9974:                     sugar[i] = MAX(sugar[i],s);
                   9975:                     red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   9976:                 }
                   9977:             }
                   9978:             l--;
                   9979:         }
                   9980:     return rank;
                   9981: }
1.1       noro     9982:
1.7       noro     9983: int ndv_ishomo(NDV p)
                   9984: {
                   9985:     NMV m;
                   9986:     int len,h;
1.1       noro     9987:
                   9988:     if ( !p ) return 1;
                   9989:     len = LEN(p);
                   9990:     m = BDY(p);
                   9991:     h = TD(DL(m));
                   9992:     NMV_ADV(m);
                   9993:     for ( len--; len; len--, NMV_ADV(m) )
1.20      noro     9994:         if ( TD(DL(m)) != h ) {
                   9995:           return 0;
                   9996:         }
1.1       noro     9997:     return 1;
                   9998: }
                   9999:
                   10000: void ndv_save(NDV p,int index)
                   10001: {
                   10002:     FILE *s;
                   10003:     char name[BUFSIZ];
                   10004:     short id;
                   10005:     int nv,sugar,len,n,i,td,e,j;
                   10006:     NMV m;
                   10007:     unsigned int *dl;
                   10008:     int mpos;
                   10009:
                   10010:     sprintf(name,"%s/%d",Demand,index);
                   10011:     s = fopen(name,"w");
                   10012:     savevl(s,0);
                   10013:     if ( !p ) {
                   10014:         saveobj(s,0);
                   10015:         return;
                   10016:     }
                   10017:     id = O_DP;
                   10018:     nv = NV(p);
                   10019:     sugar = SG(p);
                   10020:     len = LEN(p);
                   10021:     write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
                   10022:     write_int(s,(unsigned int *)&len);
                   10023:
                   10024:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     10025:         saveobj(s,(Obj)CZ(m));
1.1       noro     10026:         dl = DL(m);
                   10027:         td = TD(dl);
                   10028:         write_int(s,(unsigned int *)&td);
                   10029:         for ( j = 0; j < nv; j++ ) {
                   10030:             e = GET_EXP(dl,j);
                   10031:             write_int(s,(unsigned int *)&e);
                   10032:         }
                   10033:         if ( nd_module ) {
                   10034:             mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
                   10035:         }
                   10036:     }
                   10037:     fclose(s);
                   10038: }
                   10039:
                   10040: void nd_save_mod(ND p,int index)
                   10041: {
                   10042:     FILE *s;
                   10043:     char name[BUFSIZ];
                   10044:     int nv,sugar,len,c;
                   10045:     NM m;
                   10046:
                   10047:     sprintf(name,"%s/%d",Demand,index);
                   10048:     s = fopen(name,"w");
                   10049:     if ( !p ) {
                   10050:     len = 0;
                   10051:       write_int(s,(unsigned int *)&len);
                   10052:     fclose(s);
                   10053:         return;
                   10054:     }
                   10055:     nv = NV(p);
                   10056:     sugar = SG(p);
                   10057:     len = LEN(p);
                   10058:     write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
                   10059:   for ( m = BDY(p); m; m = NEXT(m) ) {
                   10060:     c = CM(m); write_int(s,(unsigned int *)&c);
                   10061:     write_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   10062:   }
                   10063:     fclose(s);
                   10064: }
                   10065:
                   10066: NDV ndv_load(int index)
                   10067: {
                   10068:     FILE *s;
                   10069:     char name[BUFSIZ];
                   10070:     short id;
                   10071:     int nv,sugar,len,n,i,td,e,j;
                   10072:     NDV d;
                   10073:     NMV m0,m;
                   10074:     unsigned int *dl;
                   10075:     Obj obj;
                   10076:     int mpos;
                   10077:
                   10078:     sprintf(name,"%s/%d",Demand,index);
                   10079:     s = fopen(name,"r");
                   10080:     if ( !s ) return 0;
                   10081:
                   10082:     skipvl(s);
                   10083:     read_short(s,(unsigned short *)&id);
                   10084:     if ( !id ) return 0;
                   10085:     read_int(s,(unsigned int *)&nv);
                   10086:     read_int(s,(unsigned int *)&sugar);
                   10087:     read_int(s,(unsigned int *)&len);
                   10088:
                   10089:     m0 = m = MALLOC(len*nmv_adv);
                   10090:     for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     10091:         loadobj(s,&obj); CZ(m) = (Z)obj;
1.1       noro     10092:         dl = DL(m);
                   10093:         ndl_zero(dl);
                   10094:         read_int(s,(unsigned int *)&td); TD(dl) = td;
                   10095:         for ( j = 0; j < nv; j++ ) {
                   10096:             read_int(s,(unsigned int *)&e);
                   10097:             PUT_EXP(dl,j,e);
                   10098:         }
                   10099:         if ( nd_module ) {
                   10100:             read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
                   10101:         }
                   10102:         if ( nd_blockmask ) ndl_weight_mask(dl);
                   10103:     }
                   10104:     fclose(s);
                   10105:     MKNDV(nv,m0,len,d);
                   10106:     SG(d) = sugar;
                   10107:     return d;
                   10108: }
                   10109:
                   10110: ND nd_load_mod(int index)
                   10111: {
                   10112:     FILE *s;
                   10113:     char name[BUFSIZ];
                   10114:     int nv,sugar,len,i,c;
                   10115:   ND d;
                   10116:     NM m0,m;
                   10117:
                   10118:     sprintf(name,"%s/%d",Demand,index);
                   10119:     s = fopen(name,"r");
                   10120:   /* if the file does not exist, it means p[index]=0 */
                   10121:     if ( !s ) return 0;
                   10122:
                   10123:     read_int(s,(unsigned int *)&nv);
                   10124:   if ( !nv ) { fclose(s); return 0; }
                   10125:
                   10126:     read_int(s,(unsigned int *)&sugar);
                   10127:     read_int(s,(unsigned int *)&len);
                   10128:   for ( m0 = 0, i = 0; i < len; i++ ) {
                   10129:     NEXTNM(m0,m);
                   10130:     read_int(s,(unsigned int *)&c); CM(m) = c;
                   10131:     read_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   10132:   }
                   10133:   NEXT(m) = 0;
                   10134:     MKND(nv,m0,len,d);
                   10135:     SG(d) = sugar;
                   10136:   fclose(s);
                   10137:     return d;
                   10138: }
                   10139:
                   10140: void nd_det(int mod,MAT f,P *rp)
                   10141: {
                   10142:     VL fv,tv;
                   10143:     int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
                   10144:     pointer **m;
                   10145:     P **w;
                   10146:     P mp,r;
                   10147:     NDV **dm;
                   10148:     NDV *t,*mi,*mj;
                   10149:     NDV d,s,mij,mjj;
                   10150:     ND u;
                   10151:     NMV nmv;
                   10152:     UINT *bound;
                   10153:     PGeoBucket bucket;
                   10154:     struct order_spec *ord;
                   10155:     Z dq,dt,ds;
                   10156:     Z mone;
                   10157:     Z gn,qn,dn0,nm,dn;
                   10158:
                   10159:     create_order_spec(0,0,&ord);
                   10160:     nd_init_ord(ord);
                   10161:     get_vars((Obj)f,&fv);
                   10162:     if ( f->row != f->col )
                   10163:         error("nd_det : non-square matrix");
                   10164:     n = f->row;
                   10165:     m = f->body;
                   10166:     for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   10167:
                   10168:     if ( !nvar ) {
                   10169:         if ( !mod )
                   10170:             detp(CO,(P **)m,n,rp);
                   10171:         else {
                   10172:             w = (P **)almat_pointer(n,n);
                   10173:             for ( i = 0; i < n; i++ )
                   10174:                 for ( j = 0; j < n; j++ )
                   10175:                     ptomp(mod,(P)m[i][j],&w[i][j]);
                   10176:             detmp(CO,mod,w,n,&mp);
                   10177:             mptop(mp,rp);
                   10178:         }
                   10179:         return;
                   10180:     }
                   10181:
                   10182:     if ( !mod ) {
                   10183:         w = (P **)almat_pointer(n,n);
                   10184:         dq = ONE;
                   10185:         for ( i = 0; i < n; i++ ) {
                   10186:             dn0 = ONE;
                   10187:             for ( j = 0; j < n; j++ ) {
                   10188:                 if ( !m[i][j] ) continue;
                   10189:                 lgp(m[i][j],&nm,&dn);
1.6       noro     10190:                 gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1       noro     10191:             }
                   10192:             if ( !UNIZ(dn0) ) {
                   10193:                 ds = dn0;
                   10194:                 for ( j = 0; j < n; j++ )
                   10195:                     mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   10196:                 mulz(dq,ds,&dt); dq = dt;
                   10197:             } else
                   10198:                 for ( j = 0; j < n; j++ )
                   10199:                     w[i][j] = (P)m[i][j];
                   10200:         }
                   10201:         m = (pointer **)w;
                   10202:     }
                   10203:
                   10204:     for ( i = 0, max = 1; i < n; i++ )
                   10205:         for ( j = 0; j < n; j++ )
                   10206:             for ( tv = fv; tv; tv = NEXT(tv) ) {
                   10207:                 e = getdeg(tv->v,(P)m[i][j]);
                   10208:                 max = MAX(e,max);
                   10209:             }
                   10210:     nd_setup_parameters(nvar,max);
                   10211:     dm = (NDV **)almat_pointer(n,n);
                   10212:     for ( i = 0, max = 1; i < n; i++ )
                   10213:         for ( j = 0; j < n; j++ ) {
                   10214:             dm[i][j] = ptondv(CO,fv,m[i][j]);
                   10215:             if ( mod ) ndv_mod(mod,dm[i][j]);
                   10216:             if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   10217:         }
                   10218:     d = ptondv(CO,fv,(P)ONE);
                   10219:     if ( mod ) ndv_mod(mod,d);
                   10220:     chsgnz(ONE,&mone);
                   10221:     for ( j = 0, sgn = 1; j < n; j++ ) {
                   10222:       if ( DP_Print ) {
                   10223:         fprintf(asir_out,".");
                   10224:       }
                   10225:         for ( i = j; i < n && !dm[i][j]; i++ );
                   10226:         if ( i == n ) {
                   10227:             *rp = 0;
                   10228:             return;
                   10229:         }
                   10230:         k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
                   10231:         for ( k = j; k < n; k++ )
                   10232:             for ( l = j; l < n; l++ )
                   10233:                 if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   10234:                     k0 = k; l0 = l; len0 = LEN(dm[k][l]);
                   10235:                 }
                   10236:         if ( k0 != j ) {
                   10237:             t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   10238:             sgn = -sgn;
                   10239:         }
                   10240:         if ( l0 != j ) {
                   10241:             for ( k = j; k < n; k++ ) {
                   10242:                 s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   10243:             }
                   10244:             sgn = -sgn;
                   10245:         }
                   10246:         bound = nd_det_compute_bound(dm,n,j);
                   10247:         for ( k = 0; k < nd_nvar; k++ )
                   10248:             if ( bound[k]*2 > nd_mask0 ) break;
                   10249:         if ( k < nd_nvar )
                   10250:             nd_det_reconstruct(dm,n,j,d);
                   10251:
                   10252:         for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
                   10253: /*            if ( DP_Print ) fprintf(asir_out,"    i=%d\n        ",i); */
                   10254:             mi = dm[i]; mij = mi[j];
                   10255:             if ( mod )
                   10256:                 ndv_mul_c(mod,mij,mod-1);
                   10257:             else
                   10258:                 ndv_mul_c_q(mij,mone);
                   10259:             for ( k = j+1; k < n; k++ ) {
                   10260: /*                if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
                   10261:                 bucket = create_pbucket();
                   10262:                 if ( mi[k] ) {
                   10263:                     nmv = BDY(mjj); len = LEN(mjj);
                   10264:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   10265:                         u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   10266:                         add_pbucket(mod,bucket,u);
                   10267:                     }
                   10268:                 }
                   10269:                 if ( mj[k] && mij ) {
                   10270:                     nmv = BDY(mij); len = LEN(mij);
                   10271:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   10272:                         u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   10273:                         add_pbucket(mod,bucket,u);
                   10274:                     }
                   10275:                 }
                   10276:                 u = nd_quo(mod,bucket,d);
                   10277:                 mi[k] = ndtondv(mod,u);
                   10278:             }
                   10279: /*            if ( DP_Print ) fprintf(asir_out,"\n",k); */
                   10280:         }
                   10281:         d = mjj;
                   10282:     }
                   10283:     if ( DP_Print ) {
                   10284:       fprintf(asir_out,"\n");
                   10285:     }
                   10286:     if ( sgn < 0 ) {
                   10287:         if ( mod )
                   10288:             ndv_mul_c(mod,d,mod-1);
                   10289:         else
                   10290:             ndv_mul_c_q(d,mone);
                   10291:     }
                   10292:     r = ndvtop(mod,CO,fv,d);
                   10293:     if ( !mod && !UNIQ(dq) )
                   10294:         divsp(CO,r,(P)dq,rp);
                   10295:     else
                   10296:         *rp = r;
                   10297: }
                   10298:
                   10299: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
                   10300: {
                   10301:     NM mr,mr0;
                   10302:     NM tnm;
                   10303:     NMV m;
                   10304:     UINT *d0,*dt,*dm;
                   10305:     int c,n,td,i,c1,c2,len;
                   10306:     Z q;
                   10307:     ND r;
                   10308:
                   10309:     if ( !p ) return 0;
                   10310:     else {
                   10311:         n = NV(p); m = BDY(p); len = LEN(p);
                   10312:         d0 = DL(m0);
                   10313:         td = TD(d);
                   10314:         mr0 = 0;
                   10315:         NEWNM(tnm);
                   10316:         if ( mod ) {
                   10317:             c = CM(m0);
                   10318:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   10319:                 ndl_add(DL(m),d0,DL(tnm));
                   10320:                 if ( ndl_reducible(DL(tnm),d) ) {
                   10321:                     NEXTNM(mr0,mr);
                   10322:                     c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
                   10323:                     ndl_copy(DL(tnm),DL(mr));
                   10324:                 }
                   10325:             }
                   10326:         } else {
1.6       noro     10327:             q = CZ(m0);
1.1       noro     10328:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   10329:                 ndl_add(DL(m),d0,DL(tnm));
                   10330:                 if ( ndl_reducible(DL(tnm),d) ) {
                   10331:                     NEXTNM(mr0,mr);
1.6       noro     10332:                     mulz(CZ(m),q,&CZ(mr));
1.1       noro     10333:                     ndl_copy(DL(tnm),DL(mr));
                   10334:                 }
                   10335:             }
                   10336:         }
                   10337:         if ( !mr0 )
                   10338:             return 0;
                   10339:         else {
                   10340:             NEXT(mr) = 0;
                   10341:             for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   10342:             MKND(NV(p),mr0,len,r);
                   10343:             SG(r) = SG(p) + TD(d0);
                   10344:             return r;
                   10345:         }
                   10346:     }
                   10347: }
                   10348:
                   10349: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   10350: {
                   10351:     int i,obpe,oadv,h,k,l;
                   10352:     static NM prev_nm_free_list;
                   10353:     EPOS oepos;
                   10354:
                   10355:     obpe = nd_bpe;
                   10356:     oadv = nmv_adv;
                   10357:     oepos = nd_epos;
                   10358:     if ( obpe < 2 ) nd_bpe = 2;
                   10359:     else if ( obpe < 3 ) nd_bpe = 3;
                   10360:     else if ( obpe < 4 ) nd_bpe = 4;
                   10361:     else if ( obpe < 5 ) nd_bpe = 5;
                   10362:     else if ( obpe < 6 ) nd_bpe = 6;
                   10363:     else if ( obpe < 8 ) nd_bpe = 8;
                   10364:     else if ( obpe < 10 ) nd_bpe = 10;
                   10365:     else if ( obpe < 16 ) nd_bpe = 16;
                   10366:     else if ( obpe < 32 ) nd_bpe = 32;
                   10367:     else error("nd_det_reconstruct : exponent too large");
                   10368:
                   10369:     nd_setup_parameters(nd_nvar,0);
                   10370:     prev_nm_free_list = _nm_free_list;
                   10371:     _nm_free_list = 0;
                   10372:     for ( k = j; k < n; k++ )
                   10373:         for (l = j; l < n; l++ )
                   10374:             ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   10375:     ndv_realloc(d,obpe,oadv,oepos);
                   10376:     prev_nm_free_list = 0;
                   10377: #if 0
                   10378:     GC_gcollect();
                   10379: #endif
                   10380: }
                   10381:
                   10382: /* returns a UINT array containing degree bounds */
                   10383:
                   10384: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   10385: {
                   10386:     UINT *d0,*d1,*d,*t,*r;
                   10387:     int k,l,i;
                   10388:
                   10389:     d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
                   10390:     for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
                   10391:     for ( k = j; k < n; k++ )
                   10392:         for ( l = j; l < n; l++ )
                   10393:             if ( dm[k][l] ) {
                   10394:                 d = ndv_compute_bound(dm[k][l]);
                   10395:                 for ( i = 0; i < nd_nvar; i++ )
                   10396:                     d0[i] = MAX(d0[i],d[i]);
                   10397:             }
                   10398:     return d0;
                   10399: }
                   10400:
                   10401: DL nd_separate_d(UINT *d,UINT *trans)
                   10402: {
                   10403:     int n,td,i,e,j;
                   10404:     DL a;
                   10405:
                   10406:     ndl_zero(trans);
                   10407:     td = 0;
                   10408:     for ( i = 0; i < nd_ntrans; i++ ) {
                   10409:         e = GET_EXP(d,i);
                   10410:         PUT_EXP(trans,i,e);
                   10411:         td += MUL_WEIGHT(e,i);
                   10412:     }
                   10413:     if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   10414:         /* homogenized */
                   10415:         i = nd_nvar-1;
                   10416:         e = GET_EXP(d,i);
                   10417:         PUT_EXP(trans,i,e);
                   10418:         td += MUL_WEIGHT(e,i);
                   10419:     }
                   10420:     TD(trans) = td;
                   10421:     if ( nd_blockmask) ndl_weight_mask(trans);
                   10422:     NEWDL(a,nd_nalg);
                   10423:     td = 0;
                   10424:     for ( i = 0; i < nd_nalg; i++ ) {
                   10425:         j = nd_ntrans+i;
                   10426:         e = GET_EXP(d,j);
                   10427:         a->d[i] = e;
                   10428:         td += e;
                   10429:     }
                   10430:     a->td = td;
                   10431:     return a;
                   10432: }
                   10433:
                   10434: int nd_monic(int mod,ND *p)
                   10435: {
                   10436:     UINT *trans,*t;
                   10437:     DL alg;
                   10438:     MP mp0,mp;
                   10439:     NM m,m0,m1,ma0,ma,mb,mr0,mr;
                   10440:     ND r;
                   10441:     DL dl;
                   10442:     DP nm;
                   10443:     NDV ndv;
                   10444:     DAlg inv,cd;
                   10445:     ND s,c;
                   10446:     Z l,mul;
                   10447:     Z ln;
                   10448:     int n,ntrans,i,e,td,is_lc,len;
                   10449:     NumberField nf;
                   10450:     struct oEGT eg0,eg1;
                   10451:
                   10452:     if ( !(nf = get_numberfield()) )
                   10453:         error("nd_monic : current_numberfield is not set");
                   10454:
                   10455:     /* Q coef -> DAlg coef */
                   10456:     NEWNM(ma0); ma = ma0;
                   10457:     m = BDY(*p);
                   10458:     is_lc = 1;
                   10459:     while ( 1 ) {
                   10460:         NEWMP(mp0); mp = mp0;
1.6       noro     10461:         mp->c = (Obj)CZ(m);
1.1       noro     10462:         mp->dl = nd_separate_d(DL(m),DL(ma));
                   10463:         NEWNM(mb);
                   10464:         for ( m = NEXT(m); m; m = NEXT(m) ) {
                   10465:             alg = nd_separate_d(DL(m),DL(mb));
                   10466:             if ( !ndl_equal(DL(ma),DL(mb)) )
                   10467:                 break;
1.6       noro     10468:             NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1       noro     10469:         }
                   10470:         NEXT(mp) = 0;
                   10471:         MKDP(nd_nalg,mp0,nm);
                   10472:         MKDAlg(nm,ONE,cd);
                   10473:         if ( is_lc == 1 ) {
                   10474:             /* if the lc is a rational number, we have nothing to do */
                   10475:             if ( !mp0->dl->td )
                   10476:                 return 1;
                   10477:
                   10478:             get_eg(&eg0);
                   10479:             invdalg(cd,&inv);
                   10480:             get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   10481:             /* check the validity of inv */
                   10482:             if ( mod && !remqi((Q)inv->dn,mod) )
                   10483:                 return 0;
                   10484:             CA(ma) = nf->one;
                   10485:             is_lc = 0;
                   10486:             ln = ONE;
                   10487:         } else {
                   10488:             muldalg(cd,inv,&CA(ma));
                   10489:             lcmz(ln,CA(ma)->dn,&ln);
                   10490:         }
                   10491:         if ( m ) {
                   10492:             NEXT(ma) = mb; ma = mb;
                   10493:         } else {
                   10494:             NEXT(ma) = 0;
                   10495:             break;
                   10496:         }
                   10497:     }
                   10498:     /* l = lcm(denoms) */
                   10499:     l = ln;
                   10500:     for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6       noro     10501:         divsz(l,CA(m)->dn,&mul);
1.1       noro     10502:         for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   10503:             NEXTNM(mr0,mr);
1.6       noro     10504:             mulz((Z)mp->c,mul,&CZ(mr));
1.1       noro     10505:             dl = mp->dl;
                   10506:             td = TD(DL(m));
                   10507:             ndl_copy(DL(m),DL(mr));
                   10508:             for ( i = 0; i < nd_nalg; i++ ) {
                   10509:                 e = dl->d[i];
                   10510:                 PUT_EXP(DL(mr),i+nd_ntrans,e);
                   10511:                 td += MUL_WEIGHT(e,i+nd_ntrans);
                   10512:             }
                   10513:             if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
                   10514:             TD(DL(mr)) = td;
                   10515:             if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   10516:         }
                   10517:     }
                   10518:     NEXT(mr) = 0;
                   10519:     for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   10520:     MKND(NV(*p),mr0,len,r);
                   10521:     /* XXX */
                   10522:     SG(r) = SG(*p);
                   10523:     nd_free(*p);
                   10524:     *p = r;
                   10525:     return 1;
                   10526: }
                   10527:
                   10528: NODE reverse_node(NODE n)
                   10529: {
                   10530:     NODE t,t1;
                   10531:
                   10532:     for ( t = 0; n; n = NEXT(n) ) {
                   10533:         MKNODE(t1,BDY(n),t); t = t1;
                   10534:     }
                   10535:     return t;
                   10536: }
                   10537:
                   10538: P ndc_div(int mod,union oNDC a,union oNDC b)
                   10539: {
                   10540:     union oNDC c;
                   10541:     int inv,t;
                   10542:
                   10543:     if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10      noro     10544:     else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1       noro     10545:     else if ( mod ) {
                   10546:         inv = invm(b.m,mod);
                   10547:         DMAR(a.m,inv,0,mod,t); c.m = t;
                   10548:     } else if ( nd_vc )
                   10549:        divsp(nd_vc,a.p,b.p,&c.p);
                   10550:     else
                   10551:        divsz(a.z,b.z,&c.z);
                   10552:     return ndctop(mod,c);
                   10553: }
                   10554:
                   10555: P ndctop(int mod,union oNDC c)
                   10556: {
                   10557:     Z q;
                   10558:     int e;
                   10559:     GFS gfs;
                   10560:
                   10561:     if ( mod == -1 ) {
                   10562:         e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
                   10563:     } else if ( mod == -2 ) {
1.10      noro     10564:        q = c.z; return (P)q;
1.1       noro     10565:     } else if ( mod > 0 ) {
1.6       noro     10566:         STOZ(c.m,q); return (P)q;
1.1       noro     10567:     } else
                   10568:         return (P)c.p;
                   10569: }
                   10570:
                   10571: /* [0,0,0,cont] = p -> p/cont */
                   10572:
                   10573: void finalize_tracelist(int i,P cont)
                   10574: {
                   10575:  LIST l;
                   10576:  NODE node;
                   10577:  Z iq;
                   10578:
                   10579:   if ( !UNIQ(cont) ) {
                   10580:     node = mknode(4,NULLP,NULLP,NULLP,cont);
                   10581:     MKLIST(l,node); MKNODE(node,l,nd_tracelist);
                   10582:     nd_tracelist = node;
                   10583:   }
1.6       noro     10584:   STOZ(i,iq);
1.1       noro     10585:   nd_tracelist = reverse_node(nd_tracelist);
                   10586:   MKLIST(l,nd_tracelist);
                   10587:   node = mknode(2,iq,l); MKLIST(l,node);
                   10588:   MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
                   10589:   nd_alltracelist = node; nd_tracelist = 0;
                   10590: }
                   10591:
                   10592: void conv_ilist(int demand,int trace,NODE g,int **indp)
                   10593: {
                   10594:     int n,i,j;
                   10595:   int *ind;
                   10596:   NODE t;
                   10597:
                   10598:     n = length(g);
                   10599:   ind = (int *)MALLOC(n*sizeof(int));
                   10600:   for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
                   10601:     j = (long)BDY(t); ind[i] = j;
                   10602:     BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
                   10603:   }
                   10604:   if ( indp ) *indp = ind;
                   10605: }
                   10606:
1.30      noro     10607: NODE conv_ilist_s(int demand,int trace,int **indp)
                   10608: {
                   10609:   int n,i,j;
                   10610:   int *ind;
                   10611:   NODE g0,g;
                   10612:
                   10613:   n = nd_psn;
                   10614:   ind = (int *)MALLOC(n*sizeof(int));
                   10615:   g0 = 0;
                   10616:   for ( i = 0; i < n; i++ ) {
                   10617:     ind[i] = i;
                   10618:     NEXTNODE(g0,g);
                   10619:     BDY(g) = (pointer)(demand?ndv_load(i):(trace?nd_ps_trace[i]:nd_ps[i]));
                   10620:   }
                   10621:   if ( g0 ) NEXT(g) = 0;
                   10622:   if ( indp ) *indp = ind;
                   10623:   return g0;
                   10624: }
                   10625:
1.46      noro     10626: void parse_nd_option(VL vl,NODE opt)
1.1       noro     10627: {
1.38      noro     10628:   NODE t,p,u;
1.1       noro     10629:   int i,s,n;
1.38      noro     10630:   char *key;
                   10631:   Obj value;
1.46      noro     10632:   VL oldvl;
1.1       noro     10633:
1.41      noro     10634:   nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_norb = 0; nd_gbblock = 0;
1.1       noro     10635:   nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
                   10636:   nd_splist = 0; nd_check_splist = 0;
1.38      noro     10637:   nd_sugarweight = 0; nd_f4red =0; nd_rank0 = 0;
                   10638:   nd_f4_td = 0; nd_sba_f4step = 2; nd_sba_pot = 0; nd_sba_largelcm = 0;
1.41      noro     10639:   nd_sba_dontsort = 0; nd_top = 0; nd_sba_redundant_check = 0;
1.46      noro     10640:   nd_sba_syz = 0; nd_sba_modord = 0; nd_sba_inputisgb = 0;
1.49      noro     10641:   nd_hpdata = 0;
1.39      noro     10642:
1.38      noro     10643:   for ( t = opt; t; t = NEXT(t) ) {
                   10644:     p = BDY((LIST)BDY(t));
                   10645:     key = BDY((STRING)BDY(p));
                   10646:     value = (Obj)BDY(NEXT(p));
                   10647:     if ( !strcmp(key,"gentrace") )
                   10648:       nd_gentrace = value?1:0;
                   10649:     else if ( !strcmp(key,"gensyz") )
                   10650:       nd_gensyz = value?1:0;
                   10651:     else if ( !strcmp(key,"nora") )
                   10652:       nd_nora = value?1:0;
1.41      noro     10653:     else if ( !strcmp(key,"norb") )
                   10654:       nd_norb = value?1:0;
1.38      noro     10655:     else if ( !strcmp(key,"gbblock") ) {
                   10656:       if ( value && OID(value) == O_LIST ) {
1.1       noro     10657:         u = BDY((LIST)value);
1.38      noro     10658:         nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.1       noro     10659:         for ( i = 0; u; u = NEXT(u) ) {
                   10660:           p = BDY((LIST)BDY(u));
1.6       noro     10661:           s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
                   10662:           nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1       noro     10663:         }
                   10664:         nd_gbblock[i] = -1;
1.38      noro     10665:       } else
                   10666:         nd_gbblock = 0;
1.1       noro     10667:     } else if ( !strcmp(key,"newelim") )
                   10668:             nd_newelim = value?1:0;
                   10669:     else if ( !strcmp(key,"intersect") )
                   10670:             nd_intersect = value?1:0;
1.17      noro     10671:     else if ( !strcmp(key,"syzgen") )
                   10672:             nd_intersect = ZTOS((Q)value);
1.1       noro     10673:     else if ( !strcmp(key,"lf") )
                   10674:             nd_lf = value?1:0;
                   10675:     else if ( !strcmp(key,"trace") ) {
1.38      noro     10676:       if ( value ) {
                   10677:         u = BDY((LIST)value);
                   10678:         nd_nzlist = BDY((LIST)ARG2(u));
                   10679:         nd_bpe = ZTOS((Q)ARG3(u));
                   10680:       }
1.1       noro     10681:     } else if ( !strcmp(key,"f4red") ) {
1.38      noro     10682:       nd_f4red = ZTOS((Q)value);
1.1       noro     10683:     } else if ( !strcmp(key,"rank0") ) {
1.38      noro     10684:       nd_rank0 = value?1:0;
1.1       noro     10685:     } else if ( !strcmp(key,"splist") ) {
1.38      noro     10686:       nd_splist = value?1:0;
1.1       noro     10687:     } else if ( !strcmp(key,"check_splist") ) {
                   10688:       nd_check_splist = BDY((LIST)value);
1.49      noro     10689:     } else if ( !strcmp(key,"hpdata") ) {
1.50      noro     10690:       if ( value )
                   10691:         nd_hpdata = BDY((LIST)value);
1.1       noro     10692:     } else if ( !strcmp(key,"sugarweight") ) {
                   10693:       u = BDY((LIST)value);
1.38      noro     10694:       n = length(u);
                   10695:       nd_sugarweight = MALLOC(n*sizeof(int));
1.1       noro     10696:       for ( i = 0; i < n; i++, u = NEXT(u) )
1.38      noro     10697:         nd_sugarweight[i] = ZTOS((Q)BDY(u));
                   10698:     } else if ( !strcmp(key,"f4_td") ) {
                   10699:       nd_f4_td = value?1:0;
                   10700:     } else if ( !strcmp(key,"sba_f4step") ) {
                   10701:       nd_sba_f4step = value?ZTOS((Q)value):0;
                   10702:     } else if ( !strcmp(key,"sba_pot") ) {
1.44      noro     10703:       nd_sba_pot = ZTOS((Q)value);
1.38      noro     10704:     } else if ( !strcmp(key,"sba_largelcm") ) {
                   10705:       nd_sba_largelcm = value?1:0;
1.39      noro     10706:     } else if ( !strcmp(key,"sba_dontsort") ) {
                   10707:       nd_sba_dontsort = value?1:0;
1.44      noro     10708:     } else if ( !strcmp(key,"sba_syz") ) {
                   10709:       nd_sba_syz = value?1:0;
1.46      noro     10710:     } else if ( !strcmp(key,"sba_modord") ) {
                   10711:       // value=[vlist,ordspec,weight]
                   10712:       u = BDY((LIST)value);
                   10713:       pltovl((LIST)ARG0(u),&oldvl);
                   10714:       nd_sba_modord = create_comp_sig_spec(vl,oldvl,(Obj)ARG1(u),argc(u)==3?ARG2(u):0);
                   10715:     } else if ( !strcmp(key,"sba_gbinput") ) {
                   10716:       nd_sba_inputisgb = value?1:0;
                   10717:       if ( nd_sba_inputisgb != 0 ) {
                   10718:         // value=[vlist,ordspec,weight]
                   10719:         u = BDY((LIST)value);
                   10720:         pltovl((LIST)ARG0(u),&oldvl);
                   10721:         nd_sba_modord = create_comp_sig_spec(vl,oldvl,(Obj)ARG1(u),argc(u)==3?ARG2(u):0);
                   10722:       }
1.41      noro     10723:     } else if ( !strcmp(key,"sba_redundant_check") ) {
                   10724:       nd_sba_redundant_check = value?1:0;
1.39      noro     10725:     } else if ( !strcmp(key,"top") ) {
                   10726:       nd_top = value?1:0;
1.1       noro     10727:     }
1.38      noro     10728:   }
1.44      noro     10729:   if ( nd_sba_syz ) nd_sba_dontsort = 1;
1.1       noro     10730: }
                   10731:
                   10732: ND mdptond(DP d);
                   10733: ND nd_mul_nm(int mod,NM m0,ND p);
                   10734: ND nd_mul_nm_lf(NM m0,ND p);
                   10735: ND *btog(NODE ti,ND **p,int nb,int mod);
                   10736: ND btog_one(NODE ti,ND *p,int nb,int mod);
                   10737: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
                   10738: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
                   10739:
                   10740: /* d:monomial */
                   10741: ND mdptond(DP d)
                   10742: {
                   10743:   NM m;
                   10744:   ND r;
                   10745:
                   10746:   if ( OID(d) == 1 )
                   10747:   r = ptond(CO,CO,(P)d);
                   10748:   else {
                   10749:     NEWNM(m);
                   10750:     dltondl(NV(d),BDY(d)->dl,DL(m));
1.6       noro     10751:     CZ(m) = (Z)BDY(d)->c;
1.1       noro     10752:     NEXT(m) = 0;
                   10753:     MKND(NV(d),m,1,r);
                   10754:   }
                   10755:     return r;
                   10756: }
                   10757:
                   10758: ND nd_mul_nm(int mod,NM m0,ND p)
                   10759: {
                   10760:   UINT *d0;
                   10761:   int c0,c1,c;
                   10762:   NM tm,mr,mr0;
                   10763:   ND r;
                   10764:
                   10765:   if ( !p ) return 0;
                   10766:   d0 = DL(m0);
                   10767:   c0 = CM(m0);
                   10768:   mr0 = 0;
                   10769:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   10770:     NEXTNM(mr0,mr);
                   10771:   c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
                   10772:   ndl_add(d0,DL(tm),DL(mr));
                   10773:   }
                   10774:   NEXT(mr) = 0;
                   10775:   MKND(NV(p),mr0,LEN(p),r);
                   10776:   return r;
                   10777: }
                   10778:
                   10779: ND nd_mul_nm_lf(NM m0,ND p)
                   10780: {
                   10781:   UINT *d0;
                   10782:   Z c0,c1,c;
                   10783:   NM tm,mr,mr0;
                   10784:   ND r;
                   10785:
                   10786:   if ( !p ) return 0;
                   10787:   d0 = DL(m0);
                   10788:   c0 = CZ(m0);
                   10789:   mr0 = 0;
                   10790:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   10791:     NEXTNM(mr0,mr);
                   10792:     c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
                   10793:     ndl_add(d0,DL(tm),DL(mr));
                   10794:   }
                   10795:   NEXT(mr) = 0;
                   10796:   MKND(NV(p),mr0,LEN(p),r);
                   10797:   return r;
                   10798: }
                   10799:
                   10800: ND *btog(NODE ti,ND **p,int nb,int mod)
                   10801: {
                   10802:   PGeoBucket *r;
                   10803:   int i,ci;
                   10804:   NODE t,s;
                   10805:   ND m,tp;
                   10806:   ND *pi,*rd;
                   10807:   P c;
                   10808:
                   10809:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   10810:   for ( i = 0; i < nb; i++ )
                   10811:     r[i] = create_pbucket();
                   10812:   for ( t = ti; t; t = NEXT(t) ) {
                   10813:   s = BDY((LIST)BDY(t));
                   10814:     if ( ARG0(s) ) {
                   10815:     m = mdptond((DP)ARG2(s));
1.6       noro     10816:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     10817:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   10818:       HCM(m) = ci;
1.6       noro     10819:       pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     10820:       for ( i = 0; i < nb; i++ ) {
                   10821:       tp = nd_mul_nm(mod,BDY(m),pi[i]);
                   10822:         add_pbucket(mod,r[i],tp);
                   10823:       }
                   10824:     }
                   10825:     ci = 1;
                   10826:     } else {
                   10827:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   10828:     ci = invm(ci,mod);
                   10829:   }
                   10830:   }
                   10831:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   10832:   for ( i = 0; i < nb; i++ )
                   10833:   rd[i] = normalize_pbucket(mod,r[i]);
                   10834:   if ( ci != 1 )
                   10835:     for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
                   10836:    return rd;
                   10837: }
                   10838:
                   10839: /* YYY */
                   10840: ND *btog_lf(NODE ti,ND **p,int nb)
                   10841: {
                   10842:   PGeoBucket *r;
                   10843:   int i;
                   10844:   NODE t,s;
                   10845:   ND m,tp;
                   10846:   ND *pi,*rd;
                   10847:   LM lm;
                   10848:   Z lf,c;
                   10849:
                   10850:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   10851:   for ( i = 0; i < nb; i++ )
                   10852:     r[i] = create_pbucket();
                   10853:   for ( t = ti; t; t = NEXT(t) ) {
                   10854:     s = BDY((LIST)BDY(t));
                   10855:     if ( ARG0(s) ) {
                   10856:       m = mdptond((DP)ARG2(s));
1.6       noro     10857:       simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1       noro     10858:       if ( lm ) {
                   10859:         lmtolf(lm,&lf); HCZ(m) = lf;
1.6       noro     10860:         pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     10861:         for ( i = 0; i < nb; i++ ) {
                   10862:           tp = nd_mul_nm_lf(BDY(m),pi[i]);
                   10863:           add_pbucket(-2,r[i],tp);
                   10864:         }
                   10865:       }
                   10866:       c = ONE;
                   10867:     } else {
                   10868:       simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
                   10869:     }
                   10870:   }
                   10871:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   10872:   for ( i = 0; i < nb; i++ )
                   10873:     rd[i] = normalize_pbucket(-2,r[i]);
                   10874:   for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
                   10875:   return rd;
                   10876: }
                   10877:
                   10878: ND btog_one(NODE ti,ND *p,int nb,int mod)
                   10879: {
                   10880:   PGeoBucket r;
                   10881:   int i,ci,j;
                   10882:   NODE t,s;
                   10883:   ND m,tp;
                   10884:   ND pi,rd;
                   10885:   P c;
                   10886:
                   10887:   r = create_pbucket();
                   10888:   for ( t = ti; t; t = NEXT(t) ) {
                   10889:   s = BDY((LIST)BDY(t));
                   10890:     if ( ARG0(s) ) {
                   10891:     m = mdptond((DP)ARG2(s));
1.6       noro     10892:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     10893:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   10894:       HCM(m) = ci;
1.6       noro     10895:       pi = p[j=ZTOS((Q)ARG1(s))];
1.1       noro     10896:     if ( !pi ) {
                   10897:       pi = nd_load_mod(j);
                   10898:       tp = nd_mul_nm(mod,BDY(m),pi);
                   10899:       nd_free(pi);
                   10900:         add_pbucket(mod,r,tp);
                   10901:     } else {
                   10902:       tp = nd_mul_nm(mod,BDY(m),pi);
                   10903:         add_pbucket(mod,r,tp);
                   10904:       }
                   10905:     }
                   10906:     ci = 1;
                   10907:     } else {
                   10908:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   10909:     ci = invm(ci,mod);
                   10910:   }
                   10911:   }
                   10912:   rd = normalize_pbucket(mod,r);
                   10913:   free_pbucket(r);
                   10914:   if ( ci != 1 ) nd_mul_c(mod,rd,ci);
                   10915:   return rd;
                   10916: }
                   10917:
                   10918: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
                   10919:
                   10920: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
                   10921: {
                   10922:   int i,j,n,m,nb,pi0,pi1,nvar;
                   10923:   VL fv,tv,vv;
                   10924:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   10925:   ND **p;
                   10926:   ND *c;
                   10927:   ND u;
                   10928:   P inv;
                   10929:   MAT mat;
                   10930:
                   10931:   if ( mod == -2 )
                   10932:     return nd_btog_lf(f,v,ord,tlist,rp);
                   10933:
                   10934:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     10935:   parse_nd_option(vv,current_option);
1.1       noro     10936:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   10937:   switch ( ord->id ) {
                   10938:     case 1:
                   10939:       if ( ord->nv != nvar )
                   10940:         error("nd_check : invalid order specification");
                   10941:       break;
                   10942:     default:
                   10943:       break;
                   10944:   }
                   10945:   nd_init_ord(ord);
                   10946: #if 0
1.6       noro     10947:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     10948: #else
                   10949:   nd_bpe = 32;
                   10950: #endif
                   10951:   nd_setup_parameters(nvar,0);
                   10952:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   10953:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   10954:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   10955:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   10956:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     10957:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     10958:   if ( j > i ) i = j;
                   10959:   }
                   10960:   n = i+1;
                   10961:   nb = length(BDY(f));
                   10962:   p = (ND **)MALLOC(n*sizeof(ND *));
                   10963:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   10964:     pi = BDY((LIST)BDY(t));
1.6       noro     10965:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     10966:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   10967:     ptomp(mod,(P)ARG2(pi),&inv);
                   10968:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   10969:     u = ptond(CO,vv,(P)ONE);
                   10970:     HCM(u) = ((MQ)inv)->cont;
                   10971:     c[pi1] = u;
                   10972:   }
                   10973:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   10974:     printf("%d ",i); fflush(stdout);
                   10975:     ti = BDY((LIST)BDY(t));
1.6       noro     10976:     p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10977:   }
                   10978:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   10979:     printf("%d ",i); fflush(stdout);
                   10980:     ti = BDY((LIST)BDY(t));
1.6       noro     10981:     p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10982:   }
                   10983:   m = length(ind);
                   10984:   MKMAT(mat,nb,m);
                   10985:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     10986:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     10987:       BDY(mat)[i][j] = ndtodp(mod,c[i]);
                   10988:   return mat;
                   10989: }
                   10990:
                   10991: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
                   10992: {
                   10993:   int i,j,n,m,nb,pi0,pi1,nvar;
                   10994:   VL fv,tv,vv;
                   10995:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   10996:   ND **p;
                   10997:   ND *c;
                   10998:   ND u;
                   10999:   MAT mat;
                   11000:   LM lm;
                   11001:   Z lf,inv;
                   11002:
                   11003:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     11004:   parse_nd_option(vv,current_option);
1.1       noro     11005:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   11006:   switch ( ord->id ) {
                   11007:     case 1:
                   11008:       if ( ord->nv != nvar )
                   11009:         error("nd_check : invalid order specification");
                   11010:       break;
                   11011:     default:
                   11012:       break;
                   11013:   }
                   11014:   nd_init_ord(ord);
                   11015: #if 0
1.6       noro     11016:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     11017: #else
                   11018:   nd_bpe = 32;
                   11019: #endif
                   11020:   nd_setup_parameters(nvar,0);
                   11021:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   11022:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   11023:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   11024:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   11025:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     11026:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     11027:   if ( j > i ) i = j;
                   11028:   }
                   11029:   n = i+1;
                   11030:   nb = length(BDY(f));
                   11031:   p = (ND **)MALLOC(n*sizeof(ND *));
                   11032:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   11033:     pi = BDY((LIST)BDY(t));
1.6       noro     11034:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     11035:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   11036:     simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
                   11037:     u = ptond(CO,vv,(P)ONE);
                   11038:     HCZ(u) = inv;
                   11039:     c[pi1] = u;
                   11040:   }
                   11041:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   11042:     printf("%d ",i); fflush(stdout);
                   11043:     ti = BDY((LIST)BDY(t));
1.6       noro     11044:     p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     11045:   }
                   11046:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   11047:     printf("%d ",i); fflush(stdout);
                   11048:     ti = BDY((LIST)BDY(t));
1.6       noro     11049:     p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     11050:   }
                   11051:   m = length(ind);
                   11052:   MKMAT(mat,nb,m);
                   11053:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     11054:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     11055:       BDY(mat)[i][j] = ndtodp(-2,c[i]);
                   11056:   return mat;
                   11057: }
                   11058:
                   11059: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
                   11060:   LIST tlist,int pos,MAT *rp)
                   11061: {
                   11062:   int i,j,n,m,nb,pi0,pi1,nvar;
                   11063:   VL fv,tv,vv;
                   11064:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   11065:   ND *p;
                   11066:   ND *c;
                   11067:   ND u;
                   11068:   P inv;
                   11069:   VECT vect;
                   11070:
                   11071:   if ( mod == -2 )
                   11072:     error("nd_btog_one : not implemented yet for a large finite field");
                   11073:
                   11074:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     11075:   parse_nd_option(vv,current_option);
1.1       noro     11076:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   11077:   switch ( ord->id ) {
                   11078:     case 1:
                   11079:       if ( ord->nv != nvar )
                   11080:         error("nd_check : invalid order specification");
                   11081:       break;
                   11082:     default:
                   11083:       break;
                   11084:   }
                   11085:   nd_init_ord(ord);
                   11086: #if 0
1.6       noro     11087:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     11088: #else
                   11089:   nd_bpe = 32;
                   11090: #endif
                   11091:   nd_setup_parameters(nvar,0);
                   11092:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   11093:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   11094:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   11095:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   11096:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     11097:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     11098:   if ( j > i ) i = j;
                   11099:   }
                   11100:   n = i+1;
                   11101:   nb = length(BDY(f));
                   11102:   p = (ND *)MALLOC(n*sizeof(ND *));
                   11103:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   11104:     pi = BDY((LIST)BDY(t));
1.6       noro     11105:   pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     11106:   if ( pi1 == pos ) {
                   11107:     ptomp(mod,(P)ARG2(pi),&inv);
                   11108:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   11109:     u = ptond(CO,vv,(P)ONE);
                   11110:     HCM(u) = ((MQ)inv)->cont;
                   11111:     p[pi0] = u;
                   11112:     }
                   11113:   }
                   11114:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   11115:   printf("%d ",i); fflush(stdout);
                   11116:     ti = BDY((LIST)BDY(t));
1.6       noro     11117:     p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     11118:     if ( Demand ) {
                   11119:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   11120:   }
                   11121:   }
                   11122:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   11123:   printf("%d ",i); fflush(stdout);
                   11124:     ti = BDY((LIST)BDY(t));
1.6       noro     11125:     p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     11126:     if ( Demand ) {
                   11127:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   11128:   }
                   11129:   }
                   11130:   m = length(ind);
                   11131:   MKVECT(vect,m);
                   11132:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6       noro     11133:   u = p[ZTOS((Q)BDY(t))];
1.1       noro     11134:   if ( !u ) {
1.6       noro     11135:     u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1       noro     11136:     BDY(vect)[j] = ndtodp(mod,u);
                   11137:     nd_free(u);
                   11138:   } else
                   11139:     BDY(vect)[j] = ndtodp(mod,u);
                   11140:   }
                   11141:   return vect;
                   11142: }
                   11143:
                   11144: void ndv_print_lf(NDV p)
                   11145: {
                   11146:     NMV m;
                   11147:     int i,len;
                   11148:
                   11149:     if ( !p ) printf("0\n");
                   11150:     else {
                   11151:         len = LEN(p);
                   11152:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   11153:             printf("+");
                   11154:             mpz_out_str(asir_out,10,BDY(CZ(m)));
                   11155:             printf("*");
                   11156:             ndl_print(DL(m));
                   11157:         }
                   11158:         printf("\n");
                   11159:     }
                   11160: }
                   11161:
                   11162: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
                   11163: {
                   11164:     VL tv,fv,vv,vc,av;
                   11165:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   11166:     int m,nocheck,nvar,mindex,e,max;
                   11167:     NDV c;
                   11168:     NMV a;
                   11169:     P p,zp;
                   11170:     Q dmy;
                   11171:     EPOS oepos;
                   11172:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   11173:     Alg alpha,dp;
                   11174:     P poly;
                   11175:     LIST f1,f2,zpl;
                   11176:     Obj obj;
                   11177:     NumberField nf;
                   11178:     struct order_spec *ord1;
                   11179:     struct oEGT eg_check,eg0,eg1;
                   11180:     NODE tr,tl1,tl2,tl3,tl4;
                   11181:     LIST l1,l2,l3,l4,l5;
                   11182:     int *perm;
                   11183:     int j,ret;
                   11184:     NODE retn;
                   11185:     Q jq,bpe;
                   11186:
                   11187:     nd_module = 0;
                   11188:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     11189:     parse_nd_option(vv,current_option);
1.1       noro     11190:     if ( nd_vc )
                   11191:       error("nd_f4_lf_trace : computation over a rational function field is not implemented");
                   11192:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   11193:     switch ( ord->id ) {
                   11194:         case 1:
                   11195:             if ( ord->nv != nvar )
                   11196:                 error("nd_f4_lf_trace : invalid order specification");
                   11197:             break;
                   11198:         default:
                   11199:             break;
                   11200:     }
                   11201:
                   11202:     nd_ntrans = nvar;
                   11203:     nd_nalg = 0;
                   11204:
                   11205:     nocheck = 0;
                   11206:     mindex = 0;
                   11207:
                   11208:     /* do not use on-demand load/save */
                   11209:     nd_demand = 0;
                   11210:     m = trace > 1 ? trace : get_lprime(mindex);
                   11211:     nd_init_ord(ord);
                   11212:     mrank = 0;
                   11213:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   11214:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   11215:             if ( nd_module ) {
                   11216:                 s = BDY((LIST)BDY(t));
                   11217:                 trank = length(s);
                   11218:                 mrank = MAX(mrank,trank);
                   11219:                 for ( ; s; s = NEXT(s) ) {
                   11220:                     e = getdeg(tv->v,(P)BDY(s));
                   11221:                     max = MAX(e,max);
                   11222:                 }
                   11223:             } else {
                   11224:                 e = getdeg(tv->v,(P)BDY(t));
                   11225:                 max = MAX(e,max);
                   11226:             }
                   11227:         }
                   11228:     nd_setup_parameters(nvar,max);
                   11229:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   11230:     ishomo = 1;
                   11231:     /* XXX */
                   11232:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   11233:         if ( nd_module ) {
                   11234:             c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
                   11235:         } else {
                   11236:             c = (pointer)ptondv(CO,vv,(P)BDY(t));
                   11237:         }
                   11238:         if ( ishomo )
                   11239:             ishomo = ishomo && ndv_ishomo(c);
                   11240:         if ( c ) {
                   11241:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   11242:             ndv_mod(-2,c);
                   11243:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   11244:         }
                   11245:     }
                   11246:     if ( in0 ) NEXT(in) = 0;
                   11247:     if ( fd0 ) NEXT(fd) = 0;
                   11248:     if ( !ishomo && homo ) {
                   11249:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   11250:             c = (NDV)BDY(t); len = LEN(c);
                   11251:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   11252:                 wmax = MAX(TD(DL(a)),wmax);
                   11253:         }
                   11254:         homogenize_order(ord,nvar,&ord1);
                   11255:         nd_init_ord(ord1);
                   11256:         nd_setup_parameters(nvar+1,wmax);
                   11257:         for ( t = fd0; t; t = NEXT(t) )
                   11258:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   11259:     }
                   11260:     if ( MaxDeg > 0 ) nocheck = 1;
1.24      noro     11261:     ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1       noro     11262:     if ( ret )
                   11263:       cand = nd_f4_lf_trace_main(m,&perm);
                   11264:     if ( !ret || !cand ) {
                   11265:        *rp = 0; return;
                   11266:     }
                   11267:     if ( !ishomo && homo ) {
                   11268:       /* dehomogenization */
                   11269:       for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   11270:       nd_init_ord(ord);
                   11271:       nd_setup_parameters(nvar,0);
                   11272:     }
                   11273:     cand = ndv_reducebase(cand,perm);
                   11274:     cand = ndv_reduceall(-2,cand);
                   11275:     cbpe = nd_bpe;
                   11276:     get_eg(&eg0);
                   11277:     if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   11278:       /* gbcheck : cand is a GB of Id(cand) ? */
                   11279:       retn = nd_f4(-2,0,0);
                   11280:     }
                   11281:   if ( !retn ) {
                   11282:       /* failure */
                   11283:       *rp = 0; return;
                   11284:     }
                   11285:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   11286:     if ( DP_Print )
1.5       noro     11287:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     11288:     /* dp->p */
                   11289:     nd_bpe = cbpe;
                   11290:     nd_setup_parameters(nd_nvar,0);
                   11291:     for ( r = cand; r; r = NEXT(r) ) {
                   11292:       if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
                   11293:       else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
                   11294:     }
                   11295:     MKLIST(*rp,cand);
                   11296: }
                   11297:
                   11298: NODE nd_f4_lf_trace_main(int m,int **indp)
                   11299: {
                   11300:     int i,nh,stat,index;
                   11301:     NODE r,rm,g;
                   11302:     ND_pairs d,l,l0,t;
                   11303:     ND spol,red;
                   11304:     NDV nf,redv,nfqv,nfv;
                   11305:     NM s0,s;
                   11306:     NODE rp0,srp0,nflist,nflist_lf;
                   11307:     int nsp,nred,col,rank,len,k,j,a;
                   11308:     UINT c;
                   11309:     UINT **spmat;
                   11310:     UINT *s0vect,*svect,*p,*v;
                   11311:     int *colstat;
                   11312:     IndArray *imat;
                   11313:     int *rhead;
                   11314:     int spcol,sprow;
                   11315:     int sugar;
                   11316:     PGeoBucket bucket;
                   11317:     struct oEGT eg0,eg1,eg_f4;
                   11318:
                   11319:     g = 0; d = 0;
                   11320:     for ( i = 0; i < nd_psn; i++ ) {
                   11321:         d = update_pairs(d,g,i,0);
                   11322:         g = update_base(g,i);
                   11323:     }
                   11324:     while ( d ) {
                   11325:         get_eg(&eg0);
                   11326:         l = nd_minsugarp(d,&d);
                   11327:         sugar = SG(l);
                   11328:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   11329:         bucket = create_pbucket();
                   11330:         stat = nd_sp_f4(m,0,l,bucket);
                   11331:         if ( !stat ) {
                   11332:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11333:             NEXT(t) = d; d = l;
                   11334:             d = nd_reconstruct(1,d);
                   11335:             continue;
                   11336:         }
                   11337:         if ( bucket->m < 0 ) continue;
                   11338:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   11339:         if ( !col ) {
                   11340:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11341:             NEXT(t) = d; d = l;
                   11342:             d = nd_reconstruct(1,d);
                   11343:             continue;
                   11344:         }
                   11345:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   11346:         if ( DP_Print )
1.5       noro     11347:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1       noro     11348:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   11349:         if ( !l0 ) continue;
                   11350:         l = l0;
                   11351:
                   11352:         /* over LF */
                   11353:         bucket = create_pbucket();
                   11354:         stat = nd_sp_f4(-2,1,l,bucket);
                   11355:         if ( !stat ) {
                   11356:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11357:             NEXT(t) = d; d = l;
                   11358:             d = nd_reconstruct(1,d);
                   11359:             continue;
                   11360:         }
                   11361:         if ( bucket->m < 0 ) continue;
                   11362:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   11363:         if ( !col ) {
                   11364:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11365:             NEXT(t) = d; d = l;
                   11366:             d = nd_reconstruct(1,d);
                   11367:             continue;
                   11368:         }
                   11369:         nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
                   11370:         /* adding new bases */
                   11371:         for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
                   11372:             nfv = (NDV)BDY(rm);
                   11373:             nfqv = (NDV)BDY(r);
                   11374:             if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
                   11375:             ndv_removecont(m,nfv);
                   11376:             ndv_removecont(-2,nfqv);
1.24      noro     11377:             nh = ndv_newps(-2,nfv,nfqv);
1.1       noro     11378:             d = update_pairs(d,g,nh,0);
                   11379:             g = update_base(g,nh);
                   11380:         }
                   11381:         if ( r || rm ) return 0;
                   11382:     }
                   11383:   conv_ilist(nd_demand,1,g,indp);
                   11384:     return g;
                   11385: }
                   11386:
1.7       noro     11387: #if SIZEOF_LONG==8
                   11388:
                   11389: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   11390: {
                   11391:     int j,k,len;
                   11392:     UINT *p;
                   11393:     UINT c;
                   11394:     NDV r;
                   11395:     NMV mr0,mr;
                   11396:
                   11397:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   11398:     if ( !len ) return 0;
                   11399:     else {
                   11400:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   11401: #if 0
                   11402:         ndv_alloc += nmv_adv*len;
                   11403: #endif
                   11404:         mr = mr0;
                   11405:         p = s0vect;
                   11406:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   11407:             if ( !rhead[j] ) {
                   11408:                 if ( (c = (UINT)vect[k++]) != 0 ) {
                   11409:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   11410:                 }
                   11411:             }
                   11412:         MKNDV(nd_nvar,mr0,len,r);
                   11413:         return r;
                   11414:     }
                   11415: }
                   11416:
1.28      noro     11417: NDV vect64_to_ndv_s(mp_limb_t *vect,int col,UINT *s0vect)
                   11418: {
                   11419:     int j,k,len;
                   11420:     UINT *p;
                   11421:     UINT c;
                   11422:     NDV r;
                   11423:     NMV mr0,mr;
                   11424:
                   11425:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   11426:     if ( !len ) return 0;
                   11427:     else {
                   11428:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   11429:         mr = mr0;
                   11430:         p = s0vect;
                   11431:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   11432:           if ( (c = (UINT)vect[k++]) != 0 ) {
                   11433:             ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   11434:           }
                   11435:         MKNDV(nd_nvar,mr0,len,r);
                   11436:         return r;
                   11437:     }
                   11438: }
                   11439:
1.7       noro     11440: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
                   11441: {
                   11442:     NM m;
1.11      noro     11443:     UINT *t,*s,*u;
                   11444:     int i,st,ed,md,prev,c;
1.7       noro     11445:
                   11446:     for ( i = 0; i < n; i++ ) r[i] = 0;
1.11      noro     11447:     prev = 0;
                   11448:     for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
                   11449:       t = DL(m);
                   11450:       st = prev;
                   11451:       ed = n;
                   11452:       while ( ed > st ) {
                   11453:         md = (st+ed)/2;
                   11454:         u = s0+md*nd_wpd;
                   11455:         c = DL_COMPARE(u,t);
                   11456:         if ( c == 0 ) break;
                   11457:         else if ( c > 0 ) st = md;
                   11458:         else ed = md;
                   11459:       }
                   11460:       r[md] = (mp_limb_t)CM(m);
                   11461:       prev = md;
1.7       noro     11462:     }
                   11463:     for ( i = 0; !r[i]; i++ );
                   11464:     return i;
                   11465: }
                   11466:
                   11467: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
                   11468:
1.28      noro     11469: 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     11470: {
                   11471:     int i,j,k,len,pos,prev;
                   11472:     mp_limb_t a,c,c1,c2;
                   11473:     IndArray ivect;
                   11474:     unsigned char *ivc;
                   11475:     unsigned short *ivs;
                   11476:     unsigned int *ivi;
                   11477:     NDV redv;
                   11478:     NMV mr;
                   11479:     NODE rp;
                   11480:     int maxrs;
                   11481:
                   11482:     for ( i = 0; i < col; i++ ) cvect[i] = 0;
                   11483:     maxrs = 0;
                   11484:     for ( i = 0; i < nred; i++ ) {
                   11485:         ivect = imat[i];
                   11486:         k = ivect->head;
                   11487:         a = svect[k]; c = cvect[k];
                   11488:         MOD128(a,c,m);
                   11489:         svect[k] = a; cvect[k] = 0;
1.28      noro     11490:         if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.11      noro     11491:             Nf4_red++;
1.7       noro     11492:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   11493:             c = m-c; redv = nd_ps[rp0[i]->index];
                   11494:             len = LEN(redv); mr = BDY(redv);
                   11495:             svect[k] = 0; prev = k;
                   11496:             switch ( ivect->width ) {
                   11497:                 case 1:
                   11498:                     ivc = ivect->index.c;
                   11499:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   11500:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12      noro     11501:                         c2 = svect[pos]+c1*c;
                   11502:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   11503:                         svect[pos] = c2;
1.7       noro     11504:                     }
                   11505:                     break;
                   11506:                 case 2:
                   11507:                     ivs = ivect->index.s;
                   11508:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   11509:                         pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12      noro     11510:                         c2 = svect[pos]+c1*c;
                   11511:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   11512:                         svect[pos] = c2;
1.7       noro     11513:                     }
                   11514:                     break;
                   11515:                 case 4:
                   11516:                     ivi = ivect->index.i;
                   11517:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   11518:                         pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12      noro     11519:                         c2 = svect[pos]+c1*c;
                   11520:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   11521:                         svect[pos] = c2;
1.7       noro     11522:                     }
                   11523:                     break;
                   11524:             }
                   11525:         }
                   11526:     }
                   11527:     for ( i = 0; i < col; i++ ) {
                   11528:       a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
                   11529:     }
                   11530:     return maxrs;
                   11531: }
                   11532:
                   11533: /* for Fp, 2^15=<p<2^29 */
                   11534:
                   11535: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   11536:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   11537: {
                   11538:     int spcol,sprow,a;
                   11539:     int i,j,k,l,rank;
                   11540:     NODE r0,r;
                   11541:     ND_pairs sp;
                   11542:     ND spol;
                   11543:     mp_limb_t **spmat;
                   11544:     mp_limb_t *svect,*cvect;
                   11545:     mp_limb_t *v;
                   11546:     int *colstat;
                   11547:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   11548:     int maxrs;
                   11549:     int *spsugar;
                   11550:     ND_pairs *spactive;
                   11551:
                   11552:     spcol = col-nred;
                   11553:     get_eg(&eg0);
                   11554:     /* elimination (1st step) */
                   11555:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   11556:     svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11557:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11558:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   11559:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   11560:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   11561:         nd_sp(m,0,sp,&spol);
                   11562:         if ( !spol ) continue;
                   11563:         nd_to_vect64(m,s0vect,col,spol,svect);
1.28      noro     11564:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,0);
1.7       noro     11565:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   11566:         if ( i < col ) {
                   11567:             spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
                   11568:             for ( j = k = 0; j < col; j++ )
                   11569:                 if ( !rhead[j] ) v[k++] = (UINT)svect[j];
                   11570:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   11571:             if ( nz )
                   11572:             spactive[sprow] = sp;
                   11573:             sprow++;
                   11574:         }
                   11575:         nd_free(spol);
                   11576:     }
1.12      noro     11577:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7       noro     11578:     if ( DP_Print ) {
                   11579:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   11580:         fflush(asir_out);
                   11581:     }
                   11582:     /* free index arrays */
                   11583:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   11584:
                   11585:     /* elimination (2nd step) */
                   11586:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   11587:     rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   11588:     r0 = 0;
                   11589:     for ( i = 0; i < rank; i++ ) {
                   11590:         NEXTNODE(r0,r); BDY(r) =
                   11591:           (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   11592:         SG((NDV)BDY(r)) = spsugar[i];
                   11593:         GCFREE(spmat[i]);
                   11594:     }
                   11595:     if ( r0 ) NEXT(r) = 0;
                   11596:
                   11597:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12      noro     11598:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7       noro     11599:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   11600:     if ( DP_Print ) {
                   11601:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   11602:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   11603:             nsp,nred,sprow,spcol,rank);
                   11604:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   11605:     }
                   11606:     if ( nz ) {
                   11607:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   11608:         if ( rank > 0 ) {
                   11609:             NEXT(spactive[rank-1]) = 0;
                   11610:             *nz = spactive[0];
                   11611:         } else
                   11612:             *nz = 0;
                   11613:     }
                   11614:     return r0;
                   11615: }
                   11616:
                   11617: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   11618: {
                   11619:   int i,j,k,l,rank,s;
                   11620:   mp_limb_t inv;
                   11621:   mp_limb_t a;
                   11622:   UINT c;
                   11623:   mp_limb_t *t,*pivot,*pk;
                   11624:   UINT *ck;
                   11625:   UINT **cmat;
                   11626:   UINT *ct;
                   11627:   ND_pairs pair;
                   11628:
                   11629:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   11630:   for ( i = 0; i < row; i++ ) {
                   11631:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   11632:     bzero(cmat[i],col*sizeof(UINT));
                   11633:   }
                   11634:
                   11635:   for ( rank = 0, j = 0; j < col; j++ ) {
                   11636:     for ( i = rank; i < row; i++ ) {
                   11637:       a = mat[i][j]; c = cmat[i][j];
                   11638:       MOD128(a,c,md);
                   11639:       mat[i][j] = a; cmat[i][j] = 0;
                   11640:     }
                   11641:     for ( i = rank; i < row; i++ )
                   11642:       if ( mat[i][j] )
                   11643:         break;
                   11644:     if ( i == row ) {
                   11645:       colstat[j] = 0;
                   11646:       continue;
                   11647:     } else
                   11648:       colstat[j] = 1;
                   11649:     if ( i != rank ) {
                   11650:       t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   11651:       ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
                   11652:       s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   11653:       if ( spactive ) {
                   11654:         pair = spactive[i]; spactive[i] = spactive[rank];
                   11655:         spactive[rank] = pair;
                   11656:       }
                   11657:     }
                   11658:     /* column j is normalized */
                   11659:     s = sugar[rank];
                   11660:     inv = invm((UINT)mat[rank][j],md);
                   11661:     /* normalize pivot row */
                   11662:     for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
                   11663:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   11664:     }
                   11665:     for ( i = rank+1; i < row; i++ ) {
                   11666:       if ( (a = mat[i][j]) != 0 ) {
                   11667:         sugar[i] = MAX(sugar[i],s);
                   11668:         red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11      noro     11669:         Nf4_red++;
1.7       noro     11670:       }
                   11671:     }
                   11672:     rank++;
                   11673:   }
                   11674:   for ( j = col-1, l = rank-1; j >= 0; j-- )
                   11675:     if ( colstat[j] ) {
                   11676:       for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
                   11677:         a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
                   11678:       }
                   11679:       s = sugar[l];
                   11680:       for ( i = 0; i < l; i++ ) {
                   11681:         a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
                   11682:         if ( a ) {
                   11683:           sugar[i] = MAX(sugar[i],s);
                   11684:           red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11      noro     11685:           Nf4_red++;
1.7       noro     11686:         }
                   11687:       }
                   11688:       l--;
                   11689:     }
                   11690:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   11691:   GCFREE(cmat);
                   11692:   return rank;
                   11693: }
                   11694:
1.28      noro     11695: 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)
                   11696: {
                   11697:   int i,j,k,l,rank,s,imin;
                   11698:   mp_limb_t inv;
                   11699:   mp_limb_t a;
                   11700:   UINT c;
                   11701:   mp_limb_t *t,*pivot,*pk;
                   11702:   UINT *ck;
                   11703:   UINT **cmat;
                   11704:   UINT *ct;
                   11705:   ND_pairs pair;
                   11706:   SIG sg;
1.31      noro     11707:   int *used;
1.28      noro     11708:
1.31      noro     11709:   used = (int *)MALLOC(row*sizeof(int));
1.28      noro     11710:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   11711:   for ( i = 0; i < row; i++ ) {
                   11712:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   11713:     bzero(cmat[i],col*sizeof(UINT));
                   11714:   }
                   11715:
1.31      noro     11716:   for ( j = 0; j < col; j++ ) {
                   11717:     for ( i = 0; i < row; i++ ) {
1.28      noro     11718:       a = mat[i][j]; c = cmat[i][j];
                   11719:       MOD128(a,c,md);
                   11720:       mat[i][j] = a; cmat[i][j] = 0;
                   11721:     }
1.31      noro     11722:     for ( i = 0; i < row; i++ )
                   11723:       if ( !used[i] && mat[i][j] ) break;
                   11724:     if ( i == row ) {
1.28      noro     11725:       colstat[j] = 0;
                   11726:       continue;
1.31      noro     11727:     } else {
1.28      noro     11728:       colstat[j] = 1;
1.31      noro     11729:       used[i] = 1;
1.28      noro     11730:     }
                   11731:     /* column j is normalized */
1.31      noro     11732:     s = sugar[i];
                   11733:     inv = invm((UINT)mat[i][j],md);
1.28      noro     11734:     /* normalize pivot row */
1.31      noro     11735:     for ( k = j, pk = mat[i]+j, ck = cmat[i]+j; k < col; k++, pk++, ck++ ) {
1.28      noro     11736:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   11737:     }
1.31      noro     11738:     for ( k = i+1; k < row; k++ ) {
                   11739:       if ( (a = mat[k][j]) != 0 ) {
                   11740:         sugar[k] = MAX(sugar[k],s);
                   11741:         red_by_vect64(md,mat[k]+j,cmat[k]+j,mat[i]+j,(int)(md-a),col-j);
1.28      noro     11742:         Nf4_red++;
                   11743:       }
                   11744:     }
                   11745:   }
1.31      noro     11746:   rank = 0;
                   11747:   for ( i = 0; i < row; i++ ) {
                   11748:     for ( j = 0; j < col; j++ )
                   11749:       if ( mat[i][j] ) break;
                   11750:     if ( j == col ) sugar[i] = -1;
                   11751:     else rank++;
                   11752:   }
1.28      noro     11753:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   11754:   GCFREE(cmat);
                   11755:   return rank;
                   11756: }
                   11757:
                   11758: NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   11759:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
                   11760: {
                   11761:     int spcol,sprow,a;
                   11762:     int i,j,k,l,rank;
                   11763:     NODE r0,r;
                   11764:     ND_pairs sp;
                   11765:     ND spol;
                   11766:     mp_limb_t **spmat;
                   11767:     mp_limb_t *svect,*cvect;
                   11768:     mp_limb_t *v;
                   11769:     int *colstat;
                   11770:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   11771:     int maxrs;
                   11772:     int *spsugar;
                   11773:     ND_pairs *spactive;
                   11774:     SIG *spsig;
                   11775:
                   11776:     get_eg(&eg0);
                   11777:     /* elimination (1st step) */
                   11778:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   11779:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11780:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   11781:     spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
                   11782:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   11783:         nd_sp(m,0,sp,&spol);
                   11784:         if ( !spol ) {
1.29      noro     11785:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     11786:           continue;
                   11787:         }
                   11788:         svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11789:         nd_to_vect64(m,s0vect,col,spol,svect);
                   11790:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,spol->sig);
                   11791:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   11792:         if ( i < col ) {
                   11793:             spmat[sprow] = svect;
                   11794:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   11795:             spsig[sprow] = sp->sig;
                   11796:             sprow++;
                   11797:         } else {
1.29      noro     11798:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     11799:         }
                   11800:         nd_free(spol);
                   11801:     }
                   11802:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
                   11803:     if ( DP_Print ) {
                   11804:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   11805:         fflush(asir_out);
                   11806:     }
                   11807:     /* free index arrays */
                   11808:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   11809:
                   11810:     /* elimination (2nd step) */
                   11811:     colstat = (int *)MALLOC(col*sizeof(int));
                   11812:     rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
                   11813:     r0 = 0;
1.31      noro     11814:     for ( i = 0; i < sprow; i++ ) {
                   11815:         if ( spsugar[i] >= 0 ) {
                   11816:           NEXTNODE(r0,r);
                   11817:           BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
                   11818:           SG((NDV)BDY(r)) = spsugar[i];
                   11819:           ((NDV)BDY(r))->sig = spsig[i];
                   11820:         } else
                   11821:           syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
1.28      noro     11822:         GCFREE(spmat[i]);
                   11823:     }
                   11824:     if ( r0 ) NEXT(r) = 0;
                   11825:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
                   11826:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   11827:     if ( DP_Print ) {
                   11828:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   11829:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   11830:             nsp,nred,sprow,col,rank);
                   11831:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   11832:     }
                   11833:     return r0;
                   11834: }
1.40      noro     11835: #endif
1.28      noro     11836:
                   11837: NODE nd_f4_red_s(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,NODE *syzlistp)
                   11838: {
                   11839:   IndArray *imat;
                   11840:   int nsp,nred,i,start;
                   11841:   int *rhead;
                   11842:   NODE r0,rp;
                   11843:   ND_pairs sp;
                   11844:   NM_ind_pair *rvect;
                   11845:   UINT *s;
                   11846:   int *s0hash;
                   11847:   struct oEGT eg0,eg1,eg_conv;
                   11848:
                   11849:   for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   11850:   nred = length(rp0);
                   11851:   imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   11852:   rhead = (int *)MALLOC(col*sizeof(int));
                   11853:   for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   11854:
                   11855:   /* construction of index arrays */
                   11856:   get_eg(&eg0);
                   11857:   if ( DP_Print ) {
                   11858:     fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   11859:     fflush(asir_out);
                   11860:   }
                   11861:   rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   11862:   for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   11863:     rvect[i] = (NM_ind_pair)BDY(rp);
                   11864:     imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
                   11865:     rhead[imat[i]->head] = 1;
                   11866:     start = imat[i]->head;
                   11867:   }
                   11868:   get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
                   11869:   if ( DP_Print ) {
                   11870:     fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   11871:     fflush(asir_out);
                   11872:   }
                   11873:   if ( m > 0 )
1.40      noro     11874: #if SIZEOF_LONG==8
1.28      noro     11875:     r0 = nd_f4_red_mod64_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
1.40      noro     11876: #else
                   11877:     r0 = nd_f4_red_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
                   11878: #endif
1.28      noro     11879:   else
                   11880: //    r0 = nd_f4_red_q_main_s(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   11881:     error("nd_f4_red_q_main_s : not implemented yet");
                   11882:   return r0;
                   11883: }
                   11884:
                   11885: INLINE int ndl_find_reducer_minsig(UINT *dg)
                   11886: {
                   11887:   RHist r;
                   11888:   int i,singular,ret,d,k,imin;
                   11889:   SIG t;
                   11890:   static int wpd,nvar;
                   11891:   static SIG quo,quomin;
                   11892:   static UINT *tmp;
                   11893:
                   11894:   if ( !quo || nvar != nd_nvar ) { NEWSIG(quo); NEWSIG(quomin); }
                   11895:   if ( wpd != nd_wpd ) {
                   11896:     wpd = nd_wpd;
                   11897:     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
                   11898:   }
                   11899: #if 0
                   11900:   d = ndl_hash_value(dg);
                   11901:   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   11902:     if ( ndl_equal(dg,DL(r)) ) {
                   11903:       return r->index;
                   11904:     }
                   11905:   }
                   11906: #endif
                   11907:   imin = -1;
                   11908:   for ( i = 0; i < nd_psn; i++ ) {
                   11909:     r = nd_psh[i];
                   11910:     if ( ndl_reducible(dg,DL(r)) ) {
                   11911:       ndl_sub(dg,DL(r),tmp);
                   11912:       _ndltodl(tmp,DL(quo));
                   11913:       _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
                   11914:       quo->pos = nd_psh[i]->sig->pos;
                   11915:       if ( imin < 0 || comp_sig(quomin,quo) > 0 ) {
                   11916:         t = quo; quo = quomin; quomin = t;
                   11917:         imin = i;
                   11918:       }
                   11919:     }
                   11920:   }
                   11921:   if ( imin == -1 ) return nd_psn;
                   11922:   else {
                   11923: #if 0
                   11924:     nd_append_red(dg,i);
                   11925: #endif
                   11926:     return imin;
                   11927:   }
                   11928: }
                   11929:
                   11930: int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   11931: {
                   11932:   NODE rp0,rp;
                   11933:   NM mul,head,s0,s;
                   11934:   int index,col,i,sugar;
                   11935:   RHist h;
                   11936:   UINT *s0v,*p;
                   11937:   NM_ind_pair pair;
                   11938:   ND red;
                   11939:   NDV *ps;
                   11940:   SIG sig;
                   11941:
                   11942:   s0 = 0; rp0 = 0; col = 0;
                   11943:   if ( nd_demand )
                   11944:     ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   11945:   else
                   11946:     ps = trace?nd_ps_trace:nd_ps;
                   11947:   while ( 1 ) {
                   11948:     head = remove_head_pbucket_symbolic(bucket);
                   11949:     if ( !head ) break;
                   11950:     if ( !s0 ) s0 = head;
                   11951:     else NEXT(s) = head;
                   11952:     s = head;
                   11953:     index = ndl_find_reducer_minsig(DL(head));
                   11954:     if ( index >= 0 && index < nd_psn ) {
                   11955:       h = nd_psh[index];
                   11956:       NEWNM(mul);
                   11957:       ndl_sub(DL(head),DL(h),DL(mul));
                   11958:       if ( ndl_check_bound2(index,DL(mul)) )
                   11959:         return 0;
                   11960:       sugar = TD(DL(mul))+SG(ps[index]);
                   11961:       NEWSIG(sig);
                   11962:       _ndltodl(DL(mul),DL(sig));
                   11963:       _addtodl(nd_nvar,DL(nd_psh[index]->sig),DL(sig));
                   11964:       sig->pos = nd_psh[index]->sig->pos;
                   11965:       MKNM_ind_pair(pair,mul,index,sugar,sig);
                   11966:       red = ndv_mul_nm_symbolic(mul,ps[index]);
                   11967:       add_pbucket_symbolic(bucket,nd_remove_head(red));
                   11968:       NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   11969:     }
                   11970:     col++;
                   11971:   }
                   11972:   if ( rp0 ) NEXT(rp) = 0;
                   11973:   NEXT(s) = 0;
                   11974:   s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   11975:   for ( i = 0, p = s0v, s = s0; i < col;
                   11976:     i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   11977:   *s0vect = s0v;
                   11978:   *r = rp0;
                   11979:
                   11980:   return col;
                   11981: }
                   11982:
                   11983: NODE nd_sba_f4(int m,int **indp)
                   11984: {
1.34      noro     11985:   int i,nh,stat,index,f4red,f4step;
1.33      noro     11986:   int col,rank,len,k,j,a,sugar,nbase,psugar,ms;
                   11987:   NODE r,g,rp0,nflist;
1.41      noro     11988:   ND_pairs d,l,t,l1;
1.33      noro     11989:   ND h,nf;
                   11990:   NDV nfv;
                   11991:   union oNDC hc;
                   11992:   UINT *s0vect;
1.28      noro     11993:   UINT c;
                   11994:   PGeoBucket bucket;
1.33      noro     11995:   NODE *syzlist;
                   11996:   SIG sig;
1.28      noro     11997:   struct oEGT eg0,eg1,eg_f4;
1.33      noro     11998:   struct oEGT eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.28      noro     11999:
                   12000:   Nf4_red=0;
1.30      noro     12001:   d = 0;
1.29      noro     12002:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.28      noro     12003:   for ( i = 0; i < nd_psn; i++ ) {
1.30      noro     12004:     d = update_pairs_s(d,i,syzlist);
1.28      noro     12005:   }
1.30      noro     12006:   nd_nbase = nd_psn;
1.28      noro     12007:   f4red = 1;
1.33      noro     12008:   psugar = 0;
1.34      noro     12009:   f4step = 0;
1.28      noro     12010:   while ( d ) {
1.33      noro     12011:     for ( t = d, ms = SG(d); t; t = NEXT(t) )
                   12012:       if ( SG(t) < ms ) ms = SG(t);
1.38      noro     12013:     if ( ms == psugar && f4step >= nd_sba_f4step ) {
1.33      noro     12014: again:
                   12015:       l = d; d = d->next;
1.41      noro     12016: #if 0
1.33      noro     12017:       if ( small_lcm(l) ) {
                   12018:         if ( DP_Print ) fprintf(asir_out,"M");
                   12019:         continue;
                   12020:       }
                   12021:       sig = l->sig;
                   12022:       stat = nd_sp(m,0,l,&h);
1.41      noro     12023: #else
                   12024:       l1 = find_smallest_lcm(l);
                   12025:       if ( l1 == 0 ) {
                   12026:         if ( DP_Print ) fprintf(asir_out,"M");
                   12027:         continue;
                   12028:       }
                   12029:       sig = l1->sig;
                   12030:       stat = nd_sp(m,0,l1,&h);
                   12031: #endif
1.33      noro     12032:       if ( !stat ) {
                   12033:         NEXT(l) = d; d = l;
                   12034:         d = nd_reconstruct(0,d);
                   12035:         goto again;
                   12036:       }
                   12037:   get_eg(&eg1);
                   12038:   #if USE_GEOBUCKET
1.39      noro     12039:       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     12040:   #else
1.39      noro     12041:       stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.33      noro     12042:   #endif
                   12043:   get_eg(&eg2);
                   12044:       if ( !stat ) {
                   12045:         NEXT(l) = d; d = l;
                   12046:         d = nd_reconstruct(0,d);
                   12047:         goto again;
                   12048:       } else if ( stat == -1 ) {
                   12049:         if ( DP_Print ) { printf("S"); fflush(stdout); }
                   12050:         FREENDP(l);
                   12051:       } else if ( nf ) {
                   12052:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   12053:         add_eg(&eg_nf,&eg1,&eg2);
                   12054:         hc = HCU(nf);
                   12055:         nd_removecont(m,nf);
                   12056:         nfv = ndtondv(m,nf); nd_free(nf);
                   12057:         nh = ndv_newps(m,nfv,0);
                   12058:
1.30      noro     12059:         d = update_pairs_s(d,nh,syzlist);
                   12060:         nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.33      noro     12061:         FREENDP(l);
1.28      noro     12062:       } else {
1.33      noro     12063:         add_eg(&eg_nfzero,&eg1,&eg2);
                   12064:        // syzygy
                   12065:   get_eg(&eg1);
                   12066:         d = remove_spair_s(d,sig);
                   12067:   get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29      noro     12068:         syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.33      noro     12069:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   12070:         FREENDP(l);
                   12071:       }
                   12072:     } else {
1.34      noro     12073:       if ( ms != psugar ) f4step = 1;
                   12074:       else f4step++;
1.33      noro     12075: again2:
                   12076:       psugar = ms;
                   12077:       l = nd_minsugarp_s(d,&d);
                   12078:       sugar = nd_sugarweight?d->sugar2:SG(d);
                   12079:       bucket = create_pbucket();
                   12080:       stat = nd_sp_f4(m,0,l,bucket);
                   12081:       if ( !stat ) {
                   12082:         for ( t = l; NEXT(t); t = NEXT(t) );
                   12083:         NEXT(t) = d; d = l;
                   12084:         d = nd_reconstruct(0,d);
                   12085:         goto again2;
                   12086:       }
                   12087:       if ( bucket->m < 0 ) continue;
                   12088:       col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
                   12089:       if ( !col ) {
1.37      noro     12090:         for ( t = l; NEXT(t); t = NEXT(t) )
                   12091:           ;
                   12092:         NEXT(t) = d; d = l;
1.33      noro     12093:         d = nd_reconstruct(0,d);
                   12094:         goto again2;
                   12095:       }
1.34      noro     12096:       if ( DP_Print ) fprintf(asir_out,"\nsugar=%d,",psugar);
1.33      noro     12097:       nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
                   12098:       /* adding new bases */
                   12099:       for ( r = nflist; r; r = NEXT(r) ) {
                   12100:         nfv = (NDV)BDY(r);
1.38      noro     12101:         if ( nd_f4_td ) SG(nfv) = nd_tdeg(nfv);
1.33      noro     12102:         ndv_removecont(m,nfv);
                   12103:         nh = ndv_newps(m,nfv,0);
                   12104:         d = update_pairs_s(d,nh,syzlist);
                   12105:         nd_sba_pos[nfv->sig->pos] = append_one(nd_sba_pos[nfv->sig->pos],nh);
                   12106:       }
                   12107:       for ( i = 0; i < nd_nbase; i++ )
                   12108:         for ( r = syzlist[i]; r; r = NEXT(r) )
                   12109:             d = remove_spair_s(d,(SIG)BDY(r));
                   12110:       d = remove_large_lcm(d);
                   12111:       if ( DP_Print ) {
1.34      noro     12112:         fprintf(asir_out,"f4red=%d,gblen=%d",f4red,nd_psn); fflush(asir_out);
1.33      noro     12113:       }
                   12114:       f4red++;
1.28      noro     12115:     }
                   12116:   }
                   12117:   if ( DP_Print ) {
1.34      noro     12118:     fprintf(asir_out,"\nnumber of red=%d,",Nf4_red);
1.28      noro     12119:   }
1.30      noro     12120:   g = conv_ilist_s(nd_demand,0,indp);
1.28      noro     12121:   return g;
                   12122: }

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