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

1.38    ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2018/engine/nd.c,v 1.37 2020/10/06 06:31:19 noro Exp $ */
1.1       noro        2:
                      3: #include "nd.h"
                      4:
1.11      noro        5: int Nnd_add,Nf4_red;
1.12      noro        6: struct oEGT eg_search,f4_symb,f4_conv,f4_elim1,f4_elim2;
1.1       noro        7:
                      8: int diag_period = 6;
                      9: int weight_check = 1;
                     10: int (*ndl_compare_function)(UINT *a1,UINT *a2);
1.21      noro       11: /* for general module order */
1.19      noro       12: int (*ndl_base_compare_function)(UINT *a1,UINT *a2);
1.21      noro       13: int (*dl_base_compare_function)(int nv,DL a,DL b);
                     14: int nd_base_ordtype;
1.1       noro       15: int nd_dcomp;
                     16: int nd_rref2;
                     17: NM _nm_free_list;
                     18: ND _nd_free_list;
                     19: ND_pairs _ndp_free_list;
                     20: NODE nd_hcf;
1.35      noro       21: int Nsyz,Nsamesig;
1.1       noro       22:
                     23: Obj nd_top_weight;
                     24:
                     25: static NODE nd_subst;
                     26: static VL nd_vc;
                     27: static int nd_ntrans;
                     28: static int nd_nalg;
                     29: #if 0
                     30: static int ndv_alloc;
                     31: #endif
                     32: #if 1
                     33: static int nd_f4_nsp=0x7fffffff;
                     34: #else
                     35: static int nd_f4_nsp=50;
                     36: #endif
                     37: static double nd_scale=2;
                     38: static UINT **nd_bound;
                     39: static struct order_spec *nd_ord;
                     40: static EPOS nd_epos;
                     41: static BlockMask nd_blockmask;
                     42: static int nd_nvar;
                     43: static int nd_isrlex;
                     44: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
                     45: static UINT nd_mask[32];
                     46: static UINT nd_mask0,nd_mask1;
                     47:
                     48: static NDV *nd_ps;
                     49: static NDV *nd_ps_trace;
                     50: static NDV *nd_ps_sym;
                     51: static NDV *nd_ps_trace_sym;
                     52: static RHist *nd_psh;
1.30      noro       53: static int nd_psn,nd_pslen,nd_nbase;
1.1       noro       54: static RHist *nd_red;
                     55: static int *nd_work_vector;
                     56: static int **nd_matrix;
                     57: static int nd_matrix_len;
                     58: static struct weight_or_block *nd_worb;
                     59: static int nd_worb_len;
                     60: static int nd_found,nd_create,nd_notfirst;
                     61: static int nmv_adv;
                     62: static int nd_demand;
1.21      noro       63: static int nd_module,nd_module_ordtype,nd_mpos,nd_pot_nelim;
1.1       noro       64: static int nd_module_rank,nd_poly_weight_len;
                     65: static int *nd_poly_weight,*nd_module_weight;
                     66: static NODE nd_tracelist;
                     67: static NODE nd_alltracelist;
                     68: static int nd_gentrace,nd_gensyz,nd_nora,nd_newelim,nd_intersect,nd_lf;
1.38    ! noro       69: static int nd_f4_td,nd_sba_f4step,nd_sba_pot,nd_sba_largelcm;
1.1       noro       70: static int *nd_gbblock;
                     71: static NODE nd_nzlist,nd_check_splist;
                     72: static int nd_splist;
                     73: static int *nd_sugarweight;
                     74: static int nd_f4red,nd_rank0,nd_last_nonzero;
1.27      noro       75: static DL *nd_sba_hm;
1.29      noro       76: static NODE *nd_sba_pos;
1.1       noro       77:
                     78: NumberField get_numberfield();
                     79: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
                     80: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
                     81: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
                     82: int nd_monic(int m,ND *p);
                     83: NDV plain_vect_to_ndv_q(Z *mat,int col,UINT *s0vect);
                     84: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank);
                     85: NDV pltondv(VL vl,VL dvl,LIST p);
                     86: void pltozpl(LIST l,Q *cont,LIST *pp);
                     87: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
                     88: void nmtodp(int mod,NM m,DP *r);
1.15      noro       89: void ndltodp(UINT *d,DP *r);
1.1       noro       90: NODE reverse_node(NODE n);
                     91: P ndc_div(int mod,union oNDC a,union oNDC b);
                     92: P ndctop(int mod,union oNDC c);
                     93: void finalize_tracelist(int i,P cont);
                     94: void conv_ilist(int demand,int trace,NODE g,int **indp);
                     95: void parse_nd_option(NODE opt);
                     96: void dltondl(int n,DL dl,UINT *r);
                     97: DP ndvtodp(int mod,NDV p);
                     98: DP ndtodp(int mod,ND p);
1.16      noro       99: DPM ndvtodpm(int mod,NDV p);
                    100: NDV dpmtondv(int mod,DPM p);
                    101: int dpm_getdeg(DPM p,int *rank);
                    102: void dpm_ptozp(DPM p,Z *cont,DPM *r);
                    103: int compdmm(int nv,DMM a,DMM b);
1.1       noro      104:
                    105: void Pdp_set_weight(NODE,VECT *);
                    106: void Pox_cmo_rpc(NODE,Obj *);
                    107:
                    108: ND nd_add_lf(ND p1,ND p2);
                    109: void nd_mul_c_lf(ND p,Z mul);
                    110: void ndv_mul_c_lf(NDV p,Z mul);
                    111: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                    112:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
                    113: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                    114:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
                    115: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                    116:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
                    117: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
                    118: NODE nd_f4_lf_trace_main(int m,int **indp);
                    119: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
                    120:
                    121: extern int lf_lazy;
                    122: extern Z current_mod_lf;
                    123:
                    124: extern int Denominator,DP_Multiple,MaxDeg;
                    125:
                    126: #define BLEN (8*sizeof(unsigned long))
                    127:
                    128: typedef struct matrix {
                    129:   int row,col;
                    130:   unsigned long **a;
                    131: } *matrix;
                    132:
                    133:
                    134: void nd_free_private_storage()
                    135: {
                    136:     _nm_free_list = 0;
                    137:     _ndp_free_list = 0;
                    138: #if 0
                    139:     GC_gcollect();
                    140: #endif
                    141: }
                    142:
                    143: void _NM_alloc()
                    144: {
                    145:     NM p;
                    146:     int i;
                    147:
                    148:     for ( i = 0; i < 1024; i++ ) {
                    149:         p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                    150:         p->next = _nm_free_list; _nm_free_list = p;
                    151:     }
                    152: }
                    153:
                    154: matrix alloc_matrix(int row,int col)
                    155: {
                    156:   unsigned long **a;
                    157:   int i,len,blen;
                    158:   matrix mat;
                    159:
                    160:   mat = (matrix)MALLOC(sizeof(struct matrix));
                    161:   mat->row = row;
                    162:   mat->col = col;
                    163:   mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
                    164:   return mat;
                    165: }
                    166:
                    167:
                    168: void _ND_alloc()
                    169: {
                    170:     ND p;
                    171:     int i;
                    172:
                    173:     for ( i = 0; i < 1024; i++ ) {
                    174:         p = (ND)MALLOC(sizeof(struct oND));
                    175:         p->body = (NM)_nd_free_list; _nd_free_list = p;
                    176:     }
                    177: }
                    178:
                    179: void _NDP_alloc()
                    180: {
                    181:     ND_pairs p;
                    182:     int i;
                    183:
                    184:     for ( i = 0; i < 1024; i++ ) {
                    185:         p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
                    186:             +(nd_wpd-1)*sizeof(UINT));
                    187:         p->next = _ndp_free_list; _ndp_free_list = p;
                    188:     }
                    189: }
                    190:
                    191: INLINE int nd_length(ND p)
                    192: {
                    193:     NM m;
                    194:     int i;
                    195:
                    196:     if ( !p )
                    197:         return 0;
                    198:     else {
                    199:         for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
                    200:         return i;
                    201:     }
                    202: }
                    203:
                    204: extern int dp_negative_weight;
                    205:
                    206: INLINE int ndl_reducible(UINT *d1,UINT *d2)
                    207: {
                    208:     UINT u1,u2;
                    209:     int i,j;
                    210:
                    211:     if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
                    212:
                    213:     if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
                    214: #if USE_UNROLL
                    215:     switch ( nd_bpe ) {
                    216:         case 3:
                    217:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    218:                 u1 = d1[i]; u2 = d2[i];
                    219:                 if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
                    220:                 if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
                    221:                 if ( (u1&  0xe00000) < (u2&  0xe00000) ) return 0;
                    222:                 if ( (u1&  0x1c0000) < (u2&  0x1c0000) ) return 0;
                    223:                 if ( (u1&   0x38000) < (u2&   0x38000) ) return 0;
                    224:                 if ( (u1&    0x7000) < (u2&    0x7000) ) return 0;
                    225:                 if ( (u1&     0xe00) < (u2&     0xe00) ) return 0;
                    226:                 if ( (u1&     0x1c0) < (u2&     0x1c0) ) return 0;
                    227:                 if ( (u1&      0x38) < (u2&      0x38) ) return 0;
                    228:                 if ( (u1&       0x7) < (u2&       0x7) ) return 0;
                    229:             }
                    230:             return 1;
                    231:             break;
                    232:         case 4:
                    233:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    234:                 u1 = d1[i]; u2 = d2[i];
                    235:                 if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
                    236:                 if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
                    237:                 if ( (u1&  0xf00000) < (u2&  0xf00000) ) return 0;
                    238:                 if ( (u1&   0xf0000) < (u2&   0xf0000) ) return 0;
                    239:                 if ( (u1&    0xf000) < (u2&    0xf000) ) return 0;
                    240:                 if ( (u1&     0xf00) < (u2&     0xf00) ) return 0;
                    241:                 if ( (u1&      0xf0) < (u2&      0xf0) ) return 0;
                    242:                 if ( (u1&       0xf) < (u2&       0xf) ) return 0;
                    243:             }
                    244:             return 1;
                    245:             break;
                    246:         case 6:
                    247:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    248:                 u1 = d1[i]; u2 = d2[i];
                    249:                 if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
                    250:                 if ( (u1&  0xfc0000) < (u2&  0xfc0000) ) return 0;
                    251:                 if ( (u1&   0x3f000) < (u2&   0x3f000) ) return 0;
                    252:                 if ( (u1&     0xfc0) < (u2&     0xfc0) ) return 0;
                    253:                 if ( (u1&      0x3f) < (u2&      0x3f) ) return 0;
                    254:             }
                    255:             return 1;
                    256:             break;
                    257:         case 8:
                    258:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    259:                 u1 = d1[i]; u2 = d2[i];
                    260:                 if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
                    261:                 if ( (u1&  0xff0000) < (u2&  0xff0000) ) return 0;
                    262:                 if ( (u1&    0xff00) < (u2&    0xff00) ) return 0;
                    263:                 if ( (u1&      0xff) < (u2&      0xff) ) return 0;
                    264:             }
                    265:             return 1;
                    266:             break;
                    267:         case 16:
                    268:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    269:                 u1 = d1[i]; u2 = d2[i];
                    270:                 if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
                    271:                 if ( (u1&    0xffff) < (u2&    0xffff) ) return 0;
                    272:             }
                    273:             return 1;
                    274:             break;
                    275:         case 32:
                    276:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                    277:                 if ( d1[i] < d2[i] ) return 0;
                    278:             return 1;
                    279:             break;
                    280:         default:
                    281:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    282:                 u1 = d1[i]; u2 = d2[i];
                    283:                 for ( j = 0; j < nd_epw; j++ )
                    284:                     if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    285:             }
                    286:             return 1;
                    287:     }
                    288: #else
                    289:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    290:         u1 = d1[i]; u2 = d2[i];
                    291:         for ( j = 0; j < nd_epw; j++ )
                    292:             if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    293:     }
                    294:     return 1;
                    295: #endif
                    296: }
                    297:
                    298: /*
                    299:  * If the current order is a block order,
                    300:  * then the last block is length 1 and contains
                    301:  * the homo variable. Otherwise, the original
                    302:  * order is either 0 or 2.
                    303:  */
                    304:
                    305: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
                    306: {
                    307:     int w,i,e,n,omask0;
                    308:
                    309:     omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
                    310:     n = nd_nvar-1;
                    311:     ndl_zero(r);
                    312:     for ( i = 0; i < n; i++ ) {
                    313:         e = GET_EXP_OLD(d,i);
                    314:         PUT_EXP(r,i,e);
                    315:     }
                    316:     w = TD(d);
                    317:     PUT_EXP(r,nd_nvar-1,weight-w);
                    318:     if ( nd_module ) MPOS(r) = d[ompos];
                    319:     TD(r) = weight;
                    320:     if ( nd_blockmask ) ndl_weight_mask(r);
                    321: }
                    322:
                    323: void ndl_dehomogenize(UINT *d)
                    324: {
                    325:     UINT mask;
                    326:     UINT h;
                    327:     int i,bits;
                    328:
                    329:     if ( nd_blockmask ) {
                    330:         h = GET_EXP(d,nd_nvar-1);
                    331:         XOR_EXP(d,nd_nvar-1,h);
                    332:         TD(d) -= h;
                    333:         ndl_weight_mask(d);
                    334:     } else {
                    335:         if ( nd_isrlex ) {
                    336:             if ( nd_bpe == 32 ) {
                    337:                 h = d[nd_exporigin];
                    338:                 for ( i = nd_exporigin+1; i < nd_wpd; i++ )
                    339:                     d[i-1] = d[i];
                    340:                 d[i-1] = 0;
                    341:                 TD(d) -= h;
                    342:             } else {
                    343:                 bits = nd_epw*nd_bpe;
                    344:                 mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
                    345:                 h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
                    346:                 for ( i = nd_exporigin; i < nd_wpd; i++ )
                    347:                     d[i] = ((d[i]<<nd_bpe)&mask)
                    348:                         |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
                    349:                 TD(d) -= h;
                    350:             }
                    351:         } else {
                    352:             h = GET_EXP(d,nd_nvar-1);
                    353:             XOR_EXP(d,nd_nvar-1,h);
                    354:             TD(d) -= h;
                    355:         }
                    356:     }
                    357: }
                    358:
                    359: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
                    360: {
                    361:     UINT t1,t2,u,u1,u2;
                    362:     int i,j,l;
                    363:
                    364:     if ( nd_module && (MPOS(d1) != MPOS(d2)) )
                    365:         error("ndl_lcm : inconsistent monomials");
                    366: #if USE_UNROLL
                    367:     switch ( nd_bpe ) {
                    368:         case 3:
                    369:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    370:                 u1 = d1[i]; u2 = d2[i];
                    371:                 t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
                    372:                 t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
                    373:                 t1 = (u1&  0xe00000); t2 = (u2&  0xe00000); u |= t1>t2?t1:t2;
                    374:                 t1 = (u1&  0x1c0000); t2 = (u2&  0x1c0000); u |= t1>t2?t1:t2;
                    375:                 t1 = (u1&   0x38000); t2 = (u2&   0x38000); u |= t1>t2?t1:t2;
                    376:                 t1 = (u1&    0x7000); t2 = (u2&    0x7000); u |= t1>t2?t1:t2;
                    377:                 t1 = (u1&     0xe00); t2 = (u2&     0xe00); u |= t1>t2?t1:t2;
                    378:                 t1 = (u1&     0x1c0); t2 = (u2&     0x1c0); u |= t1>t2?t1:t2;
                    379:                 t1 = (u1&      0x38); t2 = (u2&      0x38); u |= t1>t2?t1:t2;
                    380:                 t1 = (u1&       0x7); t2 = (u2&       0x7); u |= t1>t2?t1:t2;
                    381:                 d[i] = u;
                    382:             }
                    383:             break;
                    384:         case 4:
                    385:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    386:                 u1 = d1[i]; u2 = d2[i];
                    387:                 t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
                    388:                 t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
                    389:                 t1 = (u1&  0xf00000); t2 = (u2&  0xf00000); u |= t1>t2?t1:t2;
                    390:                 t1 = (u1&   0xf0000); t2 = (u2&   0xf0000); u |= t1>t2?t1:t2;
                    391:                 t1 = (u1&    0xf000); t2 = (u2&    0xf000); u |= t1>t2?t1:t2;
                    392:                 t1 = (u1&     0xf00); t2 = (u2&     0xf00); u |= t1>t2?t1:t2;
                    393:                 t1 = (u1&      0xf0); t2 = (u2&      0xf0); u |= t1>t2?t1:t2;
                    394:                 t1 = (u1&       0xf); t2 = (u2&       0xf); u |= t1>t2?t1:t2;
                    395:                 d[i] = u;
                    396:             }
                    397:             break;
                    398:         case 6:
                    399:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    400:                 u1 = d1[i]; u2 = d2[i];
                    401:                 t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
                    402:                 t1 = (u1&  0xfc0000); t2 = (u2&  0xfc0000); u |= t1>t2?t1:t2;
                    403:                 t1 = (u1&   0x3f000); t2 = (u2&   0x3f000); u |= t1>t2?t1:t2;
                    404:                 t1 = (u1&     0xfc0); t2 = (u2&     0xfc0); u |= t1>t2?t1:t2;
                    405:                 t1 = (u1&      0x3f); t2 = (u2&      0x3f); u |= t1>t2?t1:t2;
                    406:                 d[i] = u;
                    407:             }
                    408:             break;
                    409:         case 8:
                    410:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    411:                 u1 = d1[i]; u2 = d2[i];
                    412:                 t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
                    413:                 t1 = (u1&  0xff0000); t2 = (u2&  0xff0000); u |= t1>t2?t1:t2;
                    414:                 t1 = (u1&    0xff00); t2 = (u2&    0xff00); u |= t1>t2?t1:t2;
                    415:                 t1 = (u1&      0xff); t2 = (u2&      0xff); u |= t1>t2?t1:t2;
                    416:                 d[i] = u;
                    417:             }
                    418:             break;
                    419:         case 16:
                    420:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    421:                 u1 = d1[i]; u2 = d2[i];
                    422:                 t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
                    423:                 t1 = (u1&    0xffff); t2 = (u2&    0xffff); u |= t1>t2?t1:t2;
                    424:                 d[i] = u;
                    425:             }
                    426:             break;
                    427:         case 32:
                    428:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    429:                 u1 = d1[i]; u2 = d2[i];
                    430:                 d[i] = u1>u2?u1:u2;
                    431:             }
                    432:             break;
                    433:         default:
                    434:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    435:                 u1 = d1[i]; u2 = d2[i];
                    436:                 for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    437:                     t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    438:                 }
                    439:                 d[i] = u;
                    440:             }
                    441:             break;
                    442:     }
                    443: #else
                    444:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    445:         u1 = d1[i]; u2 = d2[i];
                    446:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    447:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    448:         }
                    449:         d[i] = u;
                    450:     }
                    451: #endif
                    452:     if ( nd_module ) MPOS(d) = MPOS(d1);
                    453:     TD(d) = ndl_weight(d);
                    454:     if ( nd_blockmask ) ndl_weight_mask(d);
                    455: }
                    456:
                    457: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
                    458: {
                    459:     UINT t1,t2,u,u1,u2;
                    460:     int i,j,l;
                    461:
                    462:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    463:         u1 = d1[i]; u2 = d2[i];
                    464:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    465:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    466:         }
                    467:         d[i] = u;
                    468:     }
                    469: }
                    470:
                    471: int ndl_weight(UINT *d)
                    472: {
                    473:     UINT t,u;
                    474:     int i,j;
                    475:
                    476:     if ( current_dl_weight_vector )
                    477:         for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    478:             u = GET_EXP(d,i);
                    479:             t += MUL_WEIGHT(u,i);
                    480:         }
                    481:     else
                    482:         for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    483:             u = d[i];
                    484:             for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    485:                 t += (u&nd_mask0);
                    486:         }
1.20      noro      487:     if ( nd_module && nd_module_rank && MPOS(d) )
                    488:         t += nd_module_weight[MPOS(d)-1];
                    489:     for ( i = nd_exporigin; i < nd_wpd; i++ )
                    490:       if ( d[i] && !t )
                    491:         printf("afo\n");
1.1       noro      492:     return t;
                    493: }
                    494:
                    495: /* for sugarweight */
                    496:
                    497: int ndl_weight2(UINT *d)
                    498: {
                    499:     int t,u;
                    500:     int i,j;
                    501:
                    502:     for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    503:         u = GET_EXP(d,i);
                    504:         t += nd_sugarweight[i]*u;
                    505:     }
1.20      noro      506:     if ( nd_module && nd_module_rank && MPOS(d) )
                    507:         t += nd_module_weight[MPOS(d)-1];
1.1       noro      508:     return t;
                    509: }
                    510:
                    511: void ndl_weight_mask(UINT *d)
                    512: {
                    513:     UINT t,u;
                    514:     UINT *mask;
                    515:     int i,j,k,l;
                    516:
                    517:     l = nd_blockmask->n;
                    518:     for ( k = 0; k < l; k++ ) {
                    519:         mask = nd_blockmask->mask[k];
                    520:         if ( current_dl_weight_vector )
                    521:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    522:                 u = GET_EXP_MASK(d,i,mask);
                    523:                 t += MUL_WEIGHT(u,i);
                    524:             }
                    525:         else
                    526:             for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    527:                 u = d[i]&mask[i];
                    528:                 for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    529:                     t += (u&nd_mask0);
                    530:             }
                    531:         d[k+1] = t;
                    532:     }
                    533: }
                    534:
1.21      noro      535: int ndl_glex_compare(UINT *d1,UINT *d2)
                    536: {
                    537:   if ( TD(d1) > TD(d2) ) return 1;
                    538:   else if ( TD(d1) < TD(d2) ) return -1;
                    539:   else return ndl_lex_compare(d1,d2);
                    540: }
                    541:
1.1       noro      542: int ndl_lex_compare(UINT *d1,UINT *d2)
                    543: {
                    544:     int i;
                    545:
                    546:     d1 += nd_exporigin;
                    547:     d2 += nd_exporigin;
                    548:     for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
                    549:         if ( *d1 > *d2 )
                    550:             return nd_isrlex ? -1 : 1;
                    551:         else if ( *d1 < *d2 )
                    552:             return nd_isrlex ? 1 : -1;
                    553:     return 0;
                    554: }
                    555:
                    556: int ndl_block_compare(UINT *d1,UINT *d2)
                    557: {
                    558:     int i,l,j,ord_o,ord_l;
                    559:     struct order_pair *op;
                    560:     UINT t1,t2,m;
                    561:     UINT *mask;
                    562:
                    563:     l = nd_blockmask->n;
                    564:     op = nd_blockmask->order_pair;
                    565:     for ( j = 0; j < l; j++ ) {
                    566:         mask = nd_blockmask->mask[j];
                    567:         ord_o = op[j].order;
                    568:         if ( ord_o < 2 ) {
                    569:             if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
                    570:             else if ( t1 < t2 ) return -1;
                    571:         }
                    572:         for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    573:             m = mask[i];
                    574:             t1 = d1[i]&m;
                    575:             t2 = d2[i]&m;
                    576:             if ( t1 > t2 )
                    577:                 return !ord_o ? -1 : 1;
                    578:             else if ( t1 < t2 )
                    579:                 return !ord_o ? 1 : -1;
                    580:         }
                    581:     }
                    582:     return 0;
                    583: }
                    584:
                    585: int ndl_matrix_compare(UINT *d1,UINT *d2)
                    586: {
                    587:   int i,j,s,row;
                    588:   int *v;
                    589:   Z **mat;
                    590:   Z *w;
                    591:   Z t1;
                    592:   Z t,t2;
                    593:
1.6       noro      594:   for ( j = 0; j < nd_nvar; j++ )
                    595:     nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.1       noro      596:   if ( nd_top_weight ) {
                    597:     if ( OID(nd_top_weight) == O_VECT ) {
1.6       noro      598:       mat = (Z **)&BDY((VECT)nd_top_weight);
                    599:       row = 1;
1.1       noro      600:     } else {
                    601:       mat = (Z **)BDY((MAT)nd_top_weight);
1.6       noro      602:       row = ((MAT)nd_top_weight)->row;
1.1       noro      603:     }
                    604:     for ( i = 0; i < row; i++ ) {
1.6       noro      605:       w = mat[i];
1.1       noro      606:       for ( j = 0, t = 0; j < nd_nvar; j++ ) {
1.6       noro      607:         STOZ(nd_work_vector[j],t1);
1.1       noro      608:         mulz(w[j],t1,&t2);
                    609:         addz(t,t2,&t1);
                    610:         t = t1;
                    611:       }
1.6       noro      612:       if ( t ) {
                    613:         s = sgnz(t);
1.1       noro      614:         if ( s > 0 ) return 1;
                    615:         else if ( s < 0 ) return -1;
1.6       noro      616:       }
1.1       noro      617:     }
1.6       noro      618:   }
                    619:   for ( i = 0; i < nd_matrix_len; i++ ) {
                    620:     v = nd_matrix[i];
                    621:     for ( j = 0, s = 0; j < nd_nvar; j++ )
                    622:       s += v[j]*nd_work_vector[j];
                    623:     if ( s > 0 ) return 1;
                    624:     else if ( s < 0 ) return -1;
                    625:   }
1.1       noro      626:   if ( !ndl_equal(d1,d2) )
1.6       noro      627:     error("ndl_matrix_compare : invalid matrix");
                    628:   return 0;
1.1       noro      629: }
                    630:
                    631: int ndl_composite_compare(UINT *d1,UINT *d2)
                    632: {
                    633:     int i,j,s,start,end,len,o;
                    634:     int *v;
                    635:     struct sparse_weight *sw;
                    636:
                    637:     for ( j = 0; j < nd_nvar; j++ )
                    638:         nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
                    639:     for ( i = 0; i < nd_worb_len; i++ ) {
                    640:         len = nd_worb[i].length;
                    641:         switch ( nd_worb[i].type ) {
                    642:             case IS_DENSE_WEIGHT:
                    643:                 v = nd_worb[i].body.dense_weight;
                    644:                 for ( j = 0, s = 0; j < len; j++ )
                    645:                     s += v[j]*nd_work_vector[j];
                    646:                 if ( s > 0 ) return 1;
                    647:                 else if ( s < 0 ) return -1;
                    648:                 break;
                    649:             case IS_SPARSE_WEIGHT:
                    650:                 sw = nd_worb[i].body.sparse_weight;
                    651:                 for ( j = 0, s = 0; j < len; j++ )
                    652:                     s += sw[j].value*nd_work_vector[sw[j].pos];
                    653:                 if ( s > 0 ) return 1;
                    654:                 else if ( s < 0 ) return -1;
                    655:                 break;
                    656:             case IS_BLOCK:
                    657:                 o = nd_worb[i].body.block.order;
                    658:                 start = nd_worb[i].body.block.start;
                    659:                 switch ( o ) {
                    660:                     case 0:
                    661:                         end = start+len;
                    662:                         for ( j = start, s = 0; j < end; j++ )
                    663:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    664:                         if ( s > 0 ) return 1;
                    665:                         else if ( s < 0 ) return -1;
                    666:                         for ( j = end-1; j >= start; j-- )
                    667:                             if ( nd_work_vector[j] < 0 ) return 1;
                    668:                             else if ( nd_work_vector[j] > 0 ) return -1;
                    669:                         break;
                    670:                     case 1:
                    671:                         end = start+len;
                    672:                         for ( j = start, s = 0; j < end; j++ )
                    673:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    674:                         if ( s > 0 ) return 1;
                    675:                         else if ( s < 0 ) return -1;
                    676:                         for ( j = start; j < end; j++ )
                    677:                             if ( nd_work_vector[j] > 0 ) return 1;
                    678:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    679:                         break;
                    680:                     case 2:
                    681:                         end = start+len;
                    682:                         for ( j = start; j < end; j++ )
                    683:                             if ( nd_work_vector[j] > 0 ) return 1;
                    684:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    685:                         break;
                    686:                 }
                    687:                 break;
                    688:         }
                    689:     }
                    690:     return 0;
                    691: }
                    692:
                    693: /* TDH -> WW -> TD-> RL */
                    694:
                    695: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
                    696: {
                    697:     int i,m,e1,e2;
                    698:
                    699:     if ( TD(d1) > TD(d2) ) return 1;
                    700:     else if ( TD(d1) < TD(d2) ) return -1;
                    701:     m = nd_nvar>>1;
                    702:     for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
                    703:         e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
                    704:         e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
                    705:     }
                    706:     if ( e1 > e2 ) return 1;
                    707:     else if ( e1 < e2 ) return -1;
                    708:     return ndl_lex_compare(d1,d2);
                    709: }
                    710:
1.21      noro      711: // common function for module glex and grlex comparison
                    712: int ndl_module_glex_compare(UINT *d1,UINT *d2)
1.1       noro      713: {
1.21      noro      714:   int c;
1.1       noro      715:
1.21      noro      716:   switch ( nd_module_ordtype ) {
                    717:     case 0:
                    718:       if ( TD(d1) > TD(d2) ) return 1;
                    719:       else if ( TD(d1) < TD(d2) ) return -1;
                    720:       else if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
                    721:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    722:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    723:       else return 0;
                    724:       break;
1.1       noro      725:
1.21      noro      726:     case 1:
1.19      noro      727:       if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
                    728:          if ( TD(d1) > TD(d2) ) return 1;
                    729:          else if ( TD(d1) < TD(d2) ) return -1;
                    730:          if ( (c = ndl_lex_compare(d1,d2)) != 0 ) return c;
                    731:          if ( MPOS(d1) < MPOS(d2) ) return 1;
                    732:          else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    733:       }
                    734:       if ( MPOS(d1) < MPOS(d2) ) return 1;
                    735:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
1.21      noro      736:       else if ( TD(d1) > TD(d2) ) return 1;
                    737:       else if ( TD(d1) < TD(d2) ) return -1;
                    738:       else return ndl_lex_compare(d1,d2);
                    739:       break;
1.1       noro      740:
1.21      noro      741:     case 2: // weight -> POT
                    742:       if ( TD(d1) > TD(d2) ) return 1;
                    743:       else if ( TD(d1) < TD(d2) ) return -1;
                    744:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    745:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    746:       else return ndl_lex_compare(d1,d2);
                    747:       break;
1.1       noro      748:
1.21      noro      749:     default:
                    750:       error("ndl_module_glex_compare : invalid module_ordtype");
1.28      noro      751:       return 0;
1.21      noro      752:   }
1.1       noro      753: }
                    754:
1.21      noro      755: // common  for module comparison
                    756: int ndl_module_compare(UINT *d1,UINT *d2)
1.1       noro      757: {
1.21      noro      758:   int c;
1.1       noro      759:
1.21      noro      760:   switch ( nd_module_ordtype ) {
                    761:     case 0:
1.23      noro      762:       if ( (c = (*ndl_base_compare_function)(d1,d2)) != 0 ) return c;
1.21      noro      763:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    764:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    765:       else return 0;
                    766:       break;
1.1       noro      767:
1.21      noro      768:     case 1:
                    769:       if ( MPOS(d1) < MPOS(d2) ) return 1;
                    770:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    771:       else return (*ndl_base_compare_function)(d1,d2);
                    772:       break;
1.1       noro      773:
1.21      noro      774:     case 2: // weight -> POT
                    775:       if ( TD(d1) > TD(d2) ) return 1;
                    776:       else if ( TD(d1) < TD(d2) ) return -1;
                    777:       else if ( MPOS(d1) < MPOS(d2) ) return 1;
                    778:       else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    779:       else return (*ndl_base_compare_function)(d1,d2);
                    780:       break;
1.1       noro      781:
1.21      noro      782:     default:
                    783:       error("ndl_module_compare : invalid module_ordtype");
1.28      noro      784:       return 0;
1.21      noro      785:   }
1.1       noro      786: }
                    787:
1.21      noro      788: extern DMMstack dmm_stack;
                    789: void _addtodl(int n,DL d1,DL d2);
1.31      noro      790: void _adddl(int n,DL d1,DL d2,DL d3);
1.21      noro      791: int _eqdl(int n,DL d1,DL d2);
                    792:
                    793: int ndl_module_schreyer_compare(UINT *m1,UINT *m2)
                    794: {
                    795:   int pos1,pos2,t,j;
                    796:   DMM *in;
                    797:   DMMstack s;
                    798:   static DL d1=0;
                    799:   static DL d2=0;
                    800:   static int dlen=0;
                    801:
                    802:   pos1 = MPOS(m1); pos2 = MPOS(m2);
                    803:   if ( pos1 == pos2 ) return (*ndl_base_compare_function)(m1,m2);
                    804:   if ( nd_nvar > dlen ) {
                    805:     NEWDL(d1,nd_nvar);
                    806:     NEWDL(d2,nd_nvar);
                    807:     dlen = nd_nvar;
                    808:   }
                    809:   d1->td = TD(m1);
                    810:   for ( j = 0; j < nd_nvar; j++ ) d1->d[j] = GET_EXP(m1,j);
                    811:   d2->td = TD(m2);
                    812:   for ( j = 0; j < nd_nvar; j++ ) d2->d[j] = GET_EXP(m2,j);
                    813:   for ( s = dmm_stack; s; s = NEXT(s) ) {
                    814:     in = s->in;
                    815:     _addtodl(nd_nvar,in[pos1]->dl,d1);
                    816:     _addtodl(nd_nvar,in[pos2]->dl,d2);
                    817:     if ( in[pos1]->pos == in[pos2]->pos && _eqdl(nd_nvar,d1,d2)) {
                    818:       if ( pos1 < pos2 ) return 1;
                    819:       else if ( pos1 > pos2 ) return -1;
                    820:       else return 0;
                    821:     }
                    822:     pos1 = in[pos1]->pos;
                    823:     pos2 = in[pos2]->pos;
                    824:     if ( pos1 == pos2 ) return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    825:   }
                    826:   // comparison by the bottom order
                    827: LAST:
                    828:   switch ( nd_base_ordtype ) {
                    829:     case 0:
                    830:       t = (*dl_base_compare_function)(nd_nvar,d1,d2);
                    831:       if ( t ) return t;
                    832:       else if ( pos1 < pos2 ) return 1;
                    833:       else if ( pos1 > pos2 ) return -1;
                    834:       else return 0;
                    835:       break;
                    836:     case 1:
                    837:       if ( pos1 < pos2 ) return 1;
                    838:       else if ( pos1 > pos2 ) return -1;
                    839:       else return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    840:       break;
                    841:     case 2:
                    842:       if ( d1->td > d2->td  ) return 1;
                    843:       else if ( d1->td < d2->td ) return -1;
                    844:       else if ( pos1 < pos2 ) return 1;
                    845:       else if ( pos1 > pos2 ) return -1;
                    846:       else return (*dl_base_compare_function)(nd_nvar,d1,d2);
                    847:       break;
                    848:     default:
                    849:       error("ndl_schreyer_compare : invalid base ordtype");
1.28      noro      850:       return 0;
1.21      noro      851:   }
1.1       noro      852: }
                    853:
                    854: INLINE int ndl_equal(UINT *d1,UINT *d2)
                    855: {
                    856:     int i;
                    857:
                    858:     switch ( nd_wpd ) {
                    859:         case 2:
                    860:             if ( TD(d2) != TD(d1) ) return 0;
                    861:             if ( d2[1] != d1[1] ) return 0;
                    862:             return 1;
                    863:             break;
                    864:         case 3:
                    865:             if ( TD(d2) != TD(d1) ) return 0;
                    866:             if ( d2[1] != d1[1] ) return 0;
                    867:             if ( d2[2] != d1[2] ) return 0;
                    868:             return 1;
                    869:             break;
                    870:         default:
                    871:             for ( i = 0; i < nd_wpd; i++ )
                    872:                 if ( *d1++ != *d2++ ) return 0;
                    873:             return 1;
                    874:             break;
                    875:     }
                    876: }
                    877:
                    878: INLINE void ndl_copy(UINT *d1,UINT *d2)
                    879: {
                    880:     int i;
                    881:
                    882:     switch ( nd_wpd ) {
                    883:         case 2:
                    884:             TD(d2) = TD(d1);
                    885:             d2[1] = d1[1];
                    886:             break;
                    887:         case 3:
                    888:             TD(d2) = TD(d1);
                    889:             d2[1] = d1[1];
                    890:             d2[2] = d1[2];
                    891:             break;
                    892:         default:
                    893:             for ( i = 0; i < nd_wpd; i++ )
                    894:                 d2[i] = d1[i];
                    895:             break;
                    896:     }
                    897: }
                    898:
                    899: INLINE void ndl_zero(UINT *d)
                    900: {
                    901:     int i;
                    902:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                    903: }
                    904:
                    905: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
                    906: {
                    907:     int i;
                    908:
                    909:     if ( nd_module ) {
                    910:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
                    911:         error("ndl_add : invalid operation");
                    912:     }
                    913: #if 1
                    914:     switch ( nd_wpd ) {
                    915:         case 2:
                    916:             TD(d) = TD(d1)+TD(d2);
                    917:             d[1] = d1[1]+d2[1];
                    918:             break;
                    919:         case 3:
                    920:             TD(d) = TD(d1)+TD(d2);
                    921:             d[1] = d1[1]+d2[1];
                    922:             d[2] = d1[2]+d2[2];
                    923:             break;
                    924:         default:
                    925:             for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    926:             break;
                    927:     }
                    928: #else
                    929:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    930: #endif
                    931: }
                    932:
                    933: /* d1 += d2 */
                    934: INLINE void ndl_addto(UINT *d1,UINT *d2)
                    935: {
                    936:     int i;
                    937:
                    938:     if ( nd_module ) {
                    939:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
                    940:             error("ndl_addto : invalid operation");
                    941:     }
                    942: #if 1
                    943:     switch ( nd_wpd ) {
                    944:         case 2:
                    945:             TD(d1) += TD(d2);
                    946:             d1[1] += d2[1];
                    947:             break;
                    948:         case 3:
                    949:             TD(d1) += TD(d2);
                    950:             d1[1] += d2[1];
                    951:             d1[2] += d2[2];
                    952:             break;
                    953:         default:
                    954:             for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    955:             break;
                    956:     }
                    957: #else
                    958:     for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    959: #endif
                    960: }
                    961:
                    962: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
                    963: {
                    964:     int i;
                    965:
                    966:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
                    967: }
                    968:
                    969: int ndl_disjoint(UINT *d1,UINT *d2)
                    970: {
                    971:     UINT t1,t2,u,u1,u2;
                    972:     int i,j;
                    973:
                    974:     if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
                    975: #if USE_UNROLL
                    976:     switch ( nd_bpe ) {
                    977:         case 3:
                    978:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    979:                 u1 = d1[i]; u2 = d2[i];
                    980:                 t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
                    981:                 t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
                    982:                 t1 = u1&  0xe00000; t2 = u2&  0xe00000; if ( t1&&t2 ) return 0;
                    983:                 t1 = u1&  0x1c0000; t2 = u2&  0x1c0000; if ( t1&&t2 ) return 0;
                    984:                 t1 = u1&   0x38000; t2 = u2&   0x38000; if ( t1&&t2 ) return 0;
                    985:                 t1 = u1&    0x7000; t2 = u2&    0x7000; if ( t1&&t2 ) return 0;
                    986:                 t1 = u1&     0xe00; t2 = u2&     0xe00; if ( t1&&t2 ) return 0;
                    987:                 t1 = u1&     0x1c0; t2 = u2&     0x1c0; if ( t1&&t2 ) return 0;
                    988:                 t1 = u1&      0x38; t2 = u2&      0x38; if ( t1&&t2 ) return 0;
                    989:                 t1 = u1&       0x7; t2 = u2&       0x7; if ( t1&&t2 ) return 0;
                    990:             }
                    991:             return 1;
                    992:             break;
                    993:         case 4:
                    994:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    995:                 u1 = d1[i]; u2 = d2[i];
                    996:                 t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
                    997:                 t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
                    998:                 t1 = u1&  0xf00000; t2 = u2&  0xf00000; if ( t1&&t2 ) return 0;
                    999:                 t1 = u1&   0xf0000; t2 = u2&   0xf0000; if ( t1&&t2 ) return 0;
                   1000:                 t1 = u1&    0xf000; t2 = u2&    0xf000; if ( t1&&t2 ) return 0;
                   1001:                 t1 = u1&     0xf00; t2 = u2&     0xf00; if ( t1&&t2 ) return 0;
                   1002:                 t1 = u1&      0xf0; t2 = u2&      0xf0; if ( t1&&t2 ) return 0;
                   1003:                 t1 = u1&       0xf; t2 = u2&       0xf; if ( t1&&t2 ) return 0;
                   1004:             }
                   1005:             return 1;
                   1006:             break;
                   1007:         case 6:
                   1008:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1009:                 u1 = d1[i]; u2 = d2[i];
                   1010:                 t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
                   1011:                 t1 = u1&  0xfc0000; t2 = u2&  0xfc0000; if ( t1&&t2 ) return 0;
                   1012:                 t1 = u1&   0x3f000; t2 = u2&   0x3f000; if ( t1&&t2 ) return 0;
                   1013:                 t1 = u1&     0xfc0; t2 = u2&     0xfc0; if ( t1&&t2 ) return 0;
                   1014:                 t1 = u1&      0x3f; t2 = u2&      0x3f; if ( t1&&t2 ) return 0;
                   1015:             }
                   1016:             return 1;
                   1017:             break;
                   1018:         case 8:
                   1019:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1020:                 u1 = d1[i]; u2 = d2[i];
                   1021:                 t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
                   1022:                 t1 = u1&  0xff0000; t2 = u2&  0xff0000; if ( t1&&t2 ) return 0;
                   1023:                 t1 = u1&    0xff00; t2 = u2&    0xff00; if ( t1&&t2 ) return 0;
                   1024:                 t1 = u1&      0xff; t2 = u2&      0xff; if ( t1&&t2 ) return 0;
                   1025:             }
                   1026:             return 1;
                   1027:             break;
                   1028:         case 16:
                   1029:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1030:                 u1 = d1[i]; u2 = d2[i];
                   1031:                 t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
                   1032:                 t1 = u1&    0xffff; t2 = u2&    0xffff; if ( t1&&t2 ) return 0;
                   1033:             }
                   1034:             return 1;
                   1035:             break;
                   1036:         case 32:
                   1037:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                   1038:                 if ( d1[i] && d2[i] ) return 0;
                   1039:             return 1;
                   1040:             break;
                   1041:         default:
                   1042:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1043:                 u1 = d1[i]; u2 = d2[i];
                   1044:                 for ( j = 0; j < nd_epw; j++ ) {
                   1045:                     if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                   1046:                     u1 >>= nd_bpe; u2 >>= nd_bpe;
                   1047:                 }
                   1048:             }
                   1049:             return 1;
                   1050:             break;
                   1051:     }
                   1052: #else
                   1053:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1054:         u1 = d1[i]; u2 = d2[i];
                   1055:         for ( j = 0; j < nd_epw; j++ ) {
                   1056:             if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                   1057:             u1 >>= nd_bpe; u2 >>= nd_bpe;
                   1058:         }
                   1059:     }
                   1060:     return 1;
                   1061: #endif
                   1062: }
                   1063:
                   1064: int ndl_check_bound(UINT *d1,UINT *d2)
                   1065: {
                   1066:     UINT u2;
                   1067:     int i,j,ind,k;
                   1068:
                   1069:     ind = 0;
                   1070: #if USE_UNROLL
                   1071:     switch ( nd_bpe ) {
                   1072:         case 3:
                   1073:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1074:                 u2 = d2[i];
                   1075:                 if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
                   1076:                 if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
                   1077:                 if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
                   1078:                 if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
                   1079:                 if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
                   1080:                 if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
                   1081:                 if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
                   1082:                 if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
                   1083:                 if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
                   1084:                 if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
                   1085:             }
                   1086:             return 0;
                   1087:             break;
                   1088:         case 4:
                   1089:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1090:                 u2 = d2[i];
                   1091:                 if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
                   1092:                 if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
                   1093:                 if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
                   1094:                 if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
                   1095:                 if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
                   1096:                 if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
                   1097:                 if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
                   1098:                 if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
                   1099:             }
                   1100:             return 0;
                   1101:             break;
                   1102:         case 6:
                   1103:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1104:                 u2 = d2[i];
                   1105:                 if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
                   1106:                 if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
                   1107:                 if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
                   1108:                 if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
                   1109:                 if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
                   1110:             }
                   1111:             return 0;
                   1112:             break;
                   1113:         case 8:
                   1114:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1115:                 u2 = d2[i];
                   1116:                 if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
                   1117:                 if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
                   1118:                 if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
                   1119:                 if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
                   1120:             }
                   1121:             return 0;
                   1122:             break;
                   1123:         case 16:
                   1124:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1125:                 u2 = d2[i];
                   1126:                 if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
                   1127:                 if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
                   1128:             }
                   1129:             return 0;
                   1130:             break;
                   1131:         case 32:
                   1132:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                   1133:                 if ( d1[i]+d2[i]<d1[i] ) return 1;
                   1134:             return 0;
                   1135:             break;
                   1136:         default:
                   1137:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1138:                 u2 = d2[i];
                   1139:                 k = (nd_epw-1)*nd_bpe;
                   1140:                 for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1141:                     if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1142:             }
                   1143:             return 0;
                   1144:             break;
                   1145:     }
                   1146: #else
                   1147:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1148:         u2 = d2[i];
                   1149:         k = (nd_epw-1)*nd_bpe;
                   1150:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1151:             if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1152:     }
                   1153:     return 0;
                   1154: #endif
                   1155: }
                   1156:
                   1157: int ndl_check_bound2(int index,UINT *d2)
                   1158: {
                   1159:     return ndl_check_bound(nd_bound[index],d2);
                   1160: }
                   1161:
                   1162: INLINE int ndl_hash_value(UINT *d)
                   1163: {
                   1164:     int i;
1.11      noro     1165:     UINT r;
1.1       noro     1166:
                   1167:     r = 0;
                   1168:     for ( i = 0; i < nd_wpd; i++ )
1.12      noro     1169:         r = (r*1511+d[i]);
1.11      noro     1170:     r %= REDTAB_LEN;
1.1       noro     1171:     return r;
                   1172: }
                   1173:
                   1174: INLINE int ndl_find_reducer(UINT *dg)
                   1175: {
                   1176:     RHist r;
                   1177:     int d,k,i;
                   1178:
                   1179:     d = ndl_hash_value(dg);
                   1180:     for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   1181:         if ( ndl_equal(dg,DL(r)) ) {
                   1182:             if ( k > 0 ) nd_notfirst++;
                   1183:             nd_found++;
                   1184:             return r->index;
                   1185:         }
                   1186:     }
                   1187:     if ( Reverse )
                   1188:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   1189:             r = nd_psh[i];
                   1190:             if ( ndl_reducible(dg,DL(r)) ) {
                   1191:                 nd_create++;
                   1192:                 nd_append_red(dg,i);
                   1193:                 return i;
                   1194:             }
                   1195:         }
                   1196:     else
                   1197:         for ( i = 0; i < nd_psn; i++ ) {
                   1198:             r = nd_psh[i];
                   1199:             if ( ndl_reducible(dg,DL(r)) ) {
                   1200:                 nd_create++;
                   1201:                 nd_append_red(dg,i);
                   1202:                 return i;
                   1203:             }
                   1204:         }
                   1205:     return -1;
                   1206: }
                   1207:
1.24      noro     1208: // ret=0,...,nd_psn-1 => reducer found
                   1209: // ret=nd_psn => reducer not found
                   1210: // ret=-1 => singular top reducible
                   1211:
                   1212: int comp_sig(SIG s1,SIG s2);
                   1213: void _ndltodl(UINT *ndl,DL dl);
                   1214:
                   1215: void print_sig(SIG s)
                   1216: {
                   1217:   int i;
                   1218:
                   1219:   fprintf(asir_out,"<<");
                   1220:   for ( i = 0; i < nd_nvar; i++ ) {
                   1221:     fprintf(asir_out,"%d",s->dl->d[i]);
                   1222:     if ( i != nd_nvar-1 ) fprintf(asir_out,",");
                   1223:   }
                   1224:   fprintf(asir_out,">>*e%d",s->pos);
                   1225: }
                   1226:
1.35      noro     1227: // assuming increasing order wrt signature
                   1228:
1.24      noro     1229: INLINE int ndl_find_reducer_s(UINT *dg,SIG sig)
                   1230: {
                   1231:   RHist r;
1.27      noro     1232:   int i,singular,ret,d,k;
1.26      noro     1233:   static int wpd,nvar;
1.24      noro     1234:   static SIG quo;
                   1235:   static UINT *tmp;
                   1236:
1.26      noro     1237:   if ( !quo || nvar != nd_nvar ) NEWSIG(quo);
                   1238:   if ( wpd != nd_wpd ) {
1.24      noro     1239:     wpd = nd_wpd;
                   1240:     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
                   1241:   }
1.27      noro     1242:   d = ndl_hash_value(dg);
1.35      noro     1243: #if 1
1.27      noro     1244:   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   1245:     if ( ndl_equal(dg,DL(r)) ) {
                   1246:       return r->index;
                   1247:     }
                   1248:   }
1.35      noro     1249: #endif
1.24      noro     1250:   singular = 0;
                   1251:   for ( i = 0; i < nd_psn; i++ ) {
                   1252:     r = nd_psh[i];
                   1253:     if ( ndl_reducible(dg,DL(r)) ) {
1.27      noro     1254:       ndl_sub(dg,DL(r),tmp);
1.24      noro     1255:       _ndltodl(tmp,DL(quo));
                   1256:       _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
                   1257:       quo->pos = nd_psh[i]->sig->pos;
                   1258:       ret = comp_sig(sig,quo);
                   1259:       if ( ret > 0 ) { singular = 0; break; }
1.38    ! noro     1260:       if ( ret == 0 ) { /* fprintf(asir_out,"s"); fflush(asir_out); */ singular = 1; }
1.24      noro     1261:     }
                   1262:   }
                   1263:   if ( singular ) return -1;
1.27      noro     1264:   else if ( i < nd_psn )
                   1265:     nd_append_red(dg,i);
                   1266:   return i;
1.24      noro     1267: }
                   1268:
1.1       noro     1269: ND nd_merge(ND p1,ND p2)
                   1270: {
                   1271:     int n,c;
                   1272:     int t,can,td1,td2;
                   1273:     ND r;
                   1274:     NM m1,m2,mr0,mr,s;
                   1275:
                   1276:     if ( !p1 ) return p2;
                   1277:     else if ( !p2 ) return p1;
                   1278:     else {
                   1279:         can = 0;
                   1280:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1281:             c = DL_COMPARE(DL(m1),DL(m2));
                   1282:             switch ( c ) {
                   1283:                 case 0:
                   1284:                     s = m1; m1 = NEXT(m1);
                   1285:                     can++; NEXTNM2(mr0,mr,s);
                   1286:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1287:                     break;
                   1288:                 case 1:
                   1289:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1290:                     break;
                   1291:                 case -1:
                   1292:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1293:                     break;
                   1294:             }
                   1295:         }
                   1296:         if ( !mr0 )
                   1297:             if ( m1 ) mr0 = m1;
                   1298:             else if ( m2 ) mr0 = m2;
                   1299:             else return 0;
                   1300:         else if ( m1 ) NEXT(mr) = m1;
                   1301:         else if ( m2 ) NEXT(mr) = m2;
                   1302:         else NEXT(mr) = 0;
                   1303:         BDY(p1) = mr0;
                   1304:         SG(p1) = MAX(SG(p1),SG(p2));
                   1305:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1306:         FREEND(p2);
                   1307:         return p1;
                   1308:     }
                   1309: }
                   1310:
                   1311: ND nd_add(int mod,ND p1,ND p2)
                   1312: {
                   1313:     int n,c;
                   1314:     int t,can,td1,td2;
                   1315:     ND r;
                   1316:     NM m1,m2,mr0,mr,s;
                   1317:
1.11      noro     1318:     Nnd_add++;
1.1       noro     1319:     if ( !p1 ) return p2;
                   1320:     else if ( !p2 ) return p1;
                   1321:     else if ( mod == -1 ) return nd_add_sf(p1,p2);
                   1322:     else if ( mod == -2 ) return nd_add_lf(p1,p2);
                   1323:     else if ( !mod ) return nd_add_q(p1,p2);
                   1324:     else {
                   1325:         can = 0;
                   1326:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1327:             c = DL_COMPARE(DL(m1),DL(m2));
                   1328:             switch ( c ) {
                   1329:                 case 0:
                   1330:                     t = ((CM(m1))+(CM(m2))) - mod;
                   1331:                     if ( t < 0 ) t += mod;
                   1332:                     s = m1; m1 = NEXT(m1);
                   1333:                     if ( t ) {
                   1334:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1335:                     } else {
                   1336:                         can += 2; FREENM(s);
                   1337:                     }
                   1338:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1339:                     break;
                   1340:                 case 1:
                   1341:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1342:                     break;
                   1343:                 case -1:
                   1344:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1345:                     break;
                   1346:             }
                   1347:         }
                   1348:         if ( !mr0 )
                   1349:             if ( m1 ) mr0 = m1;
                   1350:             else if ( m2 ) mr0 = m2;
                   1351:             else return 0;
                   1352:         else if ( m1 ) NEXT(mr) = m1;
                   1353:         else if ( m2 ) NEXT(mr) = m2;
                   1354:         else NEXT(mr) = 0;
                   1355:         BDY(p1) = mr0;
                   1356:         SG(p1) = MAX(SG(p1),SG(p2));
                   1357:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1358:         FREEND(p2);
                   1359:         return p1;
                   1360:     }
                   1361: }
                   1362:
                   1363: /* XXX on opteron, the inlined manipulation of destructive additon of
                   1364:  * two NM seems to make gcc optimizer get confused, so the part is
                   1365:  * done in a function.
                   1366:  */
                   1367:
                   1368: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
                   1369: {
                   1370:     NM s;
                   1371:     P t;
                   1372:     int can;
                   1373:
                   1374:     addp(nd_vc,CP(*m1),CP(*m2),&t);
                   1375:     s = *m1; *m1 = NEXT(*m1);
                   1376:     if ( t ) {
                   1377:         can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
                   1378:     } else {
                   1379:         can = 2; FREENM(s);
                   1380:     }
                   1381:     s = *m2; *m2 = NEXT(*m2); FREENM(s);
                   1382:     return can;
                   1383: }
                   1384:
                   1385: ND nd_add_q(ND p1,ND p2)
                   1386: {
                   1387:     int n,c,can;
                   1388:     ND r;
                   1389:     NM m1,m2,mr0,mr,s;
                   1390:     P t;
                   1391:
                   1392:     if ( !p1 ) return p2;
                   1393:     else if ( !p2 ) return p1;
                   1394:     else {
                   1395:         can = 0;
                   1396:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1397:             c = DL_COMPARE(DL(m1),DL(m2));
                   1398:             switch ( c ) {
                   1399:                 case 0:
                   1400: #if defined(__x86_64__)
                   1401:                     can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
                   1402: #else
                   1403:                     addp(nd_vc,CP(m1),CP(m2),&t);
                   1404:                     s = m1; m1 = NEXT(m1);
                   1405:                     if ( t ) {
                   1406:                         can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
                   1407:                     } else {
                   1408:                         can += 2; FREENM(s);
                   1409:                     }
                   1410:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1411: #endif
                   1412:                     break;
                   1413:                 case 1:
                   1414:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1415:                     break;
                   1416:                 case -1:
                   1417:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1418:                     break;
                   1419:             }
                   1420:         }
                   1421:         if ( !mr0 )
                   1422:             if ( m1 ) mr0 = m1;
                   1423:             else if ( m2 ) mr0 = m2;
                   1424:             else return 0;
                   1425:         else if ( m1 ) NEXT(mr) = m1;
                   1426:         else if ( m2 ) NEXT(mr) = m2;
                   1427:         else NEXT(mr) = 0;
                   1428:         BDY(p1) = mr0;
                   1429:         SG(p1) = MAX(SG(p1),SG(p2));
                   1430:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1431:         FREEND(p2);
                   1432:         return p1;
                   1433:     }
                   1434: }
                   1435:
                   1436: ND nd_add_sf(ND p1,ND p2)
                   1437: {
                   1438:     int n,c,can;
                   1439:     ND r;
                   1440:     NM m1,m2,mr0,mr,s;
                   1441:     int t;
                   1442:
                   1443:     if ( !p1 ) return p2;
                   1444:     else if ( !p2 ) return p1;
                   1445:     else {
                   1446:         can = 0;
                   1447:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1448:             c = DL_COMPARE(DL(m1),DL(m2));
                   1449:             switch ( c ) {
                   1450:                 case 0:
                   1451:                     t = _addsf(CM(m1),CM(m2));
                   1452:                     s = m1; m1 = NEXT(m1);
                   1453:                     if ( t ) {
                   1454:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1455:                     } else {
                   1456:                         can += 2; FREENM(s);
                   1457:                     }
                   1458:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1459:                     break;
                   1460:                 case 1:
                   1461:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1462:                     break;
                   1463:                 case -1:
                   1464:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1465:                     break;
                   1466:             }
                   1467:         }
                   1468:         if ( !mr0 )
                   1469:             if ( m1 ) mr0 = m1;
                   1470:             else if ( m2 ) mr0 = m2;
                   1471:             else return 0;
                   1472:         else if ( m1 ) NEXT(mr) = m1;
                   1473:         else if ( m2 ) NEXT(mr) = m2;
                   1474:         else NEXT(mr) = 0;
                   1475:         BDY(p1) = mr0;
                   1476:         SG(p1) = MAX(SG(p1),SG(p2));
                   1477:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1478:         FREEND(p2);
                   1479:         return p1;
                   1480:     }
                   1481: }
                   1482:
                   1483:
                   1484: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
                   1485: {
                   1486:     int c,c1,c2;
                   1487:     Z cg,cred,gcd,tq;
                   1488:     P cgp,credp,gcdp;
                   1489:     Obj tr,tr1;
                   1490:
                   1491:     if ( mod == -1 ) {
                   1492:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1493:         *divp = (Obj)ONE;
                   1494:     } else if ( mod == -2 ) {
                   1495:         Z inv,t;
                   1496:         divlf(ONE,HCZ(p),&inv);
                   1497:         chsgnlf(HCZ(g),&t);
                   1498:         mullf(inv,t,&CZ(mul));
                   1499:         *divp = (Obj)ONE;
                   1500:     } else if ( mod ) {
                   1501:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1502:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1503:         *divp = (Obj)ONE;
                   1504:     } else if ( nd_vc ) {
                   1505:         ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
                   1506:         divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
                   1507:         chsgnp(cgp,&CP(mul));
                   1508:         nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
                   1509:         if ( dn ) {
                   1510:             mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
                   1511:             reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
                   1512:         }
                   1513:         *divp = (Obj)credp;
                   1514:     } else {
1.6       noro     1515:         igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1516:         chsgnz(cg,&CZ(mul));
1.1       noro     1517:         nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
                   1518:         if ( dn ) {
                   1519:             mulz(dn->z,cred,&tq); dn->z = tq;
                   1520:         }
                   1521:         *divp = (Obj)cred;
                   1522:     }
                   1523:     return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
                   1524: }
                   1525:
                   1526: /* ret=1 : success, ret=0 : overflow */
1.6       noro     1527: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
1.1       noro     1528: {
                   1529:     NM m,mrd,tail;
                   1530:     NM mul;
                   1531:     int n,sugar,psugar,sugar0,stat,index;
                   1532:     int c,c1,c2,dummy;
                   1533:     RHist h;
                   1534:     NDV p,red;
                   1535:     Q cg,cred,gcd,tq,qq;
                   1536:     Z iq;
                   1537:     DP dmul;
                   1538:     NODE node;
                   1539:     LIST hist;
                   1540:     double hmag;
                   1541:     P tp,tp1;
                   1542:     Obj tr,tr1,div;
                   1543:     union oNDC hg;
                   1544:     P cont;
                   1545:
                   1546:     if ( !g ) {
                   1547:         *rp = d;
                   1548:         return 1;
                   1549:     }
                   1550:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1551:
                   1552:     sugar0 = sugar = SG(g);
                   1553:     n = NV(g);
                   1554:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1555:     if ( d )
                   1556:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1557:     for ( ; g; ) {
                   1558:         index = ndl_find_reducer(HDL(g));
                   1559:         if ( index >= 0 ) {
                   1560:             h = nd_psh[index];
                   1561:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1562:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1563:                 nd_free(g); nd_free(d);
                   1564:                 return 0;
                   1565:             }
                   1566:             p = nd_demand ? ndv_load(index) : ps[index];
                   1567:             /* d+g -> div*(d+g)+mul*p */
1.6       noro     1568:             g = nd_reduce2(mod,d,g,p,mul,0,&div);
1.1       noro     1569:             if ( nd_gentrace ) {
                   1570:                 /* Trace=[div,index,mul,ONE] */
1.6       noro     1571:                 STOZ(index,iq);
1.1       noro     1572:                 nmtodp(mod,mul,&dmul);
                   1573:                 node = mknode(4,div,iq,dmul,ONE);
                   1574:             }
                   1575:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
                   1576:             if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
                   1577:                 hg = HCU(g);
                   1578:                 nd_removecont2(d,g);
1.6       noro     1579:                 if ( nd_gentrace ) {
1.1       noro     1580:                     /* overwrite cont : Trace=[div,index,mul,cont] */
1.6       noro     1581:                     /* exact division */
1.1       noro     1582:                     cont = ndc_div(mod,hg,HCU(g));
                   1583:                     if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
                   1584:                 }
                   1585:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1586:             }
                   1587:             MKLIST(hist,node);
                   1588:             MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
                   1589:         } else if ( !full ) {
                   1590:             *rp = g;
                   1591:             return 1;
                   1592:         } else {
                   1593:             m = BDY(g);
                   1594:             if ( NEXT(m) ) {
                   1595:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1596:             } else {
                   1597:                 FREEND(g); g = 0;
                   1598:             }
                   1599:             if ( d ) {
                   1600:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1601:             } else {
                   1602:                 MKND(n,m,1,d); tail = BDY(d);
                   1603:             }
                   1604:         }
                   1605:     }
                   1606:     if ( d ) SG(d) = sugar;
                   1607:     *rp = d;
                   1608:     return 1;
                   1609: }
                   1610:
1.24      noro     1611: // ret=1 => success
                   1612: // ret=0 => overflow
                   1613: // ret=-1 => singular top reducible
                   1614:
                   1615: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *rp)
                   1616: {
                   1617:     NM m,mrd,tail;
                   1618:     NM mul;
                   1619:     int n,sugar,psugar,sugar0,stat,index;
                   1620:     int c,c1,c2,dummy;
                   1621:     RHist h;
                   1622:     NDV p,red;
                   1623:     Q cg,cred,gcd,tq,qq;
                   1624:     Z iq;
                   1625:     DP dmul;
                   1626:     NODE node;
                   1627:     LIST hist;
                   1628:     double hmag;
                   1629:     P tp,tp1;
                   1630:     Obj tr,tr1,div;
                   1631:     union oNDC hg;
                   1632:     P cont;
                   1633:     SIG sig;
                   1634:
                   1635:     if ( !g ) {
                   1636:         *rp = d;
                   1637:         return 1;
                   1638:     }
                   1639:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1640:
                   1641:     sugar0 = sugar = SG(g);
                   1642:     n = NV(g);
                   1643:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1644:     if ( d )
                   1645:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1646:     sig = g->sig;
                   1647:     for ( ; g; ) {
                   1648:         index = ndl_find_reducer_s(HDL(g),sig);
                   1649:         if ( index >= 0 && index < nd_psn ) {
                   1650:             // reducer found
                   1651:             h = nd_psh[index];
                   1652:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1653:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1654:                 nd_free(g); nd_free(d);
                   1655:                 return 0;
                   1656:             }
                   1657:             p = ps[index];
                   1658:             /* d+g -> div*(d+g)+mul*p */
                   1659:             g = nd_reduce2(mod,d,g,p,mul,0,&div);
                   1660:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
                   1661:             if ( !mod && g && ((double)(p_mag(HCP(g))) > hmag) ) {
                   1662:                 hg = HCU(g);
                   1663:                 nd_removecont2(d,g);
                   1664:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1665:             }
                   1666:         } else if ( index == -1 ) {
                   1667:           // singular top reducible
                   1668:           return -1;
                   1669:         } else if ( !full ) {
                   1670:             *rp = g;
                   1671:             g->sig = sig;
                   1672:             return 1;
                   1673:         } else {
                   1674:             m = BDY(g);
                   1675:             if ( NEXT(m) ) {
                   1676:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1677:             } else {
                   1678:                 FREEND(g); g = 0;
                   1679:             }
                   1680:             if ( d ) {
                   1681:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1682:             } else {
                   1683:                 MKND(n,m,1,d); tail = BDY(d);
                   1684:             }
                   1685:         }
                   1686:     }
                   1687:     if ( d ) {
                   1688:       SG(d) = sugar;
                   1689:       d->sig = sig;
                   1690:     }
                   1691:     *rp = d;
                   1692:     return 1;
                   1693: }
                   1694:
1.1       noro     1695: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
                   1696: {
                   1697:     int hindex,index;
                   1698:     NDV p;
                   1699:     ND u,d,red;
                   1700:     NODE l;
                   1701:     NM mul,m,mrd,tail;
                   1702:     int sugar,psugar,n,h_reducible;
                   1703:     PGeoBucket bucket;
                   1704:     int c,c1,c2;
                   1705:     Z cg,cred,gcd,zzz;
                   1706:     RHist h;
                   1707:     double hmag,gmag;
                   1708:     int count = 0;
                   1709:     int hcount = 0;
                   1710:
                   1711:     if ( !g ) {
                   1712:         *rp = 0;
                   1713:         return 1;
                   1714:     }
                   1715:     sugar = SG(g);
                   1716:     n = NV(g);
1.6       noro     1717:     if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1       noro     1718:     bucket = create_pbucket();
                   1719:     add_pbucket(mod,bucket,g);
                   1720:     d = 0;
                   1721:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1722:     while ( 1 ) {
                   1723:         if ( mod > 0 || mod == -1 )
                   1724:           hindex = head_pbucket(mod,bucket);
                   1725:         else if ( mod == -2 )
                   1726:           hindex = head_pbucket_lf(bucket);
                   1727:         else
                   1728:           hindex = head_pbucket_q(bucket);
                   1729:         if ( hindex < 0 ) {
                   1730:             if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1731:             if ( d ) SG(d) = sugar;
                   1732:             *rp = d;
                   1733:             return 1;
                   1734:         }
                   1735:         g = bucket->body[hindex];
                   1736:         index = ndl_find_reducer(HDL(g));
                   1737:         if ( index >= 0 ) {
                   1738:             count++;
                   1739:             if ( !d ) hcount++;
                   1740:             h = nd_psh[index];
                   1741:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1742:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1743:                 nd_free(d);
                   1744:                 free_pbucket(bucket);
                   1745:                 *rp = 0;
                   1746:                 return 0;
                   1747:             }
                   1748:             p = ps[index];
                   1749:             if ( mod == -1 )
                   1750:                 CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1751:             else if ( mod == -2 ) {
                   1752:                 Z inv,t;
                   1753:                 divlf(ONE,HCZ(p),&inv);
                   1754:                 chsgnlf(HCZ(g),&t);
                   1755:                 mullf(inv,t,&CZ(mul));
                   1756:             } else if ( mod ) {
                   1757:                 c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1758:                 DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1759:             } else {
1.6       noro     1760:                 igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1761:                 chsgnz(cg,&CZ(mul));
1.1       noro     1762:                 nd_mul_c_q(d,(P)cred);
                   1763:                 mulq_pbucket(bucket,cred);
                   1764:                 g = bucket->body[hindex];
1.6       noro     1765:                 gmag = (double)p_mag((P)HCZ(g));
1.1       noro     1766:             }
                   1767:             red = ndv_mul_nm(mod,mul,p);
                   1768:             bucket->body[hindex] = nd_remove_head(g);
                   1769:             red = nd_remove_head(red);
                   1770:             add_pbucket(mod,bucket,red);
                   1771:             psugar = SG(p)+TD(DL(mul));
                   1772:             sugar = MAX(sugar,psugar);
                   1773:             if ( !mod && hmag && (gmag > hmag) ) {
                   1774:                 g = normalize_pbucket(mod,bucket);
                   1775:                 if ( !g ) {
                   1776:                     if ( d ) SG(d) = sugar;
                   1777:                     *rp = d;
                   1778:                     return 1;
                   1779:                 }
                   1780:                 nd_removecont2(d,g);
1.6       noro     1781:                 hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
1.1       noro     1782:                 add_pbucket(mod,bucket,g);
                   1783:             }
                   1784:         } else if ( !full ) {
                   1785:             g = normalize_pbucket(mod,bucket);
                   1786:             if ( g ) SG(g) = sugar;
                   1787:             *rp = g;
                   1788:             return 1;
                   1789:         } else {
                   1790:             m = BDY(g);
                   1791:             if ( NEXT(m) ) {
                   1792:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1793:             } else {
                   1794:                 FREEND(g); g = 0;
                   1795:             }
                   1796:             bucket->body[hindex] = g;
                   1797:             NEXT(m) = 0;
                   1798:             if ( d ) {
                   1799:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1800:             } else {
                   1801:                 MKND(n,m,1,d); tail = BDY(d);
                   1802:             }
                   1803:         }
                   1804:     }
                   1805: }
                   1806:
1.25      noro     1807: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *rp)
                   1808: {
                   1809:   int hindex,index;
                   1810:   NDV p;
                   1811:   ND u,d,red;
                   1812:   NODE l;
                   1813:   NM mul,m,mrd,tail;
                   1814:   int sugar,psugar,n,h_reducible;
                   1815:   PGeoBucket bucket;
                   1816:   int c,c1,c2;
                   1817:   Z cg,cred,gcd,zzz;
                   1818:   RHist h;
                   1819:   double hmag,gmag;
                   1820:   int count = 0;
                   1821:   int hcount = 0;
                   1822:   SIG sig;
                   1823:
                   1824:   if ( !g ) {
                   1825:     *rp = 0;
                   1826:     return 1;
                   1827:   }
                   1828:   sugar = SG(g);
                   1829:   n = NV(g);
                   1830:   if ( !mod ) hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
                   1831:   bucket = create_pbucket();
                   1832:   add_pbucket(mod,bucket,g);
                   1833:   d = 0;
                   1834:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1835:   sig = g->sig;
                   1836:   while ( 1 ) {
                   1837:     if ( mod > 0 || mod == -1 )
                   1838:       hindex = head_pbucket(mod,bucket);
                   1839:     else if ( mod == -2 )
                   1840:       hindex = head_pbucket_lf(bucket);
                   1841:     else
                   1842:       hindex = head_pbucket_q(bucket);
                   1843:     if ( hindex < 0 ) {
                   1844:       if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1845:       if ( d ) {
                   1846:         SG(d) = sugar;
                   1847:         d->sig = sig;
                   1848:       }
                   1849:       *rp = d;
                   1850:       return 1;
                   1851:     }
                   1852:     g = bucket->body[hindex];
                   1853:     index = ndl_find_reducer_s(HDL(g),sig);
                   1854:     if ( index >= 0 && index < nd_psn ) {
                   1855:       count++;
                   1856:       if ( !d ) hcount++;
                   1857:       h = nd_psh[index];
                   1858:       ndl_sub(HDL(g),DL(h),DL(mul));
                   1859:       if ( ndl_check_bound2(index,DL(mul)) ) {
                   1860:         nd_free(d);
                   1861:         free_pbucket(bucket);
                   1862:         *rp = 0;
                   1863:         return 0;
                   1864:       }
                   1865:       p = ps[index];
                   1866:       if ( mod == -1 )
                   1867:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1868:       else if ( mod == -2 ) {
                   1869:         Z inv,t;
                   1870:         divlf(ONE,HCZ(p),&inv);
                   1871:         chsgnlf(HCZ(g),&t);
                   1872:         mullf(inv,t,&CZ(mul));
                   1873:       } else if ( mod ) {
                   1874:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1875:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1876:       } else {
                   1877:         igcd_cofactor(HCZ(g),HCZ(p),&gcd,&cg,&cred);
                   1878:         chsgnz(cg,&CZ(mul));
                   1879:         nd_mul_c_q(d,(P)cred);
                   1880:         mulq_pbucket(bucket,cred);
                   1881:         g = bucket->body[hindex];
                   1882:         gmag = (double)p_mag((P)HCZ(g));
                   1883:       }
                   1884:       red = ndv_mul_nm(mod,mul,p);
                   1885:       bucket->body[hindex] = nd_remove_head(g);
                   1886:       red = nd_remove_head(red);
                   1887:       add_pbucket(mod,bucket,red);
                   1888:       psugar = SG(p)+TD(DL(mul));
                   1889:       sugar = MAX(sugar,psugar);
                   1890:       if ( !mod && hmag && (gmag > hmag) ) {
                   1891:          g = normalize_pbucket(mod,bucket);
                   1892:          if ( !g ) {
                   1893:            if ( d ) {
                   1894:              SG(d) = sugar;
                   1895:              d->sig = sig;
                   1896:            }
                   1897:            *rp = d;
                   1898:            return 1;
                   1899:          }
                   1900:          nd_removecont2(d,g);
                   1901:          hmag = ((double)p_mag((P)HCZ(g)))*nd_scale;
                   1902:          add_pbucket(mod,bucket,g);
                   1903:       }
                   1904:     } else if ( index == -1 ) {
                   1905:       // singular top reducible
                   1906:       return -1;
                   1907:     } else if ( !full ) {
                   1908:       g = normalize_pbucket(mod,bucket);
                   1909:       if ( g ) {
                   1910:         SG(g) = sugar;
                   1911:         g->sig = sig;
                   1912:       }
                   1913:       *rp = g;
                   1914:       return 1;
                   1915:     } else {
                   1916:       m = BDY(g);
                   1917:       if ( NEXT(m) ) {
                   1918:         BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1919:       } else {
                   1920:         FREEND(g); g = 0;
                   1921:       }
                   1922:       bucket->body[hindex] = g;
                   1923:       NEXT(m) = 0;
                   1924:       if ( d ) {
                   1925:         NEXT(tail)=m; tail=m; LEN(d)++;
                   1926:       } else {
                   1927:         MKND(n,m,1,d); tail = BDY(d);
                   1928:       }
                   1929:     }
                   1930:   }
                   1931: }
                   1932:
1.1       noro     1933: /* input : list of NDV, cand : list of NDV */
                   1934:
                   1935: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
                   1936: {
                   1937:     int n,i,stat;
                   1938:     ND nf,d;
                   1939:     NDV r;
                   1940:     NODE t,s;
                   1941:     union oNDC dn;
                   1942:     Z q;
                   1943:     LIST list;
                   1944:
1.24      noro     1945:     ndv_setup(m,0,cand,nd_gentrace?1:0,1,0);
1.1       noro     1946:     n = length(cand);
                   1947:
                   1948:   if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
                   1949:     /* membercheck : list is a subset of Id(cand) ? */
                   1950:     for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
                   1951: again:
                   1952:     nd_tracelist = 0;
                   1953:         if ( nd_bpe > obpe )
                   1954:             r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
                   1955:         else
                   1956:             r = (NDV)BDY(t);
                   1957: #if 0
                   1958:         // moved to nd_f4_lf_trace()
                   1959:         if ( m == -2 ) ndv_mod(m,r);
                   1960: #endif
                   1961:         d = ndvtond(m,r);
1.6       noro     1962:         stat = nd_nf(m,0,d,nd_ps,0,&nf);
1.1       noro     1963:         if ( !stat ) {
                   1964:             nd_reconstruct(0,0);
                   1965:             goto again;
                   1966:         } else if ( nf ) return 0;
                   1967:     if ( nd_gentrace ) {
                   1968:       nd_tracelist = reverse_node(nd_tracelist);
                   1969:       MKLIST(list,nd_tracelist);
1.6       noro     1970:       STOZ(i,q); s = mknode(2,q,list); MKLIST(list,s);
1.1       noro     1971:       MKNODE(s,list,nd_alltracelist);
                   1972:       nd_alltracelist = s; nd_tracelist = 0;
                   1973:     }
                   1974:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   1975:     }
                   1976:     if ( DP_Print ) { printf("\n"); }
                   1977:     return 1;
                   1978: }
                   1979:
                   1980: ND nd_remove_head(ND p)
                   1981: {
                   1982:     NM m;
                   1983:
                   1984:     m = BDY(p);
                   1985:     if ( !NEXT(m) ) {
                   1986:         FREEND(p); p = 0;
                   1987:     } else {
                   1988:         BDY(p) = NEXT(m); LEN(p)--;
                   1989:     }
                   1990:     FREENM(m);
                   1991:     return p;
                   1992: }
                   1993:
                   1994: ND nd_separate_head(ND p,ND *head)
                   1995: {
                   1996:     NM m,m0;
                   1997:     ND r;
                   1998:
                   1999:     m = BDY(p);
                   2000:     if ( !NEXT(m) ) {
                   2001:         *head = p; p = 0;
                   2002:     } else {
                   2003:         m0 = m;
                   2004:         BDY(p) = NEXT(m); LEN(p)--;
                   2005:         NEXT(m0) = 0;
                   2006:         MKND(NV(p),m0,1,r);
                   2007:         *head = r;
                   2008:     }
                   2009:     return p;
                   2010: }
                   2011:
                   2012: PGeoBucket create_pbucket()
                   2013: {
                   2014:     PGeoBucket g;
                   2015:
                   2016:     g = CALLOC(1,sizeof(struct oPGeoBucket));
                   2017:     g->m = -1;
                   2018:     return g;
                   2019: }
                   2020:
                   2021: void free_pbucket(PGeoBucket b) {
                   2022:     int i;
                   2023:
                   2024:     for ( i = 0; i <= b->m; i++ )
                   2025:         if ( b->body[i] ) {
                   2026:             nd_free(b->body[i]);
                   2027:             b->body[i] = 0;
                   2028:         }
                   2029:     GCFREE(b);
                   2030: }
                   2031:
                   2032: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   2033: {
                   2034:     int l,i,k,m;
                   2035:
                   2036:     if ( !d )
                   2037:         return;
                   2038:     l = LEN(d);
                   2039:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2040:     /* 2^(k-1) < l <= 2^k (=m) */
                   2041:     d = nd_merge(g->body[k],d);
                   2042:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   2043:         g->body[k] = 0;
                   2044:         d = nd_merge(g->body[k+1],d);
                   2045:     }
                   2046:     g->body[k] = d;
                   2047:     g->m = MAX(g->m,k);
                   2048: }
                   2049:
                   2050: void add_pbucket(int mod,PGeoBucket g,ND d)
                   2051: {
                   2052:     int l,i,k,m;
                   2053:
                   2054:     if ( !d )
                   2055:         return;
                   2056:     l = LEN(d);
                   2057:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   2058:     /* 2^(k-1) < l <= 2^k (=m) */
                   2059:     d = nd_add(mod,g->body[k],d);
                   2060:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   2061:         g->body[k] = 0;
                   2062:         d = nd_add(mod,g->body[k+1],d);
                   2063:     }
                   2064:     g->body[k] = d;
                   2065:     g->m = MAX(g->m,k);
                   2066: }
                   2067:
                   2068: void mulq_pbucket(PGeoBucket g,Z c)
                   2069: {
                   2070:     int k;
                   2071:
                   2072:     for ( k = 0; k <= g->m; k++ )
                   2073:         nd_mul_c_q(g->body[k],(P)c);
                   2074: }
                   2075:
                   2076: NM remove_head_pbucket_symbolic(PGeoBucket g)
                   2077: {
                   2078:     int j,i,k,c;
                   2079:     NM head;
                   2080:
                   2081:     k = g->m;
                   2082:     j = -1;
                   2083:     for ( i = 0; i <= k; i++ ) {
                   2084:         if ( !g->body[i] ) continue;
                   2085:         if ( j < 0 ) j = i;
                   2086:         else {
                   2087:             c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
                   2088:             if ( c > 0 )
                   2089:                 j = i;
                   2090:             else if ( c == 0 )
                   2091:                 g->body[i] = nd_remove_head(g->body[i]);
                   2092:         }
                   2093:     }
                   2094:     if ( j < 0 ) return 0;
                   2095:     else {
                   2096:         head = BDY(g->body[j]);
                   2097:         if ( !NEXT(head) ) {
                   2098:             FREEND(g->body[j]);
                   2099:             g->body[j] = 0;
                   2100:         } else {
                   2101:             BDY(g->body[j]) = NEXT(head);
                   2102:             LEN(g->body[j])--;
                   2103:         }
                   2104:         return head;
                   2105:     }
                   2106: }
                   2107:
                   2108: int head_pbucket(int mod,PGeoBucket g)
                   2109: {
                   2110:     int j,i,c,k,nv,sum;
                   2111:     UINT *di,*dj;
                   2112:     ND gi,gj;
                   2113:
                   2114:     k = g->m;
                   2115:     while ( 1 ) {
                   2116:         j = -1;
                   2117:         for ( i = 0; i <= k; i++ ) {
                   2118:             if ( !(gi = g->body[i]) )
                   2119:                 continue;
                   2120:             if ( j < 0 ) {
                   2121:                 j = i;
                   2122:                 gj = g->body[j];
                   2123:                 dj = HDL(gj);
                   2124:                 sum = HCM(gj);
                   2125:             } else {
                   2126:                 c = DL_COMPARE(HDL(gi),dj);
                   2127:                 if ( c > 0 ) {
                   2128:                     if ( sum ) HCM(gj) = sum;
                   2129:                     else g->body[j] = nd_remove_head(gj);
                   2130:                     j = i;
                   2131:                     gj = g->body[j];
                   2132:                     dj = HDL(gj);
                   2133:                     sum = HCM(gj);
                   2134:                 } else if ( c == 0 ) {
                   2135:                     if ( mod == -1 )
                   2136:                         sum = _addsf(sum,HCM(gi));
                   2137:                     else {
                   2138:                         sum = sum+HCM(gi)-mod;
                   2139:                         if ( sum < 0 ) sum += mod;
                   2140:                     }
                   2141:                     g->body[i] = nd_remove_head(gi);
                   2142:                 }
                   2143:             }
                   2144:         }
                   2145:         if ( j < 0 ) return -1;
                   2146:         else if ( sum ) {
                   2147:             HCM(gj) = sum;
                   2148:             return j;
                   2149:         } else
                   2150:             g->body[j] = nd_remove_head(gj);
                   2151:     }
                   2152: }
                   2153:
                   2154: int head_pbucket_q(PGeoBucket g)
                   2155: {
                   2156:     int j,i,c,k,nv;
                   2157:     Z sum,t;
                   2158:     ND gi,gj;
                   2159:
                   2160:     k = g->m;
                   2161:     while ( 1 ) {
                   2162:         j = -1;
                   2163:         for ( i = 0; i <= k; i++ ) {
                   2164:             if ( !(gi = g->body[i]) ) continue;
                   2165:             if ( j < 0 ) {
                   2166:                 j = i;
                   2167:                 gj = g->body[j];
1.6       noro     2168:                 sum = HCZ(gj);
1.1       noro     2169:             } else {
                   2170:                 nv = NV(gi);
                   2171:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   2172:                 if ( c > 0 ) {
1.6       noro     2173:                     if ( sum ) HCZ(gj) = sum;
1.1       noro     2174:                     else g->body[j] = nd_remove_head(gj);
                   2175:                     j = i;
                   2176:                     gj = g->body[j];
1.6       noro     2177:                     sum = HCZ(gj);
1.1       noro     2178:                 } else if ( c == 0 ) {
1.6       noro     2179:                     addz(sum,HCZ(gi),&t);
1.1       noro     2180:                     sum = t;
                   2181:                     g->body[i] = nd_remove_head(gi);
                   2182:                 }
                   2183:             }
                   2184:         }
                   2185:         if ( j < 0 ) return -1;
                   2186:         else if ( sum ) {
1.6       noro     2187:             HCZ(gj) = sum;
1.1       noro     2188:             return j;
                   2189:         } else
                   2190:             g->body[j] = nd_remove_head(gj);
                   2191:     }
                   2192: }
                   2193:
                   2194: int head_pbucket_lf(PGeoBucket g)
                   2195: {
                   2196:     int j,i,c,k,nv;
                   2197:     Z sum,t;
                   2198:     ND gi,gj;
                   2199:
                   2200:     k = g->m;
                   2201:     while ( 1 ) {
                   2202:         j = -1;
                   2203:         for ( i = 0; i <= k; i++ ) {
                   2204:             if ( !(gi = g->body[i]) ) continue;
                   2205:             if ( j < 0 ) {
                   2206:                 j = i;
                   2207:                 gj = g->body[j];
                   2208:                 sum = HCZ(gj);
                   2209:             } else {
                   2210:                 nv = NV(gi);
                   2211:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   2212:                 if ( c > 0 ) {
                   2213:                     if ( sum ) HCZ(gj) = sum;
                   2214:                     else g->body[j] = nd_remove_head(gj);
                   2215:                     j = i;
                   2216:                     gj = g->body[j];
                   2217:                     sum = HCZ(gj);
                   2218:                 } else if ( c == 0 ) {
                   2219:                     addlf(sum,HCZ(gi),&t);
                   2220:                     sum = t;
                   2221:                     g->body[i] = nd_remove_head(gi);
                   2222:                 }
                   2223:             }
                   2224:         }
                   2225:         if ( j < 0 ) return -1;
                   2226:         else if ( sum ) {
                   2227:             HCZ(gj) = sum;
                   2228:             return j;
                   2229:         } else
                   2230:             g->body[j] = nd_remove_head(gj);
                   2231:     }
                   2232: }
                   2233:
                   2234: ND normalize_pbucket(int mod,PGeoBucket g)
                   2235: {
                   2236:     int i;
                   2237:     ND r,t;
                   2238:
                   2239:     r = 0;
                   2240:     for ( i = 0; i <= g->m; i++ ) {
                   2241:         r = nd_add(mod,r,g->body[i]);
                   2242:         g->body[i] = 0;
                   2243:     }
                   2244:     g->m = -1;
                   2245:     return r;
                   2246: }
                   2247:
                   2248: #if 0
                   2249: void register_hcf(NDV p)
                   2250: {
                   2251:     DCP dc,t;
                   2252:     P hc,h;
                   2253:     int c;
                   2254:     NODE l,l1,prev;
                   2255:
                   2256:     hc = p->body->c.p;
                   2257:     if ( !nd_vc || NUM(hc) ) return;
                   2258:     fctrp(nd_vc,hc,&dc);
                   2259:     for ( t = dc; t; t = NEXT(t) ) {
                   2260:         h = t->c;
                   2261:         if ( NUM(h) ) continue;
                   2262:         for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   2263:             c = compp(nd_vc,h,(P)BDY(l));
                   2264:             if ( c >= 0 ) break;
                   2265:         }
                   2266:         if ( !l || c > 0  ) {
                   2267:             MKNODE(l1,h,l);
                   2268:             if ( !prev )
                   2269:                 nd_hcf = l1;
                   2270:             else
                   2271:                 NEXT(prev) = l1;
                   2272:         }
                   2273:     }
                   2274: }
                   2275: #else
                   2276: void register_hcf(NDV p)
                   2277: {
                   2278:     DCP dc,t;
                   2279:     P hc,h,q;
                   2280:     Q dmy;
                   2281:     int c;
                   2282:     NODE l,l1,prev;
                   2283:
                   2284:     hc = p->body->c.p;
                   2285:     if ( NUM(hc) ) return;
                   2286:     ptozp(hc,1,&dmy,&h);
                   2287: #if 1
                   2288:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   2289:         while ( 1 ) {
                   2290:             if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
                   2291:             else break;
                   2292:         }
                   2293:     }
                   2294:     if ( NUM(h) ) return;
                   2295: #endif
                   2296:     for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   2297:         c = compp(nd_vc,h,(P)BDY(l));
                   2298:         if ( c >= 0 ) break;
                   2299:     }
                   2300:     if ( !l || c > 0  ) {
                   2301:         MKNODE(l1,h,l);
                   2302:         if ( !prev )
                   2303:             nd_hcf = l1;
                   2304:         else
                   2305:             NEXT(prev) = l1;
                   2306:     }
                   2307: }
                   2308: #endif
                   2309:
                   2310: int do_diagonalize(int sugar,int m)
                   2311: {
1.6       noro     2312:   int i,nh,stat;
                   2313:   NODE r,g,t;
                   2314:   ND h,nf,s,head;
                   2315:   NDV nfv;
                   2316:   Q q;
                   2317:   P nm,nmp,dn,mnp,dnp,cont,cont1;
                   2318:   union oNDC hc;
                   2319:   NODE node;
                   2320:   LIST l;
                   2321:   Z iq;
1.1       noro     2322:
1.6       noro     2323:   for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   2324:     if ( nd_gentrace ) {
                   2325:       /* Trace = [1,index,1,1] */
                   2326:       STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2327:       MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2328:     }
                   2329:     if ( nd_demand )
                   2330:       nfv = ndv_load(i);
                   2331:     else
                   2332:       nfv = nd_ps[i];
                   2333:     s = ndvtond(m,nfv);
                   2334:     s = nd_separate_head(s,&head);
                   2335:     stat = nd_nf(m,head,s,nd_ps,1,&nf);
                   2336:     if ( !stat ) return 0;
                   2337:     ndv_free(nfv);
                   2338:     hc = HCU(nf); nd_removecont(m,nf);
                   2339:     /* exact division */
                   2340:     cont = ndc_div(m,hc,HCU(nf));
1.1       noro     2341:     if ( nd_gentrace ) finalize_tracelist(i,cont);
1.6       noro     2342:     nfv = ndtondv(m,nf);
                   2343:     nd_free(nf);
                   2344:     nd_bound[i] = ndv_compute_bound(nfv);
                   2345:     if ( !m ) register_hcf(nfv);
                   2346:     if ( nd_demand ) {
                   2347:       ndv_save(nfv,i);
                   2348:       ndv_free(nfv);
                   2349:     } else
                   2350:       nd_ps[i] = nfv;
                   2351:   }
                   2352:   return 1;
1.1       noro     2353: }
                   2354:
                   2355: LIST compute_splist()
                   2356: {
                   2357:   NODE g,tn0,tn,node;
                   2358:   LIST l0;
                   2359:   ND_pairs d,t;
                   2360:   int i;
                   2361:   Z i1,i2;
                   2362:
                   2363:     g = 0; d = 0;
                   2364:     for ( i = 0; i < nd_psn; i++ ) {
                   2365:         d = update_pairs(d,g,i,0);
                   2366:         g = update_base(g,i);
                   2367:     }
                   2368:   for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
                   2369:     NEXTNODE(tn0,tn);
1.6       noro     2370:         STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1       noro     2371:         node = mknode(2,i1,i2); MKLIST(l0,node);
                   2372:     BDY(tn) = l0;
                   2373:   }
                   2374:   if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
                   2375:   return l0;
                   2376: }
                   2377:
                   2378: /* return value = 0 => input is not a GB */
                   2379:
                   2380: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
                   2381: {
1.6       noro     2382:   int i,nh,sugar,stat;
                   2383:   NODE r,g,t;
                   2384:   ND_pairs d;
                   2385:   ND_pairs l;
                   2386:   ND h,nf,s,head,nf1;
                   2387:   NDV nfv;
                   2388:   Z q;
                   2389:   union oNDC dn,hc;
                   2390:   int diag_count = 0;
                   2391:   P cont;
                   2392:   LIST list;
1.27      noro     2393: struct oEGT eg1,eg2,eg_update;
1.6       noro     2394:
1.27      noro     2395: init_eg(&eg_update);
1.11      noro     2396:   Nnd_add = 0;
1.6       noro     2397:   g = 0; d = 0;
                   2398:   for ( i = 0; i < nd_psn; i++ ) {
                   2399:     d = update_pairs(d,g,i,gensyz);
                   2400:     g = update_base(g,i);
                   2401:   }
                   2402:   sugar = 0;
                   2403:   while ( d ) {
1.1       noro     2404: again:
1.6       noro     2405:     l = nd_minp(d,&d);
                   2406:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   2407:     if ( SG(l) != sugar ) {
                   2408:       if ( ishomo ) {
                   2409:         diag_count = 0;
                   2410:         stat = do_diagonalize(sugar,m);
1.1       noro     2411:         if ( !stat ) {
1.6       noro     2412:           NEXT(l) = d; d = l;
                   2413:           d = nd_reconstruct(0,d);
                   2414:           goto again;
1.1       noro     2415:         }
1.6       noro     2416:       }
                   2417:       sugar = SG(l);
                   2418:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2419:     }
                   2420:     stat = nd_sp(m,0,l,&h);
                   2421:     if ( !stat ) {
                   2422:       NEXT(l) = d; d = l;
                   2423:       d = nd_reconstruct(0,d);
                   2424:       goto again;
                   2425:     }
1.1       noro     2426: #if USE_GEOBUCKET
1.6       noro     2427:     stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!Top,&nf)
                   2428:       :nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1       noro     2429: #else
1.6       noro     2430:     stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1       noro     2431: #endif
1.6       noro     2432:     if ( !stat ) {
                   2433:       NEXT(l) = d; d = l;
                   2434:       d = nd_reconstruct(0,d);
                   2435:       goto again;
                   2436:     } else if ( nf ) {
                   2437:       if ( checkonly || gensyz ) return 0;
1.1       noro     2438:       if ( nd_newelim ) {
                   2439:         if ( nd_module ) {
                   2440:           if ( MPOS(HDL(nf)) > 1 ) return 0;
                   2441:         } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
                   2442:       }
1.6       noro     2443:       if ( DP_Print ) { printf("+"); fflush(stdout); }
                   2444:       hc = HCU(nf);
                   2445:       nd_removecont(m,nf);
                   2446:       if ( !m && nd_nalg ) {
                   2447:         nd_monic(0,&nf);
                   2448:         nd_removecont(m,nf);
                   2449:       }
                   2450:       if ( nd_gentrace ) {
                   2451:         /* exact division */
1.1       noro     2452:         cont = ndc_div(m,hc,HCU(nf));
                   2453:         if ( m || !UNIQ(cont) ) {
1.6       noro     2454:           t = mknode(4,NULLP,NULLP,NULLP,cont);
                   2455:           MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.1       noro     2456:           nd_tracelist = t;
                   2457:         }
                   2458:       }
1.6       noro     2459:       nfv = ndtondv(m,nf); nd_free(nf);
1.24      noro     2460:       nh = ndv_newps(m,nfv,0);
1.6       noro     2461:       if ( !m && (ishomo && ++diag_count == diag_period) ) {
                   2462:         diag_count = 0;
                   2463:         stat = do_diagonalize(sugar,m);
                   2464:         if ( !stat ) {
                   2465:           NEXT(l) = d; d = l;
                   2466:           d = nd_reconstruct(1,d);
                   2467:           goto again;
1.1       noro     2468:         }
1.6       noro     2469:       }
1.27      noro     2470: get_eg(&eg1);
1.6       noro     2471:       d = update_pairs(d,g,nh,0);
1.27      noro     2472: get_eg(&eg2); add_eg(&eg_update,&eg1,&eg2);
1.6       noro     2473:       g = update_base(g,nh);
                   2474:       FREENDP(l);
                   2475:    } else {
                   2476:      if ( nd_gentrace && gensyz ) {
                   2477:        nd_tracelist = reverse_node(nd_tracelist);
                   2478:        MKLIST(list,nd_tracelist);
                   2479:        STOZ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
                   2480:        MKNODE(t,list,nd_alltracelist);
                   2481:        nd_alltracelist = t; nd_tracelist = 0;
                   2482:      }
                   2483:      if ( DP_Print ) { printf("."); fflush(stdout); }
                   2484:        FREENDP(l);
1.36      noro     2485:      }
                   2486:   }
                   2487:   conv_ilist(nd_demand,0,g,indp);
                   2488:   if ( !checkonly && DP_Print ) { printf("nd_gb done. Number of nd_add=%d\n",Nnd_add); fflush(stdout); }
                   2489:
                   2490:   if ( DP_Print )
                   2491:     print_eg("update",&eg_update);
                   2492:   return g;
1.1       noro     2493: }
                   2494:
1.30      noro     2495: ND_pairs update_pairs_s(ND_pairs d,int t,NODE *syz);
                   2496: ND_pairs nd_newpairs_s(int t ,NODE *syz);
1.24      noro     2497:
                   2498: int nd_nf_pbucket_s(int mod,ND g,NDV *ps,int full,ND *nf);
                   2499: int nd_nf_s(int mod,ND d,ND g,NDV *ps,int full,ND *nf);
                   2500:
                   2501: void _copydl(int n,DL d1,DL d2);
                   2502: void _subfromdl(int n,DL d1,DL d2);
                   2503: extern int (*cmpdl)(int n,DL d1,DL d2);
                   2504:
                   2505: NODE insert_sig(NODE l,SIG s)
                   2506: {
                   2507:   int pos;
                   2508:   DL sig;
                   2509:   struct oNODE root;
                   2510:   NODE p,prev,r;
                   2511:   SIG t;
                   2512:
                   2513:   pos = s->pos; sig = DL(s);
                   2514:   root.next = l; prev = &root;
                   2515:   for ( p = l; p; p = p->next ) {
                   2516:     t = (SIG)p->body;
                   2517:     if ( t->pos == pos ) {
                   2518:       if ( _dl_redble(DL(t),sig,nd_nvar) )
                   2519:         return root.next;
                   2520:       else if ( _dl_redble(sig,DL(t),nd_nvar) )
                   2521:         // remove p
                   2522:         prev->next = p->next;
                   2523:     } else
                   2524:       prev = p;
                   2525:   }
1.27      noro     2526:   NEWNODE(r); r->body = (pointer)s; r->next = 0;
                   2527:   for ( p = &root; p->next; p = p->next );
                   2528:   p->next = r;
                   2529: //  r->next = root.next;
                   2530: //  return r;
                   2531:   return root.next;
1.24      noro     2532: }
                   2533:
                   2534: ND_pairs remove_spair_s(ND_pairs d,SIG sig)
                   2535: {
                   2536:   struct oND_pairs root;
                   2537:   ND_pairs prev,p;
                   2538:   SIG spsig;
                   2539:
                   2540:   root.next = d;
                   2541:   prev = &root; p = d;
                   2542:   while ( p ) {
                   2543:     spsig = p->sig;
1.26      noro     2544:     if ( sig->pos == spsig->pos && _dl_redble(DL(sig),DL(spsig),nd_nvar) ) {
1.24      noro     2545:       // remove p
                   2546:       prev->next = p->next;
1.26      noro     2547:       Nsyz++;
                   2548:     } else
1.24      noro     2549:       prev = p;
                   2550:     p = p->next;
                   2551:   }
                   2552:   return (ND_pairs)root.next;
                   2553: }
                   2554:
1.29      noro     2555: int _dl_redble_ext(DL,DL,DL,int);
                   2556:
1.28      noro     2557: int small_lcm(ND_pairs l)
                   2558: {
                   2559:   SIG sig;
                   2560:   int i;
1.29      noro     2561:   NODE t;
1.28      noro     2562:   static DL lcm,mul,quo;
                   2563:   static int nvar;
                   2564:
1.38    ! noro     2565:   if ( nd_sba_largelcm ) return 0;
1.28      noro     2566:   if ( nvar < nd_nvar ) {
                   2567:     nvar = nd_nvar; NEWDL(lcm,nvar); NEWDL(quo,nvar); NEWDL(mul,nvar);
                   2568:   }
                   2569:   sig = l->sig;
                   2570:   _ndltodl(l->lcm,lcm);
1.29      noro     2571: #if 0
1.28      noro     2572:   for ( i = 0; i < nd_psn; i++ ) {
                   2573:     if ( sig->pos == nd_psh[i]->sig->pos &&
1.29      noro     2574:       _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
1.28      noro     2575:       _ndltodl(DL(nd_psh[i]),mul);
                   2576:       _addtodl(nd_nvar,quo,mul);
                   2577:       if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
                   2578:         break;
                   2579:     }
                   2580:   }
                   2581:   if ( i < nd_psn ) return 1;
                   2582:   else return 0;
1.29      noro     2583: #else
                   2584:   for ( t = nd_sba_pos[sig->pos]; t; t = t->next ) {
                   2585:     i = (long)BDY(t);
                   2586:     if ( _dl_redble_ext(DL(nd_psh[i]->sig),DL(sig),quo,nd_nvar) ) {
                   2587:       _ndltodl(DL(nd_psh[i]),mul);
                   2588:       _addtodl(nd_nvar,quo,mul);
                   2589:       if ( (*cmpdl)(nd_nvar,lcm,mul) > 0 )
                   2590:         break;
                   2591:     }
                   2592:   }
                   2593:   if ( t ) return 1;
                   2594:   else return 0;
                   2595: #endif
1.28      noro     2596: }
                   2597:
                   2598: ND_pairs remove_large_lcm(ND_pairs d)
                   2599: {
                   2600:   struct oND_pairs root;
                   2601:   ND_pairs prev,p;
                   2602:
                   2603:   root.next = d;
                   2604:   prev = &root; p = d;
                   2605:   while ( p ) {
                   2606:     if ( small_lcm(p) ) {
                   2607:       // remove p
                   2608:       prev->next = p->next;
                   2609:     } else
                   2610:       prev = p;
                   2611:     p = p->next;
                   2612:   }
                   2613:   return (ND_pairs)root.next;
                   2614: }
                   2615:
1.26      noro     2616: struct oEGT eg_create,eg_newpairs,eg_merge;
                   2617:
1.30      noro     2618: NODE conv_ilist_s(int demand,int trace,int **indp);
                   2619:
1.24      noro     2620: NODE nd_sba_buch(int m,int ishomo,int **indp)
                   2621: {
1.29      noro     2622:   int i,j,nh,sugar,stat;
1.30      noro     2623:   NODE r,t,g;
1.24      noro     2624:   ND_pairs d;
                   2625:   ND_pairs l;
                   2626:   ND h,nf,s,head,nf1;
                   2627:   NDV nfv;
                   2628:   Z q;
                   2629:   union oNDC dn,hc;
                   2630:   P cont;
                   2631:   LIST list;
                   2632:   SIG sig;
1.29      noro     2633:   NODE *syzlist;
1.26      noro     2634:   int Nredundant;
1.28      noro     2635:   DL lcm,quo,mul;
1.31      noro     2636:   struct oEGT eg1,eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.24      noro     2637:
1.27      noro     2638: init_eg(&eg_remove);
1.29      noro     2639:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.26      noro     2640:   Nsyz = 0;
1.24      noro     2641:   Nnd_add = 0;
1.26      noro     2642:   Nredundant = 0;
1.30      noro     2643:   d = 0;
1.24      noro     2644:   for ( i = 0; i < nd_psn; i++ ) {
1.30      noro     2645:     d = update_pairs_s(d,i,syzlist);
1.24      noro     2646:   }
1.29      noro     2647:   for ( i = 0; i < nd_psn; i++ )
                   2648:     for ( j = i+1; j < nd_psn; j++ ) {
                   2649:       NEWSIG(sig); sig->pos = j;
                   2650:       _copydl(nd_nvar,nd_sba_hm[i],sig->dl);
                   2651:       syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
                   2652:     }
1.24      noro     2653:   sugar = 0;
1.26      noro     2654:   NEWDL(lcm,nd_nvar); NEWDL(quo,nd_nvar); NEWDL(mul,nd_nvar);
                   2655: init_eg(&eg_create);
                   2656: init_eg(&eg_merge);
1.29      noro     2657: init_eg(&eg_large);
1.31      noro     2658: init_eg(&eg_nf);
                   2659: init_eg(&eg_nfzero);
1.24      noro     2660:   while ( d ) {
                   2661: again:
1.26      noro     2662:     if ( DP_Print ) {
                   2663:       int len;
                   2664:       ND_pairs td;
1.28      noro     2665:       for ( td = d, len=0; td; td = td->next, len++)
                   2666:         ;
1.26      noro     2667:        if ( !(len%100) ) fprintf(asir_out,"(%d)",len);
                   2668:       }
1.24      noro     2669:     l = d; d = d->next;
1.28      noro     2670:     if ( small_lcm(l) ) {
1.24      noro     2671:       if ( DP_Print ) fprintf(asir_out,"M");
1.26      noro     2672:       Nredundant++;
1.24      noro     2673:       continue;
                   2674:     }
                   2675:     if ( SG(l) != sugar ) {
                   2676:       sugar = SG(l);
                   2677:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2678:     }
1.28      noro     2679:     sig = l->sig;
1.24      noro     2680:     stat = nd_sp(m,0,l,&h);
                   2681:     if ( !stat ) {
                   2682:       NEXT(l) = d; d = l;
                   2683:       d = nd_reconstruct(0,d);
                   2684:       goto again;
                   2685:     }
1.31      noro     2686: get_eg(&eg1);
1.25      noro     2687: #if USE_GEOBUCKET
1.24      noro     2688:     stat = m?nd_nf_pbucket_s(m,h,nd_ps,!Top,&nf):nd_nf_s(m,0,h,nd_ps,!Top,&nf);
                   2689: #else
                   2690:     stat = nd_nf_s(m,0,h,nd_ps,!Top,&nf);
                   2691: #endif
1.31      noro     2692: get_eg(&eg2);
1.24      noro     2693:     if ( !stat ) {
                   2694:       NEXT(l) = d; d = l;
                   2695:       d = nd_reconstruct(0,d);
                   2696:       goto again;
                   2697:     } else if ( stat == -1 ) {
                   2698:       if ( DP_Print ) { printf("S"); fflush(stdout); }
1.27      noro     2699:       FREENDP(l);
1.24      noro     2700:     } else if ( nf ) {
                   2701:       if ( DP_Print ) { printf("+"); fflush(stdout); }
1.31      noro     2702:       add_eg(&eg_nf,&eg1,&eg2);
1.24      noro     2703:       hc = HCU(nf);
                   2704:       nd_removecont(m,nf);
                   2705:       nfv = ndtondv(m,nf); nd_free(nf);
                   2706:       nh = ndv_newps(m,nfv,0);
1.26      noro     2707:
1.30      noro     2708:       d = update_pairs_s(d,nh,syzlist);
1.29      noro     2709:       nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.24      noro     2710:       FREENDP(l);
                   2711:    } else {
1.31      noro     2712:       add_eg(&eg_nfzero,&eg1,&eg2);
1.24      noro     2713:      // syzygy
1.27      noro     2714: get_eg(&eg1);
1.24      noro     2715:      d = remove_spair_s(d,sig);
1.27      noro     2716: get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29      noro     2717:      syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.24      noro     2718:      if ( DP_Print ) { printf("."); fflush(stdout); }
                   2719:      FREENDP(l);
                   2720:    }
                   2721:  }
1.30      noro     2722:  g = conv_ilist_s(nd_demand,0,indp);
1.26      noro     2723:  if ( DP_Print ) {
1.35      noro     2724:    printf("\nnd_sba done. nd_add=%d,Nsyz=%d,Nsamesig=%d,Nredundant=%d\n",Nnd_add,Nsyz,Nsamesig,Nredundant);
1.26      noro     2725:    fflush(stdout);
                   2726:    print_eg("create",&eg_create);
                   2727:    print_eg("merge",&eg_merge);
1.27      noro     2728:    print_eg("remove",&eg_remove);
1.31      noro     2729:    print_eg("nf",&eg_nf);
                   2730:    print_eg("nfzero",&eg_nfzero);
1.27      noro     2731:    printf("\n");
1.26      noro     2732:  }
1.24      noro     2733:  return g;
                   2734: }
                   2735:
1.1       noro     2736: /* splist = [[i1,i2],...] */
                   2737:
                   2738: int check_splist(int m,NODE splist)
                   2739: {
                   2740:   NODE t,p;
                   2741:   ND_pairs d,r,l;
                   2742:   int stat;
                   2743:   ND h,nf;
                   2744:
                   2745:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   2746:     p = BDY((LIST)BDY(t));
1.6       noro     2747:     NEXTND_pairs(d,r);
                   2748:     r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
                   2749:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     2750:     SG(r) = TD(LCM(r)); /* XXX */
                   2751:   }
                   2752:   if ( d ) NEXT(r) = 0;
                   2753:
1.6       noro     2754:   while ( d ) {
1.1       noro     2755: again:
1.6       noro     2756:     l = nd_minp(d,&d);
                   2757:     stat = nd_sp(m,0,l,&h);
                   2758:     if ( !stat ) {
                   2759:       NEXT(l) = d; d = l;
                   2760:       d = nd_reconstruct(0,d);
                   2761:       goto again;
                   2762:     }
                   2763:     stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
                   2764:     if ( !stat ) {
                   2765:       NEXT(l) = d; d = l;
                   2766:       d = nd_reconstruct(0,d);
                   2767:       goto again;
                   2768:     } else if ( nf ) return 0;
1.1       noro     2769:     if ( DP_Print) { printf("."); fflush(stdout); }
1.6       noro     2770:   }
1.1       noro     2771:   if ( DP_Print) { printf("done.\n"); fflush(stdout); }
                   2772:   return 1;
                   2773: }
                   2774:
                   2775: int check_splist_f4(int m,NODE splist)
                   2776: {
                   2777:   UINT *s0vect;
1.6       noro     2778:   PGeoBucket bucket;
1.1       noro     2779:   NODE p,rp0,t;
                   2780:   ND_pairs d,r,l,ll;
                   2781:   int col,stat;
                   2782:
                   2783:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   2784:     p = BDY((LIST)BDY(t));
1.6       noro     2785:     NEXTND_pairs(d,r);
                   2786:     r->i1 = ZTOS((Q)ARG0(p)); r->i2 = ZTOS((Q)ARG1(p));
                   2787:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.1       noro     2788:     SG(r) = TD(LCM(r)); /* XXX */
                   2789:   }
                   2790:   if ( d ) NEXT(r) = 0;
                   2791:
1.6       noro     2792:   while ( d ) {
                   2793:     l = nd_minsugarp(d,&d);
                   2794:     bucket = create_pbucket();
                   2795:     stat = nd_sp_f4(m,0,l,bucket);
                   2796:     if ( !stat ) {
                   2797:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   2798:       NEXT(ll) = d; d = l;
                   2799:       d = nd_reconstruct(0,d);
                   2800:       continue;
                   2801:     }
                   2802:     if ( bucket->m < 0 ) continue;
                   2803:     col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   2804:     if ( !col ) {
                   2805:       for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   2806:       NEXT(ll) = d; d = l;
                   2807:       d = nd_reconstruct(0,d);
                   2808:       continue;
1.1       noro     2809:     }
1.6       noro     2810:     if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
                   2811:   }
                   2812:   return 1;
1.1       noro     2813: }
                   2814:
                   2815: int do_diagonalize_trace(int sugar,int m)
                   2816: {
1.6       noro     2817:   int i,nh,stat;
                   2818:   NODE r,g,t;
                   2819:   ND h,nf,nfq,s,head;
                   2820:   NDV nfv,nfqv;
                   2821:   Q q,den,num;
                   2822:   union oNDC hc;
                   2823:   NODE node;
                   2824:   LIST l;
                   2825:   Z iq;
                   2826:   P cont,cont1;
1.1       noro     2827:
1.6       noro     2828:   for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
                   2829:     if ( nd_gentrace ) {
                   2830:         /* Trace = [1,index,1,1] */
                   2831:         STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2832:         MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2833:     }
                   2834:     /* for nd_ps */
                   2835:     s = ndvtond(m,nd_ps[i]);
                   2836:     s = nd_separate_head(s,&head);
                   2837:     stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
                   2838:     if ( !stat ) return 0;
                   2839:     nf = nd_add(m,head,nf);
                   2840:     ndv_free(nd_ps[i]);
                   2841:     nd_ps[i] = ndtondv(m,nf);
                   2842:     nd_free(nf);
                   2843:
                   2844:     /* for nd_ps_trace */
                   2845:     if ( nd_demand )
                   2846:         nfv = ndv_load(i);
                   2847:     else
                   2848:         nfv = nd_ps_trace[i];
                   2849:     s = ndvtond(0,nfv);
                   2850:     s = nd_separate_head(s,&head);
                   2851:     stat = nd_nf(0,head,s,nd_ps_trace,1,&nf);
                   2852:     if ( !stat ) return 0;
                   2853:     ndv_free(nfv);
                   2854:     hc = HCU(nf); nd_removecont(0,nf);
                   2855:     /* exact division */
1.1       noro     2856:     cont = ndc_div(0,hc,HCU(nf));
1.6       noro     2857:     if ( nd_gentrace ) finalize_tracelist(i,cont);
                   2858:     nfv = ndtondv(0,nf);
                   2859:     nd_free(nf);
                   2860:     nd_bound[i] = ndv_compute_bound(nfv);
                   2861:     register_hcf(nfv);
                   2862:     if ( nd_demand ) {
                   2863:     ndv_save(nfv,i);
                   2864:     ndv_free(nfv);
                   2865:     } else
                   2866:     nd_ps_trace[i] = nfv;
                   2867:   }
                   2868:   return 1;
1.1       noro     2869: }
                   2870:
                   2871: static struct oEGT eg_invdalg;
                   2872: struct oEGT eg_le;
                   2873:
                   2874: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
                   2875: {
                   2876:     NODE tn;
                   2877:     P p1;
                   2878:
                   2879:     for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
                   2880:         substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
                   2881:     }
                   2882:     *r = p;
                   2883: }
                   2884:
                   2885: NODE nd_gb_trace(int m,int ishomo,int **indp)
                   2886: {
1.6       noro     2887:   int i,nh,sugar,stat;
                   2888:   NODE r,g,t;
                   2889:   ND_pairs d;
                   2890:   ND_pairs l;
                   2891:   ND h,nf,nfq,s,head;
                   2892:   NDV nfv,nfqv;
                   2893:   Z q,den,num;
                   2894:   P hc;
                   2895:   union oNDC dn,hnfq;
                   2896:   struct oEGT eg_monic,egm0,egm1;
                   2897:   int diag_count = 0;
                   2898:   P cont;
                   2899:   LIST list;
                   2900:
                   2901:   init_eg(&eg_monic);
                   2902:   init_eg(&eg_invdalg);
                   2903:   init_eg(&eg_le);
                   2904:   g = 0; d = 0;
                   2905:   for ( i = 0; i < nd_psn; i++ ) {
                   2906:     d = update_pairs(d,g,i,0);
                   2907:     g = update_base(g,i);
                   2908:   }
                   2909:   sugar = 0;
                   2910:   while ( d ) {
1.1       noro     2911: again:
1.6       noro     2912:     l = nd_minp(d,&d);
                   2913:     if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
                   2914:     if ( SG(l) != sugar ) {
1.1       noro     2915: #if 1
1.6       noro     2916:       if ( ishomo ) {
                   2917:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2918:         stat = do_diagonalize_trace(sugar,m);
                   2919:         if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2920:         diag_count = 0;
1.1       noro     2921:         if ( !stat ) {
1.6       noro     2922:           NEXT(l) = d; d = l;
                   2923:           d = nd_reconstruct(1,d);
                   2924:           goto again;
1.1       noro     2925:         }
1.6       noro     2926:       }
                   2927: #endif
                   2928:       sugar = SG(l);
                   2929:       if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2930:     }
                   2931:     stat = nd_sp(m,0,l,&h);
                   2932:     if ( !stat ) {
                   2933:       NEXT(l) = d; d = l;
                   2934:       d = nd_reconstruct(1,d);
                   2935:       goto again;
                   2936:     }
1.1       noro     2937: #if USE_GEOBUCKET
1.6       noro     2938:     stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.1       noro     2939: #else
1.6       noro     2940:     stat = nd_nf(m,0,h,nd_ps,!Top,&nf);
1.1       noro     2941: #endif
1.6       noro     2942:     if ( !stat ) {
                   2943:       NEXT(l) = d; d = l;
                   2944:       d = nd_reconstruct(1,d);
                   2945:       goto again;
                   2946:     } else if ( nf ) {
                   2947:       if ( nd_demand ) {
                   2948:         nfqv = ndv_load(nd_psn);
                   2949:         nfq = ndvtond(0,nfqv);
                   2950:       } else
                   2951:         nfq = 0;
                   2952:       if ( !nfq ) {
                   2953:         if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!Top,&nfq) ) {
                   2954:           NEXT(l) = d; d = l;
                   2955:           d = nd_reconstruct(1,d);
                   2956:           goto again;
                   2957:         }
                   2958:       }
                   2959:       if ( nfq ) {
                   2960:         /* m|HC(nfq) => failure */
                   2961:         if ( nd_vc ) {
                   2962:           nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Z)hc;
                   2963:         } else
                   2964:           q = HCZ(nfq);
                   2965:         if ( !remqi((Q)q,m) ) return 0;
                   2966:
                   2967:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   2968:         hnfq = HCU(nfq);
                   2969:         if ( nd_nalg ) {
                   2970:           /* m|DN(HC(nf)^(-1)) => failure */
                   2971:           get_eg(&egm0);
                   2972:           if ( !nd_monic(m,&nfq) ) return 0;
                   2973:           get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
                   2974:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   2975:           nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
                   2976:         } else {
                   2977:           nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   2978:           nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
                   2979:         }
                   2980:         if ( nd_gentrace ) {
                   2981:           /* exact division */
                   2982:           cont = ndc_div(0,hnfq,HCU(nfqv));
                   2983:           if ( !UNIQ(cont) ) {
                   2984:             t = mknode(4,NULLP,NULLP,NULLP,cont);
                   2985:             MKLIST(list,t); MKNODE(t,list,nd_tracelist);
                   2986:             nd_tracelist = t;
                   2987:           }
                   2988:         }
1.24      noro     2989:         nh = ndv_newps(0,nfv,nfqv);
1.6       noro     2990:         if ( ishomo && ++diag_count == diag_period ) {
                   2991:           diag_count = 0;
                   2992:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2993:           stat = do_diagonalize_trace(sugar,m);
                   2994:           if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2995:           if ( !stat ) {
1.1       noro     2996:             NEXT(l) = d; d = l;
                   2997:             d = nd_reconstruct(1,d);
                   2998:             goto again;
1.6       noro     2999:           }
1.1       noro     3000:         }
1.6       noro     3001:         d = update_pairs(d,g,nh,0);
                   3002:         g = update_base(g,nh);
                   3003:       } else {
                   3004:         if ( DP_Print ) { printf("*"); fflush(stdout); }
                   3005:       }
                   3006:     } else {
                   3007:       if ( DP_Print ) { printf("."); fflush(stdout); }
1.1       noro     3008:     }
1.6       noro     3009:     FREENDP(l);
                   3010:   }
                   3011:   if ( nd_nalg ) {
                   3012:     if ( DP_Print ) {
                   3013:       print_eg("monic",&eg_monic);
                   3014:       print_eg("invdalg",&eg_invdalg);
                   3015:       print_eg("le",&eg_le);
1.1       noro     3016:     }
1.6       noro     3017:   }
1.1       noro     3018:   conv_ilist(nd_demand,1,g,indp);
1.6       noro     3019:   if ( DP_Print ) { printf("nd_gb_trace done.\n"); fflush(stdout); }
                   3020:   return g;
1.1       noro     3021: }
                   3022:
                   3023: int ndv_compare(NDV *p1,NDV *p2)
                   3024: {
                   3025:     return DL_COMPARE(HDL(*p1),HDL(*p2));
                   3026: }
                   3027:
                   3028: int ndv_compare_rev(NDV *p1,NDV *p2)
                   3029: {
                   3030:     return -DL_COMPARE(HDL(*p1),HDL(*p2));
                   3031: }
                   3032:
                   3033: int ndvi_compare(NDVI p1,NDVI p2)
                   3034: {
                   3035:     return DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3036: }
                   3037:
                   3038: int ndvi_compare_rev(NDVI p1,NDVI p2)
                   3039: {
                   3040:     return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   3041: }
                   3042:
                   3043: NODE ndv_reduceall(int m,NODE f)
                   3044: {
                   3045:   int i,j,n,stat;
                   3046:   ND nf,g,head;
                   3047:   NODE t,a0,a;
                   3048:   union oNDC dn;
                   3049:   Q q,num,den;
                   3050:   NODE node;
                   3051:   LIST l;
                   3052:   Z iq,jq;
                   3053:   int *perm;
                   3054:   union oNDC hc;
                   3055:   P cont,cont1;
                   3056:
                   3057:   if ( nd_nora ) return f;
                   3058:   n = length(f);
1.24      noro     3059:   ndv_setup(m,0,f,0,1,0);
1.1       noro     3060:   perm = (int *)MALLOC(n*sizeof(int));
                   3061:   if ( nd_gentrace ) {
                   3062:     for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
1.6       noro     3063:       perm[i] = ZTOS((Q)ARG1(BDY((LIST)BDY(t))));
1.1       noro     3064:   }
                   3065:   for ( i = 0; i < n; ) {
                   3066:     if ( nd_gentrace ) {
                   3067:       /* Trace = [1,index,1,1] */
1.6       noro     3068:       STOZ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
1.1       noro     3069:       MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   3070:     }
                   3071:     g = ndvtond(m,nd_ps[i]);
                   3072:     g = nd_separate_head(g,&head);
1.6       noro     3073:     stat = nd_nf(m,head,g,nd_ps,1,&nf);
1.1       noro     3074:     if ( !stat )
                   3075:       nd_reconstruct(0,0);
                   3076:     else {
                   3077:       if ( DP_Print ) { printf("."); fflush(stdout); }
                   3078:       ndv_free(nd_ps[i]);
                   3079:       hc = HCU(nf); nd_removecont(m,nf);
                   3080:       if ( nd_gentrace ) {
                   3081:         for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.6       noro     3082:           jq = ARG1(BDY((LIST)BDY(t))); j = ZTOS(jq);
                   3083:           STOZ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
1.1       noro     3084:         }
1.6       noro     3085:         /* exact division */
1.1       noro     3086:         cont = ndc_div(m,hc,HCU(nf));
                   3087:         finalize_tracelist(perm[i],cont);
                   3088:       }
                   3089:       nd_ps[i] = ndtondv(m,nf); nd_free(nf);
                   3090:       nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   3091:       i++;
                   3092:     }
                   3093:   }
                   3094:   if ( DP_Print ) { printf("\n"); }
                   3095:   for ( a0 = 0, i = 0; i < n; i++ ) {
                   3096:     NEXTNODE(a0,a);
                   3097:     if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
                   3098:     else {
                   3099:       for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
                   3100:       BDY(a) = (pointer)nd_ps[j];
                   3101:     }
                   3102:   }
                   3103:   NEXT(a) = 0;
                   3104:   return a0;
                   3105: }
                   3106:
                   3107: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
                   3108: {
                   3109:   ND_pairs d1,nd,cur,head,prev,remove;
                   3110:
                   3111:   if ( !g ) return d;
                   3112:   /* for testing */
                   3113:   if ( gensyz && nd_gensyz == 2 ) {
                   3114:     d1 = nd_newpairs(g,t);
                   3115:     if ( !d )
                   3116:       return d1;
                   3117:     else {
                   3118:       nd = d;
                   3119:       while ( NEXT(nd) ) nd = NEXT(nd);
                   3120:       NEXT(nd) = d1;
                   3121:       return d;
                   3122:     }
                   3123:   }
                   3124:   d = crit_B(d,t);
                   3125:   d1 = nd_newpairs(g,t);
                   3126:   d1 = crit_M(d1);
                   3127:   d1 = crit_F(d1);
                   3128:   if ( gensyz || do_weyl )
                   3129:     head = d1;
                   3130:   else {
                   3131:     prev = 0; cur = head = d1;
                   3132:     while ( cur ) {
                   3133:       if ( crit_2( cur->i1,cur->i2 ) ) {
                   3134:         remove = cur;
                   3135:         if ( !prev ) head = cur = NEXT(cur);
                   3136:         else cur = NEXT(prev) = NEXT(cur);
                   3137:         FREENDP(remove);
                   3138:       } else {
                   3139:         prev = cur; cur = NEXT(cur);
                   3140:       }
                   3141:     }
                   3142:   }
                   3143:   if ( !d )
                   3144:     return head;
                   3145:   else {
                   3146:     nd = d;
                   3147:     while ( NEXT(nd) ) nd = NEXT(nd);
                   3148:     NEXT(nd) = head;
                   3149:     return d;
                   3150:   }
                   3151: }
                   3152:
1.24      noro     3153: ND_pairs merge_pairs_s(ND_pairs d,ND_pairs d1);
                   3154:
1.30      noro     3155: ND_pairs update_pairs_s( ND_pairs d, int t,NODE *syz)
1.24      noro     3156: {
                   3157:   ND_pairs d1;
1.26      noro     3158:   struct oEGT eg1,eg2,eg3;
1.24      noro     3159:
1.30      noro     3160:   if ( !t ) return d;
1.26      noro     3161: get_eg(&eg1);
1.30      noro     3162:   d1 = nd_newpairs_s(t,syz);
1.26      noro     3163: get_eg(&eg2); add_eg(&eg_create,&eg1,&eg2);
1.24      noro     3164:   d = merge_pairs_s(d,d1);
1.26      noro     3165: get_eg(&eg3); add_eg(&eg_merge,&eg2,&eg3);
1.24      noro     3166:   return d;
                   3167: }
1.1       noro     3168:
                   3169: ND_pairs nd_newpairs( NODE g, int t )
                   3170: {
                   3171:   NODE h;
                   3172:   UINT *dl;
                   3173:   int ts,s,i,t0,min,max;
                   3174:   ND_pairs r,r0;
                   3175:
                   3176:   dl = DL(nd_psh[t]);
                   3177:   ts = SG(nd_psh[t]) - TD(dl);
1.17      noro     3178:   if ( nd_module && nd_intersect && (MPOS(dl) > nd_intersect) ) return 0;
1.1       noro     3179:   for ( r0 = 0, h = g; h; h = NEXT(h) ) {
                   3180:     if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
                   3181:       continue;
                   3182:     if ( nd_gbblock ) {
                   3183:       t0 = (long)BDY(h);
                   3184:       for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
                   3185:         min = nd_gbblock[i]; max = nd_gbblock[i+1];
                   3186:         if ( t0 >= min && t0 <= max && t >= min && t <= max )
                   3187:           break;
                   3188:       }
                   3189:       if ( nd_gbblock[i] >= 0 )
                   3190:         continue;
                   3191:     }
                   3192:     NEXTND_pairs(r0,r);
                   3193:     r->i1 = (long)BDY(h);
                   3194:     r->i2 = t;
                   3195:     ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
                   3196:     s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   3197:     SG(r) = MAX(s,ts) + TD(LCM(r));
                   3198:     /* experimental */
                   3199:     if ( nd_sugarweight )
                   3200:       r->sugar2 = ndl_weight2(r->lcm);
                   3201:   }
                   3202:   if ( r0 ) NEXT(r) = 0;
                   3203:   return r0;
                   3204: }
                   3205:
1.24      noro     3206: int comp_sig(SIG s1,SIG s2)
                   3207: {
1.38    ! noro     3208:   if ( nd_sba_pot ) {
        !          3209:     if ( s1->pos > s2->pos ) return 1;
        !          3210:     else if ( s1->pos < s2->pos ) return -1;
        !          3211:     else return (*cmpdl)(nd_nvar,s1->dl,s2->dl);
        !          3212:   } else {
        !          3213:     static DL m1,m2;
        !          3214:     static int nvar;
        !          3215:     int ret;
        !          3216:
        !          3217:     if ( nvar != nd_nvar ) {
        !          3218:       nvar = nd_nvar; NEWDL(m1,nvar); NEWDL(m2,nvar);
        !          3219:     }
        !          3220:     _adddl(nd_nvar,s1->dl,nd_sba_hm[s1->pos],m1);
        !          3221:     _adddl(nd_nvar,s2->dl,nd_sba_hm[s2->pos],m2);
        !          3222:     ret = (*cmpdl)(nd_nvar,m1,m2);
        !          3223:     if ( ret != 0 ) return ret;
        !          3224:     else if ( s1->pos > s2->pos ) return 1;
        !          3225:     else if ( s1->pos < s2->pos ) return -1;
        !          3226:     else return 0;
1.24      noro     3227:   }
                   3228: }
                   3229:
                   3230: int _create_spair_s(int i1,int i2,ND_pairs sp,SIG sig1,SIG sig2)
                   3231: {
                   3232:   int ret,s1,s2;
                   3233:   RHist p1,p2;
                   3234:   static int wpd;
                   3235:   static UINT *lcm;
                   3236:
                   3237:   sp->i1 = i1;
                   3238:   sp->i2 = i2;
                   3239:   p1 = nd_psh[i1];
                   3240:   p2 = nd_psh[i2];
                   3241:   ndl_lcm(DL(p1),DL(p2),sp->lcm);
                   3242:   s1 = SG(p1)-TD(DL(p1));
                   3243:   s2 = SG(p2)-TD(DL(p2));
                   3244:   SG(sp) = MAX(s1,s2) + TD(sp->lcm);
                   3245:
1.26      noro     3246:   if ( wpd != nd_wpd ) {
1.24      noro     3247:     wpd = nd_wpd;
                   3248:     lcm = (UINT *)MALLOC(wpd*sizeof(UINT));
                   3249:   }
                   3250:   // DL(sig1) <- sp->lcm
                   3251:   // DL(sig1) -= DL(p1)
                   3252:   // DL(sig1) += DL(p1->sig)
1.27      noro     3253:   ndl_sub(sp->lcm,DL(p1),lcm);
1.24      noro     3254:   _ndltodl(lcm,DL(sig1));
                   3255:   _addtodl(nd_nvar,DL(p1->sig),DL(sig1));
                   3256:   sig1->pos = p1->sig->pos;
                   3257:
                   3258:   // DL(sig2) <- sp->lcm
                   3259:   // DL(sig2) -= DL(p2)
                   3260:   // DL(sig2) += DL(p2->sig)
1.27      noro     3261:   ndl_sub(sp->lcm,DL(p2),lcm);
1.24      noro     3262:   _ndltodl(lcm,DL(sig2));
                   3263:   _addtodl(nd_nvar,DL(p2->sig),DL(sig2));
                   3264:   sig2->pos = p2->sig->pos;
                   3265:
                   3266:   ret = comp_sig(sig1,sig2);
                   3267:   if ( ret == 0 ) return 0;
                   3268:   else if ( ret > 0 ) sp->sig = sig1;
                   3269:   else sp->sig = sig2;
                   3270:   return 1;
                   3271: }
                   3272:
                   3273: SIG dup_sig(SIG sig)
                   3274: {
                   3275:   SIG r;
                   3276:
                   3277:   if ( !sig ) return 0;
                   3278:   else {
                   3279:     NEWSIG(r);
                   3280:     _copydl(nd_nvar,DL(sig),DL(r));
                   3281:     r->pos = sig->pos;
                   3282:     return r;
                   3283:   }
                   3284: }
                   3285:
                   3286: void dup_ND_pairs(ND_pairs to,ND_pairs from)
                   3287: {
                   3288:   to->i1 = from->i1;
                   3289:   to->i2 = from->i2;
                   3290:   to->sugar = from->sugar;
                   3291:   to->sugar2 = from->sugar2;
                   3292:   ndl_copy(from->lcm,to->lcm);
                   3293:   to->sig = dup_sig(from->sig);
                   3294: }
                   3295:
                   3296: ND_pairs merge_pairs_s(ND_pairs p1,ND_pairs p2)
                   3297: {
                   3298:   struct oND_pairs root;
                   3299:   ND_pairs q1,q2,r0,r;
                   3300:   int ret;
                   3301:
                   3302:   r = &root;
                   3303:   for ( q1 = p1, q2 = p2; q1 != 0 && q2 != 0; ) {
                   3304:     ret = comp_sig(q1->sig,q2->sig);
                   3305:     if ( ret < 0 ) {
                   3306:       r->next = q1; r = q1; q1 = q1->next;
                   3307:     } else if ( ret > 0 ) {
                   3308:       r->next = q2; r = q2; q2 = q2->next;
                   3309:     } else {
                   3310:       ret = DL_COMPARE(q1->lcm,q2->lcm);
1.35      noro     3311:       Nsamesig++;
1.24      noro     3312:       if ( ret < 0 ) {
                   3313:         r->next = q1; r = q1; q1 = q1->next;
                   3314:         q2 = q2->next;
                   3315:       } else {
                   3316:         r->next = q2; r = q2; q2 = q2->next;
                   3317:         q1 = q1->next;
                   3318:       }
                   3319:     }
                   3320:   }
                   3321:   if ( q1 ) {
                   3322:     r->next = q1;
                   3323:   } else {
                   3324:     r->next = q2;
                   3325:   }
                   3326:   return root.next;
                   3327: }
                   3328:
                   3329: ND_pairs insert_pair_s(ND_pairs l,ND_pairs s)
                   3330: {
                   3331:   ND_pairs p,prev;
                   3332:   int ret;
                   3333:
                   3334:   for ( p = l, prev = 0; p != 0; prev = p, p = p->next ) {
                   3335:     if ( (ret = comp_sig(s->sig,p->sig)) <= 0 )
                   3336:       break;
                   3337:   }
                   3338:   if ( ret == 0 ) {
                   3339:     ret = DL_COMPARE(s->lcm,p->lcm);
                   3340:     if ( ret < 0 ) {
                   3341:       // replace p with s
                   3342:       s->next = p->next;
                   3343:       if ( prev == 0 ) {
                   3344:         return s;
                   3345:       } else {
                   3346:         prev->next = s;
                   3347:         return l;
                   3348:       }
                   3349:     } else
                   3350:       return l;
                   3351:   } else {
                   3352:     // insert s between prev and p
                   3353:     s->next = p;
                   3354:     if ( prev == 0 ) {
                   3355:       return s;
                   3356:     } else {
                   3357:       prev->next = s;
                   3358:       return l;
                   3359:     }
                   3360:   }
                   3361: }
                   3362:
1.29      noro     3363: INLINE int __dl_redble(DL d1,DL d2,int nvar)
                   3364: {
                   3365:   int i;
                   3366:
                   3367:   if ( d1->td > d2->td )
                   3368:     return 0;
                   3369:   for ( i = nvar-1; i >= 0; i-- )
                   3370:     if ( d1->d[i] > d2->d[i] )
                   3371:       break;
                   3372:   if ( i >= 0 )
                   3373:     return 0;
                   3374:   else
                   3375:     return 1;
                   3376: }
                   3377:
1.30      noro     3378: ND_pairs nd_newpairs_s(int t, NODE *syz)
1.24      noro     3379: {
                   3380:   NODE h,s;
                   3381:   UINT *dl;
1.27      noro     3382:   int ts,ret,i;
1.24      noro     3383:   ND_pairs r,r0,_sp,sp;
1.29      noro     3384:   SIG spsig,tsig;
                   3385:   static int nvar;
                   3386:   static SIG _sig1,_sig2;
1.26      noro     3387:   struct oEGT eg1,eg2,eg3,eg4;
1.24      noro     3388:
                   3389:   NEWND_pairs(_sp);
1.29      noro     3390:   if ( !_sig1 || nvar != nd_nvar ) {
                   3391:     nvar = nd_nvar; NEWSIG(_sig1); NEWSIG(_sig2);
                   3392:   }
1.24      noro     3393:   r0 = 0;
1.27      noro     3394:   for ( i = 0; i < t; i++ ) {
                   3395:     ret = _create_spair_s(i,t,_sp,_sig1,_sig2);
1.24      noro     3396:     if ( ret ) {
                   3397:       spsig = _sp->sig;
1.29      noro     3398:       for ( s = syz[spsig->pos]; s; s = s->next ) {
1.24      noro     3399:         tsig = (SIG)s->body;
1.29      noro     3400:         if ( _dl_redble(DL(tsig),DL(spsig),nd_nvar) )
1.24      noro     3401:           break;
                   3402:       }
                   3403:       if ( s == 0 ) {
                   3404:         NEWND_pairs(sp);
                   3405:         dup_ND_pairs(sp,_sp);
                   3406:         r0 = insert_pair_s(r0,sp);
1.26      noro     3407:       } else
                   3408:         Nsyz++;
1.24      noro     3409:     }
                   3410:   }
                   3411:   return r0;
                   3412: }
                   3413:
1.1       noro     3414: /* ipair = [i1,i2],[i1,i2],... */
                   3415: ND_pairs nd_ipairtospair(NODE ipair)
                   3416: {
                   3417:   int s1,s2;
                   3418:   NODE tn,t;
                   3419:   ND_pairs r,r0;
                   3420:
                   3421:   for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
                   3422:     NEXTND_pairs(r0,r);
                   3423:     tn = BDY((LIST)BDY(t));
1.6       noro     3424:     r->i1 = ZTOS((Q)ARG0(tn));
                   3425:     r->i2 = ZTOS((Q)ARG1(tn));
1.1       noro     3426:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
                   3427:     s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   3428:     s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
                   3429:     SG(r) = MAX(s1,s2) + TD(LCM(r));
                   3430:     /* experimental */
                   3431:     if ( nd_sugarweight )
                   3432:         r->sugar2 = ndl_weight2(r->lcm);
                   3433:   }
                   3434:   if ( r0 ) NEXT(r) = 0;
                   3435:   return r0;
                   3436: }
                   3437:
                   3438: /* kokokara */
                   3439:
                   3440: ND_pairs crit_B( ND_pairs d, int s )
                   3441: {
                   3442:   ND_pairs cur,head,prev,remove;
                   3443:   UINT *t,*tl,*lcm;
                   3444:   int td,tdl;
                   3445:
                   3446:   if ( !d ) return 0;
                   3447:   t = DL(nd_psh[s]);
                   3448:   prev = 0;
                   3449:   head = cur = d;
                   3450:   lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   3451:   while ( cur ) {
                   3452:     tl = cur->lcm;
                   3453:     if ( ndl_reducible(tl,t) ) {
                   3454:       ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
                   3455:       if ( !ndl_equal(lcm,tl) ) {
                   3456:         ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
                   3457:         if (!ndl_equal(lcm,tl)) {
                   3458:           remove = cur;
                   3459:           if ( !prev ) {
                   3460:             head = cur = NEXT(cur);
                   3461:           } else {
                   3462:             cur = NEXT(prev) = NEXT(cur);
                   3463:           }
                   3464:           FREENDP(remove);
                   3465:         } else {
                   3466:           prev = cur; cur = NEXT(cur);
                   3467:         }
                   3468:       } else {
                   3469:         prev = cur; cur = NEXT(cur);
                   3470:       }
                   3471:     } else {
                   3472:       prev = cur; cur = NEXT(cur);
                   3473:     }
                   3474:   }
                   3475:   return head;
                   3476: }
                   3477:
                   3478: ND_pairs crit_M( ND_pairs d1 )
                   3479: {
                   3480:     ND_pairs e,d2,d3,dd,p;
                   3481:     UINT *id,*jd;
                   3482:
                   3483:     if ( !d1 ) return d1;
                   3484:     for ( dd = 0, e = d1; e; e = d3 ) {
                   3485:         if ( !(d2 = NEXT(e)) ) {
                   3486:             NEXT(e) = dd;
                   3487:             return e;
                   3488:         }
                   3489:         id = LCM(e);
                   3490:         for ( d3 = 0; d2; d2 = p ) {
                   3491:             p = NEXT(d2);
                   3492:             jd = LCM(d2);
                   3493:             if ( ndl_equal(jd,id) )
                   3494:                 ;
                   3495:             else if ( TD(jd) > TD(id) )
                   3496:                 if ( ndl_reducible(jd,id) ) continue;
                   3497:                 else ;
                   3498:             else if ( ndl_reducible(id,jd) ) goto delit;
                   3499:             NEXT(d2) = d3;
                   3500:             d3 = d2;
                   3501:         }
                   3502:         NEXT(e) = dd;
                   3503:         dd = e;
                   3504:         continue;
                   3505:         /**/
                   3506:     delit:    NEXT(d2) = d3;
                   3507:         d3 = d2;
                   3508:         for ( ; p; p = d2 ) {
                   3509:             d2 = NEXT(p);
                   3510:             NEXT(p) = d3;
                   3511:             d3 = p;
                   3512:         }
                   3513:         FREENDP(e);
                   3514:     }
                   3515:     return dd;
                   3516: }
                   3517:
                   3518: ND_pairs crit_F( ND_pairs d1 )
                   3519: {
                   3520:     ND_pairs rest, head,remove;
                   3521:     ND_pairs last, p, r, w;
                   3522:     int s;
                   3523:
                   3524:     if ( !d1 ) return d1;
                   3525:     for ( head = last = 0, p = d1; NEXT(p); ) {
                   3526:         r = w = equivalent_pairs(p,&rest);
                   3527:         s = SG(r);
                   3528:         w = NEXT(w);
                   3529:         while ( w ) {
                   3530:             if ( crit_2(w->i1,w->i2) ) {
                   3531:                 r = w;
                   3532:                 w = NEXT(w);
                   3533:                 while ( w ) {
                   3534:                     remove = w;
                   3535:                     w = NEXT(w);
                   3536:                     FREENDP(remove);
                   3537:                 }
                   3538:                 break;
                   3539:             } else if ( SG(w) < s ) {
                   3540:                 FREENDP(r);
                   3541:                 r = w;
                   3542:                 s = SG(r);
                   3543:                 w = NEXT(w);
                   3544:             } else {
                   3545:                 remove = w;
                   3546:                 w = NEXT(w);
                   3547:                 FREENDP(remove);
                   3548:             }
                   3549:         }
                   3550:         if ( last ) NEXT(last) = r;
                   3551:         else head = r;
                   3552:         NEXT(last = r) = 0;
                   3553:         p = rest;
                   3554:         if ( !p ) return head;
                   3555:     }
                   3556:     if ( !last ) return p;
                   3557:     NEXT(last) = p;
                   3558:     return head;
                   3559: }
                   3560:
                   3561: int crit_2( int dp1, int dp2 )
                   3562: {
                   3563:     return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
                   3564: }
                   3565:
                   3566: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
                   3567: {
                   3568:     ND_pairs w,p,r,s;
                   3569:     UINT *d;
                   3570:
                   3571:     w = d1;
                   3572:     d = LCM(w);
                   3573:     s = NEXT(w);
                   3574:     NEXT(w) = 0;
                   3575:     for ( r = 0; s; s = p ) {
                   3576:         p = NEXT(s);
                   3577:         if ( ndl_equal(d,LCM(s)) ) {
                   3578:             NEXT(s) = w; w = s;
                   3579:         } else {
                   3580:             NEXT(s) = r; r = s;
                   3581:         }
                   3582:     }
                   3583:     *prest = r;
                   3584:     return w;
                   3585: }
                   3586:
                   3587: NODE update_base(NODE nd,int ndp)
                   3588: {
                   3589:     UINT *dl, *dln;
                   3590:     NODE last, p, head;
                   3591:
                   3592:     dl = DL(nd_psh[ndp]);
                   3593:     for ( head = last = 0, p = nd; p; ) {
                   3594:         dln = DL(nd_psh[(long)BDY(p)]);
                   3595:         if ( ndl_reducible( dln, dl ) ) {
                   3596:             p = NEXT(p);
                   3597:             if ( last ) NEXT(last) = p;
                   3598:         } else {
                   3599:             if ( !last ) head = p;
                   3600:             p = NEXT(last = p);
                   3601:         }
                   3602:     }
                   3603:     head = append_one(head,ndp);
                   3604:     return head;
                   3605: }
                   3606:
                   3607: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   3608: {
                   3609:     ND_pairs m,ml,p,l;
                   3610:     UINT *lcm;
                   3611:     int s,td,len,tlen,c,c1;
                   3612:
                   3613:     if ( !(p = NEXT(m = d)) ) {
                   3614:         *prest = p;
                   3615:         NEXT(m) = 0;
                   3616:         return m;
                   3617:     }
                   3618:     if ( !NoSugar ) {
                   3619:         if ( nd_sugarweight ) {
                   3620:             s = m->sugar2;
                   3621:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   3622:                 if ( (p->sugar2 < s)
                   3623:                     || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   3624:                     ml = l; m = p; s = m->sugar2;
                   3625:                 }
                   3626:         } else {
                   3627:             s = SG(m);
                   3628:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   3629:                 if ( (SG(p) < s)
                   3630:                     || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   3631:                     ml = l; m = p; s = SG(m);
                   3632:                 }
                   3633:         }
                   3634:     } else {
                   3635:         for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   3636:             if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
                   3637:                 ml = l; m = p; s = SG(m);
                   3638:             }
                   3639:     }
                   3640:     if ( !ml ) *prest = NEXT(m);
                   3641:     else {
                   3642:         NEXT(ml) = NEXT(m);
                   3643:         *prest = d;
                   3644:     }
                   3645:     NEXT(m) = 0;
                   3646:     return m;
                   3647: }
                   3648:
                   3649: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   3650: {
                   3651:     int msugar,i;
                   3652:     ND_pairs t,dm0,dm,dr0,dr;
                   3653:
                   3654:     if ( nd_sugarweight ) {
                   3655:         for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
                   3656:             if ( t->sugar2 < msugar ) msugar = t->sugar2;
                   3657:         dm0 = 0; dr0 = 0;
                   3658:         for ( i = 0, t = d; t; t = NEXT(t) )
                   3659:             if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
                   3660:                 if ( dm0 ) NEXT(dm) = t;
                   3661:                 else dm0 = t;
                   3662:                 dm = t;
                   3663:                 i++;
                   3664:             } else {
                   3665:                 if ( dr0 ) NEXT(dr) = t;
                   3666:                 else dr0 = t;
                   3667:                 dr = t;
                   3668:             }
                   3669:     } else {
                   3670:         for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   3671:             if ( SG(t) < msugar ) msugar = SG(t);
                   3672:         dm0 = 0; dr0 = 0;
                   3673:         for ( i = 0, t = d; t; t = NEXT(t) )
                   3674:             if ( i < nd_f4_nsp && SG(t) == msugar ) {
                   3675:                 if ( dm0 ) NEXT(dm) = t;
                   3676:                 else dm0 = t;
                   3677:                 dm = t;
                   3678:                 i++;
                   3679:             } else {
                   3680:                 if ( dr0 ) NEXT(dr) = t;
                   3681:                 else dr0 = t;
                   3682:                 dr = t;
                   3683:             }
                   3684:     }
                   3685:     NEXT(dm) = 0;
                   3686:     if ( dr0 ) NEXT(dr) = 0;
                   3687:     *prest = dr0;
                   3688:     return dm0;
                   3689: }
                   3690:
1.28      noro     3691: ND_pairs nd_minsugarp_s( ND_pairs d, ND_pairs *prest )
                   3692: {
                   3693:   int msugar;
                   3694:   ND_pairs t,last;
                   3695:
                   3696:   for ( msugar = SG(d), t = d; t; t = NEXT(t) )
                   3697:     if ( SG(t) == msugar ) last = t;
                   3698:   *prest = last->next;
                   3699:   last->next = 0;
                   3700:   return d;
                   3701: }
                   3702:
1.1       noro     3703: int nd_tdeg(NDV c)
                   3704: {
                   3705:   int wmax = 0;
                   3706:   int i,len;
                   3707:   NMV a;
                   3708:
                   3709:   len = LEN(c);
                   3710:   for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   3711:     wmax = MAX(TD(DL(a)),wmax);
                   3712:   return wmax;
                   3713: }
                   3714:
1.24      noro     3715: int ndv_newps(int m,NDV a,NDV aq)
1.1       noro     3716: {
                   3717:     int len;
                   3718:     RHist r;
                   3719:     NDV b;
                   3720:     NODE tn;
                   3721:     LIST l;
                   3722:     Z iq;
                   3723:
                   3724:     if ( nd_psn == nd_pslen ) {
                   3725:         nd_pslen *= 2;
                   3726:         nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
                   3727:         nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
                   3728:         nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
                   3729:         nd_bound = (UINT **)
                   3730:             REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
                   3731:         nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
                   3732:         nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
                   3733:     }
                   3734:     NEWRHist(r); nd_psh[nd_psn] = r;
                   3735:     nd_ps[nd_psn] = a;
                   3736:     if ( aq ) {
                   3737:         nd_ps_trace[nd_psn] = aq;
                   3738:         if ( !m ) {
                   3739:           register_hcf(aq);
                   3740:         } else if ( m == -2 ) {
                   3741:           /* do nothing */
                   3742:         } else
                   3743:           error("ndv_newps : invalud modulus");
                   3744:         nd_bound[nd_psn] = ndv_compute_bound(aq);
                   3745: #if 1
                   3746:         SG(r) = SG(aq);
                   3747: #else
                   3748:         SG(r) = nd_tdeg(aq);
                   3749: #endif
                   3750:         ndl_copy(HDL(aq),DL(r));
1.24      noro     3751:         r->sig = dup_sig(aq->sig);
1.1       noro     3752:     } else {
                   3753:         if ( !m ) register_hcf(a);
                   3754:         nd_bound[nd_psn] = ndv_compute_bound(a);
                   3755: #if 1
                   3756:         SG(r) = SG(a);
                   3757: #else
                   3758:         SG(r) = nd_tdeg(a);
                   3759: #endif
                   3760:         ndl_copy(HDL(a),DL(r));
1.24      noro     3761:         r->sig = dup_sig(a->sig);
1.1       noro     3762:     }
                   3763:     if ( nd_demand ) {
                   3764:         if ( aq ) {
                   3765:             ndv_save(nd_ps_trace[nd_psn],nd_psn);
                   3766:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   3767:             nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
                   3768:             nd_ps_trace[nd_psn] = 0;
                   3769:         } else {
                   3770:             ndv_save(nd_ps[nd_psn],nd_psn);
                   3771:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
                   3772:             nd_ps[nd_psn] = 0;
                   3773:         }
                   3774:     }
                   3775:     if ( nd_gentrace ) {
                   3776:         /* reverse the tracelist and append it to alltracelist */
                   3777:         nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
1.6       noro     3778:         STOZ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
1.1       noro     3779:         MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
                   3780:     }
                   3781:     return nd_psn++;
                   3782: }
                   3783:
                   3784: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
                   3785: /* return 1 if success, 0 if failure (HC(a mod p)) */
                   3786:
1.24      noro     3787: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont,int sba)
1.1       noro     3788: {
1.6       noro     3789:   int i,j,td,len,max;
                   3790:   NODE s,s0,f0,tn;
                   3791:   UINT *d;
                   3792:   RHist r;
                   3793:   NDVI w;
                   3794:   NDV a,am;
                   3795:   union oNDC hc;
                   3796:   NODE node;
                   3797:   P hcp;
                   3798:   Z iq,jq;
                   3799:   LIST l;
                   3800:
                   3801:   nd_found = 0; nd_notfirst = 0; nd_create = 0;
                   3802:   /* initialize the tracelist */
                   3803:   nd_tracelist = 0;
                   3804:
                   3805:   for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
                   3806:   w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
                   3807:   for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
                   3808:     if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
                   3809:   if ( !dont_sort ) {
                   3810:     /* XXX heuristic */
1.26      noro     3811:     if ( !sba && !nd_ord->id && (nd_ord->ord.simple<2) )
1.6       noro     3812:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   3813:         (int (*)(const void *,const void *))ndvi_compare_rev);
                   3814:     else
                   3815:       qsort(w,nd_psn,sizeof(struct oNDVI),
                   3816:         (int (*)(const void *,const void *))ndvi_compare);
                   3817:   }
                   3818:   nd_pslen = 2*nd_psn;
                   3819:   nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3820:   nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3821:   nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3822:   nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3823:   nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
                   3824:   nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
                   3825:   nd_hcf = 0;
1.1       noro     3826:
1.6       noro     3827:   if ( trace && nd_vc )
                   3828:     makesubst(nd_vc,&nd_subst);
                   3829:   else
                   3830:     nd_subst = 0;
1.1       noro     3831:
1.6       noro     3832:   if ( !nd_red )
                   3833:     nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   3834:   for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
                   3835:   for ( i = 0; i < nd_psn; i++ ) {
                   3836:     hc = HCU(w[i].p);
                   3837:     if ( trace ) {
                   3838:       if ( mod == -2 ) {
                   3839:         /* over a large finite field */
                   3840:         /* trace = small modulus */
                   3841:         a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
                   3842:         ndv_mod(-2,a);
                   3843:         if ( !dont_removecont) ndv_removecont(-2,a);
                   3844:         am = nd_ps[i] = ndv_dup(trace,w[i].p);
                   3845:         ndv_mod(trace,am);
                   3846:       if ( DL_COMPARE(HDL(am),HDL(a)) )
                   3847:         return 0;
                   3848:         ndv_removecont(trace,am);
                   3849:       } else {
                   3850:         a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
                   3851:         if ( !dont_removecont) ndv_removecont(0,a);
                   3852:         register_hcf(a);
                   3853:         am = nd_ps[i] = ndv_dup(mod,a);
                   3854:         ndv_mod(mod,am);
                   3855:       if ( DL_COMPARE(HDL(am),HDL(a)) )
                   3856:         return 0;
                   3857:         ndv_removecont(mod,am);
                   3858:       }
                   3859:     } else {
                   3860:       a = nd_ps[i] = ndv_dup(mod,w[i].p);
                   3861:       if ( mod || !dont_removecont ) ndv_removecont(mod,a);
                   3862:       if ( !mod ) register_hcf(a);
1.1       noro     3863:     }
1.6       noro     3864:     if ( nd_gentrace ) {
                   3865:       STOZ(i,iq); STOZ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
                   3866:       /* exact division */
1.1       noro     3867:       if ( !dont_removecont )
1.6       noro     3868:         ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
                   3869:       MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
                   3870:     }
                   3871:     NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
                   3872:     nd_bound[i] = ndv_compute_bound(a);
                   3873:     nd_psh[i] = r;
                   3874:     if ( nd_demand ) {
                   3875:       if ( trace ) {
                   3876:         ndv_save(nd_ps_trace[i],i);
                   3877:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   3878:         nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
                   3879:         nd_ps_trace[i] = 0;
                   3880:       } else {
                   3881:         ndv_save(nd_ps[i],i);
                   3882:         nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
                   3883:         nd_ps[i] = 0;
                   3884:       }
1.1       noro     3885:     }
1.6       noro     3886:   }
1.24      noro     3887:   if ( sba ) {
1.27      noro     3888:     nd_sba_hm = (DL *)MALLOC(nd_psn*sizeof(DL));
1.24      noro     3889:    // setup signatures
1.27      noro     3890:     for ( i = 0; i < nd_psn; i++ ) {
                   3891:       SIG sig;
1.24      noro     3892:
1.27      noro     3893:       NEWSIG(sig); sig->pos = i;
                   3894:       nd_ps[i]->sig = sig;
                   3895:       if ( nd_demand ) nd_ps_sym[i]->sig = sig;
                   3896:         nd_psh[i]->sig = sig;
                   3897:       if ( trace ) {
                   3898:         nd_ps_trace[i]->sig = sig;
                   3899:         if ( nd_demand ) nd_ps_trace_sym[i]->sig = sig;
                   3900:       }
                   3901:       NEWDL(nd_sba_hm[i],nd_nvar);
                   3902:       _ndltodl(DL(nd_psh[i]),nd_sba_hm[i]);
                   3903:     }
1.29      noro     3904:     nd_sba_pos = (NODE *)MALLOC(nd_psn*sizeof(NODE));
                   3905:     for ( i = 0; i < nd_psn; i++ ) {
                   3906:       j = nd_psh[i]->sig->pos;
                   3907:       nd_sba_pos[j] = append_one(nd_sba_pos[j],i);
                   3908:     }
1.24      noro     3909:   }
1.6       noro     3910:   if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
                   3911:   return 1;
1.1       noro     3912: }
                   3913:
                   3914: struct order_spec *append_block(struct order_spec *spec,
                   3915:     int nv,int nalg,int ord);
                   3916:
                   3917: extern VECT current_dl_weight_vector_obj;
                   3918: static VECT prev_weight_vector_obj;
                   3919:
                   3920: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
                   3921:     struct order_spec **ord1p,LIST *f1p,NODE *alistp)
                   3922: {
                   3923:     NODE alist,t,s,r0,r,arg;
                   3924:     VL tv;
                   3925:     P poly;
                   3926:     DP d;
                   3927:     Alg alpha,dp;
                   3928:     DAlg inv,da,hc;
                   3929:     MP m;
                   3930:     int i,nvar,nalg,n;
                   3931:     NumberField nf;
                   3932:     LIST f1,f2;
                   3933:     struct order_spec *current_spec;
                   3934:     VECT obj,obj0;
                   3935:     VECT tmp;
                   3936:
                   3937:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
                   3938:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
                   3939:
                   3940:     for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
                   3941:         NEXTNODE(alist,t); MKV(tv->v,poly);
                   3942:         MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
                   3943:         tv->v = tv->v->priv;
                   3944:     }
                   3945:     NEXT(t) = 0;
                   3946:
                   3947:     /* simplification, making polynomials monic */
                   3948:     setfield_dalg(alist);
                   3949:     obj_algtodalg((Obj)f,(Obj *)&f1);
                   3950:     for ( t = BDY(f); t; t = NEXT(t) ) {
                   3951:         initd(ord); ptod(vv,vv,(P)BDY(t),&d);
                   3952:         hc = (DAlg)BDY(d)->c;
                   3953:         if ( NID(hc) == N_DA ) {
                   3954:             invdalg(hc,&inv);
                   3955:             for ( m = BDY(d); m; m = NEXT(m) ) {
                   3956:                 muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
                   3957:             }
                   3958:         }
                   3959:         initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
                   3960:     }
                   3961:     obj_dalgtoalg((Obj)f1,(Obj *)&f);
                   3962:
                   3963:     /* append alg vars to the var list */
                   3964:     for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
                   3965:     NEXT(tv) = av;
                   3966:
                   3967:     /* append a block to ord */
                   3968:     *ord1p = append_block(ord,nvar,nalg,2);
                   3969:
                   3970:     /* create generator list */
                   3971:     nf = get_numberfield();
                   3972:     for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
                   3973:         MKAlg(nf->defpoly[i],dp);
                   3974:         MKNODE(s,dp,t); t = s;
                   3975:     }
                   3976:     MKLIST(f1,t);
                   3977:     *alistp = alist;
                   3978:     algobjtorat((Obj)f1,(Obj *)f1p);
                   3979:
                   3980:     /* creating a new weight vector */
                   3981:     prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
                   3982:     n = nvar+nalg+1;
                   3983:     MKVECT(obj,n);
                   3984:     if ( obj0 && obj0->len == nvar )
                   3985:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
                   3986:     else
                   3987:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
                   3988:     for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
                   3989:     BDY(obj)[n-1] = (pointer)ONE;
                   3990:     arg = mknode(1,obj);
                   3991:     Pdp_set_weight(arg,&tmp);
                   3992: }
                   3993:
                   3994: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
                   3995: {
                   3996:     NODE s,t,u0,u;
                   3997:     P p;
                   3998:     VL tv;
                   3999:     Obj obj;
                   4000:     VECT tmp;
                   4001:     NODE arg;
                   4002:
                   4003:     u0 = 0;
                   4004:     for ( t = r; t; t = NEXT(t) ) {
                   4005:         p = (P)BDY(t);
                   4006:         for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
                   4007:             substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
                   4008:         }
                   4009:         if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
                   4010:             NEXTNODE(u0,u);
                   4011:             BDY(u) = (pointer)p;
                   4012:         }
                   4013:     }
                   4014:     arg = mknode(1,prev_weight_vector_obj);
                   4015:     Pdp_set_weight(arg,&tmp);
                   4016:
                   4017:     return u0;
                   4018: }
                   4019:
                   4020: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
                   4021: {
                   4022:     VL tv,fv,vv,vc,av;
                   4023:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4024:     int e,max,nvar,i;
                   4025:     NDV b;
                   4026:     int ishomo,nalg,mrank,trank,wmax,len;
                   4027:   NMV a;
                   4028:     Alg alpha,dp;
                   4029:     P p,zp;
                   4030:     Q dmy;
                   4031:     LIST f1,f2,zpl;
                   4032:     Obj obj;
                   4033:     NumberField nf;
                   4034:     struct order_spec *ord1;
                   4035:     NODE tr,tl1,tl2,tl3,tl4,nzlist;
                   4036:     LIST l1,l2,l3,l4,l5;
                   4037:   int j;
                   4038:   Z jq,bpe,last_nonzero;
                   4039:     int *perm;
                   4040:     EPOS oepos;
                   4041:     int obpe,oadv,ompos,cbpe;
1.15      noro     4042:     VECT hvect;
1.1       noro     4043:
                   4044:     nd_module = 0;
                   4045:     if ( !m && Demand ) nd_demand = 1;
                   4046:     else nd_demand = 0;
                   4047:     parse_nd_option(current_option);
                   4048:
                   4049:     if ( DP_Multiple )
                   4050:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4051: #if 0
                   4052:     ndv_alloc = 0;
                   4053: #endif
                   4054:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4055:     if ( m && nd_vc )
                   4056:        error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
                   4057:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4058:     switch ( ord->id ) {
                   4059:         case 1:
                   4060:             if ( ord->nv != nvar )
                   4061:                 error("nd_{gr,f4} : invalid order specification");
                   4062:             break;
                   4063:         default:
                   4064:             break;
                   4065:     }
                   4066:     nd_nalg = 0;
                   4067:     av = 0;
                   4068:     if ( !m ) {
                   4069:         get_algtree((Obj)f,&av);
                   4070:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4071:         nd_ntrans = nvar;
                   4072:         nd_nalg = nalg;
                   4073:         /* #i -> t#i */
                   4074:         if ( nalg ) {
                   4075:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4076:             ord = ord1;
                   4077:             f = f1;
                   4078:         }
                   4079:         nvar += nalg;
                   4080:     }
                   4081:     nd_init_ord(ord);
                   4082:     mrank = 0;
                   4083:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4084:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4085:             if ( nd_module ) {
1.16      noro     4086:                 if ( OID(BDY(t)) == O_DPM ) {
                   4087:                   e = dpm_getdeg((DPM)BDY(t),&trank);
                   4088:                   max = MAX(e,max);
                   4089:                   mrank = MAX(mrank,trank);
                   4090:                 } else {
                   4091:                   s = BDY((LIST)BDY(t));
                   4092:                   trank = length(s);
                   4093:                   mrank = MAX(mrank,trank);
                   4094:                   for ( ; s; s = NEXT(s) ) {
                   4095:                       e = getdeg(tv->v,(P)BDY(s));
                   4096:                       max = MAX(e,max);
                   4097:                   }
1.1       noro     4098:                 }
                   4099:             } else {
                   4100:                 e = getdeg(tv->v,(P)BDY(t));
                   4101:                 max = MAX(e,max);
                   4102:             }
                   4103:         }
                   4104:     nd_setup_parameters(nvar,nd_nzlist?0:max);
                   4105:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   4106:     ishomo = 1;
                   4107:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4108:       if ( nd_module ) {
1.16      noro     4109:         if ( OID(BDY(t)) == O_DPM ) {
                   4110:           Z cont;
                   4111:           DPM zdpm;
                   4112:
                   4113:           if ( !m && !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
                   4114:           else zdpm = (DPM)BDY(t);
                   4115:           b = (pointer)dpmtondv(m,zdpm);
                   4116:         } else {
                   4117:           if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   4118:           else zpl = (LIST)BDY(t);
1.1       noro     4119:           b = (pointer)pltondv(CO,vv,zpl);
1.16      noro     4120:         }
1.1       noro     4121:       } else {
                   4122:         if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   4123:         else zp = (P)BDY(t);
                   4124:         b = (pointer)ptondv(CO,vv,zp);
                   4125:       }
                   4126:       if ( ishomo )
                   4127:         ishomo = ishomo && ndv_ishomo(b);
                   4128:       if ( m ) ndv_mod(m,b);
                   4129:       if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4130:     }
                   4131:     if ( fd0 ) NEXT(fd) = 0;
                   4132:
                   4133:   if ( !ishomo && homo ) {
                   4134:         for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   4135:             b = (NDV)BDY(t); len = LEN(b);
                   4136:             for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   4137:                 wmax = MAX(TD(DL(a)),wmax);
                   4138:         }
                   4139:         homogenize_order(ord,nvar,&ord1);
                   4140:         nd_init_ord(ord1);
                   4141:         nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   4142:         for ( t = fd0; t; t = NEXT(t) )
                   4143:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   4144:     }
                   4145:
1.24      noro     4146:     ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0,0);
1.1       noro     4147:     if ( nd_gentrace ) {
                   4148:         MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   4149:     }
                   4150:   if ( nd_splist ) {
                   4151:     *rp = compute_splist();
                   4152:     return;
                   4153:   }
                   4154:   if ( nd_check_splist ) {
                   4155:         if ( f4 ) {
                   4156:             if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
                   4157:             else *rp = 0;
                   4158:         } else {
                   4159:             if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
                   4160:             else *rp = 0;
                   4161:         }
                   4162:     return;
                   4163:   }
                   4164:     x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
                   4165:   if ( !x ) {
                   4166:     *rp = 0; return;
                   4167:   }
1.15      noro     4168:   if ( nd_gentrace ) {
                   4169:     MKVECT(hvect,nd_psn);
                   4170:     for ( i = 0; i < nd_psn; i++ )
                   4171:        ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   4172:   }
1.1       noro     4173:   if ( !ishomo && homo ) {
                   4174:        /* dehomogenization */
                   4175:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   4176:     nd_init_ord(ord);
                   4177:     nd_setup_parameters(nvar,0);
                   4178:   }
                   4179:     nd_demand = 0;
                   4180:   if ( nd_module && nd_intersect ) {
                   4181:     for ( j = nd_psn-1, x = 0; j >= 0; j-- )
1.17      noro     4182:       if ( MPOS(DL(nd_psh[j])) > nd_intersect ) {
1.1       noro     4183:         MKNODE(xx,(pointer)((unsigned long)j),x); x = xx;
                   4184:       }
                   4185:     conv_ilist(nd_demand,0,x,0);
                   4186:     goto FINAL;
                   4187:   }
                   4188:     if ( nd_gentrace  && f4 ) { nzlist = nd_alltracelist; }
                   4189:     x = ndv_reducebase(x,perm);
                   4190:     if ( nd_gentrace  && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
                   4191:     x = ndv_reduceall(m,x);
                   4192:     cbpe = nd_bpe;
                   4193:     if ( nd_gentrace && !f4 ) {
                   4194:         tl2 = nd_alltracelist; nd_alltracelist = 0;
                   4195:         ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
                   4196:         tl3 = nd_alltracelist; nd_alltracelist = 0;
                   4197:         if ( nd_gensyz ) {
                   4198:           nd_gb(m,0,1,1,0);
                   4199:             tl4 = nd_alltracelist; nd_alltracelist = 0;
                   4200:         } else tl4 = 0;
                   4201:     }
                   4202:     nd_bpe = cbpe;
                   4203:     nd_setup_parameters(nd_nvar,0);
                   4204: FINAL:
                   4205:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.16      noro     4206:       NEXTNODE(r0,r);
                   4207:       if ( nd_module ) {
                   4208:         if ( retdp ) BDY(r) = ndvtodpm(m,BDY(t));
                   4209:         else BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
                   4210:       } else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
                   4211:       else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.1       noro     4212:     }
                   4213:     if ( r0 ) NEXT(r) = 0;
                   4214:     if ( !m && nd_nalg )
                   4215:         r0 = postprocess_algcoef(av,alist,r0);
                   4216:     MKLIST(*rp,r0);
                   4217:     if ( nd_gentrace ) {
                   4218:   if ( f4 ) {
1.6       noro     4219:             STOZ(16,bpe);
                   4220:             STOZ(nd_last_nonzero,last_nonzero);
1.15      noro     4221:             tr = mknode(6,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero,hvect); MKLIST(*rp,tr);
1.1       noro     4222:         } else {
                   4223:             tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   4224:             tl3 = reverse_node(tl3);
                   4225:             /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   4226:             for ( t = tl2; t; t = NEXT(t) ) {
                   4227:             /* s = [i,[*,j,*,*],...] */
                   4228:                 s = BDY((LIST)BDY(t));
1.6       noro     4229:                 j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     4230:                 for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     4231:                     j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     4232:                     ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   4233:                 }
                   4234:             }
                   4235:             for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
1.6       noro     4236:                 STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     4237:             }
                   4238:             MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   4239:             MKLIST(l5,tl4);
1.6       noro     4240:             STOZ(nd_bpe,bpe);
1.15      noro     4241:             tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     4242:         }
                   4243:     }
                   4244: #if 0
                   4245:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   4246: #endif
                   4247: }
                   4248:
1.28      noro     4249: NODE nd_sba_f4(int m,int **indp);
                   4250:
                   4251: void nd_sba(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.24      noro     4252: {
                   4253:   VL tv,fv,vv,vc,av;
                   4254:   NODE fd,fd0,r,r0,t,x,s,xx;
                   4255:   int e,max,nvar,i;
                   4256:   NDV b;
                   4257:   int ishomo,nalg,wmax,len;
                   4258:   NMV a;
                   4259:   P p,zp;
                   4260:   Q dmy;
                   4261:   struct order_spec *ord1;
                   4262:   int j;
                   4263:   int *perm;
                   4264:   EPOS oepos;
                   4265:   int obpe,oadv,ompos,cbpe;
1.29      noro     4266:   struct oEGT eg0,eg1,egconv;
1.24      noro     4267:
                   4268:   nd_module = 0;
                   4269:   nd_demand = 0;
                   4270:   parse_nd_option(current_option);
1.35      noro     4271:   Nsamesig = 0;
1.24      noro     4272:   if ( DP_Multiple )
                   4273:     nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4274:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4275:   if ( m && nd_vc )
                   4276:     error("nd_sba : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
                   4277:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4278:   switch ( ord->id ) {
                   4279:     case 1:
                   4280:       if ( ord->nv != nvar )
                   4281:         error("nd_sba : invalid order specification");
                   4282:         break;
                   4283:       default:
                   4284:         break;
                   4285:   }
                   4286:   nd_nalg = 0;
                   4287:   nd_init_ord(ord);
                   4288:   // for SIG comparison
                   4289:   initd(ord);
                   4290:   for ( t = BDY(f), max = 1; t; t = NEXT(t) ) {
                   4291:     for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4292:       e = getdeg(tv->v,(P)BDY(t));
                   4293:       max = MAX(e,max);
                   4294:     }
                   4295:   }
1.25      noro     4296:   nd_setup_parameters(nvar,max);
1.24      noro     4297:   obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   4298:   ishomo = 1;
                   4299:   for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4300:     if ( !m ) ptozp((P)BDY(t),1,&dmy,&zp);
                   4301:     else zp = (P)BDY(t);
                   4302:     b = (pointer)ptondv(CO,vv,zp);
                   4303:     if ( ishomo )
                   4304:       ishomo = ishomo && ndv_ishomo(b);
                   4305:     if ( m ) ndv_mod(m,b);
                   4306:     if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4307:   }
                   4308:   if ( fd0 ) NEXT(fd) = 0;
                   4309:
                   4310:   if ( !ishomo && homo ) {
                   4311:     for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   4312:       b = (NDV)BDY(t); len = LEN(b);
                   4313:       for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   4314:         wmax = MAX(TD(DL(a)),wmax);
                   4315:       }
                   4316:       homogenize_order(ord,nvar,&ord1);
                   4317:       nd_init_ord(ord1);
                   4318:       nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
                   4319:       for ( t = fd0; t; t = NEXT(t) )
                   4320:         ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   4321:   }
                   4322:
                   4323:   ndv_setup(m,0,fd0,0,0,1);
1.28      noro     4324:   x = f4 ? nd_sba_f4(m,&perm) : nd_sba_buch(m,ishomo || homo,&perm);
1.24      noro     4325:   if ( !x ) {
                   4326:     *rp = 0; return;
                   4327:   }
                   4328:   if ( !ishomo && homo ) {
                   4329:        /* dehomogenization */
                   4330:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   4331:     nd_init_ord(ord);
                   4332:     nd_setup_parameters(nvar,0);
                   4333:   }
                   4334:   nd_demand = 0;
                   4335:   x = ndv_reducebase(x,perm);
                   4336:   x = ndv_reduceall(m,x);
                   4337:   nd_setup_parameters(nd_nvar,0);
1.29      noro     4338:   get_eg(&eg0);
1.24      noro     4339:   for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   4340:     NEXTNODE(r0,r);
                   4341:     if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
1.34      noro     4342:     else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.24      noro     4343:   }
                   4344:   if ( r0 ) NEXT(r) = 0;
                   4345:   MKLIST(*rp,r0);
1.29      noro     4346:   get_eg(&eg1); init_eg(&egconv); add_eg(&egconv,&eg0,&eg1);
                   4347:   print_eg("conv",&egconv); fprintf(asir_out,"\n");
1.24      noro     4348: }
                   4349:
1.1       noro     4350: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
                   4351: {
                   4352:     VL tv,fv,vv,vc,av;
                   4353:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4354:     int e,max,nvar,i;
                   4355:     NDV b;
                   4356:     int ishomo,nalg;
                   4357:     Alg alpha,dp;
                   4358:     P p,zp;
                   4359:     Q dmy;
                   4360:     LIST f1,f2;
                   4361:     Obj obj;
                   4362:     NumberField nf;
                   4363:     struct order_spec *ord1;
                   4364:     int *perm;
                   4365:
                   4366:     parse_nd_option(current_option);
                   4367:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4368:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4369:     switch ( ord->id ) {
                   4370:         case 1:
                   4371:             if ( ord->nv != nvar )
                   4372:                 error("nd_check : invalid order specification");
                   4373:             break;
                   4374:         default:
                   4375:             break;
                   4376:     }
                   4377:     nd_nalg = 0;
                   4378:     av = 0;
                   4379:     if ( !m ) {
                   4380:         get_algtree((Obj)f,&av);
                   4381:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4382:         nd_ntrans = nvar;
                   4383:         nd_nalg = nalg;
                   4384:         /* #i -> t#i */
                   4385:         if ( nalg ) {
                   4386:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4387:             ord = ord1;
                   4388:             f = f1;
                   4389:         }
                   4390:         nvar += nalg;
                   4391:     }
                   4392:     nd_init_ord(ord);
                   4393:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4394:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4395:             e = getdeg(tv->v,(P)BDY(t));
                   4396:             max = MAX(e,max);
                   4397:         }
                   4398:     nd_setup_parameters(nvar,max);
                   4399:     ishomo = 1;
                   4400:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4401:         ptozp((P)BDY(t),1,&dmy,&zp);
                   4402:         b = (pointer)ptondv(CO,vv,zp);
                   4403:         if ( ishomo )
                   4404:             ishomo = ishomo && ndv_ishomo(b);
                   4405:         if ( m ) ndv_mod(m,b);
                   4406:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   4407:     }
                   4408:     if ( fd0 ) NEXT(fd) = 0;
1.24      noro     4409:     ndv_setup(m,0,fd0,0,1,0);
1.1       noro     4410:     for ( x = 0, i = 0; i < nd_psn; i++ )
                   4411:         x = update_base(x,i);
                   4412:     if ( do_check ) {
                   4413:         x = nd_gb(m,ishomo,1,0,&perm);
                   4414:         if ( !x ) {
                   4415:             *rp = 0;
                   4416:             return;
                   4417:         }
                   4418:     } else {
                   4419: #if 0
                   4420:     /* bug ? */
                   4421:         for ( t = x; t; t = NEXT(t) )
                   4422:             BDY(t) = (pointer)nd_ps[(long)BDY(t)];
                   4423: #else
                   4424:     conv_ilist(0,0,x,&perm);
                   4425: #endif
                   4426:     }
                   4427:     x = ndv_reducebase(x,perm);
                   4428:     x = ndv_reduceall(m,x);
                   4429:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   4430:         NEXTNODE(r0,r);
                   4431:         BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   4432:     }
                   4433:     if ( r0 ) NEXT(r) = 0;
                   4434:     if ( !m && nd_nalg )
                   4435:         r0 = postprocess_algcoef(av,alist,r0);
                   4436:     MKLIST(*rp,r0);
                   4437: }
                   4438:
                   4439: NDV recompute_trace(NODE trace,NDV *p,int m);
                   4440: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
                   4441:
                   4442: NDV recompute_trace(NODE ti,NDV *p,int mod)
                   4443: {
                   4444:   int c,c1,c2,i;
                   4445:   NM mul,m,tail;
                   4446:   ND d,r,rm;
                   4447:   NODE sj;
                   4448:   NDV red;
                   4449:   Obj mj;
                   4450:
                   4451:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   4452:   CM(mul) = 1;
                   4453:   tail = 0;
                   4454:   for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
                   4455:     sj = BDY((LIST)BDY(ti));
                   4456:     if ( ARG0(sj) ) {
1.6       noro     4457:       red = p[ZTOS((Q)ARG1(sj))];
1.1       noro     4458:       mj = (Obj)ARG2(sj);
                   4459:       if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
                   4460:       else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
                   4461:       rm = ndv_mul_nm(mod,mul,red);
                   4462:       if ( !r ) r = rm;
                   4463:       else {
                   4464:         for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
                   4465:           if ( d ) {
                   4466:             NEXT(tail) = m; tail = m; LEN(d)++;
                   4467:           } else {
                   4468:             MKND(nd_nvar,m,1,d); tail = BDY(d);
                   4469:           }
                   4470:         }
                   4471:         if ( !m ) return 0; /* failure */
                   4472:                 else {
                   4473:           BDY(r) = m;
                   4474:                     if ( mod > 0 || mod == -1 ) {
                   4475:             c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
                   4476:             DMAR(c1,c2,0,mod,c);
                   4477:             nd_mul_c(mod,rm,c);
                   4478:                     } else {
                   4479:                       Z t,u;
                   4480:
                   4481:                       chsgnlf(HCZ(r),&t);
                   4482:                       divlf(t,HCZ(rm),&u);
                   4483:                       nd_mul_c_lf(rm,u);
                   4484:                     }
                   4485:           r = nd_add(mod,r,rm);
                   4486:         }
                   4487:           }
                   4488:      }
                   4489:   }
                   4490:   if ( tail ) NEXT(tail) = 0;
                   4491:   d = nd_add(mod,d,r);
                   4492:   nd_mul_c(mod,d,invm(HCM(d),mod));
                   4493:   return ndtondv(mod,d);
                   4494: }
                   4495:
                   4496: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
                   4497: {
                   4498:     VL tv,fv,vv,vc,av;
                   4499:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   4500:     int e,max,nvar,i;
                   4501:     NDV b;
                   4502:     int ishomo,nalg;
                   4503:     Alg alpha,dp;
                   4504:     P p,zp;
                   4505:     Q dmy;
                   4506:     LIST f1,f2;
                   4507:     Obj obj;
                   4508:     NumberField nf;
                   4509:     struct order_spec *ord1;
                   4510:   NODE permtrace,intred,ind,perm,trace,ti;
                   4511:   int len,n,j;
                   4512:   NDV *db,*pb;
                   4513:
                   4514:     parse_nd_option(current_option);
                   4515:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4516:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4517:     switch ( ord->id ) {
                   4518:         case 1:
                   4519:             if ( ord->nv != nvar )
                   4520:                 error("nd_check : invalid order specification");
                   4521:             break;
                   4522:         default:
                   4523:             break;
                   4524:     }
                   4525:     nd_init_ord(ord);
1.6       noro     4526:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     4527:     nd_setup_parameters(nvar,0);
                   4528:
                   4529:   len = length(BDY(f));
                   4530:   db = (NDV *)MALLOC(len*sizeof(NDV *));
                   4531:   for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
                   4532:       ptozp((P)BDY(t),1,&dmy,&zp);
                   4533:       b = ptondv(CO,vv,zp);
                   4534:         ndv_mod(m,b);
                   4535:     ndv_mul_c(m,b,invm(HCM(b),m));
                   4536:     db[i] = b;
                   4537:     }
                   4538:
                   4539:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   4540:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   4541:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   4542:   perm = BDY((LIST)ARG0(permtrace));
                   4543:   trace = NEXT(permtrace);
                   4544:
                   4545:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     4546:     j = ZTOS((Q)ARG0(BDY((LIST)BDY(t))));
1.1       noro     4547:     if ( j > i ) i = j;
                   4548:   }
                   4549:   n = i+1;
                   4550:   pb = (NDV *)MALLOC(n*sizeof(NDV *));
                   4551:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   4552:     ti = BDY((LIST)BDY(t));
1.6       noro     4553:     pb[ZTOS((Q)ARG0(ti))] = db[ZTOS((Q)ARG1(ti))];
1.1       noro     4554:   }
                   4555:   for ( t = trace; t; t = NEXT(t) ) {
                   4556:     ti = BDY((LIST)BDY(t));
1.6       noro     4557:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   4558:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     4559:       if ( DP_Print ) {
                   4560:            fprintf(asir_out,"."); fflush(asir_out);
                   4561:       }
                   4562:   }
                   4563:   for ( t = intred; t; t = NEXT(t) ) {
                   4564:     ti = BDY((LIST)BDY(t));
1.6       noro     4565:     pb[ZTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   4566:     if ( !pb[ZTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
1.1       noro     4567:       if ( DP_Print ) {
                   4568:            fprintf(asir_out,"*"); fflush(asir_out);
                   4569:       }
                   4570:   }
                   4571:     for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
                   4572:         NEXTNODE(r0,r);
1.6       noro     4573:     b = pb[ZTOS((Q)BDY(t))];
1.1       noro     4574:         ndv_mul_c(m,b,invm(HCM(b),m));
                   4575: #if 0
1.6       noro     4576:         BDY(r) = ndvtop(m,CO,vv,pb[ZTOS((Q)BDY(t))]);
1.1       noro     4577: #else
1.6       noro     4578:         BDY(r) = ndvtodp(m,pb[ZTOS((Q)BDY(t))]);
1.1       noro     4579: #endif
                   4580:     }
                   4581:     if ( r0 ) NEXT(r) = 0;
                   4582:     MKLIST(*rp,r0);
                   4583:     if ( DP_Print ) fprintf(asir_out,"\n");
                   4584: }
                   4585:
1.16      noro     4586: 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     4587: {
                   4588:     VL tv,fv,vv,vc,av;
                   4589:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   4590:     int m,nocheck,nvar,mindex,e,max;
                   4591:     NDV c;
                   4592:     NMV a;
                   4593:     P p,zp;
                   4594:     Q dmy;
                   4595:     EPOS oepos;
                   4596:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   4597:     Alg alpha,dp;
                   4598:     P poly;
                   4599:     LIST f1,f2,zpl;
                   4600:     Obj obj;
                   4601:     NumberField nf;
                   4602:     struct order_spec *ord1;
                   4603:     struct oEGT eg_check,eg0,eg1;
                   4604:     NODE tr,tl1,tl2,tl3,tl4;
                   4605:     LIST l1,l2,l3,l4,l5;
                   4606:     int *perm;
                   4607:     int j,ret;
                   4608:     Z jq,bpe;
1.15      noro     4609:     VECT hvect;
1.1       noro     4610:
                   4611:     nd_module = 0;
                   4612:     nd_lf = 0;
                   4613:     parse_nd_option(current_option);
                   4614:     if ( nd_lf ) {
                   4615:       if ( f4 )
                   4616:         nd_f4_lf_trace(f,v,trace,homo,ord,rp);
                   4617:       else
                   4618:         error("nd_gr_trace is not implemented yet over a large finite field");
                   4619:       return;
                   4620:     }
                   4621:     if ( DP_Multiple )
                   4622:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   4623:
                   4624:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   4625:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   4626:     switch ( ord->id ) {
                   4627:         case 1:
                   4628:             if ( ord->nv != nvar )
                   4629:                 error("nd_gr_trace : invalid order specification");
                   4630:             break;
                   4631:         default:
                   4632:             break;
                   4633:     }
                   4634:
                   4635:     get_algtree((Obj)f,&av);
                   4636:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   4637:     nd_ntrans = nvar;
                   4638:     nd_nalg = nalg;
                   4639:     /* #i -> t#i */
                   4640:     if ( nalg ) {
                   4641:         preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   4642:         ord = ord1;
                   4643:         f = f1;
                   4644:     }
                   4645:     nvar += nalg;
                   4646:
                   4647:     nocheck = 0;
                   4648:     mindex = 0;
                   4649:
                   4650:     if ( Demand ) nd_demand = 1;
                   4651:     else nd_demand = 0;
                   4652:
                   4653:     /* setup modulus */
                   4654:     if ( trace < 0 ) {
                   4655:         trace = -trace;
                   4656:         nocheck = 1;
                   4657:     }
                   4658:     m = trace > 1 ? trace : get_lprime(mindex);
                   4659:     nd_init_ord(ord);
                   4660:     mrank = 0;
                   4661:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   4662:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   4663:             if ( nd_module ) {
1.16      noro     4664:               if ( OID(BDY(t)) == O_DPM ) {
                   4665:                 e = dpm_getdeg((DPM)BDY(t),&trank);
                   4666:                 max = MAX(e,max);
                   4667:                 mrank = MAX(mrank,trank);
                   4668:               } else {
1.1       noro     4669:                 s = BDY((LIST)BDY(t));
                   4670:                 trank = length(s);
                   4671:                 mrank = MAX(mrank,trank);
                   4672:                 for ( ; s; s = NEXT(s) ) {
                   4673:                     e = getdeg(tv->v,(P)BDY(s));
                   4674:                     max = MAX(e,max);
                   4675:                 }
1.16      noro     4676:               }
1.1       noro     4677:             } else {
                   4678:                 e = getdeg(tv->v,(P)BDY(t));
                   4679:                 max = MAX(e,max);
                   4680:             }
                   4681:         }
                   4682:     nd_setup_parameters(nvar,max);
                   4683:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   4684:     ishomo = 1;
                   4685:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   4686:         if ( nd_module ) {
1.16      noro     4687:           if ( OID(BDY(t)) == O_DPM ) {
                   4688:             Z cont;
                   4689:             DPM zdpm;
                   4690:
1.17      noro     4691:             if ( !nd_gentrace ) dpm_ptozp((DPM)BDY(t),&cont,&zdpm);
1.16      noro     4692:             else zdpm = (DPM)BDY(t);
                   4693:             c = (pointer)dpmtondv(m,zdpm);
                   4694:           } else {
                   4695:             if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   4696:             else zpl = (LIST)BDY(t);
1.1       noro     4697:             c = (pointer)pltondv(CO,vv,zpl);
1.16      noro     4698:           }
1.1       noro     4699:         } else {
1.16      noro     4700:           if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   4701:           else zp = (P)BDY(t);
                   4702:           c = (pointer)ptondv(CO,vv,zp);
1.1       noro     4703:         }
                   4704:         if ( ishomo )
                   4705:             ishomo = ishomo && ndv_ishomo(c);
                   4706:         if ( c ) {
                   4707:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   4708:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   4709:         }
                   4710:     }
                   4711:     if ( in0 ) NEXT(in) = 0;
                   4712:     if ( fd0 ) NEXT(fd) = 0;
                   4713:     if ( !ishomo && homo ) {
                   4714:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   4715:             c = (NDV)BDY(t); len = LEN(c);
                   4716:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   4717:                 wmax = MAX(TD(DL(a)),wmax);
                   4718:         }
                   4719:         homogenize_order(ord,nvar,&ord1);
                   4720:         nd_init_ord(ord1);
                   4721:         nd_setup_parameters(nvar+1,wmax);
                   4722:         for ( t = fd0; t; t = NEXT(t) )
                   4723:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   4724:     }
                   4725:     if ( MaxDeg > 0 ) nocheck = 1;
                   4726:     while ( 1 ) {
                   4727:     tl1 = tl2 = tl3 = tl4 = 0;
                   4728:         if ( Demand )
                   4729:             nd_demand = 1;
1.24      noro     4730:         ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0,0);
1.1       noro     4731:         if ( nd_gentrace ) {
                   4732:             MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   4733:         }
                   4734:         if ( ret )
                   4735:             cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
                   4736:         if ( !ret || !cand ) {
                   4737:             /* failure */
                   4738:             if ( trace > 1 ) { *rp = 0; return; }
                   4739:             else m = get_lprime(++mindex);
                   4740:             continue;
                   4741:         }
1.15      noro     4742:         if ( nd_gentrace ) {
                   4743:           MKVECT(hvect,nd_psn);
                   4744:           for ( i = 0; i < nd_psn; i++ )
                   4745:              ndltodp(nd_psh[i]->dl,(DP *)&BDY(hvect)[i]);
                   4746:         }
1.1       noro     4747:         if ( !ishomo && homo ) {
                   4748:             /* dehomogenization */
                   4749:             for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   4750:             nd_init_ord(ord);
                   4751:             nd_setup_parameters(nvar,0);
                   4752:         }
                   4753:         nd_demand = 0;
                   4754:         cand = ndv_reducebase(cand,perm);
                   4755:         if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
                   4756:         cand = ndv_reduceall(0,cand);
                   4757:         cbpe = nd_bpe;
                   4758:         if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
                   4759:         get_eg(&eg0);
                   4760:         if ( nocheck )
                   4761:             break;
                   4762:         if ( (ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   4763:             if ( nd_gentrace ) {
                   4764:           tl3 = nd_alltracelist; nd_alltracelist = 0;
                   4765:         } else tl3 = 0;
                   4766:             /* gbcheck : cand is a GB of Id(cand) ? */
                   4767:             if ( nd_vc || nd_gentrace || nd_gensyz )
                   4768:               ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
                   4769:             else
                   4770:               ret = nd_f4(0,1,0)!=0;
                   4771:             if ( nd_gentrace && nd_gensyz ) {
                   4772:           tl4 = nd_alltracelist; nd_alltracelist = 0;
                   4773:         } else tl4 = 0;
                   4774:     }
                   4775:     if ( ret ) break;
                   4776:         else if ( trace > 1 ) {
                   4777:             /* failure */
                   4778:             *rp = 0; return;
                   4779:         } else {
                   4780:             /* try the next modulus */
                   4781:             m = get_lprime(++mindex);
                   4782:             /* reset the parameters */
                   4783:             if ( !ishomo && homo ) {
                   4784:                 nd_init_ord(ord1);
                   4785:                 nd_setup_parameters(nvar+1,wmax);
                   4786:             } else {
                   4787:                 nd_init_ord(ord);
                   4788:                 nd_setup_parameters(nvar,max);
                   4789:             }
                   4790:         }
                   4791:     }
                   4792:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   4793:     if ( DP_Print )
1.6       noro     4794:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     4795:     /* dp->p */
                   4796:     nd_bpe = cbpe;
                   4797:     nd_setup_parameters(nd_nvar,0);
                   4798:     for ( r = cand; r; r = NEXT(r) ) {
1.16      noro     4799:       if ( nd_module ) {
1.17      noro     4800:         if ( retdp ) BDY(r) = ndvtodpm(0,BDY(r));
1.16      noro     4801:         else BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.17      noro     4802:       } else if ( retdp ) BDY(r) = ndvtodp(0,BDY(r));
                   4803:       else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.1       noro     4804:     }
                   4805:     if ( nd_nalg )
                   4806:         cand = postprocess_algcoef(av,alist,cand);
                   4807:     MKLIST(*rp,cand);
                   4808:     if ( nd_gentrace ) {
                   4809:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   4810:     tl3 = reverse_node(tl3);
                   4811:     /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   4812:         for ( t = tl2; t; t = NEXT(t) ) {
                   4813:       /* s = [i,[*,j,*,*],...] */
                   4814:             s = BDY((LIST)BDY(t));
1.6       noro     4815:             j = perm[ZTOS((Q)ARG0(s))]; STOZ(j,jq); ARG0(s) = (pointer)jq;
1.1       noro     4816:       for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.6       noro     4817:                 j = perm[ZTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOZ(j,jq);
1.1       noro     4818:         ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   4819:             }
                   4820:     }
                   4821:     for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
1.6       noro     4822:         STOZ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.1       noro     4823:     }
                   4824:         MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   4825:     MKLIST(l5,tl4);
1.6       noro     4826:       STOZ(nd_bpe,bpe);
1.15      noro     4827:         tr = mknode(9,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe,hvect); MKLIST(*rp,tr);
1.1       noro     4828:     }
                   4829: }
                   4830:
                   4831: /* XXX : module element is not considered  */
                   4832:
                   4833: void dltondl(int n,DL dl,UINT *r)
                   4834: {
                   4835:     UINT *d;
                   4836:     int i,j,l,s,ord_l;
                   4837:     struct order_pair *op;
                   4838:
                   4839:     d = (unsigned int *)dl->d;
                   4840:     for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
                   4841:     if ( nd_blockmask ) {
                   4842:         l = nd_blockmask->n;
                   4843:         op = nd_blockmask->order_pair;
                   4844:         for ( j = 0, s = 0; j < l; j++ ) {
                   4845:             ord_l = op[j].length;
                   4846:             for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
                   4847:         }
                   4848:         TD(r) = ndl_weight(r);
                   4849:         ndl_weight_mask(r);
                   4850:     } else {
                   4851:         for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
                   4852:         TD(r) = ndl_weight(r);
                   4853:     }
                   4854: }
                   4855:
                   4856: DL ndltodl(int n,UINT *ndl)
                   4857: {
                   4858:     DL dl;
                   4859:     int *d;
                   4860:     int i,j,l,s,ord_l;
                   4861:     struct order_pair *op;
                   4862:
1.33      noro     4863:     NEWDL_NOINIT(dl,n);
1.1       noro     4864:     dl->td = TD(ndl);
                   4865:     d = dl->d;
                   4866:     if ( nd_blockmask ) {
                   4867:         l = nd_blockmask->n;
                   4868:         op = nd_blockmask->order_pair;
                   4869:         for ( j = 0, s = 0; j < l; j++ ) {
                   4870:             ord_l = op[j].length;
                   4871:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   4872:         }
                   4873:     } else {
                   4874:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   4875:     }
                   4876:     return dl;
                   4877: }
                   4878:
1.24      noro     4879: void _ndltodl(UINT *ndl,DL dl)
                   4880: {
                   4881:     int *d;
                   4882:     int i,j,l,s,ord_l,n;
                   4883:     struct order_pair *op;
                   4884:
                   4885:     n = nd_nvar;
                   4886:     dl->td = TD(ndl);
                   4887:     d = dl->d;
                   4888:     if ( nd_blockmask ) {
                   4889:         l = nd_blockmask->n;
                   4890:         op = nd_blockmask->order_pair;
                   4891:         for ( j = 0, s = 0; j < l; j++ ) {
                   4892:             ord_l = op[j].length;
                   4893:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   4894:         }
                   4895:     } else {
                   4896:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   4897:     }
                   4898: }
                   4899:
1.1       noro     4900: void nmtodp(int mod,NM m,DP *r)
                   4901: {
                   4902:     DP dp;
                   4903:     MP mr;
                   4904:
                   4905:     NEWMP(mr);
                   4906:     mr->dl = ndltodl(nd_nvar,DL(m));
                   4907:     mr->c = (Obj)ndctop(mod,m->c);
                   4908:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   4909:     *r = dp;
                   4910: }
                   4911:
1.15      noro     4912: void ndltodp(UINT *d,DP *r)
                   4913: {
                   4914:     DP dp;
                   4915:     MP mr;
                   4916:
                   4917:     NEWMP(mr);
                   4918:     mr->dl = ndltodl(nd_nvar,d);
                   4919:     mr->c = (Obj)ONE;
                   4920:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   4921:     *r = dp;
                   4922: }
                   4923:
1.1       noro     4924: void ndl_print(UINT *dl)
                   4925: {
                   4926:     int n;
                   4927:     int i,j,l,ord_l,s,s0;
                   4928:     struct order_pair *op;
                   4929:
                   4930:     n = nd_nvar;
                   4931:     printf("<<");
                   4932:     if ( nd_blockmask ) {
                   4933:         l = nd_blockmask->n;
                   4934:         op = nd_blockmask->order_pair;
                   4935:         for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   4936:             ord_l = op[j].length;
                   4937:             for ( i = 0; i < ord_l; i++, s++ )
                   4938:                 printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
                   4939:         }
                   4940:     } else {
                   4941:         for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
                   4942:     }
                   4943:     printf(">>");
                   4944:     if ( nd_module && MPOS(dl) )
                   4945:         printf("*e%d",MPOS(dl));
                   4946: }
                   4947:
                   4948: void nd_print(ND p)
                   4949: {
                   4950:     NM m;
                   4951:
                   4952:     if ( !p )
                   4953:         printf("0\n");
                   4954:     else {
                   4955:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   4956:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     4957:             else printf("+%ld*",CM(m));
1.1       noro     4958:             ndl_print(DL(m));
                   4959:         }
                   4960:         printf("\n");
                   4961:     }
                   4962: }
                   4963:
                   4964: void nd_print_q(ND p)
                   4965: {
                   4966:     NM m;
                   4967:
                   4968:     if ( !p )
                   4969:         printf("0\n");
                   4970:     else {
                   4971:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   4972:             printf("+");
1.6       noro     4973:             printexpr(CO,(Obj)CZ(m));
1.1       noro     4974:             printf("*");
                   4975:             ndl_print(DL(m));
                   4976:         }
                   4977:         printf("\n");
                   4978:     }
                   4979: }
                   4980:
                   4981: void ndp_print(ND_pairs d)
                   4982: {
                   4983:     ND_pairs t;
                   4984:
                   4985:     for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
                   4986:     printf("\n");
                   4987: }
                   4988:
                   4989: void nd_removecont(int mod,ND p)
                   4990: {
                   4991:     int i,n;
                   4992:     Z *w;
                   4993:     NM m;
                   4994:     struct oVECT v;
                   4995:
                   4996:     if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
                   4997:     else if ( mod == -2 ) {
                   4998:       Z inv;
                   4999:       divlf(ONE,HCZ(p),&inv);
                   5000:       nd_mul_c_lf(p,inv);
                   5001:     } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
                   5002:     else {
                   5003:         for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   5004:         w = (Z *)MALLOC(n*sizeof(Q));
                   5005:         v.len = n;
                   5006:         v.body = (pointer *)w;
1.6       noro     5007:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5008:         removecont_array((P *)w,n,1);
1.6       noro     5009:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5010:     }
                   5011: }
                   5012:
                   5013: void nd_removecont2(ND p1,ND p2)
                   5014: {
                   5015:     int i,n1,n2,n;
                   5016:     Z *w;
                   5017:     NM m;
                   5018:     struct oVECT v;
                   5019:
                   5020:     n1 = nd_length(p1);
                   5021:     n2 = nd_length(p2);
                   5022:     n = n1+n2;
                   5023:     w = (Z *)MALLOC(n*sizeof(Q));
                   5024:     v.len = n;
                   5025:     v.body = (pointer *)w;
                   5026:     i = 0;
                   5027:     if ( p1 )
1.6       noro     5028:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5029:     if ( p2 )
1.6       noro     5030:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CZ(m);
1.1       noro     5031:     removecont_array((P *)w,n,1);
                   5032:     i = 0;
                   5033:     if ( p1 )
1.6       noro     5034:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5035:     if ( p2 )
1.6       noro     5036:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CZ(m) = w[i];
1.1       noro     5037: }
                   5038:
                   5039: void ndv_removecont(int mod,NDV p)
                   5040: {
                   5041:     int i,len,all_p;
                   5042:     Z *c;
                   5043:     P *w;
                   5044:     Z dvr,t;
                   5045:     P g,cont,tp;
                   5046:     NMV m;
                   5047:
                   5048:     if ( mod == -1 )
                   5049:         ndv_mul_c(mod,p,_invsf(HCM(p)));
                   5050:     else if ( mod == -2 ) {
                   5051:       Z inv;
                   5052:       divlf(ONE,HCZ(p),&inv);
                   5053:       ndv_mul_c_lf(p,inv);
                   5054:     } else if ( mod )
                   5055:         ndv_mul_c(mod,p,invm(HCM(p),mod));
                   5056:     else {
                   5057:         len = p->len;
                   5058:         w = (P *)MALLOC(len*sizeof(P));
                   5059:         c = (Z *)MALLOC(len*sizeof(Q));
                   5060:         for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
                   5061:             ptozp(CP(m),1,(Q *)&c[i],&w[i]);
                   5062:             all_p = all_p && !NUM(w[i]);
                   5063:         }
                   5064:         if ( all_p ) {
                   5065:             qltozl((Q *)c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
                   5066:             mulp(nd_vc,(P)dvr,g,&cont);
                   5067:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5068:                 divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
                   5069:             }
                   5070:         } else {
                   5071:             sortbynm((Q *)c,len);
                   5072:             qltozl((Q *)c,len,&dvr);
                   5073:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   5074:                 divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
                   5075:             }
                   5076:         }
                   5077:     }
                   5078: }
                   5079:
                   5080: /* koko */
                   5081:
                   5082: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
                   5083: {
                   5084:     int len,i,max;
                   5085:     NMV m,mr0,mr,t;
                   5086:
                   5087:     len = p->len;
1.14      noro     5088:     for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.1       noro     5089:         max = MAX(max,TD(DL(m)));
                   5090:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   5091:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   5092:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   5093:     t = (NMV)MALLOC(nmv_adv);
                   5094:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   5095:         ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.6       noro     5096:         CZ(mr) = CZ(m);
1.1       noro     5097:         ndl_copy(DL(t),DL(mr));
                   5098:     }
                   5099:     NV(p)++;
                   5100:     BDY(p) = mr0;
                   5101: }
                   5102:
                   5103: void ndv_dehomogenize(NDV p,struct order_spec *ord)
                   5104: {
                   5105:     int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
                   5106:     int pos;
                   5107:     Q *w;
                   5108:     Q dvr,t;
                   5109:     NMV m,r;
                   5110:
                   5111:     len = p->len;
                   5112:     newnvar = nd_nvar-1;
                   5113:     newexporigin = nd_get_exporigin(ord);
                   5114:     if ( nd_module ) newmpos = newexporigin-1;
                   5115:     newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
                   5116:     for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
                   5117:         ndl_dehomogenize(DL(m));
                   5118:     if ( newwpd != nd_wpd ) {
                   5119:         newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
                   5120:         for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.6       noro     5121:             CZ(r) = CZ(m);
1.1       noro     5122:             if ( nd_module ) pos = MPOS(DL(m));
                   5123:             for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   5124:             adj = nd_exporigin-newexporigin;
                   5125:             for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
                   5126:             if ( nd_module ) {
                   5127:                 DL(r)[newmpos] = pos;
                   5128:             }
                   5129:         }
                   5130:     }
                   5131:     NV(p)--;
                   5132: }
                   5133:
                   5134: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
                   5135: {
                   5136:     int i;
                   5137:     P *tpl,*tpl1;
                   5138:     NODE l;
                   5139:     P h,gcd,t;
                   5140:
                   5141:     tpl = (P *)MALLOC(m*sizeof(P));
                   5142:     tpl1 = (P *)MALLOC(m*sizeof(P));
                   5143:     bcopy(pl,tpl,m*sizeof(P));
                   5144:     gcd = (P)ONE;
                   5145:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   5146:         h = (P)BDY(l);
                   5147:         while ( 1 ) {
                   5148:             for ( i = 0; i < m; i++ )
                   5149:                 if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
                   5150:                     break;
                   5151:             if ( i == m ) {
                   5152:                 bcopy(tpl1,tpl,m*sizeof(P));
                   5153:                 mulp(vl,gcd,h,&t); gcd = t;
                   5154:             } else
                   5155:                 break;
                   5156:         }
                   5157:     }
                   5158:     if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
                   5159:     if ( full ) {
                   5160:         heu_nezgcdnpz(vl,tpl,m,&t);
                   5161:         mulp(vl,gcd,t,pr);
                   5162:     } else
                   5163:         *pr = gcd;
                   5164: }
                   5165:
                   5166: void removecont_array(P *p,int n,int full)
                   5167: {
                   5168:     int all_p,all_q,i;
                   5169:     Z *c;
                   5170:     P *w;
                   5171:     P t,s;
                   5172:
                   5173:     for ( all_q = 1, i = 0; i < n; i++ )
                   5174:         all_q = all_q && NUM(p[i]);
                   5175:     if ( all_q ) {
                   5176:         removecont_array_q((Z *)p,n);
                   5177:     } else {
                   5178:         c = (Z *)MALLOC(n*sizeof(Z));
                   5179:         w = (P *)MALLOC(n*sizeof(P));
                   5180:         for ( i = 0; i < n; i++ ) {
                   5181:             ptozp(p[i],1,(Q *)&c[i],&w[i]);
                   5182:         }
                   5183:         removecont_array_q(c,n);
                   5184:         nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
                   5185:         for ( i = 0; i < n; i++ ) {
                   5186:             divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
                   5187:         }
                   5188:     }
                   5189: }
                   5190:
                   5191: /* c is an int array */
                   5192:
                   5193: void removecont_array_q(Z *c,int n)
                   5194: {
                   5195:   struct oVECT v;
                   5196:   Z d0,d1,a,u,u1,gcd;
                   5197:   int i,j;
                   5198:   Z *q,*r;
                   5199:
                   5200:   q = (Z *)MALLOC(n*sizeof(Z));
                   5201:   r = (Z *)MALLOC(n*sizeof(Z));
                   5202:   v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   5203:   gcdvz_estimate(&v,&d0);
                   5204:   for ( i = 0; i < n; i++ ) {
                   5205:     divqrz(c[i],d0,&q[i],&r[i]);
                   5206:   }
                   5207:   for ( i = 0; i < n; i++ ) if ( r[i] ) break;
                   5208:   if ( i < n ) {
                   5209:     v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   5210:     gcdvz(&v,&d1);
                   5211:     gcdz(d0,d1,&gcd);
1.6       noro     5212:     /* exact division */
                   5213:     divsz(d0,gcd,&a);
1.1       noro     5214:     for ( i = 0; i < n; i++ ) {
                   5215:       mulz(a,q[i],&u);
                   5216:       if ( r[i] ) {
1.6       noro     5217:         /* exact division */
                   5218:         divsz(r[i],gcd,&u1);
1.1       noro     5219:         addz(u,u1,&q[i]);
                   5220:       } else
                   5221:         q[i] = u;
                   5222:     }
                   5223:   }
                   5224:   for ( i = 0; i < n; i++ ) c[i] = q[i];
                   5225: }
                   5226:
1.4       noro     5227: void gcdv_mpz_estimate(mpz_t d0,mpz_t *c,int n);
                   5228:
                   5229: void mpz_removecont_array(mpz_t *c,int n)
                   5230: {
                   5231:   mpz_t d0,a,u,u1,gcd;
                   5232:   int i,j;
1.13      noro     5233:   static mpz_t *q,*r;
                   5234:   static int c_len = 0;
1.4       noro     5235:
                   5236:   for ( i = 0; i < n; i++ )
                   5237:     if ( mpz_sgn(c[i]) ) break;
                   5238:   if ( i == n ) return;
                   5239:   gcdv_mpz_estimate(d0,c,n);
1.13      noro     5240:   if ( n > c_len ) {
                   5241:     q = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   5242:     r = (mpz_t *)MALLOC(n*sizeof(mpz_t));
                   5243:     c_len = n;
                   5244:   }
1.4       noro     5245:   for ( i = 0; i < n; i++ ) {
                   5246:     mpz_init(q[i]); mpz_init(r[i]);
                   5247:     mpz_fdiv_qr(q[i],r[i],c[i],d0);
                   5248:   }
                   5249:   for ( i = 0; i < n; i++ )
                   5250:     if ( mpz_sgn(r[i]) ) break;
                   5251:   mpz_init(gcd); mpz_init(a); mpz_init(u); mpz_init(u1);
                   5252:   if ( i < n ) {
                   5253:     mpz_gcd(gcd,d0,r[i]);
                   5254:     for ( j = i+1; j < n; j++ ) mpz_gcd(gcd,gcd,r[j]);
                   5255:     mpz_div(a,d0,gcd);
                   5256:     for ( i = 0; i < n; i++ ) {
                   5257:       mpz_mul(u,a,q[i]);
                   5258:       if ( mpz_sgn(r[i]) ) {
                   5259:         mpz_div(u1,r[i],gcd);
                   5260:         mpz_add(q[i],u,u1);
                   5261:       } else
                   5262:         mpz_set(q[i],u);
                   5263:     }
                   5264:   }
                   5265:   for ( i = 0; i < n; i++ )
                   5266:     mpz_set(c[i],q[i]);
                   5267: }
                   5268:
1.1       noro     5269: void nd_mul_c(int mod,ND p,int mul)
                   5270: {
                   5271:     NM m;
                   5272:     int c,c1;
                   5273:
                   5274:     if ( !p ) return;
                   5275:     if ( mul == 1 ) return;
                   5276:     if ( mod == -1 )
                   5277:         for ( m = BDY(p); m; m = NEXT(m) )
                   5278:             CM(m) = _mulsf(CM(m),mul);
                   5279:     else
                   5280:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   5281:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   5282:         }
                   5283: }
                   5284:
                   5285: void nd_mul_c_lf(ND p,Z mul)
                   5286: {
                   5287:     NM m;
                   5288:     Z c;
                   5289:
                   5290:     if ( !p ) return;
                   5291:     if ( UNIZ(mul) ) return;
                   5292:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   5293:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   5294:     }
                   5295: }
                   5296:
                   5297: void nd_mul_c_q(ND p,P mul)
                   5298: {
                   5299:     NM m;
                   5300:     P c;
                   5301:
                   5302:     if ( !p ) return;
                   5303:     if ( UNIQ(mul) ) return;
                   5304:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   5305:         mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
                   5306:     }
                   5307: }
                   5308:
                   5309: void nd_mul_c_p(VL vl,ND p,P mul)
                   5310: {
                   5311:     NM m;
                   5312:     P c;
                   5313:
                   5314:     if ( !p ) return;
                   5315:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   5316:         mulp(vl,CP(m),mul,&c); CP(m) = c;
                   5317:     }
                   5318: }
                   5319:
                   5320: void nd_free(ND p)
                   5321: {
                   5322:     NM t,s;
                   5323:
                   5324:     if ( !p ) return;
                   5325:     t = BDY(p);
                   5326:     while ( t ) {
                   5327:         s = NEXT(t);
                   5328:         FREENM(t);
                   5329:         t = s;
                   5330:     }
                   5331:     FREEND(p);
                   5332: }
                   5333:
                   5334: void ndv_free(NDV p)
                   5335: {
                   5336:     GCFREE(BDY(p));
                   5337: }
                   5338:
                   5339: void nd_append_red(UINT *d,int i)
                   5340: {
                   5341:     RHist m,m0;
                   5342:     int h;
                   5343:
                   5344:     NEWRHist(m);
                   5345:     h = ndl_hash_value(d);
                   5346:     m->index = i;
                   5347:     ndl_copy(d,DL(m));
                   5348:     NEXT(m) = nd_red[h];
                   5349:     nd_red[h] = m;
                   5350: }
                   5351:
                   5352: UINT *ndv_compute_bound(NDV p)
                   5353: {
                   5354:     UINT *d1,*d2,*t;
                   5355:     UINT u;
                   5356:     int i,j,k,l,len,ind;
                   5357:     NMV m;
                   5358:
                   5359:     if ( !p )
                   5360:         return 0;
                   5361:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5362:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5363:     len = LEN(p);
                   5364:     m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   5365:     for ( i = 1; i < len; i++, NMV_ADV(m) ) {
                   5366:         ndl_max(DL(m),d1,d2);
                   5367:         t = d1; d1 = d2; d2 = t;
                   5368:     }
                   5369:     l = nd_nvar+31;
                   5370:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   5371:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   5372:         u = d1[i];
                   5373:         k = (nd_epw-1)*nd_bpe;
                   5374:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   5375:             t[ind] = (u>>k)&nd_mask0;
                   5376:     }
                   5377:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   5378:     return t;
                   5379: }
                   5380:
                   5381: UINT *nd_compute_bound(ND p)
                   5382: {
                   5383:     UINT *d1,*d2,*t;
                   5384:     UINT u;
                   5385:     int i,j,k,l,len,ind;
                   5386:     NM m;
                   5387:
                   5388:     if ( !p )
                   5389:         return 0;
                   5390:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5391:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5392:     len = LEN(p);
                   5393:     m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   5394:     for ( m = NEXT(m); m; m = NEXT(m) ) {
                   5395:         ndl_lcm(DL(m),d1,d2);
                   5396:         t = d1; d1 = d2; d2 = t;
                   5397:     }
                   5398:     l = nd_nvar+31;
                   5399:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   5400:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   5401:         u = d1[i];
                   5402:         k = (nd_epw-1)*nd_bpe;
                   5403:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   5404:             t[ind] = (u>>k)&nd_mask0;
                   5405:     }
                   5406:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   5407:     return t;
                   5408: }
                   5409:
                   5410: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
                   5411: /* of a term. In this case we need additional 1 word. */
                   5412:
                   5413: int nd_get_exporigin(struct order_spec *ord)
                   5414: {
                   5415:     switch ( ord->id ) {
1.21      noro     5416:         case 0: case 2: case 256: case 258: case 300:
1.1       noro     5417:             return 1+nd_module;
                   5418:         case 1: case 257:
                   5419:             /* block order */
                   5420:             /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
                   5421:             /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
                   5422:             return ord->ord.block.length+1+nd_module;
                   5423:         case 3: case 259:
                   5424: #if 0
                   5425:             error("nd_get_exporigin : composite order is not supported yet.");
                   5426: #else
                   5427:             return 1+nd_module;
                   5428: #endif
                   5429:         default:
                   5430:             error("nd_get_exporigin : ivalid argument.");
                   5431:             return 0;
                   5432:     }
                   5433: }
                   5434:
                   5435: void nd_setup_parameters(int nvar,int max) {
                   5436:     int i,j,n,elen,ord_o,ord_l,l,s,wpd;
                   5437:     struct order_pair *op;
                   5438:
                   5439:     nd_nvar = nvar;
                   5440:     if ( max ) {
                   5441:         /* XXX */
                   5442:         if ( do_weyl ) nd_bpe = 32;
                   5443:         else if ( max < 2 ) nd_bpe = 1;
                   5444:         else if ( max < 4 ) nd_bpe = 2;
                   5445:         else if ( max < 8 ) nd_bpe = 3;
                   5446:         else if ( max < 16 ) nd_bpe = 4;
                   5447:         else if ( max < 32 ) nd_bpe = 5;
                   5448:         else if ( max < 64 ) nd_bpe = 6;
                   5449:         else if ( max < 256 ) nd_bpe = 8;
                   5450:         else if ( max < 1024 ) nd_bpe = 10;
                   5451:         else if ( max < 65536 ) nd_bpe = 16;
                   5452:         else nd_bpe = 32;
                   5453:     }
                   5454:     if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
                   5455:         UINT t;
                   5456:     int st;
                   5457:         int *v;
                   5458:   /* t = max(weights) */
                   5459:         t = 0;
                   5460:         if ( current_dl_weight_vector )
                   5461:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                   5462:                 if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
                   5463:                 if ( t < st ) t = st;
                   5464:             }
                   5465:         if ( nd_matrix )
                   5466:             for ( i = 0; i < nd_matrix_len; i++ )
                   5467:                 for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
                   5468:                     if ( (st=v[j]) < 0 ) st = -st;
                   5469:                     if ( t < st ) t = st;
                   5470:         }
                   5471:         /* i = bitsize of t */
                   5472:         for ( i = 0; t; t >>=1, i++ );
                   5473:         /* i += bitsize of nd_nvar */
                   5474:         for ( t = nd_nvar; t; t >>=1, i++);
                   5475:         /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
                   5476:         if ( (nd_bpe+i) >= 31 )
                   5477:             error("nd_setup_parameters : too large weight");
                   5478:     }
                   5479:     nd_epw = (sizeof(UINT)*8)/nd_bpe;
                   5480:     elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   5481:     nd_exporigin = nd_get_exporigin(nd_ord);
                   5482:     wpd = nd_exporigin+elen;
                   5483:     if ( nd_module )
                   5484:         nd_mpos = nd_exporigin-1;
                   5485:     else
                   5486:         nd_mpos = -1;
                   5487:     if ( wpd != nd_wpd ) {
                   5488:         nd_free_private_storage();
                   5489:         nd_wpd = wpd;
                   5490:     }
                   5491:     if ( nd_bpe < 32 ) {
                   5492:         nd_mask0 = (1<<nd_bpe)-1;
                   5493:     } else {
                   5494:         nd_mask0 = 0xffffffff;
                   5495:     }
                   5496:     bzero(nd_mask,sizeof(nd_mask));
                   5497:     nd_mask1 = 0;
                   5498:     for ( i = 0; i < nd_epw; i++ ) {
                   5499:         nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   5500:         nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   5501:     }
                   5502:     nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
                   5503:     nd_epos = nd_create_epos(nd_ord);
                   5504:     nd_blockmask = nd_create_blockmask(nd_ord);
                   5505:     nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
                   5506: }
                   5507:
                   5508: ND_pairs nd_reconstruct(int trace,ND_pairs d)
                   5509: {
                   5510:     int i,obpe,oadv,h;
                   5511:     static NM prev_nm_free_list;
                   5512:     static ND_pairs prev_ndp_free_list;
                   5513:     RHist mr0,mr;
                   5514:     RHist r;
                   5515:     RHist *old_red;
                   5516:     ND_pairs s0,s,t;
                   5517:     EPOS oepos;
                   5518:
                   5519:     obpe = nd_bpe;
                   5520:     oadv = nmv_adv;
                   5521:     oepos = nd_epos;
                   5522:     if ( obpe < 2 ) nd_bpe = 2;
                   5523:     else if ( obpe < 3 ) nd_bpe = 3;
                   5524:     else if ( obpe < 4 ) nd_bpe = 4;
                   5525:     else if ( obpe < 5 ) nd_bpe = 5;
                   5526:     else if ( obpe < 6 ) nd_bpe = 6;
                   5527:     else if ( obpe < 8 ) nd_bpe = 8;
                   5528:     else if ( obpe < 10 ) nd_bpe = 10;
                   5529:     else if ( obpe < 16 ) nd_bpe = 16;
                   5530:     else if ( obpe < 32 ) nd_bpe = 32;
                   5531:     else error("nd_reconstruct : exponent too large");
                   5532:
                   5533:     nd_setup_parameters(nd_nvar,0);
                   5534:     prev_nm_free_list = _nm_free_list;
                   5535:     prev_ndp_free_list = _ndp_free_list;
                   5536:     _nm_free_list = 0;
                   5537:     _ndp_free_list = 0;
                   5538:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   5539:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   5540:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   5541:     }
                   5542:     if ( trace )
                   5543:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   5544:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   5545:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   5546:         }
                   5547:     s0 = 0;
                   5548:     for ( t = d; t; t = NEXT(t) ) {
                   5549:         NEXTND_pairs(s0,s);
                   5550:         s->i1 = t->i1;
                   5551:         s->i2 = t->i2;
1.24      noro     5552:         s->sig = t->sig;
1.1       noro     5553:         SG(s) = SG(t);
                   5554:         ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   5555:     }
                   5556:
                   5557:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   5558:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   5559:         old_red[i] = nd_red[i];
                   5560:         nd_red[i] = 0;
                   5561:     }
                   5562:     for ( i = 0; i < REDTAB_LEN; i++ )
                   5563:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   5564:             NEWRHist(mr);
                   5565:             mr->index = r->index;
                   5566:             SG(mr) = SG(r);
                   5567:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   5568:             h = ndl_hash_value(DL(mr));
                   5569:             NEXT(mr) = nd_red[h];
                   5570:             nd_red[h] = mr;
1.24      noro     5571:             mr->sig = r->sig;
1.1       noro     5572:         }
                   5573:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   5574:     old_red = 0;
                   5575:     for ( i = 0; i < nd_psn; i++ ) {
                   5576:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   5577:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.24      noro     5578:         r->sig = nd_psh[i]->sig;
1.1       noro     5579:         nd_psh[i] = r;
                   5580:     }
                   5581:     if ( s0 ) NEXT(s) = 0;
                   5582:     prev_nm_free_list = 0;
                   5583:     prev_ndp_free_list = 0;
                   5584: #if 0
                   5585:     GC_gcollect();
                   5586: #endif
                   5587:     return s0;
                   5588: }
                   5589:
1.30      noro     5590: void nd_reconstruct_s(int trace,ND_pairs *d)
                   5591: {
                   5592:     int i,obpe,oadv,h;
                   5593:     static NM prev_nm_free_list;
                   5594:     static ND_pairs prev_ndp_free_list;
                   5595:     RHist mr0,mr;
                   5596:     RHist r;
                   5597:     RHist *old_red;
                   5598:     ND_pairs s0,s,t;
                   5599:     EPOS oepos;
                   5600:
                   5601:     obpe = nd_bpe;
                   5602:     oadv = nmv_adv;
                   5603:     oepos = nd_epos;
                   5604:     if ( obpe < 2 ) nd_bpe = 2;
                   5605:     else if ( obpe < 3 ) nd_bpe = 3;
                   5606:     else if ( obpe < 4 ) nd_bpe = 4;
                   5607:     else if ( obpe < 5 ) nd_bpe = 5;
                   5608:     else if ( obpe < 6 ) nd_bpe = 6;
                   5609:     else if ( obpe < 8 ) nd_bpe = 8;
                   5610:     else if ( obpe < 10 ) nd_bpe = 10;
                   5611:     else if ( obpe < 16 ) nd_bpe = 16;
                   5612:     else if ( obpe < 32 ) nd_bpe = 32;
                   5613:     else error("nd_reconstruct_s : exponent too large");
                   5614:
                   5615:     nd_setup_parameters(nd_nvar,0);
                   5616:     prev_nm_free_list = _nm_free_list;
                   5617:     prev_ndp_free_list = _ndp_free_list;
                   5618:     _nm_free_list = 0;
                   5619:     _ndp_free_list = 0;
                   5620:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   5621:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   5622:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   5623:     }
                   5624:     if ( trace )
                   5625:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   5626:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
                   5627:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   5628:         }
                   5629:
                   5630:     for ( i = 0; i < nd_nbase; i++ ) {
                   5631:       s0 = 0;
                   5632:       for ( t = d[i]; t; t = NEXT(t) ) {
                   5633:           NEXTND_pairs(s0,s);
                   5634:           s->i1 = t->i1;
                   5635:           s->i2 = t->i2;
                   5636:           s->sig = t->sig;
                   5637:           SG(s) = SG(t);
                   5638:           ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   5639:       }
                   5640:       d[i] = s0;
                   5641:     }
                   5642:
                   5643:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   5644:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   5645:         old_red[i] = nd_red[i];
                   5646:         nd_red[i] = 0;
                   5647:     }
                   5648:     for ( i = 0; i < REDTAB_LEN; i++ )
                   5649:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   5650:             NEWRHist(mr);
                   5651:             mr->index = r->index;
                   5652:             SG(mr) = SG(r);
                   5653:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   5654:             h = ndl_hash_value(DL(mr));
                   5655:             NEXT(mr) = nd_red[h];
                   5656:             nd_red[h] = mr;
                   5657:             mr->sig = r->sig;
                   5658:         }
                   5659:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   5660:     old_red = 0;
                   5661:     for ( i = 0; i < nd_psn; i++ ) {
                   5662:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   5663:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
                   5664:         r->sig = nd_psh[i]->sig;
                   5665:         nd_psh[i] = r;
                   5666:     }
                   5667:     if ( s0 ) NEXT(s) = 0;
                   5668:     prev_nm_free_list = 0;
                   5669:     prev_ndp_free_list = 0;
                   5670: #if 0
                   5671:     GC_gcollect();
                   5672: #endif
                   5673: }
                   5674:
1.1       noro     5675: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
                   5676: {
                   5677:     int n,i,ei,oepw,omask0,j,s,ord_l,l;
                   5678:     struct order_pair *op;
                   5679:
                   5680:     n = nd_nvar;
                   5681:     oepw = (sizeof(UINT)*8)/obpe;
                   5682:     omask0 = (1<<obpe)-1;
                   5683:     TD(r) = TD(d);
                   5684:     for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
                   5685:     if ( nd_blockmask ) {
                   5686:         l = nd_blockmask->n;
                   5687:         op = nd_blockmask->order_pair;
                   5688:         for ( i = 1; i < nd_exporigin; i++ )
                   5689:             r[i] = d[i];
                   5690:         for ( j = 0, s = 0; j < l; j++ ) {
                   5691:             ord_l = op[j].length;
                   5692:             for ( i = 0; i < ord_l; i++, s++ ) {
                   5693:                 ei =  GET_EXP_OLD(d,s);
                   5694:                 PUT_EXP(r,s,ei);
                   5695:             }
                   5696:         }
                   5697:     } else {
                   5698:         for ( i = 0; i < n; i++ ) {
                   5699:             ei = GET_EXP_OLD(d,i);
                   5700:             PUT_EXP(r,i,ei);
                   5701:         }
                   5702:     }
                   5703:     if ( nd_module ) MPOS(r) = MPOS(d);
                   5704: }
                   5705:
                   5706: ND nd_copy(ND p)
                   5707: {
                   5708:     NM m,mr,mr0;
                   5709:     int c,n;
                   5710:     ND r;
                   5711:
                   5712:     if ( !p )
                   5713:         return 0;
                   5714:     else {
                   5715:         for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   5716:             NEXTNM(mr0,mr);
                   5717:             CM(mr) = CM(m);
                   5718:             ndl_copy(DL(m),DL(mr));
                   5719:         }
                   5720:         NEXT(mr) = 0;
                   5721:         MKND(NV(p),mr0,LEN(p),r);
                   5722:         SG(r) = SG(p);
                   5723:         return r;
                   5724:     }
                   5725: }
                   5726:
                   5727: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
                   5728: {
                   5729:     NM m1,m2;
                   5730:     NDV p1,p2;
                   5731:     ND t1,t2;
                   5732:     UINT *lcm;
                   5733:     P gp,tp;
                   5734:     Z g,t;
                   5735:     Z iq;
                   5736:     int td;
                   5737:     LIST hist;
                   5738:     NODE node;
                   5739:     DP d;
                   5740:
                   5741:     if ( !mod && nd_demand ) {
                   5742:         p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
                   5743:     } else {
                   5744:         if ( trace ) {
                   5745:             p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   5746:         } else {
                   5747:             p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   5748:         }
                   5749:     }
                   5750:     lcm = LCM(p);
                   5751:     NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
                   5752:     if ( ndl_check_bound2(p->i1,DL(m1)) ) {
                   5753:         FREENM(m1); return 0;
                   5754:     }
                   5755:     NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
                   5756:     if ( ndl_check_bound2(p->i2,DL(m2)) ) {
                   5757:         FREENM(m1); FREENM(m2); return 0;
                   5758:     }
                   5759:
                   5760:     if ( mod == -1 ) {
                   5761:         CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
                   5762:     } else if ( mod > 0 ) {
                   5763:         CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
                   5764:     } else if ( mod == -2 ) {
                   5765:         CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
                   5766:     } else if ( nd_vc ) {
                   5767:         ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
                   5768:         divsp(nd_vc,HCP(p2),gp,&CP(m1));
                   5769:         divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
                   5770:     } else {
1.6       noro     5771:         igcd_cofactor(HCZ(p1),HCZ(p2),&g,&t,&CZ(m1)); chsgnz(t,&CZ(m2));
1.1       noro     5772:     }
                   5773:     t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
                   5774:     *rp = nd_add(mod,t1,t2);
                   5775:     if ( nd_gentrace ) {
                   5776:         /* nd_tracelist is initialized */
1.6       noro     5777:         STOZ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     5778:         MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
1.6       noro     5779:         STOZ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
1.1       noro     5780:         MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
                   5781:         nd_tracelist = node;
                   5782:     }
1.24      noro     5783:     if ( *rp )
                   5784:       (*rp)->sig = p->sig;
1.1       noro     5785:     FREENM(m1); FREENM(m2);
                   5786:     return 1;
                   5787: }
                   5788:
                   5789: void ndv_mul_c(int mod,NDV p,int mul)
                   5790: {
                   5791:     NMV m;
                   5792:     int c,c1,len,i;
                   5793:
                   5794:     if ( !p ) return;
                   5795:     len = LEN(p);
                   5796:     if ( mod == -1 )
                   5797:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   5798:             CM(m) = _mulsf(CM(m),mul);
                   5799:     else
                   5800:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   5801:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   5802:         }
                   5803: }
                   5804:
                   5805: void ndv_mul_c_lf(NDV p,Z mul)
                   5806: {
                   5807:     NMV m;
                   5808:     Z c;
                   5809:     int len,i;
                   5810:
                   5811:     if ( !p ) return;
                   5812:     len = LEN(p);
                   5813:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   5814:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   5815:     }
                   5816: }
                   5817:
                   5818: /* for nd_det */
                   5819: void ndv_mul_c_q(NDV p,Z mul)
                   5820: {
                   5821:     NMV m;
                   5822:     Z c;
                   5823:     int len,i;
                   5824:
                   5825:     if ( !p ) return;
                   5826:     len = LEN(p);
                   5827:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     5828:         mulz(CZ(m),mul,&c); CZ(m) = c;
1.1       noro     5829:     }
                   5830: }
                   5831:
                   5832: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
                   5833:     int n2,i,j,l,n,tlen;
                   5834:     UINT *d0;
                   5835:     NM *tab,*psum;
                   5836:     ND s,r;
                   5837:     NM t;
                   5838:     NMV m1;
                   5839:
                   5840:     if ( !p ) return 0;
                   5841:     n = NV(p); n2 = n>>1;
                   5842:     d0 = DL(m0);
                   5843:     l = LEN(p);
                   5844:     for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   5845:     tab = (NM *)MALLOC(tlen*sizeof(NM));
                   5846:     psum = (NM *)MALLOC(tlen*sizeof(NM));
                   5847:     for ( i = 0; i < tlen; i++ ) psum[i] = 0;
                   5848:     m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   5849:     for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
                   5850:         /* m0(NM) * m1(NMV) => tab(NM) */
                   5851:         weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
                   5852:         for ( j = 0; j < tlen; j++ ) {
                   5853:             if ( tab[j] ) {
                   5854:                 NEXT(tab[j]) = psum[j];    psum[j] = tab[j];
                   5855:             }
                   5856:         }
                   5857:     }
                   5858:     for ( i = tlen-1, r = 0; i >= 0; i-- )
                   5859:         if ( psum[i] ) {
                   5860:             for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   5861:             MKND(n,psum[i],j,s);
                   5862:             r = nd_add(mod,r,s);
                   5863:         }
                   5864:     if ( r ) SG(r) = SG(p)+TD(d0);
                   5865:     return r;
                   5866: }
                   5867:
                   5868: /* product of monomials */
                   5869: /* XXX block order is not handled correctly */
                   5870:
                   5871: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   5872: {
                   5873:     int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
                   5874:     UINT *d0,*d1,*d,*dt,*ctab;
                   5875:     Z *ctab_q;
                   5876:     Z q,q1;
                   5877:     UINT c0,c1,c;
                   5878:     NM *p;
                   5879:     NM m,t;
                   5880:     int mpos;
                   5881:
                   5882:     for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   5883:     if ( !m0 || !m1 ) return;
                   5884:     d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
                   5885:     if ( nd_module )
                   5886:         if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
                   5887:
                   5888:     NEWNM(m); d = DL(m);
                   5889:     if ( mod ) {
                   5890:         c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
                   5891:     } else if ( nd_vc )
                   5892:         mulp(nd_vc,CP(m0),CP(m1),&CP(m));
                   5893:   else
1.6       noro     5894:         mulz(CZ(m0),CZ(m1),&CZ(m));
1.1       noro     5895:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   5896:     homo = n&1 ? 1 : 0;
                   5897:     if ( homo ) {
                   5898:         /* offset of h-degree */
                   5899:         h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   5900:         PUT_EXP(DL(m),n-1,h);
                   5901:         TD(DL(m)) = h;
                   5902:         if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   5903:     }
                   5904:     tab[0] = m;
                   5905:     NEWNM(m); d = DL(m);
                   5906:     for ( i = 0, curlen = 1; i < n2; i++ ) {
                   5907:         a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   5908:         k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   5909:         /* xi^a*(Di^k*xi^l)*Di^b */
                   5910:         a += l; b += k;
                   5911:         s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
                   5912:         if ( !k || !l ) {
                   5913:             for ( j = 0; j < curlen; j++ )
                   5914:                 if ( (t = tab[j]) != 0 ) {
                   5915:                     dt = DL(t);
                   5916:                     PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
                   5917:                     if ( nd_blockmask ) ndl_weight_mask(dt);
                   5918:                 }
                   5919:             curlen *= k+1;
                   5920:             continue;
                   5921:         }
                   5922:         min = MIN(k,l);
                   5923:         if ( mod ) {
                   5924:             ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
                   5925:             mkwcm(k,l,mod,(int *)ctab);
                   5926:         } else {
                   5927:             ctab_q = (Z *)MALLOC((min+1)*sizeof(Z));
                   5928:             mkwc(k,l,ctab_q);
                   5929:         }
                   5930:         for ( j = min; j >= 0; j-- ) {
                   5931:             for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
                   5932:             PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
                   5933:             h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
                   5934:             if ( homo ) {
                   5935:                 TD(d) = s;
                   5936:                 PUT_EXP(d,n-1,s-h);
                   5937:             } else TD(d) = h;
                   5938:             if ( nd_blockmask ) ndl_weight_mask(d);
                   5939:             if ( mod ) c = ctab[j];
                   5940:             else q = ctab_q[j];
                   5941:             p = tab+curlen*j;
                   5942:             if ( j == 0 ) {
                   5943:                 for ( u = 0; u < curlen; u++, p++ ) {
                   5944:                     if ( tab[u] ) {
                   5945:                         ndl_addto(DL(tab[u]),d);
                   5946:                         if ( mod ) {
                   5947:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
                   5948:                         } else if ( nd_vc )
                   5949:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
                   5950:             else {
1.6       noro     5951:                             mulz(CZ(tab[u]),q,&q1); CZ(tab[u]) = q1;
1.1       noro     5952:                         }
                   5953:                     }
                   5954:                 }
                   5955:             } else {
                   5956:                 for ( u = 0; u < curlen; u++, p++ ) {
                   5957:                     if ( tab[u] ) {
                   5958:                         NEWNM(t);
                   5959:                         ndl_add(DL(tab[u]),d,DL(t));
                   5960:                         if ( mod ) {
                   5961:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
                   5962:                         } else if ( nd_vc )
                   5963:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
                   5964:             else
1.6       noro     5965:                             mulz(CZ(tab[u]),q,&CZ(t));
1.1       noro     5966:                         *p = t;
                   5967:                     }
                   5968:                 }
                   5969:             }
                   5970:         }
                   5971:         curlen *= k+1;
                   5972:     }
                   5973:     FREENM(m);
                   5974:     if ( nd_module ) {
                   5975:         mpos = MPOS(d1);
                   5976:         for ( i = 0; i < tlen; i++ )
                   5977:             if ( tab[i] ) {
                   5978:                 d = DL(tab[i]);
                   5979:                 MPOS(d) = mpos;
                   5980:                 TD(d) = ndl_weight(d);
                   5981:             }
                   5982:     }
                   5983: }
                   5984:
                   5985: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   5986: {
                   5987:     NM mr,mr0;
                   5988:     NMV m;
                   5989:     UINT *d,*dt,*dm;
                   5990:     int c,n,td,i,c1,c2,len;
                   5991:     Q q;
                   5992:     ND r;
                   5993:
                   5994:     if ( !p ) return 0;
                   5995:     else {
                   5996:         n = NV(p); m = BDY(p);
                   5997:         d = DL(m0);
                   5998:         len = LEN(p);
                   5999:         mr0 = 0;
                   6000:         td = TD(d);
                   6001:         c = CM(m0);
                   6002:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6003:             NEXTNM(mr0,mr);
                   6004:             CM(mr) = 1;
                   6005:             ndl_add(DL(m),d,DL(mr));
                   6006:         }
                   6007:         NEXT(mr) = 0;
                   6008:         MKND(NV(p),mr0,len,r);
                   6009:         SG(r) = SG(p) + TD(d);
                   6010:         return r;
                   6011:     }
                   6012: }
                   6013:
                   6014: ND ndv_mul_nm(int mod,NM m0,NDV p)
                   6015: {
                   6016:     NM mr,mr0;
                   6017:     NMV m;
                   6018:     UINT *d,*dt,*dm;
                   6019:     int c,n,td,i,c1,c2,len;
                   6020:     P q;
                   6021:     ND r;
                   6022:
                   6023:     if ( !p ) return 0;
                   6024:     else if ( do_weyl ) {
                   6025:         if ( mod < 0 ) {
                   6026:             error("ndv_mul_nm : not implemented (weyl)");
                   6027:             return 0;
                   6028:         } else
                   6029:             return weyl_ndv_mul_nm(mod,m0,p);
                   6030:     } else {
                   6031:         n = NV(p); m = BDY(p);
                   6032:         d = DL(m0);
                   6033:         len = LEN(p);
                   6034:         mr0 = 0;
                   6035:         td = TD(d);
                   6036:         if ( mod == -1 ) {
                   6037:             c = CM(m0);
                   6038:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6039:                 NEXTNM(mr0,mr);
                   6040:                 CM(mr) = _mulsf(CM(m),c);
                   6041:                 ndl_add(DL(m),d,DL(mr));
                   6042:             }
                   6043:         } else if ( mod == -2 ) {
                   6044:             Z cl;
                   6045:             cl = CZ(m0);
                   6046:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6047:                 NEXTNM(mr0,mr);
                   6048:                 mullf(CZ(m),cl,&CZ(mr));
                   6049:                 ndl_add(DL(m),d,DL(mr));
                   6050:             }
                   6051:         } else if ( mod ) {
                   6052:             c = CM(m0);
                   6053:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6054:                 NEXTNM(mr0,mr);
                   6055:                 c1 = CM(m);
                   6056:                 DMAR(c1,c,0,mod,c2);
                   6057:                 CM(mr) = c2;
                   6058:                 ndl_add(DL(m),d,DL(mr));
                   6059:             }
                   6060:         } else {
                   6061:             q = CP(m0);
                   6062:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6063:                 NEXTNM(mr0,mr);
                   6064:                 mulp(nd_vc,CP(m),q,&CP(mr));
                   6065:                 ndl_add(DL(m),d,DL(mr));
                   6066:             }
                   6067:         }
                   6068:         NEXT(mr) = 0;
                   6069:         MKND(NV(p),mr0,len,r);
                   6070:         SG(r) = SG(p) + TD(d);
                   6071:         return r;
                   6072:     }
                   6073: }
                   6074:
                   6075: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
                   6076: {
                   6077:     NM mq0,mq;
                   6078:     NMV tm;
                   6079:     Q q;
                   6080:     int i,nv,sg,c,c1,c2,hindex;
                   6081:     ND p,t,r;
                   6082:
                   6083:     if ( bucket->m < 0 ) return 0;
                   6084:     else {
                   6085:         nv = NV(d);
                   6086:         mq0 = 0;
                   6087:         tm = (NMV)MALLOC(nmv_adv);
                   6088:         while ( 1 ) {
                   6089:             if ( mod > 0 || mod == -1 )
                   6090:               hindex = head_pbucket(mod,bucket);
                   6091:             else if ( mod == -2 )
                   6092:               hindex = head_pbucket_lf(bucket);
                   6093:             else
                   6094:               hindex = head_pbucket_q(bucket);
                   6095:             if ( hindex < 0 ) break;
                   6096:             p = bucket->body[hindex];
                   6097:             NEXTNM(mq0,mq);
                   6098:             ndl_sub(HDL(p),HDL(d),DL(mq));
                   6099:             ndl_copy(DL(mq),DL(tm));
                   6100:             if ( mod ) {
                   6101:                 c1 = invm(HCM(d),mod); c2 = HCM(p);
                   6102:                 DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   6103:                 CM(tm) = mod-c;
                   6104:             } else {
1.6       noro     6105:                 divsz(HCZ(p),HCZ(d),&CZ(mq));
                   6106:                 chsgnz(CZ(mq),&CZ(tm));
1.1       noro     6107:             }
                   6108:             t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
                   6109:             bucket->body[hindex] = nd_remove_head(p);
                   6110:             t = nd_remove_head(t);
                   6111:             add_pbucket(mod,bucket,t);
                   6112:         }
                   6113:         if ( !mq0 )
                   6114:             r = 0;
                   6115:         else {
                   6116:             NEXT(mq) = 0;
                   6117:             for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   6118:             MKND(nv,mq0,i,r);
                   6119:             /* XXX */
                   6120:             SG(r) = HTD(r);
                   6121:         }
                   6122:         return r;
                   6123:     }
                   6124: }
                   6125:
                   6126: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   6127: {
                   6128:     NMV m,mr,mr0,t;
                   6129:     int len,i,k;
                   6130:
                   6131:     if ( !p ) return;
                   6132:     m = BDY(p); len = LEN(p);
                   6133:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   6134:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   6135:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   6136:     t = (NMV)MALLOC(nmv_adv);
                   6137:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.6       noro     6138:         CZ(t) = CZ(m);
1.1       noro     6139:         for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   6140:         ndl_reconstruct(DL(m),DL(t),obpe,oepos);
1.6       noro     6141:         CZ(mr) = CZ(t);
1.1       noro     6142:         ndl_copy(DL(t),DL(mr));
                   6143:     }
                   6144:     BDY(p) = mr0;
                   6145: }
                   6146:
                   6147: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   6148: {
                   6149:     NMV m,mr,mr0;
                   6150:     int len,i;
                   6151:     NDV r;
                   6152:
                   6153:     if ( !p ) return 0;
                   6154:     m = BDY(p); len = LEN(p);
                   6155:     mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   6156:     for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   6157:         ndl_zero(DL(mr));
                   6158:         ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
1.6       noro     6159:         CZ(mr) = CZ(m);
1.1       noro     6160:     }
                   6161:     MKNDV(NV(p),mr0,len,r);
                   6162:     SG(r) = SG(p);
1.24      noro     6163:     r->sig = p->sig;
1.1       noro     6164:     return r;
                   6165: }
                   6166:
                   6167: /* duplicate p */
                   6168:
                   6169: NDV ndv_dup(int mod,NDV p)
                   6170: {
                   6171:     NDV d;
                   6172:     NMV t,m,m0;
                   6173:     int i,len;
                   6174:
                   6175:     if ( !p ) return 0;
                   6176:     len = LEN(p);
                   6177:     m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   6178:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   6179:         ndl_copy(DL(t),DL(m));
1.6       noro     6180:         CZ(m) = CZ(t);
1.1       noro     6181:     }
                   6182:     MKNDV(NV(p),m0,len,d);
                   6183:     SG(d) = SG(p);
                   6184:     return d;
                   6185: }
                   6186:
                   6187: NDV ndv_symbolic(int mod,NDV p)
                   6188: {
                   6189:     NDV d;
                   6190:     NMV t,m,m0;
                   6191:     int i,len;
                   6192:
                   6193:     if ( !p ) return 0;
                   6194:     len = LEN(p);
                   6195:     m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   6196:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   6197:         ndl_copy(DL(t),DL(m));
1.6       noro     6198:         CZ(m) = ONE;
1.1       noro     6199:     }
                   6200:     MKNDV(NV(p),m0,len,d);
                   6201:     SG(d) = SG(p);
                   6202:     return d;
                   6203: }
                   6204:
                   6205: ND nd_dup(ND p)
                   6206: {
                   6207:     ND d;
                   6208:     NM t,m,m0;
                   6209:
                   6210:     if ( !p ) return 0;
                   6211:     for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   6212:         NEXTNM(m0,m);
                   6213:         ndl_copy(DL(t),DL(m));
1.6       noro     6214:         CZ(m) = CZ(t);
1.1       noro     6215:     }
                   6216:     if ( m0 ) NEXT(m) = 0;
                   6217:     MKND(NV(p),m0,LEN(p),d);
                   6218:     SG(d) = SG(p);
                   6219:     return d;
                   6220: }
                   6221:
                   6222: /* XXX if p->len == 0 then it represents 0 */
                   6223:
                   6224: void ndv_mod(int mod,NDV p)
                   6225: {
                   6226:     NMV t,d;
                   6227:     int r,s,u;
                   6228:     int i,len,dlen;
                   6229:     P cp;
                   6230:     Z c;
                   6231:     Obj gfs;
                   6232:
                   6233:     if ( !p ) return;
                   6234:     len = LEN(p);
                   6235:     dlen = 0;
                   6236:     if ( mod == -1 )
                   6237:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   6238:             simp_ff((Obj)CP(t),&gfs);
                   6239:             if ( gfs ) {
                   6240:               r = FTOIF(CONT((GFS)gfs));
                   6241:               CM(d) = r;
                   6242:               ndl_copy(DL(t),DL(d));
                   6243:               NMV_ADV(d);
                   6244:               dlen++;
                   6245:             }
                   6246:         }
                   6247:     else if ( mod == -2 )
                   6248:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   6249:             simp_ff((Obj)CP(t),&gfs);
                   6250:             if ( gfs ) {
                   6251:               lmtolf((LM)gfs,&CZ(d));
                   6252:               ndl_copy(DL(t),DL(d));
                   6253:               NMV_ADV(d);
                   6254:               dlen++;
                   6255:             }
                   6256:         }
                   6257:     else
                   6258:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   6259:             if ( nd_vc ) {
                   6260:                 nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
                   6261:                 c = (Z)cp;
                   6262:             } else
1.6       noro     6263:                 c = CZ(t);
1.1       noro     6264:             r = remqi((Q)c,mod);
                   6265:             if ( r ) {
                   6266:                 CM(d) = r;
                   6267:                 ndl_copy(DL(t),DL(d));
                   6268:                 NMV_ADV(d);
                   6269:                 dlen++;
                   6270:             }
                   6271:         }
                   6272:     LEN(p) = dlen;
                   6273: }
                   6274:
                   6275: NDV ptondv(VL vl,VL dvl,P p)
                   6276: {
                   6277:     ND nd;
                   6278:
                   6279:     nd = ptond(vl,dvl,p);
                   6280:     return ndtondv(0,nd);
                   6281: }
                   6282:
                   6283: void pltozpl(LIST l,Q *cont,LIST *pp)
                   6284: {
1.16      noro     6285:   NODE nd,nd1;
                   6286:   int n;
                   6287:   P *pl;
                   6288:   Q *cl;
                   6289:   int i;
                   6290:   P dmy;
                   6291:   Z dvr,inv;
                   6292:   LIST r;
                   6293:
                   6294:   nd = BDY(l); n = length(nd);
                   6295:   pl = (P *)MALLOC(n*sizeof(P));
                   6296:   cl = (Q *)MALLOC(n*sizeof(Q));
                   6297:   for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
                   6298:     ptozp((P)BDY(nd),1,&cl[i],&dmy);
                   6299:   }
                   6300:   qltozl(cl,n,&dvr);
                   6301:   divz(ONE,dvr,&inv);
                   6302:   nd = BDY(l);
                   6303:   for ( i = 0; i < n; i++, nd = NEXT(nd) )
                   6304:     divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
                   6305:   nd = 0;
                   6306:   for ( i = n-1; i >= 0; i-- ) {
                   6307:     MKNODE(nd1,pl[i],nd); nd = nd1;
                   6308:   }
                   6309:   MKLIST(r,nd);
                   6310:   *pp = r;
1.1       noro     6311: }
                   6312:
                   6313: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
                   6314:
                   6315: NDV pltondv(VL vl,VL dvl,LIST p)
                   6316: {
                   6317:     int i;
                   6318:     NODE t;
                   6319:     ND r,ri;
                   6320:     NM m;
                   6321:
                   6322:     if ( !nd_module ) error("pltond : module order must be set");
                   6323:     r = 0;
                   6324:     for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
                   6325:         ri = ptond(vl,dvl,(P)BDY(t));
                   6326:         if ( ri )
                   6327:             for ( m = BDY(ri); m; m = NEXT(m) ) {
                   6328:                 MPOS(DL(m)) = i;
                   6329:                 TD(DL(m)) = ndl_weight(DL(m));
                   6330:                 if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   6331:             }
                   6332:         r = nd_add(0,r,ri);
                   6333:     }
                   6334:     return ndtondv(0,r);
                   6335: }
                   6336:
                   6337: ND ptond(VL vl,VL dvl,P p)
                   6338: {
                   6339:     int n,i,j,k,e;
                   6340:     VL tvl;
                   6341:     V v;
                   6342:     DCP dc;
                   6343:     DCP *w;
                   6344:     ND r,s,t,u;
                   6345:     P x;
                   6346:     int c;
                   6347:     UINT *d;
                   6348:     NM m,m0;
                   6349:
                   6350:     if ( !p )
                   6351:         return 0;
                   6352:     else if ( NUM(p) ) {
                   6353:         NEWNM(m);
                   6354:         ndl_zero(DL(m));
                   6355:         if ( !INT((Q)p) )
                   6356:           error("ptond : input must be integer-coefficient");
1.6       noro     6357:         CZ(m) = (Z)p;
1.1       noro     6358:         NEXT(m) = 0;
                   6359:         MKND(nd_nvar,m,1,r);
                   6360:         SG(r) = 0;
                   6361:         return r;
                   6362:     } else {
                   6363:         for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   6364:         w = (DCP *)MALLOC(k*sizeof(DCP));
                   6365:         for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   6366:         for ( i = 0, tvl = dvl, v = VR(p);
                   6367:             tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   6368:         if ( !tvl ) {
                   6369:             for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   6370:                 t = ptond(vl,dvl,COEF(w[j]));
                   6371:                 pwrp(vl,x,DEG(w[j]),&p);
                   6372:                 nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   6373:             }
                   6374:             return s;
                   6375:         } else {
                   6376:             NEWNM(m0); d = DL(m0);
                   6377:             for ( j = k-1, s = 0; j >= 0; j-- ) {
1.6       noro     6378:                 ndl_zero(d); e = ZTOS(DEG(w[j])); PUT_EXP(d,i,e);
1.1       noro     6379:                 TD(d) = MUL_WEIGHT(e,i);
                   6380:                 if ( nd_blockmask) ndl_weight_mask(d);
                   6381:                 if ( nd_module ) MPOS(d) = 0;
                   6382:                 t = ptond(vl,dvl,COEF(w[j]));
                   6383:                 for ( m = BDY(t); m; m = NEXT(m) )
                   6384:                     ndl_addto(DL(m),d);
                   6385:                 SG(t) += TD(d);
                   6386:                 s = nd_add(0,s,t);
                   6387:             }
                   6388:             FREENM(m0);
                   6389:             return s;
                   6390:         }
                   6391:     }
                   6392: }
                   6393:
                   6394: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   6395: {
                   6396:     VL tvl;
                   6397:     int len,n,j,i,e;
                   6398:     NMV m;
                   6399:     Z q;
                   6400:     P c;
                   6401:     UINT *d;
                   6402:     P s,r,u,t,w;
                   6403:     GFS gfs;
                   6404:
                   6405:     if ( !p ) return 0;
                   6406:     else {
                   6407:         len = LEN(p);
                   6408:         n = NV(p);
                   6409:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   6410:         for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   6411:             if ( mod == -1 ) {
                   6412:                e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   6413:             } else if ( mod == -2 ) {
                   6414:                c = (P)CZ(m);
                   6415:             } else if ( mod > 0 ) {
1.6       noro     6416:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     6417:             } else
                   6418:                 c = CP(m);
                   6419:             d = DL(m);
                   6420:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     6421:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     6422:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   6423:             }
                   6424:             addp(vl,s,t,&u); s = u;
                   6425:         }
                   6426:         return s;
                   6427:     }
                   6428: }
                   6429:
                   6430: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
                   6431: {
                   6432:     VL tvl;
                   6433:     int len,n,j,i,e;
                   6434:     NMV m;
                   6435:     Z q;
                   6436:     P c;
                   6437:     UINT *d;
                   6438:     P s,r,u,t,w;
                   6439:     GFS gfs;
                   6440:     P *a;
                   6441:     LIST l;
                   6442:     NODE nd,nd1;
                   6443:
                   6444:     if ( !p ) return 0;
                   6445:     else {
                   6446:         a = (P *)MALLOC((rank+1)*sizeof(P));
                   6447:         for ( i = 0; i <= rank; i++ ) a[i] = 0;
                   6448:         len = LEN(p);
                   6449:         n = NV(p);
                   6450:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   6451:         for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
                   6452:             if ( mod == -1 ) {
                   6453:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   6454:             } else if ( mod ) {
1.6       noro     6455:                 STOZ(CM(m),q); c = (P)q;
1.1       noro     6456:             } else
                   6457:                 c = CP(m);
                   6458:             d = DL(m);
                   6459:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
1.6       noro     6460:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOZ(e,q);
1.1       noro     6461:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   6462:             }
                   6463:             addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
                   6464:         }
                   6465:         nd = 0;
                   6466:         for ( i = rank; i > 0; i-- ) {
                   6467:             MKNODE(nd1,a[i],nd); nd = nd1;
                   6468:         }
                   6469:         MKLIST(l,nd);
                   6470:         return l;
                   6471:     }
                   6472: }
                   6473:
                   6474: NDV ndtondv(int mod,ND p)
                   6475: {
                   6476:     NDV d;
                   6477:     NMV m,m0;
                   6478:     NM t;
                   6479:     int i,len;
                   6480:
                   6481:     if ( !p ) return 0;
                   6482:     len = LEN(p);
                   6483:     if ( mod > 0 || mod == -1 )
                   6484:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   6485:     else
                   6486:         m0 = m = MALLOC(len*nmv_adv);
                   6487: #if 0
                   6488:     ndv_alloc += nmv_adv*len;
                   6489: #endif
                   6490:     for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   6491:         ndl_copy(DL(t),DL(m));
1.6       noro     6492:         CZ(m) = CZ(t);
1.1       noro     6493:     }
                   6494:     MKNDV(NV(p),m0,len,d);
                   6495:     SG(d) = SG(p);
1.24      noro     6496:     d->sig = p->sig;
1.1       noro     6497:     return d;
                   6498: }
                   6499:
1.16      noro     6500: static int dmm_comp_nv;
                   6501:
                   6502: int dmm_comp(DMM *a,DMM *b)
                   6503: {
                   6504:    return -compdmm(dmm_comp_nv,*a,*b);
                   6505: }
                   6506:
                   6507: void dmm_sort_by_ord(DMM *a,int len,int nv)
                   6508: {
                   6509:   dmm_comp_nv = nv;
                   6510:   qsort(a,len,sizeof(DMM),(int (*)(const void *,const void *))dmm_comp);
                   6511: }
                   6512:
                   6513: void dpm_sort(DPM p,DPM *rp)
                   6514: {
                   6515:   DMM t,t1;
                   6516:   int len,i,n;
                   6517:   DMM *a;
                   6518:   DPM d;
                   6519:
                   6520:   if ( !p ) *rp = 0;
                   6521:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   6522:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   6523:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   6524:   n = p->nv;
                   6525:   dmm_sort_by_ord(a,len,n);
                   6526:   t = 0;
                   6527:   for ( i = len-1; i >= 0; i-- ) {
                   6528:     NEWDMM(t1);
                   6529:     t1->c = a[i]->c;
                   6530:     t1->dl = a[i]->dl;
                   6531:     t1->pos = a[i]->pos;
                   6532:     t1->next = t;
                   6533:     t = t1;
                   6534:   }
                   6535:   MKDPM(n,t,d);
                   6536:   SG(d) = SG(p);
                   6537:   *rp = d;
                   6538: }
                   6539:
1.18      noro     6540: int dpm_comp(DPM *a,DPM *b)
                   6541: {
1.22      noro     6542:   return -compdpm(CO,*a,*b);
1.18      noro     6543: }
                   6544:
                   6545: NODE dpm_sort_list(NODE l)
                   6546: {
                   6547:   int i,len;
                   6548:   NODE t,t1;
                   6549:   DPM *a;
                   6550:
                   6551:   len = length(l);
                   6552:   a = (DPM *)MALLOC(len*sizeof(DPM));
                   6553:   for ( t = l, i = 0; i < len; i++, t = NEXT(t) ) a[i] = (DPM)BDY(t);
                   6554:   qsort(a,len,sizeof(DPM),(int (*)(const void *,const void *))dpm_comp);
                   6555:   t = 0;
                   6556:   for ( i = len-1; i >= 0; i-- ) {
                   6557:     MKNODE(t1,(pointer)a[i],t); t = t1;
                   6558:   }
                   6559:   return t;
                   6560: }
                   6561:
1.20      noro     6562: int nmv_comp(NMV a,NMV b)
                   6563: {
1.21      noro     6564:   int t;
                   6565:   t = DL_COMPARE(a->dl,b->dl);
                   6566:   return -t;
1.20      noro     6567: }
                   6568:
1.16      noro     6569: NDV dpmtondv(int mod,DPM p)
                   6570: {
                   6571:   NDV d;
                   6572:   NMV m,m0;
                   6573:   DMM t;
                   6574:   DMM *a;
                   6575:   int i,len,n;
                   6576:
                   6577:   if ( !p ) return 0;
                   6578:   for ( t = BDY(p), len = 0; t; t = NEXT(t), len++ );
                   6579:   a = (DMM *)MALLOC(len*sizeof(DMM));
                   6580:   for ( i = 0, t = BDY(p); i < len; i++, t = NEXT(t) ) a[i] = t;
                   6581:   n = p->nv;
                   6582:   dmm_sort_by_ord(a,len,n);
                   6583:   if ( mod > 0 || mod == -1 )
                   6584:     m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
                   6585:   else
                   6586:     m0 = m = MALLOC(len*nmv_adv);
                   6587: #if 0
                   6588:   ndv_alloc += nmv_adv*len;
                   6589: #endif
                   6590:   for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   6591:     dltondl(n,a[i]->dl,DL(m));
                   6592:     MPOS(DL(m)) = a[i]->pos;
1.20      noro     6593:     TD(DL(m)) = ndl_weight(DL(m));
1.16      noro     6594:     CZ(m) = (Z)a[i]->c;
                   6595:   }
1.20      noro     6596:   qsort(m0,len,nmv_adv,(int (*)(const void *,const void *))nmv_comp);
1.16      noro     6597:   MKNDV(NV(p),m0,len,d);
                   6598:   SG(d) = SG(p);
                   6599:   return d;
                   6600: }
                   6601:
1.1       noro     6602: ND ndvtond(int mod,NDV p)
                   6603: {
                   6604:     ND d;
                   6605:     NM m,m0;
                   6606:     NMV t;
                   6607:     int i,len;
                   6608:
                   6609:     if ( !p ) return 0;
                   6610:     m0 = 0;
                   6611:     len = p->len;
                   6612:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   6613:         NEXTNM(m0,m);
                   6614:         ndl_copy(DL(t),DL(m));
1.6       noro     6615:         CZ(m) = CZ(t);
1.1       noro     6616:     }
                   6617:     NEXT(m) = 0;
                   6618:     MKND(NV(p),m0,len,d);
                   6619:     SG(d) = SG(p);
1.28      noro     6620:     d->sig = p->sig;
1.1       noro     6621:     return d;
                   6622: }
                   6623:
                   6624: DP ndvtodp(int mod,NDV p)
                   6625: {
                   6626:     MP m,m0;
                   6627:   DP d;
                   6628:     NMV t;
                   6629:     int i,len;
                   6630:
                   6631:     if ( !p ) return 0;
                   6632:     m0 = 0;
                   6633:     len = p->len;
                   6634:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   6635:         NEXTMP(m0,m);
                   6636:       m->dl = ndltodl(nd_nvar,DL(t));
                   6637:       m->c = (Obj)ndctop(mod,t->c);
                   6638:     }
                   6639:     NEXT(m) = 0;
                   6640:   MKDP(nd_nvar,m0,d);
                   6641:     SG(d) = SG(p);
                   6642:     return d;
                   6643: }
                   6644:
1.16      noro     6645: DPM ndvtodpm(int mod,NDV p)
                   6646: {
                   6647:   DMM m,m0;
                   6648:   DPM d;
                   6649:   NMV t;
                   6650:   int i,len;
                   6651:
                   6652:   if ( !p ) return 0;
                   6653:   m0 = 0;
                   6654:   len = p->len;
                   6655:   for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   6656:     NEXTDMM(m0,m);
                   6657:     m->dl = ndltodl(nd_nvar,DL(t));
                   6658:     m->c = (Obj)ndctop(mod,t->c);
                   6659:     m->pos = MPOS(DL(t));
                   6660:   }
                   6661:   NEXT(m) = 0;
                   6662:   MKDPM(nd_nvar,m0,d);
                   6663:   SG(d) = SG(p);
                   6664:   return d;
                   6665: }
                   6666:
                   6667:
1.1       noro     6668: DP ndtodp(int mod,ND p)
                   6669: {
                   6670:     MP m,m0;
                   6671:   DP d;
                   6672:     NM t;
                   6673:     int i,len;
                   6674:
                   6675:     if ( !p ) return 0;
                   6676:     m0 = 0;
                   6677:     len = p->len;
                   6678:     for ( t = BDY(p); t; t = NEXT(t) ) {
                   6679:         NEXTMP(m0,m);
                   6680:       m->dl = ndltodl(nd_nvar,DL(t));
                   6681:       m->c = (Obj)ndctop(mod,t->c);
                   6682:     }
                   6683:     NEXT(m) = 0;
                   6684:   MKDP(nd_nvar,m0,d);
                   6685:     SG(d) = SG(p);
                   6686:     return d;
                   6687: }
                   6688:
                   6689: void ndv_print(NDV p)
                   6690: {
                   6691:     NMV m;
                   6692:     int i,len;
                   6693:
                   6694:     if ( !p ) printf("0\n");
                   6695:     else {
                   6696:         len = LEN(p);
                   6697:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6698:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
1.28      noro     6699:             else printf("+%ld*",CM(m));
1.1       noro     6700:             ndl_print(DL(m));
                   6701:         }
                   6702:         printf("\n");
                   6703:     }
                   6704: }
                   6705:
                   6706: void ndv_print_q(NDV p)
                   6707: {
                   6708:     NMV m;
                   6709:     int i,len;
                   6710:
                   6711:     if ( !p ) printf("0\n");
                   6712:     else {
                   6713:         len = LEN(p);
                   6714:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   6715:             printf("+");
1.6       noro     6716:             printexpr(CO,(Obj)CZ(m));
1.1       noro     6717:             printf("*");
                   6718:             ndl_print(DL(m));
                   6719:         }
                   6720:         printf("\n");
                   6721:     }
                   6722: }
                   6723:
                   6724: NODE ndv_reducebase(NODE x,int *perm)
                   6725: {
                   6726:     int len,i,j;
                   6727:     NDVI w;
                   6728:     NODE t,t0;
                   6729:
                   6730:     len = length(x);
                   6731:     w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
                   6732:     for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
                   6733:         w[i].p = BDY(t); w[i].i = perm[i];
                   6734:     }
                   6735:     for ( i = 0; i < len; i++ ) {
                   6736:         for ( j = 0; j < i; j++ ) {
                   6737:             if ( w[i].p && w[j].p ) {
                   6738:                 if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
                   6739:                 else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
                   6740:             }
                   6741:         }
                   6742:     }
                   6743:     for ( i = j = 0, t0 = 0; i < len; i++ ) {
                   6744:         if ( w[i].p ) {
                   6745:             NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
                   6746:             perm[j++] = w[i].i;
                   6747:         }
                   6748:     }
                   6749:     NEXT(t) = 0; x = t0;
                   6750:     return x;
                   6751: }
                   6752:
                   6753: /* XXX incomplete */
                   6754:
1.21      noro     6755: extern DMMstack dmm_stack;
                   6756: int ndl_module_schreyer_compare(UINT *a,UINT *b);
1.16      noro     6757:
1.1       noro     6758: void nd_init_ord(struct order_spec *ord)
                   6759: {
                   6760:   nd_module = (ord->id >= 256);
                   6761:   if ( nd_module ) {
                   6762:     nd_dcomp = -1;
1.21      noro     6763:     nd_module_ordtype = ord->module_ordtype;
1.1       noro     6764:     nd_pot_nelim = ord->pot_nelim;
                   6765:     nd_poly_weight_len = ord->nv;
                   6766:     nd_poly_weight = ord->top_weight;
                   6767:     nd_module_rank = ord->module_rank;
                   6768:     nd_module_weight = ord->module_top_weight;
                   6769:   }
                   6770:   nd_matrix = 0;
                   6771:   nd_matrix_len = 0;
                   6772:     switch ( ord->id ) {
                   6773:         case 0:
                   6774:             switch ( ord->ord.simple ) {
                   6775:                 case 0:
                   6776:                     nd_dcomp = 1;
                   6777:                     nd_isrlex = 1;
                   6778:                     break;
                   6779:                 case 1:
                   6780:                     nd_dcomp = 1;
                   6781:                     nd_isrlex = 0;
                   6782:                     break;
                   6783:                 case 2:
                   6784:                     nd_dcomp = 0;
                   6785:                     nd_isrlex = 0;
                   6786:                     ndl_compare_function = ndl_lex_compare;
                   6787:                     break;
                   6788:                 case 11:
                   6789:                     /* XXX */
                   6790:                     nd_dcomp = 0;
                   6791:                     nd_isrlex = 1;
                   6792:                     ndl_compare_function = ndl_ww_lex_compare;
                   6793:                     break;
                   6794:                 default:
                   6795:                     error("nd_gr : unsupported order");
                   6796:             }
                   6797:             break;
                   6798:         case 1:
                   6799:             /* block order */
                   6800:             /* XXX */
                   6801:             nd_dcomp = -1;
                   6802:             nd_isrlex = 0;
                   6803:             ndl_compare_function = ndl_block_compare;
                   6804:             break;
                   6805:         case 2:
                   6806:             /* matrix order */
                   6807:             /* XXX */
                   6808:             nd_dcomp = -1;
                   6809:             nd_isrlex = 0;
                   6810:             nd_matrix_len = ord->ord.matrix.row;
                   6811:             nd_matrix = ord->ord.matrix.matrix;
                   6812:             ndl_compare_function = ndl_matrix_compare;
                   6813:             break;
                   6814:         case 3:
                   6815:             /* composite order */
                   6816:             nd_dcomp = -1;
                   6817:             nd_isrlex = 0;
                   6818:             nd_worb_len = ord->ord.composite.length;
                   6819:             nd_worb = ord->ord.composite.w_or_b;
                   6820:             ndl_compare_function = ndl_composite_compare;
                   6821:             break;
                   6822:
                   6823:         /* module order */
                   6824:         case 256:
                   6825:             switch ( ord->ord.simple ) {
                   6826:                 case 0:
1.21      noro     6827:                     nd_dcomp = 0;
1.1       noro     6828:                     nd_isrlex = 1;
1.21      noro     6829:                     ndl_compare_function = ndl_module_glex_compare;
1.1       noro     6830:                     break;
                   6831:                 case 1:
1.21      noro     6832:                     nd_dcomp = 0;
1.1       noro     6833:                     nd_isrlex = 0;
                   6834:                     ndl_compare_function = ndl_module_glex_compare;
                   6835:                     break;
                   6836:                 case 2:
1.21      noro     6837:                     nd_dcomp = 0;
1.1       noro     6838:                     nd_isrlex = 0;
1.21      noro     6839:                     ndl_compare_function = ndl_module_compare;
                   6840:                     ndl_base_compare_function = ndl_lex_compare;
1.1       noro     6841:                     break;
                   6842:                 default:
1.21      noro     6843:                     error("nd_init_ord : unsupported order");
1.1       noro     6844:             }
                   6845:             break;
                   6846:         case 257:
                   6847:             /* block order */
                   6848:             nd_isrlex = 0;
1.21      noro     6849:             ndl_compare_function = ndl_module_compare;
                   6850:             ndl_base_compare_function = ndl_block_compare;
1.1       noro     6851:             break;
                   6852:         case 258:
                   6853:             /* matrix order */
                   6854:             nd_isrlex = 0;
                   6855:             nd_matrix_len = ord->ord.matrix.row;
                   6856:             nd_matrix = ord->ord.matrix.matrix;
1.21      noro     6857:             ndl_compare_function = ndl_module_compare;
                   6858:             ndl_base_compare_function = ndl_matrix_compare;
1.1       noro     6859:             break;
                   6860:         case 259:
                   6861:             /* composite order */
                   6862:             nd_isrlex = 0;
                   6863:             nd_worb_len = ord->ord.composite.length;
                   6864:             nd_worb = ord->ord.composite.w_or_b;
1.21      noro     6865:             ndl_compare_function = ndl_module_compare;
                   6866:             ndl_base_compare_function = ndl_composite_compare;
                   6867:             break;
                   6868:         case 300:
                   6869:             /* schreyer order */
                   6870:             if ( ord->base->id != 256 )
                   6871:                error("nd_init_ord : unsupported base order");
                   6872:             ndl_compare_function = ndl_module_schreyer_compare;
                   6873:             dmm_stack = ord->dmmstack;
                   6874:             switch ( ord->base->ord.simple ) {
                   6875:                 case 0:
                   6876:                     nd_isrlex = 1;
                   6877:                     ndl_base_compare_function = ndl_glex_compare;
                   6878:                     dl_base_compare_function = cmpdl_revgradlex;
                   6879:                     break;
                   6880:                 case 1:
                   6881:                     nd_isrlex = 0;
                   6882:                     ndl_base_compare_function = ndl_glex_compare;
                   6883:                     dl_base_compare_function = cmpdl_gradlex;
                   6884:                     break;
                   6885:                 case 2:
                   6886:                     nd_isrlex = 0;
                   6887:                     ndl_base_compare_function = ndl_lex_compare;
                   6888:                     dl_base_compare_function = cmpdl_lex;
                   6889:                     break;
                   6890:                 default:
                   6891:                     error("nd_init_ord : unsupported order");
                   6892:             }
1.1       noro     6893:             break;
                   6894:     }
                   6895:     nd_ord = ord;
                   6896: }
                   6897:
                   6898: BlockMask nd_create_blockmask(struct order_spec *ord)
                   6899: {
                   6900:     int n,i,j,s,l;
                   6901:     UINT *t;
                   6902:     BlockMask bm;
                   6903:
                   6904:     /* we only create mask table for block order */
                   6905:     if ( ord->id != 1 && ord->id != 257 )
                   6906:         return 0;
                   6907:     n = ord->ord.block.length;
                   6908:     bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   6909:     bm->n = n;
                   6910:     bm->order_pair = ord->ord.block.order_pair;
                   6911:     bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
                   6912:     for ( i = 0, s = 0; i < n; i++ ) {
                   6913:         bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
                   6914:         for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   6915:         l = bm->order_pair[i].length;
                   6916:         for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   6917:     }
                   6918:     return bm;
                   6919: }
                   6920:
                   6921: EPOS nd_create_epos(struct order_spec *ord)
                   6922: {
                   6923:     int i,j,l,s,ord_l,ord_o;
                   6924:     EPOS epos;
                   6925:     struct order_pair *op;
                   6926:
                   6927:     epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   6928:     switch ( ord->id ) {
1.21      noro     6929:         case 0: case 256: case 300:
1.1       noro     6930:             if ( nd_isrlex ) {
                   6931:                 for ( i = 0; i < nd_nvar; i++ ) {
                   6932:                     epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   6933:                     epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   6934:                 }
                   6935:             } else {
                   6936:                 for ( i = 0; i < nd_nvar; i++ ) {
                   6937:                     epos[i].i = nd_exporigin + i/nd_epw;
                   6938:                     epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   6939:                 }
                   6940:             }
                   6941:             break;
                   6942:         case 1: case 257:
                   6943:             /* block order */
                   6944:             l = ord->ord.block.length;
                   6945:             op = ord->ord.block.order_pair;
                   6946:             for ( j = 0, s = 0; j < l; j++ ) {
                   6947:                 ord_o = op[j].order;
                   6948:                 ord_l = op[j].length;
                   6949:                 if ( !ord_o )
                   6950:                     for ( i = 0; i < ord_l; i++ ) {
                   6951:                         epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   6952:                         epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   6953:                     }
                   6954:                 else
                   6955:                     for ( i = 0; i < ord_l; i++ ) {
                   6956:                         epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   6957:                         epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   6958:                     }
                   6959:                 s += ord_l;
                   6960:             }
                   6961:             break;
                   6962:         case 2:
                   6963:             /* matrix order */
                   6964:         case 3:
                   6965:             /* composite order */
                   6966:         default:
                   6967:             for ( i = 0; i < nd_nvar; i++ ) {
                   6968:                 epos[i].i = nd_exporigin + i/nd_epw;
                   6969:                 epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   6970:             }
                   6971:             break;
                   6972:     }
                   6973:     return epos;
                   6974: }
                   6975:
                   6976: /* external interface */
                   6977:
                   6978: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
                   6979: {
                   6980:     NODE t,in0,in;
                   6981:     ND ndf,nf;
                   6982:     NDV ndvf;
                   6983:     VL vv,tv;
                   6984:     int stat,nvar,max,mrank;
                   6985:     union oNDC dn;
                   6986:     Q cont;
                   6987:     P pp;
                   6988:     LIST ppl;
                   6989:
                   6990:     if ( !f ) {
                   6991:         *rp = 0;
                   6992:         return;
                   6993:     }
                   6994:     pltovl(v,&vv);
                   6995:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   6996:
                   6997:     /* max=65536 implies nd_bpe=32 */
                   6998:     max = 65536;
                   6999:
                   7000:   nd_module = 0;
                   7001:   /* nd_module will be set if ord is a module ordering */
                   7002:     nd_init_ord(ord);
                   7003:     nd_setup_parameters(nvar,max);
                   7004:     if ( nd_module && OID(f) != O_LIST )
                   7005:         error("nd_nf_p : the first argument must be a list");
                   7006:   if ( nd_module ) mrank = length(BDY((LIST)f));
                   7007:     /* conversion to ndv */
                   7008:     for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   7009:         NEXTNODE(in0,in);
                   7010:         if ( nd_module ) {
                   7011:           if ( !BDY(t) || OID(BDY(t)) != O_LIST
                   7012:                || length(BDY((LIST)BDY(t))) != mrank )
                   7013:               error("nd_nf_p : inconsistent basis element");
                   7014:           if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
                   7015:           else ppl = (LIST)BDY(t);
                   7016:           BDY(in) = (pointer)pltondv(CO,vv,ppl);
                   7017:         } else {
                   7018:           if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
                   7019:           else pp = (P)BDY(t);
                   7020:           BDY(in) = (pointer)ptondv(CO,vv,pp);
                   7021:         }
                   7022:         if ( m ) ndv_mod(m,(NDV)BDY(in));
                   7023:     }
                   7024:     if ( in0 ) NEXT(in) = 0;
                   7025:
                   7026:     if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
                   7027:     else ndvf = ptondv(CO,vv,(P)f);
                   7028:     if ( m ) ndv_mod(m,ndvf);
                   7029:     ndf = (pointer)ndvtond(m,ndvf);
                   7030:
                   7031:     /* dont sort, dont removecont */
1.24      noro     7032:     ndv_setup(m,0,in0,1,1,0);
1.1       noro     7033:     nd_scale=2;
1.6       noro     7034:     stat = nd_nf(m,0,ndf,nd_ps,1,&nf);
1.1       noro     7035:     if ( !stat )
                   7036:         error("nd_nf_p : exponent too large");
                   7037:     if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
                   7038:     else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
                   7039: }
                   7040:
                   7041: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   7042: {
                   7043:     NM m;
                   7044:     UINT *t,*s;
                   7045:     int i;
                   7046:
                   7047:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   7048:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7049:         t = DL(m);
                   7050:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   7051:         r[i] = CM(m);
                   7052:     }
                   7053:     for ( i = 0; !r[i]; i++ );
                   7054:     return i;
                   7055: }
                   7056:
                   7057: int nd_to_vect_q(UINT *s0,int n,ND d,Z *r)
                   7058: {
                   7059:     NM m;
                   7060:     UINT *t,*s;
                   7061:     int i;
                   7062:
                   7063:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   7064:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7065:         t = DL(m);
                   7066:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     7067:         r[i] = CZ(m);
1.1       noro     7068:     }
                   7069:     for ( i = 0; !r[i]; i++ );
                   7070:     return i;
                   7071: }
                   7072:
                   7073: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
                   7074: {
                   7075:     NM m;
                   7076:     UINT *t,*s;
                   7077:     int i;
                   7078:
                   7079:     for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
                   7080:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   7081:         t = DL(m);
                   7082:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   7083:         mpz_set(r[i],BDY(CZ(m)));
                   7084:     }
                   7085:     for ( i = 0; !mpz_sgn(r[i]); i++ );
                   7086:     return i;
                   7087: }
                   7088:
                   7089: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
                   7090: {
                   7091:     NM m;
                   7092:     unsigned long *v;
                   7093:     int i,j,h,size;
                   7094:   UINT *s,*t;
                   7095:
                   7096:   size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
                   7097:     v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
                   7098:     bzero(v,size);
                   7099:     for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
                   7100:     t = DL(m);
                   7101:     h = ndl_hash_value(t);
                   7102:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   7103:       v[i/BLEN] |= 1L <<(i%BLEN);
                   7104:     }
                   7105:     return v;
                   7106: }
                   7107:
                   7108: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
                   7109: {
                   7110:     NMV mr;
                   7111:     UINT *d,*t,*s;
                   7112:     int i,j,len,h,head;
                   7113:
                   7114:     d = DL(m);
                   7115:     len = LEN(p);
                   7116:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   7117:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   7118:         ndl_add(d,DL(mr),t);
                   7119:     h = ndl_hash_value(t);
                   7120:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   7121:     if ( j == 0 ) head = i;
                   7122:       v[i/BLEN] |= 1L <<(i%BLEN);
                   7123:     }
                   7124:     return head;
                   7125: }
                   7126:
                   7127: Z *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   7128: {
                   7129:     NM m;
                   7130:     NMV mr;
                   7131:     UINT *d,*t,*s;
                   7132:     NDV p;
                   7133:     int i,j,len;
                   7134:     Z *r;
                   7135:
                   7136:     m = pair->mul;
                   7137:     d = DL(m);
                   7138:     p = nd_ps[pair->index];
                   7139:     len = LEN(p);
                   7140:     r = (Z *)CALLOC(n,sizeof(Q));
                   7141:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   7142:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   7143:         ndl_add(d,DL(mr),t);
                   7144:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.6       noro     7145:         r[i] = CZ(mr);
1.1       noro     7146:     }
                   7147:     return r;
                   7148: }
                   7149:
1.11      noro     7150: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,NM_ind_pair pair,int start)
1.1       noro     7151: {
                   7152:     NM m;
                   7153:     NMV mr;
1.11      noro     7154:     UINT *d,*t,*s,*u;
1.1       noro     7155:     NDV p;
                   7156:     unsigned char *ivc;
                   7157:     unsigned short *ivs;
                   7158:     UINT *v,*ivi,*s0v;
1.11      noro     7159:     int i,j,len,prev,diff,cdiff,h,st,ed,md,c;
1.1       noro     7160:     IndArray r;
                   7161:
                   7162:     m = pair->mul;
                   7163:     d = DL(m);
                   7164:     if ( trace )
                   7165:       p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
                   7166:     else
                   7167:       p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
                   7168:
                   7169:     len = LEN(p);
                   7170:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   7171:     v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.11      noro     7172:     for ( prev = start, mr = BDY(p), j = 0; j < len; j++, NMV_ADV(mr) ) {
                   7173:       ndl_add(d,DL(mr),t);
                   7174:       st = prev;
                   7175:       ed = n;
                   7176:       while ( ed > st ) {
                   7177:         md = (st+ed)/2;
                   7178:         u = s0+md*nd_wpd;
                   7179:         c = DL_COMPARE(u,t);
                   7180:         if ( c == 0 ) break;
                   7181:         else if ( c > 0 ) st = md;
                   7182:         else ed = md;
                   7183:       }
                   7184:       prev = v[j] = md;
1.1       noro     7185:     }
                   7186:     r = (IndArray)MALLOC(sizeof(struct oIndArray));
                   7187:     r->head = v[0];
                   7188:     diff = 0;
                   7189:     for ( i = 1; i < len; i++ ) {
                   7190:         cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   7191:     }
                   7192:     if ( diff < 256 ) {
                   7193:         r->width = 1;
                   7194:         ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   7195:         r->index.c = ivc;
                   7196:         for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   7197:     } else if ( diff < 65536 ) {
                   7198:         r->width = 2;
                   7199:         ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   7200:         r->index.s = ivs;
                   7201:         for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   7202:     } else {
                   7203:         r->width = 4;
                   7204:         ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   7205:         r->index.i = ivi;
                   7206:         for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
                   7207:     }
                   7208:     return r;
                   7209: }
                   7210:
                   7211: int compress_array(Z *svect,Z *cvect,int n)
                   7212: {
                   7213:     int i,j;
                   7214:
                   7215:     for ( i = j = 0; i < n; i++ )
                   7216:         if ( svect[i] ) cvect[j++] = svect[i];
                   7217:     return j;
                   7218: }
                   7219:
                   7220: void expand_array(Z *svect,Z *cvect,int n)
                   7221: {
                   7222:     int i,j;
                   7223:
                   7224:     for ( i = j = 0; j < n;  i++  )
                   7225:         if ( svect[i] ) svect[i] = cvect[j++];
                   7226: }
                   7227:
1.8       noro     7228: #if 0
1.1       noro     7229: int ndv_reduce_vect_q(Z *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7230: {
                   7231:     int i,j,k,len,pos,prev,nz;
                   7232:     Z cs,mcs,c1,c2,cr,gcd,t;
                   7233:     IndArray ivect;
                   7234:     unsigned char *ivc;
                   7235:     unsigned short *ivs;
                   7236:     unsigned int *ivi;
                   7237:     NDV redv;
                   7238:     NMV mr;
                   7239:     NODE rp;
                   7240:     int maxrs;
                   7241:     double hmag;
                   7242:     Z *cvect;
1.3       noro     7243:     int l;
1.1       noro     7244:
                   7245:     maxrs = 0;
                   7246:     for ( i = 0; i < col && !svect[i]; i++ );
                   7247:     if ( i == col ) return maxrs;
                   7248:     hmag = p_mag((P)svect[i])*nd_scale;
                   7249:     cvect = (Z *)MALLOC(col*sizeof(Q));
                   7250:     for ( i = 0; i < nred; i++ ) {
                   7251:         ivect = imat[i];
                   7252:         k = ivect->head;
                   7253:         if ( svect[k] ) {
                   7254:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7255:             redv = nd_demand?ndv_load(rp0[i]->index)
                   7256:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   7257:             len = LEN(redv); mr = BDY(redv);
1.6       noro     7258:             igcd_cofactor(svect[k],CZ(mr),&gcd,&cs,&cr);
1.1       noro     7259:             chsgnz(cs,&mcs);
                   7260:             if ( !UNIQ(cr) ) {
                   7261:                 for ( j = 0; j < col; j++ ) {
                   7262:                     mulz(svect[j],cr,&c1); svect[j] = c1;
                   7263:                 }
                   7264:             }
                   7265:             svect[k] = 0; prev = k;
                   7266:             switch ( ivect->width ) {
                   7267:                 case 1:
                   7268:                     ivc = ivect->index.c;
                   7269:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7270:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     7271:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     7272:                     }
                   7273:                     break;
                   7274:                 case 2:
                   7275:                     ivs = ivect->index.s;
                   7276:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7277:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     7278:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     7279:                     }
                   7280:                     break;
                   7281:                 case 4:
                   7282:                     ivi = ivect->index.i;
                   7283:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7284:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     7285:                         muladdtoz(CZ(mr),mcs,&svect[pos]);
1.1       noro     7286:                     }
                   7287:                     break;
                   7288:             }
                   7289:             for ( j = k+1; j < col && !svect[j]; j++ );
                   7290:             if ( j == col ) break;
                   7291:             if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
                   7292:                 nz = compress_array(svect,cvect,col);
                   7293:                 removecont_array((P *)cvect,nz,1);
                   7294:                 expand_array(svect,cvect,nz);
                   7295:                 hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   7296:             }
                   7297:         }
                   7298:     }
                   7299:     nz = compress_array(svect,cvect,col);
                   7300:     removecont_array((P *)cvect,nz,1);
                   7301:     expand_array(svect,cvect,nz);
                   7302:     if ( DP_Print ) {
                   7303:         fprintf(asir_out,"-"); fflush(asir_out);
                   7304:     }
                   7305:     return maxrs;
                   7306: }
1.4       noro     7307: #else
1.9       noro     7308:
1.4       noro     7309: /* direct mpz version */
                   7310: int ndv_reduce_vect_q(Z *svect0,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7311: {
                   7312:     int i,j,k,len,pos,prev;
                   7313:     mpz_t cs,cr,gcd;
                   7314:     IndArray ivect;
                   7315:     unsigned char *ivc;
                   7316:     unsigned short *ivs;
                   7317:     unsigned int *ivi;
                   7318:     NDV redv;
                   7319:     NMV mr;
                   7320:     NODE rp;
                   7321:     int maxrs;
                   7322:     double hmag;
                   7323:     int l;
1.13      noro     7324:     static mpz_t *svect;
                   7325:     static int svect_len=0;
1.4       noro     7326:
                   7327:     maxrs = 0;
                   7328:     for ( i = 0; i < col && !svect0[i]; i++ );
                   7329:     if ( i == col ) return maxrs;
                   7330:     hmag = p_mag((P)svect0[i])*nd_scale;
1.13      noro     7331:     if ( col > svect_len ) {
                   7332:       svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   7333:       svect_len = col;
                   7334:     }
1.4       noro     7335:     for ( i = 0; i < col; i++ ) {
                   7336:       mpz_init(svect[i]);
                   7337:       if ( svect0[i] )
                   7338:         mpz_set(svect[i],BDY(svect0[i]));
                   7339:       else
                   7340:         mpz_set_ui(svect[i],0);
                   7341:     }
                   7342:     mpz_init(gcd); mpz_init(cs); mpz_init(cr);
                   7343:     for ( i = 0; i < nred; i++ ) {
                   7344:         ivect = imat[i];
                   7345:         k = ivect->head;
                   7346:         if ( mpz_sgn(svect[k]) ) {
                   7347:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7348:             redv = nd_demand?ndv_load(rp0[i]->index)
                   7349:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
                   7350:             len = LEN(redv); mr = BDY(redv);
1.6       noro     7351:             mpz_gcd(gcd,svect[k],BDY(CZ(mr)));
1.4       noro     7352:             mpz_div(cs,svect[k],gcd);
1.6       noro     7353:             mpz_div(cr,BDY(CZ(mr)),gcd);
1.4       noro     7354:             mpz_neg(cs,cs);
1.9       noro     7355:             if ( MUNIMPZ(cr) )
                   7356:               for ( j = 0; j < col; j++ ) mpz_neg(svect[j],svect[j]);
                   7357:             else if ( !UNIMPZ(cr) )
                   7358:               for ( j = 0; j < col; j++ ) {
                   7359:                 if ( mpz_sgn(svect[j]) ) mpz_mul(svect[j],svect[j],cr);
                   7360:               }
1.4       noro     7361:             mpz_set_ui(svect[k],0);
                   7362:             prev = k;
                   7363:             switch ( ivect->width ) {
                   7364:                 case 1:
                   7365:                     ivc = ivect->index.c;
                   7366:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7367:                         pos = prev+ivc[j]; prev = pos;
1.6       noro     7368:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     7369:                     }
                   7370:                     break;
                   7371:                 case 2:
                   7372:                     ivs = ivect->index.s;
                   7373:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7374:                         pos = prev+ivs[j]; prev = pos;
1.6       noro     7375:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     7376:                     }
                   7377:                     break;
                   7378:                 case 4:
                   7379:                     ivi = ivect->index.i;
                   7380:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7381:                         pos = prev+ivi[j]; prev = pos;
1.6       noro     7382:                         mpz_addmul(svect[pos],BDY(CZ(mr)),cs);
1.4       noro     7383:                     }
                   7384:                     break;
                   7385:             }
                   7386:             for ( j = k+1; j < col && !svect[j]; j++ );
                   7387:             if ( j == col ) break;
                   7388:             if ( hmag && ((double)mpz_sizeinbase(svect[j],2) > hmag) ) {
                   7389:                 mpz_removecont_array(svect,col);
                   7390:                 hmag = ((double)mpz_sizeinbase(svect[j],2))*nd_scale;
                   7391:             }
                   7392:         }
                   7393:     }
                   7394:     mpz_removecont_array(svect,col);
                   7395:     if ( DP_Print ) {
                   7396:         fprintf(asir_out,"-"); fflush(asir_out);
                   7397:     }
                   7398:     for ( i = 0; i < col; i++ )
                   7399:       if ( mpz_sgn(svect[i]) ) MPZTOZ(svect[i],svect0[i]);
                   7400:       else svect0[i] = 0;
                   7401:     return maxrs;
                   7402: }
                   7403: #endif
1.1       noro     7404:
                   7405: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7406: {
                   7407:     int i,j,k,len,pos,prev;
                   7408:     UINT c,c1,c2,c3,up,lo,dmy;
                   7409:     IndArray ivect;
                   7410:     unsigned char *ivc;
                   7411:     unsigned short *ivs;
                   7412:     unsigned int *ivi;
                   7413:     NDV redv;
                   7414:     NMV mr;
                   7415:     NODE rp;
                   7416:     int maxrs;
                   7417:
                   7418:     maxrs = 0;
                   7419:     for ( i = 0; i < nred; i++ ) {
                   7420:         ivect = imat[i];
                   7421:         k = ivect->head; svect[k] %= m;
                   7422:         if ( (c = svect[k]) != 0 ) {
                   7423:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7424:             c = m-c; redv = nd_ps[rp0[i]->index];
                   7425:             len = LEN(redv); mr = BDY(redv);
                   7426:             svect[k] = 0; prev = k;
                   7427:             switch ( ivect->width ) {
                   7428:                 case 1:
                   7429:                     ivc = ivect->index.c;
                   7430:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7431:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
                   7432:             if ( c1 ) {
                   7433:               c2 = svect[pos];
                   7434:                           DMA(c1,c,c2,up,lo);
                   7435:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   7436:                           } else svect[pos] = lo;
                   7437:             }
                   7438:                     }
                   7439:                     break;
                   7440:                 case 2:
                   7441:                     ivs = ivect->index.s;
                   7442:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7443:                         pos = prev+ivs[j]; c1 = CM(mr);
                   7444:                         prev = pos;
                   7445:             if ( c1 ) {
                   7446:               c2 = svect[pos];
                   7447:                           DMA(c1,c,c2,up,lo);
                   7448:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   7449:                           } else svect[pos] = lo;
                   7450:             }
                   7451:                     }
                   7452:                     break;
                   7453:                 case 4:
                   7454:                     ivi = ivect->index.i;
                   7455:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7456:                         pos = prev+ivi[j]; c1 = CM(mr);
                   7457:                         prev = pos;
                   7458:             if ( c1 ) {
                   7459:               c2 = svect[pos];
                   7460:                           DMA(c1,c,c2,up,lo);
                   7461:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   7462:                           } else svect[pos] = lo;
                   7463:             }
                   7464:                     }
                   7465:                     break;
                   7466:             }
                   7467:         }
                   7468:     }
                   7469:     for ( i = 0; i < col; i++ )
                   7470:         if ( svect[i] >= (UINT)m ) svect[i] %= m;
                   7471:     return maxrs;
                   7472: }
                   7473:
                   7474: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7475: {
                   7476:     int i,j,k,len,pos,prev;
                   7477:     UINT c,c1,c2,c3,up,lo,dmy;
                   7478:     IndArray ivect;
                   7479:     unsigned char *ivc;
                   7480:     unsigned short *ivs;
                   7481:     unsigned int *ivi;
                   7482:     NDV redv;
                   7483:     NMV mr;
                   7484:     NODE rp;
                   7485:     int maxrs;
                   7486:
                   7487:     maxrs = 0;
                   7488:     for ( i = 0; i < nred; i++ ) {
                   7489:         ivect = imat[i];
                   7490:         k = ivect->head;
                   7491:         if ( (c = svect[k]) != 0 ) {
                   7492:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7493:             c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
                   7494:             len = LEN(redv); mr = BDY(redv);
                   7495:             svect[k] = 0; prev = k;
                   7496:             switch ( ivect->width ) {
                   7497:                 case 1:
                   7498:                     ivc = ivect->index.c;
                   7499:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7500:                         pos = prev+ivc[j]; prev = pos;
                   7501:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   7502:                     }
                   7503:                     break;
                   7504:                 case 2:
                   7505:                     ivs = ivect->index.s;
                   7506:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7507:                         pos = prev+ivs[j]; prev = pos;
                   7508:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   7509:                     }
                   7510:                     break;
                   7511:                 case 4:
                   7512:                     ivi = ivect->index.i;
                   7513:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7514:                         pos = prev+ivi[j]; prev = pos;
                   7515:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   7516:                     }
                   7517:                     break;
                   7518:             }
                   7519:         }
                   7520:     }
                   7521:     return maxrs;
                   7522: }
                   7523:
                   7524: ND nd_add_lf(ND p1,ND p2)
                   7525: {
                   7526:     int n,c,can;
                   7527:     ND r;
                   7528:     NM m1,m2,mr0,mr,s;
                   7529:     Z t;
                   7530:
                   7531:     if ( !p1 ) return p2;
                   7532:     else if ( !p2 ) return p1;
                   7533:     else {
                   7534:         can = 0;
                   7535:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   7536:             c = DL_COMPARE(DL(m1),DL(m2));
                   7537:             switch ( c ) {
                   7538:                 case 0:
                   7539:                     addlf(CZ(m1),CZ(m2),&t);
                   7540:                     s = m1; m1 = NEXT(m1);
                   7541:                     if ( t ) {
                   7542:                         can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
                   7543:                     } else {
                   7544:                         can += 2; FREENM(s);
                   7545:                     }
                   7546:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   7547:                     break;
                   7548:                 case 1:
                   7549:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   7550:                     break;
                   7551:                 case -1:
                   7552:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   7553:                     break;
                   7554:             }
                   7555:         }
                   7556:         if ( !mr0 )
                   7557:             if ( m1 ) mr0 = m1;
                   7558:             else if ( m2 ) mr0 = m2;
                   7559:             else return 0;
                   7560:         else if ( m1 ) NEXT(mr) = m1;
                   7561:         else if ( m2 ) NEXT(mr) = m2;
                   7562:         else NEXT(mr) = 0;
                   7563:         BDY(p1) = mr0;
                   7564:         SG(p1) = MAX(SG(p1),SG(p2));
                   7565:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   7566:         FREEND(p2);
                   7567:         return p1;
                   7568:     }
                   7569: }
                   7570:
                   7571: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   7572: {
                   7573:     int i,j,k,len,pos,prev;
                   7574:     mpz_t c,mc,c1;
                   7575:     IndArray ivect;
                   7576:     unsigned char *ivc;
                   7577:     unsigned short *ivs;
                   7578:     unsigned int *ivi;
                   7579:     NDV redv;
                   7580:     NMV mr;
                   7581:     NODE rp;
                   7582:     int maxrs;
                   7583:
                   7584:     maxrs = 0;
                   7585:     lf_lazy = 1;
                   7586:     for ( i = 0; i < nred; i++ ) {
                   7587:         ivect = imat[i];
                   7588:         k = ivect->head;
                   7589:         mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
                   7590:         if ( mpz_sgn(svect[k]) ) {
                   7591:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   7592:             mpz_neg(svect[k],svect[k]);
                   7593:             redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
                   7594:             len = LEN(redv); mr = BDY(redv);
                   7595:             prev = k;
                   7596:             switch ( ivect->width ) {
                   7597:                 case 1:
                   7598:                     ivc = ivect->index.c;
                   7599:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7600:                         pos = prev+ivc[j]; prev = pos;
                   7601:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   7602:                     }
                   7603:                     break;
                   7604:                 case 2:
                   7605:                     ivs = ivect->index.s;
                   7606:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7607:                         pos = prev+ivs[j]; prev = pos;
                   7608:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   7609:                     }
                   7610:                     break;
                   7611:                 case 4:
                   7612:                     ivi = ivect->index.i;
                   7613:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   7614:                         pos = prev+ivi[j]; prev = pos;
                   7615:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   7616:                     }
                   7617:                     break;
                   7618:             }
                   7619:             mpz_set_ui(svect[k],0);
                   7620:         }
                   7621:     }
                   7622:     lf_lazy=0;
                   7623:     for ( i = 0; i < col; i++ ) {
                   7624:         mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
                   7625:     }
                   7626:     return maxrs;
                   7627: }
                   7628:
                   7629: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
                   7630: {
                   7631:     int i,j,k,l,rank,s;
                   7632:     mpz_t a,a1,inv;
                   7633:     mpz_t *t,*pivot,*pk;
                   7634:     mpz_t **mat;
                   7635:     struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
                   7636:     int size,size1;
                   7637:
                   7638:     mpz_init(inv);
                   7639:     mpz_init(a);
                   7640:     mat = (mpz_t **)mat0;
                   7641:         size = 0;
                   7642:     for ( rank = 0, j = 0; j < col; j++ ) {
                   7643:         for ( i = rank; i < row; i++ ) {
                   7644:             mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
                   7645:         }
                   7646:         for ( i = rank; i < row; i++ )
                   7647:             if ( mpz_sgn(mat[i][j]) )
                   7648:                 break;
                   7649:         if ( i == row ) {
                   7650:             colstat[j] = 0;
                   7651:             continue;
                   7652:         } else
                   7653:             colstat[j] = 1;
                   7654:         if ( i != rank ) {
                   7655:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   7656:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   7657:         }
                   7658:         pivot = mat[rank];
                   7659:         s = sugar[rank];
                   7660:         mpz_invert(inv,pivot[j],BDY(current_mod_lf));
                   7661:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   7662:             if ( mpz_sgn(*pk) ) {
                   7663:                 mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
                   7664:             }
                   7665:         for ( i = rank+1; i < row; i++ ) {
                   7666:             t = mat[i];
                   7667:             if ( mpz_sgn(t[j]) ) {
                   7668:                 sugar[i] = MAX(sugar[i],s);
                   7669:                 mpz_neg(a,t[j]);
                   7670:                 red_by_vect_lf(t+j,pivot+j,a,col-j);
                   7671:             }
                   7672:         }
                   7673:         rank++;
                   7674:     }
                   7675:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   7676:         if ( colstat[j] ) {
                   7677:             pivot = mat[l];
                   7678:             s = sugar[l];
                   7679:             for ( k = j; k < col; k++ )
                   7680:               mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
                   7681:             for ( i = 0; i < l; i++ ) {
                   7682:                 t = mat[i];
                   7683:                 if ( mpz_sgn(t[j]) ) {
                   7684:                     sugar[i] = MAX(sugar[i],s);
                   7685:                     mpz_neg(a,t[j]);
                   7686:                     red_by_vect_lf(t+j,pivot+j,a,col-j);
                   7687:                 }
                   7688:             }
                   7689:             l--;
                   7690:         }
                   7691:     for ( j = 0, l = 0; l < rank; j++ )
                   7692:         if ( colstat[j] ) {
                   7693:             t = mat[l];
                   7694:             for ( k = j; k < col; k++ ) {
                   7695:                 mpz_mod(t[k],t[k],BDY(current_mod_lf));
                   7696:             }
                   7697:             l++;
                   7698:         }
                   7699:     return rank;
                   7700: }
                   7701:
                   7702:
                   7703: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   7704: {
                   7705:     int j,k,len;
                   7706:     UINT *p;
                   7707:     UINT c;
                   7708:     NDV r;
                   7709:     NMV mr0,mr;
                   7710:
                   7711:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   7712:     if ( !len ) return 0;
                   7713:     else {
                   7714:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   7715: #if 0
                   7716:         ndv_alloc += nmv_adv*len;
                   7717: #endif
                   7718:         mr = mr0;
                   7719:         p = s0vect;
                   7720:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   7721:             if ( !rhead[j] ) {
                   7722:                 if ( (c = vect[k++]) != 0 ) {
                   7723:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   7724:                 }
                   7725:             }
                   7726:         MKNDV(nd_nvar,mr0,len,r);
                   7727:         return r;
                   7728:     }
                   7729: }
                   7730:
                   7731: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
                   7732: {
                   7733:     int j,k,len;
                   7734:     UINT *p;
                   7735:     NDV r;
                   7736:     NMV mr0,mr;
                   7737:
                   7738:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
                   7739:     if ( !len ) return 0;
                   7740:     else {
                   7741:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   7742:         mr = mr0;
                   7743:         p = s0vect;
                   7744:         for ( j = 0; j < col; j++, p += nd_wpd )
                   7745:       if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
                   7746:             ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
                   7747:           }
                   7748:         MKNDV(nd_nvar,mr0,len,r);
                   7749:         return r;
                   7750:     }
                   7751: }
                   7752:
                   7753: /* for preprocessed vector */
                   7754:
                   7755: NDV vect_to_ndv_q(Z *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   7756: {
1.6       noro     7757:   int j,k,len;
                   7758:   UINT *p;
                   7759:   Z c;
                   7760:   NDV r;
                   7761:   NMV mr0,mr;
1.1       noro     7762:
1.6       noro     7763:   for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   7764:   if ( !len ) return 0;
                   7765:   else {
                   7766:     mr0 = (NMV)MALLOC(nmv_adv*len);
1.1       noro     7767: #if 0
1.6       noro     7768:     ndv_alloc += nmv_adv*len;
1.1       noro     7769: #endif
1.6       noro     7770:     mr = mr0;
                   7771:     p = s0vect;
                   7772:     for ( j = k = 0; j < col; j++, p += nd_wpd ) {
                   7773:       if ( !rhead[j] ) {
                   7774:         if ( (c = vect[k++]) != 0 ) {
                   7775:           if ( !INT(c) )
                   7776:             error("vect_to_ndv_q : components must be integers");
                   7777:             ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
                   7778:         }
                   7779:       }
1.1       noro     7780:     }
1.6       noro     7781:     MKNDV(nd_nvar,mr0,len,r);
                   7782:     return r;
                   7783:   }
1.1       noro     7784: }
                   7785:
                   7786: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   7787: {
                   7788:     int j,k,len;
                   7789:     UINT *p;
                   7790:     mpz_t c;
                   7791:     NDV r;
                   7792:     NMV mr0,mr;
                   7793:
                   7794:     for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
                   7795:     if ( !len ) return 0;
                   7796:     else {
                   7797:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   7798: #if 0
                   7799:         ndv_alloc += nmv_adv*len;
                   7800: #endif
                   7801:         mr = mr0;
                   7802:         p = s0vect;
                   7803:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   7804:             if ( !rhead[j] ) {
                   7805:                 c[0] = vect[k++][0];
                   7806:                 if ( mpz_sgn(c) ) {
                   7807:                     ndl_copy(p,DL(mr)); MPZTOZ(c,CZ(mr)); NMV_ADV(mr);
                   7808:                 }
                   7809:             }
                   7810:         MKNDV(nd_nvar,mr0,len,r);
                   7811:         return r;
                   7812:     }
                   7813: }
                   7814:
                   7815: /* for plain vector */
                   7816:
                   7817: NDV plain_vect_to_ndv_q(Z *vect,int col,UINT *s0vect)
                   7818: {
                   7819:     int j,k,len;
                   7820:     UINT *p;
                   7821:     Z c;
                   7822:     NDV r;
                   7823:     NMV mr0,mr;
                   7824:
                   7825:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   7826:     if ( !len ) return 0;
                   7827:     else {
                   7828:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   7829: #if 0
                   7830:         ndv_alloc += nmv_adv*len;
                   7831: #endif
                   7832:         mr = mr0;
                   7833:         p = s0vect;
                   7834:         for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   7835:             if ( (c = vect[k]) != 0 ) {
                   7836:                 if ( !INT(c) )
1.6       noro     7837:                     error("plain_vect_to_ndv_q : components must be integers");
                   7838:                 ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
1.1       noro     7839:             }
                   7840:         MKNDV(nd_nvar,mr0,len,r);
                   7841:         return r;
                   7842:     }
                   7843: }
                   7844:
                   7845: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
                   7846: {
                   7847:     ND_pairs t;
                   7848:     NODE sp0,sp;
                   7849:     int stat;
                   7850:     ND spol;
                   7851:
                   7852:     for ( t = l; t; t = NEXT(t) ) {
                   7853:         stat = nd_sp(m,trace,t,&spol);
                   7854:         if ( !stat ) return 0;
                   7855:         if ( spol ) {
                   7856:             add_pbucket_symbolic(bucket,spol);
                   7857:         }
                   7858:     }
                   7859:     return 1;
                   7860: }
                   7861:
                   7862: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   7863: {
                   7864:     NODE rp0,rp;
                   7865:     NM mul,head,s0,s;
                   7866:     int index,col,i,sugar;
                   7867:     RHist h;
                   7868:     UINT *s0v,*p;
                   7869:     NM_ind_pair pair;
                   7870:     ND red;
                   7871:     NDV *ps;
                   7872:
                   7873:     s0 = 0; rp0 = 0; col = 0;
                   7874:   if ( nd_demand )
                   7875:       ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   7876:   else
                   7877:       ps = trace?nd_ps_trace:nd_ps;
                   7878:     while ( 1 ) {
                   7879:         head = remove_head_pbucket_symbolic(bucket);
                   7880:         if ( !head ) break;
                   7881:         if ( !s0 ) s0 = head;
                   7882:         else NEXT(s) = head;
                   7883:         s = head;
                   7884:         index = ndl_find_reducer(DL(head));
                   7885:         if ( index >= 0 ) {
                   7886:             h = nd_psh[index];
                   7887:             NEWNM(mul);
                   7888:             ndl_sub(DL(head),DL(h),DL(mul));
                   7889:             if ( ndl_check_bound2(index,DL(mul)) )
                   7890:                 return 0;
                   7891:             sugar = TD(DL(mul))+SG(ps[index]);
1.28      noro     7892:             MKNM_ind_pair(pair,mul,index,sugar,0);
1.1       noro     7893:             red = ndv_mul_nm_symbolic(mul,ps[index]);
                   7894:             add_pbucket_symbolic(bucket,nd_remove_head(red));
                   7895:             NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   7896:         }
                   7897:         col++;
                   7898:     }
                   7899:     if ( rp0 ) NEXT(rp) = 0;
                   7900:     NEXT(s) = 0;
                   7901:     s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   7902:     for ( i = 0, p = s0v, s = s0; i < col;
                   7903:         i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   7904:     *s0vect = s0v;
                   7905:     *r = rp0;
                   7906:     return col;
                   7907: }
                   7908:
                   7909: void print_ndp(ND_pairs l)
                   7910: {
                   7911:   ND_pairs t;
                   7912:
                   7913:   for ( t = l; t; t = NEXT(t) )
                   7914:     printf("[%d,%d] ",t->i1,t->i2);
                   7915:   printf("\n");
                   7916: }
                   7917:
                   7918: NODE nd_f4(int m,int checkonly,int **indp)
                   7919: {
                   7920:     int i,nh,stat,index,f4red;
                   7921:     NODE r,g,tn0,tn,node;
                   7922:     ND_pairs d,l,t,ll0,ll,lh;
                   7923:   LIST l0,l1;
                   7924:     ND spol,red;
                   7925:     NDV nf,redv;
                   7926:     NM s0,s;
                   7927:     NODE rp0,srp0,nflist,nzlist,nzlist_t;
                   7928:     int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
                   7929:     UINT c;
                   7930:     UINT **spmat;
                   7931:     UINT *s0vect,*svect,*p,*v;
                   7932:     int *colstat;
                   7933:     IndArray *imat;
                   7934:     int *rhead;
                   7935:     int spcol,sprow;
                   7936:     int sugar,sugarh;
                   7937:     PGeoBucket bucket;
                   7938:     struct oEGT eg0,eg1,eg_f4;
                   7939:     Z i1,i2,sugarq;
1.12      noro     7940:
                   7941:     init_eg(&f4_symb); init_eg(&f4_conv); init_eg(&f4_conv); init_eg(&f4_elim1); init_eg(&f4_elim2);
1.1       noro     7942: #if 0
                   7943:     ndv_alloc = 0;
                   7944: #endif
1.11      noro     7945:     Nf4_red=0;
1.1       noro     7946:     g = 0; d = 0;
                   7947:     for ( i = 0; i < nd_psn; i++ ) {
                   7948:         d = update_pairs(d,g,i,0);
                   7949:         g = update_base(g,i);
                   7950:     }
                   7951:   nzlist = 0;
                   7952:     nzlist_t = nd_nzlist;
                   7953:     f4red = 1;
                   7954:     nd_last_nonzero = 0;
                   7955:     while ( d ) {
                   7956:         get_eg(&eg0);
                   7957:         l = nd_minsugarp(d,&d);
                   7958:         sugar = nd_sugarweight?l->sugar2:SG(l);
                   7959:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   7960:         if ( nzlist_t ) {
                   7961:             node = BDY((LIST)BDY(nzlist_t));
1.6       noro     7962:             sugarh = ZTOS((Q)ARG0(node));
1.1       noro     7963:             tn = BDY((LIST)ARG1(node));
                   7964:             if ( !tn ) {
                   7965:               nzlist_t = NEXT(nzlist_t);
                   7966:               continue;
                   7967:             }
                   7968:             /* tn = [[i1,i2],...] */
                   7969:             lh = nd_ipairtospair(tn);
                   7970:         }
                   7971:         bucket = create_pbucket();
                   7972:         stat = nd_sp_f4(m,0,l,bucket);
                   7973:         if ( !stat ) {
                   7974:             for ( t = l; NEXT(t); t = NEXT(t) );
                   7975:             NEXT(t) = d; d = l;
                   7976:             d = nd_reconstruct(0,d);
                   7977:             continue;
                   7978:         }
                   7979:         if ( bucket->m < 0 ) continue;
                   7980:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   7981:         if ( !col ) {
                   7982:             for ( t = l; NEXT(t); t = NEXT(t) );
                   7983:             NEXT(t) = d; d = l;
                   7984:             d = nd_reconstruct(0,d);
                   7985:             continue;
                   7986:         }
1.12      noro     7987:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1); add_eg(&f4_symb,&eg0,&eg1);
1.1       noro     7988:         if ( DP_Print )
1.6       noro     7989:             fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.5       noro     7990:                 sugar,eg_f4.exectime);
1.1       noro     7991:         nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
                   7992:         if ( checkonly && nflist ) return 0;
                   7993:         /* adding new bases */
                   7994:         if ( nflist ) nd_last_nonzero = f4red;
                   7995:         for ( r = nflist; r; r = NEXT(r) ) {
                   7996:             nf = (NDV)BDY(r);
1.38    ! noro     7997:             if ( nd_f4_td ) SG(nf) = nd_tdeg(nf);
1.1       noro     7998:             ndv_removecont(m,nf);
                   7999:             if ( !m && nd_nalg ) {
                   8000:                 ND nf1;
                   8001:
                   8002:                 nf1 = ndvtond(m,nf);
                   8003:                 nd_monic(0,&nf1);
                   8004:                 nd_removecont(m,nf1);
                   8005:                 nf = ndtondv(m,nf1);
                   8006:             }
1.24      noro     8007:             nh = ndv_newps(m,nf,0);
1.1       noro     8008:             d = update_pairs(d,g,nh,0);
                   8009:             g = update_base(g,nh);
                   8010:         }
                   8011:         if ( DP_Print ) {
                   8012:           fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
                   8013:         }
                   8014:         if ( nd_gentrace ) {
                   8015:       for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
                   8016:         NEXTNODE(tn0,tn);
1.6       noro     8017:                 STOZ(t->i1,i1); STOZ(t->i2,i2);
1.1       noro     8018:                 node = mknode(2,i1,i2); MKLIST(l0,node);
                   8019:         BDY(tn) = l0;
                   8020:       }
                   8021:       if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.6       noro     8022:             STOZ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
1.1       noro     8023:             MKNODE(node,l1,nzlist); nzlist = node;
                   8024:         }
                   8025:         if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
                   8026:         f4red++;
                   8027:         if ( nd_f4red && f4red > nd_f4red ) break;
                   8028:         if ( nd_rank0 && !nflist ) break;
                   8029:     }
                   8030:     if ( nd_gentrace ) {
                   8031:     MKLIST(l0,reverse_node(nzlist));
                   8032:         MKNODE(nd_alltracelist,l0,0);
                   8033:     }
                   8034: #if 0
                   8035:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   8036: #endif
1.12      noro     8037:   if ( DP_Print ) {
                   8038:     fprintf(asir_out,"number of red=%d,",Nf4_red);
                   8039:     fprintf(asir_out,"symb=%.3fsec,conv=%.3fsec,elim1=%.3fsec,elim2=%.3fsec\n",
                   8040:       f4_symb.exectime,f4_conv.exectime,f4_elim1.exectime,f4_elim2.exectime);
                   8041:   }
1.1       noro     8042:   conv_ilist(nd_demand,0,g,indp);
                   8043:     return g;
                   8044: }
                   8045:
                   8046: NODE nd_f4_trace(int m,int **indp)
                   8047: {
                   8048:     int i,nh,stat,index;
                   8049:     NODE r,g;
                   8050:     ND_pairs d,l,l0,t;
                   8051:     ND spol,red;
                   8052:     NDV nf,redv,nfqv,nfv;
                   8053:     NM s0,s;
                   8054:     NODE rp0,srp0,nflist;
                   8055:     int nsp,nred,col,rank,len,k,j,a;
                   8056:     UINT c;
                   8057:     UINT **spmat;
                   8058:     UINT *s0vect,*svect,*p,*v;
                   8059:     int *colstat;
                   8060:     IndArray *imat;
                   8061:     int *rhead;
                   8062:     int spcol,sprow;
                   8063:     int sugar;
                   8064:     PGeoBucket bucket;
                   8065:     struct oEGT eg0,eg1,eg_f4;
                   8066:
                   8067:     g = 0; d = 0;
                   8068:     for ( i = 0; i < nd_psn; i++ ) {
                   8069:         d = update_pairs(d,g,i,0);
                   8070:         g = update_base(g,i);
                   8071:     }
                   8072:     while ( d ) {
                   8073:         get_eg(&eg0);
                   8074:         l = nd_minsugarp(d,&d);
                   8075:         sugar = SG(l);
                   8076:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   8077:         bucket = create_pbucket();
                   8078:         stat = nd_sp_f4(m,0,l,bucket);
                   8079:         if ( !stat ) {
                   8080:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8081:             NEXT(t) = d; d = l;
                   8082:             d = nd_reconstruct(1,d);
                   8083:             continue;
                   8084:         }
                   8085:         if ( bucket->m < 0 ) continue;
                   8086:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   8087:         if ( !col ) {
                   8088:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8089:             NEXT(t) = d; d = l;
                   8090:             d = nd_reconstruct(1,d);
                   8091:             continue;
                   8092:         }
                   8093:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   8094:         if ( DP_Print )
1.3       noro     8095:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",
1.5       noro     8096:                 sugar,eg_f4.exectime);
1.1       noro     8097:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   8098:         if ( !l0 ) continue;
                   8099:         l = l0;
                   8100:
                   8101:         /* over Q */
                   8102:         bucket = create_pbucket();
                   8103:         stat = nd_sp_f4(0,1,l,bucket);
                   8104:         if ( !stat ) {
                   8105:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8106:             NEXT(t) = d; d = l;
                   8107:             d = nd_reconstruct(1,d);
                   8108:             continue;
                   8109:         }
                   8110:         if ( bucket->m < 0 ) continue;
                   8111:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   8112:         if ( !col ) {
                   8113:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8114:             NEXT(t) = d; d = l;
                   8115:             d = nd_reconstruct(1,d);
                   8116:             continue;
                   8117:         }
                   8118:         nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   8119:         /* adding new bases */
                   8120:         for ( r = nflist; r; r = NEXT(r) ) {
                   8121:             nfqv = (NDV)BDY(r);
                   8122:             ndv_removecont(0,nfqv);
1.6       noro     8123:             if ( !remqi((Q)HCZ(nfqv),m) ) return 0;
1.1       noro     8124:             if ( nd_nalg ) {
                   8125:                 ND nf1;
                   8126:
                   8127:                 nf1 = ndvtond(m,nfqv);
                   8128:                 nd_monic(0,&nf1);
                   8129:                 nd_removecont(0,nf1);
                   8130:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   8131:             }
                   8132:             nfv = ndv_dup(0,nfqv);
                   8133:             ndv_mod(m,nfv);
                   8134:             ndv_removecont(m,nfv);
1.24      noro     8135:             nh = ndv_newps(0,nfv,nfqv);
1.1       noro     8136:             d = update_pairs(d,g,nh,0);
                   8137:             g = update_base(g,nh);
                   8138:         }
                   8139:     }
                   8140: #if 0
                   8141:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   8142: #endif
                   8143:   conv_ilist(nd_demand,1,g,indp);
                   8144:     return g;
                   8145: }
                   8146:
                   8147: int rref(matrix mat,int *sugar)
                   8148: {
                   8149:   int row,col,i,j,k,l,s,wcol,wj;
                   8150:   unsigned long bj;
                   8151:   unsigned long **a;
                   8152:   unsigned long *ai,*ak,*as,*t;
                   8153:   int *pivot;
                   8154:
                   8155:   row = mat->row;
                   8156:   col = mat->col;
                   8157:   a = mat->a;
                   8158:   wcol = (col+BLEN-1)/BLEN;
                   8159:   pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   8160:   i = 0;
                   8161:   for ( j = 0; j < col; j++ ) {
                   8162:   wj = j/BLEN; bj = 1L<<(j%BLEN);
                   8163:     for ( k = i; k < row; k++ )
                   8164:     if ( a[k][wj] & bj ) break;
                   8165:     if ( k == row ) continue;
                   8166:   pivot[i] = j;
                   8167:     if ( k != i ) {
                   8168:    t = a[i]; a[i] = a[k]; a[k] = t;
                   8169:    s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
                   8170:   }
                   8171:   ai = a[i];
                   8172:     for ( k = i+1; k < row; k++ ) {
                   8173:     ak = a[k];
                   8174:     if ( ak[wj] & bj ) {
                   8175:       for ( l = wj; l < wcol; l++ )
                   8176:       ak[l] ^= ai[l];
                   8177:       sugar[k] = MAX(sugar[k],sugar[i]);
                   8178:     }
                   8179:   }
                   8180:   i++;
                   8181:   }
                   8182:   for ( k = i-1; k >= 0; k-- ) {
                   8183:     j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
                   8184:   ak = a[k];
                   8185:     for ( s = 0; s < k; s++ ) {
                   8186:     as = a[s];
                   8187:       if ( as[wj] & bj ) {
                   8188:         for ( l = wj; l < wcol; l++ )
                   8189:       as[l] ^= ak[l];
                   8190:       sugar[s] = MAX(sugar[s],sugar[k]);
                   8191:     }
                   8192:   }
                   8193:   }
                   8194:   return i;
                   8195: }
                   8196:
                   8197: void print_matrix(matrix mat)
                   8198: {
                   8199:   int row,col,i,j;
                   8200:   unsigned long *ai;
                   8201:
                   8202:   row = mat->row;
                   8203:   col = mat->col;
                   8204:   printf("%d x %d\n",row,col);
                   8205:   for ( i = 0; i < row; i++ ) {
                   8206:   ai = mat->a[i];
                   8207:     for ( j = 0; j < col; j++ ) {
                   8208:     if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
                   8209:     else putchar('0');
                   8210:   }
                   8211:   putchar('\n');
                   8212:   }
                   8213: }
                   8214:
                   8215: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
                   8216:
                   8217: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
                   8218: {
                   8219:   int row,col,wcol,wj,i,j;
                   8220:   unsigned long bj;
                   8221:   unsigned long *ai;
                   8222:   unsigned long **a;
                   8223:   int len;
                   8224:   int *pos;
                   8225:
                   8226:   row = mat->row;
                   8227:   col = mat->col;
                   8228:   wcol = (col+BLEN-1)/BLEN;
                   8229:   pos = (int *)MALLOC(wcol*sizeof(int));
                   8230:   bzero(pos,wcol*sizeof(int));
                   8231:   for ( i = j = 0; i < wcol; i++ )
                   8232:     if ( v[i] ) pos[j++] = i;;
                   8233:   len = j;
                   8234:   wj = rhead/BLEN;
                   8235:   bj = 1L<<rhead%BLEN;
                   8236:   a = mat->a;
                   8237:   for ( i = 0; i < row; i++ ) {
                   8238:   ai = a[i];
                   8239:     if ( ai[wj]&bj ) {
                   8240:     for ( j = 0; j < len; j++ )
                   8241:       ai[pos[j]] ^= v[pos[j]];
                   8242:     sugar[i] = MAX(sugar[i],rsugar);
                   8243:   }
                   8244:   }
                   8245: }
                   8246:
                   8247: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   8248: {
                   8249:     int nsp,nred,i,i0,k,rank,row;
                   8250:     NODE r0,rp;
                   8251:     ND_pairs sp;
                   8252:   ND spol;
                   8253:   NM_ind_pair rt;
                   8254:     int *s0hash;
                   8255:   UINT *s;
                   8256:   int *pivot,*sugar,*head;
                   8257:   matrix mat;
                   8258:     NM m;
                   8259:     NODE r;
                   8260:   struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
                   8261:   int rhead,rsugar,size;
                   8262:     unsigned long *v;
                   8263:
                   8264:     get_eg(&eg0);
                   8265:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   8266:     nred = length(rp0);
                   8267:     mat = alloc_matrix(nsp,col);
                   8268:     s0hash = (int *)MALLOC(col*sizeof(int));
                   8269:     for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
                   8270:         s0hash[i] = ndl_hash_value(s);
                   8271:
                   8272:   sugar = (int *)MALLOC(nsp*sizeof(int));
                   8273:   for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
                   8274:     nd_sp(2,0,sp,&spol);
                   8275:     if ( spol ) {
                   8276:         mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
                   8277:       sugar[i] = SG(spol);
                   8278:       i++;
                   8279:     }
                   8280:   }
                   8281:   mat->row = i;
                   8282:     if ( DP_Print ) {
                   8283:       fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
                   8284:     }
                   8285:   size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
                   8286:   v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
                   8287:     for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
                   8288:     rt = (NM_ind_pair)BDY(rp);
                   8289:     bzero(v,size);
                   8290:         rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
                   8291:     rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
                   8292:       red_by_vect_2(mat,sugar,v,rhead,rsugar);
                   8293:   }
                   8294:
                   8295:     get_eg(&eg1);
                   8296:     init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
                   8297:   rank = rref(mat,sugar);
                   8298:
                   8299:     for ( i = 0, r0 = 0; i < rank; i++ ) {
                   8300:       NEXTNODE(r0,r);
                   8301:     BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
                   8302:       SG((NDV)BDY(r)) = sugar[i];
                   8303:     }
                   8304:     if ( r0 ) NEXT(r) = 0;
                   8305:     get_eg(&eg2);
                   8306:     init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
                   8307:     if ( DP_Print ) {
                   8308:         fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.5       noro     8309:       eg_elim1.exectime,eg_elim2.exectime);
1.1       noro     8310:         fflush(asir_out);
                   8311:   }
                   8312:     return r0;
                   8313: }
                   8314:
                   8315:
                   8316: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   8317: {
                   8318:     IndArray *imat;
1.11      noro     8319:     int nsp,nred,i,start;
1.1       noro     8320:     int *rhead;
                   8321:     NODE r0,rp;
                   8322:     ND_pairs sp;
                   8323:     NM_ind_pair *rvect;
                   8324:     UINT *s;
                   8325:     int *s0hash;
1.11      noro     8326:     struct oEGT eg0,eg1,eg_conv;
1.1       noro     8327:
                   8328:     if ( m == 2 && nd_rref2 )
                   8329:      return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
                   8330:
                   8331:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   8332:     nred = length(rp0);
                   8333:     imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   8334:     rhead = (int *)MALLOC(col*sizeof(int));
                   8335:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   8336:
                   8337:     /* construction of index arrays */
1.11      noro     8338:     get_eg(&eg0);
1.1       noro     8339:     if ( DP_Print ) {
1.11      noro     8340:       fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   8341:       fflush(asir_out);
1.1       noro     8342:     }
                   8343:     rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
1.11      noro     8344:     for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
1.1       noro     8345:         rvect[i] = (NM_ind_pair)BDY(rp);
1.11      noro     8346:         imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
1.1       noro     8347:         rhead[imat[i]->head] = 1;
1.11      noro     8348:         start = imat[i]->head;
                   8349:     }
1.12      noro     8350:     get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
1.11      noro     8351:     if ( DP_Print ) {
                   8352:       fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   8353:       fflush(asir_out);
1.1       noro     8354:     }
                   8355:     if ( m > 0 )
1.7       noro     8356: #if SIZEOF_LONG==8
1.1       noro     8357:         r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   8358: #else
                   8359:         r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   8360: #endif
                   8361:     else if ( m == -1 )
                   8362:         r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   8363:     else if ( m == -2 )
                   8364:         r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   8365:     else
                   8366:         r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   8367:     return r0;
                   8368: }
                   8369:
                   8370: /* for Fp, 2<=p<2^16 */
                   8371:
                   8372: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   8373:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   8374: {
                   8375:     int spcol,sprow,a;
                   8376:     int i,j,k,l,rank;
                   8377:     NODE r0,r;
                   8378:     ND_pairs sp;
                   8379:     ND spol;
                   8380:     UINT **spmat;
                   8381:     UINT *svect,*v;
                   8382:     int *colstat;
                   8383:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8384:     int maxrs;
                   8385:     int *spsugar;
                   8386:     ND_pairs *spactive;
                   8387:
                   8388:     spcol = col-nred;
                   8389:     get_eg(&eg0);
                   8390:     /* elimination (1st step) */
                   8391:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   8392:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   8393:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8394:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   8395:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8396:         nd_sp(m,0,sp,&spol);
                   8397:         if ( !spol ) continue;
                   8398:         nd_to_vect(m,s0vect,col,spol,svect);
                   8399:         if ( m == -1 )
                   8400:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   8401:         else
                   8402:             maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
                   8403:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   8404:         if ( i < col ) {
                   8405:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   8406:             for ( j = k = 0; j < col; j++ )
                   8407:                 if ( !rhead[j] ) v[k++] = svect[j];
                   8408:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8409:             if ( nz )
                   8410:             spactive[sprow] = sp;
                   8411:             sprow++;
                   8412:         }
                   8413:         nd_free(spol);
                   8414:     }
                   8415:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8416:     if ( DP_Print ) {
1.5       noro     8417:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8418:         fflush(asir_out);
                   8419:     }
                   8420:     /* free index arrays */
                   8421:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   8422:
                   8423:     /* elimination (2nd step) */
                   8424:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8425:     if ( m == -1 )
                   8426:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   8427:     else
                   8428:         rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   8429:     r0 = 0;
                   8430:     for ( i = 0; i < rank; i++ ) {
                   8431:         NEXTNODE(r0,r); BDY(r) =
                   8432:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   8433:         SG((NDV)BDY(r)) = spsugar[i];
                   8434:         GCFREE(spmat[i]);
                   8435:     }
                   8436:     if ( r0 ) NEXT(r) = 0;
                   8437:
                   8438:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   8439:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8440:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8441:     if ( DP_Print ) {
1.5       noro     8442:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8443:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8444:             nsp,nred,sprow,spcol,rank);
1.5       noro     8445:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     8446:     }
                   8447:     if ( nz ) {
                   8448:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   8449:         if ( rank > 0 ) {
                   8450:             NEXT(spactive[rank-1]) = 0;
                   8451:             *nz = spactive[0];
                   8452:         } else
                   8453:             *nz = 0;
                   8454:     }
                   8455:     return r0;
                   8456: }
                   8457:
                   8458:
                   8459: /* for small finite fields */
                   8460:
                   8461: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   8462:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   8463: {
                   8464:     int spcol,sprow,a;
                   8465:     int i,j,k,l,rank;
                   8466:     NODE r0,r;
                   8467:     ND_pairs sp;
                   8468:     ND spol;
                   8469:     UINT **spmat;
                   8470:     UINT *svect,*v;
                   8471:     int *colstat;
                   8472:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8473:     int maxrs;
                   8474:     int *spsugar;
                   8475:     ND_pairs *spactive;
                   8476:
                   8477:     spcol = col-nred;
                   8478:     get_eg(&eg0);
                   8479:     /* elimination (1st step) */
                   8480:     spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   8481:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   8482:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8483:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   8484:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8485:         nd_sp(m,0,sp,&spol);
                   8486:         if ( !spol ) continue;
                   8487:         nd_to_vect(m,s0vect,col,spol,svect);
                   8488:         maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   8489:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   8490:         if ( i < col ) {
                   8491:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   8492:             for ( j = k = 0; j < col; j++ )
                   8493:                 if ( !rhead[j] ) v[k++] = svect[j];
                   8494:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8495:             if ( nz )
                   8496:             spactive[sprow] = sp;
                   8497:             sprow++;
                   8498:         }
                   8499:         nd_free(spol);
                   8500:     }
                   8501:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8502:     if ( DP_Print ) {
1.5       noro     8503:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8504:         fflush(asir_out);
                   8505:     }
                   8506:     /* free index arrays */
                   8507:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   8508:
                   8509:     /* elimination (2nd step) */
                   8510:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8511:     rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   8512:     r0 = 0;
                   8513:     for ( i = 0; i < rank; i++ ) {
                   8514:         NEXTNODE(r0,r); BDY(r) =
                   8515:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   8516:         SG((NDV)BDY(r)) = spsugar[i];
                   8517:         GCFREE(spmat[i]);
                   8518:     }
                   8519:     if ( r0 ) NEXT(r) = 0;
                   8520:
                   8521:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   8522:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8523:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8524:     if ( DP_Print ) {
1.5       noro     8525:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8526:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8527:             nsp,nred,sprow,spcol,rank);
1.5       noro     8528:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     8529:     }
                   8530:     if ( nz ) {
                   8531:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   8532:         if ( rank > 0 ) {
                   8533:             NEXT(spactive[rank-1]) = 0;
                   8534:             *nz = spactive[0];
                   8535:         } else
                   8536:             *nz = 0;
                   8537:     }
                   8538:     return r0;
                   8539: }
                   8540:
                   8541: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   8542:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   8543: {
                   8544:     int spcol,sprow,a;
                   8545:     int i,j,k,l,rank;
                   8546:     NODE r0,r;
                   8547:     ND_pairs sp;
                   8548:     ND spol;
                   8549:     mpz_t **spmat;
                   8550:     mpz_t *svect,*v;
                   8551:     int *colstat;
                   8552:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8553:     int maxrs;
                   8554:     int *spsugar;
                   8555:     pointer *w;
                   8556:
                   8557:     spcol = col-nred;
                   8558:     get_eg(&eg0);
                   8559:     /* elimination (1st step) */
                   8560:     spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
                   8561:     svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   8562:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8563:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8564:         nd_sp(m,trace,sp,&spol);
                   8565:         if ( !spol ) continue;
                   8566:         nd_to_vect_lf(s0vect,col,spol,svect);
                   8567:         maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
                   8568:         for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
                   8569:         if ( i < col ) {
                   8570:             spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
                   8571:             for ( j = k = 0; j < col; j++ )
                   8572:                 if ( !rhead[j] ) v[k++][0] = svect[j][0];
                   8573:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8574:             sprow++;
                   8575:         }
                   8576: /*        nd_free(spol); */
                   8577:     }
                   8578:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8579:     if ( DP_Print ) {
1.5       noro     8580:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8581:         fflush(asir_out);
                   8582:     }
                   8583:     /* free index arrays */
                   8584: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   8585:
                   8586:     /* elimination (2nd step) */
                   8587:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8588:     rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
                   8589:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   8590:     for ( i = 0; i < rank; i++ ) {
                   8591: #if 0
                   8592:         w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   8593:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   8594: #else
                   8595:         w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   8596:         SG((NDV)w[i]) = spsugar[i];
                   8597: #endif
                   8598: /*        GCFREE(spmat[i]); */
                   8599:
                   8600:     }
                   8601: #if 0
                   8602:     qsort(w,rank,sizeof(NDV),
                   8603:         (int (*)(const void *,const void *))ndv_compare);
                   8604: #endif
                   8605:     r0 = 0;
                   8606:     for ( i = 0; i < rank; i++ ) {
                   8607:         NEXTNODE(r0,r); BDY(r) = w[i];
                   8608:     }
                   8609:     if ( r0 ) NEXT(r) = 0;
                   8610:
                   8611: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   8612:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8613:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8614:     if ( DP_Print ) {
1.5       noro     8615:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8616:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8617:             nsp,nred,sprow,spcol,rank);
1.5       noro     8618:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     8619:     }
                   8620:     return r0;
                   8621: }
                   8622:
                   8623: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   8624:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   8625: {
                   8626:     int spcol,sprow,a;
                   8627:     int i,j,k,l,rank;
                   8628:     NODE r0,r;
                   8629:     ND_pairs sp;
                   8630:     ND spol;
                   8631:     Z **spmat;
                   8632:     Z *svect,*v;
                   8633:     int *colstat;
                   8634:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   8635:     int maxrs;
                   8636:     int *spsugar;
                   8637:     pointer *w;
                   8638:
                   8639:     spcol = col-nred;
                   8640:     get_eg(&eg0);
                   8641:     /* elimination (1st step) */
                   8642:     spmat = (Z **)MALLOC(nsp*sizeof(Q *));
                   8643:     svect = (Z *)MALLOC(col*sizeof(Q));
                   8644:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   8645:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   8646:         nd_sp(0,trace,sp,&spol);
                   8647:         if ( !spol ) continue;
                   8648:         nd_to_vect_q(s0vect,col,spol,svect);
                   8649:         maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
                   8650:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   8651:         if ( i < col ) {
                   8652:             spmat[sprow] = v = (Z *)MALLOC(spcol*sizeof(Q));
                   8653:             for ( j = k = 0; j < col; j++ )
                   8654:                 if ( !rhead[j] ) v[k++] = svect[j];
                   8655:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   8656:             sprow++;
                   8657:         }
                   8658: /*        nd_free(spol); */
                   8659:     }
                   8660:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   8661:     if ( DP_Print ) {
1.5       noro     8662:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
1.1       noro     8663:         fflush(asir_out);
                   8664:     }
                   8665:     /* free index arrays */
                   8666: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   8667:
                   8668:     /* elimination (2nd step) */
                   8669:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   8670:     rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
                   8671:     w = (pointer *)MALLOC(rank*sizeof(pointer));
                   8672:     for ( i = 0; i < rank; i++ ) {
                   8673: #if 0
                   8674:         w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   8675:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   8676: #else
                   8677:         w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   8678:         SG((NDV)w[i]) = spsugar[i];
                   8679: #endif
                   8680: /*        GCFREE(spmat[i]); */
                   8681:     }
                   8682: #if 0
                   8683:     qsort(w,rank,sizeof(NDV),
                   8684:         (int (*)(const void *,const void *))ndv_compare);
                   8685: #endif
                   8686:     r0 = 0;
                   8687:     for ( i = 0; i < rank; i++ ) {
                   8688:         NEXTNODE(r0,r); BDY(r) = w[i];
                   8689:     }
                   8690:     if ( r0 ) NEXT(r) = 0;
                   8691:
                   8692: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   8693:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   8694:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   8695:     if ( DP_Print ) {
1.5       noro     8696:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
1.1       noro     8697:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   8698:             nsp,nred,sprow,spcol,rank);
1.5       noro     8699:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
1.1       noro     8700:     }
                   8701:     return r0;
                   8702: }
                   8703:
                   8704: FILE *nd_write,*nd_read;
                   8705:
                   8706: void nd_send_int(int a) {
                   8707:     write_int(nd_write,(unsigned int *)&a);
                   8708: }
                   8709:
                   8710: void nd_send_intarray(int *p,int len) {
                   8711:     write_intarray(nd_write,(unsigned int *)p,len);
                   8712: }
                   8713:
                   8714: int nd_recv_int() {
                   8715:     int a;
                   8716:
                   8717:     read_int(nd_read,(unsigned int *)&a);
                   8718:     return a;
                   8719: }
                   8720:
                   8721: void nd_recv_intarray(int *p,int len) {
                   8722:     read_intarray(nd_read,(unsigned int *)p,len);
                   8723: }
                   8724:
                   8725: void nd_send_ndv(NDV p) {
                   8726:     int len,i;
                   8727:     NMV m;
                   8728:
                   8729:     if ( !p ) nd_send_int(0);
                   8730:     else {
                   8731:         len = LEN(p);
                   8732:         nd_send_int(len);
                   8733:         m = BDY(p);
                   8734:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   8735:             nd_send_int(CM(m));
                   8736:             nd_send_intarray((int *)DL(m),nd_wpd);
                   8737:         }
                   8738:     }
                   8739: }
                   8740:
                   8741: void nd_send_nd(ND p) {
                   8742:     int len,i;
                   8743:     NM m;
                   8744:
                   8745:     if ( !p ) nd_send_int(0);
                   8746:     else {
                   8747:         len = LEN(p);
                   8748:         nd_send_int(len);
                   8749:         m = BDY(p);
                   8750:         for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   8751:             nd_send_int(CM(m));
                   8752:             nd_send_intarray((int *)DL(m),nd_wpd);
                   8753:         }
                   8754:     }
                   8755: }
                   8756:
                   8757: NDV nd_recv_ndv()
                   8758: {
                   8759:     int len,i;
                   8760:     NMV m,m0;
                   8761:     NDV r;
                   8762:
                   8763:     len = nd_recv_int();
                   8764:     if ( !len ) return 0;
                   8765:     else {
                   8766:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   8767: #if 0
                   8768:         ndv_alloc += len*nmv_adv;
                   8769: #endif
                   8770:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   8771:             CM(m) = nd_recv_int();
                   8772:             nd_recv_intarray((int *)DL(m),nd_wpd);
                   8773:         }
                   8774:         MKNDV(nd_nvar,m0,len,r);
                   8775:         return r;
                   8776:     }
                   8777: }
                   8778:
                   8779: int nd_gauss_elim_q(Z **mat0,int *sugar,int row,int col,int *colstat)
                   8780: {
                   8781:     int i,j,t,c,rank,inv;
                   8782:     int *ci,*ri;
                   8783:     Z dn;
                   8784:     MAT m,nm;
                   8785:
                   8786:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
                   8787:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   8788:     for ( i = 0; i < row; i++ )
                   8789:         for ( j = 0; j < col; j++ )
                   8790:             mat0[i][j] = 0;
                   8791:     c = col-rank;
                   8792:     for ( i = 0; i < rank; i++ ) {
                   8793:         mat0[i][ri[i]] = dn;
                   8794:         for ( j = 0; j < c; j++ )
                   8795:             mat0[i][ci[j]] = (Z)BDY(nm)[i][j];
                   8796:     }
                   8797:     return rank;
                   8798: }
                   8799:
                   8800: int nd_gauss_elim_mod(UINT **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   8801: {
                   8802:     int i,j,k,l,inv,a,rank,s;
                   8803:     unsigned int *t,*pivot,*pk;
                   8804:     unsigned int **mat;
                   8805:     ND_pairs pair;
                   8806:
                   8807:     mat = (unsigned int **)mat0;
                   8808:     for ( rank = 0, j = 0; j < col; j++ ) {
                   8809:         for ( i = rank; i < row; i++ )
                   8810:             mat[i][j] %= md;
                   8811:         for ( i = rank; i < row; i++ )
                   8812:             if ( mat[i][j] )
                   8813:                 break;
                   8814:         if ( i == row ) {
                   8815:             colstat[j] = 0;
                   8816:             continue;
                   8817:         } else
                   8818:             colstat[j] = 1;
                   8819:         if ( i != rank ) {
                   8820:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   8821:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   8822:             if ( spactive ) {
                   8823:                 pair = spactive[i]; spactive[i] = spactive[rank];
                   8824:                 spactive[rank] = pair;
                   8825:             }
                   8826:         }
                   8827:         pivot = mat[rank];
                   8828:         s = sugar[rank];
                   8829:         inv = invm(pivot[j],md);
                   8830:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   8831:             if ( *pk ) {
                   8832:                 if ( *pk >= (unsigned int)md )
                   8833:                     *pk %= md;
                   8834:                 DMAR(*pk,inv,0,md,*pk)
                   8835:             }
                   8836:         for ( i = rank+1; i < row; i++ ) {
                   8837:             t = mat[i];
                   8838:             if ( (a = t[j]) != 0 ) {
                   8839:                 sugar[i] = MAX(sugar[i],s);
                   8840:                 red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   8841:             }
                   8842:         }
                   8843:         rank++;
                   8844:     }
                   8845:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   8846:         if ( colstat[j] ) {
                   8847:             pivot = mat[l];
                   8848:             s = sugar[l];
                   8849:             for ( i = 0; i < l; i++ ) {
                   8850:                 t = mat[i];
                   8851:                 t[j] %= md;
                   8852:                 if ( (a = t[j]) != 0 ) {
                   8853:                     sugar[i] = MAX(sugar[i],s);
                   8854:                     red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   8855:                 }
                   8856:             }
                   8857:             l--;
                   8858:         }
                   8859:     for ( j = 0, l = 0; l < rank; j++ )
                   8860:         if ( colstat[j] ) {
                   8861:             t = mat[l];
                   8862:             for ( k = j; k < col; k++ )
                   8863:                 if ( t[k] >= (unsigned int)md )
                   8864:                     t[k] %= md;
                   8865:             l++;
                   8866:         }
                   8867:     return rank;
                   8868: }
                   8869:
                   8870:
1.7       noro     8871: int nd_gauss_elim_sf(UINT **mat0,int *sugar,int row,int col,int md,int *colstat)
1.1       noro     8872: {
1.7       noro     8873:     int i,j,k,l,inv,a,rank,s;
                   8874:     unsigned int *t,*pivot,*pk;
                   8875:     unsigned int **mat;
                   8876:
                   8877:     mat = (unsigned int **)mat0;
                   8878:     for ( rank = 0, j = 0; j < col; j++ ) {
                   8879:         for ( i = rank; i < row; i++ )
                   8880:             if ( mat[i][j] )
                   8881:                 break;
                   8882:         if ( i == row ) {
                   8883:             colstat[j] = 0;
                   8884:             continue;
                   8885:         } else
                   8886:             colstat[j] = 1;
                   8887:         if ( i != rank ) {
                   8888:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   8889:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   8890:         }
                   8891:         pivot = mat[rank];
                   8892:         s = sugar[rank];
                   8893:         inv = _invsf(pivot[j]);
                   8894:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   8895:             if ( *pk )
                   8896:                 *pk = _mulsf(*pk,inv);
                   8897:         for ( i = rank+1; i < row; i++ ) {
                   8898:             t = mat[i];
                   8899:             if ( (a = t[j]) != 0 ) {
                   8900:                 sugar[i] = MAX(sugar[i],s);
                   8901:                 red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   8902:             }
                   8903:         }
                   8904:         rank++;
                   8905:     }
                   8906:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   8907:         if ( colstat[j] ) {
                   8908:             pivot = mat[l];
                   8909:             s = sugar[l];
                   8910:             for ( i = 0; i < l; i++ ) {
                   8911:                 t = mat[i];
                   8912:                 if ( (a = t[j]) != 0 ) {
                   8913:                     sugar[i] = MAX(sugar[i],s);
                   8914:                     red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   8915:                 }
                   8916:             }
                   8917:             l--;
                   8918:         }
                   8919:     return rank;
                   8920: }
1.1       noro     8921:
1.7       noro     8922: int ndv_ishomo(NDV p)
                   8923: {
                   8924:     NMV m;
                   8925:     int len,h;
1.1       noro     8926:
                   8927:     if ( !p ) return 1;
                   8928:     len = LEN(p);
                   8929:     m = BDY(p);
                   8930:     h = TD(DL(m));
                   8931:     NMV_ADV(m);
                   8932:     for ( len--; len; len--, NMV_ADV(m) )
1.20      noro     8933:         if ( TD(DL(m)) != h ) {
                   8934:           return 0;
                   8935:         }
1.1       noro     8936:     return 1;
                   8937: }
                   8938:
                   8939: void ndv_save(NDV p,int index)
                   8940: {
                   8941:     FILE *s;
                   8942:     char name[BUFSIZ];
                   8943:     short id;
                   8944:     int nv,sugar,len,n,i,td,e,j;
                   8945:     NMV m;
                   8946:     unsigned int *dl;
                   8947:     int mpos;
                   8948:
                   8949:     sprintf(name,"%s/%d",Demand,index);
                   8950:     s = fopen(name,"w");
                   8951:     savevl(s,0);
                   8952:     if ( !p ) {
                   8953:         saveobj(s,0);
                   8954:         return;
                   8955:     }
                   8956:     id = O_DP;
                   8957:     nv = NV(p);
                   8958:     sugar = SG(p);
                   8959:     len = LEN(p);
                   8960:     write_short(s,(unsigned short *)&id); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar);
                   8961:     write_int(s,(unsigned int *)&len);
                   8962:
                   8963:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     8964:         saveobj(s,(Obj)CZ(m));
1.1       noro     8965:         dl = DL(m);
                   8966:         td = TD(dl);
                   8967:         write_int(s,(unsigned int *)&td);
                   8968:         for ( j = 0; j < nv; j++ ) {
                   8969:             e = GET_EXP(dl,j);
                   8970:             write_int(s,(unsigned int *)&e);
                   8971:         }
                   8972:         if ( nd_module ) {
                   8973:             mpos = MPOS(dl); write_int(s,(unsigned int *)&mpos);
                   8974:         }
                   8975:     }
                   8976:     fclose(s);
                   8977: }
                   8978:
                   8979: void nd_save_mod(ND p,int index)
                   8980: {
                   8981:     FILE *s;
                   8982:     char name[BUFSIZ];
                   8983:     int nv,sugar,len,c;
                   8984:     NM m;
                   8985:
                   8986:     sprintf(name,"%s/%d",Demand,index);
                   8987:     s = fopen(name,"w");
                   8988:     if ( !p ) {
                   8989:     len = 0;
                   8990:       write_int(s,(unsigned int *)&len);
                   8991:     fclose(s);
                   8992:         return;
                   8993:     }
                   8994:     nv = NV(p);
                   8995:     sugar = SG(p);
                   8996:     len = LEN(p);
                   8997:     write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&len);
                   8998:   for ( m = BDY(p); m; m = NEXT(m) ) {
                   8999:     c = CM(m); write_int(s,(unsigned int *)&c);
                   9000:     write_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   9001:   }
                   9002:     fclose(s);
                   9003: }
                   9004:
                   9005: NDV ndv_load(int index)
                   9006: {
                   9007:     FILE *s;
                   9008:     char name[BUFSIZ];
                   9009:     short id;
                   9010:     int nv,sugar,len,n,i,td,e,j;
                   9011:     NDV d;
                   9012:     NMV m0,m;
                   9013:     unsigned int *dl;
                   9014:     Obj obj;
                   9015:     int mpos;
                   9016:
                   9017:     sprintf(name,"%s/%d",Demand,index);
                   9018:     s = fopen(name,"r");
                   9019:     if ( !s ) return 0;
                   9020:
                   9021:     skipvl(s);
                   9022:     read_short(s,(unsigned short *)&id);
                   9023:     if ( !id ) return 0;
                   9024:     read_int(s,(unsigned int *)&nv);
                   9025:     read_int(s,(unsigned int *)&sugar);
                   9026:     read_int(s,(unsigned int *)&len);
                   9027:
                   9028:     m0 = m = MALLOC(len*nmv_adv);
                   9029:     for ( i = 0; i < len; i++, NMV_ADV(m) ) {
1.6       noro     9030:         loadobj(s,&obj); CZ(m) = (Z)obj;
1.1       noro     9031:         dl = DL(m);
                   9032:         ndl_zero(dl);
                   9033:         read_int(s,(unsigned int *)&td); TD(dl) = td;
                   9034:         for ( j = 0; j < nv; j++ ) {
                   9035:             read_int(s,(unsigned int *)&e);
                   9036:             PUT_EXP(dl,j,e);
                   9037:         }
                   9038:         if ( nd_module ) {
                   9039:             read_int(s,(unsigned int *)&mpos); MPOS(dl) = mpos;
                   9040:         }
                   9041:         if ( nd_blockmask ) ndl_weight_mask(dl);
                   9042:     }
                   9043:     fclose(s);
                   9044:     MKNDV(nv,m0,len,d);
                   9045:     SG(d) = sugar;
                   9046:     return d;
                   9047: }
                   9048:
                   9049: ND nd_load_mod(int index)
                   9050: {
                   9051:     FILE *s;
                   9052:     char name[BUFSIZ];
                   9053:     int nv,sugar,len,i,c;
                   9054:   ND d;
                   9055:     NM m0,m;
                   9056:
                   9057:     sprintf(name,"%s/%d",Demand,index);
                   9058:     s = fopen(name,"r");
                   9059:   /* if the file does not exist, it means p[index]=0 */
                   9060:     if ( !s ) return 0;
                   9061:
                   9062:     read_int(s,(unsigned int *)&nv);
                   9063:   if ( !nv ) { fclose(s); return 0; }
                   9064:
                   9065:     read_int(s,(unsigned int *)&sugar);
                   9066:     read_int(s,(unsigned int *)&len);
                   9067:   for ( m0 = 0, i = 0; i < len; i++ ) {
                   9068:     NEXTNM(m0,m);
                   9069:     read_int(s,(unsigned int *)&c); CM(m) = c;
                   9070:     read_intarray(s,(unsigned int *)DL(m),nd_wpd);
                   9071:   }
                   9072:   NEXT(m) = 0;
                   9073:     MKND(nv,m0,len,d);
                   9074:     SG(d) = sugar;
                   9075:   fclose(s);
                   9076:     return d;
                   9077: }
                   9078:
                   9079: void nd_det(int mod,MAT f,P *rp)
                   9080: {
                   9081:     VL fv,tv;
                   9082:     int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
                   9083:     pointer **m;
                   9084:     P **w;
                   9085:     P mp,r;
                   9086:     NDV **dm;
                   9087:     NDV *t,*mi,*mj;
                   9088:     NDV d,s,mij,mjj;
                   9089:     ND u;
                   9090:     NMV nmv;
                   9091:     UINT *bound;
                   9092:     PGeoBucket bucket;
                   9093:     struct order_spec *ord;
                   9094:     Z dq,dt,ds;
                   9095:     Z mone;
                   9096:     Z gn,qn,dn0,nm,dn;
                   9097:
                   9098:     create_order_spec(0,0,&ord);
                   9099:     nd_init_ord(ord);
                   9100:     get_vars((Obj)f,&fv);
                   9101:     if ( f->row != f->col )
                   9102:         error("nd_det : non-square matrix");
                   9103:     n = f->row;
                   9104:     m = f->body;
                   9105:     for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   9106:
                   9107:     if ( !nvar ) {
                   9108:         if ( !mod )
                   9109:             detp(CO,(P **)m,n,rp);
                   9110:         else {
                   9111:             w = (P **)almat_pointer(n,n);
                   9112:             for ( i = 0; i < n; i++ )
                   9113:                 for ( j = 0; j < n; j++ )
                   9114:                     ptomp(mod,(P)m[i][j],&w[i][j]);
                   9115:             detmp(CO,mod,w,n,&mp);
                   9116:             mptop(mp,rp);
                   9117:         }
                   9118:         return;
                   9119:     }
                   9120:
                   9121:     if ( !mod ) {
                   9122:         w = (P **)almat_pointer(n,n);
                   9123:         dq = ONE;
                   9124:         for ( i = 0; i < n; i++ ) {
                   9125:             dn0 = ONE;
                   9126:             for ( j = 0; j < n; j++ ) {
                   9127:                 if ( !m[i][j] ) continue;
                   9128:                 lgp(m[i][j],&nm,&dn);
1.6       noro     9129:                 gcdz(dn0,dn,&gn); divsz(dn0,gn,&qn); mulz(qn,dn,&dn0);
1.1       noro     9130:             }
                   9131:             if ( !UNIZ(dn0) ) {
                   9132:                 ds = dn0;
                   9133:                 for ( j = 0; j < n; j++ )
                   9134:                     mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   9135:                 mulz(dq,ds,&dt); dq = dt;
                   9136:             } else
                   9137:                 for ( j = 0; j < n; j++ )
                   9138:                     w[i][j] = (P)m[i][j];
                   9139:         }
                   9140:         m = (pointer **)w;
                   9141:     }
                   9142:
                   9143:     for ( i = 0, max = 1; i < n; i++ )
                   9144:         for ( j = 0; j < n; j++ )
                   9145:             for ( tv = fv; tv; tv = NEXT(tv) ) {
                   9146:                 e = getdeg(tv->v,(P)m[i][j]);
                   9147:                 max = MAX(e,max);
                   9148:             }
                   9149:     nd_setup_parameters(nvar,max);
                   9150:     dm = (NDV **)almat_pointer(n,n);
                   9151:     for ( i = 0, max = 1; i < n; i++ )
                   9152:         for ( j = 0; j < n; j++ ) {
                   9153:             dm[i][j] = ptondv(CO,fv,m[i][j]);
                   9154:             if ( mod ) ndv_mod(mod,dm[i][j]);
                   9155:             if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   9156:         }
                   9157:     d = ptondv(CO,fv,(P)ONE);
                   9158:     if ( mod ) ndv_mod(mod,d);
                   9159:     chsgnz(ONE,&mone);
                   9160:     for ( j = 0, sgn = 1; j < n; j++ ) {
                   9161:       if ( DP_Print ) {
                   9162:         fprintf(asir_out,".");
                   9163:       }
                   9164:         for ( i = j; i < n && !dm[i][j]; i++ );
                   9165:         if ( i == n ) {
                   9166:             *rp = 0;
                   9167:             return;
                   9168:         }
                   9169:         k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
                   9170:         for ( k = j; k < n; k++ )
                   9171:             for ( l = j; l < n; l++ )
                   9172:                 if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   9173:                     k0 = k; l0 = l; len0 = LEN(dm[k][l]);
                   9174:                 }
                   9175:         if ( k0 != j ) {
                   9176:             t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   9177:             sgn = -sgn;
                   9178:         }
                   9179:         if ( l0 != j ) {
                   9180:             for ( k = j; k < n; k++ ) {
                   9181:                 s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   9182:             }
                   9183:             sgn = -sgn;
                   9184:         }
                   9185:         bound = nd_det_compute_bound(dm,n,j);
                   9186:         for ( k = 0; k < nd_nvar; k++ )
                   9187:             if ( bound[k]*2 > nd_mask0 ) break;
                   9188:         if ( k < nd_nvar )
                   9189:             nd_det_reconstruct(dm,n,j,d);
                   9190:
                   9191:         for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
                   9192: /*            if ( DP_Print ) fprintf(asir_out,"    i=%d\n        ",i); */
                   9193:             mi = dm[i]; mij = mi[j];
                   9194:             if ( mod )
                   9195:                 ndv_mul_c(mod,mij,mod-1);
                   9196:             else
                   9197:                 ndv_mul_c_q(mij,mone);
                   9198:             for ( k = j+1; k < n; k++ ) {
                   9199: /*                if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
                   9200:                 bucket = create_pbucket();
                   9201:                 if ( mi[k] ) {
                   9202:                     nmv = BDY(mjj); len = LEN(mjj);
                   9203:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   9204:                         u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   9205:                         add_pbucket(mod,bucket,u);
                   9206:                     }
                   9207:                 }
                   9208:                 if ( mj[k] && mij ) {
                   9209:                     nmv = BDY(mij); len = LEN(mij);
                   9210:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   9211:                         u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   9212:                         add_pbucket(mod,bucket,u);
                   9213:                     }
                   9214:                 }
                   9215:                 u = nd_quo(mod,bucket,d);
                   9216:                 mi[k] = ndtondv(mod,u);
                   9217:             }
                   9218: /*            if ( DP_Print ) fprintf(asir_out,"\n",k); */
                   9219:         }
                   9220:         d = mjj;
                   9221:     }
                   9222:     if ( DP_Print ) {
                   9223:       fprintf(asir_out,"\n");
                   9224:     }
                   9225:     if ( sgn < 0 ) {
                   9226:         if ( mod )
                   9227:             ndv_mul_c(mod,d,mod-1);
                   9228:         else
                   9229:             ndv_mul_c_q(d,mone);
                   9230:     }
                   9231:     r = ndvtop(mod,CO,fv,d);
                   9232:     if ( !mod && !UNIQ(dq) )
                   9233:         divsp(CO,r,(P)dq,rp);
                   9234:     else
                   9235:         *rp = r;
                   9236: }
                   9237:
                   9238: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
                   9239: {
                   9240:     NM mr,mr0;
                   9241:     NM tnm;
                   9242:     NMV m;
                   9243:     UINT *d0,*dt,*dm;
                   9244:     int c,n,td,i,c1,c2,len;
                   9245:     Z q;
                   9246:     ND r;
                   9247:
                   9248:     if ( !p ) return 0;
                   9249:     else {
                   9250:         n = NV(p); m = BDY(p); len = LEN(p);
                   9251:         d0 = DL(m0);
                   9252:         td = TD(d);
                   9253:         mr0 = 0;
                   9254:         NEWNM(tnm);
                   9255:         if ( mod ) {
                   9256:             c = CM(m0);
                   9257:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9258:                 ndl_add(DL(m),d0,DL(tnm));
                   9259:                 if ( ndl_reducible(DL(tnm),d) ) {
                   9260:                     NEXTNM(mr0,mr);
                   9261:                     c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
                   9262:                     ndl_copy(DL(tnm),DL(mr));
                   9263:                 }
                   9264:             }
                   9265:         } else {
1.6       noro     9266:             q = CZ(m0);
1.1       noro     9267:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   9268:                 ndl_add(DL(m),d0,DL(tnm));
                   9269:                 if ( ndl_reducible(DL(tnm),d) ) {
                   9270:                     NEXTNM(mr0,mr);
1.6       noro     9271:                     mulz(CZ(m),q,&CZ(mr));
1.1       noro     9272:                     ndl_copy(DL(tnm),DL(mr));
                   9273:                 }
                   9274:             }
                   9275:         }
                   9276:         if ( !mr0 )
                   9277:             return 0;
                   9278:         else {
                   9279:             NEXT(mr) = 0;
                   9280:             for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   9281:             MKND(NV(p),mr0,len,r);
                   9282:             SG(r) = SG(p) + TD(d0);
                   9283:             return r;
                   9284:         }
                   9285:     }
                   9286: }
                   9287:
                   9288: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   9289: {
                   9290:     int i,obpe,oadv,h,k,l;
                   9291:     static NM prev_nm_free_list;
                   9292:     EPOS oepos;
                   9293:
                   9294:     obpe = nd_bpe;
                   9295:     oadv = nmv_adv;
                   9296:     oepos = nd_epos;
                   9297:     if ( obpe < 2 ) nd_bpe = 2;
                   9298:     else if ( obpe < 3 ) nd_bpe = 3;
                   9299:     else if ( obpe < 4 ) nd_bpe = 4;
                   9300:     else if ( obpe < 5 ) nd_bpe = 5;
                   9301:     else if ( obpe < 6 ) nd_bpe = 6;
                   9302:     else if ( obpe < 8 ) nd_bpe = 8;
                   9303:     else if ( obpe < 10 ) nd_bpe = 10;
                   9304:     else if ( obpe < 16 ) nd_bpe = 16;
                   9305:     else if ( obpe < 32 ) nd_bpe = 32;
                   9306:     else error("nd_det_reconstruct : exponent too large");
                   9307:
                   9308:     nd_setup_parameters(nd_nvar,0);
                   9309:     prev_nm_free_list = _nm_free_list;
                   9310:     _nm_free_list = 0;
                   9311:     for ( k = j; k < n; k++ )
                   9312:         for (l = j; l < n; l++ )
                   9313:             ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   9314:     ndv_realloc(d,obpe,oadv,oepos);
                   9315:     prev_nm_free_list = 0;
                   9316: #if 0
                   9317:     GC_gcollect();
                   9318: #endif
                   9319: }
                   9320:
                   9321: /* returns a UINT array containing degree bounds */
                   9322:
                   9323: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   9324: {
                   9325:     UINT *d0,*d1,*d,*t,*r;
                   9326:     int k,l,i;
                   9327:
                   9328:     d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
                   9329:     for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
                   9330:     for ( k = j; k < n; k++ )
                   9331:         for ( l = j; l < n; l++ )
                   9332:             if ( dm[k][l] ) {
                   9333:                 d = ndv_compute_bound(dm[k][l]);
                   9334:                 for ( i = 0; i < nd_nvar; i++ )
                   9335:                     d0[i] = MAX(d0[i],d[i]);
                   9336:             }
                   9337:     return d0;
                   9338: }
                   9339:
                   9340: DL nd_separate_d(UINT *d,UINT *trans)
                   9341: {
                   9342:     int n,td,i,e,j;
                   9343:     DL a;
                   9344:
                   9345:     ndl_zero(trans);
                   9346:     td = 0;
                   9347:     for ( i = 0; i < nd_ntrans; i++ ) {
                   9348:         e = GET_EXP(d,i);
                   9349:         PUT_EXP(trans,i,e);
                   9350:         td += MUL_WEIGHT(e,i);
                   9351:     }
                   9352:     if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   9353:         /* homogenized */
                   9354:         i = nd_nvar-1;
                   9355:         e = GET_EXP(d,i);
                   9356:         PUT_EXP(trans,i,e);
                   9357:         td += MUL_WEIGHT(e,i);
                   9358:     }
                   9359:     TD(trans) = td;
                   9360:     if ( nd_blockmask) ndl_weight_mask(trans);
                   9361:     NEWDL(a,nd_nalg);
                   9362:     td = 0;
                   9363:     for ( i = 0; i < nd_nalg; i++ ) {
                   9364:         j = nd_ntrans+i;
                   9365:         e = GET_EXP(d,j);
                   9366:         a->d[i] = e;
                   9367:         td += e;
                   9368:     }
                   9369:     a->td = td;
                   9370:     return a;
                   9371: }
                   9372:
                   9373: int nd_monic(int mod,ND *p)
                   9374: {
                   9375:     UINT *trans,*t;
                   9376:     DL alg;
                   9377:     MP mp0,mp;
                   9378:     NM m,m0,m1,ma0,ma,mb,mr0,mr;
                   9379:     ND r;
                   9380:     DL dl;
                   9381:     DP nm;
                   9382:     NDV ndv;
                   9383:     DAlg inv,cd;
                   9384:     ND s,c;
                   9385:     Z l,mul;
                   9386:     Z ln;
                   9387:     int n,ntrans,i,e,td,is_lc,len;
                   9388:     NumberField nf;
                   9389:     struct oEGT eg0,eg1;
                   9390:
                   9391:     if ( !(nf = get_numberfield()) )
                   9392:         error("nd_monic : current_numberfield is not set");
                   9393:
                   9394:     /* Q coef -> DAlg coef */
                   9395:     NEWNM(ma0); ma = ma0;
                   9396:     m = BDY(*p);
                   9397:     is_lc = 1;
                   9398:     while ( 1 ) {
                   9399:         NEWMP(mp0); mp = mp0;
1.6       noro     9400:         mp->c = (Obj)CZ(m);
1.1       noro     9401:         mp->dl = nd_separate_d(DL(m),DL(ma));
                   9402:         NEWNM(mb);
                   9403:         for ( m = NEXT(m); m; m = NEXT(m) ) {
                   9404:             alg = nd_separate_d(DL(m),DL(mb));
                   9405:             if ( !ndl_equal(DL(ma),DL(mb)) )
                   9406:                 break;
1.6       noro     9407:             NEXTMP(mp0,mp); mp->c = (Obj)CZ(m); mp->dl = alg;
1.1       noro     9408:         }
                   9409:         NEXT(mp) = 0;
                   9410:         MKDP(nd_nalg,mp0,nm);
                   9411:         MKDAlg(nm,ONE,cd);
                   9412:         if ( is_lc == 1 ) {
                   9413:             /* if the lc is a rational number, we have nothing to do */
                   9414:             if ( !mp0->dl->td )
                   9415:                 return 1;
                   9416:
                   9417:             get_eg(&eg0);
                   9418:             invdalg(cd,&inv);
                   9419:             get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   9420:             /* check the validity of inv */
                   9421:             if ( mod && !remqi((Q)inv->dn,mod) )
                   9422:                 return 0;
                   9423:             CA(ma) = nf->one;
                   9424:             is_lc = 0;
                   9425:             ln = ONE;
                   9426:         } else {
                   9427:             muldalg(cd,inv,&CA(ma));
                   9428:             lcmz(ln,CA(ma)->dn,&ln);
                   9429:         }
                   9430:         if ( m ) {
                   9431:             NEXT(ma) = mb; ma = mb;
                   9432:         } else {
                   9433:             NEXT(ma) = 0;
                   9434:             break;
                   9435:         }
                   9436:     }
                   9437:     /* l = lcm(denoms) */
                   9438:     l = ln;
                   9439:     for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
1.6       noro     9440:         divsz(l,CA(m)->dn,&mul);
1.1       noro     9441:         for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   9442:             NEXTNM(mr0,mr);
1.6       noro     9443:             mulz((Z)mp->c,mul,&CZ(mr));
1.1       noro     9444:             dl = mp->dl;
                   9445:             td = TD(DL(m));
                   9446:             ndl_copy(DL(m),DL(mr));
                   9447:             for ( i = 0; i < nd_nalg; i++ ) {
                   9448:                 e = dl->d[i];
                   9449:                 PUT_EXP(DL(mr),i+nd_ntrans,e);
                   9450:                 td += MUL_WEIGHT(e,i+nd_ntrans);
                   9451:             }
                   9452:             if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
                   9453:             TD(DL(mr)) = td;
                   9454:             if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   9455:         }
                   9456:     }
                   9457:     NEXT(mr) = 0;
                   9458:     for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   9459:     MKND(NV(*p),mr0,len,r);
                   9460:     /* XXX */
                   9461:     SG(r) = SG(*p);
                   9462:     nd_free(*p);
                   9463:     *p = r;
                   9464:     return 1;
                   9465: }
                   9466:
                   9467: NODE reverse_node(NODE n)
                   9468: {
                   9469:     NODE t,t1;
                   9470:
                   9471:     for ( t = 0; n; n = NEXT(n) ) {
                   9472:         MKNODE(t1,BDY(n),t); t = t1;
                   9473:     }
                   9474:     return t;
                   9475: }
                   9476:
                   9477: P ndc_div(int mod,union oNDC a,union oNDC b)
                   9478: {
                   9479:     union oNDC c;
                   9480:     int inv,t;
                   9481:
                   9482:     if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.10      noro     9483:     else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.1       noro     9484:     else if ( mod ) {
                   9485:         inv = invm(b.m,mod);
                   9486:         DMAR(a.m,inv,0,mod,t); c.m = t;
                   9487:     } else if ( nd_vc )
                   9488:        divsp(nd_vc,a.p,b.p,&c.p);
                   9489:     else
                   9490:        divsz(a.z,b.z,&c.z);
                   9491:     return ndctop(mod,c);
                   9492: }
                   9493:
                   9494: P ndctop(int mod,union oNDC c)
                   9495: {
                   9496:     Z q;
                   9497:     int e;
                   9498:     GFS gfs;
                   9499:
                   9500:     if ( mod == -1 ) {
                   9501:         e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
                   9502:     } else if ( mod == -2 ) {
1.10      noro     9503:        q = c.z; return (P)q;
1.1       noro     9504:     } else if ( mod > 0 ) {
1.6       noro     9505:         STOZ(c.m,q); return (P)q;
1.1       noro     9506:     } else
                   9507:         return (P)c.p;
                   9508: }
                   9509:
                   9510: /* [0,0,0,cont] = p -> p/cont */
                   9511:
                   9512: void finalize_tracelist(int i,P cont)
                   9513: {
                   9514:  LIST l;
                   9515:  NODE node;
                   9516:  Z iq;
                   9517:
                   9518:   if ( !UNIQ(cont) ) {
                   9519:     node = mknode(4,NULLP,NULLP,NULLP,cont);
                   9520:     MKLIST(l,node); MKNODE(node,l,nd_tracelist);
                   9521:     nd_tracelist = node;
                   9522:   }
1.6       noro     9523:   STOZ(i,iq);
1.1       noro     9524:   nd_tracelist = reverse_node(nd_tracelist);
                   9525:   MKLIST(l,nd_tracelist);
                   9526:   node = mknode(2,iq,l); MKLIST(l,node);
                   9527:   MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
                   9528:   nd_alltracelist = node; nd_tracelist = 0;
                   9529: }
                   9530:
                   9531: void conv_ilist(int demand,int trace,NODE g,int **indp)
                   9532: {
                   9533:     int n,i,j;
                   9534:   int *ind;
                   9535:   NODE t;
                   9536:
                   9537:     n = length(g);
                   9538:   ind = (int *)MALLOC(n*sizeof(int));
                   9539:   for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
                   9540:     j = (long)BDY(t); ind[i] = j;
                   9541:     BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
                   9542:   }
                   9543:   if ( indp ) *indp = ind;
                   9544: }
                   9545:
1.30      noro     9546: NODE conv_ilist_s(int demand,int trace,int **indp)
                   9547: {
                   9548:   int n,i,j;
                   9549:   int *ind;
                   9550:   NODE g0,g;
                   9551:
                   9552:   n = nd_psn;
                   9553:   ind = (int *)MALLOC(n*sizeof(int));
                   9554:   g0 = 0;
                   9555:   for ( i = 0; i < n; i++ ) {
                   9556:     ind[i] = i;
                   9557:     NEXTNODE(g0,g);
                   9558:     BDY(g) = (pointer)(demand?ndv_load(i):(trace?nd_ps_trace[i]:nd_ps[i]));
                   9559:   }
                   9560:   if ( g0 ) NEXT(g) = 0;
                   9561:   if ( indp ) *indp = ind;
                   9562:   return g0;
                   9563: }
                   9564:
1.1       noro     9565: void parse_nd_option(NODE opt)
                   9566: {
1.38    ! noro     9567:   NODE t,p,u;
1.1       noro     9568:   int i,s,n;
1.38    ! noro     9569:   char *key;
        !          9570:   Obj value;
1.1       noro     9571:
1.38    ! noro     9572:   nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
1.1       noro     9573:   nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
                   9574:   nd_splist = 0; nd_check_splist = 0;
1.38    ! noro     9575:   nd_sugarweight = 0; nd_f4red =0; nd_rank0 = 0;
        !          9576:   nd_f4_td = 0; nd_sba_f4step = 2; nd_sba_pot = 0; nd_sba_largelcm = 0;
        !          9577:   for ( t = opt; t; t = NEXT(t) ) {
        !          9578:     p = BDY((LIST)BDY(t));
        !          9579:     key = BDY((STRING)BDY(p));
        !          9580:     value = (Obj)BDY(NEXT(p));
        !          9581:     if ( !strcmp(key,"gentrace") )
        !          9582:       nd_gentrace = value?1:0;
        !          9583:     else if ( !strcmp(key,"gensyz") )
        !          9584:       nd_gensyz = value?1:0;
        !          9585:     else if ( !strcmp(key,"nora") )
        !          9586:       nd_nora = value?1:0;
        !          9587:     else if ( !strcmp(key,"gbblock") ) {
        !          9588:       if ( value && OID(value) == O_LIST ) {
1.1       noro     9589:         u = BDY((LIST)value);
1.38    ! noro     9590:         nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.1       noro     9591:         for ( i = 0; u; u = NEXT(u) ) {
                   9592:           p = BDY((LIST)BDY(u));
1.6       noro     9593:           s = nd_gbblock[i++] = ZTOS((Q)BDY(p));
                   9594:           nd_gbblock[i++] = s+ZTOS((Q)BDY(NEXT(p)))-1;
1.1       noro     9595:         }
                   9596:         nd_gbblock[i] = -1;
1.38    ! noro     9597:       } else
        !          9598:         nd_gbblock = 0;
1.1       noro     9599:     } else if ( !strcmp(key,"newelim") )
                   9600:             nd_newelim = value?1:0;
                   9601:     else if ( !strcmp(key,"intersect") )
                   9602:             nd_intersect = value?1:0;
1.17      noro     9603:     else if ( !strcmp(key,"syzgen") )
                   9604:             nd_intersect = ZTOS((Q)value);
1.1       noro     9605:     else if ( !strcmp(key,"lf") )
                   9606:             nd_lf = value?1:0;
                   9607:     else if ( !strcmp(key,"trace") ) {
1.38    ! noro     9608:       if ( value ) {
        !          9609:         u = BDY((LIST)value);
        !          9610:         nd_nzlist = BDY((LIST)ARG2(u));
        !          9611:         nd_bpe = ZTOS((Q)ARG3(u));
        !          9612:       }
1.1       noro     9613:     } else if ( !strcmp(key,"f4red") ) {
1.38    ! noro     9614:       nd_f4red = ZTOS((Q)value);
1.1       noro     9615:     } else if ( !strcmp(key,"rank0") ) {
1.38    ! noro     9616:       nd_rank0 = value?1:0;
1.1       noro     9617:     } else if ( !strcmp(key,"splist") ) {
1.38    ! noro     9618:       nd_splist = value?1:0;
1.1       noro     9619:     } else if ( !strcmp(key,"check_splist") ) {
                   9620:       nd_check_splist = BDY((LIST)value);
                   9621:     } else if ( !strcmp(key,"sugarweight") ) {
                   9622:       u = BDY((LIST)value);
1.38    ! noro     9623:       n = length(u);
        !          9624:       nd_sugarweight = MALLOC(n*sizeof(int));
1.1       noro     9625:       for ( i = 0; i < n; i++, u = NEXT(u) )
1.38    ! noro     9626:         nd_sugarweight[i] = ZTOS((Q)BDY(u));
        !          9627:     } else if ( !strcmp(key,"f4_td") ) {
        !          9628:       nd_f4_td = value?1:0;
        !          9629:     } else if ( !strcmp(key,"sba_f4step") ) {
        !          9630:       nd_sba_f4step = value?ZTOS((Q)value):0;
        !          9631:     } else if ( !strcmp(key,"sba_pot") ) {
        !          9632:       nd_sba_pot = value?1:0;
        !          9633:     } else if ( !strcmp(key,"sba_largelcm") ) {
        !          9634:       nd_sba_largelcm = value?1:0;
1.1       noro     9635:     }
1.38    ! noro     9636:   }
1.1       noro     9637: }
                   9638:
                   9639: ND mdptond(DP d);
                   9640: ND nd_mul_nm(int mod,NM m0,ND p);
                   9641: ND nd_mul_nm_lf(NM m0,ND p);
                   9642: ND *btog(NODE ti,ND **p,int nb,int mod);
                   9643: ND btog_one(NODE ti,ND *p,int nb,int mod);
                   9644: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
                   9645: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
                   9646:
                   9647: /* d:monomial */
                   9648: ND mdptond(DP d)
                   9649: {
                   9650:   NM m;
                   9651:   ND r;
                   9652:
                   9653:   if ( OID(d) == 1 )
                   9654:   r = ptond(CO,CO,(P)d);
                   9655:   else {
                   9656:     NEWNM(m);
                   9657:     dltondl(NV(d),BDY(d)->dl,DL(m));
1.6       noro     9658:     CZ(m) = (Z)BDY(d)->c;
1.1       noro     9659:     NEXT(m) = 0;
                   9660:     MKND(NV(d),m,1,r);
                   9661:   }
                   9662:     return r;
                   9663: }
                   9664:
                   9665: ND nd_mul_nm(int mod,NM m0,ND p)
                   9666: {
                   9667:   UINT *d0;
                   9668:   int c0,c1,c;
                   9669:   NM tm,mr,mr0;
                   9670:   ND r;
                   9671:
                   9672:   if ( !p ) return 0;
                   9673:   d0 = DL(m0);
                   9674:   c0 = CM(m0);
                   9675:   mr0 = 0;
                   9676:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   9677:     NEXTNM(mr0,mr);
                   9678:   c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
                   9679:   ndl_add(d0,DL(tm),DL(mr));
                   9680:   }
                   9681:   NEXT(mr) = 0;
                   9682:   MKND(NV(p),mr0,LEN(p),r);
                   9683:   return r;
                   9684: }
                   9685:
                   9686: ND nd_mul_nm_lf(NM m0,ND p)
                   9687: {
                   9688:   UINT *d0;
                   9689:   Z c0,c1,c;
                   9690:   NM tm,mr,mr0;
                   9691:   ND r;
                   9692:
                   9693:   if ( !p ) return 0;
                   9694:   d0 = DL(m0);
                   9695:   c0 = CZ(m0);
                   9696:   mr0 = 0;
                   9697:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   9698:     NEXTNM(mr0,mr);
                   9699:     c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
                   9700:     ndl_add(d0,DL(tm),DL(mr));
                   9701:   }
                   9702:   NEXT(mr) = 0;
                   9703:   MKND(NV(p),mr0,LEN(p),r);
                   9704:   return r;
                   9705: }
                   9706:
                   9707: ND *btog(NODE ti,ND **p,int nb,int mod)
                   9708: {
                   9709:   PGeoBucket *r;
                   9710:   int i,ci;
                   9711:   NODE t,s;
                   9712:   ND m,tp;
                   9713:   ND *pi,*rd;
                   9714:   P c;
                   9715:
                   9716:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   9717:   for ( i = 0; i < nb; i++ )
                   9718:     r[i] = create_pbucket();
                   9719:   for ( t = ti; t; t = NEXT(t) ) {
                   9720:   s = BDY((LIST)BDY(t));
                   9721:     if ( ARG0(s) ) {
                   9722:     m = mdptond((DP)ARG2(s));
1.6       noro     9723:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     9724:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   9725:       HCM(m) = ci;
1.6       noro     9726:       pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     9727:       for ( i = 0; i < nb; i++ ) {
                   9728:       tp = nd_mul_nm(mod,BDY(m),pi[i]);
                   9729:         add_pbucket(mod,r[i],tp);
                   9730:       }
                   9731:     }
                   9732:     ci = 1;
                   9733:     } else {
                   9734:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   9735:     ci = invm(ci,mod);
                   9736:   }
                   9737:   }
                   9738:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   9739:   for ( i = 0; i < nb; i++ )
                   9740:   rd[i] = normalize_pbucket(mod,r[i]);
                   9741:   if ( ci != 1 )
                   9742:     for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
                   9743:    return rd;
                   9744: }
                   9745:
                   9746: /* YYY */
                   9747: ND *btog_lf(NODE ti,ND **p,int nb)
                   9748: {
                   9749:   PGeoBucket *r;
                   9750:   int i;
                   9751:   NODE t,s;
                   9752:   ND m,tp;
                   9753:   ND *pi,*rd;
                   9754:   LM lm;
                   9755:   Z lf,c;
                   9756:
                   9757:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   9758:   for ( i = 0; i < nb; i++ )
                   9759:     r[i] = create_pbucket();
                   9760:   for ( t = ti; t; t = NEXT(t) ) {
                   9761:     s = BDY((LIST)BDY(t));
                   9762:     if ( ARG0(s) ) {
                   9763:       m = mdptond((DP)ARG2(s));
1.6       noro     9764:       simp_ff((Obj)HCZ(m),(Obj *)&lm);
1.1       noro     9765:       if ( lm ) {
                   9766:         lmtolf(lm,&lf); HCZ(m) = lf;
1.6       noro     9767:         pi = p[ZTOS((Q)ARG1(s))];
1.1       noro     9768:         for ( i = 0; i < nb; i++ ) {
                   9769:           tp = nd_mul_nm_lf(BDY(m),pi[i]);
                   9770:           add_pbucket(-2,r[i],tp);
                   9771:         }
                   9772:       }
                   9773:       c = ONE;
                   9774:     } else {
                   9775:       simp_ff((Obj)ARG3(s),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&c);
                   9776:     }
                   9777:   }
                   9778:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   9779:   for ( i = 0; i < nb; i++ )
                   9780:     rd[i] = normalize_pbucket(-2,r[i]);
                   9781:   for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
                   9782:   return rd;
                   9783: }
                   9784:
                   9785: ND btog_one(NODE ti,ND *p,int nb,int mod)
                   9786: {
                   9787:   PGeoBucket r;
                   9788:   int i,ci,j;
                   9789:   NODE t,s;
                   9790:   ND m,tp;
                   9791:   ND pi,rd;
                   9792:   P c;
                   9793:
                   9794:   r = create_pbucket();
                   9795:   for ( t = ti; t; t = NEXT(t) ) {
                   9796:   s = BDY((LIST)BDY(t));
                   9797:     if ( ARG0(s) ) {
                   9798:     m = mdptond((DP)ARG2(s));
1.6       noro     9799:     ptomp(mod,(P)HCZ(m),&c);
1.1       noro     9800:     if ( (ci = ((MQ)c)->cont) != 0 ) {
                   9801:       HCM(m) = ci;
1.6       noro     9802:       pi = p[j=ZTOS((Q)ARG1(s))];
1.1       noro     9803:     if ( !pi ) {
                   9804:       pi = nd_load_mod(j);
                   9805:       tp = nd_mul_nm(mod,BDY(m),pi);
                   9806:       nd_free(pi);
                   9807:         add_pbucket(mod,r,tp);
                   9808:     } else {
                   9809:       tp = nd_mul_nm(mod,BDY(m),pi);
                   9810:         add_pbucket(mod,r,tp);
                   9811:       }
                   9812:     }
                   9813:     ci = 1;
                   9814:     } else {
                   9815:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   9816:     ci = invm(ci,mod);
                   9817:   }
                   9818:   }
                   9819:   rd = normalize_pbucket(mod,r);
                   9820:   free_pbucket(r);
                   9821:   if ( ci != 1 ) nd_mul_c(mod,rd,ci);
                   9822:   return rd;
                   9823: }
                   9824:
                   9825: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
                   9826:
                   9827: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
                   9828: {
                   9829:   int i,j,n,m,nb,pi0,pi1,nvar;
                   9830:   VL fv,tv,vv;
                   9831:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   9832:   ND **p;
                   9833:   ND *c;
                   9834:   ND u;
                   9835:   P inv;
                   9836:   MAT mat;
                   9837:
                   9838:   if ( mod == -2 )
                   9839:     return nd_btog_lf(f,v,ord,tlist,rp);
                   9840:
                   9841:   parse_nd_option(current_option);
                   9842:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   9843:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   9844:   switch ( ord->id ) {
                   9845:     case 1:
                   9846:       if ( ord->nv != nvar )
                   9847:         error("nd_check : invalid order specification");
                   9848:       break;
                   9849:     default:
                   9850:       break;
                   9851:   }
                   9852:   nd_init_ord(ord);
                   9853: #if 0
1.6       noro     9854:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     9855: #else
                   9856:   nd_bpe = 32;
                   9857: #endif
                   9858:   nd_setup_parameters(nvar,0);
                   9859:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   9860:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   9861:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   9862:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   9863:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     9864:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     9865:   if ( j > i ) i = j;
                   9866:   }
                   9867:   n = i+1;
                   9868:   nb = length(BDY(f));
                   9869:   p = (ND **)MALLOC(n*sizeof(ND *));
                   9870:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   9871:     pi = BDY((LIST)BDY(t));
1.6       noro     9872:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     9873:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   9874:     ptomp(mod,(P)ARG2(pi),&inv);
                   9875:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   9876:     u = ptond(CO,vv,(P)ONE);
                   9877:     HCM(u) = ((MQ)inv)->cont;
                   9878:     c[pi1] = u;
                   9879:   }
                   9880:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   9881:     printf("%d ",i); fflush(stdout);
                   9882:     ti = BDY((LIST)BDY(t));
1.6       noro     9883:     p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     9884:   }
                   9885:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   9886:     printf("%d ",i); fflush(stdout);
                   9887:     ti = BDY((LIST)BDY(t));
1.6       noro     9888:     p[j=ZTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     9889:   }
                   9890:   m = length(ind);
                   9891:   MKMAT(mat,nb,m);
                   9892:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     9893:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     9894:       BDY(mat)[i][j] = ndtodp(mod,c[i]);
                   9895:   return mat;
                   9896: }
                   9897:
                   9898: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
                   9899: {
                   9900:   int i,j,n,m,nb,pi0,pi1,nvar;
                   9901:   VL fv,tv,vv;
                   9902:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   9903:   ND **p;
                   9904:   ND *c;
                   9905:   ND u;
                   9906:   MAT mat;
                   9907:   LM lm;
                   9908:   Z lf,inv;
                   9909:
                   9910:   parse_nd_option(current_option);
                   9911:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   9912:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   9913:   switch ( ord->id ) {
                   9914:     case 1:
                   9915:       if ( ord->nv != nvar )
                   9916:         error("nd_check : invalid order specification");
                   9917:       break;
                   9918:     default:
                   9919:       break;
                   9920:   }
                   9921:   nd_init_ord(ord);
                   9922: #if 0
1.6       noro     9923:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     9924: #else
                   9925:   nd_bpe = 32;
                   9926: #endif
                   9927:   nd_setup_parameters(nvar,0);
                   9928:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   9929:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   9930:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   9931:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   9932:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     9933:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     9934:   if ( j > i ) i = j;
                   9935:   }
                   9936:   n = i+1;
                   9937:   nb = length(BDY(f));
                   9938:   p = (ND **)MALLOC(n*sizeof(ND *));
                   9939:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   9940:     pi = BDY((LIST)BDY(t));
1.6       noro     9941:     pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     9942:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   9943:     simp_ff((Obj)ARG2(pi),(Obj *)&lm); lmtolf(lm,&lf); invz(lf,current_mod_lf,&inv);
                   9944:     u = ptond(CO,vv,(P)ONE);
                   9945:     HCZ(u) = inv;
                   9946:     c[pi1] = u;
                   9947:   }
                   9948:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   9949:     printf("%d ",i); fflush(stdout);
                   9950:     ti = BDY((LIST)BDY(t));
1.6       noro     9951:     p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     9952:   }
                   9953:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   9954:     printf("%d ",i); fflush(stdout);
                   9955:     ti = BDY((LIST)BDY(t));
1.6       noro     9956:     p[j=ZTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
1.1       noro     9957:   }
                   9958:   m = length(ind);
                   9959:   MKMAT(mat,nb,m);
                   9960:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
1.6       noro     9961:     for ( i = 0, c = p[ZTOS((Q)BDY(t))]; i < nb; i++ )
1.1       noro     9962:       BDY(mat)[i][j] = ndtodp(-2,c[i]);
                   9963:   return mat;
                   9964: }
                   9965:
                   9966: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
                   9967:   LIST tlist,int pos,MAT *rp)
                   9968: {
                   9969:   int i,j,n,m,nb,pi0,pi1,nvar;
                   9970:   VL fv,tv,vv;
                   9971:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   9972:   ND *p;
                   9973:   ND *c;
                   9974:   ND u;
                   9975:   P inv;
                   9976:   VECT vect;
                   9977:
                   9978:   if ( mod == -2 )
                   9979:     error("nd_btog_one : not implemented yet for a large finite field");
                   9980:
                   9981:   parse_nd_option(current_option);
                   9982:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   9983:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   9984:   switch ( ord->id ) {
                   9985:     case 1:
                   9986:       if ( ord->nv != nvar )
                   9987:         error("nd_check : invalid order specification");
                   9988:       break;
                   9989:     default:
                   9990:       break;
                   9991:   }
                   9992:   nd_init_ord(ord);
                   9993: #if 0
1.6       noro     9994:   nd_bpe = ZTOS((Q)ARG7(BDY(tlist)));
1.1       noro     9995: #else
                   9996:   nd_bpe = 32;
                   9997: #endif
                   9998:   nd_setup_parameters(nvar,0);
                   9999:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   10000:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   10001:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   10002:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   10003:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.6       noro     10004:     j = ZTOS((Q)BDY(BDY((LIST)BDY(t))));
1.1       noro     10005:   if ( j > i ) i = j;
                   10006:   }
                   10007:   n = i+1;
                   10008:   nb = length(BDY(f));
                   10009:   p = (ND *)MALLOC(n*sizeof(ND *));
                   10010:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   10011:     pi = BDY((LIST)BDY(t));
1.6       noro     10012:   pi0 = ZTOS((Q)ARG0(pi)); pi1 = ZTOS((Q)ARG1(pi));
1.1       noro     10013:   if ( pi1 == pos ) {
                   10014:     ptomp(mod,(P)ARG2(pi),&inv);
                   10015:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   10016:     u = ptond(CO,vv,(P)ONE);
                   10017:     HCM(u) = ((MQ)inv)->cont;
                   10018:     p[pi0] = u;
                   10019:     }
                   10020:   }
                   10021:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   10022:   printf("%d ",i); fflush(stdout);
                   10023:     ti = BDY((LIST)BDY(t));
1.6       noro     10024:     p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10025:     if ( Demand ) {
                   10026:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   10027:   }
                   10028:   }
                   10029:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   10030:   printf("%d ",i); fflush(stdout);
                   10031:     ti = BDY((LIST)BDY(t));
1.6       noro     10032:     p[j=ZTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.1       noro     10033:     if ( Demand ) {
                   10034:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   10035:   }
                   10036:   }
                   10037:   m = length(ind);
                   10038:   MKVECT(vect,m);
                   10039:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.6       noro     10040:   u = p[ZTOS((Q)BDY(t))];
1.1       noro     10041:   if ( !u ) {
1.6       noro     10042:     u = nd_load_mod(ZTOS((Q)BDY(t)));
1.1       noro     10043:     BDY(vect)[j] = ndtodp(mod,u);
                   10044:     nd_free(u);
                   10045:   } else
                   10046:     BDY(vect)[j] = ndtodp(mod,u);
                   10047:   }
                   10048:   return vect;
                   10049: }
                   10050:
                   10051: void ndv_print_lf(NDV p)
                   10052: {
                   10053:     NMV m;
                   10054:     int i,len;
                   10055:
                   10056:     if ( !p ) printf("0\n");
                   10057:     else {
                   10058:         len = LEN(p);
                   10059:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   10060:             printf("+");
                   10061:             mpz_out_str(asir_out,10,BDY(CZ(m)));
                   10062:             printf("*");
                   10063:             ndl_print(DL(m));
                   10064:         }
                   10065:         printf("\n");
                   10066:     }
                   10067: }
                   10068:
                   10069: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
                   10070: {
                   10071:     VL tv,fv,vv,vc,av;
                   10072:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   10073:     int m,nocheck,nvar,mindex,e,max;
                   10074:     NDV c;
                   10075:     NMV a;
                   10076:     P p,zp;
                   10077:     Q dmy;
                   10078:     EPOS oepos;
                   10079:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   10080:     Alg alpha,dp;
                   10081:     P poly;
                   10082:     LIST f1,f2,zpl;
                   10083:     Obj obj;
                   10084:     NumberField nf;
                   10085:     struct order_spec *ord1;
                   10086:     struct oEGT eg_check,eg0,eg1;
                   10087:     NODE tr,tl1,tl2,tl3,tl4;
                   10088:     LIST l1,l2,l3,l4,l5;
                   10089:     int *perm;
                   10090:     int j,ret;
                   10091:     NODE retn;
                   10092:     Q jq,bpe;
                   10093:
                   10094:     nd_module = 0;
                   10095:     parse_nd_option(current_option);
                   10096:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   10097:     if ( nd_vc )
                   10098:       error("nd_f4_lf_trace : computation over a rational function field is not implemented");
                   10099:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   10100:     switch ( ord->id ) {
                   10101:         case 1:
                   10102:             if ( ord->nv != nvar )
                   10103:                 error("nd_f4_lf_trace : invalid order specification");
                   10104:             break;
                   10105:         default:
                   10106:             break;
                   10107:     }
                   10108:
                   10109:     nd_ntrans = nvar;
                   10110:     nd_nalg = 0;
                   10111:
                   10112:     nocheck = 0;
                   10113:     mindex = 0;
                   10114:
                   10115:     /* do not use on-demand load/save */
                   10116:     nd_demand = 0;
                   10117:     m = trace > 1 ? trace : get_lprime(mindex);
                   10118:     nd_init_ord(ord);
                   10119:     mrank = 0;
                   10120:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   10121:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   10122:             if ( nd_module ) {
                   10123:                 s = BDY((LIST)BDY(t));
                   10124:                 trank = length(s);
                   10125:                 mrank = MAX(mrank,trank);
                   10126:                 for ( ; s; s = NEXT(s) ) {
                   10127:                     e = getdeg(tv->v,(P)BDY(s));
                   10128:                     max = MAX(e,max);
                   10129:                 }
                   10130:             } else {
                   10131:                 e = getdeg(tv->v,(P)BDY(t));
                   10132:                 max = MAX(e,max);
                   10133:             }
                   10134:         }
                   10135:     nd_setup_parameters(nvar,max);
                   10136:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   10137:     ishomo = 1;
                   10138:     /* XXX */
                   10139:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   10140:         if ( nd_module ) {
                   10141:             c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
                   10142:         } else {
                   10143:             c = (pointer)ptondv(CO,vv,(P)BDY(t));
                   10144:         }
                   10145:         if ( ishomo )
                   10146:             ishomo = ishomo && ndv_ishomo(c);
                   10147:         if ( c ) {
                   10148:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   10149:             ndv_mod(-2,c);
                   10150:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   10151:         }
                   10152:     }
                   10153:     if ( in0 ) NEXT(in) = 0;
                   10154:     if ( fd0 ) NEXT(fd) = 0;
                   10155:     if ( !ishomo && homo ) {
                   10156:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   10157:             c = (NDV)BDY(t); len = LEN(c);
                   10158:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   10159:                 wmax = MAX(TD(DL(a)),wmax);
                   10160:         }
                   10161:         homogenize_order(ord,nvar,&ord1);
                   10162:         nd_init_ord(ord1);
                   10163:         nd_setup_parameters(nvar+1,wmax);
                   10164:         for ( t = fd0; t; t = NEXT(t) )
                   10165:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   10166:     }
                   10167:     if ( MaxDeg > 0 ) nocheck = 1;
1.24      noro     10168:     ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0,0);
1.1       noro     10169:     if ( ret )
                   10170:       cand = nd_f4_lf_trace_main(m,&perm);
                   10171:     if ( !ret || !cand ) {
                   10172:        *rp = 0; return;
                   10173:     }
                   10174:     if ( !ishomo && homo ) {
                   10175:       /* dehomogenization */
                   10176:       for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   10177:       nd_init_ord(ord);
                   10178:       nd_setup_parameters(nvar,0);
                   10179:     }
                   10180:     cand = ndv_reducebase(cand,perm);
                   10181:     cand = ndv_reduceall(-2,cand);
                   10182:     cbpe = nd_bpe;
                   10183:     get_eg(&eg0);
                   10184:     if ( (ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand)) != 0 ) {
                   10185:       /* gbcheck : cand is a GB of Id(cand) ? */
                   10186:       retn = nd_f4(-2,0,0);
                   10187:     }
                   10188:   if ( !retn ) {
                   10189:       /* failure */
                   10190:       *rp = 0; return;
                   10191:     }
                   10192:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   10193:     if ( DP_Print )
1.5       noro     10194:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime);
1.1       noro     10195:     /* dp->p */
                   10196:     nd_bpe = cbpe;
                   10197:     nd_setup_parameters(nd_nvar,0);
                   10198:     for ( r = cand; r; r = NEXT(r) ) {
                   10199:       if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
                   10200:       else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
                   10201:     }
                   10202:     MKLIST(*rp,cand);
                   10203: }
                   10204:
                   10205: NODE nd_f4_lf_trace_main(int m,int **indp)
                   10206: {
                   10207:     int i,nh,stat,index;
                   10208:     NODE r,rm,g;
                   10209:     ND_pairs d,l,l0,t;
                   10210:     ND spol,red;
                   10211:     NDV nf,redv,nfqv,nfv;
                   10212:     NM s0,s;
                   10213:     NODE rp0,srp0,nflist,nflist_lf;
                   10214:     int nsp,nred,col,rank,len,k,j,a;
                   10215:     UINT c;
                   10216:     UINT **spmat;
                   10217:     UINT *s0vect,*svect,*p,*v;
                   10218:     int *colstat;
                   10219:     IndArray *imat;
                   10220:     int *rhead;
                   10221:     int spcol,sprow;
                   10222:     int sugar;
                   10223:     PGeoBucket bucket;
                   10224:     struct oEGT eg0,eg1,eg_f4;
                   10225:
                   10226:     g = 0; d = 0;
                   10227:     for ( i = 0; i < nd_psn; i++ ) {
                   10228:         d = update_pairs(d,g,i,0);
                   10229:         g = update_base(g,i);
                   10230:     }
                   10231:     while ( d ) {
                   10232:         get_eg(&eg0);
                   10233:         l = nd_minsugarp(d,&d);
                   10234:         sugar = SG(l);
                   10235:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   10236:         bucket = create_pbucket();
                   10237:         stat = nd_sp_f4(m,0,l,bucket);
                   10238:         if ( !stat ) {
                   10239:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10240:             NEXT(t) = d; d = l;
                   10241:             d = nd_reconstruct(1,d);
                   10242:             continue;
                   10243:         }
                   10244:         if ( bucket->m < 0 ) continue;
                   10245:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   10246:         if ( !col ) {
                   10247:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10248:             NEXT(t) = d; d = l;
                   10249:             d = nd_reconstruct(1,d);
                   10250:             continue;
                   10251:         }
                   10252:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   10253:         if ( DP_Print )
1.5       noro     10254:             fprintf(asir_out,"\nsugar=%d,symb=%.3fsec,",sugar,eg_f4.exectime);
1.1       noro     10255:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   10256:         if ( !l0 ) continue;
                   10257:         l = l0;
                   10258:
                   10259:         /* over LF */
                   10260:         bucket = create_pbucket();
                   10261:         stat = nd_sp_f4(-2,1,l,bucket);
                   10262:         if ( !stat ) {
                   10263:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10264:             NEXT(t) = d; d = l;
                   10265:             d = nd_reconstruct(1,d);
                   10266:             continue;
                   10267:         }
                   10268:         if ( bucket->m < 0 ) continue;
                   10269:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   10270:         if ( !col ) {
                   10271:             for ( t = l; NEXT(t); t = NEXT(t) );
                   10272:             NEXT(t) = d; d = l;
                   10273:             d = nd_reconstruct(1,d);
                   10274:             continue;
                   10275:         }
                   10276:         nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
                   10277:         /* adding new bases */
                   10278:         for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
                   10279:             nfv = (NDV)BDY(rm);
                   10280:             nfqv = (NDV)BDY(r);
                   10281:             if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
                   10282:             ndv_removecont(m,nfv);
                   10283:             ndv_removecont(-2,nfqv);
1.24      noro     10284:             nh = ndv_newps(-2,nfv,nfqv);
1.1       noro     10285:             d = update_pairs(d,g,nh,0);
                   10286:             g = update_base(g,nh);
                   10287:         }
                   10288:         if ( r || rm ) return 0;
                   10289:     }
                   10290:   conv_ilist(nd_demand,1,g,indp);
                   10291:     return g;
                   10292: }
                   10293:
1.7       noro     10294: #if SIZEOF_LONG==8
                   10295:
                   10296: NDV vect64_to_ndv(mp_limb_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   10297: {
                   10298:     int j,k,len;
                   10299:     UINT *p;
                   10300:     UINT c;
                   10301:     NDV r;
                   10302:     NMV mr0,mr;
                   10303:
                   10304:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   10305:     if ( !len ) return 0;
                   10306:     else {
                   10307:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   10308: #if 0
                   10309:         ndv_alloc += nmv_adv*len;
                   10310: #endif
                   10311:         mr = mr0;
                   10312:         p = s0vect;
                   10313:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   10314:             if ( !rhead[j] ) {
                   10315:                 if ( (c = (UINT)vect[k++]) != 0 ) {
                   10316:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   10317:                 }
                   10318:             }
                   10319:         MKNDV(nd_nvar,mr0,len,r);
                   10320:         return r;
                   10321:     }
                   10322: }
                   10323:
1.28      noro     10324: NDV vect64_to_ndv_s(mp_limb_t *vect,int col,UINT *s0vect)
                   10325: {
                   10326:     int j,k,len;
                   10327:     UINT *p;
                   10328:     UINT c;
                   10329:     NDV r;
                   10330:     NMV mr0,mr;
                   10331:
                   10332:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   10333:     if ( !len ) return 0;
                   10334:     else {
                   10335:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   10336:         mr = mr0;
                   10337:         p = s0vect;
                   10338:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   10339:           if ( (c = (UINT)vect[k++]) != 0 ) {
                   10340:             ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   10341:           }
                   10342:         MKNDV(nd_nvar,mr0,len,r);
                   10343:         return r;
                   10344:     }
                   10345: }
                   10346:
1.7       noro     10347: int nd_to_vect64(int mod,UINT *s0,int n,ND d,mp_limb_t *r)
                   10348: {
                   10349:     NM m;
1.11      noro     10350:     UINT *t,*s,*u;
                   10351:     int i,st,ed,md,prev,c;
1.7       noro     10352:
                   10353:     for ( i = 0; i < n; i++ ) r[i] = 0;
1.11      noro     10354:     prev = 0;
                   10355:     for ( i = 0, m = BDY(d); m; m = NEXT(m) ) {
                   10356:       t = DL(m);
                   10357:       st = prev;
                   10358:       ed = n;
                   10359:       while ( ed > st ) {
                   10360:         md = (st+ed)/2;
                   10361:         u = s0+md*nd_wpd;
                   10362:         c = DL_COMPARE(u,t);
                   10363:         if ( c == 0 ) break;
                   10364:         else if ( c > 0 ) st = md;
                   10365:         else ed = md;
                   10366:       }
                   10367:       r[md] = (mp_limb_t)CM(m);
                   10368:       prev = md;
1.7       noro     10369:     }
                   10370:     for ( i = 0; !r[i]; i++ );
                   10371:     return i;
                   10372: }
                   10373:
                   10374: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
                   10375:
1.28      noro     10376: 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     10377: {
                   10378:     int i,j,k,len,pos,prev;
                   10379:     mp_limb_t a,c,c1,c2;
                   10380:     IndArray ivect;
                   10381:     unsigned char *ivc;
                   10382:     unsigned short *ivs;
                   10383:     unsigned int *ivi;
                   10384:     NDV redv;
                   10385:     NMV mr;
                   10386:     NODE rp;
                   10387:     int maxrs;
                   10388:
                   10389:     for ( i = 0; i < col; i++ ) cvect[i] = 0;
                   10390:     maxrs = 0;
                   10391:     for ( i = 0; i < nred; i++ ) {
                   10392:         ivect = imat[i];
                   10393:         k = ivect->head;
                   10394:         a = svect[k]; c = cvect[k];
                   10395:         MOD128(a,c,m);
                   10396:         svect[k] = a; cvect[k] = 0;
1.28      noro     10397:         if ( (c = svect[k]) != 0 && (sig == 0 || comp_sig(sig,rp0[i]->sig) > 0 ) ) {
1.11      noro     10398:             Nf4_red++;
1.7       noro     10399:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   10400:             c = m-c; redv = nd_ps[rp0[i]->index];
                   10401:             len = LEN(redv); mr = BDY(redv);
                   10402:             svect[k] = 0; prev = k;
                   10403:             switch ( ivect->width ) {
                   10404:                 case 1:
                   10405:                     ivc = ivect->index.c;
                   10406:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   10407:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.12      noro     10408:                         c2 = svect[pos]+c1*c;
                   10409:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   10410:                         svect[pos] = c2;
1.7       noro     10411:                     }
                   10412:                     break;
                   10413:                 case 2:
                   10414:                     ivs = ivect->index.s;
                   10415:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   10416:                         pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.12      noro     10417:                         c2 = svect[pos]+c1*c;
                   10418:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   10419:                         svect[pos] = c2;
1.7       noro     10420:                     }
                   10421:                     break;
                   10422:                 case 4:
                   10423:                     ivi = ivect->index.i;
                   10424:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   10425:                         pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.12      noro     10426:                         c2 = svect[pos]+c1*c;
                   10427:                         if ( c2 < svect[pos] ) cvect[pos]++;
                   10428:                         svect[pos] = c2;
1.7       noro     10429:                     }
                   10430:                     break;
                   10431:             }
                   10432:         }
                   10433:     }
                   10434:     for ( i = 0; i < col; i++ ) {
                   10435:       a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
                   10436:     }
                   10437:     return maxrs;
                   10438: }
                   10439:
                   10440: /* for Fp, 2^15=<p<2^29 */
                   10441:
                   10442: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   10443:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   10444: {
                   10445:     int spcol,sprow,a;
                   10446:     int i,j,k,l,rank;
                   10447:     NODE r0,r;
                   10448:     ND_pairs sp;
                   10449:     ND spol;
                   10450:     mp_limb_t **spmat;
                   10451:     mp_limb_t *svect,*cvect;
                   10452:     mp_limb_t *v;
                   10453:     int *colstat;
                   10454:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   10455:     int maxrs;
                   10456:     int *spsugar;
                   10457:     ND_pairs *spactive;
                   10458:
                   10459:     spcol = col-nred;
                   10460:     get_eg(&eg0);
                   10461:     /* elimination (1st step) */
                   10462:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   10463:     svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   10464:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   10465:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   10466:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   10467:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   10468:         nd_sp(m,0,sp,&spol);
                   10469:         if ( !spol ) continue;
                   10470:         nd_to_vect64(m,s0vect,col,spol,svect);
1.28      noro     10471:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,0);
1.7       noro     10472:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   10473:         if ( i < col ) {
                   10474:             spmat[sprow] = v = (mp_limb_t *)MALLOC_ATOMIC(spcol*sizeof(mp_limb_t));
                   10475:             for ( j = k = 0; j < col; j++ )
                   10476:                 if ( !rhead[j] ) v[k++] = (UINT)svect[j];
                   10477:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   10478:             if ( nz )
                   10479:             spactive[sprow] = sp;
                   10480:             sprow++;
                   10481:         }
                   10482:         nd_free(spol);
                   10483:     }
1.12      noro     10484:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
1.7       noro     10485:     if ( DP_Print ) {
                   10486:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   10487:         fflush(asir_out);
                   10488:     }
                   10489:     /* free index arrays */
                   10490:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   10491:
                   10492:     /* elimination (2nd step) */
                   10493:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   10494:     rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   10495:     r0 = 0;
                   10496:     for ( i = 0; i < rank; i++ ) {
                   10497:         NEXTNODE(r0,r); BDY(r) =
                   10498:           (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   10499:         SG((NDV)BDY(r)) = spsugar[i];
                   10500:         GCFREE(spmat[i]);
                   10501:     }
                   10502:     if ( r0 ) NEXT(r) = 0;
                   10503:
                   10504:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.12      noro     10505:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
1.7       noro     10506:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   10507:     if ( DP_Print ) {
                   10508:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   10509:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   10510:             nsp,nred,sprow,spcol,rank);
                   10511:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   10512:     }
                   10513:     if ( nz ) {
                   10514:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   10515:         if ( rank > 0 ) {
                   10516:             NEXT(spactive[rank-1]) = 0;
                   10517:             *nz = spactive[0];
                   10518:         } else
                   10519:             *nz = 0;
                   10520:     }
                   10521:     return r0;
                   10522: }
                   10523:
                   10524: int nd_gauss_elim_mod64(mp_limb_t **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
                   10525: {
                   10526:   int i,j,k,l,rank,s;
                   10527:   mp_limb_t inv;
                   10528:   mp_limb_t a;
                   10529:   UINT c;
                   10530:   mp_limb_t *t,*pivot,*pk;
                   10531:   UINT *ck;
                   10532:   UINT **cmat;
                   10533:   UINT *ct;
                   10534:   ND_pairs pair;
                   10535:
                   10536:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   10537:   for ( i = 0; i < row; i++ ) {
                   10538:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   10539:     bzero(cmat[i],col*sizeof(UINT));
                   10540:   }
                   10541:
                   10542:   for ( rank = 0, j = 0; j < col; j++ ) {
                   10543:     for ( i = rank; i < row; i++ ) {
                   10544:       a = mat[i][j]; c = cmat[i][j];
                   10545:       MOD128(a,c,md);
                   10546:       mat[i][j] = a; cmat[i][j] = 0;
                   10547:     }
                   10548:     for ( i = rank; i < row; i++ )
                   10549:       if ( mat[i][j] )
                   10550:         break;
                   10551:     if ( i == row ) {
                   10552:       colstat[j] = 0;
                   10553:       continue;
                   10554:     } else
                   10555:       colstat[j] = 1;
                   10556:     if ( i != rank ) {
                   10557:       t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   10558:       ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
                   10559:       s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   10560:       if ( spactive ) {
                   10561:         pair = spactive[i]; spactive[i] = spactive[rank];
                   10562:         spactive[rank] = pair;
                   10563:       }
                   10564:     }
                   10565:     /* column j is normalized */
                   10566:     s = sugar[rank];
                   10567:     inv = invm((UINT)mat[rank][j],md);
                   10568:     /* normalize pivot row */
                   10569:     for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
                   10570:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   10571:     }
                   10572:     for ( i = rank+1; i < row; i++ ) {
                   10573:       if ( (a = mat[i][j]) != 0 ) {
                   10574:         sugar[i] = MAX(sugar[i],s);
                   10575:         red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
1.11      noro     10576:         Nf4_red++;
1.7       noro     10577:       }
                   10578:     }
                   10579:     rank++;
                   10580:   }
                   10581:   for ( j = col-1, l = rank-1; j >= 0; j-- )
                   10582:     if ( colstat[j] ) {
                   10583:       for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
                   10584:         a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
                   10585:       }
                   10586:       s = sugar[l];
                   10587:       for ( i = 0; i < l; i++ ) {
                   10588:         a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
                   10589:         if ( a ) {
                   10590:           sugar[i] = MAX(sugar[i],s);
                   10591:           red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
1.11      noro     10592:           Nf4_red++;
1.7       noro     10593:         }
                   10594:       }
                   10595:       l--;
                   10596:     }
                   10597:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   10598:   GCFREE(cmat);
                   10599:   return rank;
                   10600: }
                   10601: #endif
                   10602:
1.28      noro     10603: 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)
                   10604: {
                   10605:   int i,j,k,l,rank,s,imin;
                   10606:   mp_limb_t inv;
                   10607:   mp_limb_t a;
                   10608:   UINT c;
                   10609:   mp_limb_t *t,*pivot,*pk;
                   10610:   UINT *ck;
                   10611:   UINT **cmat;
                   10612:   UINT *ct;
                   10613:   ND_pairs pair;
                   10614:   SIG sg;
1.31      noro     10615:   int *used;
1.28      noro     10616:
1.31      noro     10617:   used = (int *)MALLOC(row*sizeof(int));
1.28      noro     10618:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   10619:   for ( i = 0; i < row; i++ ) {
                   10620:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   10621:     bzero(cmat[i],col*sizeof(UINT));
                   10622:   }
                   10623:
1.31      noro     10624:   for ( j = 0; j < col; j++ ) {
                   10625:     for ( i = 0; i < row; i++ ) {
1.28      noro     10626:       a = mat[i][j]; c = cmat[i][j];
                   10627:       MOD128(a,c,md);
                   10628:       mat[i][j] = a; cmat[i][j] = 0;
                   10629:     }
1.31      noro     10630:     for ( i = 0; i < row; i++ )
                   10631:       if ( !used[i] && mat[i][j] ) break;
                   10632:     if ( i == row ) {
1.28      noro     10633:       colstat[j] = 0;
                   10634:       continue;
1.31      noro     10635:     } else {
1.28      noro     10636:       colstat[j] = 1;
1.31      noro     10637:       used[i] = 1;
1.28      noro     10638:     }
                   10639:     /* column j is normalized */
1.31      noro     10640:     s = sugar[i];
                   10641:     inv = invm((UINT)mat[i][j],md);
1.28      noro     10642:     /* normalize pivot row */
1.31      noro     10643:     for ( k = j, pk = mat[i]+j, ck = cmat[i]+j; k < col; k++, pk++, ck++ ) {
1.28      noro     10644:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   10645:     }
1.31      noro     10646:     for ( k = i+1; k < row; k++ ) {
                   10647:       if ( (a = mat[k][j]) != 0 ) {
                   10648:         sugar[k] = MAX(sugar[k],s);
                   10649:         red_by_vect64(md,mat[k]+j,cmat[k]+j,mat[i]+j,(int)(md-a),col-j);
1.28      noro     10650:         Nf4_red++;
                   10651:       }
                   10652:     }
                   10653:   }
1.31      noro     10654:   rank = 0;
                   10655:   for ( i = 0; i < row; i++ ) {
                   10656:     for ( j = 0; j < col; j++ )
                   10657:       if ( mat[i][j] ) break;
                   10658:     if ( j == col ) sugar[i] = -1;
                   10659:     else rank++;
                   10660:   }
1.28      noro     10661:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   10662:   GCFREE(cmat);
                   10663:   return rank;
                   10664: }
                   10665:
                   10666: NODE nd_f4_red_mod64_main_s(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   10667:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,NODE *syzlistp)
                   10668: {
                   10669:     int spcol,sprow,a;
                   10670:     int i,j,k,l,rank;
                   10671:     NODE r0,r;
                   10672:     ND_pairs sp;
                   10673:     ND spol;
                   10674:     mp_limb_t **spmat;
                   10675:     mp_limb_t *svect,*cvect;
                   10676:     mp_limb_t *v;
                   10677:     int *colstat;
                   10678:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   10679:     int maxrs;
                   10680:     int *spsugar;
                   10681:     ND_pairs *spactive;
                   10682:     SIG *spsig;
                   10683:
                   10684:     get_eg(&eg0);
                   10685:     /* elimination (1st step) */
                   10686:     spmat = (mp_limb_t **)MALLOC(nsp*sizeof(mp_limb_t *));
                   10687:     cvect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   10688:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   10689:     spsig = (SIG *)MALLOC(nsp*sizeof(SIG));
                   10690:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   10691:         nd_sp(m,0,sp,&spol);
                   10692:         if ( !spol ) {
1.29      noro     10693:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     10694:           continue;
                   10695:         }
                   10696:         svect = (mp_limb_t *)MALLOC(col*sizeof(mp_limb_t));
                   10697:         nd_to_vect64(m,s0vect,col,spol,svect);
                   10698:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred,spol->sig);
                   10699:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   10700:         if ( i < col ) {
                   10701:             spmat[sprow] = svect;
                   10702:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   10703:             spsig[sprow] = sp->sig;
                   10704:             sprow++;
                   10705:         } else {
1.29      noro     10706:           syzlistp[sp->sig->pos] = insert_sig(syzlistp[sp->sig->pos],sp->sig);
1.28      noro     10707:         }
                   10708:         nd_free(spol);
                   10709:     }
                   10710:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1); add_eg(&f4_elim1,&eg0,&eg1);
                   10711:     if ( DP_Print ) {
                   10712:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime);
                   10713:         fflush(asir_out);
                   10714:     }
                   10715:     /* free index arrays */
                   10716:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   10717:
                   10718:     /* elimination (2nd step) */
                   10719:     colstat = (int *)MALLOC(col*sizeof(int));
                   10720:     rank = nd_gauss_elim_mod64_s(spmat,spsugar,0,sprow,col,m,colstat,spsig);
                   10721:     r0 = 0;
1.31      noro     10722:     for ( i = 0; i < sprow; i++ ) {
                   10723:         if ( spsugar[i] >= 0 ) {
                   10724:           NEXTNODE(r0,r);
                   10725:           BDY(r) = vect64_to_ndv_s(spmat[i],col,s0vect);
                   10726:           SG((NDV)BDY(r)) = spsugar[i];
                   10727:           ((NDV)BDY(r))->sig = spsig[i];
                   10728:         } else
                   10729:           syzlistp[spsig[i]->pos] = insert_sig(syzlistp[spsig[i]->pos],spsig[i]);
1.28      noro     10730:         GCFREE(spmat[i]);
                   10731:     }
                   10732:     if ( r0 ) NEXT(r) = 0;
                   10733:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2); add_eg(&f4_elim2,&eg1,&eg2);
                   10734:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   10735:     if ( DP_Print ) {
                   10736:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime);
                   10737:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
                   10738:             nsp,nred,sprow,col,rank);
                   10739:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime);
                   10740:     }
                   10741:     return r0;
                   10742: }
                   10743:
                   10744: NODE nd_f4_red_s(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,NODE *syzlistp)
                   10745: {
                   10746:   IndArray *imat;
                   10747:   int nsp,nred,i,start;
                   10748:   int *rhead;
                   10749:   NODE r0,rp;
                   10750:   ND_pairs sp;
                   10751:   NM_ind_pair *rvect;
                   10752:   UINT *s;
                   10753:   int *s0hash;
                   10754:   struct oEGT eg0,eg1,eg_conv;
                   10755:
                   10756:   for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   10757:   nred = length(rp0);
                   10758:   imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   10759:   rhead = (int *)MALLOC(col*sizeof(int));
                   10760:   for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   10761:
                   10762:   /* construction of index arrays */
                   10763:   get_eg(&eg0);
                   10764:   if ( DP_Print ) {
                   10765:     fprintf(asir_out,"%dx%d,",nsp+nred,col);
                   10766:     fflush(asir_out);
                   10767:   }
                   10768:   rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   10769:   for ( start = 0, rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   10770:     rvect[i] = (NM_ind_pair)BDY(rp);
                   10771:     imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,rvect[i],start);
                   10772:     rhead[imat[i]->head] = 1;
                   10773:     start = imat[i]->head;
                   10774:   }
                   10775:   get_eg(&eg1); init_eg(&eg_conv); add_eg(&eg_conv,&eg0,&eg1); add_eg(&f4_conv,&eg0,&eg1);
                   10776:   if ( DP_Print ) {
                   10777:     fprintf(asir_out,"conv=%.3fsec,",eg_conv.exectime);
                   10778:     fflush(asir_out);
                   10779:   }
                   10780:   if ( m > 0 )
                   10781:     r0 = nd_f4_red_mod64_main_s(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,syzlistp);
                   10782:   else
                   10783: //    r0 = nd_f4_red_q_main_s(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   10784:     error("nd_f4_red_q_main_s : not implemented yet");
                   10785:   return r0;
                   10786: }
                   10787:
                   10788: INLINE int ndl_find_reducer_minsig(UINT *dg)
                   10789: {
                   10790:   RHist r;
                   10791:   int i,singular,ret,d,k,imin;
                   10792:   SIG t;
                   10793:   static int wpd,nvar;
                   10794:   static SIG quo,quomin;
                   10795:   static UINT *tmp;
                   10796:
                   10797:   if ( !quo || nvar != nd_nvar ) { NEWSIG(quo); NEWSIG(quomin); }
                   10798:   if ( wpd != nd_wpd ) {
                   10799:     wpd = nd_wpd;
                   10800:     tmp = (UINT *)MALLOC(wpd*sizeof(UINT));
                   10801:   }
                   10802: #if 0
                   10803:   d = ndl_hash_value(dg);
                   10804:   for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   10805:     if ( ndl_equal(dg,DL(r)) ) {
                   10806:       return r->index;
                   10807:     }
                   10808:   }
                   10809: #endif
                   10810:   imin = -1;
                   10811:   for ( i = 0; i < nd_psn; i++ ) {
                   10812:     r = nd_psh[i];
                   10813:     if ( ndl_reducible(dg,DL(r)) ) {
                   10814:       ndl_sub(dg,DL(r),tmp);
                   10815:       _ndltodl(tmp,DL(quo));
                   10816:       _addtodl(nd_nvar,DL(nd_psh[i]->sig),DL(quo));
                   10817:       quo->pos = nd_psh[i]->sig->pos;
                   10818:       if ( imin < 0 || comp_sig(quomin,quo) > 0 ) {
                   10819:         t = quo; quo = quomin; quomin = t;
                   10820:         imin = i;
                   10821:       }
                   10822:     }
                   10823:   }
                   10824:   if ( imin == -1 ) return nd_psn;
                   10825:   else {
                   10826: #if 0
                   10827:     nd_append_red(dg,i);
                   10828: #endif
                   10829:     return imin;
                   10830:   }
                   10831: }
                   10832:
                   10833: int nd_symbolic_preproc_s(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
                   10834: {
                   10835:   NODE rp0,rp;
                   10836:   NM mul,head,s0,s;
                   10837:   int index,col,i,sugar;
                   10838:   RHist h;
                   10839:   UINT *s0v,*p;
                   10840:   NM_ind_pair pair;
                   10841:   ND red;
                   10842:   NDV *ps;
                   10843:   SIG sig;
                   10844:
                   10845:   s0 = 0; rp0 = 0; col = 0;
                   10846:   if ( nd_demand )
                   10847:     ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   10848:   else
                   10849:     ps = trace?nd_ps_trace:nd_ps;
                   10850:   while ( 1 ) {
                   10851:     head = remove_head_pbucket_symbolic(bucket);
                   10852:     if ( !head ) break;
                   10853:     if ( !s0 ) s0 = head;
                   10854:     else NEXT(s) = head;
                   10855:     s = head;
                   10856:     index = ndl_find_reducer_minsig(DL(head));
                   10857:     if ( index >= 0 && index < nd_psn ) {
                   10858:       h = nd_psh[index];
                   10859:       NEWNM(mul);
                   10860:       ndl_sub(DL(head),DL(h),DL(mul));
                   10861:       if ( ndl_check_bound2(index,DL(mul)) )
                   10862:         return 0;
                   10863:       sugar = TD(DL(mul))+SG(ps[index]);
                   10864:       NEWSIG(sig);
                   10865:       _ndltodl(DL(mul),DL(sig));
                   10866:       _addtodl(nd_nvar,DL(nd_psh[index]->sig),DL(sig));
                   10867:       sig->pos = nd_psh[index]->sig->pos;
                   10868:       MKNM_ind_pair(pair,mul,index,sugar,sig);
                   10869:       red = ndv_mul_nm_symbolic(mul,ps[index]);
                   10870:       add_pbucket_symbolic(bucket,nd_remove_head(red));
                   10871:       NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   10872:     }
                   10873:     col++;
                   10874:   }
                   10875:   if ( rp0 ) NEXT(rp) = 0;
                   10876:   NEXT(s) = 0;
                   10877:   s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   10878:   for ( i = 0, p = s0v, s = s0; i < col;
                   10879:     i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   10880:   *s0vect = s0v;
                   10881:   *r = rp0;
                   10882:
                   10883:   return col;
                   10884: }
                   10885:
                   10886: NODE nd_sba_f4(int m,int **indp)
                   10887: {
1.34      noro     10888:   int i,nh,stat,index,f4red,f4step;
1.33      noro     10889:   int col,rank,len,k,j,a,sugar,nbase,psugar,ms;
                   10890:   NODE r,g,rp0,nflist;
                   10891:   ND_pairs d,l,t;
                   10892:   ND h,nf;
                   10893:   NDV nfv;
                   10894:   union oNDC hc;
                   10895:   UINT *s0vect;
1.28      noro     10896:   UINT c;
                   10897:   PGeoBucket bucket;
1.33      noro     10898:   NODE *syzlist;
                   10899:   SIG sig;
1.28      noro     10900:   struct oEGT eg0,eg1,eg_f4;
1.33      noro     10901:   struct oEGT eg2,eg_update,eg_remove,eg_large,eg_nf,eg_nfzero;
1.28      noro     10902:
                   10903:   Nf4_red=0;
1.30      noro     10904:   d = 0;
1.29      noro     10905:   syzlist = (NODE *)MALLOC(nd_psn*sizeof(NODE));
1.28      noro     10906:   for ( i = 0; i < nd_psn; i++ ) {
1.30      noro     10907:     d = update_pairs_s(d,i,syzlist);
1.28      noro     10908:   }
1.30      noro     10909:   nd_nbase = nd_psn;
1.28      noro     10910:   f4red = 1;
1.33      noro     10911:   psugar = 0;
1.34      noro     10912:   f4step = 0;
1.28      noro     10913:   while ( d ) {
1.33      noro     10914:     for ( t = d, ms = SG(d); t; t = NEXT(t) )
                   10915:       if ( SG(t) < ms ) ms = SG(t);
1.38    ! noro     10916:     if ( ms == psugar && f4step >= nd_sba_f4step ) {
1.33      noro     10917: again:
                   10918:       l = d; d = d->next;
                   10919:       if ( small_lcm(l) ) {
                   10920:         if ( DP_Print ) fprintf(asir_out,"M");
                   10921:         continue;
                   10922:       }
                   10923:       sig = l->sig;
                   10924:       stat = nd_sp(m,0,l,&h);
                   10925:       if ( !stat ) {
                   10926:         NEXT(l) = d; d = l;
                   10927:         d = nd_reconstruct(0,d);
                   10928:         goto again;
                   10929:       }
                   10930:   get_eg(&eg1);
                   10931:   #if USE_GEOBUCKET
                   10932:       stat = m?nd_nf_pbucket_s(m,h,nd_ps,!Top,&nf):nd_nf_s(m,0,h,nd_ps,!Top,&nf);
                   10933:   #else
                   10934:       stat = nd_nf_s(m,0,h,nd_ps,!Top,&nf);
                   10935:   #endif
                   10936:   get_eg(&eg2);
                   10937:       if ( !stat ) {
                   10938:         NEXT(l) = d; d = l;
                   10939:         d = nd_reconstruct(0,d);
                   10940:         goto again;
                   10941:       } else if ( stat == -1 ) {
                   10942:         if ( DP_Print ) { printf("S"); fflush(stdout); }
                   10943:         FREENDP(l);
                   10944:       } else if ( nf ) {
                   10945:         if ( DP_Print ) { printf("+"); fflush(stdout); }
                   10946:         add_eg(&eg_nf,&eg1,&eg2);
                   10947:         hc = HCU(nf);
                   10948:         nd_removecont(m,nf);
                   10949:         nfv = ndtondv(m,nf); nd_free(nf);
                   10950:         nh = ndv_newps(m,nfv,0);
                   10951:
1.30      noro     10952:         d = update_pairs_s(d,nh,syzlist);
                   10953:         nd_sba_pos[sig->pos] = append_one(nd_sba_pos[sig->pos],nh);
1.33      noro     10954:         FREENDP(l);
1.28      noro     10955:       } else {
1.33      noro     10956:         add_eg(&eg_nfzero,&eg1,&eg2);
                   10957:        // syzygy
                   10958:   get_eg(&eg1);
                   10959:         d = remove_spair_s(d,sig);
                   10960:   get_eg(&eg2); add_eg(&eg_remove,&eg1,&eg2);
1.29      noro     10961:         syzlist[sig->pos] = insert_sig(syzlist[sig->pos],sig);
1.33      noro     10962:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   10963:         FREENDP(l);
                   10964:       }
                   10965:     } else {
1.34      noro     10966:       if ( ms != psugar ) f4step = 1;
                   10967:       else f4step++;
1.33      noro     10968: again2:
                   10969:       psugar = ms;
                   10970:       l = nd_minsugarp_s(d,&d);
                   10971:       sugar = nd_sugarweight?d->sugar2:SG(d);
                   10972:       bucket = create_pbucket();
                   10973:       stat = nd_sp_f4(m,0,l,bucket);
                   10974:       if ( !stat ) {
                   10975:         for ( t = l; NEXT(t); t = NEXT(t) );
                   10976:         NEXT(t) = d; d = l;
                   10977:         d = nd_reconstruct(0,d);
                   10978:         goto again2;
                   10979:       }
                   10980:       if ( bucket->m < 0 ) continue;
                   10981:       col = nd_symbolic_preproc_s(bucket,0,&s0vect,&rp0);
                   10982:       if ( !col ) {
1.37      noro     10983:         for ( t = l; NEXT(t); t = NEXT(t) )
                   10984:           ;
                   10985:         NEXT(t) = d; d = l;
1.33      noro     10986:         d = nd_reconstruct(0,d);
                   10987:         goto again2;
                   10988:       }
1.34      noro     10989:       if ( DP_Print ) fprintf(asir_out,"\nsugar=%d,",psugar);
1.33      noro     10990:       nflist = nd_f4_red_s(m,l,0,s0vect,col,rp0,syzlist);
                   10991:       /* adding new bases */
                   10992:       for ( r = nflist; r; r = NEXT(r) ) {
                   10993:         nfv = (NDV)BDY(r);
1.38    ! noro     10994:         if ( nd_f4_td ) SG(nfv) = nd_tdeg(nfv);
1.33      noro     10995:         ndv_removecont(m,nfv);
                   10996:         nh = ndv_newps(m,nfv,0);
                   10997:         d = update_pairs_s(d,nh,syzlist);
                   10998:         nd_sba_pos[nfv->sig->pos] = append_one(nd_sba_pos[nfv->sig->pos],nh);
                   10999:       }
                   11000:       for ( i = 0; i < nd_nbase; i++ )
                   11001:         for ( r = syzlist[i]; r; r = NEXT(r) )
                   11002:             d = remove_spair_s(d,(SIG)BDY(r));
                   11003:       d = remove_large_lcm(d);
                   11004:       if ( DP_Print ) {
1.34      noro     11005:         fprintf(asir_out,"f4red=%d,gblen=%d",f4red,nd_psn); fflush(asir_out);
1.33      noro     11006:       }
                   11007:       f4red++;
1.28      noro     11008:     }
                   11009:   }
                   11010:   if ( DP_Print ) {
1.34      noro     11011:     fprintf(asir_out,"\nnumber of red=%d,",Nf4_red);
1.28      noro     11012:   }
1.30      noro     11013:   g = conv_ilist_s(nd_demand,0,indp);
1.28      noro     11014:   return g;
                   11015: }

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