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

1.64    ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.63 2022/01/13 08:15:02 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);
1.64    ! noro     1306: //      if ( ret >= 0 ) { singular = 0; break; }
1.24      noro     1307:       if ( ret > 0 ) { singular = 0; break; }
1.38      noro     1308:       if ( ret == 0 ) { /* fprintf(asir_out,"s"); fflush(asir_out); */ singular = 1; }
1.24      noro     1309:     }
                   1310:   }
                   1311:   if ( singular ) return -1;
1.27      noro     1312:   else if ( i < nd_psn )
                   1313:     nd_append_red(dg,i);
                   1314:   return i;
1.24      noro     1315: }
                   1316:
1.1       noro     1317: ND nd_merge(ND p1,ND p2)
                   1318: {
                   1319:     int n,c;
                   1320:     int t,can,td1,td2;
                   1321:     ND r;
                   1322:     NM m1,m2,mr0,mr,s;
                   1323:
                   1324:     if ( !p1 ) return p2;
                   1325:     else if ( !p2 ) return p1;
                   1326:     else {
                   1327:         can = 0;
                   1328:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1329:             c = DL_COMPARE(DL(m1),DL(m2));
                   1330:             switch ( c ) {
                   1331:                 case 0:
                   1332:                     s = m1; m1 = NEXT(m1);
                   1333:                     can++; NEXTNM2(mr0,mr,s);
                   1334:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1335:                     break;
                   1336:                 case 1:
                   1337:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1338:                     break;
                   1339:                 case -1:
                   1340:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1341:                     break;
                   1342:             }
                   1343:         }
                   1344:         if ( !mr0 )
                   1345:             if ( m1 ) mr0 = m1;
                   1346:             else if ( m2 ) mr0 = m2;
                   1347:             else return 0;
                   1348:         else if ( m1 ) NEXT(mr) = m1;
                   1349:         else if ( m2 ) NEXT(mr) = m2;
                   1350:         else NEXT(mr) = 0;
                   1351:         BDY(p1) = mr0;
                   1352:         SG(p1) = MAX(SG(p1),SG(p2));
                   1353:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1354:         FREEND(p2);
                   1355:         return p1;
                   1356:     }
                   1357: }
                   1358:
                   1359: ND nd_add(int mod,ND p1,ND p2)
                   1360: {
                   1361:     int n,c;
                   1362:     int t,can,td1,td2;
                   1363:     ND r;
                   1364:     NM m1,m2,mr0,mr,s;
                   1365:
1.11      noro     1366:     Nnd_add++;
1.1       noro     1367:     if ( !p1 ) return p2;
                   1368:     else if ( !p2 ) return p1;
                   1369:     else if ( mod == -1 ) return nd_add_sf(p1,p2);
                   1370:     else if ( mod == -2 ) return nd_add_lf(p1,p2);
                   1371:     else if ( !mod ) return nd_add_q(p1,p2);
                   1372:     else {
                   1373:         can = 0;
                   1374:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1375:             c = DL_COMPARE(DL(m1),DL(m2));
                   1376:             switch ( c ) {
                   1377:                 case 0:
                   1378:                     t = ((CM(m1))+(CM(m2))) - mod;
                   1379:                     if ( t < 0 ) t += mod;
                   1380:                     s = m1; m1 = NEXT(m1);
                   1381:                     if ( t ) {
                   1382:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1383:                     } else {
                   1384:                         can += 2; FREENM(s);
                   1385:                     }
                   1386:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1387:                     break;
                   1388:                 case 1:
                   1389:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1390:                     break;
                   1391:                 case -1:
                   1392:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1393:                     break;
                   1394:             }
                   1395:         }
                   1396:         if ( !mr0 )
                   1397:             if ( m1 ) mr0 = m1;
                   1398:             else if ( m2 ) mr0 = m2;
                   1399:             else return 0;
                   1400:         else if ( m1 ) NEXT(mr) = m1;
                   1401:         else if ( m2 ) NEXT(mr) = m2;
                   1402:         else NEXT(mr) = 0;
                   1403:         BDY(p1) = mr0;
                   1404:         SG(p1) = MAX(SG(p1),SG(p2));
                   1405:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1406:         FREEND(p2);
                   1407:         return p1;
                   1408:     }
                   1409: }
                   1410:
                   1411: /* XXX on opteron, the inlined manipulation of destructive additon of
                   1412:  * two NM seems to make gcc optimizer get confused, so the part is
                   1413:  * done in a function.
                   1414:  */
                   1415:
                   1416: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
                   1417: {
                   1418:     NM s;
                   1419:     P t;
                   1420:     int can;
                   1421:
                   1422:     addp(nd_vc,CP(*m1),CP(*m2),&t);
                   1423:     s = *m1; *m1 = NEXT(*m1);
                   1424:     if ( t ) {
                   1425:         can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
                   1426:     } else {
                   1427:         can = 2; FREENM(s);
                   1428:     }
                   1429:     s = *m2; *m2 = NEXT(*m2); FREENM(s);
                   1430:     return can;
                   1431: }
                   1432:
                   1433: ND nd_add_q(ND p1,ND p2)
                   1434: {
                   1435:     int n,c,can;
                   1436:     ND r;
                   1437:     NM m1,m2,mr0,mr,s;
                   1438:     P t;
                   1439:
                   1440:     if ( !p1 ) return p2;
                   1441:     else if ( !p2 ) return p1;
                   1442:     else {
                   1443:         can = 0;
                   1444:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1445:             c = DL_COMPARE(DL(m1),DL(m2));
                   1446:             switch ( c ) {
                   1447:                 case 0:
                   1448: #if defined(__x86_64__)
                   1449:                     can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
                   1450: #else
                   1451:                     addp(nd_vc,CP(m1),CP(m2),&t);
                   1452:                     s = m1; m1 = NEXT(m1);
                   1453:                     if ( t ) {
                   1454:                         can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
                   1455:                     } else {
                   1456:                         can += 2; FREENM(s);
                   1457:                     }
                   1458:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1459: #endif
                   1460:                     break;
                   1461:                 case 1:
                   1462:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1463:                     break;
                   1464:                 case -1:
                   1465:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1466:                     break;
                   1467:             }
                   1468:         }
                   1469:         if ( !mr0 )
                   1470:             if ( m1 ) mr0 = m1;
                   1471:             else if ( m2 ) mr0 = m2;
                   1472:             else return 0;
                   1473:         else if ( m1 ) NEXT(mr) = m1;
                   1474:         else if ( m2 ) NEXT(mr) = m2;
                   1475:         else NEXT(mr) = 0;
                   1476:         BDY(p1) = mr0;
                   1477:         SG(p1) = MAX(SG(p1),SG(p2));
                   1478:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1479:         FREEND(p2);
                   1480:         return p1;
                   1481:     }
                   1482: }
                   1483:
                   1484: ND nd_add_sf(ND p1,ND p2)
                   1485: {
                   1486:     int n,c,can;
                   1487:     ND r;
                   1488:     NM m1,m2,mr0,mr,s;
                   1489:     int t;
                   1490:
                   1491:     if ( !p1 ) return p2;
                   1492:     else if ( !p2 ) return p1;
                   1493:     else {
                   1494:         can = 0;
                   1495:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1496:             c = DL_COMPARE(DL(m1),DL(m2));
                   1497:             switch ( c ) {
                   1498:                 case 0:
                   1499:                     t = _addsf(CM(m1),CM(m2));
                   1500:                     s = m1; m1 = NEXT(m1);
                   1501:                     if ( t ) {
                   1502:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1503:                     } else {
                   1504:                         can += 2; FREENM(s);
                   1505:                     }
                   1506:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1507:                     break;
                   1508:                 case 1:
                   1509:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1510:                     break;
                   1511:                 case -1:
                   1512:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1513:                     break;
                   1514:             }
                   1515:         }
                   1516:         if ( !mr0 )
                   1517:             if ( m1 ) mr0 = m1;
                   1518:             else if ( m2 ) mr0 = m2;
                   1519:             else return 0;
                   1520:         else if ( m1 ) NEXT(mr) = m1;
                   1521:         else if ( m2 ) NEXT(mr) = m2;
                   1522:         else NEXT(mr) = 0;
                   1523:         BDY(p1) = mr0;
                   1524:         SG(p1) = MAX(SG(p1),SG(p2));
                   1525:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1526:         FREEND(p2);
                   1527:         return p1;
                   1528:     }
                   1529: }
                   1530:
                   1531:
                   1532: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
                   1533: {
                   1534:     int c,c1,c2;
                   1535:     Z cg,cred,gcd,tq;
                   1536:     P cgp,credp,gcdp;
                   1537:     Obj tr,tr1;
                   1538:
                   1539:     if ( mod == -1 ) {
                   1540:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1541:         *divp = (Obj)ONE;
                   1542:     } else if ( mod == -2 ) {
                   1543:         Z inv,t;
                   1544:         divlf(ONE,HCZ(p),&inv);
                   1545:         chsgnlf(HCZ(g),&t);
                   1546:         mullf(inv,t,&CZ(mul));
                   1547:         *divp = (Obj)ONE;
                   1548:     } else if ( mod ) {
                   1549:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1550:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1551:         *divp = (Obj)ONE;
                   1552:     } else if ( nd_vc ) {
                   1553:         ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
                   1554:         divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
                   1555:         chsgnp(cgp,&CP(mul));
                   1556:         nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
                   1557:         if ( dn ) {
                   1558:             mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
                   1559:             reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
                   1560:         }
                   1561:         *divp = (Obj)credp;
                   1562:     } else {
1.6       noro     1563:         igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1564:         chsgnz(cg,&CZ(mul));
1.1       noro     1565:         nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
                   1566:         if ( dn ) {
                   1567:             mulz(dn->z,cred,&tq); dn->z = tq;
                   1568:         }
                   1569:         *divp = (Obj)cred;
                   1570:     }
                   1571:     return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
                   1572: }
                   1573:
                   1574: /* ret=1 : success, ret=0 : overflow */
1.6       noro     1575: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1       noro     1576: {
                   1577:     NM m,mrd,tail;
                   1578:     NM mul;
                   1579:     int n,sugar,psugar,sugar0,stat,index;
                   1580:     int c,c1,c2,dummy;
                   1581:     RHist h;
                   1582:     NDV p,red;
                   1583:     Q cg,cred,gcd,tq,qq;
                   1584:     Z iq;
                   1585:     DP dmul;
                   1586:     NODE node;
                   1587:     LIST hist;
                   1588:     double hmag;
                   1589:     P tp,tp1;
                   1590:     Obj tr,tr1,div;
                   1591:     union oNDC hg;
                   1592:     P cont;
                   1593:
                   1594:     if ( !g ) {
                   1595:         *rp = d;
                   1596:         return 1;
                   1597:     }
                   1598:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1599:
                   1600:     sugar0 = sugar = SG(g);
                   1601:     n = NV(g);
                   1602:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1603:     if ( d )
                   1604:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1605:     for ( ; g; ) {
                   1606:         index = ndl_find_reducer(HDL(g));
                   1607:         if ( index >= 0 ) {
                   1608:             h = nd_psh[index];
                   1609:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1610:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1611:                 nd_free(g); nd_free(d);
                   1612:                 return 0;
                   1613:             }
                   1614:             p = nd_demand ? ndv_load(index) : ps[index];
                   1615:             /* d+g -> div*(d+g)+mul*p */
1.6       noro     1616:             g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1       noro     1617:             if ( nd_gentrace ) {
                   1618:                 /* Trace=[div,index,mul,ONE] */
1.6       noro     1619:                 STOZ(index,iq);
1.1       noro     1620:                 nmtodp(mod,mul,&dmul);
                   1621:                 node = mknode(4,div,iq,dmul,ONE);
                   1622:             }
                   1623:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
                   1624:             if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
                   1625:                 hg = HCU(g);
                   1626:                 nd_removecont2(d,g);
1.6       noro     1627:                 if ( nd_gentrace ) {
1.1       noro     1628:                     /* overwrite cont : Trace=[div,index,mul,cont] */
1.6       noro     1629:                     /* exact division */
1.1       noro     1630:                     cont = ndc_div(mod,hg,HCU(g));
                   1631:                     if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
                   1632:                 }
                   1633:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1634:             }
                   1635:             MKLIST(hist,node);
                   1636:             MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
                   1637:         } else if ( !full ) {
                   1638:             *rp = g;
                   1639:             return 1;
                   1640:         } else {
                   1641:             m = BDY(g);
                   1642:             if ( NEXT(m) ) {
                   1643:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1644:             } else {
                   1645:                 FREEND(g); g = 0;
                   1646:             }
                   1647:             if ( d ) {
                   1648:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1649:             } else {
                   1650:                 MKND(n,m,1,d); tail = BDY(d);
                   1651:             }
                   1652:         }
                   1653:     }
                   1654:     if ( d ) SG(d) = sugar;
                   1655:     *rp = d;
                   1656:     return 1;
                   1657: }
                   1658:
1.24      noro     1659: // ret=1 => success
                   1660: // ret=0 => overflow
                   1661: // ret=-1 => singular top reducible
                   1662:
                   1663: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
                   1664: {
                   1665:     NM m,mrd,tail;
                   1666:     NM mul;
                   1667:     int n,sugar,psugar,sugar0,stat,index;
                   1668:     int c,c1,c2,dummy;
                   1669:     RHist h;
                   1670:     NDV p,red;
                   1671:     Q cg,cred,gcd,tq,qq;
                   1672:     Z iq;
                   1673:     DP dmul;
                   1674:     NODE node;
                   1675:     LIST hist;
                   1676:     double hmag;
                   1677:     P tp,tp1;
                   1678:     Obj tr,tr1,div;
                   1679:     union oNDC hg;
                   1680:     P cont;
                   1681:     SIG sig;
                   1682:
                   1683:     if ( !g ) {
                   1684:         *rp = d;
                   1685:         return 1;
                   1686:     }
                   1687:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1688:
                   1689:     sugar0 = sugar = SG(g);
                   1690:     n = NV(g);
                   1691:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1692:     if ( d )
                   1693:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1694:     sig = g->sig;
                   1695:     for ( ; g; ) {
                   1696:         index = ndl_find_reducer_s(HDL(g),sig);
                   1697:         if ( index >= 0 && index < nd_psn ) {
                   1698:             // reducer found
                   1699:             h = nd_psh[index];
                   1700:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1701:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1702:                 nd_free(g); nd_free(d);
                   1703:                 return 0;
                   1704:             }
                   1705:             p = ps[index];
                   1706:             /* d+g -> div*(d+g)+mul*p */
                   1707:             g = nd_reduce2(mod,d,g,p,mul,0,&div);
                   1708:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
                   1709:             if ( !mod && g && ((double)(p_mag(HCP(g))) > hmag) ) {
                   1710:                 hg = HCU(g);
                   1711:                 nd_removecont2(d,g);
                   1712:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1713:             }
1.55      noro     1714:             if ( nd_gentrace ) {
                   1715:                 /* Trace=[div,index,mul,ONE] */
                   1716:                 STOZ(index,iq);
                   1717:                 nmtodp(mod,mul,&dmul);
                   1718:                 node = mknode(4,div,iq,dmul,ONE);
                   1719:             }
                   1720:             MKLIST(hist,node);
                   1721:             MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1.24      noro     1722:         } else if ( index == -1 ) {
                   1723:           // singular top reducible
                   1724:           return -1;
                   1725:         } else if ( !full ) {
                   1726:             *rp = g;
                   1727:             g->sig = sig;
                   1728:             return 1;
                   1729:         } else {
                   1730:             m = BDY(g);
                   1731:             if ( NEXT(m) ) {
                   1732:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1733:             } else {
                   1734:                 FREEND(g); g = 0;
                   1735:             }
                   1736:             if ( d ) {
                   1737:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1738:             } else {
                   1739:                 MKND(n,m,1,d); tail = BDY(d);
                   1740:             }
                   1741:         }
                   1742:     }
                   1743:     if ( d ) {
                   1744:       SG(d) = sugar;
                   1745:       d->sig = sig;
                   1746:     }
                   1747:     *rp = d;
                   1748:     return 1;
                   1749: }
                   1750:
1.1       noro     1751: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
                   1752: {
                   1753:     int hindex,index;
                   1754:     NDV p;
                   1755:     ND u,d,red;
                   1756:     NODE l;
                   1757:     NM mul,m,mrd,tail;
                   1758:     int sugar,psugar,n,h_reducible;
                   1759:     PGeoBucket bucket;
                   1760:     int c,c1,c2;
                   1761:     Z cg,cred,gcd,zzz;
                   1762:     RHist h;
                   1763:     double hmag,gmag;
                   1764:     int count = 0;
                   1765:     int hcount = 0;
                   1766:
                   1767:     if ( !g ) {
                   1768:         *rp = 0;
                   1769:         return 1;
                   1770:     }
                   1771:     sugar = SG(g);
                   1772:     n = NV(g);
1.6       noro     1773:     if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1       noro     1774:     bucket = create_pbucket();
                   1775:     add_pbucket(mod,bucket,g);
                   1776:     d = 0;
                   1777:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1778:     while ( 1 ) {
                   1779:         if ( mod > 0 || mod == -1 )
                   1780:           hindex = head_pbucket(mod,bucket);
                   1781:         else if ( mod == -2 )
                   1782:           hindex = head_pbucket_lf(bucket);
                   1783:         else
                   1784:           hindex = head_pbucket_q(bucket);
                   1785:         if ( hindex < 0 ) {
                   1786:             if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1787:             if ( d ) SG(d) = sugar;
                   1788:             *rp = d;
                   1789:             return 1;
                   1790:         }
                   1791:         g = bucket->body[hindex];
                   1792:         index = ndl_find_reducer(HDL(g));
                   1793:         if ( index >= 0 ) {
                   1794:             count++;
                   1795:             if ( !d ) hcount++;
                   1796:             h = nd_psh[index];
                   1797:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1798:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1799:                 nd_free(d);
                   1800:                 free_pbucket(bucket);
                   1801:                 *rp = 0;
                   1802:                 return 0;
                   1803:             }
                   1804:             p = ps[index];
                   1805:             if ( mod == -1 )
                   1806:                 CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1807:             else if ( mod == -2 ) {
                   1808:                 Z inv,t;
                   1809:                 divlf(ONE,HCZ(p),&inv);
                   1810:                 chsgnlf(HCZ(g),&t);
                   1811:                 mullf(inv,t,&CZ(mul));
                   1812:             } else if ( mod ) {
                   1813:                 c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1814:                 DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1815:             } else {
1.6       noro     1816:                 igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1817:                 chsgnz(cg,&CZ(mul));
1.1       noro     1818:                 nd_mul_c_q(d,(P)cred);
                   1819:                 mulq_pbucket(bucket,cred);
                   1820:                 g = bucket->body[hindex];
1.6       noro     1821:                 gmag = (double)p_mag((P)HCZ(g));
1.1       noro     1822:             }
                   1823:             red = ndv_mul_nm(mod,mul,p);
                   1824:             bucket->body[hindex] = nd_remove_head(g);
                   1825:             red = nd_remove_head(red);
                   1826:             add_pbucket(mod,bucket,red);
                   1827:             psugar = SG(p)+TD(DL(mul));
                   1828:             sugar = MAX(sugar,psugar);
                   1829:             if ( !mod && hmag && (gmag > hmag) ) {
                   1830:                 g = normalize_pbucket(mod,bucket);
                   1831:                 if ( !g ) {
                   1832:                     if ( d ) SG(d) = sugar;
                   1833:                     *rp = d;
                   1834:                     return 1;
                   1835:                 }
                   1836:                 nd_removecont2(d,g);
1.6       noro     1837:                 hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1       noro     1838:                 add_pbucket(mod,bucket,g);
                   1839:             }
                   1840:         } else if ( !full ) {
                   1841:             g = normalize_pbucket(mod,bucket);
                   1842:             if ( g ) SG(g) = sugar;
                   1843:             *rp = g;
                   1844:             return 1;
                   1845:         } else {
                   1846:             m = BDY(g);
                   1847:             if ( NEXT(m) ) {
                   1848:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1849:             } else {
                   1850:                 FREEND(g); g = 0;
                   1851:             }
                   1852:             bucket->body[hindex] = g;
                   1853:             NEXT(m) = 0;
                   1854:             if ( d ) {
                   1855:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1856:             } else {
                   1857:                 MKND(n,m,1,d); tail = BDY(d);
                   1858:             }
                   1859:         }
                   1860:     }
                   1861: }
                   1862:
1.25      noro     1863: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *rp)
                   1864: {
                   1865:   int hindex,index;
                   1866:   NDV p;
                   1867:   ND u,d,red;
                   1868:   NODE l;
                   1869:   NM mul,m,mrd,tail;
                   1870:   int sugar,psugar,n,h_reducible;
                   1871:   PGeoBucket bucket;
                   1872:   int c,c1,c2;
                   1873:   Z cg,cred,gcd,zzz;
                   1874:   RHist h;
                   1875:   double hmag,gmag;
                   1876:   int count = 0;
                   1877:   int hcount = 0;
                   1878:   SIG sig;
                   1879:
                   1880:   if ( !g ) {
                   1881:     *rp = 0;
                   1882:     return 1;
                   1883:   }
                   1884:   sugar = SG(g);
                   1885:   n = NV(g);
                   1886:   if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
                   1887:   bucket = create_pbucket();
                   1888:   add_pbucket(mod,bucket,g);
                   1889:   d = 0;
                   1890:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1891:   sig = g->sig;
                   1892:   while ( 1 ) {
                   1893:     if ( mod > 0 || mod == -1 )
                   1894:       hindex = head_pbucket(mod,bucket);
                   1895:     else if ( mod == -2 )
                   1896:       hindex = head_pbucket_lf(bucket);
                   1897:     else
                   1898:       hindex = head_pbucket_q(bucket);
                   1899:     if ( hindex < 0 ) {
                   1900:       if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1901:       if ( d ) {
                   1902:         SG(d) = sugar;
                   1903:         d->sig = sig;
                   1904:       }
                   1905:       *rp = d;
                   1906:       return 1;
                   1907:     }
                   1908:     g = bucket->body[hindex];
                   1909:     index = ndl_find_reducer_s(HDL(g),sig);
                   1910:     if ( index >= 0 && index < nd_psn ) {
                   1911:       count++;
                   1912:       if ( !d ) hcount++;
                   1913:       h = nd_psh[index];
                   1914:       ndl_sub(HDL(g),DL(h),DL(mul));
                   1915:       if ( ndl_check_bound2(index,DL(mul)) ) {
                   1916:         nd_free(d);
                   1917:         free_pbucket(bucket);
                   1918:         *rp = 0;
                   1919:         return 0;
                   1920:       }
                   1921:       p = ps[index];
                   1922:       if ( mod == -1 )
                   1923:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1924:       else if ( mod == -2 ) {
                   1925:         Z inv,t;
                   1926:         divlf(ONE,HCZ(p),&inv);
                   1927:         chsgnlf(HCZ(g),&t);
                   1928:         mullf(inv,t,&CZ(mul));
                   1929:       } else if ( mod ) {
                   1930:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1931:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1932:       } else {
                   1933:         igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1934:         chsgnz(cg,&CZ(mul));
                   1935:         nd_mul_c_q(d,(P)cred);
                   1936:         mulq_pbucket(bucket,cred);
                   1937:         g = bucket->body[hindex];
                   1938:         gmag = (double)p_mag((P)HCZ(g));
                   1939:       }
                   1940:       red = ndv_mul_nm(mod,mul,p);
                   1941:       bucket->body[hindex] = nd_remove_head(g);
                   1942:       red = nd_remove_head(red);
                   1943:       add_pbucket(mod,bucket,red);
                   1944:       psugar = SG(p)+TD(DL(mul));
                   1945:       sugar = MAX(sugar,psugar);
                   1946:       if ( !mod && hmag && (gmag > hmag) ) {
                   1947:          g = normalize_pbucket(mod,bucket);
                   1948:          if ( !g ) {
                   1949:            if ( d ) {
                   1950:              SG(d) = sugar;
                   1951:              d->sig = sig;
                   1952:            }
                   1953:            *rp = d;
                   1954:            return 1;
                   1955:          }
                   1956:          nd_removecont2(d,g);
                   1957:          hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
                   1958:          add_pbucket(mod,bucket,g);
                   1959:       }
                   1960:     } else if ( index == -1 ) {
                   1961:       // singular top reducible
                   1962:       return -1;
                   1963:     } else if ( !full ) {
                   1964:       g = normalize_pbucket(mod,bucket);
                   1965:       if ( g ) {
                   1966:         SG(g) = sugar;
                   1967:         g->sig = sig;
                   1968:       }
                   1969:       *rp = g;
                   1970:       return 1;
                   1971:     } else {
                   1972:       m = BDY(g);
                   1973:       if ( NEXT(m) ) {
                   1974:         BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1975:       } else {
                   1976:         FREEND(g); g = 0;
                   1977:       }
                   1978:       bucket->body[hindex] = g;
                   1979:       NEXT(m) = 0;
                   1980:       if ( d ) {
                   1981:         NEXT(tail)=m; tail=m; LEN(d)++;
                   1982:       } else {
                   1983:         MKND(n,m,1,d); tail = BDY(d);
                   1984:       }
                   1985:     }
                   1986:   }
                   1987: }
                   1988:
1.1       noro     1989: /* input : list of NDV, cand : list of NDV */
                   1990:
                   1991: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
                   1992: {
                   1993:     int n,i,stat;
                   1994:     ND nf,d;
                   1995:     NDV r;
                   1996:     NODE t,s;
                   1997:     union oNDC dn;
                   1998:     Z q;
                   1999:     LIST list;
                   2000:
1.24      noro     2001:     ndv_setup(m,0,cand,nd_gentrace?1:0,1,0);
1.1       noro     2002:     n = length(cand);
                   2003:
                   2004:   if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
                   2005:     /* membercheck : list is a subset of Id(cand) ? */
                   2006:     for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
                   2007: again:
                   2008:     nd_tracelist = 0;
                   2009:         if ( nd_bpe > obpe )
                   2010:             r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
                   2011:         else
                   2012:             r = (NDV)BDY(t);
                   2013: #if 0
                   2014:         // moved to nd_f4_lf_trace()
                   2015:         if ( m == -2 ) ndv_mod(m,r);
                   2016: #endif
                   2017:         d = ndvtond(m,r);
1.6       noro     2018:         stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1       noro     2019:         if ( !stat ) {
                   2020:             nd_reconstruct(0,0);
                   2021:             goto again;
                   2022:         } else if ( nf ) return 0;
                   2023:     if ( nd_gentrace ) {
                   2024:       nd_tracelist = reverse_node(nd_tracelist);
                   2025:       MKLIST(list,nd_tracelist);
1.6       noro     2026:       STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1       noro     2027:       MKNODE(s,list,nd_alltracelist);
                   2028:       nd_alltracelist = s; nd_tracelist = 0;
                   2029:     }
                   2030:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   2031:     }
                   2032:     if ( DP_Print ) { printf("\n"); }
                   2033:     return 1;
                   2034: }
                   2035:
                   2036: ND nd_remove_head(ND p)
                   2037: {
                   2038:     NM m;
                   2039:
                   2040:     m = BDY(p);
                   2041:     if ( !NEXT(m) ) {
                   2042:         FREEND(p); p = 0;
                   2043:     } else {
                   2044:         BDY(p) = NEXT(m); LEN(p)--;
                   2045:     }
                   2046:     FREENM(m);
                   2047:     return p;
                   2048: }
                   2049:
                   2050: ND nd_separate_head(ND p,ND *head)
                   2051: {
                   2052:     NM m,m0;
                   2053:     ND r;
                   2054:
                   2055:     m = BDY(p);
                   2056:     if ( !NEXT(m) ) {
                   2057:         *head = p; p = 0;
                   2058:     } else {
                   2059:         m0 = m;
                   2060:         BDY(p) = NEXT(m); LEN(p)--;
                   2061:         NEXT(m0) = 0;
                   2062:         MKND(NV(p),m0,1,r);
                   2063:         *head = r;
                   2064:     }
                   2065:     return p;
                   2066: }
                   2067:
                   2068: PGeoBucket create_pbucket()
                   2069: {
                   2070:     PGeoBucket g;
                   2071:
                   2072:     g = CALLOC(1,sizeof(struct oPGeoBucket));
                   2073:     g->m = -1;
                   2074:     return g;
                   2075: }
                   2076:
                   2077: void free_pbucket(PGeoBucket b) {
                   2078:     int i;
                   2079:
                   2080:     for ( i = 0; i <= b->m; i++ )
                   2081:         if ( b->body[i] ) {
                   2082:             nd_free(b->body[i]);
                   2083:             b->body[i] = 0;
                   2084:         }
                   2085:     GCFREE(b);
                   2086: }
                   2087:
1.39      noro     2088: #if 0
1.1       noro     2089: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   2090: {
                   2091:     int l,i,k,m;
                   2092:
                   2093:     if ( !d )
                   2094:         return;
                   2095:     l = LEN(d);
                   2096:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2097:     /* 2^(k-1) < l <= 2^k (=m) */
                   2098:     d = nd_merge(g->body[k],d);
                   2099:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   2100:         g->body[k] = 0;
                   2101:         d = nd_merge(g->body[k+1],d);
                   2102:     }
                   2103:     g->body[k] = d;
                   2104:     g->m = MAX(g->m,k);
                   2105: }
1.39      noro     2106: #else
                   2107: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   2108: {
                   2109:   int l,i,k,m,m0;
                   2110:
                   2111:   if ( !d )
                   2112:     return;
                   2113:   m0 = g->m;
                   2114:   while ( 1 ) {
                   2115:     l = LEN(d);
                   2116:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2117:     /* 2^(k-1) < l <= 2^k (=m) */
                   2118:     if ( g->body[k] == 0 ) {
                   2119:       g->body[k] = d;
                   2120:       m0 = MAX(k,m0);
                   2121:       break;
                   2122:     } else {
                   2123:       d = nd_merge(g->body[k],d);
                   2124:       g->body[k] = 0;
                   2125:     }
                   2126:   }
                   2127:   g->m = m0;
                   2128: }
                   2129: #endif
1.1       noro     2130:
1.39      noro     2131: #if 0
1.1       noro     2132: void add_pbucket(int mod,PGeoBucket g,ND d)
                   2133: {
                   2134:     int l,i,k,m;
                   2135:
                   2136:     if ( !d )
                   2137:         return;
                   2138:     l = LEN(d);
                   2139:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2140:     /* 2^(k-1) < l <= 2^k (=m) */
                   2141:     d = nd_add(mod,g->body[k],d);
                   2142:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   2143:         g->body[k] = 0;
                   2144:         d = nd_add(mod,g->body[k+1],d);
                   2145:     }
                   2146:     g->body[k] = d;
                   2147:     g->m = MAX(g->m,k);
                   2148: }
1.39      noro     2149: #else
                   2150: void add_pbucket(int mod,PGeoBucket g,ND d)
                   2151: {
                   2152:   int l,i,k,m,m0;
                   2153:
                   2154:   m0 = g->m;
                   2155:   while ( d != 0 ) {
                   2156:     l = LEN(d);
                   2157:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2158:     /* 2^(k-1) < l <= 2^k (=m) */
                   2159:     if ( g->body[k] == 0 ) {
                   2160:       g->body[k] = d;
                   2161:       m0 = MAX(k,m0);
                   2162:       break;
                   2163:     } else {
                   2164:       d = nd_add(mod,g->body[k],d);
                   2165:       g->body[k] = 0;
                   2166:     }
                   2167:   }
                   2168:   g->m = m0;
                   2169: }
                   2170: #endif
1.1       noro     2171:
                   2172: void mulq_pbucket(PGeoBucket g,Z c)
                   2173: {
                   2174:     int k;
                   2175:
                   2176:     for ( k = 0; k <= g->m; k++ )
                   2177:         nd_mul_c_q(g->body[k],(P)c);
                   2178: }
                   2179:
                   2180: NM remove_head_pbucket_symbolic(PGeoBucket g)
                   2181: {
                   2182:     int j,i,k,c;
                   2183:     NM head;
                   2184:
                   2185:     k = g->m;
                   2186:     j = -1;
                   2187:     for ( i = 0; i <= k; i++ ) {
                   2188:         if ( !g->body[i] ) continue;
                   2189:         if ( j < 0 ) j = i;
                   2190:         else {
                   2191:             c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
                   2192:             if ( c > 0 )
                   2193:                 j = i;
                   2194:             else if ( c == 0 )
                   2195:                 g->body[i] = nd_remove_head(g->body[i]);
                   2196:         }
                   2197:     }
                   2198:     if ( j < 0 ) return 0;
                   2199:     else {
                   2200:         head = BDY(g->body[j]);
                   2201:         if ( !NEXT(head) ) {
                   2202:             FREEND(g->body[j]);
                   2203:             g->body[j] = 0;
                   2204:         } else {
                   2205:             BDY(g->body[j]) = NEXT(head);
                   2206:             LEN(g->body[j])--;
                   2207:         }
                   2208:         return head;
                   2209:     }
                   2210: }
                   2211:
                   2212: int head_pbucket(int mod,PGeoBucket g)
                   2213: {
                   2214:     int j,i,c,k,nv,sum;
                   2215:     UINT *di,*dj;
                   2216:     ND gi,gj;
                   2217:
                   2218:     k = g->m;
                   2219:     while ( 1 ) {
                   2220:         j = -1;
                   2221:         for ( i = 0; i <= k; i++ ) {
                   2222:             if ( !(gi = g->body[i]) )
                   2223:                 continue;
                   2224:             if ( j < 0 ) {
                   2225:                 j = i;
                   2226:                 gj = g->body[j];
                   2227:                 dj = HDL(gj);
                   2228:                 sum = HCM(gj);
                   2229:             } else {
                   2230:                 c = DL_COMPARE(HDL(gi),dj);
                   2231:                 if ( c > 0 ) {
                   2232:                     if ( sum ) HCM(gj) = sum;
                   2233:                     else g->body[j] = nd_remove_head(gj);
                   2234:                     j = i;
                   2235:                     gj = g->body[j];
                   2236:                     dj = HDL(gj);
                   2237:                     sum = HCM(gj);
                   2238:                 } else if ( c == 0 ) {
                   2239:                     if ( mod == -1 )
                   2240:                         sum = _addsf(sum,HCM(gi));
                   2241:                     else {
                   2242:                         sum = sum+HCM(gi)-mod;
                   2243:                         if ( sum < 0 ) sum += mod;
                   2244:                     }
                   2245:                     g->body[i] = nd_remove_head(gi);
                   2246:                 }
                   2247:             }
                   2248:         }
                   2249:         if ( j < 0 ) return -1;
                   2250:         else if ( sum ) {
                   2251:             HCM(gj) = sum;
                   2252:             return j;
                   2253:         } else
                   2254:             g->body[j] = nd_remove_head(gj);
                   2255:     }
                   2256: }
                   2257:
                   2258: int head_pbucket_q(PGeoBucket g)
                   2259: {
                   2260:     int j,i,c,k,nv;
                   2261:     Z sum,t;
                   2262:     ND gi,gj;
                   2263:
                   2264:     k = g->m;
                   2265:     while ( 1 ) {
                   2266:         j = -1;
                   2267:         for ( i = 0; i <= k; i++ ) {
                   2268:             if ( !(gi = g->body[i]) ) continue;
                   2269:             if ( j < 0 ) {
                   2270:                 j = i;
                   2271:                 gj = g->body[j];
1.6       noro     2272:                 sum = HCZ(gj);
1.1       noro     2273:             } else {
                   2274:                 nv = NV(gi);
                   2275:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   2276:                 if ( c > 0 ) {
1.6       noro     2277:                     if ( sum ) HCZ(gj) = sum;
1.1       noro     2278:                     else g->body[j] = nd_remove_head(gj);
                   2279:                     j = i;
                   2280:                     gj = g->body[j];
1.6       noro     2281:                     sum = HCZ(gj);
1.1       noro     2282:                 } else if ( c == 0 ) {
1.6       noro     2283:                     addz(sum,HCZ(gi),&t);
1.1       noro     2284:                     sum = t;
                   2285:                     g->body[i] = nd_remove_head(gi);
                   2286:                 }
                   2287:             }
                   2288:         }
                   2289:         if ( j < 0 ) return -1;
                   2290:         else if ( sum ) {
1.6       noro     2291:             HCZ(gj) = sum;
1.1       noro     2292:             return j;
                   2293:         } else
                   2294:             g->body[j] = nd_remove_head(gj);
                   2295:     }
                   2296: }
                   2297:
                   2298: int head_pbucket_lf(PGeoBucket g)
                   2299: {
                   2300:     int j,i,c,k,nv;
                   2301:     Z sum,t;
                   2302:     ND gi,gj;
                   2303:
                   2304:     k = g->m;
                   2305:     while ( 1 ) {
                   2306:         j = -1;
                   2307:         for ( i = 0; i <= k; i++ ) {
                   2308:             if ( !(gi = g->body[i]) ) continue;
                   2309:             if ( j < 0 ) {
                   2310:                 j = i;
                   2311:                 gj = g->body[j];
                   2312:                 sum = HCZ(gj);
                   2313:             } else {
                   2314:                 nv = NV(gi);
                   2315:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   2316:                 if ( c > 0 ) {
                   2317:                     if ( sum ) HCZ(gj) = sum;
                   2318:                     else g->body[j] = nd_remove_head(gj);
                   2319:                     j = i;
                   2320:                     gj = g->body[j];
                   2321:                     sum = HCZ(gj);
                   2322:                 } else if ( c == 0 ) {
                   2323:                     addlf(sum,HCZ(gi),&t);
                   2324:                     sum = t;
                   2325:                     g->body[i] = nd_remove_head(gi);
                   2326:                 }
                   2327:             }
                   2328:         }
                   2329:         if ( j < 0 ) return -1;
                   2330:         else if ( sum ) {
                   2331:             HCZ(gj) = sum;
                   2332:             return j;
                   2333:         } else
                   2334:             g->body[j] = nd_remove_head(gj);
                   2335:     }
                   2336: }
                   2337:
                   2338: ND normalize_pbucket(int mod,PGeoBucket g)
                   2339: {
                   2340:     int i;
                   2341:     ND r,t;
                   2342:
                   2343:     r = 0;
                   2344:     for ( i = 0; i <= g->m; i++ ) {
                   2345:         r = nd_add(mod,r,g->body[i]);
                   2346:         g->body[i] = 0;
                   2347:     }
                   2348:     g->m = -1;
                   2349:     return r;
                   2350: }
                   2351:
                   2352: #if 0
                   2353: void register_hcf(NDV p)
                   2354: {
                   2355:     DCP dc,t;
                   2356:     P hc,h;
                   2357:     int c;
                   2358:     NODE l,l1,prev;
                   2359:
                   2360:     hc = p->body->c.p;
                   2361:     if ( !nd_vc || NUM(hc) ) return;
                   2362:     fctrp(nd_vc,hc,&dc);
                   2363:     for ( t = dc; t; t = NEXT(t) ) {
                   2364:         h = t->c;
                   2365:         if ( NUM(h) ) continue;
                   2366:         for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   2367:             c = compp(nd_vc,h,(P)BDY(l));
                   2368:             if ( c >= 0 ) break;
                   2369:         }
                   2370:         if ( !l || c > 0  ) {
                   2371:             MKNODE(l1,h,l);
                   2372:             if ( !prev )
                   2373:                 nd_hcf = l1;
                   2374:             else
                   2375:                 NEXT(prev) = l1;
                   2376:         }
                   2377:     }
                   2378: }
                   2379: #else
                   2380: void register_hcf(NDV p)
                   2381: {
                   2382:     DCP dc,t;
                   2383:     P hc,h,q;
                   2384:     Q dmy;
                   2385:     int c;
                   2386:     NODE l,l1,prev;
                   2387:
                   2388:     hc = p->body->c.p;
                   2389:     if ( NUM(hc) ) return;
                   2390:     ptozp(hc,1,&dmy,&h);
                   2391: #if 1
                   2392:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   2393:         while ( 1 ) {
                   2394:             if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
                   2395:             else break;
                   2396:         }
                   2397:     }
                   2398:     if ( NUM(h) ) return;
                   2399: #endif
                   2400:     for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   2401:         c = compp(nd_vc,h,(P)BDY(l));
                   2402:         if ( c >= 0 ) break;
                   2403:     }
                   2404:     if ( !l || c > 0  ) {
                   2405:         MKNODE(l1,h,l);
                   2406:         if ( !prev )
                   2407:             nd_hcf = l1;
                   2408:         else
                   2409:             NEXT(prev) = l1;
                   2410:     }
                   2411: }
                   2412: #endif
                   2413:
                   2414: int do_diagonalize(int sugar,int m)
                   2415: {
1.6       noro     2416:   int i,nh,stat;
                   2417:   NODE r,g,t;
                   2418:   ND h,nf,s,head;
                   2419:   NDV nfv;
                   2420:   Q q;
                   2421:   P nm,nmp,dn,mnp,dnp,cont,cont1;
                   2422:   union oNDC hc;
                   2423:   NODE node;
                   2424:   LIST l;
                   2425:   Z iq;
1.1       noro     2426:
1.64    ! noro     2427:   if ( diag_period == 0 ) return 1;
1.6       noro     2428:   for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   2429:     if ( nd_gentrace ) {
                   2430:       /* Trace = [1,index,1,1] */
                   2431:       STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2432:       MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2433:     }
                   2434:     if ( nd_demand )
                   2435:       nfv = ndv_load(i);
                   2436:     else
                   2437:       nfv = nd_ps[i];
                   2438:     s = ndvtond(m,nfv);
                   2439:     s = nd_separate_head(s,&head);
                   2440:     stat = nd_nf(m,head,s,nd_ps,1,&nf);
                   2441:     if ( !stat ) return 0;
                   2442:     ndv_free(nfv);
                   2443:     hc = HCU(nf); nd_removecont(m,nf);
                   2444:     /* exact division */
                   2445:     cont = ndc_div(m,hc,HCU(nf));
1.1       noro     2446:     if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6       noro     2447:     nfv = ndtondv(m,nf);
                   2448:     nd_free(nf);
                   2449:     nd_bound[i] = ndv_compute_bound(nfv);
                   2450:     if ( !m ) register_hcf(nfv);
                   2451:     if ( nd_demand ) {
                   2452:       ndv_save(nfv,i);
                   2453:       ndv_free(nfv);
                   2454:     } else
                   2455:       nd_ps[i] = nfv;
                   2456:   }
                   2457:   return 1;
1.1       noro     2458: }
                   2459:
                   2460: LIST compute_splist()
                   2461: {
                   2462:   NODE g,tn0,tn,node;
                   2463:   LIST l0;
                   2464:   ND_pairs d,t;
                   2465:   int i;
                   2466:   Z i1,i2;
                   2467:
                   2468:     g = 0; d = 0;
                   2469:     for ( i = 0; i < nd_psn; i++ ) {
                   2470:         d = update_pairs(d,g,i,0);
                   2471:         g = update_base(g,i);
                   2472:     }
                   2473:   for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
                   2474:     NEXTNODE(tn0,tn);
1.6       noro     2475:         STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1       noro     2476:         node = mknode(2,i1,i2); MKLIST(l0,node);
                   2477:     BDY(tn) = l0;
                   2478:   }
                   2479:   if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
                   2480:   return l0;
                   2481: }
                   2482:
1.49      noro     2483: typedef struct oHPDATA {
1.56      noro     2484:   int n;
                   2485:   P hn; // HP(t)=hn(t)/((1-t^w0)*...*(1-t^w(n-1)))
1.49      noro     2486:   VECT x; // BDY(x)[i] = <<0,...,1,...,0>>
1.56      noro     2487:   int *w;
1.49      noro     2488: } *HPDATA;
                   2489:
                   2490: void make_reduced(VECT b,int nv);
                   2491: void mhp_rec(VECT b,VECT x,P t,P *r);
                   2492: P mhp_ctop(P *r,P *plist,int n);
                   2493: DL monomial_colon(DL a,DL b,int n);
1.56      noro     2494: LIST dp_monomial_hilbert_poincare(VECT b,VECT x);
                   2495: LIST dp_monomial_hilbert_poincare_weight(VECT b,VECT x,int *w);
1.49      noro     2496:
                   2497: void setup_hpdata(HPDATA final,HPDATA current)
                   2498: {
1.56      noro     2499:   int n,i,wlen;
1.49      noro     2500:   DL *p;
1.56      noro     2501:   VECT b,x;
1.49      noro     2502:   DL dl;
1.56      noro     2503:   LIST weight;
                   2504:   LIST ret;
                   2505:   int *w;
                   2506:   NODE nd;
                   2507:
                   2508:   final->n = n = nd_nvar;
                   2509:   final->hn = (P)BDY(nd_hpdata);
1.59      noro     2510: #if 0
1.56      noro     2511:   if ( NEXT(nd_hpdata) != 0 && (weight=(LIST)BDY(NEXT(nd_hpdata))) != 0 ) {
                   2512:     wlen = length(BDY(weight));
                   2513:     if ( n != wlen )
                   2514:       error("setup_hpdata : inconsistent weight length");
                   2515:     w = (int *)MALLOC(n*sizeof(int));
                   2516:     for ( i = 0, nd = BDY((LIST)weight); i < n; i++, nd = NEXT(nd) )
                   2517:       w[i] = ZTOS((Z)BDY(nd));
                   2518:   } else
                   2519:     w = 0;
1.59      noro     2520: #else
                   2521:   w = current_dl_weight_vector;
                   2522: #endif
1.49      noro     2523:   MKVECT(x,n);
                   2524:   for ( i = 0; i < n; i++ ) {
                   2525:     NEWDL(dl,n); dl->d[i] = 1; dl->td = 1; BDY(x)[i] = dl;
                   2526:   }
                   2527:   final->x = x;
1.56      noro     2528:   final->w = w;
1.49      noro     2529:
                   2530:   MKVECT(b,nd_psn); p = (DL *)BDY(b);
                   2531:   for ( i = 0; i < nd_psn; i++ ) {
                   2532:     p[i] = ndltodl(n,nd_psh[i]->dl);
                   2533:   }
1.56      noro     2534:   if ( w ) {
                   2535:     ret = dp_monomial_hilbert_poincare_weight(b,x,w);
                   2536:   } else
                   2537:     ret = dp_monomial_hilbert_poincare(b,x);
                   2538:   current->n = n;
                   2539:   current->hn = (P)BDY(BDY(ret));
1.49      noro     2540:   current->x = x;
1.56      noro     2541:   current->w = w;
                   2542: }
                   2543:
                   2544: int comp_hn(P a, P b)
                   2545: {
                   2546:   P s;
                   2547:   DCP dc;
                   2548:
                   2549:   subp(CO,a,b,&s);
1.57      noro     2550:   if ( !s ) return -1;
1.56      noro     2551:   else if ( OID(s) == 1 ) return 0;
                   2552:   else {
                   2553:     for ( dc = DC(s); NEXT(dc); dc = NEXT(dc) );
                   2554:     return (int)ZTOS((Z)dc->d);
                   2555:   }
1.49      noro     2556: }
                   2557:
1.56      noro     2558: void update_hpdata(HPDATA current,int nh)
1.49      noro     2559: {
                   2560:   NODE data1,nd,t;
                   2561:   DL new,dl;
                   2562:   int len,i,n;
                   2563:   Z dz;
                   2564:   DL *p;
                   2565:   VECT b,head;
                   2566:   P tv,td,s,hn,hpoly;
                   2567:   LIST list1;
                   2568:
                   2569:   n = nd_nvar;
                   2570:   new = ndltodl(n,nd_psh[nh]->dl);
                   2571:   MKVECT(b,nh); p = (DL *)BDY(b);
                   2572:   for ( i = 0; i < nh; i++ ) {
                   2573:     p[i] = monomial_colon(ndltodl(n,nd_psh[i]->dl),new,n);
                   2574:   }
                   2575:   // compute HP(I:new)
1.56      noro     2576:   if ( current->w )
                   2577:     list1 = dp_monomial_hilbert_poincare_weight(b,current->x,current->w);
                   2578:   else
                   2579:     list1 = dp_monomial_hilbert_poincare(b,current->x);
1.49      noro     2580:   data1 = BDY((LIST)list1);
                   2581:   // HP(I+<new>) = H(I)-t^d*H(I:new), d=tdeg(new)
                   2582:   makevar("t",&tv); UTOZ(new->td,dz);
                   2583:   pwrp(CO,tv,dz,&td);
                   2584:   mulp(CO,(P)ARG0(data1),td,&s);
                   2585:   subp(CO,current->hn,s,&hn);
                   2586:   current->hn = hn;
                   2587: }
                   2588:
                   2589: ND_pairs nd_remove_same_sugar( ND_pairs d, int sugar)
                   2590: {
                   2591:   struct oND_pairs root;
                   2592:   ND_pairs prev,cur;
1.61      noro     2593:   int i;
1.49      noro     2594:
                   2595:   root.next = d;
                   2596:   prev = &root; cur = d;
1.61      noro     2597:   i = 0;
1.49      noro     2598:   while ( cur ) {
1.61      noro     2599:     if ( SG(cur) == sugar ) {
1.49      noro     2600:       prev->next = cur->next;
1.61      noro     2601:       i++;
                   2602:     } else
1.49      noro     2603:       prev = cur;
                   2604:     cur = cur->next;
                   2605:   }
1.63      noro     2606:   if ( DP_Print && i ) fprintf(asir_out,"[%d]",i);
1.49      noro     2607:   return root.next;
                   2608: }
                   2609:
1.1       noro     2610: /* return value = 0 => input is not a GB */
                   2611:
                   2612: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
                   2613: {
1.6       noro     2614:   int i,nh,sugar,stat;
                   2615:   NODE r,g,t;
                   2616:   ND_pairs d;
                   2617:   ND_pairs l;
                   2618:   ND h,nf,s,head,nf1;
                   2619:   NDV nfv;
                   2620:   Z q;
                   2621:   union oNDC dn,hc;
                   2622:   int diag_count = 0;
1.41      noro     2623:   int Nnfnz = 0,Nnfz = 0;
1.6       noro     2624:   P cont;
                   2625:   LIST list;
1.49      noro     2626:   struct oHPDATA current_hpdata,final_hpdata;
                   2627:   int final_hpvalue;
                   2628:
1.27      noro     2629: struct oEGT eg1,eg2,eg_update;
1.6       noro     2630:
1.27      noro     2631: init_eg(&eg_update);
1.11      noro     2632:   Nnd_add = 0;
1.6       noro     2633:   g = 0; d = 0;
                   2634:   for ( i = 0; i < nd_psn; i++ ) {
                   2635:     d = update_pairs(d,g,i,gensyz);
                   2636:     g = update_base(g,i);
                   2637:   }
                   2638:   sugar = 0;
1.49      noro     2639:   if ( nd_hpdata ) {
1.52      noro     2640:     if ( DP_Print ) fprintf(asir_out,"Hilbert driven algorithm.\n");
1.49      noro     2641:     setup_hpdata(&final_hpdata,&current_hpdata);
                   2642:   }
1.6       noro     2643:   while ( d ) {
1.1       noro     2644: again:
1.6       noro     2645:     l = nd_minp(d,&d);
                   2646:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   2647:     if ( SG(l) != sugar ) {
                   2648:       if ( ishomo ) {
                   2649:         diag_count = 0;
                   2650:         stat = do_diagonalize(sugar,m);
1.1       noro     2651:         if ( !stat ) {
1.6       noro     2652:           NEXT(l) = d; d = l;
                   2653:           d = nd_reconstruct(0,d);
                   2654:           goto again;
1.1       noro     2655:         }
1.6       noro     2656:       }
                   2657:       sugar = SG(l);
                   2658:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2659:     }
                   2660:     stat = nd_sp(m,0,l,&h);
                   2661:     if ( !stat ) {
                   2662:       NEXT(l) = d; d = l;
                   2663:       d = nd_reconstruct(0,d);
                   2664:       goto again;
                   2665:     }
1.1       noro     2666: #if USE_GEOBUCKET
1.39      noro     2667:     stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf)
                   2668:       :nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     2669: #else
1.39      noro     2670:     stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     2671: #endif
1.6       noro     2672:     if ( !stat ) {
                   2673:       NEXT(l) = d; d = l;
                   2674:       d = nd_reconstruct(0,d);
                   2675:       goto again;
                   2676:     } else if ( nf ) {
1.41      noro     2677:       Nnfnz++;
1.6       noro     2678:       if ( checkonly || gensyz ) return 0;
1.1       noro     2679:       if ( nd_newelim ) {
                   2680:         if ( nd_module ) {
                   2681:           if ( MPOS(HDL(nf)) > 1 ) return 0;
                   2682:         } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
                   2683:       }
1.6       noro     2684:       if ( DP_Print ) { printf("+"); fflush(stdout); }
                   2685:       hc = HCU(nf);
                   2686:       nd_removecont(m,nf);
                   2687:       if ( !m && nd_nalg ) {
                   2688:         nd_monic(0,&nf);
                   2689:         nd_removecont(m,nf);
                   2690:       }
                   2691:       if ( nd_gentrace ) {
                   2692:         /* exact division */
1.1       noro     2693:         cont = ndc_div(m,hc,HCU(nf));
                   2694:         if ( m || !UNIQ(cont) ) {
1.6       noro     2695:           t = mknode(4,NULLP,NULLP,NULLP,cont);
                   2696:           MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1       noro     2697:           nd_tracelist = t;
                   2698:         }
                   2699:       }
1.6       noro     2700:       nfv = ndtondv(m,nf); nd_free(nf);
1.24      noro     2701:       nh = ndv_newps(m,nfv,0);
1.6       noro     2702:       if ( !m && (ishomo && ++diag_count == diag_period) ) {
                   2703:         diag_count = 0;
                   2704:         stat = do_diagonalize(sugar,m);
                   2705:         if ( !stat ) {
                   2706:           NEXT(l) = d; d = l;
                   2707:           d = nd_reconstruct(1,d);
                   2708:           goto again;
1.1       noro     2709:         }
1.6       noro     2710:       }
1.27      noro     2711: get_eg(&eg1);
1.6       noro     2712:       d = update_pairs(d,g,nh,0);
1.27      noro     2713: get_eg(&eg2); add_eg(&eg_update,&eg1,&eg2);
1.6       noro     2714:       g = update_base(g,nh);
                   2715:       FREENDP(l);
1.49      noro     2716:       if ( nd_hpdata ) {
1.57      noro     2717:         int dg,sugar0;
1.56      noro     2718:
                   2719:         update_hpdata(&current_hpdata,nh);
                   2720:         dg = comp_hn(final_hpdata.hn,current_hpdata.hn);
1.57      noro     2721:         if ( dg < 0 ) {
1.61      noro     2722:            int d_len;
                   2723:            for ( d_len = 0; d; d = d->next, d_len++);
                   2724:            fprintf(asir_out,"[%d] We found a gb\n",d_len);
1.57      noro     2725:         }
                   2726:         sugar0 = sugar;
                   2727:         while ( d && dg > sugar0 ) {
1.58      noro     2728:           d = nd_remove_same_sugar(d,sugar0);
1.57      noro     2729:           sugar0++;
1.49      noro     2730:         }
                   2731:       }
1.41      noro     2732:     } else {
                   2733:       Nnfz++;
                   2734:       if ( nd_gentrace && gensyz ) {
                   2735:         nd_tracelist = reverse_node(nd_tracelist);
                   2736:         MKLIST(list,nd_tracelist);
                   2737:         STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
                   2738:         MKNODE(t,list,nd_alltracelist);
                   2739:         nd_alltracelist = t; nd_tracelist = 0;
                   2740:       }
1.57      noro     2741:       if ( DP_Print ) { fprintf(asir_out,"."); fflush(asir_out); }
1.41      noro     2742:         FREENDP(l);
                   2743:     }
1.36      noro     2744:   }
                   2745:   conv_ilist(nd_demand,0,g,indp);
1.41      noro     2746:   if ( !checkonly && DP_Print ) {
1.60      noro     2747:     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     2748:     fprintf(asir_out,"Nremoved=%d\n",NcriB+NcriMF+Ncri2);
1.41      noro     2749:     fflush(asir_out);
                   2750:   }
                   2751:   if ( DP_Print ) {
                   2752:     print_eg("update",&eg_update); fprintf(asir_out,"\n");
                   2753:   }
1.36      noro     2754:   return g;
1.1       noro     2755: }
                   2756:
1.30      noro     2757: ND_pairs update_pairs_s(ND_pairs d,int t,NODE *syz);
1.47      noro     2758: int update_pairs_array_s(ND_pairs *d,int t,NODE *syz);
1.30      noro     2759: ND_pairs nd_newpairs_s(int t ,NODE *syz);
1.47      noro     2760: ND_pairs *nd_newpairs_array_s(int t ,NODE *syz);
1.24      noro     2761:
                   2762: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *nf);
                   2763: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
                   2764:
                   2765: void _copydl(int n,DL d1,DL d2);
                   2766: void _subfromdl(int n,DL d1,DL d2);
                   2767: extern int (*cmpdl)(int n,DL d1,DL d2);
1.53      noro     2768: int _dl_redble_ext(DL,DL,DL,int);
                   2769:
                   2770: int primitive_irred(ND p,SIG sig)
                   2771: {
                   2772:   static int wpd=0,dlen=0;
                   2773:   static DL dquo,squo;
                   2774:   static UINT *quo;
                   2775:   int i;
                   2776:
                   2777:   if ( dlen < nd_nvar ) {
                   2778:     NEWDL(dquo,nd_nvar);
                   2779:     NEWDL(squo,nd_nvar);
                   2780:     dlen = nd_nvar;
                   2781:   }
                   2782:   if ( wpd != nd_wpd ) {
                   2783:     wpd = nd_wpd;
                   2784:     quo = (UINT *)MALLOC(wpd*sizeof(UINT));
                   2785:   }
                   2786:   for ( i = 0; i < nd_psn; i++ ) {
                   2787:     if ( sig->pos == nd_psh[i]->sig->pos &&
                   2788:       _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),squo,nd_nvar) )
                   2789:       if ( ndl_reducible(HDL(p),DL(nd_psh[i])) ) {
                   2790:         if ( DP_Print ) fprintf(asir_out,"D");
                   2791:         ndl_sub(HDL(p),DL(nd_psh[i]),quo);
                   2792:         _ndltodl(quo,dquo);
                   2793:         if ( _eqdl(nd_nvar,squo,dquo) )
                   2794:           return 0;
                   2795:       }
                   2796:   }
                   2797:   return 1;
                   2798: }
1.24      noro     2799:
                   2800: NODE insert_sig(NODE l,SIG s)
                   2801: {
                   2802:   int pos;
                   2803:   DL sig;
                   2804:   struct oNODE root;
                   2805:   NODE p,prev,r;
                   2806:   SIG t;
                   2807:
                   2808:   pos = s->pos; sig = DL(s);
                   2809:   root.next = l; prev = &root;
                   2810:   for ( p = l; p; p = p->next ) {
                   2811:     t = (SIG)p->body;
                   2812:     if ( t->pos == pos ) {
                   2813:       if ( _dl_redble(DL(t),sig,nd_nvar) )
                   2814:         return root.next;
                   2815:       else if ( _dl_redble(sig,DL(t),nd_nvar) )
                   2816:         // remove p
                   2817:         prev->next = p->next;
1.45      noro     2818:       else
                   2819:         prev = p;
1.24      noro     2820:     } else
                   2821:       prev = p;
                   2822:   }
1.27      noro     2823:   NEWNODE(r); r->body = (pointer)s; r->next = 0;
                   2824:   for ( p = &root; p->next; p = p->next );
                   2825:   p->next = r;
                   2826: //  r->next = root.next;
                   2827: //  return r;
                   2828:   return root.next;
1.24      noro     2829: }
                   2830:
                   2831: ND_pairs remove_spair_s(ND_pairs d,SIG sig)
                   2832: {
                   2833:   struct oND_pairs root;
                   2834:   ND_pairs prev,p;
                   2835:   SIG spsig;
                   2836:
                   2837:   root.next = d;
                   2838:   prev = &root; p = d;
                   2839:   while ( p ) {
                   2840:     spsig = p->sig;
1.26      noro     2841:     if ( sig->pos == spsig->pos && _dl_redble(DL(sig),DL(spsig),nd_nvar) ) {
1.24      noro     2842:       // remove p
                   2843:       prev->next = p->next;
1.26      noro     2844:       Nsyz++;
                   2845:     } else
1.24      noro     2846:       prev = p;
                   2847:     p = p->next;
                   2848:   }
                   2849:   return (ND_pairs)root.next;
                   2850: }
                   2851:
1.28      noro     2852: int small_lcm(ND_pairs l)
                   2853: {
                   2854:   SIG sig;
                   2855:   int i;
1.29      noro     2856:   NODE t;
1.28      noro     2857:   static DL lcm,mul,quo;
1.44      noro     2858:   static int nvar = 0;
1.28      noro     2859:
1.38      noro     2860:   if ( nd_sba_largelcm ) return 0;
1.28      noro     2861:   if ( nvar < nd_nvar ) {
                   2862:     nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(quo,nvar); NEWDL(mul,nvar);
                   2863:   }
                   2864:   sig = l->sig;
                   2865:   _ndltodl(l->lcm,lcm);
1.29      noro     2866: #if 0
1.28      noro     2867:   for ( i = 0; i < nd_psn; i++ ) {
                   2868:     if ( sig->pos == nd_psh[i]->sig->pos &&
1.29      noro     2869:       _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
1.28      noro     2870:       _ndltodl(DL(nd_psh[i]),mul);
                   2871:       _addtodl(nd_nvar,quo,mul);
                   2872:       if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
                   2873:         break;
                   2874:     }
                   2875:   }
                   2876:   if ( i < nd_psn ) return 1;
                   2877:   else return 0;
1.29      noro     2878: #else
                   2879:   for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
                   2880:     i = (long)BDY(t);
                   2881:     if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
                   2882:       _ndltodl(DL(nd_psh[i]),mul);
                   2883:       _addtodl(nd_nvar,quo,mul);
                   2884:       if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
                   2885:         break;
                   2886:     }
                   2887:   }
                   2888:   if ( t ) return 1;
                   2889:   else return 0;
                   2890: #endif
1.28      noro     2891: }
                   2892:
1.41      noro     2893: ND_pairs find_smallest_lcm(ND_pairs l)
                   2894: {
                   2895:   SIG sig;
                   2896:   int i,minindex;
                   2897:   NODE t;
                   2898:   ND_pairs r;
                   2899:   struct oSIG sig1;
1.55      noro     2900:   static DL mul,quo,quo2,minlm;
1.44      noro     2901:   static int nvar = 0;
1.41      noro     2902:
                   2903:   if ( nvar < nd_nvar ) {
                   2904:     nvar = nd_nvar;
1.55      noro     2905:     NEWDL(quo,nvar); NEWDL(quo2,nvar); NEWDL(mul,nvar);
1.41      noro     2906:     NEWDL(minlm,nvar);
                   2907:   }
                   2908:   sig = l->sig;
                   2909:   // find mg s.t. m*s(g)=sig and m*lm(g) is minimal
                   2910:   _ndltodl(l->lcm,minlm); minindex = -1;
                   2911:   for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
                   2912:     i = (long)BDY(t);
                   2913:     if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
                   2914:       _ndltodl(DL(nd_psh[i]),mul);
                   2915:       _addtodl(nd_nvar,quo,mul);
                   2916:       if ( (*cmpdl)(nd_nvar,minlm,mul) > 0 ) {
                   2917:         minindex = i;
1.47      noro     2918:         break;
1.41      noro     2919:         _copydl(nd_nvar,mul,minlm);
                   2920:       }
                   2921:     }
                   2922:   }
                   2923:   // l->lcm is minimal; return l itself
                   2924:   if ( minindex < 0 ) return l;
1.47      noro     2925:   else return 0;
1.41      noro     2926:   for ( i = 0; i < nd_psn; i++ ) {
                   2927:     if ( i == minindex ) continue;
                   2928:     _ndltodl(DL(nd_psh[i]),mul);
                   2929:     if ( _dl_redble_ext(mul,minlm,quo,nd_nvar) ) {
                   2930:       _addtodl(nd_nvar,nd_ps[i]->sig->dl,quo);
                   2931:       sig1.pos = nd_ps[i]->sig->pos;
                   2932:       sig1.dl = quo;
1.55      noro     2933:       sig1.dl2 = quo2;
                   2934:       _adddl(nd_nvar,sig1.dl,nd_sba_hm[sig1.pos],sig1.dl2);
1.41      noro     2935:       if ( comp_sig(sig,&sig1) > 0 ) {
                   2936: //        printf("X");
                   2937:         NEWND_pairs(r);
                   2938:         r->sig = sig;
                   2939:         r->i1 = minindex;
                   2940:         r->i2 = i;
                   2941:         dltondl(nd_nvar,minlm,r->lcm);
                   2942:         r->next = 0;
                   2943:         return r;
                   2944:       }
                   2945:     }
                   2946:   }
                   2947:   // there is no suitable spair
                   2948:   return 0;
                   2949: }
                   2950:
1.28      noro     2951: ND_pairs remove_large_lcm(ND_pairs d)
                   2952: {
                   2953:   struct oND_pairs root;
                   2954:   ND_pairs prev,p;
                   2955:
                   2956:   root.next = d;
                   2957:   prev = &root; p = d;
                   2958:   while ( p ) {
1.41      noro     2959: #if 0
1.28      noro     2960:     if ( small_lcm(p) ) {
                   2961:       // remove p
                   2962:       prev->next = p->next;
                   2963:     } else
1.41      noro     2964: #else
                   2965:     if ( find_smallest_lcm(p) == 0 ) {
                   2966:       // remove p
                   2967:       prev->next = p->next;
                   2968:     } else
                   2969: #endif
1.28      noro     2970:       prev = p;
                   2971:     p = p->next;
                   2972:   }
                   2973:   return (ND_pairs)root.next;
                   2974: }
                   2975:
1.26      noro     2976: struct oEGT eg_create,eg_newpairs,eg_merge;
                   2977:
1.30      noro     2978: NODE conv_ilist_s(int demand,int trace,int **indp);
                   2979:
1.44      noro     2980: // S(fj*ei-fi*ej)
                   2981:
1.45      noro     2982: void _subdl(int,DL,DL,DL);
                   2983:
1.44      noro     2984: SIG trivial_sig(int i,int j)
                   2985: {
1.45      noro     2986:   static DL lcm;
1.44      noro     2987:   static struct oSIG sigi,sigj;
                   2988:   static int nvar = 0;
                   2989:   SIG sig;
                   2990:
                   2991:   if ( nvar != nd_nvar ) {
1.45      noro     2992:     nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(sigi.dl,nvar); NEWDL(sigj.dl,nvar);
1.55      noro     2993:     NEWDL(sigi.dl2,nvar); NEWDL(sigj.dl2,nvar);
1.45      noro     2994:   }
1.46      noro     2995:   if ( nd_sba_inputisgb != 0 ) {
1.45      noro     2996:     lcm_of_DL(nd_nvar,nd_sba_hm[i],nd_sba_hm[j],lcm);
                   2997:     sigi.pos = i; _subdl(nd_nvar,lcm,nd_sba_hm[i],sigi.dl);
1.55      noro     2998:     _copydl(nd_nvar,lcm,sigi.dl2);
1.45      noro     2999:     sigj.pos = j; _subdl(nd_nvar,lcm,nd_sba_hm[j],sigj.dl);
1.55      noro     3000:     _copydl(nd_nvar,lcm,sigj.dl2);
1.45      noro     3001:     if ( comp_sig(&sigi,&sigj) > 0 ) sig = dup_sig(&sigi);
                   3002:     else sig = dup_sig(&sigj);
                   3003:   } else {
                   3004:     sigi.pos = i; _copydl(nd_nvar,nd_sba_hm[j],sigi.dl);
1.55      noro     3005:     _adddl(nd_nvar,sigi.dl,nd_sba_hm[i],sigi.dl2);
1.45      noro     3006:     sigj.pos = j; _copydl(nd_nvar,nd_sba_hm[i],sigj.dl);
1.55      noro     3007:     _adddl(nd_nvar,sigj.dl,nd_sba_hm[j],sigj.dl2);
1.45      noro     3008:     if ( comp_sig(&sigi,&sigj) > 0 ) sig = dup_sig(&sigi);
                   3009:     else sig = dup_sig(&sigj);
1.44      noro     3010:   }
                   3011:   return sig;
                   3012: }
                   3013:
1.47      noro     3014: int nd_minsig(ND_pairs *d)
                   3015: {
                   3016:   int min,i,ret;
                   3017:
                   3018:   min = -1;
                   3019:   for ( i = 0; i < nd_nbase; i++ ) {
                   3020:     if ( d[i] != 0 ) {
                   3021:       if ( min < 0 ) min = i;
                   3022:       else {
                   3023:         ret = comp_sig(d[i]->sig,d[min]->sig);
                   3024:         if ( ret < 0 ) min = i;
                   3025:       }
                   3026:     }
                   3027:   }
                   3028:   return min;
                   3029: }
                   3030:
                   3031: int dlength(ND_pairs d)
                   3032: {
                   3033:   int i;
                   3034:   for ( i = 0; d; d = d->next, i++ );
                   3035:   return i;
                   3036: }
                   3037:
1.44      noro     3038: NODE nd_sba_buch(int m,int ishomo,int **indp,NODE *syzp)
1.24      noro     3039: {
1.39      noro     3040:   int i,j,nh,sugar,stat,pos;
1.30      noro     3041:   NODE r,t,g;
1.47      noro     3042:   ND_pairs *d;
1.41      noro     3043:   ND_pairs l,l1;
1.24      noro     3044:   ND h,nf,s,head,nf1;
                   3045:   NDV nfv;
                   3046:   Z q;
                   3047:   union oNDC dn,hc;
                   3048:   P cont;
                   3049:   LIST list;
                   3050:   SIG sig;
1.29      noro     3051:   NODE *syzlist;
1.47      noro     3052:   int ngen,ind;
1.41      noro     3053:   int Nnominimal,Nredundant;
1.28      noro     3054:   DL lcm,quo,mul;
1.49      noro     3055:   struct oHPDATA final_hpdata,current_hpdata;
1.50      noro     3056:   struct oEGT eg1,eg2,eg3,eg4,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
                   3057:   struct oEGT eg_minsig,eg_smallest,eg_removecont,eg_hpdata,eg_updatepairs,eg_sbabuch,eg_sp;
1.47      noro     3058:   int Nnfs=0,Nnfz=0,Nnfnz=0,dlen,nsyz;
1.24      noro     3059:
1.27      noro     3060: init_eg(&eg_remove);
1.29      noro     3061:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.47      noro     3062:   d = (ND_pairs *)MALLOC(nd_psn*sizeof(ND_pairs));
                   3063:   nd_nbase = nd_psn;
1.26      noro     3064:   Nsyz = 0;
1.24      noro     3065:   Nnd_add = 0;
1.41      noro     3066:   Nnominimal = 0;
1.26      noro     3067:   Nredundant = 0;
1.44      noro     3068:   ngen = nd_psn;
1.56      noro     3069:   if ( !do_weyl || nd_sba_inputisgb ) {
1.48      noro     3070:     for ( i = 0; i < nd_psn; i++ )
                   3071:       for ( j = i+1; j < nd_psn; j++ ) {
                   3072:         sig = trivial_sig(i,j);
                   3073:         syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
                   3074:       }
1.29      noro     3075:     }
1.47      noro     3076:   dlen = 0;
1.41      noro     3077:   for ( i = 0; i < nd_psn; i++ ) {
1.47      noro     3078:     dlen += update_pairs_array_s(d,i,syzlist);
1.41      noro     3079:   }
1.24      noro     3080:   sugar = 0;
1.39      noro     3081:   pos = 0;
1.49      noro     3082:   if ( nd_hpdata ) {
                   3083:     setup_hpdata(&final_hpdata,&current_hpdata);
                   3084:   }
1.26      noro     3085:   NEWDL(lcm,nd_nvar); NEWDL(quo,nd_nvar); NEWDL(mul,nd_nvar);
1.50      noro     3086: init_eg(&eg_sp);
1.26      noro     3087: init_eg(&eg_create);
                   3088: init_eg(&eg_merge);
1.47      noro     3089: init_eg(&eg_minsig);
                   3090: init_eg(&eg_smallest);
1.29      noro     3091: init_eg(&eg_large);
1.31      noro     3092: init_eg(&eg_nf);
                   3093: init_eg(&eg_nfzero);
1.50      noro     3094: init_eg(&eg_removecont);
                   3095: init_eg(&eg_updatepairs);
                   3096: init_eg(&eg_hpdata);
                   3097: init_eg(&eg_sbabuch);
                   3098: get_eg(&eg3);
1.47      noro     3099:   while ( 1 ) {
1.53      noro     3100:     if ( DP_Print && !nd_hpdata && dlen%1000 == 0 ) fprintf(asir_out,"(%d)",dlen);
1.47      noro     3101: again :
                   3102: get_eg(&eg1);
                   3103:     ind = nd_minsig(d);
                   3104: get_eg(&eg2); add_eg(&eg_minsig,&eg1,&eg2);
                   3105:     if ( ind < 0 ) break;
                   3106:     l = d[ind];
                   3107: //    printf("(%d,%d)",l->i1,l->i2); print_sig(l->sig); printf("\n");
                   3108: get_eg(&eg1);
1.41      noro     3109:     l1 = find_smallest_lcm(l);
1.47      noro     3110: get_eg(&eg2); add_eg(&eg_smallest,&eg1,&eg2);
1.41      noro     3111:     if ( l1 == 0 ) {
1.47      noro     3112:       d[ind] = d[ind]->next; dlen--;
1.53      noro     3113: //      if ( DP_Print && !nd_hpdata ) fprintf(asir_out,"M");
1.41      noro     3114:       Nnominimal++;
                   3115:       continue;
                   3116:     }
                   3117:     if ( SG(l1) != sugar ) {
                   3118:       sugar = SG(l1);
                   3119:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   3120:     }
                   3121:     sig = l1->sig;
                   3122:     if ( DP_Print && nd_sba_pot ) {
                   3123:       if ( sig->pos != pos ) {
                   3124:         fprintf(asir_out,"[%d]",sig->pos);
                   3125:         pos = sig->pos;
                   3126:       }
                   3127:     }
1.50      noro     3128: get_eg(&eg1);
1.41      noro     3129:     stat = nd_sp(m,0,l1,&h);
1.50      noro     3130: get_eg(&eg2); add_eg(&eg_sp,&eg1,&eg2);
1.24      noro     3131:     if ( !stat ) {
1.47      noro     3132:       nd_reconstruct_s(0,d);
1.24      noro     3133:       goto again;
                   3134:     }
1.31      noro     3135: get_eg(&eg1);
1.25      noro     3136: #if USE_GEOBUCKET
1.55      noro     3137:     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     3138: #else
1.39      noro     3139:     stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.24      noro     3140: #endif
1.31      noro     3141: get_eg(&eg2);
1.24      noro     3142:     if ( !stat ) {
1.47      noro     3143:       nd_reconstruct_s(0,d);
1.24      noro     3144:       goto again;
                   3145:     } else if ( stat == -1 ) {
1.47      noro     3146:       d[ind] = d[ind]->next; dlen--;
1.41      noro     3147:       Nnfs++;
1.24      noro     3148:       if ( DP_Print ) { printf("S"); fflush(stdout); }
                   3149:     } else if ( nf ) {
1.47      noro     3150:       d[ind] = d[ind]->next; dlen--;
1.41      noro     3151:       Nnfnz++;
                   3152:       if ( DP_Print ) {
                   3153:         if ( nd_sba_redundant_check ) {
                   3154:           if ( ndl_find_reducer_nonsig(HDL(nf)) >= 0 ) {
                   3155:             Nredundant++;
                   3156:             printf("R");
                   3157:           } else
                   3158:             printf("+");
                   3159:         } else
                   3160:           printf("+");
                   3161:         fflush(stdout);
                   3162:       }
1.31      noro     3163:       add_eg(&eg_nf,&eg1,&eg2);
1.24      noro     3164:       hc = HCU(nf);
1.55      noro     3165:        get_eg(&eg1);
                   3166:        nd_removecont(m,nf);
                   3167:        get_eg(&eg2); add_eg(&eg_removecont,&eg1,&eg2);
                   3168:        nfv = ndtondv(m,nf); nd_free(nf);
                   3169:        nh = ndv_newps(m,nfv,0);
                   3170:
                   3171:        get_eg(&eg1);
                   3172:        dlen += update_pairs_array_s(d,nh,syzlist);
                   3173:        get_eg(&eg2); add_eg(&eg_updatepairs,&eg1,&eg2);
                   3174:        nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
                   3175:        if ( nd_hpdata ) {
1.63      noro     3176:          int dg,sugar0;
                   3177:
1.55      noro     3178:          get_eg(&eg1);
1.56      noro     3179:          update_hpdata(&current_hpdata,nh);
1.55      noro     3180:          get_eg(&eg2); add_eg(&eg_hpdata,&eg1,&eg2);
1.63      noro     3181:          dg = comp_hn(final_hpdata.hn,current_hpdata.hn);
                   3182: //         if ( !compp(CO,final_hpdata.hn,current_hpdata.hn) )
                   3183:          if ( dg < 0 ) {
1.55      noro     3184:            if ( DP_Print ) { printf("\nWe found a gb.\n"); }
                   3185:              break;
1.63      noro     3186:          } else if ( nd_sba_heu == 1 ) {
                   3187:            for ( i = 0; i < ngen; i++ ) {
                   3188:              sugar0 = sugar;
                   3189:              while ( d[i] && dg > sugar0 ) {
                   3190:                d[i] = nd_remove_same_sugar(d[i],sugar0);
                   3191:                sugar0++;
                   3192:              }
                   3193:           }
                   3194:         }
1.49      noro     3195:       }
1.24      noro     3196:    } else {
1.47      noro     3197:       d[ind] = d[ind]->next; dlen--;
1.41      noro     3198:       Nnfz++;
1.31      noro     3199:       add_eg(&eg_nfzero,&eg1,&eg2);
1.24      noro     3200:      // syzygy
1.27      noro     3201: get_eg(&eg1);
1.47      noro     3202:      nsyz = Nsyz;
                   3203:      d[sig->pos] = remove_spair_s(d[sig->pos],sig);
                   3204:      dlen -= Nsyz-nsyz;
1.27      noro     3205: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29      noro     3206:      syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.24      noro     3207:      if ( DP_Print ) { printf("."); fflush(stdout); }
                   3208:    }
                   3209:  }
1.50      noro     3210:  get_eg(&eg4); add_eg(&eg_sbabuch,&eg3,&eg4);
1.30      noro     3211:  g = conv_ilist_s(nd_demand,0,indp);
1.26      noro     3212:  if ( DP_Print ) {
1.47      noro     3213:    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     3214:    printf("Nbase=%d,Nnfnz=%d,Nnfz=%d,Nnfsingular=%d\n",nd_psn,Nnfnz,Nnfz,Nnfs);
1.50      noro     3215:    fflush(stdout);
1.41      noro     3216:    if ( nd_sba_redundant_check )
                   3217:    printf("Nredundant=%d\n",Nredundant);
1.50      noro     3218:    fflush(stdout);
                   3219:    print_eg("sp",&eg_sp);
1.26      noro     3220:    print_eg("create",&eg_create);
                   3221:    print_eg("merge",&eg_merge);
1.47      noro     3222:    print_eg("minsig",&eg_minsig);
                   3223:    print_eg("smallest",&eg_smallest);
                   3224:    print_eg("remove",&eg_remove);
1.50      noro     3225:    printf("\n");
1.47      noro     3226:    print_eg("nf",&eg_nf);
                   3227:    print_eg("nfzero",&eg_nfzero);
1.50      noro     3228:    print_eg("removecont",&eg_removecont);
                   3229:    print_eg("updatepairs",&eg_updatepairs);
                   3230:    print_eg("hpdata",&eg_hpdata);
                   3231:    print_eg("total",&eg_sbabuch);
1.47      noro     3232:    printf("\n");
                   3233:  }
                   3234:  if ( nd_sba_syz ) {
1.27      noro     3235:    print_eg("remove",&eg_remove);
1.31      noro     3236:    print_eg("nf",&eg_nf);
                   3237:    print_eg("nfzero",&eg_nfzero);
1.27      noro     3238:    printf("\n");
1.26      noro     3239:  }
1.44      noro     3240:  if ( nd_sba_syz ) {
                   3241:    NODE hsyz,tsyz,prev;
                   3242:
                   3243:    hsyz = 0;
                   3244:    for ( i = 0; i < ngen; i++ ) {
                   3245:      tsyz = syzlist[i];
                   3246:      for ( prev = 0; tsyz != 0; prev = tsyz, tsyz = NEXT(tsyz))
                   3247:        BDY(tsyz) = (pointer)sigtodpm((SIG)BDY(tsyz));
                   3248:      if ( prev != 0 ) {
                   3249:        prev->next = hsyz; hsyz = syzlist[i];
                   3250:      }
                   3251:    }
                   3252:    *syzp = hsyz;
                   3253:  } else *syzp = 0;
1.24      noro     3254:  return g;
                   3255: }
                   3256:
1.1       noro     3257: /* splist = [[i1,i2],...] */
                   3258:
                   3259: int check_splist(int m,NODE splist)
                   3260: {
                   3261:   NODE t,p;
                   3262:   ND_pairs d,r,l;
                   3263:   int stat;
                   3264:   ND h,nf;
                   3265:
                   3266:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   3267:     p = BDY((LIST)BDY(t));
1.6       noro     3268:     NEXTND_pairs(d,r);
                   3269:     r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
                   3270:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     3271:     SG(r) = TD(LCM(r)); /* XXX */
                   3272:   }
                   3273:   if ( d ) NEXT(r) = 0;
                   3274:
1.6       noro     3275:   while ( d ) {
1.1       noro     3276: again:
1.6       noro     3277:     l = nd_minp(d,&d);
                   3278:     stat = nd_sp(m,0,l,&h);
                   3279:     if ( !stat ) {
                   3280:       NEXT(l) = d; d = l;
                   3281:       d = nd_reconstruct(0,d);
                   3282:       goto again;
                   3283:     }
1.39      noro     3284:     stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.6       noro     3285:     if ( !stat ) {
                   3286:       NEXT(l) = d; d = l;
                   3287:       d = nd_reconstruct(0,d);
                   3288:       goto again;
                   3289:     } else if ( nf ) return 0;
1.1       noro     3290:     if ( DP_Print) { printf("."); fflush(stdout); }
1.6       noro     3291:   }
1.1       noro     3292:   if ( DP_Print) { printf("done.\n"); fflush(stdout); }
                   3293:   return 1;
                   3294: }
                   3295:
                   3296: int check_splist_f4(int m,NODE splist)
                   3297: {
                   3298:   UINT *s0vect;
1.6       noro     3299:   PGeoBucket bucket;
1.1       noro     3300:   NODE p,rp0,t;
                   3301:   ND_pairs d,r,l,ll;
                   3302:   int col,stat;
                   3303:
                   3304:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   3305:     p = BDY((LIST)BDY(t));
1.6       noro     3306:     NEXTND_pairs(d,r);
                   3307:     r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
                   3308:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     3309:     SG(r) = TD(LCM(r)); /* XXX */
                   3310:   }
                   3311:   if ( d ) NEXT(r) = 0;
                   3312:
1.6       noro     3313:   while ( d ) {
                   3314:     l = nd_minsugarp(d,&d);
                   3315:     bucket = create_pbucket();
                   3316:     stat = nd_sp_f4(m,0,l,bucket);
                   3317:     if ( !stat ) {
                   3318:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   3319:       NEXT(ll) = d; d = l;
                   3320:       d = nd_reconstruct(0,d);
                   3321:       continue;
                   3322:     }
                   3323:     if ( bucket->m < 0 ) continue;
                   3324:     col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   3325:     if ( !col ) {
                   3326:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   3327:       NEXT(ll) = d; d = l;
                   3328:       d = nd_reconstruct(0,d);
                   3329:       continue;
1.1       noro     3330:     }
1.6       noro     3331:     if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
                   3332:   }
                   3333:   return 1;
1.1       noro     3334: }
                   3335:
                   3336: int do_diagonalize_trace(int sugar,int m)
                   3337: {
1.6       noro     3338:   int i,nh,stat;
                   3339:   NODE r,g,t;
                   3340:   ND h,nf,nfq,s,head;
                   3341:   NDV nfv,nfqv;
                   3342:   Q q,den,num;
                   3343:   union oNDC hc;
                   3344:   NODE node;
                   3345:   LIST l;
                   3346:   Z iq;
                   3347:   P cont,cont1;
1.1       noro     3348:
1.64    ! noro     3349:   if ( diag_period == 0 ) return 1;
1.6       noro     3350:   for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   3351:     if ( nd_gentrace ) {
                   3352:         /* Trace = [1,index,1,1] */
                   3353:         STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   3354:         MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   3355:     }
                   3356:     /* for nd_ps */
                   3357:     s = ndvtond(m,nd_ps[i]);
                   3358:     s = nd_separate_head(s,&head);
                   3359:     stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
                   3360:     if ( !stat ) return 0;
                   3361:     nf = nd_add(m,head,nf);
                   3362:     ndv_free(nd_ps[i]);
                   3363:     nd_ps[i] = ndtondv(m,nf);
                   3364:     nd_free(nf);
                   3365:
                   3366:     /* for nd_ps_trace */
                   3367:     if ( nd_demand )
                   3368:         nfv = ndv_load(i);
                   3369:     else
                   3370:         nfv = nd_ps_trace[i];
                   3371:     s = ndvtond(0,nfv);
                   3372:     s = nd_separate_head(s,&head);
                   3373:     stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
                   3374:     if ( !stat ) return 0;
                   3375:     ndv_free(nfv);
                   3376:     hc = HCU(nf); nd_removecont(0,nf);
                   3377:     /* exact division */
1.1       noro     3378:     cont = ndc_div(0,hc,HCU(nf));
1.6       noro     3379:     if ( nd_gentrace ) finalize_tracelist(i,cont);
                   3380:     nfv = ndtondv(0,nf);
                   3381:     nd_free(nf);
                   3382:     nd_bound[i] = ndv_compute_bound(nfv);
                   3383:     register_hcf(nfv);
                   3384:     if ( nd_demand ) {
                   3385:     ndv_save(nfv,i);
                   3386:     ndv_free(nfv);
                   3387:     } else
                   3388:     nd_ps_trace[i] = nfv;
                   3389:   }
                   3390:   return 1;
1.1       noro     3391: }
                   3392:
                   3393: static struct oEGT eg_invdalg;
                   3394: struct oEGT eg_le;
                   3395:
                   3396: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
                   3397: {
                   3398:     NODE tn;
                   3399:     P p1;
                   3400:
                   3401:     for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
                   3402:         substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
                   3403:     }
                   3404:     *r = p;
                   3405: }
                   3406:
                   3407: NODE nd_gb_trace(int m,int ishomo,int **indp)
                   3408: {
1.6       noro     3409:   int i,nh,sugar,stat;
                   3410:   NODE r,g,t;
                   3411:   ND_pairs d;
                   3412:   ND_pairs l;
                   3413:   ND h,nf,nfq,s,head;
                   3414:   NDV nfv,nfqv;
                   3415:   Z q,den,num;
                   3416:   P hc;
                   3417:   union oNDC dn,hnfq;
                   3418:   struct oEGT eg_monic,egm0,egm1;
                   3419:   int diag_count = 0;
                   3420:   P cont;
                   3421:   LIST list;
1.49      noro     3422:   struct oHPDATA current_hpdata,final_hpdata;
                   3423:   int final_hpvalue;
1.6       noro     3424:
                   3425:   init_eg(&eg_monic);
                   3426:   init_eg(&eg_invdalg);
                   3427:   init_eg(&eg_le);
                   3428:   g = 0; d = 0;
                   3429:   for ( i = 0; i < nd_psn; i++ ) {
                   3430:     d = update_pairs(d,g,i,0);
                   3431:     g = update_base(g,i);
                   3432:   }
                   3433:   sugar = 0;
1.49      noro     3434:   if ( nd_hpdata ) {
1.52      noro     3435:     if ( DP_Print ) fprintf(asir_out,"Hilbert driven algorithm.\n");
1.49      noro     3436:     setup_hpdata(&final_hpdata,&current_hpdata);
                   3437:   }
                   3438:
1.6       noro     3439:   while ( d ) {
1.1       noro     3440: again:
1.6       noro     3441:     l = nd_minp(d,&d);
                   3442:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   3443:     if ( SG(l) != sugar ) {
1.1       noro     3444: #if 1
1.6       noro     3445:       if ( ishomo ) {
                   3446:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3447:         stat = do_diagonalize_trace(sugar,m);
                   3448:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3449:         diag_count = 0;
1.1       noro     3450:         if ( !stat ) {
1.6       noro     3451:           NEXT(l) = d; d = l;
                   3452:           d = nd_reconstruct(1,d);
                   3453:           goto again;
1.1       noro     3454:         }
1.6       noro     3455:       }
                   3456: #endif
                   3457:       sugar = SG(l);
                   3458:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   3459:     }
                   3460:     stat = nd_sp(m,0,l,&h);
                   3461:     if ( !stat ) {
                   3462:       NEXT(l) = d; d = l;
                   3463:       d = nd_reconstruct(1,d);
                   3464:       goto again;
                   3465:     }
1.1       noro     3466: #if USE_GEOBUCKET
1.39      noro     3467:     stat = nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     3468: #else
1.39      noro     3469:     stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     3470: #endif
1.6       noro     3471:     if ( !stat ) {
                   3472:       NEXT(l) = d; d = l;
                   3473:       d = nd_reconstruct(1,d);
                   3474:       goto again;
                   3475:     } else if ( nf ) {
                   3476:       if ( nd_demand ) {
                   3477:         nfqv = ndv_load(nd_psn);
                   3478:         nfq = ndvtond(0,nfqv);
                   3479:       } else
                   3480:         nfq = 0;
                   3481:       if ( !nfq ) {
1.39      noro     3482:         if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!nd_top&&!Top,&nfq) ) {
1.6       noro     3483:           NEXT(l) = d; d = l;
                   3484:           d = nd_reconstruct(1,d);
                   3485:           goto again;
                   3486:         }
                   3487:       }
                   3488:       if ( nfq ) {
                   3489:         /* m|HC(nfq) => failure */
                   3490:         if ( nd_vc ) {
                   3491:           nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
                   3492:         } else
                   3493:           q = HCZ(nfq);
                   3494:         if ( !remqi((Q)q,m) ) return 0;
                   3495:
                   3496:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   3497:         hnfq = HCU(nfq);
                   3498:         if ( nd_nalg ) {
                   3499:           /* m|DN(HC(nf)^(-1)) => failure */
                   3500:           get_eg(&egm0);
                   3501:           if ( !nd_monic(m,&nfq) ) return 0;
                   3502:           get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
                   3503:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   3504:           nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
                   3505:         } else {
                   3506:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   3507:           nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
                   3508:         }
                   3509:         if ( nd_gentrace ) {
                   3510:           /* exact division */
                   3511:           cont = ndc_div(0,hnfq,HCU(nfqv));
                   3512:           if ( !UNIQ(cont) ) {
                   3513:             t = mknode(4,NULLP,NULLP,NULLP,cont);
                   3514:             MKLIST(list,t); MKNODE(t,list,nd_tracelist);
                   3515:             nd_tracelist = t;
                   3516:           }
                   3517:         }
1.24      noro     3518:         nh = ndv_newps(0,nfv,nfqv);
1.6       noro     3519:         if ( ishomo && ++diag_count == diag_period ) {
                   3520:           diag_count = 0;
                   3521:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3522:           stat = do_diagonalize_trace(sugar,m);
                   3523:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3524:           if ( !stat ) {
1.1       noro     3525:             NEXT(l) = d; d = l;
                   3526:             d = nd_reconstruct(1,d);
                   3527:             goto again;
1.6       noro     3528:           }
1.1       noro     3529:         }
1.6       noro     3530:         d = update_pairs(d,g,nh,0);
                   3531:         g = update_base(g,nh);
1.49      noro     3532:         if ( nd_hpdata ) {
1.57      noro     3533:           int dg,sugar0;
1.56      noro     3534:
                   3535:           update_hpdata(&current_hpdata,nh);
                   3536:           dg = comp_hn(final_hpdata.hn,current_hpdata.hn);
1.57      noro     3537:           if ( dg < 0 ) {
1.61      noro     3538:              int d_len;
                   3539:              for ( d_len = 0; d; d = d->next, d_len++);
                   3540:              fprintf(asir_out,"[%d] We found a gb\n",d_len);
1.57      noro     3541:           }
                   3542:           sugar0 = sugar;
                   3543:           while ( d && dg > sugar0 ) {
1.58      noro     3544:             d = nd_remove_same_sugar(d,sugar0);
1.57      noro     3545:             sugar0++;
1.49      noro     3546:           }
                   3547:         }
1.6       noro     3548:       } else {
1.57      noro     3549:         if ( DP_Print ) { fprintf(asir_out,"*"); fflush(asir_out); }
1.6       noro     3550:       }
                   3551:     } else {
1.57      noro     3552:       if ( DP_Print ) { fprintf(asir_out,"."); fflush(asir_out); }
1.1       noro     3553:     }
1.6       noro     3554:     FREENDP(l);
                   3555:   }
                   3556:   if ( nd_nalg ) {
                   3557:     if ( DP_Print ) {
                   3558:       print_eg("monic",&eg_monic);
                   3559:       print_eg("invdalg",&eg_invdalg);
                   3560:       print_eg("le",&eg_le);
1.1       noro     3561:     }
1.6       noro     3562:   }
1.1       noro     3563:   conv_ilist(nd_demand,1,g,indp);
1.60      noro     3564:   if ( DP_Print ) { fprintf(asir_out,"\nnd_gb_trace done. Nbase=%d\n",nd_psn); fflush(asir_out); }
1.6       noro     3565:   return g;
1.1       noro     3566: }
                   3567:
                   3568: int ndv_compare(NDV *p1,NDV *p2)
                   3569: {
                   3570:     return DL_COMPARE(HDL(*p1),HDL(*p2));
                   3571: }
                   3572:
                   3573: int ndv_compare_rev(NDV *p1,NDV *p2)
                   3574: {
                   3575:     return -DL_COMPARE(HDL(*p1),HDL(*p2));
                   3576: }
                   3577:
                   3578: int ndvi_compare(NDVI p1,NDVI p2)
                   3579: {
                   3580:     return DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3581: }
                   3582:
                   3583: int ndvi_compare_rev(NDVI p1,NDVI p2)
                   3584: {
                   3585:     return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3586: }
                   3587:
                   3588: NODE ndv_reduceall(int m,NODE f)
                   3589: {
                   3590:   int i,j,n,stat;
                   3591:   ND nf,g,head;
                   3592:   NODE t,a0,a;
                   3593:   union oNDC dn;
                   3594:   Q q,num,den;
                   3595:   NODE node;
                   3596:   LIST l;
                   3597:   Z iq,jq;
                   3598:   int *perm;
                   3599:   union oNDC hc;
                   3600:   P cont,cont1;
                   3601:
                   3602:   if ( nd_nora ) return f;
                   3603:   n = length(f);
1.24      noro     3604:   ndv_setup(m,0,f,0,1,0);
1.1       noro     3605:   perm = (int *)MALLOC(n*sizeof(int));
                   3606:   if ( nd_gentrace ) {
                   3607:     for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6       noro     3608:       perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1       noro     3609:   }
                   3610:   for ( i = 0; i < n; ) {
                   3611:     if ( nd_gentrace ) {
                   3612:       /* Trace = [1,index,1,1] */
1.6       noro     3613:       STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1       noro     3614:       MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   3615:     }
                   3616:     g = ndvtond(m,nd_ps[i]);
                   3617:     g = nd_separate_head(g,&head);
1.6       noro     3618:     stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1       noro     3619:     if ( !stat )
                   3620:       nd_reconstruct(0,0);
                   3621:     else {
                   3622:       if ( DP_Print ) { printf("."); fflush(stdout); }
                   3623:       ndv_free(nd_ps[i]);
                   3624:       hc = HCU(nf); nd_removecont(m,nf);
                   3625:       if ( nd_gentrace ) {
                   3626:         for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6       noro     3627:           jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
                   3628:           STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1       noro     3629:         }
1.6       noro     3630:         /* exact division */
1.1       noro     3631:         cont = ndc_div(m,hc,HCU(nf));
                   3632:         finalize_tracelist(perm[i],cont);
                   3633:       }
                   3634:       nd_ps[i] = ndtondv(m,nf); nd_free(nf);
                   3635:       nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   3636:       i++;
                   3637:     }
                   3638:   }
                   3639:   if ( DP_Print ) { printf("\n"); }
                   3640:   for ( a0 = 0, i = 0; i < n; i++ ) {
                   3641:     NEXTNODE(a0,a);
                   3642:     if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
                   3643:     else {
                   3644:       for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
                   3645:       BDY(a) = (pointer)nd_ps[j];
                   3646:     }
                   3647:   }
                   3648:   NEXT(a) = 0;
                   3649:   return a0;
                   3650: }
                   3651:
1.41      noro     3652: int ndplength(ND_pairs d)
                   3653: {
                   3654:   int i;
                   3655:   for ( i = 0; d; i++ ) d = NEXT(d);
                   3656:   return i;
                   3657: }
                   3658:
1.1       noro     3659: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
                   3660: {
                   3661:   ND_pairs d1,nd,cur,head,prev,remove;
1.41      noro     3662:   int len0;
1.1       noro     3663:
                   3664:   if ( !g ) return d;
                   3665:   /* for testing */
                   3666:   if ( gensyz && nd_gensyz == 2 ) {
                   3667:     d1 = nd_newpairs(g,t);
                   3668:     if ( !d )
                   3669:       return d1;
                   3670:     else {
                   3671:       nd = d;
                   3672:       while ( NEXT(nd) ) nd = NEXT(nd);
                   3673:       NEXT(nd) = d1;
                   3674:       return d;
                   3675:     }
                   3676:   }
                   3677:   d = crit_B(d,t);
                   3678:   d1 = nd_newpairs(g,t);
1.41      noro     3679:   len0 = ndplength(d1);
1.1       noro     3680:   d1 = crit_M(d1);
                   3681:   d1 = crit_F(d1);
1.41      noro     3682:   NcriMF += len0-ndplength(d1);
1.1       noro     3683:   if ( gensyz || do_weyl )
                   3684:     head = d1;
                   3685:   else {
                   3686:     prev = 0; cur = head = d1;
                   3687:     while ( cur ) {
                   3688:       if ( crit_2( cur->i1,cur->i2 ) ) {
                   3689:         remove = cur;
                   3690:         if ( !prev ) head = cur = NEXT(cur);
                   3691:         else cur = NEXT(prev) = NEXT(cur);
1.41      noro     3692:         FREENDP(remove); Ncri2++;
1.1       noro     3693:       } else {
                   3694:         prev = cur; cur = NEXT(cur);
                   3695:       }
                   3696:     }
                   3697:   }
                   3698:   if ( !d )
                   3699:     return head;
                   3700:   else {
                   3701:     nd = d;
                   3702:     while ( NEXT(nd) ) nd = NEXT(nd);
                   3703:     NEXT(nd) = head;
                   3704:     return d;
                   3705:   }
                   3706: }
                   3707:
1.24      noro     3708: ND_pairs merge_pairs_s(ND_pairs d,ND_pairs d1);
                   3709:
1.30      noro     3710: ND_pairs update_pairs_s( ND_pairs d, int t,NODE *syz)
1.24      noro     3711: {
                   3712:   ND_pairs d1;
1.26      noro     3713:   struct oEGT eg1,eg2,eg3;
1.24      noro     3714:
1.30      noro     3715:   if ( !t ) return d;
1.26      noro     3716: get_eg(&eg1);
1.30      noro     3717:   d1 = nd_newpairs_s(t,syz);
1.26      noro     3718: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
1.24      noro     3719:   d = merge_pairs_s(d,d1);
1.26      noro     3720: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
1.24      noro     3721:   return d;
                   3722: }
1.1       noro     3723:
1.47      noro     3724: int update_pairs_array_s( ND_pairs *d, int t,NODE *syz)
                   3725: {
                   3726:   ND_pairs *d1;
                   3727:   struct oEGT eg1,eg2,eg3;
                   3728:   int i;
                   3729:
                   3730:   if ( !t ) return 0;
                   3731: get_eg(&eg1);
                   3732:   Nnewpair = 0;
                   3733:   d1 = nd_newpairs_array_s(t,syz);
                   3734: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
                   3735:   for ( i = 0; i < nd_nbase; i++ )
                   3736:     d[i] = merge_pairs_s(d[i],d1[i]);
                   3737: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
                   3738:   return Nnewpair;
                   3739: }
                   3740:
1.1       noro     3741: ND_pairs nd_newpairs( NODE g, int t )
                   3742: {
                   3743:   NODE h;
                   3744:   UINT *dl;
                   3745:   int ts,s,i,t0,min,max;
                   3746:   ND_pairs r,r0;
                   3747:
                   3748:   dl = DL(nd_psh[t]);
                   3749:   ts = SG(nd_psh[t]) - TD(dl);
1.17      noro     3750:   if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1       noro     3751:   for ( r0 = 0, h = g; h; h = NEXT(h) ) {
                   3752:     if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
                   3753:       continue;
                   3754:     if ( nd_gbblock ) {
                   3755:       t0 = (long)BDY(h);
                   3756:       for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
                   3757:         min = nd_gbblock[i]; max = nd_gbblock[i+1];
                   3758:         if ( t0 >= min && t0 <= max && t >= min && t <= max )
                   3759:           break;
                   3760:       }
                   3761:       if ( nd_gbblock[i] >= 0 )
                   3762:         continue;
                   3763:     }
1.41      noro     3764:     NEXTND_pairs(r0,r); Npairs++;
1.1       noro     3765:     r->i1 = (long)BDY(h);
                   3766:     r->i2 = t;
                   3767:     ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
                   3768:     s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   3769:     SG(r) = MAX(s,ts) + TD(LCM(r));
                   3770:     /* experimental */
                   3771:     if ( nd_sugarweight )
                   3772:       r->sugar2 = ndl_weight2(r->lcm);
                   3773:   }
                   3774:   if ( r0 ) NEXT(r) = 0;
                   3775:   return r0;
                   3776: }
                   3777:
1.46      noro     3778: int sig_cmpdl_op(int n,DL d1,DL d2)
                   3779: {
                   3780:   int e1,e2,i,j,l;
                   3781:   int *t1,*t2;
                   3782:   int len,head;
                   3783:   struct order_pair *pair;
                   3784:
                   3785:   len = nd_sba_modord->block_length;
                   3786:   pair = nd_sba_modord->order_pair;
                   3787:
                   3788:   head = 0;
                   3789:   for ( i = 0, t1 = d1->d, t2 = d2->d; i < len; i++ ) {
                   3790:     l = pair[i].length;
                   3791:     switch ( pair[i].order ) {
                   3792:       case 0:
                   3793:         for ( j = 0, e1 = e2 = 0; j < l; j++ ) {
                   3794:           e1 += t1[j];
                   3795:           e2 += t2[j];
                   3796:         }
                   3797:         if ( e1 > e2 )
                   3798:           return 1;
                   3799:         else if ( e1 < e2 )
                   3800:           return -1;
                   3801:         else {
                   3802:           for ( j = l - 1; j >= 0 && t1[j] == t2[j]; j-- );
                   3803:           if ( j >= 0 )
                   3804:             return t1[j] < t2[j] ? 1 : -1;
                   3805:         }
                   3806:         break;
                   3807:       case 1:
                   3808:         for ( j = 0, e1 = e2 = 0; j < l; j++ ) {
                   3809:           e1 += t1[j];
                   3810:           e2 += t2[j];
                   3811:         }
                   3812:         if ( e1 > e2 )
                   3813:           return 1;
                   3814:         else if ( e1 < e2 )
                   3815:           return -1;
                   3816:         else {
                   3817:           for ( j = 0; j < l && t1[j] == t2[j]; j++ );
                   3818:           if ( j < l )
                   3819:             return t1[j] > t2[j] ? 1 : -1;
                   3820:         }
                   3821:         break;
                   3822:       case 2:
                   3823:         for ( j = 0; j < l && t1[j] == t2[j]; j++ );
                   3824:         if ( j < l )
                   3825:           return t1[j] > t2[j] ? 1 : -1;
                   3826:         break;
                   3827:       default:
                   3828:         error("sig_cmpdl_op : invalid order"); break;
                   3829:     }
                   3830:     t1 += l; t2 += l; head += l;
                   3831:   }
                   3832:   return 0;
                   3833: }
                   3834:
                   3835: int sig_cmpdl_mat(int n,DL d1,DL d2)
                   3836: {
                   3837:   int *v,*t1,*t2;
                   3838:   int s,i,j,len;
                   3839:   int **matrix;
                   3840:   static int *w;
                   3841:   static int nvar = 0;
                   3842:
                   3843:   if ( nvar != n ) {
                   3844:     nvar = n; w = (int *)MALLOC(n*sizeof(int));
                   3845:   }
                   3846:   for ( i = 0, t1 = d1->d, t2 = d2->d; i < n; i++ )
                   3847:     w[i] = t1[i]-t2[i];
                   3848:   len = nd_sba_modord->row;
                   3849:   matrix = nd_sba_modord->matrix;
                   3850:   for ( j = 0; j < len; j++ ) {
                   3851:     v = matrix[j];
                   3852:     for ( i = 0, s = 0; i < n; i++ )
                   3853:       s += v[i]*w[i];
                   3854:     if ( s > 0 )
                   3855:       return 1;
                   3856:     else if ( s < 0 )
                   3857:       return -1;
                   3858:   }
                   3859:   return 0;
                   3860: }
                   3861:
                   3862: struct comp_sig_spec *create_comp_sig_spec(VL current_vl,VL old_vl,Obj ord,Obj weight)
                   3863: {
                   3864:   struct comp_sig_spec *spec;
                   3865:   VL ovl,vl;
                   3866:   V ov;
                   3867:   int i,j,n,nvar,s;
                   3868:   NODE node,t,tn;
                   3869:   struct order_pair *l;
                   3870:   MAT m;
                   3871:   Obj **b;
                   3872:   int **w;
                   3873:   int *a;
                   3874:
                   3875:   spec = (struct comp_sig_spec *)MALLOC(sizeof(struct comp_sig_spec));
                   3876:   for ( i = 0, vl = current_vl; vl; vl = NEXT(vl), i++ );
                   3877:   spec->n = nvar = i;
                   3878:   if ( old_vl != 0 ) {
                   3879:     spec->oldv = (int *)MALLOC(nvar*sizeof(int));
                   3880:     for ( i = 0, ovl = old_vl; i < nvar; ovl = NEXT(ovl), i++ ) {
                   3881:       ov = ovl->v;
                   3882:       for ( j = 0, vl = current_vl; vl; vl = NEXT(vl), j++ )
                   3883:         if ( ov == vl->v ) break;
                   3884:       spec->oldv[i] = j;
                   3885:     }
                   3886:   } else
                   3887:     spec->oldv = 0;
                   3888:   if ( !ord || NUM(ord) ) {
                   3889:     switch ( ZTOS((Z)ord) ) {
                   3890:       case 0:
                   3891:         spec->cmpdl = cmpdl_revgradlex; break;
                   3892:       case 1:
                   3893:         spec->cmpdl = cmpdl_gradlex; break;
                   3894:       case 2:
                   3895:         spec->cmpdl = cmpdl_lex; break;
                   3896:       default:
                   3897:         error("create_comp_sig_spec : invalid spec"); break;
                   3898:     }
                   3899:   } else if ( OID(ord) == O_LIST ) {
                   3900:     node = BDY((LIST)ord);
                   3901:     for ( n = 0, t = node; t; t = NEXT(t), n++ );
                   3902:     l = (struct order_pair *)MALLOC_ATOMIC(n*sizeof(struct order_pair));
                   3903:     for ( i = 0, t = node, s = 0; i < n; t = NEXT(t), i++ ) {
                   3904:       tn = BDY((LIST)BDY(t)); l[i].order = ZTOS((Q)BDY(tn));
                   3905:       tn = NEXT(tn); l[i].length = ZTOS((Q)BDY(tn));
                   3906:       s += l[i].length;
                   3907:     }
                   3908:     if ( s != nvar )
                   3909:       error("create_comp_sig_spec : invalid spec");
                   3910:     spec->order_pair = l;
                   3911:     spec->block_length = n;
                   3912:     spec->cmpdl = sig_cmpdl_op;
                   3913:   } else if ( OID(ord) == O_MAT ) {
                   3914:     m = (MAT)ord; b = (Obj **)BDY(m);
                   3915:     if ( m->col != nvar )
                   3916:       error("create_comp_sig_spec : invalid spec");
                   3917:     w = almat(m->row,m->col);
                   3918:     for ( i = 0; i < m->row; i++ )
                   3919:       for ( j = 0; j < m->col; j++ )
                   3920:         w[i][j] = ZTOS((Q)b[i][j]);
                   3921:     spec->row = m->row;
                   3922:     spec->matrix = w;
                   3923:     spec->cmpdl = sig_cmpdl_mat;
                   3924:   } else
                   3925:     error("create_comp_sig_spec : invalid spec");
                   3926:   if ( weight != 0 ) {
                   3927:     node = BDY((LIST)weight);
                   3928:     a = (int *)MALLOC(nvar*sizeof(int));
                   3929:     for ( i = 0; i < nvar; i++, node = NEXT(node) )
                   3930:       a[i] = ZTOS((Z)BDY(node));
                   3931:     spec->weight = a;
                   3932:   }
                   3933:   return spec;
                   3934: }
                   3935:
                   3936: #define SIG_MUL_WEIGHT(a,i) (weight?(a)*weight[i]:(a))
                   3937:
                   3938: int comp_sig_monomial(int n,DL d1,DL d2)
                   3939: {
                   3940:   static DL m1,m2;
                   3941:   static int nvar = 0;
                   3942:   int *oldv,*weight;
                   3943:   int i,w1,w2;
                   3944:
                   3945:   if ( nvar != n ) {
                   3946:     nvar = n; NEWDL(m1,nvar); NEWDL(m2,nvar);
                   3947:   }
                   3948:   if ( !nd_sba_modord )
                   3949:     return (*cmpdl)(n,d1,d2);
1.55      noro     3950:   else if ( !nd_sba_modord->weight && !nd_sba_modord->oldv ) {
                   3951:     return (*nd_sba_modord->cmpdl)(n,d1,d2);
                   3952:   } else {
1.46      noro     3953:     weight = nd_sba_modord->weight;
                   3954:     oldv = nd_sba_modord->oldv;
                   3955:     if ( oldv ) {
                   3956:       for ( i = 0; i < n; i++ ) {
                   3957:         m1->d[i] = d1->d[oldv[i]]; m2->d[i] = d2->d[oldv[i]];
                   3958:       }
                   3959:     } else {
                   3960:       for ( i = 0; i < n; i++ ) {
                   3961:         m1->d[i] = d1->d[i]; m2->d[i] = d2->d[i];
                   3962:       }
                   3963:     }
                   3964:     for ( i = 0, w1 = w2 = 0; i < n; i++ ) {
                   3965:       w1 += SIG_MUL_WEIGHT(m1->d[i],i);
                   3966:       w2 += SIG_MUL_WEIGHT(m2->d[i],i);
                   3967:     }
                   3968:     m1->td = w1; m2->td = w2;
                   3969:     return (*nd_sba_modord->cmpdl)(n,m1,m2);
                   3970:   }
                   3971: }
                   3972:
1.24      noro     3973: int comp_sig(SIG s1,SIG s2)
                   3974: {
1.38      noro     3975:   if ( nd_sba_pot ) {
                   3976:     if ( s1->pos > s2->pos ) return 1;
                   3977:     else if ( s1->pos < s2->pos ) return -1;
1.55      noro     3978:     else return comp_sig_monomial(nd_nvar,DL(s1),DL(s2));
1.38      noro     3979:   } else {
                   3980:     static DL m1,m2;
1.44      noro     3981:     static int nvar = 0;
1.38      noro     3982:     int ret;
                   3983:
                   3984:     if ( nvar != nd_nvar ) {
                   3985:       nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
                   3986:     }
1.47      noro     3987:     if ( !nd_sba_modord )
1.55      noro     3988:       ret = (*cmpdl)(nd_nvar,DL2(s1),DL2(s2));
1.47      noro     3989:     else
1.55      noro     3990:       ret = comp_sig_monomial(nd_nvar,DL2(s1),DL2(s2));
1.38      noro     3991:     if ( ret != 0 ) return ret;
                   3992:     else if ( s1->pos > s2->pos ) return 1;
                   3993:     else if ( s1->pos < s2->pos ) return -1;
                   3994:     else return 0;
1.24      noro     3995:   }
                   3996: }
                   3997:
                   3998: int _create_spair_s(int i1,int i2,ND_pairs sp,SIG sig1,SIG sig2)
                   3999: {
                   4000:   int ret,s1,s2;
                   4001:   RHist p1,p2;
                   4002:   static int wpd;
                   4003:   static UINT *lcm;
                   4004:
                   4005:   sp->i1 = i1;
                   4006:   sp->i2 = i2;
                   4007:   p1 = nd_psh[i1];
                   4008:   p2 = nd_psh[i2];
                   4009:   ndl_lcm(DL(p1),DL(p2),sp->lcm);
1.49      noro     4010: #if 0
1.24      noro     4011:   s1 = SG(p1)-TD(DL(p1));
                   4012:   s2 = SG(p2)-TD(DL(p2));
                   4013:   SG(sp) = MAX(s1,s2) + TD(sp->lcm);
1.49      noro     4014: #endif
1.24      noro     4015:
1.26      noro     4016:   if ( wpd != nd_wpd ) {
1.24      noro     4017:     wpd = nd_wpd;
                   4018:     lcm = (UINT *)MALLOC(wpd*sizeof(UINT));
                   4019:   }
                   4020:   // DL(sig1) <- sp->lcm
                   4021:   // DL(sig1) -= DL(p1)
                   4022:   // DL(sig1) += DL(p1->sig)
1.27      noro     4023:   ndl_sub(sp->lcm,DL(p1),lcm);
1.24      noro     4024:   _ndltodl(lcm,DL(sig1));
                   4025:   _addtodl(nd_nvar,DL(p1->sig),DL(sig1));
                   4026:   sig1->pos = p1->sig->pos;
1.55      noro     4027:   _adddl(nd_nvar,DL(sig1),nd_sba_hm[sig1->pos],DL2(sig1));
1.24      noro     4028:
                   4029:   // DL(sig2) <- sp->lcm
                   4030:   // DL(sig2) -= DL(p2)
                   4031:   // DL(sig2) += DL(p2->sig)
1.27      noro     4032:   ndl_sub(sp->lcm,DL(p2),lcm);
1.24      noro     4033:   _ndltodl(lcm,DL(sig2));
                   4034:   _addtodl(nd_nvar,DL(p2->sig),DL(sig2));
                   4035:   sig2->pos = p2->sig->pos;
1.55      noro     4036:   _adddl(nd_nvar,DL(sig2),nd_sba_hm[sig2->pos],DL2(sig2));
1.24      noro     4037:
                   4038:   ret = comp_sig(sig1,sig2);
                   4039:   if ( ret == 0 ) return 0;
                   4040:   else if ( ret > 0 ) sp->sig = sig1;
                   4041:   else sp->sig = sig2;
1.49      noro     4042:
                   4043:   s1 = DL(sig1)->td+nd_sba_hm[p1->sig->pos]->td;
                   4044:   s2 = DL(sig2)->td+nd_sba_hm[p2->sig->pos]->td;
                   4045:   SG(sp) = MAX(s1,s2);
                   4046:
1.24      noro     4047:   return 1;
                   4048: }
                   4049:
                   4050: SIG dup_sig(SIG sig)
                   4051: {
                   4052:   SIG r;
                   4053:
                   4054:   if ( !sig ) return 0;
                   4055:   else {
                   4056:     NEWSIG(r);
                   4057:     _copydl(nd_nvar,DL(sig),DL(r));
1.55      noro     4058:     _copydl(nd_nvar,DL2(sig),DL2(r));
1.24      noro     4059:     r->pos = sig->pos;
                   4060:     return r;
                   4061:   }
                   4062: }
                   4063:
                   4064: void dup_ND_pairs(ND_pairs to,ND_pairs from)
                   4065: {
                   4066:   to->i1 = from->i1;
                   4067:   to->i2 = from->i2;
                   4068:   to->sugar = from->sugar;
                   4069:   to->sugar2 = from->sugar2;
                   4070:   ndl_copy(from->lcm,to->lcm);
                   4071:   to->sig = dup_sig(from->sig);
                   4072: }
                   4073:
                   4074: ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
                   4075: {
                   4076:   struct oND_pairs root;
                   4077:   ND_pairs q1,q2,r0,r;
                   4078:   int ret;
                   4079:
                   4080:   r = &root;
                   4081:   for ( q1 = p1, q2 = p2; q1 != 0 && q2 != 0; ) {
                   4082:     ret = comp_sig(q1->sig,q2->sig);
                   4083:     if ( ret < 0 ) {
                   4084:       r->next = q1; r = q1; q1 = q1->next;
                   4085:     } else if ( ret > 0 ) {
                   4086:       r->next = q2; r = q2; q2 = q2->next;
                   4087:     } else {
1.47      noro     4088:       Nnewpair--;
1.24      noro     4089:       ret = DL_COMPARE(q1->lcm,q2->lcm);
1.35      noro     4090:       Nsamesig++;
1.24      noro     4091:       if ( ret < 0 ) {
                   4092:         r->next = q1; r = q1; q1 = q1->next;
                   4093:         q2 = q2->next;
                   4094:       } else {
                   4095:         r->next = q2; r = q2; q2 = q2->next;
                   4096:         q1 = q1->next;
                   4097:       }
                   4098:     }
                   4099:   }
                   4100:   if ( q1 ) {
                   4101:     r->next = q1;
                   4102:   } else {
                   4103:     r->next = q2;
                   4104:   }
                   4105:   return root.next;
                   4106: }
                   4107:
                   4108: ND_pairs insert_pair_s(ND_pairs l,ND_pairs s)
                   4109: {
                   4110:   ND_pairs p,prev;
1.47      noro     4111:   int ret=1;
1.24      noro     4112:
                   4113:   for ( p = l, prev = 0; p != 0; prev = p, p = p->next ) {
                   4114:     if ( (ret = comp_sig(s->sig,p->sig)) <= 0 )
                   4115:       break;
                   4116:   }
                   4117:   if ( ret == 0 ) {
                   4118:     ret = DL_COMPARE(s->lcm,p->lcm);
                   4119:     if ( ret < 0 ) {
                   4120:       // replace p with s
                   4121:       s->next = p->next;
                   4122:       if ( prev == 0 ) {
                   4123:         return s;
                   4124:       } else {
                   4125:         prev->next = s;
                   4126:         return l;
                   4127:       }
                   4128:     } else
                   4129:       return l;
                   4130:   } else {
                   4131:     // insert s between prev and p
1.47      noro     4132:     Nnewpair++;
1.24      noro     4133:     s->next = p;
                   4134:     if ( prev == 0 ) {
                   4135:       return s;
                   4136:     } else {
                   4137:       prev->next = s;
                   4138:       return l;
                   4139:     }
                   4140:   }
                   4141: }
                   4142:
1.29      noro     4143: INLINE int __dl_redble(DL d1,DL d2,int nvar)
                   4144: {
                   4145:   int i;
                   4146:
                   4147:   if ( d1->td > d2->td )
                   4148:     return 0;
                   4149:   for ( i = nvar-1; i >= 0; i-- )
                   4150:     if ( d1->d[i] > d2->d[i] )
                   4151:       break;
                   4152:   if ( i >= 0 )
                   4153:     return 0;
                   4154:   else
                   4155:     return 1;
                   4156: }
                   4157:
1.30      noro     4158: ND_pairs nd_newpairs_s(int t, NODE *syz)
1.24      noro     4159: {
                   4160:   NODE h,s;
                   4161:   UINT *dl;
1.27      noro     4162:   int ts,ret,i;
1.24      noro     4163:   ND_pairs r,r0,_sp,sp;
1.29      noro     4164:   SIG spsig,tsig;
1.44      noro     4165:   static int nvar = 0;
1.29      noro     4166:   static SIG _sig1,_sig2;
1.26      noro     4167:   struct oEGT eg1,eg2,eg3,eg4;
1.24      noro     4168:
                   4169:   NEWND_pairs(_sp);
1.29      noro     4170:   if ( !_sig1 || nvar != nd_nvar ) {
                   4171:     nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
                   4172:   }
1.24      noro     4173:   r0 = 0;
1.27      noro     4174:   for ( i = 0; i < t; i++ ) {
                   4175:     ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
1.24      noro     4176:     if ( ret ) {
                   4177:       spsig = _sp->sig;
1.29      noro     4178:       for ( s = syz[spsig->pos]; s; s = s->next ) {
1.24      noro     4179:         tsig = (SIG)s->body;
1.29      noro     4180:         if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
1.24      noro     4181:           break;
                   4182:       }
                   4183:       if ( s == 0 ) {
                   4184:         NEWND_pairs(sp);
                   4185:         dup_ND_pairs(sp,_sp);
                   4186:         r0 = insert_pair_s(r0,sp);
1.26      noro     4187:       } else
                   4188:         Nsyz++;
1.24      noro     4189:     }
                   4190:   }
                   4191:   return r0;
                   4192: }
                   4193:
1.47      noro     4194: ND_pairs *nd_newpairs_array_s(int t, NODE *syz)
                   4195: {
                   4196:   NODE h,s;
                   4197:   UINT *dl;
                   4198:   int ts,ret,i;
                   4199:   ND_pairs r,r0,_sp,sp;
                   4200:   ND_pairs *d;
                   4201:   SIG spsig,tsig;
                   4202:   static int nvar = 0;
                   4203:   static SIG _sig1,_sig2;
                   4204:   struct oEGT eg1,eg2,eg3,eg4;
                   4205:
                   4206:   NEWND_pairs(_sp);
                   4207:   if ( !_sig1 || nvar != nd_nvar ) {
                   4208:     nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
                   4209:   }
                   4210:   d = (ND_pairs *)MALLOC(nd_nbase*sizeof(ND_pairs));
                   4211:   Nnewpair = 0;
                   4212:   for ( i = 0; i < t; i++ ) {
                   4213:     ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
                   4214:     if ( ret ) {
                   4215:       spsig = _sp->sig;
                   4216:       for ( s = syz[spsig->pos]; s; s = s->next ) {
                   4217:         tsig = (SIG)s->body;
                   4218:         if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
                   4219:           break;
                   4220:       }
                   4221:       if ( s == 0 ) {
                   4222:         NEWND_pairs(sp);
                   4223:         dup_ND_pairs(sp,_sp);
                   4224:         d[spsig->pos] = insert_pair_s(d[spsig->pos],sp);
                   4225:       } else
                   4226:         Nsyz++;
                   4227:     }
                   4228:   }
                   4229:   return d;
                   4230: }
                   4231:
1.1       noro     4232: /* ipair = [i1,i2],[i1,i2],... */
                   4233: ND_pairs nd_ipairtospair(NODE ipair)
                   4234: {
                   4235:   int s1,s2;
                   4236:   NODE tn,t;
                   4237:   ND_pairs r,r0;
                   4238:
                   4239:   for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
                   4240:     NEXTND_pairs(r0,r);
                   4241:     tn = BDY((LIST)BDY(t));
1.6       noro     4242:     r->i1 = ZTOS((Q)ARG0(tn));
                   4243:     r->i2 = ZTOS((Q)ARG1(tn));
1.1       noro     4244:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
                   4245:     s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   4246:     s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
                   4247:     SG(r) = MAX(s1,s2) + TD(LCM(r));
                   4248:     /* experimental */
                   4249:     if ( nd_sugarweight )
                   4250:         r->sugar2 = ndl_weight2(r->lcm);
                   4251:   }
                   4252:   if ( r0 ) NEXT(r) = 0;
                   4253:   return r0;
                   4254: }
                   4255:
                   4256: /* kokokara */
                   4257:
                   4258: ND_pairs crit_B( ND_pairs d, int s )
                   4259: {
                   4260:   ND_pairs cur,head,prev,remove;
                   4261:   UINT *t,*tl,*lcm;
                   4262:   int td,tdl;
                   4263:
                   4264:   if ( !d ) return 0;
                   4265:   t = DL(nd_psh[s]);
                   4266:   prev = 0;
                   4267:   head = cur = d;
                   4268:   lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   4269:   while ( cur ) {
                   4270:     tl = cur->lcm;
                   4271:     if ( ndl_reducible(tl,t) ) {
                   4272:       ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
                   4273:       if ( !ndl_equal(lcm,tl) ) {
                   4274:         ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
                   4275:         if (!ndl_equal(lcm,tl)) {
                   4276:           remove = cur;
                   4277:           if ( !prev ) {
                   4278:             head = cur = NEXT(cur);
                   4279:           } else {
                   4280:             cur = NEXT(prev) = NEXT(cur);
                   4281:           }
1.41      noro     4282:           FREENDP(remove); NcriB++;
1.1       noro     4283:         } else {
                   4284:           prev = cur; cur = NEXT(cur);
                   4285:         }
                   4286:       } else {
                   4287:         prev = cur; cur = NEXT(cur);
                   4288:       }
                   4289:     } else {
                   4290:       prev = cur; cur = NEXT(cur);
                   4291:     }
                   4292:   }
                   4293:   return head;
                   4294: }
                   4295:
                   4296: ND_pairs crit_M( ND_pairs d1 )
                   4297: {
                   4298:     ND_pairs e,d2,d3,dd,p;
                   4299:     UINT *id,*jd;
                   4300:
                   4301:     if ( !d1 ) return d1;
                   4302:     for ( dd = 0, e = d1; e; e = d3 ) {
                   4303:         if ( !(d2 = NEXT(e)) ) {
                   4304:             NEXT(e) = dd;
                   4305:             return e;
                   4306:         }
                   4307:         id = LCM(e);
                   4308:         for ( d3 = 0; d2; d2 = p ) {
                   4309:             p = NEXT(d2);
                   4310:             jd = LCM(d2);
                   4311:             if ( ndl_equal(jd,id) )
                   4312:                 ;
                   4313:             else if ( TD(jd) > TD(id) )
                   4314:                 if ( ndl_reducible(jd,id) ) continue;
                   4315:                 else ;
                   4316:             else if ( ndl_reducible(id,jd) ) goto delit;
                   4317:             NEXT(d2) = d3;
                   4318:             d3 = d2;
                   4319:         }
                   4320:         NEXT(e) = dd;
                   4321:         dd = e;
                   4322:         continue;
                   4323:         /**/
                   4324:     delit:    NEXT(d2) = d3;
                   4325:         d3 = d2;
                   4326:         for ( ; p; p = d2 ) {
                   4327:             d2 = NEXT(p);
                   4328:             NEXT(p) = d3;
                   4329:             d3 = p;
                   4330:         }
                   4331:         FREENDP(e);
                   4332:     }
                   4333:     return dd;
                   4334: }
                   4335:
                   4336: ND_pairs crit_F( ND_pairs d1 )
                   4337: {
                   4338:     ND_pairs rest, head,remove;
                   4339:     ND_pairs last, p, r, w;
                   4340:     int s;
                   4341:
                   4342:     if ( !d1 ) return d1;
                   4343:     for ( head = last = 0, p = d1; NEXT(p); ) {
                   4344:         r = w = equivalent_pairs(p,&rest);
                   4345:         s = SG(r);
                   4346:         w = NEXT(w);
                   4347:         while ( w ) {
                   4348:             if ( crit_2(w->i1,w->i2) ) {
                   4349:                 r = w;
                   4350:                 w = NEXT(w);
                   4351:                 while ( w ) {
                   4352:                     remove = w;
                   4353:                     w = NEXT(w);
                   4354:                     FREENDP(remove);
                   4355:                 }
                   4356:                 break;
                   4357:             } else if ( SG(w) < s ) {
                   4358:                 FREENDP(r);
                   4359:                 r = w;
                   4360:                 s = SG(r);
                   4361:                 w = NEXT(w);
                   4362:             } else {
                   4363:                 remove = w;
                   4364:                 w = NEXT(w);
                   4365:                 FREENDP(remove);
                   4366:             }
                   4367:         }
                   4368:         if ( last ) NEXT(last) = r;
                   4369:         else head = r;
                   4370:         NEXT(last = r) = 0;
                   4371:         p = rest;
                   4372:         if ( !p ) return head;
                   4373:     }
                   4374:     if ( !last ) return p;
                   4375:     NEXT(last) = p;
                   4376:     return head;
                   4377: }
                   4378:
                   4379: int crit_2( int dp1, int dp2 )
                   4380: {
                   4381:     return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
                   4382: }
                   4383:
                   4384: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
                   4385: {
                   4386:     ND_pairs w,p,r,s;
                   4387:     UINT *d;
                   4388:
                   4389:     w = d1;
                   4390:     d = LCM(w);
                   4391:     s = NEXT(w);
                   4392:     NEXT(w) = 0;
                   4393:     for ( r = 0; s; s = p ) {
                   4394:         p = NEXT(s);
                   4395:         if ( ndl_equal(d,LCM(s)) ) {
                   4396:             NEXT(s) = w; w = s;
                   4397:         } else {
                   4398:             NEXT(s) = r; r = s;
                   4399:         }
                   4400:     }
                   4401:     *prest = r;
                   4402:     return w;
                   4403: }
                   4404:
                   4405: NODE update_base(NODE nd,int ndp)
                   4406: {
                   4407:     UINT *dl, *dln;
                   4408:     NODE last, p, head;
                   4409:
                   4410:     dl = DL(nd_psh[ndp]);
                   4411:     for ( head = last = 0, p = nd; p; ) {
                   4412:         dln = DL(nd_psh[(long)BDY(p)]);
                   4413:         if ( ndl_reducible( dln, dl ) ) {
                   4414:             p = NEXT(p);
                   4415:             if ( last ) NEXT(last) = p;
                   4416:         } else {
                   4417:             if ( !last ) head = p;
                   4418:             p = NEXT(last = p);
                   4419:         }
                   4420:     }
                   4421:     head = append_one(head,ndp);
                   4422:     return head;
                   4423: }
                   4424:
                   4425: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   4426: {
                   4427:     ND_pairs m,ml,p,l;
                   4428:     UINT *lcm;
                   4429:     int s,td,len,tlen,c,c1;
                   4430:
                   4431:     if ( !(p = NEXT(m = d)) ) {
                   4432:         *prest = p;
                   4433:         NEXT(m) = 0;
                   4434:         return m;
                   4435:     }
                   4436:     if ( !NoSugar ) {
                   4437:         if ( nd_sugarweight ) {
                   4438:             s = m->sugar2;
                   4439:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   4440:                 if ( (p->sugar2 < s)
                   4441:                     || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   4442:                     ml = l; m = p; s = m->sugar2;
                   4443:                 }
                   4444:         } else {
                   4445:             s = SG(m);
                   4446:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   4447:                 if ( (SG(p) < s)
                   4448:                     || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   4449:                     ml = l; m = p; s = SG(m);
                   4450:                 }
                   4451:         }
                   4452:     } else {
                   4453:         for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   4454:             if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
                   4455:                 ml = l; m = p; s = SG(m);
                   4456:             }
                   4457:     }
                   4458:     if ( !ml ) *prest = NEXT(m);
                   4459:     else {
                   4460:         NEXT(ml) = NEXT(m);
                   4461:         *prest = d;
                   4462:     }
                   4463:     NEXT(m) = 0;
                   4464:     return m;
                   4465: }
                   4466:
                   4467: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   4468: {
                   4469:     int msugar,i;
                   4470:     ND_pairs t,dm0,dm,dr0,dr;
                   4471:
                   4472:     if ( nd_sugarweight ) {
                   4473:         for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
                   4474:             if ( t->sugar2 < msugar ) msugar = t->sugar2;
                   4475:         dm0 = 0; dr0 = 0;
                   4476:         for ( i = 0, t = d; t; t = NEXT(t) )
                   4477:             if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
                   4478:                 if ( dm0 ) NEXT(dm) = t;
                   4479:                 else dm0 = t;
                   4480:                 dm = t;
                   4481:                 i++;
                   4482:             } else {
                   4483:                 if ( dr0 ) NEXT(dr) = t;
                   4484:                 else dr0 = t;
                   4485:                 dr = t;
                   4486:             }
                   4487:     } else {
                   4488:         for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   4489:             if ( SG(t) < msugar ) msugar = SG(t);
                   4490:         dm0 = 0; dr0 = 0;
                   4491:         for ( i = 0, t = d; t; t = NEXT(t) )
                   4492:             if ( i < nd_f4_nsp && SG(t) == msugar ) {
                   4493:                 if ( dm0 ) NEXT(dm) = t;
                   4494:                 else dm0 = t;
                   4495:                 dm = t;
                   4496:                 i++;
                   4497:             } else {
                   4498:                 if ( dr0 ) NEXT(dr) = t;
                   4499:                 else dr0 = t;
                   4500:                 dr = t;
                   4501:             }
                   4502:     }
                   4503:     NEXT(dm) = 0;
                   4504:     if ( dr0 ) NEXT(dr) = 0;
                   4505:     *prest = dr0;
                   4506:     return dm0;
                   4507: }
                   4508:
1.28      noro     4509: ND_pairs nd_minsugarp_s( ND_pairs d, ND_pairs *prest )
                   4510: {
                   4511:   int msugar;
                   4512:   ND_pairs t,last;
                   4513:
                   4514:   for ( msugar = SG(d), t = d; t; t = NEXT(t) )
                   4515:     if ( SG(t) == msugar ) last = t;
                   4516:   *prest = last->next;
                   4517:   last->next = 0;
                   4518:   return d;
                   4519: }
                   4520:
1.1       noro     4521: int nd_tdeg(NDV c)
                   4522: {
                   4523:   int wmax = 0;
                   4524:   int i,len;
                   4525:   NMV a;
                   4526:
                   4527:   len = LEN(c);
                   4528:   for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   4529:     wmax = MAX(TD(DL(a)),wmax);
                   4530:   return wmax;
                   4531: }
                   4532:
1.24      noro     4533: int ndv_newps(int m,NDV a,NDV aq)
1.1       noro     4534: {
                   4535:     int len;
                   4536:     RHist r;
                   4537:     NDV b;
                   4538:     NODE tn;
                   4539:     LIST l;
                   4540:     Z iq;
                   4541:
                   4542:     if ( nd_psn == nd_pslen ) {
                   4543:         nd_pslen *= 2;
                   4544:         nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
                   4545:         nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
                   4546:         nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
                   4547:         nd_bound = (UINT **)
                   4548:             REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
                   4549:         nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
                   4550:         nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
                   4551:     }
                   4552:     NEWRHist(r); nd_psh[nd_psn] = r;
                   4553:     nd_ps[nd_psn] = a;
                   4554:     if ( aq ) {
                   4555:         nd_ps_trace[nd_psn] = aq;
                   4556:         if ( !m ) {
                   4557:           register_hcf(aq);
                   4558:         } else if ( m == -2 ) {
                   4559:           /* do nothing */
                   4560:         } else
                   4561:           error("ndv_newps : invalud modulus");
                   4562:         nd_bound[nd_psn] = ndv_compute_bound(aq);
                   4563: #if 1
                   4564:         SG(r) = SG(aq);
                   4565: #else
                   4566:         SG(r) = nd_tdeg(aq);
                   4567: #endif
                   4568:         ndl_copy(HDL(aq),DL(r));
1.24      noro     4569:         r->sig = dup_sig(aq->sig);
1.1       noro     4570:     } else {
                   4571:         if ( !m ) register_hcf(a);
                   4572:         nd_bound[nd_psn] = ndv_compute_bound(a);
                   4573: #if 1
                   4574:         SG(r) = SG(a);
                   4575: #else
                   4576:         SG(r) = nd_tdeg(a);
                   4577: #endif
                   4578:         ndl_copy(HDL(a),DL(r));
1.24      noro     4579:         r->sig = dup_sig(a->sig);
1.1       noro     4580:     }
                   4581:     if ( nd_demand ) {
                   4582:         if ( aq ) {
                   4583:             ndv_save(nd_ps_trace[nd_psn],nd_psn);
                   4584:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   4585:             nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   4586:             nd_ps_trace[nd_psn] = 0;
                   4587:         } else {
                   4588:             ndv_save(nd_ps[nd_psn],nd_psn);
                   4589:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
                   4590:             nd_ps[nd_psn] = 0;
                   4591:         }
                   4592:     }
                   4593:     if ( nd_gentrace ) {
                   4594:         /* reverse the tracelist and append it to alltracelist */
                   4595:         nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6       noro     4596:         STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1       noro     4597:         MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
                   4598:     }
                   4599:     return nd_psn++;
                   4600: }
                   4601:
1.46      noro     4602: // find LM wrt the specified modord
                   4603: void ndv_lm_modord(NDV p,DL d)
1.45      noro     4604: {
                   4605:   NMV m;
                   4606:   DL tmp;
                   4607:   int len,i,ret;
                   4608:
                   4609:   NEWDL(tmp,nd_nvar);
                   4610:   m = BDY(p); len = LEN(p);
1.46      noro     4611:   _ndltodl(DL(m),d); // printdl(d); printf("->");
1.45      noro     4612:   for ( i = 1, NMV_ADV(m); i < len; i++, NMV_ADV(m) ) {
                   4613:     _ndltodl(DL(m),tmp);
1.46      noro     4614:     ret = comp_sig_monomial(nd_nvar,tmp,d);
1.45      noro     4615:     if ( ret > 0 ) _copydl(nd_nvar,tmp,d);
                   4616:   }
1.46      noro     4617: //   printdl(d); printf("\n");
1.45      noro     4618: }
                   4619:
1.1       noro     4620: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
                   4621: /* return 1 if success, 0 if failure (HC(a mod p)) */
                   4622:
1.24      noro     4623: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont,int sba)
1.1       noro     4624: {
1.6       noro     4625:   int i,j,td,len,max;
                   4626:   NODE s,s0,f0,tn;
                   4627:   UINT *d;
                   4628:   RHist r;
                   4629:   NDVI w;
                   4630:   NDV a,am;
                   4631:   union oNDC hc;
                   4632:   NODE node;
                   4633:   P hcp;
                   4634:   Z iq,jq;
                   4635:   LIST l;
                   4636:
                   4637:   nd_found = 0; nd_notfirst = 0; nd_create = 0;
                   4638:   /* initialize the tracelist */
                   4639:   nd_tracelist = 0;
                   4640:
                   4641:   for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
                   4642:   w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
                   4643:   for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
                   4644:     if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
                   4645:   if ( !dont_sort ) {
                   4646:     /* XXX heuristic */
1.26      noro     4647:     if ( !sba && !nd_ord->id && (nd_ord->ord.simple<2) )
1.6       noro     4648:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   4649:         (int (*)(const void *,const void *))ndvi_compare_rev);
                   4650:     else
                   4651:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   4652:         (int (*)(const void *,const void *))ndvi_compare);
                   4653:   }
                   4654:   nd_pslen = 2*nd_psn;
                   4655:   nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4656:   nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4657:   nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4658:   nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   4659:   nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
                   4660:   nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
                   4661:   nd_hcf = 0;
1.1       noro     4662:
1.6       noro     4663:   if ( trace && nd_vc )
                   4664:     makesubst(nd_vc,&nd_subst);
                   4665:   else
                   4666:     nd_subst = 0;
1.1       noro     4667:
1.6       noro     4668:   if ( !nd_red )
                   4669:     nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   4670:   for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
                   4671:   for ( i = 0; i < nd_psn; i++ ) {
                   4672:     hc = HCU(w[i].p);
                   4673:     if ( trace ) {
                   4674:       if ( mod == -2 ) {
                   4675:         /* over a large finite field */
                   4676:         /* trace = small modulus */
                   4677:         a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
                   4678:         ndv_mod(-2,a);
                   4679:         if ( !dont_removecont) ndv_removecont(-2,a);
                   4680:         am = nd_ps[i] = ndv_dup(trace,w[i].p);
                   4681:         ndv_mod(trace,am);
                   4682:       if ( DL_COMPARE(HDL(am),HDL(a)) )
                   4683:         return 0;
                   4684:         ndv_removecont(trace,am);
                   4685:       } else {
                   4686:         a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
                   4687:         if ( !dont_removecont) ndv_removecont(0,a);
                   4688:         register_hcf(a);
                   4689:         am = nd_ps[i] = ndv_dup(mod,a);
                   4690:         ndv_mod(mod,am);
                   4691:       if ( DL_COMPARE(HDL(am),HDL(a)) )
                   4692:         return 0;
                   4693:         ndv_removecont(mod,am);
                   4694:       }
                   4695:     } else {
                   4696:       a = nd_ps[i] = ndv_dup(mod,w[i].p);
                   4697:       if ( mod || !dont_removecont ) ndv_removecont(mod,a);
                   4698:       if ( !mod ) register_hcf(a);
1.1       noro     4699:     }
1.6       noro     4700:     if ( nd_gentrace ) {
                   4701:       STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
                   4702:       /* exact division */
1.1       noro     4703:       if ( !dont_removecont )
1.6       noro     4704:         ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
                   4705:       MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
                   4706:     }
                   4707:     NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
                   4708:     nd_bound[i] = ndv_compute_bound(a);
                   4709:     nd_psh[i] = r;
                   4710:     if ( nd_demand ) {
                   4711:       if ( trace ) {
                   4712:         ndv_save(nd_ps_trace[i],i);
                   4713:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   4714:         nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   4715:         nd_ps_trace[i] = 0;
                   4716:       } else {
                   4717:         ndv_save(nd_ps[i],i);
                   4718:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
                   4719:         nd_ps[i] = 0;
                   4720:       }
1.1       noro     4721:     }
1.6       noro     4722:   }
1.24      noro     4723:   if ( sba ) {
1.27      noro     4724:     nd_sba_hm = (DL *)MALLOC(nd_psn*sizeof(DL));
1.24      noro     4725:    // setup signatures
1.27      noro     4726:     for ( i = 0; i < nd_psn; i++ ) {
                   4727:       SIG sig;
1.24      noro     4728:
1.27      noro     4729:       NEWSIG(sig); sig->pos = i;
                   4730:       nd_ps[i]->sig = sig;
                   4731:       if ( nd_demand ) nd_ps_sym[i]->sig = sig;
                   4732:         nd_psh[i]->sig = sig;
                   4733:       if ( trace ) {
                   4734:         nd_ps_trace[i]->sig = sig;
                   4735:         if ( nd_demand ) nd_ps_trace_sym[i]->sig = sig;
                   4736:       }
                   4737:       NEWDL(nd_sba_hm[i],nd_nvar);
1.46      noro     4738:       if ( nd_sba_modord )
                   4739:         ndv_lm_modord(nd_ps[i],nd_sba_hm[i]);
1.45      noro     4740:       else
                   4741:         _ndltodl(DL(nd_psh[i]),nd_sba_hm[i]);
1.55      noro     4742:       _adddl(nd_nvar,DL(sig),nd_sba_hm[i],DL2(sig));
1.27      noro     4743:     }
1.29      noro     4744:     nd_sba_pos = (NODE *)MALLOC(nd_psn*sizeof(NODE));
                   4745:     for ( i = 0; i < nd_psn; i++ ) {
                   4746:       j = nd_psh[i]->sig->pos;
                   4747:       nd_sba_pos[j] = append_one(nd_sba_pos[j],i);
                   4748:     }
1.24      noro     4749:   }
1.6       noro     4750:   if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
                   4751:   return 1;
1.1       noro     4752: }
                   4753:
                   4754: struct order_spec *append_block(struct order_spec *spec,
                   4755:     int nv,int nalg,int ord);
                   4756:
                   4757: extern VECT current_dl_weight_vector_obj;
                   4758: static VECT prev_weight_vector_obj;
                   4759:
                   4760: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
                   4761:     struct order_spec **ord1p,LIST *f1p,NODE *alistp)
                   4762: {
                   4763:     NODE alist,t,s,r0,r,arg;
                   4764:     VL tv;
                   4765:     P poly;
                   4766:     DP d;
                   4767:     Alg alpha,dp;
                   4768:     DAlg inv,da,hc;
                   4769:     MP m;
                   4770:     int i,nvar,nalg,n;
                   4771:     NumberField nf;
                   4772:     LIST f1,f2;
                   4773:     struct order_spec *current_spec;
                   4774:     VECT obj,obj0;
                   4775:     VECT tmp;
                   4776:
                   4777:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
                   4778:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
                   4779:
                   4780:     for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
                   4781:         NEXTNODE(alist,t); MKV(tv->v,poly);
                   4782:         MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
                   4783:         tv->v = tv->v->priv;
                   4784:     }
                   4785:     NEXT(t) = 0;
                   4786:
                   4787:     /* simplification, making polynomials monic */
                   4788:     setfield_dalg(alist);
                   4789:     obj_algtodalg((Obj)f,(Obj *)&f1);
                   4790:     for ( t = BDY(f); t; t = NEXT(t) ) {
                   4791:         initd(ord); ptod(vv,vv,(P)BDY(t),&d);
                   4792:         hc = (DAlg)BDY(d)->c;
                   4793:         if ( NID(hc) == N_DA ) {
                   4794:             invdalg(hc,&inv);
                   4795:             for ( m = BDY(d); m; m = NEXT(m) ) {
                   4796:                 muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
                   4797:             }
                   4798:         }
                   4799:         initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
                   4800:     }
                   4801:     obj_dalgtoalg((Obj)f1,(Obj *)&f);
                   4802:
                   4803:     /* append alg vars to the var list */
                   4804:     for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
                   4805:     NEXT(tv) = av;
                   4806:
                   4807:     /* append a block to ord */
                   4808:     *ord1p = append_block(ord,nvar,nalg,2);
                   4809:
                   4810:     /* create generator list */
                   4811:     nf = get_numberfield();
                   4812:     for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
                   4813:         MKAlg(nf->defpoly[i],dp);
                   4814:         MKNODE(s,dp,t); t = s;
                   4815:     }
                   4816:     MKLIST(f1,t);
                   4817:     *alistp = alist;
                   4818:     algobjtorat((Obj)f1,(Obj *)f1p);
                   4819:
                   4820:     /* creating a new weight vector */
                   4821:     prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
                   4822:     n = nvar+nalg+1;
                   4823:     MKVECT(obj,n);
                   4824:     if ( obj0 && obj0->len == nvar )
                   4825:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
                   4826:     else
                   4827:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
                   4828:     for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
                   4829:     BDY(obj)[n-1] = (pointer)ONE;
                   4830:     arg = mknode(1,obj);
                   4831:     Pdp_set_weight(arg,&tmp);
                   4832: }
                   4833:
                   4834: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
                   4835: {
                   4836:     NODE s,t,u0,u;
                   4837:     P p;
                   4838:     VL tv;
                   4839:     Obj obj;
                   4840:     VECT tmp;
                   4841:     NODE arg;
                   4842:
                   4843:     u0 = 0;
                   4844:     for ( t = r; t; t = NEXT(t) ) {
                   4845:         p = (P)BDY(t);
                   4846:         for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
                   4847:             substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
                   4848:         }
                   4849:         if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
                   4850:             NEXTNODE(u0,u);
                   4851:             BDY(u) = (pointer)p;
                   4852:         }
                   4853:     }
                   4854:     arg = mknode(1,prev_weight_vector_obj);
                   4855:     Pdp_set_weight(arg,&tmp);
                   4856:
                   4857:     return u0;
                   4858: }
                   4859:
                   4860: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
                   4861: {
                   4862:     VL tv,fv,vv,vc,av;
                   4863:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4864:     int e,max,nvar,i;
                   4865:     NDV b;
                   4866:     int ishomo,nalg,mrank,trank,wmax,len;
                   4867:   NMV a;
                   4868:     Alg alpha,dp;
                   4869:     P p,zp;
                   4870:     Q dmy;
                   4871:     LIST f1,f2,zpl;
                   4872:     Obj obj;
                   4873:     NumberField nf;
                   4874:     struct order_spec *ord1;
                   4875:     NODE tr,tl1,tl2,tl3,tl4,nzlist;
                   4876:     LIST l1,l2,l3,l4,l5;
                   4877:   int j;
                   4878:   Z jq,bpe,last_nonzero;
                   4879:     int *perm;
                   4880:     EPOS oepos;
                   4881:     int obpe,oadv,ompos,cbpe;
1.15      noro     4882:     VECT hvect;
1.1       noro     4883:
1.41      noro     4884:     NcriB = NcriMF = Ncri2 = 0;
1.1       noro     4885:     nd_module = 0;
                   4886:     if ( !m && Demand ) nd_demand = 1;
                   4887:     else nd_demand = 0;
                   4888:
                   4889:     if ( DP_Multiple )
                   4890:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4891: #if 0
                   4892:     ndv_alloc = 0;
                   4893: #endif
                   4894:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     4895:     parse_nd_option(vv,current_option);
1.1       noro     4896:     if ( m && nd_vc )
                   4897:        error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
                   4898:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4899:     switch ( ord->id ) {
                   4900:         case 1:
                   4901:             if ( ord->nv != nvar )
                   4902:                 error("nd_{gr,f4} : invalid order specification");
                   4903:             break;
                   4904:         default:
                   4905:             break;
                   4906:     }
                   4907:     nd_nalg = 0;
                   4908:     av = 0;
                   4909:     if ( !m ) {
                   4910:         get_algtree((Obj)f,&av);
                   4911:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4912:         nd_ntrans = nvar;
                   4913:         nd_nalg = nalg;
                   4914:         /* #i -> t#i */
                   4915:         if ( nalg ) {
                   4916:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4917:             ord = ord1;
                   4918:             f = f1;
                   4919:         }
                   4920:         nvar += nalg;
                   4921:     }
                   4922:     nd_init_ord(ord);
                   4923:     mrank = 0;
                   4924:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4925:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4926:             if ( nd_module ) {
1.16      noro     4927:                 if ( OID(BDY(t)) == O_DPM ) {
                   4928:                   e = dpm_getdeg((DPM)BDY(t),&trank);
                   4929:                   max = MAX(e,max);
                   4930:                   mrank = MAX(mrank,trank);
                   4931:                 } else {
                   4932:                   s = BDY((LIST)BDY(t));
                   4933:                   trank = length(s);
                   4934:                   mrank = MAX(mrank,trank);
                   4935:                   for ( ; s; s = NEXT(s) ) {
                   4936:                       e = getdeg(tv->v,(P)BDY(s));
                   4937:                       max = MAX(e,max);
                   4938:                   }
1.1       noro     4939:                 }
                   4940:             } else {
1.43      noro     4941:                 if ( OID(BDY(t)) == O_DP ) {
                   4942:                   e = dp_getdeg((DP)BDY(t));
                   4943:                   max = MAX(e,max);
                   4944:                 } else {
                   4945:                   e = getdeg(tv->v,(P)BDY(t));
                   4946:                   max = MAX(e,max);
                   4947:                 }
1.1       noro     4948:             }
                   4949:         }
                   4950:     nd_setup_parameters(nvar,nd_nzlist?0:max);
                   4951:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   4952:     ishomo = 1;
                   4953:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4954:       if ( nd_module ) {
1.16      noro     4955:         if ( OID(BDY(t)) == O_DPM ) {
                   4956:           Z cont;
                   4957:           DPM zdpm;
                   4958:
                   4959:           if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
                   4960:           else zdpm = (DPM)BDY(t);
                   4961:           b = (pointer)dpmtondv(m,zdpm);
                   4962:         } else {
                   4963:           if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   4964:           else zpl = (LIST)BDY(t);
1.1       noro     4965:           b = (pointer)pltondv(CO,vv,zpl);
1.16      noro     4966:         }
1.1       noro     4967:       } else {
1.43      noro     4968:         if ( OID(BDY(t)) == O_DP ) {
                   4969:           DP zdp;
                   4970:
                   4971:           if ( !m && !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
                   4972:           else zdp = (DP)BDY(t);
                   4973:           b = (pointer)dptondv(m,zdp);
                   4974:         } else {
                   4975:           if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   4976:           else zp = (P)BDY(t);
                   4977:           b = (pointer)ptondv(CO,vv,zp);
                   4978:         }
1.1       noro     4979:       }
                   4980:       if ( ishomo )
                   4981:         ishomo = ishomo && ndv_ishomo(b);
                   4982:       if ( m ) ndv_mod(m,b);
                   4983:       if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4984:     }
                   4985:     if ( fd0 ) NEXT(fd) = 0;
                   4986:
                   4987:   if ( !ishomo && homo ) {
                   4988:         for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   4989:             b = (NDV)BDY(t); len = LEN(b);
                   4990:             for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   4991:                 wmax = MAX(TD(DL(a)),wmax);
                   4992:         }
                   4993:         homogenize_order(ord,nvar,&ord1);
                   4994:         nd_init_ord(ord1);
                   4995:         nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   4996:         for ( t = fd0; t; t = NEXT(t) )
                   4997:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   4998:     }
                   4999:
1.24      noro     5000:     ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0,0);
1.1       noro     5001:     if ( nd_gentrace ) {
                   5002:         MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   5003:     }
                   5004:   if ( nd_splist ) {
                   5005:     *rp = compute_splist();
                   5006:     return;
                   5007:   }
                   5008:   if ( nd_check_splist ) {
                   5009:         if ( f4 ) {
                   5010:             if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
                   5011:             else *rp = 0;
                   5012:         } else {
                   5013:             if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
                   5014:             else *rp = 0;
                   5015:         }
                   5016:     return;
                   5017:   }
                   5018:     x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
                   5019:   if ( !x ) {
                   5020:     *rp = 0; return;
                   5021:   }
1.15      noro     5022:   if ( nd_gentrace ) {
                   5023:     MKVECT(hvect,nd_psn);
                   5024:     for ( i = 0; i < nd_psn; i++ )
                   5025:        ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   5026:   }
1.1       noro     5027:   if ( !ishomo && homo ) {
                   5028:        /* dehomogenization */
                   5029:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   5030:     nd_init_ord(ord);
                   5031:     nd_setup_parameters(nvar,0);
                   5032:   }
                   5033:     nd_demand = 0;
                   5034:   if ( nd_module && nd_intersect ) {
                   5035:     for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17      noro     5036:       if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1       noro     5037:         MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
                   5038:       }
                   5039:     conv_ilist(nd_demand,0,x,0);
                   5040:     goto FINAL;
                   5041:   }
                   5042:     if ( nd_gentrace  && f4 ) { nzlist = nd_alltracelist; }
                   5043:     x = ndv_reducebase(x,perm);
                   5044:     if ( nd_gentrace  && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
                   5045:     x = ndv_reduceall(m,x);
                   5046:     cbpe = nd_bpe;
                   5047:     if ( nd_gentrace && !f4 ) {
                   5048:         tl2 = nd_alltracelist; nd_alltracelist = 0;
                   5049:         ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
                   5050:         tl3 = nd_alltracelist; nd_alltracelist = 0;
                   5051:         if ( nd_gensyz ) {
                   5052:           nd_gb(m,0,1,1,0);
                   5053:             tl4 = nd_alltracelist; nd_alltracelist = 0;
                   5054:         } else tl4 = 0;
                   5055:     }
                   5056:     nd_bpe = cbpe;
                   5057:     nd_setup_parameters(nd_nvar,0);
                   5058: FINAL:
                   5059:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16      noro     5060:       NEXTNODE(r0,r);
                   5061:       if ( nd_module ) {
                   5062:         if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
                   5063:         else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
                   5064:       } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
                   5065:       else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1       noro     5066:     }
                   5067:     if ( r0 ) NEXT(r) = 0;
                   5068:     if ( !m && nd_nalg )
                   5069:         r0 = postprocess_algcoef(av,alist,r0);
                   5070:     MKLIST(*rp,r0);
                   5071:     if ( nd_gentrace ) {
                   5072:   if ( f4 ) {
1.6       noro     5073:             STOZ(16,bpe);
                   5074:             STOZ(nd_last_nonzero,last_nonzero);
1.15      noro     5075:             tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1       noro     5076:         } else {
                   5077:             tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   5078:             tl3 = reverse_node(tl3);
                   5079:             /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   5080:             for ( t = tl2; t; t = NEXT(t) ) {
                   5081:             /* s = [i,[*,j,*,*],...] */
                   5082:                 s = BDY((LIST)BDY(t));
1.6       noro     5083:                 j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     5084:                 for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     5085:                     j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     5086:                     ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   5087:                 }
                   5088:             }
                   5089:             for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6       noro     5090:                 STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     5091:             }
                   5092:             MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   5093:             MKLIST(l5,tl4);
1.6       noro     5094:             STOZ(nd_bpe,bpe);
1.15      noro     5095:             tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     5096:         }
                   5097:     }
                   5098: #if 0
                   5099:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   5100: #endif
                   5101: }
                   5102:
1.28      noro     5103: NODE nd_sba_f4(int m,int **indp);
                   5104:
                   5105: void nd_sba(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.24      noro     5106: {
                   5107:   VL tv,fv,vv,vc,av;
1.55      noro     5108:   NODE fd,fd0,r,r0,t,x,s,xx,nd,nd1,syz;
1.24      noro     5109:   int e,max,nvar,i;
                   5110:   NDV b;
                   5111:   int ishomo,nalg,wmax,len;
                   5112:   NMV a;
                   5113:   P p,zp;
                   5114:   Q dmy;
                   5115:   struct order_spec *ord1;
                   5116:   int j;
                   5117:   int *perm;
                   5118:   EPOS oepos;
                   5119:   int obpe,oadv,ompos,cbpe;
1.51      noro     5120:   struct oEGT eg0,eg1,egconv,egintred;
1.55      noro     5121:   LIST l1,redind;
                   5122:   Z z;
1.24      noro     5123:
                   5124:   nd_module = 0;
                   5125:   nd_demand = 0;
1.35      noro     5126:   Nsamesig = 0;
1.24      noro     5127:   if ( DP_Multiple )
                   5128:     nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   5129:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     5130:   parse_nd_option(vv,current_option);
1.24      noro     5131:   if ( m && nd_vc )
                   5132:     error("nd_sba : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
                   5133:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5134:   switch ( ord->id ) {
                   5135:     case 1:
                   5136:       if ( ord->nv != nvar )
                   5137:         error("nd_sba : invalid order specification");
                   5138:         break;
                   5139:       default:
                   5140:         break;
                   5141:   }
                   5142:   nd_nalg = 0;
                   5143:   nd_init_ord(ord);
                   5144:   // for SIG comparison
                   5145:   initd(ord);
                   5146:   for ( t = BDY(f), max = 1; t; t = NEXT(t) ) {
                   5147:     for ( tv = vv; tv; tv = NEXT(tv) ) {
1.43      noro     5148:       if ( OID(BDY(t)) == O_DP ) {
                   5149:         e = dp_getdeg((DP)BDY(t));
                   5150:         max = MAX(e,max);
                   5151:       } else {
                   5152:         e = getdeg(tv->v,(P)BDY(t));
                   5153:         max = MAX(e,max);
                   5154:       }
1.24      noro     5155:     }
                   5156:   }
1.25      noro     5157:   nd_setup_parameters(nvar,max);
1.24      noro     5158:   obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   5159:   ishomo = 1;
                   5160:   for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.43      noro     5161:     if ( OID(BDY(t)) == O_DP ) {
                   5162:       DP zdp;
                   5163:
                   5164:       if ( !m ) dp_ptozp((DP)BDY(t),&zdp);
                   5165:       else zdp = (DP)BDY(t);
                   5166:       b = (pointer)dptondv(m,zdp);
                   5167:     } else {
                   5168:       if ( !m ) ptozp((P)BDY(t),1,&dmy,&zp);
                   5169:       else zp = (P)BDY(t);
                   5170:       b = (pointer)ptondv(CO,vv,zp);
                   5171:     }
1.24      noro     5172:     if ( ishomo )
                   5173:       ishomo = ishomo && ndv_ishomo(b);
                   5174:     if ( m ) ndv_mod(m,b);
                   5175:     if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   5176:   }
                   5177:   if ( fd0 ) NEXT(fd) = 0;
                   5178:
                   5179:   if ( !ishomo && homo ) {
                   5180:     for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   5181:       b = (NDV)BDY(t); len = LEN(b);
                   5182:       for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   5183:         wmax = MAX(TD(DL(a)),wmax);
                   5184:       }
                   5185:       homogenize_order(ord,nvar,&ord1);
                   5186:       nd_init_ord(ord1);
1.42      noro     5187:       // for SIG comparison
                   5188:       initd(ord1);
1.24      noro     5189:       nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   5190:       for ( t = fd0; t; t = NEXT(t) )
                   5191:         ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   5192:   }
                   5193:
1.39      noro     5194:   ndv_setup(m,0,fd0,nd_sba_dontsort,0,1);
1.55      noro     5195:   if ( nd_gentrace ) {
                   5196:     MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   5197:   }
1.44      noro     5198:   x = f4 ? nd_sba_f4(m,&perm) : nd_sba_buch(m,ishomo || homo,&perm,&syz);
1.24      noro     5199:   if ( !x ) {
                   5200:     *rp = 0; return;
                   5201:   }
                   5202:   if ( !ishomo && homo ) {
                   5203:        /* dehomogenization */
                   5204:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   5205:     nd_init_ord(ord);
1.42      noro     5206:     // for SIG comparison
                   5207:     initd(ord);
1.24      noro     5208:     nd_setup_parameters(nvar,0);
                   5209:   }
                   5210:   nd_demand = 0;
1.51      noro     5211:   get_eg(&eg0);
1.24      noro     5212:   x = ndv_reducebase(x,perm);
1.55      noro     5213:   for ( nd = 0, i = length(x)-1; i >= 0; i-- ) {
                   5214:     STOZ(perm[i],z); MKNODE(nd1,z,nd); nd = nd1;
                   5215:   }
                   5216:   MKLIST(redind,nd);
1.24      noro     5217:   x = ndv_reduceall(m,x);
1.51      noro     5218:   get_eg(&eg1); init_eg(&egintred); add_eg(&egintred,&eg0,&eg1);
1.24      noro     5219:   nd_setup_parameters(nd_nvar,0);
1.29      noro     5220:   get_eg(&eg0);
1.24      noro     5221:   for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   5222:     NEXTNODE(r0,r);
                   5223:     if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
1.34      noro     5224:     else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.24      noro     5225:   }
                   5226:   if ( r0 ) NEXT(r) = 0;
1.44      noro     5227:   if ( nd_sba_syz ) {
                   5228:     LIST gb,hsyz;
                   5229:
                   5230:     MKLIST(gb,r0);
                   5231:     MKLIST(hsyz,syz);
                   5232:     nd = mknode(2,gb,hsyz);
                   5233:     MKLIST(*rp,nd);
1.55      noro     5234:   } else if ( nd_gentrace ) {
                   5235:     LIST gb,trace;
                   5236:
                   5237:     MKLIST(trace,nd_alltracelist);
                   5238:     MKLIST(gb,r0);
                   5239:     nd = mknode(3,gb,redind,trace);
                   5240:     MKLIST(*rp,nd);
1.44      noro     5241:   } else
                   5242:     MKLIST(*rp,r0);
1.29      noro     5243:   get_eg(&eg1); init_eg(&egconv); add_eg(&egconv,&eg0,&eg1);
1.51      noro     5244:   print_eg("intred",&egintred); fprintf(asir_out,"\n");
1.29      noro     5245:   print_eg("conv",&egconv); fprintf(asir_out,"\n");
1.24      noro     5246: }
                   5247:
1.1       noro     5248: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
                   5249: {
                   5250:     VL tv,fv,vv,vc,av;
                   5251:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   5252:     int e,max,nvar,i;
                   5253:     NDV b;
                   5254:     int ishomo,nalg;
                   5255:     Alg alpha,dp;
                   5256:     P p,zp;
                   5257:     Q dmy;
                   5258:     LIST f1,f2;
                   5259:     Obj obj;
                   5260:     NumberField nf;
                   5261:     struct order_spec *ord1;
                   5262:     int *perm;
                   5263:
                   5264:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     5265:     parse_nd_option(vv,current_option);
1.1       noro     5266:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5267:     switch ( ord->id ) {
                   5268:         case 1:
                   5269:             if ( ord->nv != nvar )
                   5270:                 error("nd_check : invalid order specification");
                   5271:             break;
                   5272:         default:
                   5273:             break;
                   5274:     }
                   5275:     nd_nalg = 0;
                   5276:     av = 0;
                   5277:     if ( !m ) {
                   5278:         get_algtree((Obj)f,&av);
                   5279:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   5280:         nd_ntrans = nvar;
                   5281:         nd_nalg = nalg;
                   5282:         /* #i -> t#i */
                   5283:         if ( nalg ) {
                   5284:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   5285:             ord = ord1;
                   5286:             f = f1;
                   5287:         }
                   5288:         nvar += nalg;
                   5289:     }
                   5290:     nd_init_ord(ord);
                   5291:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   5292:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   5293:             e = getdeg(tv->v,(P)BDY(t));
                   5294:             max = MAX(e,max);
                   5295:         }
                   5296:     nd_setup_parameters(nvar,max);
                   5297:     ishomo = 1;
                   5298:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   5299:         ptozp((P)BDY(t),1,&dmy,&zp);
                   5300:         b = (pointer)ptondv(CO,vv,zp);
                   5301:         if ( ishomo )
                   5302:             ishomo = ishomo && ndv_ishomo(b);
                   5303:         if ( m ) ndv_mod(m,b);
                   5304:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   5305:     }
                   5306:     if ( fd0 ) NEXT(fd) = 0;
1.24      noro     5307:     ndv_setup(m,0,fd0,0,1,0);
1.1       noro     5308:     for ( x = 0, i = 0; i < nd_psn; i++ )
                   5309:         x = update_base(x,i);
                   5310:     if ( do_check ) {
                   5311:         x = nd_gb(m,ishomo,1,0,&perm);
                   5312:         if ( !x ) {
                   5313:             *rp = 0;
                   5314:             return;
                   5315:         }
                   5316:     } else {
                   5317: #if 0
                   5318:     /* bug ? */
                   5319:         for ( t = x; t; t = NEXT(t) )
                   5320:             BDY(t) = (pointer)nd_ps[(long)BDY(t)];
                   5321: #else
                   5322:     conv_ilist(0,0,x,&perm);
                   5323: #endif
                   5324:     }
                   5325:     x = ndv_reducebase(x,perm);
                   5326:     x = ndv_reduceall(m,x);
                   5327:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   5328:         NEXTNODE(r0,r);
                   5329:         BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   5330:     }
                   5331:     if ( r0 ) NEXT(r) = 0;
                   5332:     if ( !m && nd_nalg )
                   5333:         r0 = postprocess_algcoef(av,alist,r0);
                   5334:     MKLIST(*rp,r0);
                   5335: }
                   5336:
                   5337: NDV recompute_trace(NODE trace,NDV *p,int m);
                   5338: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
                   5339:
                   5340: NDV recompute_trace(NODE ti,NDV *p,int mod)
                   5341: {
                   5342:   int c,c1,c2,i;
                   5343:   NM mul,m,tail;
                   5344:   ND d,r,rm;
                   5345:   NODE sj;
                   5346:   NDV red;
                   5347:   Obj mj;
                   5348:
                   5349:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   5350:   CM(mul) = 1;
                   5351:   tail = 0;
                   5352:   for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
                   5353:     sj = BDY((LIST)BDY(ti));
                   5354:     if ( ARG0(sj) ) {
1.6       noro     5355:       red = p[ZTOS((Q)ARG1(sj))];
1.1       noro     5356:       mj = (Obj)ARG2(sj);
                   5357:       if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
                   5358:       else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
                   5359:       rm = ndv_mul_nm(mod,mul,red);
                   5360:       if ( !r ) r = rm;
                   5361:       else {
                   5362:         for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
                   5363:           if ( d ) {
                   5364:             NEXT(tail) = m; tail = m; LEN(d)++;
                   5365:           } else {
                   5366:             MKND(nd_nvar,m,1,d); tail = BDY(d);
                   5367:           }
                   5368:         }
                   5369:         if ( !m ) return 0; /* failure */
                   5370:                 else {
                   5371:           BDY(r) = m;
                   5372:                     if ( mod > 0 || mod == -1 ) {
                   5373:             c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
                   5374:             DMAR(c1,c2,0,mod,c);
                   5375:             nd_mul_c(mod,rm,c);
                   5376:                     } else {
                   5377:                       Z t,u;
                   5378:
                   5379:                       chsgnlf(HCZ(r),&t);
                   5380:                       divlf(t,HCZ(rm),&u);
                   5381:                       nd_mul_c_lf(rm,u);
                   5382:                     }
                   5383:           r = nd_add(mod,r,rm);
                   5384:         }
                   5385:           }
                   5386:      }
                   5387:   }
                   5388:   if ( tail ) NEXT(tail) = 0;
                   5389:   d = nd_add(mod,d,r);
                   5390:   nd_mul_c(mod,d,invm(HCM(d),mod));
                   5391:   return ndtondv(mod,d);
                   5392: }
                   5393:
                   5394: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
                   5395: {
                   5396:     VL tv,fv,vv,vc,av;
                   5397:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   5398:     int e,max,nvar,i;
                   5399:     NDV b;
                   5400:     int ishomo,nalg;
                   5401:     Alg alpha,dp;
                   5402:     P p,zp;
                   5403:     Q dmy;
                   5404:     LIST f1,f2;
                   5405:     Obj obj;
                   5406:     NumberField nf;
                   5407:     struct order_spec *ord1;
                   5408:   NODE permtrace,intred,ind,perm,trace,ti;
                   5409:   int len,n,j;
                   5410:   NDV *db,*pb;
                   5411:
                   5412:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     5413:     parse_nd_option(vv,current_option);
1.1       noro     5414:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5415:     switch ( ord->id ) {
                   5416:         case 1:
                   5417:             if ( ord->nv != nvar )
                   5418:                 error("nd_check : invalid order specification");
                   5419:             break;
                   5420:         default:
                   5421:             break;
                   5422:     }
                   5423:     nd_init_ord(ord);
1.6       noro     5424:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     5425:     nd_setup_parameters(nvar,0);
                   5426:
                   5427:   len = length(BDY(f));
                   5428:   db = (NDV *)MALLOC(len*sizeof(NDV *));
                   5429:   for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
                   5430:       ptozp((P)BDY(t),1,&dmy,&zp);
                   5431:       b = ptondv(CO,vv,zp);
                   5432:         ndv_mod(m,b);
                   5433:     ndv_mul_c(m,b,invm(HCM(b),m));
                   5434:     db[i] = b;
                   5435:     }
                   5436:
                   5437:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   5438:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   5439:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   5440:   perm = BDY((LIST)ARG0(permtrace));
                   5441:   trace = NEXT(permtrace);
                   5442:
                   5443:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     5444:     j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1       noro     5445:     if ( j > i ) i = j;
                   5446:   }
                   5447:   n = i+1;
                   5448:   pb = (NDV *)MALLOC(n*sizeof(NDV *));
                   5449:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   5450:     ti = BDY((LIST)BDY(t));
1.6       noro     5451:     pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1       noro     5452:   }
                   5453:   for ( t = trace; t; t = NEXT(t) ) {
                   5454:     ti = BDY((LIST)BDY(t));
1.6       noro     5455:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   5456:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     5457:       if ( DP_Print ) {
                   5458:            fprintf(asir_out,"."); fflush(asir_out);
                   5459:       }
                   5460:   }
                   5461:   for ( t = intred; t; t = NEXT(t) ) {
                   5462:     ti = BDY((LIST)BDY(t));
1.6       noro     5463:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   5464:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     5465:       if ( DP_Print ) {
                   5466:            fprintf(asir_out,"*"); fflush(asir_out);
                   5467:       }
                   5468:   }
                   5469:     for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
                   5470:         NEXTNODE(r0,r);
1.6       noro     5471:     b = pb[ZTOS((Q)BDY(t))];
1.1       noro     5472:         ndv_mul_c(m,b,invm(HCM(b),m));
                   5473: #if 0
1.6       noro     5474:         BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1       noro     5475: #else
1.6       noro     5476:         BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1       noro     5477: #endif
                   5478:     }
                   5479:     if ( r0 ) NEXT(r) = 0;
                   5480:     MKLIST(*rp,r0);
                   5481:     if ( DP_Print ) fprintf(asir_out,"\n");
                   5482: }
                   5483:
1.16      noro     5484: 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     5485: {
                   5486:     VL tv,fv,vv,vc,av;
                   5487:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   5488:     int m,nocheck,nvar,mindex,e,max;
                   5489:     NDV c;
                   5490:     NMV a;
                   5491:     P p,zp;
                   5492:     Q dmy;
                   5493:     EPOS oepos;
                   5494:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   5495:     Alg alpha,dp;
                   5496:     P poly;
                   5497:     LIST f1,f2,zpl;
                   5498:     Obj obj;
                   5499:     NumberField nf;
                   5500:     struct order_spec *ord1;
1.64    ! noro     5501:     struct oEGT eg_check,eg_gb,eg_intred,eg0,eg1;
1.1       noro     5502:     NODE tr,tl1,tl2,tl3,tl4;
                   5503:     LIST l1,l2,l3,l4,l5;
                   5504:     int *perm;
                   5505:     int j,ret;
                   5506:     Z jq,bpe;
1.15      noro     5507:     VECT hvect;
1.1       noro     5508:
1.41      noro     5509:     NcriB = NcriMF = Ncri2 = 0;
1.1       noro     5510:     nd_module = 0;
                   5511:     nd_lf = 0;
1.46      noro     5512:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   5513:     parse_nd_option(vv,current_option);
1.1       noro     5514:     if ( nd_lf ) {
                   5515:       if ( f4 )
                   5516:         nd_f4_lf_trace(f,v,trace,homo,ord,rp);
                   5517:       else
                   5518:         error("nd_gr_trace is not implemented yet over a large finite field");
                   5519:       return;
                   5520:     }
                   5521:     if ( DP_Multiple )
                   5522:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   5523:
                   5524:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5525:     switch ( ord->id ) {
                   5526:         case 1:
                   5527:             if ( ord->nv != nvar )
                   5528:                 error("nd_gr_trace : invalid order specification");
                   5529:             break;
                   5530:         default:
                   5531:             break;
                   5532:     }
                   5533:
                   5534:     get_algtree((Obj)f,&av);
                   5535:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   5536:     nd_ntrans = nvar;
                   5537:     nd_nalg = nalg;
                   5538:     /* #i -> t#i */
                   5539:     if ( nalg ) {
                   5540:         preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   5541:         ord = ord1;
                   5542:         f = f1;
                   5543:     }
                   5544:     nvar += nalg;
                   5545:
                   5546:     nocheck = 0;
                   5547:     mindex = 0;
                   5548:
                   5549:     if ( Demand ) nd_demand = 1;
                   5550:     else nd_demand = 0;
                   5551:
                   5552:     /* setup modulus */
                   5553:     if ( trace < 0 ) {
                   5554:         trace = -trace;
                   5555:         nocheck = 1;
                   5556:     }
                   5557:     m = trace > 1 ? trace : get_lprime(mindex);
                   5558:     nd_init_ord(ord);
                   5559:     mrank = 0;
                   5560:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   5561:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   5562:             if ( nd_module ) {
1.16      noro     5563:               if ( OID(BDY(t)) == O_DPM ) {
                   5564:                 e = dpm_getdeg((DPM)BDY(t),&trank);
                   5565:                 max = MAX(e,max);
                   5566:                 mrank = MAX(mrank,trank);
                   5567:               } else {
1.1       noro     5568:                 s = BDY((LIST)BDY(t));
                   5569:                 trank = length(s);
                   5570:                 mrank = MAX(mrank,trank);
                   5571:                 for ( ; s; s = NEXT(s) ) {
                   5572:                     e = getdeg(tv->v,(P)BDY(s));
                   5573:                     max = MAX(e,max);
                   5574:                 }
1.16      noro     5575:               }
1.1       noro     5576:             } else {
1.43      noro     5577:               if ( OID(BDY(t)) == O_DP ) {
                   5578:                 e = dp_getdeg((DP)BDY(t));
                   5579:                 max = MAX(e,max);
                   5580:               } else {
1.1       noro     5581:                 e = getdeg(tv->v,(P)BDY(t));
                   5582:                 max = MAX(e,max);
1.43      noro     5583:               }
1.1       noro     5584:             }
                   5585:         }
                   5586:     nd_setup_parameters(nvar,max);
                   5587:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   5588:     ishomo = 1;
                   5589:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   5590:         if ( nd_module ) {
1.16      noro     5591:           if ( OID(BDY(t)) == O_DPM ) {
                   5592:             Z cont;
                   5593:             DPM zdpm;
                   5594:
1.17      noro     5595:             if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16      noro     5596:             else zdpm = (DPM)BDY(t);
                   5597:             c = (pointer)dpmtondv(m,zdpm);
                   5598:           } else {
                   5599:             if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   5600:             else zpl = (LIST)BDY(t);
1.1       noro     5601:             c = (pointer)pltondv(CO,vv,zpl);
1.16      noro     5602:           }
1.1       noro     5603:         } else {
1.43      noro     5604:           if ( OID(BDY(t)) == O_DP ) {
                   5605:             DP zdp;
                   5606:
                   5607:             if ( !nd_gentrace ) dp_ptozp((DP)BDY(t),&zdp);
                   5608:             else zdp = (DP)BDY(t);
                   5609:             c = (pointer)dptondv(m,zdp);
                   5610:           } else {
                   5611:             if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   5612:             else zp = (P)BDY(t);
                   5613:             c = (pointer)ptondv(CO,vv,zp);
                   5614:           }
1.1       noro     5615:         }
                   5616:         if ( ishomo )
                   5617:             ishomo = ishomo && ndv_ishomo(c);
                   5618:         if ( c ) {
                   5619:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   5620:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   5621:         }
                   5622:     }
                   5623:     if ( in0 ) NEXT(in) = 0;
                   5624:     if ( fd0 ) NEXT(fd) = 0;
                   5625:     if ( !ishomo && homo ) {
                   5626:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   5627:             c = (NDV)BDY(t); len = LEN(c);
                   5628:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   5629:                 wmax = MAX(TD(DL(a)),wmax);
                   5630:         }
                   5631:         homogenize_order(ord,nvar,&ord1);
                   5632:         nd_init_ord(ord1);
                   5633:         nd_setup_parameters(nvar+1,wmax);
                   5634:         for ( t = fd0; t; t = NEXT(t) )
                   5635:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   5636:     }
                   5637:     if ( MaxDeg > 0 ) nocheck = 1;
                   5638:     while ( 1 ) {
1.64    ! noro     5639:         get_eg(&eg0);
        !          5640:         tl1 = tl2 = tl3 = tl4 = 0;
1.1       noro     5641:         if ( Demand )
                   5642:             nd_demand = 1;
1.24      noro     5643:         ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1       noro     5644:         if ( nd_gentrace ) {
                   5645:             MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   5646:         }
                   5647:         if ( ret )
                   5648:             cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
                   5649:         if ( !ret || !cand ) {
                   5650:             /* failure */
                   5651:             if ( trace > 1 ) { *rp = 0; return; }
                   5652:             else m = get_lprime(++mindex);
                   5653:             continue;
                   5654:         }
1.15      noro     5655:         if ( nd_gentrace ) {
                   5656:           MKVECT(hvect,nd_psn);
                   5657:           for ( i = 0; i < nd_psn; i++ )
                   5658:              ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   5659:         }
1.1       noro     5660:         if ( !ishomo && homo ) {
                   5661:             /* dehomogenization */
                   5662:             for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   5663:             nd_init_ord(ord);
                   5664:             nd_setup_parameters(nvar,0);
                   5665:         }
1.64    ! noro     5666:         get_eg(&eg1); init_eg(&eg_gb); add_eg(&eg_gb,&eg0,&eg1);
1.1       noro     5667:         nd_demand = 0;
                   5668:         cand = ndv_reducebase(cand,perm);
                   5669:         if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
1.64    ! noro     5670:         get_eg(&eg0);
1.1       noro     5671:         cand = ndv_reduceall(0,cand);
1.64    ! noro     5672:         get_eg(&eg1); init_eg(&eg_intred); add_eg(&eg_intred,&eg0,&eg1);
1.1       noro     5673:         cbpe = nd_bpe;
                   5674:         if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
                   5675:         get_eg(&eg0);
                   5676:         if ( nocheck )
                   5677:             break;
                   5678:         if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   5679:             if ( nd_gentrace ) {
                   5680:           tl3 = nd_alltracelist; nd_alltracelist = 0;
                   5681:         } else tl3 = 0;
                   5682:             /* gbcheck : cand is a GB of Id(cand) ? */
1.54      noro     5683:             if ( nd_vc || nd_gentrace || nd_gensyz || do_weyl )
1.1       noro     5684:               ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
                   5685:             else
                   5686:               ret = nd_f4(0,1,0)!=0;
                   5687:             if ( nd_gentrace && nd_gensyz ) {
                   5688:           tl4 = nd_alltracelist; nd_alltracelist = 0;
                   5689:         } else tl4 = 0;
                   5690:     }
                   5691:     if ( ret ) break;
                   5692:         else if ( trace > 1 ) {
                   5693:             /* failure */
                   5694:             *rp = 0; return;
                   5695:         } else {
                   5696:             /* try the next modulus */
                   5697:             m = get_lprime(++mindex);
                   5698:             /* reset the parameters */
                   5699:             if ( !ishomo && homo ) {
                   5700:                 nd_init_ord(ord1);
                   5701:                 nd_setup_parameters(nvar+1,wmax);
                   5702:             } else {
                   5703:                 nd_init_ord(ord);
                   5704:                 nd_setup_parameters(nvar,max);
                   5705:             }
                   5706:         }
                   5707:     }
                   5708:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   5709:     if ( DP_Print )
1.64    ! noro     5710:         fprintf(asir_out,"gb=%.3fsec,check=%.3fsec,intred=%.3fsec\n",
        !          5711:           eg_gb.exectime,eg_check.exectime,eg_intred.exectime);
1.1       noro     5712:     /* dp->p */
                   5713:     nd_bpe = cbpe;
                   5714:     nd_setup_parameters(nd_nvar,0);
                   5715:     for ( r = cand; r; r = NEXT(r) ) {
1.16      noro     5716:       if ( nd_module ) {
1.17      noro     5717:         if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16      noro     5718:         else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17      noro     5719:       } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
                   5720:       else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1       noro     5721:     }
                   5722:     if ( nd_nalg )
                   5723:         cand = postprocess_algcoef(av,alist,cand);
                   5724:     MKLIST(*rp,cand);
                   5725:     if ( nd_gentrace ) {
                   5726:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   5727:     tl3 = reverse_node(tl3);
                   5728:     /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   5729:         for ( t = tl2; t; t = NEXT(t) ) {
                   5730:       /* s = [i,[*,j,*,*],...] */
                   5731:             s = BDY((LIST)BDY(t));
1.6       noro     5732:             j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     5733:       for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     5734:                 j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     5735:         ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   5736:             }
                   5737:     }
                   5738:     for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6       noro     5739:         STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     5740:     }
                   5741:         MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   5742:     MKLIST(l5,tl4);
1.6       noro     5743:       STOZ(nd_bpe,bpe);
1.15      noro     5744:         tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     5745:     }
                   5746: }
                   5747:
                   5748: /* XXX : module element is not considered  */
                   5749:
                   5750: void dltondl(int n,DL dl,UINT *r)
                   5751: {
                   5752:     UINT *d;
                   5753:     int i,j,l,s,ord_l;
                   5754:     struct order_pair *op;
                   5755:
                   5756:     d = (unsigned int *)dl->d;
                   5757:     for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
                   5758:     if ( nd_blockmask ) {
                   5759:         l = nd_blockmask->n;
                   5760:         op = nd_blockmask->order_pair;
                   5761:         for ( j = 0, s = 0; j < l; j++ ) {
                   5762:             ord_l = op[j].length;
                   5763:             for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
                   5764:         }
                   5765:         TD(r) = ndl_weight(r);
                   5766:         ndl_weight_mask(r);
                   5767:     } else {
                   5768:         for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
                   5769:         TD(r) = ndl_weight(r);
                   5770:     }
                   5771: }
                   5772:
                   5773: DL ndltodl(int n,UINT *ndl)
                   5774: {
                   5775:     DL dl;
                   5776:     int *d;
                   5777:     int i,j,l,s,ord_l;
                   5778:     struct order_pair *op;
                   5779:
1.33      noro     5780:     NEWDL_NOINIT(dl,n);
1.1       noro     5781:     dl->td = TD(ndl);
                   5782:     d = dl->d;
                   5783:     if ( nd_blockmask ) {
                   5784:         l = nd_blockmask->n;
                   5785:         op = nd_blockmask->order_pair;
                   5786:         for ( j = 0, s = 0; j < l; j++ ) {
                   5787:             ord_l = op[j].length;
                   5788:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   5789:         }
                   5790:     } else {
                   5791:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   5792:     }
                   5793:     return dl;
                   5794: }
                   5795:
1.24      noro     5796: void _ndltodl(UINT *ndl,DL dl)
                   5797: {
                   5798:     int *d;
                   5799:     int i,j,l,s,ord_l,n;
                   5800:     struct order_pair *op;
                   5801:
                   5802:     n = nd_nvar;
                   5803:     dl->td = TD(ndl);
                   5804:     d = dl->d;
                   5805:     if ( nd_blockmask ) {
                   5806:         l = nd_blockmask->n;
                   5807:         op = nd_blockmask->order_pair;
                   5808:         for ( j = 0, s = 0; j < l; j++ ) {
                   5809:             ord_l = op[j].length;
                   5810:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   5811:         }
                   5812:     } else {
                   5813:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   5814:     }
                   5815: }
                   5816:
1.1       noro     5817: void nmtodp(int mod,NM m,DP *r)
                   5818: {
                   5819:     DP dp;
                   5820:     MP mr;
                   5821:
                   5822:     NEWMP(mr);
                   5823:     mr->dl = ndltodl(nd_nvar,DL(m));
                   5824:     mr->c = (Obj)ndctop(mod,m->c);
                   5825:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   5826:     *r = dp;
                   5827: }
                   5828:
1.15      noro     5829: void ndltodp(UINT *d,DP *r)
                   5830: {
                   5831:     DP dp;
                   5832:     MP mr;
                   5833:
                   5834:     NEWMP(mr);
                   5835:     mr->dl = ndltodl(nd_nvar,d);
                   5836:     mr->c = (Obj)ONE;
                   5837:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   5838:     *r = dp;
                   5839: }
                   5840:
1.1       noro     5841: void ndl_print(UINT *dl)
                   5842: {
                   5843:     int n;
                   5844:     int i,j,l,ord_l,s,s0;
                   5845:     struct order_pair *op;
                   5846:
                   5847:     n = nd_nvar;
                   5848:     printf("<<");
                   5849:     if ( nd_blockmask ) {
                   5850:         l = nd_blockmask->n;
                   5851:         op = nd_blockmask->order_pair;
                   5852:         for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   5853:             ord_l = op[j].length;
                   5854:             for ( i = 0; i < ord_l; i++, s++ )
                   5855:                 printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
                   5856:         }
                   5857:     } else {
                   5858:         for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
                   5859:     }
                   5860:     printf(">>");
                   5861:     if ( nd_module && MPOS(dl) )
                   5862:         printf("*e%d",MPOS(dl));
                   5863: }
                   5864:
                   5865: void nd_print(ND p)
                   5866: {
                   5867:     NM m;
                   5868:
                   5869:     if ( !p )
                   5870:         printf("0\n");
                   5871:     else {
                   5872:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5873:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     5874:             else printf("+%ld*",CM(m));
1.1       noro     5875:             ndl_print(DL(m));
                   5876:         }
                   5877:         printf("\n");
                   5878:     }
                   5879: }
                   5880:
                   5881: void nd_print_q(ND p)
                   5882: {
                   5883:     NM m;
                   5884:
                   5885:     if ( !p )
                   5886:         printf("0\n");
                   5887:     else {
                   5888:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5889:             printf("+");
1.6       noro     5890:             printexpr(CO,(Obj)CZ(m));
1.1       noro     5891:             printf("*");
                   5892:             ndl_print(DL(m));
                   5893:         }
                   5894:         printf("\n");
                   5895:     }
                   5896: }
                   5897:
                   5898: void ndp_print(ND_pairs d)
                   5899: {
                   5900:     ND_pairs t;
                   5901:
                   5902:     for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
                   5903:     printf("\n");
                   5904: }
                   5905:
                   5906: void nd_removecont(int mod,ND p)
                   5907: {
                   5908:     int i,n;
                   5909:     Z *w;
                   5910:     NM m;
                   5911:     struct oVECT v;
                   5912:
                   5913:     if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
                   5914:     else if ( mod == -2 ) {
                   5915:       Z inv;
                   5916:       divlf(ONE,HCZ(p),&inv);
                   5917:       nd_mul_c_lf(p,inv);
                   5918:     } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
                   5919:     else {
                   5920:         for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   5921:         w = (Z *)MALLOC(n*sizeof(Q));
                   5922:         v.len = n;
                   5923:         v.body = (pointer *)w;
1.6       noro     5924:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5925:         removecont_array((P *)w,n,1);
1.6       noro     5926:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5927:     }
                   5928: }
                   5929:
                   5930: void nd_removecont2(ND p1,ND p2)
                   5931: {
                   5932:     int i,n1,n2,n;
                   5933:     Z *w;
                   5934:     NM m;
                   5935:     struct oVECT v;
                   5936:
                   5937:     n1 = nd_length(p1);
                   5938:     n2 = nd_length(p2);
                   5939:     n = n1+n2;
                   5940:     w = (Z *)MALLOC(n*sizeof(Q));
                   5941:     v.len = n;
                   5942:     v.body = (pointer *)w;
                   5943:     i = 0;
                   5944:     if ( p1 )
1.6       noro     5945:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5946:     if ( p2 )
1.6       noro     5947:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5948:     removecont_array((P *)w,n,1);
                   5949:     i = 0;
                   5950:     if ( p1 )
1.6       noro     5951:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5952:     if ( p2 )
1.6       noro     5953:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5954: }
                   5955:
                   5956: void ndv_removecont(int mod,NDV p)
                   5957: {
                   5958:     int i,len,all_p;
                   5959:     Z *c;
                   5960:     P *w;
                   5961:     Z dvr,t;
                   5962:     P g,cont,tp;
                   5963:     NMV m;
                   5964:
                   5965:     if ( mod == -1 )
                   5966:         ndv_mul_c(mod,p,_invsf(HCM(p)));
                   5967:     else if ( mod == -2 ) {
                   5968:       Z inv;
                   5969:       divlf(ONE,HCZ(p),&inv);
                   5970:       ndv_mul_c_lf(p,inv);
                   5971:     } else if ( mod )
                   5972:         ndv_mul_c(mod,p,invm(HCM(p),mod));
                   5973:     else {
                   5974:         len = p->len;
                   5975:         w = (P *)MALLOC(len*sizeof(P));
                   5976:         c = (Z *)MALLOC(len*sizeof(Q));
                   5977:         for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
                   5978:             ptozp(CP(m),1,(Q *)&c[i],&w[i]);
                   5979:             all_p = all_p && !NUM(w[i]);
                   5980:         }
                   5981:         if ( all_p ) {
                   5982:             qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
                   5983:             mulp(nd_vc,(P)dvr,g,&cont);
                   5984:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5985:                 divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
                   5986:             }
                   5987:         } else {
                   5988:             sortbynm((Q *)c,len);
                   5989:             qltozl((Q *)c,len,&dvr);
                   5990:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5991:                 divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
                   5992:             }
                   5993:         }
                   5994:     }
                   5995: }
                   5996:
                   5997: /* koko */
                   5998:
                   5999: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
                   6000: {
                   6001:     int len,i,max;
                   6002:     NMV m,mr0,mr,t;
                   6003:
                   6004:     len = p->len;
1.14      noro     6005:     for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1       noro     6006:         max = MAX(max,TD(DL(m)));
                   6007:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   6008:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   6009:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   6010:     t = (NMV)MALLOC(nmv_adv);
                   6011:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   6012:         ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6       noro     6013:         CZ(mr) = CZ(m);
1.1       noro     6014:         ndl_copy(DL(t),DL(mr));
                   6015:     }
                   6016:     NV(p)++;
                   6017:     BDY(p) = mr0;
                   6018: }
                   6019:
                   6020: void ndv_dehomogenize(NDV p,struct order_spec *ord)
                   6021: {
                   6022:     int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
                   6023:     int pos;
                   6024:     Q *w;
                   6025:     Q dvr,t;
                   6026:     NMV m,r;
                   6027:
                   6028:     len = p->len;
                   6029:     newnvar = nd_nvar-1;
                   6030:     newexporigin = nd_get_exporigin(ord);
                   6031:     if ( nd_module ) newmpos = newexporigin-1;
                   6032:     newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
                   6033:     for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
                   6034:         ndl_dehomogenize(DL(m));
                   6035:     if ( newwpd != nd_wpd ) {
                   6036:         newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
                   6037:         for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6       noro     6038:             CZ(r) = CZ(m);
1.1       noro     6039:             if ( nd_module ) pos = MPOS(DL(m));
                   6040:             for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   6041:             adj = nd_exporigin-newexporigin;
                   6042:             for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
                   6043:             if ( nd_module ) {
                   6044:                 DL(r)[newmpos] = pos;
                   6045:             }
                   6046:         }
                   6047:     }
                   6048:     NV(p)--;
                   6049: }
                   6050:
                   6051: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
                   6052: {
                   6053:     int i;
                   6054:     P *tpl,*tpl1;
                   6055:     NODE l;
                   6056:     P h,gcd,t;
                   6057:
                   6058:     tpl = (P *)MALLOC(m*sizeof(P));
                   6059:     tpl1 = (P *)MALLOC(m*sizeof(P));
                   6060:     bcopy(pl,tpl,m*sizeof(P));
                   6061:     gcd = (P)ONE;
                   6062:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   6063:         h = (P)BDY(l);
                   6064:         while ( 1 ) {
                   6065:             for ( i = 0; i < m; i++ )
                   6066:                 if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
                   6067:                     break;
                   6068:             if ( i == m ) {
                   6069:                 bcopy(tpl1,tpl,m*sizeof(P));
                   6070:                 mulp(vl,gcd,h,&t); gcd = t;
                   6071:             } else
                   6072:                 break;
                   6073:         }
                   6074:     }
                   6075:     if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
                   6076:     if ( full ) {
                   6077:         heu_nezgcdnpz(vl,tpl,m,&t);
                   6078:         mulp(vl,gcd,t,pr);
                   6079:     } else
                   6080:         *pr = gcd;
                   6081: }
                   6082:
                   6083: void removecont_array(P *p,int n,int full)
                   6084: {
                   6085:     int all_p,all_q,i;
                   6086:     Z *c;
                   6087:     P *w;
                   6088:     P t,s;
                   6089:
                   6090:     for ( all_q = 1, i = 0; i < n; i++ )
                   6091:         all_q = all_q && NUM(p[i]);
                   6092:     if ( all_q ) {
                   6093:         removecont_array_q((Z *)p,n);
                   6094:     } else {
                   6095:         c = (Z *)MALLOC(n*sizeof(Z));
                   6096:         w = (P *)MALLOC(n*sizeof(P));
                   6097:         for ( i = 0; i < n; i++ ) {
                   6098:             ptozp(p[i],1,(Q *)&c[i],&w[i]);
                   6099:         }
                   6100:         removecont_array_q(c,n);
                   6101:         nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
                   6102:         for ( i = 0; i < n; i++ ) {
                   6103:             divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
                   6104:         }
                   6105:     }
                   6106: }
                   6107:
                   6108: /* c is an int array */
                   6109:
                   6110: void removecont_array_q(Z *c,int n)
                   6111: {
                   6112:   struct oVECT v;
                   6113:   Z d0,d1,a,u,u1,gcd;
                   6114:   int i,j;
                   6115:   Z *q,*r;
                   6116:
                   6117:   q = (Z *)MALLOC(n*sizeof(Z));
                   6118:   r = (Z *)MALLOC(n*sizeof(Z));
                   6119:   v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   6120:   gcdvz_estimate(&v,&d0);
                   6121:   for ( i = 0; i < n; i++ ) {
                   6122:     divqrz(c[i],d0,&q[i],&r[i]);
                   6123:   }
                   6124:   for ( i = 0; i < n; i++ ) if ( r[i] ) break;
                   6125:   if ( i < n ) {
                   6126:     v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   6127:     gcdvz(&v,&d1);
                   6128:     gcdz(d0,d1,&gcd);
1.6       noro     6129:     /* exact division */
                   6130:     divsz(d0,gcd,&a);
1.1       noro     6131:     for ( i = 0; i < n; i++ ) {
                   6132:       mulz(a,q[i],&u);
                   6133:       if ( r[i] ) {
1.6       noro     6134:         /* exact division */
                   6135:         divsz(r[i],gcd,&u1);
1.1       noro     6136:         addz(u,u1,&q[i]);
                   6137:       } else
                   6138:         q[i] = u;
                   6139:     }
                   6140:   }
                   6141:   for ( i = 0; i < n; i++ ) c[i] = q[i];
                   6142: }
                   6143:
1.4       noro     6144: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
                   6145:
                   6146: void mpz_removecont_array(mpz_t *c,int n)
                   6147: {
                   6148:   mpz_t d0,a,u,u1,gcd;
                   6149:   int i,j;
1.13      noro     6150:   static mpz_t *q,*r;
                   6151:   static int c_len = 0;
1.4       noro     6152:
                   6153:   for ( i = 0; i < n; i++ )
                   6154:     if ( mpz_sgn(c[i]) ) break;
                   6155:   if ( i == n ) return;
                   6156:   gcdv_mpz_estimate(d0,c,n);
1.13      noro     6157:   if ( n > c_len ) {
                   6158:     q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   6159:     r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   6160:     c_len = n;
                   6161:   }
1.4       noro     6162:   for ( i = 0; i < n; i++ ) {
                   6163:     mpz_init(q[i]); mpz_init(r[i]);
                   6164:     mpz_fdiv_qr(q[i],r[i],c[i],d0);
                   6165:   }
                   6166:   for ( i = 0; i < n; i++ )
                   6167:     if ( mpz_sgn(r[i]) ) break;
                   6168:   mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
                   6169:   if ( i < n ) {
                   6170:     mpz_gcd(gcd,d0,r[i]);
                   6171:     for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
                   6172:     mpz_div(a,d0,gcd);
                   6173:     for ( i = 0; i < n; i++ ) {
                   6174:       mpz_mul(u,a,q[i]);
                   6175:       if ( mpz_sgn(r[i]) ) {
                   6176:         mpz_div(u1,r[i],gcd);
                   6177:         mpz_add(q[i],u,u1);
                   6178:       } else
                   6179:         mpz_set(q[i],u);
                   6180:     }
                   6181:   }
                   6182:   for ( i = 0; i < n; i++ )
                   6183:     mpz_set(c[i],q[i]);
                   6184: }
                   6185:
1.1       noro     6186: void nd_mul_c(int mod,ND p,int mul)
                   6187: {
                   6188:     NM m;
                   6189:     int c,c1;
                   6190:
                   6191:     if ( !p ) return;
                   6192:     if ( mul == 1 ) return;
                   6193:     if ( mod == -1 )
                   6194:         for ( m = BDY(p); m; m = NEXT(m) )
                   6195:             CM(m) = _mulsf(CM(m),mul);
                   6196:     else
                   6197:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   6198:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   6199:         }
                   6200: }
                   6201:
                   6202: void nd_mul_c_lf(ND p,Z mul)
                   6203: {
                   6204:     NM m;
                   6205:     Z c;
                   6206:
                   6207:     if ( !p ) return;
                   6208:     if ( UNIZ(mul) ) return;
                   6209:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   6210:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   6211:     }
                   6212: }
                   6213:
                   6214: void nd_mul_c_q(ND p,P mul)
                   6215: {
                   6216:     NM m;
                   6217:     P c;
                   6218:
                   6219:     if ( !p ) return;
                   6220:     if ( UNIQ(mul) ) return;
                   6221:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   6222:         mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
                   6223:     }
                   6224: }
                   6225:
                   6226: void nd_mul_c_p(VL vl,ND p,P mul)
                   6227: {
                   6228:     NM m;
                   6229:     P c;
                   6230:
                   6231:     if ( !p ) return;
                   6232:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   6233:         mulp(vl,CP(m),mul,&c); CP(m) = c;
                   6234:     }
                   6235: }
                   6236:
                   6237: void nd_free(ND p)
                   6238: {
                   6239:     NM t,s;
                   6240:
                   6241:     if ( !p ) return;
                   6242:     t = BDY(p);
                   6243:     while ( t ) {
                   6244:         s = NEXT(t);
                   6245:         FREENM(t);
                   6246:         t = s;
                   6247:     }
                   6248:     FREEND(p);
                   6249: }
                   6250:
                   6251: void ndv_free(NDV p)
                   6252: {
                   6253:     GCFREE(BDY(p));
                   6254: }
                   6255:
                   6256: void nd_append_red(UINT *d,int i)
                   6257: {
                   6258:     RHist m,m0;
                   6259:     int h;
                   6260:
                   6261:     NEWRHist(m);
                   6262:     h = ndl_hash_value(d);
                   6263:     m->index = i;
                   6264:     ndl_copy(d,DL(m));
                   6265:     NEXT(m) = nd_red[h];
                   6266:     nd_red[h] = m;
                   6267: }
                   6268:
                   6269: UINT *ndv_compute_bound(NDV p)
                   6270: {
                   6271:     UINT *d1,*d2,*t;
                   6272:     UINT u;
                   6273:     int i,j,k,l,len,ind;
                   6274:     NMV m;
                   6275:
                   6276:     if ( !p )
                   6277:         return 0;
                   6278:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6279:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6280:     len = LEN(p);
                   6281:     m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   6282:     for ( i = 1; i < len; i++, NMV_ADV(m) ) {
                   6283:         ndl_max(DL(m),d1,d2);
                   6284:         t = d1; d1 = d2; d2 = t;
                   6285:     }
                   6286:     l = nd_nvar+31;
                   6287:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   6288:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   6289:         u = d1[i];
                   6290:         k = (nd_epw-1)*nd_bpe;
                   6291:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   6292:             t[ind] = (u>>k)&nd_mask0;
                   6293:     }
                   6294:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   6295:     return t;
                   6296: }
                   6297:
                   6298: UINT *nd_compute_bound(ND p)
                   6299: {
                   6300:     UINT *d1,*d2,*t;
                   6301:     UINT u;
                   6302:     int i,j,k,l,len,ind;
                   6303:     NM m;
                   6304:
                   6305:     if ( !p )
                   6306:         return 0;
                   6307:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6308:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   6309:     len = LEN(p);
                   6310:     m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   6311:     for ( m = NEXT(m); m; m = NEXT(m) ) {
                   6312:         ndl_lcm(DL(m),d1,d2);
                   6313:         t = d1; d1 = d2; d2 = t;
                   6314:     }
                   6315:     l = nd_nvar+31;
                   6316:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   6317:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   6318:         u = d1[i];
                   6319:         k = (nd_epw-1)*nd_bpe;
                   6320:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   6321:             t[ind] = (u>>k)&nd_mask0;
                   6322:     }
                   6323:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   6324:     return t;
                   6325: }
                   6326:
                   6327: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
                   6328: /* of a term. In this case we need additional 1 word. */
                   6329:
                   6330: int nd_get_exporigin(struct order_spec *ord)
                   6331: {
                   6332:     switch ( ord->id ) {
1.21      noro     6333:         case 0: case 2: case 256: case 258: case 300:
1.1       noro     6334:             return 1+nd_module;
                   6335:         case 1: case 257:
                   6336:             /* block order */
                   6337:             /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
                   6338:             /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
                   6339:             return ord->ord.block.length+1+nd_module;
                   6340:         case 3: case 259:
                   6341: #if 0
                   6342:             error("nd_get_exporigin : composite order is not supported yet.");
                   6343: #else
                   6344:             return 1+nd_module;
                   6345: #endif
                   6346:         default:
                   6347:             error("nd_get_exporigin : ivalid argument.");
                   6348:             return 0;
                   6349:     }
                   6350: }
                   6351:
                   6352: void nd_setup_parameters(int nvar,int max) {
                   6353:     int i,j,n,elen,ord_o,ord_l,l,s,wpd;
                   6354:     struct order_pair *op;
1.45      noro     6355:     extern int CNVars;
1.1       noro     6356:
                   6357:     nd_nvar = nvar;
1.45      noro     6358:     CNVars = nvar;
1.1       noro     6359:     if ( max ) {
                   6360:         /* XXX */
                   6361:         if ( do_weyl ) nd_bpe = 32;
                   6362:         else if ( max < 2 ) nd_bpe = 1;
                   6363:         else if ( max < 4 ) nd_bpe = 2;
                   6364:         else if ( max < 8 ) nd_bpe = 3;
                   6365:         else if ( max < 16 ) nd_bpe = 4;
                   6366:         else if ( max < 32 ) nd_bpe = 5;
                   6367:         else if ( max < 64 ) nd_bpe = 6;
                   6368:         else if ( max < 256 ) nd_bpe = 8;
                   6369:         else if ( max < 1024 ) nd_bpe = 10;
                   6370:         else if ( max < 65536 ) nd_bpe = 16;
                   6371:         else nd_bpe = 32;
                   6372:     }
                   6373:     if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
                   6374:         UINT t;
                   6375:     int st;
                   6376:         int *v;
                   6377:   /* t = max(weights) */
                   6378:         t = 0;
                   6379:         if ( current_dl_weight_vector )
                   6380:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                   6381:                 if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
                   6382:                 if ( t < st ) t = st;
                   6383:             }
                   6384:         if ( nd_matrix )
                   6385:             for ( i = 0; i < nd_matrix_len; i++ )
                   6386:                 for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
                   6387:                     if ( (st=v[j]) < 0 ) st = -st;
                   6388:                     if ( t < st ) t = st;
                   6389:         }
                   6390:         /* i = bitsize of t */
                   6391:         for ( i = 0; t; t >>=1, i++ );
                   6392:         /* i += bitsize of nd_nvar */
                   6393:         for ( t = nd_nvar; t; t >>=1, i++);
                   6394:         /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
                   6395:         if ( (nd_bpe+i) >= 31 )
                   6396:             error("nd_setup_parameters : too large weight");
                   6397:     }
                   6398:     nd_epw = (sizeof(UINT)*8)/nd_bpe;
                   6399:     elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   6400:     nd_exporigin = nd_get_exporigin(nd_ord);
                   6401:     wpd = nd_exporigin+elen;
                   6402:     if ( nd_module )
                   6403:         nd_mpos = nd_exporigin-1;
                   6404:     else
                   6405:         nd_mpos = -1;
                   6406:     if ( wpd != nd_wpd ) {
                   6407:         nd_free_private_storage();
                   6408:         nd_wpd = wpd;
                   6409:     }
                   6410:     if ( nd_bpe < 32 ) {
                   6411:         nd_mask0 = (1<<nd_bpe)-1;
                   6412:     } else {
                   6413:         nd_mask0 = 0xffffffff;
                   6414:     }
                   6415:     bzero(nd_mask,sizeof(nd_mask));
                   6416:     nd_mask1 = 0;
                   6417:     for ( i = 0; i < nd_epw; i++ ) {
                   6418:         nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   6419:         nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   6420:     }
                   6421:     nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
                   6422:     nd_epos = nd_create_epos(nd_ord);
                   6423:     nd_blockmask = nd_create_blockmask(nd_ord);
                   6424:     nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
                   6425: }
                   6426:
                   6427: ND_pairs nd_reconstruct(int trace,ND_pairs d)
                   6428: {
                   6429:     int i,obpe,oadv,h;
                   6430:     static NM prev_nm_free_list;
                   6431:     static ND_pairs prev_ndp_free_list;
                   6432:     RHist mr0,mr;
                   6433:     RHist r;
                   6434:     RHist *old_red;
                   6435:     ND_pairs s0,s,t;
                   6436:     EPOS oepos;
                   6437:
                   6438:     obpe = nd_bpe;
                   6439:     oadv = nmv_adv;
                   6440:     oepos = nd_epos;
                   6441:     if ( obpe < 2 ) nd_bpe = 2;
                   6442:     else if ( obpe < 3 ) nd_bpe = 3;
                   6443:     else if ( obpe < 4 ) nd_bpe = 4;
                   6444:     else if ( obpe < 5 ) nd_bpe = 5;
                   6445:     else if ( obpe < 6 ) nd_bpe = 6;
                   6446:     else if ( obpe < 8 ) nd_bpe = 8;
                   6447:     else if ( obpe < 10 ) nd_bpe = 10;
                   6448:     else if ( obpe < 16 ) nd_bpe = 16;
                   6449:     else if ( obpe < 32 ) nd_bpe = 32;
                   6450:     else error("nd_reconstruct : exponent too large");
                   6451:
                   6452:     nd_setup_parameters(nd_nvar,0);
                   6453:     prev_nm_free_list = _nm_free_list;
                   6454:     prev_ndp_free_list = _ndp_free_list;
                   6455:     _nm_free_list = 0;
                   6456:     _ndp_free_list = 0;
                   6457:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   6458:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   6459:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   6460:     }
                   6461:     if ( trace )
                   6462:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   6463:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   6464:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   6465:         }
                   6466:     s0 = 0;
                   6467:     for ( t = d; t; t = NEXT(t) ) {
                   6468:         NEXTND_pairs(s0,s);
                   6469:         s->i1 = t->i1;
                   6470:         s->i2 = t->i2;
1.24      noro     6471:         s->sig = t->sig;
1.1       noro     6472:         SG(s) = SG(t);
                   6473:         ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   6474:     }
                   6475:
                   6476:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   6477:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   6478:         old_red[i] = nd_red[i];
                   6479:         nd_red[i] = 0;
                   6480:     }
                   6481:     for ( i = 0; i < REDTAB_LEN; i++ )
                   6482:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   6483:             NEWRHist(mr);
                   6484:             mr->index = r->index;
                   6485:             SG(mr) = SG(r);
                   6486:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   6487:             h = ndl_hash_value(DL(mr));
                   6488:             NEXT(mr) = nd_red[h];
                   6489:             nd_red[h] = mr;
1.24      noro     6490:             mr->sig = r->sig;
1.1       noro     6491:         }
                   6492:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   6493:     old_red = 0;
                   6494:     for ( i = 0; i < nd_psn; i++ ) {
                   6495:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   6496:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24      noro     6497:         r->sig = nd_psh[i]->sig;
1.1       noro     6498:         nd_psh[i] = r;
                   6499:     }
                   6500:     if ( s0 ) NEXT(s) = 0;
                   6501:     prev_nm_free_list = 0;
                   6502:     prev_ndp_free_list = 0;
                   6503: #if 0
                   6504:     GC_gcollect();
                   6505: #endif
                   6506:     return s0;
                   6507: }
                   6508:
1.30      noro     6509: void nd_reconstruct_s(int trace,ND_pairs *d)
                   6510: {
                   6511:     int i,obpe,oadv,h;
                   6512:     static NM prev_nm_free_list;
                   6513:     static ND_pairs prev_ndp_free_list;
                   6514:     RHist mr0,mr;
                   6515:     RHist r;
                   6516:     RHist *old_red;
                   6517:     ND_pairs s0,s,t;
                   6518:     EPOS oepos;
                   6519:
                   6520:     obpe = nd_bpe;
                   6521:     oadv = nmv_adv;
                   6522:     oepos = nd_epos;
                   6523:     if ( obpe < 2 ) nd_bpe = 2;
                   6524:     else if ( obpe < 3 ) nd_bpe = 3;
                   6525:     else if ( obpe < 4 ) nd_bpe = 4;
                   6526:     else if ( obpe < 5 ) nd_bpe = 5;
                   6527:     else if ( obpe < 6 ) nd_bpe = 6;
                   6528:     else if ( obpe < 8 ) nd_bpe = 8;
                   6529:     else if ( obpe < 10 ) nd_bpe = 10;
                   6530:     else if ( obpe < 16 ) nd_bpe = 16;
                   6531:     else if ( obpe < 32 ) nd_bpe = 32;
                   6532:     else error("nd_reconstruct_s : exponent too large");
                   6533:
                   6534:     nd_setup_parameters(nd_nvar,0);
                   6535:     prev_nm_free_list = _nm_free_list;
                   6536:     prev_ndp_free_list = _ndp_free_list;
                   6537:     _nm_free_list = 0;
                   6538:     _ndp_free_list = 0;
                   6539:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   6540:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   6541:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   6542:     }
                   6543:     if ( trace )
                   6544:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   6545:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   6546:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   6547:         }
                   6548:
                   6549:     for ( i = 0; i < nd_nbase; i++ ) {
                   6550:       s0 = 0;
                   6551:       for ( t = d[i]; t; t = NEXT(t) ) {
                   6552:           NEXTND_pairs(s0,s);
                   6553:           s->i1 = t->i1;
                   6554:           s->i2 = t->i2;
                   6555:           s->sig = t->sig;
                   6556:           SG(s) = SG(t);
                   6557:           ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   6558:       }
1.47      noro     6559:       if ( s0 ) NEXT(s) = 0;
1.30      noro     6560:       d[i] = s0;
                   6561:     }
                   6562:
                   6563:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   6564:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   6565:         old_red[i] = nd_red[i];
                   6566:         nd_red[i] = 0;
                   6567:     }
                   6568:     for ( i = 0; i < REDTAB_LEN; i++ )
                   6569:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   6570:             NEWRHist(mr);
                   6571:             mr->index = r->index;
                   6572:             SG(mr) = SG(r);
                   6573:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   6574:             h = ndl_hash_value(DL(mr));
                   6575:             NEXT(mr) = nd_red[h];
                   6576:             nd_red[h] = mr;
                   6577:             mr->sig = r->sig;
                   6578:         }
                   6579:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   6580:     old_red = 0;
                   6581:     for ( i = 0; i < nd_psn; i++ ) {
                   6582:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   6583:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
                   6584:         r->sig = nd_psh[i]->sig;
                   6585:         nd_psh[i] = r;
                   6586:     }
                   6587:     if ( s0 ) NEXT(s) = 0;
                   6588:     prev_nm_free_list = 0;
                   6589:     prev_ndp_free_list = 0;
                   6590: #if 0
                   6591:     GC_gcollect();
                   6592: #endif
                   6593: }
                   6594:
1.1       noro     6595: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
                   6596: {
                   6597:     int n,i,ei,oepw,omask0,j,s,ord_l,l;
                   6598:     struct order_pair *op;
                   6599:
                   6600:     n = nd_nvar;
                   6601:     oepw = (sizeof(UINT)*8)/obpe;
                   6602:     omask0 = (1<<obpe)-1;
                   6603:     TD(r) = TD(d);
                   6604:     for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
                   6605:     if ( nd_blockmask ) {
                   6606:         l = nd_blockmask->n;
                   6607:         op = nd_blockmask->order_pair;
                   6608:         for ( i = 1; i < nd_exporigin; i++ )
                   6609:             r[i] = d[i];
                   6610:         for ( j = 0, s = 0; j < l; j++ ) {
                   6611:             ord_l = op[j].length;
                   6612:             for ( i = 0; i < ord_l; i++, s++ ) {
                   6613:                 ei =  GET_EXP_OLD(d,s);
                   6614:                 PUT_EXP(r,s,ei);
                   6615:             }
                   6616:         }
                   6617:     } else {
                   6618:         for ( i = 0; i < n; i++ ) {
                   6619:             ei = GET_EXP_OLD(d,i);
                   6620:             PUT_EXP(r,i,ei);
                   6621:         }
                   6622:     }
                   6623:     if ( nd_module ) MPOS(r) = MPOS(d);
                   6624: }
                   6625:
                   6626: ND nd_copy(ND p)
                   6627: {
                   6628:     NM m,mr,mr0;
                   6629:     int c,n;
                   6630:     ND r;
                   6631:
                   6632:     if ( !p )
                   6633:         return 0;
                   6634:     else {
                   6635:         for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   6636:             NEXTNM(mr0,mr);
                   6637:             CM(mr) = CM(m);
                   6638:             ndl_copy(DL(m),DL(mr));
                   6639:         }
                   6640:         NEXT(mr) = 0;
                   6641:         MKND(NV(p),mr0,LEN(p),r);
                   6642:         SG(r) = SG(p);
                   6643:         return r;
                   6644:     }
                   6645: }
                   6646:
                   6647: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
                   6648: {
                   6649:     NM m1,m2;
                   6650:     NDV p1,p2;
                   6651:     ND t1,t2;
                   6652:     UINT *lcm;
                   6653:     P gp,tp;
                   6654:     Z g,t;
                   6655:     Z iq;
                   6656:     int td;
                   6657:     LIST hist;
                   6658:     NODE node;
                   6659:     DP d;
                   6660:
                   6661:     if ( !mod && nd_demand ) {
                   6662:         p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
                   6663:     } else {
                   6664:         if ( trace ) {
                   6665:             p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   6666:         } else {
                   6667:             p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   6668:         }
                   6669:     }
                   6670:     lcm = LCM(p);
                   6671:     NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
                   6672:     if ( ndl_check_bound2(p->i1,DL(m1)) ) {
                   6673:         FREENM(m1); return 0;
                   6674:     }
                   6675:     NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
                   6676:     if ( ndl_check_bound2(p->i2,DL(m2)) ) {
                   6677:         FREENM(m1); FREENM(m2); return 0;
                   6678:     }
                   6679:
                   6680:     if ( mod == -1 ) {
                   6681:         CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
                   6682:     } else if ( mod > 0 ) {
                   6683:         CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
                   6684:     } else if ( mod == -2 ) {
                   6685:         CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
                   6686:     } else if ( nd_vc ) {
                   6687:         ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
                   6688:         divsp(nd_vc,HCP(p2),gp,&CP(m1));
                   6689:         divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
                   6690:     } else {
1.6       noro     6691:         igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1       noro     6692:     }
                   6693:     t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
                   6694:     *rp = nd_add(mod,t1,t2);
                   6695:     if ( nd_gentrace ) {
                   6696:         /* nd_tracelist is initialized */
1.6       noro     6697:         STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     6698:         MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6       noro     6699:         STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     6700:         MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
                   6701:         nd_tracelist = node;
                   6702:     }
1.24      noro     6703:     if ( *rp )
                   6704:       (*rp)->sig = p->sig;
1.1       noro     6705:     FREENM(m1); FREENM(m2);
                   6706:     return 1;
                   6707: }
                   6708:
                   6709: void ndv_mul_c(int mod,NDV p,int mul)
                   6710: {
                   6711:     NMV m;
                   6712:     int c,c1,len,i;
                   6713:
                   6714:     if ( !p ) return;
                   6715:     len = LEN(p);
                   6716:     if ( mod == -1 )
                   6717:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   6718:             CM(m) = _mulsf(CM(m),mul);
                   6719:     else
                   6720:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6721:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   6722:         }
                   6723: }
                   6724:
                   6725: void ndv_mul_c_lf(NDV p,Z mul)
                   6726: {
                   6727:     NMV m;
                   6728:     Z c;
                   6729:     int len,i;
                   6730:
                   6731:     if ( !p ) return;
                   6732:     len = LEN(p);
                   6733:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6734:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   6735:     }
                   6736: }
                   6737:
                   6738: /* for nd_det */
                   6739: void ndv_mul_c_q(NDV p,Z mul)
                   6740: {
                   6741:     NMV m;
                   6742:     Z c;
                   6743:     int len,i;
                   6744:
                   6745:     if ( !p ) return;
                   6746:     len = LEN(p);
                   6747:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     6748:         mulz(CZ(m),mul,&c); CZ(m) = c;
1.1       noro     6749:     }
                   6750: }
                   6751:
                   6752: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
                   6753:     int n2,i,j,l,n,tlen;
                   6754:     UINT *d0;
                   6755:     NM *tab,*psum;
                   6756:     ND s,r;
                   6757:     NM t;
                   6758:     NMV m1;
                   6759:
                   6760:     if ( !p ) return 0;
                   6761:     n = NV(p); n2 = n>>1;
                   6762:     d0 = DL(m0);
                   6763:     l = LEN(p);
                   6764:     for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   6765:     tab = (NM *)MALLOC(tlen*sizeof(NM));
                   6766:     psum = (NM *)MALLOC(tlen*sizeof(NM));
                   6767:     for ( i = 0; i < tlen; i++ ) psum[i] = 0;
                   6768:     m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   6769:     for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
                   6770:         /* m0(NM) * m1(NMV) => tab(NM) */
                   6771:         weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
                   6772:         for ( j = 0; j < tlen; j++ ) {
                   6773:             if ( tab[j] ) {
                   6774:                 NEXT(tab[j]) = psum[j];    psum[j] = tab[j];
                   6775:             }
                   6776:         }
                   6777:     }
                   6778:     for ( i = tlen-1, r = 0; i >= 0; i-- )
                   6779:         if ( psum[i] ) {
                   6780:             for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   6781:             MKND(n,psum[i],j,s);
                   6782:             r = nd_add(mod,r,s);
                   6783:         }
                   6784:     if ( r ) SG(r) = SG(p)+TD(d0);
                   6785:     return r;
                   6786: }
                   6787:
                   6788: /* product of monomials */
                   6789: /* XXX block order is not handled correctly */
                   6790:
                   6791: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   6792: {
                   6793:     int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
                   6794:     UINT *d0,*d1,*d,*dt,*ctab;
                   6795:     Z *ctab_q;
                   6796:     Z q,q1;
                   6797:     UINT c0,c1,c;
                   6798:     NM *p;
                   6799:     NM m,t;
                   6800:     int mpos;
                   6801:
                   6802:     for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   6803:     if ( !m0 || !m1 ) return;
                   6804:     d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
                   6805:     if ( nd_module )
                   6806:         if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
                   6807:
                   6808:     NEWNM(m); d = DL(m);
                   6809:     if ( mod ) {
                   6810:         c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
                   6811:     } else if ( nd_vc )
                   6812:         mulp(nd_vc,CP(m0),CP(m1),&CP(m));
                   6813:   else
1.6       noro     6814:         mulz(CZ(m0),CZ(m1),&CZ(m));
1.1       noro     6815:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   6816:     homo = n&1 ? 1 : 0;
                   6817:     if ( homo ) {
                   6818:         /* offset of h-degree */
                   6819:         h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   6820:         PUT_EXP(DL(m),n-1,h);
                   6821:         TD(DL(m)) = h;
                   6822:         if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   6823:     }
                   6824:     tab[0] = m;
                   6825:     NEWNM(m); d = DL(m);
                   6826:     for ( i = 0, curlen = 1; i < n2; i++ ) {
                   6827:         a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   6828:         k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   6829:         /* xi^a*(Di^k*xi^l)*Di^b */
                   6830:         a += l; b += k;
                   6831:         s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
                   6832:         if ( !k || !l ) {
                   6833:             for ( j = 0; j < curlen; j++ )
                   6834:                 if ( (t = tab[j]) != 0 ) {
                   6835:                     dt = DL(t);
                   6836:                     PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
                   6837:                     if ( nd_blockmask ) ndl_weight_mask(dt);
                   6838:                 }
                   6839:             curlen *= k+1;
                   6840:             continue;
                   6841:         }
                   6842:         min = MIN(k,l);
                   6843:         if ( mod ) {
                   6844:             ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
                   6845:             mkwcm(k,l,mod,(int *)ctab);
                   6846:         } else {
                   6847:             ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
                   6848:             mkwc(k,l,ctab_q);
                   6849:         }
                   6850:         for ( j = min; j >= 0; j-- ) {
                   6851:             for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
                   6852:             PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
                   6853:             h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
                   6854:             if ( homo ) {
                   6855:                 TD(d) = s;
                   6856:                 PUT_EXP(d,n-1,s-h);
                   6857:             } else TD(d) = h;
                   6858:             if ( nd_blockmask ) ndl_weight_mask(d);
                   6859:             if ( mod ) c = ctab[j];
                   6860:             else q = ctab_q[j];
                   6861:             p = tab+curlen*j;
                   6862:             if ( j == 0 ) {
                   6863:                 for ( u = 0; u < curlen; u++, p++ ) {
                   6864:                     if ( tab[u] ) {
                   6865:                         ndl_addto(DL(tab[u]),d);
                   6866:                         if ( mod ) {
                   6867:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
                   6868:                         } else if ( nd_vc )
                   6869:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
                   6870:             else {
1.6       noro     6871:                             mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1       noro     6872:                         }
                   6873:                     }
                   6874:                 }
                   6875:             } else {
                   6876:                 for ( u = 0; u < curlen; u++, p++ ) {
                   6877:                     if ( tab[u] ) {
                   6878:                         NEWNM(t);
                   6879:                         ndl_add(DL(tab[u]),d,DL(t));
                   6880:                         if ( mod ) {
                   6881:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
                   6882:                         } else if ( nd_vc )
                   6883:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
                   6884:             else
1.6       noro     6885:                             mulz(CZ(tab[u]),q,&CZ(t));
1.1       noro     6886:                         *p = t;
                   6887:                     }
                   6888:                 }
                   6889:             }
                   6890:         }
                   6891:         curlen *= k+1;
                   6892:     }
                   6893:     FREENM(m);
                   6894:     if ( nd_module ) {
                   6895:         mpos = MPOS(d1);
                   6896:         for ( i = 0; i < tlen; i++ )
                   6897:             if ( tab[i] ) {
                   6898:                 d = DL(tab[i]);
                   6899:                 MPOS(d) = mpos;
                   6900:                 TD(d) = ndl_weight(d);
                   6901:             }
                   6902:     }
                   6903: }
                   6904:
                   6905: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   6906: {
                   6907:     NM mr,mr0;
                   6908:     NMV m;
                   6909:     UINT *d,*dt,*dm;
                   6910:     int c,n,td,i,c1,c2,len;
                   6911:     Q q;
                   6912:     ND r;
                   6913:
                   6914:     if ( !p ) return 0;
                   6915:     else {
                   6916:         n = NV(p); m = BDY(p);
                   6917:         d = DL(m0);
                   6918:         len = LEN(p);
                   6919:         mr0 = 0;
                   6920:         td = TD(d);
                   6921:         c = CM(m0);
                   6922:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6923:             NEXTNM(mr0,mr);
                   6924:             CM(mr) = 1;
                   6925:             ndl_add(DL(m),d,DL(mr));
                   6926:         }
                   6927:         NEXT(mr) = 0;
                   6928:         MKND(NV(p),mr0,len,r);
                   6929:         SG(r) = SG(p) + TD(d);
                   6930:         return r;
                   6931:     }
                   6932: }
                   6933:
                   6934: ND ndv_mul_nm(int mod,NM m0,NDV p)
                   6935: {
                   6936:     NM mr,mr0;
                   6937:     NMV m;
                   6938:     UINT *d,*dt,*dm;
                   6939:     int c,n,td,i,c1,c2,len;
                   6940:     P q;
                   6941:     ND r;
                   6942:
                   6943:     if ( !p ) return 0;
                   6944:     else if ( do_weyl ) {
                   6945:         if ( mod < 0 ) {
                   6946:             error("ndv_mul_nm : not implemented (weyl)");
                   6947:             return 0;
                   6948:         } else
                   6949:             return weyl_ndv_mul_nm(mod,m0,p);
                   6950:     } else {
                   6951:         n = NV(p); m = BDY(p);
                   6952:         d = DL(m0);
                   6953:         len = LEN(p);
                   6954:         mr0 = 0;
                   6955:         td = TD(d);
                   6956:         if ( mod == -1 ) {
                   6957:             c = CM(m0);
                   6958:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6959:                 NEXTNM(mr0,mr);
                   6960:                 CM(mr) = _mulsf(CM(m),c);
                   6961:                 ndl_add(DL(m),d,DL(mr));
                   6962:             }
                   6963:         } else if ( mod == -2 ) {
                   6964:             Z cl;
                   6965:             cl = CZ(m0);
                   6966:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6967:                 NEXTNM(mr0,mr);
                   6968:                 mullf(CZ(m),cl,&CZ(mr));
                   6969:                 ndl_add(DL(m),d,DL(mr));
                   6970:             }
                   6971:         } else if ( mod ) {
                   6972:             c = CM(m0);
                   6973:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6974:                 NEXTNM(mr0,mr);
                   6975:                 c1 = CM(m);
                   6976:                 DMAR(c1,c,0,mod,c2);
                   6977:                 CM(mr) = c2;
                   6978:                 ndl_add(DL(m),d,DL(mr));
                   6979:             }
                   6980:         } else {
                   6981:             q = CP(m0);
                   6982:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6983:                 NEXTNM(mr0,mr);
                   6984:                 mulp(nd_vc,CP(m),q,&CP(mr));
                   6985:                 ndl_add(DL(m),d,DL(mr));
                   6986:             }
                   6987:         }
                   6988:         NEXT(mr) = 0;
                   6989:         MKND(NV(p),mr0,len,r);
                   6990:         SG(r) = SG(p) + TD(d);
                   6991:         return r;
                   6992:     }
                   6993: }
                   6994:
                   6995: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
                   6996: {
                   6997:     NM mq0,mq;
                   6998:     NMV tm;
                   6999:     Q q;
                   7000:     int i,nv,sg,c,c1,c2,hindex;
                   7001:     ND p,t,r;
                   7002:
                   7003:     if ( bucket->m < 0 ) return 0;
                   7004:     else {
                   7005:         nv = NV(d);
                   7006:         mq0 = 0;
                   7007:         tm = (NMV)MALLOC(nmv_adv);
                   7008:         while ( 1 ) {
                   7009:             if ( mod > 0 || mod == -1 )
                   7010:               hindex = head_pbucket(mod,bucket);
                   7011:             else if ( mod == -2 )
                   7012:               hindex = head_pbucket_lf(bucket);
                   7013:             else
                   7014:               hindex = head_pbucket_q(bucket);
                   7015:             if ( hindex < 0 ) break;
                   7016:             p = bucket->body[hindex];
                   7017:             NEXTNM(mq0,mq);
                   7018:             ndl_sub(HDL(p),HDL(d),DL(mq));
                   7019:             ndl_copy(DL(mq),DL(tm));
                   7020:             if ( mod ) {
                   7021:                 c1 = invm(HCM(d),mod); c2 = HCM(p);
                   7022:                 DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   7023:                 CM(tm) = mod-c;
                   7024:             } else {
1.6       noro     7025:                 divsz(HCZ(p),HCZ(d),&CZ(mq));
                   7026:                 chsgnz(CZ(mq),&CZ(tm));
1.1       noro     7027:             }
                   7028:             t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
                   7029:             bucket->body[hindex] = nd_remove_head(p);
                   7030:             t = nd_remove_head(t);
                   7031:             add_pbucket(mod,bucket,t);
                   7032:         }
                   7033:         if ( !mq0 )
                   7034:             r = 0;
                   7035:         else {
                   7036:             NEXT(mq) = 0;
                   7037:             for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   7038:             MKND(nv,mq0,i,r);
                   7039:             /* XXX */
                   7040:             SG(r) = HTD(r);
                   7041:         }
                   7042:         return r;
                   7043:     }
                   7044: }
                   7045:
                   7046: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   7047: {
                   7048:     NMV m,mr,mr0,t;
                   7049:     int len,i,k;
                   7050:
                   7051:     if ( !p ) return;
                   7052:     m = BDY(p); len = LEN(p);
                   7053:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   7054:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   7055:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   7056:     t = (NMV)MALLOC(nmv_adv);
                   7057:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6       noro     7058:         CZ(t) = CZ(m);
1.1       noro     7059:         for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   7060:         ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6       noro     7061:         CZ(mr) = CZ(t);
1.1       noro     7062:         ndl_copy(DL(t),DL(mr));
                   7063:     }
                   7064:     BDY(p) = mr0;
                   7065: }
                   7066:
                   7067: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   7068: {
                   7069:     NMV m,mr,mr0;
                   7070:     int len,i;
                   7071:     NDV r;
                   7072:
                   7073:     if ( !p ) return 0;
                   7074:     m = BDY(p); len = LEN(p);
                   7075:     mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   7076:     for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   7077:         ndl_zero(DL(mr));
                   7078:         ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6       noro     7079:         CZ(mr) = CZ(m);
1.1       noro     7080:     }
                   7081:     MKNDV(NV(p),mr0,len,r);
                   7082:     SG(r) = SG(p);
1.24      noro     7083:     r->sig = p->sig;
1.1       noro     7084:     return r;
                   7085: }
                   7086:
                   7087: /* duplicate p */
                   7088:
                   7089: NDV ndv_dup(int mod,NDV p)
                   7090: {
                   7091:     NDV d;
                   7092:     NMV t,m,m0;
                   7093:     int i,len;
                   7094:
                   7095:     if ( !p ) return 0;
                   7096:     len = LEN(p);
                   7097:     m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   7098:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   7099:         ndl_copy(DL(t),DL(m));
1.6       noro     7100:         CZ(m) = CZ(t);
1.1       noro     7101:     }
                   7102:     MKNDV(NV(p),m0,len,d);
                   7103:     SG(d) = SG(p);
                   7104:     return d;
                   7105: }
                   7106:
                   7107: NDV ndv_symbolic(int mod,NDV p)
                   7108: {
                   7109:     NDV d;
                   7110:     NMV t,m,m0;
                   7111:     int i,len;
                   7112:
                   7113:     if ( !p ) return 0;
                   7114:     len = LEN(p);
                   7115:     m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   7116:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   7117:         ndl_copy(DL(t),DL(m));
1.6       noro     7118:         CZ(m) = ONE;
1.1       noro     7119:     }
                   7120:     MKNDV(NV(p),m0,len,d);
                   7121:     SG(d) = SG(p);
                   7122:     return d;
                   7123: }
                   7124:
                   7125: ND nd_dup(ND p)
                   7126: {
                   7127:     ND d;
                   7128:     NM t,m,m0;
                   7129:
                   7130:     if ( !p ) return 0;
                   7131:     for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   7132:         NEXTNM(m0,m);
                   7133:         ndl_copy(DL(t),DL(m));
1.6       noro     7134:         CZ(m) = CZ(t);
1.1       noro     7135:     }
                   7136:     if ( m0 ) NEXT(m) = 0;
                   7137:     MKND(NV(p),m0,LEN(p),d);
                   7138:     SG(d) = SG(p);
                   7139:     return d;
                   7140: }
                   7141:
                   7142: /* XXX if p->len == 0 then it represents 0 */
                   7143:
                   7144: void ndv_mod(int mod,NDV p)
                   7145: {
                   7146:     NMV t,d;
                   7147:     int r,s,u;
                   7148:     int i,len,dlen;
                   7149:     P cp;
                   7150:     Z c;
                   7151:     Obj gfs;
                   7152:
                   7153:     if ( !p ) return;
                   7154:     len = LEN(p);
                   7155:     dlen = 0;
                   7156:     if ( mod == -1 )
                   7157:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   7158:             simp_ff((Obj)CP(t),&gfs);
                   7159:             if ( gfs ) {
                   7160:               r = FTOIF(CONT((GFS)gfs));
                   7161:               CM(d) = r;
                   7162:               ndl_copy(DL(t),DL(d));
                   7163:               NMV_ADV(d);
                   7164:               dlen++;
                   7165:             }
                   7166:         }
                   7167:     else if ( mod == -2 )
                   7168:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   7169:             simp_ff((Obj)CP(t),&gfs);
                   7170:             if ( gfs ) {
                   7171:               lmtolf((LM)gfs,&CZ(d));
                   7172:               ndl_copy(DL(t),DL(d));
                   7173:               NMV_ADV(d);
                   7174:               dlen++;
                   7175:             }
                   7176:         }
                   7177:     else
                   7178:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   7179:             if ( nd_vc ) {
                   7180:                 nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
                   7181:                 c = (Z)cp;
                   7182:             } else
1.6       noro     7183:                 c = CZ(t);
1.1       noro     7184:             r = remqi((Q)c,mod);
                   7185:             if ( r ) {
                   7186:                 CM(d) = r;
                   7187:                 ndl_copy(DL(t),DL(d));
                   7188:                 NMV_ADV(d);
                   7189:                 dlen++;
                   7190:             }
                   7191:         }
                   7192:     LEN(p) = dlen;
                   7193: }
                   7194:
                   7195: NDV ptondv(VL vl,VL dvl,P p)
                   7196: {
                   7197:     ND nd;
                   7198:
                   7199:     nd = ptond(vl,dvl,p);
                   7200:     return ndtondv(0,nd);
                   7201: }
                   7202:
                   7203: void pltozpl(LIST l,Q *cont,LIST *pp)
                   7204: {
1.16      noro     7205:   NODE nd,nd1;
                   7206:   int n;
                   7207:   P *pl;
                   7208:   Q *cl;
                   7209:   int i;
                   7210:   P dmy;
                   7211:   Z dvr,inv;
                   7212:   LIST r;
                   7213:
                   7214:   nd = BDY(l); n = length(nd);
                   7215:   pl = (P *)MALLOC(n*sizeof(P));
                   7216:   cl = (Q *)MALLOC(n*sizeof(Q));
                   7217:   for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
                   7218:     ptozp((P)BDY(nd),1,&cl[i],&dmy);
                   7219:   }
                   7220:   qltozl(cl,n,&dvr);
                   7221:   divz(ONE,dvr,&inv);
                   7222:   nd = BDY(l);
                   7223:   for ( i = 0; i < n; i++, nd = NEXT(nd) )
                   7224:     divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
                   7225:   nd = 0;
                   7226:   for ( i = n-1; i >= 0; i-- ) {
                   7227:     MKNODE(nd1,pl[i],nd); nd = nd1;
                   7228:   }
                   7229:   MKLIST(r,nd);
                   7230:   *pp = r;
1.1       noro     7231: }
                   7232:
                   7233: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
                   7234:
                   7235: NDV pltondv(VL vl,VL dvl,LIST p)
                   7236: {
                   7237:     int i;
                   7238:     NODE t;
                   7239:     ND r,ri;
                   7240:     NM m;
                   7241:
                   7242:     if ( !nd_module ) error("pltond : module order must be set");
                   7243:     r = 0;
                   7244:     for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
                   7245:         ri = ptond(vl,dvl,(P)BDY(t));
                   7246:         if ( ri )
                   7247:             for ( m = BDY(ri); m; m = NEXT(m) ) {
                   7248:                 MPOS(DL(m)) = i;
                   7249:                 TD(DL(m)) = ndl_weight(DL(m));
                   7250:                 if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   7251:             }
                   7252:         r = nd_add(0,r,ri);
                   7253:     }
                   7254:     return ndtondv(0,r);
                   7255: }
                   7256:
                   7257: ND ptond(VL vl,VL dvl,P p)
                   7258: {
                   7259:     int n,i,j,k,e;
                   7260:     VL tvl;
                   7261:     V v;
                   7262:     DCP dc;
                   7263:     DCP *w;
                   7264:     ND r,s,t,u;
                   7265:     P x;
                   7266:     int c;
                   7267:     UINT *d;
                   7268:     NM m,m0;
                   7269:
                   7270:     if ( !p )
                   7271:         return 0;
                   7272:     else if ( NUM(p) ) {
                   7273:         NEWNM(m);
                   7274:         ndl_zero(DL(m));
1.48      noro     7275:         if ( RATN(p) && !INT((Q)p) )
1.1       noro     7276:           error("ptond : input must be integer-coefficient");
1.6       noro     7277:         CZ(m) = (Z)p;
1.1       noro     7278:         NEXT(m) = 0;
                   7279:         MKND(nd_nvar,m,1,r);
                   7280:         SG(r) = 0;
                   7281:         return r;
                   7282:     } else {
                   7283:         for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   7284:         w = (DCP *)MALLOC(k*sizeof(DCP));
                   7285:         for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   7286:         for ( i = 0, tvl = dvl, v = VR(p);
                   7287:             tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   7288:         if ( !tvl ) {
                   7289:             for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   7290:                 t = ptond(vl,dvl,COEF(w[j]));
                   7291:                 pwrp(vl,x,DEG(w[j]),&p);
                   7292:                 nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   7293:             }
                   7294:             return s;
                   7295:         } else {
                   7296:             NEWNM(m0); d = DL(m0);
                   7297:             for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6       noro     7298:                 ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1       noro     7299:                 TD(d) = MUL_WEIGHT(e,i);
                   7300:                 if ( nd_blockmask) ndl_weight_mask(d);
                   7301:                 if ( nd_module ) MPOS(d) = 0;
                   7302:                 t = ptond(vl,dvl,COEF(w[j]));
                   7303:                 for ( m = BDY(t); m; m = NEXT(m) )
                   7304:                     ndl_addto(DL(m),d);
                   7305:                 SG(t) += TD(d);
                   7306:                 s = nd_add(0,s,t);
                   7307:             }
                   7308:             FREENM(m0);
                   7309:             return s;
                   7310:         }
                   7311:     }
                   7312: }
                   7313:
                   7314: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   7315: {
                   7316:     VL tvl;
                   7317:     int len,n,j,i,e;
                   7318:     NMV m;
                   7319:     Z q;
                   7320:     P c;
                   7321:     UINT *d;
                   7322:     P s,r,u,t,w;
                   7323:     GFS gfs;
                   7324:
                   7325:     if ( !p ) return 0;
                   7326:     else {
                   7327:         len = LEN(p);
                   7328:         n = NV(p);
                   7329:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   7330:         for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   7331:             if ( mod == -1 ) {
                   7332:                e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   7333:             } else if ( mod == -2 ) {
                   7334:                c = (P)CZ(m);
                   7335:             } else if ( mod > 0 ) {
1.6       noro     7336:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     7337:             } else
                   7338:                 c = CP(m);
                   7339:             d = DL(m);
                   7340:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     7341:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     7342:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   7343:             }
                   7344:             addp(vl,s,t,&u); s = u;
                   7345:         }
                   7346:         return s;
                   7347:     }
                   7348: }
                   7349:
                   7350: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
                   7351: {
                   7352:     VL tvl;
                   7353:     int len,n,j,i,e;
                   7354:     NMV m;
                   7355:     Z q;
                   7356:     P c;
                   7357:     UINT *d;
                   7358:     P s,r,u,t,w;
                   7359:     GFS gfs;
                   7360:     P *a;
                   7361:     LIST l;
                   7362:     NODE nd,nd1;
                   7363:
                   7364:     if ( !p ) return 0;
                   7365:     else {
                   7366:         a = (P *)MALLOC((rank+1)*sizeof(P));
                   7367:         for ( i = 0; i <= rank; i++ ) a[i] = 0;
                   7368:         len = LEN(p);
                   7369:         n = NV(p);
                   7370:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   7371:         for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
                   7372:             if ( mod == -1 ) {
                   7373:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   7374:             } else if ( mod ) {
1.6       noro     7375:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     7376:             } else
                   7377:                 c = CP(m);
                   7378:             d = DL(m);
                   7379:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     7380:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     7381:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   7382:             }
                   7383:             addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
                   7384:         }
                   7385:         nd = 0;
                   7386:         for ( i = rank; i > 0; i-- ) {
                   7387:             MKNODE(nd1,a[i],nd); nd = nd1;
                   7388:         }
                   7389:         MKLIST(l,nd);
                   7390:         return l;
                   7391:     }
                   7392: }
                   7393:
                   7394: NDV ndtondv(int mod,ND p)
                   7395: {
                   7396:     NDV d;
                   7397:     NMV m,m0;
                   7398:     NM t;
                   7399:     int i,len;
                   7400:
                   7401:     if ( !p ) return 0;
                   7402:     len = LEN(p);
                   7403:     if ( mod > 0 || mod == -1 )
                   7404:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   7405:     else
                   7406:         m0 = m = MALLOC(len*nmv_adv);
                   7407: #if 0
                   7408:     ndv_alloc += nmv_adv*len;
                   7409: #endif
                   7410:     for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   7411:         ndl_copy(DL(t),DL(m));
1.6       noro     7412:         CZ(m) = CZ(t);
1.1       noro     7413:     }
                   7414:     MKNDV(NV(p),m0,len,d);
                   7415:     SG(d) = SG(p);
1.24      noro     7416:     d->sig = p->sig;
1.1       noro     7417:     return d;
                   7418: }
                   7419:
1.16      noro     7420: static int dmm_comp_nv;
                   7421:
                   7422: int dmm_comp(DMM *a,DMM *b)
                   7423: {
                   7424:    return -compdmm(dmm_comp_nv,*a,*b);
                   7425: }
                   7426:
                   7427: void dmm_sort_by_ord(DMM *a,int len,int nv)
                   7428: {
                   7429:   dmm_comp_nv = nv;
                   7430:   qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
                   7431: }
                   7432:
                   7433: void dpm_sort(DPM p,DPM *rp)
                   7434: {
                   7435:   DMM t,t1;
                   7436:   int len,i,n;
                   7437:   DMM *a;
                   7438:   DPM d;
                   7439:
                   7440:   if ( !p ) *rp = 0;
                   7441:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   7442:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   7443:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   7444:   n = p->nv;
                   7445:   dmm_sort_by_ord(a,len,n);
                   7446:   t = 0;
                   7447:   for ( i = len-1; i >= 0; i-- ) {
                   7448:     NEWDMM(t1);
                   7449:     t1->c = a[i]->c;
                   7450:     t1->dl = a[i]->dl;
                   7451:     t1->pos = a[i]->pos;
                   7452:     t1->next = t;
                   7453:     t = t1;
                   7454:   }
                   7455:   MKDPM(n,t,d);
                   7456:   SG(d) = SG(p);
                   7457:   *rp = d;
                   7458: }
                   7459:
1.18      noro     7460: int dpm_comp(DPM *a,DPM *b)
                   7461: {
1.22      noro     7462:   return -compdpm(CO,*a,*b);
1.18      noro     7463: }
                   7464:
                   7465: NODE dpm_sort_list(NODE l)
                   7466: {
                   7467:   int i,len;
                   7468:   NODE t,t1;
                   7469:   DPM *a;
                   7470:
                   7471:   len = length(l);
                   7472:   a = (DPM *)MALLOC(len*sizeof(DPM));
                   7473:   for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
                   7474:   qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
                   7475:   t = 0;
                   7476:   for ( i = len-1; i >= 0; i-- ) {
                   7477:     MKNODE(t1,(pointer)a[i],t); t = t1;
                   7478:   }
                   7479:   return t;
                   7480: }
                   7481:
1.20      noro     7482: int nmv_comp(NMV a,NMV b)
                   7483: {
1.21      noro     7484:   int t;
                   7485:   t = DL_COMPARE(a->dl,b->dl);
                   7486:   return -t;
1.20      noro     7487: }
                   7488:
1.43      noro     7489: NDV dptondv(int mod,DP p)
                   7490: {
                   7491:   NDV d;
                   7492:   NMV m,m0;
                   7493:   MP t;
                   7494:   MP *a;
                   7495:   int i,len,n;
                   7496:
                   7497:   if ( !p ) return 0;
                   7498:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   7499:   n = p->nv;
                   7500:   if ( mod > 0 || mod == -1 )
                   7501:     m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   7502:   else
                   7503:     m0 = m = MALLOC(len*nmv_adv);
                   7504:   for ( i = 0, t = BDY(p); i < len; i++, NMV_ADV(m), t = NEXT(t) ) {
                   7505:     dltondl(n,t->dl,DL(m));
                   7506:     TD(DL(m)) = ndl_weight(DL(m));
                   7507:     CZ(m) = (Z)t->c;
                   7508:   }
                   7509:   qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
                   7510:   MKNDV(NV(p),m0,len,d);
                   7511:   SG(d) = SG(p);
                   7512:   return d;
                   7513: }
                   7514:
1.16      noro     7515: NDV dpmtondv(int mod,DPM p)
                   7516: {
                   7517:   NDV d;
                   7518:   NMV m,m0;
                   7519:   DMM t;
                   7520:   DMM *a;
                   7521:   int i,len,n;
                   7522:
                   7523:   if ( !p ) return 0;
                   7524:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   7525:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   7526:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   7527:   n = p->nv;
                   7528:   dmm_sort_by_ord(a,len,n);
                   7529:   if ( mod > 0 || mod == -1 )
                   7530:     m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   7531:   else
                   7532:     m0 = m = MALLOC(len*nmv_adv);
                   7533: #if 0
                   7534:   ndv_alloc += nmv_adv*len;
                   7535: #endif
                   7536:   for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   7537:     dltondl(n,a[i]->dl,DL(m));
                   7538:     MPOS(DL(m)) = a[i]->pos;
1.20      noro     7539:     TD(DL(m)) = ndl_weight(DL(m));
1.16      noro     7540:     CZ(m) = (Z)a[i]->c;
                   7541:   }
1.20      noro     7542:   qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16      noro     7543:   MKNDV(NV(p),m0,len,d);
                   7544:   SG(d) = SG(p);
                   7545:   return d;
                   7546: }
                   7547:
1.1       noro     7548: ND ndvtond(int mod,NDV p)
                   7549: {
                   7550:     ND d;
                   7551:     NM m,m0;
                   7552:     NMV t;
                   7553:     int i,len;
                   7554:
                   7555:     if ( !p ) return 0;
                   7556:     m0 = 0;
                   7557:     len = p->len;
                   7558:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   7559:         NEXTNM(m0,m);
                   7560:         ndl_copy(DL(t),DL(m));
1.6       noro     7561:         CZ(m) = CZ(t);
1.1       noro     7562:     }
                   7563:     NEXT(m) = 0;
                   7564:     MKND(NV(p),m0,len,d);
                   7565:     SG(d) = SG(p);
1.28      noro     7566:     d->sig = p->sig;
1.1       noro     7567:     return d;
                   7568: }
                   7569:
                   7570: DP ndvtodp(int mod,NDV p)
                   7571: {
                   7572:     MP m,m0;
                   7573:   DP d;
                   7574:     NMV t;
                   7575:     int i,len;
                   7576:
                   7577:     if ( !p ) return 0;
                   7578:     m0 = 0;
                   7579:     len = p->len;
                   7580:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   7581:         NEXTMP(m0,m);
                   7582:       m->dl = ndltodl(nd_nvar,DL(t));
                   7583:       m->c = (Obj)ndctop(mod,t->c);
                   7584:     }
                   7585:     NEXT(m) = 0;
                   7586:   MKDP(nd_nvar,m0,d);
                   7587:     SG(d) = SG(p);
                   7588:     return d;
                   7589: }
                   7590:
1.44      noro     7591: DPM sigtodpm(SIG s)
                   7592: {
                   7593:   DMM m;
                   7594:   DPM d;
                   7595:
                   7596:   NEWDMM(m);
                   7597:   m->c = (Obj)ONE;
                   7598:   m->dl = s->dl;
                   7599:   m->pos = s->pos+1;
                   7600:   m->next = 0;
                   7601:   MKDPM(nd_nvar,m,d);
                   7602:   SG(d) = s->dl->td;
                   7603:   return d;
                   7604: }
                   7605:
1.16      noro     7606: DPM ndvtodpm(int mod,NDV p)
                   7607: {
                   7608:   DMM m,m0;
                   7609:   DPM d;
                   7610:   NMV t;
                   7611:   int i,len;
                   7612:
                   7613:   if ( !p ) return 0;
                   7614:   m0 = 0;
                   7615:   len = p->len;
                   7616:   for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   7617:     NEXTDMM(m0,m);
                   7618:     m->dl = ndltodl(nd_nvar,DL(t));
                   7619:     m->c = (Obj)ndctop(mod,t->c);
                   7620:     m->pos = MPOS(DL(t));
                   7621:   }
                   7622:   NEXT(m) = 0;
                   7623:   MKDPM(nd_nvar,m0,d);
                   7624:   SG(d) = SG(p);
                   7625:   return d;
                   7626: }
                   7627:
                   7628:
1.1       noro     7629: DP ndtodp(int mod,ND p)
                   7630: {
                   7631:     MP m,m0;
                   7632:   DP d;
                   7633:     NM t;
                   7634:     int i,len;
                   7635:
                   7636:     if ( !p ) return 0;
                   7637:     m0 = 0;
                   7638:     len = p->len;
                   7639:     for ( t = BDY(p); t; t = NEXT(t) ) {
                   7640:         NEXTMP(m0,m);
                   7641:       m->dl = ndltodl(nd_nvar,DL(t));
                   7642:       m->c = (Obj)ndctop(mod,t->c);
                   7643:     }
                   7644:     NEXT(m) = 0;
                   7645:   MKDP(nd_nvar,m0,d);
                   7646:     SG(d) = SG(p);
                   7647:     return d;
                   7648: }
                   7649:
                   7650: void ndv_print(NDV p)
                   7651: {
                   7652:     NMV m;
                   7653:     int i,len;
                   7654:
                   7655:     if ( !p ) printf("0\n");
                   7656:     else {
                   7657:         len = LEN(p);
                   7658:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   7659:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     7660:             else printf("+%ld*",CM(m));
1.1       noro     7661:             ndl_print(DL(m));
                   7662:         }
                   7663:         printf("\n");
                   7664:     }
                   7665: }
                   7666:
                   7667: void ndv_print_q(NDV p)
                   7668: {
                   7669:     NMV m;
                   7670:     int i,len;
                   7671:
                   7672:     if ( !p ) printf("0\n");
                   7673:     else {
                   7674:         len = LEN(p);
                   7675:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   7676:             printf("+");
1.6       noro     7677:             printexpr(CO,(Obj)CZ(m));
1.1       noro     7678:             printf("*");
                   7679:             ndl_print(DL(m));
                   7680:         }
                   7681:         printf("\n");
                   7682:     }
                   7683: }
                   7684:
                   7685: NODE ndv_reducebase(NODE x,int *perm)
                   7686: {
                   7687:     int len,i,j;
                   7688:     NDVI w;
                   7689:     NODE t,t0;
                   7690:
1.41      noro     7691:     if ( nd_norb ) return x;
1.1       noro     7692:     len = length(x);
                   7693:     w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
                   7694:     for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
                   7695:         w[i].p = BDY(t); w[i].i = perm[i];
                   7696:     }
                   7697:     for ( i = 0; i < len; i++ ) {
                   7698:         for ( j = 0; j < i; j++ ) {
                   7699:             if ( w[i].p && w[j].p ) {
                   7700:                 if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
                   7701:                 else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
                   7702:             }
                   7703:         }
                   7704:     }
                   7705:     for ( i = j = 0, t0 = 0; i < len; i++ ) {
                   7706:         if ( w[i].p ) {
                   7707:             NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
                   7708:             perm[j++] = w[i].i;
                   7709:         }
                   7710:     }
                   7711:     NEXT(t) = 0; x = t0;
                   7712:     return x;
                   7713: }
                   7714:
                   7715: /* XXX incomplete */
                   7716:
1.21      noro     7717: extern DMMstack dmm_stack;
                   7718: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16      noro     7719:
1.1       noro     7720: void nd_init_ord(struct order_spec *ord)
                   7721: {
                   7722:   nd_module = (ord->id >= 256);
                   7723:   if ( nd_module ) {
                   7724:     nd_dcomp = -1;
1.21      noro     7725:     nd_module_ordtype = ord->module_ordtype;
1.1       noro     7726:     nd_pot_nelim = ord->pot_nelim;
                   7727:     nd_poly_weight_len = ord->nv;
                   7728:     nd_poly_weight = ord->top_weight;
                   7729:     nd_module_rank = ord->module_rank;
                   7730:     nd_module_weight = ord->module_top_weight;
                   7731:   }
                   7732:   nd_matrix = 0;
                   7733:   nd_matrix_len = 0;
                   7734:     switch ( ord->id ) {
                   7735:         case 0:
                   7736:             switch ( ord->ord.simple ) {
                   7737:                 case 0:
                   7738:                     nd_dcomp = 1;
                   7739:                     nd_isrlex = 1;
                   7740:                     break;
                   7741:                 case 1:
                   7742:                     nd_dcomp = 1;
                   7743:                     nd_isrlex = 0;
                   7744:                     break;
                   7745:                 case 2:
                   7746:                     nd_dcomp = 0;
                   7747:                     nd_isrlex = 0;
                   7748:                     ndl_compare_function = ndl_lex_compare;
                   7749:                     break;
                   7750:                 case 11:
                   7751:                     /* XXX */
                   7752:                     nd_dcomp = 0;
                   7753:                     nd_isrlex = 1;
                   7754:                     ndl_compare_function = ndl_ww_lex_compare;
                   7755:                     break;
                   7756:                 default:
                   7757:                     error("nd_gr : unsupported order");
                   7758:             }
                   7759:             break;
                   7760:         case 1:
                   7761:             /* block order */
                   7762:             /* XXX */
                   7763:             nd_dcomp = -1;
                   7764:             nd_isrlex = 0;
                   7765:             ndl_compare_function = ndl_block_compare;
                   7766:             break;
                   7767:         case 2:
                   7768:             /* matrix order */
                   7769:             /* XXX */
                   7770:             nd_dcomp = -1;
                   7771:             nd_isrlex = 0;
                   7772:             nd_matrix_len = ord->ord.matrix.row;
                   7773:             nd_matrix = ord->ord.matrix.matrix;
                   7774:             ndl_compare_function = ndl_matrix_compare;
                   7775:             break;
                   7776:         case 3:
                   7777:             /* composite order */
                   7778:             nd_dcomp = -1;
                   7779:             nd_isrlex = 0;
                   7780:             nd_worb_len = ord->ord.composite.length;
                   7781:             nd_worb = ord->ord.composite.w_or_b;
                   7782:             ndl_compare_function = ndl_composite_compare;
                   7783:             break;
                   7784:
                   7785:         /* module order */
                   7786:         case 256:
                   7787:             switch ( ord->ord.simple ) {
                   7788:                 case 0:
1.21      noro     7789:                     nd_dcomp = 0;
1.1       noro     7790:                     nd_isrlex = 1;
1.21      noro     7791:                     ndl_compare_function = ndl_module_glex_compare;
1.1       noro     7792:                     break;
                   7793:                 case 1:
1.21      noro     7794:                     nd_dcomp = 0;
1.1       noro     7795:                     nd_isrlex = 0;
                   7796:                     ndl_compare_function = ndl_module_glex_compare;
                   7797:                     break;
                   7798:                 case 2:
1.21      noro     7799:                     nd_dcomp = 0;
1.1       noro     7800:                     nd_isrlex = 0;
1.21      noro     7801:                     ndl_compare_function = ndl_module_compare;
                   7802:                     ndl_base_compare_function = ndl_lex_compare;
1.1       noro     7803:                     break;
                   7804:                 default:
1.21      noro     7805:                     error("nd_init_ord : unsupported order");
1.1       noro     7806:             }
                   7807:             break;
                   7808:         case 257:
                   7809:             /* block order */
                   7810:             nd_isrlex = 0;
1.21      noro     7811:             ndl_compare_function = ndl_module_compare;
                   7812:             ndl_base_compare_function = ndl_block_compare;
1.1       noro     7813:             break;
                   7814:         case 258:
                   7815:             /* matrix order */
                   7816:             nd_isrlex = 0;
                   7817:             nd_matrix_len = ord->ord.matrix.row;
                   7818:             nd_matrix = ord->ord.matrix.matrix;
1.21      noro     7819:             ndl_compare_function = ndl_module_compare;
                   7820:             ndl_base_compare_function = ndl_matrix_compare;
1.1       noro     7821:             break;
                   7822:         case 259:
                   7823:             /* composite order */
                   7824:             nd_isrlex = 0;
                   7825:             nd_worb_len = ord->ord.composite.length;
                   7826:             nd_worb = ord->ord.composite.w_or_b;
1.21      noro     7827:             ndl_compare_function = ndl_module_compare;
                   7828:             ndl_base_compare_function = ndl_composite_compare;
                   7829:             break;
                   7830:         case 300:
                   7831:             /* schreyer order */
                   7832:             if ( ord->base->id != 256 )
                   7833:                error("nd_init_ord : unsupported base order");
                   7834:             ndl_compare_function = ndl_module_schreyer_compare;
                   7835:             dmm_stack = ord->dmmstack;
                   7836:             switch ( ord->base->ord.simple ) {
                   7837:                 case 0:
                   7838:                     nd_isrlex = 1;
                   7839:                     ndl_base_compare_function = ndl_glex_compare;
                   7840:                     dl_base_compare_function = cmpdl_revgradlex;
                   7841:                     break;
                   7842:                 case 1:
                   7843:                     nd_isrlex = 0;
                   7844:                     ndl_base_compare_function = ndl_glex_compare;
                   7845:                     dl_base_compare_function = cmpdl_gradlex;
                   7846:                     break;
                   7847:                 case 2:
                   7848:                     nd_isrlex = 0;
                   7849:                     ndl_base_compare_function = ndl_lex_compare;
                   7850:                     dl_base_compare_function = cmpdl_lex;
                   7851:                     break;
                   7852:                 default:
                   7853:                     error("nd_init_ord : unsupported order");
                   7854:             }
1.1       noro     7855:             break;
                   7856:     }
                   7857:     nd_ord = ord;
                   7858: }
                   7859:
                   7860: BlockMask nd_create_blockmask(struct order_spec *ord)
                   7861: {
                   7862:     int n,i,j,s,l;
                   7863:     UINT *t;
                   7864:     BlockMask bm;
                   7865:
                   7866:     /* we only create mask table for block order */
                   7867:     if ( ord->id != 1 && ord->id != 257 )
                   7868:         return 0;
                   7869:     n = ord->ord.block.length;
                   7870:     bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   7871:     bm->n = n;
                   7872:     bm->order_pair = ord->ord.block.order_pair;
                   7873:     bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
                   7874:     for ( i = 0, s = 0; i < n; i++ ) {
                   7875:         bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
                   7876:         for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   7877:         l = bm->order_pair[i].length;
                   7878:         for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   7879:     }
                   7880:     return bm;
                   7881: }
                   7882:
                   7883: EPOS nd_create_epos(struct order_spec *ord)
                   7884: {
                   7885:     int i,j,l,s,ord_l,ord_o;
                   7886:     EPOS epos;
                   7887:     struct order_pair *op;
                   7888:
                   7889:     epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   7890:     switch ( ord->id ) {
1.21      noro     7891:         case 0: case 256: case 300:
1.1       noro     7892:             if ( nd_isrlex ) {
                   7893:                 for ( i = 0; i < nd_nvar; i++ ) {
                   7894:                     epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   7895:                     epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   7896:                 }
                   7897:             } else {
                   7898:                 for ( i = 0; i < nd_nvar; i++ ) {
                   7899:                     epos[i].i = nd_exporigin + i/nd_epw;
                   7900:                     epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   7901:                 }
                   7902:             }
                   7903:             break;
                   7904:         case 1: case 257:
                   7905:             /* block order */
                   7906:             l = ord->ord.block.length;
                   7907:             op = ord->ord.block.order_pair;
                   7908:             for ( j = 0, s = 0; j < l; j++ ) {
                   7909:                 ord_o = op[j].order;
                   7910:                 ord_l = op[j].length;
                   7911:                 if ( !ord_o )
                   7912:                     for ( i = 0; i < ord_l; i++ ) {
                   7913:                         epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   7914:                         epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   7915:                     }
                   7916:                 else
                   7917:                     for ( i = 0; i < ord_l; i++ ) {
                   7918:                         epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   7919:                         epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   7920:                     }
                   7921:                 s += ord_l;
                   7922:             }
                   7923:             break;
                   7924:         case 2:
                   7925:             /* matrix order */
                   7926:         case 3:
                   7927:             /* composite order */
                   7928:         default:
                   7929:             for ( i = 0; i < nd_nvar; i++ ) {
                   7930:                 epos[i].i = nd_exporigin + i/nd_epw;
                   7931:                 epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   7932:             }
                   7933:             break;
                   7934:     }
                   7935:     return epos;
                   7936: }
                   7937:
                   7938: /* external interface */
                   7939:
                   7940: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
                   7941: {
                   7942:     NODE t,in0,in;
                   7943:     ND ndf,nf;
                   7944:     NDV ndvf;
                   7945:     VL vv,tv;
                   7946:     int stat,nvar,max,mrank;
                   7947:     union oNDC dn;
                   7948:     Q cont;
                   7949:     P pp;
                   7950:     LIST ppl;
                   7951:
                   7952:     if ( !f ) {
                   7953:         *rp = 0;
                   7954:         return;
                   7955:     }
                   7956:     pltovl(v,&vv);
                   7957:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   7958:
                   7959:     /* max=65536 implies nd_bpe=32 */
                   7960:     max = 65536;
                   7961:
                   7962:   nd_module = 0;
                   7963:   /* nd_module will be set if ord is a module ordering */
                   7964:     nd_init_ord(ord);
                   7965:     nd_setup_parameters(nvar,max);
                   7966:     if ( nd_module && OID(f) != O_LIST )
                   7967:         error("nd_nf_p : the first argument must be a list");
                   7968:   if ( nd_module ) mrank = length(BDY((LIST)f));
                   7969:     /* conversion to ndv */
                   7970:     for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   7971:         NEXTNODE(in0,in);
                   7972:         if ( nd_module ) {
                   7973:           if ( !BDY(t) || OID(BDY(t)) != O_LIST
                   7974:                || length(BDY((LIST)BDY(t))) != mrank )
                   7975:               error("nd_nf_p : inconsistent basis element");
                   7976:           if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
                   7977:           else ppl = (LIST)BDY(t);
                   7978:           BDY(in) = (pointer)pltondv(CO,vv,ppl);
                   7979:         } else {
                   7980:           if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
                   7981:           else pp = (P)BDY(t);
                   7982:           BDY(in) = (pointer)ptondv(CO,vv,pp);
                   7983:         }
                   7984:         if ( m ) ndv_mod(m,(NDV)BDY(in));
                   7985:     }
                   7986:     if ( in0 ) NEXT(in) = 0;
                   7987:
                   7988:     if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
                   7989:     else ndvf = ptondv(CO,vv,(P)f);
                   7990:     if ( m ) ndv_mod(m,ndvf);
                   7991:     ndf = (pointer)ndvtond(m,ndvf);
                   7992:
                   7993:     /* dont sort, dont removecont */
1.24      noro     7994:     ndv_setup(m,0,in0,1,1,0);
1.1       noro     7995:     nd_scale=2;
1.6       noro     7996:     stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1       noro     7997:     if ( !stat )
                   7998:         error("nd_nf_p : exponent too large");
                   7999:     if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
                   8000:     else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
                   8001: }
                   8002:
                   8003: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   8004: {
                   8005:     NM m;
                   8006:     UINT *t,*s;
                   8007:     int i;
                   8008:
                   8009:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   8010:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   8011:         t = DL(m);
                   8012:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   8013:         r[i] = CM(m);
                   8014:     }
                   8015:     for ( i = 0; !r[i]; i++ );
                   8016:     return i;
                   8017: }
                   8018:
                   8019: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
                   8020: {
                   8021:     NM m;
                   8022:     UINT *t,*s;
                   8023:     int i;
                   8024:
                   8025:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   8026:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   8027:         t = DL(m);
                   8028:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     8029:         r[i] = CZ(m);
1.1       noro     8030:     }
                   8031:     for ( i = 0; !r[i]; i++ );
                   8032:     return i;
                   8033: }
                   8034:
                   8035: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
                   8036: {
                   8037:     NM m;
                   8038:     UINT *t,*s;
                   8039:     int i;
                   8040:
                   8041:     for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
                   8042:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   8043:         t = DL(m);
                   8044:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   8045:         mpz_set(r[i],BDY(CZ(m)));
                   8046:     }
                   8047:     for ( i = 0; !mpz_sgn(r[i]); i++ );
                   8048:     return i;
                   8049: }
                   8050:
                   8051: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
                   8052: {
                   8053:     NM m;
                   8054:     unsigned long *v;
                   8055:     int i,j,h,size;
                   8056:   UINT *s,*t;
                   8057:
                   8058:   size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
                   8059:     v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
                   8060:     bzero(v,size);
                   8061:     for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
                   8062:     t = DL(m);
                   8063:     h = ndl_hash_value(t);
                   8064:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   8065:       v[i/BLEN] |= 1L <<(i%BLEN);
                   8066:     }
                   8067:     return v;
                   8068: }
                   8069:
                   8070: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
                   8071: {
                   8072:     NMV mr;
                   8073:     UINT *d,*t,*s;
                   8074:     int i,j,len,h,head;
                   8075:
                   8076:     d = DL(m);
                   8077:     len = LEN(p);
                   8078:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   8079:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   8080:         ndl_add(d,DL(mr),t);
                   8081:     h = ndl_hash_value(t);
                   8082:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   8083:     if ( j == 0 ) head = i;
                   8084:       v[i/BLEN] |= 1L <<(i%BLEN);
                   8085:     }
                   8086:     return head;
                   8087: }
                   8088:
                   8089: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   8090: {
                   8091:     NM m;
                   8092:     NMV mr;
                   8093:     UINT *d,*t,*s;
                   8094:     NDV p;
                   8095:     int i,j,len;
                   8096:     Z *r;
                   8097:
                   8098:     m = pair->mul;
                   8099:     d = DL(m);
                   8100:     p = nd_ps[pair->index];
                   8101:     len = LEN(p);
                   8102:     r = (Z *)CALLOC(n,sizeof(Q));
                   8103:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   8104:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   8105:         ndl_add(d,DL(mr),t);
                   8106:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     8107:         r[i] = CZ(mr);
1.1       noro     8108:     }
                   8109:     return r;
                   8110: }
                   8111:
1.11      noro     8112: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1       noro     8113: {
                   8114:     NM m;
                   8115:     NMV mr;
1.11      noro     8116:     UINT *d,*t,*s,*u;
1.1       noro     8117:     NDV p;
                   8118:     unsigned char *ivc;
                   8119:     unsigned short *ivs;
                   8120:     UINT *v,*ivi,*s0v;
1.11      noro     8121:     int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1       noro     8122:     IndArray r;
                   8123:
                   8124:     m = pair->mul;
                   8125:     d = DL(m);
                   8126:     if ( trace )
                   8127:       p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
                   8128:     else
                   8129:       p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
                   8130:
                   8131:     len = LEN(p);
                   8132:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   8133:     v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11      noro     8134:     for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
                   8135:       ndl_add(d,DL(mr),t);
                   8136:       st = prev;
                   8137:       ed = n;
                   8138:       while ( ed > st ) {
                   8139:         md = (st+ed)/2;
                   8140:         u = s0+md*nd_wpd;
                   8141:         c = DL_COMPARE(u,t);
                   8142:         if ( c == 0 ) break;
                   8143:         else if ( c > 0 ) st = md;
                   8144:         else ed = md;
                   8145:       }
                   8146:       prev = v[j] = md;
1.1       noro     8147:     }
                   8148:     r = (IndArray)MALLOC(sizeof(struct oIndArray));
                   8149:     r->head = v[0];
                   8150:     diff = 0;
                   8151:     for ( i = 1; i < len; i++ ) {
                   8152:         cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   8153:     }
                   8154:     if ( diff < 256 ) {
                   8155:         r->width = 1;
                   8156:         ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   8157:         r->index.c = ivc;
                   8158:         for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   8159:     } else if ( diff < 65536 ) {
                   8160:         r->width = 2;
                   8161:         ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   8162:         r->index.s = ivs;
                   8163:         for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   8164:     } else {
                   8165:         r->width = 4;
                   8166:         ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   8167:         r->index.i = ivi;
                   8168:         for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
                   8169:     }
                   8170:     return r;
                   8171: }
                   8172:
                   8173: int compress_array(Z *svect,Z *cvect,int n)
                   8174: {
                   8175:     int i,j;
                   8176:
                   8177:     for ( i = j = 0; i < n; i++ )
                   8178:         if ( svect[i] ) cvect[j++] = svect[i];
                   8179:     return j;
                   8180: }
                   8181:
                   8182: void expand_array(Z *svect,Z *cvect,int n)
                   8183: {
                   8184:     int i,j;
                   8185:
                   8186:     for ( i = j = 0; j < n;  i++  )
                   8187:         if ( svect[i] ) svect[i] = cvect[j++];
                   8188: }
                   8189:
1.8       noro     8190: #if 0
1.1       noro     8191: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8192: {
                   8193:     int i,j,k,len,pos,prev,nz;
                   8194:     Z cs,mcs,c1,c2,cr,gcd,t;
                   8195:     IndArray ivect;
                   8196:     unsigned char *ivc;
                   8197:     unsigned short *ivs;
                   8198:     unsigned int *ivi;
                   8199:     NDV redv;
                   8200:     NMV mr;
                   8201:     NODE rp;
                   8202:     int maxrs;
                   8203:     double hmag;
                   8204:     Z *cvect;
1.3       noro     8205:     int l;
1.1       noro     8206:
                   8207:     maxrs = 0;
                   8208:     for ( i = 0; i < col && !svect[i]; i++ );
                   8209:     if ( i == col ) return maxrs;
                   8210:     hmag = p_mag((P)svect[i])*nd_scale;
                   8211:     cvect = (Z *)MALLOC(col*sizeof(Q));
                   8212:     for ( i = 0; i < nred; i++ ) {
                   8213:         ivect = imat[i];
                   8214:         k = ivect->head;
                   8215:         if ( svect[k] ) {
                   8216:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8217:             redv = nd_demand?ndv_load(rp0[i]->index)
                   8218:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   8219:             len = LEN(redv); mr = BDY(redv);
1.6       noro     8220:             igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1       noro     8221:             chsgnz(cs,&mcs);
                   8222:             if ( !UNIQ(cr) ) {
                   8223:                 for ( j = 0; j < col; j++ ) {
                   8224:                     mulz(svect[j],cr,&c1); svect[j] = c1;
                   8225:                 }
                   8226:             }
                   8227:             svect[k] = 0; prev = k;
                   8228:             switch ( ivect->width ) {
                   8229:                 case 1:
                   8230:                     ivc = ivect->index.c;
                   8231:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8232:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     8233:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     8234:                     }
                   8235:                     break;
                   8236:                 case 2:
                   8237:                     ivs = ivect->index.s;
                   8238:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8239:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     8240:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     8241:                     }
                   8242:                     break;
                   8243:                 case 4:
                   8244:                     ivi = ivect->index.i;
                   8245:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8246:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     8247:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     8248:                     }
                   8249:                     break;
                   8250:             }
                   8251:             for ( j = k+1; j < col && !svect[j]; j++ );
                   8252:             if ( j == col ) break;
                   8253:             if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
                   8254:                 nz = compress_array(svect,cvect,col);
                   8255:                 removecont_array((P *)cvect,nz,1);
                   8256:                 expand_array(svect,cvect,nz);
                   8257:                 hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   8258:             }
                   8259:         }
                   8260:     }
                   8261:     nz = compress_array(svect,cvect,col);
                   8262:     removecont_array((P *)cvect,nz,1);
                   8263:     expand_array(svect,cvect,nz);
                   8264:     if ( DP_Print ) {
                   8265:         fprintf(asir_out,"-"); fflush(asir_out);
                   8266:     }
                   8267:     return maxrs;
                   8268: }
1.4       noro     8269: #else
1.9       noro     8270:
1.4       noro     8271: /* direct mpz version */
                   8272: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8273: {
                   8274:     int i,j,k,len,pos,prev;
                   8275:     mpz_t cs,cr,gcd;
                   8276:     IndArray ivect;
                   8277:     unsigned char *ivc;
                   8278:     unsigned short *ivs;
                   8279:     unsigned int *ivi;
                   8280:     NDV redv;
                   8281:     NMV mr;
                   8282:     NODE rp;
                   8283:     int maxrs;
                   8284:     double hmag;
                   8285:     int l;
1.13      noro     8286:     static mpz_t *svect;
                   8287:     static int svect_len=0;
1.4       noro     8288:
                   8289:     maxrs = 0;
                   8290:     for ( i = 0; i < col && !svect0[i]; i++ );
                   8291:     if ( i == col ) return maxrs;
                   8292:     hmag = p_mag((P)svect0[i])*nd_scale;
1.13      noro     8293:     if ( col > svect_len ) {
                   8294:       svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   8295:       svect_len = col;
                   8296:     }
1.4       noro     8297:     for ( i = 0; i < col; i++ ) {
                   8298:       mpz_init(svect[i]);
                   8299:       if ( svect0[i] )
                   8300:         mpz_set(svect[i],BDY(svect0[i]));
                   8301:       else
                   8302:         mpz_set_ui(svect[i],0);
                   8303:     }
                   8304:     mpz_init(gcd); mpz_init(cs); mpz_init(cr);
                   8305:     for ( i = 0; i < nred; i++ ) {
                   8306:         ivect = imat[i];
                   8307:         k = ivect->head;
                   8308:         if ( mpz_sgn(svect[k]) ) {
                   8309:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8310:             redv = nd_demand?ndv_load(rp0[i]->index)
                   8311:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   8312:             len = LEN(redv); mr = BDY(redv);
1.6       noro     8313:             mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4       noro     8314:             mpz_div(cs,svect[k],gcd);
1.6       noro     8315:             mpz_div(cr,BDY(CZ(mr)),gcd);
1.4       noro     8316:             mpz_neg(cs,cs);
1.9       noro     8317:             if ( MUNIMPZ(cr) )
                   8318:               for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
                   8319:             else if ( !UNIMPZ(cr) )
                   8320:               for ( j = 0; j < col; j++ ) {
                   8321:                 if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
                   8322:               }
1.4       noro     8323:             mpz_set_ui(svect[k],0);
                   8324:             prev = k;
                   8325:             switch ( ivect->width ) {
                   8326:                 case 1:
                   8327:                     ivc = ivect->index.c;
                   8328:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8329:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     8330:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     8331:                     }
                   8332:                     break;
                   8333:                 case 2:
                   8334:                     ivs = ivect->index.s;
                   8335:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8336:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     8337:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     8338:                     }
                   8339:                     break;
                   8340:                 case 4:
                   8341:                     ivi = ivect->index.i;
                   8342:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8343:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     8344:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     8345:                     }
                   8346:                     break;
                   8347:             }
                   8348:             for ( j = k+1; j < col && !svect[j]; j++ );
                   8349:             if ( j == col ) break;
                   8350:             if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
                   8351:                 mpz_removecont_array(svect,col);
                   8352:                 hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
                   8353:             }
                   8354:         }
                   8355:     }
                   8356:     mpz_removecont_array(svect,col);
                   8357:     if ( DP_Print ) {
                   8358:         fprintf(asir_out,"-"); fflush(asir_out);
                   8359:     }
                   8360:     for ( i = 0; i < col; i++ )
                   8361:       if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
                   8362:       else svect0[i] = 0;
                   8363:     return maxrs;
                   8364: }
                   8365: #endif
1.1       noro     8366:
1.40      noro     8367: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred,SIG sig)
1.1       noro     8368: {
                   8369:     int i,j,k,len,pos,prev;
                   8370:     UINT c,c1,c2,c3,up,lo,dmy;
                   8371:     IndArray ivect;
                   8372:     unsigned char *ivc;
                   8373:     unsigned short *ivs;
                   8374:     unsigned int *ivi;
                   8375:     NDV redv;
                   8376:     NMV mr;
                   8377:     NODE rp;
                   8378:     int maxrs;
                   8379:
                   8380:     maxrs = 0;
                   8381:     for ( i = 0; i < nred; i++ ) {
                   8382:         ivect = imat[i];
                   8383:         k = ivect->head; svect[k] %= m;
1.40      noro     8384:         if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.1       noro     8385:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8386:             c = m-c; redv = nd_ps[rp0[i]->index];
                   8387:             len = LEN(redv); mr = BDY(redv);
                   8388:             svect[k] = 0; prev = k;
                   8389:             switch ( ivect->width ) {
                   8390:                 case 1:
                   8391:                     ivc = ivect->index.c;
                   8392:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8393:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.40      noro     8394:                         if ( c1 ) {
                   8395:                           c2 = svect[pos];
1.1       noro     8396:                           DMA(c1,c,c2,up,lo);
                   8397:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   8398:                           } else svect[pos] = lo;
1.40      noro     8399:                         }
1.1       noro     8400:                     }
                   8401:                     break;
                   8402:                 case 2:
                   8403:                     ivs = ivect->index.s;
                   8404:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8405:                         pos = prev+ivs[j]; c1 = CM(mr);
                   8406:                         prev = pos;
1.40      noro     8407:                         if ( c1 ) {
                   8408:                           c2 = svect[pos];
1.1       noro     8409:                           DMA(c1,c,c2,up,lo);
                   8410:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   8411:                           } else svect[pos] = lo;
1.40      noro     8412:                         }
1.1       noro     8413:                     }
                   8414:                     break;
                   8415:                 case 4:
                   8416:                     ivi = ivect->index.i;
                   8417:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8418:                         pos = prev+ivi[j]; c1 = CM(mr);
                   8419:                         prev = pos;
1.40      noro     8420:                         if ( c1 ) {
                   8421:                           c2 = svect[pos];
1.1       noro     8422:                           DMA(c1,c,c2,up,lo);
                   8423:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   8424:                           } else svect[pos] = lo;
1.40      noro     8425:                         }
1.1       noro     8426:                     }
                   8427:                     break;
                   8428:             }
                   8429:         }
                   8430:     }
                   8431:     for ( i = 0; i < col; i++ )
                   8432:         if ( svect[i] >= (UINT)m ) svect[i] %= m;
                   8433:     return maxrs;
                   8434: }
                   8435:
                   8436: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8437: {
                   8438:     int i,j,k,len,pos,prev;
                   8439:     UINT c,c1,c2,c3,up,lo,dmy;
                   8440:     IndArray ivect;
                   8441:     unsigned char *ivc;
                   8442:     unsigned short *ivs;
                   8443:     unsigned int *ivi;
                   8444:     NDV redv;
                   8445:     NMV mr;
                   8446:     NODE rp;
                   8447:     int maxrs;
                   8448:
                   8449:     maxrs = 0;
                   8450:     for ( i = 0; i < nred; i++ ) {
                   8451:         ivect = imat[i];
                   8452:         k = ivect->head;
                   8453:         if ( (c = svect[k]) != 0 ) {
                   8454:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8455:             c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
                   8456:             len = LEN(redv); mr = BDY(redv);
                   8457:             svect[k] = 0; prev = k;
                   8458:             switch ( ivect->width ) {
                   8459:                 case 1:
                   8460:                     ivc = ivect->index.c;
                   8461:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8462:                         pos = prev+ivc[j]; prev = pos;
                   8463:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   8464:                     }
                   8465:                     break;
                   8466:                 case 2:
                   8467:                     ivs = ivect->index.s;
                   8468:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8469:                         pos = prev+ivs[j]; prev = pos;
                   8470:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   8471:                     }
                   8472:                     break;
                   8473:                 case 4:
                   8474:                     ivi = ivect->index.i;
                   8475:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8476:                         pos = prev+ivi[j]; prev = pos;
                   8477:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   8478:                     }
                   8479:                     break;
                   8480:             }
                   8481:         }
                   8482:     }
                   8483:     return maxrs;
                   8484: }
                   8485:
                   8486: ND nd_add_lf(ND p1,ND p2)
                   8487: {
                   8488:     int n,c,can;
                   8489:     ND r;
                   8490:     NM m1,m2,mr0,mr,s;
                   8491:     Z t;
                   8492:
                   8493:     if ( !p1 ) return p2;
                   8494:     else if ( !p2 ) return p1;
                   8495:     else {
                   8496:         can = 0;
                   8497:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   8498:             c = DL_COMPARE(DL(m1),DL(m2));
                   8499:             switch ( c ) {
                   8500:                 case 0:
                   8501:                     addlf(CZ(m1),CZ(m2),&t);
                   8502:                     s = m1; m1 = NEXT(m1);
                   8503:                     if ( t ) {
                   8504:                         can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
                   8505:                     } else {
                   8506:                         can += 2; FREENM(s);
                   8507:                     }
                   8508:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   8509:                     break;
                   8510:                 case 1:
                   8511:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   8512:                     break;
                   8513:                 case -1:
                   8514:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   8515:                     break;
                   8516:             }
                   8517:         }
                   8518:         if ( !mr0 )
                   8519:             if ( m1 ) mr0 = m1;
                   8520:             else if ( m2 ) mr0 = m2;
                   8521:             else return 0;
                   8522:         else if ( m1 ) NEXT(mr) = m1;
                   8523:         else if ( m2 ) NEXT(mr) = m2;
                   8524:         else NEXT(mr) = 0;
                   8525:         BDY(p1) = mr0;
                   8526:         SG(p1) = MAX(SG(p1),SG(p2));
                   8527:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   8528:         FREEND(p2);
                   8529:         return p1;
                   8530:     }
                   8531: }
                   8532:
                   8533: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   8534: {
                   8535:     int i,j,k,len,pos,prev;
                   8536:     mpz_t c,mc,c1;
                   8537:     IndArray ivect;
                   8538:     unsigned char *ivc;
                   8539:     unsigned short *ivs;
                   8540:     unsigned int *ivi;
                   8541:     NDV redv;
                   8542:     NMV mr;
                   8543:     NODE rp;
                   8544:     int maxrs;
                   8545:
                   8546:     maxrs = 0;
                   8547:     lf_lazy = 1;
                   8548:     for ( i = 0; i < nred; i++ ) {
                   8549:         ivect = imat[i];
                   8550:         k = ivect->head;
                   8551:         mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
                   8552:         if ( mpz_sgn(svect[k]) ) {
                   8553:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   8554:             mpz_neg(svect[k],svect[k]);
                   8555:             redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
                   8556:             len = LEN(redv); mr = BDY(redv);
                   8557:             prev = k;
                   8558:             switch ( ivect->width ) {
                   8559:                 case 1:
                   8560:                     ivc = ivect->index.c;
                   8561:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8562:                         pos = prev+ivc[j]; prev = pos;
                   8563:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   8564:                     }
                   8565:                     break;
                   8566:                 case 2:
                   8567:                     ivs = ivect->index.s;
                   8568:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8569:                         pos = prev+ivs[j]; prev = pos;
                   8570:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   8571:                     }
                   8572:                     break;
                   8573:                 case 4:
                   8574:                     ivi = ivect->index.i;
                   8575:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   8576:                         pos = prev+ivi[j]; prev = pos;
                   8577:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   8578:                     }
                   8579:                     break;
                   8580:             }
                   8581:             mpz_set_ui(svect[k],0);
                   8582:         }
                   8583:     }
                   8584:     lf_lazy=0;
                   8585:     for ( i = 0; i < col; i++ ) {
                   8586:         mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
                   8587:     }
                   8588:     return maxrs;
                   8589: }
                   8590:
                   8591: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
                   8592: {
                   8593:     int i,j,k,l,rank,s;
                   8594:     mpz_t a,a1,inv;
                   8595:     mpz_t *t,*pivot,*pk;
                   8596:     mpz_t **mat;
                   8597:     struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
                   8598:     int size,size1;
                   8599:
                   8600:     mpz_init(inv);
                   8601:     mpz_init(a);
                   8602:     mat = (mpz_t **)mat0;
                   8603:         size = 0;
                   8604:     for ( rank = 0, j = 0; j < col; j++ ) {
                   8605:         for ( i = rank; i < row; i++ ) {
                   8606:             mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
                   8607:         }
                   8608:         for ( i = rank; i < row; i++ )
                   8609:             if ( mpz_sgn(mat[i][j]) )
                   8610:                 break;
                   8611:         if ( i == row ) {
                   8612:             colstat[j] = 0;
                   8613:             continue;
                   8614:         } else
                   8615:             colstat[j] = 1;
                   8616:         if ( i != rank ) {
                   8617:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   8618:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   8619:         }
                   8620:         pivot = mat[rank];
                   8621:         s = sugar[rank];
                   8622:         mpz_invert(inv,pivot[j],BDY(current_mod_lf));
                   8623:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   8624:             if ( mpz_sgn(*pk) ) {
                   8625:                 mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
                   8626:             }
                   8627:         for ( i = rank+1; i < row; i++ ) {
                   8628:             t = mat[i];
                   8629:             if ( mpz_sgn(t[j]) ) {
                   8630:                 sugar[i] = MAX(sugar[i],s);
                   8631:                 mpz_neg(a,t[j]);
                   8632:                 red_by_vect_lf(t+j,pivot+j,a,col-j);
                   8633:             }
                   8634:         }
                   8635:         rank++;
                   8636:     }
                   8637:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   8638:         if ( colstat[j] ) {
                   8639:             pivot = mat[l];
                   8640:             s = sugar[l];
                   8641:             for ( k = j; k < col; k++ )
                   8642:               mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
                   8643:             for ( i = 0; i < l; i++ ) {
                   8644:                 t = mat[i];
                   8645:                 if ( mpz_sgn(t[j]) ) {
                   8646:                     sugar[i] = MAX(sugar[i],s);
                   8647:                     mpz_neg(a,t[j]);
                   8648:                     red_by_vect_lf(t+j,pivot+j,a,col-j);
                   8649:                 }
                   8650:             }
                   8651:             l--;
                   8652:         }
                   8653:     for ( j = 0, l = 0; l < rank; j++ )
                   8654:         if ( colstat[j] ) {
                   8655:             t = mat[l];
                   8656:             for ( k = j; k < col; k++ ) {
                   8657:                 mpz_mod(t[k],t[k],BDY(current_mod_lf));
                   8658:             }
                   8659:             l++;
                   8660:         }
                   8661:     return rank;
                   8662: }
                   8663:
                   8664:
                   8665: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   8666: {
                   8667:     int j,k,len;
                   8668:     UINT *p;
                   8669:     UINT c;
                   8670:     NDV r;
                   8671:     NMV mr0,mr;
                   8672:
                   8673:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   8674:     if ( !len ) return 0;
                   8675:     else {
                   8676:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8677: #if 0
                   8678:         ndv_alloc += nmv_adv*len;
                   8679: #endif
                   8680:         mr = mr0;
                   8681:         p = s0vect;
                   8682:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   8683:             if ( !rhead[j] ) {
                   8684:                 if ( (c = vect[k++]) != 0 ) {
                   8685:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   8686:                 }
                   8687:             }
                   8688:         MKNDV(nd_nvar,mr0,len,r);
                   8689:         return r;
                   8690:     }
                   8691: }
                   8692:
1.40      noro     8693: NDV vect_to_ndv_s(UINT *vect,int col,UINT *s0vect)
                   8694: {
                   8695:     int j,k,len;
                   8696:     UINT *p;
                   8697:     UINT c;
                   8698:     NDV r;
                   8699:     NMV mr0,mr;
                   8700:
                   8701:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   8702:     if ( !len ) return 0;
                   8703:     else {
                   8704:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8705:         mr = mr0;
                   8706:         p = s0vect;
                   8707:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   8708:           if ( (c = vect[k++]) != 0 ) {
                   8709:             ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   8710:           }
                   8711:         MKNDV(nd_nvar,mr0,len,r);
                   8712:         return r;
                   8713:     }
                   8714: }
                   8715:
1.1       noro     8716: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
                   8717: {
                   8718:     int j,k,len;
                   8719:     UINT *p;
                   8720:     NDV r;
                   8721:     NMV mr0,mr;
                   8722:
                   8723:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
                   8724:     if ( !len ) return 0;
                   8725:     else {
                   8726:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8727:         mr = mr0;
                   8728:         p = s0vect;
                   8729:         for ( j = 0; j < col; j++, p += nd_wpd )
                   8730:       if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
                   8731:             ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
                   8732:           }
                   8733:         MKNDV(nd_nvar,mr0,len,r);
                   8734:         return r;
                   8735:     }
                   8736: }
                   8737:
                   8738: /* for preprocessed vector */
                   8739:
                   8740: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   8741: {
1.6       noro     8742:   int j,k,len;
                   8743:   UINT *p;
                   8744:   Z c;
                   8745:   NDV r;
                   8746:   NMV mr0,mr;
1.1       noro     8747:
1.6       noro     8748:   for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   8749:   if ( !len ) return 0;
                   8750:   else {
                   8751:     mr0 = (NMV)MALLOC(nmv_adv*len);
1.1       noro     8752: #if 0
1.6       noro     8753:     ndv_alloc += nmv_adv*len;
1.1       noro     8754: #endif
1.6       noro     8755:     mr = mr0;
                   8756:     p = s0vect;
                   8757:     for ( j = k = 0; j < col; j++, p += nd_wpd ) {
                   8758:       if ( !rhead[j] ) {
                   8759:         if ( (c = vect[k++]) != 0 ) {
                   8760:           if ( !INT(c) )
                   8761:             error("vect_to_ndv_q : components must be integers");
                   8762:             ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
                   8763:         }
                   8764:       }
1.1       noro     8765:     }
1.6       noro     8766:     MKNDV(nd_nvar,mr0,len,r);
                   8767:     return r;
                   8768:   }
1.1       noro     8769: }
                   8770:
                   8771: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   8772: {
                   8773:     int j,k,len;
                   8774:     UINT *p;
                   8775:     mpz_t c;
                   8776:     NDV r;
                   8777:     NMV mr0,mr;
                   8778:
                   8779:     for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
                   8780:     if ( !len ) return 0;
                   8781:     else {
                   8782:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   8783: #if 0
                   8784:         ndv_alloc += nmv_adv*len;
                   8785: #endif
                   8786:         mr = mr0;
                   8787:         p = s0vect;
                   8788:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   8789:             if ( !rhead[j] ) {
                   8790:                 c[0] = vect[k++][0];
                   8791:                 if ( mpz_sgn(c) ) {
                   8792:                     ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
                   8793:                 }
                   8794:             }
                   8795:         MKNDV(nd_nvar,mr0,len,r);
                   8796:         return r;
                   8797:     }
                   8798: }
                   8799:
                   8800: /* for plain vector */
                   8801:
                   8802: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
                   8803: {
                   8804:     int j,k,len;
                   8805:     UINT *p;
                   8806:     Z c;
                   8807:     NDV r;
                   8808:     NMV mr0,mr;
                   8809:
                   8810:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   8811:     if ( !len ) return 0;
                   8812:     else {
                   8813:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   8814: #if 0
                   8815:         ndv_alloc += nmv_adv*len;
                   8816: #endif
                   8817:         mr = mr0;
                   8818:         p = s0vect;
                   8819:         for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   8820:             if ( (c = vect[k]) != 0 ) {
                   8821:                 if ( !INT(c) )
1.6       noro     8822:                     error("plain_vect_to_ndv_q : components must be integers");
                   8823:                 ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1       noro     8824:             }
                   8825:         MKNDV(nd_nvar,mr0,len,r);
                   8826:         return r;
                   8827:     }
                   8828: }
                   8829:
                   8830: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
                   8831: {
                   8832:     ND_pairs t;
                   8833:     NODE sp0,sp;
                   8834:     int stat;
                   8835:     ND spol;
                   8836:
                   8837:     for ( t = l; t; t = NEXT(t) ) {
                   8838:         stat = nd_sp(m,trace,t,&spol);
                   8839:         if ( !stat ) return 0;
                   8840:         if ( spol ) {
                   8841:             add_pbucket_symbolic(bucket,spol);
                   8842:         }
                   8843:     }
                   8844:     return 1;
                   8845: }
                   8846:
                   8847: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   8848: {
                   8849:     NODE rp0,rp;
                   8850:     NM mul,head,s0,s;
                   8851:     int index,col,i,sugar;
                   8852:     RHist h;
                   8853:     UINT *s0v,*p;
                   8854:     NM_ind_pair pair;
                   8855:     ND red;
                   8856:     NDV *ps;
                   8857:
                   8858:     s0 = 0; rp0 = 0; col = 0;
                   8859:   if ( nd_demand )
                   8860:       ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   8861:   else
                   8862:       ps = trace?nd_ps_trace:nd_ps;
                   8863:     while ( 1 ) {
                   8864:         head = remove_head_pbucket_symbolic(bucket);
                   8865:         if ( !head ) break;
                   8866:         if ( !s0 ) s0 = head;
                   8867:         else NEXT(s) = head;
                   8868:         s = head;
                   8869:         index = ndl_find_reducer(DL(head));
                   8870:         if ( index >= 0 ) {
                   8871:             h = nd_psh[index];
                   8872:             NEWNM(mul);
                   8873:             ndl_sub(DL(head),DL(h),DL(mul));
                   8874:             if ( ndl_check_bound2(index,DL(mul)) )
                   8875:                 return 0;
                   8876:             sugar = TD(DL(mul))+SG(ps[index]);
1.28      noro     8877:             MKNM_ind_pair(pair,mul,index,sugar,0);
1.1       noro     8878:             red = ndv_mul_nm_symbolic(mul,ps[index]);
                   8879:             add_pbucket_symbolic(bucket,nd_remove_head(red));
                   8880:             NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   8881:         }
                   8882:         col++;
                   8883:     }
                   8884:     if ( rp0 ) NEXT(rp) = 0;
                   8885:     NEXT(s) = 0;
                   8886:     s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   8887:     for ( i = 0, p = s0v, s = s0; i < col;
                   8888:         i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   8889:     *s0vect = s0v;
                   8890:     *r = rp0;
                   8891:     return col;
                   8892: }
                   8893:
                   8894: void print_ndp(ND_pairs l)
                   8895: {
                   8896:   ND_pairs t;
                   8897:
                   8898:   for ( t = l; t; t = NEXT(t) )
                   8899:     printf("[%d,%d] ",t->i1,t->i2);
                   8900:   printf("\n");
                   8901: }
                   8902:
                   8903: NODE nd_f4(int m,int checkonly,int **indp)
                   8904: {
                   8905:     int i,nh,stat,index,f4red;
                   8906:     NODE r,g,tn0,tn,node;
                   8907:     ND_pairs d,l,t,ll0,ll,lh;
                   8908:   LIST l0,l1;
                   8909:     ND spol,red;
                   8910:     NDV nf,redv;
                   8911:     NM s0,s;
                   8912:     NODE rp0,srp0,nflist,nzlist,nzlist_t;
                   8913:     int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
                   8914:     UINT c;
                   8915:     UINT **spmat;
                   8916:     UINT *s0vect,*svect,*p,*v;
                   8917:     int *colstat;
                   8918:     IndArray *imat;
                   8919:     int *rhead;
                   8920:     int spcol,sprow;
                   8921:     int sugar,sugarh;
                   8922:     PGeoBucket bucket;
                   8923:     struct oEGT eg0,eg1,eg_f4;
                   8924:     Z i1,i2,sugarq;
1.12      noro     8925:
                   8926:     init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1       noro     8927: #if 0
                   8928:     ndv_alloc = 0;
                   8929: #endif
1.11      noro     8930:     Nf4_red=0;
1.1       noro     8931:     g = 0; d = 0;
                   8932:     for ( i = 0; i < nd_psn; i++ ) {
                   8933:         d = update_pairs(d,g,i,0);
                   8934:         g = update_base(g,i);
                   8935:     }
                   8936:   nzlist = 0;
                   8937:     nzlist_t = nd_nzlist;
                   8938:     f4red = 1;
                   8939:     nd_last_nonzero = 0;
                   8940:     while ( d ) {
                   8941:         get_eg(&eg0);
                   8942:         l = nd_minsugarp(d,&d);
                   8943:         sugar = nd_sugarweight?l->sugar2:SG(l);
                   8944:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   8945:         if ( nzlist_t ) {
                   8946:             node = BDY((LIST)BDY(nzlist_t));
1.6       noro     8947:             sugarh = ZTOS((Q)ARG0(node));
1.1       noro     8948:             tn = BDY((LIST)ARG1(node));
                   8949:             if ( !tn ) {
                   8950:               nzlist_t = NEXT(nzlist_t);
                   8951:               continue;
                   8952:             }
                   8953:             /* tn = [[i1,i2],...] */
                   8954:             lh = nd_ipairtospair(tn);
                   8955:         }
                   8956:         bucket = create_pbucket();
                   8957:         stat = nd_sp_f4(m,0,l,bucket);
                   8958:         if ( !stat ) {
                   8959:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8960:             NEXT(t) = d; d = l;
                   8961:             d = nd_reconstruct(0,d);
                   8962:             continue;
                   8963:         }
                   8964:         if ( bucket->m < 0 ) continue;
                   8965:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   8966:         if ( !col ) {
                   8967:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8968:             NEXT(t) = d; d = l;
                   8969:             d = nd_reconstruct(0,d);
                   8970:             continue;
                   8971:         }
1.12      noro     8972:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1       noro     8973:         if ( DP_Print )
1.6       noro     8974:             fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5       noro     8975:                 sugar,eg_f4.exectime);
1.1       noro     8976:         nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
                   8977:         if ( checkonly && nflist ) return 0;
                   8978:         /* adding new bases */
                   8979:         if ( nflist ) nd_last_nonzero = f4red;
                   8980:         for ( r = nflist; r; r = NEXT(r) ) {
                   8981:             nf = (NDV)BDY(r);
1.38      noro     8982:             if ( nd_f4_td ) SG(nf) = nd_tdeg(nf);
1.1       noro     8983:             ndv_removecont(m,nf);
                   8984:             if ( !m && nd_nalg ) {
                   8985:                 ND nf1;
                   8986:
                   8987:                 nf1 = ndvtond(m,nf);
                   8988:                 nd_monic(0,&nf1);
                   8989:                 nd_removecont(m,nf1);
                   8990:                 nf = ndtondv(m,nf1);
                   8991:             }
1.24      noro     8992:             nh = ndv_newps(m,nf,0);
1.1       noro     8993:             d = update_pairs(d,g,nh,0);
                   8994:             g = update_base(g,nh);
                   8995:         }
                   8996:         if ( DP_Print ) {
                   8997:           fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
                   8998:         }
                   8999:         if ( nd_gentrace ) {
                   9000:       for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
                   9001:         NEXTNODE(tn0,tn);
1.6       noro     9002:                 STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1       noro     9003:                 node = mknode(2,i1,i2); MKLIST(l0,node);
                   9004:         BDY(tn) = l0;
                   9005:       }
                   9006:       if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6       noro     9007:             STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1       noro     9008:             MKNODE(node,l1,nzlist); nzlist = node;
                   9009:         }
                   9010:         if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
                   9011:         f4red++;
                   9012:         if ( nd_f4red && f4red > nd_f4red ) break;
                   9013:         if ( nd_rank0 && !nflist ) break;
                   9014:     }
                   9015:     if ( nd_gentrace ) {
                   9016:     MKLIST(l0,reverse_node(nzlist));
                   9017:         MKNODE(nd_alltracelist,l0,0);
                   9018:     }
                   9019: #if 0
                   9020:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   9021: #endif
1.12      noro     9022:   if ( DP_Print ) {
                   9023:     fprintf(asir_out,"number of red=%d,",Nf4_red);
                   9024:     fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
                   9025:       f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
1.41      noro     9026:     fprintf(asir_out,"number of removed pairs=%d\n,",NcriB+NcriMF+Ncri2);
1.12      noro     9027:   }
1.1       noro     9028:   conv_ilist(nd_demand,0,g,indp);
                   9029:     return g;
                   9030: }
                   9031:
                   9032: NODE nd_f4_trace(int m,int **indp)
                   9033: {
                   9034:     int i,nh,stat,index;
                   9035:     NODE r,g;
                   9036:     ND_pairs d,l,l0,t;
                   9037:     ND spol,red;
                   9038:     NDV nf,redv,nfqv,nfv;
                   9039:     NM s0,s;
                   9040:     NODE rp0,srp0,nflist;
                   9041:     int nsp,nred,col,rank,len,k,j,a;
                   9042:     UINT c;
                   9043:     UINT **spmat;
                   9044:     UINT *s0vect,*svect,*p,*v;
                   9045:     int *colstat;
                   9046:     IndArray *imat;
                   9047:     int *rhead;
                   9048:     int spcol,sprow;
                   9049:     int sugar;
                   9050:     PGeoBucket bucket;
                   9051:     struct oEGT eg0,eg1,eg_f4;
                   9052:
                   9053:     g = 0; d = 0;
                   9054:     for ( i = 0; i < nd_psn; i++ ) {
                   9055:         d = update_pairs(d,g,i,0);
                   9056:         g = update_base(g,i);
                   9057:     }
                   9058:     while ( d ) {
                   9059:         get_eg(&eg0);
                   9060:         l = nd_minsugarp(d,&d);
                   9061:         sugar = SG(l);
                   9062:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   9063:         bucket = create_pbucket();
                   9064:         stat = nd_sp_f4(m,0,l,bucket);
                   9065:         if ( !stat ) {
                   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:         if ( bucket->m < 0 ) continue;
                   9072:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   9073:         if ( !col ) {
                   9074:             for ( t = l; NEXT(t); t = NEXT(t) );
                   9075:             NEXT(t) = d; d = l;
                   9076:             d = nd_reconstruct(1,d);
                   9077:             continue;
                   9078:         }
                   9079:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   9080:         if ( DP_Print )
1.3       noro     9081:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5       noro     9082:                 sugar,eg_f4.exectime);
1.1       noro     9083:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   9084:         if ( !l0 ) continue;
                   9085:         l = l0;
                   9086:
                   9087:         /* over Q */
                   9088:         bucket = create_pbucket();
                   9089:         stat = nd_sp_f4(0,1,l,bucket);
                   9090:         if ( !stat ) {
                   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:         if ( bucket->m < 0 ) continue;
                   9097:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   9098:         if ( !col ) {
                   9099:             for ( t = l; NEXT(t); t = NEXT(t) );
                   9100:             NEXT(t) = d; d = l;
                   9101:             d = nd_reconstruct(1,d);
                   9102:             continue;
                   9103:         }
                   9104:         nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   9105:         /* adding new bases */
                   9106:         for ( r = nflist; r; r = NEXT(r) ) {
                   9107:             nfqv = (NDV)BDY(r);
                   9108:             ndv_removecont(0,nfqv);
1.6       noro     9109:             if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1       noro     9110:             if ( nd_nalg ) {
                   9111:                 ND nf1;
                   9112:
                   9113:                 nf1 = ndvtond(m,nfqv);
                   9114:                 nd_monic(0,&nf1);
                   9115:                 nd_removecont(0,nf1);
                   9116:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   9117:             }
                   9118:             nfv = ndv_dup(0,nfqv);
                   9119:             ndv_mod(m,nfv);
                   9120:             ndv_removecont(m,nfv);
1.24      noro     9121:             nh = ndv_newps(0,nfv,nfqv);
1.1       noro     9122:             d = update_pairs(d,g,nh,0);
                   9123:             g = update_base(g,nh);
                   9124:         }
                   9125:     }
                   9126: #if 0
                   9127:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   9128: #endif
                   9129:   conv_ilist(nd_demand,1,g,indp);
                   9130:     return g;
                   9131: }
                   9132:
                   9133: int rref(matrix mat,int *sugar)
                   9134: {
                   9135:   int row,col,i,j,k,l,s,wcol,wj;
                   9136:   unsigned long bj;
                   9137:   unsigned long **a;
                   9138:   unsigned long *ai,*ak,*as,*t;
                   9139:   int *pivot;
                   9140:
                   9141:   row = mat->row;
                   9142:   col = mat->col;
                   9143:   a = mat->a;
                   9144:   wcol = (col+BLEN-1)/BLEN;
                   9145:   pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   9146:   i = 0;
                   9147:   for ( j = 0; j < col; j++ ) {
                   9148:   wj = j/BLEN; bj = 1L<<(j%BLEN);
                   9149:     for ( k = i; k < row; k++ )
                   9150:     if ( a[k][wj] & bj ) break;
                   9151:     if ( k == row ) continue;
                   9152:   pivot[i] = j;
                   9153:     if ( k != i ) {
                   9154:    t = a[i]; a[i] = a[k]; a[k] = t;
                   9155:    s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
                   9156:   }
                   9157:   ai = a[i];
                   9158:     for ( k = i+1; k < row; k++ ) {
                   9159:     ak = a[k];
                   9160:     if ( ak[wj] & bj ) {
                   9161:       for ( l = wj; l < wcol; l++ )
                   9162:       ak[l] ^= ai[l];
                   9163:       sugar[k] = MAX(sugar[k],sugar[i]);
                   9164:     }
                   9165:   }
                   9166:   i++;
                   9167:   }
                   9168:   for ( k = i-1; k >= 0; k-- ) {
                   9169:     j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
                   9170:   ak = a[k];
                   9171:     for ( s = 0; s < k; s++ ) {
                   9172:     as = a[s];
                   9173:       if ( as[wj] & bj ) {
                   9174:         for ( l = wj; l < wcol; l++ )
                   9175:       as[l] ^= ak[l];
                   9176:       sugar[s] = MAX(sugar[s],sugar[k]);
                   9177:     }
                   9178:   }
                   9179:   }
                   9180:   return i;
                   9181: }
                   9182:
                   9183: void print_matrix(matrix mat)
                   9184: {
                   9185:   int row,col,i,j;
                   9186:   unsigned long *ai;
                   9187:
                   9188:   row = mat->row;
                   9189:   col = mat->col;
                   9190:   printf("%d x %d\n",row,col);
                   9191:   for ( i = 0; i < row; i++ ) {
                   9192:   ai = mat->a[i];
                   9193:     for ( j = 0; j < col; j++ ) {
                   9194:     if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
                   9195:     else putchar('0');
                   9196:   }
                   9197:   putchar('\n');
                   9198:   }
                   9199: }
                   9200:
                   9201: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
                   9202:
                   9203: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
                   9204: {
                   9205:   int row,col,wcol,wj,i,j;
                   9206:   unsigned long bj;
                   9207:   unsigned long *ai;
                   9208:   unsigned long **a;
                   9209:   int len;
                   9210:   int *pos;
                   9211:
                   9212:   row = mat->row;
                   9213:   col = mat->col;
                   9214:   wcol = (col+BLEN-1)/BLEN;
                   9215:   pos = (int *)MALLOC(wcol*sizeof(int));
                   9216:   bzero(pos,wcol*sizeof(int));
                   9217:   for ( i = j = 0; i < wcol; i++ )
                   9218:     if ( v[i] ) pos[j++] = i;;
                   9219:   len = j;
                   9220:   wj = rhead/BLEN;
                   9221:   bj = 1L<<rhead%BLEN;
                   9222:   a = mat->a;
                   9223:   for ( i = 0; i < row; i++ ) {
                   9224:   ai = a[i];
                   9225:     if ( ai[wj]&bj ) {
                   9226:     for ( j = 0; j < len; j++ )
                   9227:       ai[pos[j]] ^= v[pos[j]];
                   9228:     sugar[i] = MAX(sugar[i],rsugar);
                   9229:   }
                   9230:   }
                   9231: }
                   9232:
                   9233: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   9234: {
                   9235:     int nsp,nred,i,i0,k,rank,row;
                   9236:     NODE r0,rp;
                   9237:     ND_pairs sp;
                   9238:   ND spol;
                   9239:   NM_ind_pair rt;
                   9240:     int *s0hash;
                   9241:   UINT *s;
                   9242:   int *pivot,*sugar,*head;
                   9243:   matrix mat;
                   9244:     NM m;
                   9245:     NODE r;
                   9246:   struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
                   9247:   int rhead,rsugar,size;
                   9248:     unsigned long *v;
                   9249:
                   9250:     get_eg(&eg0);
                   9251:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   9252:     nred = length(rp0);
                   9253:     mat = alloc_matrix(nsp,col);
                   9254:     s0hash = (int *)MALLOC(col*sizeof(int));
                   9255:     for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
                   9256:         s0hash[i] = ndl_hash_value(s);
                   9257:
                   9258:   sugar = (int *)MALLOC(nsp*sizeof(int));
                   9259:   for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
                   9260:     nd_sp(2,0,sp,&spol);
                   9261:     if ( spol ) {
                   9262:         mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
                   9263:       sugar[i] = SG(spol);
                   9264:       i++;
                   9265:     }
                   9266:   }
                   9267:   mat->row = i;
                   9268:     if ( DP_Print ) {
                   9269:       fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
                   9270:     }
                   9271:   size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
                   9272:   v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
                   9273:     for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
                   9274:     rt = (NM_ind_pair)BDY(rp);
                   9275:     bzero(v,size);
                   9276:         rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
                   9277:     rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
                   9278:       red_by_vect_2(mat,sugar,v,rhead,rsugar);
                   9279:   }
                   9280:
                   9281:     get_eg(&eg1);
                   9282:     init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
                   9283:   rank = rref(mat,sugar);
                   9284:
                   9285:     for ( i = 0, r0 = 0; i < rank; i++ ) {
                   9286:       NEXTNODE(r0,r);
                   9287:     BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
                   9288:       SG((NDV)BDY(r)) = sugar[i];
                   9289:     }
                   9290:     if ( r0 ) NEXT(r) = 0;
                   9291:     get_eg(&eg2);
                   9292:     init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
                   9293:     if ( DP_Print ) {
                   9294:         fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5       noro     9295:       eg_elim1.exectime,eg_elim2.exectime);
1.1       noro     9296:         fflush(asir_out);
                   9297:   }
                   9298:     return r0;
                   9299: }
                   9300:
                   9301:
                   9302: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   9303: {
                   9304:     IndArray *imat;
1.11      noro     9305:     int nsp,nred,i,start;
1.1       noro     9306:     int *rhead;
                   9307:     NODE r0,rp;
                   9308:     ND_pairs sp;
                   9309:     NM_ind_pair *rvect;
                   9310:     UINT *s;
                   9311:     int *s0hash;
1.11      noro     9312:     struct oEGT eg0,eg1,eg_conv;
1.1       noro     9313:
                   9314:     if ( m == 2 && nd_rref2 )
                   9315:      return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
                   9316:
                   9317:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   9318:     nred = length(rp0);
                   9319:     imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   9320:     rhead = (int *)MALLOC(col*sizeof(int));
                   9321:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   9322:
                   9323:     /* construction of index arrays */
1.11      noro     9324:     get_eg(&eg0);
1.1       noro     9325:     if ( DP_Print ) {
1.11      noro     9326:       fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   9327:       fflush(asir_out);
1.1       noro     9328:     }
                   9329:     rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11      noro     9330:     for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1       noro     9331:         rvect[i] = (NM_ind_pair)BDY(rp);
1.11      noro     9332:         imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1       noro     9333:         rhead[imat[i]->head] = 1;
1.11      noro     9334:         start = imat[i]->head;
                   9335:     }
1.12      noro     9336:     get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11      noro     9337:     if ( DP_Print ) {
                   9338:       fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   9339:       fflush(asir_out);
1.1       noro     9340:     }
                   9341:     if ( m > 0 )
1.7       noro     9342: #if SIZEOF_LONG==8
1.1       noro     9343:         r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   9344: #else
                   9345:         r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   9346: #endif
                   9347:     else if ( m == -1 )
                   9348:         r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   9349:     else if ( m == -2 )
                   9350:         r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   9351:     else
                   9352:         r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   9353:     return r0;
                   9354: }
                   9355:
                   9356: /* for Fp, 2<=p<2^16 */
                   9357:
                   9358: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   9359:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   9360: {
                   9361:     int spcol,sprow,a;
                   9362:     int i,j,k,l,rank;
                   9363:     NODE r0,r;
                   9364:     ND_pairs sp;
                   9365:     ND spol;
                   9366:     UINT **spmat;
                   9367:     UINT *svect,*v;
                   9368:     int *colstat;
                   9369:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9370:     int maxrs;
                   9371:     int *spsugar;
                   9372:     ND_pairs *spactive;
                   9373:
                   9374:     spcol = col-nred;
                   9375:     get_eg(&eg0);
                   9376:     /* elimination (1st step) */
                   9377:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   9378:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   9379:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9380:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   9381:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9382:         nd_sp(m,0,sp,&spol);
                   9383:         if ( !spol ) continue;
                   9384:         nd_to_vect(m,s0vect,col,spol,svect);
                   9385:         if ( m == -1 )
                   9386:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   9387:         else
1.40      noro     9388:             maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,0);
1.1       noro     9389:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9390:         if ( i < col ) {
                   9391:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   9392:             for ( j = k = 0; j < col; j++ )
                   9393:                 if ( !rhead[j] ) v[k++] = svect[j];
                   9394:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9395:             if ( nz )
                   9396:             spactive[sprow] = sp;
                   9397:             sprow++;
                   9398:         }
                   9399:         nd_free(spol);
                   9400:     }
                   9401:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9402:     if ( DP_Print ) {
1.5       noro     9403:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9404:         fflush(asir_out);
                   9405:     }
                   9406:     /* free index arrays */
                   9407:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   9408:
                   9409:     /* elimination (2nd step) */
                   9410:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9411:     if ( m == -1 )
                   9412:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   9413:     else
                   9414:         rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   9415:     r0 = 0;
                   9416:     for ( i = 0; i < rank; i++ ) {
                   9417:         NEXTNODE(r0,r); BDY(r) =
                   9418:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   9419:         SG((NDV)BDY(r)) = spsugar[i];
                   9420:         GCFREE(spmat[i]);
                   9421:     }
                   9422:     if ( r0 ) NEXT(r) = 0;
                   9423:
                   9424:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   9425:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9426:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9427:     if ( DP_Print ) {
1.5       noro     9428:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9429:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9430:             nsp,nred,sprow,spcol,rank);
1.5       noro     9431:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9432:     }
                   9433:     if ( nz ) {
                   9434:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   9435:         if ( rank > 0 ) {
                   9436:             NEXT(spactive[rank-1]) = 0;
                   9437:             *nz = spactive[0];
                   9438:         } else
                   9439:             *nz = 0;
                   9440:     }
                   9441:     return r0;
                   9442: }
                   9443:
1.40      noro     9444: NODE nd_f4_red_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   9445:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
                   9446: {
                   9447:     int spcol,sprow,a;
                   9448:     int i,j,k,l,rank;
                   9449:     NODE r0,r;
                   9450:     ND_pairs sp;
                   9451:     ND spol;
                   9452:     UINT **spmat;
                   9453:     UINT *svect,*cvect;
                   9454:     UINT *v;
                   9455:     int *colstat;
                   9456:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9457:     int maxrs;
                   9458:     int *spsugar;
                   9459:     ND_pairs *spactive;
                   9460:     SIG *spsig;
                   9461:
                   9462:     get_eg(&eg0);
                   9463:     /* elimination (1st step) */
                   9464:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   9465:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9466:     spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
                   9467:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9468:         nd_sp(m,0,sp,&spol);
                   9469:         if ( !spol ) {
                   9470:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
                   9471:           continue;
                   9472:         }
                   9473:         svect = (UINT *)MALLOC(col*sizeof(UINT));
                   9474:         nd_to_vect(m,s0vect,col,spol,svect);
                   9475:         maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred,spol->sig);
                   9476:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9477:         if ( i < col ) {
                   9478:             spmat[sprow] = svect;
                   9479:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9480:             spsig[sprow] = sp->sig;
                   9481:             sprow++;
                   9482:         } else {
                   9483:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
                   9484:         }
                   9485:         nd_free(spol);
                   9486:     }
                   9487:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
                   9488:     if ( DP_Print ) {
                   9489:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   9490:         fflush(asir_out);
                   9491:     }
                   9492:     /* free index arrays */
                   9493:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   9494:
                   9495:     /* elimination (2nd step) */
                   9496:     colstat = (int *)MALLOC(col*sizeof(int));
                   9497:     rank = nd_gauss_elim_mod_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
                   9498:     r0 = 0;
                   9499:     for ( i = 0; i < sprow; i++ ) {
                   9500:         if ( spsugar[i] >= 0 ) {
                   9501:           NEXTNODE(r0,r);
                   9502:           BDY(r) = vect_to_ndv_s(spmat[i],col,s0vect);
                   9503:           SG((NDV)BDY(r)) = spsugar[i];
                   9504:           ((NDV)BDY(r))->sig = spsig[i];
                   9505:         } else
                   9506:           syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
                   9507:         GCFREE(spmat[i]);
                   9508:     }
                   9509:     if ( r0 ) NEXT(r) = 0;
                   9510:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
                   9511:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9512:     if ( DP_Print ) {
                   9513:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   9514:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9515:             nsp,nred,sprow,col,rank);
                   9516:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   9517:     }
                   9518:     return r0;
                   9519: }
                   9520:
1.1       noro     9521:
                   9522: /* for small finite fields */
                   9523:
                   9524: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   9525:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   9526: {
                   9527:     int spcol,sprow,a;
                   9528:     int i,j,k,l,rank;
                   9529:     NODE r0,r;
                   9530:     ND_pairs sp;
                   9531:     ND spol;
                   9532:     UINT **spmat;
                   9533:     UINT *svect,*v;
                   9534:     int *colstat;
                   9535:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9536:     int maxrs;
                   9537:     int *spsugar;
                   9538:     ND_pairs *spactive;
                   9539:
                   9540:     spcol = col-nred;
                   9541:     get_eg(&eg0);
                   9542:     /* elimination (1st step) */
                   9543:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   9544:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   9545:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9546:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   9547:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9548:         nd_sp(m,0,sp,&spol);
                   9549:         if ( !spol ) continue;
                   9550:         nd_to_vect(m,s0vect,col,spol,svect);
                   9551:         maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   9552:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9553:         if ( i < col ) {
                   9554:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   9555:             for ( j = k = 0; j < col; j++ )
                   9556:                 if ( !rhead[j] ) v[k++] = svect[j];
                   9557:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9558:             if ( nz )
                   9559:             spactive[sprow] = sp;
                   9560:             sprow++;
                   9561:         }
                   9562:         nd_free(spol);
                   9563:     }
                   9564:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9565:     if ( DP_Print ) {
1.5       noro     9566:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9567:         fflush(asir_out);
                   9568:     }
                   9569:     /* free index arrays */
                   9570:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   9571:
                   9572:     /* elimination (2nd step) */
                   9573:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9574:     rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   9575:     r0 = 0;
                   9576:     for ( i = 0; i < rank; i++ ) {
                   9577:         NEXTNODE(r0,r); BDY(r) =
                   9578:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   9579:         SG((NDV)BDY(r)) = spsugar[i];
                   9580:         GCFREE(spmat[i]);
                   9581:     }
                   9582:     if ( r0 ) NEXT(r) = 0;
                   9583:
                   9584:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   9585:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9586:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9587:     if ( DP_Print ) {
1.5       noro     9588:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9589:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9590:             nsp,nred,sprow,spcol,rank);
1.5       noro     9591:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9592:     }
                   9593:     if ( nz ) {
                   9594:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   9595:         if ( rank > 0 ) {
                   9596:             NEXT(spactive[rank-1]) = 0;
                   9597:             *nz = spactive[0];
                   9598:         } else
                   9599:             *nz = 0;
                   9600:     }
                   9601:     return r0;
                   9602: }
                   9603:
                   9604: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   9605:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   9606: {
                   9607:     int spcol,sprow,a;
                   9608:     int i,j,k,l,rank;
                   9609:     NODE r0,r;
                   9610:     ND_pairs sp;
                   9611:     ND spol;
                   9612:     mpz_t **spmat;
                   9613:     mpz_t *svect,*v;
                   9614:     int *colstat;
                   9615:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9616:     int maxrs;
                   9617:     int *spsugar;
                   9618:     pointer *w;
                   9619:
                   9620:     spcol = col-nred;
                   9621:     get_eg(&eg0);
                   9622:     /* elimination (1st step) */
                   9623:     spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
                   9624:     svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   9625:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9626:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9627:         nd_sp(m,trace,sp,&spol);
                   9628:         if ( !spol ) continue;
                   9629:         nd_to_vect_lf(s0vect,col,spol,svect);
                   9630:         maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
                   9631:         for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
                   9632:         if ( i < col ) {
                   9633:             spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
                   9634:             for ( j = k = 0; j < col; j++ )
                   9635:                 if ( !rhead[j] ) v[k++][0] = svect[j][0];
                   9636:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9637:             sprow++;
                   9638:         }
                   9639: /*        nd_free(spol); */
                   9640:     }
                   9641:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9642:     if ( DP_Print ) {
1.5       noro     9643:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9644:         fflush(asir_out);
                   9645:     }
                   9646:     /* free index arrays */
                   9647: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   9648:
                   9649:     /* elimination (2nd step) */
                   9650:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9651:     rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
                   9652:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   9653:     for ( i = 0; i < rank; i++ ) {
                   9654: #if 0
                   9655:         w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   9656:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   9657: #else
                   9658:         w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   9659:         SG((NDV)w[i]) = spsugar[i];
                   9660: #endif
                   9661: /*        GCFREE(spmat[i]); */
                   9662:
                   9663:     }
                   9664: #if 0
                   9665:     qsort(w,rank,sizeof(NDV),
                   9666:         (int (*)(const void *,const void *))ndv_compare);
                   9667: #endif
                   9668:     r0 = 0;
                   9669:     for ( i = 0; i < rank; i++ ) {
                   9670:         NEXTNODE(r0,r); BDY(r) = w[i];
                   9671:     }
                   9672:     if ( r0 ) NEXT(r) = 0;
                   9673:
                   9674: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   9675:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9676:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9677:     if ( DP_Print ) {
1.5       noro     9678:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9679:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9680:             nsp,nred,sprow,spcol,rank);
1.5       noro     9681:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9682:     }
                   9683:     return r0;
                   9684: }
                   9685:
                   9686: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   9687:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   9688: {
                   9689:     int spcol,sprow,a;
                   9690:     int i,j,k,l,rank;
                   9691:     NODE r0,r;
                   9692:     ND_pairs sp;
                   9693:     ND spol;
                   9694:     Z **spmat;
                   9695:     Z *svect,*v;
                   9696:     int *colstat;
                   9697:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   9698:     int maxrs;
                   9699:     int *spsugar;
                   9700:     pointer *w;
                   9701:
                   9702:     spcol = col-nred;
                   9703:     get_eg(&eg0);
                   9704:     /* elimination (1st step) */
                   9705:     spmat = (Z **)MALLOC(nsp*sizeof(Q *));
                   9706:     svect = (Z *)MALLOC(col*sizeof(Q));
                   9707:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   9708:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   9709:         nd_sp(0,trace,sp,&spol);
                   9710:         if ( !spol ) continue;
                   9711:         nd_to_vect_q(s0vect,col,spol,svect);
                   9712:         maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
                   9713:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   9714:         if ( i < col ) {
                   9715:             spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
                   9716:             for ( j = k = 0; j < col; j++ )
                   9717:                 if ( !rhead[j] ) v[k++] = svect[j];
                   9718:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   9719:             sprow++;
                   9720:         }
                   9721: /*        nd_free(spol); */
                   9722:     }
                   9723:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   9724:     if ( DP_Print ) {
1.5       noro     9725:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     9726:         fflush(asir_out);
                   9727:     }
                   9728:     /* free index arrays */
                   9729: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   9730:
                   9731:     /* elimination (2nd step) */
                   9732:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   9733:     rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
                   9734:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   9735:     for ( i = 0; i < rank; i++ ) {
                   9736: #if 0
                   9737:         w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   9738:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   9739: #else
                   9740:         w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   9741:         SG((NDV)w[i]) = spsugar[i];
                   9742: #endif
                   9743: /*        GCFREE(spmat[i]); */
                   9744:     }
                   9745: #if 0
                   9746:     qsort(w,rank,sizeof(NDV),
                   9747:         (int (*)(const void *,const void *))ndv_compare);
                   9748: #endif
                   9749:     r0 = 0;
                   9750:     for ( i = 0; i < rank; i++ ) {
                   9751:         NEXTNODE(r0,r); BDY(r) = w[i];
                   9752:     }
                   9753:     if ( r0 ) NEXT(r) = 0;
                   9754:
                   9755: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   9756:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   9757:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   9758:     if ( DP_Print ) {
1.5       noro     9759:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     9760:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   9761:             nsp,nred,sprow,spcol,rank);
1.5       noro     9762:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     9763:     }
                   9764:     return r0;
                   9765: }
                   9766:
                   9767: FILE *nd_write,*nd_read;
                   9768:
                   9769: void nd_send_int(int a) {
                   9770:     write_int(nd_write,(unsigned int *)&a);
                   9771: }
                   9772:
                   9773: void nd_send_intarray(int *p,int len) {
                   9774:     write_intarray(nd_write,(unsigned int *)p,len);
                   9775: }
                   9776:
                   9777: int nd_recv_int() {
                   9778:     int a;
                   9779:
                   9780:     read_int(nd_read,(unsigned int *)&a);
                   9781:     return a;
                   9782: }
                   9783:
                   9784: void nd_recv_intarray(int *p,int len) {
                   9785:     read_intarray(nd_read,(unsigned int *)p,len);
                   9786: }
                   9787:
                   9788: void nd_send_ndv(NDV p) {
                   9789:     int len,i;
                   9790:     NMV m;
                   9791:
                   9792:     if ( !p ) nd_send_int(0);
                   9793:     else {
                   9794:         len = LEN(p);
                   9795:         nd_send_int(len);
                   9796:         m = BDY(p);
                   9797:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9798:             nd_send_int(CM(m));
                   9799:             nd_send_intarray((int *)DL(m),nd_wpd);
                   9800:         }
                   9801:     }
                   9802: }
                   9803:
                   9804: void nd_send_nd(ND p) {
                   9805:     int len,i;
                   9806:     NM m;
                   9807:
                   9808:     if ( !p ) nd_send_int(0);
                   9809:     else {
                   9810:         len = LEN(p);
                   9811:         nd_send_int(len);
                   9812:         m = BDY(p);
                   9813:         for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   9814:             nd_send_int(CM(m));
                   9815:             nd_send_intarray((int *)DL(m),nd_wpd);
                   9816:         }
                   9817:     }
                   9818: }
                   9819:
                   9820: NDV nd_recv_ndv()
                   9821: {
                   9822:     int len,i;
                   9823:     NMV m,m0;
                   9824:     NDV r;
                   9825:
                   9826:     len = nd_recv_int();
                   9827:     if ( !len ) return 0;
                   9828:     else {
                   9829:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   9830: #if 0
                   9831:         ndv_alloc += len*nmv_adv;
                   9832: #endif
                   9833:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9834:             CM(m) = nd_recv_int();
                   9835:             nd_recv_intarray((int *)DL(m),nd_wpd);
                   9836:         }
                   9837:         MKNDV(nd_nvar,m0,len,r);
                   9838:         return r;
                   9839:     }
                   9840: }
                   9841:
                   9842: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
                   9843: {
                   9844:     int i,j,t,c,rank,inv;
                   9845:     int *ci,*ri;
                   9846:     Z dn;
                   9847:     MAT m,nm;
                   9848:
                   9849:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
                   9850:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   9851:     for ( i = 0; i < row; i++ )
                   9852:         for ( j = 0; j < col; j++ )
                   9853:             mat0[i][j] = 0;
                   9854:     c = col-rank;
                   9855:     for ( i = 0; i < rank; i++ ) {
                   9856:         mat0[i][ri[i]] = dn;
                   9857:         for ( j = 0; j < c; j++ )
                   9858:             mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
                   9859:     }
                   9860:     return rank;
                   9861: }
                   9862:
                   9863: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   9864: {
                   9865:     int i,j,k,l,inv,a,rank,s;
                   9866:     unsigned int *t,*pivot,*pk;
                   9867:     unsigned int **mat;
                   9868:     ND_pairs pair;
                   9869:
                   9870:     mat = (unsigned int **)mat0;
                   9871:     for ( rank = 0, j = 0; j < col; j++ ) {
                   9872:         for ( i = rank; i < row; i++ )
                   9873:             mat[i][j] %= md;
                   9874:         for ( i = rank; i < row; i++ )
                   9875:             if ( mat[i][j] )
                   9876:                 break;
                   9877:         if ( i == row ) {
                   9878:             colstat[j] = 0;
                   9879:             continue;
                   9880:         } else
                   9881:             colstat[j] = 1;
                   9882:         if ( i != rank ) {
                   9883:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   9884:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   9885:             if ( spactive ) {
                   9886:                 pair = spactive[i]; spactive[i] = spactive[rank];
                   9887:                 spactive[rank] = pair;
                   9888:             }
                   9889:         }
                   9890:         pivot = mat[rank];
                   9891:         s = sugar[rank];
                   9892:         inv = invm(pivot[j],md);
                   9893:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   9894:             if ( *pk ) {
                   9895:                 if ( *pk >= (unsigned int)md )
                   9896:                     *pk %= md;
                   9897:                 DMAR(*pk,inv,0,md,*pk)
                   9898:             }
                   9899:         for ( i = rank+1; i < row; i++ ) {
                   9900:             t = mat[i];
                   9901:             if ( (a = t[j]) != 0 ) {
                   9902:                 sugar[i] = MAX(sugar[i],s);
                   9903:                 red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   9904:             }
                   9905:         }
                   9906:         rank++;
                   9907:     }
                   9908:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   9909:         if ( colstat[j] ) {
                   9910:             pivot = mat[l];
                   9911:             s = sugar[l];
                   9912:             for ( i = 0; i < l; i++ ) {
                   9913:                 t = mat[i];
                   9914:                 t[j] %= md;
                   9915:                 if ( (a = t[j]) != 0 ) {
                   9916:                     sugar[i] = MAX(sugar[i],s);
                   9917:                     red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   9918:                 }
                   9919:             }
                   9920:             l--;
                   9921:         }
                   9922:     for ( j = 0, l = 0; l < rank; j++ )
                   9923:         if ( colstat[j] ) {
                   9924:             t = mat[l];
                   9925:             for ( k = j; k < col; k++ )
                   9926:                 if ( t[k] >= (unsigned int)md )
                   9927:                     t[k] %= md;
                   9928:             l++;
                   9929:         }
                   9930:     return rank;
                   9931: }
                   9932:
1.40      noro     9933: int nd_gauss_elim_mod_s(UINT **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat,SIG *sig)
                   9934: {
                   9935:   int i,j,k,l,rank,s,imin;
                   9936:   UINT inv;
                   9937:   UINT a;
                   9938:   UINT *t,*pivot,*pk;
                   9939:   UINT *ck;
                   9940:   UINT *ct;
                   9941:   ND_pairs pair;
                   9942:   SIG sg;
                   9943:   int *used;
                   9944:
                   9945:   used = (int *)MALLOC(row*sizeof(int));
                   9946:   for ( j = 0; j < col; j++ ) {
                   9947:     for ( i = 0; i < row; i++ )
                   9948:       a = mat[i][j] %= md;
                   9949:     for ( i = 0; i < row; i++ )
                   9950:       if ( !used[i] && mat[i][j] ) break;
                   9951:     if ( i == row ) {
                   9952:       colstat[j] = 0;
                   9953:       continue;
                   9954:     } else {
                   9955:       colstat[j] = 1;
                   9956:       used[i] = 1;
                   9957:     }
                   9958:     /* column j is normalized */
                   9959:     s = sugar[i];
                   9960:     inv = invm(mat[i][j],md);
                   9961:     /* normalize pivot row */
                   9962:     for ( k = j, pk = mat[i]+j; k < col; k++, pk++, ck++ ) {
                   9963:       DMAR(*pk,inv,0,md,*pk);
                   9964:     }
                   9965:     for ( k = i+1; k < row; k++ ) {
                   9966:       if ( (a = mat[k][j]) != 0 ) {
                   9967:         sugar[k] = MAX(sugar[k],s);
                   9968:         red_by_vect(md,mat[k]+j,mat[i]+j,(int)(md-a),col-j);
                   9969:         Nf4_red++;
                   9970:       }
                   9971:     }
                   9972:   }
                   9973:   rank = 0;
                   9974:   for ( i = 0; i < row; i++ ) {
                   9975:     for ( j = 0; j < col; j++ )
                   9976:       if ( mat[i][j] ) break;
                   9977:     if ( j == col ) sugar[i] = -1;
                   9978:     else rank++;
                   9979:   }
                   9980:   return rank;
                   9981: }
                   9982:
1.1       noro     9983:
1.7       noro     9984: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1       noro     9985: {
1.7       noro     9986:     int i,j,k,l,inv,a,rank,s;
                   9987:     unsigned int *t,*pivot,*pk;
                   9988:     unsigned int **mat;
                   9989:
                   9990:     mat = (unsigned int **)mat0;
                   9991:     for ( rank = 0, j = 0; j < col; j++ ) {
                   9992:         for ( i = rank; i < row; i++ )
                   9993:             if ( mat[i][j] )
                   9994:                 break;
                   9995:         if ( i == row ) {
                   9996:             colstat[j] = 0;
                   9997:             continue;
                   9998:         } else
                   9999:             colstat[j] = 1;
                   10000:         if ( i != rank ) {
                   10001:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   10002:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   10003:         }
                   10004:         pivot = mat[rank];
                   10005:         s = sugar[rank];
                   10006:         inv = _invsf(pivot[j]);
                   10007:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   10008:             if ( *pk )
                   10009:                 *pk = _mulsf(*pk,inv);
                   10010:         for ( i = rank+1; i < row; i++ ) {
                   10011:             t = mat[i];
                   10012:             if ( (a = t[j]) != 0 ) {
                   10013:                 sugar[i] = MAX(sugar[i],s);
                   10014:                 red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   10015:             }
                   10016:         }
                   10017:         rank++;
                   10018:     }
                   10019:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   10020:         if ( colstat[j] ) {
                   10021:             pivot = mat[l];
                   10022:             s = sugar[l];
                   10023:             for ( i = 0; i < l; i++ ) {
                   10024:                 t = mat[i];
                   10025:                 if ( (a = t[j]) != 0 ) {
                   10026:                     sugar[i] = MAX(sugar[i],s);
                   10027:                     red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   10028:                 }
                   10029:             }
                   10030:             l--;
                   10031:         }
                   10032:     return rank;
                   10033: }
1.1       noro     10034:
1.7       noro     10035: int ndv_ishomo(NDV p)
                   10036: {
                   10037:     NMV m;
                   10038:     int len,h;
1.1       noro     10039:
                   10040:     if ( !p ) return 1;
                   10041:     len = LEN(p);
                   10042:     m = BDY(p);
                   10043:     h = TD(DL(m));
                   10044:     NMV_ADV(m);
                   10045:     for ( len--; len; len--, NMV_ADV(m) )
1.20      noro     10046:         if ( TD(DL(m)) != h ) {
                   10047:           return 0;
                   10048:         }
1.1       noro     10049:     return 1;
                   10050: }
                   10051:
                   10052: void ndv_save(NDV p,int index)
                   10053: {
                   10054:     FILE *s;
                   10055:     char name[BUFSIZ];
                   10056:     short id;
                   10057:     int nv,sugar,len,n,i,td,e,j;
                   10058:     NMV m;
                   10059:     unsigned int *dl;
                   10060:     int mpos;
                   10061:
                   10062:     sprintf(name,"%s/%d",Demand,index);
                   10063:     s = fopen(name,"w");
                   10064:     savevl(s,0);
                   10065:     if ( !p ) {
                   10066:         saveobj(s,0);
                   10067:         return;
                   10068:     }
                   10069:     id = O_DP;
                   10070:     nv = NV(p);
                   10071:     sugar = SG(p);
                   10072:     len = LEN(p);
                   10073:     write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
                   10074:     write_int(s,(unsigned int *)&len);
                   10075:
                   10076:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     10077:         saveobj(s,(Obj)CZ(m));
1.1       noro     10078:         dl = DL(m);
                   10079:         td = TD(dl);
                   10080:         write_int(s,(unsigned int *)&td);
                   10081:         for ( j = 0; j < nv; j++ ) {
                   10082:             e = GET_EXP(dl,j);
                   10083:             write_int(s,(unsigned int *)&e);
                   10084:         }
                   10085:         if ( nd_module ) {
                   10086:             mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
                   10087:         }
                   10088:     }
                   10089:     fclose(s);
                   10090: }
                   10091:
                   10092: void nd_save_mod(ND p,int index)
                   10093: {
                   10094:     FILE *s;
                   10095:     char name[BUFSIZ];
                   10096:     int nv,sugar,len,c;
                   10097:     NM m;
                   10098:
                   10099:     sprintf(name,"%s/%d",Demand,index);
                   10100:     s = fopen(name,"w");
                   10101:     if ( !p ) {
                   10102:     len = 0;
                   10103:       write_int(s,(unsigned int *)&len);
                   10104:     fclose(s);
                   10105:         return;
                   10106:     }
                   10107:     nv = NV(p);
                   10108:     sugar = SG(p);
                   10109:     len = LEN(p);
                   10110:     write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
                   10111:   for ( m = BDY(p); m; m = NEXT(m) ) {
                   10112:     c = CM(m); write_int(s,(unsigned int *)&c);
                   10113:     write_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   10114:   }
                   10115:     fclose(s);
                   10116: }
                   10117:
                   10118: NDV ndv_load(int index)
                   10119: {
                   10120:     FILE *s;
                   10121:     char name[BUFSIZ];
                   10122:     short id;
                   10123:     int nv,sugar,len,n,i,td,e,j;
                   10124:     NDV d;
                   10125:     NMV m0,m;
                   10126:     unsigned int *dl;
                   10127:     Obj obj;
                   10128:     int mpos;
                   10129:
                   10130:     sprintf(name,"%s/%d",Demand,index);
                   10131:     s = fopen(name,"r");
                   10132:     if ( !s ) return 0;
                   10133:
                   10134:     skipvl(s);
                   10135:     read_short(s,(unsigned short *)&id);
                   10136:     if ( !id ) return 0;
                   10137:     read_int(s,(unsigned int *)&nv);
                   10138:     read_int(s,(unsigned int *)&sugar);
                   10139:     read_int(s,(unsigned int *)&len);
                   10140:
                   10141:     m0 = m = MALLOC(len*nmv_adv);
                   10142:     for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     10143:         loadobj(s,&obj); CZ(m) = (Z)obj;
1.1       noro     10144:         dl = DL(m);
                   10145:         ndl_zero(dl);
                   10146:         read_int(s,(unsigned int *)&td); TD(dl) = td;
                   10147:         for ( j = 0; j < nv; j++ ) {
                   10148:             read_int(s,(unsigned int *)&e);
                   10149:             PUT_EXP(dl,j,e);
                   10150:         }
                   10151:         if ( nd_module ) {
                   10152:             read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
                   10153:         }
                   10154:         if ( nd_blockmask ) ndl_weight_mask(dl);
                   10155:     }
                   10156:     fclose(s);
                   10157:     MKNDV(nv,m0,len,d);
                   10158:     SG(d) = sugar;
                   10159:     return d;
                   10160: }
                   10161:
                   10162: ND nd_load_mod(int index)
                   10163: {
                   10164:     FILE *s;
                   10165:     char name[BUFSIZ];
                   10166:     int nv,sugar,len,i,c;
                   10167:   ND d;
                   10168:     NM m0,m;
                   10169:
                   10170:     sprintf(name,"%s/%d",Demand,index);
                   10171:     s = fopen(name,"r");
                   10172:   /* if the file does not exist, it means p[index]=0 */
                   10173:     if ( !s ) return 0;
                   10174:
                   10175:     read_int(s,(unsigned int *)&nv);
                   10176:   if ( !nv ) { fclose(s); return 0; }
                   10177:
                   10178:     read_int(s,(unsigned int *)&sugar);
                   10179:     read_int(s,(unsigned int *)&len);
                   10180:   for ( m0 = 0, i = 0; i < len; i++ ) {
                   10181:     NEXTNM(m0,m);
                   10182:     read_int(s,(unsigned int *)&c); CM(m) = c;
                   10183:     read_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   10184:   }
                   10185:   NEXT(m) = 0;
                   10186:     MKND(nv,m0,len,d);
                   10187:     SG(d) = sugar;
                   10188:   fclose(s);
                   10189:     return d;
                   10190: }
                   10191:
                   10192: void nd_det(int mod,MAT f,P *rp)
                   10193: {
                   10194:     VL fv,tv;
                   10195:     int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
                   10196:     pointer **m;
                   10197:     P **w;
                   10198:     P mp,r;
                   10199:     NDV **dm;
                   10200:     NDV *t,*mi,*mj;
                   10201:     NDV d,s,mij,mjj;
                   10202:     ND u;
                   10203:     NMV nmv;
                   10204:     UINT *bound;
                   10205:     PGeoBucket bucket;
                   10206:     struct order_spec *ord;
                   10207:     Z dq,dt,ds;
                   10208:     Z mone;
                   10209:     Z gn,qn,dn0,nm,dn;
                   10210:
                   10211:     create_order_spec(0,0,&ord);
                   10212:     nd_init_ord(ord);
                   10213:     get_vars((Obj)f,&fv);
                   10214:     if ( f->row != f->col )
                   10215:         error("nd_det : non-square matrix");
                   10216:     n = f->row;
                   10217:     m = f->body;
                   10218:     for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   10219:
                   10220:     if ( !nvar ) {
                   10221:         if ( !mod )
                   10222:             detp(CO,(P **)m,n,rp);
                   10223:         else {
                   10224:             w = (P **)almat_pointer(n,n);
                   10225:             for ( i = 0; i < n; i++ )
                   10226:                 for ( j = 0; j < n; j++ )
                   10227:                     ptomp(mod,(P)m[i][j],&w[i][j]);
                   10228:             detmp(CO,mod,w,n,&mp);
                   10229:             mptop(mp,rp);
                   10230:         }
                   10231:         return;
                   10232:     }
                   10233:
                   10234:     if ( !mod ) {
                   10235:         w = (P **)almat_pointer(n,n);
                   10236:         dq = ONE;
                   10237:         for ( i = 0; i < n; i++ ) {
                   10238:             dn0 = ONE;
                   10239:             for ( j = 0; j < n; j++ ) {
                   10240:                 if ( !m[i][j] ) continue;
                   10241:                 lgp(m[i][j],&nm,&dn);
1.6       noro     10242:                 gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1       noro     10243:             }
                   10244:             if ( !UNIZ(dn0) ) {
                   10245:                 ds = dn0;
                   10246:                 for ( j = 0; j < n; j++ )
                   10247:                     mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   10248:                 mulz(dq,ds,&dt); dq = dt;
                   10249:             } else
                   10250:                 for ( j = 0; j < n; j++ )
                   10251:                     w[i][j] = (P)m[i][j];
                   10252:         }
                   10253:         m = (pointer **)w;
                   10254:     }
                   10255:
                   10256:     for ( i = 0, max = 1; i < n; i++ )
                   10257:         for ( j = 0; j < n; j++ )
                   10258:             for ( tv = fv; tv; tv = NEXT(tv) ) {
                   10259:                 e = getdeg(tv->v,(P)m[i][j]);
                   10260:                 max = MAX(e,max);
                   10261:             }
                   10262:     nd_setup_parameters(nvar,max);
                   10263:     dm = (NDV **)almat_pointer(n,n);
                   10264:     for ( i = 0, max = 1; i < n; i++ )
                   10265:         for ( j = 0; j < n; j++ ) {
                   10266:             dm[i][j] = ptondv(CO,fv,m[i][j]);
                   10267:             if ( mod ) ndv_mod(mod,dm[i][j]);
                   10268:             if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   10269:         }
                   10270:     d = ptondv(CO,fv,(P)ONE);
                   10271:     if ( mod ) ndv_mod(mod,d);
                   10272:     chsgnz(ONE,&mone);
                   10273:     for ( j = 0, sgn = 1; j < n; j++ ) {
                   10274:       if ( DP_Print ) {
                   10275:         fprintf(asir_out,".");
                   10276:       }
                   10277:         for ( i = j; i < n && !dm[i][j]; i++ );
                   10278:         if ( i == n ) {
                   10279:             *rp = 0;
                   10280:             return;
                   10281:         }
                   10282:         k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
                   10283:         for ( k = j; k < n; k++ )
                   10284:             for ( l = j; l < n; l++ )
                   10285:                 if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   10286:                     k0 = k; l0 = l; len0 = LEN(dm[k][l]);
                   10287:                 }
                   10288:         if ( k0 != j ) {
                   10289:             t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   10290:             sgn = -sgn;
                   10291:         }
                   10292:         if ( l0 != j ) {
                   10293:             for ( k = j; k < n; k++ ) {
                   10294:                 s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   10295:             }
                   10296:             sgn = -sgn;
                   10297:         }
                   10298:         bound = nd_det_compute_bound(dm,n,j);
                   10299:         for ( k = 0; k < nd_nvar; k++ )
                   10300:             if ( bound[k]*2 > nd_mask0 ) break;
                   10301:         if ( k < nd_nvar )
                   10302:             nd_det_reconstruct(dm,n,j,d);
                   10303:
                   10304:         for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
                   10305: /*            if ( DP_Print ) fprintf(asir_out,"    i=%d\n        ",i); */
                   10306:             mi = dm[i]; mij = mi[j];
                   10307:             if ( mod )
                   10308:                 ndv_mul_c(mod,mij,mod-1);
                   10309:             else
                   10310:                 ndv_mul_c_q(mij,mone);
                   10311:             for ( k = j+1; k < n; k++ ) {
                   10312: /*                if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
                   10313:                 bucket = create_pbucket();
                   10314:                 if ( mi[k] ) {
                   10315:                     nmv = BDY(mjj); len = LEN(mjj);
                   10316:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   10317:                         u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   10318:                         add_pbucket(mod,bucket,u);
                   10319:                     }
                   10320:                 }
                   10321:                 if ( mj[k] && mij ) {
                   10322:                     nmv = BDY(mij); len = LEN(mij);
                   10323:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   10324:                         u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   10325:                         add_pbucket(mod,bucket,u);
                   10326:                     }
                   10327:                 }
                   10328:                 u = nd_quo(mod,bucket,d);
                   10329:                 mi[k] = ndtondv(mod,u);
                   10330:             }
                   10331: /*            if ( DP_Print ) fprintf(asir_out,"\n",k); */
                   10332:         }
                   10333:         d = mjj;
                   10334:     }
                   10335:     if ( DP_Print ) {
                   10336:       fprintf(asir_out,"\n");
                   10337:     }
                   10338:     if ( sgn < 0 ) {
                   10339:         if ( mod )
                   10340:             ndv_mul_c(mod,d,mod-1);
                   10341:         else
                   10342:             ndv_mul_c_q(d,mone);
                   10343:     }
                   10344:     r = ndvtop(mod,CO,fv,d);
                   10345:     if ( !mod && !UNIQ(dq) )
                   10346:         divsp(CO,r,(P)dq,rp);
                   10347:     else
                   10348:         *rp = r;
                   10349: }
                   10350:
                   10351: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
                   10352: {
                   10353:     NM mr,mr0;
                   10354:     NM tnm;
                   10355:     NMV m;
                   10356:     UINT *d0,*dt,*dm;
                   10357:     int c,n,td,i,c1,c2,len;
                   10358:     Z q;
                   10359:     ND r;
                   10360:
                   10361:     if ( !p ) return 0;
                   10362:     else {
                   10363:         n = NV(p); m = BDY(p); len = LEN(p);
                   10364:         d0 = DL(m0);
                   10365:         td = TD(d);
                   10366:         mr0 = 0;
                   10367:         NEWNM(tnm);
                   10368:         if ( mod ) {
                   10369:             c = CM(m0);
                   10370:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   10371:                 ndl_add(DL(m),d0,DL(tnm));
                   10372:                 if ( ndl_reducible(DL(tnm),d) ) {
                   10373:                     NEXTNM(mr0,mr);
                   10374:                     c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
                   10375:                     ndl_copy(DL(tnm),DL(mr));
                   10376:                 }
                   10377:             }
                   10378:         } else {
1.6       noro     10379:             q = CZ(m0);
1.1       noro     10380:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   10381:                 ndl_add(DL(m),d0,DL(tnm));
                   10382:                 if ( ndl_reducible(DL(tnm),d) ) {
                   10383:                     NEXTNM(mr0,mr);
1.6       noro     10384:                     mulz(CZ(m),q,&CZ(mr));
1.1       noro     10385:                     ndl_copy(DL(tnm),DL(mr));
                   10386:                 }
                   10387:             }
                   10388:         }
                   10389:         if ( !mr0 )
                   10390:             return 0;
                   10391:         else {
                   10392:             NEXT(mr) = 0;
                   10393:             for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   10394:             MKND(NV(p),mr0,len,r);
                   10395:             SG(r) = SG(p) + TD(d0);
                   10396:             return r;
                   10397:         }
                   10398:     }
                   10399: }
                   10400:
                   10401: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   10402: {
                   10403:     int i,obpe,oadv,h,k,l;
                   10404:     static NM prev_nm_free_list;
                   10405:     EPOS oepos;
                   10406:
                   10407:     obpe = nd_bpe;
                   10408:     oadv = nmv_adv;
                   10409:     oepos = nd_epos;
                   10410:     if ( obpe < 2 ) nd_bpe = 2;
                   10411:     else if ( obpe < 3 ) nd_bpe = 3;
                   10412:     else if ( obpe < 4 ) nd_bpe = 4;
                   10413:     else if ( obpe < 5 ) nd_bpe = 5;
                   10414:     else if ( obpe < 6 ) nd_bpe = 6;
                   10415:     else if ( obpe < 8 ) nd_bpe = 8;
                   10416:     else if ( obpe < 10 ) nd_bpe = 10;
                   10417:     else if ( obpe < 16 ) nd_bpe = 16;
                   10418:     else if ( obpe < 32 ) nd_bpe = 32;
                   10419:     else error("nd_det_reconstruct : exponent too large");
                   10420:
                   10421:     nd_setup_parameters(nd_nvar,0);
                   10422:     prev_nm_free_list = _nm_free_list;
                   10423:     _nm_free_list = 0;
                   10424:     for ( k = j; k < n; k++ )
                   10425:         for (l = j; l < n; l++ )
                   10426:             ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   10427:     ndv_realloc(d,obpe,oadv,oepos);
                   10428:     prev_nm_free_list = 0;
                   10429: #if 0
                   10430:     GC_gcollect();
                   10431: #endif
                   10432: }
                   10433:
                   10434: /* returns a UINT array containing degree bounds */
                   10435:
                   10436: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   10437: {
                   10438:     UINT *d0,*d1,*d,*t,*r;
                   10439:     int k,l,i;
                   10440:
                   10441:     d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
                   10442:     for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
                   10443:     for ( k = j; k < n; k++ )
                   10444:         for ( l = j; l < n; l++ )
                   10445:             if ( dm[k][l] ) {
                   10446:                 d = ndv_compute_bound(dm[k][l]);
                   10447:                 for ( i = 0; i < nd_nvar; i++ )
                   10448:                     d0[i] = MAX(d0[i],d[i]);
                   10449:             }
                   10450:     return d0;
                   10451: }
                   10452:
                   10453: DL nd_separate_d(UINT *d,UINT *trans)
                   10454: {
                   10455:     int n,td,i,e,j;
                   10456:     DL a;
                   10457:
                   10458:     ndl_zero(trans);
                   10459:     td = 0;
                   10460:     for ( i = 0; i < nd_ntrans; i++ ) {
                   10461:         e = GET_EXP(d,i);
                   10462:         PUT_EXP(trans,i,e);
                   10463:         td += MUL_WEIGHT(e,i);
                   10464:     }
                   10465:     if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   10466:         /* homogenized */
                   10467:         i = nd_nvar-1;
                   10468:         e = GET_EXP(d,i);
                   10469:         PUT_EXP(trans,i,e);
                   10470:         td += MUL_WEIGHT(e,i);
                   10471:     }
                   10472:     TD(trans) = td;
                   10473:     if ( nd_blockmask) ndl_weight_mask(trans);
                   10474:     NEWDL(a,nd_nalg);
                   10475:     td = 0;
                   10476:     for ( i = 0; i < nd_nalg; i++ ) {
                   10477:         j = nd_ntrans+i;
                   10478:         e = GET_EXP(d,j);
                   10479:         a->d[i] = e;
                   10480:         td += e;
                   10481:     }
                   10482:     a->td = td;
                   10483:     return a;
                   10484: }
                   10485:
                   10486: int nd_monic(int mod,ND *p)
                   10487: {
                   10488:     UINT *trans,*t;
                   10489:     DL alg;
                   10490:     MP mp0,mp;
                   10491:     NM m,m0,m1,ma0,ma,mb,mr0,mr;
                   10492:     ND r;
                   10493:     DL dl;
                   10494:     DP nm;
                   10495:     NDV ndv;
                   10496:     DAlg inv,cd;
                   10497:     ND s,c;
                   10498:     Z l,mul;
                   10499:     Z ln;
                   10500:     int n,ntrans,i,e,td,is_lc,len;
                   10501:     NumberField nf;
                   10502:     struct oEGT eg0,eg1;
                   10503:
                   10504:     if ( !(nf = get_numberfield()) )
                   10505:         error("nd_monic : current_numberfield is not set");
                   10506:
                   10507:     /* Q coef -> DAlg coef */
                   10508:     NEWNM(ma0); ma = ma0;
                   10509:     m = BDY(*p);
                   10510:     is_lc = 1;
                   10511:     while ( 1 ) {
                   10512:         NEWMP(mp0); mp = mp0;
1.6       noro     10513:         mp->c = (Obj)CZ(m);
1.1       noro     10514:         mp->dl = nd_separate_d(DL(m),DL(ma));
                   10515:         NEWNM(mb);
                   10516:         for ( m = NEXT(m); m; m = NEXT(m) ) {
                   10517:             alg = nd_separate_d(DL(m),DL(mb));
                   10518:             if ( !ndl_equal(DL(ma),DL(mb)) )
                   10519:                 break;
1.6       noro     10520:             NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1       noro     10521:         }
                   10522:         NEXT(mp) = 0;
                   10523:         MKDP(nd_nalg,mp0,nm);
                   10524:         MKDAlg(nm,ONE,cd);
                   10525:         if ( is_lc == 1 ) {
                   10526:             /* if the lc is a rational number, we have nothing to do */
                   10527:             if ( !mp0->dl->td )
                   10528:                 return 1;
                   10529:
                   10530:             get_eg(&eg0);
                   10531:             invdalg(cd,&inv);
                   10532:             get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   10533:             /* check the validity of inv */
                   10534:             if ( mod && !remqi((Q)inv->dn,mod) )
                   10535:                 return 0;
                   10536:             CA(ma) = nf->one;
                   10537:             is_lc = 0;
                   10538:             ln = ONE;
                   10539:         } else {
                   10540:             muldalg(cd,inv,&CA(ma));
                   10541:             lcmz(ln,CA(ma)->dn,&ln);
                   10542:         }
                   10543:         if ( m ) {
                   10544:             NEXT(ma) = mb; ma = mb;
                   10545:         } else {
                   10546:             NEXT(ma) = 0;
                   10547:             break;
                   10548:         }
                   10549:     }
                   10550:     /* l = lcm(denoms) */
                   10551:     l = ln;
                   10552:     for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6       noro     10553:         divsz(l,CA(m)->dn,&mul);
1.1       noro     10554:         for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   10555:             NEXTNM(mr0,mr);
1.6       noro     10556:             mulz((Z)mp->c,mul,&CZ(mr));
1.1       noro     10557:             dl = mp->dl;
                   10558:             td = TD(DL(m));
                   10559:             ndl_copy(DL(m),DL(mr));
                   10560:             for ( i = 0; i < nd_nalg; i++ ) {
                   10561:                 e = dl->d[i];
                   10562:                 PUT_EXP(DL(mr),i+nd_ntrans,e);
                   10563:                 td += MUL_WEIGHT(e,i+nd_ntrans);
                   10564:             }
                   10565:             if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
                   10566:             TD(DL(mr)) = td;
                   10567:             if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   10568:         }
                   10569:     }
                   10570:     NEXT(mr) = 0;
                   10571:     for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   10572:     MKND(NV(*p),mr0,len,r);
                   10573:     /* XXX */
                   10574:     SG(r) = SG(*p);
                   10575:     nd_free(*p);
                   10576:     *p = r;
                   10577:     return 1;
                   10578: }
                   10579:
                   10580: NODE reverse_node(NODE n)
                   10581: {
                   10582:     NODE t,t1;
                   10583:
                   10584:     for ( t = 0; n; n = NEXT(n) ) {
                   10585:         MKNODE(t1,BDY(n),t); t = t1;
                   10586:     }
                   10587:     return t;
                   10588: }
                   10589:
                   10590: P ndc_div(int mod,union oNDC a,union oNDC b)
                   10591: {
                   10592:     union oNDC c;
                   10593:     int inv,t;
                   10594:
                   10595:     if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10      noro     10596:     else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1       noro     10597:     else if ( mod ) {
                   10598:         inv = invm(b.m,mod);
                   10599:         DMAR(a.m,inv,0,mod,t); c.m = t;
                   10600:     } else if ( nd_vc )
                   10601:        divsp(nd_vc,a.p,b.p,&c.p);
                   10602:     else
                   10603:        divsz(a.z,b.z,&c.z);
                   10604:     return ndctop(mod,c);
                   10605: }
                   10606:
                   10607: P ndctop(int mod,union oNDC c)
                   10608: {
                   10609:     Z q;
                   10610:     int e;
                   10611:     GFS gfs;
                   10612:
                   10613:     if ( mod == -1 ) {
                   10614:         e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
                   10615:     } else if ( mod == -2 ) {
1.10      noro     10616:        q = c.z; return (P)q;
1.1       noro     10617:     } else if ( mod > 0 ) {
1.6       noro     10618:         STOZ(c.m,q); return (P)q;
1.1       noro     10619:     } else
                   10620:         return (P)c.p;
                   10621: }
                   10622:
                   10623: /* [0,0,0,cont] = p -> p/cont */
                   10624:
                   10625: void finalize_tracelist(int i,P cont)
                   10626: {
                   10627:  LIST l;
                   10628:  NODE node;
                   10629:  Z iq;
                   10630:
                   10631:   if ( !UNIQ(cont) ) {
                   10632:     node = mknode(4,NULLP,NULLP,NULLP,cont);
                   10633:     MKLIST(l,node); MKNODE(node,l,nd_tracelist);
                   10634:     nd_tracelist = node;
                   10635:   }
1.6       noro     10636:   STOZ(i,iq);
1.1       noro     10637:   nd_tracelist = reverse_node(nd_tracelist);
                   10638:   MKLIST(l,nd_tracelist);
                   10639:   node = mknode(2,iq,l); MKLIST(l,node);
                   10640:   MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
                   10641:   nd_alltracelist = node; nd_tracelist = 0;
                   10642: }
                   10643:
                   10644: void conv_ilist(int demand,int trace,NODE g,int **indp)
                   10645: {
                   10646:     int n,i,j;
                   10647:   int *ind;
                   10648:   NODE t;
                   10649:
                   10650:     n = length(g);
                   10651:   ind = (int *)MALLOC(n*sizeof(int));
                   10652:   for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
                   10653:     j = (long)BDY(t); ind[i] = j;
                   10654:     BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
                   10655:   }
                   10656:   if ( indp ) *indp = ind;
                   10657: }
                   10658:
1.30      noro     10659: NODE conv_ilist_s(int demand,int trace,int **indp)
                   10660: {
                   10661:   int n,i,j;
                   10662:   int *ind;
                   10663:   NODE g0,g;
                   10664:
                   10665:   n = nd_psn;
                   10666:   ind = (int *)MALLOC(n*sizeof(int));
                   10667:   g0 = 0;
                   10668:   for ( i = 0; i < n; i++ ) {
                   10669:     ind[i] = i;
                   10670:     NEXTNODE(g0,g);
                   10671:     BDY(g) = (pointer)(demand?ndv_load(i):(trace?nd_ps_trace[i]:nd_ps[i]));
                   10672:   }
                   10673:   if ( g0 ) NEXT(g) = 0;
                   10674:   if ( indp ) *indp = ind;
                   10675:   return g0;
                   10676: }
                   10677:
1.46      noro     10678: void parse_nd_option(VL vl,NODE opt)
1.1       noro     10679: {
1.38      noro     10680:   NODE t,p,u;
1.1       noro     10681:   int i,s,n;
1.38      noro     10682:   char *key;
                   10683:   Obj value;
1.46      noro     10684:   VL oldvl;
1.1       noro     10685:
1.41      noro     10686:   nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_norb = 0; nd_gbblock = 0;
1.1       noro     10687:   nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
                   10688:   nd_splist = 0; nd_check_splist = 0;
1.38      noro     10689:   nd_sugarweight = 0; nd_f4red =0; nd_rank0 = 0;
                   10690:   nd_f4_td = 0; nd_sba_f4step = 2; nd_sba_pot = 0; nd_sba_largelcm = 0;
1.41      noro     10691:   nd_sba_dontsort = 0; nd_top = 0; nd_sba_redundant_check = 0;
1.46      noro     10692:   nd_sba_syz = 0; nd_sba_modord = 0; nd_sba_inputisgb = 0;
1.63      noro     10693:   nd_hpdata = 0; nd_sba_heu = 0;
1.39      noro     10694:
1.38      noro     10695:   for ( t = opt; t; t = NEXT(t) ) {
                   10696:     p = BDY((LIST)BDY(t));
                   10697:     key = BDY((STRING)BDY(p));
                   10698:     value = (Obj)BDY(NEXT(p));
                   10699:     if ( !strcmp(key,"gentrace") )
                   10700:       nd_gentrace = value?1:0;
                   10701:     else if ( !strcmp(key,"gensyz") )
                   10702:       nd_gensyz = value?1:0;
                   10703:     else if ( !strcmp(key,"nora") )
                   10704:       nd_nora = value?1:0;
1.41      noro     10705:     else if ( !strcmp(key,"norb") )
                   10706:       nd_norb = value?1:0;
1.38      noro     10707:     else if ( !strcmp(key,"gbblock") ) {
                   10708:       if ( value && OID(value) == O_LIST ) {
1.1       noro     10709:         u = BDY((LIST)value);
1.38      noro     10710:         nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.1       noro     10711:         for ( i = 0; u; u = NEXT(u) ) {
                   10712:           p = BDY((LIST)BDY(u));
1.6       noro     10713:           s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
                   10714:           nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1       noro     10715:         }
                   10716:         nd_gbblock[i] = -1;
1.38      noro     10717:       } else
                   10718:         nd_gbblock = 0;
1.1       noro     10719:     } else if ( !strcmp(key,"newelim") )
                   10720:             nd_newelim = value?1:0;
                   10721:     else if ( !strcmp(key,"intersect") )
                   10722:             nd_intersect = value?1:0;
1.17      noro     10723:     else if ( !strcmp(key,"syzgen") )
                   10724:             nd_intersect = ZTOS((Q)value);
1.1       noro     10725:     else if ( !strcmp(key,"lf") )
                   10726:             nd_lf = value?1:0;
                   10727:     else if ( !strcmp(key,"trace") ) {
1.38      noro     10728:       if ( value ) {
                   10729:         u = BDY((LIST)value);
                   10730:         nd_nzlist = BDY((LIST)ARG2(u));
                   10731:         nd_bpe = ZTOS((Q)ARG3(u));
                   10732:       }
1.1       noro     10733:     } else if ( !strcmp(key,"f4red") ) {
1.38      noro     10734:       nd_f4red = ZTOS((Q)value);
1.1       noro     10735:     } else if ( !strcmp(key,"rank0") ) {
1.38      noro     10736:       nd_rank0 = value?1:0;
1.1       noro     10737:     } else if ( !strcmp(key,"splist") ) {
1.38      noro     10738:       nd_splist = value?1:0;
1.1       noro     10739:     } else if ( !strcmp(key,"check_splist") ) {
                   10740:       nd_check_splist = BDY((LIST)value);
1.49      noro     10741:     } else if ( !strcmp(key,"hpdata") ) {
1.50      noro     10742:       if ( value )
                   10743:         nd_hpdata = BDY((LIST)value);
1.1       noro     10744:     } else if ( !strcmp(key,"sugarweight") ) {
                   10745:       u = BDY((LIST)value);
1.38      noro     10746:       n = length(u);
                   10747:       nd_sugarweight = MALLOC(n*sizeof(int));
1.1       noro     10748:       for ( i = 0; i < n; i++, u = NEXT(u) )
1.38      noro     10749:         nd_sugarweight[i] = ZTOS((Q)BDY(u));
                   10750:     } else if ( !strcmp(key,"f4_td") ) {
                   10751:       nd_f4_td = value?1:0;
                   10752:     } else if ( !strcmp(key,"sba_f4step") ) {
                   10753:       nd_sba_f4step = value?ZTOS((Q)value):0;
                   10754:     } else if ( !strcmp(key,"sba_pot") ) {
1.44      noro     10755:       nd_sba_pot = ZTOS((Q)value);
1.38      noro     10756:     } else if ( !strcmp(key,"sba_largelcm") ) {
                   10757:       nd_sba_largelcm = value?1:0;
1.39      noro     10758:     } else if ( !strcmp(key,"sba_dontsort") ) {
                   10759:       nd_sba_dontsort = value?1:0;
1.44      noro     10760:     } else if ( !strcmp(key,"sba_syz") ) {
                   10761:       nd_sba_syz = value?1:0;
1.63      noro     10762:     } else if ( !strcmp(key,"sba_heu") ) {
                   10763:       nd_sba_heu = value?1:0;
1.46      noro     10764:     } else if ( !strcmp(key,"sba_modord") ) {
                   10765:       // value=[vlist,ordspec,weight]
                   10766:       u = BDY((LIST)value);
                   10767:       pltovl((LIST)ARG0(u),&oldvl);
                   10768:       nd_sba_modord = create_comp_sig_spec(vl,oldvl,(Obj)ARG1(u),argc(u)==3?ARG2(u):0);
                   10769:     } else if ( !strcmp(key,"sba_gbinput") ) {
                   10770:       nd_sba_inputisgb = value?1:0;
                   10771:       if ( nd_sba_inputisgb != 0 ) {
                   10772:         // value=[vlist,ordspec,weight]
                   10773:         u = BDY((LIST)value);
                   10774:         pltovl((LIST)ARG0(u),&oldvl);
                   10775:         nd_sba_modord = create_comp_sig_spec(vl,oldvl,(Obj)ARG1(u),argc(u)==3?ARG2(u):0);
                   10776:       }
1.41      noro     10777:     } else if ( !strcmp(key,"sba_redundant_check") ) {
                   10778:       nd_sba_redundant_check = value?1:0;
1.39      noro     10779:     } else if ( !strcmp(key,"top") ) {
                   10780:       nd_top = value?1:0;
1.1       noro     10781:     }
1.38      noro     10782:   }
1.44      noro     10783:   if ( nd_sba_syz ) nd_sba_dontsort = 1;
1.1       noro     10784: }
                   10785:
                   10786: ND mdptond(DP d);
                   10787: ND nd_mul_nm(int mod,NM m0,ND p);
                   10788: ND nd_mul_nm_lf(NM m0,ND p);
                   10789: ND *btog(NODE ti,ND **p,int nb,int mod);
                   10790: ND btog_one(NODE ti,ND *p,int nb,int mod);
                   10791: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
                   10792: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
                   10793:
                   10794: /* d:monomial */
                   10795: ND mdptond(DP d)
                   10796: {
                   10797:   NM m;
                   10798:   ND r;
                   10799:
                   10800:   if ( OID(d) == 1 )
                   10801:   r = ptond(CO,CO,(P)d);
                   10802:   else {
                   10803:     NEWNM(m);
                   10804:     dltondl(NV(d),BDY(d)->dl,DL(m));
1.6       noro     10805:     CZ(m) = (Z)BDY(d)->c;
1.1       noro     10806:     NEXT(m) = 0;
                   10807:     MKND(NV(d),m,1,r);
                   10808:   }
                   10809:     return r;
                   10810: }
                   10811:
                   10812: ND nd_mul_nm(int mod,NM m0,ND p)
                   10813: {
                   10814:   UINT *d0;
                   10815:   int c0,c1,c;
                   10816:   NM tm,mr,mr0;
                   10817:   ND r;
                   10818:
                   10819:   if ( !p ) return 0;
                   10820:   d0 = DL(m0);
                   10821:   c0 = CM(m0);
                   10822:   mr0 = 0;
                   10823:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   10824:     NEXTNM(mr0,mr);
                   10825:   c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
                   10826:   ndl_add(d0,DL(tm),DL(mr));
                   10827:   }
                   10828:   NEXT(mr) = 0;
                   10829:   MKND(NV(p),mr0,LEN(p),r);
                   10830:   return r;
                   10831: }
                   10832:
                   10833: ND nd_mul_nm_lf(NM m0,ND p)
                   10834: {
                   10835:   UINT *d0;
                   10836:   Z c0,c1,c;
                   10837:   NM tm,mr,mr0;
                   10838:   ND r;
                   10839:
                   10840:   if ( !p ) return 0;
                   10841:   d0 = DL(m0);
                   10842:   c0 = CZ(m0);
                   10843:   mr0 = 0;
                   10844:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   10845:     NEXTNM(mr0,mr);
                   10846:     c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
                   10847:     ndl_add(d0,DL(tm),DL(mr));
                   10848:   }
                   10849:   NEXT(mr) = 0;
                   10850:   MKND(NV(p),mr0,LEN(p),r);
                   10851:   return r;
                   10852: }
                   10853:
                   10854: ND *btog(NODE ti,ND **p,int nb,int mod)
                   10855: {
                   10856:   PGeoBucket *r;
                   10857:   int i,ci;
                   10858:   NODE t,s;
                   10859:   ND m,tp;
                   10860:   ND *pi,*rd;
                   10861:   P c;
                   10862:
                   10863:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   10864:   for ( i = 0; i < nb; i++ )
                   10865:     r[i] = create_pbucket();
                   10866:   for ( t = ti; t; t = NEXT(t) ) {
                   10867:   s = BDY((LIST)BDY(t));
                   10868:     if ( ARG0(s) ) {
                   10869:     m = mdptond((DP)ARG2(s));
1.6       noro     10870:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     10871:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   10872:       HCM(m) = ci;
1.6       noro     10873:       pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     10874:       for ( i = 0; i < nb; i++ ) {
                   10875:       tp = nd_mul_nm(mod,BDY(m),pi[i]);
                   10876:         add_pbucket(mod,r[i],tp);
                   10877:       }
                   10878:     }
                   10879:     ci = 1;
                   10880:     } else {
                   10881:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   10882:     ci = invm(ci,mod);
                   10883:   }
                   10884:   }
                   10885:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   10886:   for ( i = 0; i < nb; i++ )
                   10887:   rd[i] = normalize_pbucket(mod,r[i]);
                   10888:   if ( ci != 1 )
                   10889:     for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
                   10890:    return rd;
                   10891: }
                   10892:
                   10893: /* YYY */
                   10894: ND *btog_lf(NODE ti,ND **p,int nb)
                   10895: {
                   10896:   PGeoBucket *r;
                   10897:   int i;
                   10898:   NODE t,s;
                   10899:   ND m,tp;
                   10900:   ND *pi,*rd;
                   10901:   LM lm;
                   10902:   Z lf,c;
                   10903:
                   10904:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   10905:   for ( i = 0; i < nb; i++ )
                   10906:     r[i] = create_pbucket();
                   10907:   for ( t = ti; t; t = NEXT(t) ) {
                   10908:     s = BDY((LIST)BDY(t));
                   10909:     if ( ARG0(s) ) {
                   10910:       m = mdptond((DP)ARG2(s));
1.6       noro     10911:       simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1       noro     10912:       if ( lm ) {
                   10913:         lmtolf(lm,&lf); HCZ(m) = lf;
1.6       noro     10914:         pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     10915:         for ( i = 0; i < nb; i++ ) {
                   10916:           tp = nd_mul_nm_lf(BDY(m),pi[i]);
                   10917:           add_pbucket(-2,r[i],tp);
                   10918:         }
                   10919:       }
                   10920:       c = ONE;
                   10921:     } else {
                   10922:       simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
                   10923:     }
                   10924:   }
                   10925:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   10926:   for ( i = 0; i < nb; i++ )
                   10927:     rd[i] = normalize_pbucket(-2,r[i]);
                   10928:   for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
                   10929:   return rd;
                   10930: }
                   10931:
                   10932: ND btog_one(NODE ti,ND *p,int nb,int mod)
                   10933: {
                   10934:   PGeoBucket r;
                   10935:   int i,ci,j;
                   10936:   NODE t,s;
                   10937:   ND m,tp;
                   10938:   ND pi,rd;
                   10939:   P c;
                   10940:
                   10941:   r = create_pbucket();
                   10942:   for ( t = ti; t; t = NEXT(t) ) {
                   10943:   s = BDY((LIST)BDY(t));
                   10944:     if ( ARG0(s) ) {
                   10945:     m = mdptond((DP)ARG2(s));
1.6       noro     10946:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     10947:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   10948:       HCM(m) = ci;
1.6       noro     10949:       pi = p[j=ZTOS((Q)ARG1(s))];
1.1       noro     10950:     if ( !pi ) {
                   10951:       pi = nd_load_mod(j);
                   10952:       tp = nd_mul_nm(mod,BDY(m),pi);
                   10953:       nd_free(pi);
                   10954:         add_pbucket(mod,r,tp);
                   10955:     } else {
                   10956:       tp = nd_mul_nm(mod,BDY(m),pi);
                   10957:         add_pbucket(mod,r,tp);
                   10958:       }
                   10959:     }
                   10960:     ci = 1;
                   10961:     } else {
                   10962:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   10963:     ci = invm(ci,mod);
                   10964:   }
                   10965:   }
                   10966:   rd = normalize_pbucket(mod,r);
                   10967:   free_pbucket(r);
                   10968:   if ( ci != 1 ) nd_mul_c(mod,rd,ci);
                   10969:   return rd;
                   10970: }
                   10971:
                   10972: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
                   10973:
                   10974: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
                   10975: {
                   10976:   int i,j,n,m,nb,pi0,pi1,nvar;
                   10977:   VL fv,tv,vv;
                   10978:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   10979:   ND **p;
                   10980:   ND *c;
                   10981:   ND u;
                   10982:   P inv;
                   10983:   MAT mat;
                   10984:
                   10985:   if ( mod == -2 )
                   10986:     return nd_btog_lf(f,v,ord,tlist,rp);
                   10987:
                   10988:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     10989:   parse_nd_option(vv,current_option);
1.1       noro     10990:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   10991:   switch ( ord->id ) {
                   10992:     case 1:
                   10993:       if ( ord->nv != nvar )
                   10994:         error("nd_check : invalid order specification");
                   10995:       break;
                   10996:     default:
                   10997:       break;
                   10998:   }
                   10999:   nd_init_ord(ord);
                   11000: #if 0
1.6       noro     11001:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     11002: #else
                   11003:   nd_bpe = 32;
                   11004: #endif
                   11005:   nd_setup_parameters(nvar,0);
                   11006:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   11007:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   11008:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   11009:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   11010:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     11011:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     11012:   if ( j > i ) i = j;
                   11013:   }
                   11014:   n = i+1;
                   11015:   nb = length(BDY(f));
                   11016:   p = (ND **)MALLOC(n*sizeof(ND *));
                   11017:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   11018:     pi = BDY((LIST)BDY(t));
1.6       noro     11019:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     11020:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   11021:     ptomp(mod,(P)ARG2(pi),&inv);
                   11022:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   11023:     u = ptond(CO,vv,(P)ONE);
                   11024:     HCM(u) = ((MQ)inv)->cont;
                   11025:     c[pi1] = u;
                   11026:   }
                   11027:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   11028:     printf("%d ",i); fflush(stdout);
                   11029:     ti = BDY((LIST)BDY(t));
1.6       noro     11030:     p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     11031:   }
                   11032:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   11033:     printf("%d ",i); fflush(stdout);
                   11034:     ti = BDY((LIST)BDY(t));
1.6       noro     11035:     p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     11036:   }
                   11037:   m = length(ind);
                   11038:   MKMAT(mat,nb,m);
                   11039:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     11040:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     11041:       BDY(mat)[i][j] = ndtodp(mod,c[i]);
                   11042:   return mat;
                   11043: }
                   11044:
                   11045: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
                   11046: {
                   11047:   int i,j,n,m,nb,pi0,pi1,nvar;
                   11048:   VL fv,tv,vv;
                   11049:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   11050:   ND **p;
                   11051:   ND *c;
                   11052:   ND u;
                   11053:   MAT mat;
                   11054:   LM lm;
                   11055:   Z lf,inv;
                   11056:
                   11057:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     11058:   parse_nd_option(vv,current_option);
1.1       noro     11059:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   11060:   switch ( ord->id ) {
                   11061:     case 1:
                   11062:       if ( ord->nv != nvar )
                   11063:         error("nd_check : invalid order specification");
                   11064:       break;
                   11065:     default:
                   11066:       break;
                   11067:   }
                   11068:   nd_init_ord(ord);
                   11069: #if 0
1.6       noro     11070:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     11071: #else
                   11072:   nd_bpe = 32;
                   11073: #endif
                   11074:   nd_setup_parameters(nvar,0);
                   11075:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   11076:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   11077:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   11078:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   11079:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     11080:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     11081:   if ( j > i ) i = j;
                   11082:   }
                   11083:   n = i+1;
                   11084:   nb = length(BDY(f));
                   11085:   p = (ND **)MALLOC(n*sizeof(ND *));
                   11086:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   11087:     pi = BDY((LIST)BDY(t));
1.6       noro     11088:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     11089:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   11090:     simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
                   11091:     u = ptond(CO,vv,(P)ONE);
                   11092:     HCZ(u) = inv;
                   11093:     c[pi1] = u;
                   11094:   }
                   11095:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   11096:     printf("%d ",i); fflush(stdout);
                   11097:     ti = BDY((LIST)BDY(t));
1.6       noro     11098:     p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     11099:   }
                   11100:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   11101:     printf("%d ",i); fflush(stdout);
                   11102:     ti = BDY((LIST)BDY(t));
1.6       noro     11103:     p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     11104:   }
                   11105:   m = length(ind);
                   11106:   MKMAT(mat,nb,m);
                   11107:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     11108:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     11109:       BDY(mat)[i][j] = ndtodp(-2,c[i]);
                   11110:   return mat;
                   11111: }
                   11112:
                   11113: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
                   11114:   LIST tlist,int pos,MAT *rp)
                   11115: {
                   11116:   int i,j,n,m,nb,pi0,pi1,nvar;
                   11117:   VL fv,tv,vv;
                   11118:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   11119:   ND *p;
                   11120:   ND *c;
                   11121:   ND u;
                   11122:   P inv;
                   11123:   VECT vect;
                   11124:
                   11125:   if ( mod == -2 )
                   11126:     error("nd_btog_one : not implemented yet for a large finite field");
                   11127:
                   11128:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     11129:   parse_nd_option(vv,current_option);
1.1       noro     11130:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   11131:   switch ( ord->id ) {
                   11132:     case 1:
                   11133:       if ( ord->nv != nvar )
                   11134:         error("nd_check : invalid order specification");
                   11135:       break;
                   11136:     default:
                   11137:       break;
                   11138:   }
                   11139:   nd_init_ord(ord);
                   11140: #if 0
1.6       noro     11141:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     11142: #else
                   11143:   nd_bpe = 32;
                   11144: #endif
                   11145:   nd_setup_parameters(nvar,0);
                   11146:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   11147:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   11148:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   11149:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   11150:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     11151:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     11152:   if ( j > i ) i = j;
                   11153:   }
                   11154:   n = i+1;
                   11155:   nb = length(BDY(f));
                   11156:   p = (ND *)MALLOC(n*sizeof(ND *));
                   11157:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   11158:     pi = BDY((LIST)BDY(t));
1.6       noro     11159:   pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     11160:   if ( pi1 == pos ) {
                   11161:     ptomp(mod,(P)ARG2(pi),&inv);
                   11162:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   11163:     u = ptond(CO,vv,(P)ONE);
                   11164:     HCM(u) = ((MQ)inv)->cont;
                   11165:     p[pi0] = u;
                   11166:     }
                   11167:   }
                   11168:   for ( t = trace,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:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   11177:   printf("%d ",i); fflush(stdout);
                   11178:     ti = BDY((LIST)BDY(t));
1.6       noro     11179:     p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     11180:     if ( Demand ) {
                   11181:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   11182:   }
                   11183:   }
                   11184:   m = length(ind);
                   11185:   MKVECT(vect,m);
                   11186:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6       noro     11187:   u = p[ZTOS((Q)BDY(t))];
1.1       noro     11188:   if ( !u ) {
1.6       noro     11189:     u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1       noro     11190:     BDY(vect)[j] = ndtodp(mod,u);
                   11191:     nd_free(u);
                   11192:   } else
                   11193:     BDY(vect)[j] = ndtodp(mod,u);
                   11194:   }
                   11195:   return vect;
                   11196: }
                   11197:
                   11198: void ndv_print_lf(NDV p)
                   11199: {
                   11200:     NMV m;
                   11201:     int i,len;
                   11202:
                   11203:     if ( !p ) printf("0\n");
                   11204:     else {
                   11205:         len = LEN(p);
                   11206:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   11207:             printf("+");
                   11208:             mpz_out_str(asir_out,10,BDY(CZ(m)));
                   11209:             printf("*");
                   11210:             ndl_print(DL(m));
                   11211:         }
                   11212:         printf("\n");
                   11213:     }
                   11214: }
                   11215:
                   11216: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
                   11217: {
                   11218:     VL tv,fv,vv,vc,av;
                   11219:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   11220:     int m,nocheck,nvar,mindex,e,max;
                   11221:     NDV c;
                   11222:     NMV a;
                   11223:     P p,zp;
                   11224:     Q dmy;
                   11225:     EPOS oepos;
                   11226:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   11227:     Alg alpha,dp;
                   11228:     P poly;
                   11229:     LIST f1,f2,zpl;
                   11230:     Obj obj;
                   11231:     NumberField nf;
                   11232:     struct order_spec *ord1;
                   11233:     struct oEGT eg_check,eg0,eg1;
                   11234:     NODE tr,tl1,tl2,tl3,tl4;
                   11235:     LIST l1,l2,l3,l4,l5;
                   11236:     int *perm;
                   11237:     int j,ret;
                   11238:     NODE retn;
                   11239:     Q jq,bpe;
                   11240:
                   11241:     nd_module = 0;
                   11242:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.46      noro     11243:     parse_nd_option(vv,current_option);
1.1       noro     11244:     if ( nd_vc )
                   11245:       error("nd_f4_lf_trace : computation over a rational function field is not implemented");
                   11246:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   11247:     switch ( ord->id ) {
                   11248:         case 1:
                   11249:             if ( ord->nv != nvar )
                   11250:                 error("nd_f4_lf_trace : invalid order specification");
                   11251:             break;
                   11252:         default:
                   11253:             break;
                   11254:     }
                   11255:
                   11256:     nd_ntrans = nvar;
                   11257:     nd_nalg = 0;
                   11258:
                   11259:     nocheck = 0;
                   11260:     mindex = 0;
                   11261:
                   11262:     /* do not use on-demand load/save */
                   11263:     nd_demand = 0;
                   11264:     m = trace > 1 ? trace : get_lprime(mindex);
                   11265:     nd_init_ord(ord);
                   11266:     mrank = 0;
                   11267:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   11268:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   11269:             if ( nd_module ) {
                   11270:                 s = BDY((LIST)BDY(t));
                   11271:                 trank = length(s);
                   11272:                 mrank = MAX(mrank,trank);
                   11273:                 for ( ; s; s = NEXT(s) ) {
                   11274:                     e = getdeg(tv->v,(P)BDY(s));
                   11275:                     max = MAX(e,max);
                   11276:                 }
                   11277:             } else {
                   11278:                 e = getdeg(tv->v,(P)BDY(t));
                   11279:                 max = MAX(e,max);
                   11280:             }
                   11281:         }
                   11282:     nd_setup_parameters(nvar,max);
                   11283:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   11284:     ishomo = 1;
                   11285:     /* XXX */
                   11286:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   11287:         if ( nd_module ) {
                   11288:             c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
                   11289:         } else {
                   11290:             c = (pointer)ptondv(CO,vv,(P)BDY(t));
                   11291:         }
                   11292:         if ( ishomo )
                   11293:             ishomo = ishomo && ndv_ishomo(c);
                   11294:         if ( c ) {
                   11295:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   11296:             ndv_mod(-2,c);
                   11297:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   11298:         }
                   11299:     }
                   11300:     if ( in0 ) NEXT(in) = 0;
                   11301:     if ( fd0 ) NEXT(fd) = 0;
                   11302:     if ( !ishomo && homo ) {
                   11303:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   11304:             c = (NDV)BDY(t); len = LEN(c);
                   11305:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   11306:                 wmax = MAX(TD(DL(a)),wmax);
                   11307:         }
                   11308:         homogenize_order(ord,nvar,&ord1);
                   11309:         nd_init_ord(ord1);
                   11310:         nd_setup_parameters(nvar+1,wmax);
                   11311:         for ( t = fd0; t; t = NEXT(t) )
                   11312:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   11313:     }
                   11314:     if ( MaxDeg > 0 ) nocheck = 1;
1.24      noro     11315:     ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1       noro     11316:     if ( ret )
                   11317:       cand = nd_f4_lf_trace_main(m,&perm);
                   11318:     if ( !ret || !cand ) {
                   11319:        *rp = 0; return;
                   11320:     }
                   11321:     if ( !ishomo && homo ) {
                   11322:       /* dehomogenization */
                   11323:       for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   11324:       nd_init_ord(ord);
                   11325:       nd_setup_parameters(nvar,0);
                   11326:     }
                   11327:     cand = ndv_reducebase(cand,perm);
                   11328:     cand = ndv_reduceall(-2,cand);
                   11329:     cbpe = nd_bpe;
                   11330:     get_eg(&eg0);
                   11331:     if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   11332:       /* gbcheck : cand is a GB of Id(cand) ? */
                   11333:       retn = nd_f4(-2,0,0);
                   11334:     }
                   11335:   if ( !retn ) {
                   11336:       /* failure */
                   11337:       *rp = 0; return;
                   11338:     }
                   11339:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   11340:     if ( DP_Print )
1.5       noro     11341:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     11342:     /* dp->p */
                   11343:     nd_bpe = cbpe;
                   11344:     nd_setup_parameters(nd_nvar,0);
                   11345:     for ( r = cand; r; r = NEXT(r) ) {
                   11346:       if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
                   11347:       else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
                   11348:     }
                   11349:     MKLIST(*rp,cand);
                   11350: }
                   11351:
                   11352: NODE nd_f4_lf_trace_main(int m,int **indp)
                   11353: {
                   11354:     int i,nh,stat,index;
                   11355:     NODE r,rm,g;
                   11356:     ND_pairs d,l,l0,t;
                   11357:     ND spol,red;
                   11358:     NDV nf,redv,nfqv,nfv;
                   11359:     NM s0,s;
                   11360:     NODE rp0,srp0,nflist,nflist_lf;
                   11361:     int nsp,nred,col,rank,len,k,j,a;
                   11362:     UINT c;
                   11363:     UINT **spmat;
                   11364:     UINT *s0vect,*svect,*p,*v;
                   11365:     int *colstat;
                   11366:     IndArray *imat;
                   11367:     int *rhead;
                   11368:     int spcol,sprow;
                   11369:     int sugar;
                   11370:     PGeoBucket bucket;
                   11371:     struct oEGT eg0,eg1,eg_f4;
                   11372:
                   11373:     g = 0; d = 0;
                   11374:     for ( i = 0; i < nd_psn; i++ ) {
                   11375:         d = update_pairs(d,g,i,0);
                   11376:         g = update_base(g,i);
                   11377:     }
                   11378:     while ( d ) {
                   11379:         get_eg(&eg0);
                   11380:         l = nd_minsugarp(d,&d);
                   11381:         sugar = SG(l);
                   11382:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   11383:         bucket = create_pbucket();
                   11384:         stat = nd_sp_f4(m,0,l,bucket);
                   11385:         if ( !stat ) {
                   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:         if ( bucket->m < 0 ) continue;
                   11392:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   11393:         if ( !col ) {
                   11394:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11395:             NEXT(t) = d; d = l;
                   11396:             d = nd_reconstruct(1,d);
                   11397:             continue;
                   11398:         }
                   11399:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   11400:         if ( DP_Print )
1.5       noro     11401:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1       noro     11402:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   11403:         if ( !l0 ) continue;
                   11404:         l = l0;
                   11405:
                   11406:         /* over LF */
                   11407:         bucket = create_pbucket();
                   11408:         stat = nd_sp_f4(-2,1,l,bucket);
                   11409:         if ( !stat ) {
                   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:         if ( bucket->m < 0 ) continue;
                   11416:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   11417:         if ( !col ) {
                   11418:             for ( t = l; NEXT(t); t = NEXT(t) );
                   11419:             NEXT(t) = d; d = l;
                   11420:             d = nd_reconstruct(1,d);
                   11421:             continue;
                   11422:         }
                   11423:         nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
                   11424:         /* adding new bases */
                   11425:         for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
                   11426:             nfv = (NDV)BDY(rm);
                   11427:             nfqv = (NDV)BDY(r);
                   11428:             if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
                   11429:             ndv_removecont(m,nfv);
                   11430:             ndv_removecont(-2,nfqv);
1.24      noro     11431:             nh = ndv_newps(-2,nfv,nfqv);
1.1       noro     11432:             d = update_pairs(d,g,nh,0);
                   11433:             g = update_base(g,nh);
                   11434:         }
                   11435:         if ( r || rm ) return 0;
                   11436:     }
                   11437:   conv_ilist(nd_demand,1,g,indp);
                   11438:     return g;
                   11439: }
                   11440:
1.7       noro     11441: #if SIZEOF_LONG==8
                   11442:
                   11443: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   11444: {
                   11445:     int j,k,len;
                   11446:     UINT *p;
                   11447:     UINT c;
                   11448:     NDV r;
                   11449:     NMV mr0,mr;
                   11450:
                   11451:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   11452:     if ( !len ) return 0;
                   11453:     else {
                   11454:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   11455: #if 0
                   11456:         ndv_alloc += nmv_adv*len;
                   11457: #endif
                   11458:         mr = mr0;
                   11459:         p = s0vect;
                   11460:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   11461:             if ( !rhead[j] ) {
                   11462:                 if ( (c = (UINT)vect[k++]) != 0 ) {
                   11463:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   11464:                 }
                   11465:             }
                   11466:         MKNDV(nd_nvar,mr0,len,r);
                   11467:         return r;
                   11468:     }
                   11469: }
                   11470:
1.28      noro     11471: NDV vect64_to_ndv_s(mp_limb_t *vect,int col,UINT *s0vect)
                   11472: {
                   11473:     int j,k,len;
                   11474:     UINT *p;
                   11475:     UINT c;
                   11476:     NDV r;
                   11477:     NMV mr0,mr;
                   11478:
                   11479:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   11480:     if ( !len ) return 0;
                   11481:     else {
                   11482:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   11483:         mr = mr0;
                   11484:         p = s0vect;
                   11485:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   11486:           if ( (c = (UINT)vect[k++]) != 0 ) {
                   11487:             ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   11488:           }
                   11489:         MKNDV(nd_nvar,mr0,len,r);
                   11490:         return r;
                   11491:     }
                   11492: }
                   11493:
1.7       noro     11494: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
                   11495: {
                   11496:     NM m;
1.11      noro     11497:     UINT *t,*s,*u;
                   11498:     int i,st,ed,md,prev,c;
1.7       noro     11499:
                   11500:     for ( i = 0; i < n; i++ ) r[i] = 0;
1.11      noro     11501:     prev = 0;
                   11502:     for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
                   11503:       t = DL(m);
                   11504:       st = prev;
                   11505:       ed = n;
                   11506:       while ( ed > st ) {
                   11507:         md = (st+ed)/2;
                   11508:         u = s0+md*nd_wpd;
                   11509:         c = DL_COMPARE(u,t);
                   11510:         if ( c == 0 ) break;
                   11511:         else if ( c > 0 ) st = md;
                   11512:         else ed = md;
                   11513:       }
                   11514:       r[md] = (mp_limb_t)CM(m);
                   11515:       prev = md;
1.7       noro     11516:     }
                   11517:     for ( i = 0; !r[i]; i++ );
                   11518:     return i;
                   11519: }
                   11520:
                   11521: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
                   11522:
1.28      noro     11523: 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     11524: {
                   11525:     int i,j,k,len,pos,prev;
                   11526:     mp_limb_t a,c,c1,c2;
                   11527:     IndArray ivect;
                   11528:     unsigned char *ivc;
                   11529:     unsigned short *ivs;
                   11530:     unsigned int *ivi;
                   11531:     NDV redv;
                   11532:     NMV mr;
                   11533:     NODE rp;
                   11534:     int maxrs;
                   11535:
                   11536:     for ( i = 0; i < col; i++ ) cvect[i] = 0;
                   11537:     maxrs = 0;
                   11538:     for ( i = 0; i < nred; i++ ) {
                   11539:         ivect = imat[i];
                   11540:         k = ivect->head;
                   11541:         a = svect[k]; c = cvect[k];
                   11542:         MOD128(a,c,m);
                   11543:         svect[k] = a; cvect[k] = 0;
1.28      noro     11544:         if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.11      noro     11545:             Nf4_red++;
1.7       noro     11546:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   11547:             c = m-c; redv = nd_ps[rp0[i]->index];
                   11548:             len = LEN(redv); mr = BDY(redv);
                   11549:             svect[k] = 0; prev = k;
                   11550:             switch ( ivect->width ) {
                   11551:                 case 1:
                   11552:                     ivc = ivect->index.c;
                   11553:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   11554:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12      noro     11555:                         c2 = svect[pos]+c1*c;
                   11556:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   11557:                         svect[pos] = c2;
1.7       noro     11558:                     }
                   11559:                     break;
                   11560:                 case 2:
                   11561:                     ivs = ivect->index.s;
                   11562:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   11563:                         pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12      noro     11564:                         c2 = svect[pos]+c1*c;
                   11565:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   11566:                         svect[pos] = c2;
1.7       noro     11567:                     }
                   11568:                     break;
                   11569:                 case 4:
                   11570:                     ivi = ivect->index.i;
                   11571:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   11572:                         pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12      noro     11573:                         c2 = svect[pos]+c1*c;
                   11574:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   11575:                         svect[pos] = c2;
1.7       noro     11576:                     }
                   11577:                     break;
                   11578:             }
                   11579:         }
                   11580:     }
                   11581:     for ( i = 0; i < col; i++ ) {
                   11582:       a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
                   11583:     }
                   11584:     return maxrs;
                   11585: }
                   11586:
                   11587: /* for Fp, 2^15=<p<2^29 */
                   11588:
                   11589: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   11590:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   11591: {
                   11592:     int spcol,sprow,a;
                   11593:     int i,j,k,l,rank;
                   11594:     NODE r0,r;
                   11595:     ND_pairs sp;
                   11596:     ND spol;
                   11597:     mp_limb_t **spmat;
                   11598:     mp_limb_t *svect,*cvect;
                   11599:     mp_limb_t *v;
                   11600:     int *colstat;
                   11601:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   11602:     int maxrs;
                   11603:     int *spsugar;
                   11604:     ND_pairs *spactive;
                   11605:
                   11606:     spcol = col-nred;
                   11607:     get_eg(&eg0);
                   11608:     /* elimination (1st step) */
                   11609:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   11610:     svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11611:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11612:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   11613:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   11614:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   11615:         nd_sp(m,0,sp,&spol);
                   11616:         if ( !spol ) continue;
                   11617:         nd_to_vect64(m,s0vect,col,spol,svect);
1.28      noro     11618:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,0);
1.7       noro     11619:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   11620:         if ( i < col ) {
                   11621:             spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
                   11622:             for ( j = k = 0; j < col; j++ )
                   11623:                 if ( !rhead[j] ) v[k++] = (UINT)svect[j];
                   11624:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   11625:             if ( nz )
                   11626:             spactive[sprow] = sp;
                   11627:             sprow++;
                   11628:         }
                   11629:         nd_free(spol);
                   11630:     }
1.12      noro     11631:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7       noro     11632:     if ( DP_Print ) {
                   11633:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   11634:         fflush(asir_out);
                   11635:     }
                   11636:     /* free index arrays */
                   11637:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   11638:
                   11639:     /* elimination (2nd step) */
                   11640:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   11641:     rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   11642:     r0 = 0;
                   11643:     for ( i = 0; i < rank; i++ ) {
                   11644:         NEXTNODE(r0,r); BDY(r) =
                   11645:           (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   11646:         SG((NDV)BDY(r)) = spsugar[i];
                   11647:         GCFREE(spmat[i]);
                   11648:     }
                   11649:     if ( r0 ) NEXT(r) = 0;
                   11650:
                   11651:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12      noro     11652:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7       noro     11653:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   11654:     if ( DP_Print ) {
                   11655:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   11656:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   11657:             nsp,nred,sprow,spcol,rank);
                   11658:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   11659:     }
                   11660:     if ( nz ) {
                   11661:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   11662:         if ( rank > 0 ) {
                   11663:             NEXT(spactive[rank-1]) = 0;
                   11664:             *nz = spactive[0];
                   11665:         } else
                   11666:             *nz = 0;
                   11667:     }
                   11668:     return r0;
                   11669: }
                   11670:
                   11671: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   11672: {
                   11673:   int i,j,k,l,rank,s;
                   11674:   mp_limb_t inv;
                   11675:   mp_limb_t a;
                   11676:   UINT c;
                   11677:   mp_limb_t *t,*pivot,*pk;
                   11678:   UINT *ck;
                   11679:   UINT **cmat;
                   11680:   UINT *ct;
                   11681:   ND_pairs pair;
                   11682:
                   11683:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   11684:   for ( i = 0; i < row; i++ ) {
                   11685:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   11686:     bzero(cmat[i],col*sizeof(UINT));
                   11687:   }
                   11688:
                   11689:   for ( rank = 0, j = 0; j < col; j++ ) {
                   11690:     for ( i = rank; i < row; i++ ) {
                   11691:       a = mat[i][j]; c = cmat[i][j];
                   11692:       MOD128(a,c,md);
                   11693:       mat[i][j] = a; cmat[i][j] = 0;
                   11694:     }
                   11695:     for ( i = rank; i < row; i++ )
                   11696:       if ( mat[i][j] )
                   11697:         break;
                   11698:     if ( i == row ) {
                   11699:       colstat[j] = 0;
                   11700:       continue;
                   11701:     } else
                   11702:       colstat[j] = 1;
                   11703:     if ( i != rank ) {
                   11704:       t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   11705:       ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
                   11706:       s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   11707:       if ( spactive ) {
                   11708:         pair = spactive[i]; spactive[i] = spactive[rank];
                   11709:         spactive[rank] = pair;
                   11710:       }
                   11711:     }
                   11712:     /* column j is normalized */
                   11713:     s = sugar[rank];
                   11714:     inv = invm((UINT)mat[rank][j],md);
                   11715:     /* normalize pivot row */
                   11716:     for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
                   11717:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   11718:     }
                   11719:     for ( i = rank+1; i < row; i++ ) {
                   11720:       if ( (a = mat[i][j]) != 0 ) {
                   11721:         sugar[i] = MAX(sugar[i],s);
                   11722:         red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11      noro     11723:         Nf4_red++;
1.7       noro     11724:       }
                   11725:     }
                   11726:     rank++;
                   11727:   }
                   11728:   for ( j = col-1, l = rank-1; j >= 0; j-- )
                   11729:     if ( colstat[j] ) {
                   11730:       for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
                   11731:         a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
                   11732:       }
                   11733:       s = sugar[l];
                   11734:       for ( i = 0; i < l; i++ ) {
                   11735:         a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
                   11736:         if ( a ) {
                   11737:           sugar[i] = MAX(sugar[i],s);
                   11738:           red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11      noro     11739:           Nf4_red++;
1.7       noro     11740:         }
                   11741:       }
                   11742:       l--;
                   11743:     }
                   11744:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   11745:   GCFREE(cmat);
                   11746:   return rank;
                   11747: }
                   11748:
1.28      noro     11749: 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)
                   11750: {
                   11751:   int i,j,k,l,rank,s,imin;
                   11752:   mp_limb_t inv;
                   11753:   mp_limb_t a;
                   11754:   UINT c;
                   11755:   mp_limb_t *t,*pivot,*pk;
                   11756:   UINT *ck;
                   11757:   UINT **cmat;
                   11758:   UINT *ct;
                   11759:   ND_pairs pair;
                   11760:   SIG sg;
1.31      noro     11761:   int *used;
1.28      noro     11762:
1.31      noro     11763:   used = (int *)MALLOC(row*sizeof(int));
1.28      noro     11764:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   11765:   for ( i = 0; i < row; i++ ) {
                   11766:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   11767:     bzero(cmat[i],col*sizeof(UINT));
                   11768:   }
                   11769:
1.31      noro     11770:   for ( j = 0; j < col; j++ ) {
                   11771:     for ( i = 0; i < row; i++ ) {
1.28      noro     11772:       a = mat[i][j]; c = cmat[i][j];
                   11773:       MOD128(a,c,md);
                   11774:       mat[i][j] = a; cmat[i][j] = 0;
                   11775:     }
1.31      noro     11776:     for ( i = 0; i < row; i++ )
                   11777:       if ( !used[i] && mat[i][j] ) break;
                   11778:     if ( i == row ) {
1.28      noro     11779:       colstat[j] = 0;
                   11780:       continue;
1.31      noro     11781:     } else {
1.28      noro     11782:       colstat[j] = 1;
1.31      noro     11783:       used[i] = 1;
1.28      noro     11784:     }
                   11785:     /* column j is normalized */
1.31      noro     11786:     s = sugar[i];
                   11787:     inv = invm((UINT)mat[i][j],md);
1.28      noro     11788:     /* normalize pivot row */
1.31      noro     11789:     for ( k = j, pk = mat[i]+j, ck = cmat[i]+j; k < col; k++, pk++, ck++ ) {
1.28      noro     11790:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   11791:     }
1.31      noro     11792:     for ( k = i+1; k < row; k++ ) {
                   11793:       if ( (a = mat[k][j]) != 0 ) {
                   11794:         sugar[k] = MAX(sugar[k],s);
                   11795:         red_by_vect64(md,mat[k]+j,cmat[k]+j,mat[i]+j,(int)(md-a),col-j);
1.28      noro     11796:         Nf4_red++;
                   11797:       }
                   11798:     }
                   11799:   }
1.31      noro     11800:   rank = 0;
                   11801:   for ( i = 0; i < row; i++ ) {
                   11802:     for ( j = 0; j < col; j++ )
                   11803:       if ( mat[i][j] ) break;
                   11804:     if ( j == col ) sugar[i] = -1;
                   11805:     else rank++;
                   11806:   }
1.28      noro     11807:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   11808:   GCFREE(cmat);
                   11809:   return rank;
                   11810: }
                   11811:
                   11812: NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   11813:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
                   11814: {
                   11815:     int spcol,sprow,a;
                   11816:     int i,j,k,l,rank;
                   11817:     NODE r0,r;
                   11818:     ND_pairs sp;
                   11819:     ND spol;
                   11820:     mp_limb_t **spmat;
                   11821:     mp_limb_t *svect,*cvect;
                   11822:     mp_limb_t *v;
                   11823:     int *colstat;
                   11824:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   11825:     int maxrs;
                   11826:     int *spsugar;
                   11827:     ND_pairs *spactive;
                   11828:     SIG *spsig;
                   11829:
                   11830:     get_eg(&eg0);
                   11831:     /* elimination (1st step) */
                   11832:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   11833:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11834:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   11835:     spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
                   11836:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   11837:         nd_sp(m,0,sp,&spol);
                   11838:         if ( !spol ) {
1.29      noro     11839:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     11840:           continue;
                   11841:         }
                   11842:         svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   11843:         nd_to_vect64(m,s0vect,col,spol,svect);
                   11844:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,spol->sig);
                   11845:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   11846:         if ( i < col ) {
                   11847:             spmat[sprow] = svect;
                   11848:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   11849:             spsig[sprow] = sp->sig;
                   11850:             sprow++;
                   11851:         } else {
1.29      noro     11852:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     11853:         }
                   11854:         nd_free(spol);
                   11855:     }
                   11856:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
                   11857:     if ( DP_Print ) {
                   11858:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   11859:         fflush(asir_out);
                   11860:     }
                   11861:     /* free index arrays */
                   11862:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   11863:
                   11864:     /* elimination (2nd step) */
                   11865:     colstat = (int *)MALLOC(col*sizeof(int));
                   11866:     rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
                   11867:     r0 = 0;
1.31      noro     11868:     for ( i = 0; i < sprow; i++ ) {
                   11869:         if ( spsugar[i] >= 0 ) {
                   11870:           NEXTNODE(r0,r);
                   11871:           BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
                   11872:           SG((NDV)BDY(r)) = spsugar[i];
                   11873:           ((NDV)BDY(r))->sig = spsig[i];
                   11874:         } else
                   11875:           syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
1.28      noro     11876:         GCFREE(spmat[i]);
                   11877:     }
                   11878:     if ( r0 ) NEXT(r) = 0;
                   11879:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
                   11880:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   11881:     if ( DP_Print ) {
                   11882:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   11883:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   11884:             nsp,nred,sprow,col,rank);
                   11885:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   11886:     }
                   11887:     return r0;
                   11888: }
1.40      noro     11889: #endif
1.28      noro     11890:
                   11891: NODE nd_f4_red_s(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,NODE *syzlistp)
                   11892: {
                   11893:   IndArray *imat;
                   11894:   int nsp,nred,i,start;
                   11895:   int *rhead;
                   11896:   NODE r0,rp;
                   11897:   ND_pairs sp;
                   11898:   NM_ind_pair *rvect;
                   11899:   UINT *s;
                   11900:   int *s0hash;
                   11901:   struct oEGT eg0,eg1,eg_conv;
                   11902:
                   11903:   for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   11904:   nred = length(rp0);
                   11905:   imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   11906:   rhead = (int *)MALLOC(col*sizeof(int));
                   11907:   for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   11908:
                   11909:   /* construction of index arrays */
                   11910:   get_eg(&eg0);
                   11911:   if ( DP_Print ) {
                   11912:     fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   11913:     fflush(asir_out);
                   11914:   }
                   11915:   rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   11916:   for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   11917:     rvect[i] = (NM_ind_pair)BDY(rp);
                   11918:     imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
                   11919:     rhead[imat[i]->head] = 1;
                   11920:     start = imat[i]->head;
                   11921:   }
                   11922:   get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
                   11923:   if ( DP_Print ) {
                   11924:     fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   11925:     fflush(asir_out);
                   11926:   }
                   11927:   if ( m > 0 )
1.40      noro     11928: #if SIZEOF_LONG==8
1.28      noro     11929:     r0 = nd_f4_red_mod64_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
1.40      noro     11930: #else
                   11931:     r0 = nd_f4_red_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
                   11932: #endif
1.28      noro     11933:   else
                   11934: //    r0 = nd_f4_red_q_main_s(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   11935:     error("nd_f4_red_q_main_s : not implemented yet");
                   11936:   return r0;
                   11937: }
                   11938:
                   11939: INLINE int ndl_find_reducer_minsig(UINT *dg)
                   11940: {
                   11941:   RHist r;
                   11942:   int i,singular,ret,d,k,imin;
                   11943:   SIG t;
                   11944:   static int wpd,nvar;
                   11945:   static SIG quo,quomin;
                   11946:   static UINT *tmp;
                   11947:
                   11948:   if ( !quo || nvar != nd_nvar ) { NEWSIG(quo); NEWSIG(quomin); }
                   11949:   if ( wpd != nd_wpd ) {
                   11950:     wpd = nd_wpd;
                   11951:     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
                   11952:   }
                   11953: #if 0
                   11954:   d = ndl_hash_value(dg);
                   11955:   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   11956:     if ( ndl_equal(dg,DL(r)) ) {
                   11957:       return r->index;
                   11958:     }
                   11959:   }
                   11960: #endif
                   11961:   imin = -1;
                   11962:   for ( i = 0; i < nd_psn; i++ ) {
                   11963:     r = nd_psh[i];
                   11964:     if ( ndl_reducible(dg,DL(r)) ) {
                   11965:       ndl_sub(dg,DL(r),tmp);
                   11966:       _ndltodl(tmp,DL(quo));
                   11967:       _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
                   11968:       quo->pos = nd_psh[i]->sig->pos;
1.55      noro     11969:       _adddl(nd_nvar,DL(quo),nd_sba_hm[quo->pos],DL2(quo));
1.28      noro     11970:       if ( imin < 0 || comp_sig(quomin,quo) > 0 ) {
                   11971:         t = quo; quo = quomin; quomin = t;
                   11972:         imin = i;
                   11973:       }
                   11974:     }
                   11975:   }
                   11976:   if ( imin == -1 ) return nd_psn;
                   11977:   else {
                   11978: #if 0
                   11979:     nd_append_red(dg,i);
                   11980: #endif
                   11981:     return imin;
                   11982:   }
                   11983: }
                   11984:
                   11985: int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   11986: {
                   11987:   NODE rp0,rp;
                   11988:   NM mul,head,s0,s;
                   11989:   int index,col,i,sugar;
                   11990:   RHist h;
                   11991:   UINT *s0v,*p;
                   11992:   NM_ind_pair pair;
                   11993:   ND red;
                   11994:   NDV *ps;
                   11995:   SIG sig;
                   11996:
                   11997:   s0 = 0; rp0 = 0; col = 0;
                   11998:   if ( nd_demand )
                   11999:     ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   12000:   else
                   12001:     ps = trace?nd_ps_trace:nd_ps;
                   12002:   while ( 1 ) {
                   12003:     head = remove_head_pbucket_symbolic(bucket);
                   12004:     if ( !head ) break;
                   12005:     if ( !s0 ) s0 = head;
                   12006:     else NEXT(s) = head;
                   12007:     s = head;
                   12008:     index = ndl_find_reducer_minsig(DL(head));
                   12009:     if ( index >= 0 && index < nd_psn ) {
                   12010:       h = nd_psh[index];
                   12011:       NEWNM(mul);
                   12012:       ndl_sub(DL(head),DL(h),DL(mul));
                   12013:       if ( ndl_check_bound2(index,DL(mul)) )
                   12014:         return 0;
                   12015:       sugar = TD(DL(mul))+SG(ps[index]);
                   12016:       NEWSIG(sig);
                   12017:       _ndltodl(DL(mul),DL(sig));
                   12018:       _addtodl(nd_nvar,DL(nd_psh[index]->sig),DL(sig));
                   12019:       sig->pos = nd_psh[index]->sig->pos;
1.55      noro     12020:       _adddl(nd_nvar,DL(sig),nd_sba_hm[sig->pos],DL2(sig));
1.28      noro     12021:       MKNM_ind_pair(pair,mul,index,sugar,sig);
                   12022:       red = ndv_mul_nm_symbolic(mul,ps[index]);
                   12023:       add_pbucket_symbolic(bucket,nd_remove_head(red));
                   12024:       NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   12025:     }
                   12026:     col++;
                   12027:   }
                   12028:   if ( rp0 ) NEXT(rp) = 0;
                   12029:   NEXT(s) = 0;
                   12030:   s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   12031:   for ( i = 0, p = s0v, s = s0; i < col;
                   12032:     i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   12033:   *s0vect = s0v;
                   12034:   *r = rp0;
                   12035:
                   12036:   return col;
                   12037: }
                   12038:
                   12039: NODE nd_sba_f4(int m,int **indp)
                   12040: {
1.34      noro     12041:   int i,nh,stat,index,f4red,f4step;
1.33      noro     12042:   int col,rank,len,k,j,a,sugar,nbase,psugar,ms;
                   12043:   NODE r,g,rp0,nflist;
1.41      noro     12044:   ND_pairs d,l,t,l1;
1.33      noro     12045:   ND h,nf;
                   12046:   NDV nfv;
                   12047:   union oNDC hc;
                   12048:   UINT *s0vect;
1.28      noro     12049:   UINT c;
                   12050:   PGeoBucket bucket;
1.33      noro     12051:   NODE *syzlist;
                   12052:   SIG sig;
1.28      noro     12053:   struct oEGT eg0,eg1,eg_f4;
1.33      noro     12054:   struct oEGT eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.28      noro     12055:
                   12056:   Nf4_red=0;
1.30      noro     12057:   d = 0;
1.29      noro     12058:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.28      noro     12059:   for ( i = 0; i < nd_psn; i++ ) {
1.30      noro     12060:     d = update_pairs_s(d,i,syzlist);
1.28      noro     12061:   }
1.30      noro     12062:   nd_nbase = nd_psn;
1.28      noro     12063:   f4red = 1;
1.33      noro     12064:   psugar = 0;
1.34      noro     12065:   f4step = 0;
1.28      noro     12066:   while ( d ) {
1.33      noro     12067:     for ( t = d, ms = SG(d); t; t = NEXT(t) )
                   12068:       if ( SG(t) < ms ) ms = SG(t);
1.38      noro     12069:     if ( ms == psugar && f4step >= nd_sba_f4step ) {
1.33      noro     12070: again:
                   12071:       l = d; d = d->next;
1.41      noro     12072: #if 0
1.33      noro     12073:       if ( small_lcm(l) ) {
                   12074:         if ( DP_Print ) fprintf(asir_out,"M");
                   12075:         continue;
                   12076:       }
                   12077:       sig = l->sig;
                   12078:       stat = nd_sp(m,0,l,&h);
1.41      noro     12079: #else
                   12080:       l1 = find_smallest_lcm(l);
                   12081:       if ( l1 == 0 ) {
                   12082:         if ( DP_Print ) fprintf(asir_out,"M");
                   12083:         continue;
                   12084:       }
                   12085:       sig = l1->sig;
                   12086:       stat = nd_sp(m,0,l1,&h);
                   12087: #endif
1.33      noro     12088:       if ( !stat ) {
                   12089:         NEXT(l) = d; d = l;
                   12090:         d = nd_reconstruct(0,d);
                   12091:         goto again;
                   12092:       }
                   12093:   get_eg(&eg1);
                   12094:   #if USE_GEOBUCKET
1.39      noro     12095:       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     12096:   #else
1.39      noro     12097:       stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.33      noro     12098:   #endif
                   12099:   get_eg(&eg2);
                   12100:       if ( !stat ) {
                   12101:         NEXT(l) = d; d = l;
                   12102:         d = nd_reconstruct(0,d);
                   12103:         goto again;
                   12104:       } else if ( stat == -1 ) {
                   12105:         if ( DP_Print ) { printf("S"); fflush(stdout); }
                   12106:         FREENDP(l);
                   12107:       } else if ( nf ) {
                   12108:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   12109:         add_eg(&eg_nf,&eg1,&eg2);
                   12110:         hc = HCU(nf);
                   12111:         nd_removecont(m,nf);
                   12112:         nfv = ndtondv(m,nf); nd_free(nf);
                   12113:         nh = ndv_newps(m,nfv,0);
                   12114:
1.30      noro     12115:         d = update_pairs_s(d,nh,syzlist);
                   12116:         nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.33      noro     12117:         FREENDP(l);
1.28      noro     12118:       } else {
1.33      noro     12119:         add_eg(&eg_nfzero,&eg1,&eg2);
                   12120:        // syzygy
                   12121:   get_eg(&eg1);
                   12122:         d = remove_spair_s(d,sig);
                   12123:   get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29      noro     12124:         syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.33      noro     12125:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   12126:         FREENDP(l);
                   12127:       }
                   12128:     } else {
1.34      noro     12129:       if ( ms != psugar ) f4step = 1;
                   12130:       else f4step++;
1.33      noro     12131: again2:
                   12132:       psugar = ms;
                   12133:       l = nd_minsugarp_s(d,&d);
                   12134:       sugar = nd_sugarweight?d->sugar2:SG(d);
                   12135:       bucket = create_pbucket();
                   12136:       stat = nd_sp_f4(m,0,l,bucket);
                   12137:       if ( !stat ) {
                   12138:         for ( t = l; NEXT(t); t = NEXT(t) );
                   12139:         NEXT(t) = d; d = l;
                   12140:         d = nd_reconstruct(0,d);
                   12141:         goto again2;
                   12142:       }
                   12143:       if ( bucket->m < 0 ) continue;
                   12144:       col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
                   12145:       if ( !col ) {
1.37      noro     12146:         for ( t = l; NEXT(t); t = NEXT(t) )
                   12147:           ;
                   12148:         NEXT(t) = d; d = l;
1.33      noro     12149:         d = nd_reconstruct(0,d);
                   12150:         goto again2;
                   12151:       }
1.34      noro     12152:       if ( DP_Print ) fprintf(asir_out,"\nsugar=%d,",psugar);
1.33      noro     12153:       nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
                   12154:       /* adding new bases */
                   12155:       for ( r = nflist; r; r = NEXT(r) ) {
                   12156:         nfv = (NDV)BDY(r);
1.38      noro     12157:         if ( nd_f4_td ) SG(nfv) = nd_tdeg(nfv);
1.33      noro     12158:         ndv_removecont(m,nfv);
                   12159:         nh = ndv_newps(m,nfv,0);
                   12160:         d = update_pairs_s(d,nh,syzlist);
                   12161:         nd_sba_pos[nfv->sig->pos] = append_one(nd_sba_pos[nfv->sig->pos],nh);
                   12162:       }
                   12163:       for ( i = 0; i < nd_nbase; i++ )
                   12164:         for ( r = syzlist[i]; r; r = NEXT(r) )
                   12165:             d = remove_spair_s(d,(SIG)BDY(r));
                   12166:       d = remove_large_lcm(d);
                   12167:       if ( DP_Print ) {
1.34      noro     12168:         fprintf(asir_out,"f4red=%d,gblen=%d",f4red,nd_psn); fflush(asir_out);
1.33      noro     12169:       }
                   12170:       f4red++;
1.28      noro     12171:     }
                   12172:   }
                   12173:   if ( DP_Print ) {
1.34      noro     12174:     fprintf(asir_out,"\nnumber of red=%d,",Nf4_red);
1.28      noro     12175:   }
1.30      noro     12176:   g = conv_ilist_s(nd_demand,0,indp);
1.28      noro     12177:   return g;
                   12178: }

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