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

1.65    ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.64 2022/03/09 07:50:51 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.63      noro       73: static int nd_top,nd_sba_syz,nd_sba_inputisgb,nd_sba_heu;
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:   matrix mat;
                    182:
                    183:   mat = (matrix)MALLOC(sizeof(struct matrix));
                    184:   mat->row = row;
                    185:   mat->col = col;
                    186:   mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
                    187:   return mat;
                    188: }
                    189:
                    190:
                    191: void _ND_alloc()
                    192: {
                    193:     ND p;
                    194:     int i;
                    195:
                    196:     for ( i = 0; i < 1024; i++ ) {
                    197:         p = (ND)MALLOC(sizeof(struct oND));
                    198:         p->body = (NM)_nd_free_list; _nd_free_list = p;
                    199:     }
                    200: }
                    201:
                    202: void _NDP_alloc()
                    203: {
                    204:     ND_pairs p;
                    205:     int i;
                    206:
                    207:     for ( i = 0; i < 1024; i++ ) {
                    208:         p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
                    209:             +(nd_wpd-1)*sizeof(UINT));
                    210:         p->next = _ndp_free_list; _ndp_free_list = p;
                    211:     }
                    212: }
                    213:
                    214: INLINE int nd_length(ND p)
                    215: {
                    216:     NM m;
                    217:     int i;
                    218:
                    219:     if ( !p )
                    220:         return 0;
                    221:     else {
                    222:         for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
                    223:         return i;
                    224:     }
                    225: }
                    226:
                    227: extern int dp_negative_weight;
                    228:
                    229: INLINE int ndl_reducible(UINT *d1,UINT *d2)
                    230: {
                    231:     UINT u1,u2;
                    232:     int i,j;
                    233:
                    234:     if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
                    235:
                    236:     if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
                    237: #if USE_UNROLL
                    238:     switch ( nd_bpe ) {
                    239:         case 3:
                    240:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    241:                 u1 = d1[i]; u2 = d2[i];
                    242:                 if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
                    243:                 if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
                    244:                 if ( (u1&  0xe00000) < (u2&  0xe00000) ) return 0;
                    245:                 if ( (u1&  0x1c0000) < (u2&  0x1c0000) ) return 0;
                    246:                 if ( (u1&   0x38000) < (u2&   0x38000) ) return 0;
                    247:                 if ( (u1&    0x7000) < (u2&    0x7000) ) return 0;
                    248:                 if ( (u1&     0xe00) < (u2&     0xe00) ) return 0;
                    249:                 if ( (u1&     0x1c0) < (u2&     0x1c0) ) return 0;
                    250:                 if ( (u1&      0x38) < (u2&      0x38) ) return 0;
                    251:                 if ( (u1&       0x7) < (u2&       0x7) ) return 0;
                    252:             }
                    253:             return 1;
                    254:             break;
                    255:         case 4:
                    256:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    257:                 u1 = d1[i]; u2 = d2[i];
                    258:                 if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
                    259:                 if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
                    260:                 if ( (u1&  0xf00000) < (u2&  0xf00000) ) return 0;
                    261:                 if ( (u1&   0xf0000) < (u2&   0xf0000) ) return 0;
                    262:                 if ( (u1&    0xf000) < (u2&    0xf000) ) return 0;
                    263:                 if ( (u1&     0xf00) < (u2&     0xf00) ) return 0;
                    264:                 if ( (u1&      0xf0) < (u2&      0xf0) ) return 0;
                    265:                 if ( (u1&       0xf) < (u2&       0xf) ) return 0;
                    266:             }
                    267:             return 1;
                    268:             break;
                    269:         case 6:
                    270:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    271:                 u1 = d1[i]; u2 = d2[i];
                    272:                 if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
                    273:                 if ( (u1&  0xfc0000) < (u2&  0xfc0000) ) return 0;
                    274:                 if ( (u1&   0x3f000) < (u2&   0x3f000) ) return 0;
                    275:                 if ( (u1&     0xfc0) < (u2&     0xfc0) ) return 0;
                    276:                 if ( (u1&      0x3f) < (u2&      0x3f) ) return 0;
                    277:             }
                    278:             return 1;
                    279:             break;
                    280:         case 8:
                    281:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    282:                 u1 = d1[i]; u2 = d2[i];
                    283:                 if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
                    284:                 if ( (u1&  0xff0000) < (u2&  0xff0000) ) return 0;
                    285:                 if ( (u1&    0xff00) < (u2&    0xff00) ) return 0;
                    286:                 if ( (u1&      0xff) < (u2&      0xff) ) return 0;
                    287:             }
                    288:             return 1;
                    289:             break;
                    290:         case 16:
                    291:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    292:                 u1 = d1[i]; u2 = d2[i];
                    293:                 if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
                    294:                 if ( (u1&    0xffff) < (u2&    0xffff) ) return 0;
                    295:             }
                    296:             return 1;
                    297:             break;
                    298:         case 32:
                    299:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                    300:                 if ( d1[i] < d2[i] ) return 0;
                    301:             return 1;
                    302:             break;
                    303:         default:
                    304:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    305:                 u1 = d1[i]; u2 = d2[i];
                    306:                 for ( j = 0; j < nd_epw; j++ )
                    307:                     if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    308:             }
                    309:             return 1;
                    310:     }
                    311: #else
                    312:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    313:         u1 = d1[i]; u2 = d2[i];
                    314:         for ( j = 0; j < nd_epw; j++ )
                    315:             if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    316:     }
                    317:     return 1;
                    318: #endif
                    319: }
                    320:
                    321: /*
                    322:  * If the current order is a block order,
                    323:  * then the last block is length 1 and contains
                    324:  * the homo variable. Otherwise, the original
                    325:  * order is either 0 or 2.
                    326:  */
                    327:
                    328: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
                    329: {
                    330:     int w,i,e,n,omask0;
                    331:
                    332:     omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
                    333:     n = nd_nvar-1;
                    334:     ndl_zero(r);
                    335:     for ( i = 0; i < n; i++ ) {
                    336:         e = GET_EXP_OLD(d,i);
                    337:         PUT_EXP(r,i,e);
                    338:     }
                    339:     w = TD(d);
                    340:     PUT_EXP(r,nd_nvar-1,weight-w);
                    341:     if ( nd_module ) MPOS(r) = d[ompos];
                    342:     TD(r) = weight;
                    343:     if ( nd_blockmask ) ndl_weight_mask(r);
                    344: }
                    345:
                    346: void ndl_dehomogenize(UINT *d)
                    347: {
                    348:     UINT mask;
                    349:     UINT h;
                    350:     int i,bits;
                    351:
                    352:     if ( nd_blockmask ) {
                    353:         h = GET_EXP(d,nd_nvar-1);
                    354:         XOR_EXP(d,nd_nvar-1,h);
                    355:         TD(d) -= h;
                    356:         ndl_weight_mask(d);
                    357:     } else {
                    358:         if ( nd_isrlex ) {
                    359:             if ( nd_bpe == 32 ) {
                    360:                 h = d[nd_exporigin];
                    361:                 for ( i = nd_exporigin+1; i < nd_wpd; i++ )
                    362:                     d[i-1] = d[i];
                    363:                 d[i-1] = 0;
                    364:                 TD(d) -= h;
                    365:             } else {
                    366:                 bits = nd_epw*nd_bpe;
                    367:                 mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
                    368:                 h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
                    369:                 for ( i = nd_exporigin; i < nd_wpd; i++ )
                    370:                     d[i] = ((d[i]<<nd_bpe)&mask)
                    371:                         |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
                    372:                 TD(d) -= h;
                    373:             }
                    374:         } else {
                    375:             h = GET_EXP(d,nd_nvar-1);
                    376:             XOR_EXP(d,nd_nvar-1,h);
                    377:             TD(d) -= h;
                    378:         }
                    379:     }
                    380: }
                    381:
                    382: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
                    383: {
                    384:     UINT t1,t2,u,u1,u2;
1.65    ! noro      385:     int i,j;
1.1       noro      386:
                    387:     if ( nd_module && (MPOS(d1) != MPOS(d2)) )
                    388:         error("ndl_lcm : inconsistent monomials");
                    389: #if USE_UNROLL
                    390:     switch ( nd_bpe ) {
                    391:         case 3:
                    392:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    393:                 u1 = d1[i]; u2 = d2[i];
                    394:                 t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
                    395:                 t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
                    396:                 t1 = (u1&  0xe00000); t2 = (u2&  0xe00000); u |= t1>t2?t1:t2;
                    397:                 t1 = (u1&  0x1c0000); t2 = (u2&  0x1c0000); u |= t1>t2?t1:t2;
                    398:                 t1 = (u1&   0x38000); t2 = (u2&   0x38000); u |= t1>t2?t1:t2;
                    399:                 t1 = (u1&    0x7000); t2 = (u2&    0x7000); u |= t1>t2?t1:t2;
                    400:                 t1 = (u1&     0xe00); t2 = (u2&     0xe00); u |= t1>t2?t1:t2;
                    401:                 t1 = (u1&     0x1c0); t2 = (u2&     0x1c0); u |= t1>t2?t1:t2;
                    402:                 t1 = (u1&      0x38); t2 = (u2&      0x38); u |= t1>t2?t1:t2;
                    403:                 t1 = (u1&       0x7); t2 = (u2&       0x7); u |= t1>t2?t1:t2;
                    404:                 d[i] = u;
                    405:             }
                    406:             break;
                    407:         case 4:
                    408:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    409:                 u1 = d1[i]; u2 = d2[i];
                    410:                 t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
                    411:                 t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
                    412:                 t1 = (u1&  0xf00000); t2 = (u2&  0xf00000); u |= t1>t2?t1:t2;
                    413:                 t1 = (u1&   0xf0000); t2 = (u2&   0xf0000); u |= t1>t2?t1:t2;
                    414:                 t1 = (u1&    0xf000); t2 = (u2&    0xf000); u |= t1>t2?t1:t2;
                    415:                 t1 = (u1&     0xf00); t2 = (u2&     0xf00); u |= t1>t2?t1:t2;
                    416:                 t1 = (u1&      0xf0); t2 = (u2&      0xf0); u |= t1>t2?t1:t2;
                    417:                 t1 = (u1&       0xf); t2 = (u2&       0xf); u |= t1>t2?t1:t2;
                    418:                 d[i] = u;
                    419:             }
                    420:             break;
                    421:         case 6:
                    422:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    423:                 u1 = d1[i]; u2 = d2[i];
                    424:                 t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
                    425:                 t1 = (u1&  0xfc0000); t2 = (u2&  0xfc0000); u |= t1>t2?t1:t2;
                    426:                 t1 = (u1&   0x3f000); t2 = (u2&   0x3f000); u |= t1>t2?t1:t2;
                    427:                 t1 = (u1&     0xfc0); t2 = (u2&     0xfc0); u |= t1>t2?t1:t2;
                    428:                 t1 = (u1&      0x3f); t2 = (u2&      0x3f); u |= t1>t2?t1:t2;
                    429:                 d[i] = u;
                    430:             }
                    431:             break;
                    432:         case 8:
                    433:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    434:                 u1 = d1[i]; u2 = d2[i];
                    435:                 t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
                    436:                 t1 = (u1&  0xff0000); t2 = (u2&  0xff0000); u |= t1>t2?t1:t2;
                    437:                 t1 = (u1&    0xff00); t2 = (u2&    0xff00); u |= t1>t2?t1:t2;
                    438:                 t1 = (u1&      0xff); t2 = (u2&      0xff); u |= t1>t2?t1:t2;
                    439:                 d[i] = u;
                    440:             }
                    441:             break;
                    442:         case 16:
                    443:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    444:                 u1 = d1[i]; u2 = d2[i];
                    445:                 t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
                    446:                 t1 = (u1&    0xffff); t2 = (u2&    0xffff); u |= t1>t2?t1:t2;
                    447:                 d[i] = u;
                    448:             }
                    449:             break;
                    450:         case 32:
                    451:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    452:                 u1 = d1[i]; u2 = d2[i];
                    453:                 d[i] = u1>u2?u1:u2;
                    454:             }
                    455:             break;
                    456:         default:
                    457:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    458:                 u1 = d1[i]; u2 = d2[i];
                    459:                 for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    460:                     t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    461:                 }
                    462:                 d[i] = u;
                    463:             }
                    464:             break;
                    465:     }
                    466: #else
                    467:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    468:         u1 = d1[i]; u2 = d2[i];
                    469:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    470:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    471:         }
                    472:         d[i] = u;
                    473:     }
                    474: #endif
                    475:     if ( nd_module ) MPOS(d) = MPOS(d1);
                    476:     TD(d) = ndl_weight(d);
                    477:     if ( nd_blockmask ) ndl_weight_mask(d);
                    478: }
                    479:
                    480: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
                    481: {
                    482:     UINT t1,t2,u,u1,u2;
1.65    ! noro      483:     int i,j;
1.1       noro      484:
                    485:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    486:         u1 = d1[i]; u2 = d2[i];
                    487:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    488:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    489:         }
                    490:         d[i] = u;
                    491:     }
                    492: }
                    493:
                    494: int ndl_weight(UINT *d)
                    495: {
                    496:     UINT t,u;
                    497:     int i,j;
                    498:
                    499:     if ( current_dl_weight_vector )
                    500:         for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    501:             u = GET_EXP(d,i);
                    502:             t += MUL_WEIGHT(u,i);
                    503:         }
                    504:     else
                    505:         for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    506:             u = d[i];
                    507:             for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    508:                 t += (u&nd_mask0);
                    509:         }
1.20      noro      510:     if ( nd_module && nd_module_rank && MPOS(d) )
                    511:         t += nd_module_weight[MPOS(d)-1];
                    512:     for ( i = nd_exporigin; i < nd_wpd; i++ )
                    513:       if ( d[i] && !t )
                    514:         printf("afo\n");
1.1       noro      515:     return t;
                    516: }
                    517:
                    518: /* for sugarweight */
                    519:
                    520: int ndl_weight2(UINT *d)
                    521: {
                    522:     int t,u;
1.65    ! noro      523:     int i;
1.1       noro      524:
                    525:     for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    526:         u = GET_EXP(d,i);
                    527:         t += nd_sugarweight[i]*u;
                    528:     }
1.20      noro      529:     if ( nd_module && nd_module_rank && MPOS(d) )
                    530:         t += nd_module_weight[MPOS(d)-1];
1.1       noro      531:     return t;
                    532: }
                    533:
                    534: void ndl_weight_mask(UINT *d)
                    535: {
                    536:     UINT t,u;
                    537:     UINT *mask;
                    538:     int i,j,k,l;
                    539:
                    540:     l = nd_blockmask->n;
                    541:     for ( k = 0; k < l; k++ ) {
                    542:         mask = nd_blockmask->mask[k];
                    543:         if ( current_dl_weight_vector )
                    544:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    545:                 u = GET_EXP_MASK(d,i,mask);
                    546:                 t += MUL_WEIGHT(u,i);
                    547:             }
                    548:         else
                    549:             for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    550:                 u = d[i]&mask[i];
                    551:                 for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    552:                     t += (u&nd_mask0);
                    553:             }
                    554:         d[k+1] = t;
                    555:     }
                    556: }
                    557:
1.21      noro      558: int ndl_glex_compare(UINT *d1,UINT *d2)
                    559: {
                    560:   if ( TD(d1) > TD(d2) ) return 1;
                    561:   else if ( TD(d1) < TD(d2) ) return -1;
                    562:   else return ndl_lex_compare(d1,d2);
                    563: }
                    564:
1.1       noro      565: int ndl_lex_compare(UINT *d1,UINT *d2)
                    566: {
                    567:     int i;
                    568:
                    569:     d1 += nd_exporigin;
                    570:     d2 += nd_exporigin;
                    571:     for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
                    572:         if ( *d1 > *d2 )
                    573:             return nd_isrlex ? -1 : 1;
                    574:         else if ( *d1 < *d2 )
                    575:             return nd_isrlex ? 1 : -1;
                    576:     return 0;
                    577: }
                    578:
                    579: int ndl_block_compare(UINT *d1,UINT *d2)
                    580: {
1.65    ! noro      581:     int i,l,j,ord_o;
1.1       noro      582:     struct order_pair *op;
                    583:     UINT t1,t2,m;
                    584:     UINT *mask;
                    585:
                    586:     l = nd_blockmask->n;
                    587:     op = nd_blockmask->order_pair;
                    588:     for ( j = 0; j < l; j++ ) {
                    589:         mask = nd_blockmask->mask[j];
                    590:         ord_o = op[j].order;
                    591:         if ( ord_o < 2 ) {
                    592:             if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
                    593:             else if ( t1 < t2 ) return -1;
                    594:         }
                    595:         for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    596:             m = mask[i];
                    597:             t1 = d1[i]&m;
                    598:             t2 = d2[i]&m;
                    599:             if ( t1 > t2 )
                    600:                 return !ord_o ? -1 : 1;
                    601:             else if ( t1 < t2 )
                    602:                 return !ord_o ? 1 : -1;
                    603:         }
                    604:     }
                    605:     return 0;
                    606: }
                    607:
                    608: int ndl_matrix_compare(UINT *d1,UINT *d2)
                    609: {
                    610:   int i,j,s,row;
                    611:   int *v;
                    612:   Z **mat;
                    613:   Z *w;
                    614:   Z t1;
                    615:   Z t,t2;
                    616:
1.6       noro      617:   for ( j = 0; j < nd_nvar; j++ )
                    618:     nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.1       noro      619:   if ( nd_top_weight ) {
                    620:     if ( OID(nd_top_weight) == O_VECT ) {
1.6       noro      621:       mat = (Z **)&BDY((VECT)nd_top_weight);
                    622:       row = 1;
1.1       noro      623:     } else {
                    624:       mat = (Z **)BDY((MAT)nd_top_weight);
1.6       noro      625:       row = ((MAT)nd_top_weight)->row;
1.1       noro      626:     }
                    627:     for ( i = 0; i < row; i++ ) {
1.6       noro      628:       w = mat[i];
1.1       noro      629:       for ( j = 0, t = 0; j < nd_nvar; j++ ) {
1.6       noro      630:         STOZ(nd_work_vector[j],t1);
1.1       noro      631:         mulz(w[j],t1,&t2);
                    632:         addz(t,t2,&t1);
                    633:         t = t1;
                    634:       }
1.6       noro      635:       if ( t ) {
                    636:         s = sgnz(t);
1.1       noro      637:         if ( s > 0 ) return 1;
                    638:         else if ( s < 0 ) return -1;
1.6       noro      639:       }
1.1       noro      640:     }
1.6       noro      641:   }
                    642:   for ( i = 0; i < nd_matrix_len; i++ ) {
                    643:     v = nd_matrix[i];
                    644:     for ( j = 0, s = 0; j < nd_nvar; j++ )
                    645:       s += v[j]*nd_work_vector[j];
                    646:     if ( s > 0 ) return 1;
                    647:     else if ( s < 0 ) return -1;
                    648:   }
1.1       noro      649:   if ( !ndl_equal(d1,d2) )
1.6       noro      650:     error("ndl_matrix_compare : invalid matrix");
                    651:   return 0;
1.1       noro      652: }
                    653:
                    654: int ndl_composite_compare(UINT *d1,UINT *d2)
                    655: {
                    656:     int i,j,s,start,end,len,o;
                    657:     int *v;
                    658:     struct sparse_weight *sw;
                    659:
                    660:     for ( j = 0; j < nd_nvar; j++ )
                    661:         nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
                    662:     for ( i = 0; i < nd_worb_len; i++ ) {
                    663:         len = nd_worb[i].length;
                    664:         switch ( nd_worb[i].type ) {
                    665:             case IS_DENSE_WEIGHT:
                    666:                 v = nd_worb[i].body.dense_weight;
                    667:                 for ( j = 0, s = 0; j < len; j++ )
                    668:                     s += v[j]*nd_work_vector[j];
                    669:                 if ( s > 0 ) return 1;
                    670:                 else if ( s < 0 ) return -1;
                    671:                 break;
                    672:             case IS_SPARSE_WEIGHT:
                    673:                 sw = nd_worb[i].body.sparse_weight;
                    674:                 for ( j = 0, s = 0; j < len; j++ )
                    675:                     s += sw[j].value*nd_work_vector[sw[j].pos];
                    676:                 if ( s > 0 ) return 1;
                    677:                 else if ( s < 0 ) return -1;
                    678:                 break;
                    679:             case IS_BLOCK:
                    680:                 o = nd_worb[i].body.block.order;
                    681:                 start = nd_worb[i].body.block.start;
                    682:                 switch ( o ) {
                    683:                     case 0:
                    684:                         end = start+len;
                    685:                         for ( j = start, s = 0; j < end; j++ )
                    686:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    687:                         if ( s > 0 ) return 1;
                    688:                         else if ( s < 0 ) return -1;
                    689:                         for ( j = end-1; j >= start; j-- )
                    690:                             if ( nd_work_vector[j] < 0 ) return 1;
                    691:                             else if ( nd_work_vector[j] > 0 ) return -1;
                    692:                         break;
                    693:                     case 1:
                    694:                         end = start+len;
                    695:                         for ( j = start, s = 0; j < end; j++ )
                    696:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    697:                         if ( s > 0 ) return 1;
                    698:                         else if ( s < 0 ) return -1;
                    699:                         for ( j = start; j < end; j++ )
                    700:                             if ( nd_work_vector[j] > 0 ) return 1;
                    701:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    702:                         break;
                    703:                     case 2:
                    704:                         end = start+len;
                    705:                         for ( j = start; j < end; j++ )
                    706:                             if ( nd_work_vector[j] > 0 ) return 1;
                    707:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    708:                         break;
                    709:                 }
                    710:                 break;
                    711:         }
                    712:     }
                    713:     return 0;
                    714: }
                    715:
                    716: /* TDH -> WW -> TD-> RL */
                    717:
                    718: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
                    719: {
                    720:     int i,m,e1,e2;
                    721:
                    722:     if ( TD(d1) > TD(d2) ) return 1;
                    723:     else if ( TD(d1) < TD(d2) ) return -1;
                    724:     m = nd_nvar>>1;
                    725:     for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
                    726:         e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
                    727:         e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
                    728:     }
                    729:     if ( e1 > e2 ) return 1;
                    730:     else if ( e1 < e2 ) return -1;
                    731:     return ndl_lex_compare(d1,d2);
                    732: }
                    733:
1.21      noro      734: // common function for module glex and grlex comparison
                    735: int ndl_module_glex_compare(UINT *d1,UINT *d2)
1.1       noro      736: {
1.21      noro      737:   int c;
1.1       noro      738:
1.21      noro      739:   switch ( nd_module_ordtype ) {
                    740:     case 0:
                    741:       if ( TD(d1) > TD(d2) ) return 1;
                    742:       else if ( TD(d1) < TD(d2) ) return -1;
                    743:       else if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
                    744:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    745:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    746:       else return 0;
                    747:       break;
1.1       noro      748:
1.21      noro      749:     case 1:
1.65    ! noro      750:       if ( nd_pot_nelim && MPOS(d1)>=(UINT)(nd_pot_nelim+1) && MPOS(d2) >= (UINT)(nd_pot_nelim+1) ) {
1.19      noro      751:          if ( TD(d1) > TD(d2) ) return 1;
                    752:          else if ( TD(d1) < TD(d2) ) return -1;
                    753:          if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
                    754:          if ( MPOS(d1) < MPOS(d2) ) return 1;
                    755:          else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    756:       }
                    757:       if ( MPOS(d1) < MPOS(d2) ) return 1;
                    758:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
1.21      noro      759:       else if ( TD(d1) > TD(d2) ) return 1;
                    760:       else if ( TD(d1) < TD(d2) ) return -1;
                    761:       else return ndl_lex_compare(d1,d2);
                    762:       break;
1.1       noro      763:
1.21      noro      764:     case 2: // weight -> POT
                    765:       if ( TD(d1) > TD(d2) ) return 1;
                    766:       else if ( TD(d1) < TD(d2) ) return -1;
                    767:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    768:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    769:       else return ndl_lex_compare(d1,d2);
                    770:       break;
1.1       noro      771:
1.21      noro      772:     default:
                    773:       error("ndl_module_glex_compare : invalid module_ordtype");
1.28      noro      774:       return 0;
1.21      noro      775:   }
1.1       noro      776: }
                    777:
1.21      noro      778: // common  for module comparison
                    779: int ndl_module_compare(UINT *d1,UINT *d2)
1.1       noro      780: {
1.21      noro      781:   int c;
1.1       noro      782:
1.21      noro      783:   switch ( nd_module_ordtype ) {
                    784:     case 0:
1.23      noro      785:       if ( (c = (*ndl_base_compare_function)(d1,d2)) != 0 ) return c;
1.21      noro      786:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    787:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    788:       else return 0;
                    789:       break;
1.1       noro      790:
1.21      noro      791:     case 1:
                    792:       if ( MPOS(d1) < MPOS(d2) ) return 1;
                    793:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    794:       else return (*ndl_base_compare_function)(d1,d2);
                    795:       break;
1.1       noro      796:
1.21      noro      797:     case 2: // weight -> POT
                    798:       if ( TD(d1) > TD(d2) ) return 1;
                    799:       else if ( TD(d1) < TD(d2) ) return -1;
                    800:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    801:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    802:       else return (*ndl_base_compare_function)(d1,d2);
                    803:       break;
1.1       noro      804:
1.21      noro      805:     default:
                    806:       error("ndl_module_compare : invalid module_ordtype");
1.28      noro      807:       return 0;
1.21      noro      808:   }
1.1       noro      809: }
                    810:
1.21      noro      811: extern DMMstack dmm_stack;
                    812: void _addtodl(int n,DL d1,DL d2);
1.31      noro      813: void _adddl(int n,DL d1,DL d2,DL d3);
1.21      noro      814: int _eqdl(int n,DL d1,DL d2);
                    815:
                    816: int ndl_module_schreyer_compare(UINT *m1,UINT *m2)
                    817: {
1.44      noro      818:   int pos1,pos2,t,j,retpot;
1.21      noro      819:   DMM *in;
                    820:   DMMstack s;
                    821:   static DL d1=0;
                    822:   static DL d2=0;
                    823:   static int dlen=0;
1.44      noro      824:   extern int ReversePOT;
1.21      noro      825:
1.44      noro      826:   if ( ReversePOT ) retpot = -1;
                    827:   else retpot = 1;
1.21      noro      828:   pos1 = MPOS(m1); pos2 = MPOS(m2);
                    829:   if ( pos1 == pos2 ) return (*ndl_base_compare_function)(m1,m2);
                    830:   if ( nd_nvar > dlen ) {
                    831:     NEWDL(d1,nd_nvar);
                    832:     NEWDL(d2,nd_nvar);
                    833:     dlen = nd_nvar;
                    834:   }
                    835:   d1->td = TD(m1);
                    836:   for ( j = 0; j < nd_nvar; j++ ) d1->d[j] = GET_EXP(m1,j);
                    837:   d2->td = TD(m2);
                    838:   for ( j = 0; j < nd_nvar; j++ ) d2->d[j] = GET_EXP(m2,j);
                    839:   for ( s = dmm_stack; s; s = NEXT(s) ) {
                    840:     in = s->in;
                    841:     _addtodl(nd_nvar,in[pos1]->dl,d1);
                    842:     _addtodl(nd_nvar,in[pos2]->dl,d2);
                    843:     if ( in[pos1]->pos == in[pos2]->pos && _eqdl(nd_nvar,d1,d2)) {
1.44      noro      844:       if ( pos1 < pos2 ) return retpot;
                    845:       else if ( pos1 > pos2 ) return -retpot;
1.21      noro      846:       else return 0;
                    847:     }
                    848:     pos1 = in[pos1]->pos;
                    849:     pos2 = in[pos2]->pos;
                    850:     if ( pos1 == pos2 ) return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    851:   }
                    852:   // comparison by the bottom order
                    853:   switch ( nd_base_ordtype ) {
                    854:     case 0:
                    855:       t = (*dl_base_compare_function)(nd_nvar,d1,d2);
                    856:       if ( t ) return t;
1.44      noro      857:       else if ( pos1 < pos2 ) return retpot;
                    858:       else if ( pos1 > pos2 ) return -retpot;
1.21      noro      859:       else return 0;
                    860:       break;
                    861:     case 1:
1.44      noro      862:       if ( pos1 < pos2 ) return retpot;
                    863:       else if ( pos1 > pos2 ) return -retpot;
1.21      noro      864:       else return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    865:       break;
                    866:     case 2:
                    867:       if ( d1->td > d2->td  ) return 1;
                    868:       else if ( d1->td < d2->td ) return -1;
1.44      noro      869:       else if ( pos1 < pos2 ) return retpot;
                    870:       else if ( pos1 > pos2 ) return -retpot;
1.21      noro      871:       else return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    872:       break;
                    873:     default:
                    874:       error("ndl_schreyer_compare : invalid base ordtype");
1.28      noro      875:       return 0;
1.21      noro      876:   }
1.1       noro      877: }
                    878:
                    879: INLINE int ndl_equal(UINT *d1,UINT *d2)
                    880: {
                    881:     int i;
                    882:
                    883:     switch ( nd_wpd ) {
                    884:         case 2:
                    885:             if ( TD(d2) != TD(d1) ) return 0;
                    886:             if ( d2[1] != d1[1] ) return 0;
                    887:             return 1;
                    888:             break;
                    889:         case 3:
                    890:             if ( TD(d2) != TD(d1) ) return 0;
                    891:             if ( d2[1] != d1[1] ) return 0;
                    892:             if ( d2[2] != d1[2] ) return 0;
                    893:             return 1;
                    894:             break;
                    895:         default:
                    896:             for ( i = 0; i < nd_wpd; i++ )
                    897:                 if ( *d1++ != *d2++ ) return 0;
                    898:             return 1;
                    899:             break;
                    900:     }
                    901: }
                    902:
                    903: INLINE void ndl_copy(UINT *d1,UINT *d2)
                    904: {
                    905:     int i;
                    906:
                    907:     switch ( nd_wpd ) {
                    908:         case 2:
                    909:             TD(d2) = TD(d1);
                    910:             d2[1] = d1[1];
                    911:             break;
                    912:         case 3:
                    913:             TD(d2) = TD(d1);
                    914:             d2[1] = d1[1];
                    915:             d2[2] = d1[2];
                    916:             break;
                    917:         default:
                    918:             for ( i = 0; i < nd_wpd; i++ )
                    919:                 d2[i] = d1[i];
                    920:             break;
                    921:     }
                    922: }
                    923:
                    924: INLINE void ndl_zero(UINT *d)
                    925: {
                    926:     int i;
                    927:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                    928: }
                    929:
                    930: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
                    931: {
                    932:     int i;
                    933:
                    934:     if ( nd_module ) {
                    935:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
                    936:         error("ndl_add : invalid operation");
                    937:     }
                    938: #if 1
                    939:     switch ( nd_wpd ) {
                    940:         case 2:
                    941:             TD(d) = TD(d1)+TD(d2);
                    942:             d[1] = d1[1]+d2[1];
                    943:             break;
                    944:         case 3:
                    945:             TD(d) = TD(d1)+TD(d2);
                    946:             d[1] = d1[1]+d2[1];
                    947:             d[2] = d1[2]+d2[2];
                    948:             break;
                    949:         default:
                    950:             for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    951:             break;
                    952:     }
                    953: #else
                    954:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    955: #endif
                    956: }
                    957:
                    958: /* d1 += d2 */
                    959: INLINE void ndl_addto(UINT *d1,UINT *d2)
                    960: {
                    961:     int i;
                    962:
                    963:     if ( nd_module ) {
                    964:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
                    965:             error("ndl_addto : invalid operation");
                    966:     }
                    967: #if 1
                    968:     switch ( nd_wpd ) {
                    969:         case 2:
                    970:             TD(d1) += TD(d2);
                    971:             d1[1] += d2[1];
                    972:             break;
                    973:         case 3:
                    974:             TD(d1) += TD(d2);
                    975:             d1[1] += d2[1];
                    976:             d1[2] += d2[2];
                    977:             break;
                    978:         default:
                    979:             for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    980:             break;
                    981:     }
                    982: #else
                    983:     for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    984: #endif
                    985: }
                    986:
                    987: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
                    988: {
                    989:     int i;
                    990:
                    991:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
                    992: }
                    993:
                    994: int ndl_disjoint(UINT *d1,UINT *d2)
                    995: {
1.65    ! noro      996:     UINT t1,t2,u1,u2;
1.1       noro      997:     int i,j;
                    998:
                    999:     if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
                   1000: #if USE_UNROLL
                   1001:     switch ( nd_bpe ) {
                   1002:         case 3:
                   1003:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1004:                 u1 = d1[i]; u2 = d2[i];
                   1005:                 t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
                   1006:                 t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
                   1007:                 t1 = u1&  0xe00000; t2 = u2&  0xe00000; if ( t1&&t2 ) return 0;
                   1008:                 t1 = u1&  0x1c0000; t2 = u2&  0x1c0000; if ( t1&&t2 ) return 0;
                   1009:                 t1 = u1&   0x38000; t2 = u2&   0x38000; if ( t1&&t2 ) return 0;
                   1010:                 t1 = u1&    0x7000; t2 = u2&    0x7000; if ( t1&&t2 ) return 0;
                   1011:                 t1 = u1&     0xe00; t2 = u2&     0xe00; if ( t1&&t2 ) return 0;
                   1012:                 t1 = u1&     0x1c0; t2 = u2&     0x1c0; if ( t1&&t2 ) return 0;
                   1013:                 t1 = u1&      0x38; t2 = u2&      0x38; if ( t1&&t2 ) return 0;
                   1014:                 t1 = u1&       0x7; t2 = u2&       0x7; if ( t1&&t2 ) return 0;
                   1015:             }
                   1016:             return 1;
                   1017:             break;
                   1018:         case 4:
                   1019:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1020:                 u1 = d1[i]; u2 = d2[i];
                   1021:                 t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
                   1022:                 t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
                   1023:                 t1 = u1&  0xf00000; t2 = u2&  0xf00000; if ( t1&&t2 ) return 0;
                   1024:                 t1 = u1&   0xf0000; t2 = u2&   0xf0000; if ( t1&&t2 ) return 0;
                   1025:                 t1 = u1&    0xf000; t2 = u2&    0xf000; if ( t1&&t2 ) return 0;
                   1026:                 t1 = u1&     0xf00; t2 = u2&     0xf00; if ( t1&&t2 ) return 0;
                   1027:                 t1 = u1&      0xf0; t2 = u2&      0xf0; if ( t1&&t2 ) return 0;
                   1028:                 t1 = u1&       0xf; t2 = u2&       0xf; if ( t1&&t2 ) return 0;
                   1029:             }
                   1030:             return 1;
                   1031:             break;
                   1032:         case 6:
                   1033:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1034:                 u1 = d1[i]; u2 = d2[i];
                   1035:                 t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
                   1036:                 t1 = u1&  0xfc0000; t2 = u2&  0xfc0000; if ( t1&&t2 ) return 0;
                   1037:                 t1 = u1&   0x3f000; t2 = u2&   0x3f000; if ( t1&&t2 ) return 0;
                   1038:                 t1 = u1&     0xfc0; t2 = u2&     0xfc0; if ( t1&&t2 ) return 0;
                   1039:                 t1 = u1&      0x3f; t2 = u2&      0x3f; if ( t1&&t2 ) return 0;
                   1040:             }
                   1041:             return 1;
                   1042:             break;
                   1043:         case 8:
                   1044:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1045:                 u1 = d1[i]; u2 = d2[i];
                   1046:                 t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
                   1047:                 t1 = u1&  0xff0000; t2 = u2&  0xff0000; if ( t1&&t2 ) return 0;
                   1048:                 t1 = u1&    0xff00; t2 = u2&    0xff00; if ( t1&&t2 ) return 0;
                   1049:                 t1 = u1&      0xff; t2 = u2&      0xff; if ( t1&&t2 ) return 0;
                   1050:             }
                   1051:             return 1;
                   1052:             break;
                   1053:         case 16:
                   1054:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1055:                 u1 = d1[i]; u2 = d2[i];
                   1056:                 t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
                   1057:                 t1 = u1&    0xffff; t2 = u2&    0xffff; if ( t1&&t2 ) return 0;
                   1058:             }
                   1059:             return 1;
                   1060:             break;
                   1061:         case 32:
                   1062:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                   1063:                 if ( d1[i] && d2[i] ) return 0;
                   1064:             return 1;
                   1065:             break;
                   1066:         default:
                   1067:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1068:                 u1 = d1[i]; u2 = d2[i];
                   1069:                 for ( j = 0; j < nd_epw; j++ ) {
                   1070:                     if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                   1071:                     u1 >>= nd_bpe; u2 >>= nd_bpe;
                   1072:                 }
                   1073:             }
                   1074:             return 1;
                   1075:             break;
                   1076:     }
                   1077: #else
                   1078:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1079:         u1 = d1[i]; u2 = d2[i];
                   1080:         for ( j = 0; j < nd_epw; j++ ) {
                   1081:             if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                   1082:             u1 >>= nd_bpe; u2 >>= nd_bpe;
                   1083:         }
                   1084:     }
                   1085:     return 1;
                   1086: #endif
                   1087: }
                   1088:
                   1089: int ndl_check_bound(UINT *d1,UINT *d2)
                   1090: {
                   1091:     UINT u2;
                   1092:     int i,j,ind,k;
                   1093:
                   1094:     ind = 0;
                   1095: #if USE_UNROLL
                   1096:     switch ( nd_bpe ) {
                   1097:         case 3:
                   1098:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1099:                 u2 = d2[i];
                   1100:                 if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
                   1101:                 if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
                   1102:                 if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
                   1103:                 if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
                   1104:                 if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
                   1105:                 if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
                   1106:                 if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
                   1107:                 if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
                   1108:                 if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
                   1109:                 if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
                   1110:             }
                   1111:             return 0;
                   1112:             break;
                   1113:         case 4:
                   1114:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1115:                 u2 = d2[i];
                   1116:                 if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
                   1117:                 if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
                   1118:                 if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
                   1119:                 if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
                   1120:                 if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
                   1121:                 if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
                   1122:                 if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
                   1123:                 if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
                   1124:             }
                   1125:             return 0;
                   1126:             break;
                   1127:         case 6:
                   1128:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1129:                 u2 = d2[i];
                   1130:                 if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
                   1131:                 if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
                   1132:                 if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
                   1133:                 if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
                   1134:                 if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
                   1135:             }
                   1136:             return 0;
                   1137:             break;
                   1138:         case 8:
                   1139:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1140:                 u2 = d2[i];
                   1141:                 if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
                   1142:                 if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
                   1143:                 if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
                   1144:                 if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
                   1145:             }
                   1146:             return 0;
                   1147:             break;
                   1148:         case 16:
                   1149:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1150:                 u2 = d2[i];
                   1151:                 if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
                   1152:                 if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
                   1153:             }
                   1154:             return 0;
                   1155:             break;
                   1156:         case 32:
                   1157:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                   1158:                 if ( d1[i]+d2[i]<d1[i] ) return 1;
                   1159:             return 0;
                   1160:             break;
                   1161:         default:
                   1162:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1163:                 u2 = d2[i];
                   1164:                 k = (nd_epw-1)*nd_bpe;
                   1165:                 for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1166:                     if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1167:             }
                   1168:             return 0;
                   1169:             break;
                   1170:     }
                   1171: #else
                   1172:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1173:         u2 = d2[i];
                   1174:         k = (nd_epw-1)*nd_bpe;
                   1175:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1176:             if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1177:     }
                   1178:     return 0;
                   1179: #endif
                   1180: }
                   1181:
                   1182: int ndl_check_bound2(int index,UINT *d2)
                   1183: {
                   1184:     return ndl_check_bound(nd_bound[index],d2);
                   1185: }
                   1186:
                   1187: INLINE int ndl_hash_value(UINT *d)
                   1188: {
                   1189:     int i;
1.11      noro     1190:     UINT r;
1.1       noro     1191:
                   1192:     r = 0;
                   1193:     for ( i = 0; i < nd_wpd; i++ )
1.12      noro     1194:         r = (r*1511+d[i]);
1.11      noro     1195:     r %= REDTAB_LEN;
1.1       noro     1196:     return r;
                   1197: }
                   1198:
                   1199: INLINE int ndl_find_reducer(UINT *dg)
                   1200: {
                   1201:     RHist r;
                   1202:     int d,k,i;
                   1203:
                   1204:     d = ndl_hash_value(dg);
                   1205:     for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   1206:         if ( ndl_equal(dg,DL(r)) ) {
                   1207:             if ( k > 0 ) nd_notfirst++;
                   1208:             nd_found++;
                   1209:             return r->index;
                   1210:         }
                   1211:     }
                   1212:     if ( Reverse )
                   1213:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   1214:             r = nd_psh[i];
                   1215:             if ( ndl_reducible(dg,DL(r)) ) {
                   1216:                 nd_create++;
                   1217:                 nd_append_red(dg,i);
                   1218:                 return i;
                   1219:             }
                   1220:         }
                   1221:     else
                   1222:         for ( i = 0; i < nd_psn; i++ ) {
                   1223:             r = nd_psh[i];
                   1224:             if ( ndl_reducible(dg,DL(r)) ) {
                   1225:                 nd_create++;
                   1226:                 nd_append_red(dg,i);
                   1227:                 return i;
                   1228:             }
                   1229:         }
                   1230:     return -1;
                   1231: }
                   1232:
1.41      noro     1233: INLINE int ndl_find_reducer_nonsig(UINT *dg)
                   1234: {
                   1235:     RHist r;
                   1236:     int i;
                   1237:
                   1238:     for ( i = 0; i < nd_psn; i++ ) {
                   1239:       r = nd_psh[i];
                   1240:       if ( ndl_reducible(dg,DL(r)) ) return i;
                   1241:     }
                   1242:     return -1;
                   1243: }
                   1244:
1.24      noro     1245: // ret=0,...,nd_psn-1 => reducer found
                   1246: // ret=nd_psn => reducer not found
                   1247: // ret=-1 => singular top reducible
                   1248:
                   1249: int comp_sig(SIG s1,SIG s2);
                   1250: void _ndltodl(UINT *ndl,DL dl);
                   1251:
                   1252: void print_sig(SIG s)
                   1253: {
                   1254:   int i;
                   1255:
                   1256:   fprintf(asir_out,"<<");
                   1257:   for ( i = 0; i < nd_nvar; i++ ) {
                   1258:     fprintf(asir_out,"%d",s->dl->d[i]);
                   1259:     if ( i != nd_nvar-1 ) fprintf(asir_out,",");
                   1260:   }
                   1261:   fprintf(asir_out,">>*e%d",s->pos);
                   1262: }
                   1263:
1.45      noro     1264: void print_siglist(NODE l)
                   1265: {
                   1266:   for ( ; l; l = NEXT(l) )
                   1267:    print_sig((SIG)l->body);
                   1268: }
                   1269:
                   1270:
1.35      noro     1271: // assuming increasing order wrt signature
                   1272:
1.24      noro     1273: INLINE int ndl_find_reducer_s(UINT *dg,SIG sig)
                   1274: {
                   1275:   RHist r;
1.27      noro     1276:   int i,singular,ret,d,k;
1.26      noro     1277:   static int wpd,nvar;
1.24      noro     1278:   static SIG quo;
                   1279:   static UINT *tmp;
                   1280:
1.26      noro     1281:   if ( !quo || nvar != nd_nvar ) NEWSIG(quo);
                   1282:   if ( wpd != nd_wpd ) {
1.24      noro     1283:     wpd = nd_wpd;
                   1284:     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
                   1285:   }
1.27      noro     1286:   d = ndl_hash_value(dg);
1.35      noro     1287: #if 1
1.27      noro     1288:   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   1289:     if ( ndl_equal(dg,DL(r)) ) {
                   1290:       return r->index;
                   1291:     }
                   1292:   }
1.35      noro     1293: #endif
1.24      noro     1294:   singular = 0;
                   1295:   for ( i = 0; i < nd_psn; i++ ) {
                   1296:     r = nd_psh[i];
                   1297:     if ( ndl_reducible(dg,DL(r)) ) {
1.27      noro     1298:       ndl_sub(dg,DL(r),tmp);
1.24      noro     1299:       _ndltodl(tmp,DL(quo));
                   1300:       _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
                   1301:       quo->pos = nd_psh[i]->sig->pos;
1.55      noro     1302:       _adddl(nd_nvar,DL(quo),nd_sba_hm[quo->pos],DL2(quo));
1.24      noro     1303:       ret = comp_sig(sig,quo);
1.64      noro     1304: //      if ( ret >= 0 ) { singular = 0; break; }
1.24      noro     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: {
1.65    ! noro     1317:     int c;
        !          1318:     int can;
1.1       noro     1319:     NM m1,m2,mr0,mr,s;
                   1320:
                   1321:     if ( !p1 ) return p2;
                   1322:     else if ( !p2 ) return p1;
                   1323:     else {
                   1324:         can = 0;
1.65    ! noro     1325:         for (  m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1.1       noro     1326:             c = DL_COMPARE(DL(m1),DL(m2));
                   1327:             switch ( c ) {
                   1328:                 case 0:
                   1329:                     s = m1; m1 = NEXT(m1);
                   1330:                     can++; NEXTNM2(mr0,mr,s);
                   1331:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1332:                     break;
                   1333:                 case 1:
                   1334:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1335:                     break;
                   1336:                 case -1:
                   1337:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1338:                     break;
                   1339:             }
                   1340:         }
                   1341:         if ( !mr0 )
                   1342:             if ( m1 ) mr0 = m1;
                   1343:             else if ( m2 ) mr0 = m2;
                   1344:             else return 0;
                   1345:         else if ( m1 ) NEXT(mr) = m1;
                   1346:         else if ( m2 ) NEXT(mr) = m2;
                   1347:         else NEXT(mr) = 0;
                   1348:         BDY(p1) = mr0;
                   1349:         SG(p1) = MAX(SG(p1),SG(p2));
                   1350:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1351:         FREEND(p2);
                   1352:         return p1;
                   1353:     }
                   1354: }
                   1355:
                   1356: ND nd_add(int mod,ND p1,ND p2)
                   1357: {
1.65    ! noro     1358:     int c;
        !          1359:     int t,can;
1.1       noro     1360:     NM m1,m2,mr0,mr,s;
                   1361:
1.11      noro     1362:     Nnd_add++;
1.1       noro     1363:     if ( !p1 ) return p2;
                   1364:     else if ( !p2 ) return p1;
                   1365:     else if ( mod == -1 ) return nd_add_sf(p1,p2);
                   1366:     else if ( mod == -2 ) return nd_add_lf(p1,p2);
                   1367:     else if ( !mod ) return nd_add_q(p1,p2);
                   1368:     else {
                   1369:         can = 0;
1.65    ! noro     1370:         for ( m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1.1       noro     1371:             c = DL_COMPARE(DL(m1),DL(m2));
                   1372:             switch ( c ) {
                   1373:                 case 0:
                   1374:                     t = ((CM(m1))+(CM(m2))) - mod;
                   1375:                     if ( t < 0 ) t += mod;
                   1376:                     s = m1; m1 = NEXT(m1);
                   1377:                     if ( t ) {
                   1378:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1379:                     } else {
                   1380:                         can += 2; FREENM(s);
                   1381:                     }
                   1382:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1383:                     break;
                   1384:                 case 1:
                   1385:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1386:                     break;
                   1387:                 case -1:
                   1388:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1389:                     break;
                   1390:             }
                   1391:         }
                   1392:         if ( !mr0 )
                   1393:             if ( m1 ) mr0 = m1;
                   1394:             else if ( m2 ) mr0 = m2;
                   1395:             else return 0;
                   1396:         else if ( m1 ) NEXT(mr) = m1;
                   1397:         else if ( m2 ) NEXT(mr) = m2;
                   1398:         else NEXT(mr) = 0;
                   1399:         BDY(p1) = mr0;
                   1400:         SG(p1) = MAX(SG(p1),SG(p2));
                   1401:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1402:         FREEND(p2);
                   1403:         return p1;
                   1404:     }
                   1405: }
                   1406:
                   1407: /* XXX on opteron, the inlined manipulation of destructive additon of
                   1408:  * two NM seems to make gcc optimizer get confused, so the part is
                   1409:  * done in a function.
                   1410:  */
                   1411:
                   1412: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
                   1413: {
                   1414:     NM s;
                   1415:     P t;
                   1416:     int can;
                   1417:
                   1418:     addp(nd_vc,CP(*m1),CP(*m2),&t);
                   1419:     s = *m1; *m1 = NEXT(*m1);
                   1420:     if ( t ) {
                   1421:         can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
                   1422:     } else {
                   1423:         can = 2; FREENM(s);
                   1424:     }
                   1425:     s = *m2; *m2 = NEXT(*m2); FREENM(s);
                   1426:     return can;
                   1427: }
                   1428:
                   1429: ND nd_add_q(ND p1,ND p2)
                   1430: {
1.65    ! noro     1431:     int c,can;
1.1       noro     1432:     NM m1,m2,mr0,mr,s;
                   1433:
                   1434:     if ( !p1 ) return p2;
                   1435:     else if ( !p2 ) return p1;
                   1436:     else {
                   1437:         can = 0;
1.65    ! noro     1438:         for ( m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1.1       noro     1439:             c = DL_COMPARE(DL(m1),DL(m2));
                   1440:             switch ( c ) {
                   1441:                 case 0:
                   1442: #if defined(__x86_64__)
                   1443:                     can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
                   1444: #else
                   1445:                     addp(nd_vc,CP(m1),CP(m2),&t);
                   1446:                     s = m1; m1 = NEXT(m1);
                   1447:                     if ( t ) {
                   1448:                         can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
                   1449:                     } else {
                   1450:                         can += 2; FREENM(s);
                   1451:                     }
                   1452:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1453: #endif
                   1454:                     break;
                   1455:                 case 1:
                   1456:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1457:                     break;
                   1458:                 case -1:
                   1459:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1460:                     break;
                   1461:             }
                   1462:         }
                   1463:         if ( !mr0 )
                   1464:             if ( m1 ) mr0 = m1;
                   1465:             else if ( m2 ) mr0 = m2;
                   1466:             else return 0;
                   1467:         else if ( m1 ) NEXT(mr) = m1;
                   1468:         else if ( m2 ) NEXT(mr) = m2;
                   1469:         else NEXT(mr) = 0;
                   1470:         BDY(p1) = mr0;
                   1471:         SG(p1) = MAX(SG(p1),SG(p2));
                   1472:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1473:         FREEND(p2);
                   1474:         return p1;
                   1475:     }
                   1476: }
                   1477:
                   1478: ND nd_add_sf(ND p1,ND p2)
                   1479: {
1.65    ! noro     1480:     int c,can;
1.1       noro     1481:     NM m1,m2,mr0,mr,s;
                   1482:     int t;
                   1483:
                   1484:     if ( !p1 ) return p2;
                   1485:     else if ( !p2 ) return p1;
                   1486:     else {
                   1487:         can = 0;
1.65    ! noro     1488:         for ( m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1.1       noro     1489:             c = DL_COMPARE(DL(m1),DL(m2));
                   1490:             switch ( c ) {
                   1491:                 case 0:
                   1492:                     t = _addsf(CM(m1),CM(m2));
                   1493:                     s = m1; m1 = NEXT(m1);
                   1494:                     if ( t ) {
                   1495:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1496:                     } else {
                   1497:                         can += 2; FREENM(s);
                   1498:                     }
                   1499:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1500:                     break;
                   1501:                 case 1:
                   1502:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1503:                     break;
                   1504:                 case -1:
                   1505:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1506:                     break;
                   1507:             }
                   1508:         }
                   1509:         if ( !mr0 )
                   1510:             if ( m1 ) mr0 = m1;
                   1511:             else if ( m2 ) mr0 = m2;
                   1512:             else return 0;
                   1513:         else if ( m1 ) NEXT(mr) = m1;
                   1514:         else if ( m2 ) NEXT(mr) = m2;
                   1515:         else NEXT(mr) = 0;
                   1516:         BDY(p1) = mr0;
                   1517:         SG(p1) = MAX(SG(p1),SG(p2));
                   1518:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1519:         FREEND(p2);
                   1520:         return p1;
                   1521:     }
                   1522: }
                   1523:
                   1524:
                   1525: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
                   1526: {
                   1527:     int c,c1,c2;
                   1528:     Z cg,cred,gcd,tq;
                   1529:     P cgp,credp,gcdp;
                   1530:     Obj tr,tr1;
                   1531:
                   1532:     if ( mod == -1 ) {
                   1533:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1534:         *divp = (Obj)ONE;
                   1535:     } else if ( mod == -2 ) {
                   1536:         Z inv,t;
                   1537:         divlf(ONE,HCZ(p),&inv);
                   1538:         chsgnlf(HCZ(g),&t);
                   1539:         mullf(inv,t,&CZ(mul));
                   1540:         *divp = (Obj)ONE;
                   1541:     } else if ( mod ) {
                   1542:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1543:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1544:         *divp = (Obj)ONE;
                   1545:     } else if ( nd_vc ) {
                   1546:         ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
                   1547:         divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
                   1548:         chsgnp(cgp,&CP(mul));
                   1549:         nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
                   1550:         if ( dn ) {
                   1551:             mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
                   1552:             reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
                   1553:         }
                   1554:         *divp = (Obj)credp;
                   1555:     } else {
1.6       noro     1556:         igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1557:         chsgnz(cg,&CZ(mul));
1.1       noro     1558:         nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
                   1559:         if ( dn ) {
                   1560:             mulz(dn->z,cred,&tq); dn->z = tq;
                   1561:         }
                   1562:         *divp = (Obj)cred;
                   1563:     }
                   1564:     return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
                   1565: }
                   1566:
                   1567: /* ret=1 : success, ret=0 : overflow */
1.6       noro     1568: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1       noro     1569: {
1.65    ! noro     1570:     NM m,tail;
1.1       noro     1571:     NM mul;
1.65    ! noro     1572:     int n,sugar,index;
1.1       noro     1573:     RHist h;
1.65    ! noro     1574:     NDV p;
1.1       noro     1575:     Z iq;
                   1576:     DP dmul;
                   1577:     NODE node;
                   1578:     LIST hist;
                   1579:     double hmag;
1.65    ! noro     1580:     Obj div;
1.1       noro     1581:     union oNDC hg;
                   1582:     P cont;
                   1583:
                   1584:     if ( !g ) {
                   1585:         *rp = d;
                   1586:         return 1;
                   1587:     }
                   1588:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1589:
1.65    ! noro     1590:     sugar = SG(g);
1.1       noro     1591:     n = NV(g);
                   1592:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1593:     if ( d )
                   1594:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1595:     for ( ; g; ) {
                   1596:         index = ndl_find_reducer(HDL(g));
                   1597:         if ( index >= 0 ) {
                   1598:             h = nd_psh[index];
                   1599:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1600:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1601:                 nd_free(g); nd_free(d);
                   1602:                 return 0;
                   1603:             }
                   1604:             p = nd_demand ? ndv_load(index) : ps[index];
                   1605:             /* d+g -> div*(d+g)+mul*p */
1.6       noro     1606:             g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1       noro     1607:             if ( nd_gentrace ) {
                   1608:                 /* Trace=[div,index,mul,ONE] */
1.6       noro     1609:                 STOZ(index,iq);
1.1       noro     1610:                 nmtodp(mod,mul,&dmul);
                   1611:                 node = mknode(4,div,iq,dmul,ONE);
                   1612:             }
1.65    ! noro     1613:             sugar = MAX(sugar,(int)(SG(p)+TD(DL(mul))));
1.1       noro     1614:             if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
                   1615:                 hg = HCU(g);
                   1616:                 nd_removecont2(d,g);
1.6       noro     1617:                 if ( nd_gentrace ) {
1.1       noro     1618:                     /* overwrite cont : Trace=[div,index,mul,cont] */
1.6       noro     1619:                     /* exact division */
1.1       noro     1620:                     cont = ndc_div(mod,hg,HCU(g));
                   1621:                     if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
                   1622:                 }
                   1623:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1624:             }
                   1625:             MKLIST(hist,node);
                   1626:             MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
                   1627:         } else if ( !full ) {
                   1628:             *rp = g;
                   1629:             return 1;
                   1630:         } else {
                   1631:             m = BDY(g);
                   1632:             if ( NEXT(m) ) {
                   1633:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1634:             } else {
                   1635:                 FREEND(g); g = 0;
                   1636:             }
                   1637:             if ( d ) {
                   1638:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1639:             } else {
                   1640:                 MKND(n,m,1,d); tail = BDY(d);
                   1641:             }
                   1642:         }
                   1643:     }
                   1644:     if ( d ) SG(d) = sugar;
                   1645:     *rp = d;
                   1646:     return 1;
                   1647: }
                   1648:
1.24      noro     1649: // ret=1 => success
                   1650: // ret=0 => overflow
                   1651: // ret=-1 => singular top reducible
                   1652:
                   1653: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
                   1654: {
1.65    ! noro     1655:     NM m,tail;
1.24      noro     1656:     NM mul;
1.65    ! noro     1657:     int n,sugar,index;
1.24      noro     1658:     RHist h;
1.65    ! noro     1659:     NDV p;
1.24      noro     1660:     Z iq;
                   1661:     DP dmul;
                   1662:     NODE node;
                   1663:     LIST hist;
                   1664:     double hmag;
1.65    ! noro     1665:     Obj div;
        !          1666: //    union oNDC hg;
1.24      noro     1667:     SIG sig;
                   1668:
                   1669:     if ( !g ) {
                   1670:         *rp = d;
                   1671:         return 1;
                   1672:     }
                   1673:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1674:
1.65    ! noro     1675:     sugar = SG(g);
1.24      noro     1676:     n = NV(g);
                   1677:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1678:     if ( d )
                   1679:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1680:     sig = g->sig;
                   1681:     for ( ; g; ) {
                   1682:         index = ndl_find_reducer_s(HDL(g),sig);
                   1683:         if ( index >= 0 && index < nd_psn ) {
                   1684:             // reducer found
                   1685:             h = nd_psh[index];
                   1686:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1687:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1688:                 nd_free(g); nd_free(d);
                   1689:                 return 0;
                   1690:             }
                   1691:             p = ps[index];
                   1692:             /* d+g -> div*(d+g)+mul*p */
                   1693:             g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.65    ! noro     1694:             sugar = MAX(sugar,(int)(SG(p)+TD(DL(mul))));
1.24      noro     1695:             if ( !mod && g && ((double)(p_mag(HCP(g))) > hmag) ) {
1.65    ! noro     1696: //                hg = HCU(g);
1.24      noro     1697:                 nd_removecont2(d,g);
                   1698:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1699:             }
1.55      noro     1700:             if ( nd_gentrace ) {
                   1701:                 /* Trace=[div,index,mul,ONE] */
                   1702:                 STOZ(index,iq);
                   1703:                 nmtodp(mod,mul,&dmul);
                   1704:                 node = mknode(4,div,iq,dmul,ONE);
                   1705:             }
                   1706:             MKLIST(hist,node);
                   1707:             MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1.24      noro     1708:         } else if ( index == -1 ) {
                   1709:           // singular top reducible
                   1710:           return -1;
                   1711:         } else if ( !full ) {
                   1712:             *rp = g;
                   1713:             g->sig = sig;
                   1714:             return 1;
                   1715:         } else {
                   1716:             m = BDY(g);
                   1717:             if ( NEXT(m) ) {
                   1718:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1719:             } else {
                   1720:                 FREEND(g); g = 0;
                   1721:             }
                   1722:             if ( d ) {
                   1723:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1724:             } else {
                   1725:                 MKND(n,m,1,d); tail = BDY(d);
                   1726:             }
                   1727:         }
                   1728:     }
                   1729:     if ( d ) {
                   1730:       SG(d) = sugar;
                   1731:       d->sig = sig;
                   1732:     }
                   1733:     *rp = d;
                   1734:     return 1;
                   1735: }
                   1736:
1.1       noro     1737: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
                   1738: {
                   1739:     int hindex,index;
                   1740:     NDV p;
1.65    ! noro     1741:     ND d,red;
        !          1742:     NM mul,m,tail;
        !          1743:     int sugar,psugar,n;
1.1       noro     1744:     PGeoBucket bucket;
                   1745:     int c,c1,c2;
1.65    ! noro     1746:     Z cg,cred,gcd;
1.1       noro     1747:     RHist h;
                   1748:     double hmag,gmag;
                   1749:     int count = 0;
                   1750:     int hcount = 0;
                   1751:
                   1752:     if ( !g ) {
                   1753:         *rp = 0;
                   1754:         return 1;
                   1755:     }
                   1756:     sugar = SG(g);
                   1757:     n = NV(g);
1.6       noro     1758:     if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1       noro     1759:     bucket = create_pbucket();
                   1760:     add_pbucket(mod,bucket,g);
                   1761:     d = 0;
                   1762:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1763:     while ( 1 ) {
                   1764:         if ( mod > 0 || mod == -1 )
                   1765:           hindex = head_pbucket(mod,bucket);
                   1766:         else if ( mod == -2 )
                   1767:           hindex = head_pbucket_lf(bucket);
                   1768:         else
                   1769:           hindex = head_pbucket_q(bucket);
                   1770:         if ( hindex < 0 ) {
                   1771:             if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1772:             if ( d ) SG(d) = sugar;
                   1773:             *rp = d;
                   1774:             return 1;
                   1775:         }
                   1776:         g = bucket->body[hindex];
                   1777:         index = ndl_find_reducer(HDL(g));
                   1778:         if ( index >= 0 ) {
                   1779:             count++;
                   1780:             if ( !d ) hcount++;
                   1781:             h = nd_psh[index];
                   1782:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1783:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1784:                 nd_free(d);
                   1785:                 free_pbucket(bucket);
                   1786:                 *rp = 0;
                   1787:                 return 0;
                   1788:             }
                   1789:             p = ps[index];
                   1790:             if ( mod == -1 )
                   1791:                 CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1792:             else if ( mod == -2 ) {
                   1793:                 Z inv,t;
                   1794:                 divlf(ONE,HCZ(p),&inv);
                   1795:                 chsgnlf(HCZ(g),&t);
                   1796:                 mullf(inv,t,&CZ(mul));
                   1797:             } else if ( mod ) {
                   1798:                 c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1799:                 DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1800:             } else {
1.6       noro     1801:                 igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1802:                 chsgnz(cg,&CZ(mul));
1.1       noro     1803:                 nd_mul_c_q(d,(P)cred);
                   1804:                 mulq_pbucket(bucket,cred);
                   1805:                 g = bucket->body[hindex];
1.6       noro     1806:                 gmag = (double)p_mag((P)HCZ(g));
1.1       noro     1807:             }
                   1808:             red = ndv_mul_nm(mod,mul,p);
                   1809:             bucket->body[hindex] = nd_remove_head(g);
                   1810:             red = nd_remove_head(red);
                   1811:             add_pbucket(mod,bucket,red);
                   1812:             psugar = SG(p)+TD(DL(mul));
                   1813:             sugar = MAX(sugar,psugar);
                   1814:             if ( !mod && hmag && (gmag > hmag) ) {
                   1815:                 g = normalize_pbucket(mod,bucket);
                   1816:                 if ( !g ) {
                   1817:                     if ( d ) SG(d) = sugar;
                   1818:                     *rp = d;
                   1819:                     return 1;
                   1820:                 }
                   1821:                 nd_removecont2(d,g);
1.6       noro     1822:                 hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1       noro     1823:                 add_pbucket(mod,bucket,g);
                   1824:             }
                   1825:         } else if ( !full ) {
                   1826:             g = normalize_pbucket(mod,bucket);
                   1827:             if ( g ) SG(g) = sugar;
                   1828:             *rp = g;
                   1829:             return 1;
                   1830:         } else {
                   1831:             m = BDY(g);
                   1832:             if ( NEXT(m) ) {
                   1833:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1834:             } else {
                   1835:                 FREEND(g); g = 0;
                   1836:             }
                   1837:             bucket->body[hindex] = g;
                   1838:             NEXT(m) = 0;
                   1839:             if ( d ) {
                   1840:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1841:             } else {
                   1842:                 MKND(n,m,1,d); tail = BDY(d);
                   1843:             }
                   1844:         }
                   1845:     }
                   1846: }
                   1847:
1.25      noro     1848: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *rp)
                   1849: {
                   1850:   int hindex,index;
                   1851:   NDV p;
1.65    ! noro     1852:   ND d,red;
        !          1853:   NM mul,m,tail;
        !          1854:   int sugar,psugar,n;
1.25      noro     1855:   PGeoBucket bucket;
                   1856:   int c,c1,c2;
1.65    ! noro     1857:   Z cg,cred,gcd;
1.25      noro     1858:   RHist h;
                   1859:   double hmag,gmag;
                   1860:   int count = 0;
                   1861:   int hcount = 0;
                   1862:   SIG sig;
                   1863:
                   1864:   if ( !g ) {
                   1865:     *rp = 0;
                   1866:     return 1;
                   1867:   }
                   1868:   sugar = SG(g);
                   1869:   n = NV(g);
                   1870:   if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
                   1871:   bucket = create_pbucket();
                   1872:   add_pbucket(mod,bucket,g);
                   1873:   d = 0;
                   1874:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1875:   sig = g->sig;
                   1876:   while ( 1 ) {
                   1877:     if ( mod > 0 || mod == -1 )
                   1878:       hindex = head_pbucket(mod,bucket);
                   1879:     else if ( mod == -2 )
                   1880:       hindex = head_pbucket_lf(bucket);
                   1881:     else
                   1882:       hindex = head_pbucket_q(bucket);
                   1883:     if ( hindex < 0 ) {
                   1884:       if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1885:       if ( d ) {
                   1886:         SG(d) = sugar;
                   1887:         d->sig = sig;
                   1888:       }
                   1889:       *rp = d;
                   1890:       return 1;
                   1891:     }
                   1892:     g = bucket->body[hindex];
                   1893:     index = ndl_find_reducer_s(HDL(g),sig);
                   1894:     if ( index >= 0 && index < nd_psn ) {
                   1895:       count++;
                   1896:       if ( !d ) hcount++;
                   1897:       h = nd_psh[index];
                   1898:       ndl_sub(HDL(g),DL(h),DL(mul));
                   1899:       if ( ndl_check_bound2(index,DL(mul)) ) {
                   1900:         nd_free(d);
                   1901:         free_pbucket(bucket);
                   1902:         *rp = 0;
                   1903:         return 0;
                   1904:       }
                   1905:       p = ps[index];
                   1906:       if ( mod == -1 )
                   1907:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1908:       else if ( mod == -2 ) {
                   1909:         Z inv,t;
                   1910:         divlf(ONE,HCZ(p),&inv);
                   1911:         chsgnlf(HCZ(g),&t);
                   1912:         mullf(inv,t,&CZ(mul));
                   1913:       } else if ( mod ) {
                   1914:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1915:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1916:       } else {
                   1917:         igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1918:         chsgnz(cg,&CZ(mul));
                   1919:         nd_mul_c_q(d,(P)cred);
                   1920:         mulq_pbucket(bucket,cred);
                   1921:         g = bucket->body[hindex];
                   1922:         gmag = (double)p_mag((P)HCZ(g));
                   1923:       }
                   1924:       red = ndv_mul_nm(mod,mul,p);
                   1925:       bucket->body[hindex] = nd_remove_head(g);
                   1926:       red = nd_remove_head(red);
                   1927:       add_pbucket(mod,bucket,red);
                   1928:       psugar = SG(p)+TD(DL(mul));
                   1929:       sugar = MAX(sugar,psugar);
                   1930:       if ( !mod && hmag && (gmag > hmag) ) {
                   1931:          g = normalize_pbucket(mod,bucket);
                   1932:          if ( !g ) {
                   1933:            if ( d ) {
                   1934:              SG(d) = sugar;
                   1935:              d->sig = sig;
                   1936:            }
                   1937:            *rp = d;
                   1938:            return 1;
                   1939:          }
                   1940:          nd_removecont2(d,g);
                   1941:          hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
                   1942:          add_pbucket(mod,bucket,g);
                   1943:       }
                   1944:     } else if ( index == -1 ) {
                   1945:       // singular top reducible
                   1946:       return -1;
                   1947:     } else if ( !full ) {
                   1948:       g = normalize_pbucket(mod,bucket);
                   1949:       if ( g ) {
                   1950:         SG(g) = sugar;
                   1951:         g->sig = sig;
                   1952:       }
                   1953:       *rp = g;
                   1954:       return 1;
                   1955:     } else {
                   1956:       m = BDY(g);
                   1957:       if ( NEXT(m) ) {
                   1958:         BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1959:       } else {
                   1960:         FREEND(g); g = 0;
                   1961:       }
                   1962:       bucket->body[hindex] = g;
                   1963:       NEXT(m) = 0;
                   1964:       if ( d ) {
                   1965:         NEXT(tail)=m; tail=m; LEN(d)++;
                   1966:       } else {
                   1967:         MKND(n,m,1,d); tail = BDY(d);
                   1968:       }
                   1969:     }
                   1970:   }
                   1971: }
                   1972:
1.1       noro     1973: /* input : list of NDV, cand : list of NDV */
                   1974:
                   1975: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
                   1976: {
1.65    ! noro     1977:     int i,stat;
1.1       noro     1978:     ND nf,d;
                   1979:     NDV r;
                   1980:     NODE t,s;
                   1981:     Z q;
                   1982:     LIST list;
                   1983:
1.24      noro     1984:     ndv_setup(m,0,cand,nd_gentrace?1:0,1,0);
1.65    ! noro     1985: //    n = length(cand);
1.1       noro     1986:
                   1987:   if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
                   1988:     /* membercheck : list is a subset of Id(cand) ? */
                   1989:     for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
                   1990: again:
                   1991:     nd_tracelist = 0;
                   1992:         if ( nd_bpe > obpe )
                   1993:             r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
                   1994:         else
                   1995:             r = (NDV)BDY(t);
                   1996: #if 0
                   1997:         // moved to nd_f4_lf_trace()
                   1998:         if ( m == -2 ) ndv_mod(m,r);
                   1999: #endif
                   2000:         d = ndvtond(m,r);
1.6       noro     2001:         stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1       noro     2002:         if ( !stat ) {
                   2003:             nd_reconstruct(0,0);
                   2004:             goto again;
                   2005:         } else if ( nf ) return 0;
                   2006:     if ( nd_gentrace ) {
                   2007:       nd_tracelist = reverse_node(nd_tracelist);
                   2008:       MKLIST(list,nd_tracelist);
1.6       noro     2009:       STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1       noro     2010:       MKNODE(s,list,nd_alltracelist);
                   2011:       nd_alltracelist = s; nd_tracelist = 0;
                   2012:     }
                   2013:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   2014:     }
                   2015:     if ( DP_Print ) { printf("\n"); }
                   2016:     return 1;
                   2017: }
                   2018:
                   2019: ND nd_remove_head(ND p)
                   2020: {
                   2021:     NM m;
                   2022:
                   2023:     m = BDY(p);
                   2024:     if ( !NEXT(m) ) {
                   2025:         FREEND(p); p = 0;
                   2026:     } else {
                   2027:         BDY(p) = NEXT(m); LEN(p)--;
                   2028:     }
                   2029:     FREENM(m);
                   2030:     return p;
                   2031: }
                   2032:
                   2033: ND nd_separate_head(ND p,ND *head)
                   2034: {
                   2035:     NM m,m0;
                   2036:     ND r;
                   2037:
                   2038:     m = BDY(p);
                   2039:     if ( !NEXT(m) ) {
                   2040:         *head = p; p = 0;
                   2041:     } else {
                   2042:         m0 = m;
                   2043:         BDY(p) = NEXT(m); LEN(p)--;
                   2044:         NEXT(m0) = 0;
                   2045:         MKND(NV(p),m0,1,r);
                   2046:         *head = r;
                   2047:     }
                   2048:     return p;
                   2049: }
                   2050:
                   2051: PGeoBucket create_pbucket()
                   2052: {
                   2053:     PGeoBucket g;
                   2054:
                   2055:     g = CALLOC(1,sizeof(struct oPGeoBucket));
                   2056:     g->m = -1;
                   2057:     return g;
                   2058: }
                   2059:
                   2060: void free_pbucket(PGeoBucket b) {
                   2061:     int i;
                   2062:
                   2063:     for ( i = 0; i <= b->m; i++ )
                   2064:         if ( b->body[i] ) {
                   2065:             nd_free(b->body[i]);
                   2066:             b->body[i] = 0;
                   2067:         }
                   2068:     GCFREE(b);
                   2069: }
                   2070:
1.39      noro     2071: #if 0
1.1       noro     2072: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   2073: {
                   2074:     int l,i,k,m;
                   2075:
                   2076:     if ( !d )
                   2077:         return;
                   2078:     l = LEN(d);
                   2079:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2080:     /* 2^(k-1) < l <= 2^k (=m) */
                   2081:     d = nd_merge(g->body[k],d);
                   2082:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   2083:         g->body[k] = 0;
                   2084:         d = nd_merge(g->body[k+1],d);
                   2085:     }
                   2086:     g->body[k] = d;
                   2087:     g->m = MAX(g->m,k);
                   2088: }
1.39      noro     2089: #else
                   2090: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   2091: {
1.65    ! noro     2092:   int l,k,m,m0;
1.39      noro     2093:
                   2094:   if ( !d )
                   2095:     return;
                   2096:   m0 = g->m;
                   2097:   while ( 1 ) {
                   2098:     l = LEN(d);
                   2099:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2100:     /* 2^(k-1) < l <= 2^k (=m) */
                   2101:     if ( g->body[k] == 0 ) {
                   2102:       g->body[k] = d;
                   2103:       m0 = MAX(k,m0);
                   2104:       break;
                   2105:     } else {
                   2106:       d = nd_merge(g->body[k],d);
                   2107:       g->body[k] = 0;
                   2108:     }
                   2109:   }
                   2110:   g->m = m0;
                   2111: }
                   2112: #endif
1.1       noro     2113:
1.39      noro     2114: #if 0
1.1       noro     2115: void add_pbucket(int mod,PGeoBucket g,ND d)
                   2116: {
                   2117:     int l,i,k,m;
                   2118:
                   2119:     if ( !d )
                   2120:         return;
                   2121:     l = LEN(d);
                   2122:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2123:     /* 2^(k-1) < l <= 2^k (=m) */
                   2124:     d = nd_add(mod,g->body[k],d);
                   2125:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   2126:         g->body[k] = 0;
                   2127:         d = nd_add(mod,g->body[k+1],d);
                   2128:     }
                   2129:     g->body[k] = d;
                   2130:     g->m = MAX(g->m,k);
                   2131: }
1.39      noro     2132: #else
                   2133: void add_pbucket(int mod,PGeoBucket g,ND d)
                   2134: {
1.65    ! noro     2135:   int l,k,m,m0;
1.39      noro     2136:
                   2137:   m0 = g->m;
                   2138:   while ( d != 0 ) {
                   2139:     l = LEN(d);
                   2140:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2141:     /* 2^(k-1) < l <= 2^k (=m) */
                   2142:     if ( g->body[k] == 0 ) {
                   2143:       g->body[k] = d;
                   2144:       m0 = MAX(k,m0);
                   2145:       break;
                   2146:     } else {
                   2147:       d = nd_add(mod,g->body[k],d);
                   2148:       g->body[k] = 0;
                   2149:     }
                   2150:   }
                   2151:   g->m = m0;
                   2152: }
                   2153: #endif
1.1       noro     2154:
                   2155: void mulq_pbucket(PGeoBucket g,Z c)
                   2156: {
                   2157:     int k;
                   2158:
                   2159:     for ( k = 0; k <= g->m; k++ )
                   2160:         nd_mul_c_q(g->body[k],(P)c);
                   2161: }
                   2162:
                   2163: NM remove_head_pbucket_symbolic(PGeoBucket g)
                   2164: {
                   2165:     int j,i,k,c;
                   2166:     NM head;
                   2167:
                   2168:     k = g->m;
                   2169:     j = -1;
                   2170:     for ( i = 0; i <= k; i++ ) {
                   2171:         if ( !g->body[i] ) continue;
                   2172:         if ( j < 0 ) j = i;
                   2173:         else {
                   2174:             c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
                   2175:             if ( c > 0 )
                   2176:                 j = i;
                   2177:             else if ( c == 0 )
                   2178:                 g->body[i] = nd_remove_head(g->body[i]);
                   2179:         }
                   2180:     }
                   2181:     if ( j < 0 ) return 0;
                   2182:     else {
                   2183:         head = BDY(g->body[j]);
                   2184:         if ( !NEXT(head) ) {
                   2185:             FREEND(g->body[j]);
                   2186:             g->body[j] = 0;
                   2187:         } else {
                   2188:             BDY(g->body[j]) = NEXT(head);
                   2189:             LEN(g->body[j])--;
                   2190:         }
                   2191:         return head;
                   2192:     }
                   2193: }
                   2194:
                   2195: int head_pbucket(int mod,PGeoBucket g)
                   2196: {
1.65    ! noro     2197:     int j,i,c,k,sum;
        !          2198:     UINT *dj;
1.1       noro     2199:     ND gi,gj;
                   2200:
                   2201:     k = g->m;
                   2202:     while ( 1 ) {
                   2203:         j = -1;
                   2204:         for ( i = 0; i <= k; i++ ) {
                   2205:             if ( !(gi = g->body[i]) )
                   2206:                 continue;
                   2207:             if ( j < 0 ) {
                   2208:                 j = i;
                   2209:                 gj = g->body[j];
                   2210:                 dj = HDL(gj);
                   2211:                 sum = HCM(gj);
                   2212:             } else {
                   2213:                 c = DL_COMPARE(HDL(gi),dj);
                   2214:                 if ( c > 0 ) {
                   2215:                     if ( sum ) HCM(gj) = sum;
                   2216:                     else g->body[j] = nd_remove_head(gj);
                   2217:                     j = i;
                   2218:                     gj = g->body[j];
                   2219:                     dj = HDL(gj);
                   2220:                     sum = HCM(gj);
                   2221:                 } else if ( c == 0 ) {
                   2222:                     if ( mod == -1 )
                   2223:                         sum = _addsf(sum,HCM(gi));
                   2224:                     else {
                   2225:                         sum = sum+HCM(gi)-mod;
                   2226:                         if ( sum < 0 ) sum += mod;
                   2227:                     }
                   2228:                     g->body[i] = nd_remove_head(gi);
                   2229:                 }
                   2230:             }
                   2231:         }
                   2232:         if ( j < 0 ) return -1;
                   2233:         else if ( sum ) {
                   2234:             HCM(gj) = sum;
                   2235:             return j;
                   2236:         } else
                   2237:             g->body[j] = nd_remove_head(gj);
                   2238:     }
                   2239: }
                   2240:
                   2241: int head_pbucket_q(PGeoBucket g)
                   2242: {
1.65    ! noro     2243:     int j,i,c,k;
1.1       noro     2244:     Z sum,t;
                   2245:     ND gi,gj;
                   2246:
                   2247:     k = g->m;
                   2248:     while ( 1 ) {
                   2249:         j = -1;
                   2250:         for ( i = 0; i <= k; i++ ) {
                   2251:             if ( !(gi = g->body[i]) ) continue;
                   2252:             if ( j < 0 ) {
                   2253:                 j = i;
                   2254:                 gj = g->body[j];
1.6       noro     2255:                 sum = HCZ(gj);
1.1       noro     2256:             } else {
1.65    ! noro     2257: //               nv = NV(gi);
1.1       noro     2258:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   2259:                 if ( c > 0 ) {
1.6       noro     2260:                     if ( sum ) HCZ(gj) = sum;
1.1       noro     2261:                     else g->body[j] = nd_remove_head(gj);
                   2262:                     j = i;
                   2263:                     gj = g->body[j];
1.6       noro     2264:                     sum = HCZ(gj);
1.1       noro     2265:                 } else if ( c == 0 ) {
1.6       noro     2266:                     addz(sum,HCZ(gi),&t);
1.1       noro     2267:                     sum = t;
                   2268:                     g->body[i] = nd_remove_head(gi);
                   2269:                 }
                   2270:             }
                   2271:         }
                   2272:         if ( j < 0 ) return -1;
                   2273:         else if ( sum ) {
1.6       noro     2274:             HCZ(gj) = sum;
1.1       noro     2275:             return j;
                   2276:         } else
                   2277:             g->body[j] = nd_remove_head(gj);
                   2278:     }
                   2279: }
                   2280:
                   2281: int head_pbucket_lf(PGeoBucket g)
                   2282: {
1.65    ! noro     2283:     int j,i,c,k;
1.1       noro     2284:     Z sum,t;
                   2285:     ND gi,gj;
                   2286:
                   2287:     k = g->m;
                   2288:     while ( 1 ) {
                   2289:         j = -1;
                   2290:         for ( i = 0; i <= k; i++ ) {
                   2291:             if ( !(gi = g->body[i]) ) continue;
                   2292:             if ( j < 0 ) {
                   2293:                 j = i;
                   2294:                 gj = g->body[j];
                   2295:                 sum = HCZ(gj);
                   2296:             } else {
1.65    ! noro     2297: //              nv = NV(gi);
1.1       noro     2298:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   2299:                 if ( c > 0 ) {
                   2300:                     if ( sum ) HCZ(gj) = sum;
                   2301:                     else g->body[j] = nd_remove_head(gj);
                   2302:                     j = i;
                   2303:                     gj = g->body[j];
                   2304:                     sum = HCZ(gj);
                   2305:                 } else if ( c == 0 ) {
                   2306:                     addlf(sum,HCZ(gi),&t);
                   2307:                     sum = t;
                   2308:                     g->body[i] = nd_remove_head(gi);
                   2309:                 }
                   2310:             }
                   2311:         }
                   2312:         if ( j < 0 ) return -1;
                   2313:         else if ( sum ) {
                   2314:             HCZ(gj) = sum;
                   2315:             return j;
                   2316:         } else
                   2317:             g->body[j] = nd_remove_head(gj);
                   2318:     }
                   2319: }
                   2320:
                   2321: ND normalize_pbucket(int mod,PGeoBucket g)
                   2322: {
                   2323:     int i;
1.65    ! noro     2324:     ND r;
1.1       noro     2325:
                   2326:     r = 0;
                   2327:     for ( i = 0; i <= g->m; i++ ) {
                   2328:         r = nd_add(mod,r,g->body[i]);
                   2329:         g->body[i] = 0;
                   2330:     }
                   2331:     g->m = -1;
                   2332:     return r;
                   2333: }
                   2334:
                   2335: #if 0
                   2336: void register_hcf(NDV p)
                   2337: {
                   2338:     DCP dc,t;
                   2339:     P hc,h;
                   2340:     int c;
                   2341:     NODE l,l1,prev;
                   2342:
                   2343:     hc = p->body->c.p;
                   2344:     if ( !nd_vc || NUM(hc) ) return;
                   2345:     fctrp(nd_vc,hc,&dc);
                   2346:     for ( t = dc; t; t = NEXT(t) ) {
                   2347:         h = t->c;
                   2348:         if ( NUM(h) ) continue;
                   2349:         for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   2350:             c = compp(nd_vc,h,(P)BDY(l));
                   2351:             if ( c >= 0 ) break;
                   2352:         }
                   2353:         if ( !l || c > 0  ) {
                   2354:             MKNODE(l1,h,l);
                   2355:             if ( !prev )
                   2356:                 nd_hcf = l1;
                   2357:             else
                   2358:                 NEXT(prev) = l1;
                   2359:         }
                   2360:     }
                   2361: }
                   2362: #else
                   2363: void register_hcf(NDV p)
                   2364: {
                   2365:     P hc,h,q;
                   2366:     Q dmy;
                   2367:     int c;
                   2368:     NODE l,l1,prev;
                   2369:
                   2370:     hc = p->body->c.p;
                   2371:     if ( NUM(hc) ) return;
                   2372:     ptozp(hc,1,&dmy,&h);
                   2373: #if 1
                   2374:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   2375:         while ( 1 ) {
                   2376:             if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
                   2377:             else break;
                   2378:         }
                   2379:     }
                   2380:     if ( NUM(h) ) return;
                   2381: #endif
                   2382:     for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   2383:         c = compp(nd_vc,h,(P)BDY(l));
                   2384:         if ( c >= 0 ) break;
                   2385:     }
                   2386:     if ( !l || c > 0  ) {
                   2387:         MKNODE(l1,h,l);
                   2388:         if ( !prev )
                   2389:             nd_hcf = l1;
                   2390:         else
                   2391:             NEXT(prev) = l1;
                   2392:     }
                   2393: }
                   2394: #endif
                   2395:
                   2396: int do_diagonalize(int sugar,int m)
                   2397: {
1.65    ! noro     2398:   int i,stat;
        !          2399:   ND nf,s,head;
1.6       noro     2400:   NDV nfv;
1.65    ! noro     2401:   P cont;
1.6       noro     2402:   union oNDC hc;
                   2403:   NODE node;
                   2404:   LIST l;
                   2405:   Z iq;
1.1       noro     2406:
1.64      noro     2407:   if ( diag_period == 0 ) return 1;
1.6       noro     2408:   for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   2409:     if ( nd_gentrace ) {
                   2410:       /* Trace = [1,index,1,1] */
                   2411:       STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2412:       MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2413:     }
                   2414:     if ( nd_demand )
                   2415:       nfv = ndv_load(i);
                   2416:     else
                   2417:       nfv = nd_ps[i];
                   2418:     s = ndvtond(m,nfv);
                   2419:     s = nd_separate_head(s,&head);
                   2420:     stat = nd_nf(m,head,s,nd_ps,1,&nf);
                   2421:     if ( !stat ) return 0;
                   2422:     ndv_free(nfv);
                   2423:     hc = HCU(nf); nd_removecont(m,nf);
                   2424:     /* exact division */
                   2425:     cont = ndc_div(m,hc,HCU(nf));
1.1       noro     2426:     if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6       noro     2427:     nfv = ndtondv(m,nf);
                   2428:     nd_free(nf);
                   2429:     nd_bound[i] = ndv_compute_bound(nfv);
                   2430:     if ( !m ) register_hcf(nfv);
                   2431:     if ( nd_demand ) {
                   2432:       ndv_save(nfv,i);
                   2433:       ndv_free(nfv);
                   2434:     } else
                   2435:       nd_ps[i] = nfv;
                   2436:   }
                   2437:   return 1;
1.1       noro     2438: }
                   2439:
                   2440: LIST compute_splist()
                   2441: {
                   2442:   NODE g,tn0,tn,node;
                   2443:   LIST l0;
                   2444:   ND_pairs d,t;
                   2445:   int i;
                   2446:   Z i1,i2;
                   2447:
                   2448:     g = 0; d = 0;
                   2449:     for ( i = 0; i < nd_psn; i++ ) {
                   2450:         d = update_pairs(d,g,i,0);
                   2451:         g = update_base(g,i);
                   2452:     }
                   2453:   for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
                   2454:     NEXTNODE(tn0,tn);
1.6       noro     2455:         STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1       noro     2456:         node = mknode(2,i1,i2); MKLIST(l0,node);
                   2457:     BDY(tn) = l0;
                   2458:   }
                   2459:   if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
                   2460:   return l0;
                   2461: }
                   2462:
1.49      noro     2463: typedef struct oHPDATA {
1.56      noro     2464:   int n;
                   2465:   P hn; // HP(t)=hn(t)/((1-t^w0)*...*(1-t^w(n-1)))
1.49      noro     2466:   VECT x; // BDY(x)[i] = <<0,...,1,...,0>>
1.56      noro     2467:   int *w;
1.49      noro     2468: } *HPDATA;
                   2469:
                   2470: void make_reduced(VECT b,int nv);
                   2471: void mhp_rec(VECT b,VECT x,P t,P *r);
                   2472: P mhp_ctop(P *r,P *plist,int n);
                   2473: DL monomial_colon(DL a,DL b,int n);
1.56      noro     2474: LIST dp_monomial_hilbert_poincare(VECT b,VECT x);
                   2475: LIST dp_monomial_hilbert_poincare_weight(VECT b,VECT x,int *w);
1.49      noro     2476:
                   2477: void setup_hpdata(HPDATA final,HPDATA current)
                   2478: {
1.65    ! noro     2479:   int n,i;
1.49      noro     2480:   DL *p;
1.56      noro     2481:   VECT b,x;
1.49      noro     2482:   DL dl;
1.56      noro     2483:   LIST ret;
                   2484:   int *w;
                   2485:
                   2486:   final->n = n = nd_nvar;
                   2487:   final->hn = (P)BDY(nd_hpdata);
1.59      noro     2488: #if 0
1.56      noro     2489:   if ( NEXT(nd_hpdata) != 0 && (weight=(LIST)BDY(NEXT(nd_hpdata))) != 0 ) {
                   2490:     wlen = length(BDY(weight));
                   2491:     if ( n != wlen )
                   2492:       error("setup_hpdata : inconsistent weight length");
                   2493:     w = (int *)MALLOC(n*sizeof(int));
                   2494:     for ( i = 0, nd = BDY((LIST)weight); i < n; i++, nd = NEXT(nd) )
                   2495:       w[i] = ZTOS((Z)BDY(nd));
                   2496:   } else
                   2497:     w = 0;
1.59      noro     2498: #else
                   2499:   w = current_dl_weight_vector;
                   2500: #endif
1.49      noro     2501:   MKVECT(x,n);
                   2502:   for ( i = 0; i < n; i++ ) {
                   2503:     NEWDL(dl,n); dl->d[i] = 1; dl->td = 1; BDY(x)[i] = dl;
                   2504:   }
                   2505:   final->x = x;
1.56      noro     2506:   final->w = w;
1.49      noro     2507:
                   2508:   MKVECT(b,nd_psn); p = (DL *)BDY(b);
                   2509:   for ( i = 0; i < nd_psn; i++ ) {
                   2510:     p[i] = ndltodl(n,nd_psh[i]->dl);
                   2511:   }
1.56      noro     2512:   if ( w ) {
                   2513:     ret = dp_monomial_hilbert_poincare_weight(b,x,w);
                   2514:   } else
                   2515:     ret = dp_monomial_hilbert_poincare(b,x);
                   2516:   current->n = n;
                   2517:   current->hn = (P)BDY(BDY(ret));
1.49      noro     2518:   current->x = x;
1.56      noro     2519:   current->w = w;
                   2520: }
                   2521:
                   2522: int comp_hn(P a, P b)
                   2523: {
                   2524:   P s;
                   2525:   DCP dc;
                   2526:
                   2527:   subp(CO,a,b,&s);
1.57      noro     2528:   if ( !s ) return -1;
1.56      noro     2529:   else if ( OID(s) == 1 ) return 0;
                   2530:   else {
                   2531:     for ( dc = DC(s); NEXT(dc); dc = NEXT(dc) );
                   2532:     return (int)ZTOS((Z)dc->d);
                   2533:   }
1.49      noro     2534: }
                   2535:
1.56      noro     2536: void update_hpdata(HPDATA current,int nh)
1.49      noro     2537: {
1.65    ! noro     2538:   NODE data1;
        !          2539:   DL new;
        !          2540:   int i,n;
1.49      noro     2541:   Z dz;
                   2542:   DL *p;
1.65    ! noro     2543:   VECT b;
        !          2544:   P tv,td,s,hn;
1.49      noro     2545:   LIST list1;
                   2546:
                   2547:   n = nd_nvar;
                   2548:   new = ndltodl(n,nd_psh[nh]->dl);
                   2549:   MKVECT(b,nh); p = (DL *)BDY(b);
                   2550:   for ( i = 0; i < nh; i++ ) {
                   2551:     p[i] = monomial_colon(ndltodl(n,nd_psh[i]->dl),new,n);
                   2552:   }
                   2553:   // compute HP(I:new)
1.56      noro     2554:   if ( current->w )
                   2555:     list1 = dp_monomial_hilbert_poincare_weight(b,current->x,current->w);
                   2556:   else
                   2557:     list1 = dp_monomial_hilbert_poincare(b,current->x);
1.49      noro     2558:   data1 = BDY((LIST)list1);
                   2559:   // HP(I+<new>) = H(I)-t^d*H(I:new), d=tdeg(new)
                   2560:   makevar("t",&tv); UTOZ(new->td,dz);
                   2561:   pwrp(CO,tv,dz,&td);
                   2562:   mulp(CO,(P)ARG0(data1),td,&s);
                   2563:   subp(CO,current->hn,s,&hn);
                   2564:   current->hn = hn;
                   2565: }
                   2566:
                   2567: ND_pairs nd_remove_same_sugar( ND_pairs d, int sugar)
                   2568: {
                   2569:   struct oND_pairs root;
                   2570:   ND_pairs prev,cur;
1.61      noro     2571:   int i;
1.49      noro     2572:
                   2573:   root.next = d;
                   2574:   prev = &root; cur = d;
1.61      noro     2575:   i = 0;
1.49      noro     2576:   while ( cur ) {
1.61      noro     2577:     if ( SG(cur) == sugar ) {
1.49      noro     2578:       prev->next = cur->next;
1.61      noro     2579:       i++;
                   2580:     } else
1.49      noro     2581:       prev = cur;
                   2582:     cur = cur->next;
                   2583:   }
1.63      noro     2584:   if ( DP_Print && i ) fprintf(asir_out,"[%d]",i);
1.49      noro     2585:   return root.next;
                   2586: }
                   2587:
1.1       noro     2588: /* return value = 0 => input is not a GB */
                   2589:
                   2590: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
                   2591: {
1.6       noro     2592:   int i,nh,sugar,stat;
1.65    ! noro     2593:   NODE g,t;
1.6       noro     2594:   ND_pairs d;
                   2595:   ND_pairs l;
1.65    ! noro     2596:   ND h,nf;
1.6       noro     2597:   NDV nfv;
                   2598:   Z q;
1.65    ! noro     2599:   union oNDC hc;
1.6       noro     2600:   int diag_count = 0;
1.41      noro     2601:   int Nnfnz = 0,Nnfz = 0;
1.6       noro     2602:   P cont;
                   2603:   LIST list;
1.49      noro     2604:   struct oHPDATA current_hpdata,final_hpdata;
                   2605:
1.27      noro     2606: struct oEGT eg1,eg2,eg_update;
1.6       noro     2607:
1.27      noro     2608: init_eg(&eg_update);
1.11      noro     2609:   Nnd_add = 0;
1.6       noro     2610:   g = 0; d = 0;
                   2611:   for ( i = 0; i < nd_psn; i++ ) {
                   2612:     d = update_pairs(d,g,i,gensyz);
                   2613:     g = update_base(g,i);
                   2614:   }
                   2615:   sugar = 0;
1.49      noro     2616:   if ( nd_hpdata ) {
1.52      noro     2617:     if ( DP_Print ) fprintf(asir_out,"Hilbert driven algorithm.\n");
1.49      noro     2618:     setup_hpdata(&final_hpdata,&current_hpdata);
                   2619:   }
1.6       noro     2620:   while ( d ) {
1.1       noro     2621: again:
1.6       noro     2622:     l = nd_minp(d,&d);
                   2623:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   2624:     if ( SG(l) != sugar ) {
                   2625:       if ( ishomo ) {
                   2626:         diag_count = 0;
                   2627:         stat = do_diagonalize(sugar,m);
1.1       noro     2628:         if ( !stat ) {
1.6       noro     2629:           NEXT(l) = d; d = l;
                   2630:           d = nd_reconstruct(0,d);
                   2631:           goto again;
1.1       noro     2632:         }
1.6       noro     2633:       }
                   2634:       sugar = SG(l);
                   2635:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2636:     }
                   2637:     stat = nd_sp(m,0,l,&h);
                   2638:     if ( !stat ) {
                   2639:       NEXT(l) = d; d = l;
                   2640:       d = nd_reconstruct(0,d);
                   2641:       goto again;
                   2642:     }
1.1       noro     2643: #if USE_GEOBUCKET
1.39      noro     2644:     stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf)
                   2645:       :nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     2646: #else
1.39      noro     2647:     stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     2648: #endif
1.6       noro     2649:     if ( !stat ) {
                   2650:       NEXT(l) = d; d = l;
                   2651:       d = nd_reconstruct(0,d);
                   2652:       goto again;
                   2653:     } else if ( nf ) {
1.41      noro     2654:       Nnfnz++;
1.6       noro     2655:       if ( checkonly || gensyz ) return 0;
1.1       noro     2656:       if ( nd_newelim ) {
                   2657:         if ( nd_module ) {
                   2658:           if ( MPOS(HDL(nf)) > 1 ) return 0;
                   2659:         } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
                   2660:       }
1.6       noro     2661:       if ( DP_Print ) { printf("+"); fflush(stdout); }
                   2662:       hc = HCU(nf);
                   2663:       nd_removecont(m,nf);
                   2664:       if ( !m && nd_nalg ) {
                   2665:         nd_monic(0,&nf);
                   2666:         nd_removecont(m,nf);
                   2667:       }
                   2668:       if ( nd_gentrace ) {
                   2669:         /* exact division */
1.1       noro     2670:         cont = ndc_div(m,hc,HCU(nf));
                   2671:         if ( m || !UNIQ(cont) ) {
1.6       noro     2672:           t = mknode(4,NULLP,NULLP,NULLP,cont);
                   2673:           MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1       noro     2674:           nd_tracelist = t;
                   2675:         }
                   2676:       }
1.6       noro     2677:       nfv = ndtondv(m,nf); nd_free(nf);
1.24      noro     2678:       nh = ndv_newps(m,nfv,0);
1.6       noro     2679:       if ( !m && (ishomo && ++diag_count == diag_period) ) {
                   2680:         diag_count = 0;
                   2681:         stat = do_diagonalize(sugar,m);
                   2682:         if ( !stat ) {
                   2683:           NEXT(l) = d; d = l;
                   2684:           d = nd_reconstruct(1,d);
                   2685:           goto again;
1.1       noro     2686:         }
1.6       noro     2687:       }
1.27      noro     2688: get_eg(&eg1);
1.6       noro     2689:       d = update_pairs(d,g,nh,0);
1.27      noro     2690: get_eg(&eg2); add_eg(&eg_update,&eg1,&eg2);
1.6       noro     2691:       g = update_base(g,nh);
                   2692:       FREENDP(l);
1.49      noro     2693:       if ( nd_hpdata ) {
1.57      noro     2694:         int dg,sugar0;
1.56      noro     2695:
                   2696:         update_hpdata(&current_hpdata,nh);
                   2697:         dg = comp_hn(final_hpdata.hn,current_hpdata.hn);
1.57      noro     2698:         if ( dg < 0 ) {
1.61      noro     2699:            int d_len;
                   2700:            for ( d_len = 0; d; d = d->next, d_len++);
                   2701:            fprintf(asir_out,"[%d] We found a gb\n",d_len);
1.57      noro     2702:         }
                   2703:         sugar0 = sugar;
                   2704:         while ( d && dg > sugar0 ) {
1.58      noro     2705:           d = nd_remove_same_sugar(d,sugar0);
1.57      noro     2706:           sugar0++;
1.49      noro     2707:         }
                   2708:       }
1.41      noro     2709:     } else {
                   2710:       Nnfz++;
                   2711:       if ( nd_gentrace && gensyz ) {
                   2712:         nd_tracelist = reverse_node(nd_tracelist);
                   2713:         MKLIST(list,nd_tracelist);
                   2714:         STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
                   2715:         MKNODE(t,list,nd_alltracelist);
                   2716:         nd_alltracelist = t; nd_tracelist = 0;
                   2717:       }
1.57      noro     2718:       if ( DP_Print ) { fprintf(asir_out,"."); fflush(asir_out); }
1.41      noro     2719:         FREENDP(l);
                   2720:     }
1.36      noro     2721:   }
                   2722:   conv_ilist(nd_demand,0,g,indp);
1.41      noro     2723:   if ( !checkonly && DP_Print ) {
1.60      noro     2724:     fprintf(asir_out,"\nnd_gb done. Nbase=%d,Nnd_add=%d,Npairs=%d, Nnfnz=%d,Nnfz=%d,",nd_psn,Nnd_add,Npairs,Nnfnz,Nnfz);
1.57      noro     2725:     fprintf(asir_out,"Nremoved=%d\n",NcriB+NcriMF+Ncri2);
1.41      noro     2726:     fflush(asir_out);
                   2727:   }
                   2728:   if ( DP_Print ) {
                   2729:     print_eg("update",&eg_update); fprintf(asir_out,"\n");
                   2730:   }
1.36      noro     2731:   return g;
1.1       noro     2732: }
                   2733:
1.30      noro     2734: ND_pairs update_pairs_s(ND_pairs d,int t,NODE *syz);
1.47      noro     2735: int update_pairs_array_s(ND_pairs *d,int t,NODE *syz);
1.30      noro     2736: ND_pairs nd_newpairs_s(int t ,NODE *syz);
1.47      noro     2737: ND_pairs *nd_newpairs_array_s(int t ,NODE *syz);
1.24      noro     2738:
                   2739: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *nf);
                   2740: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
                   2741:
                   2742: void _copydl(int n,DL d1,DL d2);
                   2743: void _subfromdl(int n,DL d1,DL d2);
                   2744: extern int (*cmpdl)(int n,DL d1,DL d2);
1.53      noro     2745: int _dl_redble_ext(DL,DL,DL,int);
                   2746:
                   2747: int primitive_irred(ND p,SIG sig)
                   2748: {
                   2749:   static int wpd=0,dlen=0;
                   2750:   static DL dquo,squo;
                   2751:   static UINT *quo;
                   2752:   int i;
                   2753:
                   2754:   if ( dlen < nd_nvar ) {
                   2755:     NEWDL(dquo,nd_nvar);
                   2756:     NEWDL(squo,nd_nvar);
                   2757:     dlen = nd_nvar;
                   2758:   }
                   2759:   if ( wpd != nd_wpd ) {
                   2760:     wpd = nd_wpd;
                   2761:     quo = (UINT *)MALLOC(wpd*sizeof(UINT));
                   2762:   }
                   2763:   for ( i = 0; i < nd_psn; i++ ) {
                   2764:     if ( sig->pos == nd_psh[i]->sig->pos &&
                   2765:       _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),squo,nd_nvar) )
                   2766:       if ( ndl_reducible(HDL(p),DL(nd_psh[i])) ) {
                   2767:         if ( DP_Print ) fprintf(asir_out,"D");
                   2768:         ndl_sub(HDL(p),DL(nd_psh[i]),quo);
                   2769:         _ndltodl(quo,dquo);
                   2770:         if ( _eqdl(nd_nvar,squo,dquo) )
                   2771:           return 0;
                   2772:       }
                   2773:   }
                   2774:   return 1;
                   2775: }
1.24      noro     2776:
                   2777: NODE insert_sig(NODE l,SIG s)
                   2778: {
                   2779:   int pos;
                   2780:   DL sig;
                   2781:   struct oNODE root;
                   2782:   NODE p,prev,r;
                   2783:   SIG t;
                   2784:
                   2785:   pos = s->pos; sig = DL(s);
                   2786:   root.next = l; prev = &root;
                   2787:   for ( p = l; p; p = p->next ) {
                   2788:     t = (SIG)p->body;
                   2789:     if ( t->pos == pos ) {
                   2790:       if ( _dl_redble(DL(t),sig,nd_nvar) )
                   2791:         return root.next;
                   2792:       else if ( _dl_redble(sig,DL(t),nd_nvar) )
                   2793:         // remove p
                   2794:         prev->next = p->next;
1.45      noro     2795:       else
                   2796:         prev = p;
1.24      noro     2797:     } else
                   2798:       prev = p;
                   2799:   }
1.27      noro     2800:   NEWNODE(r); r->body = (pointer)s; r->next = 0;
                   2801:   for ( p = &root; p->next; p = p->next );
                   2802:   p->next = r;
                   2803: //  r->next = root.next;
                   2804: //  return r;
                   2805:   return root.next;
1.24      noro     2806: }
                   2807:
                   2808: ND_pairs remove_spair_s(ND_pairs d,SIG sig)
                   2809: {
                   2810:   struct oND_pairs root;
                   2811:   ND_pairs prev,p;
                   2812:   SIG spsig;
                   2813:
                   2814:   root.next = d;
                   2815:   prev = &root; p = d;
                   2816:   while ( p ) {
                   2817:     spsig = p->sig;
1.26      noro     2818:     if ( sig->pos == spsig->pos && _dl_redble(DL(sig),DL(spsig),nd_nvar) ) {
1.24      noro     2819:       // remove p
                   2820:       prev->next = p->next;
1.26      noro     2821:       Nsyz++;
                   2822:     } else
1.24      noro     2823:       prev = p;
                   2824:     p = p->next;
                   2825:   }
                   2826:   return (ND_pairs)root.next;
                   2827: }
                   2828:
1.28      noro     2829: int small_lcm(ND_pairs l)
                   2830: {
                   2831:   SIG sig;
                   2832:   int i;
1.29      noro     2833:   NODE t;
1.28      noro     2834:   static DL lcm,mul,quo;
1.44      noro     2835:   static int nvar = 0;
1.28      noro     2836:
1.38      noro     2837:   if ( nd_sba_largelcm ) return 0;
1.28      noro     2838:   if ( nvar < nd_nvar ) {
                   2839:     nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(quo,nvar); NEWDL(mul,nvar);
                   2840:   }
                   2841:   sig = l->sig;
                   2842:   _ndltodl(l->lcm,lcm);
1.29      noro     2843: #if 0
1.28      noro     2844:   for ( i = 0; i < nd_psn; i++ ) {
                   2845:     if ( sig->pos == nd_psh[i]->sig->pos &&
1.29      noro     2846:       _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
1.28      noro     2847:       _ndltodl(DL(nd_psh[i]),mul);
                   2848:       _addtodl(nd_nvar,quo,mul);
                   2849:       if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
                   2850:         break;
                   2851:     }
                   2852:   }
                   2853:   if ( i < nd_psn ) return 1;
                   2854:   else return 0;
1.29      noro     2855: #else
                   2856:   for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
                   2857:     i = (long)BDY(t);
                   2858:     if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
                   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 ( t ) return 1;
                   2866:   else return 0;
                   2867: #endif
1.28      noro     2868: }
                   2869:
1.41      noro     2870: ND_pairs find_smallest_lcm(ND_pairs l)
                   2871: {
                   2872:   SIG sig;
                   2873:   int i,minindex;
                   2874:   NODE t;
                   2875:   ND_pairs r;
                   2876:   struct oSIG sig1;
1.55      noro     2877:   static DL mul,quo,quo2,minlm;
1.44      noro     2878:   static int nvar = 0;
1.41      noro     2879:
                   2880:   if ( nvar < nd_nvar ) {
                   2881:     nvar = nd_nvar;
1.55      noro     2882:     NEWDL(quo,nvar); NEWDL(quo2,nvar); NEWDL(mul,nvar);
1.41      noro     2883:     NEWDL(minlm,nvar);
                   2884:   }
                   2885:   sig = l->sig;
                   2886:   // find mg s.t. m*s(g)=sig and m*lm(g) is minimal
                   2887:   _ndltodl(l->lcm,minlm); minindex = -1;
                   2888:   for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
                   2889:     i = (long)BDY(t);
                   2890:     if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
                   2891:       _ndltodl(DL(nd_psh[i]),mul);
                   2892:       _addtodl(nd_nvar,quo,mul);
                   2893:       if ( (*cmpdl)(nd_nvar,minlm,mul) > 0 ) {
                   2894:         minindex = i;
1.47      noro     2895:         break;
1.41      noro     2896:         _copydl(nd_nvar,mul,minlm);
                   2897:       }
                   2898:     }
                   2899:   }
                   2900:   // l->lcm is minimal; return l itself
                   2901:   if ( minindex < 0 ) return l;
1.47      noro     2902:   else return 0;
1.41      noro     2903:   for ( i = 0; i < nd_psn; i++ ) {
                   2904:     if ( i == minindex ) continue;
                   2905:     _ndltodl(DL(nd_psh[i]),mul);
                   2906:     if ( _dl_redble_ext(mul,minlm,quo,nd_nvar) ) {
                   2907:       _addtodl(nd_nvar,nd_ps[i]->sig->dl,quo);
                   2908:       sig1.pos = nd_ps[i]->sig->pos;
                   2909:       sig1.dl = quo;
1.55      noro     2910:       sig1.dl2 = quo2;
                   2911:       _adddl(nd_nvar,sig1.dl,nd_sba_hm[sig1.pos],sig1.dl2);
1.41      noro     2912:       if ( comp_sig(sig,&sig1) > 0 ) {
                   2913: //        printf("X");
                   2914:         NEWND_pairs(r);
                   2915:         r->sig = sig;
                   2916:         r->i1 = minindex;
                   2917:         r->i2 = i;
                   2918:         dltondl(nd_nvar,minlm,r->lcm);
                   2919:         r->next = 0;
                   2920:         return r;
                   2921:       }
                   2922:     }
                   2923:   }
                   2924:   // there is no suitable spair
                   2925:   return 0;
                   2926: }
                   2927:
1.28      noro     2928: ND_pairs remove_large_lcm(ND_pairs d)
                   2929: {
                   2930:   struct oND_pairs root;
                   2931:   ND_pairs prev,p;
                   2932:
                   2933:   root.next = d;
                   2934:   prev = &root; p = d;
                   2935:   while ( p ) {
1.41      noro     2936: #if 0
1.28      noro     2937:     if ( small_lcm(p) ) {
                   2938:       // remove p
                   2939:       prev->next = p->next;
                   2940:     } else
1.41      noro     2941: #else
                   2942:     if ( find_smallest_lcm(p) == 0 ) {
                   2943:       // remove p
                   2944:       prev->next = p->next;
                   2945:     } else
                   2946: #endif
1.28      noro     2947:       prev = p;
                   2948:     p = p->next;
                   2949:   }
                   2950:   return (ND_pairs)root.next;
                   2951: }
                   2952:
1.26      noro     2953: struct oEGT eg_create,eg_newpairs,eg_merge;
                   2954:
1.30      noro     2955: NODE conv_ilist_s(int demand,int trace,int **indp);
                   2956:
1.44      noro     2957: // S(fj*ei-fi*ej)
                   2958:
1.45      noro     2959: void _subdl(int,DL,DL,DL);
                   2960:
1.44      noro     2961: SIG trivial_sig(int i,int j)
                   2962: {
1.45      noro     2963:   static DL lcm;
1.44      noro     2964:   static struct oSIG sigi,sigj;
                   2965:   static int nvar = 0;
                   2966:   SIG sig;
                   2967:
                   2968:   if ( nvar != nd_nvar ) {
1.45      noro     2969:     nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(sigi.dl,nvar); NEWDL(sigj.dl,nvar);
1.55      noro     2970:     NEWDL(sigi.dl2,nvar); NEWDL(sigj.dl2,nvar);
1.45      noro     2971:   }
1.46      noro     2972:   if ( nd_sba_inputisgb != 0 ) {
1.45      noro     2973:     lcm_of_DL(nd_nvar,nd_sba_hm[i],nd_sba_hm[j],lcm);
                   2974:     sigi.pos = i; _subdl(nd_nvar,lcm,nd_sba_hm[i],sigi.dl);
1.55      noro     2975:     _copydl(nd_nvar,lcm,sigi.dl2);
1.45      noro     2976:     sigj.pos = j; _subdl(nd_nvar,lcm,nd_sba_hm[j],sigj.dl);
1.55      noro     2977:     _copydl(nd_nvar,lcm,sigj.dl2);
1.45      noro     2978:     if ( comp_sig(&sigi,&sigj) > 0 ) sig = dup_sig(&sigi);
                   2979:     else sig = dup_sig(&sigj);
                   2980:   } else {
                   2981:     sigi.pos = i; _copydl(nd_nvar,nd_sba_hm[j],sigi.dl);
1.55      noro     2982:     _adddl(nd_nvar,sigi.dl,nd_sba_hm[i],sigi.dl2);
1.45      noro     2983:     sigj.pos = j; _copydl(nd_nvar,nd_sba_hm[i],sigj.dl);
1.55      noro     2984:     _adddl(nd_nvar,sigj.dl,nd_sba_hm[j],sigj.dl2);
1.45      noro     2985:     if ( comp_sig(&sigi,&sigj) > 0 ) sig = dup_sig(&sigi);
                   2986:     else sig = dup_sig(&sigj);
1.44      noro     2987:   }
                   2988:   return sig;
                   2989: }
                   2990:
1.47      noro     2991: int nd_minsig(ND_pairs *d)
                   2992: {
                   2993:   int min,i,ret;
                   2994:
                   2995:   min = -1;
                   2996:   for ( i = 0; i < nd_nbase; i++ ) {
                   2997:     if ( d[i] != 0 ) {
                   2998:       if ( min < 0 ) min = i;
                   2999:       else {
                   3000:         ret = comp_sig(d[i]->sig,d[min]->sig);
                   3001:         if ( ret < 0 ) min = i;
                   3002:       }
                   3003:     }
                   3004:   }
                   3005:   return min;
                   3006: }
                   3007:
                   3008: int dlength(ND_pairs d)
                   3009: {
                   3010:   int i;
                   3011:   for ( i = 0; d; d = d->next, i++ );
                   3012:   return i;
                   3013: }
                   3014:
1.44      noro     3015: NODE nd_sba_buch(int m,int ishomo,int **indp,NODE *syzp)
1.24      noro     3016: {
1.39      noro     3017:   int i,j,nh,sugar,stat,pos;
1.65    ! noro     3018:   NODE g;
1.47      noro     3019:   ND_pairs *d;
1.41      noro     3020:   ND_pairs l,l1;
1.65    ! noro     3021:   ND h,nf;
1.24      noro     3022:   NDV nfv;
1.65    ! noro     3023: //  union oNDC hc;
1.24      noro     3024:   SIG sig;
1.29      noro     3025:   NODE *syzlist;
1.47      noro     3026:   int ngen,ind;
1.41      noro     3027:   int Nnominimal,Nredundant;
1.28      noro     3028:   DL lcm,quo,mul;
1.49      noro     3029:   struct oHPDATA final_hpdata,current_hpdata;
1.65    ! noro     3030:   struct oEGT eg1,eg2,eg3,eg4,eg_remove,eg_large,eg_nf,eg_nfzero;
1.50      noro     3031:   struct oEGT eg_minsig,eg_smallest,eg_removecont,eg_hpdata,eg_updatepairs,eg_sbabuch,eg_sp;
1.47      noro     3032:   int Nnfs=0,Nnfz=0,Nnfnz=0,dlen,nsyz;
1.24      noro     3033:
1.27      noro     3034: init_eg(&eg_remove);
1.29      noro     3035:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.47      noro     3036:   d = (ND_pairs *)MALLOC(nd_psn*sizeof(ND_pairs));
                   3037:   nd_nbase = nd_psn;
1.26      noro     3038:   Nsyz = 0;
1.24      noro     3039:   Nnd_add = 0;
1.41      noro     3040:   Nnominimal = 0;
1.26      noro     3041:   Nredundant = 0;
1.44      noro     3042:   ngen = nd_psn;
1.56      noro     3043:   if ( !do_weyl || nd_sba_inputisgb ) {
1.48      noro     3044:     for ( i = 0; i < nd_psn; i++ )
                   3045:       for ( j = i+1; j < nd_psn; j++ ) {
                   3046:         sig = trivial_sig(i,j);
                   3047:         syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
                   3048:       }
1.29      noro     3049:     }
1.47      noro     3050:   dlen = 0;
1.41      noro     3051:   for ( i = 0; i < nd_psn; i++ ) {
1.47      noro     3052:     dlen += update_pairs_array_s(d,i,syzlist);
1.41      noro     3053:   }
1.24      noro     3054:   sugar = 0;
1.39      noro     3055:   pos = 0;
1.49      noro     3056:   if ( nd_hpdata ) {
                   3057:     setup_hpdata(&final_hpdata,&current_hpdata);
                   3058:   }
1.26      noro     3059:   NEWDL(lcm,nd_nvar); NEWDL(quo,nd_nvar); NEWDL(mul,nd_nvar);
1.50      noro     3060: init_eg(&eg_sp);
1.26      noro     3061: init_eg(&eg_create);
                   3062: init_eg(&eg_merge);
1.47      noro     3063: init_eg(&eg_minsig);
                   3064: init_eg(&eg_smallest);
1.29      noro     3065: init_eg(&eg_large);
1.31      noro     3066: init_eg(&eg_nf);
                   3067: init_eg(&eg_nfzero);
1.50      noro     3068: init_eg(&eg_removecont);
                   3069: init_eg(&eg_updatepairs);
                   3070: init_eg(&eg_hpdata);
                   3071: init_eg(&eg_sbabuch);
                   3072: get_eg(&eg3);
1.47      noro     3073:   while ( 1 ) {
1.53      noro     3074:     if ( DP_Print && !nd_hpdata && dlen%1000 == 0 ) fprintf(asir_out,"(%d)",dlen);
1.47      noro     3075: again :
                   3076: get_eg(&eg1);
                   3077:     ind = nd_minsig(d);
                   3078: get_eg(&eg2); add_eg(&eg_minsig,&eg1,&eg2);
                   3079:     if ( ind < 0 ) break;
                   3080:     l = d[ind];
                   3081: //    printf("(%d,%d)",l->i1,l->i2); print_sig(l->sig); printf("\n");
                   3082: get_eg(&eg1);
1.41      noro     3083:     l1 = find_smallest_lcm(l);
1.47      noro     3084: get_eg(&eg2); add_eg(&eg_smallest,&eg1,&eg2);
1.41      noro     3085:     if ( l1 == 0 ) {
1.47      noro     3086:       d[ind] = d[ind]->next; dlen--;
1.53      noro     3087: //      if ( DP_Print && !nd_hpdata ) fprintf(asir_out,"M");
1.41      noro     3088:       Nnominimal++;
                   3089:       continue;
                   3090:     }
                   3091:     if ( SG(l1) != sugar ) {
                   3092:       sugar = SG(l1);
                   3093:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   3094:     }
                   3095:     sig = l1->sig;
                   3096:     if ( DP_Print && nd_sba_pot ) {
                   3097:       if ( sig->pos != pos ) {
                   3098:         fprintf(asir_out,"[%d]",sig->pos);
                   3099:         pos = sig->pos;
                   3100:       }
                   3101:     }
1.50      noro     3102: get_eg(&eg1);
1.41      noro     3103:     stat = nd_sp(m,0,l1,&h);
1.50      noro     3104: get_eg(&eg2); add_eg(&eg_sp,&eg1,&eg2);
1.24      noro     3105:     if ( !stat ) {
1.47      noro     3106:       nd_reconstruct_s(0,d);
1.24      noro     3107:       goto again;
                   3108:     }
1.31      noro     3109: get_eg(&eg1);
1.25      noro     3110: #if USE_GEOBUCKET
1.55      noro     3111:     stat = (m&&!nd_gentrace)?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     3112: #else
1.39      noro     3113:     stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.24      noro     3114: #endif
1.31      noro     3115: get_eg(&eg2);
1.24      noro     3116:     if ( !stat ) {
1.47      noro     3117:       nd_reconstruct_s(0,d);
1.24      noro     3118:       goto again;
                   3119:     } else if ( stat == -1 ) {
1.47      noro     3120:       d[ind] = d[ind]->next; dlen--;
1.41      noro     3121:       Nnfs++;
1.24      noro     3122:       if ( DP_Print ) { printf("S"); fflush(stdout); }
                   3123:     } else if ( nf ) {
1.47      noro     3124:       d[ind] = d[ind]->next; dlen--;
1.41      noro     3125:       Nnfnz++;
                   3126:       if ( DP_Print ) {
                   3127:         if ( nd_sba_redundant_check ) {
                   3128:           if ( ndl_find_reducer_nonsig(HDL(nf)) >= 0 ) {
                   3129:             Nredundant++;
                   3130:             printf("R");
                   3131:           } else
                   3132:             printf("+");
                   3133:         } else
                   3134:           printf("+");
                   3135:         fflush(stdout);
                   3136:       }
1.31      noro     3137:       add_eg(&eg_nf,&eg1,&eg2);
1.65    ! noro     3138: //      hc = HCU(nf);
        !          3139:       get_eg(&eg1);
        !          3140:       nd_removecont(m,nf);
        !          3141:       get_eg(&eg2); add_eg(&eg_removecont,&eg1,&eg2);
        !          3142:       nfv = ndtondv(m,nf); nd_free(nf);
        !          3143:       nh = ndv_newps(m,nfv,0);
1.55      noro     3144:
1.65    ! noro     3145:       get_eg(&eg1);
        !          3146:       dlen += update_pairs_array_s(d,nh,syzlist);
        !          3147:       get_eg(&eg2); add_eg(&eg_updatepairs,&eg1,&eg2);
        !          3148:       nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
        !          3149:       if ( nd_hpdata ) {
        !          3150:         int dg,sugar0;
        !          3151:
        !          3152:         get_eg(&eg1);
        !          3153:         update_hpdata(&current_hpdata,nh);
        !          3154:         get_eg(&eg2); add_eg(&eg_hpdata,&eg1,&eg2);
        !          3155:         dg = comp_hn(final_hpdata.hn,current_hpdata.hn);
        !          3156: //        if ( !compp(CO,final_hpdata.hn,current_hpdata.hn) )
        !          3157:         if ( dg < 0 ) {
        !          3158:           if ( DP_Print ) { printf("\nWe found a gb.\n"); }
        !          3159:             break;
        !          3160:         } else if ( nd_sba_heu == 1 ) {
        !          3161:           for ( i = 0; i < ngen; i++ ) {
        !          3162:             sugar0 = sugar;
        !          3163:             while ( d[i] && dg > sugar0 ) {
        !          3164:               d[i] = nd_remove_same_sugar(d[i],sugar0);
        !          3165:               sugar0++;
        !          3166:             }
1.63      noro     3167:           }
                   3168:         }
1.49      noro     3169:       }
1.65    ! noro     3170:     } else {
1.47      noro     3171:       d[ind] = d[ind]->next; dlen--;
1.41      noro     3172:       Nnfz++;
1.31      noro     3173:       add_eg(&eg_nfzero,&eg1,&eg2);
1.65    ! noro     3174:     // syzygy
1.27      noro     3175: get_eg(&eg1);
1.65    ! noro     3176:       nsyz = Nsyz;
        !          3177:       d[sig->pos] = remove_spair_s(d[sig->pos],sig);
        !          3178:       dlen -= Nsyz-nsyz;
1.27      noro     3179: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.65    ! noro     3180:       syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
        !          3181:       if ( DP_Print ) { printf("."); fflush(stdout); }
        !          3182:     }
        !          3183:   }
        !          3184:   get_eg(&eg4); add_eg(&eg_sbabuch,&eg3,&eg4);
        !          3185:   g = conv_ilist_s(nd_demand,0,indp);
        !          3186:   if ( DP_Print ) {
        !          3187:     printf("\ndlen=%d,nd_sba done. nd_add=%d,Nsyz=%d,Nsamesig=%d,Nnominimal=%d\n",dlen,Nnd_add,Nsyz,Nsamesig,Nnominimal);
        !          3188:     printf("Nbase=%d,Nnfnz=%d,Nnfz=%d,Nnfsingular=%d\n",nd_psn,Nnfnz,Nnfz,Nnfs);
        !          3189:     fflush(stdout);
        !          3190:     if ( nd_sba_redundant_check )
        !          3191:       printf("Nredundant=%d\n",Nredundant);
        !          3192:     fflush(stdout);
        !          3193:      print_eg("sp",&eg_sp);
        !          3194:      print_eg("create",&eg_create);
        !          3195:      print_eg("merge",&eg_merge);
        !          3196:      print_eg("minsig",&eg_minsig);
        !          3197:      print_eg("smallest",&eg_smallest);
        !          3198:      print_eg("remove",&eg_remove);
        !          3199:      printf("\n");
        !          3200:      print_eg("nf",&eg_nf);
        !          3201:      print_eg("nfzero",&eg_nfzero);
        !          3202:      print_eg("removecont",&eg_removecont);
        !          3203:      print_eg("updatepairs",&eg_updatepairs);
        !          3204:      print_eg("hpdata",&eg_hpdata);
        !          3205:      print_eg("total",&eg_sbabuch);
        !          3206:      printf("\n");
        !          3207:   }
        !          3208:   if ( nd_sba_syz ) {
        !          3209:     print_eg("remove",&eg_remove);
        !          3210:     print_eg("nf",&eg_nf);
        !          3211:     print_eg("nfzero",&eg_nfzero);
        !          3212:     printf("\n");
        !          3213:   }
        !          3214:   if ( nd_sba_syz ) {
        !          3215:     NODE hsyz,tsyz,prev;
        !          3216:
        !          3217:     hsyz = 0;
        !          3218:     for ( i = 0; i < ngen; i++ ) {
        !          3219:       tsyz = syzlist[i];
        !          3220:       for ( prev = 0; tsyz != 0; prev = tsyz, tsyz = NEXT(tsyz))
        !          3221:         BDY(tsyz) = (pointer)sigtodpm((SIG)BDY(tsyz));
        !          3222:       if ( prev != 0 ) {
        !          3223:         prev->next = hsyz; hsyz = syzlist[i];
        !          3224:       }
        !          3225:     }
        !          3226:     *syzp = hsyz;
        !          3227:   } else *syzp = 0;
        !          3228:   return g;
1.24      noro     3229: }
                   3230:
1.1       noro     3231: /* splist = [[i1,i2],...] */
                   3232:
                   3233: int check_splist(int m,NODE splist)
                   3234: {
                   3235:   NODE t,p;
                   3236:   ND_pairs d,r,l;
                   3237:   int stat;
                   3238:   ND h,nf;
                   3239:
                   3240:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   3241:     p = BDY((LIST)BDY(t));
1.6       noro     3242:     NEXTND_pairs(d,r);
1.65    ! noro     3243:     r->i1 = (int)ZTOS((Q)ARG0(p)); r->i2 = (int)ZTOS((Q)ARG1(p));
1.6       noro     3244:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     3245:     SG(r) = TD(LCM(r)); /* XXX */
                   3246:   }
                   3247:   if ( d ) NEXT(r) = 0;
                   3248:
1.6       noro     3249:   while ( d ) {
1.1       noro     3250: again:
1.6       noro     3251:     l = nd_minp(d,&d);
                   3252:     stat = nd_sp(m,0,l,&h);
                   3253:     if ( !stat ) {
                   3254:       NEXT(l) = d; d = l;
                   3255:       d = nd_reconstruct(0,d);
                   3256:       goto again;
                   3257:     }
1.39      noro     3258:     stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.6       noro     3259:     if ( !stat ) {
                   3260:       NEXT(l) = d; d = l;
                   3261:       d = nd_reconstruct(0,d);
                   3262:       goto again;
                   3263:     } else if ( nf ) return 0;
1.1       noro     3264:     if ( DP_Print) { printf("."); fflush(stdout); }
1.6       noro     3265:   }
1.1       noro     3266:   if ( DP_Print) { printf("done.\n"); fflush(stdout); }
                   3267:   return 1;
                   3268: }
                   3269:
                   3270: int check_splist_f4(int m,NODE splist)
                   3271: {
                   3272:   UINT *s0vect;
1.6       noro     3273:   PGeoBucket bucket;
1.1       noro     3274:   NODE p,rp0,t;
                   3275:   ND_pairs d,r,l,ll;
                   3276:   int col,stat;
                   3277:
                   3278:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   3279:     p = BDY((LIST)BDY(t));
1.6       noro     3280:     NEXTND_pairs(d,r);
1.65    ! noro     3281:     r->i1 = (int)ZTOS((Q)ARG0(p)); r->i2 = (int)ZTOS((Q)ARG1(p));
1.6       noro     3282:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     3283:     SG(r) = TD(LCM(r)); /* XXX */
                   3284:   }
                   3285:   if ( d ) NEXT(r) = 0;
                   3286:
1.6       noro     3287:   while ( d ) {
                   3288:     l = nd_minsugarp(d,&d);
                   3289:     bucket = create_pbucket();
                   3290:     stat = nd_sp_f4(m,0,l,bucket);
                   3291:     if ( !stat ) {
                   3292:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   3293:       NEXT(ll) = d; d = l;
                   3294:       d = nd_reconstruct(0,d);
                   3295:       continue;
                   3296:     }
                   3297:     if ( bucket->m < 0 ) continue;
                   3298:     col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   3299:     if ( !col ) {
                   3300:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   3301:       NEXT(ll) = d; d = l;
                   3302:       d = nd_reconstruct(0,d);
                   3303:       continue;
1.1       noro     3304:     }
1.6       noro     3305:     if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
                   3306:   }
                   3307:   return 1;
1.1       noro     3308: }
                   3309:
                   3310: int do_diagonalize_trace(int sugar,int m)
                   3311: {
1.65    ! noro     3312:   int i,stat;
        !          3313:   ND nf,s,head;
        !          3314:   NDV nfv;
1.6       noro     3315:   union oNDC hc;
                   3316:   NODE node;
                   3317:   LIST l;
                   3318:   Z iq;
1.65    ! noro     3319:   P cont;
1.1       noro     3320:
1.64      noro     3321:   if ( diag_period == 0 ) return 1;
1.6       noro     3322:   for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   3323:     if ( nd_gentrace ) {
                   3324:         /* Trace = [1,index,1,1] */
                   3325:         STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   3326:         MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   3327:     }
                   3328:     /* for nd_ps */
                   3329:     s = ndvtond(m,nd_ps[i]);
                   3330:     s = nd_separate_head(s,&head);
                   3331:     stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
                   3332:     if ( !stat ) return 0;
                   3333:     nf = nd_add(m,head,nf);
                   3334:     ndv_free(nd_ps[i]);
                   3335:     nd_ps[i] = ndtondv(m,nf);
                   3336:     nd_free(nf);
                   3337:
                   3338:     /* for nd_ps_trace */
                   3339:     if ( nd_demand )
                   3340:         nfv = ndv_load(i);
                   3341:     else
                   3342:         nfv = nd_ps_trace[i];
                   3343:     s = ndvtond(0,nfv);
                   3344:     s = nd_separate_head(s,&head);
                   3345:     stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
                   3346:     if ( !stat ) return 0;
                   3347:     ndv_free(nfv);
                   3348:     hc = HCU(nf); nd_removecont(0,nf);
                   3349:     /* exact division */
1.1       noro     3350:     cont = ndc_div(0,hc,HCU(nf));
1.6       noro     3351:     if ( nd_gentrace ) finalize_tracelist(i,cont);
                   3352:     nfv = ndtondv(0,nf);
                   3353:     nd_free(nf);
                   3354:     nd_bound[i] = ndv_compute_bound(nfv);
                   3355:     register_hcf(nfv);
                   3356:     if ( nd_demand ) {
                   3357:     ndv_save(nfv,i);
                   3358:     ndv_free(nfv);
                   3359:     } else
                   3360:     nd_ps_trace[i] = nfv;
                   3361:   }
                   3362:   return 1;
1.1       noro     3363: }
                   3364:
                   3365: static struct oEGT eg_invdalg;
                   3366: struct oEGT eg_le;
                   3367:
                   3368: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
                   3369: {
                   3370:     NODE tn;
                   3371:     P p1;
                   3372:
                   3373:     for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
                   3374:         substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
                   3375:     }
                   3376:     *r = p;
                   3377: }
                   3378:
                   3379: NODE nd_gb_trace(int m,int ishomo,int **indp)
                   3380: {
1.6       noro     3381:   int i,nh,sugar,stat;
1.65    ! noro     3382:   NODE g,t;
1.6       noro     3383:   ND_pairs d;
                   3384:   ND_pairs l;
1.65    ! noro     3385:   ND h,nf,nfq;
1.6       noro     3386:   NDV nfv,nfqv;
1.65    ! noro     3387:   Z q;
1.6       noro     3388:   P hc;
1.65    ! noro     3389:   union oNDC hnfq;
1.6       noro     3390:   struct oEGT eg_monic,egm0,egm1;
                   3391:   int diag_count = 0;
                   3392:   P cont;
                   3393:   LIST list;
1.49      noro     3394:   struct oHPDATA current_hpdata,final_hpdata;
1.6       noro     3395:
                   3396:   init_eg(&eg_monic);
                   3397:   init_eg(&eg_invdalg);
                   3398:   init_eg(&eg_le);
                   3399:   g = 0; d = 0;
                   3400:   for ( i = 0; i < nd_psn; i++ ) {
                   3401:     d = update_pairs(d,g,i,0);
                   3402:     g = update_base(g,i);
                   3403:   }
                   3404:   sugar = 0;
1.49      noro     3405:   if ( nd_hpdata ) {
1.52      noro     3406:     if ( DP_Print ) fprintf(asir_out,"Hilbert driven algorithm.\n");
1.49      noro     3407:     setup_hpdata(&final_hpdata,&current_hpdata);
                   3408:   }
                   3409:
1.6       noro     3410:   while ( d ) {
1.1       noro     3411: again:
1.6       noro     3412:     l = nd_minp(d,&d);
                   3413:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   3414:     if ( SG(l) != sugar ) {
1.1       noro     3415: #if 1
1.6       noro     3416:       if ( ishomo ) {
                   3417:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3418:         stat = do_diagonalize_trace(sugar,m);
                   3419:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3420:         diag_count = 0;
1.1       noro     3421:         if ( !stat ) {
1.6       noro     3422:           NEXT(l) = d; d = l;
                   3423:           d = nd_reconstruct(1,d);
                   3424:           goto again;
1.1       noro     3425:         }
1.6       noro     3426:       }
                   3427: #endif
                   3428:       sugar = SG(l);
                   3429:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   3430:     }
                   3431:     stat = nd_sp(m,0,l,&h);
                   3432:     if ( !stat ) {
                   3433:       NEXT(l) = d; d = l;
                   3434:       d = nd_reconstruct(1,d);
                   3435:       goto again;
                   3436:     }
1.1       noro     3437: #if USE_GEOBUCKET
1.39      noro     3438:     stat = nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     3439: #else
1.39      noro     3440:     stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     3441: #endif
1.6       noro     3442:     if ( !stat ) {
                   3443:       NEXT(l) = d; d = l;
                   3444:       d = nd_reconstruct(1,d);
                   3445:       goto again;
                   3446:     } else if ( nf ) {
                   3447:       if ( nd_demand ) {
                   3448:         nfqv = ndv_load(nd_psn);
                   3449:         nfq = ndvtond(0,nfqv);
                   3450:       } else
                   3451:         nfq = 0;
                   3452:       if ( !nfq ) {
1.39      noro     3453:         if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!nd_top&&!Top,&nfq) ) {
1.6       noro     3454:           NEXT(l) = d; d = l;
                   3455:           d = nd_reconstruct(1,d);
                   3456:           goto again;
                   3457:         }
                   3458:       }
                   3459:       if ( nfq ) {
                   3460:         /* m|HC(nfq) => failure */
                   3461:         if ( nd_vc ) {
                   3462:           nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
                   3463:         } else
                   3464:           q = HCZ(nfq);
                   3465:         if ( !remqi((Q)q,m) ) return 0;
                   3466:
                   3467:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   3468:         hnfq = HCU(nfq);
                   3469:         if ( nd_nalg ) {
                   3470:           /* m|DN(HC(nf)^(-1)) => failure */
                   3471:           get_eg(&egm0);
                   3472:           if ( !nd_monic(m,&nfq) ) return 0;
                   3473:           get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
                   3474:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   3475:           nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
                   3476:         } else {
                   3477:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   3478:           nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
                   3479:         }
                   3480:         if ( nd_gentrace ) {
                   3481:           /* exact division */
                   3482:           cont = ndc_div(0,hnfq,HCU(nfqv));
                   3483:           if ( !UNIQ(cont) ) {
                   3484:             t = mknode(4,NULLP,NULLP,NULLP,cont);
                   3485:             MKLIST(list,t); MKNODE(t,list,nd_tracelist);
                   3486:             nd_tracelist = t;
                   3487:           }
                   3488:         }
1.24      noro     3489:         nh = ndv_newps(0,nfv,nfqv);
1.6       noro     3490:         if ( ishomo && ++diag_count == diag_period ) {
                   3491:           diag_count = 0;
                   3492:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3493:           stat = do_diagonalize_trace(sugar,m);
                   3494:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3495:           if ( !stat ) {
1.1       noro     3496:             NEXT(l) = d; d = l;
                   3497:             d = nd_reconstruct(1,d);
                   3498:             goto again;
1.6       noro     3499:           }
1.1       noro     3500:         }
1.6       noro     3501:         d = update_pairs(d,g,nh,0);
                   3502:         g = update_base(g,nh);
1.49      noro     3503:         if ( nd_hpdata ) {
1.57      noro     3504:           int dg,sugar0;
1.56      noro     3505:
                   3506:           update_hpdata(&current_hpdata,nh);
                   3507:           dg = comp_hn(final_hpdata.hn,current_hpdata.hn);
1.57      noro     3508:           if ( dg < 0 ) {
1.61      noro     3509:              int d_len;
                   3510:              for ( d_len = 0; d; d = d->next, d_len++);
                   3511:              fprintf(asir_out,"[%d] We found a gb\n",d_len);
1.57      noro     3512:           }
                   3513:           sugar0 = sugar;
                   3514:           while ( d && dg > sugar0 ) {
1.58      noro     3515:             d = nd_remove_same_sugar(d,sugar0);
1.57      noro     3516:             sugar0++;
1.49      noro     3517:           }
                   3518:         }
1.6       noro     3519:       } else {
1.57      noro     3520:         if ( DP_Print ) { fprintf(asir_out,"*"); fflush(asir_out); }
1.6       noro     3521:       }
                   3522:     } else {
1.57      noro     3523:       if ( DP_Print ) { fprintf(asir_out,"."); fflush(asir_out); }
1.1       noro     3524:     }
1.6       noro     3525:     FREENDP(l);
                   3526:   }
                   3527:   if ( nd_nalg ) {
                   3528:     if ( DP_Print ) {
                   3529:       print_eg("monic",&eg_monic);
                   3530:       print_eg("invdalg",&eg_invdalg);
                   3531:       print_eg("le",&eg_le);
1.1       noro     3532:     }
1.6       noro     3533:   }
1.1       noro     3534:   conv_ilist(nd_demand,1,g,indp);
1.60      noro     3535:   if ( DP_Print ) { fprintf(asir_out,"\nnd_gb_trace done. Nbase=%d\n",nd_psn); fflush(asir_out); }
1.6       noro     3536:   return g;
1.1       noro     3537: }
                   3538:
                   3539: int ndv_compare(NDV *p1,NDV *p2)
                   3540: {
                   3541:     return DL_COMPARE(HDL(*p1),HDL(*p2));
                   3542: }
                   3543:
                   3544: int ndv_compare_rev(NDV *p1,NDV *p2)
                   3545: {
                   3546:     return -DL_COMPARE(HDL(*p1),HDL(*p2));
                   3547: }
                   3548:
                   3549: int ndvi_compare(NDVI p1,NDVI p2)
                   3550: {
                   3551:     return DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3552: }
                   3553:
                   3554: int ndvi_compare_rev(NDVI p1,NDVI p2)
                   3555: {
                   3556:     return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3557: }
                   3558:
                   3559: NODE ndv_reduceall(int m,NODE f)
                   3560: {
                   3561:   int i,j,n,stat;
                   3562:   ND nf,g,head;
                   3563:   NODE t,a0,a;
                   3564:   NODE node;
                   3565:   LIST l;
                   3566:   Z iq,jq;
                   3567:   int *perm;
                   3568:   union oNDC hc;
1.65    ! noro     3569:   P cont;
1.1       noro     3570:
                   3571:   if ( nd_nora ) return f;
                   3572:   n = length(f);
1.24      noro     3573:   ndv_setup(m,0,f,0,1,0);
1.1       noro     3574:   perm = (int *)MALLOC(n*sizeof(int));
                   3575:   if ( nd_gentrace ) {
                   3576:     for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.65    ! noro     3577:       perm[i] = (int)ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1       noro     3578:   }
                   3579:   for ( i = 0; i < n; ) {
                   3580:     if ( nd_gentrace ) {
                   3581:       /* Trace = [1,index,1,1] */
1.6       noro     3582:       STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1       noro     3583:       MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   3584:     }
                   3585:     g = ndvtond(m,nd_ps[i]);
                   3586:     g = nd_separate_head(g,&head);
1.6       noro     3587:     stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1       noro     3588:     if ( !stat )
                   3589:       nd_reconstruct(0,0);
                   3590:     else {
                   3591:       if ( DP_Print ) { printf("."); fflush(stdout); }
                   3592:       ndv_free(nd_ps[i]);
                   3593:       hc = HCU(nf); nd_removecont(m,nf);
                   3594:       if ( nd_gentrace ) {
                   3595:         for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.65    ! noro     3596:           jq = ARG1(BDY((LIST)BDY(t))); j = (int)ZTOS(jq);
1.6       noro     3597:           STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1       noro     3598:         }
1.6       noro     3599:         /* exact division */
1.1       noro     3600:         cont = ndc_div(m,hc,HCU(nf));
                   3601:         finalize_tracelist(perm[i],cont);
                   3602:       }
                   3603:       nd_ps[i] = ndtondv(m,nf); nd_free(nf);
                   3604:       nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   3605:       i++;
                   3606:     }
                   3607:   }
                   3608:   if ( DP_Print ) { printf("\n"); }
                   3609:   for ( a0 = 0, i = 0; i < n; i++ ) {
                   3610:     NEXTNODE(a0,a);
                   3611:     if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
                   3612:     else {
                   3613:       for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
                   3614:       BDY(a) = (pointer)nd_ps[j];
                   3615:     }
                   3616:   }
                   3617:   NEXT(a) = 0;
                   3618:   return a0;
                   3619: }
                   3620:
1.41      noro     3621: int ndplength(ND_pairs d)
                   3622: {
                   3623:   int i;
                   3624:   for ( i = 0; d; i++ ) d = NEXT(d);
                   3625:   return i;
                   3626: }
                   3627:
1.1       noro     3628: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
                   3629: {
                   3630:   ND_pairs d1,nd,cur,head,prev,remove;
1.41      noro     3631:   int len0;
1.1       noro     3632:
                   3633:   if ( !g ) return d;
                   3634:   /* for testing */
                   3635:   if ( gensyz && nd_gensyz == 2 ) {
                   3636:     d1 = nd_newpairs(g,t);
                   3637:     if ( !d )
                   3638:       return d1;
                   3639:     else {
                   3640:       nd = d;
                   3641:       while ( NEXT(nd) ) nd = NEXT(nd);
                   3642:       NEXT(nd) = d1;
                   3643:       return d;
                   3644:     }
                   3645:   }
                   3646:   d = crit_B(d,t);
                   3647:   d1 = nd_newpairs(g,t);
1.41      noro     3648:   len0 = ndplength(d1);
1.1       noro     3649:   d1 = crit_M(d1);
                   3650:   d1 = crit_F(d1);
1.41      noro     3651:   NcriMF += len0-ndplength(d1);
1.1       noro     3652:   if ( gensyz || do_weyl )
                   3653:     head = d1;
                   3654:   else {
                   3655:     prev = 0; cur = head = d1;
                   3656:     while ( cur ) {
                   3657:       if ( crit_2( cur->i1,cur->i2 ) ) {
                   3658:         remove = cur;
                   3659:         if ( !prev ) head = cur = NEXT(cur);
                   3660:         else cur = NEXT(prev) = NEXT(cur);
1.41      noro     3661:         FREENDP(remove); Ncri2++;
1.1       noro     3662:       } else {
                   3663:         prev = cur; cur = NEXT(cur);
                   3664:       }
                   3665:     }
                   3666:   }
                   3667:   if ( !d )
                   3668:     return head;
                   3669:   else {
                   3670:     nd = d;
                   3671:     while ( NEXT(nd) ) nd = NEXT(nd);
                   3672:     NEXT(nd) = head;
                   3673:     return d;
                   3674:   }
                   3675: }
                   3676:
1.24      noro     3677: ND_pairs merge_pairs_s(ND_pairs d,ND_pairs d1);
                   3678:
1.30      noro     3679: ND_pairs update_pairs_s( ND_pairs d, int t,NODE *syz)
1.24      noro     3680: {
                   3681:   ND_pairs d1;
1.26      noro     3682:   struct oEGT eg1,eg2,eg3;
1.24      noro     3683:
1.30      noro     3684:   if ( !t ) return d;
1.26      noro     3685: get_eg(&eg1);
1.30      noro     3686:   d1 = nd_newpairs_s(t,syz);
1.26      noro     3687: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
1.24      noro     3688:   d = merge_pairs_s(d,d1);
1.26      noro     3689: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
1.24      noro     3690:   return d;
                   3691: }
1.1       noro     3692:
1.47      noro     3693: int update_pairs_array_s( ND_pairs *d, int t,NODE *syz)
                   3694: {
                   3695:   ND_pairs *d1;
                   3696:   struct oEGT eg1,eg2,eg3;
                   3697:   int i;
                   3698:
                   3699:   if ( !t ) return 0;
                   3700: get_eg(&eg1);
                   3701:   Nnewpair = 0;
                   3702:   d1 = nd_newpairs_array_s(t,syz);
                   3703: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
                   3704:   for ( i = 0; i < nd_nbase; i++ )
                   3705:     d[i] = merge_pairs_s(d[i],d1[i]);
                   3706: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
                   3707:   return Nnewpair;
                   3708: }
                   3709:
1.1       noro     3710: ND_pairs nd_newpairs( NODE g, int t )
                   3711: {
                   3712:   NODE h;
                   3713:   UINT *dl;
                   3714:   int ts,s,i,t0,min,max;
                   3715:   ND_pairs r,r0;
                   3716:
                   3717:   dl = DL(nd_psh[t]);
                   3718:   ts = SG(nd_psh[t]) - TD(dl);
1.65    ! noro     3719:   if ( nd_module && nd_intersect && (MPOS(dl) > (UINT)nd_intersect) ) return 0;
1.1       noro     3720:   for ( r0 = 0, h = g; h; h = NEXT(h) ) {
                   3721:     if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
                   3722:       continue;
                   3723:     if ( nd_gbblock ) {
                   3724:       t0 = (long)BDY(h);
                   3725:       for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
                   3726:         min = nd_gbblock[i]; max = nd_gbblock[i+1];
                   3727:         if ( t0 >= min && t0 <= max && t >= min && t <= max )
                   3728:           break;
                   3729:       }
                   3730:       if ( nd_gbblock[i] >= 0 )
                   3731:         continue;
                   3732:     }
1.41      noro     3733:     NEXTND_pairs(r0,r); Npairs++;
1.1       noro     3734:     r->i1 = (long)BDY(h);
                   3735:     r->i2 = t;
                   3736:     ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
                   3737:     s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   3738:     SG(r) = MAX(s,ts) + TD(LCM(r));
                   3739:     /* experimental */
                   3740:     if ( nd_sugarweight )
                   3741:       r->sugar2 = ndl_weight2(r->lcm);
                   3742:   }
                   3743:   if ( r0 ) NEXT(r) = 0;
                   3744:   return r0;
                   3745: }
                   3746:
1.46      noro     3747: int sig_cmpdl_op(int n,DL d1,DL d2)
                   3748: {
                   3749:   int e1,e2,i,j,l;
                   3750:   int *t1,*t2;
1.65    ! noro     3751:   int len;
1.46      noro     3752:   struct order_pair *pair;
                   3753:
                   3754:   len = nd_sba_modord->block_length;
                   3755:   pair = nd_sba_modord->order_pair;
                   3756:
                   3757:   for ( i = 0, t1 = d1->d, t2 = d2->d; i < len; i++ ) {
                   3758:     l = pair[i].length;
                   3759:     switch ( pair[i].order ) {
                   3760:       case 0:
                   3761:         for ( j = 0, e1 = e2 = 0; j < l; j++ ) {
                   3762:           e1 += t1[j];
                   3763:           e2 += t2[j];
                   3764:         }
                   3765:         if ( e1 > e2 )
                   3766:           return 1;
                   3767:         else if ( e1 < e2 )
                   3768:           return -1;
                   3769:         else {
                   3770:           for ( j = l - 1; j >= 0 && t1[j] == t2[j]; j-- );
                   3771:           if ( j >= 0 )
                   3772:             return t1[j] < t2[j] ? 1 : -1;
                   3773:         }
                   3774:         break;
                   3775:       case 1:
                   3776:         for ( j = 0, e1 = e2 = 0; j < l; j++ ) {
                   3777:           e1 += t1[j];
                   3778:           e2 += t2[j];
                   3779:         }
                   3780:         if ( e1 > e2 )
                   3781:           return 1;
                   3782:         else if ( e1 < e2 )
                   3783:           return -1;
                   3784:         else {
                   3785:           for ( j = 0; j < l && t1[j] == t2[j]; j++ );
                   3786:           if ( j < l )
                   3787:             return t1[j] > t2[j] ? 1 : -1;
                   3788:         }
                   3789:         break;
                   3790:       case 2:
                   3791:         for ( j = 0; j < l && t1[j] == t2[j]; j++ );
                   3792:         if ( j < l )
                   3793:           return t1[j] > t2[j] ? 1 : -1;
                   3794:         break;
                   3795:       default:
                   3796:         error("sig_cmpdl_op : invalid order"); break;
                   3797:     }
1.65    ! noro     3798:     t1 += l; t2 += l; // head += l;
1.46      noro     3799:   }
                   3800:   return 0;
                   3801: }
                   3802:
                   3803: int sig_cmpdl_mat(int n,DL d1,DL d2)
                   3804: {
                   3805:   int *v,*t1,*t2;
                   3806:   int s,i,j,len;
                   3807:   int **matrix;
                   3808:   static int *w;
                   3809:   static int nvar = 0;
                   3810:
                   3811:   if ( nvar != n ) {
                   3812:     nvar = n; w = (int *)MALLOC(n*sizeof(int));
                   3813:   }
                   3814:   for ( i = 0, t1 = d1->d, t2 = d2->d; i < n; i++ )
                   3815:     w[i] = t1[i]-t2[i];
                   3816:   len = nd_sba_modord->row;
                   3817:   matrix = nd_sba_modord->matrix;
                   3818:   for ( j = 0; j < len; j++ ) {
                   3819:     v = matrix[j];
                   3820:     for ( i = 0, s = 0; i < n; i++ )
                   3821:       s += v[i]*w[i];
                   3822:     if ( s > 0 )
                   3823:       return 1;
                   3824:     else if ( s < 0 )
                   3825:       return -1;
                   3826:   }
                   3827:   return 0;
                   3828: }
                   3829:
                   3830: struct comp_sig_spec *create_comp_sig_spec(VL current_vl,VL old_vl,Obj ord,Obj weight)
                   3831: {
                   3832:   struct comp_sig_spec *spec;
                   3833:   VL ovl,vl;
                   3834:   V ov;
                   3835:   int i,j,n,nvar,s;
                   3836:   NODE node,t,tn;
                   3837:   struct order_pair *l;
                   3838:   MAT m;
                   3839:   Obj **b;
                   3840:   int **w;
                   3841:   int *a;
                   3842:
                   3843:   spec = (struct comp_sig_spec *)MALLOC(sizeof(struct comp_sig_spec));
                   3844:   for ( i = 0, vl = current_vl; vl; vl = NEXT(vl), i++ );
                   3845:   spec->n = nvar = i;
                   3846:   if ( old_vl != 0 ) {
                   3847:     spec->oldv = (int *)MALLOC(nvar*sizeof(int));
                   3848:     for ( i = 0, ovl = old_vl; i < nvar; ovl = NEXT(ovl), i++ ) {
                   3849:       ov = ovl->v;
                   3850:       for ( j = 0, vl = current_vl; vl; vl = NEXT(vl), j++ )
                   3851:         if ( ov == vl->v ) break;
                   3852:       spec->oldv[i] = j;
                   3853:     }
                   3854:   } else
                   3855:     spec->oldv = 0;
                   3856:   if ( !ord || NUM(ord) ) {
                   3857:     switch ( ZTOS((Z)ord) ) {
                   3858:       case 0:
                   3859:         spec->cmpdl = cmpdl_revgradlex; break;
                   3860:       case 1:
                   3861:         spec->cmpdl = cmpdl_gradlex; break;
                   3862:       case 2:
                   3863:         spec->cmpdl = cmpdl_lex; break;
                   3864:       default:
                   3865:         error("create_comp_sig_spec : invalid spec"); break;
                   3866:     }
                   3867:   } else if ( OID(ord) == O_LIST ) {
                   3868:     node = BDY((LIST)ord);
                   3869:     for ( n = 0, t = node; t; t = NEXT(t), n++ );
                   3870:     l = (struct order_pair *)MALLOC_ATOMIC(n*sizeof(struct order_pair));
                   3871:     for ( i = 0, t = node, s = 0; i < n; t = NEXT(t), i++ ) {
1.65    ! noro     3872:       tn = BDY((LIST)BDY(t)); l[i].order = (int)ZTOS((Q)BDY(tn));
        !          3873:       tn = NEXT(tn); l[i].length = (int)ZTOS((Q)BDY(tn));
1.46      noro     3874:       s += l[i].length;
                   3875:     }
                   3876:     if ( s != nvar )
                   3877:       error("create_comp_sig_spec : invalid spec");
                   3878:     spec->order_pair = l;
                   3879:     spec->block_length = n;
                   3880:     spec->cmpdl = sig_cmpdl_op;
                   3881:   } else if ( OID(ord) == O_MAT ) {
                   3882:     m = (MAT)ord; b = (Obj **)BDY(m);
                   3883:     if ( m->col != nvar )
                   3884:       error("create_comp_sig_spec : invalid spec");
                   3885:     w = almat(m->row,m->col);
                   3886:     for ( i = 0; i < m->row; i++ )
                   3887:       for ( j = 0; j < m->col; j++ )
1.65    ! noro     3888:         w[i][j] = (int)ZTOS((Q)b[i][j]);
1.46      noro     3889:     spec->row = m->row;
                   3890:     spec->matrix = w;
                   3891:     spec->cmpdl = sig_cmpdl_mat;
                   3892:   } else
                   3893:     error("create_comp_sig_spec : invalid spec");
                   3894:   if ( weight != 0 ) {
                   3895:     node = BDY((LIST)weight);
                   3896:     a = (int *)MALLOC(nvar*sizeof(int));
                   3897:     for ( i = 0; i < nvar; i++, node = NEXT(node) )
1.65    ! noro     3898:       a[i] = (int)ZTOS((Z)BDY(node));
1.46      noro     3899:     spec->weight = a;
                   3900:   }
                   3901:   return spec;
                   3902: }
                   3903:
                   3904: #define SIG_MUL_WEIGHT(a,i) (weight?(a)*weight[i]:(a))
                   3905:
                   3906: int comp_sig_monomial(int n,DL d1,DL d2)
                   3907: {
                   3908:   static DL m1,m2;
                   3909:   static int nvar = 0;
                   3910:   int *oldv,*weight;
                   3911:   int i,w1,w2;
                   3912:
                   3913:   if ( nvar != n ) {
                   3914:     nvar = n; NEWDL(m1,nvar); NEWDL(m2,nvar);
                   3915:   }
                   3916:   if ( !nd_sba_modord )
                   3917:     return (*cmpdl)(n,d1,d2);
1.55      noro     3918:   else if ( !nd_sba_modord->weight && !nd_sba_modord->oldv ) {
                   3919:     return (*nd_sba_modord->cmpdl)(n,d1,d2);
                   3920:   } else {
1.46      noro     3921:     weight = nd_sba_modord->weight;
                   3922:     oldv = nd_sba_modord->oldv;
                   3923:     if ( oldv ) {
                   3924:       for ( i = 0; i < n; i++ ) {
                   3925:         m1->d[i] = d1->d[oldv[i]]; m2->d[i] = d2->d[oldv[i]];
                   3926:       }
                   3927:     } else {
                   3928:       for ( i = 0; i < n; i++ ) {
                   3929:         m1->d[i] = d1->d[i]; m2->d[i] = d2->d[i];
                   3930:       }
                   3931:     }
                   3932:     for ( i = 0, w1 = w2 = 0; i < n; i++ ) {
                   3933:       w1 += SIG_MUL_WEIGHT(m1->d[i],i);
                   3934:       w2 += SIG_MUL_WEIGHT(m2->d[i],i);
                   3935:     }
                   3936:     m1->td = w1; m2->td = w2;
                   3937:     return (*nd_sba_modord->cmpdl)(n,m1,m2);
                   3938:   }
                   3939: }
                   3940:
1.24      noro     3941: int comp_sig(SIG s1,SIG s2)
                   3942: {
1.38      noro     3943:   if ( nd_sba_pot ) {
                   3944:     if ( s1->pos > s2->pos ) return 1;
                   3945:     else if ( s1->pos < s2->pos ) return -1;
1.55      noro     3946:     else return comp_sig_monomial(nd_nvar,DL(s1),DL(s2));
1.38      noro     3947:   } else {
                   3948:     static DL m1,m2;
1.44      noro     3949:     static int nvar = 0;
1.38      noro     3950:     int ret;
                   3951:
                   3952:     if ( nvar != nd_nvar ) {
                   3953:       nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
                   3954:     }
1.47      noro     3955:     if ( !nd_sba_modord )
1.55      noro     3956:       ret = (*cmpdl)(nd_nvar,DL2(s1),DL2(s2));
1.47      noro     3957:     else
1.55      noro     3958:       ret = comp_sig_monomial(nd_nvar,DL2(s1),DL2(s2));
1.38      noro     3959:     if ( ret != 0 ) return ret;
                   3960:     else if ( s1->pos > s2->pos ) return 1;
                   3961:     else if ( s1->pos < s2->pos ) return -1;
                   3962:     else return 0;
1.24      noro     3963:   }
                   3964: }
                   3965:
                   3966: int _create_spair_s(int i1,int i2,ND_pairs sp,SIG sig1,SIG sig2)
                   3967: {
                   3968:   int ret,s1,s2;
                   3969:   RHist p1,p2;
                   3970:   static int wpd;
                   3971:   static UINT *lcm;
                   3972:
                   3973:   sp->i1 = i1;
                   3974:   sp->i2 = i2;
                   3975:   p1 = nd_psh[i1];
                   3976:   p2 = nd_psh[i2];
                   3977:   ndl_lcm(DL(p1),DL(p2),sp->lcm);
1.49      noro     3978: #if 0
1.24      noro     3979:   s1 = SG(p1)-TD(DL(p1));
                   3980:   s2 = SG(p2)-TD(DL(p2));
                   3981:   SG(sp) = MAX(s1,s2) + TD(sp->lcm);
1.49      noro     3982: #endif
1.24      noro     3983:
1.26      noro     3984:   if ( wpd != nd_wpd ) {
1.24      noro     3985:     wpd = nd_wpd;
                   3986:     lcm = (UINT *)MALLOC(wpd*sizeof(UINT));
                   3987:   }
                   3988:   // DL(sig1) <- sp->lcm
                   3989:   // DL(sig1) -= DL(p1)
                   3990:   // DL(sig1) += DL(p1->sig)
1.27      noro     3991:   ndl_sub(sp->lcm,DL(p1),lcm);
1.24      noro     3992:   _ndltodl(lcm,DL(sig1));
                   3993:   _addtodl(nd_nvar,DL(p1->sig),DL(sig1));
                   3994:   sig1->pos = p1->sig->pos;
1.55      noro     3995:   _adddl(nd_nvar,DL(sig1),nd_sba_hm[sig1->pos],DL2(sig1));
1.24      noro     3996:
                   3997:   // DL(sig2) <- sp->lcm
                   3998:   // DL(sig2) -= DL(p2)
                   3999:   // DL(sig2) += DL(p2->sig)
1.27      noro     4000:   ndl_sub(sp->lcm,DL(p2),lcm);
1.24      noro     4001:   _ndltodl(lcm,DL(sig2));
                   4002:   _addtodl(nd_nvar,DL(p2->sig),DL(sig2));
                   4003:   sig2->pos = p2->sig->pos;
1.55      noro     4004:   _adddl(nd_nvar,DL(sig2),nd_sba_hm[sig2->pos],DL2(sig2));
1.24      noro     4005:
                   4006:   ret = comp_sig(sig1,sig2);
                   4007:   if ( ret == 0 ) return 0;
                   4008:   else if ( ret > 0 ) sp->sig = sig1;
                   4009:   else sp->sig = sig2;
1.49      noro     4010:
                   4011:   s1 = DL(sig1)->td+nd_sba_hm[p1->sig->pos]->td;
                   4012:   s2 = DL(sig2)->td+nd_sba_hm[p2->sig->pos]->td;
                   4013:   SG(sp) = MAX(s1,s2);
                   4014:
1.24      noro     4015:   return 1;
                   4016: }
                   4017:
                   4018: SIG dup_sig(SIG sig)
                   4019: {
                   4020:   SIG r;
                   4021:
                   4022:   if ( !sig ) return 0;
                   4023:   else {
                   4024:     NEWSIG(r);
                   4025:     _copydl(nd_nvar,DL(sig),DL(r));
1.55      noro     4026:     _copydl(nd_nvar,DL2(sig),DL2(r));
1.24      noro     4027:     r->pos = sig->pos;
                   4028:     return r;
                   4029:   }
                   4030: }
                   4031:
                   4032: void dup_ND_pairs(ND_pairs to,ND_pairs from)
                   4033: {
                   4034:   to->i1 = from->i1;
                   4035:   to->i2 = from->i2;
                   4036:   to->sugar = from->sugar;
                   4037:   to->sugar2 = from->sugar2;
                   4038:   ndl_copy(from->lcm,to->lcm);
                   4039:   to->sig = dup_sig(from->sig);
                   4040: }
                   4041:
                   4042: ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
                   4043: {
                   4044:   struct oND_pairs root;
1.65    ! noro     4045:   ND_pairs q1,q2,r;
1.24      noro     4046:   int ret;
                   4047:
                   4048:   r = &root;
                   4049:   for ( q1 = p1, q2 = p2; q1 != 0 && q2 != 0; ) {
                   4050:     ret = comp_sig(q1->sig,q2->sig);
                   4051:     if ( ret < 0 ) {
                   4052:       r->next = q1; r = q1; q1 = q1->next;
                   4053:     } else if ( ret > 0 ) {
                   4054:       r->next = q2; r = q2; q2 = q2->next;
                   4055:     } else {
1.47      noro     4056:       Nnewpair--;
1.24      noro     4057:       ret = DL_COMPARE(q1->lcm,q2->lcm);
1.35      noro     4058:       Nsamesig++;
1.24      noro     4059:       if ( ret < 0 ) {
                   4060:         r->next = q1; r = q1; q1 = q1->next;
                   4061:         q2 = q2->next;
                   4062:       } else {
                   4063:         r->next = q2; r = q2; q2 = q2->next;
                   4064:         q1 = q1->next;
                   4065:       }
                   4066:     }
                   4067:   }
                   4068:   if ( q1 ) {
                   4069:     r->next = q1;
                   4070:   } else {
                   4071:     r->next = q2;
                   4072:   }
                   4073:   return root.next;
                   4074: }
                   4075:
                   4076: ND_pairs insert_pair_s(ND_pairs l,ND_pairs s)
                   4077: {
                   4078:   ND_pairs p,prev;
1.47      noro     4079:   int ret=1;
1.24      noro     4080:
                   4081:   for ( p = l, prev = 0; p != 0; prev = p, p = p->next ) {
                   4082:     if ( (ret = comp_sig(s->sig,p->sig)) <= 0 )
                   4083:       break;
                   4084:   }
                   4085:   if ( ret == 0 ) {
                   4086:     ret = DL_COMPARE(s->lcm,p->lcm);
                   4087:     if ( ret < 0 ) {
                   4088:       // replace p with s
                   4089:       s->next = p->next;
                   4090:       if ( prev == 0 ) {
                   4091:         return s;
                   4092:       } else {
                   4093:         prev->next = s;
                   4094:         return l;
                   4095:       }
                   4096:     } else
                   4097:       return l;
                   4098:   } else {
                   4099:     // insert s between prev and p
1.47      noro     4100:     Nnewpair++;
1.24      noro     4101:     s->next = p;
                   4102:     if ( prev == 0 ) {
                   4103:       return s;
                   4104:     } else {
                   4105:       prev->next = s;
                   4106:       return l;
                   4107:     }
                   4108:   }
                   4109: }
                   4110:
1.29      noro     4111: INLINE int __dl_redble(DL d1,DL d2,int nvar)
                   4112: {
                   4113:   int i;
                   4114:
                   4115:   if ( d1->td > d2->td )
                   4116:     return 0;
                   4117:   for ( i = nvar-1; i >= 0; i-- )
                   4118:     if ( d1->d[i] > d2->d[i] )
                   4119:       break;
                   4120:   if ( i >= 0 )
                   4121:     return 0;
                   4122:   else
                   4123:     return 1;
                   4124: }
                   4125:
1.30      noro     4126: ND_pairs nd_newpairs_s(int t, NODE *syz)
1.24      noro     4127: {
1.65    ! noro     4128:   NODE s;
        !          4129:   int ret,i;
        !          4130:   ND_pairs r0,_sp,sp;
1.29      noro     4131:   SIG spsig,tsig;
1.44      noro     4132:   static int nvar = 0;
1.29      noro     4133:   static SIG _sig1,_sig2;
1.24      noro     4134:
                   4135:   NEWND_pairs(_sp);
1.29      noro     4136:   if ( !_sig1 || nvar != nd_nvar ) {
                   4137:     nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
                   4138:   }
1.24      noro     4139:   r0 = 0;
1.27      noro     4140:   for ( i = 0; i < t; i++ ) {
                   4141:     ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
1.24      noro     4142:     if ( ret ) {
                   4143:       spsig = _sp->sig;
1.29      noro     4144:       for ( s = syz[spsig->pos]; s; s = s->next ) {
1.24      noro     4145:         tsig = (SIG)s->body;
1.29      noro     4146:         if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
1.24      noro     4147:           break;
                   4148:       }
                   4149:       if ( s == 0 ) {
                   4150:         NEWND_pairs(sp);
                   4151:         dup_ND_pairs(sp,_sp);
                   4152:         r0 = insert_pair_s(r0,sp);
1.26      noro     4153:       } else
                   4154:         Nsyz++;
1.24      noro     4155:     }
                   4156:   }
                   4157:   return r0;
                   4158: }
                   4159:
1.47      noro     4160: ND_pairs *nd_newpairs_array_s(int t, NODE *syz)
                   4161: {
1.65    ! noro     4162:   NODE s;
        !          4163:   int ret,i;
        !          4164:   ND_pairs _sp,sp;
1.47      noro     4165:   ND_pairs *d;
                   4166:   SIG spsig,tsig;
                   4167:   static int nvar = 0;
                   4168:   static SIG _sig1,_sig2;
                   4169:
                   4170:   NEWND_pairs(_sp);
                   4171:   if ( !_sig1 || nvar != nd_nvar ) {
                   4172:     nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
                   4173:   }
                   4174:   d = (ND_pairs *)MALLOC(nd_nbase*sizeof(ND_pairs));
                   4175:   Nnewpair = 0;
                   4176:   for ( i = 0; i < t; i++ ) {
                   4177:     ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
                   4178:     if ( ret ) {
                   4179:       spsig = _sp->sig;
                   4180:       for ( s = syz[spsig->pos]; s; s = s->next ) {
                   4181:         tsig = (SIG)s->body;
                   4182:         if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
                   4183:           break;
                   4184:       }
                   4185:       if ( s == 0 ) {
                   4186:         NEWND_pairs(sp);
                   4187:         dup_ND_pairs(sp,_sp);
                   4188:         d[spsig->pos] = insert_pair_s(d[spsig->pos],sp);
                   4189:       } else
                   4190:         Nsyz++;
                   4191:     }
                   4192:   }
                   4193:   return d;
                   4194: }
                   4195:
1.1       noro     4196: /* ipair = [i1,i2],[i1,i2],... */
                   4197: ND_pairs nd_ipairtospair(NODE ipair)
                   4198: {
                   4199:   int s1,s2;
                   4200:   NODE tn,t;
                   4201:   ND_pairs r,r0;
                   4202:
                   4203:   for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
                   4204:     NEXTND_pairs(r0,r);
                   4205:     tn = BDY((LIST)BDY(t));
1.65    ! noro     4206:     r->i1 = (int)ZTOS((Q)ARG0(tn));
        !          4207:     r->i2 = (int)ZTOS((Q)ARG1(tn));
1.1       noro     4208:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
                   4209:     s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   4210:     s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
                   4211:     SG(r) = MAX(s1,s2) + TD(LCM(r));
                   4212:     /* experimental */
                   4213:     if ( nd_sugarweight )
                   4214:         r->sugar2 = ndl_weight2(r->lcm);
                   4215:   }
                   4216:   if ( r0 ) NEXT(r) = 0;
                   4217:   return r0;
                   4218: }
                   4219:
                   4220: /* kokokara */
                   4221:
                   4222: ND_pairs crit_B( ND_pairs d, int s )
                   4223: {
                   4224:   ND_pairs cur,head,prev,remove;
                   4225:   UINT *t,*tl,*lcm;
                   4226:
                   4227:   if ( !d ) return 0;
                   4228:   t = DL(nd_psh[s]);
                   4229:   prev = 0;
                   4230:   head = cur = d;
                   4231:   lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   4232:   while ( cur ) {
                   4233:     tl = cur->lcm;
                   4234:     if ( ndl_reducible(tl,t) ) {
                   4235:       ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
                   4236:       if ( !ndl_equal(lcm,tl) ) {
                   4237:         ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
                   4238:         if (!ndl_equal(lcm,tl)) {
                   4239:           remove = cur;
                   4240:           if ( !prev ) {
                   4241:             head = cur = NEXT(cur);
                   4242:           } else {
                   4243:             cur = NEXT(prev) = NEXT(cur);
                   4244:           }
1.41      noro     4245:           FREENDP(remove); NcriB++;
1.1       noro     4246:         } else {
                   4247:           prev = cur; cur = NEXT(cur);
                   4248:         }
                   4249:       } else {
                   4250:         prev = cur; cur = NEXT(cur);
                   4251:       }
                   4252:     } else {
                   4253:       prev = cur; cur = NEXT(cur);
                   4254:     }
                   4255:   }
                   4256:   return head;
                   4257: }
                   4258:
                   4259: ND_pairs crit_M( ND_pairs d1 )
                   4260: {
                   4261:     ND_pairs e,d2,d3,dd,p;
                   4262:     UINT *id,*jd;
                   4263:
                   4264:     if ( !d1 ) return d1;
                   4265:     for ( dd = 0, e = d1; e; e = d3 ) {
                   4266:         if ( !(d2 = NEXT(e)) ) {
                   4267:             NEXT(e) = dd;
                   4268:             return e;
                   4269:         }
                   4270:         id = LCM(e);
                   4271:         for ( d3 = 0; d2; d2 = p ) {
                   4272:             p = NEXT(d2);
                   4273:             jd = LCM(d2);
                   4274:             if ( ndl_equal(jd,id) )
                   4275:                 ;
                   4276:             else if ( TD(jd) > TD(id) )
                   4277:                 if ( ndl_reducible(jd,id) ) continue;
                   4278:                 else ;
                   4279:             else if ( ndl_reducible(id,jd) ) goto delit;
                   4280:             NEXT(d2) = d3;
                   4281:             d3 = d2;
                   4282:         }
                   4283:         NEXT(e) = dd;
                   4284:         dd = e;
                   4285:         continue;
                   4286:         /**/
                   4287:     delit:    NEXT(d2) = d3;
                   4288:         d3 = d2;
                   4289:         for ( ; p; p = d2 ) {
                   4290:             d2 = NEXT(p);
                   4291:             NEXT(p) = d3;
                   4292:             d3 = p;
                   4293:         }
                   4294:         FREENDP(e);
                   4295:     }
                   4296:     return dd;
                   4297: }
                   4298:
                   4299: ND_pairs crit_F( ND_pairs d1 )
                   4300: {
                   4301:     ND_pairs rest, head,remove;
                   4302:     ND_pairs last, p, r, w;
                   4303:     int s;
                   4304:
                   4305:     if ( !d1 ) return d1;
                   4306:     for ( head = last = 0, p = d1; NEXT(p); ) {
                   4307:         r = w = equivalent_pairs(p,&rest);
                   4308:         s = SG(r);
                   4309:         w = NEXT(w);
                   4310:         while ( w ) {
                   4311:             if ( crit_2(w->i1,w->i2) ) {
                   4312:                 r = w;
                   4313:                 w = NEXT(w);
                   4314:                 while ( w ) {
                   4315:                     remove = w;
                   4316:                     w = NEXT(w);
                   4317:                     FREENDP(remove);
                   4318:                 }
                   4319:                 break;
                   4320:             } else if ( SG(w) < s ) {
                   4321:                 FREENDP(r);
                   4322:                 r = w;
                   4323:                 s = SG(r);
                   4324:                 w = NEXT(w);
                   4325:             } else {
                   4326:                 remove = w;
                   4327:                 w = NEXT(w);
                   4328:                 FREENDP(remove);
                   4329:             }
                   4330:         }
                   4331:         if ( last ) NEXT(last) = r;
                   4332:         else head = r;
                   4333:         NEXT(last = r) = 0;
                   4334:         p = rest;
                   4335:         if ( !p ) return head;
                   4336:     }
                   4337:     if ( !last ) return p;
                   4338:     NEXT(last) = p;
                   4339:     return head;
                   4340: }
                   4341:
                   4342: int crit_2( int dp1, int dp2 )
                   4343: {
                   4344:     return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
                   4345: }
                   4346:
                   4347: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
                   4348: {
                   4349:     ND_pairs w,p,r,s;
                   4350:     UINT *d;
                   4351:
                   4352:     w = d1;
                   4353:     d = LCM(w);
                   4354:     s = NEXT(w);
                   4355:     NEXT(w) = 0;
                   4356:     for ( r = 0; s; s = p ) {
                   4357:         p = NEXT(s);
                   4358:         if ( ndl_equal(d,LCM(s)) ) {
                   4359:             NEXT(s) = w; w = s;
                   4360:         } else {
                   4361:             NEXT(s) = r; r = s;
                   4362:         }
                   4363:     }
                   4364:     *prest = r;
                   4365:     return w;
                   4366: }
                   4367:
                   4368: NODE update_base(NODE nd,int ndp)
                   4369: {
                   4370:     UINT *dl, *dln;
                   4371:     NODE last, p, head;
                   4372:
                   4373:     dl = DL(nd_psh[ndp]);
                   4374:     for ( head = last = 0, p = nd; p; ) {
                   4375:         dln = DL(nd_psh[(long)BDY(p)]);
                   4376:         if ( ndl_reducible( dln, dl ) ) {
                   4377:             p = NEXT(p);
                   4378:             if ( last ) NEXT(last) = p;
                   4379:         } else {
                   4380:             if ( !last ) head = p;
                   4381:             p = NEXT(last = p);
                   4382:         }
                   4383:     }
                   4384:     head = append_one(head,ndp);
                   4385:     return head;
                   4386: }
                   4387:
                   4388: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   4389: {
                   4390:     ND_pairs m,ml,p,l;
1.65    ! noro     4391:     int s;
1.1       noro     4392:
                   4393:     if ( !(p = NEXT(m = d)) ) {
                   4394:         *prest = p;
                   4395:         NEXT(m) = 0;
                   4396:         return m;
                   4397:     }
                   4398:     if ( !NoSugar ) {
                   4399:         if ( nd_sugarweight ) {
                   4400:             s = m->sugar2;
                   4401:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   4402:                 if ( (p->sugar2 < s)
                   4403:                     || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   4404:                     ml = l; m = p; s = m->sugar2;
                   4405:                 }
                   4406:         } else {
                   4407:             s = SG(m);
                   4408:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   4409:                 if ( (SG(p) < s)
                   4410:                     || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   4411:                     ml = l; m = p; s = SG(m);
                   4412:                 }
                   4413:         }
                   4414:     } else {
                   4415:         for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   4416:             if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
                   4417:                 ml = l; m = p; s = SG(m);
                   4418:             }
                   4419:     }
                   4420:     if ( !ml ) *prest = NEXT(m);
                   4421:     else {
                   4422:         NEXT(ml) = NEXT(m);
                   4423:         *prest = d;
                   4424:     }
                   4425:     NEXT(m) = 0;
                   4426:     return m;
                   4427: }
                   4428:
                   4429: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   4430: {
                   4431:     int msugar,i;
                   4432:     ND_pairs t,dm0,dm,dr0,dr;
                   4433:
                   4434:     if ( nd_sugarweight ) {
                   4435:         for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
                   4436:             if ( t->sugar2 < msugar ) msugar = t->sugar2;
                   4437:         dm0 = 0; dr0 = 0;
                   4438:         for ( i = 0, t = d; t; t = NEXT(t) )
                   4439:             if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
                   4440:                 if ( dm0 ) NEXT(dm) = t;
                   4441:                 else dm0 = t;
                   4442:                 dm = t;
                   4443:                 i++;
                   4444:             } else {
                   4445:                 if ( dr0 ) NEXT(dr) = t;
                   4446:                 else dr0 = t;
                   4447:                 dr = t;
                   4448:             }
                   4449:     } else {
                   4450:         for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   4451:             if ( SG(t) < msugar ) msugar = SG(t);
                   4452:         dm0 = 0; dr0 = 0;
                   4453:         for ( i = 0, t = d; t; t = NEXT(t) )
                   4454:             if ( i < nd_f4_nsp && SG(t) == msugar ) {
                   4455:                 if ( dm0 ) NEXT(dm) = t;
                   4456:                 else dm0 = t;
                   4457:                 dm = t;
                   4458:                 i++;
                   4459:             } else {
                   4460:                 if ( dr0 ) NEXT(dr) = t;
                   4461:                 else dr0 = t;
                   4462:                 dr = t;
                   4463:             }
                   4464:     }
                   4465:     NEXT(dm) = 0;
                   4466:     if ( dr0 ) NEXT(dr) = 0;
                   4467:     *prest = dr0;
                   4468:     return dm0;
                   4469: }
                   4470:
1.28      noro     4471: ND_pairs nd_minsugarp_s( ND_pairs d, ND_pairs *prest )
                   4472: {
                   4473:   int msugar;
                   4474:   ND_pairs t,last;
                   4475:
                   4476:   for ( msugar = SG(d), t = d; t; t = NEXT(t) )
                   4477:     if ( SG(t) == msugar ) last = t;
                   4478:   *prest = last->next;
                   4479:   last->next = 0;
                   4480:   return d;
                   4481: }
                   4482:
1.1       noro     4483: int nd_tdeg(NDV c)
                   4484: {
                   4485:   int wmax = 0;
                   4486:   int i,len;
                   4487:   NMV a;
                   4488:
                   4489:   len = LEN(c);
                   4490:   for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
1.65    ! noro     4491:     wmax = MAX((int)TD(DL(a)),wmax);
1.1       noro     4492:   return wmax;
                   4493: }
                   4494:
1.24      noro     4495: int ndv_newps(int m,NDV a,NDV aq)
1.1       noro     4496: {
                   4497:     RHist r;
                   4498:     NODE tn;
                   4499:     LIST l;
                   4500:     Z iq;
                   4501:
                   4502:     if ( nd_psn == nd_pslen ) {
                   4503:         nd_pslen *= 2;
                   4504:         nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
                   4505:         nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
                   4506:         nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
                   4507:         nd_bound = (UINT **)
                   4508:             REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
                   4509:         nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
                   4510:         nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
                   4511:     }
                   4512:     NEWRHist(r); nd_psh[nd_psn] = r;
                   4513:     nd_ps[nd_psn] = a;
                   4514:     if ( aq ) {
                   4515:         nd_ps_trace[nd_psn] = aq;
                   4516:         if ( !m ) {
                   4517:           register_hcf(aq);
                   4518:         } else if ( m == -2 ) {
                   4519:           /* do nothing */
                   4520:         } else
                   4521:           error("ndv_newps : invalud modulus");
                   4522:         nd_bound[nd_psn] = ndv_compute_bound(aq);
                   4523: #if 1
                   4524:         SG(r) = SG(aq);
                   4525: #else
                   4526:         SG(r) = nd_tdeg(aq);
                   4527: #endif
                   4528:         ndl_copy(HDL(aq),DL(r));
1.24      noro     4529:         r->sig = dup_sig(aq->sig);
1.1       noro     4530:     } else {
                   4531:         if ( !m ) register_hcf(a);
                   4532:         nd_bound[nd_psn] = ndv_compute_bound(a);
                   4533: #if 1
                   4534:         SG(r) = SG(a);
                   4535: #else
                   4536:         SG(r) = nd_tdeg(a);
                   4537: #endif
                   4538:         ndl_copy(HDL(a),DL(r));
1.24      noro     4539:         r->sig = dup_sig(a->sig);
1.1       noro     4540:     }
                   4541:     if ( nd_demand ) {
                   4542:         if ( aq ) {
                   4543:             ndv_save(nd_ps_trace[nd_psn],nd_psn);
                   4544:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   4545:             nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   4546:             nd_ps_trace[nd_psn] = 0;
                   4547:         } else {
                   4548:             ndv_save(nd_ps[nd_psn],nd_psn);
                   4549:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
                   4550:             nd_ps[nd_psn] = 0;
                   4551:         }
                   4552:     }
                   4553:     if ( nd_gentrace ) {
                   4554:         /* reverse the tracelist and append it to alltracelist */
                   4555:         nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6       noro     4556:         STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1       noro     4557:         MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
                   4558:     }
                   4559:     return nd_psn++;
                   4560: }
                   4561:
1.46      noro     4562: // find LM wrt the specified modord
                   4563: void ndv_lm_modord(NDV p,DL d)
1.45      noro     4564: {
                   4565:   NMV m;
                   4566:   DL tmp;
                   4567:   int len,i,ret;
                   4568:
                   4569:   NEWDL(tmp,nd_nvar);
                   4570:   m = BDY(p); len = LEN(p);
1.46      noro     4571:   _ndltodl(DL(m),d); // printdl(d); printf("->");
1.45      noro     4572:   for ( i = 1, NMV_ADV(m); i < len; i++, NMV_ADV(m) ) {
                   4573:     _ndltodl(DL(m),tmp);
1.46      noro     4574:     ret = comp_sig_monomial(nd_nvar,tmp,d);
1.45      noro     4575:     if ( ret > 0 ) _copydl(nd_nvar,tmp,d);
                   4576:   }
1.46      noro     4577: //   printdl(d); printf("\n");
1.45      noro     4578: }
                   4579:
1.1       noro     4580: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
                   4581: /* return 1 if success, 0 if failure (HC(a mod p)) */
                   4582:
1.24      noro     4583: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont,int sba)
1.1       noro     4584: {
1.65    ! noro     4585:   int i,j;
        !          4586:   NODE s,tn;
1.6       noro     4587:   RHist r;
                   4588:   NDVI w;
                   4589:   NDV a,am;
                   4590:   union oNDC hc;
                   4591:   NODE node;
                   4592:   Z iq,jq;
                   4593:   LIST l;
                   4594:
                   4595:   nd_found = 0; nd_notfirst = 0; nd_create = 0;
                   4596:   /* initialize the tracelist */
                   4597:   nd_tracelist = 0;
                   4598:
                   4599:   for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
                   4600:   w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
                   4601:   for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
                   4602:     if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
                   4603:   if ( !dont_sort ) {
                   4604:     /* XXX heuristic */
1.26      noro     4605:     if ( !sba && !nd_ord->id && (nd_ord->ord.simple<2) )
1.6       noro     4606:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   4607:         (int (*)(const void *,const void *))ndvi_compare_rev);
                   4608:     else
                   4609:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   4610:         (int (*)(const void *,const void *))ndvi_compare);
                   4611:   }
                   4612:   nd_pslen = 2*nd_psn;
                   4613:   nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4614:   nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4615:   nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4616:   nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4617:   nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
                   4618:   nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
                   4619:   nd_hcf = 0;
1.1       noro     4620:
1.6       noro     4621:   if ( trace && nd_vc )
                   4622:     makesubst(nd_vc,&nd_subst);
                   4623:   else
                   4624:     nd_subst = 0;
1.1       noro     4625:
1.6       noro     4626:   if ( !nd_red )
                   4627:     nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   4628:   for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
                   4629:   for ( i = 0; i < nd_psn; i++ ) {
                   4630:     hc = HCU(w[i].p);
                   4631:     if ( trace ) {
                   4632:       if ( mod == -2 ) {
                   4633:         /* over a large finite field */
                   4634:         /* trace = small modulus */
                   4635:         a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
                   4636:         ndv_mod(-2,a);
                   4637:         if ( !dont_removecont) ndv_removecont(-2,a);
                   4638:         am = nd_ps[i] = ndv_dup(trace,w[i].p);
                   4639:         ndv_mod(trace,am);
1.65    ! noro     4640:         if ( DL_COMPARE(HDL(am),HDL(a)) ) return 0;
1.6       noro     4641:         ndv_removecont(trace,am);
                   4642:       } else {
                   4643:         a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
                   4644:         if ( !dont_removecont) ndv_removecont(0,a);
                   4645:         register_hcf(a);
                   4646:         am = nd_ps[i] = ndv_dup(mod,a);
                   4647:         ndv_mod(mod,am);
1.65    ! noro     4648:         if ( DL_COMPARE(HDL(am),HDL(a)) ) return 0;
1.6       noro     4649:         ndv_removecont(mod,am);
                   4650:       }
                   4651:     } else {
                   4652:       a = nd_ps[i] = ndv_dup(mod,w[i].p);
                   4653:       if ( mod || !dont_removecont ) ndv_removecont(mod,a);
                   4654:       if ( !mod ) register_hcf(a);
1.1       noro     4655:     }
1.6       noro     4656:     if ( nd_gentrace ) {
                   4657:       STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
                   4658:       /* exact division */
1.1       noro     4659:       if ( !dont_removecont )
1.6       noro     4660:         ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
                   4661:       MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
                   4662:     }
                   4663:     NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
                   4664:     nd_bound[i] = ndv_compute_bound(a);
                   4665:     nd_psh[i] = r;
                   4666:     if ( nd_demand ) {
                   4667:       if ( trace ) {
                   4668:         ndv_save(nd_ps_trace[i],i);
                   4669:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   4670:         nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   4671:         nd_ps_trace[i] = 0;
                   4672:       } else {
                   4673:         ndv_save(nd_ps[i],i);
                   4674:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
                   4675:         nd_ps[i] = 0;
                   4676:       }
1.1       noro     4677:     }
1.6       noro     4678:   }
1.24      noro     4679:   if ( sba ) {
1.27      noro     4680:     nd_sba_hm = (DL *)MALLOC(nd_psn*sizeof(DL));
1.24      noro     4681:    // setup signatures
1.27      noro     4682:     for ( i = 0; i < nd_psn; i++ ) {
                   4683:       SIG sig;
1.24      noro     4684:
1.27      noro     4685:       NEWSIG(sig); sig->pos = i;
                   4686:       nd_ps[i]->sig = sig;
                   4687:       if ( nd_demand ) nd_ps_sym[i]->sig = sig;
                   4688:         nd_psh[i]->sig = sig;
                   4689:       if ( trace ) {
                   4690:         nd_ps_trace[i]->sig = sig;
                   4691:         if ( nd_demand ) nd_ps_trace_sym[i]->sig = sig;
                   4692:       }
                   4693:       NEWDL(nd_sba_hm[i],nd_nvar);
1.46      noro     4694:       if ( nd_sba_modord )
                   4695:         ndv_lm_modord(nd_ps[i],nd_sba_hm[i]);
1.45      noro     4696:       else
                   4697:         _ndltodl(DL(nd_psh[i]),nd_sba_hm[i]);
1.55      noro     4698:       _adddl(nd_nvar,DL(sig),nd_sba_hm[i],DL2(sig));
1.27      noro     4699:     }
1.29      noro     4700:     nd_sba_pos = (NODE *)MALLOC(nd_psn*sizeof(NODE));
                   4701:     for ( i = 0; i < nd_psn; i++ ) {
                   4702:       j = nd_psh[i]->sig->pos;
                   4703:       nd_sba_pos[j] = append_one(nd_sba_pos[j],i);
                   4704:     }
1.24      noro     4705:   }
1.6       noro     4706:   if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
                   4707:   return 1;
1.1       noro     4708: }
                   4709:
                   4710: struct order_spec *append_block(struct order_spec *spec,
                   4711:     int nv,int nalg,int ord);
                   4712:
                   4713: extern VECT current_dl_weight_vector_obj;
                   4714: static VECT prev_weight_vector_obj;
                   4715:
                   4716: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
                   4717:     struct order_spec **ord1p,LIST *f1p,NODE *alistp)
                   4718: {
1.65    ! noro     4719:     NODE alist,t,s,arg;
1.1       noro     4720:     VL tv;
                   4721:     P poly;
                   4722:     DP d;
                   4723:     Alg alpha,dp;
                   4724:     DAlg inv,da,hc;
                   4725:     MP m;
                   4726:     int i,nvar,nalg,n;
                   4727:     NumberField nf;
1.65    ! noro     4728:     LIST f1;
1.1       noro     4729:     VECT obj,obj0;
                   4730:     VECT tmp;
                   4731:
                   4732:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
                   4733:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
                   4734:
                   4735:     for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
                   4736:         NEXTNODE(alist,t); MKV(tv->v,poly);
                   4737:         MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
                   4738:         tv->v = tv->v->priv;
                   4739:     }
                   4740:     NEXT(t) = 0;
                   4741:
                   4742:     /* simplification, making polynomials monic */
                   4743:     setfield_dalg(alist);
                   4744:     obj_algtodalg((Obj)f,(Obj *)&f1);
                   4745:     for ( t = BDY(f); t; t = NEXT(t) ) {
                   4746:         initd(ord); ptod(vv,vv,(P)BDY(t),&d);
                   4747:         hc = (DAlg)BDY(d)->c;
                   4748:         if ( NID(hc) == N_DA ) {
                   4749:             invdalg(hc,&inv);
                   4750:             for ( m = BDY(d); m; m = NEXT(m) ) {
                   4751:                 muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
                   4752:             }
                   4753:         }
                   4754:         initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
                   4755:     }
                   4756:     obj_dalgtoalg((Obj)f1,(Obj *)&f);
                   4757:
                   4758:     /* append alg vars to the var list */
                   4759:     for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
                   4760:     NEXT(tv) = av;
                   4761:
                   4762:     /* append a block to ord */
                   4763:     *ord1p = append_block(ord,nvar,nalg,2);
                   4764:
                   4765:     /* create generator list */
                   4766:     nf = get_numberfield();
                   4767:     for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
                   4768:         MKAlg(nf->defpoly[i],dp);
                   4769:         MKNODE(s,dp,t); t = s;
                   4770:     }
                   4771:     MKLIST(f1,t);
                   4772:     *alistp = alist;
                   4773:     algobjtorat((Obj)f1,(Obj *)f1p);
                   4774:
                   4775:     /* creating a new weight vector */
                   4776:     prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
                   4777:     n = nvar+nalg+1;
                   4778:     MKVECT(obj,n);
                   4779:     if ( obj0 && obj0->len == nvar )
                   4780:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
                   4781:     else
                   4782:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
                   4783:     for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
                   4784:     BDY(obj)[n-1] = (pointer)ONE;
                   4785:     arg = mknode(1,obj);
                   4786:     Pdp_set_weight(arg,&tmp);
                   4787: }
                   4788:
                   4789: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
                   4790: {
                   4791:     NODE s,t,u0,u;
                   4792:     P p;
                   4793:     VL tv;
                   4794:     Obj obj;
                   4795:     VECT tmp;
                   4796:     NODE arg;
                   4797:
                   4798:     u0 = 0;
                   4799:     for ( t = r; t; t = NEXT(t) ) {
                   4800:         p = (P)BDY(t);
                   4801:         for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
                   4802:             substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
                   4803:         }
                   4804:         if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
                   4805:             NEXTNODE(u0,u);
                   4806:             BDY(u) = (pointer)p;
                   4807:         }
                   4808:     }
                   4809:     arg = mknode(1,prev_weight_vector_obj);
                   4810:     Pdp_set_weight(arg,&tmp);
                   4811:
                   4812:     return u0;
                   4813: }
                   4814:
                   4815: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
                   4816: {
1.65    ! noro     4817:   VL tv,fv,vv,av;
        !          4818:   NODE fd,fd0,r0,t,x,s,xx,alist,r;
        !          4819:   int e,max,nvar,i;
        !          4820:   NDV b;
        !          4821:   int ishomo,nalg,mrank,trank,wmax,len;
1.1       noro     4822:   NMV a;
1.65    ! noro     4823:   P zp;
        !          4824:   Q dmy;
        !          4825:   LIST f1,zpl;
        !          4826:   struct order_spec *ord1;
        !          4827:   NODE tr,tl1,tl2,tl3,tl4,nzlist;
        !          4828:   LIST l1,l2,l3,l4,l5;
1.1       noro     4829:   int j;
                   4830:   Z jq,bpe,last_nonzero;
1.65    ! noro     4831:   int *perm;
        !          4832:   EPOS oepos;
        !          4833:   int obpe,oadv,ompos,cbpe;
        !          4834:   VECT hvect;
1.1       noro     4835:
1.65    ! noro     4836:   NcriB = NcriMF = Ncri2 = 0;
        !          4837:   nd_module = 0;
        !          4838:   if ( !m && Demand ) nd_demand = 1;
        !          4839:   else nd_demand = 0;
1.1       noro     4840:
1.65    ! noro     4841:   if ( DP_Multiple )
        !          4842:     nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
1.1       noro     4843: #if 0
1.65    ! noro     4844:   ndv_alloc = 0;
1.1       noro     4845: #endif
1.65    ! noro     4846:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
        !          4847:   parse_nd_option(vv,current_option);
        !          4848:   if ( m && nd_vc )
        !          4849:     error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
        !          4850:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
        !          4851:   switch ( ord->id ) {
        !          4852:     case 1:
        !          4853:       if ( ord->nv != nvar )
        !          4854:         error("nd_{gr,f4} : invalid order specification");
        !          4855:       break;
        !          4856:     default:
        !          4857:       break;
        !          4858:   }
        !          4859:   nd_nalg = 0;
        !          4860:   av = 0;
        !          4861:   if ( !m ) {
        !          4862:     get_algtree((Obj)f,&av);
        !          4863:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
        !          4864:     nd_ntrans = nvar;
        !          4865:     nd_nalg = nalg;
        !          4866:     /* #i -> t#i */
        !          4867:     if ( nalg ) {
        !          4868:       preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
        !          4869:       ord = ord1;
        !          4870:       f = f1;
1.1       noro     4871:     }
1.65    ! noro     4872:     nvar += nalg;
        !          4873:   }
        !          4874:   nd_init_ord(ord);
        !          4875:   mrank = 0;
        !          4876:   for ( t = BDY(f), max = 1; t; t = NEXT(t) )
        !          4877:     for ( tv = vv; tv; tv = NEXT(tv) ) {
1.1       noro     4878:       if ( nd_module ) {
1.16      noro     4879:         if ( OID(BDY(t)) == O_DPM ) {
1.65    ! noro     4880:           e = dpm_getdeg((DPM)BDY(t),&trank);
        !          4881:           max = MAX(e,max);
        !          4882:           mrank = MAX(mrank,trank);
1.16      noro     4883:         } else {
1.65    ! noro     4884:           s = BDY((LIST)BDY(t));
        !          4885:           trank = length(s);
        !          4886:           mrank = MAX(mrank,trank);
        !          4887:           for ( ; s; s = NEXT(s) ) {
        !          4888:             e = getdeg(tv->v,(P)BDY(s));
        !          4889:             max = MAX(e,max);
        !          4890:           }
1.16      noro     4891:         }
1.1       noro     4892:       } else {
1.43      noro     4893:         if ( OID(BDY(t)) == O_DP ) {
1.65    ! noro     4894:           e = dp_getdeg((DP)BDY(t));
        !          4895:           max = MAX(e,max);
1.43      noro     4896:         } else {
1.65    ! noro     4897:           e = getdeg(tv->v,(P)BDY(t));
        !          4898:           max = MAX(e,max);
1.43      noro     4899:         }
1.1       noro     4900:       }
                   4901:     }
1.65    ! noro     4902:   nd_setup_parameters(nvar,nd_nzlist?0:max);
        !          4903:   obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
        !          4904:   ishomo = 1;
        !          4905:   for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
        !          4906:     if ( nd_module ) {
        !          4907:       if ( OID(BDY(t)) == O_DPM ) {
        !          4908:         Z cont;
        !          4909:         DPM zdpm;
        !          4910:
        !          4911:         if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
        !          4912:         else zdpm = (DPM)BDY(t);
        !          4913:         b = (pointer)dpmtondv(m,zdpm);
        !          4914:       } else {
        !          4915:         if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
        !          4916:         else zpl = (LIST)BDY(t);
        !          4917:         b = (pointer)pltondv(CO,vv,zpl);
        !          4918:       }
        !          4919:     } else {
        !          4920:       if ( OID(BDY(t)) == O_DP ) {
        !          4921:         DP zdp;
        !          4922:
        !          4923:         if ( !m && !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
        !          4924:         else zdp = (DP)BDY(t);
        !          4925:         b = (pointer)dptondv(m,zdp);
        !          4926:       } else {
        !          4927:         if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
        !          4928:         else zp = (P)BDY(t);
        !          4929:         b = (pointer)ptondv(CO,vv,zp);
        !          4930:       }
        !          4931:     }
        !          4932:     if ( ishomo )
        !          4933:       ishomo = ishomo && ndv_ishomo(b);
        !          4934:     if ( m ) ndv_mod(m,b);
        !          4935:     if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
        !          4936:   }
        !          4937:   if ( fd0 ) NEXT(fd) = 0;
1.1       noro     4938:
                   4939:   if ( !ishomo && homo ) {
                   4940:         for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   4941:             b = (NDV)BDY(t); len = LEN(b);
                   4942:             for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
1.65    ! noro     4943:                 wmax = MAX((int)TD(DL(a)),wmax);
1.1       noro     4944:         }
                   4945:         homogenize_order(ord,nvar,&ord1);
                   4946:         nd_init_ord(ord1);
                   4947:         nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   4948:         for ( t = fd0; t; t = NEXT(t) )
                   4949:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
1.65    ! noro     4950:   }
1.1       noro     4951:
1.65    ! noro     4952:   ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0,0);
        !          4953:   if ( nd_gentrace ) {
        !          4954:       MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
        !          4955:   }
1.1       noro     4956:   if ( nd_splist ) {
                   4957:     *rp = compute_splist();
                   4958:     return;
                   4959:   }
                   4960:   if ( nd_check_splist ) {
1.65    ! noro     4961:     if ( f4 ) {
        !          4962:       if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
        !          4963:       else *rp = 0;
        !          4964:     } else {
        !          4965:       if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
        !          4966:       else *rp = 0;
        !          4967:     }
1.1       noro     4968:     return;
                   4969:   }
1.65    ! noro     4970:   x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
1.1       noro     4971:   if ( !x ) {
                   4972:     *rp = 0; return;
                   4973:   }
1.15      noro     4974:   if ( nd_gentrace ) {
                   4975:     MKVECT(hvect,nd_psn);
                   4976:     for ( i = 0; i < nd_psn; i++ )
1.65    ! noro     4977:       ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
1.15      noro     4978:   }
1.1       noro     4979:   if ( !ishomo && homo ) {
                   4980:        /* dehomogenization */
                   4981:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   4982:     nd_init_ord(ord);
                   4983:     nd_setup_parameters(nvar,0);
                   4984:   }
1.65    ! noro     4985:   nd_demand = 0;
1.1       noro     4986:   if ( nd_module && nd_intersect ) {
                   4987:     for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.65    ! noro     4988:       if ( MPOS(DL(nd_psh[j])) > (UINT)nd_intersect ) {
1.1       noro     4989:         MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
                   4990:       }
                   4991:     conv_ilist(nd_demand,0,x,0);
                   4992:     goto FINAL;
                   4993:   }
1.65    ! noro     4994:   if ( nd_gentrace  && f4 ) { nzlist = nd_alltracelist; }
        !          4995:   x = ndv_reducebase(x,perm);
        !          4996:   if ( nd_gentrace  && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
        !          4997:   x = ndv_reduceall(m,x);
        !          4998:   cbpe = nd_bpe;
        !          4999:   if ( nd_gentrace && !f4 ) {
        !          5000:     tl2 = nd_alltracelist; nd_alltracelist = 0;
        !          5001:     ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
        !          5002:     tl3 = nd_alltracelist; nd_alltracelist = 0;
        !          5003:     if ( nd_gensyz ) {
        !          5004:       nd_gb(m,0,1,1,0);
        !          5005:       tl4 = nd_alltracelist; nd_alltracelist = 0;
        !          5006:     } else tl4 = 0;
        !          5007:   }
        !          5008:   nd_bpe = cbpe;
        !          5009:   nd_setup_parameters(nd_nvar,0);
1.1       noro     5010: FINAL:
1.65    ! noro     5011:   for ( r0 = 0, t = x; t; t = NEXT(t) ) {
        !          5012:     NEXTNODE(r0,r);
        !          5013:     if ( nd_module ) {
        !          5014:       if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
        !          5015:       else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
        !          5016:     } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
        !          5017:     else BDY(r) = ndvtop(m,CO,vv,BDY(t));
        !          5018:   }
        !          5019:   if ( r0 ) NEXT(r) = 0;
        !          5020:   if ( !m && nd_nalg )
1.1       noro     5021:         r0 = postprocess_algcoef(av,alist,r0);
1.65    ! noro     5022:   MKLIST(*rp,r0);
        !          5023:   if ( nd_gentrace ) {
1.1       noro     5024:   if ( f4 ) {
1.6       noro     5025:             STOZ(16,bpe);
                   5026:             STOZ(nd_last_nonzero,last_nonzero);
1.15      noro     5027:             tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1       noro     5028:         } else {
                   5029:             tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   5030:             tl3 = reverse_node(tl3);
                   5031:             /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   5032:             for ( t = tl2; t; t = NEXT(t) ) {
                   5033:             /* s = [i,[*,j,*,*],...] */
                   5034:                 s = BDY((LIST)BDY(t));
1.6       noro     5035:                 j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     5036:                 for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     5037:                     j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     5038:                     ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   5039:                 }
                   5040:             }
                   5041:             for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6       noro     5042:                 STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     5043:             }
                   5044:             MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   5045:             MKLIST(l5,tl4);
1.6       noro     5046:             STOZ(nd_bpe,bpe);
1.15      noro     5047:             tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     5048:         }
                   5049:     }
                   5050: #if 0
                   5051:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   5052: #endif
                   5053: }
                   5054:
1.28      noro     5055: NODE nd_sba_f4(int m,int **indp);
                   5056:
                   5057: void nd_sba(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.24      noro     5058: {
1.65    ! noro     5059:   VL tv,fv,vv;
        !          5060:   NODE fd,fd0,r,r0,t,x,nd,nd1,syz;
1.24      noro     5061:   int e,max,nvar,i;
                   5062:   NDV b;
1.65    ! noro     5063:   int ishomo,wmax,len;
1.24      noro     5064:   NMV a;
1.65    ! noro     5065:   P zp;
1.24      noro     5066:   Q dmy;
                   5067:   struct order_spec *ord1;
                   5068:   int *perm;
                   5069:   EPOS oepos;
1.65    ! noro     5070:   int obpe,oadv,ompos;
1.51      noro     5071:   struct oEGT eg0,eg1,egconv,egintred;
1.55      noro     5072:   LIST l1,redind;
                   5073:   Z z;
1.24      noro     5074:
                   5075:   nd_module = 0;
                   5076:   nd_demand = 0;
1.35      noro     5077:   Nsamesig = 0;
1.24      noro     5078:   if ( DP_Multiple )
                   5079:     nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   5080:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     5081:   parse_nd_option(vv,current_option);
1.24      noro     5082:   if ( m && nd_vc )
                   5083:     error("nd_sba : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
                   5084:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5085:   switch ( ord->id ) {
                   5086:     case 1:
                   5087:       if ( ord->nv != nvar )
                   5088:         error("nd_sba : invalid order specification");
1.65    ! noro     5089:       break;
        !          5090:     default:
        !          5091:       break;
1.24      noro     5092:   }
                   5093:   nd_nalg = 0;
                   5094:   nd_init_ord(ord);
                   5095:   // for SIG comparison
                   5096:   initd(ord);
                   5097:   for ( t = BDY(f), max = 1; t; t = NEXT(t) ) {
                   5098:     for ( tv = vv; tv; tv = NEXT(tv) ) {
1.43      noro     5099:       if ( OID(BDY(t)) == O_DP ) {
                   5100:         e = dp_getdeg((DP)BDY(t));
                   5101:         max = MAX(e,max);
                   5102:       } else {
                   5103:         e = getdeg(tv->v,(P)BDY(t));
                   5104:         max = MAX(e,max);
                   5105:       }
1.24      noro     5106:     }
                   5107:   }
1.25      noro     5108:   nd_setup_parameters(nvar,max);
1.24      noro     5109:   obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   5110:   ishomo = 1;
                   5111:   for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.43      noro     5112:     if ( OID(BDY(t)) == O_DP ) {
                   5113:       DP zdp;
                   5114:
                   5115:       if ( !m ) dp_ptozp((DP)BDY(t),&zdp);
                   5116:       else zdp = (DP)BDY(t);
                   5117:       b = (pointer)dptondv(m,zdp);
                   5118:     } else {
                   5119:       if ( !m ) ptozp((P)BDY(t),1,&dmy,&zp);
                   5120:       else zp = (P)BDY(t);
                   5121:       b = (pointer)ptondv(CO,vv,zp);
                   5122:     }
1.24      noro     5123:     if ( ishomo )
                   5124:       ishomo = ishomo && ndv_ishomo(b);
                   5125:     if ( m ) ndv_mod(m,b);
                   5126:     if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   5127:   }
                   5128:   if ( fd0 ) NEXT(fd) = 0;
                   5129:
                   5130:   if ( !ishomo && homo ) {
                   5131:     for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   5132:       b = (NDV)BDY(t); len = LEN(b);
                   5133:       for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
1.65    ! noro     5134:         wmax = MAX((int)TD(DL(a)),wmax);
1.24      noro     5135:       }
                   5136:       homogenize_order(ord,nvar,&ord1);
                   5137:       nd_init_ord(ord1);
1.42      noro     5138:       // for SIG comparison
                   5139:       initd(ord1);
1.24      noro     5140:       nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   5141:       for ( t = fd0; t; t = NEXT(t) )
                   5142:         ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   5143:   }
                   5144:
1.39      noro     5145:   ndv_setup(m,0,fd0,nd_sba_dontsort,0,1);
1.55      noro     5146:   if ( nd_gentrace ) {
                   5147:     MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   5148:   }
1.44      noro     5149:   x = f4 ? nd_sba_f4(m,&perm) : nd_sba_buch(m,ishomo || homo,&perm,&syz);
1.24      noro     5150:   if ( !x ) {
                   5151:     *rp = 0; return;
                   5152:   }
                   5153:   if ( !ishomo && homo ) {
                   5154:        /* dehomogenization */
                   5155:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   5156:     nd_init_ord(ord);
1.42      noro     5157:     // for SIG comparison
                   5158:     initd(ord);
1.24      noro     5159:     nd_setup_parameters(nvar,0);
                   5160:   }
                   5161:   nd_demand = 0;
1.51      noro     5162:   get_eg(&eg0);
1.24      noro     5163:   x = ndv_reducebase(x,perm);
1.55      noro     5164:   for ( nd = 0, i = length(x)-1; i >= 0; i-- ) {
                   5165:     STOZ(perm[i],z); MKNODE(nd1,z,nd); nd = nd1;
                   5166:   }
                   5167:   MKLIST(redind,nd);
1.24      noro     5168:   x = ndv_reduceall(m,x);
1.51      noro     5169:   get_eg(&eg1); init_eg(&egintred); add_eg(&egintred,&eg0,&eg1);
1.24      noro     5170:   nd_setup_parameters(nd_nvar,0);
1.29      noro     5171:   get_eg(&eg0);
1.24      noro     5172:   for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   5173:     NEXTNODE(r0,r);
                   5174:     if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
1.34      noro     5175:     else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.24      noro     5176:   }
                   5177:   if ( r0 ) NEXT(r) = 0;
1.44      noro     5178:   if ( nd_sba_syz ) {
                   5179:     LIST gb,hsyz;
                   5180:
                   5181:     MKLIST(gb,r0);
                   5182:     MKLIST(hsyz,syz);
                   5183:     nd = mknode(2,gb,hsyz);
                   5184:     MKLIST(*rp,nd);
1.55      noro     5185:   } else if ( nd_gentrace ) {
                   5186:     LIST gb,trace;
                   5187:
                   5188:     MKLIST(trace,nd_alltracelist);
                   5189:     MKLIST(gb,r0);
                   5190:     nd = mknode(3,gb,redind,trace);
                   5191:     MKLIST(*rp,nd);
1.44      noro     5192:   } else
                   5193:     MKLIST(*rp,r0);
1.29      noro     5194:   get_eg(&eg1); init_eg(&egconv); add_eg(&egconv,&eg0,&eg1);
1.51      noro     5195:   print_eg("intred",&egintred); fprintf(asir_out,"\n");
1.29      noro     5196:   print_eg("conv",&egconv); fprintf(asir_out,"\n");
1.24      noro     5197: }
                   5198:
1.1       noro     5199: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
                   5200: {
1.65    ! noro     5201:     VL tv,fv,vv,av;
        !          5202:     NODE fd,fd0,r,r0,t,x,alist;
        !          5203:     int e,max,nvar,i,nalg;
1.1       noro     5204:     NDV b;
1.65    ! noro     5205:     int ishomo;
        !          5206:     P zp;
1.1       noro     5207:     Q dmy;
1.65    ! noro     5208:     LIST f1;
1.1       noro     5209:     struct order_spec *ord1;
                   5210:     int *perm;
                   5211:
                   5212:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     5213:     parse_nd_option(vv,current_option);
1.1       noro     5214:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5215:     switch ( ord->id ) {
                   5216:         case 1:
                   5217:             if ( ord->nv != nvar )
                   5218:                 error("nd_check : invalid order specification");
                   5219:             break;
                   5220:         default:
                   5221:             break;
                   5222:     }
                   5223:     nd_nalg = 0;
                   5224:     av = 0;
                   5225:     if ( !m ) {
                   5226:         get_algtree((Obj)f,&av);
                   5227:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   5228:         nd_ntrans = nvar;
                   5229:         nd_nalg = nalg;
                   5230:         /* #i -> t#i */
                   5231:         if ( nalg ) {
                   5232:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   5233:             ord = ord1;
                   5234:             f = f1;
                   5235:         }
                   5236:         nvar += nalg;
                   5237:     }
                   5238:     nd_init_ord(ord);
                   5239:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   5240:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   5241:             e = getdeg(tv->v,(P)BDY(t));
                   5242:             max = MAX(e,max);
                   5243:         }
                   5244:     nd_setup_parameters(nvar,max);
                   5245:     ishomo = 1;
                   5246:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   5247:         ptozp((P)BDY(t),1,&dmy,&zp);
                   5248:         b = (pointer)ptondv(CO,vv,zp);
                   5249:         if ( ishomo )
                   5250:             ishomo = ishomo && ndv_ishomo(b);
                   5251:         if ( m ) ndv_mod(m,b);
                   5252:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   5253:     }
                   5254:     if ( fd0 ) NEXT(fd) = 0;
1.24      noro     5255:     ndv_setup(m,0,fd0,0,1,0);
1.1       noro     5256:     for ( x = 0, i = 0; i < nd_psn; i++ )
                   5257:         x = update_base(x,i);
                   5258:     if ( do_check ) {
                   5259:         x = nd_gb(m,ishomo,1,0,&perm);
                   5260:         if ( !x ) {
                   5261:             *rp = 0;
                   5262:             return;
                   5263:         }
                   5264:     } else {
                   5265: #if 0
                   5266:     /* bug ? */
                   5267:         for ( t = x; t; t = NEXT(t) )
                   5268:             BDY(t) = (pointer)nd_ps[(long)BDY(t)];
                   5269: #else
                   5270:     conv_ilist(0,0,x,&perm);
                   5271: #endif
                   5272:     }
                   5273:     x = ndv_reducebase(x,perm);
                   5274:     x = ndv_reduceall(m,x);
                   5275:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   5276:         NEXTNODE(r0,r);
                   5277:         BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   5278:     }
                   5279:     if ( r0 ) NEXT(r) = 0;
                   5280:     if ( !m && nd_nalg )
                   5281:         r0 = postprocess_algcoef(av,alist,r0);
                   5282:     MKLIST(*rp,r0);
                   5283: }
                   5284:
                   5285: NDV recompute_trace(NODE trace,NDV *p,int m);
                   5286: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
                   5287:
                   5288: NDV recompute_trace(NODE ti,NDV *p,int mod)
                   5289: {
                   5290:   int c,c1,c2,i;
                   5291:   NM mul,m,tail;
                   5292:   ND d,r,rm;
                   5293:   NODE sj;
                   5294:   NDV red;
                   5295:   Obj mj;
                   5296:
                   5297:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   5298:   CM(mul) = 1;
                   5299:   tail = 0;
                   5300:   for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
                   5301:     sj = BDY((LIST)BDY(ti));
                   5302:     if ( ARG0(sj) ) {
1.6       noro     5303:       red = p[ZTOS((Q)ARG1(sj))];
1.1       noro     5304:       mj = (Obj)ARG2(sj);
                   5305:       if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
                   5306:       else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
                   5307:       rm = ndv_mul_nm(mod,mul,red);
                   5308:       if ( !r ) r = rm;
                   5309:       else {
                   5310:         for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
                   5311:           if ( d ) {
                   5312:             NEXT(tail) = m; tail = m; LEN(d)++;
                   5313:           } else {
                   5314:             MKND(nd_nvar,m,1,d); tail = BDY(d);
                   5315:           }
                   5316:         }
                   5317:         if ( !m ) return 0; /* failure */
                   5318:                 else {
                   5319:           BDY(r) = m;
                   5320:                     if ( mod > 0 || mod == -1 ) {
                   5321:             c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
                   5322:             DMAR(c1,c2,0,mod,c);
                   5323:             nd_mul_c(mod,rm,c);
                   5324:                     } else {
                   5325:                       Z t,u;
                   5326:
                   5327:                       chsgnlf(HCZ(r),&t);
                   5328:                       divlf(t,HCZ(rm),&u);
                   5329:                       nd_mul_c_lf(rm,u);
                   5330:                     }
                   5331:           r = nd_add(mod,r,rm);
                   5332:         }
                   5333:           }
                   5334:      }
                   5335:   }
                   5336:   if ( tail ) NEXT(tail) = 0;
                   5337:   d = nd_add(mod,d,r);
                   5338:   nd_mul_c(mod,d,invm(HCM(d),mod));
                   5339:   return ndtondv(mod,d);
                   5340: }
                   5341:
                   5342: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
                   5343: {
1.65    ! noro     5344:   VL tv,fv,vv;
        !          5345:   NODE r,r0,t;
        !          5346:   int nvar,i;
        !          5347:   NDV b;
        !          5348:   P zp;
        !          5349:   Q dmy;
1.1       noro     5350:   NODE permtrace,intred,ind,perm,trace,ti;
                   5351:   int len,n,j;
                   5352:   NDV *db,*pb;
                   5353:
1.65    ! noro     5354:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
        !          5355:   parse_nd_option(vv,current_option);
        !          5356:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
        !          5357:   switch ( ord->id ) {
        !          5358:     case 1:
        !          5359:       if ( ord->nv != nvar )
        !          5360:         error("nd_check : invalid order specification");
        !          5361:       break;
        !          5362:     default:
        !          5363:       break;
        !          5364:   }
        !          5365:   nd_init_ord(ord);
        !          5366:   nd_bpe = (int)ZTOS((Q)ARG7(BDY(tlist)));
        !          5367:   nd_setup_parameters(nvar,0);
1.1       noro     5368:
                   5369:   len = length(BDY(f));
                   5370:   db = (NDV *)MALLOC(len*sizeof(NDV *));
                   5371:   for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
1.65    ! noro     5372:     ptozp((P)BDY(t),1,&dmy,&zp);
        !          5373:     b = ptondv(CO,vv,zp);
        !          5374:     ndv_mod(m,b);
1.1       noro     5375:     ndv_mul_c(m,b,invm(HCM(b),m));
                   5376:     db[i] = b;
1.65    ! noro     5377:   }
1.1       noro     5378:
                   5379:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   5380:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   5381:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   5382:   perm = BDY((LIST)ARG0(permtrace));
                   5383:   trace = NEXT(permtrace);
                   5384:
                   5385:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.65    ! noro     5386:     j = (int)ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1       noro     5387:     if ( j > i ) i = j;
                   5388:   }
                   5389:   n = i+1;
                   5390:   pb = (NDV *)MALLOC(n*sizeof(NDV *));
                   5391:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   5392:     ti = BDY((LIST)BDY(t));
1.6       noro     5393:     pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1       noro     5394:   }
                   5395:   for ( t = trace; t; t = NEXT(t) ) {
                   5396:     ti = BDY((LIST)BDY(t));
1.6       noro     5397:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   5398:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     5399:       if ( DP_Print ) {
                   5400:            fprintf(asir_out,"."); fflush(asir_out);
                   5401:       }
                   5402:   }
                   5403:   for ( t = intred; t; t = NEXT(t) ) {
                   5404:     ti = BDY((LIST)BDY(t));
1.6       noro     5405:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   5406:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     5407:       if ( DP_Print ) {
1.65    ! noro     5408:         fprintf(asir_out,"*"); fflush(asir_out);
        !          5409:     }
1.1       noro     5410:   }
1.65    ! noro     5411:   for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
        !          5412:     NEXTNODE(r0,r);
1.6       noro     5413:     b = pb[ZTOS((Q)BDY(t))];
1.65    ! noro     5414:     ndv_mul_c(m,b,invm(HCM(b),m));
1.1       noro     5415: #if 0
1.65    ! noro     5416:     BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1       noro     5417: #else
1.65    ! noro     5418:     BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1       noro     5419: #endif
1.65    ! noro     5420:   }
        !          5421:   if ( r0 ) NEXT(r) = 0;
        !          5422:   MKLIST(*rp,r0);
        !          5423:   if ( DP_Print ) fprintf(asir_out,"\n");
1.1       noro     5424: }
                   5425:
1.16      noro     5426: 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     5427: {
1.65    ! noro     5428:     VL tv,fv,vv,av;
        !          5429:     NODE fd,fd0,in0,in,r,t,s,cand,alist;
1.1       noro     5430:     int m,nocheck,nvar,mindex,e,max;
                   5431:     NDV c;
                   5432:     NMV a;
1.65    ! noro     5433:     P zp;
1.1       noro     5434:     Q dmy;
                   5435:     EPOS oepos;
                   5436:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
1.65    ! noro     5437:     LIST f1,zpl;
1.1       noro     5438:     struct order_spec *ord1;
1.64      noro     5439:     struct oEGT eg_check,eg_gb,eg_intred,eg0,eg1;
1.1       noro     5440:     NODE tr,tl1,tl2,tl3,tl4;
                   5441:     LIST l1,l2,l3,l4,l5;
                   5442:     int *perm;
                   5443:     int j,ret;
                   5444:     Z jq,bpe;
1.15      noro     5445:     VECT hvect;
1.1       noro     5446:
1.41      noro     5447:     NcriB = NcriMF = Ncri2 = 0;
1.1       noro     5448:     nd_module = 0;
                   5449:     nd_lf = 0;
1.46      noro     5450:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   5451:     parse_nd_option(vv,current_option);
1.1       noro     5452:     if ( nd_lf ) {
                   5453:       if ( f4 )
                   5454:         nd_f4_lf_trace(f,v,trace,homo,ord,rp);
                   5455:       else
                   5456:         error("nd_gr_trace is not implemented yet over a large finite field");
                   5457:       return;
                   5458:     }
                   5459:     if ( DP_Multiple )
                   5460:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   5461:
                   5462:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5463:     switch ( ord->id ) {
                   5464:         case 1:
                   5465:             if ( ord->nv != nvar )
                   5466:                 error("nd_gr_trace : invalid order specification");
                   5467:             break;
                   5468:         default:
                   5469:             break;
                   5470:     }
                   5471:
                   5472:     get_algtree((Obj)f,&av);
                   5473:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   5474:     nd_ntrans = nvar;
                   5475:     nd_nalg = nalg;
                   5476:     /* #i -> t#i */
                   5477:     if ( nalg ) {
                   5478:         preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   5479:         ord = ord1;
                   5480:         f = f1;
                   5481:     }
                   5482:     nvar += nalg;
                   5483:
                   5484:     nocheck = 0;
                   5485:     mindex = 0;
                   5486:
                   5487:     if ( Demand ) nd_demand = 1;
                   5488:     else nd_demand = 0;
                   5489:
                   5490:     /* setup modulus */
                   5491:     if ( trace < 0 ) {
                   5492:         trace = -trace;
                   5493:         nocheck = 1;
                   5494:     }
                   5495:     m = trace > 1 ? trace : get_lprime(mindex);
                   5496:     nd_init_ord(ord);
                   5497:     mrank = 0;
                   5498:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   5499:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   5500:             if ( nd_module ) {
1.16      noro     5501:               if ( OID(BDY(t)) == O_DPM ) {
                   5502:                 e = dpm_getdeg((DPM)BDY(t),&trank);
                   5503:                 max = MAX(e,max);
                   5504:                 mrank = MAX(mrank,trank);
                   5505:               } else {
1.1       noro     5506:                 s = BDY((LIST)BDY(t));
                   5507:                 trank = length(s);
                   5508:                 mrank = MAX(mrank,trank);
                   5509:                 for ( ; s; s = NEXT(s) ) {
                   5510:                     e = getdeg(tv->v,(P)BDY(s));
                   5511:                     max = MAX(e,max);
                   5512:                 }
1.16      noro     5513:               }
1.1       noro     5514:             } else {
1.43      noro     5515:               if ( OID(BDY(t)) == O_DP ) {
                   5516:                 e = dp_getdeg((DP)BDY(t));
                   5517:                 max = MAX(e,max);
                   5518:               } else {
1.1       noro     5519:                 e = getdeg(tv->v,(P)BDY(t));
                   5520:                 max = MAX(e,max);
1.43      noro     5521:               }
1.1       noro     5522:             }
                   5523:         }
                   5524:     nd_setup_parameters(nvar,max);
                   5525:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   5526:     ishomo = 1;
                   5527:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   5528:         if ( nd_module ) {
1.16      noro     5529:           if ( OID(BDY(t)) == O_DPM ) {
                   5530:             Z cont;
                   5531:             DPM zdpm;
                   5532:
1.17      noro     5533:             if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16      noro     5534:             else zdpm = (DPM)BDY(t);
                   5535:             c = (pointer)dpmtondv(m,zdpm);
                   5536:           } else {
                   5537:             if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   5538:             else zpl = (LIST)BDY(t);
1.1       noro     5539:             c = (pointer)pltondv(CO,vv,zpl);
1.16      noro     5540:           }
1.1       noro     5541:         } else {
1.43      noro     5542:           if ( OID(BDY(t)) == O_DP ) {
                   5543:             DP zdp;
                   5544:
                   5545:             if ( !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
                   5546:             else zdp = (DP)BDY(t);
                   5547:             c = (pointer)dptondv(m,zdp);
                   5548:           } else {
                   5549:             if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   5550:             else zp = (P)BDY(t);
                   5551:             c = (pointer)ptondv(CO,vv,zp);
                   5552:           }
1.1       noro     5553:         }
                   5554:         if ( ishomo )
                   5555:             ishomo = ishomo && ndv_ishomo(c);
                   5556:         if ( c ) {
                   5557:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   5558:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   5559:         }
                   5560:     }
                   5561:     if ( in0 ) NEXT(in) = 0;
                   5562:     if ( fd0 ) NEXT(fd) = 0;
                   5563:     if ( !ishomo && homo ) {
                   5564:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   5565:             c = (NDV)BDY(t); len = LEN(c);
                   5566:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
1.65    ! noro     5567:                 wmax = MAX((int)TD(DL(a)),wmax);
1.1       noro     5568:         }
                   5569:         homogenize_order(ord,nvar,&ord1);
                   5570:         nd_init_ord(ord1);
                   5571:         nd_setup_parameters(nvar+1,wmax);
                   5572:         for ( t = fd0; t; t = NEXT(t) )
                   5573:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   5574:     }
                   5575:     if ( MaxDeg > 0 ) nocheck = 1;
                   5576:     while ( 1 ) {
1.64      noro     5577:         get_eg(&eg0);
                   5578:         tl1 = tl2 = tl3 = tl4 = 0;
1.1       noro     5579:         if ( Demand )
                   5580:             nd_demand = 1;
1.24      noro     5581:         ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1       noro     5582:         if ( nd_gentrace ) {
                   5583:             MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   5584:         }
                   5585:         if ( ret )
                   5586:             cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
                   5587:         if ( !ret || !cand ) {
                   5588:             /* failure */
                   5589:             if ( trace > 1 ) { *rp = 0; return; }
                   5590:             else m = get_lprime(++mindex);
                   5591:             continue;
                   5592:         }
1.15      noro     5593:         if ( nd_gentrace ) {
                   5594:           MKVECT(hvect,nd_psn);
                   5595:           for ( i = 0; i < nd_psn; i++ )
                   5596:              ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   5597:         }
1.1       noro     5598:         if ( !ishomo && homo ) {
                   5599:             /* dehomogenization */
                   5600:             for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   5601:             nd_init_ord(ord);
                   5602:             nd_setup_parameters(nvar,0);
                   5603:         }
1.64      noro     5604:         get_eg(&eg1); init_eg(&eg_gb); add_eg(&eg_gb,&eg0,&eg1);
1.1       noro     5605:         nd_demand = 0;
                   5606:         cand = ndv_reducebase(cand,perm);
                   5607:         if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
1.64      noro     5608:         get_eg(&eg0);
1.1       noro     5609:         cand = ndv_reduceall(0,cand);
1.64      noro     5610:         get_eg(&eg1); init_eg(&eg_intred); add_eg(&eg_intred,&eg0,&eg1);
1.1       noro     5611:         cbpe = nd_bpe;
                   5612:         if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
                   5613:         get_eg(&eg0);
                   5614:         if ( nocheck )
                   5615:             break;
                   5616:         if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   5617:             if ( nd_gentrace ) {
                   5618:           tl3 = nd_alltracelist; nd_alltracelist = 0;
                   5619:         } else tl3 = 0;
                   5620:             /* gbcheck : cand is a GB of Id(cand) ? */
1.54      noro     5621:             if ( nd_vc || nd_gentrace || nd_gensyz || do_weyl )
1.1       noro     5622:               ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
                   5623:             else
                   5624:               ret = nd_f4(0,1,0)!=0;
                   5625:             if ( nd_gentrace && nd_gensyz ) {
                   5626:           tl4 = nd_alltracelist; nd_alltracelist = 0;
                   5627:         } else tl4 = 0;
                   5628:     }
                   5629:     if ( ret ) break;
                   5630:         else if ( trace > 1 ) {
                   5631:             /* failure */
                   5632:             *rp = 0; return;
                   5633:         } else {
                   5634:             /* try the next modulus */
                   5635:             m = get_lprime(++mindex);
                   5636:             /* reset the parameters */
                   5637:             if ( !ishomo && homo ) {
                   5638:                 nd_init_ord(ord1);
                   5639:                 nd_setup_parameters(nvar+1,wmax);
                   5640:             } else {
                   5641:                 nd_init_ord(ord);
                   5642:                 nd_setup_parameters(nvar,max);
                   5643:             }
                   5644:         }
                   5645:     }
                   5646:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   5647:     if ( DP_Print )
1.64      noro     5648:         fprintf(asir_out,"gb=%.3fsec,check=%.3fsec,intred=%.3fsec\n",
                   5649:           eg_gb.exectime,eg_check.exectime,eg_intred.exectime);
1.1       noro     5650:     /* dp->p */
                   5651:     nd_bpe = cbpe;
                   5652:     nd_setup_parameters(nd_nvar,0);
                   5653:     for ( r = cand; r; r = NEXT(r) ) {
1.16      noro     5654:       if ( nd_module ) {
1.17      noro     5655:         if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16      noro     5656:         else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17      noro     5657:       } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
                   5658:       else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1       noro     5659:     }
                   5660:     if ( nd_nalg )
                   5661:         cand = postprocess_algcoef(av,alist,cand);
                   5662:     MKLIST(*rp,cand);
                   5663:     if ( nd_gentrace ) {
                   5664:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   5665:     tl3 = reverse_node(tl3);
                   5666:     /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   5667:         for ( t = tl2; t; t = NEXT(t) ) {
                   5668:       /* s = [i,[*,j,*,*],...] */
                   5669:             s = BDY((LIST)BDY(t));
1.6       noro     5670:             j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     5671:       for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     5672:                 j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     5673:         ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   5674:             }
                   5675:     }
                   5676:     for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6       noro     5677:         STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     5678:     }
                   5679:         MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   5680:     MKLIST(l5,tl4);
1.6       noro     5681:       STOZ(nd_bpe,bpe);
1.15      noro     5682:         tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     5683:     }
                   5684: }
                   5685:
                   5686: /* XXX : module element is not considered  */
                   5687:
                   5688: void dltondl(int n,DL dl,UINT *r)
                   5689: {
                   5690:     UINT *d;
                   5691:     int i,j,l,s,ord_l;
                   5692:     struct order_pair *op;
                   5693:
                   5694:     d = (unsigned int *)dl->d;
                   5695:     for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
                   5696:     if ( nd_blockmask ) {
                   5697:         l = nd_blockmask->n;
                   5698:         op = nd_blockmask->order_pair;
                   5699:         for ( j = 0, s = 0; j < l; j++ ) {
                   5700:             ord_l = op[j].length;
                   5701:             for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
                   5702:         }
                   5703:         TD(r) = ndl_weight(r);
                   5704:         ndl_weight_mask(r);
                   5705:     } else {
                   5706:         for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
                   5707:         TD(r) = ndl_weight(r);
                   5708:     }
                   5709: }
                   5710:
                   5711: DL ndltodl(int n,UINT *ndl)
                   5712: {
                   5713:     DL dl;
                   5714:     int *d;
                   5715:     int i,j,l,s,ord_l;
                   5716:     struct order_pair *op;
                   5717:
1.33      noro     5718:     NEWDL_NOINIT(dl,n);
1.1       noro     5719:     dl->td = TD(ndl);
                   5720:     d = dl->d;
                   5721:     if ( nd_blockmask ) {
                   5722:         l = nd_blockmask->n;
                   5723:         op = nd_blockmask->order_pair;
                   5724:         for ( j = 0, s = 0; j < l; j++ ) {
                   5725:             ord_l = op[j].length;
                   5726:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   5727:         }
                   5728:     } else {
                   5729:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   5730:     }
                   5731:     return dl;
                   5732: }
                   5733:
1.24      noro     5734: void _ndltodl(UINT *ndl,DL dl)
                   5735: {
                   5736:     int *d;
                   5737:     int i,j,l,s,ord_l,n;
                   5738:     struct order_pair *op;
                   5739:
                   5740:     n = nd_nvar;
                   5741:     dl->td = TD(ndl);
                   5742:     d = dl->d;
                   5743:     if ( nd_blockmask ) {
                   5744:         l = nd_blockmask->n;
                   5745:         op = nd_blockmask->order_pair;
                   5746:         for ( j = 0, s = 0; j < l; j++ ) {
                   5747:             ord_l = op[j].length;
                   5748:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   5749:         }
                   5750:     } else {
                   5751:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   5752:     }
                   5753: }
                   5754:
1.1       noro     5755: void nmtodp(int mod,NM m,DP *r)
                   5756: {
                   5757:     DP dp;
                   5758:     MP mr;
                   5759:
                   5760:     NEWMP(mr);
                   5761:     mr->dl = ndltodl(nd_nvar,DL(m));
                   5762:     mr->c = (Obj)ndctop(mod,m->c);
                   5763:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   5764:     *r = dp;
                   5765: }
                   5766:
1.15      noro     5767: void ndltodp(UINT *d,DP *r)
                   5768: {
                   5769:     DP dp;
                   5770:     MP mr;
                   5771:
                   5772:     NEWMP(mr);
                   5773:     mr->dl = ndltodl(nd_nvar,d);
                   5774:     mr->c = (Obj)ONE;
                   5775:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   5776:     *r = dp;
                   5777: }
                   5778:
1.1       noro     5779: void ndl_print(UINT *dl)
                   5780: {
                   5781:     int n;
                   5782:     int i,j,l,ord_l,s,s0;
                   5783:     struct order_pair *op;
                   5784:
                   5785:     n = nd_nvar;
                   5786:     printf("<<");
                   5787:     if ( nd_blockmask ) {
                   5788:         l = nd_blockmask->n;
                   5789:         op = nd_blockmask->order_pair;
                   5790:         for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   5791:             ord_l = op[j].length;
                   5792:             for ( i = 0; i < ord_l; i++, s++ )
                   5793:                 printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
                   5794:         }
                   5795:     } else {
                   5796:         for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
                   5797:     }
                   5798:     printf(">>");
                   5799:     if ( nd_module && MPOS(dl) )
                   5800:         printf("*e%d",MPOS(dl));
                   5801: }
                   5802:
                   5803: void nd_print(ND p)
                   5804: {
                   5805:     NM m;
                   5806:
                   5807:     if ( !p )
                   5808:         printf("0\n");
                   5809:     else {
                   5810:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5811:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     5812:             else printf("+%ld*",CM(m));
1.1       noro     5813:             ndl_print(DL(m));
                   5814:         }
                   5815:         printf("\n");
                   5816:     }
                   5817: }
                   5818:
                   5819: void nd_print_q(ND p)
                   5820: {
                   5821:     NM m;
                   5822:
                   5823:     if ( !p )
                   5824:         printf("0\n");
                   5825:     else {
                   5826:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5827:             printf("+");
1.6       noro     5828:             printexpr(CO,(Obj)CZ(m));
1.1       noro     5829:             printf("*");
                   5830:             ndl_print(DL(m));
                   5831:         }
                   5832:         printf("\n");
                   5833:     }
                   5834: }
                   5835:
                   5836: void ndp_print(ND_pairs d)
                   5837: {
                   5838:     ND_pairs t;
                   5839:
                   5840:     for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
                   5841:     printf("\n");
                   5842: }
                   5843:
                   5844: void nd_removecont(int mod,ND p)
                   5845: {
                   5846:     int i,n;
                   5847:     Z *w;
                   5848:     NM m;
                   5849:     struct oVECT v;
                   5850:
                   5851:     if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
                   5852:     else if ( mod == -2 ) {
                   5853:       Z inv;
                   5854:       divlf(ONE,HCZ(p),&inv);
                   5855:       nd_mul_c_lf(p,inv);
                   5856:     } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
                   5857:     else {
                   5858:         for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   5859:         w = (Z *)MALLOC(n*sizeof(Q));
                   5860:         v.len = n;
                   5861:         v.body = (pointer *)w;
1.6       noro     5862:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5863:         removecont_array((P *)w,n,1);
1.6       noro     5864:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5865:     }
                   5866: }
                   5867:
                   5868: void nd_removecont2(ND p1,ND p2)
                   5869: {
                   5870:     int i,n1,n2,n;
                   5871:     Z *w;
                   5872:     NM m;
                   5873:     struct oVECT v;
                   5874:
                   5875:     n1 = nd_length(p1);
                   5876:     n2 = nd_length(p2);
                   5877:     n = n1+n2;
                   5878:     w = (Z *)MALLOC(n*sizeof(Q));
                   5879:     v.len = n;
                   5880:     v.body = (pointer *)w;
                   5881:     i = 0;
                   5882:     if ( p1 )
1.6       noro     5883:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5884:     if ( p2 )
1.6       noro     5885:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5886:     removecont_array((P *)w,n,1);
                   5887:     i = 0;
                   5888:     if ( p1 )
1.6       noro     5889:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5890:     if ( p2 )
1.6       noro     5891:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5892: }
                   5893:
                   5894: void ndv_removecont(int mod,NDV p)
                   5895: {
                   5896:     int i,len,all_p;
                   5897:     Z *c;
                   5898:     P *w;
1.65    ! noro     5899:     Z dvr;
1.1       noro     5900:     P g,cont,tp;
                   5901:     NMV m;
                   5902:
                   5903:     if ( mod == -1 )
                   5904:         ndv_mul_c(mod,p,_invsf(HCM(p)));
                   5905:     else if ( mod == -2 ) {
                   5906:       Z inv;
                   5907:       divlf(ONE,HCZ(p),&inv);
                   5908:       ndv_mul_c_lf(p,inv);
                   5909:     } else if ( mod )
                   5910:         ndv_mul_c(mod,p,invm(HCM(p),mod));
                   5911:     else {
                   5912:         len = p->len;
                   5913:         w = (P *)MALLOC(len*sizeof(P));
                   5914:         c = (Z *)MALLOC(len*sizeof(Q));
                   5915:         for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
                   5916:             ptozp(CP(m),1,(Q *)&c[i],&w[i]);
                   5917:             all_p = all_p && !NUM(w[i]);
                   5918:         }
                   5919:         if ( all_p ) {
                   5920:             qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
                   5921:             mulp(nd_vc,(P)dvr,g,&cont);
                   5922:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5923:                 divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
                   5924:             }
                   5925:         } else {
                   5926:             sortbynm((Q *)c,len);
                   5927:             qltozl((Q *)c,len,&dvr);
                   5928:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5929:                 divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
                   5930:             }
                   5931:         }
                   5932:     }
                   5933: }
                   5934:
                   5935: /* koko */
                   5936:
                   5937: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
                   5938: {
                   5939:     int len,i,max;
                   5940:     NMV m,mr0,mr,t;
                   5941:
                   5942:     len = p->len;
1.14      noro     5943:     for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.65    ! noro     5944:         max = MAX(max,(int)TD(DL(m)));
1.1       noro     5945:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   5946:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   5947:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   5948:     t = (NMV)MALLOC(nmv_adv);
                   5949:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   5950:         ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6       noro     5951:         CZ(mr) = CZ(m);
1.1       noro     5952:         ndl_copy(DL(t),DL(mr));
                   5953:     }
                   5954:     NV(p)++;
                   5955:     BDY(p) = mr0;
                   5956: }
                   5957:
                   5958: void ndv_dehomogenize(NDV p,struct order_spec *ord)
                   5959: {
                   5960:     int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
                   5961:     int pos;
                   5962:     NMV m,r;
                   5963:
                   5964:     len = p->len;
                   5965:     newnvar = nd_nvar-1;
                   5966:     newexporigin = nd_get_exporigin(ord);
                   5967:     if ( nd_module ) newmpos = newexporigin-1;
                   5968:     newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
                   5969:     for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
                   5970:         ndl_dehomogenize(DL(m));
                   5971:     if ( newwpd != nd_wpd ) {
                   5972:         newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
                   5973:         for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6       noro     5974:             CZ(r) = CZ(m);
1.1       noro     5975:             if ( nd_module ) pos = MPOS(DL(m));
                   5976:             for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   5977:             adj = nd_exporigin-newexporigin;
                   5978:             for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
                   5979:             if ( nd_module ) {
                   5980:                 DL(r)[newmpos] = pos;
                   5981:             }
                   5982:         }
                   5983:     }
                   5984:     NV(p)--;
                   5985: }
                   5986:
                   5987: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
                   5988: {
                   5989:     int i;
                   5990:     P *tpl,*tpl1;
                   5991:     NODE l;
                   5992:     P h,gcd,t;
                   5993:
                   5994:     tpl = (P *)MALLOC(m*sizeof(P));
                   5995:     tpl1 = (P *)MALLOC(m*sizeof(P));
                   5996:     bcopy(pl,tpl,m*sizeof(P));
                   5997:     gcd = (P)ONE;
                   5998:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   5999:         h = (P)BDY(l);
                   6000:         while ( 1 ) {
                   6001:             for ( i = 0; i < m; i++ )
                   6002:                 if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
                   6003:                     break;
                   6004:             if ( i == m ) {
                   6005:                 bcopy(tpl1,tpl,m*sizeof(P));
                   6006:                 mulp(vl,gcd,h,&t); gcd = t;
                   6007:             } else
                   6008:                 break;
                   6009:         }
                   6010:     }
                   6011:     if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
                   6012:     if ( full ) {
                   6013:         heu_nezgcdnpz(vl,tpl,m,&t);
                   6014:         mulp(vl,gcd,t,pr);
                   6015:     } else
                   6016:         *pr = gcd;
                   6017: }
                   6018:
                   6019: void removecont_array(P *p,int n,int full)
                   6020: {
1.65    ! noro     6021:     int all_q,i;
1.1       noro     6022:     Z *c;
                   6023:     P *w;
                   6024:     P t,s;
                   6025:
                   6026:     for ( all_q = 1, i = 0; i < n; i++ )
                   6027:         all_q = all_q && NUM(p[i]);
                   6028:     if ( all_q ) {
                   6029:         removecont_array_q((Z *)p,n);
                   6030:     } else {
                   6031:         c = (Z *)MALLOC(n*sizeof(Z));
                   6032:         w = (P *)MALLOC(n*sizeof(P));
                   6033:         for ( i = 0; i < n; i++ ) {
                   6034:             ptozp(p[i],1,(Q *)&c[i],&w[i]);
                   6035:         }
                   6036:         removecont_array_q(c,n);
                   6037:         nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
                   6038:         for ( i = 0; i < n; i++ ) {
                   6039:             divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
                   6040:         }
                   6041:     }
                   6042: }
                   6043:
                   6044: /* c is an int array */
                   6045:
                   6046: void removecont_array_q(Z *c,int n)
                   6047: {
                   6048:   struct oVECT v;
                   6049:   Z d0,d1,a,u,u1,gcd;
1.65    ! noro     6050:   int i;
1.1       noro     6051:   Z *q,*r;
                   6052:
                   6053:   q = (Z *)MALLOC(n*sizeof(Z));
                   6054:   r = (Z *)MALLOC(n*sizeof(Z));
                   6055:   v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   6056:   gcdvz_estimate(&v,&d0);
                   6057:   for ( i = 0; i < n; i++ ) {
                   6058:     divqrz(c[i],d0,&q[i],&r[i]);
                   6059:   }
                   6060:   for ( i = 0; i < n; i++ ) if ( r[i] ) break;
                   6061:   if ( i < n ) {
                   6062:     v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   6063:     gcdvz(&v,&d1);
                   6064:     gcdz(d0,d1,&gcd);
1.6       noro     6065:     /* exact division */
                   6066:     divsz(d0,gcd,&a);
1.1       noro     6067:     for ( i = 0; i < n; i++ ) {
                   6068:       mulz(a,q[i],&u);
                   6069:       if ( r[i] ) {
1.6       noro     6070:         /* exact division */
                   6071:         divsz(r[i],gcd,&u1);
1.1       noro     6072:         addz(u,u1,&q[i]);
                   6073:       } else
                   6074:         q[i] = u;
                   6075:     }
                   6076:   }
                   6077:   for ( i = 0; i < n; i++ ) c[i] = q[i];
                   6078: }
                   6079:
1.4       noro     6080: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
                   6081:
                   6082: void mpz_removecont_array(mpz_t *c,int n)
                   6083: {
                   6084:   mpz_t d0,a,u,u1,gcd;
                   6085:   int i,j;
1.13      noro     6086:   static mpz_t *q,*r;
                   6087:   static int c_len = 0;
1.4       noro     6088:
                   6089:   for ( i = 0; i < n; i++ )
                   6090:     if ( mpz_sgn(c[i]) ) break;
                   6091:   if ( i == n ) return;
                   6092:   gcdv_mpz_estimate(d0,c,n);
1.13      noro     6093:   if ( n > c_len ) {
                   6094:     q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   6095:     r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   6096:     c_len = n;
                   6097:   }
1.4       noro     6098:   for ( i = 0; i < n; i++ ) {
                   6099:     mpz_init(q[i]); mpz_init(r[i]);
                   6100:     mpz_fdiv_qr(q[i],r[i],c[i],d0);
                   6101:   }
                   6102:   for ( i = 0; i < n; i++ )
                   6103:     if ( mpz_sgn(r[i]) ) break;
                   6104:   mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
                   6105:   if ( i < n ) {
                   6106:     mpz_gcd(gcd,d0,r[i]);
                   6107:     for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
                   6108:     mpz_div(a,d0,gcd);
                   6109:     for ( i = 0; i < n; i++ ) {
                   6110:       mpz_mul(u,a,q[i]);
                   6111:       if ( mpz_sgn(r[i]) ) {
                   6112:         mpz_div(u1,r[i],gcd);
                   6113:         mpz_add(q[i],u,u1);
                   6114:       } else
                   6115:         mpz_set(q[i],u);
                   6116:     }
                   6117:   }
                   6118:   for ( i = 0; i < n; i++ )
                   6119:     mpz_set(c[i],q[i]);
                   6120: }
                   6121:
1.1       noro     6122: void nd_mul_c(int mod,ND p,int mul)
                   6123: {
                   6124:     NM m;
                   6125:     int c,c1;
                   6126:
                   6127:     if ( !p ) return;
                   6128:     if ( mul == 1 ) return;
                   6129:     if ( mod == -1 )
                   6130:         for ( m = BDY(p); m; m = NEXT(m) )
                   6131:             CM(m) = _mulsf(CM(m),mul);
                   6132:     else
                   6133:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   6134:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   6135:         }
                   6136: }
                   6137:
                   6138: void nd_mul_c_lf(ND p,Z mul)
                   6139: {
                   6140:     NM m;
                   6141:     Z c;
                   6142:
                   6143:     if ( !p ) return;
                   6144:     if ( UNIZ(mul) ) return;
                   6145:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   6146:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   6147:     }
                   6148: }
                   6149:
                   6150: void nd_mul_c_q(ND p,P mul)
                   6151: {
                   6152:     NM m;
                   6153:     P c;
                   6154:
                   6155:     if ( !p ) return;
                   6156:     if ( UNIQ(mul) ) return;
                   6157:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   6158:         mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
                   6159:     }
                   6160: }
                   6161:
                   6162: void nd_mul_c_p(VL vl,ND p,P mul)
                   6163: {
                   6164:     NM m;
                   6165:     P c;
                   6166:
                   6167:     if ( !p ) return;
                   6168:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   6169:         mulp(vl,CP(m),mul,&c); CP(m) = c;
                   6170:     }
                   6171: }
                   6172:
                   6173: void nd_free(ND p)
                   6174: {
                   6175:     NM t,s;
                   6176:
                   6177:     if ( !p ) return;
                   6178:     t = BDY(p);
                   6179:     while ( t ) {
                   6180:         s = NEXT(t);
                   6181:         FREENM(t);
                   6182:         t = s;
                   6183:     }
                   6184:     FREEND(p);
                   6185: }
                   6186:
                   6187: void ndv_free(NDV p)
                   6188: {
                   6189:     GCFREE(BDY(p));
                   6190: }
                   6191:
                   6192: void nd_append_red(UINT *d,int i)
                   6193: {
1.65    ! noro     6194:     RHist m;
1.1       noro     6195:     int h;
                   6196:
                   6197:     NEWRHist(m);
                   6198:     h = ndl_hash_value(d);
                   6199:     m->index = i;
                   6200:     ndl_copy(d,DL(m));
                   6201:     NEXT(m) = nd_red[h];
                   6202:     nd_red[h] = m;
                   6203: }
                   6204:
                   6205: UINT *ndv_compute_bound(NDV p)
                   6206: {
                   6207:     UINT *d1,*d2,*t;
                   6208:     UINT u;
                   6209:     int i,j,k,l,len,ind;
                   6210:     NMV m;
                   6211:
                   6212:     if ( !p )
                   6213:         return 0;
                   6214:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6215:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6216:     len = LEN(p);
                   6217:     m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   6218:     for ( i = 1; i < len; i++, NMV_ADV(m) ) {
                   6219:         ndl_max(DL(m),d1,d2);
                   6220:         t = d1; d1 = d2; d2 = t;
                   6221:     }
                   6222:     l = nd_nvar+31;
                   6223:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   6224:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   6225:         u = d1[i];
                   6226:         k = (nd_epw-1)*nd_bpe;
                   6227:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   6228:             t[ind] = (u>>k)&nd_mask0;
                   6229:     }
                   6230:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   6231:     return t;
                   6232: }
                   6233:
                   6234: UINT *nd_compute_bound(ND p)
                   6235: {
                   6236:     UINT *d1,*d2,*t;
                   6237:     UINT u;
1.65    ! noro     6238:     int i,j,k,l,ind;
1.1       noro     6239:     NM m;
                   6240:
                   6241:     if ( !p )
                   6242:         return 0;
                   6243:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6244:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6245:     m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   6246:     for ( m = NEXT(m); m; m = NEXT(m) ) {
                   6247:         ndl_lcm(DL(m),d1,d2);
                   6248:         t = d1; d1 = d2; d2 = t;
                   6249:     }
                   6250:     l = nd_nvar+31;
                   6251:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   6252:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   6253:         u = d1[i];
                   6254:         k = (nd_epw-1)*nd_bpe;
                   6255:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   6256:             t[ind] = (u>>k)&nd_mask0;
                   6257:     }
                   6258:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   6259:     return t;
                   6260: }
                   6261:
                   6262: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
                   6263: /* of a term. In this case we need additional 1 word. */
                   6264:
                   6265: int nd_get_exporigin(struct order_spec *ord)
                   6266: {
                   6267:     switch ( ord->id ) {
1.21      noro     6268:         case 0: case 2: case 256: case 258: case 300:
1.1       noro     6269:             return 1+nd_module;
                   6270:         case 1: case 257:
                   6271:             /* block order */
                   6272:             /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
                   6273:             /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
                   6274:             return ord->ord.block.length+1+nd_module;
                   6275:         case 3: case 259:
                   6276: #if 0
                   6277:             error("nd_get_exporigin : composite order is not supported yet.");
                   6278: #else
                   6279:             return 1+nd_module;
                   6280: #endif
                   6281:         default:
                   6282:             error("nd_get_exporigin : ivalid argument.");
                   6283:             return 0;
                   6284:     }
                   6285: }
                   6286:
                   6287: void nd_setup_parameters(int nvar,int max) {
1.65    ! noro     6288:     int i,j,elen,wpd;
1.45      noro     6289:     extern int CNVars;
1.1       noro     6290:
                   6291:     nd_nvar = nvar;
1.45      noro     6292:     CNVars = nvar;
1.1       noro     6293:     if ( max ) {
                   6294:         /* XXX */
                   6295:         if ( do_weyl ) nd_bpe = 32;
                   6296:         else if ( max < 2 ) nd_bpe = 1;
                   6297:         else if ( max < 4 ) nd_bpe = 2;
                   6298:         else if ( max < 8 ) nd_bpe = 3;
                   6299:         else if ( max < 16 ) nd_bpe = 4;
                   6300:         else if ( max < 32 ) nd_bpe = 5;
                   6301:         else if ( max < 64 ) nd_bpe = 6;
                   6302:         else if ( max < 256 ) nd_bpe = 8;
                   6303:         else if ( max < 1024 ) nd_bpe = 10;
                   6304:         else if ( max < 65536 ) nd_bpe = 16;
                   6305:         else nd_bpe = 32;
                   6306:     }
                   6307:     if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
                   6308:         UINT t;
1.65    ! noro     6309:         int st;
1.1       noro     6310:         int *v;
                   6311:   /* t = max(weights) */
                   6312:         t = 0;
                   6313:         if ( current_dl_weight_vector )
                   6314:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                   6315:                 if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
1.65    ! noro     6316:                 if ( t < (UINT)st ) t = st;
1.1       noro     6317:             }
                   6318:         if ( nd_matrix )
                   6319:             for ( i = 0; i < nd_matrix_len; i++ )
                   6320:                 for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
                   6321:                     if ( (st=v[j]) < 0 ) st = -st;
1.65    ! noro     6322:                     if ( t < (UINT)st ) t = st;
1.1       noro     6323:         }
                   6324:         /* i = bitsize of t */
                   6325:         for ( i = 0; t; t >>=1, i++ );
                   6326:         /* i += bitsize of nd_nvar */
                   6327:         for ( t = nd_nvar; t; t >>=1, i++);
                   6328:         /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
                   6329:         if ( (nd_bpe+i) >= 31 )
                   6330:             error("nd_setup_parameters : too large weight");
                   6331:     }
                   6332:     nd_epw = (sizeof(UINT)*8)/nd_bpe;
                   6333:     elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   6334:     nd_exporigin = nd_get_exporigin(nd_ord);
                   6335:     wpd = nd_exporigin+elen;
                   6336:     if ( nd_module )
                   6337:         nd_mpos = nd_exporigin-1;
                   6338:     else
                   6339:         nd_mpos = -1;
                   6340:     if ( wpd != nd_wpd ) {
                   6341:         nd_free_private_storage();
                   6342:         nd_wpd = wpd;
                   6343:     }
                   6344:     if ( nd_bpe < 32 ) {
                   6345:         nd_mask0 = (1<<nd_bpe)-1;
                   6346:     } else {
                   6347:         nd_mask0 = 0xffffffff;
                   6348:     }
                   6349:     bzero(nd_mask,sizeof(nd_mask));
                   6350:     nd_mask1 = 0;
                   6351:     for ( i = 0; i < nd_epw; i++ ) {
                   6352:         nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   6353:         nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   6354:     }
                   6355:     nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
                   6356:     nd_epos = nd_create_epos(nd_ord);
                   6357:     nd_blockmask = nd_create_blockmask(nd_ord);
                   6358:     nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
                   6359: }
                   6360:
                   6361: ND_pairs nd_reconstruct(int trace,ND_pairs d)
                   6362: {
                   6363:     int i,obpe,oadv,h;
                   6364:     static NM prev_nm_free_list;
                   6365:     static ND_pairs prev_ndp_free_list;
1.65    ! noro     6366:     RHist mr;
1.1       noro     6367:     RHist r;
                   6368:     RHist *old_red;
                   6369:     ND_pairs s0,s,t;
                   6370:     EPOS oepos;
                   6371:
                   6372:     obpe = nd_bpe;
                   6373:     oadv = nmv_adv;
                   6374:     oepos = nd_epos;
                   6375:     if ( obpe < 2 ) nd_bpe = 2;
                   6376:     else if ( obpe < 3 ) nd_bpe = 3;
                   6377:     else if ( obpe < 4 ) nd_bpe = 4;
                   6378:     else if ( obpe < 5 ) nd_bpe = 5;
                   6379:     else if ( obpe < 6 ) nd_bpe = 6;
                   6380:     else if ( obpe < 8 ) nd_bpe = 8;
                   6381:     else if ( obpe < 10 ) nd_bpe = 10;
                   6382:     else if ( obpe < 16 ) nd_bpe = 16;
                   6383:     else if ( obpe < 32 ) nd_bpe = 32;
                   6384:     else error("nd_reconstruct : exponent too large");
                   6385:
                   6386:     nd_setup_parameters(nd_nvar,0);
                   6387:     prev_nm_free_list = _nm_free_list;
                   6388:     prev_ndp_free_list = _ndp_free_list;
                   6389:     _nm_free_list = 0;
                   6390:     _ndp_free_list = 0;
                   6391:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   6392:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   6393:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   6394:     }
                   6395:     if ( trace )
                   6396:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   6397:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   6398:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   6399:         }
                   6400:     s0 = 0;
                   6401:     for ( t = d; t; t = NEXT(t) ) {
                   6402:         NEXTND_pairs(s0,s);
                   6403:         s->i1 = t->i1;
                   6404:         s->i2 = t->i2;
1.24      noro     6405:         s->sig = t->sig;
1.1       noro     6406:         SG(s) = SG(t);
                   6407:         ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   6408:     }
                   6409:
                   6410:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   6411:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   6412:         old_red[i] = nd_red[i];
                   6413:         nd_red[i] = 0;
                   6414:     }
                   6415:     for ( i = 0; i < REDTAB_LEN; i++ )
                   6416:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   6417:             NEWRHist(mr);
                   6418:             mr->index = r->index;
                   6419:             SG(mr) = SG(r);
                   6420:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   6421:             h = ndl_hash_value(DL(mr));
                   6422:             NEXT(mr) = nd_red[h];
                   6423:             nd_red[h] = mr;
1.24      noro     6424:             mr->sig = r->sig;
1.1       noro     6425:         }
                   6426:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   6427:     old_red = 0;
                   6428:     for ( i = 0; i < nd_psn; i++ ) {
                   6429:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   6430:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24      noro     6431:         r->sig = nd_psh[i]->sig;
1.1       noro     6432:         nd_psh[i] = r;
                   6433:     }
                   6434:     if ( s0 ) NEXT(s) = 0;
                   6435:     prev_nm_free_list = 0;
                   6436:     prev_ndp_free_list = 0;
                   6437: #if 0
                   6438:     GC_gcollect();
                   6439: #endif
                   6440:     return s0;
                   6441: }
                   6442:
1.30      noro     6443: void nd_reconstruct_s(int trace,ND_pairs *d)
                   6444: {
                   6445:     int i,obpe,oadv,h;
                   6446:     static NM prev_nm_free_list;
                   6447:     static ND_pairs prev_ndp_free_list;
1.65    ! noro     6448:     RHist mr;
1.30      noro     6449:     RHist r;
                   6450:     RHist *old_red;
                   6451:     ND_pairs s0,s,t;
                   6452:     EPOS oepos;
                   6453:
                   6454:     obpe = nd_bpe;
                   6455:     oadv = nmv_adv;
                   6456:     oepos = nd_epos;
                   6457:     if ( obpe < 2 ) nd_bpe = 2;
                   6458:     else if ( obpe < 3 ) nd_bpe = 3;
                   6459:     else if ( obpe < 4 ) nd_bpe = 4;
                   6460:     else if ( obpe < 5 ) nd_bpe = 5;
                   6461:     else if ( obpe < 6 ) nd_bpe = 6;
                   6462:     else if ( obpe < 8 ) nd_bpe = 8;
                   6463:     else if ( obpe < 10 ) nd_bpe = 10;
                   6464:     else if ( obpe < 16 ) nd_bpe = 16;
                   6465:     else if ( obpe < 32 ) nd_bpe = 32;
                   6466:     else error("nd_reconstruct_s : exponent too large");
                   6467:
                   6468:     nd_setup_parameters(nd_nvar,0);
                   6469:     prev_nm_free_list = _nm_free_list;
                   6470:     prev_ndp_free_list = _ndp_free_list;
                   6471:     _nm_free_list = 0;
                   6472:     _ndp_free_list = 0;
                   6473:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   6474:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   6475:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   6476:     }
                   6477:     if ( trace )
                   6478:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   6479:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   6480:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   6481:         }
                   6482:
                   6483:     for ( i = 0; i < nd_nbase; i++ ) {
                   6484:       s0 = 0;
                   6485:       for ( t = d[i]; t; t = NEXT(t) ) {
                   6486:           NEXTND_pairs(s0,s);
                   6487:           s->i1 = t->i1;
                   6488:           s->i2 = t->i2;
                   6489:           s->sig = t->sig;
                   6490:           SG(s) = SG(t);
                   6491:           ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   6492:       }
1.47      noro     6493:       if ( s0 ) NEXT(s) = 0;
1.30      noro     6494:       d[i] = s0;
                   6495:     }
                   6496:
                   6497:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   6498:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   6499:         old_red[i] = nd_red[i];
                   6500:         nd_red[i] = 0;
                   6501:     }
                   6502:     for ( i = 0; i < REDTAB_LEN; i++ )
                   6503:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   6504:             NEWRHist(mr);
                   6505:             mr->index = r->index;
                   6506:             SG(mr) = SG(r);
                   6507:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   6508:             h = ndl_hash_value(DL(mr));
                   6509:             NEXT(mr) = nd_red[h];
                   6510:             nd_red[h] = mr;
                   6511:             mr->sig = r->sig;
                   6512:         }
                   6513:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   6514:     old_red = 0;
                   6515:     for ( i = 0; i < nd_psn; i++ ) {
                   6516:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   6517:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
                   6518:         r->sig = nd_psh[i]->sig;
                   6519:         nd_psh[i] = r;
                   6520:     }
                   6521:     if ( s0 ) NEXT(s) = 0;
                   6522:     prev_nm_free_list = 0;
                   6523:     prev_ndp_free_list = 0;
                   6524: #if 0
                   6525:     GC_gcollect();
                   6526: #endif
                   6527: }
                   6528:
1.1       noro     6529: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
                   6530: {
1.65    ! noro     6531:     int n,i,ei,omask0,j,s,ord_l,l;
1.1       noro     6532:     struct order_pair *op;
                   6533:
                   6534:     n = nd_nvar;
1.65    ! noro     6535: //    oepw = (sizeof(UINT)*8)/obpe;
1.1       noro     6536:     omask0 = (1<<obpe)-1;
                   6537:     TD(r) = TD(d);
                   6538:     for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
                   6539:     if ( nd_blockmask ) {
                   6540:         l = nd_blockmask->n;
                   6541:         op = nd_blockmask->order_pair;
                   6542:         for ( i = 1; i < nd_exporigin; i++ )
                   6543:             r[i] = d[i];
                   6544:         for ( j = 0, s = 0; j < l; j++ ) {
                   6545:             ord_l = op[j].length;
                   6546:             for ( i = 0; i < ord_l; i++, s++ ) {
                   6547:                 ei =  GET_EXP_OLD(d,s);
                   6548:                 PUT_EXP(r,s,ei);
                   6549:             }
                   6550:         }
                   6551:     } else {
                   6552:         for ( i = 0; i < n; i++ ) {
                   6553:             ei = GET_EXP_OLD(d,i);
                   6554:             PUT_EXP(r,i,ei);
                   6555:         }
                   6556:     }
                   6557:     if ( nd_module ) MPOS(r) = MPOS(d);
                   6558: }
                   6559:
                   6560: ND nd_copy(ND p)
                   6561: {
                   6562:     NM m,mr,mr0;
                   6563:     ND r;
                   6564:
                   6565:     if ( !p )
                   6566:         return 0;
                   6567:     else {
                   6568:         for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   6569:             NEXTNM(mr0,mr);
                   6570:             CM(mr) = CM(m);
                   6571:             ndl_copy(DL(m),DL(mr));
                   6572:         }
                   6573:         NEXT(mr) = 0;
                   6574:         MKND(NV(p),mr0,LEN(p),r);
                   6575:         SG(r) = SG(p);
                   6576:         return r;
                   6577:     }
                   6578: }
                   6579:
                   6580: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
                   6581: {
                   6582:     NM m1,m2;
                   6583:     NDV p1,p2;
                   6584:     ND t1,t2;
                   6585:     UINT *lcm;
                   6586:     P gp,tp;
                   6587:     Z g,t;
                   6588:     Z iq;
                   6589:     LIST hist;
                   6590:     NODE node;
                   6591:     DP d;
                   6592:
                   6593:     if ( !mod && nd_demand ) {
                   6594:         p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
                   6595:     } else {
                   6596:         if ( trace ) {
                   6597:             p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   6598:         } else {
                   6599:             p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   6600:         }
                   6601:     }
                   6602:     lcm = LCM(p);
                   6603:     NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
                   6604:     if ( ndl_check_bound2(p->i1,DL(m1)) ) {
                   6605:         FREENM(m1); return 0;
                   6606:     }
                   6607:     NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
                   6608:     if ( ndl_check_bound2(p->i2,DL(m2)) ) {
                   6609:         FREENM(m1); FREENM(m2); return 0;
                   6610:     }
                   6611:
                   6612:     if ( mod == -1 ) {
                   6613:         CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
                   6614:     } else if ( mod > 0 ) {
                   6615:         CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
                   6616:     } else if ( mod == -2 ) {
                   6617:         CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
                   6618:     } else if ( nd_vc ) {
                   6619:         ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
                   6620:         divsp(nd_vc,HCP(p2),gp,&CP(m1));
                   6621:         divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
                   6622:     } else {
1.6       noro     6623:         igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1       noro     6624:     }
                   6625:     t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
                   6626:     *rp = nd_add(mod,t1,t2);
                   6627:     if ( nd_gentrace ) {
                   6628:         /* nd_tracelist is initialized */
1.6       noro     6629:         STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     6630:         MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6       noro     6631:         STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     6632:         MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
                   6633:         nd_tracelist = node;
                   6634:     }
1.24      noro     6635:     if ( *rp )
                   6636:       (*rp)->sig = p->sig;
1.1       noro     6637:     FREENM(m1); FREENM(m2);
                   6638:     return 1;
                   6639: }
                   6640:
                   6641: void ndv_mul_c(int mod,NDV p,int mul)
                   6642: {
                   6643:     NMV m;
                   6644:     int c,c1,len,i;
                   6645:
                   6646:     if ( !p ) return;
                   6647:     len = LEN(p);
                   6648:     if ( mod == -1 )
                   6649:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   6650:             CM(m) = _mulsf(CM(m),mul);
                   6651:     else
                   6652:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6653:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   6654:         }
                   6655: }
                   6656:
                   6657: void ndv_mul_c_lf(NDV p,Z mul)
                   6658: {
                   6659:     NMV m;
                   6660:     Z c;
                   6661:     int len,i;
                   6662:
                   6663:     if ( !p ) return;
                   6664:     len = LEN(p);
                   6665:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6666:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   6667:     }
                   6668: }
                   6669:
                   6670: /* for nd_det */
                   6671: void ndv_mul_c_q(NDV p,Z mul)
                   6672: {
                   6673:     NMV m;
                   6674:     Z c;
                   6675:     int len,i;
                   6676:
                   6677:     if ( !p ) return;
                   6678:     len = LEN(p);
                   6679:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     6680:         mulz(CZ(m),mul,&c); CZ(m) = c;
1.1       noro     6681:     }
                   6682: }
                   6683:
                   6684: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
                   6685:     int n2,i,j,l,n,tlen;
                   6686:     UINT *d0;
                   6687:     NM *tab,*psum;
                   6688:     ND s,r;
                   6689:     NM t;
                   6690:     NMV m1;
                   6691:
                   6692:     if ( !p ) return 0;
                   6693:     n = NV(p); n2 = n>>1;
                   6694:     d0 = DL(m0);
                   6695:     l = LEN(p);
                   6696:     for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   6697:     tab = (NM *)MALLOC(tlen*sizeof(NM));
                   6698:     psum = (NM *)MALLOC(tlen*sizeof(NM));
                   6699:     for ( i = 0; i < tlen; i++ ) psum[i] = 0;
                   6700:     m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   6701:     for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
                   6702:         /* m0(NM) * m1(NMV) => tab(NM) */
                   6703:         weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
                   6704:         for ( j = 0; j < tlen; j++ ) {
                   6705:             if ( tab[j] ) {
                   6706:                 NEXT(tab[j]) = psum[j];    psum[j] = tab[j];
                   6707:             }
                   6708:         }
                   6709:     }
                   6710:     for ( i = tlen-1, r = 0; i >= 0; i-- )
                   6711:         if ( psum[i] ) {
                   6712:             for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   6713:             MKND(n,psum[i],j,s);
                   6714:             r = nd_add(mod,r,s);
                   6715:         }
                   6716:     if ( r ) SG(r) = SG(p)+TD(d0);
                   6717:     return r;
                   6718: }
                   6719:
                   6720: /* product of monomials */
                   6721: /* XXX block order is not handled correctly */
                   6722:
                   6723: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   6724: {
                   6725:     int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
                   6726:     UINT *d0,*d1,*d,*dt,*ctab;
                   6727:     Z *ctab_q;
                   6728:     Z q,q1;
                   6729:     UINT c0,c1,c;
                   6730:     NM *p;
                   6731:     NM m,t;
                   6732:     int mpos;
                   6733:
                   6734:     for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   6735:     if ( !m0 || !m1 ) return;
                   6736:     d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
                   6737:     if ( nd_module )
                   6738:         if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
                   6739:
                   6740:     NEWNM(m); d = DL(m);
                   6741:     if ( mod ) {
                   6742:         c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
                   6743:     } else if ( nd_vc )
                   6744:         mulp(nd_vc,CP(m0),CP(m1),&CP(m));
                   6745:   else
1.6       noro     6746:         mulz(CZ(m0),CZ(m1),&CZ(m));
1.1       noro     6747:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   6748:     homo = n&1 ? 1 : 0;
                   6749:     if ( homo ) {
                   6750:         /* offset of h-degree */
                   6751:         h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   6752:         PUT_EXP(DL(m),n-1,h);
                   6753:         TD(DL(m)) = h;
                   6754:         if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   6755:     }
                   6756:     tab[0] = m;
                   6757:     NEWNM(m); d = DL(m);
                   6758:     for ( i = 0, curlen = 1; i < n2; i++ ) {
                   6759:         a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   6760:         k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   6761:         /* xi^a*(Di^k*xi^l)*Di^b */
                   6762:         a += l; b += k;
                   6763:         s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
                   6764:         if ( !k || !l ) {
                   6765:             for ( j = 0; j < curlen; j++ )
                   6766:                 if ( (t = tab[j]) != 0 ) {
                   6767:                     dt = DL(t);
                   6768:                     PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
                   6769:                     if ( nd_blockmask ) ndl_weight_mask(dt);
                   6770:                 }
                   6771:             curlen *= k+1;
                   6772:             continue;
                   6773:         }
                   6774:         min = MIN(k,l);
                   6775:         if ( mod ) {
                   6776:             ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
                   6777:             mkwcm(k,l,mod,(int *)ctab);
                   6778:         } else {
                   6779:             ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
                   6780:             mkwc(k,l,ctab_q);
                   6781:         }
                   6782:         for ( j = min; j >= 0; j-- ) {
                   6783:             for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
                   6784:             PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
                   6785:             h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
                   6786:             if ( homo ) {
                   6787:                 TD(d) = s;
                   6788:                 PUT_EXP(d,n-1,s-h);
                   6789:             } else TD(d) = h;
                   6790:             if ( nd_blockmask ) ndl_weight_mask(d);
                   6791:             if ( mod ) c = ctab[j];
                   6792:             else q = ctab_q[j];
                   6793:             p = tab+curlen*j;
                   6794:             if ( j == 0 ) {
                   6795:                 for ( u = 0; u < curlen; u++, p++ ) {
                   6796:                     if ( tab[u] ) {
                   6797:                         ndl_addto(DL(tab[u]),d);
                   6798:                         if ( mod ) {
                   6799:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
                   6800:                         } else if ( nd_vc )
                   6801:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
                   6802:             else {
1.6       noro     6803:                             mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1       noro     6804:                         }
                   6805:                     }
                   6806:                 }
                   6807:             } else {
                   6808:                 for ( u = 0; u < curlen; u++, p++ ) {
                   6809:                     if ( tab[u] ) {
                   6810:                         NEWNM(t);
                   6811:                         ndl_add(DL(tab[u]),d,DL(t));
                   6812:                         if ( mod ) {
                   6813:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
                   6814:                         } else if ( nd_vc )
                   6815:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
                   6816:             else
1.6       noro     6817:                             mulz(CZ(tab[u]),q,&CZ(t));
1.1       noro     6818:                         *p = t;
                   6819:                     }
                   6820:                 }
                   6821:             }
                   6822:         }
                   6823:         curlen *= k+1;
                   6824:     }
                   6825:     FREENM(m);
                   6826:     if ( nd_module ) {
                   6827:         mpos = MPOS(d1);
                   6828:         for ( i = 0; i < tlen; i++ )
                   6829:             if ( tab[i] ) {
                   6830:                 d = DL(tab[i]);
                   6831:                 MPOS(d) = mpos;
                   6832:                 TD(d) = ndl_weight(d);
                   6833:             }
                   6834:     }
                   6835: }
                   6836:
                   6837: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   6838: {
                   6839:     NM mr,mr0;
                   6840:     NMV m;
1.65    ! noro     6841:     UINT *d;
        !          6842:     int i,len;
1.1       noro     6843:     ND r;
                   6844:
                   6845:     if ( !p ) return 0;
                   6846:     else {
1.65    ! noro     6847:         m = BDY(p);
1.1       noro     6848:         d = DL(m0);
                   6849:         len = LEN(p);
                   6850:         mr0 = 0;
                   6851:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6852:             NEXTNM(mr0,mr);
                   6853:             CM(mr) = 1;
                   6854:             ndl_add(DL(m),d,DL(mr));
                   6855:         }
                   6856:         NEXT(mr) = 0;
                   6857:         MKND(NV(p),mr0,len,r);
                   6858:         SG(r) = SG(p) + TD(d);
                   6859:         return r;
                   6860:     }
                   6861: }
                   6862:
                   6863: ND ndv_mul_nm(int mod,NM m0,NDV p)
                   6864: {
                   6865:     NM mr,mr0;
                   6866:     NMV m;
1.65    ! noro     6867:     UINT *d;
        !          6868:     int c,i,c1,c2,len;
1.1       noro     6869:     P q;
                   6870:     ND r;
                   6871:
                   6872:     if ( !p ) return 0;
                   6873:     else if ( do_weyl ) {
                   6874:         if ( mod < 0 ) {
                   6875:             error("ndv_mul_nm : not implemented (weyl)");
                   6876:             return 0;
                   6877:         } else
                   6878:             return weyl_ndv_mul_nm(mod,m0,p);
                   6879:     } else {
1.65    ! noro     6880:         m = BDY(p);
1.1       noro     6881:         d = DL(m0);
                   6882:         len = LEN(p);
                   6883:         mr0 = 0;
                   6884:         if ( mod == -1 ) {
                   6885:             c = CM(m0);
                   6886:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6887:                 NEXTNM(mr0,mr);
                   6888:                 CM(mr) = _mulsf(CM(m),c);
                   6889:                 ndl_add(DL(m),d,DL(mr));
                   6890:             }
                   6891:         } else if ( mod == -2 ) {
                   6892:             Z cl;
                   6893:             cl = CZ(m0);
                   6894:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6895:                 NEXTNM(mr0,mr);
                   6896:                 mullf(CZ(m),cl,&CZ(mr));
                   6897:                 ndl_add(DL(m),d,DL(mr));
                   6898:             }
                   6899:         } else if ( mod ) {
                   6900:             c = CM(m0);
                   6901:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6902:                 NEXTNM(mr0,mr);
                   6903:                 c1 = CM(m);
                   6904:                 DMAR(c1,c,0,mod,c2);
                   6905:                 CM(mr) = c2;
                   6906:                 ndl_add(DL(m),d,DL(mr));
                   6907:             }
                   6908:         } else {
                   6909:             q = CP(m0);
                   6910:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6911:                 NEXTNM(mr0,mr);
                   6912:                 mulp(nd_vc,CP(m),q,&CP(mr));
                   6913:                 ndl_add(DL(m),d,DL(mr));
                   6914:             }
                   6915:         }
                   6916:         NEXT(mr) = 0;
                   6917:         MKND(NV(p),mr0,len,r);
                   6918:         SG(r) = SG(p) + TD(d);
                   6919:         return r;
                   6920:     }
                   6921: }
                   6922:
                   6923: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
                   6924: {
                   6925:     NM mq0,mq;
                   6926:     NMV tm;
1.65    ! noro     6927:     int i,nv,c,c1,c2,hindex;
1.1       noro     6928:     ND p,t,r;
                   6929:
                   6930:     if ( bucket->m < 0 ) return 0;
                   6931:     else {
                   6932:         nv = NV(d);
                   6933:         mq0 = 0;
                   6934:         tm = (NMV)MALLOC(nmv_adv);
                   6935:         while ( 1 ) {
                   6936:             if ( mod > 0 || mod == -1 )
                   6937:               hindex = head_pbucket(mod,bucket);
                   6938:             else if ( mod == -2 )
                   6939:               hindex = head_pbucket_lf(bucket);
                   6940:             else
                   6941:               hindex = head_pbucket_q(bucket);
                   6942:             if ( hindex < 0 ) break;
                   6943:             p = bucket->body[hindex];
                   6944:             NEXTNM(mq0,mq);
                   6945:             ndl_sub(HDL(p),HDL(d),DL(mq));
                   6946:             ndl_copy(DL(mq),DL(tm));
                   6947:             if ( mod ) {
                   6948:                 c1 = invm(HCM(d),mod); c2 = HCM(p);
                   6949:                 DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   6950:                 CM(tm) = mod-c;
                   6951:             } else {
1.6       noro     6952:                 divsz(HCZ(p),HCZ(d),&CZ(mq));
                   6953:                 chsgnz(CZ(mq),&CZ(tm));
1.1       noro     6954:             }
                   6955:             t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
                   6956:             bucket->body[hindex] = nd_remove_head(p);
                   6957:             t = nd_remove_head(t);
                   6958:             add_pbucket(mod,bucket,t);
                   6959:         }
                   6960:         if ( !mq0 )
                   6961:             r = 0;
                   6962:         else {
                   6963:             NEXT(mq) = 0;
                   6964:             for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   6965:             MKND(nv,mq0,i,r);
                   6966:             /* XXX */
                   6967:             SG(r) = HTD(r);
                   6968:         }
                   6969:         return r;
                   6970:     }
                   6971: }
                   6972:
                   6973: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   6974: {
                   6975:     NMV m,mr,mr0,t;
                   6976:     int len,i,k;
                   6977:
                   6978:     if ( !p ) return;
                   6979:     m = BDY(p); len = LEN(p);
                   6980:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   6981:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   6982:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   6983:     t = (NMV)MALLOC(nmv_adv);
                   6984:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6       noro     6985:         CZ(t) = CZ(m);
1.1       noro     6986:         for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   6987:         ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6       noro     6988:         CZ(mr) = CZ(t);
1.1       noro     6989:         ndl_copy(DL(t),DL(mr));
                   6990:     }
                   6991:     BDY(p) = mr0;
                   6992: }
                   6993:
                   6994: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   6995: {
                   6996:     NMV m,mr,mr0;
                   6997:     int len,i;
                   6998:     NDV r;
                   6999:
                   7000:     if ( !p ) return 0;
                   7001:     m = BDY(p); len = LEN(p);
                   7002:     mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   7003:     for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   7004:         ndl_zero(DL(mr));
                   7005:         ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6       noro     7006:         CZ(mr) = CZ(m);
1.1       noro     7007:     }
                   7008:     MKNDV(NV(p),mr0,len,r);
                   7009:     SG(r) = SG(p);
1.24      noro     7010:     r->sig = p->sig;
1.1       noro     7011:     return r;
                   7012: }
                   7013:
                   7014: /* duplicate p */
                   7015:
                   7016: NDV ndv_dup(int mod,NDV p)
                   7017: {
                   7018:     NDV d;
                   7019:     NMV t,m,m0;
                   7020:     int i,len;
                   7021:
                   7022:     if ( !p ) return 0;
                   7023:     len = LEN(p);
                   7024:     m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   7025:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   7026:         ndl_copy(DL(t),DL(m));
1.6       noro     7027:         CZ(m) = CZ(t);
1.1       noro     7028:     }
                   7029:     MKNDV(NV(p),m0,len,d);
                   7030:     SG(d) = SG(p);
                   7031:     return d;
                   7032: }
                   7033:
                   7034: NDV ndv_symbolic(int mod,NDV p)
                   7035: {
                   7036:     NDV d;
                   7037:     NMV t,m,m0;
                   7038:     int i,len;
                   7039:
                   7040:     if ( !p ) return 0;
                   7041:     len = LEN(p);
                   7042:     m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   7043:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   7044:         ndl_copy(DL(t),DL(m));
1.6       noro     7045:         CZ(m) = ONE;
1.1       noro     7046:     }
                   7047:     MKNDV(NV(p),m0,len,d);
                   7048:     SG(d) = SG(p);
                   7049:     return d;
                   7050: }
                   7051:
                   7052: ND nd_dup(ND p)
                   7053: {
                   7054:     ND d;
                   7055:     NM t,m,m0;
                   7056:
                   7057:     if ( !p ) return 0;
                   7058:     for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   7059:         NEXTNM(m0,m);
                   7060:         ndl_copy(DL(t),DL(m));
1.6       noro     7061:         CZ(m) = CZ(t);
1.1       noro     7062:     }
                   7063:     if ( m0 ) NEXT(m) = 0;
                   7064:     MKND(NV(p),m0,LEN(p),d);
                   7065:     SG(d) = SG(p);
                   7066:     return d;
                   7067: }
                   7068:
                   7069: /* XXX if p->len == 0 then it represents 0 */
                   7070:
                   7071: void ndv_mod(int mod,NDV p)
                   7072: {
                   7073:     NMV t,d;
1.65    ! noro     7074:     int r;
1.1       noro     7075:     int i,len,dlen;
                   7076:     P cp;
                   7077:     Z c;
                   7078:     Obj gfs;
                   7079:
                   7080:     if ( !p ) return;
                   7081:     len = LEN(p);
                   7082:     dlen = 0;
                   7083:     if ( mod == -1 )
                   7084:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   7085:             simp_ff((Obj)CP(t),&gfs);
                   7086:             if ( gfs ) {
                   7087:               r = FTOIF(CONT((GFS)gfs));
                   7088:               CM(d) = r;
                   7089:               ndl_copy(DL(t),DL(d));
                   7090:               NMV_ADV(d);
                   7091:               dlen++;
                   7092:             }
                   7093:         }
                   7094:     else if ( mod == -2 )
                   7095:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   7096:             simp_ff((Obj)CP(t),&gfs);
                   7097:             if ( gfs ) {
                   7098:               lmtolf((LM)gfs,&CZ(d));
                   7099:               ndl_copy(DL(t),DL(d));
                   7100:               NMV_ADV(d);
                   7101:               dlen++;
                   7102:             }
                   7103:         }
                   7104:     else
                   7105:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   7106:             if ( nd_vc ) {
                   7107:                 nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
                   7108:                 c = (Z)cp;
                   7109:             } else
1.6       noro     7110:                 c = CZ(t);
1.1       noro     7111:             r = remqi((Q)c,mod);
                   7112:             if ( r ) {
                   7113:                 CM(d) = r;
                   7114:                 ndl_copy(DL(t),DL(d));
                   7115:                 NMV_ADV(d);
                   7116:                 dlen++;
                   7117:             }
                   7118:         }
                   7119:     LEN(p) = dlen;
                   7120: }
                   7121:
                   7122: NDV ptondv(VL vl,VL dvl,P p)
                   7123: {
                   7124:     ND nd;
                   7125:
                   7126:     nd = ptond(vl,dvl,p);
                   7127:     return ndtondv(0,nd);
                   7128: }
                   7129:
                   7130: void pltozpl(LIST l,Q *cont,LIST *pp)
                   7131: {
1.16      noro     7132:   NODE nd,nd1;
                   7133:   int n;
                   7134:   P *pl;
                   7135:   Q *cl;
                   7136:   int i;
                   7137:   P dmy;
                   7138:   Z dvr,inv;
                   7139:   LIST r;
                   7140:
                   7141:   nd = BDY(l); n = length(nd);
                   7142:   pl = (P *)MALLOC(n*sizeof(P));
                   7143:   cl = (Q *)MALLOC(n*sizeof(Q));
                   7144:   for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
                   7145:     ptozp((P)BDY(nd),1,&cl[i],&dmy);
                   7146:   }
                   7147:   qltozl(cl,n,&dvr);
                   7148:   divz(ONE,dvr,&inv);
                   7149:   nd = BDY(l);
                   7150:   for ( i = 0; i < n; i++, nd = NEXT(nd) )
                   7151:     divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
                   7152:   nd = 0;
                   7153:   for ( i = n-1; i >= 0; i-- ) {
                   7154:     MKNODE(nd1,pl[i],nd); nd = nd1;
                   7155:   }
                   7156:   MKLIST(r,nd);
                   7157:   *pp = r;
1.1       noro     7158: }
                   7159:
                   7160: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
                   7161:
                   7162: NDV pltondv(VL vl,VL dvl,LIST p)
                   7163: {
                   7164:     int i;
                   7165:     NODE t;
                   7166:     ND r,ri;
                   7167:     NM m;
                   7168:
                   7169:     if ( !nd_module ) error("pltond : module order must be set");
                   7170:     r = 0;
                   7171:     for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
                   7172:         ri = ptond(vl,dvl,(P)BDY(t));
                   7173:         if ( ri )
                   7174:             for ( m = BDY(ri); m; m = NEXT(m) ) {
                   7175:                 MPOS(DL(m)) = i;
                   7176:                 TD(DL(m)) = ndl_weight(DL(m));
                   7177:                 if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   7178:             }
                   7179:         r = nd_add(0,r,ri);
                   7180:     }
                   7181:     return ndtondv(0,r);
                   7182: }
                   7183:
                   7184: ND ptond(VL vl,VL dvl,P p)
                   7185: {
1.65    ! noro     7186:     int j,k,e,i;
1.1       noro     7187:     VL tvl;
                   7188:     V v;
                   7189:     DCP dc;
                   7190:     DCP *w;
1.65    ! noro     7191:     ND r,t,s;
1.1       noro     7192:     P x;
                   7193:     UINT *d;
                   7194:     NM m,m0;
                   7195:
                   7196:     if ( !p )
                   7197:         return 0;
                   7198:     else if ( NUM(p) ) {
                   7199:         NEWNM(m);
                   7200:         ndl_zero(DL(m));
1.48      noro     7201:         if ( RATN(p) && !INT((Q)p) )
1.1       noro     7202:           error("ptond : input must be integer-coefficient");
1.6       noro     7203:         CZ(m) = (Z)p;
1.1       noro     7204:         NEXT(m) = 0;
                   7205:         MKND(nd_nvar,m,1,r);
                   7206:         SG(r) = 0;
                   7207:         return r;
                   7208:     } else {
                   7209:         for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   7210:         w = (DCP *)MALLOC(k*sizeof(DCP));
                   7211:         for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   7212:         for ( i = 0, tvl = dvl, v = VR(p);
                   7213:             tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   7214:         if ( !tvl ) {
                   7215:             for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   7216:                 t = ptond(vl,dvl,COEF(w[j]));
                   7217:                 pwrp(vl,x,DEG(w[j]),&p);
                   7218:                 nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   7219:             }
                   7220:             return s;
                   7221:         } else {
                   7222:             NEWNM(m0); d = DL(m0);
                   7223:             for ( j = k-1, s = 0; j >= 0; j-- ) {
1.65    ! noro     7224:                 ndl_zero(d); e = (int)ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1       noro     7225:                 TD(d) = MUL_WEIGHT(e,i);
                   7226:                 if ( nd_blockmask) ndl_weight_mask(d);
                   7227:                 if ( nd_module ) MPOS(d) = 0;
                   7228:                 t = ptond(vl,dvl,COEF(w[j]));
                   7229:                 for ( m = BDY(t); m; m = NEXT(m) )
                   7230:                     ndl_addto(DL(m),d);
                   7231:                 SG(t) += TD(d);
                   7232:                 s = nd_add(0,s,t);
                   7233:             }
                   7234:             FREENM(m0);
                   7235:             return s;
                   7236:         }
                   7237:     }
                   7238: }
                   7239:
                   7240: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   7241: {
                   7242:     VL tvl;
                   7243:     int len,n,j,i,e;
                   7244:     NMV m;
                   7245:     Z q;
                   7246:     P c;
                   7247:     UINT *d;
                   7248:     P s,r,u,t,w;
                   7249:     GFS gfs;
                   7250:
                   7251:     if ( !p ) return 0;
                   7252:     else {
                   7253:         len = LEN(p);
                   7254:         n = NV(p);
                   7255:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   7256:         for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   7257:             if ( mod == -1 ) {
                   7258:                e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   7259:             } else if ( mod == -2 ) {
                   7260:                c = (P)CZ(m);
                   7261:             } else if ( mod > 0 ) {
1.6       noro     7262:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     7263:             } else
                   7264:                 c = CP(m);
                   7265:             d = DL(m);
                   7266:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     7267:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     7268:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   7269:             }
                   7270:             addp(vl,s,t,&u); s = u;
                   7271:         }
                   7272:         return s;
                   7273:     }
                   7274: }
                   7275:
                   7276: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
                   7277: {
                   7278:     VL tvl;
                   7279:     int len,n,j,i,e;
                   7280:     NMV m;
                   7281:     Z q;
                   7282:     P c;
                   7283:     UINT *d;
1.65    ! noro     7284:     P r,u,t,w;
1.1       noro     7285:     GFS gfs;
                   7286:     P *a;
                   7287:     LIST l;
                   7288:     NODE nd,nd1;
                   7289:
                   7290:     if ( !p ) return 0;
                   7291:     else {
                   7292:         a = (P *)MALLOC((rank+1)*sizeof(P));
                   7293:         for ( i = 0; i <= rank; i++ ) a[i] = 0;
                   7294:         len = LEN(p);
                   7295:         n = NV(p);
                   7296:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   7297:         for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
                   7298:             if ( mod == -1 ) {
                   7299:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   7300:             } else if ( mod ) {
1.6       noro     7301:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     7302:             } else
                   7303:                 c = CP(m);
                   7304:             d = DL(m);
                   7305:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     7306:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     7307:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   7308:             }
                   7309:             addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
                   7310:         }
                   7311:         nd = 0;
                   7312:         for ( i = rank; i > 0; i-- ) {
                   7313:             MKNODE(nd1,a[i],nd); nd = nd1;
                   7314:         }
                   7315:         MKLIST(l,nd);
                   7316:         return l;
                   7317:     }
                   7318: }
                   7319:
                   7320: NDV ndtondv(int mod,ND p)
                   7321: {
                   7322:     NDV d;
                   7323:     NMV m,m0;
                   7324:     NM t;
                   7325:     int i,len;
                   7326:
                   7327:     if ( !p ) return 0;
                   7328:     len = LEN(p);
                   7329:     if ( mod > 0 || mod == -1 )
                   7330:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   7331:     else
                   7332:         m0 = m = MALLOC(len*nmv_adv);
                   7333: #if 0
                   7334:     ndv_alloc += nmv_adv*len;
                   7335: #endif
                   7336:     for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   7337:         ndl_copy(DL(t),DL(m));
1.6       noro     7338:         CZ(m) = CZ(t);
1.1       noro     7339:     }
                   7340:     MKNDV(NV(p),m0,len,d);
                   7341:     SG(d) = SG(p);
1.24      noro     7342:     d->sig = p->sig;
1.1       noro     7343:     return d;
                   7344: }
                   7345:
1.16      noro     7346: static int dmm_comp_nv;
                   7347:
                   7348: int dmm_comp(DMM *a,DMM *b)
                   7349: {
                   7350:    return -compdmm(dmm_comp_nv,*a,*b);
                   7351: }
                   7352:
                   7353: void dmm_sort_by_ord(DMM *a,int len,int nv)
                   7354: {
                   7355:   dmm_comp_nv = nv;
                   7356:   qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
                   7357: }
                   7358:
                   7359: void dpm_sort(DPM p,DPM *rp)
                   7360: {
                   7361:   DMM t,t1;
                   7362:   int len,i,n;
                   7363:   DMM *a;
                   7364:   DPM d;
                   7365:
                   7366:   if ( !p ) *rp = 0;
                   7367:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   7368:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   7369:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   7370:   n = p->nv;
                   7371:   dmm_sort_by_ord(a,len,n);
                   7372:   t = 0;
                   7373:   for ( i = len-1; i >= 0; i-- ) {
                   7374:     NEWDMM(t1);
                   7375:     t1->c = a[i]->c;
                   7376:     t1->dl = a[i]->dl;
                   7377:     t1->pos = a[i]->pos;
                   7378:     t1->next = t;
                   7379:     t = t1;
                   7380:   }
                   7381:   MKDPM(n,t,d);
                   7382:   SG(d) = SG(p);
                   7383:   *rp = d;
                   7384: }
                   7385:
1.18      noro     7386: int dpm_comp(DPM *a,DPM *b)
                   7387: {
1.22      noro     7388:   return -compdpm(CO,*a,*b);
1.18      noro     7389: }
                   7390:
                   7391: NODE dpm_sort_list(NODE l)
                   7392: {
                   7393:   int i,len;
                   7394:   NODE t,t1;
                   7395:   DPM *a;
                   7396:
                   7397:   len = length(l);
                   7398:   a = (DPM *)MALLOC(len*sizeof(DPM));
                   7399:   for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
                   7400:   qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
                   7401:   t = 0;
                   7402:   for ( i = len-1; i >= 0; i-- ) {
                   7403:     MKNODE(t1,(pointer)a[i],t); t = t1;
                   7404:   }
                   7405:   return t;
                   7406: }
                   7407:
1.20      noro     7408: int nmv_comp(NMV a,NMV b)
                   7409: {
1.21      noro     7410:   int t;
                   7411:   t = DL_COMPARE(a->dl,b->dl);
                   7412:   return -t;
1.20      noro     7413: }
                   7414:
1.43      noro     7415: NDV dptondv(int mod,DP p)
                   7416: {
                   7417:   NDV d;
                   7418:   NMV m,m0;
                   7419:   MP t;
                   7420:   int i,len,n;
                   7421:
                   7422:   if ( !p ) return 0;
                   7423:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   7424:   n = p->nv;
                   7425:   if ( mod > 0 || mod == -1 )
                   7426:     m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   7427:   else
                   7428:     m0 = m = MALLOC(len*nmv_adv);
                   7429:   for ( i = 0, t = BDY(p); i < len; i++, NMV_ADV(m), t = NEXT(t) ) {
                   7430:     dltondl(n,t->dl,DL(m));
                   7431:     TD(DL(m)) = ndl_weight(DL(m));
                   7432:     CZ(m) = (Z)t->c;
                   7433:   }
                   7434:   qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
                   7435:   MKNDV(NV(p),m0,len,d);
                   7436:   SG(d) = SG(p);
                   7437:   return d;
                   7438: }
                   7439:
1.16      noro     7440: NDV dpmtondv(int mod,DPM p)
                   7441: {
                   7442:   NDV d;
                   7443:   NMV m,m0;
                   7444:   DMM t;
                   7445:   DMM *a;
                   7446:   int i,len,n;
                   7447:
                   7448:   if ( !p ) return 0;
                   7449:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   7450:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   7451:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   7452:   n = p->nv;
                   7453:   dmm_sort_by_ord(a,len,n);
                   7454:   if ( mod > 0 || mod == -1 )
                   7455:     m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   7456:   else
                   7457:     m0 = m = MALLOC(len*nmv_adv);
                   7458: #if 0
                   7459:   ndv_alloc += nmv_adv*len;
                   7460: #endif
                   7461:   for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   7462:     dltondl(n,a[i]->dl,DL(m));
                   7463:     MPOS(DL(m)) = a[i]->pos;
1.20      noro     7464:     TD(DL(m)) = ndl_weight(DL(m));
1.16      noro     7465:     CZ(m) = (Z)a[i]->c;
                   7466:   }
1.20      noro     7467:   qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16      noro     7468:   MKNDV(NV(p),m0,len,d);
                   7469:   SG(d) = SG(p);
                   7470:   return d;
                   7471: }
                   7472:
1.1       noro     7473: ND ndvtond(int mod,NDV p)
                   7474: {
                   7475:     ND d;
                   7476:     NM m,m0;
                   7477:     NMV t;
                   7478:     int i,len;
                   7479:
                   7480:     if ( !p ) return 0;
                   7481:     m0 = 0;
                   7482:     len = p->len;
                   7483:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   7484:         NEXTNM(m0,m);
                   7485:         ndl_copy(DL(t),DL(m));
1.6       noro     7486:         CZ(m) = CZ(t);
1.1       noro     7487:     }
                   7488:     NEXT(m) = 0;
                   7489:     MKND(NV(p),m0,len,d);
                   7490:     SG(d) = SG(p);
1.28      noro     7491:     d->sig = p->sig;
1.1       noro     7492:     return d;
                   7493: }
                   7494:
                   7495: DP ndvtodp(int mod,NDV p)
                   7496: {
                   7497:     MP m,m0;
                   7498:   DP d;
                   7499:     NMV t;
                   7500:     int i,len;
                   7501:
                   7502:     if ( !p ) return 0;
                   7503:     m0 = 0;
                   7504:     len = p->len;
                   7505:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   7506:         NEXTMP(m0,m);
                   7507:       m->dl = ndltodl(nd_nvar,DL(t));
                   7508:       m->c = (Obj)ndctop(mod,t->c);
                   7509:     }
                   7510:     NEXT(m) = 0;
                   7511:   MKDP(nd_nvar,m0,d);
                   7512:     SG(d) = SG(p);
                   7513:     return d;
                   7514: }
                   7515:
1.44      noro     7516: DPM sigtodpm(SIG s)
                   7517: {
                   7518:   DMM m;
                   7519:   DPM d;
                   7520:
                   7521:   NEWDMM(m);
                   7522:   m->c = (Obj)ONE;
                   7523:   m->dl = s->dl;
                   7524:   m->pos = s->pos+1;
                   7525:   m->next = 0;
                   7526:   MKDPM(nd_nvar,m,d);
                   7527:   SG(d) = s->dl->td;
                   7528:   return d;
                   7529: }
                   7530:
1.16      noro     7531: DPM ndvtodpm(int mod,NDV p)
                   7532: {
                   7533:   DMM m,m0;
                   7534:   DPM d;
                   7535:   NMV t;
                   7536:   int i,len;
                   7537:
                   7538:   if ( !p ) return 0;
                   7539:   m0 = 0;
                   7540:   len = p->len;
                   7541:   for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   7542:     NEXTDMM(m0,m);
                   7543:     m->dl = ndltodl(nd_nvar,DL(t));
                   7544:     m->c = (Obj)ndctop(mod,t->c);
                   7545:     m->pos = MPOS(DL(t));
                   7546:   }
                   7547:   NEXT(m) = 0;
                   7548:   MKDPM(nd_nvar,m0,d);
                   7549:   SG(d) = SG(p);
                   7550:   return d;
                   7551: }
                   7552:
                   7553:
1.1       noro     7554: DP ndtodp(int mod,ND p)
                   7555: {
1.65    ! noro     7556:   MP m,m0;
1.1       noro     7557:   DP d;
1.65    ! noro     7558:   NM t;
1.1       noro     7559:
1.65    ! noro     7560:   if ( !p ) return 0;
        !          7561:   m0 = 0;
        !          7562:   for ( t = BDY(p); t; t = NEXT(t) ) {
        !          7563:     NEXTMP(m0,m);
        !          7564:     m->dl = ndltodl(nd_nvar,DL(t));
        !          7565:     m->c = (Obj)ndctop(mod,t->c);
        !          7566:   }
        !          7567:   NEXT(m) = 0;
1.1       noro     7568:   MKDP(nd_nvar,m0,d);
1.65    ! noro     7569:   SG(d) = SG(p);
        !          7570:   return d;
1.1       noro     7571: }
                   7572:
                   7573: void ndv_print(NDV p)
                   7574: {
                   7575:     NMV m;
                   7576:     int i,len;
                   7577:
                   7578:     if ( !p ) printf("0\n");
                   7579:     else {
                   7580:         len = LEN(p);
                   7581:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   7582:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     7583:             else printf("+%ld*",CM(m));
1.1       noro     7584:             ndl_print(DL(m));
                   7585:         }
                   7586:         printf("\n");
                   7587:     }
                   7588: }
                   7589:
                   7590: void ndv_print_q(NDV p)
                   7591: {
                   7592:     NMV m;
                   7593:     int i,len;
                   7594:
                   7595:     if ( !p ) printf("0\n");
                   7596:     else {
                   7597:         len = LEN(p);
                   7598:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   7599:             printf("+");
1.6       noro     7600:             printexpr(CO,(Obj)CZ(m));
1.1       noro     7601:             printf("*");
                   7602:             ndl_print(DL(m));
                   7603:         }
                   7604:         printf("\n");
                   7605:     }
                   7606: }
                   7607:
                   7608: NODE ndv_reducebase(NODE x,int *perm)
                   7609: {
                   7610:     int len,i,j;
                   7611:     NDVI w;
                   7612:     NODE t,t0;
                   7613:
1.41      noro     7614:     if ( nd_norb ) return x;
1.1       noro     7615:     len = length(x);
                   7616:     w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
                   7617:     for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
                   7618:         w[i].p = BDY(t); w[i].i = perm[i];
                   7619:     }
                   7620:     for ( i = 0; i < len; i++ ) {
                   7621:         for ( j = 0; j < i; j++ ) {
                   7622:             if ( w[i].p && w[j].p ) {
                   7623:                 if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
                   7624:                 else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
                   7625:             }
                   7626:         }
                   7627:     }
                   7628:     for ( i = j = 0, t0 = 0; i < len; i++ ) {
                   7629:         if ( w[i].p ) {
                   7630:             NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
                   7631:             perm[j++] = w[i].i;
                   7632:         }
                   7633:     }
                   7634:     NEXT(t) = 0; x = t0;
                   7635:     return x;
                   7636: }
                   7637:
                   7638: /* XXX incomplete */
                   7639:
1.21      noro     7640: extern DMMstack dmm_stack;
                   7641: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16      noro     7642:
1.1       noro     7643: void nd_init_ord(struct order_spec *ord)
                   7644: {
                   7645:   nd_module = (ord->id >= 256);
                   7646:   if ( nd_module ) {
                   7647:     nd_dcomp = -1;
1.21      noro     7648:     nd_module_ordtype = ord->module_ordtype;
1.1       noro     7649:     nd_pot_nelim = ord->pot_nelim;
                   7650:     nd_poly_weight_len = ord->nv;
                   7651:     nd_poly_weight = ord->top_weight;
                   7652:     nd_module_rank = ord->module_rank;
                   7653:     nd_module_weight = ord->module_top_weight;
                   7654:   }
                   7655:   nd_matrix = 0;
                   7656:   nd_matrix_len = 0;
                   7657:     switch ( ord->id ) {
                   7658:         case 0:
                   7659:             switch ( ord->ord.simple ) {
                   7660:                 case 0:
                   7661:                     nd_dcomp = 1;
                   7662:                     nd_isrlex = 1;
                   7663:                     break;
                   7664:                 case 1:
                   7665:                     nd_dcomp = 1;
                   7666:                     nd_isrlex = 0;
                   7667:                     break;
                   7668:                 case 2:
                   7669:                     nd_dcomp = 0;
                   7670:                     nd_isrlex = 0;
                   7671:                     ndl_compare_function = ndl_lex_compare;
                   7672:                     break;
                   7673:                 case 11:
                   7674:                     /* XXX */
                   7675:                     nd_dcomp = 0;
                   7676:                     nd_isrlex = 1;
                   7677:                     ndl_compare_function = ndl_ww_lex_compare;
                   7678:                     break;
                   7679:                 default:
                   7680:                     error("nd_gr : unsupported order");
                   7681:             }
                   7682:             break;
                   7683:         case 1:
                   7684:             /* block order */
                   7685:             /* XXX */
                   7686:             nd_dcomp = -1;
                   7687:             nd_isrlex = 0;
                   7688:             ndl_compare_function = ndl_block_compare;
                   7689:             break;
                   7690:         case 2:
                   7691:             /* matrix order */
                   7692:             /* XXX */
                   7693:             nd_dcomp = -1;
                   7694:             nd_isrlex = 0;
                   7695:             nd_matrix_len = ord->ord.matrix.row;
                   7696:             nd_matrix = ord->ord.matrix.matrix;
                   7697:             ndl_compare_function = ndl_matrix_compare;
                   7698:             break;
                   7699:         case 3:
                   7700:             /* composite order */
                   7701:             nd_dcomp = -1;
                   7702:             nd_isrlex = 0;
                   7703:             nd_worb_len = ord->ord.composite.length;
                   7704:             nd_worb = ord->ord.composite.w_or_b;
                   7705:             ndl_compare_function = ndl_composite_compare;
                   7706:             break;
                   7707:
                   7708:         /* module order */
                   7709:         case 256:
                   7710:             switch ( ord->ord.simple ) {
                   7711:                 case 0:
1.21      noro     7712:                     nd_dcomp = 0;
1.1       noro     7713:                     nd_isrlex = 1;
1.21      noro     7714:                     ndl_compare_function = ndl_module_glex_compare;
1.1       noro     7715:                     break;
                   7716:                 case 1:
1.21      noro     7717:                     nd_dcomp = 0;
1.1       noro     7718:                     nd_isrlex = 0;
                   7719:                     ndl_compare_function = ndl_module_glex_compare;
                   7720:                     break;
                   7721:                 case 2:
1.21      noro     7722:                     nd_dcomp = 0;
1.1       noro     7723:                     nd_isrlex = 0;
1.21      noro     7724:                     ndl_compare_function = ndl_module_compare;
                   7725:                     ndl_base_compare_function = ndl_lex_compare;
1.1       noro     7726:                     break;
                   7727:                 default:
1.21      noro     7728:                     error("nd_init_ord : unsupported order");
1.1       noro     7729:             }
                   7730:             break;
                   7731:         case 257:
                   7732:             /* block order */
                   7733:             nd_isrlex = 0;
1.21      noro     7734:             ndl_compare_function = ndl_module_compare;
                   7735:             ndl_base_compare_function = ndl_block_compare;
1.1       noro     7736:             break;
                   7737:         case 258:
                   7738:             /* matrix order */
                   7739:             nd_isrlex = 0;
                   7740:             nd_matrix_len = ord->ord.matrix.row;
                   7741:             nd_matrix = ord->ord.matrix.matrix;
1.21      noro     7742:             ndl_compare_function = ndl_module_compare;
                   7743:             ndl_base_compare_function = ndl_matrix_compare;
1.1       noro     7744:             break;
                   7745:         case 259:
                   7746:             /* composite order */
                   7747:             nd_isrlex = 0;
                   7748:             nd_worb_len = ord->ord.composite.length;
                   7749:             nd_worb = ord->ord.composite.w_or_b;
1.21      noro     7750:             ndl_compare_function = ndl_module_compare;
                   7751:             ndl_base_compare_function = ndl_composite_compare;
                   7752:             break;
                   7753:         case 300:
                   7754:             /* schreyer order */
                   7755:             if ( ord->base->id != 256 )
                   7756:                error("nd_init_ord : unsupported base order");
                   7757:             ndl_compare_function = ndl_module_schreyer_compare;
                   7758:             dmm_stack = ord->dmmstack;
                   7759:             switch ( ord->base->ord.simple ) {
                   7760:                 case 0:
                   7761:                     nd_isrlex = 1;
                   7762:                     ndl_base_compare_function = ndl_glex_compare;
                   7763:                     dl_base_compare_function = cmpdl_revgradlex;
                   7764:                     break;
                   7765:                 case 1:
                   7766:                     nd_isrlex = 0;
                   7767:                     ndl_base_compare_function = ndl_glex_compare;
                   7768:                     dl_base_compare_function = cmpdl_gradlex;
                   7769:                     break;
                   7770:                 case 2:
                   7771:                     nd_isrlex = 0;
                   7772:                     ndl_base_compare_function = ndl_lex_compare;
                   7773:                     dl_base_compare_function = cmpdl_lex;
                   7774:                     break;
                   7775:                 default:
                   7776:                     error("nd_init_ord : unsupported order");
                   7777:             }
1.1       noro     7778:             break;
                   7779:     }
                   7780:     nd_ord = ord;
                   7781: }
                   7782:
                   7783: BlockMask nd_create_blockmask(struct order_spec *ord)
                   7784: {
                   7785:     int n,i,j,s,l;
                   7786:     UINT *t;
                   7787:     BlockMask bm;
                   7788:
                   7789:     /* we only create mask table for block order */
                   7790:     if ( ord->id != 1 && ord->id != 257 )
                   7791:         return 0;
                   7792:     n = ord->ord.block.length;
                   7793:     bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   7794:     bm->n = n;
                   7795:     bm->order_pair = ord->ord.block.order_pair;
                   7796:     bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
                   7797:     for ( i = 0, s = 0; i < n; i++ ) {
                   7798:         bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
                   7799:         for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   7800:         l = bm->order_pair[i].length;
                   7801:         for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   7802:     }
                   7803:     return bm;
                   7804: }
                   7805:
                   7806: EPOS nd_create_epos(struct order_spec *ord)
                   7807: {
                   7808:     int i,j,l,s,ord_l,ord_o;
                   7809:     EPOS epos;
                   7810:     struct order_pair *op;
                   7811:
                   7812:     epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   7813:     switch ( ord->id ) {
1.21      noro     7814:         case 0: case 256: case 300:
1.1       noro     7815:             if ( nd_isrlex ) {
                   7816:                 for ( i = 0; i < nd_nvar; i++ ) {
                   7817:                     epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   7818:                     epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   7819:                 }
                   7820:             } else {
                   7821:                 for ( i = 0; i < nd_nvar; i++ ) {
                   7822:                     epos[i].i = nd_exporigin + i/nd_epw;
                   7823:                     epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   7824:                 }
                   7825:             }
                   7826:             break;
                   7827:         case 1: case 257:
                   7828:             /* block order */
                   7829:             l = ord->ord.block.length;
                   7830:             op = ord->ord.block.order_pair;
                   7831:             for ( j = 0, s = 0; j < l; j++ ) {
                   7832:                 ord_o = op[j].order;
                   7833:                 ord_l = op[j].length;
                   7834:                 if ( !ord_o )
                   7835:                     for ( i = 0; i < ord_l; i++ ) {
                   7836:                         epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   7837:                         epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   7838:                     }
                   7839:                 else
                   7840:                     for ( i = 0; i < ord_l; i++ ) {
                   7841:                         epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   7842:                         epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   7843:                     }
                   7844:                 s += ord_l;
                   7845:             }
                   7846:             break;
                   7847:         case 2:
                   7848:             /* matrix order */
                   7849:         case 3:
                   7850:             /* composite order */
                   7851:         default:
                   7852:             for ( i = 0; i < nd_nvar; i++ ) {
                   7853:                 epos[i].i = nd_exporigin + i/nd_epw;
                   7854:                 epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   7855:             }
                   7856:             break;
                   7857:     }
                   7858:     return epos;
                   7859: }
                   7860:
                   7861: /* external interface */
                   7862:
                   7863: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
                   7864: {
                   7865:     NODE t,in0,in;
                   7866:     ND ndf,nf;
                   7867:     NDV ndvf;
                   7868:     VL vv,tv;
                   7869:     int stat,nvar,max,mrank;
                   7870:     Q cont;
                   7871:     P pp;
                   7872:     LIST ppl;
                   7873:
                   7874:     if ( !f ) {
                   7875:         *rp = 0;
                   7876:         return;
                   7877:     }
                   7878:     pltovl(v,&vv);
                   7879:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   7880:
                   7881:     /* max=65536 implies nd_bpe=32 */
                   7882:     max = 65536;
                   7883:
                   7884:   nd_module = 0;
                   7885:   /* nd_module will be set if ord is a module ordering */
                   7886:     nd_init_ord(ord);
                   7887:     nd_setup_parameters(nvar,max);
                   7888:     if ( nd_module && OID(f) != O_LIST )
                   7889:         error("nd_nf_p : the first argument must be a list");
                   7890:   if ( nd_module ) mrank = length(BDY((LIST)f));
                   7891:     /* conversion to ndv */
                   7892:     for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   7893:         NEXTNODE(in0,in);
                   7894:         if ( nd_module ) {
                   7895:           if ( !BDY(t) || OID(BDY(t)) != O_LIST
                   7896:                || length(BDY((LIST)BDY(t))) != mrank )
                   7897:               error("nd_nf_p : inconsistent basis element");
                   7898:           if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
                   7899:           else ppl = (LIST)BDY(t);
                   7900:           BDY(in) = (pointer)pltondv(CO,vv,ppl);
                   7901:         } else {
                   7902:           if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
                   7903:           else pp = (P)BDY(t);
                   7904:           BDY(in) = (pointer)ptondv(CO,vv,pp);
                   7905:         }
                   7906:         if ( m ) ndv_mod(m,(NDV)BDY(in));
                   7907:     }
                   7908:     if ( in0 ) NEXT(in) = 0;
                   7909:
                   7910:     if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
                   7911:     else ndvf = ptondv(CO,vv,(P)f);
                   7912:     if ( m ) ndv_mod(m,ndvf);
                   7913:     ndf = (pointer)ndvtond(m,ndvf);
                   7914:
                   7915:     /* dont sort, dont removecont */
1.24      noro     7916:     ndv_setup(m,0,in0,1,1,0);
1.1       noro     7917:     nd_scale=2;
1.6       noro     7918:     stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1       noro     7919:     if ( !stat )
                   7920:         error("nd_nf_p : exponent too large");
                   7921:     if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
                   7922:     else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
                   7923: }
                   7924:
                   7925: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   7926: {
                   7927:     NM m;
                   7928:     UINT *t,*s;
                   7929:     int i;
                   7930:
                   7931:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   7932:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7933:         t = DL(m);
                   7934:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   7935:         r[i] = CM(m);
                   7936:     }
                   7937:     for ( i = 0; !r[i]; i++ );
                   7938:     return i;
                   7939: }
                   7940:
                   7941: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
                   7942: {
                   7943:     NM m;
                   7944:     UINT *t,*s;
                   7945:     int i;
                   7946:
                   7947:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   7948:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7949:         t = DL(m);
                   7950:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     7951:         r[i] = CZ(m);
1.1       noro     7952:     }
                   7953:     for ( i = 0; !r[i]; i++ );
                   7954:     return i;
                   7955: }
                   7956:
                   7957: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
                   7958: {
                   7959:     NM m;
                   7960:     UINT *t,*s;
                   7961:     int i;
                   7962:
                   7963:     for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
                   7964:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7965:         t = DL(m);
                   7966:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   7967:         mpz_set(r[i],BDY(CZ(m)));
                   7968:     }
                   7969:     for ( i = 0; !mpz_sgn(r[i]); i++ );
                   7970:     return i;
                   7971: }
                   7972:
                   7973: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
                   7974: {
                   7975:     NM m;
                   7976:     unsigned long *v;
                   7977:     int i,j,h,size;
                   7978:   UINT *s,*t;
                   7979:
                   7980:   size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
                   7981:     v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
                   7982:     bzero(v,size);
                   7983:     for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
                   7984:     t = DL(m);
                   7985:     h = ndl_hash_value(t);
                   7986:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   7987:       v[i/BLEN] |= 1L <<(i%BLEN);
                   7988:     }
                   7989:     return v;
                   7990: }
                   7991:
                   7992: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
                   7993: {
                   7994:     NMV mr;
                   7995:     UINT *d,*t,*s;
                   7996:     int i,j,len,h,head;
                   7997:
                   7998:     d = DL(m);
                   7999:     len = LEN(p);
                   8000:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   8001:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   8002:         ndl_add(d,DL(mr),t);
                   8003:     h = ndl_hash_value(t);
                   8004:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   8005:     if ( j == 0 ) head = i;
                   8006:       v[i/BLEN] |= 1L <<(i%BLEN);
                   8007:     }
                   8008:     return head;
                   8009: }
                   8010:
                   8011: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   8012: {
                   8013:     NM m;
                   8014:     NMV mr;
                   8015:     UINT *d,*t,*s;
                   8016:     NDV p;
                   8017:     int i,j,len;
                   8018:     Z *r;
                   8019:
                   8020:     m = pair->mul;
                   8021:     d = DL(m);
                   8022:     p = nd_ps[pair->index];
                   8023:     len = LEN(p);
                   8024:     r = (Z *)CALLOC(n,sizeof(Q));
                   8025:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   8026:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   8027:         ndl_add(d,DL(mr),t);
                   8028:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     8029:         r[i] = CZ(mr);
1.1       noro     8030:     }
                   8031:     return r;
                   8032: }
                   8033:
1.11      noro     8034: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1       noro     8035: {
                   8036:     NM m;
                   8037:     NMV mr;
1.65    ! noro     8038:     UINT *d,*t,*u;
1.1       noro     8039:     NDV p;
                   8040:     unsigned char *ivc;
                   8041:     unsigned short *ivs;
1.65    ! noro     8042:     UINT *v,*ivi;
        !          8043:     int i,j,len,prev,diff,cdiff,st,ed,md,c;
1.1       noro     8044:     IndArray r;
                   8045:
                   8046:     m = pair->mul;
                   8047:     d = DL(m);
                   8048:     if ( trace )
                   8049:       p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
                   8050:     else
                   8051:       p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
                   8052:
                   8053:     len = LEN(p);
                   8054:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   8055:     v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11      noro     8056:     for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
                   8057:       ndl_add(d,DL(mr),t);
                   8058:       st = prev;
                   8059:       ed = n;
                   8060:       while ( ed > st ) {
                   8061:         md = (st+ed)/2;
                   8062:         u = s0+md*nd_wpd;
                   8063:         c = DL_COMPARE(u,t);
                   8064:         if ( c == 0 ) break;
                   8065:         else if ( c > 0 ) st = md;
                   8066:         else ed = md;
                   8067:       }
                   8068:       prev = v[j] = md;
1.1       noro     8069:     }
                   8070:     r = (IndArray)MALLOC(sizeof(struct oIndArray));
                   8071:     r->head = v[0];
                   8072:     diff = 0;
                   8073:     for ( i = 1; i < len; i++ ) {
                   8074:         cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   8075:     }
                   8076:     if ( diff < 256 ) {
                   8077:         r->width = 1;
                   8078:         ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   8079:         r->index.c = ivc;
                   8080:         for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   8081:     } else if ( diff < 65536 ) {
                   8082:         r->width = 2;
                   8083:         ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   8084:         r->index.s = ivs;
                   8085:         for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   8086:     } else {
                   8087:         r->width = 4;
                   8088:         ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   8089:         r->index.i = ivi;
                   8090:         for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
                   8091:     }
                   8092:     return r;
                   8093: }
                   8094:
                   8095: int compress_array(Z *svect,Z *cvect,int n)
                   8096: {
                   8097:     int i,j;
                   8098:
                   8099:     for ( i = j = 0; i < n; i++ )
                   8100:         if ( svect[i] ) cvect[j++] = svect[i];
                   8101:     return j;
                   8102: }
                   8103:
                   8104: void expand_array(Z *svect,Z *cvect,int n)
                   8105: {
                   8106:     int i,j;
                   8107:
                   8108:     for ( i = j = 0; j < n;  i++  )
                   8109:         if ( svect[i] ) svect[i] = cvect[j++];
                   8110: }
                   8111:
1.8       noro     8112: #if 0
1.1       noro     8113: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8114: {
                   8115:     int i,j,k,len,pos,prev,nz;
                   8116:     Z cs,mcs,c1,c2,cr,gcd,t;
                   8117:     IndArray ivect;
                   8118:     unsigned char *ivc;
                   8119:     unsigned short *ivs;
                   8120:     unsigned int *ivi;
                   8121:     NDV redv;
                   8122:     NMV mr;
                   8123:     NODE rp;
                   8124:     int maxrs;
                   8125:     double hmag;
                   8126:     Z *cvect;
1.3       noro     8127:     int l;
1.1       noro     8128:
                   8129:     maxrs = 0;
                   8130:     for ( i = 0; i < col && !svect[i]; i++ );
                   8131:     if ( i == col ) return maxrs;
                   8132:     hmag = p_mag((P)svect[i])*nd_scale;
                   8133:     cvect = (Z *)MALLOC(col*sizeof(Q));
                   8134:     for ( i = 0; i < nred; i++ ) {
                   8135:         ivect = imat[i];
                   8136:         k = ivect->head;
                   8137:         if ( svect[k] ) {
                   8138:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8139:             redv = nd_demand?ndv_load(rp0[i]->index)
                   8140:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   8141:             len = LEN(redv); mr = BDY(redv);
1.6       noro     8142:             igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1       noro     8143:             chsgnz(cs,&mcs);
                   8144:             if ( !UNIQ(cr) ) {
                   8145:                 for ( j = 0; j < col; j++ ) {
                   8146:                     mulz(svect[j],cr,&c1); svect[j] = c1;
                   8147:                 }
                   8148:             }
                   8149:             svect[k] = 0; prev = k;
                   8150:             switch ( ivect->width ) {
                   8151:                 case 1:
                   8152:                     ivc = ivect->index.c;
                   8153:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8154:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     8155:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     8156:                     }
                   8157:                     break;
                   8158:                 case 2:
                   8159:                     ivs = ivect->index.s;
                   8160:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8161:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     8162:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     8163:                     }
                   8164:                     break;
                   8165:                 case 4:
                   8166:                     ivi = ivect->index.i;
                   8167:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8168:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     8169:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     8170:                     }
                   8171:                     break;
                   8172:             }
                   8173:             for ( j = k+1; j < col && !svect[j]; j++ );
                   8174:             if ( j == col ) break;
                   8175:             if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
                   8176:                 nz = compress_array(svect,cvect,col);
                   8177:                 removecont_array((P *)cvect,nz,1);
                   8178:                 expand_array(svect,cvect,nz);
                   8179:                 hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   8180:             }
                   8181:         }
                   8182:     }
                   8183:     nz = compress_array(svect,cvect,col);
                   8184:     removecont_array((P *)cvect,nz,1);
                   8185:     expand_array(svect,cvect,nz);
                   8186:     if ( DP_Print ) {
                   8187:         fprintf(asir_out,"-"); fflush(asir_out);
                   8188:     }
                   8189:     return maxrs;
                   8190: }
1.4       noro     8191: #else
1.9       noro     8192:
1.4       noro     8193: /* direct mpz version */
                   8194: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8195: {
                   8196:     int i,j,k,len,pos,prev;
                   8197:     mpz_t cs,cr,gcd;
                   8198:     IndArray ivect;
                   8199:     unsigned char *ivc;
                   8200:     unsigned short *ivs;
                   8201:     unsigned int *ivi;
                   8202:     NDV redv;
                   8203:     NMV mr;
                   8204:     int maxrs;
                   8205:     double hmag;
1.13      noro     8206:     static mpz_t *svect;
                   8207:     static int svect_len=0;
1.4       noro     8208:
                   8209:     maxrs = 0;
                   8210:     for ( i = 0; i < col && !svect0[i]; i++ );
                   8211:     if ( i == col ) return maxrs;
                   8212:     hmag = p_mag((P)svect0[i])*nd_scale;
1.13      noro     8213:     if ( col > svect_len ) {
                   8214:       svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   8215:       svect_len = col;
                   8216:     }
1.4       noro     8217:     for ( i = 0; i < col; i++ ) {
                   8218:       mpz_init(svect[i]);
                   8219:       if ( svect0[i] )
                   8220:         mpz_set(svect[i],BDY(svect0[i]));
                   8221:       else
                   8222:         mpz_set_ui(svect[i],0);
                   8223:     }
                   8224:     mpz_init(gcd); mpz_init(cs); mpz_init(cr);
                   8225:     for ( i = 0; i < nred; i++ ) {
                   8226:         ivect = imat[i];
                   8227:         k = ivect->head;
                   8228:         if ( mpz_sgn(svect[k]) ) {
                   8229:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8230:             redv = nd_demand?ndv_load(rp0[i]->index)
                   8231:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   8232:             len = LEN(redv); mr = BDY(redv);
1.6       noro     8233:             mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4       noro     8234:             mpz_div(cs,svect[k],gcd);
1.6       noro     8235:             mpz_div(cr,BDY(CZ(mr)),gcd);
1.4       noro     8236:             mpz_neg(cs,cs);
1.9       noro     8237:             if ( MUNIMPZ(cr) )
                   8238:               for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
                   8239:             else if ( !UNIMPZ(cr) )
                   8240:               for ( j = 0; j < col; j++ ) {
                   8241:                 if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
                   8242:               }
1.4       noro     8243:             mpz_set_ui(svect[k],0);
                   8244:             prev = k;
                   8245:             switch ( ivect->width ) {
                   8246:                 case 1:
                   8247:                     ivc = ivect->index.c;
                   8248:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8249:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     8250:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     8251:                     }
                   8252:                     break;
                   8253:                 case 2:
                   8254:                     ivs = ivect->index.s;
                   8255:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8256:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     8257:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     8258:                     }
                   8259:                     break;
                   8260:                 case 4:
                   8261:                     ivi = ivect->index.i;
                   8262:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8263:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     8264:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     8265:                     }
                   8266:                     break;
                   8267:             }
                   8268:             for ( j = k+1; j < col && !svect[j]; j++ );
                   8269:             if ( j == col ) break;
                   8270:             if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
                   8271:                 mpz_removecont_array(svect,col);
                   8272:                 hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
                   8273:             }
                   8274:         }
                   8275:     }
                   8276:     mpz_removecont_array(svect,col);
                   8277:     if ( DP_Print ) {
                   8278:         fprintf(asir_out,"-"); fflush(asir_out);
                   8279:     }
                   8280:     for ( i = 0; i < col; i++ )
                   8281:       if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
                   8282:       else svect0[i] = 0;
                   8283:     return maxrs;
                   8284: }
                   8285: #endif
1.1       noro     8286:
1.40      noro     8287: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred,SIG sig)
1.1       noro     8288: {
                   8289:     int i,j,k,len,pos,prev;
                   8290:     UINT c,c1,c2,c3,up,lo,dmy;
                   8291:     IndArray ivect;
                   8292:     unsigned char *ivc;
                   8293:     unsigned short *ivs;
                   8294:     unsigned int *ivi;
                   8295:     NDV redv;
                   8296:     NMV mr;
                   8297:     int maxrs;
                   8298:
                   8299:     maxrs = 0;
                   8300:     for ( i = 0; i < nred; i++ ) {
                   8301:         ivect = imat[i];
                   8302:         k = ivect->head; svect[k] %= m;
1.40      noro     8303:         if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.1       noro     8304:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8305:             c = m-c; redv = nd_ps[rp0[i]->index];
                   8306:             len = LEN(redv); mr = BDY(redv);
                   8307:             svect[k] = 0; prev = k;
                   8308:             switch ( ivect->width ) {
                   8309:                 case 1:
                   8310:                     ivc = ivect->index.c;
                   8311:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8312:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.40      noro     8313:                         if ( c1 ) {
                   8314:                           c2 = svect[pos];
1.1       noro     8315:                           DMA(c1,c,c2,up,lo);
                   8316:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   8317:                           } else svect[pos] = lo;
1.40      noro     8318:                         }
1.1       noro     8319:                     }
                   8320:                     break;
                   8321:                 case 2:
                   8322:                     ivs = ivect->index.s;
                   8323:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8324:                         pos = prev+ivs[j]; c1 = CM(mr);
                   8325:                         prev = pos;
1.40      noro     8326:                         if ( c1 ) {
                   8327:                           c2 = svect[pos];
1.1       noro     8328:                           DMA(c1,c,c2,up,lo);
                   8329:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   8330:                           } else svect[pos] = lo;
1.40      noro     8331:                         }
1.1       noro     8332:                     }
                   8333:                     break;
                   8334:                 case 4:
                   8335:                     ivi = ivect->index.i;
                   8336:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8337:                         pos = prev+ivi[j]; c1 = CM(mr);
                   8338:                         prev = pos;
1.40      noro     8339:                         if ( c1 ) {
                   8340:                           c2 = svect[pos];
1.1       noro     8341:                           DMA(c1,c,c2,up,lo);
                   8342:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   8343:                           } else svect[pos] = lo;
1.40      noro     8344:                         }
1.1       noro     8345:                     }
                   8346:                     break;
                   8347:             }
                   8348:         }
                   8349:     }
                   8350:     for ( i = 0; i < col; i++ )
                   8351:         if ( svect[i] >= (UINT)m ) svect[i] %= m;
                   8352:     return maxrs;
                   8353: }
                   8354:
                   8355: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8356: {
                   8357:     int i,j,k,len,pos,prev;
1.65    ! noro     8358:     UINT c;
1.1       noro     8359:     IndArray ivect;
                   8360:     unsigned char *ivc;
                   8361:     unsigned short *ivs;
                   8362:     unsigned int *ivi;
                   8363:     NDV redv;
                   8364:     NMV mr;
                   8365:     int maxrs;
                   8366:
                   8367:     maxrs = 0;
                   8368:     for ( i = 0; i < nred; i++ ) {
                   8369:         ivect = imat[i];
                   8370:         k = ivect->head;
                   8371:         if ( (c = svect[k]) != 0 ) {
                   8372:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8373:             c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
                   8374:             len = LEN(redv); mr = BDY(redv);
                   8375:             svect[k] = 0; prev = k;
                   8376:             switch ( ivect->width ) {
                   8377:                 case 1:
                   8378:                     ivc = ivect->index.c;
                   8379:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8380:                         pos = prev+ivc[j]; prev = pos;
                   8381:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   8382:                     }
                   8383:                     break;
                   8384:                 case 2:
                   8385:                     ivs = ivect->index.s;
                   8386:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8387:                         pos = prev+ivs[j]; prev = pos;
                   8388:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   8389:                     }
                   8390:                     break;
                   8391:                 case 4:
                   8392:                     ivi = ivect->index.i;
                   8393:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8394:                         pos = prev+ivi[j]; prev = pos;
                   8395:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   8396:                     }
                   8397:                     break;
                   8398:             }
                   8399:         }
                   8400:     }
                   8401:     return maxrs;
                   8402: }
                   8403:
                   8404: ND nd_add_lf(ND p1,ND p2)
                   8405: {
1.65    ! noro     8406:     int c,can;
1.1       noro     8407:     NM m1,m2,mr0,mr,s;
                   8408:     Z t;
                   8409:
                   8410:     if ( !p1 ) return p2;
                   8411:     else if ( !p2 ) return p1;
                   8412:     else {
                   8413:         can = 0;
1.65    ! noro     8414:         for (  m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1.1       noro     8415:             c = DL_COMPARE(DL(m1),DL(m2));
                   8416:             switch ( c ) {
                   8417:                 case 0:
                   8418:                     addlf(CZ(m1),CZ(m2),&t);
                   8419:                     s = m1; m1 = NEXT(m1);
                   8420:                     if ( t ) {
                   8421:                         can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
                   8422:                     } else {
                   8423:                         can += 2; FREENM(s);
                   8424:                     }
                   8425:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   8426:                     break;
                   8427:                 case 1:
                   8428:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   8429:                     break;
                   8430:                 case -1:
                   8431:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   8432:                     break;
                   8433:             }
                   8434:         }
                   8435:         if ( !mr0 )
                   8436:             if ( m1 ) mr0 = m1;
                   8437:             else if ( m2 ) mr0 = m2;
                   8438:             else return 0;
                   8439:         else if ( m1 ) NEXT(mr) = m1;
                   8440:         else if ( m2 ) NEXT(mr) = m2;
                   8441:         else NEXT(mr) = 0;
                   8442:         BDY(p1) = mr0;
                   8443:         SG(p1) = MAX(SG(p1),SG(p2));
                   8444:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   8445:         FREEND(p2);
                   8446:         return p1;
                   8447:     }
                   8448: }
                   8449:
                   8450: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8451: {
                   8452:     int i,j,k,len,pos,prev;
                   8453:     IndArray ivect;
                   8454:     unsigned char *ivc;
                   8455:     unsigned short *ivs;
                   8456:     unsigned int *ivi;
                   8457:     NDV redv;
                   8458:     NMV mr;
                   8459:     int maxrs;
                   8460:
                   8461:     maxrs = 0;
                   8462:     lf_lazy = 1;
                   8463:     for ( i = 0; i < nred; i++ ) {
                   8464:         ivect = imat[i];
                   8465:         k = ivect->head;
                   8466:         mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
                   8467:         if ( mpz_sgn(svect[k]) ) {
                   8468:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8469:             mpz_neg(svect[k],svect[k]);
                   8470:             redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
                   8471:             len = LEN(redv); mr = BDY(redv);
                   8472:             prev = k;
                   8473:             switch ( ivect->width ) {
                   8474:                 case 1:
                   8475:                     ivc = ivect->index.c;
                   8476:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8477:                         pos = prev+ivc[j]; prev = pos;
                   8478:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   8479:                     }
                   8480:                     break;
                   8481:                 case 2:
                   8482:                     ivs = ivect->index.s;
                   8483:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8484:                         pos = prev+ivs[j]; prev = pos;
                   8485:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   8486:                     }
                   8487:                     break;
                   8488:                 case 4:
                   8489:                     ivi = ivect->index.i;
                   8490:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8491:                         pos = prev+ivi[j]; prev = pos;
                   8492:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   8493:                     }
                   8494:                     break;
                   8495:             }
                   8496:             mpz_set_ui(svect[k],0);
                   8497:         }
                   8498:     }
                   8499:     lf_lazy=0;
                   8500:     for ( i = 0; i < col; i++ ) {
                   8501:         mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
                   8502:     }
                   8503:     return maxrs;
                   8504: }
                   8505:
                   8506: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
                   8507: {
                   8508:     int i,j,k,l,rank,s;
1.65    ! noro     8509:     mpz_t a,inv;
1.1       noro     8510:     mpz_t *t,*pivot,*pk;
                   8511:     mpz_t **mat;
                   8512:
                   8513:     mpz_init(inv);
                   8514:     mpz_init(a);
                   8515:     mat = (mpz_t **)mat0;
                   8516:     for ( rank = 0, j = 0; j < col; j++ ) {
                   8517:         for ( i = rank; i < row; i++ ) {
                   8518:             mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
                   8519:         }
                   8520:         for ( i = rank; i < row; i++ )
                   8521:             if ( mpz_sgn(mat[i][j]) )
                   8522:                 break;
                   8523:         if ( i == row ) {
                   8524:             colstat[j] = 0;
                   8525:             continue;
                   8526:         } else
                   8527:             colstat[j] = 1;
                   8528:         if ( i != rank ) {
                   8529:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   8530:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   8531:         }
                   8532:         pivot = mat[rank];
                   8533:         s = sugar[rank];
                   8534:         mpz_invert(inv,pivot[j],BDY(current_mod_lf));
                   8535:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   8536:             if ( mpz_sgn(*pk) ) {
                   8537:                 mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
                   8538:             }
                   8539:         for ( i = rank+1; i < row; i++ ) {
                   8540:             t = mat[i];
                   8541:             if ( mpz_sgn(t[j]) ) {
                   8542:                 sugar[i] = MAX(sugar[i],s);
                   8543:                 mpz_neg(a,t[j]);
                   8544:                 red_by_vect_lf(t+j,pivot+j,a,col-j);
                   8545:             }
                   8546:         }
                   8547:         rank++;
                   8548:     }
                   8549:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   8550:         if ( colstat[j] ) {
                   8551:             pivot = mat[l];
                   8552:             s = sugar[l];
                   8553:             for ( k = j; k < col; k++ )
                   8554:               mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
                   8555:             for ( i = 0; i < l; i++ ) {
                   8556:                 t = mat[i];
                   8557:                 if ( mpz_sgn(t[j]) ) {
                   8558:                     sugar[i] = MAX(sugar[i],s);
                   8559:                     mpz_neg(a,t[j]);
                   8560:                     red_by_vect_lf(t+j,pivot+j,a,col-j);
                   8561:                 }
                   8562:             }
                   8563:             l--;
                   8564:         }
                   8565:     for ( j = 0, l = 0; l < rank; j++ )
                   8566:         if ( colstat[j] ) {
                   8567:             t = mat[l];
                   8568:             for ( k = j; k < col; k++ ) {
                   8569:                 mpz_mod(t[k],t[k],BDY(current_mod_lf));
                   8570:             }
                   8571:             l++;
                   8572:         }
                   8573:     return rank;
                   8574: }
                   8575:
                   8576:
                   8577: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   8578: {
                   8579:     int j,k,len;
                   8580:     UINT *p;
                   8581:     UINT c;
                   8582:     NDV r;
                   8583:     NMV mr0,mr;
                   8584:
                   8585:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   8586:     if ( !len ) return 0;
                   8587:     else {
                   8588:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8589: #if 0
                   8590:         ndv_alloc += nmv_adv*len;
                   8591: #endif
                   8592:         mr = mr0;
                   8593:         p = s0vect;
                   8594:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   8595:             if ( !rhead[j] ) {
                   8596:                 if ( (c = vect[k++]) != 0 ) {
                   8597:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   8598:                 }
                   8599:             }
                   8600:         MKNDV(nd_nvar,mr0,len,r);
                   8601:         return r;
                   8602:     }
                   8603: }
                   8604:
1.40      noro     8605: NDV vect_to_ndv_s(UINT *vect,int col,UINT *s0vect)
                   8606: {
                   8607:     int j,k,len;
                   8608:     UINT *p;
                   8609:     UINT c;
                   8610:     NDV r;
                   8611:     NMV mr0,mr;
                   8612:
                   8613:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   8614:     if ( !len ) return 0;
                   8615:     else {
                   8616:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8617:         mr = mr0;
                   8618:         p = s0vect;
                   8619:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   8620:           if ( (c = vect[k++]) != 0 ) {
                   8621:             ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   8622:           }
                   8623:         MKNDV(nd_nvar,mr0,len,r);
                   8624:         return r;
                   8625:     }
                   8626: }
                   8627:
1.1       noro     8628: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
                   8629: {
1.65    ! noro     8630:     int j,len;
1.1       noro     8631:     UINT *p;
                   8632:     NDV r;
                   8633:     NMV mr0,mr;
                   8634:
                   8635:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
                   8636:     if ( !len ) return 0;
                   8637:     else {
                   8638:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8639:         mr = mr0;
                   8640:         p = s0vect;
                   8641:         for ( j = 0; j < col; j++, p += nd_wpd )
                   8642:       if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
                   8643:             ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
                   8644:           }
                   8645:         MKNDV(nd_nvar,mr0,len,r);
                   8646:         return r;
                   8647:     }
                   8648: }
                   8649:
                   8650: /* for preprocessed vector */
                   8651:
                   8652: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   8653: {
1.6       noro     8654:   int j,k,len;
                   8655:   UINT *p;
                   8656:   Z c;
                   8657:   NDV r;
                   8658:   NMV mr0,mr;
1.1       noro     8659:
1.6       noro     8660:   for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   8661:   if ( !len ) return 0;
                   8662:   else {
                   8663:     mr0 = (NMV)MALLOC(nmv_adv*len);
1.1       noro     8664: #if 0
1.6       noro     8665:     ndv_alloc += nmv_adv*len;
1.1       noro     8666: #endif
1.6       noro     8667:     mr = mr0;
                   8668:     p = s0vect;
                   8669:     for ( j = k = 0; j < col; j++, p += nd_wpd ) {
                   8670:       if ( !rhead[j] ) {
                   8671:         if ( (c = vect[k++]) != 0 ) {
                   8672:           if ( !INT(c) )
                   8673:             error("vect_to_ndv_q : components must be integers");
1.65    ! noro     8674:           ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.6       noro     8675:         }
                   8676:       }
1.1       noro     8677:     }
1.6       noro     8678:     MKNDV(nd_nvar,mr0,len,r);
                   8679:     return r;
                   8680:   }
1.1       noro     8681: }
                   8682:
                   8683: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   8684: {
                   8685:     int j,k,len;
                   8686:     UINT *p;
                   8687:     mpz_t c;
                   8688:     NDV r;
                   8689:     NMV mr0,mr;
                   8690:
                   8691:     for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
                   8692:     if ( !len ) return 0;
                   8693:     else {
                   8694:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   8695: #if 0
                   8696:         ndv_alloc += nmv_adv*len;
                   8697: #endif
                   8698:         mr = mr0;
                   8699:         p = s0vect;
                   8700:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   8701:             if ( !rhead[j] ) {
                   8702:                 c[0] = vect[k++][0];
                   8703:                 if ( mpz_sgn(c) ) {
                   8704:                     ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
                   8705:                 }
                   8706:             }
                   8707:         MKNDV(nd_nvar,mr0,len,r);
                   8708:         return r;
                   8709:     }
                   8710: }
                   8711:
                   8712: /* for plain vector */
                   8713:
                   8714: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
                   8715: {
                   8716:     int j,k,len;
                   8717:     UINT *p;
                   8718:     Z c;
                   8719:     NDV r;
                   8720:     NMV mr0,mr;
                   8721:
                   8722:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   8723:     if ( !len ) return 0;
                   8724:     else {
                   8725:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   8726: #if 0
                   8727:         ndv_alloc += nmv_adv*len;
                   8728: #endif
                   8729:         mr = mr0;
                   8730:         p = s0vect;
                   8731:         for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   8732:             if ( (c = vect[k]) != 0 ) {
                   8733:                 if ( !INT(c) )
1.6       noro     8734:                     error("plain_vect_to_ndv_q : components must be integers");
                   8735:                 ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1       noro     8736:             }
                   8737:         MKNDV(nd_nvar,mr0,len,r);
                   8738:         return r;
                   8739:     }
                   8740: }
                   8741:
                   8742: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
                   8743: {
                   8744:     ND_pairs t;
                   8745:     int stat;
                   8746:     ND spol;
                   8747:
                   8748:     for ( t = l; t; t = NEXT(t) ) {
                   8749:         stat = nd_sp(m,trace,t,&spol);
                   8750:         if ( !stat ) return 0;
                   8751:         if ( spol ) {
                   8752:             add_pbucket_symbolic(bucket,spol);
                   8753:         }
                   8754:     }
                   8755:     return 1;
                   8756: }
                   8757:
                   8758: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   8759: {
                   8760:     NODE rp0,rp;
                   8761:     NM mul,head,s0,s;
                   8762:     int index,col,i,sugar;
                   8763:     RHist h;
                   8764:     UINT *s0v,*p;
                   8765:     NM_ind_pair pair;
                   8766:     ND red;
                   8767:     NDV *ps;
                   8768:
                   8769:     s0 = 0; rp0 = 0; col = 0;
                   8770:   if ( nd_demand )
                   8771:       ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   8772:   else
                   8773:       ps = trace?nd_ps_trace:nd_ps;
                   8774:     while ( 1 ) {
                   8775:         head = remove_head_pbucket_symbolic(bucket);
                   8776:         if ( !head ) break;
                   8777:         if ( !s0 ) s0 = head;
                   8778:         else NEXT(s) = head;
                   8779:         s = head;
                   8780:         index = ndl_find_reducer(DL(head));
                   8781:         if ( index >= 0 ) {
                   8782:             h = nd_psh[index];
                   8783:             NEWNM(mul);
                   8784:             ndl_sub(DL(head),DL(h),DL(mul));
                   8785:             if ( ndl_check_bound2(index,DL(mul)) )
                   8786:                 return 0;
                   8787:             sugar = TD(DL(mul))+SG(ps[index]);
1.28      noro     8788:             MKNM_ind_pair(pair,mul,index,sugar,0);
1.1       noro     8789:             red = ndv_mul_nm_symbolic(mul,ps[index]);
                   8790:             add_pbucket_symbolic(bucket,nd_remove_head(red));
                   8791:             NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   8792:         }
                   8793:         col++;
                   8794:     }
                   8795:     if ( rp0 ) NEXT(rp) = 0;
                   8796:     NEXT(s) = 0;
                   8797:     s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   8798:     for ( i = 0, p = s0v, s = s0; i < col;
                   8799:         i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   8800:     *s0vect = s0v;
                   8801:     *r = rp0;
                   8802:     return col;
                   8803: }
                   8804:
                   8805: void print_ndp(ND_pairs l)
                   8806: {
                   8807:   ND_pairs t;
                   8808:
                   8809:   for ( t = l; t; t = NEXT(t) )
                   8810:     printf("[%d,%d] ",t->i1,t->i2);
                   8811:   printf("\n");
                   8812: }
                   8813:
                   8814: NODE nd_f4(int m,int checkonly,int **indp)
                   8815: {
1.65    ! noro     8816:   int i,nh,stat,f4red;
        !          8817:   NODE r,g,tn0,tn,node;
        !          8818:   ND_pairs d,l,t,ll,lh;
1.1       noro     8819:   LIST l0,l1;
1.65    ! noro     8820:   NDV nf;
        !          8821:   NODE rp0,nflist,nzlist,nzlist_t;
        !          8822:   int col;
        !          8823:   UINT *s0vect;
        !          8824:   int sugar;
        !          8825:   PGeoBucket bucket;
        !          8826:   struct oEGT eg0,eg1,eg_f4;
        !          8827:   Z i1,i2,sugarq;
1.12      noro     8828:
1.65    ! noro     8829:   init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1       noro     8830: #if 0
1.65    ! noro     8831:   ndv_alloc = 0;
1.1       noro     8832: #endif
1.65    ! noro     8833:   Nf4_red=0;
        !          8834:   g = 0; d = 0;
        !          8835:   for ( i = 0; i < nd_psn; i++ ) {
        !          8836:     d = update_pairs(d,g,i,0);
        !          8837:     g = update_base(g,i);
        !          8838:   }
        !          8839:   nzlist = 0;
        !          8840:   nzlist_t = nd_nzlist;
        !          8841:   f4red = 1;
        !          8842:   nd_last_nonzero = 0;
        !          8843:   while ( d ) {
        !          8844:     get_eg(&eg0);
        !          8845:     l = nd_minsugarp(d,&d);
        !          8846:     sugar = nd_sugarweight?l->sugar2:SG(l);
        !          8847:     if ( MaxDeg > 0 && sugar > MaxDeg ) break;
        !          8848:     if ( nzlist_t ) {
        !          8849:       node = BDY((LIST)BDY(nzlist_t));
        !          8850: //      sugarh = (int)ZTOS((Q)ARG0(node));
        !          8851:       tn = BDY((LIST)ARG1(node));
        !          8852:       if ( !tn ) {
        !          8853:         nzlist_t = NEXT(nzlist_t);
        !          8854:         continue;
        !          8855:       }
        !          8856:       /* tn = [[i1,i2],...] */
        !          8857:           lh = nd_ipairtospair(tn);
        !          8858:     }
        !          8859:     bucket = create_pbucket();
        !          8860:     stat = nd_sp_f4(m,0,l,bucket);
        !          8861:     if ( !stat ) {
        !          8862:       for ( t = l; NEXT(t); t = NEXT(t) );
        !          8863:       NEXT(t) = d; d = l;
        !          8864:       d = nd_reconstruct(0,d);
        !          8865:       continue;
        !          8866:     }
        !          8867:     if ( bucket->m < 0 ) continue;
        !          8868:     col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
        !          8869:     if ( !col ) {
        !          8870:       for ( t = l; NEXT(t); t = NEXT(t) );
        !          8871:       NEXT(t) = d; d = l;
        !          8872:       d = nd_reconstruct(0,d);
        !          8873:       continue;
1.1       noro     8874:     }
1.65    ! noro     8875:     get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
        !          8876:     if ( DP_Print )
        !          8877:       fprintf(asir_out,"sugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
        !          8878:     nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
        !          8879:     if ( checkonly && nflist ) return 0;
        !          8880:     /* adding new bases */
        !          8881:     if ( nflist ) nd_last_nonzero = f4red;
        !          8882:     for ( r = nflist; r; r = NEXT(r) ) {
        !          8883:       nf = (NDV)BDY(r);
        !          8884:       if ( nd_f4_td ) SG(nf) = nd_tdeg(nf);
        !          8885:       ndv_removecont(m,nf);
        !          8886:       if ( !m && nd_nalg ) {
        !          8887:         ND nf1;
1.1       noro     8888:
1.65    ! noro     8889:         nf1 = ndvtond(m,nf);
        !          8890:         nd_monic(0,&nf1);
        !          8891:         nd_removecont(m,nf1);
        !          8892:         nf = ndtondv(m,nf1);
        !          8893:       }
        !          8894:       nh = ndv_newps(m,nf,0);
        !          8895:       d = update_pairs(d,g,nh,0);
        !          8896:       g = update_base(g,nh);
        !          8897:     }
        !          8898:     if ( DP_Print ) {
        !          8899:       fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
        !          8900:     }
        !          8901:     if ( nd_gentrace ) {
1.1       noro     8902:       for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
                   8903:         NEXTNODE(tn0,tn);
1.65    ! noro     8904:         STOZ(t->i1,i1); STOZ(t->i2,i2);
        !          8905:         node = mknode(2,i1,i2); MKLIST(l0,node);
1.1       noro     8906:         BDY(tn) = l0;
                   8907:       }
                   8908:       if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.65    ! noro     8909:         STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
        !          8910:         MKNODE(node,l1,nzlist); nzlist = node;
1.1       noro     8911:     }
1.65    ! noro     8912:     if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
        !          8913:     f4red++;
        !          8914:     if ( nd_f4red && f4red > nd_f4red ) break;
        !          8915:     if ( nd_rank0 && !nflist ) break;
        !          8916:   }
        !          8917:   if ( nd_gentrace ) {
        !          8918:   MKLIST(l0,reverse_node(nzlist));
        !          8919:       MKNODE(nd_alltracelist,l0,0);
        !          8920:   }
1.1       noro     8921: #if 0
1.65    ! noro     8922:   fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.1       noro     8923: #endif
1.12      noro     8924:   if ( DP_Print ) {
1.65    ! noro     8925:   fprintf(asir_out,"number of red=%d,",Nf4_red);
        !          8926:   fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
        !          8927:     f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
        !          8928:   fprintf(asir_out,"number of removed pairs=%d\n,",NcriB+NcriMF+Ncri2);
1.12      noro     8929:   }
1.1       noro     8930:   conv_ilist(nd_demand,0,g,indp);
1.65    ! noro     8931:   return g;
1.1       noro     8932: }
                   8933:
                   8934: NODE nd_f4_trace(int m,int **indp)
                   8935: {
1.65    ! noro     8936:     int i,nh,stat;
1.1       noro     8937:     NODE r,g;
                   8938:     ND_pairs d,l,l0,t;
1.65    ! noro     8939:     NDV nfqv,nfv;
        !          8940:     NODE rp0,nflist;
        !          8941:     int col;
        !          8942:     UINT *s0vect;
1.1       noro     8943:     int sugar;
                   8944:     PGeoBucket bucket;
                   8945:     struct oEGT eg0,eg1,eg_f4;
                   8946:
                   8947:     g = 0; d = 0;
                   8948:     for ( i = 0; i < nd_psn; i++ ) {
                   8949:         d = update_pairs(d,g,i,0);
                   8950:         g = update_base(g,i);
                   8951:     }
                   8952:     while ( d ) {
                   8953:         get_eg(&eg0);
                   8954:         l = nd_minsugarp(d,&d);
                   8955:         sugar = SG(l);
                   8956:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   8957:         bucket = create_pbucket();
                   8958:         stat = nd_sp_f4(m,0,l,bucket);
                   8959:         if ( !stat ) {
                   8960:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8961:             NEXT(t) = d; d = l;
                   8962:             d = nd_reconstruct(1,d);
                   8963:             continue;
                   8964:         }
                   8965:         if ( bucket->m < 0 ) continue;
                   8966:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   8967:         if ( !col ) {
                   8968:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8969:             NEXT(t) = d; d = l;
                   8970:             d = nd_reconstruct(1,d);
                   8971:             continue;
                   8972:         }
                   8973:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   8974:         if ( DP_Print )
1.3       noro     8975:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5       noro     8976:                 sugar,eg_f4.exectime);
1.1       noro     8977:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   8978:         if ( !l0 ) continue;
                   8979:         l = l0;
                   8980:
                   8981:         /* over Q */
                   8982:         bucket = create_pbucket();
                   8983:         stat = nd_sp_f4(0,1,l,bucket);
                   8984:         if ( !stat ) {
                   8985:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8986:             NEXT(t) = d; d = l;
                   8987:             d = nd_reconstruct(1,d);
                   8988:             continue;
                   8989:         }
                   8990:         if ( bucket->m < 0 ) continue;
                   8991:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   8992:         if ( !col ) {
                   8993:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8994:             NEXT(t) = d; d = l;
                   8995:             d = nd_reconstruct(1,d);
                   8996:             continue;
                   8997:         }
                   8998:         nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   8999:         /* adding new bases */
                   9000:         for ( r = nflist; r; r = NEXT(r) ) {
                   9001:             nfqv = (NDV)BDY(r);
                   9002:             ndv_removecont(0,nfqv);
1.6       noro     9003:             if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1       noro     9004:             if ( nd_nalg ) {
                   9005:                 ND nf1;
                   9006:
                   9007:                 nf1 = ndvtond(m,nfqv);
                   9008:                 nd_monic(0,&nf1);
                   9009:                 nd_removecont(0,nf1);
                   9010:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   9011:             }
                   9012:             nfv = ndv_dup(0,nfqv);
                   9013:             ndv_mod(m,nfv);
                   9014:             ndv_removecont(m,nfv);
1.24      noro     9015:             nh = ndv_newps(0,nfv,nfqv);
1.1       noro     9016:             d = update_pairs(d,g,nh,0);
                   9017:             g = update_base(g,nh);
                   9018:         }
                   9019:     }
                   9020: #if 0
                   9021:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   9022: #endif
                   9023:   conv_ilist(nd_demand,1,g,indp);
                   9024:     return g;
                   9025: }
                   9026:
                   9027: int rref(matrix mat,int *sugar)
                   9028: {
                   9029:   int row,col,i,j,k,l,s,wcol,wj;
                   9030:   unsigned long bj;
                   9031:   unsigned long **a;
                   9032:   unsigned long *ai,*ak,*as,*t;
                   9033:   int *pivot;
                   9034:
                   9035:   row = mat->row;
                   9036:   col = mat->col;
                   9037:   a = mat->a;
                   9038:   wcol = (col+BLEN-1)/BLEN;
                   9039:   pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   9040:   i = 0;
                   9041:   for ( j = 0; j < col; j++ ) {
                   9042:   wj = j/BLEN; bj = 1L<<(j%BLEN);
                   9043:     for ( k = i; k < row; k++ )
                   9044:     if ( a[k][wj] & bj ) break;
                   9045:     if ( k == row ) continue;
                   9046:   pivot[i] = j;
                   9047:     if ( k != i ) {
                   9048:    t = a[i]; a[i] = a[k]; a[k] = t;
                   9049:    s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
                   9050:   }
                   9051:   ai = a[i];
                   9052:     for ( k = i+1; k < row; k++ ) {
                   9053:     ak = a[k];
                   9054:     if ( ak[wj] & bj ) {
                   9055:       for ( l = wj; l < wcol; l++ )
                   9056:       ak[l] ^= ai[l];
                   9057:       sugar[k] = MAX(sugar[k],sugar[i]);
                   9058:     }
                   9059:   }
                   9060:   i++;
                   9061:   }
                   9062:   for ( k = i-1; k >= 0; k-- ) {
                   9063:     j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
                   9064:   ak = a[k];
                   9065:     for ( s = 0; s < k; s++ ) {
                   9066:     as = a[s];
                   9067:       if ( as[wj] & bj ) {
                   9068:         for ( l = wj; l < wcol; l++ )
                   9069:       as[l] ^= ak[l];
                   9070:       sugar[s] = MAX(sugar[s],sugar[k]);
                   9071:     }
                   9072:   }
                   9073:   }
                   9074:   return i;
                   9075: }
                   9076:
                   9077: void print_matrix(matrix mat)
                   9078: {
                   9079:   int row,col,i,j;
                   9080:   unsigned long *ai;
                   9081:
                   9082:   row = mat->row;
                   9083:   col = mat->col;
                   9084:   printf("%d x %d\n",row,col);
                   9085:   for ( i = 0; i < row; i++ ) {
                   9086:   ai = mat->a[i];
                   9087:     for ( j = 0; j < col; j++ ) {
                   9088:     if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
                   9089:     else putchar('0');
                   9090:   }
                   9091:   putchar('\n');
                   9092:   }
                   9093: }
                   9094:
                   9095: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
                   9096:
                   9097: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
                   9098: {
                   9099:   int row,col,wcol,wj,i,j;
                   9100:   unsigned long bj;
                   9101:   unsigned long *ai;
                   9102:   unsigned long **a;
                   9103:   int len;
                   9104:   int *pos;
                   9105:
                   9106:   row = mat->row;
                   9107:   col = mat->col;
                   9108:   wcol = (col+BLEN-1)/BLEN;
                   9109:   pos = (int *)MALLOC(wcol*sizeof(int));
                   9110:   bzero(pos,wcol*sizeof(int));
                   9111:   for ( i = j = 0; i < wcol; i++ )
                   9112:     if ( v[i] ) pos[j++] = i;;
                   9113:   len = j;
                   9114:   wj = rhead/BLEN;
                   9115:   bj = 1L<<rhead%BLEN;
                   9116:   a = mat->a;
                   9117:   for ( i = 0; i < row; i++ ) {
                   9118:   ai = a[i];
                   9119:     if ( ai[wj]&bj ) {
                   9120:     for ( j = 0; j < len; j++ )
                   9121:       ai[pos[j]] ^= v[pos[j]];
                   9122:     sugar[i] = MAX(sugar[i],rsugar);
                   9123:   }
                   9124:   }
                   9125: }
                   9126:
                   9127: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   9128: {
1.65    ! noro     9129:   int nsp,i,rank;
        !          9130:   NODE r0,rp;
        !          9131:   ND_pairs sp;
1.1       noro     9132:   ND spol;
                   9133:   NM_ind_pair rt;
1.65    ! noro     9134:   int *s0hash;
1.1       noro     9135:   UINT *s;
1.65    ! noro     9136:   int *sugar;
1.1       noro     9137:   matrix mat;
1.65    ! noro     9138:   NODE r;
1.1       noro     9139:   struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
                   9140:   int rhead,rsugar,size;
1.65    ! noro     9141:   unsigned long *v;
1.1       noro     9142:
1.65    ! noro     9143:   get_eg(&eg0);
        !          9144:   for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
        !          9145: //  nred = length(rp0);
        !          9146:   mat = alloc_matrix(nsp,col);
        !          9147:   s0hash = (int *)MALLOC(col*sizeof(int));
        !          9148:   for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
        !          9149:     s0hash[i] = ndl_hash_value(s);
1.1       noro     9150:
                   9151:   sugar = (int *)MALLOC(nsp*sizeof(int));
                   9152:   for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
                   9153:     nd_sp(2,0,sp,&spol);
                   9154:     if ( spol ) {
                   9155:         mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
                   9156:       sugar[i] = SG(spol);
                   9157:       i++;
                   9158:     }
                   9159:   }
                   9160:   mat->row = i;
                   9161:     if ( DP_Print ) {
                   9162:       fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
                   9163:     }
                   9164:   size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
                   9165:   v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
                   9166:     for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
                   9167:     rt = (NM_ind_pair)BDY(rp);
                   9168:     bzero(v,size);
                   9169:         rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
                   9170:     rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
                   9171:       red_by_vect_2(mat,sugar,v,rhead,rsugar);
                   9172:   }
                   9173:
                   9174:     get_eg(&eg1);
                   9175:     init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
                   9176:   rank = rref(mat,sugar);
                   9177:
                   9178:     for ( i = 0, r0 = 0; i < rank; i++ ) {
                   9179:       NEXTNODE(r0,r);
                   9180:     BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
                   9181:       SG((NDV)BDY(r)) = sugar[i];
                   9182:     }
                   9183:     if ( r0 ) NEXT(r) = 0;
                   9184:     get_eg(&eg2);
                   9185:     init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
                   9186:     if ( DP_Print ) {
                   9187:         fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5       noro     9188:       eg_elim1.exectime,eg_elim2.exectime);
1.1       noro     9189:         fflush(asir_out);
                   9190:   }
                   9191:     return r0;
                   9192: }
                   9193:
                   9194:
                   9195: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   9196: {
                   9197:     IndArray *imat;
1.11      noro     9198:     int nsp,nred,i,start;
1.1       noro     9199:     int *rhead;
                   9200:     NODE r0,rp;
                   9201:     ND_pairs sp;
                   9202:     NM_ind_pair *rvect;
1.11      noro     9203:     struct oEGT eg0,eg1,eg_conv;
1.1       noro     9204:
                   9205:     if ( m == 2 && nd_rref2 )
                   9206:      return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
                   9207:
                   9208:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   9209:     nred = length(rp0);
                   9210:     imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   9211:     rhead = (int *)MALLOC(col*sizeof(int));
                   9212:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   9213:
                   9214:     /* construction of index arrays */
1.11      noro     9215:     get_eg(&eg0);
1.1       noro     9216:     if ( DP_Print ) {
1.11      noro     9217:       fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   9218:       fflush(asir_out);
1.1       noro     9219:     }
                   9220:     rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11      noro     9221:     for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1       noro     9222:         rvect[i] = (NM_ind_pair)BDY(rp);
1.11      noro     9223:         imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1       noro     9224:         rhead[imat[i]->head] = 1;
1.11      noro     9225:         start = imat[i]->head;
                   9226:     }
1.12      noro     9227:     get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11      noro     9228:     if ( DP_Print ) {
                   9229:       fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   9230:       fflush(asir_out);
1.1       noro     9231:     }
                   9232:     if ( m > 0 )
1.7       noro     9233: #if SIZEOF_LONG==8
1.1       noro     9234:         r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   9235: #else
                   9236:         r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   9237: #endif
                   9238:     else if ( m == -1 )
                   9239:         r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   9240:     else if ( m == -2 )
                   9241:         r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   9242:     else
                   9243:         r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   9244:     return r0;
                   9245: }
                   9246:
                   9247: /* for Fp, 2<=p<2^16 */
                   9248:
                   9249: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   9250:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   9251: {
                   9252:     int spcol,sprow,a;
1.65    ! noro     9253:     int i,j,k,rank;
1.1       noro     9254:     NODE r0,r;
                   9255:     ND_pairs sp;
                   9256:     ND spol;
                   9257:     UINT **spmat;
                   9258:     UINT *svect,*v;
                   9259:     int *colstat;
                   9260:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9261:     int maxrs;
                   9262:     int *spsugar;
                   9263:     ND_pairs *spactive;
                   9264:
                   9265:     spcol = col-nred;
                   9266:     get_eg(&eg0);
                   9267:     /* elimination (1st step) */
                   9268:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   9269:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   9270:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9271:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   9272:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9273:         nd_sp(m,0,sp,&spol);
                   9274:         if ( !spol ) continue;
                   9275:         nd_to_vect(m,s0vect,col,spol,svect);
                   9276:         if ( m == -1 )
                   9277:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   9278:         else
1.40      noro     9279:             maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,0);
1.1       noro     9280:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9281:         if ( i < col ) {
                   9282:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   9283:             for ( j = k = 0; j < col; j++ )
                   9284:                 if ( !rhead[j] ) v[k++] = svect[j];
                   9285:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9286:             if ( nz )
                   9287:             spactive[sprow] = sp;
                   9288:             sprow++;
                   9289:         }
                   9290:         nd_free(spol);
                   9291:     }
                   9292:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9293:     if ( DP_Print ) {
1.5       noro     9294:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9295:         fflush(asir_out);
                   9296:     }
                   9297:     /* free index arrays */
                   9298:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   9299:
                   9300:     /* elimination (2nd step) */
                   9301:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9302:     if ( m == -1 )
                   9303:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   9304:     else
                   9305:         rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   9306:     r0 = 0;
                   9307:     for ( i = 0; i < rank; i++ ) {
                   9308:         NEXTNODE(r0,r); BDY(r) =
                   9309:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   9310:         SG((NDV)BDY(r)) = spsugar[i];
                   9311:         GCFREE(spmat[i]);
                   9312:     }
                   9313:     if ( r0 ) NEXT(r) = 0;
                   9314:
                   9315:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   9316:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9317:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9318:     if ( DP_Print ) {
1.5       noro     9319:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9320:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9321:             nsp,nred,sprow,spcol,rank);
1.5       noro     9322:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9323:     }
                   9324:     if ( nz ) {
                   9325:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   9326:         if ( rank > 0 ) {
                   9327:             NEXT(spactive[rank-1]) = 0;
                   9328:             *nz = spactive[0];
                   9329:         } else
                   9330:             *nz = 0;
                   9331:     }
                   9332:     return r0;
                   9333: }
                   9334:
1.40      noro     9335: NODE nd_f4_red_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   9336:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
                   9337: {
1.65    ! noro     9338:     int sprow,a;
        !          9339:     int i,rank;
1.40      noro     9340:     NODE r0,r;
                   9341:     ND_pairs sp;
                   9342:     ND spol;
                   9343:     UINT **spmat;
1.65    ! noro     9344:     UINT *svect;
1.40      noro     9345:     int *colstat;
                   9346:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9347:     int maxrs;
                   9348:     int *spsugar;
                   9349:     SIG *spsig;
                   9350:
                   9351:     get_eg(&eg0);
                   9352:     /* elimination (1st step) */
                   9353:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   9354:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9355:     spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
                   9356:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9357:         nd_sp(m,0,sp,&spol);
                   9358:         if ( !spol ) {
                   9359:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
                   9360:           continue;
                   9361:         }
                   9362:         svect = (UINT *)MALLOC(col*sizeof(UINT));
                   9363:         nd_to_vect(m,s0vect,col,spol,svect);
                   9364:         maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,spol->sig);
                   9365:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9366:         if ( i < col ) {
                   9367:             spmat[sprow] = svect;
                   9368:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9369:             spsig[sprow] = sp->sig;
                   9370:             sprow++;
                   9371:         } else {
                   9372:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
                   9373:         }
                   9374:         nd_free(spol);
                   9375:     }
                   9376:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
                   9377:     if ( DP_Print ) {
                   9378:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   9379:         fflush(asir_out);
                   9380:     }
                   9381:     /* free index arrays */
                   9382:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   9383:
                   9384:     /* elimination (2nd step) */
                   9385:     colstat = (int *)MALLOC(col*sizeof(int));
                   9386:     rank = nd_gauss_elim_mod_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
                   9387:     r0 = 0;
                   9388:     for ( i = 0; i < sprow; i++ ) {
                   9389:         if ( spsugar[i] >= 0 ) {
                   9390:           NEXTNODE(r0,r);
                   9391:           BDY(r) = vect_to_ndv_s(spmat[i],col,s0vect);
                   9392:           SG((NDV)BDY(r)) = spsugar[i];
                   9393:           ((NDV)BDY(r))->sig = spsig[i];
                   9394:         } else
                   9395:           syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
                   9396:         GCFREE(spmat[i]);
                   9397:     }
                   9398:     if ( r0 ) NEXT(r) = 0;
                   9399:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
                   9400:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9401:     if ( DP_Print ) {
                   9402:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   9403:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9404:             nsp,nred,sprow,col,rank);
                   9405:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   9406:     }
                   9407:     return r0;
                   9408: }
                   9409:
1.1       noro     9410:
                   9411: /* for small finite fields */
                   9412:
                   9413: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   9414:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   9415: {
                   9416:     int spcol,sprow,a;
1.65    ! noro     9417:     int i,j,k,rank;
1.1       noro     9418:     NODE r0,r;
                   9419:     ND_pairs sp;
                   9420:     ND spol;
                   9421:     UINT **spmat;
                   9422:     UINT *svect,*v;
                   9423:     int *colstat;
                   9424:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9425:     int maxrs;
                   9426:     int *spsugar;
                   9427:     ND_pairs *spactive;
                   9428:
                   9429:     spcol = col-nred;
                   9430:     get_eg(&eg0);
                   9431:     /* elimination (1st step) */
                   9432:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   9433:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   9434:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9435:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   9436:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9437:         nd_sp(m,0,sp,&spol);
                   9438:         if ( !spol ) continue;
                   9439:         nd_to_vect(m,s0vect,col,spol,svect);
                   9440:         maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   9441:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9442:         if ( i < col ) {
                   9443:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   9444:             for ( j = k = 0; j < col; j++ )
                   9445:                 if ( !rhead[j] ) v[k++] = svect[j];
                   9446:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9447:             if ( nz )
                   9448:             spactive[sprow] = sp;
                   9449:             sprow++;
                   9450:         }
                   9451:         nd_free(spol);
                   9452:     }
                   9453:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9454:     if ( DP_Print ) {
1.5       noro     9455:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9456:         fflush(asir_out);
                   9457:     }
                   9458:     /* free index arrays */
                   9459:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   9460:
                   9461:     /* elimination (2nd step) */
                   9462:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9463:     rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   9464:     r0 = 0;
                   9465:     for ( i = 0; i < rank; i++ ) {
                   9466:         NEXTNODE(r0,r); BDY(r) =
                   9467:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   9468:         SG((NDV)BDY(r)) = spsugar[i];
                   9469:         GCFREE(spmat[i]);
                   9470:     }
                   9471:     if ( r0 ) NEXT(r) = 0;
                   9472:
                   9473:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   9474:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9475:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9476:     if ( DP_Print ) {
1.5       noro     9477:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9478:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9479:             nsp,nred,sprow,spcol,rank);
1.5       noro     9480:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9481:     }
                   9482:     if ( nz ) {
                   9483:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   9484:         if ( rank > 0 ) {
                   9485:             NEXT(spactive[rank-1]) = 0;
                   9486:             *nz = spactive[0];
                   9487:         } else
                   9488:             *nz = 0;
                   9489:     }
                   9490:     return r0;
                   9491: }
                   9492:
                   9493: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   9494:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   9495: {
                   9496:     int spcol,sprow,a;
1.65    ! noro     9497:     int i,j,k,rank;
1.1       noro     9498:     NODE r0,r;
                   9499:     ND_pairs sp;
                   9500:     ND spol;
                   9501:     mpz_t **spmat;
                   9502:     mpz_t *svect,*v;
                   9503:     int *colstat;
                   9504:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9505:     int maxrs;
                   9506:     int *spsugar;
                   9507:     pointer *w;
                   9508:
                   9509:     spcol = col-nred;
                   9510:     get_eg(&eg0);
                   9511:     /* elimination (1st step) */
                   9512:     spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
                   9513:     svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   9514:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9515:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9516:         nd_sp(m,trace,sp,&spol);
                   9517:         if ( !spol ) continue;
                   9518:         nd_to_vect_lf(s0vect,col,spol,svect);
                   9519:         maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
                   9520:         for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
                   9521:         if ( i < col ) {
                   9522:             spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
                   9523:             for ( j = k = 0; j < col; j++ )
                   9524:                 if ( !rhead[j] ) v[k++][0] = svect[j][0];
                   9525:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9526:             sprow++;
                   9527:         }
                   9528: /*        nd_free(spol); */
                   9529:     }
                   9530:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9531:     if ( DP_Print ) {
1.5       noro     9532:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9533:         fflush(asir_out);
                   9534:     }
                   9535:     /* free index arrays */
                   9536: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   9537:
                   9538:     /* elimination (2nd step) */
                   9539:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9540:     rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
                   9541:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   9542:     for ( i = 0; i < rank; i++ ) {
                   9543: #if 0
                   9544:         w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   9545:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   9546: #else
                   9547:         w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   9548:         SG((NDV)w[i]) = spsugar[i];
                   9549: #endif
                   9550: /*        GCFREE(spmat[i]); */
                   9551:
                   9552:     }
                   9553: #if 0
                   9554:     qsort(w,rank,sizeof(NDV),
                   9555:         (int (*)(const void *,const void *))ndv_compare);
                   9556: #endif
                   9557:     r0 = 0;
                   9558:     for ( i = 0; i < rank; i++ ) {
                   9559:         NEXTNODE(r0,r); BDY(r) = w[i];
                   9560:     }
                   9561:     if ( r0 ) NEXT(r) = 0;
                   9562:
                   9563: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   9564:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9565:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9566:     if ( DP_Print ) {
1.5       noro     9567:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9568:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9569:             nsp,nred,sprow,spcol,rank);
1.5       noro     9570:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9571:     }
                   9572:     return r0;
                   9573: }
                   9574:
                   9575: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   9576:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   9577: {
                   9578:     int spcol,sprow,a;
1.65    ! noro     9579:     int i,j,k,rank;
1.1       noro     9580:     NODE r0,r;
                   9581:     ND_pairs sp;
                   9582:     ND spol;
                   9583:     Z **spmat;
                   9584:     Z *svect,*v;
                   9585:     int *colstat;
                   9586:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9587:     int maxrs;
                   9588:     int *spsugar;
                   9589:     pointer *w;
                   9590:
                   9591:     spcol = col-nred;
                   9592:     get_eg(&eg0);
                   9593:     /* elimination (1st step) */
                   9594:     spmat = (Z **)MALLOC(nsp*sizeof(Q *));
                   9595:     svect = (Z *)MALLOC(col*sizeof(Q));
                   9596:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9597:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9598:         nd_sp(0,trace,sp,&spol);
                   9599:         if ( !spol ) continue;
                   9600:         nd_to_vect_q(s0vect,col,spol,svect);
                   9601:         maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
                   9602:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9603:         if ( i < col ) {
                   9604:             spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
                   9605:             for ( j = k = 0; j < col; j++ )
                   9606:                 if ( !rhead[j] ) v[k++] = svect[j];
                   9607:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9608:             sprow++;
                   9609:         }
                   9610: /*        nd_free(spol); */
                   9611:     }
                   9612:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9613:     if ( DP_Print ) {
1.5       noro     9614:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9615:         fflush(asir_out);
                   9616:     }
                   9617:     /* free index arrays */
                   9618: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   9619:
                   9620:     /* elimination (2nd step) */
                   9621:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9622:     rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
                   9623:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   9624:     for ( i = 0; i < rank; i++ ) {
                   9625: #if 0
                   9626:         w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   9627:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   9628: #else
                   9629:         w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   9630:         SG((NDV)w[i]) = spsugar[i];
                   9631: #endif
                   9632: /*        GCFREE(spmat[i]); */
                   9633:     }
                   9634: #if 0
                   9635:     qsort(w,rank,sizeof(NDV),
                   9636:         (int (*)(const void *,const void *))ndv_compare);
                   9637: #endif
                   9638:     r0 = 0;
                   9639:     for ( i = 0; i < rank; i++ ) {
                   9640:         NEXTNODE(r0,r); BDY(r) = w[i];
                   9641:     }
                   9642:     if ( r0 ) NEXT(r) = 0;
                   9643:
                   9644: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   9645:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9646:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9647:     if ( DP_Print ) {
1.5       noro     9648:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9649:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9650:             nsp,nred,sprow,spcol,rank);
1.5       noro     9651:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9652:     }
                   9653:     return r0;
                   9654: }
                   9655:
                   9656: FILE *nd_write,*nd_read;
                   9657:
                   9658: void nd_send_int(int a) {
                   9659:     write_int(nd_write,(unsigned int *)&a);
                   9660: }
                   9661:
                   9662: void nd_send_intarray(int *p,int len) {
                   9663:     write_intarray(nd_write,(unsigned int *)p,len);
                   9664: }
                   9665:
                   9666: int nd_recv_int() {
                   9667:     int a;
                   9668:
                   9669:     read_int(nd_read,(unsigned int *)&a);
                   9670:     return a;
                   9671: }
                   9672:
                   9673: void nd_recv_intarray(int *p,int len) {
                   9674:     read_intarray(nd_read,(unsigned int *)p,len);
                   9675: }
                   9676:
                   9677: void nd_send_ndv(NDV p) {
                   9678:     int len,i;
                   9679:     NMV m;
                   9680:
                   9681:     if ( !p ) nd_send_int(0);
                   9682:     else {
                   9683:         len = LEN(p);
                   9684:         nd_send_int(len);
                   9685:         m = BDY(p);
                   9686:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9687:             nd_send_int(CM(m));
                   9688:             nd_send_intarray((int *)DL(m),nd_wpd);
                   9689:         }
                   9690:     }
                   9691: }
                   9692:
                   9693: void nd_send_nd(ND p) {
                   9694:     int len,i;
                   9695:     NM m;
                   9696:
                   9697:     if ( !p ) nd_send_int(0);
                   9698:     else {
                   9699:         len = LEN(p);
                   9700:         nd_send_int(len);
                   9701:         m = BDY(p);
                   9702:         for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   9703:             nd_send_int(CM(m));
                   9704:             nd_send_intarray((int *)DL(m),nd_wpd);
                   9705:         }
                   9706:     }
                   9707: }
                   9708:
                   9709: NDV nd_recv_ndv()
                   9710: {
                   9711:     int len,i;
                   9712:     NMV m,m0;
                   9713:     NDV r;
                   9714:
                   9715:     len = nd_recv_int();
                   9716:     if ( !len ) return 0;
                   9717:     else {
                   9718:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   9719: #if 0
                   9720:         ndv_alloc += len*nmv_adv;
                   9721: #endif
                   9722:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9723:             CM(m) = nd_recv_int();
                   9724:             nd_recv_intarray((int *)DL(m),nd_wpd);
                   9725:         }
                   9726:         MKNDV(nd_nvar,m0,len,r);
                   9727:         return r;
                   9728:     }
                   9729: }
                   9730:
                   9731: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
                   9732: {
1.65    ! noro     9733:     int i,j,c,rank;
1.1       noro     9734:     int *ci,*ri;
                   9735:     Z dn;
                   9736:     MAT m,nm;
                   9737:
                   9738:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
                   9739:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   9740:     for ( i = 0; i < row; i++ )
                   9741:         for ( j = 0; j < col; j++ )
                   9742:             mat0[i][j] = 0;
                   9743:     c = col-rank;
                   9744:     for ( i = 0; i < rank; i++ ) {
                   9745:         mat0[i][ri[i]] = dn;
                   9746:         for ( j = 0; j < c; j++ )
                   9747:             mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
                   9748:     }
                   9749:     return rank;
                   9750: }
                   9751:
                   9752: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   9753: {
                   9754:     int i,j,k,l,inv,a,rank,s;
                   9755:     unsigned int *t,*pivot,*pk;
                   9756:     unsigned int **mat;
                   9757:     ND_pairs pair;
                   9758:
                   9759:     mat = (unsigned int **)mat0;
                   9760:     for ( rank = 0, j = 0; j < col; j++ ) {
                   9761:         for ( i = rank; i < row; i++ )
                   9762:             mat[i][j] %= md;
                   9763:         for ( i = rank; i < row; i++ )
                   9764:             if ( mat[i][j] )
                   9765:                 break;
                   9766:         if ( i == row ) {
                   9767:             colstat[j] = 0;
                   9768:             continue;
                   9769:         } else
                   9770:             colstat[j] = 1;
                   9771:         if ( i != rank ) {
                   9772:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   9773:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   9774:             if ( spactive ) {
                   9775:                 pair = spactive[i]; spactive[i] = spactive[rank];
                   9776:                 spactive[rank] = pair;
                   9777:             }
                   9778:         }
                   9779:         pivot = mat[rank];
                   9780:         s = sugar[rank];
                   9781:         inv = invm(pivot[j],md);
                   9782:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   9783:             if ( *pk ) {
                   9784:                 if ( *pk >= (unsigned int)md )
                   9785:                     *pk %= md;
                   9786:                 DMAR(*pk,inv,0,md,*pk)
                   9787:             }
                   9788:         for ( i = rank+1; i < row; i++ ) {
                   9789:             t = mat[i];
                   9790:             if ( (a = t[j]) != 0 ) {
                   9791:                 sugar[i] = MAX(sugar[i],s);
                   9792:                 red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   9793:             }
                   9794:         }
                   9795:         rank++;
                   9796:     }
                   9797:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   9798:         if ( colstat[j] ) {
                   9799:             pivot = mat[l];
                   9800:             s = sugar[l];
                   9801:             for ( i = 0; i < l; i++ ) {
                   9802:                 t = mat[i];
                   9803:                 t[j] %= md;
                   9804:                 if ( (a = t[j]) != 0 ) {
                   9805:                     sugar[i] = MAX(sugar[i],s);
                   9806:                     red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   9807:                 }
                   9808:             }
                   9809:             l--;
                   9810:         }
                   9811:     for ( j = 0, l = 0; l < rank; j++ )
                   9812:         if ( colstat[j] ) {
                   9813:             t = mat[l];
                   9814:             for ( k = j; k < col; k++ )
                   9815:                 if ( t[k] >= (unsigned int)md )
                   9816:                     t[k] %= md;
                   9817:             l++;
                   9818:         }
                   9819:     return rank;
                   9820: }
                   9821:
1.40      noro     9822: int nd_gauss_elim_mod_s(UINT **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat,SIG *sig)
                   9823: {
1.65    ! noro     9824:   int i,j,k,rank,s;
1.40      noro     9825:   UINT inv;
                   9826:   UINT a;
1.65    ! noro     9827:   UINT *pk;
1.40      noro     9828:   int *used;
                   9829:
                   9830:   used = (int *)MALLOC(row*sizeof(int));
                   9831:   for ( j = 0; j < col; j++ ) {
                   9832:     for ( i = 0; i < row; i++ )
                   9833:       a = mat[i][j] %= md;
                   9834:     for ( i = 0; i < row; i++ )
                   9835:       if ( !used[i] && mat[i][j] ) break;
                   9836:     if ( i == row ) {
                   9837:       colstat[j] = 0;
                   9838:       continue;
                   9839:     } else {
                   9840:       colstat[j] = 1;
                   9841:       used[i] = 1;
                   9842:     }
                   9843:     /* column j is normalized */
                   9844:     s = sugar[i];
                   9845:     inv = invm(mat[i][j],md);
                   9846:     /* normalize pivot row */
1.65    ! noro     9847:     for ( k = j, pk = mat[i]+j; k < col; k++, pk++ ) {
1.40      noro     9848:       DMAR(*pk,inv,0,md,*pk);
                   9849:     }
                   9850:     for ( k = i+1; k < row; k++ ) {
                   9851:       if ( (a = mat[k][j]) != 0 ) {
                   9852:         sugar[k] = MAX(sugar[k],s);
                   9853:         red_by_vect(md,mat[k]+j,mat[i]+j,(int)(md-a),col-j);
                   9854:         Nf4_red++;
                   9855:       }
                   9856:     }
                   9857:   }
                   9858:   rank = 0;
                   9859:   for ( i = 0; i < row; i++ ) {
                   9860:     for ( j = 0; j < col; j++ )
                   9861:       if ( mat[i][j] ) break;
                   9862:     if ( j == col ) sugar[i] = -1;
                   9863:     else rank++;
                   9864:   }
                   9865:   return rank;
                   9866: }
                   9867:
1.1       noro     9868:
1.7       noro     9869: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1       noro     9870: {
1.7       noro     9871:     int i,j,k,l,inv,a,rank,s;
                   9872:     unsigned int *t,*pivot,*pk;
                   9873:     unsigned int **mat;
                   9874:
                   9875:     mat = (unsigned int **)mat0;
                   9876:     for ( rank = 0, j = 0; j < col; j++ ) {
                   9877:         for ( i = rank; i < row; i++ )
                   9878:             if ( mat[i][j] )
                   9879:                 break;
                   9880:         if ( i == row ) {
                   9881:             colstat[j] = 0;
                   9882:             continue;
                   9883:         } else
                   9884:             colstat[j] = 1;
                   9885:         if ( i != rank ) {
                   9886:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   9887:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   9888:         }
                   9889:         pivot = mat[rank];
                   9890:         s = sugar[rank];
                   9891:         inv = _invsf(pivot[j]);
                   9892:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   9893:             if ( *pk )
                   9894:                 *pk = _mulsf(*pk,inv);
                   9895:         for ( i = rank+1; i < row; i++ ) {
                   9896:             t = mat[i];
                   9897:             if ( (a = t[j]) != 0 ) {
                   9898:                 sugar[i] = MAX(sugar[i],s);
                   9899:                 red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   9900:             }
                   9901:         }
                   9902:         rank++;
                   9903:     }
                   9904:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   9905:         if ( colstat[j] ) {
                   9906:             pivot = mat[l];
                   9907:             s = sugar[l];
                   9908:             for ( i = 0; i < l; i++ ) {
                   9909:                 t = mat[i];
                   9910:                 if ( (a = t[j]) != 0 ) {
                   9911:                     sugar[i] = MAX(sugar[i],s);
                   9912:                     red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   9913:                 }
                   9914:             }
                   9915:             l--;
                   9916:         }
                   9917:     return rank;
                   9918: }
1.1       noro     9919:
1.7       noro     9920: int ndv_ishomo(NDV p)
                   9921: {
                   9922:     NMV m;
                   9923:     int len,h;
1.1       noro     9924:
                   9925:     if ( !p ) return 1;
                   9926:     len = LEN(p);
                   9927:     m = BDY(p);
                   9928:     h = TD(DL(m));
                   9929:     NMV_ADV(m);
                   9930:     for ( len--; len; len--, NMV_ADV(m) )
1.20      noro     9931:         if ( TD(DL(m)) != h ) {
                   9932:           return 0;
                   9933:         }
1.1       noro     9934:     return 1;
                   9935: }
                   9936:
                   9937: void ndv_save(NDV p,int index)
                   9938: {
                   9939:     FILE *s;
                   9940:     char name[BUFSIZ];
                   9941:     short id;
1.65    ! noro     9942:     int nv,sugar,len,i,td,e,j;
1.1       noro     9943:     NMV m;
                   9944:     unsigned int *dl;
                   9945:     int mpos;
                   9946:
                   9947:     sprintf(name,"%s/%d",Demand,index);
                   9948:     s = fopen(name,"w");
                   9949:     savevl(s,0);
                   9950:     if ( !p ) {
                   9951:         saveobj(s,0);
                   9952:         return;
                   9953:     }
                   9954:     id = O_DP;
                   9955:     nv = NV(p);
                   9956:     sugar = SG(p);
                   9957:     len = LEN(p);
                   9958:     write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
                   9959:     write_int(s,(unsigned int *)&len);
                   9960:
                   9961:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     9962:         saveobj(s,(Obj)CZ(m));
1.1       noro     9963:         dl = DL(m);
                   9964:         td = TD(dl);
                   9965:         write_int(s,(unsigned int *)&td);
                   9966:         for ( j = 0; j < nv; j++ ) {
                   9967:             e = GET_EXP(dl,j);
                   9968:             write_int(s,(unsigned int *)&e);
                   9969:         }
                   9970:         if ( nd_module ) {
                   9971:             mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
                   9972:         }
                   9973:     }
                   9974:     fclose(s);
                   9975: }
                   9976:
                   9977: void nd_save_mod(ND p,int index)
                   9978: {
                   9979:     FILE *s;
                   9980:     char name[BUFSIZ];
                   9981:     int nv,sugar,len,c;
                   9982:     NM m;
                   9983:
                   9984:     sprintf(name,"%s/%d",Demand,index);
                   9985:     s = fopen(name,"w");
                   9986:     if ( !p ) {
                   9987:     len = 0;
                   9988:       write_int(s,(unsigned int *)&len);
                   9989:     fclose(s);
                   9990:         return;
                   9991:     }
                   9992:     nv = NV(p);
                   9993:     sugar = SG(p);
                   9994:     len = LEN(p);
                   9995:     write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
                   9996:   for ( m = BDY(p); m; m = NEXT(m) ) {
                   9997:     c = CM(m); write_int(s,(unsigned int *)&c);
                   9998:     write_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   9999:   }
                   10000:     fclose(s);
                   10001: }
                   10002:
                   10003: NDV ndv_load(int index)
                   10004: {
                   10005:     FILE *s;
                   10006:     char name[BUFSIZ];
                   10007:     short id;
1.65    ! noro     10008:     int nv,sugar,len,i,td,e,j;
1.1       noro     10009:     NDV d;
                   10010:     NMV m0,m;
                   10011:     unsigned int *dl;
                   10012:     Obj obj;
                   10013:     int mpos;
                   10014:
                   10015:     sprintf(name,"%s/%d",Demand,index);
                   10016:     s = fopen(name,"r");
                   10017:     if ( !s ) return 0;
                   10018:
                   10019:     skipvl(s);
                   10020:     read_short(s,(unsigned short *)&id);
                   10021:     if ( !id ) return 0;
                   10022:     read_int(s,(unsigned int *)&nv);
                   10023:     read_int(s,(unsigned int *)&sugar);
                   10024:     read_int(s,(unsigned int *)&len);
                   10025:
                   10026:     m0 = m = MALLOC(len*nmv_adv);
                   10027:     for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     10028:         loadobj(s,&obj); CZ(m) = (Z)obj;
1.1       noro     10029:         dl = DL(m);
                   10030:         ndl_zero(dl);
                   10031:         read_int(s,(unsigned int *)&td); TD(dl) = td;
                   10032:         for ( j = 0; j < nv; j++ ) {
                   10033:             read_int(s,(unsigned int *)&e);
                   10034:             PUT_EXP(dl,j,e);
                   10035:         }
                   10036:         if ( nd_module ) {
                   10037:             read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
                   10038:         }
                   10039:         if ( nd_blockmask ) ndl_weight_mask(dl);
                   10040:     }
                   10041:     fclose(s);
                   10042:     MKNDV(nv,m0,len,d);
                   10043:     SG(d) = sugar;
                   10044:     return d;
                   10045: }
                   10046:
                   10047: ND nd_load_mod(int index)
                   10048: {
                   10049:     FILE *s;
                   10050:     char name[BUFSIZ];
                   10051:     int nv,sugar,len,i,c;
                   10052:   ND d;
                   10053:     NM m0,m;
                   10054:
                   10055:     sprintf(name,"%s/%d",Demand,index);
                   10056:     s = fopen(name,"r");
                   10057:   /* if the file does not exist, it means p[index]=0 */
                   10058:     if ( !s ) return 0;
                   10059:
                   10060:     read_int(s,(unsigned int *)&nv);
                   10061:   if ( !nv ) { fclose(s); return 0; }
                   10062:
                   10063:     read_int(s,(unsigned int *)&sugar);
                   10064:     read_int(s,(unsigned int *)&len);
                   10065:   for ( m0 = 0, i = 0; i < len; i++ ) {
                   10066:     NEXTNM(m0,m);
                   10067:     read_int(s,(unsigned int *)&c); CM(m) = c;
                   10068:     read_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   10069:   }
                   10070:   NEXT(m) = 0;
                   10071:     MKND(nv,m0,len,d);
                   10072:     SG(d) = sugar;
                   10073:   fclose(s);
                   10074:     return d;
                   10075: }
                   10076:
                   10077: void nd_det(int mod,MAT f,P *rp)
                   10078: {
                   10079:     VL fv,tv;
                   10080:     int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
                   10081:     pointer **m;
                   10082:     P **w;
                   10083:     P mp,r;
                   10084:     NDV **dm;
                   10085:     NDV *t,*mi,*mj;
                   10086:     NDV d,s,mij,mjj;
                   10087:     ND u;
                   10088:     NMV nmv;
                   10089:     UINT *bound;
                   10090:     PGeoBucket bucket;
                   10091:     struct order_spec *ord;
                   10092:     Z dq,dt,ds;
                   10093:     Z mone;
                   10094:     Z gn,qn,dn0,nm,dn;
                   10095:
                   10096:     create_order_spec(0,0,&ord);
                   10097:     nd_init_ord(ord);
                   10098:     get_vars((Obj)f,&fv);
                   10099:     if ( f->row != f->col )
                   10100:         error("nd_det : non-square matrix");
                   10101:     n = f->row;
                   10102:     m = f->body;
                   10103:     for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   10104:
                   10105:     if ( !nvar ) {
                   10106:         if ( !mod )
                   10107:             detp(CO,(P **)m,n,rp);
                   10108:         else {
                   10109:             w = (P **)almat_pointer(n,n);
                   10110:             for ( i = 0; i < n; i++ )
                   10111:                 for ( j = 0; j < n; j++ )
                   10112:                     ptomp(mod,(P)m[i][j],&w[i][j]);
                   10113:             detmp(CO,mod,w,n,&mp);
                   10114:             mptop(mp,rp);
                   10115:         }
                   10116:         return;
                   10117:     }
                   10118:
                   10119:     if ( !mod ) {
                   10120:         w = (P **)almat_pointer(n,n);
                   10121:         dq = ONE;
                   10122:         for ( i = 0; i < n; i++ ) {
                   10123:             dn0 = ONE;
                   10124:             for ( j = 0; j < n; j++ ) {
                   10125:                 if ( !m[i][j] ) continue;
                   10126:                 lgp(m[i][j],&nm,&dn);
1.6       noro     10127:                 gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1       noro     10128:             }
                   10129:             if ( !UNIZ(dn0) ) {
                   10130:                 ds = dn0;
                   10131:                 for ( j = 0; j < n; j++ )
                   10132:                     mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   10133:                 mulz(dq,ds,&dt); dq = dt;
                   10134:             } else
                   10135:                 for ( j = 0; j < n; j++ )
                   10136:                     w[i][j] = (P)m[i][j];
                   10137:         }
                   10138:         m = (pointer **)w;
                   10139:     }
                   10140:
                   10141:     for ( i = 0, max = 1; i < n; i++ )
                   10142:         for ( j = 0; j < n; j++ )
                   10143:             for ( tv = fv; tv; tv = NEXT(tv) ) {
                   10144:                 e = getdeg(tv->v,(P)m[i][j]);
                   10145:                 max = MAX(e,max);
                   10146:             }
                   10147:     nd_setup_parameters(nvar,max);
                   10148:     dm = (NDV **)almat_pointer(n,n);
                   10149:     for ( i = 0, max = 1; i < n; i++ )
                   10150:         for ( j = 0; j < n; j++ ) {
                   10151:             dm[i][j] = ptondv(CO,fv,m[i][j]);
                   10152:             if ( mod ) ndv_mod(mod,dm[i][j]);
                   10153:             if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   10154:         }
                   10155:     d = ptondv(CO,fv,(P)ONE);
                   10156:     if ( mod ) ndv_mod(mod,d);
                   10157:     chsgnz(ONE,&mone);
                   10158:     for ( j = 0, sgn = 1; j < n; j++ ) {
                   10159:       if ( DP_Print ) {
                   10160:         fprintf(asir_out,".");
                   10161:       }
                   10162:         for ( i = j; i < n && !dm[i][j]; i++ );
                   10163:         if ( i == n ) {
                   10164:             *rp = 0;
                   10165:             return;
                   10166:         }
                   10167:         k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
                   10168:         for ( k = j; k < n; k++ )
                   10169:             for ( l = j; l < n; l++ )
                   10170:                 if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   10171:                     k0 = k; l0 = l; len0 = LEN(dm[k][l]);
                   10172:                 }
                   10173:         if ( k0 != j ) {
                   10174:             t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   10175:             sgn = -sgn;
                   10176:         }
                   10177:         if ( l0 != j ) {
                   10178:             for ( k = j; k < n; k++ ) {
                   10179:                 s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   10180:             }
                   10181:             sgn = -sgn;
                   10182:         }
                   10183:         bound = nd_det_compute_bound(dm,n,j);
                   10184:         for ( k = 0; k < nd_nvar; k++ )
                   10185:             if ( bound[k]*2 > nd_mask0 ) break;
                   10186:         if ( k < nd_nvar )
                   10187:             nd_det_reconstruct(dm,n,j,d);
                   10188:
                   10189:         for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
                   10190: /*            if ( DP_Print ) fprintf(asir_out,"    i=%d\n        ",i); */
                   10191:             mi = dm[i]; mij = mi[j];
                   10192:             if ( mod )
                   10193:                 ndv_mul_c(mod,mij,mod-1);
                   10194:             else
                   10195:                 ndv_mul_c_q(mij,mone);
                   10196:             for ( k = j+1; k < n; k++ ) {
                   10197: /*                if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
                   10198:                 bucket = create_pbucket();
                   10199:                 if ( mi[k] ) {
                   10200:                     nmv = BDY(mjj); len = LEN(mjj);
                   10201:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   10202:                         u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   10203:                         add_pbucket(mod,bucket,u);
                   10204:                     }
                   10205:                 }
                   10206:                 if ( mj[k] && mij ) {
                   10207:                     nmv = BDY(mij); len = LEN(mij);
                   10208:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   10209:                         u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   10210:                         add_pbucket(mod,bucket,u);
                   10211:                     }
                   10212:                 }
                   10213:                 u = nd_quo(mod,bucket,d);
                   10214:                 mi[k] = ndtondv(mod,u);
                   10215:             }
                   10216: /*            if ( DP_Print ) fprintf(asir_out,"\n",k); */
                   10217:         }
                   10218:         d = mjj;
                   10219:     }
                   10220:     if ( DP_Print ) {
                   10221:       fprintf(asir_out,"\n");
                   10222:     }
                   10223:     if ( sgn < 0 ) {
                   10224:         if ( mod )
                   10225:             ndv_mul_c(mod,d,mod-1);
                   10226:         else
                   10227:             ndv_mul_c_q(d,mone);
                   10228:     }
                   10229:     r = ndvtop(mod,CO,fv,d);
                   10230:     if ( !mod && !UNIQ(dq) )
                   10231:         divsp(CO,r,(P)dq,rp);
                   10232:     else
                   10233:         *rp = r;
                   10234: }
                   10235:
                   10236: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
                   10237: {
                   10238:     NM mr,mr0;
                   10239:     NM tnm;
                   10240:     NMV m;
1.65    ! noro     10241:     UINT *d0;
        !          10242:     int c,i,c1,c2,len;
1.1       noro     10243:     Z q;
                   10244:     ND r;
                   10245:
                   10246:     if ( !p ) return 0;
                   10247:     else {
1.65    ! noro     10248:         m = BDY(p); len = LEN(p);
1.1       noro     10249:         d0 = DL(m0);
                   10250:         mr0 = 0;
                   10251:         NEWNM(tnm);
                   10252:         if ( mod ) {
                   10253:             c = CM(m0);
                   10254:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   10255:                 ndl_add(DL(m),d0,DL(tnm));
                   10256:                 if ( ndl_reducible(DL(tnm),d) ) {
                   10257:                     NEXTNM(mr0,mr);
                   10258:                     c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
                   10259:                     ndl_copy(DL(tnm),DL(mr));
                   10260:                 }
                   10261:             }
                   10262:         } else {
1.6       noro     10263:             q = CZ(m0);
1.1       noro     10264:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   10265:                 ndl_add(DL(m),d0,DL(tnm));
                   10266:                 if ( ndl_reducible(DL(tnm),d) ) {
                   10267:                     NEXTNM(mr0,mr);
1.6       noro     10268:                     mulz(CZ(m),q,&CZ(mr));
1.1       noro     10269:                     ndl_copy(DL(tnm),DL(mr));
                   10270:                 }
                   10271:             }
                   10272:         }
                   10273:         if ( !mr0 )
                   10274:             return 0;
                   10275:         else {
                   10276:             NEXT(mr) = 0;
                   10277:             for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   10278:             MKND(NV(p),mr0,len,r);
                   10279:             SG(r) = SG(p) + TD(d0);
                   10280:             return r;
                   10281:         }
                   10282:     }
                   10283: }
                   10284:
                   10285: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   10286: {
1.65    ! noro     10287:     int obpe,oadv,k,l;
1.1       noro     10288:     static NM prev_nm_free_list;
                   10289:     EPOS oepos;
                   10290:
                   10291:     obpe = nd_bpe;
                   10292:     oadv = nmv_adv;
                   10293:     oepos = nd_epos;
                   10294:     if ( obpe < 2 ) nd_bpe = 2;
                   10295:     else if ( obpe < 3 ) nd_bpe = 3;
                   10296:     else if ( obpe < 4 ) nd_bpe = 4;
                   10297:     else if ( obpe < 5 ) nd_bpe = 5;
                   10298:     else if ( obpe < 6 ) nd_bpe = 6;
                   10299:     else if ( obpe < 8 ) nd_bpe = 8;
                   10300:     else if ( obpe < 10 ) nd_bpe = 10;
                   10301:     else if ( obpe < 16 ) nd_bpe = 16;
                   10302:     else if ( obpe < 32 ) nd_bpe = 32;
                   10303:     else error("nd_det_reconstruct : exponent too large");
                   10304:
                   10305:     nd_setup_parameters(nd_nvar,0);
                   10306:     prev_nm_free_list = _nm_free_list;
                   10307:     _nm_free_list = 0;
                   10308:     for ( k = j; k < n; k++ )
                   10309:         for (l = j; l < n; l++ )
                   10310:             ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   10311:     ndv_realloc(d,obpe,oadv,oepos);
                   10312:     prev_nm_free_list = 0;
                   10313: #if 0
                   10314:     GC_gcollect();
                   10315: #endif
                   10316: }
                   10317:
                   10318: /* returns a UINT array containing degree bounds */
                   10319:
                   10320: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   10321: {
1.65    ! noro     10322:     UINT *d0,*d;
1.1       noro     10323:     int k,l,i;
                   10324:
                   10325:     d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
                   10326:     for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
                   10327:     for ( k = j; k < n; k++ )
                   10328:         for ( l = j; l < n; l++ )
                   10329:             if ( dm[k][l] ) {
                   10330:                 d = ndv_compute_bound(dm[k][l]);
                   10331:                 for ( i = 0; i < nd_nvar; i++ )
                   10332:                     d0[i] = MAX(d0[i],d[i]);
                   10333:             }
                   10334:     return d0;
                   10335: }
                   10336:
                   10337: DL nd_separate_d(UINT *d,UINT *trans)
                   10338: {
1.65    ! noro     10339:     int td,i,e,j;
1.1       noro     10340:     DL a;
                   10341:
                   10342:     ndl_zero(trans);
                   10343:     td = 0;
                   10344:     for ( i = 0; i < nd_ntrans; i++ ) {
                   10345:         e = GET_EXP(d,i);
                   10346:         PUT_EXP(trans,i,e);
                   10347:         td += MUL_WEIGHT(e,i);
                   10348:     }
                   10349:     if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   10350:         /* homogenized */
                   10351:         i = nd_nvar-1;
                   10352:         e = GET_EXP(d,i);
                   10353:         PUT_EXP(trans,i,e);
                   10354:         td += MUL_WEIGHT(e,i);
                   10355:     }
                   10356:     TD(trans) = td;
                   10357:     if ( nd_blockmask) ndl_weight_mask(trans);
                   10358:     NEWDL(a,nd_nalg);
                   10359:     td = 0;
                   10360:     for ( i = 0; i < nd_nalg; i++ ) {
                   10361:         j = nd_ntrans+i;
                   10362:         e = GET_EXP(d,j);
                   10363:         a->d[i] = e;
                   10364:         td += e;
                   10365:     }
                   10366:     a->td = td;
                   10367:     return a;
                   10368: }
                   10369:
                   10370: int nd_monic(int mod,ND *p)
                   10371: {
                   10372:     DL alg;
                   10373:     MP mp0,mp;
1.65    ! noro     10374:     NM m,ma0,ma,mb,mr0,mr;
1.1       noro     10375:     ND r;
                   10376:     DL dl;
                   10377:     DP nm;
                   10378:     DAlg inv,cd;
                   10379:     Z l,mul;
                   10380:     Z ln;
1.65    ! noro     10381:     int i,e,td,is_lc,len;
1.1       noro     10382:     NumberField nf;
                   10383:     struct oEGT eg0,eg1;
                   10384:
                   10385:     if ( !(nf = get_numberfield()) )
                   10386:         error("nd_monic : current_numberfield is not set");
                   10387:
                   10388:     /* Q coef -> DAlg coef */
                   10389:     NEWNM(ma0); ma = ma0;
                   10390:     m = BDY(*p);
                   10391:     is_lc = 1;
                   10392:     while ( 1 ) {
                   10393:         NEWMP(mp0); mp = mp0;
1.6       noro     10394:         mp->c = (Obj)CZ(m);
1.1       noro     10395:         mp->dl = nd_separate_d(DL(m),DL(ma));
                   10396:         NEWNM(mb);
                   10397:         for ( m = NEXT(m); m; m = NEXT(m) ) {
                   10398:             alg = nd_separate_d(DL(m),DL(mb));
                   10399:             if ( !ndl_equal(DL(ma),DL(mb)) )
                   10400:                 break;
1.6       noro     10401:             NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1       noro     10402:         }
                   10403:         NEXT(mp) = 0;
                   10404:         MKDP(nd_nalg,mp0,nm);
                   10405:         MKDAlg(nm,ONE,cd);
                   10406:         if ( is_lc == 1 ) {
                   10407:             /* if the lc is a rational number, we have nothing to do */
                   10408:             if ( !mp0->dl->td )
                   10409:                 return 1;
                   10410:
                   10411:             get_eg(&eg0);
                   10412:             invdalg(cd,&inv);
                   10413:             get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   10414:             /* check the validity of inv */
                   10415:             if ( mod && !remqi((Q)inv->dn,mod) )
                   10416:                 return 0;
                   10417:             CA(ma) = nf->one;
                   10418:             is_lc = 0;
                   10419:             ln = ONE;
                   10420:         } else {
                   10421:             muldalg(cd,inv,&CA(ma));
                   10422:             lcmz(ln,CA(ma)->dn,&ln);
                   10423:         }
                   10424:         if ( m ) {
                   10425:             NEXT(ma) = mb; ma = mb;
                   10426:         } else {
                   10427:             NEXT(ma) = 0;
                   10428:             break;
                   10429:         }
                   10430:     }
                   10431:     /* l = lcm(denoms) */
                   10432:     l = ln;
                   10433:     for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6       noro     10434:         divsz(l,CA(m)->dn,&mul);
1.1       noro     10435:         for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   10436:             NEXTNM(mr0,mr);
1.6       noro     10437:             mulz((Z)mp->c,mul,&CZ(mr));
1.1       noro     10438:             dl = mp->dl;
                   10439:             td = TD(DL(m));
                   10440:             ndl_copy(DL(m),DL(mr));
                   10441:             for ( i = 0; i < nd_nalg; i++ ) {
                   10442:                 e = dl->d[i];
                   10443:                 PUT_EXP(DL(mr),i+nd_ntrans,e);
                   10444:                 td += MUL_WEIGHT(e,i+nd_ntrans);
                   10445:             }
                   10446:             if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
                   10447:             TD(DL(mr)) = td;
                   10448:             if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   10449:         }
                   10450:     }
                   10451:     NEXT(mr) = 0;
                   10452:     for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   10453:     MKND(NV(*p),mr0,len,r);
                   10454:     /* XXX */
                   10455:     SG(r) = SG(*p);
                   10456:     nd_free(*p);
                   10457:     *p = r;
                   10458:     return 1;
                   10459: }
                   10460:
                   10461: NODE reverse_node(NODE n)
                   10462: {
                   10463:     NODE t,t1;
                   10464:
                   10465:     for ( t = 0; n; n = NEXT(n) ) {
                   10466:         MKNODE(t1,BDY(n),t); t = t1;
                   10467:     }
                   10468:     return t;
                   10469: }
                   10470:
                   10471: P ndc_div(int mod,union oNDC a,union oNDC b)
                   10472: {
                   10473:     union oNDC c;
                   10474:     int inv,t;
                   10475:
                   10476:     if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10      noro     10477:     else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1       noro     10478:     else if ( mod ) {
                   10479:         inv = invm(b.m,mod);
                   10480:         DMAR(a.m,inv,0,mod,t); c.m = t;
                   10481:     } else if ( nd_vc )
                   10482:        divsp(nd_vc,a.p,b.p,&c.p);
                   10483:     else
                   10484:        divsz(a.z,b.z,&c.z);
                   10485:     return ndctop(mod,c);
                   10486: }
                   10487:
                   10488: P ndctop(int mod,union oNDC c)
                   10489: {
                   10490:     Z q;
                   10491:     int e;
                   10492:     GFS gfs;
                   10493:
                   10494:     if ( mod == -1 ) {
                   10495:         e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
                   10496:     } else if ( mod == -2 ) {
1.10      noro     10497:        q = c.z; return (P)q;
1.1       noro     10498:     } else if ( mod > 0 ) {
1.6       noro     10499:         STOZ(c.m,q); return (P)q;
1.1       noro     10500:     } else
                   10501:         return (P)c.p;
                   10502: }
                   10503:
                   10504: /* [0,0,0,cont] = p -> p/cont */
                   10505:
                   10506: void finalize_tracelist(int i,P cont)
                   10507: {
                   10508:  LIST l;
                   10509:  NODE node;
                   10510:  Z iq;
                   10511:
                   10512:   if ( !UNIQ(cont) ) {
                   10513:     node = mknode(4,NULLP,NULLP,NULLP,cont);
                   10514:     MKLIST(l,node); MKNODE(node,l,nd_tracelist);
                   10515:     nd_tracelist = node;
                   10516:   }
1.6       noro     10517:   STOZ(i,iq);
1.1       noro     10518:   nd_tracelist = reverse_node(nd_tracelist);
                   10519:   MKLIST(l,nd_tracelist);
                   10520:   node = mknode(2,iq,l); MKLIST(l,node);
                   10521:   MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
                   10522:   nd_alltracelist = node; nd_tracelist = 0;
                   10523: }
                   10524:
                   10525: void conv_ilist(int demand,int trace,NODE g,int **indp)
                   10526: {
                   10527:     int n,i,j;
                   10528:   int *ind;
                   10529:   NODE t;
                   10530:
                   10531:     n = length(g);
                   10532:   ind = (int *)MALLOC(n*sizeof(int));
                   10533:   for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
                   10534:     j = (long)BDY(t); ind[i] = j;
                   10535:     BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
                   10536:   }
                   10537:   if ( indp ) *indp = ind;
                   10538: }
                   10539:
1.30      noro     10540: NODE conv_ilist_s(int demand,int trace,int **indp)
                   10541: {
1.65    ! noro     10542:   int n,i;
1.30      noro     10543:   int *ind;
                   10544:   NODE g0,g;
                   10545:
                   10546:   n = nd_psn;
                   10547:   ind = (int *)MALLOC(n*sizeof(int));
                   10548:   g0 = 0;
                   10549:   for ( i = 0; i < n; i++ ) {
                   10550:     ind[i] = i;
                   10551:     NEXTNODE(g0,g);
                   10552:     BDY(g) = (pointer)(demand?ndv_load(i):(trace?nd_ps_trace[i]:nd_ps[i]));
                   10553:   }
                   10554:   if ( g0 ) NEXT(g) = 0;
                   10555:   if ( indp ) *indp = ind;
                   10556:   return g0;
                   10557: }
                   10558:
1.46      noro     10559: void parse_nd_option(VL vl,NODE opt)
1.1       noro     10560: {
1.38      noro     10561:   NODE t,p,u;
1.1       noro     10562:   int i,s,n;
1.38      noro     10563:   char *key;
                   10564:   Obj value;
1.46      noro     10565:   VL oldvl;
1.1       noro     10566:
1.41      noro     10567:   nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_norb = 0; nd_gbblock = 0;
1.1       noro     10568:   nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
                   10569:   nd_splist = 0; nd_check_splist = 0;
1.38      noro     10570:   nd_sugarweight = 0; nd_f4red =0; nd_rank0 = 0;
                   10571:   nd_f4_td = 0; nd_sba_f4step = 2; nd_sba_pot = 0; nd_sba_largelcm = 0;
1.41      noro     10572:   nd_sba_dontsort = 0; nd_top = 0; nd_sba_redundant_check = 0;
1.46      noro     10573:   nd_sba_syz = 0; nd_sba_modord = 0; nd_sba_inputisgb = 0;
1.63      noro     10574:   nd_hpdata = 0; nd_sba_heu = 0;
1.39      noro     10575:
1.38      noro     10576:   for ( t = opt; t; t = NEXT(t) ) {
                   10577:     p = BDY((LIST)BDY(t));
                   10578:     key = BDY((STRING)BDY(p));
                   10579:     value = (Obj)BDY(NEXT(p));
                   10580:     if ( !strcmp(key,"gentrace") )
                   10581:       nd_gentrace = value?1:0;
                   10582:     else if ( !strcmp(key,"gensyz") )
                   10583:       nd_gensyz = value?1:0;
                   10584:     else if ( !strcmp(key,"nora") )
                   10585:       nd_nora = value?1:0;
1.41      noro     10586:     else if ( !strcmp(key,"norb") )
                   10587:       nd_norb = value?1:0;
1.38      noro     10588:     else if ( !strcmp(key,"gbblock") ) {
                   10589:       if ( value && OID(value) == O_LIST ) {
1.1       noro     10590:         u = BDY((LIST)value);
1.38      noro     10591:         nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.1       noro     10592:         for ( i = 0; u; u = NEXT(u) ) {
                   10593:           p = BDY((LIST)BDY(u));
1.65    ! noro     10594:           s = nd_gbblock[i++] = (int)ZTOS((Q)BDY(p));
        !          10595:           nd_gbblock[i++] = (int)(s+ZTOS((Q)BDY(NEXT(p)))-1);
1.1       noro     10596:         }
                   10597:         nd_gbblock[i] = -1;
1.38      noro     10598:       } else
                   10599:         nd_gbblock = 0;
1.1       noro     10600:     } else if ( !strcmp(key,"newelim") )
                   10601:             nd_newelim = value?1:0;
                   10602:     else if ( !strcmp(key,"intersect") )
                   10603:             nd_intersect = value?1:0;
1.17      noro     10604:     else if ( !strcmp(key,"syzgen") )
1.65    ! noro     10605:             nd_intersect = (int)ZTOS((Q)value);
1.1       noro     10606:     else if ( !strcmp(key,"lf") )
                   10607:             nd_lf = value?1:0;
                   10608:     else if ( !strcmp(key,"trace") ) {
1.38      noro     10609:       if ( value ) {
                   10610:         u = BDY((LIST)value);
                   10611:         nd_nzlist = BDY((LIST)ARG2(u));
1.65    ! noro     10612:         nd_bpe = (int)ZTOS((Q)ARG3(u));
1.38      noro     10613:       }
1.1       noro     10614:     } else if ( !strcmp(key,"f4red") ) {
1.65    ! noro     10615:       nd_f4red = (int)ZTOS((Q)value);
1.1       noro     10616:     } else if ( !strcmp(key,"rank0") ) {
1.38      noro     10617:       nd_rank0 = value?1:0;
1.1       noro     10618:     } else if ( !strcmp(key,"splist") ) {
1.38      noro     10619:       nd_splist = value?1:0;
1.1       noro     10620:     } else if ( !strcmp(key,"check_splist") ) {
                   10621:       nd_check_splist = BDY((LIST)value);
1.49      noro     10622:     } else if ( !strcmp(key,"hpdata") ) {
1.50      noro     10623:       if ( value )
                   10624:         nd_hpdata = BDY((LIST)value);
1.1       noro     10625:     } else if ( !strcmp(key,"sugarweight") ) {
                   10626:       u = BDY((LIST)value);
1.38      noro     10627:       n = length(u);
                   10628:       nd_sugarweight = MALLOC(n*sizeof(int));
1.1       noro     10629:       for ( i = 0; i < n; i++, u = NEXT(u) )
1.65    ! noro     10630:         nd_sugarweight[i] = (int)ZTOS((Q)BDY(u));
1.38      noro     10631:     } else if ( !strcmp(key,"f4_td") ) {
                   10632:       nd_f4_td = value?1:0;
                   10633:     } else if ( !strcmp(key,"sba_f4step") ) {
1.65    ! noro     10634:       nd_sba_f4step = value?(int)ZTOS((Q)value):0;
1.38      noro     10635:     } else if ( !strcmp(key,"sba_pot") ) {
1.65    ! noro     10636:       nd_sba_pot = (int)ZTOS((Q)value);
1.38      noro     10637:     } else if ( !strcmp(key,"sba_largelcm") ) {
                   10638:       nd_sba_largelcm = value?1:0;
1.39      noro     10639:     } else if ( !strcmp(key,"sba_dontsort") ) {
                   10640:       nd_sba_dontsort = value?1:0;
1.44      noro     10641:     } else if ( !strcmp(key,"sba_syz") ) {
                   10642:       nd_sba_syz = value?1:0;
1.63      noro     10643:     } else if ( !strcmp(key,"sba_heu") ) {
                   10644:       nd_sba_heu = value?1:0;
1.46      noro     10645:     } else if ( !strcmp(key,"sba_modord") ) {
                   10646:       // value=[vlist,ordspec,weight]
                   10647:       u = BDY((LIST)value);
                   10648:       pltovl((LIST)ARG0(u),&oldvl);
                   10649:       nd_sba_modord = create_comp_sig_spec(vl,oldvl,(Obj)ARG1(u),argc(u)==3?ARG2(u):0);
                   10650:     } else if ( !strcmp(key,"sba_gbinput") ) {
                   10651:       nd_sba_inputisgb = value?1:0;
                   10652:       if ( nd_sba_inputisgb != 0 ) {
                   10653:         // value=[vlist,ordspec,weight]
                   10654:         u = BDY((LIST)value);
                   10655:         pltovl((LIST)ARG0(u),&oldvl);
                   10656:         nd_sba_modord = create_comp_sig_spec(vl,oldvl,(Obj)ARG1(u),argc(u)==3?ARG2(u):0);
                   10657:       }
1.41      noro     10658:     } else if ( !strcmp(key,"sba_redundant_check") ) {
                   10659:       nd_sba_redundant_check = value?1:0;
1.39      noro     10660:     } else if ( !strcmp(key,"top") ) {
                   10661:       nd_top = value?1:0;
1.1       noro     10662:     }
1.38      noro     10663:   }
1.44      noro     10664:   if ( nd_sba_syz ) nd_sba_dontsort = 1;
1.1       noro     10665: }
                   10666:
                   10667: ND mdptond(DP d);
                   10668: ND nd_mul_nm(int mod,NM m0,ND p);
                   10669: ND nd_mul_nm_lf(NM m0,ND p);
                   10670: ND *btog(NODE ti,ND **p,int nb,int mod);
                   10671: ND btog_one(NODE ti,ND *p,int nb,int mod);
                   10672: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
                   10673: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
                   10674:
                   10675: /* d:monomial */
                   10676: ND mdptond(DP d)
                   10677: {
                   10678:   NM m;
                   10679:   ND r;
                   10680:
                   10681:   if ( OID(d) == 1 )
                   10682:   r = ptond(CO,CO,(P)d);
                   10683:   else {
                   10684:     NEWNM(m);
                   10685:     dltondl(NV(d),BDY(d)->dl,DL(m));
1.6       noro     10686:     CZ(m) = (Z)BDY(d)->c;
1.1       noro     10687:     NEXT(m) = 0;
                   10688:     MKND(NV(d),m,1,r);
                   10689:   }
                   10690:     return r;
                   10691: }
                   10692:
                   10693: ND nd_mul_nm(int mod,NM m0,ND p)
                   10694: {
                   10695:   UINT *d0;
                   10696:   int c0,c1,c;
                   10697:   NM tm,mr,mr0;
                   10698:   ND r;
                   10699:
                   10700:   if ( !p ) return 0;
                   10701:   d0 = DL(m0);
                   10702:   c0 = CM(m0);
                   10703:   mr0 = 0;
                   10704:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   10705:     NEXTNM(mr0,mr);
                   10706:   c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
                   10707:   ndl_add(d0,DL(tm),DL(mr));
                   10708:   }
                   10709:   NEXT(mr) = 0;
                   10710:   MKND(NV(p),mr0,LEN(p),r);
                   10711:   return r;
                   10712: }
                   10713:
                   10714: ND nd_mul_nm_lf(NM m0,ND p)
                   10715: {
                   10716:   UINT *d0;
1.65    ! noro     10717:   Z c0,c1;
1.1       noro     10718:   NM tm,mr,mr0;
                   10719:   ND r;
                   10720:
                   10721:   if ( !p ) return 0;
                   10722:   d0 = DL(m0);
                   10723:   c0 = CZ(m0);
                   10724:   mr0 = 0;
                   10725:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   10726:     NEXTNM(mr0,mr);
1.65    ! noro     10727:     mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
1.1       noro     10728:     ndl_add(d0,DL(tm),DL(mr));
                   10729:   }
                   10730:   NEXT(mr) = 0;
                   10731:   MKND(NV(p),mr0,LEN(p),r);
                   10732:   return r;
                   10733: }
                   10734:
                   10735: ND *btog(NODE ti,ND **p,int nb,int mod)
                   10736: {
                   10737:   PGeoBucket *r;
                   10738:   int i,ci;
                   10739:   NODE t,s;
                   10740:   ND m,tp;
                   10741:   ND *pi,*rd;
                   10742:   P c;
                   10743:
                   10744:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   10745:   for ( i = 0; i < nb; i++ )
                   10746:     r[i] = create_pbucket();
                   10747:   for ( t = ti; t; t = NEXT(t) ) {
                   10748:   s = BDY((LIST)BDY(t));
                   10749:     if ( ARG0(s) ) {
                   10750:     m = mdptond((DP)ARG2(s));
1.6       noro     10751:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     10752:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   10753:       HCM(m) = ci;
1.6       noro     10754:       pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     10755:       for ( i = 0; i < nb; i++ ) {
                   10756:       tp = nd_mul_nm(mod,BDY(m),pi[i]);
                   10757:         add_pbucket(mod,r[i],tp);
                   10758:       }
                   10759:     }
                   10760:     ci = 1;
                   10761:     } else {
                   10762:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   10763:     ci = invm(ci,mod);
                   10764:   }
                   10765:   }
                   10766:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   10767:   for ( i = 0; i < nb; i++ )
                   10768:   rd[i] = normalize_pbucket(mod,r[i]);
                   10769:   if ( ci != 1 )
                   10770:     for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
                   10771:    return rd;
                   10772: }
                   10773:
                   10774: /* YYY */
                   10775: ND *btog_lf(NODE ti,ND **p,int nb)
                   10776: {
                   10777:   PGeoBucket *r;
                   10778:   int i;
                   10779:   NODE t,s;
                   10780:   ND m,tp;
                   10781:   ND *pi,*rd;
                   10782:   LM lm;
                   10783:   Z lf,c;
                   10784:
                   10785:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   10786:   for ( i = 0; i < nb; i++ )
                   10787:     r[i] = create_pbucket();
                   10788:   for ( t = ti; t; t = NEXT(t) ) {
                   10789:     s = BDY((LIST)BDY(t));
                   10790:     if ( ARG0(s) ) {
                   10791:       m = mdptond((DP)ARG2(s));
1.6       noro     10792:       simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1       noro     10793:       if ( lm ) {
                   10794:         lmtolf(lm,&lf); HCZ(m) = lf;
1.6       noro     10795:         pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     10796:         for ( i = 0; i < nb; i++ ) {
                   10797:           tp = nd_mul_nm_lf(BDY(m),pi[i]);
                   10798:           add_pbucket(-2,r[i],tp);
                   10799:         }
                   10800:       }
                   10801:       c = ONE;
                   10802:     } else {
                   10803:       simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
                   10804:     }
                   10805:   }
                   10806:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   10807:   for ( i = 0; i < nb; i++ )
                   10808:     rd[i] = normalize_pbucket(-2,r[i]);
                   10809:   for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
                   10810:   return rd;
                   10811: }
                   10812:
                   10813: ND btog_one(NODE ti,ND *p,int nb,int mod)
                   10814: {
                   10815:   PGeoBucket r;
1.65    ! noro     10816:   int ci,j;
1.1       noro     10817:   NODE t,s;
                   10818:   ND m,tp;
                   10819:   ND pi,rd;
                   10820:   P c;
                   10821:
                   10822:   r = create_pbucket();
                   10823:   for ( t = ti; t; t = NEXT(t) ) {
                   10824:   s = BDY((LIST)BDY(t));
                   10825:     if ( ARG0(s) ) {
                   10826:     m = mdptond((DP)ARG2(s));
1.6       noro     10827:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     10828:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   10829:       HCM(m) = ci;
1.65    ! noro     10830:       pi = p[j=(int)ZTOS((Q)ARG1(s))];
1.1       noro     10831:     if ( !pi ) {
                   10832:       pi = nd_load_mod(j);
                   10833:       tp = nd_mul_nm(mod,BDY(m),pi);
                   10834:       nd_free(pi);
                   10835:         add_pbucket(mod,r,tp);
                   10836:     } else {
                   10837:       tp = nd_mul_nm(mod,BDY(m),pi);
                   10838:         add_pbucket(mod,r,tp);
                   10839:       }
                   10840:     }
                   10841:     ci = 1;
                   10842:     } else {
                   10843:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   10844:     ci = invm(ci,mod);
                   10845:   }
                   10846:   }
                   10847:   rd = normalize_pbucket(mod,r);
                   10848:   free_pbucket(r);
                   10849:   if ( ci != 1 ) nd_mul_c(mod,rd,ci);
                   10850:   return rd;
                   10851: }
                   10852:
                   10853: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
                   10854:
                   10855: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
                   10856: {
                   10857:   int i,j,n,m,nb,pi0,pi1,nvar;
                   10858:   VL fv,tv,vv;
                   10859:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   10860:   ND **p;
                   10861:   ND *c;
                   10862:   ND u;
                   10863:   P inv;
                   10864:   MAT mat;
                   10865:
                   10866:   if ( mod == -2 )
                   10867:     return nd_btog_lf(f,v,ord,tlist,rp);
                   10868:
                   10869:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     10870:   parse_nd_option(vv,current_option);
1.1       noro     10871:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   10872:   switch ( ord->id ) {
                   10873:     case 1:
                   10874:       if ( ord->nv != nvar )
                   10875:         error("nd_check : invalid order specification");
                   10876:       break;
                   10877:     default:
                   10878:       break;
                   10879:   }
                   10880:   nd_init_ord(ord);
                   10881: #if 0
1.6       noro     10882:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     10883: #else
                   10884:   nd_bpe = 32;
                   10885: #endif
                   10886:   nd_setup_parameters(nvar,0);
                   10887:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   10888:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   10889:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   10890:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   10891:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.65    ! noro     10892:     j = (int)ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     10893:   if ( j > i ) i = j;
                   10894:   }
                   10895:   n = i+1;
                   10896:   nb = length(BDY(f));
                   10897:   p = (ND **)MALLOC(n*sizeof(ND *));
                   10898:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   10899:     pi = BDY((LIST)BDY(t));
1.65    ! noro     10900:     pi0 = (int)ZTOS((Q)ARG0(pi)); pi1 = (int)ZTOS((Q)ARG1(pi));
1.1       noro     10901:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   10902:     ptomp(mod,(P)ARG2(pi),&inv);
                   10903:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   10904:     u = ptond(CO,vv,(P)ONE);
                   10905:     HCM(u) = ((MQ)inv)->cont;
                   10906:     c[pi1] = u;
                   10907:   }
                   10908:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   10909:     printf("%d ",i); fflush(stdout);
                   10910:     ti = BDY((LIST)BDY(t));
1.65    ! noro     10911:     p[j=(int)ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10912:   }
                   10913:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   10914:     printf("%d ",i); fflush(stdout);
                   10915:     ti = BDY((LIST)BDY(t));
1.65    ! noro     10916:     p[j=(int)ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10917:   }
                   10918:   m = length(ind);
                   10919:   MKMAT(mat,nb,m);
                   10920:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     10921:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     10922:       BDY(mat)[i][j] = ndtodp(mod,c[i]);
                   10923:   return mat;
                   10924: }
                   10925:
                   10926: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
                   10927: {
                   10928:   int i,j,n,m,nb,pi0,pi1,nvar;
                   10929:   VL fv,tv,vv;
                   10930:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   10931:   ND **p;
                   10932:   ND *c;
                   10933:   ND u;
                   10934:   MAT mat;
                   10935:   LM lm;
                   10936:   Z lf,inv;
                   10937:
                   10938:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     10939:   parse_nd_option(vv,current_option);
1.1       noro     10940:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   10941:   switch ( ord->id ) {
                   10942:     case 1:
                   10943:       if ( ord->nv != nvar )
                   10944:         error("nd_check : invalid order specification");
                   10945:       break;
                   10946:     default:
                   10947:       break;
                   10948:   }
                   10949:   nd_init_ord(ord);
                   10950: #if 0
1.6       noro     10951:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     10952: #else
                   10953:   nd_bpe = 32;
                   10954: #endif
                   10955:   nd_setup_parameters(nvar,0);
                   10956:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   10957:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   10958:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   10959:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   10960:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.65    ! noro     10961:     j = (int)ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     10962:   if ( j > i ) i = j;
                   10963:   }
                   10964:   n = i+1;
                   10965:   nb = length(BDY(f));
                   10966:   p = (ND **)MALLOC(n*sizeof(ND *));
                   10967:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   10968:     pi = BDY((LIST)BDY(t));
1.65    ! noro     10969:     pi0 = (int)ZTOS((Q)ARG0(pi)); pi1 = (int)ZTOS((Q)ARG1(pi));
1.1       noro     10970:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   10971:     simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
                   10972:     u = ptond(CO,vv,(P)ONE);
                   10973:     HCZ(u) = inv;
                   10974:     c[pi1] = u;
                   10975:   }
                   10976:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   10977:     printf("%d ",i); fflush(stdout);
                   10978:     ti = BDY((LIST)BDY(t));
1.65    ! noro     10979:     p[j=(int)ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     10980:   }
                   10981:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   10982:     printf("%d ",i); fflush(stdout);
                   10983:     ti = BDY((LIST)BDY(t));
1.65    ! noro     10984:     p[j=(int)ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     10985:   }
                   10986:   m = length(ind);
                   10987:   MKMAT(mat,nb,m);
                   10988:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     10989:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     10990:       BDY(mat)[i][j] = ndtodp(-2,c[i]);
                   10991:   return mat;
                   10992: }
                   10993:
                   10994: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
                   10995:   LIST tlist,int pos,MAT *rp)
                   10996: {
                   10997:   int i,j,n,m,nb,pi0,pi1,nvar;
                   10998:   VL fv,tv,vv;
                   10999:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   11000:   ND *p;
                   11001:   ND u;
                   11002:   P inv;
                   11003:   VECT vect;
                   11004:
                   11005:   if ( mod == -2 )
                   11006:     error("nd_btog_one : not implemented yet for a large finite field");
                   11007:
                   11008:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     11009:   parse_nd_option(vv,current_option);
1.1       noro     11010:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   11011:   switch ( ord->id ) {
                   11012:     case 1:
                   11013:       if ( ord->nv != nvar )
                   11014:         error("nd_check : invalid order specification");
                   11015:       break;
                   11016:     default:
                   11017:       break;
                   11018:   }
                   11019:   nd_init_ord(ord);
                   11020: #if 0
1.6       noro     11021:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     11022: #else
                   11023:   nd_bpe = 32;
                   11024: #endif
                   11025:   nd_setup_parameters(nvar,0);
                   11026:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   11027:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   11028:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   11029:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   11030:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.65    ! noro     11031:     j = (int)ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     11032:   if ( j > i ) i = j;
                   11033:   }
                   11034:   n = i+1;
                   11035:   nb = length(BDY(f));
                   11036:   p = (ND *)MALLOC(n*sizeof(ND *));
                   11037:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   11038:     pi = BDY((LIST)BDY(t));
1.65    ! noro     11039:   pi0 = (int)ZTOS((Q)ARG0(pi)); pi1 = (int)ZTOS((Q)ARG1(pi));
1.1       noro     11040:   if ( pi1 == pos ) {
                   11041:     ptomp(mod,(P)ARG2(pi),&inv);
                   11042:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   11043:     u = ptond(CO,vv,(P)ONE);
                   11044:     HCM(u) = ((MQ)inv)->cont;
                   11045:     p[pi0] = u;
                   11046:     }
                   11047:   }
                   11048:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   11049:   printf("%d ",i); fflush(stdout);
                   11050:     ti = BDY((LIST)BDY(t));
1.65    ! noro     11051:     p[j=(int)ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     11052:     if ( Demand ) {
                   11053:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   11054:   }
                   11055:   }
                   11056:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   11057:   printf("%d ",i); fflush(stdout);
                   11058:     ti = BDY((LIST)BDY(t));
1.65    ! noro     11059:     p[j=(int)ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     11060:     if ( Demand ) {
                   11061:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   11062:   }
                   11063:   }
                   11064:   m = length(ind);
                   11065:   MKVECT(vect,m);
                   11066:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6       noro     11067:   u = p[ZTOS((Q)BDY(t))];
1.1       noro     11068:   if ( !u ) {
1.65    ! noro     11069:     u = nd_load_mod((int)ZTOS((Q)BDY(t)));
1.1       noro     11070:     BDY(vect)[j] = ndtodp(mod,u);
                   11071:     nd_free(u);
                   11072:   } else
                   11073:     BDY(vect)[j] = ndtodp(mod,u);
                   11074:   }
                   11075:   return vect;
                   11076: }
                   11077:
                   11078: void ndv_print_lf(NDV p)
                   11079: {
                   11080:     NMV m;
                   11081:     int i,len;
                   11082:
                   11083:     if ( !p ) printf("0\n");
                   11084:     else {
                   11085:         len = LEN(p);
                   11086:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   11087:             printf("+");
                   11088:             mpz_out_str(asir_out,10,BDY(CZ(m)));
                   11089:             printf("*");
                   11090:             ndl_print(DL(m));
                   11091:         }
                   11092:         printf("\n");
                   11093:     }
                   11094: }
                   11095:
                   11096: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
                   11097: {
1.65    ! noro     11098:     VL tv,fv,vv;
        !          11099:     NODE fd,fd0,in0,in,r,t,s,cand;
        !          11100:     int m,nvar,mindex,e,max;
1.1       noro     11101:     NDV c;
                   11102:     NMV a;
                   11103:     EPOS oepos;
1.65    ! noro     11104:     int obpe,oadv,wmax,i,len,cbpe,ishomo,mrank,trank,ompos;
1.1       noro     11105:     struct order_spec *ord1;
                   11106:     struct oEGT eg_check,eg0,eg1;
                   11107:     int *perm;
1.65    ! noro     11108:     int ret;
1.1       noro     11109:     NODE retn;
                   11110:
                   11111:     nd_module = 0;
                   11112:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     11113:     parse_nd_option(vv,current_option);
1.1       noro     11114:     if ( nd_vc )
                   11115:       error("nd_f4_lf_trace : computation over a rational function field is not implemented");
                   11116:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   11117:     switch ( ord->id ) {
                   11118:         case 1:
                   11119:             if ( ord->nv != nvar )
                   11120:                 error("nd_f4_lf_trace : invalid order specification");
                   11121:             break;
                   11122:         default:
                   11123:             break;
                   11124:     }
                   11125:
                   11126:     nd_ntrans = nvar;
                   11127:     nd_nalg = 0;
                   11128:
1.65    ! noro     11129: //    nocheck = 0;
1.1       noro     11130:     mindex = 0;
                   11131:
                   11132:     /* do not use on-demand load/save */
                   11133:     nd_demand = 0;
                   11134:     m = trace > 1 ? trace : get_lprime(mindex);
                   11135:     nd_init_ord(ord);
                   11136:     mrank = 0;
                   11137:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   11138:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   11139:             if ( nd_module ) {
                   11140:                 s = BDY((LIST)BDY(t));
                   11141:                 trank = length(s);
                   11142:                 mrank = MAX(mrank,trank);
                   11143:                 for ( ; s; s = NEXT(s) ) {
                   11144:                     e = getdeg(tv->v,(P)BDY(s));
                   11145:                     max = MAX(e,max);
                   11146:                 }
                   11147:             } else {
                   11148:                 e = getdeg(tv->v,(P)BDY(t));
                   11149:                 max = MAX(e,max);
                   11150:             }
                   11151:         }
                   11152:     nd_setup_parameters(nvar,max);
                   11153:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   11154:     ishomo = 1;
                   11155:     /* XXX */
                   11156:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   11157:         if ( nd_module ) {
                   11158:             c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
                   11159:         } else {
                   11160:             c = (pointer)ptondv(CO,vv,(P)BDY(t));
                   11161:         }
                   11162:         if ( ishomo )
                   11163:             ishomo = ishomo && ndv_ishomo(c);
                   11164:         if ( c ) {
                   11165:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   11166:             ndv_mod(-2,c);
                   11167:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   11168:         }
                   11169:     }
                   11170:     if ( in0 ) NEXT(in) = 0;
                   11171:     if ( fd0 ) NEXT(fd) = 0;
                   11172:     if ( !ishomo && homo ) {
                   11173:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   11174:             c = (NDV)BDY(t); len = LEN(c);
                   11175:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
1.65    ! noro     11176:                 wmax = MAX((int)TD(DL(a)),wmax);
1.1       noro     11177:         }
                   11178:         homogenize_order(ord,nvar,&ord1);
                   11179:         nd_init_ord(ord1);
                   11180:         nd_setup_parameters(nvar+1,wmax);
                   11181:         for ( t = fd0; t; t = NEXT(t) )
                   11182:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   11183:     }
1.65    ! noro     11184: //    if ( MaxDeg > 0 ) nocheck = 1;
1.24      noro     11185:     ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1       noro     11186:     if ( ret )
                   11187:       cand = nd_f4_lf_trace_main(m,&perm);
                   11188:     if ( !ret || !cand ) {
                   11189:        *rp = 0; return;
                   11190:     }
                   11191:     if ( !ishomo && homo ) {
                   11192:       /* dehomogenization */
                   11193:       for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   11194:       nd_init_ord(ord);
                   11195:       nd_setup_parameters(nvar,0);
                   11196:     }
                   11197:     cand = ndv_reducebase(cand,perm);
                   11198:     cand = ndv_reduceall(-2,cand);
                   11199:     cbpe = nd_bpe;
                   11200:     get_eg(&eg0);
                   11201:     if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   11202:       /* gbcheck : cand is a GB of Id(cand) ? */
                   11203:       retn = nd_f4(-2,0,0);
1.65    ! noro     11204:       if ( !retn ) {
        !          11205:         /* failure */
        !          11206:         *rp = 0; return;
        !          11207:       }
1.1       noro     11208:     }
                   11209:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   11210:     if ( DP_Print )
1.5       noro     11211:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     11212:     /* dp->p */
                   11213:     nd_bpe = cbpe;
                   11214:     nd_setup_parameters(nd_nvar,0);
                   11215:     for ( r = cand; r; r = NEXT(r) ) {
                   11216:       if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
                   11217:       else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
                   11218:     }
                   11219:     MKLIST(*rp,cand);
                   11220: }
                   11221:
                   11222: NODE nd_f4_lf_trace_main(int m,int **indp)
                   11223: {
1.65    ! noro     11224:     int i,nh,stat;
1.1       noro     11225:     NODE r,rm,g;
                   11226:     ND_pairs d,l,l0,t;
1.65    ! noro     11227:     NDV nfqv,nfv;
        !          11228:     NODE rp0,nflist,nflist_lf;
        !          11229:     UINT *s0vect;
        !          11230:     int col;
1.1       noro     11231:     int sugar;
                   11232:     PGeoBucket bucket;
                   11233:     struct oEGT eg0,eg1,eg_f4;
                   11234:
                   11235:     g = 0; d = 0;
                   11236:     for ( i = 0; i < nd_psn; i++ ) {
                   11237:         d = update_pairs(d,g,i,0);
                   11238:         g = update_base(g,i);
                   11239:     }
                   11240:     while ( d ) {
                   11241:         get_eg(&eg0);
                   11242:         l = nd_minsugarp(d,&d);
                   11243:         sugar = SG(l);
                   11244:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   11245:         bucket = create_pbucket();
                   11246:         stat = nd_sp_f4(m,0,l,bucket);
                   11247:         if ( !stat ) {
                   11248:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11249:             NEXT(t) = d; d = l;
                   11250:             d = nd_reconstruct(1,d);
                   11251:             continue;
                   11252:         }
                   11253:         if ( bucket->m < 0 ) continue;
                   11254:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   11255:         if ( !col ) {
                   11256:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11257:             NEXT(t) = d; d = l;
                   11258:             d = nd_reconstruct(1,d);
                   11259:             continue;
                   11260:         }
                   11261:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   11262:         if ( DP_Print )
1.5       noro     11263:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1       noro     11264:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   11265:         if ( !l0 ) continue;
                   11266:         l = l0;
                   11267:
                   11268:         /* over LF */
                   11269:         bucket = create_pbucket();
                   11270:         stat = nd_sp_f4(-2,1,l,bucket);
                   11271:         if ( !stat ) {
                   11272:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11273:             NEXT(t) = d; d = l;
                   11274:             d = nd_reconstruct(1,d);
                   11275:             continue;
                   11276:         }
                   11277:         if ( bucket->m < 0 ) continue;
                   11278:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   11279:         if ( !col ) {
                   11280:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11281:             NEXT(t) = d; d = l;
                   11282:             d = nd_reconstruct(1,d);
                   11283:             continue;
                   11284:         }
                   11285:         nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
                   11286:         /* adding new bases */
                   11287:         for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
                   11288:             nfv = (NDV)BDY(rm);
                   11289:             nfqv = (NDV)BDY(r);
                   11290:             if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
                   11291:             ndv_removecont(m,nfv);
                   11292:             ndv_removecont(-2,nfqv);
1.24      noro     11293:             nh = ndv_newps(-2,nfv,nfqv);
1.1       noro     11294:             d = update_pairs(d,g,nh,0);
                   11295:             g = update_base(g,nh);
                   11296:         }
                   11297:         if ( r || rm ) return 0;
                   11298:     }
                   11299:   conv_ilist(nd_demand,1,g,indp);
                   11300:     return g;
                   11301: }
                   11302:
1.7       noro     11303: #if SIZEOF_LONG==8
                   11304:
                   11305: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   11306: {
                   11307:     int j,k,len;
                   11308:     UINT *p;
                   11309:     UINT c;
                   11310:     NDV r;
                   11311:     NMV mr0,mr;
                   11312:
                   11313:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   11314:     if ( !len ) return 0;
                   11315:     else {
                   11316:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   11317: #if 0
                   11318:         ndv_alloc += nmv_adv*len;
                   11319: #endif
                   11320:         mr = mr0;
                   11321:         p = s0vect;
                   11322:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   11323:             if ( !rhead[j] ) {
                   11324:                 if ( (c = (UINT)vect[k++]) != 0 ) {
                   11325:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   11326:                 }
                   11327:             }
                   11328:         MKNDV(nd_nvar,mr0,len,r);
                   11329:         return r;
                   11330:     }
                   11331: }
                   11332:
1.28      noro     11333: NDV vect64_to_ndv_s(mp_limb_t *vect,int col,UINT *s0vect)
                   11334: {
                   11335:     int j,k,len;
                   11336:     UINT *p;
                   11337:     UINT c;
                   11338:     NDV r;
                   11339:     NMV mr0,mr;
                   11340:
                   11341:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   11342:     if ( !len ) return 0;
                   11343:     else {
                   11344:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   11345:         mr = mr0;
                   11346:         p = s0vect;
                   11347:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   11348:           if ( (c = (UINT)vect[k++]) != 0 ) {
                   11349:             ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   11350:           }
                   11351:         MKNDV(nd_nvar,mr0,len,r);
                   11352:         return r;
                   11353:     }
                   11354: }
                   11355:
1.7       noro     11356: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
                   11357: {
                   11358:     NM m;
1.11      noro     11359:     UINT *t,*s,*u;
                   11360:     int i,st,ed,md,prev,c;
1.7       noro     11361:
                   11362:     for ( i = 0; i < n; i++ ) r[i] = 0;
1.11      noro     11363:     prev = 0;
                   11364:     for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
                   11365:       t = DL(m);
                   11366:       st = prev;
                   11367:       ed = n;
                   11368:       while ( ed > st ) {
                   11369:         md = (st+ed)/2;
                   11370:         u = s0+md*nd_wpd;
                   11371:         c = DL_COMPARE(u,t);
                   11372:         if ( c == 0 ) break;
                   11373:         else if ( c > 0 ) st = md;
                   11374:         else ed = md;
                   11375:       }
                   11376:       r[md] = (mp_limb_t)CM(m);
                   11377:       prev = md;
1.7       noro     11378:     }
                   11379:     for ( i = 0; !r[i]; i++ );
                   11380:     return i;
                   11381: }
                   11382:
                   11383: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
                   11384:
1.28      noro     11385: 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     11386: {
                   11387:     int i,j,k,len,pos,prev;
                   11388:     mp_limb_t a,c,c1,c2;
                   11389:     IndArray ivect;
                   11390:     unsigned char *ivc;
                   11391:     unsigned short *ivs;
                   11392:     unsigned int *ivi;
                   11393:     NDV redv;
                   11394:     NMV mr;
                   11395:     NODE rp;
                   11396:     int maxrs;
                   11397:
                   11398:     for ( i = 0; i < col; i++ ) cvect[i] = 0;
                   11399:     maxrs = 0;
                   11400:     for ( i = 0; i < nred; i++ ) {
                   11401:         ivect = imat[i];
                   11402:         k = ivect->head;
                   11403:         a = svect[k]; c = cvect[k];
                   11404:         MOD128(a,c,m);
                   11405:         svect[k] = a; cvect[k] = 0;
1.28      noro     11406:         if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.11      noro     11407:             Nf4_red++;
1.7       noro     11408:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   11409:             c = m-c; redv = nd_ps[rp0[i]->index];
                   11410:             len = LEN(redv); mr = BDY(redv);
                   11411:             svect[k] = 0; prev = k;
                   11412:             switch ( ivect->width ) {
                   11413:                 case 1:
                   11414:                     ivc = ivect->index.c;
                   11415:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   11416:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12      noro     11417:                         c2 = svect[pos]+c1*c;
                   11418:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   11419:                         svect[pos] = c2;
1.7       noro     11420:                     }
                   11421:                     break;
                   11422:                 case 2:
                   11423:                     ivs = ivect->index.s;
                   11424:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   11425:                         pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12      noro     11426:                         c2 = svect[pos]+c1*c;
                   11427:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   11428:                         svect[pos] = c2;
1.7       noro     11429:                     }
                   11430:                     break;
                   11431:                 case 4:
                   11432:                     ivi = ivect->index.i;
                   11433:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   11434:                         pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12      noro     11435:                         c2 = svect[pos]+c1*c;
                   11436:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   11437:                         svect[pos] = c2;
1.7       noro     11438:                     }
                   11439:                     break;
                   11440:             }
                   11441:         }
                   11442:     }
                   11443:     for ( i = 0; i < col; i++ ) {
                   11444:       a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
                   11445:     }
                   11446:     return maxrs;
                   11447: }
                   11448:
                   11449: /* for Fp, 2^15=<p<2^29 */
                   11450:
                   11451: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   11452:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   11453: {
                   11454:     int spcol,sprow,a;
                   11455:     int i,j,k,l,rank;
                   11456:     NODE r0,r;
                   11457:     ND_pairs sp;
                   11458:     ND spol;
                   11459:     mp_limb_t **spmat;
                   11460:     mp_limb_t *svect,*cvect;
                   11461:     mp_limb_t *v;
                   11462:     int *colstat;
                   11463:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   11464:     int maxrs;
                   11465:     int *spsugar;
                   11466:     ND_pairs *spactive;
                   11467:
                   11468:     spcol = col-nred;
                   11469:     get_eg(&eg0);
                   11470:     /* elimination (1st step) */
                   11471:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   11472:     svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11473:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11474:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   11475:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   11476:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   11477:         nd_sp(m,0,sp,&spol);
                   11478:         if ( !spol ) continue;
                   11479:         nd_to_vect64(m,s0vect,col,spol,svect);
1.28      noro     11480:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,0);
1.7       noro     11481:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   11482:         if ( i < col ) {
                   11483:             spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
                   11484:             for ( j = k = 0; j < col; j++ )
                   11485:                 if ( !rhead[j] ) v[k++] = (UINT)svect[j];
                   11486:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   11487:             if ( nz )
                   11488:             spactive[sprow] = sp;
                   11489:             sprow++;
                   11490:         }
                   11491:         nd_free(spol);
                   11492:     }
1.12      noro     11493:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7       noro     11494:     if ( DP_Print ) {
                   11495:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   11496:         fflush(asir_out);
                   11497:     }
                   11498:     /* free index arrays */
                   11499:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   11500:
                   11501:     /* elimination (2nd step) */
                   11502:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   11503:     rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   11504:     r0 = 0;
                   11505:     for ( i = 0; i < rank; i++ ) {
                   11506:         NEXTNODE(r0,r); BDY(r) =
                   11507:           (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   11508:         SG((NDV)BDY(r)) = spsugar[i];
                   11509:         GCFREE(spmat[i]);
                   11510:     }
                   11511:     if ( r0 ) NEXT(r) = 0;
                   11512:
                   11513:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12      noro     11514:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7       noro     11515:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   11516:     if ( DP_Print ) {
                   11517:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   11518:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   11519:             nsp,nred,sprow,spcol,rank);
                   11520:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   11521:     }
                   11522:     if ( nz ) {
                   11523:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   11524:         if ( rank > 0 ) {
                   11525:             NEXT(spactive[rank-1]) = 0;
                   11526:             *nz = spactive[0];
                   11527:         } else
                   11528:             *nz = 0;
                   11529:     }
                   11530:     return r0;
                   11531: }
                   11532:
                   11533: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   11534: {
                   11535:   int i,j,k,l,rank,s;
                   11536:   mp_limb_t inv;
                   11537:   mp_limb_t a;
                   11538:   UINT c;
                   11539:   mp_limb_t *t,*pivot,*pk;
                   11540:   UINT *ck;
                   11541:   UINT **cmat;
                   11542:   UINT *ct;
                   11543:   ND_pairs pair;
                   11544:
                   11545:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   11546:   for ( i = 0; i < row; i++ ) {
                   11547:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   11548:     bzero(cmat[i],col*sizeof(UINT));
                   11549:   }
                   11550:
                   11551:   for ( rank = 0, j = 0; j < col; j++ ) {
                   11552:     for ( i = rank; i < row; i++ ) {
                   11553:       a = mat[i][j]; c = cmat[i][j];
                   11554:       MOD128(a,c,md);
                   11555:       mat[i][j] = a; cmat[i][j] = 0;
                   11556:     }
                   11557:     for ( i = rank; i < row; i++ )
                   11558:       if ( mat[i][j] )
                   11559:         break;
                   11560:     if ( i == row ) {
                   11561:       colstat[j] = 0;
                   11562:       continue;
                   11563:     } else
                   11564:       colstat[j] = 1;
                   11565:     if ( i != rank ) {
                   11566:       t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   11567:       ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
                   11568:       s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   11569:       if ( spactive ) {
                   11570:         pair = spactive[i]; spactive[i] = spactive[rank];
                   11571:         spactive[rank] = pair;
                   11572:       }
                   11573:     }
                   11574:     /* column j is normalized */
                   11575:     s = sugar[rank];
                   11576:     inv = invm((UINT)mat[rank][j],md);
                   11577:     /* normalize pivot row */
                   11578:     for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
                   11579:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   11580:     }
                   11581:     for ( i = rank+1; i < row; i++ ) {
                   11582:       if ( (a = mat[i][j]) != 0 ) {
                   11583:         sugar[i] = MAX(sugar[i],s);
                   11584:         red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11      noro     11585:         Nf4_red++;
1.7       noro     11586:       }
                   11587:     }
                   11588:     rank++;
                   11589:   }
                   11590:   for ( j = col-1, l = rank-1; j >= 0; j-- )
                   11591:     if ( colstat[j] ) {
                   11592:       for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
                   11593:         a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
                   11594:       }
                   11595:       s = sugar[l];
                   11596:       for ( i = 0; i < l; i++ ) {
                   11597:         a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
                   11598:         if ( a ) {
                   11599:           sugar[i] = MAX(sugar[i],s);
                   11600:           red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11      noro     11601:           Nf4_red++;
1.7       noro     11602:         }
                   11603:       }
                   11604:       l--;
                   11605:     }
                   11606:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   11607:   GCFREE(cmat);
                   11608:   return rank;
                   11609: }
                   11610:
1.28      noro     11611: 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)
                   11612: {
                   11613:   int i,j,k,l,rank,s,imin;
                   11614:   mp_limb_t inv;
                   11615:   mp_limb_t a;
                   11616:   UINT c;
                   11617:   mp_limb_t *t,*pivot,*pk;
                   11618:   UINT *ck;
                   11619:   UINT **cmat;
                   11620:   UINT *ct;
                   11621:   ND_pairs pair;
                   11622:   SIG sg;
1.31      noro     11623:   int *used;
1.28      noro     11624:
1.31      noro     11625:   used = (int *)MALLOC(row*sizeof(int));
1.28      noro     11626:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   11627:   for ( i = 0; i < row; i++ ) {
                   11628:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   11629:     bzero(cmat[i],col*sizeof(UINT));
                   11630:   }
                   11631:
1.31      noro     11632:   for ( j = 0; j < col; j++ ) {
                   11633:     for ( i = 0; i < row; i++ ) {
1.28      noro     11634:       a = mat[i][j]; c = cmat[i][j];
                   11635:       MOD128(a,c,md);
                   11636:       mat[i][j] = a; cmat[i][j] = 0;
                   11637:     }
1.31      noro     11638:     for ( i = 0; i < row; i++ )
                   11639:       if ( !used[i] && mat[i][j] ) break;
                   11640:     if ( i == row ) {
1.28      noro     11641:       colstat[j] = 0;
                   11642:       continue;
1.31      noro     11643:     } else {
1.28      noro     11644:       colstat[j] = 1;
1.31      noro     11645:       used[i] = 1;
1.28      noro     11646:     }
                   11647:     /* column j is normalized */
1.31      noro     11648:     s = sugar[i];
                   11649:     inv = invm((UINT)mat[i][j],md);
1.28      noro     11650:     /* normalize pivot row */
1.31      noro     11651:     for ( k = j, pk = mat[i]+j, ck = cmat[i]+j; k < col; k++, pk++, ck++ ) {
1.28      noro     11652:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   11653:     }
1.31      noro     11654:     for ( k = i+1; k < row; k++ ) {
                   11655:       if ( (a = mat[k][j]) != 0 ) {
                   11656:         sugar[k] = MAX(sugar[k],s);
                   11657:         red_by_vect64(md,mat[k]+j,cmat[k]+j,mat[i]+j,(int)(md-a),col-j);
1.28      noro     11658:         Nf4_red++;
                   11659:       }
                   11660:     }
                   11661:   }
1.31      noro     11662:   rank = 0;
                   11663:   for ( i = 0; i < row; i++ ) {
                   11664:     for ( j = 0; j < col; j++ )
                   11665:       if ( mat[i][j] ) break;
                   11666:     if ( j == col ) sugar[i] = -1;
                   11667:     else rank++;
                   11668:   }
1.28      noro     11669:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   11670:   GCFREE(cmat);
                   11671:   return rank;
                   11672: }
                   11673:
                   11674: NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   11675:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
                   11676: {
                   11677:     int spcol,sprow,a;
                   11678:     int i,j,k,l,rank;
                   11679:     NODE r0,r;
                   11680:     ND_pairs sp;
                   11681:     ND spol;
                   11682:     mp_limb_t **spmat;
                   11683:     mp_limb_t *svect,*cvect;
                   11684:     mp_limb_t *v;
                   11685:     int *colstat;
                   11686:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   11687:     int maxrs;
                   11688:     int *spsugar;
                   11689:     ND_pairs *spactive;
                   11690:     SIG *spsig;
                   11691:
                   11692:     get_eg(&eg0);
                   11693:     /* elimination (1st step) */
                   11694:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   11695:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11696:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   11697:     spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
                   11698:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   11699:         nd_sp(m,0,sp,&spol);
                   11700:         if ( !spol ) {
1.29      noro     11701:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     11702:           continue;
                   11703:         }
                   11704:         svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11705:         nd_to_vect64(m,s0vect,col,spol,svect);
                   11706:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,spol->sig);
                   11707:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   11708:         if ( i < col ) {
                   11709:             spmat[sprow] = svect;
                   11710:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   11711:             spsig[sprow] = sp->sig;
                   11712:             sprow++;
                   11713:         } else {
1.29      noro     11714:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     11715:         }
                   11716:         nd_free(spol);
                   11717:     }
                   11718:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
                   11719:     if ( DP_Print ) {
                   11720:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   11721:         fflush(asir_out);
                   11722:     }
                   11723:     /* free index arrays */
                   11724:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   11725:
                   11726:     /* elimination (2nd step) */
                   11727:     colstat = (int *)MALLOC(col*sizeof(int));
                   11728:     rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
                   11729:     r0 = 0;
1.31      noro     11730:     for ( i = 0; i < sprow; i++ ) {
                   11731:         if ( spsugar[i] >= 0 ) {
                   11732:           NEXTNODE(r0,r);
                   11733:           BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
                   11734:           SG((NDV)BDY(r)) = spsugar[i];
                   11735:           ((NDV)BDY(r))->sig = spsig[i];
                   11736:         } else
                   11737:           syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
1.28      noro     11738:         GCFREE(spmat[i]);
                   11739:     }
                   11740:     if ( r0 ) NEXT(r) = 0;
                   11741:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
                   11742:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   11743:     if ( DP_Print ) {
                   11744:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   11745:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   11746:             nsp,nred,sprow,col,rank);
                   11747:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   11748:     }
                   11749:     return r0;
                   11750: }
1.40      noro     11751: #endif
1.28      noro     11752:
                   11753: NODE nd_f4_red_s(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,NODE *syzlistp)
                   11754: {
                   11755:   IndArray *imat;
                   11756:   int nsp,nred,i,start;
                   11757:   int *rhead;
                   11758:   NODE r0,rp;
                   11759:   ND_pairs sp;
                   11760:   NM_ind_pair *rvect;
                   11761:   struct oEGT eg0,eg1,eg_conv;
                   11762:
                   11763:   for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   11764:   nred = length(rp0);
                   11765:   imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   11766:   rhead = (int *)MALLOC(col*sizeof(int));
                   11767:   for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   11768:
                   11769:   /* construction of index arrays */
                   11770:   get_eg(&eg0);
                   11771:   if ( DP_Print ) {
                   11772:     fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   11773:     fflush(asir_out);
                   11774:   }
                   11775:   rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   11776:   for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   11777:     rvect[i] = (NM_ind_pair)BDY(rp);
                   11778:     imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
                   11779:     rhead[imat[i]->head] = 1;
                   11780:     start = imat[i]->head;
                   11781:   }
                   11782:   get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
                   11783:   if ( DP_Print ) {
                   11784:     fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   11785:     fflush(asir_out);
                   11786:   }
                   11787:   if ( m > 0 )
1.40      noro     11788: #if SIZEOF_LONG==8
1.28      noro     11789:     r0 = nd_f4_red_mod64_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
1.40      noro     11790: #else
                   11791:     r0 = nd_f4_red_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
                   11792: #endif
1.65    ! noro     11793:   else {
1.28      noro     11794: //    r0 = nd_f4_red_q_main_s(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
1.65    ! noro     11795:     r0 = 0;
1.28      noro     11796:     error("nd_f4_red_q_main_s : not implemented yet");
1.65    ! noro     11797:   }
1.28      noro     11798:   return r0;
                   11799: }
                   11800:
                   11801: INLINE int ndl_find_reducer_minsig(UINT *dg)
                   11802: {
                   11803:   RHist r;
1.65    ! noro     11804:   int i,imin;
1.28      noro     11805:   SIG t;
                   11806:   static int wpd,nvar;
                   11807:   static SIG quo,quomin;
                   11808:   static UINT *tmp;
                   11809:
                   11810:   if ( !quo || nvar != nd_nvar ) { NEWSIG(quo); NEWSIG(quomin); }
                   11811:   if ( wpd != nd_wpd ) {
                   11812:     wpd = nd_wpd;
                   11813:     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
                   11814:   }
                   11815: #if 0
                   11816:   d = ndl_hash_value(dg);
                   11817:   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   11818:     if ( ndl_equal(dg,DL(r)) ) {
                   11819:       return r->index;
                   11820:     }
                   11821:   }
                   11822: #endif
                   11823:   imin = -1;
                   11824:   for ( i = 0; i < nd_psn; i++ ) {
                   11825:     r = nd_psh[i];
                   11826:     if ( ndl_reducible(dg,DL(r)) ) {
                   11827:       ndl_sub(dg,DL(r),tmp);
                   11828:       _ndltodl(tmp,DL(quo));
                   11829:       _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
                   11830:       quo->pos = nd_psh[i]->sig->pos;
1.55      noro     11831:       _adddl(nd_nvar,DL(quo),nd_sba_hm[quo->pos],DL2(quo));
1.28      noro     11832:       if ( imin < 0 || comp_sig(quomin,quo) > 0 ) {
                   11833:         t = quo; quo = quomin; quomin = t;
                   11834:         imin = i;
                   11835:       }
                   11836:     }
                   11837:   }
                   11838:   if ( imin == -1 ) return nd_psn;
                   11839:   else {
                   11840: #if 0
                   11841:     nd_append_red(dg,i);
                   11842: #endif
                   11843:     return imin;
                   11844:   }
                   11845: }
                   11846:
                   11847: int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   11848: {
                   11849:   NODE rp0,rp;
                   11850:   NM mul,head,s0,s;
                   11851:   int index,col,i,sugar;
                   11852:   RHist h;
                   11853:   UINT *s0v,*p;
                   11854:   NM_ind_pair pair;
                   11855:   ND red;
                   11856:   NDV *ps;
                   11857:   SIG sig;
                   11858:
                   11859:   s0 = 0; rp0 = 0; col = 0;
                   11860:   if ( nd_demand )
                   11861:     ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   11862:   else
                   11863:     ps = trace?nd_ps_trace:nd_ps;
                   11864:   while ( 1 ) {
                   11865:     head = remove_head_pbucket_symbolic(bucket);
                   11866:     if ( !head ) break;
                   11867:     if ( !s0 ) s0 = head;
                   11868:     else NEXT(s) = head;
                   11869:     s = head;
                   11870:     index = ndl_find_reducer_minsig(DL(head));
                   11871:     if ( index >= 0 && index < nd_psn ) {
                   11872:       h = nd_psh[index];
                   11873:       NEWNM(mul);
                   11874:       ndl_sub(DL(head),DL(h),DL(mul));
                   11875:       if ( ndl_check_bound2(index,DL(mul)) )
                   11876:         return 0;
                   11877:       sugar = TD(DL(mul))+SG(ps[index]);
                   11878:       NEWSIG(sig);
                   11879:       _ndltodl(DL(mul),DL(sig));
                   11880:       _addtodl(nd_nvar,DL(nd_psh[index]->sig),DL(sig));
                   11881:       sig->pos = nd_psh[index]->sig->pos;
1.55      noro     11882:       _adddl(nd_nvar,DL(sig),nd_sba_hm[sig->pos],DL2(sig));
1.28      noro     11883:       MKNM_ind_pair(pair,mul,index,sugar,sig);
                   11884:       red = ndv_mul_nm_symbolic(mul,ps[index]);
                   11885:       add_pbucket_symbolic(bucket,nd_remove_head(red));
                   11886:       NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   11887:     }
                   11888:     col++;
                   11889:   }
                   11890:   if ( rp0 ) NEXT(rp) = 0;
                   11891:   NEXT(s) = 0;
                   11892:   s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   11893:   for ( i = 0, p = s0v, s = s0; i < col;
                   11894:     i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   11895:   *s0vect = s0v;
                   11896:   *r = rp0;
                   11897:
                   11898:   return col;
                   11899: }
                   11900:
                   11901: NODE nd_sba_f4(int m,int **indp)
                   11902: {
1.65    ! noro     11903:   int i,nh,stat,f4red,f4step;
        !          11904:   int col,psugar,ms;
1.33      noro     11905:   NODE r,g,rp0,nflist;
1.41      noro     11906:   ND_pairs d,l,t,l1;
1.33      noro     11907:   ND h,nf;
                   11908:   NDV nfv;
1.65    ! noro     11909: //  union oNDC hc;
1.33      noro     11910:   UINT *s0vect;
1.28      noro     11911:   PGeoBucket bucket;
1.33      noro     11912:   NODE *syzlist;
                   11913:   SIG sig;
1.65    ! noro     11914:   struct oEGT eg1;
        !          11915:   struct oEGT eg2,eg_remove,eg_nf,eg_nfzero;
1.28      noro     11916:
                   11917:   Nf4_red=0;
1.30      noro     11918:   d = 0;
1.29      noro     11919:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.28      noro     11920:   for ( i = 0; i < nd_psn; i++ ) {
1.30      noro     11921:     d = update_pairs_s(d,i,syzlist);
1.28      noro     11922:   }
1.30      noro     11923:   nd_nbase = nd_psn;
1.28      noro     11924:   f4red = 1;
1.33      noro     11925:   psugar = 0;
1.34      noro     11926:   f4step = 0;
1.28      noro     11927:   while ( d ) {
1.33      noro     11928:     for ( t = d, ms = SG(d); t; t = NEXT(t) )
                   11929:       if ( SG(t) < ms ) ms = SG(t);
1.38      noro     11930:     if ( ms == psugar && f4step >= nd_sba_f4step ) {
1.33      noro     11931: again:
                   11932:       l = d; d = d->next;
1.41      noro     11933: #if 0
1.33      noro     11934:       if ( small_lcm(l) ) {
                   11935:         if ( DP_Print ) fprintf(asir_out,"M");
                   11936:         continue;
                   11937:       }
                   11938:       sig = l->sig;
                   11939:       stat = nd_sp(m,0,l,&h);
1.41      noro     11940: #else
                   11941:       l1 = find_smallest_lcm(l);
                   11942:       if ( l1 == 0 ) {
                   11943:         if ( DP_Print ) fprintf(asir_out,"M");
                   11944:         continue;
                   11945:       }
                   11946:       sig = l1->sig;
                   11947:       stat = nd_sp(m,0,l1,&h);
                   11948: #endif
1.33      noro     11949:       if ( !stat ) {
                   11950:         NEXT(l) = d; d = l;
                   11951:         d = nd_reconstruct(0,d);
                   11952:         goto again;
                   11953:       }
                   11954:   get_eg(&eg1);
                   11955:   #if USE_GEOBUCKET
1.39      noro     11956:       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     11957:   #else
1.39      noro     11958:       stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.33      noro     11959:   #endif
                   11960:   get_eg(&eg2);
                   11961:       if ( !stat ) {
                   11962:         NEXT(l) = d; d = l;
                   11963:         d = nd_reconstruct(0,d);
                   11964:         goto again;
                   11965:       } else if ( stat == -1 ) {
                   11966:         if ( DP_Print ) { printf("S"); fflush(stdout); }
                   11967:         FREENDP(l);
                   11968:       } else if ( nf ) {
                   11969:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   11970:         add_eg(&eg_nf,&eg1,&eg2);
1.65    ! noro     11971: //        hc = HCU(nf);
1.33      noro     11972:         nd_removecont(m,nf);
                   11973:         nfv = ndtondv(m,nf); nd_free(nf);
                   11974:         nh = ndv_newps(m,nfv,0);
                   11975:
1.30      noro     11976:         d = update_pairs_s(d,nh,syzlist);
                   11977:         nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.33      noro     11978:         FREENDP(l);
1.28      noro     11979:       } else {
1.33      noro     11980:         add_eg(&eg_nfzero,&eg1,&eg2);
                   11981:        // syzygy
                   11982:   get_eg(&eg1);
                   11983:         d = remove_spair_s(d,sig);
                   11984:   get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29      noro     11985:         syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.33      noro     11986:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   11987:         FREENDP(l);
                   11988:       }
                   11989:     } else {
1.34      noro     11990:       if ( ms != psugar ) f4step = 1;
                   11991:       else f4step++;
1.33      noro     11992: again2:
                   11993:       psugar = ms;
                   11994:       l = nd_minsugarp_s(d,&d);
1.65    ! noro     11995: //      sugar = nd_sugarweight?d->sugar2:SG(d);
1.33      noro     11996:       bucket = create_pbucket();
                   11997:       stat = nd_sp_f4(m,0,l,bucket);
                   11998:       if ( !stat ) {
                   11999:         for ( t = l; NEXT(t); t = NEXT(t) );
                   12000:         NEXT(t) = d; d = l;
                   12001:         d = nd_reconstruct(0,d);
                   12002:         goto again2;
                   12003:       }
                   12004:       if ( bucket->m < 0 ) continue;
                   12005:       col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
                   12006:       if ( !col ) {
1.37      noro     12007:         for ( t = l; NEXT(t); t = NEXT(t) )
                   12008:           ;
                   12009:         NEXT(t) = d; d = l;
1.33      noro     12010:         d = nd_reconstruct(0,d);
                   12011:         goto again2;
                   12012:       }
1.34      noro     12013:       if ( DP_Print ) fprintf(asir_out,"\nsugar=%d,",psugar);
1.33      noro     12014:       nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
                   12015:       /* adding new bases */
                   12016:       for ( r = nflist; r; r = NEXT(r) ) {
                   12017:         nfv = (NDV)BDY(r);
1.38      noro     12018:         if ( nd_f4_td ) SG(nfv) = nd_tdeg(nfv);
1.33      noro     12019:         ndv_removecont(m,nfv);
                   12020:         nh = ndv_newps(m,nfv,0);
                   12021:         d = update_pairs_s(d,nh,syzlist);
                   12022:         nd_sba_pos[nfv->sig->pos] = append_one(nd_sba_pos[nfv->sig->pos],nh);
                   12023:       }
                   12024:       for ( i = 0; i < nd_nbase; i++ )
                   12025:         for ( r = syzlist[i]; r; r = NEXT(r) )
                   12026:             d = remove_spair_s(d,(SIG)BDY(r));
                   12027:       d = remove_large_lcm(d);
                   12028:       if ( DP_Print ) {
1.34      noro     12029:         fprintf(asir_out,"f4red=%d,gblen=%d",f4red,nd_psn); fflush(asir_out);
1.33      noro     12030:       }
                   12031:       f4red++;
1.28      noro     12032:     }
                   12033:   }
                   12034:   if ( DP_Print ) {
1.34      noro     12035:     fprintf(asir_out,"\nnumber of red=%d,",Nf4_red);
1.28      noro     12036:   }
1.30      noro     12037:   g = conv_ilist_s(nd_demand,0,indp);
1.28      noro     12038:   return g;
                   12039: }

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