[BACK]Return to nd.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / engine

Annotation of OpenXM_contrib2/asir2000/engine/nd.c, Revision 1.248

1.248   ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.247 2018/07/28 00:45:55 noro Exp $ */
1.2       noro        2:
1.94      noro        3: #include "nd.h"
1.63      noro        4:
1.198     noro        5: struct oEGT eg_search;
                      6:
1.131     noro        7: int diag_period = 6;
1.202     noro        8: int weight_check = 1;
1.61      noro        9: int (*ndl_compare_function)(UINT *a1,UINT *a2);
1.94      noro       10: int nd_dcomp;
1.220     noro       11: int nd_rref2;
1.94      noro       12: NM _nm_free_list;
                     13: ND _nd_free_list;
                     14: ND_pairs _ndp_free_list;
1.150     noro       15: NODE nd_hcf;
1.32      noro       16:
1.219     noro       17: Obj nd_top_weight;
                     18:
1.146     noro       19: static NODE nd_subst;
                     20: static VL nd_vc;
1.121     noro       21: static int nd_ntrans;
1.117     noro       22: static int nd_nalg;
1.103     noro       23: #if 0
1.74      noro       24: static int ndv_alloc;
1.103     noro       25: #endif
1.87      noro       26: #if 1
1.69      noro       27: static int nd_f4_nsp=0x7fffffff;
1.87      noro       28: #else
                     29: static int nd_f4_nsp=50;
                     30: #endif
1.42      noro       31: static double nd_scale=2;
1.61      noro       32: static UINT **nd_bound;
1.42      noro       33: static struct order_spec *nd_ord;
                     34: static EPOS nd_epos;
1.43      noro       35: static BlockMask nd_blockmask;
1.42      noro       36: static int nd_nvar;
                     37: static int nd_isrlex;
                     38: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
1.61      noro       39: static UINT nd_mask[32];
                     40: static UINT nd_mask0,nd_mask1;
1.42      noro       41:
1.20      noro       42: static NDV *nd_ps;
1.215     noro       43: static NDV *nd_ps_gz;
1.53      noro       44: static NDV *nd_ps_trace;
1.215     noro       45: static NDV *nd_ps_sym;
                     46: static NDV *nd_ps_trace_sym;
1.42      noro       47: static RHist *nd_psh;
                     48: static int nd_psn,nd_pslen;
                     49: static RHist *nd_red;
1.96      noro       50: static int *nd_work_vector;
                     51: static int **nd_matrix;
                     52: static int nd_matrix_len;
1.97      noro       53: static struct weight_or_block *nd_worb;
                     54: static int nd_worb_len;
1.42      noro       55: static int nd_found,nd_create,nd_notfirst;
                     56: static int nmv_adv;
1.77      noro       57: static int nd_demand;
1.174     noro       58: static int nd_module,nd_ispot,nd_mpos,nd_pot_nelim;
1.224     noro       59: static int nd_module_rank,nd_poly_weight_len;
                     60: static int *nd_poly_weight,*nd_module_weight;
1.167     noro       61: static NODE nd_tracelist;
                     62: static NODE nd_alltracelist;
1.234     noro       63: static int nd_gentrace,nd_gensyz,nd_nora,nd_newelim,nd_intersect,nd_lf;
1.187     noro       64: static int *nd_gbblock;
1.209     noro       65: static NODE nd_nzlist,nd_check_splist;
                     66: static int nd_splist;
1.231     noro       67: static int *nd_sugarweight;
1.241     noro       68: static int nd_f4red,nd_rank0,nd_last_nonzero;
1.1       noro       69:
1.119     noro       70: NumberField get_numberfield();
1.114     noro       71: UINT *nd_det_compute_bound(NDV **dm,int n,int j);
                     72: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d);
1.152     ohara      73: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr);
1.118     noro       74: int nd_monic(int m,ND *p);
1.129     noro       75: NDV plain_vect_to_ndv_q(Q *mat,int col,UINT *s0vect);
1.157     noro       76: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank);
                     77: NDV pltondv(VL vl,VL dvl,LIST p);
                     78: void pltozpl(LIST l,Q *cont,LIST *pp);
1.159     noro       79: void ndl_max(UINT *d1,unsigned *d2,UINT *d);
1.167     noro       80: void nmtodp(int mod,NM m,DP *r);
                     81: NODE reverse_node(NODE n);
                     82: P ndc_div(int mod,union oNDC a,union oNDC b);
                     83: P ndctop(int mod,union oNDC c);
                     84: void finalize_tracelist(int i,P cont);
                     85: void conv_ilist(int demand,int trace,NODE g,int **indp);
1.172     noro       86: void parse_nd_option(NODE opt);
1.198     noro       87: void dltondl(int n,DL dl,UINT *r);
                     88: DP ndvtodp(int mod,NDV p);
1.204     noro       89: DP ndtodp(int mod,ND p);
1.215     noro       90: NDV ndvtondvgz(NDV p);
                     91: NDV ndvgztondv(NDV p);
                     92: ND ndtondgz(ND p);
                     93: ND ndgztond(ND p);
1.114     noro       94:
1.221     ohara      95: void Pdp_set_weight(NODE,VECT *);
                     96: void Pox_cmo_rpc(NODE,Obj *);
                     97:
1.232     noro       98: ND nd_add_lf(ND p1,ND p2);
                     99: void nd_mul_c_lf(ND p,GZ mul);
                    100: void ndv_mul_c_lf(NDV p,GZ mul);
1.239     noro      101: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                    102:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
1.240     noro      103: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
1.239     noro      104:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz);
1.234     noro      105: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
1.232     noro      106:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
                    107: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
1.234     noro      108: NODE nd_f4_lf_trace_main(int m,int **indp);
                    109: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
                    110:
1.232     noro      111: extern int lf_lazy;
                    112: extern GZ current_mod_lf;
                    113:
1.228     noro      114: extern int Denominator,DP_Multiple,MaxDeg;
1.149     noro      115:
1.220     noro      116: #define BLEN (8*sizeof(unsigned long))
                    117:
                    118: typedef struct matrix {
                    119:   int row,col;
                    120:   unsigned long **a;
                    121: } *matrix;
                    122:
                    123:
1.1       noro      124: void nd_free_private_storage()
                    125: {
1.157     noro      126:     _nm_free_list = 0;
                    127:     _ndp_free_list = 0;
1.71      noro      128: #if 0
1.157     noro      129:     GC_gcollect();
1.71      noro      130: #endif
1.1       noro      131: }
                    132:
                    133: void _NM_alloc()
                    134: {
1.157     noro      135:     NM p;
                    136:     int i;
1.1       noro      137:
1.157     noro      138:     for ( i = 0; i < 1024; i++ ) {
1.200     noro      139:         p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.157     noro      140:         p->next = _nm_free_list; _nm_free_list = p;
                    141:     }
1.1       noro      142: }
                    143:
1.220     noro      144: matrix alloc_matrix(int row,int col)
                    145: {
                    146:   unsigned long **a;
                    147:   int i,len,blen;
                    148:   matrix mat;
                    149:
                    150:   mat = (matrix)MALLOC(sizeof(struct matrix));
                    151:   mat->row = row;
                    152:   mat->col = col;
                    153:   mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
                    154:   return mat;
                    155: }
                    156:
                    157:
1.1       noro      158: void _ND_alloc()
                    159: {
1.157     noro      160:     ND p;
                    161:     int i;
1.1       noro      162:
1.157     noro      163:     for ( i = 0; i < 1024; i++ ) {
1.200     noro      164:         p = (ND)MALLOC(sizeof(struct oND));
1.157     noro      165:         p->body = (NM)_nd_free_list; _nd_free_list = p;
                    166:     }
1.1       noro      167: }
                    168:
                    169: void _NDP_alloc()
                    170: {
1.157     noro      171:     ND_pairs p;
                    172:     int i;
1.1       noro      173:
1.157     noro      174:     for ( i = 0; i < 1024; i++ ) {
1.200     noro      175:         p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
1.157     noro      176:             +(nd_wpd-1)*sizeof(UINT));
                    177:         p->next = _ndp_free_list; _ndp_free_list = p;
                    178:     }
1.1       noro      179: }
                    180:
1.30      noro      181: INLINE int nd_length(ND p)
1.1       noro      182: {
1.157     noro      183:     NM m;
                    184:     int i;
1.1       noro      185:
1.157     noro      186:     if ( !p )
                    187:         return 0;
                    188:     else {
                    189:         for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
                    190:         return i;
                    191:     }
1.1       noro      192: }
                    193:
1.230     noro      194: extern int dp_negative_weight;
                    195:
1.61      noro      196: INLINE int ndl_reducible(UINT *d1,UINT *d2)
1.1       noro      197: {
1.157     noro      198:     UINT u1,u2;
                    199:     int i,j;
1.1       noro      200:
1.157     noro      201:     if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
                    202:
1.230     noro      203:     if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
1.65      noro      204: #if USE_UNROLL
1.157     noro      205:     switch ( nd_bpe ) {
                    206:         case 3:
                    207:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    208:                 u1 = d1[i]; u2 = d2[i];
                    209:                 if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
                    210:                 if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
                    211:                 if ( (u1&  0xe00000) < (u2&  0xe00000) ) return 0;
                    212:                 if ( (u1&  0x1c0000) < (u2&  0x1c0000) ) return 0;
                    213:                 if ( (u1&   0x38000) < (u2&   0x38000) ) return 0;
                    214:                 if ( (u1&    0x7000) < (u2&    0x7000) ) return 0;
                    215:                 if ( (u1&     0xe00) < (u2&     0xe00) ) return 0;
                    216:                 if ( (u1&     0x1c0) < (u2&     0x1c0) ) return 0;
                    217:                 if ( (u1&      0x38) < (u2&      0x38) ) return 0;
                    218:                 if ( (u1&       0x7) < (u2&       0x7) ) return 0;
                    219:             }
                    220:             return 1;
                    221:             break;
                    222:         case 4:
                    223:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    224:                 u1 = d1[i]; u2 = d2[i];
                    225:                 if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
                    226:                 if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
                    227:                 if ( (u1&  0xf00000) < (u2&  0xf00000) ) return 0;
                    228:                 if ( (u1&   0xf0000) < (u2&   0xf0000) ) return 0;
                    229:                 if ( (u1&    0xf000) < (u2&    0xf000) ) return 0;
                    230:                 if ( (u1&     0xf00) < (u2&     0xf00) ) return 0;
                    231:                 if ( (u1&      0xf0) < (u2&      0xf0) ) return 0;
                    232:                 if ( (u1&       0xf) < (u2&       0xf) ) return 0;
                    233:             }
                    234:             return 1;
                    235:             break;
                    236:         case 6:
                    237:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    238:                 u1 = d1[i]; u2 = d2[i];
                    239:                 if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
                    240:                 if ( (u1&  0xfc0000) < (u2&  0xfc0000) ) return 0;
                    241:                 if ( (u1&   0x3f000) < (u2&   0x3f000) ) return 0;
                    242:                 if ( (u1&     0xfc0) < (u2&     0xfc0) ) return 0;
                    243:                 if ( (u1&      0x3f) < (u2&      0x3f) ) return 0;
                    244:             }
                    245:             return 1;
                    246:             break;
                    247:         case 8:
                    248:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    249:                 u1 = d1[i]; u2 = d2[i];
                    250:                 if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
                    251:                 if ( (u1&  0xff0000) < (u2&  0xff0000) ) return 0;
                    252:                 if ( (u1&    0xff00) < (u2&    0xff00) ) return 0;
                    253:                 if ( (u1&      0xff) < (u2&      0xff) ) return 0;
                    254:             }
                    255:             return 1;
                    256:             break;
                    257:         case 16:
                    258:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    259:                 u1 = d1[i]; u2 = d2[i];
                    260:                 if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
                    261:                 if ( (u1&    0xffff) < (u2&    0xffff) ) return 0;
                    262:             }
                    263:             return 1;
                    264:             break;
                    265:         case 32:
                    266:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                    267:                 if ( d1[i] < d2[i] ) return 0;
                    268:             return 1;
                    269:             break;
                    270:         default:
                    271:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    272:                 u1 = d1[i]; u2 = d2[i];
                    273:                 for ( j = 0; j < nd_epw; j++ )
                    274:                     if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    275:             }
                    276:             return 1;
                    277:     }
1.65      noro      278: #else
1.157     noro      279:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    280:         u1 = d1[i]; u2 = d2[i];
                    281:         for ( j = 0; j < nd_epw; j++ )
                    282:             if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    283:     }
                    284:     return 1;
1.65      noro      285: #endif
1.1       noro      286: }
                    287:
1.61      noro      288: /*
                    289:  * If the current order is a block order,
                    290:  * then the last block is length 1 and contains
                    291:  * the homo variable. Otherwise, the original
                    292:  * order is either 0 or 2.
                    293:  */
                    294:
1.164     noro      295: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
1.23      noro      296: {
1.157     noro      297:     int w,i,e,n,omask0;
1.61      noro      298:
1.157     noro      299:     omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
                    300:     n = nd_nvar-1;
                    301:     ndl_zero(r);
                    302:     for ( i = 0; i < n; i++ ) {
                    303:         e = GET_EXP_OLD(d,i);
                    304:         PUT_EXP(r,i,e);
                    305:     }
                    306:     w = TD(d);
                    307:     PUT_EXP(r,nd_nvar-1,weight-w);
1.164     noro      308:     if ( nd_module ) MPOS(r) = d[ompos];
1.157     noro      309:     TD(r) = weight;
                    310:     if ( nd_blockmask ) ndl_weight_mask(r);
1.61      noro      311: }
                    312:
                    313: void ndl_dehomogenize(UINT *d)
                    314: {
1.157     noro      315:     UINT mask;
                    316:     UINT h;
                    317:     int i,bits;
                    318:
                    319:     if ( nd_blockmask ) {
                    320:         h = GET_EXP(d,nd_nvar-1);
                    321:         XOR_EXP(d,nd_nvar-1,h);
                    322:         TD(d) -= h;
                    323:         ndl_weight_mask(d);
                    324:     } else {
                    325:         if ( nd_isrlex ) {
                    326:             if ( nd_bpe == 32 ) {
                    327:                 h = d[nd_exporigin];
                    328:                 for ( i = nd_exporigin+1; i < nd_wpd; i++ )
                    329:                     d[i-1] = d[i];
                    330:                 d[i-1] = 0;
                    331:                 TD(d) -= h;
                    332:             } else {
                    333:                 bits = nd_epw*nd_bpe;
                    334:                 mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
                    335:                 h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
                    336:                 for ( i = nd_exporigin; i < nd_wpd; i++ )
                    337:                     d[i] = ((d[i]<<nd_bpe)&mask)
                    338:                         |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
                    339:                 TD(d) -= h;
                    340:             }
                    341:         } else {
                    342:             h = GET_EXP(d,nd_nvar-1);
                    343:             XOR_EXP(d,nd_nvar-1,h);
                    344:             TD(d) -= h;
                    345:         }
                    346:     }
1.23      noro      347: }
                    348:
1.61      noro      349: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
1.1       noro      350: {
1.157     noro      351:     UINT t1,t2,u,u1,u2;
                    352:     int i,j,l;
                    353:
                    354:     if ( nd_module && (MPOS(d1) != MPOS(d2)) )
                    355:         error("ndl_lcm : inconsistent monomials");
                    356: #if USE_UNROLL
                    357:     switch ( nd_bpe ) {
                    358:         case 3:
                    359:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    360:                 u1 = d1[i]; u2 = d2[i];
                    361:                 t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
                    362:                 t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
                    363:                 t1 = (u1&  0xe00000); t2 = (u2&  0xe00000); u |= t1>t2?t1:t2;
                    364:                 t1 = (u1&  0x1c0000); t2 = (u2&  0x1c0000); u |= t1>t2?t1:t2;
                    365:                 t1 = (u1&   0x38000); t2 = (u2&   0x38000); u |= t1>t2?t1:t2;
                    366:                 t1 = (u1&    0x7000); t2 = (u2&    0x7000); u |= t1>t2?t1:t2;
                    367:                 t1 = (u1&     0xe00); t2 = (u2&     0xe00); u |= t1>t2?t1:t2;
                    368:                 t1 = (u1&     0x1c0); t2 = (u2&     0x1c0); u |= t1>t2?t1:t2;
                    369:                 t1 = (u1&      0x38); t2 = (u2&      0x38); u |= t1>t2?t1:t2;
                    370:                 t1 = (u1&       0x7); t2 = (u2&       0x7); u |= t1>t2?t1:t2;
                    371:                 d[i] = u;
                    372:             }
                    373:             break;
                    374:         case 4:
                    375:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    376:                 u1 = d1[i]; u2 = d2[i];
                    377:                 t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
                    378:                 t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
                    379:                 t1 = (u1&  0xf00000); t2 = (u2&  0xf00000); u |= t1>t2?t1:t2;
                    380:                 t1 = (u1&   0xf0000); t2 = (u2&   0xf0000); u |= t1>t2?t1:t2;
                    381:                 t1 = (u1&    0xf000); t2 = (u2&    0xf000); u |= t1>t2?t1:t2;
                    382:                 t1 = (u1&     0xf00); t2 = (u2&     0xf00); u |= t1>t2?t1:t2;
                    383:                 t1 = (u1&      0xf0); t2 = (u2&      0xf0); u |= t1>t2?t1:t2;
                    384:                 t1 = (u1&       0xf); t2 = (u2&       0xf); u |= t1>t2?t1:t2;
                    385:                 d[i] = u;
                    386:             }
                    387:             break;
                    388:         case 6:
                    389:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    390:                 u1 = d1[i]; u2 = d2[i];
                    391:                 t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
                    392:                 t1 = (u1&  0xfc0000); t2 = (u2&  0xfc0000); u |= t1>t2?t1:t2;
                    393:                 t1 = (u1&   0x3f000); t2 = (u2&   0x3f000); u |= t1>t2?t1:t2;
                    394:                 t1 = (u1&     0xfc0); t2 = (u2&     0xfc0); u |= t1>t2?t1:t2;
                    395:                 t1 = (u1&      0x3f); t2 = (u2&      0x3f); u |= t1>t2?t1:t2;
                    396:                 d[i] = u;
                    397:             }
                    398:             break;
                    399:         case 8:
                    400:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    401:                 u1 = d1[i]; u2 = d2[i];
                    402:                 t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
                    403:                 t1 = (u1&  0xff0000); t2 = (u2&  0xff0000); u |= t1>t2?t1:t2;
                    404:                 t1 = (u1&    0xff00); t2 = (u2&    0xff00); u |= t1>t2?t1:t2;
                    405:                 t1 = (u1&      0xff); t2 = (u2&      0xff); u |= t1>t2?t1:t2;
                    406:                 d[i] = u;
                    407:             }
                    408:             break;
                    409:         case 16:
                    410:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    411:                 u1 = d1[i]; u2 = d2[i];
                    412:                 t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
                    413:                 t1 = (u1&    0xffff); t2 = (u2&    0xffff); u |= t1>t2?t1:t2;
                    414:                 d[i] = u;
                    415:             }
                    416:             break;
                    417:         case 32:
                    418:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    419:                 u1 = d1[i]; u2 = d2[i];
                    420:                 d[i] = u1>u2?u1:u2;
                    421:             }
                    422:             break;
                    423:         default:
                    424:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    425:                 u1 = d1[i]; u2 = d2[i];
                    426:                 for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    427:                     t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    428:                 }
                    429:                 d[i] = u;
                    430:             }
                    431:             break;
                    432:     }
                    433: #else
                    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: #endif
1.163     noro      442:     if ( nd_module ) MPOS(d) = MPOS(d1);
1.157     noro      443:     TD(d) = ndl_weight(d);
                    444:     if ( nd_blockmask ) ndl_weight_mask(d);
                    445: }
                    446:
1.159     noro      447: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
1.157     noro      448: {
                    449:     UINT t1,t2,u,u1,u2;
                    450:     int i,j,l;
1.1       noro      451:
1.157     noro      452:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    453:         u1 = d1[i]; u2 = d2[i];
                    454:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    455:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    456:         }
                    457:         d[i] = u;
                    458:     }
1.57      noro      459: }
                    460:
1.61      noro      461: int ndl_weight(UINT *d)
1.1       noro      462: {
1.157     noro      463:     UINT t,u;
                    464:     int i,j;
1.1       noro      465:
1.157     noro      466:     if ( current_dl_weight_vector )
                    467:         for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    468:             u = GET_EXP(d,i);
                    469:             t += MUL_WEIGHT(u,i);
                    470:         }
                    471:     else
                    472:         for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    473:             u = d[i];
                    474:             for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    475:                 t += (u&nd_mask0);
                    476:         }
1.167     noro      477:     if ( nd_module && current_module_weight_vector && MPOS(d) )
                    478:         t += current_module_weight_vector[MPOS(d)];
1.157     noro      479:     return t;
1.1       noro      480: }
                    481:
1.231     noro      482: /* for sugarweight */
                    483:
                    484: int ndl_weight2(UINT *d)
                    485: {
                    486:     int t,u;
                    487:     int i,j;
                    488:
                    489:     for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    490:         u = GET_EXP(d,i);
                    491:         t += nd_sugarweight[i]*u;
                    492:     }
                    493:     if ( nd_module && current_module_weight_vector && MPOS(d) )
                    494:         t += current_module_weight_vector[MPOS(d)];
                    495:     return t;
                    496: }
                    497:
1.61      noro      498: void ndl_weight_mask(UINT *d)
1.43      noro      499: {
1.157     noro      500:     UINT t,u;
                    501:     UINT *mask;
                    502:     int i,j,k,l;
                    503:
                    504:     l = nd_blockmask->n;
                    505:     for ( k = 0; k < l; k++ ) {
                    506:         mask = nd_blockmask->mask[k];
                    507:         if ( current_dl_weight_vector )
                    508:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    509:                 u = GET_EXP_MASK(d,i,mask);
                    510:                 t += MUL_WEIGHT(u,i);
                    511:             }
                    512:         else
                    513:             for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    514:                 u = d[i]&mask[i];
                    515:                 for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    516:                     t += (u&nd_mask0);
                    517:             }
                    518:         d[k+1] = t;
                    519:     }
1.43      noro      520: }
                    521:
1.61      noro      522: int ndl_lex_compare(UINT *d1,UINT *d2)
1.1       noro      523: {
1.157     noro      524:     int i;
1.1       noro      525:
1.157     noro      526:     d1 += nd_exporigin;
                    527:     d2 += nd_exporigin;
                    528:     for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
                    529:         if ( *d1 > *d2 )
                    530:             return nd_isrlex ? -1 : 1;
                    531:         else if ( *d1 < *d2 )
                    532:             return nd_isrlex ? 1 : -1;
                    533:     return 0;
1.1       noro      534: }
                    535:
1.61      noro      536: int ndl_block_compare(UINT *d1,UINT *d2)
1.43      noro      537: {
1.157     noro      538:     int i,l,j,ord_o,ord_l;
                    539:     struct order_pair *op;
                    540:     UINT t1,t2,m;
                    541:     UINT *mask;
                    542:
                    543:     l = nd_blockmask->n;
                    544:     op = nd_blockmask->order_pair;
                    545:     for ( j = 0; j < l; j++ ) {
                    546:         mask = nd_blockmask->mask[j];
                    547:         ord_o = op[j].order;
                    548:         if ( ord_o < 2 )
                    549:             if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
                    550:             else if ( t1 < t2 ) return -1;
                    551:         for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    552:             m = mask[i];
                    553:             t1 = d1[i]&m;
                    554:             t2 = d2[i]&m;
                    555:             if ( t1 > t2 )
                    556:                 return !ord_o ? -1 : 1;
                    557:             else if ( t1 < t2 )
                    558:                 return !ord_o ? 1 : -1;
                    559:         }
                    560:     }
                    561:     return 0;
1.43      noro      562: }
                    563:
1.96      noro      564: int ndl_matrix_compare(UINT *d1,UINT *d2)
                    565: {
1.219     noro      566:     int i,j,s,row;
1.157     noro      567:     int *v;
1.245     noro      568:   Q **mat;
1.219     noro      569:     Q *w;
                    570:     Q t,t1,t2;
1.96      noro      571:
1.157     noro      572:     for ( j = 0; j < nd_nvar; j++ )
                    573:         nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.245     noro      574:   if ( nd_top_weight ) {
                    575:     if ( OID(nd_top_weight) == O_VECT ) {
                    576:         mat = (Q **)&BDY((VECT)nd_top_weight);
                    577:         row = 1;
                    578:     } else {
                    579:       mat = (Q **)BDY((MAT)nd_top_weight);
                    580:         row = ((MAT)nd_top_weight)->row;
                    581:     }
                    582:     for ( i = 0; i < row; i++ ) {
                    583:         w = (Q *)mat[i];
                    584:       for ( j = 0, t = 0; j < nd_nvar; j++ ) {
                    585:         STOQ(nd_work_vector[j],t1);
                    586:           mulq(w[j],t1,&t2);
                    587:         addq(t,t2,&t1);
                    588:         t = t1;
                    589:       }
                    590:         if ( t ) {
                    591:           s = SGN(t);
                    592:             if ( s > 0 ) return 1;
                    593:             else if ( s < 0 ) return -1;
                    594:         }
                    595:     }
                    596:   }
1.157     noro      597:     for ( i = 0; i < nd_matrix_len; i++ ) {
                    598:         v = nd_matrix[i];
                    599:         for ( j = 0, s = 0; j < nd_nvar; j++ )
                    600:             s += v[j]*nd_work_vector[j];
                    601:         if ( s > 0 ) return 1;
                    602:         else if ( s < 0 ) return -1;
                    603:     }
1.245     noro      604:   if ( !ndl_equal(d1,d2) )
                    605:     error("afo");
1.157     noro      606:     return 0;
1.96      noro      607: }
                    608:
1.97      noro      609: int ndl_composite_compare(UINT *d1,UINT *d2)
                    610: {
1.157     noro      611:     int i,j,s,start,end,len,o;
                    612:     int *v;
                    613:     struct sparse_weight *sw;
                    614:
                    615:     for ( j = 0; j < nd_nvar; j++ )
                    616:         nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
                    617:     for ( i = 0; i < nd_worb_len; i++ ) {
                    618:         len = nd_worb[i].length;
                    619:         switch ( nd_worb[i].type ) {
                    620:             case IS_DENSE_WEIGHT:
                    621:                 v = nd_worb[i].body.dense_weight;
                    622:                 for ( j = 0, s = 0; j < len; j++ )
                    623:                     s += v[j]*nd_work_vector[j];
                    624:                 if ( s > 0 ) return 1;
                    625:                 else if ( s < 0 ) return -1;
                    626:                 break;
                    627:             case IS_SPARSE_WEIGHT:
                    628:                 sw = nd_worb[i].body.sparse_weight;
                    629:                 for ( j = 0, s = 0; j < len; j++ )
                    630:                     s += sw[j].value*nd_work_vector[sw[j].pos];
                    631:                 if ( s > 0 ) return 1;
                    632:                 else if ( s < 0 ) return -1;
                    633:                 break;
                    634:             case IS_BLOCK:
                    635:                 o = nd_worb[i].body.block.order;
                    636:                 start = nd_worb[i].body.block.start;
                    637:                 switch ( o ) {
                    638:                     case 0:
                    639:                         end = start+len;
                    640:                         for ( j = start, s = 0; j < end; j++ )
                    641:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    642:                         if ( s > 0 ) return 1;
                    643:                         else if ( s < 0 ) return -1;
                    644:                         for ( j = end-1; j >= start; j-- )
                    645:                             if ( nd_work_vector[j] < 0 ) return 1;
                    646:                             else if ( nd_work_vector[j] > 0 ) return -1;
                    647:                         break;
                    648:                     case 1:
                    649:                         end = start+len;
                    650:                         for ( j = start, s = 0; j < end; j++ )
                    651:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    652:                         if ( s > 0 ) return 1;
                    653:                         else if ( s < 0 ) return -1;
                    654:                         for ( j = start; j < end; j++ )
                    655:                             if ( nd_work_vector[j] > 0 ) return 1;
                    656:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    657:                         break;
                    658:                     case 2:
1.236     noro      659:                         end = start+len;
1.157     noro      660:                         for ( j = start; j < end; j++ )
                    661:                             if ( nd_work_vector[j] > 0 ) return 1;
                    662:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    663:                         break;
                    664:                 }
                    665:                 break;
                    666:         }
                    667:     }
                    668:     return 0;
1.97      noro      669: }
                    670:
1.58      noro      671: /* TDH -> WW -> TD-> RL */
                    672:
1.61      noro      673: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
1.58      noro      674: {
1.157     noro      675:     int i,m,e1,e2;
1.58      noro      676:
1.157     noro      677:     if ( TD(d1) > TD(d2) ) return 1;
                    678:     else if ( TD(d1) < TD(d2) ) return -1;
                    679:     m = nd_nvar>>1;
                    680:     for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
                    681:         e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
                    682:         e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
                    683:     }
                    684:     if ( e1 > e2 ) return 1;
                    685:     else if ( e1 < e2 ) return -1;
                    686:     return ndl_lex_compare(d1,d2);
                    687: }
                    688:
1.224     noro      689: int ndl_module_weight_compare(UINT *d1,UINT *d2)
                    690: {
                    691:   int s,j;
                    692:
                    693:   if ( nd_nvar != nd_poly_weight_len )
                    694:     error("invalid module weight : the length of polynomial weight != the number of variables");
                    695:   s = 0;
                    696:   for ( j = 0; j < nd_nvar; j++ )
                    697:      s += (GET_EXP(d1,j)-GET_EXP(d2,j))*nd_poly_weight[j];
1.226     noro      698:   if ( MPOS(d1) >= 1 && MPOS(d2) >= 1 ) {
                    699:     s += nd_module_weight[MPOS(d1)-1]-nd_module_weight[MPOS(d2)-1];
                    700:   }
1.224     noro      701:   if ( s > 0 ) return 1;
                    702:   else if ( s < 0 ) return -1;
                    703:   else return 0;
                    704: }
                    705:
1.157     noro      706: int ndl_module_grlex_compare(UINT *d1,UINT *d2)
                    707: {
1.164     noro      708:     int i,c;
1.157     noro      709:
1.224     noro      710:     if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
1.160     noro      711:     if ( nd_ispot ) {
1.245     noro      712:     if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
1.174     noro      713:             if ( TD(d1) > TD(d2) ) return 1;
                    714:             else if ( TD(d1) < TD(d2) ) return -1;
                    715:             if ( c = ndl_lex_compare(d1,d2) ) return c;
                    716:             if ( MPOS(d1) < MPOS(d2) ) return 1;
                    717:             else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    718:             return 0;
1.245     noro      719:     }
1.157     noro      720:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    721:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    722:     }
                    723:     if ( TD(d1) > TD(d2) ) return 1;
                    724:     else if ( TD(d1) < TD(d2) ) return -1;
1.167     noro      725:     if ( c = ndl_lex_compare(d1,d2) ) return c;
1.160     noro      726:     if ( !nd_ispot ) {
1.157     noro      727:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    728:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    729:     }
                    730:     return 0;
                    731: }
                    732:
                    733: int ndl_module_glex_compare(UINT *d1,UINT *d2)
                    734: {
1.164     noro      735:     int i,c;
1.157     noro      736:
1.224     noro      737:     if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
1.160     noro      738:     if ( nd_ispot ) {
1.157     noro      739:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    740:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    741:     }
                    742:     if ( TD(d1) > TD(d2) ) return 1;
                    743:     else if ( TD(d1) < TD(d2) ) return -1;
1.167     noro      744:     if ( c = ndl_lex_compare(d1,d2) ) return c;
1.160     noro      745:     if ( !nd_ispot ) {
1.157     noro      746:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    747:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    748:     }
                    749:     return 0;
                    750: }
                    751:
                    752: int ndl_module_lex_compare(UINT *d1,UINT *d2)
                    753: {
1.164     noro      754:     int i,c;
1.157     noro      755:
1.224     noro      756:     if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
1.160     noro      757:     if ( nd_ispot ) {
1.157     noro      758:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    759:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    760:     }
1.167     noro      761:     if ( c = ndl_lex_compare(d1,d2) ) return c;
1.160     noro      762:     if ( !nd_ispot ) {
1.157     noro      763:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    764:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    765:     }
                    766:     return 0;
                    767: }
                    768:
                    769: int ndl_module_block_compare(UINT *d1,UINT *d2)
                    770: {
                    771:     int i,c;
                    772:
1.224     noro      773:     if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
1.160     noro      774:     if ( nd_ispot ) {
1.157     noro      775:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    776:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    777:     }
                    778:     if ( c = ndl_block_compare(d1,d2) ) return c;
1.160     noro      779:     if ( !nd_ispot ) {
1.157     noro      780:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    781:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    782:     }
                    783:     return 0;
                    784: }
                    785:
                    786: int ndl_module_matrix_compare(UINT *d1,UINT *d2)
                    787: {
                    788:     int i,c;
                    789:
1.224     noro      790:     if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
1.160     noro      791:     if ( nd_ispot ) {
1.157     noro      792:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    793:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    794:     }
                    795:     if ( c = ndl_matrix_compare(d1,d2) ) return c;
1.160     noro      796:     if ( !nd_ispot ) {
1.157     noro      797:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    798:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    799:     }
                    800:     return 0;
                    801: }
                    802:
                    803: int ndl_module_composite_compare(UINT *d1,UINT *d2)
                    804: {
                    805:     int i,c;
                    806:
1.224     noro      807:     if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
1.160     noro      808:     if ( nd_ispot ) {
1.157     noro      809:         if ( MPOS(d1) > MPOS(d2) ) return 1;
                    810:         else if ( MPOS(d1) < MPOS(d2) ) return -1;
                    811:     }
                    812:     if ( c = ndl_composite_compare(d1,d2) ) return c;
1.160     noro      813:     if ( !nd_ispot ) {
1.157     noro      814:         if ( MPOS(d1) > MPOS(d2) ) return 1;
                    815:         else if ( MPOS(d1) < MPOS(d2) ) return -1;
                    816:     }
                    817:     return 0;
1.58      noro      818: }
                    819:
1.61      noro      820: INLINE int ndl_equal(UINT *d1,UINT *d2)
1.1       noro      821: {
1.157     noro      822:     int i;
1.1       noro      823:
1.157     noro      824:     switch ( nd_wpd ) {
                    825:         case 2:
                    826:             if ( TD(d2) != TD(d1) ) return 0;
                    827:             if ( d2[1] != d1[1] ) return 0;
                    828:             return 1;
                    829:             break;
                    830:         case 3:
                    831:             if ( TD(d2) != TD(d1) ) return 0;
                    832:             if ( d2[1] != d1[1] ) return 0;
                    833:             if ( d2[2] != d1[2] ) return 0;
                    834:             return 1;
                    835:             break;
                    836:         default:
                    837:             for ( i = 0; i < nd_wpd; i++ )
                    838:                 if ( *d1++ != *d2++ ) return 0;
                    839:             return 1;
                    840:             break;
                    841:     }
1.1       noro      842: }
                    843:
1.61      noro      844: INLINE void ndl_copy(UINT *d1,UINT *d2)
1.6       noro      845: {
1.157     noro      846:     int i;
1.6       noro      847:
1.157     noro      848:     switch ( nd_wpd ) {
                    849:         case 2:
                    850:             TD(d2) = TD(d1);
                    851:             d2[1] = d1[1];
                    852:             break;
                    853:         case 3:
                    854:             TD(d2) = TD(d1);
                    855:             d2[1] = d1[1];
                    856:             d2[2] = d1[2];
                    857:             break;
                    858:         default:
                    859:             for ( i = 0; i < nd_wpd; i++ )
                    860:                 d2[i] = d1[i];
                    861:             break;
                    862:     }
1.6       noro      863: }
                    864:
1.61      noro      865: INLINE void ndl_zero(UINT *d)
                    866: {
1.157     noro      867:     int i;
                    868:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
1.61      noro      869: }
                    870:
                    871: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
1.1       noro      872: {
1.157     noro      873:     int i;
1.1       noro      874:
1.162     noro      875:     if ( nd_module ) {
                    876:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
1.167     noro      877:         error("ndl_add : invalid operation");
1.162     noro      878:     }
1.43      noro      879: #if 1
1.157     noro      880:     switch ( nd_wpd ) {
                    881:         case 2:
                    882:             TD(d) = TD(d1)+TD(d2);
                    883:             d[1] = d1[1]+d2[1];
                    884:             break;
                    885:         case 3:
                    886:             TD(d) = TD(d1)+TD(d2);
                    887:             d[1] = d1[1]+d2[1];
                    888:             d[2] = d1[2]+d2[2];
                    889:             break;
                    890:         default:
                    891:             for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    892:             break;
                    893:     }
1.43      noro      894: #else
1.157     noro      895:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
1.43      noro      896: #endif
1.6       noro      897: }
                    898:
1.55      noro      899: /* d1 += d2 */
1.61      noro      900: INLINE void ndl_addto(UINT *d1,UINT *d2)
1.55      noro      901: {
1.157     noro      902:     int i;
1.55      noro      903:
1.162     noro      904:     if ( nd_module ) {
                    905:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
                    906:             error("ndl_addto : invalid operation");
                    907:     }
1.55      noro      908: #if 1
1.157     noro      909:     switch ( nd_wpd ) {
                    910:         case 2:
                    911:             TD(d1) += TD(d2);
                    912:             d1[1] += d2[1];
                    913:             break;
                    914:         case 3:
                    915:             TD(d1) += TD(d2);
                    916:             d1[1] += d2[1];
                    917:             d1[2] += d2[2];
                    918:             break;
                    919:         default:
                    920:             for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    921:             break;
                    922:     }
1.55      noro      923: #else
1.157     noro      924:     for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
1.55      noro      925: #endif
                    926: }
                    927:
1.61      noro      928: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
1.6       noro      929: {
1.157     noro      930:     int i;
1.6       noro      931:
1.157     noro      932:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
1.1       noro      933: }
                    934:
1.61      noro      935: int ndl_disjoint(UINT *d1,UINT *d2)
1.1       noro      936: {
1.157     noro      937:     UINT t1,t2,u,u1,u2;
                    938:     int i,j;
1.1       noro      939:
1.157     noro      940:     if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
1.65      noro      941: #if USE_UNROLL
1.157     noro      942:     switch ( nd_bpe ) {
                    943:         case 3:
                    944:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    945:                 u1 = d1[i]; u2 = d2[i];
                    946:                 t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
                    947:                 t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
                    948:                 t1 = u1&  0xe00000; t2 = u2&  0xe00000; if ( t1&&t2 ) return 0;
                    949:                 t1 = u1&  0x1c0000; t2 = u2&  0x1c0000; if ( t1&&t2 ) return 0;
                    950:                 t1 = u1&   0x38000; t2 = u2&   0x38000; if ( t1&&t2 ) return 0;
                    951:                 t1 = u1&    0x7000; t2 = u2&    0x7000; if ( t1&&t2 ) return 0;
                    952:                 t1 = u1&     0xe00; t2 = u2&     0xe00; if ( t1&&t2 ) return 0;
                    953:                 t1 = u1&     0x1c0; t2 = u2&     0x1c0; if ( t1&&t2 ) return 0;
                    954:                 t1 = u1&      0x38; t2 = u2&      0x38; if ( t1&&t2 ) return 0;
                    955:                 t1 = u1&       0x7; t2 = u2&       0x7; if ( t1&&t2 ) return 0;
                    956:             }
                    957:             return 1;
                    958:             break;
                    959:         case 4:
                    960:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    961:                 u1 = d1[i]; u2 = d2[i];
                    962:                 t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
                    963:                 t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
                    964:                 t1 = u1&  0xf00000; t2 = u2&  0xf00000; if ( t1&&t2 ) return 0;
                    965:                 t1 = u1&   0xf0000; t2 = u2&   0xf0000; if ( t1&&t2 ) return 0;
                    966:                 t1 = u1&    0xf000; t2 = u2&    0xf000; if ( t1&&t2 ) return 0;
                    967:                 t1 = u1&     0xf00; t2 = u2&     0xf00; if ( t1&&t2 ) return 0;
                    968:                 t1 = u1&      0xf0; t2 = u2&      0xf0; if ( t1&&t2 ) return 0;
                    969:                 t1 = u1&       0xf; t2 = u2&       0xf; if ( t1&&t2 ) return 0;
                    970:             }
                    971:             return 1;
                    972:             break;
                    973:         case 6:
                    974:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    975:                 u1 = d1[i]; u2 = d2[i];
                    976:                 t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
                    977:                 t1 = u1&  0xfc0000; t2 = u2&  0xfc0000; if ( t1&&t2 ) return 0;
                    978:                 t1 = u1&   0x3f000; t2 = u2&   0x3f000; if ( t1&&t2 ) return 0;
                    979:                 t1 = u1&     0xfc0; t2 = u2&     0xfc0; if ( t1&&t2 ) return 0;
                    980:                 t1 = u1&      0x3f; t2 = u2&      0x3f; if ( t1&&t2 ) return 0;
                    981:             }
                    982:             return 1;
                    983:             break;
                    984:         case 8:
                    985:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    986:                 u1 = d1[i]; u2 = d2[i];
                    987:                 t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
                    988:                 t1 = u1&  0xff0000; t2 = u2&  0xff0000; if ( t1&&t2 ) return 0;
                    989:                 t1 = u1&    0xff00; t2 = u2&    0xff00; if ( t1&&t2 ) return 0;
                    990:                 t1 = u1&      0xff; t2 = u2&      0xff; if ( t1&&t2 ) return 0;
                    991:             }
                    992:             return 1;
                    993:             break;
                    994:         case 16:
                    995:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    996:                 u1 = d1[i]; u2 = d2[i];
                    997:                 t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
                    998:                 t1 = u1&    0xffff; t2 = u2&    0xffff; if ( t1&&t2 ) return 0;
                    999:             }
                   1000:             return 1;
                   1001:             break;
                   1002:         case 32:
                   1003:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                   1004:                 if ( d1[i] && d2[i] ) return 0;
                   1005:             return 1;
                   1006:             break;
                   1007:         default:
                   1008:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1009:                 u1 = d1[i]; u2 = d2[i];
                   1010:                 for ( j = 0; j < nd_epw; j++ ) {
                   1011:                     if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                   1012:                     u1 >>= nd_bpe; u2 >>= nd_bpe;
                   1013:                 }
                   1014:             }
                   1015:             return 1;
                   1016:             break;
                   1017:     }
1.65      noro     1018: #else
1.157     noro     1019:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1020:         u1 = d1[i]; u2 = d2[i];
                   1021:         for ( j = 0; j < nd_epw; j++ ) {
                   1022:             if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                   1023:             u1 >>= nd_bpe; u2 >>= nd_bpe;
                   1024:         }
                   1025:     }
                   1026:     return 1;
1.65      noro     1027: #endif
1.1       noro     1028: }
                   1029:
1.114     noro     1030: int ndl_check_bound(UINT *d1,UINT *d2)
1.1       noro     1031: {
1.157     noro     1032:     UINT u2;
                   1033:     int i,j,ind,k;
1.1       noro     1034:
1.157     noro     1035:     ind = 0;
1.65      noro     1036: #if USE_UNROLL
1.157     noro     1037:     switch ( nd_bpe ) {
                   1038:         case 3:
                   1039:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1040:                 u2 = d2[i];
                   1041:                 if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
                   1042:                 if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
                   1043:                 if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
                   1044:                 if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
                   1045:                 if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
                   1046:                 if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
                   1047:                 if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
                   1048:                 if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
                   1049:                 if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
                   1050:                 if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
                   1051:             }
                   1052:             return 0;
                   1053:             break;
                   1054:         case 4:
                   1055:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1056:                 u2 = d2[i];
                   1057:                 if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
                   1058:                 if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
                   1059:                 if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
                   1060:                 if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
                   1061:                 if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
                   1062:                 if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
                   1063:                 if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
                   1064:                 if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
                   1065:             }
                   1066:             return 0;
                   1067:             break;
                   1068:         case 6:
                   1069:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1070:                 u2 = d2[i];
                   1071:                 if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
                   1072:                 if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
                   1073:                 if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
                   1074:                 if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
                   1075:                 if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
                   1076:             }
                   1077:             return 0;
                   1078:             break;
                   1079:         case 8:
                   1080:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1081:                 u2 = d2[i];
                   1082:                 if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
                   1083:                 if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
                   1084:                 if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
                   1085:                 if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
                   1086:             }
                   1087:             return 0;
                   1088:             break;
                   1089:         case 16:
                   1090:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1091:                 u2 = d2[i];
                   1092:                 if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
                   1093:                 if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
                   1094:             }
                   1095:             return 0;
                   1096:             break;
                   1097:         case 32:
                   1098:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                   1099:                 if ( d1[i]+d2[i]<d1[i] ) return 1;
                   1100:             return 0;
                   1101:             break;
                   1102:         default:
                   1103:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1104:                 u2 = d2[i];
                   1105:                 k = (nd_epw-1)*nd_bpe;
                   1106:                 for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1107:                     if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1108:             }
                   1109:             return 0;
                   1110:             break;
                   1111:     }
1.65      noro     1112: #else
1.157     noro     1113:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1114:         u2 = d2[i];
                   1115:         k = (nd_epw-1)*nd_bpe;
                   1116:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1117:             if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1118:     }
                   1119:     return 0;
1.65      noro     1120: #endif
1.1       noro     1121: }
                   1122:
1.114     noro     1123: int ndl_check_bound2(int index,UINT *d2)
                   1124: {
1.157     noro     1125:     return ndl_check_bound(nd_bound[index],d2);
1.114     noro     1126: }
                   1127:
1.61      noro     1128: INLINE int ndl_hash_value(UINT *d)
1.1       noro     1129: {
1.157     noro     1130:     int i;
                   1131:     int r;
1.1       noro     1132:
1.157     noro     1133:     r = 0;
                   1134:     for ( i = 0; i < nd_wpd; i++ )
                   1135:         r = ((r<<16)+d[i])%REDTAB_LEN;
                   1136:     return r;
1.1       noro     1137: }
                   1138:
1.63      noro     1139: INLINE int ndl_find_reducer(UINT *dg)
1.1       noro     1140: {
1.157     noro     1141:     RHist r;
                   1142:     int d,k,i;
1.1       noro     1143:
1.157     noro     1144:     d = ndl_hash_value(dg);
                   1145:     for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   1146:         if ( ndl_equal(dg,DL(r)) ) {
                   1147:             if ( k > 0 ) nd_notfirst++;
                   1148:             nd_found++;
                   1149:             return r->index;
                   1150:         }
                   1151:     }
                   1152:     if ( Reverse )
                   1153:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   1154:             r = nd_psh[i];
                   1155:             if ( ndl_reducible(dg,DL(r)) ) {
                   1156:                 nd_create++;
                   1157:                 nd_append_red(dg,i);
                   1158:                 return i;
                   1159:             }
                   1160:         }
                   1161:     else
                   1162:         for ( i = 0; i < nd_psn; i++ ) {
                   1163:             r = nd_psh[i];
                   1164:             if ( ndl_reducible(dg,DL(r)) ) {
                   1165:                 nd_create++;
                   1166:                 nd_append_red(dg,i);
                   1167:                 return i;
                   1168:             }
                   1169:         }
                   1170:     return -1;
1.1       noro     1171: }
                   1172:
1.63      noro     1173: ND nd_merge(ND p1,ND p2)
                   1174: {
1.157     noro     1175:     int n,c;
                   1176:     int t,can,td1,td2;
                   1177:     ND r;
                   1178:     NM m1,m2,mr0,mr,s;
                   1179:
                   1180:     if ( !p1 ) return p2;
                   1181:     else if ( !p2 ) return p1;
                   1182:     else {
                   1183:         can = 0;
                   1184:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1185:             c = DL_COMPARE(DL(m1),DL(m2));
                   1186:             switch ( c ) {
                   1187:                 case 0:
                   1188:                     s = m1; m1 = NEXT(m1);
                   1189:                     can++; NEXTNM2(mr0,mr,s);
                   1190:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1191:                     break;
                   1192:                 case 1:
                   1193:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1194:                     break;
                   1195:                 case -1:
                   1196:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1197:                     break;
                   1198:             }
                   1199:         }
                   1200:         if ( !mr0 )
                   1201:             if ( m1 ) mr0 = m1;
                   1202:             else if ( m2 ) mr0 = m2;
                   1203:             else return 0;
                   1204:         else if ( m1 ) NEXT(mr) = m1;
                   1205:         else if ( m2 ) NEXT(mr) = m2;
                   1206:         else NEXT(mr) = 0;
                   1207:         BDY(p1) = mr0;
                   1208:         SG(p1) = MAX(SG(p1),SG(p2));
                   1209:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1210:         FREEND(p2);
                   1211:         return p1;
                   1212:     }
1.63      noro     1213: }
                   1214:
1.31      noro     1215: ND nd_add(int mod,ND p1,ND p2)
1.1       noro     1216: {
1.157     noro     1217:     int n,c;
                   1218:     int t,can,td1,td2;
                   1219:     ND r;
                   1220:     NM m1,m2,mr0,mr,s;
                   1221:
                   1222:     if ( !p1 ) return p2;
                   1223:     else if ( !p2 ) return p1;
                   1224:     else if ( mod == -1 ) return nd_add_sf(p1,p2);
1.232     noro     1225:     else if ( mod == -2 ) return nd_add_lf(p1,p2);
1.157     noro     1226:     else if ( !mod ) return nd_add_q(p1,p2);
                   1227:     else {
                   1228:         can = 0;
                   1229:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1230:             c = DL_COMPARE(DL(m1),DL(m2));
                   1231:             switch ( c ) {
                   1232:                 case 0:
                   1233:                     t = ((CM(m1))+(CM(m2))) - mod;
                   1234:                     if ( t < 0 ) t += mod;
                   1235:                     s = m1; m1 = NEXT(m1);
                   1236:                     if ( t ) {
                   1237:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1238:                     } else {
                   1239:                         can += 2; FREENM(s);
                   1240:                     }
                   1241:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1242:                     break;
                   1243:                 case 1:
                   1244:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1245:                     break;
                   1246:                 case -1:
                   1247:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1248:                     break;
                   1249:             }
                   1250:         }
                   1251:         if ( !mr0 )
                   1252:             if ( m1 ) mr0 = m1;
                   1253:             else if ( m2 ) mr0 = m2;
                   1254:             else return 0;
                   1255:         else if ( m1 ) NEXT(mr) = m1;
                   1256:         else if ( m2 ) NEXT(mr) = m2;
                   1257:         else NEXT(mr) = 0;
                   1258:         BDY(p1) = mr0;
                   1259:         SG(p1) = MAX(SG(p1),SG(p2));
                   1260:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1261:         FREEND(p2);
                   1262:         return p1;
                   1263:     }
1.95      noro     1264: }
                   1265:
                   1266: /* XXX on opteron, the inlined manipulation of destructive additon of
                   1267:  * two NM seems to make gcc optimizer get confused, so the part is
                   1268:  * done in a function.
                   1269:  */
                   1270:
1.113     noro     1271: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
1.95      noro     1272: {
1.157     noro     1273:     NM s;
                   1274:     P t;
                   1275:     int can;
                   1276:
                   1277:     addp(nd_vc,CP(*m1),CP(*m2),&t);
                   1278:     s = *m1; *m1 = NEXT(*m1);
                   1279:     if ( t ) {
                   1280:         can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
                   1281:     } else {
                   1282:         can = 2; FREENM(s);
                   1283:     }
                   1284:     s = *m2; *m2 = NEXT(*m2); FREENM(s);
                   1285:     return can;
1.95      noro     1286: }
                   1287:
1.113     noro     1288: ND nd_add_q(ND p1,ND p2)
1.95      noro     1289: {
1.157     noro     1290:     int n,c,can;
                   1291:     ND r;
                   1292:     NM m1,m2,mr0,mr,s;
                   1293:     P t;
                   1294:
                   1295:     if ( !p1 ) return p2;
                   1296:     else if ( !p2 ) return p1;
                   1297:     else {
                   1298:         can = 0;
                   1299:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1300:             c = DL_COMPARE(DL(m1),DL(m2));
                   1301:             switch ( c ) {
                   1302:                 case 0:
1.95      noro     1303: #if defined(__x86_64__)
1.157     noro     1304:                     can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
1.95      noro     1305: #else
1.157     noro     1306:                     addp(nd_vc,CP(m1),CP(m2),&t);
                   1307:                     s = m1; m1 = NEXT(m1);
                   1308:                     if ( t ) {
                   1309:                         can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
                   1310:                     } else {
                   1311:                         can += 2; FREENM(s);
                   1312:                     }
                   1313:                     s = m2; m2 = NEXT(m2); FREENM(s);
1.95      noro     1314: #endif
1.157     noro     1315:                     break;
                   1316:                 case 1:
                   1317:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1318:                     break;
                   1319:                 case -1:
                   1320:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1321:                     break;
                   1322:             }
                   1323:         }
                   1324:         if ( !mr0 )
                   1325:             if ( m1 ) mr0 = m1;
                   1326:             else if ( m2 ) mr0 = m2;
                   1327:             else return 0;
                   1328:         else if ( m1 ) NEXT(mr) = m1;
                   1329:         else if ( m2 ) NEXT(mr) = m2;
                   1330:         else NEXT(mr) = 0;
                   1331:         BDY(p1) = mr0;
                   1332:         SG(p1) = MAX(SG(p1),SG(p2));
                   1333:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1334:         FREEND(p2);
                   1335:         return p1;
                   1336:     }
1.17      noro     1337: }
                   1338:
1.71      noro     1339: ND nd_add_sf(ND p1,ND p2)
                   1340: {
1.157     noro     1341:     int n,c,can;
                   1342:     ND r;
                   1343:     NM m1,m2,mr0,mr,s;
                   1344:     int t;
                   1345:
                   1346:     if ( !p1 ) return p2;
                   1347:     else if ( !p2 ) return p1;
                   1348:     else {
                   1349:         can = 0;
                   1350:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1351:             c = DL_COMPARE(DL(m1),DL(m2));
                   1352:             switch ( c ) {
                   1353:                 case 0:
                   1354:                     t = _addsf(CM(m1),CM(m2));
                   1355:                     s = m1; m1 = NEXT(m1);
                   1356:                     if ( t ) {
                   1357:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1358:                     } else {
                   1359:                         can += 2; FREENM(s);
                   1360:                     }
                   1361:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1362:                     break;
                   1363:                 case 1:
                   1364:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1365:                     break;
                   1366:                 case -1:
                   1367:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1368:                     break;
                   1369:             }
                   1370:         }
                   1371:         if ( !mr0 )
                   1372:             if ( m1 ) mr0 = m1;
                   1373:             else if ( m2 ) mr0 = m2;
                   1374:             else return 0;
                   1375:         else if ( m1 ) NEXT(mr) = m1;
                   1376:         else if ( m2 ) NEXT(mr) = m2;
                   1377:         else NEXT(mr) = 0;
                   1378:         BDY(p1) = mr0;
                   1379:         SG(p1) = MAX(SG(p1),SG(p2));
                   1380:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1381:         FREEND(p2);
                   1382:         return p1;
                   1383:     }
1.71      noro     1384: }
                   1385:
1.232     noro     1386:
1.167     noro     1387: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
1.146     noro     1388: {
1.157     noro     1389:     int c,c1,c2;
                   1390:     Q cg,cred,gcd,tq;
                   1391:     P cgp,credp,gcdp;
                   1392:     Obj tr,tr1;
                   1393:
1.167     noro     1394:     if ( mod == -1 ) {
1.157     noro     1395:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1.167     noro     1396:         *divp = (Obj)ONE;
1.232     noro     1397:     } else if ( mod == -2 ) {
                   1398:         GZ inv,t;
                   1399:         divlf(ONEGZ,HCZ(p),&inv);
                   1400:         chsgnlf(HCZ(g),&t);
                   1401:         mullf(inv,t,&CZ(mul));
                   1402:         *divp = (Obj)ONE;
1.167     noro     1403:     } else if ( mod ) {
1.157     noro     1404:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1405:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
1.167     noro     1406:         *divp = (Obj)ONE;
1.157     noro     1407:     } else if ( nd_vc ) {
                   1408:         ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
                   1409:         divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
                   1410:         chsgnp(cgp,&CP(mul));
                   1411:         nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
                   1412:         if ( dn ) {
                   1413:             mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
                   1414:             reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
                   1415:         }
1.167     noro     1416:         *divp = (Obj)credp;
1.157     noro     1417:     } else {
                   1418:         igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
                   1419:         chsgnq(cg,&CQ(mul));
                   1420:         nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
                   1421:         if ( dn ) {
                   1422:             mulq(dn->z,cred,&tq); dn->z = tq;
                   1423:         }
1.167     noro     1424:         *divp = (Obj)cred;
1.157     noro     1425:     }
                   1426:     return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1.146     noro     1427: }
                   1428:
1.1       noro     1429: /* ret=1 : success, ret=0 : overflow */
1.146     noro     1430: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,NDC dn,ND *rp)
1.1       noro     1431: {
1.157     noro     1432:     NM m,mrd,tail;
                   1433:     NM mul;
                   1434:     int n,sugar,psugar,sugar0,stat,index;
                   1435:     int c,c1,c2,dummy;
                   1436:     RHist h;
                   1437:     NDV p,red;
1.167     noro     1438:     Q cg,cred,gcd,tq,qq,iq;
                   1439:     DP dmul;
                   1440:     NODE node;
                   1441:     LIST hist;
1.157     noro     1442:     double hmag;
                   1443:     P tp,tp1;
1.167     noro     1444:     Obj tr,tr1,div;
                   1445:     union oNDC hg;
                   1446:     P cont;
1.157     noro     1447:
                   1448:     if ( dn ) {
                   1449:         if ( mod )
                   1450:             dn->m = 1;
                   1451:         else if ( nd_vc )
                   1452:             dn->r = (R)ONE;
                   1453:         else
                   1454:             dn->z = ONE;
                   1455:     }
                   1456:     if ( !g ) {
                   1457:         *rp = d;
                   1458:         return 1;
                   1459:     }
                   1460:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1461:
                   1462:     sugar0 = sugar = SG(g);
                   1463:     n = NV(g);
1.235     noro     1464:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.157     noro     1465:     if ( d )
                   1466:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1467:     for ( ; g; ) {
                   1468:         index = ndl_find_reducer(HDL(g));
                   1469:         if ( index >= 0 ) {
                   1470:             h = nd_psh[index];
                   1471:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1472:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1473:                 nd_free(g); nd_free(d);
                   1474:                 return 0;
                   1475:             }
                   1476:             p = nd_demand ? ndv_load(index) : ps[index];
1.167     noro     1477:             /* d+g -> div*(d+g)+mul*p */
                   1478:             g = nd_reduce2(mod,d,g,p,mul,dn,&div);
1.172     noro     1479:             if ( nd_gentrace ) {
1.167     noro     1480:                 /* Trace=[div,index,mul,ONE] */
                   1481:                 STOQ(index,iq);
                   1482:                 nmtodp(mod,mul,&dmul);
                   1483:                 node = mknode(4,div,iq,dmul,ONE);
                   1484:             }
1.157     noro     1485:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1.193     noro     1486:             if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
1.167     noro     1487:                 hg = HCU(g);
1.157     noro     1488:                 nd_removecont2(d,g);
1.172     noro     1489:                 if ( dn || nd_gentrace ) {
1.167     noro     1490:                     /* overwrite cont : Trace=[div,index,mul,cont] */
                   1491:                     cont = ndc_div(mod,hg,HCU(g));
                   1492:                     if ( dn ) {
                   1493:                         if ( nd_vc ) {
                   1494:                             divr(nd_vc,(Obj)dn->r,(Obj)cont,&tr);
                   1495:                             reductr(nd_vc,(Obj)tr,&tr1); dn->r = (R)tr1;
                   1496:                         } else divq(dn->z,(Q)cont,&dn->z);
1.157     noro     1497:                     }
1.172     noro     1498:                     if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
1.157     noro     1499:                 }
                   1500:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1501:             }
1.167     noro     1502:             MKLIST(hist,node);
                   1503:             MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1.157     noro     1504:         } else if ( !full ) {
                   1505:             *rp = g;
                   1506:             return 1;
                   1507:         } else {
                   1508:             m = BDY(g);
                   1509:             if ( NEXT(m) ) {
                   1510:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1511:             } else {
                   1512:                 FREEND(g); g = 0;
                   1513:             }
                   1514:             if ( d ) {
                   1515:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1516:             } else {
                   1517:                 MKND(n,m,1,d); tail = BDY(d);
                   1518:             }
                   1519:         }
                   1520:     }
                   1521:     if ( d ) SG(d) = sugar;
                   1522:     *rp = d;
                   1523:     return 1;
1.1       noro     1524: }
1.28      noro     1525:
1.53      noro     1526: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1.25      noro     1527: {
1.157     noro     1528:     int hindex,index;
                   1529:     NDV p;
                   1530:     ND u,d,red;
                   1531:     NODE l;
                   1532:     NM mul,m,mrd,tail;
                   1533:     int sugar,psugar,n,h_reducible;
                   1534:     PGeoBucket bucket;
                   1535:     int c,c1,c2;
                   1536:     Q cg,cred,gcd,zzz;
                   1537:     RHist h;
                   1538:     double hmag,gmag;
                   1539:     int count = 0;
                   1540:     int hcount = 0;
                   1541:
                   1542:     if ( !g ) {
                   1543:         *rp = 0;
                   1544:         return 1;
                   1545:     }
                   1546:     sugar = SG(g);
                   1547:     n = NV(g);
                   1548:     if ( !mod ) hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
                   1549:     bucket = create_pbucket();
                   1550:     add_pbucket(mod,bucket,g);
                   1551:     d = 0;
1.235     noro     1552:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.157     noro     1553:     while ( 1 ) {
1.232     noro     1554:         if ( mod > 0 || mod == -1 )
                   1555:           hindex = head_pbucket(mod,bucket);
                   1556:         else if ( mod == -2 )
                   1557:           hindex = head_pbucket_lf(bucket);
                   1558:         else
                   1559:           hindex = head_pbucket_q(bucket);
1.157     noro     1560:         if ( hindex < 0 ) {
                   1561:             if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1562:             if ( d ) SG(d) = sugar;
                   1563:             *rp = d;
                   1564:             return 1;
                   1565:         }
                   1566:         g = bucket->body[hindex];
                   1567:         index = ndl_find_reducer(HDL(g));
                   1568:         if ( index >= 0 ) {
                   1569:             count++;
                   1570:             if ( !d ) hcount++;
                   1571:             h = nd_psh[index];
                   1572:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1573:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1574:                 nd_free(d);
                   1575:                 free_pbucket(bucket);
                   1576:                 *rp = 0;
                   1577:                 return 0;
                   1578:             }
                   1579:             p = ps[index];
                   1580:             if ( mod == -1 )
                   1581:                 CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1.232     noro     1582:             else if ( mod == -2 ) {
                   1583:                 GZ inv,t;
                   1584:                 divlf(ONEGZ,HCZ(p),&inv);
                   1585:                 chsgnlf(HCZ(g),&t);
                   1586:                 mullf(inv,t,&CZ(mul));
                   1587:             } else if ( mod ) {
1.157     noro     1588:                 c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1589:                 DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1590:             } else {
                   1591:                 igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
                   1592:                 chsgnq(cg,&CQ(mul));
                   1593:                 nd_mul_c_q(d,(P)cred);
                   1594:                 mulq_pbucket(bucket,cred);
                   1595:                 g = bucket->body[hindex];
                   1596:                 gmag = (double)p_mag((P)HCQ(g));
                   1597:             }
                   1598:             red = ndv_mul_nm(mod,mul,p);
                   1599:             bucket->body[hindex] = nd_remove_head(g);
                   1600:             red = nd_remove_head(red);
                   1601:             add_pbucket(mod,bucket,red);
                   1602:             psugar = SG(p)+TD(DL(mul));
                   1603:             sugar = MAX(sugar,psugar);
                   1604:             if ( !mod && hmag && (gmag > hmag) ) {
                   1605:                 g = normalize_pbucket(mod,bucket);
                   1606:                 if ( !g ) {
                   1607:                     if ( d ) SG(d) = sugar;
                   1608:                     *rp = d;
                   1609:                     return 1;
                   1610:                 }
                   1611:                 nd_removecont2(d,g);
                   1612:                 hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
                   1613:                 add_pbucket(mod,bucket,g);
                   1614:             }
                   1615:         } else if ( !full ) {
                   1616:             g = normalize_pbucket(mod,bucket);
                   1617:             if ( g ) SG(g) = sugar;
                   1618:             *rp = g;
                   1619:             return 1;
                   1620:         } else {
                   1621:             m = BDY(g);
                   1622:             if ( NEXT(m) ) {
                   1623:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1624:             } else {
                   1625:                 FREEND(g); g = 0;
                   1626:             }
                   1627:             bucket->body[hindex] = g;
                   1628:             NEXT(m) = 0;
                   1629:             if ( d ) {
                   1630:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1631:             } else {
                   1632:                 MKND(n,m,1,d); tail = BDY(d);
                   1633:             }
                   1634:         }
                   1635:     }
1.25      noro     1636: }
1.27      noro     1637:
1.61      noro     1638: /* input : list of NDV, cand : list of NDV */
1.28      noro     1639:
1.170     noro     1640: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1.28      noro     1641: {
1.157     noro     1642:     int n,i,stat;
                   1643:     ND nf,d;
                   1644:     NDV r;
                   1645:     NODE t,s;
                   1646:     union oNDC dn;
1.168     noro     1647:     Q q;
                   1648:     LIST list;
1.45      noro     1649:
1.172     noro     1650:     ndv_setup(m,0,cand,nd_gentrace?1:0,1);
1.157     noro     1651:     n = length(cand);
1.28      noro     1652:
1.245     noro     1653:   if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
1.157     noro     1654:     /* membercheck : list is a subset of Id(cand) ? */
1.168     noro     1655:     for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
1.45      noro     1656: again:
1.245     noro     1657:     nd_tracelist = 0;
1.157     noro     1658:         if ( nd_bpe > obpe )
                   1659:             r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
                   1660:         else
                   1661:             r = (NDV)BDY(t);
1.247     noro     1662: #if 0
                   1663:         // moved to nd_f4_lf_trace()
1.246     noro     1664:         if ( m == -2 ) ndv_mod(m,r);
1.247     noro     1665: #endif
1.171     noro     1666:         d = ndvtond(m,r);
                   1667:         stat = nd_nf(m,0,d,nd_ps,0,0,&nf);
1.157     noro     1668:         if ( !stat ) {
                   1669:             nd_reconstruct(0,0);
                   1670:             goto again;
                   1671:         } else if ( nf ) return 0;
1.245     noro     1672:     if ( nd_gentrace ) {
                   1673:       nd_tracelist = reverse_node(nd_tracelist);
                   1674:       MKLIST(list,nd_tracelist);
                   1675:       STOQ(i,q); s = mknode(2,q,list); MKLIST(list,s);
                   1676:       MKNODE(s,list,nd_alltracelist);
                   1677:       nd_alltracelist = s; nd_tracelist = 0;
                   1678:     }
1.157     noro     1679:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   1680:     }
                   1681:     if ( DP_Print ) { printf("\n"); }
                   1682:     return 1;
1.23      noro     1683: }
1.1       noro     1684:
                   1685: ND nd_remove_head(ND p)
                   1686: {
1.157     noro     1687:     NM m;
1.1       noro     1688:
1.157     noro     1689:     m = BDY(p);
                   1690:     if ( !NEXT(m) ) {
                   1691:         FREEND(p); p = 0;
                   1692:     } else {
                   1693:         BDY(p) = NEXT(m); LEN(p)--;
                   1694:     }
                   1695:     FREENM(m);
                   1696:     return p;
1.1       noro     1697: }
                   1698:
1.69      noro     1699: ND nd_separate_head(ND p,ND *head)
                   1700: {
1.157     noro     1701:     NM m,m0;
                   1702:     ND r;
1.69      noro     1703:
1.157     noro     1704:     m = BDY(p);
                   1705:     if ( !NEXT(m) ) {
                   1706:         *head = p; p = 0;
                   1707:     } else {
                   1708:         m0 = m;
                   1709:         BDY(p) = NEXT(m); LEN(p)--;
                   1710:         NEXT(m0) = 0;
                   1711:         MKND(NV(p),m0,1,r);
                   1712:         *head = r;
                   1713:     }
                   1714:     return p;
1.69      noro     1715: }
                   1716:
1.1       noro     1717: PGeoBucket create_pbucket()
                   1718: {
                   1719:     PGeoBucket g;
1.157     noro     1720:
                   1721:     g = CALLOC(1,sizeof(struct oPGeoBucket));
                   1722:     g->m = -1;
                   1723:     return g;
1.1       noro     1724: }
                   1725:
1.25      noro     1726: void free_pbucket(PGeoBucket b) {
1.157     noro     1727:     int i;
1.25      noro     1728:
1.157     noro     1729:     for ( i = 0; i <= b->m; i++ )
                   1730:         if ( b->body[i] ) {
                   1731:             nd_free(b->body[i]);
                   1732:             b->body[i] = 0;
                   1733:         }
1.200     noro     1734:     GCFREE(b);
1.25      noro     1735: }
                   1736:
1.63      noro     1737: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   1738: {
1.157     noro     1739:     int l,i,k,m;
1.63      noro     1740:
1.157     noro     1741:     if ( !d )
                   1742:         return;
                   1743:     l = LEN(d);
                   1744:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   1745:     /* 2^(k-1) < l <= 2^k (=m) */
                   1746:     d = nd_merge(g->body[k],d);
                   1747:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   1748:         g->body[k] = 0;
                   1749:         d = nd_merge(g->body[k+1],d);
                   1750:     }
                   1751:     g->body[k] = d;
                   1752:     g->m = MAX(g->m,k);
1.63      noro     1753: }
                   1754:
1.31      noro     1755: void add_pbucket(int mod,PGeoBucket g,ND d)
1.1       noro     1756: {
1.157     noro     1757:     int l,i,k,m;
1.1       noro     1758:
1.157     noro     1759:     if ( !d )
                   1760:         return;
                   1761:     l = LEN(d);
                   1762:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   1763:     /* 2^(k-1) < l <= 2^k (=m) */
                   1764:     d = nd_add(mod,g->body[k],d);
                   1765:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   1766:         g->body[k] = 0;
                   1767:         d = nd_add(mod,g->body[k+1],d);
                   1768:     }
                   1769:     g->body[k] = d;
                   1770:     g->m = MAX(g->m,k);
1.1       noro     1771: }
                   1772:
1.113     noro     1773: void mulq_pbucket(PGeoBucket g,Q c)
1.26      noro     1774: {
1.157     noro     1775:     int k;
1.26      noro     1776:
1.157     noro     1777:     for ( k = 0; k <= g->m; k++ )
                   1778:         nd_mul_c_q(g->body[k],(P)c);
1.26      noro     1779: }
                   1780:
1.63      noro     1781: NM remove_head_pbucket_symbolic(PGeoBucket g)
                   1782: {
1.157     noro     1783:     int j,i,k,c;
                   1784:     NM head;
                   1785:
                   1786:     k = g->m;
                   1787:     j = -1;
                   1788:     for ( i = 0; i <= k; i++ ) {
                   1789:         if ( !g->body[i] ) continue;
                   1790:         if ( j < 0 ) j = i;
                   1791:         else {
                   1792:             c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
                   1793:             if ( c > 0 )
                   1794:                 j = i;
                   1795:             else if ( c == 0 )
                   1796:                 g->body[i] = nd_remove_head(g->body[i]);
                   1797:         }
                   1798:     }
                   1799:     if ( j < 0 ) return 0;
                   1800:     else {
                   1801:         head = BDY(g->body[j]);
                   1802:         if ( !NEXT(head) ) {
                   1803:             FREEND(g->body[j]);
                   1804:             g->body[j] = 0;
                   1805:         } else {
                   1806:             BDY(g->body[j]) = NEXT(head);
                   1807:             LEN(g->body[j])--;
                   1808:         }
                   1809:         return head;
                   1810:     }
1.63      noro     1811: }
                   1812:
1.19      noro     1813: int head_pbucket(int mod,PGeoBucket g)
1.1       noro     1814: {
1.157     noro     1815:     int j,i,c,k,nv,sum;
                   1816:     UINT *di,*dj;
                   1817:     ND gi,gj;
                   1818:
                   1819:     k = g->m;
                   1820:     while ( 1 ) {
                   1821:         j = -1;
                   1822:         for ( i = 0; i <= k; i++ ) {
                   1823:             if ( !(gi = g->body[i]) )
                   1824:                 continue;
                   1825:             if ( j < 0 ) {
                   1826:                 j = i;
                   1827:                 gj = g->body[j];
                   1828:                 dj = HDL(gj);
                   1829:                 sum = HCM(gj);
                   1830:             } else {
                   1831:                 c = DL_COMPARE(HDL(gi),dj);
                   1832:                 if ( c > 0 ) {
                   1833:                     if ( sum ) HCM(gj) = sum;
                   1834:                     else g->body[j] = nd_remove_head(gj);
                   1835:                     j = i;
                   1836:                     gj = g->body[j];
                   1837:                     dj = HDL(gj);
                   1838:                     sum = HCM(gj);
                   1839:                 } else if ( c == 0 ) {
                   1840:                     if ( mod == -1 )
                   1841:                         sum = _addsf(sum,HCM(gi));
                   1842:                     else {
                   1843:                         sum = sum+HCM(gi)-mod;
                   1844:                         if ( sum < 0 ) sum += mod;
                   1845:                     }
                   1846:                     g->body[i] = nd_remove_head(gi);
                   1847:                 }
                   1848:             }
                   1849:         }
                   1850:         if ( j < 0 ) return -1;
                   1851:         else if ( sum ) {
                   1852:             HCM(gj) = sum;
                   1853:             return j;
                   1854:         } else
                   1855:             g->body[j] = nd_remove_head(gj);
                   1856:     }
1.26      noro     1857: }
                   1858:
1.113     noro     1859: int head_pbucket_q(PGeoBucket g)
1.26      noro     1860: {
1.157     noro     1861:     int j,i,c,k,nv;
                   1862:     Q sum,t;
                   1863:     ND gi,gj;
                   1864:
                   1865:     k = g->m;
                   1866:     while ( 1 ) {
                   1867:         j = -1;
                   1868:         for ( i = 0; i <= k; i++ ) {
                   1869:             if ( !(gi = g->body[i]) ) continue;
                   1870:             if ( j < 0 ) {
                   1871:                 j = i;
                   1872:                 gj = g->body[j];
                   1873:                 sum = HCQ(gj);
                   1874:             } else {
                   1875:                 nv = NV(gi);
                   1876:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   1877:                 if ( c > 0 ) {
                   1878:                     if ( sum ) HCQ(gj) = sum;
                   1879:                     else g->body[j] = nd_remove_head(gj);
                   1880:                     j = i;
                   1881:                     gj = g->body[j];
                   1882:                     sum = HCQ(gj);
                   1883:                 } else if ( c == 0 ) {
                   1884:                     addq(sum,HCQ(gi),&t);
                   1885:                     sum = t;
                   1886:                     g->body[i] = nd_remove_head(gi);
                   1887:                 }
                   1888:             }
                   1889:         }
                   1890:         if ( j < 0 ) return -1;
                   1891:         else if ( sum ) {
                   1892:             HCQ(gj) = sum;
                   1893:             return j;
                   1894:         } else
                   1895:             g->body[j] = nd_remove_head(gj);
                   1896:     }
1.1       noro     1897: }
                   1898:
1.232     noro     1899: int head_pbucket_lf(PGeoBucket g)
                   1900: {
                   1901:     int j,i,c,k,nv;
                   1902:     GZ sum,t;
                   1903:     ND gi,gj;
                   1904:
                   1905:     k = g->m;
                   1906:     while ( 1 ) {
                   1907:         j = -1;
                   1908:         for ( i = 0; i <= k; i++ ) {
                   1909:             if ( !(gi = g->body[i]) ) continue;
                   1910:             if ( j < 0 ) {
                   1911:                 j = i;
                   1912:                 gj = g->body[j];
                   1913:                 sum = HCZ(gj);
                   1914:             } else {
                   1915:                 nv = NV(gi);
                   1916:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   1917:                 if ( c > 0 ) {
                   1918:                     if ( sum ) HCZ(gj) = sum;
                   1919:                     else g->body[j] = nd_remove_head(gj);
                   1920:                     j = i;
                   1921:                     gj = g->body[j];
                   1922:                     sum = HCZ(gj);
                   1923:                 } else if ( c == 0 ) {
                   1924:                     addlf(sum,HCZ(gi),&t);
                   1925:                     sum = t;
                   1926:                     g->body[i] = nd_remove_head(gi);
                   1927:                 }
                   1928:             }
                   1929:         }
                   1930:         if ( j < 0 ) return -1;
                   1931:         else if ( sum ) {
                   1932:             HCZ(gj) = sum;
                   1933:             return j;
                   1934:         } else
                   1935:             g->body[j] = nd_remove_head(gj);
                   1936:     }
                   1937: }
                   1938:
1.25      noro     1939: ND normalize_pbucket(int mod,PGeoBucket g)
1.1       noro     1940: {
1.157     noro     1941:     int i;
                   1942:     ND r,t;
1.1       noro     1943:
1.157     noro     1944:     r = 0;
                   1945:     for ( i = 0; i <= g->m; i++ ) {
                   1946:         r = nd_add(mod,r,g->body[i]);
                   1947:         g->body[i] = 0;
                   1948:     }
                   1949:     g->m = -1;
                   1950:     return r;
1.1       noro     1951: }
                   1952:
1.150     noro     1953: #if 0
                   1954: void register_hcf(NDV p)
                   1955: {
1.157     noro     1956:     DCP dc,t;
                   1957:     P hc,h;
                   1958:     int c;
                   1959:     NODE l,l1,prev;
                   1960:
                   1961:     hc = p->body->c.p;
                   1962:     if ( !nd_vc || NUM(hc) ) return;
                   1963:     fctrp(nd_vc,hc,&dc);
                   1964:     for ( t = dc; t; t = NEXT(t) ) {
                   1965:         h = t->c;
                   1966:         if ( NUM(h) ) continue;
                   1967:         for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   1968:             c = compp(nd_vc,h,(P)BDY(l));
                   1969:             if ( c >= 0 ) break;
                   1970:         }
                   1971:         if ( !l || c > 0  ) {
                   1972:             MKNODE(l1,h,l);
                   1973:             if ( !prev )
                   1974:                 nd_hcf = l1;
                   1975:             else
                   1976:                 NEXT(prev) = l1;
                   1977:         }
                   1978:     }
1.150     noro     1979: }
                   1980: #else
                   1981: void register_hcf(NDV p)
                   1982: {
1.157     noro     1983:     DCP dc,t;
                   1984:     P hc,h,q;
                   1985:     Q dmy;
                   1986:     int c;
                   1987:     NODE l,l1,prev;
                   1988:
                   1989:     hc = p->body->c.p;
                   1990:     if ( NUM(hc) ) return;
                   1991:     ptozp(hc,1,&dmy,&h);
1.150     noro     1992: #if 1
1.157     noro     1993:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   1994:         while ( 1 ) {
                   1995:             if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
                   1996:             else break;
                   1997:         }
                   1998:     }
                   1999:     if ( NUM(h) ) return;
1.150     noro     2000: #endif
1.157     noro     2001:     for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   2002:         c = compp(nd_vc,h,(P)BDY(l));
                   2003:         if ( c >= 0 ) break;
                   2004:     }
                   2005:     if ( !l || c > 0  ) {
                   2006:         MKNODE(l1,h,l);
                   2007:         if ( !prev )
                   2008:             nd_hcf = l1;
                   2009:         else
                   2010:             NEXT(prev) = l1;
                   2011:     }
1.150     noro     2012: }
                   2013: #endif
                   2014:
1.122     noro     2015: int do_diagonalize(int sugar,int m)
1.92      noro     2016: {
1.157     noro     2017:     int i,nh,stat;
                   2018:     NODE r,g,t;
                   2019:     ND h,nf,s,head;
                   2020:     NDV nfv;
                   2021:     Q q,num,den;
1.167     noro     2022:     P nm,nmp,dn,mnp,dnp,cont,cont1;
                   2023:     union oNDC hc;
                   2024:     NODE node;
                   2025:     LIST l;
                   2026:     Q iq;
1.157     noro     2027:
                   2028:     for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
1.172     noro     2029:         if ( nd_gentrace ) {
1.167     noro     2030:             /* Trace = [1,index,1,1] */
                   2031:             STOQ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2032:             MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2033:         }
1.157     noro     2034:         if ( nd_demand )
                   2035:             nfv = ndv_load(i);
                   2036:         else
                   2037:             nfv = nd_ps[i];
                   2038:         s = ndvtond(m,nfv);
                   2039:         s = nd_separate_head(s,&head);
                   2040:         stat = nd_nf(m,head,s,nd_ps,1,0,&nf);
                   2041:         if ( !stat ) return 0;
                   2042:         ndv_free(nfv);
1.167     noro     2043:         hc = HCU(nf); nd_removecont(m,nf);
                   2044:         cont = ndc_div(m,hc,HCU(nf));
1.245     noro     2045:     if ( nd_gentrace ) finalize_tracelist(i,cont);
1.157     noro     2046:         nfv = ndtondv(m,nf);
                   2047:         nd_free(nf);
                   2048:         nd_bound[i] = ndv_compute_bound(nfv);
                   2049:         if ( !m ) register_hcf(nfv);
                   2050:         if ( nd_demand ) {
                   2051:             ndv_save(nfv,i);
                   2052:             ndv_free(nfv);
                   2053:         } else
                   2054:             nd_ps[i] = nfv;
                   2055:     }
                   2056:     return 1;
1.92      noro     2057: }
                   2058:
1.209     noro     2059: LIST compute_splist()
                   2060: {
1.245     noro     2061:   NODE g,tn0,tn,node;
                   2062:   LIST l0;
                   2063:   ND_pairs d,t;
                   2064:   int i;
                   2065:   Q i1,i2;
1.209     noro     2066:
                   2067:     g = 0; d = 0;
                   2068:     for ( i = 0; i < nd_psn; i++ ) {
                   2069:         d = update_pairs(d,g,i,0);
                   2070:         g = update_base(g,i);
                   2071:     }
1.245     noro     2072:   for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
                   2073:     NEXTNODE(tn0,tn);
1.209     noro     2074:         STOQ(t->i1,i1); STOQ(t->i2,i2);
                   2075:         node = mknode(2,i1,i2); MKLIST(l0,node);
1.245     noro     2076:     BDY(tn) = l0;
                   2077:   }
                   2078:   if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
                   2079:   return l0;
1.209     noro     2080: }
                   2081:
1.27      noro     2082: /* return value = 0 => input is not a GB */
                   2083:
1.168     noro     2084: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
1.1       noro     2085: {
1.157     noro     2086:     int i,nh,sugar,stat;
                   2087:     NODE r,g,t;
                   2088:     ND_pairs d;
                   2089:     ND_pairs l;
                   2090:     ND h,nf,s,head,nf1;
                   2091:     NDV nfv;
                   2092:     Q q,num,den;
1.167     noro     2093:     union oNDC dn,hc;
1.157     noro     2094:     int diag_count = 0;
1.167     noro     2095:     P cont;
                   2096:     LIST list;
1.157     noro     2097:
                   2098:     g = 0; d = 0;
                   2099:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     2100:         d = update_pairs(d,g,i,gensyz);
1.157     noro     2101:         g = update_base(g,i);
                   2102:     }
                   2103:     sugar = 0;
                   2104:     while ( d ) {
1.1       noro     2105: again:
1.157     noro     2106:         l = nd_minp(d,&d);
1.228     noro     2107:         if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
1.157     noro     2108:         if ( SG(l) != sugar ) {
                   2109:             if ( ishomo ) {
                   2110:                 diag_count = 0;
                   2111:                 stat = do_diagonalize(sugar,m);
                   2112:                 if ( !stat ) {
                   2113:                     NEXT(l) = d; d = l;
                   2114:                     d = nd_reconstruct(0,d);
                   2115:                     goto again;
                   2116:                 }
                   2117:             }
                   2118:             sugar = SG(l);
                   2119:             if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2120:         }
                   2121:         stat = nd_sp(m,0,l,&h);
                   2122:         if ( !stat ) {
                   2123:             NEXT(l) = d; d = l;
                   2124:             d = nd_reconstruct(0,d);
                   2125:             goto again;
                   2126:         }
1.41      noro     2127: #if USE_GEOBUCKET
1.172     noro     2128:         stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!Top,&nf)
1.167     noro     2129:                :nd_nf(m,0,h,nd_ps,!Top,0,&nf);
1.41      noro     2130: #else
1.157     noro     2131:         stat = nd_nf(m,0,h,nd_ps,!Top,0,&nf);
1.41      noro     2132: #endif
1.157     noro     2133:         if ( !stat ) {
                   2134:             NEXT(l) = d; d = l;
                   2135:             d = nd_reconstruct(0,d);
                   2136:             goto again;
                   2137:         } else if ( nf ) {
1.168     noro     2138:             if ( checkonly || gensyz ) return 0;
1.245     noro     2139:       if ( nd_newelim ) {
                   2140:         if ( nd_module ) {
                   2141:           if ( MPOS(HDL(nf)) > 1 ) return 0;
                   2142:         } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
                   2143:       }
1.157     noro     2144:             if ( DP_Print ) { printf("+"); fflush(stdout); }
1.167     noro     2145:             hc = HCU(nf);
1.157     noro     2146:             nd_removecont(m,nf);
                   2147:             if ( !m && nd_nalg ) {
                   2148:                 nd_monic(0,&nf);
                   2149:                 nd_removecont(m,nf);
                   2150:             }
1.172     noro     2151:             if ( nd_gentrace ) {
1.245     noro     2152:         cont = ndc_div(m,hc,HCU(nf));
                   2153:         if ( m || !UNIQ(cont) ) {
1.196     noro     2154:                     t = mknode(4,NULLP,NULLP,NULLP,cont);
1.167     noro     2155:                     MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.245     noro     2156:           nd_tracelist = t;
                   2157:         }
1.167     noro     2158:             }
1.157     noro     2159:             nfv = ndtondv(m,nf); nd_free(nf);
1.215     noro     2160:             nh = ndv_newps(m,nfv,0,0);
1.157     noro     2161:             if ( !m && (ishomo && ++diag_count == diag_period) ) {
                   2162:                 diag_count = 0;
                   2163:                 stat = do_diagonalize(sugar,m);
                   2164:                 if ( !stat ) {
                   2165:                     NEXT(l) = d; d = l;
                   2166:                     d = nd_reconstruct(1,d);
                   2167:                     goto again;
                   2168:                 }
                   2169:             }
1.168     noro     2170:             d = update_pairs(d,g,nh,0);
1.157     noro     2171:             g = update_base(g,nh);
                   2172:             FREENDP(l);
                   2173:         } else {
1.245     noro     2174:         if ( nd_gentrace && gensyz ) {
1.168     noro     2175:                 nd_tracelist = reverse_node(nd_tracelist);
1.245     noro     2176:         MKLIST(list,nd_tracelist);
1.168     noro     2177:                 STOQ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
                   2178:                 MKNODE(t,list,nd_alltracelist);
1.245     noro     2179:         nd_alltracelist = t; nd_tracelist = 0;
                   2180:       }
1.157     noro     2181:             if ( DP_Print ) { printf("."); fflush(stdout); }
                   2182:             FREENDP(l);
                   2183:         }
                   2184:     }
1.245     noro     2185:   conv_ilist(nd_demand,0,g,indp);
1.157     noro     2186:     if ( !checkonly && DP_Print ) { printf("nd_gb done.\n"); fflush(stdout); }
                   2187:     return g;
1.1       noro     2188: }
                   2189:
1.209     noro     2190: /* splist = [[i1,i2],...] */
                   2191:
                   2192: int check_splist(int m,NODE splist)
                   2193: {
1.245     noro     2194:   NODE t,p;
                   2195:   ND_pairs d,r,l;
                   2196:   int stat;
                   2197:   ND h,nf;
1.209     noro     2198:
1.245     noro     2199:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   2200:     p = BDY((LIST)BDY(t));
1.209     noro     2201:         NEXTND_pairs(d,r);
                   2202:         r->i1 = QTOS((Q)ARG0(p)); r->i2 = QTOS((Q)ARG1(p));
                   2203:         ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.245     noro     2204:     SG(r) = TD(LCM(r)); /* XXX */
                   2205:   }
                   2206:   if ( d ) NEXT(r) = 0;
1.209     noro     2207:
                   2208:     while ( d ) {
                   2209: again:
                   2210:         l = nd_minp(d,&d);
                   2211:         stat = nd_sp(m,0,l,&h);
                   2212:         if ( !stat ) {
                   2213:             NEXT(l) = d; d = l;
                   2214:             d = nd_reconstruct(0,d);
                   2215:             goto again;
                   2216:         }
                   2217:         stat = nd_nf(m,0,h,nd_ps,!Top,0,&nf);
                   2218:         if ( !stat ) {
                   2219:             NEXT(l) = d; d = l;
                   2220:             d = nd_reconstruct(0,d);
                   2221:             goto again;
                   2222:         } else if ( nf ) return 0;
1.245     noro     2223:     if ( DP_Print) { printf("."); fflush(stdout); }
1.209     noro     2224:     }
1.245     noro     2225:   if ( DP_Print) { printf("done.\n"); fflush(stdout); }
                   2226:   return 1;
1.209     noro     2227: }
                   2228:
1.214     noro     2229: int check_splist_f4(int m,NODE splist)
                   2230: {
1.245     noro     2231:   UINT *s0vect;
1.214     noro     2232:     PGeoBucket bucket;
1.245     noro     2233:   NODE p,rp0,t;
                   2234:   ND_pairs d,r,l,ll;
                   2235:   int col,stat;
1.214     noro     2236:
1.245     noro     2237:   for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   2238:     p = BDY((LIST)BDY(t));
1.214     noro     2239:         NEXTND_pairs(d,r);
                   2240:         r->i1 = QTOS((Q)ARG0(p)); r->i2 = QTOS((Q)ARG1(p));
                   2241:         ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
1.245     noro     2242:     SG(r) = TD(LCM(r)); /* XXX */
                   2243:   }
                   2244:   if ( d ) NEXT(r) = 0;
1.214     noro     2245:
                   2246:     while ( d ) {
                   2247:         l = nd_minsugarp(d,&d);
                   2248:         bucket = create_pbucket();
                   2249:         stat = nd_sp_f4(m,0,l,bucket);
                   2250:         if ( !stat ) {
                   2251:             for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   2252:             NEXT(ll) = d; d = l;
                   2253:             d = nd_reconstruct(0,d);
                   2254:             continue;
                   2255:         }
                   2256:         if ( bucket->m < 0 ) continue;
                   2257:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   2258:         if ( !col ) {
                   2259:             for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   2260:             NEXT(ll) = d; d = l;
                   2261:             d = nd_reconstruct(0,d);
                   2262:             continue;
                   2263:         }
                   2264:         if ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
                   2265:     }
                   2266:     return 1;
                   2267: }
                   2268:
1.122     noro     2269: int do_diagonalize_trace(int sugar,int m)
1.91      noro     2270: {
1.157     noro     2271:     int i,nh,stat;
                   2272:     NODE r,g,t;
                   2273:     ND h,nf,nfq,s,head;
                   2274:     NDV nfv,nfqv;
                   2275:     Q q,den,num;
1.167     noro     2276:     union oNDC hc;
                   2277:     NODE node;
                   2278:     LIST l;
                   2279:     Q iq;
                   2280:     P cont,cont1;
1.157     noro     2281:
                   2282:     for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
1.172     noro     2283:         if ( nd_gentrace ) {
1.167     noro     2284:             /* Trace = [1,index,1,1] */
                   2285:             STOQ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2286:             MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2287:         }
1.157     noro     2288:         /* for nd_ps */
                   2289:         s = ndvtond(m,nd_ps[i]);
                   2290:         s = nd_separate_head(s,&head);
                   2291:         stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
                   2292:         if ( !stat ) return 0;
                   2293:         nf = nd_add(m,head,nf);
                   2294:         ndv_free(nd_ps[i]);
                   2295:         nd_ps[i] = ndtondv(m,nf);
                   2296:         nd_free(nf);
                   2297:
                   2298:         /* for nd_ps_trace */
                   2299:         if ( nd_demand )
                   2300:             nfv = ndv_load(i);
                   2301:         else
                   2302:             nfv = nd_ps_trace[i];
                   2303:         s = ndvtond(0,nfv);
                   2304:         s = nd_separate_head(s,&head);
                   2305:         stat = nd_nf(0,head,s,nd_ps_trace,1,0,&nf);
                   2306:         if ( !stat ) return 0;
                   2307:         ndv_free(nfv);
1.167     noro     2308:         hc = HCU(nf); nd_removecont(0,nf);
1.245     noro     2309:     cont = ndc_div(0,hc,HCU(nf));
1.172     noro     2310:         if ( nd_gentrace ) finalize_tracelist(i,cont);
1.157     noro     2311:         nfv = ndtondv(0,nf);
                   2312:         nd_free(nf);
                   2313:         nd_bound[i] = ndv_compute_bound(nfv);
                   2314:         register_hcf(nfv);
                   2315:         if ( nd_demand ) {
                   2316:             ndv_save(nfv,i);
                   2317:             ndv_free(nfv);
                   2318:         } else
                   2319:             nd_ps_trace[i] = nfv;
                   2320:     }
                   2321:     return 1;
1.91      noro     2322: }
                   2323:
1.118     noro     2324: static struct oEGT eg_invdalg;
                   2325: struct oEGT eg_le;
                   2326:
1.147     noro     2327: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
                   2328: {
1.157     noro     2329:     NODE tn;
                   2330:     P p1;
1.147     noro     2331:
1.157     noro     2332:     for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
                   2333:         substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
                   2334:     }
                   2335:     *r = p;
1.147     noro     2336: }
                   2337:
1.167     noro     2338: NODE nd_gb_trace(int m,int ishomo,int **indp)
1.20      noro     2339: {
1.157     noro     2340:     int i,nh,sugar,stat;
                   2341:     NODE r,g,t;
                   2342:     ND_pairs d;
                   2343:     ND_pairs l;
                   2344:     ND h,nf,nfq,s,head;
                   2345:     NDV nfv,nfqv;
                   2346:     Q q,den,num;
                   2347:     P hc;
1.167     noro     2348:     union oNDC dn,hnfq;
1.157     noro     2349:     struct oEGT eg_monic,egm0,egm1;
                   2350:     int diag_count = 0;
1.167     noro     2351:     P cont;
                   2352:     LIST list;
1.157     noro     2353:
                   2354:     init_eg(&eg_monic);
                   2355:     init_eg(&eg_invdalg);
                   2356:     init_eg(&eg_le);
                   2357:     g = 0; d = 0;
                   2358:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     2359:         d = update_pairs(d,g,i,0);
1.157     noro     2360:         g = update_base(g,i);
                   2361:     }
                   2362:     sugar = 0;
                   2363:     while ( d ) {
1.20      noro     2364: again:
1.157     noro     2365:         l = nd_minp(d,&d);
1.228     noro     2366:         if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
1.157     noro     2367:         if ( SG(l) != sugar ) {
1.130     noro     2368: #if 1
1.157     noro     2369:             if ( ishomo ) {
                   2370:                 if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2371:                 stat = do_diagonalize_trace(sugar,m);
                   2372:                 if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2373:                 diag_count = 0;
                   2374:                 if ( !stat ) {
                   2375:                     NEXT(l) = d; d = l;
                   2376:                     d = nd_reconstruct(1,d);
                   2377:                     goto again;
                   2378:                 }
                   2379:             }
1.130     noro     2380: #endif
1.157     noro     2381:             sugar = SG(l);
                   2382:             if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2383:         }
                   2384:         stat = nd_sp(m,0,l,&h);
                   2385:         if ( !stat ) {
                   2386:             NEXT(l) = d; d = l;
                   2387:             d = nd_reconstruct(1,d);
                   2388:             goto again;
                   2389:         }
1.41      noro     2390: #if USE_GEOBUCKET
1.157     noro     2391:         stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.41      noro     2392: #else
1.157     noro     2393:         stat = nd_nf(m,0,h,nd_ps,!Top,0,&nf);
1.41      noro     2394: #endif
1.157     noro     2395:         if ( !stat ) {
                   2396:             NEXT(l) = d; d = l;
                   2397:             d = nd_reconstruct(1,d);
                   2398:             goto again;
                   2399:         } else if ( nf ) {
                   2400:             if ( nd_demand ) {
                   2401:                 nfqv = ndv_load(nd_psn);
                   2402:                 nfq = ndvtond(0,nfqv);
                   2403:             } else
                   2404:                 nfq = 0;
                   2405:             if ( !nfq ) {
                   2406:                 if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!Top,0,&nfq) ) {
                   2407:                     NEXT(l) = d; d = l;
                   2408:                     d = nd_reconstruct(1,d);
                   2409:                     goto again;
                   2410:                 }
                   2411:             }
                   2412:             if ( nfq ) {
                   2413:                 /* m|HC(nfq) => failure */
                   2414:                 if ( nd_vc ) {
                   2415:                     nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Q)hc;
                   2416:                 } else
                   2417:                     q = HCQ(nfq);
                   2418:                 if ( !rem(NM(q),m) ) return 0;
                   2419:
                   2420:                 if ( DP_Print ) { printf("+"); fflush(stdout); }
1.167     noro     2421:                 hnfq = HCU(nfq);
1.157     noro     2422:                 if ( nd_nalg ) {
                   2423:                     /* m|DN(HC(nf)^(-1)) => failure */
                   2424:                     get_eg(&egm0);
                   2425:                     if ( !nd_monic(m,&nfq) ) return 0;
                   2426:                     get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
                   2427:                     nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   2428:                     nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
                   2429:                 } else {
                   2430:                     nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   2431:                     nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
                   2432:                 }
1.172     noro     2433:                 if ( nd_gentrace ) {
1.245     noro     2434:            cont = ndc_div(0,hnfq,HCU(nfqv));
                   2435:            if ( !UNIQ(cont) ) {
1.196     noro     2436:                        t = mknode(4,NULLP,NULLP,NULLP,cont);
1.167     noro     2437:                        MKLIST(list,t); MKNODE(t,list,nd_tracelist);
1.245     noro     2438:              nd_tracelist = t;
                   2439:            }
1.167     noro     2440:                 }
1.215     noro     2441:                 nh = ndv_newps(0,nfv,nfqv,0);
1.157     noro     2442:                 if ( ishomo && ++diag_count == diag_period ) {
                   2443:                     diag_count = 0;
                   2444:                     if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2445:                     stat = do_diagonalize_trace(sugar,m);
                   2446:                     if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2447:                     if ( !stat ) {
                   2448:                         NEXT(l) = d; d = l;
                   2449:                         d = nd_reconstruct(1,d);
                   2450:                         goto again;
                   2451:                     }
                   2452:                 }
1.168     noro     2453:                 d = update_pairs(d,g,nh,0);
1.157     noro     2454:                 g = update_base(g,nh);
                   2455:             } else {
                   2456:                 if ( DP_Print ) { printf("*"); fflush(stdout); }
                   2457:             }
                   2458:         } else {
                   2459:             if ( DP_Print ) { printf("."); fflush(stdout); }
                   2460:         }
                   2461:         FREENDP(l);
                   2462:     }
                   2463:     if ( nd_nalg ) {
1.227     noro     2464:         if ( DP_Print ) {
                   2465:           print_eg("monic",&eg_monic);
                   2466:           print_eg("invdalg",&eg_invdalg);
                   2467:           print_eg("le",&eg_le);
                   2468:         }
1.157     noro     2469:     }
1.245     noro     2470:   conv_ilist(nd_demand,1,g,indp);
1.157     noro     2471:     if ( DP_Print ) { printf("nd_gb_trace done.\n"); fflush(stdout); }
                   2472:     return g;
1.20      noro     2473: }
                   2474:
1.23      noro     2475: int ndv_compare(NDV *p1,NDV *p2)
                   2476: {
1.157     noro     2477:     return DL_COMPARE(HDL(*p1),HDL(*p2));
1.23      noro     2478: }
                   2479:
                   2480: int ndv_compare_rev(NDV *p1,NDV *p2)
                   2481: {
1.157     noro     2482:     return -DL_COMPARE(HDL(*p1),HDL(*p2));
1.23      noro     2483: }
                   2484:
1.167     noro     2485: int ndvi_compare(NDVI p1,NDVI p2)
                   2486: {
                   2487:     return DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   2488: }
                   2489:
                   2490: int ndvi_compare_rev(NDVI p1,NDVI p2)
                   2491: {
                   2492:     return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   2493: }
                   2494:
1.61      noro     2495: NODE ndv_reduceall(int m,NODE f)
1.23      noro     2496: {
1.167     noro     2497:     int i,j,n,stat;
1.157     noro     2498:     ND nf,g,head;
                   2499:     NODE t,a0,a;
                   2500:     union oNDC dn;
                   2501:     Q q,num,den;
1.167     noro     2502:     NODE node;
1.245     noro     2503:   LIST l;
                   2504:   Q iq,jq;
1.167     noro     2505:     int *perm;
                   2506:     union oNDC hc;
                   2507:     P cont,cont1;
1.23      noro     2508:
1.173     noro     2509:     if ( nd_nora ) return f;
1.157     noro     2510:     n = length(f);
                   2511:     ndv_setup(m,0,f,0,1);
1.245     noro     2512:   perm = (int *)MALLOC(n*sizeof(int));
                   2513:   if ( nd_gentrace ) {
                   2514:       for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
                   2515:         perm[i] = QTOS((Q)ARG1(BDY((LIST)BDY(t))));
                   2516:   }
1.157     noro     2517:     for ( i = 0; i < n; ) {
1.172     noro     2518:         if ( nd_gentrace ) {
1.167     noro     2519:             /* Trace = [1,index,1,1] */
                   2520:             STOQ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2521:             MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2522:         }
1.157     noro     2523:         g = ndvtond(m,nd_ps[i]);
                   2524:         g = nd_separate_head(g,&head);
                   2525:         stat = nd_nf(m,head,g,nd_ps,1,0,&nf);
                   2526:         if ( !stat )
                   2527:             nd_reconstruct(0,0);
                   2528:         else {
                   2529:             if ( DP_Print ) { printf("."); fflush(stdout); }
                   2530:             ndv_free(nd_ps[i]);
1.167     noro     2531:             hc = HCU(nf); nd_removecont(m,nf);
1.172     noro     2532:             if ( nd_gentrace ) {
1.245     noro     2533:         for ( t = nd_tracelist; t; t = NEXT(t) ) {
1.167     noro     2534:                     jq = ARG1(BDY((LIST)BDY(t))); j = QTOS(jq);
                   2535:                     STOQ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
                   2536:                 }
                   2537:                 cont = ndc_div(m,hc,HCU(nf));
                   2538:                 finalize_tracelist(perm[i],cont);
                   2539:             }
1.157     noro     2540:             nd_ps[i] = ndtondv(m,nf); nd_free(nf);
                   2541:             nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   2542:             i++;
                   2543:         }
                   2544:     }
                   2545:     if ( DP_Print ) { printf("\n"); }
                   2546:     for ( a0 = 0, i = 0; i < n; i++ ) {
                   2547:         NEXTNODE(a0,a);
1.245     noro     2548:     if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
                   2549:     else {
                   2550:       for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
                   2551:       BDY(a) = (pointer)nd_ps[j];
                   2552:     }
1.157     noro     2553:     }
                   2554:     NEXT(a) = 0;
                   2555:     return a0;
1.23      noro     2556: }
                   2557:
1.168     noro     2558: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
1.1       noro     2559: {
1.157     noro     2560:     ND_pairs d1,nd,cur,head,prev,remove;
1.1       noro     2561:
1.157     noro     2562:     if ( !g ) return d;
1.245     noro     2563:   /* for testing */
                   2564:   if ( gensyz && nd_gensyz == 2 ) {
                   2565:       d1 = nd_newpairs(g,t);
                   2566:       if ( !d )
                   2567:           return d1;
                   2568:       else {
                   2569:           nd = d;
                   2570:           while ( NEXT(nd) ) nd = NEXT(nd);
                   2571:           NEXT(nd) = d1;
                   2572:           return d;
                   2573:       }
                   2574:   }
1.157     noro     2575:     d = crit_B(d,t);
                   2576:     d1 = nd_newpairs(g,t);
                   2577:     d1 = crit_M(d1);
                   2578:     d1 = crit_F(d1);
1.168     noro     2579:     if ( gensyz || do_weyl )
1.157     noro     2580:         head = d1;
                   2581:     else {
                   2582:         prev = 0; cur = head = d1;
                   2583:         while ( cur ) {
                   2584:             if ( crit_2( cur->i1,cur->i2 ) ) {
                   2585:                 remove = cur;
                   2586:                 if ( !prev ) head = cur = NEXT(cur);
                   2587:                 else cur = NEXT(prev) = NEXT(cur);
                   2588:                 FREENDP(remove);
                   2589:             } else {
                   2590:                 prev = cur; cur = NEXT(cur);
                   2591:             }
                   2592:         }
                   2593:     }
                   2594:     if ( !d )
                   2595:         return head;
                   2596:     else {
                   2597:         nd = d;
                   2598:         while ( NEXT(nd) ) nd = NEXT(nd);
                   2599:         NEXT(nd) = head;
                   2600:         return d;
                   2601:     }
1.1       noro     2602: }
                   2603:
1.157     noro     2604:
1.1       noro     2605: ND_pairs nd_newpairs( NODE g, int t )
                   2606: {
1.157     noro     2607:     NODE h;
                   2608:     UINT *dl;
1.187     noro     2609:     int ts,s,i,t0,min,max;
1.157     noro     2610:     ND_pairs r,r0;
                   2611:
                   2612:     dl = DL(nd_psh[t]);
                   2613:     ts = SG(nd_psh[t]) - TD(dl);
1.245     noro     2614:   if ( nd_module && nd_intersect && (MPOS(dl) > 1) ) return 0;
1.157     noro     2615:     for ( r0 = 0, h = g; h; h = NEXT(h) ) {
1.159     noro     2616:         if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
1.157     noro     2617:                 continue;
1.245     noro     2618:     if ( nd_gbblock ) {
                   2619:       t0 = (long)BDY(h);
                   2620:       for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
                   2621:         min = nd_gbblock[i]; max = nd_gbblock[i+1];
                   2622:         if ( t0 >= min && t0 <= max && t >= min && t <= max )
                   2623:           break;
                   2624:       }
                   2625:       if ( nd_gbblock[i] >= 0 )
                   2626:         continue;
                   2627:     }
1.157     noro     2628:         NEXTND_pairs(r0,r);
1.159     noro     2629:         r->i1 = (long)BDY(h);
1.157     noro     2630:         r->i2 = t;
                   2631:         ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
                   2632:         s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   2633:         SG(r) = MAX(s,ts) + TD(LCM(r));
1.231     noro     2634:         /* experimental */
                   2635:         if ( nd_sugarweight )
                   2636:             r->sugar2 = ndl_weight2(r->lcm);
1.157     noro     2637:     }
                   2638:     if ( r0 ) NEXT(r) = 0;
                   2639:     return r0;
1.1       noro     2640: }
                   2641:
1.231     noro     2642: /* ipair = [i1,i2],[i1,i2],... */
                   2643: ND_pairs nd_ipairtospair(NODE ipair)
                   2644: {
                   2645:   int s1,s2;
                   2646:   NODE tn,t;
                   2647:   ND_pairs r,r0;
                   2648:
                   2649:   for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
                   2650:     NEXTND_pairs(r0,r);
                   2651:     tn = BDY((LIST)BDY(t));
                   2652:     r->i1 = QTOS((Q)ARG0(tn));
                   2653:     r->i2 = QTOS((Q)ARG1(tn));
                   2654:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
                   2655:     s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   2656:     s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
                   2657:     SG(r) = MAX(s1,s2) + TD(LCM(r));
                   2658:     /* experimental */
                   2659:     if ( nd_sugarweight )
                   2660:         r->sugar2 = ndl_weight2(r->lcm);
                   2661:   }
                   2662:   if ( r0 ) NEXT(r) = 0;
                   2663:   return r0;
                   2664: }
                   2665:
1.157     noro     2666: /* kokokara */
                   2667:
1.1       noro     2668: ND_pairs crit_B( ND_pairs d, int s )
                   2669: {
1.157     noro     2670:     ND_pairs cur,head,prev,remove;
                   2671:     UINT *t,*tl,*lcm;
                   2672:     int td,tdl;
                   2673:
                   2674:     if ( !d ) return 0;
                   2675:     t = DL(nd_psh[s]);
                   2676:     prev = 0;
                   2677:     head = cur = d;
1.235     noro     2678:     lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
1.157     noro     2679:     while ( cur ) {
                   2680:         tl = cur->lcm;
1.163     noro     2681:         if ( ndl_reducible(tl,t) ) {
                   2682:             ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
1.167     noro     2683:             if ( !ndl_equal(lcm,tl) ) {
                   2684:                 ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
                   2685:                 if (!ndl_equal(lcm,tl)) {
                   2686:                     remove = cur;
                   2687:                     if ( !prev ) {
                   2688:                         head = cur = NEXT(cur);
                   2689:                     } else {
                   2690:                         cur = NEXT(prev) = NEXT(cur);
                   2691:                     }
                   2692:                     FREENDP(remove);
                   2693:                 } else {
                   2694:                     prev = cur; cur = NEXT(cur);
                   2695:                 }
                   2696:             } else {
                   2697:                     prev = cur; cur = NEXT(cur);
                   2698:             }
1.157     noro     2699:         } else {
                   2700:             prev = cur; cur = NEXT(cur);
                   2701:         }
                   2702:     }
                   2703:     return head;
1.1       noro     2704: }
                   2705:
                   2706: ND_pairs crit_M( ND_pairs d1 )
                   2707: {
1.157     noro     2708:     ND_pairs e,d2,d3,dd,p;
                   2709:     UINT *id,*jd;
1.1       noro     2710:
1.157     noro     2711:     if ( !d1 ) return d1;
                   2712:     for ( dd = 0, e = d1; e; e = d3 ) {
                   2713:         if ( !(d2 = NEXT(e)) ) {
                   2714:             NEXT(e) = dd;
                   2715:             return e;
                   2716:         }
                   2717:         id = LCM(e);
                   2718:         for ( d3 = 0; d2; d2 = p ) {
                   2719:             p = NEXT(d2);
                   2720:             jd = LCM(d2);
                   2721:             if ( ndl_equal(jd,id) )
                   2722:                 ;
                   2723:             else if ( TD(jd) > TD(id) )
                   2724:                 if ( ndl_reducible(jd,id) ) continue;
                   2725:                 else ;
                   2726:             else if ( ndl_reducible(id,jd) ) goto delit;
                   2727:             NEXT(d2) = d3;
                   2728:             d3 = d2;
                   2729:         }
                   2730:         NEXT(e) = dd;
                   2731:         dd = e;
                   2732:         continue;
                   2733:         /**/
                   2734:     delit:    NEXT(d2) = d3;
                   2735:         d3 = d2;
                   2736:         for ( ; p; p = d2 ) {
                   2737:             d2 = NEXT(p);
                   2738:             NEXT(p) = d3;
                   2739:             d3 = p;
                   2740:         }
                   2741:         FREENDP(e);
                   2742:     }
                   2743:     return dd;
1.1       noro     2744: }
                   2745:
                   2746: ND_pairs crit_F( ND_pairs d1 )
                   2747: {
1.157     noro     2748:     ND_pairs rest, head,remove;
                   2749:     ND_pairs last, p, r, w;
                   2750:     int s;
                   2751:
                   2752:     if ( !d1 ) return d1;
                   2753:     for ( head = last = 0, p = d1; NEXT(p); ) {
                   2754:         r = w = equivalent_pairs(p,&rest);
                   2755:         s = SG(r);
                   2756:         w = NEXT(w);
                   2757:         while ( w ) {
                   2758:             if ( crit_2(w->i1,w->i2) ) {
                   2759:                 r = w;
                   2760:                 w = NEXT(w);
                   2761:                 while ( w ) {
                   2762:                     remove = w;
                   2763:                     w = NEXT(w);
                   2764:                     FREENDP(remove);
                   2765:                 }
                   2766:                 break;
                   2767:             } else if ( SG(w) < s ) {
                   2768:                 FREENDP(r);
                   2769:                 r = w;
                   2770:                 s = SG(r);
                   2771:                 w = NEXT(w);
                   2772:             } else {
                   2773:                 remove = w;
                   2774:                 w = NEXT(w);
                   2775:                 FREENDP(remove);
                   2776:             }
                   2777:         }
                   2778:         if ( last ) NEXT(last) = r;
                   2779:         else head = r;
                   2780:         NEXT(last = r) = 0;
                   2781:         p = rest;
                   2782:         if ( !p ) return head;
                   2783:     }
                   2784:     if ( !last ) return p;
                   2785:     NEXT(last) = p;
                   2786:     return head;
1.1       noro     2787: }
                   2788:
                   2789: int crit_2( int dp1, int dp2 )
                   2790: {
1.157     noro     2791:     return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
1.1       noro     2792: }
                   2793:
1.40      noro     2794: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
1.1       noro     2795: {
1.157     noro     2796:     ND_pairs w,p,r,s;
                   2797:     UINT *d;
1.1       noro     2798:
1.157     noro     2799:     w = d1;
                   2800:     d = LCM(w);
                   2801:     s = NEXT(w);
                   2802:     NEXT(w) = 0;
                   2803:     for ( r = 0; s; s = p ) {
                   2804:         p = NEXT(s);
                   2805:         if ( ndl_equal(d,LCM(s)) ) {
                   2806:             NEXT(s) = w; w = s;
                   2807:         } else {
                   2808:             NEXT(s) = r; r = s;
                   2809:         }
                   2810:     }
                   2811:     *prest = r;
                   2812:     return w;
1.1       noro     2813: }
                   2814:
                   2815: NODE update_base(NODE nd,int ndp)
                   2816: {
1.157     noro     2817:     UINT *dl, *dln;
                   2818:     NODE last, p, head;
1.1       noro     2819:
1.157     noro     2820:     dl = DL(nd_psh[ndp]);
                   2821:     for ( head = last = 0, p = nd; p; ) {
1.159     noro     2822:         dln = DL(nd_psh[(long)BDY(p)]);
1.157     noro     2823:         if ( ndl_reducible( dln, dl ) ) {
                   2824:             p = NEXT(p);
                   2825:             if ( last ) NEXT(last) = p;
                   2826:         } else {
                   2827:             if ( !last ) head = p;
                   2828:             p = NEXT(last = p);
                   2829:         }
                   2830:     }
                   2831:     head = append_one(head,ndp);
                   2832:     return head;
1.1       noro     2833: }
                   2834:
                   2835: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   2836: {
1.157     noro     2837:     ND_pairs m,ml,p,l;
                   2838:     UINT *lcm;
                   2839:     int s,td,len,tlen,c,c1;
                   2840:
                   2841:     if ( !(p = NEXT(m = d)) ) {
                   2842:         *prest = p;
                   2843:         NEXT(m) = 0;
                   2844:         return m;
                   2845:     }
                   2846:     if ( !NoSugar ) {
1.231     noro     2847:         if ( nd_sugarweight ) {
                   2848:             s = m->sugar2;
                   2849:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2850:                 if ( (p->sugar2 < s)
                   2851:                     || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   2852:                     ml = l; m = p; s = m->sugar2;
                   2853:                 }
                   2854:         } else {
                   2855:             s = SG(m);
                   2856:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2857:                 if ( (SG(p) < s)
                   2858:                     || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   2859:                     ml = l; m = p; s = SG(m);
                   2860:                 }
                   2861:         }
1.157     noro     2862:     } else {
                   2863:         for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2864:             if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
                   2865:                 ml = l; m = p; s = SG(m);
                   2866:             }
                   2867:     }
                   2868:     if ( !ml ) *prest = NEXT(m);
                   2869:     else {
                   2870:         NEXT(ml) = NEXT(m);
                   2871:         *prest = d;
                   2872:     }
                   2873:     NEXT(m) = 0;
                   2874:     return m;
1.1       noro     2875: }
                   2876:
1.63      noro     2877: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   2878: {
1.157     noro     2879:     int msugar,i;
                   2880:     ND_pairs t,dm0,dm,dr0,dr;
1.63      noro     2881:
1.231     noro     2882:     if ( nd_sugarweight ) {
                   2883:         for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
                   2884:             if ( t->sugar2 < msugar ) msugar = t->sugar2;
                   2885:         dm0 = 0; dr0 = 0;
                   2886:         for ( i = 0, t = d; t; t = NEXT(t) )
                   2887:             if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
                   2888:                 if ( dm0 ) NEXT(dm) = t;
                   2889:                 else dm0 = t;
                   2890:                 dm = t;
                   2891:                 i++;
                   2892:             } else {
                   2893:                 if ( dr0 ) NEXT(dr) = t;
                   2894:                 else dr0 = t;
                   2895:                 dr = t;
                   2896:             }
                   2897:     } else {
                   2898:         for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   2899:             if ( SG(t) < msugar ) msugar = SG(t);
                   2900:         dm0 = 0; dr0 = 0;
                   2901:         for ( i = 0, t = d; t; t = NEXT(t) )
                   2902:             if ( i < nd_f4_nsp && SG(t) == msugar ) {
                   2903:                 if ( dm0 ) NEXT(dm) = t;
                   2904:                 else dm0 = t;
                   2905:                 dm = t;
                   2906:                 i++;
                   2907:             } else {
                   2908:                 if ( dr0 ) NEXT(dr) = t;
                   2909:                 else dr0 = t;
                   2910:                 dr = t;
                   2911:             }
                   2912:     }
1.157     noro     2913:     NEXT(dm) = 0;
                   2914:     if ( dr0 ) NEXT(dr) = 0;
                   2915:     *prest = dr0;
                   2916:     return dm0;
1.63      noro     2917: }
                   2918:
1.239     noro     2919: int nd_tdeg(NDV c)
                   2920: {
                   2921:   int wmax = 0;
                   2922:   int i,len;
                   2923:   NMV a;
                   2924:
                   2925:   len = LEN(c);
                   2926:   for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   2927:     wmax = MAX(TD(DL(a)),wmax);
                   2928:   return wmax;
                   2929: }
                   2930:
1.215     noro     2931: int ndv_newps(int m,NDV a,NDV aq,int f4)
1.1       noro     2932: {
1.157     noro     2933:     int len;
                   2934:     RHist r;
                   2935:     NDV b;
1.167     noro     2936:     NODE tn;
                   2937:     LIST l;
                   2938:     Q iq;
1.157     noro     2939:
                   2940:     if ( nd_psn == nd_pslen ) {
                   2941:         nd_pslen *= 2;
                   2942:         nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
1.215     noro     2943:         nd_ps_gz = (NDV *)REALLOC((char *)nd_ps_gz,nd_pslen*sizeof(NDV));
1.157     noro     2944:         nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
                   2945:         nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
                   2946:         nd_bound = (UINT **)
                   2947:             REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
1.215     noro     2948:         nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
                   2949:         nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
1.157     noro     2950:     }
                   2951:     NEWRHist(r); nd_psh[nd_psn] = r;
                   2952:     nd_ps[nd_psn] = a;
                   2953:     if ( aq ) {
                   2954:         nd_ps_trace[nd_psn] = aq;
1.234     noro     2955:         if ( !m ) {
1.245     noro     2956:       if ( !nd_vc ) nd_ps_gz[nd_psn] = ndvtondvgz(aq);
1.234     noro     2957:           register_hcf(aq);
                   2958:         } else if ( m == -2 ) {
                   2959:           /* do nothing */
                   2960:         } else
                   2961:           error("ndv_newps : invalud modulus");
1.157     noro     2962:         nd_bound[nd_psn] = ndv_compute_bound(aq);
1.239     noro     2963: #if 1
                   2964:         SG(r) = SG(aq);
                   2965: #else
                   2966:         SG(r) = nd_tdeg(aq);
                   2967: #endif
                   2968:         ndl_copy(HDL(aq),DL(r));
1.157     noro     2969:     } else {
                   2970:         if ( !m ) register_hcf(a);
                   2971:         nd_bound[nd_psn] = ndv_compute_bound(a);
1.239     noro     2972: #if 1
                   2973:         SG(r) = SG(a);
                   2974: #else
                   2975:         SG(r) = nd_tdeg(a);
                   2976: #endif
                   2977:         ndl_copy(HDL(a),DL(r));
1.245     noro     2978:     if ( !m && !nd_vc ) nd_ps_gz[nd_psn] = ndvtondvgz(a);
1.157     noro     2979:     }
                   2980:     if ( nd_demand ) {
                   2981:         if ( aq ) {
                   2982:             ndv_save(nd_ps_trace[nd_psn],nd_psn);
1.235     noro     2983:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
1.215     noro     2984:             nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
1.157     noro     2985:             nd_ps_trace[nd_psn] = 0;
                   2986:         } else {
                   2987:             ndv_save(nd_ps[nd_psn],nd_psn);
1.215     noro     2988:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
1.157     noro     2989:             nd_ps[nd_psn] = 0;
                   2990:         }
                   2991:     }
1.172     noro     2992:     if ( nd_gentrace ) {
1.167     noro     2993:         /* reverse the tracelist and append it to alltracelist */
                   2994:         nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
                   2995:         STOQ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
                   2996:         MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
                   2997:     }
1.157     noro     2998:     return nd_psn++;
1.1       noro     2999: }
                   3000:
1.167     noro     3001: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
1.177     noro     3002: /* return 1 if success, 0 if failure (HC(a mod p)) */
1.167     noro     3003:
1.177     noro     3004: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont)
1.1       noro     3005: {
1.157     noro     3006:     int i,j,td,len,max;
1.167     noro     3007:     NODE s,s0,f0,tn;
1.157     noro     3008:     UINT *d;
                   3009:     RHist r;
1.167     noro     3010:     NDVI w;
1.157     noro     3011:     NDV a,am;
1.167     noro     3012:     union oNDC hc;
                   3013:     NODE node;
                   3014:     P hcp;
                   3015:     Q iq,jq,hcq;
                   3016:     LIST l;
1.157     noro     3017:
                   3018:     nd_found = 0; nd_notfirst = 0; nd_create = 0;
1.167     noro     3019:     /* initialize the tracelist */
                   3020:     nd_tracelist = 0;
1.157     noro     3021:
                   3022:     for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
1.235     noro     3023:     w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
1.167     noro     3024:     for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
                   3025:         if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
1.157     noro     3026:     if ( !dont_sort ) {
                   3027:         /* XXX heuristic */
                   3028:         if ( !nd_ord->id && (nd_ord->ord.simple<2) )
1.167     noro     3029:             qsort(w,nd_psn,sizeof(struct oNDVI),
                   3030:                 (int (*)(const void *,const void *))ndvi_compare_rev);
1.157     noro     3031:         else
1.167     noro     3032:             qsort(w,nd_psn,sizeof(struct oNDVI),
                   3033:                 (int (*)(const void *,const void *))ndvi_compare);
1.157     noro     3034:     }
                   3035:     nd_pslen = 2*nd_psn;
                   3036:     nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.215     noro     3037:     nd_ps_gz = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.157     noro     3038:     nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.215     noro     3039:     nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3040:     nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.157     noro     3041:     nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
                   3042:     nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
                   3043:     nd_hcf = 0;
                   3044:
                   3045:     if ( trace && nd_vc )
                   3046:         makesubst(nd_vc,&nd_subst);
                   3047:     else
                   3048:         nd_subst = 0;
                   3049:
                   3050:     if ( !nd_red )
                   3051:         nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   3052:     for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
                   3053:     for ( i = 0; i < nd_psn; i++ ) {
1.167     noro     3054:         hc = HCU(w[i].p);
1.157     noro     3055:         if ( trace ) {
1.234     noro     3056:             if ( mod == -2 ) {
                   3057:               /* over a large finite field */
                   3058:               /* trace = small modulus */
                   3059:               a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
                   3060:               ndv_mod(-2,a);
                   3061:               if ( !dont_removecont) ndv_removecont(-2,a);
                   3062:               am = nd_ps[i] = ndv_dup(trace,w[i].p);
                   3063:               ndv_mod(trace,am);
1.245     noro     3064:             if ( DL_COMPARE(HDL(am),HDL(a)) )
                   3065:               return 0;
1.234     noro     3066:               ndv_removecont(trace,am);
                   3067:             } else {
                   3068:               a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
1.245     noro     3069:         if ( !nd_vc ) nd_ps_gz[i] = ndvtondvgz(a);
1.234     noro     3070:               if ( !dont_removecont) ndv_removecont(0,a);
                   3071:               register_hcf(a);
                   3072:               am = nd_ps[i] = ndv_dup(mod,a);
                   3073:               ndv_mod(mod,am);
1.245     noro     3074:             if ( DL_COMPARE(HDL(am),HDL(a)) )
                   3075:               return 0;
1.234     noro     3076:               ndv_removecont(mod,am);
                   3077:             }
1.157     noro     3078:         } else {
1.167     noro     3079:             a = nd_ps[i] = ndv_dup(mod,w[i].p);
1.245     noro     3080:       if ( !mod && !nd_vc ) nd_ps_gz[i] = ndvtondvgz(a);
1.157     noro     3081:             if ( mod || !dont_removecont ) ndv_removecont(mod,a);
                   3082:             if ( !mod ) register_hcf(a);
                   3083:         }
1.172     noro     3084:         if ( nd_gentrace ) {
1.167     noro     3085:             STOQ(i,iq); STOQ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
1.245     noro     3086:       if ( !dont_removecont )
1.168     noro     3087:                 ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
1.167     noro     3088:             MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
                   3089:         }
1.157     noro     3090:         NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
                   3091:         nd_bound[i] = ndv_compute_bound(a);
                   3092:         nd_psh[i] = r;
                   3093:         if ( nd_demand ) {
                   3094:             if ( trace ) {
                   3095:                 ndv_save(nd_ps_trace[i],i);
1.235     noro     3096:                 nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
1.215     noro     3097:                 nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
1.157     noro     3098:                 nd_ps_trace[i] = 0;
                   3099:             } else {
                   3100:                 ndv_save(nd_ps[i],i);
1.215     noro     3101:                 nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
1.157     noro     3102:                 nd_ps[i] = 0;
                   3103:             }
                   3104:         }
                   3105:     }
1.172     noro     3106:     if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
1.177     noro     3107:     return 1;
1.20      noro     3108: }
                   3109:
1.119     noro     3110: struct order_spec *append_block(struct order_spec *spec,
                   3111:     int nv,int nalg,int ord);
                   3112:
1.121     noro     3113: extern VECT current_dl_weight_vector_obj;
                   3114: static VECT prev_weight_vector_obj;
                   3115:
1.120     noro     3116: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
1.157     noro     3117:     struct order_spec **ord1p,LIST *f1p,NODE *alistp)
1.120     noro     3118: {
1.157     noro     3119:     NODE alist,t,s,r0,r,arg;
                   3120:     VL tv;
                   3121:     P poly;
                   3122:     DP d;
                   3123:     Alg alpha,dp;
                   3124:     DAlg inv,da,hc;
                   3125:     MP m;
                   3126:     int i,nvar,nalg,n;
                   3127:     NumberField nf;
                   3128:     LIST f1,f2;
                   3129:     struct order_spec *current_spec;
                   3130:     VECT obj,obj0;
1.232     noro     3131:     VECT tmp;
1.157     noro     3132:
                   3133:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
                   3134:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
                   3135:
                   3136:     for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
                   3137:         NEXTNODE(alist,t); MKV(tv->v,poly);
                   3138:         MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
                   3139:         tv->v = tv->v->priv;
                   3140:     }
                   3141:     NEXT(t) = 0;
                   3142:
1.167     noro     3143:     /* simplification, making polynomials monic */
1.157     noro     3144:     setfield_dalg(alist);
1.232     noro     3145:     obj_algtodalg((Obj)f,(Obj *)&f1);
1.157     noro     3146:     for ( t = BDY(f); t; t = NEXT(t) ) {
                   3147:         initd(ord); ptod(vv,vv,(P)BDY(t),&d);
                   3148:         hc = (DAlg)BDY(d)->c;
                   3149:         if ( NID(hc) == N_DA ) {
                   3150:             invdalg(hc,&inv);
                   3151:             for ( m = BDY(d); m; m = NEXT(m) ) {
1.238     noro     3152:                 muldalg(inv,(DAlg)m->c,&da); m->c = (Obj)da;
1.157     noro     3153:             }
                   3154:         }
1.238     noro     3155:         initd(ord); dtop(vv,vv,d,(Obj *)&poly); BDY(f) = (pointer)poly;
1.157     noro     3156:     }
1.232     noro     3157:     obj_dalgtoalg((Obj)f1,(Obj *)&f);
1.157     noro     3158:
                   3159:     /* append alg vars to the var list */
                   3160:     for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
                   3161:     NEXT(tv) = av;
                   3162:
                   3163:     /* append a block to ord */
                   3164:     *ord1p = append_block(ord,nvar,nalg,2);
                   3165:
                   3166:     /* create generator list */
                   3167:     nf = get_numberfield();
                   3168:     for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
                   3169:         MKAlg(nf->defpoly[i],dp);
                   3170:         MKNODE(s,dp,t); t = s;
                   3171:     }
                   3172:     MKLIST(f1,t);
                   3173:     *alistp = alist;
1.232     noro     3174:     algobjtorat((Obj)f1,(Obj *)f1p);
1.157     noro     3175:
                   3176:     /* creating a new weight vector */
                   3177:     prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
                   3178:     n = nvar+nalg+1;
                   3179:     MKVECT(obj,n);
                   3180:     if ( obj0 && obj0->len == nvar )
                   3181:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
                   3182:     else
                   3183:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
                   3184:     for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
                   3185:     BDY(obj)[n-1] = (pointer)ONE;
                   3186:     arg = mknode(1,obj);
                   3187:     Pdp_set_weight(arg,&tmp);
1.121     noro     3188: }
                   3189:
                   3190: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
                   3191: {
1.157     noro     3192:     NODE s,t,u0,u;
                   3193:     P p;
                   3194:     VL tv;
1.232     noro     3195:     Obj obj;
                   3196:     VECT tmp;
1.157     noro     3197:     NODE arg;
                   3198:
                   3199:     u0 = 0;
                   3200:     for ( t = r; t; t = NEXT(t) ) {
                   3201:         p = (P)BDY(t);
                   3202:         for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
                   3203:             substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
                   3204:         }
                   3205:         if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
                   3206:             NEXTNODE(u0,u);
                   3207:             BDY(u) = (pointer)p;
                   3208:         }
                   3209:     }
                   3210:     arg = mknode(1,prev_weight_vector_obj);
                   3211:     Pdp_set_weight(arg,&tmp);
1.121     noro     3212:
1.157     noro     3213:     return u0;
1.120     noro     3214: }
                   3215:
1.199     noro     3216: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.1       noro     3217: {
1.157     noro     3218:     VL tv,fv,vv,vc,av;
                   3219:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   3220:     int e,max,nvar,i;
                   3221:     NDV b;
1.184     noro     3222:     int ishomo,nalg,mrank,trank,wmax,len;
1.245     noro     3223:   NMV a;
1.157     noro     3224:     Alg alpha,dp;
                   3225:     P p,zp;
                   3226:     Q dmy;
                   3227:     LIST f1,f2,zpl;
                   3228:     Obj obj;
                   3229:     NumberField nf;
                   3230:     struct order_spec *ord1;
1.208     noro     3231:     NODE tr,tl1,tl2,tl3,tl4,nzlist;
1.170     noro     3232:     LIST l1,l2,l3,l4,l5;
1.245     noro     3233:   int j;
                   3234:   Q jq,bpe,last_nonzero;
1.167     noro     3235:     int *perm;
1.170     noro     3236:     EPOS oepos;
1.194     noro     3237:     int obpe,oadv,ompos,cbpe;
1.1       noro     3238:
1.174     noro     3239:     nd_module = 0;
1.157     noro     3240:     if ( !m && Demand ) nd_demand = 1;
                   3241:     else nd_demand = 0;
1.172     noro     3242:     parse_nd_option(current_option);
1.78      noro     3243:
1.157     noro     3244:     if ( DP_Multiple )
                   3245:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
1.103     noro     3246: #if 0
1.157     noro     3247:     ndv_alloc = 0;
1.103     noro     3248: #endif
1.157     noro     3249:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.229     noro     3250:     if ( m && nd_vc )
                   3251:        error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
1.157     noro     3252:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   3253:     switch ( ord->id ) {
                   3254:         case 1:
                   3255:             if ( ord->nv != nvar )
                   3256:                 error("nd_{gr,f4} : invalid order specification");
                   3257:             break;
                   3258:         default:
                   3259:             break;
                   3260:     }
                   3261:     nd_nalg = 0;
                   3262:     av = 0;
                   3263:     if ( !m ) {
                   3264:         get_algtree((Obj)f,&av);
                   3265:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   3266:         nd_ntrans = nvar;
                   3267:         nd_nalg = nalg;
                   3268:         /* #i -> t#i */
                   3269:         if ( nalg ) {
                   3270:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   3271:             ord = ord1;
                   3272:             f = f1;
                   3273:         }
                   3274:         nvar += nalg;
                   3275:     }
                   3276:     nd_init_ord(ord);
                   3277:     mrank = 0;
1.178     noro     3278:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
1.157     noro     3279:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   3280:             if ( nd_module ) {
                   3281:                 s = BDY((LIST)BDY(t));
                   3282:                 trank = length(s);
                   3283:                 mrank = MAX(mrank,trank);
                   3284:                 for ( ; s; s = NEXT(s) ) {
                   3285:                     e = getdeg(tv->v,(P)BDY(s));
                   3286:                     max = MAX(e,max);
                   3287:                 }
                   3288:             } else {
                   3289:                 e = getdeg(tv->v,(P)BDY(t));
                   3290:                 max = MAX(e,max);
                   3291:             }
                   3292:         }
1.208     noro     3293:     nd_setup_parameters(nvar,nd_nzlist?0:max);
1.170     noro     3294:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
1.157     noro     3295:     ishomo = 1;
                   3296:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.167     noro     3297:         if ( nd_module ) {
1.245     noro     3298:       if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   3299:       else zpl = (LIST)BDY(t);
1.157     noro     3300:             b = (pointer)pltondv(CO,vv,zpl);
                   3301:         } else {
1.245     noro     3302:       if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   3303:       else zp = (P)BDY(t);
1.157     noro     3304:             b = (pointer)ptondv(CO,vv,zp);
1.167     noro     3305:         }
1.157     noro     3306:         if ( ishomo )
                   3307:             ishomo = ishomo && ndv_ishomo(b);
                   3308:         if ( m ) ndv_mod(m,b);
                   3309:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   3310:     }
                   3311:     if ( fd0 ) NEXT(fd) = 0;
1.184     noro     3312:
1.245     noro     3313:   if ( !ishomo && homo ) {
1.184     noro     3314:         for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   3315:             b = (NDV)BDY(t); len = LEN(b);
                   3316:             for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   3317:                 wmax = MAX(TD(DL(a)),wmax);
                   3318:         }
                   3319:         homogenize_order(ord,nvar,&ord1);
                   3320:         nd_init_ord(ord1);
1.243     noro     3321:         nd_setup_parameters(nvar+1,nd_nzlist?0:wmax);
1.184     noro     3322:         for ( t = fd0; t; t = NEXT(t) )
                   3323:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   3324:     }
                   3325:
1.211     noro     3326:     ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0);
1.172     noro     3327:     if ( nd_gentrace ) {
1.167     noro     3328:         MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   3329:     }
1.245     noro     3330:   if ( nd_splist ) {
                   3331:     *rp = compute_splist();
                   3332:     return;
                   3333:   }
                   3334:   if ( nd_check_splist ) {
1.214     noro     3335:         if ( f4 ) {
                   3336:             if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
                   3337:             else *rp = 0;
                   3338:         } else {
                   3339:             if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
                   3340:             else *rp = 0;
                   3341:         }
1.245     noro     3342:     return;
                   3343:   }
1.234     noro     3344:     x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
1.245     noro     3345:   if ( !x ) {
                   3346:     *rp = 0; return;
                   3347:   }
                   3348:   if ( !ishomo && homo ) {
                   3349:        /* dehomogenization */
                   3350:     for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   3351:     nd_init_ord(ord);
                   3352:     nd_setup_parameters(nvar,0);
                   3353:   }
1.157     noro     3354:     nd_demand = 0;
1.245     noro     3355:   if ( nd_module && nd_intersect ) {
                   3356:     for ( j = nd_psn-1, x = 0; j >= 0; j-- )
                   3357:       if ( MPOS(DL(nd_psh[j])) > 1 ) {
                   3358:         MKNODE(xx,(pointer)j,x); x = xx;
                   3359:       }
                   3360:     conv_ilist(nd_demand,0,x,0);
                   3361:     goto FINAL;
                   3362:   }
1.208     noro     3363:     if ( nd_gentrace  && f4 ) { nzlist = nd_alltracelist; }
1.167     noro     3364:     x = ndv_reducebase(x,perm);
1.208     noro     3365:     if ( nd_gentrace  && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
1.157     noro     3366:     x = ndv_reduceall(m,x);
1.194     noro     3367:     cbpe = nd_bpe;
1.208     noro     3368:     if ( nd_gentrace && !f4 ) {
1.170     noro     3369:         tl2 = nd_alltracelist; nd_alltracelist = 0;
                   3370:         ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
1.198     noro     3371:         tl3 = nd_alltracelist; nd_alltracelist = 0;
                   3372:         if ( nd_gensyz ) {
1.245     noro     3373:           nd_gb(m,0,1,1,0);
1.170     noro     3374:             tl4 = nd_alltracelist; nd_alltracelist = 0;
                   3375:         } else tl4 = 0;
                   3376:     }
1.194     noro     3377:     nd_bpe = cbpe;
                   3378:     nd_setup_parameters(nd_nvar,0);
1.195     noro     3379: FINAL:
1.157     noro     3380:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   3381:         NEXTNODE(r0,r);
1.194     noro     3382:         if ( nd_module ) BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
1.199     noro     3383:         else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
1.245     noro     3384:     else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.157     noro     3385:     }
                   3386:     if ( r0 ) NEXT(r) = 0;
1.208     noro     3387:     if ( !m && nd_nalg )
1.157     noro     3388:         r0 = postprocess_algcoef(av,alist,r0);
                   3389:     MKLIST(*rp,r0);
1.172     noro     3390:     if ( nd_gentrace ) {
1.245     noro     3391:   if ( f4 ) {
1.208     noro     3392:             STOQ(16,bpe);
1.241     noro     3393:             STOQ(nd_last_nonzero,last_nonzero);
                   3394:             tr = mknode(5,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe,last_nonzero); MKLIST(*rp,tr);
                   3395:
1.208     noro     3396:         } else {
                   3397:             tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   3398:             tl3 = reverse_node(tl3);
                   3399:             /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   3400:             for ( t = tl2; t; t = NEXT(t) ) {
                   3401:             /* s = [i,[*,j,*,*],...] */
                   3402:                 s = BDY((LIST)BDY(t));
                   3403:                 j = perm[QTOS((Q)ARG0(s))]; STOQ(j,jq); ARG0(s) = (pointer)jq;
                   3404:                 for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
                   3405:                     j = perm[QTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOQ(j,jq);
                   3406:                     ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   3407:                 }
                   3408:             }
                   3409:             for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
                   3410:                 STOQ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.167     noro     3411:             }
1.208     noro     3412:             MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   3413:             MKLIST(l5,tl4);
                   3414:             STOQ(nd_bpe,bpe);
                   3415:             tr = mknode(8,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe); MKLIST(*rp,tr);
                   3416:         }
1.167     noro     3417:     }
1.103     noro     3418: #if 0
1.157     noro     3419:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     3420: #endif
1.127     noro     3421: }
                   3422:
                   3423: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
                   3424: {
1.157     noro     3425:     VL tv,fv,vv,vc,av;
                   3426:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   3427:     int e,max,nvar,i;
                   3428:     NDV b;
                   3429:     int ishomo,nalg;
                   3430:     Alg alpha,dp;
                   3431:     P p,zp;
                   3432:     Q dmy;
                   3433:     LIST f1,f2;
                   3434:     Obj obj;
                   3435:     NumberField nf;
                   3436:     struct order_spec *ord1;
1.167     noro     3437:     int *perm;
1.157     noro     3438:
1.197     noro     3439:     parse_nd_option(current_option);
1.157     noro     3440:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   3441:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   3442:     switch ( ord->id ) {
                   3443:         case 1:
                   3444:             if ( ord->nv != nvar )
                   3445:                 error("nd_check : invalid order specification");
                   3446:             break;
                   3447:         default:
                   3448:             break;
                   3449:     }
                   3450:     nd_nalg = 0;
                   3451:     av = 0;
                   3452:     if ( !m ) {
                   3453:         get_algtree((Obj)f,&av);
                   3454:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   3455:         nd_ntrans = nvar;
                   3456:         nd_nalg = nalg;
                   3457:         /* #i -> t#i */
                   3458:         if ( nalg ) {
                   3459:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   3460:             ord = ord1;
                   3461:             f = f1;
                   3462:         }
                   3463:         nvar += nalg;
                   3464:     }
                   3465:     nd_init_ord(ord);
1.178     noro     3466:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
1.157     noro     3467:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   3468:             e = getdeg(tv->v,(P)BDY(t));
                   3469:             max = MAX(e,max);
                   3470:         }
                   3471:     nd_setup_parameters(nvar,max);
                   3472:     ishomo = 1;
                   3473:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   3474:         ptozp((P)BDY(t),1,&dmy,&zp);
                   3475:         b = (pointer)ptondv(CO,vv,zp);
                   3476:         if ( ishomo )
                   3477:             ishomo = ishomo && ndv_ishomo(b);
                   3478:         if ( m ) ndv_mod(m,b);
                   3479:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   3480:     }
                   3481:     if ( fd0 ) NEXT(fd) = 0;
                   3482:     ndv_setup(m,0,fd0,0,1);
                   3483:     for ( x = 0, i = 0; i < nd_psn; i++ )
                   3484:         x = update_base(x,i);
                   3485:     if ( do_check ) {
1.168     noro     3486:         x = nd_gb(m,ishomo,1,0,&perm);
1.157     noro     3487:         if ( !x ) {
                   3488:             *rp = 0;
                   3489:             return;
                   3490:         }
                   3491:     } else {
1.175     noro     3492: #if 0
1.245     noro     3493:     /* bug ? */
1.157     noro     3494:         for ( t = x; t; t = NEXT(t) )
1.159     noro     3495:             BDY(t) = (pointer)nd_ps[(long)BDY(t)];
1.175     noro     3496: #else
1.245     noro     3497:     conv_ilist(0,0,x,&perm);
1.175     noro     3498: #endif
1.157     noro     3499:     }
1.167     noro     3500:     x = ndv_reducebase(x,perm);
1.157     noro     3501:     x = ndv_reduceall(m,x);
                   3502:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   3503:         NEXTNODE(r0,r);
                   3504:         BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   3505:     }
                   3506:     if ( r0 ) NEXT(r) = 0;
1.208     noro     3507:     if ( !m && nd_nalg )
1.157     noro     3508:         r0 = postprocess_algcoef(av,alist,r0);
                   3509:     MKLIST(*rp,r0);
1.20      noro     3510: }
                   3511:
1.198     noro     3512: NDV recompute_trace(NODE trace,NDV *p,int m);
                   3513: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
                   3514:
                   3515: NDV recompute_trace(NODE ti,NDV *p,int mod)
                   3516: {
1.245     noro     3517:   int c,c1,c2,i;
                   3518:   NM mul,m,tail;
                   3519:   ND d,r,rm;
                   3520:   NODE sj;
                   3521:   NDV red;
                   3522:   Obj mj;
                   3523:   static int afo=0;
                   3524:
                   3525:   afo++;
                   3526:   mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   3527:   CM(mul) = 1;
                   3528:   tail = 0;
                   3529:   for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
                   3530:     sj = BDY((LIST)BDY(ti));
                   3531:     if ( ARG0(sj) ) {
                   3532:       red = p[QTOS((Q)ARG1(sj))];
                   3533:       mj = (Obj)ARG2(sj);
                   3534:       if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
                   3535:       else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
                   3536:       rm = ndv_mul_nm(mod,mul,red);
                   3537:       if ( !r ) r = rm;
                   3538:       else {
                   3539:         for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
                   3540:           if ( d ) {
                   3541:             NEXT(tail) = m; tail = m; LEN(d)++;
                   3542:           } else {
                   3543:             MKND(nd_nvar,m,1,d); tail = BDY(d);
                   3544:           }
                   3545:         }
                   3546:         if ( !m ) return 0; /* failure */
1.232     noro     3547:                 else {
1.245     noro     3548:           BDY(r) = m;
1.232     noro     3549:                     if ( mod > 0 || mod == -1 ) {
1.245     noro     3550:             c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
                   3551:             DMAR(c1,c2,0,mod,c);
                   3552:             nd_mul_c(mod,rm,c);
1.232     noro     3553:                     } else {
                   3554:                       GZ t,u;
                   3555:
                   3556:                       chsgnlf(HCZ(r),&t);
                   3557:                       divlf(t,HCZ(rm),&u);
                   3558:                       nd_mul_c_lf(rm,u);
                   3559:                     }
1.245     noro     3560:           r = nd_add(mod,r,rm);
                   3561:         }
1.232     noro     3562:           }
1.245     noro     3563:      }
                   3564:   }
                   3565:   if ( tail ) NEXT(tail) = 0;
                   3566:   d = nd_add(mod,d,r);
                   3567:   nd_mul_c(mod,d,invm(HCM(d),mod));
                   3568:   return ndtondv(mod,d);
1.198     noro     3569: }
                   3570:
                   3571: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
                   3572: {
                   3573:     VL tv,fv,vv,vc,av;
                   3574:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   3575:     int e,max,nvar,i;
                   3576:     NDV b;
                   3577:     int ishomo,nalg;
                   3578:     Alg alpha,dp;
                   3579:     P p,zp;
                   3580:     Q dmy;
                   3581:     LIST f1,f2;
                   3582:     Obj obj;
                   3583:     NumberField nf;
                   3584:     struct order_spec *ord1;
1.245     noro     3585:   NODE permtrace,intred,ind,perm,trace,ti;
                   3586:   int len,n,j;
                   3587:   NDV *db,*pb;
1.198     noro     3588:
                   3589:     parse_nd_option(current_option);
                   3590:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   3591:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   3592:     switch ( ord->id ) {
                   3593:         case 1:
                   3594:             if ( ord->nv != nvar )
                   3595:                 error("nd_check : invalid order specification");
                   3596:             break;
                   3597:         default:
                   3598:             break;
                   3599:     }
                   3600:     nd_init_ord(ord);
1.245     noro     3601:   nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
1.198     noro     3602:     nd_setup_parameters(nvar,0);
                   3603:
1.245     noro     3604:   len = length(BDY(f));
                   3605:   db = (NDV *)MALLOC(len*sizeof(NDV *));
                   3606:   for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
                   3607:       ptozp((P)BDY(t),1,&dmy,&zp);
                   3608:       b = ptondv(CO,vv,zp);
1.198     noro     3609:         ndv_mod(m,b);
1.245     noro     3610:     ndv_mul_c(m,b,invm(HCM(b),m));
                   3611:     db[i] = b;
1.198     noro     3612:     }
                   3613:
1.245     noro     3614:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   3615:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   3616:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   3617:   perm = BDY((LIST)ARG0(permtrace));
                   3618:   trace = NEXT(permtrace);
                   3619:
                   3620:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
                   3621:     j = QTOS((Q)ARG0(BDY((LIST)BDY(t))));
                   3622:     if ( j > i ) i = j;
                   3623:   }
                   3624:   n = i+1;
                   3625:   pb = (NDV *)MALLOC(n*sizeof(NDV *));
                   3626:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   3627:     ti = BDY((LIST)BDY(t));
                   3628:     pb[QTOS((Q)ARG0(ti))] = db[QTOS((Q)ARG1(ti))];
                   3629:   }
                   3630:   for ( t = trace; t; t = NEXT(t) ) {
                   3631:     ti = BDY((LIST)BDY(t));
                   3632:     pb[QTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   3633:     if ( !pb[QTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
                   3634:       if ( DP_Print ) {
                   3635:            fprintf(asir_out,"."); fflush(asir_out);
                   3636:       }
                   3637:   }
                   3638:   for ( t = intred; t; t = NEXT(t) ) {
                   3639:     ti = BDY((LIST)BDY(t));
                   3640:     pb[QTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   3641:     if ( !pb[QTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
                   3642:       if ( DP_Print ) {
                   3643:            fprintf(asir_out,"*"); fflush(asir_out);
                   3644:       }
                   3645:   }
1.198     noro     3646:     for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
                   3647:         NEXTNODE(r0,r);
1.245     noro     3648:     b = pb[QTOS((Q)BDY(t))];
1.198     noro     3649:         ndv_mul_c(m,b,invm(HCM(b),m));
                   3650: #if 0
                   3651:         BDY(r) = ndvtop(m,CO,vv,pb[QTOS((Q)BDY(t))]);
                   3652: #else
                   3653:         BDY(r) = ndvtodp(m,pb[QTOS((Q)BDY(t))]);
                   3654: #endif
                   3655:     }
                   3656:     if ( r0 ) NEXT(r) = 0;
                   3657:     MKLIST(*rp,r0);
                   3658:     if ( DP_Print ) fprintf(asir_out,"\n");
                   3659: }
                   3660:
1.133     noro     3661: void nd_gr_trace(LIST f,LIST v,int trace,int homo,int f4,struct order_spec *ord,LIST *rp)
1.20      noro     3662: {
1.157     noro     3663:     VL tv,fv,vv,vc,av;
                   3664:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   3665:     int m,nocheck,nvar,mindex,e,max;
                   3666:     NDV c;
                   3667:     NMV a;
                   3668:     P p,zp;
                   3669:     Q dmy;
                   3670:     EPOS oepos;
1.164     noro     3671:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
1.157     noro     3672:     Alg alpha,dp;
                   3673:     P poly;
1.158     noro     3674:     LIST f1,f2,zpl;
1.157     noro     3675:     Obj obj;
                   3676:     NumberField nf;
                   3677:     struct order_spec *ord1;
                   3678:     struct oEGT eg_check,eg0,eg1;
1.168     noro     3679:     NODE tr,tl1,tl2,tl3,tl4;
                   3680:     LIST l1,l2,l3,l4,l5;
1.167     noro     3681:     int *perm;
1.168     noro     3682:     int j,ret;
1.198     noro     3683:     Q jq,bpe;
1.157     noro     3684:
1.167     noro     3685:     nd_module = 0;
1.235     noro     3686:     nd_lf = 0;
1.172     noro     3687:     parse_nd_option(current_option);
1.234     noro     3688:     if ( nd_lf ) {
                   3689:       if ( f4 )
                   3690:         nd_f4_lf_trace(f,v,trace,homo,ord,rp);
                   3691:       else
                   3692:         error("nd_gr_trace is not implemented yet over a large finite field");
                   3693:       return;
                   3694:     }
1.157     noro     3695:     if ( DP_Multiple )
                   3696:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   3697:
                   3698:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   3699:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   3700:     switch ( ord->id ) {
                   3701:         case 1:
                   3702:             if ( ord->nv != nvar )
                   3703:                 error("nd_gr_trace : invalid order specification");
                   3704:             break;
                   3705:         default:
                   3706:             break;
                   3707:     }
                   3708:
                   3709:     get_algtree((Obj)f,&av);
                   3710:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   3711:     nd_ntrans = nvar;
                   3712:     nd_nalg = nalg;
                   3713:     /* #i -> t#i */
                   3714:     if ( nalg ) {
                   3715:         preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   3716:         ord = ord1;
                   3717:         f = f1;
                   3718:     }
                   3719:     nvar += nalg;
                   3720:
                   3721:     nocheck = 0;
                   3722:     mindex = 0;
                   3723:
                   3724:     if ( Demand ) nd_demand = 1;
                   3725:     else nd_demand = 0;
                   3726:
                   3727:     /* setup modulus */
                   3728:     if ( trace < 0 ) {
                   3729:         trace = -trace;
                   3730:         nocheck = 1;
                   3731:     }
                   3732:     m = trace > 1 ? trace : get_lprime(mindex);
1.158     noro     3733:     nd_init_ord(ord);
                   3734:     mrank = 0;
1.178     noro     3735:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
1.157     noro     3736:         for ( tv = vv; tv; tv = NEXT(tv) ) {
1.158     noro     3737:             if ( nd_module ) {
                   3738:                 s = BDY((LIST)BDY(t));
                   3739:                 trank = length(s);
                   3740:                 mrank = MAX(mrank,trank);
                   3741:                 for ( ; s; s = NEXT(s) ) {
                   3742:                     e = getdeg(tv->v,(P)BDY(s));
                   3743:                     max = MAX(e,max);
                   3744:                 }
                   3745:             } else {
                   3746:                 e = getdeg(tv->v,(P)BDY(t));
                   3747:                 max = MAX(e,max);
                   3748:             }
1.157     noro     3749:         }
                   3750:     nd_setup_parameters(nvar,max);
1.164     noro     3751:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
1.157     noro     3752:     ishomo = 1;
                   3753:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.167     noro     3754:         if ( nd_module ) {
1.245     noro     3755:       if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
                   3756:       else zpl = (LIST)BDY(t);
1.158     noro     3757:             c = (pointer)pltondv(CO,vv,zpl);
                   3758:         } else {
1.245     noro     3759:       if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
                   3760:       else zp = (P)BDY(t);
1.158     noro     3761:             c = (pointer)ptondv(CO,vv,zp);
1.167     noro     3762:         }
1.157     noro     3763:         if ( ishomo )
                   3764:             ishomo = ishomo && ndv_ishomo(c);
                   3765:         if ( c ) {
                   3766:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   3767:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   3768:         }
                   3769:     }
                   3770:     if ( in0 ) NEXT(in) = 0;
                   3771:     if ( fd0 ) NEXT(fd) = 0;
                   3772:     if ( !ishomo && homo ) {
                   3773:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   3774:             c = (NDV)BDY(t); len = LEN(c);
                   3775:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   3776:                 wmax = MAX(TD(DL(a)),wmax);
                   3777:         }
                   3778:         homogenize_order(ord,nvar,&ord1);
                   3779:         nd_init_ord(ord1);
                   3780:         nd_setup_parameters(nvar+1,wmax);
                   3781:         for ( t = fd0; t; t = NEXT(t) )
1.164     noro     3782:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
1.157     noro     3783:     }
1.228     noro     3784:     if ( MaxDeg > 0 ) nocheck = 1;
1.157     noro     3785:     while ( 1 ) {
1.245     noro     3786:     tl1 = tl2 = tl3 = tl4 = 0;
1.157     noro     3787:         if ( Demand )
                   3788:             nd_demand = 1;
1.187     noro     3789:         ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0);
1.172     noro     3790:         if ( nd_gentrace ) {
1.167     noro     3791:             MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   3792:         }
1.177     noro     3793:         if ( ret )
                   3794:             cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
                   3795:         if ( !ret || !cand ) {
1.157     noro     3796:             /* failure */
                   3797:             if ( trace > 1 ) { *rp = 0; return; }
                   3798:             else m = get_lprime(++mindex);
                   3799:             continue;
                   3800:         }
                   3801:         if ( !ishomo && homo ) {
                   3802:             /* dehomogenization */
                   3803:             for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   3804:             nd_init_ord(ord);
                   3805:             nd_setup_parameters(nvar,0);
                   3806:         }
                   3807:         nd_demand = 0;
1.167     noro     3808:         cand = ndv_reducebase(cand,perm);
1.172     noro     3809:         if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
1.157     noro     3810:         cand = ndv_reduceall(0,cand);
                   3811:         cbpe = nd_bpe;
1.172     noro     3812:         if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
1.173     noro     3813:         get_eg(&eg0);
1.157     noro     3814:         if ( nocheck )
                   3815:             break;
1.170     noro     3816:         if ( ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand) ) {
1.172     noro     3817:             if ( nd_gentrace ) {
1.245     noro     3818:           tl3 = nd_alltracelist; nd_alltracelist = 0;
                   3819:         } else tl3 = 0;
1.168     noro     3820:             /* gbcheck : cand is a GB of Id(cand) ? */
1.234     noro     3821:             if ( nd_vc || nd_gentrace || nd_gensyz )
                   3822:               ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
                   3823:             else
                   3824:               ret = nd_f4(0,1,0)!=0;
1.172     noro     3825:             if ( nd_gentrace && nd_gensyz ) {
1.245     noro     3826:           tl4 = nd_alltracelist; nd_alltracelist = 0;
                   3827:         } else tl4 = 0;
                   3828:     }
                   3829:     if ( ret ) break;
1.157     noro     3830:         else if ( trace > 1 ) {
                   3831:             /* failure */
                   3832:             *rp = 0; return;
                   3833:         } else {
                   3834:             /* try the next modulus */
                   3835:             m = get_lprime(++mindex);
                   3836:             /* reset the parameters */
                   3837:             if ( !ishomo && homo ) {
                   3838:                 nd_init_ord(ord1);
                   3839:                 nd_setup_parameters(nvar+1,wmax);
                   3840:             } else {
                   3841:                 nd_init_ord(ord);
                   3842:                 nd_setup_parameters(nvar,max);
                   3843:             }
                   3844:         }
                   3845:     }
                   3846:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   3847:     if ( DP_Print )
1.241     noro     3848:         fprintf(asir_out,"check=%.3fsec,",eg_check.exectime+eg_check.gctime);
1.157     noro     3849:     /* dp->p */
                   3850:     nd_bpe = cbpe;
                   3851:     nd_setup_parameters(nd_nvar,0);
1.158     noro     3852:     for ( r = cand; r; r = NEXT(r) ) {
1.167     noro     3853:     if ( nd_module ) BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.158     noro     3854:         else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
                   3855:     }
1.208     noro     3856:     if ( nd_nalg )
1.157     noro     3857:         cand = postprocess_algcoef(av,alist,cand);
                   3858:     MKLIST(*rp,cand);
1.172     noro     3859:     if ( nd_gentrace ) {
1.167     noro     3860:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
1.245     noro     3861:     tl3 = reverse_node(tl3);
                   3862:     /* tl2 = [[i,[[*,j,*,*],...]],...] */
1.167     noro     3863:         for ( t = tl2; t; t = NEXT(t) ) {
1.245     noro     3864:       /* s = [i,[*,j,*,*],...] */
1.167     noro     3865:             s = BDY((LIST)BDY(t));
                   3866:             j = perm[QTOS((Q)ARG0(s))]; STOQ(j,jq); ARG0(s) = (pointer)jq;
1.245     noro     3867:       for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
1.167     noro     3868:                 j = perm[QTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOQ(j,jq);
1.245     noro     3869:         ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
1.167     noro     3870:             }
1.245     noro     3871:     }
                   3872:     for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
                   3873:         STOQ(perm[j],jq); MKNODE(s,jq,t); t = s;
                   3874:     }
1.168     noro     3875:         MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
1.245     noro     3876:     MKLIST(l5,tl4);
                   3877:       STOQ(nd_bpe,bpe);
1.198     noro     3878:         tr = mknode(8,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe); MKLIST(*rp,tr);
1.167     noro     3879:     }
1.157     noro     3880: }
1.52      noro     3881:
1.157     noro     3882: /* XXX : module element is not considered  */
1.1       noro     3883:
1.61      noro     3884: void dltondl(int n,DL dl,UINT *r)
1.1       noro     3885: {
1.157     noro     3886:     UINT *d;
                   3887:     int i,j,l,s,ord_l;
                   3888:     struct order_pair *op;
                   3889:
                   3890:     d = dl->d;
                   3891:     for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
                   3892:     if ( nd_blockmask ) {
                   3893:         l = nd_blockmask->n;
                   3894:         op = nd_blockmask->order_pair;
                   3895:         for ( j = 0, s = 0; j < l; j++ ) {
                   3896:             ord_l = op[j].length;
                   3897:             for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
                   3898:         }
                   3899:         TD(r) = ndl_weight(r);
                   3900:         ndl_weight_mask(r);
                   3901:     } else {
                   3902:         for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
                   3903:         TD(r) = ndl_weight(r);
                   3904:     }
1.1       noro     3905: }
                   3906:
1.61      noro     3907: DL ndltodl(int n,UINT *ndl)
1.1       noro     3908: {
1.157     noro     3909:     DL dl;
                   3910:     int *d;
                   3911:     int i,j,l,s,ord_l;
                   3912:     struct order_pair *op;
                   3913:
                   3914:     NEWDL(dl,n);
                   3915:     dl->td = TD(ndl);
                   3916:     d = dl->d;
                   3917:     if ( nd_blockmask ) {
                   3918:         l = nd_blockmask->n;
                   3919:         op = nd_blockmask->order_pair;
                   3920:         for ( j = 0, s = 0; j < l; j++ ) {
                   3921:             ord_l = op[j].length;
                   3922:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   3923:         }
                   3924:     } else {
                   3925:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   3926:     }
                   3927:     return dl;
1.1       noro     3928: }
                   3929:
1.167     noro     3930: void nmtodp(int mod,NM m,DP *r)
                   3931: {
                   3932:     DP dp;
                   3933:     MP mr;
                   3934:
                   3935:     NEWMP(mr);
                   3936:     mr->dl = ndltodl(nd_nvar,DL(m));
1.238     noro     3937:     mr->c = (Obj)ndctop(mod,m->c);
1.167     noro     3938:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   3939:     *r = dp;
                   3940: }
                   3941:
1.61      noro     3942: void ndl_print(UINT *dl)
1.1       noro     3943: {
1.157     noro     3944:     int n;
                   3945:     int i,j,l,ord_l,s,s0;
                   3946:     struct order_pair *op;
                   3947:
                   3948:     n = nd_nvar;
                   3949:     printf("<<");
                   3950:     if ( nd_blockmask ) {
                   3951:         l = nd_blockmask->n;
                   3952:         op = nd_blockmask->order_pair;
                   3953:         for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   3954:             ord_l = op[j].length;
                   3955:             for ( i = 0; i < ord_l; i++, s++ )
                   3956:                 printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
                   3957:         }
                   3958:     } else {
                   3959:         for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
                   3960:     }
                   3961:     printf(">>");
1.236     noro     3962:     if ( nd_module && MPOS(dl) )
1.157     noro     3963:         printf("*e%d",MPOS(dl));
1.1       noro     3964: }
                   3965:
                   3966: void nd_print(ND p)
                   3967: {
1.157     noro     3968:     NM m;
1.1       noro     3969:
1.157     noro     3970:     if ( !p )
                   3971:         printf("0\n");
                   3972:     else {
                   3973:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   3974:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   3975:             else printf("+%d*",CM(m));
                   3976:             ndl_print(DL(m));
                   3977:         }
                   3978:         printf("\n");
                   3979:     }
1.1       noro     3980: }
                   3981:
1.113     noro     3982: void nd_print_q(ND p)
1.16      noro     3983: {
1.157     noro     3984:     NM m;
1.16      noro     3985:
1.157     noro     3986:     if ( !p )
                   3987:         printf("0\n");
                   3988:     else {
                   3989:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   3990:             printf("+");
                   3991:             printexpr(CO,(Obj)CQ(m));
                   3992:             printf("*");
                   3993:             ndl_print(DL(m));
                   3994:         }
                   3995:         printf("\n");
                   3996:     }
1.16      noro     3997: }
                   3998:
1.1       noro     3999: void ndp_print(ND_pairs d)
                   4000: {
1.157     noro     4001:     ND_pairs t;
1.1       noro     4002:
1.157     noro     4003:     for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
                   4004:     printf("\n");
1.1       noro     4005: }
                   4006:
1.20      noro     4007: void nd_removecont(int mod,ND p)
1.16      noro     4008: {
1.157     noro     4009:     int i,n;
                   4010:     Q *w;
                   4011:     Q dvr,t;
                   4012:     NM m;
                   4013:     struct oVECT v;
                   4014:     N q,r;
                   4015:
                   4016:     if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
1.232     noro     4017:     else if ( mod == -2 ) {
                   4018:       GZ inv;
                   4019:       divlf(ONEGZ,HCZ(p),&inv);
                   4020:       nd_mul_c_lf(p,inv);
                   4021:     } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
1.157     noro     4022:     else {
                   4023:         for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
1.235     noro     4024:         w = (Q *)MALLOC(n*sizeof(Q));
1.157     noro     4025:         v.len = n;
                   4026:         v.body = (pointer *)w;
                   4027:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
                   4028:         removecont_array((P *)w,n,1);
                   4029:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
                   4030:     }
1.16      noro     4031: }
                   4032:
1.21      noro     4033: void nd_removecont2(ND p1,ND p2)
                   4034: {
1.157     noro     4035:     int i,n1,n2,n;
                   4036:     Q *w;
                   4037:     Q dvr,t;
                   4038:     NM m;
                   4039:     struct oVECT v;
                   4040:     N q,r;
                   4041:
                   4042:     n1 = nd_length(p1);
                   4043:     n2 = nd_length(p2);
                   4044:     n = n1+n2;
1.235     noro     4045:     w = (Q *)MALLOC(n*sizeof(Q));
1.157     noro     4046:     v.len = n;
                   4047:     v.body = (pointer *)w;
                   4048:     i = 0;
                   4049:     if ( p1 )
                   4050:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CQ(m);
                   4051:     if ( p2 )
                   4052:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
                   4053:     removecont_array((P *)w,n,1);
                   4054:     i = 0;
                   4055:     if ( p1 )
                   4056:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CQ(m) = w[i];
                   4057:     if ( p2 )
                   4058:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
1.21      noro     4059: }
                   4060:
1.20      noro     4061: void ndv_removecont(int mod,NDV p)
1.16      noro     4062: {
1.157     noro     4063:     int i,len,all_p;
                   4064:     Q *c;
                   4065:     P *w;
                   4066:     Q dvr,t;
                   4067:     P g,cont,tp;
                   4068:     NMV m;
                   4069:
                   4070:     if ( mod == -1 )
                   4071:         ndv_mul_c(mod,p,_invsf(HCM(p)));
1.232     noro     4072:     else if ( mod == -2 ) {
                   4073:       GZ inv;
                   4074:       divlf(ONEGZ,HCZ(p),&inv);
                   4075:       ndv_mul_c_lf(p,inv);
                   4076:     } else if ( mod )
1.157     noro     4077:         ndv_mul_c(mod,p,invm(HCM(p),mod));
                   4078:     else {
                   4079:         len = p->len;
1.235     noro     4080:         w = (P *)MALLOC(len*sizeof(P));
                   4081:         c = (Q *)MALLOC(len*sizeof(Q));
1.157     noro     4082:         for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
                   4083:             ptozp(CP(m),1,&c[i],&w[i]);
                   4084:             all_p = all_p && !NUM(w[i]);
                   4085:         }
                   4086:         if ( all_p ) {
                   4087:             qltozl(c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
                   4088:             mulp(nd_vc,(P)dvr,g,&cont);
                   4089:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   4090:                 divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
                   4091:             }
                   4092:         } else {
                   4093:             sortbynm((Q *)c,len);
                   4094:             qltozl((Q *)c,len,&dvr);
                   4095:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   4096:                 divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
                   4097:             }
                   4098:         }
                   4099:     }
1.21      noro     4100: }
                   4101:
1.157     noro     4102: /* koko */
                   4103:
1.164     noro     4104: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
1.61      noro     4105: {
1.157     noro     4106:     int len,i,max;
                   4107:     NMV m,mr0,mr,t;
1.61      noro     4108:
1.157     noro     4109:     len = p->len;
1.248   ! noro     4110:     for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
1.157     noro     4111:         max = MAX(max,TD(DL(m)));
                   4112:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   4113:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   4114:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
1.235     noro     4115:     t = (NMV)MALLOC(nmv_adv);
1.157     noro     4116:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.164     noro     4117:         ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.157     noro     4118:         CQ(mr) = CQ(m);
                   4119:         ndl_copy(DL(t),DL(mr));
                   4120:     }
                   4121:     NV(p)++;
                   4122:     BDY(p) = mr0;
1.61      noro     4123: }
                   4124:
1.45      noro     4125: void ndv_dehomogenize(NDV p,struct order_spec *ord)
1.23      noro     4126: {
1.164     noro     4127:     int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
1.167     noro     4128:     int pos;
1.157     noro     4129:     Q *w;
                   4130:     Q dvr,t;
                   4131:     NMV m,r;
                   4132:
                   4133:     len = p->len;
                   4134:     newnvar = nd_nvar-1;
                   4135:     newexporigin = nd_get_exporigin(ord);
1.167     noro     4136:     if ( nd_module ) newmpos = newexporigin-1;
1.157     noro     4137:     newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
                   4138:     for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
                   4139:         ndl_dehomogenize(DL(m));
                   4140:     if ( newwpd != nd_wpd ) {
                   4141:         newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
                   4142:         for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
                   4143:             CQ(r) = CQ(m);
1.167     noro     4144:             if ( nd_module ) pos = MPOS(DL(m));
1.157     noro     4145:             for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   4146:             adj = nd_exporigin-newexporigin;
                   4147:             for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
1.167     noro     4148:             if ( nd_module ) {
                   4149:                 DL(r)[newmpos] = pos;
                   4150:             }
1.157     noro     4151:         }
                   4152:     }
                   4153:     NV(p)--;
1.23      noro     4154: }
                   4155:
1.150     noro     4156: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
                   4157: {
1.157     noro     4158:     int i;
                   4159:     P *tpl,*tpl1;
                   4160:     NODE l;
                   4161:     P h,gcd,t;
                   4162:
1.235     noro     4163:     tpl = (P *)MALLOC(m*sizeof(P));
                   4164:     tpl1 = (P *)MALLOC(m*sizeof(P));
1.157     noro     4165:     bcopy(pl,tpl,m*sizeof(P));
                   4166:     gcd = (P)ONE;
                   4167:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   4168:         h = (P)BDY(l);
                   4169:         while ( 1 ) {
                   4170:             for ( i = 0; i < m; i++ )
                   4171:                 if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
                   4172:                     break;
                   4173:             if ( i == m ) {
                   4174:                 bcopy(tpl1,tpl,m*sizeof(P));
                   4175:                 mulp(vl,gcd,h,&t); gcd = t;
                   4176:             } else
                   4177:                 break;
                   4178:         }
                   4179:     }
                   4180:     if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
                   4181:     if ( full ) {
                   4182:         heu_nezgcdnpz(vl,tpl,m,&t);
                   4183:         mulp(vl,gcd,t,pr);
                   4184:     } else
                   4185:         *pr = gcd;
1.150     noro     4186: }
                   4187:
                   4188: void removecont_array(P *p,int n,int full)
1.146     noro     4189: {
1.157     noro     4190:     int all_p,all_q,i;
                   4191:     Q *c;
                   4192:     P *w;
                   4193:     P t,s;
                   4194:
                   4195:     for ( all_q = 1, i = 0; i < n; i++ )
                   4196:         all_q = all_q && NUM(p[i]);
                   4197:     if ( all_q ) {
                   4198:         removecont_array_q((Q *)p,n);
                   4199:     } else {
1.235     noro     4200:         c = (Q *)MALLOC(n*sizeof(Q));
                   4201:         w = (P *)MALLOC(n*sizeof(P));
1.157     noro     4202:         for ( i = 0; i < n; i++ ) {
                   4203:             ptozp(p[i],1,&c[i],&w[i]);
                   4204:         }
                   4205:         removecont_array_q(c,n);
                   4206:         nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
                   4207:         for ( i = 0; i < n; i++ ) {
                   4208:             divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
                   4209:         }
                   4210:     }
1.146     noro     4211: }
                   4212:
                   4213: void removecont_array_q(Q *c,int n)
1.21      noro     4214: {
1.157     noro     4215:     struct oVECT v;
                   4216:     Q d0,d1,a,u,u1,gcd;
                   4217:     int i,j;
                   4218:     N qn,rn,gn;
                   4219:     Q *q,*r;
                   4220:
1.235     noro     4221:     q = (Q *)MALLOC(n*sizeof(Q));
                   4222:     r = (Q *)MALLOC(n*sizeof(Q));
1.157     noro     4223:     v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   4224:     igcdv_estimate(&v,&d0);
                   4225:     for ( i = 0; i < n; i++ ) {
                   4226:         divn(NM(c[i]),NM(d0),&qn,&rn);
                   4227:         NTOQ(qn,SGN(c[i])*SGN(d0),q[i]);
                   4228:         NTOQ(rn,SGN(c[i]),r[i]);
                   4229:     }
                   4230:     for ( i = 0; i < n; i++ ) if ( r[i] ) break;
                   4231:     if ( i < n ) {
                   4232:         v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   4233:         igcdv(&v,&d1);
                   4234:         gcdn(NM(d0),NM(d1),&gn); NTOQ(gn,1,gcd);
                   4235:         divsn(NM(d0),gn,&qn); NTOQ(qn,1,a);
                   4236:         for ( i = 0; i < n; i++ ) {
                   4237:             mulq(a,q[i],&u);
                   4238:             if ( r[i] ) {
                   4239:                 divsn(NM(r[i]),gn,&qn); NTOQ(qn,SGN(r[i]),u1);
                   4240:                 addq(u,u1,&q[i]);
                   4241:             } else
                   4242:                 q[i] = u;
                   4243:         }
                   4244:     }
                   4245:     for ( i = 0; i < n; i++ ) c[i] = q[i];
1.16      noro     4246: }
                   4247:
1.19      noro     4248: void nd_mul_c(int mod,ND p,int mul)
1.1       noro     4249: {
1.157     noro     4250:     NM m;
                   4251:     int c,c1;
1.1       noro     4252:
1.157     noro     4253:     if ( !p ) return;
                   4254:     if ( mul == 1 ) return;
                   4255:     if ( mod == -1 )
                   4256:         for ( m = BDY(p); m; m = NEXT(m) )
                   4257:             CM(m) = _mulsf(CM(m),mul);
                   4258:     else
                   4259:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   4260:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   4261:         }
1.1       noro     4262: }
                   4263:
1.232     noro     4264: void nd_mul_c_lf(ND p,GZ mul)
                   4265: {
                   4266:     NM m;
                   4267:     GZ c;
                   4268:
                   4269:     if ( !p ) return;
                   4270:     if ( UNIGZ(mul) ) return;
                   4271:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   4272:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   4273:     }
                   4274: }
                   4275:
1.146     noro     4276: void nd_mul_c_q(ND p,P mul)
1.16      noro     4277: {
1.157     noro     4278:     NM m;
                   4279:     P c;
1.16      noro     4280:
1.157     noro     4281:     if ( !p ) return;
                   4282:     if ( UNIQ(mul) ) return;
                   4283:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   4284:         mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
                   4285:     }
1.16      noro     4286: }
                   4287:
1.61      noro     4288: void nd_mul_c_p(VL vl,ND p,P mul)
                   4289: {
1.157     noro     4290:     NM m;
                   4291:     P c;
1.61      noro     4292:
1.157     noro     4293:     if ( !p ) return;
                   4294:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   4295:         mulp(vl,CP(m),mul,&c); CP(m) = c;
                   4296:     }
1.61      noro     4297: }
                   4298:
1.1       noro     4299: void nd_free(ND p)
                   4300: {
1.157     noro     4301:     NM t,s;
1.1       noro     4302:
1.157     noro     4303:     if ( !p ) return;
                   4304:     t = BDY(p);
                   4305:     while ( t ) {
                   4306:         s = NEXT(t);
                   4307:         FREENM(t);
                   4308:         t = s;
                   4309:     }
                   4310:     FREEND(p);
1.1       noro     4311: }
                   4312:
1.23      noro     4313: void ndv_free(NDV p)
                   4314: {
1.200     noro     4315:     GCFREE(BDY(p));
1.23      noro     4316: }
                   4317:
1.61      noro     4318: void nd_append_red(UINT *d,int i)
1.1       noro     4319: {
1.157     noro     4320:     RHist m,m0;
                   4321:     int h;
1.1       noro     4322:
1.157     noro     4323:     NEWRHist(m);
                   4324:     h = ndl_hash_value(d);
                   4325:     m->index = i;
                   4326:     ndl_copy(d,DL(m));
                   4327:     NEXT(m) = nd_red[h];
                   4328:     nd_red[h] = m;
1.1       noro     4329: }
                   4330:
1.61      noro     4331: UINT *ndv_compute_bound(NDV p)
1.1       noro     4332: {
1.157     noro     4333:     UINT *d1,*d2,*t;
                   4334:     UINT u;
                   4335:     int i,j,k,l,len,ind;
                   4336:     NMV m;
                   4337:
                   4338:     if ( !p )
                   4339:         return 0;
1.235     noro     4340:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   4341:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
1.157     noro     4342:     len = LEN(p);
                   4343:     m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   4344:     for ( i = 1; i < len; i++, NMV_ADV(m) ) {
1.159     noro     4345:         ndl_max(DL(m),d1,d2);
1.157     noro     4346:         t = d1; d1 = d2; d2 = t;
                   4347:     }
                   4348:     l = nd_nvar+31;
                   4349:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   4350:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   4351:         u = d1[i];
                   4352:         k = (nd_epw-1)*nd_bpe;
                   4353:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   4354:             t[ind] = (u>>k)&nd_mask0;
                   4355:     }
                   4356:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   4357:     return t;
1.1       noro     4358: }
                   4359:
1.99      noro     4360: UINT *nd_compute_bound(ND p)
                   4361: {
1.157     noro     4362:     UINT *d1,*d2,*t;
                   4363:     UINT u;
                   4364:     int i,j,k,l,len,ind;
                   4365:     NM m;
                   4366:
                   4367:     if ( !p )
                   4368:         return 0;
1.235     noro     4369:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   4370:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
1.157     noro     4371:     len = LEN(p);
                   4372:     m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   4373:     for ( m = NEXT(m); m; m = NEXT(m) ) {
                   4374:         ndl_lcm(DL(m),d1,d2);
                   4375:         t = d1; d1 = d2; d2 = t;
                   4376:     }
                   4377:     l = nd_nvar+31;
                   4378:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   4379:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   4380:         u = d1[i];
                   4381:         k = (nd_epw-1)*nd_bpe;
                   4382:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   4383:             t[ind] = (u>>k)&nd_mask0;
                   4384:     }
                   4385:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   4386:     return t;
1.99      noro     4387: }
                   4388:
1.157     noro     4389: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
                   4390: /* of a term. In this case we need additional 1 word. */
                   4391:
1.48      noro     4392: int nd_get_exporigin(struct order_spec *ord)
                   4393: {
1.157     noro     4394:     switch ( ord->id ) {
                   4395:         case 0: case 2: case 256: case 258:
                   4396:             return 1+nd_module;
                   4397:         case 1: case 257:
                   4398:             /* block order */
                   4399:             /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
                   4400:             /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
                   4401:             return ord->ord.block.length+1+nd_module;
                   4402:         case 3: case 259:
1.236     noro     4403: #if 0
1.157     noro     4404:             error("nd_get_exporigin : composite order is not supported yet.");
1.236     noro     4405: #else
                   4406:             return 1+nd_module;
                   4407: #endif
1.157     noro     4408:     }
1.48      noro     4409: }
                   4410:
1.61      noro     4411: void nd_setup_parameters(int nvar,int max) {
1.157     noro     4412:     int i,j,n,elen,ord_o,ord_l,l,s,wpd;
                   4413:     struct order_pair *op;
1.48      noro     4414:
1.157     noro     4415:     nd_nvar = nvar;
                   4416:     if ( max ) {
                   4417:         /* XXX */
                   4418:         if ( do_weyl ) nd_bpe = 32;
                   4419:         else if ( max < 2 ) nd_bpe = 1;
                   4420:         else if ( max < 4 ) nd_bpe = 2;
                   4421:         else if ( max < 8 ) nd_bpe = 3;
                   4422:         else if ( max < 16 ) nd_bpe = 4;
                   4423:         else if ( max < 32 ) nd_bpe = 5;
                   4424:         else if ( max < 64 ) nd_bpe = 6;
                   4425:         else if ( max < 256 ) nd_bpe = 8;
                   4426:         else if ( max < 1024 ) nd_bpe = 10;
                   4427:         else if ( max < 65536 ) nd_bpe = 16;
                   4428:         else nd_bpe = 32;
                   4429:     }
1.203     noro     4430:     if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
1.201     noro     4431:         UINT t;
1.245     noro     4432:     int st;
1.203     noro     4433:         int *v;
1.245     noro     4434:   /* t = max(weights) */
1.203     noro     4435:         t = 0;
                   4436:         if ( current_dl_weight_vector )
                   4437:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                   4438:                 if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
                   4439:                 if ( t < st ) t = st;
                   4440:             }
                   4441:         if ( nd_matrix )
                   4442:             for ( i = 0; i < nd_matrix_len; i++ )
                   4443:                 for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
                   4444:                     if ( (st=v[j]) < 0 ) st = -st;
                   4445:                     if ( t < st ) t = st;
1.245     noro     4446:         }
1.203     noro     4447:         /* i = bitsize of t */
                   4448:         for ( i = 0; t; t >>=1, i++ );
                   4449:         /* i += bitsize of nd_nvar */
                   4450:         for ( t = nd_nvar; t; t >>=1, i++);
                   4451:         /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
                   4452:         if ( (nd_bpe+i) >= 31 )
                   4453:             error("nd_setup_parameters : too large weight");
                   4454:     }
1.157     noro     4455:     nd_epw = (sizeof(UINT)*8)/nd_bpe;
                   4456:     elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   4457:     nd_exporigin = nd_get_exporigin(nd_ord);
                   4458:     wpd = nd_exporigin+elen;
                   4459:     if ( nd_module )
                   4460:         nd_mpos = nd_exporigin-1;
                   4461:     else
                   4462:         nd_mpos = -1;
                   4463:     if ( wpd != nd_wpd ) {
                   4464:         nd_free_private_storage();
                   4465:         nd_wpd = wpd;
                   4466:     }
                   4467:     if ( nd_bpe < 32 ) {
                   4468:         nd_mask0 = (1<<nd_bpe)-1;
                   4469:     } else {
                   4470:         nd_mask0 = 0xffffffff;
                   4471:     }
                   4472:     bzero(nd_mask,sizeof(nd_mask));
                   4473:     nd_mask1 = 0;
                   4474:     for ( i = 0; i < nd_epw; i++ ) {
                   4475:         nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   4476:         nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   4477:     }
                   4478:     nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
                   4479:     nd_epos = nd_create_epos(nd_ord);
                   4480:     nd_blockmask = nd_create_blockmask(nd_ord);
                   4481:     nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
1.1       noro     4482: }
                   4483:
1.103     noro     4484: ND_pairs nd_reconstruct(int trace,ND_pairs d)
1.1       noro     4485: {
1.157     noro     4486:     int i,obpe,oadv,h;
                   4487:     static NM prev_nm_free_list;
                   4488:     static ND_pairs prev_ndp_free_list;
                   4489:     RHist mr0,mr;
                   4490:     RHist r;
                   4491:     RHist *old_red;
                   4492:     ND_pairs s0,s,t;
                   4493:     EPOS oepos;
                   4494:
                   4495:     obpe = nd_bpe;
                   4496:     oadv = nmv_adv;
                   4497:     oepos = nd_epos;
                   4498:     if ( obpe < 2 ) nd_bpe = 2;
                   4499:     else if ( obpe < 3 ) nd_bpe = 3;
                   4500:     else if ( obpe < 4 ) nd_bpe = 4;
                   4501:     else if ( obpe < 5 ) nd_bpe = 5;
                   4502:     else if ( obpe < 6 ) nd_bpe = 6;
                   4503:     else if ( obpe < 8 ) nd_bpe = 8;
                   4504:     else if ( obpe < 10 ) nd_bpe = 10;
                   4505:     else if ( obpe < 16 ) nd_bpe = 16;
                   4506:     else if ( obpe < 32 ) nd_bpe = 32;
                   4507:     else error("nd_reconstruct : exponent too large");
                   4508:
                   4509:     nd_setup_parameters(nd_nvar,0);
                   4510:     prev_nm_free_list = _nm_free_list;
                   4511:     prev_ndp_free_list = _ndp_free_list;
                   4512:     _nm_free_list = 0;
                   4513:     _ndp_free_list = 0;
1.215     noro     4514:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   4515:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   4516:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   4517:         ndv_realloc(nd_ps_gz[i],obpe,oadv,oepos);
                   4518:     }
1.157     noro     4519:     if ( trace )
1.215     noro     4520:         for ( i = nd_psn-1; i >= 0; i-- ) {
1.157     noro     4521:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
1.215     noro     4522:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   4523:         }
1.157     noro     4524:     s0 = 0;
                   4525:     for ( t = d; t; t = NEXT(t) ) {
                   4526:         NEXTND_pairs(s0,s);
                   4527:         s->i1 = t->i1;
                   4528:         s->i2 = t->i2;
                   4529:         SG(s) = SG(t);
                   4530:         ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   4531:     }
                   4532:
1.235     noro     4533:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
1.157     noro     4534:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   4535:         old_red[i] = nd_red[i];
                   4536:         nd_red[i] = 0;
                   4537:     }
                   4538:     for ( i = 0; i < REDTAB_LEN; i++ )
                   4539:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   4540:             NEWRHist(mr);
                   4541:             mr->index = r->index;
                   4542:             SG(mr) = SG(r);
                   4543:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   4544:             h = ndl_hash_value(DL(mr));
                   4545:             NEXT(mr) = nd_red[h];
                   4546:             nd_red[h] = mr;
                   4547:         }
                   4548:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   4549:     old_red = 0;
                   4550:     for ( i = 0; i < nd_psn; i++ ) {
                   4551:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   4552:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
                   4553:         nd_psh[i] = r;
                   4554:     }
                   4555:     if ( s0 ) NEXT(s) = 0;
                   4556:     prev_nm_free_list = 0;
                   4557:     prev_ndp_free_list = 0;
1.71      noro     4558: #if 0
1.157     noro     4559:     GC_gcollect();
1.71      noro     4560: #endif
1.157     noro     4561:     return s0;
1.1       noro     4562: }
                   4563:
1.61      noro     4564: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
1.1       noro     4565: {
1.157     noro     4566:     int n,i,ei,oepw,omask0,j,s,ord_l,l;
                   4567:     struct order_pair *op;
1.1       noro     4568:
1.157     noro     4569:     n = nd_nvar;
                   4570:     oepw = (sizeof(UINT)*8)/obpe;
                   4571:     omask0 = (1<<obpe)-1;
                   4572:     TD(r) = TD(d);
                   4573:     for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
                   4574:     if ( nd_blockmask ) {
                   4575:         l = nd_blockmask->n;
                   4576:         op = nd_blockmask->order_pair;
                   4577:         for ( i = 1; i < nd_exporigin; i++ )
                   4578:             r[i] = d[i];
                   4579:         for ( j = 0, s = 0; j < l; j++ ) {
                   4580:             ord_l = op[j].length;
                   4581:             for ( i = 0; i < ord_l; i++, s++ ) {
                   4582:                 ei =  GET_EXP_OLD(d,s);
                   4583:                 PUT_EXP(r,s,ei);
                   4584:             }
                   4585:         }
                   4586:     } else {
                   4587:         for ( i = 0; i < n; i++ ) {
                   4588:             ei = GET_EXP_OLD(d,i);
                   4589:             PUT_EXP(r,i,ei);
                   4590:         }
                   4591:     }
                   4592:     if ( nd_module ) MPOS(r) = MPOS(d);
1.1       noro     4593: }
1.3       noro     4594:
1.6       noro     4595: ND nd_copy(ND p)
                   4596: {
1.157     noro     4597:     NM m,mr,mr0;
                   4598:     int c,n;
                   4599:     ND r;
                   4600:
                   4601:     if ( !p )
                   4602:         return 0;
                   4603:     else {
                   4604:         for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   4605:             NEXTNM(mr0,mr);
                   4606:             CM(mr) = CM(m);
                   4607:             ndl_copy(DL(m),DL(mr));
                   4608:         }
                   4609:         NEXT(mr) = 0;
                   4610:         MKND(NV(p),mr0,LEN(p),r);
                   4611:         SG(r) = SG(p);
                   4612:         return r;
                   4613:     }
1.6       noro     4614: }
                   4615:
1.53      noro     4616: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
1.11      noro     4617: {
1.157     noro     4618:     NM m1,m2;
                   4619:     NDV p1,p2;
                   4620:     ND t1,t2;
                   4621:     UINT *lcm;
                   4622:     P gp,tp;
1.167     noro     4623:     Q g,t,iq;
1.157     noro     4624:     int td;
1.167     noro     4625:     LIST hist;
                   4626:     NODE node;
                   4627:     DP d;
1.157     noro     4628:
                   4629:     if ( !mod && nd_demand ) {
                   4630:         p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
                   4631:     } else {
                   4632:         if ( trace ) {
                   4633:             p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   4634:         } else {
                   4635:             p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   4636:         }
                   4637:     }
                   4638:     lcm = LCM(p);
                   4639:     NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
                   4640:     if ( ndl_check_bound2(p->i1,DL(m1)) ) {
                   4641:         FREENM(m1); return 0;
                   4642:     }
                   4643:     NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
                   4644:     if ( ndl_check_bound2(p->i2,DL(m2)) ) {
                   4645:         FREENM(m1); FREENM(m2); return 0;
                   4646:     }
                   4647:
                   4648:     if ( mod == -1 ) {
                   4649:         CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
1.232     noro     4650:     } else if ( mod > 0 ) {
1.157     noro     4651:         CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
1.232     noro     4652:     } else if ( mod == -2 ) {
                   4653:         CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
1.157     noro     4654:     } else if ( nd_vc ) {
                   4655:         ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
                   4656:         divsp(nd_vc,HCP(p2),gp,&CP(m1));
                   4657:         divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
                   4658:     } else {
                   4659:         igcd_cofactor(HCQ(p1),HCQ(p2),&g,&t,&CQ(m1)); chsgnq(t,&CQ(m2));
                   4660:     }
                   4661:     t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
                   4662:     *rp = nd_add(mod,t1,t2);
1.172     noro     4663:     if ( nd_gentrace ) {
1.167     noro     4664:         /* nd_tracelist is initialized */
                   4665:         STOQ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
                   4666:         MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
                   4667:         STOQ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
                   4668:         MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
                   4669:         nd_tracelist = node;
                   4670:     }
1.157     noro     4671:     FREENM(m1); FREENM(m2);
                   4672:     return 1;
1.11      noro     4673: }
                   4674:
1.19      noro     4675: void ndv_mul_c(int mod,NDV p,int mul)
1.11      noro     4676: {
1.157     noro     4677:     NMV m;
                   4678:     int c,c1,len,i;
1.11      noro     4679:
1.157     noro     4680:     if ( !p ) return;
                   4681:     len = LEN(p);
                   4682:     if ( mod == -1 )
                   4683:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   4684:             CM(m) = _mulsf(CM(m),mul);
                   4685:     else
                   4686:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4687:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   4688:         }
1.11      noro     4689: }
                   4690:
1.232     noro     4691: void ndv_mul_c_lf(NDV p,GZ mul)
                   4692: {
                   4693:     NMV m;
                   4694:     GZ c;
                   4695:     int len,i;
                   4696:
                   4697:     if ( !p ) return;
                   4698:     len = LEN(p);
                   4699:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4700:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   4701:     }
                   4702: }
                   4703:
1.113     noro     4704: void ndv_mul_c_q(NDV p,Q mul)
1.16      noro     4705: {
1.157     noro     4706:     NMV m;
                   4707:     Q c;
                   4708:     int len,i;
                   4709:
                   4710:     if ( !p ) return;
                   4711:     len = LEN(p);
                   4712:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4713:         mulq(CQ(m),mul,&c); CQ(m) = c;
                   4714:     }
1.16      noro     4715: }
                   4716:
1.55      noro     4717: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
1.157     noro     4718:     int n2,i,j,l,n,tlen;
                   4719:     UINT *d0;
                   4720:     NM *tab,*psum;
                   4721:     ND s,r;
                   4722:     NM t;
                   4723:     NMV m1;
                   4724:
                   4725:     if ( !p ) return 0;
                   4726:     n = NV(p); n2 = n>>1;
                   4727:     d0 = DL(m0);
                   4728:     l = LEN(p);
                   4729:     for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
1.235     noro     4730:     tab = (NM *)MALLOC(tlen*sizeof(NM));
                   4731:     psum = (NM *)MALLOC(tlen*sizeof(NM));
1.157     noro     4732:     for ( i = 0; i < tlen; i++ ) psum[i] = 0;
                   4733:     m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   4734:     for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
                   4735:         /* m0(NM) * m1(NMV) => tab(NM) */
                   4736:         weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
                   4737:         for ( j = 0; j < tlen; j++ ) {
                   4738:             if ( tab[j] ) {
                   4739:                 NEXT(tab[j]) = psum[j];    psum[j] = tab[j];
                   4740:             }
                   4741:         }
                   4742:     }
                   4743:     for ( i = tlen-1, r = 0; i >= 0; i-- )
                   4744:         if ( psum[i] ) {
                   4745:             for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   4746:             MKND(n,psum[i],j,s);
                   4747:             r = nd_add(mod,r,s);
                   4748:         }
                   4749:     if ( r ) SG(r) = SG(p)+TD(d0);
                   4750:     return r;
1.55      noro     4751: }
                   4752:
1.56      noro     4753: /* product of monomials */
                   4754: /* XXX block order is not handled correctly */
                   4755:
1.55      noro     4756: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   4757: {
1.157     noro     4758:     int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
                   4759:     UINT *d0,*d1,*d,*dt,*ctab;
                   4760:     Q *ctab_q;
                   4761:     Q q,q1;
                   4762:     UINT c0,c1,c;
                   4763:     NM *p;
                   4764:     NM m,t;
                   4765:     int mpos;
                   4766:
                   4767:     for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   4768:     if ( !m0 || !m1 ) return;
                   4769:     d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
1.166     noro     4770:     if ( nd_module )
                   4771:         if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
                   4772:
1.157     noro     4773:     NEWNM(m); d = DL(m);
                   4774:     if ( mod ) {
                   4775:         c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
1.179     noro     4776:     } else if ( nd_vc )
                   4777:         mulp(nd_vc,CP(m0),CP(m1),&CP(m));
1.245     noro     4778:   else
1.157     noro     4779:         mulq(CQ(m0),CQ(m1),&CQ(m));
                   4780:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   4781:     homo = n&1 ? 1 : 0;
                   4782:     if ( homo ) {
                   4783:         /* offset of h-degree */
                   4784:         h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   4785:         PUT_EXP(DL(m),n-1,h);
                   4786:         TD(DL(m)) = h;
                   4787:         if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   4788:     }
                   4789:     tab[0] = m;
                   4790:     NEWNM(m); d = DL(m);
                   4791:     for ( i = 0, curlen = 1; i < n2; i++ ) {
                   4792:         a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   4793:         k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   4794:         /* xi^a*(Di^k*xi^l)*Di^b */
                   4795:         a += l; b += k;
                   4796:         s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
                   4797:         if ( !k || !l ) {
                   4798:             for ( j = 0; j < curlen; j++ )
                   4799:                 if ( t = tab[j] ) {
                   4800:                     dt = DL(t);
                   4801:                     PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
                   4802:                     if ( nd_blockmask ) ndl_weight_mask(dt);
                   4803:                 }
                   4804:             curlen *= k+1;
                   4805:             continue;
                   4806:         }
                   4807:         min = MIN(k,l);
                   4808:         if ( mod ) {
1.235     noro     4809:             ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
1.157     noro     4810:             mkwcm(k,l,mod,ctab);
                   4811:         } else {
1.235     noro     4812:             ctab_q = (Q *)MALLOC((min+1)*sizeof(Q));
1.157     noro     4813:             mkwc(k,l,ctab_q);
                   4814:         }
                   4815:         for ( j = min; j >= 0; j-- ) {
                   4816:             for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
                   4817:             PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
                   4818:             h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
                   4819:             if ( homo ) {
                   4820:                 TD(d) = s;
                   4821:                 PUT_EXP(d,n-1,s-h);
                   4822:             } else TD(d) = h;
                   4823:             if ( nd_blockmask ) ndl_weight_mask(d);
                   4824:             if ( mod ) c = ctab[j];
                   4825:             else q = ctab_q[j];
                   4826:             p = tab+curlen*j;
                   4827:             if ( j == 0 ) {
                   4828:                 for ( u = 0; u < curlen; u++, p++ ) {
                   4829:                     if ( tab[u] ) {
                   4830:                         ndl_addto(DL(tab[u]),d);
                   4831:                         if ( mod ) {
                   4832:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
1.180     noro     4833:                         } else if ( nd_vc )
                   4834:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
1.245     noro     4835:             else {
1.157     noro     4836:                             mulq(CQ(tab[u]),q,&q1); CQ(tab[u]) = q1;
                   4837:                         }
                   4838:                     }
                   4839:                 }
                   4840:             } else {
                   4841:                 for ( u = 0; u < curlen; u++, p++ ) {
                   4842:                     if ( tab[u] ) {
                   4843:                         NEWNM(t);
                   4844:                         ndl_add(DL(tab[u]),d,DL(t));
                   4845:                         if ( mod ) {
                   4846:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
1.181     noro     4847:                         } else if ( nd_vc )
1.180     noro     4848:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
1.245     noro     4849:             else
1.157     noro     4850:                             mulq(CQ(tab[u]),q,&CQ(t));
                   4851:                         *p = t;
                   4852:                     }
                   4853:                 }
                   4854:             }
                   4855:         }
                   4856:         curlen *= k+1;
                   4857:     }
                   4858:     FREENM(m);
1.167     noro     4859:     if ( nd_module ) {
1.166     noro     4860:         mpos = MPOS(d1);
1.167     noro     4861:         for ( i = 0; i < tlen; i++ )
                   4862:             if ( tab[i] ) {
                   4863:                 d = DL(tab[i]);
                   4864:                 MPOS(d) = mpos;
                   4865:                 TD(d) = ndl_weight(d);
                   4866:             }
                   4867:     }
1.55      noro     4868: }
                   4869:
1.63      noro     4870: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   4871: {
1.157     noro     4872:     NM mr,mr0;
                   4873:     NMV m;
                   4874:     UINT *d,*dt,*dm;
                   4875:     int c,n,td,i,c1,c2,len;
                   4876:     Q q;
                   4877:     ND r;
                   4878:
                   4879:     if ( !p ) return 0;
                   4880:     else {
                   4881:         n = NV(p); m = BDY(p);
                   4882:         d = DL(m0);
                   4883:         len = LEN(p);
                   4884:         mr0 = 0;
                   4885:         td = TD(d);
                   4886:         c = CM(m0);
                   4887:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4888:             NEXTNM(mr0,mr);
                   4889:             CM(mr) = 1;
                   4890:             ndl_add(DL(m),d,DL(mr));
                   4891:         }
                   4892:         NEXT(mr) = 0;
                   4893:         MKND(NV(p),mr0,len,r);
                   4894:         SG(r) = SG(p) + TD(d);
                   4895:         return r;
                   4896:     }
1.63      noro     4897: }
                   4898:
1.55      noro     4899: ND ndv_mul_nm(int mod,NM m0,NDV p)
1.9       noro     4900: {
1.157     noro     4901:     NM mr,mr0;
                   4902:     NMV m;
                   4903:     UINT *d,*dt,*dm;
                   4904:     int c,n,td,i,c1,c2,len;
                   4905:     P q;
                   4906:     ND r;
                   4907:
                   4908:     if ( !p ) return 0;
                   4909:     else if ( do_weyl )
1.232     noro     4910:         if ( mod < 0 )
1.157     noro     4911:             error("ndv_mul_nm : not implemented (weyl)");
                   4912:         else
                   4913:             return weyl_ndv_mul_nm(mod,m0,p);
                   4914:     else {
                   4915:         n = NV(p); m = BDY(p);
                   4916:         d = DL(m0);
                   4917:         len = LEN(p);
                   4918:         mr0 = 0;
                   4919:         td = TD(d);
                   4920:         if ( mod == -1 ) {
                   4921:             c = CM(m0);
                   4922:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4923:                 NEXTNM(mr0,mr);
                   4924:                 CM(mr) = _mulsf(CM(m),c);
                   4925:                 ndl_add(DL(m),d,DL(mr));
                   4926:             }
1.232     noro     4927:         } else if ( mod == -2 ) {
                   4928:             GZ cl;
                   4929:             cl = CZ(m0);
                   4930:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4931:                 NEXTNM(mr0,mr);
                   4932:                 mullf(CZ(m),cl,&CZ(mr));
                   4933:                 ndl_add(DL(m),d,DL(mr));
                   4934:             }
1.157     noro     4935:         } else if ( mod ) {
                   4936:             c = CM(m0);
                   4937:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4938:                 NEXTNM(mr0,mr);
                   4939:                 c1 = CM(m);
                   4940:                 DMAR(c1,c,0,mod,c2);
                   4941:                 CM(mr) = c2;
                   4942:                 ndl_add(DL(m),d,DL(mr));
                   4943:             }
                   4944:         } else {
                   4945:             q = CP(m0);
                   4946:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4947:                 NEXTNM(mr0,mr);
                   4948:                 mulp(nd_vc,CP(m),q,&CP(mr));
                   4949:                 ndl_add(DL(m),d,DL(mr));
                   4950:             }
                   4951:         }
                   4952:         NEXT(mr) = 0;
                   4953:         MKND(NV(p),mr0,len,r);
                   4954:         SG(r) = SG(p) + TD(d);
                   4955:         return r;
                   4956:     }
1.4       noro     4957: }
                   4958:
1.104     noro     4959: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
1.99      noro     4960: {
1.157     noro     4961:     NM mq0,mq;
                   4962:     NMV tm;
                   4963:     Q q;
                   4964:     int i,nv,sg,c,c1,c2,hindex;
                   4965:     ND p,t,r;
                   4966:     N tnm;
                   4967:
                   4968:     if ( bucket->m < 0 ) return 0;
                   4969:     else {
                   4970:         nv = NV(d);
                   4971:         mq0 = 0;
1.235     noro     4972:         tm = (NMV)MALLOC(nmv_adv);
1.157     noro     4973:         while ( 1 ) {
1.232     noro     4974:             if ( mod > 0 || mod == -1 )
                   4975:               hindex = head_pbucket(mod,bucket);
                   4976:             else if ( mod == -2 )
                   4977:               hindex = head_pbucket_lf(bucket);
                   4978:             else
                   4979:               hindex = head_pbucket_q(bucket);
1.157     noro     4980:             if ( hindex < 0 ) break;
                   4981:             p = bucket->body[hindex];
                   4982:             NEXTNM(mq0,mq);
                   4983:             ndl_sub(HDL(p),HDL(d),DL(mq));
                   4984:             ndl_copy(DL(mq),DL(tm));
                   4985:             if ( mod ) {
                   4986:                 c1 = invm(HCM(d),mod); c2 = HCM(p);
                   4987:                 DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   4988:                 CM(tm) = mod-c;
                   4989:             } else {
                   4990:                 divsn(NM(HCQ(p)),NM(HCQ(d)),&tnm);
                   4991:                 NTOQ(tnm,SGN(HCQ(p))*SGN(HCQ(d)),CQ(mq));
                   4992:                 chsgnq(CQ(mq),&CQ(tm));
                   4993:             }
                   4994:             t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
                   4995:             bucket->body[hindex] = nd_remove_head(p);
                   4996:             t = nd_remove_head(t);
                   4997:             add_pbucket(mod,bucket,t);
                   4998:         }
                   4999:         if ( !mq0 )
                   5000:             r = 0;
                   5001:         else {
                   5002:             NEXT(mq) = 0;
                   5003:             for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   5004:             MKND(nv,mq0,i,r);
                   5005:             /* XXX */
                   5006:             SG(r) = HTD(r);
                   5007:         }
                   5008:         return r;
                   5009:     }
1.99      noro     5010: }
                   5011:
1.43      noro     5012: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
1.11      noro     5013: {
1.157     noro     5014:     NMV m,mr,mr0,t;
                   5015:     int len,i,k;
1.11      noro     5016:
1.157     noro     5017:     if ( !p ) return;
                   5018:     m = BDY(p); len = LEN(p);
                   5019:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   5020:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   5021:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
1.235     noro     5022:     t = (NMV)MALLOC(nmv_adv);
1.157     noro     5023:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   5024:         CQ(t) = CQ(m);
                   5025:         for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   5026:         ndl_reconstruct(DL(m),DL(t),obpe,oepos);
                   5027:         CQ(mr) = CQ(t);
                   5028:         ndl_copy(DL(t),DL(mr));
                   5029:     }
                   5030:     BDY(p) = mr0;
1.61      noro     5031: }
                   5032:
                   5033: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   5034: {
1.157     noro     5035:     NMV m,mr,mr0;
                   5036:     int len,i;
                   5037:     NDV r;
                   5038:
                   5039:     if ( !p ) return 0;
                   5040:     m = BDY(p); len = LEN(p);
                   5041:     mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   5042:     for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   5043:         ndl_zero(DL(mr));
                   5044:         ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
                   5045:         CQ(mr) = CQ(m);
                   5046:     }
                   5047:     MKNDV(NV(p),mr0,len,r);
                   5048:     SG(r) = SG(p);
                   5049:     return r;
1.11      noro     5050: }
                   5051:
1.61      noro     5052: /* duplicate p */
                   5053:
                   5054: NDV ndv_dup(int mod,NDV p)
1.3       noro     5055: {
1.157     noro     5056:     NDV d;
                   5057:     NMV t,m,m0;
                   5058:     int i,len;
                   5059:
                   5060:     if ( !p ) return 0;
                   5061:     len = LEN(p);
1.232     noro     5062:     m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
1.157     noro     5063:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   5064:         ndl_copy(DL(t),DL(m));
                   5065:         CQ(m) = CQ(t);
                   5066:     }
                   5067:     MKNDV(NV(p),m0,len,d);
                   5068:     SG(d) = SG(p);
                   5069:     return d;
1.23      noro     5070: }
                   5071:
1.215     noro     5072: NDV ndvtondvgz(NDV p)
                   5073: {
1.245     noro     5074:   NDV r;
                   5075:   int len,i;
                   5076:   NMV t;
1.215     noro     5077:
1.245     noro     5078:   r = ndv_dup(0,p);
                   5079:   len = LEN(p);
1.215     noro     5080:     for ( t = BDY(r), i = 0; i < len; i++, NMV_ADV(t) ) CZ(t) = ztogz(CQ(t));
1.245     noro     5081:   return r;
1.215     noro     5082: }
                   5083:
                   5084: NDV ndvgztondv(NDV p)
                   5085: {
1.245     noro     5086:   NDV r;
                   5087:   int len,i;
                   5088:   NMV t;
1.215     noro     5089:
1.245     noro     5090:   r = ndv_dup(0,p);
                   5091:   len = LEN(p);
1.215     noro     5092:     for ( t = BDY(r), i = 0; i < len; i++, NMV_ADV(t) ) CQ(t) = gztoz(CZ(t));
1.245     noro     5093:   return r;
1.215     noro     5094: }
                   5095:
                   5096: NDV ndv_symbolic(int mod,NDV p)
                   5097: {
                   5098:     NDV d;
                   5099:     NMV t,m,m0;
                   5100:     int i,len;
                   5101:
                   5102:     if ( !p ) return 0;
                   5103:     len = LEN(p);
1.232     noro     5104:     m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
1.215     noro     5105:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   5106:         ndl_copy(DL(t),DL(m));
                   5107:         CQ(m) = ONE;
                   5108:     }
                   5109:     MKNDV(NV(p),m0,len,d);
                   5110:     SG(d) = SG(p);
                   5111:     return d;
                   5112: }
                   5113:
1.63      noro     5114: ND nd_dup(ND p)
                   5115: {
1.157     noro     5116:     ND d;
                   5117:     NM t,m,m0;
1.63      noro     5118:
1.157     noro     5119:     if ( !p ) return 0;
                   5120:     for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   5121:         NEXTNM(m0,m);
                   5122:         ndl_copy(DL(t),DL(m));
                   5123:         CQ(m) = CQ(t);
                   5124:     }
                   5125:     if ( m0 ) NEXT(m) = 0;
                   5126:     MKND(NV(p),m0,LEN(p),d);
                   5127:     SG(d) = SG(p);
                   5128:     return d;
1.63      noro     5129: }
                   5130:
1.215     noro     5131: ND ndtondgz(ND p)
                   5132: {
1.245     noro     5133:   ND r;
                   5134:   NM t;
1.215     noro     5135:
1.245     noro     5136:   r = nd_dup(p);
1.215     noro     5137:     for ( t = BDY(r); t; t = NEXT(t) ) CZ(t) = ztogz(CQ(t));
1.245     noro     5138:   return r;
1.215     noro     5139: }
                   5140:
                   5141:
                   5142: ND ndgztond(ND p)
                   5143: {
1.245     noro     5144:   ND r;
                   5145:   NM t;
1.215     noro     5146:
1.245     noro     5147:   r = nd_dup(p);
1.215     noro     5148:     for ( t = BDY(r); t; t = NEXT(t) ) CQ(t) = gztoz(CZ(t));
1.245     noro     5149:   return r;
1.215     noro     5150: }
                   5151:
                   5152:
1.61      noro     5153: /* XXX if p->len == 0 then it represents 0 */
                   5154:
                   5155: void ndv_mod(int mod,NDV p)
                   5156: {
1.157     noro     5157:     NMV t,d;
                   5158:     int r,s,u;
                   5159:     int i,len,dlen;
                   5160:     P cp;
                   5161:     Q c;
                   5162:     Obj gfs;
                   5163:
                   5164:     if ( !p ) return;
                   5165:     len = LEN(p);
                   5166:     dlen = 0;
                   5167:     if ( mod == -1 )
                   5168:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   5169:             simp_ff((Obj)CP(t),&gfs);
1.235     noro     5170:             if ( gfs ) {
                   5171:               r = FTOIF(CONT((GFS)gfs));
                   5172:               CM(d) = r;
                   5173:               ndl_copy(DL(t),DL(d));
                   5174:               NMV_ADV(d);
                   5175:               dlen++;
                   5176:             }
1.157     noro     5177:         }
1.232     noro     5178:     else if ( mod == -2 )
                   5179:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
1.235     noro     5180:             simp_ff((Obj)CP(t),&gfs);
                   5181:             if ( gfs ) {
                   5182:               lmtolf(gfs,&CZ(d));
                   5183:               ndl_copy(DL(t),DL(d));
                   5184:               NMV_ADV(d);
                   5185:               dlen++;
                   5186:             }
1.232     noro     5187:         }
1.157     noro     5188:     else
                   5189:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   5190:             if ( nd_vc ) {
                   5191:                 nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
                   5192:                 c = (Q)cp;
                   5193:             } else
                   5194:                 c = CQ(t);
                   5195:             r = rem(NM(c),mod);
                   5196:             if ( r ) {
                   5197:                 if ( SGN(c) < 0 )
                   5198:                     r = mod-r;
                   5199:                 if ( DN(c) ) {
                   5200:                     s = rem(DN(c),mod);
                   5201:                     if ( !s )
                   5202:                         error("ndv_mod : division by 0");
                   5203:                     s = invm(s,mod);
                   5204:                     DMAR(r,s,0,mod,u); r = u;
                   5205:                 }
                   5206:                 CM(d) = r;
                   5207:                 ndl_copy(DL(t),DL(d));
                   5208:                 NMV_ADV(d);
                   5209:                 dlen++;
                   5210:             }
                   5211:         }
                   5212:     LEN(p) = dlen;
1.61      noro     5213: }
                   5214:
                   5215: NDV ptondv(VL vl,VL dvl,P p)
                   5216: {
1.157     noro     5217:     ND nd;
                   5218:
                   5219:     nd = ptond(vl,dvl,p);
                   5220:     return ndtondv(0,nd);
                   5221: }
1.61      noro     5222:
1.157     noro     5223: void pltozpl(LIST l,Q *cont,LIST *pp)
                   5224: {
                   5225:     NODE nd,nd1;
                   5226:     int n;
                   5227:     P *pl;
                   5228:     Q *cl;
                   5229:     int i;
                   5230:     P dmy;
                   5231:     Q dvr;
                   5232:     LIST r;
                   5233:
                   5234:     nd = BDY(l); n = length(nd);
1.235     noro     5235:     pl = (P *)MALLOC(n*sizeof(P));
                   5236:     cl = (Q *)MALLOC(n*sizeof(P));
1.157     noro     5237:     for ( i = 0; i < n; i++, nd = NEXT(nd) )
                   5238:         ptozp((P)BDY(nd),1,&cl[i],&dmy);
                   5239:     qltozl(cl,n,&dvr);
                   5240:     nd = BDY(l);
                   5241:     for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
                   5242:         divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
                   5243:     }
                   5244:     nd = 0;
                   5245:     for ( i = n-1; i >= 0; i-- ) {
                   5246:         MKNODE(nd1,pl[i],nd); nd = nd1;
                   5247:     }
                   5248:     MKLIST(r,nd);
                   5249:     *pp = r;
                   5250: }
                   5251:
                   5252: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
                   5253:
                   5254: NDV pltondv(VL vl,VL dvl,LIST p)
                   5255: {
                   5256:     int i;
                   5257:     NODE t;
                   5258:     ND r,ri;
                   5259:     NM m;
                   5260:
                   5261:     if ( !nd_module ) error("pltond : module order must be set");
                   5262:     r = 0;
                   5263:     for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
                   5264:         ri = ptond(vl,dvl,(P)BDY(t));
1.163     noro     5265:         if ( ri )
                   5266:             for ( m = BDY(ri); m; m = NEXT(m) ) {
1.167     noro     5267:                 MPOS(DL(m)) = i;
                   5268:                 TD(DL(m)) = ndl_weight(DL(m));
1.163     noro     5269:                 if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   5270:             }
1.157     noro     5271:         r = nd_add(0,r,ri);
                   5272:     }
                   5273:     return ndtondv(0,r);
1.61      noro     5274: }
                   5275:
                   5276: ND ptond(VL vl,VL dvl,P p)
1.23      noro     5277: {
1.157     noro     5278:     int n,i,j,k,e;
                   5279:     VL tvl;
                   5280:     V v;
                   5281:     DCP dc;
                   5282:     DCP *w;
                   5283:     ND r,s,t,u;
                   5284:     P x;
                   5285:     int c;
                   5286:     UINT *d;
                   5287:     NM m,m0;
                   5288:
                   5289:     if ( !p )
                   5290:         return 0;
                   5291:     else if ( NUM(p) ) {
                   5292:         NEWNM(m);
                   5293:         ndl_zero(DL(m));
                   5294:         CQ(m) = (Q)p;
                   5295:         NEXT(m) = 0;
                   5296:         MKND(nd_nvar,m,1,r);
                   5297:         SG(r) = 0;
                   5298:         return r;
                   5299:     } else {
                   5300:         for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
1.235     noro     5301:         w = (DCP *)MALLOC(k*sizeof(DCP));
1.157     noro     5302:         for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   5303:         for ( i = 0, tvl = dvl, v = VR(p);
                   5304:             tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   5305:         if ( !tvl ) {
                   5306:             for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   5307:                 t = ptond(vl,dvl,COEF(w[j]));
                   5308:                 pwrp(vl,x,DEG(w[j]),&p);
                   5309:                 nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   5310:             }
                   5311:             return s;
                   5312:         } else {
                   5313:             NEWNM(m0); d = DL(m0);
                   5314:             for ( j = k-1, s = 0; j >= 0; j-- ) {
                   5315:                 ndl_zero(d); e = QTOS(DEG(w[j])); PUT_EXP(d,i,e);
                   5316:                 TD(d) = MUL_WEIGHT(e,i);
                   5317:                 if ( nd_blockmask) ndl_weight_mask(d);
                   5318:                 if ( nd_module ) MPOS(d) = 0;
                   5319:                 t = ptond(vl,dvl,COEF(w[j]));
                   5320:                 for ( m = BDY(t); m; m = NEXT(m) )
                   5321:                     ndl_addto(DL(m),d);
                   5322:                 SG(t) += TD(d);
                   5323:                 s = nd_add(0,s,t);
                   5324:             }
                   5325:             FREENM(m0);
                   5326:             return s;
                   5327:         }
                   5328:     }
1.61      noro     5329: }
                   5330:
                   5331: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   5332: {
1.157     noro     5333:     VL tvl;
                   5334:     int len,n,j,i,e;
                   5335:     NMV m;
                   5336:     Q q;
                   5337:     P c;
                   5338:     UINT *d;
                   5339:     P s,r,u,t,w;
                   5340:     GFS gfs;
                   5341:
                   5342:     if ( !p ) return 0;
                   5343:     else {
                   5344:         len = LEN(p);
                   5345:         n = NV(p);
                   5346:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   5347:         for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   5348:             if ( mod == -1 ) {
1.232     noro     5349:                e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   5350:             } else if ( mod == -2 ) {
1.238     noro     5351:                c = (P)gztoz(CZ(m));
1.232     noro     5352:             } else if ( mod > 0 ) {
1.157     noro     5353:                 STOQ(CM(m),q); c = (P)q;
                   5354:             } else
                   5355:                 c = CP(m);
                   5356:             d = DL(m);
                   5357:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   5358:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   5359:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   5360:             }
                   5361:             addp(vl,s,t,&u); s = u;
                   5362:         }
                   5363:         return s;
                   5364:     }
                   5365: }
                   5366:
                   5367: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
                   5368: {
                   5369:     VL tvl;
                   5370:     int len,n,j,i,e;
                   5371:     NMV m;
                   5372:     Q q;
                   5373:     P c;
                   5374:     UINT *d;
                   5375:     P s,r,u,t,w;
                   5376:     GFS gfs;
                   5377:     P *a;
                   5378:     LIST l;
                   5379:     NODE nd,nd1;
                   5380:
                   5381:     if ( !p ) return 0;
                   5382:     else {
1.235     noro     5383:         a = (P *)MALLOC((rank+1)*sizeof(P));
1.157     noro     5384:         for ( i = 0; i <= rank; i++ ) a[i] = 0;
                   5385:         len = LEN(p);
                   5386:         n = NV(p);
                   5387:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   5388:         for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
                   5389:             if ( mod == -1 ) {
                   5390:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   5391:             } else if ( mod ) {
                   5392:                 STOQ(CM(m),q); c = (P)q;
                   5393:             } else
                   5394:                 c = CP(m);
                   5395:             d = DL(m);
                   5396:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   5397:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   5398:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   5399:             }
                   5400:             addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
                   5401:         }
                   5402:         nd = 0;
                   5403:         for ( i = rank; i > 0; i-- ) {
                   5404:             MKNODE(nd1,a[i],nd); nd = nd1;
                   5405:         }
                   5406:         MKLIST(l,nd);
                   5407:         return l;
                   5408:     }
1.3       noro     5409: }
                   5410:
1.61      noro     5411: NDV ndtondv(int mod,ND p)
1.11      noro     5412: {
1.157     noro     5413:     NDV d;
                   5414:     NMV m,m0;
                   5415:     NM t;
                   5416:     int i,len;
                   5417:
                   5418:     if ( !p ) return 0;
                   5419:     len = LEN(p);
1.232     noro     5420:     if ( mod > 0 || mod == -1 )
1.200     noro     5421:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
1.157     noro     5422:     else
                   5423:         m0 = m = MALLOC(len*nmv_adv);
1.103     noro     5424: #if 0
1.157     noro     5425:     ndv_alloc += nmv_adv*len;
1.103     noro     5426: #endif
1.157     noro     5427:     for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   5428:         ndl_copy(DL(t),DL(m));
                   5429:         CQ(m) = CQ(t);
                   5430:     }
                   5431:     MKNDV(NV(p),m0,len,d);
                   5432:     SG(d) = SG(p);
                   5433:     return d;
1.11      noro     5434: }
                   5435:
1.61      noro     5436: ND ndvtond(int mod,NDV p)
1.11      noro     5437: {
1.157     noro     5438:     ND d;
                   5439:     NM m,m0;
                   5440:     NMV t;
                   5441:     int i,len;
                   5442:
                   5443:     if ( !p ) return 0;
                   5444:     m0 = 0;
                   5445:     len = p->len;
                   5446:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   5447:         NEXTNM(m0,m);
                   5448:         ndl_copy(DL(t),DL(m));
                   5449:         CQ(m) = CQ(t);
                   5450:     }
                   5451:     NEXT(m) = 0;
                   5452:     MKND(NV(p),m0,len,d);
                   5453:     SG(d) = SG(p);
                   5454:     return d;
1.11      noro     5455: }
                   5456:
1.198     noro     5457: DP ndvtodp(int mod,NDV p)
                   5458: {
                   5459:     MP m,m0;
1.245     noro     5460:   DP d;
1.198     noro     5461:     NMV t;
                   5462:     int i,len;
                   5463:
                   5464:     if ( !p ) return 0;
                   5465:     m0 = 0;
                   5466:     len = p->len;
                   5467:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   5468:         NEXTMP(m0,m);
1.245     noro     5469:       m->dl = ndltodl(nd_nvar,DL(t));
                   5470:       m->c = (Obj)ndctop(mod,t->c);
1.198     noro     5471:     }
                   5472:     NEXT(m) = 0;
1.245     noro     5473:   MKDP(nd_nvar,m0,d);
1.198     noro     5474:     SG(d) = SG(p);
                   5475:     return d;
                   5476: }
                   5477:
1.204     noro     5478: DP ndtodp(int mod,ND p)
                   5479: {
                   5480:     MP m,m0;
1.245     noro     5481:   DP d;
1.204     noro     5482:     NM t;
                   5483:     int i,len;
                   5484:
                   5485:     if ( !p ) return 0;
                   5486:     m0 = 0;
                   5487:     len = p->len;
                   5488:     for ( t = BDY(p); t; t = NEXT(t) ) {
                   5489:         NEXTMP(m0,m);
1.245     noro     5490:       m->dl = ndltodl(nd_nvar,DL(t));
                   5491:       m->c = (Obj)ndctop(mod,t->c);
1.204     noro     5492:     }
                   5493:     NEXT(m) = 0;
1.245     noro     5494:   MKDP(nd_nvar,m0,d);
1.204     noro     5495:     SG(d) = SG(p);
                   5496:     return d;
                   5497: }
                   5498:
1.3       noro     5499: void ndv_print(NDV p)
                   5500: {
1.157     noro     5501:     NMV m;
                   5502:     int i,len;
1.3       noro     5503:
1.157     noro     5504:     if ( !p ) printf("0\n");
                   5505:     else {
                   5506:         len = LEN(p);
                   5507:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   5508:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   5509:             else printf("+%d*",CM(m));
                   5510:             ndl_print(DL(m));
                   5511:         }
                   5512:         printf("\n");
                   5513:     }
1.16      noro     5514: }
                   5515:
1.113     noro     5516: void ndv_print_q(NDV p)
1.16      noro     5517: {
1.157     noro     5518:     NMV m;
                   5519:     int i,len;
1.16      noro     5520:
1.157     noro     5521:     if ( !p ) printf("0\n");
                   5522:     else {
                   5523:         len = LEN(p);
                   5524:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   5525:             printf("+");
                   5526:             printexpr(CO,(Obj)CQ(m));
                   5527:             printf("*");
                   5528:             ndl_print(DL(m));
                   5529:         }
                   5530:         printf("\n");
                   5531:     }
1.25      noro     5532: }
                   5533:
1.167     noro     5534: NODE ndv_reducebase(NODE x,int *perm)
1.27      noro     5535: {
1.157     noro     5536:     int len,i,j;
1.167     noro     5537:     NDVI w;
1.157     noro     5538:     NODE t,t0;
                   5539:
                   5540:     len = length(x);
1.235     noro     5541:     w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
1.167     noro     5542:     for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
                   5543:         w[i].p = BDY(t); w[i].i = perm[i];
                   5544:     }
1.157     noro     5545:     for ( i = 0; i < len; i++ ) {
                   5546:         for ( j = 0; j < i; j++ ) {
1.167     noro     5547:             if ( w[i].p && w[j].p )
                   5548:                 if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
                   5549:                 else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
1.157     noro     5550:         }
                   5551:     }
1.167     noro     5552:     for ( i = j = 0, t0 = 0; i < len; i++ ) {
                   5553:         if ( w[i].p ) {
                   5554:             NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
                   5555:             perm[j++] = w[i].i;
                   5556:         }
1.157     noro     5557:     }
                   5558:     NEXT(t) = 0; x = t0;
                   5559:     return x;
1.11      noro     5560: }
1.32      noro     5561:
1.43      noro     5562: /* XXX incomplete */
                   5563:
1.32      noro     5564: void nd_init_ord(struct order_spec *ord)
                   5565: {
1.224     noro     5566:   nd_module = (ord->id >= 256);
                   5567:   if ( nd_module ) {
                   5568:     nd_dcomp = -1;
                   5569:     nd_ispot = ord->ispot;
                   5570:     nd_pot_nelim = ord->pot_nelim;
                   5571:     nd_poly_weight_len = ord->nv;
                   5572:     nd_poly_weight = ord->top_weight;
                   5573:     nd_module_rank = ord->module_rank;
                   5574:     nd_module_weight = ord->module_top_weight;
                   5575:   }
1.245     noro     5576:   nd_matrix = 0;
                   5577:   nd_matrix_len = 0;
1.157     noro     5578:     switch ( ord->id ) {
                   5579:         case 0:
                   5580:             switch ( ord->ord.simple ) {
                   5581:                 case 0:
                   5582:                     nd_dcomp = 1;
                   5583:                     nd_isrlex = 1;
                   5584:                     break;
                   5585:                 case 1:
                   5586:                     nd_dcomp = 1;
                   5587:                     nd_isrlex = 0;
                   5588:                     break;
                   5589:                 case 2:
                   5590:                     nd_dcomp = 0;
                   5591:                     nd_isrlex = 0;
                   5592:                     ndl_compare_function = ndl_lex_compare;
                   5593:                     break;
                   5594:                 case 11:
                   5595:                     /* XXX */
                   5596:                     nd_dcomp = 0;
                   5597:                     nd_isrlex = 1;
                   5598:                     ndl_compare_function = ndl_ww_lex_compare;
                   5599:                     break;
                   5600:                 default:
                   5601:                     error("nd_gr : unsupported order");
                   5602:             }
                   5603:             break;
                   5604:         case 1:
                   5605:             /* block order */
                   5606:             /* XXX */
                   5607:             nd_dcomp = -1;
                   5608:             nd_isrlex = 0;
                   5609:             ndl_compare_function = ndl_block_compare;
                   5610:             break;
                   5611:         case 2:
                   5612:             /* matrix order */
                   5613:             /* XXX */
                   5614:             nd_dcomp = -1;
                   5615:             nd_isrlex = 0;
                   5616:             nd_matrix_len = ord->ord.matrix.row;
                   5617:             nd_matrix = ord->ord.matrix.matrix;
                   5618:             ndl_compare_function = ndl_matrix_compare;
                   5619:             break;
                   5620:         case 3:
                   5621:             /* composite order */
                   5622:             nd_dcomp = -1;
                   5623:             nd_isrlex = 0;
                   5624:             nd_worb_len = ord->ord.composite.length;
                   5625:             nd_worb = ord->ord.composite.w_or_b;
                   5626:             ndl_compare_function = ndl_composite_compare;
                   5627:             break;
                   5628:
                   5629:         /* module order */
                   5630:         case 256:
                   5631:             switch ( ord->ord.simple ) {
                   5632:                 case 0:
1.167     noro     5633:                     nd_isrlex = 1;
1.157     noro     5634:                     ndl_compare_function = ndl_module_grlex_compare;
                   5635:                     break;
                   5636:                 case 1:
1.167     noro     5637:                     nd_isrlex = 0;
1.157     noro     5638:                     ndl_compare_function = ndl_module_glex_compare;
                   5639:                     break;
                   5640:                 case 2:
1.167     noro     5641:                     nd_isrlex = 0;
1.157     noro     5642:                     ndl_compare_function = ndl_module_lex_compare;
                   5643:                     break;
                   5644:                 default:
                   5645:                     error("nd_gr : unsupported order");
                   5646:             }
                   5647:             break;
                   5648:         case 257:
                   5649:             /* block order */
1.174     noro     5650:             nd_isrlex = 0;
1.157     noro     5651:             ndl_compare_function = ndl_module_block_compare;
                   5652:             break;
                   5653:         case 258:
                   5654:             /* matrix order */
1.174     noro     5655:             nd_isrlex = 0;
1.157     noro     5656:             nd_matrix_len = ord->ord.matrix.row;
                   5657:             nd_matrix = ord->ord.matrix.matrix;
                   5658:             ndl_compare_function = ndl_module_matrix_compare;
                   5659:             break;
                   5660:         case 259:
                   5661:             /* composite order */
1.174     noro     5662:             nd_isrlex = 0;
1.157     noro     5663:             nd_worb_len = ord->ord.composite.length;
                   5664:             nd_worb = ord->ord.composite.w_or_b;
                   5665:             ndl_compare_function = ndl_module_composite_compare;
                   5666:             break;
                   5667:     }
                   5668:     nd_ord = ord;
1.32      noro     5669: }
                   5670:
1.43      noro     5671: BlockMask nd_create_blockmask(struct order_spec *ord)
                   5672: {
1.157     noro     5673:     int n,i,j,s,l;
                   5674:     UINT *t;
                   5675:     BlockMask bm;
                   5676:
                   5677:     /* we only create mask table for block order */
1.164     noro     5678:     if ( ord->id != 1 && ord->id != 257 )
1.157     noro     5679:         return 0;
                   5680:     n = ord->ord.block.length;
                   5681:     bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   5682:     bm->n = n;
                   5683:     bm->order_pair = ord->ord.block.order_pair;
                   5684:     bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
                   5685:     for ( i = 0, s = 0; i < n; i++ ) {
                   5686:         bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
                   5687:         for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   5688:         l = bm->order_pair[i].length;
                   5689:         for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   5690:     }
                   5691:     return bm;
1.57      noro     5692: }
                   5693:
                   5694: EPOS nd_create_epos(struct order_spec *ord)
                   5695: {
1.157     noro     5696:     int i,j,l,s,ord_l,ord_o;
                   5697:     EPOS epos;
                   5698:     struct order_pair *op;
                   5699:
                   5700:     epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   5701:     switch ( ord->id ) {
1.164     noro     5702:         case 0: case 256:
1.157     noro     5703:             if ( nd_isrlex ) {
                   5704:                 for ( i = 0; i < nd_nvar; i++ ) {
                   5705:                     epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   5706:                     epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   5707:                 }
                   5708:             } else {
                   5709:                 for ( i = 0; i < nd_nvar; i++ ) {
                   5710:                     epos[i].i = nd_exporigin + i/nd_epw;
                   5711:                     epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   5712:                 }
                   5713:             }
                   5714:             break;
1.164     noro     5715:         case 1: case 257:
1.157     noro     5716:             /* block order */
                   5717:             l = ord->ord.block.length;
                   5718:             op = ord->ord.block.order_pair;
                   5719:             for ( j = 0, s = 0; j < l; j++ ) {
                   5720:                 ord_o = op[j].order;
                   5721:                 ord_l = op[j].length;
                   5722:                 if ( !ord_o )
                   5723:                     for ( i = 0; i < ord_l; i++ ) {
                   5724:                         epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   5725:                         epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   5726:                     }
                   5727:                 else
                   5728:                     for ( i = 0; i < ord_l; i++ ) {
                   5729:                         epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   5730:                         epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   5731:                     }
                   5732:                 s += ord_l;
                   5733:             }
                   5734:             break;
                   5735:         case 2:
                   5736:             /* matrix order */
                   5737:         case 3:
                   5738:             /* composite order */
1.167     noro     5739:         default:
1.157     noro     5740:             for ( i = 0; i < nd_nvar; i++ ) {
                   5741:                 epos[i].i = nd_exporigin + i/nd_epw;
                   5742:                 epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   5743:             }
                   5744:             break;
                   5745:     }
                   5746:     return epos;
1.43      noro     5747: }
1.59      noro     5748:
                   5749: /* external interface */
                   5750:
1.191     noro     5751: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
1.59      noro     5752: {
1.157     noro     5753:     NODE t,in0,in;
1.191     noro     5754:     ND ndf,nf;
                   5755:     NDV ndvf;
1.157     noro     5756:     VL vv,tv;
1.191     noro     5757:     int stat,nvar,max,mrank;
1.157     noro     5758:     union oNDC dn;
                   5759:     Q cont;
                   5760:     P pp;
1.191     noro     5761:     LIST ppl;
1.157     noro     5762:
                   5763:     if ( !f ) {
                   5764:         *rp = 0;
                   5765:         return;
                   5766:     }
                   5767:     pltovl(v,&vv);
                   5768:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5769:
1.191     noro     5770:     /* max=65536 implies nd_bpe=32 */
                   5771:     max = 65536;
1.157     noro     5772:
1.245     noro     5773:   nd_module = 0;
                   5774:   /* nd_module will be set if ord is a module ordering */
1.157     noro     5775:     nd_init_ord(ord);
                   5776:     nd_setup_parameters(nvar,max);
1.191     noro     5777:     if ( nd_module && OID(f) != O_LIST )
                   5778:         error("nd_nf_p : the first argument must be a list");
1.245     noro     5779:   if ( nd_module ) mrank = length(BDY((LIST)f));
1.157     noro     5780:     /* conversion to ndv */
                   5781:     for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   5782:         NEXTNODE(in0,in);
1.191     noro     5783:         if ( nd_module ) {
                   5784:           if ( !BDY(t) || OID(BDY(t)) != O_LIST
                   5785:                || length(BDY((LIST)BDY(t))) != mrank )
                   5786:               error("nd_nf_p : inconsistent basis element");
                   5787:           if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
                   5788:           else ppl = (LIST)BDY(t);
                   5789:           BDY(in) = (pointer)pltondv(CO,vv,ppl);
                   5790:         } else {
                   5791:           if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
                   5792:           else pp = (P)BDY(t);
                   5793:           BDY(in) = (pointer)ptondv(CO,vv,pp);
                   5794:         }
1.157     noro     5795:         if ( m ) ndv_mod(m,(NDV)BDY(in));
                   5796:     }
1.191     noro     5797:     if ( in0 ) NEXT(in) = 0;
                   5798:
                   5799:     if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
                   5800:     else ndvf = ptondv(CO,vv,(P)f);
                   5801:     if ( m ) ndv_mod(m,ndvf);
                   5802:     ndf = (pointer)ndvtond(m,ndvf);
1.157     noro     5803:
                   5804:     /* dont sort, dont removecont */
                   5805:     ndv_setup(m,0,in0,1,1);
                   5806:     nd_scale=2;
1.191     noro     5807:     stat = nd_nf(m,0,ndf,nd_ps,1,0,&nf);
                   5808:     if ( !stat )
                   5809:         error("nd_nf_p : exponent too large");
                   5810:     if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
                   5811:     else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
1.63      noro     5812: }
                   5813:
                   5814: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   5815: {
1.157     noro     5816:     NM m;
                   5817:     UINT *t,*s;
                   5818:     int i;
                   5819:
                   5820:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   5821:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   5822:         t = DL(m);
                   5823:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5824:         r[i] = CM(m);
                   5825:     }
                   5826:     for ( i = 0; !r[i]; i++ );
                   5827:     return i;
1.63      noro     5828: }
                   5829:
1.242     noro     5830: #if defined(__GNUC__) && SIZEOF_LONG==8
1.240     noro     5831:
                   5832: #define MOD128(a,c,m) ((a)=(((c)!=0||((a)>=(m)))?(((((U128)(c))<<64)+(a))%(m)):(a)))
                   5833:
                   5834: int nd_to_vect64(int mod,UINT *s0,int n,ND d,U64 *r)
1.239     noro     5835: {
                   5836:     NM m;
                   5837:     UINT *t,*s;
                   5838:     int i;
                   5839:
                   5840:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   5841:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   5842:         t = DL(m);
                   5843:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.240     noro     5844:         r[i] = (U64)CM(m);
1.239     noro     5845:     }
                   5846:     for ( i = 0; !r[i]; i++ );
                   5847:     return i;
                   5848: }
                   5849: #endif
                   5850:
1.113     noro     5851: int nd_to_vect_q(UINT *s0,int n,ND d,Q *r)
1.74      noro     5852: {
1.157     noro     5853:     NM m;
                   5854:     UINT *t,*s;
                   5855:     int i;
                   5856:
                   5857:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   5858:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   5859:         t = DL(m);
                   5860:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5861:         r[i] = CQ(m);
                   5862:     }
                   5863:     for ( i = 0; !r[i]; i++ );
                   5864:     return i;
1.74      noro     5865: }
                   5866:
1.232     noro     5867: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
                   5868: {
                   5869:     NM m;
                   5870:     UINT *t,*s;
                   5871:     int i;
                   5872:
                   5873:     for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
                   5874:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   5875:         t = DL(m);
                   5876:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5877:         mpz_set(r[i],BDY(CZ(m)));
                   5878:     }
                   5879:     for ( i = 0; !mpz_sgn(r[i]); i++ );
                   5880:     return i;
                   5881: }
                   5882:
1.220     noro     5883: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
                   5884: {
                   5885:     NM m;
                   5886:     unsigned long *v;
                   5887:     int i,j,h,size;
1.245     noro     5888:   UINT *s,*t;
1.220     noro     5889:
1.245     noro     5890:   size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
1.220     noro     5891:     v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
                   5892:     bzero(v,size);
                   5893:     for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
1.245     noro     5894:     t = DL(m);
                   5895:     h = ndl_hash_value(t);
1.220     noro     5896:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
1.245     noro     5897:       v[i/BLEN] |= 1L <<(i%BLEN);
1.220     noro     5898:     }
                   5899:     return v;
                   5900: }
                   5901:
                   5902: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
                   5903: {
                   5904:     NMV mr;
                   5905:     UINT *d,*t,*s;
                   5906:     int i,j,len,h,head;
                   5907:
                   5908:     d = DL(m);
                   5909:     len = LEN(p);
1.235     noro     5910:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
1.220     noro     5911:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   5912:         ndl_add(d,DL(mr),t);
1.245     noro     5913:     h = ndl_hash_value(t);
1.220     noro     5914:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
1.245     noro     5915:     if ( j == 0 ) head = i;
                   5916:       v[i/BLEN] |= 1L <<(i%BLEN);
1.220     noro     5917:     }
                   5918:     return head;
                   5919: }
                   5920:
1.129     noro     5921: Q *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   5922: {
1.157     noro     5923:     NM m;
                   5924:     NMV mr;
                   5925:     UINT *d,*t,*s;
                   5926:     NDV p;
                   5927:     int i,j,len;
                   5928:     Q *r;
                   5929:
                   5930:     m = pair->mul;
                   5931:     d = DL(m);
                   5932:     p = nd_ps[pair->index];
                   5933:     len = LEN(p);
                   5934:     r = (Q *)CALLOC(n,sizeof(Q));
1.235     noro     5935:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
1.157     noro     5936:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   5937:         ndl_add(d,DL(mr),t);
                   5938:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5939:         r[i] = CQ(mr);
                   5940:     }
                   5941:     return r;
1.129     noro     5942: }
                   5943:
1.235     noro     5944: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,int *s0hash,NM_ind_pair pair)
1.64      noro     5945: {
1.157     noro     5946:     NM m;
                   5947:     NMV mr;
                   5948:     UINT *d,*t,*s;
                   5949:     NDV p;
                   5950:     unsigned char *ivc;
                   5951:     unsigned short *ivs;
                   5952:     UINT *v,*ivi,*s0v;
1.210     noro     5953:     int i,j,len,prev,diff,cdiff,h;
1.157     noro     5954:     IndArray r;
1.198     noro     5955: struct oEGT eg0,eg1;
1.157     noro     5956:
                   5957:     m = pair->mul;
                   5958:     d = DL(m);
1.235     noro     5959:     if ( trace )
                   5960:       p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
                   5961:     else
                   5962:       p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
                   5963:
1.157     noro     5964:     len = LEN(p);
1.235     noro     5965:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
                   5966:     v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.198     noro     5967: get_eg(&eg0);
1.157     noro     5968:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   5969:         ndl_add(d,DL(mr),t);
1.245     noro     5970:     h = ndl_hash_value(t);
1.210     noro     5971:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
1.157     noro     5972:         v[j] = i;
                   5973:     }
1.198     noro     5974: get_eg(&eg1); add_eg(&eg_search,&eg0,&eg1);
1.157     noro     5975:     r = (IndArray)MALLOC(sizeof(struct oIndArray));
                   5976:     r->head = v[0];
                   5977:     diff = 0;
                   5978:     for ( i = 1; i < len; i++ ) {
                   5979:         cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   5980:     }
                   5981:     if ( diff < 256 ) {
                   5982:         r->width = 1;
                   5983:         ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   5984:         r->index.c = ivc;
                   5985:         for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   5986:     } else if ( diff < 65536 ) {
                   5987:         r->width = 2;
                   5988:         ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   5989:         r->index.s = ivs;
                   5990:         for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   5991:     } else {
                   5992:         r->width = 4;
                   5993:         ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   5994:         r->index.i = ivi;
                   5995:         for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
                   5996:     }
                   5997:     return r;
1.64      noro     5998: }
                   5999:
1.135     noro     6000: int compress_array(Q *svect,Q *cvect,int n)
                   6001: {
1.157     noro     6002:     int i,j;
1.135     noro     6003:
1.157     noro     6004:     for ( i = j = 0; i < n; i++ )
                   6005:         if ( svect[i] ) cvect[j++] = svect[i];
                   6006:     return j;
1.135     noro     6007: }
                   6008:
                   6009: void expand_array(Q *svect,Q *cvect,int n)
                   6010: {
1.157     noro     6011:     int i,j;
1.135     noro     6012:
1.157     noro     6013:     for ( i = j = 0; j < n;  i++  )
                   6014:         if ( svect[i] ) svect[i] = cvect[j++];
1.135     noro     6015: }
                   6016:
1.133     noro     6017: int ndv_reduce_vect_q(Q *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.107     noro     6018: {
1.157     noro     6019:     int i,j,k,len,pos,prev,nz;
                   6020:     Q cs,mcs,c1,c2,cr,gcd,t;
                   6021:     IndArray ivect;
                   6022:     unsigned char *ivc;
                   6023:     unsigned short *ivs;
                   6024:     unsigned int *ivi;
                   6025:     NDV redv;
                   6026:     NMV mr;
                   6027:     NODE rp;
                   6028:     int maxrs;
                   6029:     double hmag;
                   6030:     Q *cvect;
                   6031:
                   6032:     maxrs = 0;
                   6033:     for ( i = 0; i < col && !svect[i]; i++ );
                   6034:     if ( i == col ) return maxrs;
                   6035:     hmag = p_mag((P)svect[i])*nd_scale;
1.235     noro     6036:     cvect = (Q *)MALLOC(col*sizeof(Q));
1.157     noro     6037:     for ( i = 0; i < nred; i++ ) {
                   6038:         ivect = imat[i];
                   6039:         k = ivect->head;
                   6040:         if ( svect[k] ) {
                   6041:             maxrs = MAX(maxrs,rp0[i]->sugar);
1.215     noro     6042:             redv = nd_demand?ndv_load(rp0[i]->index)
                   6043:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
1.157     noro     6044:             len = LEN(redv); mr = BDY(redv);
                   6045:             igcd_cofactor(svect[k],CQ(mr),&gcd,&cs,&cr);
                   6046:             chsgnq(cs,&mcs);
                   6047:             if ( !UNIQ(cr) ) {
                   6048:                 for ( j = 0; j < col; j++ ) {
                   6049:                     mulq(svect[j],cr,&c1); svect[j] = c1;
                   6050:                 }
                   6051:             }
                   6052:             svect[k] = 0; prev = k;
                   6053:             switch ( ivect->width ) {
                   6054:                 case 1:
                   6055:                     ivc = ivect->index.c;
                   6056:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6057:                         pos = prev+ivc[j]; prev = pos;
                   6058:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   6059:                     }
                   6060:                     break;
                   6061:                 case 2:
                   6062:                     ivs = ivect->index.s;
                   6063:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6064:                         pos = prev+ivs[j]; prev = pos;
                   6065:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   6066:                     }
                   6067:                     break;
                   6068:                 case 4:
                   6069:                     ivi = ivect->index.i;
                   6070:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6071:                         pos = prev+ivi[j]; prev = pos;
                   6072:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   6073:                     }
                   6074:                     break;
                   6075:             }
                   6076:             for ( j = k+1; j < col && !svect[j]; j++ );
                   6077:             if ( j == col ) break;
                   6078:             if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
                   6079:                 nz = compress_array(svect,cvect,col);
                   6080:                 removecont_array((P *)cvect,nz,1);
                   6081:                 expand_array(svect,cvect,nz);
                   6082:                 hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   6083:             }
                   6084:         }
                   6085:     }
                   6086:     nz = compress_array(svect,cvect,col);
                   6087:     removecont_array((P *)cvect,nz,1);
                   6088:     expand_array(svect,cvect,nz);
                   6089:     if ( DP_Print ) {
                   6090:         fprintf(asir_out,"-"); fflush(asir_out);
                   6091:     }
                   6092:     return maxrs;
1.107     noro     6093: }
                   6094:
1.215     noro     6095: int ndv_reduce_vect_gz(GZ *gvect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   6096: {
                   6097:     int i,j,k,l,len,pos,prev,nz;
                   6098:     GZ cs,mcs,c1,c2,cr,gcd,t;
                   6099:     IndArray ivect;
                   6100:     unsigned char *ivc;
                   6101:     unsigned short *ivs;
                   6102:     unsigned int *ivi;
                   6103:     NDV redv;
                   6104:     NMV mr;
                   6105:     NODE rp;
                   6106:     int maxrs;
                   6107:     double hmag;
1.245     noro     6108:   struct oVECT v;
1.215     noro     6109:
                   6110:     maxrs = 0;
                   6111:     for ( i = 0; i < col && !gvect[i]; i++ );
                   6112:     if ( i == col ) return maxrs;
                   6113:     hmag = (double)n_bits_gz(gvect[i])*nd_scale;
                   6114:     for ( i = 0; i < nred; i++ ) {
                   6115:         ivect = imat[i];
                   6116:         k = ivect->head;
                   6117:         if ( gvect[k] ) {
                   6118:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   6119:             redv = nd_ps_gz[rp0[i]->index];
                   6120:             len = LEN(redv); mr = BDY(redv);
1.245     noro     6121:       gcdgz(gvect[k],CZ(mr),&gcd);
                   6122:       divsgz(gvect[k],gcd,&cs);
                   6123:       divsgz(CZ(mr),gcd,&cr);
1.215     noro     6124:             chsgngz(cs,&mcs);
1.245     noro     6125:       if ( !UNIGZ(cr) ) {
1.215     noro     6126:                for ( j = 0; j < col; j++ ) {
                   6127:                     mulgz(gvect[j],cr,&c1); gvect[j] = c1;
                   6128:                 }
                   6129:             }
                   6130:             gvect[k] = 0; prev = k;
                   6131:             switch ( ivect->width ) {
                   6132:                 case 1:
                   6133:                     ivc = ivect->index.c;
                   6134:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6135:                         pos = prev+ivc[j]; prev = pos;
                   6136:                         mulgz(CZ(mr),mcs,&c2); addgz(gvect[pos],c2,&t); gvect[pos] = t;
                   6137:                     }
                   6138:                     break;
                   6139:                 case 2:
                   6140:                     ivs = ivect->index.s;
                   6141:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6142:                         pos = prev+ivs[j]; prev = pos;
                   6143:                         mulgz(CZ(mr),mcs,&c2); addgz(gvect[pos],c2,&t); gvect[pos] = t;
                   6144:                     }
                   6145:                     break;
                   6146:                 case 4:
                   6147:                     ivi = ivect->index.i;
                   6148:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6149:                         pos = prev+ivi[j]; prev = pos;
                   6150:                         mulgz(CZ(mr),mcs,&c2); addgz(gvect[pos],c2,&t); gvect[pos] = t;
                   6151:                     }
                   6152:                     break;
                   6153:             }
                   6154:             for ( j = k+1; j < col && !gvect[j]; j++ );
                   6155:             if ( j == col ) break;
                   6156:             if ( hmag && ((double)n_bits_gz(gvect[j]) > hmag) ) {
1.245     noro     6157:         v.len = col; v.body = (pointer)gvect; gcdvgz(&v,&gcd);
1.215     noro     6158: #if 1
1.245     noro     6159:         for ( l = 0; l < col; l++ ) { divsgz(gvect[l],gcd,&t); gvect[l] = t; }
1.215     noro     6160: #endif
1.245     noro     6161:           hmag = (double)n_bits_gz(gvect[j])*nd_scale;
1.215     noro     6162:             }
                   6163:         }
                   6164:     }
                   6165:     for ( j = 0; j < col && !gvect[j]; j++ );
1.245     noro     6166:   if ( j < col ) {
                   6167:     v.len = col; v.body = (pointer)gvect; gcdvgz(&v,&gcd);
                   6168:     for ( l = 0; l < col; l++ ) { divsgz(gvect[l],gcd,&t); gvect[l] = t; }
                   6169:   }
1.215     noro     6170:     if ( DP_Print ) {
                   6171:         fprintf(asir_out,"-"); fflush(asir_out);
                   6172:     }
                   6173:     return maxrs;
                   6174: }
                   6175:
1.76      noro     6176: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.65      noro     6177: {
1.157     noro     6178:     int i,j,k,len,pos,prev;
                   6179:     UINT c,c1,c2,c3,up,lo,dmy;
                   6180:     IndArray ivect;
                   6181:     unsigned char *ivc;
                   6182:     unsigned short *ivs;
                   6183:     unsigned int *ivi;
                   6184:     NDV redv;
                   6185:     NMV mr;
                   6186:     NODE rp;
                   6187:     int maxrs;
                   6188:
                   6189:     maxrs = 0;
                   6190:     for ( i = 0; i < nred; i++ ) {
                   6191:         ivect = imat[i];
                   6192:         k = ivect->head; svect[k] %= m;
                   6193:         if ( c = svect[k] ) {
                   6194:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   6195:             c = m-c; redv = nd_ps[rp0[i]->index];
                   6196:             len = LEN(redv); mr = BDY(redv);
                   6197:             svect[k] = 0; prev = k;
                   6198:             switch ( ivect->width ) {
                   6199:                 case 1:
                   6200:                     ivc = ivect->index.c;
                   6201:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
1.215     noro     6202:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.245     noro     6203:             if ( c1 ) {
                   6204:               c2 = svect[pos];
                   6205:                           DMA(c1,c,c2,up,lo);
                   6206:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   6207:                           } else svect[pos] = lo;
                   6208:             }
1.157     noro     6209:                     }
                   6210:                     break;
                   6211:                 case 2:
                   6212:                     ivs = ivect->index.s;
                   6213:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
1.215     noro     6214:                         pos = prev+ivs[j]; c1 = CM(mr);
1.157     noro     6215:                         prev = pos;
1.245     noro     6216:             if ( c1 ) {
                   6217:               c2 = svect[pos];
                   6218:                           DMA(c1,c,c2,up,lo);
                   6219:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   6220:                           } else svect[pos] = lo;
                   6221:             }
1.157     noro     6222:                     }
                   6223:                     break;
                   6224:                 case 4:
                   6225:                     ivi = ivect->index.i;
                   6226:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
1.215     noro     6227:                         pos = prev+ivi[j]; c1 = CM(mr);
1.157     noro     6228:                         prev = pos;
1.245     noro     6229:             if ( c1 ) {
                   6230:               c2 = svect[pos];
                   6231:                           DMA(c1,c,c2,up,lo);
                   6232:                           if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   6233:                           } else svect[pos] = lo;
                   6234:             }
1.157     noro     6235:                     }
                   6236:                     break;
                   6237:             }
                   6238:         }
                   6239:     }
                   6240:     for ( i = 0; i < col; i++ )
                   6241:         if ( svect[i] >= (UINT)m ) svect[i] %= m;
                   6242:     return maxrs;
1.65      noro     6243: }
                   6244:
1.242     noro     6245: #if defined(__GNUC__) && SIZEOF_LONG==8
1.240     noro     6246:
                   6247: int ndv_reduce_vect64(int m,U64 *svect,U64 *cvect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.239     noro     6248: {
                   6249:     int i,j,k,len,pos,prev;
1.240     noro     6250:     U64 a,c,c1,c2;
1.239     noro     6251:     IndArray ivect;
                   6252:     unsigned char *ivc;
                   6253:     unsigned short *ivs;
                   6254:     unsigned int *ivi;
                   6255:     NDV redv;
                   6256:     NMV mr;
                   6257:     NODE rp;
                   6258:     int maxrs;
                   6259:
1.240     noro     6260:     for ( i = 0; i < col; i++ ) cvect[i] = 0;
1.239     noro     6261:     maxrs = 0;
                   6262:     for ( i = 0; i < nred; i++ ) {
                   6263:         ivect = imat[i];
1.240     noro     6264:         k = ivect->head;
                   6265:         a = svect[k]; c = cvect[k];
                   6266:         MOD128(a,c,m);
                   6267:         svect[k] = a; cvect[k] = 0;
1.239     noro     6268:         if ( c = svect[k] ) {
                   6269:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   6270:             c = m-c; redv = nd_ps[rp0[i]->index];
                   6271:             len = LEN(redv); mr = BDY(redv);
                   6272:             svect[k] = 0; prev = k;
                   6273:             switch ( ivect->width ) {
                   6274:                 case 1:
                   6275:                     ivc = ivect->index.c;
                   6276:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6277:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
1.240     noro     6278:                         if ( c1 ) {
                   6279:                           c2 = svect[pos]+c1*c;
                   6280:                           if ( c2 < svect[pos] ) cvect[pos]++;
                   6281:                           svect[pos] = c2;
                   6282:                         }
1.239     noro     6283:                     }
                   6284:                     break;
                   6285:                 case 2:
                   6286:                     ivs = ivect->index.s;
                   6287:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6288:                         pos = prev+ivs[j]; c1 = CM(mr); prev = pos;
1.240     noro     6289:                         if ( c1 ) {
                   6290:                           c2 = svect[pos]+c1*c;
                   6291:                           if ( c2 < svect[pos] ) cvect[pos]++;
                   6292:                           svect[pos] = c2;
                   6293:                         }
1.239     noro     6294:                     }
                   6295:                     break;
                   6296:                 case 4:
                   6297:                     ivi = ivect->index.i;
                   6298:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6299:                         pos = prev+ivi[j]; c1 = CM(mr); prev = pos;
1.240     noro     6300:                         if ( c1 ) {
                   6301:                           c2 = svect[pos]+c1*c;
                   6302:                           if ( c2 < svect[pos] ) cvect[pos]++;
                   6303:                           svect[pos] = c2;
                   6304:                         }
1.239     noro     6305:                     }
                   6306:                     break;
                   6307:             }
                   6308:         }
                   6309:     }
1.240     noro     6310:     for ( i = 0; i < col; i++ ) {
                   6311:       a = svect[i]; c = cvect[i]; MOD128(a,c,m); svect[i] = a;
                   6312:     }
1.239     noro     6313:     return maxrs;
                   6314: }
                   6315: #endif
                   6316:
1.76      noro     6317: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.72      noro     6318: {
1.157     noro     6319:     int i,j,k,len,pos,prev;
                   6320:     UINT c,c1,c2,c3,up,lo,dmy;
                   6321:     IndArray ivect;
                   6322:     unsigned char *ivc;
                   6323:     unsigned short *ivs;
                   6324:     unsigned int *ivi;
                   6325:     NDV redv;
                   6326:     NMV mr;
                   6327:     NODE rp;
                   6328:     int maxrs;
                   6329:
                   6330:     maxrs = 0;
                   6331:     for ( i = 0; i < nred; i++ ) {
                   6332:         ivect = imat[i];
1.232     noro     6333:         k = ivect->head;
1.157     noro     6334:         if ( c = svect[k] ) {
                   6335:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   6336:             c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
                   6337:             len = LEN(redv); mr = BDY(redv);
                   6338:             svect[k] = 0; prev = k;
                   6339:             switch ( ivect->width ) {
                   6340:                 case 1:
                   6341:                     ivc = ivect->index.c;
                   6342:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6343:                         pos = prev+ivc[j]; prev = pos;
                   6344:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   6345:                     }
                   6346:                     break;
                   6347:                 case 2:
                   6348:                     ivs = ivect->index.s;
                   6349:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6350:                         pos = prev+ivs[j]; prev = pos;
                   6351:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   6352:                     }
                   6353:                     break;
                   6354:                 case 4:
                   6355:                     ivi = ivect->index.i;
                   6356:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6357:                         pos = prev+ivi[j]; prev = pos;
                   6358:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   6359:                     }
                   6360:                     break;
                   6361:             }
                   6362:         }
                   6363:     }
                   6364:     return maxrs;
1.72      noro     6365: }
                   6366:
1.232     noro     6367: ND nd_add_lf(ND p1,ND p2)
                   6368: {
                   6369:     int n,c,can;
                   6370:     ND r;
                   6371:     NM m1,m2,mr0,mr,s;
                   6372:     GZ t;
                   6373:
                   6374:     if ( !p1 ) return p2;
                   6375:     else if ( !p2 ) return p1;
                   6376:     else {
                   6377:         can = 0;
                   6378:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   6379:             c = DL_COMPARE(DL(m1),DL(m2));
                   6380:             switch ( c ) {
                   6381:                 case 0:
                   6382:                     addlf(CZ(m1),CZ(m2),&t);
                   6383:                     s = m1; m1 = NEXT(m1);
                   6384:                     if ( t ) {
                   6385:                         can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
                   6386:                     } else {
                   6387:                         can += 2; FREENM(s);
                   6388:                     }
                   6389:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   6390:                     break;
                   6391:                 case 1:
                   6392:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   6393:                     break;
                   6394:                 case -1:
                   6395:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   6396:                     break;
                   6397:             }
                   6398:         }
                   6399:         if ( !mr0 )
                   6400:             if ( m1 ) mr0 = m1;
                   6401:             else if ( m2 ) mr0 = m2;
                   6402:             else return 0;
                   6403:         else if ( m1 ) NEXT(mr) = m1;
                   6404:         else if ( m2 ) NEXT(mr) = m2;
                   6405:         else NEXT(mr) = 0;
                   6406:         BDY(p1) = mr0;
                   6407:         SG(p1) = MAX(SG(p1),SG(p2));
                   6408:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   6409:         FREEND(p2);
                   6410:         return p1;
                   6411:     }
                   6412: }
                   6413:
1.234     noro     6414: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.232     noro     6415: {
                   6416:     int i,j,k,len,pos,prev;
                   6417:     mpz_t c,mc,c1;
                   6418:     IndArray ivect;
                   6419:     unsigned char *ivc;
                   6420:     unsigned short *ivs;
                   6421:     unsigned int *ivi;
                   6422:     NDV redv;
                   6423:     NMV mr;
                   6424:     NODE rp;
                   6425:     int maxrs;
                   6426:
                   6427:     maxrs = 0;
                   6428:     lf_lazy = 1;
                   6429:     for ( i = 0; i < nred; i++ ) {
                   6430:         ivect = imat[i];
                   6431:         k = ivect->head;
                   6432:         mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
                   6433:         if ( mpz_sgn(svect[k]) ) {
                   6434:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   6435:             mpz_neg(svect[k],svect[k]);
1.234     noro     6436:             redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
1.232     noro     6437:             len = LEN(redv); mr = BDY(redv);
                   6438:             prev = k;
                   6439:             switch ( ivect->width ) {
                   6440:                 case 1:
                   6441:                     ivc = ivect->index.c;
                   6442:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6443:                         pos = prev+ivc[j]; prev = pos;
                   6444:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   6445:                     }
                   6446:                     break;
                   6447:                 case 2:
                   6448:                     ivs = ivect->index.s;
                   6449:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6450:                         pos = prev+ivs[j]; prev = pos;
                   6451:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   6452:                     }
                   6453:                     break;
                   6454:                 case 4:
                   6455:                     ivi = ivect->index.i;
                   6456:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6457:                         pos = prev+ivi[j]; prev = pos;
                   6458:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   6459:                     }
                   6460:                     break;
                   6461:             }
                   6462:             mpz_set_ui(svect[k],0);
                   6463:         }
                   6464:     }
                   6465:     lf_lazy=0;
                   6466:     for ( i = 0; i < col; i++ ) {
                   6467:         mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
                   6468:     }
                   6469:     return maxrs;
                   6470: }
                   6471:
                   6472: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
                   6473: {
                   6474:     int i,j,k,l,rank,s;
                   6475:     mpz_t a,a1,inv;
                   6476:     mpz_t *t,*pivot,*pk;
                   6477:     mpz_t **mat;
                   6478:     struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
1.234     noro     6479:     int size,size1;
1.232     noro     6480:
                   6481:     mpz_init(inv);
                   6482:     mpz_init(a);
                   6483:     mat = (mpz_t **)mat0;
1.234     noro     6484:         size = 0;
1.232     noro     6485:     for ( rank = 0, j = 0; j < col; j++ ) {
                   6486:         for ( i = rank; i < row; i++ ) {
                   6487:             mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
                   6488:         }
                   6489:         for ( i = rank; i < row; i++ )
                   6490:             if ( mpz_sgn(mat[i][j]) )
                   6491:                 break;
                   6492:         if ( i == row ) {
                   6493:             colstat[j] = 0;
                   6494:             continue;
                   6495:         } else
                   6496:             colstat[j] = 1;
                   6497:         if ( i != rank ) {
                   6498:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   6499:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   6500:         }
                   6501:         pivot = mat[rank];
                   6502:         s = sugar[rank];
                   6503:         mpz_invert(inv,pivot[j],BDY(current_mod_lf));
                   6504:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   6505:             if ( mpz_sgn(*pk) ) {
                   6506:                 mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
                   6507:             }
                   6508:         for ( i = rank+1; i < row; i++ ) {
                   6509:             t = mat[i];
                   6510:             if ( mpz_sgn(t[j]) ) {
                   6511:                 sugar[i] = MAX(sugar[i],s);
                   6512:                 mpz_neg(a,t[j]);
                   6513:                 red_by_vect_lf(t+j,pivot+j,a,col-j);
                   6514:             }
                   6515:         }
                   6516:         rank++;
                   6517:     }
                   6518:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   6519:         if ( colstat[j] ) {
                   6520:             pivot = mat[l];
                   6521:             s = sugar[l];
                   6522:             for ( k = j; k < col; k++ )
                   6523:               mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
                   6524:             for ( i = 0; i < l; i++ ) {
                   6525:                 t = mat[i];
                   6526:                 if ( mpz_sgn(t[j]) ) {
                   6527:                     sugar[i] = MAX(sugar[i],s);
                   6528:                     mpz_neg(a,t[j]);
                   6529:                     red_by_vect_lf(t+j,pivot+j,a,col-j);
                   6530:                 }
                   6531:             }
                   6532:             l--;
                   6533:         }
                   6534:     for ( j = 0, l = 0; l < rank; j++ )
                   6535:         if ( colstat[j] ) {
                   6536:             t = mat[l];
                   6537:             for ( k = j; k < col; k++ ) {
                   6538:                 mpz_mod(t[k],t[k],BDY(current_mod_lf));
                   6539:             }
                   6540:             l++;
                   6541:         }
                   6542:     return rank;
                   6543: }
                   6544:
                   6545:
1.65      noro     6546: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   6547: {
1.157     noro     6548:     int j,k,len;
                   6549:     UINT *p;
                   6550:     UINT c;
                   6551:     NDV r;
                   6552:     NMV mr0,mr;
                   6553:
                   6554:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   6555:     if ( !len ) return 0;
                   6556:     else {
1.200     noro     6557:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
1.103     noro     6558: #if 0
1.157     noro     6559:         ndv_alloc += nmv_adv*len;
1.103     noro     6560: #endif
1.157     noro     6561:         mr = mr0;
                   6562:         p = s0vect;
                   6563:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   6564:             if ( !rhead[j] ) {
                   6565:                 if ( c = vect[k++] ) {
                   6566:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   6567:                 }
                   6568:             }
                   6569:         MKNDV(nd_nvar,mr0,len,r);
                   6570:         return r;
                   6571:     }
1.65      noro     6572: }
                   6573:
1.242     noro     6574: #if defined(__GNUC__) && SIZEOF_LONG==8
1.240     noro     6575: NDV vect64_to_ndv(U64 *vect,int spcol,int col,int *rhead,UINT *s0vect)
1.239     noro     6576: {
                   6577:     int j,k,len;
                   6578:     UINT *p;
                   6579:     UINT c;
                   6580:     NDV r;
                   6581:     NMV mr0,mr;
                   6582:
                   6583:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   6584:     if ( !len ) return 0;
                   6585:     else {
                   6586:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   6587: #if 0
                   6588:         ndv_alloc += nmv_adv*len;
                   6589: #endif
                   6590:         mr = mr0;
                   6591:         p = s0vect;
                   6592:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   6593:             if ( !rhead[j] ) {
                   6594:                 if ( c = (UINT)vect[k++] ) {
                   6595:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   6596:                 }
                   6597:             }
                   6598:         MKNDV(nd_nvar,mr0,len,r);
                   6599:         return r;
                   6600:     }
                   6601: }
                   6602: #endif
                   6603:
1.220     noro     6604: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
                   6605: {
                   6606:     int j,k,len;
                   6607:     UINT *p;
                   6608:     NDV r;
                   6609:     NMV mr0,mr;
                   6610:
                   6611:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
                   6612:     if ( !len ) return 0;
                   6613:     else {
                   6614:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   6615:         mr = mr0;
                   6616:         p = s0vect;
                   6617:         for ( j = 0; j < col; j++, p += nd_wpd )
1.245     noro     6618:       if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
1.220     noro     6619:             ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
                   6620:           }
                   6621:         MKNDV(nd_nvar,mr0,len,r);
                   6622:         return r;
                   6623:     }
                   6624: }
                   6625:
1.129     noro     6626: /* for preprocessed vector */
                   6627:
1.113     noro     6628: NDV vect_to_ndv_q(Q *vect,int spcol,int col,int *rhead,UINT *s0vect)
1.107     noro     6629: {
1.157     noro     6630:     int j,k,len;
                   6631:     UINT *p;
                   6632:     Q c;
                   6633:     NDV r;
                   6634:     NMV mr0,mr;
                   6635:
                   6636:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   6637:     if ( !len ) return 0;
                   6638:     else {
1.200     noro     6639:         mr0 = (NMV)MALLOC(nmv_adv*len);
1.107     noro     6640: #if 0
1.157     noro     6641:         ndv_alloc += nmv_adv*len;
1.107     noro     6642: #endif
1.157     noro     6643:         mr = mr0;
                   6644:         p = s0vect;
                   6645:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   6646:             if ( !rhead[j] ) {
                   6647:                 if ( c = vect[k++] ) {
                   6648:                     if ( DN(c) )
                   6649:                         error("afo");
                   6650:                     ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
                   6651:                 }
                   6652:             }
                   6653:         MKNDV(nd_nvar,mr0,len,r);
                   6654:         return r;
                   6655:     }
1.107     noro     6656: }
                   6657:
1.215     noro     6658: NDV vect_to_ndv_gz(GZ *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   6659: {
                   6660:     int j,k,len;
                   6661:     UINT *p;
1.232     noro     6662:     GZ c;
1.215     noro     6663:     NDV r;
                   6664:     NMV mr0,mr;
                   6665:
                   6666:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   6667:     if ( !len ) return 0;
                   6668:     else {
                   6669:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   6670: #if 0
                   6671:         ndv_alloc += nmv_adv*len;
                   6672: #endif
                   6673:         mr = mr0;
                   6674:         p = s0vect;
                   6675:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   6676:             if ( !rhead[j] ) {
                   6677:                 if ( c = vect[k++] ) {
                   6678:                     ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
                   6679:                 }
                   6680:             }
                   6681:         MKNDV(nd_nvar,mr0,len,r);
                   6682:         return r;
                   6683:     }
                   6684: }
                   6685:
1.232     noro     6686: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   6687: {
                   6688:     int j,k,len;
                   6689:     UINT *p;
                   6690:     mpz_t c;
                   6691:     NDV r;
                   6692:     NMV mr0,mr;
                   6693:
                   6694:     for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
                   6695:     if ( !len ) return 0;
                   6696:     else {
                   6697:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   6698: #if 0
                   6699:         ndv_alloc += nmv_adv*len;
                   6700: #endif
                   6701:         mr = mr0;
                   6702:         p = s0vect;
                   6703:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   6704:             if ( !rhead[j] ) {
                   6705:                 c[0] = vect[k++][0];
                   6706:                 if ( mpz_sgn(c) ) {
                   6707:                     ndl_copy(p,DL(mr)); MPZTOGZ(c,CZ(mr)); NMV_ADV(mr);
                   6708:                 }
                   6709:             }
                   6710:         MKNDV(nd_nvar,mr0,len,r);
                   6711:         return r;
                   6712:     }
                   6713: }
                   6714:
1.129     noro     6715: /* for plain vector */
                   6716:
                   6717: NDV plain_vect_to_ndv_q(Q *vect,int col,UINT *s0vect)
                   6718: {
1.157     noro     6719:     int j,k,len;
                   6720:     UINT *p;
                   6721:     Q c;
                   6722:     NDV r;
                   6723:     NMV mr0,mr;
                   6724:
                   6725:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   6726:     if ( !len ) return 0;
                   6727:     else {
1.200     noro     6728:         mr0 = (NMV)MALLOC(nmv_adv*len);
1.129     noro     6729: #if 0
1.157     noro     6730:         ndv_alloc += nmv_adv*len;
1.129     noro     6731: #endif
1.157     noro     6732:         mr = mr0;
                   6733:         p = s0vect;
                   6734:         for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   6735:             if ( c = vect[k] ) {
                   6736:                 if ( DN(c) )
                   6737:                     error("afo");
                   6738:                 ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
                   6739:             }
                   6740:         MKNDV(nd_nvar,mr0,len,r);
                   6741:         return r;
                   6742:     }
1.129     noro     6743: }
                   6744:
1.133     noro     6745: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
1.65      noro     6746: {
1.157     noro     6747:     ND_pairs t;
                   6748:     NODE sp0,sp;
                   6749:     int stat;
                   6750:     ND spol;
                   6751:
                   6752:     for ( t = l; t; t = NEXT(t) ) {
                   6753:         stat = nd_sp(m,trace,t,&spol);
                   6754:         if ( !stat ) return 0;
                   6755:         if ( spol ) {
                   6756:             add_pbucket_symbolic(bucket,spol);
                   6757:         }
                   6758:     }
                   6759:     return 1;
1.65      noro     6760: }
                   6761:
1.133     noro     6762: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
1.65      noro     6763: {
1.157     noro     6764:     NODE rp0,rp;
                   6765:     NM mul,head,s0,s;
                   6766:     int index,col,i,sugar;
                   6767:     RHist h;
                   6768:     UINT *s0v,*p;
                   6769:     NM_ind_pair pair;
                   6770:     ND red;
                   6771:     NDV *ps;
1.235     noro     6772:     static int afo;
1.157     noro     6773:
                   6774:     s0 = 0; rp0 = 0; col = 0;
1.245     noro     6775:   if ( nd_demand )
                   6776:       ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   6777:   else
                   6778:       ps = trace?nd_ps_trace:nd_ps;
1.157     noro     6779:     while ( 1 ) {
                   6780:         head = remove_head_pbucket_symbolic(bucket);
                   6781:         if ( !head ) break;
                   6782:         if ( !s0 ) s0 = head;
                   6783:         else NEXT(s) = head;
                   6784:         s = head;
                   6785:         index = ndl_find_reducer(DL(head));
                   6786:         if ( index >= 0 ) {
                   6787:             h = nd_psh[index];
                   6788:             NEWNM(mul);
                   6789:             ndl_sub(DL(head),DL(h),DL(mul));
1.235     noro     6790:             if ( ndl_check_bound2(index,DL(mul)) )
                   6791:                 return 0;
1.157     noro     6792:             sugar = TD(DL(mul))+SG(ps[index]);
                   6793:             MKNM_ind_pair(pair,mul,index,sugar);
                   6794:             red = ndv_mul_nm_symbolic(mul,ps[index]);
                   6795:             add_pbucket_symbolic(bucket,nd_remove_head(red));
                   6796:             NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   6797:         }
                   6798:         col++;
                   6799:     }
                   6800:     if ( rp0 ) NEXT(rp) = 0;
                   6801:     NEXT(s) = 0;
                   6802:     s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   6803:     for ( i = 0, p = s0v, s = s0; i < col;
                   6804:         i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   6805:     *s0vect = s0v;
                   6806:     *r = rp0;
                   6807:     return col;
1.65      noro     6808: }
                   6809:
1.244     noro     6810: void print_ndp(ND_pairs l)
                   6811: {
                   6812:   ND_pairs t;
                   6813:
                   6814:   for ( t = l; t; t = NEXT(t) )
                   6815:     printf("[%d,%d] ",t->i1,t->i2);
                   6816:   printf("\n");
                   6817: }
                   6818:
1.234     noro     6819: NODE nd_f4(int m,int checkonly,int **indp)
1.69      noro     6820: {
1.234     noro     6821:     int i,nh,stat,index,f4red;
1.208     noro     6822:     NODE r,g,tn0,tn,node;
1.244     noro     6823:     ND_pairs d,l,t,ll0,ll,lh;
1.245     noro     6824:   LIST l0,l1;
1.157     noro     6825:     ND spol,red;
                   6826:     NDV nf,redv;
                   6827:     NM s0,s;
1.231     noro     6828:     NODE rp0,srp0,nflist,nzlist,nzlist_t;
1.208     noro     6829:     int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
1.157     noro     6830:     UINT c;
                   6831:     UINT **spmat;
                   6832:     UINT *s0vect,*svect,*p,*v;
                   6833:     int *colstat;
                   6834:     IndArray *imat;
                   6835:     int *rhead;
                   6836:     int spcol,sprow;
1.244     noro     6837:     int sugar,sugarh;
1.157     noro     6838:     PGeoBucket bucket;
                   6839:     struct oEGT eg0,eg1,eg_f4;
1.208     noro     6840:     Q i1,i2,sugarq;
1.103     noro     6841: #if 0
1.157     noro     6842:     ndv_alloc = 0;
1.103     noro     6843: #endif
1.157     noro     6844:     g = 0; d = 0;
                   6845:     for ( i = 0; i < nd_psn; i++ ) {
1.244     noro     6846:         d = update_pairs(d,g,i,0);
1.157     noro     6847:         g = update_base(g,i);
                   6848:     }
1.245     noro     6849:   nzlist = 0;
1.231     noro     6850:     nzlist_t = nd_nzlist;
1.241     noro     6851:     f4red = 1;
                   6852:     nd_last_nonzero = 0;
1.244     noro     6853:     while ( d ) {
1.157     noro     6854:         get_eg(&eg0);
1.244     noro     6855:         l = nd_minsugarp(d,&d);
                   6856:         sugar = nd_sugarweight?l->sugar2:SG(l);
                   6857:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   6858:         if ( nzlist_t ) {
1.231     noro     6859:             node = BDY((LIST)BDY(nzlist_t));
1.244     noro     6860:             sugarh = QTOS((Q)ARG0(node));
1.231     noro     6861:             tn = BDY((LIST)ARG1(node));
                   6862:             if ( !tn ) {
                   6863:               nzlist_t = NEXT(nzlist_t);
                   6864:               continue;
                   6865:             }
                   6866:             /* tn = [[i1,i2],...] */
1.244     noro     6867:             lh = nd_ipairtospair(tn);
1.208     noro     6868:         }
1.157     noro     6869:         bucket = create_pbucket();
                   6870:         stat = nd_sp_f4(m,0,l,bucket);
                   6871:         if ( !stat ) {
1.244     noro     6872:             for ( t = l; NEXT(t); t = NEXT(t) );
                   6873:             NEXT(t) = d; d = l;
                   6874:             d = nd_reconstruct(0,d);
1.157     noro     6875:             continue;
                   6876:         }
                   6877:         if ( bucket->m < 0 ) continue;
                   6878:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   6879:         if ( !col ) {
                   6880:             for ( t = l; NEXT(t); t = NEXT(t) );
                   6881:             NEXT(t) = d; d = l;
                   6882:             d = nd_reconstruct(0,d);
                   6883:             continue;
                   6884:         }
                   6885:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   6886:         if ( DP_Print )
1.241     noro     6887:             fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.157     noro     6888:                 sugar,eg_f4.exectime+eg_f4.gctime);
1.244     noro     6889:         nflist = nd_f4_red(m,nd_nzlist?lh:l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
1.234     noro     6890:         if ( checkonly && nflist ) return 0;
1.157     noro     6891:         /* adding new bases */
1.241     noro     6892:         if ( nflist ) nd_last_nonzero = f4red;
1.157     noro     6893:         for ( r = nflist; r; r = NEXT(r) ) {
                   6894:             nf = (NDV)BDY(r);
                   6895:             ndv_removecont(m,nf);
                   6896:             if ( !m && nd_nalg ) {
                   6897:                 ND nf1;
                   6898:
                   6899:                 nf1 = ndvtond(m,nf);
                   6900:                 nd_monic(0,&nf1);
                   6901:                 nd_removecont(m,nf1);
                   6902:                 nf = ndtondv(m,nf1);
                   6903:             }
1.215     noro     6904:             nh = ndv_newps(m,nf,0,1);
1.244     noro     6905:             d = update_pairs(d,g,nh,0);
1.157     noro     6906:             g = update_base(g,nh);
                   6907:         }
1.234     noro     6908:         if ( DP_Print ) {
1.241     noro     6909:           fprintf(asir_out,"f4red=%d,gblen=%d\n",f4red,length(g)); fflush(asir_out);
1.234     noro     6910:         }
1.208     noro     6911:         if ( nd_gentrace ) {
1.245     noro     6912:       for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
                   6913:         NEXTNODE(tn0,tn);
1.208     noro     6914:                 STOQ(t->i1,i1); STOQ(t->i2,i2);
                   6915:                 node = mknode(2,i1,i2); MKLIST(l0,node);
1.245     noro     6916:         BDY(tn) = l0;
                   6917:       }
                   6918:       if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
1.208     noro     6919:             STOQ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
                   6920:             MKNODE(node,l1,nzlist); nzlist = node;
                   6921:         }
1.231     noro     6922:         if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
1.234     noro     6923:         f4red++;
1.241     noro     6924:         if ( nd_f4red && f4red > nd_f4red ) break;
1.234     noro     6925:         if ( nd_rank0 && !nflist ) break;
1.208     noro     6926:     }
                   6927:     if ( nd_gentrace ) {
1.245     noro     6928:     MKLIST(l0,reverse_node(nzlist));
1.208     noro     6929:         MKNODE(nd_alltracelist,l0,0);
1.157     noro     6930:     }
1.103     noro     6931: #if 0
1.157     noro     6932:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     6933: #endif
1.245     noro     6934:   conv_ilist(nd_demand,0,g,indp);
1.157     noro     6935:     return g;
1.69      noro     6936: }
1.74      noro     6937:
1.167     noro     6938: NODE nd_f4_trace(int m,int **indp)
1.133     noro     6939: {
1.157     noro     6940:     int i,nh,stat,index;
                   6941:     NODE r,g;
                   6942:     ND_pairs d,l,l0,t;
                   6943:     ND spol,red;
                   6944:     NDV nf,redv,nfqv,nfv;
                   6945:     NM s0,s;
                   6946:     NODE rp0,srp0,nflist;
                   6947:     int nsp,nred,col,rank,len,k,j,a;
                   6948:     UINT c;
                   6949:     UINT **spmat;
                   6950:     UINT *s0vect,*svect,*p,*v;
                   6951:     int *colstat;
                   6952:     IndArray *imat;
                   6953:     int *rhead;
                   6954:     int spcol,sprow;
                   6955:     int sugar;
                   6956:     PGeoBucket bucket;
                   6957:     struct oEGT eg0,eg1,eg_f4;
                   6958:
                   6959:     g = 0; d = 0;
                   6960:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     6961:         d = update_pairs(d,g,i,0);
1.157     noro     6962:         g = update_base(g,i);
                   6963:     }
                   6964:     while ( d ) {
                   6965:         get_eg(&eg0);
                   6966:         l = nd_minsugarp(d,&d);
                   6967:         sugar = SG(l);
1.228     noro     6968:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
1.157     noro     6969:         bucket = create_pbucket();
                   6970:         stat = nd_sp_f4(m,0,l,bucket);
                   6971:         if ( !stat ) {
                   6972:             for ( t = l; NEXT(t); t = NEXT(t) );
                   6973:             NEXT(t) = d; d = l;
                   6974:             d = nd_reconstruct(1,d);
                   6975:             continue;
                   6976:         }
                   6977:         if ( bucket->m < 0 ) continue;
                   6978:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   6979:         if ( !col ) {
                   6980:             for ( t = l; NEXT(t); t = NEXT(t) );
                   6981:             NEXT(t) = d; d = l;
                   6982:             d = nd_reconstruct(1,d);
                   6983:             continue;
                   6984:         }
                   6985:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   6986:         if ( DP_Print )
1.241     noro     6987:             fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.157     noro     6988:                 sugar,eg_f4.exectime+eg_f4.gctime);
                   6989:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   6990:         if ( !l0 ) continue;
                   6991:         l = l0;
                   6992:
                   6993:         /* over Q */
                   6994:         bucket = create_pbucket();
                   6995:         stat = nd_sp_f4(0,1,l,bucket);
                   6996:         if ( !stat ) {
                   6997:             for ( t = l; NEXT(t); t = NEXT(t) );
                   6998:             NEXT(t) = d; d = l;
                   6999:             d = nd_reconstruct(1,d);
                   7000:             continue;
                   7001:         }
                   7002:         if ( bucket->m < 0 ) continue;
                   7003:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   7004:         if ( !col ) {
                   7005:             for ( t = l; NEXT(t); t = NEXT(t) );
                   7006:             NEXT(t) = d; d = l;
                   7007:             d = nd_reconstruct(1,d);
                   7008:             continue;
                   7009:         }
                   7010:         nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   7011:         /* adding new bases */
                   7012:         for ( r = nflist; r; r = NEXT(r) ) {
                   7013:             nfqv = (NDV)BDY(r);
                   7014:             ndv_removecont(0,nfqv);
                   7015:             if ( !rem(NM(HCQ(nfqv)),m) ) return 0;
                   7016:             if ( nd_nalg ) {
                   7017:                 ND nf1;
                   7018:
                   7019:                 nf1 = ndvtond(m,nfqv);
                   7020:                 nd_monic(0,&nf1);
                   7021:                 nd_removecont(0,nf1);
                   7022:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   7023:             }
                   7024:             nfv = ndv_dup(0,nfqv);
                   7025:             ndv_mod(m,nfv);
                   7026:             ndv_removecont(m,nfv);
1.215     noro     7027:             nh = ndv_newps(0,nfv,nfqv,1);
1.168     noro     7028:             d = update_pairs(d,g,nh,0);
1.157     noro     7029:             g = update_base(g,nh);
                   7030:         }
                   7031:     }
1.133     noro     7032: #if 0
1.157     noro     7033:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.133     noro     7034: #endif
1.245     noro     7035:   conv_ilist(nd_demand,1,g,indp);
1.157     noro     7036:     return g;
1.133     noro     7037: }
                   7038:
1.220     noro     7039: int rref(matrix mat,int *sugar)
                   7040: {
                   7041:   int row,col,i,j,k,l,s,wcol,wj;
                   7042:   unsigned long bj;
                   7043:   unsigned long **a;
                   7044:   unsigned long *ai,*ak,*as,*t;
                   7045:   int *pivot;
                   7046:
                   7047:   row = mat->row;
                   7048:   col = mat->col;
                   7049:   a = mat->a;
                   7050:   wcol = (col+BLEN-1)/BLEN;
                   7051:   pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   7052:   i = 0;
                   7053:   for ( j = 0; j < col; j++ ) {
1.245     noro     7054:   wj = j/BLEN; bj = 1L<<(j%BLEN);
1.220     noro     7055:     for ( k = i; k < row; k++ )
1.245     noro     7056:     if ( a[k][wj] & bj ) break;
1.220     noro     7057:     if ( k == row ) continue;
1.245     noro     7058:   pivot[i] = j;
1.220     noro     7059:     if ( k != i ) {
1.245     noro     7060:    t = a[i]; a[i] = a[k]; a[k] = t;
                   7061:    s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
                   7062:   }
                   7063:   ai = a[i];
1.220     noro     7064:     for ( k = i+1; k < row; k++ ) {
1.245     noro     7065:     ak = a[k];
                   7066:     if ( ak[wj] & bj ) {
                   7067:       for ( l = wj; l < wcol; l++ )
                   7068:       ak[l] ^= ai[l];
                   7069:       sugar[k] = MAX(sugar[k],sugar[i]);
                   7070:     }
                   7071:   }
                   7072:   i++;
1.220     noro     7073:   }
                   7074:   for ( k = i-1; k >= 0; k-- ) {
                   7075:     j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
1.245     noro     7076:   ak = a[k];
1.220     noro     7077:     for ( s = 0; s < k; s++ ) {
1.245     noro     7078:     as = a[s];
1.220     noro     7079:       if ( as[wj] & bj ) {
                   7080:         for ( l = wj; l < wcol; l++ )
1.245     noro     7081:       as[l] ^= ak[l];
                   7082:       sugar[s] = MAX(sugar[s],sugar[k]);
                   7083:     }
                   7084:   }
1.220     noro     7085:   }
                   7086:   return i;
                   7087: }
                   7088:
                   7089: void print_matrix(matrix mat)
                   7090: {
                   7091:   int row,col,i,j;
                   7092:   unsigned long *ai;
                   7093:
                   7094:   row = mat->row;
                   7095:   col = mat->col;
                   7096:   printf("%d x %d\n",row,col);
                   7097:   for ( i = 0; i < row; i++ ) {
1.245     noro     7098:   ai = mat->a[i];
1.220     noro     7099:     for ( j = 0; j < col; j++ ) {
1.245     noro     7100:     if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
                   7101:     else putchar('0');
                   7102:   }
                   7103:   putchar('\n');
1.220     noro     7104:   }
                   7105: }
                   7106:
                   7107: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
                   7108:
                   7109: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
                   7110: {
                   7111:   int row,col,wcol,wj,i,j;
                   7112:   unsigned long bj;
                   7113:   unsigned long *ai;
                   7114:   unsigned long **a;
                   7115:   int len;
                   7116:   int *pos;
                   7117:
                   7118:   row = mat->row;
                   7119:   col = mat->col;
                   7120:   wcol = (col+BLEN-1)/BLEN;
1.235     noro     7121:   pos = (int *)MALLOC(wcol*sizeof(int));
1.220     noro     7122:   bzero(pos,wcol*sizeof(int));
                   7123:   for ( i = j = 0; i < wcol; i++ )
                   7124:     if ( v[i] ) pos[j++] = i;;
                   7125:   len = j;
                   7126:   wj = rhead/BLEN;
                   7127:   bj = 1L<<rhead%BLEN;
                   7128:   a = mat->a;
                   7129:   for ( i = 0; i < row; i++ ) {
1.245     noro     7130:   ai = a[i];
1.220     noro     7131:     if ( ai[wj]&bj ) {
1.245     noro     7132:     for ( j = 0; j < len; j++ )
                   7133:       ai[pos[j]] ^= v[pos[j]];
                   7134:     sugar[i] = MAX(sugar[i],rsugar);
                   7135:   }
1.220     noro     7136:   }
                   7137: }
                   7138:
                   7139: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   7140: {
                   7141:     int nsp,nred,i,i0,k,rank,row;
                   7142:     NODE r0,rp;
                   7143:     ND_pairs sp;
1.245     noro     7144:   ND spol;
                   7145:   NM_ind_pair rt;
1.220     noro     7146:     int *s0hash;
1.245     noro     7147:   UINT *s;
                   7148:   int *pivot,*sugar,*head;
                   7149:   matrix mat;
1.220     noro     7150:     NM m;
                   7151:     NODE r;
1.245     noro     7152:   struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
                   7153:   int rhead,rsugar,size;
1.220     noro     7154:     unsigned long *v;
                   7155:
                   7156:     get_eg(&eg0);
                   7157: init_eg(&eg_search);
                   7158:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   7159:     nred = length(rp0);
                   7160:     mat = alloc_matrix(nsp,col);
1.235     noro     7161:     s0hash = (int *)MALLOC(col*sizeof(int));
1.220     noro     7162:     for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
                   7163:         s0hash[i] = ndl_hash_value(s);
                   7164:
1.245     noro     7165:   sugar = (int *)MALLOC(nsp*sizeof(int));
                   7166:   for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
                   7167:     nd_sp(2,0,sp,&spol);
                   7168:     if ( spol ) {
                   7169:         mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
                   7170:       sugar[i] = SG(spol);
                   7171:       i++;
                   7172:     }
                   7173:   }
                   7174:   mat->row = i;
1.227     noro     7175:     if ( DP_Print ) {
                   7176:       fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
                   7177:     }
1.245     noro     7178:   size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
                   7179:   v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
1.220     noro     7180:     for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
1.245     noro     7181:     rt = (NM_ind_pair)BDY(rp);
                   7182:     bzero(v,size);
1.220     noro     7183:         rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
1.245     noro     7184:     rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
                   7185:       red_by_vect_2(mat,sugar,v,rhead,rsugar);
                   7186:   }
1.220     noro     7187:
                   7188:     get_eg(&eg1);
                   7189:     init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
1.245     noro     7190:   rank = rref(mat,sugar);
1.220     noro     7191:
                   7192:     for ( i = 0, r0 = 0; i < rank; i++ ) {
                   7193:       NEXTNODE(r0,r);
1.245     noro     7194:     BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
1.220     noro     7195:       SG((NDV)BDY(r)) = sugar[i];
                   7196:     }
                   7197:     if ( r0 ) NEXT(r) = 0;
                   7198:     get_eg(&eg2);
                   7199:     init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
                   7200:     if ( DP_Print ) {
1.241     noro     7201:         fprintf(asir_out,"elim1=%.3fsec,elim2=%.3fsec,",
1.245     noro     7202:       eg_elim1.exectime+eg_elim1.gctime,eg_elim2.exectime+eg_elim2.gctime);
1.220     noro     7203:         fflush(asir_out);
1.245     noro     7204:   }
1.220     noro     7205:     return r0;
                   7206: }
                   7207:
                   7208:
1.133     noro     7209: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
1.63      noro     7210: {
1.157     noro     7211:     IndArray *imat;
                   7212:     int nsp,nred,i;
                   7213:     int *rhead;
                   7214:     NODE r0,rp;
                   7215:     ND_pairs sp;
                   7216:     NM_ind_pair *rvect;
1.210     noro     7217:     UINT *s;
                   7218:     int *s0hash;
                   7219:
1.220     noro     7220:     if ( m == 2 && nd_rref2 )
1.245     noro     7221:      return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
1.220     noro     7222:
1.198     noro     7223: init_eg(&eg_search);
1.157     noro     7224:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   7225:     nred = length(rp0);
1.235     noro     7226:     imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   7227:     rhead = (int *)MALLOC(col*sizeof(int));
1.157     noro     7228:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   7229:
                   7230:     /* construction of index arrays */
1.227     noro     7231:     if ( DP_Print ) {
1.245     noro     7232:     fprintf(asir_out,"%dx%d,",nsp+nred,col);
1.227     noro     7233:     }
1.235     noro     7234:     rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   7235:     s0hash = (int *)MALLOC(col*sizeof(int));
1.210     noro     7236:     for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
                   7237:         s0hash[i] = ndl_hash_value(s);
1.157     noro     7238:     for ( rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   7239:         rvect[i] = (NM_ind_pair)BDY(rp);
1.235     noro     7240:         imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,s0hash,rvect[i]);
1.157     noro     7241:         rhead[imat[i]->head] = 1;
                   7242:     }
1.240     noro     7243:     if ( m > 0 )
1.242     noro     7244: #if defined(__GNUC__) && SIZEOF_LONG==8
1.240     noro     7245:         r0 = nd_f4_red_mod64_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
                   7246: #else
                   7247:         r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
1.239     noro     7248: #endif
                   7249:     else if ( m == -1 )
                   7250:         r0 = nd_f4_red_sf_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
1.232     noro     7251:     else if ( m == -2 )
1.234     noro     7252:         r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
1.157     noro     7253:     else
1.237     noro     7254: #if defined(VISUAL)
                   7255:         r0 = nd_f4_red_q_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
                   7256: #else
1.215     noro     7257:         r0 = nd_f4_red_gz_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
1.237     noro     7258: #endif
1.241     noro     7259: #if 0
1.227     noro     7260:     if ( DP_Print ) print_eg("search",&eg_search);
1.241     noro     7261: #endif
1.157     noro     7262:     return r0;
1.106     noro     7263: }
1.74      noro     7264:
1.239     noro     7265: /* for Fp, 2<=p<2^16 */
                   7266:
1.106     noro     7267: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
1.133     noro     7268:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
1.106     noro     7269: {
1.157     noro     7270:     int spcol,sprow,a;
                   7271:     int i,j,k,l,rank;
                   7272:     NODE r0,r;
                   7273:     ND_pairs sp;
                   7274:     ND spol;
                   7275:     int **spmat;
                   7276:     UINT *svect,*v;
                   7277:     int *colstat;
                   7278:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   7279:     int maxrs;
                   7280:     int *spsugar;
                   7281:     ND_pairs *spactive;
                   7282:
                   7283:     spcol = col-nred;
                   7284:     get_eg(&eg0);
                   7285:     /* elimination (1st step) */
1.235     noro     7286:     spmat = (int **)MALLOC(nsp*sizeof(UINT *));
                   7287:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   7288:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   7289:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
1.157     noro     7290:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   7291:         nd_sp(m,0,sp,&spol);
                   7292:         if ( !spol ) continue;
                   7293:         nd_to_vect(m,s0vect,col,spol,svect);
                   7294:         if ( m == -1 )
                   7295:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   7296:         else
                   7297:             maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
                   7298:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   7299:         if ( i < col ) {
                   7300:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   7301:             for ( j = k = 0; j < col; j++ )
                   7302:                 if ( !rhead[j] ) v[k++] = svect[j];
                   7303:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   7304:             if ( nz )
                   7305:             spactive[sprow] = sp;
                   7306:             sprow++;
                   7307:         }
                   7308:         nd_free(spol);
                   7309:     }
                   7310:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   7311:     if ( DP_Print ) {
1.241     noro     7312:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
1.157     noro     7313:         fflush(asir_out);
                   7314:     }
                   7315:     /* free index arrays */
1.200     noro     7316:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
1.157     noro     7317:
                   7318:     /* elimination (2nd step) */
1.235     noro     7319:     colstat = (int *)MALLOC(spcol*sizeof(int));
1.157     noro     7320:     if ( m == -1 )
                   7321:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   7322:     else
                   7323:         rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   7324:     r0 = 0;
                   7325:     for ( i = 0; i < rank; i++ ) {
                   7326:         NEXTNODE(r0,r); BDY(r) =
                   7327:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   7328:         SG((NDV)BDY(r)) = spsugar[i];
1.200     noro     7329:         GCFREE(spmat[i]);
1.157     noro     7330:     }
                   7331:     if ( r0 ) NEXT(r) = 0;
                   7332:
1.200     noro     7333:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.157     noro     7334:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   7335:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   7336:     if ( DP_Print ) {
1.241     noro     7337:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
                   7338:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
1.157     noro     7339:             nsp,nred,sprow,spcol,rank);
1.241     noro     7340:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
1.157     noro     7341:     }
                   7342:     if ( nz ) {
                   7343:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   7344:         if ( rank > 0 ) {
                   7345:             NEXT(spactive[rank-1]) = 0;
                   7346:             *nz = spactive[0];
                   7347:         } else
                   7348:             *nz = 0;
                   7349:     }
                   7350:     return r0;
1.74      noro     7351: }
                   7352:
1.242     noro     7353: #if defined(__GNUC__) && SIZEOF_LONG==8
1.239     noro     7354: /* for Fp, 2^15=<p<2^29 */
                   7355:
1.240     noro     7356: NODE nd_f4_red_mod64_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
1.239     noro     7357:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   7358: {
                   7359:     int spcol,sprow,a;
                   7360:     int i,j,k,l,rank;
                   7361:     NODE r0,r;
                   7362:     ND_pairs sp;
                   7363:     ND spol;
1.240     noro     7364:     U64 **spmat;
                   7365:     U64 *svect,*cvect;
                   7366:     U64 *v;
1.239     noro     7367:     int *colstat;
                   7368:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   7369:     int maxrs;
                   7370:     int *spsugar;
                   7371:     ND_pairs *spactive;
                   7372:
                   7373:     spcol = col-nred;
                   7374:     get_eg(&eg0);
                   7375:     /* elimination (1st step) */
1.240     noro     7376:     spmat = (U64 **)MALLOC(nsp*sizeof(U64 *));
                   7377:     svect = (U64 *)MALLOC(col*sizeof(U64));
                   7378:     cvect = (U64 *)MALLOC(col*sizeof(U64));
1.239     noro     7379:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   7380:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   7381:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   7382:         nd_sp(m,0,sp,&spol);
                   7383:         if ( !spol ) continue;
1.240     noro     7384:         nd_to_vect64(m,s0vect,col,spol,svect);
                   7385:         maxrs = ndv_reduce_vect64(m,svect,cvect,col,imat,rvect,nred);
1.239     noro     7386:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   7387:         if ( i < col ) {
1.240     noro     7388:             spmat[sprow] = v = (U64 *)MALLOC_ATOMIC(spcol*sizeof(U64));
1.239     noro     7389:             for ( j = k = 0; j < col; j++ )
                   7390:                 if ( !rhead[j] ) v[k++] = (UINT)svect[j];
                   7391:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   7392:             if ( nz )
                   7393:             spactive[sprow] = sp;
                   7394:             sprow++;
                   7395:         }
                   7396:         nd_free(spol);
                   7397:     }
                   7398:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   7399:     if ( DP_Print ) {
1.241     noro     7400:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
1.239     noro     7401:         fflush(asir_out);
                   7402:     }
                   7403:     /* free index arrays */
                   7404:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   7405:
                   7406:     /* elimination (2nd step) */
                   7407:     colstat = (int *)MALLOC(spcol*sizeof(int));
1.240     noro     7408:     rank = nd_gauss_elim_mod64(spmat,spsugar,spactive,sprow,spcol,m,colstat);
1.239     noro     7409:     r0 = 0;
                   7410:     for ( i = 0; i < rank; i++ ) {
                   7411:         NEXTNODE(r0,r); BDY(r) =
1.240     noro     7412:           (pointer)vect64_to_ndv(spmat[i],spcol,col,rhead,s0vect);
1.239     noro     7413:         SG((NDV)BDY(r)) = spsugar[i];
                   7414:         GCFREE(spmat[i]);
                   7415:     }
                   7416:     if ( r0 ) NEXT(r) = 0;
                   7417:
                   7418:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   7419:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   7420:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   7421:     if ( DP_Print ) {
1.241     noro     7422:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
                   7423:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
1.239     noro     7424:             nsp,nred,sprow,spcol,rank);
1.241     noro     7425:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
1.239     noro     7426:     }
                   7427:     if ( nz ) {
                   7428:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   7429:         if ( rank > 0 ) {
                   7430:             NEXT(spactive[rank-1]) = 0;
                   7431:             *nz = spactive[0];
                   7432:         } else
                   7433:             *nz = 0;
                   7434:     }
                   7435:     return r0;
                   7436: }
                   7437: #endif
                   7438:
                   7439: /* for small finite fields */
                   7440:
                   7441: NODE nd_f4_red_sf_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   7442:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
                   7443: {
                   7444:     int spcol,sprow,a;
                   7445:     int i,j,k,l,rank;
                   7446:     NODE r0,r;
                   7447:     ND_pairs sp;
                   7448:     ND spol;
                   7449:     int **spmat;
                   7450:     UINT *svect,*v;
                   7451:     int *colstat;
                   7452:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   7453:     int maxrs;
                   7454:     int *spsugar;
                   7455:     ND_pairs *spactive;
                   7456:
                   7457:     spcol = col-nred;
                   7458:     get_eg(&eg0);
                   7459:     /* elimination (1st step) */
                   7460:     spmat = (int **)MALLOC(nsp*sizeof(UINT *));
                   7461:     svect = (UINT *)MALLOC(col*sizeof(UINT));
                   7462:     spsugar = (int *)MALLOC(nsp*sizeof(int));
                   7463:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   7464:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   7465:         nd_sp(m,0,sp,&spol);
                   7466:         if ( !spol ) continue;
                   7467:         nd_to_vect(m,s0vect,col,spol,svect);
                   7468:         maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   7469:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   7470:         if ( i < col ) {
                   7471:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   7472:             for ( j = k = 0; j < col; j++ )
                   7473:                 if ( !rhead[j] ) v[k++] = svect[j];
                   7474:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   7475:             if ( nz )
                   7476:             spactive[sprow] = sp;
                   7477:             sprow++;
                   7478:         }
                   7479:         nd_free(spol);
                   7480:     }
                   7481:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   7482:     if ( DP_Print ) {
1.241     noro     7483:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
1.239     noro     7484:         fflush(asir_out);
                   7485:     }
                   7486:     /* free index arrays */
                   7487:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
                   7488:
                   7489:     /* elimination (2nd step) */
                   7490:     colstat = (int *)MALLOC(spcol*sizeof(int));
                   7491:     rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   7492:     r0 = 0;
                   7493:     for ( i = 0; i < rank; i++ ) {
                   7494:         NEXTNODE(r0,r); BDY(r) =
                   7495:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   7496:         SG((NDV)BDY(r)) = spsugar[i];
                   7497:         GCFREE(spmat[i]);
                   7498:     }
                   7499:     if ( r0 ) NEXT(r) = 0;
                   7500:
                   7501:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
                   7502:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   7503:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   7504:     if ( DP_Print ) {
1.241     noro     7505:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
                   7506:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
1.239     noro     7507:             nsp,nred,sprow,spcol,rank);
1.241     noro     7508:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
1.239     noro     7509:     }
                   7510:     if ( nz ) {
                   7511:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   7512:         if ( rank > 0 ) {
                   7513:             NEXT(spactive[rank-1]) = 0;
                   7514:             *nz = spactive[0];
                   7515:         } else
                   7516:             *nz = 0;
                   7517:     }
                   7518:     return r0;
                   7519: }
                   7520:
1.234     noro     7521: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
1.232     noro     7522:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   7523: {
                   7524:     int spcol,sprow,a;
                   7525:     int i,j,k,l,rank;
                   7526:     NODE r0,r;
                   7527:     ND_pairs sp;
                   7528:     ND spol;
                   7529:     mpz_t **spmat;
                   7530:     mpz_t *svect,*v;
                   7531:     int *colstat;
                   7532:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   7533:     int maxrs;
                   7534:     int *spsugar;
                   7535:     pointer *w;
                   7536:
                   7537:     spcol = col-nred;
                   7538:     get_eg(&eg0);
                   7539:     /* elimination (1st step) */
1.235     noro     7540:     spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
                   7541:     svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
                   7542:     spsugar = (int *)MALLOC(nsp*sizeof(int));
1.232     noro     7543:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
1.234     noro     7544:         nd_sp(m,trace,sp,&spol);
1.232     noro     7545:         if ( !spol ) continue;
                   7546:         nd_to_vect_lf(s0vect,col,spol,svect);
1.234     noro     7547:         maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
1.232     noro     7548:         for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
                   7549:         if ( i < col ) {
                   7550:             spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
                   7551:             for ( j = k = 0; j < col; j++ )
                   7552:                 if ( !rhead[j] ) v[k++][0] = svect[j][0];
                   7553:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   7554:             sprow++;
                   7555:         }
                   7556: /*        nd_free(spol); */
                   7557:     }
                   7558:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   7559:     if ( DP_Print ) {
1.241     noro     7560:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
1.232     noro     7561:         fflush(asir_out);
                   7562:     }
                   7563:     /* free index arrays */
                   7564: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   7565:
                   7566:     /* elimination (2nd step) */
1.235     noro     7567:     colstat = (int *)MALLOC(spcol*sizeof(int));
1.232     noro     7568:     rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
1.235     noro     7569:     w = (pointer *)MALLOC(rank*sizeof(pointer));
1.232     noro     7570:     for ( i = 0; i < rank; i++ ) {
                   7571: #if 0
                   7572:         w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   7573:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   7574: #else
                   7575:         w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   7576:         SG((NDV)w[i]) = spsugar[i];
                   7577: #endif
                   7578: /*        GCFREE(spmat[i]); */
1.245     noro     7579:
1.232     noro     7580:     }
                   7581: #if 0
                   7582:     qsort(w,rank,sizeof(NDV),
                   7583:         (int (*)(const void *,const void *))ndv_compare);
                   7584: #endif
                   7585:     r0 = 0;
                   7586:     for ( i = 0; i < rank; i++ ) {
                   7587:         NEXTNODE(r0,r); BDY(r) = w[i];
                   7588:     }
                   7589:     if ( r0 ) NEXT(r) = 0;
                   7590:
                   7591: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   7592:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   7593:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   7594:     if ( DP_Print ) {
1.241     noro     7595:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
                   7596:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
1.232     noro     7597:             nsp,nred,sprow,spcol,rank);
1.241     noro     7598:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
1.232     noro     7599:     }
                   7600:     return r0;
                   7601: }
                   7602:
1.133     noro     7603: #if 1
                   7604: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
1.107     noro     7605:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   7606: {
1.157     noro     7607:     int spcol,sprow,a;
                   7608:     int i,j,k,l,rank;
                   7609:     NODE r0,r;
                   7610:     ND_pairs sp;
                   7611:     ND spol;
                   7612:     Q **spmat;
                   7613:     Q *svect,*v;
                   7614:     int *colstat;
                   7615:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   7616:     int maxrs;
                   7617:     int *spsugar;
                   7618:     pointer *w;
                   7619:
                   7620:     spcol = col-nred;
                   7621:     get_eg(&eg0);
                   7622:     /* elimination (1st step) */
1.235     noro     7623:     spmat = (Q **)MALLOC(nsp*sizeof(Q *));
                   7624:     svect = (Q *)MALLOC(col*sizeof(Q));
                   7625:     spsugar = (int *)MALLOC(nsp*sizeof(int));
1.157     noro     7626:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   7627:         nd_sp(0,trace,sp,&spol);
                   7628:         if ( !spol ) continue;
                   7629:         nd_to_vect_q(s0vect,col,spol,svect);
                   7630:         maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
                   7631:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   7632:         if ( i < col ) {
                   7633:             spmat[sprow] = v = (Q *)MALLOC(spcol*sizeof(Q));
                   7634:             for ( j = k = 0; j < col; j++ )
                   7635:                 if ( !rhead[j] ) v[k++] = svect[j];
                   7636:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   7637:             sprow++;
                   7638:         }
                   7639: /*        nd_free(spol); */
                   7640:     }
                   7641:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   7642:     if ( DP_Print ) {
1.241     noro     7643:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
1.157     noro     7644:         fflush(asir_out);
                   7645:     }
                   7646:     /* free index arrays */
1.200     noro     7647: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
1.157     noro     7648:
                   7649:     /* elimination (2nd step) */
1.235     noro     7650:     colstat = (int *)MALLOC(spcol*sizeof(int));
1.157     noro     7651:     rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
1.235     noro     7652:     w = (pointer *)MALLOC(rank*sizeof(pointer));
1.157     noro     7653:     for ( i = 0; i < rank; i++ ) {
1.213     noro     7654: #if 0
1.157     noro     7655:         w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   7656:         SG((NDV)w[rank-i-1]) = spsugar[i];
1.213     noro     7657: #else
                   7658:         w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   7659:         SG((NDV)w[i]) = spsugar[i];
                   7660: #endif
1.200     noro     7661: /*        GCFREE(spmat[i]); */
1.157     noro     7662:     }
1.138     noro     7663: #if 0
1.157     noro     7664:     qsort(w,rank,sizeof(NDV),
                   7665:         (int (*)(const void *,const void *))ndv_compare);
1.137     noro     7666: #endif
1.157     noro     7667:     r0 = 0;
                   7668:     for ( i = 0; i < rank; i++ ) {
                   7669:         NEXTNODE(r0,r); BDY(r) = w[i];
                   7670:     }
                   7671:     if ( r0 ) NEXT(r) = 0;
                   7672:
1.200     noro     7673: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
1.157     noro     7674:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   7675:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   7676:     if ( DP_Print ) {
1.241     noro     7677:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
                   7678:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
1.157     noro     7679:             nsp,nred,sprow,spcol,rank);
1.241     noro     7680:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
1.157     noro     7681:     }
                   7682:     return r0;
1.107     noro     7683: }
1.215     noro     7684:
                   7685: NODE nd_f4_red_gz_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   7686:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   7687: {
                   7688:     int spcol,sprow,a;
                   7689:     int i,j,k,l,rank;
                   7690:     NODE r0,r;
                   7691:     ND_pairs sp;
                   7692:     ND spol;
                   7693:     GZ **spmat;
                   7694:     GZ *svect,*v;
                   7695:     int *colstat;
                   7696:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   7697:     int maxrs;
                   7698:     int *spsugar;
                   7699:     pointer *w;
                   7700:
                   7701:     spcol = col-nred;
                   7702:     get_eg(&eg0);
                   7703:     /* elimination (1st step) */
1.235     noro     7704:     spmat = (GZ **)MALLOC(nsp*sizeof(GZ *));
                   7705:     svect = (GZ *)MALLOC(col*sizeof(GZ));
                   7706:     spsugar = (int *)MALLOC(nsp*sizeof(int));
1.215     noro     7707:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   7708:         nd_sp(0,trace,sp,&spol);
                   7709:         if ( !spol ) continue;
1.245     noro     7710:     spol = ndtondgz(spol);
1.215     noro     7711:         nd_to_vect_q(s0vect,col,spol,(Q *)svect);
                   7712:         maxrs = ndv_reduce_vect_gz(svect,trace,col,imat,rvect,nred);
                   7713:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   7714:         if ( i < col ) {
                   7715:             spmat[sprow] = v = (GZ *)MALLOC(spcol*sizeof(GZ));
                   7716:             for ( j = k = 0; j < col; j++ )
                   7717:                 if ( !rhead[j] ) v[k++] = svect[j];
                   7718:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   7719:             sprow++;
                   7720:         }
                   7721: /*        nd_free(spol); */
                   7722:     }
                   7723:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   7724:     if ( DP_Print ) {
1.241     noro     7725:         fprintf(asir_out,"elim1=%.3fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
1.215     noro     7726:         fflush(asir_out);
                   7727:     }
                   7728:     /* free index arrays */
                   7729: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   7730:
                   7731:     /* elimination (2nd step) */
1.235     noro     7732:     colstat = (int *)MALLOC(spcol*sizeof(int));
1.215     noro     7733:     rank = nd_gauss_elim_gz(spmat,spsugar,sprow,spcol,colstat);
1.235     noro     7734:     w = (pointer *)MALLOC(rank*sizeof(pointer));
1.215     noro     7735:     for ( i = 0; i < rank; i++ ) {
                   7736: #if 0
                   7737:         w[rank-i-1] = (pointer)vect_to_ndv_gz(spmat[i],spcol,col,rhead,s0vect);
1.245     noro     7738:     w[rank-i-1] = ndvgztondv(w[rank-i-1]);
1.215     noro     7739:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   7740: #else
1.232     noro     7741:         w[i] = (pointer)vect_to_ndv_gz(spmat[i],spcol,col,rhead,s0vect);
1.245     noro     7742:     w[i] = ndvgztondv(w[i]);
1.215     noro     7743:         SG((NDV)w[i]) = spsugar[i];
                   7744: #endif
                   7745: /*        GCFREE(spmat[i]); */
1.245     noro     7746:
1.215     noro     7747:     }
                   7748: #if 0
                   7749:     qsort(w,rank,sizeof(NDV),
                   7750:         (int (*)(const void *,const void *))ndv_compare);
                   7751: #endif
                   7752:     r0 = 0;
                   7753:     for ( i = 0; i < rank; i++ ) {
                   7754:         NEXTNODE(r0,r); BDY(r) = w[i];
                   7755:     }
                   7756:     if ( r0 ) NEXT(r) = 0;
                   7757:
                   7758: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   7759:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   7760:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   7761:     if ( DP_Print ) {
1.241     noro     7762:         fprintf(asir_out,"elim2=%.3fsec,",eg_f4_2.exectime+eg_f4_2.gctime);
                   7763:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
1.215     noro     7764:             nsp,nred,sprow,spcol,rank);
1.241     noro     7765:         fprintf(asir_out,"%.3fsec,",eg_f4.exectime+eg_f4.gctime);
1.215     noro     7766:     }
                   7767:     return r0;
                   7768: }
1.129     noro     7769: #else
                   7770: void printm(Q **mat,int row,int col)
                   7771: {
1.157     noro     7772:     int i,j;
                   7773:     printf("[");
                   7774:     for ( i = 0; i < row; i++ ) {
                   7775:         for ( j = 0; j < col; j++ ) {
                   7776:             printexpr(CO,mat[i][j]); printf(" ");
                   7777:         }
                   7778:         printf("]\n");
                   7779:     }
1.129     noro     7780: }
                   7781:
                   7782: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   7783:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   7784: {
1.157     noro     7785:     int row,a;
                   7786:     int i,j,rank;
                   7787:     NODE r0,r;
                   7788:     ND_pairs sp;
                   7789:     ND spol;
                   7790:     Q **mat;
                   7791:     int *colstat;
                   7792:     int *sugar;
                   7793:
                   7794:     row = nsp+nred;
                   7795:     /* make the matrix */
1.235     noro     7796:     mat = (Q **)MALLOC(row*sizeof(Q *));
                   7797:     sugar = (int *)MALLOC(row*sizeof(int));
1.157     noro     7798:     for ( row = a = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   7799:         nd_sp(0,0,sp,&spol);
                   7800:         if ( !spol ) continue;
                   7801:         mat[row] = (Q *)MALLOC(col*sizeof(Q));
                   7802:         nd_to_vect_q(s0vect,col,spol,mat[row]);
                   7803:         sugar[row] = SG(spol);
                   7804:         row++;
                   7805:     }
                   7806:     for ( i = 0; i < nred; i++, row++ ) {
                   7807:         mat[row] = nm_ind_pair_to_vect(0,s0vect,col,rvect[i]);
                   7808:         sugar[row] = rvect[i]->sugar;
                   7809:     }
                   7810:     /* elimination */
1.235     noro     7811:     colstat = (int *)MALLOC(col*sizeof(int));
1.157     noro     7812:     rank = nd_gauss_elim_q(mat,sugar,row,col,colstat);
                   7813:     r0 = 0;
                   7814:     for ( i = 0; i < rank; i++ ) {
                   7815:         for ( j = 0; j < col; j++ ) if ( mat[i][j] ) break;
                   7816:         if ( j == col ) error("nd_f4_red_q_main : cannot happen");
                   7817:         if ( rhead[j] ) continue;
                   7818:         NEXTNODE(r0,r); BDY(r) =
                   7819:             (pointer)plain_vect_to_ndv_q(mat[i],col,s0vect);
                   7820:         SG((NDV)BDY(r)) = sugar[i];
                   7821:     }
                   7822:     if ( r0 ) NEXT(r) = 0;
                   7823:     printf("\n");
                   7824:     return r0;
1.129     noro     7825: }
                   7826: #endif
1.107     noro     7827:
1.74      noro     7828: FILE *nd_write,*nd_read;
                   7829:
                   7830: void nd_send_int(int a) {
1.157     noro     7831:     write_int(nd_write,&a);
1.74      noro     7832: }
                   7833:
                   7834: void nd_send_intarray(int *p,int len) {
1.157     noro     7835:     write_intarray(nd_write,p,len);
1.74      noro     7836: }
                   7837:
                   7838: int nd_recv_int() {
1.157     noro     7839:     int a;
1.74      noro     7840:
1.157     noro     7841:     read_int(nd_read,&a);
                   7842:     return a;
1.74      noro     7843: }
                   7844:
                   7845: void nd_recv_intarray(int *p,int len) {
1.157     noro     7846:     read_intarray(nd_read,p,len);
1.74      noro     7847: }
                   7848:
                   7849: void nd_send_ndv(NDV p) {
1.157     noro     7850:     int len,i;
                   7851:     NMV m;
1.74      noro     7852:
1.157     noro     7853:     if ( !p ) nd_send_int(0);
                   7854:     else {
                   7855:         len = LEN(p);
                   7856:         nd_send_int(len);
                   7857:         m = BDY(p);
                   7858:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   7859:             nd_send_int(CM(m));
                   7860:             nd_send_intarray(DL(m),nd_wpd);
                   7861:         }
                   7862:     }
1.74      noro     7863: }
                   7864:
                   7865: void nd_send_nd(ND p) {
1.157     noro     7866:     int len,i;
                   7867:     NM m;
1.74      noro     7868:
1.157     noro     7869:     if ( !p ) nd_send_int(0);
                   7870:     else {
                   7871:         len = LEN(p);
                   7872:         nd_send_int(len);
                   7873:         m = BDY(p);
                   7874:         for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   7875:             nd_send_int(CM(m));
                   7876:             nd_send_intarray(DL(m),nd_wpd);
                   7877:         }
                   7878:     }
1.74      noro     7879: }
1.65      noro     7880:
1.74      noro     7881: NDV nd_recv_ndv()
                   7882: {
1.157     noro     7883:     int len,i;
                   7884:     NMV m,m0;
                   7885:     NDV r;
                   7886:
                   7887:     len = nd_recv_int();
                   7888:     if ( !len ) return 0;
                   7889:     else {
1.200     noro     7890:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
1.103     noro     7891: #if 0
1.157     noro     7892:         ndv_alloc += len*nmv_adv;
1.103     noro     7893: #endif
1.157     noro     7894:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   7895:             CM(m) = nd_recv_int();
                   7896:             nd_recv_intarray(DL(m),nd_wpd);
                   7897:         }
                   7898:         MKNDV(nd_nvar,m0,len,r);
                   7899:         return r;
                   7900:     }
1.74      noro     7901: }
1.65      noro     7902:
1.113     noro     7903: int nd_gauss_elim_q(Q **mat0,int *sugar,int row,int col,int *colstat)
1.107     noro     7904: {
1.176     noro     7905:     int i,j,t,c,rank,inv;
1.157     noro     7906:     int *ci,*ri;
                   7907:     Q dn;
                   7908:     MAT m,nm;
                   7909:
                   7910:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
                   7911:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   7912:     for ( i = 0; i < row; i++ )
                   7913:         for ( j = 0; j < col; j++ )
                   7914:             mat0[i][j] = 0;
                   7915:     c = col-rank;
                   7916:     for ( i = 0; i < rank; i++ ) {
                   7917:         mat0[i][ri[i]] = dn;
                   7918:         for ( j = 0; j < c; j++ )
                   7919:             mat0[i][ci[j]] = (Q)BDY(nm)[i][j];
                   7920:     }
                   7921:     return rank;
1.76      noro     7922: }
                   7923:
1.215     noro     7924: int nd_gauss_elim_gz(GZ **mat0,int *sugar,int row,int col,int *colstat)
                   7925: {
                   7926:     int i,j,t,c,rank,inv;
                   7927:     int *ci,*ri;
1.216     noro     7928:     GZ dn;
1.215     noro     7929:     MAT m,nm;
                   7930:
                   7931:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
1.216     noro     7932:     rank = gz_generic_gauss_elim(m,&nm,&dn,&ri,&ci);
1.215     noro     7933:     for ( i = 0; i < row; i++ )
                   7934:         for ( j = 0; j < col; j++ )
                   7935:             mat0[i][j] = 0;
                   7936:     c = col-rank;
                   7937:     for ( i = 0; i < rank; i++ ) {
1.216     noro     7938:         mat0[i][ri[i]] = dn;
1.215     noro     7939:         for ( j = 0; j < c; j++ )
                   7940:             mat0[i][ci[j]] = (GZ)BDY(nm)[i][j];
                   7941:     }
                   7942:     return rank;
                   7943: }
                   7944:
1.133     noro     7945: int nd_gauss_elim_mod(int **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
1.76      noro     7946: {
1.157     noro     7947:     int i,j,k,l,inv,a,rank,s;
                   7948:     unsigned int *t,*pivot,*pk;
                   7949:     unsigned int **mat;
                   7950:     ND_pairs pair;
                   7951:
                   7952:     mat = (unsigned int **)mat0;
                   7953:     for ( rank = 0, j = 0; j < col; j++ ) {
                   7954:         for ( i = rank; i < row; i++ )
                   7955:             mat[i][j] %= md;
                   7956:         for ( i = rank; i < row; i++ )
                   7957:             if ( mat[i][j] )
                   7958:                 break;
                   7959:         if ( i == row ) {
                   7960:             colstat[j] = 0;
                   7961:             continue;
                   7962:         } else
                   7963:             colstat[j] = 1;
                   7964:         if ( i != rank ) {
                   7965:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   7966:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   7967:             if ( spactive ) {
                   7968:                 pair = spactive[i]; spactive[i] = spactive[rank];
                   7969:                 spactive[rank] = pair;
                   7970:             }
                   7971:         }
                   7972:         pivot = mat[rank];
                   7973:         s = sugar[rank];
                   7974:         inv = invm(pivot[j],md);
                   7975:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   7976:             if ( *pk ) {
                   7977:                 if ( *pk >= (unsigned int)md )
                   7978:                     *pk %= md;
                   7979:                 DMAR(*pk,inv,0,md,*pk)
                   7980:             }
                   7981:         for ( i = rank+1; i < row; i++ ) {
                   7982:             t = mat[i];
                   7983:             if ( a = t[j] ) {
                   7984:                 sugar[i] = MAX(sugar[i],s);
                   7985:                 red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   7986:             }
                   7987:         }
                   7988:         rank++;
                   7989:     }
                   7990:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   7991:         if ( colstat[j] ) {
                   7992:             pivot = mat[l];
                   7993:             s = sugar[l];
                   7994:             for ( i = 0; i < l; i++ ) {
                   7995:                 t = mat[i];
                   7996:                 t[j] %= md;
                   7997:                 if ( a = t[j] ) {
                   7998:                     sugar[i] = MAX(sugar[i],s);
                   7999:                     red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   8000:                 }
                   8001:             }
                   8002:             l--;
                   8003:         }
                   8004:     for ( j = 0, l = 0; l < rank; j++ )
                   8005:         if ( colstat[j] ) {
                   8006:             t = mat[l];
                   8007:             for ( k = j; k < col; k++ )
                   8008:                 if ( t[k] >= (unsigned int)md )
                   8009:                     t[k] %= md;
                   8010:             l++;
                   8011:         }
                   8012:     return rank;
1.76      noro     8013: }
                   8014:
1.242     noro     8015: #if defined(__GNUC__) && SIZEOF_LONG==8
1.240     noro     8016:
                   8017: int nd_gauss_elim_mod64(U64 **mat,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
1.239     noro     8018: {
1.240     noro     8019:   int i,j,k,l,rank,s;
                   8020:   U64 inv;
                   8021:   U64 a;
                   8022:   UINT c;
                   8023:   U64 *t,*pivot,*pk;
                   8024:   UINT *ck;
                   8025:   UINT **cmat;
                   8026:   UINT *ct;
                   8027:   ND_pairs pair;
                   8028:
                   8029:   cmat = (UINT **)MALLOC(row*sizeof(UINT *));
                   8030:   for ( i = 0; i < row; i++ ) {
                   8031:     cmat[i] = MALLOC_ATOMIC(col*sizeof(UINT));
                   8032:     bzero(cmat[i],col*sizeof(UINT));
                   8033:   }
1.239     noro     8034:
1.240     noro     8035:   for ( rank = 0, j = 0; j < col; j++ ) {
                   8036:     for ( i = rank; i < row; i++ ) {
                   8037:       a = mat[i][j]; c = cmat[i][j];
                   8038:       MOD128(a,c,md);
                   8039:       mat[i][j] = a; cmat[i][j] = 0;
                   8040:     }
                   8041:     for ( i = rank; i < row; i++ )
                   8042:       if ( mat[i][j] )
                   8043:         break;
                   8044:     if ( i == row ) {
                   8045:       colstat[j] = 0;
                   8046:       continue;
                   8047:     } else
                   8048:       colstat[j] = 1;
                   8049:     if ( i != rank ) {
                   8050:       t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   8051:       ct = cmat[i]; cmat[i] = cmat[rank]; cmat[rank] = ct;
                   8052:       s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   8053:       if ( spactive ) {
                   8054:         pair = spactive[i]; spactive[i] = spactive[rank];
                   8055:         spactive[rank] = pair;
                   8056:       }
                   8057:     }
                   8058:     /* column j is normalized */
                   8059:     s = sugar[rank];
                   8060:     inv = invm((UINT)mat[rank][j],md);
                   8061:     /* normalize pivot row */
                   8062:     for ( k = j, pk = mat[rank]+j, ck = cmat[rank]+j; k < col; k++, pk++, ck++ ) {
                   8063:       a = *pk; c = *ck; MOD128(a,c,md); *pk = (a*inv)%md; *ck = 0;
                   8064:     }
                   8065:     for ( i = rank+1; i < row; i++ ) {
                   8066:       if ( a = mat[i][j] ) {
                   8067:         sugar[i] = MAX(sugar[i],s);
                   8068:         red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[rank]+j,(int)(md-a),col-j);
                   8069:       }
1.239     noro     8070:     }
1.240     noro     8071:     rank++;
                   8072:   }
                   8073:   for ( j = col-1, l = rank-1; j >= 0; j-- )
                   8074:     if ( colstat[j] ) {
                   8075:       for ( k = j, pk = mat[l]+j, ck = cmat[l]+j; k < col; k++, pk++, ck++ ) {
                   8076:         a = *pk; c = *ck; MOD128(a,c,md); *pk = a; *ck = 0;
                   8077:       }
                   8078:       s = sugar[l];
                   8079:       for ( i = 0; i < l; i++ ) {
                   8080:         a = mat[i][j]; c = cmat[i][j]; MOD128(a,c,md); mat[i][j] = a; cmat[i][j] = 0;
                   8081:         if ( a ) {
                   8082:           sugar[i] = MAX(sugar[i],s);
                   8083:           red_by_vect64(md,mat[i]+j,cmat[i]+j,mat[l]+j,(int)(md-a),col-j);
                   8084:         }
                   8085:       }
                   8086:       l--;
                   8087:     }
                   8088:   for ( i = 0; i < row; i++ ) GCFREE(cmat[i]);
                   8089:   GCFREE(cmat);
                   8090:   return rank;
1.239     noro     8091: }
                   8092: #endif
                   8093:
1.76      noro     8094: int nd_gauss_elim_sf(int **mat0,int *sugar,int row,int col,int md,int *colstat)
                   8095: {
1.157     noro     8096:     int i,j,k,l,inv,a,rank,s;
                   8097:     unsigned int *t,*pivot,*pk;
                   8098:     unsigned int **mat;
                   8099:
                   8100:     mat = (unsigned int **)mat0;
                   8101:     for ( rank = 0, j = 0; j < col; j++ ) {
                   8102:         for ( i = rank; i < row; i++ )
                   8103:             if ( mat[i][j] )
                   8104:                 break;
                   8105:         if ( i == row ) {
                   8106:             colstat[j] = 0;
                   8107:             continue;
                   8108:         } else
                   8109:             colstat[j] = 1;
                   8110:         if ( i != rank ) {
                   8111:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   8112:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   8113:         }
                   8114:         pivot = mat[rank];
                   8115:         s = sugar[rank];
                   8116:         inv = _invsf(pivot[j]);
                   8117:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   8118:             if ( *pk )
                   8119:                 *pk = _mulsf(*pk,inv);
                   8120:         for ( i = rank+1; i < row; i++ ) {
                   8121:             t = mat[i];
                   8122:             if ( a = t[j] ) {
                   8123:                 sugar[i] = MAX(sugar[i],s);
                   8124:                 red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   8125:             }
                   8126:         }
                   8127:         rank++;
                   8128:     }
                   8129:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   8130:         if ( colstat[j] ) {
                   8131:             pivot = mat[l];
                   8132:             s = sugar[l];
                   8133:             for ( i = 0; i < l; i++ ) {
                   8134:                 t = mat[i];
                   8135:                 if ( a = t[j] ) {
                   8136:                     sugar[i] = MAX(sugar[i],s);
                   8137:                     red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   8138:                 }
                   8139:             }
                   8140:             l--;
                   8141:         }
                   8142:     return rank;
1.77      noro     8143: }
                   8144:
                   8145: int ndv_ishomo(NDV p)
                   8146: {
1.157     noro     8147:     NMV m;
                   8148:     int len,h;
1.77      noro     8149:
1.157     noro     8150:     if ( !p ) return 1;
                   8151:     len = LEN(p);
                   8152:     m = BDY(p);
                   8153:     h = TD(DL(m));
                   8154:     NMV_ADV(m);
                   8155:     for ( len--; len; len--, NMV_ADV(m) )
                   8156:         if ( TD(DL(m)) != h ) return 0;
                   8157:     return 1;
1.77      noro     8158: }
                   8159:
                   8160: void ndv_save(NDV p,int index)
                   8161: {
1.157     noro     8162:     FILE *s;
                   8163:     char name[BUFSIZ];
                   8164:     short id;
                   8165:     int nv,sugar,len,n,i,td,e,j;
                   8166:     NMV m;
                   8167:     unsigned int *dl;
                   8168:     int mpos;
                   8169:
                   8170:     sprintf(name,"%s/%d",Demand,index);
                   8171:     s = fopen(name,"w");
                   8172:     savevl(s,0);
                   8173:     if ( !p ) {
                   8174:         saveobj(s,0);
                   8175:         return;
                   8176:     }
                   8177:     id = O_DP;
                   8178:     nv = NV(p);
                   8179:     sugar = SG(p);
                   8180:     len = LEN(p);
                   8181:     write_short(s,&id); write_int(s,&nv); write_int(s,&sugar);
                   8182:     write_int(s,&len);
                   8183:
                   8184:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   8185:         saveobj(s,(Obj)CQ(m));
                   8186:         dl = DL(m);
                   8187:         td = TD(dl);
                   8188:         write_int(s,&td);
                   8189:         for ( j = 0; j < nv; j++ ) {
                   8190:             e = GET_EXP(dl,j);
                   8191:             write_int(s,&e);
                   8192:         }
                   8193:         if ( nd_module ) {
                   8194:             mpos = MPOS(dl); write_int(s,&mpos);
                   8195:         }
                   8196:     }
                   8197:     fclose(s);
1.77      noro     8198: }
                   8199:
1.206     noro     8200: void nd_save_mod(ND p,int index)
                   8201: {
                   8202:     FILE *s;
                   8203:     char name[BUFSIZ];
                   8204:     int nv,sugar,len,c;
                   8205:     NM m;
                   8206:
                   8207:     sprintf(name,"%s/%d",Demand,index);
                   8208:     s = fopen(name,"w");
                   8209:     if ( !p ) {
1.245     noro     8210:     len = 0;
                   8211:       write_int(s,&len);
                   8212:     fclose(s);
1.206     noro     8213:         return;
                   8214:     }
                   8215:     nv = NV(p);
                   8216:     sugar = SG(p);
                   8217:     len = LEN(p);
                   8218:     write_int(s,&nv); write_int(s,&sugar); write_int(s,&len);
1.245     noro     8219:   for ( m = BDY(p); m; m = NEXT(m) ) {
                   8220:     c = CM(m); write_int(s,&c);
                   8221:     write_intarray(s,DL(m),nd_wpd);
                   8222:   }
1.206     noro     8223:     fclose(s);
                   8224: }
                   8225:
1.77      noro     8226: NDV ndv_load(int index)
                   8227: {
1.157     noro     8228:     FILE *s;
                   8229:     char name[BUFSIZ];
                   8230:     short id;
                   8231:     int nv,sugar,len,n,i,td,e,j;
                   8232:     NDV d;
                   8233:     NMV m0,m;
                   8234:     unsigned int *dl;
                   8235:     Obj obj;
                   8236:     int mpos;
                   8237:
                   8238:     sprintf(name,"%s/%d",Demand,index);
                   8239:     s = fopen(name,"r");
                   8240:     if ( !s ) return 0;
                   8241:
                   8242:     skipvl(s);
                   8243:     read_short(s,&id);
                   8244:     if ( !id ) return 0;
                   8245:     read_int(s,&nv);
                   8246:     read_int(s,&sugar);
                   8247:     read_int(s,&len);
                   8248:
                   8249:     m0 = m = MALLOC(len*nmv_adv);
                   8250:     for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   8251:         loadobj(s,&obj); CQ(m) = (Q)obj;
                   8252:         dl = DL(m);
                   8253:         ndl_zero(dl);
                   8254:         read_int(s,&td); TD(dl) = td;
                   8255:         for ( j = 0; j < nv; j++ ) {
                   8256:             read_int(s,&e);
                   8257:             PUT_EXP(dl,j,e);
                   8258:         }
                   8259:         if ( nd_module ) {
                   8260:             read_int(s,&mpos); MPOS(dl) = mpos;
                   8261:         }
                   8262:         if ( nd_blockmask ) ndl_weight_mask(dl);
                   8263:     }
                   8264:     fclose(s);
                   8265:     MKNDV(nv,m0,len,d);
                   8266:     SG(d) = sugar;
                   8267:     return d;
1.99      noro     8268: }
                   8269:
1.206     noro     8270: ND nd_load_mod(int index)
                   8271: {
                   8272:     FILE *s;
                   8273:     char name[BUFSIZ];
                   8274:     int nv,sugar,len,i,c;
1.245     noro     8275:   ND d;
1.206     noro     8276:     NM m0,m;
                   8277:
                   8278:     sprintf(name,"%s/%d",Demand,index);
                   8279:     s = fopen(name,"r");
1.245     noro     8280:   /* if the file does not exist, it means p[index]=0 */
1.206     noro     8281:     if ( !s ) return 0;
                   8282:
                   8283:     read_int(s,&nv);
1.245     noro     8284:   if ( !nv ) { fclose(s); return 0; }
1.206     noro     8285:
                   8286:     read_int(s,&sugar);
                   8287:     read_int(s,&len);
1.245     noro     8288:   for ( m0 = 0, i = 0; i < len; i++ ) {
                   8289:     NEXTNM(m0,m);
                   8290:     read_int(s,&c); CM(m) = c;
                   8291:     read_intarray(s,DL(m),nd_wpd);
                   8292:   }
                   8293:   NEXT(m) = 0;
1.206     noro     8294:     MKND(nv,m0,len,d);
                   8295:     SG(d) = sugar;
1.245     noro     8296:   fclose(s);
1.206     noro     8297:     return d;
                   8298: }
                   8299:
1.102     noro     8300: void nd_det(int mod,MAT f,P *rp)
1.99      noro     8301: {
1.157     noro     8302:     VL fv,tv;
                   8303:     int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
                   8304:     pointer **m;
                   8305:     Q mone;
                   8306:     P **w;
                   8307:     P mp,r;
                   8308:     NDV **dm;
                   8309:     NDV *t,*mi,*mj;
                   8310:     NDV d,s,mij,mjj;
                   8311:     ND u;
                   8312:     NMV nmv;
                   8313:     UINT *bound;
                   8314:     PGeoBucket bucket;
                   8315:     struct order_spec *ord;
                   8316:     Q dq,dt,ds;
                   8317:     N gn,qn,dn0,nm,dn;
                   8318:
                   8319:     create_order_spec(0,0,&ord);
                   8320:     nd_init_ord(ord);
                   8321:     get_vars((Obj)f,&fv);
                   8322:     if ( f->row != f->col )
                   8323:         error("nd_det : non-square matrix");
                   8324:     n = f->row;
                   8325:     m = f->body;
                   8326:     for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   8327:
                   8328:     if ( !nvar ) {
                   8329:         if ( !mod )
                   8330:             detp(CO,(P **)m,n,rp);
                   8331:         else {
                   8332:             w = (P **)almat_pointer(n,n);
                   8333:             for ( i = 0; i < n; i++ )
                   8334:                 for ( j = 0; j < n; j++ )
                   8335:                     ptomp(mod,(P)m[i][j],&w[i][j]);
                   8336:             detmp(CO,mod,w,n,&mp);
                   8337:             mptop(mp,rp);
                   8338:         }
                   8339:         return;
                   8340:     }
                   8341:
                   8342:     if ( !mod ) {
                   8343:         w = (P **)almat_pointer(n,n);
                   8344:         dq = ONE;
                   8345:         for ( i = 0; i < n; i++ ) {
                   8346:             dn0 = ONEN;
                   8347:             for ( j = 0; j < n; j++ ) {
                   8348:                 if ( !m[i][j] ) continue;
                   8349:                 lgp(m[i][j],&nm,&dn);
                   8350:                 gcdn(dn0,dn,&gn); divsn(dn0,gn,&qn); muln(qn,dn,&dn0);
                   8351:             }
                   8352:             if ( !UNIN(dn0) ) {
                   8353:                 NTOQ(dn0,1,ds);
                   8354:                 for ( j = 0; j < n; j++ )
                   8355:                     mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   8356:                 mulq(dq,ds,&dt); dq = dt;
                   8357:             } else
                   8358:                 for ( j = 0; j < n; j++ )
                   8359:                     w[i][j] = (P)m[i][j];
                   8360:         }
                   8361:         m = (pointer **)w;
                   8362:     }
                   8363:
1.178     noro     8364:     for ( i = 0, max = 1; i < n; i++ )
1.157     noro     8365:         for ( j = 0; j < n; j++ )
                   8366:             for ( tv = fv; tv; tv = NEXT(tv) ) {
                   8367:                 e = getdeg(tv->v,(P)m[i][j]);
                   8368:                 max = MAX(e,max);
                   8369:             }
                   8370:     nd_setup_parameters(nvar,max);
                   8371:     dm = (NDV **)almat_pointer(n,n);
1.178     noro     8372:     for ( i = 0, max = 1; i < n; i++ )
1.157     noro     8373:         for ( j = 0; j < n; j++ ) {
                   8374:             dm[i][j] = ptondv(CO,fv,m[i][j]);
                   8375:             if ( mod ) ndv_mod(mod,dm[i][j]);
                   8376:             if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   8377:         }
                   8378:     d = ptondv(CO,fv,(P)ONE);
                   8379:     if ( mod ) ndv_mod(mod,d);
                   8380:     chsgnq(ONE,&mone);
                   8381:     for ( j = 0, sgn = 1; j < n; j++ ) {
1.222     fujimoto 8382:         if ( DP_Print ) {
1.245     noro     8383:     fprintf(asir_out,".",j);
                   8384:   }
1.157     noro     8385:         for ( i = j; i < n && !dm[i][j]; i++ );
                   8386:         if ( i == n ) {
                   8387:             *rp = 0;
                   8388:             return;
                   8389:         }
                   8390:         k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
                   8391:         for ( k = j; k < n; k++ )
                   8392:             for ( l = j; l < n; l++ )
                   8393:                 if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   8394:                     k0 = k; l0 = l; len0 = LEN(dm[k][l]);
                   8395:                 }
                   8396:         if ( k0 != j ) {
                   8397:             t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   8398:             sgn = -sgn;
                   8399:         }
                   8400:         if ( l0 != j ) {
                   8401:             for ( k = j; k < n; k++ ) {
                   8402:                 s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   8403:             }
                   8404:             sgn = -sgn;
                   8405:         }
                   8406:         bound = nd_det_compute_bound(dm,n,j);
                   8407:         for ( k = 0; k < nd_nvar; k++ )
                   8408:             if ( bound[k]*2 > nd_mask0 ) break;
                   8409:         if ( k < nd_nvar )
                   8410:             nd_det_reconstruct(dm,n,j,d);
                   8411:
                   8412:         for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
1.241     noro     8413: /*            if ( DP_Print ) fprintf(asir_out,"    i=%d\n        ",i); */
1.157     noro     8414:             mi = dm[i]; mij = mi[j];
                   8415:             if ( mod )
                   8416:                 ndv_mul_c(mod,mij,mod-1);
                   8417:             else
                   8418:                 ndv_mul_c_q(mij,mone);
                   8419:             for ( k = j+1; k < n; k++ ) {
1.241     noro     8420: /*                if ( DP_Print ) fprintf(asir_out,"k=%d ",k); */
1.157     noro     8421:                 bucket = create_pbucket();
                   8422:                 if ( mi[k] ) {
                   8423:                     nmv = BDY(mjj); len = LEN(mjj);
                   8424:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   8425:                         u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   8426:                         add_pbucket(mod,bucket,u);
                   8427:                     }
                   8428:                 }
                   8429:                 if ( mj[k] && mij ) {
                   8430:                     nmv = BDY(mij); len = LEN(mij);
                   8431:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   8432:                         u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   8433:                         add_pbucket(mod,bucket,u);
                   8434:                     }
                   8435:                 }
                   8436:                 u = nd_quo(mod,bucket,d);
                   8437:                 mi[k] = ndtondv(mod,u);
                   8438:             }
1.241     noro     8439: /*            if ( DP_Print ) fprintf(asir_out,"\n",k); */
1.157     noro     8440:         }
                   8441:         d = mjj;
                   8442:     }
1.222     fujimoto 8443:     if ( DP_Print ) {
1.241     noro     8444:       fprintf(asir_out,"\n",k);
1.222     fujimoto 8445:     }
1.157     noro     8446:     if ( sgn < 0 )
                   8447:         if ( mod )
                   8448:             ndv_mul_c(mod,d,mod-1);
                   8449:         else
                   8450:             ndv_mul_c_q(d,mone);
                   8451:     r = ndvtop(mod,CO,fv,d);
                   8452:     if ( !mod && !UNIQ(dq) )
                   8453:         divsp(CO,r,(P)dq,rp);
                   8454:     else
                   8455:         *rp = r;
1.99      noro     8456: }
                   8457:
1.102     noro     8458: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
1.99      noro     8459: {
1.157     noro     8460:     NM mr,mr0;
                   8461:     NM tnm;
                   8462:     NMV m;
                   8463:     UINT *d0,*dt,*dm;
                   8464:     int c,n,td,i,c1,c2,len;
                   8465:     Q q;
                   8466:     ND r;
                   8467:
                   8468:     if ( !p ) return 0;
                   8469:     else {
                   8470:         n = NV(p); m = BDY(p); len = LEN(p);
                   8471:         d0 = DL(m0);
                   8472:         td = TD(d);
                   8473:         mr0 = 0;
                   8474:         NEWNM(tnm);
                   8475:         if ( mod ) {
                   8476:             c = CM(m0);
                   8477:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   8478:                 ndl_add(DL(m),d0,DL(tnm));
                   8479:                 if ( ndl_reducible(DL(tnm),d) ) {
                   8480:                     NEXTNM(mr0,mr);
                   8481:                     c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
                   8482:                     ndl_copy(DL(tnm),DL(mr));
                   8483:                 }
                   8484:             }
                   8485:         } else {
                   8486:             q = CQ(m0);
                   8487:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   8488:                 ndl_add(DL(m),d0,DL(tnm));
                   8489:                 if ( ndl_reducible(DL(tnm),d) ) {
                   8490:                     NEXTNM(mr0,mr);
                   8491:                     mulq(CQ(m),q,&CQ(mr));
                   8492:                     ndl_copy(DL(tnm),DL(mr));
                   8493:                 }
                   8494:             }
                   8495:         }
                   8496:         if ( !mr0 )
                   8497:             return 0;
                   8498:         else {
                   8499:             NEXT(mr) = 0;
                   8500:             for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   8501:             MKND(NV(p),mr0,len,r);
                   8502:             SG(r) = SG(p) + TD(d0);
                   8503:             return r;
                   8504:         }
                   8505:     }
1.114     noro     8506: }
                   8507:
                   8508: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   8509: {
1.157     noro     8510:     int i,obpe,oadv,h,k,l;
                   8511:     static NM prev_nm_free_list;
                   8512:     EPOS oepos;
                   8513:
                   8514:     obpe = nd_bpe;
                   8515:     oadv = nmv_adv;
                   8516:     oepos = nd_epos;
                   8517:     if ( obpe < 2 ) nd_bpe = 2;
                   8518:     else if ( obpe < 3 ) nd_bpe = 3;
                   8519:     else if ( obpe < 4 ) nd_bpe = 4;
                   8520:     else if ( obpe < 5 ) nd_bpe = 5;
                   8521:     else if ( obpe < 6 ) nd_bpe = 6;
                   8522:     else if ( obpe < 8 ) nd_bpe = 8;
                   8523:     else if ( obpe < 10 ) nd_bpe = 10;
                   8524:     else if ( obpe < 16 ) nd_bpe = 16;
                   8525:     else if ( obpe < 32 ) nd_bpe = 32;
                   8526:     else error("nd_det_reconstruct : exponent too large");
                   8527:
                   8528:     nd_setup_parameters(nd_nvar,0);
                   8529:     prev_nm_free_list = _nm_free_list;
                   8530:     _nm_free_list = 0;
                   8531:     for ( k = j; k < n; k++ )
                   8532:         for (l = j; l < n; l++ )
                   8533:             ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   8534:     ndv_realloc(d,obpe,oadv,oepos);
                   8535:     prev_nm_free_list = 0;
1.114     noro     8536: #if 0
1.157     noro     8537:     GC_gcollect();
1.114     noro     8538: #endif
                   8539: }
                   8540:
1.153     noro     8541: /* returns a UINT array containing degree bounds */
                   8542:
1.114     noro     8543: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   8544: {
1.157     noro     8545:     UINT *d0,*d1,*d,*t,*r;
                   8546:     int k,l,i;
1.114     noro     8547:
1.157     noro     8548:     d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
                   8549:     for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
                   8550:     for ( k = j; k < n; k++ )
                   8551:         for ( l = j; l < n; l++ )
                   8552:             if ( dm[k][l] ) {
                   8553:                 d = ndv_compute_bound(dm[k][l]);
                   8554:                 for ( i = 0; i < nd_nvar; i++ )
                   8555:                     d0[i] = MAX(d0[i],d[i]);
                   8556:             }
                   8557:     return d0;
1.117     noro     8558: }
                   8559:
                   8560: DL nd_separate_d(UINT *d,UINT *trans)
                   8561: {
1.157     noro     8562:     int n,td,i,e,j;
                   8563:     DL a;
1.117     noro     8564:
1.157     noro     8565:     ndl_zero(trans);
                   8566:     td = 0;
                   8567:     for ( i = 0; i < nd_ntrans; i++ ) {
                   8568:         e = GET_EXP(d,i);
                   8569:         PUT_EXP(trans,i,e);
                   8570:         td += MUL_WEIGHT(e,i);
                   8571:     }
                   8572:     if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   8573:         /* homogenized */
                   8574:         i = nd_nvar-1;
                   8575:         e = GET_EXP(d,i);
                   8576:         PUT_EXP(trans,i,e);
                   8577:         td += MUL_WEIGHT(e,i);
                   8578:     }
                   8579:     TD(trans) = td;
                   8580:     if ( nd_blockmask) ndl_weight_mask(trans);
                   8581:     NEWDL(a,nd_nalg);
                   8582:     td = 0;
                   8583:     for ( i = 0; i < nd_nalg; i++ ) {
                   8584:         j = nd_ntrans+i;
                   8585:         e = GET_EXP(d,j);
                   8586:         a->d[i] = e;
                   8587:         td += e;
                   8588:     }
                   8589:     a->td = td;
                   8590:     return a;
1.117     noro     8591: }
                   8592:
1.118     noro     8593: int nd_monic(int mod,ND *p)
1.117     noro     8594: {
1.157     noro     8595:     UINT *trans,*t;
                   8596:     DL alg;
                   8597:     MP mp0,mp;
                   8598:     NM m,m0,m1,ma0,ma,mb,mr0,mr;
                   8599:     ND r;
                   8600:     DL dl;
                   8601:     DP nm;
                   8602:     NDV ndv;
                   8603:     DAlg inv,cd;
                   8604:     ND s,c;
                   8605:     Q l,mul;
                   8606:     N ln;
                   8607:     int n,ntrans,i,e,td,is_lc,len;
                   8608:     NumberField nf;
                   8609:     struct oEGT eg0,eg1;
                   8610:
                   8611:     if ( !(nf = get_numberfield()) )
                   8612:         error("nd_monic : current_numberfield is not set");
                   8613:
                   8614:     /* Q coef -> DAlg coef */
                   8615:     NEWNM(ma0); ma = ma0;
                   8616:     m = BDY(*p);
                   8617:     is_lc = 1;
                   8618:     while ( 1 ) {
                   8619:         NEWMP(mp0); mp = mp0;
1.238     noro     8620:         mp->c = (Obj)CQ(m);
1.157     noro     8621:         mp->dl = nd_separate_d(DL(m),DL(ma));
                   8622:         NEWNM(mb);
                   8623:         for ( m = NEXT(m); m; m = NEXT(m) ) {
                   8624:             alg = nd_separate_d(DL(m),DL(mb));
                   8625:             if ( !ndl_equal(DL(ma),DL(mb)) )
                   8626:                 break;
1.238     noro     8627:             NEXTMP(mp0,mp); mp->c = (Obj)CQ(m); mp->dl = alg;
1.157     noro     8628:         }
                   8629:         NEXT(mp) = 0;
                   8630:         MKDP(nd_nalg,mp0,nm);
                   8631:         MKDAlg(nm,ONE,cd);
                   8632:         if ( is_lc == 1 ) {
                   8633:             /* if the lc is a rational number, we have nothing to do */
                   8634:             if ( !mp0->dl->td )
                   8635:                 return 1;
                   8636:
                   8637:             get_eg(&eg0);
                   8638:             invdalg(cd,&inv);
                   8639:             get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   8640:             /* check the validity of inv */
                   8641:             if ( mod && !rem(NM(inv->dn),mod) )
                   8642:                 return 0;
                   8643:             CA(ma) = nf->one;
                   8644:             is_lc = 0;
                   8645:             ln = ONEN;
                   8646:         } else {
                   8647:             muldalg(cd,inv,&CA(ma));
                   8648:             lcmn(ln,NM(CA(ma)->dn),&ln);
                   8649:         }
                   8650:         if ( m ) {
                   8651:             NEXT(ma) = mb; ma = mb;
                   8652:         } else {
                   8653:             NEXT(ma) = 0;
                   8654:             break;
                   8655:         }
                   8656:     }
                   8657:     /* l = lcm(denoms) */
                   8658:     NTOQ(ln,1,l);
                   8659:     for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
                   8660:         divq(l,CA(m)->dn,&mul);
                   8661:         for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   8662:             NEXTNM(mr0,mr);
                   8663:             mulq((Q)mp->c,mul,&CQ(mr));
                   8664:             dl = mp->dl;
                   8665:             td = TD(DL(m));
                   8666:             ndl_copy(DL(m),DL(mr));
                   8667:             for ( i = 0; i < nd_nalg; i++ ) {
                   8668:                 e = dl->d[i];
                   8669:                 PUT_EXP(DL(mr),i+nd_ntrans,e);
                   8670:                 td += MUL_WEIGHT(e,i+nd_ntrans);
                   8671:             }
1.163     noro     8672:             if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
1.157     noro     8673:             TD(DL(mr)) = td;
                   8674:             if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   8675:         }
                   8676:     }
                   8677:     NEXT(mr) = 0;
                   8678:     for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   8679:     MKND(NV(*p),mr0,len,r);
                   8680:     /* XXX */
                   8681:     SG(r) = SG(*p);
                   8682:     nd_free(*p);
                   8683:     *p = r;
                   8684:     return 1;
1.59      noro     8685: }
1.167     noro     8686:
                   8687: NODE reverse_node(NODE n)
                   8688: {
                   8689:     NODE t,t1;
                   8690:
                   8691:     for ( t = 0; n; n = NEXT(n) ) {
                   8692:         MKNODE(t1,BDY(n),t); t = t1;
                   8693:     }
                   8694:     return t;
                   8695: }
                   8696:
                   8697: P ndc_div(int mod,union oNDC a,union oNDC b)
                   8698: {
                   8699:     union oNDC c;
                   8700:     int inv,t;
                   8701:
                   8702:     if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.232     noro     8703:     else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.167     noro     8704:     else if ( mod ) {
                   8705:         inv = invm(b.m,mod);
                   8706:         DMAR(a.m,inv,0,mod,t); c.m = t;
                   8707:     } else if ( nd_vc )
                   8708:        divsp(nd_vc,a.p,b.p,&c.p);
                   8709:     else
                   8710:        divq(a.z,b.z,&c.z);
                   8711:     return ndctop(mod,c);
                   8712: }
                   8713:
                   8714: P ndctop(int mod,union oNDC c)
                   8715: {
                   8716:     Q q;
                   8717:     int e;
                   8718:     GFS gfs;
                   8719:
                   8720:     if ( mod == -1 ) {
                   8721:         e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
1.247     noro     8722:     } else if ( mod == -2 ) {
                   8723:        q = gztoz(c.gz); return (P)q;
1.232     noro     8724:     } else if ( mod > 0 ) {
1.167     noro     8725:         STOQ(c.m,q); return (P)q;
                   8726:     } else
                   8727:         return (P)c.p;
                   8728: }
                   8729:
                   8730: /* [0,0,0,cont] = p -> p/cont */
                   8731:
                   8732: void finalize_tracelist(int i,P cont)
                   8733: {
1.245     noro     8734:    LIST l;
                   8735:    NODE node;
1.167     noro     8736:      Q iq;
                   8737:
1.245     noro     8738:    if ( !UNIQ(cont) ) {
1.196     noro     8739:          node = mknode(4,NULLP,NULLP,NULLP,cont);
1.167     noro     8740:          MKLIST(l,node); MKNODE(node,l,nd_tracelist);
1.245     noro     8741:      nd_tracelist = node;
                   8742:    }
1.167     noro     8743:      STOQ(i,iq);
                   8744:      nd_tracelist = reverse_node(nd_tracelist);
                   8745:      MKLIST(l,nd_tracelist);
                   8746:      node = mknode(2,iq,l); MKLIST(l,node);
                   8747:      MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
                   8748:      nd_alltracelist = node; nd_tracelist = 0;
                   8749: }
                   8750:
                   8751: void conv_ilist(int demand,int trace,NODE g,int **indp)
                   8752: {
                   8753:     int n,i,j;
1.245     noro     8754:   int *ind;
                   8755:   NODE t;
1.167     noro     8756:
                   8757:     n = length(g);
1.245     noro     8758:   ind = (int *)MALLOC(n*sizeof(int));
                   8759:   for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
                   8760:     j = (long)BDY(t); ind[i] = j;
                   8761:     BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
                   8762:   }
                   8763:   if ( indp ) *indp = ind;
1.167     noro     8764: }
1.172     noro     8765:
                   8766: void parse_nd_option(NODE opt)
                   8767: {
1.187     noro     8768:     NODE t,p,u;
1.245     noro     8769:   int i,s,n;
1.172     noro     8770:     char *key;
                   8771:     Obj value;
                   8772:
1.187     noro     8773:     nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
1.245     noro     8774:   nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
                   8775:   nd_splist = 0; nd_check_splist = 0;
1.231     noro     8776:     nd_sugarweight = 0;
1.234     noro     8777:     nd_f4red =0;
                   8778:     nd_rank0 = 0;
1.172     noro     8779:     for ( t = opt; t; t = NEXT(t) ) {
                   8780:         p = BDY((LIST)BDY(t));
                   8781:         key = BDY((STRING)BDY(p));
                   8782:         value = (Obj)BDY(NEXT(p));
                   8783:         if ( !strcmp(key,"gentrace") )
                   8784:             nd_gentrace = value?1:0;
                   8785:         else if ( !strcmp(key,"gensyz") )
                   8786:             nd_gensyz = value?1:0;
1.173     noro     8787:         else if ( !strcmp(key,"nora") )
                   8788:             nd_nora = value?1:0;
1.187     noro     8789:         else if ( !strcmp(key,"gbblock") ) {
1.233     noro     8790:             if ( value && OID(value) == O_LIST ) {
1.245     noro     8791:         u = BDY((LIST)value);
1.233     noro     8792:               nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
1.245     noro     8793:         for ( i = 0; u; u = NEXT(u) ) {
                   8794:           p = BDY((LIST)BDY(u));
                   8795:           s = nd_gbblock[i++] = QTOS((Q)BDY(p));
                   8796:           nd_gbblock[i++] = s+QTOS((Q)BDY(NEXT(p)))-1;
                   8797:         }
                   8798:         nd_gbblock[i] = -1;
1.233     noro     8799:             } else
                   8800:               nd_gbblock = 0;
1.245     noro     8801:     } else if ( !strcmp(key,"newelim") )
1.192     noro     8802:             nd_newelim = value?1:0;
1.245     noro     8803:     else if ( !strcmp(key,"intersect") )
1.195     noro     8804:             nd_intersect = value?1:0;
1.245     noro     8805:     else if ( !strcmp(key,"lf") )
1.234     noro     8806:             nd_lf = value?1:0;
1.245     noro     8807:     else if ( !strcmp(key,"trace") ) {
1.244     noro     8808:            if ( value ) {
                   8809:                u = BDY((LIST)value);
1.245     noro     8810:            nd_nzlist = BDY((LIST)ARG2(u));
                   8811:            nd_bpe = QTOS((Q)ARG3(u));
1.244     noro     8812:            }
1.245     noro     8813:     } else if ( !strcmp(key,"f4red") ) {
                   8814:        nd_f4red = QTOS((Q)value);
                   8815:     } else if ( !strcmp(key,"rank0") ) {
1.234     noro     8816:             nd_rank0 = value?1:0;
1.245     noro     8817:     } else if ( !strcmp(key,"splist") ) {
1.209     noro     8818:             nd_splist = value?1:0;
1.245     noro     8819:     } else if ( !strcmp(key,"check_splist") ) {
                   8820:       nd_check_splist = BDY((LIST)value);
                   8821:     } else if ( !strcmp(key,"sugarweight") ) {
                   8822:       u = BDY((LIST)value);
1.231     noro     8823:             n = length(u);
                   8824:             nd_sugarweight = MALLOC(n*sizeof(int));
1.245     noro     8825:       for ( i = 0; i < n; i++, u = NEXT(u) )
1.231     noro     8826:                 nd_sugarweight[i] = QTOS((Q)BDY(u));
1.245     noro     8827:     }
1.172     noro     8828:     }
                   8829: }
1.204     noro     8830:
                   8831: ND mdptond(DP d);
                   8832: ND nd_mul_nm(int mod,NM m0,ND p);
1.247     noro     8833: ND nd_mul_nm_lf(NM m0,ND p);
1.207     noro     8834: ND *btog(NODE ti,ND **p,int nb,int mod);
                   8835: ND btog_one(NODE ti,ND *p,int nb,int mod);
1.204     noro     8836: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
1.205     noro     8837: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
1.204     noro     8838:
                   8839: /* d:monomial */
                   8840: ND mdptond(DP d)
                   8841: {
                   8842:   NM m;
                   8843:   ND r;
                   8844:
                   8845:   if ( OID(d) == 1 )
1.245     noro     8846:   r = ptond(CO,CO,(P)d);
1.204     noro     8847:   else {
                   8848:     NEWNM(m);
                   8849:     dltondl(NV(d),BDY(d)->dl,DL(m));
                   8850:     CQ(m) = (Q)BDY(d)->c;
                   8851:     NEXT(m) = 0;
                   8852:     MKND(NV(d),m,1,r);
                   8853:   }
                   8854:     return r;
                   8855: }
                   8856:
                   8857: ND nd_mul_nm(int mod,NM m0,ND p)
                   8858: {
                   8859:   UINT *d0;
                   8860:   int c0,c1,c;
                   8861:   NM tm,mr,mr0;
                   8862:   ND r;
                   8863:
                   8864:   if ( !p ) return 0;
                   8865:   d0 = DL(m0);
                   8866:   c0 = CM(m0);
                   8867:   mr0 = 0;
                   8868:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   8869:     NEXTNM(mr0,mr);
1.245     noro     8870:   c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
                   8871:   ndl_add(d0,DL(tm),DL(mr));
1.204     noro     8872:   }
                   8873:   NEXT(mr) = 0;
                   8874:   MKND(NV(p),mr0,LEN(p),r);
                   8875:   return r;
                   8876: }
                   8877:
1.247     noro     8878: ND nd_mul_nm_lf(NM m0,ND p)
                   8879: {
                   8880:   UINT *d0;
                   8881:   GZ c0,c1,c;
                   8882:   NM tm,mr,mr0;
                   8883:   ND r;
                   8884:
                   8885:   if ( !p ) return 0;
                   8886:   d0 = DL(m0);
                   8887:   c0 = CZ(m0);
                   8888:   mr0 = 0;
                   8889:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   8890:     NEXTNM(mr0,mr);
                   8891:     c = CZ(tm); mullf(c0,CZ(tm),&c1); CZ(mr) = c1;
                   8892:     ndl_add(d0,DL(tm),DL(mr));
                   8893:   }
                   8894:   NEXT(mr) = 0;
                   8895:   MKND(NV(p),mr0,LEN(p),r);
                   8896:   return r;
                   8897: }
                   8898:
1.207     noro     8899: ND *btog(NODE ti,ND **p,int nb,int mod)
1.204     noro     8900: {
                   8901:   PGeoBucket *r;
                   8902:   int i,ci;
                   8903:   NODE t,s;
                   8904:   ND m,tp;
                   8905:   ND *pi,*rd;
                   8906:   P c;
                   8907:
                   8908:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   8909:   for ( i = 0; i < nb; i++ )
1.245     noro     8910:     r[i] = create_pbucket();
1.204     noro     8911:   for ( t = ti; t; t = NEXT(t) ) {
1.245     noro     8912:   s = BDY((LIST)BDY(t));
1.204     noro     8913:     if ( ARG0(s) ) {
1.245     noro     8914:     m = mdptond((DP)ARG2(s));
                   8915:     ptomp(mod,(P)HCQ(m),&c);
                   8916:     if ( ci = ((MQ)c)->cont ) {
                   8917:       HCM(m) = ci;
                   8918:       pi = p[QTOS((Q)ARG1(s))];
                   8919:       for ( i = 0; i < nb; i++ ) {
                   8920:       tp = nd_mul_nm(mod,BDY(m),pi[i]);
                   8921:         add_pbucket(mod,r[i],tp);
                   8922:       }
                   8923:     }
                   8924:     ci = 1;
1.204     noro     8925:     } else {
1.245     noro     8926:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   8927:     ci = invm(ci,mod);
                   8928:   }
1.204     noro     8929:   }
                   8930:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   8931:   for ( i = 0; i < nb; i++ )
1.245     noro     8932:   rd[i] = normalize_pbucket(mod,r[i]);
1.204     noro     8933:   if ( ci != 1 )
                   8934:     for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
                   8935:    return rd;
                   8936: }
                   8937:
1.247     noro     8938: /* YYY */
                   8939: ND *btog_lf(NODE ti,ND **p,int nb)
                   8940: {
                   8941:   PGeoBucket *r;
                   8942:   int i;
                   8943:   NODE t,s;
                   8944:   ND m,tp;
                   8945:   ND *pi,*rd;
                   8946:   LM lm;
                   8947:   GZ lf,c;
                   8948:
                   8949:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   8950:   for ( i = 0; i < nb; i++ )
                   8951:     r[i] = create_pbucket();
                   8952:   for ( t = ti; t; t = NEXT(t) ) {
                   8953:     s = BDY((LIST)BDY(t));
                   8954:     if ( ARG0(s) ) {
                   8955:       m = mdptond((DP)ARG2(s));
                   8956:       simp_ff((Obj)HCQ(m),&lm);
                   8957:       if ( lm ) {
                   8958:         lmtolf(lm,&lf); HCZ(m) = lf;
                   8959:         pi = p[QTOS((Q)ARG1(s))];
                   8960:         for ( i = 0; i < nb; i++ ) {
                   8961:           tp = nd_mul_nm_lf(BDY(m),pi[i]);
                   8962:           add_pbucket(-2,r[i],tp);
                   8963:         }
                   8964:       }
                   8965:       c = ONEGZ;
                   8966:     } else {
                   8967:       simp_ff((Obj)ARG3(s),&lm); lmtolf(lm,&lf); invgz(lf,&c);
                   8968:     }
                   8969:   }
                   8970:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   8971:   for ( i = 0; i < nb; i++ )
                   8972:     rd[i] = normalize_pbucket(-2,r[i]);
                   8973:   for ( i = 0; i < nb; i++ ) nd_mul_c_lf(rd[i],c);
                   8974:   return rd;
                   8975: }
                   8976:
1.207     noro     8977: ND btog_one(NODE ti,ND *p,int nb,int mod)
1.205     noro     8978: {
                   8979:   PGeoBucket r;
1.206     noro     8980:   int i,ci,j;
1.205     noro     8981:   NODE t,s;
                   8982:   ND m,tp;
                   8983:   ND pi,rd;
                   8984:   P c;
                   8985:
                   8986:   r = create_pbucket();
                   8987:   for ( t = ti; t; t = NEXT(t) ) {
1.245     noro     8988:   s = BDY((LIST)BDY(t));
1.205     noro     8989:     if ( ARG0(s) ) {
1.245     noro     8990:     m = mdptond((DP)ARG2(s));
                   8991:     ptomp(mod,(P)HCQ(m),&c);
                   8992:     if ( ci = ((MQ)c)->cont ) {
                   8993:       HCM(m) = ci;
                   8994:       pi = p[j=QTOS((Q)ARG1(s))];
                   8995:     if ( !pi ) {
                   8996:       pi = nd_load_mod(j);
                   8997:       tp = nd_mul_nm(mod,BDY(m),pi);
                   8998:       nd_free(pi);
                   8999:         add_pbucket(mod,r,tp);
1.205     noro     9000:     } else {
1.245     noro     9001:       tp = nd_mul_nm(mod,BDY(m),pi);
                   9002:         add_pbucket(mod,r,tp);
                   9003:       }
                   9004:     }
                   9005:     ci = 1;
                   9006:     } else {
                   9007:     ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   9008:     ci = invm(ci,mod);
                   9009:   }
1.205     noro     9010:   }
                   9011:   rd = normalize_pbucket(mod,r);
1.206     noro     9012:   free_pbucket(r);
1.205     noro     9013:   if ( ci != 1 ) nd_mul_c(mod,rd,ci);
                   9014:   return rd;
                   9015: }
1.204     noro     9016:
1.247     noro     9017: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp);
                   9018:
1.204     noro     9019: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
                   9020: {
                   9021:   int i,j,n,m,nb,pi0,pi1,nvar;
                   9022:   VL fv,tv,vv;
                   9023:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   9024:   ND **p;
                   9025:   ND *c;
                   9026:   ND u;
                   9027:   P inv;
                   9028:   MAT mat;
                   9029:
1.247     noro     9030:   if ( mod == -2 )
                   9031:     return nd_btog_lf(f,v,ord,tlist,rp);
                   9032:
1.204     noro     9033:   parse_nd_option(current_option);
                   9034:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   9035:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   9036:   switch ( ord->id ) {
                   9037:     case 1:
                   9038:       if ( ord->nv != nvar )
                   9039:         error("nd_check : invalid order specification");
                   9040:       break;
                   9041:     default:
                   9042:       break;
                   9043:   }
                   9044:   nd_init_ord(ord);
                   9045: #if 0
                   9046:   nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
                   9047: #else
                   9048:   nd_bpe = 32;
                   9049: #endif
                   9050:   nd_setup_parameters(nvar,0);
                   9051:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   9052:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   9053:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   9054:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   9055:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.245     noro     9056:     j = QTOS((Q)BDY(BDY((LIST)BDY(t))));
                   9057:   if ( j > i ) i = j;
1.204     noro     9058:   }
                   9059:   n = i+1;
                   9060:   nb = length(BDY(f));
                   9061:   p = (ND **)MALLOC(n*sizeof(ND *));
                   9062:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   9063:     pi = BDY((LIST)BDY(t));
1.247     noro     9064:     pi0 = QTOS((Q)ARG0(pi)); pi1 = QTOS((Q)ARG1(pi));
                   9065:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   9066:     ptomp(mod,(P)ARG2(pi),&inv);
                   9067:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   9068:     u = ptond(CO,vv,(P)ONE);
                   9069:     HCM(u) = ((MQ)inv)->cont;
                   9070:     c[pi1] = u;
1.204     noro     9071:   }
                   9072:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
1.247     noro     9073:     printf("%d ",i); fflush(stdout);
1.204     noro     9074:     ti = BDY((LIST)BDY(t));
1.207     noro     9075:     p[j=QTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.204     noro     9076:   }
                   9077:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
1.247     noro     9078:     printf("%d ",i); fflush(stdout);
1.204     noro     9079:     ti = BDY((LIST)BDY(t));
1.207     noro     9080:     p[j=QTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.204     noro     9081:   }
                   9082:   m = length(ind);
                   9083:   MKMAT(mat,nb,m);
                   9084:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
                   9085:     for ( i = 0, c = p[QTOS((Q)BDY(t))]; i < nb; i++ )
1.247     noro     9086:       BDY(mat)[i][j] = ndtodp(mod,c[i]);
                   9087:   return mat;
                   9088: }
                   9089:
                   9090: MAT nd_btog_lf(LIST f,LIST v,struct order_spec *ord,LIST tlist,MAT *rp)
                   9091: {
                   9092:   int i,j,n,m,nb,pi0,pi1,nvar;
                   9093:   VL fv,tv,vv;
                   9094:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   9095:   ND **p;
                   9096:   ND *c;
                   9097:   ND u;
                   9098:   MAT mat;
                   9099:   LM lm;
                   9100:   GZ lf,inv;
                   9101:
                   9102:   parse_nd_option(current_option);
                   9103:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   9104:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   9105:   switch ( ord->id ) {
                   9106:     case 1:
                   9107:       if ( ord->nv != nvar )
                   9108:         error("nd_check : invalid order specification");
                   9109:       break;
                   9110:     default:
                   9111:       break;
                   9112:   }
                   9113:   nd_init_ord(ord);
                   9114: #if 0
                   9115:   nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
                   9116: #else
                   9117:   nd_bpe = 32;
                   9118: #endif
                   9119:   nd_setup_parameters(nvar,0);
                   9120:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   9121:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   9122:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   9123:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   9124:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
                   9125:     j = QTOS((Q)BDY(BDY((LIST)BDY(t))));
                   9126:   if ( j > i ) i = j;
                   9127:   }
                   9128:   n = i+1;
                   9129:   nb = length(BDY(f));
                   9130:   p = (ND **)MALLOC(n*sizeof(ND *));
                   9131:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   9132:     pi = BDY((LIST)BDY(t));
                   9133:     pi0 = QTOS((Q)ARG0(pi)); pi1 = QTOS((Q)ARG1(pi));
                   9134:     p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   9135:     simp_ff((Obj)ARG2(pi),&lm); lmtolf(lm,&lf); invgz(lf,&inv);
                   9136:     u = ptond(CO,vv,(P)ONE);
                   9137:     HCZ(u) = inv;
                   9138:     c[pi1] = u;
                   9139:   }
                   9140:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   9141:     printf("%d ",i); fflush(stdout);
                   9142:     ti = BDY((LIST)BDY(t));
                   9143:     p[j=QTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
                   9144:   }
                   9145:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   9146:     printf("%d ",i); fflush(stdout);
                   9147:     ti = BDY((LIST)BDY(t));
                   9148:     p[j=QTOS((Q)ARG0(ti))] = btog_lf(BDY((LIST)ARG1(ti)),p,nb);
                   9149:   }
                   9150:   m = length(ind);
                   9151:   MKMAT(mat,nb,m);
                   9152:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
                   9153:     for ( i = 0, c = p[QTOS((Q)BDY(t))]; i < nb; i++ )
                   9154:       BDY(mat)[i][j] = ndtodp(-2,c[i]);
1.204     noro     9155:   return mat;
                   9156: }
                   9157:
1.205     noro     9158: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
                   9159:   LIST tlist,int pos,MAT *rp)
                   9160: {
                   9161:   int i,j,n,m,nb,pi0,pi1,nvar;
                   9162:   VL fv,tv,vv;
                   9163:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   9164:   ND *p;
                   9165:   ND *c;
                   9166:   ND u;
                   9167:   P inv;
                   9168:   VECT vect;
                   9169:
1.247     noro     9170:   if ( mod == -2 )
                   9171:     error("nd_btog_one : not implemented yet for a large finite field");
                   9172:
1.205     noro     9173:   parse_nd_option(current_option);
                   9174:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   9175:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   9176:   switch ( ord->id ) {
                   9177:     case 1:
                   9178:       if ( ord->nv != nvar )
                   9179:         error("nd_check : invalid order specification");
                   9180:       break;
                   9181:     default:
                   9182:       break;
                   9183:   }
                   9184:   nd_init_ord(ord);
                   9185: #if 0
                   9186:   nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
                   9187: #else
                   9188:   nd_bpe = 32;
                   9189: #endif
                   9190:   nd_setup_parameters(nvar,0);
                   9191:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   9192:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   9193:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   9194:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   9195:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
1.245     noro     9196:     j = QTOS((Q)BDY(BDY((LIST)BDY(t))));
                   9197:   if ( j > i ) i = j;
1.205     noro     9198:   }
                   9199:   n = i+1;
                   9200:   nb = length(BDY(f));
                   9201:   p = (ND *)MALLOC(n*sizeof(ND *));
                   9202:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   9203:     pi = BDY((LIST)BDY(t));
1.245     noro     9204:   pi0 = QTOS((Q)ARG0(pi)); pi1 = QTOS((Q)ARG1(pi));
                   9205:   if ( pi1 == pos ) {
                   9206:     ptomp(mod,(P)ARG2(pi),&inv);
                   9207:     ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
                   9208:     u = ptond(CO,vv,(P)ONE);
                   9209:     HCM(u) = ((MQ)inv)->cont;
                   9210:     p[pi0] = u;
1.205     noro     9211:     }
                   9212:   }
                   9213:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
1.245     noro     9214:   printf("%d ",i); fflush(stdout);
1.205     noro     9215:     ti = BDY((LIST)BDY(t));
1.207     noro     9216:     p[j=QTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.206     noro     9217:     if ( Demand ) {
                   9218:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
1.245     noro     9219:   }
1.205     noro     9220:   }
                   9221:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
1.245     noro     9222:   printf("%d ",i); fflush(stdout);
1.205     noro     9223:     ti = BDY((LIST)BDY(t));
1.207     noro     9224:     p[j=QTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.206     noro     9225:     if ( Demand ) {
                   9226:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
1.245     noro     9227:   }
1.205     noro     9228:   }
                   9229:   m = length(ind);
                   9230:   MKVECT(vect,m);
1.206     noro     9231:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
1.245     noro     9232:   u = p[QTOS((Q)BDY(t))];
                   9233:   if ( !u ) {
                   9234:     u = nd_load_mod(QTOS((Q)BDY(t)));
                   9235:     BDY(vect)[j] = ndtodp(mod,u);
                   9236:     nd_free(u);
                   9237:   } else
                   9238:     BDY(vect)[j] = ndtodp(mod,u);
1.206     noro     9239:   }
1.205     noro     9240:   return vect;
                   9241: }
1.232     noro     9242:
                   9243: void ndv_print_lf(NDV p)
                   9244: {
                   9245:     NMV m;
                   9246:     int i,len;
                   9247:
                   9248:     if ( !p ) printf("0\n");
                   9249:     else {
                   9250:         len = LEN(p);
                   9251:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   9252:             printf("+");
                   9253:             mpz_out_str(asir_out,10,BDY(CZ(m)));
                   9254:             printf("*");
                   9255:             ndl_print(DL(m));
                   9256:         }
                   9257:         printf("\n");
                   9258:     }
                   9259: }
1.234     noro     9260:
                   9261: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
                   9262: {
                   9263:     VL tv,fv,vv,vc,av;
                   9264:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   9265:     int m,nocheck,nvar,mindex,e,max;
                   9266:     NDV c;
                   9267:     NMV a;
                   9268:     P p,zp;
                   9269:     Q dmy;
                   9270:     EPOS oepos;
                   9271:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   9272:     Alg alpha,dp;
                   9273:     P poly;
                   9274:     LIST f1,f2,zpl;
                   9275:     Obj obj;
                   9276:     NumberField nf;
                   9277:     struct order_spec *ord1;
                   9278:     struct oEGT eg_check,eg0,eg1;
                   9279:     NODE tr,tl1,tl2,tl3,tl4;
                   9280:     LIST l1,l2,l3,l4,l5;
                   9281:     int *perm;
                   9282:     int j,ret;
                   9283:     Q jq,bpe;
                   9284:
                   9285:     nd_module = 0;
                   9286:     parse_nd_option(current_option);
                   9287:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   9288:     if ( nd_vc )
                   9289:       error("nd_f4_lf_trace : computation over a rational function field is not implemented");
                   9290:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   9291:     switch ( ord->id ) {
                   9292:         case 1:
                   9293:             if ( ord->nv != nvar )
                   9294:                 error("nd_f4_lf_trace : invalid order specification");
                   9295:             break;
                   9296:         default:
                   9297:             break;
                   9298:     }
                   9299:
                   9300:     nd_ntrans = nvar;
                   9301:     nd_nalg = 0;
                   9302:
                   9303:     nocheck = 0;
                   9304:     mindex = 0;
                   9305:
1.235     noro     9306:     /* do not use on-demand load/save */
                   9307:     nd_demand = 0;
1.234     noro     9308:     m = trace > 1 ? trace : get_lprime(mindex);
                   9309:     nd_init_ord(ord);
                   9310:     mrank = 0;
                   9311:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   9312:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   9313:             if ( nd_module ) {
                   9314:                 s = BDY((LIST)BDY(t));
                   9315:                 trank = length(s);
                   9316:                 mrank = MAX(mrank,trank);
                   9317:                 for ( ; s; s = NEXT(s) ) {
                   9318:                     e = getdeg(tv->v,(P)BDY(s));
                   9319:                     max = MAX(e,max);
                   9320:                 }
                   9321:             } else {
                   9322:                 e = getdeg(tv->v,(P)BDY(t));
                   9323:                 max = MAX(e,max);
                   9324:             }
                   9325:         }
                   9326:     nd_setup_parameters(nvar,max);
                   9327:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   9328:     ishomo = 1;
                   9329:     /* XXX */
                   9330:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   9331:         if ( nd_module ) {
                   9332:             c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
                   9333:         } else {
                   9334:             c = (pointer)ptondv(CO,vv,(P)BDY(t));
                   9335:         }
                   9336:         if ( ishomo )
                   9337:             ishomo = ishomo && ndv_ishomo(c);
                   9338:         if ( c ) {
1.247     noro     9339:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   9340:             ndv_mod(-2,c);
1.234     noro     9341:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   9342:         }
                   9343:     }
                   9344:     if ( in0 ) NEXT(in) = 0;
                   9345:     if ( fd0 ) NEXT(fd) = 0;
                   9346:     if ( !ishomo && homo ) {
                   9347:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   9348:             c = (NDV)BDY(t); len = LEN(c);
                   9349:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   9350:                 wmax = MAX(TD(DL(a)),wmax);
                   9351:         }
                   9352:         homogenize_order(ord,nvar,&ord1);
                   9353:         nd_init_ord(ord1);
                   9354:         nd_setup_parameters(nvar+1,wmax);
                   9355:         for ( t = fd0; t; t = NEXT(t) )
                   9356:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   9357:     }
                   9358:     if ( MaxDeg > 0 ) nocheck = 1;
1.235     noro     9359:     ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0);
                   9360:     if ( ret )
                   9361:       cand = nd_f4_lf_trace_main(m,&perm);
                   9362:     if ( !ret || !cand ) {
                   9363:        *rp = 0; return;
                   9364:     }
                   9365:     if ( !ishomo && homo ) {
                   9366:       /* dehomogenization */
                   9367:       for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   9368:       nd_init_ord(ord);
                   9369:       nd_setup_parameters(nvar,0);
1.234     noro     9370:     }
1.235     noro     9371:     cand = ndv_reducebase(cand,perm);
                   9372:     cand = ndv_reduceall(-2,cand);
                   9373:     cbpe = nd_bpe;
                   9374:     get_eg(&eg0);
                   9375:     if ( ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand) ) {
                   9376:       /* gbcheck : cand is a GB of Id(cand) ? */
                   9377:       ret = nd_f4(-2,0,0);
                   9378:     }
1.245     noro     9379:   if ( !ret ) {
1.235     noro     9380:       /* failure */
                   9381:       *rp = 0; return;
                   9382:     }
1.234     noro     9383:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   9384:     if ( DP_Print )
1.241     noro     9385:         fprintf(asir_out,"check=%.3fsec\n",eg_check.exectime+eg_check.gctime);
1.234     noro     9386:     /* dp->p */
                   9387:     nd_bpe = cbpe;
                   9388:     nd_setup_parameters(nd_nvar,0);
                   9389:     for ( r = cand; r; r = NEXT(r) ) {
                   9390:       if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
                   9391:       else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
                   9392:     }
                   9393:     MKLIST(*rp,cand);
                   9394: }
                   9395:
                   9396: NODE nd_f4_lf_trace_main(int m,int **indp)
                   9397: {
                   9398:     int i,nh,stat,index;
                   9399:     NODE r,rm,g;
                   9400:     ND_pairs d,l,l0,t;
                   9401:     ND spol,red;
                   9402:     NDV nf,redv,nfqv,nfv;
                   9403:     NM s0,s;
                   9404:     NODE rp0,srp0,nflist,nflist_lf;
                   9405:     int nsp,nred,col,rank,len,k,j,a;
                   9406:     UINT c;
                   9407:     UINT **spmat;
                   9408:     UINT *s0vect,*svect,*p,*v;
                   9409:     int *colstat;
                   9410:     IndArray *imat;
                   9411:     int *rhead;
                   9412:     int spcol,sprow;
                   9413:     int sugar;
                   9414:     PGeoBucket bucket;
                   9415:     struct oEGT eg0,eg1,eg_f4;
                   9416:
                   9417:     g = 0; d = 0;
                   9418:     for ( i = 0; i < nd_psn; i++ ) {
                   9419:         d = update_pairs(d,g,i,0);
                   9420:         g = update_base(g,i);
                   9421:     }
                   9422:     while ( d ) {
                   9423:         get_eg(&eg0);
                   9424:         l = nd_minsugarp(d,&d);
                   9425:         sugar = SG(l);
                   9426:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   9427:         bucket = create_pbucket();
                   9428:         stat = nd_sp_f4(m,0,l,bucket);
                   9429:         if ( !stat ) {
                   9430:             for ( t = l; NEXT(t); t = NEXT(t) );
                   9431:             NEXT(t) = d; d = l;
                   9432:             d = nd_reconstruct(1,d);
                   9433:             continue;
                   9434:         }
                   9435:         if ( bucket->m < 0 ) continue;
                   9436:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   9437:         if ( !col ) {
                   9438:             for ( t = l; NEXT(t); t = NEXT(t) );
                   9439:             NEXT(t) = d; d = l;
                   9440:             d = nd_reconstruct(1,d);
                   9441:             continue;
                   9442:         }
                   9443:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   9444:         if ( DP_Print )
1.241     noro     9445:             fprintf(asir_out,"sugar=%d,symb=%.3fsec,",
1.234     noro     9446:                 sugar,eg_f4.exectime+eg_f4.gctime);
                   9447:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   9448:         if ( !l0 ) continue;
                   9449:         l = l0;
                   9450:
                   9451:         /* over LF */
                   9452:         bucket = create_pbucket();
                   9453:         stat = nd_sp_f4(-2,1,l,bucket);
                   9454:         if ( !stat ) {
                   9455:             for ( t = l; NEXT(t); t = NEXT(t) );
                   9456:             NEXT(t) = d; d = l;
                   9457:             d = nd_reconstruct(1,d);
                   9458:             continue;
                   9459:         }
                   9460:         if ( bucket->m < 0 ) continue;
                   9461:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   9462:         if ( !col ) {
                   9463:             for ( t = l; NEXT(t); t = NEXT(t) );
                   9464:             NEXT(t) = d; d = l;
                   9465:             d = nd_reconstruct(1,d);
                   9466:             continue;
                   9467:         }
                   9468:         nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
                   9469:         /* adding new bases */
                   9470:         for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
                   9471:             nfv = (NDV)BDY(rm);
                   9472:             nfqv = (NDV)BDY(r);
                   9473:             if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
                   9474:             ndv_removecont(m,nfv);
                   9475:             ndv_removecont(-2,nfqv);
                   9476:             nh = ndv_newps(-2,nfv,nfqv,1);
                   9477:             d = update_pairs(d,g,nh,0);
                   9478:             g = update_base(g,nh);
                   9479:         }
                   9480:         if ( r || rm ) return 0;
                   9481:     }
1.245     noro     9482:   conv_ilist(nd_demand,1,g,indp);
1.234     noro     9483:     return g;
                   9484: }
                   9485:

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