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

1.63    ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.62 2021/12/19 08:23:28 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:   int i,len,blen;
                    182:   matrix mat;
                    183:
                    184:   mat = (matrix)MALLOC(sizeof(struct matrix));
                    185:   mat->row = row;
                    186:   mat->col = col;
                    187:   mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
                    188:   return mat;
                    189: }
                    190:
                    191:
                    192: void _ND_alloc()
                    193: {
                    194:     ND p;
                    195:     int i;
                    196:
                    197:     for ( i = 0; i < 1024; i++ ) {
                    198:         p = (ND)MALLOC(sizeof(struct oND));
                    199:         p->body = (NM)_nd_free_list; _nd_free_list = p;
                    200:     }
                    201: }
                    202:
                    203: void _NDP_alloc()
                    204: {
                    205:     ND_pairs p;
                    206:     int i;
                    207:
                    208:     for ( i = 0; i < 1024; i++ ) {
                    209:         p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
                    210:             +(nd_wpd-1)*sizeof(UINT));
                    211:         p->next = _ndp_free_list; _ndp_free_list = p;
                    212:     }
                    213: }
                    214:
                    215: INLINE int nd_length(ND p)
                    216: {
                    217:     NM m;
                    218:     int i;
                    219:
                    220:     if ( !p )
                    221:         return 0;
                    222:     else {
                    223:         for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
                    224:         return i;
                    225:     }
                    226: }
                    227:
                    228: extern int dp_negative_weight;
                    229:
                    230: INLINE int ndl_reducible(UINT *d1,UINT *d2)
                    231: {
                    232:     UINT u1,u2;
                    233:     int i,j;
                    234:
                    235:     if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
                    236:
                    237:     if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
                    238: #if USE_UNROLL
                    239:     switch ( nd_bpe ) {
                    240:         case 3:
                    241:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    242:                 u1 = d1[i]; u2 = d2[i];
                    243:                 if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
                    244:                 if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
                    245:                 if ( (u1&  0xe00000) < (u2&  0xe00000) ) return 0;
                    246:                 if ( (u1&  0x1c0000) < (u2&  0x1c0000) ) return 0;
                    247:                 if ( (u1&   0x38000) < (u2&   0x38000) ) return 0;
                    248:                 if ( (u1&    0x7000) < (u2&    0x7000) ) return 0;
                    249:                 if ( (u1&     0xe00) < (u2&     0xe00) ) return 0;
                    250:                 if ( (u1&     0x1c0) < (u2&     0x1c0) ) return 0;
                    251:                 if ( (u1&      0x38) < (u2&      0x38) ) return 0;
                    252:                 if ( (u1&       0x7) < (u2&       0x7) ) return 0;
                    253:             }
                    254:             return 1;
                    255:             break;
                    256:         case 4:
                    257:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    258:                 u1 = d1[i]; u2 = d2[i];
                    259:                 if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
                    260:                 if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
                    261:                 if ( (u1&  0xf00000) < (u2&  0xf00000) ) return 0;
                    262:                 if ( (u1&   0xf0000) < (u2&   0xf0000) ) return 0;
                    263:                 if ( (u1&    0xf000) < (u2&    0xf000) ) return 0;
                    264:                 if ( (u1&     0xf00) < (u2&     0xf00) ) return 0;
                    265:                 if ( (u1&      0xf0) < (u2&      0xf0) ) return 0;
                    266:                 if ( (u1&       0xf) < (u2&       0xf) ) return 0;
                    267:             }
                    268:             return 1;
                    269:             break;
                    270:         case 6:
                    271:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    272:                 u1 = d1[i]; u2 = d2[i];
                    273:                 if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
                    274:                 if ( (u1&  0xfc0000) < (u2&  0xfc0000) ) return 0;
                    275:                 if ( (u1&   0x3f000) < (u2&   0x3f000) ) return 0;
                    276:                 if ( (u1&     0xfc0) < (u2&     0xfc0) ) return 0;
                    277:                 if ( (u1&      0x3f) < (u2&      0x3f) ) return 0;
                    278:             }
                    279:             return 1;
                    280:             break;
                    281:         case 8:
                    282:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    283:                 u1 = d1[i]; u2 = d2[i];
                    284:                 if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
                    285:                 if ( (u1&  0xff0000) < (u2&  0xff0000) ) return 0;
                    286:                 if ( (u1&    0xff00) < (u2&    0xff00) ) return 0;
                    287:                 if ( (u1&      0xff) < (u2&      0xff) ) return 0;
                    288:             }
                    289:             return 1;
                    290:             break;
                    291:         case 16:
                    292:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    293:                 u1 = d1[i]; u2 = d2[i];
                    294:                 if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
                    295:                 if ( (u1&    0xffff) < (u2&    0xffff) ) return 0;
                    296:             }
                    297:             return 1;
                    298:             break;
                    299:         case 32:
                    300:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                    301:                 if ( d1[i] < d2[i] ) return 0;
                    302:             return 1;
                    303:             break;
                    304:         default:
                    305:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    306:                 u1 = d1[i]; u2 = d2[i];
                    307:                 for ( j = 0; j < nd_epw; j++ )
                    308:                     if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    309:             }
                    310:             return 1;
                    311:     }
                    312: #else
                    313:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    314:         u1 = d1[i]; u2 = d2[i];
                    315:         for ( j = 0; j < nd_epw; j++ )
                    316:             if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    317:     }
                    318:     return 1;
                    319: #endif
                    320: }
                    321:
                    322: /*
                    323:  * If the current order is a block order,
                    324:  * then the last block is length 1 and contains
                    325:  * the homo variable. Otherwise, the original
                    326:  * order is either 0 or 2.
                    327:  */
                    328:
                    329: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
                    330: {
                    331:     int w,i,e,n,omask0;
                    332:
                    333:     omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
                    334:     n = nd_nvar-1;
                    335:     ndl_zero(r);
                    336:     for ( i = 0; i < n; i++ ) {
                    337:         e = GET_EXP_OLD(d,i);
                    338:         PUT_EXP(r,i,e);
                    339:     }
                    340:     w = TD(d);
                    341:     PUT_EXP(r,nd_nvar-1,weight-w);
                    342:     if ( nd_module ) MPOS(r) = d[ompos];
                    343:     TD(r) = weight;
                    344:     if ( nd_blockmask ) ndl_weight_mask(r);
                    345: }
                    346:
                    347: void ndl_dehomogenize(UINT *d)
                    348: {
                    349:     UINT mask;
                    350:     UINT h;
                    351:     int i,bits;
                    352:
                    353:     if ( nd_blockmask ) {
                    354:         h = GET_EXP(d,nd_nvar-1);
                    355:         XOR_EXP(d,nd_nvar-1,h);
                    356:         TD(d) -= h;
                    357:         ndl_weight_mask(d);
                    358:     } else {
                    359:         if ( nd_isrlex ) {
                    360:             if ( nd_bpe == 32 ) {
                    361:                 h = d[nd_exporigin];
                    362:                 for ( i = nd_exporigin+1; i < nd_wpd; i++ )
                    363:                     d[i-1] = d[i];
                    364:                 d[i-1] = 0;
                    365:                 TD(d) -= h;
                    366:             } else {
                    367:                 bits = nd_epw*nd_bpe;
                    368:                 mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
                    369:                 h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
                    370:                 for ( i = nd_exporigin; i < nd_wpd; i++ )
                    371:                     d[i] = ((d[i]<<nd_bpe)&mask)
                    372:                         |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
                    373:                 TD(d) -= h;
                    374:             }
                    375:         } else {
                    376:             h = GET_EXP(d,nd_nvar-1);
                    377:             XOR_EXP(d,nd_nvar-1,h);
                    378:             TD(d) -= h;
                    379:         }
                    380:     }
                    381: }
                    382:
                    383: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
                    384: {
                    385:     UINT t1,t2,u,u1,u2;
                    386:     int i,j,l;
                    387:
                    388:     if ( nd_module && (MPOS(d1) != MPOS(d2)) )
                    389:         error("ndl_lcm : inconsistent monomials");
                    390: #if USE_UNROLL
                    391:     switch ( nd_bpe ) {
                    392:         case 3:
                    393:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    394:                 u1 = d1[i]; u2 = d2[i];
                    395:                 t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
                    396:                 t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
                    397:                 t1 = (u1&  0xe00000); t2 = (u2&  0xe00000); u |= t1>t2?t1:t2;
                    398:                 t1 = (u1&  0x1c0000); t2 = (u2&  0x1c0000); u |= t1>t2?t1:t2;
                    399:                 t1 = (u1&   0x38000); t2 = (u2&   0x38000); u |= t1>t2?t1:t2;
                    400:                 t1 = (u1&    0x7000); t2 = (u2&    0x7000); u |= t1>t2?t1:t2;
                    401:                 t1 = (u1&     0xe00); t2 = (u2&     0xe00); u |= t1>t2?t1:t2;
                    402:                 t1 = (u1&     0x1c0); t2 = (u2&     0x1c0); u |= t1>t2?t1:t2;
                    403:                 t1 = (u1&      0x38); t2 = (u2&      0x38); u |= t1>t2?t1:t2;
                    404:                 t1 = (u1&       0x7); t2 = (u2&       0x7); u |= t1>t2?t1:t2;
                    405:                 d[i] = u;
                    406:             }
                    407:             break;
                    408:         case 4:
                    409:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    410:                 u1 = d1[i]; u2 = d2[i];
                    411:                 t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
                    412:                 t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
                    413:                 t1 = (u1&  0xf00000); t2 = (u2&  0xf00000); u |= t1>t2?t1:t2;
                    414:                 t1 = (u1&   0xf0000); t2 = (u2&   0xf0000); u |= t1>t2?t1:t2;
                    415:                 t1 = (u1&    0xf000); t2 = (u2&    0xf000); u |= t1>t2?t1:t2;
                    416:                 t1 = (u1&     0xf00); t2 = (u2&     0xf00); u |= t1>t2?t1:t2;
                    417:                 t1 = (u1&      0xf0); t2 = (u2&      0xf0); u |= t1>t2?t1:t2;
                    418:                 t1 = (u1&       0xf); t2 = (u2&       0xf); u |= t1>t2?t1:t2;
                    419:                 d[i] = u;
                    420:             }
                    421:             break;
                    422:         case 6:
                    423:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    424:                 u1 = d1[i]; u2 = d2[i];
                    425:                 t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
                    426:                 t1 = (u1&  0xfc0000); t2 = (u2&  0xfc0000); u |= t1>t2?t1:t2;
                    427:                 t1 = (u1&   0x3f000); t2 = (u2&   0x3f000); u |= t1>t2?t1:t2;
                    428:                 t1 = (u1&     0xfc0); t2 = (u2&     0xfc0); u |= t1>t2?t1:t2;
                    429:                 t1 = (u1&      0x3f); t2 = (u2&      0x3f); u |= t1>t2?t1:t2;
                    430:                 d[i] = u;
                    431:             }
                    432:             break;
                    433:         case 8:
                    434:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    435:                 u1 = d1[i]; u2 = d2[i];
                    436:                 t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
                    437:                 t1 = (u1&  0xff0000); t2 = (u2&  0xff0000); u |= t1>t2?t1:t2;
                    438:                 t1 = (u1&    0xff00); t2 = (u2&    0xff00); u |= t1>t2?t1:t2;
                    439:                 t1 = (u1&      0xff); t2 = (u2&      0xff); u |= t1>t2?t1:t2;
                    440:                 d[i] = u;
                    441:             }
                    442:             break;
                    443:         case 16:
                    444:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    445:                 u1 = d1[i]; u2 = d2[i];
                    446:                 t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
                    447:                 t1 = (u1&    0xffff); t2 = (u2&    0xffff); u |= t1>t2?t1:t2;
                    448:                 d[i] = u;
                    449:             }
                    450:             break;
                    451:         case 32:
                    452:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    453:                 u1 = d1[i]; u2 = d2[i];
                    454:                 d[i] = u1>u2?u1:u2;
                    455:             }
                    456:             break;
                    457:         default:
                    458:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    459:                 u1 = d1[i]; u2 = d2[i];
                    460:                 for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    461:                     t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    462:                 }
                    463:                 d[i] = u;
                    464:             }
                    465:             break;
                    466:     }
                    467: #else
                    468:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    469:         u1 = d1[i]; u2 = d2[i];
                    470:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    471:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    472:         }
                    473:         d[i] = u;
                    474:     }
                    475: #endif
                    476:     if ( nd_module ) MPOS(d) = MPOS(d1);
                    477:     TD(d) = ndl_weight(d);
                    478:     if ( nd_blockmask ) ndl_weight_mask(d);
                    479: }
                    480:
                    481: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
                    482: {
                    483:     UINT t1,t2,u,u1,u2;
                    484:     int i,j,l;
                    485:
                    486:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    487:         u1 = d1[i]; u2 = d2[i];
                    488:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    489:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    490:         }
                    491:         d[i] = u;
                    492:     }
                    493: }
                    494:
                    495: int ndl_weight(UINT *d)
                    496: {
                    497:     UINT t,u;
                    498:     int i,j;
                    499:
                    500:     if ( current_dl_weight_vector )
                    501:         for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    502:             u = GET_EXP(d,i);
                    503:             t += MUL_WEIGHT(u,i);
                    504:         }
                    505:     else
                    506:         for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    507:             u = d[i];
                    508:             for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    509:                 t += (u&nd_mask0);
                    510:         }
1.20      noro      511:     if ( nd_module && nd_module_rank && MPOS(d) )
                    512:         t += nd_module_weight[MPOS(d)-1];
                    513:     for ( i = nd_exporigin; i < nd_wpd; i++ )
                    514:       if ( d[i] && !t )
                    515:         printf("afo\n");
1.1       noro      516:     return t;
                    517: }
                    518:
                    519: /* for sugarweight */
                    520:
                    521: int ndl_weight2(UINT *d)
                    522: {
                    523:     int t,u;
                    524:     int i,j;
                    525:
                    526:     for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    527:         u = GET_EXP(d,i);
                    528:         t += nd_sugarweight[i]*u;
                    529:     }
1.20      noro      530:     if ( nd_module && nd_module_rank && MPOS(d) )
                    531:         t += nd_module_weight[MPOS(d)-1];
1.1       noro      532:     return t;
                    533: }
                    534:
                    535: void ndl_weight_mask(UINT *d)
                    536: {
                    537:     UINT t,u;
                    538:     UINT *mask;
                    539:     int i,j,k,l;
                    540:
                    541:     l = nd_blockmask->n;
                    542:     for ( k = 0; k < l; k++ ) {
                    543:         mask = nd_blockmask->mask[k];
                    544:         if ( current_dl_weight_vector )
                    545:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    546:                 u = GET_EXP_MASK(d,i,mask);
                    547:                 t += MUL_WEIGHT(u,i);
                    548:             }
                    549:         else
                    550:             for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    551:                 u = d[i]&mask[i];
                    552:                 for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    553:                     t += (u&nd_mask0);
                    554:             }
                    555:         d[k+1] = t;
                    556:     }
                    557: }
                    558:
1.21      noro      559: int ndl_glex_compare(UINT *d1,UINT *d2)
                    560: {
                    561:   if ( TD(d1) > TD(d2) ) return 1;
                    562:   else if ( TD(d1) < TD(d2) ) return -1;
                    563:   else return ndl_lex_compare(d1,d2);
                    564: }
                    565:
1.1       noro      566: int ndl_lex_compare(UINT *d1,UINT *d2)
                    567: {
                    568:     int i;
                    569:
                    570:     d1 += nd_exporigin;
                    571:     d2 += nd_exporigin;
                    572:     for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
                    573:         if ( *d1 > *d2 )
                    574:             return nd_isrlex ? -1 : 1;
                    575:         else if ( *d1 < *d2 )
                    576:             return nd_isrlex ? 1 : -1;
                    577:     return 0;
                    578: }
                    579:
                    580: int ndl_block_compare(UINT *d1,UINT *d2)
                    581: {
                    582:     int i,l,j,ord_o,ord_l;
                    583:     struct order_pair *op;
                    584:     UINT t1,t2,m;
                    585:     UINT *mask;
                    586:
                    587:     l = nd_blockmask->n;
                    588:     op = nd_blockmask->order_pair;
                    589:     for ( j = 0; j < l; j++ ) {
                    590:         mask = nd_blockmask->mask[j];
                    591:         ord_o = op[j].order;
                    592:         if ( ord_o < 2 ) {
                    593:             if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
                    594:             else if ( t1 < t2 ) return -1;
                    595:         }
                    596:         for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    597:             m = mask[i];
                    598:             t1 = d1[i]&m;
                    599:             t2 = d2[i]&m;
                    600:             if ( t1 > t2 )
                    601:                 return !ord_o ? -1 : 1;
                    602:             else if ( t1 < t2 )
                    603:                 return !ord_o ? 1 : -1;
                    604:         }
                    605:     }
                    606:     return 0;
                    607: }
                    608:
                    609: int ndl_matrix_compare(UINT *d1,UINT *d2)
                    610: {
                    611:   int i,j,s,row;
                    612:   int *v;
                    613:   Z **mat;
                    614:   Z *w;
                    615:   Z t1;
                    616:   Z t,t2;
                    617:
1.6       noro      618:   for ( j = 0; j < nd_nvar; j++ )
                    619:     nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.1       noro      620:   if ( nd_top_weight ) {
                    621:     if ( OID(nd_top_weight) == O_VECT ) {
1.6       noro      622:       mat = (Z **)&BDY((VECT)nd_top_weight);
                    623:       row = 1;
1.1       noro      624:     } else {
                    625:       mat = (Z **)BDY((MAT)nd_top_weight);
1.6       noro      626:       row = ((MAT)nd_top_weight)->row;
1.1       noro      627:     }
                    628:     for ( i = 0; i < row; i++ ) {
1.6       noro      629:       w = mat[i];
1.1       noro      630:       for ( j = 0, t = 0; j < nd_nvar; j++ ) {
1.6       noro      631:         STOZ(nd_work_vector[j],t1);
1.1       noro      632:         mulz(w[j],t1,&t2);
                    633:         addz(t,t2,&t1);
                    634:         t = t1;
                    635:       }
1.6       noro      636:       if ( t ) {
                    637:         s = sgnz(t);
1.1       noro      638:         if ( s > 0 ) return 1;
                    639:         else if ( s < 0 ) return -1;
1.6       noro      640:       }
1.1       noro      641:     }
1.6       noro      642:   }
                    643:   for ( i = 0; i < nd_matrix_len; i++ ) {
                    644:     v = nd_matrix[i];
                    645:     for ( j = 0, s = 0; j < nd_nvar; j++ )
                    646:       s += v[j]*nd_work_vector[j];
                    647:     if ( s > 0 ) return 1;
                    648:     else if ( s < 0 ) return -1;
                    649:   }
1.1       noro      650:   if ( !ndl_equal(d1,d2) )
1.6       noro      651:     error("ndl_matrix_compare : invalid matrix");
                    652:   return 0;
1.1       noro      653: }
                    654:
                    655: int ndl_composite_compare(UINT *d1,UINT *d2)
                    656: {
                    657:     int i,j,s,start,end,len,o;
                    658:     int *v;
                    659:     struct sparse_weight *sw;
                    660:
                    661:     for ( j = 0; j < nd_nvar; j++ )
                    662:         nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
                    663:     for ( i = 0; i < nd_worb_len; i++ ) {
                    664:         len = nd_worb[i].length;
                    665:         switch ( nd_worb[i].type ) {
                    666:             case IS_DENSE_WEIGHT:
                    667:                 v = nd_worb[i].body.dense_weight;
                    668:                 for ( j = 0, s = 0; j < len; j++ )
                    669:                     s += v[j]*nd_work_vector[j];
                    670:                 if ( s > 0 ) return 1;
                    671:                 else if ( s < 0 ) return -1;
                    672:                 break;
                    673:             case IS_SPARSE_WEIGHT:
                    674:                 sw = nd_worb[i].body.sparse_weight;
                    675:                 for ( j = 0, s = 0; j < len; j++ )
                    676:                     s += sw[j].value*nd_work_vector[sw[j].pos];
                    677:                 if ( s > 0 ) return 1;
                    678:                 else if ( s < 0 ) return -1;
                    679:                 break;
                    680:             case IS_BLOCK:
                    681:                 o = nd_worb[i].body.block.order;
                    682:                 start = nd_worb[i].body.block.start;
                    683:                 switch ( o ) {
                    684:                     case 0:
                    685:                         end = start+len;
                    686:                         for ( j = start, s = 0; j < end; j++ )
                    687:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    688:                         if ( s > 0 ) return 1;
                    689:                         else if ( s < 0 ) return -1;
                    690:                         for ( j = end-1; j >= start; j-- )
                    691:                             if ( nd_work_vector[j] < 0 ) return 1;
                    692:                             else if ( nd_work_vector[j] > 0 ) return -1;
                    693:                         break;
                    694:                     case 1:
                    695:                         end = start+len;
                    696:                         for ( j = start, s = 0; j < end; j++ )
                    697:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    698:                         if ( s > 0 ) return 1;
                    699:                         else if ( s < 0 ) return -1;
                    700:                         for ( j = start; j < end; j++ )
                    701:                             if ( nd_work_vector[j] > 0 ) return 1;
                    702:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    703:                         break;
                    704:                     case 2:
                    705:                         end = start+len;
                    706:                         for ( j = start; j < end; j++ )
                    707:                             if ( nd_work_vector[j] > 0 ) return 1;
                    708:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    709:                         break;
                    710:                 }
                    711:                 break;
                    712:         }
                    713:     }
                    714:     return 0;
                    715: }
                    716:
                    717: /* TDH -> WW -> TD-> RL */
                    718:
                    719: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
                    720: {
                    721:     int i,m,e1,e2;
                    722:
                    723:     if ( TD(d1) > TD(d2) ) return 1;
                    724:     else if ( TD(d1) < TD(d2) ) return -1;
                    725:     m = nd_nvar>>1;
                    726:     for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
                    727:         e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
                    728:         e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
                    729:     }
                    730:     if ( e1 > e2 ) return 1;
                    731:     else if ( e1 < e2 ) return -1;
                    732:     return ndl_lex_compare(d1,d2);
                    733: }
                    734:
1.21      noro      735: // common function for module glex and grlex comparison
                    736: int ndl_module_glex_compare(UINT *d1,UINT *d2)
1.1       noro      737: {
1.21      noro      738:   int c;
1.1       noro      739:
1.21      noro      740:   switch ( nd_module_ordtype ) {
                    741:     case 0:
                    742:       if ( TD(d1) > TD(d2) ) return 1;
                    743:       else if ( TD(d1) < TD(d2) ) return -1;
                    744:       else if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
                    745:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    746:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    747:       else return 0;
                    748:       break;
1.1       noro      749:
1.21      noro      750:     case 1:
1.19      noro      751:       if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
                    752:          if ( TD(d1) > TD(d2) ) return 1;
                    753:          else if ( TD(d1) < TD(d2) ) return -1;
                    754:          if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
                    755:          if ( MPOS(d1) < MPOS(d2) ) return 1;
                    756:          else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    757:       }
                    758:       if ( MPOS(d1) < MPOS(d2) ) return 1;
                    759:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
1.21      noro      760:       else if ( TD(d1) > TD(d2) ) return 1;
                    761:       else if ( TD(d1) < TD(d2) ) return -1;
                    762:       else return ndl_lex_compare(d1,d2);
                    763:       break;
1.1       noro      764:
1.21      noro      765:     case 2: // weight -> POT
                    766:       if ( TD(d1) > TD(d2) ) return 1;
                    767:       else if ( TD(d1) < TD(d2) ) return -1;
                    768:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    769:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    770:       else return ndl_lex_compare(d1,d2);
                    771:       break;
1.1       noro      772:
1.21      noro      773:     default:
                    774:       error("ndl_module_glex_compare : invalid module_ordtype");
1.28      noro      775:       return 0;
1.21      noro      776:   }
1.1       noro      777: }
                    778:
1.21      noro      779: // common  for module comparison
                    780: int ndl_module_compare(UINT *d1,UINT *d2)
1.1       noro      781: {
1.21      noro      782:   int c;
1.1       noro      783:
1.21      noro      784:   switch ( nd_module_ordtype ) {
                    785:     case 0:
1.23      noro      786:       if ( (c = (*ndl_base_compare_function)(d1,d2)) != 0 ) return c;
1.21      noro      787:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    788:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    789:       else return 0;
                    790:       break;
1.1       noro      791:
1.21      noro      792:     case 1:
                    793:       if ( MPOS(d1) < MPOS(d2) ) return 1;
                    794:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    795:       else return (*ndl_base_compare_function)(d1,d2);
                    796:       break;
1.1       noro      797:
1.21      noro      798:     case 2: // weight -> POT
                    799:       if ( TD(d1) > TD(d2) ) return 1;
                    800:       else if ( TD(d1) < TD(d2) ) return -1;
                    801:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    802:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    803:       else return (*ndl_base_compare_function)(d1,d2);
                    804:       break;
1.1       noro      805:
1.21      noro      806:     default:
                    807:       error("ndl_module_compare : invalid module_ordtype");
1.28      noro      808:       return 0;
1.21      noro      809:   }
1.1       noro      810: }
                    811:
1.21      noro      812: extern DMMstack dmm_stack;
                    813: void _addtodl(int n,DL d1,DL d2);
1.31      noro      814: void _adddl(int n,DL d1,DL d2,DL d3);
1.21      noro      815: int _eqdl(int n,DL d1,DL d2);
                    816:
                    817: int ndl_module_schreyer_compare(UINT *m1,UINT *m2)
                    818: {
1.44      noro      819:   int pos1,pos2,t,j,retpot;
1.21      noro      820:   DMM *in;
                    821:   DMMstack s;
                    822:   static DL d1=0;
                    823:   static DL d2=0;
                    824:   static int dlen=0;
1.44      noro      825:   extern int ReversePOT;
1.21      noro      826:
1.44      noro      827:   if ( ReversePOT ) retpot = -1;
                    828:   else retpot = 1;
1.21      noro      829:   pos1 = MPOS(m1); pos2 = MPOS(m2);
                    830:   if ( pos1 == pos2 ) return (*ndl_base_compare_function)(m1,m2);
                    831:   if ( nd_nvar > dlen ) {
                    832:     NEWDL(d1,nd_nvar);
                    833:     NEWDL(d2,nd_nvar);
                    834:     dlen = nd_nvar;
                    835:   }
                    836:   d1->td = TD(m1);
                    837:   for ( j = 0; j < nd_nvar; j++ ) d1->d[j] = GET_EXP(m1,j);
                    838:   d2->td = TD(m2);
                    839:   for ( j = 0; j < nd_nvar; j++ ) d2->d[j] = GET_EXP(m2,j);
                    840:   for ( s = dmm_stack; s; s = NEXT(s) ) {
                    841:     in = s->in;
                    842:     _addtodl(nd_nvar,in[pos1]->dl,d1);
                    843:     _addtodl(nd_nvar,in[pos2]->dl,d2);
                    844:     if ( in[pos1]->pos == in[pos2]->pos && _eqdl(nd_nvar,d1,d2)) {
1.44      noro      845:       if ( pos1 < pos2 ) return retpot;
                    846:       else if ( pos1 > pos2 ) return -retpot;
1.21      noro      847:       else return 0;
                    848:     }
                    849:     pos1 = in[pos1]->pos;
                    850:     pos2 = in[pos2]->pos;
                    851:     if ( pos1 == pos2 ) return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    852:   }
                    853:   // comparison by the bottom order
                    854: LAST:
                    855:   switch ( nd_base_ordtype ) {
                    856:     case 0:
                    857:       t = (*dl_base_compare_function)(nd_nvar,d1,d2);
                    858:       if ( t ) return t;
1.44      noro      859:       else if ( pos1 < pos2 ) return retpot;
                    860:       else if ( pos1 > pos2 ) return -retpot;
1.21      noro      861:       else return 0;
                    862:       break;
                    863:     case 1:
1.44      noro      864:       if ( pos1 < pos2 ) return retpot;
                    865:       else if ( pos1 > pos2 ) return -retpot;
1.21      noro      866:       else return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    867:       break;
                    868:     case 2:
                    869:       if ( d1->td > d2->td  ) return 1;
                    870:       else if ( d1->td < d2->td ) return -1;
1.44      noro      871:       else if ( pos1 < pos2 ) return retpot;
                    872:       else if ( pos1 > pos2 ) return -retpot;
1.21      noro      873:       else return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    874:       break;
                    875:     default:
                    876:       error("ndl_schreyer_compare : invalid base ordtype");
1.28      noro      877:       return 0;
1.21      noro      878:   }
1.1       noro      879: }
                    880:
                    881: INLINE int ndl_equal(UINT *d1,UINT *d2)
                    882: {
                    883:     int i;
                    884:
                    885:     switch ( nd_wpd ) {
                    886:         case 2:
                    887:             if ( TD(d2) != TD(d1) ) return 0;
                    888:             if ( d2[1] != d1[1] ) return 0;
                    889:             return 1;
                    890:             break;
                    891:         case 3:
                    892:             if ( TD(d2) != TD(d1) ) return 0;
                    893:             if ( d2[1] != d1[1] ) return 0;
                    894:             if ( d2[2] != d1[2] ) return 0;
                    895:             return 1;
                    896:             break;
                    897:         default:
                    898:             for ( i = 0; i < nd_wpd; i++ )
                    899:                 if ( *d1++ != *d2++ ) return 0;
                    900:             return 1;
                    901:             break;
                    902:     }
                    903: }
                    904:
                    905: INLINE void ndl_copy(UINT *d1,UINT *d2)
                    906: {
                    907:     int i;
                    908:
                    909:     switch ( nd_wpd ) {
                    910:         case 2:
                    911:             TD(d2) = TD(d1);
                    912:             d2[1] = d1[1];
                    913:             break;
                    914:         case 3:
                    915:             TD(d2) = TD(d1);
                    916:             d2[1] = d1[1];
                    917:             d2[2] = d1[2];
                    918:             break;
                    919:         default:
                    920:             for ( i = 0; i < nd_wpd; i++ )
                    921:                 d2[i] = d1[i];
                    922:             break;
                    923:     }
                    924: }
                    925:
                    926: INLINE void ndl_zero(UINT *d)
                    927: {
                    928:     int i;
                    929:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                    930: }
                    931:
                    932: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
                    933: {
                    934:     int i;
                    935:
                    936:     if ( nd_module ) {
                    937:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
                    938:         error("ndl_add : invalid operation");
                    939:     }
                    940: #if 1
                    941:     switch ( nd_wpd ) {
                    942:         case 2:
                    943:             TD(d) = TD(d1)+TD(d2);
                    944:             d[1] = d1[1]+d2[1];
                    945:             break;
                    946:         case 3:
                    947:             TD(d) = TD(d1)+TD(d2);
                    948:             d[1] = d1[1]+d2[1];
                    949:             d[2] = d1[2]+d2[2];
                    950:             break;
                    951:         default:
                    952:             for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    953:             break;
                    954:     }
                    955: #else
                    956:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    957: #endif
                    958: }
                    959:
                    960: /* d1 += d2 */
                    961: INLINE void ndl_addto(UINT *d1,UINT *d2)
                    962: {
                    963:     int i;
                    964:
                    965:     if ( nd_module ) {
                    966:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
                    967:             error("ndl_addto : invalid operation");
                    968:     }
                    969: #if 1
                    970:     switch ( nd_wpd ) {
                    971:         case 2:
                    972:             TD(d1) += TD(d2);
                    973:             d1[1] += d2[1];
                    974:             break;
                    975:         case 3:
                    976:             TD(d1) += TD(d2);
                    977:             d1[1] += d2[1];
                    978:             d1[2] += d2[2];
                    979:             break;
                    980:         default:
                    981:             for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    982:             break;
                    983:     }
                    984: #else
                    985:     for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    986: #endif
                    987: }
                    988:
                    989: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
                    990: {
                    991:     int i;
                    992:
                    993:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
                    994: }
                    995:
                    996: int ndl_disjoint(UINT *d1,UINT *d2)
                    997: {
                    998:     UINT t1,t2,u,u1,u2;
                    999:     int i,j;
                   1000:
                   1001:     if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
                   1002: #if USE_UNROLL
                   1003:     switch ( nd_bpe ) {
                   1004:         case 3:
                   1005:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1006:                 u1 = d1[i]; u2 = d2[i];
                   1007:                 t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
                   1008:                 t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
                   1009:                 t1 = u1&  0xe00000; t2 = u2&  0xe00000; if ( t1&&t2 ) return 0;
                   1010:                 t1 = u1&  0x1c0000; t2 = u2&  0x1c0000; if ( t1&&t2 ) return 0;
                   1011:                 t1 = u1&   0x38000; t2 = u2&   0x38000; if ( t1&&t2 ) return 0;
                   1012:                 t1 = u1&    0x7000; t2 = u2&    0x7000; if ( t1&&t2 ) return 0;
                   1013:                 t1 = u1&     0xe00; t2 = u2&     0xe00; if ( t1&&t2 ) return 0;
                   1014:                 t1 = u1&     0x1c0; t2 = u2&     0x1c0; if ( t1&&t2 ) return 0;
                   1015:                 t1 = u1&      0x38; t2 = u2&      0x38; if ( t1&&t2 ) return 0;
                   1016:                 t1 = u1&       0x7; t2 = u2&       0x7; if ( t1&&t2 ) return 0;
                   1017:             }
                   1018:             return 1;
                   1019:             break;
                   1020:         case 4:
                   1021:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1022:                 u1 = d1[i]; u2 = d2[i];
                   1023:                 t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
                   1024:                 t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
                   1025:                 t1 = u1&  0xf00000; t2 = u2&  0xf00000; if ( t1&&t2 ) return 0;
                   1026:                 t1 = u1&   0xf0000; t2 = u2&   0xf0000; if ( t1&&t2 ) return 0;
                   1027:                 t1 = u1&    0xf000; t2 = u2&    0xf000; if ( t1&&t2 ) return 0;
                   1028:                 t1 = u1&     0xf00; t2 = u2&     0xf00; if ( t1&&t2 ) return 0;
                   1029:                 t1 = u1&      0xf0; t2 = u2&      0xf0; if ( t1&&t2 ) return 0;
                   1030:                 t1 = u1&       0xf; t2 = u2&       0xf; if ( t1&&t2 ) return 0;
                   1031:             }
                   1032:             return 1;
                   1033:             break;
                   1034:         case 6:
                   1035:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1036:                 u1 = d1[i]; u2 = d2[i];
                   1037:                 t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
                   1038:                 t1 = u1&  0xfc0000; t2 = u2&  0xfc0000; if ( t1&&t2 ) return 0;
                   1039:                 t1 = u1&   0x3f000; t2 = u2&   0x3f000; if ( t1&&t2 ) return 0;
                   1040:                 t1 = u1&     0xfc0; t2 = u2&     0xfc0; if ( t1&&t2 ) return 0;
                   1041:                 t1 = u1&      0x3f; t2 = u2&      0x3f; if ( t1&&t2 ) return 0;
                   1042:             }
                   1043:             return 1;
                   1044:             break;
                   1045:         case 8:
                   1046:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1047:                 u1 = d1[i]; u2 = d2[i];
                   1048:                 t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
                   1049:                 t1 = u1&  0xff0000; t2 = u2&  0xff0000; if ( t1&&t2 ) return 0;
                   1050:                 t1 = u1&    0xff00; t2 = u2&    0xff00; if ( t1&&t2 ) return 0;
                   1051:                 t1 = u1&      0xff; t2 = u2&      0xff; if ( t1&&t2 ) return 0;
                   1052:             }
                   1053:             return 1;
                   1054:             break;
                   1055:         case 16:
                   1056:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1057:                 u1 = d1[i]; u2 = d2[i];
                   1058:                 t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
                   1059:                 t1 = u1&    0xffff; t2 = u2&    0xffff; if ( t1&&t2 ) return 0;
                   1060:             }
                   1061:             return 1;
                   1062:             break;
                   1063:         case 32:
                   1064:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                   1065:                 if ( d1[i] && d2[i] ) return 0;
                   1066:             return 1;
                   1067:             break;
                   1068:         default:
                   1069:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1070:                 u1 = d1[i]; u2 = d2[i];
                   1071:                 for ( j = 0; j < nd_epw; j++ ) {
                   1072:                     if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                   1073:                     u1 >>= nd_bpe; u2 >>= nd_bpe;
                   1074:                 }
                   1075:             }
                   1076:             return 1;
                   1077:             break;
                   1078:     }
                   1079: #else
                   1080:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1081:         u1 = d1[i]; u2 = d2[i];
                   1082:         for ( j = 0; j < nd_epw; j++ ) {
                   1083:             if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                   1084:             u1 >>= nd_bpe; u2 >>= nd_bpe;
                   1085:         }
                   1086:     }
                   1087:     return 1;
                   1088: #endif
                   1089: }
                   1090:
                   1091: int ndl_check_bound(UINT *d1,UINT *d2)
                   1092: {
                   1093:     UINT u2;
                   1094:     int i,j,ind,k;
                   1095:
                   1096:     ind = 0;
                   1097: #if USE_UNROLL
                   1098:     switch ( nd_bpe ) {
                   1099:         case 3:
                   1100:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1101:                 u2 = d2[i];
                   1102:                 if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
                   1103:                 if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
                   1104:                 if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
                   1105:                 if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
                   1106:                 if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
                   1107:                 if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
                   1108:                 if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
                   1109:                 if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
                   1110:                 if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
                   1111:                 if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
                   1112:             }
                   1113:             return 0;
                   1114:             break;
                   1115:         case 4:
                   1116:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1117:                 u2 = d2[i];
                   1118:                 if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
                   1119:                 if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
                   1120:                 if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
                   1121:                 if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
                   1122:                 if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
                   1123:                 if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
                   1124:                 if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
                   1125:                 if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
                   1126:             }
                   1127:             return 0;
                   1128:             break;
                   1129:         case 6:
                   1130:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1131:                 u2 = d2[i];
                   1132:                 if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
                   1133:                 if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
                   1134:                 if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
                   1135:                 if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
                   1136:                 if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
                   1137:             }
                   1138:             return 0;
                   1139:             break;
                   1140:         case 8:
                   1141:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1142:                 u2 = d2[i];
                   1143:                 if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
                   1144:                 if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
                   1145:                 if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
                   1146:                 if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
                   1147:             }
                   1148:             return 0;
                   1149:             break;
                   1150:         case 16:
                   1151:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1152:                 u2 = d2[i];
                   1153:                 if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
                   1154:                 if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
                   1155:             }
                   1156:             return 0;
                   1157:             break;
                   1158:         case 32:
                   1159:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                   1160:                 if ( d1[i]+d2[i]<d1[i] ) return 1;
                   1161:             return 0;
                   1162:             break;
                   1163:         default:
                   1164:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1165:                 u2 = d2[i];
                   1166:                 k = (nd_epw-1)*nd_bpe;
                   1167:                 for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1168:                     if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1169:             }
                   1170:             return 0;
                   1171:             break;
                   1172:     }
                   1173: #else
                   1174:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1175:         u2 = d2[i];
                   1176:         k = (nd_epw-1)*nd_bpe;
                   1177:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1178:             if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1179:     }
                   1180:     return 0;
                   1181: #endif
                   1182: }
                   1183:
                   1184: int ndl_check_bound2(int index,UINT *d2)
                   1185: {
                   1186:     return ndl_check_bound(nd_bound[index],d2);
                   1187: }
                   1188:
                   1189: INLINE int ndl_hash_value(UINT *d)
                   1190: {
                   1191:     int i;
1.11      noro     1192:     UINT r;
1.1       noro     1193:
                   1194:     r = 0;
                   1195:     for ( i = 0; i < nd_wpd; i++ )
1.12      noro     1196:         r = (r*1511+d[i]);
1.11      noro     1197:     r %= REDTAB_LEN;
1.1       noro     1198:     return r;
                   1199: }
                   1200:
                   1201: INLINE int ndl_find_reducer(UINT *dg)
                   1202: {
                   1203:     RHist r;
                   1204:     int d,k,i;
                   1205:
                   1206:     d = ndl_hash_value(dg);
                   1207:     for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   1208:         if ( ndl_equal(dg,DL(r)) ) {
                   1209:             if ( k > 0 ) nd_notfirst++;
                   1210:             nd_found++;
                   1211:             return r->index;
                   1212:         }
                   1213:     }
                   1214:     if ( Reverse )
                   1215:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   1216:             r = nd_psh[i];
                   1217:             if ( ndl_reducible(dg,DL(r)) ) {
                   1218:                 nd_create++;
                   1219:                 nd_append_red(dg,i);
                   1220:                 return i;
                   1221:             }
                   1222:         }
                   1223:     else
                   1224:         for ( i = 0; i < nd_psn; i++ ) {
                   1225:             r = nd_psh[i];
                   1226:             if ( ndl_reducible(dg,DL(r)) ) {
                   1227:                 nd_create++;
                   1228:                 nd_append_red(dg,i);
                   1229:                 return i;
                   1230:             }
                   1231:         }
                   1232:     return -1;
                   1233: }
                   1234:
1.41      noro     1235: INLINE int ndl_find_reducer_nonsig(UINT *dg)
                   1236: {
                   1237:     RHist r;
                   1238:     int i;
                   1239:
                   1240:     for ( i = 0; i < nd_psn; i++ ) {
                   1241:       r = nd_psh[i];
                   1242:       if ( ndl_reducible(dg,DL(r)) ) return i;
                   1243:     }
                   1244:     return -1;
                   1245: }
                   1246:
1.24      noro     1247: // ret=0,...,nd_psn-1 => reducer found
                   1248: // ret=nd_psn => reducer not found
                   1249: // ret=-1 => singular top reducible
                   1250:
                   1251: int comp_sig(SIG s1,SIG s2);
                   1252: void _ndltodl(UINT *ndl,DL dl);
                   1253:
                   1254: void print_sig(SIG s)
                   1255: {
                   1256:   int i;
                   1257:
                   1258:   fprintf(asir_out,"<<");
                   1259:   for ( i = 0; i < nd_nvar; i++ ) {
                   1260:     fprintf(asir_out,"%d",s->dl->d[i]);
                   1261:     if ( i != nd_nvar-1 ) fprintf(asir_out,",");
                   1262:   }
                   1263:   fprintf(asir_out,">>*e%d",s->pos);
                   1264: }
                   1265:
1.45      noro     1266: void print_siglist(NODE l)
                   1267: {
                   1268:   for ( ; l; l = NEXT(l) )
                   1269:    print_sig((SIG)l->body);
                   1270: }
                   1271:
                   1272:
1.35      noro     1273: // assuming increasing order wrt signature
                   1274:
1.24      noro     1275: INLINE int ndl_find_reducer_s(UINT *dg,SIG sig)
                   1276: {
                   1277:   RHist r;
1.27      noro     1278:   int i,singular,ret,d,k;
1.26      noro     1279:   static int wpd,nvar;
1.24      noro     1280:   static SIG quo;
                   1281:   static UINT *tmp;
                   1282:
1.26      noro     1283:   if ( !quo || nvar != nd_nvar ) NEWSIG(quo);
                   1284:   if ( wpd != nd_wpd ) {
1.24      noro     1285:     wpd = nd_wpd;
                   1286:     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
                   1287:   }
1.27      noro     1288:   d = ndl_hash_value(dg);
1.35      noro     1289: #if 1
1.27      noro     1290:   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   1291:     if ( ndl_equal(dg,DL(r)) ) {
                   1292:       return r->index;
                   1293:     }
                   1294:   }
1.35      noro     1295: #endif
1.24      noro     1296:   singular = 0;
                   1297:   for ( i = 0; i < nd_psn; i++ ) {
                   1298:     r = nd_psh[i];
                   1299:     if ( ndl_reducible(dg,DL(r)) ) {
1.27      noro     1300:       ndl_sub(dg,DL(r),tmp);
1.24      noro     1301:       _ndltodl(tmp,DL(quo));
                   1302:       _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
                   1303:       quo->pos = nd_psh[i]->sig->pos;
1.55      noro     1304:       _adddl(nd_nvar,DL(quo),nd_sba_hm[quo->pos],DL2(quo));
1.24      noro     1305:       ret = comp_sig(sig,quo);
                   1306:       if ( ret > 0 ) { singular = 0; break; }
1.38      noro     1307:       if ( ret == 0 ) { /* fprintf(asir_out,"s"); fflush(asir_out); */ singular = 1; }
1.24      noro     1308:     }
                   1309:   }
                   1310:   if ( singular ) return -1;
1.27      noro     1311:   else if ( i < nd_psn )
                   1312:     nd_append_red(dg,i);
                   1313:   return i;
1.24      noro     1314: }
                   1315:
1.1       noro     1316: ND nd_merge(ND p1,ND p2)
                   1317: {
                   1318:     int n,c;
                   1319:     int t,can,td1,td2;
                   1320:     ND r;
                   1321:     NM m1,m2,mr0,mr,s;
                   1322:
                   1323:     if ( !p1 ) return p2;
                   1324:     else if ( !p2 ) return p1;
                   1325:     else {
                   1326:         can = 0;
                   1327:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1328:             c = DL_COMPARE(DL(m1),DL(m2));
                   1329:             switch ( c ) {
                   1330:                 case 0:
                   1331:                     s = m1; m1 = NEXT(m1);
                   1332:                     can++; NEXTNM2(mr0,mr,s);
                   1333:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1334:                     break;
                   1335:                 case 1:
                   1336:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1337:                     break;
                   1338:                 case -1:
                   1339:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1340:                     break;
                   1341:             }
                   1342:         }
                   1343:         if ( !mr0 )
                   1344:             if ( m1 ) mr0 = m1;
                   1345:             else if ( m2 ) mr0 = m2;
                   1346:             else return 0;
                   1347:         else if ( m1 ) NEXT(mr) = m1;
                   1348:         else if ( m2 ) NEXT(mr) = m2;
                   1349:         else NEXT(mr) = 0;
                   1350:         BDY(p1) = mr0;
                   1351:         SG(p1) = MAX(SG(p1),SG(p2));
                   1352:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1353:         FREEND(p2);
                   1354:         return p1;
                   1355:     }
                   1356: }
                   1357:
                   1358: ND nd_add(int mod,ND p1,ND p2)
                   1359: {
                   1360:     int n,c;
                   1361:     int t,can,td1,td2;
                   1362:     ND r;
                   1363:     NM m1,m2,mr0,mr,s;
                   1364:
1.11      noro     1365:     Nnd_add++;
1.1       noro     1366:     if ( !p1 ) return p2;
                   1367:     else if ( !p2 ) return p1;
                   1368:     else if ( mod == -1 ) return nd_add_sf(p1,p2);
                   1369:     else if ( mod == -2 ) return nd_add_lf(p1,p2);
                   1370:     else if ( !mod ) return nd_add_q(p1,p2);
                   1371:     else {
                   1372:         can = 0;
                   1373:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1374:             c = DL_COMPARE(DL(m1),DL(m2));
                   1375:             switch ( c ) {
                   1376:                 case 0:
                   1377:                     t = ((CM(m1))+(CM(m2))) - mod;
                   1378:                     if ( t < 0 ) t += mod;
                   1379:                     s = m1; m1 = NEXT(m1);
                   1380:                     if ( t ) {
                   1381:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1382:                     } else {
                   1383:                         can += 2; FREENM(s);
                   1384:                     }
                   1385:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1386:                     break;
                   1387:                 case 1:
                   1388:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1389:                     break;
                   1390:                 case -1:
                   1391:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1392:                     break;
                   1393:             }
                   1394:         }
                   1395:         if ( !mr0 )
                   1396:             if ( m1 ) mr0 = m1;
                   1397:             else if ( m2 ) mr0 = m2;
                   1398:             else return 0;
                   1399:         else if ( m1 ) NEXT(mr) = m1;
                   1400:         else if ( m2 ) NEXT(mr) = m2;
                   1401:         else NEXT(mr) = 0;
                   1402:         BDY(p1) = mr0;
                   1403:         SG(p1) = MAX(SG(p1),SG(p2));
                   1404:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1405:         FREEND(p2);
                   1406:         return p1;
                   1407:     }
                   1408: }
                   1409:
                   1410: /* XXX on opteron, the inlined manipulation of destructive additon of
                   1411:  * two NM seems to make gcc optimizer get confused, so the part is
                   1412:  * done in a function.
                   1413:  */
                   1414:
                   1415: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
                   1416: {
                   1417:     NM s;
                   1418:     P t;
                   1419:     int can;
                   1420:
                   1421:     addp(nd_vc,CP(*m1),CP(*m2),&t);
                   1422:     s = *m1; *m1 = NEXT(*m1);
                   1423:     if ( t ) {
                   1424:         can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
                   1425:     } else {
                   1426:         can = 2; FREENM(s);
                   1427:     }
                   1428:     s = *m2; *m2 = NEXT(*m2); FREENM(s);
                   1429:     return can;
                   1430: }
                   1431:
                   1432: ND nd_add_q(ND p1,ND p2)
                   1433: {
                   1434:     int n,c,can;
                   1435:     ND r;
                   1436:     NM m1,m2,mr0,mr,s;
                   1437:     P t;
                   1438:
                   1439:     if ( !p1 ) return p2;
                   1440:     else if ( !p2 ) return p1;
                   1441:     else {
                   1442:         can = 0;
                   1443:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1444:             c = DL_COMPARE(DL(m1),DL(m2));
                   1445:             switch ( c ) {
                   1446:                 case 0:
                   1447: #if defined(__x86_64__)
                   1448:                     can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
                   1449: #else
                   1450:                     addp(nd_vc,CP(m1),CP(m2),&t);
                   1451:                     s = m1; m1 = NEXT(m1);
                   1452:                     if ( t ) {
                   1453:                         can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
                   1454:                     } else {
                   1455:                         can += 2; FREENM(s);
                   1456:                     }
                   1457:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1458: #endif
                   1459:                     break;
                   1460:                 case 1:
                   1461:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1462:                     break;
                   1463:                 case -1:
                   1464:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1465:                     break;
                   1466:             }
                   1467:         }
                   1468:         if ( !mr0 )
                   1469:             if ( m1 ) mr0 = m1;
                   1470:             else if ( m2 ) mr0 = m2;
                   1471:             else return 0;
                   1472:         else if ( m1 ) NEXT(mr) = m1;
                   1473:         else if ( m2 ) NEXT(mr) = m2;
                   1474:         else NEXT(mr) = 0;
                   1475:         BDY(p1) = mr0;
                   1476:         SG(p1) = MAX(SG(p1),SG(p2));
                   1477:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1478:         FREEND(p2);
                   1479:         return p1;
                   1480:     }
                   1481: }
                   1482:
                   1483: ND nd_add_sf(ND p1,ND p2)
                   1484: {
                   1485:     int n,c,can;
                   1486:     ND r;
                   1487:     NM m1,m2,mr0,mr,s;
                   1488:     int t;
                   1489:
                   1490:     if ( !p1 ) return p2;
                   1491:     else if ( !p2 ) return p1;
                   1492:     else {
                   1493:         can = 0;
                   1494:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1495:             c = DL_COMPARE(DL(m1),DL(m2));
                   1496:             switch ( c ) {
                   1497:                 case 0:
                   1498:                     t = _addsf(CM(m1),CM(m2));
                   1499:                     s = m1; m1 = NEXT(m1);
                   1500:                     if ( t ) {
                   1501:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1502:                     } else {
                   1503:                         can += 2; FREENM(s);
                   1504:                     }
                   1505:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1506:                     break;
                   1507:                 case 1:
                   1508:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1509:                     break;
                   1510:                 case -1:
                   1511:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1512:                     break;
                   1513:             }
                   1514:         }
                   1515:         if ( !mr0 )
                   1516:             if ( m1 ) mr0 = m1;
                   1517:             else if ( m2 ) mr0 = m2;
                   1518:             else return 0;
                   1519:         else if ( m1 ) NEXT(mr) = m1;
                   1520:         else if ( m2 ) NEXT(mr) = m2;
                   1521:         else NEXT(mr) = 0;
                   1522:         BDY(p1) = mr0;
                   1523:         SG(p1) = MAX(SG(p1),SG(p2));
                   1524:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1525:         FREEND(p2);
                   1526:         return p1;
                   1527:     }
                   1528: }
                   1529:
                   1530:
                   1531: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
                   1532: {
                   1533:     int c,c1,c2;
                   1534:     Z cg,cred,gcd,tq;
                   1535:     P cgp,credp,gcdp;
                   1536:     Obj tr,tr1;
                   1537:
                   1538:     if ( mod == -1 ) {
                   1539:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1540:         *divp = (Obj)ONE;
                   1541:     } else if ( mod == -2 ) {
                   1542:         Z inv,t;
                   1543:         divlf(ONE,HCZ(p),&inv);
                   1544:         chsgnlf(HCZ(g),&t);
                   1545:         mullf(inv,t,&CZ(mul));
                   1546:         *divp = (Obj)ONE;
                   1547:     } else if ( mod ) {
                   1548:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1549:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1550:         *divp = (Obj)ONE;
                   1551:     } else if ( nd_vc ) {
                   1552:         ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
                   1553:         divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
                   1554:         chsgnp(cgp,&CP(mul));
                   1555:         nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
                   1556:         if ( dn ) {
                   1557:             mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
                   1558:             reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
                   1559:         }
                   1560:         *divp = (Obj)credp;
                   1561:     } else {
1.6       noro     1562:         igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1563:         chsgnz(cg,&CZ(mul));
1.1       noro     1564:         nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
                   1565:         if ( dn ) {
                   1566:             mulz(dn->z,cred,&tq); dn->z = tq;
                   1567:         }
                   1568:         *divp = (Obj)cred;
                   1569:     }
                   1570:     return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
                   1571: }
                   1572:
                   1573: /* ret=1 : success, ret=0 : overflow */
1.6       noro     1574: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1       noro     1575: {
                   1576:     NM m,mrd,tail;
                   1577:     NM mul;
                   1578:     int n,sugar,psugar,sugar0,stat,index;
                   1579:     int c,c1,c2,dummy;
                   1580:     RHist h;
                   1581:     NDV p,red;
                   1582:     Q cg,cred,gcd,tq,qq;
                   1583:     Z iq;
                   1584:     DP dmul;
                   1585:     NODE node;
                   1586:     LIST hist;
                   1587:     double hmag;
                   1588:     P tp,tp1;
                   1589:     Obj tr,tr1,div;
                   1590:     union oNDC hg;
                   1591:     P cont;
                   1592:
                   1593:     if ( !g ) {
                   1594:         *rp = d;
                   1595:         return 1;
                   1596:     }
                   1597:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1598:
                   1599:     sugar0 = sugar = SG(g);
                   1600:     n = NV(g);
                   1601:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1602:     if ( d )
                   1603:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1604:     for ( ; g; ) {
                   1605:         index = ndl_find_reducer(HDL(g));
                   1606:         if ( index >= 0 ) {
                   1607:             h = nd_psh[index];
                   1608:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1609:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1610:                 nd_free(g); nd_free(d);
                   1611:                 return 0;
                   1612:             }
                   1613:             p = nd_demand ? ndv_load(index) : ps[index];
                   1614:             /* d+g -> div*(d+g)+mul*p */
1.6       noro     1615:             g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1       noro     1616:             if ( nd_gentrace ) {
                   1617:                 /* Trace=[div,index,mul,ONE] */
1.6       noro     1618:                 STOZ(index,iq);
1.1       noro     1619:                 nmtodp(mod,mul,&dmul);
                   1620:                 node = mknode(4,div,iq,dmul,ONE);
                   1621:             }
                   1622:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
                   1623:             if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
                   1624:                 hg = HCU(g);
                   1625:                 nd_removecont2(d,g);
1.6       noro     1626:                 if ( nd_gentrace ) {
1.1       noro     1627:                     /* overwrite cont : Trace=[div,index,mul,cont] */
1.6       noro     1628:                     /* exact division */
1.1       noro     1629:                     cont = ndc_div(mod,hg,HCU(g));
                   1630:                     if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
                   1631:                 }
                   1632:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1633:             }
                   1634:             MKLIST(hist,node);
                   1635:             MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
                   1636:         } else if ( !full ) {
                   1637:             *rp = g;
                   1638:             return 1;
                   1639:         } else {
                   1640:             m = BDY(g);
                   1641:             if ( NEXT(m) ) {
                   1642:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1643:             } else {
                   1644:                 FREEND(g); g = 0;
                   1645:             }
                   1646:             if ( d ) {
                   1647:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1648:             } else {
                   1649:                 MKND(n,m,1,d); tail = BDY(d);
                   1650:             }
                   1651:         }
                   1652:     }
                   1653:     if ( d ) SG(d) = sugar;
                   1654:     *rp = d;
                   1655:     return 1;
                   1656: }
                   1657:
1.24      noro     1658: // ret=1 => success
                   1659: // ret=0 => overflow
                   1660: // ret=-1 => singular top reducible
                   1661:
                   1662: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
                   1663: {
                   1664:     NM m,mrd,tail;
                   1665:     NM mul;
                   1666:     int n,sugar,psugar,sugar0,stat,index;
                   1667:     int c,c1,c2,dummy;
                   1668:     RHist h;
                   1669:     NDV p,red;
                   1670:     Q cg,cred,gcd,tq,qq;
                   1671:     Z iq;
                   1672:     DP dmul;
                   1673:     NODE node;
                   1674:     LIST hist;
                   1675:     double hmag;
                   1676:     P tp,tp1;
                   1677:     Obj tr,tr1,div;
                   1678:     union oNDC hg;
                   1679:     P cont;
                   1680:     SIG sig;
                   1681:
                   1682:     if ( !g ) {
                   1683:         *rp = d;
                   1684:         return 1;
                   1685:     }
                   1686:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1687:
                   1688:     sugar0 = sugar = SG(g);
                   1689:     n = NV(g);
                   1690:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1691:     if ( d )
                   1692:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1693:     sig = g->sig;
                   1694:     for ( ; g; ) {
                   1695:         index = ndl_find_reducer_s(HDL(g),sig);
                   1696:         if ( index >= 0 && index < nd_psn ) {
                   1697:             // reducer found
                   1698:             h = nd_psh[index];
                   1699:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1700:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1701:                 nd_free(g); nd_free(d);
                   1702:                 return 0;
                   1703:             }
                   1704:             p = ps[index];
                   1705:             /* d+g -> div*(d+g)+mul*p */
                   1706:             g = nd_reduce2(mod,d,g,p,mul,0,&div);
                   1707:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
                   1708:             if ( !mod && g && ((double)(p_mag(HCP(g))) > hmag) ) {
                   1709:                 hg = HCU(g);
                   1710:                 nd_removecont2(d,g);
                   1711:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1712:             }
1.55      noro     1713:             if ( nd_gentrace ) {
                   1714:                 /* Trace=[div,index,mul,ONE] */
                   1715:                 STOZ(index,iq);
                   1716:                 nmtodp(mod,mul,&dmul);
                   1717:                 node = mknode(4,div,iq,dmul,ONE);
                   1718:             }
                   1719:             MKLIST(hist,node);
                   1720:             MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1.24      noro     1721:         } else if ( index == -1 ) {
                   1722:           // singular top reducible
                   1723:           return -1;
                   1724:         } else if ( !full ) {
                   1725:             *rp = g;
                   1726:             g->sig = sig;
                   1727:             return 1;
                   1728:         } else {
                   1729:             m = BDY(g);
                   1730:             if ( NEXT(m) ) {
                   1731:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1732:             } else {
                   1733:                 FREEND(g); g = 0;
                   1734:             }
                   1735:             if ( d ) {
                   1736:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1737:             } else {
                   1738:                 MKND(n,m,1,d); tail = BDY(d);
                   1739:             }
                   1740:         }
                   1741:     }
                   1742:     if ( d ) {
                   1743:       SG(d) = sugar;
                   1744:       d->sig = sig;
                   1745:     }
                   1746:     *rp = d;
                   1747:     return 1;
                   1748: }
                   1749:
1.1       noro     1750: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
                   1751: {
                   1752:     int hindex,index;
                   1753:     NDV p;
                   1754:     ND u,d,red;
                   1755:     NODE l;
                   1756:     NM mul,m,mrd,tail;
                   1757:     int sugar,psugar,n,h_reducible;
                   1758:     PGeoBucket bucket;
                   1759:     int c,c1,c2;
                   1760:     Z cg,cred,gcd,zzz;
                   1761:     RHist h;
                   1762:     double hmag,gmag;
                   1763:     int count = 0;
                   1764:     int hcount = 0;
                   1765:
                   1766:     if ( !g ) {
                   1767:         *rp = 0;
                   1768:         return 1;
                   1769:     }
                   1770:     sugar = SG(g);
                   1771:     n = NV(g);
1.6       noro     1772:     if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1       noro     1773:     bucket = create_pbucket();
                   1774:     add_pbucket(mod,bucket,g);
                   1775:     d = 0;
                   1776:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1777:     while ( 1 ) {
                   1778:         if ( mod > 0 || mod == -1 )
                   1779:           hindex = head_pbucket(mod,bucket);
                   1780:         else if ( mod == -2 )
                   1781:           hindex = head_pbucket_lf(bucket);
                   1782:         else
                   1783:           hindex = head_pbucket_q(bucket);
                   1784:         if ( hindex < 0 ) {
                   1785:             if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1786:             if ( d ) SG(d) = sugar;
                   1787:             *rp = d;
                   1788:             return 1;
                   1789:         }
                   1790:         g = bucket->body[hindex];
                   1791:         index = ndl_find_reducer(HDL(g));
                   1792:         if ( index >= 0 ) {
                   1793:             count++;
                   1794:             if ( !d ) hcount++;
                   1795:             h = nd_psh[index];
                   1796:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1797:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1798:                 nd_free(d);
                   1799:                 free_pbucket(bucket);
                   1800:                 *rp = 0;
                   1801:                 return 0;
                   1802:             }
                   1803:             p = ps[index];
                   1804:             if ( mod == -1 )
                   1805:                 CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1806:             else if ( mod == -2 ) {
                   1807:                 Z inv,t;
                   1808:                 divlf(ONE,HCZ(p),&inv);
                   1809:                 chsgnlf(HCZ(g),&t);
                   1810:                 mullf(inv,t,&CZ(mul));
                   1811:             } else if ( mod ) {
                   1812:                 c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1813:                 DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1814:             } else {
1.6       noro     1815:                 igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1816:                 chsgnz(cg,&CZ(mul));
1.1       noro     1817:                 nd_mul_c_q(d,(P)cred);
                   1818:                 mulq_pbucket(bucket,cred);
                   1819:                 g = bucket->body[hindex];
1.6       noro     1820:                 gmag = (double)p_mag((P)HCZ(g));
1.1       noro     1821:             }
                   1822:             red = ndv_mul_nm(mod,mul,p);
                   1823:             bucket->body[hindex] = nd_remove_head(g);
                   1824:             red = nd_remove_head(red);
                   1825:             add_pbucket(mod,bucket,red);
                   1826:             psugar = SG(p)+TD(DL(mul));
                   1827:             sugar = MAX(sugar,psugar);
                   1828:             if ( !mod && hmag && (gmag > hmag) ) {
                   1829:                 g = normalize_pbucket(mod,bucket);
                   1830:                 if ( !g ) {
                   1831:                     if ( d ) SG(d) = sugar;
                   1832:                     *rp = d;
                   1833:                     return 1;
                   1834:                 }
                   1835:                 nd_removecont2(d,g);
1.6       noro     1836:                 hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1       noro     1837:                 add_pbucket(mod,bucket,g);
                   1838:             }
                   1839:         } else if ( !full ) {
                   1840:             g = normalize_pbucket(mod,bucket);
                   1841:             if ( g ) SG(g) = sugar;
                   1842:             *rp = g;
                   1843:             return 1;
                   1844:         } else {
                   1845:             m = BDY(g);
                   1846:             if ( NEXT(m) ) {
                   1847:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1848:             } else {
                   1849:                 FREEND(g); g = 0;
                   1850:             }
                   1851:             bucket->body[hindex] = g;
                   1852:             NEXT(m) = 0;
                   1853:             if ( d ) {
                   1854:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1855:             } else {
                   1856:                 MKND(n,m,1,d); tail = BDY(d);
                   1857:             }
                   1858:         }
                   1859:     }
                   1860: }
                   1861:
1.25      noro     1862: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *rp)
                   1863: {
                   1864:   int hindex,index;
                   1865:   NDV p;
                   1866:   ND u,d,red;
                   1867:   NODE l;
                   1868:   NM mul,m,mrd,tail;
                   1869:   int sugar,psugar,n,h_reducible;
                   1870:   PGeoBucket bucket;
                   1871:   int c,c1,c2;
                   1872:   Z cg,cred,gcd,zzz;
                   1873:   RHist h;
                   1874:   double hmag,gmag;
                   1875:   int count = 0;
                   1876:   int hcount = 0;
                   1877:   SIG sig;
                   1878:
                   1879:   if ( !g ) {
                   1880:     *rp = 0;
                   1881:     return 1;
                   1882:   }
                   1883:   sugar = SG(g);
                   1884:   n = NV(g);
                   1885:   if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
                   1886:   bucket = create_pbucket();
                   1887:   add_pbucket(mod,bucket,g);
                   1888:   d = 0;
                   1889:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1890:   sig = g->sig;
                   1891:   while ( 1 ) {
                   1892:     if ( mod > 0 || mod == -1 )
                   1893:       hindex = head_pbucket(mod,bucket);
                   1894:     else if ( mod == -2 )
                   1895:       hindex = head_pbucket_lf(bucket);
                   1896:     else
                   1897:       hindex = head_pbucket_q(bucket);
                   1898:     if ( hindex < 0 ) {
                   1899:       if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1900:       if ( d ) {
                   1901:         SG(d) = sugar;
                   1902:         d->sig = sig;
                   1903:       }
                   1904:       *rp = d;
                   1905:       return 1;
                   1906:     }
                   1907:     g = bucket->body[hindex];
                   1908:     index = ndl_find_reducer_s(HDL(g),sig);
                   1909:     if ( index >= 0 && index < nd_psn ) {
                   1910:       count++;
                   1911:       if ( !d ) hcount++;
                   1912:       h = nd_psh[index];
                   1913:       ndl_sub(HDL(g),DL(h),DL(mul));
                   1914:       if ( ndl_check_bound2(index,DL(mul)) ) {
                   1915:         nd_free(d);
                   1916:         free_pbucket(bucket);
                   1917:         *rp = 0;
                   1918:         return 0;
                   1919:       }
                   1920:       p = ps[index];
                   1921:       if ( mod == -1 )
                   1922:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1923:       else if ( mod == -2 ) {
                   1924:         Z inv,t;
                   1925:         divlf(ONE,HCZ(p),&inv);
                   1926:         chsgnlf(HCZ(g),&t);
                   1927:         mullf(inv,t,&CZ(mul));
                   1928:       } else if ( mod ) {
                   1929:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1930:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1931:       } else {
                   1932:         igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1933:         chsgnz(cg,&CZ(mul));
                   1934:         nd_mul_c_q(d,(P)cred);
                   1935:         mulq_pbucket(bucket,cred);
                   1936:         g = bucket->body[hindex];
                   1937:         gmag = (double)p_mag((P)HCZ(g));
                   1938:       }
                   1939:       red = ndv_mul_nm(mod,mul,p);
                   1940:       bucket->body[hindex] = nd_remove_head(g);
                   1941:       red = nd_remove_head(red);
                   1942:       add_pbucket(mod,bucket,red);
                   1943:       psugar = SG(p)+TD(DL(mul));
                   1944:       sugar = MAX(sugar,psugar);
                   1945:       if ( !mod && hmag && (gmag > hmag) ) {
                   1946:          g = normalize_pbucket(mod,bucket);
                   1947:          if ( !g ) {
                   1948:            if ( d ) {
                   1949:              SG(d) = sugar;
                   1950:              d->sig = sig;
                   1951:            }
                   1952:            *rp = d;
                   1953:            return 1;
                   1954:          }
                   1955:          nd_removecont2(d,g);
                   1956:          hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
                   1957:          add_pbucket(mod,bucket,g);
                   1958:       }
                   1959:     } else if ( index == -1 ) {
                   1960:       // singular top reducible
                   1961:       return -1;
                   1962:     } else if ( !full ) {
                   1963:       g = normalize_pbucket(mod,bucket);
                   1964:       if ( g ) {
                   1965:         SG(g) = sugar;
                   1966:         g->sig = sig;
                   1967:       }
                   1968:       *rp = g;
                   1969:       return 1;
                   1970:     } else {
                   1971:       m = BDY(g);
                   1972:       if ( NEXT(m) ) {
                   1973:         BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1974:       } else {
                   1975:         FREEND(g); g = 0;
                   1976:       }
                   1977:       bucket->body[hindex] = g;
                   1978:       NEXT(m) = 0;
                   1979:       if ( d ) {
                   1980:         NEXT(tail)=m; tail=m; LEN(d)++;
                   1981:       } else {
                   1982:         MKND(n,m,1,d); tail = BDY(d);
                   1983:       }
                   1984:     }
                   1985:   }
                   1986: }
                   1987:
1.1       noro     1988: /* input : list of NDV, cand : list of NDV */
                   1989:
                   1990: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
                   1991: {
                   1992:     int n,i,stat;
                   1993:     ND nf,d;
                   1994:     NDV r;
                   1995:     NODE t,s;
                   1996:     union oNDC dn;
                   1997:     Z q;
                   1998:     LIST list;
                   1999:
1.24      noro     2000:     ndv_setup(m,0,cand,nd_gentrace?1:0,1,0);
1.1       noro     2001:     n = length(cand);
                   2002:
                   2003:   if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
                   2004:     /* membercheck : list is a subset of Id(cand) ? */
                   2005:     for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
                   2006: again:
                   2007:     nd_tracelist = 0;
                   2008:         if ( nd_bpe > obpe )
                   2009:             r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
                   2010:         else
                   2011:             r = (NDV)BDY(t);
                   2012: #if 0
                   2013:         // moved to nd_f4_lf_trace()
                   2014:         if ( m == -2 ) ndv_mod(m,r);
                   2015: #endif
                   2016:         d = ndvtond(m,r);
1.6       noro     2017:         stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1       noro     2018:         if ( !stat ) {
                   2019:             nd_reconstruct(0,0);
                   2020:             goto again;
                   2021:         } else if ( nf ) return 0;
                   2022:     if ( nd_gentrace ) {
                   2023:       nd_tracelist = reverse_node(nd_tracelist);
                   2024:       MKLIST(list,nd_tracelist);
1.6       noro     2025:       STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1       noro     2026:       MKNODE(s,list,nd_alltracelist);
                   2027:       nd_alltracelist = s; nd_tracelist = 0;
                   2028:     }
                   2029:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   2030:     }
                   2031:     if ( DP_Print ) { printf("\n"); }
                   2032:     return 1;
                   2033: }
                   2034:
                   2035: ND nd_remove_head(ND p)
                   2036: {
                   2037:     NM m;
                   2038:
                   2039:     m = BDY(p);
                   2040:     if ( !NEXT(m) ) {
                   2041:         FREEND(p); p = 0;
                   2042:     } else {
                   2043:         BDY(p) = NEXT(m); LEN(p)--;
                   2044:     }
                   2045:     FREENM(m);
                   2046:     return p;
                   2047: }
                   2048:
                   2049: ND nd_separate_head(ND p,ND *head)
                   2050: {
                   2051:     NM m,m0;
                   2052:     ND r;
                   2053:
                   2054:     m = BDY(p);
                   2055:     if ( !NEXT(m) ) {
                   2056:         *head = p; p = 0;
                   2057:     } else {
                   2058:         m0 = m;
                   2059:         BDY(p) = NEXT(m); LEN(p)--;
                   2060:         NEXT(m0) = 0;
                   2061:         MKND(NV(p),m0,1,r);
                   2062:         *head = r;
                   2063:     }
                   2064:     return p;
                   2065: }
                   2066:
                   2067: PGeoBucket create_pbucket()
                   2068: {
                   2069:     PGeoBucket g;
                   2070:
                   2071:     g = CALLOC(1,sizeof(struct oPGeoBucket));
                   2072:     g->m = -1;
                   2073:     return g;
                   2074: }
                   2075:
                   2076: void free_pbucket(PGeoBucket b) {
                   2077:     int i;
                   2078:
                   2079:     for ( i = 0; i <= b->m; i++ )
                   2080:         if ( b->body[i] ) {
                   2081:             nd_free(b->body[i]);
                   2082:             b->body[i] = 0;
                   2083:         }
                   2084:     GCFREE(b);
                   2085: }
                   2086:
1.39      noro     2087: #if 0
1.1       noro     2088: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   2089: {
                   2090:     int l,i,k,m;
                   2091:
                   2092:     if ( !d )
                   2093:         return;
                   2094:     l = LEN(d);
                   2095:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2096:     /* 2^(k-1) < l <= 2^k (=m) */
                   2097:     d = nd_merge(g->body[k],d);
                   2098:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   2099:         g->body[k] = 0;
                   2100:         d = nd_merge(g->body[k+1],d);
                   2101:     }
                   2102:     g->body[k] = d;
                   2103:     g->m = MAX(g->m,k);
                   2104: }
1.39      noro     2105: #else
                   2106: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   2107: {
                   2108:   int l,i,k,m,m0;
                   2109:
                   2110:   if ( !d )
                   2111:     return;
                   2112:   m0 = g->m;
                   2113:   while ( 1 ) {
                   2114:     l = LEN(d);
                   2115:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2116:     /* 2^(k-1) < l <= 2^k (=m) */
                   2117:     if ( g->body[k] == 0 ) {
                   2118:       g->body[k] = d;
                   2119:       m0 = MAX(k,m0);
                   2120:       break;
                   2121:     } else {
                   2122:       d = nd_merge(g->body[k],d);
                   2123:       g->body[k] = 0;
                   2124:     }
                   2125:   }
                   2126:   g->m = m0;
                   2127: }
                   2128: #endif
1.1       noro     2129:
1.39      noro     2130: #if 0
1.1       noro     2131: void add_pbucket(int mod,PGeoBucket g,ND d)
                   2132: {
                   2133:     int l,i,k,m;
                   2134:
                   2135:     if ( !d )
                   2136:         return;
                   2137:     l = LEN(d);
                   2138:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2139:     /* 2^(k-1) < l <= 2^k (=m) */
                   2140:     d = nd_add(mod,g->body[k],d);
                   2141:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   2142:         g->body[k] = 0;
                   2143:         d = nd_add(mod,g->body[k+1],d);
                   2144:     }
                   2145:     g->body[k] = d;
                   2146:     g->m = MAX(g->m,k);
                   2147: }
1.39      noro     2148: #else
                   2149: void add_pbucket(int mod,PGeoBucket g,ND d)
                   2150: {
                   2151:   int l,i,k,m,m0;
                   2152:
                   2153:   m0 = g->m;
                   2154:   while ( d != 0 ) {
                   2155:     l = LEN(d);
                   2156:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2157:     /* 2^(k-1) < l <= 2^k (=m) */
                   2158:     if ( g->body[k] == 0 ) {
                   2159:       g->body[k] = d;
                   2160:       m0 = MAX(k,m0);
                   2161:       break;
                   2162:     } else {
                   2163:       d = nd_add(mod,g->body[k],d);
                   2164:       g->body[k] = 0;
                   2165:     }
                   2166:   }
                   2167:   g->m = m0;
                   2168: }
                   2169: #endif
1.1       noro     2170:
                   2171: void mulq_pbucket(PGeoBucket g,Z c)
                   2172: {
                   2173:     int k;
                   2174:
                   2175:     for ( k = 0; k <= g->m; k++ )
                   2176:         nd_mul_c_q(g->body[k],(P)c);
                   2177: }
                   2178:
                   2179: NM remove_head_pbucket_symbolic(PGeoBucket g)
                   2180: {
                   2181:     int j,i,k,c;
                   2182:     NM head;
                   2183:
                   2184:     k = g->m;
                   2185:     j = -1;
                   2186:     for ( i = 0; i <= k; i++ ) {
                   2187:         if ( !g->body[i] ) continue;
                   2188:         if ( j < 0 ) j = i;
                   2189:         else {
                   2190:             c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
                   2191:             if ( c > 0 )
                   2192:                 j = i;
                   2193:             else if ( c == 0 )
                   2194:                 g->body[i] = nd_remove_head(g->body[i]);
                   2195:         }
                   2196:     }
                   2197:     if ( j < 0 ) return 0;
                   2198:     else {
                   2199:         head = BDY(g->body[j]);
                   2200:         if ( !NEXT(head) ) {
                   2201:             FREEND(g->body[j]);
                   2202:             g->body[j] = 0;
                   2203:         } else {
                   2204:             BDY(g->body[j]) = NEXT(head);
                   2205:             LEN(g->body[j])--;
                   2206:         }
                   2207:         return head;
                   2208:     }
                   2209: }
                   2210:
                   2211: int head_pbucket(int mod,PGeoBucket g)
                   2212: {
                   2213:     int j,i,c,k,nv,sum;
                   2214:     UINT *di,*dj;
                   2215:     ND gi,gj;
                   2216:
                   2217:     k = g->m;
                   2218:     while ( 1 ) {
                   2219:         j = -1;
                   2220:         for ( i = 0; i <= k; i++ ) {
                   2221:             if ( !(gi = g->body[i]) )
                   2222:                 continue;
                   2223:             if ( j < 0 ) {
                   2224:                 j = i;
                   2225:                 gj = g->body[j];
                   2226:                 dj = HDL(gj);
                   2227:                 sum = HCM(gj);
                   2228:             } else {
                   2229:                 c = DL_COMPARE(HDL(gi),dj);
                   2230:                 if ( c > 0 ) {
                   2231:                     if ( sum ) HCM(gj) = sum;
                   2232:                     else g->body[j] = nd_remove_head(gj);
                   2233:                     j = i;
                   2234:                     gj = g->body[j];
                   2235:                     dj = HDL(gj);
                   2236:                     sum = HCM(gj);
                   2237:                 } else if ( c == 0 ) {
                   2238:                     if ( mod == -1 )
                   2239:                         sum = _addsf(sum,HCM(gi));
                   2240:                     else {
                   2241:                         sum = sum+HCM(gi)-mod;
                   2242:                         if ( sum < 0 ) sum += mod;
                   2243:                     }
                   2244:                     g->body[i] = nd_remove_head(gi);
                   2245:                 }
                   2246:             }
                   2247:         }
                   2248:         if ( j < 0 ) return -1;
                   2249:         else if ( sum ) {
                   2250:             HCM(gj) = sum;
                   2251:             return j;
                   2252:         } else
                   2253:             g->body[j] = nd_remove_head(gj);
                   2254:     }
                   2255: }
                   2256:
                   2257: int head_pbucket_q(PGeoBucket g)
                   2258: {
                   2259:     int j,i,c,k,nv;
                   2260:     Z sum,t;
                   2261:     ND gi,gj;
                   2262:
                   2263:     k = g->m;
                   2264:     while ( 1 ) {
                   2265:         j = -1;
                   2266:         for ( i = 0; i <= k; i++ ) {
                   2267:             if ( !(gi = g->body[i]) ) continue;
                   2268:             if ( j < 0 ) {
                   2269:                 j = i;
                   2270:                 gj = g->body[j];
1.6       noro     2271:                 sum = HCZ(gj);
1.1       noro     2272:             } else {
                   2273:                 nv = NV(gi);
                   2274:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   2275:                 if ( c > 0 ) {
1.6       noro     2276:                     if ( sum ) HCZ(gj) = sum;
1.1       noro     2277:                     else g->body[j] = nd_remove_head(gj);
                   2278:                     j = i;
                   2279:                     gj = g->body[j];
1.6       noro     2280:                     sum = HCZ(gj);
1.1       noro     2281:                 } else if ( c == 0 ) {
1.6       noro     2282:                     addz(sum,HCZ(gi),&t);
1.1       noro     2283:                     sum = t;
                   2284:                     g->body[i] = nd_remove_head(gi);
                   2285:                 }
                   2286:             }
                   2287:         }
                   2288:         if ( j < 0 ) return -1;
                   2289:         else if ( sum ) {
1.6       noro     2290:             HCZ(gj) = sum;
1.1       noro     2291:             return j;
                   2292:         } else
                   2293:             g->body[j] = nd_remove_head(gj);
                   2294:     }
                   2295: }
                   2296:
                   2297: int head_pbucket_lf(PGeoBucket g)
                   2298: {
                   2299:     int j,i,c,k,nv;
                   2300:     Z sum,t;
                   2301:     ND gi,gj;
                   2302:
                   2303:     k = g->m;
                   2304:     while ( 1 ) {
                   2305:         j = -1;
                   2306:         for ( i = 0; i <= k; i++ ) {
                   2307:             if ( !(gi = g->body[i]) ) continue;
                   2308:             if ( j < 0 ) {
                   2309:                 j = i;
                   2310:                 gj = g->body[j];
                   2311:                 sum = HCZ(gj);
                   2312:             } else {
                   2313:                 nv = NV(gi);
                   2314:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   2315:                 if ( c > 0 ) {
                   2316:                     if ( sum ) HCZ(gj) = sum;
                   2317:                     else g->body[j] = nd_remove_head(gj);
                   2318:                     j = i;
                   2319:                     gj = g->body[j];
                   2320:                     sum = HCZ(gj);
                   2321:                 } else if ( c == 0 ) {
                   2322:                     addlf(sum,HCZ(gi),&t);
                   2323:                     sum = t;
                   2324:                     g->body[i] = nd_remove_head(gi);
                   2325:                 }
                   2326:             }
                   2327:         }
                   2328:         if ( j < 0 ) return -1;
                   2329:         else if ( sum ) {
                   2330:             HCZ(gj) = sum;
                   2331:             return j;
                   2332:         } else
                   2333:             g->body[j] = nd_remove_head(gj);
                   2334:     }
                   2335: }
                   2336:
                   2337: ND normalize_pbucket(int mod,PGeoBucket g)
                   2338: {
                   2339:     int i;
                   2340:     ND r,t;
                   2341:
                   2342:     r = 0;
                   2343:     for ( i = 0; i <= g->m; i++ ) {
                   2344:         r = nd_add(mod,r,g->body[i]);
                   2345:         g->body[i] = 0;
                   2346:     }
                   2347:     g->m = -1;
                   2348:     return r;
                   2349: }
                   2350:
                   2351: #if 0
                   2352: void register_hcf(NDV p)
                   2353: {
                   2354:     DCP dc,t;
                   2355:     P hc,h;
                   2356:     int c;
                   2357:     NODE l,l1,prev;
                   2358:
                   2359:     hc = p->body->c.p;
                   2360:     if ( !nd_vc || NUM(hc) ) return;
                   2361:     fctrp(nd_vc,hc,&dc);
                   2362:     for ( t = dc; t; t = NEXT(t) ) {
                   2363:         h = t->c;
                   2364:         if ( NUM(h) ) continue;
                   2365:         for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   2366:             c = compp(nd_vc,h,(P)BDY(l));
                   2367:             if ( c >= 0 ) break;
                   2368:         }
                   2369:         if ( !l || c > 0  ) {
                   2370:             MKNODE(l1,h,l);
                   2371:             if ( !prev )
                   2372:                 nd_hcf = l1;
                   2373:             else
                   2374:                 NEXT(prev) = l1;
                   2375:         }
                   2376:     }
                   2377: }
                   2378: #else
                   2379: void register_hcf(NDV p)
                   2380: {
                   2381:     DCP dc,t;
                   2382:     P hc,h,q;
                   2383:     Q dmy;
                   2384:     int c;
                   2385:     NODE l,l1,prev;
                   2386:
                   2387:     hc = p->body->c.p;
                   2388:     if ( NUM(hc) ) return;
                   2389:     ptozp(hc,1,&dmy,&h);
                   2390: #if 1
                   2391:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   2392:         while ( 1 ) {
                   2393:             if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
                   2394:             else break;
                   2395:         }
                   2396:     }
                   2397:     if ( NUM(h) ) return;
                   2398: #endif
                   2399:     for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   2400:         c = compp(nd_vc,h,(P)BDY(l));
                   2401:         if ( c >= 0 ) break;
                   2402:     }
                   2403:     if ( !l || c > 0  ) {
                   2404:         MKNODE(l1,h,l);
                   2405:         if ( !prev )
                   2406:             nd_hcf = l1;
                   2407:         else
                   2408:             NEXT(prev) = l1;
                   2409:     }
                   2410: }
                   2411: #endif
                   2412:
                   2413: int do_diagonalize(int sugar,int m)
                   2414: {
1.6       noro     2415:   int i,nh,stat;
                   2416:   NODE r,g,t;
                   2417:   ND h,nf,s,head;
                   2418:   NDV nfv;
                   2419:   Q q;
                   2420:   P nm,nmp,dn,mnp,dnp,cont,cont1;
                   2421:   union oNDC hc;
                   2422:   NODE node;
                   2423:   LIST l;
                   2424:   Z iq;
1.1       noro     2425:
1.6       noro     2426:   for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   2427:     if ( nd_gentrace ) {
                   2428:       /* Trace = [1,index,1,1] */
                   2429:       STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2430:       MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2431:     }
                   2432:     if ( nd_demand )
                   2433:       nfv = ndv_load(i);
                   2434:     else
                   2435:       nfv = nd_ps[i];
                   2436:     s = ndvtond(m,nfv);
                   2437:     s = nd_separate_head(s,&head);
                   2438:     stat = nd_nf(m,head,s,nd_ps,1,&nf);
                   2439:     if ( !stat ) return 0;
                   2440:     ndv_free(nfv);
                   2441:     hc = HCU(nf); nd_removecont(m,nf);
                   2442:     /* exact division */
                   2443:     cont = ndc_div(m,hc,HCU(nf));
1.1       noro     2444:     if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6       noro     2445:     nfv = ndtondv(m,nf);
                   2446:     nd_free(nf);
                   2447:     nd_bound[i] = ndv_compute_bound(nfv);
                   2448:     if ( !m ) register_hcf(nfv);
                   2449:     if ( nd_demand ) {
                   2450:       ndv_save(nfv,i);
                   2451:       ndv_free(nfv);
                   2452:     } else
                   2453:       nd_ps[i] = nfv;
                   2454:   }
                   2455:   return 1;
1.1       noro     2456: }
                   2457:
                   2458: LIST compute_splist()
                   2459: {
                   2460:   NODE g,tn0,tn,node;
                   2461:   LIST l0;
                   2462:   ND_pairs d,t;
                   2463:   int i;
                   2464:   Z i1,i2;
                   2465:
                   2466:     g = 0; d = 0;
                   2467:     for ( i = 0; i < nd_psn; i++ ) {
                   2468:         d = update_pairs(d,g,i,0);
                   2469:         g = update_base(g,i);
                   2470:     }
                   2471:   for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
                   2472:     NEXTNODE(tn0,tn);
1.6       noro     2473:         STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1       noro     2474:         node = mknode(2,i1,i2); MKLIST(l0,node);
                   2475:     BDY(tn) = l0;
                   2476:   }
                   2477:   if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
                   2478:   return l0;
                   2479: }
                   2480:
1.49      noro     2481: typedef struct oHPDATA {
1.56      noro     2482:   int n;
                   2483:   P hn; // HP(t)=hn(t)/((1-t^w0)*...*(1-t^w(n-1)))
1.49      noro     2484:   VECT x; // BDY(x)[i] = <<0,...,1,...,0>>
1.56      noro     2485:   int *w;
1.49      noro     2486: } *HPDATA;
                   2487:
                   2488: void make_reduced(VECT b,int nv);
                   2489: void mhp_rec(VECT b,VECT x,P t,P *r);
                   2490: P mhp_ctop(P *r,P *plist,int n);
                   2491: DL monomial_colon(DL a,DL b,int n);
1.56      noro     2492: LIST dp_monomial_hilbert_poincare(VECT b,VECT x);
                   2493: LIST dp_monomial_hilbert_poincare_weight(VECT b,VECT x,int *w);
1.49      noro     2494:
                   2495: void setup_hpdata(HPDATA final,HPDATA current)
                   2496: {
1.56      noro     2497:   int n,i,wlen;
1.49      noro     2498:   DL *p;
1.56      noro     2499:   VECT b,x;
1.49      noro     2500:   DL dl;
1.56      noro     2501:   LIST weight;
                   2502:   LIST ret;
                   2503:   int *w;
                   2504:   NODE nd;
                   2505:
                   2506:   final->n = n = nd_nvar;
                   2507:   final->hn = (P)BDY(nd_hpdata);
1.59      noro     2508: #if 0
1.56      noro     2509:   if ( NEXT(nd_hpdata) != 0 && (weight=(LIST)BDY(NEXT(nd_hpdata))) != 0 ) {
                   2510:     wlen = length(BDY(weight));
                   2511:     if ( n != wlen )
                   2512:       error("setup_hpdata : inconsistent weight length");
                   2513:     w = (int *)MALLOC(n*sizeof(int));
                   2514:     for ( i = 0, nd = BDY((LIST)weight); i < n; i++, nd = NEXT(nd) )
                   2515:       w[i] = ZTOS((Z)BDY(nd));
                   2516:   } else
                   2517:     w = 0;
1.59      noro     2518: #else
                   2519:   w = current_dl_weight_vector;
                   2520: #endif
1.49      noro     2521:   MKVECT(x,n);
                   2522:   for ( i = 0; i < n; i++ ) {
                   2523:     NEWDL(dl,n); dl->d[i] = 1; dl->td = 1; BDY(x)[i] = dl;
                   2524:   }
                   2525:   final->x = x;
1.56      noro     2526:   final->w = w;
1.49      noro     2527:
                   2528:   MKVECT(b,nd_psn); p = (DL *)BDY(b);
                   2529:   for ( i = 0; i < nd_psn; i++ ) {
                   2530:     p[i] = ndltodl(n,nd_psh[i]->dl);
                   2531:   }
1.56      noro     2532:   if ( w ) {
                   2533:     ret = dp_monomial_hilbert_poincare_weight(b,x,w);
                   2534:   } else
                   2535:     ret = dp_monomial_hilbert_poincare(b,x);
                   2536:   current->n = n;
                   2537:   current->hn = (P)BDY(BDY(ret));
1.49      noro     2538:   current->x = x;
1.56      noro     2539:   current->w = w;
                   2540: }
                   2541:
                   2542: int comp_hn(P a, P b)
                   2543: {
                   2544:   P s;
                   2545:   DCP dc;
                   2546:
                   2547:   subp(CO,a,b,&s);
1.57      noro     2548:   if ( !s ) return -1;
1.56      noro     2549:   else if ( OID(s) == 1 ) return 0;
                   2550:   else {
                   2551:     for ( dc = DC(s); NEXT(dc); dc = NEXT(dc) );
                   2552:     return (int)ZTOS((Z)dc->d);
                   2553:   }
1.49      noro     2554: }
                   2555:
1.56      noro     2556: void update_hpdata(HPDATA current,int nh)
1.49      noro     2557: {
                   2558:   NODE data1,nd,t;
                   2559:   DL new,dl;
                   2560:   int len,i,n;
                   2561:   Z dz;
                   2562:   DL *p;
                   2563:   VECT b,head;
                   2564:   P tv,td,s,hn,hpoly;
                   2565:   LIST list1;
                   2566:
                   2567:   n = nd_nvar;
                   2568:   new = ndltodl(n,nd_psh[nh]->dl);
                   2569:   MKVECT(b,nh); p = (DL *)BDY(b);
                   2570:   for ( i = 0; i < nh; i++ ) {
                   2571:     p[i] = monomial_colon(ndltodl(n,nd_psh[i]->dl),new,n);
                   2572:   }
                   2573:   // compute HP(I:new)
1.56      noro     2574:   if ( current->w )
                   2575:     list1 = dp_monomial_hilbert_poincare_weight(b,current->x,current->w);
                   2576:   else
                   2577:     list1 = dp_monomial_hilbert_poincare(b,current->x);
1.49      noro     2578:   data1 = BDY((LIST)list1);
                   2579:   // HP(I+<new>) = H(I)-t^d*H(I:new), d=tdeg(new)
                   2580:   makevar("t",&tv); UTOZ(new->td,dz);
                   2581:   pwrp(CO,tv,dz,&td);
                   2582:   mulp(CO,(P)ARG0(data1),td,&s);
                   2583:   subp(CO,current->hn,s,&hn);
                   2584:   current->hn = hn;
                   2585: }
                   2586:
                   2587: ND_pairs nd_remove_same_sugar( ND_pairs d, int sugar)
                   2588: {
                   2589:   struct oND_pairs root;
                   2590:   ND_pairs prev,cur;
1.61      noro     2591:   int i;
1.49      noro     2592:
                   2593:   root.next = d;
                   2594:   prev = &root; cur = d;
1.61      noro     2595:   i = 0;
1.49      noro     2596:   while ( cur ) {
1.61      noro     2597:     if ( SG(cur) == sugar ) {
1.49      noro     2598:       prev->next = cur->next;
1.61      noro     2599:       i++;
                   2600:     } else
1.49      noro     2601:       prev = cur;
                   2602:     cur = cur->next;
                   2603:   }
1.63    ! noro     2604:   if ( DP_Print && i ) fprintf(asir_out,"[%d]",i);
1.49      noro     2605:   return root.next;
                   2606: }
                   2607:
1.1       noro     2608: /* return value = 0 => input is not a GB */
                   2609:
                   2610: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
                   2611: {
1.6       noro     2612:   int i,nh,sugar,stat;
                   2613:   NODE r,g,t;
                   2614:   ND_pairs d;
                   2615:   ND_pairs l;
                   2616:   ND h,nf,s,head,nf1;
                   2617:   NDV nfv;
                   2618:   Z q;
                   2619:   union oNDC dn,hc;
                   2620:   int diag_count = 0;
1.41      noro     2621:   int Nnfnz = 0,Nnfz = 0;
1.6       noro     2622:   P cont;
                   2623:   LIST list;
1.49      noro     2624:   struct oHPDATA current_hpdata,final_hpdata;
                   2625:   int final_hpvalue;
                   2626:
1.27      noro     2627: struct oEGT eg1,eg2,eg_update;
1.6       noro     2628:
1.27      noro     2629: init_eg(&eg_update);
1.11      noro     2630:   Nnd_add = 0;
1.6       noro     2631:   g = 0; d = 0;
                   2632:   for ( i = 0; i < nd_psn; i++ ) {
                   2633:     d = update_pairs(d,g,i,gensyz);
                   2634:     g = update_base(g,i);
                   2635:   }
                   2636:   sugar = 0;
1.49      noro     2637:   if ( nd_hpdata ) {
1.52      noro     2638:     if ( DP_Print ) fprintf(asir_out,"Hilbert driven algorithm.\n");
1.49      noro     2639:     setup_hpdata(&final_hpdata,&current_hpdata);
                   2640:   }
1.6       noro     2641:   while ( d ) {
1.1       noro     2642: again:
1.6       noro     2643:     l = nd_minp(d,&d);
                   2644:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   2645:     if ( SG(l) != sugar ) {
                   2646:       if ( ishomo ) {
                   2647:         diag_count = 0;
                   2648:         stat = do_diagonalize(sugar,m);
1.1       noro     2649:         if ( !stat ) {
1.6       noro     2650:           NEXT(l) = d; d = l;
                   2651:           d = nd_reconstruct(0,d);
                   2652:           goto again;
1.1       noro     2653:         }
1.6       noro     2654:       }
                   2655:       sugar = SG(l);
                   2656:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2657:     }
                   2658:     stat = nd_sp(m,0,l,&h);
                   2659:     if ( !stat ) {
                   2660:       NEXT(l) = d; d = l;
                   2661:       d = nd_reconstruct(0,d);
                   2662:       goto again;
                   2663:     }
1.1       noro     2664: #if USE_GEOBUCKET
1.39      noro     2665:     stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf)
                   2666:       :nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     2667: #else
1.39      noro     2668:     stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     2669: #endif
1.6       noro     2670:     if ( !stat ) {
                   2671:       NEXT(l) = d; d = l;
                   2672:       d = nd_reconstruct(0,d);
                   2673:       goto again;
                   2674:     } else if ( nf ) {
1.41      noro     2675:       Nnfnz++;
1.6       noro     2676:       if ( checkonly || gensyz ) return 0;
1.1       noro     2677:       if ( nd_newelim ) {
                   2678:         if ( nd_module ) {
                   2679:           if ( MPOS(HDL(nf)) > 1 ) return 0;
                   2680:         } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
                   2681:       }
1.6       noro     2682:       if ( DP_Print ) { printf("+"); fflush(stdout); }
                   2683:       hc = HCU(nf);
                   2684:       nd_removecont(m,nf);
                   2685:       if ( !m && nd_nalg ) {
                   2686:         nd_monic(0,&nf);
                   2687:         nd_removecont(m,nf);
                   2688:       }
                   2689:       if ( nd_gentrace ) {
                   2690:         /* exact division */
1.1       noro     2691:         cont = ndc_div(m,hc,HCU(nf));
                   2692:         if ( m || !UNIQ(cont) ) {
1.6       noro     2693:           t = mknode(4,NULLP,NULLP,NULLP,cont);
                   2694:           MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1       noro     2695:           nd_tracelist = t;
                   2696:         }
                   2697:       }
1.6       noro     2698:       nfv = ndtondv(m,nf); nd_free(nf);
1.24      noro     2699:       nh = ndv_newps(m,nfv,0);
1.6       noro     2700:       if ( !m && (ishomo && ++diag_count == diag_period) ) {
                   2701:         diag_count = 0;
                   2702:         stat = do_diagonalize(sugar,m);
                   2703:         if ( !stat ) {
                   2704:           NEXT(l) = d; d = l;
                   2705:           d = nd_reconstruct(1,d);
                   2706:           goto again;
1.1       noro     2707:         }
1.6       noro     2708:       }
1.27      noro     2709: get_eg(&eg1);
1.6       noro     2710:       d = update_pairs(d,g,nh,0);
1.27      noro     2711: get_eg(&eg2); add_eg(&eg_update,&eg1,&eg2);
1.6       noro     2712:       g = update_base(g,nh);
                   2713:       FREENDP(l);
1.49      noro     2714:       if ( nd_hpdata ) {
1.57      noro     2715:         int dg,sugar0;
1.56      noro     2716:
                   2717:         update_hpdata(&current_hpdata,nh);
                   2718:         dg = comp_hn(final_hpdata.hn,current_hpdata.hn);
1.57      noro     2719:         if ( dg < 0 ) {
1.61      noro     2720:            int d_len;
                   2721:            for ( d_len = 0; d; d = d->next, d_len++);
                   2722:            fprintf(asir_out,"[%d] We found a gb\n",d_len);
1.57      noro     2723:         }
                   2724:         sugar0 = sugar;
                   2725:         while ( d && dg > sugar0 ) {
1.58      noro     2726:           d = nd_remove_same_sugar(d,sugar0);
1.57      noro     2727:           sugar0++;
1.49      noro     2728:         }
                   2729:       }
1.41      noro     2730:     } else {
                   2731:       Nnfz++;
                   2732:       if ( nd_gentrace && gensyz ) {
                   2733:         nd_tracelist = reverse_node(nd_tracelist);
                   2734:         MKLIST(list,nd_tracelist);
                   2735:         STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
                   2736:         MKNODE(t,list,nd_alltracelist);
                   2737:         nd_alltracelist = t; nd_tracelist = 0;
                   2738:       }
1.57      noro     2739:       if ( DP_Print ) { fprintf(asir_out,"."); fflush(asir_out); }
1.41      noro     2740:         FREENDP(l);
                   2741:     }
1.36      noro     2742:   }
                   2743:   conv_ilist(nd_demand,0,g,indp);
1.41      noro     2744:   if ( !checkonly && DP_Print ) {
1.60      noro     2745:     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     2746:     fprintf(asir_out,"Nremoved=%d\n",NcriB+NcriMF+Ncri2);
1.41      noro     2747:     fflush(asir_out);
                   2748:   }
                   2749:   if ( DP_Print ) {
                   2750:     print_eg("update",&eg_update); fprintf(asir_out,"\n");
                   2751:   }
1.36      noro     2752:   return g;
1.1       noro     2753: }
                   2754:
1.30      noro     2755: ND_pairs update_pairs_s(ND_pairs d,int t,NODE *syz);
1.47      noro     2756: int update_pairs_array_s(ND_pairs *d,int t,NODE *syz);
1.30      noro     2757: ND_pairs nd_newpairs_s(int t ,NODE *syz);
1.47      noro     2758: ND_pairs *nd_newpairs_array_s(int t ,NODE *syz);
1.24      noro     2759:
                   2760: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *nf);
                   2761: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
                   2762:
                   2763: void _copydl(int n,DL d1,DL d2);
                   2764: void _subfromdl(int n,DL d1,DL d2);
                   2765: extern int (*cmpdl)(int n,DL d1,DL d2);
1.53      noro     2766: int _dl_redble_ext(DL,DL,DL,int);
                   2767:
                   2768: int primitive_irred(ND p,SIG sig)
                   2769: {
                   2770:   static int wpd=0,dlen=0;
                   2771:   static DL dquo,squo;
                   2772:   static UINT *quo;
                   2773:   int i;
                   2774:
                   2775:   if ( dlen < nd_nvar ) {
                   2776:     NEWDL(dquo,nd_nvar);
                   2777:     NEWDL(squo,nd_nvar);
                   2778:     dlen = nd_nvar;
                   2779:   }
                   2780:   if ( wpd != nd_wpd ) {
                   2781:     wpd = nd_wpd;
                   2782:     quo = (UINT *)MALLOC(wpd*sizeof(UINT));
                   2783:   }
                   2784:   for ( i = 0; i < nd_psn; i++ ) {
                   2785:     if ( sig->pos == nd_psh[i]->sig->pos &&
                   2786:       _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),squo,nd_nvar) )
                   2787:       if ( ndl_reducible(HDL(p),DL(nd_psh[i])) ) {
                   2788:         if ( DP_Print ) fprintf(asir_out,"D");
                   2789:         ndl_sub(HDL(p),DL(nd_psh[i]),quo);
                   2790:         _ndltodl(quo,dquo);
                   2791:         if ( _eqdl(nd_nvar,squo,dquo) )
                   2792:           return 0;
                   2793:       }
                   2794:   }
                   2795:   return 1;
                   2796: }
1.24      noro     2797:
                   2798: NODE insert_sig(NODE l,SIG s)
                   2799: {
                   2800:   int pos;
                   2801:   DL sig;
                   2802:   struct oNODE root;
                   2803:   NODE p,prev,r;
                   2804:   SIG t;
                   2805:
                   2806:   pos = s->pos; sig = DL(s);
                   2807:   root.next = l; prev = &root;
                   2808:   for ( p = l; p; p = p->next ) {
                   2809:     t = (SIG)p->body;
                   2810:     if ( t->pos == pos ) {
                   2811:       if ( _dl_redble(DL(t),sig,nd_nvar) )
                   2812:         return root.next;
                   2813:       else if ( _dl_redble(sig,DL(t),nd_nvar) )
                   2814:         // remove p
                   2815:         prev->next = p->next;
1.45      noro     2816:       else
                   2817:         prev = p;
1.24      noro     2818:     } else
                   2819:       prev = p;
                   2820:   }
1.27      noro     2821:   NEWNODE(r); r->body = (pointer)s; r->next = 0;
                   2822:   for ( p = &root; p->next; p = p->next );
                   2823:   p->next = r;
                   2824: //  r->next = root.next;
                   2825: //  return r;
                   2826:   return root.next;
1.24      noro     2827: }
                   2828:
                   2829: ND_pairs remove_spair_s(ND_pairs d,SIG sig)
                   2830: {
                   2831:   struct oND_pairs root;
                   2832:   ND_pairs prev,p;
                   2833:   SIG spsig;
                   2834:
                   2835:   root.next = d;
                   2836:   prev = &root; p = d;
                   2837:   while ( p ) {
                   2838:     spsig = p->sig;
1.26      noro     2839:     if ( sig->pos == spsig->pos && _dl_redble(DL(sig),DL(spsig),nd_nvar) ) {
1.24      noro     2840:       // remove p
                   2841:       prev->next = p->next;
1.26      noro     2842:       Nsyz++;
                   2843:     } else
1.24      noro     2844:       prev = p;
                   2845:     p = p->next;
                   2846:   }
                   2847:   return (ND_pairs)root.next;
                   2848: }
                   2849:
1.28      noro     2850: int small_lcm(ND_pairs l)
                   2851: {
                   2852:   SIG sig;
                   2853:   int i;
1.29      noro     2854:   NODE t;
1.28      noro     2855:   static DL lcm,mul,quo;
1.44      noro     2856:   static int nvar = 0;
1.28      noro     2857:
1.38      noro     2858:   if ( nd_sba_largelcm ) return 0;
1.28      noro     2859:   if ( nvar < nd_nvar ) {
                   2860:     nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(quo,nvar); NEWDL(mul,nvar);
                   2861:   }
                   2862:   sig = l->sig;
                   2863:   _ndltodl(l->lcm,lcm);
1.29      noro     2864: #if 0
1.28      noro     2865:   for ( i = 0; i < nd_psn; i++ ) {
                   2866:     if ( sig->pos == nd_psh[i]->sig->pos &&
1.29      noro     2867:       _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
1.28      noro     2868:       _ndltodl(DL(nd_psh[i]),mul);
                   2869:       _addtodl(nd_nvar,quo,mul);
                   2870:       if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
                   2871:         break;
                   2872:     }
                   2873:   }
                   2874:   if ( i < nd_psn ) return 1;
                   2875:   else return 0;
1.29      noro     2876: #else
                   2877:   for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
                   2878:     i = (long)BDY(t);
                   2879:     if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
                   2880:       _ndltodl(DL(nd_psh[i]),mul);
                   2881:       _addtodl(nd_nvar,quo,mul);
                   2882:       if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
                   2883:         break;
                   2884:     }
                   2885:   }
                   2886:   if ( t ) return 1;
                   2887:   else return 0;
                   2888: #endif
1.28      noro     2889: }
                   2890:
1.41      noro     2891: ND_pairs find_smallest_lcm(ND_pairs l)
                   2892: {
                   2893:   SIG sig;
                   2894:   int i,minindex;
                   2895:   NODE t;
                   2896:   ND_pairs r;
                   2897:   struct oSIG sig1;
1.55      noro     2898:   static DL mul,quo,quo2,minlm;
1.44      noro     2899:   static int nvar = 0;
1.41      noro     2900:
                   2901:   if ( nvar < nd_nvar ) {
                   2902:     nvar = nd_nvar;
1.55      noro     2903:     NEWDL(quo,nvar); NEWDL(quo2,nvar); NEWDL(mul,nvar);
1.41      noro     2904:     NEWDL(minlm,nvar);
                   2905:   }
                   2906:   sig = l->sig;
                   2907:   // find mg s.t. m*s(g)=sig and m*lm(g) is minimal
                   2908:   _ndltodl(l->lcm,minlm); minindex = -1;
                   2909:   for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
                   2910:     i = (long)BDY(t);
                   2911:     if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
                   2912:       _ndltodl(DL(nd_psh[i]),mul);
                   2913:       _addtodl(nd_nvar,quo,mul);
                   2914:       if ( (*cmpdl)(nd_nvar,minlm,mul) > 0 ) {
                   2915:         minindex = i;
1.47      noro     2916:         break;
1.41      noro     2917:         _copydl(nd_nvar,mul,minlm);
                   2918:       }
                   2919:     }
                   2920:   }
                   2921:   // l->lcm is minimal; return l itself
                   2922:   if ( minindex < 0 ) return l;
1.47      noro     2923:   else return 0;
1.41      noro     2924:   for ( i = 0; i < nd_psn; i++ ) {
                   2925:     if ( i == minindex ) continue;
                   2926:     _ndltodl(DL(nd_psh[i]),mul);
                   2927:     if ( _dl_redble_ext(mul,minlm,quo,nd_nvar) ) {
                   2928:       _addtodl(nd_nvar,nd_ps[i]->sig->dl,quo);
                   2929:       sig1.pos = nd_ps[i]->sig->pos;
                   2930:       sig1.dl = quo;
1.55      noro     2931:       sig1.dl2 = quo2;
                   2932:       _adddl(nd_nvar,sig1.dl,nd_sba_hm[sig1.pos],sig1.dl2);
1.41      noro     2933:       if ( comp_sig(sig,&sig1) > 0 ) {
                   2934: //        printf("X");
                   2935:         NEWND_pairs(r);
                   2936:         r->sig = sig;
                   2937:         r->i1 = minindex;
                   2938:         r->i2 = i;
                   2939:         dltondl(nd_nvar,minlm,r->lcm);
                   2940:         r->next = 0;
                   2941:         return r;
                   2942:       }
                   2943:     }
                   2944:   }
                   2945:   // there is no suitable spair
                   2946:   return 0;
                   2947: }
                   2948:
1.28      noro     2949: ND_pairs remove_large_lcm(ND_pairs d)
                   2950: {
                   2951:   struct oND_pairs root;
                   2952:   ND_pairs prev,p;
                   2953:
                   2954:   root.next = d;
                   2955:   prev = &root; p = d;
                   2956:   while ( p ) {
1.41      noro     2957: #if 0
1.28      noro     2958:     if ( small_lcm(p) ) {
                   2959:       // remove p
                   2960:       prev->next = p->next;
                   2961:     } else
1.41      noro     2962: #else
                   2963:     if ( find_smallest_lcm(p) == 0 ) {
                   2964:       // remove p
                   2965:       prev->next = p->next;
                   2966:     } else
                   2967: #endif
1.28      noro     2968:       prev = p;
                   2969:     p = p->next;
                   2970:   }
                   2971:   return (ND_pairs)root.next;
                   2972: }
                   2973:
1.26      noro     2974: struct oEGT eg_create,eg_newpairs,eg_merge;
                   2975:
1.30      noro     2976: NODE conv_ilist_s(int demand,int trace,int **indp);
                   2977:
1.44      noro     2978: // S(fj*ei-fi*ej)
                   2979:
1.45      noro     2980: void _subdl(int,DL,DL,DL);
                   2981:
1.44      noro     2982: SIG trivial_sig(int i,int j)
                   2983: {
1.45      noro     2984:   static DL lcm;
1.44      noro     2985:   static struct oSIG sigi,sigj;
                   2986:   static int nvar = 0;
                   2987:   SIG sig;
                   2988:
                   2989:   if ( nvar != nd_nvar ) {
1.45      noro     2990:     nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(sigi.dl,nvar); NEWDL(sigj.dl,nvar);
1.55      noro     2991:     NEWDL(sigi.dl2,nvar); NEWDL(sigj.dl2,nvar);
1.45      noro     2992:   }
1.46      noro     2993:   if ( nd_sba_inputisgb != 0 ) {
1.45      noro     2994:     lcm_of_DL(nd_nvar,nd_sba_hm[i],nd_sba_hm[j],lcm);
                   2995:     sigi.pos = i; _subdl(nd_nvar,lcm,nd_sba_hm[i],sigi.dl);
1.55      noro     2996:     _copydl(nd_nvar,lcm,sigi.dl2);
1.45      noro     2997:     sigj.pos = j; _subdl(nd_nvar,lcm,nd_sba_hm[j],sigj.dl);
1.55      noro     2998:     _copydl(nd_nvar,lcm,sigj.dl2);
1.45      noro     2999:     if ( comp_sig(&sigi,&sigj) > 0 ) sig = dup_sig(&sigi);
                   3000:     else sig = dup_sig(&sigj);
                   3001:   } else {
                   3002:     sigi.pos = i; _copydl(nd_nvar,nd_sba_hm[j],sigi.dl);
1.55      noro     3003:     _adddl(nd_nvar,sigi.dl,nd_sba_hm[i],sigi.dl2);
1.45      noro     3004:     sigj.pos = j; _copydl(nd_nvar,nd_sba_hm[i],sigj.dl);
1.55      noro     3005:     _adddl(nd_nvar,sigj.dl,nd_sba_hm[j],sigj.dl2);
1.45      noro     3006:     if ( comp_sig(&sigi,&sigj) > 0 ) sig = dup_sig(&sigi);
                   3007:     else sig = dup_sig(&sigj);
1.44      noro     3008:   }
                   3009:   return sig;
                   3010: }
                   3011:
1.47      noro     3012: int nd_minsig(ND_pairs *d)
                   3013: {
                   3014:   int min,i,ret;
                   3015:
                   3016:   min = -1;
                   3017:   for ( i = 0; i < nd_nbase; i++ ) {
                   3018:     if ( d[i] != 0 ) {
                   3019:       if ( min < 0 ) min = i;
                   3020:       else {
                   3021:         ret = comp_sig(d[i]->sig,d[min]->sig);
                   3022:         if ( ret < 0 ) min = i;
                   3023:       }
                   3024:     }
                   3025:   }
                   3026:   return min;
                   3027: }
                   3028:
                   3029: int dlength(ND_pairs d)
                   3030: {
                   3031:   int i;
                   3032:   for ( i = 0; d; d = d->next, i++ );
                   3033:   return i;
                   3034: }
                   3035:
1.44      noro     3036: NODE nd_sba_buch(int m,int ishomo,int **indp,NODE *syzp)
1.24      noro     3037: {
1.39      noro     3038:   int i,j,nh,sugar,stat,pos;
1.30      noro     3039:   NODE r,t,g;
1.47      noro     3040:   ND_pairs *d;
1.41      noro     3041:   ND_pairs l,l1;
1.24      noro     3042:   ND h,nf,s,head,nf1;
                   3043:   NDV nfv;
                   3044:   Z q;
                   3045:   union oNDC dn,hc;
                   3046:   P cont;
                   3047:   LIST list;
                   3048:   SIG sig;
1.29      noro     3049:   NODE *syzlist;
1.47      noro     3050:   int ngen,ind;
1.41      noro     3051:   int Nnominimal,Nredundant;
1.28      noro     3052:   DL lcm,quo,mul;
1.49      noro     3053:   struct oHPDATA final_hpdata,current_hpdata;
1.50      noro     3054:   struct oEGT eg1,eg2,eg3,eg4,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
                   3055:   struct oEGT eg_minsig,eg_smallest,eg_removecont,eg_hpdata,eg_updatepairs,eg_sbabuch,eg_sp;
1.47      noro     3056:   int Nnfs=0,Nnfz=0,Nnfnz=0,dlen,nsyz;
1.24      noro     3057:
1.27      noro     3058: init_eg(&eg_remove);
1.29      noro     3059:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.47      noro     3060:   d = (ND_pairs *)MALLOC(nd_psn*sizeof(ND_pairs));
                   3061:   nd_nbase = nd_psn;
1.26      noro     3062:   Nsyz = 0;
1.24      noro     3063:   Nnd_add = 0;
1.41      noro     3064:   Nnominimal = 0;
1.26      noro     3065:   Nredundant = 0;
1.44      noro     3066:   ngen = nd_psn;
1.56      noro     3067:   if ( !do_weyl || nd_sba_inputisgb ) {
1.48      noro     3068:     for ( i = 0; i < nd_psn; i++ )
                   3069:       for ( j = i+1; j < nd_psn; j++ ) {
                   3070:         sig = trivial_sig(i,j);
                   3071:         syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
                   3072:       }
1.29      noro     3073:     }
1.47      noro     3074:   dlen = 0;
1.41      noro     3075:   for ( i = 0; i < nd_psn; i++ ) {
1.47      noro     3076:     dlen += update_pairs_array_s(d,i,syzlist);
1.41      noro     3077:   }
1.24      noro     3078:   sugar = 0;
1.39      noro     3079:   pos = 0;
1.49      noro     3080:   if ( nd_hpdata ) {
                   3081:     setup_hpdata(&final_hpdata,&current_hpdata);
                   3082:   }
1.26      noro     3083:   NEWDL(lcm,nd_nvar); NEWDL(quo,nd_nvar); NEWDL(mul,nd_nvar);
1.50      noro     3084: init_eg(&eg_sp);
1.26      noro     3085: init_eg(&eg_create);
                   3086: init_eg(&eg_merge);
1.47      noro     3087: init_eg(&eg_minsig);
                   3088: init_eg(&eg_smallest);
1.29      noro     3089: init_eg(&eg_large);
1.31      noro     3090: init_eg(&eg_nf);
                   3091: init_eg(&eg_nfzero);
1.50      noro     3092: init_eg(&eg_removecont);
                   3093: init_eg(&eg_updatepairs);
                   3094: init_eg(&eg_hpdata);
                   3095: init_eg(&eg_sbabuch);
                   3096: get_eg(&eg3);
1.47      noro     3097:   while ( 1 ) {
1.53      noro     3098:     if ( DP_Print && !nd_hpdata && dlen%1000 == 0 ) fprintf(asir_out,"(%d)",dlen);
1.47      noro     3099: again :
                   3100: get_eg(&eg1);
                   3101:     ind = nd_minsig(d);
                   3102: get_eg(&eg2); add_eg(&eg_minsig,&eg1,&eg2);
                   3103:     if ( ind < 0 ) break;
                   3104:     l = d[ind];
                   3105: //    printf("(%d,%d)",l->i1,l->i2); print_sig(l->sig); printf("\n");
                   3106: get_eg(&eg1);
1.41      noro     3107:     l1 = find_smallest_lcm(l);
1.47      noro     3108: get_eg(&eg2); add_eg(&eg_smallest,&eg1,&eg2);
1.41      noro     3109:     if ( l1 == 0 ) {
1.47      noro     3110:       d[ind] = d[ind]->next; dlen--;
1.53      noro     3111: //      if ( DP_Print && !nd_hpdata ) fprintf(asir_out,"M");
1.41      noro     3112:       Nnominimal++;
                   3113:       continue;
                   3114:     }
                   3115:     if ( SG(l1) != sugar ) {
                   3116:       sugar = SG(l1);
                   3117:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   3118:     }
                   3119:     sig = l1->sig;
                   3120:     if ( DP_Print && nd_sba_pot ) {
                   3121:       if ( sig->pos != pos ) {
                   3122:         fprintf(asir_out,"[%d]",sig->pos);
                   3123:         pos = sig->pos;
                   3124:       }
                   3125:     }
1.50      noro     3126: get_eg(&eg1);
1.41      noro     3127:     stat = nd_sp(m,0,l1,&h);
1.50      noro     3128: get_eg(&eg2); add_eg(&eg_sp,&eg1,&eg2);
1.24      noro     3129:     if ( !stat ) {
1.47      noro     3130:       nd_reconstruct_s(0,d);
1.24      noro     3131:       goto again;
                   3132:     }
1.31      noro     3133: get_eg(&eg1);
1.25      noro     3134: #if USE_GEOBUCKET
1.55      noro     3135:     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     3136: #else
1.39      noro     3137:     stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.24      noro     3138: #endif
1.31      noro     3139: get_eg(&eg2);
1.24      noro     3140:     if ( !stat ) {
1.47      noro     3141:       nd_reconstruct_s(0,d);
1.24      noro     3142:       goto again;
                   3143:     } else if ( stat == -1 ) {
1.47      noro     3144:       d[ind] = d[ind]->next; dlen--;
1.41      noro     3145:       Nnfs++;
1.24      noro     3146:       if ( DP_Print ) { printf("S"); fflush(stdout); }
                   3147:     } else if ( nf ) {
1.47      noro     3148:       d[ind] = d[ind]->next; dlen--;
1.41      noro     3149:       Nnfnz++;
                   3150:       if ( DP_Print ) {
                   3151:         if ( nd_sba_redundant_check ) {
                   3152:           if ( ndl_find_reducer_nonsig(HDL(nf)) >= 0 ) {
                   3153:             Nredundant++;
                   3154:             printf("R");
                   3155:           } else
                   3156:             printf("+");
                   3157:         } else
                   3158:           printf("+");
                   3159:         fflush(stdout);
                   3160:       }
1.31      noro     3161:       add_eg(&eg_nf,&eg1,&eg2);
1.24      noro     3162:       hc = HCU(nf);
1.55      noro     3163:        get_eg(&eg1);
                   3164:        nd_removecont(m,nf);
                   3165:        get_eg(&eg2); add_eg(&eg_removecont,&eg1,&eg2);
                   3166:        nfv = ndtondv(m,nf); nd_free(nf);
                   3167:        nh = ndv_newps(m,nfv,0);
                   3168:
                   3169:        get_eg(&eg1);
                   3170:        dlen += update_pairs_array_s(d,nh,syzlist);
                   3171:        get_eg(&eg2); add_eg(&eg_updatepairs,&eg1,&eg2);
                   3172:        nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
                   3173:        if ( nd_hpdata ) {
1.63    ! noro     3174:          int dg,sugar0;
        !          3175:
1.55      noro     3176:          get_eg(&eg1);
1.56      noro     3177:          update_hpdata(&current_hpdata,nh);
1.55      noro     3178:          get_eg(&eg2); add_eg(&eg_hpdata,&eg1,&eg2);
1.63    ! noro     3179:          dg = comp_hn(final_hpdata.hn,current_hpdata.hn);
        !          3180: //         if ( !compp(CO,final_hpdata.hn,current_hpdata.hn) )
        !          3181:          if ( dg < 0 ) {
1.55      noro     3182:            if ( DP_Print ) { printf("\nWe found a gb.\n"); }
                   3183:              break;
1.63    ! noro     3184:          } else if ( nd_sba_heu == 1 ) {
        !          3185:            for ( i = 0; i < ngen; i++ ) {
        !          3186:              sugar0 = sugar;
        !          3187:              while ( d[i] && dg > sugar0 ) {
        !          3188:                d[i] = nd_remove_same_sugar(d[i],sugar0);
        !          3189:                sugar0++;
        !          3190:              }
        !          3191:           }
        !          3192:         }
1.49      noro     3193:       }
1.24      noro     3194:    } else {
1.47      noro     3195:       d[ind] = d[ind]->next; dlen--;
1.41      noro     3196:       Nnfz++;
1.31      noro     3197:       add_eg(&eg_nfzero,&eg1,&eg2);
1.24      noro     3198:      // syzygy
1.27      noro     3199: get_eg(&eg1);
1.47      noro     3200:      nsyz = Nsyz;
                   3201:      d[sig->pos] = remove_spair_s(d[sig->pos],sig);
                   3202:      dlen -= Nsyz-nsyz;
1.27      noro     3203: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29      noro     3204:      syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.24      noro     3205:      if ( DP_Print ) { printf("."); fflush(stdout); }
                   3206:    }
                   3207:  }
1.50      noro     3208:  get_eg(&eg4); add_eg(&eg_sbabuch,&eg3,&eg4);
1.30      noro     3209:  g = conv_ilist_s(nd_demand,0,indp);
1.26      noro     3210:  if ( DP_Print ) {
1.47      noro     3211:    printf("\ndlen=%d,nd_sba done. nd_add=%d,Nsyz=%d,Nsamesig=%d,Nnominimal=%d\n",dlen,Nnd_add,Nsyz,Nsamesig,Nnominimal);
1.62      noro     3212:    printf("Nbase=%d,Nnfnz=%d,Nnfz=%d,Nnfsingular=%d\n",nd_psn,Nnfnz,Nnfz,Nnfs);
1.50      noro     3213:    fflush(stdout);
1.41      noro     3214:    if ( nd_sba_redundant_check )
                   3215:    printf("Nredundant=%d\n",Nredundant);
1.50      noro     3216:    fflush(stdout);
                   3217:    print_eg("sp",&eg_sp);
1.26      noro     3218:    print_eg("create",&eg_create);
                   3219:    print_eg("merge",&eg_merge);
1.47      noro     3220:    print_eg("minsig",&eg_minsig);
                   3221:    print_eg("smallest",&eg_smallest);
                   3222:    print_eg("remove",&eg_remove);
1.50      noro     3223:    printf("\n");
1.47      noro     3224:    print_eg("nf",&eg_nf);
                   3225:    print_eg("nfzero",&eg_nfzero);
1.50      noro     3226:    print_eg("removecont",&eg_removecont);
                   3227:    print_eg("updatepairs",&eg_updatepairs);
                   3228:    print_eg("hpdata",&eg_hpdata);
                   3229:    print_eg("total",&eg_sbabuch);
1.47      noro     3230:    printf("\n");
                   3231:  }
                   3232:  if ( nd_sba_syz ) {
1.27      noro     3233:    print_eg("remove",&eg_remove);
1.31      noro     3234:    print_eg("nf",&eg_nf);
                   3235:    print_eg("nfzero",&eg_nfzero);
1.27      noro     3236:    printf("\n");
1.26      noro     3237:  }
1.44      noro     3238:  if ( nd_sba_syz ) {
                   3239:    NODE hsyz,tsyz,prev;
                   3240:
                   3241:    hsyz = 0;
                   3242:    for ( i = 0; i < ngen; i++ ) {
                   3243:      tsyz = syzlist[i];
                   3244:      for ( prev = 0; tsyz != 0; prev = tsyz, tsyz = NEXT(tsyz))
                   3245:        BDY(tsyz) = (pointer)sigtodpm((SIG)BDY(tsyz));
                   3246:      if ( prev != 0 ) {
                   3247:        prev->next = hsyz; hsyz = syzlist[i];
                   3248:      }
                   3249:    }
                   3250:    *syzp = hsyz;
                   3251:  } else *syzp = 0;
1.24      noro     3252:  return g;
                   3253: }
                   3254:
1.1       noro     3255: /* splist = [[i1,i2],...] */
                   3256:
                   3257: int check_splist(int m,NODE splist)
                   3258: {
                   3259:   NODE t,p;
                   3260:   ND_pairs d,r,l;
                   3261:   int stat;
                   3262:   ND h,nf;
                   3263:
                   3264:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   3265:     p = BDY((LIST)BDY(t));
1.6       noro     3266:     NEXTND_pairs(d,r);
                   3267:     r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
                   3268:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     3269:     SG(r) = TD(LCM(r)); /* XXX */
                   3270:   }
                   3271:   if ( d ) NEXT(r) = 0;
                   3272:
1.6       noro     3273:   while ( d ) {
1.1       noro     3274: again:
1.6       noro     3275:     l = nd_minp(d,&d);
                   3276:     stat = nd_sp(m,0,l,&h);
                   3277:     if ( !stat ) {
                   3278:       NEXT(l) = d; d = l;
                   3279:       d = nd_reconstruct(0,d);
                   3280:       goto again;
                   3281:     }
1.39      noro     3282:     stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.6       noro     3283:     if ( !stat ) {
                   3284:       NEXT(l) = d; d = l;
                   3285:       d = nd_reconstruct(0,d);
                   3286:       goto again;
                   3287:     } else if ( nf ) return 0;
1.1       noro     3288:     if ( DP_Print) { printf("."); fflush(stdout); }
1.6       noro     3289:   }
1.1       noro     3290:   if ( DP_Print) { printf("done.\n"); fflush(stdout); }
                   3291:   return 1;
                   3292: }
                   3293:
                   3294: int check_splist_f4(int m,NODE splist)
                   3295: {
                   3296:   UINT *s0vect;
1.6       noro     3297:   PGeoBucket bucket;
1.1       noro     3298:   NODE p,rp0,t;
                   3299:   ND_pairs d,r,l,ll;
                   3300:   int col,stat;
                   3301:
                   3302:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   3303:     p = BDY((LIST)BDY(t));
1.6       noro     3304:     NEXTND_pairs(d,r);
                   3305:     r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
                   3306:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     3307:     SG(r) = TD(LCM(r)); /* XXX */
                   3308:   }
                   3309:   if ( d ) NEXT(r) = 0;
                   3310:
1.6       noro     3311:   while ( d ) {
                   3312:     l = nd_minsugarp(d,&d);
                   3313:     bucket = create_pbucket();
                   3314:     stat = nd_sp_f4(m,0,l,bucket);
                   3315:     if ( !stat ) {
                   3316:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   3317:       NEXT(ll) = d; d = l;
                   3318:       d = nd_reconstruct(0,d);
                   3319:       continue;
                   3320:     }
                   3321:     if ( bucket->m < 0 ) continue;
                   3322:     col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   3323:     if ( !col ) {
                   3324:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   3325:       NEXT(ll) = d; d = l;
                   3326:       d = nd_reconstruct(0,d);
                   3327:       continue;
1.1       noro     3328:     }
1.6       noro     3329:     if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
                   3330:   }
                   3331:   return 1;
1.1       noro     3332: }
                   3333:
                   3334: int do_diagonalize_trace(int sugar,int m)
                   3335: {
1.6       noro     3336:   int i,nh,stat;
                   3337:   NODE r,g,t;
                   3338:   ND h,nf,nfq,s,head;
                   3339:   NDV nfv,nfqv;
                   3340:   Q q,den,num;
                   3341:   union oNDC hc;
                   3342:   NODE node;
                   3343:   LIST l;
                   3344:   Z iq;
                   3345:   P cont,cont1;
1.1       noro     3346:
1.6       noro     3347:   for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   3348:     if ( nd_gentrace ) {
                   3349:         /* Trace = [1,index,1,1] */
                   3350:         STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   3351:         MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   3352:     }
                   3353:     /* for nd_ps */
                   3354:     s = ndvtond(m,nd_ps[i]);
                   3355:     s = nd_separate_head(s,&head);
                   3356:     stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
                   3357:     if ( !stat ) return 0;
                   3358:     nf = nd_add(m,head,nf);
                   3359:     ndv_free(nd_ps[i]);
                   3360:     nd_ps[i] = ndtondv(m,nf);
                   3361:     nd_free(nf);
                   3362:
                   3363:     /* for nd_ps_trace */
                   3364:     if ( nd_demand )
                   3365:         nfv = ndv_load(i);
                   3366:     else
                   3367:         nfv = nd_ps_trace[i];
                   3368:     s = ndvtond(0,nfv);
                   3369:     s = nd_separate_head(s,&head);
                   3370:     stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
                   3371:     if ( !stat ) return 0;
                   3372:     ndv_free(nfv);
                   3373:     hc = HCU(nf); nd_removecont(0,nf);
                   3374:     /* exact division */
1.1       noro     3375:     cont = ndc_div(0,hc,HCU(nf));
1.6       noro     3376:     if ( nd_gentrace ) finalize_tracelist(i,cont);
                   3377:     nfv = ndtondv(0,nf);
                   3378:     nd_free(nf);
                   3379:     nd_bound[i] = ndv_compute_bound(nfv);
                   3380:     register_hcf(nfv);
                   3381:     if ( nd_demand ) {
                   3382:     ndv_save(nfv,i);
                   3383:     ndv_free(nfv);
                   3384:     } else
                   3385:     nd_ps_trace[i] = nfv;
                   3386:   }
                   3387:   return 1;
1.1       noro     3388: }
                   3389:
                   3390: static struct oEGT eg_invdalg;
                   3391: struct oEGT eg_le;
                   3392:
                   3393: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
                   3394: {
                   3395:     NODE tn;
                   3396:     P p1;
                   3397:
                   3398:     for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
                   3399:         substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
                   3400:     }
                   3401:     *r = p;
                   3402: }
                   3403:
                   3404: NODE nd_gb_trace(int m,int ishomo,int **indp)
                   3405: {
1.6       noro     3406:   int i,nh,sugar,stat;
                   3407:   NODE r,g,t;
                   3408:   ND_pairs d;
                   3409:   ND_pairs l;
                   3410:   ND h,nf,nfq,s,head;
                   3411:   NDV nfv,nfqv;
                   3412:   Z q,den,num;
                   3413:   P hc;
                   3414:   union oNDC dn,hnfq;
                   3415:   struct oEGT eg_monic,egm0,egm1;
                   3416:   int diag_count = 0;
                   3417:   P cont;
                   3418:   LIST list;
1.49      noro     3419:   struct oHPDATA current_hpdata,final_hpdata;
                   3420:   int final_hpvalue;
1.6       noro     3421:
                   3422:   init_eg(&eg_monic);
                   3423:   init_eg(&eg_invdalg);
                   3424:   init_eg(&eg_le);
                   3425:   g = 0; d = 0;
                   3426:   for ( i = 0; i < nd_psn; i++ ) {
                   3427:     d = update_pairs(d,g,i,0);
                   3428:     g = update_base(g,i);
                   3429:   }
                   3430:   sugar = 0;
1.49      noro     3431:   if ( nd_hpdata ) {
1.52      noro     3432:     if ( DP_Print ) fprintf(asir_out,"Hilbert driven algorithm.\n");
1.49      noro     3433:     setup_hpdata(&final_hpdata,&current_hpdata);
                   3434:   }
                   3435:
1.6       noro     3436:   while ( d ) {
1.1       noro     3437: again:
1.6       noro     3438:     l = nd_minp(d,&d);
                   3439:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   3440:     if ( SG(l) != sugar ) {
1.1       noro     3441: #if 1
1.6       noro     3442:       if ( ishomo ) {
                   3443:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3444:         stat = do_diagonalize_trace(sugar,m);
                   3445:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3446:         diag_count = 0;
1.1       noro     3447:         if ( !stat ) {
1.6       noro     3448:           NEXT(l) = d; d = l;
                   3449:           d = nd_reconstruct(1,d);
                   3450:           goto again;
1.1       noro     3451:         }
1.6       noro     3452:       }
                   3453: #endif
                   3454:       sugar = SG(l);
                   3455:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   3456:     }
                   3457:     stat = nd_sp(m,0,l,&h);
                   3458:     if ( !stat ) {
                   3459:       NEXT(l) = d; d = l;
                   3460:       d = nd_reconstruct(1,d);
                   3461:       goto again;
                   3462:     }
1.1       noro     3463: #if USE_GEOBUCKET
1.39      noro     3464:     stat = nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     3465: #else
1.39      noro     3466:     stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     3467: #endif
1.6       noro     3468:     if ( !stat ) {
                   3469:       NEXT(l) = d; d = l;
                   3470:       d = nd_reconstruct(1,d);
                   3471:       goto again;
                   3472:     } else if ( nf ) {
                   3473:       if ( nd_demand ) {
                   3474:         nfqv = ndv_load(nd_psn);
                   3475:         nfq = ndvtond(0,nfqv);
                   3476:       } else
                   3477:         nfq = 0;
                   3478:       if ( !nfq ) {
1.39      noro     3479:         if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!nd_top&&!Top,&nfq) ) {
1.6       noro     3480:           NEXT(l) = d; d = l;
                   3481:           d = nd_reconstruct(1,d);
                   3482:           goto again;
                   3483:         }
                   3484:       }
                   3485:       if ( nfq ) {
                   3486:         /* m|HC(nfq) => failure */
                   3487:         if ( nd_vc ) {
                   3488:           nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
                   3489:         } else
                   3490:           q = HCZ(nfq);
                   3491:         if ( !remqi((Q)q,m) ) return 0;
                   3492:
                   3493:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   3494:         hnfq = HCU(nfq);
                   3495:         if ( nd_nalg ) {
                   3496:           /* m|DN(HC(nf)^(-1)) => failure */
                   3497:           get_eg(&egm0);
                   3498:           if ( !nd_monic(m,&nfq) ) return 0;
                   3499:           get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
                   3500:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   3501:           nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
                   3502:         } else {
                   3503:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   3504:           nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
                   3505:         }
                   3506:         if ( nd_gentrace ) {
                   3507:           /* exact division */
                   3508:           cont = ndc_div(0,hnfq,HCU(nfqv));
                   3509:           if ( !UNIQ(cont) ) {
                   3510:             t = mknode(4,NULLP,NULLP,NULLP,cont);
                   3511:             MKLIST(list,t); MKNODE(t,list,nd_tracelist);
                   3512:             nd_tracelist = t;
                   3513:           }
                   3514:         }
1.24      noro     3515:         nh = ndv_newps(0,nfv,nfqv);
1.6       noro     3516:         if ( ishomo && ++diag_count == diag_period ) {
                   3517:           diag_count = 0;
                   3518:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3519:           stat = do_diagonalize_trace(sugar,m);
                   3520:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3521:           if ( !stat ) {
1.1       noro     3522:             NEXT(l) = d; d = l;
                   3523:             d = nd_reconstruct(1,d);
                   3524:             goto again;
1.6       noro     3525:           }
1.1       noro     3526:         }
1.6       noro     3527:         d = update_pairs(d,g,nh,0);
                   3528:         g = update_base(g,nh);
1.49      noro     3529:         if ( nd_hpdata ) {
1.57      noro     3530:           int dg,sugar0;
1.56      noro     3531:
                   3532:           update_hpdata(&current_hpdata,nh);
                   3533:           dg = comp_hn(final_hpdata.hn,current_hpdata.hn);
1.57      noro     3534:           if ( dg < 0 ) {
1.61      noro     3535:              int d_len;
                   3536:              for ( d_len = 0; d; d = d->next, d_len++);
                   3537:              fprintf(asir_out,"[%d] We found a gb\n",d_len);
1.57      noro     3538:           }
                   3539:           sugar0 = sugar;
                   3540:           while ( d && dg > sugar0 ) {
1.58      noro     3541:             d = nd_remove_same_sugar(d,sugar0);
1.57      noro     3542:             sugar0++;
1.49      noro     3543:           }
                   3544:         }
1.6       noro     3545:       } else {
1.57      noro     3546:         if ( DP_Print ) { fprintf(asir_out,"*"); fflush(asir_out); }
1.6       noro     3547:       }
                   3548:     } else {
1.57      noro     3549:       if ( DP_Print ) { fprintf(asir_out,"."); fflush(asir_out); }
1.1       noro     3550:     }
1.6       noro     3551:     FREENDP(l);
                   3552:   }
                   3553:   if ( nd_nalg ) {
                   3554:     if ( DP_Print ) {
                   3555:       print_eg("monic",&eg_monic);
                   3556:       print_eg("invdalg",&eg_invdalg);
                   3557:       print_eg("le",&eg_le);
1.1       noro     3558:     }
1.6       noro     3559:   }
1.1       noro     3560:   conv_ilist(nd_demand,1,g,indp);
1.60      noro     3561:   if ( DP_Print ) { fprintf(asir_out,"\nnd_gb_trace done. Nbase=%d\n",nd_psn); fflush(asir_out); }
1.6       noro     3562:   return g;
1.1       noro     3563: }
                   3564:
                   3565: int ndv_compare(NDV *p1,NDV *p2)
                   3566: {
                   3567:     return DL_COMPARE(HDL(*p1),HDL(*p2));
                   3568: }
                   3569:
                   3570: int ndv_compare_rev(NDV *p1,NDV *p2)
                   3571: {
                   3572:     return -DL_COMPARE(HDL(*p1),HDL(*p2));
                   3573: }
                   3574:
                   3575: int ndvi_compare(NDVI p1,NDVI p2)
                   3576: {
                   3577:     return DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3578: }
                   3579:
                   3580: int ndvi_compare_rev(NDVI p1,NDVI p2)
                   3581: {
                   3582:     return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3583: }
                   3584:
                   3585: NODE ndv_reduceall(int m,NODE f)
                   3586: {
                   3587:   int i,j,n,stat;
                   3588:   ND nf,g,head;
                   3589:   NODE t,a0,a;
                   3590:   union oNDC dn;
                   3591:   Q q,num,den;
                   3592:   NODE node;
                   3593:   LIST l;
                   3594:   Z iq,jq;
                   3595:   int *perm;
                   3596:   union oNDC hc;
                   3597:   P cont,cont1;
                   3598:
                   3599:   if ( nd_nora ) return f;
                   3600:   n = length(f);
1.24      noro     3601:   ndv_setup(m,0,f,0,1,0);
1.1       noro     3602:   perm = (int *)MALLOC(n*sizeof(int));
                   3603:   if ( nd_gentrace ) {
                   3604:     for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6       noro     3605:       perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1       noro     3606:   }
                   3607:   for ( i = 0; i < n; ) {
                   3608:     if ( nd_gentrace ) {
                   3609:       /* Trace = [1,index,1,1] */
1.6       noro     3610:       STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1       noro     3611:       MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   3612:     }
                   3613:     g = ndvtond(m,nd_ps[i]);
                   3614:     g = nd_separate_head(g,&head);
1.6       noro     3615:     stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1       noro     3616:     if ( !stat )
                   3617:       nd_reconstruct(0,0);
                   3618:     else {
                   3619:       if ( DP_Print ) { printf("."); fflush(stdout); }
                   3620:       ndv_free(nd_ps[i]);
                   3621:       hc = HCU(nf); nd_removecont(m,nf);
                   3622:       if ( nd_gentrace ) {
                   3623:         for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6       noro     3624:           jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
                   3625:           STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1       noro     3626:         }
1.6       noro     3627:         /* exact division */
1.1       noro     3628:         cont = ndc_div(m,hc,HCU(nf));
                   3629:         finalize_tracelist(perm[i],cont);
                   3630:       }
                   3631:       nd_ps[i] = ndtondv(m,nf); nd_free(nf);
                   3632:       nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   3633:       i++;
                   3634:     }
                   3635:   }
                   3636:   if ( DP_Print ) { printf("\n"); }
                   3637:   for ( a0 = 0, i = 0; i < n; i++ ) {
                   3638:     NEXTNODE(a0,a);
                   3639:     if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
                   3640:     else {
                   3641:       for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
                   3642:       BDY(a) = (pointer)nd_ps[j];
                   3643:     }
                   3644:   }
                   3645:   NEXT(a) = 0;
                   3646:   return a0;
                   3647: }
                   3648:
1.41      noro     3649: int ndplength(ND_pairs d)
                   3650: {
                   3651:   int i;
                   3652:   for ( i = 0; d; i++ ) d = NEXT(d);
                   3653:   return i;
                   3654: }
                   3655:
1.1       noro     3656: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
                   3657: {
                   3658:   ND_pairs d1,nd,cur,head,prev,remove;
1.41      noro     3659:   int len0;
1.1       noro     3660:
                   3661:   if ( !g ) return d;
                   3662:   /* for testing */
                   3663:   if ( gensyz && nd_gensyz == 2 ) {
                   3664:     d1 = nd_newpairs(g,t);
                   3665:     if ( !d )
                   3666:       return d1;
                   3667:     else {
                   3668:       nd = d;
                   3669:       while ( NEXT(nd) ) nd = NEXT(nd);
                   3670:       NEXT(nd) = d1;
                   3671:       return d;
                   3672:     }
                   3673:   }
                   3674:   d = crit_B(d,t);
                   3675:   d1 = nd_newpairs(g,t);
1.41      noro     3676:   len0 = ndplength(d1);
1.1       noro     3677:   d1 = crit_M(d1);
                   3678:   d1 = crit_F(d1);
1.41      noro     3679:   NcriMF += len0-ndplength(d1);
1.1       noro     3680:   if ( gensyz || do_weyl )
                   3681:     head = d1;
                   3682:   else {
                   3683:     prev = 0; cur = head = d1;
                   3684:     while ( cur ) {
                   3685:       if ( crit_2( cur->i1,cur->i2 ) ) {
                   3686:         remove = cur;
                   3687:         if ( !prev ) head = cur = NEXT(cur);
                   3688:         else cur = NEXT(prev) = NEXT(cur);
1.41      noro     3689:         FREENDP(remove); Ncri2++;
1.1       noro     3690:       } else {
                   3691:         prev = cur; cur = NEXT(cur);
                   3692:       }
                   3693:     }
                   3694:   }
                   3695:   if ( !d )
                   3696:     return head;
                   3697:   else {
                   3698:     nd = d;
                   3699:     while ( NEXT(nd) ) nd = NEXT(nd);
                   3700:     NEXT(nd) = head;
                   3701:     return d;
                   3702:   }
                   3703: }
                   3704:
1.24      noro     3705: ND_pairs merge_pairs_s(ND_pairs d,ND_pairs d1);
                   3706:
1.30      noro     3707: ND_pairs update_pairs_s( ND_pairs d, int t,NODE *syz)
1.24      noro     3708: {
                   3709:   ND_pairs d1;
1.26      noro     3710:   struct oEGT eg1,eg2,eg3;
1.24      noro     3711:
1.30      noro     3712:   if ( !t ) return d;
1.26      noro     3713: get_eg(&eg1);
1.30      noro     3714:   d1 = nd_newpairs_s(t,syz);
1.26      noro     3715: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
1.24      noro     3716:   d = merge_pairs_s(d,d1);
1.26      noro     3717: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
1.24      noro     3718:   return d;
                   3719: }
1.1       noro     3720:
1.47      noro     3721: int update_pairs_array_s( ND_pairs *d, int t,NODE *syz)
                   3722: {
                   3723:   ND_pairs *d1;
                   3724:   struct oEGT eg1,eg2,eg3;
                   3725:   int i;
                   3726:
                   3727:   if ( !t ) return 0;
                   3728: get_eg(&eg1);
                   3729:   Nnewpair = 0;
                   3730:   d1 = nd_newpairs_array_s(t,syz);
                   3731: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
                   3732:   for ( i = 0; i < nd_nbase; i++ )
                   3733:     d[i] = merge_pairs_s(d[i],d1[i]);
                   3734: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
                   3735:   return Nnewpair;
                   3736: }
                   3737:
1.1       noro     3738: ND_pairs nd_newpairs( NODE g, int t )
                   3739: {
                   3740:   NODE h;
                   3741:   UINT *dl;
                   3742:   int ts,s,i,t0,min,max;
                   3743:   ND_pairs r,r0;
                   3744:
                   3745:   dl = DL(nd_psh[t]);
                   3746:   ts = SG(nd_psh[t]) - TD(dl);
1.17      noro     3747:   if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1       noro     3748:   for ( r0 = 0, h = g; h; h = NEXT(h) ) {
                   3749:     if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
                   3750:       continue;
                   3751:     if ( nd_gbblock ) {
                   3752:       t0 = (long)BDY(h);
                   3753:       for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
                   3754:         min = nd_gbblock[i]; max = nd_gbblock[i+1];
                   3755:         if ( t0 >= min && t0 <= max && t >= min && t <= max )
                   3756:           break;
                   3757:       }
                   3758:       if ( nd_gbblock[i] >= 0 )
                   3759:         continue;
                   3760:     }
1.41      noro     3761:     NEXTND_pairs(r0,r); Npairs++;
1.1       noro     3762:     r->i1 = (long)BDY(h);
                   3763:     r->i2 = t;
                   3764:     ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
                   3765:     s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   3766:     SG(r) = MAX(s,ts) + TD(LCM(r));
                   3767:     /* experimental */
                   3768:     if ( nd_sugarweight )
                   3769:       r->sugar2 = ndl_weight2(r->lcm);
                   3770:   }
                   3771:   if ( r0 ) NEXT(r) = 0;
                   3772:   return r0;
                   3773: }
                   3774:
1.46      noro     3775: int sig_cmpdl_op(int n,DL d1,DL d2)
                   3776: {
                   3777:   int e1,e2,i,j,l;
                   3778:   int *t1,*t2;
                   3779:   int len,head;
                   3780:   struct order_pair *pair;
                   3781:
                   3782:   len = nd_sba_modord->block_length;
                   3783:   pair = nd_sba_modord->order_pair;
                   3784:
                   3785:   head = 0;
                   3786:   for ( i = 0, t1 = d1->d, t2 = d2->d; i < len; i++ ) {
                   3787:     l = pair[i].length;
                   3788:     switch ( pair[i].order ) {
                   3789:       case 0:
                   3790:         for ( j = 0, e1 = e2 = 0; j < l; j++ ) {
                   3791:           e1 += t1[j];
                   3792:           e2 += t2[j];
                   3793:         }
                   3794:         if ( e1 > e2 )
                   3795:           return 1;
                   3796:         else if ( e1 < e2 )
                   3797:           return -1;
                   3798:         else {
                   3799:           for ( j = l - 1; j >= 0 && t1[j] == t2[j]; j-- );
                   3800:           if ( j >= 0 )
                   3801:             return t1[j] < t2[j] ? 1 : -1;
                   3802:         }
                   3803:         break;
                   3804:       case 1:
                   3805:         for ( j = 0, e1 = e2 = 0; j < l; j++ ) {
                   3806:           e1 += t1[j];
                   3807:           e2 += t2[j];
                   3808:         }
                   3809:         if ( e1 > e2 )
                   3810:           return 1;
                   3811:         else if ( e1 < e2 )
                   3812:           return -1;
                   3813:         else {
                   3814:           for ( j = 0; j < l && t1[j] == t2[j]; j++ );
                   3815:           if ( j < l )
                   3816:             return t1[j] > t2[j] ? 1 : -1;
                   3817:         }
                   3818:         break;
                   3819:       case 2:
                   3820:         for ( j = 0; j < l && t1[j] == t2[j]; j++ );
                   3821:         if ( j < l )
                   3822:           return t1[j] > t2[j] ? 1 : -1;
                   3823:         break;
                   3824:       default:
                   3825:         error("sig_cmpdl_op : invalid order"); break;
                   3826:     }
                   3827:     t1 += l; t2 += l; head += l;
                   3828:   }
                   3829:   return 0;
                   3830: }
                   3831:
                   3832: int sig_cmpdl_mat(int n,DL d1,DL d2)
                   3833: {
                   3834:   int *v,*t1,*t2;
                   3835:   int s,i,j,len;
                   3836:   int **matrix;
                   3837:   static int *w;
                   3838:   static int nvar = 0;
                   3839:
                   3840:   if ( nvar != n ) {
                   3841:     nvar = n; w = (int *)MALLOC(n*sizeof(int));
                   3842:   }
                   3843:   for ( i = 0, t1 = d1->d, t2 = d2->d; i < n; i++ )
                   3844:     w[i] = t1[i]-t2[i];
                   3845:   len = nd_sba_modord->row;
                   3846:   matrix = nd_sba_modord->matrix;
                   3847:   for ( j = 0; j < len; j++ ) {
                   3848:     v = matrix[j];
                   3849:     for ( i = 0, s = 0; i < n; i++ )
                   3850:       s += v[i]*w[i];
                   3851:     if ( s > 0 )
                   3852:       return 1;
                   3853:     else if ( s < 0 )
                   3854:       return -1;
                   3855:   }
                   3856:   return 0;
                   3857: }
                   3858:
                   3859: struct comp_sig_spec *create_comp_sig_spec(VL current_vl,VL old_vl,Obj ord,Obj weight)
                   3860: {
                   3861:   struct comp_sig_spec *spec;
                   3862:   VL ovl,vl;
                   3863:   V ov;
                   3864:   int i,j,n,nvar,s;
                   3865:   NODE node,t,tn;
                   3866:   struct order_pair *l;
                   3867:   MAT m;
                   3868:   Obj **b;
                   3869:   int **w;
                   3870:   int *a;
                   3871:
                   3872:   spec = (struct comp_sig_spec *)MALLOC(sizeof(struct comp_sig_spec));
                   3873:   for ( i = 0, vl = current_vl; vl; vl = NEXT(vl), i++ );
                   3874:   spec->n = nvar = i;
                   3875:   if ( old_vl != 0 ) {
                   3876:     spec->oldv = (int *)MALLOC(nvar*sizeof(int));
                   3877:     for ( i = 0, ovl = old_vl; i < nvar; ovl = NEXT(ovl), i++ ) {
                   3878:       ov = ovl->v;
                   3879:       for ( j = 0, vl = current_vl; vl; vl = NEXT(vl), j++ )
                   3880:         if ( ov == vl->v ) break;
                   3881:       spec->oldv[i] = j;
                   3882:     }
                   3883:   } else
                   3884:     spec->oldv = 0;
                   3885:   if ( !ord || NUM(ord) ) {
                   3886:     switch ( ZTOS((Z)ord) ) {
                   3887:       case 0:
                   3888:         spec->cmpdl = cmpdl_revgradlex; break;
                   3889:       case 1:
                   3890:         spec->cmpdl = cmpdl_gradlex; break;
                   3891:       case 2:
                   3892:         spec->cmpdl = cmpdl_lex; break;
                   3893:       default:
                   3894:         error("create_comp_sig_spec : invalid spec"); break;
                   3895:     }
                   3896:   } else if ( OID(ord) == O_LIST ) {
                   3897:     node = BDY((LIST)ord);
                   3898:     for ( n = 0, t = node; t; t = NEXT(t), n++ );
                   3899:     l = (struct order_pair *)MALLOC_ATOMIC(n*sizeof(struct order_pair));
                   3900:     for ( i = 0, t = node, s = 0; i < n; t = NEXT(t), i++ ) {
                   3901:       tn = BDY((LIST)BDY(t)); l[i].order = ZTOS((Q)BDY(tn));
                   3902:       tn = NEXT(tn); l[i].length = ZTOS((Q)BDY(tn));
                   3903:       s += l[i].length;
                   3904:     }
                   3905:     if ( s != nvar )
                   3906:       error("create_comp_sig_spec : invalid spec");
                   3907:     spec->order_pair = l;
                   3908:     spec->block_length = n;
                   3909:     spec->cmpdl = sig_cmpdl_op;
                   3910:   } else if ( OID(ord) == O_MAT ) {
                   3911:     m = (MAT)ord; b = (Obj **)BDY(m);
                   3912:     if ( m->col != nvar )
                   3913:       error("create_comp_sig_spec : invalid spec");
                   3914:     w = almat(m->row,m->col);
                   3915:     for ( i = 0; i < m->row; i++ )
                   3916:       for ( j = 0; j < m->col; j++ )
                   3917:         w[i][j] = ZTOS((Q)b[i][j]);
                   3918:     spec->row = m->row;
                   3919:     spec->matrix = w;
                   3920:     spec->cmpdl = sig_cmpdl_mat;
                   3921:   } else
                   3922:     error("create_comp_sig_spec : invalid spec");
                   3923:   if ( weight != 0 ) {
                   3924:     node = BDY((LIST)weight);
                   3925:     a = (int *)MALLOC(nvar*sizeof(int));
                   3926:     for ( i = 0; i < nvar; i++, node = NEXT(node) )
                   3927:       a[i] = ZTOS((Z)BDY(node));
                   3928:     spec->weight = a;
                   3929:   }
                   3930:   return spec;
                   3931: }
                   3932:
                   3933: #define SIG_MUL_WEIGHT(a,i) (weight?(a)*weight[i]:(a))
                   3934:
                   3935: int comp_sig_monomial(int n,DL d1,DL d2)
                   3936: {
                   3937:   static DL m1,m2;
                   3938:   static int nvar = 0;
                   3939:   int *oldv,*weight;
                   3940:   int i,w1,w2;
                   3941:
                   3942:   if ( nvar != n ) {
                   3943:     nvar = n; NEWDL(m1,nvar); NEWDL(m2,nvar);
                   3944:   }
                   3945:   if ( !nd_sba_modord )
                   3946:     return (*cmpdl)(n,d1,d2);
1.55      noro     3947:   else if ( !nd_sba_modord->weight && !nd_sba_modord->oldv ) {
                   3948:     return (*nd_sba_modord->cmpdl)(n,d1,d2);
                   3949:   } else {
1.46      noro     3950:     weight = nd_sba_modord->weight;
                   3951:     oldv = nd_sba_modord->oldv;
                   3952:     if ( oldv ) {
                   3953:       for ( i = 0; i < n; i++ ) {
                   3954:         m1->d[i] = d1->d[oldv[i]]; m2->d[i] = d2->d[oldv[i]];
                   3955:       }
                   3956:     } else {
                   3957:       for ( i = 0; i < n; i++ ) {
                   3958:         m1->d[i] = d1->d[i]; m2->d[i] = d2->d[i];
                   3959:       }
                   3960:     }
                   3961:     for ( i = 0, w1 = w2 = 0; i < n; i++ ) {
                   3962:       w1 += SIG_MUL_WEIGHT(m1->d[i],i);
                   3963:       w2 += SIG_MUL_WEIGHT(m2->d[i],i);
                   3964:     }
                   3965:     m1->td = w1; m2->td = w2;
                   3966:     return (*nd_sba_modord->cmpdl)(n,m1,m2);
                   3967:   }
                   3968: }
                   3969:
1.24      noro     3970: int comp_sig(SIG s1,SIG s2)
                   3971: {
1.38      noro     3972:   if ( nd_sba_pot ) {
                   3973:     if ( s1->pos > s2->pos ) return 1;
                   3974:     else if ( s1->pos < s2->pos ) return -1;
1.55      noro     3975:     else return comp_sig_monomial(nd_nvar,DL(s1),DL(s2));
1.38      noro     3976:   } else {
                   3977:     static DL m1,m2;
1.44      noro     3978:     static int nvar = 0;
1.38      noro     3979:     int ret;
                   3980:
                   3981:     if ( nvar != nd_nvar ) {
                   3982:       nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
                   3983:     }
1.47      noro     3984:     if ( !nd_sba_modord )
1.55      noro     3985:       ret = (*cmpdl)(nd_nvar,DL2(s1),DL2(s2));
1.47      noro     3986:     else
1.55      noro     3987:       ret = comp_sig_monomial(nd_nvar,DL2(s1),DL2(s2));
1.38      noro     3988:     if ( ret != 0 ) return ret;
                   3989:     else if ( s1->pos > s2->pos ) return 1;
                   3990:     else if ( s1->pos < s2->pos ) return -1;
                   3991:     else return 0;
1.24      noro     3992:   }
                   3993: }
                   3994:
                   3995: int _create_spair_s(int i1,int i2,ND_pairs sp,SIG sig1,SIG sig2)
                   3996: {
                   3997:   int ret,s1,s2;
                   3998:   RHist p1,p2;
                   3999:   static int wpd;
                   4000:   static UINT *lcm;
                   4001:
                   4002:   sp->i1 = i1;
                   4003:   sp->i2 = i2;
                   4004:   p1 = nd_psh[i1];
                   4005:   p2 = nd_psh[i2];
                   4006:   ndl_lcm(DL(p1),DL(p2),sp->lcm);
1.49      noro     4007: #if 0
1.24      noro     4008:   s1 = SG(p1)-TD(DL(p1));
                   4009:   s2 = SG(p2)-TD(DL(p2));
                   4010:   SG(sp) = MAX(s1,s2) + TD(sp->lcm);
1.49      noro     4011: #endif
1.24      noro     4012:
1.26      noro     4013:   if ( wpd != nd_wpd ) {
1.24      noro     4014:     wpd = nd_wpd;
                   4015:     lcm = (UINT *)MALLOC(wpd*sizeof(UINT));
                   4016:   }
                   4017:   // DL(sig1) <- sp->lcm
                   4018:   // DL(sig1) -= DL(p1)
                   4019:   // DL(sig1) += DL(p1->sig)
1.27      noro     4020:   ndl_sub(sp->lcm,DL(p1),lcm);
1.24      noro     4021:   _ndltodl(lcm,DL(sig1));
                   4022:   _addtodl(nd_nvar,DL(p1->sig),DL(sig1));
                   4023:   sig1->pos = p1->sig->pos;
1.55      noro     4024:   _adddl(nd_nvar,DL(sig1),nd_sba_hm[sig1->pos],DL2(sig1));
1.24      noro     4025:
                   4026:   // DL(sig2) <- sp->lcm
                   4027:   // DL(sig2) -= DL(p2)
                   4028:   // DL(sig2) += DL(p2->sig)
1.27      noro     4029:   ndl_sub(sp->lcm,DL(p2),lcm);
1.24      noro     4030:   _ndltodl(lcm,DL(sig2));
                   4031:   _addtodl(nd_nvar,DL(p2->sig),DL(sig2));
                   4032:   sig2->pos = p2->sig->pos;
1.55      noro     4033:   _adddl(nd_nvar,DL(sig2),nd_sba_hm[sig2->pos],DL2(sig2));
1.24      noro     4034:
                   4035:   ret = comp_sig(sig1,sig2);
                   4036:   if ( ret == 0 ) return 0;
                   4037:   else if ( ret > 0 ) sp->sig = sig1;
                   4038:   else sp->sig = sig2;
1.49      noro     4039:
                   4040:   s1 = DL(sig1)->td+nd_sba_hm[p1->sig->pos]->td;
                   4041:   s2 = DL(sig2)->td+nd_sba_hm[p2->sig->pos]->td;
                   4042:   SG(sp) = MAX(s1,s2);
                   4043:
1.24      noro     4044:   return 1;
                   4045: }
                   4046:
                   4047: SIG dup_sig(SIG sig)
                   4048: {
                   4049:   SIG r;
                   4050:
                   4051:   if ( !sig ) return 0;
                   4052:   else {
                   4053:     NEWSIG(r);
                   4054:     _copydl(nd_nvar,DL(sig),DL(r));
1.55      noro     4055:     _copydl(nd_nvar,DL2(sig),DL2(r));
1.24      noro     4056:     r->pos = sig->pos;
                   4057:     return r;
                   4058:   }
                   4059: }
                   4060:
                   4061: void dup_ND_pairs(ND_pairs to,ND_pairs from)
                   4062: {
                   4063:   to->i1 = from->i1;
                   4064:   to->i2 = from->i2;
                   4065:   to->sugar = from->sugar;
                   4066:   to->sugar2 = from->sugar2;
                   4067:   ndl_copy(from->lcm,to->lcm);
                   4068:   to->sig = dup_sig(from->sig);
                   4069: }
                   4070:
                   4071: ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
                   4072: {
                   4073:   struct oND_pairs root;
                   4074:   ND_pairs q1,q2,r0,r;
                   4075:   int ret;
                   4076:
                   4077:   r = &root;
                   4078:   for ( q1 = p1, q2 = p2; q1 != 0 && q2 != 0; ) {
                   4079:     ret = comp_sig(q1->sig,q2->sig);
                   4080:     if ( ret < 0 ) {
                   4081:       r->next = q1; r = q1; q1 = q1->next;
                   4082:     } else if ( ret > 0 ) {
                   4083:       r->next = q2; r = q2; q2 = q2->next;
                   4084:     } else {
1.47      noro     4085:       Nnewpair--;
1.24      noro     4086:       ret = DL_COMPARE(q1->lcm,q2->lcm);
1.35      noro     4087:       Nsamesig++;
1.24      noro     4088:       if ( ret < 0 ) {
                   4089:         r->next = q1; r = q1; q1 = q1->next;
                   4090:         q2 = q2->next;
                   4091:       } else {
                   4092:         r->next = q2; r = q2; q2 = q2->next;
                   4093:         q1 = q1->next;
                   4094:       }
                   4095:     }
                   4096:   }
                   4097:   if ( q1 ) {
                   4098:     r->next = q1;
                   4099:   } else {
                   4100:     r->next = q2;
                   4101:   }
                   4102:   return root.next;
                   4103: }
                   4104:
                   4105: ND_pairs insert_pair_s(ND_pairs l,ND_pairs s)
                   4106: {
                   4107:   ND_pairs p,prev;
1.47      noro     4108:   int ret=1;
1.24      noro     4109:
                   4110:   for ( p = l, prev = 0; p != 0; prev = p, p = p->next ) {
                   4111:     if ( (ret = comp_sig(s->sig,p->sig)) <= 0 )
                   4112:       break;
                   4113:   }
                   4114:   if ( ret == 0 ) {
                   4115:     ret = DL_COMPARE(s->lcm,p->lcm);
                   4116:     if ( ret < 0 ) {
                   4117:       // replace p with s
                   4118:       s->next = p->next;
                   4119:       if ( prev == 0 ) {
                   4120:         return s;
                   4121:       } else {
                   4122:         prev->next = s;
                   4123:         return l;
                   4124:       }
                   4125:     } else
                   4126:       return l;
                   4127:   } else {
                   4128:     // insert s between prev and p
1.47      noro     4129:     Nnewpair++;
1.24      noro     4130:     s->next = p;
                   4131:     if ( prev == 0 ) {
                   4132:       return s;
                   4133:     } else {
                   4134:       prev->next = s;
                   4135:       return l;
                   4136:     }
                   4137:   }
                   4138: }
                   4139:
1.29      noro     4140: INLINE int __dl_redble(DL d1,DL d2,int nvar)
                   4141: {
                   4142:   int i;
                   4143:
                   4144:   if ( d1->td > d2->td )
                   4145:     return 0;
                   4146:   for ( i = nvar-1; i >= 0; i-- )
                   4147:     if ( d1->d[i] > d2->d[i] )
                   4148:       break;
                   4149:   if ( i >= 0 )
                   4150:     return 0;
                   4151:   else
                   4152:     return 1;
                   4153: }
                   4154:
1.30      noro     4155: ND_pairs nd_newpairs_s(int t, NODE *syz)
1.24      noro     4156: {
                   4157:   NODE h,s;
                   4158:   UINT *dl;
1.27      noro     4159:   int ts,ret,i;
1.24      noro     4160:   ND_pairs r,r0,_sp,sp;
1.29      noro     4161:   SIG spsig,tsig;
1.44      noro     4162:   static int nvar = 0;
1.29      noro     4163:   static SIG _sig1,_sig2;
1.26      noro     4164:   struct oEGT eg1,eg2,eg3,eg4;
1.24      noro     4165:
                   4166:   NEWND_pairs(_sp);
1.29      noro     4167:   if ( !_sig1 || nvar != nd_nvar ) {
                   4168:     nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
                   4169:   }
1.24      noro     4170:   r0 = 0;
1.27      noro     4171:   for ( i = 0; i < t; i++ ) {
                   4172:     ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
1.24      noro     4173:     if ( ret ) {
                   4174:       spsig = _sp->sig;
1.29      noro     4175:       for ( s = syz[spsig->pos]; s; s = s->next ) {
1.24      noro     4176:         tsig = (SIG)s->body;
1.29      noro     4177:         if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
1.24      noro     4178:           break;
                   4179:       }
                   4180:       if ( s == 0 ) {
                   4181:         NEWND_pairs(sp);
                   4182:         dup_ND_pairs(sp,_sp);
                   4183:         r0 = insert_pair_s(r0,sp);
1.26      noro     4184:       } else
                   4185:         Nsyz++;
1.24      noro     4186:     }
                   4187:   }
                   4188:   return r0;
                   4189: }
                   4190:
1.47      noro     4191: ND_pairs *nd_newpairs_array_s(int t, NODE *syz)
                   4192: {
                   4193:   NODE h,s;
                   4194:   UINT *dl;
                   4195:   int ts,ret,i;
                   4196:   ND_pairs r,r0,_sp,sp;
                   4197:   ND_pairs *d;
                   4198:   SIG spsig,tsig;
                   4199:   static int nvar = 0;
                   4200:   static SIG _sig1,_sig2;
                   4201:   struct oEGT eg1,eg2,eg3,eg4;
                   4202:
                   4203:   NEWND_pairs(_sp);
                   4204:   if ( !_sig1 || nvar != nd_nvar ) {
                   4205:     nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
                   4206:   }
                   4207:   d = (ND_pairs *)MALLOC(nd_nbase*sizeof(ND_pairs));
                   4208:   Nnewpair = 0;
                   4209:   for ( i = 0; i < t; i++ ) {
                   4210:     ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
                   4211:     if ( ret ) {
                   4212:       spsig = _sp->sig;
                   4213:       for ( s = syz[spsig->pos]; s; s = s->next ) {
                   4214:         tsig = (SIG)s->body;
                   4215:         if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
                   4216:           break;
                   4217:       }
                   4218:       if ( s == 0 ) {
                   4219:         NEWND_pairs(sp);
                   4220:         dup_ND_pairs(sp,_sp);
                   4221:         d[spsig->pos] = insert_pair_s(d[spsig->pos],sp);
                   4222:       } else
                   4223:         Nsyz++;
                   4224:     }
                   4225:   }
                   4226:   return d;
                   4227: }
                   4228:
1.1       noro     4229: /* ipair = [i1,i2],[i1,i2],... */
                   4230: ND_pairs nd_ipairtospair(NODE ipair)
                   4231: {
                   4232:   int s1,s2;
                   4233:   NODE tn,t;
                   4234:   ND_pairs r,r0;
                   4235:
                   4236:   for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
                   4237:     NEXTND_pairs(r0,r);
                   4238:     tn = BDY((LIST)BDY(t));
1.6       noro     4239:     r->i1 = ZTOS((Q)ARG0(tn));
                   4240:     r->i2 = ZTOS((Q)ARG1(tn));
1.1       noro     4241:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
                   4242:     s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   4243:     s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
                   4244:     SG(r) = MAX(s1,s2) + TD(LCM(r));
                   4245:     /* experimental */
                   4246:     if ( nd_sugarweight )
                   4247:         r->sugar2 = ndl_weight2(r->lcm);
                   4248:   }
                   4249:   if ( r0 ) NEXT(r) = 0;
                   4250:   return r0;
                   4251: }
                   4252:
                   4253: /* kokokara */
                   4254:
                   4255: ND_pairs crit_B( ND_pairs d, int s )
                   4256: {
                   4257:   ND_pairs cur,head,prev,remove;
                   4258:   UINT *t,*tl,*lcm;
                   4259:   int td,tdl;
                   4260:
                   4261:   if ( !d ) return 0;
                   4262:   t = DL(nd_psh[s]);
                   4263:   prev = 0;
                   4264:   head = cur = d;
                   4265:   lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   4266:   while ( cur ) {
                   4267:     tl = cur->lcm;
                   4268:     if ( ndl_reducible(tl,t) ) {
                   4269:       ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
                   4270:       if ( !ndl_equal(lcm,tl) ) {
                   4271:         ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
                   4272:         if (!ndl_equal(lcm,tl)) {
                   4273:           remove = cur;
                   4274:           if ( !prev ) {
                   4275:             head = cur = NEXT(cur);
                   4276:           } else {
                   4277:             cur = NEXT(prev) = NEXT(cur);
                   4278:           }
1.41      noro     4279:           FREENDP(remove); NcriB++;
1.1       noro     4280:         } else {
                   4281:           prev = cur; cur = NEXT(cur);
                   4282:         }
                   4283:       } else {
                   4284:         prev = cur; cur = NEXT(cur);
                   4285:       }
                   4286:     } else {
                   4287:       prev = cur; cur = NEXT(cur);
                   4288:     }
                   4289:   }
                   4290:   return head;
                   4291: }
                   4292:
                   4293: ND_pairs crit_M( ND_pairs d1 )
                   4294: {
                   4295:     ND_pairs e,d2,d3,dd,p;
                   4296:     UINT *id,*jd;
                   4297:
                   4298:     if ( !d1 ) return d1;
                   4299:     for ( dd = 0, e = d1; e; e = d3 ) {
                   4300:         if ( !(d2 = NEXT(e)) ) {
                   4301:             NEXT(e) = dd;
                   4302:             return e;
                   4303:         }
                   4304:         id = LCM(e);
                   4305:         for ( d3 = 0; d2; d2 = p ) {
                   4306:             p = NEXT(d2);
                   4307:             jd = LCM(d2);
                   4308:             if ( ndl_equal(jd,id) )
                   4309:                 ;
                   4310:             else if ( TD(jd) > TD(id) )
                   4311:                 if ( ndl_reducible(jd,id) ) continue;
                   4312:                 else ;
                   4313:             else if ( ndl_reducible(id,jd) ) goto delit;
                   4314:             NEXT(d2) = d3;
                   4315:             d3 = d2;
                   4316:         }
                   4317:         NEXT(e) = dd;
                   4318:         dd = e;
                   4319:         continue;
                   4320:         /**/
                   4321:     delit:    NEXT(d2) = d3;
                   4322:         d3 = d2;
                   4323:         for ( ; p; p = d2 ) {
                   4324:             d2 = NEXT(p);
                   4325:             NEXT(p) = d3;
                   4326:             d3 = p;
                   4327:         }
                   4328:         FREENDP(e);
                   4329:     }
                   4330:     return dd;
                   4331: }
                   4332:
                   4333: ND_pairs crit_F( ND_pairs d1 )
                   4334: {
                   4335:     ND_pairs rest, head,remove;
                   4336:     ND_pairs last, p, r, w;
                   4337:     int s;
                   4338:
                   4339:     if ( !d1 ) return d1;
                   4340:     for ( head = last = 0, p = d1; NEXT(p); ) {
                   4341:         r = w = equivalent_pairs(p,&rest);
                   4342:         s = SG(r);
                   4343:         w = NEXT(w);
                   4344:         while ( w ) {
                   4345:             if ( crit_2(w->i1,w->i2) ) {
                   4346:                 r = w;
                   4347:                 w = NEXT(w);
                   4348:                 while ( w ) {
                   4349:                     remove = w;
                   4350:                     w = NEXT(w);
                   4351:                     FREENDP(remove);
                   4352:                 }
                   4353:                 break;
                   4354:             } else if ( SG(w) < s ) {
                   4355:                 FREENDP(r);
                   4356:                 r = w;
                   4357:                 s = SG(r);
                   4358:                 w = NEXT(w);
                   4359:             } else {
                   4360:                 remove = w;
                   4361:                 w = NEXT(w);
                   4362:                 FREENDP(remove);
                   4363:             }
                   4364:         }
                   4365:         if ( last ) NEXT(last) = r;
                   4366:         else head = r;
                   4367:         NEXT(last = r) = 0;
                   4368:         p = rest;
                   4369:         if ( !p ) return head;
                   4370:     }
                   4371:     if ( !last ) return p;
                   4372:     NEXT(last) = p;
                   4373:     return head;
                   4374: }
                   4375:
                   4376: int crit_2( int dp1, int dp2 )
                   4377: {
                   4378:     return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
                   4379: }
                   4380:
                   4381: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
                   4382: {
                   4383:     ND_pairs w,p,r,s;
                   4384:     UINT *d;
                   4385:
                   4386:     w = d1;
                   4387:     d = LCM(w);
                   4388:     s = NEXT(w);
                   4389:     NEXT(w) = 0;
                   4390:     for ( r = 0; s; s = p ) {
                   4391:         p = NEXT(s);
                   4392:         if ( ndl_equal(d,LCM(s)) ) {
                   4393:             NEXT(s) = w; w = s;
                   4394:         } else {
                   4395:             NEXT(s) = r; r = s;
                   4396:         }
                   4397:     }
                   4398:     *prest = r;
                   4399:     return w;
                   4400: }
                   4401:
                   4402: NODE update_base(NODE nd,int ndp)
                   4403: {
                   4404:     UINT *dl, *dln;
                   4405:     NODE last, p, head;
                   4406:
                   4407:     dl = DL(nd_psh[ndp]);
                   4408:     for ( head = last = 0, p = nd; p; ) {
                   4409:         dln = DL(nd_psh[(long)BDY(p)]);
                   4410:         if ( ndl_reducible( dln, dl ) ) {
                   4411:             p = NEXT(p);
                   4412:             if ( last ) NEXT(last) = p;
                   4413:         } else {
                   4414:             if ( !last ) head = p;
                   4415:             p = NEXT(last = p);
                   4416:         }
                   4417:     }
                   4418:     head = append_one(head,ndp);
                   4419:     return head;
                   4420: }
                   4421:
                   4422: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   4423: {
                   4424:     ND_pairs m,ml,p,l;
                   4425:     UINT *lcm;
                   4426:     int s,td,len,tlen,c,c1;
                   4427:
                   4428:     if ( !(p = NEXT(m = d)) ) {
                   4429:         *prest = p;
                   4430:         NEXT(m) = 0;
                   4431:         return m;
                   4432:     }
                   4433:     if ( !NoSugar ) {
                   4434:         if ( nd_sugarweight ) {
                   4435:             s = m->sugar2;
                   4436:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   4437:                 if ( (p->sugar2 < s)
                   4438:                     || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   4439:                     ml = l; m = p; s = m->sugar2;
                   4440:                 }
                   4441:         } else {
                   4442:             s = SG(m);
                   4443:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   4444:                 if ( (SG(p) < s)
                   4445:                     || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   4446:                     ml = l; m = p; s = SG(m);
                   4447:                 }
                   4448:         }
                   4449:     } else {
                   4450:         for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   4451:             if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
                   4452:                 ml = l; m = p; s = SG(m);
                   4453:             }
                   4454:     }
                   4455:     if ( !ml ) *prest = NEXT(m);
                   4456:     else {
                   4457:         NEXT(ml) = NEXT(m);
                   4458:         *prest = d;
                   4459:     }
                   4460:     NEXT(m) = 0;
                   4461:     return m;
                   4462: }
                   4463:
                   4464: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   4465: {
                   4466:     int msugar,i;
                   4467:     ND_pairs t,dm0,dm,dr0,dr;
                   4468:
                   4469:     if ( nd_sugarweight ) {
                   4470:         for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
                   4471:             if ( t->sugar2 < msugar ) msugar = t->sugar2;
                   4472:         dm0 = 0; dr0 = 0;
                   4473:         for ( i = 0, t = d; t; t = NEXT(t) )
                   4474:             if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
                   4475:                 if ( dm0 ) NEXT(dm) = t;
                   4476:                 else dm0 = t;
                   4477:                 dm = t;
                   4478:                 i++;
                   4479:             } else {
                   4480:                 if ( dr0 ) NEXT(dr) = t;
                   4481:                 else dr0 = t;
                   4482:                 dr = t;
                   4483:             }
                   4484:     } else {
                   4485:         for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   4486:             if ( SG(t) < msugar ) msugar = SG(t);
                   4487:         dm0 = 0; dr0 = 0;
                   4488:         for ( i = 0, t = d; t; t = NEXT(t) )
                   4489:             if ( i < nd_f4_nsp && SG(t) == msugar ) {
                   4490:                 if ( dm0 ) NEXT(dm) = t;
                   4491:                 else dm0 = t;
                   4492:                 dm = t;
                   4493:                 i++;
                   4494:             } else {
                   4495:                 if ( dr0 ) NEXT(dr) = t;
                   4496:                 else dr0 = t;
                   4497:                 dr = t;
                   4498:             }
                   4499:     }
                   4500:     NEXT(dm) = 0;
                   4501:     if ( dr0 ) NEXT(dr) = 0;
                   4502:     *prest = dr0;
                   4503:     return dm0;
                   4504: }
                   4505:
1.28      noro     4506: ND_pairs nd_minsugarp_s( ND_pairs d, ND_pairs *prest )
                   4507: {
                   4508:   int msugar;
                   4509:   ND_pairs t,last;
                   4510:
                   4511:   for ( msugar = SG(d), t = d; t; t = NEXT(t) )
                   4512:     if ( SG(t) == msugar ) last = t;
                   4513:   *prest = last->next;
                   4514:   last->next = 0;
                   4515:   return d;
                   4516: }
                   4517:
1.1       noro     4518: int nd_tdeg(NDV c)
                   4519: {
                   4520:   int wmax = 0;
                   4521:   int i,len;
                   4522:   NMV a;
                   4523:
                   4524:   len = LEN(c);
                   4525:   for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   4526:     wmax = MAX(TD(DL(a)),wmax);
                   4527:   return wmax;
                   4528: }
                   4529:
1.24      noro     4530: int ndv_newps(int m,NDV a,NDV aq)
1.1       noro     4531: {
                   4532:     int len;
                   4533:     RHist r;
                   4534:     NDV b;
                   4535:     NODE tn;
                   4536:     LIST l;
                   4537:     Z iq;
                   4538:
                   4539:     if ( nd_psn == nd_pslen ) {
                   4540:         nd_pslen *= 2;
                   4541:         nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
                   4542:         nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
                   4543:         nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
                   4544:         nd_bound = (UINT **)
                   4545:             REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
                   4546:         nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
                   4547:         nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
                   4548:     }
                   4549:     NEWRHist(r); nd_psh[nd_psn] = r;
                   4550:     nd_ps[nd_psn] = a;
                   4551:     if ( aq ) {
                   4552:         nd_ps_trace[nd_psn] = aq;
                   4553:         if ( !m ) {
                   4554:           register_hcf(aq);
                   4555:         } else if ( m == -2 ) {
                   4556:           /* do nothing */
                   4557:         } else
                   4558:           error("ndv_newps : invalud modulus");
                   4559:         nd_bound[nd_psn] = ndv_compute_bound(aq);
                   4560: #if 1
                   4561:         SG(r) = SG(aq);
                   4562: #else
                   4563:         SG(r) = nd_tdeg(aq);
                   4564: #endif
                   4565:         ndl_copy(HDL(aq),DL(r));
1.24      noro     4566:         r->sig = dup_sig(aq->sig);
1.1       noro     4567:     } else {
                   4568:         if ( !m ) register_hcf(a);
                   4569:         nd_bound[nd_psn] = ndv_compute_bound(a);
                   4570: #if 1
                   4571:         SG(r) = SG(a);
                   4572: #else
                   4573:         SG(r) = nd_tdeg(a);
                   4574: #endif
                   4575:         ndl_copy(HDL(a),DL(r));
1.24      noro     4576:         r->sig = dup_sig(a->sig);
1.1       noro     4577:     }
                   4578:     if ( nd_demand ) {
                   4579:         if ( aq ) {
                   4580:             ndv_save(nd_ps_trace[nd_psn],nd_psn);
                   4581:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   4582:             nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   4583:             nd_ps_trace[nd_psn] = 0;
                   4584:         } else {
                   4585:             ndv_save(nd_ps[nd_psn],nd_psn);
                   4586:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
                   4587:             nd_ps[nd_psn] = 0;
                   4588:         }
                   4589:     }
                   4590:     if ( nd_gentrace ) {
                   4591:         /* reverse the tracelist and append it to alltracelist */
                   4592:         nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6       noro     4593:         STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1       noro     4594:         MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
                   4595:     }
                   4596:     return nd_psn++;
                   4597: }
                   4598:
1.46      noro     4599: // find LM wrt the specified modord
                   4600: void ndv_lm_modord(NDV p,DL d)
1.45      noro     4601: {
                   4602:   NMV m;
                   4603:   DL tmp;
                   4604:   int len,i,ret;
                   4605:
                   4606:   NEWDL(tmp,nd_nvar);
                   4607:   m = BDY(p); len = LEN(p);
1.46      noro     4608:   _ndltodl(DL(m),d); // printdl(d); printf("->");
1.45      noro     4609:   for ( i = 1, NMV_ADV(m); i < len; i++, NMV_ADV(m) ) {
                   4610:     _ndltodl(DL(m),tmp);
1.46      noro     4611:     ret = comp_sig_monomial(nd_nvar,tmp,d);
1.45      noro     4612:     if ( ret > 0 ) _copydl(nd_nvar,tmp,d);
                   4613:   }
1.46      noro     4614: //   printdl(d); printf("\n");
1.45      noro     4615: }
                   4616:
1.1       noro     4617: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
                   4618: /* return 1 if success, 0 if failure (HC(a mod p)) */
                   4619:
1.24      noro     4620: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont,int sba)
1.1       noro     4621: {
1.6       noro     4622:   int i,j,td,len,max;
                   4623:   NODE s,s0,f0,tn;
                   4624:   UINT *d;
                   4625:   RHist r;
                   4626:   NDVI w;
                   4627:   NDV a,am;
                   4628:   union oNDC hc;
                   4629:   NODE node;
                   4630:   P hcp;
                   4631:   Z iq,jq;
                   4632:   LIST l;
                   4633:
                   4634:   nd_found = 0; nd_notfirst = 0; nd_create = 0;
                   4635:   /* initialize the tracelist */
                   4636:   nd_tracelist = 0;
                   4637:
                   4638:   for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
                   4639:   w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
                   4640:   for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
                   4641:     if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
                   4642:   if ( !dont_sort ) {
                   4643:     /* XXX heuristic */
1.26      noro     4644:     if ( !sba && !nd_ord->id && (nd_ord->ord.simple<2) )
1.6       noro     4645:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   4646:         (int (*)(const void *,const void *))ndvi_compare_rev);
                   4647:     else
                   4648:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   4649:         (int (*)(const void *,const void *))ndvi_compare);
                   4650:   }
                   4651:   nd_pslen = 2*nd_psn;
                   4652:   nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4653:   nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4654:   nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4655:   nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4656:   nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
                   4657:   nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
                   4658:   nd_hcf = 0;
1.1       noro     4659:
1.6       noro     4660:   if ( trace && nd_vc )
                   4661:     makesubst(nd_vc,&nd_subst);
                   4662:   else
                   4663:     nd_subst = 0;
1.1       noro     4664:
1.6       noro     4665:   if ( !nd_red )
                   4666:     nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   4667:   for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
                   4668:   for ( i = 0; i < nd_psn; i++ ) {
                   4669:     hc = HCU(w[i].p);
                   4670:     if ( trace ) {
                   4671:       if ( mod == -2 ) {
                   4672:         /* over a large finite field */
                   4673:         /* trace = small modulus */
                   4674:         a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
                   4675:         ndv_mod(-2,a);
                   4676:         if ( !dont_removecont) ndv_removecont(-2,a);
                   4677:         am = nd_ps[i] = ndv_dup(trace,w[i].p);
                   4678:         ndv_mod(trace,am);
                   4679:       if ( DL_COMPARE(HDL(am),HDL(a)) )
                   4680:         return 0;
                   4681:         ndv_removecont(trace,am);
                   4682:       } else {
                   4683:         a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
                   4684:         if ( !dont_removecont) ndv_removecont(0,a);
                   4685:         register_hcf(a);
                   4686:         am = nd_ps[i] = ndv_dup(mod,a);
                   4687:         ndv_mod(mod,am);
                   4688:       if ( DL_COMPARE(HDL(am),HDL(a)) )
                   4689:         return 0;
                   4690:         ndv_removecont(mod,am);
                   4691:       }
                   4692:     } else {
                   4693:       a = nd_ps[i] = ndv_dup(mod,w[i].p);
                   4694:       if ( mod || !dont_removecont ) ndv_removecont(mod,a);
                   4695:       if ( !mod ) register_hcf(a);
1.1       noro     4696:     }
1.6       noro     4697:     if ( nd_gentrace ) {
                   4698:       STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
                   4699:       /* exact division */
1.1       noro     4700:       if ( !dont_removecont )
1.6       noro     4701:         ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
                   4702:       MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
                   4703:     }
                   4704:     NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
                   4705:     nd_bound[i] = ndv_compute_bound(a);
                   4706:     nd_psh[i] = r;
                   4707:     if ( nd_demand ) {
                   4708:       if ( trace ) {
                   4709:         ndv_save(nd_ps_trace[i],i);
                   4710:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   4711:         nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   4712:         nd_ps_trace[i] = 0;
                   4713:       } else {
                   4714:         ndv_save(nd_ps[i],i);
                   4715:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
                   4716:         nd_ps[i] = 0;
                   4717:       }
1.1       noro     4718:     }
1.6       noro     4719:   }
1.24      noro     4720:   if ( sba ) {
1.27      noro     4721:     nd_sba_hm = (DL *)MALLOC(nd_psn*sizeof(DL));
1.24      noro     4722:    // setup signatures
1.27      noro     4723:     for ( i = 0; i < nd_psn; i++ ) {
                   4724:       SIG sig;
1.24      noro     4725:
1.27      noro     4726:       NEWSIG(sig); sig->pos = i;
                   4727:       nd_ps[i]->sig = sig;
                   4728:       if ( nd_demand ) nd_ps_sym[i]->sig = sig;
                   4729:         nd_psh[i]->sig = sig;
                   4730:       if ( trace ) {
                   4731:         nd_ps_trace[i]->sig = sig;
                   4732:         if ( nd_demand ) nd_ps_trace_sym[i]->sig = sig;
                   4733:       }
                   4734:       NEWDL(nd_sba_hm[i],nd_nvar);
1.46      noro     4735:       if ( nd_sba_modord )
                   4736:         ndv_lm_modord(nd_ps[i],nd_sba_hm[i]);
1.45      noro     4737:       else
                   4738:         _ndltodl(DL(nd_psh[i]),nd_sba_hm[i]);
1.55      noro     4739:       _adddl(nd_nvar,DL(sig),nd_sba_hm[i],DL2(sig));
1.27      noro     4740:     }
1.29      noro     4741:     nd_sba_pos = (NODE *)MALLOC(nd_psn*sizeof(NODE));
                   4742:     for ( i = 0; i < nd_psn; i++ ) {
                   4743:       j = nd_psh[i]->sig->pos;
                   4744:       nd_sba_pos[j] = append_one(nd_sba_pos[j],i);
                   4745:     }
1.24      noro     4746:   }
1.6       noro     4747:   if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
                   4748:   return 1;
1.1       noro     4749: }
                   4750:
                   4751: struct order_spec *append_block(struct order_spec *spec,
                   4752:     int nv,int nalg,int ord);
                   4753:
                   4754: extern VECT current_dl_weight_vector_obj;
                   4755: static VECT prev_weight_vector_obj;
                   4756:
                   4757: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
                   4758:     struct order_spec **ord1p,LIST *f1p,NODE *alistp)
                   4759: {
                   4760:     NODE alist,t,s,r0,r,arg;
                   4761:     VL tv;
                   4762:     P poly;
                   4763:     DP d;
                   4764:     Alg alpha,dp;
                   4765:     DAlg inv,da,hc;
                   4766:     MP m;
                   4767:     int i,nvar,nalg,n;
                   4768:     NumberField nf;
                   4769:     LIST f1,f2;
                   4770:     struct order_spec *current_spec;
                   4771:     VECT obj,obj0;
                   4772:     VECT tmp;
                   4773:
                   4774:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
                   4775:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
                   4776:
                   4777:     for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
                   4778:         NEXTNODE(alist,t); MKV(tv->v,poly);
                   4779:         MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
                   4780:         tv->v = tv->v->priv;
                   4781:     }
                   4782:     NEXT(t) = 0;
                   4783:
                   4784:     /* simplification, making polynomials monic */
                   4785:     setfield_dalg(alist);
                   4786:     obj_algtodalg((Obj)f,(Obj *)&f1);
                   4787:     for ( t = BDY(f); t; t = NEXT(t) ) {
                   4788:         initd(ord); ptod(vv,vv,(P)BDY(t),&d);
                   4789:         hc = (DAlg)BDY(d)->c;
                   4790:         if ( NID(hc) == N_DA ) {
                   4791:             invdalg(hc,&inv);
                   4792:             for ( m = BDY(d); m; m = NEXT(m) ) {
                   4793:                 muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
                   4794:             }
                   4795:         }
                   4796:         initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
                   4797:     }
                   4798:     obj_dalgtoalg((Obj)f1,(Obj *)&f);
                   4799:
                   4800:     /* append alg vars to the var list */
                   4801:     for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
                   4802:     NEXT(tv) = av;
                   4803:
                   4804:     /* append a block to ord */
                   4805:     *ord1p = append_block(ord,nvar,nalg,2);
                   4806:
                   4807:     /* create generator list */
                   4808:     nf = get_numberfield();
                   4809:     for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
                   4810:         MKAlg(nf->defpoly[i],dp);
                   4811:         MKNODE(s,dp,t); t = s;
                   4812:     }
                   4813:     MKLIST(f1,t);
                   4814:     *alistp = alist;
                   4815:     algobjtorat((Obj)f1,(Obj *)f1p);
                   4816:
                   4817:     /* creating a new weight vector */
                   4818:     prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
                   4819:     n = nvar+nalg+1;
                   4820:     MKVECT(obj,n);
                   4821:     if ( obj0 && obj0->len == nvar )
                   4822:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
                   4823:     else
                   4824:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
                   4825:     for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
                   4826:     BDY(obj)[n-1] = (pointer)ONE;
                   4827:     arg = mknode(1,obj);
                   4828:     Pdp_set_weight(arg,&tmp);
                   4829: }
                   4830:
                   4831: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
                   4832: {
                   4833:     NODE s,t,u0,u;
                   4834:     P p;
                   4835:     VL tv;
                   4836:     Obj obj;
                   4837:     VECT tmp;
                   4838:     NODE arg;
                   4839:
                   4840:     u0 = 0;
                   4841:     for ( t = r; t; t = NEXT(t) ) {
                   4842:         p = (P)BDY(t);
                   4843:         for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
                   4844:             substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
                   4845:         }
                   4846:         if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
                   4847:             NEXTNODE(u0,u);
                   4848:             BDY(u) = (pointer)p;
                   4849:         }
                   4850:     }
                   4851:     arg = mknode(1,prev_weight_vector_obj);
                   4852:     Pdp_set_weight(arg,&tmp);
                   4853:
                   4854:     return u0;
                   4855: }
                   4856:
                   4857: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
                   4858: {
                   4859:     VL tv,fv,vv,vc,av;
                   4860:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4861:     int e,max,nvar,i;
                   4862:     NDV b;
                   4863:     int ishomo,nalg,mrank,trank,wmax,len;
                   4864:   NMV a;
                   4865:     Alg alpha,dp;
                   4866:     P p,zp;
                   4867:     Q dmy;
                   4868:     LIST f1,f2,zpl;
                   4869:     Obj obj;
                   4870:     NumberField nf;
                   4871:     struct order_spec *ord1;
                   4872:     NODE tr,tl1,tl2,tl3,tl4,nzlist;
                   4873:     LIST l1,l2,l3,l4,l5;
                   4874:   int j;
                   4875:   Z jq,bpe,last_nonzero;
                   4876:     int *perm;
                   4877:     EPOS oepos;
                   4878:     int obpe,oadv,ompos,cbpe;
1.15      noro     4879:     VECT hvect;
1.1       noro     4880:
1.41      noro     4881:     NcriB = NcriMF = Ncri2 = 0;
1.1       noro     4882:     nd_module = 0;
                   4883:     if ( !m && Demand ) nd_demand = 1;
                   4884:     else nd_demand = 0;
                   4885:
                   4886:     if ( DP_Multiple )
                   4887:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4888: #if 0
                   4889:     ndv_alloc = 0;
                   4890: #endif
                   4891:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     4892:     parse_nd_option(vv,current_option);
1.1       noro     4893:     if ( m && nd_vc )
                   4894:        error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
                   4895:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4896:     switch ( ord->id ) {
                   4897:         case 1:
                   4898:             if ( ord->nv != nvar )
                   4899:                 error("nd_{gr,f4} : invalid order specification");
                   4900:             break;
                   4901:         default:
                   4902:             break;
                   4903:     }
                   4904:     nd_nalg = 0;
                   4905:     av = 0;
                   4906:     if ( !m ) {
                   4907:         get_algtree((Obj)f,&av);
                   4908:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4909:         nd_ntrans = nvar;
                   4910:         nd_nalg = nalg;
                   4911:         /* #i -> t#i */
                   4912:         if ( nalg ) {
                   4913:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4914:             ord = ord1;
                   4915:             f = f1;
                   4916:         }
                   4917:         nvar += nalg;
                   4918:     }
                   4919:     nd_init_ord(ord);
                   4920:     mrank = 0;
                   4921:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4922:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4923:             if ( nd_module ) {
1.16      noro     4924:                 if ( OID(BDY(t)) == O_DPM ) {
                   4925:                   e = dpm_getdeg((DPM)BDY(t),&trank);
                   4926:                   max = MAX(e,max);
                   4927:                   mrank = MAX(mrank,trank);
                   4928:                 } else {
                   4929:                   s = BDY((LIST)BDY(t));
                   4930:                   trank = length(s);
                   4931:                   mrank = MAX(mrank,trank);
                   4932:                   for ( ; s; s = NEXT(s) ) {
                   4933:                       e = getdeg(tv->v,(P)BDY(s));
                   4934:                       max = MAX(e,max);
                   4935:                   }
1.1       noro     4936:                 }
                   4937:             } else {
1.43      noro     4938:                 if ( OID(BDY(t)) == O_DP ) {
                   4939:                   e = dp_getdeg((DP)BDY(t));
                   4940:                   max = MAX(e,max);
                   4941:                 } else {
                   4942:                   e = getdeg(tv->v,(P)BDY(t));
                   4943:                   max = MAX(e,max);
                   4944:                 }
1.1       noro     4945:             }
                   4946:         }
                   4947:     nd_setup_parameters(nvar,nd_nzlist?0:max);
                   4948:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   4949:     ishomo = 1;
                   4950:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4951:       if ( nd_module ) {
1.16      noro     4952:         if ( OID(BDY(t)) == O_DPM ) {
                   4953:           Z cont;
                   4954:           DPM zdpm;
                   4955:
                   4956:           if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
                   4957:           else zdpm = (DPM)BDY(t);
                   4958:           b = (pointer)dpmtondv(m,zdpm);
                   4959:         } else {
                   4960:           if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   4961:           else zpl = (LIST)BDY(t);
1.1       noro     4962:           b = (pointer)pltondv(CO,vv,zpl);
1.16      noro     4963:         }
1.1       noro     4964:       } else {
1.43      noro     4965:         if ( OID(BDY(t)) == O_DP ) {
                   4966:           DP zdp;
                   4967:
                   4968:           if ( !m && !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
                   4969:           else zdp = (DP)BDY(t);
                   4970:           b = (pointer)dptondv(m,zdp);
                   4971:         } else {
                   4972:           if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   4973:           else zp = (P)BDY(t);
                   4974:           b = (pointer)ptondv(CO,vv,zp);
                   4975:         }
1.1       noro     4976:       }
                   4977:       if ( ishomo )
                   4978:         ishomo = ishomo && ndv_ishomo(b);
                   4979:       if ( m ) ndv_mod(m,b);
                   4980:       if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4981:     }
                   4982:     if ( fd0 ) NEXT(fd) = 0;
                   4983:
                   4984:   if ( !ishomo && homo ) {
                   4985:         for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   4986:             b = (NDV)BDY(t); len = LEN(b);
                   4987:             for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   4988:                 wmax = MAX(TD(DL(a)),wmax);
                   4989:         }
                   4990:         homogenize_order(ord,nvar,&ord1);
                   4991:         nd_init_ord(ord1);
                   4992:         nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   4993:         for ( t = fd0; t; t = NEXT(t) )
                   4994:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   4995:     }
                   4996:
1.24      noro     4997:     ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0,0);
1.1       noro     4998:     if ( nd_gentrace ) {
                   4999:         MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   5000:     }
                   5001:   if ( nd_splist ) {
                   5002:     *rp = compute_splist();
                   5003:     return;
                   5004:   }
                   5005:   if ( nd_check_splist ) {
                   5006:         if ( f4 ) {
                   5007:             if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
                   5008:             else *rp = 0;
                   5009:         } else {
                   5010:             if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
                   5011:             else *rp = 0;
                   5012:         }
                   5013:     return;
                   5014:   }
                   5015:     x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
                   5016:   if ( !x ) {
                   5017:     *rp = 0; return;
                   5018:   }
1.15      noro     5019:   if ( nd_gentrace ) {
                   5020:     MKVECT(hvect,nd_psn);
                   5021:     for ( i = 0; i < nd_psn; i++ )
                   5022:        ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   5023:   }
1.1       noro     5024:   if ( !ishomo && homo ) {
                   5025:        /* dehomogenization */
                   5026:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   5027:     nd_init_ord(ord);
                   5028:     nd_setup_parameters(nvar,0);
                   5029:   }
                   5030:     nd_demand = 0;
                   5031:   if ( nd_module && nd_intersect ) {
                   5032:     for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17      noro     5033:       if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1       noro     5034:         MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
                   5035:       }
                   5036:     conv_ilist(nd_demand,0,x,0);
                   5037:     goto FINAL;
                   5038:   }
                   5039:     if ( nd_gentrace  && f4 ) { nzlist = nd_alltracelist; }
                   5040:     x = ndv_reducebase(x,perm);
                   5041:     if ( nd_gentrace  && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
                   5042:     x = ndv_reduceall(m,x);
                   5043:     cbpe = nd_bpe;
                   5044:     if ( nd_gentrace && !f4 ) {
                   5045:         tl2 = nd_alltracelist; nd_alltracelist = 0;
                   5046:         ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
                   5047:         tl3 = nd_alltracelist; nd_alltracelist = 0;
                   5048:         if ( nd_gensyz ) {
                   5049:           nd_gb(m,0,1,1,0);
                   5050:             tl4 = nd_alltracelist; nd_alltracelist = 0;
                   5051:         } else tl4 = 0;
                   5052:     }
                   5053:     nd_bpe = cbpe;
                   5054:     nd_setup_parameters(nd_nvar,0);
                   5055: FINAL:
                   5056:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16      noro     5057:       NEXTNODE(r0,r);
                   5058:       if ( nd_module ) {
                   5059:         if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
                   5060:         else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
                   5061:       } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
                   5062:       else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1       noro     5063:     }
                   5064:     if ( r0 ) NEXT(r) = 0;
                   5065:     if ( !m && nd_nalg )
                   5066:         r0 = postprocess_algcoef(av,alist,r0);
                   5067:     MKLIST(*rp,r0);
                   5068:     if ( nd_gentrace ) {
                   5069:   if ( f4 ) {
1.6       noro     5070:             STOZ(16,bpe);
                   5071:             STOZ(nd_last_nonzero,last_nonzero);
1.15      noro     5072:             tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1       noro     5073:         } else {
                   5074:             tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   5075:             tl3 = reverse_node(tl3);
                   5076:             /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   5077:             for ( t = tl2; t; t = NEXT(t) ) {
                   5078:             /* s = [i,[*,j,*,*],...] */
                   5079:                 s = BDY((LIST)BDY(t));
1.6       noro     5080:                 j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     5081:                 for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     5082:                     j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     5083:                     ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   5084:                 }
                   5085:             }
                   5086:             for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6       noro     5087:                 STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     5088:             }
                   5089:             MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   5090:             MKLIST(l5,tl4);
1.6       noro     5091:             STOZ(nd_bpe,bpe);
1.15      noro     5092:             tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     5093:         }
                   5094:     }
                   5095: #if 0
                   5096:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   5097: #endif
                   5098: }
                   5099:
1.28      noro     5100: NODE nd_sba_f4(int m,int **indp);
                   5101:
                   5102: void nd_sba(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.24      noro     5103: {
                   5104:   VL tv,fv,vv,vc,av;
1.55      noro     5105:   NODE fd,fd0,r,r0,t,x,s,xx,nd,nd1,syz;
1.24      noro     5106:   int e,max,nvar,i;
                   5107:   NDV b;
                   5108:   int ishomo,nalg,wmax,len;
                   5109:   NMV a;
                   5110:   P p,zp;
                   5111:   Q dmy;
                   5112:   struct order_spec *ord1;
                   5113:   int j;
                   5114:   int *perm;
                   5115:   EPOS oepos;
                   5116:   int obpe,oadv,ompos,cbpe;
1.51      noro     5117:   struct oEGT eg0,eg1,egconv,egintred;
1.55      noro     5118:   LIST l1,redind;
                   5119:   Z z;
1.24      noro     5120:
                   5121:   nd_module = 0;
                   5122:   nd_demand = 0;
1.35      noro     5123:   Nsamesig = 0;
1.24      noro     5124:   if ( DP_Multiple )
                   5125:     nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   5126:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     5127:   parse_nd_option(vv,current_option);
1.24      noro     5128:   if ( m && nd_vc )
                   5129:     error("nd_sba : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
                   5130:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5131:   switch ( ord->id ) {
                   5132:     case 1:
                   5133:       if ( ord->nv != nvar )
                   5134:         error("nd_sba : invalid order specification");
                   5135:         break;
                   5136:       default:
                   5137:         break;
                   5138:   }
                   5139:   nd_nalg = 0;
                   5140:   nd_init_ord(ord);
                   5141:   // for SIG comparison
                   5142:   initd(ord);
                   5143:   for ( t = BDY(f), max = 1; t; t = NEXT(t) ) {
                   5144:     for ( tv = vv; tv; tv = NEXT(tv) ) {
1.43      noro     5145:       if ( OID(BDY(t)) == O_DP ) {
                   5146:         e = dp_getdeg((DP)BDY(t));
                   5147:         max = MAX(e,max);
                   5148:       } else {
                   5149:         e = getdeg(tv->v,(P)BDY(t));
                   5150:         max = MAX(e,max);
                   5151:       }
1.24      noro     5152:     }
                   5153:   }
1.25      noro     5154:   nd_setup_parameters(nvar,max);
1.24      noro     5155:   obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   5156:   ishomo = 1;
                   5157:   for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.43      noro     5158:     if ( OID(BDY(t)) == O_DP ) {
                   5159:       DP zdp;
                   5160:
                   5161:       if ( !m ) dp_ptozp((DP)BDY(t),&zdp);
                   5162:       else zdp = (DP)BDY(t);
                   5163:       b = (pointer)dptondv(m,zdp);
                   5164:     } else {
                   5165:       if ( !m ) ptozp((P)BDY(t),1,&dmy,&zp);
                   5166:       else zp = (P)BDY(t);
                   5167:       b = (pointer)ptondv(CO,vv,zp);
                   5168:     }
1.24      noro     5169:     if ( ishomo )
                   5170:       ishomo = ishomo && ndv_ishomo(b);
                   5171:     if ( m ) ndv_mod(m,b);
                   5172:     if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   5173:   }
                   5174:   if ( fd0 ) NEXT(fd) = 0;
                   5175:
                   5176:   if ( !ishomo && homo ) {
                   5177:     for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   5178:       b = (NDV)BDY(t); len = LEN(b);
                   5179:       for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   5180:         wmax = MAX(TD(DL(a)),wmax);
                   5181:       }
                   5182:       homogenize_order(ord,nvar,&ord1);
                   5183:       nd_init_ord(ord1);
1.42      noro     5184:       // for SIG comparison
                   5185:       initd(ord1);
1.24      noro     5186:       nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   5187:       for ( t = fd0; t; t = NEXT(t) )
                   5188:         ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   5189:   }
                   5190:
1.39      noro     5191:   ndv_setup(m,0,fd0,nd_sba_dontsort,0,1);
1.55      noro     5192:   if ( nd_gentrace ) {
                   5193:     MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   5194:   }
1.44      noro     5195:   x = f4 ? nd_sba_f4(m,&perm) : nd_sba_buch(m,ishomo || homo,&perm,&syz);
1.24      noro     5196:   if ( !x ) {
                   5197:     *rp = 0; return;
                   5198:   }
                   5199:   if ( !ishomo && homo ) {
                   5200:        /* dehomogenization */
                   5201:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   5202:     nd_init_ord(ord);
1.42      noro     5203:     // for SIG comparison
                   5204:     initd(ord);
1.24      noro     5205:     nd_setup_parameters(nvar,0);
                   5206:   }
                   5207:   nd_demand = 0;
1.51      noro     5208:   get_eg(&eg0);
1.24      noro     5209:   x = ndv_reducebase(x,perm);
1.55      noro     5210:   for ( nd = 0, i = length(x)-1; i >= 0; i-- ) {
                   5211:     STOZ(perm[i],z); MKNODE(nd1,z,nd); nd = nd1;
                   5212:   }
                   5213:   MKLIST(redind,nd);
1.24      noro     5214:   x = ndv_reduceall(m,x);
1.51      noro     5215:   get_eg(&eg1); init_eg(&egintred); add_eg(&egintred,&eg0,&eg1);
1.24      noro     5216:   nd_setup_parameters(nd_nvar,0);
1.29      noro     5217:   get_eg(&eg0);
1.24      noro     5218:   for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   5219:     NEXTNODE(r0,r);
                   5220:     if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
1.34      noro     5221:     else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.24      noro     5222:   }
                   5223:   if ( r0 ) NEXT(r) = 0;
1.44      noro     5224:   if ( nd_sba_syz ) {
                   5225:     LIST gb,hsyz;
                   5226:
                   5227:     MKLIST(gb,r0);
                   5228:     MKLIST(hsyz,syz);
                   5229:     nd = mknode(2,gb,hsyz);
                   5230:     MKLIST(*rp,nd);
1.55      noro     5231:   } else if ( nd_gentrace ) {
                   5232:     LIST gb,trace;
                   5233:
                   5234:     MKLIST(trace,nd_alltracelist);
                   5235:     MKLIST(gb,r0);
                   5236:     nd = mknode(3,gb,redind,trace);
                   5237:     MKLIST(*rp,nd);
1.44      noro     5238:   } else
                   5239:     MKLIST(*rp,r0);
1.29      noro     5240:   get_eg(&eg1); init_eg(&egconv); add_eg(&egconv,&eg0,&eg1);
1.51      noro     5241:   print_eg("intred",&egintred); fprintf(asir_out,"\n");
1.29      noro     5242:   print_eg("conv",&egconv); fprintf(asir_out,"\n");
1.24      noro     5243: }
                   5244:
1.1       noro     5245: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
                   5246: {
                   5247:     VL tv,fv,vv,vc,av;
                   5248:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   5249:     int e,max,nvar,i;
                   5250:     NDV b;
                   5251:     int ishomo,nalg;
                   5252:     Alg alpha,dp;
                   5253:     P p,zp;
                   5254:     Q dmy;
                   5255:     LIST f1,f2;
                   5256:     Obj obj;
                   5257:     NumberField nf;
                   5258:     struct order_spec *ord1;
                   5259:     int *perm;
                   5260:
                   5261:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     5262:     parse_nd_option(vv,current_option);
1.1       noro     5263:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5264:     switch ( ord->id ) {
                   5265:         case 1:
                   5266:             if ( ord->nv != nvar )
                   5267:                 error("nd_check : invalid order specification");
                   5268:             break;
                   5269:         default:
                   5270:             break;
                   5271:     }
                   5272:     nd_nalg = 0;
                   5273:     av = 0;
                   5274:     if ( !m ) {
                   5275:         get_algtree((Obj)f,&av);
                   5276:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   5277:         nd_ntrans = nvar;
                   5278:         nd_nalg = nalg;
                   5279:         /* #i -> t#i */
                   5280:         if ( nalg ) {
                   5281:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   5282:             ord = ord1;
                   5283:             f = f1;
                   5284:         }
                   5285:         nvar += nalg;
                   5286:     }
                   5287:     nd_init_ord(ord);
                   5288:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   5289:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   5290:             e = getdeg(tv->v,(P)BDY(t));
                   5291:             max = MAX(e,max);
                   5292:         }
                   5293:     nd_setup_parameters(nvar,max);
                   5294:     ishomo = 1;
                   5295:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   5296:         ptozp((P)BDY(t),1,&dmy,&zp);
                   5297:         b = (pointer)ptondv(CO,vv,zp);
                   5298:         if ( ishomo )
                   5299:             ishomo = ishomo && ndv_ishomo(b);
                   5300:         if ( m ) ndv_mod(m,b);
                   5301:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   5302:     }
                   5303:     if ( fd0 ) NEXT(fd) = 0;
1.24      noro     5304:     ndv_setup(m,0,fd0,0,1,0);
1.1       noro     5305:     for ( x = 0, i = 0; i < nd_psn; i++ )
                   5306:         x = update_base(x,i);
                   5307:     if ( do_check ) {
                   5308:         x = nd_gb(m,ishomo,1,0,&perm);
                   5309:         if ( !x ) {
                   5310:             *rp = 0;
                   5311:             return;
                   5312:         }
                   5313:     } else {
                   5314: #if 0
                   5315:     /* bug ? */
                   5316:         for ( t = x; t; t = NEXT(t) )
                   5317:             BDY(t) = (pointer)nd_ps[(long)BDY(t)];
                   5318: #else
                   5319:     conv_ilist(0,0,x,&perm);
                   5320: #endif
                   5321:     }
                   5322:     x = ndv_reducebase(x,perm);
                   5323:     x = ndv_reduceall(m,x);
                   5324:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   5325:         NEXTNODE(r0,r);
                   5326:         BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   5327:     }
                   5328:     if ( r0 ) NEXT(r) = 0;
                   5329:     if ( !m && nd_nalg )
                   5330:         r0 = postprocess_algcoef(av,alist,r0);
                   5331:     MKLIST(*rp,r0);
                   5332: }
                   5333:
                   5334: NDV recompute_trace(NODE trace,NDV *p,int m);
                   5335: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
                   5336:
                   5337: NDV recompute_trace(NODE ti,NDV *p,int mod)
                   5338: {
                   5339:   int c,c1,c2,i;
                   5340:   NM mul,m,tail;
                   5341:   ND d,r,rm;
                   5342:   NODE sj;
                   5343:   NDV red;
                   5344:   Obj mj;
                   5345:
                   5346:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   5347:   CM(mul) = 1;
                   5348:   tail = 0;
                   5349:   for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
                   5350:     sj = BDY((LIST)BDY(ti));
                   5351:     if ( ARG0(sj) ) {
1.6       noro     5352:       red = p[ZTOS((Q)ARG1(sj))];
1.1       noro     5353:       mj = (Obj)ARG2(sj);
                   5354:       if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
                   5355:       else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
                   5356:       rm = ndv_mul_nm(mod,mul,red);
                   5357:       if ( !r ) r = rm;
                   5358:       else {
                   5359:         for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
                   5360:           if ( d ) {
                   5361:             NEXT(tail) = m; tail = m; LEN(d)++;
                   5362:           } else {
                   5363:             MKND(nd_nvar,m,1,d); tail = BDY(d);
                   5364:           }
                   5365:         }
                   5366:         if ( !m ) return 0; /* failure */
                   5367:                 else {
                   5368:           BDY(r) = m;
                   5369:                     if ( mod > 0 || mod == -1 ) {
                   5370:             c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
                   5371:             DMAR(c1,c2,0,mod,c);
                   5372:             nd_mul_c(mod,rm,c);
                   5373:                     } else {
                   5374:                       Z t,u;
                   5375:
                   5376:                       chsgnlf(HCZ(r),&t);
                   5377:                       divlf(t,HCZ(rm),&u);
                   5378:                       nd_mul_c_lf(rm,u);
                   5379:                     }
                   5380:           r = nd_add(mod,r,rm);
                   5381:         }
                   5382:           }
                   5383:      }
                   5384:   }
                   5385:   if ( tail ) NEXT(tail) = 0;
                   5386:   d = nd_add(mod,d,r);
                   5387:   nd_mul_c(mod,d,invm(HCM(d),mod));
                   5388:   return ndtondv(mod,d);
                   5389: }
                   5390:
                   5391: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
                   5392: {
                   5393:     VL tv,fv,vv,vc,av;
                   5394:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   5395:     int e,max,nvar,i;
                   5396:     NDV b;
                   5397:     int ishomo,nalg;
                   5398:     Alg alpha,dp;
                   5399:     P p,zp;
                   5400:     Q dmy;
                   5401:     LIST f1,f2;
                   5402:     Obj obj;
                   5403:     NumberField nf;
                   5404:     struct order_spec *ord1;
                   5405:   NODE permtrace,intred,ind,perm,trace,ti;
                   5406:   int len,n,j;
                   5407:   NDV *db,*pb;
                   5408:
                   5409:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     5410:     parse_nd_option(vv,current_option);
1.1       noro     5411:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5412:     switch ( ord->id ) {
                   5413:         case 1:
                   5414:             if ( ord->nv != nvar )
                   5415:                 error("nd_check : invalid order specification");
                   5416:             break;
                   5417:         default:
                   5418:             break;
                   5419:     }
                   5420:     nd_init_ord(ord);
1.6       noro     5421:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     5422:     nd_setup_parameters(nvar,0);
                   5423:
                   5424:   len = length(BDY(f));
                   5425:   db = (NDV *)MALLOC(len*sizeof(NDV *));
                   5426:   for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
                   5427:       ptozp((P)BDY(t),1,&dmy,&zp);
                   5428:       b = ptondv(CO,vv,zp);
                   5429:         ndv_mod(m,b);
                   5430:     ndv_mul_c(m,b,invm(HCM(b),m));
                   5431:     db[i] = b;
                   5432:     }
                   5433:
                   5434:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   5435:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   5436:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   5437:   perm = BDY((LIST)ARG0(permtrace));
                   5438:   trace = NEXT(permtrace);
                   5439:
                   5440:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     5441:     j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1       noro     5442:     if ( j > i ) i = j;
                   5443:   }
                   5444:   n = i+1;
                   5445:   pb = (NDV *)MALLOC(n*sizeof(NDV *));
                   5446:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   5447:     ti = BDY((LIST)BDY(t));
1.6       noro     5448:     pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1       noro     5449:   }
                   5450:   for ( t = trace; t; t = NEXT(t) ) {
                   5451:     ti = BDY((LIST)BDY(t));
1.6       noro     5452:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   5453:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     5454:       if ( DP_Print ) {
                   5455:            fprintf(asir_out,"."); fflush(asir_out);
                   5456:       }
                   5457:   }
                   5458:   for ( t = intred; t; t = NEXT(t) ) {
                   5459:     ti = BDY((LIST)BDY(t));
1.6       noro     5460:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   5461:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     5462:       if ( DP_Print ) {
                   5463:            fprintf(asir_out,"*"); fflush(asir_out);
                   5464:       }
                   5465:   }
                   5466:     for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
                   5467:         NEXTNODE(r0,r);
1.6       noro     5468:     b = pb[ZTOS((Q)BDY(t))];
1.1       noro     5469:         ndv_mul_c(m,b,invm(HCM(b),m));
                   5470: #if 0
1.6       noro     5471:         BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1       noro     5472: #else
1.6       noro     5473:         BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1       noro     5474: #endif
                   5475:     }
                   5476:     if ( r0 ) NEXT(r) = 0;
                   5477:     MKLIST(*rp,r0);
                   5478:     if ( DP_Print ) fprintf(asir_out,"\n");
                   5479: }
                   5480:
1.16      noro     5481: 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     5482: {
                   5483:     VL tv,fv,vv,vc,av;
                   5484:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   5485:     int m,nocheck,nvar,mindex,e,max;
                   5486:     NDV c;
                   5487:     NMV a;
                   5488:     P p,zp;
                   5489:     Q dmy;
                   5490:     EPOS oepos;
                   5491:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   5492:     Alg alpha,dp;
                   5493:     P poly;
                   5494:     LIST f1,f2,zpl;
                   5495:     Obj obj;
                   5496:     NumberField nf;
                   5497:     struct order_spec *ord1;
                   5498:     struct oEGT eg_check,eg0,eg1;
                   5499:     NODE tr,tl1,tl2,tl3,tl4;
                   5500:     LIST l1,l2,l3,l4,l5;
                   5501:     int *perm;
                   5502:     int j,ret;
                   5503:     Z jq,bpe;
1.15      noro     5504:     VECT hvect;
1.1       noro     5505:
1.41      noro     5506:     NcriB = NcriMF = Ncri2 = 0;
1.1       noro     5507:     nd_module = 0;
                   5508:     nd_lf = 0;
1.46      noro     5509:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   5510:     parse_nd_option(vv,current_option);
1.1       noro     5511:     if ( nd_lf ) {
                   5512:       if ( f4 )
                   5513:         nd_f4_lf_trace(f,v,trace,homo,ord,rp);
                   5514:       else
                   5515:         error("nd_gr_trace is not implemented yet over a large finite field");
                   5516:       return;
                   5517:     }
                   5518:     if ( DP_Multiple )
                   5519:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   5520:
                   5521:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5522:     switch ( ord->id ) {
                   5523:         case 1:
                   5524:             if ( ord->nv != nvar )
                   5525:                 error("nd_gr_trace : invalid order specification");
                   5526:             break;
                   5527:         default:
                   5528:             break;
                   5529:     }
                   5530:
                   5531:     get_algtree((Obj)f,&av);
                   5532:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   5533:     nd_ntrans = nvar;
                   5534:     nd_nalg = nalg;
                   5535:     /* #i -> t#i */
                   5536:     if ( nalg ) {
                   5537:         preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   5538:         ord = ord1;
                   5539:         f = f1;
                   5540:     }
                   5541:     nvar += nalg;
                   5542:
                   5543:     nocheck = 0;
                   5544:     mindex = 0;
                   5545:
                   5546:     if ( Demand ) nd_demand = 1;
                   5547:     else nd_demand = 0;
                   5548:
                   5549:     /* setup modulus */
                   5550:     if ( trace < 0 ) {
                   5551:         trace = -trace;
                   5552:         nocheck = 1;
                   5553:     }
                   5554:     m = trace > 1 ? trace : get_lprime(mindex);
                   5555:     nd_init_ord(ord);
                   5556:     mrank = 0;
                   5557:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   5558:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   5559:             if ( nd_module ) {
1.16      noro     5560:               if ( OID(BDY(t)) == O_DPM ) {
                   5561:                 e = dpm_getdeg((DPM)BDY(t),&trank);
                   5562:                 max = MAX(e,max);
                   5563:                 mrank = MAX(mrank,trank);
                   5564:               } else {
1.1       noro     5565:                 s = BDY((LIST)BDY(t));
                   5566:                 trank = length(s);
                   5567:                 mrank = MAX(mrank,trank);
                   5568:                 for ( ; s; s = NEXT(s) ) {
                   5569:                     e = getdeg(tv->v,(P)BDY(s));
                   5570:                     max = MAX(e,max);
                   5571:                 }
1.16      noro     5572:               }
1.1       noro     5573:             } else {
1.43      noro     5574:               if ( OID(BDY(t)) == O_DP ) {
                   5575:                 e = dp_getdeg((DP)BDY(t));
                   5576:                 max = MAX(e,max);
                   5577:               } else {
1.1       noro     5578:                 e = getdeg(tv->v,(P)BDY(t));
                   5579:                 max = MAX(e,max);
1.43      noro     5580:               }
1.1       noro     5581:             }
                   5582:         }
                   5583:     nd_setup_parameters(nvar,max);
                   5584:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   5585:     ishomo = 1;
                   5586:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   5587:         if ( nd_module ) {
1.16      noro     5588:           if ( OID(BDY(t)) == O_DPM ) {
                   5589:             Z cont;
                   5590:             DPM zdpm;
                   5591:
1.17      noro     5592:             if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16      noro     5593:             else zdpm = (DPM)BDY(t);
                   5594:             c = (pointer)dpmtondv(m,zdpm);
                   5595:           } else {
                   5596:             if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   5597:             else zpl = (LIST)BDY(t);
1.1       noro     5598:             c = (pointer)pltondv(CO,vv,zpl);
1.16      noro     5599:           }
1.1       noro     5600:         } else {
1.43      noro     5601:           if ( OID(BDY(t)) == O_DP ) {
                   5602:             DP zdp;
                   5603:
                   5604:             if ( !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
                   5605:             else zdp = (DP)BDY(t);
                   5606:             c = (pointer)dptondv(m,zdp);
                   5607:           } else {
                   5608:             if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   5609:             else zp = (P)BDY(t);
                   5610:             c = (pointer)ptondv(CO,vv,zp);
                   5611:           }
1.1       noro     5612:         }
                   5613:         if ( ishomo )
                   5614:             ishomo = ishomo && ndv_ishomo(c);
                   5615:         if ( c ) {
                   5616:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   5617:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   5618:         }
                   5619:     }
                   5620:     if ( in0 ) NEXT(in) = 0;
                   5621:     if ( fd0 ) NEXT(fd) = 0;
                   5622:     if ( !ishomo && homo ) {
                   5623:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   5624:             c = (NDV)BDY(t); len = LEN(c);
                   5625:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   5626:                 wmax = MAX(TD(DL(a)),wmax);
                   5627:         }
                   5628:         homogenize_order(ord,nvar,&ord1);
                   5629:         nd_init_ord(ord1);
                   5630:         nd_setup_parameters(nvar+1,wmax);
                   5631:         for ( t = fd0; t; t = NEXT(t) )
                   5632:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   5633:     }
                   5634:     if ( MaxDeg > 0 ) nocheck = 1;
                   5635:     while ( 1 ) {
                   5636:     tl1 = tl2 = tl3 = tl4 = 0;
                   5637:         if ( Demand )
                   5638:             nd_demand = 1;
1.24      noro     5639:         ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1       noro     5640:         if ( nd_gentrace ) {
                   5641:             MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   5642:         }
                   5643:         if ( ret )
                   5644:             cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
                   5645:         if ( !ret || !cand ) {
                   5646:             /* failure */
                   5647:             if ( trace > 1 ) { *rp = 0; return; }
                   5648:             else m = get_lprime(++mindex);
                   5649:             continue;
                   5650:         }
1.15      noro     5651:         if ( nd_gentrace ) {
                   5652:           MKVECT(hvect,nd_psn);
                   5653:           for ( i = 0; i < nd_psn; i++ )
                   5654:              ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   5655:         }
1.1       noro     5656:         if ( !ishomo && homo ) {
                   5657:             /* dehomogenization */
                   5658:             for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   5659:             nd_init_ord(ord);
                   5660:             nd_setup_parameters(nvar,0);
                   5661:         }
                   5662:         nd_demand = 0;
                   5663:         cand = ndv_reducebase(cand,perm);
                   5664:         if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
                   5665:         cand = ndv_reduceall(0,cand);
                   5666:         cbpe = nd_bpe;
                   5667:         if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
                   5668:         get_eg(&eg0);
                   5669:         if ( nocheck )
                   5670:             break;
                   5671:         if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   5672:             if ( nd_gentrace ) {
                   5673:           tl3 = nd_alltracelist; nd_alltracelist = 0;
                   5674:         } else tl3 = 0;
                   5675:             /* gbcheck : cand is a GB of Id(cand) ? */
1.54      noro     5676:             if ( nd_vc || nd_gentrace || nd_gensyz || do_weyl )
1.1       noro     5677:               ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
                   5678:             else
                   5679:               ret = nd_f4(0,1,0)!=0;
                   5680:             if ( nd_gentrace && nd_gensyz ) {
                   5681:           tl4 = nd_alltracelist; nd_alltracelist = 0;
                   5682:         } else tl4 = 0;
                   5683:     }
                   5684:     if ( ret ) break;
                   5685:         else if ( trace > 1 ) {
                   5686:             /* failure */
                   5687:             *rp = 0; return;
                   5688:         } else {
                   5689:             /* try the next modulus */
                   5690:             m = get_lprime(++mindex);
                   5691:             /* reset the parameters */
                   5692:             if ( !ishomo && homo ) {
                   5693:                 nd_init_ord(ord1);
                   5694:                 nd_setup_parameters(nvar+1,wmax);
                   5695:             } else {
                   5696:                 nd_init_ord(ord);
                   5697:                 nd_setup_parameters(nvar,max);
                   5698:             }
                   5699:         }
                   5700:     }
                   5701:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   5702:     if ( DP_Print )
1.6       noro     5703:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     5704:     /* dp->p */
                   5705:     nd_bpe = cbpe;
                   5706:     nd_setup_parameters(nd_nvar,0);
                   5707:     for ( r = cand; r; r = NEXT(r) ) {
1.16      noro     5708:       if ( nd_module ) {
1.17      noro     5709:         if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16      noro     5710:         else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17      noro     5711:       } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
                   5712:       else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1       noro     5713:     }
                   5714:     if ( nd_nalg )
                   5715:         cand = postprocess_algcoef(av,alist,cand);
                   5716:     MKLIST(*rp,cand);
                   5717:     if ( nd_gentrace ) {
                   5718:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   5719:     tl3 = reverse_node(tl3);
                   5720:     /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   5721:         for ( t = tl2; t; t = NEXT(t) ) {
                   5722:       /* s = [i,[*,j,*,*],...] */
                   5723:             s = BDY((LIST)BDY(t));
1.6       noro     5724:             j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     5725:       for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     5726:                 j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     5727:         ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   5728:             }
                   5729:     }
                   5730:     for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6       noro     5731:         STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     5732:     }
                   5733:         MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   5734:     MKLIST(l5,tl4);
1.6       noro     5735:       STOZ(nd_bpe,bpe);
1.15      noro     5736:         tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     5737:     }
                   5738: }
                   5739:
                   5740: /* XXX : module element is not considered  */
                   5741:
                   5742: void dltondl(int n,DL dl,UINT *r)
                   5743: {
                   5744:     UINT *d;
                   5745:     int i,j,l,s,ord_l;
                   5746:     struct order_pair *op;
                   5747:
                   5748:     d = (unsigned int *)dl->d;
                   5749:     for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
                   5750:     if ( nd_blockmask ) {
                   5751:         l = nd_blockmask->n;
                   5752:         op = nd_blockmask->order_pair;
                   5753:         for ( j = 0, s = 0; j < l; j++ ) {
                   5754:             ord_l = op[j].length;
                   5755:             for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
                   5756:         }
                   5757:         TD(r) = ndl_weight(r);
                   5758:         ndl_weight_mask(r);
                   5759:     } else {
                   5760:         for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
                   5761:         TD(r) = ndl_weight(r);
                   5762:     }
                   5763: }
                   5764:
                   5765: DL ndltodl(int n,UINT *ndl)
                   5766: {
                   5767:     DL dl;
                   5768:     int *d;
                   5769:     int i,j,l,s,ord_l;
                   5770:     struct order_pair *op;
                   5771:
1.33      noro     5772:     NEWDL_NOINIT(dl,n);
1.1       noro     5773:     dl->td = TD(ndl);
                   5774:     d = dl->d;
                   5775:     if ( nd_blockmask ) {
                   5776:         l = nd_blockmask->n;
                   5777:         op = nd_blockmask->order_pair;
                   5778:         for ( j = 0, s = 0; j < l; j++ ) {
                   5779:             ord_l = op[j].length;
                   5780:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   5781:         }
                   5782:     } else {
                   5783:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   5784:     }
                   5785:     return dl;
                   5786: }
                   5787:
1.24      noro     5788: void _ndltodl(UINT *ndl,DL dl)
                   5789: {
                   5790:     int *d;
                   5791:     int i,j,l,s,ord_l,n;
                   5792:     struct order_pair *op;
                   5793:
                   5794:     n = nd_nvar;
                   5795:     dl->td = TD(ndl);
                   5796:     d = dl->d;
                   5797:     if ( nd_blockmask ) {
                   5798:         l = nd_blockmask->n;
                   5799:         op = nd_blockmask->order_pair;
                   5800:         for ( j = 0, s = 0; j < l; j++ ) {
                   5801:             ord_l = op[j].length;
                   5802:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   5803:         }
                   5804:     } else {
                   5805:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   5806:     }
                   5807: }
                   5808:
1.1       noro     5809: void nmtodp(int mod,NM m,DP *r)
                   5810: {
                   5811:     DP dp;
                   5812:     MP mr;
                   5813:
                   5814:     NEWMP(mr);
                   5815:     mr->dl = ndltodl(nd_nvar,DL(m));
                   5816:     mr->c = (Obj)ndctop(mod,m->c);
                   5817:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   5818:     *r = dp;
                   5819: }
                   5820:
1.15      noro     5821: void ndltodp(UINT *d,DP *r)
                   5822: {
                   5823:     DP dp;
                   5824:     MP mr;
                   5825:
                   5826:     NEWMP(mr);
                   5827:     mr->dl = ndltodl(nd_nvar,d);
                   5828:     mr->c = (Obj)ONE;
                   5829:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   5830:     *r = dp;
                   5831: }
                   5832:
1.1       noro     5833: void ndl_print(UINT *dl)
                   5834: {
                   5835:     int n;
                   5836:     int i,j,l,ord_l,s,s0;
                   5837:     struct order_pair *op;
                   5838:
                   5839:     n = nd_nvar;
                   5840:     printf("<<");
                   5841:     if ( nd_blockmask ) {
                   5842:         l = nd_blockmask->n;
                   5843:         op = nd_blockmask->order_pair;
                   5844:         for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   5845:             ord_l = op[j].length;
                   5846:             for ( i = 0; i < ord_l; i++, s++ )
                   5847:                 printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
                   5848:         }
                   5849:     } else {
                   5850:         for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
                   5851:     }
                   5852:     printf(">>");
                   5853:     if ( nd_module && MPOS(dl) )
                   5854:         printf("*e%d",MPOS(dl));
                   5855: }
                   5856:
                   5857: void nd_print(ND p)
                   5858: {
                   5859:     NM m;
                   5860:
                   5861:     if ( !p )
                   5862:         printf("0\n");
                   5863:     else {
                   5864:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5865:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     5866:             else printf("+%ld*",CM(m));
1.1       noro     5867:             ndl_print(DL(m));
                   5868:         }
                   5869:         printf("\n");
                   5870:     }
                   5871: }
                   5872:
                   5873: void nd_print_q(ND p)
                   5874: {
                   5875:     NM m;
                   5876:
                   5877:     if ( !p )
                   5878:         printf("0\n");
                   5879:     else {
                   5880:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5881:             printf("+");
1.6       noro     5882:             printexpr(CO,(Obj)CZ(m));
1.1       noro     5883:             printf("*");
                   5884:             ndl_print(DL(m));
                   5885:         }
                   5886:         printf("\n");
                   5887:     }
                   5888: }
                   5889:
                   5890: void ndp_print(ND_pairs d)
                   5891: {
                   5892:     ND_pairs t;
                   5893:
                   5894:     for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
                   5895:     printf("\n");
                   5896: }
                   5897:
                   5898: void nd_removecont(int mod,ND p)
                   5899: {
                   5900:     int i,n;
                   5901:     Z *w;
                   5902:     NM m;
                   5903:     struct oVECT v;
                   5904:
                   5905:     if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
                   5906:     else if ( mod == -2 ) {
                   5907:       Z inv;
                   5908:       divlf(ONE,HCZ(p),&inv);
                   5909:       nd_mul_c_lf(p,inv);
                   5910:     } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
                   5911:     else {
                   5912:         for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   5913:         w = (Z *)MALLOC(n*sizeof(Q));
                   5914:         v.len = n;
                   5915:         v.body = (pointer *)w;
1.6       noro     5916:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5917:         removecont_array((P *)w,n,1);
1.6       noro     5918:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5919:     }
                   5920: }
                   5921:
                   5922: void nd_removecont2(ND p1,ND p2)
                   5923: {
                   5924:     int i,n1,n2,n;
                   5925:     Z *w;
                   5926:     NM m;
                   5927:     struct oVECT v;
                   5928:
                   5929:     n1 = nd_length(p1);
                   5930:     n2 = nd_length(p2);
                   5931:     n = n1+n2;
                   5932:     w = (Z *)MALLOC(n*sizeof(Q));
                   5933:     v.len = n;
                   5934:     v.body = (pointer *)w;
                   5935:     i = 0;
                   5936:     if ( p1 )
1.6       noro     5937:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5938:     if ( p2 )
1.6       noro     5939:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5940:     removecont_array((P *)w,n,1);
                   5941:     i = 0;
                   5942:     if ( p1 )
1.6       noro     5943:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5944:     if ( p2 )
1.6       noro     5945:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5946: }
                   5947:
                   5948: void ndv_removecont(int mod,NDV p)
                   5949: {
                   5950:     int i,len,all_p;
                   5951:     Z *c;
                   5952:     P *w;
                   5953:     Z dvr,t;
                   5954:     P g,cont,tp;
                   5955:     NMV m;
                   5956:
                   5957:     if ( mod == -1 )
                   5958:         ndv_mul_c(mod,p,_invsf(HCM(p)));
                   5959:     else if ( mod == -2 ) {
                   5960:       Z inv;
                   5961:       divlf(ONE,HCZ(p),&inv);
                   5962:       ndv_mul_c_lf(p,inv);
                   5963:     } else if ( mod )
                   5964:         ndv_mul_c(mod,p,invm(HCM(p),mod));
                   5965:     else {
                   5966:         len = p->len;
                   5967:         w = (P *)MALLOC(len*sizeof(P));
                   5968:         c = (Z *)MALLOC(len*sizeof(Q));
                   5969:         for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
                   5970:             ptozp(CP(m),1,(Q *)&c[i],&w[i]);
                   5971:             all_p = all_p && !NUM(w[i]);
                   5972:         }
                   5973:         if ( all_p ) {
                   5974:             qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
                   5975:             mulp(nd_vc,(P)dvr,g,&cont);
                   5976:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5977:                 divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
                   5978:             }
                   5979:         } else {
                   5980:             sortbynm((Q *)c,len);
                   5981:             qltozl((Q *)c,len,&dvr);
                   5982:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5983:                 divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
                   5984:             }
                   5985:         }
                   5986:     }
                   5987: }
                   5988:
                   5989: /* koko */
                   5990:
                   5991: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
                   5992: {
                   5993:     int len,i,max;
                   5994:     NMV m,mr0,mr,t;
                   5995:
                   5996:     len = p->len;
1.14      noro     5997:     for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1       noro     5998:         max = MAX(max,TD(DL(m)));
                   5999:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   6000:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   6001:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   6002:     t = (NMV)MALLOC(nmv_adv);
                   6003:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   6004:         ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6       noro     6005:         CZ(mr) = CZ(m);
1.1       noro     6006:         ndl_copy(DL(t),DL(mr));
                   6007:     }
                   6008:     NV(p)++;
                   6009:     BDY(p) = mr0;
                   6010: }
                   6011:
                   6012: void ndv_dehomogenize(NDV p,struct order_spec *ord)
                   6013: {
                   6014:     int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
                   6015:     int pos;
                   6016:     Q *w;
                   6017:     Q dvr,t;
                   6018:     NMV m,r;
                   6019:
                   6020:     len = p->len;
                   6021:     newnvar = nd_nvar-1;
                   6022:     newexporigin = nd_get_exporigin(ord);
                   6023:     if ( nd_module ) newmpos = newexporigin-1;
                   6024:     newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
                   6025:     for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
                   6026:         ndl_dehomogenize(DL(m));
                   6027:     if ( newwpd != nd_wpd ) {
                   6028:         newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
                   6029:         for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6       noro     6030:             CZ(r) = CZ(m);
1.1       noro     6031:             if ( nd_module ) pos = MPOS(DL(m));
                   6032:             for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   6033:             adj = nd_exporigin-newexporigin;
                   6034:             for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
                   6035:             if ( nd_module ) {
                   6036:                 DL(r)[newmpos] = pos;
                   6037:             }
                   6038:         }
                   6039:     }
                   6040:     NV(p)--;
                   6041: }
                   6042:
                   6043: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
                   6044: {
                   6045:     int i;
                   6046:     P *tpl,*tpl1;
                   6047:     NODE l;
                   6048:     P h,gcd,t;
                   6049:
                   6050:     tpl = (P *)MALLOC(m*sizeof(P));
                   6051:     tpl1 = (P *)MALLOC(m*sizeof(P));
                   6052:     bcopy(pl,tpl,m*sizeof(P));
                   6053:     gcd = (P)ONE;
                   6054:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   6055:         h = (P)BDY(l);
                   6056:         while ( 1 ) {
                   6057:             for ( i = 0; i < m; i++ )
                   6058:                 if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
                   6059:                     break;
                   6060:             if ( i == m ) {
                   6061:                 bcopy(tpl1,tpl,m*sizeof(P));
                   6062:                 mulp(vl,gcd,h,&t); gcd = t;
                   6063:             } else
                   6064:                 break;
                   6065:         }
                   6066:     }
                   6067:     if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
                   6068:     if ( full ) {
                   6069:         heu_nezgcdnpz(vl,tpl,m,&t);
                   6070:         mulp(vl,gcd,t,pr);
                   6071:     } else
                   6072:         *pr = gcd;
                   6073: }
                   6074:
                   6075: void removecont_array(P *p,int n,int full)
                   6076: {
                   6077:     int all_p,all_q,i;
                   6078:     Z *c;
                   6079:     P *w;
                   6080:     P t,s;
                   6081:
                   6082:     for ( all_q = 1, i = 0; i < n; i++ )
                   6083:         all_q = all_q && NUM(p[i]);
                   6084:     if ( all_q ) {
                   6085:         removecont_array_q((Z *)p,n);
                   6086:     } else {
                   6087:         c = (Z *)MALLOC(n*sizeof(Z));
                   6088:         w = (P *)MALLOC(n*sizeof(P));
                   6089:         for ( i = 0; i < n; i++ ) {
                   6090:             ptozp(p[i],1,(Q *)&c[i],&w[i]);
                   6091:         }
                   6092:         removecont_array_q(c,n);
                   6093:         nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
                   6094:         for ( i = 0; i < n; i++ ) {
                   6095:             divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
                   6096:         }
                   6097:     }
                   6098: }
                   6099:
                   6100: /* c is an int array */
                   6101:
                   6102: void removecont_array_q(Z *c,int n)
                   6103: {
                   6104:   struct oVECT v;
                   6105:   Z d0,d1,a,u,u1,gcd;
                   6106:   int i,j;
                   6107:   Z *q,*r;
                   6108:
                   6109:   q = (Z *)MALLOC(n*sizeof(Z));
                   6110:   r = (Z *)MALLOC(n*sizeof(Z));
                   6111:   v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   6112:   gcdvz_estimate(&v,&d0);
                   6113:   for ( i = 0; i < n; i++ ) {
                   6114:     divqrz(c[i],d0,&q[i],&r[i]);
                   6115:   }
                   6116:   for ( i = 0; i < n; i++ ) if ( r[i] ) break;
                   6117:   if ( i < n ) {
                   6118:     v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   6119:     gcdvz(&v,&d1);
                   6120:     gcdz(d0,d1,&gcd);
1.6       noro     6121:     /* exact division */
                   6122:     divsz(d0,gcd,&a);
1.1       noro     6123:     for ( i = 0; i < n; i++ ) {
                   6124:       mulz(a,q[i],&u);
                   6125:       if ( r[i] ) {
1.6       noro     6126:         /* exact division */
                   6127:         divsz(r[i],gcd,&u1);
1.1       noro     6128:         addz(u,u1,&q[i]);
                   6129:       } else
                   6130:         q[i] = u;
                   6131:     }
                   6132:   }
                   6133:   for ( i = 0; i < n; i++ ) c[i] = q[i];
                   6134: }
                   6135:
1.4       noro     6136: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
                   6137:
                   6138: void mpz_removecont_array(mpz_t *c,int n)
                   6139: {
                   6140:   mpz_t d0,a,u,u1,gcd;
                   6141:   int i,j;
1.13      noro     6142:   static mpz_t *q,*r;
                   6143:   static int c_len = 0;
1.4       noro     6144:
                   6145:   for ( i = 0; i < n; i++ )
                   6146:     if ( mpz_sgn(c[i]) ) break;
                   6147:   if ( i == n ) return;
                   6148:   gcdv_mpz_estimate(d0,c,n);
1.13      noro     6149:   if ( n > c_len ) {
                   6150:     q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   6151:     r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   6152:     c_len = n;
                   6153:   }
1.4       noro     6154:   for ( i = 0; i < n; i++ ) {
                   6155:     mpz_init(q[i]); mpz_init(r[i]);
                   6156:     mpz_fdiv_qr(q[i],r[i],c[i],d0);
                   6157:   }
                   6158:   for ( i = 0; i < n; i++ )
                   6159:     if ( mpz_sgn(r[i]) ) break;
                   6160:   mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
                   6161:   if ( i < n ) {
                   6162:     mpz_gcd(gcd,d0,r[i]);
                   6163:     for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
                   6164:     mpz_div(a,d0,gcd);
                   6165:     for ( i = 0; i < n; i++ ) {
                   6166:       mpz_mul(u,a,q[i]);
                   6167:       if ( mpz_sgn(r[i]) ) {
                   6168:         mpz_div(u1,r[i],gcd);
                   6169:         mpz_add(q[i],u,u1);
                   6170:       } else
                   6171:         mpz_set(q[i],u);
                   6172:     }
                   6173:   }
                   6174:   for ( i = 0; i < n; i++ )
                   6175:     mpz_set(c[i],q[i]);
                   6176: }
                   6177:
1.1       noro     6178: void nd_mul_c(int mod,ND p,int mul)
                   6179: {
                   6180:     NM m;
                   6181:     int c,c1;
                   6182:
                   6183:     if ( !p ) return;
                   6184:     if ( mul == 1 ) return;
                   6185:     if ( mod == -1 )
                   6186:         for ( m = BDY(p); m; m = NEXT(m) )
                   6187:             CM(m) = _mulsf(CM(m),mul);
                   6188:     else
                   6189:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   6190:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   6191:         }
                   6192: }
                   6193:
                   6194: void nd_mul_c_lf(ND p,Z mul)
                   6195: {
                   6196:     NM m;
                   6197:     Z c;
                   6198:
                   6199:     if ( !p ) return;
                   6200:     if ( UNIZ(mul) ) return;
                   6201:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   6202:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   6203:     }
                   6204: }
                   6205:
                   6206: void nd_mul_c_q(ND p,P mul)
                   6207: {
                   6208:     NM m;
                   6209:     P c;
                   6210:
                   6211:     if ( !p ) return;
                   6212:     if ( UNIQ(mul) ) return;
                   6213:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   6214:         mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
                   6215:     }
                   6216: }
                   6217:
                   6218: void nd_mul_c_p(VL vl,ND p,P mul)
                   6219: {
                   6220:     NM m;
                   6221:     P c;
                   6222:
                   6223:     if ( !p ) return;
                   6224:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   6225:         mulp(vl,CP(m),mul,&c); CP(m) = c;
                   6226:     }
                   6227: }
                   6228:
                   6229: void nd_free(ND p)
                   6230: {
                   6231:     NM t,s;
                   6232:
                   6233:     if ( !p ) return;
                   6234:     t = BDY(p);
                   6235:     while ( t ) {
                   6236:         s = NEXT(t);
                   6237:         FREENM(t);
                   6238:         t = s;
                   6239:     }
                   6240:     FREEND(p);
                   6241: }
                   6242:
                   6243: void ndv_free(NDV p)
                   6244: {
                   6245:     GCFREE(BDY(p));
                   6246: }
                   6247:
                   6248: void nd_append_red(UINT *d,int i)
                   6249: {
                   6250:     RHist m,m0;
                   6251:     int h;
                   6252:
                   6253:     NEWRHist(m);
                   6254:     h = ndl_hash_value(d);
                   6255:     m->index = i;
                   6256:     ndl_copy(d,DL(m));
                   6257:     NEXT(m) = nd_red[h];
                   6258:     nd_red[h] = m;
                   6259: }
                   6260:
                   6261: UINT *ndv_compute_bound(NDV p)
                   6262: {
                   6263:     UINT *d1,*d2,*t;
                   6264:     UINT u;
                   6265:     int i,j,k,l,len,ind;
                   6266:     NMV m;
                   6267:
                   6268:     if ( !p )
                   6269:         return 0;
                   6270:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6271:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6272:     len = LEN(p);
                   6273:     m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   6274:     for ( i = 1; i < len; i++, NMV_ADV(m) ) {
                   6275:         ndl_max(DL(m),d1,d2);
                   6276:         t = d1; d1 = d2; d2 = t;
                   6277:     }
                   6278:     l = nd_nvar+31;
                   6279:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   6280:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   6281:         u = d1[i];
                   6282:         k = (nd_epw-1)*nd_bpe;
                   6283:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   6284:             t[ind] = (u>>k)&nd_mask0;
                   6285:     }
                   6286:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   6287:     return t;
                   6288: }
                   6289:
                   6290: UINT *nd_compute_bound(ND p)
                   6291: {
                   6292:     UINT *d1,*d2,*t;
                   6293:     UINT u;
                   6294:     int i,j,k,l,len,ind;
                   6295:     NM m;
                   6296:
                   6297:     if ( !p )
                   6298:         return 0;
                   6299:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6300:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6301:     len = LEN(p);
                   6302:     m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   6303:     for ( m = NEXT(m); m; m = NEXT(m) ) {
                   6304:         ndl_lcm(DL(m),d1,d2);
                   6305:         t = d1; d1 = d2; d2 = t;
                   6306:     }
                   6307:     l = nd_nvar+31;
                   6308:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   6309:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   6310:         u = d1[i];
                   6311:         k = (nd_epw-1)*nd_bpe;
                   6312:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   6313:             t[ind] = (u>>k)&nd_mask0;
                   6314:     }
                   6315:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   6316:     return t;
                   6317: }
                   6318:
                   6319: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
                   6320: /* of a term. In this case we need additional 1 word. */
                   6321:
                   6322: int nd_get_exporigin(struct order_spec *ord)
                   6323: {
                   6324:     switch ( ord->id ) {
1.21      noro     6325:         case 0: case 2: case 256: case 258: case 300:
1.1       noro     6326:             return 1+nd_module;
                   6327:         case 1: case 257:
                   6328:             /* block order */
                   6329:             /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
                   6330:             /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
                   6331:             return ord->ord.block.length+1+nd_module;
                   6332:         case 3: case 259:
                   6333: #if 0
                   6334:             error("nd_get_exporigin : composite order is not supported yet.");
                   6335: #else
                   6336:             return 1+nd_module;
                   6337: #endif
                   6338:         default:
                   6339:             error("nd_get_exporigin : ivalid argument.");
                   6340:             return 0;
                   6341:     }
                   6342: }
                   6343:
                   6344: void nd_setup_parameters(int nvar,int max) {
                   6345:     int i,j,n,elen,ord_o,ord_l,l,s,wpd;
                   6346:     struct order_pair *op;
1.45      noro     6347:     extern int CNVars;
1.1       noro     6348:
                   6349:     nd_nvar = nvar;
1.45      noro     6350:     CNVars = nvar;
1.1       noro     6351:     if ( max ) {
                   6352:         /* XXX */
                   6353:         if ( do_weyl ) nd_bpe = 32;
                   6354:         else if ( max < 2 ) nd_bpe = 1;
                   6355:         else if ( max < 4 ) nd_bpe = 2;
                   6356:         else if ( max < 8 ) nd_bpe = 3;
                   6357:         else if ( max < 16 ) nd_bpe = 4;
                   6358:         else if ( max < 32 ) nd_bpe = 5;
                   6359:         else if ( max < 64 ) nd_bpe = 6;
                   6360:         else if ( max < 256 ) nd_bpe = 8;
                   6361:         else if ( max < 1024 ) nd_bpe = 10;
                   6362:         else if ( max < 65536 ) nd_bpe = 16;
                   6363:         else nd_bpe = 32;
                   6364:     }
                   6365:     if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
                   6366:         UINT t;
                   6367:     int st;
                   6368:         int *v;
                   6369:   /* t = max(weights) */
                   6370:         t = 0;
                   6371:         if ( current_dl_weight_vector )
                   6372:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                   6373:                 if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
                   6374:                 if ( t < st ) t = st;
                   6375:             }
                   6376:         if ( nd_matrix )
                   6377:             for ( i = 0; i < nd_matrix_len; i++ )
                   6378:                 for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
                   6379:                     if ( (st=v[j]) < 0 ) st = -st;
                   6380:                     if ( t < st ) t = st;
                   6381:         }
                   6382:         /* i = bitsize of t */
                   6383:         for ( i = 0; t; t >>=1, i++ );
                   6384:         /* i += bitsize of nd_nvar */
                   6385:         for ( t = nd_nvar; t; t >>=1, i++);
                   6386:         /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
                   6387:         if ( (nd_bpe+i) >= 31 )
                   6388:             error("nd_setup_parameters : too large weight");
                   6389:     }
                   6390:     nd_epw = (sizeof(UINT)*8)/nd_bpe;
                   6391:     elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   6392:     nd_exporigin = nd_get_exporigin(nd_ord);
                   6393:     wpd = nd_exporigin+elen;
                   6394:     if ( nd_module )
                   6395:         nd_mpos = nd_exporigin-1;
                   6396:     else
                   6397:         nd_mpos = -1;
                   6398:     if ( wpd != nd_wpd ) {
                   6399:         nd_free_private_storage();
                   6400:         nd_wpd = wpd;
                   6401:     }
                   6402:     if ( nd_bpe < 32 ) {
                   6403:         nd_mask0 = (1<<nd_bpe)-1;
                   6404:     } else {
                   6405:         nd_mask0 = 0xffffffff;
                   6406:     }
                   6407:     bzero(nd_mask,sizeof(nd_mask));
                   6408:     nd_mask1 = 0;
                   6409:     for ( i = 0; i < nd_epw; i++ ) {
                   6410:         nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   6411:         nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   6412:     }
                   6413:     nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
                   6414:     nd_epos = nd_create_epos(nd_ord);
                   6415:     nd_blockmask = nd_create_blockmask(nd_ord);
                   6416:     nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
                   6417: }
                   6418:
                   6419: ND_pairs nd_reconstruct(int trace,ND_pairs d)
                   6420: {
                   6421:     int i,obpe,oadv,h;
                   6422:     static NM prev_nm_free_list;
                   6423:     static ND_pairs prev_ndp_free_list;
                   6424:     RHist mr0,mr;
                   6425:     RHist r;
                   6426:     RHist *old_red;
                   6427:     ND_pairs s0,s,t;
                   6428:     EPOS oepos;
                   6429:
                   6430:     obpe = nd_bpe;
                   6431:     oadv = nmv_adv;
                   6432:     oepos = nd_epos;
                   6433:     if ( obpe < 2 ) nd_bpe = 2;
                   6434:     else if ( obpe < 3 ) nd_bpe = 3;
                   6435:     else if ( obpe < 4 ) nd_bpe = 4;
                   6436:     else if ( obpe < 5 ) nd_bpe = 5;
                   6437:     else if ( obpe < 6 ) nd_bpe = 6;
                   6438:     else if ( obpe < 8 ) nd_bpe = 8;
                   6439:     else if ( obpe < 10 ) nd_bpe = 10;
                   6440:     else if ( obpe < 16 ) nd_bpe = 16;
                   6441:     else if ( obpe < 32 ) nd_bpe = 32;
                   6442:     else error("nd_reconstruct : exponent too large");
                   6443:
                   6444:     nd_setup_parameters(nd_nvar,0);
                   6445:     prev_nm_free_list = _nm_free_list;
                   6446:     prev_ndp_free_list = _ndp_free_list;
                   6447:     _nm_free_list = 0;
                   6448:     _ndp_free_list = 0;
                   6449:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   6450:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   6451:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   6452:     }
                   6453:     if ( trace )
                   6454:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   6455:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   6456:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   6457:         }
                   6458:     s0 = 0;
                   6459:     for ( t = d; t; t = NEXT(t) ) {
                   6460:         NEXTND_pairs(s0,s);
                   6461:         s->i1 = t->i1;
                   6462:         s->i2 = t->i2;
1.24      noro     6463:         s->sig = t->sig;
1.1       noro     6464:         SG(s) = SG(t);
                   6465:         ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   6466:     }
                   6467:
                   6468:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   6469:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   6470:         old_red[i] = nd_red[i];
                   6471:         nd_red[i] = 0;
                   6472:     }
                   6473:     for ( i = 0; i < REDTAB_LEN; i++ )
                   6474:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   6475:             NEWRHist(mr);
                   6476:             mr->index = r->index;
                   6477:             SG(mr) = SG(r);
                   6478:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   6479:             h = ndl_hash_value(DL(mr));
                   6480:             NEXT(mr) = nd_red[h];
                   6481:             nd_red[h] = mr;
1.24      noro     6482:             mr->sig = r->sig;
1.1       noro     6483:         }
                   6484:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   6485:     old_red = 0;
                   6486:     for ( i = 0; i < nd_psn; i++ ) {
                   6487:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   6488:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24      noro     6489:         r->sig = nd_psh[i]->sig;
1.1       noro     6490:         nd_psh[i] = r;
                   6491:     }
                   6492:     if ( s0 ) NEXT(s) = 0;
                   6493:     prev_nm_free_list = 0;
                   6494:     prev_ndp_free_list = 0;
                   6495: #if 0
                   6496:     GC_gcollect();
                   6497: #endif
                   6498:     return s0;
                   6499: }
                   6500:
1.30      noro     6501: void nd_reconstruct_s(int trace,ND_pairs *d)
                   6502: {
                   6503:     int i,obpe,oadv,h;
                   6504:     static NM prev_nm_free_list;
                   6505:     static ND_pairs prev_ndp_free_list;
                   6506:     RHist mr0,mr;
                   6507:     RHist r;
                   6508:     RHist *old_red;
                   6509:     ND_pairs s0,s,t;
                   6510:     EPOS oepos;
                   6511:
                   6512:     obpe = nd_bpe;
                   6513:     oadv = nmv_adv;
                   6514:     oepos = nd_epos;
                   6515:     if ( obpe < 2 ) nd_bpe = 2;
                   6516:     else if ( obpe < 3 ) nd_bpe = 3;
                   6517:     else if ( obpe < 4 ) nd_bpe = 4;
                   6518:     else if ( obpe < 5 ) nd_bpe = 5;
                   6519:     else if ( obpe < 6 ) nd_bpe = 6;
                   6520:     else if ( obpe < 8 ) nd_bpe = 8;
                   6521:     else if ( obpe < 10 ) nd_bpe = 10;
                   6522:     else if ( obpe < 16 ) nd_bpe = 16;
                   6523:     else if ( obpe < 32 ) nd_bpe = 32;
                   6524:     else error("nd_reconstruct_s : exponent too large");
                   6525:
                   6526:     nd_setup_parameters(nd_nvar,0);
                   6527:     prev_nm_free_list = _nm_free_list;
                   6528:     prev_ndp_free_list = _ndp_free_list;
                   6529:     _nm_free_list = 0;
                   6530:     _ndp_free_list = 0;
                   6531:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   6532:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   6533:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   6534:     }
                   6535:     if ( trace )
                   6536:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   6537:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   6538:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   6539:         }
                   6540:
                   6541:     for ( i = 0; i < nd_nbase; i++ ) {
                   6542:       s0 = 0;
                   6543:       for ( t = d[i]; t; t = NEXT(t) ) {
                   6544:           NEXTND_pairs(s0,s);
                   6545:           s->i1 = t->i1;
                   6546:           s->i2 = t->i2;
                   6547:           s->sig = t->sig;
                   6548:           SG(s) = SG(t);
                   6549:           ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   6550:       }
1.47      noro     6551:       if ( s0 ) NEXT(s) = 0;
1.30      noro     6552:       d[i] = s0;
                   6553:     }
                   6554:
                   6555:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   6556:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   6557:         old_red[i] = nd_red[i];
                   6558:         nd_red[i] = 0;
                   6559:     }
                   6560:     for ( i = 0; i < REDTAB_LEN; i++ )
                   6561:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   6562:             NEWRHist(mr);
                   6563:             mr->index = r->index;
                   6564:             SG(mr) = SG(r);
                   6565:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   6566:             h = ndl_hash_value(DL(mr));
                   6567:             NEXT(mr) = nd_red[h];
                   6568:             nd_red[h] = mr;
                   6569:             mr->sig = r->sig;
                   6570:         }
                   6571:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   6572:     old_red = 0;
                   6573:     for ( i = 0; i < nd_psn; i++ ) {
                   6574:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   6575:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
                   6576:         r->sig = nd_psh[i]->sig;
                   6577:         nd_psh[i] = r;
                   6578:     }
                   6579:     if ( s0 ) NEXT(s) = 0;
                   6580:     prev_nm_free_list = 0;
                   6581:     prev_ndp_free_list = 0;
                   6582: #if 0
                   6583:     GC_gcollect();
                   6584: #endif
                   6585: }
                   6586:
1.1       noro     6587: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
                   6588: {
                   6589:     int n,i,ei,oepw,omask0,j,s,ord_l,l;
                   6590:     struct order_pair *op;
                   6591:
                   6592:     n = nd_nvar;
                   6593:     oepw = (sizeof(UINT)*8)/obpe;
                   6594:     omask0 = (1<<obpe)-1;
                   6595:     TD(r) = TD(d);
                   6596:     for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
                   6597:     if ( nd_blockmask ) {
                   6598:         l = nd_blockmask->n;
                   6599:         op = nd_blockmask->order_pair;
                   6600:         for ( i = 1; i < nd_exporigin; i++ )
                   6601:             r[i] = d[i];
                   6602:         for ( j = 0, s = 0; j < l; j++ ) {
                   6603:             ord_l = op[j].length;
                   6604:             for ( i = 0; i < ord_l; i++, s++ ) {
                   6605:                 ei =  GET_EXP_OLD(d,s);
                   6606:                 PUT_EXP(r,s,ei);
                   6607:             }
                   6608:         }
                   6609:     } else {
                   6610:         for ( i = 0; i < n; i++ ) {
                   6611:             ei = GET_EXP_OLD(d,i);
                   6612:             PUT_EXP(r,i,ei);
                   6613:         }
                   6614:     }
                   6615:     if ( nd_module ) MPOS(r) = MPOS(d);
                   6616: }
                   6617:
                   6618: ND nd_copy(ND p)
                   6619: {
                   6620:     NM m,mr,mr0;
                   6621:     int c,n;
                   6622:     ND r;
                   6623:
                   6624:     if ( !p )
                   6625:         return 0;
                   6626:     else {
                   6627:         for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   6628:             NEXTNM(mr0,mr);
                   6629:             CM(mr) = CM(m);
                   6630:             ndl_copy(DL(m),DL(mr));
                   6631:         }
                   6632:         NEXT(mr) = 0;
                   6633:         MKND(NV(p),mr0,LEN(p),r);
                   6634:         SG(r) = SG(p);
                   6635:         return r;
                   6636:     }
                   6637: }
                   6638:
                   6639: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
                   6640: {
                   6641:     NM m1,m2;
                   6642:     NDV p1,p2;
                   6643:     ND t1,t2;
                   6644:     UINT *lcm;
                   6645:     P gp,tp;
                   6646:     Z g,t;
                   6647:     Z iq;
                   6648:     int td;
                   6649:     LIST hist;
                   6650:     NODE node;
                   6651:     DP d;
                   6652:
                   6653:     if ( !mod && nd_demand ) {
                   6654:         p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
                   6655:     } else {
                   6656:         if ( trace ) {
                   6657:             p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   6658:         } else {
                   6659:             p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   6660:         }
                   6661:     }
                   6662:     lcm = LCM(p);
                   6663:     NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
                   6664:     if ( ndl_check_bound2(p->i1,DL(m1)) ) {
                   6665:         FREENM(m1); return 0;
                   6666:     }
                   6667:     NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
                   6668:     if ( ndl_check_bound2(p->i2,DL(m2)) ) {
                   6669:         FREENM(m1); FREENM(m2); return 0;
                   6670:     }
                   6671:
                   6672:     if ( mod == -1 ) {
                   6673:         CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
                   6674:     } else if ( mod > 0 ) {
                   6675:         CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
                   6676:     } else if ( mod == -2 ) {
                   6677:         CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
                   6678:     } else if ( nd_vc ) {
                   6679:         ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
                   6680:         divsp(nd_vc,HCP(p2),gp,&CP(m1));
                   6681:         divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
                   6682:     } else {
1.6       noro     6683:         igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1       noro     6684:     }
                   6685:     t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
                   6686:     *rp = nd_add(mod,t1,t2);
                   6687:     if ( nd_gentrace ) {
                   6688:         /* nd_tracelist is initialized */
1.6       noro     6689:         STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     6690:         MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6       noro     6691:         STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     6692:         MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
                   6693:         nd_tracelist = node;
                   6694:     }
1.24      noro     6695:     if ( *rp )
                   6696:       (*rp)->sig = p->sig;
1.1       noro     6697:     FREENM(m1); FREENM(m2);
                   6698:     return 1;
                   6699: }
                   6700:
                   6701: void ndv_mul_c(int mod,NDV p,int mul)
                   6702: {
                   6703:     NMV m;
                   6704:     int c,c1,len,i;
                   6705:
                   6706:     if ( !p ) return;
                   6707:     len = LEN(p);
                   6708:     if ( mod == -1 )
                   6709:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   6710:             CM(m) = _mulsf(CM(m),mul);
                   6711:     else
                   6712:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6713:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   6714:         }
                   6715: }
                   6716:
                   6717: void ndv_mul_c_lf(NDV p,Z mul)
                   6718: {
                   6719:     NMV m;
                   6720:     Z c;
                   6721:     int len,i;
                   6722:
                   6723:     if ( !p ) return;
                   6724:     len = LEN(p);
                   6725:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6726:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   6727:     }
                   6728: }
                   6729:
                   6730: /* for nd_det */
                   6731: void ndv_mul_c_q(NDV p,Z mul)
                   6732: {
                   6733:     NMV m;
                   6734:     Z c;
                   6735:     int len,i;
                   6736:
                   6737:     if ( !p ) return;
                   6738:     len = LEN(p);
                   6739:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     6740:         mulz(CZ(m),mul,&c); CZ(m) = c;
1.1       noro     6741:     }
                   6742: }
                   6743:
                   6744: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
                   6745:     int n2,i,j,l,n,tlen;
                   6746:     UINT *d0;
                   6747:     NM *tab,*psum;
                   6748:     ND s,r;
                   6749:     NM t;
                   6750:     NMV m1;
                   6751:
                   6752:     if ( !p ) return 0;
                   6753:     n = NV(p); n2 = n>>1;
                   6754:     d0 = DL(m0);
                   6755:     l = LEN(p);
                   6756:     for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   6757:     tab = (NM *)MALLOC(tlen*sizeof(NM));
                   6758:     psum = (NM *)MALLOC(tlen*sizeof(NM));
                   6759:     for ( i = 0; i < tlen; i++ ) psum[i] = 0;
                   6760:     m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   6761:     for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
                   6762:         /* m0(NM) * m1(NMV) => tab(NM) */
                   6763:         weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
                   6764:         for ( j = 0; j < tlen; j++ ) {
                   6765:             if ( tab[j] ) {
                   6766:                 NEXT(tab[j]) = psum[j];    psum[j] = tab[j];
                   6767:             }
                   6768:         }
                   6769:     }
                   6770:     for ( i = tlen-1, r = 0; i >= 0; i-- )
                   6771:         if ( psum[i] ) {
                   6772:             for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   6773:             MKND(n,psum[i],j,s);
                   6774:             r = nd_add(mod,r,s);
                   6775:         }
                   6776:     if ( r ) SG(r) = SG(p)+TD(d0);
                   6777:     return r;
                   6778: }
                   6779:
                   6780: /* product of monomials */
                   6781: /* XXX block order is not handled correctly */
                   6782:
                   6783: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   6784: {
                   6785:     int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
                   6786:     UINT *d0,*d1,*d,*dt,*ctab;
                   6787:     Z *ctab_q;
                   6788:     Z q,q1;
                   6789:     UINT c0,c1,c;
                   6790:     NM *p;
                   6791:     NM m,t;
                   6792:     int mpos;
                   6793:
                   6794:     for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   6795:     if ( !m0 || !m1 ) return;
                   6796:     d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
                   6797:     if ( nd_module )
                   6798:         if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
                   6799:
                   6800:     NEWNM(m); d = DL(m);
                   6801:     if ( mod ) {
                   6802:         c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
                   6803:     } else if ( nd_vc )
                   6804:         mulp(nd_vc,CP(m0),CP(m1),&CP(m));
                   6805:   else
1.6       noro     6806:         mulz(CZ(m0),CZ(m1),&CZ(m));
1.1       noro     6807:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   6808:     homo = n&1 ? 1 : 0;
                   6809:     if ( homo ) {
                   6810:         /* offset of h-degree */
                   6811:         h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   6812:         PUT_EXP(DL(m),n-1,h);
                   6813:         TD(DL(m)) = h;
                   6814:         if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   6815:     }
                   6816:     tab[0] = m;
                   6817:     NEWNM(m); d = DL(m);
                   6818:     for ( i = 0, curlen = 1; i < n2; i++ ) {
                   6819:         a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   6820:         k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   6821:         /* xi^a*(Di^k*xi^l)*Di^b */
                   6822:         a += l; b += k;
                   6823:         s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
                   6824:         if ( !k || !l ) {
                   6825:             for ( j = 0; j < curlen; j++ )
                   6826:                 if ( (t = tab[j]) != 0 ) {
                   6827:                     dt = DL(t);
                   6828:                     PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
                   6829:                     if ( nd_blockmask ) ndl_weight_mask(dt);
                   6830:                 }
                   6831:             curlen *= k+1;
                   6832:             continue;
                   6833:         }
                   6834:         min = MIN(k,l);
                   6835:         if ( mod ) {
                   6836:             ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
                   6837:             mkwcm(k,l,mod,(int *)ctab);
                   6838:         } else {
                   6839:             ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
                   6840:             mkwc(k,l,ctab_q);
                   6841:         }
                   6842:         for ( j = min; j >= 0; j-- ) {
                   6843:             for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
                   6844:             PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
                   6845:             h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
                   6846:             if ( homo ) {
                   6847:                 TD(d) = s;
                   6848:                 PUT_EXP(d,n-1,s-h);
                   6849:             } else TD(d) = h;
                   6850:             if ( nd_blockmask ) ndl_weight_mask(d);
                   6851:             if ( mod ) c = ctab[j];
                   6852:             else q = ctab_q[j];
                   6853:             p = tab+curlen*j;
                   6854:             if ( j == 0 ) {
                   6855:                 for ( u = 0; u < curlen; u++, p++ ) {
                   6856:                     if ( tab[u] ) {
                   6857:                         ndl_addto(DL(tab[u]),d);
                   6858:                         if ( mod ) {
                   6859:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
                   6860:                         } else if ( nd_vc )
                   6861:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
                   6862:             else {
1.6       noro     6863:                             mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1       noro     6864:                         }
                   6865:                     }
                   6866:                 }
                   6867:             } else {
                   6868:                 for ( u = 0; u < curlen; u++, p++ ) {
                   6869:                     if ( tab[u] ) {
                   6870:                         NEWNM(t);
                   6871:                         ndl_add(DL(tab[u]),d,DL(t));
                   6872:                         if ( mod ) {
                   6873:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
                   6874:                         } else if ( nd_vc )
                   6875:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
                   6876:             else
1.6       noro     6877:                             mulz(CZ(tab[u]),q,&CZ(t));
1.1       noro     6878:                         *p = t;
                   6879:                     }
                   6880:                 }
                   6881:             }
                   6882:         }
                   6883:         curlen *= k+1;
                   6884:     }
                   6885:     FREENM(m);
                   6886:     if ( nd_module ) {
                   6887:         mpos = MPOS(d1);
                   6888:         for ( i = 0; i < tlen; i++ )
                   6889:             if ( tab[i] ) {
                   6890:                 d = DL(tab[i]);
                   6891:                 MPOS(d) = mpos;
                   6892:                 TD(d) = ndl_weight(d);
                   6893:             }
                   6894:     }
                   6895: }
                   6896:
                   6897: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   6898: {
                   6899:     NM mr,mr0;
                   6900:     NMV m;
                   6901:     UINT *d,*dt,*dm;
                   6902:     int c,n,td,i,c1,c2,len;
                   6903:     Q q;
                   6904:     ND r;
                   6905:
                   6906:     if ( !p ) return 0;
                   6907:     else {
                   6908:         n = NV(p); m = BDY(p);
                   6909:         d = DL(m0);
                   6910:         len = LEN(p);
                   6911:         mr0 = 0;
                   6912:         td = TD(d);
                   6913:         c = CM(m0);
                   6914:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6915:             NEXTNM(mr0,mr);
                   6916:             CM(mr) = 1;
                   6917:             ndl_add(DL(m),d,DL(mr));
                   6918:         }
                   6919:         NEXT(mr) = 0;
                   6920:         MKND(NV(p),mr0,len,r);
                   6921:         SG(r) = SG(p) + TD(d);
                   6922:         return r;
                   6923:     }
                   6924: }
                   6925:
                   6926: ND ndv_mul_nm(int mod,NM m0,NDV p)
                   6927: {
                   6928:     NM mr,mr0;
                   6929:     NMV m;
                   6930:     UINT *d,*dt,*dm;
                   6931:     int c,n,td,i,c1,c2,len;
                   6932:     P q;
                   6933:     ND r;
                   6934:
                   6935:     if ( !p ) return 0;
                   6936:     else if ( do_weyl ) {
                   6937:         if ( mod < 0 ) {
                   6938:             error("ndv_mul_nm : not implemented (weyl)");
                   6939:             return 0;
                   6940:         } else
                   6941:             return weyl_ndv_mul_nm(mod,m0,p);
                   6942:     } else {
                   6943:         n = NV(p); m = BDY(p);
                   6944:         d = DL(m0);
                   6945:         len = LEN(p);
                   6946:         mr0 = 0;
                   6947:         td = TD(d);
                   6948:         if ( mod == -1 ) {
                   6949:             c = CM(m0);
                   6950:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6951:                 NEXTNM(mr0,mr);
                   6952:                 CM(mr) = _mulsf(CM(m),c);
                   6953:                 ndl_add(DL(m),d,DL(mr));
                   6954:             }
                   6955:         } else if ( mod == -2 ) {
                   6956:             Z cl;
                   6957:             cl = CZ(m0);
                   6958:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6959:                 NEXTNM(mr0,mr);
                   6960:                 mullf(CZ(m),cl,&CZ(mr));
                   6961:                 ndl_add(DL(m),d,DL(mr));
                   6962:             }
                   6963:         } else if ( mod ) {
                   6964:             c = CM(m0);
                   6965:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6966:                 NEXTNM(mr0,mr);
                   6967:                 c1 = CM(m);
                   6968:                 DMAR(c1,c,0,mod,c2);
                   6969:                 CM(mr) = c2;
                   6970:                 ndl_add(DL(m),d,DL(mr));
                   6971:             }
                   6972:         } else {
                   6973:             q = CP(m0);
                   6974:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6975:                 NEXTNM(mr0,mr);
                   6976:                 mulp(nd_vc,CP(m),q,&CP(mr));
                   6977:                 ndl_add(DL(m),d,DL(mr));
                   6978:             }
                   6979:         }
                   6980:         NEXT(mr) = 0;
                   6981:         MKND(NV(p),mr0,len,r);
                   6982:         SG(r) = SG(p) + TD(d);
                   6983:         return r;
                   6984:     }
                   6985: }
                   6986:
                   6987: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
                   6988: {
                   6989:     NM mq0,mq;
                   6990:     NMV tm;
                   6991:     Q q;
                   6992:     int i,nv,sg,c,c1,c2,hindex;
                   6993:     ND p,t,r;
                   6994:
                   6995:     if ( bucket->m < 0 ) return 0;
                   6996:     else {
                   6997:         nv = NV(d);
                   6998:         mq0 = 0;
                   6999:         tm = (NMV)MALLOC(nmv_adv);
                   7000:         while ( 1 ) {
                   7001:             if ( mod > 0 || mod == -1 )
                   7002:               hindex = head_pbucket(mod,bucket);
                   7003:             else if ( mod == -2 )
                   7004:               hindex = head_pbucket_lf(bucket);
                   7005:             else
                   7006:               hindex = head_pbucket_q(bucket);
                   7007:             if ( hindex < 0 ) break;
                   7008:             p = bucket->body[hindex];
                   7009:             NEXTNM(mq0,mq);
                   7010:             ndl_sub(HDL(p),HDL(d),DL(mq));
                   7011:             ndl_copy(DL(mq),DL(tm));
                   7012:             if ( mod ) {
                   7013:                 c1 = invm(HCM(d),mod); c2 = HCM(p);
                   7014:                 DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   7015:                 CM(tm) = mod-c;
                   7016:             } else {
1.6       noro     7017:                 divsz(HCZ(p),HCZ(d),&CZ(mq));
                   7018:                 chsgnz(CZ(mq),&CZ(tm));
1.1       noro     7019:             }
                   7020:             t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
                   7021:             bucket->body[hindex] = nd_remove_head(p);
                   7022:             t = nd_remove_head(t);
                   7023:             add_pbucket(mod,bucket,t);
                   7024:         }
                   7025:         if ( !mq0 )
                   7026:             r = 0;
                   7027:         else {
                   7028:             NEXT(mq) = 0;
                   7029:             for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   7030:             MKND(nv,mq0,i,r);
                   7031:             /* XXX */
                   7032:             SG(r) = HTD(r);
                   7033:         }
                   7034:         return r;
                   7035:     }
                   7036: }
                   7037:
                   7038: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   7039: {
                   7040:     NMV m,mr,mr0,t;
                   7041:     int len,i,k;
                   7042:
                   7043:     if ( !p ) return;
                   7044:     m = BDY(p); len = LEN(p);
                   7045:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   7046:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   7047:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   7048:     t = (NMV)MALLOC(nmv_adv);
                   7049:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6       noro     7050:         CZ(t) = CZ(m);
1.1       noro     7051:         for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   7052:         ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6       noro     7053:         CZ(mr) = CZ(t);
1.1       noro     7054:         ndl_copy(DL(t),DL(mr));
                   7055:     }
                   7056:     BDY(p) = mr0;
                   7057: }
                   7058:
                   7059: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   7060: {
                   7061:     NMV m,mr,mr0;
                   7062:     int len,i;
                   7063:     NDV r;
                   7064:
                   7065:     if ( !p ) return 0;
                   7066:     m = BDY(p); len = LEN(p);
                   7067:     mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   7068:     for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   7069:         ndl_zero(DL(mr));
                   7070:         ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6       noro     7071:         CZ(mr) = CZ(m);
1.1       noro     7072:     }
                   7073:     MKNDV(NV(p),mr0,len,r);
                   7074:     SG(r) = SG(p);
1.24      noro     7075:     r->sig = p->sig;
1.1       noro     7076:     return r;
                   7077: }
                   7078:
                   7079: /* duplicate p */
                   7080:
                   7081: NDV ndv_dup(int mod,NDV p)
                   7082: {
                   7083:     NDV d;
                   7084:     NMV t,m,m0;
                   7085:     int i,len;
                   7086:
                   7087:     if ( !p ) return 0;
                   7088:     len = LEN(p);
                   7089:     m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   7090:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   7091:         ndl_copy(DL(t),DL(m));
1.6       noro     7092:         CZ(m) = CZ(t);
1.1       noro     7093:     }
                   7094:     MKNDV(NV(p),m0,len,d);
                   7095:     SG(d) = SG(p);
                   7096:     return d;
                   7097: }
                   7098:
                   7099: NDV ndv_symbolic(int mod,NDV p)
                   7100: {
                   7101:     NDV d;
                   7102:     NMV t,m,m0;
                   7103:     int i,len;
                   7104:
                   7105:     if ( !p ) return 0;
                   7106:     len = LEN(p);
                   7107:     m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   7108:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   7109:         ndl_copy(DL(t),DL(m));
1.6       noro     7110:         CZ(m) = ONE;
1.1       noro     7111:     }
                   7112:     MKNDV(NV(p),m0,len,d);
                   7113:     SG(d) = SG(p);
                   7114:     return d;
                   7115: }
                   7116:
                   7117: ND nd_dup(ND p)
                   7118: {
                   7119:     ND d;
                   7120:     NM t,m,m0;
                   7121:
                   7122:     if ( !p ) return 0;
                   7123:     for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   7124:         NEXTNM(m0,m);
                   7125:         ndl_copy(DL(t),DL(m));
1.6       noro     7126:         CZ(m) = CZ(t);
1.1       noro     7127:     }
                   7128:     if ( m0 ) NEXT(m) = 0;
                   7129:     MKND(NV(p),m0,LEN(p),d);
                   7130:     SG(d) = SG(p);
                   7131:     return d;
                   7132: }
                   7133:
                   7134: /* XXX if p->len == 0 then it represents 0 */
                   7135:
                   7136: void ndv_mod(int mod,NDV p)
                   7137: {
                   7138:     NMV t,d;
                   7139:     int r,s,u;
                   7140:     int i,len,dlen;
                   7141:     P cp;
                   7142:     Z c;
                   7143:     Obj gfs;
                   7144:
                   7145:     if ( !p ) return;
                   7146:     len = LEN(p);
                   7147:     dlen = 0;
                   7148:     if ( mod == -1 )
                   7149:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   7150:             simp_ff((Obj)CP(t),&gfs);
                   7151:             if ( gfs ) {
                   7152:               r = FTOIF(CONT((GFS)gfs));
                   7153:               CM(d) = r;
                   7154:               ndl_copy(DL(t),DL(d));
                   7155:               NMV_ADV(d);
                   7156:               dlen++;
                   7157:             }
                   7158:         }
                   7159:     else if ( mod == -2 )
                   7160:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   7161:             simp_ff((Obj)CP(t),&gfs);
                   7162:             if ( gfs ) {
                   7163:               lmtolf((LM)gfs,&CZ(d));
                   7164:               ndl_copy(DL(t),DL(d));
                   7165:               NMV_ADV(d);
                   7166:               dlen++;
                   7167:             }
                   7168:         }
                   7169:     else
                   7170:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   7171:             if ( nd_vc ) {
                   7172:                 nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
                   7173:                 c = (Z)cp;
                   7174:             } else
1.6       noro     7175:                 c = CZ(t);
1.1       noro     7176:             r = remqi((Q)c,mod);
                   7177:             if ( r ) {
                   7178:                 CM(d) = r;
                   7179:                 ndl_copy(DL(t),DL(d));
                   7180:                 NMV_ADV(d);
                   7181:                 dlen++;
                   7182:             }
                   7183:         }
                   7184:     LEN(p) = dlen;
                   7185: }
                   7186:
                   7187: NDV ptondv(VL vl,VL dvl,P p)
                   7188: {
                   7189:     ND nd;
                   7190:
                   7191:     nd = ptond(vl,dvl,p);
                   7192:     return ndtondv(0,nd);
                   7193: }
                   7194:
                   7195: void pltozpl(LIST l,Q *cont,LIST *pp)
                   7196: {
1.16      noro     7197:   NODE nd,nd1;
                   7198:   int n;
                   7199:   P *pl;
                   7200:   Q *cl;
                   7201:   int i;
                   7202:   P dmy;
                   7203:   Z dvr,inv;
                   7204:   LIST r;
                   7205:
                   7206:   nd = BDY(l); n = length(nd);
                   7207:   pl = (P *)MALLOC(n*sizeof(P));
                   7208:   cl = (Q *)MALLOC(n*sizeof(Q));
                   7209:   for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
                   7210:     ptozp((P)BDY(nd),1,&cl[i],&dmy);
                   7211:   }
                   7212:   qltozl(cl,n,&dvr);
                   7213:   divz(ONE,dvr,&inv);
                   7214:   nd = BDY(l);
                   7215:   for ( i = 0; i < n; i++, nd = NEXT(nd) )
                   7216:     divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
                   7217:   nd = 0;
                   7218:   for ( i = n-1; i >= 0; i-- ) {
                   7219:     MKNODE(nd1,pl[i],nd); nd = nd1;
                   7220:   }
                   7221:   MKLIST(r,nd);
                   7222:   *pp = r;
1.1       noro     7223: }
                   7224:
                   7225: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
                   7226:
                   7227: NDV pltondv(VL vl,VL dvl,LIST p)
                   7228: {
                   7229:     int i;
                   7230:     NODE t;
                   7231:     ND r,ri;
                   7232:     NM m;
                   7233:
                   7234:     if ( !nd_module ) error("pltond : module order must be set");
                   7235:     r = 0;
                   7236:     for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
                   7237:         ri = ptond(vl,dvl,(P)BDY(t));
                   7238:         if ( ri )
                   7239:             for ( m = BDY(ri); m; m = NEXT(m) ) {
                   7240:                 MPOS(DL(m)) = i;
                   7241:                 TD(DL(m)) = ndl_weight(DL(m));
                   7242:                 if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   7243:             }
                   7244:         r = nd_add(0,r,ri);
                   7245:     }
                   7246:     return ndtondv(0,r);
                   7247: }
                   7248:
                   7249: ND ptond(VL vl,VL dvl,P p)
                   7250: {
                   7251:     int n,i,j,k,e;
                   7252:     VL tvl;
                   7253:     V v;
                   7254:     DCP dc;
                   7255:     DCP *w;
                   7256:     ND r,s,t,u;
                   7257:     P x;
                   7258:     int c;
                   7259:     UINT *d;
                   7260:     NM m,m0;
                   7261:
                   7262:     if ( !p )
                   7263:         return 0;
                   7264:     else if ( NUM(p) ) {
                   7265:         NEWNM(m);
                   7266:         ndl_zero(DL(m));
1.48      noro     7267:         if ( RATN(p) && !INT((Q)p) )
1.1       noro     7268:           error("ptond : input must be integer-coefficient");
1.6       noro     7269:         CZ(m) = (Z)p;
1.1       noro     7270:         NEXT(m) = 0;
                   7271:         MKND(nd_nvar,m,1,r);
                   7272:         SG(r) = 0;
                   7273:         return r;
                   7274:     } else {
                   7275:         for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   7276:         w = (DCP *)MALLOC(k*sizeof(DCP));
                   7277:         for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   7278:         for ( i = 0, tvl = dvl, v = VR(p);
                   7279:             tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   7280:         if ( !tvl ) {
                   7281:             for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   7282:                 t = ptond(vl,dvl,COEF(w[j]));
                   7283:                 pwrp(vl,x,DEG(w[j]),&p);
                   7284:                 nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   7285:             }
                   7286:             return s;
                   7287:         } else {
                   7288:             NEWNM(m0); d = DL(m0);
                   7289:             for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6       noro     7290:                 ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1       noro     7291:                 TD(d) = MUL_WEIGHT(e,i);
                   7292:                 if ( nd_blockmask) ndl_weight_mask(d);
                   7293:                 if ( nd_module ) MPOS(d) = 0;
                   7294:                 t = ptond(vl,dvl,COEF(w[j]));
                   7295:                 for ( m = BDY(t); m; m = NEXT(m) )
                   7296:                     ndl_addto(DL(m),d);
                   7297:                 SG(t) += TD(d);
                   7298:                 s = nd_add(0,s,t);
                   7299:             }
                   7300:             FREENM(m0);
                   7301:             return s;
                   7302:         }
                   7303:     }
                   7304: }
                   7305:
                   7306: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   7307: {
                   7308:     VL tvl;
                   7309:     int len,n,j,i,e;
                   7310:     NMV m;
                   7311:     Z q;
                   7312:     P c;
                   7313:     UINT *d;
                   7314:     P s,r,u,t,w;
                   7315:     GFS gfs;
                   7316:
                   7317:     if ( !p ) return 0;
                   7318:     else {
                   7319:         len = LEN(p);
                   7320:         n = NV(p);
                   7321:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   7322:         for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   7323:             if ( mod == -1 ) {
                   7324:                e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   7325:             } else if ( mod == -2 ) {
                   7326:                c = (P)CZ(m);
                   7327:             } else if ( mod > 0 ) {
1.6       noro     7328:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     7329:             } else
                   7330:                 c = CP(m);
                   7331:             d = DL(m);
                   7332:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     7333:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     7334:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   7335:             }
                   7336:             addp(vl,s,t,&u); s = u;
                   7337:         }
                   7338:         return s;
                   7339:     }
                   7340: }
                   7341:
                   7342: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
                   7343: {
                   7344:     VL tvl;
                   7345:     int len,n,j,i,e;
                   7346:     NMV m;
                   7347:     Z q;
                   7348:     P c;
                   7349:     UINT *d;
                   7350:     P s,r,u,t,w;
                   7351:     GFS gfs;
                   7352:     P *a;
                   7353:     LIST l;
                   7354:     NODE nd,nd1;
                   7355:
                   7356:     if ( !p ) return 0;
                   7357:     else {
                   7358:         a = (P *)MALLOC((rank+1)*sizeof(P));
                   7359:         for ( i = 0; i <= rank; i++ ) a[i] = 0;
                   7360:         len = LEN(p);
                   7361:         n = NV(p);
                   7362:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   7363:         for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
                   7364:             if ( mod == -1 ) {
                   7365:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   7366:             } else if ( mod ) {
1.6       noro     7367:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     7368:             } else
                   7369:                 c = CP(m);
                   7370:             d = DL(m);
                   7371:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     7372:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     7373:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   7374:             }
                   7375:             addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
                   7376:         }
                   7377:         nd = 0;
                   7378:         for ( i = rank; i > 0; i-- ) {
                   7379:             MKNODE(nd1,a[i],nd); nd = nd1;
                   7380:         }
                   7381:         MKLIST(l,nd);
                   7382:         return l;
                   7383:     }
                   7384: }
                   7385:
                   7386: NDV ndtondv(int mod,ND p)
                   7387: {
                   7388:     NDV d;
                   7389:     NMV m,m0;
                   7390:     NM t;
                   7391:     int i,len;
                   7392:
                   7393:     if ( !p ) return 0;
                   7394:     len = LEN(p);
                   7395:     if ( mod > 0 || mod == -1 )
                   7396:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   7397:     else
                   7398:         m0 = m = MALLOC(len*nmv_adv);
                   7399: #if 0
                   7400:     ndv_alloc += nmv_adv*len;
                   7401: #endif
                   7402:     for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   7403:         ndl_copy(DL(t),DL(m));
1.6       noro     7404:         CZ(m) = CZ(t);
1.1       noro     7405:     }
                   7406:     MKNDV(NV(p),m0,len,d);
                   7407:     SG(d) = SG(p);
1.24      noro     7408:     d->sig = p->sig;
1.1       noro     7409:     return d;
                   7410: }
                   7411:
1.16      noro     7412: static int dmm_comp_nv;
                   7413:
                   7414: int dmm_comp(DMM *a,DMM *b)
                   7415: {
                   7416:    return -compdmm(dmm_comp_nv,*a,*b);
                   7417: }
                   7418:
                   7419: void dmm_sort_by_ord(DMM *a,int len,int nv)
                   7420: {
                   7421:   dmm_comp_nv = nv;
                   7422:   qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
                   7423: }
                   7424:
                   7425: void dpm_sort(DPM p,DPM *rp)
                   7426: {
                   7427:   DMM t,t1;
                   7428:   int len,i,n;
                   7429:   DMM *a;
                   7430:   DPM d;
                   7431:
                   7432:   if ( !p ) *rp = 0;
                   7433:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   7434:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   7435:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   7436:   n = p->nv;
                   7437:   dmm_sort_by_ord(a,len,n);
                   7438:   t = 0;
                   7439:   for ( i = len-1; i >= 0; i-- ) {
                   7440:     NEWDMM(t1);
                   7441:     t1->c = a[i]->c;
                   7442:     t1->dl = a[i]->dl;
                   7443:     t1->pos = a[i]->pos;
                   7444:     t1->next = t;
                   7445:     t = t1;
                   7446:   }
                   7447:   MKDPM(n,t,d);
                   7448:   SG(d) = SG(p);
                   7449:   *rp = d;
                   7450: }
                   7451:
1.18      noro     7452: int dpm_comp(DPM *a,DPM *b)
                   7453: {
1.22      noro     7454:   return -compdpm(CO,*a,*b);
1.18      noro     7455: }
                   7456:
                   7457: NODE dpm_sort_list(NODE l)
                   7458: {
                   7459:   int i,len;
                   7460:   NODE t,t1;
                   7461:   DPM *a;
                   7462:
                   7463:   len = length(l);
                   7464:   a = (DPM *)MALLOC(len*sizeof(DPM));
                   7465:   for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
                   7466:   qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
                   7467:   t = 0;
                   7468:   for ( i = len-1; i >= 0; i-- ) {
                   7469:     MKNODE(t1,(pointer)a[i],t); t = t1;
                   7470:   }
                   7471:   return t;
                   7472: }
                   7473:
1.20      noro     7474: int nmv_comp(NMV a,NMV b)
                   7475: {
1.21      noro     7476:   int t;
                   7477:   t = DL_COMPARE(a->dl,b->dl);
                   7478:   return -t;
1.20      noro     7479: }
                   7480:
1.43      noro     7481: NDV dptondv(int mod,DP p)
                   7482: {
                   7483:   NDV d;
                   7484:   NMV m,m0;
                   7485:   MP t;
                   7486:   MP *a;
                   7487:   int i,len,n;
                   7488:
                   7489:   if ( !p ) return 0;
                   7490:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   7491:   n = p->nv;
                   7492:   if ( mod > 0 || mod == -1 )
                   7493:     m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   7494:   else
                   7495:     m0 = m = MALLOC(len*nmv_adv);
                   7496:   for ( i = 0, t = BDY(p); i < len; i++, NMV_ADV(m), t = NEXT(t) ) {
                   7497:     dltondl(n,t->dl,DL(m));
                   7498:     TD(DL(m)) = ndl_weight(DL(m));
                   7499:     CZ(m) = (Z)t->c;
                   7500:   }
                   7501:   qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
                   7502:   MKNDV(NV(p),m0,len,d);
                   7503:   SG(d) = SG(p);
                   7504:   return d;
                   7505: }
                   7506:
1.16      noro     7507: NDV dpmtondv(int mod,DPM p)
                   7508: {
                   7509:   NDV d;
                   7510:   NMV m,m0;
                   7511:   DMM t;
                   7512:   DMM *a;
                   7513:   int i,len,n;
                   7514:
                   7515:   if ( !p ) return 0;
                   7516:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   7517:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   7518:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   7519:   n = p->nv;
                   7520:   dmm_sort_by_ord(a,len,n);
                   7521:   if ( mod > 0 || mod == -1 )
                   7522:     m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   7523:   else
                   7524:     m0 = m = MALLOC(len*nmv_adv);
                   7525: #if 0
                   7526:   ndv_alloc += nmv_adv*len;
                   7527: #endif
                   7528:   for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   7529:     dltondl(n,a[i]->dl,DL(m));
                   7530:     MPOS(DL(m)) = a[i]->pos;
1.20      noro     7531:     TD(DL(m)) = ndl_weight(DL(m));
1.16      noro     7532:     CZ(m) = (Z)a[i]->c;
                   7533:   }
1.20      noro     7534:   qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16      noro     7535:   MKNDV(NV(p),m0,len,d);
                   7536:   SG(d) = SG(p);
                   7537:   return d;
                   7538: }
                   7539:
1.1       noro     7540: ND ndvtond(int mod,NDV p)
                   7541: {
                   7542:     ND d;
                   7543:     NM m,m0;
                   7544:     NMV t;
                   7545:     int i,len;
                   7546:
                   7547:     if ( !p ) return 0;
                   7548:     m0 = 0;
                   7549:     len = p->len;
                   7550:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   7551:         NEXTNM(m0,m);
                   7552:         ndl_copy(DL(t),DL(m));
1.6       noro     7553:         CZ(m) = CZ(t);
1.1       noro     7554:     }
                   7555:     NEXT(m) = 0;
                   7556:     MKND(NV(p),m0,len,d);
                   7557:     SG(d) = SG(p);
1.28      noro     7558:     d->sig = p->sig;
1.1       noro     7559:     return d;
                   7560: }
                   7561:
                   7562: DP ndvtodp(int mod,NDV p)
                   7563: {
                   7564:     MP m,m0;
                   7565:   DP d;
                   7566:     NMV t;
                   7567:     int i,len;
                   7568:
                   7569:     if ( !p ) return 0;
                   7570:     m0 = 0;
                   7571:     len = p->len;
                   7572:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   7573:         NEXTMP(m0,m);
                   7574:       m->dl = ndltodl(nd_nvar,DL(t));
                   7575:       m->c = (Obj)ndctop(mod,t->c);
                   7576:     }
                   7577:     NEXT(m) = 0;
                   7578:   MKDP(nd_nvar,m0,d);
                   7579:     SG(d) = SG(p);
                   7580:     return d;
                   7581: }
                   7582:
1.44      noro     7583: DPM sigtodpm(SIG s)
                   7584: {
                   7585:   DMM m;
                   7586:   DPM d;
                   7587:
                   7588:   NEWDMM(m);
                   7589:   m->c = (Obj)ONE;
                   7590:   m->dl = s->dl;
                   7591:   m->pos = s->pos+1;
                   7592:   m->next = 0;
                   7593:   MKDPM(nd_nvar,m,d);
                   7594:   SG(d) = s->dl->td;
                   7595:   return d;
                   7596: }
                   7597:
1.16      noro     7598: DPM ndvtodpm(int mod,NDV p)
                   7599: {
                   7600:   DMM m,m0;
                   7601:   DPM d;
                   7602:   NMV t;
                   7603:   int i,len;
                   7604:
                   7605:   if ( !p ) return 0;
                   7606:   m0 = 0;
                   7607:   len = p->len;
                   7608:   for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   7609:     NEXTDMM(m0,m);
                   7610:     m->dl = ndltodl(nd_nvar,DL(t));
                   7611:     m->c = (Obj)ndctop(mod,t->c);
                   7612:     m->pos = MPOS(DL(t));
                   7613:   }
                   7614:   NEXT(m) = 0;
                   7615:   MKDPM(nd_nvar,m0,d);
                   7616:   SG(d) = SG(p);
                   7617:   return d;
                   7618: }
                   7619:
                   7620:
1.1       noro     7621: DP ndtodp(int mod,ND p)
                   7622: {
                   7623:     MP m,m0;
                   7624:   DP d;
                   7625:     NM t;
                   7626:     int i,len;
                   7627:
                   7628:     if ( !p ) return 0;
                   7629:     m0 = 0;
                   7630:     len = p->len;
                   7631:     for ( t = BDY(p); t; t = NEXT(t) ) {
                   7632:         NEXTMP(m0,m);
                   7633:       m->dl = ndltodl(nd_nvar,DL(t));
                   7634:       m->c = (Obj)ndctop(mod,t->c);
                   7635:     }
                   7636:     NEXT(m) = 0;
                   7637:   MKDP(nd_nvar,m0,d);
                   7638:     SG(d) = SG(p);
                   7639:     return d;
                   7640: }
                   7641:
                   7642: void ndv_print(NDV p)
                   7643: {
                   7644:     NMV m;
                   7645:     int i,len;
                   7646:
                   7647:     if ( !p ) printf("0\n");
                   7648:     else {
                   7649:         len = LEN(p);
                   7650:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   7651:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     7652:             else printf("+%ld*",CM(m));
1.1       noro     7653:             ndl_print(DL(m));
                   7654:         }
                   7655:         printf("\n");
                   7656:     }
                   7657: }
                   7658:
                   7659: void ndv_print_q(NDV p)
                   7660: {
                   7661:     NMV m;
                   7662:     int i,len;
                   7663:
                   7664:     if ( !p ) printf("0\n");
                   7665:     else {
                   7666:         len = LEN(p);
                   7667:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   7668:             printf("+");
1.6       noro     7669:             printexpr(CO,(Obj)CZ(m));
1.1       noro     7670:             printf("*");
                   7671:             ndl_print(DL(m));
                   7672:         }
                   7673:         printf("\n");
                   7674:     }
                   7675: }
                   7676:
                   7677: NODE ndv_reducebase(NODE x,int *perm)
                   7678: {
                   7679:     int len,i,j;
                   7680:     NDVI w;
                   7681:     NODE t,t0;
                   7682:
1.41      noro     7683:     if ( nd_norb ) return x;
1.1       noro     7684:     len = length(x);
                   7685:     w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
                   7686:     for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
                   7687:         w[i].p = BDY(t); w[i].i = perm[i];
                   7688:     }
                   7689:     for ( i = 0; i < len; i++ ) {
                   7690:         for ( j = 0; j < i; j++ ) {
                   7691:             if ( w[i].p && w[j].p ) {
                   7692:                 if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
                   7693:                 else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
                   7694:             }
                   7695:         }
                   7696:     }
                   7697:     for ( i = j = 0, t0 = 0; i < len; i++ ) {
                   7698:         if ( w[i].p ) {
                   7699:             NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
                   7700:             perm[j++] = w[i].i;
                   7701:         }
                   7702:     }
                   7703:     NEXT(t) = 0; x = t0;
                   7704:     return x;
                   7705: }
                   7706:
                   7707: /* XXX incomplete */
                   7708:
1.21      noro     7709: extern DMMstack dmm_stack;
                   7710: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16      noro     7711:
1.1       noro     7712: void nd_init_ord(struct order_spec *ord)
                   7713: {
                   7714:   nd_module = (ord->id >= 256);
                   7715:   if ( nd_module ) {
                   7716:     nd_dcomp = -1;
1.21      noro     7717:     nd_module_ordtype = ord->module_ordtype;
1.1       noro     7718:     nd_pot_nelim = ord->pot_nelim;
                   7719:     nd_poly_weight_len = ord->nv;
                   7720:     nd_poly_weight = ord->top_weight;
                   7721:     nd_module_rank = ord->module_rank;
                   7722:     nd_module_weight = ord->module_top_weight;
                   7723:   }
                   7724:   nd_matrix = 0;
                   7725:   nd_matrix_len = 0;
                   7726:     switch ( ord->id ) {
                   7727:         case 0:
                   7728:             switch ( ord->ord.simple ) {
                   7729:                 case 0:
                   7730:                     nd_dcomp = 1;
                   7731:                     nd_isrlex = 1;
                   7732:                     break;
                   7733:                 case 1:
                   7734:                     nd_dcomp = 1;
                   7735:                     nd_isrlex = 0;
                   7736:                     break;
                   7737:                 case 2:
                   7738:                     nd_dcomp = 0;
                   7739:                     nd_isrlex = 0;
                   7740:                     ndl_compare_function = ndl_lex_compare;
                   7741:                     break;
                   7742:                 case 11:
                   7743:                     /* XXX */
                   7744:                     nd_dcomp = 0;
                   7745:                     nd_isrlex = 1;
                   7746:                     ndl_compare_function = ndl_ww_lex_compare;
                   7747:                     break;
                   7748:                 default:
                   7749:                     error("nd_gr : unsupported order");
                   7750:             }
                   7751:             break;
                   7752:         case 1:
                   7753:             /* block order */
                   7754:             /* XXX */
                   7755:             nd_dcomp = -1;
                   7756:             nd_isrlex = 0;
                   7757:             ndl_compare_function = ndl_block_compare;
                   7758:             break;
                   7759:         case 2:
                   7760:             /* matrix order */
                   7761:             /* XXX */
                   7762:             nd_dcomp = -1;
                   7763:             nd_isrlex = 0;
                   7764:             nd_matrix_len = ord->ord.matrix.row;
                   7765:             nd_matrix = ord->ord.matrix.matrix;
                   7766:             ndl_compare_function = ndl_matrix_compare;
                   7767:             break;
                   7768:         case 3:
                   7769:             /* composite order */
                   7770:             nd_dcomp = -1;
                   7771:             nd_isrlex = 0;
                   7772:             nd_worb_len = ord->ord.composite.length;
                   7773:             nd_worb = ord->ord.composite.w_or_b;
                   7774:             ndl_compare_function = ndl_composite_compare;
                   7775:             break;
                   7776:
                   7777:         /* module order */
                   7778:         case 256:
                   7779:             switch ( ord->ord.simple ) {
                   7780:                 case 0:
1.21      noro     7781:                     nd_dcomp = 0;
1.1       noro     7782:                     nd_isrlex = 1;
1.21      noro     7783:                     ndl_compare_function = ndl_module_glex_compare;
1.1       noro     7784:                     break;
                   7785:                 case 1:
1.21      noro     7786:                     nd_dcomp = 0;
1.1       noro     7787:                     nd_isrlex = 0;
                   7788:                     ndl_compare_function = ndl_module_glex_compare;
                   7789:                     break;
                   7790:                 case 2:
1.21      noro     7791:                     nd_dcomp = 0;
1.1       noro     7792:                     nd_isrlex = 0;
1.21      noro     7793:                     ndl_compare_function = ndl_module_compare;
                   7794:                     ndl_base_compare_function = ndl_lex_compare;
1.1       noro     7795:                     break;
                   7796:                 default:
1.21      noro     7797:                     error("nd_init_ord : unsupported order");
1.1       noro     7798:             }
                   7799:             break;
                   7800:         case 257:
                   7801:             /* block order */
                   7802:             nd_isrlex = 0;
1.21      noro     7803:             ndl_compare_function = ndl_module_compare;
                   7804:             ndl_base_compare_function = ndl_block_compare;
1.1       noro     7805:             break;
                   7806:         case 258:
                   7807:             /* matrix order */
                   7808:             nd_isrlex = 0;
                   7809:             nd_matrix_len = ord->ord.matrix.row;
                   7810:             nd_matrix = ord->ord.matrix.matrix;
1.21      noro     7811:             ndl_compare_function = ndl_module_compare;
                   7812:             ndl_base_compare_function = ndl_matrix_compare;
1.1       noro     7813:             break;
                   7814:         case 259:
                   7815:             /* composite order */
                   7816:             nd_isrlex = 0;
                   7817:             nd_worb_len = ord->ord.composite.length;
                   7818:             nd_worb = ord->ord.composite.w_or_b;
1.21      noro     7819:             ndl_compare_function = ndl_module_compare;
                   7820:             ndl_base_compare_function = ndl_composite_compare;
                   7821:             break;
                   7822:         case 300:
                   7823:             /* schreyer order */
                   7824:             if ( ord->base->id != 256 )
                   7825:                error("nd_init_ord : unsupported base order");
                   7826:             ndl_compare_function = ndl_module_schreyer_compare;
                   7827:             dmm_stack = ord->dmmstack;
                   7828:             switch ( ord->base->ord.simple ) {
                   7829:                 case 0:
                   7830:                     nd_isrlex = 1;
                   7831:                     ndl_base_compare_function = ndl_glex_compare;
                   7832:                     dl_base_compare_function = cmpdl_revgradlex;
                   7833:                     break;
                   7834:                 case 1:
                   7835:                     nd_isrlex = 0;
                   7836:                     ndl_base_compare_function = ndl_glex_compare;
                   7837:                     dl_base_compare_function = cmpdl_gradlex;
                   7838:                     break;
                   7839:                 case 2:
                   7840:                     nd_isrlex = 0;
                   7841:                     ndl_base_compare_function = ndl_lex_compare;
                   7842:                     dl_base_compare_function = cmpdl_lex;
                   7843:                     break;
                   7844:                 default:
                   7845:                     error("nd_init_ord : unsupported order");
                   7846:             }
1.1       noro     7847:             break;
                   7848:     }
                   7849:     nd_ord = ord;
                   7850: }
                   7851:
                   7852: BlockMask nd_create_blockmask(struct order_spec *ord)
                   7853: {
                   7854:     int n,i,j,s,l;
                   7855:     UINT *t;
                   7856:     BlockMask bm;
                   7857:
                   7858:     /* we only create mask table for block order */
                   7859:     if ( ord->id != 1 && ord->id != 257 )
                   7860:         return 0;
                   7861:     n = ord->ord.block.length;
                   7862:     bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   7863:     bm->n = n;
                   7864:     bm->order_pair = ord->ord.block.order_pair;
                   7865:     bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
                   7866:     for ( i = 0, s = 0; i < n; i++ ) {
                   7867:         bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
                   7868:         for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   7869:         l = bm->order_pair[i].length;
                   7870:         for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   7871:     }
                   7872:     return bm;
                   7873: }
                   7874:
                   7875: EPOS nd_create_epos(struct order_spec *ord)
                   7876: {
                   7877:     int i,j,l,s,ord_l,ord_o;
                   7878:     EPOS epos;
                   7879:     struct order_pair *op;
                   7880:
                   7881:     epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   7882:     switch ( ord->id ) {
1.21      noro     7883:         case 0: case 256: case 300:
1.1       noro     7884:             if ( nd_isrlex ) {
                   7885:                 for ( i = 0; i < nd_nvar; i++ ) {
                   7886:                     epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   7887:                     epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   7888:                 }
                   7889:             } else {
                   7890:                 for ( i = 0; i < nd_nvar; i++ ) {
                   7891:                     epos[i].i = nd_exporigin + i/nd_epw;
                   7892:                     epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   7893:                 }
                   7894:             }
                   7895:             break;
                   7896:         case 1: case 257:
                   7897:             /* block order */
                   7898:             l = ord->ord.block.length;
                   7899:             op = ord->ord.block.order_pair;
                   7900:             for ( j = 0, s = 0; j < l; j++ ) {
                   7901:                 ord_o = op[j].order;
                   7902:                 ord_l = op[j].length;
                   7903:                 if ( !ord_o )
                   7904:                     for ( i = 0; i < ord_l; i++ ) {
                   7905:                         epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   7906:                         epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   7907:                     }
                   7908:                 else
                   7909:                     for ( i = 0; i < ord_l; i++ ) {
                   7910:                         epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   7911:                         epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   7912:                     }
                   7913:                 s += ord_l;
                   7914:             }
                   7915:             break;
                   7916:         case 2:
                   7917:             /* matrix order */
                   7918:         case 3:
                   7919:             /* composite order */
                   7920:         default:
                   7921:             for ( i = 0; i < nd_nvar; i++ ) {
                   7922:                 epos[i].i = nd_exporigin + i/nd_epw;
                   7923:                 epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   7924:             }
                   7925:             break;
                   7926:     }
                   7927:     return epos;
                   7928: }
                   7929:
                   7930: /* external interface */
                   7931:
                   7932: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
                   7933: {
                   7934:     NODE t,in0,in;
                   7935:     ND ndf,nf;
                   7936:     NDV ndvf;
                   7937:     VL vv,tv;
                   7938:     int stat,nvar,max,mrank;
                   7939:     union oNDC dn;
                   7940:     Q cont;
                   7941:     P pp;
                   7942:     LIST ppl;
                   7943:
                   7944:     if ( !f ) {
                   7945:         *rp = 0;
                   7946:         return;
                   7947:     }
                   7948:     pltovl(v,&vv);
                   7949:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   7950:
                   7951:     /* max=65536 implies nd_bpe=32 */
                   7952:     max = 65536;
                   7953:
                   7954:   nd_module = 0;
                   7955:   /* nd_module will be set if ord is a module ordering */
                   7956:     nd_init_ord(ord);
                   7957:     nd_setup_parameters(nvar,max);
                   7958:     if ( nd_module && OID(f) != O_LIST )
                   7959:         error("nd_nf_p : the first argument must be a list");
                   7960:   if ( nd_module ) mrank = length(BDY((LIST)f));
                   7961:     /* conversion to ndv */
                   7962:     for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   7963:         NEXTNODE(in0,in);
                   7964:         if ( nd_module ) {
                   7965:           if ( !BDY(t) || OID(BDY(t)) != O_LIST
                   7966:                || length(BDY((LIST)BDY(t))) != mrank )
                   7967:               error("nd_nf_p : inconsistent basis element");
                   7968:           if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
                   7969:           else ppl = (LIST)BDY(t);
                   7970:           BDY(in) = (pointer)pltondv(CO,vv,ppl);
                   7971:         } else {
                   7972:           if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
                   7973:           else pp = (P)BDY(t);
                   7974:           BDY(in) = (pointer)ptondv(CO,vv,pp);
                   7975:         }
                   7976:         if ( m ) ndv_mod(m,(NDV)BDY(in));
                   7977:     }
                   7978:     if ( in0 ) NEXT(in) = 0;
                   7979:
                   7980:     if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
                   7981:     else ndvf = ptondv(CO,vv,(P)f);
                   7982:     if ( m ) ndv_mod(m,ndvf);
                   7983:     ndf = (pointer)ndvtond(m,ndvf);
                   7984:
                   7985:     /* dont sort, dont removecont */
1.24      noro     7986:     ndv_setup(m,0,in0,1,1,0);
1.1       noro     7987:     nd_scale=2;
1.6       noro     7988:     stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1       noro     7989:     if ( !stat )
                   7990:         error("nd_nf_p : exponent too large");
                   7991:     if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
                   7992:     else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
                   7993: }
                   7994:
                   7995: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   7996: {
                   7997:     NM m;
                   7998:     UINT *t,*s;
                   7999:     int i;
                   8000:
                   8001:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   8002:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   8003:         t = DL(m);
                   8004:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   8005:         r[i] = CM(m);
                   8006:     }
                   8007:     for ( i = 0; !r[i]; i++ );
                   8008:     return i;
                   8009: }
                   8010:
                   8011: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
                   8012: {
                   8013:     NM m;
                   8014:     UINT *t,*s;
                   8015:     int i;
                   8016:
                   8017:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   8018:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   8019:         t = DL(m);
                   8020:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     8021:         r[i] = CZ(m);
1.1       noro     8022:     }
                   8023:     for ( i = 0; !r[i]; i++ );
                   8024:     return i;
                   8025: }
                   8026:
                   8027: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
                   8028: {
                   8029:     NM m;
                   8030:     UINT *t,*s;
                   8031:     int i;
                   8032:
                   8033:     for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
                   8034:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   8035:         t = DL(m);
                   8036:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   8037:         mpz_set(r[i],BDY(CZ(m)));
                   8038:     }
                   8039:     for ( i = 0; !mpz_sgn(r[i]); i++ );
                   8040:     return i;
                   8041: }
                   8042:
                   8043: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
                   8044: {
                   8045:     NM m;
                   8046:     unsigned long *v;
                   8047:     int i,j,h,size;
                   8048:   UINT *s,*t;
                   8049:
                   8050:   size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
                   8051:     v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
                   8052:     bzero(v,size);
                   8053:     for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
                   8054:     t = DL(m);
                   8055:     h = ndl_hash_value(t);
                   8056:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   8057:       v[i/BLEN] |= 1L <<(i%BLEN);
                   8058:     }
                   8059:     return v;
                   8060: }
                   8061:
                   8062: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
                   8063: {
                   8064:     NMV mr;
                   8065:     UINT *d,*t,*s;
                   8066:     int i,j,len,h,head;
                   8067:
                   8068:     d = DL(m);
                   8069:     len = LEN(p);
                   8070:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   8071:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   8072:         ndl_add(d,DL(mr),t);
                   8073:     h = ndl_hash_value(t);
                   8074:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   8075:     if ( j == 0 ) head = i;
                   8076:       v[i/BLEN] |= 1L <<(i%BLEN);
                   8077:     }
                   8078:     return head;
                   8079: }
                   8080:
                   8081: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   8082: {
                   8083:     NM m;
                   8084:     NMV mr;
                   8085:     UINT *d,*t,*s;
                   8086:     NDV p;
                   8087:     int i,j,len;
                   8088:     Z *r;
                   8089:
                   8090:     m = pair->mul;
                   8091:     d = DL(m);
                   8092:     p = nd_ps[pair->index];
                   8093:     len = LEN(p);
                   8094:     r = (Z *)CALLOC(n,sizeof(Q));
                   8095:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   8096:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   8097:         ndl_add(d,DL(mr),t);
                   8098:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     8099:         r[i] = CZ(mr);
1.1       noro     8100:     }
                   8101:     return r;
                   8102: }
                   8103:
1.11      noro     8104: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1       noro     8105: {
                   8106:     NM m;
                   8107:     NMV mr;
1.11      noro     8108:     UINT *d,*t,*s,*u;
1.1       noro     8109:     NDV p;
                   8110:     unsigned char *ivc;
                   8111:     unsigned short *ivs;
                   8112:     UINT *v,*ivi,*s0v;
1.11      noro     8113:     int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1       noro     8114:     IndArray r;
                   8115:
                   8116:     m = pair->mul;
                   8117:     d = DL(m);
                   8118:     if ( trace )
                   8119:       p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
                   8120:     else
                   8121:       p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
                   8122:
                   8123:     len = LEN(p);
                   8124:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   8125:     v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11      noro     8126:     for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
                   8127:       ndl_add(d,DL(mr),t);
                   8128:       st = prev;
                   8129:       ed = n;
                   8130:       while ( ed > st ) {
                   8131:         md = (st+ed)/2;
                   8132:         u = s0+md*nd_wpd;
                   8133:         c = DL_COMPARE(u,t);
                   8134:         if ( c == 0 ) break;
                   8135:         else if ( c > 0 ) st = md;
                   8136:         else ed = md;
                   8137:       }
                   8138:       prev = v[j] = md;
1.1       noro     8139:     }
                   8140:     r = (IndArray)MALLOC(sizeof(struct oIndArray));
                   8141:     r->head = v[0];
                   8142:     diff = 0;
                   8143:     for ( i = 1; i < len; i++ ) {
                   8144:         cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   8145:     }
                   8146:     if ( diff < 256 ) {
                   8147:         r->width = 1;
                   8148:         ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   8149:         r->index.c = ivc;
                   8150:         for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   8151:     } else if ( diff < 65536 ) {
                   8152:         r->width = 2;
                   8153:         ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   8154:         r->index.s = ivs;
                   8155:         for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   8156:     } else {
                   8157:         r->width = 4;
                   8158:         ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   8159:         r->index.i = ivi;
                   8160:         for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
                   8161:     }
                   8162:     return r;
                   8163: }
                   8164:
                   8165: int compress_array(Z *svect,Z *cvect,int n)
                   8166: {
                   8167:     int i,j;
                   8168:
                   8169:     for ( i = j = 0; i < n; i++ )
                   8170:         if ( svect[i] ) cvect[j++] = svect[i];
                   8171:     return j;
                   8172: }
                   8173:
                   8174: void expand_array(Z *svect,Z *cvect,int n)
                   8175: {
                   8176:     int i,j;
                   8177:
                   8178:     for ( i = j = 0; j < n;  i++  )
                   8179:         if ( svect[i] ) svect[i] = cvect[j++];
                   8180: }
                   8181:
1.8       noro     8182: #if 0
1.1       noro     8183: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8184: {
                   8185:     int i,j,k,len,pos,prev,nz;
                   8186:     Z cs,mcs,c1,c2,cr,gcd,t;
                   8187:     IndArray ivect;
                   8188:     unsigned char *ivc;
                   8189:     unsigned short *ivs;
                   8190:     unsigned int *ivi;
                   8191:     NDV redv;
                   8192:     NMV mr;
                   8193:     NODE rp;
                   8194:     int maxrs;
                   8195:     double hmag;
                   8196:     Z *cvect;
1.3       noro     8197:     int l;
1.1       noro     8198:
                   8199:     maxrs = 0;
                   8200:     for ( i = 0; i < col && !svect[i]; i++ );
                   8201:     if ( i == col ) return maxrs;
                   8202:     hmag = p_mag((P)svect[i])*nd_scale;
                   8203:     cvect = (Z *)MALLOC(col*sizeof(Q));
                   8204:     for ( i = 0; i < nred; i++ ) {
                   8205:         ivect = imat[i];
                   8206:         k = ivect->head;
                   8207:         if ( svect[k] ) {
                   8208:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8209:             redv = nd_demand?ndv_load(rp0[i]->index)
                   8210:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   8211:             len = LEN(redv); mr = BDY(redv);
1.6       noro     8212:             igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1       noro     8213:             chsgnz(cs,&mcs);
                   8214:             if ( !UNIQ(cr) ) {
                   8215:                 for ( j = 0; j < col; j++ ) {
                   8216:                     mulz(svect[j],cr,&c1); svect[j] = c1;
                   8217:                 }
                   8218:             }
                   8219:             svect[k] = 0; prev = k;
                   8220:             switch ( ivect->width ) {
                   8221:                 case 1:
                   8222:                     ivc = ivect->index.c;
                   8223:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8224:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     8225:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     8226:                     }
                   8227:                     break;
                   8228:                 case 2:
                   8229:                     ivs = ivect->index.s;
                   8230:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8231:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     8232:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     8233:                     }
                   8234:                     break;
                   8235:                 case 4:
                   8236:                     ivi = ivect->index.i;
                   8237:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8238:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     8239:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     8240:                     }
                   8241:                     break;
                   8242:             }
                   8243:             for ( j = k+1; j < col && !svect[j]; j++ );
                   8244:             if ( j == col ) break;
                   8245:             if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
                   8246:                 nz = compress_array(svect,cvect,col);
                   8247:                 removecont_array((P *)cvect,nz,1);
                   8248:                 expand_array(svect,cvect,nz);
                   8249:                 hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   8250:             }
                   8251:         }
                   8252:     }
                   8253:     nz = compress_array(svect,cvect,col);
                   8254:     removecont_array((P *)cvect,nz,1);
                   8255:     expand_array(svect,cvect,nz);
                   8256:     if ( DP_Print ) {
                   8257:         fprintf(asir_out,"-"); fflush(asir_out);
                   8258:     }
                   8259:     return maxrs;
                   8260: }
1.4       noro     8261: #else
1.9       noro     8262:
1.4       noro     8263: /* direct mpz version */
                   8264: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8265: {
                   8266:     int i,j,k,len,pos,prev;
                   8267:     mpz_t cs,cr,gcd;
                   8268:     IndArray ivect;
                   8269:     unsigned char *ivc;
                   8270:     unsigned short *ivs;
                   8271:     unsigned int *ivi;
                   8272:     NDV redv;
                   8273:     NMV mr;
                   8274:     NODE rp;
                   8275:     int maxrs;
                   8276:     double hmag;
                   8277:     int l;
1.13      noro     8278:     static mpz_t *svect;
                   8279:     static int svect_len=0;
1.4       noro     8280:
                   8281:     maxrs = 0;
                   8282:     for ( i = 0; i < col && !svect0[i]; i++ );
                   8283:     if ( i == col ) return maxrs;
                   8284:     hmag = p_mag((P)svect0[i])*nd_scale;
1.13      noro     8285:     if ( col > svect_len ) {
                   8286:       svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   8287:       svect_len = col;
                   8288:     }
1.4       noro     8289:     for ( i = 0; i < col; i++ ) {
                   8290:       mpz_init(svect[i]);
                   8291:       if ( svect0[i] )
                   8292:         mpz_set(svect[i],BDY(svect0[i]));
                   8293:       else
                   8294:         mpz_set_ui(svect[i],0);
                   8295:     }
                   8296:     mpz_init(gcd); mpz_init(cs); mpz_init(cr);
                   8297:     for ( i = 0; i < nred; i++ ) {
                   8298:         ivect = imat[i];
                   8299:         k = ivect->head;
                   8300:         if ( mpz_sgn(svect[k]) ) {
                   8301:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8302:             redv = nd_demand?ndv_load(rp0[i]->index)
                   8303:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   8304:             len = LEN(redv); mr = BDY(redv);
1.6       noro     8305:             mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4       noro     8306:             mpz_div(cs,svect[k],gcd);
1.6       noro     8307:             mpz_div(cr,BDY(CZ(mr)),gcd);
1.4       noro     8308:             mpz_neg(cs,cs);
1.9       noro     8309:             if ( MUNIMPZ(cr) )
                   8310:               for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
                   8311:             else if ( !UNIMPZ(cr) )
                   8312:               for ( j = 0; j < col; j++ ) {
                   8313:                 if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
                   8314:               }
1.4       noro     8315:             mpz_set_ui(svect[k],0);
                   8316:             prev = k;
                   8317:             switch ( ivect->width ) {
                   8318:                 case 1:
                   8319:                     ivc = ivect->index.c;
                   8320:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8321:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     8322:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     8323:                     }
                   8324:                     break;
                   8325:                 case 2:
                   8326:                     ivs = ivect->index.s;
                   8327:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8328:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     8329:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     8330:                     }
                   8331:                     break;
                   8332:                 case 4:
                   8333:                     ivi = ivect->index.i;
                   8334:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8335:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     8336:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     8337:                     }
                   8338:                     break;
                   8339:             }
                   8340:             for ( j = k+1; j < col && !svect[j]; j++ );
                   8341:             if ( j == col ) break;
                   8342:             if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
                   8343:                 mpz_removecont_array(svect,col);
                   8344:                 hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
                   8345:             }
                   8346:         }
                   8347:     }
                   8348:     mpz_removecont_array(svect,col);
                   8349:     if ( DP_Print ) {
                   8350:         fprintf(asir_out,"-"); fflush(asir_out);
                   8351:     }
                   8352:     for ( i = 0; i < col; i++ )
                   8353:       if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
                   8354:       else svect0[i] = 0;
                   8355:     return maxrs;
                   8356: }
                   8357: #endif
1.1       noro     8358:
1.40      noro     8359: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred,SIG sig)
1.1       noro     8360: {
                   8361:     int i,j,k,len,pos,prev;
                   8362:     UINT c,c1,c2,c3,up,lo,dmy;
                   8363:     IndArray ivect;
                   8364:     unsigned char *ivc;
                   8365:     unsigned short *ivs;
                   8366:     unsigned int *ivi;
                   8367:     NDV redv;
                   8368:     NMV mr;
                   8369:     NODE rp;
                   8370:     int maxrs;
                   8371:
                   8372:     maxrs = 0;
                   8373:     for ( i = 0; i < nred; i++ ) {
                   8374:         ivect = imat[i];
                   8375:         k = ivect->head; svect[k] %= m;
1.40      noro     8376:         if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.1       noro     8377:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8378:             c = m-c; redv = nd_ps[rp0[i]->index];
                   8379:             len = LEN(redv); mr = BDY(redv);
                   8380:             svect[k] = 0; prev = k;
                   8381:             switch ( ivect->width ) {
                   8382:                 case 1:
                   8383:                     ivc = ivect->index.c;
                   8384:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8385:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.40      noro     8386:                         if ( c1 ) {
                   8387:                           c2 = svect[pos];
1.1       noro     8388:                           DMA(c1,c,c2,up,lo);
                   8389:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   8390:                           } else svect[pos] = lo;
1.40      noro     8391:                         }
1.1       noro     8392:                     }
                   8393:                     break;
                   8394:                 case 2:
                   8395:                     ivs = ivect->index.s;
                   8396:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8397:                         pos = prev+ivs[j]; c1 = CM(mr);
                   8398:                         prev = pos;
1.40      noro     8399:                         if ( c1 ) {
                   8400:                           c2 = svect[pos];
1.1       noro     8401:                           DMA(c1,c,c2,up,lo);
                   8402:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   8403:                           } else svect[pos] = lo;
1.40      noro     8404:                         }
1.1       noro     8405:                     }
                   8406:                     break;
                   8407:                 case 4:
                   8408:                     ivi = ivect->index.i;
                   8409:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8410:                         pos = prev+ivi[j]; c1 = CM(mr);
                   8411:                         prev = pos;
1.40      noro     8412:                         if ( c1 ) {
                   8413:                           c2 = svect[pos];
1.1       noro     8414:                           DMA(c1,c,c2,up,lo);
                   8415:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   8416:                           } else svect[pos] = lo;
1.40      noro     8417:                         }
1.1       noro     8418:                     }
                   8419:                     break;
                   8420:             }
                   8421:         }
                   8422:     }
                   8423:     for ( i = 0; i < col; i++ )
                   8424:         if ( svect[i] >= (UINT)m ) svect[i] %= m;
                   8425:     return maxrs;
                   8426: }
                   8427:
                   8428: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8429: {
                   8430:     int i,j,k,len,pos,prev;
                   8431:     UINT c,c1,c2,c3,up,lo,dmy;
                   8432:     IndArray ivect;
                   8433:     unsigned char *ivc;
                   8434:     unsigned short *ivs;
                   8435:     unsigned int *ivi;
                   8436:     NDV redv;
                   8437:     NMV mr;
                   8438:     NODE rp;
                   8439:     int maxrs;
                   8440:
                   8441:     maxrs = 0;
                   8442:     for ( i = 0; i < nred; i++ ) {
                   8443:         ivect = imat[i];
                   8444:         k = ivect->head;
                   8445:         if ( (c = svect[k]) != 0 ) {
                   8446:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8447:             c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
                   8448:             len = LEN(redv); mr = BDY(redv);
                   8449:             svect[k] = 0; prev = k;
                   8450:             switch ( ivect->width ) {
                   8451:                 case 1:
                   8452:                     ivc = ivect->index.c;
                   8453:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8454:                         pos = prev+ivc[j]; prev = pos;
                   8455:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   8456:                     }
                   8457:                     break;
                   8458:                 case 2:
                   8459:                     ivs = ivect->index.s;
                   8460:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8461:                         pos = prev+ivs[j]; prev = pos;
                   8462:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   8463:                     }
                   8464:                     break;
                   8465:                 case 4:
                   8466:                     ivi = ivect->index.i;
                   8467:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8468:                         pos = prev+ivi[j]; prev = pos;
                   8469:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   8470:                     }
                   8471:                     break;
                   8472:             }
                   8473:         }
                   8474:     }
                   8475:     return maxrs;
                   8476: }
                   8477:
                   8478: ND nd_add_lf(ND p1,ND p2)
                   8479: {
                   8480:     int n,c,can;
                   8481:     ND r;
                   8482:     NM m1,m2,mr0,mr,s;
                   8483:     Z t;
                   8484:
                   8485:     if ( !p1 ) return p2;
                   8486:     else if ( !p2 ) return p1;
                   8487:     else {
                   8488:         can = 0;
                   8489:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   8490:             c = DL_COMPARE(DL(m1),DL(m2));
                   8491:             switch ( c ) {
                   8492:                 case 0:
                   8493:                     addlf(CZ(m1),CZ(m2),&t);
                   8494:                     s = m1; m1 = NEXT(m1);
                   8495:                     if ( t ) {
                   8496:                         can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
                   8497:                     } else {
                   8498:                         can += 2; FREENM(s);
                   8499:                     }
                   8500:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   8501:                     break;
                   8502:                 case 1:
                   8503:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   8504:                     break;
                   8505:                 case -1:
                   8506:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   8507:                     break;
                   8508:             }
                   8509:         }
                   8510:         if ( !mr0 )
                   8511:             if ( m1 ) mr0 = m1;
                   8512:             else if ( m2 ) mr0 = m2;
                   8513:             else return 0;
                   8514:         else if ( m1 ) NEXT(mr) = m1;
                   8515:         else if ( m2 ) NEXT(mr) = m2;
                   8516:         else NEXT(mr) = 0;
                   8517:         BDY(p1) = mr0;
                   8518:         SG(p1) = MAX(SG(p1),SG(p2));
                   8519:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   8520:         FREEND(p2);
                   8521:         return p1;
                   8522:     }
                   8523: }
                   8524:
                   8525: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8526: {
                   8527:     int i,j,k,len,pos,prev;
                   8528:     mpz_t c,mc,c1;
                   8529:     IndArray ivect;
                   8530:     unsigned char *ivc;
                   8531:     unsigned short *ivs;
                   8532:     unsigned int *ivi;
                   8533:     NDV redv;
                   8534:     NMV mr;
                   8535:     NODE rp;
                   8536:     int maxrs;
                   8537:
                   8538:     maxrs = 0;
                   8539:     lf_lazy = 1;
                   8540:     for ( i = 0; i < nred; i++ ) {
                   8541:         ivect = imat[i];
                   8542:         k = ivect->head;
                   8543:         mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
                   8544:         if ( mpz_sgn(svect[k]) ) {
                   8545:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8546:             mpz_neg(svect[k],svect[k]);
                   8547:             redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
                   8548:             len = LEN(redv); mr = BDY(redv);
                   8549:             prev = k;
                   8550:             switch ( ivect->width ) {
                   8551:                 case 1:
                   8552:                     ivc = ivect->index.c;
                   8553:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8554:                         pos = prev+ivc[j]; prev = pos;
                   8555:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   8556:                     }
                   8557:                     break;
                   8558:                 case 2:
                   8559:                     ivs = ivect->index.s;
                   8560:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8561:                         pos = prev+ivs[j]; prev = pos;
                   8562:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   8563:                     }
                   8564:                     break;
                   8565:                 case 4:
                   8566:                     ivi = ivect->index.i;
                   8567:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8568:                         pos = prev+ivi[j]; prev = pos;
                   8569:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   8570:                     }
                   8571:                     break;
                   8572:             }
                   8573:             mpz_set_ui(svect[k],0);
                   8574:         }
                   8575:     }
                   8576:     lf_lazy=0;
                   8577:     for ( i = 0; i < col; i++ ) {
                   8578:         mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
                   8579:     }
                   8580:     return maxrs;
                   8581: }
                   8582:
                   8583: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
                   8584: {
                   8585:     int i,j,k,l,rank,s;
                   8586:     mpz_t a,a1,inv;
                   8587:     mpz_t *t,*pivot,*pk;
                   8588:     mpz_t **mat;
                   8589:     struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
                   8590:     int size,size1;
                   8591:
                   8592:     mpz_init(inv);
                   8593:     mpz_init(a);
                   8594:     mat = (mpz_t **)mat0;
                   8595:         size = 0;
                   8596:     for ( rank = 0, j = 0; j < col; j++ ) {
                   8597:         for ( i = rank; i < row; i++ ) {
                   8598:             mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
                   8599:         }
                   8600:         for ( i = rank; i < row; i++ )
                   8601:             if ( mpz_sgn(mat[i][j]) )
                   8602:                 break;
                   8603:         if ( i == row ) {
                   8604:             colstat[j] = 0;
                   8605:             continue;
                   8606:         } else
                   8607:             colstat[j] = 1;
                   8608:         if ( i != rank ) {
                   8609:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   8610:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   8611:         }
                   8612:         pivot = mat[rank];
                   8613:         s = sugar[rank];
                   8614:         mpz_invert(inv,pivot[j],BDY(current_mod_lf));
                   8615:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   8616:             if ( mpz_sgn(*pk) ) {
                   8617:                 mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
                   8618:             }
                   8619:         for ( i = rank+1; i < row; i++ ) {
                   8620:             t = mat[i];
                   8621:             if ( mpz_sgn(t[j]) ) {
                   8622:                 sugar[i] = MAX(sugar[i],s);
                   8623:                 mpz_neg(a,t[j]);
                   8624:                 red_by_vect_lf(t+j,pivot+j,a,col-j);
                   8625:             }
                   8626:         }
                   8627:         rank++;
                   8628:     }
                   8629:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   8630:         if ( colstat[j] ) {
                   8631:             pivot = mat[l];
                   8632:             s = sugar[l];
                   8633:             for ( k = j; k < col; k++ )
                   8634:               mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
                   8635:             for ( i = 0; i < l; i++ ) {
                   8636:                 t = mat[i];
                   8637:                 if ( mpz_sgn(t[j]) ) {
                   8638:                     sugar[i] = MAX(sugar[i],s);
                   8639:                     mpz_neg(a,t[j]);
                   8640:                     red_by_vect_lf(t+j,pivot+j,a,col-j);
                   8641:                 }
                   8642:             }
                   8643:             l--;
                   8644:         }
                   8645:     for ( j = 0, l = 0; l < rank; j++ )
                   8646:         if ( colstat[j] ) {
                   8647:             t = mat[l];
                   8648:             for ( k = j; k < col; k++ ) {
                   8649:                 mpz_mod(t[k],t[k],BDY(current_mod_lf));
                   8650:             }
                   8651:             l++;
                   8652:         }
                   8653:     return rank;
                   8654: }
                   8655:
                   8656:
                   8657: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   8658: {
                   8659:     int j,k,len;
                   8660:     UINT *p;
                   8661:     UINT c;
                   8662:     NDV r;
                   8663:     NMV mr0,mr;
                   8664:
                   8665:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   8666:     if ( !len ) return 0;
                   8667:     else {
                   8668:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8669: #if 0
                   8670:         ndv_alloc += nmv_adv*len;
                   8671: #endif
                   8672:         mr = mr0;
                   8673:         p = s0vect;
                   8674:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   8675:             if ( !rhead[j] ) {
                   8676:                 if ( (c = vect[k++]) != 0 ) {
                   8677:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   8678:                 }
                   8679:             }
                   8680:         MKNDV(nd_nvar,mr0,len,r);
                   8681:         return r;
                   8682:     }
                   8683: }
                   8684:
1.40      noro     8685: NDV vect_to_ndv_s(UINT *vect,int col,UINT *s0vect)
                   8686: {
                   8687:     int j,k,len;
                   8688:     UINT *p;
                   8689:     UINT c;
                   8690:     NDV r;
                   8691:     NMV mr0,mr;
                   8692:
                   8693:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   8694:     if ( !len ) return 0;
                   8695:     else {
                   8696:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8697:         mr = mr0;
                   8698:         p = s0vect;
                   8699:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   8700:           if ( (c = vect[k++]) != 0 ) {
                   8701:             ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   8702:           }
                   8703:         MKNDV(nd_nvar,mr0,len,r);
                   8704:         return r;
                   8705:     }
                   8706: }
                   8707:
1.1       noro     8708: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
                   8709: {
                   8710:     int j,k,len;
                   8711:     UINT *p;
                   8712:     NDV r;
                   8713:     NMV mr0,mr;
                   8714:
                   8715:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
                   8716:     if ( !len ) return 0;
                   8717:     else {
                   8718:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8719:         mr = mr0;
                   8720:         p = s0vect;
                   8721:         for ( j = 0; j < col; j++, p += nd_wpd )
                   8722:       if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
                   8723:             ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
                   8724:           }
                   8725:         MKNDV(nd_nvar,mr0,len,r);
                   8726:         return r;
                   8727:     }
                   8728: }
                   8729:
                   8730: /* for preprocessed vector */
                   8731:
                   8732: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   8733: {
1.6       noro     8734:   int j,k,len;
                   8735:   UINT *p;
                   8736:   Z c;
                   8737:   NDV r;
                   8738:   NMV mr0,mr;
1.1       noro     8739:
1.6       noro     8740:   for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   8741:   if ( !len ) return 0;
                   8742:   else {
                   8743:     mr0 = (NMV)MALLOC(nmv_adv*len);
1.1       noro     8744: #if 0
1.6       noro     8745:     ndv_alloc += nmv_adv*len;
1.1       noro     8746: #endif
1.6       noro     8747:     mr = mr0;
                   8748:     p = s0vect;
                   8749:     for ( j = k = 0; j < col; j++, p += nd_wpd ) {
                   8750:       if ( !rhead[j] ) {
                   8751:         if ( (c = vect[k++]) != 0 ) {
                   8752:           if ( !INT(c) )
                   8753:             error("vect_to_ndv_q : components must be integers");
                   8754:             ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
                   8755:         }
                   8756:       }
1.1       noro     8757:     }
1.6       noro     8758:     MKNDV(nd_nvar,mr0,len,r);
                   8759:     return r;
                   8760:   }
1.1       noro     8761: }
                   8762:
                   8763: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   8764: {
                   8765:     int j,k,len;
                   8766:     UINT *p;
                   8767:     mpz_t c;
                   8768:     NDV r;
                   8769:     NMV mr0,mr;
                   8770:
                   8771:     for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
                   8772:     if ( !len ) return 0;
                   8773:     else {
                   8774:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   8775: #if 0
                   8776:         ndv_alloc += nmv_adv*len;
                   8777: #endif
                   8778:         mr = mr0;
                   8779:         p = s0vect;
                   8780:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   8781:             if ( !rhead[j] ) {
                   8782:                 c[0] = vect[k++][0];
                   8783:                 if ( mpz_sgn(c) ) {
                   8784:                     ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
                   8785:                 }
                   8786:             }
                   8787:         MKNDV(nd_nvar,mr0,len,r);
                   8788:         return r;
                   8789:     }
                   8790: }
                   8791:
                   8792: /* for plain vector */
                   8793:
                   8794: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
                   8795: {
                   8796:     int j,k,len;
                   8797:     UINT *p;
                   8798:     Z c;
                   8799:     NDV r;
                   8800:     NMV mr0,mr;
                   8801:
                   8802:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   8803:     if ( !len ) return 0;
                   8804:     else {
                   8805:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   8806: #if 0
                   8807:         ndv_alloc += nmv_adv*len;
                   8808: #endif
                   8809:         mr = mr0;
                   8810:         p = s0vect;
                   8811:         for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   8812:             if ( (c = vect[k]) != 0 ) {
                   8813:                 if ( !INT(c) )
1.6       noro     8814:                     error("plain_vect_to_ndv_q : components must be integers");
                   8815:                 ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1       noro     8816:             }
                   8817:         MKNDV(nd_nvar,mr0,len,r);
                   8818:         return r;
                   8819:     }
                   8820: }
                   8821:
                   8822: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
                   8823: {
                   8824:     ND_pairs t;
                   8825:     NODE sp0,sp;
                   8826:     int stat;
                   8827:     ND spol;
                   8828:
                   8829:     for ( t = l; t; t = NEXT(t) ) {
                   8830:         stat = nd_sp(m,trace,t,&spol);
                   8831:         if ( !stat ) return 0;
                   8832:         if ( spol ) {
                   8833:             add_pbucket_symbolic(bucket,spol);
                   8834:         }
                   8835:     }
                   8836:     return 1;
                   8837: }
                   8838:
                   8839: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   8840: {
                   8841:     NODE rp0,rp;
                   8842:     NM mul,head,s0,s;
                   8843:     int index,col,i,sugar;
                   8844:     RHist h;
                   8845:     UINT *s0v,*p;
                   8846:     NM_ind_pair pair;
                   8847:     ND red;
                   8848:     NDV *ps;
                   8849:
                   8850:     s0 = 0; rp0 = 0; col = 0;
                   8851:   if ( nd_demand )
                   8852:       ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   8853:   else
                   8854:       ps = trace?nd_ps_trace:nd_ps;
                   8855:     while ( 1 ) {
                   8856:         head = remove_head_pbucket_symbolic(bucket);
                   8857:         if ( !head ) break;
                   8858:         if ( !s0 ) s0 = head;
                   8859:         else NEXT(s) = head;
                   8860:         s = head;
                   8861:         index = ndl_find_reducer(DL(head));
                   8862:         if ( index >= 0 ) {
                   8863:             h = nd_psh[index];
                   8864:             NEWNM(mul);
                   8865:             ndl_sub(DL(head),DL(h),DL(mul));
                   8866:             if ( ndl_check_bound2(index,DL(mul)) )
                   8867:                 return 0;
                   8868:             sugar = TD(DL(mul))+SG(ps[index]);
1.28      noro     8869:             MKNM_ind_pair(pair,mul,index,sugar,0);
1.1       noro     8870:             red = ndv_mul_nm_symbolic(mul,ps[index]);
                   8871:             add_pbucket_symbolic(bucket,nd_remove_head(red));
                   8872:             NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   8873:         }
                   8874:         col++;
                   8875:     }
                   8876:     if ( rp0 ) NEXT(rp) = 0;
                   8877:     NEXT(s) = 0;
                   8878:     s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   8879:     for ( i = 0, p = s0v, s = s0; i < col;
                   8880:         i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   8881:     *s0vect = s0v;
                   8882:     *r = rp0;
                   8883:     return col;
                   8884: }
                   8885:
                   8886: void print_ndp(ND_pairs l)
                   8887: {
                   8888:   ND_pairs t;
                   8889:
                   8890:   for ( t = l; t; t = NEXT(t) )
                   8891:     printf("[%d,%d] ",t->i1,t->i2);
                   8892:   printf("\n");
                   8893: }
                   8894:
                   8895: NODE nd_f4(int m,int checkonly,int **indp)
                   8896: {
                   8897:     int i,nh,stat,index,f4red;
                   8898:     NODE r,g,tn0,tn,node;
                   8899:     ND_pairs d,l,t,ll0,ll,lh;
                   8900:   LIST l0,l1;
                   8901:     ND spol,red;
                   8902:     NDV nf,redv;
                   8903:     NM s0,s;
                   8904:     NODE rp0,srp0,nflist,nzlist,nzlist_t;
                   8905:     int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
                   8906:     UINT c;
                   8907:     UINT **spmat;
                   8908:     UINT *s0vect,*svect,*p,*v;
                   8909:     int *colstat;
                   8910:     IndArray *imat;
                   8911:     int *rhead;
                   8912:     int spcol,sprow;
                   8913:     int sugar,sugarh;
                   8914:     PGeoBucket bucket;
                   8915:     struct oEGT eg0,eg1,eg_f4;
                   8916:     Z i1,i2,sugarq;
1.12      noro     8917:
                   8918:     init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1       noro     8919: #if 0
                   8920:     ndv_alloc = 0;
                   8921: #endif
1.11      noro     8922:     Nf4_red=0;
1.1       noro     8923:     g = 0; d = 0;
                   8924:     for ( i = 0; i < nd_psn; i++ ) {
                   8925:         d = update_pairs(d,g,i,0);
                   8926:         g = update_base(g,i);
                   8927:     }
                   8928:   nzlist = 0;
                   8929:     nzlist_t = nd_nzlist;
                   8930:     f4red = 1;
                   8931:     nd_last_nonzero = 0;
                   8932:     while ( d ) {
                   8933:         get_eg(&eg0);
                   8934:         l = nd_minsugarp(d,&d);
                   8935:         sugar = nd_sugarweight?l->sugar2:SG(l);
                   8936:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   8937:         if ( nzlist_t ) {
                   8938:             node = BDY((LIST)BDY(nzlist_t));
1.6       noro     8939:             sugarh = ZTOS((Q)ARG0(node));
1.1       noro     8940:             tn = BDY((LIST)ARG1(node));
                   8941:             if ( !tn ) {
                   8942:               nzlist_t = NEXT(nzlist_t);
                   8943:               continue;
                   8944:             }
                   8945:             /* tn = [[i1,i2],...] */
                   8946:             lh = nd_ipairtospair(tn);
                   8947:         }
                   8948:         bucket = create_pbucket();
                   8949:         stat = nd_sp_f4(m,0,l,bucket);
                   8950:         if ( !stat ) {
                   8951:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8952:             NEXT(t) = d; d = l;
                   8953:             d = nd_reconstruct(0,d);
                   8954:             continue;
                   8955:         }
                   8956:         if ( bucket->m < 0 ) continue;
                   8957:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   8958:         if ( !col ) {
                   8959:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8960:             NEXT(t) = d; d = l;
                   8961:             d = nd_reconstruct(0,d);
                   8962:             continue;
                   8963:         }
1.12      noro     8964:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1       noro     8965:         if ( DP_Print )
1.6       noro     8966:             fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5       noro     8967:                 sugar,eg_f4.exectime);
1.1       noro     8968:         nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
                   8969:         if ( checkonly && nflist ) return 0;
                   8970:         /* adding new bases */
                   8971:         if ( nflist ) nd_last_nonzero = f4red;
                   8972:         for ( r = nflist; r; r = NEXT(r) ) {
                   8973:             nf = (NDV)BDY(r);
1.38      noro     8974:             if ( nd_f4_td ) SG(nf) = nd_tdeg(nf);
1.1       noro     8975:             ndv_removecont(m,nf);
                   8976:             if ( !m && nd_nalg ) {
                   8977:                 ND nf1;
                   8978:
                   8979:                 nf1 = ndvtond(m,nf);
                   8980:                 nd_monic(0,&nf1);
                   8981:                 nd_removecont(m,nf1);
                   8982:                 nf = ndtondv(m,nf1);
                   8983:             }
1.24      noro     8984:             nh = ndv_newps(m,nf,0);
1.1       noro     8985:             d = update_pairs(d,g,nh,0);
                   8986:             g = update_base(g,nh);
                   8987:         }
                   8988:         if ( DP_Print ) {
                   8989:           fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
                   8990:         }
                   8991:         if ( nd_gentrace ) {
                   8992:       for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
                   8993:         NEXTNODE(tn0,tn);
1.6       noro     8994:                 STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1       noro     8995:                 node = mknode(2,i1,i2); MKLIST(l0,node);
                   8996:         BDY(tn) = l0;
                   8997:       }
                   8998:       if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6       noro     8999:             STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1       noro     9000:             MKNODE(node,l1,nzlist); nzlist = node;
                   9001:         }
                   9002:         if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
                   9003:         f4red++;
                   9004:         if ( nd_f4red && f4red > nd_f4red ) break;
                   9005:         if ( nd_rank0 && !nflist ) break;
                   9006:     }
                   9007:     if ( nd_gentrace ) {
                   9008:     MKLIST(l0,reverse_node(nzlist));
                   9009:         MKNODE(nd_alltracelist,l0,0);
                   9010:     }
                   9011: #if 0
                   9012:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   9013: #endif
1.12      noro     9014:   if ( DP_Print ) {
                   9015:     fprintf(asir_out,"number of red=%d,",Nf4_red);
                   9016:     fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
                   9017:       f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
1.41      noro     9018:     fprintf(asir_out,"number of removed pairs=%d\n,",NcriB+NcriMF+Ncri2);
1.12      noro     9019:   }
1.1       noro     9020:   conv_ilist(nd_demand,0,g,indp);
                   9021:     return g;
                   9022: }
                   9023:
                   9024: NODE nd_f4_trace(int m,int **indp)
                   9025: {
                   9026:     int i,nh,stat,index;
                   9027:     NODE r,g;
                   9028:     ND_pairs d,l,l0,t;
                   9029:     ND spol,red;
                   9030:     NDV nf,redv,nfqv,nfv;
                   9031:     NM s0,s;
                   9032:     NODE rp0,srp0,nflist;
                   9033:     int nsp,nred,col,rank,len,k,j,a;
                   9034:     UINT c;
                   9035:     UINT **spmat;
                   9036:     UINT *s0vect,*svect,*p,*v;
                   9037:     int *colstat;
                   9038:     IndArray *imat;
                   9039:     int *rhead;
                   9040:     int spcol,sprow;
                   9041:     int sugar;
                   9042:     PGeoBucket bucket;
                   9043:     struct oEGT eg0,eg1,eg_f4;
                   9044:
                   9045:     g = 0; d = 0;
                   9046:     for ( i = 0; i < nd_psn; i++ ) {
                   9047:         d = update_pairs(d,g,i,0);
                   9048:         g = update_base(g,i);
                   9049:     }
                   9050:     while ( d ) {
                   9051:         get_eg(&eg0);
                   9052:         l = nd_minsugarp(d,&d);
                   9053:         sugar = SG(l);
                   9054:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   9055:         bucket = create_pbucket();
                   9056:         stat = nd_sp_f4(m,0,l,bucket);
                   9057:         if ( !stat ) {
                   9058:             for ( t = l; NEXT(t); t = NEXT(t) );
                   9059:             NEXT(t) = d; d = l;
                   9060:             d = nd_reconstruct(1,d);
                   9061:             continue;
                   9062:         }
                   9063:         if ( bucket->m < 0 ) continue;
                   9064:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   9065:         if ( !col ) {
                   9066:             for ( t = l; NEXT(t); t = NEXT(t) );
                   9067:             NEXT(t) = d; d = l;
                   9068:             d = nd_reconstruct(1,d);
                   9069:             continue;
                   9070:         }
                   9071:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   9072:         if ( DP_Print )
1.3       noro     9073:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5       noro     9074:                 sugar,eg_f4.exectime);
1.1       noro     9075:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   9076:         if ( !l0 ) continue;
                   9077:         l = l0;
                   9078:
                   9079:         /* over Q */
                   9080:         bucket = create_pbucket();
                   9081:         stat = nd_sp_f4(0,1,l,bucket);
                   9082:         if ( !stat ) {
                   9083:             for ( t = l; NEXT(t); t = NEXT(t) );
                   9084:             NEXT(t) = d; d = l;
                   9085:             d = nd_reconstruct(1,d);
                   9086:             continue;
                   9087:         }
                   9088:         if ( bucket->m < 0 ) continue;
                   9089:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   9090:         if ( !col ) {
                   9091:             for ( t = l; NEXT(t); t = NEXT(t) );
                   9092:             NEXT(t) = d; d = l;
                   9093:             d = nd_reconstruct(1,d);
                   9094:             continue;
                   9095:         }
                   9096:         nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   9097:         /* adding new bases */
                   9098:         for ( r = nflist; r; r = NEXT(r) ) {
                   9099:             nfqv = (NDV)BDY(r);
                   9100:             ndv_removecont(0,nfqv);
1.6       noro     9101:             if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1       noro     9102:             if ( nd_nalg ) {
                   9103:                 ND nf1;
                   9104:
                   9105:                 nf1 = ndvtond(m,nfqv);
                   9106:                 nd_monic(0,&nf1);
                   9107:                 nd_removecont(0,nf1);
                   9108:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   9109:             }
                   9110:             nfv = ndv_dup(0,nfqv);
                   9111:             ndv_mod(m,nfv);
                   9112:             ndv_removecont(m,nfv);
1.24      noro     9113:             nh = ndv_newps(0,nfv,nfqv);
1.1       noro     9114:             d = update_pairs(d,g,nh,0);
                   9115:             g = update_base(g,nh);
                   9116:         }
                   9117:     }
                   9118: #if 0
                   9119:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   9120: #endif
                   9121:   conv_ilist(nd_demand,1,g,indp);
                   9122:     return g;
                   9123: }
                   9124:
                   9125: int rref(matrix mat,int *sugar)
                   9126: {
                   9127:   int row,col,i,j,k,l,s,wcol,wj;
                   9128:   unsigned long bj;
                   9129:   unsigned long **a;
                   9130:   unsigned long *ai,*ak,*as,*t;
                   9131:   int *pivot;
                   9132:
                   9133:   row = mat->row;
                   9134:   col = mat->col;
                   9135:   a = mat->a;
                   9136:   wcol = (col+BLEN-1)/BLEN;
                   9137:   pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   9138:   i = 0;
                   9139:   for ( j = 0; j < col; j++ ) {
                   9140:   wj = j/BLEN; bj = 1L<<(j%BLEN);
                   9141:     for ( k = i; k < row; k++ )
                   9142:     if ( a[k][wj] & bj ) break;
                   9143:     if ( k == row ) continue;
                   9144:   pivot[i] = j;
                   9145:     if ( k != i ) {
                   9146:    t = a[i]; a[i] = a[k]; a[k] = t;
                   9147:    s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
                   9148:   }
                   9149:   ai = a[i];
                   9150:     for ( k = i+1; k < row; k++ ) {
                   9151:     ak = a[k];
                   9152:     if ( ak[wj] & bj ) {
                   9153:       for ( l = wj; l < wcol; l++ )
                   9154:       ak[l] ^= ai[l];
                   9155:       sugar[k] = MAX(sugar[k],sugar[i]);
                   9156:     }
                   9157:   }
                   9158:   i++;
                   9159:   }
                   9160:   for ( k = i-1; k >= 0; k-- ) {
                   9161:     j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
                   9162:   ak = a[k];
                   9163:     for ( s = 0; s < k; s++ ) {
                   9164:     as = a[s];
                   9165:       if ( as[wj] & bj ) {
                   9166:         for ( l = wj; l < wcol; l++ )
                   9167:       as[l] ^= ak[l];
                   9168:       sugar[s] = MAX(sugar[s],sugar[k]);
                   9169:     }
                   9170:   }
                   9171:   }
                   9172:   return i;
                   9173: }
                   9174:
                   9175: void print_matrix(matrix mat)
                   9176: {
                   9177:   int row,col,i,j;
                   9178:   unsigned long *ai;
                   9179:
                   9180:   row = mat->row;
                   9181:   col = mat->col;
                   9182:   printf("%d x %d\n",row,col);
                   9183:   for ( i = 0; i < row; i++ ) {
                   9184:   ai = mat->a[i];
                   9185:     for ( j = 0; j < col; j++ ) {
                   9186:     if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
                   9187:     else putchar('0');
                   9188:   }
                   9189:   putchar('\n');
                   9190:   }
                   9191: }
                   9192:
                   9193: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
                   9194:
                   9195: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
                   9196: {
                   9197:   int row,col,wcol,wj,i,j;
                   9198:   unsigned long bj;
                   9199:   unsigned long *ai;
                   9200:   unsigned long **a;
                   9201:   int len;
                   9202:   int *pos;
                   9203:
                   9204:   row = mat->row;
                   9205:   col = mat->col;
                   9206:   wcol = (col+BLEN-1)/BLEN;
                   9207:   pos = (int *)MALLOC(wcol*sizeof(int));
                   9208:   bzero(pos,wcol*sizeof(int));
                   9209:   for ( i = j = 0; i < wcol; i++ )
                   9210:     if ( v[i] ) pos[j++] = i;;
                   9211:   len = j;
                   9212:   wj = rhead/BLEN;
                   9213:   bj = 1L<<rhead%BLEN;
                   9214:   a = mat->a;
                   9215:   for ( i = 0; i < row; i++ ) {
                   9216:   ai = a[i];
                   9217:     if ( ai[wj]&bj ) {
                   9218:     for ( j = 0; j < len; j++ )
                   9219:       ai[pos[j]] ^= v[pos[j]];
                   9220:     sugar[i] = MAX(sugar[i],rsugar);
                   9221:   }
                   9222:   }
                   9223: }
                   9224:
                   9225: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   9226: {
                   9227:     int nsp,nred,i,i0,k,rank,row;
                   9228:     NODE r0,rp;
                   9229:     ND_pairs sp;
                   9230:   ND spol;
                   9231:   NM_ind_pair rt;
                   9232:     int *s0hash;
                   9233:   UINT *s;
                   9234:   int *pivot,*sugar,*head;
                   9235:   matrix mat;
                   9236:     NM m;
                   9237:     NODE r;
                   9238:   struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
                   9239:   int rhead,rsugar,size;
                   9240:     unsigned long *v;
                   9241:
                   9242:     get_eg(&eg0);
                   9243:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   9244:     nred = length(rp0);
                   9245:     mat = alloc_matrix(nsp,col);
                   9246:     s0hash = (int *)MALLOC(col*sizeof(int));
                   9247:     for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
                   9248:         s0hash[i] = ndl_hash_value(s);
                   9249:
                   9250:   sugar = (int *)MALLOC(nsp*sizeof(int));
                   9251:   for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
                   9252:     nd_sp(2,0,sp,&spol);
                   9253:     if ( spol ) {
                   9254:         mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
                   9255:       sugar[i] = SG(spol);
                   9256:       i++;
                   9257:     }
                   9258:   }
                   9259:   mat->row = i;
                   9260:     if ( DP_Print ) {
                   9261:       fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
                   9262:     }
                   9263:   size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
                   9264:   v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
                   9265:     for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
                   9266:     rt = (NM_ind_pair)BDY(rp);
                   9267:     bzero(v,size);
                   9268:         rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
                   9269:     rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
                   9270:       red_by_vect_2(mat,sugar,v,rhead,rsugar);
                   9271:   }
                   9272:
                   9273:     get_eg(&eg1);
                   9274:     init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
                   9275:   rank = rref(mat,sugar);
                   9276:
                   9277:     for ( i = 0, r0 = 0; i < rank; i++ ) {
                   9278:       NEXTNODE(r0,r);
                   9279:     BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
                   9280:       SG((NDV)BDY(r)) = sugar[i];
                   9281:     }
                   9282:     if ( r0 ) NEXT(r) = 0;
                   9283:     get_eg(&eg2);
                   9284:     init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
                   9285:     if ( DP_Print ) {
                   9286:         fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5       noro     9287:       eg_elim1.exectime,eg_elim2.exectime);
1.1       noro     9288:         fflush(asir_out);
                   9289:   }
                   9290:     return r0;
                   9291: }
                   9292:
                   9293:
                   9294: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   9295: {
                   9296:     IndArray *imat;
1.11      noro     9297:     int nsp,nred,i,start;
1.1       noro     9298:     int *rhead;
                   9299:     NODE r0,rp;
                   9300:     ND_pairs sp;
                   9301:     NM_ind_pair *rvect;
                   9302:     UINT *s;
                   9303:     int *s0hash;
1.11      noro     9304:     struct oEGT eg0,eg1,eg_conv;
1.1       noro     9305:
                   9306:     if ( m == 2 && nd_rref2 )
                   9307:      return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
                   9308:
                   9309:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   9310:     nred = length(rp0);
                   9311:     imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   9312:     rhead = (int *)MALLOC(col*sizeof(int));
                   9313:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   9314:
                   9315:     /* construction of index arrays */
1.11      noro     9316:     get_eg(&eg0);
1.1       noro     9317:     if ( DP_Print ) {
1.11      noro     9318:       fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   9319:       fflush(asir_out);
1.1       noro     9320:     }
                   9321:     rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11      noro     9322:     for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1       noro     9323:         rvect[i] = (NM_ind_pair)BDY(rp);
1.11      noro     9324:         imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1       noro     9325:         rhead[imat[i]->head] = 1;
1.11      noro     9326:         start = imat[i]->head;
                   9327:     }
1.12      noro     9328:     get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11      noro     9329:     if ( DP_Print ) {
                   9330:       fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   9331:       fflush(asir_out);
1.1       noro     9332:     }
                   9333:     if ( m > 0 )
1.7       noro     9334: #if SIZEOF_LONG==8
1.1       noro     9335:         r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   9336: #else
                   9337:         r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   9338: #endif
                   9339:     else if ( m == -1 )
                   9340:         r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   9341:     else if ( m == -2 )
                   9342:         r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   9343:     else
                   9344:         r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   9345:     return r0;
                   9346: }
                   9347:
                   9348: /* for Fp, 2<=p<2^16 */
                   9349:
                   9350: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   9351:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   9352: {
                   9353:     int spcol,sprow,a;
                   9354:     int i,j,k,l,rank;
                   9355:     NODE r0,r;
                   9356:     ND_pairs sp;
                   9357:     ND spol;
                   9358:     UINT **spmat;
                   9359:     UINT *svect,*v;
                   9360:     int *colstat;
                   9361:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9362:     int maxrs;
                   9363:     int *spsugar;
                   9364:     ND_pairs *spactive;
                   9365:
                   9366:     spcol = col-nred;
                   9367:     get_eg(&eg0);
                   9368:     /* elimination (1st step) */
                   9369:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   9370:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   9371:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9372:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   9373:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9374:         nd_sp(m,0,sp,&spol);
                   9375:         if ( !spol ) continue;
                   9376:         nd_to_vect(m,s0vect,col,spol,svect);
                   9377:         if ( m == -1 )
                   9378:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   9379:         else
1.40      noro     9380:             maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,0);
1.1       noro     9381:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9382:         if ( i < col ) {
                   9383:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   9384:             for ( j = k = 0; j < col; j++ )
                   9385:                 if ( !rhead[j] ) v[k++] = svect[j];
                   9386:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9387:             if ( nz )
                   9388:             spactive[sprow] = sp;
                   9389:             sprow++;
                   9390:         }
                   9391:         nd_free(spol);
                   9392:     }
                   9393:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9394:     if ( DP_Print ) {
1.5       noro     9395:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9396:         fflush(asir_out);
                   9397:     }
                   9398:     /* free index arrays */
                   9399:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   9400:
                   9401:     /* elimination (2nd step) */
                   9402:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9403:     if ( m == -1 )
                   9404:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   9405:     else
                   9406:         rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   9407:     r0 = 0;
                   9408:     for ( i = 0; i < rank; i++ ) {
                   9409:         NEXTNODE(r0,r); BDY(r) =
                   9410:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   9411:         SG((NDV)BDY(r)) = spsugar[i];
                   9412:         GCFREE(spmat[i]);
                   9413:     }
                   9414:     if ( r0 ) NEXT(r) = 0;
                   9415:
                   9416:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   9417:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9418:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9419:     if ( DP_Print ) {
1.5       noro     9420:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9421:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9422:             nsp,nred,sprow,spcol,rank);
1.5       noro     9423:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9424:     }
                   9425:     if ( nz ) {
                   9426:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   9427:         if ( rank > 0 ) {
                   9428:             NEXT(spactive[rank-1]) = 0;
                   9429:             *nz = spactive[0];
                   9430:         } else
                   9431:             *nz = 0;
                   9432:     }
                   9433:     return r0;
                   9434: }
                   9435:
1.40      noro     9436: NODE nd_f4_red_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   9437:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
                   9438: {
                   9439:     int spcol,sprow,a;
                   9440:     int i,j,k,l,rank;
                   9441:     NODE r0,r;
                   9442:     ND_pairs sp;
                   9443:     ND spol;
                   9444:     UINT **spmat;
                   9445:     UINT *svect,*cvect;
                   9446:     UINT *v;
                   9447:     int *colstat;
                   9448:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9449:     int maxrs;
                   9450:     int *spsugar;
                   9451:     ND_pairs *spactive;
                   9452:     SIG *spsig;
                   9453:
                   9454:     get_eg(&eg0);
                   9455:     /* elimination (1st step) */
                   9456:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   9457:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9458:     spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
                   9459:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9460:         nd_sp(m,0,sp,&spol);
                   9461:         if ( !spol ) {
                   9462:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
                   9463:           continue;
                   9464:         }
                   9465:         svect = (UINT *)MALLOC(col*sizeof(UINT));
                   9466:         nd_to_vect(m,s0vect,col,spol,svect);
                   9467:         maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,spol->sig);
                   9468:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9469:         if ( i < col ) {
                   9470:             spmat[sprow] = svect;
                   9471:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9472:             spsig[sprow] = sp->sig;
                   9473:             sprow++;
                   9474:         } else {
                   9475:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
                   9476:         }
                   9477:         nd_free(spol);
                   9478:     }
                   9479:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
                   9480:     if ( DP_Print ) {
                   9481:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   9482:         fflush(asir_out);
                   9483:     }
                   9484:     /* free index arrays */
                   9485:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   9486:
                   9487:     /* elimination (2nd step) */
                   9488:     colstat = (int *)MALLOC(col*sizeof(int));
                   9489:     rank = nd_gauss_elim_mod_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
                   9490:     r0 = 0;
                   9491:     for ( i = 0; i < sprow; i++ ) {
                   9492:         if ( spsugar[i] >= 0 ) {
                   9493:           NEXTNODE(r0,r);
                   9494:           BDY(r) = vect_to_ndv_s(spmat[i],col,s0vect);
                   9495:           SG((NDV)BDY(r)) = spsugar[i];
                   9496:           ((NDV)BDY(r))->sig = spsig[i];
                   9497:         } else
                   9498:           syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
                   9499:         GCFREE(spmat[i]);
                   9500:     }
                   9501:     if ( r0 ) NEXT(r) = 0;
                   9502:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
                   9503:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9504:     if ( DP_Print ) {
                   9505:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   9506:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9507:             nsp,nred,sprow,col,rank);
                   9508:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   9509:     }
                   9510:     return r0;
                   9511: }
                   9512:
1.1       noro     9513:
                   9514: /* for small finite fields */
                   9515:
                   9516: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   9517:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   9518: {
                   9519:     int spcol,sprow,a;
                   9520:     int i,j,k,l,rank;
                   9521:     NODE r0,r;
                   9522:     ND_pairs sp;
                   9523:     ND spol;
                   9524:     UINT **spmat;
                   9525:     UINT *svect,*v;
                   9526:     int *colstat;
                   9527:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9528:     int maxrs;
                   9529:     int *spsugar;
                   9530:     ND_pairs *spactive;
                   9531:
                   9532:     spcol = col-nred;
                   9533:     get_eg(&eg0);
                   9534:     /* elimination (1st step) */
                   9535:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   9536:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   9537:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9538:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   9539:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9540:         nd_sp(m,0,sp,&spol);
                   9541:         if ( !spol ) continue;
                   9542:         nd_to_vect(m,s0vect,col,spol,svect);
                   9543:         maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   9544:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9545:         if ( i < col ) {
                   9546:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   9547:             for ( j = k = 0; j < col; j++ )
                   9548:                 if ( !rhead[j] ) v[k++] = svect[j];
                   9549:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9550:             if ( nz )
                   9551:             spactive[sprow] = sp;
                   9552:             sprow++;
                   9553:         }
                   9554:         nd_free(spol);
                   9555:     }
                   9556:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9557:     if ( DP_Print ) {
1.5       noro     9558:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9559:         fflush(asir_out);
                   9560:     }
                   9561:     /* free index arrays */
                   9562:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   9563:
                   9564:     /* elimination (2nd step) */
                   9565:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9566:     rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   9567:     r0 = 0;
                   9568:     for ( i = 0; i < rank; i++ ) {
                   9569:         NEXTNODE(r0,r); BDY(r) =
                   9570:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   9571:         SG((NDV)BDY(r)) = spsugar[i];
                   9572:         GCFREE(spmat[i]);
                   9573:     }
                   9574:     if ( r0 ) NEXT(r) = 0;
                   9575:
                   9576:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   9577:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9578:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9579:     if ( DP_Print ) {
1.5       noro     9580:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9581:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9582:             nsp,nred,sprow,spcol,rank);
1.5       noro     9583:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9584:     }
                   9585:     if ( nz ) {
                   9586:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   9587:         if ( rank > 0 ) {
                   9588:             NEXT(spactive[rank-1]) = 0;
                   9589:             *nz = spactive[0];
                   9590:         } else
                   9591:             *nz = 0;
                   9592:     }
                   9593:     return r0;
                   9594: }
                   9595:
                   9596: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   9597:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   9598: {
                   9599:     int spcol,sprow,a;
                   9600:     int i,j,k,l,rank;
                   9601:     NODE r0,r;
                   9602:     ND_pairs sp;
                   9603:     ND spol;
                   9604:     mpz_t **spmat;
                   9605:     mpz_t *svect,*v;
                   9606:     int *colstat;
                   9607:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9608:     int maxrs;
                   9609:     int *spsugar;
                   9610:     pointer *w;
                   9611:
                   9612:     spcol = col-nred;
                   9613:     get_eg(&eg0);
                   9614:     /* elimination (1st step) */
                   9615:     spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
                   9616:     svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   9617:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9618:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9619:         nd_sp(m,trace,sp,&spol);
                   9620:         if ( !spol ) continue;
                   9621:         nd_to_vect_lf(s0vect,col,spol,svect);
                   9622:         maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
                   9623:         for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
                   9624:         if ( i < col ) {
                   9625:             spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
                   9626:             for ( j = k = 0; j < col; j++ )
                   9627:                 if ( !rhead[j] ) v[k++][0] = svect[j][0];
                   9628:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9629:             sprow++;
                   9630:         }
                   9631: /*        nd_free(spol); */
                   9632:     }
                   9633:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9634:     if ( DP_Print ) {
1.5       noro     9635:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9636:         fflush(asir_out);
                   9637:     }
                   9638:     /* free index arrays */
                   9639: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   9640:
                   9641:     /* elimination (2nd step) */
                   9642:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9643:     rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
                   9644:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   9645:     for ( i = 0; i < rank; i++ ) {
                   9646: #if 0
                   9647:         w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   9648:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   9649: #else
                   9650:         w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   9651:         SG((NDV)w[i]) = spsugar[i];
                   9652: #endif
                   9653: /*        GCFREE(spmat[i]); */
                   9654:
                   9655:     }
                   9656: #if 0
                   9657:     qsort(w,rank,sizeof(NDV),
                   9658:         (int (*)(const void *,const void *))ndv_compare);
                   9659: #endif
                   9660:     r0 = 0;
                   9661:     for ( i = 0; i < rank; i++ ) {
                   9662:         NEXTNODE(r0,r); BDY(r) = w[i];
                   9663:     }
                   9664:     if ( r0 ) NEXT(r) = 0;
                   9665:
                   9666: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   9667:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9668:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9669:     if ( DP_Print ) {
1.5       noro     9670:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9671:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9672:             nsp,nred,sprow,spcol,rank);
1.5       noro     9673:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9674:     }
                   9675:     return r0;
                   9676: }
                   9677:
                   9678: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   9679:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   9680: {
                   9681:     int spcol,sprow,a;
                   9682:     int i,j,k,l,rank;
                   9683:     NODE r0,r;
                   9684:     ND_pairs sp;
                   9685:     ND spol;
                   9686:     Z **spmat;
                   9687:     Z *svect,*v;
                   9688:     int *colstat;
                   9689:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9690:     int maxrs;
                   9691:     int *spsugar;
                   9692:     pointer *w;
                   9693:
                   9694:     spcol = col-nred;
                   9695:     get_eg(&eg0);
                   9696:     /* elimination (1st step) */
                   9697:     spmat = (Z **)MALLOC(nsp*sizeof(Q *));
                   9698:     svect = (Z *)MALLOC(col*sizeof(Q));
                   9699:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9700:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9701:         nd_sp(0,trace,sp,&spol);
                   9702:         if ( !spol ) continue;
                   9703:         nd_to_vect_q(s0vect,col,spol,svect);
                   9704:         maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
                   9705:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9706:         if ( i < col ) {
                   9707:             spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
                   9708:             for ( j = k = 0; j < col; j++ )
                   9709:                 if ( !rhead[j] ) v[k++] = svect[j];
                   9710:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9711:             sprow++;
                   9712:         }
                   9713: /*        nd_free(spol); */
                   9714:     }
                   9715:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9716:     if ( DP_Print ) {
1.5       noro     9717:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9718:         fflush(asir_out);
                   9719:     }
                   9720:     /* free index arrays */
                   9721: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   9722:
                   9723:     /* elimination (2nd step) */
                   9724:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9725:     rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
                   9726:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   9727:     for ( i = 0; i < rank; i++ ) {
                   9728: #if 0
                   9729:         w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   9730:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   9731: #else
                   9732:         w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   9733:         SG((NDV)w[i]) = spsugar[i];
                   9734: #endif
                   9735: /*        GCFREE(spmat[i]); */
                   9736:     }
                   9737: #if 0
                   9738:     qsort(w,rank,sizeof(NDV),
                   9739:         (int (*)(const void *,const void *))ndv_compare);
                   9740: #endif
                   9741:     r0 = 0;
                   9742:     for ( i = 0; i < rank; i++ ) {
                   9743:         NEXTNODE(r0,r); BDY(r) = w[i];
                   9744:     }
                   9745:     if ( r0 ) NEXT(r) = 0;
                   9746:
                   9747: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   9748:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9749:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9750:     if ( DP_Print ) {
1.5       noro     9751:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9752:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9753:             nsp,nred,sprow,spcol,rank);
1.5       noro     9754:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9755:     }
                   9756:     return r0;
                   9757: }
                   9758:
                   9759: FILE *nd_write,*nd_read;
                   9760:
                   9761: void nd_send_int(int a) {
                   9762:     write_int(nd_write,(unsigned int *)&a);
                   9763: }
                   9764:
                   9765: void nd_send_intarray(int *p,int len) {
                   9766:     write_intarray(nd_write,(unsigned int *)p,len);
                   9767: }
                   9768:
                   9769: int nd_recv_int() {
                   9770:     int a;
                   9771:
                   9772:     read_int(nd_read,(unsigned int *)&a);
                   9773:     return a;
                   9774: }
                   9775:
                   9776: void nd_recv_intarray(int *p,int len) {
                   9777:     read_intarray(nd_read,(unsigned int *)p,len);
                   9778: }
                   9779:
                   9780: void nd_send_ndv(NDV p) {
                   9781:     int len,i;
                   9782:     NMV m;
                   9783:
                   9784:     if ( !p ) nd_send_int(0);
                   9785:     else {
                   9786:         len = LEN(p);
                   9787:         nd_send_int(len);
                   9788:         m = BDY(p);
                   9789:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9790:             nd_send_int(CM(m));
                   9791:             nd_send_intarray((int *)DL(m),nd_wpd);
                   9792:         }
                   9793:     }
                   9794: }
                   9795:
                   9796: void nd_send_nd(ND p) {
                   9797:     int len,i;
                   9798:     NM m;
                   9799:
                   9800:     if ( !p ) nd_send_int(0);
                   9801:     else {
                   9802:         len = LEN(p);
                   9803:         nd_send_int(len);
                   9804:         m = BDY(p);
                   9805:         for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   9806:             nd_send_int(CM(m));
                   9807:             nd_send_intarray((int *)DL(m),nd_wpd);
                   9808:         }
                   9809:     }
                   9810: }
                   9811:
                   9812: NDV nd_recv_ndv()
                   9813: {
                   9814:     int len,i;
                   9815:     NMV m,m0;
                   9816:     NDV r;
                   9817:
                   9818:     len = nd_recv_int();
                   9819:     if ( !len ) return 0;
                   9820:     else {
                   9821:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   9822: #if 0
                   9823:         ndv_alloc += len*nmv_adv;
                   9824: #endif
                   9825:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9826:             CM(m) = nd_recv_int();
                   9827:             nd_recv_intarray((int *)DL(m),nd_wpd);
                   9828:         }
                   9829:         MKNDV(nd_nvar,m0,len,r);
                   9830:         return r;
                   9831:     }
                   9832: }
                   9833:
                   9834: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
                   9835: {
                   9836:     int i,j,t,c,rank,inv;
                   9837:     int *ci,*ri;
                   9838:     Z dn;
                   9839:     MAT m,nm;
                   9840:
                   9841:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
                   9842:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   9843:     for ( i = 0; i < row; i++ )
                   9844:         for ( j = 0; j < col; j++ )
                   9845:             mat0[i][j] = 0;
                   9846:     c = col-rank;
                   9847:     for ( i = 0; i < rank; i++ ) {
                   9848:         mat0[i][ri[i]] = dn;
                   9849:         for ( j = 0; j < c; j++ )
                   9850:             mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
                   9851:     }
                   9852:     return rank;
                   9853: }
                   9854:
                   9855: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   9856: {
                   9857:     int i,j,k,l,inv,a,rank,s;
                   9858:     unsigned int *t,*pivot,*pk;
                   9859:     unsigned int **mat;
                   9860:     ND_pairs pair;
                   9861:
                   9862:     mat = (unsigned int **)mat0;
                   9863:     for ( rank = 0, j = 0; j < col; j++ ) {
                   9864:         for ( i = rank; i < row; i++ )
                   9865:             mat[i][j] %= md;
                   9866:         for ( i = rank; i < row; i++ )
                   9867:             if ( mat[i][j] )
                   9868:                 break;
                   9869:         if ( i == row ) {
                   9870:             colstat[j] = 0;
                   9871:             continue;
                   9872:         } else
                   9873:             colstat[j] = 1;
                   9874:         if ( i != rank ) {
                   9875:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   9876:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   9877:             if ( spactive ) {
                   9878:                 pair = spactive[i]; spactive[i] = spactive[rank];
                   9879:                 spactive[rank] = pair;
                   9880:             }
                   9881:         }
                   9882:         pivot = mat[rank];
                   9883:         s = sugar[rank];
                   9884:         inv = invm(pivot[j],md);
                   9885:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   9886:             if ( *pk ) {
                   9887:                 if ( *pk >= (unsigned int)md )
                   9888:                     *pk %= md;
                   9889:                 DMAR(*pk,inv,0,md,*pk)
                   9890:             }
                   9891:         for ( i = rank+1; i < row; i++ ) {
                   9892:             t = mat[i];
                   9893:             if ( (a = t[j]) != 0 ) {
                   9894:                 sugar[i] = MAX(sugar[i],s);
                   9895:                 red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   9896:             }
                   9897:         }
                   9898:         rank++;
                   9899:     }
                   9900:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   9901:         if ( colstat[j] ) {
                   9902:             pivot = mat[l];
                   9903:             s = sugar[l];
                   9904:             for ( i = 0; i < l; i++ ) {
                   9905:                 t = mat[i];
                   9906:                 t[j] %= md;
                   9907:                 if ( (a = t[j]) != 0 ) {
                   9908:                     sugar[i] = MAX(sugar[i],s);
                   9909:                     red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   9910:                 }
                   9911:             }
                   9912:             l--;
                   9913:         }
                   9914:     for ( j = 0, l = 0; l < rank; j++ )
                   9915:         if ( colstat[j] ) {
                   9916:             t = mat[l];
                   9917:             for ( k = j; k < col; k++ )
                   9918:                 if ( t[k] >= (unsigned int)md )
                   9919:                     t[k] %= md;
                   9920:             l++;
                   9921:         }
                   9922:     return rank;
                   9923: }
                   9924:
1.40      noro     9925: int nd_gauss_elim_mod_s(UINT **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat,SIG *sig)
                   9926: {
                   9927:   int i,j,k,l,rank,s,imin;
                   9928:   UINT inv;
                   9929:   UINT a;
                   9930:   UINT *t,*pivot,*pk;
                   9931:   UINT *ck;
                   9932:   UINT *ct;
                   9933:   ND_pairs pair;
                   9934:   SIG sg;
                   9935:   int *used;
                   9936:
                   9937:   used = (int *)MALLOC(row*sizeof(int));
                   9938:   for ( j = 0; j < col; j++ ) {
                   9939:     for ( i = 0; i < row; i++ )
                   9940:       a = mat[i][j] %= md;
                   9941:     for ( i = 0; i < row; i++ )
                   9942:       if ( !used[i] && mat[i][j] ) break;
                   9943:     if ( i == row ) {
                   9944:       colstat[j] = 0;
                   9945:       continue;
                   9946:     } else {
                   9947:       colstat[j] = 1;
                   9948:       used[i] = 1;
                   9949:     }
                   9950:     /* column j is normalized */
                   9951:     s = sugar[i];
                   9952:     inv = invm(mat[i][j],md);
                   9953:     /* normalize pivot row */
                   9954:     for ( k = j, pk = mat[i]+j; k < col; k++, pk++, ck++ ) {
                   9955:       DMAR(*pk,inv,0,md,*pk);
                   9956:     }
                   9957:     for ( k = i+1; k < row; k++ ) {
                   9958:       if ( (a = mat[k][j]) != 0 ) {
                   9959:         sugar[k] = MAX(sugar[k],s);
                   9960:         red_by_vect(md,mat[k]+j,mat[i]+j,(int)(md-a),col-j);
                   9961:         Nf4_red++;
                   9962:       }
                   9963:     }
                   9964:   }
                   9965:   rank = 0;
                   9966:   for ( i = 0; i < row; i++ ) {
                   9967:     for ( j = 0; j < col; j++ )
                   9968:       if ( mat[i][j] ) break;
                   9969:     if ( j == col ) sugar[i] = -1;
                   9970:     else rank++;
                   9971:   }
                   9972:   return rank;
                   9973: }
                   9974:
1.1       noro     9975:
1.7       noro     9976: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1       noro     9977: {
1.7       noro     9978:     int i,j,k,l,inv,a,rank,s;
                   9979:     unsigned int *t,*pivot,*pk;
                   9980:     unsigned int **mat;
                   9981:
                   9982:     mat = (unsigned int **)mat0;
                   9983:     for ( rank = 0, j = 0; j < col; j++ ) {
                   9984:         for ( i = rank; i < row; i++ )
                   9985:             if ( mat[i][j] )
                   9986:                 break;
                   9987:         if ( i == row ) {
                   9988:             colstat[j] = 0;
                   9989:             continue;
                   9990:         } else
                   9991:             colstat[j] = 1;
                   9992:         if ( i != rank ) {
                   9993:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   9994:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   9995:         }
                   9996:         pivot = mat[rank];
                   9997:         s = sugar[rank];
                   9998:         inv = _invsf(pivot[j]);
                   9999:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   10000:             if ( *pk )
                   10001:                 *pk = _mulsf(*pk,inv);
                   10002:         for ( i = rank+1; i < row; i++ ) {
                   10003:             t = mat[i];
                   10004:             if ( (a = t[j]) != 0 ) {
                   10005:                 sugar[i] = MAX(sugar[i],s);
                   10006:                 red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   10007:             }
                   10008:         }
                   10009:         rank++;
                   10010:     }
                   10011:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   10012:         if ( colstat[j] ) {
                   10013:             pivot = mat[l];
                   10014:             s = sugar[l];
                   10015:             for ( i = 0; i < l; i++ ) {
                   10016:                 t = mat[i];
                   10017:                 if ( (a = t[j]) != 0 ) {
                   10018:                     sugar[i] = MAX(sugar[i],s);
                   10019:                     red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   10020:                 }
                   10021:             }
                   10022:             l--;
                   10023:         }
                   10024:     return rank;
                   10025: }
1.1       noro     10026:
1.7       noro     10027: int ndv_ishomo(NDV p)
                   10028: {
                   10029:     NMV m;
                   10030:     int len,h;
1.1       noro     10031:
                   10032:     if ( !p ) return 1;
                   10033:     len = LEN(p);
                   10034:     m = BDY(p);
                   10035:     h = TD(DL(m));
                   10036:     NMV_ADV(m);
                   10037:     for ( len--; len; len--, NMV_ADV(m) )
1.20      noro     10038:         if ( TD(DL(m)) != h ) {
                   10039:           return 0;
                   10040:         }
1.1       noro     10041:     return 1;
                   10042: }
                   10043:
                   10044: void ndv_save(NDV p,int index)
                   10045: {
                   10046:     FILE *s;
                   10047:     char name[BUFSIZ];
                   10048:     short id;
                   10049:     int nv,sugar,len,n,i,td,e,j;
                   10050:     NMV m;
                   10051:     unsigned int *dl;
                   10052:     int mpos;
                   10053:
                   10054:     sprintf(name,"%s/%d",Demand,index);
                   10055:     s = fopen(name,"w");
                   10056:     savevl(s,0);
                   10057:     if ( !p ) {
                   10058:         saveobj(s,0);
                   10059:         return;
                   10060:     }
                   10061:     id = O_DP;
                   10062:     nv = NV(p);
                   10063:     sugar = SG(p);
                   10064:     len = LEN(p);
                   10065:     write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
                   10066:     write_int(s,(unsigned int *)&len);
                   10067:
                   10068:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     10069:         saveobj(s,(Obj)CZ(m));
1.1       noro     10070:         dl = DL(m);
                   10071:         td = TD(dl);
                   10072:         write_int(s,(unsigned int *)&td);
                   10073:         for ( j = 0; j < nv; j++ ) {
                   10074:             e = GET_EXP(dl,j);
                   10075:             write_int(s,(unsigned int *)&e);
                   10076:         }
                   10077:         if ( nd_module ) {
                   10078:             mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
                   10079:         }
                   10080:     }
                   10081:     fclose(s);
                   10082: }
                   10083:
                   10084: void nd_save_mod(ND p,int index)
                   10085: {
                   10086:     FILE *s;
                   10087:     char name[BUFSIZ];
                   10088:     int nv,sugar,len,c;
                   10089:     NM m;
                   10090:
                   10091:     sprintf(name,"%s/%d",Demand,index);
                   10092:     s = fopen(name,"w");
                   10093:     if ( !p ) {
                   10094:     len = 0;
                   10095:       write_int(s,(unsigned int *)&len);
                   10096:     fclose(s);
                   10097:         return;
                   10098:     }
                   10099:     nv = NV(p);
                   10100:     sugar = SG(p);
                   10101:     len = LEN(p);
                   10102:     write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
                   10103:   for ( m = BDY(p); m; m = NEXT(m) ) {
                   10104:     c = CM(m); write_int(s,(unsigned int *)&c);
                   10105:     write_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   10106:   }
                   10107:     fclose(s);
                   10108: }
                   10109:
                   10110: NDV ndv_load(int index)
                   10111: {
                   10112:     FILE *s;
                   10113:     char name[BUFSIZ];
                   10114:     short id;
                   10115:     int nv,sugar,len,n,i,td,e,j;
                   10116:     NDV d;
                   10117:     NMV m0,m;
                   10118:     unsigned int *dl;
                   10119:     Obj obj;
                   10120:     int mpos;
                   10121:
                   10122:     sprintf(name,"%s/%d",Demand,index);
                   10123:     s = fopen(name,"r");
                   10124:     if ( !s ) return 0;
                   10125:
                   10126:     skipvl(s);
                   10127:     read_short(s,(unsigned short *)&id);
                   10128:     if ( !id ) return 0;
                   10129:     read_int(s,(unsigned int *)&nv);
                   10130:     read_int(s,(unsigned int *)&sugar);
                   10131:     read_int(s,(unsigned int *)&len);
                   10132:
                   10133:     m0 = m = MALLOC(len*nmv_adv);
                   10134:     for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     10135:         loadobj(s,&obj); CZ(m) = (Z)obj;
1.1       noro     10136:         dl = DL(m);
                   10137:         ndl_zero(dl);
                   10138:         read_int(s,(unsigned int *)&td); TD(dl) = td;
                   10139:         for ( j = 0; j < nv; j++ ) {
                   10140:             read_int(s,(unsigned int *)&e);
                   10141:             PUT_EXP(dl,j,e);
                   10142:         }
                   10143:         if ( nd_module ) {
                   10144:             read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
                   10145:         }
                   10146:         if ( nd_blockmask ) ndl_weight_mask(dl);
                   10147:     }
                   10148:     fclose(s);
                   10149:     MKNDV(nv,m0,len,d);
                   10150:     SG(d) = sugar;
                   10151:     return d;
                   10152: }
                   10153:
                   10154: ND nd_load_mod(int index)
                   10155: {
                   10156:     FILE *s;
                   10157:     char name[BUFSIZ];
                   10158:     int nv,sugar,len,i,c;
                   10159:   ND d;
                   10160:     NM m0,m;
                   10161:
                   10162:     sprintf(name,"%s/%d",Demand,index);
                   10163:     s = fopen(name,"r");
                   10164:   /* if the file does not exist, it means p[index]=0 */
                   10165:     if ( !s ) return 0;
                   10166:
                   10167:     read_int(s,(unsigned int *)&nv);
                   10168:   if ( !nv ) { fclose(s); return 0; }
                   10169:
                   10170:     read_int(s,(unsigned int *)&sugar);
                   10171:     read_int(s,(unsigned int *)&len);
                   10172:   for ( m0 = 0, i = 0; i < len; i++ ) {
                   10173:     NEXTNM(m0,m);
                   10174:     read_int(s,(unsigned int *)&c); CM(m) = c;
                   10175:     read_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   10176:   }
                   10177:   NEXT(m) = 0;
                   10178:     MKND(nv,m0,len,d);
                   10179:     SG(d) = sugar;
                   10180:   fclose(s);
                   10181:     return d;
                   10182: }
                   10183:
                   10184: void nd_det(int mod,MAT f,P *rp)
                   10185: {
                   10186:     VL fv,tv;
                   10187:     int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
                   10188:     pointer **m;
                   10189:     P **w;
                   10190:     P mp,r;
                   10191:     NDV **dm;
                   10192:     NDV *t,*mi,*mj;
                   10193:     NDV d,s,mij,mjj;
                   10194:     ND u;
                   10195:     NMV nmv;
                   10196:     UINT *bound;
                   10197:     PGeoBucket bucket;
                   10198:     struct order_spec *ord;
                   10199:     Z dq,dt,ds;
                   10200:     Z mone;
                   10201:     Z gn,qn,dn0,nm,dn;
                   10202:
                   10203:     create_order_spec(0,0,&ord);
                   10204:     nd_init_ord(ord);
                   10205:     get_vars((Obj)f,&fv);
                   10206:     if ( f->row != f->col )
                   10207:         error("nd_det : non-square matrix");
                   10208:     n = f->row;
                   10209:     m = f->body;
                   10210:     for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   10211:
                   10212:     if ( !nvar ) {
                   10213:         if ( !mod )
                   10214:             detp(CO,(P **)m,n,rp);
                   10215:         else {
                   10216:             w = (P **)almat_pointer(n,n);
                   10217:             for ( i = 0; i < n; i++ )
                   10218:                 for ( j = 0; j < n; j++ )
                   10219:                     ptomp(mod,(P)m[i][j],&w[i][j]);
                   10220:             detmp(CO,mod,w,n,&mp);
                   10221:             mptop(mp,rp);
                   10222:         }
                   10223:         return;
                   10224:     }
                   10225:
                   10226:     if ( !mod ) {
                   10227:         w = (P **)almat_pointer(n,n);
                   10228:         dq = ONE;
                   10229:         for ( i = 0; i < n; i++ ) {
                   10230:             dn0 = ONE;
                   10231:             for ( j = 0; j < n; j++ ) {
                   10232:                 if ( !m[i][j] ) continue;
                   10233:                 lgp(m[i][j],&nm,&dn);
1.6       noro     10234:                 gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1       noro     10235:             }
                   10236:             if ( !UNIZ(dn0) ) {
                   10237:                 ds = dn0;
                   10238:                 for ( j = 0; j < n; j++ )
                   10239:                     mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   10240:                 mulz(dq,ds,&dt); dq = dt;
                   10241:             } else
                   10242:                 for ( j = 0; j < n; j++ )
                   10243:                     w[i][j] = (P)m[i][j];
                   10244:         }
                   10245:         m = (pointer **)w;
                   10246:     }
                   10247:
                   10248:     for ( i = 0, max = 1; i < n; i++ )
                   10249:         for ( j = 0; j < n; j++ )
                   10250:             for ( tv = fv; tv; tv = NEXT(tv) ) {
                   10251:                 e = getdeg(tv->v,(P)m[i][j]);
                   10252:                 max = MAX(e,max);
                   10253:             }
                   10254:     nd_setup_parameters(nvar,max);
                   10255:     dm = (NDV **)almat_pointer(n,n);
                   10256:     for ( i = 0, max = 1; i < n; i++ )
                   10257:         for ( j = 0; j < n; j++ ) {
                   10258:             dm[i][j] = ptondv(CO,fv,m[i][j]);
                   10259:             if ( mod ) ndv_mod(mod,dm[i][j]);
                   10260:             if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   10261:         }
                   10262:     d = ptondv(CO,fv,(P)ONE);
                   10263:     if ( mod ) ndv_mod(mod,d);
                   10264:     chsgnz(ONE,&mone);
                   10265:     for ( j = 0, sgn = 1; j < n; j++ ) {
                   10266:       if ( DP_Print ) {
                   10267:         fprintf(asir_out,".");
                   10268:       }
                   10269:         for ( i = j; i < n && !dm[i][j]; i++ );
                   10270:         if ( i == n ) {
                   10271:             *rp = 0;
                   10272:             return;
                   10273:         }
                   10274:         k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
                   10275:         for ( k = j; k < n; k++ )
                   10276:             for ( l = j; l < n; l++ )
                   10277:                 if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   10278:                     k0 = k; l0 = l; len0 = LEN(dm[k][l]);
                   10279:                 }
                   10280:         if ( k0 != j ) {
                   10281:             t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   10282:             sgn = -sgn;
                   10283:         }
                   10284:         if ( l0 != j ) {
                   10285:             for ( k = j; k < n; k++ ) {
                   10286:                 s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   10287:             }
                   10288:             sgn = -sgn;
                   10289:         }
                   10290:         bound = nd_det_compute_bound(dm,n,j);
                   10291:         for ( k = 0; k < nd_nvar; k++ )
                   10292:             if ( bound[k]*2 > nd_mask0 ) break;
                   10293:         if ( k < nd_nvar )
                   10294:             nd_det_reconstruct(dm,n,j,d);
                   10295:
                   10296:         for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
                   10297: /*            if ( DP_Print ) fprintf(asir_out,"    i=%d\n        ",i); */
                   10298:             mi = dm[i]; mij = mi[j];
                   10299:             if ( mod )
                   10300:                 ndv_mul_c(mod,mij,mod-1);
                   10301:             else
                   10302:                 ndv_mul_c_q(mij,mone);
                   10303:             for ( k = j+1; k < n; k++ ) {
                   10304: /*                if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
                   10305:                 bucket = create_pbucket();
                   10306:                 if ( mi[k] ) {
                   10307:                     nmv = BDY(mjj); len = LEN(mjj);
                   10308:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   10309:                         u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   10310:                         add_pbucket(mod,bucket,u);
                   10311:                     }
                   10312:                 }
                   10313:                 if ( mj[k] && mij ) {
                   10314:                     nmv = BDY(mij); len = LEN(mij);
                   10315:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   10316:                         u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   10317:                         add_pbucket(mod,bucket,u);
                   10318:                     }
                   10319:                 }
                   10320:                 u = nd_quo(mod,bucket,d);
                   10321:                 mi[k] = ndtondv(mod,u);
                   10322:             }
                   10323: /*            if ( DP_Print ) fprintf(asir_out,"\n",k); */
                   10324:         }
                   10325:         d = mjj;
                   10326:     }
                   10327:     if ( DP_Print ) {
                   10328:       fprintf(asir_out,"\n");
                   10329:     }
                   10330:     if ( sgn < 0 ) {
                   10331:         if ( mod )
                   10332:             ndv_mul_c(mod,d,mod-1);
                   10333:         else
                   10334:             ndv_mul_c_q(d,mone);
                   10335:     }
                   10336:     r = ndvtop(mod,CO,fv,d);
                   10337:     if ( !mod && !UNIQ(dq) )
                   10338:         divsp(CO,r,(P)dq,rp);
                   10339:     else
                   10340:         *rp = r;
                   10341: }
                   10342:
                   10343: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
                   10344: {
                   10345:     NM mr,mr0;
                   10346:     NM tnm;
                   10347:     NMV m;
                   10348:     UINT *d0,*dt,*dm;
                   10349:     int c,n,td,i,c1,c2,len;
                   10350:     Z q;
                   10351:     ND r;
                   10352:
                   10353:     if ( !p ) return 0;
                   10354:     else {
                   10355:         n = NV(p); m = BDY(p); len = LEN(p);
                   10356:         d0 = DL(m0);
                   10357:         td = TD(d);
                   10358:         mr0 = 0;
                   10359:         NEWNM(tnm);
                   10360:         if ( mod ) {
                   10361:             c = CM(m0);
                   10362:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   10363:                 ndl_add(DL(m),d0,DL(tnm));
                   10364:                 if ( ndl_reducible(DL(tnm),d) ) {
                   10365:                     NEXTNM(mr0,mr);
                   10366:                     c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
                   10367:                     ndl_copy(DL(tnm),DL(mr));
                   10368:                 }
                   10369:             }
                   10370:         } else {
1.6       noro     10371:             q = CZ(m0);
1.1       noro     10372:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   10373:                 ndl_add(DL(m),d0,DL(tnm));
                   10374:                 if ( ndl_reducible(DL(tnm),d) ) {
                   10375:                     NEXTNM(mr0,mr);
1.6       noro     10376:                     mulz(CZ(m),q,&CZ(mr));
1.1       noro     10377:                     ndl_copy(DL(tnm),DL(mr));
                   10378:                 }
                   10379:             }
                   10380:         }
                   10381:         if ( !mr0 )
                   10382:             return 0;
                   10383:         else {
                   10384:             NEXT(mr) = 0;
                   10385:             for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   10386:             MKND(NV(p),mr0,len,r);
                   10387:             SG(r) = SG(p) + TD(d0);
                   10388:             return r;
                   10389:         }
                   10390:     }
                   10391: }
                   10392:
                   10393: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   10394: {
                   10395:     int i,obpe,oadv,h,k,l;
                   10396:     static NM prev_nm_free_list;
                   10397:     EPOS oepos;
                   10398:
                   10399:     obpe = nd_bpe;
                   10400:     oadv = nmv_adv;
                   10401:     oepos = nd_epos;
                   10402:     if ( obpe < 2 ) nd_bpe = 2;
                   10403:     else if ( obpe < 3 ) nd_bpe = 3;
                   10404:     else if ( obpe < 4 ) nd_bpe = 4;
                   10405:     else if ( obpe < 5 ) nd_bpe = 5;
                   10406:     else if ( obpe < 6 ) nd_bpe = 6;
                   10407:     else if ( obpe < 8 ) nd_bpe = 8;
                   10408:     else if ( obpe < 10 ) nd_bpe = 10;
                   10409:     else if ( obpe < 16 ) nd_bpe = 16;
                   10410:     else if ( obpe < 32 ) nd_bpe = 32;
                   10411:     else error("nd_det_reconstruct : exponent too large");
                   10412:
                   10413:     nd_setup_parameters(nd_nvar,0);
                   10414:     prev_nm_free_list = _nm_free_list;
                   10415:     _nm_free_list = 0;
                   10416:     for ( k = j; k < n; k++ )
                   10417:         for (l = j; l < n; l++ )
                   10418:             ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   10419:     ndv_realloc(d,obpe,oadv,oepos);
                   10420:     prev_nm_free_list = 0;
                   10421: #if 0
                   10422:     GC_gcollect();
                   10423: #endif
                   10424: }
                   10425:
                   10426: /* returns a UINT array containing degree bounds */
                   10427:
                   10428: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   10429: {
                   10430:     UINT *d0,*d1,*d,*t,*r;
                   10431:     int k,l,i;
                   10432:
                   10433:     d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
                   10434:     for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
                   10435:     for ( k = j; k < n; k++ )
                   10436:         for ( l = j; l < n; l++ )
                   10437:             if ( dm[k][l] ) {
                   10438:                 d = ndv_compute_bound(dm[k][l]);
                   10439:                 for ( i = 0; i < nd_nvar; i++ )
                   10440:                     d0[i] = MAX(d0[i],d[i]);
                   10441:             }
                   10442:     return d0;
                   10443: }
                   10444:
                   10445: DL nd_separate_d(UINT *d,UINT *trans)
                   10446: {
                   10447:     int n,td,i,e,j;
                   10448:     DL a;
                   10449:
                   10450:     ndl_zero(trans);
                   10451:     td = 0;
                   10452:     for ( i = 0; i < nd_ntrans; i++ ) {
                   10453:         e = GET_EXP(d,i);
                   10454:         PUT_EXP(trans,i,e);
                   10455:         td += MUL_WEIGHT(e,i);
                   10456:     }
                   10457:     if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   10458:         /* homogenized */
                   10459:         i = nd_nvar-1;
                   10460:         e = GET_EXP(d,i);
                   10461:         PUT_EXP(trans,i,e);
                   10462:         td += MUL_WEIGHT(e,i);
                   10463:     }
                   10464:     TD(trans) = td;
                   10465:     if ( nd_blockmask) ndl_weight_mask(trans);
                   10466:     NEWDL(a,nd_nalg);
                   10467:     td = 0;
                   10468:     for ( i = 0; i < nd_nalg; i++ ) {
                   10469:         j = nd_ntrans+i;
                   10470:         e = GET_EXP(d,j);
                   10471:         a->d[i] = e;
                   10472:         td += e;
                   10473:     }
                   10474:     a->td = td;
                   10475:     return a;
                   10476: }
                   10477:
                   10478: int nd_monic(int mod,ND *p)
                   10479: {
                   10480:     UINT *trans,*t;
                   10481:     DL alg;
                   10482:     MP mp0,mp;
                   10483:     NM m,m0,m1,ma0,ma,mb,mr0,mr;
                   10484:     ND r;
                   10485:     DL dl;
                   10486:     DP nm;
                   10487:     NDV ndv;
                   10488:     DAlg inv,cd;
                   10489:     ND s,c;
                   10490:     Z l,mul;
                   10491:     Z ln;
                   10492:     int n,ntrans,i,e,td,is_lc,len;
                   10493:     NumberField nf;
                   10494:     struct oEGT eg0,eg1;
                   10495:
                   10496:     if ( !(nf = get_numberfield()) )
                   10497:         error("nd_monic : current_numberfield is not set");
                   10498:
                   10499:     /* Q coef -> DAlg coef */
                   10500:     NEWNM(ma0); ma = ma0;
                   10501:     m = BDY(*p);
                   10502:     is_lc = 1;
                   10503:     while ( 1 ) {
                   10504:         NEWMP(mp0); mp = mp0;
1.6       noro     10505:         mp->c = (Obj)CZ(m);
1.1       noro     10506:         mp->dl = nd_separate_d(DL(m),DL(ma));
                   10507:         NEWNM(mb);
                   10508:         for ( m = NEXT(m); m; m = NEXT(m) ) {
                   10509:             alg = nd_separate_d(DL(m),DL(mb));
                   10510:             if ( !ndl_equal(DL(ma),DL(mb)) )
                   10511:                 break;
1.6       noro     10512:             NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1       noro     10513:         }
                   10514:         NEXT(mp) = 0;
                   10515:         MKDP(nd_nalg,mp0,nm);
                   10516:         MKDAlg(nm,ONE,cd);
                   10517:         if ( is_lc == 1 ) {
                   10518:             /* if the lc is a rational number, we have nothing to do */
                   10519:             if ( !mp0->dl->td )
                   10520:                 return 1;
                   10521:
                   10522:             get_eg(&eg0);
                   10523:             invdalg(cd,&inv);
                   10524:             get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   10525:             /* check the validity of inv */
                   10526:             if ( mod && !remqi((Q)inv->dn,mod) )
                   10527:                 return 0;
                   10528:             CA(ma) = nf->one;
                   10529:             is_lc = 0;
                   10530:             ln = ONE;
                   10531:         } else {
                   10532:             muldalg(cd,inv,&CA(ma));
                   10533:             lcmz(ln,CA(ma)->dn,&ln);
                   10534:         }
                   10535:         if ( m ) {
                   10536:             NEXT(ma) = mb; ma = mb;
                   10537:         } else {
                   10538:             NEXT(ma) = 0;
                   10539:             break;
                   10540:         }
                   10541:     }
                   10542:     /* l = lcm(denoms) */
                   10543:     l = ln;
                   10544:     for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6       noro     10545:         divsz(l,CA(m)->dn,&mul);
1.1       noro     10546:         for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   10547:             NEXTNM(mr0,mr);
1.6       noro     10548:             mulz((Z)mp->c,mul,&CZ(mr));
1.1       noro     10549:             dl = mp->dl;
                   10550:             td = TD(DL(m));
                   10551:             ndl_copy(DL(m),DL(mr));
                   10552:             for ( i = 0; i < nd_nalg; i++ ) {
                   10553:                 e = dl->d[i];
                   10554:                 PUT_EXP(DL(mr),i+nd_ntrans,e);
                   10555:                 td += MUL_WEIGHT(e,i+nd_ntrans);
                   10556:             }
                   10557:             if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
                   10558:             TD(DL(mr)) = td;
                   10559:             if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   10560:         }
                   10561:     }
                   10562:     NEXT(mr) = 0;
                   10563:     for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   10564:     MKND(NV(*p),mr0,len,r);
                   10565:     /* XXX */
                   10566:     SG(r) = SG(*p);
                   10567:     nd_free(*p);
                   10568:     *p = r;
                   10569:     return 1;
                   10570: }
                   10571:
                   10572: NODE reverse_node(NODE n)
                   10573: {
                   10574:     NODE t,t1;
                   10575:
                   10576:     for ( t = 0; n; n = NEXT(n) ) {
                   10577:         MKNODE(t1,BDY(n),t); t = t1;
                   10578:     }
                   10579:     return t;
                   10580: }
                   10581:
                   10582: P ndc_div(int mod,union oNDC a,union oNDC b)
                   10583: {
                   10584:     union oNDC c;
                   10585:     int inv,t;
                   10586:
                   10587:     if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10      noro     10588:     else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1       noro     10589:     else if ( mod ) {
                   10590:         inv = invm(b.m,mod);
                   10591:         DMAR(a.m,inv,0,mod,t); c.m = t;
                   10592:     } else if ( nd_vc )
                   10593:        divsp(nd_vc,a.p,b.p,&c.p);
                   10594:     else
                   10595:        divsz(a.z,b.z,&c.z);
                   10596:     return ndctop(mod,c);
                   10597: }
                   10598:
                   10599: P ndctop(int mod,union oNDC c)
                   10600: {
                   10601:     Z q;
                   10602:     int e;
                   10603:     GFS gfs;
                   10604:
                   10605:     if ( mod == -1 ) {
                   10606:         e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
                   10607:     } else if ( mod == -2 ) {
1.10      noro     10608:        q = c.z; return (P)q;
1.1       noro     10609:     } else if ( mod > 0 ) {
1.6       noro     10610:         STOZ(c.m,q); return (P)q;
1.1       noro     10611:     } else
                   10612:         return (P)c.p;
                   10613: }
                   10614:
                   10615: /* [0,0,0,cont] = p -> p/cont */
                   10616:
                   10617: void finalize_tracelist(int i,P cont)
                   10618: {
                   10619:  LIST l;
                   10620:  NODE node;
                   10621:  Z iq;
                   10622:
                   10623:   if ( !UNIQ(cont) ) {
                   10624:     node = mknode(4,NULLP,NULLP,NULLP,cont);
                   10625:     MKLIST(l,node); MKNODE(node,l,nd_tracelist);
                   10626:     nd_tracelist = node;
                   10627:   }
1.6       noro     10628:   STOZ(i,iq);
1.1       noro     10629:   nd_tracelist = reverse_node(nd_tracelist);
                   10630:   MKLIST(l,nd_tracelist);
                   10631:   node = mknode(2,iq,l); MKLIST(l,node);
                   10632:   MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
                   10633:   nd_alltracelist = node; nd_tracelist = 0;
                   10634: }
                   10635:
                   10636: void conv_ilist(int demand,int trace,NODE g,int **indp)
                   10637: {
                   10638:     int n,i,j;
                   10639:   int *ind;
                   10640:   NODE t;
                   10641:
                   10642:     n = length(g);
                   10643:   ind = (int *)MALLOC(n*sizeof(int));
                   10644:   for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
                   10645:     j = (long)BDY(t); ind[i] = j;
                   10646:     BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
                   10647:   }
                   10648:   if ( indp ) *indp = ind;
                   10649: }
                   10650:
1.30      noro     10651: NODE conv_ilist_s(int demand,int trace,int **indp)
                   10652: {
                   10653:   int n,i,j;
                   10654:   int *ind;
                   10655:   NODE g0,g;
                   10656:
                   10657:   n = nd_psn;
                   10658:   ind = (int *)MALLOC(n*sizeof(int));
                   10659:   g0 = 0;
                   10660:   for ( i = 0; i < n; i++ ) {
                   10661:     ind[i] = i;
                   10662:     NEXTNODE(g0,g);
                   10663:     BDY(g) = (pointer)(demand?ndv_load(i):(trace?nd_ps_trace[i]:nd_ps[i]));
                   10664:   }
                   10665:   if ( g0 ) NEXT(g) = 0;
                   10666:   if ( indp ) *indp = ind;
                   10667:   return g0;
                   10668: }
                   10669:
1.46      noro     10670: void parse_nd_option(VL vl,NODE opt)
1.1       noro     10671: {
1.38      noro     10672:   NODE t,p,u;
1.1       noro     10673:   int i,s,n;
1.38      noro     10674:   char *key;
                   10675:   Obj value;
1.46      noro     10676:   VL oldvl;
1.1       noro     10677:
1.41      noro     10678:   nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_norb = 0; nd_gbblock = 0;
1.1       noro     10679:   nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
                   10680:   nd_splist = 0; nd_check_splist = 0;
1.38      noro     10681:   nd_sugarweight = 0; nd_f4red =0; nd_rank0 = 0;
                   10682:   nd_f4_td = 0; nd_sba_f4step = 2; nd_sba_pot = 0; nd_sba_largelcm = 0;
1.41      noro     10683:   nd_sba_dontsort = 0; nd_top = 0; nd_sba_redundant_check = 0;
1.46      noro     10684:   nd_sba_syz = 0; nd_sba_modord = 0; nd_sba_inputisgb = 0;
1.63    ! noro     10685:   nd_hpdata = 0; nd_sba_heu = 0;
1.39      noro     10686:
1.38      noro     10687:   for ( t = opt; t; t = NEXT(t) ) {
                   10688:     p = BDY((LIST)BDY(t));
                   10689:     key = BDY((STRING)BDY(p));
                   10690:     value = (Obj)BDY(NEXT(p));
                   10691:     if ( !strcmp(key,"gentrace") )
                   10692:       nd_gentrace = value?1:0;
                   10693:     else if ( !strcmp(key,"gensyz") )
                   10694:       nd_gensyz = value?1:0;
                   10695:     else if ( !strcmp(key,"nora") )
                   10696:       nd_nora = value?1:0;
1.41      noro     10697:     else if ( !strcmp(key,"norb") )
                   10698:       nd_norb = value?1:0;
1.38      noro     10699:     else if ( !strcmp(key,"gbblock") ) {
                   10700:       if ( value && OID(value) == O_LIST ) {
1.1       noro     10701:         u = BDY((LIST)value);
1.38      noro     10702:         nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.1       noro     10703:         for ( i = 0; u; u = NEXT(u) ) {
                   10704:           p = BDY((LIST)BDY(u));
1.6       noro     10705:           s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
                   10706:           nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1       noro     10707:         }
                   10708:         nd_gbblock[i] = -1;
1.38      noro     10709:       } else
                   10710:         nd_gbblock = 0;
1.1       noro     10711:     } else if ( !strcmp(key,"newelim") )
                   10712:             nd_newelim = value?1:0;
                   10713:     else if ( !strcmp(key,"intersect") )
                   10714:             nd_intersect = value?1:0;
1.17      noro     10715:     else if ( !strcmp(key,"syzgen") )
                   10716:             nd_intersect = ZTOS((Q)value);
1.1       noro     10717:     else if ( !strcmp(key,"lf") )
                   10718:             nd_lf = value?1:0;
                   10719:     else if ( !strcmp(key,"trace") ) {
1.38      noro     10720:       if ( value ) {
                   10721:         u = BDY((LIST)value);
                   10722:         nd_nzlist = BDY((LIST)ARG2(u));
                   10723:         nd_bpe = ZTOS((Q)ARG3(u));
                   10724:       }
1.1       noro     10725:     } else if ( !strcmp(key,"f4red") ) {
1.38      noro     10726:       nd_f4red = ZTOS((Q)value);
1.1       noro     10727:     } else if ( !strcmp(key,"rank0") ) {
1.38      noro     10728:       nd_rank0 = value?1:0;
1.1       noro     10729:     } else if ( !strcmp(key,"splist") ) {
1.38      noro     10730:       nd_splist = value?1:0;
1.1       noro     10731:     } else if ( !strcmp(key,"check_splist") ) {
                   10732:       nd_check_splist = BDY((LIST)value);
1.49      noro     10733:     } else if ( !strcmp(key,"hpdata") ) {
1.50      noro     10734:       if ( value )
                   10735:         nd_hpdata = BDY((LIST)value);
1.1       noro     10736:     } else if ( !strcmp(key,"sugarweight") ) {
                   10737:       u = BDY((LIST)value);
1.38      noro     10738:       n = length(u);
                   10739:       nd_sugarweight = MALLOC(n*sizeof(int));
1.1       noro     10740:       for ( i = 0; i < n; i++, u = NEXT(u) )
1.38      noro     10741:         nd_sugarweight[i] = ZTOS((Q)BDY(u));
                   10742:     } else if ( !strcmp(key,"f4_td") ) {
                   10743:       nd_f4_td = value?1:0;
                   10744:     } else if ( !strcmp(key,"sba_f4step") ) {
                   10745:       nd_sba_f4step = value?ZTOS((Q)value):0;
                   10746:     } else if ( !strcmp(key,"sba_pot") ) {
1.44      noro     10747:       nd_sba_pot = ZTOS((Q)value);
1.38      noro     10748:     } else if ( !strcmp(key,"sba_largelcm") ) {
                   10749:       nd_sba_largelcm = value?1:0;
1.39      noro     10750:     } else if ( !strcmp(key,"sba_dontsort") ) {
                   10751:       nd_sba_dontsort = value?1:0;
1.44      noro     10752:     } else if ( !strcmp(key,"sba_syz") ) {
                   10753:       nd_sba_syz = value?1:0;
1.63    ! noro     10754:     } else if ( !strcmp(key,"sba_heu") ) {
        !          10755:       nd_sba_heu = value?1:0;
1.46      noro     10756:     } else if ( !strcmp(key,"sba_modord") ) {
                   10757:       // value=[vlist,ordspec,weight]
                   10758:       u = BDY((LIST)value);
                   10759:       pltovl((LIST)ARG0(u),&oldvl);
                   10760:       nd_sba_modord = create_comp_sig_spec(vl,oldvl,(Obj)ARG1(u),argc(u)==3?ARG2(u):0);
                   10761:     } else if ( !strcmp(key,"sba_gbinput") ) {
                   10762:       nd_sba_inputisgb = value?1:0;
                   10763:       if ( nd_sba_inputisgb != 0 ) {
                   10764:         // value=[vlist,ordspec,weight]
                   10765:         u = BDY((LIST)value);
                   10766:         pltovl((LIST)ARG0(u),&oldvl);
                   10767:         nd_sba_modord = create_comp_sig_spec(vl,oldvl,(Obj)ARG1(u),argc(u)==3?ARG2(u):0);
                   10768:       }
1.41      noro     10769:     } else if ( !strcmp(key,"sba_redundant_check") ) {
                   10770:       nd_sba_redundant_check = value?1:0;
1.39      noro     10771:     } else if ( !strcmp(key,"top") ) {
                   10772:       nd_top = value?1:0;
1.1       noro     10773:     }
1.38      noro     10774:   }
1.44      noro     10775:   if ( nd_sba_syz ) nd_sba_dontsort = 1;
1.1       noro     10776: }
                   10777:
                   10778: ND mdptond(DP d);
                   10779: ND nd_mul_nm(int mod,NM m0,ND p);
                   10780: ND nd_mul_nm_lf(NM m0,ND p);
                   10781: ND *btog(NODE ti,ND **p,int nb,int mod);
                   10782: ND btog_one(NODE ti,ND *p,int nb,int mod);
                   10783: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
                   10784: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
                   10785:
                   10786: /* d:monomial */
                   10787: ND mdptond(DP d)
                   10788: {
                   10789:   NM m;
                   10790:   ND r;
                   10791:
                   10792:   if ( OID(d) == 1 )
                   10793:   r = ptond(CO,CO,(P)d);
                   10794:   else {
                   10795:     NEWNM(m);
                   10796:     dltondl(NV(d),BDY(d)->dl,DL(m));
1.6       noro     10797:     CZ(m) = (Z)BDY(d)->c;
1.1       noro     10798:     NEXT(m) = 0;
                   10799:     MKND(NV(d),m,1,r);
                   10800:   }
                   10801:     return r;
                   10802: }
                   10803:
                   10804: ND nd_mul_nm(int mod,NM m0,ND p)
                   10805: {
                   10806:   UINT *d0;
                   10807:   int c0,c1,c;
                   10808:   NM tm,mr,mr0;
                   10809:   ND r;
                   10810:
                   10811:   if ( !p ) return 0;
                   10812:   d0 = DL(m0);
                   10813:   c0 = CM(m0);
                   10814:   mr0 = 0;
                   10815:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   10816:     NEXTNM(mr0,mr);
                   10817:   c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
                   10818:   ndl_add(d0,DL(tm),DL(mr));
                   10819:   }
                   10820:   NEXT(mr) = 0;
                   10821:   MKND(NV(p),mr0,LEN(p),r);
                   10822:   return r;
                   10823: }
                   10824:
                   10825: ND nd_mul_nm_lf(NM m0,ND p)
                   10826: {
                   10827:   UINT *d0;
                   10828:   Z c0,c1,c;
                   10829:   NM tm,mr,mr0;
                   10830:   ND r;
                   10831:
                   10832:   if ( !p ) return 0;
                   10833:   d0 = DL(m0);
                   10834:   c0 = CZ(m0);
                   10835:   mr0 = 0;
                   10836:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   10837:     NEXTNM(mr0,mr);
                   10838:     c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
                   10839:     ndl_add(d0,DL(tm),DL(mr));
                   10840:   }
                   10841:   NEXT(mr) = 0;
                   10842:   MKND(NV(p),mr0,LEN(p),r);
                   10843:   return r;
                   10844: }
                   10845:
                   10846: ND *btog(NODE ti,ND **p,int nb,int mod)
                   10847: {
                   10848:   PGeoBucket *r;
                   10849:   int i,ci;
                   10850:   NODE t,s;
                   10851:   ND m,tp;
                   10852:   ND *pi,*rd;
                   10853:   P c;
                   10854:
                   10855:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   10856:   for ( i = 0; i < nb; i++ )
                   10857:     r[i] = create_pbucket();
                   10858:   for ( t = ti; t; t = NEXT(t) ) {
                   10859:   s = BDY((LIST)BDY(t));
                   10860:     if ( ARG0(s) ) {
                   10861:     m = mdptond((DP)ARG2(s));
1.6       noro     10862:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     10863:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   10864:       HCM(m) = ci;
1.6       noro     10865:       pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     10866:       for ( i = 0; i < nb; i++ ) {
                   10867:       tp = nd_mul_nm(mod,BDY(m),pi[i]);
                   10868:         add_pbucket(mod,r[i],tp);
                   10869:       }
                   10870:     }
                   10871:     ci = 1;
                   10872:     } else {
                   10873:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   10874:     ci = invm(ci,mod);
                   10875:   }
                   10876:   }
                   10877:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   10878:   for ( i = 0; i < nb; i++ )
                   10879:   rd[i] = normalize_pbucket(mod,r[i]);
                   10880:   if ( ci != 1 )
                   10881:     for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
                   10882:    return rd;
                   10883: }
                   10884:
                   10885: /* YYY */
                   10886: ND *btog_lf(NODE ti,ND **p,int nb)
                   10887: {
                   10888:   PGeoBucket *r;
                   10889:   int i;
                   10890:   NODE t,s;
                   10891:   ND m,tp;
                   10892:   ND *pi,*rd;
                   10893:   LM lm;
                   10894:   Z lf,c;
                   10895:
                   10896:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   10897:   for ( i = 0; i < nb; i++ )
                   10898:     r[i] = create_pbucket();
                   10899:   for ( t = ti; t; t = NEXT(t) ) {
                   10900:     s = BDY((LIST)BDY(t));
                   10901:     if ( ARG0(s) ) {
                   10902:       m = mdptond((DP)ARG2(s));
1.6       noro     10903:       simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1       noro     10904:       if ( lm ) {
                   10905:         lmtolf(lm,&lf); HCZ(m) = lf;
1.6       noro     10906:         pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     10907:         for ( i = 0; i < nb; i++ ) {
                   10908:           tp = nd_mul_nm_lf(BDY(m),pi[i]);
                   10909:           add_pbucket(-2,r[i],tp);
                   10910:         }
                   10911:       }
                   10912:       c = ONE;
                   10913:     } else {
                   10914:       simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
                   10915:     }
                   10916:   }
                   10917:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   10918:   for ( i = 0; i < nb; i++ )
                   10919:     rd[i] = normalize_pbucket(-2,r[i]);
                   10920:   for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
                   10921:   return rd;
                   10922: }
                   10923:
                   10924: ND btog_one(NODE ti,ND *p,int nb,int mod)
                   10925: {
                   10926:   PGeoBucket r;
                   10927:   int i,ci,j;
                   10928:   NODE t,s;
                   10929:   ND m,tp;
                   10930:   ND pi,rd;
                   10931:   P c;
                   10932:
                   10933:   r = create_pbucket();
                   10934:   for ( t = ti; t; t = NEXT(t) ) {
                   10935:   s = BDY((LIST)BDY(t));
                   10936:     if ( ARG0(s) ) {
                   10937:     m = mdptond((DP)ARG2(s));
1.6       noro     10938:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     10939:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   10940:       HCM(m) = ci;
1.6       noro     10941:       pi = p[j=ZTOS((Q)ARG1(s))];
1.1       noro     10942:     if ( !pi ) {
                   10943:       pi = nd_load_mod(j);
                   10944:       tp = nd_mul_nm(mod,BDY(m),pi);
                   10945:       nd_free(pi);
                   10946:         add_pbucket(mod,r,tp);
                   10947:     } else {
                   10948:       tp = nd_mul_nm(mod,BDY(m),pi);
                   10949:         add_pbucket(mod,r,tp);
                   10950:       }
                   10951:     }
                   10952:     ci = 1;
                   10953:     } else {
                   10954:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   10955:     ci = invm(ci,mod);
                   10956:   }
                   10957:   }
                   10958:   rd = normalize_pbucket(mod,r);
                   10959:   free_pbucket(r);
                   10960:   if ( ci != 1 ) nd_mul_c(mod,rd,ci);
                   10961:   return rd;
                   10962: }
                   10963:
                   10964: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
                   10965:
                   10966: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
                   10967: {
                   10968:   int i,j,n,m,nb,pi0,pi1,nvar;
                   10969:   VL fv,tv,vv;
                   10970:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   10971:   ND **p;
                   10972:   ND *c;
                   10973:   ND u;
                   10974:   P inv;
                   10975:   MAT mat;
                   10976:
                   10977:   if ( mod == -2 )
                   10978:     return nd_btog_lf(f,v,ord,tlist,rp);
                   10979:
                   10980:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     10981:   parse_nd_option(vv,current_option);
1.1       noro     10982:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   10983:   switch ( ord->id ) {
                   10984:     case 1:
                   10985:       if ( ord->nv != nvar )
                   10986:         error("nd_check : invalid order specification");
                   10987:       break;
                   10988:     default:
                   10989:       break;
                   10990:   }
                   10991:   nd_init_ord(ord);
                   10992: #if 0
1.6       noro     10993:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     10994: #else
                   10995:   nd_bpe = 32;
                   10996: #endif
                   10997:   nd_setup_parameters(nvar,0);
                   10998:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   10999:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   11000:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   11001:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   11002:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     11003:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     11004:   if ( j > i ) i = j;
                   11005:   }
                   11006:   n = i+1;
                   11007:   nb = length(BDY(f));
                   11008:   p = (ND **)MALLOC(n*sizeof(ND *));
                   11009:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   11010:     pi = BDY((LIST)BDY(t));
1.6       noro     11011:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     11012:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   11013:     ptomp(mod,(P)ARG2(pi),&inv);
                   11014:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   11015:     u = ptond(CO,vv,(P)ONE);
                   11016:     HCM(u) = ((MQ)inv)->cont;
                   11017:     c[pi1] = u;
                   11018:   }
                   11019:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   11020:     printf("%d ",i); fflush(stdout);
                   11021:     ti = BDY((LIST)BDY(t));
1.6       noro     11022:     p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     11023:   }
                   11024:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   11025:     printf("%d ",i); fflush(stdout);
                   11026:     ti = BDY((LIST)BDY(t));
1.6       noro     11027:     p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     11028:   }
                   11029:   m = length(ind);
                   11030:   MKMAT(mat,nb,m);
                   11031:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     11032:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     11033:       BDY(mat)[i][j] = ndtodp(mod,c[i]);
                   11034:   return mat;
                   11035: }
                   11036:
                   11037: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
                   11038: {
                   11039:   int i,j,n,m,nb,pi0,pi1,nvar;
                   11040:   VL fv,tv,vv;
                   11041:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   11042:   ND **p;
                   11043:   ND *c;
                   11044:   ND u;
                   11045:   MAT mat;
                   11046:   LM lm;
                   11047:   Z lf,inv;
                   11048:
                   11049:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     11050:   parse_nd_option(vv,current_option);
1.1       noro     11051:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   11052:   switch ( ord->id ) {
                   11053:     case 1:
                   11054:       if ( ord->nv != nvar )
                   11055:         error("nd_check : invalid order specification");
                   11056:       break;
                   11057:     default:
                   11058:       break;
                   11059:   }
                   11060:   nd_init_ord(ord);
                   11061: #if 0
1.6       noro     11062:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     11063: #else
                   11064:   nd_bpe = 32;
                   11065: #endif
                   11066:   nd_setup_parameters(nvar,0);
                   11067:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   11068:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   11069:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   11070:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   11071:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     11072:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     11073:   if ( j > i ) i = j;
                   11074:   }
                   11075:   n = i+1;
                   11076:   nb = length(BDY(f));
                   11077:   p = (ND **)MALLOC(n*sizeof(ND *));
                   11078:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   11079:     pi = BDY((LIST)BDY(t));
1.6       noro     11080:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     11081:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   11082:     simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
                   11083:     u = ptond(CO,vv,(P)ONE);
                   11084:     HCZ(u) = inv;
                   11085:     c[pi1] = u;
                   11086:   }
                   11087:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   11088:     printf("%d ",i); fflush(stdout);
                   11089:     ti = BDY((LIST)BDY(t));
1.6       noro     11090:     p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     11091:   }
                   11092:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   11093:     printf("%d ",i); fflush(stdout);
                   11094:     ti = BDY((LIST)BDY(t));
1.6       noro     11095:     p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     11096:   }
                   11097:   m = length(ind);
                   11098:   MKMAT(mat,nb,m);
                   11099:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     11100:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     11101:       BDY(mat)[i][j] = ndtodp(-2,c[i]);
                   11102:   return mat;
                   11103: }
                   11104:
                   11105: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
                   11106:   LIST tlist,int pos,MAT *rp)
                   11107: {
                   11108:   int i,j,n,m,nb,pi0,pi1,nvar;
                   11109:   VL fv,tv,vv;
                   11110:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   11111:   ND *p;
                   11112:   ND *c;
                   11113:   ND u;
                   11114:   P inv;
                   11115:   VECT vect;
                   11116:
                   11117:   if ( mod == -2 )
                   11118:     error("nd_btog_one : not implemented yet for a large finite field");
                   11119:
                   11120:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     11121:   parse_nd_option(vv,current_option);
1.1       noro     11122:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   11123:   switch ( ord->id ) {
                   11124:     case 1:
                   11125:       if ( ord->nv != nvar )
                   11126:         error("nd_check : invalid order specification");
                   11127:       break;
                   11128:     default:
                   11129:       break;
                   11130:   }
                   11131:   nd_init_ord(ord);
                   11132: #if 0
1.6       noro     11133:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     11134: #else
                   11135:   nd_bpe = 32;
                   11136: #endif
                   11137:   nd_setup_parameters(nvar,0);
                   11138:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   11139:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   11140:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   11141:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   11142:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     11143:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     11144:   if ( j > i ) i = j;
                   11145:   }
                   11146:   n = i+1;
                   11147:   nb = length(BDY(f));
                   11148:   p = (ND *)MALLOC(n*sizeof(ND *));
                   11149:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   11150:     pi = BDY((LIST)BDY(t));
1.6       noro     11151:   pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     11152:   if ( pi1 == pos ) {
                   11153:     ptomp(mod,(P)ARG2(pi),&inv);
                   11154:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   11155:     u = ptond(CO,vv,(P)ONE);
                   11156:     HCM(u) = ((MQ)inv)->cont;
                   11157:     p[pi0] = u;
                   11158:     }
                   11159:   }
                   11160:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   11161:   printf("%d ",i); fflush(stdout);
                   11162:     ti = BDY((LIST)BDY(t));
1.6       noro     11163:     p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     11164:     if ( Demand ) {
                   11165:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   11166:   }
                   11167:   }
                   11168:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   11169:   printf("%d ",i); fflush(stdout);
                   11170:     ti = BDY((LIST)BDY(t));
1.6       noro     11171:     p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     11172:     if ( Demand ) {
                   11173:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   11174:   }
                   11175:   }
                   11176:   m = length(ind);
                   11177:   MKVECT(vect,m);
                   11178:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6       noro     11179:   u = p[ZTOS((Q)BDY(t))];
1.1       noro     11180:   if ( !u ) {
1.6       noro     11181:     u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1       noro     11182:     BDY(vect)[j] = ndtodp(mod,u);
                   11183:     nd_free(u);
                   11184:   } else
                   11185:     BDY(vect)[j] = ndtodp(mod,u);
                   11186:   }
                   11187:   return vect;
                   11188: }
                   11189:
                   11190: void ndv_print_lf(NDV p)
                   11191: {
                   11192:     NMV m;
                   11193:     int i,len;
                   11194:
                   11195:     if ( !p ) printf("0\n");
                   11196:     else {
                   11197:         len = LEN(p);
                   11198:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   11199:             printf("+");
                   11200:             mpz_out_str(asir_out,10,BDY(CZ(m)));
                   11201:             printf("*");
                   11202:             ndl_print(DL(m));
                   11203:         }
                   11204:         printf("\n");
                   11205:     }
                   11206: }
                   11207:
                   11208: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
                   11209: {
                   11210:     VL tv,fv,vv,vc,av;
                   11211:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   11212:     int m,nocheck,nvar,mindex,e,max;
                   11213:     NDV c;
                   11214:     NMV a;
                   11215:     P p,zp;
                   11216:     Q dmy;
                   11217:     EPOS oepos;
                   11218:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   11219:     Alg alpha,dp;
                   11220:     P poly;
                   11221:     LIST f1,f2,zpl;
                   11222:     Obj obj;
                   11223:     NumberField nf;
                   11224:     struct order_spec *ord1;
                   11225:     struct oEGT eg_check,eg0,eg1;
                   11226:     NODE tr,tl1,tl2,tl3,tl4;
                   11227:     LIST l1,l2,l3,l4,l5;
                   11228:     int *perm;
                   11229:     int j,ret;
                   11230:     NODE retn;
                   11231:     Q jq,bpe;
                   11232:
                   11233:     nd_module = 0;
                   11234:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     11235:     parse_nd_option(vv,current_option);
1.1       noro     11236:     if ( nd_vc )
                   11237:       error("nd_f4_lf_trace : computation over a rational function field is not implemented");
                   11238:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   11239:     switch ( ord->id ) {
                   11240:         case 1:
                   11241:             if ( ord->nv != nvar )
                   11242:                 error("nd_f4_lf_trace : invalid order specification");
                   11243:             break;
                   11244:         default:
                   11245:             break;
                   11246:     }
                   11247:
                   11248:     nd_ntrans = nvar;
                   11249:     nd_nalg = 0;
                   11250:
                   11251:     nocheck = 0;
                   11252:     mindex = 0;
                   11253:
                   11254:     /* do not use on-demand load/save */
                   11255:     nd_demand = 0;
                   11256:     m = trace > 1 ? trace : get_lprime(mindex);
                   11257:     nd_init_ord(ord);
                   11258:     mrank = 0;
                   11259:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   11260:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   11261:             if ( nd_module ) {
                   11262:                 s = BDY((LIST)BDY(t));
                   11263:                 trank = length(s);
                   11264:                 mrank = MAX(mrank,trank);
                   11265:                 for ( ; s; s = NEXT(s) ) {
                   11266:                     e = getdeg(tv->v,(P)BDY(s));
                   11267:                     max = MAX(e,max);
                   11268:                 }
                   11269:             } else {
                   11270:                 e = getdeg(tv->v,(P)BDY(t));
                   11271:                 max = MAX(e,max);
                   11272:             }
                   11273:         }
                   11274:     nd_setup_parameters(nvar,max);
                   11275:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   11276:     ishomo = 1;
                   11277:     /* XXX */
                   11278:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   11279:         if ( nd_module ) {
                   11280:             c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
                   11281:         } else {
                   11282:             c = (pointer)ptondv(CO,vv,(P)BDY(t));
                   11283:         }
                   11284:         if ( ishomo )
                   11285:             ishomo = ishomo && ndv_ishomo(c);
                   11286:         if ( c ) {
                   11287:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   11288:             ndv_mod(-2,c);
                   11289:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   11290:         }
                   11291:     }
                   11292:     if ( in0 ) NEXT(in) = 0;
                   11293:     if ( fd0 ) NEXT(fd) = 0;
                   11294:     if ( !ishomo && homo ) {
                   11295:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   11296:             c = (NDV)BDY(t); len = LEN(c);
                   11297:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   11298:                 wmax = MAX(TD(DL(a)),wmax);
                   11299:         }
                   11300:         homogenize_order(ord,nvar,&ord1);
                   11301:         nd_init_ord(ord1);
                   11302:         nd_setup_parameters(nvar+1,wmax);
                   11303:         for ( t = fd0; t; t = NEXT(t) )
                   11304:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   11305:     }
                   11306:     if ( MaxDeg > 0 ) nocheck = 1;
1.24      noro     11307:     ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1       noro     11308:     if ( ret )
                   11309:       cand = nd_f4_lf_trace_main(m,&perm);
                   11310:     if ( !ret || !cand ) {
                   11311:        *rp = 0; return;
                   11312:     }
                   11313:     if ( !ishomo && homo ) {
                   11314:       /* dehomogenization */
                   11315:       for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   11316:       nd_init_ord(ord);
                   11317:       nd_setup_parameters(nvar,0);
                   11318:     }
                   11319:     cand = ndv_reducebase(cand,perm);
                   11320:     cand = ndv_reduceall(-2,cand);
                   11321:     cbpe = nd_bpe;
                   11322:     get_eg(&eg0);
                   11323:     if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   11324:       /* gbcheck : cand is a GB of Id(cand) ? */
                   11325:       retn = nd_f4(-2,0,0);
                   11326:     }
                   11327:   if ( !retn ) {
                   11328:       /* failure */
                   11329:       *rp = 0; return;
                   11330:     }
                   11331:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   11332:     if ( DP_Print )
1.5       noro     11333:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     11334:     /* dp->p */
                   11335:     nd_bpe = cbpe;
                   11336:     nd_setup_parameters(nd_nvar,0);
                   11337:     for ( r = cand; r; r = NEXT(r) ) {
                   11338:       if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
                   11339:       else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
                   11340:     }
                   11341:     MKLIST(*rp,cand);
                   11342: }
                   11343:
                   11344: NODE nd_f4_lf_trace_main(int m,int **indp)
                   11345: {
                   11346:     int i,nh,stat,index;
                   11347:     NODE r,rm,g;
                   11348:     ND_pairs d,l,l0,t;
                   11349:     ND spol,red;
                   11350:     NDV nf,redv,nfqv,nfv;
                   11351:     NM s0,s;
                   11352:     NODE rp0,srp0,nflist,nflist_lf;
                   11353:     int nsp,nred,col,rank,len,k,j,a;
                   11354:     UINT c;
                   11355:     UINT **spmat;
                   11356:     UINT *s0vect,*svect,*p,*v;
                   11357:     int *colstat;
                   11358:     IndArray *imat;
                   11359:     int *rhead;
                   11360:     int spcol,sprow;
                   11361:     int sugar;
                   11362:     PGeoBucket bucket;
                   11363:     struct oEGT eg0,eg1,eg_f4;
                   11364:
                   11365:     g = 0; d = 0;
                   11366:     for ( i = 0; i < nd_psn; i++ ) {
                   11367:         d = update_pairs(d,g,i,0);
                   11368:         g = update_base(g,i);
                   11369:     }
                   11370:     while ( d ) {
                   11371:         get_eg(&eg0);
                   11372:         l = nd_minsugarp(d,&d);
                   11373:         sugar = SG(l);
                   11374:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   11375:         bucket = create_pbucket();
                   11376:         stat = nd_sp_f4(m,0,l,bucket);
                   11377:         if ( !stat ) {
                   11378:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11379:             NEXT(t) = d; d = l;
                   11380:             d = nd_reconstruct(1,d);
                   11381:             continue;
                   11382:         }
                   11383:         if ( bucket->m < 0 ) continue;
                   11384:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   11385:         if ( !col ) {
                   11386:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11387:             NEXT(t) = d; d = l;
                   11388:             d = nd_reconstruct(1,d);
                   11389:             continue;
                   11390:         }
                   11391:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   11392:         if ( DP_Print )
1.5       noro     11393:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1       noro     11394:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   11395:         if ( !l0 ) continue;
                   11396:         l = l0;
                   11397:
                   11398:         /* over LF */
                   11399:         bucket = create_pbucket();
                   11400:         stat = nd_sp_f4(-2,1,l,bucket);
                   11401:         if ( !stat ) {
                   11402:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11403:             NEXT(t) = d; d = l;
                   11404:             d = nd_reconstruct(1,d);
                   11405:             continue;
                   11406:         }
                   11407:         if ( bucket->m < 0 ) continue;
                   11408:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   11409:         if ( !col ) {
                   11410:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11411:             NEXT(t) = d; d = l;
                   11412:             d = nd_reconstruct(1,d);
                   11413:             continue;
                   11414:         }
                   11415:         nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
                   11416:         /* adding new bases */
                   11417:         for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
                   11418:             nfv = (NDV)BDY(rm);
                   11419:             nfqv = (NDV)BDY(r);
                   11420:             if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
                   11421:             ndv_removecont(m,nfv);
                   11422:             ndv_removecont(-2,nfqv);
1.24      noro     11423:             nh = ndv_newps(-2,nfv,nfqv);
1.1       noro     11424:             d = update_pairs(d,g,nh,0);
                   11425:             g = update_base(g,nh);
                   11426:         }
                   11427:         if ( r || rm ) return 0;
                   11428:     }
                   11429:   conv_ilist(nd_demand,1,g,indp);
                   11430:     return g;
                   11431: }
                   11432:
1.7       noro     11433: #if SIZEOF_LONG==8
                   11434:
                   11435: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   11436: {
                   11437:     int j,k,len;
                   11438:     UINT *p;
                   11439:     UINT c;
                   11440:     NDV r;
                   11441:     NMV mr0,mr;
                   11442:
                   11443:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   11444:     if ( !len ) return 0;
                   11445:     else {
                   11446:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   11447: #if 0
                   11448:         ndv_alloc += nmv_adv*len;
                   11449: #endif
                   11450:         mr = mr0;
                   11451:         p = s0vect;
                   11452:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   11453:             if ( !rhead[j] ) {
                   11454:                 if ( (c = (UINT)vect[k++]) != 0 ) {
                   11455:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   11456:                 }
                   11457:             }
                   11458:         MKNDV(nd_nvar,mr0,len,r);
                   11459:         return r;
                   11460:     }
                   11461: }
                   11462:
1.28      noro     11463: NDV vect64_to_ndv_s(mp_limb_t *vect,int col,UINT *s0vect)
                   11464: {
                   11465:     int j,k,len;
                   11466:     UINT *p;
                   11467:     UINT c;
                   11468:     NDV r;
                   11469:     NMV mr0,mr;
                   11470:
                   11471:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   11472:     if ( !len ) return 0;
                   11473:     else {
                   11474:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   11475:         mr = mr0;
                   11476:         p = s0vect;
                   11477:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   11478:           if ( (c = (UINT)vect[k++]) != 0 ) {
                   11479:             ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   11480:           }
                   11481:         MKNDV(nd_nvar,mr0,len,r);
                   11482:         return r;
                   11483:     }
                   11484: }
                   11485:
1.7       noro     11486: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
                   11487: {
                   11488:     NM m;
1.11      noro     11489:     UINT *t,*s,*u;
                   11490:     int i,st,ed,md,prev,c;
1.7       noro     11491:
                   11492:     for ( i = 0; i < n; i++ ) r[i] = 0;
1.11      noro     11493:     prev = 0;
                   11494:     for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
                   11495:       t = DL(m);
                   11496:       st = prev;
                   11497:       ed = n;
                   11498:       while ( ed > st ) {
                   11499:         md = (st+ed)/2;
                   11500:         u = s0+md*nd_wpd;
                   11501:         c = DL_COMPARE(u,t);
                   11502:         if ( c == 0 ) break;
                   11503:         else if ( c > 0 ) st = md;
                   11504:         else ed = md;
                   11505:       }
                   11506:       r[md] = (mp_limb_t)CM(m);
                   11507:       prev = md;
1.7       noro     11508:     }
                   11509:     for ( i = 0; !r[i]; i++ );
                   11510:     return i;
                   11511: }
                   11512:
                   11513: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
                   11514:
1.28      noro     11515: 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     11516: {
                   11517:     int i,j,k,len,pos,prev;
                   11518:     mp_limb_t a,c,c1,c2;
                   11519:     IndArray ivect;
                   11520:     unsigned char *ivc;
                   11521:     unsigned short *ivs;
                   11522:     unsigned int *ivi;
                   11523:     NDV redv;
                   11524:     NMV mr;
                   11525:     NODE rp;
                   11526:     int maxrs;
                   11527:
                   11528:     for ( i = 0; i < col; i++ ) cvect[i] = 0;
                   11529:     maxrs = 0;
                   11530:     for ( i = 0; i < nred; i++ ) {
                   11531:         ivect = imat[i];
                   11532:         k = ivect->head;
                   11533:         a = svect[k]; c = cvect[k];
                   11534:         MOD128(a,c,m);
                   11535:         svect[k] = a; cvect[k] = 0;
1.28      noro     11536:         if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.11      noro     11537:             Nf4_red++;
1.7       noro     11538:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   11539:             c = m-c; redv = nd_ps[rp0[i]->index];
                   11540:             len = LEN(redv); mr = BDY(redv);
                   11541:             svect[k] = 0; prev = k;
                   11542:             switch ( ivect->width ) {
                   11543:                 case 1:
                   11544:                     ivc = ivect->index.c;
                   11545:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   11546:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12      noro     11547:                         c2 = svect[pos]+c1*c;
                   11548:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   11549:                         svect[pos] = c2;
1.7       noro     11550:                     }
                   11551:                     break;
                   11552:                 case 2:
                   11553:                     ivs = ivect->index.s;
                   11554:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   11555:                         pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12      noro     11556:                         c2 = svect[pos]+c1*c;
                   11557:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   11558:                         svect[pos] = c2;
1.7       noro     11559:                     }
                   11560:                     break;
                   11561:                 case 4:
                   11562:                     ivi = ivect->index.i;
                   11563:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   11564:                         pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12      noro     11565:                         c2 = svect[pos]+c1*c;
                   11566:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   11567:                         svect[pos] = c2;
1.7       noro     11568:                     }
                   11569:                     break;
                   11570:             }
                   11571:         }
                   11572:     }
                   11573:     for ( i = 0; i < col; i++ ) {
                   11574:       a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
                   11575:     }
                   11576:     return maxrs;
                   11577: }
                   11578:
                   11579: /* for Fp, 2^15=<p<2^29 */
                   11580:
                   11581: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   11582:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   11583: {
                   11584:     int spcol,sprow,a;
                   11585:     int i,j,k,l,rank;
                   11586:     NODE r0,r;
                   11587:     ND_pairs sp;
                   11588:     ND spol;
                   11589:     mp_limb_t **spmat;
                   11590:     mp_limb_t *svect,*cvect;
                   11591:     mp_limb_t *v;
                   11592:     int *colstat;
                   11593:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   11594:     int maxrs;
                   11595:     int *spsugar;
                   11596:     ND_pairs *spactive;
                   11597:
                   11598:     spcol = col-nred;
                   11599:     get_eg(&eg0);
                   11600:     /* elimination (1st step) */
                   11601:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   11602:     svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11603:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11604:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   11605:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   11606:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   11607:         nd_sp(m,0,sp,&spol);
                   11608:         if ( !spol ) continue;
                   11609:         nd_to_vect64(m,s0vect,col,spol,svect);
1.28      noro     11610:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,0);
1.7       noro     11611:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   11612:         if ( i < col ) {
                   11613:             spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
                   11614:             for ( j = k = 0; j < col; j++ )
                   11615:                 if ( !rhead[j] ) v[k++] = (UINT)svect[j];
                   11616:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   11617:             if ( nz )
                   11618:             spactive[sprow] = sp;
                   11619:             sprow++;
                   11620:         }
                   11621:         nd_free(spol);
                   11622:     }
1.12      noro     11623:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7       noro     11624:     if ( DP_Print ) {
                   11625:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   11626:         fflush(asir_out);
                   11627:     }
                   11628:     /* free index arrays */
                   11629:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   11630:
                   11631:     /* elimination (2nd step) */
                   11632:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   11633:     rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   11634:     r0 = 0;
                   11635:     for ( i = 0; i < rank; i++ ) {
                   11636:         NEXTNODE(r0,r); BDY(r) =
                   11637:           (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   11638:         SG((NDV)BDY(r)) = spsugar[i];
                   11639:         GCFREE(spmat[i]);
                   11640:     }
                   11641:     if ( r0 ) NEXT(r) = 0;
                   11642:
                   11643:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12      noro     11644:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7       noro     11645:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   11646:     if ( DP_Print ) {
                   11647:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   11648:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   11649:             nsp,nred,sprow,spcol,rank);
                   11650:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   11651:     }
                   11652:     if ( nz ) {
                   11653:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   11654:         if ( rank > 0 ) {
                   11655:             NEXT(spactive[rank-1]) = 0;
                   11656:             *nz = spactive[0];
                   11657:         } else
                   11658:             *nz = 0;
                   11659:     }
                   11660:     return r0;
                   11661: }
                   11662:
                   11663: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   11664: {
                   11665:   int i,j,k,l,rank,s;
                   11666:   mp_limb_t inv;
                   11667:   mp_limb_t a;
                   11668:   UINT c;
                   11669:   mp_limb_t *t,*pivot,*pk;
                   11670:   UINT *ck;
                   11671:   UINT **cmat;
                   11672:   UINT *ct;
                   11673:   ND_pairs pair;
                   11674:
                   11675:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   11676:   for ( i = 0; i < row; i++ ) {
                   11677:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   11678:     bzero(cmat[i],col*sizeof(UINT));
                   11679:   }
                   11680:
                   11681:   for ( rank = 0, j = 0; j < col; j++ ) {
                   11682:     for ( i = rank; i < row; i++ ) {
                   11683:       a = mat[i][j]; c = cmat[i][j];
                   11684:       MOD128(a,c,md);
                   11685:       mat[i][j] = a; cmat[i][j] = 0;
                   11686:     }
                   11687:     for ( i = rank; i < row; i++ )
                   11688:       if ( mat[i][j] )
                   11689:         break;
                   11690:     if ( i == row ) {
                   11691:       colstat[j] = 0;
                   11692:       continue;
                   11693:     } else
                   11694:       colstat[j] = 1;
                   11695:     if ( i != rank ) {
                   11696:       t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   11697:       ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
                   11698:       s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   11699:       if ( spactive ) {
                   11700:         pair = spactive[i]; spactive[i] = spactive[rank];
                   11701:         spactive[rank] = pair;
                   11702:       }
                   11703:     }
                   11704:     /* column j is normalized */
                   11705:     s = sugar[rank];
                   11706:     inv = invm((UINT)mat[rank][j],md);
                   11707:     /* normalize pivot row */
                   11708:     for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
                   11709:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   11710:     }
                   11711:     for ( i = rank+1; i < row; i++ ) {
                   11712:       if ( (a = mat[i][j]) != 0 ) {
                   11713:         sugar[i] = MAX(sugar[i],s);
                   11714:         red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11      noro     11715:         Nf4_red++;
1.7       noro     11716:       }
                   11717:     }
                   11718:     rank++;
                   11719:   }
                   11720:   for ( j = col-1, l = rank-1; j >= 0; j-- )
                   11721:     if ( colstat[j] ) {
                   11722:       for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
                   11723:         a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
                   11724:       }
                   11725:       s = sugar[l];
                   11726:       for ( i = 0; i < l; i++ ) {
                   11727:         a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
                   11728:         if ( a ) {
                   11729:           sugar[i] = MAX(sugar[i],s);
                   11730:           red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11      noro     11731:           Nf4_red++;
1.7       noro     11732:         }
                   11733:       }
                   11734:       l--;
                   11735:     }
                   11736:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   11737:   GCFREE(cmat);
                   11738:   return rank;
                   11739: }
                   11740:
1.28      noro     11741: 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)
                   11742: {
                   11743:   int i,j,k,l,rank,s,imin;
                   11744:   mp_limb_t inv;
                   11745:   mp_limb_t a;
                   11746:   UINT c;
                   11747:   mp_limb_t *t,*pivot,*pk;
                   11748:   UINT *ck;
                   11749:   UINT **cmat;
                   11750:   UINT *ct;
                   11751:   ND_pairs pair;
                   11752:   SIG sg;
1.31      noro     11753:   int *used;
1.28      noro     11754:
1.31      noro     11755:   used = (int *)MALLOC(row*sizeof(int));
1.28      noro     11756:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   11757:   for ( i = 0; i < row; i++ ) {
                   11758:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   11759:     bzero(cmat[i],col*sizeof(UINT));
                   11760:   }
                   11761:
1.31      noro     11762:   for ( j = 0; j < col; j++ ) {
                   11763:     for ( i = 0; i < row; i++ ) {
1.28      noro     11764:       a = mat[i][j]; c = cmat[i][j];
                   11765:       MOD128(a,c,md);
                   11766:       mat[i][j] = a; cmat[i][j] = 0;
                   11767:     }
1.31      noro     11768:     for ( i = 0; i < row; i++ )
                   11769:       if ( !used[i] && mat[i][j] ) break;
                   11770:     if ( i == row ) {
1.28      noro     11771:       colstat[j] = 0;
                   11772:       continue;
1.31      noro     11773:     } else {
1.28      noro     11774:       colstat[j] = 1;
1.31      noro     11775:       used[i] = 1;
1.28      noro     11776:     }
                   11777:     /* column j is normalized */
1.31      noro     11778:     s = sugar[i];
                   11779:     inv = invm((UINT)mat[i][j],md);
1.28      noro     11780:     /* normalize pivot row */
1.31      noro     11781:     for ( k = j, pk = mat[i]+j, ck = cmat[i]+j; k < col; k++, pk++, ck++ ) {
1.28      noro     11782:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   11783:     }
1.31      noro     11784:     for ( k = i+1; k < row; k++ ) {
                   11785:       if ( (a = mat[k][j]) != 0 ) {
                   11786:         sugar[k] = MAX(sugar[k],s);
                   11787:         red_by_vect64(md,mat[k]+j,cmat[k]+j,mat[i]+j,(int)(md-a),col-j);
1.28      noro     11788:         Nf4_red++;
                   11789:       }
                   11790:     }
                   11791:   }
1.31      noro     11792:   rank = 0;
                   11793:   for ( i = 0; i < row; i++ ) {
                   11794:     for ( j = 0; j < col; j++ )
                   11795:       if ( mat[i][j] ) break;
                   11796:     if ( j == col ) sugar[i] = -1;
                   11797:     else rank++;
                   11798:   }
1.28      noro     11799:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   11800:   GCFREE(cmat);
                   11801:   return rank;
                   11802: }
                   11803:
                   11804: NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   11805:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
                   11806: {
                   11807:     int spcol,sprow,a;
                   11808:     int i,j,k,l,rank;
                   11809:     NODE r0,r;
                   11810:     ND_pairs sp;
                   11811:     ND spol;
                   11812:     mp_limb_t **spmat;
                   11813:     mp_limb_t *svect,*cvect;
                   11814:     mp_limb_t *v;
                   11815:     int *colstat;
                   11816:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   11817:     int maxrs;
                   11818:     int *spsugar;
                   11819:     ND_pairs *spactive;
                   11820:     SIG *spsig;
                   11821:
                   11822:     get_eg(&eg0);
                   11823:     /* elimination (1st step) */
                   11824:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   11825:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11826:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   11827:     spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
                   11828:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   11829:         nd_sp(m,0,sp,&spol);
                   11830:         if ( !spol ) {
1.29      noro     11831:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     11832:           continue;
                   11833:         }
                   11834:         svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11835:         nd_to_vect64(m,s0vect,col,spol,svect);
                   11836:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,spol->sig);
                   11837:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   11838:         if ( i < col ) {
                   11839:             spmat[sprow] = svect;
                   11840:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   11841:             spsig[sprow] = sp->sig;
                   11842:             sprow++;
                   11843:         } else {
1.29      noro     11844:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     11845:         }
                   11846:         nd_free(spol);
                   11847:     }
                   11848:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
                   11849:     if ( DP_Print ) {
                   11850:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   11851:         fflush(asir_out);
                   11852:     }
                   11853:     /* free index arrays */
                   11854:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   11855:
                   11856:     /* elimination (2nd step) */
                   11857:     colstat = (int *)MALLOC(col*sizeof(int));
                   11858:     rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
                   11859:     r0 = 0;
1.31      noro     11860:     for ( i = 0; i < sprow; i++ ) {
                   11861:         if ( spsugar[i] >= 0 ) {
                   11862:           NEXTNODE(r0,r);
                   11863:           BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
                   11864:           SG((NDV)BDY(r)) = spsugar[i];
                   11865:           ((NDV)BDY(r))->sig = spsig[i];
                   11866:         } else
                   11867:           syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
1.28      noro     11868:         GCFREE(spmat[i]);
                   11869:     }
                   11870:     if ( r0 ) NEXT(r) = 0;
                   11871:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
                   11872:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   11873:     if ( DP_Print ) {
                   11874:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   11875:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   11876:             nsp,nred,sprow,col,rank);
                   11877:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   11878:     }
                   11879:     return r0;
                   11880: }
1.40      noro     11881: #endif
1.28      noro     11882:
                   11883: NODE nd_f4_red_s(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,NODE *syzlistp)
                   11884: {
                   11885:   IndArray *imat;
                   11886:   int nsp,nred,i,start;
                   11887:   int *rhead;
                   11888:   NODE r0,rp;
                   11889:   ND_pairs sp;
                   11890:   NM_ind_pair *rvect;
                   11891:   UINT *s;
                   11892:   int *s0hash;
                   11893:   struct oEGT eg0,eg1,eg_conv;
                   11894:
                   11895:   for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   11896:   nred = length(rp0);
                   11897:   imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   11898:   rhead = (int *)MALLOC(col*sizeof(int));
                   11899:   for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   11900:
                   11901:   /* construction of index arrays */
                   11902:   get_eg(&eg0);
                   11903:   if ( DP_Print ) {
                   11904:     fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   11905:     fflush(asir_out);
                   11906:   }
                   11907:   rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   11908:   for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   11909:     rvect[i] = (NM_ind_pair)BDY(rp);
                   11910:     imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
                   11911:     rhead[imat[i]->head] = 1;
                   11912:     start = imat[i]->head;
                   11913:   }
                   11914:   get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
                   11915:   if ( DP_Print ) {
                   11916:     fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   11917:     fflush(asir_out);
                   11918:   }
                   11919:   if ( m > 0 )
1.40      noro     11920: #if SIZEOF_LONG==8
1.28      noro     11921:     r0 = nd_f4_red_mod64_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
1.40      noro     11922: #else
                   11923:     r0 = nd_f4_red_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
                   11924: #endif
1.28      noro     11925:   else
                   11926: //    r0 = nd_f4_red_q_main_s(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   11927:     error("nd_f4_red_q_main_s : not implemented yet");
                   11928:   return r0;
                   11929: }
                   11930:
                   11931: INLINE int ndl_find_reducer_minsig(UINT *dg)
                   11932: {
                   11933:   RHist r;
                   11934:   int i,singular,ret,d,k,imin;
                   11935:   SIG t;
                   11936:   static int wpd,nvar;
                   11937:   static SIG quo,quomin;
                   11938:   static UINT *tmp;
                   11939:
                   11940:   if ( !quo || nvar != nd_nvar ) { NEWSIG(quo); NEWSIG(quomin); }
                   11941:   if ( wpd != nd_wpd ) {
                   11942:     wpd = nd_wpd;
                   11943:     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
                   11944:   }
                   11945: #if 0
                   11946:   d = ndl_hash_value(dg);
                   11947:   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   11948:     if ( ndl_equal(dg,DL(r)) ) {
                   11949:       return r->index;
                   11950:     }
                   11951:   }
                   11952: #endif
                   11953:   imin = -1;
                   11954:   for ( i = 0; i < nd_psn; i++ ) {
                   11955:     r = nd_psh[i];
                   11956:     if ( ndl_reducible(dg,DL(r)) ) {
                   11957:       ndl_sub(dg,DL(r),tmp);
                   11958:       _ndltodl(tmp,DL(quo));
                   11959:       _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
                   11960:       quo->pos = nd_psh[i]->sig->pos;
1.55      noro     11961:       _adddl(nd_nvar,DL(quo),nd_sba_hm[quo->pos],DL2(quo));
1.28      noro     11962:       if ( imin < 0 || comp_sig(quomin,quo) > 0 ) {
                   11963:         t = quo; quo = quomin; quomin = t;
                   11964:         imin = i;
                   11965:       }
                   11966:     }
                   11967:   }
                   11968:   if ( imin == -1 ) return nd_psn;
                   11969:   else {
                   11970: #if 0
                   11971:     nd_append_red(dg,i);
                   11972: #endif
                   11973:     return imin;
                   11974:   }
                   11975: }
                   11976:
                   11977: int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   11978: {
                   11979:   NODE rp0,rp;
                   11980:   NM mul,head,s0,s;
                   11981:   int index,col,i,sugar;
                   11982:   RHist h;
                   11983:   UINT *s0v,*p;
                   11984:   NM_ind_pair pair;
                   11985:   ND red;
                   11986:   NDV *ps;
                   11987:   SIG sig;
                   11988:
                   11989:   s0 = 0; rp0 = 0; col = 0;
                   11990:   if ( nd_demand )
                   11991:     ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   11992:   else
                   11993:     ps = trace?nd_ps_trace:nd_ps;
                   11994:   while ( 1 ) {
                   11995:     head = remove_head_pbucket_symbolic(bucket);
                   11996:     if ( !head ) break;
                   11997:     if ( !s0 ) s0 = head;
                   11998:     else NEXT(s) = head;
                   11999:     s = head;
                   12000:     index = ndl_find_reducer_minsig(DL(head));
                   12001:     if ( index >= 0 && index < nd_psn ) {
                   12002:       h = nd_psh[index];
                   12003:       NEWNM(mul);
                   12004:       ndl_sub(DL(head),DL(h),DL(mul));
                   12005:       if ( ndl_check_bound2(index,DL(mul)) )
                   12006:         return 0;
                   12007:       sugar = TD(DL(mul))+SG(ps[index]);
                   12008:       NEWSIG(sig);
                   12009:       _ndltodl(DL(mul),DL(sig));
                   12010:       _addtodl(nd_nvar,DL(nd_psh[index]->sig),DL(sig));
                   12011:       sig->pos = nd_psh[index]->sig->pos;
1.55      noro     12012:       _adddl(nd_nvar,DL(sig),nd_sba_hm[sig->pos],DL2(sig));
1.28      noro     12013:       MKNM_ind_pair(pair,mul,index,sugar,sig);
                   12014:       red = ndv_mul_nm_symbolic(mul,ps[index]);
                   12015:       add_pbucket_symbolic(bucket,nd_remove_head(red));
                   12016:       NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   12017:     }
                   12018:     col++;
                   12019:   }
                   12020:   if ( rp0 ) NEXT(rp) = 0;
                   12021:   NEXT(s) = 0;
                   12022:   s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   12023:   for ( i = 0, p = s0v, s = s0; i < col;
                   12024:     i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   12025:   *s0vect = s0v;
                   12026:   *r = rp0;
                   12027:
                   12028:   return col;
                   12029: }
                   12030:
                   12031: NODE nd_sba_f4(int m,int **indp)
                   12032: {
1.34      noro     12033:   int i,nh,stat,index,f4red,f4step;
1.33      noro     12034:   int col,rank,len,k,j,a,sugar,nbase,psugar,ms;
                   12035:   NODE r,g,rp0,nflist;
1.41      noro     12036:   ND_pairs d,l,t,l1;
1.33      noro     12037:   ND h,nf;
                   12038:   NDV nfv;
                   12039:   union oNDC hc;
                   12040:   UINT *s0vect;
1.28      noro     12041:   UINT c;
                   12042:   PGeoBucket bucket;
1.33      noro     12043:   NODE *syzlist;
                   12044:   SIG sig;
1.28      noro     12045:   struct oEGT eg0,eg1,eg_f4;
1.33      noro     12046:   struct oEGT eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.28      noro     12047:
                   12048:   Nf4_red=0;
1.30      noro     12049:   d = 0;
1.29      noro     12050:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.28      noro     12051:   for ( i = 0; i < nd_psn; i++ ) {
1.30      noro     12052:     d = update_pairs_s(d,i,syzlist);
1.28      noro     12053:   }
1.30      noro     12054:   nd_nbase = nd_psn;
1.28      noro     12055:   f4red = 1;
1.33      noro     12056:   psugar = 0;
1.34      noro     12057:   f4step = 0;
1.28      noro     12058:   while ( d ) {
1.33      noro     12059:     for ( t = d, ms = SG(d); t; t = NEXT(t) )
                   12060:       if ( SG(t) < ms ) ms = SG(t);
1.38      noro     12061:     if ( ms == psugar && f4step >= nd_sba_f4step ) {
1.33      noro     12062: again:
                   12063:       l = d; d = d->next;
1.41      noro     12064: #if 0
1.33      noro     12065:       if ( small_lcm(l) ) {
                   12066:         if ( DP_Print ) fprintf(asir_out,"M");
                   12067:         continue;
                   12068:       }
                   12069:       sig = l->sig;
                   12070:       stat = nd_sp(m,0,l,&h);
1.41      noro     12071: #else
                   12072:       l1 = find_smallest_lcm(l);
                   12073:       if ( l1 == 0 ) {
                   12074:         if ( DP_Print ) fprintf(asir_out,"M");
                   12075:         continue;
                   12076:       }
                   12077:       sig = l1->sig;
                   12078:       stat = nd_sp(m,0,l1,&h);
                   12079: #endif
1.33      noro     12080:       if ( !stat ) {
                   12081:         NEXT(l) = d; d = l;
                   12082:         d = nd_reconstruct(0,d);
                   12083:         goto again;
                   12084:       }
                   12085:   get_eg(&eg1);
                   12086:   #if USE_GEOBUCKET
1.39      noro     12087:       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     12088:   #else
1.39      noro     12089:       stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.33      noro     12090:   #endif
                   12091:   get_eg(&eg2);
                   12092:       if ( !stat ) {
                   12093:         NEXT(l) = d; d = l;
                   12094:         d = nd_reconstruct(0,d);
                   12095:         goto again;
                   12096:       } else if ( stat == -1 ) {
                   12097:         if ( DP_Print ) { printf("S"); fflush(stdout); }
                   12098:         FREENDP(l);
                   12099:       } else if ( nf ) {
                   12100:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   12101:         add_eg(&eg_nf,&eg1,&eg2);
                   12102:         hc = HCU(nf);
                   12103:         nd_removecont(m,nf);
                   12104:         nfv = ndtondv(m,nf); nd_free(nf);
                   12105:         nh = ndv_newps(m,nfv,0);
                   12106:
1.30      noro     12107:         d = update_pairs_s(d,nh,syzlist);
                   12108:         nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.33      noro     12109:         FREENDP(l);
1.28      noro     12110:       } else {
1.33      noro     12111:         add_eg(&eg_nfzero,&eg1,&eg2);
                   12112:        // syzygy
                   12113:   get_eg(&eg1);
                   12114:         d = remove_spair_s(d,sig);
                   12115:   get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29      noro     12116:         syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.33      noro     12117:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   12118:         FREENDP(l);
                   12119:       }
                   12120:     } else {
1.34      noro     12121:       if ( ms != psugar ) f4step = 1;
                   12122:       else f4step++;
1.33      noro     12123: again2:
                   12124:       psugar = ms;
                   12125:       l = nd_minsugarp_s(d,&d);
                   12126:       sugar = nd_sugarweight?d->sugar2:SG(d);
                   12127:       bucket = create_pbucket();
                   12128:       stat = nd_sp_f4(m,0,l,bucket);
                   12129:       if ( !stat ) {
                   12130:         for ( t = l; NEXT(t); t = NEXT(t) );
                   12131:         NEXT(t) = d; d = l;
                   12132:         d = nd_reconstruct(0,d);
                   12133:         goto again2;
                   12134:       }
                   12135:       if ( bucket->m < 0 ) continue;
                   12136:       col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
                   12137:       if ( !col ) {
1.37      noro     12138:         for ( t = l; NEXT(t); t = NEXT(t) )
                   12139:           ;
                   12140:         NEXT(t) = d; d = l;
1.33      noro     12141:         d = nd_reconstruct(0,d);
                   12142:         goto again2;
                   12143:       }
1.34      noro     12144:       if ( DP_Print ) fprintf(asir_out,"\nsugar=%d,",psugar);
1.33      noro     12145:       nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
                   12146:       /* adding new bases */
                   12147:       for ( r = nflist; r; r = NEXT(r) ) {
                   12148:         nfv = (NDV)BDY(r);
1.38      noro     12149:         if ( nd_f4_td ) SG(nfv) = nd_tdeg(nfv);
1.33      noro     12150:         ndv_removecont(m,nfv);
                   12151:         nh = ndv_newps(m,nfv,0);
                   12152:         d = update_pairs_s(d,nh,syzlist);
                   12153:         nd_sba_pos[nfv->sig->pos] = append_one(nd_sba_pos[nfv->sig->pos],nh);
                   12154:       }
                   12155:       for ( i = 0; i < nd_nbase; i++ )
                   12156:         for ( r = syzlist[i]; r; r = NEXT(r) )
                   12157:             d = remove_spair_s(d,(SIG)BDY(r));
                   12158:       d = remove_large_lcm(d);
                   12159:       if ( DP_Print ) {
1.34      noro     12160:         fprintf(asir_out,"f4red=%d,gblen=%d",f4red,nd_psn); fflush(asir_out);
1.33      noro     12161:       }
                   12162:       f4red++;
1.28      noro     12163:     }
                   12164:   }
                   12165:   if ( DP_Print ) {
1.34      noro     12166:     fprintf(asir_out,"\nnumber of red=%d,",Nf4_red);
1.28      noro     12167:   }
1.30      noro     12168:   g = conv_ilist_s(nd_demand,0,indp);
1.28      noro     12169:   return g;
                   12170: }

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