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

1.32    ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.31 2020/07/07 08:04:30 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.26      noro       21: int Nsyz;
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;
                     69: static int *nd_gbblock;
                     70: static NODE nd_nzlist,nd_check_splist;
                     71: static int nd_splist;
                     72: static int *nd_sugarweight;
                     73: static int nd_f4red,nd_rank0,nd_last_nonzero;
1.27      noro       74: static DL *nd_sba_hm;
1.29      noro       75: static NODE *nd_sba_pos;
1.1       noro       76:
                     77: NumberField get_numberfield();
                     78: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
                     79: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
                     80: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
                     81: int nd_monic(int m,ND *p);
                     82: NDV plain_vect_to_ndv_q(Z *mat,int col,UINT *s0vect);
                     83: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank);
                     84: NDV pltondv(VL vl,VL dvl,LIST p);
                     85: void pltozpl(LIST l,Q *cont,LIST *pp);
                     86: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
                     87: void nmtodp(int mod,NM m,DP *r);
1.15      noro       88: void ndltodp(UINT *d,DP *r);
1.1       noro       89: NODE reverse_node(NODE n);
                     90: P ndc_div(int mod,union oNDC a,union oNDC b);
                     91: P ndctop(int mod,union oNDC c);
                     92: void finalize_tracelist(int i,P cont);
                     93: void conv_ilist(int demand,int trace,NODE g,int **indp);
                     94: void parse_nd_option(NODE opt);
                     95: void dltondl(int n,DL dl,UINT *r);
                     96: DP ndvtodp(int mod,NDV p);
                     97: DP ndtodp(int mod,ND p);
1.16      noro       98: DPM ndvtodpm(int mod,NDV p);
                     99: NDV dpmtondv(int mod,DPM p);
                    100: int dpm_getdeg(DPM p,int *rank);
                    101: void dpm_ptozp(DPM p,Z *cont,DPM *r);
                    102: int compdmm(int nv,DMM a,DMM b);
1.1       noro      103:
                    104: void Pdp_set_weight(NODE,VECT *);
                    105: void Pox_cmo_rpc(NODE,Obj *);
                    106:
                    107: ND nd_add_lf(ND p1,ND p2);
                    108: void nd_mul_c_lf(ND p,Z mul);
                    109: void ndv_mul_c_lf(NDV p,Z mul);
                    110: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                    111:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
                    112: NODE nd_f4_red_mod64_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_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                    115:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
                    116: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
                    117: NODE nd_f4_lf_trace_main(int m,int **indp);
                    118: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
                    119:
                    120: extern int lf_lazy;
                    121: extern Z current_mod_lf;
                    122:
                    123: extern int Denominator,DP_Multiple,MaxDeg;
                    124:
                    125: #define BLEN (8*sizeof(unsigned long))
                    126:
                    127: typedef struct matrix {
                    128:   int row,col;
                    129:   unsigned long **a;
                    130: } *matrix;
                    131:
                    132:
                    133: void nd_free_private_storage()
                    134: {
                    135:     _nm_free_list = 0;
                    136:     _ndp_free_list = 0;
                    137: #if 0
                    138:     GC_gcollect();
                    139: #endif
                    140: }
                    141:
                    142: void _NM_alloc()
                    143: {
                    144:     NM p;
                    145:     int i;
                    146:
                    147:     for ( i = 0; i < 1024; i++ ) {
                    148:         p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                    149:         p->next = _nm_free_list; _nm_free_list = p;
                    150:     }
                    151: }
                    152:
                    153: matrix alloc_matrix(int row,int col)
                    154: {
                    155:   unsigned long **a;
                    156:   int i,len,blen;
                    157:   matrix mat;
                    158:
                    159:   mat = (matrix)MALLOC(sizeof(struct matrix));
                    160:   mat->row = row;
                    161:   mat->col = col;
                    162:   mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
                    163:   return mat;
                    164: }
                    165:
                    166:
                    167: void _ND_alloc()
                    168: {
                    169:     ND p;
                    170:     int i;
                    171:
                    172:     for ( i = 0; i < 1024; i++ ) {
                    173:         p = (ND)MALLOC(sizeof(struct oND));
                    174:         p->body = (NM)_nd_free_list; _nd_free_list = p;
                    175:     }
                    176: }
                    177:
                    178: void _NDP_alloc()
                    179: {
                    180:     ND_pairs p;
                    181:     int i;
                    182:
                    183:     for ( i = 0; i < 1024; i++ ) {
                    184:         p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
                    185:             +(nd_wpd-1)*sizeof(UINT));
                    186:         p->next = _ndp_free_list; _ndp_free_list = p;
                    187:     }
                    188: }
                    189:
                    190: INLINE int nd_length(ND p)
                    191: {
                    192:     NM m;
                    193:     int i;
                    194:
                    195:     if ( !p )
                    196:         return 0;
                    197:     else {
                    198:         for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
                    199:         return i;
                    200:     }
                    201: }
                    202:
                    203: extern int dp_negative_weight;
                    204:
                    205: INLINE int ndl_reducible(UINT *d1,UINT *d2)
                    206: {
                    207:     UINT u1,u2;
                    208:     int i,j;
                    209:
                    210:     if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
                    211:
                    212:     if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
                    213: #if USE_UNROLL
                    214:     switch ( nd_bpe ) {
                    215:         case 3:
                    216:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    217:                 u1 = d1[i]; u2 = d2[i];
                    218:                 if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
                    219:                 if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
                    220:                 if ( (u1&  0xe00000) < (u2&  0xe00000) ) return 0;
                    221:                 if ( (u1&  0x1c0000) < (u2&  0x1c0000) ) return 0;
                    222:                 if ( (u1&   0x38000) < (u2&   0x38000) ) return 0;
                    223:                 if ( (u1&    0x7000) < (u2&    0x7000) ) return 0;
                    224:                 if ( (u1&     0xe00) < (u2&     0xe00) ) return 0;
                    225:                 if ( (u1&     0x1c0) < (u2&     0x1c0) ) return 0;
                    226:                 if ( (u1&      0x38) < (u2&      0x38) ) return 0;
                    227:                 if ( (u1&       0x7) < (u2&       0x7) ) return 0;
                    228:             }
                    229:             return 1;
                    230:             break;
                    231:         case 4:
                    232:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    233:                 u1 = d1[i]; u2 = d2[i];
                    234:                 if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
                    235:                 if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
                    236:                 if ( (u1&  0xf00000) < (u2&  0xf00000) ) return 0;
                    237:                 if ( (u1&   0xf0000) < (u2&   0xf0000) ) return 0;
                    238:                 if ( (u1&    0xf000) < (u2&    0xf000) ) return 0;
                    239:                 if ( (u1&     0xf00) < (u2&     0xf00) ) return 0;
                    240:                 if ( (u1&      0xf0) < (u2&      0xf0) ) return 0;
                    241:                 if ( (u1&       0xf) < (u2&       0xf) ) return 0;
                    242:             }
                    243:             return 1;
                    244:             break;
                    245:         case 6:
                    246:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    247:                 u1 = d1[i]; u2 = d2[i];
                    248:                 if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
                    249:                 if ( (u1&  0xfc0000) < (u2&  0xfc0000) ) return 0;
                    250:                 if ( (u1&   0x3f000) < (u2&   0x3f000) ) return 0;
                    251:                 if ( (u1&     0xfc0) < (u2&     0xfc0) ) return 0;
                    252:                 if ( (u1&      0x3f) < (u2&      0x3f) ) return 0;
                    253:             }
                    254:             return 1;
                    255:             break;
                    256:         case 8:
                    257:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    258:                 u1 = d1[i]; u2 = d2[i];
                    259:                 if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
                    260:                 if ( (u1&  0xff0000) < (u2&  0xff0000) ) return 0;
                    261:                 if ( (u1&    0xff00) < (u2&    0xff00) ) return 0;
                    262:                 if ( (u1&      0xff) < (u2&      0xff) ) return 0;
                    263:             }
                    264:             return 1;
                    265:             break;
                    266:         case 16:
                    267:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    268:                 u1 = d1[i]; u2 = d2[i];
                    269:                 if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
                    270:                 if ( (u1&    0xffff) < (u2&    0xffff) ) return 0;
                    271:             }
                    272:             return 1;
                    273:             break;
                    274:         case 32:
                    275:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                    276:                 if ( d1[i] < d2[i] ) return 0;
                    277:             return 1;
                    278:             break;
                    279:         default:
                    280:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    281:                 u1 = d1[i]; u2 = d2[i];
                    282:                 for ( j = 0; j < nd_epw; j++ )
                    283:                     if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    284:             }
                    285:             return 1;
                    286:     }
                    287: #else
                    288:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    289:         u1 = d1[i]; u2 = d2[i];
                    290:         for ( j = 0; j < nd_epw; j++ )
                    291:             if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    292:     }
                    293:     return 1;
                    294: #endif
                    295: }
                    296:
                    297: /*
                    298:  * If the current order is a block order,
                    299:  * then the last block is length 1 and contains
                    300:  * the homo variable. Otherwise, the original
                    301:  * order is either 0 or 2.
                    302:  */
                    303:
                    304: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
                    305: {
                    306:     int w,i,e,n,omask0;
                    307:
                    308:     omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
                    309:     n = nd_nvar-1;
                    310:     ndl_zero(r);
                    311:     for ( i = 0; i < n; i++ ) {
                    312:         e = GET_EXP_OLD(d,i);
                    313:         PUT_EXP(r,i,e);
                    314:     }
                    315:     w = TD(d);
                    316:     PUT_EXP(r,nd_nvar-1,weight-w);
                    317:     if ( nd_module ) MPOS(r) = d[ompos];
                    318:     TD(r) = weight;
                    319:     if ( nd_blockmask ) ndl_weight_mask(r);
                    320: }
                    321:
                    322: void ndl_dehomogenize(UINT *d)
                    323: {
                    324:     UINT mask;
                    325:     UINT h;
                    326:     int i,bits;
                    327:
                    328:     if ( nd_blockmask ) {
                    329:         h = GET_EXP(d,nd_nvar-1);
                    330:         XOR_EXP(d,nd_nvar-1,h);
                    331:         TD(d) -= h;
                    332:         ndl_weight_mask(d);
                    333:     } else {
                    334:         if ( nd_isrlex ) {
                    335:             if ( nd_bpe == 32 ) {
                    336:                 h = d[nd_exporigin];
                    337:                 for ( i = nd_exporigin+1; i < nd_wpd; i++ )
                    338:                     d[i-1] = d[i];
                    339:                 d[i-1] = 0;
                    340:                 TD(d) -= h;
                    341:             } else {
                    342:                 bits = nd_epw*nd_bpe;
                    343:                 mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
                    344:                 h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
                    345:                 for ( i = nd_exporigin; i < nd_wpd; i++ )
                    346:                     d[i] = ((d[i]<<nd_bpe)&mask)
                    347:                         |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
                    348:                 TD(d) -= h;
                    349:             }
                    350:         } else {
                    351:             h = GET_EXP(d,nd_nvar-1);
                    352:             XOR_EXP(d,nd_nvar-1,h);
                    353:             TD(d) -= h;
                    354:         }
                    355:     }
                    356: }
                    357:
                    358: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
                    359: {
                    360:     UINT t1,t2,u,u1,u2;
                    361:     int i,j,l;
                    362:
                    363:     if ( nd_module && (MPOS(d1) != MPOS(d2)) )
                    364:         error("ndl_lcm : inconsistent monomials");
                    365: #if USE_UNROLL
                    366:     switch ( nd_bpe ) {
                    367:         case 3:
                    368:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    369:                 u1 = d1[i]; u2 = d2[i];
                    370:                 t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
                    371:                 t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
                    372:                 t1 = (u1&  0xe00000); t2 = (u2&  0xe00000); u |= t1>t2?t1:t2;
                    373:                 t1 = (u1&  0x1c0000); t2 = (u2&  0x1c0000); u |= t1>t2?t1:t2;
                    374:                 t1 = (u1&   0x38000); t2 = (u2&   0x38000); u |= t1>t2?t1:t2;
                    375:                 t1 = (u1&    0x7000); t2 = (u2&    0x7000); u |= t1>t2?t1:t2;
                    376:                 t1 = (u1&     0xe00); t2 = (u2&     0xe00); u |= t1>t2?t1:t2;
                    377:                 t1 = (u1&     0x1c0); t2 = (u2&     0x1c0); u |= t1>t2?t1:t2;
                    378:                 t1 = (u1&      0x38); t2 = (u2&      0x38); u |= t1>t2?t1:t2;
                    379:                 t1 = (u1&       0x7); t2 = (u2&       0x7); u |= t1>t2?t1:t2;
                    380:                 d[i] = u;
                    381:             }
                    382:             break;
                    383:         case 4:
                    384:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    385:                 u1 = d1[i]; u2 = d2[i];
                    386:                 t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
                    387:                 t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
                    388:                 t1 = (u1&  0xf00000); t2 = (u2&  0xf00000); u |= t1>t2?t1:t2;
                    389:                 t1 = (u1&   0xf0000); t2 = (u2&   0xf0000); u |= t1>t2?t1:t2;
                    390:                 t1 = (u1&    0xf000); t2 = (u2&    0xf000); u |= t1>t2?t1:t2;
                    391:                 t1 = (u1&     0xf00); t2 = (u2&     0xf00); u |= t1>t2?t1:t2;
                    392:                 t1 = (u1&      0xf0); t2 = (u2&      0xf0); u |= t1>t2?t1:t2;
                    393:                 t1 = (u1&       0xf); t2 = (u2&       0xf); u |= t1>t2?t1:t2;
                    394:                 d[i] = u;
                    395:             }
                    396:             break;
                    397:         case 6:
                    398:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    399:                 u1 = d1[i]; u2 = d2[i];
                    400:                 t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
                    401:                 t1 = (u1&  0xfc0000); t2 = (u2&  0xfc0000); u |= t1>t2?t1:t2;
                    402:                 t1 = (u1&   0x3f000); t2 = (u2&   0x3f000); u |= t1>t2?t1:t2;
                    403:                 t1 = (u1&     0xfc0); t2 = (u2&     0xfc0); u |= t1>t2?t1:t2;
                    404:                 t1 = (u1&      0x3f); t2 = (u2&      0x3f); u |= t1>t2?t1:t2;
                    405:                 d[i] = u;
                    406:             }
                    407:             break;
                    408:         case 8:
                    409:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    410:                 u1 = d1[i]; u2 = d2[i];
                    411:                 t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
                    412:                 t1 = (u1&  0xff0000); t2 = (u2&  0xff0000); u |= t1>t2?t1:t2;
                    413:                 t1 = (u1&    0xff00); t2 = (u2&    0xff00); u |= t1>t2?t1:t2;
                    414:                 t1 = (u1&      0xff); t2 = (u2&      0xff); u |= t1>t2?t1:t2;
                    415:                 d[i] = u;
                    416:             }
                    417:             break;
                    418:         case 16:
                    419:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    420:                 u1 = d1[i]; u2 = d2[i];
                    421:                 t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
                    422:                 t1 = (u1&    0xffff); t2 = (u2&    0xffff); u |= t1>t2?t1:t2;
                    423:                 d[i] = u;
                    424:             }
                    425:             break;
                    426:         case 32:
                    427:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    428:                 u1 = d1[i]; u2 = d2[i];
                    429:                 d[i] = u1>u2?u1:u2;
                    430:             }
                    431:             break;
                    432:         default:
                    433:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    434:                 u1 = d1[i]; u2 = d2[i];
                    435:                 for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    436:                     t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    437:                 }
                    438:                 d[i] = u;
                    439:             }
                    440:             break;
                    441:     }
                    442: #else
                    443:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    444:         u1 = d1[i]; u2 = d2[i];
                    445:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    446:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    447:         }
                    448:         d[i] = u;
                    449:     }
                    450: #endif
                    451:     if ( nd_module ) MPOS(d) = MPOS(d1);
                    452:     TD(d) = ndl_weight(d);
                    453:     if ( nd_blockmask ) ndl_weight_mask(d);
                    454: }
                    455:
                    456: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
                    457: {
                    458:     UINT t1,t2,u,u1,u2;
                    459:     int i,j,l;
                    460:
                    461:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    462:         u1 = d1[i]; u2 = d2[i];
                    463:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    464:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    465:         }
                    466:         d[i] = u;
                    467:     }
                    468: }
                    469:
                    470: int ndl_weight(UINT *d)
                    471: {
                    472:     UINT t,u;
                    473:     int i,j;
                    474:
                    475:     if ( current_dl_weight_vector )
                    476:         for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    477:             u = GET_EXP(d,i);
                    478:             t += MUL_WEIGHT(u,i);
                    479:         }
                    480:     else
                    481:         for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    482:             u = d[i];
                    483:             for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    484:                 t += (u&nd_mask0);
                    485:         }
1.20      noro      486:     if ( nd_module && nd_module_rank && MPOS(d) )
                    487:         t += nd_module_weight[MPOS(d)-1];
                    488:     for ( i = nd_exporigin; i < nd_wpd; i++ )
                    489:       if ( d[i] && !t )
                    490:         printf("afo\n");
1.1       noro      491:     return t;
                    492: }
                    493:
                    494: /* for sugarweight */
                    495:
                    496: int ndl_weight2(UINT *d)
                    497: {
                    498:     int t,u;
                    499:     int i,j;
                    500:
                    501:     for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    502:         u = GET_EXP(d,i);
                    503:         t += nd_sugarweight[i]*u;
                    504:     }
1.20      noro      505:     if ( nd_module && nd_module_rank && MPOS(d) )
                    506:         t += nd_module_weight[MPOS(d)-1];
1.1       noro      507:     return t;
                    508: }
                    509:
                    510: void ndl_weight_mask(UINT *d)
                    511: {
                    512:     UINT t,u;
                    513:     UINT *mask;
                    514:     int i,j,k,l;
                    515:
                    516:     l = nd_blockmask->n;
                    517:     for ( k = 0; k < l; k++ ) {
                    518:         mask = nd_blockmask->mask[k];
                    519:         if ( current_dl_weight_vector )
                    520:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    521:                 u = GET_EXP_MASK(d,i,mask);
                    522:                 t += MUL_WEIGHT(u,i);
                    523:             }
                    524:         else
                    525:             for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    526:                 u = d[i]&mask[i];
                    527:                 for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    528:                     t += (u&nd_mask0);
                    529:             }
                    530:         d[k+1] = t;
                    531:     }
                    532: }
                    533:
1.21      noro      534: int ndl_glex_compare(UINT *d1,UINT *d2)
                    535: {
                    536:   if ( TD(d1) > TD(d2) ) return 1;
                    537:   else if ( TD(d1) < TD(d2) ) return -1;
                    538:   else return ndl_lex_compare(d1,d2);
                    539: }
                    540:
1.1       noro      541: int ndl_lex_compare(UINT *d1,UINT *d2)
                    542: {
                    543:     int i;
                    544:
                    545:     d1 += nd_exporigin;
                    546:     d2 += nd_exporigin;
                    547:     for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
                    548:         if ( *d1 > *d2 )
                    549:             return nd_isrlex ? -1 : 1;
                    550:         else if ( *d1 < *d2 )
                    551:             return nd_isrlex ? 1 : -1;
                    552:     return 0;
                    553: }
                    554:
                    555: int ndl_block_compare(UINT *d1,UINT *d2)
                    556: {
                    557:     int i,l,j,ord_o,ord_l;
                    558:     struct order_pair *op;
                    559:     UINT t1,t2,m;
                    560:     UINT *mask;
                    561:
                    562:     l = nd_blockmask->n;
                    563:     op = nd_blockmask->order_pair;
                    564:     for ( j = 0; j < l; j++ ) {
                    565:         mask = nd_blockmask->mask[j];
                    566:         ord_o = op[j].order;
                    567:         if ( ord_o < 2 ) {
                    568:             if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
                    569:             else if ( t1 < t2 ) return -1;
                    570:         }
                    571:         for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    572:             m = mask[i];
                    573:             t1 = d1[i]&m;
                    574:             t2 = d2[i]&m;
                    575:             if ( t1 > t2 )
                    576:                 return !ord_o ? -1 : 1;
                    577:             else if ( t1 < t2 )
                    578:                 return !ord_o ? 1 : -1;
                    579:         }
                    580:     }
                    581:     return 0;
                    582: }
                    583:
                    584: int ndl_matrix_compare(UINT *d1,UINT *d2)
                    585: {
                    586:   int i,j,s,row;
                    587:   int *v;
                    588:   Z **mat;
                    589:   Z *w;
                    590:   Z t1;
                    591:   Z t,t2;
                    592:
1.6       noro      593:   for ( j = 0; j < nd_nvar; j++ )
                    594:     nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.1       noro      595:   if ( nd_top_weight ) {
                    596:     if ( OID(nd_top_weight) == O_VECT ) {
1.6       noro      597:       mat = (Z **)&BDY((VECT)nd_top_weight);
                    598:       row = 1;
1.1       noro      599:     } else {
                    600:       mat = (Z **)BDY((MAT)nd_top_weight);
1.6       noro      601:       row = ((MAT)nd_top_weight)->row;
1.1       noro      602:     }
                    603:     for ( i = 0; i < row; i++ ) {
1.6       noro      604:       w = mat[i];
1.1       noro      605:       for ( j = 0, t = 0; j < nd_nvar; j++ ) {
1.6       noro      606:         STOZ(nd_work_vector[j],t1);
1.1       noro      607:         mulz(w[j],t1,&t2);
                    608:         addz(t,t2,&t1);
                    609:         t = t1;
                    610:       }
1.6       noro      611:       if ( t ) {
                    612:         s = sgnz(t);
1.1       noro      613:         if ( s > 0 ) return 1;
                    614:         else if ( s < 0 ) return -1;
1.6       noro      615:       }
1.1       noro      616:     }
1.6       noro      617:   }
                    618:   for ( i = 0; i < nd_matrix_len; i++ ) {
                    619:     v = nd_matrix[i];
                    620:     for ( j = 0, s = 0; j < nd_nvar; j++ )
                    621:       s += v[j]*nd_work_vector[j];
                    622:     if ( s > 0 ) return 1;
                    623:     else if ( s < 0 ) return -1;
                    624:   }
1.1       noro      625:   if ( !ndl_equal(d1,d2) )
1.6       noro      626:     error("ndl_matrix_compare : invalid matrix");
                    627:   return 0;
1.1       noro      628: }
                    629:
                    630: int ndl_composite_compare(UINT *d1,UINT *d2)
                    631: {
                    632:     int i,j,s,start,end,len,o;
                    633:     int *v;
                    634:     struct sparse_weight *sw;
                    635:
                    636:     for ( j = 0; j < nd_nvar; j++ )
                    637:         nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
                    638:     for ( i = 0; i < nd_worb_len; i++ ) {
                    639:         len = nd_worb[i].length;
                    640:         switch ( nd_worb[i].type ) {
                    641:             case IS_DENSE_WEIGHT:
                    642:                 v = nd_worb[i].body.dense_weight;
                    643:                 for ( j = 0, s = 0; j < len; j++ )
                    644:                     s += v[j]*nd_work_vector[j];
                    645:                 if ( s > 0 ) return 1;
                    646:                 else if ( s < 0 ) return -1;
                    647:                 break;
                    648:             case IS_SPARSE_WEIGHT:
                    649:                 sw = nd_worb[i].body.sparse_weight;
                    650:                 for ( j = 0, s = 0; j < len; j++ )
                    651:                     s += sw[j].value*nd_work_vector[sw[j].pos];
                    652:                 if ( s > 0 ) return 1;
                    653:                 else if ( s < 0 ) return -1;
                    654:                 break;
                    655:             case IS_BLOCK:
                    656:                 o = nd_worb[i].body.block.order;
                    657:                 start = nd_worb[i].body.block.start;
                    658:                 switch ( o ) {
                    659:                     case 0:
                    660:                         end = start+len;
                    661:                         for ( j = start, s = 0; j < end; j++ )
                    662:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    663:                         if ( s > 0 ) return 1;
                    664:                         else if ( s < 0 ) return -1;
                    665:                         for ( j = end-1; j >= start; j-- )
                    666:                             if ( nd_work_vector[j] < 0 ) return 1;
                    667:                             else if ( nd_work_vector[j] > 0 ) return -1;
                    668:                         break;
                    669:                     case 1:
                    670:                         end = start+len;
                    671:                         for ( j = start, s = 0; j < end; j++ )
                    672:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    673:                         if ( s > 0 ) return 1;
                    674:                         else if ( s < 0 ) return -1;
                    675:                         for ( j = start; j < end; j++ )
                    676:                             if ( nd_work_vector[j] > 0 ) return 1;
                    677:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    678:                         break;
                    679:                     case 2:
                    680:                         end = start+len;
                    681:                         for ( j = start; j < end; j++ )
                    682:                             if ( nd_work_vector[j] > 0 ) return 1;
                    683:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    684:                         break;
                    685:                 }
                    686:                 break;
                    687:         }
                    688:     }
                    689:     return 0;
                    690: }
                    691:
                    692: /* TDH -> WW -> TD-> RL */
                    693:
                    694: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
                    695: {
                    696:     int i,m,e1,e2;
                    697:
                    698:     if ( TD(d1) > TD(d2) ) return 1;
                    699:     else if ( TD(d1) < TD(d2) ) return -1;
                    700:     m = nd_nvar>>1;
                    701:     for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
                    702:         e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
                    703:         e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
                    704:     }
                    705:     if ( e1 > e2 ) return 1;
                    706:     else if ( e1 < e2 ) return -1;
                    707:     return ndl_lex_compare(d1,d2);
                    708: }
                    709:
1.21      noro      710: // common function for module glex and grlex comparison
                    711: int ndl_module_glex_compare(UINT *d1,UINT *d2)
1.1       noro      712: {
1.21      noro      713:   int c;
1.1       noro      714:
1.21      noro      715:   switch ( nd_module_ordtype ) {
                    716:     case 0:
                    717:       if ( TD(d1) > TD(d2) ) return 1;
                    718:       else if ( TD(d1) < TD(d2) ) return -1;
                    719:       else if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
                    720:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    721:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    722:       else return 0;
                    723:       break;
1.1       noro      724:
1.21      noro      725:     case 1:
1.19      noro      726:       if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
                    727:          if ( TD(d1) > TD(d2) ) return 1;
                    728:          else if ( TD(d1) < TD(d2) ) return -1;
                    729:          if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
                    730:          if ( MPOS(d1) < MPOS(d2) ) return 1;
                    731:          else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    732:       }
                    733:       if ( MPOS(d1) < MPOS(d2) ) return 1;
                    734:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
1.21      noro      735:       else if ( TD(d1) > TD(d2) ) return 1;
                    736:       else if ( TD(d1) < TD(d2) ) return -1;
                    737:       else return ndl_lex_compare(d1,d2);
                    738:       break;
1.1       noro      739:
1.21      noro      740:     case 2: // weight -> POT
                    741:       if ( TD(d1) > TD(d2) ) return 1;
                    742:       else if ( TD(d1) < TD(d2) ) return -1;
                    743:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    744:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    745:       else return ndl_lex_compare(d1,d2);
                    746:       break;
1.1       noro      747:
1.21      noro      748:     default:
                    749:       error("ndl_module_glex_compare : invalid module_ordtype");
1.28      noro      750:       return 0;
1.21      noro      751:   }
1.1       noro      752: }
                    753:
1.21      noro      754: // common  for module comparison
                    755: int ndl_module_compare(UINT *d1,UINT *d2)
1.1       noro      756: {
1.21      noro      757:   int c;
1.1       noro      758:
1.21      noro      759:   switch ( nd_module_ordtype ) {
                    760:     case 0:
1.23      noro      761:       if ( (c = (*ndl_base_compare_function)(d1,d2)) != 0 ) return c;
1.21      noro      762:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    763:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    764:       else return 0;
                    765:       break;
1.1       noro      766:
1.21      noro      767:     case 1:
                    768:       if ( MPOS(d1) < MPOS(d2) ) return 1;
                    769:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    770:       else return (*ndl_base_compare_function)(d1,d2);
                    771:       break;
1.1       noro      772:
1.21      noro      773:     case 2: // weight -> POT
                    774:       if ( TD(d1) > TD(d2) ) return 1;
                    775:       else if ( TD(d1) < TD(d2) ) return -1;
                    776:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    777:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    778:       else return (*ndl_base_compare_function)(d1,d2);
                    779:       break;
1.1       noro      780:
1.21      noro      781:     default:
                    782:       error("ndl_module_compare : invalid module_ordtype");
1.28      noro      783:       return 0;
1.21      noro      784:   }
1.1       noro      785: }
                    786:
1.21      noro      787: extern DMMstack dmm_stack;
                    788: void _addtodl(int n,DL d1,DL d2);
1.31      noro      789: void _adddl(int n,DL d1,DL d2,DL d3);
1.21      noro      790: int _eqdl(int n,DL d1,DL d2);
                    791:
                    792: int ndl_module_schreyer_compare(UINT *m1,UINT *m2)
                    793: {
                    794:   int pos1,pos2,t,j;
                    795:   DMM *in;
                    796:   DMMstack s;
                    797:   static DL d1=0;
                    798:   static DL d2=0;
                    799:   static int dlen=0;
                    800:
                    801:   pos1 = MPOS(m1); pos2 = MPOS(m2);
                    802:   if ( pos1 == pos2 ) return (*ndl_base_compare_function)(m1,m2);
                    803:   if ( nd_nvar > dlen ) {
                    804:     NEWDL(d1,nd_nvar);
                    805:     NEWDL(d2,nd_nvar);
                    806:     dlen = nd_nvar;
                    807:   }
                    808:   d1->td = TD(m1);
                    809:   for ( j = 0; j < nd_nvar; j++ ) d1->d[j] = GET_EXP(m1,j);
                    810:   d2->td = TD(m2);
                    811:   for ( j = 0; j < nd_nvar; j++ ) d2->d[j] = GET_EXP(m2,j);
                    812:   for ( s = dmm_stack; s; s = NEXT(s) ) {
                    813:     in = s->in;
                    814:     _addtodl(nd_nvar,in[pos1]->dl,d1);
                    815:     _addtodl(nd_nvar,in[pos2]->dl,d2);
                    816:     if ( in[pos1]->pos == in[pos2]->pos && _eqdl(nd_nvar,d1,d2)) {
                    817:       if ( pos1 < pos2 ) return 1;
                    818:       else if ( pos1 > pos2 ) return -1;
                    819:       else return 0;
                    820:     }
                    821:     pos1 = in[pos1]->pos;
                    822:     pos2 = in[pos2]->pos;
                    823:     if ( pos1 == pos2 ) return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    824:   }
                    825:   // comparison by the bottom order
                    826: LAST:
                    827:   switch ( nd_base_ordtype ) {
                    828:     case 0:
                    829:       t = (*dl_base_compare_function)(nd_nvar,d1,d2);
                    830:       if ( t ) return t;
                    831:       else if ( pos1 < pos2 ) return 1;
                    832:       else if ( pos1 > pos2 ) return -1;
                    833:       else return 0;
                    834:       break;
                    835:     case 1:
                    836:       if ( pos1 < pos2 ) return 1;
                    837:       else if ( pos1 > pos2 ) return -1;
                    838:       else return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    839:       break;
                    840:     case 2:
                    841:       if ( d1->td > d2->td  ) return 1;
                    842:       else if ( d1->td < d2->td ) return -1;
                    843:       else if ( pos1 < pos2 ) return 1;
                    844:       else if ( pos1 > pos2 ) return -1;
                    845:       else return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    846:       break;
                    847:     default:
                    848:       error("ndl_schreyer_compare : invalid base ordtype");
1.28      noro      849:       return 0;
1.21      noro      850:   }
1.1       noro      851: }
                    852:
                    853: INLINE int ndl_equal(UINT *d1,UINT *d2)
                    854: {
                    855:     int i;
                    856:
                    857:     switch ( nd_wpd ) {
                    858:         case 2:
                    859:             if ( TD(d2) != TD(d1) ) return 0;
                    860:             if ( d2[1] != d1[1] ) return 0;
                    861:             return 1;
                    862:             break;
                    863:         case 3:
                    864:             if ( TD(d2) != TD(d1) ) return 0;
                    865:             if ( d2[1] != d1[1] ) return 0;
                    866:             if ( d2[2] != d1[2] ) return 0;
                    867:             return 1;
                    868:             break;
                    869:         default:
                    870:             for ( i = 0; i < nd_wpd; i++ )
                    871:                 if ( *d1++ != *d2++ ) return 0;
                    872:             return 1;
                    873:             break;
                    874:     }
                    875: }
                    876:
                    877: INLINE void ndl_copy(UINT *d1,UINT *d2)
                    878: {
                    879:     int i;
                    880:
                    881:     switch ( nd_wpd ) {
                    882:         case 2:
                    883:             TD(d2) = TD(d1);
                    884:             d2[1] = d1[1];
                    885:             break;
                    886:         case 3:
                    887:             TD(d2) = TD(d1);
                    888:             d2[1] = d1[1];
                    889:             d2[2] = d1[2];
                    890:             break;
                    891:         default:
                    892:             for ( i = 0; i < nd_wpd; i++ )
                    893:                 d2[i] = d1[i];
                    894:             break;
                    895:     }
                    896: }
                    897:
                    898: INLINE void ndl_zero(UINT *d)
                    899: {
                    900:     int i;
                    901:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                    902: }
                    903:
                    904: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
                    905: {
                    906:     int i;
                    907:
                    908:     if ( nd_module ) {
                    909:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
                    910:         error("ndl_add : invalid operation");
                    911:     }
                    912: #if 1
                    913:     switch ( nd_wpd ) {
                    914:         case 2:
                    915:             TD(d) = TD(d1)+TD(d2);
                    916:             d[1] = d1[1]+d2[1];
                    917:             break;
                    918:         case 3:
                    919:             TD(d) = TD(d1)+TD(d2);
                    920:             d[1] = d1[1]+d2[1];
                    921:             d[2] = d1[2]+d2[2];
                    922:             break;
                    923:         default:
                    924:             for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    925:             break;
                    926:     }
                    927: #else
                    928:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    929: #endif
                    930: }
                    931:
                    932: /* d1 += d2 */
                    933: INLINE void ndl_addto(UINT *d1,UINT *d2)
                    934: {
                    935:     int i;
                    936:
                    937:     if ( nd_module ) {
                    938:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
                    939:             error("ndl_addto : invalid operation");
                    940:     }
                    941: #if 1
                    942:     switch ( nd_wpd ) {
                    943:         case 2:
                    944:             TD(d1) += TD(d2);
                    945:             d1[1] += d2[1];
                    946:             break;
                    947:         case 3:
                    948:             TD(d1) += TD(d2);
                    949:             d1[1] += d2[1];
                    950:             d1[2] += d2[2];
                    951:             break;
                    952:         default:
                    953:             for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    954:             break;
                    955:     }
                    956: #else
                    957:     for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    958: #endif
                    959: }
                    960:
                    961: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
                    962: {
                    963:     int i;
                    964:
                    965:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
                    966: }
                    967:
                    968: int ndl_disjoint(UINT *d1,UINT *d2)
                    969: {
                    970:     UINT t1,t2,u,u1,u2;
                    971:     int i,j;
                    972:
                    973:     if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
                    974: #if USE_UNROLL
                    975:     switch ( nd_bpe ) {
                    976:         case 3:
                    977:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    978:                 u1 = d1[i]; u2 = d2[i];
                    979:                 t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
                    980:                 t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
                    981:                 t1 = u1&  0xe00000; t2 = u2&  0xe00000; if ( t1&&t2 ) return 0;
                    982:                 t1 = u1&  0x1c0000; t2 = u2&  0x1c0000; if ( t1&&t2 ) return 0;
                    983:                 t1 = u1&   0x38000; t2 = u2&   0x38000; if ( t1&&t2 ) return 0;
                    984:                 t1 = u1&    0x7000; t2 = u2&    0x7000; if ( t1&&t2 ) return 0;
                    985:                 t1 = u1&     0xe00; t2 = u2&     0xe00; if ( t1&&t2 ) return 0;
                    986:                 t1 = u1&     0x1c0; t2 = u2&     0x1c0; if ( t1&&t2 ) return 0;
                    987:                 t1 = u1&      0x38; t2 = u2&      0x38; if ( t1&&t2 ) return 0;
                    988:                 t1 = u1&       0x7; t2 = u2&       0x7; if ( t1&&t2 ) return 0;
                    989:             }
                    990:             return 1;
                    991:             break;
                    992:         case 4:
                    993:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    994:                 u1 = d1[i]; u2 = d2[i];
                    995:                 t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
                    996:                 t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
                    997:                 t1 = u1&  0xf00000; t2 = u2&  0xf00000; if ( t1&&t2 ) return 0;
                    998:                 t1 = u1&   0xf0000; t2 = u2&   0xf0000; if ( t1&&t2 ) return 0;
                    999:                 t1 = u1&    0xf000; t2 = u2&    0xf000; if ( t1&&t2 ) return 0;
                   1000:                 t1 = u1&     0xf00; t2 = u2&     0xf00; if ( t1&&t2 ) return 0;
                   1001:                 t1 = u1&      0xf0; t2 = u2&      0xf0; if ( t1&&t2 ) return 0;
                   1002:                 t1 = u1&       0xf; t2 = u2&       0xf; if ( t1&&t2 ) return 0;
                   1003:             }
                   1004:             return 1;
                   1005:             break;
                   1006:         case 6:
                   1007:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1008:                 u1 = d1[i]; u2 = d2[i];
                   1009:                 t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
                   1010:                 t1 = u1&  0xfc0000; t2 = u2&  0xfc0000; if ( t1&&t2 ) return 0;
                   1011:                 t1 = u1&   0x3f000; t2 = u2&   0x3f000; if ( t1&&t2 ) return 0;
                   1012:                 t1 = u1&     0xfc0; t2 = u2&     0xfc0; if ( t1&&t2 ) return 0;
                   1013:                 t1 = u1&      0x3f; t2 = u2&      0x3f; if ( t1&&t2 ) return 0;
                   1014:             }
                   1015:             return 1;
                   1016:             break;
                   1017:         case 8:
                   1018:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1019:                 u1 = d1[i]; u2 = d2[i];
                   1020:                 t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
                   1021:                 t1 = u1&  0xff0000; t2 = u2&  0xff0000; if ( t1&&t2 ) return 0;
                   1022:                 t1 = u1&    0xff00; t2 = u2&    0xff00; if ( t1&&t2 ) return 0;
                   1023:                 t1 = u1&      0xff; t2 = u2&      0xff; if ( t1&&t2 ) return 0;
                   1024:             }
                   1025:             return 1;
                   1026:             break;
                   1027:         case 16:
                   1028:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1029:                 u1 = d1[i]; u2 = d2[i];
                   1030:                 t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
                   1031:                 t1 = u1&    0xffff; t2 = u2&    0xffff; if ( t1&&t2 ) return 0;
                   1032:             }
                   1033:             return 1;
                   1034:             break;
                   1035:         case 32:
                   1036:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                   1037:                 if ( d1[i] && d2[i] ) return 0;
                   1038:             return 1;
                   1039:             break;
                   1040:         default:
                   1041:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1042:                 u1 = d1[i]; u2 = d2[i];
                   1043:                 for ( j = 0; j < nd_epw; j++ ) {
                   1044:                     if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                   1045:                     u1 >>= nd_bpe; u2 >>= nd_bpe;
                   1046:                 }
                   1047:             }
                   1048:             return 1;
                   1049:             break;
                   1050:     }
                   1051: #else
                   1052:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1053:         u1 = d1[i]; u2 = d2[i];
                   1054:         for ( j = 0; j < nd_epw; j++ ) {
                   1055:             if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                   1056:             u1 >>= nd_bpe; u2 >>= nd_bpe;
                   1057:         }
                   1058:     }
                   1059:     return 1;
                   1060: #endif
                   1061: }
                   1062:
                   1063: int ndl_check_bound(UINT *d1,UINT *d2)
                   1064: {
                   1065:     UINT u2;
                   1066:     int i,j,ind,k;
                   1067:
                   1068:     ind = 0;
                   1069: #if USE_UNROLL
                   1070:     switch ( nd_bpe ) {
                   1071:         case 3:
                   1072:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1073:                 u2 = d2[i];
                   1074:                 if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
                   1075:                 if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
                   1076:                 if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
                   1077:                 if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
                   1078:                 if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
                   1079:                 if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
                   1080:                 if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
                   1081:                 if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
                   1082:                 if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
                   1083:                 if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
                   1084:             }
                   1085:             return 0;
                   1086:             break;
                   1087:         case 4:
                   1088:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1089:                 u2 = d2[i];
                   1090:                 if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
                   1091:                 if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
                   1092:                 if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
                   1093:                 if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
                   1094:                 if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
                   1095:                 if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
                   1096:                 if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
                   1097:                 if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
                   1098:             }
                   1099:             return 0;
                   1100:             break;
                   1101:         case 6:
                   1102:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1103:                 u2 = d2[i];
                   1104:                 if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
                   1105:                 if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
                   1106:                 if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
                   1107:                 if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
                   1108:                 if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
                   1109:             }
                   1110:             return 0;
                   1111:             break;
                   1112:         case 8:
                   1113:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1114:                 u2 = d2[i];
                   1115:                 if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
                   1116:                 if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
                   1117:                 if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
                   1118:                 if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
                   1119:             }
                   1120:             return 0;
                   1121:             break;
                   1122:         case 16:
                   1123:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1124:                 u2 = d2[i];
                   1125:                 if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
                   1126:                 if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
                   1127:             }
                   1128:             return 0;
                   1129:             break;
                   1130:         case 32:
                   1131:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                   1132:                 if ( d1[i]+d2[i]<d1[i] ) return 1;
                   1133:             return 0;
                   1134:             break;
                   1135:         default:
                   1136:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1137:                 u2 = d2[i];
                   1138:                 k = (nd_epw-1)*nd_bpe;
                   1139:                 for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1140:                     if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1141:             }
                   1142:             return 0;
                   1143:             break;
                   1144:     }
                   1145: #else
                   1146:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1147:         u2 = d2[i];
                   1148:         k = (nd_epw-1)*nd_bpe;
                   1149:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1150:             if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1151:     }
                   1152:     return 0;
                   1153: #endif
                   1154: }
                   1155:
                   1156: int ndl_check_bound2(int index,UINT *d2)
                   1157: {
                   1158:     return ndl_check_bound(nd_bound[index],d2);
                   1159: }
                   1160:
                   1161: INLINE int ndl_hash_value(UINT *d)
                   1162: {
                   1163:     int i;
1.11      noro     1164:     UINT r;
1.1       noro     1165:
                   1166:     r = 0;
                   1167:     for ( i = 0; i < nd_wpd; i++ )
1.12      noro     1168:         r = (r*1511+d[i]);
1.11      noro     1169:     r %= REDTAB_LEN;
1.1       noro     1170:     return r;
                   1171: }
                   1172:
                   1173: INLINE int ndl_find_reducer(UINT *dg)
                   1174: {
                   1175:     RHist r;
                   1176:     int d,k,i;
                   1177:
                   1178:     d = ndl_hash_value(dg);
                   1179:     for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   1180:         if ( ndl_equal(dg,DL(r)) ) {
                   1181:             if ( k > 0 ) nd_notfirst++;
                   1182:             nd_found++;
                   1183:             return r->index;
                   1184:         }
                   1185:     }
                   1186:     if ( Reverse )
                   1187:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   1188:             r = nd_psh[i];
                   1189:             if ( ndl_reducible(dg,DL(r)) ) {
                   1190:                 nd_create++;
                   1191:                 nd_append_red(dg,i);
                   1192:                 return i;
                   1193:             }
                   1194:         }
                   1195:     else
                   1196:         for ( i = 0; i < nd_psn; i++ ) {
                   1197:             r = nd_psh[i];
                   1198:             if ( ndl_reducible(dg,DL(r)) ) {
                   1199:                 nd_create++;
                   1200:                 nd_append_red(dg,i);
                   1201:                 return i;
                   1202:             }
                   1203:         }
                   1204:     return -1;
                   1205: }
                   1206:
1.24      noro     1207: // ret=0,...,nd_psn-1 => reducer found
                   1208: // ret=nd_psn => reducer not found
                   1209: // ret=-1 => singular top reducible
                   1210:
                   1211: int comp_sig(SIG s1,SIG s2);
                   1212: void _ndltodl(UINT *ndl,DL dl);
                   1213:
                   1214: void print_sig(SIG s)
                   1215: {
                   1216:   int i;
                   1217:
                   1218:   fprintf(asir_out,"<<");
                   1219:   for ( i = 0; i < nd_nvar; i++ ) {
                   1220:     fprintf(asir_out,"%d",s->dl->d[i]);
                   1221:     if ( i != nd_nvar-1 ) fprintf(asir_out,",");
                   1222:   }
                   1223:   fprintf(asir_out,">>*e%d",s->pos);
                   1224: }
                   1225:
                   1226: INLINE int ndl_find_reducer_s(UINT *dg,SIG sig)
                   1227: {
                   1228:   RHist r;
1.27      noro     1229:   int i,singular,ret,d,k;
1.26      noro     1230:   static int wpd,nvar;
1.24      noro     1231:   static SIG quo;
                   1232:   static UINT *tmp;
                   1233:
1.26      noro     1234:   if ( !quo || nvar != nd_nvar ) NEWSIG(quo);
                   1235:   if ( wpd != nd_wpd ) {
1.24      noro     1236:     wpd = nd_wpd;
                   1237:     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
                   1238:   }
1.27      noro     1239:   d = ndl_hash_value(dg);
                   1240:   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   1241:     if ( ndl_equal(dg,DL(r)) ) {
                   1242:       return r->index;
                   1243:     }
                   1244:   }
1.24      noro     1245:   singular = 0;
                   1246:   for ( i = 0; i < nd_psn; i++ ) {
                   1247:     r = nd_psh[i];
                   1248:     if ( ndl_reducible(dg,DL(r)) ) {
1.27      noro     1249:       ndl_sub(dg,DL(r),tmp);
1.24      noro     1250:       _ndltodl(tmp,DL(quo));
                   1251:       _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
                   1252:       quo->pos = nd_psh[i]->sig->pos;
                   1253:       ret = comp_sig(sig,quo);
                   1254:       if ( ret > 0 ) { singular = 0; break; }
1.29      noro     1255:       if ( ret == 0 ) { fprintf(asir_out,"s"); fflush(asir_out); singular = 1; }
1.24      noro     1256:     }
                   1257:   }
                   1258:   if ( singular ) return -1;
1.27      noro     1259:   else if ( i < nd_psn )
                   1260:     nd_append_red(dg,i);
                   1261:   return i;
1.24      noro     1262: }
                   1263:
1.1       noro     1264: ND nd_merge(ND p1,ND p2)
                   1265: {
                   1266:     int n,c;
                   1267:     int t,can,td1,td2;
                   1268:     ND r;
                   1269:     NM m1,m2,mr0,mr,s;
                   1270:
                   1271:     if ( !p1 ) return p2;
                   1272:     else if ( !p2 ) return p1;
                   1273:     else {
                   1274:         can = 0;
                   1275:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1276:             c = DL_COMPARE(DL(m1),DL(m2));
                   1277:             switch ( c ) {
                   1278:                 case 0:
                   1279:                     s = m1; m1 = NEXT(m1);
                   1280:                     can++; NEXTNM2(mr0,mr,s);
                   1281:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1282:                     break;
                   1283:                 case 1:
                   1284:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1285:                     break;
                   1286:                 case -1:
                   1287:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1288:                     break;
                   1289:             }
                   1290:         }
                   1291:         if ( !mr0 )
                   1292:             if ( m1 ) mr0 = m1;
                   1293:             else if ( m2 ) mr0 = m2;
                   1294:             else return 0;
                   1295:         else if ( m1 ) NEXT(mr) = m1;
                   1296:         else if ( m2 ) NEXT(mr) = m2;
                   1297:         else NEXT(mr) = 0;
                   1298:         BDY(p1) = mr0;
                   1299:         SG(p1) = MAX(SG(p1),SG(p2));
                   1300:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1301:         FREEND(p2);
                   1302:         return p1;
                   1303:     }
                   1304: }
                   1305:
                   1306: ND nd_add(int mod,ND p1,ND p2)
                   1307: {
                   1308:     int n,c;
                   1309:     int t,can,td1,td2;
                   1310:     ND r;
                   1311:     NM m1,m2,mr0,mr,s;
                   1312:
1.11      noro     1313:     Nnd_add++;
1.1       noro     1314:     if ( !p1 ) return p2;
                   1315:     else if ( !p2 ) return p1;
                   1316:     else if ( mod == -1 ) return nd_add_sf(p1,p2);
                   1317:     else if ( mod == -2 ) return nd_add_lf(p1,p2);
                   1318:     else if ( !mod ) return nd_add_q(p1,p2);
                   1319:     else {
                   1320:         can = 0;
                   1321:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1322:             c = DL_COMPARE(DL(m1),DL(m2));
                   1323:             switch ( c ) {
                   1324:                 case 0:
                   1325:                     t = ((CM(m1))+(CM(m2))) - mod;
                   1326:                     if ( t < 0 ) t += mod;
                   1327:                     s = m1; m1 = NEXT(m1);
                   1328:                     if ( t ) {
                   1329:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1330:                     } else {
                   1331:                         can += 2; FREENM(s);
                   1332:                     }
                   1333:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1334:                     break;
                   1335:                 case 1:
                   1336:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1337:                     break;
                   1338:                 case -1:
                   1339:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1340:                     break;
                   1341:             }
                   1342:         }
                   1343:         if ( !mr0 )
                   1344:             if ( m1 ) mr0 = m1;
                   1345:             else if ( m2 ) mr0 = m2;
                   1346:             else return 0;
                   1347:         else if ( m1 ) NEXT(mr) = m1;
                   1348:         else if ( m2 ) NEXT(mr) = m2;
                   1349:         else NEXT(mr) = 0;
                   1350:         BDY(p1) = mr0;
                   1351:         SG(p1) = MAX(SG(p1),SG(p2));
                   1352:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1353:         FREEND(p2);
                   1354:         return p1;
                   1355:     }
                   1356: }
                   1357:
                   1358: /* XXX on opteron, the inlined manipulation of destructive additon of
                   1359:  * two NM seems to make gcc optimizer get confused, so the part is
                   1360:  * done in a function.
                   1361:  */
                   1362:
                   1363: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
                   1364: {
                   1365:     NM s;
                   1366:     P t;
                   1367:     int can;
                   1368:
                   1369:     addp(nd_vc,CP(*m1),CP(*m2),&t);
                   1370:     s = *m1; *m1 = NEXT(*m1);
                   1371:     if ( t ) {
                   1372:         can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
                   1373:     } else {
                   1374:         can = 2; FREENM(s);
                   1375:     }
                   1376:     s = *m2; *m2 = NEXT(*m2); FREENM(s);
                   1377:     return can;
                   1378: }
                   1379:
                   1380: ND nd_add_q(ND p1,ND p2)
                   1381: {
                   1382:     int n,c,can;
                   1383:     ND r;
                   1384:     NM m1,m2,mr0,mr,s;
                   1385:     P t;
                   1386:
                   1387:     if ( !p1 ) return p2;
                   1388:     else if ( !p2 ) return p1;
                   1389:     else {
                   1390:         can = 0;
                   1391:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1392:             c = DL_COMPARE(DL(m1),DL(m2));
                   1393:             switch ( c ) {
                   1394:                 case 0:
                   1395: #if defined(__x86_64__)
                   1396:                     can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
                   1397: #else
                   1398:                     addp(nd_vc,CP(m1),CP(m2),&t);
                   1399:                     s = m1; m1 = NEXT(m1);
                   1400:                     if ( t ) {
                   1401:                         can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
                   1402:                     } else {
                   1403:                         can += 2; FREENM(s);
                   1404:                     }
                   1405:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1406: #endif
                   1407:                     break;
                   1408:                 case 1:
                   1409:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1410:                     break;
                   1411:                 case -1:
                   1412:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1413:                     break;
                   1414:             }
                   1415:         }
                   1416:         if ( !mr0 )
                   1417:             if ( m1 ) mr0 = m1;
                   1418:             else if ( m2 ) mr0 = m2;
                   1419:             else return 0;
                   1420:         else if ( m1 ) NEXT(mr) = m1;
                   1421:         else if ( m2 ) NEXT(mr) = m2;
                   1422:         else NEXT(mr) = 0;
                   1423:         BDY(p1) = mr0;
                   1424:         SG(p1) = MAX(SG(p1),SG(p2));
                   1425:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1426:         FREEND(p2);
                   1427:         return p1;
                   1428:     }
                   1429: }
                   1430:
                   1431: ND nd_add_sf(ND p1,ND p2)
                   1432: {
                   1433:     int n,c,can;
                   1434:     ND r;
                   1435:     NM m1,m2,mr0,mr,s;
                   1436:     int t;
                   1437:
                   1438:     if ( !p1 ) return p2;
                   1439:     else if ( !p2 ) return p1;
                   1440:     else {
                   1441:         can = 0;
                   1442:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1443:             c = DL_COMPARE(DL(m1),DL(m2));
                   1444:             switch ( c ) {
                   1445:                 case 0:
                   1446:                     t = _addsf(CM(m1),CM(m2));
                   1447:                     s = m1; m1 = NEXT(m1);
                   1448:                     if ( t ) {
                   1449:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1450:                     } else {
                   1451:                         can += 2; FREENM(s);
                   1452:                     }
                   1453:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1454:                     break;
                   1455:                 case 1:
                   1456:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1457:                     break;
                   1458:                 case -1:
                   1459:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1460:                     break;
                   1461:             }
                   1462:         }
                   1463:         if ( !mr0 )
                   1464:             if ( m1 ) mr0 = m1;
                   1465:             else if ( m2 ) mr0 = m2;
                   1466:             else return 0;
                   1467:         else if ( m1 ) NEXT(mr) = m1;
                   1468:         else if ( m2 ) NEXT(mr) = m2;
                   1469:         else NEXT(mr) = 0;
                   1470:         BDY(p1) = mr0;
                   1471:         SG(p1) = MAX(SG(p1),SG(p2));
                   1472:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1473:         FREEND(p2);
                   1474:         return p1;
                   1475:     }
                   1476: }
                   1477:
                   1478:
                   1479: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
                   1480: {
                   1481:     int c,c1,c2;
                   1482:     Z cg,cred,gcd,tq;
                   1483:     P cgp,credp,gcdp;
                   1484:     Obj tr,tr1;
                   1485:
                   1486:     if ( mod == -1 ) {
                   1487:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1488:         *divp = (Obj)ONE;
                   1489:     } else if ( mod == -2 ) {
                   1490:         Z inv,t;
                   1491:         divlf(ONE,HCZ(p),&inv);
                   1492:         chsgnlf(HCZ(g),&t);
                   1493:         mullf(inv,t,&CZ(mul));
                   1494:         *divp = (Obj)ONE;
                   1495:     } else if ( mod ) {
                   1496:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1497:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1498:         *divp = (Obj)ONE;
                   1499:     } else if ( nd_vc ) {
                   1500:         ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
                   1501:         divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
                   1502:         chsgnp(cgp,&CP(mul));
                   1503:         nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
                   1504:         if ( dn ) {
                   1505:             mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
                   1506:             reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
                   1507:         }
                   1508:         *divp = (Obj)credp;
                   1509:     } else {
1.6       noro     1510:         igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1511:         chsgnz(cg,&CZ(mul));
1.1       noro     1512:         nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
                   1513:         if ( dn ) {
                   1514:             mulz(dn->z,cred,&tq); dn->z = tq;
                   1515:         }
                   1516:         *divp = (Obj)cred;
                   1517:     }
                   1518:     return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
                   1519: }
                   1520:
                   1521: /* ret=1 : success, ret=0 : overflow */
1.6       noro     1522: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1       noro     1523: {
                   1524:     NM m,mrd,tail;
                   1525:     NM mul;
                   1526:     int n,sugar,psugar,sugar0,stat,index;
                   1527:     int c,c1,c2,dummy;
                   1528:     RHist h;
                   1529:     NDV p,red;
                   1530:     Q cg,cred,gcd,tq,qq;
                   1531:     Z iq;
                   1532:     DP dmul;
                   1533:     NODE node;
                   1534:     LIST hist;
                   1535:     double hmag;
                   1536:     P tp,tp1;
                   1537:     Obj tr,tr1,div;
                   1538:     union oNDC hg;
                   1539:     P cont;
                   1540:
                   1541:     if ( !g ) {
                   1542:         *rp = d;
                   1543:         return 1;
                   1544:     }
                   1545:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1546:
                   1547:     sugar0 = sugar = SG(g);
                   1548:     n = NV(g);
                   1549:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1550:     if ( d )
                   1551:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1552:     for ( ; g; ) {
                   1553:         index = ndl_find_reducer(HDL(g));
                   1554:         if ( index >= 0 ) {
                   1555:             h = nd_psh[index];
                   1556:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1557:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1558:                 nd_free(g); nd_free(d);
                   1559:                 return 0;
                   1560:             }
                   1561:             p = nd_demand ? ndv_load(index) : ps[index];
                   1562:             /* d+g -> div*(d+g)+mul*p */
1.6       noro     1563:             g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1       noro     1564:             if ( nd_gentrace ) {
                   1565:                 /* Trace=[div,index,mul,ONE] */
1.6       noro     1566:                 STOZ(index,iq);
1.1       noro     1567:                 nmtodp(mod,mul,&dmul);
                   1568:                 node = mknode(4,div,iq,dmul,ONE);
                   1569:             }
                   1570:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
                   1571:             if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
                   1572:                 hg = HCU(g);
                   1573:                 nd_removecont2(d,g);
1.6       noro     1574:                 if ( nd_gentrace ) {
1.1       noro     1575:                     /* overwrite cont : Trace=[div,index,mul,cont] */
1.6       noro     1576:                     /* exact division */
1.1       noro     1577:                     cont = ndc_div(mod,hg,HCU(g));
                   1578:                     if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
                   1579:                 }
                   1580:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1581:             }
                   1582:             MKLIST(hist,node);
                   1583:             MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
                   1584:         } else if ( !full ) {
                   1585:             *rp = g;
                   1586:             return 1;
                   1587:         } else {
                   1588:             m = BDY(g);
                   1589:             if ( NEXT(m) ) {
                   1590:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1591:             } else {
                   1592:                 FREEND(g); g = 0;
                   1593:             }
                   1594:             if ( d ) {
                   1595:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1596:             } else {
                   1597:                 MKND(n,m,1,d); tail = BDY(d);
                   1598:             }
                   1599:         }
                   1600:     }
                   1601:     if ( d ) SG(d) = sugar;
                   1602:     *rp = d;
                   1603:     return 1;
                   1604: }
                   1605:
1.24      noro     1606: // ret=1 => success
                   1607: // ret=0 => overflow
                   1608: // ret=-1 => singular top reducible
                   1609:
                   1610: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
                   1611: {
                   1612:     NM m,mrd,tail;
                   1613:     NM mul;
                   1614:     int n,sugar,psugar,sugar0,stat,index;
                   1615:     int c,c1,c2,dummy;
                   1616:     RHist h;
                   1617:     NDV p,red;
                   1618:     Q cg,cred,gcd,tq,qq;
                   1619:     Z iq;
                   1620:     DP dmul;
                   1621:     NODE node;
                   1622:     LIST hist;
                   1623:     double hmag;
                   1624:     P tp,tp1;
                   1625:     Obj tr,tr1,div;
                   1626:     union oNDC hg;
                   1627:     P cont;
                   1628:     SIG sig;
                   1629:
                   1630:     if ( !g ) {
                   1631:         *rp = d;
                   1632:         return 1;
                   1633:     }
                   1634:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1635:
                   1636:     sugar0 = sugar = SG(g);
                   1637:     n = NV(g);
                   1638:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1639:     if ( d )
                   1640:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1641:     sig = g->sig;
                   1642:     for ( ; g; ) {
                   1643:         index = ndl_find_reducer_s(HDL(g),sig);
                   1644:         if ( index >= 0 && index < nd_psn ) {
                   1645:             // reducer found
                   1646:             h = nd_psh[index];
                   1647:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1648:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1649:                 nd_free(g); nd_free(d);
                   1650:                 return 0;
                   1651:             }
                   1652:             p = ps[index];
                   1653:             /* d+g -> div*(d+g)+mul*p */
                   1654:             g = nd_reduce2(mod,d,g,p,mul,0,&div);
                   1655:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
                   1656:             if ( !mod && g && ((double)(p_mag(HCP(g))) > hmag) ) {
                   1657:                 hg = HCU(g);
                   1658:                 nd_removecont2(d,g);
                   1659:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1660:             }
                   1661:         } else if ( index == -1 ) {
                   1662:           // singular top reducible
                   1663:           return -1;
                   1664:         } else if ( !full ) {
                   1665:             *rp = g;
                   1666:             g->sig = sig;
                   1667:             return 1;
                   1668:         } else {
                   1669:             m = BDY(g);
                   1670:             if ( NEXT(m) ) {
                   1671:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1672:             } else {
                   1673:                 FREEND(g); g = 0;
                   1674:             }
                   1675:             if ( d ) {
                   1676:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1677:             } else {
                   1678:                 MKND(n,m,1,d); tail = BDY(d);
                   1679:             }
                   1680:         }
                   1681:     }
                   1682:     if ( d ) {
                   1683:       SG(d) = sugar;
                   1684:       d->sig = sig;
                   1685:     }
                   1686:     *rp = d;
                   1687:     return 1;
                   1688: }
                   1689:
1.1       noro     1690: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
                   1691: {
                   1692:     int hindex,index;
                   1693:     NDV p;
                   1694:     ND u,d,red;
                   1695:     NODE l;
                   1696:     NM mul,m,mrd,tail;
                   1697:     int sugar,psugar,n,h_reducible;
                   1698:     PGeoBucket bucket;
                   1699:     int c,c1,c2;
                   1700:     Z cg,cred,gcd,zzz;
                   1701:     RHist h;
                   1702:     double hmag,gmag;
                   1703:     int count = 0;
                   1704:     int hcount = 0;
                   1705:
                   1706:     if ( !g ) {
                   1707:         *rp = 0;
                   1708:         return 1;
                   1709:     }
                   1710:     sugar = SG(g);
                   1711:     n = NV(g);
1.6       noro     1712:     if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1       noro     1713:     bucket = create_pbucket();
                   1714:     add_pbucket(mod,bucket,g);
                   1715:     d = 0;
                   1716:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1717:     while ( 1 ) {
                   1718:         if ( mod > 0 || mod == -1 )
                   1719:           hindex = head_pbucket(mod,bucket);
                   1720:         else if ( mod == -2 )
                   1721:           hindex = head_pbucket_lf(bucket);
                   1722:         else
                   1723:           hindex = head_pbucket_q(bucket);
                   1724:         if ( hindex < 0 ) {
                   1725:             if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1726:             if ( d ) SG(d) = sugar;
                   1727:             *rp = d;
                   1728:             return 1;
                   1729:         }
                   1730:         g = bucket->body[hindex];
                   1731:         index = ndl_find_reducer(HDL(g));
                   1732:         if ( index >= 0 ) {
                   1733:             count++;
                   1734:             if ( !d ) hcount++;
                   1735:             h = nd_psh[index];
                   1736:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1737:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1738:                 nd_free(d);
                   1739:                 free_pbucket(bucket);
                   1740:                 *rp = 0;
                   1741:                 return 0;
                   1742:             }
                   1743:             p = ps[index];
                   1744:             if ( mod == -1 )
                   1745:                 CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1746:             else if ( mod == -2 ) {
                   1747:                 Z inv,t;
                   1748:                 divlf(ONE,HCZ(p),&inv);
                   1749:                 chsgnlf(HCZ(g),&t);
                   1750:                 mullf(inv,t,&CZ(mul));
                   1751:             } else if ( mod ) {
                   1752:                 c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1753:                 DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1754:             } else {
1.6       noro     1755:                 igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1756:                 chsgnz(cg,&CZ(mul));
1.1       noro     1757:                 nd_mul_c_q(d,(P)cred);
                   1758:                 mulq_pbucket(bucket,cred);
                   1759:                 g = bucket->body[hindex];
1.6       noro     1760:                 gmag = (double)p_mag((P)HCZ(g));
1.1       noro     1761:             }
                   1762:             red = ndv_mul_nm(mod,mul,p);
                   1763:             bucket->body[hindex] = nd_remove_head(g);
                   1764:             red = nd_remove_head(red);
                   1765:             add_pbucket(mod,bucket,red);
                   1766:             psugar = SG(p)+TD(DL(mul));
                   1767:             sugar = MAX(sugar,psugar);
                   1768:             if ( !mod && hmag && (gmag > hmag) ) {
                   1769:                 g = normalize_pbucket(mod,bucket);
                   1770:                 if ( !g ) {
                   1771:                     if ( d ) SG(d) = sugar;
                   1772:                     *rp = d;
                   1773:                     return 1;
                   1774:                 }
                   1775:                 nd_removecont2(d,g);
1.6       noro     1776:                 hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1       noro     1777:                 add_pbucket(mod,bucket,g);
                   1778:             }
                   1779:         } else if ( !full ) {
                   1780:             g = normalize_pbucket(mod,bucket);
                   1781:             if ( g ) SG(g) = sugar;
                   1782:             *rp = g;
                   1783:             return 1;
                   1784:         } else {
                   1785:             m = BDY(g);
                   1786:             if ( NEXT(m) ) {
                   1787:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1788:             } else {
                   1789:                 FREEND(g); g = 0;
                   1790:             }
                   1791:             bucket->body[hindex] = g;
                   1792:             NEXT(m) = 0;
                   1793:             if ( d ) {
                   1794:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1795:             } else {
                   1796:                 MKND(n,m,1,d); tail = BDY(d);
                   1797:             }
                   1798:         }
                   1799:     }
                   1800: }
                   1801:
1.25      noro     1802: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *rp)
                   1803: {
                   1804:   int hindex,index;
                   1805:   NDV p;
                   1806:   ND u,d,red;
                   1807:   NODE l;
                   1808:   NM mul,m,mrd,tail;
                   1809:   int sugar,psugar,n,h_reducible;
                   1810:   PGeoBucket bucket;
                   1811:   int c,c1,c2;
                   1812:   Z cg,cred,gcd,zzz;
                   1813:   RHist h;
                   1814:   double hmag,gmag;
                   1815:   int count = 0;
                   1816:   int hcount = 0;
                   1817:   SIG sig;
                   1818:
                   1819:   if ( !g ) {
                   1820:     *rp = 0;
                   1821:     return 1;
                   1822:   }
                   1823:   sugar = SG(g);
                   1824:   n = NV(g);
                   1825:   if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
                   1826:   bucket = create_pbucket();
                   1827:   add_pbucket(mod,bucket,g);
                   1828:   d = 0;
                   1829:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1830:   sig = g->sig;
                   1831:   while ( 1 ) {
                   1832:     if ( mod > 0 || mod == -1 )
                   1833:       hindex = head_pbucket(mod,bucket);
                   1834:     else if ( mod == -2 )
                   1835:       hindex = head_pbucket_lf(bucket);
                   1836:     else
                   1837:       hindex = head_pbucket_q(bucket);
                   1838:     if ( hindex < 0 ) {
                   1839:       if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1840:       if ( d ) {
                   1841:         SG(d) = sugar;
                   1842:         d->sig = sig;
                   1843:       }
                   1844:       *rp = d;
                   1845:       return 1;
                   1846:     }
                   1847:     g = bucket->body[hindex];
                   1848:     index = ndl_find_reducer_s(HDL(g),sig);
                   1849:     if ( index >= 0 && index < nd_psn ) {
                   1850:       count++;
                   1851:       if ( !d ) hcount++;
                   1852:       h = nd_psh[index];
                   1853:       ndl_sub(HDL(g),DL(h),DL(mul));
                   1854:       if ( ndl_check_bound2(index,DL(mul)) ) {
                   1855:         nd_free(d);
                   1856:         free_pbucket(bucket);
                   1857:         *rp = 0;
                   1858:         return 0;
                   1859:       }
                   1860:       p = ps[index];
                   1861:       if ( mod == -1 )
                   1862:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1863:       else if ( mod == -2 ) {
                   1864:         Z inv,t;
                   1865:         divlf(ONE,HCZ(p),&inv);
                   1866:         chsgnlf(HCZ(g),&t);
                   1867:         mullf(inv,t,&CZ(mul));
                   1868:       } else if ( mod ) {
                   1869:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1870:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1871:       } else {
                   1872:         igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1873:         chsgnz(cg,&CZ(mul));
                   1874:         nd_mul_c_q(d,(P)cred);
                   1875:         mulq_pbucket(bucket,cred);
                   1876:         g = bucket->body[hindex];
                   1877:         gmag = (double)p_mag((P)HCZ(g));
                   1878:       }
                   1879:       red = ndv_mul_nm(mod,mul,p);
                   1880:       bucket->body[hindex] = nd_remove_head(g);
                   1881:       red = nd_remove_head(red);
                   1882:       add_pbucket(mod,bucket,red);
                   1883:       psugar = SG(p)+TD(DL(mul));
                   1884:       sugar = MAX(sugar,psugar);
                   1885:       if ( !mod && hmag && (gmag > hmag) ) {
                   1886:          g = normalize_pbucket(mod,bucket);
                   1887:          if ( !g ) {
                   1888:            if ( d ) {
                   1889:              SG(d) = sugar;
                   1890:              d->sig = sig;
                   1891:            }
                   1892:            *rp = d;
                   1893:            return 1;
                   1894:          }
                   1895:          nd_removecont2(d,g);
                   1896:          hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
                   1897:          add_pbucket(mod,bucket,g);
                   1898:       }
                   1899:     } else if ( index == -1 ) {
                   1900:       // singular top reducible
                   1901:       return -1;
                   1902:     } else if ( !full ) {
                   1903:       g = normalize_pbucket(mod,bucket);
                   1904:       if ( g ) {
                   1905:         SG(g) = sugar;
                   1906:         g->sig = sig;
                   1907:       }
                   1908:       *rp = g;
                   1909:       return 1;
                   1910:     } else {
                   1911:       m = BDY(g);
                   1912:       if ( NEXT(m) ) {
                   1913:         BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1914:       } else {
                   1915:         FREEND(g); g = 0;
                   1916:       }
                   1917:       bucket->body[hindex] = g;
                   1918:       NEXT(m) = 0;
                   1919:       if ( d ) {
                   1920:         NEXT(tail)=m; tail=m; LEN(d)++;
                   1921:       } else {
                   1922:         MKND(n,m,1,d); tail = BDY(d);
                   1923:       }
                   1924:     }
                   1925:   }
                   1926: }
                   1927:
1.1       noro     1928: /* input : list of NDV, cand : list of NDV */
                   1929:
                   1930: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
                   1931: {
                   1932:     int n,i,stat;
                   1933:     ND nf,d;
                   1934:     NDV r;
                   1935:     NODE t,s;
                   1936:     union oNDC dn;
                   1937:     Z q;
                   1938:     LIST list;
                   1939:
1.24      noro     1940:     ndv_setup(m,0,cand,nd_gentrace?1:0,1,0);
1.1       noro     1941:     n = length(cand);
                   1942:
                   1943:   if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
                   1944:     /* membercheck : list is a subset of Id(cand) ? */
                   1945:     for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
                   1946: again:
                   1947:     nd_tracelist = 0;
                   1948:         if ( nd_bpe > obpe )
                   1949:             r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
                   1950:         else
                   1951:             r = (NDV)BDY(t);
                   1952: #if 0
                   1953:         // moved to nd_f4_lf_trace()
                   1954:         if ( m == -2 ) ndv_mod(m,r);
                   1955: #endif
                   1956:         d = ndvtond(m,r);
1.6       noro     1957:         stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1       noro     1958:         if ( !stat ) {
                   1959:             nd_reconstruct(0,0);
                   1960:             goto again;
                   1961:         } else if ( nf ) return 0;
                   1962:     if ( nd_gentrace ) {
                   1963:       nd_tracelist = reverse_node(nd_tracelist);
                   1964:       MKLIST(list,nd_tracelist);
1.6       noro     1965:       STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1       noro     1966:       MKNODE(s,list,nd_alltracelist);
                   1967:       nd_alltracelist = s; nd_tracelist = 0;
                   1968:     }
                   1969:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   1970:     }
                   1971:     if ( DP_Print ) { printf("\n"); }
                   1972:     return 1;
                   1973: }
                   1974:
                   1975: ND nd_remove_head(ND p)
                   1976: {
                   1977:     NM m;
                   1978:
                   1979:     m = BDY(p);
                   1980:     if ( !NEXT(m) ) {
                   1981:         FREEND(p); p = 0;
                   1982:     } else {
                   1983:         BDY(p) = NEXT(m); LEN(p)--;
                   1984:     }
                   1985:     FREENM(m);
                   1986:     return p;
                   1987: }
                   1988:
                   1989: ND nd_separate_head(ND p,ND *head)
                   1990: {
                   1991:     NM m,m0;
                   1992:     ND r;
                   1993:
                   1994:     m = BDY(p);
                   1995:     if ( !NEXT(m) ) {
                   1996:         *head = p; p = 0;
                   1997:     } else {
                   1998:         m0 = m;
                   1999:         BDY(p) = NEXT(m); LEN(p)--;
                   2000:         NEXT(m0) = 0;
                   2001:         MKND(NV(p),m0,1,r);
                   2002:         *head = r;
                   2003:     }
                   2004:     return p;
                   2005: }
                   2006:
                   2007: PGeoBucket create_pbucket()
                   2008: {
                   2009:     PGeoBucket g;
                   2010:
                   2011:     g = CALLOC(1,sizeof(struct oPGeoBucket));
                   2012:     g->m = -1;
                   2013:     return g;
                   2014: }
                   2015:
                   2016: void free_pbucket(PGeoBucket b) {
                   2017:     int i;
                   2018:
                   2019:     for ( i = 0; i <= b->m; i++ )
                   2020:         if ( b->body[i] ) {
                   2021:             nd_free(b->body[i]);
                   2022:             b->body[i] = 0;
                   2023:         }
                   2024:     GCFREE(b);
                   2025: }
                   2026:
                   2027: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   2028: {
                   2029:     int l,i,k,m;
                   2030:
                   2031:     if ( !d )
                   2032:         return;
                   2033:     l = LEN(d);
                   2034:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2035:     /* 2^(k-1) < l <= 2^k (=m) */
                   2036:     d = nd_merge(g->body[k],d);
                   2037:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   2038:         g->body[k] = 0;
                   2039:         d = nd_merge(g->body[k+1],d);
                   2040:     }
                   2041:     g->body[k] = d;
                   2042:     g->m = MAX(g->m,k);
                   2043: }
                   2044:
                   2045: void add_pbucket(int mod,PGeoBucket g,ND d)
                   2046: {
                   2047:     int l,i,k,m;
                   2048:
                   2049:     if ( !d )
                   2050:         return;
                   2051:     l = LEN(d);
                   2052:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2053:     /* 2^(k-1) < l <= 2^k (=m) */
                   2054:     d = nd_add(mod,g->body[k],d);
                   2055:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   2056:         g->body[k] = 0;
                   2057:         d = nd_add(mod,g->body[k+1],d);
                   2058:     }
                   2059:     g->body[k] = d;
                   2060:     g->m = MAX(g->m,k);
                   2061: }
                   2062:
                   2063: void mulq_pbucket(PGeoBucket g,Z c)
                   2064: {
                   2065:     int k;
                   2066:
                   2067:     for ( k = 0; k <= g->m; k++ )
                   2068:         nd_mul_c_q(g->body[k],(P)c);
                   2069: }
                   2070:
                   2071: NM remove_head_pbucket_symbolic(PGeoBucket g)
                   2072: {
                   2073:     int j,i,k,c;
                   2074:     NM head;
                   2075:
                   2076:     k = g->m;
                   2077:     j = -1;
                   2078:     for ( i = 0; i <= k; i++ ) {
                   2079:         if ( !g->body[i] ) continue;
                   2080:         if ( j < 0 ) j = i;
                   2081:         else {
                   2082:             c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
                   2083:             if ( c > 0 )
                   2084:                 j = i;
                   2085:             else if ( c == 0 )
                   2086:                 g->body[i] = nd_remove_head(g->body[i]);
                   2087:         }
                   2088:     }
                   2089:     if ( j < 0 ) return 0;
                   2090:     else {
                   2091:         head = BDY(g->body[j]);
                   2092:         if ( !NEXT(head) ) {
                   2093:             FREEND(g->body[j]);
                   2094:             g->body[j] = 0;
                   2095:         } else {
                   2096:             BDY(g->body[j]) = NEXT(head);
                   2097:             LEN(g->body[j])--;
                   2098:         }
                   2099:         return head;
                   2100:     }
                   2101: }
                   2102:
                   2103: int head_pbucket(int mod,PGeoBucket g)
                   2104: {
                   2105:     int j,i,c,k,nv,sum;
                   2106:     UINT *di,*dj;
                   2107:     ND gi,gj;
                   2108:
                   2109:     k = g->m;
                   2110:     while ( 1 ) {
                   2111:         j = -1;
                   2112:         for ( i = 0; i <= k; i++ ) {
                   2113:             if ( !(gi = g->body[i]) )
                   2114:                 continue;
                   2115:             if ( j < 0 ) {
                   2116:                 j = i;
                   2117:                 gj = g->body[j];
                   2118:                 dj = HDL(gj);
                   2119:                 sum = HCM(gj);
                   2120:             } else {
                   2121:                 c = DL_COMPARE(HDL(gi),dj);
                   2122:                 if ( c > 0 ) {
                   2123:                     if ( sum ) HCM(gj) = sum;
                   2124:                     else g->body[j] = nd_remove_head(gj);
                   2125:                     j = i;
                   2126:                     gj = g->body[j];
                   2127:                     dj = HDL(gj);
                   2128:                     sum = HCM(gj);
                   2129:                 } else if ( c == 0 ) {
                   2130:                     if ( mod == -1 )
                   2131:                         sum = _addsf(sum,HCM(gi));
                   2132:                     else {
                   2133:                         sum = sum+HCM(gi)-mod;
                   2134:                         if ( sum < 0 ) sum += mod;
                   2135:                     }
                   2136:                     g->body[i] = nd_remove_head(gi);
                   2137:                 }
                   2138:             }
                   2139:         }
                   2140:         if ( j < 0 ) return -1;
                   2141:         else if ( sum ) {
                   2142:             HCM(gj) = sum;
                   2143:             return j;
                   2144:         } else
                   2145:             g->body[j] = nd_remove_head(gj);
                   2146:     }
                   2147: }
                   2148:
                   2149: int head_pbucket_q(PGeoBucket g)
                   2150: {
                   2151:     int j,i,c,k,nv;
                   2152:     Z sum,t;
                   2153:     ND gi,gj;
                   2154:
                   2155:     k = g->m;
                   2156:     while ( 1 ) {
                   2157:         j = -1;
                   2158:         for ( i = 0; i <= k; i++ ) {
                   2159:             if ( !(gi = g->body[i]) ) continue;
                   2160:             if ( j < 0 ) {
                   2161:                 j = i;
                   2162:                 gj = g->body[j];
1.6       noro     2163:                 sum = HCZ(gj);
1.1       noro     2164:             } else {
                   2165:                 nv = NV(gi);
                   2166:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   2167:                 if ( c > 0 ) {
1.6       noro     2168:                     if ( sum ) HCZ(gj) = sum;
1.1       noro     2169:                     else g->body[j] = nd_remove_head(gj);
                   2170:                     j = i;
                   2171:                     gj = g->body[j];
1.6       noro     2172:                     sum = HCZ(gj);
1.1       noro     2173:                 } else if ( c == 0 ) {
1.6       noro     2174:                     addz(sum,HCZ(gi),&t);
1.1       noro     2175:                     sum = t;
                   2176:                     g->body[i] = nd_remove_head(gi);
                   2177:                 }
                   2178:             }
                   2179:         }
                   2180:         if ( j < 0 ) return -1;
                   2181:         else if ( sum ) {
1.6       noro     2182:             HCZ(gj) = sum;
1.1       noro     2183:             return j;
                   2184:         } else
                   2185:             g->body[j] = nd_remove_head(gj);
                   2186:     }
                   2187: }
                   2188:
                   2189: int head_pbucket_lf(PGeoBucket g)
                   2190: {
                   2191:     int j,i,c,k,nv;
                   2192:     Z sum,t;
                   2193:     ND gi,gj;
                   2194:
                   2195:     k = g->m;
                   2196:     while ( 1 ) {
                   2197:         j = -1;
                   2198:         for ( i = 0; i <= k; i++ ) {
                   2199:             if ( !(gi = g->body[i]) ) continue;
                   2200:             if ( j < 0 ) {
                   2201:                 j = i;
                   2202:                 gj = g->body[j];
                   2203:                 sum = HCZ(gj);
                   2204:             } else {
                   2205:                 nv = NV(gi);
                   2206:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   2207:                 if ( c > 0 ) {
                   2208:                     if ( sum ) HCZ(gj) = sum;
                   2209:                     else g->body[j] = nd_remove_head(gj);
                   2210:                     j = i;
                   2211:                     gj = g->body[j];
                   2212:                     sum = HCZ(gj);
                   2213:                 } else if ( c == 0 ) {
                   2214:                     addlf(sum,HCZ(gi),&t);
                   2215:                     sum = t;
                   2216:                     g->body[i] = nd_remove_head(gi);
                   2217:                 }
                   2218:             }
                   2219:         }
                   2220:         if ( j < 0 ) return -1;
                   2221:         else if ( sum ) {
                   2222:             HCZ(gj) = sum;
                   2223:             return j;
                   2224:         } else
                   2225:             g->body[j] = nd_remove_head(gj);
                   2226:     }
                   2227: }
                   2228:
                   2229: ND normalize_pbucket(int mod,PGeoBucket g)
                   2230: {
                   2231:     int i;
                   2232:     ND r,t;
                   2233:
                   2234:     r = 0;
                   2235:     for ( i = 0; i <= g->m; i++ ) {
                   2236:         r = nd_add(mod,r,g->body[i]);
                   2237:         g->body[i] = 0;
                   2238:     }
                   2239:     g->m = -1;
                   2240:     return r;
                   2241: }
                   2242:
                   2243: #if 0
                   2244: void register_hcf(NDV p)
                   2245: {
                   2246:     DCP dc,t;
                   2247:     P hc,h;
                   2248:     int c;
                   2249:     NODE l,l1,prev;
                   2250:
                   2251:     hc = p->body->c.p;
                   2252:     if ( !nd_vc || NUM(hc) ) return;
                   2253:     fctrp(nd_vc,hc,&dc);
                   2254:     for ( t = dc; t; t = NEXT(t) ) {
                   2255:         h = t->c;
                   2256:         if ( NUM(h) ) continue;
                   2257:         for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   2258:             c = compp(nd_vc,h,(P)BDY(l));
                   2259:             if ( c >= 0 ) break;
                   2260:         }
                   2261:         if ( !l || c > 0  ) {
                   2262:             MKNODE(l1,h,l);
                   2263:             if ( !prev )
                   2264:                 nd_hcf = l1;
                   2265:             else
                   2266:                 NEXT(prev) = l1;
                   2267:         }
                   2268:     }
                   2269: }
                   2270: #else
                   2271: void register_hcf(NDV p)
                   2272: {
                   2273:     DCP dc,t;
                   2274:     P hc,h,q;
                   2275:     Q dmy;
                   2276:     int c;
                   2277:     NODE l,l1,prev;
                   2278:
                   2279:     hc = p->body->c.p;
                   2280:     if ( NUM(hc) ) return;
                   2281:     ptozp(hc,1,&dmy,&h);
                   2282: #if 1
                   2283:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   2284:         while ( 1 ) {
                   2285:             if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
                   2286:             else break;
                   2287:         }
                   2288:     }
                   2289:     if ( NUM(h) ) return;
                   2290: #endif
                   2291:     for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   2292:         c = compp(nd_vc,h,(P)BDY(l));
                   2293:         if ( c >= 0 ) break;
                   2294:     }
                   2295:     if ( !l || c > 0  ) {
                   2296:         MKNODE(l1,h,l);
                   2297:         if ( !prev )
                   2298:             nd_hcf = l1;
                   2299:         else
                   2300:             NEXT(prev) = l1;
                   2301:     }
                   2302: }
                   2303: #endif
                   2304:
                   2305: int do_diagonalize(int sugar,int m)
                   2306: {
1.6       noro     2307:   int i,nh,stat;
                   2308:   NODE r,g,t;
                   2309:   ND h,nf,s,head;
                   2310:   NDV nfv;
                   2311:   Q q;
                   2312:   P nm,nmp,dn,mnp,dnp,cont,cont1;
                   2313:   union oNDC hc;
                   2314:   NODE node;
                   2315:   LIST l;
                   2316:   Z iq;
1.1       noro     2317:
1.6       noro     2318:   for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   2319:     if ( nd_gentrace ) {
                   2320:       /* Trace = [1,index,1,1] */
                   2321:       STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2322:       MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2323:     }
                   2324:     if ( nd_demand )
                   2325:       nfv = ndv_load(i);
                   2326:     else
                   2327:       nfv = nd_ps[i];
                   2328:     s = ndvtond(m,nfv);
                   2329:     s = nd_separate_head(s,&head);
                   2330:     stat = nd_nf(m,head,s,nd_ps,1,&nf);
                   2331:     if ( !stat ) return 0;
                   2332:     ndv_free(nfv);
                   2333:     hc = HCU(nf); nd_removecont(m,nf);
                   2334:     /* exact division */
                   2335:     cont = ndc_div(m,hc,HCU(nf));
1.1       noro     2336:     if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6       noro     2337:     nfv = ndtondv(m,nf);
                   2338:     nd_free(nf);
                   2339:     nd_bound[i] = ndv_compute_bound(nfv);
                   2340:     if ( !m ) register_hcf(nfv);
                   2341:     if ( nd_demand ) {
                   2342:       ndv_save(nfv,i);
                   2343:       ndv_free(nfv);
                   2344:     } else
                   2345:       nd_ps[i] = nfv;
                   2346:   }
                   2347:   return 1;
1.1       noro     2348: }
                   2349:
                   2350: LIST compute_splist()
                   2351: {
                   2352:   NODE g,tn0,tn,node;
                   2353:   LIST l0;
                   2354:   ND_pairs d,t;
                   2355:   int i;
                   2356:   Z i1,i2;
                   2357:
                   2358:     g = 0; d = 0;
                   2359:     for ( i = 0; i < nd_psn; i++ ) {
                   2360:         d = update_pairs(d,g,i,0);
                   2361:         g = update_base(g,i);
                   2362:     }
                   2363:   for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
                   2364:     NEXTNODE(tn0,tn);
1.6       noro     2365:         STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1       noro     2366:         node = mknode(2,i1,i2); MKLIST(l0,node);
                   2367:     BDY(tn) = l0;
                   2368:   }
                   2369:   if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
                   2370:   return l0;
                   2371: }
                   2372:
                   2373: /* return value = 0 => input is not a GB */
                   2374:
                   2375: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
                   2376: {
1.6       noro     2377:   int i,nh,sugar,stat;
                   2378:   NODE r,g,t;
                   2379:   ND_pairs d;
                   2380:   ND_pairs l;
                   2381:   ND h,nf,s,head,nf1;
                   2382:   NDV nfv;
                   2383:   Z q;
                   2384:   union oNDC dn,hc;
                   2385:   int diag_count = 0;
                   2386:   P cont;
                   2387:   LIST list;
1.27      noro     2388: struct oEGT eg1,eg2,eg_update;
1.6       noro     2389:
1.27      noro     2390: init_eg(&eg_update);
1.11      noro     2391:   Nnd_add = 0;
1.6       noro     2392:   g = 0; d = 0;
                   2393:   for ( i = 0; i < nd_psn; i++ ) {
                   2394:     d = update_pairs(d,g,i,gensyz);
                   2395:     g = update_base(g,i);
                   2396:   }
                   2397:   sugar = 0;
                   2398:   while ( d ) {
1.1       noro     2399: again:
1.6       noro     2400:     l = nd_minp(d,&d);
                   2401:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   2402:     if ( SG(l) != sugar ) {
                   2403:       if ( ishomo ) {
                   2404:         diag_count = 0;
                   2405:         stat = do_diagonalize(sugar,m);
1.1       noro     2406:         if ( !stat ) {
1.6       noro     2407:           NEXT(l) = d; d = l;
                   2408:           d = nd_reconstruct(0,d);
                   2409:           goto again;
1.1       noro     2410:         }
1.6       noro     2411:       }
                   2412:       sugar = SG(l);
                   2413:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2414:     }
                   2415:     stat = nd_sp(m,0,l,&h);
                   2416:     if ( !stat ) {
                   2417:       NEXT(l) = d; d = l;
                   2418:       d = nd_reconstruct(0,d);
                   2419:       goto again;
                   2420:     }
1.1       noro     2421: #if USE_GEOBUCKET
1.6       noro     2422:     stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!Top,&nf)
                   2423:       :nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1       noro     2424: #else
1.6       noro     2425:     stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1       noro     2426: #endif
1.6       noro     2427:     if ( !stat ) {
                   2428:       NEXT(l) = d; d = l;
                   2429:       d = nd_reconstruct(0,d);
                   2430:       goto again;
                   2431:     } else if ( nf ) {
                   2432:       if ( checkonly || gensyz ) return 0;
1.1       noro     2433:       if ( nd_newelim ) {
                   2434:         if ( nd_module ) {
                   2435:           if ( MPOS(HDL(nf)) > 1 ) return 0;
                   2436:         } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
                   2437:       }
1.6       noro     2438:       if ( DP_Print ) { printf("+"); fflush(stdout); }
                   2439:       hc = HCU(nf);
                   2440:       nd_removecont(m,nf);
                   2441:       if ( !m && nd_nalg ) {
                   2442:         nd_monic(0,&nf);
                   2443:         nd_removecont(m,nf);
                   2444:       }
                   2445:       if ( nd_gentrace ) {
                   2446:         /* exact division */
1.1       noro     2447:         cont = ndc_div(m,hc,HCU(nf));
                   2448:         if ( m || !UNIQ(cont) ) {
1.6       noro     2449:           t = mknode(4,NULLP,NULLP,NULLP,cont);
                   2450:           MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1       noro     2451:           nd_tracelist = t;
                   2452:         }
                   2453:       }
1.6       noro     2454:       nfv = ndtondv(m,nf); nd_free(nf);
1.24      noro     2455:       nh = ndv_newps(m,nfv,0);
1.6       noro     2456:       if ( !m && (ishomo && ++diag_count == diag_period) ) {
                   2457:         diag_count = 0;
                   2458:         stat = do_diagonalize(sugar,m);
                   2459:         if ( !stat ) {
                   2460:           NEXT(l) = d; d = l;
                   2461:           d = nd_reconstruct(1,d);
                   2462:           goto again;
1.1       noro     2463:         }
1.6       noro     2464:       }
1.27      noro     2465: get_eg(&eg1);
1.6       noro     2466:       d = update_pairs(d,g,nh,0);
1.27      noro     2467: get_eg(&eg2); add_eg(&eg_update,&eg1,&eg2);
1.6       noro     2468:       g = update_base(g,nh);
                   2469:       FREENDP(l);
                   2470:    } else {
                   2471:      if ( nd_gentrace && gensyz ) {
                   2472:        nd_tracelist = reverse_node(nd_tracelist);
                   2473:        MKLIST(list,nd_tracelist);
                   2474:        STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
                   2475:        MKNODE(t,list,nd_alltracelist);
                   2476:        nd_alltracelist = t; nd_tracelist = 0;
                   2477:      }
                   2478:      if ( DP_Print ) { printf("."); fflush(stdout); }
                   2479:        FREENDP(l);
                   2480:    }
                   2481:  }
                   2482:  conv_ilist(nd_demand,0,g,indp);
1.11      noro     2483:     if ( !checkonly && DP_Print ) { printf("nd_gb done. Number of nd_add=%d\n",Nnd_add); fflush(stdout); }
1.27      noro     2484: print_eg("update",&eg_update);
1.1       noro     2485:     return g;
                   2486: }
                   2487:
1.30      noro     2488: ND_pairs update_pairs_s(ND_pairs d,int t,NODE *syz);
                   2489: ND_pairs nd_newpairs_s(int t ,NODE *syz);
1.24      noro     2490:
                   2491: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *nf);
                   2492: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
                   2493:
                   2494: void _copydl(int n,DL d1,DL d2);
                   2495: void _subfromdl(int n,DL d1,DL d2);
                   2496: extern int (*cmpdl)(int n,DL d1,DL d2);
                   2497:
                   2498: NODE insert_sig(NODE l,SIG s)
                   2499: {
                   2500:   int pos;
                   2501:   DL sig;
                   2502:   struct oNODE root;
                   2503:   NODE p,prev,r;
                   2504:   SIG t;
                   2505:
                   2506:   pos = s->pos; sig = DL(s);
                   2507:   root.next = l; prev = &root;
                   2508:   for ( p = l; p; p = p->next ) {
                   2509:     t = (SIG)p->body;
                   2510:     if ( t->pos == pos ) {
                   2511:       if ( _dl_redble(DL(t),sig,nd_nvar) )
                   2512:         return root.next;
                   2513:       else if ( _dl_redble(sig,DL(t),nd_nvar) )
                   2514:         // remove p
                   2515:         prev->next = p->next;
                   2516:     } else
                   2517:       prev = p;
                   2518:   }
1.27      noro     2519:   NEWNODE(r); r->body = (pointer)s; r->next = 0;
                   2520:   for ( p = &root; p->next; p = p->next );
                   2521:   p->next = r;
                   2522: //  r->next = root.next;
                   2523: //  return r;
                   2524:   return root.next;
1.24      noro     2525: }
                   2526:
                   2527: ND_pairs remove_spair_s(ND_pairs d,SIG sig)
                   2528: {
                   2529:   struct oND_pairs root;
                   2530:   ND_pairs prev,p;
                   2531:   SIG spsig;
                   2532:
                   2533:   root.next = d;
                   2534:   prev = &root; p = d;
                   2535:   while ( p ) {
                   2536:     spsig = p->sig;
1.26      noro     2537:     if ( sig->pos == spsig->pos && _dl_redble(DL(sig),DL(spsig),nd_nvar) ) {
1.24      noro     2538:       // remove p
                   2539:       prev->next = p->next;
1.26      noro     2540:       Nsyz++;
                   2541:     } else
1.24      noro     2542:       prev = p;
                   2543:     p = p->next;
                   2544:   }
                   2545:   return (ND_pairs)root.next;
                   2546: }
                   2547:
1.29      noro     2548: int _dl_redble_ext(DL,DL,DL,int);
                   2549:
1.28      noro     2550: int small_lcm(ND_pairs l)
                   2551: {
                   2552:   SIG sig;
                   2553:   int i;
1.29      noro     2554:   NODE t;
1.28      noro     2555:   static DL lcm,mul,quo;
                   2556:   static int nvar;
                   2557:
                   2558:   if ( nvar < nd_nvar ) {
                   2559:     nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(quo,nvar); NEWDL(mul,nvar);
                   2560:   }
                   2561:   sig = l->sig;
                   2562:   _ndltodl(l->lcm,lcm);
1.29      noro     2563: #if 0
1.28      noro     2564:   for ( i = 0; i < nd_psn; i++ ) {
                   2565:     if ( sig->pos == nd_psh[i]->sig->pos &&
1.29      noro     2566:       _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
1.28      noro     2567:       _ndltodl(DL(nd_psh[i]),mul);
                   2568:       _addtodl(nd_nvar,quo,mul);
                   2569:       if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
                   2570:         break;
                   2571:     }
                   2572:   }
                   2573:   if ( i < nd_psn ) return 1;
                   2574:   else return 0;
1.29      noro     2575: #else
                   2576:   for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
                   2577:     i = (long)BDY(t);
                   2578:     if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
                   2579:       _ndltodl(DL(nd_psh[i]),mul);
                   2580:       _addtodl(nd_nvar,quo,mul);
                   2581:       if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
                   2582:         break;
                   2583:     }
                   2584:   }
                   2585:   if ( t ) return 1;
                   2586:   else return 0;
                   2587: #endif
1.28      noro     2588: }
                   2589:
                   2590: ND_pairs remove_large_lcm(ND_pairs d)
                   2591: {
                   2592:   struct oND_pairs root;
                   2593:   ND_pairs prev,p;
                   2594:
                   2595:   root.next = d;
                   2596:   prev = &root; p = d;
                   2597:   while ( p ) {
                   2598:     if ( small_lcm(p) ) {
                   2599:       // remove p
                   2600:       prev->next = p->next;
                   2601:     } else
                   2602:       prev = p;
                   2603:     p = p->next;
                   2604:   }
                   2605:   return (ND_pairs)root.next;
                   2606: }
                   2607:
1.26      noro     2608: struct oEGT eg_create,eg_newpairs,eg_merge;
                   2609:
1.30      noro     2610: NODE conv_ilist_s(int demand,int trace,int **indp);
                   2611:
1.24      noro     2612: NODE nd_sba_buch(int m,int ishomo,int **indp)
                   2613: {
1.29      noro     2614:   int i,j,nh,sugar,stat;
1.30      noro     2615:   NODE r,t,g;
1.24      noro     2616:   ND_pairs d;
                   2617:   ND_pairs l;
                   2618:   ND h,nf,s,head,nf1;
                   2619:   NDV nfv;
                   2620:   Z q;
                   2621:   union oNDC dn,hc;
                   2622:   P cont;
                   2623:   LIST list;
                   2624:   SIG sig;
1.29      noro     2625:   NODE *syzlist;
1.26      noro     2626:   int Nredundant;
1.28      noro     2627:   DL lcm,quo,mul;
1.31      noro     2628:   struct oEGT eg1,eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.24      noro     2629:
1.27      noro     2630: init_eg(&eg_remove);
1.29      noro     2631:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.26      noro     2632:   Nsyz = 0;
1.24      noro     2633:   Nnd_add = 0;
1.26      noro     2634:   Nredundant = 0;
1.30      noro     2635:   d = 0;
1.24      noro     2636:   for ( i = 0; i < nd_psn; i++ ) {
1.30      noro     2637:     d = update_pairs_s(d,i,syzlist);
1.24      noro     2638:   }
1.29      noro     2639:   for ( i = 0; i < nd_psn; i++ )
                   2640:     for ( j = i+1; j < nd_psn; j++ ) {
                   2641:       NEWSIG(sig); sig->pos = j;
                   2642:       _copydl(nd_nvar,nd_sba_hm[i],sig->dl);
                   2643:       syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
                   2644:     }
1.24      noro     2645:   sugar = 0;
1.26      noro     2646:   NEWDL(lcm,nd_nvar); NEWDL(quo,nd_nvar); NEWDL(mul,nd_nvar);
                   2647: init_eg(&eg_create);
                   2648: init_eg(&eg_merge);
1.29      noro     2649: init_eg(&eg_large);
1.31      noro     2650: init_eg(&eg_nf);
                   2651: init_eg(&eg_nfzero);
1.24      noro     2652:   while ( d ) {
                   2653: again:
1.26      noro     2654:     if ( DP_Print ) {
                   2655:       int len;
                   2656:       ND_pairs td;
1.28      noro     2657:       for ( td = d, len=0; td; td = td->next, len++)
                   2658:         ;
1.26      noro     2659:        if ( !(len%100) ) fprintf(asir_out,"(%d)",len);
                   2660:       }
1.24      noro     2661:     l = d; d = d->next;
1.28      noro     2662:     if ( small_lcm(l) ) {
1.24      noro     2663:       if ( DP_Print ) fprintf(asir_out,"M");
1.26      noro     2664:       Nredundant++;
1.24      noro     2665:       continue;
                   2666:     }
                   2667:     if ( SG(l) != sugar ) {
                   2668:       sugar = SG(l);
                   2669:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2670:     }
1.28      noro     2671:     sig = l->sig;
1.24      noro     2672:     stat = nd_sp(m,0,l,&h);
                   2673:     if ( !stat ) {
                   2674:       NEXT(l) = d; d = l;
                   2675:       d = nd_reconstruct(0,d);
                   2676:       goto again;
                   2677:     }
1.31      noro     2678: get_eg(&eg1);
1.25      noro     2679: #if USE_GEOBUCKET
1.24      noro     2680:     stat = m?nd_nf_pbucket_s(m,h,nd_ps,!Top,&nf):nd_nf_s(m,0,h,nd_ps,!Top,&nf);
                   2681: #else
                   2682:     stat = nd_nf_s(m,0,h,nd_ps,!Top,&nf);
                   2683: #endif
1.31      noro     2684: get_eg(&eg2);
1.24      noro     2685:     if ( !stat ) {
                   2686:       NEXT(l) = d; d = l;
                   2687:       d = nd_reconstruct(0,d);
                   2688:       goto again;
                   2689:     } else if ( stat == -1 ) {
                   2690:       if ( DP_Print ) { printf("S"); fflush(stdout); }
1.27      noro     2691:       FREENDP(l);
1.24      noro     2692:     } else if ( nf ) {
                   2693:       if ( DP_Print ) { printf("+"); fflush(stdout); }
1.31      noro     2694:       add_eg(&eg_nf,&eg1,&eg2);
1.24      noro     2695:       hc = HCU(nf);
                   2696:       nd_removecont(m,nf);
                   2697:       nfv = ndtondv(m,nf); nd_free(nf);
                   2698:       nh = ndv_newps(m,nfv,0);
1.26      noro     2699:
1.30      noro     2700:       d = update_pairs_s(d,nh,syzlist);
1.29      noro     2701:       nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.24      noro     2702:       FREENDP(l);
                   2703:    } else {
1.31      noro     2704:       add_eg(&eg_nfzero,&eg1,&eg2);
1.24      noro     2705:      // syzygy
1.27      noro     2706: get_eg(&eg1);
1.24      noro     2707:      d = remove_spair_s(d,sig);
1.27      noro     2708: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29      noro     2709:      syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.24      noro     2710:      if ( DP_Print ) { printf("."); fflush(stdout); }
                   2711:      FREENDP(l);
                   2712:    }
                   2713:  }
1.30      noro     2714:  g = conv_ilist_s(nd_demand,0,indp);
1.26      noro     2715:  if ( DP_Print ) {
                   2716:    printf("\nnd_sba done. nd_add=%d,Nsyz=%d,Nredundant=%d\n",Nnd_add,Nsyz,Nredundant);
                   2717:    fflush(stdout);
                   2718:    print_eg("create",&eg_create);
                   2719:    print_eg("merge",&eg_merge);
1.27      noro     2720:    print_eg("remove",&eg_remove);
1.31      noro     2721:    print_eg("nf",&eg_nf);
                   2722:    print_eg("nfzero",&eg_nfzero);
1.27      noro     2723:    printf("\n");
1.26      noro     2724:  }
1.24      noro     2725:  return g;
                   2726: }
                   2727:
1.1       noro     2728: /* splist = [[i1,i2],...] */
                   2729:
                   2730: int check_splist(int m,NODE splist)
                   2731: {
                   2732:   NODE t,p;
                   2733:   ND_pairs d,r,l;
                   2734:   int stat;
                   2735:   ND h,nf;
                   2736:
                   2737:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   2738:     p = BDY((LIST)BDY(t));
1.6       noro     2739:     NEXTND_pairs(d,r);
                   2740:     r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
                   2741:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     2742:     SG(r) = TD(LCM(r)); /* XXX */
                   2743:   }
                   2744:   if ( d ) NEXT(r) = 0;
                   2745:
1.6       noro     2746:   while ( d ) {
1.1       noro     2747: again:
1.6       noro     2748:     l = nd_minp(d,&d);
                   2749:     stat = nd_sp(m,0,l,&h);
                   2750:     if ( !stat ) {
                   2751:       NEXT(l) = d; d = l;
                   2752:       d = nd_reconstruct(0,d);
                   2753:       goto again;
                   2754:     }
                   2755:     stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
                   2756:     if ( !stat ) {
                   2757:       NEXT(l) = d; d = l;
                   2758:       d = nd_reconstruct(0,d);
                   2759:       goto again;
                   2760:     } else if ( nf ) return 0;
1.1       noro     2761:     if ( DP_Print) { printf("."); fflush(stdout); }
1.6       noro     2762:   }
1.1       noro     2763:   if ( DP_Print) { printf("done.\n"); fflush(stdout); }
                   2764:   return 1;
                   2765: }
                   2766:
                   2767: int check_splist_f4(int m,NODE splist)
                   2768: {
                   2769:   UINT *s0vect;
1.6       noro     2770:   PGeoBucket bucket;
1.1       noro     2771:   NODE p,rp0,t;
                   2772:   ND_pairs d,r,l,ll;
                   2773:   int col,stat;
                   2774:
                   2775:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   2776:     p = BDY((LIST)BDY(t));
1.6       noro     2777:     NEXTND_pairs(d,r);
                   2778:     r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
                   2779:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     2780:     SG(r) = TD(LCM(r)); /* XXX */
                   2781:   }
                   2782:   if ( d ) NEXT(r) = 0;
                   2783:
1.6       noro     2784:   while ( d ) {
                   2785:     l = nd_minsugarp(d,&d);
                   2786:     bucket = create_pbucket();
                   2787:     stat = nd_sp_f4(m,0,l,bucket);
                   2788:     if ( !stat ) {
                   2789:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   2790:       NEXT(ll) = d; d = l;
                   2791:       d = nd_reconstruct(0,d);
                   2792:       continue;
                   2793:     }
                   2794:     if ( bucket->m < 0 ) continue;
                   2795:     col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   2796:     if ( !col ) {
                   2797:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   2798:       NEXT(ll) = d; d = l;
                   2799:       d = nd_reconstruct(0,d);
                   2800:       continue;
1.1       noro     2801:     }
1.6       noro     2802:     if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
                   2803:   }
                   2804:   return 1;
1.1       noro     2805: }
                   2806:
                   2807: int do_diagonalize_trace(int sugar,int m)
                   2808: {
1.6       noro     2809:   int i,nh,stat;
                   2810:   NODE r,g,t;
                   2811:   ND h,nf,nfq,s,head;
                   2812:   NDV nfv,nfqv;
                   2813:   Q q,den,num;
                   2814:   union oNDC hc;
                   2815:   NODE node;
                   2816:   LIST l;
                   2817:   Z iq;
                   2818:   P cont,cont1;
1.1       noro     2819:
1.6       noro     2820:   for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   2821:     if ( nd_gentrace ) {
                   2822:         /* Trace = [1,index,1,1] */
                   2823:         STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2824:         MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2825:     }
                   2826:     /* for nd_ps */
                   2827:     s = ndvtond(m,nd_ps[i]);
                   2828:     s = nd_separate_head(s,&head);
                   2829:     stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
                   2830:     if ( !stat ) return 0;
                   2831:     nf = nd_add(m,head,nf);
                   2832:     ndv_free(nd_ps[i]);
                   2833:     nd_ps[i] = ndtondv(m,nf);
                   2834:     nd_free(nf);
                   2835:
                   2836:     /* for nd_ps_trace */
                   2837:     if ( nd_demand )
                   2838:         nfv = ndv_load(i);
                   2839:     else
                   2840:         nfv = nd_ps_trace[i];
                   2841:     s = ndvtond(0,nfv);
                   2842:     s = nd_separate_head(s,&head);
                   2843:     stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
                   2844:     if ( !stat ) return 0;
                   2845:     ndv_free(nfv);
                   2846:     hc = HCU(nf); nd_removecont(0,nf);
                   2847:     /* exact division */
1.1       noro     2848:     cont = ndc_div(0,hc,HCU(nf));
1.6       noro     2849:     if ( nd_gentrace ) finalize_tracelist(i,cont);
                   2850:     nfv = ndtondv(0,nf);
                   2851:     nd_free(nf);
                   2852:     nd_bound[i] = ndv_compute_bound(nfv);
                   2853:     register_hcf(nfv);
                   2854:     if ( nd_demand ) {
                   2855:     ndv_save(nfv,i);
                   2856:     ndv_free(nfv);
                   2857:     } else
                   2858:     nd_ps_trace[i] = nfv;
                   2859:   }
                   2860:   return 1;
1.1       noro     2861: }
                   2862:
                   2863: static struct oEGT eg_invdalg;
                   2864: struct oEGT eg_le;
                   2865:
                   2866: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
                   2867: {
                   2868:     NODE tn;
                   2869:     P p1;
                   2870:
                   2871:     for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
                   2872:         substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
                   2873:     }
                   2874:     *r = p;
                   2875: }
                   2876:
                   2877: NODE nd_gb_trace(int m,int ishomo,int **indp)
                   2878: {
1.6       noro     2879:   int i,nh,sugar,stat;
                   2880:   NODE r,g,t;
                   2881:   ND_pairs d;
                   2882:   ND_pairs l;
                   2883:   ND h,nf,nfq,s,head;
                   2884:   NDV nfv,nfqv;
                   2885:   Z q,den,num;
                   2886:   P hc;
                   2887:   union oNDC dn,hnfq;
                   2888:   struct oEGT eg_monic,egm0,egm1;
                   2889:   int diag_count = 0;
                   2890:   P cont;
                   2891:   LIST list;
                   2892:
                   2893:   init_eg(&eg_monic);
                   2894:   init_eg(&eg_invdalg);
                   2895:   init_eg(&eg_le);
                   2896:   g = 0; d = 0;
                   2897:   for ( i = 0; i < nd_psn; i++ ) {
                   2898:     d = update_pairs(d,g,i,0);
                   2899:     g = update_base(g,i);
                   2900:   }
                   2901:   sugar = 0;
                   2902:   while ( d ) {
1.1       noro     2903: again:
1.6       noro     2904:     l = nd_minp(d,&d);
                   2905:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   2906:     if ( SG(l) != sugar ) {
1.1       noro     2907: #if 1
1.6       noro     2908:       if ( ishomo ) {
                   2909:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2910:         stat = do_diagonalize_trace(sugar,m);
                   2911:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2912:         diag_count = 0;
1.1       noro     2913:         if ( !stat ) {
1.6       noro     2914:           NEXT(l) = d; d = l;
                   2915:           d = nd_reconstruct(1,d);
                   2916:           goto again;
1.1       noro     2917:         }
1.6       noro     2918:       }
                   2919: #endif
                   2920:       sugar = SG(l);
                   2921:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2922:     }
                   2923:     stat = nd_sp(m,0,l,&h);
                   2924:     if ( !stat ) {
                   2925:       NEXT(l) = d; d = l;
                   2926:       d = nd_reconstruct(1,d);
                   2927:       goto again;
                   2928:     }
1.1       noro     2929: #if USE_GEOBUCKET
1.6       noro     2930:     stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.1       noro     2931: #else
1.6       noro     2932:     stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1       noro     2933: #endif
1.6       noro     2934:     if ( !stat ) {
                   2935:       NEXT(l) = d; d = l;
                   2936:       d = nd_reconstruct(1,d);
                   2937:       goto again;
                   2938:     } else if ( nf ) {
                   2939:       if ( nd_demand ) {
                   2940:         nfqv = ndv_load(nd_psn);
                   2941:         nfq = ndvtond(0,nfqv);
                   2942:       } else
                   2943:         nfq = 0;
                   2944:       if ( !nfq ) {
                   2945:         if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!Top,&nfq) ) {
                   2946:           NEXT(l) = d; d = l;
                   2947:           d = nd_reconstruct(1,d);
                   2948:           goto again;
                   2949:         }
                   2950:       }
                   2951:       if ( nfq ) {
                   2952:         /* m|HC(nfq) => failure */
                   2953:         if ( nd_vc ) {
                   2954:           nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
                   2955:         } else
                   2956:           q = HCZ(nfq);
                   2957:         if ( !remqi((Q)q,m) ) return 0;
                   2958:
                   2959:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   2960:         hnfq = HCU(nfq);
                   2961:         if ( nd_nalg ) {
                   2962:           /* m|DN(HC(nf)^(-1)) => failure */
                   2963:           get_eg(&egm0);
                   2964:           if ( !nd_monic(m,&nfq) ) return 0;
                   2965:           get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
                   2966:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   2967:           nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
                   2968:         } else {
                   2969:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   2970:           nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
                   2971:         }
                   2972:         if ( nd_gentrace ) {
                   2973:           /* exact division */
                   2974:           cont = ndc_div(0,hnfq,HCU(nfqv));
                   2975:           if ( !UNIQ(cont) ) {
                   2976:             t = mknode(4,NULLP,NULLP,NULLP,cont);
                   2977:             MKLIST(list,t); MKNODE(t,list,nd_tracelist);
                   2978:             nd_tracelist = t;
                   2979:           }
                   2980:         }
1.24      noro     2981:         nh = ndv_newps(0,nfv,nfqv);
1.6       noro     2982:         if ( ishomo && ++diag_count == diag_period ) {
                   2983:           diag_count = 0;
                   2984:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2985:           stat = do_diagonalize_trace(sugar,m);
                   2986:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2987:           if ( !stat ) {
1.1       noro     2988:             NEXT(l) = d; d = l;
                   2989:             d = nd_reconstruct(1,d);
                   2990:             goto again;
1.6       noro     2991:           }
1.1       noro     2992:         }
1.6       noro     2993:         d = update_pairs(d,g,nh,0);
                   2994:         g = update_base(g,nh);
                   2995:       } else {
                   2996:         if ( DP_Print ) { printf("*"); fflush(stdout); }
                   2997:       }
                   2998:     } else {
                   2999:       if ( DP_Print ) { printf("."); fflush(stdout); }
1.1       noro     3000:     }
1.6       noro     3001:     FREENDP(l);
                   3002:   }
                   3003:   if ( nd_nalg ) {
                   3004:     if ( DP_Print ) {
                   3005:       print_eg("monic",&eg_monic);
                   3006:       print_eg("invdalg",&eg_invdalg);
                   3007:       print_eg("le",&eg_le);
1.1       noro     3008:     }
1.6       noro     3009:   }
1.1       noro     3010:   conv_ilist(nd_demand,1,g,indp);
1.6       noro     3011:   if ( DP_Print ) { printf("nd_gb_trace done.\n"); fflush(stdout); }
                   3012:   return g;
1.1       noro     3013: }
                   3014:
                   3015: int ndv_compare(NDV *p1,NDV *p2)
                   3016: {
                   3017:     return DL_COMPARE(HDL(*p1),HDL(*p2));
                   3018: }
                   3019:
                   3020: int ndv_compare_rev(NDV *p1,NDV *p2)
                   3021: {
                   3022:     return -DL_COMPARE(HDL(*p1),HDL(*p2));
                   3023: }
                   3024:
                   3025: int ndvi_compare(NDVI p1,NDVI p2)
                   3026: {
                   3027:     return DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3028: }
                   3029:
                   3030: int ndvi_compare_rev(NDVI p1,NDVI p2)
                   3031: {
                   3032:     return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3033: }
                   3034:
                   3035: NODE ndv_reduceall(int m,NODE f)
                   3036: {
                   3037:   int i,j,n,stat;
                   3038:   ND nf,g,head;
                   3039:   NODE t,a0,a;
                   3040:   union oNDC dn;
                   3041:   Q q,num,den;
                   3042:   NODE node;
                   3043:   LIST l;
                   3044:   Z iq,jq;
                   3045:   int *perm;
                   3046:   union oNDC hc;
                   3047:   P cont,cont1;
                   3048:
                   3049:   if ( nd_nora ) return f;
                   3050:   n = length(f);
1.24      noro     3051:   ndv_setup(m,0,f,0,1,0);
1.1       noro     3052:   perm = (int *)MALLOC(n*sizeof(int));
                   3053:   if ( nd_gentrace ) {
                   3054:     for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6       noro     3055:       perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1       noro     3056:   }
                   3057:   for ( i = 0; i < n; ) {
                   3058:     if ( nd_gentrace ) {
                   3059:       /* Trace = [1,index,1,1] */
1.6       noro     3060:       STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1       noro     3061:       MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   3062:     }
                   3063:     g = ndvtond(m,nd_ps[i]);
                   3064:     g = nd_separate_head(g,&head);
1.6       noro     3065:     stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1       noro     3066:     if ( !stat )
                   3067:       nd_reconstruct(0,0);
                   3068:     else {
                   3069:       if ( DP_Print ) { printf("."); fflush(stdout); }
                   3070:       ndv_free(nd_ps[i]);
                   3071:       hc = HCU(nf); nd_removecont(m,nf);
                   3072:       if ( nd_gentrace ) {
                   3073:         for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6       noro     3074:           jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
                   3075:           STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1       noro     3076:         }
1.6       noro     3077:         /* exact division */
1.1       noro     3078:         cont = ndc_div(m,hc,HCU(nf));
                   3079:         finalize_tracelist(perm[i],cont);
                   3080:       }
                   3081:       nd_ps[i] = ndtondv(m,nf); nd_free(nf);
                   3082:       nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   3083:       i++;
                   3084:     }
                   3085:   }
                   3086:   if ( DP_Print ) { printf("\n"); }
                   3087:   for ( a0 = 0, i = 0; i < n; i++ ) {
                   3088:     NEXTNODE(a0,a);
                   3089:     if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
                   3090:     else {
                   3091:       for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
                   3092:       BDY(a) = (pointer)nd_ps[j];
                   3093:     }
                   3094:   }
                   3095:   NEXT(a) = 0;
                   3096:   return a0;
                   3097: }
                   3098:
                   3099: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
                   3100: {
                   3101:   ND_pairs d1,nd,cur,head,prev,remove;
                   3102:
                   3103:   if ( !g ) return d;
                   3104:   /* for testing */
                   3105:   if ( gensyz && nd_gensyz == 2 ) {
                   3106:     d1 = nd_newpairs(g,t);
                   3107:     if ( !d )
                   3108:       return d1;
                   3109:     else {
                   3110:       nd = d;
                   3111:       while ( NEXT(nd) ) nd = NEXT(nd);
                   3112:       NEXT(nd) = d1;
                   3113:       return d;
                   3114:     }
                   3115:   }
                   3116:   d = crit_B(d,t);
                   3117:   d1 = nd_newpairs(g,t);
                   3118:   d1 = crit_M(d1);
                   3119:   d1 = crit_F(d1);
                   3120:   if ( gensyz || do_weyl )
                   3121:     head = d1;
                   3122:   else {
                   3123:     prev = 0; cur = head = d1;
                   3124:     while ( cur ) {
                   3125:       if ( crit_2( cur->i1,cur->i2 ) ) {
                   3126:         remove = cur;
                   3127:         if ( !prev ) head = cur = NEXT(cur);
                   3128:         else cur = NEXT(prev) = NEXT(cur);
                   3129:         FREENDP(remove);
                   3130:       } else {
                   3131:         prev = cur; cur = NEXT(cur);
                   3132:       }
                   3133:     }
                   3134:   }
                   3135:   if ( !d )
                   3136:     return head;
                   3137:   else {
                   3138:     nd = d;
                   3139:     while ( NEXT(nd) ) nd = NEXT(nd);
                   3140:     NEXT(nd) = head;
                   3141:     return d;
                   3142:   }
                   3143: }
                   3144:
1.24      noro     3145: ND_pairs merge_pairs_s(ND_pairs d,ND_pairs d1);
                   3146:
1.30      noro     3147: ND_pairs update_pairs_s( ND_pairs d, int t,NODE *syz)
1.24      noro     3148: {
                   3149:   ND_pairs d1;
1.26      noro     3150:   struct oEGT eg1,eg2,eg3;
1.24      noro     3151:
1.30      noro     3152:   if ( !t ) return d;
1.26      noro     3153: get_eg(&eg1);
1.30      noro     3154:   d1 = nd_newpairs_s(t,syz);
1.26      noro     3155: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
1.24      noro     3156:   d = merge_pairs_s(d,d1);
1.26      noro     3157: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
1.24      noro     3158:   return d;
                   3159: }
1.1       noro     3160:
                   3161: ND_pairs nd_newpairs( NODE g, int t )
                   3162: {
                   3163:   NODE h;
                   3164:   UINT *dl;
                   3165:   int ts,s,i,t0,min,max;
                   3166:   ND_pairs r,r0;
                   3167:
                   3168:   dl = DL(nd_psh[t]);
                   3169:   ts = SG(nd_psh[t]) - TD(dl);
1.17      noro     3170:   if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1       noro     3171:   for ( r0 = 0, h = g; h; h = NEXT(h) ) {
                   3172:     if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
                   3173:       continue;
                   3174:     if ( nd_gbblock ) {
                   3175:       t0 = (long)BDY(h);
                   3176:       for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
                   3177:         min = nd_gbblock[i]; max = nd_gbblock[i+1];
                   3178:         if ( t0 >= min && t0 <= max && t >= min && t <= max )
                   3179:           break;
                   3180:       }
                   3181:       if ( nd_gbblock[i] >= 0 )
                   3182:         continue;
                   3183:     }
                   3184:     NEXTND_pairs(r0,r);
                   3185:     r->i1 = (long)BDY(h);
                   3186:     r->i2 = t;
                   3187:     ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
                   3188:     s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   3189:     SG(r) = MAX(s,ts) + TD(LCM(r));
                   3190:     /* experimental */
                   3191:     if ( nd_sugarweight )
                   3192:       r->sugar2 = ndl_weight2(r->lcm);
                   3193:   }
                   3194:   if ( r0 ) NEXT(r) = 0;
                   3195:   return r0;
                   3196: }
                   3197:
1.24      noro     3198: int comp_sig(SIG s1,SIG s2)
                   3199: {
                   3200: #if 0
                   3201:   if ( s1->pos > s2->pos ) return 1;
                   3202:   else if ( s1->pos < s2->pos ) return -1;
                   3203:   else return (*cmpdl)(nd_nvar,s1->dl,s2->dl);
                   3204: #else
                   3205:   static DL m1,m2;
                   3206:   static int nvar;
                   3207:   int ret;
                   3208:
1.26      noro     3209:   if ( nvar != nd_nvar ) {
                   3210:     nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
1.24      noro     3211:   }
1.31      noro     3212: #if 0
1.27      noro     3213:   _copydl(nd_nvar,nd_sba_hm[s1->pos],m1);
                   3214:   _copydl(nd_nvar,nd_sba_hm[s2->pos],m2);
1.24      noro     3215:   _addtodl(nd_nvar,s1->dl,m1);
                   3216:   _addtodl(nd_nvar,s2->dl,m2);
1.31      noro     3217: #else
                   3218:   _adddl(nd_nvar,s1->dl,nd_sba_hm[s1->pos],m1);
                   3219:   _adddl(nd_nvar,s2->dl,nd_sba_hm[s2->pos],m2);
                   3220: #endif
1.24      noro     3221:   ret = (*cmpdl)(nd_nvar,m1,m2);
                   3222:   if ( ret != 0 ) return ret;
                   3223:   else if ( s1->pos > s2->pos ) return 1;
                   3224:   else if ( s1->pos < s2->pos ) return -1;
                   3225:   else return 0;
                   3226: #endif
                   3227: }
                   3228:
                   3229: int _create_spair_s(int i1,int i2,ND_pairs sp,SIG sig1,SIG sig2)
                   3230: {
                   3231:   int ret,s1,s2;
                   3232:   RHist p1,p2;
                   3233:   static int wpd;
                   3234:   static UINT *lcm;
                   3235:
                   3236:   sp->i1 = i1;
                   3237:   sp->i2 = i2;
                   3238:   p1 = nd_psh[i1];
                   3239:   p2 = nd_psh[i2];
                   3240:   ndl_lcm(DL(p1),DL(p2),sp->lcm);
                   3241:   s1 = SG(p1)-TD(DL(p1));
                   3242:   s2 = SG(p2)-TD(DL(p2));
                   3243:   SG(sp) = MAX(s1,s2) + TD(sp->lcm);
                   3244:
1.26      noro     3245:   if ( wpd != nd_wpd ) {
1.24      noro     3246:     wpd = nd_wpd;
                   3247:     lcm = (UINT *)MALLOC(wpd*sizeof(UINT));
                   3248:   }
                   3249:   // DL(sig1) <- sp->lcm
                   3250:   // DL(sig1) -= DL(p1)
                   3251:   // DL(sig1) += DL(p1->sig)
1.27      noro     3252:   ndl_sub(sp->lcm,DL(p1),lcm);
1.24      noro     3253:   _ndltodl(lcm,DL(sig1));
                   3254:   _addtodl(nd_nvar,DL(p1->sig),DL(sig1));
                   3255:   sig1->pos = p1->sig->pos;
                   3256:
                   3257:   // DL(sig2) <- sp->lcm
                   3258:   // DL(sig2) -= DL(p2)
                   3259:   // DL(sig2) += DL(p2->sig)
1.27      noro     3260:   ndl_sub(sp->lcm,DL(p2),lcm);
1.24      noro     3261:   _ndltodl(lcm,DL(sig2));
                   3262:   _addtodl(nd_nvar,DL(p2->sig),DL(sig2));
                   3263:   sig2->pos = p2->sig->pos;
                   3264:
                   3265:   ret = comp_sig(sig1,sig2);
                   3266:   if ( ret == 0 ) return 0;
                   3267:   else if ( ret > 0 ) sp->sig = sig1;
                   3268:   else sp->sig = sig2;
                   3269:   return 1;
                   3270: }
                   3271:
                   3272: SIG dup_sig(SIG sig)
                   3273: {
                   3274:   SIG r;
                   3275:
                   3276:   if ( !sig ) return 0;
                   3277:   else {
                   3278:     NEWSIG(r);
                   3279:     _copydl(nd_nvar,DL(sig),DL(r));
                   3280:     r->pos = sig->pos;
                   3281:     return r;
                   3282:   }
                   3283: }
                   3284:
                   3285: void dup_ND_pairs(ND_pairs to,ND_pairs from)
                   3286: {
                   3287:   to->i1 = from->i1;
                   3288:   to->i2 = from->i2;
                   3289:   to->sugar = from->sugar;
                   3290:   to->sugar2 = from->sugar2;
                   3291:   ndl_copy(from->lcm,to->lcm);
                   3292:   to->sig = dup_sig(from->sig);
                   3293: }
                   3294:
                   3295: ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
                   3296: {
                   3297:   struct oND_pairs root;
                   3298:   ND_pairs q1,q2,r0,r;
                   3299:   int ret;
                   3300:
                   3301:   r = &root;
                   3302:   for ( q1 = p1, q2 = p2; q1 != 0 && q2 != 0; ) {
                   3303:     ret = comp_sig(q1->sig,q2->sig);
                   3304:     if ( ret < 0 ) {
                   3305:       r->next = q1; r = q1; q1 = q1->next;
                   3306:     } else if ( ret > 0 ) {
                   3307:       r->next = q2; r = q2; q2 = q2->next;
                   3308:     } else {
                   3309:       ret = DL_COMPARE(q1->lcm,q2->lcm);
                   3310:       if ( ret < 0 ) {
                   3311:         r->next = q1; r = q1; q1 = q1->next;
                   3312:         q2 = q2->next;
                   3313:       } else {
                   3314:         r->next = q2; r = q2; q2 = q2->next;
                   3315:         q1 = q1->next;
                   3316:       }
                   3317:     }
                   3318:   }
                   3319:   if ( q1 ) {
                   3320:     r->next = q1;
                   3321:   } else {
                   3322:     r->next = q2;
                   3323:   }
                   3324:   return root.next;
                   3325: }
                   3326:
                   3327: ND_pairs insert_pair_s(ND_pairs l,ND_pairs s)
                   3328: {
                   3329:   ND_pairs p,prev;
                   3330:   int ret;
                   3331:
                   3332:   for ( p = l, prev = 0; p != 0; prev = p, p = p->next ) {
                   3333:     if ( (ret = comp_sig(s->sig,p->sig)) <= 0 )
                   3334:       break;
                   3335:   }
                   3336:   if ( ret == 0 ) {
                   3337:     ret = DL_COMPARE(s->lcm,p->lcm);
                   3338:     if ( ret < 0 ) {
                   3339:       // replace p with s
                   3340:       s->next = p->next;
                   3341:       if ( prev == 0 ) {
                   3342:         return s;
                   3343:       } else {
                   3344:         prev->next = s;
                   3345:         return l;
                   3346:       }
                   3347:     } else
                   3348:       return l;
                   3349:   } else {
                   3350:     // insert s between prev and p
                   3351:     s->next = p;
                   3352:     if ( prev == 0 ) {
                   3353:       return s;
                   3354:     } else {
                   3355:       prev->next = s;
                   3356:       return l;
                   3357:     }
                   3358:   }
                   3359: }
                   3360:
1.29      noro     3361: INLINE int __dl_redble(DL d1,DL d2,int nvar)
                   3362: {
                   3363:   int i;
                   3364:
                   3365:   if ( d1->td > d2->td )
                   3366:     return 0;
                   3367:   for ( i = nvar-1; i >= 0; i-- )
                   3368:     if ( d1->d[i] > d2->d[i] )
                   3369:       break;
                   3370:   if ( i >= 0 )
                   3371:     return 0;
                   3372:   else
                   3373:     return 1;
                   3374: }
                   3375:
1.30      noro     3376: ND_pairs nd_newpairs_s(int t, NODE *syz)
1.24      noro     3377: {
                   3378:   NODE h,s;
                   3379:   UINT *dl;
1.27      noro     3380:   int ts,ret,i;
1.24      noro     3381:   ND_pairs r,r0,_sp,sp;
1.29      noro     3382:   SIG spsig,tsig;
                   3383:   static int nvar;
                   3384:   static SIG _sig1,_sig2;
1.26      noro     3385:   struct oEGT eg1,eg2,eg3,eg4;
1.24      noro     3386:
                   3387:   NEWND_pairs(_sp);
1.29      noro     3388:   if ( !_sig1 || nvar != nd_nvar ) {
                   3389:     nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
                   3390:   }
1.24      noro     3391:   r0 = 0;
1.27      noro     3392:   for ( i = 0; i < t; i++ ) {
                   3393:     ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
1.24      noro     3394:     if ( ret ) {
                   3395:       spsig = _sp->sig;
1.29      noro     3396:       for ( s = syz[spsig->pos]; s; s = s->next ) {
1.24      noro     3397:         tsig = (SIG)s->body;
1.29      noro     3398:         if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
1.24      noro     3399:           break;
                   3400:       }
                   3401:       if ( s == 0 ) {
                   3402:         NEWND_pairs(sp);
                   3403:         dup_ND_pairs(sp,_sp);
                   3404:         r0 = insert_pair_s(r0,sp);
1.26      noro     3405:       } else
                   3406:         Nsyz++;
1.24      noro     3407:     }
                   3408:   }
                   3409:   return r0;
                   3410: }
                   3411:
1.1       noro     3412: /* ipair = [i1,i2],[i1,i2],... */
                   3413: ND_pairs nd_ipairtospair(NODE ipair)
                   3414: {
                   3415:   int s1,s2;
                   3416:   NODE tn,t;
                   3417:   ND_pairs r,r0;
                   3418:
                   3419:   for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
                   3420:     NEXTND_pairs(r0,r);
                   3421:     tn = BDY((LIST)BDY(t));
1.6       noro     3422:     r->i1 = ZTOS((Q)ARG0(tn));
                   3423:     r->i2 = ZTOS((Q)ARG1(tn));
1.1       noro     3424:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
                   3425:     s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   3426:     s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
                   3427:     SG(r) = MAX(s1,s2) + TD(LCM(r));
                   3428:     /* experimental */
                   3429:     if ( nd_sugarweight )
                   3430:         r->sugar2 = ndl_weight2(r->lcm);
                   3431:   }
                   3432:   if ( r0 ) NEXT(r) = 0;
                   3433:   return r0;
                   3434: }
                   3435:
                   3436: /* kokokara */
                   3437:
                   3438: ND_pairs crit_B( ND_pairs d, int s )
                   3439: {
                   3440:   ND_pairs cur,head,prev,remove;
                   3441:   UINT *t,*tl,*lcm;
                   3442:   int td,tdl;
                   3443:
                   3444:   if ( !d ) return 0;
                   3445:   t = DL(nd_psh[s]);
                   3446:   prev = 0;
                   3447:   head = cur = d;
                   3448:   lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   3449:   while ( cur ) {
                   3450:     tl = cur->lcm;
                   3451:     if ( ndl_reducible(tl,t) ) {
                   3452:       ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
                   3453:       if ( !ndl_equal(lcm,tl) ) {
                   3454:         ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
                   3455:         if (!ndl_equal(lcm,tl)) {
                   3456:           remove = cur;
                   3457:           if ( !prev ) {
                   3458:             head = cur = NEXT(cur);
                   3459:           } else {
                   3460:             cur = NEXT(prev) = NEXT(cur);
                   3461:           }
                   3462:           FREENDP(remove);
                   3463:         } else {
                   3464:           prev = cur; cur = NEXT(cur);
                   3465:         }
                   3466:       } else {
                   3467:         prev = cur; cur = NEXT(cur);
                   3468:       }
                   3469:     } else {
                   3470:       prev = cur; cur = NEXT(cur);
                   3471:     }
                   3472:   }
                   3473:   return head;
                   3474: }
                   3475:
                   3476: ND_pairs crit_M( ND_pairs d1 )
                   3477: {
                   3478:     ND_pairs e,d2,d3,dd,p;
                   3479:     UINT *id,*jd;
                   3480:
                   3481:     if ( !d1 ) return d1;
                   3482:     for ( dd = 0, e = d1; e; e = d3 ) {
                   3483:         if ( !(d2 = NEXT(e)) ) {
                   3484:             NEXT(e) = dd;
                   3485:             return e;
                   3486:         }
                   3487:         id = LCM(e);
                   3488:         for ( d3 = 0; d2; d2 = p ) {
                   3489:             p = NEXT(d2);
                   3490:             jd = LCM(d2);
                   3491:             if ( ndl_equal(jd,id) )
                   3492:                 ;
                   3493:             else if ( TD(jd) > TD(id) )
                   3494:                 if ( ndl_reducible(jd,id) ) continue;
                   3495:                 else ;
                   3496:             else if ( ndl_reducible(id,jd) ) goto delit;
                   3497:             NEXT(d2) = d3;
                   3498:             d3 = d2;
                   3499:         }
                   3500:         NEXT(e) = dd;
                   3501:         dd = e;
                   3502:         continue;
                   3503:         /**/
                   3504:     delit:    NEXT(d2) = d3;
                   3505:         d3 = d2;
                   3506:         for ( ; p; p = d2 ) {
                   3507:             d2 = NEXT(p);
                   3508:             NEXT(p) = d3;
                   3509:             d3 = p;
                   3510:         }
                   3511:         FREENDP(e);
                   3512:     }
                   3513:     return dd;
                   3514: }
                   3515:
                   3516: ND_pairs crit_F( ND_pairs d1 )
                   3517: {
                   3518:     ND_pairs rest, head,remove;
                   3519:     ND_pairs last, p, r, w;
                   3520:     int s;
                   3521:
                   3522:     if ( !d1 ) return d1;
                   3523:     for ( head = last = 0, p = d1; NEXT(p); ) {
                   3524:         r = w = equivalent_pairs(p,&rest);
                   3525:         s = SG(r);
                   3526:         w = NEXT(w);
                   3527:         while ( w ) {
                   3528:             if ( crit_2(w->i1,w->i2) ) {
                   3529:                 r = w;
                   3530:                 w = NEXT(w);
                   3531:                 while ( w ) {
                   3532:                     remove = w;
                   3533:                     w = NEXT(w);
                   3534:                     FREENDP(remove);
                   3535:                 }
                   3536:                 break;
                   3537:             } else if ( SG(w) < s ) {
                   3538:                 FREENDP(r);
                   3539:                 r = w;
                   3540:                 s = SG(r);
                   3541:                 w = NEXT(w);
                   3542:             } else {
                   3543:                 remove = w;
                   3544:                 w = NEXT(w);
                   3545:                 FREENDP(remove);
                   3546:             }
                   3547:         }
                   3548:         if ( last ) NEXT(last) = r;
                   3549:         else head = r;
                   3550:         NEXT(last = r) = 0;
                   3551:         p = rest;
                   3552:         if ( !p ) return head;
                   3553:     }
                   3554:     if ( !last ) return p;
                   3555:     NEXT(last) = p;
                   3556:     return head;
                   3557: }
                   3558:
                   3559: int crit_2( int dp1, int dp2 )
                   3560: {
                   3561:     return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
                   3562: }
                   3563:
                   3564: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
                   3565: {
                   3566:     ND_pairs w,p,r,s;
                   3567:     UINT *d;
                   3568:
                   3569:     w = d1;
                   3570:     d = LCM(w);
                   3571:     s = NEXT(w);
                   3572:     NEXT(w) = 0;
                   3573:     for ( r = 0; s; s = p ) {
                   3574:         p = NEXT(s);
                   3575:         if ( ndl_equal(d,LCM(s)) ) {
                   3576:             NEXT(s) = w; w = s;
                   3577:         } else {
                   3578:             NEXT(s) = r; r = s;
                   3579:         }
                   3580:     }
                   3581:     *prest = r;
                   3582:     return w;
                   3583: }
                   3584:
                   3585: NODE update_base(NODE nd,int ndp)
                   3586: {
                   3587:     UINT *dl, *dln;
                   3588:     NODE last, p, head;
                   3589:
                   3590:     dl = DL(nd_psh[ndp]);
                   3591:     for ( head = last = 0, p = nd; p; ) {
                   3592:         dln = DL(nd_psh[(long)BDY(p)]);
                   3593:         if ( ndl_reducible( dln, dl ) ) {
                   3594:             p = NEXT(p);
                   3595:             if ( last ) NEXT(last) = p;
                   3596:         } else {
                   3597:             if ( !last ) head = p;
                   3598:             p = NEXT(last = p);
                   3599:         }
                   3600:     }
                   3601:     head = append_one(head,ndp);
                   3602:     return head;
                   3603: }
                   3604:
                   3605: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   3606: {
                   3607:     ND_pairs m,ml,p,l;
                   3608:     UINT *lcm;
                   3609:     int s,td,len,tlen,c,c1;
                   3610:
                   3611:     if ( !(p = NEXT(m = d)) ) {
                   3612:         *prest = p;
                   3613:         NEXT(m) = 0;
                   3614:         return m;
                   3615:     }
                   3616:     if ( !NoSugar ) {
                   3617:         if ( nd_sugarweight ) {
                   3618:             s = m->sugar2;
                   3619:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   3620:                 if ( (p->sugar2 < s)
                   3621:                     || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   3622:                     ml = l; m = p; s = m->sugar2;
                   3623:                 }
                   3624:         } else {
                   3625:             s = SG(m);
                   3626:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   3627:                 if ( (SG(p) < s)
                   3628:                     || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   3629:                     ml = l; m = p; s = SG(m);
                   3630:                 }
                   3631:         }
                   3632:     } else {
                   3633:         for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   3634:             if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
                   3635:                 ml = l; m = p; s = SG(m);
                   3636:             }
                   3637:     }
                   3638:     if ( !ml ) *prest = NEXT(m);
                   3639:     else {
                   3640:         NEXT(ml) = NEXT(m);
                   3641:         *prest = d;
                   3642:     }
                   3643:     NEXT(m) = 0;
                   3644:     return m;
                   3645: }
                   3646:
                   3647: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   3648: {
                   3649:     int msugar,i;
                   3650:     ND_pairs t,dm0,dm,dr0,dr;
                   3651:
                   3652:     if ( nd_sugarweight ) {
                   3653:         for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
                   3654:             if ( t->sugar2 < msugar ) msugar = t->sugar2;
                   3655:         dm0 = 0; dr0 = 0;
                   3656:         for ( i = 0, t = d; t; t = NEXT(t) )
                   3657:             if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
                   3658:                 if ( dm0 ) NEXT(dm) = t;
                   3659:                 else dm0 = t;
                   3660:                 dm = t;
                   3661:                 i++;
                   3662:             } else {
                   3663:                 if ( dr0 ) NEXT(dr) = t;
                   3664:                 else dr0 = t;
                   3665:                 dr = t;
                   3666:             }
                   3667:     } else {
                   3668:         for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   3669:             if ( SG(t) < msugar ) msugar = SG(t);
                   3670:         dm0 = 0; dr0 = 0;
                   3671:         for ( i = 0, t = d; t; t = NEXT(t) )
                   3672:             if ( i < nd_f4_nsp && SG(t) == msugar ) {
                   3673:                 if ( dm0 ) NEXT(dm) = t;
                   3674:                 else dm0 = t;
                   3675:                 dm = t;
                   3676:                 i++;
                   3677:             } else {
                   3678:                 if ( dr0 ) NEXT(dr) = t;
                   3679:                 else dr0 = t;
                   3680:                 dr = t;
                   3681:             }
                   3682:     }
                   3683:     NEXT(dm) = 0;
                   3684:     if ( dr0 ) NEXT(dr) = 0;
                   3685:     *prest = dr0;
                   3686:     return dm0;
                   3687: }
                   3688:
1.28      noro     3689: ND_pairs nd_minsugarp_s( ND_pairs d, ND_pairs *prest )
                   3690: {
                   3691:   int msugar;
                   3692:   ND_pairs t,last;
                   3693:
1.31      noro     3694: #if 1
1.28      noro     3695:   for ( msugar = SG(d), t = d; t; t = NEXT(t) )
                   3696:     if ( SG(t) == msugar ) last = t;
                   3697: #else
                   3698:   msugar = (d->sig->dl->td)+nd_sba_hm[d->sig->pos]->td;
                   3699:   for ( t = d; t; t = NEXT(t) )
                   3700:     if ( ((t->sig->dl->td)+nd_sba_hm[t->sig->pos]->td) == msugar ) last = t;
                   3701: #endif
                   3702:   *prest = last->next;
                   3703:   last->next = 0;
                   3704:   return d;
                   3705: }
                   3706:
1.1       noro     3707: int nd_tdeg(NDV c)
                   3708: {
                   3709:   int wmax = 0;
                   3710:   int i,len;
                   3711:   NMV a;
                   3712:
                   3713:   len = LEN(c);
                   3714:   for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   3715:     wmax = MAX(TD(DL(a)),wmax);
                   3716:   return wmax;
                   3717: }
                   3718:
1.24      noro     3719: int ndv_newps(int m,NDV a,NDV aq)
1.1       noro     3720: {
                   3721:     int len;
                   3722:     RHist r;
                   3723:     NDV b;
                   3724:     NODE tn;
                   3725:     LIST l;
                   3726:     Z iq;
                   3727:
                   3728:     if ( nd_psn == nd_pslen ) {
                   3729:         nd_pslen *= 2;
                   3730:         nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
                   3731:         nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
                   3732:         nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
                   3733:         nd_bound = (UINT **)
                   3734:             REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
                   3735:         nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
                   3736:         nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
                   3737:     }
                   3738:     NEWRHist(r); nd_psh[nd_psn] = r;
                   3739:     nd_ps[nd_psn] = a;
                   3740:     if ( aq ) {
                   3741:         nd_ps_trace[nd_psn] = aq;
                   3742:         if ( !m ) {
                   3743:           register_hcf(aq);
                   3744:         } else if ( m == -2 ) {
                   3745:           /* do nothing */
                   3746:         } else
                   3747:           error("ndv_newps : invalud modulus");
                   3748:         nd_bound[nd_psn] = ndv_compute_bound(aq);
                   3749: #if 1
                   3750:         SG(r) = SG(aq);
                   3751: #else
                   3752:         SG(r) = nd_tdeg(aq);
                   3753: #endif
                   3754:         ndl_copy(HDL(aq),DL(r));
1.24      noro     3755:         r->sig = dup_sig(aq->sig);
1.1       noro     3756:     } else {
                   3757:         if ( !m ) register_hcf(a);
                   3758:         nd_bound[nd_psn] = ndv_compute_bound(a);
                   3759: #if 1
                   3760:         SG(r) = SG(a);
                   3761: #else
                   3762:         SG(r) = nd_tdeg(a);
                   3763: #endif
                   3764:         ndl_copy(HDL(a),DL(r));
1.24      noro     3765:         r->sig = dup_sig(a->sig);
1.1       noro     3766:     }
                   3767:     if ( nd_demand ) {
                   3768:         if ( aq ) {
                   3769:             ndv_save(nd_ps_trace[nd_psn],nd_psn);
                   3770:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   3771:             nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   3772:             nd_ps_trace[nd_psn] = 0;
                   3773:         } else {
                   3774:             ndv_save(nd_ps[nd_psn],nd_psn);
                   3775:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
                   3776:             nd_ps[nd_psn] = 0;
                   3777:         }
                   3778:     }
                   3779:     if ( nd_gentrace ) {
                   3780:         /* reverse the tracelist and append it to alltracelist */
                   3781:         nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6       noro     3782:         STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1       noro     3783:         MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
                   3784:     }
                   3785:     return nd_psn++;
                   3786: }
                   3787:
                   3788: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
                   3789: /* return 1 if success, 0 if failure (HC(a mod p)) */
                   3790:
1.24      noro     3791: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont,int sba)
1.1       noro     3792: {
1.6       noro     3793:   int i,j,td,len,max;
                   3794:   NODE s,s0,f0,tn;
                   3795:   UINT *d;
                   3796:   RHist r;
                   3797:   NDVI w;
                   3798:   NDV a,am;
                   3799:   union oNDC hc;
                   3800:   NODE node;
                   3801:   P hcp;
                   3802:   Z iq,jq;
                   3803:   LIST l;
                   3804:
                   3805:   nd_found = 0; nd_notfirst = 0; nd_create = 0;
                   3806:   /* initialize the tracelist */
                   3807:   nd_tracelist = 0;
                   3808:
                   3809:   for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
                   3810:   w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
                   3811:   for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
                   3812:     if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
                   3813:   if ( !dont_sort ) {
                   3814:     /* XXX heuristic */
1.26      noro     3815:     if ( !sba && !nd_ord->id && (nd_ord->ord.simple<2) )
1.6       noro     3816:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   3817:         (int (*)(const void *,const void *))ndvi_compare_rev);
                   3818:     else
                   3819:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   3820:         (int (*)(const void *,const void *))ndvi_compare);
                   3821:   }
                   3822:   nd_pslen = 2*nd_psn;
                   3823:   nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3824:   nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3825:   nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3826:   nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3827:   nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
                   3828:   nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
                   3829:   nd_hcf = 0;
1.1       noro     3830:
1.6       noro     3831:   if ( trace && nd_vc )
                   3832:     makesubst(nd_vc,&nd_subst);
                   3833:   else
                   3834:     nd_subst = 0;
1.1       noro     3835:
1.6       noro     3836:   if ( !nd_red )
                   3837:     nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   3838:   for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
                   3839:   for ( i = 0; i < nd_psn; i++ ) {
                   3840:     hc = HCU(w[i].p);
                   3841:     if ( trace ) {
                   3842:       if ( mod == -2 ) {
                   3843:         /* over a large finite field */
                   3844:         /* trace = small modulus */
                   3845:         a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
                   3846:         ndv_mod(-2,a);
                   3847:         if ( !dont_removecont) ndv_removecont(-2,a);
                   3848:         am = nd_ps[i] = ndv_dup(trace,w[i].p);
                   3849:         ndv_mod(trace,am);
                   3850:       if ( DL_COMPARE(HDL(am),HDL(a)) )
                   3851:         return 0;
                   3852:         ndv_removecont(trace,am);
                   3853:       } else {
                   3854:         a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
                   3855:         if ( !dont_removecont) ndv_removecont(0,a);
                   3856:         register_hcf(a);
                   3857:         am = nd_ps[i] = ndv_dup(mod,a);
                   3858:         ndv_mod(mod,am);
                   3859:       if ( DL_COMPARE(HDL(am),HDL(a)) )
                   3860:         return 0;
                   3861:         ndv_removecont(mod,am);
                   3862:       }
                   3863:     } else {
                   3864:       a = nd_ps[i] = ndv_dup(mod,w[i].p);
                   3865:       if ( mod || !dont_removecont ) ndv_removecont(mod,a);
                   3866:       if ( !mod ) register_hcf(a);
1.1       noro     3867:     }
1.6       noro     3868:     if ( nd_gentrace ) {
                   3869:       STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
                   3870:       /* exact division */
1.1       noro     3871:       if ( !dont_removecont )
1.6       noro     3872:         ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
                   3873:       MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
                   3874:     }
                   3875:     NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
                   3876:     nd_bound[i] = ndv_compute_bound(a);
                   3877:     nd_psh[i] = r;
                   3878:     if ( nd_demand ) {
                   3879:       if ( trace ) {
                   3880:         ndv_save(nd_ps_trace[i],i);
                   3881:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   3882:         nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   3883:         nd_ps_trace[i] = 0;
                   3884:       } else {
                   3885:         ndv_save(nd_ps[i],i);
                   3886:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
                   3887:         nd_ps[i] = 0;
                   3888:       }
1.1       noro     3889:     }
1.6       noro     3890:   }
1.24      noro     3891:   if ( sba ) {
1.27      noro     3892:     nd_sba_hm = (DL *)MALLOC(nd_psn*sizeof(DL));
1.24      noro     3893:    // setup signatures
1.27      noro     3894:     for ( i = 0; i < nd_psn; i++ ) {
                   3895:       SIG sig;
1.24      noro     3896:
1.27      noro     3897:       NEWSIG(sig); sig->pos = i;
                   3898:       nd_ps[i]->sig = sig;
                   3899:       if ( nd_demand ) nd_ps_sym[i]->sig = sig;
                   3900:         nd_psh[i]->sig = sig;
                   3901:       if ( trace ) {
                   3902:         nd_ps_trace[i]->sig = sig;
                   3903:         if ( nd_demand ) nd_ps_trace_sym[i]->sig = sig;
                   3904:       }
                   3905:       NEWDL(nd_sba_hm[i],nd_nvar);
                   3906:       _ndltodl(DL(nd_psh[i]),nd_sba_hm[i]);
                   3907:     }
1.29      noro     3908:     nd_sba_pos = (NODE *)MALLOC(nd_psn*sizeof(NODE));
                   3909:     for ( i = 0; i < nd_psn; i++ ) {
                   3910:       j = nd_psh[i]->sig->pos;
                   3911:       nd_sba_pos[j] = append_one(nd_sba_pos[j],i);
                   3912:     }
1.24      noro     3913:   }
1.6       noro     3914:   if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
                   3915:   return 1;
1.1       noro     3916: }
                   3917:
                   3918: struct order_spec *append_block(struct order_spec *spec,
                   3919:     int nv,int nalg,int ord);
                   3920:
                   3921: extern VECT current_dl_weight_vector_obj;
                   3922: static VECT prev_weight_vector_obj;
                   3923:
                   3924: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
                   3925:     struct order_spec **ord1p,LIST *f1p,NODE *alistp)
                   3926: {
                   3927:     NODE alist,t,s,r0,r,arg;
                   3928:     VL tv;
                   3929:     P poly;
                   3930:     DP d;
                   3931:     Alg alpha,dp;
                   3932:     DAlg inv,da,hc;
                   3933:     MP m;
                   3934:     int i,nvar,nalg,n;
                   3935:     NumberField nf;
                   3936:     LIST f1,f2;
                   3937:     struct order_spec *current_spec;
                   3938:     VECT obj,obj0;
                   3939:     VECT tmp;
                   3940:
                   3941:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
                   3942:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
                   3943:
                   3944:     for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
                   3945:         NEXTNODE(alist,t); MKV(tv->v,poly);
                   3946:         MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
                   3947:         tv->v = tv->v->priv;
                   3948:     }
                   3949:     NEXT(t) = 0;
                   3950:
                   3951:     /* simplification, making polynomials monic */
                   3952:     setfield_dalg(alist);
                   3953:     obj_algtodalg((Obj)f,(Obj *)&f1);
                   3954:     for ( t = BDY(f); t; t = NEXT(t) ) {
                   3955:         initd(ord); ptod(vv,vv,(P)BDY(t),&d);
                   3956:         hc = (DAlg)BDY(d)->c;
                   3957:         if ( NID(hc) == N_DA ) {
                   3958:             invdalg(hc,&inv);
                   3959:             for ( m = BDY(d); m; m = NEXT(m) ) {
                   3960:                 muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
                   3961:             }
                   3962:         }
                   3963:         initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
                   3964:     }
                   3965:     obj_dalgtoalg((Obj)f1,(Obj *)&f);
                   3966:
                   3967:     /* append alg vars to the var list */
                   3968:     for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
                   3969:     NEXT(tv) = av;
                   3970:
                   3971:     /* append a block to ord */
                   3972:     *ord1p = append_block(ord,nvar,nalg,2);
                   3973:
                   3974:     /* create generator list */
                   3975:     nf = get_numberfield();
                   3976:     for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
                   3977:         MKAlg(nf->defpoly[i],dp);
                   3978:         MKNODE(s,dp,t); t = s;
                   3979:     }
                   3980:     MKLIST(f1,t);
                   3981:     *alistp = alist;
                   3982:     algobjtorat((Obj)f1,(Obj *)f1p);
                   3983:
                   3984:     /* creating a new weight vector */
                   3985:     prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
                   3986:     n = nvar+nalg+1;
                   3987:     MKVECT(obj,n);
                   3988:     if ( obj0 && obj0->len == nvar )
                   3989:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
                   3990:     else
                   3991:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
                   3992:     for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
                   3993:     BDY(obj)[n-1] = (pointer)ONE;
                   3994:     arg = mknode(1,obj);
                   3995:     Pdp_set_weight(arg,&tmp);
                   3996: }
                   3997:
                   3998: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
                   3999: {
                   4000:     NODE s,t,u0,u;
                   4001:     P p;
                   4002:     VL tv;
                   4003:     Obj obj;
                   4004:     VECT tmp;
                   4005:     NODE arg;
                   4006:
                   4007:     u0 = 0;
                   4008:     for ( t = r; t; t = NEXT(t) ) {
                   4009:         p = (P)BDY(t);
                   4010:         for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
                   4011:             substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
                   4012:         }
                   4013:         if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
                   4014:             NEXTNODE(u0,u);
                   4015:             BDY(u) = (pointer)p;
                   4016:         }
                   4017:     }
                   4018:     arg = mknode(1,prev_weight_vector_obj);
                   4019:     Pdp_set_weight(arg,&tmp);
                   4020:
                   4021:     return u0;
                   4022: }
                   4023:
                   4024: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
                   4025: {
                   4026:     VL tv,fv,vv,vc,av;
                   4027:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4028:     int e,max,nvar,i;
                   4029:     NDV b;
                   4030:     int ishomo,nalg,mrank,trank,wmax,len;
                   4031:   NMV a;
                   4032:     Alg alpha,dp;
                   4033:     P p,zp;
                   4034:     Q dmy;
                   4035:     LIST f1,f2,zpl;
                   4036:     Obj obj;
                   4037:     NumberField nf;
                   4038:     struct order_spec *ord1;
                   4039:     NODE tr,tl1,tl2,tl3,tl4,nzlist;
                   4040:     LIST l1,l2,l3,l4,l5;
                   4041:   int j;
                   4042:   Z jq,bpe,last_nonzero;
                   4043:     int *perm;
                   4044:     EPOS oepos;
                   4045:     int obpe,oadv,ompos,cbpe;
1.15      noro     4046:     VECT hvect;
1.1       noro     4047:
                   4048:     nd_module = 0;
                   4049:     if ( !m && Demand ) nd_demand = 1;
                   4050:     else nd_demand = 0;
                   4051:     parse_nd_option(current_option);
                   4052:
                   4053:     if ( DP_Multiple )
                   4054:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4055: #if 0
                   4056:     ndv_alloc = 0;
                   4057: #endif
                   4058:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4059:     if ( m && nd_vc )
                   4060:        error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
                   4061:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4062:     switch ( ord->id ) {
                   4063:         case 1:
                   4064:             if ( ord->nv != nvar )
                   4065:                 error("nd_{gr,f4} : invalid order specification");
                   4066:             break;
                   4067:         default:
                   4068:             break;
                   4069:     }
                   4070:     nd_nalg = 0;
                   4071:     av = 0;
                   4072:     if ( !m ) {
                   4073:         get_algtree((Obj)f,&av);
                   4074:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4075:         nd_ntrans = nvar;
                   4076:         nd_nalg = nalg;
                   4077:         /* #i -> t#i */
                   4078:         if ( nalg ) {
                   4079:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4080:             ord = ord1;
                   4081:             f = f1;
                   4082:         }
                   4083:         nvar += nalg;
                   4084:     }
                   4085:     nd_init_ord(ord);
                   4086:     mrank = 0;
                   4087:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4088:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4089:             if ( nd_module ) {
1.16      noro     4090:                 if ( OID(BDY(t)) == O_DPM ) {
                   4091:                   e = dpm_getdeg((DPM)BDY(t),&trank);
                   4092:                   max = MAX(e,max);
                   4093:                   mrank = MAX(mrank,trank);
                   4094:                 } else {
                   4095:                   s = BDY((LIST)BDY(t));
                   4096:                   trank = length(s);
                   4097:                   mrank = MAX(mrank,trank);
                   4098:                   for ( ; s; s = NEXT(s) ) {
                   4099:                       e = getdeg(tv->v,(P)BDY(s));
                   4100:                       max = MAX(e,max);
                   4101:                   }
1.1       noro     4102:                 }
                   4103:             } else {
                   4104:                 e = getdeg(tv->v,(P)BDY(t));
                   4105:                 max = MAX(e,max);
                   4106:             }
                   4107:         }
                   4108:     nd_setup_parameters(nvar,nd_nzlist?0:max);
                   4109:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   4110:     ishomo = 1;
                   4111:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4112:       if ( nd_module ) {
1.16      noro     4113:         if ( OID(BDY(t)) == O_DPM ) {
                   4114:           Z cont;
                   4115:           DPM zdpm;
                   4116:
                   4117:           if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
                   4118:           else zdpm = (DPM)BDY(t);
                   4119:           b = (pointer)dpmtondv(m,zdpm);
                   4120:         } else {
                   4121:           if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   4122:           else zpl = (LIST)BDY(t);
1.1       noro     4123:           b = (pointer)pltondv(CO,vv,zpl);
1.16      noro     4124:         }
1.1       noro     4125:       } else {
                   4126:         if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   4127:         else zp = (P)BDY(t);
                   4128:         b = (pointer)ptondv(CO,vv,zp);
                   4129:       }
                   4130:       if ( ishomo )
                   4131:         ishomo = ishomo && ndv_ishomo(b);
                   4132:       if ( m ) ndv_mod(m,b);
                   4133:       if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4134:     }
                   4135:     if ( fd0 ) NEXT(fd) = 0;
                   4136:
                   4137:   if ( !ishomo && homo ) {
                   4138:         for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   4139:             b = (NDV)BDY(t); len = LEN(b);
                   4140:             for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   4141:                 wmax = MAX(TD(DL(a)),wmax);
                   4142:         }
                   4143:         homogenize_order(ord,nvar,&ord1);
                   4144:         nd_init_ord(ord1);
                   4145:         nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   4146:         for ( t = fd0; t; t = NEXT(t) )
                   4147:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   4148:     }
                   4149:
1.24      noro     4150:     ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0,0);
1.1       noro     4151:     if ( nd_gentrace ) {
                   4152:         MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   4153:     }
                   4154:   if ( nd_splist ) {
                   4155:     *rp = compute_splist();
                   4156:     return;
                   4157:   }
                   4158:   if ( nd_check_splist ) {
                   4159:         if ( f4 ) {
                   4160:             if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
                   4161:             else *rp = 0;
                   4162:         } else {
                   4163:             if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
                   4164:             else *rp = 0;
                   4165:         }
                   4166:     return;
                   4167:   }
                   4168:     x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
                   4169:   if ( !x ) {
                   4170:     *rp = 0; return;
                   4171:   }
1.15      noro     4172:   if ( nd_gentrace ) {
                   4173:     MKVECT(hvect,nd_psn);
                   4174:     for ( i = 0; i < nd_psn; i++ )
                   4175:        ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   4176:   }
1.1       noro     4177:   if ( !ishomo && homo ) {
                   4178:        /* dehomogenization */
                   4179:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   4180:     nd_init_ord(ord);
                   4181:     nd_setup_parameters(nvar,0);
                   4182:   }
                   4183:     nd_demand = 0;
                   4184:   if ( nd_module && nd_intersect ) {
                   4185:     for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17      noro     4186:       if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1       noro     4187:         MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
                   4188:       }
                   4189:     conv_ilist(nd_demand,0,x,0);
                   4190:     goto FINAL;
                   4191:   }
                   4192:     if ( nd_gentrace  && f4 ) { nzlist = nd_alltracelist; }
                   4193:     x = ndv_reducebase(x,perm);
                   4194:     if ( nd_gentrace  && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
                   4195:     x = ndv_reduceall(m,x);
                   4196:     cbpe = nd_bpe;
                   4197:     if ( nd_gentrace && !f4 ) {
                   4198:         tl2 = nd_alltracelist; nd_alltracelist = 0;
                   4199:         ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
                   4200:         tl3 = nd_alltracelist; nd_alltracelist = 0;
                   4201:         if ( nd_gensyz ) {
                   4202:           nd_gb(m,0,1,1,0);
                   4203:             tl4 = nd_alltracelist; nd_alltracelist = 0;
                   4204:         } else tl4 = 0;
                   4205:     }
                   4206:     nd_bpe = cbpe;
                   4207:     nd_setup_parameters(nd_nvar,0);
                   4208: FINAL:
                   4209:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16      noro     4210:       NEXTNODE(r0,r);
                   4211:       if ( nd_module ) {
                   4212:         if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
                   4213:         else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
                   4214:       } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
                   4215:       else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1       noro     4216:     }
                   4217:     if ( r0 ) NEXT(r) = 0;
                   4218:     if ( !m && nd_nalg )
                   4219:         r0 = postprocess_algcoef(av,alist,r0);
                   4220:     MKLIST(*rp,r0);
                   4221:     if ( nd_gentrace ) {
                   4222:   if ( f4 ) {
1.6       noro     4223:             STOZ(16,bpe);
                   4224:             STOZ(nd_last_nonzero,last_nonzero);
1.15      noro     4225:             tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1       noro     4226:         } else {
                   4227:             tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   4228:             tl3 = reverse_node(tl3);
                   4229:             /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   4230:             for ( t = tl2; t; t = NEXT(t) ) {
                   4231:             /* s = [i,[*,j,*,*],...] */
                   4232:                 s = BDY((LIST)BDY(t));
1.6       noro     4233:                 j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     4234:                 for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     4235:                     j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     4236:                     ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   4237:                 }
                   4238:             }
                   4239:             for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6       noro     4240:                 STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     4241:             }
                   4242:             MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   4243:             MKLIST(l5,tl4);
1.6       noro     4244:             STOZ(nd_bpe,bpe);
1.15      noro     4245:             tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     4246:         }
                   4247:     }
                   4248: #if 0
                   4249:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   4250: #endif
                   4251: }
                   4252:
1.28      noro     4253: NODE nd_sba_f4(int m,int **indp);
                   4254:
                   4255: void nd_sba(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.24      noro     4256: {
                   4257:   VL tv,fv,vv,vc,av;
                   4258:   NODE fd,fd0,r,r0,t,x,s,xx;
                   4259:   int e,max,nvar,i;
                   4260:   NDV b;
                   4261:   int ishomo,nalg,wmax,len;
                   4262:   NMV a;
                   4263:   P p,zp;
                   4264:   Q dmy;
                   4265:   struct order_spec *ord1;
                   4266:   int j;
                   4267:   int *perm;
                   4268:   EPOS oepos;
                   4269:   int obpe,oadv,ompos,cbpe;
1.29      noro     4270:   struct oEGT eg0,eg1,egconv;
1.24      noro     4271:
                   4272:   nd_module = 0;
                   4273:   nd_demand = 0;
                   4274:   parse_nd_option(current_option);
                   4275:
                   4276:   if ( DP_Multiple )
                   4277:     nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4278:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4279:   if ( m && nd_vc )
                   4280:     error("nd_sba : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
                   4281:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4282:   switch ( ord->id ) {
                   4283:     case 1:
                   4284:       if ( ord->nv != nvar )
                   4285:         error("nd_sba : invalid order specification");
                   4286:         break;
                   4287:       default:
                   4288:         break;
                   4289:   }
                   4290:   nd_nalg = 0;
                   4291:   nd_init_ord(ord);
                   4292:   // for SIG comparison
                   4293:   initd(ord);
                   4294:   for ( t = BDY(f), max = 1; t; t = NEXT(t) ) {
                   4295:     for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4296:       e = getdeg(tv->v,(P)BDY(t));
                   4297:       max = MAX(e,max);
                   4298:     }
                   4299:   }
1.25      noro     4300:   nd_setup_parameters(nvar,max);
1.24      noro     4301:   obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   4302:   ishomo = 1;
                   4303:   for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4304:     if ( !m ) ptozp((P)BDY(t),1,&dmy,&zp);
                   4305:     else zp = (P)BDY(t);
                   4306:     b = (pointer)ptondv(CO,vv,zp);
                   4307:     if ( ishomo )
                   4308:       ishomo = ishomo && ndv_ishomo(b);
                   4309:     if ( m ) ndv_mod(m,b);
                   4310:     if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4311:   }
                   4312:   if ( fd0 ) NEXT(fd) = 0;
                   4313:
                   4314:   if ( !ishomo && homo ) {
                   4315:     for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   4316:       b = (NDV)BDY(t); len = LEN(b);
                   4317:       for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   4318:         wmax = MAX(TD(DL(a)),wmax);
                   4319:       }
                   4320:       homogenize_order(ord,nvar,&ord1);
                   4321:       nd_init_ord(ord1);
                   4322:       nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   4323:       for ( t = fd0; t; t = NEXT(t) )
                   4324:         ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   4325:   }
                   4326:
                   4327:   ndv_setup(m,0,fd0,0,0,1);
1.28      noro     4328:   x = f4 ? nd_sba_f4(m,&perm) : nd_sba_buch(m,ishomo || homo,&perm);
1.24      noro     4329:   if ( !x ) {
                   4330:     *rp = 0; return;
                   4331:   }
                   4332:   if ( !ishomo && homo ) {
                   4333:        /* dehomogenization */
                   4334:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   4335:     nd_init_ord(ord);
                   4336:     nd_setup_parameters(nvar,0);
                   4337:   }
                   4338:   nd_demand = 0;
                   4339:   x = ndv_reducebase(x,perm);
                   4340:   x = ndv_reduceall(m,x);
                   4341:   nd_setup_parameters(nd_nvar,0);
1.29      noro     4342:   get_eg(&eg0);
1.24      noro     4343:   for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   4344:     NEXTNODE(r0,r);
                   4345:     if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
                   4346:     BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   4347:   }
                   4348:   if ( r0 ) NEXT(r) = 0;
                   4349:   MKLIST(*rp,r0);
1.29      noro     4350:   get_eg(&eg1); init_eg(&egconv); add_eg(&egconv,&eg0,&eg1);
                   4351:   print_eg("conv",&egconv); fprintf(asir_out,"\n");
1.24      noro     4352: }
                   4353:
1.1       noro     4354: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
                   4355: {
                   4356:     VL tv,fv,vv,vc,av;
                   4357:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4358:     int e,max,nvar,i;
                   4359:     NDV b;
                   4360:     int ishomo,nalg;
                   4361:     Alg alpha,dp;
                   4362:     P p,zp;
                   4363:     Q dmy;
                   4364:     LIST f1,f2;
                   4365:     Obj obj;
                   4366:     NumberField nf;
                   4367:     struct order_spec *ord1;
                   4368:     int *perm;
                   4369:
                   4370:     parse_nd_option(current_option);
                   4371:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4372:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4373:     switch ( ord->id ) {
                   4374:         case 1:
                   4375:             if ( ord->nv != nvar )
                   4376:                 error("nd_check : invalid order specification");
                   4377:             break;
                   4378:         default:
                   4379:             break;
                   4380:     }
                   4381:     nd_nalg = 0;
                   4382:     av = 0;
                   4383:     if ( !m ) {
                   4384:         get_algtree((Obj)f,&av);
                   4385:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4386:         nd_ntrans = nvar;
                   4387:         nd_nalg = nalg;
                   4388:         /* #i -> t#i */
                   4389:         if ( nalg ) {
                   4390:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4391:             ord = ord1;
                   4392:             f = f1;
                   4393:         }
                   4394:         nvar += nalg;
                   4395:     }
                   4396:     nd_init_ord(ord);
                   4397:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4398:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4399:             e = getdeg(tv->v,(P)BDY(t));
                   4400:             max = MAX(e,max);
                   4401:         }
                   4402:     nd_setup_parameters(nvar,max);
                   4403:     ishomo = 1;
                   4404:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4405:         ptozp((P)BDY(t),1,&dmy,&zp);
                   4406:         b = (pointer)ptondv(CO,vv,zp);
                   4407:         if ( ishomo )
                   4408:             ishomo = ishomo && ndv_ishomo(b);
                   4409:         if ( m ) ndv_mod(m,b);
                   4410:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4411:     }
                   4412:     if ( fd0 ) NEXT(fd) = 0;
1.24      noro     4413:     ndv_setup(m,0,fd0,0,1,0);
1.1       noro     4414:     for ( x = 0, i = 0; i < nd_psn; i++ )
                   4415:         x = update_base(x,i);
                   4416:     if ( do_check ) {
                   4417:         x = nd_gb(m,ishomo,1,0,&perm);
                   4418:         if ( !x ) {
                   4419:             *rp = 0;
                   4420:             return;
                   4421:         }
                   4422:     } else {
                   4423: #if 0
                   4424:     /* bug ? */
                   4425:         for ( t = x; t; t = NEXT(t) )
                   4426:             BDY(t) = (pointer)nd_ps[(long)BDY(t)];
                   4427: #else
                   4428:     conv_ilist(0,0,x,&perm);
                   4429: #endif
                   4430:     }
                   4431:     x = ndv_reducebase(x,perm);
                   4432:     x = ndv_reduceall(m,x);
                   4433:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   4434:         NEXTNODE(r0,r);
                   4435:         BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   4436:     }
                   4437:     if ( r0 ) NEXT(r) = 0;
                   4438:     if ( !m && nd_nalg )
                   4439:         r0 = postprocess_algcoef(av,alist,r0);
                   4440:     MKLIST(*rp,r0);
                   4441: }
                   4442:
                   4443: NDV recompute_trace(NODE trace,NDV *p,int m);
                   4444: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
                   4445:
                   4446: NDV recompute_trace(NODE ti,NDV *p,int mod)
                   4447: {
                   4448:   int c,c1,c2,i;
                   4449:   NM mul,m,tail;
                   4450:   ND d,r,rm;
                   4451:   NODE sj;
                   4452:   NDV red;
                   4453:   Obj mj;
                   4454:
                   4455:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   4456:   CM(mul) = 1;
                   4457:   tail = 0;
                   4458:   for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
                   4459:     sj = BDY((LIST)BDY(ti));
                   4460:     if ( ARG0(sj) ) {
1.6       noro     4461:       red = p[ZTOS((Q)ARG1(sj))];
1.1       noro     4462:       mj = (Obj)ARG2(sj);
                   4463:       if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
                   4464:       else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
                   4465:       rm = ndv_mul_nm(mod,mul,red);
                   4466:       if ( !r ) r = rm;
                   4467:       else {
                   4468:         for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
                   4469:           if ( d ) {
                   4470:             NEXT(tail) = m; tail = m; LEN(d)++;
                   4471:           } else {
                   4472:             MKND(nd_nvar,m,1,d); tail = BDY(d);
                   4473:           }
                   4474:         }
                   4475:         if ( !m ) return 0; /* failure */
                   4476:                 else {
                   4477:           BDY(r) = m;
                   4478:                     if ( mod > 0 || mod == -1 ) {
                   4479:             c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
                   4480:             DMAR(c1,c2,0,mod,c);
                   4481:             nd_mul_c(mod,rm,c);
                   4482:                     } else {
                   4483:                       Z t,u;
                   4484:
                   4485:                       chsgnlf(HCZ(r),&t);
                   4486:                       divlf(t,HCZ(rm),&u);
                   4487:                       nd_mul_c_lf(rm,u);
                   4488:                     }
                   4489:           r = nd_add(mod,r,rm);
                   4490:         }
                   4491:           }
                   4492:      }
                   4493:   }
                   4494:   if ( tail ) NEXT(tail) = 0;
                   4495:   d = nd_add(mod,d,r);
                   4496:   nd_mul_c(mod,d,invm(HCM(d),mod));
                   4497:   return ndtondv(mod,d);
                   4498: }
                   4499:
                   4500: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
                   4501: {
                   4502:     VL tv,fv,vv,vc,av;
                   4503:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4504:     int e,max,nvar,i;
                   4505:     NDV b;
                   4506:     int ishomo,nalg;
                   4507:     Alg alpha,dp;
                   4508:     P p,zp;
                   4509:     Q dmy;
                   4510:     LIST f1,f2;
                   4511:     Obj obj;
                   4512:     NumberField nf;
                   4513:     struct order_spec *ord1;
                   4514:   NODE permtrace,intred,ind,perm,trace,ti;
                   4515:   int len,n,j;
                   4516:   NDV *db,*pb;
                   4517:
                   4518:     parse_nd_option(current_option);
                   4519:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4520:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4521:     switch ( ord->id ) {
                   4522:         case 1:
                   4523:             if ( ord->nv != nvar )
                   4524:                 error("nd_check : invalid order specification");
                   4525:             break;
                   4526:         default:
                   4527:             break;
                   4528:     }
                   4529:     nd_init_ord(ord);
1.6       noro     4530:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     4531:     nd_setup_parameters(nvar,0);
                   4532:
                   4533:   len = length(BDY(f));
                   4534:   db = (NDV *)MALLOC(len*sizeof(NDV *));
                   4535:   for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
                   4536:       ptozp((P)BDY(t),1,&dmy,&zp);
                   4537:       b = ptondv(CO,vv,zp);
                   4538:         ndv_mod(m,b);
                   4539:     ndv_mul_c(m,b,invm(HCM(b),m));
                   4540:     db[i] = b;
                   4541:     }
                   4542:
                   4543:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   4544:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   4545:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   4546:   perm = BDY((LIST)ARG0(permtrace));
                   4547:   trace = NEXT(permtrace);
                   4548:
                   4549:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     4550:     j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1       noro     4551:     if ( j > i ) i = j;
                   4552:   }
                   4553:   n = i+1;
                   4554:   pb = (NDV *)MALLOC(n*sizeof(NDV *));
                   4555:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   4556:     ti = BDY((LIST)BDY(t));
1.6       noro     4557:     pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1       noro     4558:   }
                   4559:   for ( t = trace; t; t = NEXT(t) ) {
                   4560:     ti = BDY((LIST)BDY(t));
1.6       noro     4561:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   4562:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     4563:       if ( DP_Print ) {
                   4564:            fprintf(asir_out,"."); fflush(asir_out);
                   4565:       }
                   4566:   }
                   4567:   for ( t = intred; t; t = NEXT(t) ) {
                   4568:     ti = BDY((LIST)BDY(t));
1.6       noro     4569:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   4570:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     4571:       if ( DP_Print ) {
                   4572:            fprintf(asir_out,"*"); fflush(asir_out);
                   4573:       }
                   4574:   }
                   4575:     for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
                   4576:         NEXTNODE(r0,r);
1.6       noro     4577:     b = pb[ZTOS((Q)BDY(t))];
1.1       noro     4578:         ndv_mul_c(m,b,invm(HCM(b),m));
                   4579: #if 0
1.6       noro     4580:         BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1       noro     4581: #else
1.6       noro     4582:         BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1       noro     4583: #endif
                   4584:     }
                   4585:     if ( r0 ) NEXT(r) = 0;
                   4586:     MKLIST(*rp,r0);
                   4587:     if ( DP_Print ) fprintf(asir_out,"\n");
                   4588: }
                   4589:
1.16      noro     4590: 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     4591: {
                   4592:     VL tv,fv,vv,vc,av;
                   4593:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   4594:     int m,nocheck,nvar,mindex,e,max;
                   4595:     NDV c;
                   4596:     NMV a;
                   4597:     P p,zp;
                   4598:     Q dmy;
                   4599:     EPOS oepos;
                   4600:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   4601:     Alg alpha,dp;
                   4602:     P poly;
                   4603:     LIST f1,f2,zpl;
                   4604:     Obj obj;
                   4605:     NumberField nf;
                   4606:     struct order_spec *ord1;
                   4607:     struct oEGT eg_check,eg0,eg1;
                   4608:     NODE tr,tl1,tl2,tl3,tl4;
                   4609:     LIST l1,l2,l3,l4,l5;
                   4610:     int *perm;
                   4611:     int j,ret;
                   4612:     Z jq,bpe;
1.15      noro     4613:     VECT hvect;
1.1       noro     4614:
                   4615:     nd_module = 0;
                   4616:     nd_lf = 0;
                   4617:     parse_nd_option(current_option);
                   4618:     if ( nd_lf ) {
                   4619:       if ( f4 )
                   4620:         nd_f4_lf_trace(f,v,trace,homo,ord,rp);
                   4621:       else
                   4622:         error("nd_gr_trace is not implemented yet over a large finite field");
                   4623:       return;
                   4624:     }
                   4625:     if ( DP_Multiple )
                   4626:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4627:
                   4628:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4629:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4630:     switch ( ord->id ) {
                   4631:         case 1:
                   4632:             if ( ord->nv != nvar )
                   4633:                 error("nd_gr_trace : invalid order specification");
                   4634:             break;
                   4635:         default:
                   4636:             break;
                   4637:     }
                   4638:
                   4639:     get_algtree((Obj)f,&av);
                   4640:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4641:     nd_ntrans = nvar;
                   4642:     nd_nalg = nalg;
                   4643:     /* #i -> t#i */
                   4644:     if ( nalg ) {
                   4645:         preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4646:         ord = ord1;
                   4647:         f = f1;
                   4648:     }
                   4649:     nvar += nalg;
                   4650:
                   4651:     nocheck = 0;
                   4652:     mindex = 0;
                   4653:
                   4654:     if ( Demand ) nd_demand = 1;
                   4655:     else nd_demand = 0;
                   4656:
                   4657:     /* setup modulus */
                   4658:     if ( trace < 0 ) {
                   4659:         trace = -trace;
                   4660:         nocheck = 1;
                   4661:     }
                   4662:     m = trace > 1 ? trace : get_lprime(mindex);
                   4663:     nd_init_ord(ord);
                   4664:     mrank = 0;
                   4665:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4666:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4667:             if ( nd_module ) {
1.16      noro     4668:               if ( OID(BDY(t)) == O_DPM ) {
                   4669:                 e = dpm_getdeg((DPM)BDY(t),&trank);
                   4670:                 max = MAX(e,max);
                   4671:                 mrank = MAX(mrank,trank);
                   4672:               } else {
1.1       noro     4673:                 s = BDY((LIST)BDY(t));
                   4674:                 trank = length(s);
                   4675:                 mrank = MAX(mrank,trank);
                   4676:                 for ( ; s; s = NEXT(s) ) {
                   4677:                     e = getdeg(tv->v,(P)BDY(s));
                   4678:                     max = MAX(e,max);
                   4679:                 }
1.16      noro     4680:               }
1.1       noro     4681:             } else {
                   4682:                 e = getdeg(tv->v,(P)BDY(t));
                   4683:                 max = MAX(e,max);
                   4684:             }
                   4685:         }
                   4686:     nd_setup_parameters(nvar,max);
                   4687:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   4688:     ishomo = 1;
                   4689:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4690:         if ( nd_module ) {
1.16      noro     4691:           if ( OID(BDY(t)) == O_DPM ) {
                   4692:             Z cont;
                   4693:             DPM zdpm;
                   4694:
1.17      noro     4695:             if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16      noro     4696:             else zdpm = (DPM)BDY(t);
                   4697:             c = (pointer)dpmtondv(m,zdpm);
                   4698:           } else {
                   4699:             if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   4700:             else zpl = (LIST)BDY(t);
1.1       noro     4701:             c = (pointer)pltondv(CO,vv,zpl);
1.16      noro     4702:           }
1.1       noro     4703:         } else {
1.16      noro     4704:           if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   4705:           else zp = (P)BDY(t);
                   4706:           c = (pointer)ptondv(CO,vv,zp);
1.1       noro     4707:         }
                   4708:         if ( ishomo )
                   4709:             ishomo = ishomo && ndv_ishomo(c);
                   4710:         if ( c ) {
                   4711:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   4712:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   4713:         }
                   4714:     }
                   4715:     if ( in0 ) NEXT(in) = 0;
                   4716:     if ( fd0 ) NEXT(fd) = 0;
                   4717:     if ( !ishomo && homo ) {
                   4718:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   4719:             c = (NDV)BDY(t); len = LEN(c);
                   4720:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   4721:                 wmax = MAX(TD(DL(a)),wmax);
                   4722:         }
                   4723:         homogenize_order(ord,nvar,&ord1);
                   4724:         nd_init_ord(ord1);
                   4725:         nd_setup_parameters(nvar+1,wmax);
                   4726:         for ( t = fd0; t; t = NEXT(t) )
                   4727:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   4728:     }
                   4729:     if ( MaxDeg > 0 ) nocheck = 1;
                   4730:     while ( 1 ) {
                   4731:     tl1 = tl2 = tl3 = tl4 = 0;
                   4732:         if ( Demand )
                   4733:             nd_demand = 1;
1.24      noro     4734:         ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1       noro     4735:         if ( nd_gentrace ) {
                   4736:             MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   4737:         }
                   4738:         if ( ret )
                   4739:             cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
                   4740:         if ( !ret || !cand ) {
                   4741:             /* failure */
                   4742:             if ( trace > 1 ) { *rp = 0; return; }
                   4743:             else m = get_lprime(++mindex);
                   4744:             continue;
                   4745:         }
1.15      noro     4746:         if ( nd_gentrace ) {
                   4747:           MKVECT(hvect,nd_psn);
                   4748:           for ( i = 0; i < nd_psn; i++ )
                   4749:              ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   4750:         }
1.1       noro     4751:         if ( !ishomo && homo ) {
                   4752:             /* dehomogenization */
                   4753:             for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   4754:             nd_init_ord(ord);
                   4755:             nd_setup_parameters(nvar,0);
                   4756:         }
                   4757:         nd_demand = 0;
                   4758:         cand = ndv_reducebase(cand,perm);
                   4759:         if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
                   4760:         cand = ndv_reduceall(0,cand);
                   4761:         cbpe = nd_bpe;
                   4762:         if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
                   4763:         get_eg(&eg0);
                   4764:         if ( nocheck )
                   4765:             break;
                   4766:         if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   4767:             if ( nd_gentrace ) {
                   4768:           tl3 = nd_alltracelist; nd_alltracelist = 0;
                   4769:         } else tl3 = 0;
                   4770:             /* gbcheck : cand is a GB of Id(cand) ? */
                   4771:             if ( nd_vc || nd_gentrace || nd_gensyz )
                   4772:               ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
                   4773:             else
                   4774:               ret = nd_f4(0,1,0)!=0;
                   4775:             if ( nd_gentrace && nd_gensyz ) {
                   4776:           tl4 = nd_alltracelist; nd_alltracelist = 0;
                   4777:         } else tl4 = 0;
                   4778:     }
                   4779:     if ( ret ) break;
                   4780:         else if ( trace > 1 ) {
                   4781:             /* failure */
                   4782:             *rp = 0; return;
                   4783:         } else {
                   4784:             /* try the next modulus */
                   4785:             m = get_lprime(++mindex);
                   4786:             /* reset the parameters */
                   4787:             if ( !ishomo && homo ) {
                   4788:                 nd_init_ord(ord1);
                   4789:                 nd_setup_parameters(nvar+1,wmax);
                   4790:             } else {
                   4791:                 nd_init_ord(ord);
                   4792:                 nd_setup_parameters(nvar,max);
                   4793:             }
                   4794:         }
                   4795:     }
                   4796:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   4797:     if ( DP_Print )
1.6       noro     4798:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     4799:     /* dp->p */
                   4800:     nd_bpe = cbpe;
                   4801:     nd_setup_parameters(nd_nvar,0);
                   4802:     for ( r = cand; r; r = NEXT(r) ) {
1.16      noro     4803:       if ( nd_module ) {
1.17      noro     4804:         if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16      noro     4805:         else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17      noro     4806:       } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
                   4807:       else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1       noro     4808:     }
                   4809:     if ( nd_nalg )
                   4810:         cand = postprocess_algcoef(av,alist,cand);
                   4811:     MKLIST(*rp,cand);
                   4812:     if ( nd_gentrace ) {
                   4813:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   4814:     tl3 = reverse_node(tl3);
                   4815:     /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   4816:         for ( t = tl2; t; t = NEXT(t) ) {
                   4817:       /* s = [i,[*,j,*,*],...] */
                   4818:             s = BDY((LIST)BDY(t));
1.6       noro     4819:             j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     4820:       for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     4821:                 j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     4822:         ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   4823:             }
                   4824:     }
                   4825:     for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6       noro     4826:         STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     4827:     }
                   4828:         MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   4829:     MKLIST(l5,tl4);
1.6       noro     4830:       STOZ(nd_bpe,bpe);
1.15      noro     4831:         tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     4832:     }
                   4833: }
                   4834:
                   4835: /* XXX : module element is not considered  */
                   4836:
                   4837: void dltondl(int n,DL dl,UINT *r)
                   4838: {
                   4839:     UINT *d;
                   4840:     int i,j,l,s,ord_l;
                   4841:     struct order_pair *op;
                   4842:
                   4843:     d = (unsigned int *)dl->d;
                   4844:     for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
                   4845:     if ( nd_blockmask ) {
                   4846:         l = nd_blockmask->n;
                   4847:         op = nd_blockmask->order_pair;
                   4848:         for ( j = 0, s = 0; j < l; j++ ) {
                   4849:             ord_l = op[j].length;
                   4850:             for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
                   4851:         }
                   4852:         TD(r) = ndl_weight(r);
                   4853:         ndl_weight_mask(r);
                   4854:     } else {
                   4855:         for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
                   4856:         TD(r) = ndl_weight(r);
                   4857:     }
                   4858: }
                   4859:
                   4860: DL ndltodl(int n,UINT *ndl)
                   4861: {
                   4862:     DL dl;
                   4863:     int *d;
                   4864:     int i,j,l,s,ord_l;
                   4865:     struct order_pair *op;
                   4866:
                   4867:     NEWDL(dl,n);
                   4868:     dl->td = TD(ndl);
                   4869:     d = dl->d;
                   4870:     if ( nd_blockmask ) {
                   4871:         l = nd_blockmask->n;
                   4872:         op = nd_blockmask->order_pair;
                   4873:         for ( j = 0, s = 0; j < l; j++ ) {
                   4874:             ord_l = op[j].length;
                   4875:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   4876:         }
                   4877:     } else {
                   4878:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   4879:     }
                   4880:     return dl;
                   4881: }
                   4882:
1.24      noro     4883: void _ndltodl(UINT *ndl,DL dl)
                   4884: {
                   4885:     int *d;
                   4886:     int i,j,l,s,ord_l,n;
                   4887:     struct order_pair *op;
                   4888:
                   4889:     n = nd_nvar;
                   4890:     dl->td = TD(ndl);
                   4891:     d = dl->d;
                   4892:     if ( nd_blockmask ) {
                   4893:         l = nd_blockmask->n;
                   4894:         op = nd_blockmask->order_pair;
                   4895:         for ( j = 0, s = 0; j < l; j++ ) {
                   4896:             ord_l = op[j].length;
                   4897:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   4898:         }
                   4899:     } else {
                   4900:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   4901:     }
                   4902: }
                   4903:
1.1       noro     4904: void nmtodp(int mod,NM m,DP *r)
                   4905: {
                   4906:     DP dp;
                   4907:     MP mr;
                   4908:
                   4909:     NEWMP(mr);
                   4910:     mr->dl = ndltodl(nd_nvar,DL(m));
                   4911:     mr->c = (Obj)ndctop(mod,m->c);
                   4912:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   4913:     *r = dp;
                   4914: }
                   4915:
1.15      noro     4916: void ndltodp(UINT *d,DP *r)
                   4917: {
                   4918:     DP dp;
                   4919:     MP mr;
                   4920:
                   4921:     NEWMP(mr);
                   4922:     mr->dl = ndltodl(nd_nvar,d);
                   4923:     mr->c = (Obj)ONE;
                   4924:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   4925:     *r = dp;
                   4926: }
                   4927:
1.1       noro     4928: void ndl_print(UINT *dl)
                   4929: {
                   4930:     int n;
                   4931:     int i,j,l,ord_l,s,s0;
                   4932:     struct order_pair *op;
                   4933:
                   4934:     n = nd_nvar;
                   4935:     printf("<<");
                   4936:     if ( nd_blockmask ) {
                   4937:         l = nd_blockmask->n;
                   4938:         op = nd_blockmask->order_pair;
                   4939:         for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   4940:             ord_l = op[j].length;
                   4941:             for ( i = 0; i < ord_l; i++, s++ )
                   4942:                 printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
                   4943:         }
                   4944:     } else {
                   4945:         for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
                   4946:     }
                   4947:     printf(">>");
                   4948:     if ( nd_module && MPOS(dl) )
                   4949:         printf("*e%d",MPOS(dl));
                   4950: }
                   4951:
                   4952: void nd_print(ND p)
                   4953: {
                   4954:     NM m;
                   4955:
                   4956:     if ( !p )
                   4957:         printf("0\n");
                   4958:     else {
                   4959:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   4960:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     4961:             else printf("+%ld*",CM(m));
1.1       noro     4962:             ndl_print(DL(m));
                   4963:         }
                   4964:         printf("\n");
                   4965:     }
                   4966: }
                   4967:
                   4968: void nd_print_q(ND p)
                   4969: {
                   4970:     NM m;
                   4971:
                   4972:     if ( !p )
                   4973:         printf("0\n");
                   4974:     else {
                   4975:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   4976:             printf("+");
1.6       noro     4977:             printexpr(CO,(Obj)CZ(m));
1.1       noro     4978:             printf("*");
                   4979:             ndl_print(DL(m));
                   4980:         }
                   4981:         printf("\n");
                   4982:     }
                   4983: }
                   4984:
                   4985: void ndp_print(ND_pairs d)
                   4986: {
                   4987:     ND_pairs t;
                   4988:
                   4989:     for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
                   4990:     printf("\n");
                   4991: }
                   4992:
                   4993: void nd_removecont(int mod,ND p)
                   4994: {
                   4995:     int i,n;
                   4996:     Z *w;
                   4997:     NM m;
                   4998:     struct oVECT v;
                   4999:
                   5000:     if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
                   5001:     else if ( mod == -2 ) {
                   5002:       Z inv;
                   5003:       divlf(ONE,HCZ(p),&inv);
                   5004:       nd_mul_c_lf(p,inv);
                   5005:     } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
                   5006:     else {
                   5007:         for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   5008:         w = (Z *)MALLOC(n*sizeof(Q));
                   5009:         v.len = n;
                   5010:         v.body = (pointer *)w;
1.6       noro     5011:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5012:         removecont_array((P *)w,n,1);
1.6       noro     5013:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5014:     }
                   5015: }
                   5016:
                   5017: void nd_removecont2(ND p1,ND p2)
                   5018: {
                   5019:     int i,n1,n2,n;
                   5020:     Z *w;
                   5021:     NM m;
                   5022:     struct oVECT v;
                   5023:
                   5024:     n1 = nd_length(p1);
                   5025:     n2 = nd_length(p2);
                   5026:     n = n1+n2;
                   5027:     w = (Z *)MALLOC(n*sizeof(Q));
                   5028:     v.len = n;
                   5029:     v.body = (pointer *)w;
                   5030:     i = 0;
                   5031:     if ( p1 )
1.6       noro     5032:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5033:     if ( p2 )
1.6       noro     5034:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5035:     removecont_array((P *)w,n,1);
                   5036:     i = 0;
                   5037:     if ( p1 )
1.6       noro     5038:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5039:     if ( p2 )
1.6       noro     5040:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5041: }
                   5042:
                   5043: void ndv_removecont(int mod,NDV p)
                   5044: {
                   5045:     int i,len,all_p;
                   5046:     Z *c;
                   5047:     P *w;
                   5048:     Z dvr,t;
                   5049:     P g,cont,tp;
                   5050:     NMV m;
                   5051:
                   5052:     if ( mod == -1 )
                   5053:         ndv_mul_c(mod,p,_invsf(HCM(p)));
                   5054:     else if ( mod == -2 ) {
                   5055:       Z inv;
                   5056:       divlf(ONE,HCZ(p),&inv);
                   5057:       ndv_mul_c_lf(p,inv);
                   5058:     } else if ( mod )
                   5059:         ndv_mul_c(mod,p,invm(HCM(p),mod));
                   5060:     else {
                   5061:         len = p->len;
                   5062:         w = (P *)MALLOC(len*sizeof(P));
                   5063:         c = (Z *)MALLOC(len*sizeof(Q));
                   5064:         for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
                   5065:             ptozp(CP(m),1,(Q *)&c[i],&w[i]);
                   5066:             all_p = all_p && !NUM(w[i]);
                   5067:         }
                   5068:         if ( all_p ) {
                   5069:             qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
                   5070:             mulp(nd_vc,(P)dvr,g,&cont);
                   5071:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5072:                 divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
                   5073:             }
                   5074:         } else {
                   5075:             sortbynm((Q *)c,len);
                   5076:             qltozl((Q *)c,len,&dvr);
                   5077:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5078:                 divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
                   5079:             }
                   5080:         }
                   5081:     }
                   5082: }
                   5083:
                   5084: /* koko */
                   5085:
                   5086: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
                   5087: {
                   5088:     int len,i,max;
                   5089:     NMV m,mr0,mr,t;
                   5090:
                   5091:     len = p->len;
1.14      noro     5092:     for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1       noro     5093:         max = MAX(max,TD(DL(m)));
                   5094:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   5095:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   5096:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   5097:     t = (NMV)MALLOC(nmv_adv);
                   5098:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   5099:         ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6       noro     5100:         CZ(mr) = CZ(m);
1.1       noro     5101:         ndl_copy(DL(t),DL(mr));
                   5102:     }
                   5103:     NV(p)++;
                   5104:     BDY(p) = mr0;
                   5105: }
                   5106:
                   5107: void ndv_dehomogenize(NDV p,struct order_spec *ord)
                   5108: {
                   5109:     int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
                   5110:     int pos;
                   5111:     Q *w;
                   5112:     Q dvr,t;
                   5113:     NMV m,r;
                   5114:
                   5115:     len = p->len;
                   5116:     newnvar = nd_nvar-1;
                   5117:     newexporigin = nd_get_exporigin(ord);
                   5118:     if ( nd_module ) newmpos = newexporigin-1;
                   5119:     newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
                   5120:     for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
                   5121:         ndl_dehomogenize(DL(m));
                   5122:     if ( newwpd != nd_wpd ) {
                   5123:         newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
                   5124:         for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6       noro     5125:             CZ(r) = CZ(m);
1.1       noro     5126:             if ( nd_module ) pos = MPOS(DL(m));
                   5127:             for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   5128:             adj = nd_exporigin-newexporigin;
                   5129:             for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
                   5130:             if ( nd_module ) {
                   5131:                 DL(r)[newmpos] = pos;
                   5132:             }
                   5133:         }
                   5134:     }
                   5135:     NV(p)--;
                   5136: }
                   5137:
                   5138: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
                   5139: {
                   5140:     int i;
                   5141:     P *tpl,*tpl1;
                   5142:     NODE l;
                   5143:     P h,gcd,t;
                   5144:
                   5145:     tpl = (P *)MALLOC(m*sizeof(P));
                   5146:     tpl1 = (P *)MALLOC(m*sizeof(P));
                   5147:     bcopy(pl,tpl,m*sizeof(P));
                   5148:     gcd = (P)ONE;
                   5149:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   5150:         h = (P)BDY(l);
                   5151:         while ( 1 ) {
                   5152:             for ( i = 0; i < m; i++ )
                   5153:                 if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
                   5154:                     break;
                   5155:             if ( i == m ) {
                   5156:                 bcopy(tpl1,tpl,m*sizeof(P));
                   5157:                 mulp(vl,gcd,h,&t); gcd = t;
                   5158:             } else
                   5159:                 break;
                   5160:         }
                   5161:     }
                   5162:     if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
                   5163:     if ( full ) {
                   5164:         heu_nezgcdnpz(vl,tpl,m,&t);
                   5165:         mulp(vl,gcd,t,pr);
                   5166:     } else
                   5167:         *pr = gcd;
                   5168: }
                   5169:
                   5170: void removecont_array(P *p,int n,int full)
                   5171: {
                   5172:     int all_p,all_q,i;
                   5173:     Z *c;
                   5174:     P *w;
                   5175:     P t,s;
                   5176:
                   5177:     for ( all_q = 1, i = 0; i < n; i++ )
                   5178:         all_q = all_q && NUM(p[i]);
                   5179:     if ( all_q ) {
                   5180:         removecont_array_q((Z *)p,n);
                   5181:     } else {
                   5182:         c = (Z *)MALLOC(n*sizeof(Z));
                   5183:         w = (P *)MALLOC(n*sizeof(P));
                   5184:         for ( i = 0; i < n; i++ ) {
                   5185:             ptozp(p[i],1,(Q *)&c[i],&w[i]);
                   5186:         }
                   5187:         removecont_array_q(c,n);
                   5188:         nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
                   5189:         for ( i = 0; i < n; i++ ) {
                   5190:             divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
                   5191:         }
                   5192:     }
                   5193: }
                   5194:
                   5195: /* c is an int array */
                   5196:
                   5197: void removecont_array_q(Z *c,int n)
                   5198: {
                   5199:   struct oVECT v;
                   5200:   Z d0,d1,a,u,u1,gcd;
                   5201:   int i,j;
                   5202:   Z *q,*r;
                   5203:
                   5204:   q = (Z *)MALLOC(n*sizeof(Z));
                   5205:   r = (Z *)MALLOC(n*sizeof(Z));
                   5206:   v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   5207:   gcdvz_estimate(&v,&d0);
                   5208:   for ( i = 0; i < n; i++ ) {
                   5209:     divqrz(c[i],d0,&q[i],&r[i]);
                   5210:   }
                   5211:   for ( i = 0; i < n; i++ ) if ( r[i] ) break;
                   5212:   if ( i < n ) {
                   5213:     v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   5214:     gcdvz(&v,&d1);
                   5215:     gcdz(d0,d1,&gcd);
1.6       noro     5216:     /* exact division */
                   5217:     divsz(d0,gcd,&a);
1.1       noro     5218:     for ( i = 0; i < n; i++ ) {
                   5219:       mulz(a,q[i],&u);
                   5220:       if ( r[i] ) {
1.6       noro     5221:         /* exact division */
                   5222:         divsz(r[i],gcd,&u1);
1.1       noro     5223:         addz(u,u1,&q[i]);
                   5224:       } else
                   5225:         q[i] = u;
                   5226:     }
                   5227:   }
                   5228:   for ( i = 0; i < n; i++ ) c[i] = q[i];
                   5229: }
                   5230:
1.4       noro     5231: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
                   5232:
                   5233: void mpz_removecont_array(mpz_t *c,int n)
                   5234: {
                   5235:   mpz_t d0,a,u,u1,gcd;
                   5236:   int i,j;
1.13      noro     5237:   static mpz_t *q,*r;
                   5238:   static int c_len = 0;
1.4       noro     5239:
                   5240:   for ( i = 0; i < n; i++ )
                   5241:     if ( mpz_sgn(c[i]) ) break;
                   5242:   if ( i == n ) return;
                   5243:   gcdv_mpz_estimate(d0,c,n);
1.13      noro     5244:   if ( n > c_len ) {
                   5245:     q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   5246:     r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   5247:     c_len = n;
                   5248:   }
1.4       noro     5249:   for ( i = 0; i < n; i++ ) {
                   5250:     mpz_init(q[i]); mpz_init(r[i]);
                   5251:     mpz_fdiv_qr(q[i],r[i],c[i],d0);
                   5252:   }
                   5253:   for ( i = 0; i < n; i++ )
                   5254:     if ( mpz_sgn(r[i]) ) break;
                   5255:   mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
                   5256:   if ( i < n ) {
                   5257:     mpz_gcd(gcd,d0,r[i]);
                   5258:     for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
                   5259:     mpz_div(a,d0,gcd);
                   5260:     for ( i = 0; i < n; i++ ) {
                   5261:       mpz_mul(u,a,q[i]);
                   5262:       if ( mpz_sgn(r[i]) ) {
                   5263:         mpz_div(u1,r[i],gcd);
                   5264:         mpz_add(q[i],u,u1);
                   5265:       } else
                   5266:         mpz_set(q[i],u);
                   5267:     }
                   5268:   }
                   5269:   for ( i = 0; i < n; i++ )
                   5270:     mpz_set(c[i],q[i]);
                   5271: }
                   5272:
1.1       noro     5273: void nd_mul_c(int mod,ND p,int mul)
                   5274: {
                   5275:     NM m;
                   5276:     int c,c1;
                   5277:
                   5278:     if ( !p ) return;
                   5279:     if ( mul == 1 ) return;
                   5280:     if ( mod == -1 )
                   5281:         for ( m = BDY(p); m; m = NEXT(m) )
                   5282:             CM(m) = _mulsf(CM(m),mul);
                   5283:     else
                   5284:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5285:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   5286:         }
                   5287: }
                   5288:
                   5289: void nd_mul_c_lf(ND p,Z mul)
                   5290: {
                   5291:     NM m;
                   5292:     Z c;
                   5293:
                   5294:     if ( !p ) return;
                   5295:     if ( UNIZ(mul) ) return;
                   5296:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   5297:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   5298:     }
                   5299: }
                   5300:
                   5301: void nd_mul_c_q(ND p,P mul)
                   5302: {
                   5303:     NM m;
                   5304:     P c;
                   5305:
                   5306:     if ( !p ) return;
                   5307:     if ( UNIQ(mul) ) return;
                   5308:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   5309:         mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
                   5310:     }
                   5311: }
                   5312:
                   5313: void nd_mul_c_p(VL vl,ND p,P mul)
                   5314: {
                   5315:     NM m;
                   5316:     P c;
                   5317:
                   5318:     if ( !p ) return;
                   5319:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   5320:         mulp(vl,CP(m),mul,&c); CP(m) = c;
                   5321:     }
                   5322: }
                   5323:
                   5324: void nd_free(ND p)
                   5325: {
                   5326:     NM t,s;
                   5327:
                   5328:     if ( !p ) return;
                   5329:     t = BDY(p);
                   5330:     while ( t ) {
                   5331:         s = NEXT(t);
                   5332:         FREENM(t);
                   5333:         t = s;
                   5334:     }
                   5335:     FREEND(p);
                   5336: }
                   5337:
                   5338: void ndv_free(NDV p)
                   5339: {
                   5340:     GCFREE(BDY(p));
                   5341: }
                   5342:
                   5343: void nd_append_red(UINT *d,int i)
                   5344: {
                   5345:     RHist m,m0;
                   5346:     int h;
                   5347:
                   5348:     NEWRHist(m);
                   5349:     h = ndl_hash_value(d);
                   5350:     m->index = i;
                   5351:     ndl_copy(d,DL(m));
                   5352:     NEXT(m) = nd_red[h];
                   5353:     nd_red[h] = m;
                   5354: }
                   5355:
                   5356: UINT *ndv_compute_bound(NDV p)
                   5357: {
                   5358:     UINT *d1,*d2,*t;
                   5359:     UINT u;
                   5360:     int i,j,k,l,len,ind;
                   5361:     NMV m;
                   5362:
                   5363:     if ( !p )
                   5364:         return 0;
                   5365:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5366:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5367:     len = LEN(p);
                   5368:     m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   5369:     for ( i = 1; i < len; i++, NMV_ADV(m) ) {
                   5370:         ndl_max(DL(m),d1,d2);
                   5371:         t = d1; d1 = d2; d2 = t;
                   5372:     }
                   5373:     l = nd_nvar+31;
                   5374:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   5375:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   5376:         u = d1[i];
                   5377:         k = (nd_epw-1)*nd_bpe;
                   5378:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   5379:             t[ind] = (u>>k)&nd_mask0;
                   5380:     }
                   5381:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   5382:     return t;
                   5383: }
                   5384:
                   5385: UINT *nd_compute_bound(ND p)
                   5386: {
                   5387:     UINT *d1,*d2,*t;
                   5388:     UINT u;
                   5389:     int i,j,k,l,len,ind;
                   5390:     NM m;
                   5391:
                   5392:     if ( !p )
                   5393:         return 0;
                   5394:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5395:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5396:     len = LEN(p);
                   5397:     m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   5398:     for ( m = NEXT(m); m; m = NEXT(m) ) {
                   5399:         ndl_lcm(DL(m),d1,d2);
                   5400:         t = d1; d1 = d2; d2 = t;
                   5401:     }
                   5402:     l = nd_nvar+31;
                   5403:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   5404:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   5405:         u = d1[i];
                   5406:         k = (nd_epw-1)*nd_bpe;
                   5407:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   5408:             t[ind] = (u>>k)&nd_mask0;
                   5409:     }
                   5410:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   5411:     return t;
                   5412: }
                   5413:
                   5414: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
                   5415: /* of a term. In this case we need additional 1 word. */
                   5416:
                   5417: int nd_get_exporigin(struct order_spec *ord)
                   5418: {
                   5419:     switch ( ord->id ) {
1.21      noro     5420:         case 0: case 2: case 256: case 258: case 300:
1.1       noro     5421:             return 1+nd_module;
                   5422:         case 1: case 257:
                   5423:             /* block order */
                   5424:             /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
                   5425:             /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
                   5426:             return ord->ord.block.length+1+nd_module;
                   5427:         case 3: case 259:
                   5428: #if 0
                   5429:             error("nd_get_exporigin : composite order is not supported yet.");
                   5430: #else
                   5431:             return 1+nd_module;
                   5432: #endif
                   5433:         default:
                   5434:             error("nd_get_exporigin : ivalid argument.");
                   5435:             return 0;
                   5436:     }
                   5437: }
                   5438:
                   5439: void nd_setup_parameters(int nvar,int max) {
                   5440:     int i,j,n,elen,ord_o,ord_l,l,s,wpd;
                   5441:     struct order_pair *op;
                   5442:
                   5443:     nd_nvar = nvar;
                   5444:     if ( max ) {
                   5445:         /* XXX */
                   5446:         if ( do_weyl ) nd_bpe = 32;
                   5447:         else if ( max < 2 ) nd_bpe = 1;
                   5448:         else if ( max < 4 ) nd_bpe = 2;
                   5449:         else if ( max < 8 ) nd_bpe = 3;
                   5450:         else if ( max < 16 ) nd_bpe = 4;
                   5451:         else if ( max < 32 ) nd_bpe = 5;
                   5452:         else if ( max < 64 ) nd_bpe = 6;
                   5453:         else if ( max < 256 ) nd_bpe = 8;
                   5454:         else if ( max < 1024 ) nd_bpe = 10;
                   5455:         else if ( max < 65536 ) nd_bpe = 16;
                   5456:         else nd_bpe = 32;
                   5457:     }
                   5458:     if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
                   5459:         UINT t;
                   5460:     int st;
                   5461:         int *v;
                   5462:   /* t = max(weights) */
                   5463:         t = 0;
                   5464:         if ( current_dl_weight_vector )
                   5465:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                   5466:                 if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
                   5467:                 if ( t < st ) t = st;
                   5468:             }
                   5469:         if ( nd_matrix )
                   5470:             for ( i = 0; i < nd_matrix_len; i++ )
                   5471:                 for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
                   5472:                     if ( (st=v[j]) < 0 ) st = -st;
                   5473:                     if ( t < st ) t = st;
                   5474:         }
                   5475:         /* i = bitsize of t */
                   5476:         for ( i = 0; t; t >>=1, i++ );
                   5477:         /* i += bitsize of nd_nvar */
                   5478:         for ( t = nd_nvar; t; t >>=1, i++);
                   5479:         /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
                   5480:         if ( (nd_bpe+i) >= 31 )
                   5481:             error("nd_setup_parameters : too large weight");
                   5482:     }
                   5483:     nd_epw = (sizeof(UINT)*8)/nd_bpe;
                   5484:     elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   5485:     nd_exporigin = nd_get_exporigin(nd_ord);
                   5486:     wpd = nd_exporigin+elen;
                   5487:     if ( nd_module )
                   5488:         nd_mpos = nd_exporigin-1;
                   5489:     else
                   5490:         nd_mpos = -1;
                   5491:     if ( wpd != nd_wpd ) {
                   5492:         nd_free_private_storage();
                   5493:         nd_wpd = wpd;
                   5494:     }
                   5495:     if ( nd_bpe < 32 ) {
                   5496:         nd_mask0 = (1<<nd_bpe)-1;
                   5497:     } else {
                   5498:         nd_mask0 = 0xffffffff;
                   5499:     }
                   5500:     bzero(nd_mask,sizeof(nd_mask));
                   5501:     nd_mask1 = 0;
                   5502:     for ( i = 0; i < nd_epw; i++ ) {
                   5503:         nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   5504:         nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   5505:     }
                   5506:     nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
                   5507:     nd_epos = nd_create_epos(nd_ord);
                   5508:     nd_blockmask = nd_create_blockmask(nd_ord);
                   5509:     nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
                   5510: }
                   5511:
                   5512: ND_pairs nd_reconstruct(int trace,ND_pairs d)
                   5513: {
                   5514:     int i,obpe,oadv,h;
                   5515:     static NM prev_nm_free_list;
                   5516:     static ND_pairs prev_ndp_free_list;
                   5517:     RHist mr0,mr;
                   5518:     RHist r;
                   5519:     RHist *old_red;
                   5520:     ND_pairs s0,s,t;
                   5521:     EPOS oepos;
                   5522:
                   5523:     obpe = nd_bpe;
                   5524:     oadv = nmv_adv;
                   5525:     oepos = nd_epos;
                   5526:     if ( obpe < 2 ) nd_bpe = 2;
                   5527:     else if ( obpe < 3 ) nd_bpe = 3;
                   5528:     else if ( obpe < 4 ) nd_bpe = 4;
                   5529:     else if ( obpe < 5 ) nd_bpe = 5;
                   5530:     else if ( obpe < 6 ) nd_bpe = 6;
                   5531:     else if ( obpe < 8 ) nd_bpe = 8;
                   5532:     else if ( obpe < 10 ) nd_bpe = 10;
                   5533:     else if ( obpe < 16 ) nd_bpe = 16;
                   5534:     else if ( obpe < 32 ) nd_bpe = 32;
                   5535:     else error("nd_reconstruct : exponent too large");
                   5536:
                   5537:     nd_setup_parameters(nd_nvar,0);
                   5538:     prev_nm_free_list = _nm_free_list;
                   5539:     prev_ndp_free_list = _ndp_free_list;
                   5540:     _nm_free_list = 0;
                   5541:     _ndp_free_list = 0;
                   5542:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   5543:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   5544:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   5545:     }
                   5546:     if ( trace )
                   5547:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   5548:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   5549:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   5550:         }
                   5551:     s0 = 0;
                   5552:     for ( t = d; t; t = NEXT(t) ) {
                   5553:         NEXTND_pairs(s0,s);
                   5554:         s->i1 = t->i1;
                   5555:         s->i2 = t->i2;
1.24      noro     5556:         s->sig = t->sig;
1.1       noro     5557:         SG(s) = SG(t);
                   5558:         ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   5559:     }
                   5560:
                   5561:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   5562:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   5563:         old_red[i] = nd_red[i];
                   5564:         nd_red[i] = 0;
                   5565:     }
                   5566:     for ( i = 0; i < REDTAB_LEN; i++ )
                   5567:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   5568:             NEWRHist(mr);
                   5569:             mr->index = r->index;
                   5570:             SG(mr) = SG(r);
                   5571:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   5572:             h = ndl_hash_value(DL(mr));
                   5573:             NEXT(mr) = nd_red[h];
                   5574:             nd_red[h] = mr;
1.24      noro     5575:             mr->sig = r->sig;
1.1       noro     5576:         }
                   5577:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   5578:     old_red = 0;
                   5579:     for ( i = 0; i < nd_psn; i++ ) {
                   5580:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   5581:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24      noro     5582:         r->sig = nd_psh[i]->sig;
1.1       noro     5583:         nd_psh[i] = r;
                   5584:     }
                   5585:     if ( s0 ) NEXT(s) = 0;
                   5586:     prev_nm_free_list = 0;
                   5587:     prev_ndp_free_list = 0;
                   5588: #if 0
                   5589:     GC_gcollect();
                   5590: #endif
                   5591:     return s0;
                   5592: }
                   5593:
1.30      noro     5594: void nd_reconstruct_s(int trace,ND_pairs *d)
                   5595: {
                   5596:     int i,obpe,oadv,h;
                   5597:     static NM prev_nm_free_list;
                   5598:     static ND_pairs prev_ndp_free_list;
                   5599:     RHist mr0,mr;
                   5600:     RHist r;
                   5601:     RHist *old_red;
                   5602:     ND_pairs s0,s,t;
                   5603:     EPOS oepos;
                   5604:
                   5605:     obpe = nd_bpe;
                   5606:     oadv = nmv_adv;
                   5607:     oepos = nd_epos;
                   5608:     if ( obpe < 2 ) nd_bpe = 2;
                   5609:     else if ( obpe < 3 ) nd_bpe = 3;
                   5610:     else if ( obpe < 4 ) nd_bpe = 4;
                   5611:     else if ( obpe < 5 ) nd_bpe = 5;
                   5612:     else if ( obpe < 6 ) nd_bpe = 6;
                   5613:     else if ( obpe < 8 ) nd_bpe = 8;
                   5614:     else if ( obpe < 10 ) nd_bpe = 10;
                   5615:     else if ( obpe < 16 ) nd_bpe = 16;
                   5616:     else if ( obpe < 32 ) nd_bpe = 32;
                   5617:     else error("nd_reconstruct_s : exponent too large");
                   5618:
                   5619:     nd_setup_parameters(nd_nvar,0);
                   5620:     prev_nm_free_list = _nm_free_list;
                   5621:     prev_ndp_free_list = _ndp_free_list;
                   5622:     _nm_free_list = 0;
                   5623:     _ndp_free_list = 0;
                   5624:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   5625:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   5626:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   5627:     }
                   5628:     if ( trace )
                   5629:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   5630:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   5631:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   5632:         }
                   5633:
                   5634:     for ( i = 0; i < nd_nbase; i++ ) {
                   5635:       s0 = 0;
                   5636:       for ( t = d[i]; t; t = NEXT(t) ) {
                   5637:           NEXTND_pairs(s0,s);
                   5638:           s->i1 = t->i1;
                   5639:           s->i2 = t->i2;
                   5640:           s->sig = t->sig;
                   5641:           SG(s) = SG(t);
                   5642:           ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   5643:       }
                   5644:       d[i] = s0;
                   5645:     }
                   5646:
                   5647:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   5648:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   5649:         old_red[i] = nd_red[i];
                   5650:         nd_red[i] = 0;
                   5651:     }
                   5652:     for ( i = 0; i < REDTAB_LEN; i++ )
                   5653:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   5654:             NEWRHist(mr);
                   5655:             mr->index = r->index;
                   5656:             SG(mr) = SG(r);
                   5657:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   5658:             h = ndl_hash_value(DL(mr));
                   5659:             NEXT(mr) = nd_red[h];
                   5660:             nd_red[h] = mr;
                   5661:             mr->sig = r->sig;
                   5662:         }
                   5663:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   5664:     old_red = 0;
                   5665:     for ( i = 0; i < nd_psn; i++ ) {
                   5666:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   5667:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
                   5668:         r->sig = nd_psh[i]->sig;
                   5669:         nd_psh[i] = r;
                   5670:     }
                   5671:     if ( s0 ) NEXT(s) = 0;
                   5672:     prev_nm_free_list = 0;
                   5673:     prev_ndp_free_list = 0;
                   5674: #if 0
                   5675:     GC_gcollect();
                   5676: #endif
                   5677: }
                   5678:
1.1       noro     5679: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
                   5680: {
                   5681:     int n,i,ei,oepw,omask0,j,s,ord_l,l;
                   5682:     struct order_pair *op;
                   5683:
                   5684:     n = nd_nvar;
                   5685:     oepw = (sizeof(UINT)*8)/obpe;
                   5686:     omask0 = (1<<obpe)-1;
                   5687:     TD(r) = TD(d);
                   5688:     for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
                   5689:     if ( nd_blockmask ) {
                   5690:         l = nd_blockmask->n;
                   5691:         op = nd_blockmask->order_pair;
                   5692:         for ( i = 1; i < nd_exporigin; i++ )
                   5693:             r[i] = d[i];
                   5694:         for ( j = 0, s = 0; j < l; j++ ) {
                   5695:             ord_l = op[j].length;
                   5696:             for ( i = 0; i < ord_l; i++, s++ ) {
                   5697:                 ei =  GET_EXP_OLD(d,s);
                   5698:                 PUT_EXP(r,s,ei);
                   5699:             }
                   5700:         }
                   5701:     } else {
                   5702:         for ( i = 0; i < n; i++ ) {
                   5703:             ei = GET_EXP_OLD(d,i);
                   5704:             PUT_EXP(r,i,ei);
                   5705:         }
                   5706:     }
                   5707:     if ( nd_module ) MPOS(r) = MPOS(d);
                   5708: }
                   5709:
                   5710: ND nd_copy(ND p)
                   5711: {
                   5712:     NM m,mr,mr0;
                   5713:     int c,n;
                   5714:     ND r;
                   5715:
                   5716:     if ( !p )
                   5717:         return 0;
                   5718:     else {
                   5719:         for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   5720:             NEXTNM(mr0,mr);
                   5721:             CM(mr) = CM(m);
                   5722:             ndl_copy(DL(m),DL(mr));
                   5723:         }
                   5724:         NEXT(mr) = 0;
                   5725:         MKND(NV(p),mr0,LEN(p),r);
                   5726:         SG(r) = SG(p);
                   5727:         return r;
                   5728:     }
                   5729: }
                   5730:
                   5731: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
                   5732: {
                   5733:     NM m1,m2;
                   5734:     NDV p1,p2;
                   5735:     ND t1,t2;
                   5736:     UINT *lcm;
                   5737:     P gp,tp;
                   5738:     Z g,t;
                   5739:     Z iq;
                   5740:     int td;
                   5741:     LIST hist;
                   5742:     NODE node;
                   5743:     DP d;
                   5744:
                   5745:     if ( !mod && nd_demand ) {
                   5746:         p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
                   5747:     } else {
                   5748:         if ( trace ) {
                   5749:             p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   5750:         } else {
                   5751:             p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   5752:         }
                   5753:     }
                   5754:     lcm = LCM(p);
                   5755:     NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
                   5756:     if ( ndl_check_bound2(p->i1,DL(m1)) ) {
                   5757:         FREENM(m1); return 0;
                   5758:     }
                   5759:     NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
                   5760:     if ( ndl_check_bound2(p->i2,DL(m2)) ) {
                   5761:         FREENM(m1); FREENM(m2); return 0;
                   5762:     }
                   5763:
                   5764:     if ( mod == -1 ) {
                   5765:         CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
                   5766:     } else if ( mod > 0 ) {
                   5767:         CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
                   5768:     } else if ( mod == -2 ) {
                   5769:         CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
                   5770:     } else if ( nd_vc ) {
                   5771:         ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
                   5772:         divsp(nd_vc,HCP(p2),gp,&CP(m1));
                   5773:         divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
                   5774:     } else {
1.6       noro     5775:         igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1       noro     5776:     }
                   5777:     t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
                   5778:     *rp = nd_add(mod,t1,t2);
                   5779:     if ( nd_gentrace ) {
                   5780:         /* nd_tracelist is initialized */
1.6       noro     5781:         STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     5782:         MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6       noro     5783:         STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     5784:         MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
                   5785:         nd_tracelist = node;
                   5786:     }
1.24      noro     5787:     if ( *rp )
                   5788:       (*rp)->sig = p->sig;
1.1       noro     5789:     FREENM(m1); FREENM(m2);
                   5790:     return 1;
                   5791: }
                   5792:
                   5793: void ndv_mul_c(int mod,NDV p,int mul)
                   5794: {
                   5795:     NMV m;
                   5796:     int c,c1,len,i;
                   5797:
                   5798:     if ( !p ) return;
                   5799:     len = LEN(p);
                   5800:     if ( mod == -1 )
                   5801:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   5802:             CM(m) = _mulsf(CM(m),mul);
                   5803:     else
                   5804:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   5805:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   5806:         }
                   5807: }
                   5808:
                   5809: void ndv_mul_c_lf(NDV p,Z mul)
                   5810: {
                   5811:     NMV m;
                   5812:     Z c;
                   5813:     int len,i;
                   5814:
                   5815:     if ( !p ) return;
                   5816:     len = LEN(p);
                   5817:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   5818:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   5819:     }
                   5820: }
                   5821:
                   5822: /* for nd_det */
                   5823: void ndv_mul_c_q(NDV p,Z mul)
                   5824: {
                   5825:     NMV m;
                   5826:     Z c;
                   5827:     int len,i;
                   5828:
                   5829:     if ( !p ) return;
                   5830:     len = LEN(p);
                   5831:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     5832:         mulz(CZ(m),mul,&c); CZ(m) = c;
1.1       noro     5833:     }
                   5834: }
                   5835:
                   5836: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
                   5837:     int n2,i,j,l,n,tlen;
                   5838:     UINT *d0;
                   5839:     NM *tab,*psum;
                   5840:     ND s,r;
                   5841:     NM t;
                   5842:     NMV m1;
                   5843:
                   5844:     if ( !p ) return 0;
                   5845:     n = NV(p); n2 = n>>1;
                   5846:     d0 = DL(m0);
                   5847:     l = LEN(p);
                   5848:     for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   5849:     tab = (NM *)MALLOC(tlen*sizeof(NM));
                   5850:     psum = (NM *)MALLOC(tlen*sizeof(NM));
                   5851:     for ( i = 0; i < tlen; i++ ) psum[i] = 0;
                   5852:     m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   5853:     for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
                   5854:         /* m0(NM) * m1(NMV) => tab(NM) */
                   5855:         weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
                   5856:         for ( j = 0; j < tlen; j++ ) {
                   5857:             if ( tab[j] ) {
                   5858:                 NEXT(tab[j]) = psum[j];    psum[j] = tab[j];
                   5859:             }
                   5860:         }
                   5861:     }
                   5862:     for ( i = tlen-1, r = 0; i >= 0; i-- )
                   5863:         if ( psum[i] ) {
                   5864:             for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   5865:             MKND(n,psum[i],j,s);
                   5866:             r = nd_add(mod,r,s);
                   5867:         }
                   5868:     if ( r ) SG(r) = SG(p)+TD(d0);
                   5869:     return r;
                   5870: }
                   5871:
                   5872: /* product of monomials */
                   5873: /* XXX block order is not handled correctly */
                   5874:
                   5875: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   5876: {
                   5877:     int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
                   5878:     UINT *d0,*d1,*d,*dt,*ctab;
                   5879:     Z *ctab_q;
                   5880:     Z q,q1;
                   5881:     UINT c0,c1,c;
                   5882:     NM *p;
                   5883:     NM m,t;
                   5884:     int mpos;
                   5885:
                   5886:     for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   5887:     if ( !m0 || !m1 ) return;
                   5888:     d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
                   5889:     if ( nd_module )
                   5890:         if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
                   5891:
                   5892:     NEWNM(m); d = DL(m);
                   5893:     if ( mod ) {
                   5894:         c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
                   5895:     } else if ( nd_vc )
                   5896:         mulp(nd_vc,CP(m0),CP(m1),&CP(m));
                   5897:   else
1.6       noro     5898:         mulz(CZ(m0),CZ(m1),&CZ(m));
1.1       noro     5899:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   5900:     homo = n&1 ? 1 : 0;
                   5901:     if ( homo ) {
                   5902:         /* offset of h-degree */
                   5903:         h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   5904:         PUT_EXP(DL(m),n-1,h);
                   5905:         TD(DL(m)) = h;
                   5906:         if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   5907:     }
                   5908:     tab[0] = m;
                   5909:     NEWNM(m); d = DL(m);
                   5910:     for ( i = 0, curlen = 1; i < n2; i++ ) {
                   5911:         a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   5912:         k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   5913:         /* xi^a*(Di^k*xi^l)*Di^b */
                   5914:         a += l; b += k;
                   5915:         s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
                   5916:         if ( !k || !l ) {
                   5917:             for ( j = 0; j < curlen; j++ )
                   5918:                 if ( (t = tab[j]) != 0 ) {
                   5919:                     dt = DL(t);
                   5920:                     PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
                   5921:                     if ( nd_blockmask ) ndl_weight_mask(dt);
                   5922:                 }
                   5923:             curlen *= k+1;
                   5924:             continue;
                   5925:         }
                   5926:         min = MIN(k,l);
                   5927:         if ( mod ) {
                   5928:             ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
                   5929:             mkwcm(k,l,mod,(int *)ctab);
                   5930:         } else {
                   5931:             ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
                   5932:             mkwc(k,l,ctab_q);
                   5933:         }
                   5934:         for ( j = min; j >= 0; j-- ) {
                   5935:             for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
                   5936:             PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
                   5937:             h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
                   5938:             if ( homo ) {
                   5939:                 TD(d) = s;
                   5940:                 PUT_EXP(d,n-1,s-h);
                   5941:             } else TD(d) = h;
                   5942:             if ( nd_blockmask ) ndl_weight_mask(d);
                   5943:             if ( mod ) c = ctab[j];
                   5944:             else q = ctab_q[j];
                   5945:             p = tab+curlen*j;
                   5946:             if ( j == 0 ) {
                   5947:                 for ( u = 0; u < curlen; u++, p++ ) {
                   5948:                     if ( tab[u] ) {
                   5949:                         ndl_addto(DL(tab[u]),d);
                   5950:                         if ( mod ) {
                   5951:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
                   5952:                         } else if ( nd_vc )
                   5953:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
                   5954:             else {
1.6       noro     5955:                             mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1       noro     5956:                         }
                   5957:                     }
                   5958:                 }
                   5959:             } else {
                   5960:                 for ( u = 0; u < curlen; u++, p++ ) {
                   5961:                     if ( tab[u] ) {
                   5962:                         NEWNM(t);
                   5963:                         ndl_add(DL(tab[u]),d,DL(t));
                   5964:                         if ( mod ) {
                   5965:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
                   5966:                         } else if ( nd_vc )
                   5967:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
                   5968:             else
1.6       noro     5969:                             mulz(CZ(tab[u]),q,&CZ(t));
1.1       noro     5970:                         *p = t;
                   5971:                     }
                   5972:                 }
                   5973:             }
                   5974:         }
                   5975:         curlen *= k+1;
                   5976:     }
                   5977:     FREENM(m);
                   5978:     if ( nd_module ) {
                   5979:         mpos = MPOS(d1);
                   5980:         for ( i = 0; i < tlen; i++ )
                   5981:             if ( tab[i] ) {
                   5982:                 d = DL(tab[i]);
                   5983:                 MPOS(d) = mpos;
                   5984:                 TD(d) = ndl_weight(d);
                   5985:             }
                   5986:     }
                   5987: }
                   5988:
                   5989: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   5990: {
                   5991:     NM mr,mr0;
                   5992:     NMV m;
                   5993:     UINT *d,*dt,*dm;
                   5994:     int c,n,td,i,c1,c2,len;
                   5995:     Q q;
                   5996:     ND r;
                   5997:
                   5998:     if ( !p ) return 0;
                   5999:     else {
                   6000:         n = NV(p); m = BDY(p);
                   6001:         d = DL(m0);
                   6002:         len = LEN(p);
                   6003:         mr0 = 0;
                   6004:         td = TD(d);
                   6005:         c = CM(m0);
                   6006:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6007:             NEXTNM(mr0,mr);
                   6008:             CM(mr) = 1;
                   6009:             ndl_add(DL(m),d,DL(mr));
                   6010:         }
                   6011:         NEXT(mr) = 0;
                   6012:         MKND(NV(p),mr0,len,r);
                   6013:         SG(r) = SG(p) + TD(d);
                   6014:         return r;
                   6015:     }
                   6016: }
                   6017:
                   6018: ND ndv_mul_nm(int mod,NM m0,NDV p)
                   6019: {
                   6020:     NM mr,mr0;
                   6021:     NMV m;
                   6022:     UINT *d,*dt,*dm;
                   6023:     int c,n,td,i,c1,c2,len;
                   6024:     P q;
                   6025:     ND r;
                   6026:
                   6027:     if ( !p ) return 0;
                   6028:     else if ( do_weyl ) {
                   6029:         if ( mod < 0 ) {
                   6030:             error("ndv_mul_nm : not implemented (weyl)");
                   6031:             return 0;
                   6032:         } else
                   6033:             return weyl_ndv_mul_nm(mod,m0,p);
                   6034:     } else {
                   6035:         n = NV(p); m = BDY(p);
                   6036:         d = DL(m0);
                   6037:         len = LEN(p);
                   6038:         mr0 = 0;
                   6039:         td = TD(d);
                   6040:         if ( mod == -1 ) {
                   6041:             c = CM(m0);
                   6042:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6043:                 NEXTNM(mr0,mr);
                   6044:                 CM(mr) = _mulsf(CM(m),c);
                   6045:                 ndl_add(DL(m),d,DL(mr));
                   6046:             }
                   6047:         } else if ( mod == -2 ) {
                   6048:             Z cl;
                   6049:             cl = CZ(m0);
                   6050:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6051:                 NEXTNM(mr0,mr);
                   6052:                 mullf(CZ(m),cl,&CZ(mr));
                   6053:                 ndl_add(DL(m),d,DL(mr));
                   6054:             }
                   6055:         } else if ( mod ) {
                   6056:             c = CM(m0);
                   6057:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6058:                 NEXTNM(mr0,mr);
                   6059:                 c1 = CM(m);
                   6060:                 DMAR(c1,c,0,mod,c2);
                   6061:                 CM(mr) = c2;
                   6062:                 ndl_add(DL(m),d,DL(mr));
                   6063:             }
                   6064:         } else {
                   6065:             q = CP(m0);
                   6066:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6067:                 NEXTNM(mr0,mr);
                   6068:                 mulp(nd_vc,CP(m),q,&CP(mr));
                   6069:                 ndl_add(DL(m),d,DL(mr));
                   6070:             }
                   6071:         }
                   6072:         NEXT(mr) = 0;
                   6073:         MKND(NV(p),mr0,len,r);
                   6074:         SG(r) = SG(p) + TD(d);
                   6075:         return r;
                   6076:     }
                   6077: }
                   6078:
                   6079: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
                   6080: {
                   6081:     NM mq0,mq;
                   6082:     NMV tm;
                   6083:     Q q;
                   6084:     int i,nv,sg,c,c1,c2,hindex;
                   6085:     ND p,t,r;
                   6086:
                   6087:     if ( bucket->m < 0 ) return 0;
                   6088:     else {
                   6089:         nv = NV(d);
                   6090:         mq0 = 0;
                   6091:         tm = (NMV)MALLOC(nmv_adv);
                   6092:         while ( 1 ) {
                   6093:             if ( mod > 0 || mod == -1 )
                   6094:               hindex = head_pbucket(mod,bucket);
                   6095:             else if ( mod == -2 )
                   6096:               hindex = head_pbucket_lf(bucket);
                   6097:             else
                   6098:               hindex = head_pbucket_q(bucket);
                   6099:             if ( hindex < 0 ) break;
                   6100:             p = bucket->body[hindex];
                   6101:             NEXTNM(mq0,mq);
                   6102:             ndl_sub(HDL(p),HDL(d),DL(mq));
                   6103:             ndl_copy(DL(mq),DL(tm));
                   6104:             if ( mod ) {
                   6105:                 c1 = invm(HCM(d),mod); c2 = HCM(p);
                   6106:                 DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   6107:                 CM(tm) = mod-c;
                   6108:             } else {
1.6       noro     6109:                 divsz(HCZ(p),HCZ(d),&CZ(mq));
                   6110:                 chsgnz(CZ(mq),&CZ(tm));
1.1       noro     6111:             }
                   6112:             t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
                   6113:             bucket->body[hindex] = nd_remove_head(p);
                   6114:             t = nd_remove_head(t);
                   6115:             add_pbucket(mod,bucket,t);
                   6116:         }
                   6117:         if ( !mq0 )
                   6118:             r = 0;
                   6119:         else {
                   6120:             NEXT(mq) = 0;
                   6121:             for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   6122:             MKND(nv,mq0,i,r);
                   6123:             /* XXX */
                   6124:             SG(r) = HTD(r);
                   6125:         }
                   6126:         return r;
                   6127:     }
                   6128: }
                   6129:
                   6130: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   6131: {
                   6132:     NMV m,mr,mr0,t;
                   6133:     int len,i,k;
                   6134:
                   6135:     if ( !p ) return;
                   6136:     m = BDY(p); len = LEN(p);
                   6137:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   6138:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   6139:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   6140:     t = (NMV)MALLOC(nmv_adv);
                   6141:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6       noro     6142:         CZ(t) = CZ(m);
1.1       noro     6143:         for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   6144:         ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6       noro     6145:         CZ(mr) = CZ(t);
1.1       noro     6146:         ndl_copy(DL(t),DL(mr));
                   6147:     }
                   6148:     BDY(p) = mr0;
                   6149: }
                   6150:
                   6151: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   6152: {
                   6153:     NMV m,mr,mr0;
                   6154:     int len,i;
                   6155:     NDV r;
                   6156:
                   6157:     if ( !p ) return 0;
                   6158:     m = BDY(p); len = LEN(p);
                   6159:     mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   6160:     for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   6161:         ndl_zero(DL(mr));
                   6162:         ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6       noro     6163:         CZ(mr) = CZ(m);
1.1       noro     6164:     }
                   6165:     MKNDV(NV(p),mr0,len,r);
                   6166:     SG(r) = SG(p);
1.24      noro     6167:     r->sig = p->sig;
1.1       noro     6168:     return r;
                   6169: }
                   6170:
                   6171: /* duplicate p */
                   6172:
                   6173: NDV ndv_dup(int mod,NDV p)
                   6174: {
                   6175:     NDV d;
                   6176:     NMV t,m,m0;
                   6177:     int i,len;
                   6178:
                   6179:     if ( !p ) return 0;
                   6180:     len = LEN(p);
                   6181:     m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   6182:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   6183:         ndl_copy(DL(t),DL(m));
1.6       noro     6184:         CZ(m) = CZ(t);
1.1       noro     6185:     }
                   6186:     MKNDV(NV(p),m0,len,d);
                   6187:     SG(d) = SG(p);
                   6188:     return d;
                   6189: }
                   6190:
                   6191: NDV ndv_symbolic(int mod,NDV p)
                   6192: {
                   6193:     NDV d;
                   6194:     NMV t,m,m0;
                   6195:     int i,len;
                   6196:
                   6197:     if ( !p ) return 0;
                   6198:     len = LEN(p);
                   6199:     m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   6200:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   6201:         ndl_copy(DL(t),DL(m));
1.6       noro     6202:         CZ(m) = ONE;
1.1       noro     6203:     }
                   6204:     MKNDV(NV(p),m0,len,d);
                   6205:     SG(d) = SG(p);
                   6206:     return d;
                   6207: }
                   6208:
                   6209: ND nd_dup(ND p)
                   6210: {
                   6211:     ND d;
                   6212:     NM t,m,m0;
                   6213:
                   6214:     if ( !p ) return 0;
                   6215:     for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   6216:         NEXTNM(m0,m);
                   6217:         ndl_copy(DL(t),DL(m));
1.6       noro     6218:         CZ(m) = CZ(t);
1.1       noro     6219:     }
                   6220:     if ( m0 ) NEXT(m) = 0;
                   6221:     MKND(NV(p),m0,LEN(p),d);
                   6222:     SG(d) = SG(p);
                   6223:     return d;
                   6224: }
                   6225:
                   6226: /* XXX if p->len == 0 then it represents 0 */
                   6227:
                   6228: void ndv_mod(int mod,NDV p)
                   6229: {
                   6230:     NMV t,d;
                   6231:     int r,s,u;
                   6232:     int i,len,dlen;
                   6233:     P cp;
                   6234:     Z c;
                   6235:     Obj gfs;
                   6236:
                   6237:     if ( !p ) return;
                   6238:     len = LEN(p);
                   6239:     dlen = 0;
                   6240:     if ( mod == -1 )
                   6241:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   6242:             simp_ff((Obj)CP(t),&gfs);
                   6243:             if ( gfs ) {
                   6244:               r = FTOIF(CONT((GFS)gfs));
                   6245:               CM(d) = r;
                   6246:               ndl_copy(DL(t),DL(d));
                   6247:               NMV_ADV(d);
                   6248:               dlen++;
                   6249:             }
                   6250:         }
                   6251:     else if ( mod == -2 )
                   6252:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   6253:             simp_ff((Obj)CP(t),&gfs);
                   6254:             if ( gfs ) {
                   6255:               lmtolf((LM)gfs,&CZ(d));
                   6256:               ndl_copy(DL(t),DL(d));
                   6257:               NMV_ADV(d);
                   6258:               dlen++;
                   6259:             }
                   6260:         }
                   6261:     else
                   6262:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   6263:             if ( nd_vc ) {
                   6264:                 nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
                   6265:                 c = (Z)cp;
                   6266:             } else
1.6       noro     6267:                 c = CZ(t);
1.1       noro     6268:             r = remqi((Q)c,mod);
                   6269:             if ( r ) {
                   6270:                 CM(d) = r;
                   6271:                 ndl_copy(DL(t),DL(d));
                   6272:                 NMV_ADV(d);
                   6273:                 dlen++;
                   6274:             }
                   6275:         }
                   6276:     LEN(p) = dlen;
                   6277: }
                   6278:
                   6279: NDV ptondv(VL vl,VL dvl,P p)
                   6280: {
                   6281:     ND nd;
                   6282:
                   6283:     nd = ptond(vl,dvl,p);
                   6284:     return ndtondv(0,nd);
                   6285: }
                   6286:
                   6287: void pltozpl(LIST l,Q *cont,LIST *pp)
                   6288: {
1.16      noro     6289:   NODE nd,nd1;
                   6290:   int n;
                   6291:   P *pl;
                   6292:   Q *cl;
                   6293:   int i;
                   6294:   P dmy;
                   6295:   Z dvr,inv;
                   6296:   LIST r;
                   6297:
                   6298:   nd = BDY(l); n = length(nd);
                   6299:   pl = (P *)MALLOC(n*sizeof(P));
                   6300:   cl = (Q *)MALLOC(n*sizeof(Q));
                   6301:   for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
                   6302:     ptozp((P)BDY(nd),1,&cl[i],&dmy);
                   6303:   }
                   6304:   qltozl(cl,n,&dvr);
                   6305:   divz(ONE,dvr,&inv);
                   6306:   nd = BDY(l);
                   6307:   for ( i = 0; i < n; i++, nd = NEXT(nd) )
                   6308:     divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
                   6309:   nd = 0;
                   6310:   for ( i = n-1; i >= 0; i-- ) {
                   6311:     MKNODE(nd1,pl[i],nd); nd = nd1;
                   6312:   }
                   6313:   MKLIST(r,nd);
                   6314:   *pp = r;
1.1       noro     6315: }
                   6316:
                   6317: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
                   6318:
                   6319: NDV pltondv(VL vl,VL dvl,LIST p)
                   6320: {
                   6321:     int i;
                   6322:     NODE t;
                   6323:     ND r,ri;
                   6324:     NM m;
                   6325:
                   6326:     if ( !nd_module ) error("pltond : module order must be set");
                   6327:     r = 0;
                   6328:     for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
                   6329:         ri = ptond(vl,dvl,(P)BDY(t));
                   6330:         if ( ri )
                   6331:             for ( m = BDY(ri); m; m = NEXT(m) ) {
                   6332:                 MPOS(DL(m)) = i;
                   6333:                 TD(DL(m)) = ndl_weight(DL(m));
                   6334:                 if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   6335:             }
                   6336:         r = nd_add(0,r,ri);
                   6337:     }
                   6338:     return ndtondv(0,r);
                   6339: }
                   6340:
                   6341: ND ptond(VL vl,VL dvl,P p)
                   6342: {
                   6343:     int n,i,j,k,e;
                   6344:     VL tvl;
                   6345:     V v;
                   6346:     DCP dc;
                   6347:     DCP *w;
                   6348:     ND r,s,t,u;
                   6349:     P x;
                   6350:     int c;
                   6351:     UINT *d;
                   6352:     NM m,m0;
                   6353:
                   6354:     if ( !p )
                   6355:         return 0;
                   6356:     else if ( NUM(p) ) {
                   6357:         NEWNM(m);
                   6358:         ndl_zero(DL(m));
                   6359:         if ( !INT((Q)p) )
                   6360:           error("ptond : input must be integer-coefficient");
1.6       noro     6361:         CZ(m) = (Z)p;
1.1       noro     6362:         NEXT(m) = 0;
                   6363:         MKND(nd_nvar,m,1,r);
                   6364:         SG(r) = 0;
                   6365:         return r;
                   6366:     } else {
                   6367:         for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   6368:         w = (DCP *)MALLOC(k*sizeof(DCP));
                   6369:         for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   6370:         for ( i = 0, tvl = dvl, v = VR(p);
                   6371:             tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   6372:         if ( !tvl ) {
                   6373:             for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   6374:                 t = ptond(vl,dvl,COEF(w[j]));
                   6375:                 pwrp(vl,x,DEG(w[j]),&p);
                   6376:                 nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   6377:             }
                   6378:             return s;
                   6379:         } else {
                   6380:             NEWNM(m0); d = DL(m0);
                   6381:             for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6       noro     6382:                 ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1       noro     6383:                 TD(d) = MUL_WEIGHT(e,i);
                   6384:                 if ( nd_blockmask) ndl_weight_mask(d);
                   6385:                 if ( nd_module ) MPOS(d) = 0;
                   6386:                 t = ptond(vl,dvl,COEF(w[j]));
                   6387:                 for ( m = BDY(t); m; m = NEXT(m) )
                   6388:                     ndl_addto(DL(m),d);
                   6389:                 SG(t) += TD(d);
                   6390:                 s = nd_add(0,s,t);
                   6391:             }
                   6392:             FREENM(m0);
                   6393:             return s;
                   6394:         }
                   6395:     }
                   6396: }
                   6397:
                   6398: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   6399: {
                   6400:     VL tvl;
                   6401:     int len,n,j,i,e;
                   6402:     NMV m;
                   6403:     Z q;
                   6404:     P c;
                   6405:     UINT *d;
                   6406:     P s,r,u,t,w;
                   6407:     GFS gfs;
                   6408:
                   6409:     if ( !p ) return 0;
                   6410:     else {
                   6411:         len = LEN(p);
                   6412:         n = NV(p);
                   6413:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   6414:         for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   6415:             if ( mod == -1 ) {
                   6416:                e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   6417:             } else if ( mod == -2 ) {
                   6418:                c = (P)CZ(m);
                   6419:             } else if ( mod > 0 ) {
1.6       noro     6420:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     6421:             } else
                   6422:                 c = CP(m);
                   6423:             d = DL(m);
                   6424:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     6425:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     6426:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   6427:             }
                   6428:             addp(vl,s,t,&u); s = u;
                   6429:         }
                   6430:         return s;
                   6431:     }
                   6432: }
                   6433:
                   6434: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
                   6435: {
                   6436:     VL tvl;
                   6437:     int len,n,j,i,e;
                   6438:     NMV m;
                   6439:     Z q;
                   6440:     P c;
                   6441:     UINT *d;
                   6442:     P s,r,u,t,w;
                   6443:     GFS gfs;
                   6444:     P *a;
                   6445:     LIST l;
                   6446:     NODE nd,nd1;
                   6447:
                   6448:     if ( !p ) return 0;
                   6449:     else {
                   6450:         a = (P *)MALLOC((rank+1)*sizeof(P));
                   6451:         for ( i = 0; i <= rank; i++ ) a[i] = 0;
                   6452:         len = LEN(p);
                   6453:         n = NV(p);
                   6454:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   6455:         for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
                   6456:             if ( mod == -1 ) {
                   6457:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   6458:             } else if ( mod ) {
1.6       noro     6459:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     6460:             } else
                   6461:                 c = CP(m);
                   6462:             d = DL(m);
                   6463:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     6464:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     6465:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   6466:             }
                   6467:             addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
                   6468:         }
                   6469:         nd = 0;
                   6470:         for ( i = rank; i > 0; i-- ) {
                   6471:             MKNODE(nd1,a[i],nd); nd = nd1;
                   6472:         }
                   6473:         MKLIST(l,nd);
                   6474:         return l;
                   6475:     }
                   6476: }
                   6477:
                   6478: NDV ndtondv(int mod,ND p)
                   6479: {
                   6480:     NDV d;
                   6481:     NMV m,m0;
                   6482:     NM t;
                   6483:     int i,len;
                   6484:
                   6485:     if ( !p ) return 0;
                   6486:     len = LEN(p);
                   6487:     if ( mod > 0 || mod == -1 )
                   6488:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   6489:     else
                   6490:         m0 = m = MALLOC(len*nmv_adv);
                   6491: #if 0
                   6492:     ndv_alloc += nmv_adv*len;
                   6493: #endif
                   6494:     for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   6495:         ndl_copy(DL(t),DL(m));
1.6       noro     6496:         CZ(m) = CZ(t);
1.1       noro     6497:     }
                   6498:     MKNDV(NV(p),m0,len,d);
                   6499:     SG(d) = SG(p);
1.24      noro     6500:     d->sig = p->sig;
1.1       noro     6501:     return d;
                   6502: }
                   6503:
1.16      noro     6504: static int dmm_comp_nv;
                   6505:
                   6506: int dmm_comp(DMM *a,DMM *b)
                   6507: {
                   6508:    return -compdmm(dmm_comp_nv,*a,*b);
                   6509: }
                   6510:
                   6511: void dmm_sort_by_ord(DMM *a,int len,int nv)
                   6512: {
                   6513:   dmm_comp_nv = nv;
                   6514:   qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
                   6515: }
                   6516:
                   6517: void dpm_sort(DPM p,DPM *rp)
                   6518: {
                   6519:   DMM t,t1;
                   6520:   int len,i,n;
                   6521:   DMM *a;
                   6522:   DPM d;
                   6523:
                   6524:   if ( !p ) *rp = 0;
                   6525:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   6526:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   6527:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   6528:   n = p->nv;
                   6529:   dmm_sort_by_ord(a,len,n);
                   6530:   t = 0;
                   6531:   for ( i = len-1; i >= 0; i-- ) {
                   6532:     NEWDMM(t1);
                   6533:     t1->c = a[i]->c;
                   6534:     t1->dl = a[i]->dl;
                   6535:     t1->pos = a[i]->pos;
                   6536:     t1->next = t;
                   6537:     t = t1;
                   6538:   }
                   6539:   MKDPM(n,t,d);
                   6540:   SG(d) = SG(p);
                   6541:   *rp = d;
                   6542: }
                   6543:
1.18      noro     6544: int dpm_comp(DPM *a,DPM *b)
                   6545: {
1.22      noro     6546:   return -compdpm(CO,*a,*b);
1.18      noro     6547: }
                   6548:
                   6549: NODE dpm_sort_list(NODE l)
                   6550: {
                   6551:   int i,len;
                   6552:   NODE t,t1;
                   6553:   DPM *a;
                   6554:
                   6555:   len = length(l);
                   6556:   a = (DPM *)MALLOC(len*sizeof(DPM));
                   6557:   for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
                   6558:   qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
                   6559:   t = 0;
                   6560:   for ( i = len-1; i >= 0; i-- ) {
                   6561:     MKNODE(t1,(pointer)a[i],t); t = t1;
                   6562:   }
                   6563:   return t;
                   6564: }
                   6565:
1.20      noro     6566: int nmv_comp(NMV a,NMV b)
                   6567: {
1.21      noro     6568:   int t;
                   6569:   t = DL_COMPARE(a->dl,b->dl);
                   6570:   return -t;
1.20      noro     6571: }
                   6572:
1.16      noro     6573: NDV dpmtondv(int mod,DPM p)
                   6574: {
                   6575:   NDV d;
                   6576:   NMV m,m0;
                   6577:   DMM t;
                   6578:   DMM *a;
                   6579:   int i,len,n;
                   6580:
                   6581:   if ( !p ) return 0;
                   6582:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   6583:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   6584:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   6585:   n = p->nv;
                   6586:   dmm_sort_by_ord(a,len,n);
                   6587:   if ( mod > 0 || mod == -1 )
                   6588:     m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   6589:   else
                   6590:     m0 = m = MALLOC(len*nmv_adv);
                   6591: #if 0
                   6592:   ndv_alloc += nmv_adv*len;
                   6593: #endif
                   6594:   for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6595:     dltondl(n,a[i]->dl,DL(m));
                   6596:     MPOS(DL(m)) = a[i]->pos;
1.20      noro     6597:     TD(DL(m)) = ndl_weight(DL(m));
1.16      noro     6598:     CZ(m) = (Z)a[i]->c;
                   6599:   }
1.20      noro     6600:   qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16      noro     6601:   MKNDV(NV(p),m0,len,d);
                   6602:   SG(d) = SG(p);
                   6603:   return d;
                   6604: }
                   6605:
1.1       noro     6606: ND ndvtond(int mod,NDV p)
                   6607: {
                   6608:     ND d;
                   6609:     NM m,m0;
                   6610:     NMV t;
                   6611:     int i,len;
                   6612:
                   6613:     if ( !p ) return 0;
                   6614:     m0 = 0;
                   6615:     len = p->len;
                   6616:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   6617:         NEXTNM(m0,m);
                   6618:         ndl_copy(DL(t),DL(m));
1.6       noro     6619:         CZ(m) = CZ(t);
1.1       noro     6620:     }
                   6621:     NEXT(m) = 0;
                   6622:     MKND(NV(p),m0,len,d);
                   6623:     SG(d) = SG(p);
1.28      noro     6624:     d->sig = p->sig;
1.1       noro     6625:     return d;
                   6626: }
                   6627:
                   6628: DP ndvtodp(int mod,NDV p)
                   6629: {
                   6630:     MP m,m0;
                   6631:   DP d;
                   6632:     NMV t;
                   6633:     int i,len;
                   6634:
                   6635:     if ( !p ) return 0;
                   6636:     m0 = 0;
                   6637:     len = p->len;
                   6638:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   6639:         NEXTMP(m0,m);
                   6640:       m->dl = ndltodl(nd_nvar,DL(t));
                   6641:       m->c = (Obj)ndctop(mod,t->c);
                   6642:     }
                   6643:     NEXT(m) = 0;
                   6644:   MKDP(nd_nvar,m0,d);
                   6645:     SG(d) = SG(p);
                   6646:     return d;
                   6647: }
                   6648:
1.16      noro     6649: DPM ndvtodpm(int mod,NDV p)
                   6650: {
                   6651:   DMM m,m0;
                   6652:   DPM d;
                   6653:   NMV t;
                   6654:   int i,len;
                   6655:
                   6656:   if ( !p ) return 0;
                   6657:   m0 = 0;
                   6658:   len = p->len;
                   6659:   for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   6660:     NEXTDMM(m0,m);
                   6661:     m->dl = ndltodl(nd_nvar,DL(t));
                   6662:     m->c = (Obj)ndctop(mod,t->c);
                   6663:     m->pos = MPOS(DL(t));
                   6664:   }
                   6665:   NEXT(m) = 0;
                   6666:   MKDPM(nd_nvar,m0,d);
                   6667:   SG(d) = SG(p);
                   6668:   return d;
                   6669: }
                   6670:
                   6671:
1.1       noro     6672: DP ndtodp(int mod,ND p)
                   6673: {
                   6674:     MP m,m0;
                   6675:   DP d;
                   6676:     NM t;
                   6677:     int i,len;
                   6678:
                   6679:     if ( !p ) return 0;
                   6680:     m0 = 0;
                   6681:     len = p->len;
                   6682:     for ( t = BDY(p); t; t = NEXT(t) ) {
                   6683:         NEXTMP(m0,m);
                   6684:       m->dl = ndltodl(nd_nvar,DL(t));
                   6685:       m->c = (Obj)ndctop(mod,t->c);
                   6686:     }
                   6687:     NEXT(m) = 0;
                   6688:   MKDP(nd_nvar,m0,d);
                   6689:     SG(d) = SG(p);
                   6690:     return d;
                   6691: }
                   6692:
                   6693: void ndv_print(NDV p)
                   6694: {
                   6695:     NMV m;
                   6696:     int i,len;
                   6697:
                   6698:     if ( !p ) printf("0\n");
                   6699:     else {
                   6700:         len = LEN(p);
                   6701:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6702:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     6703:             else printf("+%ld*",CM(m));
1.1       noro     6704:             ndl_print(DL(m));
                   6705:         }
                   6706:         printf("\n");
                   6707:     }
                   6708: }
                   6709:
                   6710: void ndv_print_q(NDV p)
                   6711: {
                   6712:     NMV m;
                   6713:     int i,len;
                   6714:
                   6715:     if ( !p ) printf("0\n");
                   6716:     else {
                   6717:         len = LEN(p);
                   6718:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6719:             printf("+");
1.6       noro     6720:             printexpr(CO,(Obj)CZ(m));
1.1       noro     6721:             printf("*");
                   6722:             ndl_print(DL(m));
                   6723:         }
                   6724:         printf("\n");
                   6725:     }
                   6726: }
                   6727:
                   6728: NODE ndv_reducebase(NODE x,int *perm)
                   6729: {
                   6730:     int len,i,j;
                   6731:     NDVI w;
                   6732:     NODE t,t0;
                   6733:
                   6734:     len = length(x);
                   6735:     w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
                   6736:     for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
                   6737:         w[i].p = BDY(t); w[i].i = perm[i];
                   6738:     }
                   6739:     for ( i = 0; i < len; i++ ) {
                   6740:         for ( j = 0; j < i; j++ ) {
                   6741:             if ( w[i].p && w[j].p ) {
                   6742:                 if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
                   6743:                 else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
                   6744:             }
                   6745:         }
                   6746:     }
                   6747:     for ( i = j = 0, t0 = 0; i < len; i++ ) {
                   6748:         if ( w[i].p ) {
                   6749:             NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
                   6750:             perm[j++] = w[i].i;
                   6751:         }
                   6752:     }
                   6753:     NEXT(t) = 0; x = t0;
                   6754:     return x;
                   6755: }
                   6756:
                   6757: /* XXX incomplete */
                   6758:
1.21      noro     6759: extern DMMstack dmm_stack;
                   6760: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16      noro     6761:
1.1       noro     6762: void nd_init_ord(struct order_spec *ord)
                   6763: {
                   6764:   nd_module = (ord->id >= 256);
                   6765:   if ( nd_module ) {
                   6766:     nd_dcomp = -1;
1.21      noro     6767:     nd_module_ordtype = ord->module_ordtype;
1.1       noro     6768:     nd_pot_nelim = ord->pot_nelim;
                   6769:     nd_poly_weight_len = ord->nv;
                   6770:     nd_poly_weight = ord->top_weight;
                   6771:     nd_module_rank = ord->module_rank;
                   6772:     nd_module_weight = ord->module_top_weight;
                   6773:   }
                   6774:   nd_matrix = 0;
                   6775:   nd_matrix_len = 0;
                   6776:     switch ( ord->id ) {
                   6777:         case 0:
                   6778:             switch ( ord->ord.simple ) {
                   6779:                 case 0:
                   6780:                     nd_dcomp = 1;
                   6781:                     nd_isrlex = 1;
                   6782:                     break;
                   6783:                 case 1:
                   6784:                     nd_dcomp = 1;
                   6785:                     nd_isrlex = 0;
                   6786:                     break;
                   6787:                 case 2:
                   6788:                     nd_dcomp = 0;
                   6789:                     nd_isrlex = 0;
                   6790:                     ndl_compare_function = ndl_lex_compare;
                   6791:                     break;
                   6792:                 case 11:
                   6793:                     /* XXX */
                   6794:                     nd_dcomp = 0;
                   6795:                     nd_isrlex = 1;
                   6796:                     ndl_compare_function = ndl_ww_lex_compare;
                   6797:                     break;
                   6798:                 default:
                   6799:                     error("nd_gr : unsupported order");
                   6800:             }
                   6801:             break;
                   6802:         case 1:
                   6803:             /* block order */
                   6804:             /* XXX */
                   6805:             nd_dcomp = -1;
                   6806:             nd_isrlex = 0;
                   6807:             ndl_compare_function = ndl_block_compare;
                   6808:             break;
                   6809:         case 2:
                   6810:             /* matrix order */
                   6811:             /* XXX */
                   6812:             nd_dcomp = -1;
                   6813:             nd_isrlex = 0;
                   6814:             nd_matrix_len = ord->ord.matrix.row;
                   6815:             nd_matrix = ord->ord.matrix.matrix;
                   6816:             ndl_compare_function = ndl_matrix_compare;
                   6817:             break;
                   6818:         case 3:
                   6819:             /* composite order */
                   6820:             nd_dcomp = -1;
                   6821:             nd_isrlex = 0;
                   6822:             nd_worb_len = ord->ord.composite.length;
                   6823:             nd_worb = ord->ord.composite.w_or_b;
                   6824:             ndl_compare_function = ndl_composite_compare;
                   6825:             break;
                   6826:
                   6827:         /* module order */
                   6828:         case 256:
                   6829:             switch ( ord->ord.simple ) {
                   6830:                 case 0:
1.21      noro     6831:                     nd_dcomp = 0;
1.1       noro     6832:                     nd_isrlex = 1;
1.21      noro     6833:                     ndl_compare_function = ndl_module_glex_compare;
1.1       noro     6834:                     break;
                   6835:                 case 1:
1.21      noro     6836:                     nd_dcomp = 0;
1.1       noro     6837:                     nd_isrlex = 0;
                   6838:                     ndl_compare_function = ndl_module_glex_compare;
                   6839:                     break;
                   6840:                 case 2:
1.21      noro     6841:                     nd_dcomp = 0;
1.1       noro     6842:                     nd_isrlex = 0;
1.21      noro     6843:                     ndl_compare_function = ndl_module_compare;
                   6844:                     ndl_base_compare_function = ndl_lex_compare;
1.1       noro     6845:                     break;
                   6846:                 default:
1.21      noro     6847:                     error("nd_init_ord : unsupported order");
1.1       noro     6848:             }
                   6849:             break;
                   6850:         case 257:
                   6851:             /* block order */
                   6852:             nd_isrlex = 0;
1.21      noro     6853:             ndl_compare_function = ndl_module_compare;
                   6854:             ndl_base_compare_function = ndl_block_compare;
1.1       noro     6855:             break;
                   6856:         case 258:
                   6857:             /* matrix order */
                   6858:             nd_isrlex = 0;
                   6859:             nd_matrix_len = ord->ord.matrix.row;
                   6860:             nd_matrix = ord->ord.matrix.matrix;
1.21      noro     6861:             ndl_compare_function = ndl_module_compare;
                   6862:             ndl_base_compare_function = ndl_matrix_compare;
1.1       noro     6863:             break;
                   6864:         case 259:
                   6865:             /* composite order */
                   6866:             nd_isrlex = 0;
                   6867:             nd_worb_len = ord->ord.composite.length;
                   6868:             nd_worb = ord->ord.composite.w_or_b;
1.21      noro     6869:             ndl_compare_function = ndl_module_compare;
                   6870:             ndl_base_compare_function = ndl_composite_compare;
                   6871:             break;
                   6872:         case 300:
                   6873:             /* schreyer order */
                   6874:             if ( ord->base->id != 256 )
                   6875:                error("nd_init_ord : unsupported base order");
                   6876:             ndl_compare_function = ndl_module_schreyer_compare;
                   6877:             dmm_stack = ord->dmmstack;
                   6878:             switch ( ord->base->ord.simple ) {
                   6879:                 case 0:
                   6880:                     nd_isrlex = 1;
                   6881:                     ndl_base_compare_function = ndl_glex_compare;
                   6882:                     dl_base_compare_function = cmpdl_revgradlex;
                   6883:                     break;
                   6884:                 case 1:
                   6885:                     nd_isrlex = 0;
                   6886:                     ndl_base_compare_function = ndl_glex_compare;
                   6887:                     dl_base_compare_function = cmpdl_gradlex;
                   6888:                     break;
                   6889:                 case 2:
                   6890:                     nd_isrlex = 0;
                   6891:                     ndl_base_compare_function = ndl_lex_compare;
                   6892:                     dl_base_compare_function = cmpdl_lex;
                   6893:                     break;
                   6894:                 default:
                   6895:                     error("nd_init_ord : unsupported order");
                   6896:             }
1.1       noro     6897:             break;
                   6898:     }
                   6899:     nd_ord = ord;
                   6900: }
                   6901:
                   6902: BlockMask nd_create_blockmask(struct order_spec *ord)
                   6903: {
                   6904:     int n,i,j,s,l;
                   6905:     UINT *t;
                   6906:     BlockMask bm;
                   6907:
                   6908:     /* we only create mask table for block order */
                   6909:     if ( ord->id != 1 && ord->id != 257 )
                   6910:         return 0;
                   6911:     n = ord->ord.block.length;
                   6912:     bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   6913:     bm->n = n;
                   6914:     bm->order_pair = ord->ord.block.order_pair;
                   6915:     bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
                   6916:     for ( i = 0, s = 0; i < n; i++ ) {
                   6917:         bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
                   6918:         for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   6919:         l = bm->order_pair[i].length;
                   6920:         for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   6921:     }
                   6922:     return bm;
                   6923: }
                   6924:
                   6925: EPOS nd_create_epos(struct order_spec *ord)
                   6926: {
                   6927:     int i,j,l,s,ord_l,ord_o;
                   6928:     EPOS epos;
                   6929:     struct order_pair *op;
                   6930:
                   6931:     epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   6932:     switch ( ord->id ) {
1.21      noro     6933:         case 0: case 256: case 300:
1.1       noro     6934:             if ( nd_isrlex ) {
                   6935:                 for ( i = 0; i < nd_nvar; i++ ) {
                   6936:                     epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   6937:                     epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   6938:                 }
                   6939:             } else {
                   6940:                 for ( i = 0; i < nd_nvar; i++ ) {
                   6941:                     epos[i].i = nd_exporigin + i/nd_epw;
                   6942:                     epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   6943:                 }
                   6944:             }
                   6945:             break;
                   6946:         case 1: case 257:
                   6947:             /* block order */
                   6948:             l = ord->ord.block.length;
                   6949:             op = ord->ord.block.order_pair;
                   6950:             for ( j = 0, s = 0; j < l; j++ ) {
                   6951:                 ord_o = op[j].order;
                   6952:                 ord_l = op[j].length;
                   6953:                 if ( !ord_o )
                   6954:                     for ( i = 0; i < ord_l; i++ ) {
                   6955:                         epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   6956:                         epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   6957:                     }
                   6958:                 else
                   6959:                     for ( i = 0; i < ord_l; i++ ) {
                   6960:                         epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   6961:                         epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   6962:                     }
                   6963:                 s += ord_l;
                   6964:             }
                   6965:             break;
                   6966:         case 2:
                   6967:             /* matrix order */
                   6968:         case 3:
                   6969:             /* composite order */
                   6970:         default:
                   6971:             for ( i = 0; i < nd_nvar; i++ ) {
                   6972:                 epos[i].i = nd_exporigin + i/nd_epw;
                   6973:                 epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   6974:             }
                   6975:             break;
                   6976:     }
                   6977:     return epos;
                   6978: }
                   6979:
                   6980: /* external interface */
                   6981:
                   6982: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
                   6983: {
                   6984:     NODE t,in0,in;
                   6985:     ND ndf,nf;
                   6986:     NDV ndvf;
                   6987:     VL vv,tv;
                   6988:     int stat,nvar,max,mrank;
                   6989:     union oNDC dn;
                   6990:     Q cont;
                   6991:     P pp;
                   6992:     LIST ppl;
                   6993:
                   6994:     if ( !f ) {
                   6995:         *rp = 0;
                   6996:         return;
                   6997:     }
                   6998:     pltovl(v,&vv);
                   6999:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   7000:
                   7001:     /* max=65536 implies nd_bpe=32 */
                   7002:     max = 65536;
                   7003:
                   7004:   nd_module = 0;
                   7005:   /* nd_module will be set if ord is a module ordering */
                   7006:     nd_init_ord(ord);
                   7007:     nd_setup_parameters(nvar,max);
                   7008:     if ( nd_module && OID(f) != O_LIST )
                   7009:         error("nd_nf_p : the first argument must be a list");
                   7010:   if ( nd_module ) mrank = length(BDY((LIST)f));
                   7011:     /* conversion to ndv */
                   7012:     for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   7013:         NEXTNODE(in0,in);
                   7014:         if ( nd_module ) {
                   7015:           if ( !BDY(t) || OID(BDY(t)) != O_LIST
                   7016:                || length(BDY((LIST)BDY(t))) != mrank )
                   7017:               error("nd_nf_p : inconsistent basis element");
                   7018:           if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
                   7019:           else ppl = (LIST)BDY(t);
                   7020:           BDY(in) = (pointer)pltondv(CO,vv,ppl);
                   7021:         } else {
                   7022:           if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
                   7023:           else pp = (P)BDY(t);
                   7024:           BDY(in) = (pointer)ptondv(CO,vv,pp);
                   7025:         }
                   7026:         if ( m ) ndv_mod(m,(NDV)BDY(in));
                   7027:     }
                   7028:     if ( in0 ) NEXT(in) = 0;
                   7029:
                   7030:     if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
                   7031:     else ndvf = ptondv(CO,vv,(P)f);
                   7032:     if ( m ) ndv_mod(m,ndvf);
                   7033:     ndf = (pointer)ndvtond(m,ndvf);
                   7034:
                   7035:     /* dont sort, dont removecont */
1.24      noro     7036:     ndv_setup(m,0,in0,1,1,0);
1.1       noro     7037:     nd_scale=2;
1.6       noro     7038:     stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1       noro     7039:     if ( !stat )
                   7040:         error("nd_nf_p : exponent too large");
                   7041:     if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
                   7042:     else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
                   7043: }
                   7044:
                   7045: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   7046: {
                   7047:     NM m;
                   7048:     UINT *t,*s;
                   7049:     int i;
                   7050:
                   7051:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   7052:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7053:         t = DL(m);
                   7054:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   7055:         r[i] = CM(m);
                   7056:     }
                   7057:     for ( i = 0; !r[i]; i++ );
                   7058:     return i;
                   7059: }
                   7060:
                   7061: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
                   7062: {
                   7063:     NM m;
                   7064:     UINT *t,*s;
                   7065:     int i;
                   7066:
                   7067:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   7068:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7069:         t = DL(m);
                   7070:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     7071:         r[i] = CZ(m);
1.1       noro     7072:     }
                   7073:     for ( i = 0; !r[i]; i++ );
                   7074:     return i;
                   7075: }
                   7076:
                   7077: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
                   7078: {
                   7079:     NM m;
                   7080:     UINT *t,*s;
                   7081:     int i;
                   7082:
                   7083:     for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
                   7084:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7085:         t = DL(m);
                   7086:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   7087:         mpz_set(r[i],BDY(CZ(m)));
                   7088:     }
                   7089:     for ( i = 0; !mpz_sgn(r[i]); i++ );
                   7090:     return i;
                   7091: }
                   7092:
                   7093: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
                   7094: {
                   7095:     NM m;
                   7096:     unsigned long *v;
                   7097:     int i,j,h,size;
                   7098:   UINT *s,*t;
                   7099:
                   7100:   size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
                   7101:     v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
                   7102:     bzero(v,size);
                   7103:     for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
                   7104:     t = DL(m);
                   7105:     h = ndl_hash_value(t);
                   7106:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   7107:       v[i/BLEN] |= 1L <<(i%BLEN);
                   7108:     }
                   7109:     return v;
                   7110: }
                   7111:
                   7112: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
                   7113: {
                   7114:     NMV mr;
                   7115:     UINT *d,*t,*s;
                   7116:     int i,j,len,h,head;
                   7117:
                   7118:     d = DL(m);
                   7119:     len = LEN(p);
                   7120:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   7121:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   7122:         ndl_add(d,DL(mr),t);
                   7123:     h = ndl_hash_value(t);
                   7124:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   7125:     if ( j == 0 ) head = i;
                   7126:       v[i/BLEN] |= 1L <<(i%BLEN);
                   7127:     }
                   7128:     return head;
                   7129: }
                   7130:
                   7131: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   7132: {
                   7133:     NM m;
                   7134:     NMV mr;
                   7135:     UINT *d,*t,*s;
                   7136:     NDV p;
                   7137:     int i,j,len;
                   7138:     Z *r;
                   7139:
                   7140:     m = pair->mul;
                   7141:     d = DL(m);
                   7142:     p = nd_ps[pair->index];
                   7143:     len = LEN(p);
                   7144:     r = (Z *)CALLOC(n,sizeof(Q));
                   7145:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   7146:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   7147:         ndl_add(d,DL(mr),t);
                   7148:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     7149:         r[i] = CZ(mr);
1.1       noro     7150:     }
                   7151:     return r;
                   7152: }
                   7153:
1.11      noro     7154: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1       noro     7155: {
                   7156:     NM m;
                   7157:     NMV mr;
1.11      noro     7158:     UINT *d,*t,*s,*u;
1.1       noro     7159:     NDV p;
                   7160:     unsigned char *ivc;
                   7161:     unsigned short *ivs;
                   7162:     UINT *v,*ivi,*s0v;
1.11      noro     7163:     int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1       noro     7164:     IndArray r;
                   7165:
                   7166:     m = pair->mul;
                   7167:     d = DL(m);
                   7168:     if ( trace )
                   7169:       p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
                   7170:     else
                   7171:       p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
                   7172:
                   7173:     len = LEN(p);
                   7174:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   7175:     v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11      noro     7176:     for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
                   7177:       ndl_add(d,DL(mr),t);
                   7178:       st = prev;
                   7179:       ed = n;
                   7180:       while ( ed > st ) {
                   7181:         md = (st+ed)/2;
                   7182:         u = s0+md*nd_wpd;
                   7183:         c = DL_COMPARE(u,t);
                   7184:         if ( c == 0 ) break;
                   7185:         else if ( c > 0 ) st = md;
                   7186:         else ed = md;
                   7187:       }
                   7188:       prev = v[j] = md;
1.1       noro     7189:     }
                   7190:     r = (IndArray)MALLOC(sizeof(struct oIndArray));
                   7191:     r->head = v[0];
                   7192:     diff = 0;
                   7193:     for ( i = 1; i < len; i++ ) {
                   7194:         cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   7195:     }
                   7196:     if ( diff < 256 ) {
                   7197:         r->width = 1;
                   7198:         ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   7199:         r->index.c = ivc;
                   7200:         for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   7201:     } else if ( diff < 65536 ) {
                   7202:         r->width = 2;
                   7203:         ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   7204:         r->index.s = ivs;
                   7205:         for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   7206:     } else {
                   7207:         r->width = 4;
                   7208:         ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   7209:         r->index.i = ivi;
                   7210:         for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
                   7211:     }
                   7212:     return r;
                   7213: }
                   7214:
                   7215: int compress_array(Z *svect,Z *cvect,int n)
                   7216: {
                   7217:     int i,j;
                   7218:
                   7219:     for ( i = j = 0; i < n; i++ )
                   7220:         if ( svect[i] ) cvect[j++] = svect[i];
                   7221:     return j;
                   7222: }
                   7223:
                   7224: void expand_array(Z *svect,Z *cvect,int n)
                   7225: {
                   7226:     int i,j;
                   7227:
                   7228:     for ( i = j = 0; j < n;  i++  )
                   7229:         if ( svect[i] ) svect[i] = cvect[j++];
                   7230: }
                   7231:
1.8       noro     7232: #if 0
1.1       noro     7233: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7234: {
                   7235:     int i,j,k,len,pos,prev,nz;
                   7236:     Z cs,mcs,c1,c2,cr,gcd,t;
                   7237:     IndArray ivect;
                   7238:     unsigned char *ivc;
                   7239:     unsigned short *ivs;
                   7240:     unsigned int *ivi;
                   7241:     NDV redv;
                   7242:     NMV mr;
                   7243:     NODE rp;
                   7244:     int maxrs;
                   7245:     double hmag;
                   7246:     Z *cvect;
1.3       noro     7247:     int l;
1.1       noro     7248:
                   7249:     maxrs = 0;
                   7250:     for ( i = 0; i < col && !svect[i]; i++ );
                   7251:     if ( i == col ) return maxrs;
                   7252:     hmag = p_mag((P)svect[i])*nd_scale;
                   7253:     cvect = (Z *)MALLOC(col*sizeof(Q));
                   7254:     for ( i = 0; i < nred; i++ ) {
                   7255:         ivect = imat[i];
                   7256:         k = ivect->head;
                   7257:         if ( svect[k] ) {
                   7258:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7259:             redv = nd_demand?ndv_load(rp0[i]->index)
                   7260:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   7261:             len = LEN(redv); mr = BDY(redv);
1.6       noro     7262:             igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1       noro     7263:             chsgnz(cs,&mcs);
                   7264:             if ( !UNIQ(cr) ) {
                   7265:                 for ( j = 0; j < col; j++ ) {
                   7266:                     mulz(svect[j],cr,&c1); svect[j] = c1;
                   7267:                 }
                   7268:             }
                   7269:             svect[k] = 0; prev = k;
                   7270:             switch ( ivect->width ) {
                   7271:                 case 1:
                   7272:                     ivc = ivect->index.c;
                   7273:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7274:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     7275:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     7276:                     }
                   7277:                     break;
                   7278:                 case 2:
                   7279:                     ivs = ivect->index.s;
                   7280:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7281:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     7282:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     7283:                     }
                   7284:                     break;
                   7285:                 case 4:
                   7286:                     ivi = ivect->index.i;
                   7287:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7288:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     7289:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     7290:                     }
                   7291:                     break;
                   7292:             }
                   7293:             for ( j = k+1; j < col && !svect[j]; j++ );
                   7294:             if ( j == col ) break;
                   7295:             if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
                   7296:                 nz = compress_array(svect,cvect,col);
                   7297:                 removecont_array((P *)cvect,nz,1);
                   7298:                 expand_array(svect,cvect,nz);
                   7299:                 hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   7300:             }
                   7301:         }
                   7302:     }
                   7303:     nz = compress_array(svect,cvect,col);
                   7304:     removecont_array((P *)cvect,nz,1);
                   7305:     expand_array(svect,cvect,nz);
                   7306:     if ( DP_Print ) {
                   7307:         fprintf(asir_out,"-"); fflush(asir_out);
                   7308:     }
                   7309:     return maxrs;
                   7310: }
1.4       noro     7311: #else
1.9       noro     7312:
1.4       noro     7313: /* direct mpz version */
                   7314: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7315: {
                   7316:     int i,j,k,len,pos,prev;
                   7317:     mpz_t cs,cr,gcd;
                   7318:     IndArray ivect;
                   7319:     unsigned char *ivc;
                   7320:     unsigned short *ivs;
                   7321:     unsigned int *ivi;
                   7322:     NDV redv;
                   7323:     NMV mr;
                   7324:     NODE rp;
                   7325:     int maxrs;
                   7326:     double hmag;
                   7327:     int l;
1.13      noro     7328:     static mpz_t *svect;
                   7329:     static int svect_len=0;
1.4       noro     7330:
                   7331:     maxrs = 0;
                   7332:     for ( i = 0; i < col && !svect0[i]; i++ );
                   7333:     if ( i == col ) return maxrs;
                   7334:     hmag = p_mag((P)svect0[i])*nd_scale;
1.13      noro     7335:     if ( col > svect_len ) {
                   7336:       svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   7337:       svect_len = col;
                   7338:     }
1.4       noro     7339:     for ( i = 0; i < col; i++ ) {
                   7340:       mpz_init(svect[i]);
                   7341:       if ( svect0[i] )
                   7342:         mpz_set(svect[i],BDY(svect0[i]));
                   7343:       else
                   7344:         mpz_set_ui(svect[i],0);
                   7345:     }
                   7346:     mpz_init(gcd); mpz_init(cs); mpz_init(cr);
                   7347:     for ( i = 0; i < nred; i++ ) {
                   7348:         ivect = imat[i];
                   7349:         k = ivect->head;
                   7350:         if ( mpz_sgn(svect[k]) ) {
                   7351:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7352:             redv = nd_demand?ndv_load(rp0[i]->index)
                   7353:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   7354:             len = LEN(redv); mr = BDY(redv);
1.6       noro     7355:             mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4       noro     7356:             mpz_div(cs,svect[k],gcd);
1.6       noro     7357:             mpz_div(cr,BDY(CZ(mr)),gcd);
1.4       noro     7358:             mpz_neg(cs,cs);
1.9       noro     7359:             if ( MUNIMPZ(cr) )
                   7360:               for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
                   7361:             else if ( !UNIMPZ(cr) )
                   7362:               for ( j = 0; j < col; j++ ) {
                   7363:                 if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
                   7364:               }
1.4       noro     7365:             mpz_set_ui(svect[k],0);
                   7366:             prev = k;
                   7367:             switch ( ivect->width ) {
                   7368:                 case 1:
                   7369:                     ivc = ivect->index.c;
                   7370:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7371:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     7372:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     7373:                     }
                   7374:                     break;
                   7375:                 case 2:
                   7376:                     ivs = ivect->index.s;
                   7377:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7378:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     7379:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     7380:                     }
                   7381:                     break;
                   7382:                 case 4:
                   7383:                     ivi = ivect->index.i;
                   7384:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7385:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     7386:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     7387:                     }
                   7388:                     break;
                   7389:             }
                   7390:             for ( j = k+1; j < col && !svect[j]; j++ );
                   7391:             if ( j == col ) break;
                   7392:             if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
                   7393:                 mpz_removecont_array(svect,col);
                   7394:                 hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
                   7395:             }
                   7396:         }
                   7397:     }
                   7398:     mpz_removecont_array(svect,col);
                   7399:     if ( DP_Print ) {
                   7400:         fprintf(asir_out,"-"); fflush(asir_out);
                   7401:     }
                   7402:     for ( i = 0; i < col; i++ )
                   7403:       if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
                   7404:       else svect0[i] = 0;
                   7405:     return maxrs;
                   7406: }
                   7407: #endif
1.1       noro     7408:
                   7409: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7410: {
                   7411:     int i,j,k,len,pos,prev;
                   7412:     UINT c,c1,c2,c3,up,lo,dmy;
                   7413:     IndArray ivect;
                   7414:     unsigned char *ivc;
                   7415:     unsigned short *ivs;
                   7416:     unsigned int *ivi;
                   7417:     NDV redv;
                   7418:     NMV mr;
                   7419:     NODE rp;
                   7420:     int maxrs;
                   7421:
                   7422:     maxrs = 0;
                   7423:     for ( i = 0; i < nred; i++ ) {
                   7424:         ivect = imat[i];
                   7425:         k = ivect->head; svect[k] %= m;
                   7426:         if ( (c = svect[k]) != 0 ) {
                   7427:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7428:             c = m-c; redv = nd_ps[rp0[i]->index];
                   7429:             len = LEN(redv); mr = BDY(redv);
                   7430:             svect[k] = 0; prev = k;
                   7431:             switch ( ivect->width ) {
                   7432:                 case 1:
                   7433:                     ivc = ivect->index.c;
                   7434:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7435:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
                   7436:             if ( c1 ) {
                   7437:               c2 = svect[pos];
                   7438:                           DMA(c1,c,c2,up,lo);
                   7439:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   7440:                           } else svect[pos] = lo;
                   7441:             }
                   7442:                     }
                   7443:                     break;
                   7444:                 case 2:
                   7445:                     ivs = ivect->index.s;
                   7446:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7447:                         pos = prev+ivs[j]; c1 = CM(mr);
                   7448:                         prev = pos;
                   7449:             if ( c1 ) {
                   7450:               c2 = svect[pos];
                   7451:                           DMA(c1,c,c2,up,lo);
                   7452:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   7453:                           } else svect[pos] = lo;
                   7454:             }
                   7455:                     }
                   7456:                     break;
                   7457:                 case 4:
                   7458:                     ivi = ivect->index.i;
                   7459:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7460:                         pos = prev+ivi[j]; c1 = CM(mr);
                   7461:                         prev = pos;
                   7462:             if ( c1 ) {
                   7463:               c2 = svect[pos];
                   7464:                           DMA(c1,c,c2,up,lo);
                   7465:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   7466:                           } else svect[pos] = lo;
                   7467:             }
                   7468:                     }
                   7469:                     break;
                   7470:             }
                   7471:         }
                   7472:     }
                   7473:     for ( i = 0; i < col; i++ )
                   7474:         if ( svect[i] >= (UINT)m ) svect[i] %= m;
                   7475:     return maxrs;
                   7476: }
                   7477:
                   7478: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7479: {
                   7480:     int i,j,k,len,pos,prev;
                   7481:     UINT c,c1,c2,c3,up,lo,dmy;
                   7482:     IndArray ivect;
                   7483:     unsigned char *ivc;
                   7484:     unsigned short *ivs;
                   7485:     unsigned int *ivi;
                   7486:     NDV redv;
                   7487:     NMV mr;
                   7488:     NODE rp;
                   7489:     int maxrs;
                   7490:
                   7491:     maxrs = 0;
                   7492:     for ( i = 0; i < nred; i++ ) {
                   7493:         ivect = imat[i];
                   7494:         k = ivect->head;
                   7495:         if ( (c = svect[k]) != 0 ) {
                   7496:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7497:             c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
                   7498:             len = LEN(redv); mr = BDY(redv);
                   7499:             svect[k] = 0; prev = k;
                   7500:             switch ( ivect->width ) {
                   7501:                 case 1:
                   7502:                     ivc = ivect->index.c;
                   7503:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7504:                         pos = prev+ivc[j]; prev = pos;
                   7505:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   7506:                     }
                   7507:                     break;
                   7508:                 case 2:
                   7509:                     ivs = ivect->index.s;
                   7510:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7511:                         pos = prev+ivs[j]; prev = pos;
                   7512:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   7513:                     }
                   7514:                     break;
                   7515:                 case 4:
                   7516:                     ivi = ivect->index.i;
                   7517:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7518:                         pos = prev+ivi[j]; prev = pos;
                   7519:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   7520:                     }
                   7521:                     break;
                   7522:             }
                   7523:         }
                   7524:     }
                   7525:     return maxrs;
                   7526: }
                   7527:
                   7528: ND nd_add_lf(ND p1,ND p2)
                   7529: {
                   7530:     int n,c,can;
                   7531:     ND r;
                   7532:     NM m1,m2,mr0,mr,s;
                   7533:     Z t;
                   7534:
                   7535:     if ( !p1 ) return p2;
                   7536:     else if ( !p2 ) return p1;
                   7537:     else {
                   7538:         can = 0;
                   7539:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   7540:             c = DL_COMPARE(DL(m1),DL(m2));
                   7541:             switch ( c ) {
                   7542:                 case 0:
                   7543:                     addlf(CZ(m1),CZ(m2),&t);
                   7544:                     s = m1; m1 = NEXT(m1);
                   7545:                     if ( t ) {
                   7546:                         can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
                   7547:                     } else {
                   7548:                         can += 2; FREENM(s);
                   7549:                     }
                   7550:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   7551:                     break;
                   7552:                 case 1:
                   7553:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   7554:                     break;
                   7555:                 case -1:
                   7556:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   7557:                     break;
                   7558:             }
                   7559:         }
                   7560:         if ( !mr0 )
                   7561:             if ( m1 ) mr0 = m1;
                   7562:             else if ( m2 ) mr0 = m2;
                   7563:             else return 0;
                   7564:         else if ( m1 ) NEXT(mr) = m1;
                   7565:         else if ( m2 ) NEXT(mr) = m2;
                   7566:         else NEXT(mr) = 0;
                   7567:         BDY(p1) = mr0;
                   7568:         SG(p1) = MAX(SG(p1),SG(p2));
                   7569:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   7570:         FREEND(p2);
                   7571:         return p1;
                   7572:     }
                   7573: }
                   7574:
                   7575: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7576: {
                   7577:     int i,j,k,len,pos,prev;
                   7578:     mpz_t c,mc,c1;
                   7579:     IndArray ivect;
                   7580:     unsigned char *ivc;
                   7581:     unsigned short *ivs;
                   7582:     unsigned int *ivi;
                   7583:     NDV redv;
                   7584:     NMV mr;
                   7585:     NODE rp;
                   7586:     int maxrs;
                   7587:
                   7588:     maxrs = 0;
                   7589:     lf_lazy = 1;
                   7590:     for ( i = 0; i < nred; i++ ) {
                   7591:         ivect = imat[i];
                   7592:         k = ivect->head;
                   7593:         mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
                   7594:         if ( mpz_sgn(svect[k]) ) {
                   7595:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7596:             mpz_neg(svect[k],svect[k]);
                   7597:             redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
                   7598:             len = LEN(redv); mr = BDY(redv);
                   7599:             prev = k;
                   7600:             switch ( ivect->width ) {
                   7601:                 case 1:
                   7602:                     ivc = ivect->index.c;
                   7603:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7604:                         pos = prev+ivc[j]; prev = pos;
                   7605:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   7606:                     }
                   7607:                     break;
                   7608:                 case 2:
                   7609:                     ivs = ivect->index.s;
                   7610:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7611:                         pos = prev+ivs[j]; prev = pos;
                   7612:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   7613:                     }
                   7614:                     break;
                   7615:                 case 4:
                   7616:                     ivi = ivect->index.i;
                   7617:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7618:                         pos = prev+ivi[j]; prev = pos;
                   7619:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   7620:                     }
                   7621:                     break;
                   7622:             }
                   7623:             mpz_set_ui(svect[k],0);
                   7624:         }
                   7625:     }
                   7626:     lf_lazy=0;
                   7627:     for ( i = 0; i < col; i++ ) {
                   7628:         mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
                   7629:     }
                   7630:     return maxrs;
                   7631: }
                   7632:
                   7633: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
                   7634: {
                   7635:     int i,j,k,l,rank,s;
                   7636:     mpz_t a,a1,inv;
                   7637:     mpz_t *t,*pivot,*pk;
                   7638:     mpz_t **mat;
                   7639:     struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
                   7640:     int size,size1;
                   7641:
                   7642:     mpz_init(inv);
                   7643:     mpz_init(a);
                   7644:     mat = (mpz_t **)mat0;
                   7645:         size = 0;
                   7646:     for ( rank = 0, j = 0; j < col; j++ ) {
                   7647:         for ( i = rank; i < row; i++ ) {
                   7648:             mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
                   7649:         }
                   7650:         for ( i = rank; i < row; i++ )
                   7651:             if ( mpz_sgn(mat[i][j]) )
                   7652:                 break;
                   7653:         if ( i == row ) {
                   7654:             colstat[j] = 0;
                   7655:             continue;
                   7656:         } else
                   7657:             colstat[j] = 1;
                   7658:         if ( i != rank ) {
                   7659:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   7660:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   7661:         }
                   7662:         pivot = mat[rank];
                   7663:         s = sugar[rank];
                   7664:         mpz_invert(inv,pivot[j],BDY(current_mod_lf));
                   7665:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   7666:             if ( mpz_sgn(*pk) ) {
                   7667:                 mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
                   7668:             }
                   7669:         for ( i = rank+1; i < row; i++ ) {
                   7670:             t = mat[i];
                   7671:             if ( mpz_sgn(t[j]) ) {
                   7672:                 sugar[i] = MAX(sugar[i],s);
                   7673:                 mpz_neg(a,t[j]);
                   7674:                 red_by_vect_lf(t+j,pivot+j,a,col-j);
                   7675:             }
                   7676:         }
                   7677:         rank++;
                   7678:     }
                   7679:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   7680:         if ( colstat[j] ) {
                   7681:             pivot = mat[l];
                   7682:             s = sugar[l];
                   7683:             for ( k = j; k < col; k++ )
                   7684:               mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
                   7685:             for ( i = 0; i < l; i++ ) {
                   7686:                 t = mat[i];
                   7687:                 if ( mpz_sgn(t[j]) ) {
                   7688:                     sugar[i] = MAX(sugar[i],s);
                   7689:                     mpz_neg(a,t[j]);
                   7690:                     red_by_vect_lf(t+j,pivot+j,a,col-j);
                   7691:                 }
                   7692:             }
                   7693:             l--;
                   7694:         }
                   7695:     for ( j = 0, l = 0; l < rank; j++ )
                   7696:         if ( colstat[j] ) {
                   7697:             t = mat[l];
                   7698:             for ( k = j; k < col; k++ ) {
                   7699:                 mpz_mod(t[k],t[k],BDY(current_mod_lf));
                   7700:             }
                   7701:             l++;
                   7702:         }
                   7703:     return rank;
                   7704: }
                   7705:
                   7706:
                   7707: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   7708: {
                   7709:     int j,k,len;
                   7710:     UINT *p;
                   7711:     UINT c;
                   7712:     NDV r;
                   7713:     NMV mr0,mr;
                   7714:
                   7715:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   7716:     if ( !len ) return 0;
                   7717:     else {
                   7718:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   7719: #if 0
                   7720:         ndv_alloc += nmv_adv*len;
                   7721: #endif
                   7722:         mr = mr0;
                   7723:         p = s0vect;
                   7724:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   7725:             if ( !rhead[j] ) {
                   7726:                 if ( (c = vect[k++]) != 0 ) {
                   7727:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   7728:                 }
                   7729:             }
                   7730:         MKNDV(nd_nvar,mr0,len,r);
                   7731:         return r;
                   7732:     }
                   7733: }
                   7734:
                   7735: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
                   7736: {
                   7737:     int j,k,len;
                   7738:     UINT *p;
                   7739:     NDV r;
                   7740:     NMV mr0,mr;
                   7741:
                   7742:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
                   7743:     if ( !len ) return 0;
                   7744:     else {
                   7745:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   7746:         mr = mr0;
                   7747:         p = s0vect;
                   7748:         for ( j = 0; j < col; j++, p += nd_wpd )
                   7749:       if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
                   7750:             ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
                   7751:           }
                   7752:         MKNDV(nd_nvar,mr0,len,r);
                   7753:         return r;
                   7754:     }
                   7755: }
                   7756:
                   7757: /* for preprocessed vector */
                   7758:
                   7759: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   7760: {
1.6       noro     7761:   int j,k,len;
                   7762:   UINT *p;
                   7763:   Z c;
                   7764:   NDV r;
                   7765:   NMV mr0,mr;
1.1       noro     7766:
1.6       noro     7767:   for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   7768:   if ( !len ) return 0;
                   7769:   else {
                   7770:     mr0 = (NMV)MALLOC(nmv_adv*len);
1.1       noro     7771: #if 0
1.6       noro     7772:     ndv_alloc += nmv_adv*len;
1.1       noro     7773: #endif
1.6       noro     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:           if ( !INT(c) )
                   7780:             error("vect_to_ndv_q : components must be integers");
                   7781:             ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
                   7782:         }
                   7783:       }
1.1       noro     7784:     }
1.6       noro     7785:     MKNDV(nd_nvar,mr0,len,r);
                   7786:     return r;
                   7787:   }
1.1       noro     7788: }
                   7789:
                   7790: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   7791: {
                   7792:     int j,k,len;
                   7793:     UINT *p;
                   7794:     mpz_t c;
                   7795:     NDV r;
                   7796:     NMV mr0,mr;
                   7797:
                   7798:     for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
                   7799:     if ( !len ) return 0;
                   7800:     else {
                   7801:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   7802: #if 0
                   7803:         ndv_alloc += nmv_adv*len;
                   7804: #endif
                   7805:         mr = mr0;
                   7806:         p = s0vect;
                   7807:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   7808:             if ( !rhead[j] ) {
                   7809:                 c[0] = vect[k++][0];
                   7810:                 if ( mpz_sgn(c) ) {
                   7811:                     ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
                   7812:                 }
                   7813:             }
                   7814:         MKNDV(nd_nvar,mr0,len,r);
                   7815:         return r;
                   7816:     }
                   7817: }
                   7818:
                   7819: /* for plain vector */
                   7820:
                   7821: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
                   7822: {
                   7823:     int j,k,len;
                   7824:     UINT *p;
                   7825:     Z c;
                   7826:     NDV r;
                   7827:     NMV mr0,mr;
                   7828:
                   7829:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   7830:     if ( !len ) return 0;
                   7831:     else {
                   7832:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   7833: #if 0
                   7834:         ndv_alloc += nmv_adv*len;
                   7835: #endif
                   7836:         mr = mr0;
                   7837:         p = s0vect;
                   7838:         for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   7839:             if ( (c = vect[k]) != 0 ) {
                   7840:                 if ( !INT(c) )
1.6       noro     7841:                     error("plain_vect_to_ndv_q : components must be integers");
                   7842:                 ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1       noro     7843:             }
                   7844:         MKNDV(nd_nvar,mr0,len,r);
                   7845:         return r;
                   7846:     }
                   7847: }
                   7848:
                   7849: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
                   7850: {
                   7851:     ND_pairs t;
                   7852:     NODE sp0,sp;
                   7853:     int stat;
                   7854:     ND spol;
                   7855:
                   7856:     for ( t = l; t; t = NEXT(t) ) {
                   7857:         stat = nd_sp(m,trace,t,&spol);
                   7858:         if ( !stat ) return 0;
                   7859:         if ( spol ) {
                   7860:             add_pbucket_symbolic(bucket,spol);
                   7861:         }
                   7862:     }
                   7863:     return 1;
                   7864: }
                   7865:
                   7866: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   7867: {
                   7868:     NODE rp0,rp;
                   7869:     NM mul,head,s0,s;
                   7870:     int index,col,i,sugar;
                   7871:     RHist h;
                   7872:     UINT *s0v,*p;
                   7873:     NM_ind_pair pair;
                   7874:     ND red;
                   7875:     NDV *ps;
                   7876:
                   7877:     s0 = 0; rp0 = 0; col = 0;
                   7878:   if ( nd_demand )
                   7879:       ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   7880:   else
                   7881:       ps = trace?nd_ps_trace:nd_ps;
                   7882:     while ( 1 ) {
                   7883:         head = remove_head_pbucket_symbolic(bucket);
                   7884:         if ( !head ) break;
                   7885:         if ( !s0 ) s0 = head;
                   7886:         else NEXT(s) = head;
                   7887:         s = head;
                   7888:         index = ndl_find_reducer(DL(head));
                   7889:         if ( index >= 0 ) {
                   7890:             h = nd_psh[index];
                   7891:             NEWNM(mul);
                   7892:             ndl_sub(DL(head),DL(h),DL(mul));
                   7893:             if ( ndl_check_bound2(index,DL(mul)) )
                   7894:                 return 0;
                   7895:             sugar = TD(DL(mul))+SG(ps[index]);
1.28      noro     7896:             MKNM_ind_pair(pair,mul,index,sugar,0);
1.1       noro     7897:             red = ndv_mul_nm_symbolic(mul,ps[index]);
                   7898:             add_pbucket_symbolic(bucket,nd_remove_head(red));
                   7899:             NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   7900:         }
                   7901:         col++;
                   7902:     }
                   7903:     if ( rp0 ) NEXT(rp) = 0;
                   7904:     NEXT(s) = 0;
                   7905:     s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   7906:     for ( i = 0, p = s0v, s = s0; i < col;
                   7907:         i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   7908:     *s0vect = s0v;
                   7909:     *r = rp0;
                   7910:     return col;
                   7911: }
                   7912:
                   7913: void print_ndp(ND_pairs l)
                   7914: {
                   7915:   ND_pairs t;
                   7916:
                   7917:   for ( t = l; t; t = NEXT(t) )
                   7918:     printf("[%d,%d] ",t->i1,t->i2);
                   7919:   printf("\n");
                   7920: }
                   7921:
                   7922: NODE nd_f4(int m,int checkonly,int **indp)
                   7923: {
                   7924:     int i,nh,stat,index,f4red;
                   7925:     NODE r,g,tn0,tn,node;
                   7926:     ND_pairs d,l,t,ll0,ll,lh;
                   7927:   LIST l0,l1;
                   7928:     ND spol,red;
                   7929:     NDV nf,redv;
                   7930:     NM s0,s;
                   7931:     NODE rp0,srp0,nflist,nzlist,nzlist_t;
                   7932:     int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
                   7933:     UINT c;
                   7934:     UINT **spmat;
                   7935:     UINT *s0vect,*svect,*p,*v;
                   7936:     int *colstat;
                   7937:     IndArray *imat;
                   7938:     int *rhead;
                   7939:     int spcol,sprow;
                   7940:     int sugar,sugarh;
                   7941:     PGeoBucket bucket;
                   7942:     struct oEGT eg0,eg1,eg_f4;
                   7943:     Z i1,i2,sugarq;
1.12      noro     7944:
                   7945:     init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1       noro     7946: #if 0
                   7947:     ndv_alloc = 0;
                   7948: #endif
1.11      noro     7949:     Nf4_red=0;
1.1       noro     7950:     g = 0; d = 0;
                   7951:     for ( i = 0; i < nd_psn; i++ ) {
                   7952:         d = update_pairs(d,g,i,0);
                   7953:         g = update_base(g,i);
                   7954:     }
                   7955:   nzlist = 0;
                   7956:     nzlist_t = nd_nzlist;
                   7957:     f4red = 1;
                   7958:     nd_last_nonzero = 0;
                   7959:     while ( d ) {
                   7960:         get_eg(&eg0);
                   7961:         l = nd_minsugarp(d,&d);
                   7962:         sugar = nd_sugarweight?l->sugar2:SG(l);
                   7963:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   7964:         if ( nzlist_t ) {
                   7965:             node = BDY((LIST)BDY(nzlist_t));
1.6       noro     7966:             sugarh = ZTOS((Q)ARG0(node));
1.1       noro     7967:             tn = BDY((LIST)ARG1(node));
                   7968:             if ( !tn ) {
                   7969:               nzlist_t = NEXT(nzlist_t);
                   7970:               continue;
                   7971:             }
                   7972:             /* tn = [[i1,i2],...] */
                   7973:             lh = nd_ipairtospair(tn);
                   7974:         }
                   7975:         bucket = create_pbucket();
                   7976:         stat = nd_sp_f4(m,0,l,bucket);
                   7977:         if ( !stat ) {
                   7978:             for ( t = l; NEXT(t); t = NEXT(t) );
                   7979:             NEXT(t) = d; d = l;
                   7980:             d = nd_reconstruct(0,d);
                   7981:             continue;
                   7982:         }
                   7983:         if ( bucket->m < 0 ) continue;
                   7984:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   7985:         if ( !col ) {
                   7986:             for ( t = l; NEXT(t); t = NEXT(t) );
                   7987:             NEXT(t) = d; d = l;
                   7988:             d = nd_reconstruct(0,d);
                   7989:             continue;
                   7990:         }
1.12      noro     7991:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1       noro     7992:         if ( DP_Print )
1.6       noro     7993:             fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5       noro     7994:                 sugar,eg_f4.exectime);
1.1       noro     7995:         nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
                   7996:         if ( checkonly && nflist ) return 0;
                   7997:         /* adding new bases */
                   7998:         if ( nflist ) nd_last_nonzero = f4red;
                   7999:         for ( r = nflist; r; r = NEXT(r) ) {
                   8000:             nf = (NDV)BDY(r);
                   8001:             ndv_removecont(m,nf);
                   8002:             if ( !m && nd_nalg ) {
                   8003:                 ND nf1;
                   8004:
                   8005:                 nf1 = ndvtond(m,nf);
                   8006:                 nd_monic(0,&nf1);
                   8007:                 nd_removecont(m,nf1);
                   8008:                 nf = ndtondv(m,nf1);
                   8009:             }
1.24      noro     8010:             nh = ndv_newps(m,nf,0);
1.1       noro     8011:             d = update_pairs(d,g,nh,0);
                   8012:             g = update_base(g,nh);
                   8013:         }
                   8014:         if ( DP_Print ) {
                   8015:           fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
                   8016:         }
                   8017:         if ( nd_gentrace ) {
                   8018:       for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
                   8019:         NEXTNODE(tn0,tn);
1.6       noro     8020:                 STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1       noro     8021:                 node = mknode(2,i1,i2); MKLIST(l0,node);
                   8022:         BDY(tn) = l0;
                   8023:       }
                   8024:       if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6       noro     8025:             STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1       noro     8026:             MKNODE(node,l1,nzlist); nzlist = node;
                   8027:         }
                   8028:         if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
                   8029:         f4red++;
                   8030:         if ( nd_f4red && f4red > nd_f4red ) break;
                   8031:         if ( nd_rank0 && !nflist ) break;
                   8032:     }
                   8033:     if ( nd_gentrace ) {
                   8034:     MKLIST(l0,reverse_node(nzlist));
                   8035:         MKNODE(nd_alltracelist,l0,0);
                   8036:     }
                   8037: #if 0
                   8038:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   8039: #endif
1.12      noro     8040:   if ( DP_Print ) {
                   8041:     fprintf(asir_out,"number of red=%d,",Nf4_red);
                   8042:     fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
                   8043:       f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
                   8044:   }
1.1       noro     8045:   conv_ilist(nd_demand,0,g,indp);
                   8046:     return g;
                   8047: }
                   8048:
                   8049: NODE nd_f4_trace(int m,int **indp)
                   8050: {
                   8051:     int i,nh,stat,index;
                   8052:     NODE r,g;
                   8053:     ND_pairs d,l,l0,t;
                   8054:     ND spol,red;
                   8055:     NDV nf,redv,nfqv,nfv;
                   8056:     NM s0,s;
                   8057:     NODE rp0,srp0,nflist;
                   8058:     int nsp,nred,col,rank,len,k,j,a;
                   8059:     UINT c;
                   8060:     UINT **spmat;
                   8061:     UINT *s0vect,*svect,*p,*v;
                   8062:     int *colstat;
                   8063:     IndArray *imat;
                   8064:     int *rhead;
                   8065:     int spcol,sprow;
                   8066:     int sugar;
                   8067:     PGeoBucket bucket;
                   8068:     struct oEGT eg0,eg1,eg_f4;
                   8069:
                   8070:     g = 0; d = 0;
                   8071:     for ( i = 0; i < nd_psn; i++ ) {
                   8072:         d = update_pairs(d,g,i,0);
                   8073:         g = update_base(g,i);
                   8074:     }
                   8075:     while ( d ) {
                   8076:         get_eg(&eg0);
                   8077:         l = nd_minsugarp(d,&d);
                   8078:         sugar = SG(l);
                   8079:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   8080:         bucket = create_pbucket();
                   8081:         stat = nd_sp_f4(m,0,l,bucket);
                   8082:         if ( !stat ) {
                   8083:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8084:             NEXT(t) = d; d = l;
                   8085:             d = nd_reconstruct(1,d);
                   8086:             continue;
                   8087:         }
                   8088:         if ( bucket->m < 0 ) continue;
                   8089:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   8090:         if ( !col ) {
                   8091:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8092:             NEXT(t) = d; d = l;
                   8093:             d = nd_reconstruct(1,d);
                   8094:             continue;
                   8095:         }
                   8096:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   8097:         if ( DP_Print )
1.3       noro     8098:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5       noro     8099:                 sugar,eg_f4.exectime);
1.1       noro     8100:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   8101:         if ( !l0 ) continue;
                   8102:         l = l0;
                   8103:
                   8104:         /* over Q */
                   8105:         bucket = create_pbucket();
                   8106:         stat = nd_sp_f4(0,1,l,bucket);
                   8107:         if ( !stat ) {
                   8108:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8109:             NEXT(t) = d; d = l;
                   8110:             d = nd_reconstruct(1,d);
                   8111:             continue;
                   8112:         }
                   8113:         if ( bucket->m < 0 ) continue;
                   8114:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   8115:         if ( !col ) {
                   8116:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8117:             NEXT(t) = d; d = l;
                   8118:             d = nd_reconstruct(1,d);
                   8119:             continue;
                   8120:         }
                   8121:         nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   8122:         /* adding new bases */
                   8123:         for ( r = nflist; r; r = NEXT(r) ) {
                   8124:             nfqv = (NDV)BDY(r);
                   8125:             ndv_removecont(0,nfqv);
1.6       noro     8126:             if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1       noro     8127:             if ( nd_nalg ) {
                   8128:                 ND nf1;
                   8129:
                   8130:                 nf1 = ndvtond(m,nfqv);
                   8131:                 nd_monic(0,&nf1);
                   8132:                 nd_removecont(0,nf1);
                   8133:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   8134:             }
                   8135:             nfv = ndv_dup(0,nfqv);
                   8136:             ndv_mod(m,nfv);
                   8137:             ndv_removecont(m,nfv);
1.24      noro     8138:             nh = ndv_newps(0,nfv,nfqv);
1.1       noro     8139:             d = update_pairs(d,g,nh,0);
                   8140:             g = update_base(g,nh);
                   8141:         }
                   8142:     }
                   8143: #if 0
                   8144:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   8145: #endif
                   8146:   conv_ilist(nd_demand,1,g,indp);
                   8147:     return g;
                   8148: }
                   8149:
                   8150: int rref(matrix mat,int *sugar)
                   8151: {
                   8152:   int row,col,i,j,k,l,s,wcol,wj;
                   8153:   unsigned long bj;
                   8154:   unsigned long **a;
                   8155:   unsigned long *ai,*ak,*as,*t;
                   8156:   int *pivot;
                   8157:
                   8158:   row = mat->row;
                   8159:   col = mat->col;
                   8160:   a = mat->a;
                   8161:   wcol = (col+BLEN-1)/BLEN;
                   8162:   pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   8163:   i = 0;
                   8164:   for ( j = 0; j < col; j++ ) {
                   8165:   wj = j/BLEN; bj = 1L<<(j%BLEN);
                   8166:     for ( k = i; k < row; k++ )
                   8167:     if ( a[k][wj] & bj ) break;
                   8168:     if ( k == row ) continue;
                   8169:   pivot[i] = j;
                   8170:     if ( k != i ) {
                   8171:    t = a[i]; a[i] = a[k]; a[k] = t;
                   8172:    s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
                   8173:   }
                   8174:   ai = a[i];
                   8175:     for ( k = i+1; k < row; k++ ) {
                   8176:     ak = a[k];
                   8177:     if ( ak[wj] & bj ) {
                   8178:       for ( l = wj; l < wcol; l++ )
                   8179:       ak[l] ^= ai[l];
                   8180:       sugar[k] = MAX(sugar[k],sugar[i]);
                   8181:     }
                   8182:   }
                   8183:   i++;
                   8184:   }
                   8185:   for ( k = i-1; k >= 0; k-- ) {
                   8186:     j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
                   8187:   ak = a[k];
                   8188:     for ( s = 0; s < k; s++ ) {
                   8189:     as = a[s];
                   8190:       if ( as[wj] & bj ) {
                   8191:         for ( l = wj; l < wcol; l++ )
                   8192:       as[l] ^= ak[l];
                   8193:       sugar[s] = MAX(sugar[s],sugar[k]);
                   8194:     }
                   8195:   }
                   8196:   }
                   8197:   return i;
                   8198: }
                   8199:
                   8200: void print_matrix(matrix mat)
                   8201: {
                   8202:   int row,col,i,j;
                   8203:   unsigned long *ai;
                   8204:
                   8205:   row = mat->row;
                   8206:   col = mat->col;
                   8207:   printf("%d x %d\n",row,col);
                   8208:   for ( i = 0; i < row; i++ ) {
                   8209:   ai = mat->a[i];
                   8210:     for ( j = 0; j < col; j++ ) {
                   8211:     if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
                   8212:     else putchar('0');
                   8213:   }
                   8214:   putchar('\n');
                   8215:   }
                   8216: }
                   8217:
                   8218: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
                   8219:
                   8220: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
                   8221: {
                   8222:   int row,col,wcol,wj,i,j;
                   8223:   unsigned long bj;
                   8224:   unsigned long *ai;
                   8225:   unsigned long **a;
                   8226:   int len;
                   8227:   int *pos;
                   8228:
                   8229:   row = mat->row;
                   8230:   col = mat->col;
                   8231:   wcol = (col+BLEN-1)/BLEN;
                   8232:   pos = (int *)MALLOC(wcol*sizeof(int));
                   8233:   bzero(pos,wcol*sizeof(int));
                   8234:   for ( i = j = 0; i < wcol; i++ )
                   8235:     if ( v[i] ) pos[j++] = i;;
                   8236:   len = j;
                   8237:   wj = rhead/BLEN;
                   8238:   bj = 1L<<rhead%BLEN;
                   8239:   a = mat->a;
                   8240:   for ( i = 0; i < row; i++ ) {
                   8241:   ai = a[i];
                   8242:     if ( ai[wj]&bj ) {
                   8243:     for ( j = 0; j < len; j++ )
                   8244:       ai[pos[j]] ^= v[pos[j]];
                   8245:     sugar[i] = MAX(sugar[i],rsugar);
                   8246:   }
                   8247:   }
                   8248: }
                   8249:
                   8250: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   8251: {
                   8252:     int nsp,nred,i,i0,k,rank,row;
                   8253:     NODE r0,rp;
                   8254:     ND_pairs sp;
                   8255:   ND spol;
                   8256:   NM_ind_pair rt;
                   8257:     int *s0hash;
                   8258:   UINT *s;
                   8259:   int *pivot,*sugar,*head;
                   8260:   matrix mat;
                   8261:     NM m;
                   8262:     NODE r;
                   8263:   struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
                   8264:   int rhead,rsugar,size;
                   8265:     unsigned long *v;
                   8266:
                   8267:     get_eg(&eg0);
                   8268:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   8269:     nred = length(rp0);
                   8270:     mat = alloc_matrix(nsp,col);
                   8271:     s0hash = (int *)MALLOC(col*sizeof(int));
                   8272:     for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
                   8273:         s0hash[i] = ndl_hash_value(s);
                   8274:
                   8275:   sugar = (int *)MALLOC(nsp*sizeof(int));
                   8276:   for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
                   8277:     nd_sp(2,0,sp,&spol);
                   8278:     if ( spol ) {
                   8279:         mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
                   8280:       sugar[i] = SG(spol);
                   8281:       i++;
                   8282:     }
                   8283:   }
                   8284:   mat->row = i;
                   8285:     if ( DP_Print ) {
                   8286:       fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
                   8287:     }
                   8288:   size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
                   8289:   v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
                   8290:     for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
                   8291:     rt = (NM_ind_pair)BDY(rp);
                   8292:     bzero(v,size);
                   8293:         rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
                   8294:     rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
                   8295:       red_by_vect_2(mat,sugar,v,rhead,rsugar);
                   8296:   }
                   8297:
                   8298:     get_eg(&eg1);
                   8299:     init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
                   8300:   rank = rref(mat,sugar);
                   8301:
                   8302:     for ( i = 0, r0 = 0; i < rank; i++ ) {
                   8303:       NEXTNODE(r0,r);
                   8304:     BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
                   8305:       SG((NDV)BDY(r)) = sugar[i];
                   8306:     }
                   8307:     if ( r0 ) NEXT(r) = 0;
                   8308:     get_eg(&eg2);
                   8309:     init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
                   8310:     if ( DP_Print ) {
                   8311:         fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5       noro     8312:       eg_elim1.exectime,eg_elim2.exectime);
1.1       noro     8313:         fflush(asir_out);
                   8314:   }
                   8315:     return r0;
                   8316: }
                   8317:
                   8318:
                   8319: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   8320: {
                   8321:     IndArray *imat;
1.11      noro     8322:     int nsp,nred,i,start;
1.1       noro     8323:     int *rhead;
                   8324:     NODE r0,rp;
                   8325:     ND_pairs sp;
                   8326:     NM_ind_pair *rvect;
                   8327:     UINT *s;
                   8328:     int *s0hash;
1.11      noro     8329:     struct oEGT eg0,eg1,eg_conv;
1.1       noro     8330:
                   8331:     if ( m == 2 && nd_rref2 )
                   8332:      return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
                   8333:
                   8334:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   8335:     nred = length(rp0);
                   8336:     imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   8337:     rhead = (int *)MALLOC(col*sizeof(int));
                   8338:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   8339:
                   8340:     /* construction of index arrays */
1.11      noro     8341:     get_eg(&eg0);
1.1       noro     8342:     if ( DP_Print ) {
1.11      noro     8343:       fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   8344:       fflush(asir_out);
1.1       noro     8345:     }
                   8346:     rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11      noro     8347:     for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1       noro     8348:         rvect[i] = (NM_ind_pair)BDY(rp);
1.11      noro     8349:         imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1       noro     8350:         rhead[imat[i]->head] = 1;
1.11      noro     8351:         start = imat[i]->head;
                   8352:     }
1.12      noro     8353:     get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11      noro     8354:     if ( DP_Print ) {
                   8355:       fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   8356:       fflush(asir_out);
1.1       noro     8357:     }
                   8358:     if ( m > 0 )
1.7       noro     8359: #if SIZEOF_LONG==8
1.1       noro     8360:         r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   8361: #else
                   8362:         r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   8363: #endif
                   8364:     else if ( m == -1 )
                   8365:         r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   8366:     else if ( m == -2 )
                   8367:         r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   8368:     else
                   8369:         r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   8370:     return r0;
                   8371: }
                   8372:
                   8373: /* for Fp, 2<=p<2^16 */
                   8374:
                   8375: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   8376:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   8377: {
                   8378:     int spcol,sprow,a;
                   8379:     int i,j,k,l,rank;
                   8380:     NODE r0,r;
                   8381:     ND_pairs sp;
                   8382:     ND spol;
                   8383:     UINT **spmat;
                   8384:     UINT *svect,*v;
                   8385:     int *colstat;
                   8386:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8387:     int maxrs;
                   8388:     int *spsugar;
                   8389:     ND_pairs *spactive;
                   8390:
                   8391:     spcol = col-nred;
                   8392:     get_eg(&eg0);
                   8393:     /* elimination (1st step) */
                   8394:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   8395:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   8396:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8397:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   8398:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8399:         nd_sp(m,0,sp,&spol);
                   8400:         if ( !spol ) continue;
                   8401:         nd_to_vect(m,s0vect,col,spol,svect);
                   8402:         if ( m == -1 )
                   8403:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   8404:         else
                   8405:             maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
                   8406:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   8407:         if ( i < col ) {
                   8408:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   8409:             for ( j = k = 0; j < col; j++ )
                   8410:                 if ( !rhead[j] ) v[k++] = svect[j];
                   8411:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8412:             if ( nz )
                   8413:             spactive[sprow] = sp;
                   8414:             sprow++;
                   8415:         }
                   8416:         nd_free(spol);
                   8417:     }
                   8418:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8419:     if ( DP_Print ) {
1.5       noro     8420:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8421:         fflush(asir_out);
                   8422:     }
                   8423:     /* free index arrays */
                   8424:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   8425:
                   8426:     /* elimination (2nd step) */
                   8427:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8428:     if ( m == -1 )
                   8429:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   8430:     else
                   8431:         rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   8432:     r0 = 0;
                   8433:     for ( i = 0; i < rank; i++ ) {
                   8434:         NEXTNODE(r0,r); BDY(r) =
                   8435:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   8436:         SG((NDV)BDY(r)) = spsugar[i];
                   8437:         GCFREE(spmat[i]);
                   8438:     }
                   8439:     if ( r0 ) NEXT(r) = 0;
                   8440:
                   8441:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   8442:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8443:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8444:     if ( DP_Print ) {
1.5       noro     8445:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8446:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8447:             nsp,nred,sprow,spcol,rank);
1.5       noro     8448:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     8449:     }
                   8450:     if ( nz ) {
                   8451:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   8452:         if ( rank > 0 ) {
                   8453:             NEXT(spactive[rank-1]) = 0;
                   8454:             *nz = spactive[0];
                   8455:         } else
                   8456:             *nz = 0;
                   8457:     }
                   8458:     return r0;
                   8459: }
                   8460:
                   8461:
                   8462: /* for small finite fields */
                   8463:
                   8464: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   8465:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   8466: {
                   8467:     int spcol,sprow,a;
                   8468:     int i,j,k,l,rank;
                   8469:     NODE r0,r;
                   8470:     ND_pairs sp;
                   8471:     ND spol;
                   8472:     UINT **spmat;
                   8473:     UINT *svect,*v;
                   8474:     int *colstat;
                   8475:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8476:     int maxrs;
                   8477:     int *spsugar;
                   8478:     ND_pairs *spactive;
                   8479:
                   8480:     spcol = col-nred;
                   8481:     get_eg(&eg0);
                   8482:     /* elimination (1st step) */
                   8483:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   8484:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   8485:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8486:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   8487:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8488:         nd_sp(m,0,sp,&spol);
                   8489:         if ( !spol ) continue;
                   8490:         nd_to_vect(m,s0vect,col,spol,svect);
                   8491:         maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   8492:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   8493:         if ( i < col ) {
                   8494:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   8495:             for ( j = k = 0; j < col; j++ )
                   8496:                 if ( !rhead[j] ) v[k++] = svect[j];
                   8497:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8498:             if ( nz )
                   8499:             spactive[sprow] = sp;
                   8500:             sprow++;
                   8501:         }
                   8502:         nd_free(spol);
                   8503:     }
                   8504:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8505:     if ( DP_Print ) {
1.5       noro     8506:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8507:         fflush(asir_out);
                   8508:     }
                   8509:     /* free index arrays */
                   8510:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   8511:
                   8512:     /* elimination (2nd step) */
                   8513:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8514:     rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   8515:     r0 = 0;
                   8516:     for ( i = 0; i < rank; i++ ) {
                   8517:         NEXTNODE(r0,r); BDY(r) =
                   8518:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   8519:         SG((NDV)BDY(r)) = spsugar[i];
                   8520:         GCFREE(spmat[i]);
                   8521:     }
                   8522:     if ( r0 ) NEXT(r) = 0;
                   8523:
                   8524:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   8525:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8526:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8527:     if ( DP_Print ) {
1.5       noro     8528:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8529:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8530:             nsp,nred,sprow,spcol,rank);
1.5       noro     8531:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     8532:     }
                   8533:     if ( nz ) {
                   8534:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   8535:         if ( rank > 0 ) {
                   8536:             NEXT(spactive[rank-1]) = 0;
                   8537:             *nz = spactive[0];
                   8538:         } else
                   8539:             *nz = 0;
                   8540:     }
                   8541:     return r0;
                   8542: }
                   8543:
                   8544: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   8545:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   8546: {
                   8547:     int spcol,sprow,a;
                   8548:     int i,j,k,l,rank;
                   8549:     NODE r0,r;
                   8550:     ND_pairs sp;
                   8551:     ND spol;
                   8552:     mpz_t **spmat;
                   8553:     mpz_t *svect,*v;
                   8554:     int *colstat;
                   8555:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8556:     int maxrs;
                   8557:     int *spsugar;
                   8558:     pointer *w;
                   8559:
                   8560:     spcol = col-nred;
                   8561:     get_eg(&eg0);
                   8562:     /* elimination (1st step) */
                   8563:     spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
                   8564:     svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   8565:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8566:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8567:         nd_sp(m,trace,sp,&spol);
                   8568:         if ( !spol ) continue;
                   8569:         nd_to_vect_lf(s0vect,col,spol,svect);
                   8570:         maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
                   8571:         for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
                   8572:         if ( i < col ) {
                   8573:             spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
                   8574:             for ( j = k = 0; j < col; j++ )
                   8575:                 if ( !rhead[j] ) v[k++][0] = svect[j][0];
                   8576:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8577:             sprow++;
                   8578:         }
                   8579: /*        nd_free(spol); */
                   8580:     }
                   8581:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8582:     if ( DP_Print ) {
1.5       noro     8583:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8584:         fflush(asir_out);
                   8585:     }
                   8586:     /* free index arrays */
                   8587: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   8588:
                   8589:     /* elimination (2nd step) */
                   8590:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8591:     rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
                   8592:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   8593:     for ( i = 0; i < rank; i++ ) {
                   8594: #if 0
                   8595:         w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   8596:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   8597: #else
                   8598:         w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   8599:         SG((NDV)w[i]) = spsugar[i];
                   8600: #endif
                   8601: /*        GCFREE(spmat[i]); */
                   8602:
                   8603:     }
                   8604: #if 0
                   8605:     qsort(w,rank,sizeof(NDV),
                   8606:         (int (*)(const void *,const void *))ndv_compare);
                   8607: #endif
                   8608:     r0 = 0;
                   8609:     for ( i = 0; i < rank; i++ ) {
                   8610:         NEXTNODE(r0,r); BDY(r) = w[i];
                   8611:     }
                   8612:     if ( r0 ) NEXT(r) = 0;
                   8613:
                   8614: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   8615:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8616:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8617:     if ( DP_Print ) {
1.5       noro     8618:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8619:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8620:             nsp,nred,sprow,spcol,rank);
1.5       noro     8621:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     8622:     }
                   8623:     return r0;
                   8624: }
                   8625:
                   8626: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   8627:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   8628: {
                   8629:     int spcol,sprow,a;
                   8630:     int i,j,k,l,rank;
                   8631:     NODE r0,r;
                   8632:     ND_pairs sp;
                   8633:     ND spol;
                   8634:     Z **spmat;
                   8635:     Z *svect,*v;
                   8636:     int *colstat;
                   8637:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8638:     int maxrs;
                   8639:     int *spsugar;
                   8640:     pointer *w;
                   8641:
                   8642:     spcol = col-nred;
                   8643:     get_eg(&eg0);
                   8644:     /* elimination (1st step) */
                   8645:     spmat = (Z **)MALLOC(nsp*sizeof(Q *));
                   8646:     svect = (Z *)MALLOC(col*sizeof(Q));
                   8647:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8648:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8649:         nd_sp(0,trace,sp,&spol);
                   8650:         if ( !spol ) continue;
                   8651:         nd_to_vect_q(s0vect,col,spol,svect);
                   8652:         maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
                   8653:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   8654:         if ( i < col ) {
                   8655:             spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
                   8656:             for ( j = k = 0; j < col; j++ )
                   8657:                 if ( !rhead[j] ) v[k++] = svect[j];
                   8658:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8659:             sprow++;
                   8660:         }
                   8661: /*        nd_free(spol); */
                   8662:     }
                   8663:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8664:     if ( DP_Print ) {
1.5       noro     8665:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8666:         fflush(asir_out);
                   8667:     }
                   8668:     /* free index arrays */
                   8669: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   8670:
                   8671:     /* elimination (2nd step) */
                   8672:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8673:     rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
                   8674:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   8675:     for ( i = 0; i < rank; i++ ) {
                   8676: #if 0
                   8677:         w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   8678:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   8679: #else
                   8680:         w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   8681:         SG((NDV)w[i]) = spsugar[i];
                   8682: #endif
                   8683: /*        GCFREE(spmat[i]); */
                   8684:     }
                   8685: #if 0
                   8686:     qsort(w,rank,sizeof(NDV),
                   8687:         (int (*)(const void *,const void *))ndv_compare);
                   8688: #endif
                   8689:     r0 = 0;
                   8690:     for ( i = 0; i < rank; i++ ) {
                   8691:         NEXTNODE(r0,r); BDY(r) = w[i];
                   8692:     }
                   8693:     if ( r0 ) NEXT(r) = 0;
                   8694:
                   8695: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   8696:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8697:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8698:     if ( DP_Print ) {
1.5       noro     8699:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8700:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8701:             nsp,nred,sprow,spcol,rank);
1.5       noro     8702:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     8703:     }
                   8704:     return r0;
                   8705: }
                   8706:
                   8707: FILE *nd_write,*nd_read;
                   8708:
                   8709: void nd_send_int(int a) {
                   8710:     write_int(nd_write,(unsigned int *)&a);
                   8711: }
                   8712:
                   8713: void nd_send_intarray(int *p,int len) {
                   8714:     write_intarray(nd_write,(unsigned int *)p,len);
                   8715: }
                   8716:
                   8717: int nd_recv_int() {
                   8718:     int a;
                   8719:
                   8720:     read_int(nd_read,(unsigned int *)&a);
                   8721:     return a;
                   8722: }
                   8723:
                   8724: void nd_recv_intarray(int *p,int len) {
                   8725:     read_intarray(nd_read,(unsigned int *)p,len);
                   8726: }
                   8727:
                   8728: void nd_send_ndv(NDV p) {
                   8729:     int len,i;
                   8730:     NMV m;
                   8731:
                   8732:     if ( !p ) nd_send_int(0);
                   8733:     else {
                   8734:         len = LEN(p);
                   8735:         nd_send_int(len);
                   8736:         m = BDY(p);
                   8737:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   8738:             nd_send_int(CM(m));
                   8739:             nd_send_intarray((int *)DL(m),nd_wpd);
                   8740:         }
                   8741:     }
                   8742: }
                   8743:
                   8744: void nd_send_nd(ND p) {
                   8745:     int len,i;
                   8746:     NM m;
                   8747:
                   8748:     if ( !p ) nd_send_int(0);
                   8749:     else {
                   8750:         len = LEN(p);
                   8751:         nd_send_int(len);
                   8752:         m = BDY(p);
                   8753:         for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   8754:             nd_send_int(CM(m));
                   8755:             nd_send_intarray((int *)DL(m),nd_wpd);
                   8756:         }
                   8757:     }
                   8758: }
                   8759:
                   8760: NDV nd_recv_ndv()
                   8761: {
                   8762:     int len,i;
                   8763:     NMV m,m0;
                   8764:     NDV r;
                   8765:
                   8766:     len = nd_recv_int();
                   8767:     if ( !len ) return 0;
                   8768:     else {
                   8769:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8770: #if 0
                   8771:         ndv_alloc += len*nmv_adv;
                   8772: #endif
                   8773:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   8774:             CM(m) = nd_recv_int();
                   8775:             nd_recv_intarray((int *)DL(m),nd_wpd);
                   8776:         }
                   8777:         MKNDV(nd_nvar,m0,len,r);
                   8778:         return r;
                   8779:     }
                   8780: }
                   8781:
                   8782: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
                   8783: {
                   8784:     int i,j,t,c,rank,inv;
                   8785:     int *ci,*ri;
                   8786:     Z dn;
                   8787:     MAT m,nm;
                   8788:
                   8789:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
                   8790:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   8791:     for ( i = 0; i < row; i++ )
                   8792:         for ( j = 0; j < col; j++ )
                   8793:             mat0[i][j] = 0;
                   8794:     c = col-rank;
                   8795:     for ( i = 0; i < rank; i++ ) {
                   8796:         mat0[i][ri[i]] = dn;
                   8797:         for ( j = 0; j < c; j++ )
                   8798:             mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
                   8799:     }
                   8800:     return rank;
                   8801: }
                   8802:
                   8803: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   8804: {
                   8805:     int i,j,k,l,inv,a,rank,s;
                   8806:     unsigned int *t,*pivot,*pk;
                   8807:     unsigned int **mat;
                   8808:     ND_pairs pair;
                   8809:
                   8810:     mat = (unsigned int **)mat0;
                   8811:     for ( rank = 0, j = 0; j < col; j++ ) {
                   8812:         for ( i = rank; i < row; i++ )
                   8813:             mat[i][j] %= md;
                   8814:         for ( i = rank; i < row; i++ )
                   8815:             if ( mat[i][j] )
                   8816:                 break;
                   8817:         if ( i == row ) {
                   8818:             colstat[j] = 0;
                   8819:             continue;
                   8820:         } else
                   8821:             colstat[j] = 1;
                   8822:         if ( i != rank ) {
                   8823:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   8824:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   8825:             if ( spactive ) {
                   8826:                 pair = spactive[i]; spactive[i] = spactive[rank];
                   8827:                 spactive[rank] = pair;
                   8828:             }
                   8829:         }
                   8830:         pivot = mat[rank];
                   8831:         s = sugar[rank];
                   8832:         inv = invm(pivot[j],md);
                   8833:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   8834:             if ( *pk ) {
                   8835:                 if ( *pk >= (unsigned int)md )
                   8836:                     *pk %= md;
                   8837:                 DMAR(*pk,inv,0,md,*pk)
                   8838:             }
                   8839:         for ( i = rank+1; i < row; i++ ) {
                   8840:             t = mat[i];
                   8841:             if ( (a = t[j]) != 0 ) {
                   8842:                 sugar[i] = MAX(sugar[i],s);
                   8843:                 red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   8844:             }
                   8845:         }
                   8846:         rank++;
                   8847:     }
                   8848:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   8849:         if ( colstat[j] ) {
                   8850:             pivot = mat[l];
                   8851:             s = sugar[l];
                   8852:             for ( i = 0; i < l; i++ ) {
                   8853:                 t = mat[i];
                   8854:                 t[j] %= md;
                   8855:                 if ( (a = t[j]) != 0 ) {
                   8856:                     sugar[i] = MAX(sugar[i],s);
                   8857:                     red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   8858:                 }
                   8859:             }
                   8860:             l--;
                   8861:         }
                   8862:     for ( j = 0, l = 0; l < rank; j++ )
                   8863:         if ( colstat[j] ) {
                   8864:             t = mat[l];
                   8865:             for ( k = j; k < col; k++ )
                   8866:                 if ( t[k] >= (unsigned int)md )
                   8867:                     t[k] %= md;
                   8868:             l++;
                   8869:         }
                   8870:     return rank;
                   8871: }
                   8872:
                   8873:
1.7       noro     8874: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1       noro     8875: {
1.7       noro     8876:     int i,j,k,l,inv,a,rank,s;
                   8877:     unsigned int *t,*pivot,*pk;
                   8878:     unsigned int **mat;
                   8879:
                   8880:     mat = (unsigned int **)mat0;
                   8881:     for ( rank = 0, j = 0; j < col; j++ ) {
                   8882:         for ( i = rank; i < row; i++ )
                   8883:             if ( mat[i][j] )
                   8884:                 break;
                   8885:         if ( i == row ) {
                   8886:             colstat[j] = 0;
                   8887:             continue;
                   8888:         } else
                   8889:             colstat[j] = 1;
                   8890:         if ( i != rank ) {
                   8891:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   8892:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   8893:         }
                   8894:         pivot = mat[rank];
                   8895:         s = sugar[rank];
                   8896:         inv = _invsf(pivot[j]);
                   8897:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   8898:             if ( *pk )
                   8899:                 *pk = _mulsf(*pk,inv);
                   8900:         for ( i = rank+1; i < row; i++ ) {
                   8901:             t = mat[i];
                   8902:             if ( (a = t[j]) != 0 ) {
                   8903:                 sugar[i] = MAX(sugar[i],s);
                   8904:                 red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   8905:             }
                   8906:         }
                   8907:         rank++;
                   8908:     }
                   8909:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   8910:         if ( colstat[j] ) {
                   8911:             pivot = mat[l];
                   8912:             s = sugar[l];
                   8913:             for ( i = 0; i < l; i++ ) {
                   8914:                 t = mat[i];
                   8915:                 if ( (a = t[j]) != 0 ) {
                   8916:                     sugar[i] = MAX(sugar[i],s);
                   8917:                     red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   8918:                 }
                   8919:             }
                   8920:             l--;
                   8921:         }
                   8922:     return rank;
                   8923: }
1.1       noro     8924:
1.7       noro     8925: int ndv_ishomo(NDV p)
                   8926: {
                   8927:     NMV m;
                   8928:     int len,h;
1.1       noro     8929:
                   8930:     if ( !p ) return 1;
                   8931:     len = LEN(p);
                   8932:     m = BDY(p);
                   8933:     h = TD(DL(m));
                   8934:     NMV_ADV(m);
                   8935:     for ( len--; len; len--, NMV_ADV(m) )
1.20      noro     8936:         if ( TD(DL(m)) != h ) {
                   8937:           return 0;
                   8938:         }
1.1       noro     8939:     return 1;
                   8940: }
                   8941:
                   8942: void ndv_save(NDV p,int index)
                   8943: {
                   8944:     FILE *s;
                   8945:     char name[BUFSIZ];
                   8946:     short id;
                   8947:     int nv,sugar,len,n,i,td,e,j;
                   8948:     NMV m;
                   8949:     unsigned int *dl;
                   8950:     int mpos;
                   8951:
                   8952:     sprintf(name,"%s/%d",Demand,index);
                   8953:     s = fopen(name,"w");
                   8954:     savevl(s,0);
                   8955:     if ( !p ) {
                   8956:         saveobj(s,0);
                   8957:         return;
                   8958:     }
                   8959:     id = O_DP;
                   8960:     nv = NV(p);
                   8961:     sugar = SG(p);
                   8962:     len = LEN(p);
                   8963:     write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
                   8964:     write_int(s,(unsigned int *)&len);
                   8965:
                   8966:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     8967:         saveobj(s,(Obj)CZ(m));
1.1       noro     8968:         dl = DL(m);
                   8969:         td = TD(dl);
                   8970:         write_int(s,(unsigned int *)&td);
                   8971:         for ( j = 0; j < nv; j++ ) {
                   8972:             e = GET_EXP(dl,j);
                   8973:             write_int(s,(unsigned int *)&e);
                   8974:         }
                   8975:         if ( nd_module ) {
                   8976:             mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
                   8977:         }
                   8978:     }
                   8979:     fclose(s);
                   8980: }
                   8981:
                   8982: void nd_save_mod(ND p,int index)
                   8983: {
                   8984:     FILE *s;
                   8985:     char name[BUFSIZ];
                   8986:     int nv,sugar,len,c;
                   8987:     NM m;
                   8988:
                   8989:     sprintf(name,"%s/%d",Demand,index);
                   8990:     s = fopen(name,"w");
                   8991:     if ( !p ) {
                   8992:     len = 0;
                   8993:       write_int(s,(unsigned int *)&len);
                   8994:     fclose(s);
                   8995:         return;
                   8996:     }
                   8997:     nv = NV(p);
                   8998:     sugar = SG(p);
                   8999:     len = LEN(p);
                   9000:     write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
                   9001:   for ( m = BDY(p); m; m = NEXT(m) ) {
                   9002:     c = CM(m); write_int(s,(unsigned int *)&c);
                   9003:     write_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   9004:   }
                   9005:     fclose(s);
                   9006: }
                   9007:
                   9008: NDV ndv_load(int index)
                   9009: {
                   9010:     FILE *s;
                   9011:     char name[BUFSIZ];
                   9012:     short id;
                   9013:     int nv,sugar,len,n,i,td,e,j;
                   9014:     NDV d;
                   9015:     NMV m0,m;
                   9016:     unsigned int *dl;
                   9017:     Obj obj;
                   9018:     int mpos;
                   9019:
                   9020:     sprintf(name,"%s/%d",Demand,index);
                   9021:     s = fopen(name,"r");
                   9022:     if ( !s ) return 0;
                   9023:
                   9024:     skipvl(s);
                   9025:     read_short(s,(unsigned short *)&id);
                   9026:     if ( !id ) return 0;
                   9027:     read_int(s,(unsigned int *)&nv);
                   9028:     read_int(s,(unsigned int *)&sugar);
                   9029:     read_int(s,(unsigned int *)&len);
                   9030:
                   9031:     m0 = m = MALLOC(len*nmv_adv);
                   9032:     for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     9033:         loadobj(s,&obj); CZ(m) = (Z)obj;
1.1       noro     9034:         dl = DL(m);
                   9035:         ndl_zero(dl);
                   9036:         read_int(s,(unsigned int *)&td); TD(dl) = td;
                   9037:         for ( j = 0; j < nv; j++ ) {
                   9038:             read_int(s,(unsigned int *)&e);
                   9039:             PUT_EXP(dl,j,e);
                   9040:         }
                   9041:         if ( nd_module ) {
                   9042:             read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
                   9043:         }
                   9044:         if ( nd_blockmask ) ndl_weight_mask(dl);
                   9045:     }
                   9046:     fclose(s);
                   9047:     MKNDV(nv,m0,len,d);
                   9048:     SG(d) = sugar;
                   9049:     return d;
                   9050: }
                   9051:
                   9052: ND nd_load_mod(int index)
                   9053: {
                   9054:     FILE *s;
                   9055:     char name[BUFSIZ];
                   9056:     int nv,sugar,len,i,c;
                   9057:   ND d;
                   9058:     NM m0,m;
                   9059:
                   9060:     sprintf(name,"%s/%d",Demand,index);
                   9061:     s = fopen(name,"r");
                   9062:   /* if the file does not exist, it means p[index]=0 */
                   9063:     if ( !s ) return 0;
                   9064:
                   9065:     read_int(s,(unsigned int *)&nv);
                   9066:   if ( !nv ) { fclose(s); return 0; }
                   9067:
                   9068:     read_int(s,(unsigned int *)&sugar);
                   9069:     read_int(s,(unsigned int *)&len);
                   9070:   for ( m0 = 0, i = 0; i < len; i++ ) {
                   9071:     NEXTNM(m0,m);
                   9072:     read_int(s,(unsigned int *)&c); CM(m) = c;
                   9073:     read_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   9074:   }
                   9075:   NEXT(m) = 0;
                   9076:     MKND(nv,m0,len,d);
                   9077:     SG(d) = sugar;
                   9078:   fclose(s);
                   9079:     return d;
                   9080: }
                   9081:
                   9082: void nd_det(int mod,MAT f,P *rp)
                   9083: {
                   9084:     VL fv,tv;
                   9085:     int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
                   9086:     pointer **m;
                   9087:     P **w;
                   9088:     P mp,r;
                   9089:     NDV **dm;
                   9090:     NDV *t,*mi,*mj;
                   9091:     NDV d,s,mij,mjj;
                   9092:     ND u;
                   9093:     NMV nmv;
                   9094:     UINT *bound;
                   9095:     PGeoBucket bucket;
                   9096:     struct order_spec *ord;
                   9097:     Z dq,dt,ds;
                   9098:     Z mone;
                   9099:     Z gn,qn,dn0,nm,dn;
                   9100:
                   9101:     create_order_spec(0,0,&ord);
                   9102:     nd_init_ord(ord);
                   9103:     get_vars((Obj)f,&fv);
                   9104:     if ( f->row != f->col )
                   9105:         error("nd_det : non-square matrix");
                   9106:     n = f->row;
                   9107:     m = f->body;
                   9108:     for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   9109:
                   9110:     if ( !nvar ) {
                   9111:         if ( !mod )
                   9112:             detp(CO,(P **)m,n,rp);
                   9113:         else {
                   9114:             w = (P **)almat_pointer(n,n);
                   9115:             for ( i = 0; i < n; i++ )
                   9116:                 for ( j = 0; j < n; j++ )
                   9117:                     ptomp(mod,(P)m[i][j],&w[i][j]);
                   9118:             detmp(CO,mod,w,n,&mp);
                   9119:             mptop(mp,rp);
                   9120:         }
                   9121:         return;
                   9122:     }
                   9123:
                   9124:     if ( !mod ) {
                   9125:         w = (P **)almat_pointer(n,n);
                   9126:         dq = ONE;
                   9127:         for ( i = 0; i < n; i++ ) {
                   9128:             dn0 = ONE;
                   9129:             for ( j = 0; j < n; j++ ) {
                   9130:                 if ( !m[i][j] ) continue;
                   9131:                 lgp(m[i][j],&nm,&dn);
1.6       noro     9132:                 gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1       noro     9133:             }
                   9134:             if ( !UNIZ(dn0) ) {
                   9135:                 ds = dn0;
                   9136:                 for ( j = 0; j < n; j++ )
                   9137:                     mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   9138:                 mulz(dq,ds,&dt); dq = dt;
                   9139:             } else
                   9140:                 for ( j = 0; j < n; j++ )
                   9141:                     w[i][j] = (P)m[i][j];
                   9142:         }
                   9143:         m = (pointer **)w;
                   9144:     }
                   9145:
                   9146:     for ( i = 0, max = 1; i < n; i++ )
                   9147:         for ( j = 0; j < n; j++ )
                   9148:             for ( tv = fv; tv; tv = NEXT(tv) ) {
                   9149:                 e = getdeg(tv->v,(P)m[i][j]);
                   9150:                 max = MAX(e,max);
                   9151:             }
                   9152:     nd_setup_parameters(nvar,max);
                   9153:     dm = (NDV **)almat_pointer(n,n);
                   9154:     for ( i = 0, max = 1; i < n; i++ )
                   9155:         for ( j = 0; j < n; j++ ) {
                   9156:             dm[i][j] = ptondv(CO,fv,m[i][j]);
                   9157:             if ( mod ) ndv_mod(mod,dm[i][j]);
                   9158:             if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   9159:         }
                   9160:     d = ptondv(CO,fv,(P)ONE);
                   9161:     if ( mod ) ndv_mod(mod,d);
                   9162:     chsgnz(ONE,&mone);
                   9163:     for ( j = 0, sgn = 1; j < n; j++ ) {
                   9164:       if ( DP_Print ) {
                   9165:         fprintf(asir_out,".");
                   9166:       }
                   9167:         for ( i = j; i < n && !dm[i][j]; i++ );
                   9168:         if ( i == n ) {
                   9169:             *rp = 0;
                   9170:             return;
                   9171:         }
                   9172:         k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
                   9173:         for ( k = j; k < n; k++ )
                   9174:             for ( l = j; l < n; l++ )
                   9175:                 if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   9176:                     k0 = k; l0 = l; len0 = LEN(dm[k][l]);
                   9177:                 }
                   9178:         if ( k0 != j ) {
                   9179:             t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   9180:             sgn = -sgn;
                   9181:         }
                   9182:         if ( l0 != j ) {
                   9183:             for ( k = j; k < n; k++ ) {
                   9184:                 s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   9185:             }
                   9186:             sgn = -sgn;
                   9187:         }
                   9188:         bound = nd_det_compute_bound(dm,n,j);
                   9189:         for ( k = 0; k < nd_nvar; k++ )
                   9190:             if ( bound[k]*2 > nd_mask0 ) break;
                   9191:         if ( k < nd_nvar )
                   9192:             nd_det_reconstruct(dm,n,j,d);
                   9193:
                   9194:         for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
                   9195: /*            if ( DP_Print ) fprintf(asir_out,"    i=%d\n        ",i); */
                   9196:             mi = dm[i]; mij = mi[j];
                   9197:             if ( mod )
                   9198:                 ndv_mul_c(mod,mij,mod-1);
                   9199:             else
                   9200:                 ndv_mul_c_q(mij,mone);
                   9201:             for ( k = j+1; k < n; k++ ) {
                   9202: /*                if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
                   9203:                 bucket = create_pbucket();
                   9204:                 if ( mi[k] ) {
                   9205:                     nmv = BDY(mjj); len = LEN(mjj);
                   9206:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   9207:                         u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   9208:                         add_pbucket(mod,bucket,u);
                   9209:                     }
                   9210:                 }
                   9211:                 if ( mj[k] && mij ) {
                   9212:                     nmv = BDY(mij); len = LEN(mij);
                   9213:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   9214:                         u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   9215:                         add_pbucket(mod,bucket,u);
                   9216:                     }
                   9217:                 }
                   9218:                 u = nd_quo(mod,bucket,d);
                   9219:                 mi[k] = ndtondv(mod,u);
                   9220:             }
                   9221: /*            if ( DP_Print ) fprintf(asir_out,"\n",k); */
                   9222:         }
                   9223:         d = mjj;
                   9224:     }
                   9225:     if ( DP_Print ) {
                   9226:       fprintf(asir_out,"\n");
                   9227:     }
                   9228:     if ( sgn < 0 ) {
                   9229:         if ( mod )
                   9230:             ndv_mul_c(mod,d,mod-1);
                   9231:         else
                   9232:             ndv_mul_c_q(d,mone);
                   9233:     }
                   9234:     r = ndvtop(mod,CO,fv,d);
                   9235:     if ( !mod && !UNIQ(dq) )
                   9236:         divsp(CO,r,(P)dq,rp);
                   9237:     else
                   9238:         *rp = r;
                   9239: }
                   9240:
                   9241: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
                   9242: {
                   9243:     NM mr,mr0;
                   9244:     NM tnm;
                   9245:     NMV m;
                   9246:     UINT *d0,*dt,*dm;
                   9247:     int c,n,td,i,c1,c2,len;
                   9248:     Z q;
                   9249:     ND r;
                   9250:
                   9251:     if ( !p ) return 0;
                   9252:     else {
                   9253:         n = NV(p); m = BDY(p); len = LEN(p);
                   9254:         d0 = DL(m0);
                   9255:         td = TD(d);
                   9256:         mr0 = 0;
                   9257:         NEWNM(tnm);
                   9258:         if ( mod ) {
                   9259:             c = CM(m0);
                   9260:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9261:                 ndl_add(DL(m),d0,DL(tnm));
                   9262:                 if ( ndl_reducible(DL(tnm),d) ) {
                   9263:                     NEXTNM(mr0,mr);
                   9264:                     c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
                   9265:                     ndl_copy(DL(tnm),DL(mr));
                   9266:                 }
                   9267:             }
                   9268:         } else {
1.6       noro     9269:             q = CZ(m0);
1.1       noro     9270:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9271:                 ndl_add(DL(m),d0,DL(tnm));
                   9272:                 if ( ndl_reducible(DL(tnm),d) ) {
                   9273:                     NEXTNM(mr0,mr);
1.6       noro     9274:                     mulz(CZ(m),q,&CZ(mr));
1.1       noro     9275:                     ndl_copy(DL(tnm),DL(mr));
                   9276:                 }
                   9277:             }
                   9278:         }
                   9279:         if ( !mr0 )
                   9280:             return 0;
                   9281:         else {
                   9282:             NEXT(mr) = 0;
                   9283:             for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   9284:             MKND(NV(p),mr0,len,r);
                   9285:             SG(r) = SG(p) + TD(d0);
                   9286:             return r;
                   9287:         }
                   9288:     }
                   9289: }
                   9290:
                   9291: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   9292: {
                   9293:     int i,obpe,oadv,h,k,l;
                   9294:     static NM prev_nm_free_list;
                   9295:     EPOS oepos;
                   9296:
                   9297:     obpe = nd_bpe;
                   9298:     oadv = nmv_adv;
                   9299:     oepos = nd_epos;
                   9300:     if ( obpe < 2 ) nd_bpe = 2;
                   9301:     else if ( obpe < 3 ) nd_bpe = 3;
                   9302:     else if ( obpe < 4 ) nd_bpe = 4;
                   9303:     else if ( obpe < 5 ) nd_bpe = 5;
                   9304:     else if ( obpe < 6 ) nd_bpe = 6;
                   9305:     else if ( obpe < 8 ) nd_bpe = 8;
                   9306:     else if ( obpe < 10 ) nd_bpe = 10;
                   9307:     else if ( obpe < 16 ) nd_bpe = 16;
                   9308:     else if ( obpe < 32 ) nd_bpe = 32;
                   9309:     else error("nd_det_reconstruct : exponent too large");
                   9310:
                   9311:     nd_setup_parameters(nd_nvar,0);
                   9312:     prev_nm_free_list = _nm_free_list;
                   9313:     _nm_free_list = 0;
                   9314:     for ( k = j; k < n; k++ )
                   9315:         for (l = j; l < n; l++ )
                   9316:             ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   9317:     ndv_realloc(d,obpe,oadv,oepos);
                   9318:     prev_nm_free_list = 0;
                   9319: #if 0
                   9320:     GC_gcollect();
                   9321: #endif
                   9322: }
                   9323:
                   9324: /* returns a UINT array containing degree bounds */
                   9325:
                   9326: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   9327: {
                   9328:     UINT *d0,*d1,*d,*t,*r;
                   9329:     int k,l,i;
                   9330:
                   9331:     d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
                   9332:     for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
                   9333:     for ( k = j; k < n; k++ )
                   9334:         for ( l = j; l < n; l++ )
                   9335:             if ( dm[k][l] ) {
                   9336:                 d = ndv_compute_bound(dm[k][l]);
                   9337:                 for ( i = 0; i < nd_nvar; i++ )
                   9338:                     d0[i] = MAX(d0[i],d[i]);
                   9339:             }
                   9340:     return d0;
                   9341: }
                   9342:
                   9343: DL nd_separate_d(UINT *d,UINT *trans)
                   9344: {
                   9345:     int n,td,i,e,j;
                   9346:     DL a;
                   9347:
                   9348:     ndl_zero(trans);
                   9349:     td = 0;
                   9350:     for ( i = 0; i < nd_ntrans; i++ ) {
                   9351:         e = GET_EXP(d,i);
                   9352:         PUT_EXP(trans,i,e);
                   9353:         td += MUL_WEIGHT(e,i);
                   9354:     }
                   9355:     if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   9356:         /* homogenized */
                   9357:         i = nd_nvar-1;
                   9358:         e = GET_EXP(d,i);
                   9359:         PUT_EXP(trans,i,e);
                   9360:         td += MUL_WEIGHT(e,i);
                   9361:     }
                   9362:     TD(trans) = td;
                   9363:     if ( nd_blockmask) ndl_weight_mask(trans);
                   9364:     NEWDL(a,nd_nalg);
                   9365:     td = 0;
                   9366:     for ( i = 0; i < nd_nalg; i++ ) {
                   9367:         j = nd_ntrans+i;
                   9368:         e = GET_EXP(d,j);
                   9369:         a->d[i] = e;
                   9370:         td += e;
                   9371:     }
                   9372:     a->td = td;
                   9373:     return a;
                   9374: }
                   9375:
                   9376: int nd_monic(int mod,ND *p)
                   9377: {
                   9378:     UINT *trans,*t;
                   9379:     DL alg;
                   9380:     MP mp0,mp;
                   9381:     NM m,m0,m1,ma0,ma,mb,mr0,mr;
                   9382:     ND r;
                   9383:     DL dl;
                   9384:     DP nm;
                   9385:     NDV ndv;
                   9386:     DAlg inv,cd;
                   9387:     ND s,c;
                   9388:     Z l,mul;
                   9389:     Z ln;
                   9390:     int n,ntrans,i,e,td,is_lc,len;
                   9391:     NumberField nf;
                   9392:     struct oEGT eg0,eg1;
                   9393:
                   9394:     if ( !(nf = get_numberfield()) )
                   9395:         error("nd_monic : current_numberfield is not set");
                   9396:
                   9397:     /* Q coef -> DAlg coef */
                   9398:     NEWNM(ma0); ma = ma0;
                   9399:     m = BDY(*p);
                   9400:     is_lc = 1;
                   9401:     while ( 1 ) {
                   9402:         NEWMP(mp0); mp = mp0;
1.6       noro     9403:         mp->c = (Obj)CZ(m);
1.1       noro     9404:         mp->dl = nd_separate_d(DL(m),DL(ma));
                   9405:         NEWNM(mb);
                   9406:         for ( m = NEXT(m); m; m = NEXT(m) ) {
                   9407:             alg = nd_separate_d(DL(m),DL(mb));
                   9408:             if ( !ndl_equal(DL(ma),DL(mb)) )
                   9409:                 break;
1.6       noro     9410:             NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1       noro     9411:         }
                   9412:         NEXT(mp) = 0;
                   9413:         MKDP(nd_nalg,mp0,nm);
                   9414:         MKDAlg(nm,ONE,cd);
                   9415:         if ( is_lc == 1 ) {
                   9416:             /* if the lc is a rational number, we have nothing to do */
                   9417:             if ( !mp0->dl->td )
                   9418:                 return 1;
                   9419:
                   9420:             get_eg(&eg0);
                   9421:             invdalg(cd,&inv);
                   9422:             get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   9423:             /* check the validity of inv */
                   9424:             if ( mod && !remqi((Q)inv->dn,mod) )
                   9425:                 return 0;
                   9426:             CA(ma) = nf->one;
                   9427:             is_lc = 0;
                   9428:             ln = ONE;
                   9429:         } else {
                   9430:             muldalg(cd,inv,&CA(ma));
                   9431:             lcmz(ln,CA(ma)->dn,&ln);
                   9432:         }
                   9433:         if ( m ) {
                   9434:             NEXT(ma) = mb; ma = mb;
                   9435:         } else {
                   9436:             NEXT(ma) = 0;
                   9437:             break;
                   9438:         }
                   9439:     }
                   9440:     /* l = lcm(denoms) */
                   9441:     l = ln;
                   9442:     for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6       noro     9443:         divsz(l,CA(m)->dn,&mul);
1.1       noro     9444:         for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   9445:             NEXTNM(mr0,mr);
1.6       noro     9446:             mulz((Z)mp->c,mul,&CZ(mr));
1.1       noro     9447:             dl = mp->dl;
                   9448:             td = TD(DL(m));
                   9449:             ndl_copy(DL(m),DL(mr));
                   9450:             for ( i = 0; i < nd_nalg; i++ ) {
                   9451:                 e = dl->d[i];
                   9452:                 PUT_EXP(DL(mr),i+nd_ntrans,e);
                   9453:                 td += MUL_WEIGHT(e,i+nd_ntrans);
                   9454:             }
                   9455:             if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
                   9456:             TD(DL(mr)) = td;
                   9457:             if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   9458:         }
                   9459:     }
                   9460:     NEXT(mr) = 0;
                   9461:     for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   9462:     MKND(NV(*p),mr0,len,r);
                   9463:     /* XXX */
                   9464:     SG(r) = SG(*p);
                   9465:     nd_free(*p);
                   9466:     *p = r;
                   9467:     return 1;
                   9468: }
                   9469:
                   9470: NODE reverse_node(NODE n)
                   9471: {
                   9472:     NODE t,t1;
                   9473:
                   9474:     for ( t = 0; n; n = NEXT(n) ) {
                   9475:         MKNODE(t1,BDY(n),t); t = t1;
                   9476:     }
                   9477:     return t;
                   9478: }
                   9479:
                   9480: P ndc_div(int mod,union oNDC a,union oNDC b)
                   9481: {
                   9482:     union oNDC c;
                   9483:     int inv,t;
                   9484:
                   9485:     if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10      noro     9486:     else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1       noro     9487:     else if ( mod ) {
                   9488:         inv = invm(b.m,mod);
                   9489:         DMAR(a.m,inv,0,mod,t); c.m = t;
                   9490:     } else if ( nd_vc )
                   9491:        divsp(nd_vc,a.p,b.p,&c.p);
                   9492:     else
                   9493:        divsz(a.z,b.z,&c.z);
                   9494:     return ndctop(mod,c);
                   9495: }
                   9496:
                   9497: P ndctop(int mod,union oNDC c)
                   9498: {
                   9499:     Z q;
                   9500:     int e;
                   9501:     GFS gfs;
                   9502:
                   9503:     if ( mod == -1 ) {
                   9504:         e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
                   9505:     } else if ( mod == -2 ) {
1.10      noro     9506:        q = c.z; return (P)q;
1.1       noro     9507:     } else if ( mod > 0 ) {
1.6       noro     9508:         STOZ(c.m,q); return (P)q;
1.1       noro     9509:     } else
                   9510:         return (P)c.p;
                   9511: }
                   9512:
                   9513: /* [0,0,0,cont] = p -> p/cont */
                   9514:
                   9515: void finalize_tracelist(int i,P cont)
                   9516: {
                   9517:  LIST l;
                   9518:  NODE node;
                   9519:  Z iq;
                   9520:
                   9521:   if ( !UNIQ(cont) ) {
                   9522:     node = mknode(4,NULLP,NULLP,NULLP,cont);
                   9523:     MKLIST(l,node); MKNODE(node,l,nd_tracelist);
                   9524:     nd_tracelist = node;
                   9525:   }
1.6       noro     9526:   STOZ(i,iq);
1.1       noro     9527:   nd_tracelist = reverse_node(nd_tracelist);
                   9528:   MKLIST(l,nd_tracelist);
                   9529:   node = mknode(2,iq,l); MKLIST(l,node);
                   9530:   MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
                   9531:   nd_alltracelist = node; nd_tracelist = 0;
                   9532: }
                   9533:
                   9534: void conv_ilist(int demand,int trace,NODE g,int **indp)
                   9535: {
                   9536:     int n,i,j;
                   9537:   int *ind;
                   9538:   NODE t;
                   9539:
                   9540:     n = length(g);
                   9541:   ind = (int *)MALLOC(n*sizeof(int));
                   9542:   for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
                   9543:     j = (long)BDY(t); ind[i] = j;
                   9544:     BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
                   9545:   }
                   9546:   if ( indp ) *indp = ind;
                   9547: }
                   9548:
1.30      noro     9549: NODE conv_ilist_s(int demand,int trace,int **indp)
                   9550: {
                   9551:   int n,i,j;
                   9552:   int *ind;
                   9553:   NODE g0,g;
                   9554:
                   9555:   n = nd_psn;
                   9556:   ind = (int *)MALLOC(n*sizeof(int));
                   9557:   g0 = 0;
                   9558:   for ( i = 0; i < n; i++ ) {
                   9559:     ind[i] = i;
                   9560:     NEXTNODE(g0,g);
                   9561:     BDY(g) = (pointer)(demand?ndv_load(i):(trace?nd_ps_trace[i]:nd_ps[i]));
                   9562:   }
                   9563:   if ( g0 ) NEXT(g) = 0;
                   9564:   if ( indp ) *indp = ind;
                   9565:   return g0;
                   9566: }
                   9567:
1.1       noro     9568: void parse_nd_option(NODE opt)
                   9569: {
                   9570:     NODE t,p,u;
                   9571:   int i,s,n;
                   9572:     char *key;
                   9573:     Obj value;
                   9574:
                   9575:     nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
                   9576:   nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
                   9577:   nd_splist = 0; nd_check_splist = 0;
                   9578:     nd_sugarweight = 0;
                   9579:     nd_f4red =0;
                   9580:     nd_rank0 = 0;
                   9581:     for ( t = opt; t; t = NEXT(t) ) {
                   9582:         p = BDY((LIST)BDY(t));
                   9583:         key = BDY((STRING)BDY(p));
                   9584:         value = (Obj)BDY(NEXT(p));
                   9585:         if ( !strcmp(key,"gentrace") )
                   9586:             nd_gentrace = value?1:0;
                   9587:         else if ( !strcmp(key,"gensyz") )
                   9588:             nd_gensyz = value?1:0;
                   9589:         else if ( !strcmp(key,"nora") )
                   9590:             nd_nora = value?1:0;
                   9591:         else if ( !strcmp(key,"gbblock") ) {
                   9592:             if ( value && OID(value) == O_LIST ) {
                   9593:         u = BDY((LIST)value);
                   9594:               nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
                   9595:         for ( i = 0; u; u = NEXT(u) ) {
                   9596:           p = BDY((LIST)BDY(u));
1.6       noro     9597:           s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
                   9598:           nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1       noro     9599:         }
                   9600:         nd_gbblock[i] = -1;
                   9601:             } else
                   9602:               nd_gbblock = 0;
                   9603:     } else if ( !strcmp(key,"newelim") )
                   9604:             nd_newelim = value?1:0;
                   9605:     else if ( !strcmp(key,"intersect") )
                   9606:             nd_intersect = value?1:0;
1.17      noro     9607:     else if ( !strcmp(key,"syzgen") )
                   9608:             nd_intersect = ZTOS((Q)value);
1.1       noro     9609:     else if ( !strcmp(key,"lf") )
                   9610:             nd_lf = value?1:0;
                   9611:     else if ( !strcmp(key,"trace") ) {
                   9612:            if ( value ) {
                   9613:                u = BDY((LIST)value);
                   9614:            nd_nzlist = BDY((LIST)ARG2(u));
1.6       noro     9615:            nd_bpe = ZTOS((Q)ARG3(u));
1.1       noro     9616:            }
                   9617:     } else if ( !strcmp(key,"f4red") ) {
1.6       noro     9618:        nd_f4red = ZTOS((Q)value);
1.1       noro     9619:     } else if ( !strcmp(key,"rank0") ) {
                   9620:             nd_rank0 = value?1:0;
                   9621:     } else if ( !strcmp(key,"splist") ) {
                   9622:             nd_splist = value?1:0;
                   9623:     } else if ( !strcmp(key,"check_splist") ) {
                   9624:       nd_check_splist = BDY((LIST)value);
                   9625:     } else if ( !strcmp(key,"sugarweight") ) {
                   9626:       u = BDY((LIST)value);
                   9627:             n = length(u);
                   9628:             nd_sugarweight = MALLOC(n*sizeof(int));
                   9629:       for ( i = 0; i < n; i++, u = NEXT(u) )
1.6       noro     9630:                 nd_sugarweight[i] = ZTOS((Q)BDY(u));
1.1       noro     9631:     }
                   9632:     }
                   9633: }
                   9634:
                   9635: ND mdptond(DP d);
                   9636: ND nd_mul_nm(int mod,NM m0,ND p);
                   9637: ND nd_mul_nm_lf(NM m0,ND p);
                   9638: ND *btog(NODE ti,ND **p,int nb,int mod);
                   9639: ND btog_one(NODE ti,ND *p,int nb,int mod);
                   9640: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
                   9641: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
                   9642:
                   9643: /* d:monomial */
                   9644: ND mdptond(DP d)
                   9645: {
                   9646:   NM m;
                   9647:   ND r;
                   9648:
                   9649:   if ( OID(d) == 1 )
                   9650:   r = ptond(CO,CO,(P)d);
                   9651:   else {
                   9652:     NEWNM(m);
                   9653:     dltondl(NV(d),BDY(d)->dl,DL(m));
1.6       noro     9654:     CZ(m) = (Z)BDY(d)->c;
1.1       noro     9655:     NEXT(m) = 0;
                   9656:     MKND(NV(d),m,1,r);
                   9657:   }
                   9658:     return r;
                   9659: }
                   9660:
                   9661: ND nd_mul_nm(int mod,NM m0,ND p)
                   9662: {
                   9663:   UINT *d0;
                   9664:   int c0,c1,c;
                   9665:   NM tm,mr,mr0;
                   9666:   ND r;
                   9667:
                   9668:   if ( !p ) return 0;
                   9669:   d0 = DL(m0);
                   9670:   c0 = CM(m0);
                   9671:   mr0 = 0;
                   9672:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   9673:     NEXTNM(mr0,mr);
                   9674:   c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
                   9675:   ndl_add(d0,DL(tm),DL(mr));
                   9676:   }
                   9677:   NEXT(mr) = 0;
                   9678:   MKND(NV(p),mr0,LEN(p),r);
                   9679:   return r;
                   9680: }
                   9681:
                   9682: ND nd_mul_nm_lf(NM m0,ND p)
                   9683: {
                   9684:   UINT *d0;
                   9685:   Z c0,c1,c;
                   9686:   NM tm,mr,mr0;
                   9687:   ND r;
                   9688:
                   9689:   if ( !p ) return 0;
                   9690:   d0 = DL(m0);
                   9691:   c0 = CZ(m0);
                   9692:   mr0 = 0;
                   9693:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   9694:     NEXTNM(mr0,mr);
                   9695:     c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
                   9696:     ndl_add(d0,DL(tm),DL(mr));
                   9697:   }
                   9698:   NEXT(mr) = 0;
                   9699:   MKND(NV(p),mr0,LEN(p),r);
                   9700:   return r;
                   9701: }
                   9702:
                   9703: ND *btog(NODE ti,ND **p,int nb,int mod)
                   9704: {
                   9705:   PGeoBucket *r;
                   9706:   int i,ci;
                   9707:   NODE t,s;
                   9708:   ND m,tp;
                   9709:   ND *pi,*rd;
                   9710:   P c;
                   9711:
                   9712:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   9713:   for ( i = 0; i < nb; i++ )
                   9714:     r[i] = create_pbucket();
                   9715:   for ( t = ti; t; t = NEXT(t) ) {
                   9716:   s = BDY((LIST)BDY(t));
                   9717:     if ( ARG0(s) ) {
                   9718:     m = mdptond((DP)ARG2(s));
1.6       noro     9719:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     9720:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   9721:       HCM(m) = ci;
1.6       noro     9722:       pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     9723:       for ( i = 0; i < nb; i++ ) {
                   9724:       tp = nd_mul_nm(mod,BDY(m),pi[i]);
                   9725:         add_pbucket(mod,r[i],tp);
                   9726:       }
                   9727:     }
                   9728:     ci = 1;
                   9729:     } else {
                   9730:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   9731:     ci = invm(ci,mod);
                   9732:   }
                   9733:   }
                   9734:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   9735:   for ( i = 0; i < nb; i++ )
                   9736:   rd[i] = normalize_pbucket(mod,r[i]);
                   9737:   if ( ci != 1 )
                   9738:     for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
                   9739:    return rd;
                   9740: }
                   9741:
                   9742: /* YYY */
                   9743: ND *btog_lf(NODE ti,ND **p,int nb)
                   9744: {
                   9745:   PGeoBucket *r;
                   9746:   int i;
                   9747:   NODE t,s;
                   9748:   ND m,tp;
                   9749:   ND *pi,*rd;
                   9750:   LM lm;
                   9751:   Z lf,c;
                   9752:
                   9753:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   9754:   for ( i = 0; i < nb; i++ )
                   9755:     r[i] = create_pbucket();
                   9756:   for ( t = ti; t; t = NEXT(t) ) {
                   9757:     s = BDY((LIST)BDY(t));
                   9758:     if ( ARG0(s) ) {
                   9759:       m = mdptond((DP)ARG2(s));
1.6       noro     9760:       simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1       noro     9761:       if ( lm ) {
                   9762:         lmtolf(lm,&lf); HCZ(m) = lf;
1.6       noro     9763:         pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     9764:         for ( i = 0; i < nb; i++ ) {
                   9765:           tp = nd_mul_nm_lf(BDY(m),pi[i]);
                   9766:           add_pbucket(-2,r[i],tp);
                   9767:         }
                   9768:       }
                   9769:       c = ONE;
                   9770:     } else {
                   9771:       simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
                   9772:     }
                   9773:   }
                   9774:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   9775:   for ( i = 0; i < nb; i++ )
                   9776:     rd[i] = normalize_pbucket(-2,r[i]);
                   9777:   for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
                   9778:   return rd;
                   9779: }
                   9780:
                   9781: ND btog_one(NODE ti,ND *p,int nb,int mod)
                   9782: {
                   9783:   PGeoBucket r;
                   9784:   int i,ci,j;
                   9785:   NODE t,s;
                   9786:   ND m,tp;
                   9787:   ND pi,rd;
                   9788:   P c;
                   9789:
                   9790:   r = create_pbucket();
                   9791:   for ( t = ti; t; t = NEXT(t) ) {
                   9792:   s = BDY((LIST)BDY(t));
                   9793:     if ( ARG0(s) ) {
                   9794:     m = mdptond((DP)ARG2(s));
1.6       noro     9795:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     9796:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   9797:       HCM(m) = ci;
1.6       noro     9798:       pi = p[j=ZTOS((Q)ARG1(s))];
1.1       noro     9799:     if ( !pi ) {
                   9800:       pi = nd_load_mod(j);
                   9801:       tp = nd_mul_nm(mod,BDY(m),pi);
                   9802:       nd_free(pi);
                   9803:         add_pbucket(mod,r,tp);
                   9804:     } else {
                   9805:       tp = nd_mul_nm(mod,BDY(m),pi);
                   9806:         add_pbucket(mod,r,tp);
                   9807:       }
                   9808:     }
                   9809:     ci = 1;
                   9810:     } else {
                   9811:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   9812:     ci = invm(ci,mod);
                   9813:   }
                   9814:   }
                   9815:   rd = normalize_pbucket(mod,r);
                   9816:   free_pbucket(r);
                   9817:   if ( ci != 1 ) nd_mul_c(mod,rd,ci);
                   9818:   return rd;
                   9819: }
                   9820:
                   9821: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
                   9822:
                   9823: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
                   9824: {
                   9825:   int i,j,n,m,nb,pi0,pi1,nvar;
                   9826:   VL fv,tv,vv;
                   9827:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   9828:   ND **p;
                   9829:   ND *c;
                   9830:   ND u;
                   9831:   P inv;
                   9832:   MAT mat;
                   9833:
                   9834:   if ( mod == -2 )
                   9835:     return nd_btog_lf(f,v,ord,tlist,rp);
                   9836:
                   9837:   parse_nd_option(current_option);
                   9838:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   9839:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   9840:   switch ( ord->id ) {
                   9841:     case 1:
                   9842:       if ( ord->nv != nvar )
                   9843:         error("nd_check : invalid order specification");
                   9844:       break;
                   9845:     default:
                   9846:       break;
                   9847:   }
                   9848:   nd_init_ord(ord);
                   9849: #if 0
1.6       noro     9850:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     9851: #else
                   9852:   nd_bpe = 32;
                   9853: #endif
                   9854:   nd_setup_parameters(nvar,0);
                   9855:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   9856:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   9857:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   9858:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   9859:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     9860:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     9861:   if ( j > i ) i = j;
                   9862:   }
                   9863:   n = i+1;
                   9864:   nb = length(BDY(f));
                   9865:   p = (ND **)MALLOC(n*sizeof(ND *));
                   9866:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   9867:     pi = BDY((LIST)BDY(t));
1.6       noro     9868:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     9869:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   9870:     ptomp(mod,(P)ARG2(pi),&inv);
                   9871:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   9872:     u = ptond(CO,vv,(P)ONE);
                   9873:     HCM(u) = ((MQ)inv)->cont;
                   9874:     c[pi1] = u;
                   9875:   }
                   9876:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   9877:     printf("%d ",i); fflush(stdout);
                   9878:     ti = BDY((LIST)BDY(t));
1.6       noro     9879:     p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     9880:   }
                   9881:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   9882:     printf("%d ",i); fflush(stdout);
                   9883:     ti = BDY((LIST)BDY(t));
1.6       noro     9884:     p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     9885:   }
                   9886:   m = length(ind);
                   9887:   MKMAT(mat,nb,m);
                   9888:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     9889:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     9890:       BDY(mat)[i][j] = ndtodp(mod,c[i]);
                   9891:   return mat;
                   9892: }
                   9893:
                   9894: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
                   9895: {
                   9896:   int i,j,n,m,nb,pi0,pi1,nvar;
                   9897:   VL fv,tv,vv;
                   9898:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   9899:   ND **p;
                   9900:   ND *c;
                   9901:   ND u;
                   9902:   MAT mat;
                   9903:   LM lm;
                   9904:   Z lf,inv;
                   9905:
                   9906:   parse_nd_option(current_option);
                   9907:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   9908:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   9909:   switch ( ord->id ) {
                   9910:     case 1:
                   9911:       if ( ord->nv != nvar )
                   9912:         error("nd_check : invalid order specification");
                   9913:       break;
                   9914:     default:
                   9915:       break;
                   9916:   }
                   9917:   nd_init_ord(ord);
                   9918: #if 0
1.6       noro     9919:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     9920: #else
                   9921:   nd_bpe = 32;
                   9922: #endif
                   9923:   nd_setup_parameters(nvar,0);
                   9924:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   9925:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   9926:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   9927:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   9928:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     9929:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     9930:   if ( j > i ) i = j;
                   9931:   }
                   9932:   n = i+1;
                   9933:   nb = length(BDY(f));
                   9934:   p = (ND **)MALLOC(n*sizeof(ND *));
                   9935:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   9936:     pi = BDY((LIST)BDY(t));
1.6       noro     9937:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     9938:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   9939:     simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
                   9940:     u = ptond(CO,vv,(P)ONE);
                   9941:     HCZ(u) = inv;
                   9942:     c[pi1] = u;
                   9943:   }
                   9944:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   9945:     printf("%d ",i); fflush(stdout);
                   9946:     ti = BDY((LIST)BDY(t));
1.6       noro     9947:     p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     9948:   }
                   9949:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   9950:     printf("%d ",i); fflush(stdout);
                   9951:     ti = BDY((LIST)BDY(t));
1.6       noro     9952:     p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     9953:   }
                   9954:   m = length(ind);
                   9955:   MKMAT(mat,nb,m);
                   9956:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     9957:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     9958:       BDY(mat)[i][j] = ndtodp(-2,c[i]);
                   9959:   return mat;
                   9960: }
                   9961:
                   9962: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
                   9963:   LIST tlist,int pos,MAT *rp)
                   9964: {
                   9965:   int i,j,n,m,nb,pi0,pi1,nvar;
                   9966:   VL fv,tv,vv;
                   9967:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   9968:   ND *p;
                   9969:   ND *c;
                   9970:   ND u;
                   9971:   P inv;
                   9972:   VECT vect;
                   9973:
                   9974:   if ( mod == -2 )
                   9975:     error("nd_btog_one : not implemented yet for a large finite field");
                   9976:
                   9977:   parse_nd_option(current_option);
                   9978:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   9979:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   9980:   switch ( ord->id ) {
                   9981:     case 1:
                   9982:       if ( ord->nv != nvar )
                   9983:         error("nd_check : invalid order specification");
                   9984:       break;
                   9985:     default:
                   9986:       break;
                   9987:   }
                   9988:   nd_init_ord(ord);
                   9989: #if 0
1.6       noro     9990:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     9991: #else
                   9992:   nd_bpe = 32;
                   9993: #endif
                   9994:   nd_setup_parameters(nvar,0);
                   9995:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   9996:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   9997:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   9998:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   9999:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     10000:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     10001:   if ( j > i ) i = j;
                   10002:   }
                   10003:   n = i+1;
                   10004:   nb = length(BDY(f));
                   10005:   p = (ND *)MALLOC(n*sizeof(ND *));
                   10006:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   10007:     pi = BDY((LIST)BDY(t));
1.6       noro     10008:   pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     10009:   if ( pi1 == pos ) {
                   10010:     ptomp(mod,(P)ARG2(pi),&inv);
                   10011:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   10012:     u = ptond(CO,vv,(P)ONE);
                   10013:     HCM(u) = ((MQ)inv)->cont;
                   10014:     p[pi0] = u;
                   10015:     }
                   10016:   }
                   10017:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   10018:   printf("%d ",i); fflush(stdout);
                   10019:     ti = BDY((LIST)BDY(t));
1.6       noro     10020:     p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10021:     if ( Demand ) {
                   10022:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   10023:   }
                   10024:   }
                   10025:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   10026:   printf("%d ",i); fflush(stdout);
                   10027:     ti = BDY((LIST)BDY(t));
1.6       noro     10028:     p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10029:     if ( Demand ) {
                   10030:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   10031:   }
                   10032:   }
                   10033:   m = length(ind);
                   10034:   MKVECT(vect,m);
                   10035:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6       noro     10036:   u = p[ZTOS((Q)BDY(t))];
1.1       noro     10037:   if ( !u ) {
1.6       noro     10038:     u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1       noro     10039:     BDY(vect)[j] = ndtodp(mod,u);
                   10040:     nd_free(u);
                   10041:   } else
                   10042:     BDY(vect)[j] = ndtodp(mod,u);
                   10043:   }
                   10044:   return vect;
                   10045: }
                   10046:
                   10047: void ndv_print_lf(NDV p)
                   10048: {
                   10049:     NMV m;
                   10050:     int i,len;
                   10051:
                   10052:     if ( !p ) printf("0\n");
                   10053:     else {
                   10054:         len = LEN(p);
                   10055:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   10056:             printf("+");
                   10057:             mpz_out_str(asir_out,10,BDY(CZ(m)));
                   10058:             printf("*");
                   10059:             ndl_print(DL(m));
                   10060:         }
                   10061:         printf("\n");
                   10062:     }
                   10063: }
                   10064:
                   10065: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
                   10066: {
                   10067:     VL tv,fv,vv,vc,av;
                   10068:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   10069:     int m,nocheck,nvar,mindex,e,max;
                   10070:     NDV c;
                   10071:     NMV a;
                   10072:     P p,zp;
                   10073:     Q dmy;
                   10074:     EPOS oepos;
                   10075:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   10076:     Alg alpha,dp;
                   10077:     P poly;
                   10078:     LIST f1,f2,zpl;
                   10079:     Obj obj;
                   10080:     NumberField nf;
                   10081:     struct order_spec *ord1;
                   10082:     struct oEGT eg_check,eg0,eg1;
                   10083:     NODE tr,tl1,tl2,tl3,tl4;
                   10084:     LIST l1,l2,l3,l4,l5;
                   10085:     int *perm;
                   10086:     int j,ret;
                   10087:     NODE retn;
                   10088:     Q jq,bpe;
                   10089:
                   10090:     nd_module = 0;
                   10091:     parse_nd_option(current_option);
                   10092:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   10093:     if ( nd_vc )
                   10094:       error("nd_f4_lf_trace : computation over a rational function field is not implemented");
                   10095:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   10096:     switch ( ord->id ) {
                   10097:         case 1:
                   10098:             if ( ord->nv != nvar )
                   10099:                 error("nd_f4_lf_trace : invalid order specification");
                   10100:             break;
                   10101:         default:
                   10102:             break;
                   10103:     }
                   10104:
                   10105:     nd_ntrans = nvar;
                   10106:     nd_nalg = 0;
                   10107:
                   10108:     nocheck = 0;
                   10109:     mindex = 0;
                   10110:
                   10111:     /* do not use on-demand load/save */
                   10112:     nd_demand = 0;
                   10113:     m = trace > 1 ? trace : get_lprime(mindex);
                   10114:     nd_init_ord(ord);
                   10115:     mrank = 0;
                   10116:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   10117:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   10118:             if ( nd_module ) {
                   10119:                 s = BDY((LIST)BDY(t));
                   10120:                 trank = length(s);
                   10121:                 mrank = MAX(mrank,trank);
                   10122:                 for ( ; s; s = NEXT(s) ) {
                   10123:                     e = getdeg(tv->v,(P)BDY(s));
                   10124:                     max = MAX(e,max);
                   10125:                 }
                   10126:             } else {
                   10127:                 e = getdeg(tv->v,(P)BDY(t));
                   10128:                 max = MAX(e,max);
                   10129:             }
                   10130:         }
                   10131:     nd_setup_parameters(nvar,max);
                   10132:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   10133:     ishomo = 1;
                   10134:     /* XXX */
                   10135:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   10136:         if ( nd_module ) {
                   10137:             c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
                   10138:         } else {
                   10139:             c = (pointer)ptondv(CO,vv,(P)BDY(t));
                   10140:         }
                   10141:         if ( ishomo )
                   10142:             ishomo = ishomo && ndv_ishomo(c);
                   10143:         if ( c ) {
                   10144:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   10145:             ndv_mod(-2,c);
                   10146:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   10147:         }
                   10148:     }
                   10149:     if ( in0 ) NEXT(in) = 0;
                   10150:     if ( fd0 ) NEXT(fd) = 0;
                   10151:     if ( !ishomo && homo ) {
                   10152:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   10153:             c = (NDV)BDY(t); len = LEN(c);
                   10154:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   10155:                 wmax = MAX(TD(DL(a)),wmax);
                   10156:         }
                   10157:         homogenize_order(ord,nvar,&ord1);
                   10158:         nd_init_ord(ord1);
                   10159:         nd_setup_parameters(nvar+1,wmax);
                   10160:         for ( t = fd0; t; t = NEXT(t) )
                   10161:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   10162:     }
                   10163:     if ( MaxDeg > 0 ) nocheck = 1;
1.24      noro     10164:     ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1       noro     10165:     if ( ret )
                   10166:       cand = nd_f4_lf_trace_main(m,&perm);
                   10167:     if ( !ret || !cand ) {
                   10168:        *rp = 0; return;
                   10169:     }
                   10170:     if ( !ishomo && homo ) {
                   10171:       /* dehomogenization */
                   10172:       for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   10173:       nd_init_ord(ord);
                   10174:       nd_setup_parameters(nvar,0);
                   10175:     }
                   10176:     cand = ndv_reducebase(cand,perm);
                   10177:     cand = ndv_reduceall(-2,cand);
                   10178:     cbpe = nd_bpe;
                   10179:     get_eg(&eg0);
                   10180:     if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   10181:       /* gbcheck : cand is a GB of Id(cand) ? */
                   10182:       retn = nd_f4(-2,0,0);
                   10183:     }
                   10184:   if ( !retn ) {
                   10185:       /* failure */
                   10186:       *rp = 0; return;
                   10187:     }
                   10188:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   10189:     if ( DP_Print )
1.5       noro     10190:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     10191:     /* dp->p */
                   10192:     nd_bpe = cbpe;
                   10193:     nd_setup_parameters(nd_nvar,0);
                   10194:     for ( r = cand; r; r = NEXT(r) ) {
                   10195:       if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
                   10196:       else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
                   10197:     }
                   10198:     MKLIST(*rp,cand);
                   10199: }
                   10200:
                   10201: NODE nd_f4_lf_trace_main(int m,int **indp)
                   10202: {
                   10203:     int i,nh,stat,index;
                   10204:     NODE r,rm,g;
                   10205:     ND_pairs d,l,l0,t;
                   10206:     ND spol,red;
                   10207:     NDV nf,redv,nfqv,nfv;
                   10208:     NM s0,s;
                   10209:     NODE rp0,srp0,nflist,nflist_lf;
                   10210:     int nsp,nred,col,rank,len,k,j,a;
                   10211:     UINT c;
                   10212:     UINT **spmat;
                   10213:     UINT *s0vect,*svect,*p,*v;
                   10214:     int *colstat;
                   10215:     IndArray *imat;
                   10216:     int *rhead;
                   10217:     int spcol,sprow;
                   10218:     int sugar;
                   10219:     PGeoBucket bucket;
                   10220:     struct oEGT eg0,eg1,eg_f4;
                   10221:
                   10222:     g = 0; d = 0;
                   10223:     for ( i = 0; i < nd_psn; i++ ) {
                   10224:         d = update_pairs(d,g,i,0);
                   10225:         g = update_base(g,i);
                   10226:     }
                   10227:     while ( d ) {
                   10228:         get_eg(&eg0);
                   10229:         l = nd_minsugarp(d,&d);
                   10230:         sugar = SG(l);
                   10231:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   10232:         bucket = create_pbucket();
                   10233:         stat = nd_sp_f4(m,0,l,bucket);
                   10234:         if ( !stat ) {
                   10235:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10236:             NEXT(t) = d; d = l;
                   10237:             d = nd_reconstruct(1,d);
                   10238:             continue;
                   10239:         }
                   10240:         if ( bucket->m < 0 ) continue;
                   10241:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   10242:         if ( !col ) {
                   10243:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10244:             NEXT(t) = d; d = l;
                   10245:             d = nd_reconstruct(1,d);
                   10246:             continue;
                   10247:         }
                   10248:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   10249:         if ( DP_Print )
1.5       noro     10250:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1       noro     10251:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   10252:         if ( !l0 ) continue;
                   10253:         l = l0;
                   10254:
                   10255:         /* over LF */
                   10256:         bucket = create_pbucket();
                   10257:         stat = nd_sp_f4(-2,1,l,bucket);
                   10258:         if ( !stat ) {
                   10259:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10260:             NEXT(t) = d; d = l;
                   10261:             d = nd_reconstruct(1,d);
                   10262:             continue;
                   10263:         }
                   10264:         if ( bucket->m < 0 ) continue;
                   10265:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   10266:         if ( !col ) {
                   10267:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10268:             NEXT(t) = d; d = l;
                   10269:             d = nd_reconstruct(1,d);
                   10270:             continue;
                   10271:         }
                   10272:         nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
                   10273:         /* adding new bases */
                   10274:         for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
                   10275:             nfv = (NDV)BDY(rm);
                   10276:             nfqv = (NDV)BDY(r);
                   10277:             if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
                   10278:             ndv_removecont(m,nfv);
                   10279:             ndv_removecont(-2,nfqv);
1.24      noro     10280:             nh = ndv_newps(-2,nfv,nfqv);
1.1       noro     10281:             d = update_pairs(d,g,nh,0);
                   10282:             g = update_base(g,nh);
                   10283:         }
                   10284:         if ( r || rm ) return 0;
                   10285:     }
                   10286:   conv_ilist(nd_demand,1,g,indp);
                   10287:     return g;
                   10288: }
                   10289:
1.7       noro     10290: #if SIZEOF_LONG==8
                   10291:
                   10292: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   10293: {
                   10294:     int j,k,len;
                   10295:     UINT *p;
                   10296:     UINT c;
                   10297:     NDV r;
                   10298:     NMV mr0,mr;
                   10299:
                   10300:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   10301:     if ( !len ) return 0;
                   10302:     else {
                   10303:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   10304: #if 0
                   10305:         ndv_alloc += nmv_adv*len;
                   10306: #endif
                   10307:         mr = mr0;
                   10308:         p = s0vect;
                   10309:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   10310:             if ( !rhead[j] ) {
                   10311:                 if ( (c = (UINT)vect[k++]) != 0 ) {
                   10312:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   10313:                 }
                   10314:             }
                   10315:         MKNDV(nd_nvar,mr0,len,r);
                   10316:         return r;
                   10317:     }
                   10318: }
                   10319:
1.28      noro     10320: NDV vect64_to_ndv_s(mp_limb_t *vect,int col,UINT *s0vect)
                   10321: {
                   10322:     int j,k,len;
                   10323:     UINT *p;
                   10324:     UINT c;
                   10325:     NDV r;
                   10326:     NMV mr0,mr;
                   10327:
                   10328:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   10329:     if ( !len ) return 0;
                   10330:     else {
                   10331:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   10332:         mr = mr0;
                   10333:         p = s0vect;
                   10334:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   10335:           if ( (c = (UINT)vect[k++]) != 0 ) {
                   10336:             ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   10337:           }
                   10338:         MKNDV(nd_nvar,mr0,len,r);
                   10339:         return r;
                   10340:     }
                   10341: }
                   10342:
1.7       noro     10343: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
                   10344: {
                   10345:     NM m;
1.11      noro     10346:     UINT *t,*s,*u;
                   10347:     int i,st,ed,md,prev,c;
1.7       noro     10348:
                   10349:     for ( i = 0; i < n; i++ ) r[i] = 0;
1.11      noro     10350:     prev = 0;
                   10351:     for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
                   10352:       t = DL(m);
                   10353:       st = prev;
                   10354:       ed = n;
                   10355:       while ( ed > st ) {
                   10356:         md = (st+ed)/2;
                   10357:         u = s0+md*nd_wpd;
                   10358:         c = DL_COMPARE(u,t);
                   10359:         if ( c == 0 ) break;
                   10360:         else if ( c > 0 ) st = md;
                   10361:         else ed = md;
                   10362:       }
                   10363:       r[md] = (mp_limb_t)CM(m);
                   10364:       prev = md;
1.7       noro     10365:     }
                   10366:     for ( i = 0; !r[i]; i++ );
                   10367:     return i;
                   10368: }
                   10369:
                   10370: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
                   10371:
1.28      noro     10372: 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     10373: {
                   10374:     int i,j,k,len,pos,prev;
                   10375:     mp_limb_t a,c,c1,c2;
                   10376:     IndArray ivect;
                   10377:     unsigned char *ivc;
                   10378:     unsigned short *ivs;
                   10379:     unsigned int *ivi;
                   10380:     NDV redv;
                   10381:     NMV mr;
                   10382:     NODE rp;
                   10383:     int maxrs;
                   10384:
                   10385:     for ( i = 0; i < col; i++ ) cvect[i] = 0;
                   10386:     maxrs = 0;
                   10387:     for ( i = 0; i < nred; i++ ) {
                   10388:         ivect = imat[i];
                   10389:         k = ivect->head;
                   10390:         a = svect[k]; c = cvect[k];
                   10391:         MOD128(a,c,m);
                   10392:         svect[k] = a; cvect[k] = 0;
1.28      noro     10393:         if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.11      noro     10394:             Nf4_red++;
1.7       noro     10395:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   10396:             c = m-c; redv = nd_ps[rp0[i]->index];
                   10397:             len = LEN(redv); mr = BDY(redv);
                   10398:             svect[k] = 0; prev = k;
                   10399:             switch ( ivect->width ) {
                   10400:                 case 1:
                   10401:                     ivc = ivect->index.c;
                   10402:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   10403:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12      noro     10404:                         c2 = svect[pos]+c1*c;
                   10405:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   10406:                         svect[pos] = c2;
1.7       noro     10407:                     }
                   10408:                     break;
                   10409:                 case 2:
                   10410:                     ivs = ivect->index.s;
                   10411:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   10412:                         pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12      noro     10413:                         c2 = svect[pos]+c1*c;
                   10414:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   10415:                         svect[pos] = c2;
1.7       noro     10416:                     }
                   10417:                     break;
                   10418:                 case 4:
                   10419:                     ivi = ivect->index.i;
                   10420:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   10421:                         pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12      noro     10422:                         c2 = svect[pos]+c1*c;
                   10423:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   10424:                         svect[pos] = c2;
1.7       noro     10425:                     }
                   10426:                     break;
                   10427:             }
                   10428:         }
                   10429:     }
                   10430:     for ( i = 0; i < col; i++ ) {
                   10431:       a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
                   10432:     }
                   10433:     return maxrs;
                   10434: }
                   10435:
                   10436: /* for Fp, 2^15=<p<2^29 */
                   10437:
                   10438: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   10439:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   10440: {
                   10441:     int spcol,sprow,a;
                   10442:     int i,j,k,l,rank;
                   10443:     NODE r0,r;
                   10444:     ND_pairs sp;
                   10445:     ND spol;
                   10446:     mp_limb_t **spmat;
                   10447:     mp_limb_t *svect,*cvect;
                   10448:     mp_limb_t *v;
                   10449:     int *colstat;
                   10450:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   10451:     int maxrs;
                   10452:     int *spsugar;
                   10453:     ND_pairs *spactive;
                   10454:
                   10455:     spcol = col-nred;
                   10456:     get_eg(&eg0);
                   10457:     /* elimination (1st step) */
                   10458:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   10459:     svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   10460:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   10461:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   10462:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   10463:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   10464:         nd_sp(m,0,sp,&spol);
                   10465:         if ( !spol ) continue;
                   10466:         nd_to_vect64(m,s0vect,col,spol,svect);
1.28      noro     10467:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,0);
1.7       noro     10468:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   10469:         if ( i < col ) {
                   10470:             spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
                   10471:             for ( j = k = 0; j < col; j++ )
                   10472:                 if ( !rhead[j] ) v[k++] = (UINT)svect[j];
                   10473:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   10474:             if ( nz )
                   10475:             spactive[sprow] = sp;
                   10476:             sprow++;
                   10477:         }
                   10478:         nd_free(spol);
                   10479:     }
1.12      noro     10480:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7       noro     10481:     if ( DP_Print ) {
                   10482:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   10483:         fflush(asir_out);
                   10484:     }
                   10485:     /* free index arrays */
                   10486:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   10487:
                   10488:     /* elimination (2nd step) */
                   10489:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   10490:     rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   10491:     r0 = 0;
                   10492:     for ( i = 0; i < rank; i++ ) {
                   10493:         NEXTNODE(r0,r); BDY(r) =
                   10494:           (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   10495:         SG((NDV)BDY(r)) = spsugar[i];
                   10496:         GCFREE(spmat[i]);
                   10497:     }
                   10498:     if ( r0 ) NEXT(r) = 0;
                   10499:
                   10500:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12      noro     10501:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7       noro     10502:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   10503:     if ( DP_Print ) {
                   10504:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   10505:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   10506:             nsp,nred,sprow,spcol,rank);
                   10507:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   10508:     }
                   10509:     if ( nz ) {
                   10510:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   10511:         if ( rank > 0 ) {
                   10512:             NEXT(spactive[rank-1]) = 0;
                   10513:             *nz = spactive[0];
                   10514:         } else
                   10515:             *nz = 0;
                   10516:     }
                   10517:     return r0;
                   10518: }
                   10519:
                   10520: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   10521: {
                   10522:   int i,j,k,l,rank,s;
                   10523:   mp_limb_t inv;
                   10524:   mp_limb_t a;
                   10525:   UINT c;
                   10526:   mp_limb_t *t,*pivot,*pk;
                   10527:   UINT *ck;
                   10528:   UINT **cmat;
                   10529:   UINT *ct;
                   10530:   ND_pairs pair;
                   10531:
                   10532:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   10533:   for ( i = 0; i < row; i++ ) {
                   10534:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   10535:     bzero(cmat[i],col*sizeof(UINT));
                   10536:   }
                   10537:
                   10538:   for ( rank = 0, j = 0; j < col; j++ ) {
                   10539:     for ( i = rank; i < row; i++ ) {
                   10540:       a = mat[i][j]; c = cmat[i][j];
                   10541:       MOD128(a,c,md);
                   10542:       mat[i][j] = a; cmat[i][j] = 0;
                   10543:     }
                   10544:     for ( i = rank; i < row; i++ )
                   10545:       if ( mat[i][j] )
                   10546:         break;
                   10547:     if ( i == row ) {
                   10548:       colstat[j] = 0;
                   10549:       continue;
                   10550:     } else
                   10551:       colstat[j] = 1;
                   10552:     if ( i != rank ) {
                   10553:       t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   10554:       ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
                   10555:       s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   10556:       if ( spactive ) {
                   10557:         pair = spactive[i]; spactive[i] = spactive[rank];
                   10558:         spactive[rank] = pair;
                   10559:       }
                   10560:     }
                   10561:     /* column j is normalized */
                   10562:     s = sugar[rank];
                   10563:     inv = invm((UINT)mat[rank][j],md);
                   10564:     /* normalize pivot row */
                   10565:     for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
                   10566:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   10567:     }
                   10568:     for ( i = rank+1; i < row; i++ ) {
                   10569:       if ( (a = mat[i][j]) != 0 ) {
                   10570:         sugar[i] = MAX(sugar[i],s);
                   10571:         red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11      noro     10572:         Nf4_red++;
1.7       noro     10573:       }
                   10574:     }
                   10575:     rank++;
                   10576:   }
                   10577:   for ( j = col-1, l = rank-1; j >= 0; j-- )
                   10578:     if ( colstat[j] ) {
                   10579:       for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
                   10580:         a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
                   10581:       }
                   10582:       s = sugar[l];
                   10583:       for ( i = 0; i < l; i++ ) {
                   10584:         a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
                   10585:         if ( a ) {
                   10586:           sugar[i] = MAX(sugar[i],s);
                   10587:           red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11      noro     10588:           Nf4_red++;
1.7       noro     10589:         }
                   10590:       }
                   10591:       l--;
                   10592:     }
                   10593:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   10594:   GCFREE(cmat);
                   10595:   return rank;
                   10596: }
                   10597: #endif
                   10598:
1.28      noro     10599: 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)
                   10600: {
                   10601:   int i,j,k,l,rank,s,imin;
                   10602:   mp_limb_t inv;
                   10603:   mp_limb_t a;
                   10604:   UINT c;
                   10605:   mp_limb_t *t,*pivot,*pk;
                   10606:   UINT *ck;
                   10607:   UINT **cmat;
                   10608:   UINT *ct;
                   10609:   ND_pairs pair;
                   10610:   SIG sg;
1.31      noro     10611:   int *used;
1.28      noro     10612:
1.31      noro     10613:   used = (int *)MALLOC(row*sizeof(int));
1.28      noro     10614:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   10615:   for ( i = 0; i < row; i++ ) {
                   10616:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   10617:     bzero(cmat[i],col*sizeof(UINT));
                   10618:   }
                   10619:
1.31      noro     10620:   for ( j = 0; j < col; j++ ) {
                   10621:     for ( i = 0; i < row; i++ ) {
1.28      noro     10622:       a = mat[i][j]; c = cmat[i][j];
                   10623:       MOD128(a,c,md);
                   10624:       mat[i][j] = a; cmat[i][j] = 0;
                   10625:     }
1.31      noro     10626:     for ( i = 0; i < row; i++ )
                   10627:       if ( !used[i] && mat[i][j] ) break;
                   10628:     if ( i == row ) {
1.28      noro     10629:       colstat[j] = 0;
                   10630:       continue;
1.31      noro     10631:     } else {
1.28      noro     10632:       colstat[j] = 1;
1.31      noro     10633:       used[i] = 1;
1.28      noro     10634:     }
                   10635:     /* column j is normalized */
1.31      noro     10636:     s = sugar[i];
                   10637:     inv = invm((UINT)mat[i][j],md);
1.28      noro     10638:     /* normalize pivot row */
1.31      noro     10639:     for ( k = j, pk = mat[i]+j, ck = cmat[i]+j; k < col; k++, pk++, ck++ ) {
1.28      noro     10640:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   10641:     }
1.31      noro     10642:     for ( k = i+1; k < row; k++ ) {
                   10643:       if ( (a = mat[k][j]) != 0 ) {
                   10644:         sugar[k] = MAX(sugar[k],s);
                   10645:         red_by_vect64(md,mat[k]+j,cmat[k]+j,mat[i]+j,(int)(md-a),col-j);
1.28      noro     10646:         Nf4_red++;
                   10647:       }
                   10648:     }
                   10649:   }
1.31      noro     10650:   rank = 0;
                   10651:   for ( i = 0; i < row; i++ ) {
                   10652:     for ( j = 0; j < col; j++ )
                   10653:       if ( mat[i][j] ) break;
                   10654:     if ( j == col ) sugar[i] = -1;
                   10655:     else rank++;
                   10656:   }
1.28      noro     10657:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   10658:   GCFREE(cmat);
                   10659:   return rank;
                   10660: }
                   10661:
                   10662: NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   10663:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
                   10664: {
                   10665:     int spcol,sprow,a;
                   10666:     int i,j,k,l,rank;
                   10667:     NODE r0,r;
                   10668:     ND_pairs sp;
                   10669:     ND spol;
                   10670:     mp_limb_t **spmat;
                   10671:     mp_limb_t *svect,*cvect;
                   10672:     mp_limb_t *v;
                   10673:     int *colstat;
                   10674:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   10675:     int maxrs;
                   10676:     int *spsugar;
                   10677:     ND_pairs *spactive;
                   10678:     SIG *spsig;
                   10679:
                   10680:     get_eg(&eg0);
                   10681:     /* elimination (1st step) */
                   10682:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   10683:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   10684:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   10685:     spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
                   10686:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   10687:         nd_sp(m,0,sp,&spol);
                   10688:         if ( !spol ) {
1.29      noro     10689:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     10690:           continue;
                   10691:         }
                   10692:         svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   10693:         nd_to_vect64(m,s0vect,col,spol,svect);
                   10694:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,spol->sig);
                   10695:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   10696:         if ( i < col ) {
                   10697:             spmat[sprow] = svect;
                   10698:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   10699:             spsig[sprow] = sp->sig;
                   10700:             sprow++;
                   10701:         } else {
1.29      noro     10702:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     10703:         }
                   10704:         nd_free(spol);
                   10705:     }
                   10706:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
                   10707:     if ( DP_Print ) {
                   10708:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   10709:         fflush(asir_out);
                   10710:     }
                   10711:     /* free index arrays */
                   10712:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   10713:
                   10714:     /* elimination (2nd step) */
                   10715:     colstat = (int *)MALLOC(col*sizeof(int));
                   10716:     rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
                   10717:     r0 = 0;
1.31      noro     10718:     for ( i = 0; i < sprow; i++ ) {
                   10719:         if ( spsugar[i] >= 0 ) {
                   10720:           NEXTNODE(r0,r);
                   10721:           BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
                   10722:           SG((NDV)BDY(r)) = spsugar[i];
                   10723:           ((NDV)BDY(r))->sig = spsig[i];
                   10724:         } else
                   10725:           syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
1.28      noro     10726:         GCFREE(spmat[i]);
                   10727:     }
                   10728:     if ( r0 ) NEXT(r) = 0;
                   10729:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
                   10730:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   10731:     if ( DP_Print ) {
                   10732:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   10733:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   10734:             nsp,nred,sprow,col,rank);
                   10735:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   10736:     }
                   10737:     return r0;
                   10738: }
                   10739:
                   10740: NODE nd_f4_red_s(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,NODE *syzlistp)
                   10741: {
                   10742:   IndArray *imat;
                   10743:   int nsp,nred,i,start;
                   10744:   int *rhead;
                   10745:   NODE r0,rp;
                   10746:   ND_pairs sp;
                   10747:   NM_ind_pair *rvect;
                   10748:   UINT *s;
                   10749:   int *s0hash;
                   10750:   struct oEGT eg0,eg1,eg_conv;
                   10751:
                   10752:   for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   10753:   nred = length(rp0);
                   10754:   imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   10755:   rhead = (int *)MALLOC(col*sizeof(int));
                   10756:   for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   10757:
                   10758:   /* construction of index arrays */
                   10759:   get_eg(&eg0);
                   10760:   if ( DP_Print ) {
                   10761:     fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   10762:     fflush(asir_out);
                   10763:   }
                   10764:   rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   10765:   for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   10766:     rvect[i] = (NM_ind_pair)BDY(rp);
                   10767:     imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
                   10768:     rhead[imat[i]->head] = 1;
                   10769:     start = imat[i]->head;
                   10770:   }
                   10771:   get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
                   10772:   if ( DP_Print ) {
                   10773:     fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   10774:     fflush(asir_out);
                   10775:   }
                   10776:   if ( m > 0 )
                   10777:     r0 = nd_f4_red_mod64_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
                   10778:   else
                   10779: //    r0 = nd_f4_red_q_main_s(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   10780:     error("nd_f4_red_q_main_s : not implemented yet");
                   10781:   return r0;
                   10782: }
                   10783:
                   10784: INLINE int ndl_find_reducer_minsig(UINT *dg)
                   10785: {
                   10786:   RHist r;
                   10787:   int i,singular,ret,d,k,imin;
                   10788:   SIG t;
                   10789:   static int wpd,nvar;
                   10790:   static SIG quo,quomin;
                   10791:   static UINT *tmp;
                   10792:
                   10793:   if ( !quo || nvar != nd_nvar ) { NEWSIG(quo); NEWSIG(quomin); }
                   10794:   if ( wpd != nd_wpd ) {
                   10795:     wpd = nd_wpd;
                   10796:     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
                   10797:   }
                   10798: #if 0
                   10799:   d = ndl_hash_value(dg);
                   10800:   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   10801:     if ( ndl_equal(dg,DL(r)) ) {
                   10802:       return r->index;
                   10803:     }
                   10804:   }
                   10805: #endif
                   10806:   imin = -1;
                   10807:   for ( i = 0; i < nd_psn; i++ ) {
                   10808:     r = nd_psh[i];
                   10809:     if ( ndl_reducible(dg,DL(r)) ) {
                   10810:       ndl_sub(dg,DL(r),tmp);
                   10811:       _ndltodl(tmp,DL(quo));
                   10812:       _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
                   10813:       quo->pos = nd_psh[i]->sig->pos;
                   10814:       if ( imin < 0 || comp_sig(quomin,quo) > 0 ) {
                   10815:         t = quo; quo = quomin; quomin = t;
                   10816:         imin = i;
                   10817:       }
                   10818:     }
                   10819:   }
                   10820:   if ( imin == -1 ) return nd_psn;
                   10821:   else {
                   10822: #if 0
                   10823:     nd_append_red(dg,i);
                   10824: #endif
                   10825:     return imin;
                   10826:   }
                   10827: }
                   10828:
                   10829: int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   10830: {
                   10831:   NODE rp0,rp;
                   10832:   NM mul,head,s0,s;
                   10833:   int index,col,i,sugar;
                   10834:   RHist h;
                   10835:   UINT *s0v,*p;
                   10836:   NM_ind_pair pair;
                   10837:   ND red;
                   10838:   NDV *ps;
                   10839:   SIG sig;
                   10840:
                   10841:   s0 = 0; rp0 = 0; col = 0;
                   10842:   if ( nd_demand )
                   10843:     ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   10844:   else
                   10845:     ps = trace?nd_ps_trace:nd_ps;
                   10846:   while ( 1 ) {
                   10847:     head = remove_head_pbucket_symbolic(bucket);
                   10848:     if ( !head ) break;
                   10849:     if ( !s0 ) s0 = head;
                   10850:     else NEXT(s) = head;
                   10851:     s = head;
                   10852:     index = ndl_find_reducer_minsig(DL(head));
                   10853:     if ( index >= 0 && index < nd_psn ) {
                   10854:       h = nd_psh[index];
                   10855:       NEWNM(mul);
                   10856:       ndl_sub(DL(head),DL(h),DL(mul));
                   10857:       if ( ndl_check_bound2(index,DL(mul)) )
                   10858:         return 0;
                   10859:       sugar = TD(DL(mul))+SG(ps[index]);
                   10860:       NEWSIG(sig);
                   10861:       _ndltodl(DL(mul),DL(sig));
                   10862:       _addtodl(nd_nvar,DL(nd_psh[index]->sig),DL(sig));
                   10863:       sig->pos = nd_psh[index]->sig->pos;
                   10864:       MKNM_ind_pair(pair,mul,index,sugar,sig);
                   10865:       red = ndv_mul_nm_symbolic(mul,ps[index]);
                   10866:       add_pbucket_symbolic(bucket,nd_remove_head(red));
                   10867:       NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   10868:     }
                   10869:     col++;
                   10870:   }
                   10871:   if ( rp0 ) NEXT(rp) = 0;
                   10872:   NEXT(s) = 0;
                   10873:   s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   10874:   for ( i = 0, p = s0v, s = s0; i < col;
                   10875:     i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   10876:   *s0vect = s0v;
                   10877:   *r = rp0;
                   10878:
                   10879:   return col;
                   10880: }
                   10881:
                   10882: NODE nd_sba_f4(int m,int **indp)
                   10883: {
                   10884:   int i,nh,stat,index,f4red;
                   10885:   NODE r,g,tn0,tn,node;
                   10886:   ND_pairs d,l,t,ll0,ll,lh;
                   10887:   LIST l0,l1;
                   10888:   ND spol,red;
                   10889:   NDV nf,redv;
                   10890:   NM s0,s;
                   10891:   NODE rp0,srp0,nflist;
                   10892:   int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
                   10893:   UINT c;
                   10894:   UINT **spmat;
                   10895:   UINT *s0vect,*svect,*p,*v;
                   10896:   int *colstat;
                   10897:   IndArray *imat;
                   10898:   int *rhead;
                   10899:   int spcol,sprow;
                   10900:   int sugar,sugarh;
1.30      noro     10901:   int nbase;
1.28      noro     10902:   PGeoBucket bucket;
                   10903:   struct oEGT eg0,eg1,eg_f4;
                   10904:   Z i1,i2,sugarq;
1.29      noro     10905:   NODE *syzlist;
1.28      noro     10906:
                   10907:   Nf4_red=0;
1.30      noro     10908:   d = 0;
1.29      noro     10909:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.28      noro     10910:   for ( i = 0; i < nd_psn; i++ ) {
1.30      noro     10911:     d = update_pairs_s(d,i,syzlist);
1.28      noro     10912:   }
1.30      noro     10913:   nd_nbase = nd_psn;
1.28      noro     10914:   f4red = 1;
                   10915:   while ( d ) {
                   10916:     l = nd_minsugarp_s(d,&d);
                   10917:     if ( !l ) continue;
                   10918:     sugar = nd_sugarweight?l->sugar2:SG(l);
                   10919:     if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   10920:     bucket = create_pbucket();
                   10921:     stat = nd_sp_f4(m,0,l,bucket);
                   10922:     if ( !stat ) {
                   10923:       for ( t = l; NEXT(t); t = NEXT(t) );
                   10924:       NEXT(t) = d; d = l;
                   10925:       d = nd_reconstruct(0,d);
                   10926:       continue;
                   10927:     }
                   10928:     if ( bucket->m < 0 ) continue;
                   10929:     col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
                   10930:     if ( !col ) {
                   10931:       for ( t = l; NEXT(t); t = NEXT(t) );
                   10932:       NEXT(t) = d; d = l;
                   10933:       d = nd_reconstruct(0,d);
                   10934:       continue;
                   10935:     }
                   10936:     if ( DP_Print ) fprintf(asir_out,"sugar=%d,",sugar);
                   10937:     l = remove_large_lcm(l);
1.29      noro     10938:     nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
1.28      noro     10939:     /* adding new bases */
                   10940:     for ( r = nflist; r; r = NEXT(r) ) {
1.31      noro     10941: #if 0
1.28      noro     10942:       ND tmp,tmpred;
                   10943:       SIG sig;
                   10944:
                   10945:       nf = (NDV)BDY(r);
                   10946:       sig = nf->sig;
                   10947:       tmp = ndvtond(m,nf);
                   10948:       stat = nd_nf_s(m,0,tmp,nd_ps,!Top,&tmpred);
1.31      noro     10949: #if 0
1.28      noro     10950:       if ( stat < 0 ) {
                   10951:         // top reducible
                   10952:         if ( DP_Print ) { fprintf(asir_out,"S"); fflush(asir_out); }
                   10953:       } else if ( tmpred ) {
1.31      noro     10954: #else
                   10955:       if ( tmpred ) {
                   10956: #endif
1.28      noro     10957:         nf = ndtondv(m,tmpred);
                   10958:         ndv_removecont(m,nf);
                   10959:         nh = ndv_newps(m,nf,0);
1.30      noro     10960:         d = update_pairs_s(d,nh,syzlist);
                   10961:         nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.28      noro     10962:       } else {
1.29      noro     10963:         syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.28      noro     10964:       }
1.31      noro     10965: #else
                   10966:       nf = (NDV)BDY(r);
                   10967:       ndv_removecont(m,nf);
                   10968:       nh = ndv_newps(m,nf,0);
                   10969:       d = update_pairs_s(d,nh,syzlist);
                   10970:       nd_sba_pos[nf->sig->pos] = append_one(nd_sba_pos[nf->sig->pos],nh);
                   10971: #endif
1.28      noro     10972:     }
1.30      noro     10973:     for ( i = 0; i < nd_nbase; i++ )
                   10974:       for ( r = syzlist[i]; r; r = NEXT(r) )
                   10975:         d = remove_spair_s(d,(SIG)BDY(r));
1.28      noro     10976:     if ( DP_Print ) {
1.32    ! noro     10977:       fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,nd_psn); fflush(asir_out);
1.28      noro     10978:     }
                   10979:     f4red++;
                   10980:     if ( nd_f4red && f4red > nd_f4red ) break;
                   10981:     if ( nd_rank0 && !nflist ) break;
                   10982:   }
                   10983:   if ( DP_Print ) {
                   10984:     fprintf(asir_out,"number of red=%d,",Nf4_red);
                   10985:   }
1.30      noro     10986:   g = conv_ilist_s(nd_demand,0,indp);
1.28      noro     10987:   return g;
                   10988: }

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