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

1.39    ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.38 2020/10/26 02:41:05 noro Exp $ */
1.1       noro        2:
                      3: #include "nd.h"
                      4:
1.11      noro        5: int Nnd_add,Nf4_red;
1.12      noro        6: struct oEGT eg_search,f4_symb,f4_conv,f4_elim1,f4_elim2;
1.1       noro        7:
                      8: int diag_period = 6;
                      9: int weight_check = 1;
                     10: int (*ndl_compare_function)(UINT *a1,UINT *a2);
1.21      noro       11: /* for general module order */
1.19      noro       12: int (*ndl_base_compare_function)(UINT *a1,UINT *a2);
1.21      noro       13: int (*dl_base_compare_function)(int nv,DL a,DL b);
                     14: int nd_base_ordtype;
1.1       noro       15: int nd_dcomp;
                     16: int nd_rref2;
                     17: NM _nm_free_list;
                     18: ND _nd_free_list;
                     19: ND_pairs _ndp_free_list;
                     20: NODE nd_hcf;
1.35      noro       21: int Nsyz,Nsamesig;
1.1       noro       22:
                     23: Obj nd_top_weight;
                     24:
                     25: static NODE nd_subst;
                     26: static VL nd_vc;
                     27: static int nd_ntrans;
                     28: static int nd_nalg;
                     29: #if 0
                     30: static int ndv_alloc;
                     31: #endif
                     32: #if 1
                     33: static int nd_f4_nsp=0x7fffffff;
                     34: #else
                     35: static int nd_f4_nsp=50;
                     36: #endif
                     37: static double nd_scale=2;
                     38: static UINT **nd_bound;
                     39: static struct order_spec *nd_ord;
                     40: static EPOS nd_epos;
                     41: static BlockMask nd_blockmask;
                     42: static int nd_nvar;
                     43: static int nd_isrlex;
                     44: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
                     45: static UINT nd_mask[32];
                     46: static UINT nd_mask0,nd_mask1;
                     47:
                     48: static NDV *nd_ps;
                     49: static NDV *nd_ps_trace;
                     50: static NDV *nd_ps_sym;
                     51: static NDV *nd_ps_trace_sym;
                     52: static RHist *nd_psh;
1.30      noro       53: static int nd_psn,nd_pslen,nd_nbase;
1.1       noro       54: static RHist *nd_red;
                     55: static int *nd_work_vector;
                     56: static int **nd_matrix;
                     57: static int nd_matrix_len;
                     58: static struct weight_or_block *nd_worb;
                     59: static int nd_worb_len;
                     60: static int nd_found,nd_create,nd_notfirst;
                     61: static int nmv_adv;
                     62: static int nd_demand;
1.21      noro       63: static int nd_module,nd_module_ordtype,nd_mpos,nd_pot_nelim;
1.1       noro       64: static int nd_module_rank,nd_poly_weight_len;
                     65: static int *nd_poly_weight,*nd_module_weight;
                     66: static NODE nd_tracelist;
                     67: static NODE nd_alltracelist;
                     68: static int nd_gentrace,nd_gensyz,nd_nora,nd_newelim,nd_intersect,nd_lf;
1.39    ! noro       69: static int nd_f4_td,nd_sba_f4step,nd_sba_pot,nd_sba_largelcm,nd_sba_dontsort;
        !            70: static int nd_top;
1.1       noro       71: static int *nd_gbblock;
                     72: static NODE nd_nzlist,nd_check_splist;
                     73: static int nd_splist;
                     74: static int *nd_sugarweight;
                     75: static int nd_f4red,nd_rank0,nd_last_nonzero;
1.27      noro       76: static DL *nd_sba_hm;
1.29      noro       77: static NODE *nd_sba_pos;
1.1       noro       78:
                     79: NumberField get_numberfield();
                     80: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
                     81: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
                     82: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
                     83: int nd_monic(int m,ND *p);
                     84: NDV plain_vect_to_ndv_q(Z *mat,int col,UINT *s0vect);
                     85: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank);
                     86: NDV pltondv(VL vl,VL dvl,LIST p);
                     87: void pltozpl(LIST l,Q *cont,LIST *pp);
                     88: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
                     89: void nmtodp(int mod,NM m,DP *r);
1.15      noro       90: void ndltodp(UINT *d,DP *r);
1.1       noro       91: NODE reverse_node(NODE n);
                     92: P ndc_div(int mod,union oNDC a,union oNDC b);
                     93: P ndctop(int mod,union oNDC c);
                     94: void finalize_tracelist(int i,P cont);
                     95: void conv_ilist(int demand,int trace,NODE g,int **indp);
                     96: void parse_nd_option(NODE opt);
                     97: void dltondl(int n,DL dl,UINT *r);
                     98: DP ndvtodp(int mod,NDV p);
                     99: DP ndtodp(int mod,ND p);
1.16      noro      100: DPM ndvtodpm(int mod,NDV p);
                    101: NDV dpmtondv(int mod,DPM p);
                    102: int dpm_getdeg(DPM p,int *rank);
                    103: void dpm_ptozp(DPM p,Z *cont,DPM *r);
                    104: int compdmm(int nv,DMM a,DMM b);
1.1       noro      105:
                    106: void Pdp_set_weight(NODE,VECT *);
                    107: void Pox_cmo_rpc(NODE,Obj *);
                    108:
                    109: ND nd_add_lf(ND p1,ND p2);
                    110: void nd_mul_c_lf(ND p,Z mul);
                    111: void ndv_mul_c_lf(NDV p,Z mul);
                    112: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                    113:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
                    114: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                    115:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
                    116: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                    117:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
                    118: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
                    119: NODE nd_f4_lf_trace_main(int m,int **indp);
                    120: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
                    121:
                    122: extern int lf_lazy;
                    123: extern Z current_mod_lf;
                    124:
                    125: extern int Denominator,DP_Multiple,MaxDeg;
                    126:
                    127: #define BLEN (8*sizeof(unsigned long))
                    128:
                    129: typedef struct matrix {
                    130:   int row,col;
                    131:   unsigned long **a;
                    132: } *matrix;
                    133:
                    134:
                    135: void nd_free_private_storage()
                    136: {
                    137:     _nm_free_list = 0;
                    138:     _ndp_free_list = 0;
                    139: #if 0
                    140:     GC_gcollect();
                    141: #endif
                    142: }
                    143:
                    144: void _NM_alloc()
                    145: {
                    146:     NM p;
                    147:     int i;
                    148:
                    149:     for ( i = 0; i < 1024; i++ ) {
                    150:         p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                    151:         p->next = _nm_free_list; _nm_free_list = p;
                    152:     }
                    153: }
                    154:
                    155: matrix alloc_matrix(int row,int col)
                    156: {
                    157:   unsigned long **a;
                    158:   int i,len,blen;
                    159:   matrix mat;
                    160:
                    161:   mat = (matrix)MALLOC(sizeof(struct matrix));
                    162:   mat->row = row;
                    163:   mat->col = col;
                    164:   mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
                    165:   return mat;
                    166: }
                    167:
                    168:
                    169: void _ND_alloc()
                    170: {
                    171:     ND p;
                    172:     int i;
                    173:
                    174:     for ( i = 0; i < 1024; i++ ) {
                    175:         p = (ND)MALLOC(sizeof(struct oND));
                    176:         p->body = (NM)_nd_free_list; _nd_free_list = p;
                    177:     }
                    178: }
                    179:
                    180: void _NDP_alloc()
                    181: {
                    182:     ND_pairs p;
                    183:     int i;
                    184:
                    185:     for ( i = 0; i < 1024; i++ ) {
                    186:         p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
                    187:             +(nd_wpd-1)*sizeof(UINT));
                    188:         p->next = _ndp_free_list; _ndp_free_list = p;
                    189:     }
                    190: }
                    191:
                    192: INLINE int nd_length(ND p)
                    193: {
                    194:     NM m;
                    195:     int i;
                    196:
                    197:     if ( !p )
                    198:         return 0;
                    199:     else {
                    200:         for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
                    201:         return i;
                    202:     }
                    203: }
                    204:
                    205: extern int dp_negative_weight;
                    206:
                    207: INLINE int ndl_reducible(UINT *d1,UINT *d2)
                    208: {
                    209:     UINT u1,u2;
                    210:     int i,j;
                    211:
                    212:     if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
                    213:
                    214:     if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
                    215: #if USE_UNROLL
                    216:     switch ( nd_bpe ) {
                    217:         case 3:
                    218:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    219:                 u1 = d1[i]; u2 = d2[i];
                    220:                 if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
                    221:                 if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
                    222:                 if ( (u1&  0xe00000) < (u2&  0xe00000) ) return 0;
                    223:                 if ( (u1&  0x1c0000) < (u2&  0x1c0000) ) return 0;
                    224:                 if ( (u1&   0x38000) < (u2&   0x38000) ) return 0;
                    225:                 if ( (u1&    0x7000) < (u2&    0x7000) ) return 0;
                    226:                 if ( (u1&     0xe00) < (u2&     0xe00) ) return 0;
                    227:                 if ( (u1&     0x1c0) < (u2&     0x1c0) ) return 0;
                    228:                 if ( (u1&      0x38) < (u2&      0x38) ) return 0;
                    229:                 if ( (u1&       0x7) < (u2&       0x7) ) return 0;
                    230:             }
                    231:             return 1;
                    232:             break;
                    233:         case 4:
                    234:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    235:                 u1 = d1[i]; u2 = d2[i];
                    236:                 if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
                    237:                 if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
                    238:                 if ( (u1&  0xf00000) < (u2&  0xf00000) ) return 0;
                    239:                 if ( (u1&   0xf0000) < (u2&   0xf0000) ) return 0;
                    240:                 if ( (u1&    0xf000) < (u2&    0xf000) ) return 0;
                    241:                 if ( (u1&     0xf00) < (u2&     0xf00) ) return 0;
                    242:                 if ( (u1&      0xf0) < (u2&      0xf0) ) return 0;
                    243:                 if ( (u1&       0xf) < (u2&       0xf) ) return 0;
                    244:             }
                    245:             return 1;
                    246:             break;
                    247:         case 6:
                    248:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    249:                 u1 = d1[i]; u2 = d2[i];
                    250:                 if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
                    251:                 if ( (u1&  0xfc0000) < (u2&  0xfc0000) ) return 0;
                    252:                 if ( (u1&   0x3f000) < (u2&   0x3f000) ) return 0;
                    253:                 if ( (u1&     0xfc0) < (u2&     0xfc0) ) return 0;
                    254:                 if ( (u1&      0x3f) < (u2&      0x3f) ) return 0;
                    255:             }
                    256:             return 1;
                    257:             break;
                    258:         case 8:
                    259:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    260:                 u1 = d1[i]; u2 = d2[i];
                    261:                 if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
                    262:                 if ( (u1&  0xff0000) < (u2&  0xff0000) ) return 0;
                    263:                 if ( (u1&    0xff00) < (u2&    0xff00) ) return 0;
                    264:                 if ( (u1&      0xff) < (u2&      0xff) ) return 0;
                    265:             }
                    266:             return 1;
                    267:             break;
                    268:         case 16:
                    269:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    270:                 u1 = d1[i]; u2 = d2[i];
                    271:                 if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
                    272:                 if ( (u1&    0xffff) < (u2&    0xffff) ) return 0;
                    273:             }
                    274:             return 1;
                    275:             break;
                    276:         case 32:
                    277:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                    278:                 if ( d1[i] < d2[i] ) return 0;
                    279:             return 1;
                    280:             break;
                    281:         default:
                    282:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    283:                 u1 = d1[i]; u2 = d2[i];
                    284:                 for ( j = 0; j < nd_epw; j++ )
                    285:                     if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    286:             }
                    287:             return 1;
                    288:     }
                    289: #else
                    290:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    291:         u1 = d1[i]; u2 = d2[i];
                    292:         for ( j = 0; j < nd_epw; j++ )
                    293:             if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    294:     }
                    295:     return 1;
                    296: #endif
                    297: }
                    298:
                    299: /*
                    300:  * If the current order is a block order,
                    301:  * then the last block is length 1 and contains
                    302:  * the homo variable. Otherwise, the original
                    303:  * order is either 0 or 2.
                    304:  */
                    305:
                    306: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
                    307: {
                    308:     int w,i,e,n,omask0;
                    309:
                    310:     omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
                    311:     n = nd_nvar-1;
                    312:     ndl_zero(r);
                    313:     for ( i = 0; i < n; i++ ) {
                    314:         e = GET_EXP_OLD(d,i);
                    315:         PUT_EXP(r,i,e);
                    316:     }
                    317:     w = TD(d);
                    318:     PUT_EXP(r,nd_nvar-1,weight-w);
                    319:     if ( nd_module ) MPOS(r) = d[ompos];
                    320:     TD(r) = weight;
                    321:     if ( nd_blockmask ) ndl_weight_mask(r);
                    322: }
                    323:
                    324: void ndl_dehomogenize(UINT *d)
                    325: {
                    326:     UINT mask;
                    327:     UINT h;
                    328:     int i,bits;
                    329:
                    330:     if ( nd_blockmask ) {
                    331:         h = GET_EXP(d,nd_nvar-1);
                    332:         XOR_EXP(d,nd_nvar-1,h);
                    333:         TD(d) -= h;
                    334:         ndl_weight_mask(d);
                    335:     } else {
                    336:         if ( nd_isrlex ) {
                    337:             if ( nd_bpe == 32 ) {
                    338:                 h = d[nd_exporigin];
                    339:                 for ( i = nd_exporigin+1; i < nd_wpd; i++ )
                    340:                     d[i-1] = d[i];
                    341:                 d[i-1] = 0;
                    342:                 TD(d) -= h;
                    343:             } else {
                    344:                 bits = nd_epw*nd_bpe;
                    345:                 mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
                    346:                 h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
                    347:                 for ( i = nd_exporigin; i < nd_wpd; i++ )
                    348:                     d[i] = ((d[i]<<nd_bpe)&mask)
                    349:                         |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
                    350:                 TD(d) -= h;
                    351:             }
                    352:         } else {
                    353:             h = GET_EXP(d,nd_nvar-1);
                    354:             XOR_EXP(d,nd_nvar-1,h);
                    355:             TD(d) -= h;
                    356:         }
                    357:     }
                    358: }
                    359:
                    360: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
                    361: {
                    362:     UINT t1,t2,u,u1,u2;
                    363:     int i,j,l;
                    364:
                    365:     if ( nd_module && (MPOS(d1) != MPOS(d2)) )
                    366:         error("ndl_lcm : inconsistent monomials");
                    367: #if USE_UNROLL
                    368:     switch ( nd_bpe ) {
                    369:         case 3:
                    370:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    371:                 u1 = d1[i]; u2 = d2[i];
                    372:                 t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
                    373:                 t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
                    374:                 t1 = (u1&  0xe00000); t2 = (u2&  0xe00000); u |= t1>t2?t1:t2;
                    375:                 t1 = (u1&  0x1c0000); t2 = (u2&  0x1c0000); u |= t1>t2?t1:t2;
                    376:                 t1 = (u1&   0x38000); t2 = (u2&   0x38000); u |= t1>t2?t1:t2;
                    377:                 t1 = (u1&    0x7000); t2 = (u2&    0x7000); u |= t1>t2?t1:t2;
                    378:                 t1 = (u1&     0xe00); t2 = (u2&     0xe00); u |= t1>t2?t1:t2;
                    379:                 t1 = (u1&     0x1c0); t2 = (u2&     0x1c0); u |= t1>t2?t1:t2;
                    380:                 t1 = (u1&      0x38); t2 = (u2&      0x38); u |= t1>t2?t1:t2;
                    381:                 t1 = (u1&       0x7); t2 = (u2&       0x7); u |= t1>t2?t1:t2;
                    382:                 d[i] = u;
                    383:             }
                    384:             break;
                    385:         case 4:
                    386:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    387:                 u1 = d1[i]; u2 = d2[i];
                    388:                 t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
                    389:                 t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
                    390:                 t1 = (u1&  0xf00000); t2 = (u2&  0xf00000); u |= t1>t2?t1:t2;
                    391:                 t1 = (u1&   0xf0000); t2 = (u2&   0xf0000); u |= t1>t2?t1:t2;
                    392:                 t1 = (u1&    0xf000); t2 = (u2&    0xf000); u |= t1>t2?t1:t2;
                    393:                 t1 = (u1&     0xf00); t2 = (u2&     0xf00); u |= t1>t2?t1:t2;
                    394:                 t1 = (u1&      0xf0); t2 = (u2&      0xf0); u |= t1>t2?t1:t2;
                    395:                 t1 = (u1&       0xf); t2 = (u2&       0xf); u |= t1>t2?t1:t2;
                    396:                 d[i] = u;
                    397:             }
                    398:             break;
                    399:         case 6:
                    400:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    401:                 u1 = d1[i]; u2 = d2[i];
                    402:                 t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
                    403:                 t1 = (u1&  0xfc0000); t2 = (u2&  0xfc0000); u |= t1>t2?t1:t2;
                    404:                 t1 = (u1&   0x3f000); t2 = (u2&   0x3f000); u |= t1>t2?t1:t2;
                    405:                 t1 = (u1&     0xfc0); t2 = (u2&     0xfc0); u |= t1>t2?t1:t2;
                    406:                 t1 = (u1&      0x3f); t2 = (u2&      0x3f); u |= t1>t2?t1:t2;
                    407:                 d[i] = u;
                    408:             }
                    409:             break;
                    410:         case 8:
                    411:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    412:                 u1 = d1[i]; u2 = d2[i];
                    413:                 t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
                    414:                 t1 = (u1&  0xff0000); t2 = (u2&  0xff0000); u |= t1>t2?t1:t2;
                    415:                 t1 = (u1&    0xff00); t2 = (u2&    0xff00); u |= t1>t2?t1:t2;
                    416:                 t1 = (u1&      0xff); t2 = (u2&      0xff); u |= t1>t2?t1:t2;
                    417:                 d[i] = u;
                    418:             }
                    419:             break;
                    420:         case 16:
                    421:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    422:                 u1 = d1[i]; u2 = d2[i];
                    423:                 t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
                    424:                 t1 = (u1&    0xffff); t2 = (u2&    0xffff); u |= t1>t2?t1:t2;
                    425:                 d[i] = u;
                    426:             }
                    427:             break;
                    428:         case 32:
                    429:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    430:                 u1 = d1[i]; u2 = d2[i];
                    431:                 d[i] = u1>u2?u1:u2;
                    432:             }
                    433:             break;
                    434:         default:
                    435:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    436:                 u1 = d1[i]; u2 = d2[i];
                    437:                 for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    438:                     t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    439:                 }
                    440:                 d[i] = u;
                    441:             }
                    442:             break;
                    443:     }
                    444: #else
                    445:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    446:         u1 = d1[i]; u2 = d2[i];
                    447:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    448:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    449:         }
                    450:         d[i] = u;
                    451:     }
                    452: #endif
                    453:     if ( nd_module ) MPOS(d) = MPOS(d1);
                    454:     TD(d) = ndl_weight(d);
                    455:     if ( nd_blockmask ) ndl_weight_mask(d);
                    456: }
                    457:
                    458: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
                    459: {
                    460:     UINT t1,t2,u,u1,u2;
                    461:     int i,j,l;
                    462:
                    463:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    464:         u1 = d1[i]; u2 = d2[i];
                    465:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    466:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    467:         }
                    468:         d[i] = u;
                    469:     }
                    470: }
                    471:
                    472: int ndl_weight(UINT *d)
                    473: {
                    474:     UINT t,u;
                    475:     int i,j;
                    476:
                    477:     if ( current_dl_weight_vector )
                    478:         for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    479:             u = GET_EXP(d,i);
                    480:             t += MUL_WEIGHT(u,i);
                    481:         }
                    482:     else
                    483:         for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    484:             u = d[i];
                    485:             for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    486:                 t += (u&nd_mask0);
                    487:         }
1.20      noro      488:     if ( nd_module && nd_module_rank && MPOS(d) )
                    489:         t += nd_module_weight[MPOS(d)-1];
                    490:     for ( i = nd_exporigin; i < nd_wpd; i++ )
                    491:       if ( d[i] && !t )
                    492:         printf("afo\n");
1.1       noro      493:     return t;
                    494: }
                    495:
                    496: /* for sugarweight */
                    497:
                    498: int ndl_weight2(UINT *d)
                    499: {
                    500:     int t,u;
                    501:     int i,j;
                    502:
                    503:     for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    504:         u = GET_EXP(d,i);
                    505:         t += nd_sugarweight[i]*u;
                    506:     }
1.20      noro      507:     if ( nd_module && nd_module_rank && MPOS(d) )
                    508:         t += nd_module_weight[MPOS(d)-1];
1.1       noro      509:     return t;
                    510: }
                    511:
                    512: void ndl_weight_mask(UINT *d)
                    513: {
                    514:     UINT t,u;
                    515:     UINT *mask;
                    516:     int i,j,k,l;
                    517:
                    518:     l = nd_blockmask->n;
                    519:     for ( k = 0; k < l; k++ ) {
                    520:         mask = nd_blockmask->mask[k];
                    521:         if ( current_dl_weight_vector )
                    522:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    523:                 u = GET_EXP_MASK(d,i,mask);
                    524:                 t += MUL_WEIGHT(u,i);
                    525:             }
                    526:         else
                    527:             for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    528:                 u = d[i]&mask[i];
                    529:                 for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    530:                     t += (u&nd_mask0);
                    531:             }
                    532:         d[k+1] = t;
                    533:     }
                    534: }
                    535:
1.21      noro      536: int ndl_glex_compare(UINT *d1,UINT *d2)
                    537: {
                    538:   if ( TD(d1) > TD(d2) ) return 1;
                    539:   else if ( TD(d1) < TD(d2) ) return -1;
                    540:   else return ndl_lex_compare(d1,d2);
                    541: }
                    542:
1.1       noro      543: int ndl_lex_compare(UINT *d1,UINT *d2)
                    544: {
                    545:     int i;
                    546:
                    547:     d1 += nd_exporigin;
                    548:     d2 += nd_exporigin;
                    549:     for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
                    550:         if ( *d1 > *d2 )
                    551:             return nd_isrlex ? -1 : 1;
                    552:         else if ( *d1 < *d2 )
                    553:             return nd_isrlex ? 1 : -1;
                    554:     return 0;
                    555: }
                    556:
                    557: int ndl_block_compare(UINT *d1,UINT *d2)
                    558: {
                    559:     int i,l,j,ord_o,ord_l;
                    560:     struct order_pair *op;
                    561:     UINT t1,t2,m;
                    562:     UINT *mask;
                    563:
                    564:     l = nd_blockmask->n;
                    565:     op = nd_blockmask->order_pair;
                    566:     for ( j = 0; j < l; j++ ) {
                    567:         mask = nd_blockmask->mask[j];
                    568:         ord_o = op[j].order;
                    569:         if ( ord_o < 2 ) {
                    570:             if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
                    571:             else if ( t1 < t2 ) return -1;
                    572:         }
                    573:         for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    574:             m = mask[i];
                    575:             t1 = d1[i]&m;
                    576:             t2 = d2[i]&m;
                    577:             if ( t1 > t2 )
                    578:                 return !ord_o ? -1 : 1;
                    579:             else if ( t1 < t2 )
                    580:                 return !ord_o ? 1 : -1;
                    581:         }
                    582:     }
                    583:     return 0;
                    584: }
                    585:
                    586: int ndl_matrix_compare(UINT *d1,UINT *d2)
                    587: {
                    588:   int i,j,s,row;
                    589:   int *v;
                    590:   Z **mat;
                    591:   Z *w;
                    592:   Z t1;
                    593:   Z t,t2;
                    594:
1.6       noro      595:   for ( j = 0; j < nd_nvar; j++ )
                    596:     nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.1       noro      597:   if ( nd_top_weight ) {
                    598:     if ( OID(nd_top_weight) == O_VECT ) {
1.6       noro      599:       mat = (Z **)&BDY((VECT)nd_top_weight);
                    600:       row = 1;
1.1       noro      601:     } else {
                    602:       mat = (Z **)BDY((MAT)nd_top_weight);
1.6       noro      603:       row = ((MAT)nd_top_weight)->row;
1.1       noro      604:     }
                    605:     for ( i = 0; i < row; i++ ) {
1.6       noro      606:       w = mat[i];
1.1       noro      607:       for ( j = 0, t = 0; j < nd_nvar; j++ ) {
1.6       noro      608:         STOZ(nd_work_vector[j],t1);
1.1       noro      609:         mulz(w[j],t1,&t2);
                    610:         addz(t,t2,&t1);
                    611:         t = t1;
                    612:       }
1.6       noro      613:       if ( t ) {
                    614:         s = sgnz(t);
1.1       noro      615:         if ( s > 0 ) return 1;
                    616:         else if ( s < 0 ) return -1;
1.6       noro      617:       }
1.1       noro      618:     }
1.6       noro      619:   }
                    620:   for ( i = 0; i < nd_matrix_len; i++ ) {
                    621:     v = nd_matrix[i];
                    622:     for ( j = 0, s = 0; j < nd_nvar; j++ )
                    623:       s += v[j]*nd_work_vector[j];
                    624:     if ( s > 0 ) return 1;
                    625:     else if ( s < 0 ) return -1;
                    626:   }
1.1       noro      627:   if ( !ndl_equal(d1,d2) )
1.6       noro      628:     error("ndl_matrix_compare : invalid matrix");
                    629:   return 0;
1.1       noro      630: }
                    631:
                    632: int ndl_composite_compare(UINT *d1,UINT *d2)
                    633: {
                    634:     int i,j,s,start,end,len,o;
                    635:     int *v;
                    636:     struct sparse_weight *sw;
                    637:
                    638:     for ( j = 0; j < nd_nvar; j++ )
                    639:         nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
                    640:     for ( i = 0; i < nd_worb_len; i++ ) {
                    641:         len = nd_worb[i].length;
                    642:         switch ( nd_worb[i].type ) {
                    643:             case IS_DENSE_WEIGHT:
                    644:                 v = nd_worb[i].body.dense_weight;
                    645:                 for ( j = 0, s = 0; j < len; j++ )
                    646:                     s += v[j]*nd_work_vector[j];
                    647:                 if ( s > 0 ) return 1;
                    648:                 else if ( s < 0 ) return -1;
                    649:                 break;
                    650:             case IS_SPARSE_WEIGHT:
                    651:                 sw = nd_worb[i].body.sparse_weight;
                    652:                 for ( j = 0, s = 0; j < len; j++ )
                    653:                     s += sw[j].value*nd_work_vector[sw[j].pos];
                    654:                 if ( s > 0 ) return 1;
                    655:                 else if ( s < 0 ) return -1;
                    656:                 break;
                    657:             case IS_BLOCK:
                    658:                 o = nd_worb[i].body.block.order;
                    659:                 start = nd_worb[i].body.block.start;
                    660:                 switch ( o ) {
                    661:                     case 0:
                    662:                         end = start+len;
                    663:                         for ( j = start, s = 0; j < end; j++ )
                    664:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    665:                         if ( s > 0 ) return 1;
                    666:                         else if ( s < 0 ) return -1;
                    667:                         for ( j = end-1; j >= start; j-- )
                    668:                             if ( nd_work_vector[j] < 0 ) return 1;
                    669:                             else if ( nd_work_vector[j] > 0 ) return -1;
                    670:                         break;
                    671:                     case 1:
                    672:                         end = start+len;
                    673:                         for ( j = start, s = 0; j < end; j++ )
                    674:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    675:                         if ( s > 0 ) return 1;
                    676:                         else if ( s < 0 ) return -1;
                    677:                         for ( j = start; j < end; j++ )
                    678:                             if ( nd_work_vector[j] > 0 ) return 1;
                    679:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    680:                         break;
                    681:                     case 2:
                    682:                         end = start+len;
                    683:                         for ( j = start; j < end; j++ )
                    684:                             if ( nd_work_vector[j] > 0 ) return 1;
                    685:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    686:                         break;
                    687:                 }
                    688:                 break;
                    689:         }
                    690:     }
                    691:     return 0;
                    692: }
                    693:
                    694: /* TDH -> WW -> TD-> RL */
                    695:
                    696: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
                    697: {
                    698:     int i,m,e1,e2;
                    699:
                    700:     if ( TD(d1) > TD(d2) ) return 1;
                    701:     else if ( TD(d1) < TD(d2) ) return -1;
                    702:     m = nd_nvar>>1;
                    703:     for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
                    704:         e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
                    705:         e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
                    706:     }
                    707:     if ( e1 > e2 ) return 1;
                    708:     else if ( e1 < e2 ) return -1;
                    709:     return ndl_lex_compare(d1,d2);
                    710: }
                    711:
1.21      noro      712: // common function for module glex and grlex comparison
                    713: int ndl_module_glex_compare(UINT *d1,UINT *d2)
1.1       noro      714: {
1.21      noro      715:   int c;
1.1       noro      716:
1.21      noro      717:   switch ( nd_module_ordtype ) {
                    718:     case 0:
                    719:       if ( TD(d1) > TD(d2) ) return 1;
                    720:       else if ( TD(d1) < TD(d2) ) return -1;
                    721:       else if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
                    722:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    723:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    724:       else return 0;
                    725:       break;
1.1       noro      726:
1.21      noro      727:     case 1:
1.19      noro      728:       if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
                    729:          if ( TD(d1) > TD(d2) ) return 1;
                    730:          else if ( TD(d1) < TD(d2) ) return -1;
                    731:          if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
                    732:          if ( MPOS(d1) < MPOS(d2) ) return 1;
                    733:          else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    734:       }
                    735:       if ( MPOS(d1) < MPOS(d2) ) return 1;
                    736:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
1.21      noro      737:       else if ( TD(d1) > TD(d2) ) return 1;
                    738:       else if ( TD(d1) < TD(d2) ) return -1;
                    739:       else return ndl_lex_compare(d1,d2);
                    740:       break;
1.1       noro      741:
1.21      noro      742:     case 2: // weight -> POT
                    743:       if ( TD(d1) > TD(d2) ) return 1;
                    744:       else if ( TD(d1) < TD(d2) ) return -1;
                    745:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    746:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    747:       else return ndl_lex_compare(d1,d2);
                    748:       break;
1.1       noro      749:
1.21      noro      750:     default:
                    751:       error("ndl_module_glex_compare : invalid module_ordtype");
1.28      noro      752:       return 0;
1.21      noro      753:   }
1.1       noro      754: }
                    755:
1.21      noro      756: // common  for module comparison
                    757: int ndl_module_compare(UINT *d1,UINT *d2)
1.1       noro      758: {
1.21      noro      759:   int c;
1.1       noro      760:
1.21      noro      761:   switch ( nd_module_ordtype ) {
                    762:     case 0:
1.23      noro      763:       if ( (c = (*ndl_base_compare_function)(d1,d2)) != 0 ) return c;
1.21      noro      764:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    765:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    766:       else return 0;
                    767:       break;
1.1       noro      768:
1.21      noro      769:     case 1:
                    770:       if ( MPOS(d1) < MPOS(d2) ) return 1;
                    771:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    772:       else return (*ndl_base_compare_function)(d1,d2);
                    773:       break;
1.1       noro      774:
1.21      noro      775:     case 2: // weight -> POT
                    776:       if ( TD(d1) > TD(d2) ) return 1;
                    777:       else if ( TD(d1) < TD(d2) ) return -1;
                    778:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    779:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    780:       else return (*ndl_base_compare_function)(d1,d2);
                    781:       break;
1.1       noro      782:
1.21      noro      783:     default:
                    784:       error("ndl_module_compare : invalid module_ordtype");
1.28      noro      785:       return 0;
1.21      noro      786:   }
1.1       noro      787: }
                    788:
1.21      noro      789: extern DMMstack dmm_stack;
                    790: void _addtodl(int n,DL d1,DL d2);
1.31      noro      791: void _adddl(int n,DL d1,DL d2,DL d3);
1.21      noro      792: int _eqdl(int n,DL d1,DL d2);
                    793:
                    794: int ndl_module_schreyer_compare(UINT *m1,UINT *m2)
                    795: {
                    796:   int pos1,pos2,t,j;
                    797:   DMM *in;
                    798:   DMMstack s;
                    799:   static DL d1=0;
                    800:   static DL d2=0;
                    801:   static int dlen=0;
                    802:
                    803:   pos1 = MPOS(m1); pos2 = MPOS(m2);
                    804:   if ( pos1 == pos2 ) return (*ndl_base_compare_function)(m1,m2);
                    805:   if ( nd_nvar > dlen ) {
                    806:     NEWDL(d1,nd_nvar);
                    807:     NEWDL(d2,nd_nvar);
                    808:     dlen = nd_nvar;
                    809:   }
                    810:   d1->td = TD(m1);
                    811:   for ( j = 0; j < nd_nvar; j++ ) d1->d[j] = GET_EXP(m1,j);
                    812:   d2->td = TD(m2);
                    813:   for ( j = 0; j < nd_nvar; j++ ) d2->d[j] = GET_EXP(m2,j);
                    814:   for ( s = dmm_stack; s; s = NEXT(s) ) {
                    815:     in = s->in;
                    816:     _addtodl(nd_nvar,in[pos1]->dl,d1);
                    817:     _addtodl(nd_nvar,in[pos2]->dl,d2);
                    818:     if ( in[pos1]->pos == in[pos2]->pos && _eqdl(nd_nvar,d1,d2)) {
                    819:       if ( pos1 < pos2 ) return 1;
                    820:       else if ( pos1 > pos2 ) return -1;
                    821:       else return 0;
                    822:     }
                    823:     pos1 = in[pos1]->pos;
                    824:     pos2 = in[pos2]->pos;
                    825:     if ( pos1 == pos2 ) return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    826:   }
                    827:   // comparison by the bottom order
                    828: LAST:
                    829:   switch ( nd_base_ordtype ) {
                    830:     case 0:
                    831:       t = (*dl_base_compare_function)(nd_nvar,d1,d2);
                    832:       if ( t ) return t;
                    833:       else if ( pos1 < pos2 ) return 1;
                    834:       else if ( pos1 > pos2 ) return -1;
                    835:       else return 0;
                    836:       break;
                    837:     case 1:
                    838:       if ( pos1 < pos2 ) return 1;
                    839:       else if ( pos1 > pos2 ) return -1;
                    840:       else return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    841:       break;
                    842:     case 2:
                    843:       if ( d1->td > d2->td  ) return 1;
                    844:       else if ( d1->td < d2->td ) return -1;
                    845:       else if ( pos1 < pos2 ) return 1;
                    846:       else if ( pos1 > pos2 ) return -1;
                    847:       else return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    848:       break;
                    849:     default:
                    850:       error("ndl_schreyer_compare : invalid base ordtype");
1.28      noro      851:       return 0;
1.21      noro      852:   }
1.1       noro      853: }
                    854:
                    855: INLINE int ndl_equal(UINT *d1,UINT *d2)
                    856: {
                    857:     int i;
                    858:
                    859:     switch ( nd_wpd ) {
                    860:         case 2:
                    861:             if ( TD(d2) != TD(d1) ) return 0;
                    862:             if ( d2[1] != d1[1] ) return 0;
                    863:             return 1;
                    864:             break;
                    865:         case 3:
                    866:             if ( TD(d2) != TD(d1) ) return 0;
                    867:             if ( d2[1] != d1[1] ) return 0;
                    868:             if ( d2[2] != d1[2] ) return 0;
                    869:             return 1;
                    870:             break;
                    871:         default:
                    872:             for ( i = 0; i < nd_wpd; i++ )
                    873:                 if ( *d1++ != *d2++ ) return 0;
                    874:             return 1;
                    875:             break;
                    876:     }
                    877: }
                    878:
                    879: INLINE void ndl_copy(UINT *d1,UINT *d2)
                    880: {
                    881:     int i;
                    882:
                    883:     switch ( nd_wpd ) {
                    884:         case 2:
                    885:             TD(d2) = TD(d1);
                    886:             d2[1] = d1[1];
                    887:             break;
                    888:         case 3:
                    889:             TD(d2) = TD(d1);
                    890:             d2[1] = d1[1];
                    891:             d2[2] = d1[2];
                    892:             break;
                    893:         default:
                    894:             for ( i = 0; i < nd_wpd; i++ )
                    895:                 d2[i] = d1[i];
                    896:             break;
                    897:     }
                    898: }
                    899:
                    900: INLINE void ndl_zero(UINT *d)
                    901: {
                    902:     int i;
                    903:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                    904: }
                    905:
                    906: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
                    907: {
                    908:     int i;
                    909:
                    910:     if ( nd_module ) {
                    911:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
                    912:         error("ndl_add : invalid operation");
                    913:     }
                    914: #if 1
                    915:     switch ( nd_wpd ) {
                    916:         case 2:
                    917:             TD(d) = TD(d1)+TD(d2);
                    918:             d[1] = d1[1]+d2[1];
                    919:             break;
                    920:         case 3:
                    921:             TD(d) = TD(d1)+TD(d2);
                    922:             d[1] = d1[1]+d2[1];
                    923:             d[2] = d1[2]+d2[2];
                    924:             break;
                    925:         default:
                    926:             for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    927:             break;
                    928:     }
                    929: #else
                    930:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    931: #endif
                    932: }
                    933:
                    934: /* d1 += d2 */
                    935: INLINE void ndl_addto(UINT *d1,UINT *d2)
                    936: {
                    937:     int i;
                    938:
                    939:     if ( nd_module ) {
                    940:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
                    941:             error("ndl_addto : invalid operation");
                    942:     }
                    943: #if 1
                    944:     switch ( nd_wpd ) {
                    945:         case 2:
                    946:             TD(d1) += TD(d2);
                    947:             d1[1] += d2[1];
                    948:             break;
                    949:         case 3:
                    950:             TD(d1) += TD(d2);
                    951:             d1[1] += d2[1];
                    952:             d1[2] += d2[2];
                    953:             break;
                    954:         default:
                    955:             for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    956:             break;
                    957:     }
                    958: #else
                    959:     for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    960: #endif
                    961: }
                    962:
                    963: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
                    964: {
                    965:     int i;
                    966:
                    967:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
                    968: }
                    969:
                    970: int ndl_disjoint(UINT *d1,UINT *d2)
                    971: {
                    972:     UINT t1,t2,u,u1,u2;
                    973:     int i,j;
                    974:
                    975:     if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
                    976: #if USE_UNROLL
                    977:     switch ( nd_bpe ) {
                    978:         case 3:
                    979:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    980:                 u1 = d1[i]; u2 = d2[i];
                    981:                 t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
                    982:                 t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
                    983:                 t1 = u1&  0xe00000; t2 = u2&  0xe00000; if ( t1&&t2 ) return 0;
                    984:                 t1 = u1&  0x1c0000; t2 = u2&  0x1c0000; if ( t1&&t2 ) return 0;
                    985:                 t1 = u1&   0x38000; t2 = u2&   0x38000; if ( t1&&t2 ) return 0;
                    986:                 t1 = u1&    0x7000; t2 = u2&    0x7000; if ( t1&&t2 ) return 0;
                    987:                 t1 = u1&     0xe00; t2 = u2&     0xe00; if ( t1&&t2 ) return 0;
                    988:                 t1 = u1&     0x1c0; t2 = u2&     0x1c0; if ( t1&&t2 ) return 0;
                    989:                 t1 = u1&      0x38; t2 = u2&      0x38; if ( t1&&t2 ) return 0;
                    990:                 t1 = u1&       0x7; t2 = u2&       0x7; if ( t1&&t2 ) return 0;
                    991:             }
                    992:             return 1;
                    993:             break;
                    994:         case 4:
                    995:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    996:                 u1 = d1[i]; u2 = d2[i];
                    997:                 t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
                    998:                 t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
                    999:                 t1 = u1&  0xf00000; t2 = u2&  0xf00000; if ( t1&&t2 ) return 0;
                   1000:                 t1 = u1&   0xf0000; t2 = u2&   0xf0000; if ( t1&&t2 ) return 0;
                   1001:                 t1 = u1&    0xf000; t2 = u2&    0xf000; if ( t1&&t2 ) return 0;
                   1002:                 t1 = u1&     0xf00; t2 = u2&     0xf00; if ( t1&&t2 ) return 0;
                   1003:                 t1 = u1&      0xf0; t2 = u2&      0xf0; if ( t1&&t2 ) return 0;
                   1004:                 t1 = u1&       0xf; t2 = u2&       0xf; if ( t1&&t2 ) return 0;
                   1005:             }
                   1006:             return 1;
                   1007:             break;
                   1008:         case 6:
                   1009:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1010:                 u1 = d1[i]; u2 = d2[i];
                   1011:                 t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
                   1012:                 t1 = u1&  0xfc0000; t2 = u2&  0xfc0000; if ( t1&&t2 ) return 0;
                   1013:                 t1 = u1&   0x3f000; t2 = u2&   0x3f000; if ( t1&&t2 ) return 0;
                   1014:                 t1 = u1&     0xfc0; t2 = u2&     0xfc0; if ( t1&&t2 ) return 0;
                   1015:                 t1 = u1&      0x3f; t2 = u2&      0x3f; if ( t1&&t2 ) return 0;
                   1016:             }
                   1017:             return 1;
                   1018:             break;
                   1019:         case 8:
                   1020:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1021:                 u1 = d1[i]; u2 = d2[i];
                   1022:                 t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
                   1023:                 t1 = u1&  0xff0000; t2 = u2&  0xff0000; if ( t1&&t2 ) return 0;
                   1024:                 t1 = u1&    0xff00; t2 = u2&    0xff00; if ( t1&&t2 ) return 0;
                   1025:                 t1 = u1&      0xff; t2 = u2&      0xff; if ( t1&&t2 ) return 0;
                   1026:             }
                   1027:             return 1;
                   1028:             break;
                   1029:         case 16:
                   1030:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1031:                 u1 = d1[i]; u2 = d2[i];
                   1032:                 t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
                   1033:                 t1 = u1&    0xffff; t2 = u2&    0xffff; if ( t1&&t2 ) return 0;
                   1034:             }
                   1035:             return 1;
                   1036:             break;
                   1037:         case 32:
                   1038:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                   1039:                 if ( d1[i] && d2[i] ) return 0;
                   1040:             return 1;
                   1041:             break;
                   1042:         default:
                   1043:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1044:                 u1 = d1[i]; u2 = d2[i];
                   1045:                 for ( j = 0; j < nd_epw; j++ ) {
                   1046:                     if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                   1047:                     u1 >>= nd_bpe; u2 >>= nd_bpe;
                   1048:                 }
                   1049:             }
                   1050:             return 1;
                   1051:             break;
                   1052:     }
                   1053: #else
                   1054:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1055:         u1 = d1[i]; u2 = d2[i];
                   1056:         for ( j = 0; j < nd_epw; j++ ) {
                   1057:             if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                   1058:             u1 >>= nd_bpe; u2 >>= nd_bpe;
                   1059:         }
                   1060:     }
                   1061:     return 1;
                   1062: #endif
                   1063: }
                   1064:
                   1065: int ndl_check_bound(UINT *d1,UINT *d2)
                   1066: {
                   1067:     UINT u2;
                   1068:     int i,j,ind,k;
                   1069:
                   1070:     ind = 0;
                   1071: #if USE_UNROLL
                   1072:     switch ( nd_bpe ) {
                   1073:         case 3:
                   1074:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1075:                 u2 = d2[i];
                   1076:                 if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
                   1077:                 if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
                   1078:                 if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
                   1079:                 if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
                   1080:                 if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
                   1081:                 if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
                   1082:                 if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
                   1083:                 if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
                   1084:                 if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
                   1085:                 if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
                   1086:             }
                   1087:             return 0;
                   1088:             break;
                   1089:         case 4:
                   1090:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1091:                 u2 = d2[i];
                   1092:                 if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
                   1093:                 if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
                   1094:                 if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
                   1095:                 if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
                   1096:                 if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
                   1097:                 if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
                   1098:                 if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
                   1099:                 if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
                   1100:             }
                   1101:             return 0;
                   1102:             break;
                   1103:         case 6:
                   1104:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1105:                 u2 = d2[i];
                   1106:                 if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
                   1107:                 if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
                   1108:                 if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
                   1109:                 if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
                   1110:                 if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
                   1111:             }
                   1112:             return 0;
                   1113:             break;
                   1114:         case 8:
                   1115:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1116:                 u2 = d2[i];
                   1117:                 if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
                   1118:                 if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
                   1119:                 if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
                   1120:                 if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
                   1121:             }
                   1122:             return 0;
                   1123:             break;
                   1124:         case 16:
                   1125:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1126:                 u2 = d2[i];
                   1127:                 if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
                   1128:                 if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
                   1129:             }
                   1130:             return 0;
                   1131:             break;
                   1132:         case 32:
                   1133:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                   1134:                 if ( d1[i]+d2[i]<d1[i] ) return 1;
                   1135:             return 0;
                   1136:             break;
                   1137:         default:
                   1138:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1139:                 u2 = d2[i];
                   1140:                 k = (nd_epw-1)*nd_bpe;
                   1141:                 for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1142:                     if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1143:             }
                   1144:             return 0;
                   1145:             break;
                   1146:     }
                   1147: #else
                   1148:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1149:         u2 = d2[i];
                   1150:         k = (nd_epw-1)*nd_bpe;
                   1151:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1152:             if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1153:     }
                   1154:     return 0;
                   1155: #endif
                   1156: }
                   1157:
                   1158: int ndl_check_bound2(int index,UINT *d2)
                   1159: {
                   1160:     return ndl_check_bound(nd_bound[index],d2);
                   1161: }
                   1162:
                   1163: INLINE int ndl_hash_value(UINT *d)
                   1164: {
                   1165:     int i;
1.11      noro     1166:     UINT r;
1.1       noro     1167:
                   1168:     r = 0;
                   1169:     for ( i = 0; i < nd_wpd; i++ )
1.12      noro     1170:         r = (r*1511+d[i]);
1.11      noro     1171:     r %= REDTAB_LEN;
1.1       noro     1172:     return r;
                   1173: }
                   1174:
                   1175: INLINE int ndl_find_reducer(UINT *dg)
                   1176: {
                   1177:     RHist r;
                   1178:     int d,k,i;
                   1179:
                   1180:     d = ndl_hash_value(dg);
                   1181:     for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   1182:         if ( ndl_equal(dg,DL(r)) ) {
                   1183:             if ( k > 0 ) nd_notfirst++;
                   1184:             nd_found++;
                   1185:             return r->index;
                   1186:         }
                   1187:     }
                   1188:     if ( Reverse )
                   1189:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   1190:             r = nd_psh[i];
                   1191:             if ( ndl_reducible(dg,DL(r)) ) {
                   1192:                 nd_create++;
                   1193:                 nd_append_red(dg,i);
                   1194:                 return i;
                   1195:             }
                   1196:         }
                   1197:     else
                   1198:         for ( i = 0; i < nd_psn; i++ ) {
                   1199:             r = nd_psh[i];
                   1200:             if ( ndl_reducible(dg,DL(r)) ) {
                   1201:                 nd_create++;
                   1202:                 nd_append_red(dg,i);
                   1203:                 return i;
                   1204:             }
                   1205:         }
                   1206:     return -1;
                   1207: }
                   1208:
1.24      noro     1209: // ret=0,...,nd_psn-1 => reducer found
                   1210: // ret=nd_psn => reducer not found
                   1211: // ret=-1 => singular top reducible
                   1212:
                   1213: int comp_sig(SIG s1,SIG s2);
                   1214: void _ndltodl(UINT *ndl,DL dl);
                   1215:
                   1216: void print_sig(SIG s)
                   1217: {
                   1218:   int i;
                   1219:
                   1220:   fprintf(asir_out,"<<");
                   1221:   for ( i = 0; i < nd_nvar; i++ ) {
                   1222:     fprintf(asir_out,"%d",s->dl->d[i]);
                   1223:     if ( i != nd_nvar-1 ) fprintf(asir_out,",");
                   1224:   }
                   1225:   fprintf(asir_out,">>*e%d",s->pos);
                   1226: }
                   1227:
1.35      noro     1228: // assuming increasing order wrt signature
                   1229:
1.24      noro     1230: INLINE int ndl_find_reducer_s(UINT *dg,SIG sig)
                   1231: {
                   1232:   RHist r;
1.27      noro     1233:   int i,singular,ret,d,k;
1.26      noro     1234:   static int wpd,nvar;
1.24      noro     1235:   static SIG quo;
                   1236:   static UINT *tmp;
                   1237:
1.26      noro     1238:   if ( !quo || nvar != nd_nvar ) NEWSIG(quo);
                   1239:   if ( wpd != nd_wpd ) {
1.24      noro     1240:     wpd = nd_wpd;
                   1241:     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
                   1242:   }
1.27      noro     1243:   d = ndl_hash_value(dg);
1.35      noro     1244: #if 1
1.27      noro     1245:   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   1246:     if ( ndl_equal(dg,DL(r)) ) {
                   1247:       return r->index;
                   1248:     }
                   1249:   }
1.35      noro     1250: #endif
1.24      noro     1251:   singular = 0;
                   1252:   for ( i = 0; i < nd_psn; i++ ) {
                   1253:     r = nd_psh[i];
                   1254:     if ( ndl_reducible(dg,DL(r)) ) {
1.27      noro     1255:       ndl_sub(dg,DL(r),tmp);
1.24      noro     1256:       _ndltodl(tmp,DL(quo));
                   1257:       _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
                   1258:       quo->pos = nd_psh[i]->sig->pos;
                   1259:       ret = comp_sig(sig,quo);
                   1260:       if ( ret > 0 ) { singular = 0; break; }
1.38      noro     1261:       if ( ret == 0 ) { /* fprintf(asir_out,"s"); fflush(asir_out); */ singular = 1; }
1.24      noro     1262:     }
                   1263:   }
                   1264:   if ( singular ) return -1;
1.27      noro     1265:   else if ( i < nd_psn )
                   1266:     nd_append_red(dg,i);
                   1267:   return i;
1.24      noro     1268: }
                   1269:
1.1       noro     1270: ND nd_merge(ND p1,ND p2)
                   1271: {
                   1272:     int n,c;
                   1273:     int t,can,td1,td2;
                   1274:     ND r;
                   1275:     NM m1,m2,mr0,mr,s;
                   1276:
                   1277:     if ( !p1 ) return p2;
                   1278:     else if ( !p2 ) return p1;
                   1279:     else {
                   1280:         can = 0;
                   1281:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1282:             c = DL_COMPARE(DL(m1),DL(m2));
                   1283:             switch ( c ) {
                   1284:                 case 0:
                   1285:                     s = m1; m1 = NEXT(m1);
                   1286:                     can++; NEXTNM2(mr0,mr,s);
                   1287:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1288:                     break;
                   1289:                 case 1:
                   1290:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1291:                     break;
                   1292:                 case -1:
                   1293:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1294:                     break;
                   1295:             }
                   1296:         }
                   1297:         if ( !mr0 )
                   1298:             if ( m1 ) mr0 = m1;
                   1299:             else if ( m2 ) mr0 = m2;
                   1300:             else return 0;
                   1301:         else if ( m1 ) NEXT(mr) = m1;
                   1302:         else if ( m2 ) NEXT(mr) = m2;
                   1303:         else NEXT(mr) = 0;
                   1304:         BDY(p1) = mr0;
                   1305:         SG(p1) = MAX(SG(p1),SG(p2));
                   1306:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1307:         FREEND(p2);
                   1308:         return p1;
                   1309:     }
                   1310: }
                   1311:
                   1312: ND nd_add(int mod,ND p1,ND p2)
                   1313: {
                   1314:     int n,c;
                   1315:     int t,can,td1,td2;
                   1316:     ND r;
                   1317:     NM m1,m2,mr0,mr,s;
                   1318:
1.11      noro     1319:     Nnd_add++;
1.1       noro     1320:     if ( !p1 ) return p2;
                   1321:     else if ( !p2 ) return p1;
                   1322:     else if ( mod == -1 ) return nd_add_sf(p1,p2);
                   1323:     else if ( mod == -2 ) return nd_add_lf(p1,p2);
                   1324:     else if ( !mod ) return nd_add_q(p1,p2);
                   1325:     else {
                   1326:         can = 0;
                   1327:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1328:             c = DL_COMPARE(DL(m1),DL(m2));
                   1329:             switch ( c ) {
                   1330:                 case 0:
                   1331:                     t = ((CM(m1))+(CM(m2))) - mod;
                   1332:                     if ( t < 0 ) t += mod;
                   1333:                     s = m1; m1 = NEXT(m1);
                   1334:                     if ( t ) {
                   1335:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1336:                     } else {
                   1337:                         can += 2; FREENM(s);
                   1338:                     }
                   1339:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1340:                     break;
                   1341:                 case 1:
                   1342:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1343:                     break;
                   1344:                 case -1:
                   1345:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1346:                     break;
                   1347:             }
                   1348:         }
                   1349:         if ( !mr0 )
                   1350:             if ( m1 ) mr0 = m1;
                   1351:             else if ( m2 ) mr0 = m2;
                   1352:             else return 0;
                   1353:         else if ( m1 ) NEXT(mr) = m1;
                   1354:         else if ( m2 ) NEXT(mr) = m2;
                   1355:         else NEXT(mr) = 0;
                   1356:         BDY(p1) = mr0;
                   1357:         SG(p1) = MAX(SG(p1),SG(p2));
                   1358:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1359:         FREEND(p2);
                   1360:         return p1;
                   1361:     }
                   1362: }
                   1363:
                   1364: /* XXX on opteron, the inlined manipulation of destructive additon of
                   1365:  * two NM seems to make gcc optimizer get confused, so the part is
                   1366:  * done in a function.
                   1367:  */
                   1368:
                   1369: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
                   1370: {
                   1371:     NM s;
                   1372:     P t;
                   1373:     int can;
                   1374:
                   1375:     addp(nd_vc,CP(*m1),CP(*m2),&t);
                   1376:     s = *m1; *m1 = NEXT(*m1);
                   1377:     if ( t ) {
                   1378:         can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
                   1379:     } else {
                   1380:         can = 2; FREENM(s);
                   1381:     }
                   1382:     s = *m2; *m2 = NEXT(*m2); FREENM(s);
                   1383:     return can;
                   1384: }
                   1385:
                   1386: ND nd_add_q(ND p1,ND p2)
                   1387: {
                   1388:     int n,c,can;
                   1389:     ND r;
                   1390:     NM m1,m2,mr0,mr,s;
                   1391:     P t;
                   1392:
                   1393:     if ( !p1 ) return p2;
                   1394:     else if ( !p2 ) return p1;
                   1395:     else {
                   1396:         can = 0;
                   1397:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1398:             c = DL_COMPARE(DL(m1),DL(m2));
                   1399:             switch ( c ) {
                   1400:                 case 0:
                   1401: #if defined(__x86_64__)
                   1402:                     can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
                   1403: #else
                   1404:                     addp(nd_vc,CP(m1),CP(m2),&t);
                   1405:                     s = m1; m1 = NEXT(m1);
                   1406:                     if ( t ) {
                   1407:                         can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
                   1408:                     } else {
                   1409:                         can += 2; FREENM(s);
                   1410:                     }
                   1411:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1412: #endif
                   1413:                     break;
                   1414:                 case 1:
                   1415:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1416:                     break;
                   1417:                 case -1:
                   1418:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1419:                     break;
                   1420:             }
                   1421:         }
                   1422:         if ( !mr0 )
                   1423:             if ( m1 ) mr0 = m1;
                   1424:             else if ( m2 ) mr0 = m2;
                   1425:             else return 0;
                   1426:         else if ( m1 ) NEXT(mr) = m1;
                   1427:         else if ( m2 ) NEXT(mr) = m2;
                   1428:         else NEXT(mr) = 0;
                   1429:         BDY(p1) = mr0;
                   1430:         SG(p1) = MAX(SG(p1),SG(p2));
                   1431:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1432:         FREEND(p2);
                   1433:         return p1;
                   1434:     }
                   1435: }
                   1436:
                   1437: ND nd_add_sf(ND p1,ND p2)
                   1438: {
                   1439:     int n,c,can;
                   1440:     ND r;
                   1441:     NM m1,m2,mr0,mr,s;
                   1442:     int t;
                   1443:
                   1444:     if ( !p1 ) return p2;
                   1445:     else if ( !p2 ) return p1;
                   1446:     else {
                   1447:         can = 0;
                   1448:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1449:             c = DL_COMPARE(DL(m1),DL(m2));
                   1450:             switch ( c ) {
                   1451:                 case 0:
                   1452:                     t = _addsf(CM(m1),CM(m2));
                   1453:                     s = m1; m1 = NEXT(m1);
                   1454:                     if ( t ) {
                   1455:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1456:                     } else {
                   1457:                         can += 2; FREENM(s);
                   1458:                     }
                   1459:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   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:
                   1485: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
                   1486: {
                   1487:     int c,c1,c2;
                   1488:     Z cg,cred,gcd,tq;
                   1489:     P cgp,credp,gcdp;
                   1490:     Obj tr,tr1;
                   1491:
                   1492:     if ( mod == -1 ) {
                   1493:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1494:         *divp = (Obj)ONE;
                   1495:     } else if ( mod == -2 ) {
                   1496:         Z inv,t;
                   1497:         divlf(ONE,HCZ(p),&inv);
                   1498:         chsgnlf(HCZ(g),&t);
                   1499:         mullf(inv,t,&CZ(mul));
                   1500:         *divp = (Obj)ONE;
                   1501:     } else if ( mod ) {
                   1502:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1503:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1504:         *divp = (Obj)ONE;
                   1505:     } else if ( nd_vc ) {
                   1506:         ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
                   1507:         divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
                   1508:         chsgnp(cgp,&CP(mul));
                   1509:         nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
                   1510:         if ( dn ) {
                   1511:             mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
                   1512:             reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
                   1513:         }
                   1514:         *divp = (Obj)credp;
                   1515:     } else {
1.6       noro     1516:         igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1517:         chsgnz(cg,&CZ(mul));
1.1       noro     1518:         nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
                   1519:         if ( dn ) {
                   1520:             mulz(dn->z,cred,&tq); dn->z = tq;
                   1521:         }
                   1522:         *divp = (Obj)cred;
                   1523:     }
                   1524:     return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
                   1525: }
                   1526:
                   1527: /* ret=1 : success, ret=0 : overflow */
1.6       noro     1528: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1       noro     1529: {
                   1530:     NM m,mrd,tail;
                   1531:     NM mul;
                   1532:     int n,sugar,psugar,sugar0,stat,index;
                   1533:     int c,c1,c2,dummy;
                   1534:     RHist h;
                   1535:     NDV p,red;
                   1536:     Q cg,cred,gcd,tq,qq;
                   1537:     Z iq;
                   1538:     DP dmul;
                   1539:     NODE node;
                   1540:     LIST hist;
                   1541:     double hmag;
                   1542:     P tp,tp1;
                   1543:     Obj tr,tr1,div;
                   1544:     union oNDC hg;
                   1545:     P cont;
                   1546:
                   1547:     if ( !g ) {
                   1548:         *rp = d;
                   1549:         return 1;
                   1550:     }
                   1551:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1552:
                   1553:     sugar0 = sugar = SG(g);
                   1554:     n = NV(g);
                   1555:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1556:     if ( d )
                   1557:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1558:     for ( ; g; ) {
                   1559:         index = ndl_find_reducer(HDL(g));
                   1560:         if ( index >= 0 ) {
                   1561:             h = nd_psh[index];
                   1562:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1563:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1564:                 nd_free(g); nd_free(d);
                   1565:                 return 0;
                   1566:             }
                   1567:             p = nd_demand ? ndv_load(index) : ps[index];
                   1568:             /* d+g -> div*(d+g)+mul*p */
1.6       noro     1569:             g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1       noro     1570:             if ( nd_gentrace ) {
                   1571:                 /* Trace=[div,index,mul,ONE] */
1.6       noro     1572:                 STOZ(index,iq);
1.1       noro     1573:                 nmtodp(mod,mul,&dmul);
                   1574:                 node = mknode(4,div,iq,dmul,ONE);
                   1575:             }
                   1576:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
                   1577:             if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
                   1578:                 hg = HCU(g);
                   1579:                 nd_removecont2(d,g);
1.6       noro     1580:                 if ( nd_gentrace ) {
1.1       noro     1581:                     /* overwrite cont : Trace=[div,index,mul,cont] */
1.6       noro     1582:                     /* exact division */
1.1       noro     1583:                     cont = ndc_div(mod,hg,HCU(g));
                   1584:                     if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
                   1585:                 }
                   1586:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1587:             }
                   1588:             MKLIST(hist,node);
                   1589:             MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
                   1590:         } else if ( !full ) {
                   1591:             *rp = g;
                   1592:             return 1;
                   1593:         } else {
                   1594:             m = BDY(g);
                   1595:             if ( NEXT(m) ) {
                   1596:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1597:             } else {
                   1598:                 FREEND(g); g = 0;
                   1599:             }
                   1600:             if ( d ) {
                   1601:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1602:             } else {
                   1603:                 MKND(n,m,1,d); tail = BDY(d);
                   1604:             }
                   1605:         }
                   1606:     }
                   1607:     if ( d ) SG(d) = sugar;
                   1608:     *rp = d;
                   1609:     return 1;
                   1610: }
                   1611:
1.24      noro     1612: // ret=1 => success
                   1613: // ret=0 => overflow
                   1614: // ret=-1 => singular top reducible
                   1615:
                   1616: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
                   1617: {
                   1618:     NM m,mrd,tail;
                   1619:     NM mul;
                   1620:     int n,sugar,psugar,sugar0,stat,index;
                   1621:     int c,c1,c2,dummy;
                   1622:     RHist h;
                   1623:     NDV p,red;
                   1624:     Q cg,cred,gcd,tq,qq;
                   1625:     Z iq;
                   1626:     DP dmul;
                   1627:     NODE node;
                   1628:     LIST hist;
                   1629:     double hmag;
                   1630:     P tp,tp1;
                   1631:     Obj tr,tr1,div;
                   1632:     union oNDC hg;
                   1633:     P cont;
                   1634:     SIG sig;
                   1635:
                   1636:     if ( !g ) {
                   1637:         *rp = d;
                   1638:         return 1;
                   1639:     }
                   1640:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1641:
                   1642:     sugar0 = sugar = SG(g);
                   1643:     n = NV(g);
                   1644:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1645:     if ( d )
                   1646:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1647:     sig = g->sig;
                   1648:     for ( ; g; ) {
                   1649:         index = ndl_find_reducer_s(HDL(g),sig);
                   1650:         if ( index >= 0 && index < nd_psn ) {
                   1651:             // reducer found
                   1652:             h = nd_psh[index];
                   1653:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1654:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1655:                 nd_free(g); nd_free(d);
                   1656:                 return 0;
                   1657:             }
                   1658:             p = ps[index];
                   1659:             /* d+g -> div*(d+g)+mul*p */
                   1660:             g = nd_reduce2(mod,d,g,p,mul,0,&div);
                   1661:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
                   1662:             if ( !mod && g && ((double)(p_mag(HCP(g))) > hmag) ) {
                   1663:                 hg = HCU(g);
                   1664:                 nd_removecont2(d,g);
                   1665:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1666:             }
                   1667:         } else if ( index == -1 ) {
                   1668:           // singular top reducible
                   1669:           return -1;
                   1670:         } else if ( !full ) {
                   1671:             *rp = g;
                   1672:             g->sig = sig;
                   1673:             return 1;
                   1674:         } else {
                   1675:             m = BDY(g);
                   1676:             if ( NEXT(m) ) {
                   1677:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1678:             } else {
                   1679:                 FREEND(g); g = 0;
                   1680:             }
                   1681:             if ( d ) {
                   1682:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1683:             } else {
                   1684:                 MKND(n,m,1,d); tail = BDY(d);
                   1685:             }
                   1686:         }
                   1687:     }
                   1688:     if ( d ) {
                   1689:       SG(d) = sugar;
                   1690:       d->sig = sig;
                   1691:     }
                   1692:     *rp = d;
                   1693:     return 1;
                   1694: }
                   1695:
1.1       noro     1696: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
                   1697: {
                   1698:     int hindex,index;
                   1699:     NDV p;
                   1700:     ND u,d,red;
                   1701:     NODE l;
                   1702:     NM mul,m,mrd,tail;
                   1703:     int sugar,psugar,n,h_reducible;
                   1704:     PGeoBucket bucket;
                   1705:     int c,c1,c2;
                   1706:     Z cg,cred,gcd,zzz;
                   1707:     RHist h;
                   1708:     double hmag,gmag;
                   1709:     int count = 0;
                   1710:     int hcount = 0;
                   1711:
                   1712:     if ( !g ) {
                   1713:         *rp = 0;
                   1714:         return 1;
                   1715:     }
                   1716:     sugar = SG(g);
                   1717:     n = NV(g);
1.6       noro     1718:     if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1       noro     1719:     bucket = create_pbucket();
                   1720:     add_pbucket(mod,bucket,g);
                   1721:     d = 0;
                   1722:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1723:     while ( 1 ) {
                   1724:         if ( mod > 0 || mod == -1 )
                   1725:           hindex = head_pbucket(mod,bucket);
                   1726:         else if ( mod == -2 )
                   1727:           hindex = head_pbucket_lf(bucket);
                   1728:         else
                   1729:           hindex = head_pbucket_q(bucket);
                   1730:         if ( hindex < 0 ) {
                   1731:             if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1732:             if ( d ) SG(d) = sugar;
                   1733:             *rp = d;
                   1734:             return 1;
                   1735:         }
                   1736:         g = bucket->body[hindex];
                   1737:         index = ndl_find_reducer(HDL(g));
                   1738:         if ( index >= 0 ) {
                   1739:             count++;
                   1740:             if ( !d ) hcount++;
                   1741:             h = nd_psh[index];
                   1742:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1743:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1744:                 nd_free(d);
                   1745:                 free_pbucket(bucket);
                   1746:                 *rp = 0;
                   1747:                 return 0;
                   1748:             }
                   1749:             p = ps[index];
                   1750:             if ( mod == -1 )
                   1751:                 CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1752:             else if ( mod == -2 ) {
                   1753:                 Z inv,t;
                   1754:                 divlf(ONE,HCZ(p),&inv);
                   1755:                 chsgnlf(HCZ(g),&t);
                   1756:                 mullf(inv,t,&CZ(mul));
                   1757:             } else if ( mod ) {
                   1758:                 c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1759:                 DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1760:             } else {
1.6       noro     1761:                 igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1762:                 chsgnz(cg,&CZ(mul));
1.1       noro     1763:                 nd_mul_c_q(d,(P)cred);
                   1764:                 mulq_pbucket(bucket,cred);
                   1765:                 g = bucket->body[hindex];
1.6       noro     1766:                 gmag = (double)p_mag((P)HCZ(g));
1.1       noro     1767:             }
                   1768:             red = ndv_mul_nm(mod,mul,p);
                   1769:             bucket->body[hindex] = nd_remove_head(g);
                   1770:             red = nd_remove_head(red);
                   1771:             add_pbucket(mod,bucket,red);
                   1772:             psugar = SG(p)+TD(DL(mul));
                   1773:             sugar = MAX(sugar,psugar);
                   1774:             if ( !mod && hmag && (gmag > hmag) ) {
                   1775:                 g = normalize_pbucket(mod,bucket);
                   1776:                 if ( !g ) {
                   1777:                     if ( d ) SG(d) = sugar;
                   1778:                     *rp = d;
                   1779:                     return 1;
                   1780:                 }
                   1781:                 nd_removecont2(d,g);
1.6       noro     1782:                 hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1       noro     1783:                 add_pbucket(mod,bucket,g);
                   1784:             }
                   1785:         } else if ( !full ) {
                   1786:             g = normalize_pbucket(mod,bucket);
                   1787:             if ( g ) SG(g) = sugar;
                   1788:             *rp = g;
                   1789:             return 1;
                   1790:         } else {
                   1791:             m = BDY(g);
                   1792:             if ( NEXT(m) ) {
                   1793:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1794:             } else {
                   1795:                 FREEND(g); g = 0;
                   1796:             }
                   1797:             bucket->body[hindex] = g;
                   1798:             NEXT(m) = 0;
                   1799:             if ( d ) {
                   1800:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1801:             } else {
                   1802:                 MKND(n,m,1,d); tail = BDY(d);
                   1803:             }
                   1804:         }
                   1805:     }
                   1806: }
                   1807:
1.25      noro     1808: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *rp)
                   1809: {
                   1810:   int hindex,index;
                   1811:   NDV p;
                   1812:   ND u,d,red;
                   1813:   NODE l;
                   1814:   NM mul,m,mrd,tail;
                   1815:   int sugar,psugar,n,h_reducible;
                   1816:   PGeoBucket bucket;
                   1817:   int c,c1,c2;
                   1818:   Z cg,cred,gcd,zzz;
                   1819:   RHist h;
                   1820:   double hmag,gmag;
                   1821:   int count = 0;
                   1822:   int hcount = 0;
                   1823:   SIG sig;
                   1824:
                   1825:   if ( !g ) {
                   1826:     *rp = 0;
                   1827:     return 1;
                   1828:   }
                   1829:   sugar = SG(g);
                   1830:   n = NV(g);
                   1831:   if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
                   1832:   bucket = create_pbucket();
                   1833:   add_pbucket(mod,bucket,g);
                   1834:   d = 0;
                   1835:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1836:   sig = g->sig;
                   1837:   while ( 1 ) {
                   1838:     if ( mod > 0 || mod == -1 )
                   1839:       hindex = head_pbucket(mod,bucket);
                   1840:     else if ( mod == -2 )
                   1841:       hindex = head_pbucket_lf(bucket);
                   1842:     else
                   1843:       hindex = head_pbucket_q(bucket);
                   1844:     if ( hindex < 0 ) {
                   1845:       if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1846:       if ( d ) {
                   1847:         SG(d) = sugar;
                   1848:         d->sig = sig;
                   1849:       }
                   1850:       *rp = d;
                   1851:       return 1;
                   1852:     }
                   1853:     g = bucket->body[hindex];
                   1854:     index = ndl_find_reducer_s(HDL(g),sig);
                   1855:     if ( index >= 0 && index < nd_psn ) {
                   1856:       count++;
                   1857:       if ( !d ) hcount++;
                   1858:       h = nd_psh[index];
                   1859:       ndl_sub(HDL(g),DL(h),DL(mul));
                   1860:       if ( ndl_check_bound2(index,DL(mul)) ) {
                   1861:         nd_free(d);
                   1862:         free_pbucket(bucket);
                   1863:         *rp = 0;
                   1864:         return 0;
                   1865:       }
                   1866:       p = ps[index];
                   1867:       if ( mod == -1 )
                   1868:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1869:       else if ( mod == -2 ) {
                   1870:         Z inv,t;
                   1871:         divlf(ONE,HCZ(p),&inv);
                   1872:         chsgnlf(HCZ(g),&t);
                   1873:         mullf(inv,t,&CZ(mul));
                   1874:       } else if ( mod ) {
                   1875:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1876:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1877:       } else {
                   1878:         igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1879:         chsgnz(cg,&CZ(mul));
                   1880:         nd_mul_c_q(d,(P)cred);
                   1881:         mulq_pbucket(bucket,cred);
                   1882:         g = bucket->body[hindex];
                   1883:         gmag = (double)p_mag((P)HCZ(g));
                   1884:       }
                   1885:       red = ndv_mul_nm(mod,mul,p);
                   1886:       bucket->body[hindex] = nd_remove_head(g);
                   1887:       red = nd_remove_head(red);
                   1888:       add_pbucket(mod,bucket,red);
                   1889:       psugar = SG(p)+TD(DL(mul));
                   1890:       sugar = MAX(sugar,psugar);
                   1891:       if ( !mod && hmag && (gmag > hmag) ) {
                   1892:          g = normalize_pbucket(mod,bucket);
                   1893:          if ( !g ) {
                   1894:            if ( d ) {
                   1895:              SG(d) = sugar;
                   1896:              d->sig = sig;
                   1897:            }
                   1898:            *rp = d;
                   1899:            return 1;
                   1900:          }
                   1901:          nd_removecont2(d,g);
                   1902:          hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
                   1903:          add_pbucket(mod,bucket,g);
                   1904:       }
                   1905:     } else if ( index == -1 ) {
                   1906:       // singular top reducible
                   1907:       return -1;
                   1908:     } else if ( !full ) {
                   1909:       g = normalize_pbucket(mod,bucket);
                   1910:       if ( g ) {
                   1911:         SG(g) = sugar;
                   1912:         g->sig = sig;
                   1913:       }
                   1914:       *rp = g;
                   1915:       return 1;
                   1916:     } else {
                   1917:       m = BDY(g);
                   1918:       if ( NEXT(m) ) {
                   1919:         BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1920:       } else {
                   1921:         FREEND(g); g = 0;
                   1922:       }
                   1923:       bucket->body[hindex] = g;
                   1924:       NEXT(m) = 0;
                   1925:       if ( d ) {
                   1926:         NEXT(tail)=m; tail=m; LEN(d)++;
                   1927:       } else {
                   1928:         MKND(n,m,1,d); tail = BDY(d);
                   1929:       }
                   1930:     }
                   1931:   }
                   1932: }
                   1933:
1.1       noro     1934: /* input : list of NDV, cand : list of NDV */
                   1935:
                   1936: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
                   1937: {
                   1938:     int n,i,stat;
                   1939:     ND nf,d;
                   1940:     NDV r;
                   1941:     NODE t,s;
                   1942:     union oNDC dn;
                   1943:     Z q;
                   1944:     LIST list;
                   1945:
1.24      noro     1946:     ndv_setup(m,0,cand,nd_gentrace?1:0,1,0);
1.1       noro     1947:     n = length(cand);
                   1948:
                   1949:   if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
                   1950:     /* membercheck : list is a subset of Id(cand) ? */
                   1951:     for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
                   1952: again:
                   1953:     nd_tracelist = 0;
                   1954:         if ( nd_bpe > obpe )
                   1955:             r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
                   1956:         else
                   1957:             r = (NDV)BDY(t);
                   1958: #if 0
                   1959:         // moved to nd_f4_lf_trace()
                   1960:         if ( m == -2 ) ndv_mod(m,r);
                   1961: #endif
                   1962:         d = ndvtond(m,r);
1.6       noro     1963:         stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1       noro     1964:         if ( !stat ) {
                   1965:             nd_reconstruct(0,0);
                   1966:             goto again;
                   1967:         } else if ( nf ) return 0;
                   1968:     if ( nd_gentrace ) {
                   1969:       nd_tracelist = reverse_node(nd_tracelist);
                   1970:       MKLIST(list,nd_tracelist);
1.6       noro     1971:       STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1       noro     1972:       MKNODE(s,list,nd_alltracelist);
                   1973:       nd_alltracelist = s; nd_tracelist = 0;
                   1974:     }
                   1975:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   1976:     }
                   1977:     if ( DP_Print ) { printf("\n"); }
                   1978:     return 1;
                   1979: }
                   1980:
                   1981: ND nd_remove_head(ND p)
                   1982: {
                   1983:     NM m;
                   1984:
                   1985:     m = BDY(p);
                   1986:     if ( !NEXT(m) ) {
                   1987:         FREEND(p); p = 0;
                   1988:     } else {
                   1989:         BDY(p) = NEXT(m); LEN(p)--;
                   1990:     }
                   1991:     FREENM(m);
                   1992:     return p;
                   1993: }
                   1994:
                   1995: ND nd_separate_head(ND p,ND *head)
                   1996: {
                   1997:     NM m,m0;
                   1998:     ND r;
                   1999:
                   2000:     m = BDY(p);
                   2001:     if ( !NEXT(m) ) {
                   2002:         *head = p; p = 0;
                   2003:     } else {
                   2004:         m0 = m;
                   2005:         BDY(p) = NEXT(m); LEN(p)--;
                   2006:         NEXT(m0) = 0;
                   2007:         MKND(NV(p),m0,1,r);
                   2008:         *head = r;
                   2009:     }
                   2010:     return p;
                   2011: }
                   2012:
                   2013: PGeoBucket create_pbucket()
                   2014: {
                   2015:     PGeoBucket g;
                   2016:
                   2017:     g = CALLOC(1,sizeof(struct oPGeoBucket));
                   2018:     g->m = -1;
                   2019:     return g;
                   2020: }
                   2021:
                   2022: void free_pbucket(PGeoBucket b) {
                   2023:     int i;
                   2024:
                   2025:     for ( i = 0; i <= b->m; i++ )
                   2026:         if ( b->body[i] ) {
                   2027:             nd_free(b->body[i]);
                   2028:             b->body[i] = 0;
                   2029:         }
                   2030:     GCFREE(b);
                   2031: }
                   2032:
1.39    ! noro     2033: #if 0
1.1       noro     2034: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   2035: {
                   2036:     int l,i,k,m;
                   2037:
                   2038:     if ( !d )
                   2039:         return;
                   2040:     l = LEN(d);
                   2041:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2042:     /* 2^(k-1) < l <= 2^k (=m) */
                   2043:     d = nd_merge(g->body[k],d);
                   2044:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   2045:         g->body[k] = 0;
                   2046:         d = nd_merge(g->body[k+1],d);
                   2047:     }
                   2048:     g->body[k] = d;
                   2049:     g->m = MAX(g->m,k);
                   2050: }
1.39    ! noro     2051: #else
        !          2052: void add_pbucket_symbolic(PGeoBucket g,ND d)
        !          2053: {
        !          2054:   int l,i,k,m,m0;
        !          2055:
        !          2056:   if ( !d )
        !          2057:     return;
        !          2058:   m0 = g->m;
        !          2059:   while ( 1 ) {
        !          2060:     l = LEN(d);
        !          2061:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
        !          2062:     /* 2^(k-1) < l <= 2^k (=m) */
        !          2063:     if ( g->body[k] == 0 ) {
        !          2064:       g->body[k] = d;
        !          2065:       m0 = MAX(k,m0);
        !          2066:       break;
        !          2067:     } else {
        !          2068:       d = nd_merge(g->body[k],d);
        !          2069:       g->body[k] = 0;
        !          2070:     }
        !          2071:   }
        !          2072:   g->m = m0;
        !          2073: }
        !          2074: #endif
1.1       noro     2075:
1.39    ! noro     2076: #if 0
1.1       noro     2077: void add_pbucket(int mod,PGeoBucket g,ND d)
                   2078: {
                   2079:     int l,i,k,m;
                   2080:
                   2081:     if ( !d )
                   2082:         return;
                   2083:     l = LEN(d);
                   2084:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2085:     /* 2^(k-1) < l <= 2^k (=m) */
                   2086:     d = nd_add(mod,g->body[k],d);
                   2087:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   2088:         g->body[k] = 0;
                   2089:         d = nd_add(mod,g->body[k+1],d);
                   2090:     }
                   2091:     g->body[k] = d;
                   2092:     g->m = MAX(g->m,k);
                   2093: }
1.39    ! noro     2094: #else
        !          2095: void add_pbucket(int mod,PGeoBucket g,ND d)
        !          2096: {
        !          2097:   int l,i,k,m,m0;
        !          2098:
        !          2099:   m0 = g->m;
        !          2100:   while ( d != 0 ) {
        !          2101:     l = LEN(d);
        !          2102:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
        !          2103:     /* 2^(k-1) < l <= 2^k (=m) */
        !          2104:     if ( g->body[k] == 0 ) {
        !          2105:       g->body[k] = d;
        !          2106:       m0 = MAX(k,m0);
        !          2107:       break;
        !          2108:     } else {
        !          2109:       d = nd_add(mod,g->body[k],d);
        !          2110:       g->body[k] = 0;
        !          2111:     }
        !          2112:   }
        !          2113:   g->m = m0;
        !          2114: }
        !          2115: #endif
1.1       noro     2116:
                   2117: void mulq_pbucket(PGeoBucket g,Z c)
                   2118: {
                   2119:     int k;
                   2120:
                   2121:     for ( k = 0; k <= g->m; k++ )
                   2122:         nd_mul_c_q(g->body[k],(P)c);
                   2123: }
                   2124:
                   2125: NM remove_head_pbucket_symbolic(PGeoBucket g)
                   2126: {
                   2127:     int j,i,k,c;
                   2128:     NM head;
                   2129:
                   2130:     k = g->m;
                   2131:     j = -1;
                   2132:     for ( i = 0; i <= k; i++ ) {
                   2133:         if ( !g->body[i] ) continue;
                   2134:         if ( j < 0 ) j = i;
                   2135:         else {
                   2136:             c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
                   2137:             if ( c > 0 )
                   2138:                 j = i;
                   2139:             else if ( c == 0 )
                   2140:                 g->body[i] = nd_remove_head(g->body[i]);
                   2141:         }
                   2142:     }
                   2143:     if ( j < 0 ) return 0;
                   2144:     else {
                   2145:         head = BDY(g->body[j]);
                   2146:         if ( !NEXT(head) ) {
                   2147:             FREEND(g->body[j]);
                   2148:             g->body[j] = 0;
                   2149:         } else {
                   2150:             BDY(g->body[j]) = NEXT(head);
                   2151:             LEN(g->body[j])--;
                   2152:         }
                   2153:         return head;
                   2154:     }
                   2155: }
                   2156:
                   2157: int head_pbucket(int mod,PGeoBucket g)
                   2158: {
                   2159:     int j,i,c,k,nv,sum;
                   2160:     UINT *di,*dj;
                   2161:     ND gi,gj;
                   2162:
                   2163:     k = g->m;
                   2164:     while ( 1 ) {
                   2165:         j = -1;
                   2166:         for ( i = 0; i <= k; i++ ) {
                   2167:             if ( !(gi = g->body[i]) )
                   2168:                 continue;
                   2169:             if ( j < 0 ) {
                   2170:                 j = i;
                   2171:                 gj = g->body[j];
                   2172:                 dj = HDL(gj);
                   2173:                 sum = HCM(gj);
                   2174:             } else {
                   2175:                 c = DL_COMPARE(HDL(gi),dj);
                   2176:                 if ( c > 0 ) {
                   2177:                     if ( sum ) HCM(gj) = sum;
                   2178:                     else g->body[j] = nd_remove_head(gj);
                   2179:                     j = i;
                   2180:                     gj = g->body[j];
                   2181:                     dj = HDL(gj);
                   2182:                     sum = HCM(gj);
                   2183:                 } else if ( c == 0 ) {
                   2184:                     if ( mod == -1 )
                   2185:                         sum = _addsf(sum,HCM(gi));
                   2186:                     else {
                   2187:                         sum = sum+HCM(gi)-mod;
                   2188:                         if ( sum < 0 ) sum += mod;
                   2189:                     }
                   2190:                     g->body[i] = nd_remove_head(gi);
                   2191:                 }
                   2192:             }
                   2193:         }
                   2194:         if ( j < 0 ) return -1;
                   2195:         else if ( sum ) {
                   2196:             HCM(gj) = sum;
                   2197:             return j;
                   2198:         } else
                   2199:             g->body[j] = nd_remove_head(gj);
                   2200:     }
                   2201: }
                   2202:
                   2203: int head_pbucket_q(PGeoBucket g)
                   2204: {
                   2205:     int j,i,c,k,nv;
                   2206:     Z sum,t;
                   2207:     ND gi,gj;
                   2208:
                   2209:     k = g->m;
                   2210:     while ( 1 ) {
                   2211:         j = -1;
                   2212:         for ( i = 0; i <= k; i++ ) {
                   2213:             if ( !(gi = g->body[i]) ) continue;
                   2214:             if ( j < 0 ) {
                   2215:                 j = i;
                   2216:                 gj = g->body[j];
1.6       noro     2217:                 sum = HCZ(gj);
1.1       noro     2218:             } else {
                   2219:                 nv = NV(gi);
                   2220:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   2221:                 if ( c > 0 ) {
1.6       noro     2222:                     if ( sum ) HCZ(gj) = sum;
1.1       noro     2223:                     else g->body[j] = nd_remove_head(gj);
                   2224:                     j = i;
                   2225:                     gj = g->body[j];
1.6       noro     2226:                     sum = HCZ(gj);
1.1       noro     2227:                 } else if ( c == 0 ) {
1.6       noro     2228:                     addz(sum,HCZ(gi),&t);
1.1       noro     2229:                     sum = t;
                   2230:                     g->body[i] = nd_remove_head(gi);
                   2231:                 }
                   2232:             }
                   2233:         }
                   2234:         if ( j < 0 ) return -1;
                   2235:         else if ( sum ) {
1.6       noro     2236:             HCZ(gj) = sum;
1.1       noro     2237:             return j;
                   2238:         } else
                   2239:             g->body[j] = nd_remove_head(gj);
                   2240:     }
                   2241: }
                   2242:
                   2243: int head_pbucket_lf(PGeoBucket g)
                   2244: {
                   2245:     int j,i,c,k,nv;
                   2246:     Z sum,t;
                   2247:     ND gi,gj;
                   2248:
                   2249:     k = g->m;
                   2250:     while ( 1 ) {
                   2251:         j = -1;
                   2252:         for ( i = 0; i <= k; i++ ) {
                   2253:             if ( !(gi = g->body[i]) ) continue;
                   2254:             if ( j < 0 ) {
                   2255:                 j = i;
                   2256:                 gj = g->body[j];
                   2257:                 sum = HCZ(gj);
                   2258:             } else {
                   2259:                 nv = NV(gi);
                   2260:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   2261:                 if ( c > 0 ) {
                   2262:                     if ( sum ) HCZ(gj) = sum;
                   2263:                     else g->body[j] = nd_remove_head(gj);
                   2264:                     j = i;
                   2265:                     gj = g->body[j];
                   2266:                     sum = HCZ(gj);
                   2267:                 } else if ( c == 0 ) {
                   2268:                     addlf(sum,HCZ(gi),&t);
                   2269:                     sum = t;
                   2270:                     g->body[i] = nd_remove_head(gi);
                   2271:                 }
                   2272:             }
                   2273:         }
                   2274:         if ( j < 0 ) return -1;
                   2275:         else if ( sum ) {
                   2276:             HCZ(gj) = sum;
                   2277:             return j;
                   2278:         } else
                   2279:             g->body[j] = nd_remove_head(gj);
                   2280:     }
                   2281: }
                   2282:
                   2283: ND normalize_pbucket(int mod,PGeoBucket g)
                   2284: {
                   2285:     int i;
                   2286:     ND r,t;
                   2287:
                   2288:     r = 0;
                   2289:     for ( i = 0; i <= g->m; i++ ) {
                   2290:         r = nd_add(mod,r,g->body[i]);
                   2291:         g->body[i] = 0;
                   2292:     }
                   2293:     g->m = -1;
                   2294:     return r;
                   2295: }
                   2296:
                   2297: #if 0
                   2298: void register_hcf(NDV p)
                   2299: {
                   2300:     DCP dc,t;
                   2301:     P hc,h;
                   2302:     int c;
                   2303:     NODE l,l1,prev;
                   2304:
                   2305:     hc = p->body->c.p;
                   2306:     if ( !nd_vc || NUM(hc) ) return;
                   2307:     fctrp(nd_vc,hc,&dc);
                   2308:     for ( t = dc; t; t = NEXT(t) ) {
                   2309:         h = t->c;
                   2310:         if ( NUM(h) ) continue;
                   2311:         for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   2312:             c = compp(nd_vc,h,(P)BDY(l));
                   2313:             if ( c >= 0 ) break;
                   2314:         }
                   2315:         if ( !l || c > 0  ) {
                   2316:             MKNODE(l1,h,l);
                   2317:             if ( !prev )
                   2318:                 nd_hcf = l1;
                   2319:             else
                   2320:                 NEXT(prev) = l1;
                   2321:         }
                   2322:     }
                   2323: }
                   2324: #else
                   2325: void register_hcf(NDV p)
                   2326: {
                   2327:     DCP dc,t;
                   2328:     P hc,h,q;
                   2329:     Q dmy;
                   2330:     int c;
                   2331:     NODE l,l1,prev;
                   2332:
                   2333:     hc = p->body->c.p;
                   2334:     if ( NUM(hc) ) return;
                   2335:     ptozp(hc,1,&dmy,&h);
                   2336: #if 1
                   2337:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   2338:         while ( 1 ) {
                   2339:             if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
                   2340:             else break;
                   2341:         }
                   2342:     }
                   2343:     if ( NUM(h) ) return;
                   2344: #endif
                   2345:     for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   2346:         c = compp(nd_vc,h,(P)BDY(l));
                   2347:         if ( c >= 0 ) break;
                   2348:     }
                   2349:     if ( !l || c > 0  ) {
                   2350:         MKNODE(l1,h,l);
                   2351:         if ( !prev )
                   2352:             nd_hcf = l1;
                   2353:         else
                   2354:             NEXT(prev) = l1;
                   2355:     }
                   2356: }
                   2357: #endif
                   2358:
                   2359: int do_diagonalize(int sugar,int m)
                   2360: {
1.6       noro     2361:   int i,nh,stat;
                   2362:   NODE r,g,t;
                   2363:   ND h,nf,s,head;
                   2364:   NDV nfv;
                   2365:   Q q;
                   2366:   P nm,nmp,dn,mnp,dnp,cont,cont1;
                   2367:   union oNDC hc;
                   2368:   NODE node;
                   2369:   LIST l;
                   2370:   Z iq;
1.1       noro     2371:
1.6       noro     2372:   for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   2373:     if ( nd_gentrace ) {
                   2374:       /* Trace = [1,index,1,1] */
                   2375:       STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2376:       MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2377:     }
                   2378:     if ( nd_demand )
                   2379:       nfv = ndv_load(i);
                   2380:     else
                   2381:       nfv = nd_ps[i];
                   2382:     s = ndvtond(m,nfv);
                   2383:     s = nd_separate_head(s,&head);
                   2384:     stat = nd_nf(m,head,s,nd_ps,1,&nf);
                   2385:     if ( !stat ) return 0;
                   2386:     ndv_free(nfv);
                   2387:     hc = HCU(nf); nd_removecont(m,nf);
                   2388:     /* exact division */
                   2389:     cont = ndc_div(m,hc,HCU(nf));
1.1       noro     2390:     if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6       noro     2391:     nfv = ndtondv(m,nf);
                   2392:     nd_free(nf);
                   2393:     nd_bound[i] = ndv_compute_bound(nfv);
                   2394:     if ( !m ) register_hcf(nfv);
                   2395:     if ( nd_demand ) {
                   2396:       ndv_save(nfv,i);
                   2397:       ndv_free(nfv);
                   2398:     } else
                   2399:       nd_ps[i] = nfv;
                   2400:   }
                   2401:   return 1;
1.1       noro     2402: }
                   2403:
                   2404: LIST compute_splist()
                   2405: {
                   2406:   NODE g,tn0,tn,node;
                   2407:   LIST l0;
                   2408:   ND_pairs d,t;
                   2409:   int i;
                   2410:   Z i1,i2;
                   2411:
                   2412:     g = 0; d = 0;
                   2413:     for ( i = 0; i < nd_psn; i++ ) {
                   2414:         d = update_pairs(d,g,i,0);
                   2415:         g = update_base(g,i);
                   2416:     }
                   2417:   for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
                   2418:     NEXTNODE(tn0,tn);
1.6       noro     2419:         STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1       noro     2420:         node = mknode(2,i1,i2); MKLIST(l0,node);
                   2421:     BDY(tn) = l0;
                   2422:   }
                   2423:   if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
                   2424:   return l0;
                   2425: }
                   2426:
                   2427: /* return value = 0 => input is not a GB */
                   2428:
                   2429: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
                   2430: {
1.6       noro     2431:   int i,nh,sugar,stat;
                   2432:   NODE r,g,t;
                   2433:   ND_pairs d;
                   2434:   ND_pairs l;
                   2435:   ND h,nf,s,head,nf1;
                   2436:   NDV nfv;
                   2437:   Z q;
                   2438:   union oNDC dn,hc;
                   2439:   int diag_count = 0;
                   2440:   P cont;
                   2441:   LIST list;
1.27      noro     2442: struct oEGT eg1,eg2,eg_update;
1.6       noro     2443:
1.27      noro     2444: init_eg(&eg_update);
1.11      noro     2445:   Nnd_add = 0;
1.6       noro     2446:   g = 0; d = 0;
                   2447:   for ( i = 0; i < nd_psn; i++ ) {
                   2448:     d = update_pairs(d,g,i,gensyz);
                   2449:     g = update_base(g,i);
                   2450:   }
                   2451:   sugar = 0;
                   2452:   while ( d ) {
1.1       noro     2453: again:
1.6       noro     2454:     l = nd_minp(d,&d);
                   2455:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   2456:     if ( SG(l) != sugar ) {
                   2457:       if ( ishomo ) {
                   2458:         diag_count = 0;
                   2459:         stat = do_diagonalize(sugar,m);
1.1       noro     2460:         if ( !stat ) {
1.6       noro     2461:           NEXT(l) = d; d = l;
                   2462:           d = nd_reconstruct(0,d);
                   2463:           goto again;
1.1       noro     2464:         }
1.6       noro     2465:       }
                   2466:       sugar = SG(l);
                   2467:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2468:     }
                   2469:     stat = nd_sp(m,0,l,&h);
                   2470:     if ( !stat ) {
                   2471:       NEXT(l) = d; d = l;
                   2472:       d = nd_reconstruct(0,d);
                   2473:       goto again;
                   2474:     }
1.1       noro     2475: #if USE_GEOBUCKET
1.39    ! noro     2476:     stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf)
        !          2477:       :nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     2478: #else
1.39    ! noro     2479:     stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     2480: #endif
1.6       noro     2481:     if ( !stat ) {
                   2482:       NEXT(l) = d; d = l;
                   2483:       d = nd_reconstruct(0,d);
                   2484:       goto again;
                   2485:     } else if ( nf ) {
                   2486:       if ( checkonly || gensyz ) return 0;
1.1       noro     2487:       if ( nd_newelim ) {
                   2488:         if ( nd_module ) {
                   2489:           if ( MPOS(HDL(nf)) > 1 ) return 0;
                   2490:         } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
                   2491:       }
1.6       noro     2492:       if ( DP_Print ) { printf("+"); fflush(stdout); }
                   2493:       hc = HCU(nf);
                   2494:       nd_removecont(m,nf);
                   2495:       if ( !m && nd_nalg ) {
                   2496:         nd_monic(0,&nf);
                   2497:         nd_removecont(m,nf);
                   2498:       }
                   2499:       if ( nd_gentrace ) {
                   2500:         /* exact division */
1.1       noro     2501:         cont = ndc_div(m,hc,HCU(nf));
                   2502:         if ( m || !UNIQ(cont) ) {
1.6       noro     2503:           t = mknode(4,NULLP,NULLP,NULLP,cont);
                   2504:           MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1       noro     2505:           nd_tracelist = t;
                   2506:         }
                   2507:       }
1.6       noro     2508:       nfv = ndtondv(m,nf); nd_free(nf);
1.24      noro     2509:       nh = ndv_newps(m,nfv,0);
1.6       noro     2510:       if ( !m && (ishomo && ++diag_count == diag_period) ) {
                   2511:         diag_count = 0;
                   2512:         stat = do_diagonalize(sugar,m);
                   2513:         if ( !stat ) {
                   2514:           NEXT(l) = d; d = l;
                   2515:           d = nd_reconstruct(1,d);
                   2516:           goto again;
1.1       noro     2517:         }
1.6       noro     2518:       }
1.27      noro     2519: get_eg(&eg1);
1.6       noro     2520:       d = update_pairs(d,g,nh,0);
1.27      noro     2521: get_eg(&eg2); add_eg(&eg_update,&eg1,&eg2);
1.6       noro     2522:       g = update_base(g,nh);
                   2523:       FREENDP(l);
                   2524:    } else {
                   2525:      if ( nd_gentrace && gensyz ) {
                   2526:        nd_tracelist = reverse_node(nd_tracelist);
                   2527:        MKLIST(list,nd_tracelist);
                   2528:        STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
                   2529:        MKNODE(t,list,nd_alltracelist);
                   2530:        nd_alltracelist = t; nd_tracelist = 0;
                   2531:      }
                   2532:      if ( DP_Print ) { printf("."); fflush(stdout); }
                   2533:        FREENDP(l);
1.36      noro     2534:      }
                   2535:   }
                   2536:   conv_ilist(nd_demand,0,g,indp);
                   2537:   if ( !checkonly && DP_Print ) { printf("nd_gb done. Number of nd_add=%d\n",Nnd_add); fflush(stdout); }
                   2538:
                   2539:   if ( DP_Print )
                   2540:     print_eg("update",&eg_update);
                   2541:   return g;
1.1       noro     2542: }
                   2543:
1.30      noro     2544: ND_pairs update_pairs_s(ND_pairs d,int t,NODE *syz);
                   2545: ND_pairs nd_newpairs_s(int t ,NODE *syz);
1.24      noro     2546:
                   2547: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *nf);
                   2548: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
                   2549:
                   2550: void _copydl(int n,DL d1,DL d2);
                   2551: void _subfromdl(int n,DL d1,DL d2);
                   2552: extern int (*cmpdl)(int n,DL d1,DL d2);
                   2553:
                   2554: NODE insert_sig(NODE l,SIG s)
                   2555: {
                   2556:   int pos;
                   2557:   DL sig;
                   2558:   struct oNODE root;
                   2559:   NODE p,prev,r;
                   2560:   SIG t;
                   2561:
                   2562:   pos = s->pos; sig = DL(s);
                   2563:   root.next = l; prev = &root;
                   2564:   for ( p = l; p; p = p->next ) {
                   2565:     t = (SIG)p->body;
                   2566:     if ( t->pos == pos ) {
                   2567:       if ( _dl_redble(DL(t),sig,nd_nvar) )
                   2568:         return root.next;
                   2569:       else if ( _dl_redble(sig,DL(t),nd_nvar) )
                   2570:         // remove p
                   2571:         prev->next = p->next;
                   2572:     } else
                   2573:       prev = p;
                   2574:   }
1.27      noro     2575:   NEWNODE(r); r->body = (pointer)s; r->next = 0;
                   2576:   for ( p = &root; p->next; p = p->next );
                   2577:   p->next = r;
                   2578: //  r->next = root.next;
                   2579: //  return r;
                   2580:   return root.next;
1.24      noro     2581: }
                   2582:
                   2583: ND_pairs remove_spair_s(ND_pairs d,SIG sig)
                   2584: {
                   2585:   struct oND_pairs root;
                   2586:   ND_pairs prev,p;
                   2587:   SIG spsig;
                   2588:
                   2589:   root.next = d;
                   2590:   prev = &root; p = d;
                   2591:   while ( p ) {
                   2592:     spsig = p->sig;
1.26      noro     2593:     if ( sig->pos == spsig->pos && _dl_redble(DL(sig),DL(spsig),nd_nvar) ) {
1.24      noro     2594:       // remove p
                   2595:       prev->next = p->next;
1.26      noro     2596:       Nsyz++;
                   2597:     } else
1.24      noro     2598:       prev = p;
                   2599:     p = p->next;
                   2600:   }
                   2601:   return (ND_pairs)root.next;
                   2602: }
                   2603:
1.29      noro     2604: int _dl_redble_ext(DL,DL,DL,int);
                   2605:
1.28      noro     2606: int small_lcm(ND_pairs l)
                   2607: {
                   2608:   SIG sig;
                   2609:   int i;
1.29      noro     2610:   NODE t;
1.28      noro     2611:   static DL lcm,mul,quo;
                   2612:   static int nvar;
                   2613:
1.38      noro     2614:   if ( nd_sba_largelcm ) return 0;
1.28      noro     2615:   if ( nvar < nd_nvar ) {
                   2616:     nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(quo,nvar); NEWDL(mul,nvar);
                   2617:   }
                   2618:   sig = l->sig;
                   2619:   _ndltodl(l->lcm,lcm);
1.29      noro     2620: #if 0
1.28      noro     2621:   for ( i = 0; i < nd_psn; i++ ) {
                   2622:     if ( sig->pos == nd_psh[i]->sig->pos &&
1.29      noro     2623:       _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
1.28      noro     2624:       _ndltodl(DL(nd_psh[i]),mul);
                   2625:       _addtodl(nd_nvar,quo,mul);
                   2626:       if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
                   2627:         break;
                   2628:     }
                   2629:   }
                   2630:   if ( i < nd_psn ) return 1;
                   2631:   else return 0;
1.29      noro     2632: #else
                   2633:   for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
                   2634:     i = (long)BDY(t);
                   2635:     if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
                   2636:       _ndltodl(DL(nd_psh[i]),mul);
                   2637:       _addtodl(nd_nvar,quo,mul);
                   2638:       if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
                   2639:         break;
                   2640:     }
                   2641:   }
                   2642:   if ( t ) return 1;
                   2643:   else return 0;
                   2644: #endif
1.28      noro     2645: }
                   2646:
                   2647: ND_pairs remove_large_lcm(ND_pairs d)
                   2648: {
                   2649:   struct oND_pairs root;
                   2650:   ND_pairs prev,p;
                   2651:
                   2652:   root.next = d;
                   2653:   prev = &root; p = d;
                   2654:   while ( p ) {
                   2655:     if ( small_lcm(p) ) {
                   2656:       // remove p
                   2657:       prev->next = p->next;
                   2658:     } else
                   2659:       prev = p;
                   2660:     p = p->next;
                   2661:   }
                   2662:   return (ND_pairs)root.next;
                   2663: }
                   2664:
1.26      noro     2665: struct oEGT eg_create,eg_newpairs,eg_merge;
                   2666:
1.30      noro     2667: NODE conv_ilist_s(int demand,int trace,int **indp);
                   2668:
1.24      noro     2669: NODE nd_sba_buch(int m,int ishomo,int **indp)
                   2670: {
1.39    ! noro     2671:   int i,j,nh,sugar,stat,pos;
1.30      noro     2672:   NODE r,t,g;
1.24      noro     2673:   ND_pairs d;
                   2674:   ND_pairs l;
                   2675:   ND h,nf,s,head,nf1;
                   2676:   NDV nfv;
                   2677:   Z q;
                   2678:   union oNDC dn,hc;
                   2679:   P cont;
                   2680:   LIST list;
                   2681:   SIG sig;
1.29      noro     2682:   NODE *syzlist;
1.26      noro     2683:   int Nredundant;
1.28      noro     2684:   DL lcm,quo,mul;
1.31      noro     2685:   struct oEGT eg1,eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.24      noro     2686:
1.27      noro     2687: init_eg(&eg_remove);
1.29      noro     2688:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.26      noro     2689:   Nsyz = 0;
1.24      noro     2690:   Nnd_add = 0;
1.26      noro     2691:   Nredundant = 0;
1.30      noro     2692:   d = 0;
1.24      noro     2693:   for ( i = 0; i < nd_psn; i++ ) {
1.30      noro     2694:     d = update_pairs_s(d,i,syzlist);
1.24      noro     2695:   }
1.29      noro     2696:   for ( i = 0; i < nd_psn; i++ )
                   2697:     for ( j = i+1; j < nd_psn; j++ ) {
                   2698:       NEWSIG(sig); sig->pos = j;
                   2699:       _copydl(nd_nvar,nd_sba_hm[i],sig->dl);
                   2700:       syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
                   2701:     }
1.24      noro     2702:   sugar = 0;
1.39    ! noro     2703:   pos = 0;
1.26      noro     2704:   NEWDL(lcm,nd_nvar); NEWDL(quo,nd_nvar); NEWDL(mul,nd_nvar);
                   2705: init_eg(&eg_create);
                   2706: init_eg(&eg_merge);
1.29      noro     2707: init_eg(&eg_large);
1.31      noro     2708: init_eg(&eg_nf);
                   2709: init_eg(&eg_nfzero);
1.24      noro     2710:   while ( d ) {
                   2711: again:
1.26      noro     2712:     if ( DP_Print ) {
                   2713:       int len;
                   2714:       ND_pairs td;
1.28      noro     2715:       for ( td = d, len=0; td; td = td->next, len++)
                   2716:         ;
1.26      noro     2717:        if ( !(len%100) ) fprintf(asir_out,"(%d)",len);
                   2718:       }
1.24      noro     2719:     l = d; d = d->next;
1.28      noro     2720:     if ( small_lcm(l) ) {
1.24      noro     2721:       if ( DP_Print ) fprintf(asir_out,"M");
1.26      noro     2722:       Nredundant++;
1.24      noro     2723:       continue;
                   2724:     }
                   2725:     if ( SG(l) != sugar ) {
                   2726:       sugar = SG(l);
                   2727:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2728:     }
1.28      noro     2729:     sig = l->sig;
1.39    ! noro     2730:     if ( DP_Print && nd_sba_pot ) {
        !          2731:       if ( sig->pos != pos ) {
        !          2732:         fprintf(asir_out,"[%d]",sig->pos);
        !          2733:         pos = sig->pos;
        !          2734:       }
        !          2735:     }
1.24      noro     2736:     stat = nd_sp(m,0,l,&h);
                   2737:     if ( !stat ) {
                   2738:       NEXT(l) = d; d = l;
                   2739:       d = nd_reconstruct(0,d);
                   2740:       goto again;
                   2741:     }
1.31      noro     2742: get_eg(&eg1);
1.25      noro     2743: #if USE_GEOBUCKET
1.39    ! noro     2744:     stat = m?nd_nf_pbucket_s(m,h,nd_ps,!nd_top&&!Top,&nf):nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.24      noro     2745: #else
1.39    ! noro     2746:     stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.24      noro     2747: #endif
1.31      noro     2748: get_eg(&eg2);
1.24      noro     2749:     if ( !stat ) {
                   2750:       NEXT(l) = d; d = l;
                   2751:       d = nd_reconstruct(0,d);
                   2752:       goto again;
                   2753:     } else if ( stat == -1 ) {
                   2754:       if ( DP_Print ) { printf("S"); fflush(stdout); }
1.27      noro     2755:       FREENDP(l);
1.24      noro     2756:     } else if ( nf ) {
                   2757:       if ( DP_Print ) { printf("+"); fflush(stdout); }
1.31      noro     2758:       add_eg(&eg_nf,&eg1,&eg2);
1.24      noro     2759:       hc = HCU(nf);
                   2760:       nd_removecont(m,nf);
                   2761:       nfv = ndtondv(m,nf); nd_free(nf);
                   2762:       nh = ndv_newps(m,nfv,0);
1.26      noro     2763:
1.30      noro     2764:       d = update_pairs_s(d,nh,syzlist);
1.29      noro     2765:       nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.24      noro     2766:       FREENDP(l);
                   2767:    } else {
1.31      noro     2768:       add_eg(&eg_nfzero,&eg1,&eg2);
1.24      noro     2769:      // syzygy
1.27      noro     2770: get_eg(&eg1);
1.24      noro     2771:      d = remove_spair_s(d,sig);
1.27      noro     2772: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29      noro     2773:      syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.24      noro     2774:      if ( DP_Print ) { printf("."); fflush(stdout); }
                   2775:      FREENDP(l);
                   2776:    }
                   2777:  }
1.30      noro     2778:  g = conv_ilist_s(nd_demand,0,indp);
1.26      noro     2779:  if ( DP_Print ) {
1.35      noro     2780:    printf("\nnd_sba done. nd_add=%d,Nsyz=%d,Nsamesig=%d,Nredundant=%d\n",Nnd_add,Nsyz,Nsamesig,Nredundant);
1.26      noro     2781:    fflush(stdout);
                   2782:    print_eg("create",&eg_create);
                   2783:    print_eg("merge",&eg_merge);
1.27      noro     2784:    print_eg("remove",&eg_remove);
1.31      noro     2785:    print_eg("nf",&eg_nf);
                   2786:    print_eg("nfzero",&eg_nfzero);
1.27      noro     2787:    printf("\n");
1.26      noro     2788:  }
1.24      noro     2789:  return g;
                   2790: }
                   2791:
1.1       noro     2792: /* splist = [[i1,i2],...] */
                   2793:
                   2794: int check_splist(int m,NODE splist)
                   2795: {
                   2796:   NODE t,p;
                   2797:   ND_pairs d,r,l;
                   2798:   int stat;
                   2799:   ND h,nf;
                   2800:
                   2801:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   2802:     p = BDY((LIST)BDY(t));
1.6       noro     2803:     NEXTND_pairs(d,r);
                   2804:     r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
                   2805:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     2806:     SG(r) = TD(LCM(r)); /* XXX */
                   2807:   }
                   2808:   if ( d ) NEXT(r) = 0;
                   2809:
1.6       noro     2810:   while ( d ) {
1.1       noro     2811: again:
1.6       noro     2812:     l = nd_minp(d,&d);
                   2813:     stat = nd_sp(m,0,l,&h);
                   2814:     if ( !stat ) {
                   2815:       NEXT(l) = d; d = l;
                   2816:       d = nd_reconstruct(0,d);
                   2817:       goto again;
                   2818:     }
1.39    ! noro     2819:     stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.6       noro     2820:     if ( !stat ) {
                   2821:       NEXT(l) = d; d = l;
                   2822:       d = nd_reconstruct(0,d);
                   2823:       goto again;
                   2824:     } else if ( nf ) return 0;
1.1       noro     2825:     if ( DP_Print) { printf("."); fflush(stdout); }
1.6       noro     2826:   }
1.1       noro     2827:   if ( DP_Print) { printf("done.\n"); fflush(stdout); }
                   2828:   return 1;
                   2829: }
                   2830:
                   2831: int check_splist_f4(int m,NODE splist)
                   2832: {
                   2833:   UINT *s0vect;
1.6       noro     2834:   PGeoBucket bucket;
1.1       noro     2835:   NODE p,rp0,t;
                   2836:   ND_pairs d,r,l,ll;
                   2837:   int col,stat;
                   2838:
                   2839:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   2840:     p = BDY((LIST)BDY(t));
1.6       noro     2841:     NEXTND_pairs(d,r);
                   2842:     r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
                   2843:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     2844:     SG(r) = TD(LCM(r)); /* XXX */
                   2845:   }
                   2846:   if ( d ) NEXT(r) = 0;
                   2847:
1.6       noro     2848:   while ( d ) {
                   2849:     l = nd_minsugarp(d,&d);
                   2850:     bucket = create_pbucket();
                   2851:     stat = nd_sp_f4(m,0,l,bucket);
                   2852:     if ( !stat ) {
                   2853:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   2854:       NEXT(ll) = d; d = l;
                   2855:       d = nd_reconstruct(0,d);
                   2856:       continue;
                   2857:     }
                   2858:     if ( bucket->m < 0 ) continue;
                   2859:     col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   2860:     if ( !col ) {
                   2861:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   2862:       NEXT(ll) = d; d = l;
                   2863:       d = nd_reconstruct(0,d);
                   2864:       continue;
1.1       noro     2865:     }
1.6       noro     2866:     if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
                   2867:   }
                   2868:   return 1;
1.1       noro     2869: }
                   2870:
                   2871: int do_diagonalize_trace(int sugar,int m)
                   2872: {
1.6       noro     2873:   int i,nh,stat;
                   2874:   NODE r,g,t;
                   2875:   ND h,nf,nfq,s,head;
                   2876:   NDV nfv,nfqv;
                   2877:   Q q,den,num;
                   2878:   union oNDC hc;
                   2879:   NODE node;
                   2880:   LIST l;
                   2881:   Z iq;
                   2882:   P cont,cont1;
1.1       noro     2883:
1.6       noro     2884:   for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   2885:     if ( nd_gentrace ) {
                   2886:         /* Trace = [1,index,1,1] */
                   2887:         STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2888:         MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2889:     }
                   2890:     /* for nd_ps */
                   2891:     s = ndvtond(m,nd_ps[i]);
                   2892:     s = nd_separate_head(s,&head);
                   2893:     stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
                   2894:     if ( !stat ) return 0;
                   2895:     nf = nd_add(m,head,nf);
                   2896:     ndv_free(nd_ps[i]);
                   2897:     nd_ps[i] = ndtondv(m,nf);
                   2898:     nd_free(nf);
                   2899:
                   2900:     /* for nd_ps_trace */
                   2901:     if ( nd_demand )
                   2902:         nfv = ndv_load(i);
                   2903:     else
                   2904:         nfv = nd_ps_trace[i];
                   2905:     s = ndvtond(0,nfv);
                   2906:     s = nd_separate_head(s,&head);
                   2907:     stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
                   2908:     if ( !stat ) return 0;
                   2909:     ndv_free(nfv);
                   2910:     hc = HCU(nf); nd_removecont(0,nf);
                   2911:     /* exact division */
1.1       noro     2912:     cont = ndc_div(0,hc,HCU(nf));
1.6       noro     2913:     if ( nd_gentrace ) finalize_tracelist(i,cont);
                   2914:     nfv = ndtondv(0,nf);
                   2915:     nd_free(nf);
                   2916:     nd_bound[i] = ndv_compute_bound(nfv);
                   2917:     register_hcf(nfv);
                   2918:     if ( nd_demand ) {
                   2919:     ndv_save(nfv,i);
                   2920:     ndv_free(nfv);
                   2921:     } else
                   2922:     nd_ps_trace[i] = nfv;
                   2923:   }
                   2924:   return 1;
1.1       noro     2925: }
                   2926:
                   2927: static struct oEGT eg_invdalg;
                   2928: struct oEGT eg_le;
                   2929:
                   2930: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
                   2931: {
                   2932:     NODE tn;
                   2933:     P p1;
                   2934:
                   2935:     for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
                   2936:         substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
                   2937:     }
                   2938:     *r = p;
                   2939: }
                   2940:
                   2941: NODE nd_gb_trace(int m,int ishomo,int **indp)
                   2942: {
1.6       noro     2943:   int i,nh,sugar,stat;
                   2944:   NODE r,g,t;
                   2945:   ND_pairs d;
                   2946:   ND_pairs l;
                   2947:   ND h,nf,nfq,s,head;
                   2948:   NDV nfv,nfqv;
                   2949:   Z q,den,num;
                   2950:   P hc;
                   2951:   union oNDC dn,hnfq;
                   2952:   struct oEGT eg_monic,egm0,egm1;
                   2953:   int diag_count = 0;
                   2954:   P cont;
                   2955:   LIST list;
                   2956:
                   2957:   init_eg(&eg_monic);
                   2958:   init_eg(&eg_invdalg);
                   2959:   init_eg(&eg_le);
                   2960:   g = 0; d = 0;
                   2961:   for ( i = 0; i < nd_psn; i++ ) {
                   2962:     d = update_pairs(d,g,i,0);
                   2963:     g = update_base(g,i);
                   2964:   }
                   2965:   sugar = 0;
                   2966:   while ( d ) {
1.1       noro     2967: again:
1.6       noro     2968:     l = nd_minp(d,&d);
                   2969:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   2970:     if ( SG(l) != sugar ) {
1.1       noro     2971: #if 1
1.6       noro     2972:       if ( ishomo ) {
                   2973:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2974:         stat = do_diagonalize_trace(sugar,m);
                   2975:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2976:         diag_count = 0;
1.1       noro     2977:         if ( !stat ) {
1.6       noro     2978:           NEXT(l) = d; d = l;
                   2979:           d = nd_reconstruct(1,d);
                   2980:           goto again;
1.1       noro     2981:         }
1.6       noro     2982:       }
                   2983: #endif
                   2984:       sugar = SG(l);
                   2985:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2986:     }
                   2987:     stat = nd_sp(m,0,l,&h);
                   2988:     if ( !stat ) {
                   2989:       NEXT(l) = d; d = l;
                   2990:       d = nd_reconstruct(1,d);
                   2991:       goto again;
                   2992:     }
1.1       noro     2993: #if USE_GEOBUCKET
1.39    ! noro     2994:     stat = nd_nf_pbucket(m,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     2995: #else
1.39    ! noro     2996:     stat = nd_nf(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.1       noro     2997: #endif
1.6       noro     2998:     if ( !stat ) {
                   2999:       NEXT(l) = d; d = l;
                   3000:       d = nd_reconstruct(1,d);
                   3001:       goto again;
                   3002:     } else if ( nf ) {
                   3003:       if ( nd_demand ) {
                   3004:         nfqv = ndv_load(nd_psn);
                   3005:         nfq = ndvtond(0,nfqv);
                   3006:       } else
                   3007:         nfq = 0;
                   3008:       if ( !nfq ) {
1.39    ! noro     3009:         if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!nd_top&&!Top,&nfq) ) {
1.6       noro     3010:           NEXT(l) = d; d = l;
                   3011:           d = nd_reconstruct(1,d);
                   3012:           goto again;
                   3013:         }
                   3014:       }
                   3015:       if ( nfq ) {
                   3016:         /* m|HC(nfq) => failure */
                   3017:         if ( nd_vc ) {
                   3018:           nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
                   3019:         } else
                   3020:           q = HCZ(nfq);
                   3021:         if ( !remqi((Q)q,m) ) return 0;
                   3022:
                   3023:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   3024:         hnfq = HCU(nfq);
                   3025:         if ( nd_nalg ) {
                   3026:           /* m|DN(HC(nf)^(-1)) => failure */
                   3027:           get_eg(&egm0);
                   3028:           if ( !nd_monic(m,&nfq) ) return 0;
                   3029:           get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
                   3030:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   3031:           nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
                   3032:         } else {
                   3033:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   3034:           nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
                   3035:         }
                   3036:         if ( nd_gentrace ) {
                   3037:           /* exact division */
                   3038:           cont = ndc_div(0,hnfq,HCU(nfqv));
                   3039:           if ( !UNIQ(cont) ) {
                   3040:             t = mknode(4,NULLP,NULLP,NULLP,cont);
                   3041:             MKLIST(list,t); MKNODE(t,list,nd_tracelist);
                   3042:             nd_tracelist = t;
                   3043:           }
                   3044:         }
1.24      noro     3045:         nh = ndv_newps(0,nfv,nfqv);
1.6       noro     3046:         if ( ishomo && ++diag_count == diag_period ) {
                   3047:           diag_count = 0;
                   3048:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3049:           stat = do_diagonalize_trace(sugar,m);
                   3050:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   3051:           if ( !stat ) {
1.1       noro     3052:             NEXT(l) = d; d = l;
                   3053:             d = nd_reconstruct(1,d);
                   3054:             goto again;
1.6       noro     3055:           }
1.1       noro     3056:         }
1.6       noro     3057:         d = update_pairs(d,g,nh,0);
                   3058:         g = update_base(g,nh);
                   3059:       } else {
                   3060:         if ( DP_Print ) { printf("*"); fflush(stdout); }
                   3061:       }
                   3062:     } else {
                   3063:       if ( DP_Print ) { printf("."); fflush(stdout); }
1.1       noro     3064:     }
1.6       noro     3065:     FREENDP(l);
                   3066:   }
                   3067:   if ( nd_nalg ) {
                   3068:     if ( DP_Print ) {
                   3069:       print_eg("monic",&eg_monic);
                   3070:       print_eg("invdalg",&eg_invdalg);
                   3071:       print_eg("le",&eg_le);
1.1       noro     3072:     }
1.6       noro     3073:   }
1.1       noro     3074:   conv_ilist(nd_demand,1,g,indp);
1.6       noro     3075:   if ( DP_Print ) { printf("nd_gb_trace done.\n"); fflush(stdout); }
                   3076:   return g;
1.1       noro     3077: }
                   3078:
                   3079: int ndv_compare(NDV *p1,NDV *p2)
                   3080: {
                   3081:     return DL_COMPARE(HDL(*p1),HDL(*p2));
                   3082: }
                   3083:
                   3084: int ndv_compare_rev(NDV *p1,NDV *p2)
                   3085: {
                   3086:     return -DL_COMPARE(HDL(*p1),HDL(*p2));
                   3087: }
                   3088:
                   3089: int ndvi_compare(NDVI p1,NDVI p2)
                   3090: {
                   3091:     return DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3092: }
                   3093:
                   3094: int ndvi_compare_rev(NDVI p1,NDVI p2)
                   3095: {
                   3096:     return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3097: }
                   3098:
                   3099: NODE ndv_reduceall(int m,NODE f)
                   3100: {
                   3101:   int i,j,n,stat;
                   3102:   ND nf,g,head;
                   3103:   NODE t,a0,a;
                   3104:   union oNDC dn;
                   3105:   Q q,num,den;
                   3106:   NODE node;
                   3107:   LIST l;
                   3108:   Z iq,jq;
                   3109:   int *perm;
                   3110:   union oNDC hc;
                   3111:   P cont,cont1;
                   3112:
                   3113:   if ( nd_nora ) return f;
                   3114:   n = length(f);
1.24      noro     3115:   ndv_setup(m,0,f,0,1,0);
1.1       noro     3116:   perm = (int *)MALLOC(n*sizeof(int));
                   3117:   if ( nd_gentrace ) {
                   3118:     for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6       noro     3119:       perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1       noro     3120:   }
                   3121:   for ( i = 0; i < n; ) {
                   3122:     if ( nd_gentrace ) {
                   3123:       /* Trace = [1,index,1,1] */
1.6       noro     3124:       STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1       noro     3125:       MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   3126:     }
                   3127:     g = ndvtond(m,nd_ps[i]);
                   3128:     g = nd_separate_head(g,&head);
1.6       noro     3129:     stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1       noro     3130:     if ( !stat )
                   3131:       nd_reconstruct(0,0);
                   3132:     else {
                   3133:       if ( DP_Print ) { printf("."); fflush(stdout); }
                   3134:       ndv_free(nd_ps[i]);
                   3135:       hc = HCU(nf); nd_removecont(m,nf);
                   3136:       if ( nd_gentrace ) {
                   3137:         for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6       noro     3138:           jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
                   3139:           STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1       noro     3140:         }
1.6       noro     3141:         /* exact division */
1.1       noro     3142:         cont = ndc_div(m,hc,HCU(nf));
                   3143:         finalize_tracelist(perm[i],cont);
                   3144:       }
                   3145:       nd_ps[i] = ndtondv(m,nf); nd_free(nf);
                   3146:       nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   3147:       i++;
                   3148:     }
                   3149:   }
                   3150:   if ( DP_Print ) { printf("\n"); }
                   3151:   for ( a0 = 0, i = 0; i < n; i++ ) {
                   3152:     NEXTNODE(a0,a);
                   3153:     if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
                   3154:     else {
                   3155:       for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
                   3156:       BDY(a) = (pointer)nd_ps[j];
                   3157:     }
                   3158:   }
                   3159:   NEXT(a) = 0;
                   3160:   return a0;
                   3161: }
                   3162:
                   3163: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
                   3164: {
                   3165:   ND_pairs d1,nd,cur,head,prev,remove;
                   3166:
                   3167:   if ( !g ) return d;
                   3168:   /* for testing */
                   3169:   if ( gensyz && nd_gensyz == 2 ) {
                   3170:     d1 = nd_newpairs(g,t);
                   3171:     if ( !d )
                   3172:       return d1;
                   3173:     else {
                   3174:       nd = d;
                   3175:       while ( NEXT(nd) ) nd = NEXT(nd);
                   3176:       NEXT(nd) = d1;
                   3177:       return d;
                   3178:     }
                   3179:   }
                   3180:   d = crit_B(d,t);
                   3181:   d1 = nd_newpairs(g,t);
                   3182:   d1 = crit_M(d1);
                   3183:   d1 = crit_F(d1);
                   3184:   if ( gensyz || do_weyl )
                   3185:     head = d1;
                   3186:   else {
                   3187:     prev = 0; cur = head = d1;
                   3188:     while ( cur ) {
                   3189:       if ( crit_2( cur->i1,cur->i2 ) ) {
                   3190:         remove = cur;
                   3191:         if ( !prev ) head = cur = NEXT(cur);
                   3192:         else cur = NEXT(prev) = NEXT(cur);
                   3193:         FREENDP(remove);
                   3194:       } else {
                   3195:         prev = cur; cur = NEXT(cur);
                   3196:       }
                   3197:     }
                   3198:   }
                   3199:   if ( !d )
                   3200:     return head;
                   3201:   else {
                   3202:     nd = d;
                   3203:     while ( NEXT(nd) ) nd = NEXT(nd);
                   3204:     NEXT(nd) = head;
                   3205:     return d;
                   3206:   }
                   3207: }
                   3208:
1.24      noro     3209: ND_pairs merge_pairs_s(ND_pairs d,ND_pairs d1);
                   3210:
1.30      noro     3211: ND_pairs update_pairs_s( ND_pairs d, int t,NODE *syz)
1.24      noro     3212: {
                   3213:   ND_pairs d1;
1.26      noro     3214:   struct oEGT eg1,eg2,eg3;
1.24      noro     3215:
1.30      noro     3216:   if ( !t ) return d;
1.26      noro     3217: get_eg(&eg1);
1.30      noro     3218:   d1 = nd_newpairs_s(t,syz);
1.26      noro     3219: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
1.24      noro     3220:   d = merge_pairs_s(d,d1);
1.26      noro     3221: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
1.24      noro     3222:   return d;
                   3223: }
1.1       noro     3224:
                   3225: ND_pairs nd_newpairs( NODE g, int t )
                   3226: {
                   3227:   NODE h;
                   3228:   UINT *dl;
                   3229:   int ts,s,i,t0,min,max;
                   3230:   ND_pairs r,r0;
                   3231:
                   3232:   dl = DL(nd_psh[t]);
                   3233:   ts = SG(nd_psh[t]) - TD(dl);
1.17      noro     3234:   if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1       noro     3235:   for ( r0 = 0, h = g; h; h = NEXT(h) ) {
                   3236:     if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
                   3237:       continue;
                   3238:     if ( nd_gbblock ) {
                   3239:       t0 = (long)BDY(h);
                   3240:       for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
                   3241:         min = nd_gbblock[i]; max = nd_gbblock[i+1];
                   3242:         if ( t0 >= min && t0 <= max && t >= min && t <= max )
                   3243:           break;
                   3244:       }
                   3245:       if ( nd_gbblock[i] >= 0 )
                   3246:         continue;
                   3247:     }
                   3248:     NEXTND_pairs(r0,r);
                   3249:     r->i1 = (long)BDY(h);
                   3250:     r->i2 = t;
                   3251:     ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
                   3252:     s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   3253:     SG(r) = MAX(s,ts) + TD(LCM(r));
                   3254:     /* experimental */
                   3255:     if ( nd_sugarweight )
                   3256:       r->sugar2 = ndl_weight2(r->lcm);
                   3257:   }
                   3258:   if ( r0 ) NEXT(r) = 0;
                   3259:   return r0;
                   3260: }
                   3261:
1.24      noro     3262: int comp_sig(SIG s1,SIG s2)
                   3263: {
1.38      noro     3264:   if ( nd_sba_pot ) {
                   3265:     if ( s1->pos > s2->pos ) return 1;
                   3266:     else if ( s1->pos < s2->pos ) return -1;
                   3267:     else return (*cmpdl)(nd_nvar,s1->dl,s2->dl);
                   3268:   } else {
                   3269:     static DL m1,m2;
                   3270:     static int nvar;
                   3271:     int ret;
                   3272:
                   3273:     if ( nvar != nd_nvar ) {
                   3274:       nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
                   3275:     }
                   3276:     _adddl(nd_nvar,s1->dl,nd_sba_hm[s1->pos],m1);
                   3277:     _adddl(nd_nvar,s2->dl,nd_sba_hm[s2->pos],m2);
                   3278:     ret = (*cmpdl)(nd_nvar,m1,m2);
                   3279:     if ( ret != 0 ) return ret;
                   3280:     else if ( s1->pos > s2->pos ) return 1;
                   3281:     else if ( s1->pos < s2->pos ) return -1;
                   3282:     else return 0;
1.24      noro     3283:   }
                   3284: }
                   3285:
                   3286: int _create_spair_s(int i1,int i2,ND_pairs sp,SIG sig1,SIG sig2)
                   3287: {
                   3288:   int ret,s1,s2;
                   3289:   RHist p1,p2;
                   3290:   static int wpd;
                   3291:   static UINT *lcm;
                   3292:
                   3293:   sp->i1 = i1;
                   3294:   sp->i2 = i2;
                   3295:   p1 = nd_psh[i1];
                   3296:   p2 = nd_psh[i2];
                   3297:   ndl_lcm(DL(p1),DL(p2),sp->lcm);
                   3298:   s1 = SG(p1)-TD(DL(p1));
                   3299:   s2 = SG(p2)-TD(DL(p2));
                   3300:   SG(sp) = MAX(s1,s2) + TD(sp->lcm);
                   3301:
1.26      noro     3302:   if ( wpd != nd_wpd ) {
1.24      noro     3303:     wpd = nd_wpd;
                   3304:     lcm = (UINT *)MALLOC(wpd*sizeof(UINT));
                   3305:   }
                   3306:   // DL(sig1) <- sp->lcm
                   3307:   // DL(sig1) -= DL(p1)
                   3308:   // DL(sig1) += DL(p1->sig)
1.27      noro     3309:   ndl_sub(sp->lcm,DL(p1),lcm);
1.24      noro     3310:   _ndltodl(lcm,DL(sig1));
                   3311:   _addtodl(nd_nvar,DL(p1->sig),DL(sig1));
                   3312:   sig1->pos = p1->sig->pos;
                   3313:
                   3314:   // DL(sig2) <- sp->lcm
                   3315:   // DL(sig2) -= DL(p2)
                   3316:   // DL(sig2) += DL(p2->sig)
1.27      noro     3317:   ndl_sub(sp->lcm,DL(p2),lcm);
1.24      noro     3318:   _ndltodl(lcm,DL(sig2));
                   3319:   _addtodl(nd_nvar,DL(p2->sig),DL(sig2));
                   3320:   sig2->pos = p2->sig->pos;
                   3321:
                   3322:   ret = comp_sig(sig1,sig2);
                   3323:   if ( ret == 0 ) return 0;
                   3324:   else if ( ret > 0 ) sp->sig = sig1;
                   3325:   else sp->sig = sig2;
                   3326:   return 1;
                   3327: }
                   3328:
                   3329: SIG dup_sig(SIG sig)
                   3330: {
                   3331:   SIG r;
                   3332:
                   3333:   if ( !sig ) return 0;
                   3334:   else {
                   3335:     NEWSIG(r);
                   3336:     _copydl(nd_nvar,DL(sig),DL(r));
                   3337:     r->pos = sig->pos;
                   3338:     return r;
                   3339:   }
                   3340: }
                   3341:
                   3342: void dup_ND_pairs(ND_pairs to,ND_pairs from)
                   3343: {
                   3344:   to->i1 = from->i1;
                   3345:   to->i2 = from->i2;
                   3346:   to->sugar = from->sugar;
                   3347:   to->sugar2 = from->sugar2;
                   3348:   ndl_copy(from->lcm,to->lcm);
                   3349:   to->sig = dup_sig(from->sig);
                   3350: }
                   3351:
                   3352: ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
                   3353: {
                   3354:   struct oND_pairs root;
                   3355:   ND_pairs q1,q2,r0,r;
                   3356:   int ret;
                   3357:
                   3358:   r = &root;
                   3359:   for ( q1 = p1, q2 = p2; q1 != 0 && q2 != 0; ) {
                   3360:     ret = comp_sig(q1->sig,q2->sig);
                   3361:     if ( ret < 0 ) {
                   3362:       r->next = q1; r = q1; q1 = q1->next;
                   3363:     } else if ( ret > 0 ) {
                   3364:       r->next = q2; r = q2; q2 = q2->next;
                   3365:     } else {
                   3366:       ret = DL_COMPARE(q1->lcm,q2->lcm);
1.35      noro     3367:       Nsamesig++;
1.24      noro     3368:       if ( ret < 0 ) {
                   3369:         r->next = q1; r = q1; q1 = q1->next;
                   3370:         q2 = q2->next;
                   3371:       } else {
                   3372:         r->next = q2; r = q2; q2 = q2->next;
                   3373:         q1 = q1->next;
                   3374:       }
                   3375:     }
                   3376:   }
                   3377:   if ( q1 ) {
                   3378:     r->next = q1;
                   3379:   } else {
                   3380:     r->next = q2;
                   3381:   }
                   3382:   return root.next;
                   3383: }
                   3384:
                   3385: ND_pairs insert_pair_s(ND_pairs l,ND_pairs s)
                   3386: {
                   3387:   ND_pairs p,prev;
                   3388:   int ret;
                   3389:
                   3390:   for ( p = l, prev = 0; p != 0; prev = p, p = p->next ) {
                   3391:     if ( (ret = comp_sig(s->sig,p->sig)) <= 0 )
                   3392:       break;
                   3393:   }
                   3394:   if ( ret == 0 ) {
                   3395:     ret = DL_COMPARE(s->lcm,p->lcm);
                   3396:     if ( ret < 0 ) {
                   3397:       // replace p with s
                   3398:       s->next = p->next;
                   3399:       if ( prev == 0 ) {
                   3400:         return s;
                   3401:       } else {
                   3402:         prev->next = s;
                   3403:         return l;
                   3404:       }
                   3405:     } else
                   3406:       return l;
                   3407:   } else {
                   3408:     // insert s between prev and p
                   3409:     s->next = p;
                   3410:     if ( prev == 0 ) {
                   3411:       return s;
                   3412:     } else {
                   3413:       prev->next = s;
                   3414:       return l;
                   3415:     }
                   3416:   }
                   3417: }
                   3418:
1.29      noro     3419: INLINE int __dl_redble(DL d1,DL d2,int nvar)
                   3420: {
                   3421:   int i;
                   3422:
                   3423:   if ( d1->td > d2->td )
                   3424:     return 0;
                   3425:   for ( i = nvar-1; i >= 0; i-- )
                   3426:     if ( d1->d[i] > d2->d[i] )
                   3427:       break;
                   3428:   if ( i >= 0 )
                   3429:     return 0;
                   3430:   else
                   3431:     return 1;
                   3432: }
                   3433:
1.30      noro     3434: ND_pairs nd_newpairs_s(int t, NODE *syz)
1.24      noro     3435: {
                   3436:   NODE h,s;
                   3437:   UINT *dl;
1.27      noro     3438:   int ts,ret,i;
1.24      noro     3439:   ND_pairs r,r0,_sp,sp;
1.29      noro     3440:   SIG spsig,tsig;
                   3441:   static int nvar;
                   3442:   static SIG _sig1,_sig2;
1.26      noro     3443:   struct oEGT eg1,eg2,eg3,eg4;
1.24      noro     3444:
                   3445:   NEWND_pairs(_sp);
1.29      noro     3446:   if ( !_sig1 || nvar != nd_nvar ) {
                   3447:     nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
                   3448:   }
1.24      noro     3449:   r0 = 0;
1.27      noro     3450:   for ( i = 0; i < t; i++ ) {
                   3451:     ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
1.24      noro     3452:     if ( ret ) {
                   3453:       spsig = _sp->sig;
1.29      noro     3454:       for ( s = syz[spsig->pos]; s; s = s->next ) {
1.24      noro     3455:         tsig = (SIG)s->body;
1.29      noro     3456:         if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
1.24      noro     3457:           break;
                   3458:       }
                   3459:       if ( s == 0 ) {
                   3460:         NEWND_pairs(sp);
                   3461:         dup_ND_pairs(sp,_sp);
                   3462:         r0 = insert_pair_s(r0,sp);
1.26      noro     3463:       } else
                   3464:         Nsyz++;
1.24      noro     3465:     }
                   3466:   }
                   3467:   return r0;
                   3468: }
                   3469:
1.1       noro     3470: /* ipair = [i1,i2],[i1,i2],... */
                   3471: ND_pairs nd_ipairtospair(NODE ipair)
                   3472: {
                   3473:   int s1,s2;
                   3474:   NODE tn,t;
                   3475:   ND_pairs r,r0;
                   3476:
                   3477:   for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
                   3478:     NEXTND_pairs(r0,r);
                   3479:     tn = BDY((LIST)BDY(t));
1.6       noro     3480:     r->i1 = ZTOS((Q)ARG0(tn));
                   3481:     r->i2 = ZTOS((Q)ARG1(tn));
1.1       noro     3482:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
                   3483:     s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   3484:     s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
                   3485:     SG(r) = MAX(s1,s2) + TD(LCM(r));
                   3486:     /* experimental */
                   3487:     if ( nd_sugarweight )
                   3488:         r->sugar2 = ndl_weight2(r->lcm);
                   3489:   }
                   3490:   if ( r0 ) NEXT(r) = 0;
                   3491:   return r0;
                   3492: }
                   3493:
                   3494: /* kokokara */
                   3495:
                   3496: ND_pairs crit_B( ND_pairs d, int s )
                   3497: {
                   3498:   ND_pairs cur,head,prev,remove;
                   3499:   UINT *t,*tl,*lcm;
                   3500:   int td,tdl;
                   3501:
                   3502:   if ( !d ) return 0;
                   3503:   t = DL(nd_psh[s]);
                   3504:   prev = 0;
                   3505:   head = cur = d;
                   3506:   lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   3507:   while ( cur ) {
                   3508:     tl = cur->lcm;
                   3509:     if ( ndl_reducible(tl,t) ) {
                   3510:       ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
                   3511:       if ( !ndl_equal(lcm,tl) ) {
                   3512:         ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
                   3513:         if (!ndl_equal(lcm,tl)) {
                   3514:           remove = cur;
                   3515:           if ( !prev ) {
                   3516:             head = cur = NEXT(cur);
                   3517:           } else {
                   3518:             cur = NEXT(prev) = NEXT(cur);
                   3519:           }
                   3520:           FREENDP(remove);
                   3521:         } else {
                   3522:           prev = cur; cur = NEXT(cur);
                   3523:         }
                   3524:       } else {
                   3525:         prev = cur; cur = NEXT(cur);
                   3526:       }
                   3527:     } else {
                   3528:       prev = cur; cur = NEXT(cur);
                   3529:     }
                   3530:   }
                   3531:   return head;
                   3532: }
                   3533:
                   3534: ND_pairs crit_M( ND_pairs d1 )
                   3535: {
                   3536:     ND_pairs e,d2,d3,dd,p;
                   3537:     UINT *id,*jd;
                   3538:
                   3539:     if ( !d1 ) return d1;
                   3540:     for ( dd = 0, e = d1; e; e = d3 ) {
                   3541:         if ( !(d2 = NEXT(e)) ) {
                   3542:             NEXT(e) = dd;
                   3543:             return e;
                   3544:         }
                   3545:         id = LCM(e);
                   3546:         for ( d3 = 0; d2; d2 = p ) {
                   3547:             p = NEXT(d2);
                   3548:             jd = LCM(d2);
                   3549:             if ( ndl_equal(jd,id) )
                   3550:                 ;
                   3551:             else if ( TD(jd) > TD(id) )
                   3552:                 if ( ndl_reducible(jd,id) ) continue;
                   3553:                 else ;
                   3554:             else if ( ndl_reducible(id,jd) ) goto delit;
                   3555:             NEXT(d2) = d3;
                   3556:             d3 = d2;
                   3557:         }
                   3558:         NEXT(e) = dd;
                   3559:         dd = e;
                   3560:         continue;
                   3561:         /**/
                   3562:     delit:    NEXT(d2) = d3;
                   3563:         d3 = d2;
                   3564:         for ( ; p; p = d2 ) {
                   3565:             d2 = NEXT(p);
                   3566:             NEXT(p) = d3;
                   3567:             d3 = p;
                   3568:         }
                   3569:         FREENDP(e);
                   3570:     }
                   3571:     return dd;
                   3572: }
                   3573:
                   3574: ND_pairs crit_F( ND_pairs d1 )
                   3575: {
                   3576:     ND_pairs rest, head,remove;
                   3577:     ND_pairs last, p, r, w;
                   3578:     int s;
                   3579:
                   3580:     if ( !d1 ) return d1;
                   3581:     for ( head = last = 0, p = d1; NEXT(p); ) {
                   3582:         r = w = equivalent_pairs(p,&rest);
                   3583:         s = SG(r);
                   3584:         w = NEXT(w);
                   3585:         while ( w ) {
                   3586:             if ( crit_2(w->i1,w->i2) ) {
                   3587:                 r = w;
                   3588:                 w = NEXT(w);
                   3589:                 while ( w ) {
                   3590:                     remove = w;
                   3591:                     w = NEXT(w);
                   3592:                     FREENDP(remove);
                   3593:                 }
                   3594:                 break;
                   3595:             } else if ( SG(w) < s ) {
                   3596:                 FREENDP(r);
                   3597:                 r = w;
                   3598:                 s = SG(r);
                   3599:                 w = NEXT(w);
                   3600:             } else {
                   3601:                 remove = w;
                   3602:                 w = NEXT(w);
                   3603:                 FREENDP(remove);
                   3604:             }
                   3605:         }
                   3606:         if ( last ) NEXT(last) = r;
                   3607:         else head = r;
                   3608:         NEXT(last = r) = 0;
                   3609:         p = rest;
                   3610:         if ( !p ) return head;
                   3611:     }
                   3612:     if ( !last ) return p;
                   3613:     NEXT(last) = p;
                   3614:     return head;
                   3615: }
                   3616:
                   3617: int crit_2( int dp1, int dp2 )
                   3618: {
                   3619:     return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
                   3620: }
                   3621:
                   3622: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
                   3623: {
                   3624:     ND_pairs w,p,r,s;
                   3625:     UINT *d;
                   3626:
                   3627:     w = d1;
                   3628:     d = LCM(w);
                   3629:     s = NEXT(w);
                   3630:     NEXT(w) = 0;
                   3631:     for ( r = 0; s; s = p ) {
                   3632:         p = NEXT(s);
                   3633:         if ( ndl_equal(d,LCM(s)) ) {
                   3634:             NEXT(s) = w; w = s;
                   3635:         } else {
                   3636:             NEXT(s) = r; r = s;
                   3637:         }
                   3638:     }
                   3639:     *prest = r;
                   3640:     return w;
                   3641: }
                   3642:
                   3643: NODE update_base(NODE nd,int ndp)
                   3644: {
                   3645:     UINT *dl, *dln;
                   3646:     NODE last, p, head;
                   3647:
                   3648:     dl = DL(nd_psh[ndp]);
                   3649:     for ( head = last = 0, p = nd; p; ) {
                   3650:         dln = DL(nd_psh[(long)BDY(p)]);
                   3651:         if ( ndl_reducible( dln, dl ) ) {
                   3652:             p = NEXT(p);
                   3653:             if ( last ) NEXT(last) = p;
                   3654:         } else {
                   3655:             if ( !last ) head = p;
                   3656:             p = NEXT(last = p);
                   3657:         }
                   3658:     }
                   3659:     head = append_one(head,ndp);
                   3660:     return head;
                   3661: }
                   3662:
                   3663: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   3664: {
                   3665:     ND_pairs m,ml,p,l;
                   3666:     UINT *lcm;
                   3667:     int s,td,len,tlen,c,c1;
                   3668:
                   3669:     if ( !(p = NEXT(m = d)) ) {
                   3670:         *prest = p;
                   3671:         NEXT(m) = 0;
                   3672:         return m;
                   3673:     }
                   3674:     if ( !NoSugar ) {
                   3675:         if ( nd_sugarweight ) {
                   3676:             s = m->sugar2;
                   3677:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   3678:                 if ( (p->sugar2 < s)
                   3679:                     || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   3680:                     ml = l; m = p; s = m->sugar2;
                   3681:                 }
                   3682:         } else {
                   3683:             s = SG(m);
                   3684:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   3685:                 if ( (SG(p) < s)
                   3686:                     || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   3687:                     ml = l; m = p; s = SG(m);
                   3688:                 }
                   3689:         }
                   3690:     } else {
                   3691:         for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   3692:             if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
                   3693:                 ml = l; m = p; s = SG(m);
                   3694:             }
                   3695:     }
                   3696:     if ( !ml ) *prest = NEXT(m);
                   3697:     else {
                   3698:         NEXT(ml) = NEXT(m);
                   3699:         *prest = d;
                   3700:     }
                   3701:     NEXT(m) = 0;
                   3702:     return m;
                   3703: }
                   3704:
                   3705: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   3706: {
                   3707:     int msugar,i;
                   3708:     ND_pairs t,dm0,dm,dr0,dr;
                   3709:
                   3710:     if ( nd_sugarweight ) {
                   3711:         for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
                   3712:             if ( t->sugar2 < msugar ) msugar = t->sugar2;
                   3713:         dm0 = 0; dr0 = 0;
                   3714:         for ( i = 0, t = d; t; t = NEXT(t) )
                   3715:             if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
                   3716:                 if ( dm0 ) NEXT(dm) = t;
                   3717:                 else dm0 = t;
                   3718:                 dm = t;
                   3719:                 i++;
                   3720:             } else {
                   3721:                 if ( dr0 ) NEXT(dr) = t;
                   3722:                 else dr0 = t;
                   3723:                 dr = t;
                   3724:             }
                   3725:     } else {
                   3726:         for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   3727:             if ( SG(t) < msugar ) msugar = SG(t);
                   3728:         dm0 = 0; dr0 = 0;
                   3729:         for ( i = 0, t = d; t; t = NEXT(t) )
                   3730:             if ( i < nd_f4_nsp && SG(t) == msugar ) {
                   3731:                 if ( dm0 ) NEXT(dm) = t;
                   3732:                 else dm0 = t;
                   3733:                 dm = t;
                   3734:                 i++;
                   3735:             } else {
                   3736:                 if ( dr0 ) NEXT(dr) = t;
                   3737:                 else dr0 = t;
                   3738:                 dr = t;
                   3739:             }
                   3740:     }
                   3741:     NEXT(dm) = 0;
                   3742:     if ( dr0 ) NEXT(dr) = 0;
                   3743:     *prest = dr0;
                   3744:     return dm0;
                   3745: }
                   3746:
1.28      noro     3747: ND_pairs nd_minsugarp_s( ND_pairs d, ND_pairs *prest )
                   3748: {
                   3749:   int msugar;
                   3750:   ND_pairs t,last;
                   3751:
                   3752:   for ( msugar = SG(d), t = d; t; t = NEXT(t) )
                   3753:     if ( SG(t) == msugar ) last = t;
                   3754:   *prest = last->next;
                   3755:   last->next = 0;
                   3756:   return d;
                   3757: }
                   3758:
1.1       noro     3759: int nd_tdeg(NDV c)
                   3760: {
                   3761:   int wmax = 0;
                   3762:   int i,len;
                   3763:   NMV a;
                   3764:
                   3765:   len = LEN(c);
                   3766:   for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   3767:     wmax = MAX(TD(DL(a)),wmax);
                   3768:   return wmax;
                   3769: }
                   3770:
1.24      noro     3771: int ndv_newps(int m,NDV a,NDV aq)
1.1       noro     3772: {
                   3773:     int len;
                   3774:     RHist r;
                   3775:     NDV b;
                   3776:     NODE tn;
                   3777:     LIST l;
                   3778:     Z iq;
                   3779:
                   3780:     if ( nd_psn == nd_pslen ) {
                   3781:         nd_pslen *= 2;
                   3782:         nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
                   3783:         nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
                   3784:         nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
                   3785:         nd_bound = (UINT **)
                   3786:             REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
                   3787:         nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
                   3788:         nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
                   3789:     }
                   3790:     NEWRHist(r); nd_psh[nd_psn] = r;
                   3791:     nd_ps[nd_psn] = a;
                   3792:     if ( aq ) {
                   3793:         nd_ps_trace[nd_psn] = aq;
                   3794:         if ( !m ) {
                   3795:           register_hcf(aq);
                   3796:         } else if ( m == -2 ) {
                   3797:           /* do nothing */
                   3798:         } else
                   3799:           error("ndv_newps : invalud modulus");
                   3800:         nd_bound[nd_psn] = ndv_compute_bound(aq);
                   3801: #if 1
                   3802:         SG(r) = SG(aq);
                   3803: #else
                   3804:         SG(r) = nd_tdeg(aq);
                   3805: #endif
                   3806:         ndl_copy(HDL(aq),DL(r));
1.24      noro     3807:         r->sig = dup_sig(aq->sig);
1.1       noro     3808:     } else {
                   3809:         if ( !m ) register_hcf(a);
                   3810:         nd_bound[nd_psn] = ndv_compute_bound(a);
                   3811: #if 1
                   3812:         SG(r) = SG(a);
                   3813: #else
                   3814:         SG(r) = nd_tdeg(a);
                   3815: #endif
                   3816:         ndl_copy(HDL(a),DL(r));
1.24      noro     3817:         r->sig = dup_sig(a->sig);
1.1       noro     3818:     }
                   3819:     if ( nd_demand ) {
                   3820:         if ( aq ) {
                   3821:             ndv_save(nd_ps_trace[nd_psn],nd_psn);
                   3822:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   3823:             nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   3824:             nd_ps_trace[nd_psn] = 0;
                   3825:         } else {
                   3826:             ndv_save(nd_ps[nd_psn],nd_psn);
                   3827:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
                   3828:             nd_ps[nd_psn] = 0;
                   3829:         }
                   3830:     }
                   3831:     if ( nd_gentrace ) {
                   3832:         /* reverse the tracelist and append it to alltracelist */
                   3833:         nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6       noro     3834:         STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1       noro     3835:         MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
                   3836:     }
                   3837:     return nd_psn++;
                   3838: }
                   3839:
                   3840: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
                   3841: /* return 1 if success, 0 if failure (HC(a mod p)) */
                   3842:
1.24      noro     3843: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont,int sba)
1.1       noro     3844: {
1.6       noro     3845:   int i,j,td,len,max;
                   3846:   NODE s,s0,f0,tn;
                   3847:   UINT *d;
                   3848:   RHist r;
                   3849:   NDVI w;
                   3850:   NDV a,am;
                   3851:   union oNDC hc;
                   3852:   NODE node;
                   3853:   P hcp;
                   3854:   Z iq,jq;
                   3855:   LIST l;
                   3856:
                   3857:   nd_found = 0; nd_notfirst = 0; nd_create = 0;
                   3858:   /* initialize the tracelist */
                   3859:   nd_tracelist = 0;
                   3860:
                   3861:   for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
                   3862:   w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
                   3863:   for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
                   3864:     if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
                   3865:   if ( !dont_sort ) {
                   3866:     /* XXX heuristic */
1.26      noro     3867:     if ( !sba && !nd_ord->id && (nd_ord->ord.simple<2) )
1.6       noro     3868:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   3869:         (int (*)(const void *,const void *))ndvi_compare_rev);
                   3870:     else
                   3871:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   3872:         (int (*)(const void *,const void *))ndvi_compare);
                   3873:   }
                   3874:   nd_pslen = 2*nd_psn;
                   3875:   nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3876:   nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3877:   nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3878:   nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3879:   nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
                   3880:   nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
                   3881:   nd_hcf = 0;
1.1       noro     3882:
1.6       noro     3883:   if ( trace && nd_vc )
                   3884:     makesubst(nd_vc,&nd_subst);
                   3885:   else
                   3886:     nd_subst = 0;
1.1       noro     3887:
1.6       noro     3888:   if ( !nd_red )
                   3889:     nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   3890:   for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
                   3891:   for ( i = 0; i < nd_psn; i++ ) {
                   3892:     hc = HCU(w[i].p);
                   3893:     if ( trace ) {
                   3894:       if ( mod == -2 ) {
                   3895:         /* over a large finite field */
                   3896:         /* trace = small modulus */
                   3897:         a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
                   3898:         ndv_mod(-2,a);
                   3899:         if ( !dont_removecont) ndv_removecont(-2,a);
                   3900:         am = nd_ps[i] = ndv_dup(trace,w[i].p);
                   3901:         ndv_mod(trace,am);
                   3902:       if ( DL_COMPARE(HDL(am),HDL(a)) )
                   3903:         return 0;
                   3904:         ndv_removecont(trace,am);
                   3905:       } else {
                   3906:         a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
                   3907:         if ( !dont_removecont) ndv_removecont(0,a);
                   3908:         register_hcf(a);
                   3909:         am = nd_ps[i] = ndv_dup(mod,a);
                   3910:         ndv_mod(mod,am);
                   3911:       if ( DL_COMPARE(HDL(am),HDL(a)) )
                   3912:         return 0;
                   3913:         ndv_removecont(mod,am);
                   3914:       }
                   3915:     } else {
                   3916:       a = nd_ps[i] = ndv_dup(mod,w[i].p);
                   3917:       if ( mod || !dont_removecont ) ndv_removecont(mod,a);
                   3918:       if ( !mod ) register_hcf(a);
1.1       noro     3919:     }
1.6       noro     3920:     if ( nd_gentrace ) {
                   3921:       STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
                   3922:       /* exact division */
1.1       noro     3923:       if ( !dont_removecont )
1.6       noro     3924:         ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
                   3925:       MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
                   3926:     }
                   3927:     NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
                   3928:     nd_bound[i] = ndv_compute_bound(a);
                   3929:     nd_psh[i] = r;
                   3930:     if ( nd_demand ) {
                   3931:       if ( trace ) {
                   3932:         ndv_save(nd_ps_trace[i],i);
                   3933:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   3934:         nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   3935:         nd_ps_trace[i] = 0;
                   3936:       } else {
                   3937:         ndv_save(nd_ps[i],i);
                   3938:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
                   3939:         nd_ps[i] = 0;
                   3940:       }
1.1       noro     3941:     }
1.6       noro     3942:   }
1.24      noro     3943:   if ( sba ) {
1.27      noro     3944:     nd_sba_hm = (DL *)MALLOC(nd_psn*sizeof(DL));
1.24      noro     3945:    // setup signatures
1.27      noro     3946:     for ( i = 0; i < nd_psn; i++ ) {
                   3947:       SIG sig;
1.24      noro     3948:
1.27      noro     3949:       NEWSIG(sig); sig->pos = i;
                   3950:       nd_ps[i]->sig = sig;
                   3951:       if ( nd_demand ) nd_ps_sym[i]->sig = sig;
                   3952:         nd_psh[i]->sig = sig;
                   3953:       if ( trace ) {
                   3954:         nd_ps_trace[i]->sig = sig;
                   3955:         if ( nd_demand ) nd_ps_trace_sym[i]->sig = sig;
                   3956:       }
                   3957:       NEWDL(nd_sba_hm[i],nd_nvar);
                   3958:       _ndltodl(DL(nd_psh[i]),nd_sba_hm[i]);
                   3959:     }
1.29      noro     3960:     nd_sba_pos = (NODE *)MALLOC(nd_psn*sizeof(NODE));
                   3961:     for ( i = 0; i < nd_psn; i++ ) {
                   3962:       j = nd_psh[i]->sig->pos;
                   3963:       nd_sba_pos[j] = append_one(nd_sba_pos[j],i);
                   3964:     }
1.24      noro     3965:   }
1.6       noro     3966:   if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
                   3967:   return 1;
1.1       noro     3968: }
                   3969:
                   3970: struct order_spec *append_block(struct order_spec *spec,
                   3971:     int nv,int nalg,int ord);
                   3972:
                   3973: extern VECT current_dl_weight_vector_obj;
                   3974: static VECT prev_weight_vector_obj;
                   3975:
                   3976: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
                   3977:     struct order_spec **ord1p,LIST *f1p,NODE *alistp)
                   3978: {
                   3979:     NODE alist,t,s,r0,r,arg;
                   3980:     VL tv;
                   3981:     P poly;
                   3982:     DP d;
                   3983:     Alg alpha,dp;
                   3984:     DAlg inv,da,hc;
                   3985:     MP m;
                   3986:     int i,nvar,nalg,n;
                   3987:     NumberField nf;
                   3988:     LIST f1,f2;
                   3989:     struct order_spec *current_spec;
                   3990:     VECT obj,obj0;
                   3991:     VECT tmp;
                   3992:
                   3993:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
                   3994:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
                   3995:
                   3996:     for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
                   3997:         NEXTNODE(alist,t); MKV(tv->v,poly);
                   3998:         MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
                   3999:         tv->v = tv->v->priv;
                   4000:     }
                   4001:     NEXT(t) = 0;
                   4002:
                   4003:     /* simplification, making polynomials monic */
                   4004:     setfield_dalg(alist);
                   4005:     obj_algtodalg((Obj)f,(Obj *)&f1);
                   4006:     for ( t = BDY(f); t; t = NEXT(t) ) {
                   4007:         initd(ord); ptod(vv,vv,(P)BDY(t),&d);
                   4008:         hc = (DAlg)BDY(d)->c;
                   4009:         if ( NID(hc) == N_DA ) {
                   4010:             invdalg(hc,&inv);
                   4011:             for ( m = BDY(d); m; m = NEXT(m) ) {
                   4012:                 muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
                   4013:             }
                   4014:         }
                   4015:         initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
                   4016:     }
                   4017:     obj_dalgtoalg((Obj)f1,(Obj *)&f);
                   4018:
                   4019:     /* append alg vars to the var list */
                   4020:     for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
                   4021:     NEXT(tv) = av;
                   4022:
                   4023:     /* append a block to ord */
                   4024:     *ord1p = append_block(ord,nvar,nalg,2);
                   4025:
                   4026:     /* create generator list */
                   4027:     nf = get_numberfield();
                   4028:     for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
                   4029:         MKAlg(nf->defpoly[i],dp);
                   4030:         MKNODE(s,dp,t); t = s;
                   4031:     }
                   4032:     MKLIST(f1,t);
                   4033:     *alistp = alist;
                   4034:     algobjtorat((Obj)f1,(Obj *)f1p);
                   4035:
                   4036:     /* creating a new weight vector */
                   4037:     prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
                   4038:     n = nvar+nalg+1;
                   4039:     MKVECT(obj,n);
                   4040:     if ( obj0 && obj0->len == nvar )
                   4041:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
                   4042:     else
                   4043:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
                   4044:     for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
                   4045:     BDY(obj)[n-1] = (pointer)ONE;
                   4046:     arg = mknode(1,obj);
                   4047:     Pdp_set_weight(arg,&tmp);
                   4048: }
                   4049:
                   4050: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
                   4051: {
                   4052:     NODE s,t,u0,u;
                   4053:     P p;
                   4054:     VL tv;
                   4055:     Obj obj;
                   4056:     VECT tmp;
                   4057:     NODE arg;
                   4058:
                   4059:     u0 = 0;
                   4060:     for ( t = r; t; t = NEXT(t) ) {
                   4061:         p = (P)BDY(t);
                   4062:         for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
                   4063:             substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
                   4064:         }
                   4065:         if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
                   4066:             NEXTNODE(u0,u);
                   4067:             BDY(u) = (pointer)p;
                   4068:         }
                   4069:     }
                   4070:     arg = mknode(1,prev_weight_vector_obj);
                   4071:     Pdp_set_weight(arg,&tmp);
                   4072:
                   4073:     return u0;
                   4074: }
                   4075:
                   4076: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
                   4077: {
                   4078:     VL tv,fv,vv,vc,av;
                   4079:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4080:     int e,max,nvar,i;
                   4081:     NDV b;
                   4082:     int ishomo,nalg,mrank,trank,wmax,len;
                   4083:   NMV a;
                   4084:     Alg alpha,dp;
                   4085:     P p,zp;
                   4086:     Q dmy;
                   4087:     LIST f1,f2,zpl;
                   4088:     Obj obj;
                   4089:     NumberField nf;
                   4090:     struct order_spec *ord1;
                   4091:     NODE tr,tl1,tl2,tl3,tl4,nzlist;
                   4092:     LIST l1,l2,l3,l4,l5;
                   4093:   int j;
                   4094:   Z jq,bpe,last_nonzero;
                   4095:     int *perm;
                   4096:     EPOS oepos;
                   4097:     int obpe,oadv,ompos,cbpe;
1.15      noro     4098:     VECT hvect;
1.1       noro     4099:
                   4100:     nd_module = 0;
                   4101:     if ( !m && Demand ) nd_demand = 1;
                   4102:     else nd_demand = 0;
                   4103:     parse_nd_option(current_option);
                   4104:
                   4105:     if ( DP_Multiple )
                   4106:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4107: #if 0
                   4108:     ndv_alloc = 0;
                   4109: #endif
                   4110:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4111:     if ( m && nd_vc )
                   4112:        error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
                   4113:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4114:     switch ( ord->id ) {
                   4115:         case 1:
                   4116:             if ( ord->nv != nvar )
                   4117:                 error("nd_{gr,f4} : invalid order specification");
                   4118:             break;
                   4119:         default:
                   4120:             break;
                   4121:     }
                   4122:     nd_nalg = 0;
                   4123:     av = 0;
                   4124:     if ( !m ) {
                   4125:         get_algtree((Obj)f,&av);
                   4126:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4127:         nd_ntrans = nvar;
                   4128:         nd_nalg = nalg;
                   4129:         /* #i -> t#i */
                   4130:         if ( nalg ) {
                   4131:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4132:             ord = ord1;
                   4133:             f = f1;
                   4134:         }
                   4135:         nvar += nalg;
                   4136:     }
                   4137:     nd_init_ord(ord);
                   4138:     mrank = 0;
                   4139:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4140:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4141:             if ( nd_module ) {
1.16      noro     4142:                 if ( OID(BDY(t)) == O_DPM ) {
                   4143:                   e = dpm_getdeg((DPM)BDY(t),&trank);
                   4144:                   max = MAX(e,max);
                   4145:                   mrank = MAX(mrank,trank);
                   4146:                 } else {
                   4147:                   s = BDY((LIST)BDY(t));
                   4148:                   trank = length(s);
                   4149:                   mrank = MAX(mrank,trank);
                   4150:                   for ( ; s; s = NEXT(s) ) {
                   4151:                       e = getdeg(tv->v,(P)BDY(s));
                   4152:                       max = MAX(e,max);
                   4153:                   }
1.1       noro     4154:                 }
                   4155:             } else {
                   4156:                 e = getdeg(tv->v,(P)BDY(t));
                   4157:                 max = MAX(e,max);
                   4158:             }
                   4159:         }
                   4160:     nd_setup_parameters(nvar,nd_nzlist?0:max);
                   4161:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   4162:     ishomo = 1;
                   4163:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4164:       if ( nd_module ) {
1.16      noro     4165:         if ( OID(BDY(t)) == O_DPM ) {
                   4166:           Z cont;
                   4167:           DPM zdpm;
                   4168:
                   4169:           if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
                   4170:           else zdpm = (DPM)BDY(t);
                   4171:           b = (pointer)dpmtondv(m,zdpm);
                   4172:         } else {
                   4173:           if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   4174:           else zpl = (LIST)BDY(t);
1.1       noro     4175:           b = (pointer)pltondv(CO,vv,zpl);
1.16      noro     4176:         }
1.1       noro     4177:       } else {
                   4178:         if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   4179:         else zp = (P)BDY(t);
                   4180:         b = (pointer)ptondv(CO,vv,zp);
                   4181:       }
                   4182:       if ( ishomo )
                   4183:         ishomo = ishomo && ndv_ishomo(b);
                   4184:       if ( m ) ndv_mod(m,b);
                   4185:       if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4186:     }
                   4187:     if ( fd0 ) NEXT(fd) = 0;
                   4188:
                   4189:   if ( !ishomo && homo ) {
                   4190:         for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   4191:             b = (NDV)BDY(t); len = LEN(b);
                   4192:             for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   4193:                 wmax = MAX(TD(DL(a)),wmax);
                   4194:         }
                   4195:         homogenize_order(ord,nvar,&ord1);
                   4196:         nd_init_ord(ord1);
                   4197:         nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   4198:         for ( t = fd0; t; t = NEXT(t) )
                   4199:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   4200:     }
                   4201:
1.24      noro     4202:     ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0,0);
1.1       noro     4203:     if ( nd_gentrace ) {
                   4204:         MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   4205:     }
                   4206:   if ( nd_splist ) {
                   4207:     *rp = compute_splist();
                   4208:     return;
                   4209:   }
                   4210:   if ( nd_check_splist ) {
                   4211:         if ( f4 ) {
                   4212:             if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
                   4213:             else *rp = 0;
                   4214:         } else {
                   4215:             if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
                   4216:             else *rp = 0;
                   4217:         }
                   4218:     return;
                   4219:   }
                   4220:     x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
                   4221:   if ( !x ) {
                   4222:     *rp = 0; return;
                   4223:   }
1.15      noro     4224:   if ( nd_gentrace ) {
                   4225:     MKVECT(hvect,nd_psn);
                   4226:     for ( i = 0; i < nd_psn; i++ )
                   4227:        ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   4228:   }
1.1       noro     4229:   if ( !ishomo && homo ) {
                   4230:        /* dehomogenization */
                   4231:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   4232:     nd_init_ord(ord);
                   4233:     nd_setup_parameters(nvar,0);
                   4234:   }
                   4235:     nd_demand = 0;
                   4236:   if ( nd_module && nd_intersect ) {
                   4237:     for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17      noro     4238:       if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1       noro     4239:         MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
                   4240:       }
                   4241:     conv_ilist(nd_demand,0,x,0);
                   4242:     goto FINAL;
                   4243:   }
                   4244:     if ( nd_gentrace  && f4 ) { nzlist = nd_alltracelist; }
                   4245:     x = ndv_reducebase(x,perm);
                   4246:     if ( nd_gentrace  && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
                   4247:     x = ndv_reduceall(m,x);
                   4248:     cbpe = nd_bpe;
                   4249:     if ( nd_gentrace && !f4 ) {
                   4250:         tl2 = nd_alltracelist; nd_alltracelist = 0;
                   4251:         ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
                   4252:         tl3 = nd_alltracelist; nd_alltracelist = 0;
                   4253:         if ( nd_gensyz ) {
                   4254:           nd_gb(m,0,1,1,0);
                   4255:             tl4 = nd_alltracelist; nd_alltracelist = 0;
                   4256:         } else tl4 = 0;
                   4257:     }
                   4258:     nd_bpe = cbpe;
                   4259:     nd_setup_parameters(nd_nvar,0);
                   4260: FINAL:
                   4261:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16      noro     4262:       NEXTNODE(r0,r);
                   4263:       if ( nd_module ) {
                   4264:         if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
                   4265:         else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
                   4266:       } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
                   4267:       else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1       noro     4268:     }
                   4269:     if ( r0 ) NEXT(r) = 0;
                   4270:     if ( !m && nd_nalg )
                   4271:         r0 = postprocess_algcoef(av,alist,r0);
                   4272:     MKLIST(*rp,r0);
                   4273:     if ( nd_gentrace ) {
                   4274:   if ( f4 ) {
1.6       noro     4275:             STOZ(16,bpe);
                   4276:             STOZ(nd_last_nonzero,last_nonzero);
1.15      noro     4277:             tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1       noro     4278:         } else {
                   4279:             tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   4280:             tl3 = reverse_node(tl3);
                   4281:             /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   4282:             for ( t = tl2; t; t = NEXT(t) ) {
                   4283:             /* s = [i,[*,j,*,*],...] */
                   4284:                 s = BDY((LIST)BDY(t));
1.6       noro     4285:                 j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     4286:                 for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     4287:                     j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     4288:                     ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   4289:                 }
                   4290:             }
                   4291:             for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6       noro     4292:                 STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     4293:             }
                   4294:             MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   4295:             MKLIST(l5,tl4);
1.6       noro     4296:             STOZ(nd_bpe,bpe);
1.15      noro     4297:             tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     4298:         }
                   4299:     }
                   4300: #if 0
                   4301:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   4302: #endif
                   4303: }
                   4304:
1.28      noro     4305: NODE nd_sba_f4(int m,int **indp);
                   4306:
                   4307: void nd_sba(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.24      noro     4308: {
                   4309:   VL tv,fv,vv,vc,av;
                   4310:   NODE fd,fd0,r,r0,t,x,s,xx;
                   4311:   int e,max,nvar,i;
                   4312:   NDV b;
                   4313:   int ishomo,nalg,wmax,len;
                   4314:   NMV a;
                   4315:   P p,zp;
                   4316:   Q dmy;
                   4317:   struct order_spec *ord1;
                   4318:   int j;
                   4319:   int *perm;
                   4320:   EPOS oepos;
                   4321:   int obpe,oadv,ompos,cbpe;
1.29      noro     4322:   struct oEGT eg0,eg1,egconv;
1.24      noro     4323:
                   4324:   nd_module = 0;
                   4325:   nd_demand = 0;
                   4326:   parse_nd_option(current_option);
1.35      noro     4327:   Nsamesig = 0;
1.24      noro     4328:   if ( DP_Multiple )
                   4329:     nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4330:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4331:   if ( m && nd_vc )
                   4332:     error("nd_sba : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
                   4333:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4334:   switch ( ord->id ) {
                   4335:     case 1:
                   4336:       if ( ord->nv != nvar )
                   4337:         error("nd_sba : invalid order specification");
                   4338:         break;
                   4339:       default:
                   4340:         break;
                   4341:   }
                   4342:   nd_nalg = 0;
                   4343:   nd_init_ord(ord);
                   4344:   // for SIG comparison
                   4345:   initd(ord);
                   4346:   for ( t = BDY(f), max = 1; t; t = NEXT(t) ) {
                   4347:     for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4348:       e = getdeg(tv->v,(P)BDY(t));
                   4349:       max = MAX(e,max);
                   4350:     }
                   4351:   }
1.25      noro     4352:   nd_setup_parameters(nvar,max);
1.24      noro     4353:   obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   4354:   ishomo = 1;
                   4355:   for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4356:     if ( !m ) ptozp((P)BDY(t),1,&dmy,&zp);
                   4357:     else zp = (P)BDY(t);
                   4358:     b = (pointer)ptondv(CO,vv,zp);
                   4359:     if ( ishomo )
                   4360:       ishomo = ishomo && ndv_ishomo(b);
                   4361:     if ( m ) ndv_mod(m,b);
                   4362:     if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4363:   }
                   4364:   if ( fd0 ) NEXT(fd) = 0;
                   4365:
                   4366:   if ( !ishomo && homo ) {
                   4367:     for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   4368:       b = (NDV)BDY(t); len = LEN(b);
                   4369:       for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   4370:         wmax = MAX(TD(DL(a)),wmax);
                   4371:       }
                   4372:       homogenize_order(ord,nvar,&ord1);
                   4373:       nd_init_ord(ord1);
                   4374:       nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   4375:       for ( t = fd0; t; t = NEXT(t) )
                   4376:         ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   4377:   }
                   4378:
1.39    ! noro     4379:   ndv_setup(m,0,fd0,nd_sba_dontsort,0,1);
1.28      noro     4380:   x = f4 ? nd_sba_f4(m,&perm) : nd_sba_buch(m,ishomo || homo,&perm);
1.24      noro     4381:   if ( !x ) {
                   4382:     *rp = 0; return;
                   4383:   }
                   4384:   if ( !ishomo && homo ) {
                   4385:        /* dehomogenization */
                   4386:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   4387:     nd_init_ord(ord);
                   4388:     nd_setup_parameters(nvar,0);
                   4389:   }
                   4390:   nd_demand = 0;
                   4391:   x = ndv_reducebase(x,perm);
                   4392:   x = ndv_reduceall(m,x);
                   4393:   nd_setup_parameters(nd_nvar,0);
1.29      noro     4394:   get_eg(&eg0);
1.24      noro     4395:   for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   4396:     NEXTNODE(r0,r);
                   4397:     if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
1.34      noro     4398:     else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.24      noro     4399:   }
                   4400:   if ( r0 ) NEXT(r) = 0;
                   4401:   MKLIST(*rp,r0);
1.29      noro     4402:   get_eg(&eg1); init_eg(&egconv); add_eg(&egconv,&eg0,&eg1);
                   4403:   print_eg("conv",&egconv); fprintf(asir_out,"\n");
1.24      noro     4404: }
                   4405:
1.1       noro     4406: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
                   4407: {
                   4408:     VL tv,fv,vv,vc,av;
                   4409:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4410:     int e,max,nvar,i;
                   4411:     NDV b;
                   4412:     int ishomo,nalg;
                   4413:     Alg alpha,dp;
                   4414:     P p,zp;
                   4415:     Q dmy;
                   4416:     LIST f1,f2;
                   4417:     Obj obj;
                   4418:     NumberField nf;
                   4419:     struct order_spec *ord1;
                   4420:     int *perm;
                   4421:
                   4422:     parse_nd_option(current_option);
                   4423:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4424:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4425:     switch ( ord->id ) {
                   4426:         case 1:
                   4427:             if ( ord->nv != nvar )
                   4428:                 error("nd_check : invalid order specification");
                   4429:             break;
                   4430:         default:
                   4431:             break;
                   4432:     }
                   4433:     nd_nalg = 0;
                   4434:     av = 0;
                   4435:     if ( !m ) {
                   4436:         get_algtree((Obj)f,&av);
                   4437:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4438:         nd_ntrans = nvar;
                   4439:         nd_nalg = nalg;
                   4440:         /* #i -> t#i */
                   4441:         if ( nalg ) {
                   4442:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4443:             ord = ord1;
                   4444:             f = f1;
                   4445:         }
                   4446:         nvar += nalg;
                   4447:     }
                   4448:     nd_init_ord(ord);
                   4449:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4450:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4451:             e = getdeg(tv->v,(P)BDY(t));
                   4452:             max = MAX(e,max);
                   4453:         }
                   4454:     nd_setup_parameters(nvar,max);
                   4455:     ishomo = 1;
                   4456:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4457:         ptozp((P)BDY(t),1,&dmy,&zp);
                   4458:         b = (pointer)ptondv(CO,vv,zp);
                   4459:         if ( ishomo )
                   4460:             ishomo = ishomo && ndv_ishomo(b);
                   4461:         if ( m ) ndv_mod(m,b);
                   4462:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4463:     }
                   4464:     if ( fd0 ) NEXT(fd) = 0;
1.24      noro     4465:     ndv_setup(m,0,fd0,0,1,0);
1.1       noro     4466:     for ( x = 0, i = 0; i < nd_psn; i++ )
                   4467:         x = update_base(x,i);
                   4468:     if ( do_check ) {
                   4469:         x = nd_gb(m,ishomo,1,0,&perm);
                   4470:         if ( !x ) {
                   4471:             *rp = 0;
                   4472:             return;
                   4473:         }
                   4474:     } else {
                   4475: #if 0
                   4476:     /* bug ? */
                   4477:         for ( t = x; t; t = NEXT(t) )
                   4478:             BDY(t) = (pointer)nd_ps[(long)BDY(t)];
                   4479: #else
                   4480:     conv_ilist(0,0,x,&perm);
                   4481: #endif
                   4482:     }
                   4483:     x = ndv_reducebase(x,perm);
                   4484:     x = ndv_reduceall(m,x);
                   4485:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   4486:         NEXTNODE(r0,r);
                   4487:         BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   4488:     }
                   4489:     if ( r0 ) NEXT(r) = 0;
                   4490:     if ( !m && nd_nalg )
                   4491:         r0 = postprocess_algcoef(av,alist,r0);
                   4492:     MKLIST(*rp,r0);
                   4493: }
                   4494:
                   4495: NDV recompute_trace(NODE trace,NDV *p,int m);
                   4496: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
                   4497:
                   4498: NDV recompute_trace(NODE ti,NDV *p,int mod)
                   4499: {
                   4500:   int c,c1,c2,i;
                   4501:   NM mul,m,tail;
                   4502:   ND d,r,rm;
                   4503:   NODE sj;
                   4504:   NDV red;
                   4505:   Obj mj;
                   4506:
                   4507:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   4508:   CM(mul) = 1;
                   4509:   tail = 0;
                   4510:   for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
                   4511:     sj = BDY((LIST)BDY(ti));
                   4512:     if ( ARG0(sj) ) {
1.6       noro     4513:       red = p[ZTOS((Q)ARG1(sj))];
1.1       noro     4514:       mj = (Obj)ARG2(sj);
                   4515:       if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
                   4516:       else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
                   4517:       rm = ndv_mul_nm(mod,mul,red);
                   4518:       if ( !r ) r = rm;
                   4519:       else {
                   4520:         for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
                   4521:           if ( d ) {
                   4522:             NEXT(tail) = m; tail = m; LEN(d)++;
                   4523:           } else {
                   4524:             MKND(nd_nvar,m,1,d); tail = BDY(d);
                   4525:           }
                   4526:         }
                   4527:         if ( !m ) return 0; /* failure */
                   4528:                 else {
                   4529:           BDY(r) = m;
                   4530:                     if ( mod > 0 || mod == -1 ) {
                   4531:             c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
                   4532:             DMAR(c1,c2,0,mod,c);
                   4533:             nd_mul_c(mod,rm,c);
                   4534:                     } else {
                   4535:                       Z t,u;
                   4536:
                   4537:                       chsgnlf(HCZ(r),&t);
                   4538:                       divlf(t,HCZ(rm),&u);
                   4539:                       nd_mul_c_lf(rm,u);
                   4540:                     }
                   4541:           r = nd_add(mod,r,rm);
                   4542:         }
                   4543:           }
                   4544:      }
                   4545:   }
                   4546:   if ( tail ) NEXT(tail) = 0;
                   4547:   d = nd_add(mod,d,r);
                   4548:   nd_mul_c(mod,d,invm(HCM(d),mod));
                   4549:   return ndtondv(mod,d);
                   4550: }
                   4551:
                   4552: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
                   4553: {
                   4554:     VL tv,fv,vv,vc,av;
                   4555:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4556:     int e,max,nvar,i;
                   4557:     NDV b;
                   4558:     int ishomo,nalg;
                   4559:     Alg alpha,dp;
                   4560:     P p,zp;
                   4561:     Q dmy;
                   4562:     LIST f1,f2;
                   4563:     Obj obj;
                   4564:     NumberField nf;
                   4565:     struct order_spec *ord1;
                   4566:   NODE permtrace,intred,ind,perm,trace,ti;
                   4567:   int len,n,j;
                   4568:   NDV *db,*pb;
                   4569:
                   4570:     parse_nd_option(current_option);
                   4571:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4572:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4573:     switch ( ord->id ) {
                   4574:         case 1:
                   4575:             if ( ord->nv != nvar )
                   4576:                 error("nd_check : invalid order specification");
                   4577:             break;
                   4578:         default:
                   4579:             break;
                   4580:     }
                   4581:     nd_init_ord(ord);
1.6       noro     4582:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     4583:     nd_setup_parameters(nvar,0);
                   4584:
                   4585:   len = length(BDY(f));
                   4586:   db = (NDV *)MALLOC(len*sizeof(NDV *));
                   4587:   for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
                   4588:       ptozp((P)BDY(t),1,&dmy,&zp);
                   4589:       b = ptondv(CO,vv,zp);
                   4590:         ndv_mod(m,b);
                   4591:     ndv_mul_c(m,b,invm(HCM(b),m));
                   4592:     db[i] = b;
                   4593:     }
                   4594:
                   4595:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   4596:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   4597:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   4598:   perm = BDY((LIST)ARG0(permtrace));
                   4599:   trace = NEXT(permtrace);
                   4600:
                   4601:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     4602:     j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1       noro     4603:     if ( j > i ) i = j;
                   4604:   }
                   4605:   n = i+1;
                   4606:   pb = (NDV *)MALLOC(n*sizeof(NDV *));
                   4607:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   4608:     ti = BDY((LIST)BDY(t));
1.6       noro     4609:     pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1       noro     4610:   }
                   4611:   for ( t = trace; t; t = NEXT(t) ) {
                   4612:     ti = BDY((LIST)BDY(t));
1.6       noro     4613:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   4614:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     4615:       if ( DP_Print ) {
                   4616:            fprintf(asir_out,"."); fflush(asir_out);
                   4617:       }
                   4618:   }
                   4619:   for ( t = intred; t; t = NEXT(t) ) {
                   4620:     ti = BDY((LIST)BDY(t));
1.6       noro     4621:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   4622:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     4623:       if ( DP_Print ) {
                   4624:            fprintf(asir_out,"*"); fflush(asir_out);
                   4625:       }
                   4626:   }
                   4627:     for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
                   4628:         NEXTNODE(r0,r);
1.6       noro     4629:     b = pb[ZTOS((Q)BDY(t))];
1.1       noro     4630:         ndv_mul_c(m,b,invm(HCM(b),m));
                   4631: #if 0
1.6       noro     4632:         BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1       noro     4633: #else
1.6       noro     4634:         BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1       noro     4635: #endif
                   4636:     }
                   4637:     if ( r0 ) NEXT(r) = 0;
                   4638:     MKLIST(*rp,r0);
                   4639:     if ( DP_Print ) fprintf(asir_out,"\n");
                   4640: }
                   4641:
1.16      noro     4642: 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     4643: {
                   4644:     VL tv,fv,vv,vc,av;
                   4645:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   4646:     int m,nocheck,nvar,mindex,e,max;
                   4647:     NDV c;
                   4648:     NMV a;
                   4649:     P p,zp;
                   4650:     Q dmy;
                   4651:     EPOS oepos;
                   4652:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   4653:     Alg alpha,dp;
                   4654:     P poly;
                   4655:     LIST f1,f2,zpl;
                   4656:     Obj obj;
                   4657:     NumberField nf;
                   4658:     struct order_spec *ord1;
                   4659:     struct oEGT eg_check,eg0,eg1;
                   4660:     NODE tr,tl1,tl2,tl3,tl4;
                   4661:     LIST l1,l2,l3,l4,l5;
                   4662:     int *perm;
                   4663:     int j,ret;
                   4664:     Z jq,bpe;
1.15      noro     4665:     VECT hvect;
1.1       noro     4666:
                   4667:     nd_module = 0;
                   4668:     nd_lf = 0;
                   4669:     parse_nd_option(current_option);
                   4670:     if ( nd_lf ) {
                   4671:       if ( f4 )
                   4672:         nd_f4_lf_trace(f,v,trace,homo,ord,rp);
                   4673:       else
                   4674:         error("nd_gr_trace is not implemented yet over a large finite field");
                   4675:       return;
                   4676:     }
                   4677:     if ( DP_Multiple )
                   4678:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4679:
                   4680:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4681:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4682:     switch ( ord->id ) {
                   4683:         case 1:
                   4684:             if ( ord->nv != nvar )
                   4685:                 error("nd_gr_trace : invalid order specification");
                   4686:             break;
                   4687:         default:
                   4688:             break;
                   4689:     }
                   4690:
                   4691:     get_algtree((Obj)f,&av);
                   4692:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4693:     nd_ntrans = nvar;
                   4694:     nd_nalg = nalg;
                   4695:     /* #i -> t#i */
                   4696:     if ( nalg ) {
                   4697:         preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4698:         ord = ord1;
                   4699:         f = f1;
                   4700:     }
                   4701:     nvar += nalg;
                   4702:
                   4703:     nocheck = 0;
                   4704:     mindex = 0;
                   4705:
                   4706:     if ( Demand ) nd_demand = 1;
                   4707:     else nd_demand = 0;
                   4708:
                   4709:     /* setup modulus */
                   4710:     if ( trace < 0 ) {
                   4711:         trace = -trace;
                   4712:         nocheck = 1;
                   4713:     }
                   4714:     m = trace > 1 ? trace : get_lprime(mindex);
                   4715:     nd_init_ord(ord);
                   4716:     mrank = 0;
                   4717:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4718:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4719:             if ( nd_module ) {
1.16      noro     4720:               if ( OID(BDY(t)) == O_DPM ) {
                   4721:                 e = dpm_getdeg((DPM)BDY(t),&trank);
                   4722:                 max = MAX(e,max);
                   4723:                 mrank = MAX(mrank,trank);
                   4724:               } else {
1.1       noro     4725:                 s = BDY((LIST)BDY(t));
                   4726:                 trank = length(s);
                   4727:                 mrank = MAX(mrank,trank);
                   4728:                 for ( ; s; s = NEXT(s) ) {
                   4729:                     e = getdeg(tv->v,(P)BDY(s));
                   4730:                     max = MAX(e,max);
                   4731:                 }
1.16      noro     4732:               }
1.1       noro     4733:             } else {
                   4734:                 e = getdeg(tv->v,(P)BDY(t));
                   4735:                 max = MAX(e,max);
                   4736:             }
                   4737:         }
                   4738:     nd_setup_parameters(nvar,max);
                   4739:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   4740:     ishomo = 1;
                   4741:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4742:         if ( nd_module ) {
1.16      noro     4743:           if ( OID(BDY(t)) == O_DPM ) {
                   4744:             Z cont;
                   4745:             DPM zdpm;
                   4746:
1.17      noro     4747:             if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16      noro     4748:             else zdpm = (DPM)BDY(t);
                   4749:             c = (pointer)dpmtondv(m,zdpm);
                   4750:           } else {
                   4751:             if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   4752:             else zpl = (LIST)BDY(t);
1.1       noro     4753:             c = (pointer)pltondv(CO,vv,zpl);
1.16      noro     4754:           }
1.1       noro     4755:         } else {
1.16      noro     4756:           if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   4757:           else zp = (P)BDY(t);
                   4758:           c = (pointer)ptondv(CO,vv,zp);
1.1       noro     4759:         }
                   4760:         if ( ishomo )
                   4761:             ishomo = ishomo && ndv_ishomo(c);
                   4762:         if ( c ) {
                   4763:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   4764:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   4765:         }
                   4766:     }
                   4767:     if ( in0 ) NEXT(in) = 0;
                   4768:     if ( fd0 ) NEXT(fd) = 0;
                   4769:     if ( !ishomo && homo ) {
                   4770:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   4771:             c = (NDV)BDY(t); len = LEN(c);
                   4772:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   4773:                 wmax = MAX(TD(DL(a)),wmax);
                   4774:         }
                   4775:         homogenize_order(ord,nvar,&ord1);
                   4776:         nd_init_ord(ord1);
                   4777:         nd_setup_parameters(nvar+1,wmax);
                   4778:         for ( t = fd0; t; t = NEXT(t) )
                   4779:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   4780:     }
                   4781:     if ( MaxDeg > 0 ) nocheck = 1;
                   4782:     while ( 1 ) {
                   4783:     tl1 = tl2 = tl3 = tl4 = 0;
                   4784:         if ( Demand )
                   4785:             nd_demand = 1;
1.24      noro     4786:         ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1       noro     4787:         if ( nd_gentrace ) {
                   4788:             MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   4789:         }
                   4790:         if ( ret )
                   4791:             cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
                   4792:         if ( !ret || !cand ) {
                   4793:             /* failure */
                   4794:             if ( trace > 1 ) { *rp = 0; return; }
                   4795:             else m = get_lprime(++mindex);
                   4796:             continue;
                   4797:         }
1.15      noro     4798:         if ( nd_gentrace ) {
                   4799:           MKVECT(hvect,nd_psn);
                   4800:           for ( i = 0; i < nd_psn; i++ )
                   4801:              ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   4802:         }
1.1       noro     4803:         if ( !ishomo && homo ) {
                   4804:             /* dehomogenization */
                   4805:             for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   4806:             nd_init_ord(ord);
                   4807:             nd_setup_parameters(nvar,0);
                   4808:         }
                   4809:         nd_demand = 0;
                   4810:         cand = ndv_reducebase(cand,perm);
                   4811:         if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
                   4812:         cand = ndv_reduceall(0,cand);
                   4813:         cbpe = nd_bpe;
                   4814:         if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
                   4815:         get_eg(&eg0);
                   4816:         if ( nocheck )
                   4817:             break;
                   4818:         if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   4819:             if ( nd_gentrace ) {
                   4820:           tl3 = nd_alltracelist; nd_alltracelist = 0;
                   4821:         } else tl3 = 0;
                   4822:             /* gbcheck : cand is a GB of Id(cand) ? */
                   4823:             if ( nd_vc || nd_gentrace || nd_gensyz )
                   4824:               ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
                   4825:             else
                   4826:               ret = nd_f4(0,1,0)!=0;
                   4827:             if ( nd_gentrace && nd_gensyz ) {
                   4828:           tl4 = nd_alltracelist; nd_alltracelist = 0;
                   4829:         } else tl4 = 0;
                   4830:     }
                   4831:     if ( ret ) break;
                   4832:         else if ( trace > 1 ) {
                   4833:             /* failure */
                   4834:             *rp = 0; return;
                   4835:         } else {
                   4836:             /* try the next modulus */
                   4837:             m = get_lprime(++mindex);
                   4838:             /* reset the parameters */
                   4839:             if ( !ishomo && homo ) {
                   4840:                 nd_init_ord(ord1);
                   4841:                 nd_setup_parameters(nvar+1,wmax);
                   4842:             } else {
                   4843:                 nd_init_ord(ord);
                   4844:                 nd_setup_parameters(nvar,max);
                   4845:             }
                   4846:         }
                   4847:     }
                   4848:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   4849:     if ( DP_Print )
1.6       noro     4850:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     4851:     /* dp->p */
                   4852:     nd_bpe = cbpe;
                   4853:     nd_setup_parameters(nd_nvar,0);
                   4854:     for ( r = cand; r; r = NEXT(r) ) {
1.16      noro     4855:       if ( nd_module ) {
1.17      noro     4856:         if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16      noro     4857:         else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17      noro     4858:       } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
                   4859:       else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1       noro     4860:     }
                   4861:     if ( nd_nalg )
                   4862:         cand = postprocess_algcoef(av,alist,cand);
                   4863:     MKLIST(*rp,cand);
                   4864:     if ( nd_gentrace ) {
                   4865:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   4866:     tl3 = reverse_node(tl3);
                   4867:     /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   4868:         for ( t = tl2; t; t = NEXT(t) ) {
                   4869:       /* s = [i,[*,j,*,*],...] */
                   4870:             s = BDY((LIST)BDY(t));
1.6       noro     4871:             j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     4872:       for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     4873:                 j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     4874:         ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   4875:             }
                   4876:     }
                   4877:     for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6       noro     4878:         STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     4879:     }
                   4880:         MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   4881:     MKLIST(l5,tl4);
1.6       noro     4882:       STOZ(nd_bpe,bpe);
1.15      noro     4883:         tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     4884:     }
                   4885: }
                   4886:
                   4887: /* XXX : module element is not considered  */
                   4888:
                   4889: void dltondl(int n,DL dl,UINT *r)
                   4890: {
                   4891:     UINT *d;
                   4892:     int i,j,l,s,ord_l;
                   4893:     struct order_pair *op;
                   4894:
                   4895:     d = (unsigned int *)dl->d;
                   4896:     for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
                   4897:     if ( nd_blockmask ) {
                   4898:         l = nd_blockmask->n;
                   4899:         op = nd_blockmask->order_pair;
                   4900:         for ( j = 0, s = 0; j < l; j++ ) {
                   4901:             ord_l = op[j].length;
                   4902:             for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
                   4903:         }
                   4904:         TD(r) = ndl_weight(r);
                   4905:         ndl_weight_mask(r);
                   4906:     } else {
                   4907:         for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
                   4908:         TD(r) = ndl_weight(r);
                   4909:     }
                   4910: }
                   4911:
                   4912: DL ndltodl(int n,UINT *ndl)
                   4913: {
                   4914:     DL dl;
                   4915:     int *d;
                   4916:     int i,j,l,s,ord_l;
                   4917:     struct order_pair *op;
                   4918:
1.33      noro     4919:     NEWDL_NOINIT(dl,n);
1.1       noro     4920:     dl->td = TD(ndl);
                   4921:     d = dl->d;
                   4922:     if ( nd_blockmask ) {
                   4923:         l = nd_blockmask->n;
                   4924:         op = nd_blockmask->order_pair;
                   4925:         for ( j = 0, s = 0; j < l; j++ ) {
                   4926:             ord_l = op[j].length;
                   4927:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   4928:         }
                   4929:     } else {
                   4930:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   4931:     }
                   4932:     return dl;
                   4933: }
                   4934:
1.24      noro     4935: void _ndltodl(UINT *ndl,DL dl)
                   4936: {
                   4937:     int *d;
                   4938:     int i,j,l,s,ord_l,n;
                   4939:     struct order_pair *op;
                   4940:
                   4941:     n = nd_nvar;
                   4942:     dl->td = TD(ndl);
                   4943:     d = dl->d;
                   4944:     if ( nd_blockmask ) {
                   4945:         l = nd_blockmask->n;
                   4946:         op = nd_blockmask->order_pair;
                   4947:         for ( j = 0, s = 0; j < l; j++ ) {
                   4948:             ord_l = op[j].length;
                   4949:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   4950:         }
                   4951:     } else {
                   4952:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   4953:     }
                   4954: }
                   4955:
1.1       noro     4956: void nmtodp(int mod,NM m,DP *r)
                   4957: {
                   4958:     DP dp;
                   4959:     MP mr;
                   4960:
                   4961:     NEWMP(mr);
                   4962:     mr->dl = ndltodl(nd_nvar,DL(m));
                   4963:     mr->c = (Obj)ndctop(mod,m->c);
                   4964:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   4965:     *r = dp;
                   4966: }
                   4967:
1.15      noro     4968: void ndltodp(UINT *d,DP *r)
                   4969: {
                   4970:     DP dp;
                   4971:     MP mr;
                   4972:
                   4973:     NEWMP(mr);
                   4974:     mr->dl = ndltodl(nd_nvar,d);
                   4975:     mr->c = (Obj)ONE;
                   4976:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   4977:     *r = dp;
                   4978: }
                   4979:
1.1       noro     4980: void ndl_print(UINT *dl)
                   4981: {
                   4982:     int n;
                   4983:     int i,j,l,ord_l,s,s0;
                   4984:     struct order_pair *op;
                   4985:
                   4986:     n = nd_nvar;
                   4987:     printf("<<");
                   4988:     if ( nd_blockmask ) {
                   4989:         l = nd_blockmask->n;
                   4990:         op = nd_blockmask->order_pair;
                   4991:         for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   4992:             ord_l = op[j].length;
                   4993:             for ( i = 0; i < ord_l; i++, s++ )
                   4994:                 printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
                   4995:         }
                   4996:     } else {
                   4997:         for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
                   4998:     }
                   4999:     printf(">>");
                   5000:     if ( nd_module && MPOS(dl) )
                   5001:         printf("*e%d",MPOS(dl));
                   5002: }
                   5003:
                   5004: void nd_print(ND p)
                   5005: {
                   5006:     NM m;
                   5007:
                   5008:     if ( !p )
                   5009:         printf("0\n");
                   5010:     else {
                   5011:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5012:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     5013:             else printf("+%ld*",CM(m));
1.1       noro     5014:             ndl_print(DL(m));
                   5015:         }
                   5016:         printf("\n");
                   5017:     }
                   5018: }
                   5019:
                   5020: void nd_print_q(ND p)
                   5021: {
                   5022:     NM m;
                   5023:
                   5024:     if ( !p )
                   5025:         printf("0\n");
                   5026:     else {
                   5027:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5028:             printf("+");
1.6       noro     5029:             printexpr(CO,(Obj)CZ(m));
1.1       noro     5030:             printf("*");
                   5031:             ndl_print(DL(m));
                   5032:         }
                   5033:         printf("\n");
                   5034:     }
                   5035: }
                   5036:
                   5037: void ndp_print(ND_pairs d)
                   5038: {
                   5039:     ND_pairs t;
                   5040:
                   5041:     for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
                   5042:     printf("\n");
                   5043: }
                   5044:
                   5045: void nd_removecont(int mod,ND p)
                   5046: {
                   5047:     int i,n;
                   5048:     Z *w;
                   5049:     NM m;
                   5050:     struct oVECT v;
                   5051:
                   5052:     if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
                   5053:     else if ( mod == -2 ) {
                   5054:       Z inv;
                   5055:       divlf(ONE,HCZ(p),&inv);
                   5056:       nd_mul_c_lf(p,inv);
                   5057:     } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
                   5058:     else {
                   5059:         for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   5060:         w = (Z *)MALLOC(n*sizeof(Q));
                   5061:         v.len = n;
                   5062:         v.body = (pointer *)w;
1.6       noro     5063:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5064:         removecont_array((P *)w,n,1);
1.6       noro     5065:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5066:     }
                   5067: }
                   5068:
                   5069: void nd_removecont2(ND p1,ND p2)
                   5070: {
                   5071:     int i,n1,n2,n;
                   5072:     Z *w;
                   5073:     NM m;
                   5074:     struct oVECT v;
                   5075:
                   5076:     n1 = nd_length(p1);
                   5077:     n2 = nd_length(p2);
                   5078:     n = n1+n2;
                   5079:     w = (Z *)MALLOC(n*sizeof(Q));
                   5080:     v.len = n;
                   5081:     v.body = (pointer *)w;
                   5082:     i = 0;
                   5083:     if ( p1 )
1.6       noro     5084:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5085:     if ( p2 )
1.6       noro     5086:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5087:     removecont_array((P *)w,n,1);
                   5088:     i = 0;
                   5089:     if ( p1 )
1.6       noro     5090:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5091:     if ( p2 )
1.6       noro     5092:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5093: }
                   5094:
                   5095: void ndv_removecont(int mod,NDV p)
                   5096: {
                   5097:     int i,len,all_p;
                   5098:     Z *c;
                   5099:     P *w;
                   5100:     Z dvr,t;
                   5101:     P g,cont,tp;
                   5102:     NMV m;
                   5103:
                   5104:     if ( mod == -1 )
                   5105:         ndv_mul_c(mod,p,_invsf(HCM(p)));
                   5106:     else if ( mod == -2 ) {
                   5107:       Z inv;
                   5108:       divlf(ONE,HCZ(p),&inv);
                   5109:       ndv_mul_c_lf(p,inv);
                   5110:     } else if ( mod )
                   5111:         ndv_mul_c(mod,p,invm(HCM(p),mod));
                   5112:     else {
                   5113:         len = p->len;
                   5114:         w = (P *)MALLOC(len*sizeof(P));
                   5115:         c = (Z *)MALLOC(len*sizeof(Q));
                   5116:         for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
                   5117:             ptozp(CP(m),1,(Q *)&c[i],&w[i]);
                   5118:             all_p = all_p && !NUM(w[i]);
                   5119:         }
                   5120:         if ( all_p ) {
                   5121:             qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
                   5122:             mulp(nd_vc,(P)dvr,g,&cont);
                   5123:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5124:                 divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
                   5125:             }
                   5126:         } else {
                   5127:             sortbynm((Q *)c,len);
                   5128:             qltozl((Q *)c,len,&dvr);
                   5129:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5130:                 divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
                   5131:             }
                   5132:         }
                   5133:     }
                   5134: }
                   5135:
                   5136: /* koko */
                   5137:
                   5138: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
                   5139: {
                   5140:     int len,i,max;
                   5141:     NMV m,mr0,mr,t;
                   5142:
                   5143:     len = p->len;
1.14      noro     5144:     for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1       noro     5145:         max = MAX(max,TD(DL(m)));
                   5146:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   5147:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   5148:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   5149:     t = (NMV)MALLOC(nmv_adv);
                   5150:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   5151:         ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6       noro     5152:         CZ(mr) = CZ(m);
1.1       noro     5153:         ndl_copy(DL(t),DL(mr));
                   5154:     }
                   5155:     NV(p)++;
                   5156:     BDY(p) = mr0;
                   5157: }
                   5158:
                   5159: void ndv_dehomogenize(NDV p,struct order_spec *ord)
                   5160: {
                   5161:     int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
                   5162:     int pos;
                   5163:     Q *w;
                   5164:     Q dvr,t;
                   5165:     NMV m,r;
                   5166:
                   5167:     len = p->len;
                   5168:     newnvar = nd_nvar-1;
                   5169:     newexporigin = nd_get_exporigin(ord);
                   5170:     if ( nd_module ) newmpos = newexporigin-1;
                   5171:     newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
                   5172:     for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
                   5173:         ndl_dehomogenize(DL(m));
                   5174:     if ( newwpd != nd_wpd ) {
                   5175:         newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
                   5176:         for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6       noro     5177:             CZ(r) = CZ(m);
1.1       noro     5178:             if ( nd_module ) pos = MPOS(DL(m));
                   5179:             for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   5180:             adj = nd_exporigin-newexporigin;
                   5181:             for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
                   5182:             if ( nd_module ) {
                   5183:                 DL(r)[newmpos] = pos;
                   5184:             }
                   5185:         }
                   5186:     }
                   5187:     NV(p)--;
                   5188: }
                   5189:
                   5190: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
                   5191: {
                   5192:     int i;
                   5193:     P *tpl,*tpl1;
                   5194:     NODE l;
                   5195:     P h,gcd,t;
                   5196:
                   5197:     tpl = (P *)MALLOC(m*sizeof(P));
                   5198:     tpl1 = (P *)MALLOC(m*sizeof(P));
                   5199:     bcopy(pl,tpl,m*sizeof(P));
                   5200:     gcd = (P)ONE;
                   5201:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   5202:         h = (P)BDY(l);
                   5203:         while ( 1 ) {
                   5204:             for ( i = 0; i < m; i++ )
                   5205:                 if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
                   5206:                     break;
                   5207:             if ( i == m ) {
                   5208:                 bcopy(tpl1,tpl,m*sizeof(P));
                   5209:                 mulp(vl,gcd,h,&t); gcd = t;
                   5210:             } else
                   5211:                 break;
                   5212:         }
                   5213:     }
                   5214:     if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
                   5215:     if ( full ) {
                   5216:         heu_nezgcdnpz(vl,tpl,m,&t);
                   5217:         mulp(vl,gcd,t,pr);
                   5218:     } else
                   5219:         *pr = gcd;
                   5220: }
                   5221:
                   5222: void removecont_array(P *p,int n,int full)
                   5223: {
                   5224:     int all_p,all_q,i;
                   5225:     Z *c;
                   5226:     P *w;
                   5227:     P t,s;
                   5228:
                   5229:     for ( all_q = 1, i = 0; i < n; i++ )
                   5230:         all_q = all_q && NUM(p[i]);
                   5231:     if ( all_q ) {
                   5232:         removecont_array_q((Z *)p,n);
                   5233:     } else {
                   5234:         c = (Z *)MALLOC(n*sizeof(Z));
                   5235:         w = (P *)MALLOC(n*sizeof(P));
                   5236:         for ( i = 0; i < n; i++ ) {
                   5237:             ptozp(p[i],1,(Q *)&c[i],&w[i]);
                   5238:         }
                   5239:         removecont_array_q(c,n);
                   5240:         nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
                   5241:         for ( i = 0; i < n; i++ ) {
                   5242:             divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
                   5243:         }
                   5244:     }
                   5245: }
                   5246:
                   5247: /* c is an int array */
                   5248:
                   5249: void removecont_array_q(Z *c,int n)
                   5250: {
                   5251:   struct oVECT v;
                   5252:   Z d0,d1,a,u,u1,gcd;
                   5253:   int i,j;
                   5254:   Z *q,*r;
                   5255:
                   5256:   q = (Z *)MALLOC(n*sizeof(Z));
                   5257:   r = (Z *)MALLOC(n*sizeof(Z));
                   5258:   v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   5259:   gcdvz_estimate(&v,&d0);
                   5260:   for ( i = 0; i < n; i++ ) {
                   5261:     divqrz(c[i],d0,&q[i],&r[i]);
                   5262:   }
                   5263:   for ( i = 0; i < n; i++ ) if ( r[i] ) break;
                   5264:   if ( i < n ) {
                   5265:     v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   5266:     gcdvz(&v,&d1);
                   5267:     gcdz(d0,d1,&gcd);
1.6       noro     5268:     /* exact division */
                   5269:     divsz(d0,gcd,&a);
1.1       noro     5270:     for ( i = 0; i < n; i++ ) {
                   5271:       mulz(a,q[i],&u);
                   5272:       if ( r[i] ) {
1.6       noro     5273:         /* exact division */
                   5274:         divsz(r[i],gcd,&u1);
1.1       noro     5275:         addz(u,u1,&q[i]);
                   5276:       } else
                   5277:         q[i] = u;
                   5278:     }
                   5279:   }
                   5280:   for ( i = 0; i < n; i++ ) c[i] = q[i];
                   5281: }
                   5282:
1.4       noro     5283: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
                   5284:
                   5285: void mpz_removecont_array(mpz_t *c,int n)
                   5286: {
                   5287:   mpz_t d0,a,u,u1,gcd;
                   5288:   int i,j;
1.13      noro     5289:   static mpz_t *q,*r;
                   5290:   static int c_len = 0;
1.4       noro     5291:
                   5292:   for ( i = 0; i < n; i++ )
                   5293:     if ( mpz_sgn(c[i]) ) break;
                   5294:   if ( i == n ) return;
                   5295:   gcdv_mpz_estimate(d0,c,n);
1.13      noro     5296:   if ( n > c_len ) {
                   5297:     q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   5298:     r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   5299:     c_len = n;
                   5300:   }
1.4       noro     5301:   for ( i = 0; i < n; i++ ) {
                   5302:     mpz_init(q[i]); mpz_init(r[i]);
                   5303:     mpz_fdiv_qr(q[i],r[i],c[i],d0);
                   5304:   }
                   5305:   for ( i = 0; i < n; i++ )
                   5306:     if ( mpz_sgn(r[i]) ) break;
                   5307:   mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
                   5308:   if ( i < n ) {
                   5309:     mpz_gcd(gcd,d0,r[i]);
                   5310:     for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
                   5311:     mpz_div(a,d0,gcd);
                   5312:     for ( i = 0; i < n; i++ ) {
                   5313:       mpz_mul(u,a,q[i]);
                   5314:       if ( mpz_sgn(r[i]) ) {
                   5315:         mpz_div(u1,r[i],gcd);
                   5316:         mpz_add(q[i],u,u1);
                   5317:       } else
                   5318:         mpz_set(q[i],u);
                   5319:     }
                   5320:   }
                   5321:   for ( i = 0; i < n; i++ )
                   5322:     mpz_set(c[i],q[i]);
                   5323: }
                   5324:
1.1       noro     5325: void nd_mul_c(int mod,ND p,int mul)
                   5326: {
                   5327:     NM m;
                   5328:     int c,c1;
                   5329:
                   5330:     if ( !p ) return;
                   5331:     if ( mul == 1 ) return;
                   5332:     if ( mod == -1 )
                   5333:         for ( m = BDY(p); m; m = NEXT(m) )
                   5334:             CM(m) = _mulsf(CM(m),mul);
                   5335:     else
                   5336:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5337:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   5338:         }
                   5339: }
                   5340:
                   5341: void nd_mul_c_lf(ND p,Z mul)
                   5342: {
                   5343:     NM m;
                   5344:     Z c;
                   5345:
                   5346:     if ( !p ) return;
                   5347:     if ( UNIZ(mul) ) return;
                   5348:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   5349:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   5350:     }
                   5351: }
                   5352:
                   5353: void nd_mul_c_q(ND p,P mul)
                   5354: {
                   5355:     NM m;
                   5356:     P c;
                   5357:
                   5358:     if ( !p ) return;
                   5359:     if ( UNIQ(mul) ) return;
                   5360:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   5361:         mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
                   5362:     }
                   5363: }
                   5364:
                   5365: void nd_mul_c_p(VL vl,ND p,P mul)
                   5366: {
                   5367:     NM m;
                   5368:     P c;
                   5369:
                   5370:     if ( !p ) return;
                   5371:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   5372:         mulp(vl,CP(m),mul,&c); CP(m) = c;
                   5373:     }
                   5374: }
                   5375:
                   5376: void nd_free(ND p)
                   5377: {
                   5378:     NM t,s;
                   5379:
                   5380:     if ( !p ) return;
                   5381:     t = BDY(p);
                   5382:     while ( t ) {
                   5383:         s = NEXT(t);
                   5384:         FREENM(t);
                   5385:         t = s;
                   5386:     }
                   5387:     FREEND(p);
                   5388: }
                   5389:
                   5390: void ndv_free(NDV p)
                   5391: {
                   5392:     GCFREE(BDY(p));
                   5393: }
                   5394:
                   5395: void nd_append_red(UINT *d,int i)
                   5396: {
                   5397:     RHist m,m0;
                   5398:     int h;
                   5399:
                   5400:     NEWRHist(m);
                   5401:     h = ndl_hash_value(d);
                   5402:     m->index = i;
                   5403:     ndl_copy(d,DL(m));
                   5404:     NEXT(m) = nd_red[h];
                   5405:     nd_red[h] = m;
                   5406: }
                   5407:
                   5408: UINT *ndv_compute_bound(NDV p)
                   5409: {
                   5410:     UINT *d1,*d2,*t;
                   5411:     UINT u;
                   5412:     int i,j,k,l,len,ind;
                   5413:     NMV m;
                   5414:
                   5415:     if ( !p )
                   5416:         return 0;
                   5417:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5418:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5419:     len = LEN(p);
                   5420:     m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   5421:     for ( i = 1; i < len; i++, NMV_ADV(m) ) {
                   5422:         ndl_max(DL(m),d1,d2);
                   5423:         t = d1; d1 = d2; d2 = t;
                   5424:     }
                   5425:     l = nd_nvar+31;
                   5426:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   5427:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   5428:         u = d1[i];
                   5429:         k = (nd_epw-1)*nd_bpe;
                   5430:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   5431:             t[ind] = (u>>k)&nd_mask0;
                   5432:     }
                   5433:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   5434:     return t;
                   5435: }
                   5436:
                   5437: UINT *nd_compute_bound(ND p)
                   5438: {
                   5439:     UINT *d1,*d2,*t;
                   5440:     UINT u;
                   5441:     int i,j,k,l,len,ind;
                   5442:     NM m;
                   5443:
                   5444:     if ( !p )
                   5445:         return 0;
                   5446:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5447:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5448:     len = LEN(p);
                   5449:     m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   5450:     for ( m = NEXT(m); m; m = NEXT(m) ) {
                   5451:         ndl_lcm(DL(m),d1,d2);
                   5452:         t = d1; d1 = d2; d2 = t;
                   5453:     }
                   5454:     l = nd_nvar+31;
                   5455:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   5456:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   5457:         u = d1[i];
                   5458:         k = (nd_epw-1)*nd_bpe;
                   5459:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   5460:             t[ind] = (u>>k)&nd_mask0;
                   5461:     }
                   5462:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   5463:     return t;
                   5464: }
                   5465:
                   5466: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
                   5467: /* of a term. In this case we need additional 1 word. */
                   5468:
                   5469: int nd_get_exporigin(struct order_spec *ord)
                   5470: {
                   5471:     switch ( ord->id ) {
1.21      noro     5472:         case 0: case 2: case 256: case 258: case 300:
1.1       noro     5473:             return 1+nd_module;
                   5474:         case 1: case 257:
                   5475:             /* block order */
                   5476:             /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
                   5477:             /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
                   5478:             return ord->ord.block.length+1+nd_module;
                   5479:         case 3: case 259:
                   5480: #if 0
                   5481:             error("nd_get_exporigin : composite order is not supported yet.");
                   5482: #else
                   5483:             return 1+nd_module;
                   5484: #endif
                   5485:         default:
                   5486:             error("nd_get_exporigin : ivalid argument.");
                   5487:             return 0;
                   5488:     }
                   5489: }
                   5490:
                   5491: void nd_setup_parameters(int nvar,int max) {
                   5492:     int i,j,n,elen,ord_o,ord_l,l,s,wpd;
                   5493:     struct order_pair *op;
                   5494:
                   5495:     nd_nvar = nvar;
                   5496:     if ( max ) {
                   5497:         /* XXX */
                   5498:         if ( do_weyl ) nd_bpe = 32;
                   5499:         else if ( max < 2 ) nd_bpe = 1;
                   5500:         else if ( max < 4 ) nd_bpe = 2;
                   5501:         else if ( max < 8 ) nd_bpe = 3;
                   5502:         else if ( max < 16 ) nd_bpe = 4;
                   5503:         else if ( max < 32 ) nd_bpe = 5;
                   5504:         else if ( max < 64 ) nd_bpe = 6;
                   5505:         else if ( max < 256 ) nd_bpe = 8;
                   5506:         else if ( max < 1024 ) nd_bpe = 10;
                   5507:         else if ( max < 65536 ) nd_bpe = 16;
                   5508:         else nd_bpe = 32;
                   5509:     }
                   5510:     if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
                   5511:         UINT t;
                   5512:     int st;
                   5513:         int *v;
                   5514:   /* t = max(weights) */
                   5515:         t = 0;
                   5516:         if ( current_dl_weight_vector )
                   5517:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                   5518:                 if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
                   5519:                 if ( t < st ) t = st;
                   5520:             }
                   5521:         if ( nd_matrix )
                   5522:             for ( i = 0; i < nd_matrix_len; i++ )
                   5523:                 for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
                   5524:                     if ( (st=v[j]) < 0 ) st = -st;
                   5525:                     if ( t < st ) t = st;
                   5526:         }
                   5527:         /* i = bitsize of t */
                   5528:         for ( i = 0; t; t >>=1, i++ );
                   5529:         /* i += bitsize of nd_nvar */
                   5530:         for ( t = nd_nvar; t; t >>=1, i++);
                   5531:         /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
                   5532:         if ( (nd_bpe+i) >= 31 )
                   5533:             error("nd_setup_parameters : too large weight");
                   5534:     }
                   5535:     nd_epw = (sizeof(UINT)*8)/nd_bpe;
                   5536:     elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   5537:     nd_exporigin = nd_get_exporigin(nd_ord);
                   5538:     wpd = nd_exporigin+elen;
                   5539:     if ( nd_module )
                   5540:         nd_mpos = nd_exporigin-1;
                   5541:     else
                   5542:         nd_mpos = -1;
                   5543:     if ( wpd != nd_wpd ) {
                   5544:         nd_free_private_storage();
                   5545:         nd_wpd = wpd;
                   5546:     }
                   5547:     if ( nd_bpe < 32 ) {
                   5548:         nd_mask0 = (1<<nd_bpe)-1;
                   5549:     } else {
                   5550:         nd_mask0 = 0xffffffff;
                   5551:     }
                   5552:     bzero(nd_mask,sizeof(nd_mask));
                   5553:     nd_mask1 = 0;
                   5554:     for ( i = 0; i < nd_epw; i++ ) {
                   5555:         nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   5556:         nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   5557:     }
                   5558:     nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
                   5559:     nd_epos = nd_create_epos(nd_ord);
                   5560:     nd_blockmask = nd_create_blockmask(nd_ord);
                   5561:     nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
                   5562: }
                   5563:
                   5564: ND_pairs nd_reconstruct(int trace,ND_pairs d)
                   5565: {
                   5566:     int i,obpe,oadv,h;
                   5567:     static NM prev_nm_free_list;
                   5568:     static ND_pairs prev_ndp_free_list;
                   5569:     RHist mr0,mr;
                   5570:     RHist r;
                   5571:     RHist *old_red;
                   5572:     ND_pairs s0,s,t;
                   5573:     EPOS oepos;
                   5574:
                   5575:     obpe = nd_bpe;
                   5576:     oadv = nmv_adv;
                   5577:     oepos = nd_epos;
                   5578:     if ( obpe < 2 ) nd_bpe = 2;
                   5579:     else if ( obpe < 3 ) nd_bpe = 3;
                   5580:     else if ( obpe < 4 ) nd_bpe = 4;
                   5581:     else if ( obpe < 5 ) nd_bpe = 5;
                   5582:     else if ( obpe < 6 ) nd_bpe = 6;
                   5583:     else if ( obpe < 8 ) nd_bpe = 8;
                   5584:     else if ( obpe < 10 ) nd_bpe = 10;
                   5585:     else if ( obpe < 16 ) nd_bpe = 16;
                   5586:     else if ( obpe < 32 ) nd_bpe = 32;
                   5587:     else error("nd_reconstruct : exponent too large");
                   5588:
                   5589:     nd_setup_parameters(nd_nvar,0);
                   5590:     prev_nm_free_list = _nm_free_list;
                   5591:     prev_ndp_free_list = _ndp_free_list;
                   5592:     _nm_free_list = 0;
                   5593:     _ndp_free_list = 0;
                   5594:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   5595:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   5596:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   5597:     }
                   5598:     if ( trace )
                   5599:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   5600:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   5601:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   5602:         }
                   5603:     s0 = 0;
                   5604:     for ( t = d; t; t = NEXT(t) ) {
                   5605:         NEXTND_pairs(s0,s);
                   5606:         s->i1 = t->i1;
                   5607:         s->i2 = t->i2;
1.24      noro     5608:         s->sig = t->sig;
1.1       noro     5609:         SG(s) = SG(t);
                   5610:         ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   5611:     }
                   5612:
                   5613:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   5614:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   5615:         old_red[i] = nd_red[i];
                   5616:         nd_red[i] = 0;
                   5617:     }
                   5618:     for ( i = 0; i < REDTAB_LEN; i++ )
                   5619:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   5620:             NEWRHist(mr);
                   5621:             mr->index = r->index;
                   5622:             SG(mr) = SG(r);
                   5623:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   5624:             h = ndl_hash_value(DL(mr));
                   5625:             NEXT(mr) = nd_red[h];
                   5626:             nd_red[h] = mr;
1.24      noro     5627:             mr->sig = r->sig;
1.1       noro     5628:         }
                   5629:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   5630:     old_red = 0;
                   5631:     for ( i = 0; i < nd_psn; i++ ) {
                   5632:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   5633:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24      noro     5634:         r->sig = nd_psh[i]->sig;
1.1       noro     5635:         nd_psh[i] = r;
                   5636:     }
                   5637:     if ( s0 ) NEXT(s) = 0;
                   5638:     prev_nm_free_list = 0;
                   5639:     prev_ndp_free_list = 0;
                   5640: #if 0
                   5641:     GC_gcollect();
                   5642: #endif
                   5643:     return s0;
                   5644: }
                   5645:
1.30      noro     5646: void nd_reconstruct_s(int trace,ND_pairs *d)
                   5647: {
                   5648:     int i,obpe,oadv,h;
                   5649:     static NM prev_nm_free_list;
                   5650:     static ND_pairs prev_ndp_free_list;
                   5651:     RHist mr0,mr;
                   5652:     RHist r;
                   5653:     RHist *old_red;
                   5654:     ND_pairs s0,s,t;
                   5655:     EPOS oepos;
                   5656:
                   5657:     obpe = nd_bpe;
                   5658:     oadv = nmv_adv;
                   5659:     oepos = nd_epos;
                   5660:     if ( obpe < 2 ) nd_bpe = 2;
                   5661:     else if ( obpe < 3 ) nd_bpe = 3;
                   5662:     else if ( obpe < 4 ) nd_bpe = 4;
                   5663:     else if ( obpe < 5 ) nd_bpe = 5;
                   5664:     else if ( obpe < 6 ) nd_bpe = 6;
                   5665:     else if ( obpe < 8 ) nd_bpe = 8;
                   5666:     else if ( obpe < 10 ) nd_bpe = 10;
                   5667:     else if ( obpe < 16 ) nd_bpe = 16;
                   5668:     else if ( obpe < 32 ) nd_bpe = 32;
                   5669:     else error("nd_reconstruct_s : exponent too large");
                   5670:
                   5671:     nd_setup_parameters(nd_nvar,0);
                   5672:     prev_nm_free_list = _nm_free_list;
                   5673:     prev_ndp_free_list = _ndp_free_list;
                   5674:     _nm_free_list = 0;
                   5675:     _ndp_free_list = 0;
                   5676:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   5677:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   5678:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   5679:     }
                   5680:     if ( trace )
                   5681:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   5682:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   5683:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   5684:         }
                   5685:
                   5686:     for ( i = 0; i < nd_nbase; i++ ) {
                   5687:       s0 = 0;
                   5688:       for ( t = d[i]; t; t = NEXT(t) ) {
                   5689:           NEXTND_pairs(s0,s);
                   5690:           s->i1 = t->i1;
                   5691:           s->i2 = t->i2;
                   5692:           s->sig = t->sig;
                   5693:           SG(s) = SG(t);
                   5694:           ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   5695:       }
                   5696:       d[i] = s0;
                   5697:     }
                   5698:
                   5699:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   5700:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   5701:         old_red[i] = nd_red[i];
                   5702:         nd_red[i] = 0;
                   5703:     }
                   5704:     for ( i = 0; i < REDTAB_LEN; i++ )
                   5705:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   5706:             NEWRHist(mr);
                   5707:             mr->index = r->index;
                   5708:             SG(mr) = SG(r);
                   5709:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   5710:             h = ndl_hash_value(DL(mr));
                   5711:             NEXT(mr) = nd_red[h];
                   5712:             nd_red[h] = mr;
                   5713:             mr->sig = r->sig;
                   5714:         }
                   5715:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   5716:     old_red = 0;
                   5717:     for ( i = 0; i < nd_psn; i++ ) {
                   5718:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   5719:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
                   5720:         r->sig = nd_psh[i]->sig;
                   5721:         nd_psh[i] = r;
                   5722:     }
                   5723:     if ( s0 ) NEXT(s) = 0;
                   5724:     prev_nm_free_list = 0;
                   5725:     prev_ndp_free_list = 0;
                   5726: #if 0
                   5727:     GC_gcollect();
                   5728: #endif
                   5729: }
                   5730:
1.1       noro     5731: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
                   5732: {
                   5733:     int n,i,ei,oepw,omask0,j,s,ord_l,l;
                   5734:     struct order_pair *op;
                   5735:
                   5736:     n = nd_nvar;
                   5737:     oepw = (sizeof(UINT)*8)/obpe;
                   5738:     omask0 = (1<<obpe)-1;
                   5739:     TD(r) = TD(d);
                   5740:     for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
                   5741:     if ( nd_blockmask ) {
                   5742:         l = nd_blockmask->n;
                   5743:         op = nd_blockmask->order_pair;
                   5744:         for ( i = 1; i < nd_exporigin; i++ )
                   5745:             r[i] = d[i];
                   5746:         for ( j = 0, s = 0; j < l; j++ ) {
                   5747:             ord_l = op[j].length;
                   5748:             for ( i = 0; i < ord_l; i++, s++ ) {
                   5749:                 ei =  GET_EXP_OLD(d,s);
                   5750:                 PUT_EXP(r,s,ei);
                   5751:             }
                   5752:         }
                   5753:     } else {
                   5754:         for ( i = 0; i < n; i++ ) {
                   5755:             ei = GET_EXP_OLD(d,i);
                   5756:             PUT_EXP(r,i,ei);
                   5757:         }
                   5758:     }
                   5759:     if ( nd_module ) MPOS(r) = MPOS(d);
                   5760: }
                   5761:
                   5762: ND nd_copy(ND p)
                   5763: {
                   5764:     NM m,mr,mr0;
                   5765:     int c,n;
                   5766:     ND r;
                   5767:
                   5768:     if ( !p )
                   5769:         return 0;
                   5770:     else {
                   5771:         for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   5772:             NEXTNM(mr0,mr);
                   5773:             CM(mr) = CM(m);
                   5774:             ndl_copy(DL(m),DL(mr));
                   5775:         }
                   5776:         NEXT(mr) = 0;
                   5777:         MKND(NV(p),mr0,LEN(p),r);
                   5778:         SG(r) = SG(p);
                   5779:         return r;
                   5780:     }
                   5781: }
                   5782:
                   5783: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
                   5784: {
                   5785:     NM m1,m2;
                   5786:     NDV p1,p2;
                   5787:     ND t1,t2;
                   5788:     UINT *lcm;
                   5789:     P gp,tp;
                   5790:     Z g,t;
                   5791:     Z iq;
                   5792:     int td;
                   5793:     LIST hist;
                   5794:     NODE node;
                   5795:     DP d;
                   5796:
                   5797:     if ( !mod && nd_demand ) {
                   5798:         p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
                   5799:     } else {
                   5800:         if ( trace ) {
                   5801:             p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   5802:         } else {
                   5803:             p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   5804:         }
                   5805:     }
                   5806:     lcm = LCM(p);
                   5807:     NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
                   5808:     if ( ndl_check_bound2(p->i1,DL(m1)) ) {
                   5809:         FREENM(m1); return 0;
                   5810:     }
                   5811:     NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
                   5812:     if ( ndl_check_bound2(p->i2,DL(m2)) ) {
                   5813:         FREENM(m1); FREENM(m2); return 0;
                   5814:     }
                   5815:
                   5816:     if ( mod == -1 ) {
                   5817:         CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
                   5818:     } else if ( mod > 0 ) {
                   5819:         CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
                   5820:     } else if ( mod == -2 ) {
                   5821:         CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
                   5822:     } else if ( nd_vc ) {
                   5823:         ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
                   5824:         divsp(nd_vc,HCP(p2),gp,&CP(m1));
                   5825:         divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
                   5826:     } else {
1.6       noro     5827:         igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1       noro     5828:     }
                   5829:     t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
                   5830:     *rp = nd_add(mod,t1,t2);
                   5831:     if ( nd_gentrace ) {
                   5832:         /* nd_tracelist is initialized */
1.6       noro     5833:         STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     5834:         MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6       noro     5835:         STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     5836:         MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
                   5837:         nd_tracelist = node;
                   5838:     }
1.24      noro     5839:     if ( *rp )
                   5840:       (*rp)->sig = p->sig;
1.1       noro     5841:     FREENM(m1); FREENM(m2);
                   5842:     return 1;
                   5843: }
                   5844:
                   5845: void ndv_mul_c(int mod,NDV p,int mul)
                   5846: {
                   5847:     NMV m;
                   5848:     int c,c1,len,i;
                   5849:
                   5850:     if ( !p ) return;
                   5851:     len = LEN(p);
                   5852:     if ( mod == -1 )
                   5853:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   5854:             CM(m) = _mulsf(CM(m),mul);
                   5855:     else
                   5856:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   5857:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   5858:         }
                   5859: }
                   5860:
                   5861: void ndv_mul_c_lf(NDV p,Z mul)
                   5862: {
                   5863:     NMV m;
                   5864:     Z c;
                   5865:     int len,i;
                   5866:
                   5867:     if ( !p ) return;
                   5868:     len = LEN(p);
                   5869:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   5870:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   5871:     }
                   5872: }
                   5873:
                   5874: /* for nd_det */
                   5875: void ndv_mul_c_q(NDV p,Z mul)
                   5876: {
                   5877:     NMV m;
                   5878:     Z c;
                   5879:     int len,i;
                   5880:
                   5881:     if ( !p ) return;
                   5882:     len = LEN(p);
                   5883:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     5884:         mulz(CZ(m),mul,&c); CZ(m) = c;
1.1       noro     5885:     }
                   5886: }
                   5887:
                   5888: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
                   5889:     int n2,i,j,l,n,tlen;
                   5890:     UINT *d0;
                   5891:     NM *tab,*psum;
                   5892:     ND s,r;
                   5893:     NM t;
                   5894:     NMV m1;
                   5895:
                   5896:     if ( !p ) return 0;
                   5897:     n = NV(p); n2 = n>>1;
                   5898:     d0 = DL(m0);
                   5899:     l = LEN(p);
                   5900:     for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   5901:     tab = (NM *)MALLOC(tlen*sizeof(NM));
                   5902:     psum = (NM *)MALLOC(tlen*sizeof(NM));
                   5903:     for ( i = 0; i < tlen; i++ ) psum[i] = 0;
                   5904:     m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   5905:     for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
                   5906:         /* m0(NM) * m1(NMV) => tab(NM) */
                   5907:         weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
                   5908:         for ( j = 0; j < tlen; j++ ) {
                   5909:             if ( tab[j] ) {
                   5910:                 NEXT(tab[j]) = psum[j];    psum[j] = tab[j];
                   5911:             }
                   5912:         }
                   5913:     }
                   5914:     for ( i = tlen-1, r = 0; i >= 0; i-- )
                   5915:         if ( psum[i] ) {
                   5916:             for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   5917:             MKND(n,psum[i],j,s);
                   5918:             r = nd_add(mod,r,s);
                   5919:         }
                   5920:     if ( r ) SG(r) = SG(p)+TD(d0);
                   5921:     return r;
                   5922: }
                   5923:
                   5924: /* product of monomials */
                   5925: /* XXX block order is not handled correctly */
                   5926:
                   5927: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   5928: {
                   5929:     int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
                   5930:     UINT *d0,*d1,*d,*dt,*ctab;
                   5931:     Z *ctab_q;
                   5932:     Z q,q1;
                   5933:     UINT c0,c1,c;
                   5934:     NM *p;
                   5935:     NM m,t;
                   5936:     int mpos;
                   5937:
                   5938:     for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   5939:     if ( !m0 || !m1 ) return;
                   5940:     d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
                   5941:     if ( nd_module )
                   5942:         if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
                   5943:
                   5944:     NEWNM(m); d = DL(m);
                   5945:     if ( mod ) {
                   5946:         c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
                   5947:     } else if ( nd_vc )
                   5948:         mulp(nd_vc,CP(m0),CP(m1),&CP(m));
                   5949:   else
1.6       noro     5950:         mulz(CZ(m0),CZ(m1),&CZ(m));
1.1       noro     5951:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   5952:     homo = n&1 ? 1 : 0;
                   5953:     if ( homo ) {
                   5954:         /* offset of h-degree */
                   5955:         h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   5956:         PUT_EXP(DL(m),n-1,h);
                   5957:         TD(DL(m)) = h;
                   5958:         if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   5959:     }
                   5960:     tab[0] = m;
                   5961:     NEWNM(m); d = DL(m);
                   5962:     for ( i = 0, curlen = 1; i < n2; i++ ) {
                   5963:         a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   5964:         k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   5965:         /* xi^a*(Di^k*xi^l)*Di^b */
                   5966:         a += l; b += k;
                   5967:         s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
                   5968:         if ( !k || !l ) {
                   5969:             for ( j = 0; j < curlen; j++ )
                   5970:                 if ( (t = tab[j]) != 0 ) {
                   5971:                     dt = DL(t);
                   5972:                     PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
                   5973:                     if ( nd_blockmask ) ndl_weight_mask(dt);
                   5974:                 }
                   5975:             curlen *= k+1;
                   5976:             continue;
                   5977:         }
                   5978:         min = MIN(k,l);
                   5979:         if ( mod ) {
                   5980:             ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
                   5981:             mkwcm(k,l,mod,(int *)ctab);
                   5982:         } else {
                   5983:             ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
                   5984:             mkwc(k,l,ctab_q);
                   5985:         }
                   5986:         for ( j = min; j >= 0; j-- ) {
                   5987:             for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
                   5988:             PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
                   5989:             h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
                   5990:             if ( homo ) {
                   5991:                 TD(d) = s;
                   5992:                 PUT_EXP(d,n-1,s-h);
                   5993:             } else TD(d) = h;
                   5994:             if ( nd_blockmask ) ndl_weight_mask(d);
                   5995:             if ( mod ) c = ctab[j];
                   5996:             else q = ctab_q[j];
                   5997:             p = tab+curlen*j;
                   5998:             if ( j == 0 ) {
                   5999:                 for ( u = 0; u < curlen; u++, p++ ) {
                   6000:                     if ( tab[u] ) {
                   6001:                         ndl_addto(DL(tab[u]),d);
                   6002:                         if ( mod ) {
                   6003:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
                   6004:                         } else if ( nd_vc )
                   6005:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
                   6006:             else {
1.6       noro     6007:                             mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1       noro     6008:                         }
                   6009:                     }
                   6010:                 }
                   6011:             } else {
                   6012:                 for ( u = 0; u < curlen; u++, p++ ) {
                   6013:                     if ( tab[u] ) {
                   6014:                         NEWNM(t);
                   6015:                         ndl_add(DL(tab[u]),d,DL(t));
                   6016:                         if ( mod ) {
                   6017:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
                   6018:                         } else if ( nd_vc )
                   6019:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
                   6020:             else
1.6       noro     6021:                             mulz(CZ(tab[u]),q,&CZ(t));
1.1       noro     6022:                         *p = t;
                   6023:                     }
                   6024:                 }
                   6025:             }
                   6026:         }
                   6027:         curlen *= k+1;
                   6028:     }
                   6029:     FREENM(m);
                   6030:     if ( nd_module ) {
                   6031:         mpos = MPOS(d1);
                   6032:         for ( i = 0; i < tlen; i++ )
                   6033:             if ( tab[i] ) {
                   6034:                 d = DL(tab[i]);
                   6035:                 MPOS(d) = mpos;
                   6036:                 TD(d) = ndl_weight(d);
                   6037:             }
                   6038:     }
                   6039: }
                   6040:
                   6041: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   6042: {
                   6043:     NM mr,mr0;
                   6044:     NMV m;
                   6045:     UINT *d,*dt,*dm;
                   6046:     int c,n,td,i,c1,c2,len;
                   6047:     Q q;
                   6048:     ND r;
                   6049:
                   6050:     if ( !p ) return 0;
                   6051:     else {
                   6052:         n = NV(p); m = BDY(p);
                   6053:         d = DL(m0);
                   6054:         len = LEN(p);
                   6055:         mr0 = 0;
                   6056:         td = TD(d);
                   6057:         c = CM(m0);
                   6058:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6059:             NEXTNM(mr0,mr);
                   6060:             CM(mr) = 1;
                   6061:             ndl_add(DL(m),d,DL(mr));
                   6062:         }
                   6063:         NEXT(mr) = 0;
                   6064:         MKND(NV(p),mr0,len,r);
                   6065:         SG(r) = SG(p) + TD(d);
                   6066:         return r;
                   6067:     }
                   6068: }
                   6069:
                   6070: ND ndv_mul_nm(int mod,NM m0,NDV p)
                   6071: {
                   6072:     NM mr,mr0;
                   6073:     NMV m;
                   6074:     UINT *d,*dt,*dm;
                   6075:     int c,n,td,i,c1,c2,len;
                   6076:     P q;
                   6077:     ND r;
                   6078:
                   6079:     if ( !p ) return 0;
                   6080:     else if ( do_weyl ) {
                   6081:         if ( mod < 0 ) {
                   6082:             error("ndv_mul_nm : not implemented (weyl)");
                   6083:             return 0;
                   6084:         } else
                   6085:             return weyl_ndv_mul_nm(mod,m0,p);
                   6086:     } else {
                   6087:         n = NV(p); m = BDY(p);
                   6088:         d = DL(m0);
                   6089:         len = LEN(p);
                   6090:         mr0 = 0;
                   6091:         td = TD(d);
                   6092:         if ( mod == -1 ) {
                   6093:             c = CM(m0);
                   6094:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6095:                 NEXTNM(mr0,mr);
                   6096:                 CM(mr) = _mulsf(CM(m),c);
                   6097:                 ndl_add(DL(m),d,DL(mr));
                   6098:             }
                   6099:         } else if ( mod == -2 ) {
                   6100:             Z cl;
                   6101:             cl = CZ(m0);
                   6102:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6103:                 NEXTNM(mr0,mr);
                   6104:                 mullf(CZ(m),cl,&CZ(mr));
                   6105:                 ndl_add(DL(m),d,DL(mr));
                   6106:             }
                   6107:         } else if ( mod ) {
                   6108:             c = CM(m0);
                   6109:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6110:                 NEXTNM(mr0,mr);
                   6111:                 c1 = CM(m);
                   6112:                 DMAR(c1,c,0,mod,c2);
                   6113:                 CM(mr) = c2;
                   6114:                 ndl_add(DL(m),d,DL(mr));
                   6115:             }
                   6116:         } else {
                   6117:             q = CP(m0);
                   6118:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6119:                 NEXTNM(mr0,mr);
                   6120:                 mulp(nd_vc,CP(m),q,&CP(mr));
                   6121:                 ndl_add(DL(m),d,DL(mr));
                   6122:             }
                   6123:         }
                   6124:         NEXT(mr) = 0;
                   6125:         MKND(NV(p),mr0,len,r);
                   6126:         SG(r) = SG(p) + TD(d);
                   6127:         return r;
                   6128:     }
                   6129: }
                   6130:
                   6131: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
                   6132: {
                   6133:     NM mq0,mq;
                   6134:     NMV tm;
                   6135:     Q q;
                   6136:     int i,nv,sg,c,c1,c2,hindex;
                   6137:     ND p,t,r;
                   6138:
                   6139:     if ( bucket->m < 0 ) return 0;
                   6140:     else {
                   6141:         nv = NV(d);
                   6142:         mq0 = 0;
                   6143:         tm = (NMV)MALLOC(nmv_adv);
                   6144:         while ( 1 ) {
                   6145:             if ( mod > 0 || mod == -1 )
                   6146:               hindex = head_pbucket(mod,bucket);
                   6147:             else if ( mod == -2 )
                   6148:               hindex = head_pbucket_lf(bucket);
                   6149:             else
                   6150:               hindex = head_pbucket_q(bucket);
                   6151:             if ( hindex < 0 ) break;
                   6152:             p = bucket->body[hindex];
                   6153:             NEXTNM(mq0,mq);
                   6154:             ndl_sub(HDL(p),HDL(d),DL(mq));
                   6155:             ndl_copy(DL(mq),DL(tm));
                   6156:             if ( mod ) {
                   6157:                 c1 = invm(HCM(d),mod); c2 = HCM(p);
                   6158:                 DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   6159:                 CM(tm) = mod-c;
                   6160:             } else {
1.6       noro     6161:                 divsz(HCZ(p),HCZ(d),&CZ(mq));
                   6162:                 chsgnz(CZ(mq),&CZ(tm));
1.1       noro     6163:             }
                   6164:             t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
                   6165:             bucket->body[hindex] = nd_remove_head(p);
                   6166:             t = nd_remove_head(t);
                   6167:             add_pbucket(mod,bucket,t);
                   6168:         }
                   6169:         if ( !mq0 )
                   6170:             r = 0;
                   6171:         else {
                   6172:             NEXT(mq) = 0;
                   6173:             for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   6174:             MKND(nv,mq0,i,r);
                   6175:             /* XXX */
                   6176:             SG(r) = HTD(r);
                   6177:         }
                   6178:         return r;
                   6179:     }
                   6180: }
                   6181:
                   6182: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   6183: {
                   6184:     NMV m,mr,mr0,t;
                   6185:     int len,i,k;
                   6186:
                   6187:     if ( !p ) return;
                   6188:     m = BDY(p); len = LEN(p);
                   6189:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   6190:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   6191:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   6192:     t = (NMV)MALLOC(nmv_adv);
                   6193:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6       noro     6194:         CZ(t) = CZ(m);
1.1       noro     6195:         for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   6196:         ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6       noro     6197:         CZ(mr) = CZ(t);
1.1       noro     6198:         ndl_copy(DL(t),DL(mr));
                   6199:     }
                   6200:     BDY(p) = mr0;
                   6201: }
                   6202:
                   6203: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   6204: {
                   6205:     NMV m,mr,mr0;
                   6206:     int len,i;
                   6207:     NDV r;
                   6208:
                   6209:     if ( !p ) return 0;
                   6210:     m = BDY(p); len = LEN(p);
                   6211:     mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   6212:     for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   6213:         ndl_zero(DL(mr));
                   6214:         ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6       noro     6215:         CZ(mr) = CZ(m);
1.1       noro     6216:     }
                   6217:     MKNDV(NV(p),mr0,len,r);
                   6218:     SG(r) = SG(p);
1.24      noro     6219:     r->sig = p->sig;
1.1       noro     6220:     return r;
                   6221: }
                   6222:
                   6223: /* duplicate p */
                   6224:
                   6225: NDV ndv_dup(int mod,NDV p)
                   6226: {
                   6227:     NDV d;
                   6228:     NMV t,m,m0;
                   6229:     int i,len;
                   6230:
                   6231:     if ( !p ) return 0;
                   6232:     len = LEN(p);
                   6233:     m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   6234:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   6235:         ndl_copy(DL(t),DL(m));
1.6       noro     6236:         CZ(m) = CZ(t);
1.1       noro     6237:     }
                   6238:     MKNDV(NV(p),m0,len,d);
                   6239:     SG(d) = SG(p);
                   6240:     return d;
                   6241: }
                   6242:
                   6243: NDV ndv_symbolic(int mod,NDV p)
                   6244: {
                   6245:     NDV d;
                   6246:     NMV t,m,m0;
                   6247:     int i,len;
                   6248:
                   6249:     if ( !p ) return 0;
                   6250:     len = LEN(p);
                   6251:     m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   6252:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   6253:         ndl_copy(DL(t),DL(m));
1.6       noro     6254:         CZ(m) = ONE;
1.1       noro     6255:     }
                   6256:     MKNDV(NV(p),m0,len,d);
                   6257:     SG(d) = SG(p);
                   6258:     return d;
                   6259: }
                   6260:
                   6261: ND nd_dup(ND p)
                   6262: {
                   6263:     ND d;
                   6264:     NM t,m,m0;
                   6265:
                   6266:     if ( !p ) return 0;
                   6267:     for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   6268:         NEXTNM(m0,m);
                   6269:         ndl_copy(DL(t),DL(m));
1.6       noro     6270:         CZ(m) = CZ(t);
1.1       noro     6271:     }
                   6272:     if ( m0 ) NEXT(m) = 0;
                   6273:     MKND(NV(p),m0,LEN(p),d);
                   6274:     SG(d) = SG(p);
                   6275:     return d;
                   6276: }
                   6277:
                   6278: /* XXX if p->len == 0 then it represents 0 */
                   6279:
                   6280: void ndv_mod(int mod,NDV p)
                   6281: {
                   6282:     NMV t,d;
                   6283:     int r,s,u;
                   6284:     int i,len,dlen;
                   6285:     P cp;
                   6286:     Z c;
                   6287:     Obj gfs;
                   6288:
                   6289:     if ( !p ) return;
                   6290:     len = LEN(p);
                   6291:     dlen = 0;
                   6292:     if ( mod == -1 )
                   6293:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   6294:             simp_ff((Obj)CP(t),&gfs);
                   6295:             if ( gfs ) {
                   6296:               r = FTOIF(CONT((GFS)gfs));
                   6297:               CM(d) = r;
                   6298:               ndl_copy(DL(t),DL(d));
                   6299:               NMV_ADV(d);
                   6300:               dlen++;
                   6301:             }
                   6302:         }
                   6303:     else if ( mod == -2 )
                   6304:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   6305:             simp_ff((Obj)CP(t),&gfs);
                   6306:             if ( gfs ) {
                   6307:               lmtolf((LM)gfs,&CZ(d));
                   6308:               ndl_copy(DL(t),DL(d));
                   6309:               NMV_ADV(d);
                   6310:               dlen++;
                   6311:             }
                   6312:         }
                   6313:     else
                   6314:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   6315:             if ( nd_vc ) {
                   6316:                 nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
                   6317:                 c = (Z)cp;
                   6318:             } else
1.6       noro     6319:                 c = CZ(t);
1.1       noro     6320:             r = remqi((Q)c,mod);
                   6321:             if ( r ) {
                   6322:                 CM(d) = r;
                   6323:                 ndl_copy(DL(t),DL(d));
                   6324:                 NMV_ADV(d);
                   6325:                 dlen++;
                   6326:             }
                   6327:         }
                   6328:     LEN(p) = dlen;
                   6329: }
                   6330:
                   6331: NDV ptondv(VL vl,VL dvl,P p)
                   6332: {
                   6333:     ND nd;
                   6334:
                   6335:     nd = ptond(vl,dvl,p);
                   6336:     return ndtondv(0,nd);
                   6337: }
                   6338:
                   6339: void pltozpl(LIST l,Q *cont,LIST *pp)
                   6340: {
1.16      noro     6341:   NODE nd,nd1;
                   6342:   int n;
                   6343:   P *pl;
                   6344:   Q *cl;
                   6345:   int i;
                   6346:   P dmy;
                   6347:   Z dvr,inv;
                   6348:   LIST r;
                   6349:
                   6350:   nd = BDY(l); n = length(nd);
                   6351:   pl = (P *)MALLOC(n*sizeof(P));
                   6352:   cl = (Q *)MALLOC(n*sizeof(Q));
                   6353:   for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
                   6354:     ptozp((P)BDY(nd),1,&cl[i],&dmy);
                   6355:   }
                   6356:   qltozl(cl,n,&dvr);
                   6357:   divz(ONE,dvr,&inv);
                   6358:   nd = BDY(l);
                   6359:   for ( i = 0; i < n; i++, nd = NEXT(nd) )
                   6360:     divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
                   6361:   nd = 0;
                   6362:   for ( i = n-1; i >= 0; i-- ) {
                   6363:     MKNODE(nd1,pl[i],nd); nd = nd1;
                   6364:   }
                   6365:   MKLIST(r,nd);
                   6366:   *pp = r;
1.1       noro     6367: }
                   6368:
                   6369: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
                   6370:
                   6371: NDV pltondv(VL vl,VL dvl,LIST p)
                   6372: {
                   6373:     int i;
                   6374:     NODE t;
                   6375:     ND r,ri;
                   6376:     NM m;
                   6377:
                   6378:     if ( !nd_module ) error("pltond : module order must be set");
                   6379:     r = 0;
                   6380:     for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
                   6381:         ri = ptond(vl,dvl,(P)BDY(t));
                   6382:         if ( ri )
                   6383:             for ( m = BDY(ri); m; m = NEXT(m) ) {
                   6384:                 MPOS(DL(m)) = i;
                   6385:                 TD(DL(m)) = ndl_weight(DL(m));
                   6386:                 if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   6387:             }
                   6388:         r = nd_add(0,r,ri);
                   6389:     }
                   6390:     return ndtondv(0,r);
                   6391: }
                   6392:
                   6393: ND ptond(VL vl,VL dvl,P p)
                   6394: {
                   6395:     int n,i,j,k,e;
                   6396:     VL tvl;
                   6397:     V v;
                   6398:     DCP dc;
                   6399:     DCP *w;
                   6400:     ND r,s,t,u;
                   6401:     P x;
                   6402:     int c;
                   6403:     UINT *d;
                   6404:     NM m,m0;
                   6405:
                   6406:     if ( !p )
                   6407:         return 0;
                   6408:     else if ( NUM(p) ) {
                   6409:         NEWNM(m);
                   6410:         ndl_zero(DL(m));
                   6411:         if ( !INT((Q)p) )
                   6412:           error("ptond : input must be integer-coefficient");
1.6       noro     6413:         CZ(m) = (Z)p;
1.1       noro     6414:         NEXT(m) = 0;
                   6415:         MKND(nd_nvar,m,1,r);
                   6416:         SG(r) = 0;
                   6417:         return r;
                   6418:     } else {
                   6419:         for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   6420:         w = (DCP *)MALLOC(k*sizeof(DCP));
                   6421:         for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   6422:         for ( i = 0, tvl = dvl, v = VR(p);
                   6423:             tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   6424:         if ( !tvl ) {
                   6425:             for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   6426:                 t = ptond(vl,dvl,COEF(w[j]));
                   6427:                 pwrp(vl,x,DEG(w[j]),&p);
                   6428:                 nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   6429:             }
                   6430:             return s;
                   6431:         } else {
                   6432:             NEWNM(m0); d = DL(m0);
                   6433:             for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6       noro     6434:                 ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1       noro     6435:                 TD(d) = MUL_WEIGHT(e,i);
                   6436:                 if ( nd_blockmask) ndl_weight_mask(d);
                   6437:                 if ( nd_module ) MPOS(d) = 0;
                   6438:                 t = ptond(vl,dvl,COEF(w[j]));
                   6439:                 for ( m = BDY(t); m; m = NEXT(m) )
                   6440:                     ndl_addto(DL(m),d);
                   6441:                 SG(t) += TD(d);
                   6442:                 s = nd_add(0,s,t);
                   6443:             }
                   6444:             FREENM(m0);
                   6445:             return s;
                   6446:         }
                   6447:     }
                   6448: }
                   6449:
                   6450: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   6451: {
                   6452:     VL tvl;
                   6453:     int len,n,j,i,e;
                   6454:     NMV m;
                   6455:     Z q;
                   6456:     P c;
                   6457:     UINT *d;
                   6458:     P s,r,u,t,w;
                   6459:     GFS gfs;
                   6460:
                   6461:     if ( !p ) return 0;
                   6462:     else {
                   6463:         len = LEN(p);
                   6464:         n = NV(p);
                   6465:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   6466:         for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   6467:             if ( mod == -1 ) {
                   6468:                e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   6469:             } else if ( mod == -2 ) {
                   6470:                c = (P)CZ(m);
                   6471:             } else if ( mod > 0 ) {
1.6       noro     6472:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     6473:             } else
                   6474:                 c = CP(m);
                   6475:             d = DL(m);
                   6476:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     6477:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     6478:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   6479:             }
                   6480:             addp(vl,s,t,&u); s = u;
                   6481:         }
                   6482:         return s;
                   6483:     }
                   6484: }
                   6485:
                   6486: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
                   6487: {
                   6488:     VL tvl;
                   6489:     int len,n,j,i,e;
                   6490:     NMV m;
                   6491:     Z q;
                   6492:     P c;
                   6493:     UINT *d;
                   6494:     P s,r,u,t,w;
                   6495:     GFS gfs;
                   6496:     P *a;
                   6497:     LIST l;
                   6498:     NODE nd,nd1;
                   6499:
                   6500:     if ( !p ) return 0;
                   6501:     else {
                   6502:         a = (P *)MALLOC((rank+1)*sizeof(P));
                   6503:         for ( i = 0; i <= rank; i++ ) a[i] = 0;
                   6504:         len = LEN(p);
                   6505:         n = NV(p);
                   6506:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   6507:         for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
                   6508:             if ( mod == -1 ) {
                   6509:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   6510:             } else if ( mod ) {
1.6       noro     6511:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     6512:             } else
                   6513:                 c = CP(m);
                   6514:             d = DL(m);
                   6515:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     6516:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     6517:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   6518:             }
                   6519:             addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
                   6520:         }
                   6521:         nd = 0;
                   6522:         for ( i = rank; i > 0; i-- ) {
                   6523:             MKNODE(nd1,a[i],nd); nd = nd1;
                   6524:         }
                   6525:         MKLIST(l,nd);
                   6526:         return l;
                   6527:     }
                   6528: }
                   6529:
                   6530: NDV ndtondv(int mod,ND p)
                   6531: {
                   6532:     NDV d;
                   6533:     NMV m,m0;
                   6534:     NM t;
                   6535:     int i,len;
                   6536:
                   6537:     if ( !p ) return 0;
                   6538:     len = LEN(p);
                   6539:     if ( mod > 0 || mod == -1 )
                   6540:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   6541:     else
                   6542:         m0 = m = MALLOC(len*nmv_adv);
                   6543: #if 0
                   6544:     ndv_alloc += nmv_adv*len;
                   6545: #endif
                   6546:     for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   6547:         ndl_copy(DL(t),DL(m));
1.6       noro     6548:         CZ(m) = CZ(t);
1.1       noro     6549:     }
                   6550:     MKNDV(NV(p),m0,len,d);
                   6551:     SG(d) = SG(p);
1.24      noro     6552:     d->sig = p->sig;
1.1       noro     6553:     return d;
                   6554: }
                   6555:
1.16      noro     6556: static int dmm_comp_nv;
                   6557:
                   6558: int dmm_comp(DMM *a,DMM *b)
                   6559: {
                   6560:    return -compdmm(dmm_comp_nv,*a,*b);
                   6561: }
                   6562:
                   6563: void dmm_sort_by_ord(DMM *a,int len,int nv)
                   6564: {
                   6565:   dmm_comp_nv = nv;
                   6566:   qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
                   6567: }
                   6568:
                   6569: void dpm_sort(DPM p,DPM *rp)
                   6570: {
                   6571:   DMM t,t1;
                   6572:   int len,i,n;
                   6573:   DMM *a;
                   6574:   DPM d;
                   6575:
                   6576:   if ( !p ) *rp = 0;
                   6577:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   6578:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   6579:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   6580:   n = p->nv;
                   6581:   dmm_sort_by_ord(a,len,n);
                   6582:   t = 0;
                   6583:   for ( i = len-1; i >= 0; i-- ) {
                   6584:     NEWDMM(t1);
                   6585:     t1->c = a[i]->c;
                   6586:     t1->dl = a[i]->dl;
                   6587:     t1->pos = a[i]->pos;
                   6588:     t1->next = t;
                   6589:     t = t1;
                   6590:   }
                   6591:   MKDPM(n,t,d);
                   6592:   SG(d) = SG(p);
                   6593:   *rp = d;
                   6594: }
                   6595:
1.18      noro     6596: int dpm_comp(DPM *a,DPM *b)
                   6597: {
1.22      noro     6598:   return -compdpm(CO,*a,*b);
1.18      noro     6599: }
                   6600:
                   6601: NODE dpm_sort_list(NODE l)
                   6602: {
                   6603:   int i,len;
                   6604:   NODE t,t1;
                   6605:   DPM *a;
                   6606:
                   6607:   len = length(l);
                   6608:   a = (DPM *)MALLOC(len*sizeof(DPM));
                   6609:   for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
                   6610:   qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
                   6611:   t = 0;
                   6612:   for ( i = len-1; i >= 0; i-- ) {
                   6613:     MKNODE(t1,(pointer)a[i],t); t = t1;
                   6614:   }
                   6615:   return t;
                   6616: }
                   6617:
1.20      noro     6618: int nmv_comp(NMV a,NMV b)
                   6619: {
1.21      noro     6620:   int t;
                   6621:   t = DL_COMPARE(a->dl,b->dl);
                   6622:   return -t;
1.20      noro     6623: }
                   6624:
1.16      noro     6625: NDV dpmtondv(int mod,DPM p)
                   6626: {
                   6627:   NDV d;
                   6628:   NMV m,m0;
                   6629:   DMM t;
                   6630:   DMM *a;
                   6631:   int i,len,n;
                   6632:
                   6633:   if ( !p ) return 0;
                   6634:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   6635:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   6636:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   6637:   n = p->nv;
                   6638:   dmm_sort_by_ord(a,len,n);
                   6639:   if ( mod > 0 || mod == -1 )
                   6640:     m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   6641:   else
                   6642:     m0 = m = MALLOC(len*nmv_adv);
                   6643: #if 0
                   6644:   ndv_alloc += nmv_adv*len;
                   6645: #endif
                   6646:   for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6647:     dltondl(n,a[i]->dl,DL(m));
                   6648:     MPOS(DL(m)) = a[i]->pos;
1.20      noro     6649:     TD(DL(m)) = ndl_weight(DL(m));
1.16      noro     6650:     CZ(m) = (Z)a[i]->c;
                   6651:   }
1.20      noro     6652:   qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16      noro     6653:   MKNDV(NV(p),m0,len,d);
                   6654:   SG(d) = SG(p);
                   6655:   return d;
                   6656: }
                   6657:
1.1       noro     6658: ND ndvtond(int mod,NDV p)
                   6659: {
                   6660:     ND d;
                   6661:     NM m,m0;
                   6662:     NMV t;
                   6663:     int i,len;
                   6664:
                   6665:     if ( !p ) return 0;
                   6666:     m0 = 0;
                   6667:     len = p->len;
                   6668:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   6669:         NEXTNM(m0,m);
                   6670:         ndl_copy(DL(t),DL(m));
1.6       noro     6671:         CZ(m) = CZ(t);
1.1       noro     6672:     }
                   6673:     NEXT(m) = 0;
                   6674:     MKND(NV(p),m0,len,d);
                   6675:     SG(d) = SG(p);
1.28      noro     6676:     d->sig = p->sig;
1.1       noro     6677:     return d;
                   6678: }
                   6679:
                   6680: DP ndvtodp(int mod,NDV p)
                   6681: {
                   6682:     MP m,m0;
                   6683:   DP d;
                   6684:     NMV t;
                   6685:     int i,len;
                   6686:
                   6687:     if ( !p ) return 0;
                   6688:     m0 = 0;
                   6689:     len = p->len;
                   6690:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   6691:         NEXTMP(m0,m);
                   6692:       m->dl = ndltodl(nd_nvar,DL(t));
                   6693:       m->c = (Obj)ndctop(mod,t->c);
                   6694:     }
                   6695:     NEXT(m) = 0;
                   6696:   MKDP(nd_nvar,m0,d);
                   6697:     SG(d) = SG(p);
                   6698:     return d;
                   6699: }
                   6700:
1.16      noro     6701: DPM ndvtodpm(int mod,NDV p)
                   6702: {
                   6703:   DMM m,m0;
                   6704:   DPM d;
                   6705:   NMV t;
                   6706:   int i,len;
                   6707:
                   6708:   if ( !p ) return 0;
                   6709:   m0 = 0;
                   6710:   len = p->len;
                   6711:   for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   6712:     NEXTDMM(m0,m);
                   6713:     m->dl = ndltodl(nd_nvar,DL(t));
                   6714:     m->c = (Obj)ndctop(mod,t->c);
                   6715:     m->pos = MPOS(DL(t));
                   6716:   }
                   6717:   NEXT(m) = 0;
                   6718:   MKDPM(nd_nvar,m0,d);
                   6719:   SG(d) = SG(p);
                   6720:   return d;
                   6721: }
                   6722:
                   6723:
1.1       noro     6724: DP ndtodp(int mod,ND p)
                   6725: {
                   6726:     MP m,m0;
                   6727:   DP d;
                   6728:     NM t;
                   6729:     int i,len;
                   6730:
                   6731:     if ( !p ) return 0;
                   6732:     m0 = 0;
                   6733:     len = p->len;
                   6734:     for ( t = BDY(p); t; t = NEXT(t) ) {
                   6735:         NEXTMP(m0,m);
                   6736:       m->dl = ndltodl(nd_nvar,DL(t));
                   6737:       m->c = (Obj)ndctop(mod,t->c);
                   6738:     }
                   6739:     NEXT(m) = 0;
                   6740:   MKDP(nd_nvar,m0,d);
                   6741:     SG(d) = SG(p);
                   6742:     return d;
                   6743: }
                   6744:
                   6745: void ndv_print(NDV p)
                   6746: {
                   6747:     NMV m;
                   6748:     int i,len;
                   6749:
                   6750:     if ( !p ) printf("0\n");
                   6751:     else {
                   6752:         len = LEN(p);
                   6753:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6754:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     6755:             else printf("+%ld*",CM(m));
1.1       noro     6756:             ndl_print(DL(m));
                   6757:         }
                   6758:         printf("\n");
                   6759:     }
                   6760: }
                   6761:
                   6762: void ndv_print_q(NDV p)
                   6763: {
                   6764:     NMV m;
                   6765:     int i,len;
                   6766:
                   6767:     if ( !p ) printf("0\n");
                   6768:     else {
                   6769:         len = LEN(p);
                   6770:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6771:             printf("+");
1.6       noro     6772:             printexpr(CO,(Obj)CZ(m));
1.1       noro     6773:             printf("*");
                   6774:             ndl_print(DL(m));
                   6775:         }
                   6776:         printf("\n");
                   6777:     }
                   6778: }
                   6779:
                   6780: NODE ndv_reducebase(NODE x,int *perm)
                   6781: {
                   6782:     int len,i,j;
                   6783:     NDVI w;
                   6784:     NODE t,t0;
                   6785:
                   6786:     len = length(x);
                   6787:     w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
                   6788:     for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
                   6789:         w[i].p = BDY(t); w[i].i = perm[i];
                   6790:     }
                   6791:     for ( i = 0; i < len; i++ ) {
                   6792:         for ( j = 0; j < i; j++ ) {
                   6793:             if ( w[i].p && w[j].p ) {
                   6794:                 if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
                   6795:                 else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
                   6796:             }
                   6797:         }
                   6798:     }
                   6799:     for ( i = j = 0, t0 = 0; i < len; i++ ) {
                   6800:         if ( w[i].p ) {
                   6801:             NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
                   6802:             perm[j++] = w[i].i;
                   6803:         }
                   6804:     }
                   6805:     NEXT(t) = 0; x = t0;
                   6806:     return x;
                   6807: }
                   6808:
                   6809: /* XXX incomplete */
                   6810:
1.21      noro     6811: extern DMMstack dmm_stack;
                   6812: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16      noro     6813:
1.1       noro     6814: void nd_init_ord(struct order_spec *ord)
                   6815: {
                   6816:   nd_module = (ord->id >= 256);
                   6817:   if ( nd_module ) {
                   6818:     nd_dcomp = -1;
1.21      noro     6819:     nd_module_ordtype = ord->module_ordtype;
1.1       noro     6820:     nd_pot_nelim = ord->pot_nelim;
                   6821:     nd_poly_weight_len = ord->nv;
                   6822:     nd_poly_weight = ord->top_weight;
                   6823:     nd_module_rank = ord->module_rank;
                   6824:     nd_module_weight = ord->module_top_weight;
                   6825:   }
                   6826:   nd_matrix = 0;
                   6827:   nd_matrix_len = 0;
                   6828:     switch ( ord->id ) {
                   6829:         case 0:
                   6830:             switch ( ord->ord.simple ) {
                   6831:                 case 0:
                   6832:                     nd_dcomp = 1;
                   6833:                     nd_isrlex = 1;
                   6834:                     break;
                   6835:                 case 1:
                   6836:                     nd_dcomp = 1;
                   6837:                     nd_isrlex = 0;
                   6838:                     break;
                   6839:                 case 2:
                   6840:                     nd_dcomp = 0;
                   6841:                     nd_isrlex = 0;
                   6842:                     ndl_compare_function = ndl_lex_compare;
                   6843:                     break;
                   6844:                 case 11:
                   6845:                     /* XXX */
                   6846:                     nd_dcomp = 0;
                   6847:                     nd_isrlex = 1;
                   6848:                     ndl_compare_function = ndl_ww_lex_compare;
                   6849:                     break;
                   6850:                 default:
                   6851:                     error("nd_gr : unsupported order");
                   6852:             }
                   6853:             break;
                   6854:         case 1:
                   6855:             /* block order */
                   6856:             /* XXX */
                   6857:             nd_dcomp = -1;
                   6858:             nd_isrlex = 0;
                   6859:             ndl_compare_function = ndl_block_compare;
                   6860:             break;
                   6861:         case 2:
                   6862:             /* matrix order */
                   6863:             /* XXX */
                   6864:             nd_dcomp = -1;
                   6865:             nd_isrlex = 0;
                   6866:             nd_matrix_len = ord->ord.matrix.row;
                   6867:             nd_matrix = ord->ord.matrix.matrix;
                   6868:             ndl_compare_function = ndl_matrix_compare;
                   6869:             break;
                   6870:         case 3:
                   6871:             /* composite order */
                   6872:             nd_dcomp = -1;
                   6873:             nd_isrlex = 0;
                   6874:             nd_worb_len = ord->ord.composite.length;
                   6875:             nd_worb = ord->ord.composite.w_or_b;
                   6876:             ndl_compare_function = ndl_composite_compare;
                   6877:             break;
                   6878:
                   6879:         /* module order */
                   6880:         case 256:
                   6881:             switch ( ord->ord.simple ) {
                   6882:                 case 0:
1.21      noro     6883:                     nd_dcomp = 0;
1.1       noro     6884:                     nd_isrlex = 1;
1.21      noro     6885:                     ndl_compare_function = ndl_module_glex_compare;
1.1       noro     6886:                     break;
                   6887:                 case 1:
1.21      noro     6888:                     nd_dcomp = 0;
1.1       noro     6889:                     nd_isrlex = 0;
                   6890:                     ndl_compare_function = ndl_module_glex_compare;
                   6891:                     break;
                   6892:                 case 2:
1.21      noro     6893:                     nd_dcomp = 0;
1.1       noro     6894:                     nd_isrlex = 0;
1.21      noro     6895:                     ndl_compare_function = ndl_module_compare;
                   6896:                     ndl_base_compare_function = ndl_lex_compare;
1.1       noro     6897:                     break;
                   6898:                 default:
1.21      noro     6899:                     error("nd_init_ord : unsupported order");
1.1       noro     6900:             }
                   6901:             break;
                   6902:         case 257:
                   6903:             /* block order */
                   6904:             nd_isrlex = 0;
1.21      noro     6905:             ndl_compare_function = ndl_module_compare;
                   6906:             ndl_base_compare_function = ndl_block_compare;
1.1       noro     6907:             break;
                   6908:         case 258:
                   6909:             /* matrix order */
                   6910:             nd_isrlex = 0;
                   6911:             nd_matrix_len = ord->ord.matrix.row;
                   6912:             nd_matrix = ord->ord.matrix.matrix;
1.21      noro     6913:             ndl_compare_function = ndl_module_compare;
                   6914:             ndl_base_compare_function = ndl_matrix_compare;
1.1       noro     6915:             break;
                   6916:         case 259:
                   6917:             /* composite order */
                   6918:             nd_isrlex = 0;
                   6919:             nd_worb_len = ord->ord.composite.length;
                   6920:             nd_worb = ord->ord.composite.w_or_b;
1.21      noro     6921:             ndl_compare_function = ndl_module_compare;
                   6922:             ndl_base_compare_function = ndl_composite_compare;
                   6923:             break;
                   6924:         case 300:
                   6925:             /* schreyer order */
                   6926:             if ( ord->base->id != 256 )
                   6927:                error("nd_init_ord : unsupported base order");
                   6928:             ndl_compare_function = ndl_module_schreyer_compare;
                   6929:             dmm_stack = ord->dmmstack;
                   6930:             switch ( ord->base->ord.simple ) {
                   6931:                 case 0:
                   6932:                     nd_isrlex = 1;
                   6933:                     ndl_base_compare_function = ndl_glex_compare;
                   6934:                     dl_base_compare_function = cmpdl_revgradlex;
                   6935:                     break;
                   6936:                 case 1:
                   6937:                     nd_isrlex = 0;
                   6938:                     ndl_base_compare_function = ndl_glex_compare;
                   6939:                     dl_base_compare_function = cmpdl_gradlex;
                   6940:                     break;
                   6941:                 case 2:
                   6942:                     nd_isrlex = 0;
                   6943:                     ndl_base_compare_function = ndl_lex_compare;
                   6944:                     dl_base_compare_function = cmpdl_lex;
                   6945:                     break;
                   6946:                 default:
                   6947:                     error("nd_init_ord : unsupported order");
                   6948:             }
1.1       noro     6949:             break;
                   6950:     }
                   6951:     nd_ord = ord;
                   6952: }
                   6953:
                   6954: BlockMask nd_create_blockmask(struct order_spec *ord)
                   6955: {
                   6956:     int n,i,j,s,l;
                   6957:     UINT *t;
                   6958:     BlockMask bm;
                   6959:
                   6960:     /* we only create mask table for block order */
                   6961:     if ( ord->id != 1 && ord->id != 257 )
                   6962:         return 0;
                   6963:     n = ord->ord.block.length;
                   6964:     bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   6965:     bm->n = n;
                   6966:     bm->order_pair = ord->ord.block.order_pair;
                   6967:     bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
                   6968:     for ( i = 0, s = 0; i < n; i++ ) {
                   6969:         bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
                   6970:         for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   6971:         l = bm->order_pair[i].length;
                   6972:         for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   6973:     }
                   6974:     return bm;
                   6975: }
                   6976:
                   6977: EPOS nd_create_epos(struct order_spec *ord)
                   6978: {
                   6979:     int i,j,l,s,ord_l,ord_o;
                   6980:     EPOS epos;
                   6981:     struct order_pair *op;
                   6982:
                   6983:     epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   6984:     switch ( ord->id ) {
1.21      noro     6985:         case 0: case 256: case 300:
1.1       noro     6986:             if ( nd_isrlex ) {
                   6987:                 for ( i = 0; i < nd_nvar; i++ ) {
                   6988:                     epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   6989:                     epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   6990:                 }
                   6991:             } else {
                   6992:                 for ( i = 0; i < nd_nvar; i++ ) {
                   6993:                     epos[i].i = nd_exporigin + i/nd_epw;
                   6994:                     epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   6995:                 }
                   6996:             }
                   6997:             break;
                   6998:         case 1: case 257:
                   6999:             /* block order */
                   7000:             l = ord->ord.block.length;
                   7001:             op = ord->ord.block.order_pair;
                   7002:             for ( j = 0, s = 0; j < l; j++ ) {
                   7003:                 ord_o = op[j].order;
                   7004:                 ord_l = op[j].length;
                   7005:                 if ( !ord_o )
                   7006:                     for ( i = 0; i < ord_l; i++ ) {
                   7007:                         epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   7008:                         epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   7009:                     }
                   7010:                 else
                   7011:                     for ( i = 0; i < ord_l; i++ ) {
                   7012:                         epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   7013:                         epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   7014:                     }
                   7015:                 s += ord_l;
                   7016:             }
                   7017:             break;
                   7018:         case 2:
                   7019:             /* matrix order */
                   7020:         case 3:
                   7021:             /* composite order */
                   7022:         default:
                   7023:             for ( i = 0; i < nd_nvar; i++ ) {
                   7024:                 epos[i].i = nd_exporigin + i/nd_epw;
                   7025:                 epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   7026:             }
                   7027:             break;
                   7028:     }
                   7029:     return epos;
                   7030: }
                   7031:
                   7032: /* external interface */
                   7033:
                   7034: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
                   7035: {
                   7036:     NODE t,in0,in;
                   7037:     ND ndf,nf;
                   7038:     NDV ndvf;
                   7039:     VL vv,tv;
                   7040:     int stat,nvar,max,mrank;
                   7041:     union oNDC dn;
                   7042:     Q cont;
                   7043:     P pp;
                   7044:     LIST ppl;
                   7045:
                   7046:     if ( !f ) {
                   7047:         *rp = 0;
                   7048:         return;
                   7049:     }
                   7050:     pltovl(v,&vv);
                   7051:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   7052:
                   7053:     /* max=65536 implies nd_bpe=32 */
                   7054:     max = 65536;
                   7055:
                   7056:   nd_module = 0;
                   7057:   /* nd_module will be set if ord is a module ordering */
                   7058:     nd_init_ord(ord);
                   7059:     nd_setup_parameters(nvar,max);
                   7060:     if ( nd_module && OID(f) != O_LIST )
                   7061:         error("nd_nf_p : the first argument must be a list");
                   7062:   if ( nd_module ) mrank = length(BDY((LIST)f));
                   7063:     /* conversion to ndv */
                   7064:     for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   7065:         NEXTNODE(in0,in);
                   7066:         if ( nd_module ) {
                   7067:           if ( !BDY(t) || OID(BDY(t)) != O_LIST
                   7068:                || length(BDY((LIST)BDY(t))) != mrank )
                   7069:               error("nd_nf_p : inconsistent basis element");
                   7070:           if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
                   7071:           else ppl = (LIST)BDY(t);
                   7072:           BDY(in) = (pointer)pltondv(CO,vv,ppl);
                   7073:         } else {
                   7074:           if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
                   7075:           else pp = (P)BDY(t);
                   7076:           BDY(in) = (pointer)ptondv(CO,vv,pp);
                   7077:         }
                   7078:         if ( m ) ndv_mod(m,(NDV)BDY(in));
                   7079:     }
                   7080:     if ( in0 ) NEXT(in) = 0;
                   7081:
                   7082:     if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
                   7083:     else ndvf = ptondv(CO,vv,(P)f);
                   7084:     if ( m ) ndv_mod(m,ndvf);
                   7085:     ndf = (pointer)ndvtond(m,ndvf);
                   7086:
                   7087:     /* dont sort, dont removecont */
1.24      noro     7088:     ndv_setup(m,0,in0,1,1,0);
1.1       noro     7089:     nd_scale=2;
1.6       noro     7090:     stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1       noro     7091:     if ( !stat )
                   7092:         error("nd_nf_p : exponent too large");
                   7093:     if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
                   7094:     else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
                   7095: }
                   7096:
                   7097: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   7098: {
                   7099:     NM m;
                   7100:     UINT *t,*s;
                   7101:     int i;
                   7102:
                   7103:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   7104:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7105:         t = DL(m);
                   7106:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   7107:         r[i] = CM(m);
                   7108:     }
                   7109:     for ( i = 0; !r[i]; i++ );
                   7110:     return i;
                   7111: }
                   7112:
                   7113: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
                   7114: {
                   7115:     NM m;
                   7116:     UINT *t,*s;
                   7117:     int i;
                   7118:
                   7119:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   7120:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7121:         t = DL(m);
                   7122:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     7123:         r[i] = CZ(m);
1.1       noro     7124:     }
                   7125:     for ( i = 0; !r[i]; i++ );
                   7126:     return i;
                   7127: }
                   7128:
                   7129: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
                   7130: {
                   7131:     NM m;
                   7132:     UINT *t,*s;
                   7133:     int i;
                   7134:
                   7135:     for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
                   7136:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7137:         t = DL(m);
                   7138:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   7139:         mpz_set(r[i],BDY(CZ(m)));
                   7140:     }
                   7141:     for ( i = 0; !mpz_sgn(r[i]); i++ );
                   7142:     return i;
                   7143: }
                   7144:
                   7145: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
                   7146: {
                   7147:     NM m;
                   7148:     unsigned long *v;
                   7149:     int i,j,h,size;
                   7150:   UINT *s,*t;
                   7151:
                   7152:   size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
                   7153:     v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
                   7154:     bzero(v,size);
                   7155:     for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
                   7156:     t = DL(m);
                   7157:     h = ndl_hash_value(t);
                   7158:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   7159:       v[i/BLEN] |= 1L <<(i%BLEN);
                   7160:     }
                   7161:     return v;
                   7162: }
                   7163:
                   7164: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
                   7165: {
                   7166:     NMV mr;
                   7167:     UINT *d,*t,*s;
                   7168:     int i,j,len,h,head;
                   7169:
                   7170:     d = DL(m);
                   7171:     len = LEN(p);
                   7172:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   7173:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   7174:         ndl_add(d,DL(mr),t);
                   7175:     h = ndl_hash_value(t);
                   7176:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   7177:     if ( j == 0 ) head = i;
                   7178:       v[i/BLEN] |= 1L <<(i%BLEN);
                   7179:     }
                   7180:     return head;
                   7181: }
                   7182:
                   7183: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   7184: {
                   7185:     NM m;
                   7186:     NMV mr;
                   7187:     UINT *d,*t,*s;
                   7188:     NDV p;
                   7189:     int i,j,len;
                   7190:     Z *r;
                   7191:
                   7192:     m = pair->mul;
                   7193:     d = DL(m);
                   7194:     p = nd_ps[pair->index];
                   7195:     len = LEN(p);
                   7196:     r = (Z *)CALLOC(n,sizeof(Q));
                   7197:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   7198:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   7199:         ndl_add(d,DL(mr),t);
                   7200:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     7201:         r[i] = CZ(mr);
1.1       noro     7202:     }
                   7203:     return r;
                   7204: }
                   7205:
1.11      noro     7206: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1       noro     7207: {
                   7208:     NM m;
                   7209:     NMV mr;
1.11      noro     7210:     UINT *d,*t,*s,*u;
1.1       noro     7211:     NDV p;
                   7212:     unsigned char *ivc;
                   7213:     unsigned short *ivs;
                   7214:     UINT *v,*ivi,*s0v;
1.11      noro     7215:     int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1       noro     7216:     IndArray r;
                   7217:
                   7218:     m = pair->mul;
                   7219:     d = DL(m);
                   7220:     if ( trace )
                   7221:       p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
                   7222:     else
                   7223:       p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
                   7224:
                   7225:     len = LEN(p);
                   7226:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   7227:     v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11      noro     7228:     for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
                   7229:       ndl_add(d,DL(mr),t);
                   7230:       st = prev;
                   7231:       ed = n;
                   7232:       while ( ed > st ) {
                   7233:         md = (st+ed)/2;
                   7234:         u = s0+md*nd_wpd;
                   7235:         c = DL_COMPARE(u,t);
                   7236:         if ( c == 0 ) break;
                   7237:         else if ( c > 0 ) st = md;
                   7238:         else ed = md;
                   7239:       }
                   7240:       prev = v[j] = md;
1.1       noro     7241:     }
                   7242:     r = (IndArray)MALLOC(sizeof(struct oIndArray));
                   7243:     r->head = v[0];
                   7244:     diff = 0;
                   7245:     for ( i = 1; i < len; i++ ) {
                   7246:         cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   7247:     }
                   7248:     if ( diff < 256 ) {
                   7249:         r->width = 1;
                   7250:         ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   7251:         r->index.c = ivc;
                   7252:         for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   7253:     } else if ( diff < 65536 ) {
                   7254:         r->width = 2;
                   7255:         ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   7256:         r->index.s = ivs;
                   7257:         for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   7258:     } else {
                   7259:         r->width = 4;
                   7260:         ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   7261:         r->index.i = ivi;
                   7262:         for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
                   7263:     }
                   7264:     return r;
                   7265: }
                   7266:
                   7267: int compress_array(Z *svect,Z *cvect,int n)
                   7268: {
                   7269:     int i,j;
                   7270:
                   7271:     for ( i = j = 0; i < n; i++ )
                   7272:         if ( svect[i] ) cvect[j++] = svect[i];
                   7273:     return j;
                   7274: }
                   7275:
                   7276: void expand_array(Z *svect,Z *cvect,int n)
                   7277: {
                   7278:     int i,j;
                   7279:
                   7280:     for ( i = j = 0; j < n;  i++  )
                   7281:         if ( svect[i] ) svect[i] = cvect[j++];
                   7282: }
                   7283:
1.8       noro     7284: #if 0
1.1       noro     7285: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7286: {
                   7287:     int i,j,k,len,pos,prev,nz;
                   7288:     Z cs,mcs,c1,c2,cr,gcd,t;
                   7289:     IndArray ivect;
                   7290:     unsigned char *ivc;
                   7291:     unsigned short *ivs;
                   7292:     unsigned int *ivi;
                   7293:     NDV redv;
                   7294:     NMV mr;
                   7295:     NODE rp;
                   7296:     int maxrs;
                   7297:     double hmag;
                   7298:     Z *cvect;
1.3       noro     7299:     int l;
1.1       noro     7300:
                   7301:     maxrs = 0;
                   7302:     for ( i = 0; i < col && !svect[i]; i++ );
                   7303:     if ( i == col ) return maxrs;
                   7304:     hmag = p_mag((P)svect[i])*nd_scale;
                   7305:     cvect = (Z *)MALLOC(col*sizeof(Q));
                   7306:     for ( i = 0; i < nred; i++ ) {
                   7307:         ivect = imat[i];
                   7308:         k = ivect->head;
                   7309:         if ( svect[k] ) {
                   7310:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7311:             redv = nd_demand?ndv_load(rp0[i]->index)
                   7312:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   7313:             len = LEN(redv); mr = BDY(redv);
1.6       noro     7314:             igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1       noro     7315:             chsgnz(cs,&mcs);
                   7316:             if ( !UNIQ(cr) ) {
                   7317:                 for ( j = 0; j < col; j++ ) {
                   7318:                     mulz(svect[j],cr,&c1); svect[j] = c1;
                   7319:                 }
                   7320:             }
                   7321:             svect[k] = 0; prev = k;
                   7322:             switch ( ivect->width ) {
                   7323:                 case 1:
                   7324:                     ivc = ivect->index.c;
                   7325:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7326:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     7327:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     7328:                     }
                   7329:                     break;
                   7330:                 case 2:
                   7331:                     ivs = ivect->index.s;
                   7332:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7333:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     7334:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     7335:                     }
                   7336:                     break;
                   7337:                 case 4:
                   7338:                     ivi = ivect->index.i;
                   7339:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7340:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     7341:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     7342:                     }
                   7343:                     break;
                   7344:             }
                   7345:             for ( j = k+1; j < col && !svect[j]; j++ );
                   7346:             if ( j == col ) break;
                   7347:             if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
                   7348:                 nz = compress_array(svect,cvect,col);
                   7349:                 removecont_array((P *)cvect,nz,1);
                   7350:                 expand_array(svect,cvect,nz);
                   7351:                 hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   7352:             }
                   7353:         }
                   7354:     }
                   7355:     nz = compress_array(svect,cvect,col);
                   7356:     removecont_array((P *)cvect,nz,1);
                   7357:     expand_array(svect,cvect,nz);
                   7358:     if ( DP_Print ) {
                   7359:         fprintf(asir_out,"-"); fflush(asir_out);
                   7360:     }
                   7361:     return maxrs;
                   7362: }
1.4       noro     7363: #else
1.9       noro     7364:
1.4       noro     7365: /* direct mpz version */
                   7366: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7367: {
                   7368:     int i,j,k,len,pos,prev;
                   7369:     mpz_t cs,cr,gcd;
                   7370:     IndArray ivect;
                   7371:     unsigned char *ivc;
                   7372:     unsigned short *ivs;
                   7373:     unsigned int *ivi;
                   7374:     NDV redv;
                   7375:     NMV mr;
                   7376:     NODE rp;
                   7377:     int maxrs;
                   7378:     double hmag;
                   7379:     int l;
1.13      noro     7380:     static mpz_t *svect;
                   7381:     static int svect_len=0;
1.4       noro     7382:
                   7383:     maxrs = 0;
                   7384:     for ( i = 0; i < col && !svect0[i]; i++ );
                   7385:     if ( i == col ) return maxrs;
                   7386:     hmag = p_mag((P)svect0[i])*nd_scale;
1.13      noro     7387:     if ( col > svect_len ) {
                   7388:       svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   7389:       svect_len = col;
                   7390:     }
1.4       noro     7391:     for ( i = 0; i < col; i++ ) {
                   7392:       mpz_init(svect[i]);
                   7393:       if ( svect0[i] )
                   7394:         mpz_set(svect[i],BDY(svect0[i]));
                   7395:       else
                   7396:         mpz_set_ui(svect[i],0);
                   7397:     }
                   7398:     mpz_init(gcd); mpz_init(cs); mpz_init(cr);
                   7399:     for ( i = 0; i < nred; i++ ) {
                   7400:         ivect = imat[i];
                   7401:         k = ivect->head;
                   7402:         if ( mpz_sgn(svect[k]) ) {
                   7403:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7404:             redv = nd_demand?ndv_load(rp0[i]->index)
                   7405:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   7406:             len = LEN(redv); mr = BDY(redv);
1.6       noro     7407:             mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4       noro     7408:             mpz_div(cs,svect[k],gcd);
1.6       noro     7409:             mpz_div(cr,BDY(CZ(mr)),gcd);
1.4       noro     7410:             mpz_neg(cs,cs);
1.9       noro     7411:             if ( MUNIMPZ(cr) )
                   7412:               for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
                   7413:             else if ( !UNIMPZ(cr) )
                   7414:               for ( j = 0; j < col; j++ ) {
                   7415:                 if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
                   7416:               }
1.4       noro     7417:             mpz_set_ui(svect[k],0);
                   7418:             prev = k;
                   7419:             switch ( ivect->width ) {
                   7420:                 case 1:
                   7421:                     ivc = ivect->index.c;
                   7422:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7423:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     7424:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     7425:                     }
                   7426:                     break;
                   7427:                 case 2:
                   7428:                     ivs = ivect->index.s;
                   7429:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7430:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     7431:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     7432:                     }
                   7433:                     break;
                   7434:                 case 4:
                   7435:                     ivi = ivect->index.i;
                   7436:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7437:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     7438:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     7439:                     }
                   7440:                     break;
                   7441:             }
                   7442:             for ( j = k+1; j < col && !svect[j]; j++ );
                   7443:             if ( j == col ) break;
                   7444:             if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
                   7445:                 mpz_removecont_array(svect,col);
                   7446:                 hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
                   7447:             }
                   7448:         }
                   7449:     }
                   7450:     mpz_removecont_array(svect,col);
                   7451:     if ( DP_Print ) {
                   7452:         fprintf(asir_out,"-"); fflush(asir_out);
                   7453:     }
                   7454:     for ( i = 0; i < col; i++ )
                   7455:       if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
                   7456:       else svect0[i] = 0;
                   7457:     return maxrs;
                   7458: }
                   7459: #endif
1.1       noro     7460:
                   7461: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7462: {
                   7463:     int i,j,k,len,pos,prev;
                   7464:     UINT c,c1,c2,c3,up,lo,dmy;
                   7465:     IndArray ivect;
                   7466:     unsigned char *ivc;
                   7467:     unsigned short *ivs;
                   7468:     unsigned int *ivi;
                   7469:     NDV redv;
                   7470:     NMV mr;
                   7471:     NODE rp;
                   7472:     int maxrs;
                   7473:
                   7474:     maxrs = 0;
                   7475:     for ( i = 0; i < nred; i++ ) {
                   7476:         ivect = imat[i];
                   7477:         k = ivect->head; svect[k] %= m;
                   7478:         if ( (c = svect[k]) != 0 ) {
                   7479:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7480:             c = m-c; redv = nd_ps[rp0[i]->index];
                   7481:             len = LEN(redv); mr = BDY(redv);
                   7482:             svect[k] = 0; prev = k;
                   7483:             switch ( ivect->width ) {
                   7484:                 case 1:
                   7485:                     ivc = ivect->index.c;
                   7486:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7487:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
                   7488:             if ( c1 ) {
                   7489:               c2 = svect[pos];
                   7490:                           DMA(c1,c,c2,up,lo);
                   7491:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   7492:                           } else svect[pos] = lo;
                   7493:             }
                   7494:                     }
                   7495:                     break;
                   7496:                 case 2:
                   7497:                     ivs = ivect->index.s;
                   7498:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7499:                         pos = prev+ivs[j]; c1 = CM(mr);
                   7500:                         prev = pos;
                   7501:             if ( c1 ) {
                   7502:               c2 = svect[pos];
                   7503:                           DMA(c1,c,c2,up,lo);
                   7504:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   7505:                           } else svect[pos] = lo;
                   7506:             }
                   7507:                     }
                   7508:                     break;
                   7509:                 case 4:
                   7510:                     ivi = ivect->index.i;
                   7511:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7512:                         pos = prev+ivi[j]; c1 = CM(mr);
                   7513:                         prev = pos;
                   7514:             if ( c1 ) {
                   7515:               c2 = svect[pos];
                   7516:                           DMA(c1,c,c2,up,lo);
                   7517:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   7518:                           } else svect[pos] = lo;
                   7519:             }
                   7520:                     }
                   7521:                     break;
                   7522:             }
                   7523:         }
                   7524:     }
                   7525:     for ( i = 0; i < col; i++ )
                   7526:         if ( svect[i] >= (UINT)m ) svect[i] %= m;
                   7527:     return maxrs;
                   7528: }
                   7529:
                   7530: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7531: {
                   7532:     int i,j,k,len,pos,prev;
                   7533:     UINT c,c1,c2,c3,up,lo,dmy;
                   7534:     IndArray ivect;
                   7535:     unsigned char *ivc;
                   7536:     unsigned short *ivs;
                   7537:     unsigned int *ivi;
                   7538:     NDV redv;
                   7539:     NMV mr;
                   7540:     NODE rp;
                   7541:     int maxrs;
                   7542:
                   7543:     maxrs = 0;
                   7544:     for ( i = 0; i < nred; i++ ) {
                   7545:         ivect = imat[i];
                   7546:         k = ivect->head;
                   7547:         if ( (c = svect[k]) != 0 ) {
                   7548:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7549:             c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
                   7550:             len = LEN(redv); mr = BDY(redv);
                   7551:             svect[k] = 0; prev = k;
                   7552:             switch ( ivect->width ) {
                   7553:                 case 1:
                   7554:                     ivc = ivect->index.c;
                   7555:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7556:                         pos = prev+ivc[j]; prev = pos;
                   7557:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   7558:                     }
                   7559:                     break;
                   7560:                 case 2:
                   7561:                     ivs = ivect->index.s;
                   7562:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7563:                         pos = prev+ivs[j]; prev = pos;
                   7564:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   7565:                     }
                   7566:                     break;
                   7567:                 case 4:
                   7568:                     ivi = ivect->index.i;
                   7569:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7570:                         pos = prev+ivi[j]; prev = pos;
                   7571:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   7572:                     }
                   7573:                     break;
                   7574:             }
                   7575:         }
                   7576:     }
                   7577:     return maxrs;
                   7578: }
                   7579:
                   7580: ND nd_add_lf(ND p1,ND p2)
                   7581: {
                   7582:     int n,c,can;
                   7583:     ND r;
                   7584:     NM m1,m2,mr0,mr,s;
                   7585:     Z t;
                   7586:
                   7587:     if ( !p1 ) return p2;
                   7588:     else if ( !p2 ) return p1;
                   7589:     else {
                   7590:         can = 0;
                   7591:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   7592:             c = DL_COMPARE(DL(m1),DL(m2));
                   7593:             switch ( c ) {
                   7594:                 case 0:
                   7595:                     addlf(CZ(m1),CZ(m2),&t);
                   7596:                     s = m1; m1 = NEXT(m1);
                   7597:                     if ( t ) {
                   7598:                         can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
                   7599:                     } else {
                   7600:                         can += 2; FREENM(s);
                   7601:                     }
                   7602:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   7603:                     break;
                   7604:                 case 1:
                   7605:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   7606:                     break;
                   7607:                 case -1:
                   7608:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   7609:                     break;
                   7610:             }
                   7611:         }
                   7612:         if ( !mr0 )
                   7613:             if ( m1 ) mr0 = m1;
                   7614:             else if ( m2 ) mr0 = m2;
                   7615:             else return 0;
                   7616:         else if ( m1 ) NEXT(mr) = m1;
                   7617:         else if ( m2 ) NEXT(mr) = m2;
                   7618:         else NEXT(mr) = 0;
                   7619:         BDY(p1) = mr0;
                   7620:         SG(p1) = MAX(SG(p1),SG(p2));
                   7621:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   7622:         FREEND(p2);
                   7623:         return p1;
                   7624:     }
                   7625: }
                   7626:
                   7627: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7628: {
                   7629:     int i,j,k,len,pos,prev;
                   7630:     mpz_t c,mc,c1;
                   7631:     IndArray ivect;
                   7632:     unsigned char *ivc;
                   7633:     unsigned short *ivs;
                   7634:     unsigned int *ivi;
                   7635:     NDV redv;
                   7636:     NMV mr;
                   7637:     NODE rp;
                   7638:     int maxrs;
                   7639:
                   7640:     maxrs = 0;
                   7641:     lf_lazy = 1;
                   7642:     for ( i = 0; i < nred; i++ ) {
                   7643:         ivect = imat[i];
                   7644:         k = ivect->head;
                   7645:         mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
                   7646:         if ( mpz_sgn(svect[k]) ) {
                   7647:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7648:             mpz_neg(svect[k],svect[k]);
                   7649:             redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
                   7650:             len = LEN(redv); mr = BDY(redv);
                   7651:             prev = k;
                   7652:             switch ( ivect->width ) {
                   7653:                 case 1:
                   7654:                     ivc = ivect->index.c;
                   7655:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7656:                         pos = prev+ivc[j]; prev = pos;
                   7657:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   7658:                     }
                   7659:                     break;
                   7660:                 case 2:
                   7661:                     ivs = ivect->index.s;
                   7662:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7663:                         pos = prev+ivs[j]; prev = pos;
                   7664:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   7665:                     }
                   7666:                     break;
                   7667:                 case 4:
                   7668:                     ivi = ivect->index.i;
                   7669:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7670:                         pos = prev+ivi[j]; prev = pos;
                   7671:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   7672:                     }
                   7673:                     break;
                   7674:             }
                   7675:             mpz_set_ui(svect[k],0);
                   7676:         }
                   7677:     }
                   7678:     lf_lazy=0;
                   7679:     for ( i = 0; i < col; i++ ) {
                   7680:         mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
                   7681:     }
                   7682:     return maxrs;
                   7683: }
                   7684:
                   7685: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
                   7686: {
                   7687:     int i,j,k,l,rank,s;
                   7688:     mpz_t a,a1,inv;
                   7689:     mpz_t *t,*pivot,*pk;
                   7690:     mpz_t **mat;
                   7691:     struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
                   7692:     int size,size1;
                   7693:
                   7694:     mpz_init(inv);
                   7695:     mpz_init(a);
                   7696:     mat = (mpz_t **)mat0;
                   7697:         size = 0;
                   7698:     for ( rank = 0, j = 0; j < col; j++ ) {
                   7699:         for ( i = rank; i < row; i++ ) {
                   7700:             mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
                   7701:         }
                   7702:         for ( i = rank; i < row; i++ )
                   7703:             if ( mpz_sgn(mat[i][j]) )
                   7704:                 break;
                   7705:         if ( i == row ) {
                   7706:             colstat[j] = 0;
                   7707:             continue;
                   7708:         } else
                   7709:             colstat[j] = 1;
                   7710:         if ( i != rank ) {
                   7711:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   7712:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   7713:         }
                   7714:         pivot = mat[rank];
                   7715:         s = sugar[rank];
                   7716:         mpz_invert(inv,pivot[j],BDY(current_mod_lf));
                   7717:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   7718:             if ( mpz_sgn(*pk) ) {
                   7719:                 mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
                   7720:             }
                   7721:         for ( i = rank+1; i < row; i++ ) {
                   7722:             t = mat[i];
                   7723:             if ( mpz_sgn(t[j]) ) {
                   7724:                 sugar[i] = MAX(sugar[i],s);
                   7725:                 mpz_neg(a,t[j]);
                   7726:                 red_by_vect_lf(t+j,pivot+j,a,col-j);
                   7727:             }
                   7728:         }
                   7729:         rank++;
                   7730:     }
                   7731:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   7732:         if ( colstat[j] ) {
                   7733:             pivot = mat[l];
                   7734:             s = sugar[l];
                   7735:             for ( k = j; k < col; k++ )
                   7736:               mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
                   7737:             for ( i = 0; i < l; i++ ) {
                   7738:                 t = mat[i];
                   7739:                 if ( mpz_sgn(t[j]) ) {
                   7740:                     sugar[i] = MAX(sugar[i],s);
                   7741:                     mpz_neg(a,t[j]);
                   7742:                     red_by_vect_lf(t+j,pivot+j,a,col-j);
                   7743:                 }
                   7744:             }
                   7745:             l--;
                   7746:         }
                   7747:     for ( j = 0, l = 0; l < rank; j++ )
                   7748:         if ( colstat[j] ) {
                   7749:             t = mat[l];
                   7750:             for ( k = j; k < col; k++ ) {
                   7751:                 mpz_mod(t[k],t[k],BDY(current_mod_lf));
                   7752:             }
                   7753:             l++;
                   7754:         }
                   7755:     return rank;
                   7756: }
                   7757:
                   7758:
                   7759: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   7760: {
                   7761:     int j,k,len;
                   7762:     UINT *p;
                   7763:     UINT c;
                   7764:     NDV r;
                   7765:     NMV mr0,mr;
                   7766:
                   7767:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   7768:     if ( !len ) return 0;
                   7769:     else {
                   7770:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   7771: #if 0
                   7772:         ndv_alloc += nmv_adv*len;
                   7773: #endif
                   7774:         mr = mr0;
                   7775:         p = s0vect;
                   7776:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   7777:             if ( !rhead[j] ) {
                   7778:                 if ( (c = vect[k++]) != 0 ) {
                   7779:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   7780:                 }
                   7781:             }
                   7782:         MKNDV(nd_nvar,mr0,len,r);
                   7783:         return r;
                   7784:     }
                   7785: }
                   7786:
                   7787: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
                   7788: {
                   7789:     int j,k,len;
                   7790:     UINT *p;
                   7791:     NDV r;
                   7792:     NMV mr0,mr;
                   7793:
                   7794:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
                   7795:     if ( !len ) return 0;
                   7796:     else {
                   7797:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   7798:         mr = mr0;
                   7799:         p = s0vect;
                   7800:         for ( j = 0; j < col; j++, p += nd_wpd )
                   7801:       if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
                   7802:             ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
                   7803:           }
                   7804:         MKNDV(nd_nvar,mr0,len,r);
                   7805:         return r;
                   7806:     }
                   7807: }
                   7808:
                   7809: /* for preprocessed vector */
                   7810:
                   7811: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   7812: {
1.6       noro     7813:   int j,k,len;
                   7814:   UINT *p;
                   7815:   Z c;
                   7816:   NDV r;
                   7817:   NMV mr0,mr;
1.1       noro     7818:
1.6       noro     7819:   for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   7820:   if ( !len ) return 0;
                   7821:   else {
                   7822:     mr0 = (NMV)MALLOC(nmv_adv*len);
1.1       noro     7823: #if 0
1.6       noro     7824:     ndv_alloc += nmv_adv*len;
1.1       noro     7825: #endif
1.6       noro     7826:     mr = mr0;
                   7827:     p = s0vect;
                   7828:     for ( j = k = 0; j < col; j++, p += nd_wpd ) {
                   7829:       if ( !rhead[j] ) {
                   7830:         if ( (c = vect[k++]) != 0 ) {
                   7831:           if ( !INT(c) )
                   7832:             error("vect_to_ndv_q : components must be integers");
                   7833:             ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
                   7834:         }
                   7835:       }
1.1       noro     7836:     }
1.6       noro     7837:     MKNDV(nd_nvar,mr0,len,r);
                   7838:     return r;
                   7839:   }
1.1       noro     7840: }
                   7841:
                   7842: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   7843: {
                   7844:     int j,k,len;
                   7845:     UINT *p;
                   7846:     mpz_t c;
                   7847:     NDV r;
                   7848:     NMV mr0,mr;
                   7849:
                   7850:     for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
                   7851:     if ( !len ) return 0;
                   7852:     else {
                   7853:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   7854: #if 0
                   7855:         ndv_alloc += nmv_adv*len;
                   7856: #endif
                   7857:         mr = mr0;
                   7858:         p = s0vect;
                   7859:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   7860:             if ( !rhead[j] ) {
                   7861:                 c[0] = vect[k++][0];
                   7862:                 if ( mpz_sgn(c) ) {
                   7863:                     ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
                   7864:                 }
                   7865:             }
                   7866:         MKNDV(nd_nvar,mr0,len,r);
                   7867:         return r;
                   7868:     }
                   7869: }
                   7870:
                   7871: /* for plain vector */
                   7872:
                   7873: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
                   7874: {
                   7875:     int j,k,len;
                   7876:     UINT *p;
                   7877:     Z c;
                   7878:     NDV r;
                   7879:     NMV mr0,mr;
                   7880:
                   7881:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   7882:     if ( !len ) return 0;
                   7883:     else {
                   7884:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   7885: #if 0
                   7886:         ndv_alloc += nmv_adv*len;
                   7887: #endif
                   7888:         mr = mr0;
                   7889:         p = s0vect;
                   7890:         for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   7891:             if ( (c = vect[k]) != 0 ) {
                   7892:                 if ( !INT(c) )
1.6       noro     7893:                     error("plain_vect_to_ndv_q : components must be integers");
                   7894:                 ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1       noro     7895:             }
                   7896:         MKNDV(nd_nvar,mr0,len,r);
                   7897:         return r;
                   7898:     }
                   7899: }
                   7900:
                   7901: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
                   7902: {
                   7903:     ND_pairs t;
                   7904:     NODE sp0,sp;
                   7905:     int stat;
                   7906:     ND spol;
                   7907:
                   7908:     for ( t = l; t; t = NEXT(t) ) {
                   7909:         stat = nd_sp(m,trace,t,&spol);
                   7910:         if ( !stat ) return 0;
                   7911:         if ( spol ) {
                   7912:             add_pbucket_symbolic(bucket,spol);
                   7913:         }
                   7914:     }
                   7915:     return 1;
                   7916: }
                   7917:
                   7918: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   7919: {
                   7920:     NODE rp0,rp;
                   7921:     NM mul,head,s0,s;
                   7922:     int index,col,i,sugar;
                   7923:     RHist h;
                   7924:     UINT *s0v,*p;
                   7925:     NM_ind_pair pair;
                   7926:     ND red;
                   7927:     NDV *ps;
                   7928:
                   7929:     s0 = 0; rp0 = 0; col = 0;
                   7930:   if ( nd_demand )
                   7931:       ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   7932:   else
                   7933:       ps = trace?nd_ps_trace:nd_ps;
                   7934:     while ( 1 ) {
                   7935:         head = remove_head_pbucket_symbolic(bucket);
                   7936:         if ( !head ) break;
                   7937:         if ( !s0 ) s0 = head;
                   7938:         else NEXT(s) = head;
                   7939:         s = head;
                   7940:         index = ndl_find_reducer(DL(head));
                   7941:         if ( index >= 0 ) {
                   7942:             h = nd_psh[index];
                   7943:             NEWNM(mul);
                   7944:             ndl_sub(DL(head),DL(h),DL(mul));
                   7945:             if ( ndl_check_bound2(index,DL(mul)) )
                   7946:                 return 0;
                   7947:             sugar = TD(DL(mul))+SG(ps[index]);
1.28      noro     7948:             MKNM_ind_pair(pair,mul,index,sugar,0);
1.1       noro     7949:             red = ndv_mul_nm_symbolic(mul,ps[index]);
                   7950:             add_pbucket_symbolic(bucket,nd_remove_head(red));
                   7951:             NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   7952:         }
                   7953:         col++;
                   7954:     }
                   7955:     if ( rp0 ) NEXT(rp) = 0;
                   7956:     NEXT(s) = 0;
                   7957:     s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   7958:     for ( i = 0, p = s0v, s = s0; i < col;
                   7959:         i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   7960:     *s0vect = s0v;
                   7961:     *r = rp0;
                   7962:     return col;
                   7963: }
                   7964:
                   7965: void print_ndp(ND_pairs l)
                   7966: {
                   7967:   ND_pairs t;
                   7968:
                   7969:   for ( t = l; t; t = NEXT(t) )
                   7970:     printf("[%d,%d] ",t->i1,t->i2);
                   7971:   printf("\n");
                   7972: }
                   7973:
                   7974: NODE nd_f4(int m,int checkonly,int **indp)
                   7975: {
                   7976:     int i,nh,stat,index,f4red;
                   7977:     NODE r,g,tn0,tn,node;
                   7978:     ND_pairs d,l,t,ll0,ll,lh;
                   7979:   LIST l0,l1;
                   7980:     ND spol,red;
                   7981:     NDV nf,redv;
                   7982:     NM s0,s;
                   7983:     NODE rp0,srp0,nflist,nzlist,nzlist_t;
                   7984:     int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
                   7985:     UINT c;
                   7986:     UINT **spmat;
                   7987:     UINT *s0vect,*svect,*p,*v;
                   7988:     int *colstat;
                   7989:     IndArray *imat;
                   7990:     int *rhead;
                   7991:     int spcol,sprow;
                   7992:     int sugar,sugarh;
                   7993:     PGeoBucket bucket;
                   7994:     struct oEGT eg0,eg1,eg_f4;
                   7995:     Z i1,i2,sugarq;
1.12      noro     7996:
                   7997:     init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1       noro     7998: #if 0
                   7999:     ndv_alloc = 0;
                   8000: #endif
1.11      noro     8001:     Nf4_red=0;
1.1       noro     8002:     g = 0; d = 0;
                   8003:     for ( i = 0; i < nd_psn; i++ ) {
                   8004:         d = update_pairs(d,g,i,0);
                   8005:         g = update_base(g,i);
                   8006:     }
                   8007:   nzlist = 0;
                   8008:     nzlist_t = nd_nzlist;
                   8009:     f4red = 1;
                   8010:     nd_last_nonzero = 0;
                   8011:     while ( d ) {
                   8012:         get_eg(&eg0);
                   8013:         l = nd_minsugarp(d,&d);
                   8014:         sugar = nd_sugarweight?l->sugar2:SG(l);
                   8015:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   8016:         if ( nzlist_t ) {
                   8017:             node = BDY((LIST)BDY(nzlist_t));
1.6       noro     8018:             sugarh = ZTOS((Q)ARG0(node));
1.1       noro     8019:             tn = BDY((LIST)ARG1(node));
                   8020:             if ( !tn ) {
                   8021:               nzlist_t = NEXT(nzlist_t);
                   8022:               continue;
                   8023:             }
                   8024:             /* tn = [[i1,i2],...] */
                   8025:             lh = nd_ipairtospair(tn);
                   8026:         }
                   8027:         bucket = create_pbucket();
                   8028:         stat = nd_sp_f4(m,0,l,bucket);
                   8029:         if ( !stat ) {
                   8030:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8031:             NEXT(t) = d; d = l;
                   8032:             d = nd_reconstruct(0,d);
                   8033:             continue;
                   8034:         }
                   8035:         if ( bucket->m < 0 ) continue;
                   8036:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   8037:         if ( !col ) {
                   8038:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8039:             NEXT(t) = d; d = l;
                   8040:             d = nd_reconstruct(0,d);
                   8041:             continue;
                   8042:         }
1.12      noro     8043:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1       noro     8044:         if ( DP_Print )
1.6       noro     8045:             fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5       noro     8046:                 sugar,eg_f4.exectime);
1.1       noro     8047:         nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
                   8048:         if ( checkonly && nflist ) return 0;
                   8049:         /* adding new bases */
                   8050:         if ( nflist ) nd_last_nonzero = f4red;
                   8051:         for ( r = nflist; r; r = NEXT(r) ) {
                   8052:             nf = (NDV)BDY(r);
1.38      noro     8053:             if ( nd_f4_td ) SG(nf) = nd_tdeg(nf);
1.1       noro     8054:             ndv_removecont(m,nf);
                   8055:             if ( !m && nd_nalg ) {
                   8056:                 ND nf1;
                   8057:
                   8058:                 nf1 = ndvtond(m,nf);
                   8059:                 nd_monic(0,&nf1);
                   8060:                 nd_removecont(m,nf1);
                   8061:                 nf = ndtondv(m,nf1);
                   8062:             }
1.24      noro     8063:             nh = ndv_newps(m,nf,0);
1.1       noro     8064:             d = update_pairs(d,g,nh,0);
                   8065:             g = update_base(g,nh);
                   8066:         }
                   8067:         if ( DP_Print ) {
                   8068:           fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
                   8069:         }
                   8070:         if ( nd_gentrace ) {
                   8071:       for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
                   8072:         NEXTNODE(tn0,tn);
1.6       noro     8073:                 STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1       noro     8074:                 node = mknode(2,i1,i2); MKLIST(l0,node);
                   8075:         BDY(tn) = l0;
                   8076:       }
                   8077:       if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6       noro     8078:             STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1       noro     8079:             MKNODE(node,l1,nzlist); nzlist = node;
                   8080:         }
                   8081:         if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
                   8082:         f4red++;
                   8083:         if ( nd_f4red && f4red > nd_f4red ) break;
                   8084:         if ( nd_rank0 && !nflist ) break;
                   8085:     }
                   8086:     if ( nd_gentrace ) {
                   8087:     MKLIST(l0,reverse_node(nzlist));
                   8088:         MKNODE(nd_alltracelist,l0,0);
                   8089:     }
                   8090: #if 0
                   8091:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   8092: #endif
1.12      noro     8093:   if ( DP_Print ) {
                   8094:     fprintf(asir_out,"number of red=%d,",Nf4_red);
                   8095:     fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
                   8096:       f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
                   8097:   }
1.1       noro     8098:   conv_ilist(nd_demand,0,g,indp);
                   8099:     return g;
                   8100: }
                   8101:
                   8102: NODE nd_f4_trace(int m,int **indp)
                   8103: {
                   8104:     int i,nh,stat,index;
                   8105:     NODE r,g;
                   8106:     ND_pairs d,l,l0,t;
                   8107:     ND spol,red;
                   8108:     NDV nf,redv,nfqv,nfv;
                   8109:     NM s0,s;
                   8110:     NODE rp0,srp0,nflist;
                   8111:     int nsp,nred,col,rank,len,k,j,a;
                   8112:     UINT c;
                   8113:     UINT **spmat;
                   8114:     UINT *s0vect,*svect,*p,*v;
                   8115:     int *colstat;
                   8116:     IndArray *imat;
                   8117:     int *rhead;
                   8118:     int spcol,sprow;
                   8119:     int sugar;
                   8120:     PGeoBucket bucket;
                   8121:     struct oEGT eg0,eg1,eg_f4;
                   8122:
                   8123:     g = 0; d = 0;
                   8124:     for ( i = 0; i < nd_psn; i++ ) {
                   8125:         d = update_pairs(d,g,i,0);
                   8126:         g = update_base(g,i);
                   8127:     }
                   8128:     while ( d ) {
                   8129:         get_eg(&eg0);
                   8130:         l = nd_minsugarp(d,&d);
                   8131:         sugar = SG(l);
                   8132:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   8133:         bucket = create_pbucket();
                   8134:         stat = nd_sp_f4(m,0,l,bucket);
                   8135:         if ( !stat ) {
                   8136:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8137:             NEXT(t) = d; d = l;
                   8138:             d = nd_reconstruct(1,d);
                   8139:             continue;
                   8140:         }
                   8141:         if ( bucket->m < 0 ) continue;
                   8142:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   8143:         if ( !col ) {
                   8144:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8145:             NEXT(t) = d; d = l;
                   8146:             d = nd_reconstruct(1,d);
                   8147:             continue;
                   8148:         }
                   8149:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   8150:         if ( DP_Print )
1.3       noro     8151:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5       noro     8152:                 sugar,eg_f4.exectime);
1.1       noro     8153:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   8154:         if ( !l0 ) continue;
                   8155:         l = l0;
                   8156:
                   8157:         /* over Q */
                   8158:         bucket = create_pbucket();
                   8159:         stat = nd_sp_f4(0,1,l,bucket);
                   8160:         if ( !stat ) {
                   8161:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8162:             NEXT(t) = d; d = l;
                   8163:             d = nd_reconstruct(1,d);
                   8164:             continue;
                   8165:         }
                   8166:         if ( bucket->m < 0 ) continue;
                   8167:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   8168:         if ( !col ) {
                   8169:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8170:             NEXT(t) = d; d = l;
                   8171:             d = nd_reconstruct(1,d);
                   8172:             continue;
                   8173:         }
                   8174:         nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   8175:         /* adding new bases */
                   8176:         for ( r = nflist; r; r = NEXT(r) ) {
                   8177:             nfqv = (NDV)BDY(r);
                   8178:             ndv_removecont(0,nfqv);
1.6       noro     8179:             if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1       noro     8180:             if ( nd_nalg ) {
                   8181:                 ND nf1;
                   8182:
                   8183:                 nf1 = ndvtond(m,nfqv);
                   8184:                 nd_monic(0,&nf1);
                   8185:                 nd_removecont(0,nf1);
                   8186:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   8187:             }
                   8188:             nfv = ndv_dup(0,nfqv);
                   8189:             ndv_mod(m,nfv);
                   8190:             ndv_removecont(m,nfv);
1.24      noro     8191:             nh = ndv_newps(0,nfv,nfqv);
1.1       noro     8192:             d = update_pairs(d,g,nh,0);
                   8193:             g = update_base(g,nh);
                   8194:         }
                   8195:     }
                   8196: #if 0
                   8197:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   8198: #endif
                   8199:   conv_ilist(nd_demand,1,g,indp);
                   8200:     return g;
                   8201: }
                   8202:
                   8203: int rref(matrix mat,int *sugar)
                   8204: {
                   8205:   int row,col,i,j,k,l,s,wcol,wj;
                   8206:   unsigned long bj;
                   8207:   unsigned long **a;
                   8208:   unsigned long *ai,*ak,*as,*t;
                   8209:   int *pivot;
                   8210:
                   8211:   row = mat->row;
                   8212:   col = mat->col;
                   8213:   a = mat->a;
                   8214:   wcol = (col+BLEN-1)/BLEN;
                   8215:   pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   8216:   i = 0;
                   8217:   for ( j = 0; j < col; j++ ) {
                   8218:   wj = j/BLEN; bj = 1L<<(j%BLEN);
                   8219:     for ( k = i; k < row; k++ )
                   8220:     if ( a[k][wj] & bj ) break;
                   8221:     if ( k == row ) continue;
                   8222:   pivot[i] = j;
                   8223:     if ( k != i ) {
                   8224:    t = a[i]; a[i] = a[k]; a[k] = t;
                   8225:    s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
                   8226:   }
                   8227:   ai = a[i];
                   8228:     for ( k = i+1; k < row; k++ ) {
                   8229:     ak = a[k];
                   8230:     if ( ak[wj] & bj ) {
                   8231:       for ( l = wj; l < wcol; l++ )
                   8232:       ak[l] ^= ai[l];
                   8233:       sugar[k] = MAX(sugar[k],sugar[i]);
                   8234:     }
                   8235:   }
                   8236:   i++;
                   8237:   }
                   8238:   for ( k = i-1; k >= 0; k-- ) {
                   8239:     j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
                   8240:   ak = a[k];
                   8241:     for ( s = 0; s < k; s++ ) {
                   8242:     as = a[s];
                   8243:       if ( as[wj] & bj ) {
                   8244:         for ( l = wj; l < wcol; l++ )
                   8245:       as[l] ^= ak[l];
                   8246:       sugar[s] = MAX(sugar[s],sugar[k]);
                   8247:     }
                   8248:   }
                   8249:   }
                   8250:   return i;
                   8251: }
                   8252:
                   8253: void print_matrix(matrix mat)
                   8254: {
                   8255:   int row,col,i,j;
                   8256:   unsigned long *ai;
                   8257:
                   8258:   row = mat->row;
                   8259:   col = mat->col;
                   8260:   printf("%d x %d\n",row,col);
                   8261:   for ( i = 0; i < row; i++ ) {
                   8262:   ai = mat->a[i];
                   8263:     for ( j = 0; j < col; j++ ) {
                   8264:     if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
                   8265:     else putchar('0');
                   8266:   }
                   8267:   putchar('\n');
                   8268:   }
                   8269: }
                   8270:
                   8271: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
                   8272:
                   8273: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
                   8274: {
                   8275:   int row,col,wcol,wj,i,j;
                   8276:   unsigned long bj;
                   8277:   unsigned long *ai;
                   8278:   unsigned long **a;
                   8279:   int len;
                   8280:   int *pos;
                   8281:
                   8282:   row = mat->row;
                   8283:   col = mat->col;
                   8284:   wcol = (col+BLEN-1)/BLEN;
                   8285:   pos = (int *)MALLOC(wcol*sizeof(int));
                   8286:   bzero(pos,wcol*sizeof(int));
                   8287:   for ( i = j = 0; i < wcol; i++ )
                   8288:     if ( v[i] ) pos[j++] = i;;
                   8289:   len = j;
                   8290:   wj = rhead/BLEN;
                   8291:   bj = 1L<<rhead%BLEN;
                   8292:   a = mat->a;
                   8293:   for ( i = 0; i < row; i++ ) {
                   8294:   ai = a[i];
                   8295:     if ( ai[wj]&bj ) {
                   8296:     for ( j = 0; j < len; j++ )
                   8297:       ai[pos[j]] ^= v[pos[j]];
                   8298:     sugar[i] = MAX(sugar[i],rsugar);
                   8299:   }
                   8300:   }
                   8301: }
                   8302:
                   8303: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   8304: {
                   8305:     int nsp,nred,i,i0,k,rank,row;
                   8306:     NODE r0,rp;
                   8307:     ND_pairs sp;
                   8308:   ND spol;
                   8309:   NM_ind_pair rt;
                   8310:     int *s0hash;
                   8311:   UINT *s;
                   8312:   int *pivot,*sugar,*head;
                   8313:   matrix mat;
                   8314:     NM m;
                   8315:     NODE r;
                   8316:   struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
                   8317:   int rhead,rsugar,size;
                   8318:     unsigned long *v;
                   8319:
                   8320:     get_eg(&eg0);
                   8321:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   8322:     nred = length(rp0);
                   8323:     mat = alloc_matrix(nsp,col);
                   8324:     s0hash = (int *)MALLOC(col*sizeof(int));
                   8325:     for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
                   8326:         s0hash[i] = ndl_hash_value(s);
                   8327:
                   8328:   sugar = (int *)MALLOC(nsp*sizeof(int));
                   8329:   for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
                   8330:     nd_sp(2,0,sp,&spol);
                   8331:     if ( spol ) {
                   8332:         mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
                   8333:       sugar[i] = SG(spol);
                   8334:       i++;
                   8335:     }
                   8336:   }
                   8337:   mat->row = i;
                   8338:     if ( DP_Print ) {
                   8339:       fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
                   8340:     }
                   8341:   size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
                   8342:   v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
                   8343:     for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
                   8344:     rt = (NM_ind_pair)BDY(rp);
                   8345:     bzero(v,size);
                   8346:         rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
                   8347:     rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
                   8348:       red_by_vect_2(mat,sugar,v,rhead,rsugar);
                   8349:   }
                   8350:
                   8351:     get_eg(&eg1);
                   8352:     init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
                   8353:   rank = rref(mat,sugar);
                   8354:
                   8355:     for ( i = 0, r0 = 0; i < rank; i++ ) {
                   8356:       NEXTNODE(r0,r);
                   8357:     BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
                   8358:       SG((NDV)BDY(r)) = sugar[i];
                   8359:     }
                   8360:     if ( r0 ) NEXT(r) = 0;
                   8361:     get_eg(&eg2);
                   8362:     init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
                   8363:     if ( DP_Print ) {
                   8364:         fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5       noro     8365:       eg_elim1.exectime,eg_elim2.exectime);
1.1       noro     8366:         fflush(asir_out);
                   8367:   }
                   8368:     return r0;
                   8369: }
                   8370:
                   8371:
                   8372: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   8373: {
                   8374:     IndArray *imat;
1.11      noro     8375:     int nsp,nred,i,start;
1.1       noro     8376:     int *rhead;
                   8377:     NODE r0,rp;
                   8378:     ND_pairs sp;
                   8379:     NM_ind_pair *rvect;
                   8380:     UINT *s;
                   8381:     int *s0hash;
1.11      noro     8382:     struct oEGT eg0,eg1,eg_conv;
1.1       noro     8383:
                   8384:     if ( m == 2 && nd_rref2 )
                   8385:      return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
                   8386:
                   8387:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   8388:     nred = length(rp0);
                   8389:     imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   8390:     rhead = (int *)MALLOC(col*sizeof(int));
                   8391:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   8392:
                   8393:     /* construction of index arrays */
1.11      noro     8394:     get_eg(&eg0);
1.1       noro     8395:     if ( DP_Print ) {
1.11      noro     8396:       fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   8397:       fflush(asir_out);
1.1       noro     8398:     }
                   8399:     rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11      noro     8400:     for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1       noro     8401:         rvect[i] = (NM_ind_pair)BDY(rp);
1.11      noro     8402:         imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1       noro     8403:         rhead[imat[i]->head] = 1;
1.11      noro     8404:         start = imat[i]->head;
                   8405:     }
1.12      noro     8406:     get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11      noro     8407:     if ( DP_Print ) {
                   8408:       fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   8409:       fflush(asir_out);
1.1       noro     8410:     }
                   8411:     if ( m > 0 )
1.7       noro     8412: #if SIZEOF_LONG==8
1.1       noro     8413:         r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   8414: #else
                   8415:         r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   8416: #endif
                   8417:     else if ( m == -1 )
                   8418:         r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   8419:     else if ( m == -2 )
                   8420:         r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   8421:     else
                   8422:         r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   8423:     return r0;
                   8424: }
                   8425:
                   8426: /* for Fp, 2<=p<2^16 */
                   8427:
                   8428: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   8429:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   8430: {
                   8431:     int spcol,sprow,a;
                   8432:     int i,j,k,l,rank;
                   8433:     NODE r0,r;
                   8434:     ND_pairs sp;
                   8435:     ND spol;
                   8436:     UINT **spmat;
                   8437:     UINT *svect,*v;
                   8438:     int *colstat;
                   8439:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8440:     int maxrs;
                   8441:     int *spsugar;
                   8442:     ND_pairs *spactive;
                   8443:
                   8444:     spcol = col-nred;
                   8445:     get_eg(&eg0);
                   8446:     /* elimination (1st step) */
                   8447:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   8448:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   8449:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8450:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   8451:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8452:         nd_sp(m,0,sp,&spol);
                   8453:         if ( !spol ) continue;
                   8454:         nd_to_vect(m,s0vect,col,spol,svect);
                   8455:         if ( m == -1 )
                   8456:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   8457:         else
                   8458:             maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
                   8459:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   8460:         if ( i < col ) {
                   8461:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   8462:             for ( j = k = 0; j < col; j++ )
                   8463:                 if ( !rhead[j] ) v[k++] = svect[j];
                   8464:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8465:             if ( nz )
                   8466:             spactive[sprow] = sp;
                   8467:             sprow++;
                   8468:         }
                   8469:         nd_free(spol);
                   8470:     }
                   8471:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8472:     if ( DP_Print ) {
1.5       noro     8473:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8474:         fflush(asir_out);
                   8475:     }
                   8476:     /* free index arrays */
                   8477:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   8478:
                   8479:     /* elimination (2nd step) */
                   8480:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8481:     if ( m == -1 )
                   8482:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   8483:     else
                   8484:         rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   8485:     r0 = 0;
                   8486:     for ( i = 0; i < rank; i++ ) {
                   8487:         NEXTNODE(r0,r); BDY(r) =
                   8488:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   8489:         SG((NDV)BDY(r)) = spsugar[i];
                   8490:         GCFREE(spmat[i]);
                   8491:     }
                   8492:     if ( r0 ) NEXT(r) = 0;
                   8493:
                   8494:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   8495:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8496:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8497:     if ( DP_Print ) {
1.5       noro     8498:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8499:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8500:             nsp,nred,sprow,spcol,rank);
1.5       noro     8501:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     8502:     }
                   8503:     if ( nz ) {
                   8504:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   8505:         if ( rank > 0 ) {
                   8506:             NEXT(spactive[rank-1]) = 0;
                   8507:             *nz = spactive[0];
                   8508:         } else
                   8509:             *nz = 0;
                   8510:     }
                   8511:     return r0;
                   8512: }
                   8513:
                   8514:
                   8515: /* for small finite fields */
                   8516:
                   8517: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   8518:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   8519: {
                   8520:     int spcol,sprow,a;
                   8521:     int i,j,k,l,rank;
                   8522:     NODE r0,r;
                   8523:     ND_pairs sp;
                   8524:     ND spol;
                   8525:     UINT **spmat;
                   8526:     UINT *svect,*v;
                   8527:     int *colstat;
                   8528:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8529:     int maxrs;
                   8530:     int *spsugar;
                   8531:     ND_pairs *spactive;
                   8532:
                   8533:     spcol = col-nred;
                   8534:     get_eg(&eg0);
                   8535:     /* elimination (1st step) */
                   8536:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   8537:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   8538:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8539:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   8540:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8541:         nd_sp(m,0,sp,&spol);
                   8542:         if ( !spol ) continue;
                   8543:         nd_to_vect(m,s0vect,col,spol,svect);
                   8544:         maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   8545:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   8546:         if ( i < col ) {
                   8547:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   8548:             for ( j = k = 0; j < col; j++ )
                   8549:                 if ( !rhead[j] ) v[k++] = svect[j];
                   8550:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8551:             if ( nz )
                   8552:             spactive[sprow] = sp;
                   8553:             sprow++;
                   8554:         }
                   8555:         nd_free(spol);
                   8556:     }
                   8557:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8558:     if ( DP_Print ) {
1.5       noro     8559:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8560:         fflush(asir_out);
                   8561:     }
                   8562:     /* free index arrays */
                   8563:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   8564:
                   8565:     /* elimination (2nd step) */
                   8566:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8567:     rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   8568:     r0 = 0;
                   8569:     for ( i = 0; i < rank; i++ ) {
                   8570:         NEXTNODE(r0,r); BDY(r) =
                   8571:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   8572:         SG((NDV)BDY(r)) = spsugar[i];
                   8573:         GCFREE(spmat[i]);
                   8574:     }
                   8575:     if ( r0 ) NEXT(r) = 0;
                   8576:
                   8577:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   8578:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8579:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8580:     if ( DP_Print ) {
1.5       noro     8581:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8582:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8583:             nsp,nred,sprow,spcol,rank);
1.5       noro     8584:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     8585:     }
                   8586:     if ( nz ) {
                   8587:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   8588:         if ( rank > 0 ) {
                   8589:             NEXT(spactive[rank-1]) = 0;
                   8590:             *nz = spactive[0];
                   8591:         } else
                   8592:             *nz = 0;
                   8593:     }
                   8594:     return r0;
                   8595: }
                   8596:
                   8597: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   8598:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   8599: {
                   8600:     int spcol,sprow,a;
                   8601:     int i,j,k,l,rank;
                   8602:     NODE r0,r;
                   8603:     ND_pairs sp;
                   8604:     ND spol;
                   8605:     mpz_t **spmat;
                   8606:     mpz_t *svect,*v;
                   8607:     int *colstat;
                   8608:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8609:     int maxrs;
                   8610:     int *spsugar;
                   8611:     pointer *w;
                   8612:
                   8613:     spcol = col-nred;
                   8614:     get_eg(&eg0);
                   8615:     /* elimination (1st step) */
                   8616:     spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
                   8617:     svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   8618:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8619:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8620:         nd_sp(m,trace,sp,&spol);
                   8621:         if ( !spol ) continue;
                   8622:         nd_to_vect_lf(s0vect,col,spol,svect);
                   8623:         maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
                   8624:         for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
                   8625:         if ( i < col ) {
                   8626:             spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
                   8627:             for ( j = k = 0; j < col; j++ )
                   8628:                 if ( !rhead[j] ) v[k++][0] = svect[j][0];
                   8629:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8630:             sprow++;
                   8631:         }
                   8632: /*        nd_free(spol); */
                   8633:     }
                   8634:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8635:     if ( DP_Print ) {
1.5       noro     8636:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8637:         fflush(asir_out);
                   8638:     }
                   8639:     /* free index arrays */
                   8640: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   8641:
                   8642:     /* elimination (2nd step) */
                   8643:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8644:     rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
                   8645:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   8646:     for ( i = 0; i < rank; i++ ) {
                   8647: #if 0
                   8648:         w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   8649:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   8650: #else
                   8651:         w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   8652:         SG((NDV)w[i]) = spsugar[i];
                   8653: #endif
                   8654: /*        GCFREE(spmat[i]); */
                   8655:
                   8656:     }
                   8657: #if 0
                   8658:     qsort(w,rank,sizeof(NDV),
                   8659:         (int (*)(const void *,const void *))ndv_compare);
                   8660: #endif
                   8661:     r0 = 0;
                   8662:     for ( i = 0; i < rank; i++ ) {
                   8663:         NEXTNODE(r0,r); BDY(r) = w[i];
                   8664:     }
                   8665:     if ( r0 ) NEXT(r) = 0;
                   8666:
                   8667: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   8668:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8669:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8670:     if ( DP_Print ) {
1.5       noro     8671:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8672:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8673:             nsp,nred,sprow,spcol,rank);
1.5       noro     8674:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     8675:     }
                   8676:     return r0;
                   8677: }
                   8678:
                   8679: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   8680:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   8681: {
                   8682:     int spcol,sprow,a;
                   8683:     int i,j,k,l,rank;
                   8684:     NODE r0,r;
                   8685:     ND_pairs sp;
                   8686:     ND spol;
                   8687:     Z **spmat;
                   8688:     Z *svect,*v;
                   8689:     int *colstat;
                   8690:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8691:     int maxrs;
                   8692:     int *spsugar;
                   8693:     pointer *w;
                   8694:
                   8695:     spcol = col-nred;
                   8696:     get_eg(&eg0);
                   8697:     /* elimination (1st step) */
                   8698:     spmat = (Z **)MALLOC(nsp*sizeof(Q *));
                   8699:     svect = (Z *)MALLOC(col*sizeof(Q));
                   8700:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8701:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8702:         nd_sp(0,trace,sp,&spol);
                   8703:         if ( !spol ) continue;
                   8704:         nd_to_vect_q(s0vect,col,spol,svect);
                   8705:         maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
                   8706:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   8707:         if ( i < col ) {
                   8708:             spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
                   8709:             for ( j = k = 0; j < col; j++ )
                   8710:                 if ( !rhead[j] ) v[k++] = svect[j];
                   8711:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8712:             sprow++;
                   8713:         }
                   8714: /*        nd_free(spol); */
                   8715:     }
                   8716:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8717:     if ( DP_Print ) {
1.5       noro     8718:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8719:         fflush(asir_out);
                   8720:     }
                   8721:     /* free index arrays */
                   8722: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   8723:
                   8724:     /* elimination (2nd step) */
                   8725:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8726:     rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
                   8727:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   8728:     for ( i = 0; i < rank; i++ ) {
                   8729: #if 0
                   8730:         w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   8731:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   8732: #else
                   8733:         w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   8734:         SG((NDV)w[i]) = spsugar[i];
                   8735: #endif
                   8736: /*        GCFREE(spmat[i]); */
                   8737:     }
                   8738: #if 0
                   8739:     qsort(w,rank,sizeof(NDV),
                   8740:         (int (*)(const void *,const void *))ndv_compare);
                   8741: #endif
                   8742:     r0 = 0;
                   8743:     for ( i = 0; i < rank; i++ ) {
                   8744:         NEXTNODE(r0,r); BDY(r) = w[i];
                   8745:     }
                   8746:     if ( r0 ) NEXT(r) = 0;
                   8747:
                   8748: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   8749:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8750:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8751:     if ( DP_Print ) {
1.5       noro     8752:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8753:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8754:             nsp,nred,sprow,spcol,rank);
1.5       noro     8755:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     8756:     }
                   8757:     return r0;
                   8758: }
                   8759:
                   8760: FILE *nd_write,*nd_read;
                   8761:
                   8762: void nd_send_int(int a) {
                   8763:     write_int(nd_write,(unsigned int *)&a);
                   8764: }
                   8765:
                   8766: void nd_send_intarray(int *p,int len) {
                   8767:     write_intarray(nd_write,(unsigned int *)p,len);
                   8768: }
                   8769:
                   8770: int nd_recv_int() {
                   8771:     int a;
                   8772:
                   8773:     read_int(nd_read,(unsigned int *)&a);
                   8774:     return a;
                   8775: }
                   8776:
                   8777: void nd_recv_intarray(int *p,int len) {
                   8778:     read_intarray(nd_read,(unsigned int *)p,len);
                   8779: }
                   8780:
                   8781: void nd_send_ndv(NDV p) {
                   8782:     int len,i;
                   8783:     NMV m;
                   8784:
                   8785:     if ( !p ) nd_send_int(0);
                   8786:     else {
                   8787:         len = LEN(p);
                   8788:         nd_send_int(len);
                   8789:         m = BDY(p);
                   8790:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   8791:             nd_send_int(CM(m));
                   8792:             nd_send_intarray((int *)DL(m),nd_wpd);
                   8793:         }
                   8794:     }
                   8795: }
                   8796:
                   8797: void nd_send_nd(ND p) {
                   8798:     int len,i;
                   8799:     NM m;
                   8800:
                   8801:     if ( !p ) nd_send_int(0);
                   8802:     else {
                   8803:         len = LEN(p);
                   8804:         nd_send_int(len);
                   8805:         m = BDY(p);
                   8806:         for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   8807:             nd_send_int(CM(m));
                   8808:             nd_send_intarray((int *)DL(m),nd_wpd);
                   8809:         }
                   8810:     }
                   8811: }
                   8812:
                   8813: NDV nd_recv_ndv()
                   8814: {
                   8815:     int len,i;
                   8816:     NMV m,m0;
                   8817:     NDV r;
                   8818:
                   8819:     len = nd_recv_int();
                   8820:     if ( !len ) return 0;
                   8821:     else {
                   8822:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8823: #if 0
                   8824:         ndv_alloc += len*nmv_adv;
                   8825: #endif
                   8826:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   8827:             CM(m) = nd_recv_int();
                   8828:             nd_recv_intarray((int *)DL(m),nd_wpd);
                   8829:         }
                   8830:         MKNDV(nd_nvar,m0,len,r);
                   8831:         return r;
                   8832:     }
                   8833: }
                   8834:
                   8835: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
                   8836: {
                   8837:     int i,j,t,c,rank,inv;
                   8838:     int *ci,*ri;
                   8839:     Z dn;
                   8840:     MAT m,nm;
                   8841:
                   8842:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
                   8843:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   8844:     for ( i = 0; i < row; i++ )
                   8845:         for ( j = 0; j < col; j++ )
                   8846:             mat0[i][j] = 0;
                   8847:     c = col-rank;
                   8848:     for ( i = 0; i < rank; i++ ) {
                   8849:         mat0[i][ri[i]] = dn;
                   8850:         for ( j = 0; j < c; j++ )
                   8851:             mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
                   8852:     }
                   8853:     return rank;
                   8854: }
                   8855:
                   8856: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   8857: {
                   8858:     int i,j,k,l,inv,a,rank,s;
                   8859:     unsigned int *t,*pivot,*pk;
                   8860:     unsigned int **mat;
                   8861:     ND_pairs pair;
                   8862:
                   8863:     mat = (unsigned int **)mat0;
                   8864:     for ( rank = 0, j = 0; j < col; j++ ) {
                   8865:         for ( i = rank; i < row; i++ )
                   8866:             mat[i][j] %= md;
                   8867:         for ( i = rank; i < row; i++ )
                   8868:             if ( mat[i][j] )
                   8869:                 break;
                   8870:         if ( i == row ) {
                   8871:             colstat[j] = 0;
                   8872:             continue;
                   8873:         } else
                   8874:             colstat[j] = 1;
                   8875:         if ( i != rank ) {
                   8876:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   8877:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   8878:             if ( spactive ) {
                   8879:                 pair = spactive[i]; spactive[i] = spactive[rank];
                   8880:                 spactive[rank] = pair;
                   8881:             }
                   8882:         }
                   8883:         pivot = mat[rank];
                   8884:         s = sugar[rank];
                   8885:         inv = invm(pivot[j],md);
                   8886:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   8887:             if ( *pk ) {
                   8888:                 if ( *pk >= (unsigned int)md )
                   8889:                     *pk %= md;
                   8890:                 DMAR(*pk,inv,0,md,*pk)
                   8891:             }
                   8892:         for ( i = rank+1; i < row; i++ ) {
                   8893:             t = mat[i];
                   8894:             if ( (a = t[j]) != 0 ) {
                   8895:                 sugar[i] = MAX(sugar[i],s);
                   8896:                 red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   8897:             }
                   8898:         }
                   8899:         rank++;
                   8900:     }
                   8901:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   8902:         if ( colstat[j] ) {
                   8903:             pivot = mat[l];
                   8904:             s = sugar[l];
                   8905:             for ( i = 0; i < l; i++ ) {
                   8906:                 t = mat[i];
                   8907:                 t[j] %= md;
                   8908:                 if ( (a = t[j]) != 0 ) {
                   8909:                     sugar[i] = MAX(sugar[i],s);
                   8910:                     red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   8911:                 }
                   8912:             }
                   8913:             l--;
                   8914:         }
                   8915:     for ( j = 0, l = 0; l < rank; j++ )
                   8916:         if ( colstat[j] ) {
                   8917:             t = mat[l];
                   8918:             for ( k = j; k < col; k++ )
                   8919:                 if ( t[k] >= (unsigned int)md )
                   8920:                     t[k] %= md;
                   8921:             l++;
                   8922:         }
                   8923:     return rank;
                   8924: }
                   8925:
                   8926:
1.7       noro     8927: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1       noro     8928: {
1.7       noro     8929:     int i,j,k,l,inv,a,rank,s;
                   8930:     unsigned int *t,*pivot,*pk;
                   8931:     unsigned int **mat;
                   8932:
                   8933:     mat = (unsigned int **)mat0;
                   8934:     for ( rank = 0, j = 0; j < col; j++ ) {
                   8935:         for ( i = rank; i < row; i++ )
                   8936:             if ( mat[i][j] )
                   8937:                 break;
                   8938:         if ( i == row ) {
                   8939:             colstat[j] = 0;
                   8940:             continue;
                   8941:         } else
                   8942:             colstat[j] = 1;
                   8943:         if ( i != rank ) {
                   8944:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   8945:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   8946:         }
                   8947:         pivot = mat[rank];
                   8948:         s = sugar[rank];
                   8949:         inv = _invsf(pivot[j]);
                   8950:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   8951:             if ( *pk )
                   8952:                 *pk = _mulsf(*pk,inv);
                   8953:         for ( i = rank+1; i < row; i++ ) {
                   8954:             t = mat[i];
                   8955:             if ( (a = t[j]) != 0 ) {
                   8956:                 sugar[i] = MAX(sugar[i],s);
                   8957:                 red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   8958:             }
                   8959:         }
                   8960:         rank++;
                   8961:     }
                   8962:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   8963:         if ( colstat[j] ) {
                   8964:             pivot = mat[l];
                   8965:             s = sugar[l];
                   8966:             for ( i = 0; i < l; i++ ) {
                   8967:                 t = mat[i];
                   8968:                 if ( (a = t[j]) != 0 ) {
                   8969:                     sugar[i] = MAX(sugar[i],s);
                   8970:                     red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   8971:                 }
                   8972:             }
                   8973:             l--;
                   8974:         }
                   8975:     return rank;
                   8976: }
1.1       noro     8977:
1.7       noro     8978: int ndv_ishomo(NDV p)
                   8979: {
                   8980:     NMV m;
                   8981:     int len,h;
1.1       noro     8982:
                   8983:     if ( !p ) return 1;
                   8984:     len = LEN(p);
                   8985:     m = BDY(p);
                   8986:     h = TD(DL(m));
                   8987:     NMV_ADV(m);
                   8988:     for ( len--; len; len--, NMV_ADV(m) )
1.20      noro     8989:         if ( TD(DL(m)) != h ) {
                   8990:           return 0;
                   8991:         }
1.1       noro     8992:     return 1;
                   8993: }
                   8994:
                   8995: void ndv_save(NDV p,int index)
                   8996: {
                   8997:     FILE *s;
                   8998:     char name[BUFSIZ];
                   8999:     short id;
                   9000:     int nv,sugar,len,n,i,td,e,j;
                   9001:     NMV m;
                   9002:     unsigned int *dl;
                   9003:     int mpos;
                   9004:
                   9005:     sprintf(name,"%s/%d",Demand,index);
                   9006:     s = fopen(name,"w");
                   9007:     savevl(s,0);
                   9008:     if ( !p ) {
                   9009:         saveobj(s,0);
                   9010:         return;
                   9011:     }
                   9012:     id = O_DP;
                   9013:     nv = NV(p);
                   9014:     sugar = SG(p);
                   9015:     len = LEN(p);
                   9016:     write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
                   9017:     write_int(s,(unsigned int *)&len);
                   9018:
                   9019:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     9020:         saveobj(s,(Obj)CZ(m));
1.1       noro     9021:         dl = DL(m);
                   9022:         td = TD(dl);
                   9023:         write_int(s,(unsigned int *)&td);
                   9024:         for ( j = 0; j < nv; j++ ) {
                   9025:             e = GET_EXP(dl,j);
                   9026:             write_int(s,(unsigned int *)&e);
                   9027:         }
                   9028:         if ( nd_module ) {
                   9029:             mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
                   9030:         }
                   9031:     }
                   9032:     fclose(s);
                   9033: }
                   9034:
                   9035: void nd_save_mod(ND p,int index)
                   9036: {
                   9037:     FILE *s;
                   9038:     char name[BUFSIZ];
                   9039:     int nv,sugar,len,c;
                   9040:     NM m;
                   9041:
                   9042:     sprintf(name,"%s/%d",Demand,index);
                   9043:     s = fopen(name,"w");
                   9044:     if ( !p ) {
                   9045:     len = 0;
                   9046:       write_int(s,(unsigned int *)&len);
                   9047:     fclose(s);
                   9048:         return;
                   9049:     }
                   9050:     nv = NV(p);
                   9051:     sugar = SG(p);
                   9052:     len = LEN(p);
                   9053:     write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
                   9054:   for ( m = BDY(p); m; m = NEXT(m) ) {
                   9055:     c = CM(m); write_int(s,(unsigned int *)&c);
                   9056:     write_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   9057:   }
                   9058:     fclose(s);
                   9059: }
                   9060:
                   9061: NDV ndv_load(int index)
                   9062: {
                   9063:     FILE *s;
                   9064:     char name[BUFSIZ];
                   9065:     short id;
                   9066:     int nv,sugar,len,n,i,td,e,j;
                   9067:     NDV d;
                   9068:     NMV m0,m;
                   9069:     unsigned int *dl;
                   9070:     Obj obj;
                   9071:     int mpos;
                   9072:
                   9073:     sprintf(name,"%s/%d",Demand,index);
                   9074:     s = fopen(name,"r");
                   9075:     if ( !s ) return 0;
                   9076:
                   9077:     skipvl(s);
                   9078:     read_short(s,(unsigned short *)&id);
                   9079:     if ( !id ) return 0;
                   9080:     read_int(s,(unsigned int *)&nv);
                   9081:     read_int(s,(unsigned int *)&sugar);
                   9082:     read_int(s,(unsigned int *)&len);
                   9083:
                   9084:     m0 = m = MALLOC(len*nmv_adv);
                   9085:     for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     9086:         loadobj(s,&obj); CZ(m) = (Z)obj;
1.1       noro     9087:         dl = DL(m);
                   9088:         ndl_zero(dl);
                   9089:         read_int(s,(unsigned int *)&td); TD(dl) = td;
                   9090:         for ( j = 0; j < nv; j++ ) {
                   9091:             read_int(s,(unsigned int *)&e);
                   9092:             PUT_EXP(dl,j,e);
                   9093:         }
                   9094:         if ( nd_module ) {
                   9095:             read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
                   9096:         }
                   9097:         if ( nd_blockmask ) ndl_weight_mask(dl);
                   9098:     }
                   9099:     fclose(s);
                   9100:     MKNDV(nv,m0,len,d);
                   9101:     SG(d) = sugar;
                   9102:     return d;
                   9103: }
                   9104:
                   9105: ND nd_load_mod(int index)
                   9106: {
                   9107:     FILE *s;
                   9108:     char name[BUFSIZ];
                   9109:     int nv,sugar,len,i,c;
                   9110:   ND d;
                   9111:     NM m0,m;
                   9112:
                   9113:     sprintf(name,"%s/%d",Demand,index);
                   9114:     s = fopen(name,"r");
                   9115:   /* if the file does not exist, it means p[index]=0 */
                   9116:     if ( !s ) return 0;
                   9117:
                   9118:     read_int(s,(unsigned int *)&nv);
                   9119:   if ( !nv ) { fclose(s); return 0; }
                   9120:
                   9121:     read_int(s,(unsigned int *)&sugar);
                   9122:     read_int(s,(unsigned int *)&len);
                   9123:   for ( m0 = 0, i = 0; i < len; i++ ) {
                   9124:     NEXTNM(m0,m);
                   9125:     read_int(s,(unsigned int *)&c); CM(m) = c;
                   9126:     read_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   9127:   }
                   9128:   NEXT(m) = 0;
                   9129:     MKND(nv,m0,len,d);
                   9130:     SG(d) = sugar;
                   9131:   fclose(s);
                   9132:     return d;
                   9133: }
                   9134:
                   9135: void nd_det(int mod,MAT f,P *rp)
                   9136: {
                   9137:     VL fv,tv;
                   9138:     int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
                   9139:     pointer **m;
                   9140:     P **w;
                   9141:     P mp,r;
                   9142:     NDV **dm;
                   9143:     NDV *t,*mi,*mj;
                   9144:     NDV d,s,mij,mjj;
                   9145:     ND u;
                   9146:     NMV nmv;
                   9147:     UINT *bound;
                   9148:     PGeoBucket bucket;
                   9149:     struct order_spec *ord;
                   9150:     Z dq,dt,ds;
                   9151:     Z mone;
                   9152:     Z gn,qn,dn0,nm,dn;
                   9153:
                   9154:     create_order_spec(0,0,&ord);
                   9155:     nd_init_ord(ord);
                   9156:     get_vars((Obj)f,&fv);
                   9157:     if ( f->row != f->col )
                   9158:         error("nd_det : non-square matrix");
                   9159:     n = f->row;
                   9160:     m = f->body;
                   9161:     for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   9162:
                   9163:     if ( !nvar ) {
                   9164:         if ( !mod )
                   9165:             detp(CO,(P **)m,n,rp);
                   9166:         else {
                   9167:             w = (P **)almat_pointer(n,n);
                   9168:             for ( i = 0; i < n; i++ )
                   9169:                 for ( j = 0; j < n; j++ )
                   9170:                     ptomp(mod,(P)m[i][j],&w[i][j]);
                   9171:             detmp(CO,mod,w,n,&mp);
                   9172:             mptop(mp,rp);
                   9173:         }
                   9174:         return;
                   9175:     }
                   9176:
                   9177:     if ( !mod ) {
                   9178:         w = (P **)almat_pointer(n,n);
                   9179:         dq = ONE;
                   9180:         for ( i = 0; i < n; i++ ) {
                   9181:             dn0 = ONE;
                   9182:             for ( j = 0; j < n; j++ ) {
                   9183:                 if ( !m[i][j] ) continue;
                   9184:                 lgp(m[i][j],&nm,&dn);
1.6       noro     9185:                 gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1       noro     9186:             }
                   9187:             if ( !UNIZ(dn0) ) {
                   9188:                 ds = dn0;
                   9189:                 for ( j = 0; j < n; j++ )
                   9190:                     mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   9191:                 mulz(dq,ds,&dt); dq = dt;
                   9192:             } else
                   9193:                 for ( j = 0; j < n; j++ )
                   9194:                     w[i][j] = (P)m[i][j];
                   9195:         }
                   9196:         m = (pointer **)w;
                   9197:     }
                   9198:
                   9199:     for ( i = 0, max = 1; i < n; i++ )
                   9200:         for ( j = 0; j < n; j++ )
                   9201:             for ( tv = fv; tv; tv = NEXT(tv) ) {
                   9202:                 e = getdeg(tv->v,(P)m[i][j]);
                   9203:                 max = MAX(e,max);
                   9204:             }
                   9205:     nd_setup_parameters(nvar,max);
                   9206:     dm = (NDV **)almat_pointer(n,n);
                   9207:     for ( i = 0, max = 1; i < n; i++ )
                   9208:         for ( j = 0; j < n; j++ ) {
                   9209:             dm[i][j] = ptondv(CO,fv,m[i][j]);
                   9210:             if ( mod ) ndv_mod(mod,dm[i][j]);
                   9211:             if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   9212:         }
                   9213:     d = ptondv(CO,fv,(P)ONE);
                   9214:     if ( mod ) ndv_mod(mod,d);
                   9215:     chsgnz(ONE,&mone);
                   9216:     for ( j = 0, sgn = 1; j < n; j++ ) {
                   9217:       if ( DP_Print ) {
                   9218:         fprintf(asir_out,".");
                   9219:       }
                   9220:         for ( i = j; i < n && !dm[i][j]; i++ );
                   9221:         if ( i == n ) {
                   9222:             *rp = 0;
                   9223:             return;
                   9224:         }
                   9225:         k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
                   9226:         for ( k = j; k < n; k++ )
                   9227:             for ( l = j; l < n; l++ )
                   9228:                 if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   9229:                     k0 = k; l0 = l; len0 = LEN(dm[k][l]);
                   9230:                 }
                   9231:         if ( k0 != j ) {
                   9232:             t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   9233:             sgn = -sgn;
                   9234:         }
                   9235:         if ( l0 != j ) {
                   9236:             for ( k = j; k < n; k++ ) {
                   9237:                 s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   9238:             }
                   9239:             sgn = -sgn;
                   9240:         }
                   9241:         bound = nd_det_compute_bound(dm,n,j);
                   9242:         for ( k = 0; k < nd_nvar; k++ )
                   9243:             if ( bound[k]*2 > nd_mask0 ) break;
                   9244:         if ( k < nd_nvar )
                   9245:             nd_det_reconstruct(dm,n,j,d);
                   9246:
                   9247:         for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
                   9248: /*            if ( DP_Print ) fprintf(asir_out,"    i=%d\n        ",i); */
                   9249:             mi = dm[i]; mij = mi[j];
                   9250:             if ( mod )
                   9251:                 ndv_mul_c(mod,mij,mod-1);
                   9252:             else
                   9253:                 ndv_mul_c_q(mij,mone);
                   9254:             for ( k = j+1; k < n; k++ ) {
                   9255: /*                if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
                   9256:                 bucket = create_pbucket();
                   9257:                 if ( mi[k] ) {
                   9258:                     nmv = BDY(mjj); len = LEN(mjj);
                   9259:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   9260:                         u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   9261:                         add_pbucket(mod,bucket,u);
                   9262:                     }
                   9263:                 }
                   9264:                 if ( mj[k] && mij ) {
                   9265:                     nmv = BDY(mij); len = LEN(mij);
                   9266:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   9267:                         u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   9268:                         add_pbucket(mod,bucket,u);
                   9269:                     }
                   9270:                 }
                   9271:                 u = nd_quo(mod,bucket,d);
                   9272:                 mi[k] = ndtondv(mod,u);
                   9273:             }
                   9274: /*            if ( DP_Print ) fprintf(asir_out,"\n",k); */
                   9275:         }
                   9276:         d = mjj;
                   9277:     }
                   9278:     if ( DP_Print ) {
                   9279:       fprintf(asir_out,"\n");
                   9280:     }
                   9281:     if ( sgn < 0 ) {
                   9282:         if ( mod )
                   9283:             ndv_mul_c(mod,d,mod-1);
                   9284:         else
                   9285:             ndv_mul_c_q(d,mone);
                   9286:     }
                   9287:     r = ndvtop(mod,CO,fv,d);
                   9288:     if ( !mod && !UNIQ(dq) )
                   9289:         divsp(CO,r,(P)dq,rp);
                   9290:     else
                   9291:         *rp = r;
                   9292: }
                   9293:
                   9294: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
                   9295: {
                   9296:     NM mr,mr0;
                   9297:     NM tnm;
                   9298:     NMV m;
                   9299:     UINT *d0,*dt,*dm;
                   9300:     int c,n,td,i,c1,c2,len;
                   9301:     Z q;
                   9302:     ND r;
                   9303:
                   9304:     if ( !p ) return 0;
                   9305:     else {
                   9306:         n = NV(p); m = BDY(p); len = LEN(p);
                   9307:         d0 = DL(m0);
                   9308:         td = TD(d);
                   9309:         mr0 = 0;
                   9310:         NEWNM(tnm);
                   9311:         if ( mod ) {
                   9312:             c = CM(m0);
                   9313:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9314:                 ndl_add(DL(m),d0,DL(tnm));
                   9315:                 if ( ndl_reducible(DL(tnm),d) ) {
                   9316:                     NEXTNM(mr0,mr);
                   9317:                     c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
                   9318:                     ndl_copy(DL(tnm),DL(mr));
                   9319:                 }
                   9320:             }
                   9321:         } else {
1.6       noro     9322:             q = CZ(m0);
1.1       noro     9323:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9324:                 ndl_add(DL(m),d0,DL(tnm));
                   9325:                 if ( ndl_reducible(DL(tnm),d) ) {
                   9326:                     NEXTNM(mr0,mr);
1.6       noro     9327:                     mulz(CZ(m),q,&CZ(mr));
1.1       noro     9328:                     ndl_copy(DL(tnm),DL(mr));
                   9329:                 }
                   9330:             }
                   9331:         }
                   9332:         if ( !mr0 )
                   9333:             return 0;
                   9334:         else {
                   9335:             NEXT(mr) = 0;
                   9336:             for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   9337:             MKND(NV(p),mr0,len,r);
                   9338:             SG(r) = SG(p) + TD(d0);
                   9339:             return r;
                   9340:         }
                   9341:     }
                   9342: }
                   9343:
                   9344: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   9345: {
                   9346:     int i,obpe,oadv,h,k,l;
                   9347:     static NM prev_nm_free_list;
                   9348:     EPOS oepos;
                   9349:
                   9350:     obpe = nd_bpe;
                   9351:     oadv = nmv_adv;
                   9352:     oepos = nd_epos;
                   9353:     if ( obpe < 2 ) nd_bpe = 2;
                   9354:     else if ( obpe < 3 ) nd_bpe = 3;
                   9355:     else if ( obpe < 4 ) nd_bpe = 4;
                   9356:     else if ( obpe < 5 ) nd_bpe = 5;
                   9357:     else if ( obpe < 6 ) nd_bpe = 6;
                   9358:     else if ( obpe < 8 ) nd_bpe = 8;
                   9359:     else if ( obpe < 10 ) nd_bpe = 10;
                   9360:     else if ( obpe < 16 ) nd_bpe = 16;
                   9361:     else if ( obpe < 32 ) nd_bpe = 32;
                   9362:     else error("nd_det_reconstruct : exponent too large");
                   9363:
                   9364:     nd_setup_parameters(nd_nvar,0);
                   9365:     prev_nm_free_list = _nm_free_list;
                   9366:     _nm_free_list = 0;
                   9367:     for ( k = j; k < n; k++ )
                   9368:         for (l = j; l < n; l++ )
                   9369:             ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   9370:     ndv_realloc(d,obpe,oadv,oepos);
                   9371:     prev_nm_free_list = 0;
                   9372: #if 0
                   9373:     GC_gcollect();
                   9374: #endif
                   9375: }
                   9376:
                   9377: /* returns a UINT array containing degree bounds */
                   9378:
                   9379: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   9380: {
                   9381:     UINT *d0,*d1,*d,*t,*r;
                   9382:     int k,l,i;
                   9383:
                   9384:     d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
                   9385:     for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
                   9386:     for ( k = j; k < n; k++ )
                   9387:         for ( l = j; l < n; l++ )
                   9388:             if ( dm[k][l] ) {
                   9389:                 d = ndv_compute_bound(dm[k][l]);
                   9390:                 for ( i = 0; i < nd_nvar; i++ )
                   9391:                     d0[i] = MAX(d0[i],d[i]);
                   9392:             }
                   9393:     return d0;
                   9394: }
                   9395:
                   9396: DL nd_separate_d(UINT *d,UINT *trans)
                   9397: {
                   9398:     int n,td,i,e,j;
                   9399:     DL a;
                   9400:
                   9401:     ndl_zero(trans);
                   9402:     td = 0;
                   9403:     for ( i = 0; i < nd_ntrans; i++ ) {
                   9404:         e = GET_EXP(d,i);
                   9405:         PUT_EXP(trans,i,e);
                   9406:         td += MUL_WEIGHT(e,i);
                   9407:     }
                   9408:     if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   9409:         /* homogenized */
                   9410:         i = nd_nvar-1;
                   9411:         e = GET_EXP(d,i);
                   9412:         PUT_EXP(trans,i,e);
                   9413:         td += MUL_WEIGHT(e,i);
                   9414:     }
                   9415:     TD(trans) = td;
                   9416:     if ( nd_blockmask) ndl_weight_mask(trans);
                   9417:     NEWDL(a,nd_nalg);
                   9418:     td = 0;
                   9419:     for ( i = 0; i < nd_nalg; i++ ) {
                   9420:         j = nd_ntrans+i;
                   9421:         e = GET_EXP(d,j);
                   9422:         a->d[i] = e;
                   9423:         td += e;
                   9424:     }
                   9425:     a->td = td;
                   9426:     return a;
                   9427: }
                   9428:
                   9429: int nd_monic(int mod,ND *p)
                   9430: {
                   9431:     UINT *trans,*t;
                   9432:     DL alg;
                   9433:     MP mp0,mp;
                   9434:     NM m,m0,m1,ma0,ma,mb,mr0,mr;
                   9435:     ND r;
                   9436:     DL dl;
                   9437:     DP nm;
                   9438:     NDV ndv;
                   9439:     DAlg inv,cd;
                   9440:     ND s,c;
                   9441:     Z l,mul;
                   9442:     Z ln;
                   9443:     int n,ntrans,i,e,td,is_lc,len;
                   9444:     NumberField nf;
                   9445:     struct oEGT eg0,eg1;
                   9446:
                   9447:     if ( !(nf = get_numberfield()) )
                   9448:         error("nd_monic : current_numberfield is not set");
                   9449:
                   9450:     /* Q coef -> DAlg coef */
                   9451:     NEWNM(ma0); ma = ma0;
                   9452:     m = BDY(*p);
                   9453:     is_lc = 1;
                   9454:     while ( 1 ) {
                   9455:         NEWMP(mp0); mp = mp0;
1.6       noro     9456:         mp->c = (Obj)CZ(m);
1.1       noro     9457:         mp->dl = nd_separate_d(DL(m),DL(ma));
                   9458:         NEWNM(mb);
                   9459:         for ( m = NEXT(m); m; m = NEXT(m) ) {
                   9460:             alg = nd_separate_d(DL(m),DL(mb));
                   9461:             if ( !ndl_equal(DL(ma),DL(mb)) )
                   9462:                 break;
1.6       noro     9463:             NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1       noro     9464:         }
                   9465:         NEXT(mp) = 0;
                   9466:         MKDP(nd_nalg,mp0,nm);
                   9467:         MKDAlg(nm,ONE,cd);
                   9468:         if ( is_lc == 1 ) {
                   9469:             /* if the lc is a rational number, we have nothing to do */
                   9470:             if ( !mp0->dl->td )
                   9471:                 return 1;
                   9472:
                   9473:             get_eg(&eg0);
                   9474:             invdalg(cd,&inv);
                   9475:             get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   9476:             /* check the validity of inv */
                   9477:             if ( mod && !remqi((Q)inv->dn,mod) )
                   9478:                 return 0;
                   9479:             CA(ma) = nf->one;
                   9480:             is_lc = 0;
                   9481:             ln = ONE;
                   9482:         } else {
                   9483:             muldalg(cd,inv,&CA(ma));
                   9484:             lcmz(ln,CA(ma)->dn,&ln);
                   9485:         }
                   9486:         if ( m ) {
                   9487:             NEXT(ma) = mb; ma = mb;
                   9488:         } else {
                   9489:             NEXT(ma) = 0;
                   9490:             break;
                   9491:         }
                   9492:     }
                   9493:     /* l = lcm(denoms) */
                   9494:     l = ln;
                   9495:     for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6       noro     9496:         divsz(l,CA(m)->dn,&mul);
1.1       noro     9497:         for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   9498:             NEXTNM(mr0,mr);
1.6       noro     9499:             mulz((Z)mp->c,mul,&CZ(mr));
1.1       noro     9500:             dl = mp->dl;
                   9501:             td = TD(DL(m));
                   9502:             ndl_copy(DL(m),DL(mr));
                   9503:             for ( i = 0; i < nd_nalg; i++ ) {
                   9504:                 e = dl->d[i];
                   9505:                 PUT_EXP(DL(mr),i+nd_ntrans,e);
                   9506:                 td += MUL_WEIGHT(e,i+nd_ntrans);
                   9507:             }
                   9508:             if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
                   9509:             TD(DL(mr)) = td;
                   9510:             if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   9511:         }
                   9512:     }
                   9513:     NEXT(mr) = 0;
                   9514:     for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   9515:     MKND(NV(*p),mr0,len,r);
                   9516:     /* XXX */
                   9517:     SG(r) = SG(*p);
                   9518:     nd_free(*p);
                   9519:     *p = r;
                   9520:     return 1;
                   9521: }
                   9522:
                   9523: NODE reverse_node(NODE n)
                   9524: {
                   9525:     NODE t,t1;
                   9526:
                   9527:     for ( t = 0; n; n = NEXT(n) ) {
                   9528:         MKNODE(t1,BDY(n),t); t = t1;
                   9529:     }
                   9530:     return t;
                   9531: }
                   9532:
                   9533: P ndc_div(int mod,union oNDC a,union oNDC b)
                   9534: {
                   9535:     union oNDC c;
                   9536:     int inv,t;
                   9537:
                   9538:     if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10      noro     9539:     else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1       noro     9540:     else if ( mod ) {
                   9541:         inv = invm(b.m,mod);
                   9542:         DMAR(a.m,inv,0,mod,t); c.m = t;
                   9543:     } else if ( nd_vc )
                   9544:        divsp(nd_vc,a.p,b.p,&c.p);
                   9545:     else
                   9546:        divsz(a.z,b.z,&c.z);
                   9547:     return ndctop(mod,c);
                   9548: }
                   9549:
                   9550: P ndctop(int mod,union oNDC c)
                   9551: {
                   9552:     Z q;
                   9553:     int e;
                   9554:     GFS gfs;
                   9555:
                   9556:     if ( mod == -1 ) {
                   9557:         e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
                   9558:     } else if ( mod == -2 ) {
1.10      noro     9559:        q = c.z; return (P)q;
1.1       noro     9560:     } else if ( mod > 0 ) {
1.6       noro     9561:         STOZ(c.m,q); return (P)q;
1.1       noro     9562:     } else
                   9563:         return (P)c.p;
                   9564: }
                   9565:
                   9566: /* [0,0,0,cont] = p -> p/cont */
                   9567:
                   9568: void finalize_tracelist(int i,P cont)
                   9569: {
                   9570:  LIST l;
                   9571:  NODE node;
                   9572:  Z iq;
                   9573:
                   9574:   if ( !UNIQ(cont) ) {
                   9575:     node = mknode(4,NULLP,NULLP,NULLP,cont);
                   9576:     MKLIST(l,node); MKNODE(node,l,nd_tracelist);
                   9577:     nd_tracelist = node;
                   9578:   }
1.6       noro     9579:   STOZ(i,iq);
1.1       noro     9580:   nd_tracelist = reverse_node(nd_tracelist);
                   9581:   MKLIST(l,nd_tracelist);
                   9582:   node = mknode(2,iq,l); MKLIST(l,node);
                   9583:   MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
                   9584:   nd_alltracelist = node; nd_tracelist = 0;
                   9585: }
                   9586:
                   9587: void conv_ilist(int demand,int trace,NODE g,int **indp)
                   9588: {
                   9589:     int n,i,j;
                   9590:   int *ind;
                   9591:   NODE t;
                   9592:
                   9593:     n = length(g);
                   9594:   ind = (int *)MALLOC(n*sizeof(int));
                   9595:   for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
                   9596:     j = (long)BDY(t); ind[i] = j;
                   9597:     BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
                   9598:   }
                   9599:   if ( indp ) *indp = ind;
                   9600: }
                   9601:
1.30      noro     9602: NODE conv_ilist_s(int demand,int trace,int **indp)
                   9603: {
                   9604:   int n,i,j;
                   9605:   int *ind;
                   9606:   NODE g0,g;
                   9607:
                   9608:   n = nd_psn;
                   9609:   ind = (int *)MALLOC(n*sizeof(int));
                   9610:   g0 = 0;
                   9611:   for ( i = 0; i < n; i++ ) {
                   9612:     ind[i] = i;
                   9613:     NEXTNODE(g0,g);
                   9614:     BDY(g) = (pointer)(demand?ndv_load(i):(trace?nd_ps_trace[i]:nd_ps[i]));
                   9615:   }
                   9616:   if ( g0 ) NEXT(g) = 0;
                   9617:   if ( indp ) *indp = ind;
                   9618:   return g0;
                   9619: }
                   9620:
1.1       noro     9621: void parse_nd_option(NODE opt)
                   9622: {
1.38      noro     9623:   NODE t,p,u;
1.1       noro     9624:   int i,s,n;
1.38      noro     9625:   char *key;
                   9626:   Obj value;
1.1       noro     9627:
1.38      noro     9628:   nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
1.1       noro     9629:   nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
                   9630:   nd_splist = 0; nd_check_splist = 0;
1.38      noro     9631:   nd_sugarweight = 0; nd_f4red =0; nd_rank0 = 0;
                   9632:   nd_f4_td = 0; nd_sba_f4step = 2; nd_sba_pot = 0; nd_sba_largelcm = 0;
1.39    ! noro     9633:   nd_sba_dontsort = 0; nd_top = 0;
        !          9634:
1.38      noro     9635:   for ( t = opt; t; t = NEXT(t) ) {
                   9636:     p = BDY((LIST)BDY(t));
                   9637:     key = BDY((STRING)BDY(p));
                   9638:     value = (Obj)BDY(NEXT(p));
                   9639:     if ( !strcmp(key,"gentrace") )
                   9640:       nd_gentrace = value?1:0;
                   9641:     else if ( !strcmp(key,"gensyz") )
                   9642:       nd_gensyz = value?1:0;
                   9643:     else if ( !strcmp(key,"nora") )
                   9644:       nd_nora = value?1:0;
                   9645:     else if ( !strcmp(key,"gbblock") ) {
                   9646:       if ( value && OID(value) == O_LIST ) {
1.1       noro     9647:         u = BDY((LIST)value);
1.38      noro     9648:         nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.1       noro     9649:         for ( i = 0; u; u = NEXT(u) ) {
                   9650:           p = BDY((LIST)BDY(u));
1.6       noro     9651:           s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
                   9652:           nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1       noro     9653:         }
                   9654:         nd_gbblock[i] = -1;
1.38      noro     9655:       } else
                   9656:         nd_gbblock = 0;
1.1       noro     9657:     } else if ( !strcmp(key,"newelim") )
                   9658:             nd_newelim = value?1:0;
                   9659:     else if ( !strcmp(key,"intersect") )
                   9660:             nd_intersect = value?1:0;
1.17      noro     9661:     else if ( !strcmp(key,"syzgen") )
                   9662:             nd_intersect = ZTOS((Q)value);
1.1       noro     9663:     else if ( !strcmp(key,"lf") )
                   9664:             nd_lf = value?1:0;
                   9665:     else if ( !strcmp(key,"trace") ) {
1.38      noro     9666:       if ( value ) {
                   9667:         u = BDY((LIST)value);
                   9668:         nd_nzlist = BDY((LIST)ARG2(u));
                   9669:         nd_bpe = ZTOS((Q)ARG3(u));
                   9670:       }
1.1       noro     9671:     } else if ( !strcmp(key,"f4red") ) {
1.38      noro     9672:       nd_f4red = ZTOS((Q)value);
1.1       noro     9673:     } else if ( !strcmp(key,"rank0") ) {
1.38      noro     9674:       nd_rank0 = value?1:0;
1.1       noro     9675:     } else if ( !strcmp(key,"splist") ) {
1.38      noro     9676:       nd_splist = value?1:0;
1.1       noro     9677:     } else if ( !strcmp(key,"check_splist") ) {
                   9678:       nd_check_splist = BDY((LIST)value);
                   9679:     } else if ( !strcmp(key,"sugarweight") ) {
                   9680:       u = BDY((LIST)value);
1.38      noro     9681:       n = length(u);
                   9682:       nd_sugarweight = MALLOC(n*sizeof(int));
1.1       noro     9683:       for ( i = 0; i < n; i++, u = NEXT(u) )
1.38      noro     9684:         nd_sugarweight[i] = ZTOS((Q)BDY(u));
                   9685:     } else if ( !strcmp(key,"f4_td") ) {
                   9686:       nd_f4_td = value?1:0;
                   9687:     } else if ( !strcmp(key,"sba_f4step") ) {
                   9688:       nd_sba_f4step = value?ZTOS((Q)value):0;
                   9689:     } else if ( !strcmp(key,"sba_pot") ) {
                   9690:       nd_sba_pot = value?1:0;
                   9691:     } else if ( !strcmp(key,"sba_largelcm") ) {
                   9692:       nd_sba_largelcm = value?1:0;
1.39    ! noro     9693:     } else if ( !strcmp(key,"sba_dontsort") ) {
        !          9694:       nd_sba_dontsort = value?1:0;
        !          9695:     } else if ( !strcmp(key,"top") ) {
        !          9696:       nd_top = value?1:0;
1.1       noro     9697:     }
1.38      noro     9698:   }
1.1       noro     9699: }
                   9700:
                   9701: ND mdptond(DP d);
                   9702: ND nd_mul_nm(int mod,NM m0,ND p);
                   9703: ND nd_mul_nm_lf(NM m0,ND p);
                   9704: ND *btog(NODE ti,ND **p,int nb,int mod);
                   9705: ND btog_one(NODE ti,ND *p,int nb,int mod);
                   9706: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
                   9707: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
                   9708:
                   9709: /* d:monomial */
                   9710: ND mdptond(DP d)
                   9711: {
                   9712:   NM m;
                   9713:   ND r;
                   9714:
                   9715:   if ( OID(d) == 1 )
                   9716:   r = ptond(CO,CO,(P)d);
                   9717:   else {
                   9718:     NEWNM(m);
                   9719:     dltondl(NV(d),BDY(d)->dl,DL(m));
1.6       noro     9720:     CZ(m) = (Z)BDY(d)->c;
1.1       noro     9721:     NEXT(m) = 0;
                   9722:     MKND(NV(d),m,1,r);
                   9723:   }
                   9724:     return r;
                   9725: }
                   9726:
                   9727: ND nd_mul_nm(int mod,NM m0,ND p)
                   9728: {
                   9729:   UINT *d0;
                   9730:   int c0,c1,c;
                   9731:   NM tm,mr,mr0;
                   9732:   ND r;
                   9733:
                   9734:   if ( !p ) return 0;
                   9735:   d0 = DL(m0);
                   9736:   c0 = CM(m0);
                   9737:   mr0 = 0;
                   9738:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   9739:     NEXTNM(mr0,mr);
                   9740:   c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
                   9741:   ndl_add(d0,DL(tm),DL(mr));
                   9742:   }
                   9743:   NEXT(mr) = 0;
                   9744:   MKND(NV(p),mr0,LEN(p),r);
                   9745:   return r;
                   9746: }
                   9747:
                   9748: ND nd_mul_nm_lf(NM m0,ND p)
                   9749: {
                   9750:   UINT *d0;
                   9751:   Z c0,c1,c;
                   9752:   NM tm,mr,mr0;
                   9753:   ND r;
                   9754:
                   9755:   if ( !p ) return 0;
                   9756:   d0 = DL(m0);
                   9757:   c0 = CZ(m0);
                   9758:   mr0 = 0;
                   9759:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   9760:     NEXTNM(mr0,mr);
                   9761:     c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
                   9762:     ndl_add(d0,DL(tm),DL(mr));
                   9763:   }
                   9764:   NEXT(mr) = 0;
                   9765:   MKND(NV(p),mr0,LEN(p),r);
                   9766:   return r;
                   9767: }
                   9768:
                   9769: ND *btog(NODE ti,ND **p,int nb,int mod)
                   9770: {
                   9771:   PGeoBucket *r;
                   9772:   int i,ci;
                   9773:   NODE t,s;
                   9774:   ND m,tp;
                   9775:   ND *pi,*rd;
                   9776:   P c;
                   9777:
                   9778:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   9779:   for ( i = 0; i < nb; i++ )
                   9780:     r[i] = create_pbucket();
                   9781:   for ( t = ti; t; t = NEXT(t) ) {
                   9782:   s = BDY((LIST)BDY(t));
                   9783:     if ( ARG0(s) ) {
                   9784:     m = mdptond((DP)ARG2(s));
1.6       noro     9785:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     9786:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   9787:       HCM(m) = ci;
1.6       noro     9788:       pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     9789:       for ( i = 0; i < nb; i++ ) {
                   9790:       tp = nd_mul_nm(mod,BDY(m),pi[i]);
                   9791:         add_pbucket(mod,r[i],tp);
                   9792:       }
                   9793:     }
                   9794:     ci = 1;
                   9795:     } else {
                   9796:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   9797:     ci = invm(ci,mod);
                   9798:   }
                   9799:   }
                   9800:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   9801:   for ( i = 0; i < nb; i++ )
                   9802:   rd[i] = normalize_pbucket(mod,r[i]);
                   9803:   if ( ci != 1 )
                   9804:     for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
                   9805:    return rd;
                   9806: }
                   9807:
                   9808: /* YYY */
                   9809: ND *btog_lf(NODE ti,ND **p,int nb)
                   9810: {
                   9811:   PGeoBucket *r;
                   9812:   int i;
                   9813:   NODE t,s;
                   9814:   ND m,tp;
                   9815:   ND *pi,*rd;
                   9816:   LM lm;
                   9817:   Z lf,c;
                   9818:
                   9819:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   9820:   for ( i = 0; i < nb; i++ )
                   9821:     r[i] = create_pbucket();
                   9822:   for ( t = ti; t; t = NEXT(t) ) {
                   9823:     s = BDY((LIST)BDY(t));
                   9824:     if ( ARG0(s) ) {
                   9825:       m = mdptond((DP)ARG2(s));
1.6       noro     9826:       simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1       noro     9827:       if ( lm ) {
                   9828:         lmtolf(lm,&lf); HCZ(m) = lf;
1.6       noro     9829:         pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     9830:         for ( i = 0; i < nb; i++ ) {
                   9831:           tp = nd_mul_nm_lf(BDY(m),pi[i]);
                   9832:           add_pbucket(-2,r[i],tp);
                   9833:         }
                   9834:       }
                   9835:       c = ONE;
                   9836:     } else {
                   9837:       simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
                   9838:     }
                   9839:   }
                   9840:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   9841:   for ( i = 0; i < nb; i++ )
                   9842:     rd[i] = normalize_pbucket(-2,r[i]);
                   9843:   for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
                   9844:   return rd;
                   9845: }
                   9846:
                   9847: ND btog_one(NODE ti,ND *p,int nb,int mod)
                   9848: {
                   9849:   PGeoBucket r;
                   9850:   int i,ci,j;
                   9851:   NODE t,s;
                   9852:   ND m,tp;
                   9853:   ND pi,rd;
                   9854:   P c;
                   9855:
                   9856:   r = create_pbucket();
                   9857:   for ( t = ti; t; t = NEXT(t) ) {
                   9858:   s = BDY((LIST)BDY(t));
                   9859:     if ( ARG0(s) ) {
                   9860:     m = mdptond((DP)ARG2(s));
1.6       noro     9861:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     9862:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   9863:       HCM(m) = ci;
1.6       noro     9864:       pi = p[j=ZTOS((Q)ARG1(s))];
1.1       noro     9865:     if ( !pi ) {
                   9866:       pi = nd_load_mod(j);
                   9867:       tp = nd_mul_nm(mod,BDY(m),pi);
                   9868:       nd_free(pi);
                   9869:         add_pbucket(mod,r,tp);
                   9870:     } else {
                   9871:       tp = nd_mul_nm(mod,BDY(m),pi);
                   9872:         add_pbucket(mod,r,tp);
                   9873:       }
                   9874:     }
                   9875:     ci = 1;
                   9876:     } else {
                   9877:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   9878:     ci = invm(ci,mod);
                   9879:   }
                   9880:   }
                   9881:   rd = normalize_pbucket(mod,r);
                   9882:   free_pbucket(r);
                   9883:   if ( ci != 1 ) nd_mul_c(mod,rd,ci);
                   9884:   return rd;
                   9885: }
                   9886:
                   9887: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
                   9888:
                   9889: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
                   9890: {
                   9891:   int i,j,n,m,nb,pi0,pi1,nvar;
                   9892:   VL fv,tv,vv;
                   9893:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   9894:   ND **p;
                   9895:   ND *c;
                   9896:   ND u;
                   9897:   P inv;
                   9898:   MAT mat;
                   9899:
                   9900:   if ( mod == -2 )
                   9901:     return nd_btog_lf(f,v,ord,tlist,rp);
                   9902:
                   9903:   parse_nd_option(current_option);
                   9904:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   9905:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   9906:   switch ( ord->id ) {
                   9907:     case 1:
                   9908:       if ( ord->nv != nvar )
                   9909:         error("nd_check : invalid order specification");
                   9910:       break;
                   9911:     default:
                   9912:       break;
                   9913:   }
                   9914:   nd_init_ord(ord);
                   9915: #if 0
1.6       noro     9916:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     9917: #else
                   9918:   nd_bpe = 32;
                   9919: #endif
                   9920:   nd_setup_parameters(nvar,0);
                   9921:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   9922:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   9923:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   9924:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   9925:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     9926:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     9927:   if ( j > i ) i = j;
                   9928:   }
                   9929:   n = i+1;
                   9930:   nb = length(BDY(f));
                   9931:   p = (ND **)MALLOC(n*sizeof(ND *));
                   9932:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   9933:     pi = BDY((LIST)BDY(t));
1.6       noro     9934:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     9935:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   9936:     ptomp(mod,(P)ARG2(pi),&inv);
                   9937:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   9938:     u = ptond(CO,vv,(P)ONE);
                   9939:     HCM(u) = ((MQ)inv)->cont;
                   9940:     c[pi1] = u;
                   9941:   }
                   9942:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   9943:     printf("%d ",i); fflush(stdout);
                   9944:     ti = BDY((LIST)BDY(t));
1.6       noro     9945:     p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     9946:   }
                   9947:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   9948:     printf("%d ",i); fflush(stdout);
                   9949:     ti = BDY((LIST)BDY(t));
1.6       noro     9950:     p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     9951:   }
                   9952:   m = length(ind);
                   9953:   MKMAT(mat,nb,m);
                   9954:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     9955:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     9956:       BDY(mat)[i][j] = ndtodp(mod,c[i]);
                   9957:   return mat;
                   9958: }
                   9959:
                   9960: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
                   9961: {
                   9962:   int i,j,n,m,nb,pi0,pi1,nvar;
                   9963:   VL fv,tv,vv;
                   9964:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   9965:   ND **p;
                   9966:   ND *c;
                   9967:   ND u;
                   9968:   MAT mat;
                   9969:   LM lm;
                   9970:   Z lf,inv;
                   9971:
                   9972:   parse_nd_option(current_option);
                   9973:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   9974:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   9975:   switch ( ord->id ) {
                   9976:     case 1:
                   9977:       if ( ord->nv != nvar )
                   9978:         error("nd_check : invalid order specification");
                   9979:       break;
                   9980:     default:
                   9981:       break;
                   9982:   }
                   9983:   nd_init_ord(ord);
                   9984: #if 0
1.6       noro     9985:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     9986: #else
                   9987:   nd_bpe = 32;
                   9988: #endif
                   9989:   nd_setup_parameters(nvar,0);
                   9990:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   9991:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   9992:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   9993:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   9994:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     9995:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     9996:   if ( j > i ) i = j;
                   9997:   }
                   9998:   n = i+1;
                   9999:   nb = length(BDY(f));
                   10000:   p = (ND **)MALLOC(n*sizeof(ND *));
                   10001:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   10002:     pi = BDY((LIST)BDY(t));
1.6       noro     10003:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     10004:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   10005:     simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
                   10006:     u = ptond(CO,vv,(P)ONE);
                   10007:     HCZ(u) = inv;
                   10008:     c[pi1] = u;
                   10009:   }
                   10010:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   10011:     printf("%d ",i); fflush(stdout);
                   10012:     ti = BDY((LIST)BDY(t));
1.6       noro     10013:     p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     10014:   }
                   10015:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   10016:     printf("%d ",i); fflush(stdout);
                   10017:     ti = BDY((LIST)BDY(t));
1.6       noro     10018:     p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     10019:   }
                   10020:   m = length(ind);
                   10021:   MKMAT(mat,nb,m);
                   10022:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     10023:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     10024:       BDY(mat)[i][j] = ndtodp(-2,c[i]);
                   10025:   return mat;
                   10026: }
                   10027:
                   10028: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
                   10029:   LIST tlist,int pos,MAT *rp)
                   10030: {
                   10031:   int i,j,n,m,nb,pi0,pi1,nvar;
                   10032:   VL fv,tv,vv;
                   10033:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   10034:   ND *p;
                   10035:   ND *c;
                   10036:   ND u;
                   10037:   P inv;
                   10038:   VECT vect;
                   10039:
                   10040:   if ( mod == -2 )
                   10041:     error("nd_btog_one : not implemented yet for a large finite field");
                   10042:
                   10043:   parse_nd_option(current_option);
                   10044:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   10045:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   10046:   switch ( ord->id ) {
                   10047:     case 1:
                   10048:       if ( ord->nv != nvar )
                   10049:         error("nd_check : invalid order specification");
                   10050:       break;
                   10051:     default:
                   10052:       break;
                   10053:   }
                   10054:   nd_init_ord(ord);
                   10055: #if 0
1.6       noro     10056:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     10057: #else
                   10058:   nd_bpe = 32;
                   10059: #endif
                   10060:   nd_setup_parameters(nvar,0);
                   10061:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   10062:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   10063:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   10064:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   10065:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     10066:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     10067:   if ( j > i ) i = j;
                   10068:   }
                   10069:   n = i+1;
                   10070:   nb = length(BDY(f));
                   10071:   p = (ND *)MALLOC(n*sizeof(ND *));
                   10072:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   10073:     pi = BDY((LIST)BDY(t));
1.6       noro     10074:   pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     10075:   if ( pi1 == pos ) {
                   10076:     ptomp(mod,(P)ARG2(pi),&inv);
                   10077:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   10078:     u = ptond(CO,vv,(P)ONE);
                   10079:     HCM(u) = ((MQ)inv)->cont;
                   10080:     p[pi0] = u;
                   10081:     }
                   10082:   }
                   10083:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   10084:   printf("%d ",i); fflush(stdout);
                   10085:     ti = BDY((LIST)BDY(t));
1.6       noro     10086:     p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10087:     if ( Demand ) {
                   10088:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   10089:   }
                   10090:   }
                   10091:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   10092:   printf("%d ",i); fflush(stdout);
                   10093:     ti = BDY((LIST)BDY(t));
1.6       noro     10094:     p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10095:     if ( Demand ) {
                   10096:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   10097:   }
                   10098:   }
                   10099:   m = length(ind);
                   10100:   MKVECT(vect,m);
                   10101:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6       noro     10102:   u = p[ZTOS((Q)BDY(t))];
1.1       noro     10103:   if ( !u ) {
1.6       noro     10104:     u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1       noro     10105:     BDY(vect)[j] = ndtodp(mod,u);
                   10106:     nd_free(u);
                   10107:   } else
                   10108:     BDY(vect)[j] = ndtodp(mod,u);
                   10109:   }
                   10110:   return vect;
                   10111: }
                   10112:
                   10113: void ndv_print_lf(NDV p)
                   10114: {
                   10115:     NMV m;
                   10116:     int i,len;
                   10117:
                   10118:     if ( !p ) printf("0\n");
                   10119:     else {
                   10120:         len = LEN(p);
                   10121:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   10122:             printf("+");
                   10123:             mpz_out_str(asir_out,10,BDY(CZ(m)));
                   10124:             printf("*");
                   10125:             ndl_print(DL(m));
                   10126:         }
                   10127:         printf("\n");
                   10128:     }
                   10129: }
                   10130:
                   10131: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
                   10132: {
                   10133:     VL tv,fv,vv,vc,av;
                   10134:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   10135:     int m,nocheck,nvar,mindex,e,max;
                   10136:     NDV c;
                   10137:     NMV a;
                   10138:     P p,zp;
                   10139:     Q dmy;
                   10140:     EPOS oepos;
                   10141:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   10142:     Alg alpha,dp;
                   10143:     P poly;
                   10144:     LIST f1,f2,zpl;
                   10145:     Obj obj;
                   10146:     NumberField nf;
                   10147:     struct order_spec *ord1;
                   10148:     struct oEGT eg_check,eg0,eg1;
                   10149:     NODE tr,tl1,tl2,tl3,tl4;
                   10150:     LIST l1,l2,l3,l4,l5;
                   10151:     int *perm;
                   10152:     int j,ret;
                   10153:     NODE retn;
                   10154:     Q jq,bpe;
                   10155:
                   10156:     nd_module = 0;
                   10157:     parse_nd_option(current_option);
                   10158:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   10159:     if ( nd_vc )
                   10160:       error("nd_f4_lf_trace : computation over a rational function field is not implemented");
                   10161:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   10162:     switch ( ord->id ) {
                   10163:         case 1:
                   10164:             if ( ord->nv != nvar )
                   10165:                 error("nd_f4_lf_trace : invalid order specification");
                   10166:             break;
                   10167:         default:
                   10168:             break;
                   10169:     }
                   10170:
                   10171:     nd_ntrans = nvar;
                   10172:     nd_nalg = 0;
                   10173:
                   10174:     nocheck = 0;
                   10175:     mindex = 0;
                   10176:
                   10177:     /* do not use on-demand load/save */
                   10178:     nd_demand = 0;
                   10179:     m = trace > 1 ? trace : get_lprime(mindex);
                   10180:     nd_init_ord(ord);
                   10181:     mrank = 0;
                   10182:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   10183:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   10184:             if ( nd_module ) {
                   10185:                 s = BDY((LIST)BDY(t));
                   10186:                 trank = length(s);
                   10187:                 mrank = MAX(mrank,trank);
                   10188:                 for ( ; s; s = NEXT(s) ) {
                   10189:                     e = getdeg(tv->v,(P)BDY(s));
                   10190:                     max = MAX(e,max);
                   10191:                 }
                   10192:             } else {
                   10193:                 e = getdeg(tv->v,(P)BDY(t));
                   10194:                 max = MAX(e,max);
                   10195:             }
                   10196:         }
                   10197:     nd_setup_parameters(nvar,max);
                   10198:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   10199:     ishomo = 1;
                   10200:     /* XXX */
                   10201:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   10202:         if ( nd_module ) {
                   10203:             c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
                   10204:         } else {
                   10205:             c = (pointer)ptondv(CO,vv,(P)BDY(t));
                   10206:         }
                   10207:         if ( ishomo )
                   10208:             ishomo = ishomo && ndv_ishomo(c);
                   10209:         if ( c ) {
                   10210:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   10211:             ndv_mod(-2,c);
                   10212:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   10213:         }
                   10214:     }
                   10215:     if ( in0 ) NEXT(in) = 0;
                   10216:     if ( fd0 ) NEXT(fd) = 0;
                   10217:     if ( !ishomo && homo ) {
                   10218:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   10219:             c = (NDV)BDY(t); len = LEN(c);
                   10220:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   10221:                 wmax = MAX(TD(DL(a)),wmax);
                   10222:         }
                   10223:         homogenize_order(ord,nvar,&ord1);
                   10224:         nd_init_ord(ord1);
                   10225:         nd_setup_parameters(nvar+1,wmax);
                   10226:         for ( t = fd0; t; t = NEXT(t) )
                   10227:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   10228:     }
                   10229:     if ( MaxDeg > 0 ) nocheck = 1;
1.24      noro     10230:     ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1       noro     10231:     if ( ret )
                   10232:       cand = nd_f4_lf_trace_main(m,&perm);
                   10233:     if ( !ret || !cand ) {
                   10234:        *rp = 0; return;
                   10235:     }
                   10236:     if ( !ishomo && homo ) {
                   10237:       /* dehomogenization */
                   10238:       for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   10239:       nd_init_ord(ord);
                   10240:       nd_setup_parameters(nvar,0);
                   10241:     }
                   10242:     cand = ndv_reducebase(cand,perm);
                   10243:     cand = ndv_reduceall(-2,cand);
                   10244:     cbpe = nd_bpe;
                   10245:     get_eg(&eg0);
                   10246:     if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   10247:       /* gbcheck : cand is a GB of Id(cand) ? */
                   10248:       retn = nd_f4(-2,0,0);
                   10249:     }
                   10250:   if ( !retn ) {
                   10251:       /* failure */
                   10252:       *rp = 0; return;
                   10253:     }
                   10254:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   10255:     if ( DP_Print )
1.5       noro     10256:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     10257:     /* dp->p */
                   10258:     nd_bpe = cbpe;
                   10259:     nd_setup_parameters(nd_nvar,0);
                   10260:     for ( r = cand; r; r = NEXT(r) ) {
                   10261:       if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
                   10262:       else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
                   10263:     }
                   10264:     MKLIST(*rp,cand);
                   10265: }
                   10266:
                   10267: NODE nd_f4_lf_trace_main(int m,int **indp)
                   10268: {
                   10269:     int i,nh,stat,index;
                   10270:     NODE r,rm,g;
                   10271:     ND_pairs d,l,l0,t;
                   10272:     ND spol,red;
                   10273:     NDV nf,redv,nfqv,nfv;
                   10274:     NM s0,s;
                   10275:     NODE rp0,srp0,nflist,nflist_lf;
                   10276:     int nsp,nred,col,rank,len,k,j,a;
                   10277:     UINT c;
                   10278:     UINT **spmat;
                   10279:     UINT *s0vect,*svect,*p,*v;
                   10280:     int *colstat;
                   10281:     IndArray *imat;
                   10282:     int *rhead;
                   10283:     int spcol,sprow;
                   10284:     int sugar;
                   10285:     PGeoBucket bucket;
                   10286:     struct oEGT eg0,eg1,eg_f4;
                   10287:
                   10288:     g = 0; d = 0;
                   10289:     for ( i = 0; i < nd_psn; i++ ) {
                   10290:         d = update_pairs(d,g,i,0);
                   10291:         g = update_base(g,i);
                   10292:     }
                   10293:     while ( d ) {
                   10294:         get_eg(&eg0);
                   10295:         l = nd_minsugarp(d,&d);
                   10296:         sugar = SG(l);
                   10297:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   10298:         bucket = create_pbucket();
                   10299:         stat = nd_sp_f4(m,0,l,bucket);
                   10300:         if ( !stat ) {
                   10301:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10302:             NEXT(t) = d; d = l;
                   10303:             d = nd_reconstruct(1,d);
                   10304:             continue;
                   10305:         }
                   10306:         if ( bucket->m < 0 ) continue;
                   10307:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   10308:         if ( !col ) {
                   10309:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10310:             NEXT(t) = d; d = l;
                   10311:             d = nd_reconstruct(1,d);
                   10312:             continue;
                   10313:         }
                   10314:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   10315:         if ( DP_Print )
1.5       noro     10316:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1       noro     10317:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   10318:         if ( !l0 ) continue;
                   10319:         l = l0;
                   10320:
                   10321:         /* over LF */
                   10322:         bucket = create_pbucket();
                   10323:         stat = nd_sp_f4(-2,1,l,bucket);
                   10324:         if ( !stat ) {
                   10325:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10326:             NEXT(t) = d; d = l;
                   10327:             d = nd_reconstruct(1,d);
                   10328:             continue;
                   10329:         }
                   10330:         if ( bucket->m < 0 ) continue;
                   10331:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   10332:         if ( !col ) {
                   10333:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10334:             NEXT(t) = d; d = l;
                   10335:             d = nd_reconstruct(1,d);
                   10336:             continue;
                   10337:         }
                   10338:         nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
                   10339:         /* adding new bases */
                   10340:         for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
                   10341:             nfv = (NDV)BDY(rm);
                   10342:             nfqv = (NDV)BDY(r);
                   10343:             if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
                   10344:             ndv_removecont(m,nfv);
                   10345:             ndv_removecont(-2,nfqv);
1.24      noro     10346:             nh = ndv_newps(-2,nfv,nfqv);
1.1       noro     10347:             d = update_pairs(d,g,nh,0);
                   10348:             g = update_base(g,nh);
                   10349:         }
                   10350:         if ( r || rm ) return 0;
                   10351:     }
                   10352:   conv_ilist(nd_demand,1,g,indp);
                   10353:     return g;
                   10354: }
                   10355:
1.7       noro     10356: #if SIZEOF_LONG==8
                   10357:
                   10358: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   10359: {
                   10360:     int j,k,len;
                   10361:     UINT *p;
                   10362:     UINT c;
                   10363:     NDV r;
                   10364:     NMV mr0,mr;
                   10365:
                   10366:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   10367:     if ( !len ) return 0;
                   10368:     else {
                   10369:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   10370: #if 0
                   10371:         ndv_alloc += nmv_adv*len;
                   10372: #endif
                   10373:         mr = mr0;
                   10374:         p = s0vect;
                   10375:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   10376:             if ( !rhead[j] ) {
                   10377:                 if ( (c = (UINT)vect[k++]) != 0 ) {
                   10378:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   10379:                 }
                   10380:             }
                   10381:         MKNDV(nd_nvar,mr0,len,r);
                   10382:         return r;
                   10383:     }
                   10384: }
                   10385:
1.28      noro     10386: NDV vect64_to_ndv_s(mp_limb_t *vect,int col,UINT *s0vect)
                   10387: {
                   10388:     int j,k,len;
                   10389:     UINT *p;
                   10390:     UINT c;
                   10391:     NDV r;
                   10392:     NMV mr0,mr;
                   10393:
                   10394:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   10395:     if ( !len ) return 0;
                   10396:     else {
                   10397:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   10398:         mr = mr0;
                   10399:         p = s0vect;
                   10400:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   10401:           if ( (c = (UINT)vect[k++]) != 0 ) {
                   10402:             ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   10403:           }
                   10404:         MKNDV(nd_nvar,mr0,len,r);
                   10405:         return r;
                   10406:     }
                   10407: }
                   10408:
1.7       noro     10409: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
                   10410: {
                   10411:     NM m;
1.11      noro     10412:     UINT *t,*s,*u;
                   10413:     int i,st,ed,md,prev,c;
1.7       noro     10414:
                   10415:     for ( i = 0; i < n; i++ ) r[i] = 0;
1.11      noro     10416:     prev = 0;
                   10417:     for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
                   10418:       t = DL(m);
                   10419:       st = prev;
                   10420:       ed = n;
                   10421:       while ( ed > st ) {
                   10422:         md = (st+ed)/2;
                   10423:         u = s0+md*nd_wpd;
                   10424:         c = DL_COMPARE(u,t);
                   10425:         if ( c == 0 ) break;
                   10426:         else if ( c > 0 ) st = md;
                   10427:         else ed = md;
                   10428:       }
                   10429:       r[md] = (mp_limb_t)CM(m);
                   10430:       prev = md;
1.7       noro     10431:     }
                   10432:     for ( i = 0; !r[i]; i++ );
                   10433:     return i;
                   10434: }
                   10435:
                   10436: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
                   10437:
1.28      noro     10438: 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     10439: {
                   10440:     int i,j,k,len,pos,prev;
                   10441:     mp_limb_t a,c,c1,c2;
                   10442:     IndArray ivect;
                   10443:     unsigned char *ivc;
                   10444:     unsigned short *ivs;
                   10445:     unsigned int *ivi;
                   10446:     NDV redv;
                   10447:     NMV mr;
                   10448:     NODE rp;
                   10449:     int maxrs;
                   10450:
                   10451:     for ( i = 0; i < col; i++ ) cvect[i] = 0;
                   10452:     maxrs = 0;
                   10453:     for ( i = 0; i < nred; i++ ) {
                   10454:         ivect = imat[i];
                   10455:         k = ivect->head;
                   10456:         a = svect[k]; c = cvect[k];
                   10457:         MOD128(a,c,m);
                   10458:         svect[k] = a; cvect[k] = 0;
1.28      noro     10459:         if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.11      noro     10460:             Nf4_red++;
1.7       noro     10461:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   10462:             c = m-c; redv = nd_ps[rp0[i]->index];
                   10463:             len = LEN(redv); mr = BDY(redv);
                   10464:             svect[k] = 0; prev = k;
                   10465:             switch ( ivect->width ) {
                   10466:                 case 1:
                   10467:                     ivc = ivect->index.c;
                   10468:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   10469:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12      noro     10470:                         c2 = svect[pos]+c1*c;
                   10471:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   10472:                         svect[pos] = c2;
1.7       noro     10473:                     }
                   10474:                     break;
                   10475:                 case 2:
                   10476:                     ivs = ivect->index.s;
                   10477:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   10478:                         pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12      noro     10479:                         c2 = svect[pos]+c1*c;
                   10480:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   10481:                         svect[pos] = c2;
1.7       noro     10482:                     }
                   10483:                     break;
                   10484:                 case 4:
                   10485:                     ivi = ivect->index.i;
                   10486:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   10487:                         pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12      noro     10488:                         c2 = svect[pos]+c1*c;
                   10489:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   10490:                         svect[pos] = c2;
1.7       noro     10491:                     }
                   10492:                     break;
                   10493:             }
                   10494:         }
                   10495:     }
                   10496:     for ( i = 0; i < col; i++ ) {
                   10497:       a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
                   10498:     }
                   10499:     return maxrs;
                   10500: }
                   10501:
                   10502: /* for Fp, 2^15=<p<2^29 */
                   10503:
                   10504: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   10505:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   10506: {
                   10507:     int spcol,sprow,a;
                   10508:     int i,j,k,l,rank;
                   10509:     NODE r0,r;
                   10510:     ND_pairs sp;
                   10511:     ND spol;
                   10512:     mp_limb_t **spmat;
                   10513:     mp_limb_t *svect,*cvect;
                   10514:     mp_limb_t *v;
                   10515:     int *colstat;
                   10516:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   10517:     int maxrs;
                   10518:     int *spsugar;
                   10519:     ND_pairs *spactive;
                   10520:
                   10521:     spcol = col-nred;
                   10522:     get_eg(&eg0);
                   10523:     /* elimination (1st step) */
                   10524:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   10525:     svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   10526:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   10527:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   10528:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   10529:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   10530:         nd_sp(m,0,sp,&spol);
                   10531:         if ( !spol ) continue;
                   10532:         nd_to_vect64(m,s0vect,col,spol,svect);
1.28      noro     10533:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,0);
1.7       noro     10534:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   10535:         if ( i < col ) {
                   10536:             spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
                   10537:             for ( j = k = 0; j < col; j++ )
                   10538:                 if ( !rhead[j] ) v[k++] = (UINT)svect[j];
                   10539:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   10540:             if ( nz )
                   10541:             spactive[sprow] = sp;
                   10542:             sprow++;
                   10543:         }
                   10544:         nd_free(spol);
                   10545:     }
1.12      noro     10546:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7       noro     10547:     if ( DP_Print ) {
                   10548:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   10549:         fflush(asir_out);
                   10550:     }
                   10551:     /* free index arrays */
                   10552:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   10553:
                   10554:     /* elimination (2nd step) */
                   10555:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   10556:     rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   10557:     r0 = 0;
                   10558:     for ( i = 0; i < rank; i++ ) {
                   10559:         NEXTNODE(r0,r); BDY(r) =
                   10560:           (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   10561:         SG((NDV)BDY(r)) = spsugar[i];
                   10562:         GCFREE(spmat[i]);
                   10563:     }
                   10564:     if ( r0 ) NEXT(r) = 0;
                   10565:
                   10566:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12      noro     10567:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7       noro     10568:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   10569:     if ( DP_Print ) {
                   10570:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   10571:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   10572:             nsp,nred,sprow,spcol,rank);
                   10573:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   10574:     }
                   10575:     if ( nz ) {
                   10576:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   10577:         if ( rank > 0 ) {
                   10578:             NEXT(spactive[rank-1]) = 0;
                   10579:             *nz = spactive[0];
                   10580:         } else
                   10581:             *nz = 0;
                   10582:     }
                   10583:     return r0;
                   10584: }
                   10585:
                   10586: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   10587: {
                   10588:   int i,j,k,l,rank,s;
                   10589:   mp_limb_t inv;
                   10590:   mp_limb_t a;
                   10591:   UINT c;
                   10592:   mp_limb_t *t,*pivot,*pk;
                   10593:   UINT *ck;
                   10594:   UINT **cmat;
                   10595:   UINT *ct;
                   10596:   ND_pairs pair;
                   10597:
                   10598:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   10599:   for ( i = 0; i < row; i++ ) {
                   10600:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   10601:     bzero(cmat[i],col*sizeof(UINT));
                   10602:   }
                   10603:
                   10604:   for ( rank = 0, j = 0; j < col; j++ ) {
                   10605:     for ( i = rank; i < row; i++ ) {
                   10606:       a = mat[i][j]; c = cmat[i][j];
                   10607:       MOD128(a,c,md);
                   10608:       mat[i][j] = a; cmat[i][j] = 0;
                   10609:     }
                   10610:     for ( i = rank; i < row; i++ )
                   10611:       if ( mat[i][j] )
                   10612:         break;
                   10613:     if ( i == row ) {
                   10614:       colstat[j] = 0;
                   10615:       continue;
                   10616:     } else
                   10617:       colstat[j] = 1;
                   10618:     if ( i != rank ) {
                   10619:       t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   10620:       ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
                   10621:       s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   10622:       if ( spactive ) {
                   10623:         pair = spactive[i]; spactive[i] = spactive[rank];
                   10624:         spactive[rank] = pair;
                   10625:       }
                   10626:     }
                   10627:     /* column j is normalized */
                   10628:     s = sugar[rank];
                   10629:     inv = invm((UINT)mat[rank][j],md);
                   10630:     /* normalize pivot row */
                   10631:     for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
                   10632:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   10633:     }
                   10634:     for ( i = rank+1; i < row; i++ ) {
                   10635:       if ( (a = mat[i][j]) != 0 ) {
                   10636:         sugar[i] = MAX(sugar[i],s);
                   10637:         red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11      noro     10638:         Nf4_red++;
1.7       noro     10639:       }
                   10640:     }
                   10641:     rank++;
                   10642:   }
                   10643:   for ( j = col-1, l = rank-1; j >= 0; j-- )
                   10644:     if ( colstat[j] ) {
                   10645:       for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
                   10646:         a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
                   10647:       }
                   10648:       s = sugar[l];
                   10649:       for ( i = 0; i < l; i++ ) {
                   10650:         a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
                   10651:         if ( a ) {
                   10652:           sugar[i] = MAX(sugar[i],s);
                   10653:           red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11      noro     10654:           Nf4_red++;
1.7       noro     10655:         }
                   10656:       }
                   10657:       l--;
                   10658:     }
                   10659:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   10660:   GCFREE(cmat);
                   10661:   return rank;
                   10662: }
                   10663: #endif
                   10664:
1.28      noro     10665: 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)
                   10666: {
                   10667:   int i,j,k,l,rank,s,imin;
                   10668:   mp_limb_t inv;
                   10669:   mp_limb_t a;
                   10670:   UINT c;
                   10671:   mp_limb_t *t,*pivot,*pk;
                   10672:   UINT *ck;
                   10673:   UINT **cmat;
                   10674:   UINT *ct;
                   10675:   ND_pairs pair;
                   10676:   SIG sg;
1.31      noro     10677:   int *used;
1.28      noro     10678:
1.31      noro     10679:   used = (int *)MALLOC(row*sizeof(int));
1.28      noro     10680:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   10681:   for ( i = 0; i < row; i++ ) {
                   10682:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   10683:     bzero(cmat[i],col*sizeof(UINT));
                   10684:   }
                   10685:
1.31      noro     10686:   for ( j = 0; j < col; j++ ) {
                   10687:     for ( i = 0; i < row; i++ ) {
1.28      noro     10688:       a = mat[i][j]; c = cmat[i][j];
                   10689:       MOD128(a,c,md);
                   10690:       mat[i][j] = a; cmat[i][j] = 0;
                   10691:     }
1.31      noro     10692:     for ( i = 0; i < row; i++ )
                   10693:       if ( !used[i] && mat[i][j] ) break;
                   10694:     if ( i == row ) {
1.28      noro     10695:       colstat[j] = 0;
                   10696:       continue;
1.31      noro     10697:     } else {
1.28      noro     10698:       colstat[j] = 1;
1.31      noro     10699:       used[i] = 1;
1.28      noro     10700:     }
                   10701:     /* column j is normalized */
1.31      noro     10702:     s = sugar[i];
                   10703:     inv = invm((UINT)mat[i][j],md);
1.28      noro     10704:     /* normalize pivot row */
1.31      noro     10705:     for ( k = j, pk = mat[i]+j, ck = cmat[i]+j; k < col; k++, pk++, ck++ ) {
1.28      noro     10706:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   10707:     }
1.31      noro     10708:     for ( k = i+1; k < row; k++ ) {
                   10709:       if ( (a = mat[k][j]) != 0 ) {
                   10710:         sugar[k] = MAX(sugar[k],s);
                   10711:         red_by_vect64(md,mat[k]+j,cmat[k]+j,mat[i]+j,(int)(md-a),col-j);
1.28      noro     10712:         Nf4_red++;
                   10713:       }
                   10714:     }
                   10715:   }
1.31      noro     10716:   rank = 0;
                   10717:   for ( i = 0; i < row; i++ ) {
                   10718:     for ( j = 0; j < col; j++ )
                   10719:       if ( mat[i][j] ) break;
                   10720:     if ( j == col ) sugar[i] = -1;
                   10721:     else rank++;
                   10722:   }
1.28      noro     10723:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   10724:   GCFREE(cmat);
                   10725:   return rank;
                   10726: }
                   10727:
                   10728: NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   10729:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
                   10730: {
                   10731:     int spcol,sprow,a;
                   10732:     int i,j,k,l,rank;
                   10733:     NODE r0,r;
                   10734:     ND_pairs sp;
                   10735:     ND spol;
                   10736:     mp_limb_t **spmat;
                   10737:     mp_limb_t *svect,*cvect;
                   10738:     mp_limb_t *v;
                   10739:     int *colstat;
                   10740:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   10741:     int maxrs;
                   10742:     int *spsugar;
                   10743:     ND_pairs *spactive;
                   10744:     SIG *spsig;
                   10745:
                   10746:     get_eg(&eg0);
                   10747:     /* elimination (1st step) */
                   10748:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   10749:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   10750:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   10751:     spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
                   10752:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   10753:         nd_sp(m,0,sp,&spol);
                   10754:         if ( !spol ) {
1.29      noro     10755:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     10756:           continue;
                   10757:         }
                   10758:         svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   10759:         nd_to_vect64(m,s0vect,col,spol,svect);
                   10760:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,spol->sig);
                   10761:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   10762:         if ( i < col ) {
                   10763:             spmat[sprow] = svect;
                   10764:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   10765:             spsig[sprow] = sp->sig;
                   10766:             sprow++;
                   10767:         } else {
1.29      noro     10768:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     10769:         }
                   10770:         nd_free(spol);
                   10771:     }
                   10772:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
                   10773:     if ( DP_Print ) {
                   10774:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   10775:         fflush(asir_out);
                   10776:     }
                   10777:     /* free index arrays */
                   10778:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   10779:
                   10780:     /* elimination (2nd step) */
                   10781:     colstat = (int *)MALLOC(col*sizeof(int));
                   10782:     rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
                   10783:     r0 = 0;
1.31      noro     10784:     for ( i = 0; i < sprow; i++ ) {
                   10785:         if ( spsugar[i] >= 0 ) {
                   10786:           NEXTNODE(r0,r);
                   10787:           BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
                   10788:           SG((NDV)BDY(r)) = spsugar[i];
                   10789:           ((NDV)BDY(r))->sig = spsig[i];
                   10790:         } else
                   10791:           syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
1.28      noro     10792:         GCFREE(spmat[i]);
                   10793:     }
                   10794:     if ( r0 ) NEXT(r) = 0;
                   10795:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
                   10796:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   10797:     if ( DP_Print ) {
                   10798:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   10799:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   10800:             nsp,nred,sprow,col,rank);
                   10801:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   10802:     }
                   10803:     return r0;
                   10804: }
                   10805:
                   10806: NODE nd_f4_red_s(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,NODE *syzlistp)
                   10807: {
                   10808:   IndArray *imat;
                   10809:   int nsp,nred,i,start;
                   10810:   int *rhead;
                   10811:   NODE r0,rp;
                   10812:   ND_pairs sp;
                   10813:   NM_ind_pair *rvect;
                   10814:   UINT *s;
                   10815:   int *s0hash;
                   10816:   struct oEGT eg0,eg1,eg_conv;
                   10817:
                   10818:   for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   10819:   nred = length(rp0);
                   10820:   imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   10821:   rhead = (int *)MALLOC(col*sizeof(int));
                   10822:   for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   10823:
                   10824:   /* construction of index arrays */
                   10825:   get_eg(&eg0);
                   10826:   if ( DP_Print ) {
                   10827:     fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   10828:     fflush(asir_out);
                   10829:   }
                   10830:   rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   10831:   for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   10832:     rvect[i] = (NM_ind_pair)BDY(rp);
                   10833:     imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
                   10834:     rhead[imat[i]->head] = 1;
                   10835:     start = imat[i]->head;
                   10836:   }
                   10837:   get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
                   10838:   if ( DP_Print ) {
                   10839:     fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   10840:     fflush(asir_out);
                   10841:   }
                   10842:   if ( m > 0 )
                   10843:     r0 = nd_f4_red_mod64_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
                   10844:   else
                   10845: //    r0 = nd_f4_red_q_main_s(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   10846:     error("nd_f4_red_q_main_s : not implemented yet");
                   10847:   return r0;
                   10848: }
                   10849:
                   10850: INLINE int ndl_find_reducer_minsig(UINT *dg)
                   10851: {
                   10852:   RHist r;
                   10853:   int i,singular,ret,d,k,imin;
                   10854:   SIG t;
                   10855:   static int wpd,nvar;
                   10856:   static SIG quo,quomin;
                   10857:   static UINT *tmp;
                   10858:
                   10859:   if ( !quo || nvar != nd_nvar ) { NEWSIG(quo); NEWSIG(quomin); }
                   10860:   if ( wpd != nd_wpd ) {
                   10861:     wpd = nd_wpd;
                   10862:     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
                   10863:   }
                   10864: #if 0
                   10865:   d = ndl_hash_value(dg);
                   10866:   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   10867:     if ( ndl_equal(dg,DL(r)) ) {
                   10868:       return r->index;
                   10869:     }
                   10870:   }
                   10871: #endif
                   10872:   imin = -1;
                   10873:   for ( i = 0; i < nd_psn; i++ ) {
                   10874:     r = nd_psh[i];
                   10875:     if ( ndl_reducible(dg,DL(r)) ) {
                   10876:       ndl_sub(dg,DL(r),tmp);
                   10877:       _ndltodl(tmp,DL(quo));
                   10878:       _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
                   10879:       quo->pos = nd_psh[i]->sig->pos;
                   10880:       if ( imin < 0 || comp_sig(quomin,quo) > 0 ) {
                   10881:         t = quo; quo = quomin; quomin = t;
                   10882:         imin = i;
                   10883:       }
                   10884:     }
                   10885:   }
                   10886:   if ( imin == -1 ) return nd_psn;
                   10887:   else {
                   10888: #if 0
                   10889:     nd_append_red(dg,i);
                   10890: #endif
                   10891:     return imin;
                   10892:   }
                   10893: }
                   10894:
                   10895: int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   10896: {
                   10897:   NODE rp0,rp;
                   10898:   NM mul,head,s0,s;
                   10899:   int index,col,i,sugar;
                   10900:   RHist h;
                   10901:   UINT *s0v,*p;
                   10902:   NM_ind_pair pair;
                   10903:   ND red;
                   10904:   NDV *ps;
                   10905:   SIG sig;
                   10906:
                   10907:   s0 = 0; rp0 = 0; col = 0;
                   10908:   if ( nd_demand )
                   10909:     ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   10910:   else
                   10911:     ps = trace?nd_ps_trace:nd_ps;
                   10912:   while ( 1 ) {
                   10913:     head = remove_head_pbucket_symbolic(bucket);
                   10914:     if ( !head ) break;
                   10915:     if ( !s0 ) s0 = head;
                   10916:     else NEXT(s) = head;
                   10917:     s = head;
                   10918:     index = ndl_find_reducer_minsig(DL(head));
                   10919:     if ( index >= 0 && index < nd_psn ) {
                   10920:       h = nd_psh[index];
                   10921:       NEWNM(mul);
                   10922:       ndl_sub(DL(head),DL(h),DL(mul));
                   10923:       if ( ndl_check_bound2(index,DL(mul)) )
                   10924:         return 0;
                   10925:       sugar = TD(DL(mul))+SG(ps[index]);
                   10926:       NEWSIG(sig);
                   10927:       _ndltodl(DL(mul),DL(sig));
                   10928:       _addtodl(nd_nvar,DL(nd_psh[index]->sig),DL(sig));
                   10929:       sig->pos = nd_psh[index]->sig->pos;
                   10930:       MKNM_ind_pair(pair,mul,index,sugar,sig);
                   10931:       red = ndv_mul_nm_symbolic(mul,ps[index]);
                   10932:       add_pbucket_symbolic(bucket,nd_remove_head(red));
                   10933:       NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   10934:     }
                   10935:     col++;
                   10936:   }
                   10937:   if ( rp0 ) NEXT(rp) = 0;
                   10938:   NEXT(s) = 0;
                   10939:   s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   10940:   for ( i = 0, p = s0v, s = s0; i < col;
                   10941:     i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   10942:   *s0vect = s0v;
                   10943:   *r = rp0;
                   10944:
                   10945:   return col;
                   10946: }
                   10947:
                   10948: NODE nd_sba_f4(int m,int **indp)
                   10949: {
1.34      noro     10950:   int i,nh,stat,index,f4red,f4step;
1.33      noro     10951:   int col,rank,len,k,j,a,sugar,nbase,psugar,ms;
                   10952:   NODE r,g,rp0,nflist;
                   10953:   ND_pairs d,l,t;
                   10954:   ND h,nf;
                   10955:   NDV nfv;
                   10956:   union oNDC hc;
                   10957:   UINT *s0vect;
1.28      noro     10958:   UINT c;
                   10959:   PGeoBucket bucket;
1.33      noro     10960:   NODE *syzlist;
                   10961:   SIG sig;
1.28      noro     10962:   struct oEGT eg0,eg1,eg_f4;
1.33      noro     10963:   struct oEGT eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.28      noro     10964:
                   10965:   Nf4_red=0;
1.30      noro     10966:   d = 0;
1.29      noro     10967:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.28      noro     10968:   for ( i = 0; i < nd_psn; i++ ) {
1.30      noro     10969:     d = update_pairs_s(d,i,syzlist);
1.28      noro     10970:   }
1.30      noro     10971:   nd_nbase = nd_psn;
1.28      noro     10972:   f4red = 1;
1.33      noro     10973:   psugar = 0;
1.34      noro     10974:   f4step = 0;
1.28      noro     10975:   while ( d ) {
1.33      noro     10976:     for ( t = d, ms = SG(d); t; t = NEXT(t) )
                   10977:       if ( SG(t) < ms ) ms = SG(t);
1.38      noro     10978:     if ( ms == psugar && f4step >= nd_sba_f4step ) {
1.33      noro     10979: again:
                   10980:       l = d; d = d->next;
                   10981:       if ( small_lcm(l) ) {
                   10982:         if ( DP_Print ) fprintf(asir_out,"M");
                   10983:         continue;
                   10984:       }
                   10985:       sig = l->sig;
                   10986:       stat = nd_sp(m,0,l,&h);
                   10987:       if ( !stat ) {
                   10988:         NEXT(l) = d; d = l;
                   10989:         d = nd_reconstruct(0,d);
                   10990:         goto again;
                   10991:       }
                   10992:   get_eg(&eg1);
                   10993:   #if USE_GEOBUCKET
1.39    ! noro     10994:       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     10995:   #else
1.39    ! noro     10996:       stat = nd_nf_s(m,0,h,nd_ps,!nd_top&&!Top,&nf);
1.33      noro     10997:   #endif
                   10998:   get_eg(&eg2);
                   10999:       if ( !stat ) {
                   11000:         NEXT(l) = d; d = l;
                   11001:         d = nd_reconstruct(0,d);
                   11002:         goto again;
                   11003:       } else if ( stat == -1 ) {
                   11004:         if ( DP_Print ) { printf("S"); fflush(stdout); }
                   11005:         FREENDP(l);
                   11006:       } else if ( nf ) {
                   11007:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   11008:         add_eg(&eg_nf,&eg1,&eg2);
                   11009:         hc = HCU(nf);
                   11010:         nd_removecont(m,nf);
                   11011:         nfv = ndtondv(m,nf); nd_free(nf);
                   11012:         nh = ndv_newps(m,nfv,0);
                   11013:
1.30      noro     11014:         d = update_pairs_s(d,nh,syzlist);
                   11015:         nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.33      noro     11016:         FREENDP(l);
1.28      noro     11017:       } else {
1.33      noro     11018:         add_eg(&eg_nfzero,&eg1,&eg2);
                   11019:        // syzygy
                   11020:   get_eg(&eg1);
                   11021:         d = remove_spair_s(d,sig);
                   11022:   get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29      noro     11023:         syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.33      noro     11024:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   11025:         FREENDP(l);
                   11026:       }
                   11027:     } else {
1.34      noro     11028:       if ( ms != psugar ) f4step = 1;
                   11029:       else f4step++;
1.33      noro     11030: again2:
                   11031:       psugar = ms;
                   11032:       l = nd_minsugarp_s(d,&d);
                   11033:       sugar = nd_sugarweight?d->sugar2:SG(d);
                   11034:       bucket = create_pbucket();
                   11035:       stat = nd_sp_f4(m,0,l,bucket);
                   11036:       if ( !stat ) {
                   11037:         for ( t = l; NEXT(t); t = NEXT(t) );
                   11038:         NEXT(t) = d; d = l;
                   11039:         d = nd_reconstruct(0,d);
                   11040:         goto again2;
                   11041:       }
                   11042:       if ( bucket->m < 0 ) continue;
                   11043:       col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
                   11044:       if ( !col ) {
1.37      noro     11045:         for ( t = l; NEXT(t); t = NEXT(t) )
                   11046:           ;
                   11047:         NEXT(t) = d; d = l;
1.33      noro     11048:         d = nd_reconstruct(0,d);
                   11049:         goto again2;
                   11050:       }
1.34      noro     11051:       if ( DP_Print ) fprintf(asir_out,"\nsugar=%d,",psugar);
1.33      noro     11052:       nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
                   11053:       /* adding new bases */
                   11054:       for ( r = nflist; r; r = NEXT(r) ) {
                   11055:         nfv = (NDV)BDY(r);
1.38      noro     11056:         if ( nd_f4_td ) SG(nfv) = nd_tdeg(nfv);
1.33      noro     11057:         ndv_removecont(m,nfv);
                   11058:         nh = ndv_newps(m,nfv,0);
                   11059:         d = update_pairs_s(d,nh,syzlist);
                   11060:         nd_sba_pos[nfv->sig->pos] = append_one(nd_sba_pos[nfv->sig->pos],nh);
                   11061:       }
                   11062:       for ( i = 0; i < nd_nbase; i++ )
                   11063:         for ( r = syzlist[i]; r; r = NEXT(r) )
                   11064:             d = remove_spair_s(d,(SIG)BDY(r));
                   11065:       d = remove_large_lcm(d);
                   11066:       if ( DP_Print ) {
1.34      noro     11067:         fprintf(asir_out,"f4red=%d,gblen=%d",f4red,nd_psn); fflush(asir_out);
1.33      noro     11068:       }
                   11069:       f4red++;
1.28      noro     11070:     }
                   11071:   }
                   11072:   if ( DP_Print ) {
1.34      noro     11073:     fprintf(asir_out,"\nnumber of red=%d,",Nf4_red);
1.28      noro     11074:   }
1.30      noro     11075:   g = conv_ilist_s(nd_demand,0,indp);
1.28      noro     11076:   return g;
                   11077: }

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