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

1.234   ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.233 2017/02/07 08:30:30 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.234   ! noro       68: static int nd_f4red,nd_rank0;
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.234   ! noro      101: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
1.232     noro      102:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred);
                    103: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat);
1.234   ! noro      104: NODE nd_f4_lf_trace_main(int m,int **indp);
        !           105: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
        !           106:
1.232     noro      107: extern int lf_lazy;
                    108: extern GZ current_mod_lf;
                    109:
1.228     noro      110: extern int Denominator,DP_Multiple,MaxDeg;
1.149     noro      111:
1.220     noro      112: #define BLEN (8*sizeof(unsigned long))
                    113:
                    114: typedef struct matrix {
                    115:   int row,col;
                    116:   unsigned long **a;
                    117: } *matrix;
                    118:
                    119:
1.1       noro      120: void nd_free_private_storage()
                    121: {
1.157     noro      122:     _nm_free_list = 0;
                    123:     _ndp_free_list = 0;
1.71      noro      124: #if 0
1.157     noro      125:     GC_gcollect();
1.71      noro      126: #endif
1.1       noro      127: }
                    128:
                    129: void _NM_alloc()
                    130: {
1.157     noro      131:     NM p;
                    132:     int i;
1.1       noro      133:
1.157     noro      134:     for ( i = 0; i < 1024; i++ ) {
1.200     noro      135:         p = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.157     noro      136:         p->next = _nm_free_list; _nm_free_list = p;
                    137:     }
1.1       noro      138: }
                    139:
1.220     noro      140: matrix alloc_matrix(int row,int col)
                    141: {
                    142:   unsigned long **a;
                    143:   int i,len,blen;
                    144:   matrix mat;
                    145:
                    146:   mat = (matrix)MALLOC(sizeof(struct matrix));
                    147:   mat->row = row;
                    148:   mat->col = col;
                    149:   mat->a = a = (unsigned long **)MALLOC(row*sizeof(unsigned long *));
                    150:   return mat;
                    151: }
                    152:
                    153:
1.1       noro      154: void _ND_alloc()
                    155: {
1.157     noro      156:     ND p;
                    157:     int i;
1.1       noro      158:
1.157     noro      159:     for ( i = 0; i < 1024; i++ ) {
1.200     noro      160:         p = (ND)MALLOC(sizeof(struct oND));
1.157     noro      161:         p->body = (NM)_nd_free_list; _nd_free_list = p;
                    162:     }
1.1       noro      163: }
                    164:
                    165: void _NDP_alloc()
                    166: {
1.157     noro      167:     ND_pairs p;
                    168:     int i;
1.1       noro      169:
1.157     noro      170:     for ( i = 0; i < 1024; i++ ) {
1.200     noro      171:         p = (ND_pairs)MALLOC(sizeof(struct oND_pairs)
1.157     noro      172:             +(nd_wpd-1)*sizeof(UINT));
                    173:         p->next = _ndp_free_list; _ndp_free_list = p;
                    174:     }
1.1       noro      175: }
                    176:
1.30      noro      177: INLINE int nd_length(ND p)
1.1       noro      178: {
1.157     noro      179:     NM m;
                    180:     int i;
1.1       noro      181:
1.157     noro      182:     if ( !p )
                    183:         return 0;
                    184:     else {
                    185:         for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
                    186:         return i;
                    187:     }
1.1       noro      188: }
                    189:
1.230     noro      190: extern int dp_negative_weight;
                    191:
1.61      noro      192: INLINE int ndl_reducible(UINT *d1,UINT *d2)
1.1       noro      193: {
1.157     noro      194:     UINT u1,u2;
                    195:     int i,j;
1.1       noro      196:
1.157     noro      197:     if ( nd_module && (MPOS(d1) != MPOS(d2)) ) return 0;
                    198:
1.230     noro      199:     if ( !dp_negative_weight && TD(d1) < TD(d2) ) return 0;
1.65      noro      200: #if USE_UNROLL
1.157     noro      201:     switch ( nd_bpe ) {
                    202:         case 3:
                    203:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    204:                 u1 = d1[i]; u2 = d2[i];
                    205:                 if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
                    206:                 if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
                    207:                 if ( (u1&  0xe00000) < (u2&  0xe00000) ) return 0;
                    208:                 if ( (u1&  0x1c0000) < (u2&  0x1c0000) ) return 0;
                    209:                 if ( (u1&   0x38000) < (u2&   0x38000) ) return 0;
                    210:                 if ( (u1&    0x7000) < (u2&    0x7000) ) return 0;
                    211:                 if ( (u1&     0xe00) < (u2&     0xe00) ) return 0;
                    212:                 if ( (u1&     0x1c0) < (u2&     0x1c0) ) return 0;
                    213:                 if ( (u1&      0x38) < (u2&      0x38) ) return 0;
                    214:                 if ( (u1&       0x7) < (u2&       0x7) ) return 0;
                    215:             }
                    216:             return 1;
                    217:             break;
                    218:         case 4:
                    219:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    220:                 u1 = d1[i]; u2 = d2[i];
                    221:                 if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
                    222:                 if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
                    223:                 if ( (u1&  0xf00000) < (u2&  0xf00000) ) return 0;
                    224:                 if ( (u1&   0xf0000) < (u2&   0xf0000) ) return 0;
                    225:                 if ( (u1&    0xf000) < (u2&    0xf000) ) return 0;
                    226:                 if ( (u1&     0xf00) < (u2&     0xf00) ) return 0;
                    227:                 if ( (u1&      0xf0) < (u2&      0xf0) ) return 0;
                    228:                 if ( (u1&       0xf) < (u2&       0xf) ) return 0;
                    229:             }
                    230:             return 1;
                    231:             break;
                    232:         case 6:
                    233:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    234:                 u1 = d1[i]; u2 = d2[i];
                    235:                 if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
                    236:                 if ( (u1&  0xfc0000) < (u2&  0xfc0000) ) return 0;
                    237:                 if ( (u1&   0x3f000) < (u2&   0x3f000) ) return 0;
                    238:                 if ( (u1&     0xfc0) < (u2&     0xfc0) ) return 0;
                    239:                 if ( (u1&      0x3f) < (u2&      0x3f) ) return 0;
                    240:             }
                    241:             return 1;
                    242:             break;
                    243:         case 8:
                    244:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    245:                 u1 = d1[i]; u2 = d2[i];
                    246:                 if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
                    247:                 if ( (u1&  0xff0000) < (u2&  0xff0000) ) return 0;
                    248:                 if ( (u1&    0xff00) < (u2&    0xff00) ) return 0;
                    249:                 if ( (u1&      0xff) < (u2&      0xff) ) return 0;
                    250:             }
                    251:             return 1;
                    252:             break;
                    253:         case 16:
                    254:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    255:                 u1 = d1[i]; u2 = d2[i];
                    256:                 if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
                    257:                 if ( (u1&    0xffff) < (u2&    0xffff) ) return 0;
                    258:             }
                    259:             return 1;
                    260:             break;
                    261:         case 32:
                    262:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                    263:                 if ( d1[i] < d2[i] ) return 0;
                    264:             return 1;
                    265:             break;
                    266:         default:
                    267:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    268:                 u1 = d1[i]; u2 = d2[i];
                    269:                 for ( j = 0; j < nd_epw; j++ )
                    270:                     if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    271:             }
                    272:             return 1;
                    273:     }
1.65      noro      274: #else
1.157     noro      275:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    276:         u1 = d1[i]; u2 = d2[i];
                    277:         for ( j = 0; j < nd_epw; j++ )
                    278:             if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    279:     }
                    280:     return 1;
1.65      noro      281: #endif
1.1       noro      282: }
                    283:
1.61      noro      284: /*
                    285:  * If the current order is a block order,
                    286:  * then the last block is length 1 and contains
                    287:  * the homo variable. Otherwise, the original
                    288:  * order is either 0 or 2.
                    289:  */
                    290:
1.164     noro      291: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int ompos,int weight)
1.23      noro      292: {
1.157     noro      293:     int w,i,e,n,omask0;
1.61      noro      294:
1.157     noro      295:     omask0 = obpe==32?0xffffffff:((1<<obpe)-1);
                    296:     n = nd_nvar-1;
                    297:     ndl_zero(r);
                    298:     for ( i = 0; i < n; i++ ) {
                    299:         e = GET_EXP_OLD(d,i);
                    300:         PUT_EXP(r,i,e);
                    301:     }
                    302:     w = TD(d);
                    303:     PUT_EXP(r,nd_nvar-1,weight-w);
1.164     noro      304:     if ( nd_module ) MPOS(r) = d[ompos];
1.157     noro      305:     TD(r) = weight;
                    306:     if ( nd_blockmask ) ndl_weight_mask(r);
1.61      noro      307: }
                    308:
                    309: void ndl_dehomogenize(UINT *d)
                    310: {
1.157     noro      311:     UINT mask;
                    312:     UINT h;
                    313:     int i,bits;
                    314:
                    315:     if ( nd_blockmask ) {
                    316:         h = GET_EXP(d,nd_nvar-1);
                    317:         XOR_EXP(d,nd_nvar-1,h);
                    318:         TD(d) -= h;
                    319:         ndl_weight_mask(d);
                    320:     } else {
                    321:         if ( nd_isrlex ) {
                    322:             if ( nd_bpe == 32 ) {
                    323:                 h = d[nd_exporigin];
                    324:                 for ( i = nd_exporigin+1; i < nd_wpd; i++ )
                    325:                     d[i-1] = d[i];
                    326:                 d[i-1] = 0;
                    327:                 TD(d) -= h;
                    328:             } else {
                    329:                 bits = nd_epw*nd_bpe;
                    330:                 mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
                    331:                 h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
                    332:                 for ( i = nd_exporigin; i < nd_wpd; i++ )
                    333:                     d[i] = ((d[i]<<nd_bpe)&mask)
                    334:                         |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
                    335:                 TD(d) -= h;
                    336:             }
                    337:         } else {
                    338:             h = GET_EXP(d,nd_nvar-1);
                    339:             XOR_EXP(d,nd_nvar-1,h);
                    340:             TD(d) -= h;
                    341:         }
                    342:     }
1.23      noro      343: }
                    344:
1.61      noro      345: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
1.1       noro      346: {
1.157     noro      347:     UINT t1,t2,u,u1,u2;
                    348:     int i,j,l;
                    349:
                    350:     if ( nd_module && (MPOS(d1) != MPOS(d2)) )
                    351:         error("ndl_lcm : inconsistent monomials");
                    352: #if USE_UNROLL
                    353:     switch ( nd_bpe ) {
                    354:         case 3:
                    355:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    356:                 u1 = d1[i]; u2 = d2[i];
                    357:                 t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
                    358:                 t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
                    359:                 t1 = (u1&  0xe00000); t2 = (u2&  0xe00000); u |= t1>t2?t1:t2;
                    360:                 t1 = (u1&  0x1c0000); t2 = (u2&  0x1c0000); u |= t1>t2?t1:t2;
                    361:                 t1 = (u1&   0x38000); t2 = (u2&   0x38000); u |= t1>t2?t1:t2;
                    362:                 t1 = (u1&    0x7000); t2 = (u2&    0x7000); u |= t1>t2?t1:t2;
                    363:                 t1 = (u1&     0xe00); t2 = (u2&     0xe00); u |= t1>t2?t1:t2;
                    364:                 t1 = (u1&     0x1c0); t2 = (u2&     0x1c0); u |= t1>t2?t1:t2;
                    365:                 t1 = (u1&      0x38); t2 = (u2&      0x38); u |= t1>t2?t1:t2;
                    366:                 t1 = (u1&       0x7); t2 = (u2&       0x7); u |= t1>t2?t1:t2;
                    367:                 d[i] = u;
                    368:             }
                    369:             break;
                    370:         case 4:
                    371:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    372:                 u1 = d1[i]; u2 = d2[i];
                    373:                 t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
                    374:                 t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
                    375:                 t1 = (u1&  0xf00000); t2 = (u2&  0xf00000); u |= t1>t2?t1:t2;
                    376:                 t1 = (u1&   0xf0000); t2 = (u2&   0xf0000); u |= t1>t2?t1:t2;
                    377:                 t1 = (u1&    0xf000); t2 = (u2&    0xf000); u |= t1>t2?t1:t2;
                    378:                 t1 = (u1&     0xf00); t2 = (u2&     0xf00); u |= t1>t2?t1:t2;
                    379:                 t1 = (u1&      0xf0); t2 = (u2&      0xf0); u |= t1>t2?t1:t2;
                    380:                 t1 = (u1&       0xf); t2 = (u2&       0xf); u |= t1>t2?t1:t2;
                    381:                 d[i] = u;
                    382:             }
                    383:             break;
                    384:         case 6:
                    385:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    386:                 u1 = d1[i]; u2 = d2[i];
                    387:                 t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
                    388:                 t1 = (u1&  0xfc0000); t2 = (u2&  0xfc0000); u |= t1>t2?t1:t2;
                    389:                 t1 = (u1&   0x3f000); t2 = (u2&   0x3f000); u |= t1>t2?t1:t2;
                    390:                 t1 = (u1&     0xfc0); t2 = (u2&     0xfc0); u |= t1>t2?t1:t2;
                    391:                 t1 = (u1&      0x3f); t2 = (u2&      0x3f); u |= t1>t2?t1:t2;
                    392:                 d[i] = u;
                    393:             }
                    394:             break;
                    395:         case 8:
                    396:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    397:                 u1 = d1[i]; u2 = d2[i];
                    398:                 t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
                    399:                 t1 = (u1&  0xff0000); t2 = (u2&  0xff0000); u |= t1>t2?t1:t2;
                    400:                 t1 = (u1&    0xff00); t2 = (u2&    0xff00); u |= t1>t2?t1:t2;
                    401:                 t1 = (u1&      0xff); t2 = (u2&      0xff); u |= t1>t2?t1:t2;
                    402:                 d[i] = u;
                    403:             }
                    404:             break;
                    405:         case 16:
                    406:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    407:                 u1 = d1[i]; u2 = d2[i];
                    408:                 t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
                    409:                 t1 = (u1&    0xffff); t2 = (u2&    0xffff); u |= t1>t2?t1:t2;
                    410:                 d[i] = u;
                    411:             }
                    412:             break;
                    413:         case 32:
                    414:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    415:                 u1 = d1[i]; u2 = d2[i];
                    416:                 d[i] = u1>u2?u1:u2;
                    417:             }
                    418:             break;
                    419:         default:
                    420:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    421:                 u1 = d1[i]; u2 = d2[i];
                    422:                 for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    423:                     t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    424:                 }
                    425:                 d[i] = u;
                    426:             }
                    427:             break;
                    428:     }
                    429: #else
                    430:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    431:         u1 = d1[i]; u2 = d2[i];
                    432:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    433:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    434:         }
                    435:         d[i] = u;
                    436:     }
                    437: #endif
1.163     noro      438:     if ( nd_module ) MPOS(d) = MPOS(d1);
1.157     noro      439:     TD(d) = ndl_weight(d);
                    440:     if ( nd_blockmask ) ndl_weight_mask(d);
                    441: }
                    442:
1.159     noro      443: void ndl_max(UINT *d1,unsigned *d2,UINT *d)
1.157     noro      444: {
                    445:     UINT t1,t2,u,u1,u2;
                    446:     int i,j,l;
1.1       noro      447:
1.157     noro      448:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    449:         u1 = d1[i]; u2 = d2[i];
                    450:         for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    451:             t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    452:         }
                    453:         d[i] = u;
                    454:     }
1.57      noro      455: }
                    456:
1.61      noro      457: int ndl_weight(UINT *d)
1.1       noro      458: {
1.157     noro      459:     UINT t,u;
                    460:     int i,j;
1.1       noro      461:
1.157     noro      462:     if ( current_dl_weight_vector )
                    463:         for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    464:             u = GET_EXP(d,i);
                    465:             t += MUL_WEIGHT(u,i);
                    466:         }
                    467:     else
                    468:         for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    469:             u = d[i];
                    470:             for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    471:                 t += (u&nd_mask0);
                    472:         }
1.167     noro      473:     if ( nd_module && current_module_weight_vector && MPOS(d) )
                    474:         t += current_module_weight_vector[MPOS(d)];
1.157     noro      475:     return t;
1.1       noro      476: }
                    477:
1.231     noro      478: /* for sugarweight */
                    479:
                    480: int ndl_weight2(UINT *d)
                    481: {
                    482:     int t,u;
                    483:     int i,j;
                    484:
                    485:     for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    486:         u = GET_EXP(d,i);
                    487:         t += nd_sugarweight[i]*u;
                    488:     }
                    489:     if ( nd_module && current_module_weight_vector && MPOS(d) )
                    490:         t += current_module_weight_vector[MPOS(d)];
                    491:     return t;
                    492: }
                    493:
1.61      noro      494: void ndl_weight_mask(UINT *d)
1.43      noro      495: {
1.157     noro      496:     UINT t,u;
                    497:     UINT *mask;
                    498:     int i,j,k,l;
                    499:
                    500:     l = nd_blockmask->n;
                    501:     for ( k = 0; k < l; k++ ) {
                    502:         mask = nd_blockmask->mask[k];
                    503:         if ( current_dl_weight_vector )
                    504:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    505:                 u = GET_EXP_MASK(d,i,mask);
                    506:                 t += MUL_WEIGHT(u,i);
                    507:             }
                    508:         else
                    509:             for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    510:                 u = d[i]&mask[i];
                    511:                 for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    512:                     t += (u&nd_mask0);
                    513:             }
                    514:         d[k+1] = t;
                    515:     }
1.43      noro      516: }
                    517:
1.61      noro      518: int ndl_lex_compare(UINT *d1,UINT *d2)
1.1       noro      519: {
1.157     noro      520:     int i;
1.1       noro      521:
1.157     noro      522:     d1 += nd_exporigin;
                    523:     d2 += nd_exporigin;
                    524:     for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
                    525:         if ( *d1 > *d2 )
                    526:             return nd_isrlex ? -1 : 1;
                    527:         else if ( *d1 < *d2 )
                    528:             return nd_isrlex ? 1 : -1;
                    529:     return 0;
1.1       noro      530: }
                    531:
1.61      noro      532: int ndl_block_compare(UINT *d1,UINT *d2)
1.43      noro      533: {
1.157     noro      534:     int i,l,j,ord_o,ord_l;
                    535:     struct order_pair *op;
                    536:     UINT t1,t2,m;
                    537:     UINT *mask;
                    538:
                    539:     l = nd_blockmask->n;
                    540:     op = nd_blockmask->order_pair;
                    541:     for ( j = 0; j < l; j++ ) {
                    542:         mask = nd_blockmask->mask[j];
                    543:         ord_o = op[j].order;
                    544:         if ( ord_o < 2 )
                    545:             if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
                    546:             else if ( t1 < t2 ) return -1;
                    547:         for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    548:             m = mask[i];
                    549:             t1 = d1[i]&m;
                    550:             t2 = d2[i]&m;
                    551:             if ( t1 > t2 )
                    552:                 return !ord_o ? -1 : 1;
                    553:             else if ( t1 < t2 )
                    554:                 return !ord_o ? 1 : -1;
                    555:         }
                    556:     }
                    557:     return 0;
1.43      noro      558: }
                    559:
1.96      noro      560: int ndl_matrix_compare(UINT *d1,UINT *d2)
                    561: {
1.219     noro      562:     int i,j,s,row;
1.157     noro      563:     int *v;
1.219     noro      564:        Q **mat;
                    565:     Q *w;
                    566:     Q t,t1,t2;
1.96      noro      567:
1.157     noro      568:     for ( j = 0; j < nd_nvar; j++ )
                    569:         nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
1.219     noro      570:        if ( nd_top_weight ) {
                    571:                if ( OID(nd_top_weight) == O_VECT ) {
                    572:                    mat = (Q **)&BDY((VECT)nd_top_weight);
                    573:                    row = 1;
                    574:                } else {
                    575:                        mat = (Q **)BDY((MAT)nd_top_weight);
                    576:                    row = ((MAT)nd_top_weight)->row;
                    577:                }
                    578:                for ( i = 0; i < row; i++ ) {
                    579:                    w = (Q *)mat[i];
                    580:                        for ( j = 0, t = 0; j < nd_nvar; j++ ) {
                    581:                                STOQ(nd_work_vector[j],t1);
                    582:                            mulq(w[j],t1,&t2);
                    583:                                addq(t,t2,&t1);
                    584:                                t = t1;
                    585:                        }
                    586:                    if ( t ) {
                    587:                        s = SGN(t);
                    588:                        if ( s > 0 ) return 1;
                    589:                        else if ( s < 0 ) return -1;
                    590:                    }
                    591:                }
                    592:        }
1.157     noro      593:     for ( i = 0; i < nd_matrix_len; i++ ) {
                    594:         v = nd_matrix[i];
                    595:         for ( j = 0, s = 0; j < nd_nvar; j++ )
                    596:             s += v[j]*nd_work_vector[j];
                    597:         if ( s > 0 ) return 1;
                    598:         else if ( s < 0 ) return -1;
                    599:     }
1.219     noro      600:        if ( !ndl_equal(d1,d2) )
                    601:                error("afo");
1.157     noro      602:     return 0;
1.96      noro      603: }
                    604:
1.97      noro      605: int ndl_composite_compare(UINT *d1,UINT *d2)
                    606: {
1.157     noro      607:     int i,j,s,start,end,len,o;
                    608:     int *v;
                    609:     struct sparse_weight *sw;
                    610:
                    611:     for ( j = 0; j < nd_nvar; j++ )
                    612:         nd_work_vector[j] = GET_EXP(d1,j)-GET_EXP(d2,j);
                    613:     for ( i = 0; i < nd_worb_len; i++ ) {
                    614:         len = nd_worb[i].length;
                    615:         switch ( nd_worb[i].type ) {
                    616:             case IS_DENSE_WEIGHT:
                    617:                 v = nd_worb[i].body.dense_weight;
                    618:                 for ( j = 0, s = 0; j < len; j++ )
                    619:                     s += v[j]*nd_work_vector[j];
                    620:                 if ( s > 0 ) return 1;
                    621:                 else if ( s < 0 ) return -1;
                    622:                 break;
                    623:             case IS_SPARSE_WEIGHT:
                    624:                 sw = nd_worb[i].body.sparse_weight;
                    625:                 for ( j = 0, s = 0; j < len; j++ )
                    626:                     s += sw[j].value*nd_work_vector[sw[j].pos];
                    627:                 if ( s > 0 ) return 1;
                    628:                 else if ( s < 0 ) return -1;
                    629:                 break;
                    630:             case IS_BLOCK:
                    631:                 o = nd_worb[i].body.block.order;
                    632:                 start = nd_worb[i].body.block.start;
                    633:                 switch ( o ) {
                    634:                     case 0:
                    635:                         end = start+len;
                    636:                         for ( j = start, s = 0; j < end; j++ )
                    637:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    638:                         if ( s > 0 ) return 1;
                    639:                         else if ( s < 0 ) return -1;
                    640:                         for ( j = end-1; j >= start; j-- )
                    641:                             if ( nd_work_vector[j] < 0 ) return 1;
                    642:                             else if ( nd_work_vector[j] > 0 ) return -1;
                    643:                         break;
                    644:                     case 1:
                    645:                         end = start+len;
                    646:                         for ( j = start, s = 0; j < end; j++ )
                    647:                             s += MUL_WEIGHT(nd_work_vector[j],j);
                    648:                         if ( s > 0 ) return 1;
                    649:                         else if ( s < 0 ) return -1;
                    650:                         for ( j = start; j < end; j++ )
                    651:                             if ( nd_work_vector[j] > 0 ) return 1;
                    652:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    653:                         break;
                    654:                     case 2:
                    655:                         for ( j = start; j < end; j++ )
                    656:                             if ( nd_work_vector[j] > 0 ) return 1;
                    657:                             else if ( nd_work_vector[j] < 0 ) return -1;
                    658:                         break;
                    659:                 }
                    660:                 break;
                    661:         }
                    662:     }
                    663:     return 0;
1.97      noro      664: }
                    665:
1.58      noro      666: /* TDH -> WW -> TD-> RL */
                    667:
1.61      noro      668: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
1.58      noro      669: {
1.157     noro      670:     int i,m,e1,e2;
1.58      noro      671:
1.157     noro      672:     if ( TD(d1) > TD(d2) ) return 1;
                    673:     else if ( TD(d1) < TD(d2) ) return -1;
                    674:     m = nd_nvar>>1;
                    675:     for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
                    676:         e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
                    677:         e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
                    678:     }
                    679:     if ( e1 > e2 ) return 1;
                    680:     else if ( e1 < e2 ) return -1;
                    681:     return ndl_lex_compare(d1,d2);
                    682: }
                    683:
1.224     noro      684: int ndl_module_weight_compare(UINT *d1,UINT *d2)
                    685: {
                    686:   int s,j;
                    687:
                    688:   if ( nd_nvar != nd_poly_weight_len )
                    689:     error("invalid module weight : the length of polynomial weight != the number of variables");
                    690:   s = 0;
                    691:   for ( j = 0; j < nd_nvar; j++ )
                    692:      s += (GET_EXP(d1,j)-GET_EXP(d2,j))*nd_poly_weight[j];
1.226     noro      693:   if ( MPOS(d1) >= 1 && MPOS(d2) >= 1 ) {
                    694:     s += nd_module_weight[MPOS(d1)-1]-nd_module_weight[MPOS(d2)-1];
                    695:   }
1.224     noro      696:   if ( s > 0 ) return 1;
                    697:   else if ( s < 0 ) return -1;
                    698:   else return 0;
                    699: }
                    700:
1.157     noro      701: int ndl_module_grlex_compare(UINT *d1,UINT *d2)
                    702: {
1.164     noro      703:     int i,c;
1.157     noro      704:
1.224     noro      705:     if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
1.160     noro      706:     if ( nd_ispot ) {
1.174     noro      707:                if ( nd_pot_nelim && MPOS(d1)>=nd_pot_nelim+1 && MPOS(d2) >= nd_pot_nelim+1 ) {
                    708:             if ( TD(d1) > TD(d2) ) return 1;
                    709:             else if ( TD(d1) < TD(d2) ) return -1;
                    710:             if ( c = ndl_lex_compare(d1,d2) ) return c;
                    711:             if ( MPOS(d1) < MPOS(d2) ) return 1;
                    712:             else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    713:             return 0;
                    714:                }
1.157     noro      715:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    716:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    717:     }
                    718:     if ( TD(d1) > TD(d2) ) return 1;
                    719:     else if ( TD(d1) < TD(d2) ) return -1;
1.167     noro      720:     if ( c = ndl_lex_compare(d1,d2) ) return c;
1.160     noro      721:     if ( !nd_ispot ) {
1.157     noro      722:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    723:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    724:     }
                    725:     return 0;
                    726: }
                    727:
                    728: int ndl_module_glex_compare(UINT *d1,UINT *d2)
                    729: {
1.164     noro      730:     int i,c;
1.157     noro      731:
1.224     noro      732:     if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
1.160     noro      733:     if ( nd_ispot ) {
1.157     noro      734:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    735:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    736:     }
                    737:     if ( TD(d1) > TD(d2) ) return 1;
                    738:     else if ( TD(d1) < TD(d2) ) return -1;
1.167     noro      739:     if ( c = ndl_lex_compare(d1,d2) ) return c;
1.160     noro      740:     if ( !nd_ispot ) {
1.157     noro      741:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    742:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    743:     }
                    744:     return 0;
                    745: }
                    746:
                    747: int ndl_module_lex_compare(UINT *d1,UINT *d2)
                    748: {
1.164     noro      749:     int i,c;
1.157     noro      750:
1.224     noro      751:     if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
1.160     noro      752:     if ( nd_ispot ) {
1.157     noro      753:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    754:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    755:     }
1.167     noro      756:     if ( c = ndl_lex_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:     }
                    761:     return 0;
                    762: }
                    763:
                    764: int ndl_module_block_compare(UINT *d1,UINT *d2)
                    765: {
                    766:     int i,c;
                    767:
1.224     noro      768:     if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
1.160     noro      769:     if ( nd_ispot ) {
1.157     noro      770:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    771:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    772:     }
                    773:     if ( c = ndl_block_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:     return 0;
                    779: }
                    780:
                    781: int ndl_module_matrix_compare(UINT *d1,UINT *d2)
                    782: {
                    783:     int i,c;
                    784:
1.224     noro      785:     if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
1.160     noro      786:     if ( nd_ispot ) {
1.157     noro      787:         if ( MPOS(d1) < MPOS(d2) ) return 1;
                    788:         else if ( MPOS(d1) > MPOS(d2) ) return -1;
                    789:     }
                    790:     if ( c = ndl_matrix_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:     return 0;
                    796: }
                    797:
                    798: int ndl_module_composite_compare(UINT *d1,UINT *d2)
                    799: {
                    800:     int i,c;
                    801:
1.224     noro      802:     if ( nd_module_rank && (c = ndl_module_weight_compare(d1,d2)) ) return c;
1.160     noro      803:     if ( nd_ispot ) {
1.157     noro      804:         if ( MPOS(d1) > MPOS(d2) ) return 1;
                    805:         else if ( MPOS(d1) < MPOS(d2) ) return -1;
                    806:     }
                    807:     if ( c = ndl_composite_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:     return 0;
1.58      noro      813: }
                    814:
1.61      noro      815: INLINE int ndl_equal(UINT *d1,UINT *d2)
1.1       noro      816: {
1.157     noro      817:     int i;
1.1       noro      818:
1.157     noro      819:     switch ( nd_wpd ) {
                    820:         case 2:
                    821:             if ( TD(d2) != TD(d1) ) return 0;
                    822:             if ( d2[1] != d1[1] ) return 0;
                    823:             return 1;
                    824:             break;
                    825:         case 3:
                    826:             if ( TD(d2) != TD(d1) ) return 0;
                    827:             if ( d2[1] != d1[1] ) return 0;
                    828:             if ( d2[2] != d1[2] ) return 0;
                    829:             return 1;
                    830:             break;
                    831:         default:
                    832:             for ( i = 0; i < nd_wpd; i++ )
                    833:                 if ( *d1++ != *d2++ ) return 0;
                    834:             return 1;
                    835:             break;
                    836:     }
1.1       noro      837: }
                    838:
1.61      noro      839: INLINE void ndl_copy(UINT *d1,UINT *d2)
1.6       noro      840: {
1.157     noro      841:     int i;
1.6       noro      842:
1.157     noro      843:     switch ( nd_wpd ) {
                    844:         case 2:
                    845:             TD(d2) = TD(d1);
                    846:             d2[1] = d1[1];
                    847:             break;
                    848:         case 3:
                    849:             TD(d2) = TD(d1);
                    850:             d2[1] = d1[1];
                    851:             d2[2] = d1[2];
                    852:             break;
                    853:         default:
                    854:             for ( i = 0; i < nd_wpd; i++ )
                    855:                 d2[i] = d1[i];
                    856:             break;
                    857:     }
1.6       noro      858: }
                    859:
1.61      noro      860: INLINE void ndl_zero(UINT *d)
                    861: {
1.157     noro      862:     int i;
                    863:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
1.61      noro      864: }
                    865:
                    866: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
1.1       noro      867: {
1.157     noro      868:     int i;
1.1       noro      869:
1.162     noro      870:     if ( nd_module ) {
                    871:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
1.167     noro      872:         error("ndl_add : invalid operation");
1.162     noro      873:     }
1.43      noro      874: #if 1
1.157     noro      875:     switch ( nd_wpd ) {
                    876:         case 2:
                    877:             TD(d) = TD(d1)+TD(d2);
                    878:             d[1] = d1[1]+d2[1];
                    879:             break;
                    880:         case 3:
                    881:             TD(d) = TD(d1)+TD(d2);
                    882:             d[1] = d1[1]+d2[1];
                    883:             d[2] = d1[2]+d2[2];
                    884:             break;
                    885:         default:
                    886:             for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    887:             break;
                    888:     }
1.43      noro      889: #else
1.157     noro      890:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
1.43      noro      891: #endif
1.6       noro      892: }
                    893:
1.55      noro      894: /* d1 += d2 */
1.61      noro      895: INLINE void ndl_addto(UINT *d1,UINT *d2)
1.55      noro      896: {
1.157     noro      897:     int i;
1.55      noro      898:
1.162     noro      899:     if ( nd_module ) {
                    900:         if ( MPOS(d1) && MPOS(d2) && (MPOS(d1) != MPOS(d2)) )
                    901:             error("ndl_addto : invalid operation");
                    902:     }
1.55      noro      903: #if 1
1.157     noro      904:     switch ( nd_wpd ) {
                    905:         case 2:
                    906:             TD(d1) += TD(d2);
                    907:             d1[1] += d2[1];
                    908:             break;
                    909:         case 3:
                    910:             TD(d1) += TD(d2);
                    911:             d1[1] += d2[1];
                    912:             d1[2] += d2[2];
                    913:             break;
                    914:         default:
                    915:             for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    916:             break;
                    917:     }
1.55      noro      918: #else
1.157     noro      919:     for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
1.55      noro      920: #endif
                    921: }
                    922:
1.61      noro      923: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
1.6       noro      924: {
1.157     noro      925:     int i;
1.6       noro      926:
1.157     noro      927:     for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
1.1       noro      928: }
                    929:
1.61      noro      930: int ndl_disjoint(UINT *d1,UINT *d2)
1.1       noro      931: {
1.157     noro      932:     UINT t1,t2,u,u1,u2;
                    933:     int i,j;
1.1       noro      934:
1.157     noro      935:     if ( nd_module && (MPOS(d1) == MPOS(d2)) ) return 0;
1.65      noro      936: #if USE_UNROLL
1.157     noro      937:     switch ( nd_bpe ) {
                    938:         case 3:
                    939:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    940:                 u1 = d1[i]; u2 = d2[i];
                    941:                 t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
                    942:                 t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
                    943:                 t1 = u1&  0xe00000; t2 = u2&  0xe00000; if ( t1&&t2 ) return 0;
                    944:                 t1 = u1&  0x1c0000; t2 = u2&  0x1c0000; if ( t1&&t2 ) return 0;
                    945:                 t1 = u1&   0x38000; t2 = u2&   0x38000; if ( t1&&t2 ) return 0;
                    946:                 t1 = u1&    0x7000; t2 = u2&    0x7000; if ( t1&&t2 ) return 0;
                    947:                 t1 = u1&     0xe00; t2 = u2&     0xe00; if ( t1&&t2 ) return 0;
                    948:                 t1 = u1&     0x1c0; t2 = u2&     0x1c0; if ( t1&&t2 ) return 0;
                    949:                 t1 = u1&      0x38; t2 = u2&      0x38; if ( t1&&t2 ) return 0;
                    950:                 t1 = u1&       0x7; t2 = u2&       0x7; if ( t1&&t2 ) return 0;
                    951:             }
                    952:             return 1;
                    953:             break;
                    954:         case 4:
                    955:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    956:                 u1 = d1[i]; u2 = d2[i];
                    957:                 t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
                    958:                 t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
                    959:                 t1 = u1&  0xf00000; t2 = u2&  0xf00000; if ( t1&&t2 ) return 0;
                    960:                 t1 = u1&   0xf0000; t2 = u2&   0xf0000; if ( t1&&t2 ) return 0;
                    961:                 t1 = u1&    0xf000; t2 = u2&    0xf000; if ( t1&&t2 ) return 0;
                    962:                 t1 = u1&     0xf00; t2 = u2&     0xf00; if ( t1&&t2 ) return 0;
                    963:                 t1 = u1&      0xf0; t2 = u2&      0xf0; if ( t1&&t2 ) return 0;
                    964:                 t1 = u1&       0xf; t2 = u2&       0xf; if ( t1&&t2 ) return 0;
                    965:             }
                    966:             return 1;
                    967:             break;
                    968:         case 6:
                    969:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    970:                 u1 = d1[i]; u2 = d2[i];
                    971:                 t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
                    972:                 t1 = u1&  0xfc0000; t2 = u2&  0xfc0000; if ( t1&&t2 ) return 0;
                    973:                 t1 = u1&   0x3f000; t2 = u2&   0x3f000; if ( t1&&t2 ) return 0;
                    974:                 t1 = u1&     0xfc0; t2 = u2&     0xfc0; if ( t1&&t2 ) return 0;
                    975:                 t1 = u1&      0x3f; t2 = u2&      0x3f; if ( t1&&t2 ) return 0;
                    976:             }
                    977:             return 1;
                    978:             break;
                    979:         case 8:
                    980:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    981:                 u1 = d1[i]; u2 = d2[i];
                    982:                 t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
                    983:                 t1 = u1&  0xff0000; t2 = u2&  0xff0000; if ( t1&&t2 ) return 0;
                    984:                 t1 = u1&    0xff00; t2 = u2&    0xff00; if ( t1&&t2 ) return 0;
                    985:                 t1 = u1&      0xff; t2 = u2&      0xff; if ( t1&&t2 ) return 0;
                    986:             }
                    987:             return 1;
                    988:             break;
                    989:         case 16:
                    990:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    991:                 u1 = d1[i]; u2 = d2[i];
                    992:                 t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
                    993:                 t1 = u1&    0xffff; t2 = u2&    0xffff; if ( t1&&t2 ) return 0;
                    994:             }
                    995:             return 1;
                    996:             break;
                    997:         case 32:
                    998:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                    999:                 if ( d1[i] && d2[i] ) return 0;
                   1000:             return 1;
                   1001:             break;
                   1002:         default:
                   1003:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1004:                 u1 = d1[i]; u2 = d2[i];
                   1005:                 for ( j = 0; j < nd_epw; j++ ) {
                   1006:                     if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                   1007:                     u1 >>= nd_bpe; u2 >>= nd_bpe;
                   1008:                 }
                   1009:             }
                   1010:             return 1;
                   1011:             break;
                   1012:     }
1.65      noro     1013: #else
1.157     noro     1014:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1015:         u1 = d1[i]; u2 = d2[i];
                   1016:         for ( j = 0; j < nd_epw; j++ ) {
                   1017:             if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                   1018:             u1 >>= nd_bpe; u2 >>= nd_bpe;
                   1019:         }
                   1020:     }
                   1021:     return 1;
1.65      noro     1022: #endif
1.1       noro     1023: }
                   1024:
1.114     noro     1025: int ndl_check_bound(UINT *d1,UINT *d2)
1.1       noro     1026: {
1.157     noro     1027:     UINT u2;
                   1028:     int i,j,ind,k;
1.1       noro     1029:
1.157     noro     1030:     ind = 0;
1.65      noro     1031: #if USE_UNROLL
1.157     noro     1032:     switch ( nd_bpe ) {
                   1033:         case 3:
                   1034:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1035:                 u2 = d2[i];
                   1036:                 if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
                   1037:                 if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
                   1038:                 if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
                   1039:                 if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
                   1040:                 if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
                   1041:                 if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
                   1042:                 if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
                   1043:                 if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
                   1044:                 if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
                   1045:                 if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
                   1046:             }
                   1047:             return 0;
                   1048:             break;
                   1049:         case 4:
                   1050:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1051:                 u2 = d2[i];
                   1052:                 if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
                   1053:                 if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
                   1054:                 if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
                   1055:                 if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
                   1056:                 if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
                   1057:                 if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
                   1058:                 if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
                   1059:                 if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
                   1060:             }
                   1061:             return 0;
                   1062:             break;
                   1063:         case 6:
                   1064:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1065:                 u2 = d2[i];
                   1066:                 if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
                   1067:                 if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
                   1068:                 if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
                   1069:                 if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
                   1070:                 if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
                   1071:             }
                   1072:             return 0;
                   1073:             break;
                   1074:         case 8:
                   1075:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1076:                 u2 = d2[i];
                   1077:                 if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
                   1078:                 if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
                   1079:                 if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
                   1080:                 if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
                   1081:             }
                   1082:             return 0;
                   1083:             break;
                   1084:         case 16:
                   1085:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1086:                 u2 = d2[i];
                   1087:                 if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
                   1088:                 if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
                   1089:             }
                   1090:             return 0;
                   1091:             break;
                   1092:         case 32:
                   1093:             for ( i = nd_exporigin; i < nd_wpd; i++ )
                   1094:                 if ( d1[i]+d2[i]<d1[i] ) return 1;
                   1095:             return 0;
                   1096:             break;
                   1097:         default:
                   1098:             for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1099:                 u2 = d2[i];
                   1100:                 k = (nd_epw-1)*nd_bpe;
                   1101:                 for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1102:                     if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1103:             }
                   1104:             return 0;
                   1105:             break;
                   1106:     }
1.65      noro     1107: #else
1.157     noro     1108:     for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1109:         u2 = d2[i];
                   1110:         k = (nd_epw-1)*nd_bpe;
                   1111:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1112:             if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1113:     }
                   1114:     return 0;
1.65      noro     1115: #endif
1.1       noro     1116: }
                   1117:
1.114     noro     1118: int ndl_check_bound2(int index,UINT *d2)
                   1119: {
1.157     noro     1120:     return ndl_check_bound(nd_bound[index],d2);
1.114     noro     1121: }
                   1122:
1.61      noro     1123: INLINE int ndl_hash_value(UINT *d)
1.1       noro     1124: {
1.157     noro     1125:     int i;
                   1126:     int r;
1.1       noro     1127:
1.157     noro     1128:     r = 0;
                   1129:     for ( i = 0; i < nd_wpd; i++ )
                   1130:         r = ((r<<16)+d[i])%REDTAB_LEN;
                   1131:     return r;
1.1       noro     1132: }
                   1133:
1.63      noro     1134: INLINE int ndl_find_reducer(UINT *dg)
1.1       noro     1135: {
1.157     noro     1136:     RHist r;
                   1137:     int d,k,i;
1.1       noro     1138:
1.157     noro     1139:     d = ndl_hash_value(dg);
                   1140:     for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
                   1141:         if ( ndl_equal(dg,DL(r)) ) {
                   1142:             if ( k > 0 ) nd_notfirst++;
                   1143:             nd_found++;
                   1144:             return r->index;
                   1145:         }
                   1146:     }
                   1147:     if ( Reverse )
                   1148:         for ( i = nd_psn-1; i >= 0; i-- ) {
                   1149:             r = nd_psh[i];
                   1150:             if ( ndl_reducible(dg,DL(r)) ) {
                   1151:                 nd_create++;
                   1152:                 nd_append_red(dg,i);
                   1153:                 return i;
                   1154:             }
                   1155:         }
                   1156:     else
                   1157:         for ( i = 0; i < nd_psn; i++ ) {
                   1158:             r = nd_psh[i];
                   1159:             if ( ndl_reducible(dg,DL(r)) ) {
                   1160:                 nd_create++;
                   1161:                 nd_append_red(dg,i);
                   1162:                 return i;
                   1163:             }
                   1164:         }
                   1165:     return -1;
1.1       noro     1166: }
                   1167:
1.63      noro     1168: ND nd_merge(ND p1,ND p2)
                   1169: {
1.157     noro     1170:     int n,c;
                   1171:     int t,can,td1,td2;
                   1172:     ND r;
                   1173:     NM m1,m2,mr0,mr,s;
                   1174:
                   1175:     if ( !p1 ) return p2;
                   1176:     else if ( !p2 ) return p1;
                   1177:     else {
                   1178:         can = 0;
                   1179:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1180:             c = DL_COMPARE(DL(m1),DL(m2));
                   1181:             switch ( c ) {
                   1182:                 case 0:
                   1183:                     s = m1; m1 = NEXT(m1);
                   1184:                     can++; NEXTNM2(mr0,mr,s);
                   1185:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1186:                     break;
                   1187:                 case 1:
                   1188:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1189:                     break;
                   1190:                 case -1:
                   1191:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1192:                     break;
                   1193:             }
                   1194:         }
                   1195:         if ( !mr0 )
                   1196:             if ( m1 ) mr0 = m1;
                   1197:             else if ( m2 ) mr0 = m2;
                   1198:             else return 0;
                   1199:         else if ( m1 ) NEXT(mr) = m1;
                   1200:         else if ( m2 ) NEXT(mr) = m2;
                   1201:         else NEXT(mr) = 0;
                   1202:         BDY(p1) = mr0;
                   1203:         SG(p1) = MAX(SG(p1),SG(p2));
                   1204:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1205:         FREEND(p2);
                   1206:         return p1;
                   1207:     }
1.63      noro     1208: }
                   1209:
1.31      noro     1210: ND nd_add(int mod,ND p1,ND p2)
1.1       noro     1211: {
1.157     noro     1212:     int n,c;
                   1213:     int t,can,td1,td2;
                   1214:     ND r;
                   1215:     NM m1,m2,mr0,mr,s;
                   1216:
                   1217:     if ( !p1 ) return p2;
                   1218:     else if ( !p2 ) return p1;
                   1219:     else if ( mod == -1 ) return nd_add_sf(p1,p2);
1.232     noro     1220:     else if ( mod == -2 ) return nd_add_lf(p1,p2);
1.157     noro     1221:     else if ( !mod ) return nd_add_q(p1,p2);
                   1222:     else {
                   1223:         can = 0;
                   1224:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1225:             c = DL_COMPARE(DL(m1),DL(m2));
                   1226:             switch ( c ) {
                   1227:                 case 0:
                   1228:                     t = ((CM(m1))+(CM(m2))) - mod;
                   1229:                     if ( t < 0 ) t += mod;
                   1230:                     s = m1; m1 = NEXT(m1);
                   1231:                     if ( t ) {
                   1232:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1233:                     } else {
                   1234:                         can += 2; FREENM(s);
                   1235:                     }
                   1236:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1237:                     break;
                   1238:                 case 1:
                   1239:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1240:                     break;
                   1241:                 case -1:
                   1242:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1243:                     break;
                   1244:             }
                   1245:         }
                   1246:         if ( !mr0 )
                   1247:             if ( m1 ) mr0 = m1;
                   1248:             else if ( m2 ) mr0 = m2;
                   1249:             else return 0;
                   1250:         else if ( m1 ) NEXT(mr) = m1;
                   1251:         else if ( m2 ) NEXT(mr) = m2;
                   1252:         else NEXT(mr) = 0;
                   1253:         BDY(p1) = mr0;
                   1254:         SG(p1) = MAX(SG(p1),SG(p2));
                   1255:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1256:         FREEND(p2);
                   1257:         return p1;
                   1258:     }
1.95      noro     1259: }
                   1260:
                   1261: /* XXX on opteron, the inlined manipulation of destructive additon of
                   1262:  * two NM seems to make gcc optimizer get confused, so the part is
                   1263:  * done in a function.
                   1264:  */
                   1265:
1.113     noro     1266: int nm_destructive_add_q(NM *m1,NM *m2,NM *mr0,NM *mr)
1.95      noro     1267: {
1.157     noro     1268:     NM s;
                   1269:     P t;
                   1270:     int can;
                   1271:
                   1272:     addp(nd_vc,CP(*m1),CP(*m2),&t);
                   1273:     s = *m1; *m1 = NEXT(*m1);
                   1274:     if ( t ) {
                   1275:         can = 1; NEXTNM2(*mr0,*mr,s); CP(*mr) = (t);
                   1276:     } else {
                   1277:         can = 2; FREENM(s);
                   1278:     }
                   1279:     s = *m2; *m2 = NEXT(*m2); FREENM(s);
                   1280:     return can;
1.95      noro     1281: }
                   1282:
1.113     noro     1283: ND nd_add_q(ND p1,ND p2)
1.95      noro     1284: {
1.157     noro     1285:     int n,c,can;
                   1286:     ND r;
                   1287:     NM m1,m2,mr0,mr,s;
                   1288:     P t;
                   1289:
                   1290:     if ( !p1 ) return p2;
                   1291:     else if ( !p2 ) return p1;
                   1292:     else {
                   1293:         can = 0;
                   1294:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1295:             c = DL_COMPARE(DL(m1),DL(m2));
                   1296:             switch ( c ) {
                   1297:                 case 0:
1.95      noro     1298: #if defined(__x86_64__)
1.157     noro     1299:                     can += nm_destructive_add_q(&m1,&m2,&mr0,&mr);
1.95      noro     1300: #else
1.157     noro     1301:                     addp(nd_vc,CP(m1),CP(m2),&t);
                   1302:                     s = m1; m1 = NEXT(m1);
                   1303:                     if ( t ) {
                   1304:                         can++; NEXTNM2(mr0,mr,s); CP(mr) = (t);
                   1305:                     } else {
                   1306:                         can += 2; FREENM(s);
                   1307:                     }
                   1308:                     s = m2; m2 = NEXT(m2); FREENM(s);
1.95      noro     1309: #endif
1.157     noro     1310:                     break;
                   1311:                 case 1:
                   1312:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1313:                     break;
                   1314:                 case -1:
                   1315:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1316:                     break;
                   1317:             }
                   1318:         }
                   1319:         if ( !mr0 )
                   1320:             if ( m1 ) mr0 = m1;
                   1321:             else if ( m2 ) mr0 = m2;
                   1322:             else return 0;
                   1323:         else if ( m1 ) NEXT(mr) = m1;
                   1324:         else if ( m2 ) NEXT(mr) = m2;
                   1325:         else NEXT(mr) = 0;
                   1326:         BDY(p1) = mr0;
                   1327:         SG(p1) = MAX(SG(p1),SG(p2));
                   1328:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1329:         FREEND(p2);
                   1330:         return p1;
                   1331:     }
1.17      noro     1332: }
                   1333:
1.71      noro     1334: ND nd_add_sf(ND p1,ND p2)
                   1335: {
1.157     noro     1336:     int n,c,can;
                   1337:     ND r;
                   1338:     NM m1,m2,mr0,mr,s;
                   1339:     int t;
                   1340:
                   1341:     if ( !p1 ) return p2;
                   1342:     else if ( !p2 ) return p1;
                   1343:     else {
                   1344:         can = 0;
                   1345:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1346:             c = DL_COMPARE(DL(m1),DL(m2));
                   1347:             switch ( c ) {
                   1348:                 case 0:
                   1349:                     t = _addsf(CM(m1),CM(m2));
                   1350:                     s = m1; m1 = NEXT(m1);
                   1351:                     if ( t ) {
                   1352:                         can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1353:                     } else {
                   1354:                         can += 2; FREENM(s);
                   1355:                     }
                   1356:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   1357:                     break;
                   1358:                 case 1:
                   1359:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1360:                     break;
                   1361:                 case -1:
                   1362:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1363:                     break;
                   1364:             }
                   1365:         }
                   1366:         if ( !mr0 )
                   1367:             if ( m1 ) mr0 = m1;
                   1368:             else if ( m2 ) mr0 = m2;
                   1369:             else return 0;
                   1370:         else if ( m1 ) NEXT(mr) = m1;
                   1371:         else if ( m2 ) NEXT(mr) = m2;
                   1372:         else NEXT(mr) = 0;
                   1373:         BDY(p1) = mr0;
                   1374:         SG(p1) = MAX(SG(p1),SG(p2));
                   1375:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1376:         FREEND(p2);
                   1377:         return p1;
                   1378:     }
1.71      noro     1379: }
                   1380:
1.232     noro     1381:
1.167     noro     1382: ND nd_reduce2(int mod,ND d,ND g,NDV p,NM mul,NDC dn,Obj *divp)
1.146     noro     1383: {
1.157     noro     1384:     int c,c1,c2;
                   1385:     Q cg,cred,gcd,tq;
                   1386:     P cgp,credp,gcdp;
                   1387:     Obj tr,tr1;
                   1388:
1.167     noro     1389:     if ( mod == -1 ) {
1.157     noro     1390:         CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1.167     noro     1391:         *divp = (Obj)ONE;
1.232     noro     1392:     } else if ( mod == -2 ) {
                   1393:         GZ inv,t;
                   1394:         divlf(ONEGZ,HCZ(p),&inv);
                   1395:         chsgnlf(HCZ(g),&t);
                   1396:         mullf(inv,t,&CZ(mul));
                   1397:         *divp = (Obj)ONE;
1.167     noro     1398:     } else if ( mod ) {
1.157     noro     1399:         c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1400:         DMAR(c1,c2,0,mod,c); CM(mul) = c;
1.167     noro     1401:         *divp = (Obj)ONE;
1.157     noro     1402:     } else if ( nd_vc ) {
                   1403:         ezgcdpz(nd_vc,HCP(g),HCP(p),&gcdp);
                   1404:         divsp(nd_vc,HCP(g),gcdp,&cgp); divsp(nd_vc,HCP(p),gcdp,&credp);
                   1405:         chsgnp(cgp,&CP(mul));
                   1406:         nd_mul_c_q(d,credp); nd_mul_c_q(g,credp);
                   1407:         if ( dn ) {
                   1408:             mulr(nd_vc,(Obj)dn->r,(Obj)credp,&tr);
                   1409:             reductr(nd_vc,tr,&tr1); dn->r = (R)tr1;
                   1410:         }
1.167     noro     1411:         *divp = (Obj)credp;
1.157     noro     1412:     } else {
                   1413:         igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
                   1414:         chsgnq(cg,&CQ(mul));
                   1415:         nd_mul_c_q(d,(P)cred); nd_mul_c_q(g,(P)cred);
                   1416:         if ( dn ) {
                   1417:             mulq(dn->z,cred,&tq); dn->z = tq;
                   1418:         }
1.167     noro     1419:         *divp = (Obj)cred;
1.157     noro     1420:     }
                   1421:     return nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1.146     noro     1422: }
                   1423:
1.1       noro     1424: /* ret=1 : success, ret=0 : overflow */
1.146     noro     1425: int nd_nf(int mod,ND d,ND g,NDV *ps,int full,NDC dn,ND *rp)
1.1       noro     1426: {
1.157     noro     1427:     NM m,mrd,tail;
                   1428:     NM mul;
                   1429:     int n,sugar,psugar,sugar0,stat,index;
                   1430:     int c,c1,c2,dummy;
                   1431:     RHist h;
                   1432:     NDV p,red;
1.167     noro     1433:     Q cg,cred,gcd,tq,qq,iq;
                   1434:     DP dmul;
                   1435:     NODE node;
                   1436:     LIST hist;
1.157     noro     1437:     double hmag;
                   1438:     P tp,tp1;
1.167     noro     1439:     Obj tr,tr1,div;
                   1440:     union oNDC hg;
                   1441:     P cont;
1.157     noro     1442:
                   1443:     if ( dn ) {
                   1444:         if ( mod )
                   1445:             dn->m = 1;
                   1446:         else if ( nd_vc )
                   1447:             dn->r = (R)ONE;
                   1448:         else
                   1449:             dn->z = ONE;
                   1450:     }
                   1451:     if ( !g ) {
                   1452:         *rp = d;
                   1453:         return 1;
                   1454:     }
                   1455:     if ( !mod ) hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1456:
                   1457:     sugar0 = sugar = SG(g);
                   1458:     n = NV(g);
                   1459:     mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1460:     if ( d )
                   1461:         for ( tail = BDY(d); NEXT(tail); tail = NEXT(tail) );
                   1462:     for ( ; g; ) {
                   1463:         index = ndl_find_reducer(HDL(g));
                   1464:         if ( index >= 0 ) {
                   1465:             h = nd_psh[index];
                   1466:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1467:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1468:                 nd_free(g); nd_free(d);
                   1469:                 return 0;
                   1470:             }
                   1471:             p = nd_demand ? ndv_load(index) : ps[index];
1.167     noro     1472:             /* d+g -> div*(d+g)+mul*p */
                   1473:             g = nd_reduce2(mod,d,g,p,mul,dn,&div);
1.172     noro     1474:             if ( nd_gentrace ) {
1.167     noro     1475:                 /* Trace=[div,index,mul,ONE] */
                   1476:                 STOQ(index,iq);
                   1477:                 nmtodp(mod,mul,&dmul);
                   1478:                 node = mknode(4,div,iq,dmul,ONE);
                   1479:             }
1.157     noro     1480:             sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1.193     noro     1481:             if ( !mod && g && !nd_vc && ((double)(p_mag(HCP(g))) > hmag) ) {
1.167     noro     1482:                 hg = HCU(g);
1.157     noro     1483:                 nd_removecont2(d,g);
1.172     noro     1484:                 if ( dn || nd_gentrace ) {
1.167     noro     1485:                     /* overwrite cont : Trace=[div,index,mul,cont] */
                   1486:                     cont = ndc_div(mod,hg,HCU(g));
                   1487:                     if ( dn ) {
                   1488:                         if ( nd_vc ) {
                   1489:                             divr(nd_vc,(Obj)dn->r,(Obj)cont,&tr);
                   1490:                             reductr(nd_vc,(Obj)tr,&tr1); dn->r = (R)tr1;
                   1491:                         } else divq(dn->z,(Q)cont,&dn->z);
1.157     noro     1492:                     }
1.172     noro     1493:                     if ( nd_gentrace && !UNIQ(cont) ) ARG3(node) = (pointer)cont;
1.157     noro     1494:                 }
                   1495:                 hmag = ((double)p_mag(HCP(g)))*nd_scale;
                   1496:             }
1.167     noro     1497:             MKLIST(hist,node);
                   1498:             MKNODE(node,hist,nd_tracelist); nd_tracelist = node;
1.157     noro     1499:         } else if ( !full ) {
                   1500:             *rp = g;
                   1501:             return 1;
                   1502:         } else {
                   1503:             m = BDY(g);
                   1504:             if ( NEXT(m) ) {
                   1505:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1506:             } else {
                   1507:                 FREEND(g); g = 0;
                   1508:             }
                   1509:             if ( d ) {
                   1510:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1511:             } else {
                   1512:                 MKND(n,m,1,d); tail = BDY(d);
                   1513:             }
                   1514:         }
                   1515:     }
                   1516:     if ( d ) SG(d) = sugar;
                   1517:     *rp = d;
                   1518:     return 1;
1.1       noro     1519: }
1.28      noro     1520:
1.53      noro     1521: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1.25      noro     1522: {
1.157     noro     1523:     int hindex,index;
                   1524:     NDV p;
                   1525:     ND u,d,red;
                   1526:     NODE l;
                   1527:     NM mul,m,mrd,tail;
                   1528:     int sugar,psugar,n,h_reducible;
                   1529:     PGeoBucket bucket;
                   1530:     int c,c1,c2;
                   1531:     Q cg,cred,gcd,zzz;
                   1532:     RHist h;
                   1533:     double hmag,gmag;
                   1534:     int count = 0;
                   1535:     int hcount = 0;
                   1536:
                   1537:     if ( !g ) {
                   1538:         *rp = 0;
                   1539:         return 1;
                   1540:     }
                   1541:     sugar = SG(g);
                   1542:     n = NV(g);
                   1543:     if ( !mod ) hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
                   1544:     bucket = create_pbucket();
                   1545:     add_pbucket(mod,bucket,g);
                   1546:     d = 0;
                   1547:     mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   1548:     while ( 1 ) {
1.232     noro     1549:         if ( mod > 0 || mod == -1 )
                   1550:           hindex = head_pbucket(mod,bucket);
                   1551:         else if ( mod == -2 )
                   1552:           hindex = head_pbucket_lf(bucket);
                   1553:         else
                   1554:           hindex = head_pbucket_q(bucket);
1.157     noro     1555:         if ( hindex < 0 ) {
                   1556:             if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
                   1557:             if ( d ) SG(d) = sugar;
                   1558:             *rp = d;
                   1559:             return 1;
                   1560:         }
                   1561:         g = bucket->body[hindex];
                   1562:         index = ndl_find_reducer(HDL(g));
                   1563:         if ( index >= 0 ) {
                   1564:             count++;
                   1565:             if ( !d ) hcount++;
                   1566:             h = nd_psh[index];
                   1567:             ndl_sub(HDL(g),DL(h),DL(mul));
                   1568:             if ( ndl_check_bound2(index,DL(mul)) ) {
                   1569:                 nd_free(d);
                   1570:                 free_pbucket(bucket);
                   1571:                 *rp = 0;
                   1572:                 return 0;
                   1573:             }
                   1574:             p = ps[index];
                   1575:             if ( mod == -1 )
                   1576:                 CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
1.232     noro     1577:             else if ( mod == -2 ) {
                   1578:                 GZ inv,t;
                   1579:                 divlf(ONEGZ,HCZ(p),&inv);
                   1580:                 chsgnlf(HCZ(g),&t);
                   1581:                 mullf(inv,t,&CZ(mul));
                   1582:             } else if ( mod ) {
1.157     noro     1583:                 c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1584:                 DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1585:             } else {
                   1586:                 igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
                   1587:                 chsgnq(cg,&CQ(mul));
                   1588:                 nd_mul_c_q(d,(P)cred);
                   1589:                 mulq_pbucket(bucket,cred);
                   1590:                 g = bucket->body[hindex];
                   1591:                 gmag = (double)p_mag((P)HCQ(g));
                   1592:             }
                   1593:             red = ndv_mul_nm(mod,mul,p);
                   1594:             bucket->body[hindex] = nd_remove_head(g);
                   1595:             red = nd_remove_head(red);
                   1596:             add_pbucket(mod,bucket,red);
                   1597:             psugar = SG(p)+TD(DL(mul));
                   1598:             sugar = MAX(sugar,psugar);
                   1599:             if ( !mod && hmag && (gmag > hmag) ) {
                   1600:                 g = normalize_pbucket(mod,bucket);
                   1601:                 if ( !g ) {
                   1602:                     if ( d ) SG(d) = sugar;
                   1603:                     *rp = d;
                   1604:                     return 1;
                   1605:                 }
                   1606:                 nd_removecont2(d,g);
                   1607:                 hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
                   1608:                 add_pbucket(mod,bucket,g);
                   1609:             }
                   1610:         } else if ( !full ) {
                   1611:             g = normalize_pbucket(mod,bucket);
                   1612:             if ( g ) SG(g) = sugar;
                   1613:             *rp = g;
                   1614:             return 1;
                   1615:         } else {
                   1616:             m = BDY(g);
                   1617:             if ( NEXT(m) ) {
                   1618:                 BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
                   1619:             } else {
                   1620:                 FREEND(g); g = 0;
                   1621:             }
                   1622:             bucket->body[hindex] = g;
                   1623:             NEXT(m) = 0;
                   1624:             if ( d ) {
                   1625:                 NEXT(tail)=m; tail=m; LEN(d)++;
                   1626:             } else {
                   1627:                 MKND(n,m,1,d); tail = BDY(d);
                   1628:             }
                   1629:         }
                   1630:     }
1.25      noro     1631: }
1.27      noro     1632:
1.61      noro     1633: /* input : list of NDV, cand : list of NDV */
1.28      noro     1634:
1.170     noro     1635: int ndv_check_membership(int m,NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1.28      noro     1636: {
1.157     noro     1637:     int n,i,stat;
                   1638:     ND nf,d;
                   1639:     NDV r;
                   1640:     NODE t,s;
                   1641:     union oNDC dn;
1.168     noro     1642:     Q q;
                   1643:     LIST list;
1.45      noro     1644:
1.172     noro     1645:     ndv_setup(m,0,cand,nd_gentrace?1:0,1);
1.157     noro     1646:     n = length(cand);
1.28      noro     1647:
1.172     noro     1648:        if ( nd_gentrace ) { nd_alltracelist = 0; nd_tracelist = 0; }
1.157     noro     1649:     /* membercheck : list is a subset of Id(cand) ? */
1.168     noro     1650:     for ( t = input, i = 0; t; t = NEXT(t), i++ ) {
1.45      noro     1651: again:
1.168     noro     1652:                nd_tracelist = 0;
1.157     noro     1653:         if ( nd_bpe > obpe )
                   1654:             r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
                   1655:         else
                   1656:             r = (NDV)BDY(t);
1.234   ! noro     1657:         if ( m ) ndv_mod(m,r);
1.171     noro     1658:         d = ndvtond(m,r);
                   1659:         stat = nd_nf(m,0,d,nd_ps,0,0,&nf);
1.157     noro     1660:         if ( !stat ) {
                   1661:             nd_reconstruct(0,0);
                   1662:             goto again;
                   1663:         } else if ( nf ) return 0;
1.172     noro     1664:                if ( nd_gentrace ) {
1.168     noro     1665:                        nd_tracelist = reverse_node(nd_tracelist);
                   1666:                        MKLIST(list,nd_tracelist);
                   1667:                        STOQ(i,q); s = mknode(2,q,list); MKLIST(list,s);
                   1668:                        MKNODE(s,list,nd_alltracelist);
                   1669:                        nd_alltracelist = s; nd_tracelist = 0;
                   1670:                }
1.157     noro     1671:         if ( DP_Print ) { printf("."); fflush(stdout); }
                   1672:     }
                   1673:     if ( DP_Print ) { printf("\n"); }
                   1674:     return 1;
1.23      noro     1675: }
1.1       noro     1676:
                   1677: ND nd_remove_head(ND p)
                   1678: {
1.157     noro     1679:     NM m;
1.1       noro     1680:
1.157     noro     1681:     m = BDY(p);
                   1682:     if ( !NEXT(m) ) {
                   1683:         FREEND(p); p = 0;
                   1684:     } else {
                   1685:         BDY(p) = NEXT(m); LEN(p)--;
                   1686:     }
                   1687:     FREENM(m);
                   1688:     return p;
1.1       noro     1689: }
                   1690:
1.69      noro     1691: ND nd_separate_head(ND p,ND *head)
                   1692: {
1.157     noro     1693:     NM m,m0;
                   1694:     ND r;
1.69      noro     1695:
1.157     noro     1696:     m = BDY(p);
                   1697:     if ( !NEXT(m) ) {
                   1698:         *head = p; p = 0;
                   1699:     } else {
                   1700:         m0 = m;
                   1701:         BDY(p) = NEXT(m); LEN(p)--;
                   1702:         NEXT(m0) = 0;
                   1703:         MKND(NV(p),m0,1,r);
                   1704:         *head = r;
                   1705:     }
                   1706:     return p;
1.69      noro     1707: }
                   1708:
1.1       noro     1709: PGeoBucket create_pbucket()
                   1710: {
                   1711:     PGeoBucket g;
1.157     noro     1712:
                   1713:     g = CALLOC(1,sizeof(struct oPGeoBucket));
                   1714:     g->m = -1;
                   1715:     return g;
1.1       noro     1716: }
                   1717:
1.25      noro     1718: void free_pbucket(PGeoBucket b) {
1.157     noro     1719:     int i;
1.25      noro     1720:
1.157     noro     1721:     for ( i = 0; i <= b->m; i++ )
                   1722:         if ( b->body[i] ) {
                   1723:             nd_free(b->body[i]);
                   1724:             b->body[i] = 0;
                   1725:         }
1.200     noro     1726:     GCFREE(b);
1.25      noro     1727: }
                   1728:
1.63      noro     1729: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   1730: {
1.157     noro     1731:     int l,i,k,m;
1.63      noro     1732:
1.157     noro     1733:     if ( !d )
                   1734:         return;
                   1735:     l = LEN(d);
                   1736:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   1737:     /* 2^(k-1) < l <= 2^k (=m) */
                   1738:     d = nd_merge(g->body[k],d);
                   1739:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   1740:         g->body[k] = 0;
                   1741:         d = nd_merge(g->body[k+1],d);
                   1742:     }
                   1743:     g->body[k] = d;
                   1744:     g->m = MAX(g->m,k);
1.63      noro     1745: }
                   1746:
1.31      noro     1747: void add_pbucket(int mod,PGeoBucket g,ND d)
1.1       noro     1748: {
1.157     noro     1749:     int l,i,k,m;
1.1       noro     1750:
1.157     noro     1751:     if ( !d )
                   1752:         return;
                   1753:     l = LEN(d);
                   1754:     for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   1755:     /* 2^(k-1) < l <= 2^k (=m) */
                   1756:     d = nd_add(mod,g->body[k],d);
                   1757:     for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   1758:         g->body[k] = 0;
                   1759:         d = nd_add(mod,g->body[k+1],d);
                   1760:     }
                   1761:     g->body[k] = d;
                   1762:     g->m = MAX(g->m,k);
1.1       noro     1763: }
                   1764:
1.113     noro     1765: void mulq_pbucket(PGeoBucket g,Q c)
1.26      noro     1766: {
1.157     noro     1767:     int k;
1.26      noro     1768:
1.157     noro     1769:     for ( k = 0; k <= g->m; k++ )
                   1770:         nd_mul_c_q(g->body[k],(P)c);
1.26      noro     1771: }
                   1772:
1.63      noro     1773: NM remove_head_pbucket_symbolic(PGeoBucket g)
                   1774: {
1.157     noro     1775:     int j,i,k,c;
                   1776:     NM head;
                   1777:
                   1778:     k = g->m;
                   1779:     j = -1;
                   1780:     for ( i = 0; i <= k; i++ ) {
                   1781:         if ( !g->body[i] ) continue;
                   1782:         if ( j < 0 ) j = i;
                   1783:         else {
                   1784:             c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
                   1785:             if ( c > 0 )
                   1786:                 j = i;
                   1787:             else if ( c == 0 )
                   1788:                 g->body[i] = nd_remove_head(g->body[i]);
                   1789:         }
                   1790:     }
                   1791:     if ( j < 0 ) return 0;
                   1792:     else {
                   1793:         head = BDY(g->body[j]);
                   1794:         if ( !NEXT(head) ) {
                   1795:             FREEND(g->body[j]);
                   1796:             g->body[j] = 0;
                   1797:         } else {
                   1798:             BDY(g->body[j]) = NEXT(head);
                   1799:             LEN(g->body[j])--;
                   1800:         }
                   1801:         return head;
                   1802:     }
1.63      noro     1803: }
                   1804:
1.19      noro     1805: int head_pbucket(int mod,PGeoBucket g)
1.1       noro     1806: {
1.157     noro     1807:     int j,i,c,k,nv,sum;
                   1808:     UINT *di,*dj;
                   1809:     ND gi,gj;
                   1810:
                   1811:     k = g->m;
                   1812:     while ( 1 ) {
                   1813:         j = -1;
                   1814:         for ( i = 0; i <= k; i++ ) {
                   1815:             if ( !(gi = g->body[i]) )
                   1816:                 continue;
                   1817:             if ( j < 0 ) {
                   1818:                 j = i;
                   1819:                 gj = g->body[j];
                   1820:                 dj = HDL(gj);
                   1821:                 sum = HCM(gj);
                   1822:             } else {
                   1823:                 c = DL_COMPARE(HDL(gi),dj);
                   1824:                 if ( c > 0 ) {
                   1825:                     if ( sum ) HCM(gj) = sum;
                   1826:                     else g->body[j] = nd_remove_head(gj);
                   1827:                     j = i;
                   1828:                     gj = g->body[j];
                   1829:                     dj = HDL(gj);
                   1830:                     sum = HCM(gj);
                   1831:                 } else if ( c == 0 ) {
                   1832:                     if ( mod == -1 )
                   1833:                         sum = _addsf(sum,HCM(gi));
                   1834:                     else {
                   1835:                         sum = sum+HCM(gi)-mod;
                   1836:                         if ( sum < 0 ) sum += mod;
                   1837:                     }
                   1838:                     g->body[i] = nd_remove_head(gi);
                   1839:                 }
                   1840:             }
                   1841:         }
                   1842:         if ( j < 0 ) return -1;
                   1843:         else if ( sum ) {
                   1844:             HCM(gj) = sum;
                   1845:             return j;
                   1846:         } else
                   1847:             g->body[j] = nd_remove_head(gj);
                   1848:     }
1.26      noro     1849: }
                   1850:
1.113     noro     1851: int head_pbucket_q(PGeoBucket g)
1.26      noro     1852: {
1.157     noro     1853:     int j,i,c,k,nv;
                   1854:     Q sum,t;
                   1855:     ND gi,gj;
                   1856:
                   1857:     k = g->m;
                   1858:     while ( 1 ) {
                   1859:         j = -1;
                   1860:         for ( i = 0; i <= k; i++ ) {
                   1861:             if ( !(gi = g->body[i]) ) continue;
                   1862:             if ( j < 0 ) {
                   1863:                 j = i;
                   1864:                 gj = g->body[j];
                   1865:                 sum = HCQ(gj);
                   1866:             } else {
                   1867:                 nv = NV(gi);
                   1868:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   1869:                 if ( c > 0 ) {
                   1870:                     if ( sum ) HCQ(gj) = sum;
                   1871:                     else g->body[j] = nd_remove_head(gj);
                   1872:                     j = i;
                   1873:                     gj = g->body[j];
                   1874:                     sum = HCQ(gj);
                   1875:                 } else if ( c == 0 ) {
                   1876:                     addq(sum,HCQ(gi),&t);
                   1877:                     sum = t;
                   1878:                     g->body[i] = nd_remove_head(gi);
                   1879:                 }
                   1880:             }
                   1881:         }
                   1882:         if ( j < 0 ) return -1;
                   1883:         else if ( sum ) {
                   1884:             HCQ(gj) = sum;
                   1885:             return j;
                   1886:         } else
                   1887:             g->body[j] = nd_remove_head(gj);
                   1888:     }
1.1       noro     1889: }
                   1890:
1.232     noro     1891: int head_pbucket_lf(PGeoBucket g)
                   1892: {
                   1893:     int j,i,c,k,nv;
                   1894:     GZ sum,t;
                   1895:     ND gi,gj;
                   1896:
                   1897:     k = g->m;
                   1898:     while ( 1 ) {
                   1899:         j = -1;
                   1900:         for ( i = 0; i <= k; i++ ) {
                   1901:             if ( !(gi = g->body[i]) ) continue;
                   1902:             if ( j < 0 ) {
                   1903:                 j = i;
                   1904:                 gj = g->body[j];
                   1905:                 sum = HCZ(gj);
                   1906:             } else {
                   1907:                 nv = NV(gi);
                   1908:                 c = DL_COMPARE(HDL(gi),HDL(gj));
                   1909:                 if ( c > 0 ) {
                   1910:                     if ( sum ) HCZ(gj) = sum;
                   1911:                     else g->body[j] = nd_remove_head(gj);
                   1912:                     j = i;
                   1913:                     gj = g->body[j];
                   1914:                     sum = HCZ(gj);
                   1915:                 } else if ( c == 0 ) {
                   1916:                     addlf(sum,HCZ(gi),&t);
                   1917:                     sum = t;
                   1918:                     g->body[i] = nd_remove_head(gi);
                   1919:                 }
                   1920:             }
                   1921:         }
                   1922:         if ( j < 0 ) return -1;
                   1923:         else if ( sum ) {
                   1924:             HCZ(gj) = sum;
                   1925:             return j;
                   1926:         } else
                   1927:             g->body[j] = nd_remove_head(gj);
                   1928:     }
                   1929: }
                   1930:
1.25      noro     1931: ND normalize_pbucket(int mod,PGeoBucket g)
1.1       noro     1932: {
1.157     noro     1933:     int i;
                   1934:     ND r,t;
1.1       noro     1935:
1.157     noro     1936:     r = 0;
                   1937:     for ( i = 0; i <= g->m; i++ ) {
                   1938:         r = nd_add(mod,r,g->body[i]);
                   1939:         g->body[i] = 0;
                   1940:     }
                   1941:     g->m = -1;
                   1942:     return r;
1.1       noro     1943: }
                   1944:
1.150     noro     1945: #if 0
                   1946: void register_hcf(NDV p)
                   1947: {
1.157     noro     1948:     DCP dc,t;
                   1949:     P hc,h;
                   1950:     int c;
                   1951:     NODE l,l1,prev;
                   1952:
                   1953:     hc = p->body->c.p;
                   1954:     if ( !nd_vc || NUM(hc) ) return;
                   1955:     fctrp(nd_vc,hc,&dc);
                   1956:     for ( t = dc; t; t = NEXT(t) ) {
                   1957:         h = t->c;
                   1958:         if ( NUM(h) ) continue;
                   1959:         for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   1960:             c = compp(nd_vc,h,(P)BDY(l));
                   1961:             if ( c >= 0 ) break;
                   1962:         }
                   1963:         if ( !l || c > 0  ) {
                   1964:             MKNODE(l1,h,l);
                   1965:             if ( !prev )
                   1966:                 nd_hcf = l1;
                   1967:             else
                   1968:                 NEXT(prev) = l1;
                   1969:         }
                   1970:     }
1.150     noro     1971: }
                   1972: #else
                   1973: void register_hcf(NDV p)
                   1974: {
1.157     noro     1975:     DCP dc,t;
                   1976:     P hc,h,q;
                   1977:     Q dmy;
                   1978:     int c;
                   1979:     NODE l,l1,prev;
                   1980:
                   1981:     hc = p->body->c.p;
                   1982:     if ( NUM(hc) ) return;
                   1983:     ptozp(hc,1,&dmy,&h);
1.150     noro     1984: #if 1
1.157     noro     1985:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   1986:         while ( 1 ) {
                   1987:             if ( divtpz(nd_vc,h,(P)BDY(l),&q) ) h = q;
                   1988:             else break;
                   1989:         }
                   1990:     }
                   1991:     if ( NUM(h) ) return;
1.150     noro     1992: #endif
1.157     noro     1993:     for ( prev = 0, l = nd_hcf; l; prev = l, l = NEXT(l) ) {
                   1994:         c = compp(nd_vc,h,(P)BDY(l));
                   1995:         if ( c >= 0 ) break;
                   1996:     }
                   1997:     if ( !l || c > 0  ) {
                   1998:         MKNODE(l1,h,l);
                   1999:         if ( !prev )
                   2000:             nd_hcf = l1;
                   2001:         else
                   2002:             NEXT(prev) = l1;
                   2003:     }
1.150     noro     2004: }
                   2005: #endif
                   2006:
1.122     noro     2007: int do_diagonalize(int sugar,int m)
1.92      noro     2008: {
1.157     noro     2009:     int i,nh,stat;
                   2010:     NODE r,g,t;
                   2011:     ND h,nf,s,head;
                   2012:     NDV nfv;
                   2013:     Q q,num,den;
1.167     noro     2014:     P nm,nmp,dn,mnp,dnp,cont,cont1;
                   2015:     union oNDC hc;
                   2016:     NODE node;
                   2017:     LIST l;
                   2018:     Q iq;
1.157     noro     2019:
                   2020:     for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
1.172     noro     2021:         if ( nd_gentrace ) {
1.167     noro     2022:             /* Trace = [1,index,1,1] */
                   2023:             STOQ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2024:             MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2025:         }
1.157     noro     2026:         if ( nd_demand )
                   2027:             nfv = ndv_load(i);
                   2028:         else
                   2029:             nfv = nd_ps[i];
                   2030:         s = ndvtond(m,nfv);
                   2031:         s = nd_separate_head(s,&head);
                   2032:         stat = nd_nf(m,head,s,nd_ps,1,0,&nf);
                   2033:         if ( !stat ) return 0;
                   2034:         ndv_free(nfv);
1.167     noro     2035:         hc = HCU(nf); nd_removecont(m,nf);
                   2036:         cont = ndc_div(m,hc,HCU(nf));
1.172     noro     2037:                if ( nd_gentrace ) finalize_tracelist(i,cont);
1.157     noro     2038:         nfv = ndtondv(m,nf);
                   2039:         nd_free(nf);
                   2040:         nd_bound[i] = ndv_compute_bound(nfv);
                   2041:         if ( !m ) register_hcf(nfv);
                   2042:         if ( nd_demand ) {
                   2043:             ndv_save(nfv,i);
                   2044:             ndv_free(nfv);
                   2045:         } else
                   2046:             nd_ps[i] = nfv;
                   2047:     }
                   2048:     return 1;
1.92      noro     2049: }
                   2050:
1.209     noro     2051: LIST compute_splist()
                   2052: {
                   2053:        NODE g,tn0,tn,node;
                   2054:        LIST l0;
                   2055:        ND_pairs d,t;
                   2056:        int i;
                   2057:        Q i1,i2;
                   2058:
                   2059:     g = 0; d = 0;
                   2060:     for ( i = 0; i < nd_psn; i++ ) {
                   2061:         d = update_pairs(d,g,i,0);
                   2062:         g = update_base(g,i);
                   2063:     }
                   2064:        for ( t = d, tn0 = 0; t; t = NEXT(t) ) {
                   2065:                NEXTNODE(tn0,tn);
                   2066:         STOQ(t->i1,i1); STOQ(t->i2,i2);
                   2067:         node = mknode(2,i1,i2); MKLIST(l0,node);
                   2068:                BDY(tn) = l0;
                   2069:        }
                   2070:        if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
                   2071:        return l0;
                   2072: }
                   2073:
1.27      noro     2074: /* return value = 0 => input is not a GB */
                   2075:
1.168     noro     2076: NODE nd_gb(int m,int ishomo,int checkonly,int gensyz,int **indp)
1.1       noro     2077: {
1.157     noro     2078:     int i,nh,sugar,stat;
                   2079:     NODE r,g,t;
                   2080:     ND_pairs d;
                   2081:     ND_pairs l;
                   2082:     ND h,nf,s,head,nf1;
                   2083:     NDV nfv;
                   2084:     Q q,num,den;
1.167     noro     2085:     union oNDC dn,hc;
1.157     noro     2086:     int diag_count = 0;
1.167     noro     2087:     P cont;
                   2088:     LIST list;
1.157     noro     2089:
                   2090:     g = 0; d = 0;
                   2091:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     2092:         d = update_pairs(d,g,i,gensyz);
1.157     noro     2093:         g = update_base(g,i);
                   2094:     }
                   2095:     sugar = 0;
                   2096:     while ( d ) {
1.1       noro     2097: again:
1.157     noro     2098:         l = nd_minp(d,&d);
1.228     noro     2099:         if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
1.157     noro     2100:         if ( SG(l) != sugar ) {
                   2101:             if ( ishomo ) {
                   2102:                 diag_count = 0;
                   2103:                 stat = do_diagonalize(sugar,m);
                   2104:                 if ( !stat ) {
                   2105:                     NEXT(l) = d; d = l;
                   2106:                     d = nd_reconstruct(0,d);
                   2107:                     goto again;
                   2108:                 }
                   2109:             }
                   2110:             sugar = SG(l);
                   2111:             if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2112:         }
                   2113:         stat = nd_sp(m,0,l,&h);
                   2114:         if ( !stat ) {
                   2115:             NEXT(l) = d; d = l;
                   2116:             d = nd_reconstruct(0,d);
                   2117:             goto again;
                   2118:         }
1.41      noro     2119: #if USE_GEOBUCKET
1.172     noro     2120:         stat = (m&&!nd_gentrace)?nd_nf_pbucket(m,h,nd_ps,!Top,&nf)
1.167     noro     2121:                :nd_nf(m,0,h,nd_ps,!Top,0,&nf);
1.41      noro     2122: #else
1.157     noro     2123:         stat = nd_nf(m,0,h,nd_ps,!Top,0,&nf);
1.41      noro     2124: #endif
1.157     noro     2125:         if ( !stat ) {
                   2126:             NEXT(l) = d; d = l;
                   2127:             d = nd_reconstruct(0,d);
                   2128:             goto again;
                   2129:         } else if ( nf ) {
1.168     noro     2130:             if ( checkonly || gensyz ) return 0;
1.192     noro     2131:                        if ( nd_newelim ) {
                   2132:                                if ( nd_module ) {
                   2133:                                        if ( MPOS(HDL(nf)) > 1 ) return 0;
                   2134:                                } else if ( !(HDL(nf)[nd_exporigin] & nd_mask[0]) ) return 0;
                   2135:                        }
1.157     noro     2136:             if ( DP_Print ) { printf("+"); fflush(stdout); }
1.167     noro     2137:             hc = HCU(nf);
1.157     noro     2138:             nd_removecont(m,nf);
                   2139:             if ( !m && nd_nalg ) {
                   2140:                 nd_monic(0,&nf);
                   2141:                 nd_removecont(m,nf);
                   2142:             }
1.172     noro     2143:             if ( nd_gentrace ) {
1.167     noro     2144:                                cont = ndc_div(m,hc,HCU(nf));
                   2145:                                if ( m || !UNIQ(cont) ) {
1.196     noro     2146:                     t = mknode(4,NULLP,NULLP,NULLP,cont);
1.167     noro     2147:                     MKLIST(list,t); MKNODE(t,list,nd_tracelist);
                   2148:                                        nd_tracelist = t;
                   2149:                                }
                   2150:             }
1.157     noro     2151:             nfv = ndtondv(m,nf); nd_free(nf);
1.215     noro     2152:             nh = ndv_newps(m,nfv,0,0);
1.157     noro     2153:             if ( !m && (ishomo && ++diag_count == diag_period) ) {
                   2154:                 diag_count = 0;
                   2155:                 stat = do_diagonalize(sugar,m);
                   2156:                 if ( !stat ) {
                   2157:                     NEXT(l) = d; d = l;
                   2158:                     d = nd_reconstruct(1,d);
                   2159:                     goto again;
                   2160:                 }
                   2161:             }
1.168     noro     2162:             d = update_pairs(d,g,nh,0);
1.157     noro     2163:             g = update_base(g,nh);
                   2164:             FREENDP(l);
                   2165:         } else {
1.172     noro     2166:                    if ( nd_gentrace && gensyz ) {
1.168     noro     2167:                 nd_tracelist = reverse_node(nd_tracelist);
                   2168:                                MKLIST(list,nd_tracelist);
                   2169:                 STOQ(-1,q); t = mknode(2,q,list); MKLIST(list,t);
                   2170:                 MKNODE(t,list,nd_alltracelist);
                   2171:                                nd_alltracelist = t; nd_tracelist = 0;
                   2172:                        }
1.157     noro     2173:             if ( DP_Print ) { printf("."); fflush(stdout); }
                   2174:             FREENDP(l);
                   2175:         }
                   2176:     }
1.167     noro     2177:        conv_ilist(nd_demand,0,g,indp);
1.157     noro     2178:     if ( !checkonly && DP_Print ) { printf("nd_gb done.\n"); fflush(stdout); }
                   2179:     return g;
1.1       noro     2180: }
                   2181:
1.209     noro     2182: /* splist = [[i1,i2],...] */
                   2183:
                   2184: int check_splist(int m,NODE splist)
                   2185: {
                   2186:        NODE t,p;
                   2187:        ND_pairs d,r,l;
                   2188:        int stat;
                   2189:        ND h,nf;
                   2190:
                   2191:        for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   2192:                p = BDY((LIST)BDY(t));
                   2193:         NEXTND_pairs(d,r);
                   2194:         r->i1 = QTOS((Q)ARG0(p)); r->i2 = QTOS((Q)ARG1(p));
                   2195:         ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
                   2196:                SG(r) = TD(LCM(r)); /* XXX */
                   2197:        }
                   2198:        if ( d ) NEXT(r) = 0;
                   2199:
                   2200:     while ( d ) {
                   2201: again:
                   2202:         l = nd_minp(d,&d);
                   2203:         stat = nd_sp(m,0,l,&h);
                   2204:         if ( !stat ) {
                   2205:             NEXT(l) = d; d = l;
                   2206:             d = nd_reconstruct(0,d);
                   2207:             goto again;
                   2208:         }
                   2209:         stat = nd_nf(m,0,h,nd_ps,!Top,0,&nf);
                   2210:         if ( !stat ) {
                   2211:             NEXT(l) = d; d = l;
                   2212:             d = nd_reconstruct(0,d);
                   2213:             goto again;
                   2214:         } else if ( nf ) return 0;
                   2215:                if ( DP_Print) { printf("."); fflush(stdout); }
                   2216:     }
                   2217:        if ( DP_Print) { printf("done.\n"); fflush(stdout); }
                   2218:        return 1;
                   2219: }
                   2220:
1.214     noro     2221: int check_splist_f4(int m,NODE splist)
                   2222: {
                   2223:        UINT *s0vect;
                   2224:     PGeoBucket bucket;
                   2225:        NODE p,rp0,t;
                   2226:        ND_pairs d,r,l,ll;
                   2227:        int col,stat;
                   2228:
                   2229:        for ( d = 0, t = splist; t; t = NEXT(t) ) {
                   2230:                p = BDY((LIST)BDY(t));
                   2231:         NEXTND_pairs(d,r);
                   2232:         r->i1 = QTOS((Q)ARG0(p)); r->i2 = QTOS((Q)ARG1(p));
                   2233:         ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
                   2234:                SG(r) = TD(LCM(r)); /* XXX */
                   2235:        }
                   2236:        if ( d ) NEXT(r) = 0;
                   2237:
                   2238:     while ( d ) {
                   2239:         l = nd_minsugarp(d,&d);
                   2240:         bucket = create_pbucket();
                   2241:         stat = nd_sp_f4(m,0,l,bucket);
                   2242:         if ( !stat ) {
                   2243:             for ( ll = l; NEXT(ll); ll = NEXT(ll) );
                   2244:             NEXT(ll) = d; d = l;
                   2245:             d = nd_reconstruct(0,d);
                   2246:             continue;
                   2247:         }
                   2248:         if ( bucket->m < 0 ) continue;
                   2249:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   2250:         if ( !col ) {
                   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 ( nd_f4_red(m,l,0,s0vect,col,rp0,0) ) return 0;
                   2257:     }
                   2258:     return 1;
                   2259: }
                   2260:
1.122     noro     2261: int do_diagonalize_trace(int sugar,int m)
1.91      noro     2262: {
1.157     noro     2263:     int i,nh,stat;
                   2264:     NODE r,g,t;
                   2265:     ND h,nf,nfq,s,head;
                   2266:     NDV nfv,nfqv;
                   2267:     Q q,den,num;
1.167     noro     2268:     union oNDC hc;
                   2269:     NODE node;
                   2270:     LIST l;
                   2271:     Q iq;
                   2272:     P cont,cont1;
1.157     noro     2273:
                   2274:     for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
1.172     noro     2275:         if ( nd_gentrace ) {
1.167     noro     2276:             /* Trace = [1,index,1,1] */
                   2277:             STOQ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2278:             MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2279:         }
1.157     noro     2280:         /* for nd_ps */
                   2281:         s = ndvtond(m,nd_ps[i]);
                   2282:         s = nd_separate_head(s,&head);
                   2283:         stat = nd_nf_pbucket(m,s,nd_ps,1,&nf);
                   2284:         if ( !stat ) return 0;
                   2285:         nf = nd_add(m,head,nf);
                   2286:         ndv_free(nd_ps[i]);
                   2287:         nd_ps[i] = ndtondv(m,nf);
                   2288:         nd_free(nf);
                   2289:
                   2290:         /* for nd_ps_trace */
                   2291:         if ( nd_demand )
                   2292:             nfv = ndv_load(i);
                   2293:         else
                   2294:             nfv = nd_ps_trace[i];
                   2295:         s = ndvtond(0,nfv);
                   2296:         s = nd_separate_head(s,&head);
                   2297:         stat = nd_nf(0,head,s,nd_ps_trace,1,0,&nf);
                   2298:         if ( !stat ) return 0;
                   2299:         ndv_free(nfv);
1.167     noro     2300:         hc = HCU(nf); nd_removecont(0,nf);
                   2301:                cont = ndc_div(0,hc,HCU(nf));
1.172     noro     2302:         if ( nd_gentrace ) finalize_tracelist(i,cont);
1.157     noro     2303:         nfv = ndtondv(0,nf);
                   2304:         nd_free(nf);
                   2305:         nd_bound[i] = ndv_compute_bound(nfv);
                   2306:         register_hcf(nfv);
                   2307:         if ( nd_demand ) {
                   2308:             ndv_save(nfv,i);
                   2309:             ndv_free(nfv);
                   2310:         } else
                   2311:             nd_ps_trace[i] = nfv;
                   2312:     }
                   2313:     return 1;
1.91      noro     2314: }
                   2315:
1.118     noro     2316: static struct oEGT eg_invdalg;
                   2317: struct oEGT eg_le;
                   2318:
1.147     noro     2319: void nd_subst_vector(VL vl,P p,NODE subst,P *r)
                   2320: {
1.157     noro     2321:     NODE tn;
                   2322:     P p1;
1.147     noro     2323:
1.157     noro     2324:     for ( tn = subst; tn; tn = NEXT(NEXT(tn)) ) {
                   2325:         substp(vl,p,BDY(tn),BDY(NEXT(tn)),&p1); p = p1;
                   2326:     }
                   2327:     *r = p;
1.147     noro     2328: }
                   2329:
1.167     noro     2330: NODE nd_gb_trace(int m,int ishomo,int **indp)
1.20      noro     2331: {
1.157     noro     2332:     int i,nh,sugar,stat;
                   2333:     NODE r,g,t;
                   2334:     ND_pairs d;
                   2335:     ND_pairs l;
                   2336:     ND h,nf,nfq,s,head;
                   2337:     NDV nfv,nfqv;
                   2338:     Q q,den,num;
                   2339:     P hc;
1.167     noro     2340:     union oNDC dn,hnfq;
1.157     noro     2341:     struct oEGT eg_monic,egm0,egm1;
                   2342:     int diag_count = 0;
1.167     noro     2343:     P cont;
                   2344:     LIST list;
1.157     noro     2345:
                   2346:     init_eg(&eg_monic);
                   2347:     init_eg(&eg_invdalg);
                   2348:     init_eg(&eg_le);
                   2349:     g = 0; d = 0;
                   2350:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     2351:         d = update_pairs(d,g,i,0);
1.157     noro     2352:         g = update_base(g,i);
                   2353:     }
                   2354:     sugar = 0;
                   2355:     while ( d ) {
1.20      noro     2356: again:
1.157     noro     2357:         l = nd_minp(d,&d);
1.228     noro     2358:         if ( MaxDeg > 0 && SG(l) > MaxDeg ) break;
1.157     noro     2359:         if ( SG(l) != sugar ) {
1.130     noro     2360: #if 1
1.157     noro     2361:             if ( ishomo ) {
                   2362:                 if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2363:                 stat = do_diagonalize_trace(sugar,m);
                   2364:                 if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2365:                 diag_count = 0;
                   2366:                 if ( !stat ) {
                   2367:                     NEXT(l) = d; d = l;
                   2368:                     d = nd_reconstruct(1,d);
                   2369:                     goto again;
                   2370:                 }
                   2371:             }
1.130     noro     2372: #endif
1.157     noro     2373:             sugar = SG(l);
                   2374:             if ( DP_Print ) fprintf(asir_out,"%d",sugar);
                   2375:         }
                   2376:         stat = nd_sp(m,0,l,&h);
                   2377:         if ( !stat ) {
                   2378:             NEXT(l) = d; d = l;
                   2379:             d = nd_reconstruct(1,d);
                   2380:             goto again;
                   2381:         }
1.41      noro     2382: #if USE_GEOBUCKET
1.157     noro     2383:         stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.41      noro     2384: #else
1.157     noro     2385:         stat = nd_nf(m,0,h,nd_ps,!Top,0,&nf);
1.41      noro     2386: #endif
1.157     noro     2387:         if ( !stat ) {
                   2388:             NEXT(l) = d; d = l;
                   2389:             d = nd_reconstruct(1,d);
                   2390:             goto again;
                   2391:         } else if ( nf ) {
                   2392:             if ( nd_demand ) {
                   2393:                 nfqv = ndv_load(nd_psn);
                   2394:                 nfq = ndvtond(0,nfqv);
                   2395:             } else
                   2396:                 nfq = 0;
                   2397:             if ( !nfq ) {
                   2398:                 if ( !nd_sp(0,1,l,&h) || !nd_nf(0,0,h,nd_ps_trace,!Top,0,&nfq) ) {
                   2399:                     NEXT(l) = d; d = l;
                   2400:                     d = nd_reconstruct(1,d);
                   2401:                     goto again;
                   2402:                 }
                   2403:             }
                   2404:             if ( nfq ) {
                   2405:                 /* m|HC(nfq) => failure */
                   2406:                 if ( nd_vc ) {
                   2407:                     nd_subst_vector(nd_vc,HCP(nfq),nd_subst,&hc); q = (Q)hc;
                   2408:                 } else
                   2409:                     q = HCQ(nfq);
                   2410:                 if ( !rem(NM(q),m) ) return 0;
                   2411:
                   2412:                 if ( DP_Print ) { printf("+"); fflush(stdout); }
1.167     noro     2413:                 hnfq = HCU(nfq);
1.157     noro     2414:                 if ( nd_nalg ) {
                   2415:                     /* m|DN(HC(nf)^(-1)) => failure */
                   2416:                     get_eg(&egm0);
                   2417:                     if ( !nd_monic(m,&nfq) ) return 0;
                   2418:                     get_eg(&egm1); add_eg(&eg_monic,&egm0,&egm1);
                   2419:                     nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   2420:                     nfv = ndv_dup(0,nfqv); ndv_mod(m,nfv); nd_free(nf);
                   2421:                 } else {
                   2422:                     nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   2423:                     nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
                   2424:                 }
1.172     noro     2425:                 if ( nd_gentrace ) {
1.167     noro     2426:                                   cont = ndc_div(0,hnfq,HCU(nfqv));
                   2427:                                   if ( !UNIQ(cont) ) {
1.196     noro     2428:                        t = mknode(4,NULLP,NULLP,NULLP,cont);
1.167     noro     2429:                        MKLIST(list,t); MKNODE(t,list,nd_tracelist);
                   2430:                                           nd_tracelist = t;
                   2431:                                   }
                   2432:                 }
1.215     noro     2433:                 nh = ndv_newps(0,nfv,nfqv,0);
1.157     noro     2434:                 if ( ishomo && ++diag_count == diag_period ) {
                   2435:                     diag_count = 0;
                   2436:                     if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2437:                     stat = do_diagonalize_trace(sugar,m);
                   2438:                     if ( DP_Print > 2 ) fprintf(asir_out,"|");
                   2439:                     if ( !stat ) {
                   2440:                         NEXT(l) = d; d = l;
                   2441:                         d = nd_reconstruct(1,d);
                   2442:                         goto again;
                   2443:                     }
                   2444:                 }
1.168     noro     2445:                 d = update_pairs(d,g,nh,0);
1.157     noro     2446:                 g = update_base(g,nh);
                   2447:             } else {
                   2448:                 if ( DP_Print ) { printf("*"); fflush(stdout); }
                   2449:             }
                   2450:         } else {
                   2451:             if ( DP_Print ) { printf("."); fflush(stdout); }
                   2452:         }
                   2453:         FREENDP(l);
                   2454:     }
                   2455:     if ( nd_nalg ) {
1.227     noro     2456:         if ( DP_Print ) {
                   2457:           print_eg("monic",&eg_monic);
                   2458:           print_eg("invdalg",&eg_invdalg);
                   2459:           print_eg("le",&eg_le);
                   2460:         }
1.157     noro     2461:     }
1.167     noro     2462:        conv_ilist(nd_demand,1,g,indp);
1.157     noro     2463:     if ( DP_Print ) { printf("nd_gb_trace done.\n"); fflush(stdout); }
                   2464:     return g;
1.20      noro     2465: }
                   2466:
1.23      noro     2467: int ndv_compare(NDV *p1,NDV *p2)
                   2468: {
1.157     noro     2469:     return DL_COMPARE(HDL(*p1),HDL(*p2));
1.23      noro     2470: }
                   2471:
                   2472: int ndv_compare_rev(NDV *p1,NDV *p2)
                   2473: {
1.157     noro     2474:     return -DL_COMPARE(HDL(*p1),HDL(*p2));
1.23      noro     2475: }
                   2476:
1.167     noro     2477: int ndvi_compare(NDVI p1,NDVI p2)
                   2478: {
                   2479:     return DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   2480: }
                   2481:
                   2482: int ndvi_compare_rev(NDVI p1,NDVI p2)
                   2483: {
                   2484:     return -DL_COMPARE(HDL(p1->p),HDL(p2->p));
                   2485: }
                   2486:
1.61      noro     2487: NODE ndv_reduceall(int m,NODE f)
1.23      noro     2488: {
1.167     noro     2489:     int i,j,n,stat;
1.157     noro     2490:     ND nf,g,head;
                   2491:     NODE t,a0,a;
                   2492:     union oNDC dn;
                   2493:     Q q,num,den;
1.167     noro     2494:     NODE node;
                   2495:        LIST l;
                   2496:        Q iq,jq;
                   2497:     int *perm;
                   2498:     union oNDC hc;
                   2499:     P cont,cont1;
1.23      noro     2500:
1.173     noro     2501:     if ( nd_nora ) return f;
1.157     noro     2502:     n = length(f);
                   2503:     ndv_setup(m,0,f,0,1);
1.167     noro     2504:        perm = (int *)MALLOC(n*sizeof(int));
1.172     noro     2505:        if ( nd_gentrace ) {
1.167     noro     2506:            for ( t = nd_tracelist, i = 0; i < n; i++, t = NEXT(t) )
                   2507:                    perm[i] = QTOS((Q)ARG1(BDY((LIST)BDY(t))));
                   2508:        }
1.157     noro     2509:     for ( i = 0; i < n; ) {
1.172     noro     2510:         if ( nd_gentrace ) {
1.167     noro     2511:             /* Trace = [1,index,1,1] */
                   2512:             STOQ(i,iq); node = mknode(4,ONE,iq,ONE,ONE);
                   2513:             MKLIST(l,node); MKNODE(nd_tracelist,l,0);
                   2514:         }
1.157     noro     2515:         g = ndvtond(m,nd_ps[i]);
                   2516:         g = nd_separate_head(g,&head);
                   2517:         stat = nd_nf(m,head,g,nd_ps,1,0,&nf);
                   2518:         if ( !stat )
                   2519:             nd_reconstruct(0,0);
                   2520:         else {
                   2521:             if ( DP_Print ) { printf("."); fflush(stdout); }
                   2522:             ndv_free(nd_ps[i]);
1.167     noro     2523:             hc = HCU(nf); nd_removecont(m,nf);
1.172     noro     2524:             if ( nd_gentrace ) {
1.167     noro     2525:                                for ( t = nd_tracelist; t; t = NEXT(t) ) {
                   2526:                     jq = ARG1(BDY((LIST)BDY(t))); j = QTOS(jq);
                   2527:                     STOQ(perm[j],jq); ARG1(BDY((LIST)BDY(t))) = jq;
                   2528:                 }
                   2529:                 cont = ndc_div(m,hc,HCU(nf));
                   2530:                 finalize_tracelist(perm[i],cont);
                   2531:             }
1.157     noro     2532:             nd_ps[i] = ndtondv(m,nf); nd_free(nf);
                   2533:             nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   2534:             i++;
                   2535:         }
                   2536:     }
                   2537:     if ( DP_Print ) { printf("\n"); }
                   2538:     for ( a0 = 0, i = 0; i < n; i++ ) {
                   2539:         NEXTNODE(a0,a);
1.172     noro     2540:                if ( !nd_gentrace ) BDY(a) = (pointer)nd_ps[i];
1.167     noro     2541:                else {
                   2542:                        for ( j = 0; j < n; j++ ) if ( perm[j] == i ) break;
                   2543:                        BDY(a) = (pointer)nd_ps[j];
                   2544:                }
1.157     noro     2545:     }
                   2546:     NEXT(a) = 0;
                   2547:     return a0;
1.23      noro     2548: }
                   2549:
1.168     noro     2550: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t, int gensyz)
1.1       noro     2551: {
1.157     noro     2552:     ND_pairs d1,nd,cur,head,prev,remove;
1.1       noro     2553:
1.157     noro     2554:     if ( !g ) return d;
1.168     noro     2555:        /* for testing */
1.172     noro     2556:        if ( gensyz && nd_gensyz == 2 ) {
1.168     noro     2557:        d1 = nd_newpairs(g,t);
                   2558:        if ( !d )
                   2559:                 return d1;
                   2560:        else {
                   2561:                 nd = d;
                   2562:                 while ( NEXT(nd) ) nd = NEXT(nd);
                   2563:                 NEXT(nd) = d1;
                   2564:                 return d;
                   2565:        }
                   2566:        }
1.157     noro     2567:     d = crit_B(d,t);
                   2568:     d1 = nd_newpairs(g,t);
                   2569:     d1 = crit_M(d1);
                   2570:     d1 = crit_F(d1);
1.168     noro     2571:     if ( gensyz || do_weyl )
1.157     noro     2572:         head = d1;
                   2573:     else {
                   2574:         prev = 0; cur = head = d1;
                   2575:         while ( cur ) {
                   2576:             if ( crit_2( cur->i1,cur->i2 ) ) {
                   2577:                 remove = cur;
                   2578:                 if ( !prev ) head = cur = NEXT(cur);
                   2579:                 else cur = NEXT(prev) = NEXT(cur);
                   2580:                 FREENDP(remove);
                   2581:             } else {
                   2582:                 prev = cur; cur = NEXT(cur);
                   2583:             }
                   2584:         }
                   2585:     }
                   2586:     if ( !d )
                   2587:         return head;
                   2588:     else {
                   2589:         nd = d;
                   2590:         while ( NEXT(nd) ) nd = NEXT(nd);
                   2591:         NEXT(nd) = head;
                   2592:         return d;
                   2593:     }
1.1       noro     2594: }
                   2595:
1.157     noro     2596:
1.1       noro     2597: ND_pairs nd_newpairs( NODE g, int t )
                   2598: {
1.157     noro     2599:     NODE h;
                   2600:     UINT *dl;
1.187     noro     2601:     int ts,s,i,t0,min,max;
1.157     noro     2602:     ND_pairs r,r0;
                   2603:
                   2604:     dl = DL(nd_psh[t]);
                   2605:     ts = SG(nd_psh[t]) - TD(dl);
1.195     noro     2606:        if ( nd_module && nd_intersect && (MPOS(dl) > 1) ) return 0;
1.157     noro     2607:     for ( r0 = 0, h = g; h; h = NEXT(h) ) {
1.159     noro     2608:         if ( nd_module && (MPOS(DL(nd_psh[(long)BDY(h)])) != MPOS(dl)) )
1.157     noro     2609:                 continue;
1.187     noro     2610:                if ( nd_gbblock ) {
                   2611:                        t0 = (long)BDY(h);
                   2612:                        for ( i = 0; nd_gbblock[i] >= 0; i += 2 ) {
                   2613:                                min = nd_gbblock[i]; max = nd_gbblock[i+1];
                   2614:                                if ( t0 >= min && t0 <= max && t >= min && t <= max )
                   2615:                                        break;
                   2616:                        }
1.188     noro     2617:                        if ( nd_gbblock[i] >= 0 )
1.187     noro     2618:                                continue;
                   2619:                }
1.157     noro     2620:         NEXTND_pairs(r0,r);
1.159     noro     2621:         r->i1 = (long)BDY(h);
1.157     noro     2622:         r->i2 = t;
                   2623:         ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
                   2624:         s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   2625:         SG(r) = MAX(s,ts) + TD(LCM(r));
1.231     noro     2626:         /* experimental */
                   2627:         if ( nd_sugarweight )
                   2628:             r->sugar2 = ndl_weight2(r->lcm);
1.157     noro     2629:     }
                   2630:     if ( r0 ) NEXT(r) = 0;
                   2631:     return r0;
1.1       noro     2632: }
                   2633:
1.231     noro     2634: /* ipair = [i1,i2],[i1,i2],... */
                   2635: ND_pairs nd_ipairtospair(NODE ipair)
                   2636: {
                   2637:   int s1,s2;
                   2638:   NODE tn,t;
                   2639:   ND_pairs r,r0;
                   2640:
                   2641:   for ( r0 = 0, t = ipair; t; t = NEXT(t) ) {
                   2642:     NEXTND_pairs(r0,r);
                   2643:     tn = BDY((LIST)BDY(t));
                   2644:     r->i1 = QTOS((Q)ARG0(tn));
                   2645:     r->i2 = QTOS((Q)ARG1(tn));
                   2646:     ndl_lcm(DL(nd_psh[r->i1]),DL(nd_psh[r->i2]),r->lcm);
                   2647:     s1 = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   2648:     s2 = SG(nd_psh[r->i2])-TD(DL(nd_psh[r->i2]));
                   2649:     SG(r) = MAX(s1,s2) + TD(LCM(r));
                   2650:     /* experimental */
                   2651:     if ( nd_sugarweight )
                   2652:         r->sugar2 = ndl_weight2(r->lcm);
                   2653:   }
                   2654:   if ( r0 ) NEXT(r) = 0;
                   2655:   return r0;
                   2656: }
                   2657:
1.157     noro     2658: /* kokokara */
                   2659:
1.1       noro     2660: ND_pairs crit_B( ND_pairs d, int s )
                   2661: {
1.157     noro     2662:     ND_pairs cur,head,prev,remove;
                   2663:     UINT *t,*tl,*lcm;
                   2664:     int td,tdl;
                   2665:
                   2666:     if ( !d ) return 0;
                   2667:     t = DL(nd_psh[s]);
                   2668:     prev = 0;
                   2669:     head = cur = d;
                   2670:     lcm = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   2671:     while ( cur ) {
                   2672:         tl = cur->lcm;
1.163     noro     2673:         if ( ndl_reducible(tl,t) ) {
                   2674:             ndl_lcm(DL(nd_psh[cur->i1]),t,lcm);
1.167     noro     2675:             if ( !ndl_equal(lcm,tl) ) {
                   2676:                 ndl_lcm(DL(nd_psh[cur->i2]),t,lcm);
                   2677:                 if (!ndl_equal(lcm,tl)) {
                   2678:                     remove = cur;
                   2679:                     if ( !prev ) {
                   2680:                         head = cur = NEXT(cur);
                   2681:                     } else {
                   2682:                         cur = NEXT(prev) = NEXT(cur);
                   2683:                     }
                   2684:                     FREENDP(remove);
                   2685:                 } else {
                   2686:                     prev = cur; cur = NEXT(cur);
                   2687:                 }
                   2688:             } else {
                   2689:                     prev = cur; cur = NEXT(cur);
                   2690:             }
1.157     noro     2691:         } else {
                   2692:             prev = cur; cur = NEXT(cur);
                   2693:         }
                   2694:     }
                   2695:     return head;
1.1       noro     2696: }
                   2697:
                   2698: ND_pairs crit_M( ND_pairs d1 )
                   2699: {
1.157     noro     2700:     ND_pairs e,d2,d3,dd,p;
                   2701:     UINT *id,*jd;
1.1       noro     2702:
1.157     noro     2703:     if ( !d1 ) return d1;
                   2704:     for ( dd = 0, e = d1; e; e = d3 ) {
                   2705:         if ( !(d2 = NEXT(e)) ) {
                   2706:             NEXT(e) = dd;
                   2707:             return e;
                   2708:         }
                   2709:         id = LCM(e);
                   2710:         for ( d3 = 0; d2; d2 = p ) {
                   2711:             p = NEXT(d2);
                   2712:             jd = LCM(d2);
                   2713:             if ( ndl_equal(jd,id) )
                   2714:                 ;
                   2715:             else if ( TD(jd) > TD(id) )
                   2716:                 if ( ndl_reducible(jd,id) ) continue;
                   2717:                 else ;
                   2718:             else if ( ndl_reducible(id,jd) ) goto delit;
                   2719:             NEXT(d2) = d3;
                   2720:             d3 = d2;
                   2721:         }
                   2722:         NEXT(e) = dd;
                   2723:         dd = e;
                   2724:         continue;
                   2725:         /**/
                   2726:     delit:    NEXT(d2) = d3;
                   2727:         d3 = d2;
                   2728:         for ( ; p; p = d2 ) {
                   2729:             d2 = NEXT(p);
                   2730:             NEXT(p) = d3;
                   2731:             d3 = p;
                   2732:         }
                   2733:         FREENDP(e);
                   2734:     }
                   2735:     return dd;
1.1       noro     2736: }
                   2737:
                   2738: ND_pairs crit_F( ND_pairs d1 )
                   2739: {
1.157     noro     2740:     ND_pairs rest, head,remove;
                   2741:     ND_pairs last, p, r, w;
                   2742:     int s;
                   2743:
                   2744:     if ( !d1 ) return d1;
                   2745:     for ( head = last = 0, p = d1; NEXT(p); ) {
                   2746:         r = w = equivalent_pairs(p,&rest);
                   2747:         s = SG(r);
                   2748:         w = NEXT(w);
                   2749:         while ( w ) {
                   2750:             if ( crit_2(w->i1,w->i2) ) {
                   2751:                 r = w;
                   2752:                 w = NEXT(w);
                   2753:                 while ( w ) {
                   2754:                     remove = w;
                   2755:                     w = NEXT(w);
                   2756:                     FREENDP(remove);
                   2757:                 }
                   2758:                 break;
                   2759:             } else if ( SG(w) < s ) {
                   2760:                 FREENDP(r);
                   2761:                 r = w;
                   2762:                 s = SG(r);
                   2763:                 w = NEXT(w);
                   2764:             } else {
                   2765:                 remove = w;
                   2766:                 w = NEXT(w);
                   2767:                 FREENDP(remove);
                   2768:             }
                   2769:         }
                   2770:         if ( last ) NEXT(last) = r;
                   2771:         else head = r;
                   2772:         NEXT(last = r) = 0;
                   2773:         p = rest;
                   2774:         if ( !p ) return head;
                   2775:     }
                   2776:     if ( !last ) return p;
                   2777:     NEXT(last) = p;
                   2778:     return head;
1.1       noro     2779: }
                   2780:
                   2781: int crit_2( int dp1, int dp2 )
                   2782: {
1.157     noro     2783:     return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
1.1       noro     2784: }
                   2785:
1.40      noro     2786: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
1.1       noro     2787: {
1.157     noro     2788:     ND_pairs w,p,r,s;
                   2789:     UINT *d;
1.1       noro     2790:
1.157     noro     2791:     w = d1;
                   2792:     d = LCM(w);
                   2793:     s = NEXT(w);
                   2794:     NEXT(w) = 0;
                   2795:     for ( r = 0; s; s = p ) {
                   2796:         p = NEXT(s);
                   2797:         if ( ndl_equal(d,LCM(s)) ) {
                   2798:             NEXT(s) = w; w = s;
                   2799:         } else {
                   2800:             NEXT(s) = r; r = s;
                   2801:         }
                   2802:     }
                   2803:     *prest = r;
                   2804:     return w;
1.1       noro     2805: }
                   2806:
                   2807: NODE update_base(NODE nd,int ndp)
                   2808: {
1.157     noro     2809:     UINT *dl, *dln;
                   2810:     NODE last, p, head;
1.1       noro     2811:
1.157     noro     2812:     dl = DL(nd_psh[ndp]);
                   2813:     for ( head = last = 0, p = nd; p; ) {
1.159     noro     2814:         dln = DL(nd_psh[(long)BDY(p)]);
1.157     noro     2815:         if ( ndl_reducible( dln, dl ) ) {
                   2816:             p = NEXT(p);
                   2817:             if ( last ) NEXT(last) = p;
                   2818:         } else {
                   2819:             if ( !last ) head = p;
                   2820:             p = NEXT(last = p);
                   2821:         }
                   2822:     }
                   2823:     head = append_one(head,ndp);
                   2824:     return head;
1.1       noro     2825: }
                   2826:
                   2827: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   2828: {
1.157     noro     2829:     ND_pairs m,ml,p,l;
                   2830:     UINT *lcm;
                   2831:     int s,td,len,tlen,c,c1;
                   2832:
                   2833:     if ( !(p = NEXT(m = d)) ) {
                   2834:         *prest = p;
                   2835:         NEXT(m) = 0;
                   2836:         return m;
                   2837:     }
                   2838:     if ( !NoSugar ) {
1.231     noro     2839:         if ( nd_sugarweight ) {
                   2840:             s = m->sugar2;
                   2841:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2842:                 if ( (p->sugar2 < s)
                   2843:                     || ((p->sugar2 == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   2844:                     ml = l; m = p; s = m->sugar2;
                   2845:                 }
                   2846:         } else {
                   2847:             s = SG(m);
                   2848:             for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2849:                 if ( (SG(p) < s)
                   2850:                     || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   2851:                     ml = l; m = p; s = SG(m);
                   2852:                 }
                   2853:         }
1.157     noro     2854:     } else {
                   2855:         for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2856:             if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
                   2857:                 ml = l; m = p; s = SG(m);
                   2858:             }
                   2859:     }
                   2860:     if ( !ml ) *prest = NEXT(m);
                   2861:     else {
                   2862:         NEXT(ml) = NEXT(m);
                   2863:         *prest = d;
                   2864:     }
                   2865:     NEXT(m) = 0;
                   2866:     return m;
1.1       noro     2867: }
                   2868:
1.63      noro     2869: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   2870: {
1.157     noro     2871:     int msugar,i;
                   2872:     ND_pairs t,dm0,dm,dr0,dr;
1.63      noro     2873:
1.231     noro     2874:     if ( nd_sugarweight ) {
                   2875:         for ( msugar = d->sugar2, t = NEXT(d); t; t = NEXT(t) )
                   2876:             if ( t->sugar2 < msugar ) msugar = t->sugar2;
                   2877:         dm0 = 0; dr0 = 0;
                   2878:         for ( i = 0, t = d; t; t = NEXT(t) )
                   2879:             if ( i < nd_f4_nsp && t->sugar2 == msugar ) {
                   2880:                 if ( dm0 ) NEXT(dm) = t;
                   2881:                 else dm0 = t;
                   2882:                 dm = t;
                   2883:                 i++;
                   2884:             } else {
                   2885:                 if ( dr0 ) NEXT(dr) = t;
                   2886:                 else dr0 = t;
                   2887:                 dr = t;
                   2888:             }
                   2889:     } else {
                   2890:         for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   2891:             if ( SG(t) < msugar ) msugar = SG(t);
                   2892:         dm0 = 0; dr0 = 0;
                   2893:         for ( i = 0, t = d; t; t = NEXT(t) )
                   2894:             if ( i < nd_f4_nsp && SG(t) == msugar ) {
                   2895:                 if ( dm0 ) NEXT(dm) = t;
                   2896:                 else dm0 = t;
                   2897:                 dm = t;
                   2898:                 i++;
                   2899:             } else {
                   2900:                 if ( dr0 ) NEXT(dr) = t;
                   2901:                 else dr0 = t;
                   2902:                 dr = t;
                   2903:             }
                   2904:     }
1.157     noro     2905:     NEXT(dm) = 0;
                   2906:     if ( dr0 ) NEXT(dr) = 0;
                   2907:     *prest = dr0;
                   2908:     return dm0;
1.63      noro     2909: }
                   2910:
1.215     noro     2911: int ndv_newps(int m,NDV a,NDV aq,int f4)
1.1       noro     2912: {
1.157     noro     2913:     int len;
                   2914:     RHist r;
                   2915:     NDV b;
1.167     noro     2916:     NODE tn;
                   2917:     LIST l;
                   2918:     Q iq;
1.157     noro     2919:
                   2920:     if ( nd_psn == nd_pslen ) {
                   2921:         nd_pslen *= 2;
                   2922:         nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
1.215     noro     2923:         nd_ps_gz = (NDV *)REALLOC((char *)nd_ps_gz,nd_pslen*sizeof(NDV));
1.157     noro     2924:         nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
                   2925:         nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
                   2926:         nd_bound = (UINT **)
                   2927:             REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
1.215     noro     2928:         nd_ps_sym = (NDV *)REALLOC((char *)nd_ps_sym,nd_pslen*sizeof(NDV));
                   2929:         nd_ps_trace_sym = (NDV *)REALLOC((char *)nd_ps_trace_sym,nd_pslen*sizeof(NDV));
1.157     noro     2930:     }
                   2931:     NEWRHist(r); nd_psh[nd_psn] = r;
                   2932:     nd_ps[nd_psn] = a;
                   2933:     if ( aq ) {
                   2934:         nd_ps_trace[nd_psn] = aq;
1.234   ! noro     2935:         if ( !m ) {
        !          2936:                  if ( !nd_vc ) nd_ps_gz[nd_psn] = ndvtondvgz(aq);
        !          2937:           register_hcf(aq);
        !          2938:         } else if ( m == -2 ) {
        !          2939:           /* do nothing */
        !          2940:         } else
        !          2941:           error("ndv_newps : invalud modulus");
1.157     noro     2942:         nd_bound[nd_psn] = ndv_compute_bound(aq);
                   2943:         SG(r) = SG(aq); ndl_copy(HDL(aq),DL(r));
                   2944:     } else {
                   2945:         if ( !m ) register_hcf(a);
                   2946:         nd_bound[nd_psn] = ndv_compute_bound(a);
                   2947:         SG(r) = SG(a); ndl_copy(HDL(a),DL(r));
1.217     noro     2948:                if ( !m && !nd_vc ) nd_ps_gz[nd_psn] = ndvtondvgz(a);
1.157     noro     2949:     }
                   2950:     if ( nd_demand ) {
                   2951:         if ( aq ) {
                   2952:             ndv_save(nd_ps_trace[nd_psn],nd_psn);
1.215     noro     2953:             nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
1.157     noro     2954:             nd_ps_trace[nd_psn] = 0;
                   2955:         } else {
                   2956:             ndv_save(nd_ps[nd_psn],nd_psn);
1.215     noro     2957:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
1.157     noro     2958:             nd_ps[nd_psn] = 0;
                   2959:         }
                   2960:     }
1.172     noro     2961:     if ( nd_gentrace ) {
1.167     noro     2962:         /* reverse the tracelist and append it to alltracelist */
                   2963:         nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
                   2964:         STOQ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
                   2965:         MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
                   2966:     }
1.157     noro     2967:     return nd_psn++;
1.1       noro     2968: }
                   2969:
1.167     noro     2970: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
1.177     noro     2971: /* return 1 if success, 0 if failure (HC(a mod p)) */
1.167     noro     2972:
1.177     noro     2973: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont)
1.1       noro     2974: {
1.157     noro     2975:     int i,j,td,len,max;
1.167     noro     2976:     NODE s,s0,f0,tn;
1.157     noro     2977:     UINT *d;
                   2978:     RHist r;
1.167     noro     2979:     NDVI w;
1.157     noro     2980:     NDV a,am;
1.167     noro     2981:     union oNDC hc;
                   2982:     NODE node;
                   2983:     P hcp;
                   2984:     Q iq,jq,hcq;
                   2985:     LIST l;
1.157     noro     2986:
                   2987:     nd_found = 0; nd_notfirst = 0; nd_create = 0;
1.167     noro     2988:     /* initialize the tracelist */
                   2989:     nd_tracelist = 0;
1.157     noro     2990:
                   2991:     for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
1.167     noro     2992:     w = (NDVI)ALLOCA(nd_psn*sizeof(struct oNDVI));
                   2993:     for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
                   2994:         if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
1.157     noro     2995:     if ( !dont_sort ) {
                   2996:         /* XXX heuristic */
                   2997:         if ( !nd_ord->id && (nd_ord->ord.simple<2) )
1.167     noro     2998:             qsort(w,nd_psn,sizeof(struct oNDVI),
                   2999:                 (int (*)(const void *,const void *))ndvi_compare_rev);
1.157     noro     3000:         else
1.167     noro     3001:             qsort(w,nd_psn,sizeof(struct oNDVI),
                   3002:                 (int (*)(const void *,const void *))ndvi_compare);
1.157     noro     3003:     }
                   3004:     nd_pslen = 2*nd_psn;
                   3005:     nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.215     noro     3006:     nd_ps_gz = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.157     noro     3007:     nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.215     noro     3008:     nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3009:     nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.157     noro     3010:     nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
                   3011:     nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
                   3012:     nd_hcf = 0;
                   3013:
                   3014:     if ( trace && nd_vc )
                   3015:         makesubst(nd_vc,&nd_subst);
                   3016:     else
                   3017:         nd_subst = 0;
                   3018:
                   3019:     if ( !nd_red )
                   3020:         nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   3021:     for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
                   3022:     for ( i = 0; i < nd_psn; i++ ) {
1.167     noro     3023:         hc = HCU(w[i].p);
1.157     noro     3024:         if ( trace ) {
1.234   ! noro     3025:             if ( mod == -2 ) {
        !          3026:               /* over a large finite field */
        !          3027:               /* trace = small modulus */
        !          3028:               a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
        !          3029:               ndv_mod(-2,a);
        !          3030:               if ( !dont_removecont) ndv_removecont(-2,a);
        !          3031:               am = nd_ps[i] = ndv_dup(trace,w[i].p);
        !          3032:               ndv_mod(trace,am);
        !          3033:                  if ( DL_COMPARE(HDL(am),HDL(a)) )
        !          3034:                      return 0;
        !          3035:               ndv_removecont(trace,am);
        !          3036:             } else {
        !          3037:               a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
        !          3038:                          if ( !nd_vc ) nd_ps_gz[i] = ndvtondvgz(a);
        !          3039:               if ( !dont_removecont) ndv_removecont(0,a);
        !          3040:               register_hcf(a);
        !          3041:               am = nd_ps[i] = ndv_dup(mod,a);
        !          3042:               ndv_mod(mod,am);
        !          3043:                  if ( DL_COMPARE(HDL(am),HDL(a)) )
        !          3044:                      return 0;
        !          3045:               ndv_removecont(mod,am);
        !          3046:             }
1.157     noro     3047:         } else {
1.167     noro     3048:             a = nd_ps[i] = ndv_dup(mod,w[i].p);
1.217     noro     3049:                        if ( !mod && !nd_vc ) nd_ps_gz[i] = ndvtondvgz(a);
1.157     noro     3050:             if ( mod || !dont_removecont ) ndv_removecont(mod,a);
                   3051:             if ( !mod ) register_hcf(a);
                   3052:         }
1.172     noro     3053:         if ( nd_gentrace ) {
1.167     noro     3054:             STOQ(i,iq); STOQ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
1.168     noro     3055:                        if ( !dont_removecont )
                   3056:                 ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
1.167     noro     3057:             MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
                   3058:         }
1.157     noro     3059:         NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
                   3060:         nd_bound[i] = ndv_compute_bound(a);
                   3061:         nd_psh[i] = r;
                   3062:         if ( nd_demand ) {
                   3063:             if ( trace ) {
                   3064:                 ndv_save(nd_ps_trace[i],i);
1.215     noro     3065:                 nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
1.157     noro     3066:                 nd_ps_trace[i] = 0;
                   3067:             } else {
                   3068:                 ndv_save(nd_ps[i],i);
1.215     noro     3069:                 nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
1.157     noro     3070:                 nd_ps[i] = 0;
                   3071:             }
                   3072:         }
                   3073:     }
1.172     noro     3074:     if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
1.177     noro     3075:     return 1;
1.20      noro     3076: }
                   3077:
1.119     noro     3078: struct order_spec *append_block(struct order_spec *spec,
                   3079:     int nv,int nalg,int ord);
                   3080:
1.121     noro     3081: extern VECT current_dl_weight_vector_obj;
                   3082: static VECT prev_weight_vector_obj;
                   3083:
1.120     noro     3084: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
1.157     noro     3085:     struct order_spec **ord1p,LIST *f1p,NODE *alistp)
1.120     noro     3086: {
1.157     noro     3087:     NODE alist,t,s,r0,r,arg;
                   3088:     VL tv;
                   3089:     P poly;
                   3090:     DP d;
                   3091:     Alg alpha,dp;
                   3092:     DAlg inv,da,hc;
                   3093:     MP m;
                   3094:     int i,nvar,nalg,n;
                   3095:     NumberField nf;
                   3096:     LIST f1,f2;
                   3097:     struct order_spec *current_spec;
                   3098:     VECT obj,obj0;
1.232     noro     3099:     VECT tmp;
1.157     noro     3100:
                   3101:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
                   3102:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
                   3103:
                   3104:     for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
                   3105:         NEXTNODE(alist,t); MKV(tv->v,poly);
                   3106:         MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
                   3107:         tv->v = tv->v->priv;
                   3108:     }
                   3109:     NEXT(t) = 0;
                   3110:
1.167     noro     3111:     /* simplification, making polynomials monic */
1.157     noro     3112:     setfield_dalg(alist);
1.232     noro     3113:     obj_algtodalg((Obj)f,(Obj *)&f1);
1.157     noro     3114:     for ( t = BDY(f); t; t = NEXT(t) ) {
                   3115:         initd(ord); ptod(vv,vv,(P)BDY(t),&d);
                   3116:         hc = (DAlg)BDY(d)->c;
                   3117:         if ( NID(hc) == N_DA ) {
                   3118:             invdalg(hc,&inv);
                   3119:             for ( m = BDY(d); m; m = NEXT(m) ) {
                   3120:                 muldalg(inv,(DAlg)m->c,&da); m->c = (P)da;
                   3121:             }
                   3122:         }
                   3123:         initd(ord); dtop(vv,vv,d,&poly); BDY(f) = (pointer)poly;
                   3124:     }
1.232     noro     3125:     obj_dalgtoalg((Obj)f1,(Obj *)&f);
1.157     noro     3126:
                   3127:     /* append alg vars to the var list */
                   3128:     for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
                   3129:     NEXT(tv) = av;
                   3130:
                   3131:     /* append a block to ord */
                   3132:     *ord1p = append_block(ord,nvar,nalg,2);
                   3133:
                   3134:     /* create generator list */
                   3135:     nf = get_numberfield();
                   3136:     for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
                   3137:         MKAlg(nf->defpoly[i],dp);
                   3138:         MKNODE(s,dp,t); t = s;
                   3139:     }
                   3140:     MKLIST(f1,t);
                   3141:     *alistp = alist;
1.232     noro     3142:     algobjtorat((Obj)f1,(Obj *)f1p);
1.157     noro     3143:
                   3144:     /* creating a new weight vector */
                   3145:     prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
                   3146:     n = nvar+nalg+1;
                   3147:     MKVECT(obj,n);
                   3148:     if ( obj0 && obj0->len == nvar )
                   3149:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
                   3150:     else
                   3151:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
                   3152:     for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
                   3153:     BDY(obj)[n-1] = (pointer)ONE;
                   3154:     arg = mknode(1,obj);
                   3155:     Pdp_set_weight(arg,&tmp);
1.121     noro     3156: }
                   3157:
                   3158: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
                   3159: {
1.157     noro     3160:     NODE s,t,u0,u;
                   3161:     P p;
                   3162:     VL tv;
1.232     noro     3163:     Obj obj;
                   3164:     VECT tmp;
1.157     noro     3165:     NODE arg;
                   3166:
                   3167:     u0 = 0;
                   3168:     for ( t = r; t; t = NEXT(t) ) {
                   3169:         p = (P)BDY(t);
                   3170:         for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
                   3171:             substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
                   3172:         }
                   3173:         if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
                   3174:             NEXTNODE(u0,u);
                   3175:             BDY(u) = (pointer)p;
                   3176:         }
                   3177:     }
                   3178:     arg = mknode(1,prev_weight_vector_obj);
                   3179:     Pdp_set_weight(arg,&tmp);
1.121     noro     3180:
1.157     noro     3181:     return u0;
1.120     noro     3182: }
                   3183:
1.199     noro     3184: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.1       noro     3185: {
1.157     noro     3186:     VL tv,fv,vv,vc,av;
                   3187:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   3188:     int e,max,nvar,i;
                   3189:     NDV b;
1.184     noro     3190:     int ishomo,nalg,mrank,trank,wmax,len;
                   3191:        NMV a;
1.157     noro     3192:     Alg alpha,dp;
                   3193:     P p,zp;
                   3194:     Q dmy;
                   3195:     LIST f1,f2,zpl;
                   3196:     Obj obj;
                   3197:     NumberField nf;
                   3198:     struct order_spec *ord1;
1.208     noro     3199:     NODE tr,tl1,tl2,tl3,tl4,nzlist;
1.170     noro     3200:     LIST l1,l2,l3,l4,l5;
1.167     noro     3201:        int j;
1.198     noro     3202:        Q jq,bpe;
1.167     noro     3203:     int *perm;
1.170     noro     3204:     EPOS oepos;
1.194     noro     3205:     int obpe,oadv,ompos,cbpe;
1.1       noro     3206:
1.174     noro     3207:     nd_module = 0;
1.157     noro     3208:     if ( !m && Demand ) nd_demand = 1;
                   3209:     else nd_demand = 0;
1.172     noro     3210:     parse_nd_option(current_option);
1.78      noro     3211:
1.157     noro     3212:     if ( DP_Multiple )
                   3213:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
1.103     noro     3214: #if 0
1.157     noro     3215:     ndv_alloc = 0;
1.103     noro     3216: #endif
1.157     noro     3217:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.229     noro     3218:     if ( m && nd_vc )
                   3219:        error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
1.157     noro     3220:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   3221:     switch ( ord->id ) {
                   3222:         case 1:
                   3223:             if ( ord->nv != nvar )
                   3224:                 error("nd_{gr,f4} : invalid order specification");
                   3225:             break;
                   3226:         default:
                   3227:             break;
                   3228:     }
                   3229:     nd_nalg = 0;
                   3230:     av = 0;
                   3231:     if ( !m ) {
                   3232:         get_algtree((Obj)f,&av);
                   3233:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   3234:         nd_ntrans = nvar;
                   3235:         nd_nalg = nalg;
                   3236:         /* #i -> t#i */
                   3237:         if ( nalg ) {
                   3238:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   3239:             ord = ord1;
                   3240:             f = f1;
                   3241:         }
                   3242:         nvar += nalg;
                   3243:     }
                   3244:     nd_init_ord(ord);
                   3245:     mrank = 0;
1.178     noro     3246:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
1.157     noro     3247:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   3248:             if ( nd_module ) {
                   3249:                 s = BDY((LIST)BDY(t));
                   3250:                 trank = length(s);
                   3251:                 mrank = MAX(mrank,trank);
                   3252:                 for ( ; s; s = NEXT(s) ) {
                   3253:                     e = getdeg(tv->v,(P)BDY(s));
                   3254:                     max = MAX(e,max);
                   3255:                 }
                   3256:             } else {
                   3257:                 e = getdeg(tv->v,(P)BDY(t));
                   3258:                 max = MAX(e,max);
                   3259:             }
                   3260:         }
1.208     noro     3261:     nd_setup_parameters(nvar,nd_nzlist?0:max);
1.170     noro     3262:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
1.157     noro     3263:     ishomo = 1;
                   3264:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.167     noro     3265:         if ( nd_module ) {
1.172     noro     3266:                        if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
1.167     noro     3267:                        else zpl = (LIST)BDY(t);
1.157     noro     3268:             b = (pointer)pltondv(CO,vv,zpl);
                   3269:         } else {
1.172     noro     3270:                        if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
1.167     noro     3271:                        else zp = (P)BDY(t);
1.157     noro     3272:             b = (pointer)ptondv(CO,vv,zp);
1.167     noro     3273:         }
1.157     noro     3274:         if ( ishomo )
                   3275:             ishomo = ishomo && ndv_ishomo(b);
                   3276:         if ( m ) ndv_mod(m,b);
                   3277:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   3278:     }
                   3279:     if ( fd0 ) NEXT(fd) = 0;
1.184     noro     3280:
                   3281:        if ( !ishomo && homo ) {
                   3282:         for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   3283:             b = (NDV)BDY(t); len = LEN(b);
                   3284:             for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   3285:                 wmax = MAX(TD(DL(a)),wmax);
                   3286:         }
                   3287:         homogenize_order(ord,nvar,&ord1);
                   3288:         nd_init_ord(ord1);
                   3289:         nd_setup_parameters(nvar+1,wmax);
                   3290:         for ( t = fd0; t; t = NEXT(t) )
                   3291:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   3292:     }
                   3293:
1.211     noro     3294:     ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0);
1.172     noro     3295:     if ( nd_gentrace ) {
1.167     noro     3296:         MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   3297:     }
1.209     noro     3298:        if ( nd_splist ) {
                   3299:                *rp = compute_splist();
                   3300:                return;
                   3301:        }
                   3302:        if ( nd_check_splist ) {
1.214     noro     3303:         if ( f4 ) {
                   3304:             if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
                   3305:             else *rp = 0;
                   3306:         } else {
                   3307:             if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
                   3308:             else *rp = 0;
                   3309:         }
1.209     noro     3310:                return;
                   3311:        }
1.234   ! noro     3312:     x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
1.192     noro     3313:        if ( !x ) {
                   3314:                *rp = 0; return;
                   3315:        }
1.184     noro     3316:        if ( !ishomo && homo ) {
                   3317:                /* dehomogenization */
                   3318:                for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   3319:                nd_init_ord(ord);
                   3320:                nd_setup_parameters(nvar,0);
                   3321:        }
1.157     noro     3322:     nd_demand = 0;
1.195     noro     3323:        if ( nd_module && nd_intersect ) {
                   3324:                for ( j = nd_psn-1, x = 0; j >= 0; j-- )
                   3325:                        if ( MPOS(DL(nd_psh[j])) > 1 ) {
                   3326:                                MKNODE(xx,(pointer)j,x); x = xx;
                   3327:                        }
                   3328:          conv_ilist(nd_demand,0,x,0);
                   3329:          goto FINAL;
                   3330:        }
1.208     noro     3331:     if ( nd_gentrace  && f4 ) { nzlist = nd_alltracelist; }
1.167     noro     3332:     x = ndv_reducebase(x,perm);
1.208     noro     3333:     if ( nd_gentrace  && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
1.157     noro     3334:     x = ndv_reduceall(m,x);
1.194     noro     3335:     cbpe = nd_bpe;
1.208     noro     3336:     if ( nd_gentrace && !f4 ) {
1.170     noro     3337:         tl2 = nd_alltracelist; nd_alltracelist = 0;
                   3338:         ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
1.198     noro     3339:         tl3 = nd_alltracelist; nd_alltracelist = 0;
                   3340:         if ( nd_gensyz ) {
                   3341:                nd_gb(m,0,1,1,0);
1.170     noro     3342:             tl4 = nd_alltracelist; nd_alltracelist = 0;
                   3343:         } else tl4 = 0;
                   3344:     }
1.194     noro     3345:     nd_bpe = cbpe;
                   3346:     nd_setup_parameters(nd_nvar,0);
1.195     noro     3347: FINAL:
1.157     noro     3348:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   3349:         NEXTNODE(r0,r);
1.194     noro     3350:         if ( nd_module ) BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
1.199     noro     3351:         else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
                   3352:                else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.157     noro     3353:     }
                   3354:     if ( r0 ) NEXT(r) = 0;
1.208     noro     3355:     if ( !m && nd_nalg )
1.157     noro     3356:         r0 = postprocess_algcoef(av,alist,r0);
                   3357:     MKLIST(*rp,r0);
1.172     noro     3358:     if ( nd_gentrace ) {
1.208     noro     3359:        if ( f4 ) {
                   3360:             STOQ(16,bpe);
                   3361:             tr = mknode(4,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe); MKLIST(*rp,tr);
                   3362:         } else {
                   3363:             tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   3364:             tl3 = reverse_node(tl3);
                   3365:             /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   3366:             for ( t = tl2; t; t = NEXT(t) ) {
                   3367:             /* s = [i,[*,j,*,*],...] */
                   3368:                 s = BDY((LIST)BDY(t));
                   3369:                 j = perm[QTOS((Q)ARG0(s))]; STOQ(j,jq); ARG0(s) = (pointer)jq;
                   3370:                 for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
                   3371:                     j = perm[QTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOQ(j,jq);
                   3372:                     ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   3373:                 }
                   3374:             }
                   3375:             for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
                   3376:                 STOQ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.167     noro     3377:             }
1.208     noro     3378:             MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   3379:             MKLIST(l5,tl4);
                   3380:             STOQ(nd_bpe,bpe);
                   3381:             tr = mknode(8,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe); MKLIST(*rp,tr);
                   3382:         }
1.167     noro     3383:     }
1.103     noro     3384: #if 0
1.157     noro     3385:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     3386: #endif
1.127     noro     3387: }
                   3388:
                   3389: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
                   3390: {
1.157     noro     3391:     VL tv,fv,vv,vc,av;
                   3392:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   3393:     int e,max,nvar,i;
                   3394:     NDV b;
                   3395:     int ishomo,nalg;
                   3396:     Alg alpha,dp;
                   3397:     P p,zp;
                   3398:     Q dmy;
                   3399:     LIST f1,f2;
                   3400:     Obj obj;
                   3401:     NumberField nf;
                   3402:     struct order_spec *ord1;
1.167     noro     3403:     int *perm;
1.157     noro     3404:
1.197     noro     3405:     parse_nd_option(current_option);
1.157     noro     3406:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   3407:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   3408:     switch ( ord->id ) {
                   3409:         case 1:
                   3410:             if ( ord->nv != nvar )
                   3411:                 error("nd_check : invalid order specification");
                   3412:             break;
                   3413:         default:
                   3414:             break;
                   3415:     }
                   3416:     nd_nalg = 0;
                   3417:     av = 0;
                   3418:     if ( !m ) {
                   3419:         get_algtree((Obj)f,&av);
                   3420:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   3421:         nd_ntrans = nvar;
                   3422:         nd_nalg = nalg;
                   3423:         /* #i -> t#i */
                   3424:         if ( nalg ) {
                   3425:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   3426:             ord = ord1;
                   3427:             f = f1;
                   3428:         }
                   3429:         nvar += nalg;
                   3430:     }
                   3431:     nd_init_ord(ord);
1.178     noro     3432:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
1.157     noro     3433:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   3434:             e = getdeg(tv->v,(P)BDY(t));
                   3435:             max = MAX(e,max);
                   3436:         }
                   3437:     nd_setup_parameters(nvar,max);
                   3438:     ishomo = 1;
                   3439:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   3440:         ptozp((P)BDY(t),1,&dmy,&zp);
                   3441:         b = (pointer)ptondv(CO,vv,zp);
                   3442:         if ( ishomo )
                   3443:             ishomo = ishomo && ndv_ishomo(b);
                   3444:         if ( m ) ndv_mod(m,b);
                   3445:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   3446:     }
                   3447:     if ( fd0 ) NEXT(fd) = 0;
                   3448:     ndv_setup(m,0,fd0,0,1);
                   3449:     for ( x = 0, i = 0; i < nd_psn; i++ )
                   3450:         x = update_base(x,i);
                   3451:     if ( do_check ) {
1.168     noro     3452:         x = nd_gb(m,ishomo,1,0,&perm);
1.157     noro     3453:         if ( !x ) {
                   3454:             *rp = 0;
                   3455:             return;
                   3456:         }
                   3457:     } else {
1.175     noro     3458: #if 0
                   3459:                /* bug ? */
1.157     noro     3460:         for ( t = x; t; t = NEXT(t) )
1.159     noro     3461:             BDY(t) = (pointer)nd_ps[(long)BDY(t)];
1.175     noro     3462: #else
                   3463:                conv_ilist(0,0,x,&perm);
                   3464: #endif
1.157     noro     3465:     }
1.167     noro     3466:     x = ndv_reducebase(x,perm);
1.157     noro     3467:     x = ndv_reduceall(m,x);
                   3468:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   3469:         NEXTNODE(r0,r);
                   3470:         BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   3471:     }
                   3472:     if ( r0 ) NEXT(r) = 0;
1.208     noro     3473:     if ( !m && nd_nalg )
1.157     noro     3474:         r0 = postprocess_algcoef(av,alist,r0);
                   3475:     MKLIST(*rp,r0);
1.20      noro     3476: }
                   3477:
1.198     noro     3478: NDV recompute_trace(NODE trace,NDV *p,int m);
                   3479: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
                   3480:
                   3481: NDV recompute_trace(NODE ti,NDV *p,int mod)
                   3482: {
                   3483:        int c,c1,c2,i;
                   3484:        NM mul,m,tail;
                   3485:        ND d,r,rm;
                   3486:        NODE sj;
                   3487:        NDV red;
                   3488:        Obj mj;
                   3489:        static int afo=0;
                   3490:
                   3491:        afo++;
                   3492:        mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   3493:        CM(mul) = 1;
                   3494:        tail = 0;
                   3495:        for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
                   3496:                sj = BDY((LIST)BDY(ti));
                   3497:                if ( ARG0(sj) ) {
                   3498:                        red = p[QTOS((Q)ARG1(sj))];
                   3499:                        mj = (Obj)ARG2(sj);
                   3500:                        if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
                   3501:                        else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
                   3502:                        rm = ndv_mul_nm(mod,mul,red);
                   3503:                        if ( !r ) r = rm;
                   3504:                        else {
                   3505:                                for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
                   3506:                                        if ( d ) {
                   3507:                                                NEXT(tail) = m; tail = m; LEN(d)++;
                   3508:                                        } else {
                   3509:                                                MKND(nd_nvar,m,1,d); tail = BDY(d);
                   3510:                                        }
                   3511:                                }
                   3512:                                if ( !m ) return 0; /* failure */
1.232     noro     3513:                 else {
1.198     noro     3514:                                        BDY(r) = m;
1.232     noro     3515:                     if ( mod > 0 || mod == -1 ) {
                   3516:                                          c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
                   3517:                                          DMAR(c1,c2,0,mod,c);
                   3518:                                          nd_mul_c(mod,rm,c);
                   3519:                     } else {
                   3520:                       GZ t,u;
                   3521:
                   3522:                       chsgnlf(HCZ(r),&t);
                   3523:                       divlf(t,HCZ(rm),&u);
                   3524:                       nd_mul_c_lf(rm,u);
                   3525:                     }
1.198     noro     3526:                                        r = nd_add(mod,r,rm);
1.232     noro     3527:                          }
                   3528:           }
                   3529:           }
1.198     noro     3530:        }
                   3531:        if ( tail ) NEXT(tail) = 0;
                   3532:        d = nd_add(mod,d,r);
                   3533:        nd_mul_c(mod,d,invm(HCM(d),mod));
                   3534:        return ndtondv(mod,d);
                   3535: }
                   3536:
                   3537: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
                   3538: {
                   3539:     VL tv,fv,vv,vc,av;
                   3540:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   3541:     int e,max,nvar,i;
                   3542:     NDV b;
                   3543:     int ishomo,nalg;
                   3544:     Alg alpha,dp;
                   3545:     P p,zp;
                   3546:     Q dmy;
                   3547:     LIST f1,f2;
                   3548:     Obj obj;
                   3549:     NumberField nf;
                   3550:     struct order_spec *ord1;
                   3551:        NODE permtrace,intred,ind,perm,trace,ti;
                   3552:        int len,n,j;
                   3553:        NDV *db,*pb;
                   3554:
                   3555:     parse_nd_option(current_option);
                   3556:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   3557:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   3558:     switch ( ord->id ) {
                   3559:         case 1:
                   3560:             if ( ord->nv != nvar )
                   3561:                 error("nd_check : invalid order specification");
                   3562:             break;
                   3563:         default:
                   3564:             break;
                   3565:     }
                   3566:     nd_init_ord(ord);
                   3567:        nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
                   3568:     nd_setup_parameters(nvar,0);
                   3569:
                   3570:        len = length(BDY(f));
                   3571:        db = (NDV *)MALLOC(len*sizeof(NDV *));
                   3572:        for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
                   3573:            ptozp((P)BDY(t),1,&dmy,&zp);
                   3574:            b = ptondv(CO,vv,zp);
                   3575:         ndv_mod(m,b);
                   3576:                ndv_mul_c(m,b,invm(HCM(b),m));
                   3577:                db[i] = b;
                   3578:     }
                   3579:
                   3580:        permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   3581:        intred = BDY((LIST)ARG3(BDY(tlist)));
                   3582:        ind = BDY((LIST)ARG4(BDY(tlist)));
                   3583:        perm = BDY((LIST)ARG0(permtrace));
                   3584:        trace = NEXT(permtrace);
                   3585:
                   3586:        for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
                   3587:                j = QTOS((Q)ARG0(BDY((LIST)BDY(t))));
                   3588:                if ( j > i ) i = j;
                   3589:        }
                   3590:        n = i+1;
                   3591:        pb = (NDV *)MALLOC(n*sizeof(NDV *));
                   3592:        for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   3593:                ti = BDY((LIST)BDY(t));
                   3594:                pb[QTOS((Q)ARG0(ti))] = db[QTOS((Q)ARG1(ti))];
                   3595:        }
                   3596:        for ( t = trace; t; t = NEXT(t) ) {
                   3597:                ti = BDY((LIST)BDY(t));
                   3598:                pb[QTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   3599:                if ( !pb[QTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
                   3600:        if ( DP_Print ) {
                   3601:                        fprintf(asir_out,"."); fflush(asir_out);
                   3602:        }
                   3603:        }
                   3604:        for ( t = intred; t; t = NEXT(t) ) {
                   3605:                ti = BDY((LIST)BDY(t));
                   3606:                pb[QTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   3607:                if ( !pb[QTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
                   3608:        if ( DP_Print ) {
                   3609:                        fprintf(asir_out,"*"); fflush(asir_out);
                   3610:        }
                   3611:        }
                   3612:     for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
                   3613:         NEXTNODE(r0,r);
                   3614:                b = pb[QTOS((Q)BDY(t))];
                   3615:         ndv_mul_c(m,b,invm(HCM(b),m));
                   3616: #if 0
                   3617:         BDY(r) = ndvtop(m,CO,vv,pb[QTOS((Q)BDY(t))]);
                   3618: #else
                   3619:         BDY(r) = ndvtodp(m,pb[QTOS((Q)BDY(t))]);
                   3620: #endif
                   3621:     }
                   3622:     if ( r0 ) NEXT(r) = 0;
                   3623:     MKLIST(*rp,r0);
                   3624:     if ( DP_Print ) fprintf(asir_out,"\n");
                   3625: }
                   3626:
1.133     noro     3627: void nd_gr_trace(LIST f,LIST v,int trace,int homo,int f4,struct order_spec *ord,LIST *rp)
1.20      noro     3628: {
1.157     noro     3629:     VL tv,fv,vv,vc,av;
                   3630:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   3631:     int m,nocheck,nvar,mindex,e,max;
                   3632:     NDV c;
                   3633:     NMV a;
                   3634:     P p,zp;
                   3635:     Q dmy;
                   3636:     EPOS oepos;
1.164     noro     3637:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
1.157     noro     3638:     Alg alpha,dp;
                   3639:     P poly;
1.158     noro     3640:     LIST f1,f2,zpl;
1.157     noro     3641:     Obj obj;
                   3642:     NumberField nf;
                   3643:     struct order_spec *ord1;
                   3644:     struct oEGT eg_check,eg0,eg1;
1.168     noro     3645:     NODE tr,tl1,tl2,tl3,tl4;
                   3646:     LIST l1,l2,l3,l4,l5;
1.167     noro     3647:     int *perm;
1.168     noro     3648:     int j,ret;
1.198     noro     3649:     Q jq,bpe;
1.157     noro     3650:
1.167     noro     3651:     nd_module = 0;
1.172     noro     3652:     parse_nd_option(current_option);
1.234   ! noro     3653:     if ( nd_lf ) {
        !          3654:       if ( f4 )
        !          3655:         nd_f4_lf_trace(f,v,trace,homo,ord,rp);
        !          3656:       else
        !          3657:         error("nd_gr_trace is not implemented yet over a large finite field");
        !          3658:       return;
        !          3659:     }
1.157     noro     3660:     if ( DP_Multiple )
                   3661:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   3662:
                   3663:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   3664:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   3665:     switch ( ord->id ) {
                   3666:         case 1:
                   3667:             if ( ord->nv != nvar )
                   3668:                 error("nd_gr_trace : invalid order specification");
                   3669:             break;
                   3670:         default:
                   3671:             break;
                   3672:     }
                   3673:
                   3674:     get_algtree((Obj)f,&av);
                   3675:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   3676:     nd_ntrans = nvar;
                   3677:     nd_nalg = nalg;
                   3678:     /* #i -> t#i */
                   3679:     if ( nalg ) {
                   3680:         preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   3681:         ord = ord1;
                   3682:         f = f1;
                   3683:     }
                   3684:     nvar += nalg;
                   3685:
                   3686:     nocheck = 0;
                   3687:     mindex = 0;
                   3688:
                   3689:     if ( Demand ) nd_demand = 1;
                   3690:     else nd_demand = 0;
                   3691:
                   3692:     /* setup modulus */
                   3693:     if ( trace < 0 ) {
                   3694:         trace = -trace;
                   3695:         nocheck = 1;
                   3696:     }
                   3697:     m = trace > 1 ? trace : get_lprime(mindex);
1.158     noro     3698:     nd_init_ord(ord);
                   3699:     mrank = 0;
1.178     noro     3700:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
1.157     noro     3701:         for ( tv = vv; tv; tv = NEXT(tv) ) {
1.158     noro     3702:             if ( nd_module ) {
                   3703:                 s = BDY((LIST)BDY(t));
                   3704:                 trank = length(s);
                   3705:                 mrank = MAX(mrank,trank);
                   3706:                 for ( ; s; s = NEXT(s) ) {
                   3707:                     e = getdeg(tv->v,(P)BDY(s));
                   3708:                     max = MAX(e,max);
                   3709:                 }
                   3710:             } else {
                   3711:                 e = getdeg(tv->v,(P)BDY(t));
                   3712:                 max = MAX(e,max);
                   3713:             }
1.157     noro     3714:         }
                   3715:     nd_setup_parameters(nvar,max);
1.164     noro     3716:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
1.157     noro     3717:     ishomo = 1;
                   3718:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.167     noro     3719:         if ( nd_module ) {
1.172     noro     3720:                        if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
1.167     noro     3721:                        else zpl = (LIST)BDY(t);
1.158     noro     3722:             c = (pointer)pltondv(CO,vv,zpl);
                   3723:         } else {
1.172     noro     3724:                        if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
1.167     noro     3725:                        else zp = (P)BDY(t);
1.158     noro     3726:             c = (pointer)ptondv(CO,vv,zp);
1.167     noro     3727:         }
1.157     noro     3728:         if ( ishomo )
                   3729:             ishomo = ishomo && ndv_ishomo(c);
                   3730:         if ( c ) {
                   3731:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   3732:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   3733:         }
                   3734:     }
                   3735:     if ( in0 ) NEXT(in) = 0;
                   3736:     if ( fd0 ) NEXT(fd) = 0;
                   3737:     if ( !ishomo && homo ) {
                   3738:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   3739:             c = (NDV)BDY(t); len = LEN(c);
                   3740:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   3741:                 wmax = MAX(TD(DL(a)),wmax);
                   3742:         }
                   3743:         homogenize_order(ord,nvar,&ord1);
                   3744:         nd_init_ord(ord1);
                   3745:         nd_setup_parameters(nvar+1,wmax);
                   3746:         for ( t = fd0; t; t = NEXT(t) )
1.164     noro     3747:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
1.157     noro     3748:     }
1.228     noro     3749:     if ( MaxDeg > 0 ) nocheck = 1;
1.157     noro     3750:     while ( 1 ) {
1.183     noro     3751:                tl1 = tl2 = tl3 = tl4 = 0;
1.157     noro     3752:         if ( Demand )
                   3753:             nd_demand = 1;
1.187     noro     3754:         ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0);
1.172     noro     3755:         if ( nd_gentrace ) {
1.167     noro     3756:             MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   3757:         }
1.177     noro     3758:         if ( ret )
                   3759:             cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
                   3760:         if ( !ret || !cand ) {
1.157     noro     3761:             /* failure */
                   3762:             if ( trace > 1 ) { *rp = 0; return; }
                   3763:             else m = get_lprime(++mindex);
                   3764:             continue;
                   3765:         }
                   3766:         if ( !ishomo && homo ) {
                   3767:             /* dehomogenization */
                   3768:             for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   3769:             nd_init_ord(ord);
                   3770:             nd_setup_parameters(nvar,0);
                   3771:         }
                   3772:         nd_demand = 0;
1.167     noro     3773:         cand = ndv_reducebase(cand,perm);
1.172     noro     3774:         if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
1.157     noro     3775:         cand = ndv_reduceall(0,cand);
                   3776:         cbpe = nd_bpe;
1.172     noro     3777:         if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
1.173     noro     3778:         get_eg(&eg0);
1.157     noro     3779:         if ( nocheck )
                   3780:             break;
1.170     noro     3781:         if ( ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand) ) {
1.172     noro     3782:             if ( nd_gentrace ) {
1.168     noro     3783:                            tl3 = nd_alltracelist; nd_alltracelist = 0;
                   3784:                    } else tl3 = 0;
                   3785:             /* gbcheck : cand is a GB of Id(cand) ? */
1.234   ! noro     3786:             if ( nd_vc || nd_gentrace || nd_gensyz )
        !          3787:               ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
        !          3788:             else
        !          3789:               ret = nd_f4(0,1,0)!=0;
1.172     noro     3790:             if ( nd_gentrace && nd_gensyz ) {
1.168     noro     3791:                            tl4 = nd_alltracelist; nd_alltracelist = 0;
                   3792:                    } else tl4 = 0;
                   3793:                }
                   3794:                if ( ret ) break;
1.157     noro     3795:         else if ( trace > 1 ) {
                   3796:             /* failure */
                   3797:             *rp = 0; return;
                   3798:         } else {
                   3799:             /* try the next modulus */
                   3800:             m = get_lprime(++mindex);
                   3801:             /* reset the parameters */
                   3802:             if ( !ishomo && homo ) {
                   3803:                 nd_init_ord(ord1);
                   3804:                 nd_setup_parameters(nvar+1,wmax);
                   3805:             } else {
                   3806:                 nd_init_ord(ord);
                   3807:                 nd_setup_parameters(nvar,max);
                   3808:             }
                   3809:         }
                   3810:     }
                   3811:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   3812:     if ( DP_Print )
                   3813:         fprintf(asir_out,"check=%fsec\n",eg_check.exectime+eg_check.gctime);
                   3814:     /* dp->p */
                   3815:     nd_bpe = cbpe;
                   3816:     nd_setup_parameters(nd_nvar,0);
1.158     noro     3817:     for ( r = cand; r; r = NEXT(r) ) {
1.167     noro     3818:     if ( nd_module ) BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.158     noro     3819:         else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
                   3820:     }
1.208     noro     3821:     if ( nd_nalg )
1.157     noro     3822:         cand = postprocess_algcoef(av,alist,cand);
                   3823:     MKLIST(*rp,cand);
1.172     noro     3824:     if ( nd_gentrace ) {
1.167     noro     3825:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
1.168     noro     3826:                tl3 = reverse_node(tl3);
1.167     noro     3827:                /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   3828:         for ( t = tl2; t; t = NEXT(t) ) {
                   3829:                        /* s = [i,[*,j,*,*],...] */
                   3830:             s = BDY((LIST)BDY(t));
                   3831:             j = perm[QTOS((Q)ARG0(s))]; STOQ(j,jq); ARG0(s) = (pointer)jq;
                   3832:                        for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
                   3833:                 j = perm[QTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOQ(j,jq);
                   3834:                                ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   3835:             }
                   3836:                }
                   3837:                for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
                   3838:                    STOQ(perm[j],jq); MKNODE(s,jq,t); t = s;
                   3839:                }
1.168     noro     3840:         MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   3841:                MKLIST(l5,tl4);
1.198     noro     3842:                STOQ(nd_bpe,bpe);
                   3843:         tr = mknode(8,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe); MKLIST(*rp,tr);
1.167     noro     3844:     }
1.157     noro     3845: }
1.52      noro     3846:
1.157     noro     3847: /* XXX : module element is not considered  */
1.1       noro     3848:
1.61      noro     3849: void dltondl(int n,DL dl,UINT *r)
1.1       noro     3850: {
1.157     noro     3851:     UINT *d;
                   3852:     int i,j,l,s,ord_l;
                   3853:     struct order_pair *op;
                   3854:
                   3855:     d = dl->d;
                   3856:     for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
                   3857:     if ( nd_blockmask ) {
                   3858:         l = nd_blockmask->n;
                   3859:         op = nd_blockmask->order_pair;
                   3860:         for ( j = 0, s = 0; j < l; j++ ) {
                   3861:             ord_l = op[j].length;
                   3862:             for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
                   3863:         }
                   3864:         TD(r) = ndl_weight(r);
                   3865:         ndl_weight_mask(r);
                   3866:     } else {
                   3867:         for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
                   3868:         TD(r) = ndl_weight(r);
                   3869:     }
1.1       noro     3870: }
                   3871:
1.61      noro     3872: DL ndltodl(int n,UINT *ndl)
1.1       noro     3873: {
1.157     noro     3874:     DL dl;
                   3875:     int *d;
                   3876:     int i,j,l,s,ord_l;
                   3877:     struct order_pair *op;
                   3878:
                   3879:     NEWDL(dl,n);
                   3880:     dl->td = TD(ndl);
                   3881:     d = dl->d;
                   3882:     if ( nd_blockmask ) {
                   3883:         l = nd_blockmask->n;
                   3884:         op = nd_blockmask->order_pair;
                   3885:         for ( j = 0, s = 0; j < l; j++ ) {
                   3886:             ord_l = op[j].length;
                   3887:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   3888:         }
                   3889:     } else {
                   3890:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   3891:     }
                   3892:     return dl;
1.1       noro     3893: }
                   3894:
1.167     noro     3895: void nmtodp(int mod,NM m,DP *r)
                   3896: {
                   3897:     DP dp;
                   3898:     MP mr;
                   3899:
                   3900:     NEWMP(mr);
                   3901:     mr->dl = ndltodl(nd_nvar,DL(m));
                   3902:     mr->c = ndctop(mod,m->c);
                   3903:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   3904:     *r = dp;
                   3905: }
                   3906:
1.61      noro     3907: void ndl_print(UINT *dl)
1.1       noro     3908: {
1.157     noro     3909:     int n;
                   3910:     int i,j,l,ord_l,s,s0;
                   3911:     struct order_pair *op;
                   3912:
                   3913:     n = nd_nvar;
                   3914:     printf("<<");
                   3915:     if ( nd_blockmask ) {
                   3916:         l = nd_blockmask->n;
                   3917:         op = nd_blockmask->order_pair;
                   3918:         for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   3919:             ord_l = op[j].length;
                   3920:             for ( i = 0; i < ord_l; i++, s++ )
                   3921:                 printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
                   3922:         }
                   3923:     } else {
                   3924:         for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
                   3925:     }
                   3926:     printf(">>");
                   3927:     if ( MPOS(dl) )
                   3928:         printf("*e%d",MPOS(dl));
1.1       noro     3929: }
                   3930:
                   3931: void nd_print(ND p)
                   3932: {
1.157     noro     3933:     NM m;
1.1       noro     3934:
1.157     noro     3935:     if ( !p )
                   3936:         printf("0\n");
                   3937:     else {
                   3938:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   3939:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   3940:             else printf("+%d*",CM(m));
                   3941:             ndl_print(DL(m));
                   3942:         }
                   3943:         printf("\n");
                   3944:     }
1.1       noro     3945: }
                   3946:
1.113     noro     3947: void nd_print_q(ND p)
1.16      noro     3948: {
1.157     noro     3949:     NM m;
1.16      noro     3950:
1.157     noro     3951:     if ( !p )
                   3952:         printf("0\n");
                   3953:     else {
                   3954:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   3955:             printf("+");
                   3956:             printexpr(CO,(Obj)CQ(m));
                   3957:             printf("*");
                   3958:             ndl_print(DL(m));
                   3959:         }
                   3960:         printf("\n");
                   3961:     }
1.16      noro     3962: }
                   3963:
1.1       noro     3964: void ndp_print(ND_pairs d)
                   3965: {
1.157     noro     3966:     ND_pairs t;
1.1       noro     3967:
1.157     noro     3968:     for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
                   3969:     printf("\n");
1.1       noro     3970: }
                   3971:
1.20      noro     3972: void nd_removecont(int mod,ND p)
1.16      noro     3973: {
1.157     noro     3974:     int i,n;
                   3975:     Q *w;
                   3976:     Q dvr,t;
                   3977:     NM m;
                   3978:     struct oVECT v;
                   3979:     N q,r;
                   3980:
                   3981:     if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
1.232     noro     3982:     else if ( mod == -2 ) {
                   3983:       GZ inv;
                   3984:       divlf(ONEGZ,HCZ(p),&inv);
                   3985:       nd_mul_c_lf(p,inv);
                   3986:     } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
1.157     noro     3987:     else {
                   3988:         for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   3989:         w = (Q *)ALLOCA(n*sizeof(Q));
                   3990:         v.len = n;
                   3991:         v.body = (pointer *)w;
                   3992:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
                   3993:         removecont_array((P *)w,n,1);
                   3994:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
                   3995:     }
1.16      noro     3996: }
                   3997:
1.21      noro     3998: void nd_removecont2(ND p1,ND p2)
                   3999: {
1.157     noro     4000:     int i,n1,n2,n;
                   4001:     Q *w;
                   4002:     Q dvr,t;
                   4003:     NM m;
                   4004:     struct oVECT v;
                   4005:     N q,r;
                   4006:
                   4007:     n1 = nd_length(p1);
                   4008:     n2 = nd_length(p2);
                   4009:     n = n1+n2;
                   4010:     w = (Q *)ALLOCA(n*sizeof(Q));
                   4011:     v.len = n;
                   4012:     v.body = (pointer *)w;
                   4013:     i = 0;
                   4014:     if ( p1 )
                   4015:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CQ(m);
                   4016:     if ( p2 )
                   4017:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
                   4018:     removecont_array((P *)w,n,1);
                   4019:     i = 0;
                   4020:     if ( p1 )
                   4021:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CQ(m) = w[i];
                   4022:     if ( p2 )
                   4023:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
1.21      noro     4024: }
                   4025:
1.20      noro     4026: void ndv_removecont(int mod,NDV p)
1.16      noro     4027: {
1.157     noro     4028:     int i,len,all_p;
                   4029:     Q *c;
                   4030:     P *w;
                   4031:     Q dvr,t;
                   4032:     P g,cont,tp;
                   4033:     NMV m;
                   4034:
                   4035:     if ( mod == -1 )
                   4036:         ndv_mul_c(mod,p,_invsf(HCM(p)));
1.232     noro     4037:     else if ( mod == -2 ) {
                   4038:       GZ inv;
                   4039:       divlf(ONEGZ,HCZ(p),&inv);
                   4040:       ndv_mul_c_lf(p,inv);
                   4041:     } else if ( mod )
1.157     noro     4042:         ndv_mul_c(mod,p,invm(HCM(p),mod));
                   4043:     else {
                   4044:         len = p->len;
                   4045:         w = (P *)ALLOCA(len*sizeof(P));
                   4046:         c = (Q *)ALLOCA(len*sizeof(Q));
                   4047:         for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
                   4048:             ptozp(CP(m),1,&c[i],&w[i]);
                   4049:             all_p = all_p && !NUM(w[i]);
                   4050:         }
                   4051:         if ( all_p ) {
                   4052:             qltozl(c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
                   4053:             mulp(nd_vc,(P)dvr,g,&cont);
                   4054:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   4055:                 divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
                   4056:             }
                   4057:         } else {
                   4058:             sortbynm((Q *)c,len);
                   4059:             qltozl((Q *)c,len,&dvr);
                   4060:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   4061:                 divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
                   4062:             }
                   4063:         }
                   4064:     }
1.21      noro     4065: }
                   4066:
1.157     noro     4067: /* koko */
                   4068:
1.164     noro     4069: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
1.61      noro     4070: {
1.157     noro     4071:     int len,i,max;
                   4072:     NMV m,mr0,mr,t;
1.61      noro     4073:
1.157     noro     4074:     len = p->len;
1.178     noro     4075:     for ( m = BDY(p), i = 0, max = 1; i < len; NMV_OADV(m), i++ )
1.157     noro     4076:         max = MAX(max,TD(DL(m)));
                   4077:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   4078:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   4079:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   4080:     t = (NMV)ALLOCA(nmv_adv);
                   4081:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.164     noro     4082:         ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.157     noro     4083:         CQ(mr) = CQ(m);
                   4084:         ndl_copy(DL(t),DL(mr));
                   4085:     }
                   4086:     NV(p)++;
                   4087:     BDY(p) = mr0;
1.61      noro     4088: }
                   4089:
1.45      noro     4090: void ndv_dehomogenize(NDV p,struct order_spec *ord)
1.23      noro     4091: {
1.164     noro     4092:     int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
1.167     noro     4093:     int pos;
1.157     noro     4094:     Q *w;
                   4095:     Q dvr,t;
                   4096:     NMV m,r;
                   4097:
                   4098:     len = p->len;
                   4099:     newnvar = nd_nvar-1;
                   4100:     newexporigin = nd_get_exporigin(ord);
1.167     noro     4101:     if ( nd_module ) newmpos = newexporigin-1;
1.157     noro     4102:     newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
                   4103:     for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
                   4104:         ndl_dehomogenize(DL(m));
                   4105:     if ( newwpd != nd_wpd ) {
                   4106:         newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
                   4107:         for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
                   4108:             CQ(r) = CQ(m);
1.167     noro     4109:             if ( nd_module ) pos = MPOS(DL(m));
1.157     noro     4110:             for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   4111:             adj = nd_exporigin-newexporigin;
                   4112:             for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
1.167     noro     4113:             if ( nd_module ) {
                   4114:                 DL(r)[newmpos] = pos;
                   4115:             }
1.157     noro     4116:         }
                   4117:     }
                   4118:     NV(p)--;
1.23      noro     4119: }
                   4120:
1.150     noro     4121: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
                   4122: {
1.157     noro     4123:     int i;
                   4124:     P *tpl,*tpl1;
                   4125:     NODE l;
                   4126:     P h,gcd,t;
                   4127:
                   4128:     tpl = (P *)ALLOCA(m*sizeof(P));
                   4129:     tpl1 = (P *)ALLOCA(m*sizeof(P));
                   4130:     bcopy(pl,tpl,m*sizeof(P));
                   4131:     gcd = (P)ONE;
                   4132:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   4133:         h = (P)BDY(l);
                   4134:         while ( 1 ) {
                   4135:             for ( i = 0; i < m; i++ )
                   4136:                 if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
                   4137:                     break;
                   4138:             if ( i == m ) {
                   4139:                 bcopy(tpl1,tpl,m*sizeof(P));
                   4140:                 mulp(vl,gcd,h,&t); gcd = t;
                   4141:             } else
                   4142:                 break;
                   4143:         }
                   4144:     }
                   4145:     if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
                   4146:     if ( full ) {
                   4147:         heu_nezgcdnpz(vl,tpl,m,&t);
                   4148:         mulp(vl,gcd,t,pr);
                   4149:     } else
                   4150:         *pr = gcd;
1.150     noro     4151: }
                   4152:
                   4153: void removecont_array(P *p,int n,int full)
1.146     noro     4154: {
1.157     noro     4155:     int all_p,all_q,i;
                   4156:     Q *c;
                   4157:     P *w;
                   4158:     P t,s;
                   4159:
                   4160:     for ( all_q = 1, i = 0; i < n; i++ )
                   4161:         all_q = all_q && NUM(p[i]);
                   4162:     if ( all_q ) {
                   4163:         removecont_array_q((Q *)p,n);
                   4164:     } else {
                   4165:         c = (Q *)ALLOCA(n*sizeof(Q));
                   4166:         w = (P *)ALLOCA(n*sizeof(P));
                   4167:         for ( i = 0; i < n; i++ ) {
                   4168:             ptozp(p[i],1,&c[i],&w[i]);
                   4169:         }
                   4170:         removecont_array_q(c,n);
                   4171:         nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
                   4172:         for ( i = 0; i < n; i++ ) {
                   4173:             divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
                   4174:         }
                   4175:     }
1.146     noro     4176: }
                   4177:
                   4178: void removecont_array_q(Q *c,int n)
1.21      noro     4179: {
1.157     noro     4180:     struct oVECT v;
                   4181:     Q d0,d1,a,u,u1,gcd;
                   4182:     int i,j;
                   4183:     N qn,rn,gn;
                   4184:     Q *q,*r;
                   4185:
                   4186:     q = (Q *)ALLOCA(n*sizeof(Q));
                   4187:     r = (Q *)ALLOCA(n*sizeof(Q));
                   4188:     v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   4189:     igcdv_estimate(&v,&d0);
                   4190:     for ( i = 0; i < n; i++ ) {
                   4191:         divn(NM(c[i]),NM(d0),&qn,&rn);
                   4192:         NTOQ(qn,SGN(c[i])*SGN(d0),q[i]);
                   4193:         NTOQ(rn,SGN(c[i]),r[i]);
                   4194:     }
                   4195:     for ( i = 0; i < n; i++ ) if ( r[i] ) break;
                   4196:     if ( i < n ) {
                   4197:         v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   4198:         igcdv(&v,&d1);
                   4199:         gcdn(NM(d0),NM(d1),&gn); NTOQ(gn,1,gcd);
                   4200:         divsn(NM(d0),gn,&qn); NTOQ(qn,1,a);
                   4201:         for ( i = 0; i < n; i++ ) {
                   4202:             mulq(a,q[i],&u);
                   4203:             if ( r[i] ) {
                   4204:                 divsn(NM(r[i]),gn,&qn); NTOQ(qn,SGN(r[i]),u1);
                   4205:                 addq(u,u1,&q[i]);
                   4206:             } else
                   4207:                 q[i] = u;
                   4208:         }
                   4209:     }
                   4210:     for ( i = 0; i < n; i++ ) c[i] = q[i];
1.16      noro     4211: }
                   4212:
1.19      noro     4213: void nd_mul_c(int mod,ND p,int mul)
1.1       noro     4214: {
1.157     noro     4215:     NM m;
                   4216:     int c,c1;
1.1       noro     4217:
1.157     noro     4218:     if ( !p ) return;
                   4219:     if ( mul == 1 ) return;
                   4220:     if ( mod == -1 )
                   4221:         for ( m = BDY(p); m; m = NEXT(m) )
                   4222:             CM(m) = _mulsf(CM(m),mul);
                   4223:     else
                   4224:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   4225:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   4226:         }
1.1       noro     4227: }
                   4228:
1.232     noro     4229: void nd_mul_c_lf(ND p,GZ mul)
                   4230: {
                   4231:     NM m;
                   4232:     GZ c;
                   4233:
                   4234:     if ( !p ) return;
                   4235:     if ( UNIGZ(mul) ) return;
                   4236:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   4237:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   4238:     }
                   4239: }
                   4240:
1.146     noro     4241: void nd_mul_c_q(ND p,P mul)
1.16      noro     4242: {
1.157     noro     4243:     NM m;
                   4244:     P c;
1.16      noro     4245:
1.157     noro     4246:     if ( !p ) return;
                   4247:     if ( UNIQ(mul) ) return;
                   4248:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   4249:         mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
                   4250:     }
1.16      noro     4251: }
                   4252:
1.61      noro     4253: void nd_mul_c_p(VL vl,ND p,P mul)
                   4254: {
1.157     noro     4255:     NM m;
                   4256:     P c;
1.61      noro     4257:
1.157     noro     4258:     if ( !p ) return;
                   4259:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   4260:         mulp(vl,CP(m),mul,&c); CP(m) = c;
                   4261:     }
1.61      noro     4262: }
                   4263:
1.1       noro     4264: void nd_free(ND p)
                   4265: {
1.157     noro     4266:     NM t,s;
1.1       noro     4267:
1.157     noro     4268:     if ( !p ) return;
                   4269:     t = BDY(p);
                   4270:     while ( t ) {
                   4271:         s = NEXT(t);
                   4272:         FREENM(t);
                   4273:         t = s;
                   4274:     }
                   4275:     FREEND(p);
1.1       noro     4276: }
                   4277:
1.23      noro     4278: void ndv_free(NDV p)
                   4279: {
1.200     noro     4280:     GCFREE(BDY(p));
1.23      noro     4281: }
                   4282:
1.61      noro     4283: void nd_append_red(UINT *d,int i)
1.1       noro     4284: {
1.157     noro     4285:     RHist m,m0;
                   4286:     int h;
1.1       noro     4287:
1.157     noro     4288:     NEWRHist(m);
                   4289:     h = ndl_hash_value(d);
                   4290:     m->index = i;
                   4291:     ndl_copy(d,DL(m));
                   4292:     NEXT(m) = nd_red[h];
                   4293:     nd_red[h] = m;
1.1       noro     4294: }
                   4295:
1.61      noro     4296: UINT *ndv_compute_bound(NDV p)
1.1       noro     4297: {
1.157     noro     4298:     UINT *d1,*d2,*t;
                   4299:     UINT u;
                   4300:     int i,j,k,l,len,ind;
                   4301:     NMV m;
                   4302:
                   4303:     if ( !p )
                   4304:         return 0;
                   4305:     d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   4306:     d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   4307:     len = LEN(p);
                   4308:     m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   4309:     for ( i = 1; i < len; i++, NMV_ADV(m) ) {
1.159     noro     4310:         ndl_max(DL(m),d1,d2);
1.157     noro     4311:         t = d1; d1 = d2; d2 = t;
                   4312:     }
                   4313:     l = nd_nvar+31;
                   4314:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   4315:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   4316:         u = d1[i];
                   4317:         k = (nd_epw-1)*nd_bpe;
                   4318:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   4319:             t[ind] = (u>>k)&nd_mask0;
                   4320:     }
                   4321:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   4322:     return t;
1.1       noro     4323: }
                   4324:
1.99      noro     4325: UINT *nd_compute_bound(ND p)
                   4326: {
1.157     noro     4327:     UINT *d1,*d2,*t;
                   4328:     UINT u;
                   4329:     int i,j,k,l,len,ind;
                   4330:     NM m;
                   4331:
                   4332:     if ( !p )
                   4333:         return 0;
                   4334:     d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   4335:     d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   4336:     len = LEN(p);
                   4337:     m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   4338:     for ( m = NEXT(m); m; m = NEXT(m) ) {
                   4339:         ndl_lcm(DL(m),d1,d2);
                   4340:         t = d1; d1 = d2; d2 = t;
                   4341:     }
                   4342:     l = nd_nvar+31;
                   4343:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   4344:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   4345:         u = d1[i];
                   4346:         k = (nd_epw-1)*nd_bpe;
                   4347:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   4348:             t[ind] = (u>>k)&nd_mask0;
                   4349:     }
                   4350:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   4351:     return t;
1.99      noro     4352: }
                   4353:
1.157     noro     4354: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
                   4355: /* of a term. In this case we need additional 1 word. */
                   4356:
1.48      noro     4357: int nd_get_exporigin(struct order_spec *ord)
                   4358: {
1.157     noro     4359:     switch ( ord->id ) {
                   4360:         case 0: case 2: case 256: case 258:
                   4361:             return 1+nd_module;
                   4362:         case 1: case 257:
                   4363:             /* block order */
                   4364:             /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
                   4365:             /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
                   4366:             return ord->ord.block.length+1+nd_module;
                   4367:         case 3: case 259:
                   4368:             error("nd_get_exporigin : composite order is not supported yet.");
                   4369:     }
1.48      noro     4370: }
                   4371:
1.61      noro     4372: void nd_setup_parameters(int nvar,int max) {
1.157     noro     4373:     int i,j,n,elen,ord_o,ord_l,l,s,wpd;
                   4374:     struct order_pair *op;
1.48      noro     4375:
1.157     noro     4376:     nd_nvar = nvar;
                   4377:     if ( max ) {
                   4378:         /* XXX */
                   4379:         if ( do_weyl ) nd_bpe = 32;
                   4380:         else if ( max < 2 ) nd_bpe = 1;
                   4381:         else if ( max < 4 ) nd_bpe = 2;
                   4382:         else if ( max < 8 ) nd_bpe = 3;
                   4383:         else if ( max < 16 ) nd_bpe = 4;
                   4384:         else if ( max < 32 ) nd_bpe = 5;
                   4385:         else if ( max < 64 ) nd_bpe = 6;
                   4386:         else if ( max < 256 ) nd_bpe = 8;
                   4387:         else if ( max < 1024 ) nd_bpe = 10;
                   4388:         else if ( max < 65536 ) nd_bpe = 16;
                   4389:         else nd_bpe = 32;
                   4390:     }
1.203     noro     4391:     if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
1.201     noro     4392:         UINT t;
1.203     noro     4393:                int st;
                   4394:         int *v;
                   4395:        /* t = max(weights) */
                   4396:         t = 0;
                   4397:         if ( current_dl_weight_vector )
                   4398:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                   4399:                 if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
                   4400:                 if ( t < st ) t = st;
                   4401:             }
                   4402:         if ( nd_matrix )
                   4403:             for ( i = 0; i < nd_matrix_len; i++ )
                   4404:                 for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
                   4405:                     if ( (st=v[j]) < 0 ) st = -st;
                   4406:                     if ( t < st ) t = st;
                   4407:                                }
                   4408:         /* i = bitsize of t */
                   4409:         for ( i = 0; t; t >>=1, i++ );
                   4410:         /* i += bitsize of nd_nvar */
                   4411:         for ( t = nd_nvar; t; t >>=1, i++);
                   4412:         /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
                   4413:         if ( (nd_bpe+i) >= 31 )
                   4414:             error("nd_setup_parameters : too large weight");
                   4415:     }
1.157     noro     4416:     nd_epw = (sizeof(UINT)*8)/nd_bpe;
                   4417:     elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   4418:     nd_exporigin = nd_get_exporigin(nd_ord);
                   4419:     wpd = nd_exporigin+elen;
                   4420:     if ( nd_module )
                   4421:         nd_mpos = nd_exporigin-1;
                   4422:     else
                   4423:         nd_mpos = -1;
                   4424:     if ( wpd != nd_wpd ) {
                   4425:         nd_free_private_storage();
                   4426:         nd_wpd = wpd;
                   4427:     }
                   4428:     if ( nd_bpe < 32 ) {
                   4429:         nd_mask0 = (1<<nd_bpe)-1;
                   4430:     } else {
                   4431:         nd_mask0 = 0xffffffff;
                   4432:     }
                   4433:     bzero(nd_mask,sizeof(nd_mask));
                   4434:     nd_mask1 = 0;
                   4435:     for ( i = 0; i < nd_epw; i++ ) {
                   4436:         nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   4437:         nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   4438:     }
                   4439:     nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
                   4440:     nd_epos = nd_create_epos(nd_ord);
                   4441:     nd_blockmask = nd_create_blockmask(nd_ord);
                   4442:     nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
1.1       noro     4443: }
                   4444:
1.103     noro     4445: ND_pairs nd_reconstruct(int trace,ND_pairs d)
1.1       noro     4446: {
1.157     noro     4447:     int i,obpe,oadv,h;
                   4448:     static NM prev_nm_free_list;
                   4449:     static ND_pairs prev_ndp_free_list;
                   4450:     RHist mr0,mr;
                   4451:     RHist r;
                   4452:     RHist *old_red;
                   4453:     ND_pairs s0,s,t;
                   4454:     EPOS oepos;
                   4455:
                   4456:     obpe = nd_bpe;
                   4457:     oadv = nmv_adv;
                   4458:     oepos = nd_epos;
                   4459:     if ( obpe < 2 ) nd_bpe = 2;
                   4460:     else if ( obpe < 3 ) nd_bpe = 3;
                   4461:     else if ( obpe < 4 ) nd_bpe = 4;
                   4462:     else if ( obpe < 5 ) nd_bpe = 5;
                   4463:     else if ( obpe < 6 ) nd_bpe = 6;
                   4464:     else if ( obpe < 8 ) nd_bpe = 8;
                   4465:     else if ( obpe < 10 ) nd_bpe = 10;
                   4466:     else if ( obpe < 16 ) nd_bpe = 16;
                   4467:     else if ( obpe < 32 ) nd_bpe = 32;
                   4468:     else error("nd_reconstruct : exponent too large");
                   4469:
                   4470:     nd_setup_parameters(nd_nvar,0);
                   4471:     prev_nm_free_list = _nm_free_list;
                   4472:     prev_ndp_free_list = _ndp_free_list;
                   4473:     _nm_free_list = 0;
                   4474:     _ndp_free_list = 0;
1.215     noro     4475:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   4476:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   4477:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   4478:         ndv_realloc(nd_ps_gz[i],obpe,oadv,oepos);
                   4479:     }
1.157     noro     4480:     if ( trace )
1.215     noro     4481:         for ( i = nd_psn-1; i >= 0; i-- ) {
1.157     noro     4482:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
1.215     noro     4483:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   4484:         }
1.157     noro     4485:     s0 = 0;
                   4486:     for ( t = d; t; t = NEXT(t) ) {
                   4487:         NEXTND_pairs(s0,s);
                   4488:         s->i1 = t->i1;
                   4489:         s->i2 = t->i2;
                   4490:         SG(s) = SG(t);
                   4491:         ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   4492:     }
                   4493:
                   4494:     old_red = (RHist *)ALLOCA(REDTAB_LEN*sizeof(RHist));
                   4495:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   4496:         old_red[i] = nd_red[i];
                   4497:         nd_red[i] = 0;
                   4498:     }
                   4499:     for ( i = 0; i < REDTAB_LEN; i++ )
                   4500:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   4501:             NEWRHist(mr);
                   4502:             mr->index = r->index;
                   4503:             SG(mr) = SG(r);
                   4504:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   4505:             h = ndl_hash_value(DL(mr));
                   4506:             NEXT(mr) = nd_red[h];
                   4507:             nd_red[h] = mr;
                   4508:         }
                   4509:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   4510:     old_red = 0;
                   4511:     for ( i = 0; i < nd_psn; i++ ) {
                   4512:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   4513:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
                   4514:         nd_psh[i] = r;
                   4515:     }
                   4516:     if ( s0 ) NEXT(s) = 0;
                   4517:     prev_nm_free_list = 0;
                   4518:     prev_ndp_free_list = 0;
1.71      noro     4519: #if 0
1.157     noro     4520:     GC_gcollect();
1.71      noro     4521: #endif
1.157     noro     4522:     return s0;
1.1       noro     4523: }
                   4524:
1.61      noro     4525: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
1.1       noro     4526: {
1.157     noro     4527:     int n,i,ei,oepw,omask0,j,s,ord_l,l;
                   4528:     struct order_pair *op;
1.1       noro     4529:
1.157     noro     4530:     n = nd_nvar;
                   4531:     oepw = (sizeof(UINT)*8)/obpe;
                   4532:     omask0 = (1<<obpe)-1;
                   4533:     TD(r) = TD(d);
                   4534:     for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
                   4535:     if ( nd_blockmask ) {
                   4536:         l = nd_blockmask->n;
                   4537:         op = nd_blockmask->order_pair;
                   4538:         for ( i = 1; i < nd_exporigin; i++ )
                   4539:             r[i] = d[i];
                   4540:         for ( j = 0, s = 0; j < l; j++ ) {
                   4541:             ord_l = op[j].length;
                   4542:             for ( i = 0; i < ord_l; i++, s++ ) {
                   4543:                 ei =  GET_EXP_OLD(d,s);
                   4544:                 PUT_EXP(r,s,ei);
                   4545:             }
                   4546:         }
                   4547:     } else {
                   4548:         for ( i = 0; i < n; i++ ) {
                   4549:             ei = GET_EXP_OLD(d,i);
                   4550:             PUT_EXP(r,i,ei);
                   4551:         }
                   4552:     }
                   4553:     if ( nd_module ) MPOS(r) = MPOS(d);
1.1       noro     4554: }
1.3       noro     4555:
1.6       noro     4556: ND nd_copy(ND p)
                   4557: {
1.157     noro     4558:     NM m,mr,mr0;
                   4559:     int c,n;
                   4560:     ND r;
                   4561:
                   4562:     if ( !p )
                   4563:         return 0;
                   4564:     else {
                   4565:         for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   4566:             NEXTNM(mr0,mr);
                   4567:             CM(mr) = CM(m);
                   4568:             ndl_copy(DL(m),DL(mr));
                   4569:         }
                   4570:         NEXT(mr) = 0;
                   4571:         MKND(NV(p),mr0,LEN(p),r);
                   4572:         SG(r) = SG(p);
                   4573:         return r;
                   4574:     }
1.6       noro     4575: }
                   4576:
1.53      noro     4577: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
1.11      noro     4578: {
1.157     noro     4579:     NM m1,m2;
                   4580:     NDV p1,p2;
                   4581:     ND t1,t2;
                   4582:     UINT *lcm;
                   4583:     P gp,tp;
1.167     noro     4584:     Q g,t,iq;
1.157     noro     4585:     int td;
1.167     noro     4586:     LIST hist;
                   4587:     NODE node;
                   4588:     DP d;
1.157     noro     4589:
                   4590:     if ( !mod && nd_demand ) {
                   4591:         p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
                   4592:     } else {
                   4593:         if ( trace ) {
                   4594:             p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   4595:         } else {
                   4596:             p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   4597:         }
                   4598:     }
                   4599:     lcm = LCM(p);
                   4600:     NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
                   4601:     if ( ndl_check_bound2(p->i1,DL(m1)) ) {
                   4602:         FREENM(m1); return 0;
                   4603:     }
                   4604:     NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
                   4605:     if ( ndl_check_bound2(p->i2,DL(m2)) ) {
                   4606:         FREENM(m1); FREENM(m2); return 0;
                   4607:     }
                   4608:
                   4609:     if ( mod == -1 ) {
                   4610:         CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
1.232     noro     4611:     } else if ( mod > 0 ) {
1.157     noro     4612:         CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
1.232     noro     4613:     } else if ( mod == -2 ) {
                   4614:         CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
1.157     noro     4615:     } else if ( nd_vc ) {
                   4616:         ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
                   4617:         divsp(nd_vc,HCP(p2),gp,&CP(m1));
                   4618:         divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
                   4619:     } else {
                   4620:         igcd_cofactor(HCQ(p1),HCQ(p2),&g,&t,&CQ(m1)); chsgnq(t,&CQ(m2));
                   4621:     }
                   4622:     t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
                   4623:     *rp = nd_add(mod,t1,t2);
1.172     noro     4624:     if ( nd_gentrace ) {
1.167     noro     4625:         /* nd_tracelist is initialized */
                   4626:         STOQ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
                   4627:         MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
                   4628:         STOQ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
                   4629:         MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
                   4630:         nd_tracelist = node;
                   4631:     }
1.157     noro     4632:     FREENM(m1); FREENM(m2);
                   4633:     return 1;
1.11      noro     4634: }
                   4635:
1.19      noro     4636: void ndv_mul_c(int mod,NDV p,int mul)
1.11      noro     4637: {
1.157     noro     4638:     NMV m;
                   4639:     int c,c1,len,i;
1.11      noro     4640:
1.157     noro     4641:     if ( !p ) return;
                   4642:     len = LEN(p);
                   4643:     if ( mod == -1 )
                   4644:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   4645:             CM(m) = _mulsf(CM(m),mul);
                   4646:     else
                   4647:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4648:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   4649:         }
1.11      noro     4650: }
                   4651:
1.232     noro     4652: void ndv_mul_c_lf(NDV p,GZ mul)
                   4653: {
                   4654:     NMV m;
                   4655:     GZ c;
                   4656:     int len,i;
                   4657:
                   4658:     if ( !p ) return;
                   4659:     len = LEN(p);
                   4660:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4661:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   4662:     }
                   4663: }
                   4664:
1.113     noro     4665: void ndv_mul_c_q(NDV p,Q mul)
1.16      noro     4666: {
1.157     noro     4667:     NMV m;
                   4668:     Q c;
                   4669:     int len,i;
                   4670:
                   4671:     if ( !p ) return;
                   4672:     len = LEN(p);
                   4673:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4674:         mulq(CQ(m),mul,&c); CQ(m) = c;
                   4675:     }
1.16      noro     4676: }
                   4677:
1.55      noro     4678: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
1.157     noro     4679:     int n2,i,j,l,n,tlen;
                   4680:     UINT *d0;
                   4681:     NM *tab,*psum;
                   4682:     ND s,r;
                   4683:     NM t;
                   4684:     NMV m1;
                   4685:
                   4686:     if ( !p ) return 0;
                   4687:     n = NV(p); n2 = n>>1;
                   4688:     d0 = DL(m0);
                   4689:     l = LEN(p);
                   4690:     for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   4691:     tab = (NM *)ALLOCA(tlen*sizeof(NM));
                   4692:     psum = (NM *)ALLOCA(tlen*sizeof(NM));
                   4693:     for ( i = 0; i < tlen; i++ ) psum[i] = 0;
                   4694:     m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   4695:     for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
                   4696:         /* m0(NM) * m1(NMV) => tab(NM) */
                   4697:         weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
                   4698:         for ( j = 0; j < tlen; j++ ) {
                   4699:             if ( tab[j] ) {
                   4700:                 NEXT(tab[j]) = psum[j];    psum[j] = tab[j];
                   4701:             }
                   4702:         }
                   4703:     }
                   4704:     for ( i = tlen-1, r = 0; i >= 0; i-- )
                   4705:         if ( psum[i] ) {
                   4706:             for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   4707:             MKND(n,psum[i],j,s);
                   4708:             r = nd_add(mod,r,s);
                   4709:         }
                   4710:     if ( r ) SG(r) = SG(p)+TD(d0);
                   4711:     return r;
1.55      noro     4712: }
                   4713:
1.56      noro     4714: /* product of monomials */
                   4715: /* XXX block order is not handled correctly */
                   4716:
1.55      noro     4717: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   4718: {
1.157     noro     4719:     int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
                   4720:     UINT *d0,*d1,*d,*dt,*ctab;
                   4721:     Q *ctab_q;
                   4722:     Q q,q1;
                   4723:     UINT c0,c1,c;
                   4724:     NM *p;
                   4725:     NM m,t;
                   4726:     int mpos;
                   4727:
                   4728:     for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   4729:     if ( !m0 || !m1 ) return;
                   4730:     d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
1.166     noro     4731:     if ( nd_module )
                   4732:         if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
                   4733:
1.157     noro     4734:     NEWNM(m); d = DL(m);
                   4735:     if ( mod ) {
                   4736:         c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
1.179     noro     4737:     } else if ( nd_vc )
                   4738:         mulp(nd_vc,CP(m0),CP(m1),&CP(m));
                   4739:        else
1.157     noro     4740:         mulq(CQ(m0),CQ(m1),&CQ(m));
                   4741:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   4742:     homo = n&1 ? 1 : 0;
                   4743:     if ( homo ) {
                   4744:         /* offset of h-degree */
                   4745:         h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   4746:         PUT_EXP(DL(m),n-1,h);
                   4747:         TD(DL(m)) = h;
                   4748:         if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   4749:     }
                   4750:     tab[0] = m;
                   4751:     NEWNM(m); d = DL(m);
                   4752:     for ( i = 0, curlen = 1; i < n2; i++ ) {
                   4753:         a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   4754:         k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   4755:         /* xi^a*(Di^k*xi^l)*Di^b */
                   4756:         a += l; b += k;
                   4757:         s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
                   4758:         if ( !k || !l ) {
                   4759:             for ( j = 0; j < curlen; j++ )
                   4760:                 if ( t = tab[j] ) {
                   4761:                     dt = DL(t);
                   4762:                     PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
                   4763:                     if ( nd_blockmask ) ndl_weight_mask(dt);
                   4764:                 }
                   4765:             curlen *= k+1;
                   4766:             continue;
                   4767:         }
                   4768:         min = MIN(k,l);
                   4769:         if ( mod ) {
                   4770:             ctab = (UINT *)ALLOCA((min+1)*sizeof(UINT));
                   4771:             mkwcm(k,l,mod,ctab);
                   4772:         } else {
                   4773:             ctab_q = (Q *)ALLOCA((min+1)*sizeof(Q));
                   4774:             mkwc(k,l,ctab_q);
                   4775:         }
                   4776:         for ( j = min; j >= 0; j-- ) {
                   4777:             for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
                   4778:             PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
                   4779:             h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
                   4780:             if ( homo ) {
                   4781:                 TD(d) = s;
                   4782:                 PUT_EXP(d,n-1,s-h);
                   4783:             } else TD(d) = h;
                   4784:             if ( nd_blockmask ) ndl_weight_mask(d);
                   4785:             if ( mod ) c = ctab[j];
                   4786:             else q = ctab_q[j];
                   4787:             p = tab+curlen*j;
                   4788:             if ( j == 0 ) {
                   4789:                 for ( u = 0; u < curlen; u++, p++ ) {
                   4790:                     if ( tab[u] ) {
                   4791:                         ndl_addto(DL(tab[u]),d);
                   4792:                         if ( mod ) {
                   4793:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
1.180     noro     4794:                         } else if ( nd_vc )
                   4795:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
                   4796:                                                else {
1.157     noro     4797:                             mulq(CQ(tab[u]),q,&q1); CQ(tab[u]) = q1;
                   4798:                         }
                   4799:                     }
                   4800:                 }
                   4801:             } else {
                   4802:                 for ( u = 0; u < curlen; u++, p++ ) {
                   4803:                     if ( tab[u] ) {
                   4804:                         NEWNM(t);
                   4805:                         ndl_add(DL(tab[u]),d,DL(t));
                   4806:                         if ( mod ) {
                   4807:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
1.181     noro     4808:                         } else if ( nd_vc )
1.180     noro     4809:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
                   4810:                                                else
1.157     noro     4811:                             mulq(CQ(tab[u]),q,&CQ(t));
                   4812:                         *p = t;
                   4813:                     }
                   4814:                 }
                   4815:             }
                   4816:         }
                   4817:         curlen *= k+1;
                   4818:     }
                   4819:     FREENM(m);
1.167     noro     4820:     if ( nd_module ) {
1.166     noro     4821:         mpos = MPOS(d1);
1.167     noro     4822:         for ( i = 0; i < tlen; i++ )
                   4823:             if ( tab[i] ) {
                   4824:                 d = DL(tab[i]);
                   4825:                 MPOS(d) = mpos;
                   4826:                 TD(d) = ndl_weight(d);
                   4827:             }
                   4828:     }
1.55      noro     4829: }
                   4830:
1.63      noro     4831: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   4832: {
1.157     noro     4833:     NM mr,mr0;
                   4834:     NMV m;
                   4835:     UINT *d,*dt,*dm;
                   4836:     int c,n,td,i,c1,c2,len;
                   4837:     Q q;
                   4838:     ND r;
                   4839:
                   4840:     if ( !p ) return 0;
                   4841:     else {
                   4842:         n = NV(p); m = BDY(p);
                   4843:         d = DL(m0);
                   4844:         len = LEN(p);
                   4845:         mr0 = 0;
                   4846:         td = TD(d);
                   4847:         c = CM(m0);
                   4848:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4849:             NEXTNM(mr0,mr);
                   4850:             CM(mr) = 1;
                   4851:             ndl_add(DL(m),d,DL(mr));
                   4852:         }
                   4853:         NEXT(mr) = 0;
                   4854:         MKND(NV(p),mr0,len,r);
                   4855:         SG(r) = SG(p) + TD(d);
                   4856:         return r;
                   4857:     }
1.63      noro     4858: }
                   4859:
1.55      noro     4860: ND ndv_mul_nm(int mod,NM m0,NDV p)
1.9       noro     4861: {
1.157     noro     4862:     NM mr,mr0;
                   4863:     NMV m;
                   4864:     UINT *d,*dt,*dm;
                   4865:     int c,n,td,i,c1,c2,len;
                   4866:     P q;
                   4867:     ND r;
                   4868:
                   4869:     if ( !p ) return 0;
                   4870:     else if ( do_weyl )
1.232     noro     4871:         if ( mod < 0 )
1.157     noro     4872:             error("ndv_mul_nm : not implemented (weyl)");
                   4873:         else
                   4874:             return weyl_ndv_mul_nm(mod,m0,p);
                   4875:     else {
                   4876:         n = NV(p); m = BDY(p);
                   4877:         d = DL(m0);
                   4878:         len = LEN(p);
                   4879:         mr0 = 0;
                   4880:         td = TD(d);
                   4881:         if ( mod == -1 ) {
                   4882:             c = CM(m0);
                   4883:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4884:                 NEXTNM(mr0,mr);
                   4885:                 CM(mr) = _mulsf(CM(m),c);
                   4886:                 ndl_add(DL(m),d,DL(mr));
                   4887:             }
1.232     noro     4888:         } else if ( mod == -2 ) {
                   4889:             GZ cl;
                   4890:             cl = CZ(m0);
                   4891:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4892:                 NEXTNM(mr0,mr);
                   4893:                 mullf(CZ(m),cl,&CZ(mr));
                   4894:                 ndl_add(DL(m),d,DL(mr));
                   4895:             }
1.157     noro     4896:         } else if ( mod ) {
                   4897:             c = CM(m0);
                   4898:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4899:                 NEXTNM(mr0,mr);
                   4900:                 c1 = CM(m);
                   4901:                 DMAR(c1,c,0,mod,c2);
                   4902:                 CM(mr) = c2;
                   4903:                 ndl_add(DL(m),d,DL(mr));
                   4904:             }
                   4905:         } else {
                   4906:             q = CP(m0);
                   4907:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4908:                 NEXTNM(mr0,mr);
                   4909:                 mulp(nd_vc,CP(m),q,&CP(mr));
                   4910:                 ndl_add(DL(m),d,DL(mr));
                   4911:             }
                   4912:         }
                   4913:         NEXT(mr) = 0;
                   4914:         MKND(NV(p),mr0,len,r);
                   4915:         SG(r) = SG(p) + TD(d);
                   4916:         return r;
                   4917:     }
1.4       noro     4918: }
                   4919:
1.104     noro     4920: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
1.99      noro     4921: {
1.157     noro     4922:     NM mq0,mq;
                   4923:     NMV tm;
                   4924:     Q q;
                   4925:     int i,nv,sg,c,c1,c2,hindex;
                   4926:     ND p,t,r;
                   4927:     N tnm;
                   4928:
                   4929:     if ( bucket->m < 0 ) return 0;
                   4930:     else {
                   4931:         nv = NV(d);
                   4932:         mq0 = 0;
                   4933:         tm = (NMV)ALLOCA(nmv_adv);
                   4934:         while ( 1 ) {
1.232     noro     4935:             if ( mod > 0 || mod == -1 )
                   4936:               hindex = head_pbucket(mod,bucket);
                   4937:             else if ( mod == -2 )
                   4938:               hindex = head_pbucket_lf(bucket);
                   4939:             else
                   4940:               hindex = head_pbucket_q(bucket);
1.157     noro     4941:             if ( hindex < 0 ) break;
                   4942:             p = bucket->body[hindex];
                   4943:             NEXTNM(mq0,mq);
                   4944:             ndl_sub(HDL(p),HDL(d),DL(mq));
                   4945:             ndl_copy(DL(mq),DL(tm));
                   4946:             if ( mod ) {
                   4947:                 c1 = invm(HCM(d),mod); c2 = HCM(p);
                   4948:                 DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   4949:                 CM(tm) = mod-c;
                   4950:             } else {
                   4951:                 divsn(NM(HCQ(p)),NM(HCQ(d)),&tnm);
                   4952:                 NTOQ(tnm,SGN(HCQ(p))*SGN(HCQ(d)),CQ(mq));
                   4953:                 chsgnq(CQ(mq),&CQ(tm));
                   4954:             }
                   4955:             t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
                   4956:             bucket->body[hindex] = nd_remove_head(p);
                   4957:             t = nd_remove_head(t);
                   4958:             add_pbucket(mod,bucket,t);
                   4959:         }
                   4960:         if ( !mq0 )
                   4961:             r = 0;
                   4962:         else {
                   4963:             NEXT(mq) = 0;
                   4964:             for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   4965:             MKND(nv,mq0,i,r);
                   4966:             /* XXX */
                   4967:             SG(r) = HTD(r);
                   4968:         }
                   4969:         return r;
                   4970:     }
1.99      noro     4971: }
                   4972:
1.43      noro     4973: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
1.11      noro     4974: {
1.157     noro     4975:     NMV m,mr,mr0,t;
                   4976:     int len,i,k;
1.11      noro     4977:
1.157     noro     4978:     if ( !p ) return;
                   4979:     m = BDY(p); len = LEN(p);
                   4980:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   4981:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   4982:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   4983:     t = (NMV)ALLOCA(nmv_adv);
                   4984:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   4985:         CQ(t) = CQ(m);
                   4986:         for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   4987:         ndl_reconstruct(DL(m),DL(t),obpe,oepos);
                   4988:         CQ(mr) = CQ(t);
                   4989:         ndl_copy(DL(t),DL(mr));
                   4990:     }
                   4991:     BDY(p) = mr0;
1.61      noro     4992: }
                   4993:
                   4994: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   4995: {
1.157     noro     4996:     NMV m,mr,mr0;
                   4997:     int len,i;
                   4998:     NDV r;
                   4999:
                   5000:     if ( !p ) return 0;
                   5001:     m = BDY(p); len = LEN(p);
                   5002:     mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   5003:     for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   5004:         ndl_zero(DL(mr));
                   5005:         ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
                   5006:         CQ(mr) = CQ(m);
                   5007:     }
                   5008:     MKNDV(NV(p),mr0,len,r);
                   5009:     SG(r) = SG(p);
                   5010:     return r;
1.11      noro     5011: }
                   5012:
1.61      noro     5013: /* duplicate p */
                   5014:
                   5015: NDV ndv_dup(int mod,NDV p)
1.3       noro     5016: {
1.157     noro     5017:     NDV d;
                   5018:     NMV t,m,m0;
                   5019:     int i,len;
                   5020:
                   5021:     if ( !p ) return 0;
                   5022:     len = LEN(p);
1.232     noro     5023:     m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
1.157     noro     5024:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   5025:         ndl_copy(DL(t),DL(m));
                   5026:         CQ(m) = CQ(t);
                   5027:     }
                   5028:     MKNDV(NV(p),m0,len,d);
                   5029:     SG(d) = SG(p);
                   5030:     return d;
1.23      noro     5031: }
                   5032:
1.215     noro     5033: NDV ndvtondvgz(NDV p)
                   5034: {
                   5035:        NDV r;
                   5036:        int len,i;
                   5037:        NMV t;
                   5038:
                   5039:        r = ndv_dup(0,p);
                   5040:        len = LEN(p);
                   5041:     for ( t = BDY(r), i = 0; i < len; i++, NMV_ADV(t) ) CZ(t) = ztogz(CQ(t));
                   5042:        return r;
                   5043: }
                   5044:
                   5045: NDV ndvgztondv(NDV p)
                   5046: {
                   5047:        NDV r;
                   5048:        int len,i;
                   5049:        NMV t;
                   5050:
                   5051:        r = ndv_dup(0,p);
                   5052:        len = LEN(p);
                   5053:     for ( t = BDY(r), i = 0; i < len; i++, NMV_ADV(t) ) CQ(t) = gztoz(CZ(t));
                   5054:        return r;
                   5055: }
                   5056:
                   5057: NDV ndv_symbolic(int mod,NDV p)
                   5058: {
                   5059:     NDV d;
                   5060:     NMV t,m,m0;
                   5061:     int i,len;
                   5062:
                   5063:     if ( !p ) return 0;
                   5064:     len = LEN(p);
1.232     noro     5065:     m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
1.215     noro     5066:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   5067:         ndl_copy(DL(t),DL(m));
                   5068:         CQ(m) = ONE;
                   5069:     }
                   5070:     MKNDV(NV(p),m0,len,d);
                   5071:     SG(d) = SG(p);
                   5072:     return d;
                   5073: }
                   5074:
1.63      noro     5075: ND nd_dup(ND p)
                   5076: {
1.157     noro     5077:     ND d;
                   5078:     NM t,m,m0;
1.63      noro     5079:
1.157     noro     5080:     if ( !p ) return 0;
                   5081:     for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   5082:         NEXTNM(m0,m);
                   5083:         ndl_copy(DL(t),DL(m));
                   5084:         CQ(m) = CQ(t);
                   5085:     }
                   5086:     if ( m0 ) NEXT(m) = 0;
                   5087:     MKND(NV(p),m0,LEN(p),d);
                   5088:     SG(d) = SG(p);
                   5089:     return d;
1.63      noro     5090: }
                   5091:
1.215     noro     5092: ND ndtondgz(ND p)
                   5093: {
                   5094:        ND r;
                   5095:        NM t;
                   5096:
                   5097:        r = nd_dup(p);
                   5098:     for ( t = BDY(r); t; t = NEXT(t) ) CZ(t) = ztogz(CQ(t));
                   5099:        return r;
                   5100: }
                   5101:
                   5102:
                   5103: ND ndgztond(ND p)
                   5104: {
                   5105:        ND r;
                   5106:        NM t;
                   5107:
                   5108:        r = nd_dup(p);
                   5109:     for ( t = BDY(r); t; t = NEXT(t) ) CQ(t) = gztoz(CZ(t));
                   5110:        return r;
                   5111: }
                   5112:
                   5113:
1.61      noro     5114: /* XXX if p->len == 0 then it represents 0 */
                   5115:
                   5116: void ndv_mod(int mod,NDV p)
                   5117: {
1.157     noro     5118:     NMV t,d;
                   5119:     int r,s,u;
                   5120:     int i,len,dlen;
                   5121:     P cp;
                   5122:     Q c;
                   5123:     Obj gfs;
                   5124:
                   5125:     if ( !p ) return;
                   5126:     len = LEN(p);
                   5127:     dlen = 0;
                   5128:     if ( mod == -1 )
                   5129:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   5130:             simp_ff((Obj)CP(t),&gfs);
                   5131:             r = FTOIF(CONT((GFS)gfs));
                   5132:             CM(d) = r;
                   5133:             ndl_copy(DL(t),DL(d));
                   5134:             NMV_ADV(d);
                   5135:             dlen++;
                   5136:         }
1.232     noro     5137:     else if ( mod == -2 )
                   5138:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   5139:             simp_ff((Obj)CP(t),&gfs); lmtolf(gfs,&CZ(d));
                   5140:             ndl_copy(DL(t),DL(d));
                   5141:             NMV_ADV(d);
                   5142:             dlen++;
                   5143:         }
1.157     noro     5144:     else
                   5145:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   5146:             if ( nd_vc ) {
                   5147:                 nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
                   5148:                 c = (Q)cp;
                   5149:             } else
                   5150:                 c = CQ(t);
                   5151:             r = rem(NM(c),mod);
                   5152:             if ( r ) {
                   5153:                 if ( SGN(c) < 0 )
                   5154:                     r = mod-r;
                   5155:                 if ( DN(c) ) {
                   5156:                     s = rem(DN(c),mod);
                   5157:                     if ( !s )
                   5158:                         error("ndv_mod : division by 0");
                   5159:                     s = invm(s,mod);
                   5160:                     DMAR(r,s,0,mod,u); r = u;
                   5161:                 }
                   5162:                 CM(d) = r;
                   5163:                 ndl_copy(DL(t),DL(d));
                   5164:                 NMV_ADV(d);
                   5165:                 dlen++;
                   5166:             }
                   5167:         }
                   5168:     LEN(p) = dlen;
1.61      noro     5169: }
                   5170:
                   5171: NDV ptondv(VL vl,VL dvl,P p)
                   5172: {
1.157     noro     5173:     ND nd;
                   5174:
                   5175:     nd = ptond(vl,dvl,p);
                   5176:     return ndtondv(0,nd);
                   5177: }
1.61      noro     5178:
1.157     noro     5179: void pltozpl(LIST l,Q *cont,LIST *pp)
                   5180: {
                   5181:     NODE nd,nd1;
                   5182:     int n;
                   5183:     P *pl;
                   5184:     Q *cl;
                   5185:     int i;
                   5186:     P dmy;
                   5187:     Q dvr;
                   5188:     LIST r;
                   5189:
                   5190:     nd = BDY(l); n = length(nd);
                   5191:     pl = (P *)ALLOCA(n*sizeof(P));
                   5192:     cl = (Q *)ALLOCA(n*sizeof(P));
                   5193:     for ( i = 0; i < n; i++, nd = NEXT(nd) )
                   5194:         ptozp((P)BDY(nd),1,&cl[i],&dmy);
                   5195:     qltozl(cl,n,&dvr);
                   5196:     nd = BDY(l);
                   5197:     for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
                   5198:         divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
                   5199:     }
                   5200:     nd = 0;
                   5201:     for ( i = n-1; i >= 0; i-- ) {
                   5202:         MKNODE(nd1,pl[i],nd); nd = nd1;
                   5203:     }
                   5204:     MKLIST(r,nd);
                   5205:     *pp = r;
                   5206: }
                   5207:
                   5208: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
                   5209:
                   5210: NDV pltondv(VL vl,VL dvl,LIST p)
                   5211: {
                   5212:     int i;
                   5213:     NODE t;
                   5214:     ND r,ri;
                   5215:     NM m;
                   5216:
                   5217:     if ( !nd_module ) error("pltond : module order must be set");
                   5218:     r = 0;
                   5219:     for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
                   5220:         ri = ptond(vl,dvl,(P)BDY(t));
1.163     noro     5221:         if ( ri )
                   5222:             for ( m = BDY(ri); m; m = NEXT(m) ) {
1.167     noro     5223:                 MPOS(DL(m)) = i;
                   5224:                 TD(DL(m)) = ndl_weight(DL(m));
1.163     noro     5225:                 if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   5226:             }
1.157     noro     5227:         r = nd_add(0,r,ri);
                   5228:     }
                   5229:     return ndtondv(0,r);
1.61      noro     5230: }
                   5231:
                   5232: ND ptond(VL vl,VL dvl,P p)
1.23      noro     5233: {
1.157     noro     5234:     int n,i,j,k,e;
                   5235:     VL tvl;
                   5236:     V v;
                   5237:     DCP dc;
                   5238:     DCP *w;
                   5239:     ND r,s,t,u;
                   5240:     P x;
                   5241:     int c;
                   5242:     UINT *d;
                   5243:     NM m,m0;
                   5244:
                   5245:     if ( !p )
                   5246:         return 0;
                   5247:     else if ( NUM(p) ) {
                   5248:         NEWNM(m);
                   5249:         ndl_zero(DL(m));
                   5250:         CQ(m) = (Q)p;
                   5251:         NEXT(m) = 0;
                   5252:         MKND(nd_nvar,m,1,r);
                   5253:         SG(r) = 0;
                   5254:         return r;
                   5255:     } else {
                   5256:         for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   5257:         w = (DCP *)ALLOCA(k*sizeof(DCP));
                   5258:         for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   5259:         for ( i = 0, tvl = dvl, v = VR(p);
                   5260:             tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   5261:         if ( !tvl ) {
                   5262:             for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   5263:                 t = ptond(vl,dvl,COEF(w[j]));
                   5264:                 pwrp(vl,x,DEG(w[j]),&p);
                   5265:                 nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   5266:             }
                   5267:             return s;
                   5268:         } else {
                   5269:             NEWNM(m0); d = DL(m0);
                   5270:             for ( j = k-1, s = 0; j >= 0; j-- ) {
                   5271:                 ndl_zero(d); e = QTOS(DEG(w[j])); PUT_EXP(d,i,e);
                   5272:                 TD(d) = MUL_WEIGHT(e,i);
                   5273:                 if ( nd_blockmask) ndl_weight_mask(d);
                   5274:                 if ( nd_module ) MPOS(d) = 0;
                   5275:                 t = ptond(vl,dvl,COEF(w[j]));
                   5276:                 for ( m = BDY(t); m; m = NEXT(m) )
                   5277:                     ndl_addto(DL(m),d);
                   5278:                 SG(t) += TD(d);
                   5279:                 s = nd_add(0,s,t);
                   5280:             }
                   5281:             FREENM(m0);
                   5282:             return s;
                   5283:         }
                   5284:     }
1.61      noro     5285: }
                   5286:
                   5287: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   5288: {
1.157     noro     5289:     VL tvl;
                   5290:     int len,n,j,i,e;
                   5291:     NMV m;
                   5292:     Q q;
                   5293:     P c;
                   5294:     UINT *d;
                   5295:     P s,r,u,t,w;
                   5296:     GFS gfs;
                   5297:
                   5298:     if ( !p ) return 0;
                   5299:     else {
                   5300:         len = LEN(p);
                   5301:         n = NV(p);
                   5302:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   5303:         for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   5304:             if ( mod == -1 ) {
1.232     noro     5305:                e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   5306:             } else if ( mod == -2 ) {
                   5307:                c = gztoz(CZ(m));
                   5308:             } else if ( mod > 0 ) {
1.157     noro     5309:                 STOQ(CM(m),q); c = (P)q;
                   5310:             } else
                   5311:                 c = CP(m);
                   5312:             d = DL(m);
                   5313:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   5314:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   5315:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   5316:             }
                   5317:             addp(vl,s,t,&u); s = u;
                   5318:         }
                   5319:         return s;
                   5320:     }
                   5321: }
                   5322:
                   5323: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
                   5324: {
                   5325:     VL tvl;
                   5326:     int len,n,j,i,e;
                   5327:     NMV m;
                   5328:     Q q;
                   5329:     P c;
                   5330:     UINT *d;
                   5331:     P s,r,u,t,w;
                   5332:     GFS gfs;
                   5333:     P *a;
                   5334:     LIST l;
                   5335:     NODE nd,nd1;
                   5336:
                   5337:     if ( !p ) return 0;
                   5338:     else {
                   5339:         a = (P *)ALLOCA((rank+1)*sizeof(P));
                   5340:         for ( i = 0; i <= rank; i++ ) a[i] = 0;
                   5341:         len = LEN(p);
                   5342:         n = NV(p);
                   5343:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   5344:         for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
                   5345:             if ( mod == -1 ) {
                   5346:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   5347:             } else if ( mod ) {
                   5348:                 STOQ(CM(m),q); c = (P)q;
                   5349:             } else
                   5350:                 c = CP(m);
                   5351:             d = DL(m);
                   5352:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   5353:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   5354:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   5355:             }
                   5356:             addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
                   5357:         }
                   5358:         nd = 0;
                   5359:         for ( i = rank; i > 0; i-- ) {
                   5360:             MKNODE(nd1,a[i],nd); nd = nd1;
                   5361:         }
                   5362:         MKLIST(l,nd);
                   5363:         return l;
                   5364:     }
1.3       noro     5365: }
                   5366:
1.61      noro     5367: NDV ndtondv(int mod,ND p)
1.11      noro     5368: {
1.157     noro     5369:     NDV d;
                   5370:     NMV m,m0;
                   5371:     NM t;
                   5372:     int i,len;
                   5373:
                   5374:     if ( !p ) return 0;
                   5375:     len = LEN(p);
1.232     noro     5376:     if ( mod > 0 || mod == -1 )
1.200     noro     5377:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
1.157     noro     5378:     else
                   5379:         m0 = m = MALLOC(len*nmv_adv);
1.103     noro     5380: #if 0
1.157     noro     5381:     ndv_alloc += nmv_adv*len;
1.103     noro     5382: #endif
1.157     noro     5383:     for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   5384:         ndl_copy(DL(t),DL(m));
                   5385:         CQ(m) = CQ(t);
                   5386:     }
                   5387:     MKNDV(NV(p),m0,len,d);
                   5388:     SG(d) = SG(p);
                   5389:     return d;
1.11      noro     5390: }
                   5391:
1.61      noro     5392: ND ndvtond(int mod,NDV p)
1.11      noro     5393: {
1.157     noro     5394:     ND d;
                   5395:     NM m,m0;
                   5396:     NMV t;
                   5397:     int i,len;
                   5398:
                   5399:     if ( !p ) return 0;
                   5400:     m0 = 0;
                   5401:     len = p->len;
                   5402:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   5403:         NEXTNM(m0,m);
                   5404:         ndl_copy(DL(t),DL(m));
                   5405:         CQ(m) = CQ(t);
                   5406:     }
                   5407:     NEXT(m) = 0;
                   5408:     MKND(NV(p),m0,len,d);
                   5409:     SG(d) = SG(p);
                   5410:     return d;
1.11      noro     5411: }
                   5412:
1.198     noro     5413: DP ndvtodp(int mod,NDV p)
                   5414: {
                   5415:     MP m,m0;
                   5416:        DP d;
                   5417:     NMV t;
                   5418:     int i,len;
                   5419:
                   5420:     if ( !p ) return 0;
                   5421:     m0 = 0;
                   5422:     len = p->len;
                   5423:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   5424:         NEXTMP(m0,m);
                   5425:        m->dl = ndltodl(nd_nvar,DL(t));
                   5426:        m->c = ndctop(mod,t->c);
                   5427:     }
                   5428:     NEXT(m) = 0;
                   5429:        MKDP(nd_nvar,m0,d);
                   5430:     SG(d) = SG(p);
                   5431:     return d;
                   5432: }
                   5433:
1.204     noro     5434: DP ndtodp(int mod,ND p)
                   5435: {
                   5436:     MP m,m0;
                   5437:        DP d;
                   5438:     NM t;
                   5439:     int i,len;
                   5440:
                   5441:     if ( !p ) return 0;
                   5442:     m0 = 0;
                   5443:     len = p->len;
                   5444:     for ( t = BDY(p); t; t = NEXT(t) ) {
                   5445:         NEXTMP(m0,m);
                   5446:        m->dl = ndltodl(nd_nvar,DL(t));
                   5447:        m->c = ndctop(mod,t->c);
                   5448:     }
                   5449:     NEXT(m) = 0;
                   5450:        MKDP(nd_nvar,m0,d);
                   5451:     SG(d) = SG(p);
                   5452:     return d;
                   5453: }
                   5454:
1.3       noro     5455: void ndv_print(NDV p)
                   5456: {
1.157     noro     5457:     NMV m;
                   5458:     int i,len;
1.3       noro     5459:
1.157     noro     5460:     if ( !p ) printf("0\n");
                   5461:     else {
                   5462:         len = LEN(p);
                   5463:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   5464:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   5465:             else printf("+%d*",CM(m));
                   5466:             ndl_print(DL(m));
                   5467:         }
                   5468:         printf("\n");
                   5469:     }
1.16      noro     5470: }
                   5471:
1.113     noro     5472: void ndv_print_q(NDV p)
1.16      noro     5473: {
1.157     noro     5474:     NMV m;
                   5475:     int i,len;
1.16      noro     5476:
1.157     noro     5477:     if ( !p ) printf("0\n");
                   5478:     else {
                   5479:         len = LEN(p);
                   5480:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   5481:             printf("+");
                   5482:             printexpr(CO,(Obj)CQ(m));
                   5483:             printf("*");
                   5484:             ndl_print(DL(m));
                   5485:         }
                   5486:         printf("\n");
                   5487:     }
1.25      noro     5488: }
                   5489:
1.167     noro     5490: NODE ndv_reducebase(NODE x,int *perm)
1.27      noro     5491: {
1.157     noro     5492:     int len,i,j;
1.167     noro     5493:     NDVI w;
1.157     noro     5494:     NODE t,t0;
                   5495:
                   5496:     len = length(x);
1.167     noro     5497:     w = (NDVI)ALLOCA(len*sizeof(struct oNDVI));
                   5498:     for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
                   5499:         w[i].p = BDY(t); w[i].i = perm[i];
                   5500:     }
1.157     noro     5501:     for ( i = 0; i < len; i++ ) {
                   5502:         for ( j = 0; j < i; j++ ) {
1.167     noro     5503:             if ( w[i].p && w[j].p )
                   5504:                 if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
                   5505:                 else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
1.157     noro     5506:         }
                   5507:     }
1.167     noro     5508:     for ( i = j = 0, t0 = 0; i < len; i++ ) {
                   5509:         if ( w[i].p ) {
                   5510:             NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
                   5511:             perm[j++] = w[i].i;
                   5512:         }
1.157     noro     5513:     }
                   5514:     NEXT(t) = 0; x = t0;
                   5515:     return x;
1.11      noro     5516: }
1.32      noro     5517:
1.43      noro     5518: /* XXX incomplete */
                   5519:
1.32      noro     5520: void nd_init_ord(struct order_spec *ord)
                   5521: {
1.224     noro     5522:   nd_module = (ord->id >= 256);
                   5523:   if ( nd_module ) {
                   5524:     nd_dcomp = -1;
                   5525:     nd_ispot = ord->ispot;
                   5526:     nd_pot_nelim = ord->pot_nelim;
                   5527:     nd_poly_weight_len = ord->nv;
                   5528:     nd_poly_weight = ord->top_weight;
                   5529:     nd_module_rank = ord->module_rank;
                   5530:     nd_module_weight = ord->module_top_weight;
                   5531:   }
1.203     noro     5532:        nd_matrix = 0;
                   5533:        nd_matrix_len = 0;
1.157     noro     5534:     switch ( ord->id ) {
                   5535:         case 0:
                   5536:             switch ( ord->ord.simple ) {
                   5537:                 case 0:
                   5538:                     nd_dcomp = 1;
                   5539:                     nd_isrlex = 1;
                   5540:                     break;
                   5541:                 case 1:
                   5542:                     nd_dcomp = 1;
                   5543:                     nd_isrlex = 0;
                   5544:                     break;
                   5545:                 case 2:
                   5546:                     nd_dcomp = 0;
                   5547:                     nd_isrlex = 0;
                   5548:                     ndl_compare_function = ndl_lex_compare;
                   5549:                     break;
                   5550:                 case 11:
                   5551:                     /* XXX */
                   5552:                     nd_dcomp = 0;
                   5553:                     nd_isrlex = 1;
                   5554:                     ndl_compare_function = ndl_ww_lex_compare;
                   5555:                     break;
                   5556:                 default:
                   5557:                     error("nd_gr : unsupported order");
                   5558:             }
                   5559:             break;
                   5560:         case 1:
                   5561:             /* block order */
                   5562:             /* XXX */
                   5563:             nd_dcomp = -1;
                   5564:             nd_isrlex = 0;
                   5565:             ndl_compare_function = ndl_block_compare;
                   5566:             break;
                   5567:         case 2:
                   5568:             /* matrix order */
                   5569:             /* XXX */
                   5570:             nd_dcomp = -1;
                   5571:             nd_isrlex = 0;
                   5572:             nd_matrix_len = ord->ord.matrix.row;
                   5573:             nd_matrix = ord->ord.matrix.matrix;
                   5574:             ndl_compare_function = ndl_matrix_compare;
                   5575:             break;
                   5576:         case 3:
                   5577:             /* composite order */
                   5578:             nd_dcomp = -1;
                   5579:             nd_isrlex = 0;
                   5580:             nd_worb_len = ord->ord.composite.length;
                   5581:             nd_worb = ord->ord.composite.w_or_b;
                   5582:             ndl_compare_function = ndl_composite_compare;
                   5583:             break;
                   5584:
                   5585:         /* module order */
                   5586:         case 256:
                   5587:             switch ( ord->ord.simple ) {
                   5588:                 case 0:
1.167     noro     5589:                     nd_isrlex = 1;
1.157     noro     5590:                     ndl_compare_function = ndl_module_grlex_compare;
                   5591:                     break;
                   5592:                 case 1:
1.167     noro     5593:                     nd_isrlex = 0;
1.157     noro     5594:                     ndl_compare_function = ndl_module_glex_compare;
                   5595:                     break;
                   5596:                 case 2:
1.167     noro     5597:                     nd_isrlex = 0;
1.157     noro     5598:                     ndl_compare_function = ndl_module_lex_compare;
                   5599:                     break;
                   5600:                 default:
                   5601:                     error("nd_gr : unsupported order");
                   5602:             }
                   5603:             break;
                   5604:         case 257:
                   5605:             /* block order */
1.174     noro     5606:             nd_isrlex = 0;
1.157     noro     5607:             ndl_compare_function = ndl_module_block_compare;
                   5608:             break;
                   5609:         case 258:
                   5610:             /* matrix order */
1.174     noro     5611:             nd_isrlex = 0;
1.157     noro     5612:             nd_matrix_len = ord->ord.matrix.row;
                   5613:             nd_matrix = ord->ord.matrix.matrix;
                   5614:             ndl_compare_function = ndl_module_matrix_compare;
                   5615:             break;
                   5616:         case 259:
                   5617:             /* composite order */
1.174     noro     5618:             nd_isrlex = 0;
1.157     noro     5619:             nd_worb_len = ord->ord.composite.length;
                   5620:             nd_worb = ord->ord.composite.w_or_b;
                   5621:             ndl_compare_function = ndl_module_composite_compare;
                   5622:             break;
                   5623:     }
                   5624:     nd_ord = ord;
1.32      noro     5625: }
                   5626:
1.43      noro     5627: BlockMask nd_create_blockmask(struct order_spec *ord)
                   5628: {
1.157     noro     5629:     int n,i,j,s,l;
                   5630:     UINT *t;
                   5631:     BlockMask bm;
                   5632:
                   5633:     /* we only create mask table for block order */
1.164     noro     5634:     if ( ord->id != 1 && ord->id != 257 )
1.157     noro     5635:         return 0;
                   5636:     n = ord->ord.block.length;
                   5637:     bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   5638:     bm->n = n;
                   5639:     bm->order_pair = ord->ord.block.order_pair;
                   5640:     bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
                   5641:     for ( i = 0, s = 0; i < n; i++ ) {
                   5642:         bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
                   5643:         for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   5644:         l = bm->order_pair[i].length;
                   5645:         for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   5646:     }
                   5647:     return bm;
1.57      noro     5648: }
                   5649:
                   5650: EPOS nd_create_epos(struct order_spec *ord)
                   5651: {
1.157     noro     5652:     int i,j,l,s,ord_l,ord_o;
                   5653:     EPOS epos;
                   5654:     struct order_pair *op;
                   5655:
                   5656:     epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   5657:     switch ( ord->id ) {
1.164     noro     5658:         case 0: case 256:
1.157     noro     5659:             if ( nd_isrlex ) {
                   5660:                 for ( i = 0; i < nd_nvar; i++ ) {
                   5661:                     epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   5662:                     epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   5663:                 }
                   5664:             } else {
                   5665:                 for ( i = 0; i < nd_nvar; i++ ) {
                   5666:                     epos[i].i = nd_exporigin + i/nd_epw;
                   5667:                     epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   5668:                 }
                   5669:             }
                   5670:             break;
1.164     noro     5671:         case 1: case 257:
1.157     noro     5672:             /* block order */
                   5673:             l = ord->ord.block.length;
                   5674:             op = ord->ord.block.order_pair;
                   5675:             for ( j = 0, s = 0; j < l; j++ ) {
                   5676:                 ord_o = op[j].order;
                   5677:                 ord_l = op[j].length;
                   5678:                 if ( !ord_o )
                   5679:                     for ( i = 0; i < ord_l; i++ ) {
                   5680:                         epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   5681:                         epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   5682:                     }
                   5683:                 else
                   5684:                     for ( i = 0; i < ord_l; i++ ) {
                   5685:                         epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   5686:                         epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   5687:                     }
                   5688:                 s += ord_l;
                   5689:             }
                   5690:             break;
                   5691:         case 2:
                   5692:             /* matrix order */
                   5693:         case 3:
                   5694:             /* composite order */
1.167     noro     5695:         default:
1.157     noro     5696:             for ( i = 0; i < nd_nvar; i++ ) {
                   5697:                 epos[i].i = nd_exporigin + i/nd_epw;
                   5698:                 epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   5699:             }
                   5700:             break;
                   5701:     }
                   5702:     return epos;
1.43      noro     5703: }
1.59      noro     5704:
                   5705: /* external interface */
                   5706:
1.191     noro     5707: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
1.59      noro     5708: {
1.157     noro     5709:     NODE t,in0,in;
1.191     noro     5710:     ND ndf,nf;
                   5711:     NDV ndvf;
1.157     noro     5712:     VL vv,tv;
1.191     noro     5713:     int stat,nvar,max,mrank;
1.157     noro     5714:     union oNDC dn;
                   5715:     Q cont;
                   5716:     P pp;
1.191     noro     5717:     LIST ppl;
1.157     noro     5718:
                   5719:     if ( !f ) {
                   5720:         *rp = 0;
                   5721:         return;
                   5722:     }
                   5723:     pltovl(v,&vv);
                   5724:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5725:
1.191     noro     5726:     /* max=65536 implies nd_bpe=32 */
                   5727:     max = 65536;
1.157     noro     5728:
1.191     noro     5729:        nd_module = 0;
                   5730:        /* nd_module will be set if ord is a module ordering */
1.157     noro     5731:     nd_init_ord(ord);
                   5732:     nd_setup_parameters(nvar,max);
1.191     noro     5733:     if ( nd_module && OID(f) != O_LIST )
                   5734:         error("nd_nf_p : the first argument must be a list");
                   5735:        if ( nd_module ) mrank = length(BDY((LIST)f));
1.157     noro     5736:     /* conversion to ndv */
                   5737:     for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   5738:         NEXTNODE(in0,in);
1.191     noro     5739:         if ( nd_module ) {
                   5740:           if ( !BDY(t) || OID(BDY(t)) != O_LIST
                   5741:                || length(BDY((LIST)BDY(t))) != mrank )
                   5742:               error("nd_nf_p : inconsistent basis element");
                   5743:           if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
                   5744:           else ppl = (LIST)BDY(t);
                   5745:           BDY(in) = (pointer)pltondv(CO,vv,ppl);
                   5746:         } else {
                   5747:           if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
                   5748:           else pp = (P)BDY(t);
                   5749:           BDY(in) = (pointer)ptondv(CO,vv,pp);
                   5750:         }
1.157     noro     5751:         if ( m ) ndv_mod(m,(NDV)BDY(in));
                   5752:     }
1.191     noro     5753:     if ( in0 ) NEXT(in) = 0;
                   5754:
                   5755:     if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
                   5756:     else ndvf = ptondv(CO,vv,(P)f);
                   5757:     if ( m ) ndv_mod(m,ndvf);
                   5758:     ndf = (pointer)ndvtond(m,ndvf);
1.157     noro     5759:
                   5760:     /* dont sort, dont removecont */
                   5761:     ndv_setup(m,0,in0,1,1);
                   5762:     nd_scale=2;
1.191     noro     5763:     stat = nd_nf(m,0,ndf,nd_ps,1,0,&nf);
                   5764:     if ( !stat )
                   5765:         error("nd_nf_p : exponent too large");
                   5766:     if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
                   5767:     else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
1.63      noro     5768: }
                   5769:
                   5770: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   5771: {
1.157     noro     5772:     NM m;
                   5773:     UINT *t,*s;
                   5774:     int i;
                   5775:
                   5776:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   5777:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   5778:         t = DL(m);
                   5779:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5780:         r[i] = CM(m);
                   5781:     }
                   5782:     for ( i = 0; !r[i]; i++ );
                   5783:     return i;
1.63      noro     5784: }
                   5785:
1.113     noro     5786: int nd_to_vect_q(UINT *s0,int n,ND d,Q *r)
1.74      noro     5787: {
1.157     noro     5788:     NM m;
                   5789:     UINT *t,*s;
                   5790:     int i;
                   5791:
                   5792:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   5793:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   5794:         t = DL(m);
                   5795:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5796:         r[i] = CQ(m);
                   5797:     }
                   5798:     for ( i = 0; !r[i]; i++ );
                   5799:     return i;
1.74      noro     5800: }
                   5801:
1.232     noro     5802: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
                   5803: {
                   5804:     NM m;
                   5805:     UINT *t,*s;
                   5806:     int i;
                   5807:
                   5808:     for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
                   5809:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   5810:         t = DL(m);
                   5811:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5812:         mpz_set(r[i],BDY(CZ(m)));
                   5813:     }
                   5814:     for ( i = 0; !mpz_sgn(r[i]); i++ );
                   5815:     return i;
                   5816: }
                   5817:
1.220     noro     5818: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
                   5819: {
                   5820:     NM m;
                   5821:     unsigned long *v;
                   5822:     int i,j,h,size;
                   5823:        UINT *s,*t;
                   5824:
                   5825:        size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
                   5826:     v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
                   5827:     bzero(v,size);
                   5828:     for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
                   5829:                t = DL(m);
                   5830:                h = ndl_hash_value(t);
                   5831:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   5832:            v[i/BLEN] |= 1L <<(i%BLEN);
                   5833:     }
                   5834:     return v;
                   5835: }
                   5836:
                   5837: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
                   5838: {
                   5839:     NMV mr;
                   5840:     UINT *d,*t,*s;
                   5841:     int i,j,len,h,head;
                   5842:
                   5843:     d = DL(m);
                   5844:     len = LEN(p);
                   5845:     t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   5846:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   5847:         ndl_add(d,DL(mr),t);
                   5848:                h = ndl_hash_value(t);
                   5849:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   5850:                if ( j == 0 ) head = i;
                   5851:            v[i/BLEN] |= 1L <<(i%BLEN);
                   5852:     }
                   5853:     return head;
                   5854: }
                   5855:
1.129     noro     5856: Q *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   5857: {
1.157     noro     5858:     NM m;
                   5859:     NMV mr;
                   5860:     UINT *d,*t,*s;
                   5861:     NDV p;
                   5862:     int i,j,len;
                   5863:     Q *r;
                   5864:
                   5865:     m = pair->mul;
                   5866:     d = DL(m);
                   5867:     p = nd_ps[pair->index];
                   5868:     len = LEN(p);
                   5869:     r = (Q *)CALLOC(n,sizeof(Q));
                   5870:     t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   5871:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   5872:         ndl_add(d,DL(mr),t);
                   5873:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5874:         r[i] = CQ(mr);
                   5875:     }
                   5876:     return r;
1.129     noro     5877: }
                   5878:
1.210     noro     5879: IndArray nm_ind_pair_to_vect_compress(int mod,UINT *s0,int n,int *s0hash,NM_ind_pair pair)
1.64      noro     5880: {
1.157     noro     5881:     NM m;
                   5882:     NMV mr;
                   5883:     UINT *d,*t,*s;
                   5884:     NDV p;
                   5885:     unsigned char *ivc;
                   5886:     unsigned short *ivs;
                   5887:     UINT *v,*ivi,*s0v;
1.210     noro     5888:     int i,j,len,prev,diff,cdiff,h;
1.157     noro     5889:     IndArray r;
1.198     noro     5890: struct oEGT eg0,eg1;
1.157     noro     5891:
                   5892:     m = pair->mul;
                   5893:     d = DL(m);
1.215     noro     5894:     p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
1.157     noro     5895:     len = LEN(p);
                   5896:     t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   5897:     v = (unsigned int *)ALLOCA(len*sizeof(unsigned int));
1.198     noro     5898: get_eg(&eg0);
1.157     noro     5899:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   5900:         ndl_add(d,DL(mr),t);
1.210     noro     5901:                h = ndl_hash_value(t);
                   5902:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
1.157     noro     5903:         v[j] = i;
                   5904:     }
1.198     noro     5905: get_eg(&eg1); add_eg(&eg_search,&eg0,&eg1);
1.157     noro     5906:     r = (IndArray)MALLOC(sizeof(struct oIndArray));
                   5907:     r->head = v[0];
                   5908:     diff = 0;
                   5909:     for ( i = 1; i < len; i++ ) {
                   5910:         cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   5911:     }
                   5912:     if ( diff < 256 ) {
                   5913:         r->width = 1;
                   5914:         ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   5915:         r->index.c = ivc;
                   5916:         for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   5917:     } else if ( diff < 65536 ) {
                   5918:         r->width = 2;
                   5919:         ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   5920:         r->index.s = ivs;
                   5921:         for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   5922:     } else {
                   5923:         r->width = 4;
                   5924:         ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   5925:         r->index.i = ivi;
                   5926:         for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
                   5927:     }
                   5928:     return r;
1.64      noro     5929: }
                   5930:
1.135     noro     5931: int compress_array(Q *svect,Q *cvect,int n)
                   5932: {
1.157     noro     5933:     int i,j;
1.135     noro     5934:
1.157     noro     5935:     for ( i = j = 0; i < n; i++ )
                   5936:         if ( svect[i] ) cvect[j++] = svect[i];
                   5937:     return j;
1.135     noro     5938: }
                   5939:
                   5940: void expand_array(Q *svect,Q *cvect,int n)
                   5941: {
1.157     noro     5942:     int i,j;
1.135     noro     5943:
1.157     noro     5944:     for ( i = j = 0; j < n;  i++  )
                   5945:         if ( svect[i] ) svect[i] = cvect[j++];
1.135     noro     5946: }
                   5947:
1.133     noro     5948: int ndv_reduce_vect_q(Q *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.107     noro     5949: {
1.157     noro     5950:     int i,j,k,len,pos,prev,nz;
                   5951:     Q cs,mcs,c1,c2,cr,gcd,t;
                   5952:     IndArray ivect;
                   5953:     unsigned char *ivc;
                   5954:     unsigned short *ivs;
                   5955:     unsigned int *ivi;
                   5956:     NDV redv;
                   5957:     NMV mr;
                   5958:     NODE rp;
                   5959:     int maxrs;
                   5960:     double hmag;
                   5961:     Q *cvect;
                   5962:
                   5963:     maxrs = 0;
                   5964:     for ( i = 0; i < col && !svect[i]; i++ );
                   5965:     if ( i == col ) return maxrs;
                   5966:     hmag = p_mag((P)svect[i])*nd_scale;
                   5967:     cvect = (Q *)ALLOCA(col*sizeof(Q));
                   5968:     for ( i = 0; i < nred; i++ ) {
                   5969:         ivect = imat[i];
                   5970:         k = ivect->head;
                   5971:         if ( svect[k] ) {
                   5972:             maxrs = MAX(maxrs,rp0[i]->sugar);
1.215     noro     5973:             redv = nd_demand?ndv_load(rp0[i]->index)
                   5974:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
1.157     noro     5975:             len = LEN(redv); mr = BDY(redv);
                   5976:             igcd_cofactor(svect[k],CQ(mr),&gcd,&cs,&cr);
                   5977:             chsgnq(cs,&mcs);
                   5978:             if ( !UNIQ(cr) ) {
                   5979:                 for ( j = 0; j < col; j++ ) {
                   5980:                     mulq(svect[j],cr,&c1); svect[j] = c1;
                   5981:                 }
                   5982:             }
                   5983:             svect[k] = 0; prev = k;
                   5984:             switch ( ivect->width ) {
                   5985:                 case 1:
                   5986:                     ivc = ivect->index.c;
                   5987:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5988:                         pos = prev+ivc[j]; prev = pos;
                   5989:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   5990:                     }
                   5991:                     break;
                   5992:                 case 2:
                   5993:                     ivs = ivect->index.s;
                   5994:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   5995:                         pos = prev+ivs[j]; prev = pos;
                   5996:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   5997:                     }
                   5998:                     break;
                   5999:                 case 4:
                   6000:                     ivi = ivect->index.i;
                   6001:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6002:                         pos = prev+ivi[j]; prev = pos;
                   6003:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   6004:                     }
                   6005:                     break;
                   6006:             }
                   6007:             for ( j = k+1; j < col && !svect[j]; j++ );
                   6008:             if ( j == col ) break;
                   6009:             if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
                   6010:                 nz = compress_array(svect,cvect,col);
                   6011:                 removecont_array((P *)cvect,nz,1);
                   6012:                 expand_array(svect,cvect,nz);
                   6013:                 hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   6014:             }
                   6015:         }
                   6016:     }
                   6017:     nz = compress_array(svect,cvect,col);
                   6018:     removecont_array((P *)cvect,nz,1);
                   6019:     expand_array(svect,cvect,nz);
                   6020:     if ( DP_Print ) {
                   6021:         fprintf(asir_out,"-"); fflush(asir_out);
                   6022:     }
                   6023:     return maxrs;
1.107     noro     6024: }
                   6025:
1.215     noro     6026: int ndv_reduce_vect_gz(GZ *gvect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   6027: {
                   6028:     int i,j,k,l,len,pos,prev,nz;
                   6029:     GZ cs,mcs,c1,c2,cr,gcd,t;
                   6030:     IndArray ivect;
                   6031:     unsigned char *ivc;
                   6032:     unsigned short *ivs;
                   6033:     unsigned int *ivi;
                   6034:     NDV redv;
                   6035:     NMV mr;
                   6036:     NODE rp;
                   6037:     int maxrs;
                   6038:     double hmag;
                   6039:        struct oVECT v;
                   6040:
                   6041:     maxrs = 0;
                   6042:     for ( i = 0; i < col && !gvect[i]; i++ );
                   6043:     if ( i == col ) return maxrs;
                   6044:     hmag = (double)n_bits_gz(gvect[i])*nd_scale;
                   6045:     for ( i = 0; i < nred; i++ ) {
                   6046:         ivect = imat[i];
                   6047:         k = ivect->head;
                   6048:         if ( gvect[k] ) {
                   6049:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   6050:             redv = nd_ps_gz[rp0[i]->index];
                   6051:             len = LEN(redv); mr = BDY(redv);
                   6052:                        gcdgz(gvect[k],CZ(mr),&gcd);
                   6053:                        divsgz(gvect[k],gcd,&cs);
                   6054:                        divsgz(CZ(mr),gcd,&cr);
                   6055:             chsgngz(cs,&mcs);
                   6056:                        if ( !UNIGZ(cr) ) {
                   6057:                for ( j = 0; j < col; j++ ) {
                   6058:                     mulgz(gvect[j],cr,&c1); gvect[j] = c1;
                   6059:                 }
                   6060:             }
                   6061:             gvect[k] = 0; prev = k;
                   6062:             switch ( ivect->width ) {
                   6063:                 case 1:
                   6064:                     ivc = ivect->index.c;
                   6065:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6066:                         pos = prev+ivc[j]; prev = pos;
                   6067:                         mulgz(CZ(mr),mcs,&c2); addgz(gvect[pos],c2,&t); gvect[pos] = t;
                   6068:                     }
                   6069:                     break;
                   6070:                 case 2:
                   6071:                     ivs = ivect->index.s;
                   6072:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6073:                         pos = prev+ivs[j]; prev = pos;
                   6074:                         mulgz(CZ(mr),mcs,&c2); addgz(gvect[pos],c2,&t); gvect[pos] = t;
                   6075:                     }
                   6076:                     break;
                   6077:                 case 4:
                   6078:                     ivi = ivect->index.i;
                   6079:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6080:                         pos = prev+ivi[j]; prev = pos;
                   6081:                         mulgz(CZ(mr),mcs,&c2); addgz(gvect[pos],c2,&t); gvect[pos] = t;
                   6082:                     }
                   6083:                     break;
                   6084:             }
                   6085:             for ( j = k+1; j < col && !gvect[j]; j++ );
                   6086:             if ( j == col ) break;
                   6087:             if ( hmag && ((double)n_bits_gz(gvect[j]) > hmag) ) {
                   6088:                                v.len = col; v.body = (pointer)gvect; gcdvgz(&v,&gcd);
                   6089: #if 1
                   6090:                                for ( l = 0; l < col; l++ ) { divsgz(gvect[l],gcd,&t); gvect[l] = t; }
                   6091: #endif
                   6092:                        hmag = (double)n_bits_gz(gvect[j])*nd_scale;
                   6093:             }
                   6094:         }
                   6095:     }
                   6096:     for ( j = 0; j < col && !gvect[j]; j++ );
                   6097:        if ( j < col ) {
                   6098:                v.len = col; v.body = (pointer)gvect; gcdvgz(&v,&gcd);
                   6099:                for ( l = 0; l < col; l++ ) { divsgz(gvect[l],gcd,&t); gvect[l] = t; }
                   6100:        }
                   6101:     if ( DP_Print ) {
                   6102:         fprintf(asir_out,"-"); fflush(asir_out);
                   6103:     }
                   6104:     return maxrs;
                   6105: }
                   6106:
                   6107:
1.76      noro     6108: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.65      noro     6109: {
1.157     noro     6110:     int i,j,k,len,pos,prev;
                   6111:     UINT c,c1,c2,c3,up,lo,dmy;
                   6112:     IndArray ivect;
                   6113:     unsigned char *ivc;
                   6114:     unsigned short *ivs;
                   6115:     unsigned int *ivi;
                   6116:     NDV redv;
                   6117:     NMV mr;
                   6118:     NODE rp;
                   6119:     int maxrs;
                   6120:
                   6121:     maxrs = 0;
                   6122:     for ( i = 0; i < nred; i++ ) {
                   6123:         ivect = imat[i];
                   6124:         k = ivect->head; svect[k] %= m;
                   6125:         if ( c = svect[k] ) {
                   6126:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   6127:             c = m-c; redv = nd_ps[rp0[i]->index];
                   6128:             len = LEN(redv); mr = BDY(redv);
                   6129:             svect[k] = 0; prev = k;
                   6130:             switch ( ivect->width ) {
                   6131:                 case 1:
                   6132:                     ivc = ivect->index.c;
                   6133:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
1.215     noro     6134:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
                   6135:                                                if ( c1 ) {
                   6136:                                                        c2 = svect[pos];
                   6137:                                DMA(c1,c,c2,up,lo);
                   6138:                                if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   6139:                                } else svect[pos] = lo;
                   6140:                                                }
1.157     noro     6141:                     }
                   6142:                     break;
                   6143:                 case 2:
                   6144:                     ivs = ivect->index.s;
                   6145:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
1.215     noro     6146:                         pos = prev+ivs[j]; c1 = CM(mr);
1.157     noro     6147:                         prev = pos;
1.215     noro     6148:                                                if ( c1 ) {
                   6149:                                                        c2 = svect[pos];
                   6150:                                DMA(c1,c,c2,up,lo);
                   6151:                                if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   6152:                                } else svect[pos] = lo;
                   6153:                                                }
1.157     noro     6154:                     }
                   6155:                     break;
                   6156:                 case 4:
                   6157:                     ivi = ivect->index.i;
                   6158:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
1.215     noro     6159:                         pos = prev+ivi[j]; c1 = CM(mr);
1.157     noro     6160:                         prev = pos;
1.215     noro     6161:                                                if ( c1 ) {
                   6162:                                                        c2 = svect[pos];
                   6163:                                DMA(c1,c,c2,up,lo);
                   6164:                                if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   6165:                                } else svect[pos] = lo;
                   6166:                                                }
1.157     noro     6167:                     }
                   6168:                     break;
                   6169:             }
                   6170:         }
                   6171:     }
                   6172:     for ( i = 0; i < col; i++ )
                   6173:         if ( svect[i] >= (UINT)m ) svect[i] %= m;
                   6174:     return maxrs;
1.65      noro     6175: }
                   6176:
1.76      noro     6177: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.72      noro     6178: {
1.157     noro     6179:     int i,j,k,len,pos,prev;
                   6180:     UINT c,c1,c2,c3,up,lo,dmy;
                   6181:     IndArray ivect;
                   6182:     unsigned char *ivc;
                   6183:     unsigned short *ivs;
                   6184:     unsigned int *ivi;
                   6185:     NDV redv;
                   6186:     NMV mr;
                   6187:     NODE rp;
                   6188:     int maxrs;
                   6189:
                   6190:     maxrs = 0;
                   6191:     for ( i = 0; i < nred; i++ ) {
                   6192:         ivect = imat[i];
1.232     noro     6193:         k = ivect->head;
1.157     noro     6194:         if ( c = svect[k] ) {
                   6195:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   6196:             c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
                   6197:             len = LEN(redv); mr = BDY(redv);
                   6198:             svect[k] = 0; prev = k;
                   6199:             switch ( ivect->width ) {
                   6200:                 case 1:
                   6201:                     ivc = ivect->index.c;
                   6202:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6203:                         pos = prev+ivc[j]; prev = pos;
                   6204:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   6205:                     }
                   6206:                     break;
                   6207:                 case 2:
                   6208:                     ivs = ivect->index.s;
                   6209:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6210:                         pos = prev+ivs[j]; prev = pos;
                   6211:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   6212:                     }
                   6213:                     break;
                   6214:                 case 4:
                   6215:                     ivi = ivect->index.i;
                   6216:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6217:                         pos = prev+ivi[j]; prev = pos;
                   6218:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   6219:                     }
                   6220:                     break;
                   6221:             }
                   6222:         }
                   6223:     }
                   6224:     return maxrs;
1.72      noro     6225: }
                   6226:
1.232     noro     6227: ND nd_add_lf(ND p1,ND p2)
                   6228: {
                   6229:     int n,c,can;
                   6230:     ND r;
                   6231:     NM m1,m2,mr0,mr,s;
                   6232:     GZ t;
                   6233:
                   6234:     if ( !p1 ) return p2;
                   6235:     else if ( !p2 ) return p1;
                   6236:     else {
                   6237:         can = 0;
                   6238:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   6239:             c = DL_COMPARE(DL(m1),DL(m2));
                   6240:             switch ( c ) {
                   6241:                 case 0:
                   6242:                     addlf(CZ(m1),CZ(m2),&t);
                   6243:                     s = m1; m1 = NEXT(m1);
                   6244:                     if ( t ) {
                   6245:                         can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
                   6246:                     } else {
                   6247:                         can += 2; FREENM(s);
                   6248:                     }
                   6249:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   6250:                     break;
                   6251:                 case 1:
                   6252:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   6253:                     break;
                   6254:                 case -1:
                   6255:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   6256:                     break;
                   6257:             }
                   6258:         }
                   6259:         if ( !mr0 )
                   6260:             if ( m1 ) mr0 = m1;
                   6261:             else if ( m2 ) mr0 = m2;
                   6262:             else return 0;
                   6263:         else if ( m1 ) NEXT(mr) = m1;
                   6264:         else if ( m2 ) NEXT(mr) = m2;
                   6265:         else NEXT(mr) = 0;
                   6266:         BDY(p1) = mr0;
                   6267:         SG(p1) = MAX(SG(p1),SG(p2));
                   6268:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   6269:         FREEND(p2);
                   6270:         return p1;
                   6271:     }
                   6272: }
                   6273:
1.234   ! noro     6274: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.232     noro     6275: {
                   6276:     int i,j,k,len,pos,prev;
                   6277:     mpz_t c,mc,c1;
                   6278:     IndArray ivect;
                   6279:     unsigned char *ivc;
                   6280:     unsigned short *ivs;
                   6281:     unsigned int *ivi;
                   6282:     NDV redv;
                   6283:     NMV mr;
                   6284:     NODE rp;
                   6285:     int maxrs;
                   6286:
                   6287:     maxrs = 0;
                   6288:     lf_lazy = 1;
                   6289:     for ( i = 0; i < nred; i++ ) {
                   6290:         ivect = imat[i];
                   6291:         k = ivect->head;
                   6292:         mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
                   6293:         if ( mpz_sgn(svect[k]) ) {
                   6294:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   6295:             mpz_neg(svect[k],svect[k]);
1.234   ! noro     6296:             redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
1.232     noro     6297:             len = LEN(redv); mr = BDY(redv);
                   6298:             prev = k;
                   6299:             switch ( ivect->width ) {
                   6300:                 case 1:
                   6301:                     ivc = ivect->index.c;
                   6302:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6303:                         pos = prev+ivc[j]; prev = pos;
                   6304:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   6305:                     }
                   6306:                     break;
                   6307:                 case 2:
                   6308:                     ivs = ivect->index.s;
                   6309:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6310:                         pos = prev+ivs[j]; prev = pos;
                   6311:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   6312:                     }
                   6313:                     break;
                   6314:                 case 4:
                   6315:                     ivi = ivect->index.i;
                   6316:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6317:                         pos = prev+ivi[j]; prev = pos;
                   6318:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   6319:                     }
                   6320:                     break;
                   6321:             }
                   6322:             mpz_set_ui(svect[k],0);
                   6323:         }
                   6324:     }
                   6325:     lf_lazy=0;
                   6326:     for ( i = 0; i < col; i++ ) {
                   6327:         mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
                   6328:     }
                   6329:     return maxrs;
                   6330: }
                   6331:
                   6332: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
                   6333: {
                   6334:     int i,j,k,l,rank,s;
                   6335:     mpz_t a,a1,inv;
                   6336:     mpz_t *t,*pivot,*pk;
                   6337:     mpz_t **mat;
                   6338:     struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
1.234   ! noro     6339:     int size,size1;
1.232     noro     6340:
                   6341:     mpz_init(inv);
                   6342:     mpz_init(a);
                   6343:     mat = (mpz_t **)mat0;
1.234   ! noro     6344:         size = 0;
1.232     noro     6345:     for ( rank = 0, j = 0; j < col; j++ ) {
                   6346:         for ( i = rank; i < row; i++ ) {
                   6347:             mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
                   6348:         }
                   6349:         for ( i = rank; i < row; i++ )
                   6350:             if ( mpz_sgn(mat[i][j]) )
                   6351:                 break;
                   6352:         if ( i == row ) {
                   6353:             colstat[j] = 0;
                   6354:             continue;
                   6355:         } else
                   6356:             colstat[j] = 1;
                   6357:         if ( i != rank ) {
                   6358:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   6359:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   6360:         }
                   6361:         pivot = mat[rank];
                   6362:         s = sugar[rank];
                   6363:         mpz_invert(inv,pivot[j],BDY(current_mod_lf));
                   6364:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   6365:             if ( mpz_sgn(*pk) ) {
                   6366:                 mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
                   6367:             }
                   6368:         for ( i = rank+1; i < row; i++ ) {
                   6369:             t = mat[i];
                   6370:             if ( mpz_sgn(t[j]) ) {
                   6371:                 sugar[i] = MAX(sugar[i],s);
                   6372:                 mpz_neg(a,t[j]);
                   6373:                 red_by_vect_lf(t+j,pivot+j,a,col-j);
                   6374:             }
                   6375:         }
                   6376:         rank++;
                   6377:     }
                   6378:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   6379:         if ( colstat[j] ) {
                   6380:             pivot = mat[l];
                   6381:             s = sugar[l];
                   6382:             for ( k = j; k < col; k++ )
                   6383:               mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
                   6384:             for ( i = 0; i < l; i++ ) {
                   6385:                 t = mat[i];
                   6386:                 if ( mpz_sgn(t[j]) ) {
                   6387:                     sugar[i] = MAX(sugar[i],s);
                   6388:                     mpz_neg(a,t[j]);
                   6389:                     red_by_vect_lf(t+j,pivot+j,a,col-j);
                   6390:                 }
                   6391:             }
                   6392:             l--;
                   6393:         }
                   6394:     for ( j = 0, l = 0; l < rank; j++ )
                   6395:         if ( colstat[j] ) {
                   6396:             t = mat[l];
                   6397:             for ( k = j; k < col; k++ ) {
                   6398:                 mpz_mod(t[k],t[k],BDY(current_mod_lf));
                   6399:             }
                   6400:             l++;
                   6401:         }
                   6402:     return rank;
                   6403: }
                   6404:
                   6405:
1.65      noro     6406: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   6407: {
1.157     noro     6408:     int j,k,len;
                   6409:     UINT *p;
                   6410:     UINT c;
                   6411:     NDV r;
                   6412:     NMV mr0,mr;
                   6413:
                   6414:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   6415:     if ( !len ) return 0;
                   6416:     else {
1.200     noro     6417:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
1.103     noro     6418: #if 0
1.157     noro     6419:         ndv_alloc += nmv_adv*len;
1.103     noro     6420: #endif
1.157     noro     6421:         mr = mr0;
                   6422:         p = s0vect;
                   6423:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   6424:             if ( !rhead[j] ) {
                   6425:                 if ( c = vect[k++] ) {
                   6426:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   6427:                 }
                   6428:             }
                   6429:         MKNDV(nd_nvar,mr0,len,r);
                   6430:         return r;
                   6431:     }
1.65      noro     6432: }
                   6433:
1.220     noro     6434: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
                   6435: {
                   6436:     int j,k,len;
                   6437:     UINT *p;
                   6438:     NDV r;
                   6439:     NMV mr0,mr;
                   6440:
                   6441:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
                   6442:     if ( !len ) return 0;
                   6443:     else {
                   6444:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   6445:         mr = mr0;
                   6446:         p = s0vect;
                   6447:         for ( j = 0; j < col; j++, p += nd_wpd )
                   6448:                  if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
                   6449:             ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
                   6450:           }
                   6451:         MKNDV(nd_nvar,mr0,len,r);
                   6452:         return r;
                   6453:     }
                   6454: }
                   6455:
1.129     noro     6456: /* for preprocessed vector */
                   6457:
1.113     noro     6458: NDV vect_to_ndv_q(Q *vect,int spcol,int col,int *rhead,UINT *s0vect)
1.107     noro     6459: {
1.157     noro     6460:     int j,k,len;
                   6461:     UINT *p;
                   6462:     Q c;
                   6463:     NDV r;
                   6464:     NMV mr0,mr;
                   6465:
                   6466:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   6467:     if ( !len ) return 0;
                   6468:     else {
1.200     noro     6469:         mr0 = (NMV)MALLOC(nmv_adv*len);
1.107     noro     6470: #if 0
1.157     noro     6471:         ndv_alloc += nmv_adv*len;
1.107     noro     6472: #endif
1.157     noro     6473:         mr = mr0;
                   6474:         p = s0vect;
                   6475:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   6476:             if ( !rhead[j] ) {
                   6477:                 if ( c = vect[k++] ) {
                   6478:                     if ( DN(c) )
                   6479:                         error("afo");
                   6480:                     ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
                   6481:                 }
                   6482:             }
                   6483:         MKNDV(nd_nvar,mr0,len,r);
                   6484:         return r;
                   6485:     }
1.107     noro     6486: }
                   6487:
1.215     noro     6488: NDV vect_to_ndv_gz(GZ *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   6489: {
                   6490:     int j,k,len;
                   6491:     UINT *p;
1.232     noro     6492:     GZ c;
1.215     noro     6493:     NDV r;
                   6494:     NMV mr0,mr;
                   6495:
                   6496:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   6497:     if ( !len ) return 0;
                   6498:     else {
                   6499:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   6500: #if 0
                   6501:         ndv_alloc += nmv_adv*len;
                   6502: #endif
                   6503:         mr = mr0;
                   6504:         p = s0vect;
                   6505:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   6506:             if ( !rhead[j] ) {
                   6507:                 if ( c = vect[k++] ) {
                   6508:                     ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
                   6509:                 }
                   6510:             }
                   6511:         MKNDV(nd_nvar,mr0,len,r);
                   6512:         return r;
                   6513:     }
                   6514: }
                   6515:
1.232     noro     6516: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   6517: {
                   6518:     int j,k,len;
                   6519:     UINT *p;
                   6520:     mpz_t c;
                   6521:     NDV r;
                   6522:     NMV mr0,mr;
                   6523:
                   6524:     for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
                   6525:     if ( !len ) return 0;
                   6526:     else {
                   6527:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   6528: #if 0
                   6529:         ndv_alloc += nmv_adv*len;
                   6530: #endif
                   6531:         mr = mr0;
                   6532:         p = s0vect;
                   6533:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   6534:             if ( !rhead[j] ) {
                   6535:                 c[0] = vect[k++][0];
                   6536:                 if ( mpz_sgn(c) ) {
                   6537:                     ndl_copy(p,DL(mr)); MPZTOGZ(c,CZ(mr)); NMV_ADV(mr);
                   6538:                 }
                   6539:             }
                   6540:         MKNDV(nd_nvar,mr0,len,r);
                   6541:         return r;
                   6542:     }
                   6543: }
                   6544:
1.129     noro     6545: /* for plain vector */
                   6546:
                   6547: NDV plain_vect_to_ndv_q(Q *vect,int col,UINT *s0vect)
                   6548: {
1.157     noro     6549:     int j,k,len;
                   6550:     UINT *p;
                   6551:     Q c;
                   6552:     NDV r;
                   6553:     NMV mr0,mr;
                   6554:
                   6555:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   6556:     if ( !len ) return 0;
                   6557:     else {
1.200     noro     6558:         mr0 = (NMV)MALLOC(nmv_adv*len);
1.129     noro     6559: #if 0
1.157     noro     6560:         ndv_alloc += nmv_adv*len;
1.129     noro     6561: #endif
1.157     noro     6562:         mr = mr0;
                   6563:         p = s0vect;
                   6564:         for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   6565:             if ( c = vect[k] ) {
                   6566:                 if ( DN(c) )
                   6567:                     error("afo");
                   6568:                 ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
                   6569:             }
                   6570:         MKNDV(nd_nvar,mr0,len,r);
                   6571:         return r;
                   6572:     }
1.129     noro     6573: }
                   6574:
1.133     noro     6575: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
1.65      noro     6576: {
1.157     noro     6577:     ND_pairs t;
                   6578:     NODE sp0,sp;
                   6579:     int stat;
                   6580:     ND spol;
                   6581:
                   6582:     for ( t = l; t; t = NEXT(t) ) {
                   6583:         stat = nd_sp(m,trace,t,&spol);
                   6584:         if ( !stat ) return 0;
                   6585:         if ( spol ) {
                   6586:             add_pbucket_symbolic(bucket,spol);
                   6587:         }
                   6588:     }
                   6589:     return 1;
1.65      noro     6590: }
                   6591:
1.133     noro     6592: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
1.65      noro     6593: {
1.157     noro     6594:     NODE rp0,rp;
                   6595:     NM mul,head,s0,s;
                   6596:     int index,col,i,sugar;
                   6597:     RHist h;
                   6598:     UINT *s0v,*p;
                   6599:     NM_ind_pair pair;
                   6600:     ND red;
                   6601:     NDV *ps;
                   6602:
                   6603:     s0 = 0; rp0 = 0; col = 0;
1.215     noro     6604:        if ( nd_demand )
                   6605:        ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   6606:        else
                   6607:        ps = trace?nd_ps_trace:nd_ps;
1.157     noro     6608:     while ( 1 ) {
                   6609:         head = remove_head_pbucket_symbolic(bucket);
                   6610:         if ( !head ) break;
                   6611:         if ( !s0 ) s0 = head;
                   6612:         else NEXT(s) = head;
                   6613:         s = head;
                   6614:         index = ndl_find_reducer(DL(head));
                   6615:         if ( index >= 0 ) {
                   6616:             h = nd_psh[index];
                   6617:             NEWNM(mul);
                   6618:             ndl_sub(DL(head),DL(h),DL(mul));
                   6619:             if ( ndl_check_bound2(index,DL(mul)) ) return 0;
                   6620:             sugar = TD(DL(mul))+SG(ps[index]);
                   6621:             MKNM_ind_pair(pair,mul,index,sugar);
                   6622:             red = ndv_mul_nm_symbolic(mul,ps[index]);
                   6623:             add_pbucket_symbolic(bucket,nd_remove_head(red));
                   6624:             NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   6625:         }
                   6626:         col++;
                   6627:     }
                   6628:     if ( rp0 ) NEXT(rp) = 0;
                   6629:     NEXT(s) = 0;
                   6630:     s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   6631:     for ( i = 0, p = s0v, s = s0; i < col;
                   6632:         i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   6633:     *s0vect = s0v;
                   6634:     *r = rp0;
                   6635:     return col;
1.65      noro     6636: }
                   6637:
1.234   ! noro     6638: NODE nd_f4(int m,int checkonly,int **indp)
1.69      noro     6639: {
1.234   ! noro     6640:     int i,nh,stat,index,f4red;
1.208     noro     6641:     NODE r,g,tn0,tn,node;
                   6642:     ND_pairs d,l,t,ll0,ll;
                   6643:        LIST l0,l1;
1.157     noro     6644:     ND spol,red;
                   6645:     NDV nf,redv;
                   6646:     NM s0,s;
1.231     noro     6647:     NODE rp0,srp0,nflist,nzlist,nzlist_t;
1.208     noro     6648:     int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
1.157     noro     6649:     UINT c;
                   6650:     UINT **spmat;
                   6651:     UINT *s0vect,*svect,*p,*v;
                   6652:     int *colstat;
                   6653:     IndArray *imat;
                   6654:     int *rhead;
                   6655:     int spcol,sprow;
                   6656:     int sugar;
                   6657:     PGeoBucket bucket;
                   6658:     struct oEGT eg0,eg1,eg_f4;
1.208     noro     6659:     Q i1,i2,sugarq;
1.103     noro     6660: #if 0
1.157     noro     6661:     ndv_alloc = 0;
1.103     noro     6662: #endif
1.157     noro     6663:     g = 0; d = 0;
                   6664:     for ( i = 0; i < nd_psn; i++ ) {
1.231     noro     6665:         if ( !nd_nzlist ) d = update_pairs(d,g,i,0);
1.157     noro     6666:         g = update_base(g,i);
                   6667:     }
1.208     noro     6668:        nzlist = 0;
1.231     noro     6669:     nzlist_t = nd_nzlist;
1.234   ! noro     6670:     f4red = 0;
1.231     noro     6671:     while ( d || nzlist_t ) {
1.157     noro     6672:         get_eg(&eg0);
1.208     noro     6673:         if ( nd_nzlist ) {
1.231     noro     6674:             node = BDY((LIST)BDY(nzlist_t));
1.232     noro     6675:             sugar = (int)ARG0(node);
1.231     noro     6676:             tn = BDY((LIST)ARG1(node));
                   6677:             if ( !tn ) {
                   6678:               nzlist_t = NEXT(nzlist_t);
                   6679:               continue;
                   6680:             }
                   6681:             /* tn = [[i1,i2],...] */
                   6682:             l = nd_ipairtospair(tn);
                   6683:         } else {
                   6684:             l = nd_minsugarp(d,&d);
                   6685:             sugar = nd_sugarweight?l->sugar2:SG(l);
                   6686:             if ( MaxDeg > 0 && sugar > MaxDeg ) break;
1.208     noro     6687:         }
1.157     noro     6688:         bucket = create_pbucket();
                   6689:         stat = nd_sp_f4(m,0,l,bucket);
                   6690:         if ( !stat ) {
1.231     noro     6691:             if ( !nd_nzlist ) {
                   6692:                 for ( t = l; NEXT(t); t = NEXT(t) );
                   6693:                 NEXT(t) = d; d = l;
                   6694:                 d = nd_reconstruct(0,d);
                   6695:             }
1.157     noro     6696:             continue;
                   6697:         }
                   6698:         if ( bucket->m < 0 ) continue;
                   6699:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   6700:         if ( !col ) {
                   6701:             for ( t = l; NEXT(t); t = NEXT(t) );
                   6702:             NEXT(t) = d; d = l;
                   6703:             d = nd_reconstruct(0,d);
                   6704:             continue;
                   6705:         }
                   6706:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   6707:         if ( DP_Print )
                   6708:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   6709:                 sugar,eg_f4.exectime+eg_f4.gctime);
1.210     noro     6710:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
1.234   ! noro     6711:         if ( checkonly && nflist ) return 0;
1.157     noro     6712:         /* adding new bases */
                   6713:         for ( r = nflist; r; r = NEXT(r) ) {
                   6714:             nf = (NDV)BDY(r);
                   6715:             ndv_removecont(m,nf);
                   6716:             if ( !m && nd_nalg ) {
                   6717:                 ND nf1;
                   6718:
                   6719:                 nf1 = ndvtond(m,nf);
                   6720:                 nd_monic(0,&nf1);
                   6721:                 nd_removecont(m,nf1);
                   6722:                 nf = ndtondv(m,nf1);
                   6723:             }
1.215     noro     6724:             nh = ndv_newps(m,nf,0,1);
1.231     noro     6725:             if ( !nd_nzlist ) d = update_pairs(d,g,nh,0);
1.157     noro     6726:             g = update_base(g,nh);
                   6727:         }
1.234   ! noro     6728:         if ( DP_Print ) {
        !          6729:           fprintf(asir_out,"f4red=%d,gblen=%d,",f4red,length(g)); fflush(asir_out);
        !          6730:         }
1.208     noro     6731:         if ( nd_gentrace ) {
                   6732:                        for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
                   6733:                                NEXTNODE(tn0,tn);
                   6734:                 STOQ(t->i1,i1); STOQ(t->i2,i2);
                   6735:                 node = mknode(2,i1,i2); MKLIST(l0,node);
                   6736:                                BDY(tn) = l0;
                   6737:                        }
                   6738:                        if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
                   6739:             STOQ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
                   6740:             MKNODE(node,l1,nzlist); nzlist = node;
                   6741:         }
1.231     noro     6742:         if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
1.234   ! noro     6743:         f4red++;
        !          6744:         if ( nd_f4red && f4red >= nd_f4red ) break;
        !          6745:         if ( nd_rank0 && !nflist ) break;
1.208     noro     6746:     }
                   6747:     if ( nd_gentrace ) {
                   6748:                MKLIST(l0,reverse_node(nzlist));
                   6749:         MKNODE(nd_alltracelist,l0,0);
1.157     noro     6750:     }
1.103     noro     6751: #if 0
1.157     noro     6752:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     6753: #endif
1.215     noro     6754:        conv_ilist(nd_demand,0,g,indp);
1.157     noro     6755:     return g;
1.69      noro     6756: }
1.74      noro     6757:
1.167     noro     6758: NODE nd_f4_trace(int m,int **indp)
1.133     noro     6759: {
1.157     noro     6760:     int i,nh,stat,index;
                   6761:     NODE r,g;
                   6762:     ND_pairs d,l,l0,t;
                   6763:     ND spol,red;
                   6764:     NDV nf,redv,nfqv,nfv;
                   6765:     NM s0,s;
                   6766:     NODE rp0,srp0,nflist;
                   6767:     int nsp,nred,col,rank,len,k,j,a;
                   6768:     UINT c;
                   6769:     UINT **spmat;
                   6770:     UINT *s0vect,*svect,*p,*v;
                   6771:     int *colstat;
                   6772:     IndArray *imat;
                   6773:     int *rhead;
                   6774:     int spcol,sprow;
                   6775:     int sugar;
                   6776:     PGeoBucket bucket;
                   6777:     struct oEGT eg0,eg1,eg_f4;
                   6778:
                   6779:     g = 0; d = 0;
                   6780:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     6781:         d = update_pairs(d,g,i,0);
1.157     noro     6782:         g = update_base(g,i);
                   6783:     }
                   6784:     while ( d ) {
                   6785:         get_eg(&eg0);
                   6786:         l = nd_minsugarp(d,&d);
                   6787:         sugar = SG(l);
1.228     noro     6788:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
1.157     noro     6789:         bucket = create_pbucket();
                   6790:         stat = nd_sp_f4(m,0,l,bucket);
                   6791:         if ( !stat ) {
                   6792:             for ( t = l; NEXT(t); t = NEXT(t) );
                   6793:             NEXT(t) = d; d = l;
                   6794:             d = nd_reconstruct(1,d);
                   6795:             continue;
                   6796:         }
                   6797:         if ( bucket->m < 0 ) continue;
                   6798:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   6799:         if ( !col ) {
                   6800:             for ( t = l; NEXT(t); t = NEXT(t) );
                   6801:             NEXT(t) = d; d = l;
                   6802:             d = nd_reconstruct(1,d);
                   6803:             continue;
                   6804:         }
                   6805:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   6806:         if ( DP_Print )
                   6807:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   6808:                 sugar,eg_f4.exectime+eg_f4.gctime);
                   6809:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   6810:         if ( !l0 ) continue;
                   6811:         l = l0;
                   6812:
                   6813:         /* over Q */
                   6814:         bucket = create_pbucket();
                   6815:         stat = nd_sp_f4(0,1,l,bucket);
                   6816:         if ( !stat ) {
                   6817:             for ( t = l; NEXT(t); t = NEXT(t) );
                   6818:             NEXT(t) = d; d = l;
                   6819:             d = nd_reconstruct(1,d);
                   6820:             continue;
                   6821:         }
                   6822:         if ( bucket->m < 0 ) continue;
                   6823:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   6824:         if ( !col ) {
                   6825:             for ( t = l; NEXT(t); t = NEXT(t) );
                   6826:             NEXT(t) = d; d = l;
                   6827:             d = nd_reconstruct(1,d);
                   6828:             continue;
                   6829:         }
                   6830:         nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   6831:         /* adding new bases */
                   6832:         for ( r = nflist; r; r = NEXT(r) ) {
                   6833:             nfqv = (NDV)BDY(r);
                   6834:             ndv_removecont(0,nfqv);
                   6835:             if ( !rem(NM(HCQ(nfqv)),m) ) return 0;
                   6836:             if ( nd_nalg ) {
                   6837:                 ND nf1;
                   6838:
                   6839:                 nf1 = ndvtond(m,nfqv);
                   6840:                 nd_monic(0,&nf1);
                   6841:                 nd_removecont(0,nf1);
                   6842:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   6843:             }
                   6844:             nfv = ndv_dup(0,nfqv);
                   6845:             ndv_mod(m,nfv);
                   6846:             ndv_removecont(m,nfv);
1.215     noro     6847:             nh = ndv_newps(0,nfv,nfqv,1);
1.168     noro     6848:             d = update_pairs(d,g,nh,0);
1.157     noro     6849:             g = update_base(g,nh);
                   6850:         }
                   6851:     }
1.133     noro     6852: #if 0
1.157     noro     6853:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.133     noro     6854: #endif
1.215     noro     6855:        conv_ilist(nd_demand,1,g,indp);
1.157     noro     6856:     return g;
1.133     noro     6857: }
                   6858:
1.176     noro     6859: NODE nd_f4_pseudo_trace(int m,int **indp)
                   6860: {
                   6861:     int i,nh,stat,index;
                   6862:     NODE r,g;
                   6863:     ND_pairs d,l,l0,t;
                   6864:     ND spol,red;
                   6865:     NDV nf,redv,nfqv,nfv;
                   6866:     NM s0,s;
                   6867:     NODE rp0,srp0,nflist;
                   6868:     int nsp,nred,col,rank,len,k,j,a;
                   6869:     UINT c;
                   6870:     UINT **spmat;
                   6871:     UINT *s0vect,*svect,*p,*v;
                   6872:     int *colstat;
                   6873:     IndArray *imat;
                   6874:     int *rhead;
                   6875:     int spcol,sprow;
                   6876:     int sugar;
                   6877:     PGeoBucket bucket;
                   6878:     struct oEGT eg0,eg1,eg_f4;
                   6879:
                   6880:     g = 0; d = 0;
                   6881:     for ( i = 0; i < nd_psn; i++ ) {
                   6882:         d = update_pairs(d,g,i,0);
                   6883:         g = update_base(g,i);
                   6884:     }
                   6885:     while ( d ) {
                   6886:         get_eg(&eg0);
                   6887:         l = nd_minsugarp(d,&d);
                   6888:         sugar = SG(l);
                   6889:         bucket = create_pbucket();
                   6890:         stat = nd_sp_f4(m,0,l,bucket);
                   6891:         if ( !stat ) {
                   6892:             for ( t = l; NEXT(t); t = NEXT(t) );
                   6893:             NEXT(t) = d; d = l;
                   6894:             d = nd_reconstruct(1,d);
                   6895:             continue;
                   6896:         }
                   6897:         if ( bucket->m < 0 ) continue;
                   6898:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   6899:         if ( !col ) {
                   6900:             for ( t = l; NEXT(t); t = NEXT(t) );
                   6901:             NEXT(t) = d; d = l;
                   6902:             d = nd_reconstruct(1,d);
                   6903:             continue;
                   6904:         }
                   6905:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   6906:         if ( DP_Print )
                   6907:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   6908:                 sugar,eg_f4.exectime+eg_f4.gctime);
                   6909:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   6910:         if ( !l0 ) continue;
                   6911:         l = l0;
                   6912:
                   6913:         /* over Q */
                   6914:                while ( 1 ) {
                   6915:                bucket = create_pbucket();
                   6916:                stat = nd_sp_f4(0,1,l,bucket);
                   6917:                if ( !stat ) {
                   6918:                for ( t = l; NEXT(t); t = NEXT(t) );
                   6919:                NEXT(t) = d; d = l;
                   6920:                d = nd_reconstruct(1,d);
                   6921:                continue;
                   6922:                }
                   6923:                if ( bucket->m < 0 ) continue;
                   6924:                col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   6925:                if ( !col ) {
                   6926:                for ( t = l; NEXT(t); t = NEXT(t) );
                   6927:                NEXT(t) = d; d = l;
                   6928:                d = nd_reconstruct(1,d);
                   6929:                continue;
                   6930:                }
                   6931:                nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   6932:                }
                   6933:
                   6934:         /* adding new bases */
                   6935:         for ( r = nflist; r; r = NEXT(r) ) {
                   6936:             nfqv = (NDV)BDY(r);
                   6937:             ndv_removecont(0,nfqv);
                   6938:             if ( !rem(NM(HCQ(nfqv)),m) ) return 0;
                   6939:             if ( nd_nalg ) {
                   6940:                 ND nf1;
                   6941:
                   6942:                 nf1 = ndvtond(m,nfqv);
                   6943:                 nd_monic(0,&nf1);
                   6944:                 nd_removecont(0,nf1);
                   6945:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   6946:             }
                   6947:             nfv = ndv_dup(0,nfqv);
                   6948:             ndv_mod(m,nfv);
                   6949:             ndv_removecont(m,nfv);
1.215     noro     6950:             nh = ndv_newps(0,nfv,nfqv,1);
1.176     noro     6951:             d = update_pairs(d,g,nh,0);
                   6952:             g = update_base(g,nh);
                   6953:         }
                   6954:     }
                   6955: #if 0
                   6956:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
                   6957: #endif
                   6958:        conv_ilist(0,1,g,indp);
                   6959:     return g;
                   6960: }
                   6961:
1.220     noro     6962: int rref(matrix mat,int *sugar)
                   6963: {
                   6964:   int row,col,i,j,k,l,s,wcol,wj;
                   6965:   unsigned long bj;
                   6966:   unsigned long **a;
                   6967:   unsigned long *ai,*ak,*as,*t;
                   6968:   int *pivot;
                   6969:
                   6970:   row = mat->row;
                   6971:   col = mat->col;
                   6972:   a = mat->a;
                   6973:   wcol = (col+BLEN-1)/BLEN;
                   6974:   pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   6975:   i = 0;
                   6976:   for ( j = 0; j < col; j++ ) {
                   6977:        wj = j/BLEN; bj = 1L<<(j%BLEN);
                   6978:     for ( k = i; k < row; k++ )
                   6979:          if ( a[k][wj] & bj ) break;
                   6980:     if ( k == row ) continue;
                   6981:        pivot[i] = j;
                   6982:     if ( k != i ) {
                   6983:         t = a[i]; a[i] = a[k]; a[k] = t;
                   6984:         s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
                   6985:        }
                   6986:        ai = a[i];
                   6987:     for ( k = i+1; k < row; k++ ) {
                   6988:          ak = a[k];
                   6989:          if ( ak[wj] & bj ) {
                   6990:            for ( l = wj; l < wcol; l++ )
                   6991:                  ak[l] ^= ai[l];
                   6992:            sugar[k] = MAX(sugar[k],sugar[i]);
                   6993:          }
                   6994:        }
                   6995:        i++;
                   6996:   }
                   6997:   for ( k = i-1; k >= 0; k-- ) {
                   6998:     j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
                   6999:        ak = a[k];
                   7000:     for ( s = 0; s < k; s++ ) {
                   7001:          as = a[s];
                   7002:       if ( as[wj] & bj ) {
                   7003:         for ( l = wj; l < wcol; l++ )
                   7004:                  as[l] ^= ak[l];
                   7005:            sugar[s] = MAX(sugar[s],sugar[k]);
                   7006:          }
                   7007:        }
                   7008:   }
                   7009:   return i;
                   7010: }
                   7011:
                   7012: void print_matrix(matrix mat)
                   7013: {
                   7014:   int row,col,i,j;
                   7015:   unsigned long *ai;
                   7016:
                   7017:   row = mat->row;
                   7018:   col = mat->col;
                   7019:   printf("%d x %d\n",row,col);
                   7020:   for ( i = 0; i < row; i++ ) {
                   7021:        ai = mat->a[i];
                   7022:     for ( j = 0; j < col; j++ ) {
                   7023:          if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
                   7024:          else putchar('0');
                   7025:        }
                   7026:        putchar('\n');
                   7027:   }
                   7028: }
                   7029:
                   7030: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
                   7031:
                   7032: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
                   7033: {
                   7034:   int row,col,wcol,wj,i,j;
                   7035:   unsigned long bj;
                   7036:   unsigned long *ai;
                   7037:   unsigned long **a;
                   7038:   int len;
                   7039:   int *pos;
                   7040:
                   7041:   row = mat->row;
                   7042:   col = mat->col;
                   7043:   wcol = (col+BLEN-1)/BLEN;
                   7044:   pos = (int *)ALLOCA(wcol*sizeof(int));
                   7045:   bzero(pos,wcol*sizeof(int));
                   7046:   for ( i = j = 0; i < wcol; i++ )
                   7047:     if ( v[i] ) pos[j++] = i;;
                   7048:   len = j;
                   7049:   wj = rhead/BLEN;
                   7050:   bj = 1L<<rhead%BLEN;
                   7051:   a = mat->a;
                   7052:   for ( i = 0; i < row; i++ ) {
                   7053:        ai = a[i];
                   7054:     if ( ai[wj]&bj ) {
                   7055:          for ( j = 0; j < len; j++ )
                   7056:            ai[pos[j]] ^= v[pos[j]];
                   7057:          sugar[i] = MAX(sugar[i],rsugar);
                   7058:        }
                   7059:   }
                   7060: }
                   7061:
                   7062: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   7063: {
                   7064:     int nsp,nred,i,i0,k,rank,row;
                   7065:     NODE r0,rp;
                   7066:     ND_pairs sp;
                   7067:        ND spol;
                   7068:        NM_ind_pair rt;
                   7069:     int *s0hash;
                   7070:        UINT *s;
                   7071:        int *pivot,*sugar,*head;
                   7072:        matrix mat;
                   7073:     NM m;
                   7074:     NODE r;
                   7075:        struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
                   7076:        int rhead,rsugar,size;
                   7077:     unsigned long *v;
                   7078:
                   7079:     get_eg(&eg0);
                   7080: init_eg(&eg_search);
                   7081:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   7082:     nred = length(rp0);
                   7083:     mat = alloc_matrix(nsp,col);
                   7084:     s0hash = (int *)ALLOCA(col*sizeof(int));
                   7085:     for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
                   7086:         s0hash[i] = ndl_hash_value(s);
                   7087:
                   7088:        sugar = (int *)ALLOCA(nsp*sizeof(int));
                   7089:        for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
                   7090:                nd_sp(2,0,sp,&spol);
                   7091:                if ( spol ) {
                   7092:              mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
                   7093:                  sugar[i] = SG(spol);
                   7094:                  i++;
                   7095:                }
                   7096:        }
                   7097:        mat->row = i;
1.227     noro     7098:     if ( DP_Print ) {
                   7099:       fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
                   7100:     }
1.220     noro     7101:        size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
                   7102:        v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
                   7103:     for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
                   7104:                rt = (NM_ind_pair)BDY(rp);
                   7105:                bzero(v,size);
                   7106:         rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
                   7107:                rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
                   7108:            red_by_vect_2(mat,sugar,v,rhead,rsugar);
                   7109:        }
                   7110:
                   7111:     get_eg(&eg1);
                   7112:     init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
                   7113:        rank = rref(mat,sugar);
                   7114:
                   7115:     for ( i = 0, r0 = 0; i < rank; i++ ) {
                   7116:       NEXTNODE(r0,r);
                   7117:          BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
                   7118:       SG((NDV)BDY(r)) = sugar[i];
                   7119:     }
                   7120:     if ( r0 ) NEXT(r) = 0;
                   7121:     get_eg(&eg2);
                   7122:     init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
                   7123:     if ( DP_Print ) {
                   7124:         fprintf(asir_out,"elim1=%fsec,elim2=%fsec\n",
                   7125:                  eg_elim1.exectime+eg_elim1.gctime,eg_elim2.exectime+eg_elim2.gctime);
                   7126:         fflush(asir_out);
                   7127:        }
                   7128:     return r0;
                   7129: }
                   7130:
                   7131:
1.133     noro     7132: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
1.63      noro     7133: {
1.157     noro     7134:     IndArray *imat;
                   7135:     int nsp,nred,i;
                   7136:     int *rhead;
                   7137:     NODE r0,rp;
                   7138:     ND_pairs sp;
                   7139:     NM_ind_pair *rvect;
1.210     noro     7140:     UINT *s;
                   7141:     int *s0hash;
                   7142:
1.220     noro     7143:     if ( m == 2 && nd_rref2 )
                   7144:           return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
                   7145:
1.198     noro     7146: init_eg(&eg_search);
1.157     noro     7147:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   7148:     nred = length(rp0);
                   7149:     imat = (IndArray *)ALLOCA(nred*sizeof(IndArray));
                   7150:     rhead = (int *)ALLOCA(col*sizeof(int));
                   7151:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   7152:
                   7153:     /* construction of index arrays */
1.227     noro     7154:     if ( DP_Print ) {
                   7155:          fprintf(stderr,"%dx%d,",nsp+nred,col);
                   7156:     }
1.157     noro     7157:     rvect = (NM_ind_pair *)ALLOCA(nred*sizeof(NM_ind_pair));
1.210     noro     7158:     s0hash = (int *)ALLOCA(col*sizeof(int));
                   7159:     for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
                   7160:         s0hash[i] = ndl_hash_value(s);
1.157     noro     7161:     for ( rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   7162:         rvect[i] = (NM_ind_pair)BDY(rp);
1.210     noro     7163:         imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,s0hash,rvect[i]);
1.157     noro     7164:         rhead[imat[i]->head] = 1;
                   7165:     }
1.232     noro     7166:     if ( m > 0 || m == -1 )
1.157     noro     7167:         r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
1.232     noro     7168:     else if ( m == -2 )
1.234   ! noro     7169:         r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
1.157     noro     7170:     else
1.215     noro     7171:         r0 = nd_f4_red_gz_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
1.227     noro     7172:     if ( DP_Print ) print_eg("search",&eg_search);
1.157     noro     7173:     return r0;
1.106     noro     7174: }
1.74      noro     7175:
1.232     noro     7176: /* for small finite fields  */
1.106     noro     7177: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
1.133     noro     7178:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
1.106     noro     7179: {
1.157     noro     7180:     int spcol,sprow,a;
                   7181:     int i,j,k,l,rank;
                   7182:     NODE r0,r;
                   7183:     ND_pairs sp;
                   7184:     ND spol;
                   7185:     int **spmat;
                   7186:     UINT *svect,*v;
                   7187:     int *colstat;
                   7188:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   7189:     int maxrs;
                   7190:     int *spsugar;
                   7191:     ND_pairs *spactive;
                   7192:
                   7193:     spcol = col-nred;
                   7194:     get_eg(&eg0);
                   7195:     /* elimination (1st step) */
                   7196:     spmat = (int **)ALLOCA(nsp*sizeof(UINT *));
                   7197:     svect = (UINT *)ALLOCA(col*sizeof(UINT));
1.232     noro     7198:     spsugar = (int *)ALLOCA(nsp*sizeof(int));
1.157     noro     7199:     spactive = !nz?0:(ND_pairs *)ALLOCA(nsp*sizeof(ND_pairs));
                   7200:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   7201:         nd_sp(m,0,sp,&spol);
                   7202:         if ( !spol ) continue;
                   7203:         nd_to_vect(m,s0vect,col,spol,svect);
                   7204:         if ( m == -1 )
                   7205:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   7206:         else
                   7207:             maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
                   7208:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   7209:         if ( i < col ) {
                   7210:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   7211:             for ( j = k = 0; j < col; j++ )
                   7212:                 if ( !rhead[j] ) v[k++] = svect[j];
                   7213:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   7214:             if ( nz )
                   7215:             spactive[sprow] = sp;
                   7216:             sprow++;
                   7217:         }
                   7218:         nd_free(spol);
                   7219:     }
                   7220:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   7221:     if ( DP_Print ) {
                   7222:         fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   7223:         fflush(asir_out);
                   7224:     }
                   7225:     /* free index arrays */
1.200     noro     7226:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
1.157     noro     7227:
                   7228:     /* elimination (2nd step) */
                   7229:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   7230:     if ( m == -1 )
                   7231:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   7232:     else
                   7233:         rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   7234:     r0 = 0;
                   7235:     for ( i = 0; i < rank; i++ ) {
                   7236:         NEXTNODE(r0,r); BDY(r) =
                   7237:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   7238:         SG((NDV)BDY(r)) = spsugar[i];
1.200     noro     7239:         GCFREE(spmat[i]);
1.157     noro     7240:     }
                   7241:     if ( r0 ) NEXT(r) = 0;
                   7242:
1.200     noro     7243:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.157     noro     7244:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   7245:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   7246:     if ( DP_Print ) {
                   7247:         fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   7248:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   7249:             nsp,nred,sprow,spcol,rank);
                   7250:         fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   7251:     }
                   7252:     if ( nz ) {
                   7253:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   7254:         if ( rank > 0 ) {
                   7255:             NEXT(spactive[rank-1]) = 0;
                   7256:             *nz = spactive[0];
                   7257:         } else
                   7258:             *nz = 0;
                   7259:     }
                   7260:     return r0;
1.74      noro     7261: }
                   7262:
1.234   ! noro     7263: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
1.232     noro     7264:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   7265: {
                   7266:     int spcol,sprow,a;
                   7267:     int i,j,k,l,rank;
                   7268:     NODE r0,r;
                   7269:     ND_pairs sp;
                   7270:     ND spol;
                   7271:     mpz_t **spmat;
                   7272:     mpz_t *svect,*v;
                   7273:     int *colstat;
                   7274:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   7275:     int maxrs;
                   7276:     int *spsugar;
                   7277:     pointer *w;
                   7278:
                   7279:     spcol = col-nred;
                   7280:     get_eg(&eg0);
                   7281:     /* elimination (1st step) */
                   7282:     spmat = (mpz_t **)ALLOCA(nsp*sizeof(mpz_t *));
                   7283:     svect = (mpz_t *)ALLOCA(col*sizeof(mpz_t));
                   7284:     spsugar = (int *)ALLOCA(nsp*sizeof(int));
                   7285:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
1.234   ! noro     7286:         nd_sp(m,trace,sp,&spol);
1.232     noro     7287:         if ( !spol ) continue;
                   7288:         nd_to_vect_lf(s0vect,col,spol,svect);
1.234   ! noro     7289:         maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
1.232     noro     7290:         for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
                   7291:         if ( i < col ) {
                   7292:             spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
                   7293:             for ( j = k = 0; j < col; j++ )
                   7294:                 if ( !rhead[j] ) v[k++][0] = svect[j][0];
                   7295:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   7296:             sprow++;
                   7297:         }
                   7298: /*        nd_free(spol); */
                   7299:     }
                   7300:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   7301:     if ( DP_Print ) {
                   7302:         fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   7303:         fflush(asir_out);
                   7304:     }
                   7305:     /* free index arrays */
                   7306: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   7307:
                   7308:     /* elimination (2nd step) */
                   7309:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   7310:     rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
                   7311:     w = (pointer *)ALLOCA(rank*sizeof(pointer));
                   7312:     for ( i = 0; i < rank; i++ ) {
                   7313: #if 0
                   7314:         w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   7315:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   7316: #else
                   7317:         w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   7318:         SG((NDV)w[i]) = spsugar[i];
                   7319: #endif
                   7320: /*        GCFREE(spmat[i]); */
                   7321:
                   7322:     }
                   7323: #if 0
                   7324:     qsort(w,rank,sizeof(NDV),
                   7325:         (int (*)(const void *,const void *))ndv_compare);
                   7326: #endif
                   7327:     r0 = 0;
                   7328:     for ( i = 0; i < rank; i++ ) {
                   7329:         NEXTNODE(r0,r); BDY(r) = w[i];
                   7330:     }
                   7331:     if ( r0 ) NEXT(r) = 0;
                   7332:
                   7333: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   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 ) {
                   7337:         fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   7338:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   7339:             nsp,nred,sprow,spcol,rank);
                   7340:         fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   7341:     }
                   7342:     return r0;
                   7343: }
                   7344:
1.133     noro     7345: #if 1
                   7346: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
1.107     noro     7347:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   7348: {
1.157     noro     7349:     int spcol,sprow,a;
                   7350:     int i,j,k,l,rank;
                   7351:     NODE r0,r;
                   7352:     ND_pairs sp;
                   7353:     ND spol;
                   7354:     Q **spmat;
                   7355:     Q *svect,*v;
                   7356:     int *colstat;
                   7357:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   7358:     int maxrs;
                   7359:     int *spsugar;
                   7360:     pointer *w;
                   7361:
                   7362:     spcol = col-nred;
                   7363:     get_eg(&eg0);
                   7364:     /* elimination (1st step) */
                   7365:     spmat = (Q **)ALLOCA(nsp*sizeof(Q *));
                   7366:     svect = (Q *)ALLOCA(col*sizeof(Q));
1.232     noro     7367:     spsugar = (int *)ALLOCA(nsp*sizeof(int));
1.157     noro     7368:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   7369:         nd_sp(0,trace,sp,&spol);
                   7370:         if ( !spol ) continue;
                   7371:         nd_to_vect_q(s0vect,col,spol,svect);
                   7372:         maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
                   7373:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   7374:         if ( i < col ) {
                   7375:             spmat[sprow] = v = (Q *)MALLOC(spcol*sizeof(Q));
                   7376:             for ( j = k = 0; j < col; j++ )
                   7377:                 if ( !rhead[j] ) v[k++] = svect[j];
                   7378:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   7379:             sprow++;
                   7380:         }
                   7381: /*        nd_free(spol); */
                   7382:     }
                   7383:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   7384:     if ( DP_Print ) {
                   7385:         fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   7386:         fflush(asir_out);
                   7387:     }
                   7388:     /* free index arrays */
1.200     noro     7389: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
1.157     noro     7390:
                   7391:     /* elimination (2nd step) */
                   7392:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   7393:     rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
                   7394:     w = (pointer *)ALLOCA(rank*sizeof(pointer));
                   7395:     for ( i = 0; i < rank; i++ ) {
1.213     noro     7396: #if 0
1.157     noro     7397:         w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   7398:         SG((NDV)w[rank-i-1]) = spsugar[i];
1.213     noro     7399: #else
                   7400:         w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   7401:         SG((NDV)w[i]) = spsugar[i];
                   7402: #endif
1.200     noro     7403: /*        GCFREE(spmat[i]); */
1.157     noro     7404:     }
1.138     noro     7405: #if 0
1.157     noro     7406:     qsort(w,rank,sizeof(NDV),
                   7407:         (int (*)(const void *,const void *))ndv_compare);
1.137     noro     7408: #endif
1.157     noro     7409:     r0 = 0;
                   7410:     for ( i = 0; i < rank; i++ ) {
                   7411:         NEXTNODE(r0,r); BDY(r) = w[i];
                   7412:     }
                   7413:     if ( r0 ) NEXT(r) = 0;
                   7414:
1.200     noro     7415: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
1.157     noro     7416:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   7417:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   7418:     if ( DP_Print ) {
                   7419:         fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   7420:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   7421:             nsp,nred,sprow,spcol,rank);
                   7422:         fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   7423:     }
                   7424:     return r0;
1.107     noro     7425: }
1.215     noro     7426:
                   7427: NODE nd_f4_red_gz_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   7428:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   7429: {
                   7430:     int spcol,sprow,a;
                   7431:     int i,j,k,l,rank;
                   7432:     NODE r0,r;
                   7433:     ND_pairs sp;
                   7434:     ND spol;
                   7435:     GZ **spmat;
                   7436:     GZ *svect,*v;
                   7437:     int *colstat;
                   7438:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   7439:     int maxrs;
                   7440:     int *spsugar;
                   7441:     pointer *w;
                   7442:
                   7443:     spcol = col-nred;
                   7444:     get_eg(&eg0);
                   7445:     /* elimination (1st step) */
                   7446:     spmat = (GZ **)ALLOCA(nsp*sizeof(GZ *));
                   7447:     svect = (GZ *)ALLOCA(col*sizeof(GZ));
1.232     noro     7448:     spsugar = (int *)ALLOCA(nsp*sizeof(int));
1.215     noro     7449:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   7450:         nd_sp(0,trace,sp,&spol);
                   7451:         if ( !spol ) continue;
                   7452:                spol = ndtondgz(spol);
                   7453:         nd_to_vect_q(s0vect,col,spol,(Q *)svect);
                   7454:         maxrs = ndv_reduce_vect_gz(svect,trace,col,imat,rvect,nred);
                   7455:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   7456:         if ( i < col ) {
                   7457:             spmat[sprow] = v = (GZ *)MALLOC(spcol*sizeof(GZ));
                   7458:             for ( j = k = 0; j < col; j++ )
                   7459:                 if ( !rhead[j] ) v[k++] = svect[j];
                   7460:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   7461:             sprow++;
                   7462:         }
                   7463: /*        nd_free(spol); */
                   7464:     }
                   7465:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   7466:     if ( DP_Print ) {
                   7467:         fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   7468:         fflush(asir_out);
                   7469:     }
                   7470:     /* free index arrays */
                   7471: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   7472:
                   7473:     /* elimination (2nd step) */
                   7474:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   7475:     rank = nd_gauss_elim_gz(spmat,spsugar,sprow,spcol,colstat);
                   7476:     w = (pointer *)ALLOCA(rank*sizeof(pointer));
                   7477:     for ( i = 0; i < rank; i++ ) {
                   7478: #if 0
                   7479:         w[rank-i-1] = (pointer)vect_to_ndv_gz(spmat[i],spcol,col,rhead,s0vect);
                   7480:                w[rank-i-1] = ndvgztondv(w[rank-i-1]);
                   7481:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   7482: #else
1.232     noro     7483:         w[i] = (pointer)vect_to_ndv_gz(spmat[i],spcol,col,rhead,s0vect);
1.215     noro     7484:                w[i] = ndvgztondv(w[i]);
                   7485:         SG((NDV)w[i]) = spsugar[i];
                   7486: #endif
                   7487: /*        GCFREE(spmat[i]); */
                   7488:
                   7489:     }
                   7490: #if 0
                   7491:     qsort(w,rank,sizeof(NDV),
                   7492:         (int (*)(const void *,const void *))ndv_compare);
                   7493: #endif
                   7494:     r0 = 0;
                   7495:     for ( i = 0; i < rank; i++ ) {
                   7496:         NEXTNODE(r0,r); BDY(r) = w[i];
                   7497:     }
                   7498:     if ( r0 ) NEXT(r) = 0;
                   7499:
                   7500: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   7501:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   7502:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   7503:     if ( DP_Print ) {
                   7504:         fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   7505:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   7506:             nsp,nred,sprow,spcol,rank);
                   7507:         fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   7508:     }
                   7509:     return r0;
                   7510: }
1.129     noro     7511: #else
                   7512: void printm(Q **mat,int row,int col)
                   7513: {
1.157     noro     7514:     int i,j;
                   7515:     printf("[");
                   7516:     for ( i = 0; i < row; i++ ) {
                   7517:         for ( j = 0; j < col; j++ ) {
                   7518:             printexpr(CO,mat[i][j]); printf(" ");
                   7519:         }
                   7520:         printf("]\n");
                   7521:     }
1.129     noro     7522: }
                   7523:
                   7524: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   7525:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   7526: {
1.157     noro     7527:     int row,a;
                   7528:     int i,j,rank;
                   7529:     NODE r0,r;
                   7530:     ND_pairs sp;
                   7531:     ND spol;
                   7532:     Q **mat;
                   7533:     int *colstat;
                   7534:     int *sugar;
                   7535:
                   7536:     row = nsp+nred;
                   7537:     /* make the matrix */
                   7538:     mat = (Q **)ALLOCA(row*sizeof(Q *));
                   7539:     sugar = (int *)ALLOCA(row*sizeof(int));
                   7540:     for ( row = a = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   7541:         nd_sp(0,0,sp,&spol);
                   7542:         if ( !spol ) continue;
                   7543:         mat[row] = (Q *)MALLOC(col*sizeof(Q));
                   7544:         nd_to_vect_q(s0vect,col,spol,mat[row]);
                   7545:         sugar[row] = SG(spol);
                   7546:         row++;
                   7547:     }
                   7548:     for ( i = 0; i < nred; i++, row++ ) {
                   7549:         mat[row] = nm_ind_pair_to_vect(0,s0vect,col,rvect[i]);
                   7550:         sugar[row] = rvect[i]->sugar;
                   7551:     }
                   7552:     /* elimination */
                   7553:     colstat = (int *)ALLOCA(col*sizeof(int));
                   7554:     rank = nd_gauss_elim_q(mat,sugar,row,col,colstat);
                   7555:     r0 = 0;
                   7556:     for ( i = 0; i < rank; i++ ) {
                   7557:         for ( j = 0; j < col; j++ ) if ( mat[i][j] ) break;
                   7558:         if ( j == col ) error("nd_f4_red_q_main : cannot happen");
                   7559:         if ( rhead[j] ) continue;
                   7560:         NEXTNODE(r0,r); BDY(r) =
                   7561:             (pointer)plain_vect_to_ndv_q(mat[i],col,s0vect);
                   7562:         SG((NDV)BDY(r)) = sugar[i];
                   7563:     }
                   7564:     if ( r0 ) NEXT(r) = 0;
                   7565:     printf("\n");
                   7566:     return r0;
1.129     noro     7567: }
                   7568: #endif
1.107     noro     7569:
1.74      noro     7570: FILE *nd_write,*nd_read;
                   7571:
                   7572: void nd_send_int(int a) {
1.157     noro     7573:     write_int(nd_write,&a);
1.74      noro     7574: }
                   7575:
                   7576: void nd_send_intarray(int *p,int len) {
1.157     noro     7577:     write_intarray(nd_write,p,len);
1.74      noro     7578: }
                   7579:
                   7580: int nd_recv_int() {
1.157     noro     7581:     int a;
1.74      noro     7582:
1.157     noro     7583:     read_int(nd_read,&a);
                   7584:     return a;
1.74      noro     7585: }
                   7586:
                   7587: void nd_recv_intarray(int *p,int len) {
1.157     noro     7588:     read_intarray(nd_read,p,len);
1.74      noro     7589: }
                   7590:
                   7591: void nd_send_ndv(NDV p) {
1.157     noro     7592:     int len,i;
                   7593:     NMV m;
1.74      noro     7594:
1.157     noro     7595:     if ( !p ) nd_send_int(0);
                   7596:     else {
                   7597:         len = LEN(p);
                   7598:         nd_send_int(len);
                   7599:         m = BDY(p);
                   7600:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   7601:             nd_send_int(CM(m));
                   7602:             nd_send_intarray(DL(m),nd_wpd);
                   7603:         }
                   7604:     }
1.74      noro     7605: }
                   7606:
                   7607: void nd_send_nd(ND p) {
1.157     noro     7608:     int len,i;
                   7609:     NM m;
1.74      noro     7610:
1.157     noro     7611:     if ( !p ) nd_send_int(0);
                   7612:     else {
                   7613:         len = LEN(p);
                   7614:         nd_send_int(len);
                   7615:         m = BDY(p);
                   7616:         for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   7617:             nd_send_int(CM(m));
                   7618:             nd_send_intarray(DL(m),nd_wpd);
                   7619:         }
                   7620:     }
1.74      noro     7621: }
1.65      noro     7622:
1.74      noro     7623: NDV nd_recv_ndv()
                   7624: {
1.157     noro     7625:     int len,i;
                   7626:     NMV m,m0;
                   7627:     NDV r;
                   7628:
                   7629:     len = nd_recv_int();
                   7630:     if ( !len ) return 0;
                   7631:     else {
1.200     noro     7632:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
1.103     noro     7633: #if 0
1.157     noro     7634:         ndv_alloc += len*nmv_adv;
1.103     noro     7635: #endif
1.157     noro     7636:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   7637:             CM(m) = nd_recv_int();
                   7638:             nd_recv_intarray(DL(m),nd_wpd);
                   7639:         }
                   7640:         MKNDV(nd_nvar,m0,len,r);
                   7641:         return r;
                   7642:     }
1.74      noro     7643: }
1.65      noro     7644:
1.74      noro     7645: int ox_exec_f4_red(Q proc)
                   7646: {
1.157     noro     7647:     Obj obj;
                   7648:     STRING fname;
                   7649:     NODE arg;
                   7650:     int s;
                   7651:     extern int ox_need_conv,ox_file_io;
                   7652:
                   7653:     MKSTR(fname,"nd_exec_f4_red");
                   7654:     arg = mknode(2,proc,fname);
                   7655:     Pox_cmo_rpc(arg,&obj);
                   7656:     s = get_ox_server_id(QTOS(proc));
                   7657:     nd_write = iofp[s].out;
                   7658:     nd_read = iofp[s].in;
                   7659:     ox_need_conv = ox_file_io = 0;
                   7660:     return s;
1.74      noro     7661: }
                   7662:
1.210     noro     7663: #if 0
1.133     noro     7664: NODE nd_f4_red_dist(int m,ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
1.74      noro     7665: {
1.157     noro     7666:     int nsp,nred;
                   7667:     int i,rank,s;
                   7668:     NODE rp,r0,r;
                   7669:     ND_pairs sp;
                   7670:     NM_ind_pair pair;
                   7671:     NMV nmv;
                   7672:     NM nm;
                   7673:     NDV nf;
                   7674:     Obj proc,dmy;
                   7675:
                   7676:     ox_launch_main(0,0,&proc);
                   7677:     s = ox_exec_f4_red((Q)proc);
                   7678:
                   7679:     nd_send_int(m);
                   7680:     nd_send_int(nd_nvar);
                   7681:     nd_send_int(nd_bpe);
                   7682:     nd_send_int(nd_wpd);
                   7683:     nd_send_int(nmv_adv);
                   7684:
                   7685:     saveobj(nd_write,dp_current_spec->obj); fflush(nd_write);
                   7686:
                   7687:     nd_send_int(nd_psn);
                   7688:     for ( i = 0; i < nd_psn; i++ ) nd_send_ndv(nd_ps[i]);
                   7689:
                   7690:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   7691:     nd_send_int(nsp);
                   7692:     for ( i = 0, sp = sp0; i < nsp; i++, sp = NEXT(sp) ) {
                   7693:         nd_send_int(sp->i1); nd_send_int(sp->i2);
                   7694:     }
                   7695:
                   7696:     nd_send_int(col); nd_send_intarray(s0vect,col*nd_wpd);
                   7697:
                   7698:     nred = length(rp0); nd_send_int(nred);
                   7699:     for ( i = 0, rp = rp0; i < nred; i++, rp = NEXT(rp) ) {
                   7700:         pair = (NM_ind_pair)BDY(rp);
                   7701:         nd_send_int(pair->index);
                   7702:         nd_send_intarray(pair->mul->dl,nd_wpd);
                   7703:     }
                   7704:     fflush(nd_write);
                   7705:     rank = nd_recv_int();
                   7706:     fprintf(asir_out,"rank=%d\n",rank);
                   7707:     r0 = 0;
                   7708:     for ( i = 0; i < rank; i++ ) {
                   7709:         nf = nd_recv_ndv();
                   7710:         NEXTNODE(r0,r); BDY(r) = (pointer)nf;
                   7711:     }
                   7712:     Pox_shutdown(mknode(1,proc),&dmy);
                   7713:     return r0;
1.74      noro     7714: }
                   7715:
                   7716: /* server side */
                   7717:
                   7718: void nd_exec_f4_red_dist()
                   7719: {
1.157     noro     7720:     int m,i,nsp,col,s0size,nred,spcol,j,k;
                   7721:     NM_ind_pair *rp0;
                   7722:     NDV nf;
                   7723:     UINT *s0vect;
                   7724:     IndArray *imat;
                   7725:     int *rhead;
                   7726:     int **spmat;
                   7727:     UINT *svect,*v;
                   7728:     ND_pairs *sp0;
                   7729:     int *colstat;
                   7730:     int a,sprow,rank;
                   7731:     struct order_spec *ord;
                   7732:     Obj ordspec;
                   7733:     ND spol;
                   7734:     int maxrs;
                   7735:     int *spsugar;
                   7736:
                   7737:     nd_read = iofp[0].in;
                   7738:     nd_write = iofp[0].out;
                   7739:     m = nd_recv_int();
                   7740:     nd_nvar = nd_recv_int();
                   7741:     nd_bpe = nd_recv_int();
                   7742:     nd_wpd = nd_recv_int();
                   7743:     nmv_adv = nd_recv_int();
                   7744:
                   7745:     loadobj(nd_read,&ordspec);
                   7746:     create_order_spec(0,ordspec,&ord);
                   7747:     nd_init_ord(ord);
                   7748:     nd_setup_parameters(nd_nvar,0);
                   7749:
                   7750:     nd_psn = nd_recv_int();
                   7751:     nd_ps = (NDV *)MALLOC(nd_psn*sizeof(NDV));
                   7752:     nd_bound = (UINT **)MALLOC(nd_psn*sizeof(UINT *));
                   7753:     for ( i = 0; i < nd_psn; i++ ) {
                   7754:         nd_ps[i] = nd_recv_ndv();
                   7755:         nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   7756:     }
                   7757:
                   7758:     nsp = nd_recv_int();
                   7759:     sp0 = (ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   7760:     for ( i = 0; i < nsp; i++ ) {
                   7761:         NEWND_pairs(sp0[i]);
                   7762:         sp0[i]->i1 = nd_recv_int(); sp0[i]->i2 = nd_recv_int();
                   7763:         ndl_lcm(HDL(nd_ps[sp0[i]->i1]),HDL(nd_ps[sp0[i]->i2]),LCM(sp0[i]));
                   7764:     }
                   7765:
                   7766:     col = nd_recv_int();
                   7767:     s0size = col*nd_wpd;
                   7768:     s0vect = (UINT *)MALLOC(s0size*sizeof(UINT));
                   7769:     nd_recv_intarray(s0vect,s0size);
                   7770:
                   7771:     nred = nd_recv_int();
                   7772:     rp0 = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   7773:     for ( i = 0; i < nred; i++ ) {
                   7774:         rp0[i] = (NM_ind_pair)MALLOC(sizeof(struct oNM_ind_pair));
                   7775:         rp0[i]->index = nd_recv_int();
                   7776:         rp0[i]->mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   7777:         nd_recv_intarray(rp0[i]->mul->dl,nd_wpd);
                   7778:     }
                   7779:
                   7780:     spcol = col-nred;
                   7781:     imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   7782:     rhead = (int *)MALLOC(col*sizeof(int));
                   7783:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   7784:
                   7785:     /* construction of index arrays */
                   7786:     for ( i = 0; i < nred; i++ ) {
                   7787:         imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rp0[i]);
                   7788:         rhead[imat[i]->head] = 1;
                   7789:     }
                   7790:
                   7791:     /* elimination (1st step) */
                   7792:     spmat = (int **)MALLOC(nsp*sizeof(UINT *));
                   7793:     svect = (UINT *)MALLOC(col*sizeof(UINT));
1.232     noro     7794:     spsugar = (int *)ALLOCA(nsp*sizeof(int));
1.157     noro     7795:     for ( a = sprow = 0; a < nsp; a++ ) {
                   7796:         nd_sp(m,0,sp0[a],&spol);
                   7797:         if ( !spol ) continue;
                   7798:         nd_to_vect(m,s0vect,col,spol,svect);
                   7799:         if ( m == -1 )
                   7800:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rp0,nred);
                   7801:         else
                   7802:             maxrs = ndv_reduce_vect(m,svect,col,imat,rp0,nred);
                   7803:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   7804:         if ( i < col ) {
                   7805:             spmat[sprow] = v = (UINT *)MALLOC(spcol*sizeof(UINT));
                   7806:             for ( j = k = 0; j < col; j++ )
                   7807:                 if ( !rhead[j] ) v[k++] = svect[j];
                   7808:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   7809:             sprow++;
                   7810:         }
                   7811:         nd_free(spol);
                   7812:     }
                   7813:     /* elimination (2nd step) */
                   7814:     colstat = (int *)ALLOCA(spcol*sizeof(int));
                   7815:     if ( m == -1 )
                   7816:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   7817:     else
                   7818:         rank = nd_gauss_elim_mod(spmat,spsugar,0,sprow,spcol,m,colstat);
                   7819:     nd_send_int(rank);
                   7820:     for ( i = 0; i < rank; i++ ) {
                   7821:         nf = vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   7822:         nd_send_ndv(nf);
                   7823:     }
                   7824:     fflush(nd_write);
1.107     noro     7825: }
1.210     noro     7826: #endif
1.107     noro     7827:
1.113     noro     7828: int nd_gauss_elim_q(Q **mat0,int *sugar,int row,int col,int *colstat)
1.107     noro     7829: {
1.176     noro     7830:     int i,j,t,c,rank,inv;
1.157     noro     7831:     int *ci,*ri;
                   7832:     Q dn;
                   7833:     MAT m,nm;
                   7834:
                   7835:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
                   7836:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   7837:     for ( i = 0; i < row; i++ )
                   7838:         for ( j = 0; j < col; j++ )
                   7839:             mat0[i][j] = 0;
                   7840:     c = col-rank;
                   7841:     for ( i = 0; i < rank; i++ ) {
                   7842:         mat0[i][ri[i]] = dn;
                   7843:         for ( j = 0; j < c; j++ )
                   7844:             mat0[i][ci[j]] = (Q)BDY(nm)[i][j];
                   7845:     }
                   7846:     return rank;
1.76      noro     7847: }
                   7848:
1.215     noro     7849: int nd_gauss_elim_gz(GZ **mat0,int *sugar,int row,int col,int *colstat)
                   7850: {
                   7851:     int i,j,t,c,rank,inv;
                   7852:     int *ci,*ri;
1.216     noro     7853:     GZ dn;
1.215     noro     7854:     MAT m,nm;
                   7855:
                   7856:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
1.216     noro     7857:     rank = gz_generic_gauss_elim(m,&nm,&dn,&ri,&ci);
1.215     noro     7858:     for ( i = 0; i < row; i++ )
                   7859:         for ( j = 0; j < col; j++ )
                   7860:             mat0[i][j] = 0;
                   7861:     c = col-rank;
                   7862:     for ( i = 0; i < rank; i++ ) {
1.216     noro     7863:         mat0[i][ri[i]] = dn;
1.215     noro     7864:         for ( j = 0; j < c; j++ )
                   7865:             mat0[i][ci[j]] = (GZ)BDY(nm)[i][j];
                   7866:     }
                   7867:     return rank;
                   7868: }
                   7869:
1.133     noro     7870: int nd_gauss_elim_mod(int **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
1.76      noro     7871: {
1.157     noro     7872:     int i,j,k,l,inv,a,rank,s;
                   7873:     unsigned int *t,*pivot,*pk;
                   7874:     unsigned int **mat;
                   7875:     ND_pairs pair;
                   7876:
                   7877:     mat = (unsigned int **)mat0;
                   7878:     for ( rank = 0, j = 0; j < col; j++ ) {
                   7879:         for ( i = rank; i < row; i++ )
                   7880:             mat[i][j] %= md;
                   7881:         for ( i = rank; i < row; i++ )
                   7882:             if ( mat[i][j] )
                   7883:                 break;
                   7884:         if ( i == row ) {
                   7885:             colstat[j] = 0;
                   7886:             continue;
                   7887:         } else
                   7888:             colstat[j] = 1;
                   7889:         if ( i != rank ) {
                   7890:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   7891:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   7892:             if ( spactive ) {
                   7893:                 pair = spactive[i]; spactive[i] = spactive[rank];
                   7894:                 spactive[rank] = pair;
                   7895:             }
                   7896:         }
                   7897:         pivot = mat[rank];
                   7898:         s = sugar[rank];
                   7899:         inv = invm(pivot[j],md);
                   7900:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   7901:             if ( *pk ) {
                   7902:                 if ( *pk >= (unsigned int)md )
                   7903:                     *pk %= md;
                   7904:                 DMAR(*pk,inv,0,md,*pk)
                   7905:             }
                   7906:         for ( i = rank+1; i < row; i++ ) {
                   7907:             t = mat[i];
                   7908:             if ( a = t[j] ) {
                   7909:                 sugar[i] = MAX(sugar[i],s);
                   7910:                 red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   7911:             }
                   7912:         }
                   7913:         rank++;
                   7914:     }
                   7915:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   7916:         if ( colstat[j] ) {
                   7917:             pivot = mat[l];
                   7918:             s = sugar[l];
                   7919:             for ( i = 0; i < l; i++ ) {
                   7920:                 t = mat[i];
                   7921:                 t[j] %= md;
                   7922:                 if ( a = t[j] ) {
                   7923:                     sugar[i] = MAX(sugar[i],s);
                   7924:                     red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   7925:                 }
                   7926:             }
                   7927:             l--;
                   7928:         }
                   7929:     for ( j = 0, l = 0; l < rank; j++ )
                   7930:         if ( colstat[j] ) {
                   7931:             t = mat[l];
                   7932:             for ( k = j; k < col; k++ )
                   7933:                 if ( t[k] >= (unsigned int)md )
                   7934:                     t[k] %= md;
                   7935:             l++;
                   7936:         }
                   7937:     return rank;
1.76      noro     7938: }
                   7939:
                   7940: int nd_gauss_elim_sf(int **mat0,int *sugar,int row,int col,int md,int *colstat)
                   7941: {
1.157     noro     7942:     int i,j,k,l,inv,a,rank,s;
                   7943:     unsigned int *t,*pivot,*pk;
                   7944:     unsigned int **mat;
                   7945:
                   7946:     mat = (unsigned int **)mat0;
                   7947:     for ( rank = 0, j = 0; j < col; j++ ) {
                   7948:         for ( i = rank; i < row; i++ )
                   7949:             if ( mat[i][j] )
                   7950:                 break;
                   7951:         if ( i == row ) {
                   7952:             colstat[j] = 0;
                   7953:             continue;
                   7954:         } else
                   7955:             colstat[j] = 1;
                   7956:         if ( i != rank ) {
                   7957:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   7958:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   7959:         }
                   7960:         pivot = mat[rank];
                   7961:         s = sugar[rank];
                   7962:         inv = _invsf(pivot[j]);
                   7963:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   7964:             if ( *pk )
                   7965:                 *pk = _mulsf(*pk,inv);
                   7966:         for ( i = rank+1; i < row; i++ ) {
                   7967:             t = mat[i];
                   7968:             if ( a = t[j] ) {
                   7969:                 sugar[i] = MAX(sugar[i],s);
                   7970:                 red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   7971:             }
                   7972:         }
                   7973:         rank++;
                   7974:     }
                   7975:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   7976:         if ( colstat[j] ) {
                   7977:             pivot = mat[l];
                   7978:             s = sugar[l];
                   7979:             for ( i = 0; i < l; i++ ) {
                   7980:                 t = mat[i];
                   7981:                 if ( a = t[j] ) {
                   7982:                     sugar[i] = MAX(sugar[i],s);
                   7983:                     red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   7984:                 }
                   7985:             }
                   7986:             l--;
                   7987:         }
                   7988:     return rank;
1.77      noro     7989: }
                   7990:
                   7991: int ndv_ishomo(NDV p)
                   7992: {
1.157     noro     7993:     NMV m;
                   7994:     int len,h;
1.77      noro     7995:
1.157     noro     7996:     if ( !p ) return 1;
                   7997:     len = LEN(p);
                   7998:     m = BDY(p);
                   7999:     h = TD(DL(m));
                   8000:     NMV_ADV(m);
                   8001:     for ( len--; len; len--, NMV_ADV(m) )
                   8002:         if ( TD(DL(m)) != h ) return 0;
                   8003:     return 1;
1.77      noro     8004: }
                   8005:
                   8006: void ndv_save(NDV p,int index)
                   8007: {
1.157     noro     8008:     FILE *s;
                   8009:     char name[BUFSIZ];
                   8010:     short id;
                   8011:     int nv,sugar,len,n,i,td,e,j;
                   8012:     NMV m;
                   8013:     unsigned int *dl;
                   8014:     int mpos;
                   8015:
                   8016:     sprintf(name,"%s/%d",Demand,index);
                   8017:     s = fopen(name,"w");
                   8018:     savevl(s,0);
                   8019:     if ( !p ) {
                   8020:         saveobj(s,0);
                   8021:         return;
                   8022:     }
                   8023:     id = O_DP;
                   8024:     nv = NV(p);
                   8025:     sugar = SG(p);
                   8026:     len = LEN(p);
                   8027:     write_short(s,&id); write_int(s,&nv); write_int(s,&sugar);
                   8028:     write_int(s,&len);
                   8029:
                   8030:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   8031:         saveobj(s,(Obj)CQ(m));
                   8032:         dl = DL(m);
                   8033:         td = TD(dl);
                   8034:         write_int(s,&td);
                   8035:         for ( j = 0; j < nv; j++ ) {
                   8036:             e = GET_EXP(dl,j);
                   8037:             write_int(s,&e);
                   8038:         }
                   8039:         if ( nd_module ) {
                   8040:             mpos = MPOS(dl); write_int(s,&mpos);
                   8041:         }
                   8042:     }
                   8043:     fclose(s);
1.77      noro     8044: }
                   8045:
1.206     noro     8046: void nd_save_mod(ND p,int index)
                   8047: {
                   8048:     FILE *s;
                   8049:     char name[BUFSIZ];
                   8050:     int nv,sugar,len,c;
                   8051:     NM m;
                   8052:
                   8053:     sprintf(name,"%s/%d",Demand,index);
                   8054:     s = fopen(name,"w");
                   8055:     if ( !p ) {
                   8056:                len = 0;
                   8057:        write_int(s,&len);
                   8058:                fclose(s);
                   8059:         return;
                   8060:     }
                   8061:     nv = NV(p);
                   8062:     sugar = SG(p);
                   8063:     len = LEN(p);
                   8064:     write_int(s,&nv); write_int(s,&sugar); write_int(s,&len);
                   8065:        for ( m = BDY(p); m; m = NEXT(m) ) {
                   8066:          c = CM(m); write_int(s,&c);
                   8067:          write_intarray(s,DL(m),nd_wpd);
                   8068:        }
                   8069:     fclose(s);
                   8070: }
                   8071:
1.77      noro     8072: NDV ndv_load(int index)
                   8073: {
1.157     noro     8074:     FILE *s;
                   8075:     char name[BUFSIZ];
                   8076:     short id;
                   8077:     int nv,sugar,len,n,i,td,e,j;
                   8078:     NDV d;
                   8079:     NMV m0,m;
                   8080:     unsigned int *dl;
                   8081:     Obj obj;
                   8082:     int mpos;
                   8083:
                   8084:     sprintf(name,"%s/%d",Demand,index);
                   8085:     s = fopen(name,"r");
                   8086:     if ( !s ) return 0;
                   8087:
                   8088:     skipvl(s);
                   8089:     read_short(s,&id);
                   8090:     if ( !id ) return 0;
                   8091:     read_int(s,&nv);
                   8092:     read_int(s,&sugar);
                   8093:     read_int(s,&len);
                   8094:
                   8095:     m0 = m = MALLOC(len*nmv_adv);
                   8096:     for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   8097:         loadobj(s,&obj); CQ(m) = (Q)obj;
                   8098:         dl = DL(m);
                   8099:         ndl_zero(dl);
                   8100:         read_int(s,&td); TD(dl) = td;
                   8101:         for ( j = 0; j < nv; j++ ) {
                   8102:             read_int(s,&e);
                   8103:             PUT_EXP(dl,j,e);
                   8104:         }
                   8105:         if ( nd_module ) {
                   8106:             read_int(s,&mpos); MPOS(dl) = mpos;
                   8107:         }
                   8108:         if ( nd_blockmask ) ndl_weight_mask(dl);
                   8109:     }
                   8110:     fclose(s);
                   8111:     MKNDV(nv,m0,len,d);
                   8112:     SG(d) = sugar;
                   8113:     return d;
1.99      noro     8114: }
                   8115:
1.206     noro     8116: ND nd_load_mod(int index)
                   8117: {
                   8118:     FILE *s;
                   8119:     char name[BUFSIZ];
                   8120:     int nv,sugar,len,i,c;
                   8121:        ND d;
                   8122:     NM m0,m;
                   8123:
                   8124:     sprintf(name,"%s/%d",Demand,index);
                   8125:     s = fopen(name,"r");
                   8126:        /* if the file does not exist, it means p[index]=0 */
                   8127:     if ( !s ) return 0;
                   8128:
                   8129:     read_int(s,&nv);
                   8130:        if ( !nv ) { fclose(s); return 0; }
                   8131:
                   8132:     read_int(s,&sugar);
                   8133:     read_int(s,&len);
                   8134:        for ( m0 = 0, i = 0; i < len; i++ ) {
                   8135:                NEXTNM(m0,m);
                   8136:                read_int(s,&c); CM(m) = c;
                   8137:                read_intarray(s,DL(m),nd_wpd);
                   8138:        }
                   8139:        NEXT(m) = 0;
                   8140:     MKND(nv,m0,len,d);
                   8141:     SG(d) = sugar;
                   8142:        fclose(s);
                   8143:     return d;
                   8144: }
                   8145:
1.102     noro     8146: void nd_det(int mod,MAT f,P *rp)
1.99      noro     8147: {
1.157     noro     8148:     VL fv,tv;
                   8149:     int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
                   8150:     pointer **m;
                   8151:     Q mone;
                   8152:     P **w;
                   8153:     P mp,r;
                   8154:     NDV **dm;
                   8155:     NDV *t,*mi,*mj;
                   8156:     NDV d,s,mij,mjj;
                   8157:     ND u;
                   8158:     NMV nmv;
                   8159:     UINT *bound;
                   8160:     PGeoBucket bucket;
                   8161:     struct order_spec *ord;
                   8162:     Q dq,dt,ds;
                   8163:     N gn,qn,dn0,nm,dn;
                   8164:
                   8165:     create_order_spec(0,0,&ord);
                   8166:     nd_init_ord(ord);
                   8167:     get_vars((Obj)f,&fv);
                   8168:     if ( f->row != f->col )
                   8169:         error("nd_det : non-square matrix");
                   8170:     n = f->row;
                   8171:     m = f->body;
                   8172:     for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   8173:
                   8174:     if ( !nvar ) {
                   8175:         if ( !mod )
                   8176:             detp(CO,(P **)m,n,rp);
                   8177:         else {
                   8178:             w = (P **)almat_pointer(n,n);
                   8179:             for ( i = 0; i < n; i++ )
                   8180:                 for ( j = 0; j < n; j++ )
                   8181:                     ptomp(mod,(P)m[i][j],&w[i][j]);
                   8182:             detmp(CO,mod,w,n,&mp);
                   8183:             mptop(mp,rp);
                   8184:         }
                   8185:         return;
                   8186:     }
                   8187:
                   8188:     if ( !mod ) {
                   8189:         w = (P **)almat_pointer(n,n);
                   8190:         dq = ONE;
                   8191:         for ( i = 0; i < n; i++ ) {
                   8192:             dn0 = ONEN;
                   8193:             for ( j = 0; j < n; j++ ) {
                   8194:                 if ( !m[i][j] ) continue;
                   8195:                 lgp(m[i][j],&nm,&dn);
                   8196:                 gcdn(dn0,dn,&gn); divsn(dn0,gn,&qn); muln(qn,dn,&dn0);
                   8197:             }
                   8198:             if ( !UNIN(dn0) ) {
                   8199:                 NTOQ(dn0,1,ds);
                   8200:                 for ( j = 0; j < n; j++ )
                   8201:                     mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   8202:                 mulq(dq,ds,&dt); dq = dt;
                   8203:             } else
                   8204:                 for ( j = 0; j < n; j++ )
                   8205:                     w[i][j] = (P)m[i][j];
                   8206:         }
                   8207:         m = (pointer **)w;
                   8208:     }
                   8209:
1.178     noro     8210:     for ( i = 0, max = 1; i < n; i++ )
1.157     noro     8211:         for ( j = 0; j < n; j++ )
                   8212:             for ( tv = fv; tv; tv = NEXT(tv) ) {
                   8213:                 e = getdeg(tv->v,(P)m[i][j]);
                   8214:                 max = MAX(e,max);
                   8215:             }
                   8216:     nd_setup_parameters(nvar,max);
                   8217:     dm = (NDV **)almat_pointer(n,n);
1.178     noro     8218:     for ( i = 0, max = 1; i < n; i++ )
1.157     noro     8219:         for ( j = 0; j < n; j++ ) {
                   8220:             dm[i][j] = ptondv(CO,fv,m[i][j]);
                   8221:             if ( mod ) ndv_mod(mod,dm[i][j]);
                   8222:             if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   8223:         }
                   8224:     d = ptondv(CO,fv,(P)ONE);
                   8225:     if ( mod ) ndv_mod(mod,d);
                   8226:     chsgnq(ONE,&mone);
                   8227:     for ( j = 0, sgn = 1; j < n; j++ ) {
1.222     fujimoto 8228:         if ( DP_Print ) {
                   8229:          fprintf(stderr,".",j);
                   8230:        }
1.157     noro     8231:         for ( i = j; i < n && !dm[i][j]; i++ );
                   8232:         if ( i == n ) {
                   8233:             *rp = 0;
                   8234:             return;
                   8235:         }
                   8236:         k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
                   8237:         for ( k = j; k < n; k++ )
                   8238:             for ( l = j; l < n; l++ )
                   8239:                 if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   8240:                     k0 = k; l0 = l; len0 = LEN(dm[k][l]);
                   8241:                 }
                   8242:         if ( k0 != j ) {
                   8243:             t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   8244:             sgn = -sgn;
                   8245:         }
                   8246:         if ( l0 != j ) {
                   8247:             for ( k = j; k < n; k++ ) {
                   8248:                 s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   8249:             }
                   8250:             sgn = -sgn;
                   8251:         }
                   8252:         bound = nd_det_compute_bound(dm,n,j);
                   8253:         for ( k = 0; k < nd_nvar; k++ )
                   8254:             if ( bound[k]*2 > nd_mask0 ) break;
                   8255:         if ( k < nd_nvar )
                   8256:             nd_det_reconstruct(dm,n,j,d);
                   8257:
                   8258:         for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
                   8259: /*            if ( DP_Print ) fprintf(stderr,"    i=%d\n        ",i); */
                   8260:             mi = dm[i]; mij = mi[j];
                   8261:             if ( mod )
                   8262:                 ndv_mul_c(mod,mij,mod-1);
                   8263:             else
                   8264:                 ndv_mul_c_q(mij,mone);
                   8265:             for ( k = j+1; k < n; k++ ) {
                   8266: /*                if ( DP_Print ) fprintf(stderr,"k=%d ",k); */
                   8267:                 bucket = create_pbucket();
                   8268:                 if ( mi[k] ) {
                   8269:                     nmv = BDY(mjj); len = LEN(mjj);
                   8270:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   8271:                         u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   8272:                         add_pbucket(mod,bucket,u);
                   8273:                     }
                   8274:                 }
                   8275:                 if ( mj[k] && mij ) {
                   8276:                     nmv = BDY(mij); len = LEN(mij);
                   8277:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   8278:                         u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   8279:                         add_pbucket(mod,bucket,u);
                   8280:                     }
                   8281:                 }
                   8282:                 u = nd_quo(mod,bucket,d);
                   8283:                 mi[k] = ndtondv(mod,u);
                   8284:             }
                   8285: /*            if ( DP_Print ) fprintf(stderr,"\n",k); */
                   8286:         }
                   8287:         d = mjj;
                   8288:     }
1.222     fujimoto 8289:     if ( DP_Print ) {
                   8290:       fprintf(stderr,"\n",k);
                   8291:     }
1.157     noro     8292:     if ( sgn < 0 )
                   8293:         if ( mod )
                   8294:             ndv_mul_c(mod,d,mod-1);
                   8295:         else
                   8296:             ndv_mul_c_q(d,mone);
                   8297:     r = ndvtop(mod,CO,fv,d);
                   8298:     if ( !mod && !UNIQ(dq) )
                   8299:         divsp(CO,r,(P)dq,rp);
                   8300:     else
                   8301:         *rp = r;
1.99      noro     8302: }
                   8303:
1.102     noro     8304: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
1.99      noro     8305: {
1.157     noro     8306:     NM mr,mr0;
                   8307:     NM tnm;
                   8308:     NMV m;
                   8309:     UINT *d0,*dt,*dm;
                   8310:     int c,n,td,i,c1,c2,len;
                   8311:     Q q;
                   8312:     ND r;
                   8313:
                   8314:     if ( !p ) return 0;
                   8315:     else {
                   8316:         n = NV(p); m = BDY(p); len = LEN(p);
                   8317:         d0 = DL(m0);
                   8318:         td = TD(d);
                   8319:         mr0 = 0;
                   8320:         NEWNM(tnm);
                   8321:         if ( mod ) {
                   8322:             c = CM(m0);
                   8323:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   8324:                 ndl_add(DL(m),d0,DL(tnm));
                   8325:                 if ( ndl_reducible(DL(tnm),d) ) {
                   8326:                     NEXTNM(mr0,mr);
                   8327:                     c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
                   8328:                     ndl_copy(DL(tnm),DL(mr));
                   8329:                 }
                   8330:             }
                   8331:         } else {
                   8332:             q = CQ(m0);
                   8333:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   8334:                 ndl_add(DL(m),d0,DL(tnm));
                   8335:                 if ( ndl_reducible(DL(tnm),d) ) {
                   8336:                     NEXTNM(mr0,mr);
                   8337:                     mulq(CQ(m),q,&CQ(mr));
                   8338:                     ndl_copy(DL(tnm),DL(mr));
                   8339:                 }
                   8340:             }
                   8341:         }
                   8342:         if ( !mr0 )
                   8343:             return 0;
                   8344:         else {
                   8345:             NEXT(mr) = 0;
                   8346:             for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   8347:             MKND(NV(p),mr0,len,r);
                   8348:             SG(r) = SG(p) + TD(d0);
                   8349:             return r;
                   8350:         }
                   8351:     }
1.114     noro     8352: }
                   8353:
                   8354: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   8355: {
1.157     noro     8356:     int i,obpe,oadv,h,k,l;
                   8357:     static NM prev_nm_free_list;
                   8358:     EPOS oepos;
                   8359:
                   8360:     obpe = nd_bpe;
                   8361:     oadv = nmv_adv;
                   8362:     oepos = nd_epos;
                   8363:     if ( obpe < 2 ) nd_bpe = 2;
                   8364:     else if ( obpe < 3 ) nd_bpe = 3;
                   8365:     else if ( obpe < 4 ) nd_bpe = 4;
                   8366:     else if ( obpe < 5 ) nd_bpe = 5;
                   8367:     else if ( obpe < 6 ) nd_bpe = 6;
                   8368:     else if ( obpe < 8 ) nd_bpe = 8;
                   8369:     else if ( obpe < 10 ) nd_bpe = 10;
                   8370:     else if ( obpe < 16 ) nd_bpe = 16;
                   8371:     else if ( obpe < 32 ) nd_bpe = 32;
                   8372:     else error("nd_det_reconstruct : exponent too large");
                   8373:
                   8374:     nd_setup_parameters(nd_nvar,0);
                   8375:     prev_nm_free_list = _nm_free_list;
                   8376:     _nm_free_list = 0;
                   8377:     for ( k = j; k < n; k++ )
                   8378:         for (l = j; l < n; l++ )
                   8379:             ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   8380:     ndv_realloc(d,obpe,oadv,oepos);
                   8381:     prev_nm_free_list = 0;
1.114     noro     8382: #if 0
1.157     noro     8383:     GC_gcollect();
1.114     noro     8384: #endif
                   8385: }
                   8386:
1.153     noro     8387: /* returns a UINT array containing degree bounds */
                   8388:
1.114     noro     8389: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   8390: {
1.157     noro     8391:     UINT *d0,*d1,*d,*t,*r;
                   8392:     int k,l,i;
1.114     noro     8393:
1.157     noro     8394:     d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
                   8395:     for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
                   8396:     for ( k = j; k < n; k++ )
                   8397:         for ( l = j; l < n; l++ )
                   8398:             if ( dm[k][l] ) {
                   8399:                 d = ndv_compute_bound(dm[k][l]);
                   8400:                 for ( i = 0; i < nd_nvar; i++ )
                   8401:                     d0[i] = MAX(d0[i],d[i]);
                   8402:             }
                   8403:     return d0;
1.117     noro     8404: }
                   8405:
                   8406: DL nd_separate_d(UINT *d,UINT *trans)
                   8407: {
1.157     noro     8408:     int n,td,i,e,j;
                   8409:     DL a;
1.117     noro     8410:
1.157     noro     8411:     ndl_zero(trans);
                   8412:     td = 0;
                   8413:     for ( i = 0; i < nd_ntrans; i++ ) {
                   8414:         e = GET_EXP(d,i);
                   8415:         PUT_EXP(trans,i,e);
                   8416:         td += MUL_WEIGHT(e,i);
                   8417:     }
                   8418:     if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   8419:         /* homogenized */
                   8420:         i = nd_nvar-1;
                   8421:         e = GET_EXP(d,i);
                   8422:         PUT_EXP(trans,i,e);
                   8423:         td += MUL_WEIGHT(e,i);
                   8424:     }
                   8425:     TD(trans) = td;
                   8426:     if ( nd_blockmask) ndl_weight_mask(trans);
                   8427:     NEWDL(a,nd_nalg);
                   8428:     td = 0;
                   8429:     for ( i = 0; i < nd_nalg; i++ ) {
                   8430:         j = nd_ntrans+i;
                   8431:         e = GET_EXP(d,j);
                   8432:         a->d[i] = e;
                   8433:         td += e;
                   8434:     }
                   8435:     a->td = td;
                   8436:     return a;
1.117     noro     8437: }
                   8438:
1.118     noro     8439: int nd_monic(int mod,ND *p)
1.117     noro     8440: {
1.157     noro     8441:     UINT *trans,*t;
                   8442:     DL alg;
                   8443:     MP mp0,mp;
                   8444:     NM m,m0,m1,ma0,ma,mb,mr0,mr;
                   8445:     ND r;
                   8446:     DL dl;
                   8447:     DP nm;
                   8448:     NDV ndv;
                   8449:     DAlg inv,cd;
                   8450:     ND s,c;
                   8451:     Q l,mul;
                   8452:     N ln;
                   8453:     int n,ntrans,i,e,td,is_lc,len;
                   8454:     NumberField nf;
                   8455:     struct oEGT eg0,eg1;
                   8456:
                   8457:     if ( !(nf = get_numberfield()) )
                   8458:         error("nd_monic : current_numberfield is not set");
                   8459:
                   8460:     /* Q coef -> DAlg coef */
                   8461:     NEWNM(ma0); ma = ma0;
                   8462:     m = BDY(*p);
                   8463:     is_lc = 1;
                   8464:     while ( 1 ) {
                   8465:         NEWMP(mp0); mp = mp0;
                   8466:         mp->c = (P)CQ(m);
                   8467:         mp->dl = nd_separate_d(DL(m),DL(ma));
                   8468:         NEWNM(mb);
                   8469:         for ( m = NEXT(m); m; m = NEXT(m) ) {
                   8470:             alg = nd_separate_d(DL(m),DL(mb));
                   8471:             if ( !ndl_equal(DL(ma),DL(mb)) )
                   8472:                 break;
                   8473:             NEXTMP(mp0,mp); mp->c = (P)CQ(m); mp->dl = alg;
                   8474:         }
                   8475:         NEXT(mp) = 0;
                   8476:         MKDP(nd_nalg,mp0,nm);
                   8477:         MKDAlg(nm,ONE,cd);
                   8478:         if ( is_lc == 1 ) {
                   8479:             /* if the lc is a rational number, we have nothing to do */
                   8480:             if ( !mp0->dl->td )
                   8481:                 return 1;
                   8482:
                   8483:             get_eg(&eg0);
                   8484:             invdalg(cd,&inv);
                   8485:             get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   8486:             /* check the validity of inv */
                   8487:             if ( mod && !rem(NM(inv->dn),mod) )
                   8488:                 return 0;
                   8489:             CA(ma) = nf->one;
                   8490:             is_lc = 0;
                   8491:             ln = ONEN;
                   8492:         } else {
                   8493:             muldalg(cd,inv,&CA(ma));
                   8494:             lcmn(ln,NM(CA(ma)->dn),&ln);
                   8495:         }
                   8496:         if ( m ) {
                   8497:             NEXT(ma) = mb; ma = mb;
                   8498:         } else {
                   8499:             NEXT(ma) = 0;
                   8500:             break;
                   8501:         }
                   8502:     }
                   8503:     /* l = lcm(denoms) */
                   8504:     NTOQ(ln,1,l);
                   8505:     for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
                   8506:         divq(l,CA(m)->dn,&mul);
                   8507:         for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   8508:             NEXTNM(mr0,mr);
                   8509:             mulq((Q)mp->c,mul,&CQ(mr));
                   8510:             dl = mp->dl;
                   8511:             td = TD(DL(m));
                   8512:             ndl_copy(DL(m),DL(mr));
                   8513:             for ( i = 0; i < nd_nalg; i++ ) {
                   8514:                 e = dl->d[i];
                   8515:                 PUT_EXP(DL(mr),i+nd_ntrans,e);
                   8516:                 td += MUL_WEIGHT(e,i+nd_ntrans);
                   8517:             }
1.163     noro     8518:             if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
1.157     noro     8519:             TD(DL(mr)) = td;
                   8520:             if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   8521:         }
                   8522:     }
                   8523:     NEXT(mr) = 0;
                   8524:     for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   8525:     MKND(NV(*p),mr0,len,r);
                   8526:     /* XXX */
                   8527:     SG(r) = SG(*p);
                   8528:     nd_free(*p);
                   8529:     *p = r;
                   8530:     return 1;
1.59      noro     8531: }
1.167     noro     8532:
                   8533: NODE reverse_node(NODE n)
                   8534: {
                   8535:     NODE t,t1;
                   8536:
                   8537:     for ( t = 0; n; n = NEXT(n) ) {
                   8538:         MKNODE(t1,BDY(n),t); t = t1;
                   8539:     }
                   8540:     return t;
                   8541: }
                   8542:
                   8543: P ndc_div(int mod,union oNDC a,union oNDC b)
                   8544: {
                   8545:     union oNDC c;
                   8546:     int inv,t;
                   8547:
                   8548:     if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.232     noro     8549:     else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.167     noro     8550:     else if ( mod ) {
                   8551:         inv = invm(b.m,mod);
                   8552:         DMAR(a.m,inv,0,mod,t); c.m = t;
                   8553:     } else if ( nd_vc )
                   8554:        divsp(nd_vc,a.p,b.p,&c.p);
                   8555:     else
                   8556:        divq(a.z,b.z,&c.z);
                   8557:     return ndctop(mod,c);
                   8558: }
                   8559:
                   8560: P ndctop(int mod,union oNDC c)
                   8561: {
                   8562:     Q q;
                   8563:     int e;
                   8564:     GFS gfs;
                   8565:
                   8566:     if ( mod == -1 ) {
                   8567:         e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
1.232     noro     8568:     } else if ( mod > 0 ) {
1.167     noro     8569:         STOQ(c.m,q); return (P)q;
                   8570:     } else
                   8571:         return (P)c.p;
                   8572: }
                   8573:
                   8574: /* [0,0,0,cont] = p -> p/cont */
                   8575:
                   8576: void finalize_tracelist(int i,P cont)
                   8577: {
                   8578:         LIST l;
                   8579:         NODE node;
                   8580:      Q iq;
                   8581:
                   8582:         if ( !UNIQ(cont) ) {
1.196     noro     8583:          node = mknode(4,NULLP,NULLP,NULLP,cont);
1.167     noro     8584:          MKLIST(l,node); MKNODE(node,l,nd_tracelist);
                   8585:                 nd_tracelist = node;
                   8586:         }
                   8587:      STOQ(i,iq);
                   8588:      nd_tracelist = reverse_node(nd_tracelist);
                   8589:      MKLIST(l,nd_tracelist);
                   8590:      node = mknode(2,iq,l); MKLIST(l,node);
                   8591:      MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
                   8592:      nd_alltracelist = node; nd_tracelist = 0;
                   8593: }
                   8594:
                   8595: void conv_ilist(int demand,int trace,NODE g,int **indp)
                   8596: {
                   8597:     int n,i,j;
                   8598:        int *ind;
                   8599:        NODE t;
                   8600:
                   8601:     n = length(g);
                   8602:        ind = (int *)MALLOC(n*sizeof(int));
                   8603:        for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
                   8604:                j = (long)BDY(t); ind[i] = j;
                   8605:                BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
                   8606:        }
                   8607:        if ( indp ) *indp = ind;
                   8608: }
1.172     noro     8609:
                   8610: void parse_nd_option(NODE opt)
                   8611: {
1.187     noro     8612:     NODE t,p,u;
1.231     noro     8613:        int i,s,n;
1.172     noro     8614:     char *key;
                   8615:     Obj value;
                   8616:
1.187     noro     8617:     nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
1.208     noro     8618:        nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
1.209     noro     8619:        nd_splist = 0; nd_check_splist = 0;
1.231     noro     8620:     nd_sugarweight = 0;
1.234   ! noro     8621:     nd_f4red =0;
        !          8622:     nd_rank0 = 0;
1.172     noro     8623:     for ( t = opt; t; t = NEXT(t) ) {
                   8624:         p = BDY((LIST)BDY(t));
                   8625:         key = BDY((STRING)BDY(p));
                   8626:         value = (Obj)BDY(NEXT(p));
                   8627:         if ( !strcmp(key,"gentrace") )
                   8628:             nd_gentrace = value?1:0;
                   8629:         else if ( !strcmp(key,"gensyz") )
                   8630:             nd_gensyz = value?1:0;
1.173     noro     8631:         else if ( !strcmp(key,"nora") )
                   8632:             nd_nora = value?1:0;
1.187     noro     8633:         else if ( !strcmp(key,"gbblock") ) {
1.233     noro     8634:             if ( value && OID(value) == O_LIST ) {
                   8635:                          u = BDY((LIST)value);
                   8636:               nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
                   8637:                          for ( i = 0; u; u = NEXT(u) ) {
                   8638:                                  p = BDY((LIST)BDY(u));
                   8639:                                  s = nd_gbblock[i++] = QTOS((Q)BDY(p));
                   8640:                                  nd_gbblock[i++] = s+QTOS((Q)BDY(NEXT(p)))-1;
                   8641:                          }
                   8642:                          nd_gbblock[i] = -1;
                   8643:             } else
                   8644:               nd_gbblock = 0;
1.192     noro     8645:                } else if ( !strcmp(key,"newelim") )
                   8646:             nd_newelim = value?1:0;
1.195     noro     8647:                else if ( !strcmp(key,"intersect") )
                   8648:             nd_intersect = value?1:0;
1.234   ! noro     8649:                else if ( !strcmp(key,"lf") )
        !          8650:             nd_lf = value?1:0;
1.208     noro     8651:                else if ( !strcmp(key,"trace") ) {
                   8652:            u = BDY((LIST)value);
                   8653:                   nd_nzlist = BDY((LIST)ARG2(u));
                   8654:                   nd_bpe = QTOS((Q)ARG3(u));
1.234   ! noro     8655:                } else if ( !strcmp(key,"f4red") ) {
        !          8656:                   nd_f4red = QTOS((Q)value);
        !          8657:                } else if ( !strcmp(key,"rank0") ) {
        !          8658:             nd_rank0 = value?1:0;
        !          8659:                } else if ( !strcmp(key,"splist") ) {
1.209     noro     8660:             nd_splist = value?1:0;
1.234   ! noro     8661:                } else if ( !strcmp(key,"check_splist") ) {
1.209     noro     8662:                        nd_check_splist = BDY((LIST)value);
1.231     noro     8663:                } else if ( !strcmp(key,"sugarweight") ) {
                   8664:                        u = BDY((LIST)value);
                   8665:             n = length(u);
                   8666:             nd_sugarweight = MALLOC(n*sizeof(int));
                   8667:                        for ( i = 0; i < n; i++, u = NEXT(u) )
                   8668:                 nd_sugarweight[i] = QTOS((Q)BDY(u));
1.208     noro     8669:                }
1.172     noro     8670:     }
                   8671: }
1.204     noro     8672:
                   8673: ND mdptond(DP d);
                   8674: ND nd_mul_nm(int mod,NM m0,ND p);
1.207     noro     8675: ND *btog(NODE ti,ND **p,int nb,int mod);
                   8676: ND btog_one(NODE ti,ND *p,int nb,int mod);
1.204     noro     8677: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
1.205     noro     8678: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
1.204     noro     8679:
                   8680: /* d:monomial */
                   8681: ND mdptond(DP d)
                   8682: {
                   8683:   NM m;
                   8684:   ND r;
                   8685:
                   8686:   if ( OID(d) == 1 )
                   8687:        r = ptond(CO,CO,(P)d);
                   8688:   else {
                   8689:     NEWNM(m);
                   8690:     dltondl(NV(d),BDY(d)->dl,DL(m));
                   8691:     CQ(m) = (Q)BDY(d)->c;
                   8692:     NEXT(m) = 0;
                   8693:     MKND(NV(d),m,1,r);
                   8694:   }
                   8695:     return r;
                   8696: }
                   8697:
                   8698: ND nd_mul_nm(int mod,NM m0,ND p)
                   8699: {
                   8700:   UINT *d0;
                   8701:   int c0,c1,c;
                   8702:   NM tm,mr,mr0;
                   8703:   ND r;
                   8704:
                   8705:   if ( !p ) return 0;
                   8706:   d0 = DL(m0);
                   8707:   c0 = CM(m0);
                   8708:   mr0 = 0;
                   8709:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   8710:     NEXTNM(mr0,mr);
                   8711:        c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
                   8712:        ndl_add(d0,DL(tm),DL(mr));
                   8713:   }
                   8714:   NEXT(mr) = 0;
                   8715:   MKND(NV(p),mr0,LEN(p),r);
                   8716:   return r;
                   8717: }
                   8718:
1.207     noro     8719: ND *btog(NODE ti,ND **p,int nb,int mod)
1.204     noro     8720: {
                   8721:   PGeoBucket *r;
                   8722:   int i,ci;
                   8723:   NODE t,s;
                   8724:   ND m,tp;
                   8725:   ND *pi,*rd;
                   8726:   P c;
                   8727:
                   8728:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   8729:   for ( i = 0; i < nb; i++ )
                   8730:        r[i] = create_pbucket();
                   8731:   for ( t = ti; t; t = NEXT(t) ) {
                   8732:        s = BDY((LIST)BDY(t));
                   8733:     if ( ARG0(s) ) {
                   8734:          m = mdptond((DP)ARG2(s));
                   8735:          ptomp(mod,(P)HCQ(m),&c);
                   8736:          if ( ci = ((MQ)c)->cont ) {
                   8737:            HCM(m) = ci;
                   8738:            pi = p[QTOS((Q)ARG1(s))];
                   8739:            for ( i = 0; i < nb; i++ ) {
                   8740:                  tp = nd_mul_nm(mod,BDY(m),pi[i]);
                   8741:              add_pbucket(mod,r[i],tp);
                   8742:            }
                   8743:          }
                   8744:          ci = 1;
                   8745:     } else {
                   8746:          ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   8747:          ci = invm(ci,mod);
                   8748:        }
                   8749:   }
                   8750:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   8751:   for ( i = 0; i < nb; i++ )
                   8752:        rd[i] = normalize_pbucket(mod,r[i]);
                   8753:   if ( ci != 1 )
                   8754:     for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
                   8755:    return rd;
                   8756: }
                   8757:
1.207     noro     8758: ND btog_one(NODE ti,ND *p,int nb,int mod)
1.205     noro     8759: {
                   8760:   PGeoBucket r;
1.206     noro     8761:   int i,ci,j;
1.205     noro     8762:   NODE t,s;
                   8763:   ND m,tp;
                   8764:   ND pi,rd;
                   8765:   P c;
                   8766:
                   8767:   r = create_pbucket();
                   8768:   for ( t = ti; t; t = NEXT(t) ) {
                   8769:        s = BDY((LIST)BDY(t));
                   8770:     if ( ARG0(s) ) {
                   8771:          m = mdptond((DP)ARG2(s));
                   8772:          ptomp(mod,(P)HCQ(m),&c);
                   8773:          if ( ci = ((MQ)c)->cont ) {
                   8774:            HCM(m) = ci;
1.206     noro     8775:            pi = p[j=QTOS((Q)ARG1(s))];
                   8776:                if ( !pi ) {
                   8777:                  pi = nd_load_mod(j);
                   8778:                  tp = nd_mul_nm(mod,BDY(m),pi);
                   8779:                  nd_free(pi);
                   8780:              add_pbucket(mod,r,tp);
                   8781:                } else {
                   8782:                  tp = nd_mul_nm(mod,BDY(m),pi);
                   8783:              add_pbucket(mod,r,tp);
                   8784:            }
1.205     noro     8785:          }
                   8786:          ci = 1;
                   8787:     } else {
                   8788:          ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   8789:          ci = invm(ci,mod);
                   8790:        }
                   8791:   }
                   8792:   rd = normalize_pbucket(mod,r);
1.206     noro     8793:   free_pbucket(r);
1.205     noro     8794:   if ( ci != 1 ) nd_mul_c(mod,rd,ci);
                   8795:   return rd;
                   8796: }
1.204     noro     8797:
                   8798: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
                   8799: {
                   8800:   int i,j,n,m,nb,pi0,pi1,nvar;
                   8801:   VL fv,tv,vv;
                   8802:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   8803:   ND **p;
                   8804:   ND *c;
                   8805:   ND u;
                   8806:   P inv;
                   8807:   MAT mat;
                   8808:
                   8809:   parse_nd_option(current_option);
                   8810:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   8811:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   8812:   switch ( ord->id ) {
                   8813:     case 1:
                   8814:       if ( ord->nv != nvar )
                   8815:         error("nd_check : invalid order specification");
                   8816:       break;
                   8817:     default:
                   8818:       break;
                   8819:   }
                   8820:   nd_init_ord(ord);
                   8821: #if 0
                   8822:   nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
                   8823: #else
                   8824:   nd_bpe = 32;
                   8825: #endif
                   8826:   nd_setup_parameters(nvar,0);
                   8827:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   8828:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   8829:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   8830:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   8831:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
                   8832:        j = QTOS((Q)BDY(BDY((LIST)BDY(t))));
                   8833:        if ( j > i ) i = j;
                   8834:   }
                   8835:   n = i+1;
                   8836:   nb = length(BDY(f));
                   8837:   p = (ND **)MALLOC(n*sizeof(ND *));
                   8838:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   8839:     pi = BDY((LIST)BDY(t));
                   8840:        pi0 = QTOS((Q)ARG0(pi)); pi1 = QTOS((Q)ARG1(pi));
                   8841:        p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   8842:        ptomp(mod,(P)ARG2(pi),&inv);
1.218     noro     8843:        ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
1.204     noro     8844:        u = ptond(CO,vv,(P)ONE);
                   8845:        HCM(u) = ((MQ)inv)->cont;
                   8846:        c[pi1] = u;
                   8847:   }
                   8848:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   8849:        printf("%d ",i); fflush(stdout);
                   8850:     ti = BDY((LIST)BDY(t));
1.207     noro     8851:     p[j=QTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.204     noro     8852:     if ( j == 441 )
                   8853:                printf("afo");
                   8854:   }
                   8855:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   8856:        printf("%d ",i); fflush(stdout);
                   8857:     ti = BDY((LIST)BDY(t));
1.207     noro     8858:     p[j=QTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.204     noro     8859:     if ( j == 441 )
                   8860:                printf("afo");
                   8861:   }
                   8862:   m = length(ind);
                   8863:   MKMAT(mat,nb,m);
                   8864:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
                   8865:     for ( i = 0, c = p[QTOS((Q)BDY(t))]; i < nb; i++ )
                   8866:                BDY(mat)[i][j] = ndtodp(mod,c[i]);
                   8867:   return mat;
                   8868: }
                   8869:
1.205     noro     8870: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
                   8871:   LIST tlist,int pos,MAT *rp)
                   8872: {
                   8873:   int i,j,n,m,nb,pi0,pi1,nvar;
                   8874:   VL fv,tv,vv;
                   8875:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   8876:   ND *p;
                   8877:   ND *c;
                   8878:   ND u;
                   8879:   P inv;
                   8880:   VECT vect;
                   8881:
                   8882:   parse_nd_option(current_option);
                   8883:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   8884:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   8885:   switch ( ord->id ) {
                   8886:     case 1:
                   8887:       if ( ord->nv != nvar )
                   8888:         error("nd_check : invalid order specification");
                   8889:       break;
                   8890:     default:
                   8891:       break;
                   8892:   }
                   8893:   nd_init_ord(ord);
                   8894: #if 0
                   8895:   nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
                   8896: #else
                   8897:   nd_bpe = 32;
                   8898: #endif
                   8899:   nd_setup_parameters(nvar,0);
                   8900:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   8901:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   8902:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   8903:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   8904:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
                   8905:        j = QTOS((Q)BDY(BDY((LIST)BDY(t))));
                   8906:        if ( j > i ) i = j;
                   8907:   }
                   8908:   n = i+1;
                   8909:   nb = length(BDY(f));
                   8910:   p = (ND *)MALLOC(n*sizeof(ND *));
                   8911:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   8912:     pi = BDY((LIST)BDY(t));
                   8913:        pi0 = QTOS((Q)ARG0(pi)); pi1 = QTOS((Q)ARG1(pi));
                   8914:        if ( pi1 == pos ) {
1.218     noro     8915:          ptomp(mod,(P)ARG2(pi),&inv);
                   8916:          ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
1.205     noro     8917:          u = ptond(CO,vv,(P)ONE);
                   8918:          HCM(u) = ((MQ)inv)->cont;
                   8919:          p[pi0] = u;
                   8920:     }
                   8921:   }
                   8922:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   8923:        printf("%d ",i); fflush(stdout);
                   8924:     ti = BDY((LIST)BDY(t));
1.207     noro     8925:     p[j=QTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.206     noro     8926:     if ( Demand ) {
                   8927:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   8928:        }
1.205     noro     8929:   }
                   8930:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   8931:        printf("%d ",i); fflush(stdout);
                   8932:     ti = BDY((LIST)BDY(t));
1.207     noro     8933:     p[j=QTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.206     noro     8934:     if ( Demand ) {
                   8935:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   8936:        }
1.205     noro     8937:   }
                   8938:   m = length(ind);
                   8939:   MKVECT(vect,m);
1.206     noro     8940:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
                   8941:        u = p[QTOS((Q)BDY(t))];
                   8942:        if ( !u ) {
                   8943:          u = nd_load_mod(QTOS((Q)BDY(t)));
                   8944:          BDY(vect)[j] = ndtodp(mod,u);
                   8945:          nd_free(u);
                   8946:        } else
                   8947:          BDY(vect)[j] = ndtodp(mod,u);
                   8948:   }
1.205     noro     8949:   return vect;
                   8950: }
1.232     noro     8951:
                   8952: void ndv_print_lf(NDV p)
                   8953: {
                   8954:     NMV m;
                   8955:     int i,len;
                   8956:
                   8957:     if ( !p ) printf("0\n");
                   8958:     else {
                   8959:         len = LEN(p);
                   8960:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   8961:             printf("+");
                   8962:             mpz_out_str(asir_out,10,BDY(CZ(m)));
                   8963:             printf("*");
                   8964:             ndl_print(DL(m));
                   8965:         }
                   8966:         printf("\n");
                   8967:     }
                   8968: }
1.234   ! noro     8969:
        !          8970: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
        !          8971: {
        !          8972:     VL tv,fv,vv,vc,av;
        !          8973:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
        !          8974:     int m,nocheck,nvar,mindex,e,max;
        !          8975:     NDV c;
        !          8976:     NMV a;
        !          8977:     P p,zp;
        !          8978:     Q dmy;
        !          8979:     EPOS oepos;
        !          8980:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
        !          8981:     Alg alpha,dp;
        !          8982:     P poly;
        !          8983:     LIST f1,f2,zpl;
        !          8984:     Obj obj;
        !          8985:     NumberField nf;
        !          8986:     struct order_spec *ord1;
        !          8987:     struct oEGT eg_check,eg0,eg1;
        !          8988:     NODE tr,tl1,tl2,tl3,tl4;
        !          8989:     LIST l1,l2,l3,l4,l5;
        !          8990:     int *perm;
        !          8991:     int j,ret;
        !          8992:     Q jq,bpe;
        !          8993:
        !          8994:     nd_module = 0;
        !          8995:     parse_nd_option(current_option);
        !          8996:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
        !          8997:     if ( nd_vc )
        !          8998:       error("nd_f4_lf_trace : computation over a rational function field is not implemented");
        !          8999:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
        !          9000:     switch ( ord->id ) {
        !          9001:         case 1:
        !          9002:             if ( ord->nv != nvar )
        !          9003:                 error("nd_f4_lf_trace : invalid order specification");
        !          9004:             break;
        !          9005:         default:
        !          9006:             break;
        !          9007:     }
        !          9008:
        !          9009:     nd_ntrans = nvar;
        !          9010:     nd_nalg = 0;
        !          9011:
        !          9012:     nocheck = 0;
        !          9013:     mindex = 0;
        !          9014:
        !          9015:     if ( Demand ) nd_demand = 1;
        !          9016:     else nd_demand = 0;
        !          9017:
        !          9018:     /* setup modulus */
        !          9019:     if ( trace < 0 ) {
        !          9020:         trace = -trace;
        !          9021:         nocheck = 1;
        !          9022:     }
        !          9023:     m = trace > 1 ? trace : get_lprime(mindex);
        !          9024:     nd_init_ord(ord);
        !          9025:     mrank = 0;
        !          9026:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
        !          9027:         for ( tv = vv; tv; tv = NEXT(tv) ) {
        !          9028:             if ( nd_module ) {
        !          9029:                 s = BDY((LIST)BDY(t));
        !          9030:                 trank = length(s);
        !          9031:                 mrank = MAX(mrank,trank);
        !          9032:                 for ( ; s; s = NEXT(s) ) {
        !          9033:                     e = getdeg(tv->v,(P)BDY(s));
        !          9034:                     max = MAX(e,max);
        !          9035:                 }
        !          9036:             } else {
        !          9037:                 e = getdeg(tv->v,(P)BDY(t));
        !          9038:                 max = MAX(e,max);
        !          9039:             }
        !          9040:         }
        !          9041:     nd_setup_parameters(nvar,max);
        !          9042:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
        !          9043:     ishomo = 1;
        !          9044:     /* XXX */
        !          9045:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
        !          9046:         if ( nd_module ) {
        !          9047:             c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
        !          9048:         } else {
        !          9049:             c = (pointer)ptondv(CO,vv,(P)BDY(t));
        !          9050:         }
        !          9051:         if ( ishomo )
        !          9052:             ishomo = ishomo && ndv_ishomo(c);
        !          9053:         if ( c ) {
        !          9054:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
        !          9055:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
        !          9056:         }
        !          9057:     }
        !          9058:     if ( in0 ) NEXT(in) = 0;
        !          9059:     if ( fd0 ) NEXT(fd) = 0;
        !          9060:     if ( !ishomo && homo ) {
        !          9061:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
        !          9062:             c = (NDV)BDY(t); len = LEN(c);
        !          9063:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
        !          9064:                 wmax = MAX(TD(DL(a)),wmax);
        !          9065:         }
        !          9066:         homogenize_order(ord,nvar,&ord1);
        !          9067:         nd_init_ord(ord1);
        !          9068:         nd_setup_parameters(nvar+1,wmax);
        !          9069:         for ( t = fd0; t; t = NEXT(t) )
        !          9070:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
        !          9071:     }
        !          9072:     if ( MaxDeg > 0 ) nocheck = 1;
        !          9073:     while ( 1 ) {
        !          9074:         if ( Demand )
        !          9075:             nd_demand = 1;
        !          9076:         ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0);
        !          9077:         if ( ret )
        !          9078:           cand = nd_f4_lf_trace_main(m,&perm);
        !          9079:         if ( !ret || !cand ) {
        !          9080:             /* failure */
        !          9081:             if ( trace > 1 ) { *rp = 0; return; }
        !          9082:             else m = get_lprime(++mindex);
        !          9083:             continue;
        !          9084:         }
        !          9085:         if ( !ishomo && homo ) {
        !          9086:             /* dehomogenization */
        !          9087:             for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
        !          9088:             nd_init_ord(ord);
        !          9089:             nd_setup_parameters(nvar,0);
        !          9090:         }
        !          9091:         nd_demand = 0;
        !          9092:         cand = ndv_reducebase(cand,perm);
        !          9093:         cand = ndv_reduceall(-2,cand);
        !          9094:         cbpe = nd_bpe;
        !          9095:         get_eg(&eg0);
        !          9096:         if ( nocheck )
        !          9097:             break;
        !          9098:         if ( ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand) ) {
        !          9099:             /* gbcheck : cand is a GB of Id(cand) ? */
        !          9100:             ret = nd_f4(-2,0,0);
        !          9101:                }
        !          9102:                if ( ret ) break;
        !          9103:         else if ( trace > 1 ) {
        !          9104:             /* failure */
        !          9105:             *rp = 0; return;
        !          9106:         } else {
        !          9107:             /* try the next modulus */
        !          9108:             m = get_lprime(++mindex);
        !          9109:             /* reset the parameters */
        !          9110:             if ( !ishomo && homo ) {
        !          9111:                 nd_init_ord(ord1);
        !          9112:                 nd_setup_parameters(nvar+1,wmax);
        !          9113:             } else {
        !          9114:                 nd_init_ord(ord);
        !          9115:                 nd_setup_parameters(nvar,max);
        !          9116:             }
        !          9117:         }
        !          9118:     }
        !          9119:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
        !          9120:     if ( DP_Print )
        !          9121:         fprintf(asir_out,"check=%fsec\n",eg_check.exectime+eg_check.gctime);
        !          9122:     /* dp->p */
        !          9123:     nd_bpe = cbpe;
        !          9124:     nd_setup_parameters(nd_nvar,0);
        !          9125:     for ( r = cand; r; r = NEXT(r) ) {
        !          9126:       if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
        !          9127:       else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
        !          9128:     }
        !          9129:     MKLIST(*rp,cand);
        !          9130: }
        !          9131:
        !          9132: NODE nd_f4_lf_trace_main(int m,int **indp)
        !          9133: {
        !          9134:     int i,nh,stat,index;
        !          9135:     NODE r,rm,g;
        !          9136:     ND_pairs d,l,l0,t;
        !          9137:     ND spol,red;
        !          9138:     NDV nf,redv,nfqv,nfv;
        !          9139:     NM s0,s;
        !          9140:     NODE rp0,srp0,nflist,nflist_lf;
        !          9141:     int nsp,nred,col,rank,len,k,j,a;
        !          9142:     UINT c;
        !          9143:     UINT **spmat;
        !          9144:     UINT *s0vect,*svect,*p,*v;
        !          9145:     int *colstat;
        !          9146:     IndArray *imat;
        !          9147:     int *rhead;
        !          9148:     int spcol,sprow;
        !          9149:     int sugar;
        !          9150:     PGeoBucket bucket;
        !          9151:     struct oEGT eg0,eg1,eg_f4;
        !          9152:
        !          9153:     g = 0; d = 0;
        !          9154:     for ( i = 0; i < nd_psn; i++ ) {
        !          9155:         d = update_pairs(d,g,i,0);
        !          9156:         g = update_base(g,i);
        !          9157:     }
        !          9158:     while ( d ) {
        !          9159:         get_eg(&eg0);
        !          9160:         l = nd_minsugarp(d,&d);
        !          9161:         sugar = SG(l);
        !          9162:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
        !          9163:         bucket = create_pbucket();
        !          9164:         stat = nd_sp_f4(m,0,l,bucket);
        !          9165:         if ( !stat ) {
        !          9166:             for ( t = l; NEXT(t); t = NEXT(t) );
        !          9167:             NEXT(t) = d; d = l;
        !          9168:             d = nd_reconstruct(1,d);
        !          9169:             continue;
        !          9170:         }
        !          9171:         if ( bucket->m < 0 ) continue;
        !          9172:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
        !          9173:         if ( !col ) {
        !          9174:             for ( t = l; NEXT(t); t = NEXT(t) );
        !          9175:             NEXT(t) = d; d = l;
        !          9176:             d = nd_reconstruct(1,d);
        !          9177:             continue;
        !          9178:         }
        !          9179:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
        !          9180:         if ( DP_Print )
        !          9181:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
        !          9182:                 sugar,eg_f4.exectime+eg_f4.gctime);
        !          9183:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
        !          9184:         if ( !l0 ) continue;
        !          9185:         l = l0;
        !          9186:
        !          9187:         /* over LF */
        !          9188:         bucket = create_pbucket();
        !          9189:         stat = nd_sp_f4(-2,1,l,bucket);
        !          9190:         if ( !stat ) {
        !          9191:             for ( t = l; NEXT(t); t = NEXT(t) );
        !          9192:             NEXT(t) = d; d = l;
        !          9193:             d = nd_reconstruct(1,d);
        !          9194:             continue;
        !          9195:         }
        !          9196:         if ( bucket->m < 0 ) continue;
        !          9197:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
        !          9198:         if ( !col ) {
        !          9199:             for ( t = l; NEXT(t); t = NEXT(t) );
        !          9200:             NEXT(t) = d; d = l;
        !          9201:             d = nd_reconstruct(1,d);
        !          9202:             continue;
        !          9203:         }
        !          9204:         nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
        !          9205:         /* adding new bases */
        !          9206:         for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
        !          9207:             nfv = (NDV)BDY(rm);
        !          9208:             nfqv = (NDV)BDY(r);
        !          9209:             if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
        !          9210:             ndv_removecont(m,nfv);
        !          9211:             ndv_removecont(-2,nfqv);
        !          9212:             nh = ndv_newps(-2,nfv,nfqv,1);
        !          9213:             d = update_pairs(d,g,nh,0);
        !          9214:             g = update_base(g,nh);
        !          9215:         }
        !          9216:         if ( r || rm ) return 0;
        !          9217:     }
        !          9218:        conv_ilist(nd_demand,1,g,indp);
        !          9219:     return g;
        !          9220: }
        !          9221:

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