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

1.235   ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.234 2017/02/21 09:20:23 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);
1.235   ! noro     1459:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.157     noro     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;
1.235   ! noro     1547:     mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.157     noro     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;
1.235   ! noro     2670:     lcm = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
1.157     noro     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.235   ! noro     2953:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
1.215     noro     2954:             nd_ps_trace_sym[nd_psn] = ndv_symbolic(m,nd_ps_trace[nd_psn]);
1.157     noro     2955:             nd_ps_trace[nd_psn] = 0;
                   2956:         } else {
                   2957:             ndv_save(nd_ps[nd_psn],nd_psn);
1.215     noro     2958:             nd_ps_sym[nd_psn] = ndv_symbolic(m,nd_ps[nd_psn]);
1.157     noro     2959:             nd_ps[nd_psn] = 0;
                   2960:         }
                   2961:     }
1.172     noro     2962:     if ( nd_gentrace ) {
1.167     noro     2963:         /* reverse the tracelist and append it to alltracelist */
                   2964:         nd_tracelist = reverse_node(nd_tracelist); MKLIST(l,nd_tracelist);
                   2965:         STOQ(nd_psn,iq); tn = mknode(2,iq,l); MKLIST(l,tn);
                   2966:         MKNODE(tn,l,nd_alltracelist); nd_alltracelist = tn; nd_tracelist = 0;
                   2967:     }
1.157     noro     2968:     return nd_psn++;
1.1       noro     2969: }
                   2970:
1.167     noro     2971: /* nd_tracelist = [[0,index,div],...,[nd_psn-1,index,div]] */
1.177     noro     2972: /* return 1 if success, 0 if failure (HC(a mod p)) */
1.167     noro     2973:
1.177     noro     2974: int ndv_setup(int mod,int trace,NODE f,int dont_sort,int dont_removecont)
1.1       noro     2975: {
1.157     noro     2976:     int i,j,td,len,max;
1.167     noro     2977:     NODE s,s0,f0,tn;
1.157     noro     2978:     UINT *d;
                   2979:     RHist r;
1.167     noro     2980:     NDVI w;
1.157     noro     2981:     NDV a,am;
1.167     noro     2982:     union oNDC hc;
                   2983:     NODE node;
                   2984:     P hcp;
                   2985:     Q iq,jq,hcq;
                   2986:     LIST l;
1.157     noro     2987:
                   2988:     nd_found = 0; nd_notfirst = 0; nd_create = 0;
1.167     noro     2989:     /* initialize the tracelist */
                   2990:     nd_tracelist = 0;
1.157     noro     2991:
                   2992:     for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
1.235   ! noro     2993:     w = (NDVI)MALLOC(nd_psn*sizeof(struct oNDVI));
1.167     noro     2994:     for ( i = j = 0, s = f; s; s = NEXT(s), j++ )
                   2995:         if ( BDY(s) ) { w[i].p = BDY(s); w[i].i = j; i++; }
1.157     noro     2996:     if ( !dont_sort ) {
                   2997:         /* XXX heuristic */
                   2998:         if ( !nd_ord->id && (nd_ord->ord.simple<2) )
1.167     noro     2999:             qsort(w,nd_psn,sizeof(struct oNDVI),
                   3000:                 (int (*)(const void *,const void *))ndvi_compare_rev);
1.157     noro     3001:         else
1.167     noro     3002:             qsort(w,nd_psn,sizeof(struct oNDVI),
                   3003:                 (int (*)(const void *,const void *))ndvi_compare);
1.157     noro     3004:     }
                   3005:     nd_pslen = 2*nd_psn;
                   3006:     nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.215     noro     3007:     nd_ps_gz = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.157     noro     3008:     nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.215     noro     3009:     nd_ps_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
                   3010:     nd_ps_trace_sym = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.157     noro     3011:     nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
                   3012:     nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
                   3013:     nd_hcf = 0;
                   3014:
                   3015:     if ( trace && nd_vc )
                   3016:         makesubst(nd_vc,&nd_subst);
                   3017:     else
                   3018:         nd_subst = 0;
                   3019:
                   3020:     if ( !nd_red )
                   3021:         nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   3022:     for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
                   3023:     for ( i = 0; i < nd_psn; i++ ) {
1.167     noro     3024:         hc = HCU(w[i].p);
1.157     noro     3025:         if ( trace ) {
1.234     noro     3026:             if ( mod == -2 ) {
                   3027:               /* over a large finite field */
                   3028:               /* trace = small modulus */
                   3029:               a = nd_ps_trace[i] = ndv_dup(-2,w[i].p);
                   3030:               ndv_mod(-2,a);
                   3031:               if ( !dont_removecont) ndv_removecont(-2,a);
                   3032:               am = nd_ps[i] = ndv_dup(trace,w[i].p);
                   3033:               ndv_mod(trace,am);
                   3034:                  if ( DL_COMPARE(HDL(am),HDL(a)) )
                   3035:                      return 0;
                   3036:               ndv_removecont(trace,am);
                   3037:             } else {
                   3038:               a = nd_ps_trace[i] = ndv_dup(0,w[i].p);
                   3039:                          if ( !nd_vc ) nd_ps_gz[i] = ndvtondvgz(a);
                   3040:               if ( !dont_removecont) ndv_removecont(0,a);
                   3041:               register_hcf(a);
                   3042:               am = nd_ps[i] = ndv_dup(mod,a);
                   3043:               ndv_mod(mod,am);
                   3044:                  if ( DL_COMPARE(HDL(am),HDL(a)) )
                   3045:                      return 0;
                   3046:               ndv_removecont(mod,am);
                   3047:             }
1.157     noro     3048:         } else {
1.167     noro     3049:             a = nd_ps[i] = ndv_dup(mod,w[i].p);
1.217     noro     3050:                        if ( !mod && !nd_vc ) nd_ps_gz[i] = ndvtondvgz(a);
1.157     noro     3051:             if ( mod || !dont_removecont ) ndv_removecont(mod,a);
                   3052:             if ( !mod ) register_hcf(a);
                   3053:         }
1.172     noro     3054:         if ( nd_gentrace ) {
1.167     noro     3055:             STOQ(i,iq); STOQ(w[i].i,jq); node = mknode(3,iq,jq,ONE);
1.168     noro     3056:                        if ( !dont_removecont )
                   3057:                 ARG2(node) = (pointer)ndc_div(trace?0:mod,hc,HCU(a));
1.167     noro     3058:             MKLIST(l,node); NEXTNODE(nd_tracelist,tn); BDY(tn) = l;
                   3059:         }
1.157     noro     3060:         NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
                   3061:         nd_bound[i] = ndv_compute_bound(a);
                   3062:         nd_psh[i] = r;
                   3063:         if ( nd_demand ) {
                   3064:             if ( trace ) {
                   3065:                 ndv_save(nd_ps_trace[i],i);
1.235   ! noro     3066:                 nd_ps_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
1.215     noro     3067:                 nd_ps_trace_sym[i] = ndv_symbolic(mod,nd_ps_trace[i]);
1.157     noro     3068:                 nd_ps_trace[i] = 0;
                   3069:             } else {
                   3070:                 ndv_save(nd_ps[i],i);
1.215     noro     3071:                 nd_ps_sym[i] = ndv_symbolic(mod,nd_ps[i]);
1.157     noro     3072:                 nd_ps[i] = 0;
                   3073:             }
                   3074:         }
                   3075:     }
1.172     noro     3076:     if ( nd_gentrace && nd_tracelist ) NEXT(tn) = 0;
1.177     noro     3077:     return 1;
1.20      noro     3078: }
                   3079:
1.119     noro     3080: struct order_spec *append_block(struct order_spec *spec,
                   3081:     int nv,int nalg,int ord);
                   3082:
1.121     noro     3083: extern VECT current_dl_weight_vector_obj;
                   3084: static VECT prev_weight_vector_obj;
                   3085:
1.120     noro     3086: void preprocess_algcoef(VL vv,VL av,struct order_spec *ord,LIST f,
1.157     noro     3087:     struct order_spec **ord1p,LIST *f1p,NODE *alistp)
1.120     noro     3088: {
1.157     noro     3089:     NODE alist,t,s,r0,r,arg;
                   3090:     VL tv;
                   3091:     P poly;
                   3092:     DP d;
                   3093:     Alg alpha,dp;
                   3094:     DAlg inv,da,hc;
                   3095:     MP m;
                   3096:     int i,nvar,nalg,n;
                   3097:     NumberField nf;
                   3098:     LIST f1,f2;
                   3099:     struct order_spec *current_spec;
                   3100:     VECT obj,obj0;
1.232     noro     3101:     VECT tmp;
1.157     noro     3102:
                   3103:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++);
                   3104:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++);
                   3105:
                   3106:     for ( alist = 0, tv = av; tv; tv = NEXT(tv) ) {
                   3107:         NEXTNODE(alist,t); MKV(tv->v,poly);
                   3108:         MKAlg(poly,alpha); BDY(t) = (pointer)alpha;
                   3109:         tv->v = tv->v->priv;
                   3110:     }
                   3111:     NEXT(t) = 0;
                   3112:
1.167     noro     3113:     /* simplification, making polynomials monic */
1.157     noro     3114:     setfield_dalg(alist);
1.232     noro     3115:     obj_algtodalg((Obj)f,(Obj *)&f1);
1.157     noro     3116:     for ( t = BDY(f); t; t = NEXT(t) ) {
                   3117:         initd(ord); ptod(vv,vv,(P)BDY(t),&d);
                   3118:         hc = (DAlg)BDY(d)->c;
                   3119:         if ( NID(hc) == N_DA ) {
                   3120:             invdalg(hc,&inv);
                   3121:             for ( m = BDY(d); m; m = NEXT(m) ) {
                   3122:                 muldalg(inv,(DAlg)m->c,&da); m->c = (P)da;
                   3123:             }
                   3124:         }
                   3125:         initd(ord); dtop(vv,vv,d,&poly); BDY(f) = (pointer)poly;
                   3126:     }
1.232     noro     3127:     obj_dalgtoalg((Obj)f1,(Obj *)&f);
1.157     noro     3128:
                   3129:     /* append alg vars to the var list */
                   3130:     for ( tv = vv; NEXT(tv); tv = NEXT(tv) );
                   3131:     NEXT(tv) = av;
                   3132:
                   3133:     /* append a block to ord */
                   3134:     *ord1p = append_block(ord,nvar,nalg,2);
                   3135:
                   3136:     /* create generator list */
                   3137:     nf = get_numberfield();
                   3138:     for ( i = nalg-1, t = BDY(f); i >= 0; i-- ) {
                   3139:         MKAlg(nf->defpoly[i],dp);
                   3140:         MKNODE(s,dp,t); t = s;
                   3141:     }
                   3142:     MKLIST(f1,t);
                   3143:     *alistp = alist;
1.232     noro     3144:     algobjtorat((Obj)f1,(Obj *)f1p);
1.157     noro     3145:
                   3146:     /* creating a new weight vector */
                   3147:     prev_weight_vector_obj = obj0 = current_dl_weight_vector_obj;
                   3148:     n = nvar+nalg+1;
                   3149:     MKVECT(obj,n);
                   3150:     if ( obj0 && obj0->len == nvar )
                   3151:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = BDY(obj0)[i];
                   3152:     else
                   3153:         for ( i = 0; i < nvar; i++ ) BDY(obj)[i] = (pointer)ONE;
                   3154:     for ( i = 0; i < nalg; i++ ) BDY(obj)[i+nvar] = 0;
                   3155:     BDY(obj)[n-1] = (pointer)ONE;
                   3156:     arg = mknode(1,obj);
                   3157:     Pdp_set_weight(arg,&tmp);
1.121     noro     3158: }
                   3159:
                   3160: NODE postprocess_algcoef(VL av,NODE alist,NODE r)
                   3161: {
1.157     noro     3162:     NODE s,t,u0,u;
                   3163:     P p;
                   3164:     VL tv;
1.232     noro     3165:     Obj obj;
                   3166:     VECT tmp;
1.157     noro     3167:     NODE arg;
                   3168:
                   3169:     u0 = 0;
                   3170:     for ( t = r; t; t = NEXT(t) ) {
                   3171:         p = (P)BDY(t);
                   3172:         for ( tv = av, s = alist; tv; tv = NEXT(tv), s = NEXT(s) ) {
                   3173:             substr(CO,0,(Obj)p,tv->v,(Obj)BDY(s),&obj); p = (P)obj;
                   3174:         }
                   3175:         if ( OID(p) == O_P || (OID(p) == O_N && NID((Num)p) != N_A) ) {
                   3176:             NEXTNODE(u0,u);
                   3177:             BDY(u) = (pointer)p;
                   3178:         }
                   3179:     }
                   3180:     arg = mknode(1,prev_weight_vector_obj);
                   3181:     Pdp_set_weight(arg,&tmp);
1.121     noro     3182:
1.157     noro     3183:     return u0;
1.120     noro     3184: }
                   3185:
1.199     noro     3186: void nd_gr(LIST f,LIST v,int m,int homo,int retdp,int f4,struct order_spec *ord,LIST *rp)
1.1       noro     3187: {
1.157     noro     3188:     VL tv,fv,vv,vc,av;
                   3189:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   3190:     int e,max,nvar,i;
                   3191:     NDV b;
1.184     noro     3192:     int ishomo,nalg,mrank,trank,wmax,len;
                   3193:        NMV a;
1.157     noro     3194:     Alg alpha,dp;
                   3195:     P p,zp;
                   3196:     Q dmy;
                   3197:     LIST f1,f2,zpl;
                   3198:     Obj obj;
                   3199:     NumberField nf;
                   3200:     struct order_spec *ord1;
1.208     noro     3201:     NODE tr,tl1,tl2,tl3,tl4,nzlist;
1.170     noro     3202:     LIST l1,l2,l3,l4,l5;
1.167     noro     3203:        int j;
1.198     noro     3204:        Q jq,bpe;
1.167     noro     3205:     int *perm;
1.170     noro     3206:     EPOS oepos;
1.194     noro     3207:     int obpe,oadv,ompos,cbpe;
1.1       noro     3208:
1.174     noro     3209:     nd_module = 0;
1.157     noro     3210:     if ( !m && Demand ) nd_demand = 1;
                   3211:     else nd_demand = 0;
1.172     noro     3212:     parse_nd_option(current_option);
1.78      noro     3213:
1.157     noro     3214:     if ( DP_Multiple )
                   3215:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
1.103     noro     3216: #if 0
1.157     noro     3217:     ndv_alloc = 0;
1.103     noro     3218: #endif
1.157     noro     3219:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
1.229     noro     3220:     if ( m && nd_vc )
                   3221:        error("nd_{gr,f4} : computation over Fp(X) is unsupported. Use dp_gr_mod_main().");
1.157     noro     3222:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   3223:     switch ( ord->id ) {
                   3224:         case 1:
                   3225:             if ( ord->nv != nvar )
                   3226:                 error("nd_{gr,f4} : invalid order specification");
                   3227:             break;
                   3228:         default:
                   3229:             break;
                   3230:     }
                   3231:     nd_nalg = 0;
                   3232:     av = 0;
                   3233:     if ( !m ) {
                   3234:         get_algtree((Obj)f,&av);
                   3235:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   3236:         nd_ntrans = nvar;
                   3237:         nd_nalg = nalg;
                   3238:         /* #i -> t#i */
                   3239:         if ( nalg ) {
                   3240:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   3241:             ord = ord1;
                   3242:             f = f1;
                   3243:         }
                   3244:         nvar += nalg;
                   3245:     }
                   3246:     nd_init_ord(ord);
                   3247:     mrank = 0;
1.178     noro     3248:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
1.157     noro     3249:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   3250:             if ( nd_module ) {
                   3251:                 s = BDY((LIST)BDY(t));
                   3252:                 trank = length(s);
                   3253:                 mrank = MAX(mrank,trank);
                   3254:                 for ( ; s; s = NEXT(s) ) {
                   3255:                     e = getdeg(tv->v,(P)BDY(s));
                   3256:                     max = MAX(e,max);
                   3257:                 }
                   3258:             } else {
                   3259:                 e = getdeg(tv->v,(P)BDY(t));
                   3260:                 max = MAX(e,max);
                   3261:             }
                   3262:         }
1.208     noro     3263:     nd_setup_parameters(nvar,nd_nzlist?0:max);
1.170     noro     3264:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
1.157     noro     3265:     ishomo = 1;
                   3266:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.167     noro     3267:         if ( nd_module ) {
1.172     noro     3268:                        if ( !m && !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
1.167     noro     3269:                        else zpl = (LIST)BDY(t);
1.157     noro     3270:             b = (pointer)pltondv(CO,vv,zpl);
                   3271:         } else {
1.172     noro     3272:                        if ( !m && !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
1.167     noro     3273:                        else zp = (P)BDY(t);
1.157     noro     3274:             b = (pointer)ptondv(CO,vv,zp);
1.167     noro     3275:         }
1.157     noro     3276:         if ( ishomo )
                   3277:             ishomo = ishomo && ndv_ishomo(b);
                   3278:         if ( m ) ndv_mod(m,b);
                   3279:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   3280:     }
                   3281:     if ( fd0 ) NEXT(fd) = 0;
1.184     noro     3282:
                   3283:        if ( !ishomo && homo ) {
                   3284:         for ( t = fd0, wmax = max; t; t = NEXT(t) ) {
                   3285:             b = (NDV)BDY(t); len = LEN(b);
                   3286:             for ( a = BDY(b), i = 0; i < len; i++, NMV_ADV(a) )
                   3287:                 wmax = MAX(TD(DL(a)),wmax);
                   3288:         }
                   3289:         homogenize_order(ord,nvar,&ord1);
                   3290:         nd_init_ord(ord1);
                   3291:         nd_setup_parameters(nvar+1,wmax);
                   3292:         for ( t = fd0; t; t = NEXT(t) )
                   3293:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   3294:     }
                   3295:
1.211     noro     3296:     ndv_setup(m,0,fd0,(nd_gbblock||nd_splist||nd_check_splist)?1:0,0);
1.172     noro     3297:     if ( nd_gentrace ) {
1.167     noro     3298:         MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   3299:     }
1.209     noro     3300:        if ( nd_splist ) {
                   3301:                *rp = compute_splist();
                   3302:                return;
                   3303:        }
                   3304:        if ( nd_check_splist ) {
1.214     noro     3305:         if ( f4 ) {
                   3306:             if ( check_splist_f4(m,nd_check_splist) ) *rp = (LIST)ONE;
                   3307:             else *rp = 0;
                   3308:         } else {
                   3309:             if ( check_splist(m,nd_check_splist) ) *rp = (LIST)ONE;
                   3310:             else *rp = 0;
                   3311:         }
1.209     noro     3312:                return;
                   3313:        }
1.234     noro     3314:     x = f4?nd_f4(m,0,&perm):nd_gb(m,ishomo || homo,0,0,&perm);
1.192     noro     3315:        if ( !x ) {
                   3316:                *rp = 0; return;
                   3317:        }
1.184     noro     3318:        if ( !ishomo && homo ) {
                   3319:                /* dehomogenization */
                   3320:                for ( t = x; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   3321:                nd_init_ord(ord);
                   3322:                nd_setup_parameters(nvar,0);
                   3323:        }
1.157     noro     3324:     nd_demand = 0;
1.195     noro     3325:        if ( nd_module && nd_intersect ) {
                   3326:                for ( j = nd_psn-1, x = 0; j >= 0; j-- )
                   3327:                        if ( MPOS(DL(nd_psh[j])) > 1 ) {
                   3328:                                MKNODE(xx,(pointer)j,x); x = xx;
                   3329:                        }
                   3330:          conv_ilist(nd_demand,0,x,0);
                   3331:          goto FINAL;
                   3332:        }
1.208     noro     3333:     if ( nd_gentrace  && f4 ) { nzlist = nd_alltracelist; }
1.167     noro     3334:     x = ndv_reducebase(x,perm);
1.208     noro     3335:     if ( nd_gentrace  && !f4 ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
1.157     noro     3336:     x = ndv_reduceall(m,x);
1.194     noro     3337:     cbpe = nd_bpe;
1.208     noro     3338:     if ( nd_gentrace && !f4 ) {
1.170     noro     3339:         tl2 = nd_alltracelist; nd_alltracelist = 0;
                   3340:         ndv_check_membership(m,fd0,obpe,oadv,oepos,x);
1.198     noro     3341:         tl3 = nd_alltracelist; nd_alltracelist = 0;
                   3342:         if ( nd_gensyz ) {
                   3343:                nd_gb(m,0,1,1,0);
1.170     noro     3344:             tl4 = nd_alltracelist; nd_alltracelist = 0;
                   3345:         } else tl4 = 0;
                   3346:     }
1.194     noro     3347:     nd_bpe = cbpe;
                   3348:     nd_setup_parameters(nd_nvar,0);
1.195     noro     3349: FINAL:
1.157     noro     3350:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   3351:         NEXTNODE(r0,r);
1.194     noro     3352:         if ( nd_module ) BDY(r) = ndvtopl(m,CO,vv,BDY(t),mrank);
1.199     noro     3353:         else if ( retdp ) BDY(r) = ndvtodp(m,BDY(t));
                   3354:                else BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.157     noro     3355:     }
                   3356:     if ( r0 ) NEXT(r) = 0;
1.208     noro     3357:     if ( !m && nd_nalg )
1.157     noro     3358:         r0 = postprocess_algcoef(av,alist,r0);
                   3359:     MKLIST(*rp,r0);
1.172     noro     3360:     if ( nd_gentrace ) {
1.208     noro     3361:        if ( f4 ) {
                   3362:             STOQ(16,bpe);
                   3363:             tr = mknode(4,*rp,(!ishomo&&homo)?ONE:0,BDY(nzlist),bpe); MKLIST(*rp,tr);
                   3364:         } else {
                   3365:             tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
                   3366:             tl3 = reverse_node(tl3);
                   3367:             /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   3368:             for ( t = tl2; t; t = NEXT(t) ) {
                   3369:             /* s = [i,[*,j,*,*],...] */
                   3370:                 s = BDY((LIST)BDY(t));
                   3371:                 j = perm[QTOS((Q)ARG0(s))]; STOQ(j,jq); ARG0(s) = (pointer)jq;
                   3372:                 for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
                   3373:                     j = perm[QTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOQ(j,jq);
                   3374:                     ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   3375:                 }
                   3376:             }
                   3377:             for ( j = length(x)-1, t = 0; j >= 0; j-- ) {
                   3378:                 STOQ(perm[j],jq); MKNODE(s,jq,t); t = s;
1.167     noro     3379:             }
1.208     noro     3380:             MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   3381:             MKLIST(l5,tl4);
                   3382:             STOQ(nd_bpe,bpe);
                   3383:             tr = mknode(8,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe); MKLIST(*rp,tr);
                   3384:         }
1.167     noro     3385:     }
1.103     noro     3386: #if 0
1.157     noro     3387:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     3388: #endif
1.127     noro     3389: }
                   3390:
                   3391: void nd_gr_postproc(LIST f,LIST v,int m,struct order_spec *ord,int do_check,LIST *rp)
                   3392: {
1.157     noro     3393:     VL tv,fv,vv,vc,av;
                   3394:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   3395:     int e,max,nvar,i;
                   3396:     NDV b;
                   3397:     int ishomo,nalg;
                   3398:     Alg alpha,dp;
                   3399:     P p,zp;
                   3400:     Q dmy;
                   3401:     LIST f1,f2;
                   3402:     Obj obj;
                   3403:     NumberField nf;
                   3404:     struct order_spec *ord1;
1.167     noro     3405:     int *perm;
1.157     noro     3406:
1.197     noro     3407:     parse_nd_option(current_option);
1.157     noro     3408:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   3409:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   3410:     switch ( ord->id ) {
                   3411:         case 1:
                   3412:             if ( ord->nv != nvar )
                   3413:                 error("nd_check : invalid order specification");
                   3414:             break;
                   3415:         default:
                   3416:             break;
                   3417:     }
                   3418:     nd_nalg = 0;
                   3419:     av = 0;
                   3420:     if ( !m ) {
                   3421:         get_algtree((Obj)f,&av);
                   3422:         for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   3423:         nd_ntrans = nvar;
                   3424:         nd_nalg = nalg;
                   3425:         /* #i -> t#i */
                   3426:         if ( nalg ) {
                   3427:             preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   3428:             ord = ord1;
                   3429:             f = f1;
                   3430:         }
                   3431:         nvar += nalg;
                   3432:     }
                   3433:     nd_init_ord(ord);
1.178     noro     3434:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
1.157     noro     3435:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   3436:             e = getdeg(tv->v,(P)BDY(t));
                   3437:             max = MAX(e,max);
                   3438:         }
                   3439:     nd_setup_parameters(nvar,max);
                   3440:     ishomo = 1;
                   3441:     for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   3442:         ptozp((P)BDY(t),1,&dmy,&zp);
                   3443:         b = (pointer)ptondv(CO,vv,zp);
                   3444:         if ( ishomo )
                   3445:             ishomo = ishomo && ndv_ishomo(b);
                   3446:         if ( m ) ndv_mod(m,b);
                   3447:         if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
                   3448:     }
                   3449:     if ( fd0 ) NEXT(fd) = 0;
                   3450:     ndv_setup(m,0,fd0,0,1);
                   3451:     for ( x = 0, i = 0; i < nd_psn; i++ )
                   3452:         x = update_base(x,i);
                   3453:     if ( do_check ) {
1.168     noro     3454:         x = nd_gb(m,ishomo,1,0,&perm);
1.157     noro     3455:         if ( !x ) {
                   3456:             *rp = 0;
                   3457:             return;
                   3458:         }
                   3459:     } else {
1.175     noro     3460: #if 0
                   3461:                /* bug ? */
1.157     noro     3462:         for ( t = x; t; t = NEXT(t) )
1.159     noro     3463:             BDY(t) = (pointer)nd_ps[(long)BDY(t)];
1.175     noro     3464: #else
                   3465:                conv_ilist(0,0,x,&perm);
                   3466: #endif
1.157     noro     3467:     }
1.167     noro     3468:     x = ndv_reducebase(x,perm);
1.157     noro     3469:     x = ndv_reduceall(m,x);
                   3470:     for ( r0 = 0, t = x; t; t = NEXT(t) ) {
                   3471:         NEXTNODE(r0,r);
                   3472:         BDY(r) = ndvtop(m,CO,vv,BDY(t));
                   3473:     }
                   3474:     if ( r0 ) NEXT(r) = 0;
1.208     noro     3475:     if ( !m && nd_nalg )
1.157     noro     3476:         r0 = postprocess_algcoef(av,alist,r0);
                   3477:     MKLIST(*rp,r0);
1.20      noro     3478: }
                   3479:
1.198     noro     3480: NDV recompute_trace(NODE trace,NDV *p,int m);
                   3481: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp);
                   3482:
                   3483: NDV recompute_trace(NODE ti,NDV *p,int mod)
                   3484: {
                   3485:        int c,c1,c2,i;
                   3486:        NM mul,m,tail;
                   3487:        ND d,r,rm;
                   3488:        NODE sj;
                   3489:        NDV red;
                   3490:        Obj mj;
                   3491:        static int afo=0;
                   3492:
                   3493:        afo++;
1.235   ! noro     3494:        mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.198     noro     3495:        CM(mul) = 1;
                   3496:        tail = 0;
                   3497:        for ( i = 0, d = r = 0; ti; ti = NEXT(ti), i++ ) {
                   3498:                sj = BDY((LIST)BDY(ti));
                   3499:                if ( ARG0(sj) ) {
                   3500:                        red = p[QTOS((Q)ARG1(sj))];
                   3501:                        mj = (Obj)ARG2(sj);
                   3502:                        if ( OID(mj) != O_DP ) ndl_zero(DL(mul));
                   3503:                        else dltondl(nd_nvar,BDY((DP)mj)->dl,DL(mul));
                   3504:                        rm = ndv_mul_nm(mod,mul,red);
                   3505:                        if ( !r ) r = rm;
                   3506:                        else {
                   3507:                                for ( m = BDY(r); m && !ndl_equal(m->dl,BDY(rm)->dl); m = NEXT(m), LEN(r)-- ) {
                   3508:                                        if ( d ) {
                   3509:                                                NEXT(tail) = m; tail = m; LEN(d)++;
                   3510:                                        } else {
                   3511:                                                MKND(nd_nvar,m,1,d); tail = BDY(d);
                   3512:                                        }
                   3513:                                }
                   3514:                                if ( !m ) return 0; /* failure */
1.232     noro     3515:                 else {
1.198     noro     3516:                                        BDY(r) = m;
1.232     noro     3517:                     if ( mod > 0 || mod == -1 ) {
                   3518:                                          c1 = invm(HCM(rm),mod); c2 = mod-HCM(r);
                   3519:                                          DMAR(c1,c2,0,mod,c);
                   3520:                                          nd_mul_c(mod,rm,c);
                   3521:                     } else {
                   3522:                       GZ t,u;
                   3523:
                   3524:                       chsgnlf(HCZ(r),&t);
                   3525:                       divlf(t,HCZ(rm),&u);
                   3526:                       nd_mul_c_lf(rm,u);
                   3527:                     }
1.198     noro     3528:                                        r = nd_add(mod,r,rm);
1.232     noro     3529:                          }
                   3530:           }
                   3531:           }
1.198     noro     3532:        }
                   3533:        if ( tail ) NEXT(tail) = 0;
                   3534:        d = nd_add(mod,d,r);
                   3535:        nd_mul_c(mod,d,invm(HCM(d),mod));
                   3536:        return ndtondv(mod,d);
                   3537: }
                   3538:
                   3539: void nd_gr_recompute_trace(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,LIST *rp)
                   3540: {
                   3541:     VL tv,fv,vv,vc,av;
                   3542:     NODE fd,fd0,r,r0,t,x,s,xx,alist;
                   3543:     int e,max,nvar,i;
                   3544:     NDV b;
                   3545:     int ishomo,nalg;
                   3546:     Alg alpha,dp;
                   3547:     P p,zp;
                   3548:     Q dmy;
                   3549:     LIST f1,f2;
                   3550:     Obj obj;
                   3551:     NumberField nf;
                   3552:     struct order_spec *ord1;
                   3553:        NODE permtrace,intred,ind,perm,trace,ti;
                   3554:        int len,n,j;
                   3555:        NDV *db,*pb;
                   3556:
                   3557:     parse_nd_option(current_option);
                   3558:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   3559:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   3560:     switch ( ord->id ) {
                   3561:         case 1:
                   3562:             if ( ord->nv != nvar )
                   3563:                 error("nd_check : invalid order specification");
                   3564:             break;
                   3565:         default:
                   3566:             break;
                   3567:     }
                   3568:     nd_init_ord(ord);
                   3569:        nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
                   3570:     nd_setup_parameters(nvar,0);
                   3571:
                   3572:        len = length(BDY(f));
                   3573:        db = (NDV *)MALLOC(len*sizeof(NDV *));
                   3574:        for ( i = 0, t = BDY(f); t; i++, t = NEXT(t) ) {
                   3575:            ptozp((P)BDY(t),1,&dmy,&zp);
                   3576:            b = ptondv(CO,vv,zp);
                   3577:         ndv_mod(m,b);
                   3578:                ndv_mul_c(m,b,invm(HCM(b),m));
                   3579:                db[i] = b;
                   3580:     }
                   3581:
                   3582:        permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   3583:        intred = BDY((LIST)ARG3(BDY(tlist)));
                   3584:        ind = BDY((LIST)ARG4(BDY(tlist)));
                   3585:        perm = BDY((LIST)ARG0(permtrace));
                   3586:        trace = NEXT(permtrace);
                   3587:
                   3588:        for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
                   3589:                j = QTOS((Q)ARG0(BDY((LIST)BDY(t))));
                   3590:                if ( j > i ) i = j;
                   3591:        }
                   3592:        n = i+1;
                   3593:        pb = (NDV *)MALLOC(n*sizeof(NDV *));
                   3594:        for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   3595:                ti = BDY((LIST)BDY(t));
                   3596:                pb[QTOS((Q)ARG0(ti))] = db[QTOS((Q)ARG1(ti))];
                   3597:        }
                   3598:        for ( t = trace; t; t = NEXT(t) ) {
                   3599:                ti = BDY((LIST)BDY(t));
                   3600:                pb[QTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   3601:                if ( !pb[QTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
                   3602:        if ( DP_Print ) {
                   3603:                        fprintf(asir_out,"."); fflush(asir_out);
                   3604:        }
                   3605:        }
                   3606:        for ( t = intred; t; t = NEXT(t) ) {
                   3607:                ti = BDY((LIST)BDY(t));
                   3608:                pb[QTOS((Q)ARG0(ti))] = recompute_trace(BDY((LIST)ARG1(ti)),pb,m);
                   3609:                if ( !pb[QTOS((Q)ARG0(ti))] ) { *rp = 0; return; }
                   3610:        if ( DP_Print ) {
                   3611:                        fprintf(asir_out,"*"); fflush(asir_out);
                   3612:        }
                   3613:        }
                   3614:     for ( r0 = 0, t = ind; t; t = NEXT(t) ) {
                   3615:         NEXTNODE(r0,r);
                   3616:                b = pb[QTOS((Q)BDY(t))];
                   3617:         ndv_mul_c(m,b,invm(HCM(b),m));
                   3618: #if 0
                   3619:         BDY(r) = ndvtop(m,CO,vv,pb[QTOS((Q)BDY(t))]);
                   3620: #else
                   3621:         BDY(r) = ndvtodp(m,pb[QTOS((Q)BDY(t))]);
                   3622: #endif
                   3623:     }
                   3624:     if ( r0 ) NEXT(r) = 0;
                   3625:     MKLIST(*rp,r0);
                   3626:     if ( DP_Print ) fprintf(asir_out,"\n");
                   3627: }
                   3628:
1.133     noro     3629: void nd_gr_trace(LIST f,LIST v,int trace,int homo,int f4,struct order_spec *ord,LIST *rp)
1.20      noro     3630: {
1.157     noro     3631:     VL tv,fv,vv,vc,av;
                   3632:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   3633:     int m,nocheck,nvar,mindex,e,max;
                   3634:     NDV c;
                   3635:     NMV a;
                   3636:     P p,zp;
                   3637:     Q dmy;
                   3638:     EPOS oepos;
1.164     noro     3639:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
1.157     noro     3640:     Alg alpha,dp;
                   3641:     P poly;
1.158     noro     3642:     LIST f1,f2,zpl;
1.157     noro     3643:     Obj obj;
                   3644:     NumberField nf;
                   3645:     struct order_spec *ord1;
                   3646:     struct oEGT eg_check,eg0,eg1;
1.168     noro     3647:     NODE tr,tl1,tl2,tl3,tl4;
                   3648:     LIST l1,l2,l3,l4,l5;
1.167     noro     3649:     int *perm;
1.168     noro     3650:     int j,ret;
1.198     noro     3651:     Q jq,bpe;
1.157     noro     3652:
1.167     noro     3653:     nd_module = 0;
1.235   ! noro     3654:     nd_lf = 0;
1.172     noro     3655:     parse_nd_option(current_option);
1.234     noro     3656:     if ( nd_lf ) {
                   3657:       if ( f4 )
                   3658:         nd_f4_lf_trace(f,v,trace,homo,ord,rp);
                   3659:       else
                   3660:         error("nd_gr_trace is not implemented yet over a large finite field");
                   3661:       return;
                   3662:     }
1.157     noro     3663:     if ( DP_Multiple )
                   3664:         nd_scale = ((double)DP_Multiple)/(double)(Denominator?Denominator:1);
                   3665:
                   3666:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   3667:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   3668:     switch ( ord->id ) {
                   3669:         case 1:
                   3670:             if ( ord->nv != nvar )
                   3671:                 error("nd_gr_trace : invalid order specification");
                   3672:             break;
                   3673:         default:
                   3674:             break;
                   3675:     }
                   3676:
                   3677:     get_algtree((Obj)f,&av);
                   3678:     for ( nalg = 0, tv = av; tv; tv = NEXT(tv), nalg++ );
                   3679:     nd_ntrans = nvar;
                   3680:     nd_nalg = nalg;
                   3681:     /* #i -> t#i */
                   3682:     if ( nalg ) {
                   3683:         preprocess_algcoef(vv,av,ord,f,&ord1,&f1,&alist);
                   3684:         ord = ord1;
                   3685:         f = f1;
                   3686:     }
                   3687:     nvar += nalg;
                   3688:
                   3689:     nocheck = 0;
                   3690:     mindex = 0;
                   3691:
                   3692:     if ( Demand ) nd_demand = 1;
                   3693:     else nd_demand = 0;
                   3694:
                   3695:     /* setup modulus */
                   3696:     if ( trace < 0 ) {
                   3697:         trace = -trace;
                   3698:         nocheck = 1;
                   3699:     }
                   3700:     m = trace > 1 ? trace : get_lprime(mindex);
1.158     noro     3701:     nd_init_ord(ord);
                   3702:     mrank = 0;
1.178     noro     3703:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
1.157     noro     3704:         for ( tv = vv; tv; tv = NEXT(tv) ) {
1.158     noro     3705:             if ( nd_module ) {
                   3706:                 s = BDY((LIST)BDY(t));
                   3707:                 trank = length(s);
                   3708:                 mrank = MAX(mrank,trank);
                   3709:                 for ( ; s; s = NEXT(s) ) {
                   3710:                     e = getdeg(tv->v,(P)BDY(s));
                   3711:                     max = MAX(e,max);
                   3712:                 }
                   3713:             } else {
                   3714:                 e = getdeg(tv->v,(P)BDY(t));
                   3715:                 max = MAX(e,max);
                   3716:             }
1.157     noro     3717:         }
                   3718:     nd_setup_parameters(nvar,max);
1.164     noro     3719:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
1.157     noro     3720:     ishomo = 1;
                   3721:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.167     noro     3722:         if ( nd_module ) {
1.172     noro     3723:                        if ( !nd_gentrace ) pltozpl((LIST)BDY(t),&dmy,&zpl);
1.167     noro     3724:                        else zpl = (LIST)BDY(t);
1.158     noro     3725:             c = (pointer)pltondv(CO,vv,zpl);
                   3726:         } else {
1.172     noro     3727:                        if ( !nd_gentrace ) ptozp((P)BDY(t),1,&dmy,&zp);
1.167     noro     3728:                        else zp = (P)BDY(t);
1.158     noro     3729:             c = (pointer)ptondv(CO,vv,zp);
1.167     noro     3730:         }
1.157     noro     3731:         if ( ishomo )
                   3732:             ishomo = ishomo && ndv_ishomo(c);
                   3733:         if ( c ) {
                   3734:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   3735:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   3736:         }
                   3737:     }
                   3738:     if ( in0 ) NEXT(in) = 0;
                   3739:     if ( fd0 ) NEXT(fd) = 0;
                   3740:     if ( !ishomo && homo ) {
                   3741:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   3742:             c = (NDV)BDY(t); len = LEN(c);
                   3743:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   3744:                 wmax = MAX(TD(DL(a)),wmax);
                   3745:         }
                   3746:         homogenize_order(ord,nvar,&ord1);
                   3747:         nd_init_ord(ord1);
                   3748:         nd_setup_parameters(nvar+1,wmax);
                   3749:         for ( t = fd0; t; t = NEXT(t) )
1.164     noro     3750:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
1.157     noro     3751:     }
1.228     noro     3752:     if ( MaxDeg > 0 ) nocheck = 1;
1.157     noro     3753:     while ( 1 ) {
1.183     noro     3754:                tl1 = tl2 = tl3 = tl4 = 0;
1.157     noro     3755:         if ( Demand )
                   3756:             nd_demand = 1;
1.187     noro     3757:         ret = ndv_setup(m,1,fd0,nd_gbblock?1:0,0);
1.172     noro     3758:         if ( nd_gentrace ) {
1.167     noro     3759:             MKLIST(l1,nd_tracelist); MKNODE(nd_alltracelist,l1,0);
                   3760:         }
1.177     noro     3761:         if ( ret )
                   3762:             cand = f4?nd_f4_trace(m,&perm):nd_gb_trace(m,ishomo || homo,&perm);
                   3763:         if ( !ret || !cand ) {
1.157     noro     3764:             /* failure */
                   3765:             if ( trace > 1 ) { *rp = 0; return; }
                   3766:             else m = get_lprime(++mindex);
                   3767:             continue;
                   3768:         }
                   3769:         if ( !ishomo && homo ) {
                   3770:             /* dehomogenization */
                   3771:             for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
                   3772:             nd_init_ord(ord);
                   3773:             nd_setup_parameters(nvar,0);
                   3774:         }
                   3775:         nd_demand = 0;
1.167     noro     3776:         cand = ndv_reducebase(cand,perm);
1.172     noro     3777:         if ( nd_gentrace ) { tl1 = nd_alltracelist; nd_alltracelist = 0; }
1.157     noro     3778:         cand = ndv_reduceall(0,cand);
                   3779:         cbpe = nd_bpe;
1.172     noro     3780:         if ( nd_gentrace ) { tl2 = nd_alltracelist; nd_alltracelist = 0; }
1.173     noro     3781:         get_eg(&eg0);
1.157     noro     3782:         if ( nocheck )
                   3783:             break;
1.170     noro     3784:         if ( ret = ndv_check_membership(0,in0,obpe,oadv,oepos,cand) ) {
1.172     noro     3785:             if ( nd_gentrace ) {
1.168     noro     3786:                            tl3 = nd_alltracelist; nd_alltracelist = 0;
                   3787:                    } else tl3 = 0;
                   3788:             /* gbcheck : cand is a GB of Id(cand) ? */
1.234     noro     3789:             if ( nd_vc || nd_gentrace || nd_gensyz )
                   3790:               ret = nd_gb(0,0,1,nd_gensyz?1:0,0)!=0;
                   3791:             else
                   3792:               ret = nd_f4(0,1,0)!=0;
1.172     noro     3793:             if ( nd_gentrace && nd_gensyz ) {
1.168     noro     3794:                            tl4 = nd_alltracelist; nd_alltracelist = 0;
                   3795:                    } else tl4 = 0;
                   3796:                }
                   3797:                if ( ret ) break;
1.157     noro     3798:         else if ( trace > 1 ) {
                   3799:             /* failure */
                   3800:             *rp = 0; return;
                   3801:         } else {
                   3802:             /* try the next modulus */
                   3803:             m = get_lprime(++mindex);
                   3804:             /* reset the parameters */
                   3805:             if ( !ishomo && homo ) {
                   3806:                 nd_init_ord(ord1);
                   3807:                 nd_setup_parameters(nvar+1,wmax);
                   3808:             } else {
                   3809:                 nd_init_ord(ord);
                   3810:                 nd_setup_parameters(nvar,max);
                   3811:             }
                   3812:         }
                   3813:     }
                   3814:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   3815:     if ( DP_Print )
                   3816:         fprintf(asir_out,"check=%fsec\n",eg_check.exectime+eg_check.gctime);
                   3817:     /* dp->p */
                   3818:     nd_bpe = cbpe;
                   3819:     nd_setup_parameters(nd_nvar,0);
1.158     noro     3820:     for ( r = cand; r; r = NEXT(r) ) {
1.167     noro     3821:     if ( nd_module ) BDY(r) = ndvtopl(0,CO,vv,BDY(r),mrank);
1.158     noro     3822:         else BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
                   3823:     }
1.208     noro     3824:     if ( nd_nalg )
1.157     noro     3825:         cand = postprocess_algcoef(av,alist,cand);
                   3826:     MKLIST(*rp,cand);
1.172     noro     3827:     if ( nd_gentrace ) {
1.167     noro     3828:         tl1 = reverse_node(tl1); tl2 = reverse_node(tl2);
1.168     noro     3829:                tl3 = reverse_node(tl3);
1.167     noro     3830:                /* tl2 = [[i,[[*,j,*,*],...]],...] */
                   3831:         for ( t = tl2; t; t = NEXT(t) ) {
                   3832:                        /* s = [i,[*,j,*,*],...] */
                   3833:             s = BDY((LIST)BDY(t));
                   3834:             j = perm[QTOS((Q)ARG0(s))]; STOQ(j,jq); ARG0(s) = (pointer)jq;
                   3835:                        for ( s = BDY((LIST)ARG1(s)); s; s = NEXT(s) ) {
                   3836:                 j = perm[QTOS((Q)ARG1(BDY((LIST)BDY(s))))]; STOQ(j,jq);
                   3837:                                ARG1(BDY((LIST)BDY(s))) = (pointer)jq;
                   3838:             }
                   3839:                }
                   3840:                for ( j = length(cand)-1, t = 0; j >= 0; j-- ) {
                   3841:                    STOQ(perm[j],jq); MKNODE(s,jq,t); t = s;
                   3842:                }
1.168     noro     3843:         MKLIST(l1,tl1); MKLIST(l2,tl2); MKLIST(l3,t); MKLIST(l4,tl3);
                   3844:                MKLIST(l5,tl4);
1.198     noro     3845:                STOQ(nd_bpe,bpe);
                   3846:         tr = mknode(8,*rp,(!ishomo&&homo)?ONE:0,l1,l2,l3,l4,l5,bpe); MKLIST(*rp,tr);
1.167     noro     3847:     }
1.157     noro     3848: }
1.52      noro     3849:
1.157     noro     3850: /* XXX : module element is not considered  */
1.1       noro     3851:
1.61      noro     3852: void dltondl(int n,DL dl,UINT *r)
1.1       noro     3853: {
1.157     noro     3854:     UINT *d;
                   3855:     int i,j,l,s,ord_l;
                   3856:     struct order_pair *op;
                   3857:
                   3858:     d = dl->d;
                   3859:     for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
                   3860:     if ( nd_blockmask ) {
                   3861:         l = nd_blockmask->n;
                   3862:         op = nd_blockmask->order_pair;
                   3863:         for ( j = 0, s = 0; j < l; j++ ) {
                   3864:             ord_l = op[j].length;
                   3865:             for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
                   3866:         }
                   3867:         TD(r) = ndl_weight(r);
                   3868:         ndl_weight_mask(r);
                   3869:     } else {
                   3870:         for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
                   3871:         TD(r) = ndl_weight(r);
                   3872:     }
1.1       noro     3873: }
                   3874:
1.61      noro     3875: DL ndltodl(int n,UINT *ndl)
1.1       noro     3876: {
1.157     noro     3877:     DL dl;
                   3878:     int *d;
                   3879:     int i,j,l,s,ord_l;
                   3880:     struct order_pair *op;
                   3881:
                   3882:     NEWDL(dl,n);
                   3883:     dl->td = TD(ndl);
                   3884:     d = dl->d;
                   3885:     if ( nd_blockmask ) {
                   3886:         l = nd_blockmask->n;
                   3887:         op = nd_blockmask->order_pair;
                   3888:         for ( j = 0, s = 0; j < l; j++ ) {
                   3889:             ord_l = op[j].length;
                   3890:             for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
                   3891:         }
                   3892:     } else {
                   3893:         for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
                   3894:     }
                   3895:     return dl;
1.1       noro     3896: }
                   3897:
1.167     noro     3898: void nmtodp(int mod,NM m,DP *r)
                   3899: {
                   3900:     DP dp;
                   3901:     MP mr;
                   3902:
                   3903:     NEWMP(mr);
                   3904:     mr->dl = ndltodl(nd_nvar,DL(m));
                   3905:     mr->c = ndctop(mod,m->c);
                   3906:     NEXT(mr) = 0; MKDP(nd_nvar,mr,dp); dp->sugar = mr->dl->td;
                   3907:     *r = dp;
                   3908: }
                   3909:
1.61      noro     3910: void ndl_print(UINT *dl)
1.1       noro     3911: {
1.157     noro     3912:     int n;
                   3913:     int i,j,l,ord_l,s,s0;
                   3914:     struct order_pair *op;
                   3915:
                   3916:     n = nd_nvar;
                   3917:     printf("<<");
                   3918:     if ( nd_blockmask ) {
                   3919:         l = nd_blockmask->n;
                   3920:         op = nd_blockmask->order_pair;
                   3921:         for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   3922:             ord_l = op[j].length;
                   3923:             for ( i = 0; i < ord_l; i++, s++ )
                   3924:                 printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
                   3925:         }
                   3926:     } else {
                   3927:         for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
                   3928:     }
                   3929:     printf(">>");
                   3930:     if ( MPOS(dl) )
                   3931:         printf("*e%d",MPOS(dl));
1.1       noro     3932: }
                   3933:
                   3934: void nd_print(ND p)
                   3935: {
1.157     noro     3936:     NM m;
1.1       noro     3937:
1.157     noro     3938:     if ( !p )
                   3939:         printf("0\n");
                   3940:     else {
                   3941:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   3942:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   3943:             else printf("+%d*",CM(m));
                   3944:             ndl_print(DL(m));
                   3945:         }
                   3946:         printf("\n");
                   3947:     }
1.1       noro     3948: }
                   3949:
1.113     noro     3950: void nd_print_q(ND p)
1.16      noro     3951: {
1.157     noro     3952:     NM m;
1.16      noro     3953:
1.157     noro     3954:     if ( !p )
                   3955:         printf("0\n");
                   3956:     else {
                   3957:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   3958:             printf("+");
                   3959:             printexpr(CO,(Obj)CQ(m));
                   3960:             printf("*");
                   3961:             ndl_print(DL(m));
                   3962:         }
                   3963:         printf("\n");
                   3964:     }
1.16      noro     3965: }
                   3966:
1.1       noro     3967: void ndp_print(ND_pairs d)
                   3968: {
1.157     noro     3969:     ND_pairs t;
1.1       noro     3970:
1.157     noro     3971:     for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
                   3972:     printf("\n");
1.1       noro     3973: }
                   3974:
1.20      noro     3975: void nd_removecont(int mod,ND p)
1.16      noro     3976: {
1.157     noro     3977:     int i,n;
                   3978:     Q *w;
                   3979:     Q dvr,t;
                   3980:     NM m;
                   3981:     struct oVECT v;
                   3982:     N q,r;
                   3983:
                   3984:     if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
1.232     noro     3985:     else if ( mod == -2 ) {
                   3986:       GZ inv;
                   3987:       divlf(ONEGZ,HCZ(p),&inv);
                   3988:       nd_mul_c_lf(p,inv);
                   3989:     } else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
1.157     noro     3990:     else {
                   3991:         for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
1.235   ! noro     3992:         w = (Q *)MALLOC(n*sizeof(Q));
1.157     noro     3993:         v.len = n;
                   3994:         v.body = (pointer *)w;
                   3995:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
                   3996:         removecont_array((P *)w,n,1);
                   3997:         for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
                   3998:     }
1.16      noro     3999: }
                   4000:
1.21      noro     4001: void nd_removecont2(ND p1,ND p2)
                   4002: {
1.157     noro     4003:     int i,n1,n2,n;
                   4004:     Q *w;
                   4005:     Q dvr,t;
                   4006:     NM m;
                   4007:     struct oVECT v;
                   4008:     N q,r;
                   4009:
                   4010:     n1 = nd_length(p1);
                   4011:     n2 = nd_length(p2);
                   4012:     n = n1+n2;
1.235   ! noro     4013:     w = (Q *)MALLOC(n*sizeof(Q));
1.157     noro     4014:     v.len = n;
                   4015:     v.body = (pointer *)w;
                   4016:     i = 0;
                   4017:     if ( p1 )
                   4018:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) w[i] = CQ(m);
                   4019:     if ( p2 )
                   4020:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
                   4021:     removecont_array((P *)w,n,1);
                   4022:     i = 0;
                   4023:     if ( p1 )
                   4024:         for ( m = BDY(p1); i < n1; m = NEXT(m), i++ ) CQ(m) = w[i];
                   4025:     if ( p2 )
                   4026:         for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
1.21      noro     4027: }
                   4028:
1.20      noro     4029: void ndv_removecont(int mod,NDV p)
1.16      noro     4030: {
1.157     noro     4031:     int i,len,all_p;
                   4032:     Q *c;
                   4033:     P *w;
                   4034:     Q dvr,t;
                   4035:     P g,cont,tp;
                   4036:     NMV m;
                   4037:
                   4038:     if ( mod == -1 )
                   4039:         ndv_mul_c(mod,p,_invsf(HCM(p)));
1.232     noro     4040:     else if ( mod == -2 ) {
                   4041:       GZ inv;
                   4042:       divlf(ONEGZ,HCZ(p),&inv);
                   4043:       ndv_mul_c_lf(p,inv);
                   4044:     } else if ( mod )
1.157     noro     4045:         ndv_mul_c(mod,p,invm(HCM(p),mod));
                   4046:     else {
                   4047:         len = p->len;
1.235   ! noro     4048:         w = (P *)MALLOC(len*sizeof(P));
        !          4049:         c = (Q *)MALLOC(len*sizeof(Q));
1.157     noro     4050:         for ( m = BDY(p), all_p = 1, i = 0; i < len; NMV_ADV(m), i++ ) {
                   4051:             ptozp(CP(m),1,&c[i],&w[i]);
                   4052:             all_p = all_p && !NUM(w[i]);
                   4053:         }
                   4054:         if ( all_p ) {
                   4055:             qltozl(c,len,&dvr); nd_heu_nezgcdnpz(nd_vc,w,len,1,&g);
                   4056:             mulp(nd_vc,(P)dvr,g,&cont);
                   4057:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   4058:                 divsp(nd_vc,CP(m),cont,&tp); CP(m) = tp;
                   4059:             }
                   4060:         } else {
                   4061:             sortbynm((Q *)c,len);
                   4062:             qltozl((Q *)c,len,&dvr);
                   4063:             for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   4064:                 divsp(nd_vc,CP(m),(P)dvr,&tp); CP(m) = tp;
                   4065:             }
                   4066:         }
                   4067:     }
1.21      noro     4068: }
                   4069:
1.157     noro     4070: /* koko */
                   4071:
1.164     noro     4072: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos,int ompos)
1.61      noro     4073: {
1.157     noro     4074:     int len,i,max;
                   4075:     NMV m,mr0,mr,t;
1.61      noro     4076:
1.157     noro     4077:     len = p->len;
1.178     noro     4078:     for ( m = BDY(p), i = 0, max = 1; i < len; NMV_OADV(m), i++ )
1.157     noro     4079:         max = MAX(max,TD(DL(m)));
                   4080:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   4081:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   4082:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
1.235   ! noro     4083:     t = (NMV)MALLOC(nmv_adv);
1.157     noro     4084:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.164     noro     4085:         ndl_homogenize(DL(m),DL(t),obpe,oepos,ompos,max);
1.157     noro     4086:         CQ(mr) = CQ(m);
                   4087:         ndl_copy(DL(t),DL(mr));
                   4088:     }
                   4089:     NV(p)++;
                   4090:     BDY(p) = mr0;
1.61      noro     4091: }
                   4092:
1.45      noro     4093: void ndv_dehomogenize(NDV p,struct order_spec *ord)
1.23      noro     4094: {
1.164     noro     4095:     int i,j,adj,len,newnvar,newwpd,newadv,newexporigin,newmpos;
1.167     noro     4096:     int pos;
1.157     noro     4097:     Q *w;
                   4098:     Q dvr,t;
                   4099:     NMV m,r;
                   4100:
                   4101:     len = p->len;
                   4102:     newnvar = nd_nvar-1;
                   4103:     newexporigin = nd_get_exporigin(ord);
1.167     noro     4104:     if ( nd_module ) newmpos = newexporigin-1;
1.157     noro     4105:     newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
                   4106:     for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
                   4107:         ndl_dehomogenize(DL(m));
                   4108:     if ( newwpd != nd_wpd ) {
                   4109:         newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
                   4110:         for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
                   4111:             CQ(r) = CQ(m);
1.167     noro     4112:             if ( nd_module ) pos = MPOS(DL(m));
1.157     noro     4113:             for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   4114:             adj = nd_exporigin-newexporigin;
                   4115:             for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
1.167     noro     4116:             if ( nd_module ) {
                   4117:                 DL(r)[newmpos] = pos;
                   4118:             }
1.157     noro     4119:         }
                   4120:     }
                   4121:     NV(p)--;
1.23      noro     4122: }
                   4123:
1.150     noro     4124: void nd_heu_nezgcdnpz(VL vl,P *pl,int m,int full,P *pr)
                   4125: {
1.157     noro     4126:     int i;
                   4127:     P *tpl,*tpl1;
                   4128:     NODE l;
                   4129:     P h,gcd,t;
                   4130:
1.235   ! noro     4131:     tpl = (P *)MALLOC(m*sizeof(P));
        !          4132:     tpl1 = (P *)MALLOC(m*sizeof(P));
1.157     noro     4133:     bcopy(pl,tpl,m*sizeof(P));
                   4134:     gcd = (P)ONE;
                   4135:     for ( l = nd_hcf; l; l = NEXT(l) ) {
                   4136:         h = (P)BDY(l);
                   4137:         while ( 1 ) {
                   4138:             for ( i = 0; i < m; i++ )
                   4139:                 if ( !divtpz(vl,tpl[i],h,&tpl1[i]) )
                   4140:                     break;
                   4141:             if ( i == m ) {
                   4142:                 bcopy(tpl1,tpl,m*sizeof(P));
                   4143:                 mulp(vl,gcd,h,&t); gcd = t;
                   4144:             } else
                   4145:                 break;
                   4146:         }
                   4147:     }
                   4148:     if ( DP_Print > 2 ){fprintf(asir_out,"[%d]",nmonop(gcd)); fflush(asir_out);}
                   4149:     if ( full ) {
                   4150:         heu_nezgcdnpz(vl,tpl,m,&t);
                   4151:         mulp(vl,gcd,t,pr);
                   4152:     } else
                   4153:         *pr = gcd;
1.150     noro     4154: }
                   4155:
                   4156: void removecont_array(P *p,int n,int full)
1.146     noro     4157: {
1.157     noro     4158:     int all_p,all_q,i;
                   4159:     Q *c;
                   4160:     P *w;
                   4161:     P t,s;
                   4162:
                   4163:     for ( all_q = 1, i = 0; i < n; i++ )
                   4164:         all_q = all_q && NUM(p[i]);
                   4165:     if ( all_q ) {
                   4166:         removecont_array_q((Q *)p,n);
                   4167:     } else {
1.235   ! noro     4168:         c = (Q *)MALLOC(n*sizeof(Q));
        !          4169:         w = (P *)MALLOC(n*sizeof(P));
1.157     noro     4170:         for ( i = 0; i < n; i++ ) {
                   4171:             ptozp(p[i],1,&c[i],&w[i]);
                   4172:         }
                   4173:         removecont_array_q(c,n);
                   4174:         nd_heu_nezgcdnpz(nd_vc,w,n,full,&t);
                   4175:         for ( i = 0; i < n; i++ ) {
                   4176:             divsp(nd_vc,w[i],t,&s); mulp(nd_vc,s,(P)c[i],&p[i]);
                   4177:         }
                   4178:     }
1.146     noro     4179: }
                   4180:
                   4181: void removecont_array_q(Q *c,int n)
1.21      noro     4182: {
1.157     noro     4183:     struct oVECT v;
                   4184:     Q d0,d1,a,u,u1,gcd;
                   4185:     int i,j;
                   4186:     N qn,rn,gn;
                   4187:     Q *q,*r;
                   4188:
1.235   ! noro     4189:     q = (Q *)MALLOC(n*sizeof(Q));
        !          4190:     r = (Q *)MALLOC(n*sizeof(Q));
1.157     noro     4191:     v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   4192:     igcdv_estimate(&v,&d0);
                   4193:     for ( i = 0; i < n; i++ ) {
                   4194:         divn(NM(c[i]),NM(d0),&qn,&rn);
                   4195:         NTOQ(qn,SGN(c[i])*SGN(d0),q[i]);
                   4196:         NTOQ(rn,SGN(c[i]),r[i]);
                   4197:     }
                   4198:     for ( i = 0; i < n; i++ ) if ( r[i] ) break;
                   4199:     if ( i < n ) {
                   4200:         v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   4201:         igcdv(&v,&d1);
                   4202:         gcdn(NM(d0),NM(d1),&gn); NTOQ(gn,1,gcd);
                   4203:         divsn(NM(d0),gn,&qn); NTOQ(qn,1,a);
                   4204:         for ( i = 0; i < n; i++ ) {
                   4205:             mulq(a,q[i],&u);
                   4206:             if ( r[i] ) {
                   4207:                 divsn(NM(r[i]),gn,&qn); NTOQ(qn,SGN(r[i]),u1);
                   4208:                 addq(u,u1,&q[i]);
                   4209:             } else
                   4210:                 q[i] = u;
                   4211:         }
                   4212:     }
                   4213:     for ( i = 0; i < n; i++ ) c[i] = q[i];
1.16      noro     4214: }
                   4215:
1.19      noro     4216: void nd_mul_c(int mod,ND p,int mul)
1.1       noro     4217: {
1.157     noro     4218:     NM m;
                   4219:     int c,c1;
1.1       noro     4220:
1.157     noro     4221:     if ( !p ) return;
                   4222:     if ( mul == 1 ) return;
                   4223:     if ( mod == -1 )
                   4224:         for ( m = BDY(p); m; m = NEXT(m) )
                   4225:             CM(m) = _mulsf(CM(m),mul);
                   4226:     else
                   4227:         for ( m = BDY(p); m; m = NEXT(m) ) {
                   4228:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   4229:         }
1.1       noro     4230: }
                   4231:
1.232     noro     4232: void nd_mul_c_lf(ND p,GZ mul)
                   4233: {
                   4234:     NM m;
                   4235:     GZ c;
                   4236:
                   4237:     if ( !p ) return;
                   4238:     if ( UNIGZ(mul) ) return;
                   4239:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   4240:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   4241:     }
                   4242: }
                   4243:
1.146     noro     4244: void nd_mul_c_q(ND p,P mul)
1.16      noro     4245: {
1.157     noro     4246:     NM m;
                   4247:     P c;
1.16      noro     4248:
1.157     noro     4249:     if ( !p ) return;
                   4250:     if ( UNIQ(mul) ) return;
                   4251:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   4252:         mulp(nd_vc,CP(m),mul,&c); CP(m) = c;
                   4253:     }
1.16      noro     4254: }
                   4255:
1.61      noro     4256: void nd_mul_c_p(VL vl,ND p,P mul)
                   4257: {
1.157     noro     4258:     NM m;
                   4259:     P c;
1.61      noro     4260:
1.157     noro     4261:     if ( !p ) return;
                   4262:     for ( m = BDY(p); m; m = NEXT(m) ) {
                   4263:         mulp(vl,CP(m),mul,&c); CP(m) = c;
                   4264:     }
1.61      noro     4265: }
                   4266:
1.1       noro     4267: void nd_free(ND p)
                   4268: {
1.157     noro     4269:     NM t,s;
1.1       noro     4270:
1.157     noro     4271:     if ( !p ) return;
                   4272:     t = BDY(p);
                   4273:     while ( t ) {
                   4274:         s = NEXT(t);
                   4275:         FREENM(t);
                   4276:         t = s;
                   4277:     }
                   4278:     FREEND(p);
1.1       noro     4279: }
                   4280:
1.23      noro     4281: void ndv_free(NDV p)
                   4282: {
1.200     noro     4283:     GCFREE(BDY(p));
1.23      noro     4284: }
                   4285:
1.61      noro     4286: void nd_append_red(UINT *d,int i)
1.1       noro     4287: {
1.157     noro     4288:     RHist m,m0;
                   4289:     int h;
1.1       noro     4290:
1.157     noro     4291:     NEWRHist(m);
                   4292:     h = ndl_hash_value(d);
                   4293:     m->index = i;
                   4294:     ndl_copy(d,DL(m));
                   4295:     NEXT(m) = nd_red[h];
                   4296:     nd_red[h] = m;
1.1       noro     4297: }
                   4298:
1.61      noro     4299: UINT *ndv_compute_bound(NDV p)
1.1       noro     4300: {
1.157     noro     4301:     UINT *d1,*d2,*t;
                   4302:     UINT u;
                   4303:     int i,j,k,l,len,ind;
                   4304:     NMV m;
                   4305:
                   4306:     if ( !p )
                   4307:         return 0;
1.235   ! noro     4308:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
        !          4309:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
1.157     noro     4310:     len = LEN(p);
                   4311:     m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   4312:     for ( i = 1; i < len; i++, NMV_ADV(m) ) {
1.159     noro     4313:         ndl_max(DL(m),d1,d2);
1.157     noro     4314:         t = d1; d1 = d2; d2 = t;
                   4315:     }
                   4316:     l = nd_nvar+31;
                   4317:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   4318:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   4319:         u = d1[i];
                   4320:         k = (nd_epw-1)*nd_bpe;
                   4321:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   4322:             t[ind] = (u>>k)&nd_mask0;
                   4323:     }
                   4324:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   4325:     return t;
1.1       noro     4326: }
                   4327:
1.99      noro     4328: UINT *nd_compute_bound(ND p)
                   4329: {
1.157     noro     4330:     UINT *d1,*d2,*t;
                   4331:     UINT u;
                   4332:     int i,j,k,l,len,ind;
                   4333:     NM m;
                   4334:
                   4335:     if ( !p )
                   4336:         return 0;
1.235   ! noro     4337:     d1 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
        !          4338:     d2 = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
1.157     noro     4339:     len = LEN(p);
                   4340:     m = BDY(p); ndl_copy(DL(m),d1); m = NEXT(m);
                   4341:     for ( m = NEXT(m); m; m = NEXT(m) ) {
                   4342:         ndl_lcm(DL(m),d1,d2);
                   4343:         t = d1; d1 = d2; d2 = t;
                   4344:     }
                   4345:     l = nd_nvar+31;
                   4346:     t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
                   4347:     for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   4348:         u = d1[i];
                   4349:         k = (nd_epw-1)*nd_bpe;
                   4350:         for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   4351:             t[ind] = (u>>k)&nd_mask0;
                   4352:     }
                   4353:     for ( ; ind < l; ind++ ) t[ind] = 0;
                   4354:     return t;
1.99      noro     4355: }
                   4356:
1.157     noro     4357: /* if nd_module == 1 then d[nd_exporigin-1] indicates the position */
                   4358: /* of a term. In this case we need additional 1 word. */
                   4359:
1.48      noro     4360: int nd_get_exporigin(struct order_spec *ord)
                   4361: {
1.157     noro     4362:     switch ( ord->id ) {
                   4363:         case 0: case 2: case 256: case 258:
                   4364:             return 1+nd_module;
                   4365:         case 1: case 257:
                   4366:             /* block order */
                   4367:             /* poly ring d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
                   4368:             /* module d[0]:weight d[1]:w0,...,d[nd_exporigin-2]:w(n-1) */
                   4369:             return ord->ord.block.length+1+nd_module;
                   4370:         case 3: case 259:
                   4371:             error("nd_get_exporigin : composite order is not supported yet.");
                   4372:     }
1.48      noro     4373: }
                   4374:
1.61      noro     4375: void nd_setup_parameters(int nvar,int max) {
1.157     noro     4376:     int i,j,n,elen,ord_o,ord_l,l,s,wpd;
                   4377:     struct order_pair *op;
1.48      noro     4378:
1.157     noro     4379:     nd_nvar = nvar;
                   4380:     if ( max ) {
                   4381:         /* XXX */
                   4382:         if ( do_weyl ) nd_bpe = 32;
                   4383:         else if ( max < 2 ) nd_bpe = 1;
                   4384:         else if ( max < 4 ) nd_bpe = 2;
                   4385:         else if ( max < 8 ) nd_bpe = 3;
                   4386:         else if ( max < 16 ) nd_bpe = 4;
                   4387:         else if ( max < 32 ) nd_bpe = 5;
                   4388:         else if ( max < 64 ) nd_bpe = 6;
                   4389:         else if ( max < 256 ) nd_bpe = 8;
                   4390:         else if ( max < 1024 ) nd_bpe = 10;
                   4391:         else if ( max < 65536 ) nd_bpe = 16;
                   4392:         else nd_bpe = 32;
                   4393:     }
1.203     noro     4394:     if ( !do_weyl && weight_check && (current_dl_weight_vector || nd_matrix) ) {
1.201     noro     4395:         UINT t;
1.203     noro     4396:                int st;
                   4397:         int *v;
                   4398:        /* t = max(weights) */
                   4399:         t = 0;
                   4400:         if ( current_dl_weight_vector )
                   4401:             for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                   4402:                 if ( (st=current_dl_weight_vector[i]) < 0 ) st = -st;
                   4403:                 if ( t < st ) t = st;
                   4404:             }
                   4405:         if ( nd_matrix )
                   4406:             for ( i = 0; i < nd_matrix_len; i++ )
                   4407:                 for ( j = 0, v = nd_matrix[i]; j < nd_nvar; j++ ) {
                   4408:                     if ( (st=v[j]) < 0 ) st = -st;
                   4409:                     if ( t < st ) t = st;
                   4410:                                }
                   4411:         /* i = bitsize of t */
                   4412:         for ( i = 0; t; t >>=1, i++ );
                   4413:         /* i += bitsize of nd_nvar */
                   4414:         for ( t = nd_nvar; t; t >>=1, i++);
                   4415:         /* nd_bpe+i = bitsize of max(weights)*max(exp)*nd_nvar */
                   4416:         if ( (nd_bpe+i) >= 31 )
                   4417:             error("nd_setup_parameters : too large weight");
                   4418:     }
1.157     noro     4419:     nd_epw = (sizeof(UINT)*8)/nd_bpe;
                   4420:     elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   4421:     nd_exporigin = nd_get_exporigin(nd_ord);
                   4422:     wpd = nd_exporigin+elen;
                   4423:     if ( nd_module )
                   4424:         nd_mpos = nd_exporigin-1;
                   4425:     else
                   4426:         nd_mpos = -1;
                   4427:     if ( wpd != nd_wpd ) {
                   4428:         nd_free_private_storage();
                   4429:         nd_wpd = wpd;
                   4430:     }
                   4431:     if ( nd_bpe < 32 ) {
                   4432:         nd_mask0 = (1<<nd_bpe)-1;
                   4433:     } else {
                   4434:         nd_mask0 = 0xffffffff;
                   4435:     }
                   4436:     bzero(nd_mask,sizeof(nd_mask));
                   4437:     nd_mask1 = 0;
                   4438:     for ( i = 0; i < nd_epw; i++ ) {
                   4439:         nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   4440:         nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   4441:     }
                   4442:     nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
                   4443:     nd_epos = nd_create_epos(nd_ord);
                   4444:     nd_blockmask = nd_create_blockmask(nd_ord);
                   4445:     nd_work_vector = (int *)REALLOC(nd_work_vector,nd_nvar*sizeof(int));
1.1       noro     4446: }
                   4447:
1.103     noro     4448: ND_pairs nd_reconstruct(int trace,ND_pairs d)
1.1       noro     4449: {
1.157     noro     4450:     int i,obpe,oadv,h;
                   4451:     static NM prev_nm_free_list;
                   4452:     static ND_pairs prev_ndp_free_list;
                   4453:     RHist mr0,mr;
                   4454:     RHist r;
                   4455:     RHist *old_red;
                   4456:     ND_pairs s0,s,t;
                   4457:     EPOS oepos;
                   4458:
                   4459:     obpe = nd_bpe;
                   4460:     oadv = nmv_adv;
                   4461:     oepos = nd_epos;
                   4462:     if ( obpe < 2 ) nd_bpe = 2;
                   4463:     else if ( obpe < 3 ) nd_bpe = 3;
                   4464:     else if ( obpe < 4 ) nd_bpe = 4;
                   4465:     else if ( obpe < 5 ) nd_bpe = 5;
                   4466:     else if ( obpe < 6 ) nd_bpe = 6;
                   4467:     else if ( obpe < 8 ) nd_bpe = 8;
                   4468:     else if ( obpe < 10 ) nd_bpe = 10;
                   4469:     else if ( obpe < 16 ) nd_bpe = 16;
                   4470:     else if ( obpe < 32 ) nd_bpe = 32;
                   4471:     else error("nd_reconstruct : exponent too large");
                   4472:
                   4473:     nd_setup_parameters(nd_nvar,0);
                   4474:     prev_nm_free_list = _nm_free_list;
                   4475:     prev_ndp_free_list = _ndp_free_list;
                   4476:     _nm_free_list = 0;
                   4477:     _ndp_free_list = 0;
1.215     noro     4478:     for ( i = nd_psn-1; i >= 0; i-- ) {
                   4479:         ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   4480:         ndv_realloc(nd_ps_sym[i],obpe,oadv,oepos);
                   4481:         ndv_realloc(nd_ps_gz[i],obpe,oadv,oepos);
                   4482:     }
1.157     noro     4483:     if ( trace )
1.215     noro     4484:         for ( i = nd_psn-1; i >= 0; i-- ) {
1.157     noro     4485:             ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
1.215     noro     4486:             ndv_realloc(nd_ps_trace_sym[i],obpe,oadv,oepos);
                   4487:         }
1.157     noro     4488:     s0 = 0;
                   4489:     for ( t = d; t; t = NEXT(t) ) {
                   4490:         NEXTND_pairs(s0,s);
                   4491:         s->i1 = t->i1;
                   4492:         s->i2 = t->i2;
                   4493:         SG(s) = SG(t);
                   4494:         ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
                   4495:     }
                   4496:
1.235   ! noro     4497:     old_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
1.157     noro     4498:     for ( i = 0; i < REDTAB_LEN; i++ ) {
                   4499:         old_red[i] = nd_red[i];
                   4500:         nd_red[i] = 0;
                   4501:     }
                   4502:     for ( i = 0; i < REDTAB_LEN; i++ )
                   4503:         for ( r = old_red[i]; r; r = NEXT(r) ) {
                   4504:             NEWRHist(mr);
                   4505:             mr->index = r->index;
                   4506:             SG(mr) = SG(r);
                   4507:             ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
                   4508:             h = ndl_hash_value(DL(mr));
                   4509:             NEXT(mr) = nd_red[h];
                   4510:             nd_red[h] = mr;
                   4511:         }
                   4512:     for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   4513:     old_red = 0;
                   4514:     for ( i = 0; i < nd_psn; i++ ) {
                   4515:         NEWRHist(r); SG(r) = SG(nd_psh[i]);
                   4516:         ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
                   4517:         nd_psh[i] = r;
                   4518:     }
                   4519:     if ( s0 ) NEXT(s) = 0;
                   4520:     prev_nm_free_list = 0;
                   4521:     prev_ndp_free_list = 0;
1.71      noro     4522: #if 0
1.157     noro     4523:     GC_gcollect();
1.71      noro     4524: #endif
1.157     noro     4525:     return s0;
1.1       noro     4526: }
                   4527:
1.61      noro     4528: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
1.1       noro     4529: {
1.157     noro     4530:     int n,i,ei,oepw,omask0,j,s,ord_l,l;
                   4531:     struct order_pair *op;
1.1       noro     4532:
1.157     noro     4533:     n = nd_nvar;
                   4534:     oepw = (sizeof(UINT)*8)/obpe;
                   4535:     omask0 = (1<<obpe)-1;
                   4536:     TD(r) = TD(d);
                   4537:     for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
                   4538:     if ( nd_blockmask ) {
                   4539:         l = nd_blockmask->n;
                   4540:         op = nd_blockmask->order_pair;
                   4541:         for ( i = 1; i < nd_exporigin; i++ )
                   4542:             r[i] = d[i];
                   4543:         for ( j = 0, s = 0; j < l; j++ ) {
                   4544:             ord_l = op[j].length;
                   4545:             for ( i = 0; i < ord_l; i++, s++ ) {
                   4546:                 ei =  GET_EXP_OLD(d,s);
                   4547:                 PUT_EXP(r,s,ei);
                   4548:             }
                   4549:         }
                   4550:     } else {
                   4551:         for ( i = 0; i < n; i++ ) {
                   4552:             ei = GET_EXP_OLD(d,i);
                   4553:             PUT_EXP(r,i,ei);
                   4554:         }
                   4555:     }
                   4556:     if ( nd_module ) MPOS(r) = MPOS(d);
1.1       noro     4557: }
1.3       noro     4558:
1.6       noro     4559: ND nd_copy(ND p)
                   4560: {
1.157     noro     4561:     NM m,mr,mr0;
                   4562:     int c,n;
                   4563:     ND r;
                   4564:
                   4565:     if ( !p )
                   4566:         return 0;
                   4567:     else {
                   4568:         for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   4569:             NEXTNM(mr0,mr);
                   4570:             CM(mr) = CM(m);
                   4571:             ndl_copy(DL(m),DL(mr));
                   4572:         }
                   4573:         NEXT(mr) = 0;
                   4574:         MKND(NV(p),mr0,LEN(p),r);
                   4575:         SG(r) = SG(p);
                   4576:         return r;
                   4577:     }
1.6       noro     4578: }
                   4579:
1.53      noro     4580: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
1.11      noro     4581: {
1.157     noro     4582:     NM m1,m2;
                   4583:     NDV p1,p2;
                   4584:     ND t1,t2;
                   4585:     UINT *lcm;
                   4586:     P gp,tp;
1.167     noro     4587:     Q g,t,iq;
1.157     noro     4588:     int td;
1.167     noro     4589:     LIST hist;
                   4590:     NODE node;
                   4591:     DP d;
1.157     noro     4592:
                   4593:     if ( !mod && nd_demand ) {
                   4594:         p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
                   4595:     } else {
                   4596:         if ( trace ) {
                   4597:             p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   4598:         } else {
                   4599:             p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   4600:         }
                   4601:     }
                   4602:     lcm = LCM(p);
                   4603:     NEWNM(m1); ndl_sub(lcm,HDL(p1),DL(m1));
                   4604:     if ( ndl_check_bound2(p->i1,DL(m1)) ) {
                   4605:         FREENM(m1); return 0;
                   4606:     }
                   4607:     NEWNM(m2); ndl_sub(lcm,HDL(p2),DL(m2));
                   4608:     if ( ndl_check_bound2(p->i2,DL(m2)) ) {
                   4609:         FREENM(m1); FREENM(m2); return 0;
                   4610:     }
                   4611:
                   4612:     if ( mod == -1 ) {
                   4613:         CM(m1) = HCM(p2); CM(m2) = _chsgnsf(HCM(p1));
1.232     noro     4614:     } else if ( mod > 0 ) {
1.157     noro     4615:         CM(m1) = HCM(p2); CM(m2) = mod-HCM(p1);
1.232     noro     4616:     } else if ( mod == -2 ) {
                   4617:         CZ(m1) = HCZ(p2); chsgnlf(HCZ(p1),&CZ(m2));
1.157     noro     4618:     } else if ( nd_vc ) {
                   4619:         ezgcdpz(nd_vc,HCP(p1),HCP(p2),&gp);
                   4620:         divsp(nd_vc,HCP(p2),gp,&CP(m1));
                   4621:         divsp(nd_vc,HCP(p1),gp,&tp); chsgnp(tp,&CP(m2));
                   4622:     } else {
                   4623:         igcd_cofactor(HCQ(p1),HCQ(p2),&g,&t,&CQ(m1)); chsgnq(t,&CQ(m2));
                   4624:     }
                   4625:     t1 = ndv_mul_nm(mod,m1,p1); t2 = ndv_mul_nm(mod,m2,p2);
                   4626:     *rp = nd_add(mod,t1,t2);
1.172     noro     4627:     if ( nd_gentrace ) {
1.167     noro     4628:         /* nd_tracelist is initialized */
                   4629:         STOQ(p->i1,iq); nmtodp(mod,m1,&d); node = mknode(4,ONE,iq,d,ONE);
                   4630:         MKLIST(hist,node); MKNODE(nd_tracelist,hist,0);
                   4631:         STOQ(p->i2,iq); nmtodp(mod,m2,&d); node = mknode(4,ONE,iq,d,ONE);
                   4632:         MKLIST(hist,node); MKNODE(node,hist,nd_tracelist);
                   4633:         nd_tracelist = node;
                   4634:     }
1.157     noro     4635:     FREENM(m1); FREENM(m2);
                   4636:     return 1;
1.11      noro     4637: }
                   4638:
1.19      noro     4639: void ndv_mul_c(int mod,NDV p,int mul)
1.11      noro     4640: {
1.157     noro     4641:     NMV m;
                   4642:     int c,c1,len,i;
1.11      noro     4643:
1.157     noro     4644:     if ( !p ) return;
                   4645:     len = LEN(p);
                   4646:     if ( mod == -1 )
                   4647:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   4648:             CM(m) = _mulsf(CM(m),mul);
                   4649:     else
                   4650:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4651:             c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   4652:         }
1.11      noro     4653: }
                   4654:
1.232     noro     4655: void ndv_mul_c_lf(NDV p,GZ mul)
                   4656: {
                   4657:     NMV m;
                   4658:     GZ c;
                   4659:     int len,i;
                   4660:
                   4661:     if ( !p ) return;
                   4662:     len = LEN(p);
                   4663:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4664:         mullf(CZ(m),mul,&c); CZ(m) = c;
                   4665:     }
                   4666: }
                   4667:
1.113     noro     4668: void ndv_mul_c_q(NDV p,Q mul)
1.16      noro     4669: {
1.157     noro     4670:     NMV m;
                   4671:     Q c;
                   4672:     int len,i;
                   4673:
                   4674:     if ( !p ) return;
                   4675:     len = LEN(p);
                   4676:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4677:         mulq(CQ(m),mul,&c); CQ(m) = c;
                   4678:     }
1.16      noro     4679: }
                   4680:
1.55      noro     4681: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
1.157     noro     4682:     int n2,i,j,l,n,tlen;
                   4683:     UINT *d0;
                   4684:     NM *tab,*psum;
                   4685:     ND s,r;
                   4686:     NM t;
                   4687:     NMV m1;
                   4688:
                   4689:     if ( !p ) return 0;
                   4690:     n = NV(p); n2 = n>>1;
                   4691:     d0 = DL(m0);
                   4692:     l = LEN(p);
                   4693:     for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
1.235   ! noro     4694:     tab = (NM *)MALLOC(tlen*sizeof(NM));
        !          4695:     psum = (NM *)MALLOC(tlen*sizeof(NM));
1.157     noro     4696:     for ( i = 0; i < tlen; i++ ) psum[i] = 0;
                   4697:     m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   4698:     for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
                   4699:         /* m0(NM) * m1(NMV) => tab(NM) */
                   4700:         weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
                   4701:         for ( j = 0; j < tlen; j++ ) {
                   4702:             if ( tab[j] ) {
                   4703:                 NEXT(tab[j]) = psum[j];    psum[j] = tab[j];
                   4704:             }
                   4705:         }
                   4706:     }
                   4707:     for ( i = tlen-1, r = 0; i >= 0; i-- )
                   4708:         if ( psum[i] ) {
                   4709:             for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   4710:             MKND(n,psum[i],j,s);
                   4711:             r = nd_add(mod,r,s);
                   4712:         }
                   4713:     if ( r ) SG(r) = SG(p)+TD(d0);
                   4714:     return r;
1.55      noro     4715: }
                   4716:
1.56      noro     4717: /* product of monomials */
                   4718: /* XXX block order is not handled correctly */
                   4719:
1.55      noro     4720: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   4721: {
1.157     noro     4722:     int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
                   4723:     UINT *d0,*d1,*d,*dt,*ctab;
                   4724:     Q *ctab_q;
                   4725:     Q q,q1;
                   4726:     UINT c0,c1,c;
                   4727:     NM *p;
                   4728:     NM m,t;
                   4729:     int mpos;
                   4730:
                   4731:     for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   4732:     if ( !m0 || !m1 ) return;
                   4733:     d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
1.166     noro     4734:     if ( nd_module )
                   4735:         if ( MPOS(d0) ) error("weyl_mul_nm_nmv : invalid operation");
                   4736:
1.157     noro     4737:     NEWNM(m); d = DL(m);
                   4738:     if ( mod ) {
                   4739:         c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
1.179     noro     4740:     } else if ( nd_vc )
                   4741:         mulp(nd_vc,CP(m0),CP(m1),&CP(m));
                   4742:        else
1.157     noro     4743:         mulq(CQ(m0),CQ(m1),&CQ(m));
                   4744:     for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   4745:     homo = n&1 ? 1 : 0;
                   4746:     if ( homo ) {
                   4747:         /* offset of h-degree */
                   4748:         h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   4749:         PUT_EXP(DL(m),n-1,h);
                   4750:         TD(DL(m)) = h;
                   4751:         if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   4752:     }
                   4753:     tab[0] = m;
                   4754:     NEWNM(m); d = DL(m);
                   4755:     for ( i = 0, curlen = 1; i < n2; i++ ) {
                   4756:         a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   4757:         k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   4758:         /* xi^a*(Di^k*xi^l)*Di^b */
                   4759:         a += l; b += k;
                   4760:         s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
                   4761:         if ( !k || !l ) {
                   4762:             for ( j = 0; j < curlen; j++ )
                   4763:                 if ( t = tab[j] ) {
                   4764:                     dt = DL(t);
                   4765:                     PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
                   4766:                     if ( nd_blockmask ) ndl_weight_mask(dt);
                   4767:                 }
                   4768:             curlen *= k+1;
                   4769:             continue;
                   4770:         }
                   4771:         min = MIN(k,l);
                   4772:         if ( mod ) {
1.235   ! noro     4773:             ctab = (UINT *)MALLOC((min+1)*sizeof(UINT));
1.157     noro     4774:             mkwcm(k,l,mod,ctab);
                   4775:         } else {
1.235   ! noro     4776:             ctab_q = (Q *)MALLOC((min+1)*sizeof(Q));
1.157     noro     4777:             mkwc(k,l,ctab_q);
                   4778:         }
                   4779:         for ( j = min; j >= 0; j-- ) {
                   4780:             for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
                   4781:             PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
                   4782:             h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
                   4783:             if ( homo ) {
                   4784:                 TD(d) = s;
                   4785:                 PUT_EXP(d,n-1,s-h);
                   4786:             } else TD(d) = h;
                   4787:             if ( nd_blockmask ) ndl_weight_mask(d);
                   4788:             if ( mod ) c = ctab[j];
                   4789:             else q = ctab_q[j];
                   4790:             p = tab+curlen*j;
                   4791:             if ( j == 0 ) {
                   4792:                 for ( u = 0; u < curlen; u++, p++ ) {
                   4793:                     if ( tab[u] ) {
                   4794:                         ndl_addto(DL(tab[u]),d);
                   4795:                         if ( mod ) {
                   4796:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
1.180     noro     4797:                         } else if ( nd_vc )
                   4798:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(tab[u]));
                   4799:                                                else {
1.157     noro     4800:                             mulq(CQ(tab[u]),q,&q1); CQ(tab[u]) = q1;
                   4801:                         }
                   4802:                     }
                   4803:                 }
                   4804:             } else {
                   4805:                 for ( u = 0; u < curlen; u++, p++ ) {
                   4806:                     if ( tab[u] ) {
                   4807:                         NEWNM(t);
                   4808:                         ndl_add(DL(tab[u]),d,DL(t));
                   4809:                         if ( mod ) {
                   4810:                             c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
1.181     noro     4811:                         } else if ( nd_vc )
1.180     noro     4812:                             mulp(nd_vc,CP(tab[u]),(P)q,&CP(t));
                   4813:                                                else
1.157     noro     4814:                             mulq(CQ(tab[u]),q,&CQ(t));
                   4815:                         *p = t;
                   4816:                     }
                   4817:                 }
                   4818:             }
                   4819:         }
                   4820:         curlen *= k+1;
                   4821:     }
                   4822:     FREENM(m);
1.167     noro     4823:     if ( nd_module ) {
1.166     noro     4824:         mpos = MPOS(d1);
1.167     noro     4825:         for ( i = 0; i < tlen; i++ )
                   4826:             if ( tab[i] ) {
                   4827:                 d = DL(tab[i]);
                   4828:                 MPOS(d) = mpos;
                   4829:                 TD(d) = ndl_weight(d);
                   4830:             }
                   4831:     }
1.55      noro     4832: }
                   4833:
1.63      noro     4834: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   4835: {
1.157     noro     4836:     NM mr,mr0;
                   4837:     NMV m;
                   4838:     UINT *d,*dt,*dm;
                   4839:     int c,n,td,i,c1,c2,len;
                   4840:     Q q;
                   4841:     ND r;
                   4842:
                   4843:     if ( !p ) return 0;
                   4844:     else {
                   4845:         n = NV(p); m = BDY(p);
                   4846:         d = DL(m0);
                   4847:         len = LEN(p);
                   4848:         mr0 = 0;
                   4849:         td = TD(d);
                   4850:         c = CM(m0);
                   4851:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4852:             NEXTNM(mr0,mr);
                   4853:             CM(mr) = 1;
                   4854:             ndl_add(DL(m),d,DL(mr));
                   4855:         }
                   4856:         NEXT(mr) = 0;
                   4857:         MKND(NV(p),mr0,len,r);
                   4858:         SG(r) = SG(p) + TD(d);
                   4859:         return r;
                   4860:     }
1.63      noro     4861: }
                   4862:
1.55      noro     4863: ND ndv_mul_nm(int mod,NM m0,NDV p)
1.9       noro     4864: {
1.157     noro     4865:     NM mr,mr0;
                   4866:     NMV m;
                   4867:     UINT *d,*dt,*dm;
                   4868:     int c,n,td,i,c1,c2,len;
                   4869:     P q;
                   4870:     ND r;
                   4871:
                   4872:     if ( !p ) return 0;
                   4873:     else if ( do_weyl )
1.232     noro     4874:         if ( mod < 0 )
1.157     noro     4875:             error("ndv_mul_nm : not implemented (weyl)");
                   4876:         else
                   4877:             return weyl_ndv_mul_nm(mod,m0,p);
                   4878:     else {
                   4879:         n = NV(p); m = BDY(p);
                   4880:         d = DL(m0);
                   4881:         len = LEN(p);
                   4882:         mr0 = 0;
                   4883:         td = TD(d);
                   4884:         if ( mod == -1 ) {
                   4885:             c = CM(m0);
                   4886:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4887:                 NEXTNM(mr0,mr);
                   4888:                 CM(mr) = _mulsf(CM(m),c);
                   4889:                 ndl_add(DL(m),d,DL(mr));
                   4890:             }
1.232     noro     4891:         } else if ( mod == -2 ) {
                   4892:             GZ cl;
                   4893:             cl = CZ(m0);
                   4894:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4895:                 NEXTNM(mr0,mr);
                   4896:                 mullf(CZ(m),cl,&CZ(mr));
                   4897:                 ndl_add(DL(m),d,DL(mr));
                   4898:             }
1.157     noro     4899:         } else if ( mod ) {
                   4900:             c = CM(m0);
                   4901:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4902:                 NEXTNM(mr0,mr);
                   4903:                 c1 = CM(m);
                   4904:                 DMAR(c1,c,0,mod,c2);
                   4905:                 CM(mr) = c2;
                   4906:                 ndl_add(DL(m),d,DL(mr));
                   4907:             }
                   4908:         } else {
                   4909:             q = CP(m0);
                   4910:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4911:                 NEXTNM(mr0,mr);
                   4912:                 mulp(nd_vc,CP(m),q,&CP(mr));
                   4913:                 ndl_add(DL(m),d,DL(mr));
                   4914:             }
                   4915:         }
                   4916:         NEXT(mr) = 0;
                   4917:         MKND(NV(p),mr0,len,r);
                   4918:         SG(r) = SG(p) + TD(d);
                   4919:         return r;
                   4920:     }
1.4       noro     4921: }
                   4922:
1.104     noro     4923: ND nd_quo(int mod,PGeoBucket bucket,NDV d)
1.99      noro     4924: {
1.157     noro     4925:     NM mq0,mq;
                   4926:     NMV tm;
                   4927:     Q q;
                   4928:     int i,nv,sg,c,c1,c2,hindex;
                   4929:     ND p,t,r;
                   4930:     N tnm;
                   4931:
                   4932:     if ( bucket->m < 0 ) return 0;
                   4933:     else {
                   4934:         nv = NV(d);
                   4935:         mq0 = 0;
1.235   ! noro     4936:         tm = (NMV)MALLOC(nmv_adv);
1.157     noro     4937:         while ( 1 ) {
1.232     noro     4938:             if ( mod > 0 || mod == -1 )
                   4939:               hindex = head_pbucket(mod,bucket);
                   4940:             else if ( mod == -2 )
                   4941:               hindex = head_pbucket_lf(bucket);
                   4942:             else
                   4943:               hindex = head_pbucket_q(bucket);
1.157     noro     4944:             if ( hindex < 0 ) break;
                   4945:             p = bucket->body[hindex];
                   4946:             NEXTNM(mq0,mq);
                   4947:             ndl_sub(HDL(p),HDL(d),DL(mq));
                   4948:             ndl_copy(DL(mq),DL(tm));
                   4949:             if ( mod ) {
                   4950:                 c1 = invm(HCM(d),mod); c2 = HCM(p);
                   4951:                 DMAR(c1,c2,0,mod,c); CM(mq) = c;
                   4952:                 CM(tm) = mod-c;
                   4953:             } else {
                   4954:                 divsn(NM(HCQ(p)),NM(HCQ(d)),&tnm);
                   4955:                 NTOQ(tnm,SGN(HCQ(p))*SGN(HCQ(d)),CQ(mq));
                   4956:                 chsgnq(CQ(mq),&CQ(tm));
                   4957:             }
                   4958:             t = ndv_mul_nmv_trunc(mod,tm,d,HDL(d));
                   4959:             bucket->body[hindex] = nd_remove_head(p);
                   4960:             t = nd_remove_head(t);
                   4961:             add_pbucket(mod,bucket,t);
                   4962:         }
                   4963:         if ( !mq0 )
                   4964:             r = 0;
                   4965:         else {
                   4966:             NEXT(mq) = 0;
                   4967:             for ( i = 0, mq = mq0; mq; mq = NEXT(mq), i++ );
                   4968:             MKND(nv,mq0,i,r);
                   4969:             /* XXX */
                   4970:             SG(r) = HTD(r);
                   4971:         }
                   4972:         return r;
                   4973:     }
1.99      noro     4974: }
                   4975:
1.43      noro     4976: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
1.11      noro     4977: {
1.157     noro     4978:     NMV m,mr,mr0,t;
                   4979:     int len,i,k;
1.11      noro     4980:
1.157     noro     4981:     if ( !p ) return;
                   4982:     m = BDY(p); len = LEN(p);
                   4983:     mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   4984:     m = (NMV)((char *)mr0+(len-1)*oadv);
                   4985:     mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
1.235   ! noro     4986:     t = (NMV)MALLOC(nmv_adv);
1.157     noro     4987:     for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   4988:         CQ(t) = CQ(m);
                   4989:         for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   4990:         ndl_reconstruct(DL(m),DL(t),obpe,oepos);
                   4991:         CQ(mr) = CQ(t);
                   4992:         ndl_copy(DL(t),DL(mr));
                   4993:     }
                   4994:     BDY(p) = mr0;
1.61      noro     4995: }
                   4996:
                   4997: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   4998: {
1.157     noro     4999:     NMV m,mr,mr0;
                   5000:     int len,i;
                   5001:     NDV r;
                   5002:
                   5003:     if ( !p ) return 0;
                   5004:     m = BDY(p); len = LEN(p);
                   5005:     mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   5006:     for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   5007:         ndl_zero(DL(mr));
                   5008:         ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
                   5009:         CQ(mr) = CQ(m);
                   5010:     }
                   5011:     MKNDV(NV(p),mr0,len,r);
                   5012:     SG(r) = SG(p);
                   5013:     return r;
1.11      noro     5014: }
                   5015:
1.61      noro     5016: /* duplicate p */
                   5017:
                   5018: NDV ndv_dup(int mod,NDV p)
1.3       noro     5019: {
1.157     noro     5020:     NDV d;
                   5021:     NMV t,m,m0;
                   5022:     int i,len;
                   5023:
                   5024:     if ( !p ) return 0;
                   5025:     len = LEN(p);
1.232     noro     5026:     m0 = m = (NMV)((mod>0 || mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
1.157     noro     5027:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   5028:         ndl_copy(DL(t),DL(m));
                   5029:         CQ(m) = CQ(t);
                   5030:     }
                   5031:     MKNDV(NV(p),m0,len,d);
                   5032:     SG(d) = SG(p);
                   5033:     return d;
1.23      noro     5034: }
                   5035:
1.215     noro     5036: NDV ndvtondvgz(NDV p)
                   5037: {
                   5038:        NDV r;
                   5039:        int len,i;
                   5040:        NMV t;
                   5041:
                   5042:        r = ndv_dup(0,p);
                   5043:        len = LEN(p);
                   5044:     for ( t = BDY(r), i = 0; i < len; i++, NMV_ADV(t) ) CZ(t) = ztogz(CQ(t));
                   5045:        return r;
                   5046: }
                   5047:
                   5048: NDV ndvgztondv(NDV p)
                   5049: {
                   5050:        NDV r;
                   5051:        int len,i;
                   5052:        NMV t;
                   5053:
                   5054:        r = ndv_dup(0,p);
                   5055:        len = LEN(p);
                   5056:     for ( t = BDY(r), i = 0; i < len; i++, NMV_ADV(t) ) CQ(t) = gztoz(CZ(t));
                   5057:        return r;
                   5058: }
                   5059:
                   5060: NDV ndv_symbolic(int mod,NDV p)
                   5061: {
                   5062:     NDV d;
                   5063:     NMV t,m,m0;
                   5064:     int i,len;
                   5065:
                   5066:     if ( !p ) return 0;
                   5067:     len = LEN(p);
1.232     noro     5068:     m0 = m = (NMV)((mod>0||mod==-1)?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
1.215     noro     5069:     for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
                   5070:         ndl_copy(DL(t),DL(m));
                   5071:         CQ(m) = ONE;
                   5072:     }
                   5073:     MKNDV(NV(p),m0,len,d);
                   5074:     SG(d) = SG(p);
                   5075:     return d;
                   5076: }
                   5077:
1.63      noro     5078: ND nd_dup(ND p)
                   5079: {
1.157     noro     5080:     ND d;
                   5081:     NM t,m,m0;
1.63      noro     5082:
1.157     noro     5083:     if ( !p ) return 0;
                   5084:     for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   5085:         NEXTNM(m0,m);
                   5086:         ndl_copy(DL(t),DL(m));
                   5087:         CQ(m) = CQ(t);
                   5088:     }
                   5089:     if ( m0 ) NEXT(m) = 0;
                   5090:     MKND(NV(p),m0,LEN(p),d);
                   5091:     SG(d) = SG(p);
                   5092:     return d;
1.63      noro     5093: }
                   5094:
1.215     noro     5095: ND ndtondgz(ND p)
                   5096: {
                   5097:        ND r;
                   5098:        NM t;
                   5099:
                   5100:        r = nd_dup(p);
                   5101:     for ( t = BDY(r); t; t = NEXT(t) ) CZ(t) = ztogz(CQ(t));
                   5102:        return r;
                   5103: }
                   5104:
                   5105:
                   5106: ND ndgztond(ND p)
                   5107: {
                   5108:        ND r;
                   5109:        NM t;
                   5110:
                   5111:        r = nd_dup(p);
                   5112:     for ( t = BDY(r); t; t = NEXT(t) ) CQ(t) = gztoz(CZ(t));
                   5113:        return r;
                   5114: }
                   5115:
                   5116:
1.61      noro     5117: /* XXX if p->len == 0 then it represents 0 */
                   5118:
                   5119: void ndv_mod(int mod,NDV p)
                   5120: {
1.157     noro     5121:     NMV t,d;
                   5122:     int r,s,u;
                   5123:     int i,len,dlen;
                   5124:     P cp;
                   5125:     Q c;
                   5126:     Obj gfs;
                   5127:
                   5128:     if ( !p ) return;
                   5129:     len = LEN(p);
                   5130:     dlen = 0;
                   5131:     if ( mod == -1 )
                   5132:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   5133:             simp_ff((Obj)CP(t),&gfs);
1.235   ! noro     5134:             if ( gfs ) {
        !          5135:               r = FTOIF(CONT((GFS)gfs));
        !          5136:               CM(d) = r;
        !          5137:               ndl_copy(DL(t),DL(d));
        !          5138:               NMV_ADV(d);
        !          5139:               dlen++;
        !          5140:             }
1.157     noro     5141:         }
1.232     noro     5142:     else if ( mod == -2 )
                   5143:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
1.235   ! noro     5144:             simp_ff((Obj)CP(t),&gfs);
        !          5145:             if ( gfs ) {
        !          5146:               lmtolf(gfs,&CZ(d));
        !          5147:               ndl_copy(DL(t),DL(d));
        !          5148:               NMV_ADV(d);
        !          5149:               dlen++;
        !          5150:             }
1.232     noro     5151:         }
1.157     noro     5152:     else
                   5153:         for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   5154:             if ( nd_vc ) {
                   5155:                 nd_subst_vector(nd_vc,CP(t),nd_subst,&cp);
                   5156:                 c = (Q)cp;
                   5157:             } else
                   5158:                 c = CQ(t);
                   5159:             r = rem(NM(c),mod);
                   5160:             if ( r ) {
                   5161:                 if ( SGN(c) < 0 )
                   5162:                     r = mod-r;
                   5163:                 if ( DN(c) ) {
                   5164:                     s = rem(DN(c),mod);
                   5165:                     if ( !s )
                   5166:                         error("ndv_mod : division by 0");
                   5167:                     s = invm(s,mod);
                   5168:                     DMAR(r,s,0,mod,u); r = u;
                   5169:                 }
                   5170:                 CM(d) = r;
                   5171:                 ndl_copy(DL(t),DL(d));
                   5172:                 NMV_ADV(d);
                   5173:                 dlen++;
                   5174:             }
                   5175:         }
                   5176:     LEN(p) = dlen;
1.61      noro     5177: }
                   5178:
                   5179: NDV ptondv(VL vl,VL dvl,P p)
                   5180: {
1.157     noro     5181:     ND nd;
                   5182:
                   5183:     nd = ptond(vl,dvl,p);
                   5184:     return ndtondv(0,nd);
                   5185: }
1.61      noro     5186:
1.157     noro     5187: void pltozpl(LIST l,Q *cont,LIST *pp)
                   5188: {
                   5189:     NODE nd,nd1;
                   5190:     int n;
                   5191:     P *pl;
                   5192:     Q *cl;
                   5193:     int i;
                   5194:     P dmy;
                   5195:     Q dvr;
                   5196:     LIST r;
                   5197:
                   5198:     nd = BDY(l); n = length(nd);
1.235   ! noro     5199:     pl = (P *)MALLOC(n*sizeof(P));
        !          5200:     cl = (Q *)MALLOC(n*sizeof(P));
1.157     noro     5201:     for ( i = 0; i < n; i++, nd = NEXT(nd) )
                   5202:         ptozp((P)BDY(nd),1,&cl[i],&dmy);
                   5203:     qltozl(cl,n,&dvr);
                   5204:     nd = BDY(l);
                   5205:     for ( i = 0; i < n; i++, nd = NEXT(nd) ) {
                   5206:         divsp(CO,(P)BDY(nd),(P)dvr,&pl[i]);
                   5207:     }
                   5208:     nd = 0;
                   5209:     for ( i = n-1; i >= 0; i-- ) {
                   5210:         MKNODE(nd1,pl[i],nd); nd = nd1;
                   5211:     }
                   5212:     MKLIST(r,nd);
                   5213:     *pp = r;
                   5214: }
                   5215:
                   5216: /* (a1,a2,...,an) -> a1*e(1)+...+an*e(n) */
                   5217:
                   5218: NDV pltondv(VL vl,VL dvl,LIST p)
                   5219: {
                   5220:     int i;
                   5221:     NODE t;
                   5222:     ND r,ri;
                   5223:     NM m;
                   5224:
                   5225:     if ( !nd_module ) error("pltond : module order must be set");
                   5226:     r = 0;
                   5227:     for ( i = 1, t = BDY(p); t; t = NEXT(t), i++ ) {
                   5228:         ri = ptond(vl,dvl,(P)BDY(t));
1.163     noro     5229:         if ( ri )
                   5230:             for ( m = BDY(ri); m; m = NEXT(m) ) {
1.167     noro     5231:                 MPOS(DL(m)) = i;
                   5232:                 TD(DL(m)) = ndl_weight(DL(m));
1.163     noro     5233:                 if ( nd_blockmask ) ndl_weight_mask(DL(m));
                   5234:             }
1.157     noro     5235:         r = nd_add(0,r,ri);
                   5236:     }
                   5237:     return ndtondv(0,r);
1.61      noro     5238: }
                   5239:
                   5240: ND ptond(VL vl,VL dvl,P p)
1.23      noro     5241: {
1.157     noro     5242:     int n,i,j,k,e;
                   5243:     VL tvl;
                   5244:     V v;
                   5245:     DCP dc;
                   5246:     DCP *w;
                   5247:     ND r,s,t,u;
                   5248:     P x;
                   5249:     int c;
                   5250:     UINT *d;
                   5251:     NM m,m0;
                   5252:
                   5253:     if ( !p )
                   5254:         return 0;
                   5255:     else if ( NUM(p) ) {
                   5256:         NEWNM(m);
                   5257:         ndl_zero(DL(m));
                   5258:         CQ(m) = (Q)p;
                   5259:         NEXT(m) = 0;
                   5260:         MKND(nd_nvar,m,1,r);
                   5261:         SG(r) = 0;
                   5262:         return r;
                   5263:     } else {
                   5264:         for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
1.235   ! noro     5265:         w = (DCP *)MALLOC(k*sizeof(DCP));
1.157     noro     5266:         for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   5267:         for ( i = 0, tvl = dvl, v = VR(p);
                   5268:             tvl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   5269:         if ( !tvl ) {
                   5270:             for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   5271:                 t = ptond(vl,dvl,COEF(w[j]));
                   5272:                 pwrp(vl,x,DEG(w[j]),&p);
                   5273:                 nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   5274:             }
                   5275:             return s;
                   5276:         } else {
                   5277:             NEWNM(m0); d = DL(m0);
                   5278:             for ( j = k-1, s = 0; j >= 0; j-- ) {
                   5279:                 ndl_zero(d); e = QTOS(DEG(w[j])); PUT_EXP(d,i,e);
                   5280:                 TD(d) = MUL_WEIGHT(e,i);
                   5281:                 if ( nd_blockmask) ndl_weight_mask(d);
                   5282:                 if ( nd_module ) MPOS(d) = 0;
                   5283:                 t = ptond(vl,dvl,COEF(w[j]));
                   5284:                 for ( m = BDY(t); m; m = NEXT(m) )
                   5285:                     ndl_addto(DL(m),d);
                   5286:                 SG(t) += TD(d);
                   5287:                 s = nd_add(0,s,t);
                   5288:             }
                   5289:             FREENM(m0);
                   5290:             return s;
                   5291:         }
                   5292:     }
1.61      noro     5293: }
                   5294:
                   5295: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   5296: {
1.157     noro     5297:     VL tvl;
                   5298:     int len,n,j,i,e;
                   5299:     NMV m;
                   5300:     Q q;
                   5301:     P c;
                   5302:     UINT *d;
                   5303:     P s,r,u,t,w;
                   5304:     GFS gfs;
                   5305:
                   5306:     if ( !p ) return 0;
                   5307:     else {
                   5308:         len = LEN(p);
                   5309:         n = NV(p);
                   5310:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   5311:         for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   5312:             if ( mod == -1 ) {
1.232     noro     5313:                e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   5314:             } else if ( mod == -2 ) {
                   5315:                c = gztoz(CZ(m));
                   5316:             } else if ( mod > 0 ) {
1.157     noro     5317:                 STOQ(CM(m),q); c = (P)q;
                   5318:             } else
                   5319:                 c = CP(m);
                   5320:             d = DL(m);
                   5321:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   5322:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   5323:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   5324:             }
                   5325:             addp(vl,s,t,&u); s = u;
                   5326:         }
                   5327:         return s;
                   5328:     }
                   5329: }
                   5330:
                   5331: LIST ndvtopl(int mod,VL vl,VL dvl,NDV p,int rank)
                   5332: {
                   5333:     VL tvl;
                   5334:     int len,n,j,i,e;
                   5335:     NMV m;
                   5336:     Q q;
                   5337:     P c;
                   5338:     UINT *d;
                   5339:     P s,r,u,t,w;
                   5340:     GFS gfs;
                   5341:     P *a;
                   5342:     LIST l;
                   5343:     NODE nd,nd1;
                   5344:
                   5345:     if ( !p ) return 0;
                   5346:     else {
1.235   ! noro     5347:         a = (P *)MALLOC((rank+1)*sizeof(P));
1.157     noro     5348:         for ( i = 0; i <= rank; i++ ) a[i] = 0;
                   5349:         len = LEN(p);
                   5350:         n = NV(p);
                   5351:         m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   5352:         for ( j = len-1; j >= 0; j--, NMV_PREV(m) ) {
                   5353:             if ( mod == -1 ) {
                   5354:                 e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   5355:             } else if ( mod ) {
                   5356:                 STOQ(CM(m),q); c = (P)q;
                   5357:             } else
                   5358:                 c = CP(m);
                   5359:             d = DL(m);
                   5360:             for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   5361:                 MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   5362:                 pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   5363:             }
                   5364:             addp(vl,a[MPOS(d)],t,&u); a[MPOS(d)] = u;
                   5365:         }
                   5366:         nd = 0;
                   5367:         for ( i = rank; i > 0; i-- ) {
                   5368:             MKNODE(nd1,a[i],nd); nd = nd1;
                   5369:         }
                   5370:         MKLIST(l,nd);
                   5371:         return l;
                   5372:     }
1.3       noro     5373: }
                   5374:
1.61      noro     5375: NDV ndtondv(int mod,ND p)
1.11      noro     5376: {
1.157     noro     5377:     NDV d;
                   5378:     NMV m,m0;
                   5379:     NM t;
                   5380:     int i,len;
                   5381:
                   5382:     if ( !p ) return 0;
                   5383:     len = LEN(p);
1.232     noro     5384:     if ( mod > 0 || mod == -1 )
1.200     noro     5385:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(len*nmv_adv);
1.157     noro     5386:     else
                   5387:         m0 = m = MALLOC(len*nmv_adv);
1.103     noro     5388: #if 0
1.157     noro     5389:     ndv_alloc += nmv_adv*len;
1.103     noro     5390: #endif
1.157     noro     5391:     for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   5392:         ndl_copy(DL(t),DL(m));
                   5393:         CQ(m) = CQ(t);
                   5394:     }
                   5395:     MKNDV(NV(p),m0,len,d);
                   5396:     SG(d) = SG(p);
                   5397:     return d;
1.11      noro     5398: }
                   5399:
1.61      noro     5400: ND ndvtond(int mod,NDV p)
1.11      noro     5401: {
1.157     noro     5402:     ND d;
                   5403:     NM m,m0;
                   5404:     NMV t;
                   5405:     int i,len;
                   5406:
                   5407:     if ( !p ) return 0;
                   5408:     m0 = 0;
                   5409:     len = p->len;
                   5410:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   5411:         NEXTNM(m0,m);
                   5412:         ndl_copy(DL(t),DL(m));
                   5413:         CQ(m) = CQ(t);
                   5414:     }
                   5415:     NEXT(m) = 0;
                   5416:     MKND(NV(p),m0,len,d);
                   5417:     SG(d) = SG(p);
                   5418:     return d;
1.11      noro     5419: }
                   5420:
1.198     noro     5421: DP ndvtodp(int mod,NDV p)
                   5422: {
                   5423:     MP m,m0;
                   5424:        DP d;
                   5425:     NMV t;
                   5426:     int i,len;
                   5427:
                   5428:     if ( !p ) return 0;
                   5429:     m0 = 0;
                   5430:     len = p->len;
                   5431:     for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   5432:         NEXTMP(m0,m);
                   5433:        m->dl = ndltodl(nd_nvar,DL(t));
                   5434:        m->c = ndctop(mod,t->c);
                   5435:     }
                   5436:     NEXT(m) = 0;
                   5437:        MKDP(nd_nvar,m0,d);
                   5438:     SG(d) = SG(p);
                   5439:     return d;
                   5440: }
                   5441:
1.204     noro     5442: DP ndtodp(int mod,ND p)
                   5443: {
                   5444:     MP m,m0;
                   5445:        DP d;
                   5446:     NM t;
                   5447:     int i,len;
                   5448:
                   5449:     if ( !p ) return 0;
                   5450:     m0 = 0;
                   5451:     len = p->len;
                   5452:     for ( t = BDY(p); t; t = NEXT(t) ) {
                   5453:         NEXTMP(m0,m);
                   5454:        m->dl = ndltodl(nd_nvar,DL(t));
                   5455:        m->c = ndctop(mod,t->c);
                   5456:     }
                   5457:     NEXT(m) = 0;
                   5458:        MKDP(nd_nvar,m0,d);
                   5459:     SG(d) = SG(p);
                   5460:     return d;
                   5461: }
                   5462:
1.3       noro     5463: void ndv_print(NDV p)
                   5464: {
1.157     noro     5465:     NMV m;
                   5466:     int i,len;
1.3       noro     5467:
1.157     noro     5468:     if ( !p ) printf("0\n");
                   5469:     else {
                   5470:         len = LEN(p);
                   5471:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   5472:             if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   5473:             else printf("+%d*",CM(m));
                   5474:             ndl_print(DL(m));
                   5475:         }
                   5476:         printf("\n");
                   5477:     }
1.16      noro     5478: }
                   5479:
1.113     noro     5480: void ndv_print_q(NDV p)
1.16      noro     5481: {
1.157     noro     5482:     NMV m;
                   5483:     int i,len;
1.16      noro     5484:
1.157     noro     5485:     if ( !p ) printf("0\n");
                   5486:     else {
                   5487:         len = LEN(p);
                   5488:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   5489:             printf("+");
                   5490:             printexpr(CO,(Obj)CQ(m));
                   5491:             printf("*");
                   5492:             ndl_print(DL(m));
                   5493:         }
                   5494:         printf("\n");
                   5495:     }
1.25      noro     5496: }
                   5497:
1.167     noro     5498: NODE ndv_reducebase(NODE x,int *perm)
1.27      noro     5499: {
1.157     noro     5500:     int len,i,j;
1.167     noro     5501:     NDVI w;
1.157     noro     5502:     NODE t,t0;
                   5503:
                   5504:     len = length(x);
1.235   ! noro     5505:     w = (NDVI)MALLOC(len*sizeof(struct oNDVI));
1.167     noro     5506:     for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) {
                   5507:         w[i].p = BDY(t); w[i].i = perm[i];
                   5508:     }
1.157     noro     5509:     for ( i = 0; i < len; i++ ) {
                   5510:         for ( j = 0; j < i; j++ ) {
1.167     noro     5511:             if ( w[i].p && w[j].p )
                   5512:                 if ( ndl_reducible(HDL(w[i].p),HDL(w[j].p)) ) w[i].p = 0;
                   5513:                 else if ( ndl_reducible(HDL(w[j].p),HDL(w[i].p)) ) w[j].p = 0;
1.157     noro     5514:         }
                   5515:     }
1.167     noro     5516:     for ( i = j = 0, t0 = 0; i < len; i++ ) {
                   5517:         if ( w[i].p ) {
                   5518:             NEXTNODE(t0,t); BDY(t) = (pointer)w[i].p;
                   5519:             perm[j++] = w[i].i;
                   5520:         }
1.157     noro     5521:     }
                   5522:     NEXT(t) = 0; x = t0;
                   5523:     return x;
1.11      noro     5524: }
1.32      noro     5525:
1.43      noro     5526: /* XXX incomplete */
                   5527:
1.32      noro     5528: void nd_init_ord(struct order_spec *ord)
                   5529: {
1.224     noro     5530:   nd_module = (ord->id >= 256);
                   5531:   if ( nd_module ) {
                   5532:     nd_dcomp = -1;
                   5533:     nd_ispot = ord->ispot;
                   5534:     nd_pot_nelim = ord->pot_nelim;
                   5535:     nd_poly_weight_len = ord->nv;
                   5536:     nd_poly_weight = ord->top_weight;
                   5537:     nd_module_rank = ord->module_rank;
                   5538:     nd_module_weight = ord->module_top_weight;
                   5539:   }
1.203     noro     5540:        nd_matrix = 0;
                   5541:        nd_matrix_len = 0;
1.157     noro     5542:     switch ( ord->id ) {
                   5543:         case 0:
                   5544:             switch ( ord->ord.simple ) {
                   5545:                 case 0:
                   5546:                     nd_dcomp = 1;
                   5547:                     nd_isrlex = 1;
                   5548:                     break;
                   5549:                 case 1:
                   5550:                     nd_dcomp = 1;
                   5551:                     nd_isrlex = 0;
                   5552:                     break;
                   5553:                 case 2:
                   5554:                     nd_dcomp = 0;
                   5555:                     nd_isrlex = 0;
                   5556:                     ndl_compare_function = ndl_lex_compare;
                   5557:                     break;
                   5558:                 case 11:
                   5559:                     /* XXX */
                   5560:                     nd_dcomp = 0;
                   5561:                     nd_isrlex = 1;
                   5562:                     ndl_compare_function = ndl_ww_lex_compare;
                   5563:                     break;
                   5564:                 default:
                   5565:                     error("nd_gr : unsupported order");
                   5566:             }
                   5567:             break;
                   5568:         case 1:
                   5569:             /* block order */
                   5570:             /* XXX */
                   5571:             nd_dcomp = -1;
                   5572:             nd_isrlex = 0;
                   5573:             ndl_compare_function = ndl_block_compare;
                   5574:             break;
                   5575:         case 2:
                   5576:             /* matrix order */
                   5577:             /* XXX */
                   5578:             nd_dcomp = -1;
                   5579:             nd_isrlex = 0;
                   5580:             nd_matrix_len = ord->ord.matrix.row;
                   5581:             nd_matrix = ord->ord.matrix.matrix;
                   5582:             ndl_compare_function = ndl_matrix_compare;
                   5583:             break;
                   5584:         case 3:
                   5585:             /* composite order */
                   5586:             nd_dcomp = -1;
                   5587:             nd_isrlex = 0;
                   5588:             nd_worb_len = ord->ord.composite.length;
                   5589:             nd_worb = ord->ord.composite.w_or_b;
                   5590:             ndl_compare_function = ndl_composite_compare;
                   5591:             break;
                   5592:
                   5593:         /* module order */
                   5594:         case 256:
                   5595:             switch ( ord->ord.simple ) {
                   5596:                 case 0:
1.167     noro     5597:                     nd_isrlex = 1;
1.157     noro     5598:                     ndl_compare_function = ndl_module_grlex_compare;
                   5599:                     break;
                   5600:                 case 1:
1.167     noro     5601:                     nd_isrlex = 0;
1.157     noro     5602:                     ndl_compare_function = ndl_module_glex_compare;
                   5603:                     break;
                   5604:                 case 2:
1.167     noro     5605:                     nd_isrlex = 0;
1.157     noro     5606:                     ndl_compare_function = ndl_module_lex_compare;
                   5607:                     break;
                   5608:                 default:
                   5609:                     error("nd_gr : unsupported order");
                   5610:             }
                   5611:             break;
                   5612:         case 257:
                   5613:             /* block order */
1.174     noro     5614:             nd_isrlex = 0;
1.157     noro     5615:             ndl_compare_function = ndl_module_block_compare;
                   5616:             break;
                   5617:         case 258:
                   5618:             /* matrix order */
1.174     noro     5619:             nd_isrlex = 0;
1.157     noro     5620:             nd_matrix_len = ord->ord.matrix.row;
                   5621:             nd_matrix = ord->ord.matrix.matrix;
                   5622:             ndl_compare_function = ndl_module_matrix_compare;
                   5623:             break;
                   5624:         case 259:
                   5625:             /* composite order */
1.174     noro     5626:             nd_isrlex = 0;
1.157     noro     5627:             nd_worb_len = ord->ord.composite.length;
                   5628:             nd_worb = ord->ord.composite.w_or_b;
                   5629:             ndl_compare_function = ndl_module_composite_compare;
                   5630:             break;
                   5631:     }
                   5632:     nd_ord = ord;
1.32      noro     5633: }
                   5634:
1.43      noro     5635: BlockMask nd_create_blockmask(struct order_spec *ord)
                   5636: {
1.157     noro     5637:     int n,i,j,s,l;
                   5638:     UINT *t;
                   5639:     BlockMask bm;
                   5640:
                   5641:     /* we only create mask table for block order */
1.164     noro     5642:     if ( ord->id != 1 && ord->id != 257 )
1.157     noro     5643:         return 0;
                   5644:     n = ord->ord.block.length;
                   5645:     bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   5646:     bm->n = n;
                   5647:     bm->order_pair = ord->ord.block.order_pair;
                   5648:     bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
                   5649:     for ( i = 0, s = 0; i < n; i++ ) {
                   5650:         bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
                   5651:         for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   5652:         l = bm->order_pair[i].length;
                   5653:         for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   5654:     }
                   5655:     return bm;
1.57      noro     5656: }
                   5657:
                   5658: EPOS nd_create_epos(struct order_spec *ord)
                   5659: {
1.157     noro     5660:     int i,j,l,s,ord_l,ord_o;
                   5661:     EPOS epos;
                   5662:     struct order_pair *op;
                   5663:
                   5664:     epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   5665:     switch ( ord->id ) {
1.164     noro     5666:         case 0: case 256:
1.157     noro     5667:             if ( nd_isrlex ) {
                   5668:                 for ( i = 0; i < nd_nvar; i++ ) {
                   5669:                     epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   5670:                     epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   5671:                 }
                   5672:             } else {
                   5673:                 for ( i = 0; i < nd_nvar; i++ ) {
                   5674:                     epos[i].i = nd_exporigin + i/nd_epw;
                   5675:                     epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   5676:                 }
                   5677:             }
                   5678:             break;
1.164     noro     5679:         case 1: case 257:
1.157     noro     5680:             /* block order */
                   5681:             l = ord->ord.block.length;
                   5682:             op = ord->ord.block.order_pair;
                   5683:             for ( j = 0, s = 0; j < l; j++ ) {
                   5684:                 ord_o = op[j].order;
                   5685:                 ord_l = op[j].length;
                   5686:                 if ( !ord_o )
                   5687:                     for ( i = 0; i < ord_l; i++ ) {
                   5688:                         epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   5689:                         epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   5690:                     }
                   5691:                 else
                   5692:                     for ( i = 0; i < ord_l; i++ ) {
                   5693:                         epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   5694:                         epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   5695:                     }
                   5696:                 s += ord_l;
                   5697:             }
                   5698:             break;
                   5699:         case 2:
                   5700:             /* matrix order */
                   5701:         case 3:
                   5702:             /* composite order */
1.167     noro     5703:         default:
1.157     noro     5704:             for ( i = 0; i < nd_nvar; i++ ) {
                   5705:                 epos[i].i = nd_exporigin + i/nd_epw;
                   5706:                 epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   5707:             }
                   5708:             break;
                   5709:     }
                   5710:     return epos;
1.43      noro     5711: }
1.59      noro     5712:
                   5713: /* external interface */
                   5714:
1.191     noro     5715: void nd_nf_p(Obj f,LIST g,LIST v,int m,struct order_spec *ord,Obj *rp)
1.59      noro     5716: {
1.157     noro     5717:     NODE t,in0,in;
1.191     noro     5718:     ND ndf,nf;
                   5719:     NDV ndvf;
1.157     noro     5720:     VL vv,tv;
1.191     noro     5721:     int stat,nvar,max,mrank;
1.157     noro     5722:     union oNDC dn;
                   5723:     Q cont;
                   5724:     P pp;
1.191     noro     5725:     LIST ppl;
1.157     noro     5726:
                   5727:     if ( !f ) {
                   5728:         *rp = 0;
                   5729:         return;
                   5730:     }
                   5731:     pltovl(v,&vv);
                   5732:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   5733:
1.191     noro     5734:     /* max=65536 implies nd_bpe=32 */
                   5735:     max = 65536;
1.157     noro     5736:
1.191     noro     5737:        nd_module = 0;
                   5738:        /* nd_module will be set if ord is a module ordering */
1.157     noro     5739:     nd_init_ord(ord);
                   5740:     nd_setup_parameters(nvar,max);
1.191     noro     5741:     if ( nd_module && OID(f) != O_LIST )
                   5742:         error("nd_nf_p : the first argument must be a list");
                   5743:        if ( nd_module ) mrank = length(BDY((LIST)f));
1.157     noro     5744:     /* conversion to ndv */
                   5745:     for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   5746:         NEXTNODE(in0,in);
1.191     noro     5747:         if ( nd_module ) {
                   5748:           if ( !BDY(t) || OID(BDY(t)) != O_LIST
                   5749:                || length(BDY((LIST)BDY(t))) != mrank )
                   5750:               error("nd_nf_p : inconsistent basis element");
                   5751:           if ( !m ) pltozpl((LIST)BDY(t),&cont,&ppl);
                   5752:           else ppl = (LIST)BDY(t);
                   5753:           BDY(in) = (pointer)pltondv(CO,vv,ppl);
                   5754:         } else {
                   5755:           if ( !m ) ptozp((P)BDY(t),1,&cont,&pp);
                   5756:           else pp = (P)BDY(t);
                   5757:           BDY(in) = (pointer)ptondv(CO,vv,pp);
                   5758:         }
1.157     noro     5759:         if ( m ) ndv_mod(m,(NDV)BDY(in));
                   5760:     }
1.191     noro     5761:     if ( in0 ) NEXT(in) = 0;
                   5762:
                   5763:     if ( nd_module ) ndvf = pltondv(CO,vv,(LIST)f);
                   5764:     else ndvf = ptondv(CO,vv,(P)f);
                   5765:     if ( m ) ndv_mod(m,ndvf);
                   5766:     ndf = (pointer)ndvtond(m,ndvf);
1.157     noro     5767:
                   5768:     /* dont sort, dont removecont */
                   5769:     ndv_setup(m,0,in0,1,1);
                   5770:     nd_scale=2;
1.191     noro     5771:     stat = nd_nf(m,0,ndf,nd_ps,1,0,&nf);
                   5772:     if ( !stat )
                   5773:         error("nd_nf_p : exponent too large");
                   5774:     if ( nd_module ) *rp = (Obj)ndvtopl(m,CO,vv,ndtondv(m,nf),mrank);
                   5775:     else *rp = (Obj)ndvtop(m,CO,vv,ndtondv(m,nf));
1.63      noro     5776: }
                   5777:
                   5778: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   5779: {
1.157     noro     5780:     NM m;
                   5781:     UINT *t,*s;
                   5782:     int i;
                   5783:
                   5784:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   5785:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   5786:         t = DL(m);
                   5787:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5788:         r[i] = CM(m);
                   5789:     }
                   5790:     for ( i = 0; !r[i]; i++ );
                   5791:     return i;
1.63      noro     5792: }
                   5793:
1.113     noro     5794: int nd_to_vect_q(UINT *s0,int n,ND d,Q *r)
1.74      noro     5795: {
1.157     noro     5796:     NM m;
                   5797:     UINT *t,*s;
                   5798:     int i;
                   5799:
                   5800:     for ( i = 0; i < n; i++ ) r[i] = 0;
                   5801:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   5802:         t = DL(m);
                   5803:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5804:         r[i] = CQ(m);
                   5805:     }
                   5806:     for ( i = 0; !r[i]; i++ );
                   5807:     return i;
1.74      noro     5808: }
                   5809:
1.232     noro     5810: int nd_to_vect_lf(UINT *s0,int n,ND d,mpz_t *r)
                   5811: {
                   5812:     NM m;
                   5813:     UINT *t,*s;
                   5814:     int i;
                   5815:
                   5816:     for ( i = 0; i < n; i++ ) { mpz_init(r[i]); mpz_set_ui(r[i],0); }
                   5817:     for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   5818:         t = DL(m);
                   5819:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5820:         mpz_set(r[i],BDY(CZ(m)));
                   5821:     }
                   5822:     for ( i = 0; !mpz_sgn(r[i]); i++ );
                   5823:     return i;
                   5824: }
                   5825:
1.220     noro     5826: unsigned long *nd_to_vect_2(UINT *s0,int n,int *s0hash,ND p)
                   5827: {
                   5828:     NM m;
                   5829:     unsigned long *v;
                   5830:     int i,j,h,size;
                   5831:        UINT *s,*t;
                   5832:
                   5833:        size = sizeof(unsigned long)*(n+BLEN-1)/BLEN;
                   5834:     v = (unsigned long *)MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
                   5835:     bzero(v,size);
                   5836:     for ( i = j = 0, s = s0, m = BDY(p); m; j++, m = NEXT(m) ) {
                   5837:                t = DL(m);
                   5838:                h = ndl_hash_value(t);
                   5839:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   5840:            v[i/BLEN] |= 1L <<(i%BLEN);
                   5841:     }
                   5842:     return v;
                   5843: }
                   5844:
                   5845: int nd_nm_to_vect_2(UINT *s0,int n,int *s0hash,NDV p,NM m,unsigned long *v)
                   5846: {
                   5847:     NMV mr;
                   5848:     UINT *d,*t,*s;
                   5849:     int i,j,len,h,head;
                   5850:
                   5851:     d = DL(m);
                   5852:     len = LEN(p);
1.235   ! noro     5853:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
1.220     noro     5854:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   5855:         ndl_add(d,DL(mr),t);
                   5856:                h = ndl_hash_value(t);
                   5857:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
                   5858:                if ( j == 0 ) head = i;
                   5859:            v[i/BLEN] |= 1L <<(i%BLEN);
                   5860:     }
                   5861:     return head;
                   5862: }
                   5863:
1.129     noro     5864: Q *nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair)
                   5865: {
1.157     noro     5866:     NM m;
                   5867:     NMV mr;
                   5868:     UINT *d,*t,*s;
                   5869:     NDV p;
                   5870:     int i,j,len;
                   5871:     Q *r;
                   5872:
                   5873:     m = pair->mul;
                   5874:     d = DL(m);
                   5875:     p = nd_ps[pair->index];
                   5876:     len = LEN(p);
                   5877:     r = (Q *)CALLOC(n,sizeof(Q));
1.235   ! noro     5878:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
1.157     noro     5879:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   5880:         ndl_add(d,DL(mr),t);
                   5881:         for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   5882:         r[i] = CQ(mr);
                   5883:     }
                   5884:     return r;
1.129     noro     5885: }
                   5886:
1.235   ! noro     5887: IndArray nm_ind_pair_to_vect_compress(int trace,UINT *s0,int n,int *s0hash,NM_ind_pair pair)
1.64      noro     5888: {
1.157     noro     5889:     NM m;
                   5890:     NMV mr;
                   5891:     UINT *d,*t,*s;
                   5892:     NDV p;
                   5893:     unsigned char *ivc;
                   5894:     unsigned short *ivs;
                   5895:     UINT *v,*ivi,*s0v;
1.210     noro     5896:     int i,j,len,prev,diff,cdiff,h;
1.157     noro     5897:     IndArray r;
1.198     noro     5898: struct oEGT eg0,eg1;
1.157     noro     5899:
                   5900:     m = pair->mul;
                   5901:     d = DL(m);
1.235   ! noro     5902:     if ( trace )
        !          5903:       p = nd_demand?nd_ps_trace_sym[pair->index]:nd_ps_trace[pair->index];
        !          5904:     else
        !          5905:       p = nd_demand?nd_ps_sym[pair->index]:nd_ps[pair->index];
        !          5906:
1.157     noro     5907:     len = LEN(p);
1.235   ! noro     5908:     t = (UINT *)MALLOC(nd_wpd*sizeof(UINT));
        !          5909:     v = (unsigned int *)MALLOC(len*sizeof(unsigned int));
1.198     noro     5910: get_eg(&eg0);
1.157     noro     5911:     for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   5912:         ndl_add(d,DL(mr),t);
1.210     noro     5913:                h = ndl_hash_value(t);
                   5914:         for ( ; h != s0hash[i] || !ndl_equal(t,s); s += nd_wpd, i++ );
1.157     noro     5915:         v[j] = i;
                   5916:     }
1.198     noro     5917: get_eg(&eg1); add_eg(&eg_search,&eg0,&eg1);
1.157     noro     5918:     r = (IndArray)MALLOC(sizeof(struct oIndArray));
                   5919:     r->head = v[0];
                   5920:     diff = 0;
                   5921:     for ( i = 1; i < len; i++ ) {
                   5922:         cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   5923:     }
                   5924:     if ( diff < 256 ) {
                   5925:         r->width = 1;
                   5926:         ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   5927:         r->index.c = ivc;
                   5928:         for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   5929:     } else if ( diff < 65536 ) {
                   5930:         r->width = 2;
                   5931:         ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   5932:         r->index.s = ivs;
                   5933:         for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   5934:     } else {
                   5935:         r->width = 4;
                   5936:         ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   5937:         r->index.i = ivi;
                   5938:         for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
                   5939:     }
                   5940:     return r;
1.64      noro     5941: }
                   5942:
1.135     noro     5943: int compress_array(Q *svect,Q *cvect,int n)
                   5944: {
1.157     noro     5945:     int i,j;
1.135     noro     5946:
1.157     noro     5947:     for ( i = j = 0; i < n; i++ )
                   5948:         if ( svect[i] ) cvect[j++] = svect[i];
                   5949:     return j;
1.135     noro     5950: }
                   5951:
                   5952: void expand_array(Q *svect,Q *cvect,int n)
                   5953: {
1.157     noro     5954:     int i,j;
1.135     noro     5955:
1.157     noro     5956:     for ( i = j = 0; j < n;  i++  )
                   5957:         if ( svect[i] ) svect[i] = cvect[j++];
1.135     noro     5958: }
                   5959:
1.133     noro     5960: int ndv_reduce_vect_q(Q *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.107     noro     5961: {
1.157     noro     5962:     int i,j,k,len,pos,prev,nz;
                   5963:     Q cs,mcs,c1,c2,cr,gcd,t;
                   5964:     IndArray ivect;
                   5965:     unsigned char *ivc;
                   5966:     unsigned short *ivs;
                   5967:     unsigned int *ivi;
                   5968:     NDV redv;
                   5969:     NMV mr;
                   5970:     NODE rp;
                   5971:     int maxrs;
                   5972:     double hmag;
                   5973:     Q *cvect;
                   5974:
                   5975:     maxrs = 0;
                   5976:     for ( i = 0; i < col && !svect[i]; i++ );
                   5977:     if ( i == col ) return maxrs;
                   5978:     hmag = p_mag((P)svect[i])*nd_scale;
1.235   ! noro     5979:     cvect = (Q *)MALLOC(col*sizeof(Q));
1.157     noro     5980:     for ( i = 0; i < nred; i++ ) {
                   5981:         ivect = imat[i];
                   5982:         k = ivect->head;
                   5983:         if ( svect[k] ) {
                   5984:             maxrs = MAX(maxrs,rp0[i]->sugar);
1.215     noro     5985:             redv = nd_demand?ndv_load(rp0[i]->index)
                   5986:                      :(trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index]);
1.157     noro     5987:             len = LEN(redv); mr = BDY(redv);
                   5988:             igcd_cofactor(svect[k],CQ(mr),&gcd,&cs,&cr);
                   5989:             chsgnq(cs,&mcs);
                   5990:             if ( !UNIQ(cr) ) {
                   5991:                 for ( j = 0; j < col; j++ ) {
                   5992:                     mulq(svect[j],cr,&c1); svect[j] = c1;
                   5993:                 }
                   5994:             }
                   5995:             svect[k] = 0; prev = k;
                   5996:             switch ( ivect->width ) {
                   5997:                 case 1:
                   5998:                     ivc = ivect->index.c;
                   5999:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6000:                         pos = prev+ivc[j]; prev = pos;
                   6001:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   6002:                     }
                   6003:                     break;
                   6004:                 case 2:
                   6005:                     ivs = ivect->index.s;
                   6006:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6007:                         pos = prev+ivs[j]; prev = pos;
                   6008:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   6009:                     }
                   6010:                     break;
                   6011:                 case 4:
                   6012:                     ivi = ivect->index.i;
                   6013:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6014:                         pos = prev+ivi[j]; prev = pos;
                   6015:                         mulq(CQ(mr),mcs,&c2); addq(svect[pos],c2,&t); svect[pos] = t;
                   6016:                     }
                   6017:                     break;
                   6018:             }
                   6019:             for ( j = k+1; j < col && !svect[j]; j++ );
                   6020:             if ( j == col ) break;
                   6021:             if ( hmag && ((double)p_mag((P)svect[j]) > hmag) ) {
                   6022:                 nz = compress_array(svect,cvect,col);
                   6023:                 removecont_array((P *)cvect,nz,1);
                   6024:                 expand_array(svect,cvect,nz);
                   6025:                 hmag = ((double)p_mag((P)svect[j]))*nd_scale;
                   6026:             }
                   6027:         }
                   6028:     }
                   6029:     nz = compress_array(svect,cvect,col);
                   6030:     removecont_array((P *)cvect,nz,1);
                   6031:     expand_array(svect,cvect,nz);
                   6032:     if ( DP_Print ) {
                   6033:         fprintf(asir_out,"-"); fflush(asir_out);
                   6034:     }
                   6035:     return maxrs;
1.107     noro     6036: }
                   6037:
1.215     noro     6038: int ndv_reduce_vect_gz(GZ *gvect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
                   6039: {
                   6040:     int i,j,k,l,len,pos,prev,nz;
                   6041:     GZ cs,mcs,c1,c2,cr,gcd,t;
                   6042:     IndArray ivect;
                   6043:     unsigned char *ivc;
                   6044:     unsigned short *ivs;
                   6045:     unsigned int *ivi;
                   6046:     NDV redv;
                   6047:     NMV mr;
                   6048:     NODE rp;
                   6049:     int maxrs;
                   6050:     double hmag;
                   6051:        struct oVECT v;
                   6052:
                   6053:     maxrs = 0;
                   6054:     for ( i = 0; i < col && !gvect[i]; i++ );
                   6055:     if ( i == col ) return maxrs;
                   6056:     hmag = (double)n_bits_gz(gvect[i])*nd_scale;
                   6057:     for ( i = 0; i < nred; i++ ) {
                   6058:         ivect = imat[i];
                   6059:         k = ivect->head;
                   6060:         if ( gvect[k] ) {
                   6061:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   6062:             redv = nd_ps_gz[rp0[i]->index];
                   6063:             len = LEN(redv); mr = BDY(redv);
                   6064:                        gcdgz(gvect[k],CZ(mr),&gcd);
                   6065:                        divsgz(gvect[k],gcd,&cs);
                   6066:                        divsgz(CZ(mr),gcd,&cr);
                   6067:             chsgngz(cs,&mcs);
                   6068:                        if ( !UNIGZ(cr) ) {
                   6069:                for ( j = 0; j < col; j++ ) {
                   6070:                     mulgz(gvect[j],cr,&c1); gvect[j] = c1;
                   6071:                 }
                   6072:             }
                   6073:             gvect[k] = 0; prev = k;
                   6074:             switch ( ivect->width ) {
                   6075:                 case 1:
                   6076:                     ivc = ivect->index.c;
                   6077:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6078:                         pos = prev+ivc[j]; prev = pos;
                   6079:                         mulgz(CZ(mr),mcs,&c2); addgz(gvect[pos],c2,&t); gvect[pos] = t;
                   6080:                     }
                   6081:                     break;
                   6082:                 case 2:
                   6083:                     ivs = ivect->index.s;
                   6084:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6085:                         pos = prev+ivs[j]; prev = pos;
                   6086:                         mulgz(CZ(mr),mcs,&c2); addgz(gvect[pos],c2,&t); gvect[pos] = t;
                   6087:                     }
                   6088:                     break;
                   6089:                 case 4:
                   6090:                     ivi = ivect->index.i;
                   6091:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6092:                         pos = prev+ivi[j]; prev = pos;
                   6093:                         mulgz(CZ(mr),mcs,&c2); addgz(gvect[pos],c2,&t); gvect[pos] = t;
                   6094:                     }
                   6095:                     break;
                   6096:             }
                   6097:             for ( j = k+1; j < col && !gvect[j]; j++ );
                   6098:             if ( j == col ) break;
                   6099:             if ( hmag && ((double)n_bits_gz(gvect[j]) > hmag) ) {
                   6100:                                v.len = col; v.body = (pointer)gvect; gcdvgz(&v,&gcd);
                   6101: #if 1
                   6102:                                for ( l = 0; l < col; l++ ) { divsgz(gvect[l],gcd,&t); gvect[l] = t; }
                   6103: #endif
                   6104:                        hmag = (double)n_bits_gz(gvect[j])*nd_scale;
                   6105:             }
                   6106:         }
                   6107:     }
                   6108:     for ( j = 0; j < col && !gvect[j]; j++ );
                   6109:        if ( j < col ) {
                   6110:                v.len = col; v.body = (pointer)gvect; gcdvgz(&v,&gcd);
                   6111:                for ( l = 0; l < col; l++ ) { divsgz(gvect[l],gcd,&t); gvect[l] = t; }
                   6112:        }
                   6113:     if ( DP_Print ) {
                   6114:         fprintf(asir_out,"-"); fflush(asir_out);
                   6115:     }
                   6116:     return maxrs;
                   6117: }
                   6118:
                   6119:
1.76      noro     6120: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.65      noro     6121: {
1.157     noro     6122:     int i,j,k,len,pos,prev;
                   6123:     UINT c,c1,c2,c3,up,lo,dmy;
                   6124:     IndArray ivect;
                   6125:     unsigned char *ivc;
                   6126:     unsigned short *ivs;
                   6127:     unsigned int *ivi;
                   6128:     NDV redv;
                   6129:     NMV mr;
                   6130:     NODE rp;
                   6131:     int maxrs;
                   6132:
                   6133:     maxrs = 0;
                   6134:     for ( i = 0; i < nred; i++ ) {
                   6135:         ivect = imat[i];
                   6136:         k = ivect->head; svect[k] %= m;
                   6137:         if ( c = svect[k] ) {
                   6138:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   6139:             c = m-c; redv = nd_ps[rp0[i]->index];
                   6140:             len = LEN(redv); mr = BDY(redv);
                   6141:             svect[k] = 0; prev = k;
                   6142:             switch ( ivect->width ) {
                   6143:                 case 1:
                   6144:                     ivc = ivect->index.c;
                   6145:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
1.215     noro     6146:                         pos = prev+ivc[j]; c1 = CM(mr); prev = pos;
                   6147:                                                if ( c1 ) {
                   6148:                                                        c2 = svect[pos];
                   6149:                                DMA(c1,c,c2,up,lo);
                   6150:                                if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   6151:                                } else svect[pos] = lo;
                   6152:                                                }
1.157     noro     6153:                     }
                   6154:                     break;
                   6155:                 case 2:
                   6156:                     ivs = ivect->index.s;
                   6157:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
1.215     noro     6158:                         pos = prev+ivs[j]; c1 = CM(mr);
1.157     noro     6159:                         prev = pos;
1.215     noro     6160:                                                if ( c1 ) {
                   6161:                                                        c2 = svect[pos];
                   6162:                                DMA(c1,c,c2,up,lo);
                   6163:                                if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   6164:                                } else svect[pos] = lo;
                   6165:                                                }
1.157     noro     6166:                     }
                   6167:                     break;
                   6168:                 case 4:
                   6169:                     ivi = ivect->index.i;
                   6170:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
1.215     noro     6171:                         pos = prev+ivi[j]; c1 = CM(mr);
1.157     noro     6172:                         prev = pos;
1.215     noro     6173:                                                if ( c1 ) {
                   6174:                                                        c2 = svect[pos];
                   6175:                                DMA(c1,c,c2,up,lo);
                   6176:                                if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   6177:                                } else svect[pos] = lo;
                   6178:                                                }
1.157     noro     6179:                     }
                   6180:                     break;
                   6181:             }
                   6182:         }
                   6183:     }
                   6184:     for ( i = 0; i < col; i++ )
                   6185:         if ( svect[i] >= (UINT)m ) svect[i] %= m;
                   6186:     return maxrs;
1.65      noro     6187: }
                   6188:
1.76      noro     6189: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.72      noro     6190: {
1.157     noro     6191:     int i,j,k,len,pos,prev;
                   6192:     UINT c,c1,c2,c3,up,lo,dmy;
                   6193:     IndArray ivect;
                   6194:     unsigned char *ivc;
                   6195:     unsigned short *ivs;
                   6196:     unsigned int *ivi;
                   6197:     NDV redv;
                   6198:     NMV mr;
                   6199:     NODE rp;
                   6200:     int maxrs;
                   6201:
                   6202:     maxrs = 0;
                   6203:     for ( i = 0; i < nred; i++ ) {
                   6204:         ivect = imat[i];
1.232     noro     6205:         k = ivect->head;
1.157     noro     6206:         if ( c = svect[k] ) {
                   6207:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   6208:             c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
                   6209:             len = LEN(redv); mr = BDY(redv);
                   6210:             svect[k] = 0; prev = k;
                   6211:             switch ( ivect->width ) {
                   6212:                 case 1:
                   6213:                     ivc = ivect->index.c;
                   6214:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6215:                         pos = prev+ivc[j]; prev = pos;
                   6216:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   6217:                     }
                   6218:                     break;
                   6219:                 case 2:
                   6220:                     ivs = ivect->index.s;
                   6221:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6222:                         pos = prev+ivs[j]; prev = pos;
                   6223:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   6224:                     }
                   6225:                     break;
                   6226:                 case 4:
                   6227:                     ivi = ivect->index.i;
                   6228:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6229:                         pos = prev+ivi[j]; prev = pos;
                   6230:                         svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   6231:                     }
                   6232:                     break;
                   6233:             }
                   6234:         }
                   6235:     }
                   6236:     return maxrs;
1.72      noro     6237: }
                   6238:
1.232     noro     6239: ND nd_add_lf(ND p1,ND p2)
                   6240: {
                   6241:     int n,c,can;
                   6242:     ND r;
                   6243:     NM m1,m2,mr0,mr,s;
                   6244:     GZ t;
                   6245:
                   6246:     if ( !p1 ) return p2;
                   6247:     else if ( !p2 ) return p1;
                   6248:     else {
                   6249:         can = 0;
                   6250:         for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   6251:             c = DL_COMPARE(DL(m1),DL(m2));
                   6252:             switch ( c ) {
                   6253:                 case 0:
                   6254:                     addlf(CZ(m1),CZ(m2),&t);
                   6255:                     s = m1; m1 = NEXT(m1);
                   6256:                     if ( t ) {
                   6257:                         can++; NEXTNM2(mr0,mr,s); CZ(mr) = (t);
                   6258:                     } else {
                   6259:                         can += 2; FREENM(s);
                   6260:                     }
                   6261:                     s = m2; m2 = NEXT(m2); FREENM(s);
                   6262:                     break;
                   6263:                 case 1:
                   6264:                     s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   6265:                     break;
                   6266:                 case -1:
                   6267:                     s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   6268:                     break;
                   6269:             }
                   6270:         }
                   6271:         if ( !mr0 )
                   6272:             if ( m1 ) mr0 = m1;
                   6273:             else if ( m2 ) mr0 = m2;
                   6274:             else return 0;
                   6275:         else if ( m1 ) NEXT(mr) = m1;
                   6276:         else if ( m2 ) NEXT(mr) = m2;
                   6277:         else NEXT(mr) = 0;
                   6278:         BDY(p1) = mr0;
                   6279:         SG(p1) = MAX(SG(p1),SG(p2));
                   6280:         LEN(p1) = LEN(p1)+LEN(p2)-can;
                   6281:         FREEND(p2);
                   6282:         return p1;
                   6283:     }
                   6284: }
                   6285:
1.234     noro     6286: int ndv_reduce_vect_lf(mpz_t *svect,int trace,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.232     noro     6287: {
                   6288:     int i,j,k,len,pos,prev;
                   6289:     mpz_t c,mc,c1;
                   6290:     IndArray ivect;
                   6291:     unsigned char *ivc;
                   6292:     unsigned short *ivs;
                   6293:     unsigned int *ivi;
                   6294:     NDV redv;
                   6295:     NMV mr;
                   6296:     NODE rp;
                   6297:     int maxrs;
                   6298:
                   6299:     maxrs = 0;
                   6300:     lf_lazy = 1;
                   6301:     for ( i = 0; i < nred; i++ ) {
                   6302:         ivect = imat[i];
                   6303:         k = ivect->head;
                   6304:         mpz_mod(svect[k],svect[k],BDY(current_mod_lf));
                   6305:         if ( mpz_sgn(svect[k]) ) {
                   6306:             maxrs = MAX(maxrs,rp0[i]->sugar);
                   6307:             mpz_neg(svect[k],svect[k]);
1.234     noro     6308:             redv = trace?nd_ps_trace[rp0[i]->index]:nd_ps[rp0[i]->index];
1.232     noro     6309:             len = LEN(redv); mr = BDY(redv);
                   6310:             prev = k;
                   6311:             switch ( ivect->width ) {
                   6312:                 case 1:
                   6313:                     ivc = ivect->index.c;
                   6314:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6315:                         pos = prev+ivc[j]; prev = pos;
                   6316:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   6317:                     }
                   6318:                     break;
                   6319:                 case 2:
                   6320:                     ivs = ivect->index.s;
                   6321:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6322:                         pos = prev+ivs[j]; prev = pos;
                   6323:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   6324:                     }
                   6325:                     break;
                   6326:                 case 4:
                   6327:                     ivi = ivect->index.i;
                   6328:                     for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   6329:                         pos = prev+ivi[j]; prev = pos;
                   6330:                         mpz_addmul(svect[pos],svect[k],BDY(CZ(mr)));
                   6331:                     }
                   6332:                     break;
                   6333:             }
                   6334:             mpz_set_ui(svect[k],0);
                   6335:         }
                   6336:     }
                   6337:     lf_lazy=0;
                   6338:     for ( i = 0; i < col; i++ ) {
                   6339:         mpz_mod(svect[i],svect[i],BDY(current_mod_lf));
                   6340:     }
                   6341:     return maxrs;
                   6342: }
                   6343:
                   6344: int nd_gauss_elim_lf(mpz_t **mat0,int *sugar,int row,int col,int *colstat)
                   6345: {
                   6346:     int i,j,k,l,rank,s;
                   6347:     mpz_t a,a1,inv;
                   6348:     mpz_t *t,*pivot,*pk;
                   6349:     mpz_t **mat;
                   6350:     struct oEGT eg0,eg1,eg_forward,eg_mod,eg_back;
1.234     noro     6351:     int size,size1;
1.232     noro     6352:
                   6353:     mpz_init(inv);
                   6354:     mpz_init(a);
                   6355:     mat = (mpz_t **)mat0;
1.234     noro     6356:         size = 0;
1.232     noro     6357:     for ( rank = 0, j = 0; j < col; j++ ) {
                   6358:         for ( i = rank; i < row; i++ ) {
                   6359:             mpz_mod(mat[i][j],mat[i][j],BDY(current_mod_lf));
                   6360:         }
                   6361:         for ( i = rank; i < row; i++ )
                   6362:             if ( mpz_sgn(mat[i][j]) )
                   6363:                 break;
                   6364:         if ( i == row ) {
                   6365:             colstat[j] = 0;
                   6366:             continue;
                   6367:         } else
                   6368:             colstat[j] = 1;
                   6369:         if ( i != rank ) {
                   6370:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   6371:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   6372:         }
                   6373:         pivot = mat[rank];
                   6374:         s = sugar[rank];
                   6375:         mpz_invert(inv,pivot[j],BDY(current_mod_lf));
                   6376:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   6377:             if ( mpz_sgn(*pk) ) {
                   6378:                 mpz_mul(a,*pk,inv); mpz_mod(*pk,a,BDY(current_mod_lf));
                   6379:             }
                   6380:         for ( i = rank+1; i < row; i++ ) {
                   6381:             t = mat[i];
                   6382:             if ( mpz_sgn(t[j]) ) {
                   6383:                 sugar[i] = MAX(sugar[i],s);
                   6384:                 mpz_neg(a,t[j]);
                   6385:                 red_by_vect_lf(t+j,pivot+j,a,col-j);
                   6386:             }
                   6387:         }
                   6388:         rank++;
                   6389:     }
                   6390:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   6391:         if ( colstat[j] ) {
                   6392:             pivot = mat[l];
                   6393:             s = sugar[l];
                   6394:             for ( k = j; k < col; k++ )
                   6395:               mpz_mod(pivot[k],pivot[k],BDY(current_mod_lf));
                   6396:             for ( i = 0; i < l; i++ ) {
                   6397:                 t = mat[i];
                   6398:                 if ( mpz_sgn(t[j]) ) {
                   6399:                     sugar[i] = MAX(sugar[i],s);
                   6400:                     mpz_neg(a,t[j]);
                   6401:                     red_by_vect_lf(t+j,pivot+j,a,col-j);
                   6402:                 }
                   6403:             }
                   6404:             l--;
                   6405:         }
                   6406:     for ( j = 0, l = 0; l < rank; j++ )
                   6407:         if ( colstat[j] ) {
                   6408:             t = mat[l];
                   6409:             for ( k = j; k < col; k++ ) {
                   6410:                 mpz_mod(t[k],t[k],BDY(current_mod_lf));
                   6411:             }
                   6412:             l++;
                   6413:         }
                   6414:     return rank;
                   6415: }
                   6416:
                   6417:
1.65      noro     6418: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   6419: {
1.157     noro     6420:     int j,k,len;
                   6421:     UINT *p;
                   6422:     UINT c;
                   6423:     NDV r;
                   6424:     NMV mr0,mr;
                   6425:
                   6426:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   6427:     if ( !len ) return 0;
                   6428:     else {
1.200     noro     6429:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
1.103     noro     6430: #if 0
1.157     noro     6431:         ndv_alloc += nmv_adv*len;
1.103     noro     6432: #endif
1.157     noro     6433:         mr = mr0;
                   6434:         p = s0vect;
                   6435:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   6436:             if ( !rhead[j] ) {
                   6437:                 if ( c = vect[k++] ) {
                   6438:                     ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   6439:                 }
                   6440:             }
                   6441:         MKNDV(nd_nvar,mr0,len,r);
                   6442:         return r;
                   6443:     }
1.65      noro     6444: }
                   6445:
1.220     noro     6446: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect)
                   6447: {
                   6448:     int j,k,len;
                   6449:     UINT *p;
                   6450:     NDV r;
                   6451:     NMV mr0,mr;
                   6452:
                   6453:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) len++;
                   6454:     if ( !len ) return 0;
                   6455:     else {
                   6456:         mr0 = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
                   6457:         mr = mr0;
                   6458:         p = s0vect;
                   6459:         for ( j = 0; j < col; j++, p += nd_wpd )
                   6460:                  if ( vect[j/BLEN] & (1L<<(j%BLEN)) ) {
                   6461:             ndl_copy(p,DL(mr)); CM(mr) = 1; NMV_ADV(mr);
                   6462:           }
                   6463:         MKNDV(nd_nvar,mr0,len,r);
                   6464:         return r;
                   6465:     }
                   6466: }
                   6467:
1.129     noro     6468: /* for preprocessed vector */
                   6469:
1.113     noro     6470: NDV vect_to_ndv_q(Q *vect,int spcol,int col,int *rhead,UINT *s0vect)
1.107     noro     6471: {
1.157     noro     6472:     int j,k,len;
                   6473:     UINT *p;
                   6474:     Q c;
                   6475:     NDV r;
                   6476:     NMV mr0,mr;
                   6477:
                   6478:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   6479:     if ( !len ) return 0;
                   6480:     else {
1.200     noro     6481:         mr0 = (NMV)MALLOC(nmv_adv*len);
1.107     noro     6482: #if 0
1.157     noro     6483:         ndv_alloc += nmv_adv*len;
1.107     noro     6484: #endif
1.157     noro     6485:         mr = mr0;
                   6486:         p = s0vect;
                   6487:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   6488:             if ( !rhead[j] ) {
                   6489:                 if ( c = vect[k++] ) {
                   6490:                     if ( DN(c) )
                   6491:                         error("afo");
                   6492:                     ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
                   6493:                 }
                   6494:             }
                   6495:         MKNDV(nd_nvar,mr0,len,r);
                   6496:         return r;
                   6497:     }
1.107     noro     6498: }
                   6499:
1.215     noro     6500: NDV vect_to_ndv_gz(GZ *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   6501: {
                   6502:     int j,k,len;
                   6503:     UINT *p;
1.232     noro     6504:     GZ c;
1.215     noro     6505:     NDV r;
                   6506:     NMV mr0,mr;
                   6507:
                   6508:     for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   6509:     if ( !len ) return 0;
                   6510:     else {
                   6511:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   6512: #if 0
                   6513:         ndv_alloc += nmv_adv*len;
                   6514: #endif
                   6515:         mr = mr0;
                   6516:         p = s0vect;
                   6517:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   6518:             if ( !rhead[j] ) {
                   6519:                 if ( c = vect[k++] ) {
                   6520:                     ndl_copy(p,DL(mr)); CZ(mr) = c; NMV_ADV(mr);
                   6521:                 }
                   6522:             }
                   6523:         MKNDV(nd_nvar,mr0,len,r);
                   6524:         return r;
                   6525:     }
                   6526: }
                   6527:
1.232     noro     6528: NDV vect_to_ndv_lf(mpz_t *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   6529: {
                   6530:     int j,k,len;
                   6531:     UINT *p;
                   6532:     mpz_t c;
                   6533:     NDV r;
                   6534:     NMV mr0,mr;
                   6535:
                   6536:     for ( j = 0, len = 0; j < spcol; j++ ) if ( mpz_sgn(vect[j]) ) len++;
                   6537:     if ( !len ) return 0;
                   6538:     else {
                   6539:         mr0 = (NMV)MALLOC(nmv_adv*len);
                   6540: #if 0
                   6541:         ndv_alloc += nmv_adv*len;
                   6542: #endif
                   6543:         mr = mr0;
                   6544:         p = s0vect;
                   6545:         for ( j = k = 0; j < col; j++, p += nd_wpd )
                   6546:             if ( !rhead[j] ) {
                   6547:                 c[0] = vect[k++][0];
                   6548:                 if ( mpz_sgn(c) ) {
                   6549:                     ndl_copy(p,DL(mr)); MPZTOGZ(c,CZ(mr)); NMV_ADV(mr);
                   6550:                 }
                   6551:             }
                   6552:         MKNDV(nd_nvar,mr0,len,r);
                   6553:         return r;
                   6554:     }
                   6555: }
                   6556:
1.129     noro     6557: /* for plain vector */
                   6558:
                   6559: NDV plain_vect_to_ndv_q(Q *vect,int col,UINT *s0vect)
                   6560: {
1.157     noro     6561:     int j,k,len;
                   6562:     UINT *p;
                   6563:     Q c;
                   6564:     NDV r;
                   6565:     NMV mr0,mr;
                   6566:
                   6567:     for ( j = 0, len = 0; j < col; j++ ) if ( vect[j] ) len++;
                   6568:     if ( !len ) return 0;
                   6569:     else {
1.200     noro     6570:         mr0 = (NMV)MALLOC(nmv_adv*len);
1.129     noro     6571: #if 0
1.157     noro     6572:         ndv_alloc += nmv_adv*len;
1.129     noro     6573: #endif
1.157     noro     6574:         mr = mr0;
                   6575:         p = s0vect;
                   6576:         for ( j = k = 0; j < col; j++, p += nd_wpd, k++ )
                   6577:             if ( c = vect[k] ) {
                   6578:                 if ( DN(c) )
                   6579:                     error("afo");
                   6580:                 ndl_copy(p,DL(mr)); CQ(mr) = c; NMV_ADV(mr);
                   6581:             }
                   6582:         MKNDV(nd_nvar,mr0,len,r);
                   6583:         return r;
                   6584:     }
1.129     noro     6585: }
                   6586:
1.133     noro     6587: int nd_sp_f4(int m,int trace,ND_pairs l,PGeoBucket bucket)
1.65      noro     6588: {
1.157     noro     6589:     ND_pairs t;
                   6590:     NODE sp0,sp;
                   6591:     int stat;
                   6592:     ND spol;
                   6593:
                   6594:     for ( t = l; t; t = NEXT(t) ) {
                   6595:         stat = nd_sp(m,trace,t,&spol);
                   6596:         if ( !stat ) return 0;
                   6597:         if ( spol ) {
                   6598:             add_pbucket_symbolic(bucket,spol);
                   6599:         }
                   6600:     }
                   6601:     return 1;
1.65      noro     6602: }
                   6603:
1.133     noro     6604: int nd_symbolic_preproc(PGeoBucket bucket,int trace,UINT **s0vect,NODE *r)
1.65      noro     6605: {
1.157     noro     6606:     NODE rp0,rp;
                   6607:     NM mul,head,s0,s;
                   6608:     int index,col,i,sugar;
                   6609:     RHist h;
                   6610:     UINT *s0v,*p;
                   6611:     NM_ind_pair pair;
                   6612:     ND red;
                   6613:     NDV *ps;
1.235   ! noro     6614:     static int afo;
1.157     noro     6615:
                   6616:     s0 = 0; rp0 = 0; col = 0;
1.215     noro     6617:        if ( nd_demand )
                   6618:        ps = trace?nd_ps_trace_sym:nd_ps_sym;
                   6619:        else
                   6620:        ps = trace?nd_ps_trace:nd_ps;
1.157     noro     6621:     while ( 1 ) {
                   6622:         head = remove_head_pbucket_symbolic(bucket);
                   6623:         if ( !head ) break;
                   6624:         if ( !s0 ) s0 = head;
                   6625:         else NEXT(s) = head;
                   6626:         s = head;
                   6627:         index = ndl_find_reducer(DL(head));
                   6628:         if ( index >= 0 ) {
                   6629:             h = nd_psh[index];
                   6630:             NEWNM(mul);
                   6631:             ndl_sub(DL(head),DL(h),DL(mul));
1.235   ! noro     6632:             if ( ndl_check_bound2(index,DL(mul)) )
        !          6633:                 return 0;
1.157     noro     6634:             sugar = TD(DL(mul))+SG(ps[index]);
                   6635:             MKNM_ind_pair(pair,mul,index,sugar);
                   6636:             red = ndv_mul_nm_symbolic(mul,ps[index]);
                   6637:             add_pbucket_symbolic(bucket,nd_remove_head(red));
                   6638:             NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   6639:         }
                   6640:         col++;
                   6641:     }
                   6642:     if ( rp0 ) NEXT(rp) = 0;
                   6643:     NEXT(s) = 0;
                   6644:     s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   6645:     for ( i = 0, p = s0v, s = s0; i < col;
                   6646:         i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   6647:     *s0vect = s0v;
                   6648:     *r = rp0;
                   6649:     return col;
1.65      noro     6650: }
                   6651:
1.234     noro     6652: NODE nd_f4(int m,int checkonly,int **indp)
1.69      noro     6653: {
1.234     noro     6654:     int i,nh,stat,index,f4red;
1.208     noro     6655:     NODE r,g,tn0,tn,node;
                   6656:     ND_pairs d,l,t,ll0,ll;
                   6657:        LIST l0,l1;
1.157     noro     6658:     ND spol,red;
                   6659:     NDV nf,redv;
                   6660:     NM s0,s;
1.231     noro     6661:     NODE rp0,srp0,nflist,nzlist,nzlist_t;
1.208     noro     6662:     int nsp,nred,col,rank,len,k,j,a,i1s,i2s;
1.157     noro     6663:     UINT c;
                   6664:     UINT **spmat;
                   6665:     UINT *s0vect,*svect,*p,*v;
                   6666:     int *colstat;
                   6667:     IndArray *imat;
                   6668:     int *rhead;
                   6669:     int spcol,sprow;
                   6670:     int sugar;
                   6671:     PGeoBucket bucket;
                   6672:     struct oEGT eg0,eg1,eg_f4;
1.208     noro     6673:     Q i1,i2,sugarq;
1.103     noro     6674: #if 0
1.157     noro     6675:     ndv_alloc = 0;
1.103     noro     6676: #endif
1.157     noro     6677:     g = 0; d = 0;
                   6678:     for ( i = 0; i < nd_psn; i++ ) {
1.231     noro     6679:         if ( !nd_nzlist ) d = update_pairs(d,g,i,0);
1.157     noro     6680:         g = update_base(g,i);
                   6681:     }
1.208     noro     6682:        nzlist = 0;
1.231     noro     6683:     nzlist_t = nd_nzlist;
1.234     noro     6684:     f4red = 0;
1.231     noro     6685:     while ( d || nzlist_t ) {
1.157     noro     6686:         get_eg(&eg0);
1.208     noro     6687:         if ( nd_nzlist ) {
1.231     noro     6688:             node = BDY((LIST)BDY(nzlist_t));
1.232     noro     6689:             sugar = (int)ARG0(node);
1.231     noro     6690:             tn = BDY((LIST)ARG1(node));
                   6691:             if ( !tn ) {
                   6692:               nzlist_t = NEXT(nzlist_t);
                   6693:               continue;
                   6694:             }
                   6695:             /* tn = [[i1,i2],...] */
                   6696:             l = nd_ipairtospair(tn);
                   6697:         } else {
                   6698:             l = nd_minsugarp(d,&d);
                   6699:             sugar = nd_sugarweight?l->sugar2:SG(l);
                   6700:             if ( MaxDeg > 0 && sugar > MaxDeg ) break;
1.208     noro     6701:         }
1.157     noro     6702:         bucket = create_pbucket();
                   6703:         stat = nd_sp_f4(m,0,l,bucket);
                   6704:         if ( !stat ) {
1.231     noro     6705:             if ( !nd_nzlist ) {
                   6706:                 for ( t = l; NEXT(t); t = NEXT(t) );
                   6707:                 NEXT(t) = d; d = l;
                   6708:                 d = nd_reconstruct(0,d);
                   6709:             }
1.157     noro     6710:             continue;
                   6711:         }
                   6712:         if ( bucket->m < 0 ) continue;
                   6713:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   6714:         if ( !col ) {
                   6715:             for ( t = l; NEXT(t); t = NEXT(t) );
                   6716:             NEXT(t) = d; d = l;
                   6717:             d = nd_reconstruct(0,d);
                   6718:             continue;
                   6719:         }
                   6720:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   6721:         if ( DP_Print )
                   6722:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   6723:                 sugar,eg_f4.exectime+eg_f4.gctime);
1.210     noro     6724:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,nd_gentrace?&ll:0);
1.234     noro     6725:         if ( checkonly && nflist ) return 0;
1.157     noro     6726:         /* adding new bases */
                   6727:         for ( r = nflist; r; r = NEXT(r) ) {
                   6728:             nf = (NDV)BDY(r);
                   6729:             ndv_removecont(m,nf);
                   6730:             if ( !m && nd_nalg ) {
                   6731:                 ND nf1;
                   6732:
                   6733:                 nf1 = ndvtond(m,nf);
                   6734:                 nd_monic(0,&nf1);
                   6735:                 nd_removecont(m,nf1);
                   6736:                 nf = ndtondv(m,nf1);
                   6737:             }
1.215     noro     6738:             nh = ndv_newps(m,nf,0,1);
1.231     noro     6739:             if ( !nd_nzlist ) d = update_pairs(d,g,nh,0);
1.157     noro     6740:             g = update_base(g,nh);
                   6741:         }
1.234     noro     6742:         if ( DP_Print ) {
                   6743:           fprintf(asir_out,"f4red=%d,gblen=%d,",f4red,length(g)); fflush(asir_out);
                   6744:         }
1.208     noro     6745:         if ( nd_gentrace ) {
                   6746:                        for ( t = ll, tn0 = 0; t; t = NEXT(t) ) {
                   6747:                                NEXTNODE(tn0,tn);
                   6748:                 STOQ(t->i1,i1); STOQ(t->i2,i2);
                   6749:                 node = mknode(2,i1,i2); MKLIST(l0,node);
                   6750:                                BDY(tn) = l0;
                   6751:                        }
                   6752:                        if ( tn0 ) NEXT(tn) = 0; MKLIST(l0,tn0);
                   6753:             STOQ(sugar,sugarq); node = mknode(2,sugarq,l0); MKLIST(l1,node);
                   6754:             MKNODE(node,l1,nzlist); nzlist = node;
                   6755:         }
1.231     noro     6756:         if ( nd_nzlist ) nzlist_t = NEXT(nzlist_t);
1.234     noro     6757:         f4red++;
                   6758:         if ( nd_f4red && f4red >= nd_f4red ) break;
                   6759:         if ( nd_rank0 && !nflist ) break;
1.208     noro     6760:     }
                   6761:     if ( nd_gentrace ) {
                   6762:                MKLIST(l0,reverse_node(nzlist));
                   6763:         MKNODE(nd_alltracelist,l0,0);
1.157     noro     6764:     }
1.103     noro     6765: #if 0
1.157     noro     6766:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.103     noro     6767: #endif
1.215     noro     6768:        conv_ilist(nd_demand,0,g,indp);
1.157     noro     6769:     return g;
1.69      noro     6770: }
1.74      noro     6771:
1.167     noro     6772: NODE nd_f4_trace(int m,int **indp)
1.133     noro     6773: {
1.157     noro     6774:     int i,nh,stat,index;
                   6775:     NODE r,g;
                   6776:     ND_pairs d,l,l0,t;
                   6777:     ND spol,red;
                   6778:     NDV nf,redv,nfqv,nfv;
                   6779:     NM s0,s;
                   6780:     NODE rp0,srp0,nflist;
                   6781:     int nsp,nred,col,rank,len,k,j,a;
                   6782:     UINT c;
                   6783:     UINT **spmat;
                   6784:     UINT *s0vect,*svect,*p,*v;
                   6785:     int *colstat;
                   6786:     IndArray *imat;
                   6787:     int *rhead;
                   6788:     int spcol,sprow;
                   6789:     int sugar;
                   6790:     PGeoBucket bucket;
                   6791:     struct oEGT eg0,eg1,eg_f4;
                   6792:
                   6793:     g = 0; d = 0;
                   6794:     for ( i = 0; i < nd_psn; i++ ) {
1.168     noro     6795:         d = update_pairs(d,g,i,0);
1.157     noro     6796:         g = update_base(g,i);
                   6797:     }
                   6798:     while ( d ) {
                   6799:         get_eg(&eg0);
                   6800:         l = nd_minsugarp(d,&d);
                   6801:         sugar = SG(l);
1.228     noro     6802:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
1.157     noro     6803:         bucket = create_pbucket();
                   6804:         stat = nd_sp_f4(m,0,l,bucket);
                   6805:         if ( !stat ) {
                   6806:             for ( t = l; NEXT(t); t = NEXT(t) );
                   6807:             NEXT(t) = d; d = l;
                   6808:             d = nd_reconstruct(1,d);
                   6809:             continue;
                   6810:         }
                   6811:         if ( bucket->m < 0 ) continue;
                   6812:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   6813:         if ( !col ) {
                   6814:             for ( t = l; NEXT(t); t = NEXT(t) );
                   6815:             NEXT(t) = d; d = l;
                   6816:             d = nd_reconstruct(1,d);
                   6817:             continue;
                   6818:         }
                   6819:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   6820:         if ( DP_Print )
                   6821:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   6822:                 sugar,eg_f4.exectime+eg_f4.gctime);
                   6823:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   6824:         if ( !l0 ) continue;
                   6825:         l = l0;
                   6826:
                   6827:         /* over Q */
                   6828:         bucket = create_pbucket();
                   6829:         stat = nd_sp_f4(0,1,l,bucket);
                   6830:         if ( !stat ) {
                   6831:             for ( t = l; NEXT(t); t = NEXT(t) );
                   6832:             NEXT(t) = d; d = l;
                   6833:             d = nd_reconstruct(1,d);
                   6834:             continue;
                   6835:         }
                   6836:         if ( bucket->m < 0 ) continue;
                   6837:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   6838:         if ( !col ) {
                   6839:             for ( t = l; NEXT(t); t = NEXT(t) );
                   6840:             NEXT(t) = d; d = l;
                   6841:             d = nd_reconstruct(1,d);
                   6842:             continue;
                   6843:         }
                   6844:         nflist = nd_f4_red(0,l,1,s0vect,col,rp0,0);
                   6845:         /* adding new bases */
                   6846:         for ( r = nflist; r; r = NEXT(r) ) {
                   6847:             nfqv = (NDV)BDY(r);
                   6848:             ndv_removecont(0,nfqv);
                   6849:             if ( !rem(NM(HCQ(nfqv)),m) ) return 0;
                   6850:             if ( nd_nalg ) {
                   6851:                 ND nf1;
                   6852:
                   6853:                 nf1 = ndvtond(m,nfqv);
                   6854:                 nd_monic(0,&nf1);
                   6855:                 nd_removecont(0,nf1);
                   6856:                 nfqv = ndtondv(0,nf1); nd_free(nf1);
                   6857:             }
                   6858:             nfv = ndv_dup(0,nfqv);
                   6859:             ndv_mod(m,nfv);
                   6860:             ndv_removecont(m,nfv);
1.215     noro     6861:             nh = ndv_newps(0,nfv,nfqv,1);
1.168     noro     6862:             d = update_pairs(d,g,nh,0);
1.157     noro     6863:             g = update_base(g,nh);
                   6864:         }
                   6865:     }
1.133     noro     6866: #if 0
1.157     noro     6867:     fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.133     noro     6868: #endif
1.215     noro     6869:        conv_ilist(nd_demand,1,g,indp);
1.157     noro     6870:     return g;
1.133     noro     6871: }
                   6872:
1.220     noro     6873: int rref(matrix mat,int *sugar)
                   6874: {
                   6875:   int row,col,i,j,k,l,s,wcol,wj;
                   6876:   unsigned long bj;
                   6877:   unsigned long **a;
                   6878:   unsigned long *ai,*ak,*as,*t;
                   6879:   int *pivot;
                   6880:
                   6881:   row = mat->row;
                   6882:   col = mat->col;
                   6883:   a = mat->a;
                   6884:   wcol = (col+BLEN-1)/BLEN;
                   6885:   pivot = (int *)MALLOC_ATOMIC(row*sizeof(int));
                   6886:   i = 0;
                   6887:   for ( j = 0; j < col; j++ ) {
                   6888:        wj = j/BLEN; bj = 1L<<(j%BLEN);
                   6889:     for ( k = i; k < row; k++ )
                   6890:          if ( a[k][wj] & bj ) break;
                   6891:     if ( k == row ) continue;
                   6892:        pivot[i] = j;
                   6893:     if ( k != i ) {
                   6894:         t = a[i]; a[i] = a[k]; a[k] = t;
                   6895:         s = sugar[i]; sugar[i] = sugar[k]; sugar[k] = s;
                   6896:        }
                   6897:        ai = a[i];
                   6898:     for ( k = i+1; k < row; k++ ) {
                   6899:          ak = a[k];
                   6900:          if ( ak[wj] & bj ) {
                   6901:            for ( l = wj; l < wcol; l++ )
                   6902:                  ak[l] ^= ai[l];
                   6903:            sugar[k] = MAX(sugar[k],sugar[i]);
                   6904:          }
                   6905:        }
                   6906:        i++;
                   6907:   }
                   6908:   for ( k = i-1; k >= 0; k-- ) {
                   6909:     j = pivot[k]; wj = j/BLEN; bj = 1L<<(j%BLEN);
                   6910:        ak = a[k];
                   6911:     for ( s = 0; s < k; s++ ) {
                   6912:          as = a[s];
                   6913:       if ( as[wj] & bj ) {
                   6914:         for ( l = wj; l < wcol; l++ )
                   6915:                  as[l] ^= ak[l];
                   6916:            sugar[s] = MAX(sugar[s],sugar[k]);
                   6917:          }
                   6918:        }
                   6919:   }
                   6920:   return i;
                   6921: }
                   6922:
                   6923: void print_matrix(matrix mat)
                   6924: {
                   6925:   int row,col,i,j;
                   6926:   unsigned long *ai;
                   6927:
                   6928:   row = mat->row;
                   6929:   col = mat->col;
                   6930:   printf("%d x %d\n",row,col);
                   6931:   for ( i = 0; i < row; i++ ) {
                   6932:        ai = mat->a[i];
                   6933:     for ( j = 0; j < col; j++ ) {
                   6934:          if ( ai[j/BLEN] & (1L<<(j%BLEN)) ) putchar('1');
                   6935:          else putchar('0');
                   6936:        }
                   6937:        putchar('\n');
                   6938:   }
                   6939: }
                   6940:
                   6941: NDV vect_to_ndv_2(unsigned long *vect,int col,UINT *s0vect);
                   6942:
                   6943: void red_by_vect_2(matrix mat,int *sugar,unsigned long *v,int rhead,int rsugar)
                   6944: {
                   6945:   int row,col,wcol,wj,i,j;
                   6946:   unsigned long bj;
                   6947:   unsigned long *ai;
                   6948:   unsigned long **a;
                   6949:   int len;
                   6950:   int *pos;
                   6951:
                   6952:   row = mat->row;
                   6953:   col = mat->col;
                   6954:   wcol = (col+BLEN-1)/BLEN;
1.235   ! noro     6955:   pos = (int *)MALLOC(wcol*sizeof(int));
1.220     noro     6956:   bzero(pos,wcol*sizeof(int));
                   6957:   for ( i = j = 0; i < wcol; i++ )
                   6958:     if ( v[i] ) pos[j++] = i;;
                   6959:   len = j;
                   6960:   wj = rhead/BLEN;
                   6961:   bj = 1L<<rhead%BLEN;
                   6962:   a = mat->a;
                   6963:   for ( i = 0; i < row; i++ ) {
                   6964:        ai = a[i];
                   6965:     if ( ai[wj]&bj ) {
                   6966:          for ( j = 0; j < len; j++ )
                   6967:            ai[pos[j]] ^= v[pos[j]];
                   6968:          sugar[i] = MAX(sugar[i],rsugar);
                   6969:        }
                   6970:   }
                   6971: }
                   6972:
                   6973: NODE nd_f4_red_2(ND_pairs sp0,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
                   6974: {
                   6975:     int nsp,nred,i,i0,k,rank,row;
                   6976:     NODE r0,rp;
                   6977:     ND_pairs sp;
                   6978:        ND spol;
                   6979:        NM_ind_pair rt;
                   6980:     int *s0hash;
                   6981:        UINT *s;
                   6982:        int *pivot,*sugar,*head;
                   6983:        matrix mat;
                   6984:     NM m;
                   6985:     NODE r;
                   6986:        struct oEGT eg0,eg1,eg2,eg_elim1,eg_elim2;
                   6987:        int rhead,rsugar,size;
                   6988:     unsigned long *v;
                   6989:
                   6990:     get_eg(&eg0);
                   6991: init_eg(&eg_search);
                   6992:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   6993:     nred = length(rp0);
                   6994:     mat = alloc_matrix(nsp,col);
1.235   ! noro     6995:     s0hash = (int *)MALLOC(col*sizeof(int));
1.220     noro     6996:     for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
                   6997:         s0hash[i] = ndl_hash_value(s);
                   6998:
1.235   ! noro     6999:        sugar = (int *)MALLOC(nsp*sizeof(int));
1.220     noro     7000:        for ( i = 0, sp = sp0; sp; sp = NEXT(sp) ) {
                   7001:                nd_sp(2,0,sp,&spol);
                   7002:                if ( spol ) {
                   7003:              mat->a[i] = nd_to_vect_2(s0vect,col,s0hash,spol);
                   7004:                  sugar[i] = SG(spol);
                   7005:                  i++;
                   7006:                }
                   7007:        }
                   7008:        mat->row = i;
1.227     noro     7009:     if ( DP_Print ) {
                   7010:       fprintf(asir_out,"%dx%d,",mat->row,mat->col); fflush(asir_out);
                   7011:     }
1.220     noro     7012:        size = ((col+BLEN-1)/BLEN)*sizeof(unsigned long);
                   7013:        v = CALLOC((col+BLEN-1)/BLEN,sizeof(unsigned long));
                   7014:     for ( rp = rp0, i = 0; rp; rp = NEXT(rp), i++ ) {
                   7015:                rt = (NM_ind_pair)BDY(rp);
                   7016:                bzero(v,size);
                   7017:         rhead = nd_nm_to_vect_2(s0vect,col,s0hash,nd_ps[rt->index],rt->mul,v);
                   7018:                rsugar = SG(nd_ps[rt->index])+TD(DL(rt->mul));
                   7019:            red_by_vect_2(mat,sugar,v,rhead,rsugar);
                   7020:        }
                   7021:
                   7022:     get_eg(&eg1);
                   7023:     init_eg(&eg_elim1); add_eg(&eg_elim1,&eg0,&eg1);
                   7024:        rank = rref(mat,sugar);
                   7025:
                   7026:     for ( i = 0, r0 = 0; i < rank; i++ ) {
                   7027:       NEXTNODE(r0,r);
                   7028:          BDY(r) = (pointer)vect_to_ndv_2(mat->a[i],col,s0vect);
                   7029:       SG((NDV)BDY(r)) = sugar[i];
                   7030:     }
                   7031:     if ( r0 ) NEXT(r) = 0;
                   7032:     get_eg(&eg2);
                   7033:     init_eg(&eg_elim2); add_eg(&eg_elim2,&eg1,&eg2);
                   7034:     if ( DP_Print ) {
                   7035:         fprintf(asir_out,"elim1=%fsec,elim2=%fsec\n",
                   7036:                  eg_elim1.exectime+eg_elim1.gctime,eg_elim2.exectime+eg_elim2.gctime);
                   7037:         fflush(asir_out);
                   7038:        }
                   7039:     return r0;
                   7040: }
                   7041:
                   7042:
1.133     noro     7043: NODE nd_f4_red(int m,ND_pairs sp0,int trace,UINT *s0vect,int col,NODE rp0,ND_pairs *nz)
1.63      noro     7044: {
1.157     noro     7045:     IndArray *imat;
                   7046:     int nsp,nred,i;
                   7047:     int *rhead;
                   7048:     NODE r0,rp;
                   7049:     ND_pairs sp;
                   7050:     NM_ind_pair *rvect;
1.210     noro     7051:     UINT *s;
                   7052:     int *s0hash;
                   7053:
1.220     noro     7054:     if ( m == 2 && nd_rref2 )
                   7055:           return nd_f4_red_2(sp0,s0vect,col,rp0,nz);
                   7056:
1.198     noro     7057: init_eg(&eg_search);
1.157     noro     7058:     for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   7059:     nred = length(rp0);
1.235   ! noro     7060:     imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
        !          7061:     rhead = (int *)MALLOC(col*sizeof(int));
1.157     noro     7062:     for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   7063:
                   7064:     /* construction of index arrays */
1.227     noro     7065:     if ( DP_Print ) {
                   7066:          fprintf(stderr,"%dx%d,",nsp+nred,col);
                   7067:     }
1.235   ! noro     7068:     rvect = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
        !          7069:     s0hash = (int *)MALLOC(col*sizeof(int));
1.210     noro     7070:     for ( i = 0, s = s0vect; i < col; i++, s += nd_wpd )
                   7071:         s0hash[i] = ndl_hash_value(s);
1.157     noro     7072:     for ( rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   7073:         rvect[i] = (NM_ind_pair)BDY(rp);
1.235   ! noro     7074:         imat[i] = nm_ind_pair_to_vect_compress(trace,s0vect,col,s0hash,rvect[i]);
1.157     noro     7075:         rhead[imat[i]->head] = 1;
                   7076:     }
1.232     noro     7077:     if ( m > 0 || m == -1 )
1.157     noro     7078:         r0 = nd_f4_red_main(m,sp0,nsp,s0vect,col,rvect,rhead,imat,nred,nz);
1.232     noro     7079:     else if ( m == -2 )
1.234     noro     7080:         r0 = nd_f4_red_lf_main(m,sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
1.157     noro     7081:     else
1.215     noro     7082:         r0 = nd_f4_red_gz_main(sp0,nsp,trace,s0vect,col,rvect,rhead,imat,nred);
1.227     noro     7083:     if ( DP_Print ) print_eg("search",&eg_search);
1.157     noro     7084:     return r0;
1.106     noro     7085: }
1.74      noro     7086:
1.232     noro     7087: /* for small finite fields  */
1.106     noro     7088: NODE nd_f4_red_main(int m,ND_pairs sp0,int nsp,UINT *s0vect,int col,
1.133     noro     7089:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred,ND_pairs *nz)
1.106     noro     7090: {
1.157     noro     7091:     int spcol,sprow,a;
                   7092:     int i,j,k,l,rank;
                   7093:     NODE r0,r;
                   7094:     ND_pairs sp;
                   7095:     ND spol;
                   7096:     int **spmat;
                   7097:     UINT *svect,*v;
                   7098:     int *colstat;
                   7099:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   7100:     int maxrs;
                   7101:     int *spsugar;
                   7102:     ND_pairs *spactive;
                   7103:
                   7104:     spcol = col-nred;
                   7105:     get_eg(&eg0);
                   7106:     /* elimination (1st step) */
1.235   ! noro     7107:     spmat = (int **)MALLOC(nsp*sizeof(UINT *));
        !          7108:     svect = (UINT *)MALLOC(col*sizeof(UINT));
        !          7109:     spsugar = (int *)MALLOC(nsp*sizeof(int));
        !          7110:     spactive = !nz?0:(ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
1.157     noro     7111:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   7112:         nd_sp(m,0,sp,&spol);
                   7113:         if ( !spol ) continue;
                   7114:         nd_to_vect(m,s0vect,col,spol,svect);
                   7115:         if ( m == -1 )
                   7116:             maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   7117:         else
                   7118:             maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
                   7119:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   7120:         if ( i < col ) {
                   7121:             spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   7122:             for ( j = k = 0; j < col; j++ )
                   7123:                 if ( !rhead[j] ) v[k++] = svect[j];
                   7124:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   7125:             if ( nz )
                   7126:             spactive[sprow] = sp;
                   7127:             sprow++;
                   7128:         }
                   7129:         nd_free(spol);
                   7130:     }
                   7131:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   7132:     if ( DP_Print ) {
                   7133:         fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   7134:         fflush(asir_out);
                   7135:     }
                   7136:     /* free index arrays */
1.200     noro     7137:     for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c);
1.157     noro     7138:
                   7139:     /* elimination (2nd step) */
1.235   ! noro     7140:     colstat = (int *)MALLOC(spcol*sizeof(int));
1.157     noro     7141:     if ( m == -1 )
                   7142:         rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
                   7143:     else
                   7144:         rank = nd_gauss_elim_mod(spmat,spsugar,spactive,sprow,spcol,m,colstat);
                   7145:     r0 = 0;
                   7146:     for ( i = 0; i < rank; i++ ) {
                   7147:         NEXTNODE(r0,r); BDY(r) =
                   7148:             (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   7149:         SG((NDV)BDY(r)) = spsugar[i];
1.200     noro     7150:         GCFREE(spmat[i]);
1.157     noro     7151:     }
                   7152:     if ( r0 ) NEXT(r) = 0;
                   7153:
1.200     noro     7154:     for ( ; i < sprow; i++ ) GCFREE(spmat[i]);
1.157     noro     7155:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   7156:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   7157:     if ( DP_Print ) {
                   7158:         fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   7159:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   7160:             nsp,nred,sprow,spcol,rank);
                   7161:         fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   7162:     }
                   7163:     if ( nz ) {
                   7164:         for ( i = 0; i < rank-1; i++ ) NEXT(spactive[i]) = spactive[i+1];
                   7165:         if ( rank > 0 ) {
                   7166:             NEXT(spactive[rank-1]) = 0;
                   7167:             *nz = spactive[0];
                   7168:         } else
                   7169:             *nz = 0;
                   7170:     }
                   7171:     return r0;
1.74      noro     7172: }
                   7173:
1.234     noro     7174: NODE nd_f4_red_lf_main(int m,ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
1.232     noro     7175:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   7176: {
                   7177:     int spcol,sprow,a;
                   7178:     int i,j,k,l,rank;
                   7179:     NODE r0,r;
                   7180:     ND_pairs sp;
                   7181:     ND spol;
                   7182:     mpz_t **spmat;
                   7183:     mpz_t *svect,*v;
                   7184:     int *colstat;
                   7185:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   7186:     int maxrs;
                   7187:     int *spsugar;
                   7188:     pointer *w;
                   7189:
                   7190:     spcol = col-nred;
                   7191:     get_eg(&eg0);
                   7192:     /* elimination (1st step) */
1.235   ! noro     7193:     spmat = (mpz_t **)MALLOC(nsp*sizeof(mpz_t *));
        !          7194:     svect = (mpz_t *)MALLOC(col*sizeof(mpz_t));
        !          7195:     spsugar = (int *)MALLOC(nsp*sizeof(int));
1.232     noro     7196:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
1.234     noro     7197:         nd_sp(m,trace,sp,&spol);
1.232     noro     7198:         if ( !spol ) continue;
                   7199:         nd_to_vect_lf(s0vect,col,spol,svect);
1.234     noro     7200:         maxrs = ndv_reduce_vect_lf(svect,trace,col,imat,rvect,nred);
1.232     noro     7201:         for ( i = 0; i < col; i++ ) if ( mpz_sgn(svect[i]) ) break;
                   7202:         if ( i < col ) {
                   7203:             spmat[sprow] = v = (mpz_t *)MALLOC(spcol*sizeof(mpz_t));
                   7204:             for ( j = k = 0; j < col; j++ )
                   7205:                 if ( !rhead[j] ) v[k++][0] = svect[j][0];
                   7206:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   7207:             sprow++;
                   7208:         }
                   7209: /*        nd_free(spol); */
                   7210:     }
                   7211:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   7212:     if ( DP_Print ) {
                   7213:         fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   7214:         fflush(asir_out);
                   7215:     }
                   7216:     /* free index arrays */
                   7217: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   7218:
                   7219:     /* elimination (2nd step) */
1.235   ! noro     7220:     colstat = (int *)MALLOC(spcol*sizeof(int));
1.232     noro     7221:     rank = nd_gauss_elim_lf(spmat,spsugar,sprow,spcol,colstat);
1.235   ! noro     7222:     w = (pointer *)MALLOC(rank*sizeof(pointer));
1.232     noro     7223:     for ( i = 0; i < rank; i++ ) {
                   7224: #if 0
                   7225:         w[rank-i-1] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   7226:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   7227: #else
                   7228:         w[i] = (pointer)vect_to_ndv_lf(spmat[i],spcol,col,rhead,s0vect);
                   7229:         SG((NDV)w[i]) = spsugar[i];
                   7230: #endif
                   7231: /*        GCFREE(spmat[i]); */
                   7232:
                   7233:     }
                   7234: #if 0
                   7235:     qsort(w,rank,sizeof(NDV),
                   7236:         (int (*)(const void *,const void *))ndv_compare);
                   7237: #endif
                   7238:     r0 = 0;
                   7239:     for ( i = 0; i < rank; i++ ) {
                   7240:         NEXTNODE(r0,r); BDY(r) = w[i];
                   7241:     }
                   7242:     if ( r0 ) NEXT(r) = 0;
                   7243:
                   7244: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   7245:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   7246:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   7247:     if ( DP_Print ) {
                   7248:         fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   7249:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   7250:             nsp,nred,sprow,spcol,rank);
                   7251:         fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   7252:     }
                   7253:     return r0;
                   7254: }
                   7255:
1.133     noro     7256: #if 1
                   7257: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
1.107     noro     7258:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   7259: {
1.157     noro     7260:     int spcol,sprow,a;
                   7261:     int i,j,k,l,rank;
                   7262:     NODE r0,r;
                   7263:     ND_pairs sp;
                   7264:     ND spol;
                   7265:     Q **spmat;
                   7266:     Q *svect,*v;
                   7267:     int *colstat;
                   7268:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   7269:     int maxrs;
                   7270:     int *spsugar;
                   7271:     pointer *w;
                   7272:
                   7273:     spcol = col-nred;
                   7274:     get_eg(&eg0);
                   7275:     /* elimination (1st step) */
1.235   ! noro     7276:     spmat = (Q **)MALLOC(nsp*sizeof(Q *));
        !          7277:     svect = (Q *)MALLOC(col*sizeof(Q));
        !          7278:     spsugar = (int *)MALLOC(nsp*sizeof(int));
1.157     noro     7279:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   7280:         nd_sp(0,trace,sp,&spol);
                   7281:         if ( !spol ) continue;
                   7282:         nd_to_vect_q(s0vect,col,spol,svect);
                   7283:         maxrs = ndv_reduce_vect_q(svect,trace,col,imat,rvect,nred);
                   7284:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   7285:         if ( i < col ) {
                   7286:             spmat[sprow] = v = (Q *)MALLOC(spcol*sizeof(Q));
                   7287:             for ( j = k = 0; j < col; j++ )
                   7288:                 if ( !rhead[j] ) v[k++] = svect[j];
                   7289:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   7290:             sprow++;
                   7291:         }
                   7292: /*        nd_free(spol); */
                   7293:     }
                   7294:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   7295:     if ( DP_Print ) {
                   7296:         fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   7297:         fflush(asir_out);
                   7298:     }
                   7299:     /* free index arrays */
1.200     noro     7300: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
1.157     noro     7301:
                   7302:     /* elimination (2nd step) */
1.235   ! noro     7303:     colstat = (int *)MALLOC(spcol*sizeof(int));
1.157     noro     7304:     rank = nd_gauss_elim_q(spmat,spsugar,sprow,spcol,colstat);
1.235   ! noro     7305:     w = (pointer *)MALLOC(rank*sizeof(pointer));
1.157     noro     7306:     for ( i = 0; i < rank; i++ ) {
1.213     noro     7307: #if 0
1.157     noro     7308:         w[rank-i-1] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   7309:         SG((NDV)w[rank-i-1]) = spsugar[i];
1.213     noro     7310: #else
                   7311:         w[i] = (pointer)vect_to_ndv_q(spmat[i],spcol,col,rhead,s0vect);
                   7312:         SG((NDV)w[i]) = spsugar[i];
                   7313: #endif
1.200     noro     7314: /*        GCFREE(spmat[i]); */
1.157     noro     7315:     }
1.138     noro     7316: #if 0
1.157     noro     7317:     qsort(w,rank,sizeof(NDV),
                   7318:         (int (*)(const void *,const void *))ndv_compare);
1.137     noro     7319: #endif
1.157     noro     7320:     r0 = 0;
                   7321:     for ( i = 0; i < rank; i++ ) {
                   7322:         NEXTNODE(r0,r); BDY(r) = w[i];
                   7323:     }
                   7324:     if ( r0 ) NEXT(r) = 0;
                   7325:
1.200     noro     7326: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
1.157     noro     7327:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   7328:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   7329:     if ( DP_Print ) {
                   7330:         fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   7331:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   7332:             nsp,nred,sprow,spcol,rank);
                   7333:         fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   7334:     }
                   7335:     return r0;
1.107     noro     7336: }
1.215     noro     7337:
                   7338: NODE nd_f4_red_gz_main(ND_pairs sp0,int nsp,int trace,UINT *s0vect,int col,
                   7339:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   7340: {
                   7341:     int spcol,sprow,a;
                   7342:     int i,j,k,l,rank;
                   7343:     NODE r0,r;
                   7344:     ND_pairs sp;
                   7345:     ND spol;
                   7346:     GZ **spmat;
                   7347:     GZ *svect,*v;
                   7348:     int *colstat;
                   7349:     struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
                   7350:     int maxrs;
                   7351:     int *spsugar;
                   7352:     pointer *w;
                   7353:
                   7354:     spcol = col-nred;
                   7355:     get_eg(&eg0);
                   7356:     /* elimination (1st step) */
1.235   ! noro     7357:     spmat = (GZ **)MALLOC(nsp*sizeof(GZ *));
        !          7358:     svect = (GZ *)MALLOC(col*sizeof(GZ));
        !          7359:     spsugar = (int *)MALLOC(nsp*sizeof(int));
1.215     noro     7360:     for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   7361:         nd_sp(0,trace,sp,&spol);
                   7362:         if ( !spol ) continue;
                   7363:                spol = ndtondgz(spol);
                   7364:         nd_to_vect_q(s0vect,col,spol,(Q *)svect);
                   7365:         maxrs = ndv_reduce_vect_gz(svect,trace,col,imat,rvect,nred);
                   7366:         for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   7367:         if ( i < col ) {
                   7368:             spmat[sprow] = v = (GZ *)MALLOC(spcol*sizeof(GZ));
                   7369:             for ( j = k = 0; j < col; j++ )
                   7370:                 if ( !rhead[j] ) v[k++] = svect[j];
                   7371:             spsugar[sprow] = MAX(maxrs,SG(spol));
                   7372:             sprow++;
                   7373:         }
                   7374: /*        nd_free(spol); */
                   7375:     }
                   7376:     get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   7377:     if ( DP_Print ) {
                   7378:         fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   7379:         fflush(asir_out);
                   7380:     }
                   7381:     /* free index arrays */
                   7382: /*    for ( i = 0; i < nred; i++ ) GCFREE(imat[i]->index.c); */
                   7383:
                   7384:     /* elimination (2nd step) */
1.235   ! noro     7385:     colstat = (int *)MALLOC(spcol*sizeof(int));
1.215     noro     7386:     rank = nd_gauss_elim_gz(spmat,spsugar,sprow,spcol,colstat);
1.235   ! noro     7387:     w = (pointer *)MALLOC(rank*sizeof(pointer));
1.215     noro     7388:     for ( i = 0; i < rank; i++ ) {
                   7389: #if 0
                   7390:         w[rank-i-1] = (pointer)vect_to_ndv_gz(spmat[i],spcol,col,rhead,s0vect);
                   7391:                w[rank-i-1] = ndvgztondv(w[rank-i-1]);
                   7392:         SG((NDV)w[rank-i-1]) = spsugar[i];
                   7393: #else
1.232     noro     7394:         w[i] = (pointer)vect_to_ndv_gz(spmat[i],spcol,col,rhead,s0vect);
1.215     noro     7395:                w[i] = ndvgztondv(w[i]);
                   7396:         SG((NDV)w[i]) = spsugar[i];
                   7397: #endif
                   7398: /*        GCFREE(spmat[i]); */
                   7399:
                   7400:     }
                   7401: #if 0
                   7402:     qsort(w,rank,sizeof(NDV),
                   7403:         (int (*)(const void *,const void *))ndv_compare);
                   7404: #endif
                   7405:     r0 = 0;
                   7406:     for ( i = 0; i < rank; i++ ) {
                   7407:         NEXTNODE(r0,r); BDY(r) = w[i];
                   7408:     }
                   7409:     if ( r0 ) NEXT(r) = 0;
                   7410:
                   7411: /*    for ( ; i < sprow; i++ ) GCFREE(spmat[i]); */
                   7412:     get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   7413:     init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
                   7414:     if ( DP_Print ) {
                   7415:         fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
                   7416:         fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   7417:             nsp,nred,sprow,spcol,rank);
                   7418:         fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
                   7419:     }
                   7420:     return r0;
                   7421: }
1.129     noro     7422: #else
                   7423: void printm(Q **mat,int row,int col)
                   7424: {
1.157     noro     7425:     int i,j;
                   7426:     printf("[");
                   7427:     for ( i = 0; i < row; i++ ) {
                   7428:         for ( j = 0; j < col; j++ ) {
                   7429:             printexpr(CO,mat[i][j]); printf(" ");
                   7430:         }
                   7431:         printf("]\n");
                   7432:     }
1.129     noro     7433: }
                   7434:
                   7435: NODE nd_f4_red_q_main(ND_pairs sp0,int nsp,UINT *s0vect,int col,
                   7436:         NM_ind_pair *rvect,int *rhead,IndArray *imat,int nred)
                   7437: {
1.157     noro     7438:     int row,a;
                   7439:     int i,j,rank;
                   7440:     NODE r0,r;
                   7441:     ND_pairs sp;
                   7442:     ND spol;
                   7443:     Q **mat;
                   7444:     int *colstat;
                   7445:     int *sugar;
                   7446:
                   7447:     row = nsp+nred;
                   7448:     /* make the matrix */
1.235   ! noro     7449:     mat = (Q **)MALLOC(row*sizeof(Q *));
        !          7450:     sugar = (int *)MALLOC(row*sizeof(int));
1.157     noro     7451:     for ( row = a = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   7452:         nd_sp(0,0,sp,&spol);
                   7453:         if ( !spol ) continue;
                   7454:         mat[row] = (Q *)MALLOC(col*sizeof(Q));
                   7455:         nd_to_vect_q(s0vect,col,spol,mat[row]);
                   7456:         sugar[row] = SG(spol);
                   7457:         row++;
                   7458:     }
                   7459:     for ( i = 0; i < nred; i++, row++ ) {
                   7460:         mat[row] = nm_ind_pair_to_vect(0,s0vect,col,rvect[i]);
                   7461:         sugar[row] = rvect[i]->sugar;
                   7462:     }
                   7463:     /* elimination */
1.235   ! noro     7464:     colstat = (int *)MALLOC(col*sizeof(int));
1.157     noro     7465:     rank = nd_gauss_elim_q(mat,sugar,row,col,colstat);
                   7466:     r0 = 0;
                   7467:     for ( i = 0; i < rank; i++ ) {
                   7468:         for ( j = 0; j < col; j++ ) if ( mat[i][j] ) break;
                   7469:         if ( j == col ) error("nd_f4_red_q_main : cannot happen");
                   7470:         if ( rhead[j] ) continue;
                   7471:         NEXTNODE(r0,r); BDY(r) =
                   7472:             (pointer)plain_vect_to_ndv_q(mat[i],col,s0vect);
                   7473:         SG((NDV)BDY(r)) = sugar[i];
                   7474:     }
                   7475:     if ( r0 ) NEXT(r) = 0;
                   7476:     printf("\n");
                   7477:     return r0;
1.129     noro     7478: }
                   7479: #endif
1.107     noro     7480:
1.74      noro     7481: FILE *nd_write,*nd_read;
                   7482:
                   7483: void nd_send_int(int a) {
1.157     noro     7484:     write_int(nd_write,&a);
1.74      noro     7485: }
                   7486:
                   7487: void nd_send_intarray(int *p,int len) {
1.157     noro     7488:     write_intarray(nd_write,p,len);
1.74      noro     7489: }
                   7490:
                   7491: int nd_recv_int() {
1.157     noro     7492:     int a;
1.74      noro     7493:
1.157     noro     7494:     read_int(nd_read,&a);
                   7495:     return a;
1.74      noro     7496: }
                   7497:
                   7498: void nd_recv_intarray(int *p,int len) {
1.157     noro     7499:     read_intarray(nd_read,p,len);
1.74      noro     7500: }
                   7501:
                   7502: void nd_send_ndv(NDV p) {
1.157     noro     7503:     int len,i;
                   7504:     NMV m;
1.74      noro     7505:
1.157     noro     7506:     if ( !p ) nd_send_int(0);
                   7507:     else {
                   7508:         len = LEN(p);
                   7509:         nd_send_int(len);
                   7510:         m = BDY(p);
                   7511:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   7512:             nd_send_int(CM(m));
                   7513:             nd_send_intarray(DL(m),nd_wpd);
                   7514:         }
                   7515:     }
1.74      noro     7516: }
                   7517:
                   7518: void nd_send_nd(ND p) {
1.157     noro     7519:     int len,i;
                   7520:     NM m;
1.74      noro     7521:
1.157     noro     7522:     if ( !p ) nd_send_int(0);
                   7523:     else {
                   7524:         len = LEN(p);
                   7525:         nd_send_int(len);
                   7526:         m = BDY(p);
                   7527:         for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   7528:             nd_send_int(CM(m));
                   7529:             nd_send_intarray(DL(m),nd_wpd);
                   7530:         }
                   7531:     }
1.74      noro     7532: }
1.65      noro     7533:
1.74      noro     7534: NDV nd_recv_ndv()
                   7535: {
1.157     noro     7536:     int len,i;
                   7537:     NMV m,m0;
                   7538:     NDV r;
                   7539:
                   7540:     len = nd_recv_int();
                   7541:     if ( !len ) return 0;
                   7542:     else {
1.200     noro     7543:         m0 = m = (NMV)MALLOC_ATOMIC_IGNORE_OFF_PAGE(nmv_adv*len);
1.103     noro     7544: #if 0
1.157     noro     7545:         ndv_alloc += len*nmv_adv;
1.103     noro     7546: #endif
1.157     noro     7547:         for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   7548:             CM(m) = nd_recv_int();
                   7549:             nd_recv_intarray(DL(m),nd_wpd);
                   7550:         }
                   7551:         MKNDV(nd_nvar,m0,len,r);
                   7552:         return r;
                   7553:     }
1.74      noro     7554: }
1.65      noro     7555:
1.113     noro     7556: int nd_gauss_elim_q(Q **mat0,int *sugar,int row,int col,int *colstat)
1.107     noro     7557: {
1.176     noro     7558:     int i,j,t,c,rank,inv;
1.157     noro     7559:     int *ci,*ri;
                   7560:     Q dn;
                   7561:     MAT m,nm;
                   7562:
                   7563:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
                   7564:     rank = generic_gauss_elim(m,&nm,&dn,&ri,&ci);
                   7565:     for ( i = 0; i < row; i++ )
                   7566:         for ( j = 0; j < col; j++ )
                   7567:             mat0[i][j] = 0;
                   7568:     c = col-rank;
                   7569:     for ( i = 0; i < rank; i++ ) {
                   7570:         mat0[i][ri[i]] = dn;
                   7571:         for ( j = 0; j < c; j++ )
                   7572:             mat0[i][ci[j]] = (Q)BDY(nm)[i][j];
                   7573:     }
                   7574:     return rank;
1.76      noro     7575: }
                   7576:
1.215     noro     7577: int nd_gauss_elim_gz(GZ **mat0,int *sugar,int row,int col,int *colstat)
                   7578: {
                   7579:     int i,j,t,c,rank,inv;
                   7580:     int *ci,*ri;
1.216     noro     7581:     GZ dn;
1.215     noro     7582:     MAT m,nm;
                   7583:
                   7584:     NEWMAT(m); m->row = row; m->col = col; m->body = (pointer **)mat0;
1.216     noro     7585:     rank = gz_generic_gauss_elim(m,&nm,&dn,&ri,&ci);
1.215     noro     7586:     for ( i = 0; i < row; i++ )
                   7587:         for ( j = 0; j < col; j++ )
                   7588:             mat0[i][j] = 0;
                   7589:     c = col-rank;
                   7590:     for ( i = 0; i < rank; i++ ) {
1.216     noro     7591:         mat0[i][ri[i]] = dn;
1.215     noro     7592:         for ( j = 0; j < c; j++ )
                   7593:             mat0[i][ci[j]] = (GZ)BDY(nm)[i][j];
                   7594:     }
                   7595:     return rank;
                   7596: }
                   7597:
1.133     noro     7598: int nd_gauss_elim_mod(int **mat0,int *sugar,ND_pairs *spactive,int row,int col,int md,int *colstat)
1.76      noro     7599: {
1.157     noro     7600:     int i,j,k,l,inv,a,rank,s;
                   7601:     unsigned int *t,*pivot,*pk;
                   7602:     unsigned int **mat;
                   7603:     ND_pairs pair;
                   7604:
                   7605:     mat = (unsigned int **)mat0;
                   7606:     for ( rank = 0, j = 0; j < col; j++ ) {
                   7607:         for ( i = rank; i < row; i++ )
                   7608:             mat[i][j] %= md;
                   7609:         for ( i = rank; i < row; i++ )
                   7610:             if ( mat[i][j] )
                   7611:                 break;
                   7612:         if ( i == row ) {
                   7613:             colstat[j] = 0;
                   7614:             continue;
                   7615:         } else
                   7616:             colstat[j] = 1;
                   7617:         if ( i != rank ) {
                   7618:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   7619:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   7620:             if ( spactive ) {
                   7621:                 pair = spactive[i]; spactive[i] = spactive[rank];
                   7622:                 spactive[rank] = pair;
                   7623:             }
                   7624:         }
                   7625:         pivot = mat[rank];
                   7626:         s = sugar[rank];
                   7627:         inv = invm(pivot[j],md);
                   7628:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   7629:             if ( *pk ) {
                   7630:                 if ( *pk >= (unsigned int)md )
                   7631:                     *pk %= md;
                   7632:                 DMAR(*pk,inv,0,md,*pk)
                   7633:             }
                   7634:         for ( i = rank+1; i < row; i++ ) {
                   7635:             t = mat[i];
                   7636:             if ( a = t[j] ) {
                   7637:                 sugar[i] = MAX(sugar[i],s);
                   7638:                 red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   7639:             }
                   7640:         }
                   7641:         rank++;
                   7642:     }
                   7643:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   7644:         if ( colstat[j] ) {
                   7645:             pivot = mat[l];
                   7646:             s = sugar[l];
                   7647:             for ( i = 0; i < l; i++ ) {
                   7648:                 t = mat[i];
                   7649:                 t[j] %= md;
                   7650:                 if ( a = t[j] ) {
                   7651:                     sugar[i] = MAX(sugar[i],s);
                   7652:                     red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   7653:                 }
                   7654:             }
                   7655:             l--;
                   7656:         }
                   7657:     for ( j = 0, l = 0; l < rank; j++ )
                   7658:         if ( colstat[j] ) {
                   7659:             t = mat[l];
                   7660:             for ( k = j; k < col; k++ )
                   7661:                 if ( t[k] >= (unsigned int)md )
                   7662:                     t[k] %= md;
                   7663:             l++;
                   7664:         }
                   7665:     return rank;
1.76      noro     7666: }
                   7667:
                   7668: int nd_gauss_elim_sf(int **mat0,int *sugar,int row,int col,int md,int *colstat)
                   7669: {
1.157     noro     7670:     int i,j,k,l,inv,a,rank,s;
                   7671:     unsigned int *t,*pivot,*pk;
                   7672:     unsigned int **mat;
                   7673:
                   7674:     mat = (unsigned int **)mat0;
                   7675:     for ( rank = 0, j = 0; j < col; j++ ) {
                   7676:         for ( i = rank; i < row; i++ )
                   7677:             if ( mat[i][j] )
                   7678:                 break;
                   7679:         if ( i == row ) {
                   7680:             colstat[j] = 0;
                   7681:             continue;
                   7682:         } else
                   7683:             colstat[j] = 1;
                   7684:         if ( i != rank ) {
                   7685:             t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   7686:             s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   7687:         }
                   7688:         pivot = mat[rank];
                   7689:         s = sugar[rank];
                   7690:         inv = _invsf(pivot[j]);
                   7691:         for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   7692:             if ( *pk )
                   7693:                 *pk = _mulsf(*pk,inv);
                   7694:         for ( i = rank+1; i < row; i++ ) {
                   7695:             t = mat[i];
                   7696:             if ( a = t[j] ) {
                   7697:                 sugar[i] = MAX(sugar[i],s);
                   7698:                 red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   7699:             }
                   7700:         }
                   7701:         rank++;
                   7702:     }
                   7703:     for ( j = col-1, l = rank-1; j >= 0; j-- )
                   7704:         if ( colstat[j] ) {
                   7705:             pivot = mat[l];
                   7706:             s = sugar[l];
                   7707:             for ( i = 0; i < l; i++ ) {
                   7708:                 t = mat[i];
                   7709:                 if ( a = t[j] ) {
                   7710:                     sugar[i] = MAX(sugar[i],s);
                   7711:                     red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   7712:                 }
                   7713:             }
                   7714:             l--;
                   7715:         }
                   7716:     return rank;
1.77      noro     7717: }
                   7718:
                   7719: int ndv_ishomo(NDV p)
                   7720: {
1.157     noro     7721:     NMV m;
                   7722:     int len,h;
1.77      noro     7723:
1.157     noro     7724:     if ( !p ) return 1;
                   7725:     len = LEN(p);
                   7726:     m = BDY(p);
                   7727:     h = TD(DL(m));
                   7728:     NMV_ADV(m);
                   7729:     for ( len--; len; len--, NMV_ADV(m) )
                   7730:         if ( TD(DL(m)) != h ) return 0;
                   7731:     return 1;
1.77      noro     7732: }
                   7733:
                   7734: void ndv_save(NDV p,int index)
                   7735: {
1.157     noro     7736:     FILE *s;
                   7737:     char name[BUFSIZ];
                   7738:     short id;
                   7739:     int nv,sugar,len,n,i,td,e,j;
                   7740:     NMV m;
                   7741:     unsigned int *dl;
                   7742:     int mpos;
                   7743:
                   7744:     sprintf(name,"%s/%d",Demand,index);
                   7745:     s = fopen(name,"w");
                   7746:     savevl(s,0);
                   7747:     if ( !p ) {
                   7748:         saveobj(s,0);
                   7749:         return;
                   7750:     }
                   7751:     id = O_DP;
                   7752:     nv = NV(p);
                   7753:     sugar = SG(p);
                   7754:     len = LEN(p);
                   7755:     write_short(s,&id); write_int(s,&nv); write_int(s,&sugar);
                   7756:     write_int(s,&len);
                   7757:
                   7758:     for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   7759:         saveobj(s,(Obj)CQ(m));
                   7760:         dl = DL(m);
                   7761:         td = TD(dl);
                   7762:         write_int(s,&td);
                   7763:         for ( j = 0; j < nv; j++ ) {
                   7764:             e = GET_EXP(dl,j);
                   7765:             write_int(s,&e);
                   7766:         }
                   7767:         if ( nd_module ) {
                   7768:             mpos = MPOS(dl); write_int(s,&mpos);
                   7769:         }
                   7770:     }
                   7771:     fclose(s);
1.77      noro     7772: }
                   7773:
1.206     noro     7774: void nd_save_mod(ND p,int index)
                   7775: {
                   7776:     FILE *s;
                   7777:     char name[BUFSIZ];
                   7778:     int nv,sugar,len,c;
                   7779:     NM m;
                   7780:
                   7781:     sprintf(name,"%s/%d",Demand,index);
                   7782:     s = fopen(name,"w");
                   7783:     if ( !p ) {
                   7784:                len = 0;
                   7785:        write_int(s,&len);
                   7786:                fclose(s);
                   7787:         return;
                   7788:     }
                   7789:     nv = NV(p);
                   7790:     sugar = SG(p);
                   7791:     len = LEN(p);
                   7792:     write_int(s,&nv); write_int(s,&sugar); write_int(s,&len);
                   7793:        for ( m = BDY(p); m; m = NEXT(m) ) {
                   7794:          c = CM(m); write_int(s,&c);
                   7795:          write_intarray(s,DL(m),nd_wpd);
                   7796:        }
                   7797:     fclose(s);
                   7798: }
                   7799:
1.77      noro     7800: NDV ndv_load(int index)
                   7801: {
1.157     noro     7802:     FILE *s;
                   7803:     char name[BUFSIZ];
                   7804:     short id;
                   7805:     int nv,sugar,len,n,i,td,e,j;
                   7806:     NDV d;
                   7807:     NMV m0,m;
                   7808:     unsigned int *dl;
                   7809:     Obj obj;
                   7810:     int mpos;
                   7811:
                   7812:     sprintf(name,"%s/%d",Demand,index);
                   7813:     s = fopen(name,"r");
                   7814:     if ( !s ) return 0;
                   7815:
                   7816:     skipvl(s);
                   7817:     read_short(s,&id);
                   7818:     if ( !id ) return 0;
                   7819:     read_int(s,&nv);
                   7820:     read_int(s,&sugar);
                   7821:     read_int(s,&len);
                   7822:
                   7823:     m0 = m = MALLOC(len*nmv_adv);
                   7824:     for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   7825:         loadobj(s,&obj); CQ(m) = (Q)obj;
                   7826:         dl = DL(m);
                   7827:         ndl_zero(dl);
                   7828:         read_int(s,&td); TD(dl) = td;
                   7829:         for ( j = 0; j < nv; j++ ) {
                   7830:             read_int(s,&e);
                   7831:             PUT_EXP(dl,j,e);
                   7832:         }
                   7833:         if ( nd_module ) {
                   7834:             read_int(s,&mpos); MPOS(dl) = mpos;
                   7835:         }
                   7836:         if ( nd_blockmask ) ndl_weight_mask(dl);
                   7837:     }
                   7838:     fclose(s);
                   7839:     MKNDV(nv,m0,len,d);
                   7840:     SG(d) = sugar;
                   7841:     return d;
1.99      noro     7842: }
                   7843:
1.206     noro     7844: ND nd_load_mod(int index)
                   7845: {
                   7846:     FILE *s;
                   7847:     char name[BUFSIZ];
                   7848:     int nv,sugar,len,i,c;
                   7849:        ND d;
                   7850:     NM m0,m;
                   7851:
                   7852:     sprintf(name,"%s/%d",Demand,index);
                   7853:     s = fopen(name,"r");
                   7854:        /* if the file does not exist, it means p[index]=0 */
                   7855:     if ( !s ) return 0;
                   7856:
                   7857:     read_int(s,&nv);
                   7858:        if ( !nv ) { fclose(s); return 0; }
                   7859:
                   7860:     read_int(s,&sugar);
                   7861:     read_int(s,&len);
                   7862:        for ( m0 = 0, i = 0; i < len; i++ ) {
                   7863:                NEXTNM(m0,m);
                   7864:                read_int(s,&c); CM(m) = c;
                   7865:                read_intarray(s,DL(m),nd_wpd);
                   7866:        }
                   7867:        NEXT(m) = 0;
                   7868:     MKND(nv,m0,len,d);
                   7869:     SG(d) = sugar;
                   7870:        fclose(s);
                   7871:     return d;
                   7872: }
                   7873:
1.102     noro     7874: void nd_det(int mod,MAT f,P *rp)
1.99      noro     7875: {
1.157     noro     7876:     VL fv,tv;
                   7877:     int n,i,j,max,e,nvar,sgn,k0,l0,len0,len,k,l,a;
                   7878:     pointer **m;
                   7879:     Q mone;
                   7880:     P **w;
                   7881:     P mp,r;
                   7882:     NDV **dm;
                   7883:     NDV *t,*mi,*mj;
                   7884:     NDV d,s,mij,mjj;
                   7885:     ND u;
                   7886:     NMV nmv;
                   7887:     UINT *bound;
                   7888:     PGeoBucket bucket;
                   7889:     struct order_spec *ord;
                   7890:     Q dq,dt,ds;
                   7891:     N gn,qn,dn0,nm,dn;
                   7892:
                   7893:     create_order_spec(0,0,&ord);
                   7894:     nd_init_ord(ord);
                   7895:     get_vars((Obj)f,&fv);
                   7896:     if ( f->row != f->col )
                   7897:         error("nd_det : non-square matrix");
                   7898:     n = f->row;
                   7899:     m = f->body;
                   7900:     for ( nvar = 0, tv = fv; tv; tv = NEXT(tv), nvar++ );
                   7901:
                   7902:     if ( !nvar ) {
                   7903:         if ( !mod )
                   7904:             detp(CO,(P **)m,n,rp);
                   7905:         else {
                   7906:             w = (P **)almat_pointer(n,n);
                   7907:             for ( i = 0; i < n; i++ )
                   7908:                 for ( j = 0; j < n; j++ )
                   7909:                     ptomp(mod,(P)m[i][j],&w[i][j]);
                   7910:             detmp(CO,mod,w,n,&mp);
                   7911:             mptop(mp,rp);
                   7912:         }
                   7913:         return;
                   7914:     }
                   7915:
                   7916:     if ( !mod ) {
                   7917:         w = (P **)almat_pointer(n,n);
                   7918:         dq = ONE;
                   7919:         for ( i = 0; i < n; i++ ) {
                   7920:             dn0 = ONEN;
                   7921:             for ( j = 0; j < n; j++ ) {
                   7922:                 if ( !m[i][j] ) continue;
                   7923:                 lgp(m[i][j],&nm,&dn);
                   7924:                 gcdn(dn0,dn,&gn); divsn(dn0,gn,&qn); muln(qn,dn,&dn0);
                   7925:             }
                   7926:             if ( !UNIN(dn0) ) {
                   7927:                 NTOQ(dn0,1,ds);
                   7928:                 for ( j = 0; j < n; j++ )
                   7929:                     mulp(CO,(P)m[i][j],(P)ds,&w[i][j]);
                   7930:                 mulq(dq,ds,&dt); dq = dt;
                   7931:             } else
                   7932:                 for ( j = 0; j < n; j++ )
                   7933:                     w[i][j] = (P)m[i][j];
                   7934:         }
                   7935:         m = (pointer **)w;
                   7936:     }
                   7937:
1.178     noro     7938:     for ( i = 0, max = 1; i < n; i++ )
1.157     noro     7939:         for ( j = 0; j < n; j++ )
                   7940:             for ( tv = fv; tv; tv = NEXT(tv) ) {
                   7941:                 e = getdeg(tv->v,(P)m[i][j]);
                   7942:                 max = MAX(e,max);
                   7943:             }
                   7944:     nd_setup_parameters(nvar,max);
                   7945:     dm = (NDV **)almat_pointer(n,n);
1.178     noro     7946:     for ( i = 0, max = 1; i < n; i++ )
1.157     noro     7947:         for ( j = 0; j < n; j++ ) {
                   7948:             dm[i][j] = ptondv(CO,fv,m[i][j]);
                   7949:             if ( mod ) ndv_mod(mod,dm[i][j]);
                   7950:             if ( dm[i][j] && !LEN(dm[i][j]) ) dm[i][j] = 0;
                   7951:         }
                   7952:     d = ptondv(CO,fv,(P)ONE);
                   7953:     if ( mod ) ndv_mod(mod,d);
                   7954:     chsgnq(ONE,&mone);
                   7955:     for ( j = 0, sgn = 1; j < n; j++ ) {
1.222     fujimoto 7956:         if ( DP_Print ) {
                   7957:          fprintf(stderr,".",j);
                   7958:        }
1.157     noro     7959:         for ( i = j; i < n && !dm[i][j]; i++ );
                   7960:         if ( i == n ) {
                   7961:             *rp = 0;
                   7962:             return;
                   7963:         }
                   7964:         k0 = i; l0 = j; len0 = LEN(dm[k0][l0]);
                   7965:         for ( k = j; k < n; k++ )
                   7966:             for ( l = j; l < n; l++ )
                   7967:                 if ( dm[k][l] && LEN(dm[k][l]) < len0 ) {
                   7968:                     k0 = k; l0 = l; len0 = LEN(dm[k][l]);
                   7969:                 }
                   7970:         if ( k0 != j ) {
                   7971:             t = dm[j]; dm[j] = dm[k0]; dm[k0] = t;
                   7972:             sgn = -sgn;
                   7973:         }
                   7974:         if ( l0 != j ) {
                   7975:             for ( k = j; k < n; k++ ) {
                   7976:                 s = dm[k][j]; dm[k][j] = dm[k][l0]; dm[k][l0] = s;
                   7977:             }
                   7978:             sgn = -sgn;
                   7979:         }
                   7980:         bound = nd_det_compute_bound(dm,n,j);
                   7981:         for ( k = 0; k < nd_nvar; k++ )
                   7982:             if ( bound[k]*2 > nd_mask0 ) break;
                   7983:         if ( k < nd_nvar )
                   7984:             nd_det_reconstruct(dm,n,j,d);
                   7985:
                   7986:         for ( i = j+1, mj = dm[j], mjj = mj[j]; i < n; i++ ) {
                   7987: /*            if ( DP_Print ) fprintf(stderr,"    i=%d\n        ",i); */
                   7988:             mi = dm[i]; mij = mi[j];
                   7989:             if ( mod )
                   7990:                 ndv_mul_c(mod,mij,mod-1);
                   7991:             else
                   7992:                 ndv_mul_c_q(mij,mone);
                   7993:             for ( k = j+1; k < n; k++ ) {
                   7994: /*                if ( DP_Print ) fprintf(stderr,"k=%d ",k); */
                   7995:                 bucket = create_pbucket();
                   7996:                 if ( mi[k] ) {
                   7997:                     nmv = BDY(mjj); len = LEN(mjj);
                   7998:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   7999:                         u = ndv_mul_nmv_trunc(mod,nmv,mi[k],DL(BDY(d)));
                   8000:                         add_pbucket(mod,bucket,u);
                   8001:                     }
                   8002:                 }
                   8003:                 if ( mj[k] && mij ) {
                   8004:                     nmv = BDY(mij); len = LEN(mij);
                   8005:                     for ( a = 0; a < len; a++, NMV_ADV(nmv) ) {
                   8006:                         u = ndv_mul_nmv_trunc(mod,nmv,mj[k],DL(BDY(d)));
                   8007:                         add_pbucket(mod,bucket,u);
                   8008:                     }
                   8009:                 }
                   8010:                 u = nd_quo(mod,bucket,d);
                   8011:                 mi[k] = ndtondv(mod,u);
                   8012:             }
                   8013: /*            if ( DP_Print ) fprintf(stderr,"\n",k); */
                   8014:         }
                   8015:         d = mjj;
                   8016:     }
1.222     fujimoto 8017:     if ( DP_Print ) {
                   8018:       fprintf(stderr,"\n",k);
                   8019:     }
1.157     noro     8020:     if ( sgn < 0 )
                   8021:         if ( mod )
                   8022:             ndv_mul_c(mod,d,mod-1);
                   8023:         else
                   8024:             ndv_mul_c_q(d,mone);
                   8025:     r = ndvtop(mod,CO,fv,d);
                   8026:     if ( !mod && !UNIQ(dq) )
                   8027:         divsp(CO,r,(P)dq,rp);
                   8028:     else
                   8029:         *rp = r;
1.99      noro     8030: }
                   8031:
1.102     noro     8032: ND ndv_mul_nmv_trunc(int mod,NMV m0,NDV p,UINT *d)
1.99      noro     8033: {
1.157     noro     8034:     NM mr,mr0;
                   8035:     NM tnm;
                   8036:     NMV m;
                   8037:     UINT *d0,*dt,*dm;
                   8038:     int c,n,td,i,c1,c2,len;
                   8039:     Q q;
                   8040:     ND r;
                   8041:
                   8042:     if ( !p ) return 0;
                   8043:     else {
                   8044:         n = NV(p); m = BDY(p); len = LEN(p);
                   8045:         d0 = DL(m0);
                   8046:         td = TD(d);
                   8047:         mr0 = 0;
                   8048:         NEWNM(tnm);
                   8049:         if ( mod ) {
                   8050:             c = CM(m0);
                   8051:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   8052:                 ndl_add(DL(m),d0,DL(tnm));
                   8053:                 if ( ndl_reducible(DL(tnm),d) ) {
                   8054:                     NEXTNM(mr0,mr);
                   8055:                     c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
                   8056:                     ndl_copy(DL(tnm),DL(mr));
                   8057:                 }
                   8058:             }
                   8059:         } else {
                   8060:             q = CQ(m0);
                   8061:             for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   8062:                 ndl_add(DL(m),d0,DL(tnm));
                   8063:                 if ( ndl_reducible(DL(tnm),d) ) {
                   8064:                     NEXTNM(mr0,mr);
                   8065:                     mulq(CQ(m),q,&CQ(mr));
                   8066:                     ndl_copy(DL(tnm),DL(mr));
                   8067:                 }
                   8068:             }
                   8069:         }
                   8070:         if ( !mr0 )
                   8071:             return 0;
                   8072:         else {
                   8073:             NEXT(mr) = 0;
                   8074:             for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   8075:             MKND(NV(p),mr0,len,r);
                   8076:             SG(r) = SG(p) + TD(d0);
                   8077:             return r;
                   8078:         }
                   8079:     }
1.114     noro     8080: }
                   8081:
                   8082: void nd_det_reconstruct(NDV **dm,int n,int j,NDV d)
                   8083: {
1.157     noro     8084:     int i,obpe,oadv,h,k,l;
                   8085:     static NM prev_nm_free_list;
                   8086:     EPOS oepos;
                   8087:
                   8088:     obpe = nd_bpe;
                   8089:     oadv = nmv_adv;
                   8090:     oepos = nd_epos;
                   8091:     if ( obpe < 2 ) nd_bpe = 2;
                   8092:     else if ( obpe < 3 ) nd_bpe = 3;
                   8093:     else if ( obpe < 4 ) nd_bpe = 4;
                   8094:     else if ( obpe < 5 ) nd_bpe = 5;
                   8095:     else if ( obpe < 6 ) nd_bpe = 6;
                   8096:     else if ( obpe < 8 ) nd_bpe = 8;
                   8097:     else if ( obpe < 10 ) nd_bpe = 10;
                   8098:     else if ( obpe < 16 ) nd_bpe = 16;
                   8099:     else if ( obpe < 32 ) nd_bpe = 32;
                   8100:     else error("nd_det_reconstruct : exponent too large");
                   8101:
                   8102:     nd_setup_parameters(nd_nvar,0);
                   8103:     prev_nm_free_list = _nm_free_list;
                   8104:     _nm_free_list = 0;
                   8105:     for ( k = j; k < n; k++ )
                   8106:         for (l = j; l < n; l++ )
                   8107:             ndv_realloc(dm[k][l],obpe,oadv,oepos);
                   8108:     ndv_realloc(d,obpe,oadv,oepos);
                   8109:     prev_nm_free_list = 0;
1.114     noro     8110: #if 0
1.157     noro     8111:     GC_gcollect();
1.114     noro     8112: #endif
                   8113: }
                   8114:
1.153     noro     8115: /* returns a UINT array containing degree bounds */
                   8116:
1.114     noro     8117: UINT *nd_det_compute_bound(NDV **dm,int n,int j)
                   8118: {
1.157     noro     8119:     UINT *d0,*d1,*d,*t,*r;
                   8120:     int k,l,i;
1.114     noro     8121:
1.157     noro     8122:     d0 = (UINT *)MALLOC(nd_nvar*sizeof(UINT));
                   8123:     for ( k = 0; k < nd_nvar; k++ ) d0[k] = 0;
                   8124:     for ( k = j; k < n; k++ )
                   8125:         for ( l = j; l < n; l++ )
                   8126:             if ( dm[k][l] ) {
                   8127:                 d = ndv_compute_bound(dm[k][l]);
                   8128:                 for ( i = 0; i < nd_nvar; i++ )
                   8129:                     d0[i] = MAX(d0[i],d[i]);
                   8130:             }
                   8131:     return d0;
1.117     noro     8132: }
                   8133:
                   8134: DL nd_separate_d(UINT *d,UINT *trans)
                   8135: {
1.157     noro     8136:     int n,td,i,e,j;
                   8137:     DL a;
1.117     noro     8138:
1.157     noro     8139:     ndl_zero(trans);
                   8140:     td = 0;
                   8141:     for ( i = 0; i < nd_ntrans; i++ ) {
                   8142:         e = GET_EXP(d,i);
                   8143:         PUT_EXP(trans,i,e);
                   8144:         td += MUL_WEIGHT(e,i);
                   8145:     }
                   8146:     if ( nd_ntrans+nd_nalg < nd_nvar ) {
                   8147:         /* homogenized */
                   8148:         i = nd_nvar-1;
                   8149:         e = GET_EXP(d,i);
                   8150:         PUT_EXP(trans,i,e);
                   8151:         td += MUL_WEIGHT(e,i);
                   8152:     }
                   8153:     TD(trans) = td;
                   8154:     if ( nd_blockmask) ndl_weight_mask(trans);
                   8155:     NEWDL(a,nd_nalg);
                   8156:     td = 0;
                   8157:     for ( i = 0; i < nd_nalg; i++ ) {
                   8158:         j = nd_ntrans+i;
                   8159:         e = GET_EXP(d,j);
                   8160:         a->d[i] = e;
                   8161:         td += e;
                   8162:     }
                   8163:     a->td = td;
                   8164:     return a;
1.117     noro     8165: }
                   8166:
1.118     noro     8167: int nd_monic(int mod,ND *p)
1.117     noro     8168: {
1.157     noro     8169:     UINT *trans,*t;
                   8170:     DL alg;
                   8171:     MP mp0,mp;
                   8172:     NM m,m0,m1,ma0,ma,mb,mr0,mr;
                   8173:     ND r;
                   8174:     DL dl;
                   8175:     DP nm;
                   8176:     NDV ndv;
                   8177:     DAlg inv,cd;
                   8178:     ND s,c;
                   8179:     Q l,mul;
                   8180:     N ln;
                   8181:     int n,ntrans,i,e,td,is_lc,len;
                   8182:     NumberField nf;
                   8183:     struct oEGT eg0,eg1;
                   8184:
                   8185:     if ( !(nf = get_numberfield()) )
                   8186:         error("nd_monic : current_numberfield is not set");
                   8187:
                   8188:     /* Q coef -> DAlg coef */
                   8189:     NEWNM(ma0); ma = ma0;
                   8190:     m = BDY(*p);
                   8191:     is_lc = 1;
                   8192:     while ( 1 ) {
                   8193:         NEWMP(mp0); mp = mp0;
                   8194:         mp->c = (P)CQ(m);
                   8195:         mp->dl = nd_separate_d(DL(m),DL(ma));
                   8196:         NEWNM(mb);
                   8197:         for ( m = NEXT(m); m; m = NEXT(m) ) {
                   8198:             alg = nd_separate_d(DL(m),DL(mb));
                   8199:             if ( !ndl_equal(DL(ma),DL(mb)) )
                   8200:                 break;
                   8201:             NEXTMP(mp0,mp); mp->c = (P)CQ(m); mp->dl = alg;
                   8202:         }
                   8203:         NEXT(mp) = 0;
                   8204:         MKDP(nd_nalg,mp0,nm);
                   8205:         MKDAlg(nm,ONE,cd);
                   8206:         if ( is_lc == 1 ) {
                   8207:             /* if the lc is a rational number, we have nothing to do */
                   8208:             if ( !mp0->dl->td )
                   8209:                 return 1;
                   8210:
                   8211:             get_eg(&eg0);
                   8212:             invdalg(cd,&inv);
                   8213:             get_eg(&eg1); add_eg(&eg_invdalg,&eg0,&eg1);
                   8214:             /* check the validity of inv */
                   8215:             if ( mod && !rem(NM(inv->dn),mod) )
                   8216:                 return 0;
                   8217:             CA(ma) = nf->one;
                   8218:             is_lc = 0;
                   8219:             ln = ONEN;
                   8220:         } else {
                   8221:             muldalg(cd,inv,&CA(ma));
                   8222:             lcmn(ln,NM(CA(ma)->dn),&ln);
                   8223:         }
                   8224:         if ( m ) {
                   8225:             NEXT(ma) = mb; ma = mb;
                   8226:         } else {
                   8227:             NEXT(ma) = 0;
                   8228:             break;
                   8229:         }
                   8230:     }
                   8231:     /* l = lcm(denoms) */
                   8232:     NTOQ(ln,1,l);
                   8233:     for ( mr0 = 0, m = ma0; m; m = NEXT(m) ) {
                   8234:         divq(l,CA(m)->dn,&mul);
                   8235:         for ( mp = BDY(CA(m)->nm); mp; mp = NEXT(mp) ) {
                   8236:             NEXTNM(mr0,mr);
                   8237:             mulq((Q)mp->c,mul,&CQ(mr));
                   8238:             dl = mp->dl;
                   8239:             td = TD(DL(m));
                   8240:             ndl_copy(DL(m),DL(mr));
                   8241:             for ( i = 0; i < nd_nalg; i++ ) {
                   8242:                 e = dl->d[i];
                   8243:                 PUT_EXP(DL(mr),i+nd_ntrans,e);
                   8244:                 td += MUL_WEIGHT(e,i+nd_ntrans);
                   8245:             }
1.163     noro     8246:             if ( nd_module ) MPOS(DL(mr)) = MPOS(DL(m));
1.157     noro     8247:             TD(DL(mr)) = td;
                   8248:             if ( nd_blockmask) ndl_weight_mask(DL(mr));
                   8249:         }
                   8250:     }
                   8251:     NEXT(mr) = 0;
                   8252:     for ( len = 0, mr = mr0; mr; mr = NEXT(mr), len++ );
                   8253:     MKND(NV(*p),mr0,len,r);
                   8254:     /* XXX */
                   8255:     SG(r) = SG(*p);
                   8256:     nd_free(*p);
                   8257:     *p = r;
                   8258:     return 1;
1.59      noro     8259: }
1.167     noro     8260:
                   8261: NODE reverse_node(NODE n)
                   8262: {
                   8263:     NODE t,t1;
                   8264:
                   8265:     for ( t = 0; n; n = NEXT(n) ) {
                   8266:         MKNODE(t1,BDY(n),t); t = t1;
                   8267:     }
                   8268:     return t;
                   8269: }
                   8270:
                   8271: P ndc_div(int mod,union oNDC a,union oNDC b)
                   8272: {
                   8273:     union oNDC c;
                   8274:     int inv,t;
                   8275:
                   8276:     if ( mod == -1 ) c.m = _mulsf(a.m,_invsf(b.m));
1.232     noro     8277:     else if ( mod == -2 ) divlf(a.z,b.z,&c.z);
1.167     noro     8278:     else if ( mod ) {
                   8279:         inv = invm(b.m,mod);
                   8280:         DMAR(a.m,inv,0,mod,t); c.m = t;
                   8281:     } else if ( nd_vc )
                   8282:        divsp(nd_vc,a.p,b.p,&c.p);
                   8283:     else
                   8284:        divq(a.z,b.z,&c.z);
                   8285:     return ndctop(mod,c);
                   8286: }
                   8287:
                   8288: P ndctop(int mod,union oNDC c)
                   8289: {
                   8290:     Q q;
                   8291:     int e;
                   8292:     GFS gfs;
                   8293:
                   8294:     if ( mod == -1 ) {
                   8295:         e = IFTOF(c.m); MKGFS(e,gfs); return (P)gfs;
1.232     noro     8296:     } else if ( mod > 0 ) {
1.167     noro     8297:         STOQ(c.m,q); return (P)q;
                   8298:     } else
                   8299:         return (P)c.p;
                   8300: }
                   8301:
                   8302: /* [0,0,0,cont] = p -> p/cont */
                   8303:
                   8304: void finalize_tracelist(int i,P cont)
                   8305: {
                   8306:         LIST l;
                   8307:         NODE node;
                   8308:      Q iq;
                   8309:
                   8310:         if ( !UNIQ(cont) ) {
1.196     noro     8311:          node = mknode(4,NULLP,NULLP,NULLP,cont);
1.167     noro     8312:          MKLIST(l,node); MKNODE(node,l,nd_tracelist);
                   8313:                 nd_tracelist = node;
                   8314:         }
                   8315:      STOQ(i,iq);
                   8316:      nd_tracelist = reverse_node(nd_tracelist);
                   8317:      MKLIST(l,nd_tracelist);
                   8318:      node = mknode(2,iq,l); MKLIST(l,node);
                   8319:      MKNODE(node,l,nd_alltracelist); MKLIST(l,node);
                   8320:      nd_alltracelist = node; nd_tracelist = 0;
                   8321: }
                   8322:
                   8323: void conv_ilist(int demand,int trace,NODE g,int **indp)
                   8324: {
                   8325:     int n,i,j;
                   8326:        int *ind;
                   8327:        NODE t;
                   8328:
                   8329:     n = length(g);
                   8330:        ind = (int *)MALLOC(n*sizeof(int));
                   8331:        for ( i = 0, t = g; i < n; i++, t = NEXT(t) ) {
                   8332:                j = (long)BDY(t); ind[i] = j;
                   8333:                BDY(t) = (pointer)(demand?ndv_load(j):(trace?nd_ps_trace[j]:nd_ps[j]));
                   8334:        }
                   8335:        if ( indp ) *indp = ind;
                   8336: }
1.172     noro     8337:
                   8338: void parse_nd_option(NODE opt)
                   8339: {
1.187     noro     8340:     NODE t,p,u;
1.231     noro     8341:        int i,s,n;
1.172     noro     8342:     char *key;
                   8343:     Obj value;
                   8344:
1.187     noro     8345:     nd_gentrace = 0; nd_gensyz = 0; nd_nora = 0; nd_gbblock = 0;
1.208     noro     8346:        nd_newelim = 0; nd_intersect = 0; nd_nzlist = 0;
1.209     noro     8347:        nd_splist = 0; nd_check_splist = 0;
1.231     noro     8348:     nd_sugarweight = 0;
1.234     noro     8349:     nd_f4red =0;
                   8350:     nd_rank0 = 0;
1.172     noro     8351:     for ( t = opt; t; t = NEXT(t) ) {
                   8352:         p = BDY((LIST)BDY(t));
                   8353:         key = BDY((STRING)BDY(p));
                   8354:         value = (Obj)BDY(NEXT(p));
                   8355:         if ( !strcmp(key,"gentrace") )
                   8356:             nd_gentrace = value?1:0;
                   8357:         else if ( !strcmp(key,"gensyz") )
                   8358:             nd_gensyz = value?1:0;
1.173     noro     8359:         else if ( !strcmp(key,"nora") )
                   8360:             nd_nora = value?1:0;
1.187     noro     8361:         else if ( !strcmp(key,"gbblock") ) {
1.233     noro     8362:             if ( value && OID(value) == O_LIST ) {
                   8363:                          u = BDY((LIST)value);
                   8364:               nd_gbblock = MALLOC((2*length(u)+1)*sizeof(int));
                   8365:                          for ( i = 0; u; u = NEXT(u) ) {
                   8366:                                  p = BDY((LIST)BDY(u));
                   8367:                                  s = nd_gbblock[i++] = QTOS((Q)BDY(p));
                   8368:                                  nd_gbblock[i++] = s+QTOS((Q)BDY(NEXT(p)))-1;
                   8369:                          }
                   8370:                          nd_gbblock[i] = -1;
                   8371:             } else
                   8372:               nd_gbblock = 0;
1.192     noro     8373:                } else if ( !strcmp(key,"newelim") )
                   8374:             nd_newelim = value?1:0;
1.195     noro     8375:                else if ( !strcmp(key,"intersect") )
                   8376:             nd_intersect = value?1:0;
1.234     noro     8377:                else if ( !strcmp(key,"lf") )
                   8378:             nd_lf = value?1:0;
1.208     noro     8379:                else if ( !strcmp(key,"trace") ) {
                   8380:            u = BDY((LIST)value);
                   8381:                   nd_nzlist = BDY((LIST)ARG2(u));
                   8382:                   nd_bpe = QTOS((Q)ARG3(u));
1.234     noro     8383:                } else if ( !strcmp(key,"f4red") ) {
                   8384:                   nd_f4red = QTOS((Q)value);
                   8385:                } else if ( !strcmp(key,"rank0") ) {
                   8386:             nd_rank0 = value?1:0;
                   8387:                } else if ( !strcmp(key,"splist") ) {
1.209     noro     8388:             nd_splist = value?1:0;
1.234     noro     8389:                } else if ( !strcmp(key,"check_splist") ) {
1.209     noro     8390:                        nd_check_splist = BDY((LIST)value);
1.231     noro     8391:                } else if ( !strcmp(key,"sugarweight") ) {
                   8392:                        u = BDY((LIST)value);
                   8393:             n = length(u);
                   8394:             nd_sugarweight = MALLOC(n*sizeof(int));
                   8395:                        for ( i = 0; i < n; i++, u = NEXT(u) )
                   8396:                 nd_sugarweight[i] = QTOS((Q)BDY(u));
1.208     noro     8397:                }
1.172     noro     8398:     }
                   8399: }
1.204     noro     8400:
                   8401: ND mdptond(DP d);
                   8402: ND nd_mul_nm(int mod,NM m0,ND p);
1.207     noro     8403: ND *btog(NODE ti,ND **p,int nb,int mod);
                   8404: ND btog_one(NODE ti,ND *p,int nb,int mod);
1.204     noro     8405: MAT nd_btog(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,MAT *rp);
1.205     noro     8406: VECT nd_btog_one(LIST f,LIST v,int m,struct order_spec *ord,LIST tlist,int pos,MAT *rp);
1.204     noro     8407:
                   8408: /* d:monomial */
                   8409: ND mdptond(DP d)
                   8410: {
                   8411:   NM m;
                   8412:   ND r;
                   8413:
                   8414:   if ( OID(d) == 1 )
                   8415:        r = ptond(CO,CO,(P)d);
                   8416:   else {
                   8417:     NEWNM(m);
                   8418:     dltondl(NV(d),BDY(d)->dl,DL(m));
                   8419:     CQ(m) = (Q)BDY(d)->c;
                   8420:     NEXT(m) = 0;
                   8421:     MKND(NV(d),m,1,r);
                   8422:   }
                   8423:     return r;
                   8424: }
                   8425:
                   8426: ND nd_mul_nm(int mod,NM m0,ND p)
                   8427: {
                   8428:   UINT *d0;
                   8429:   int c0,c1,c;
                   8430:   NM tm,mr,mr0;
                   8431:   ND r;
                   8432:
                   8433:   if ( !p ) return 0;
                   8434:   d0 = DL(m0);
                   8435:   c0 = CM(m0);
                   8436:   mr0 = 0;
                   8437:   for ( tm = BDY(p); tm; tm = NEXT(tm) ) {
                   8438:     NEXTNM(mr0,mr);
                   8439:        c = CM(tm); DMAR(c0,c,0,mod,c1); CM(mr) = c1;
                   8440:        ndl_add(d0,DL(tm),DL(mr));
                   8441:   }
                   8442:   NEXT(mr) = 0;
                   8443:   MKND(NV(p),mr0,LEN(p),r);
                   8444:   return r;
                   8445: }
                   8446:
1.207     noro     8447: ND *btog(NODE ti,ND **p,int nb,int mod)
1.204     noro     8448: {
                   8449:   PGeoBucket *r;
                   8450:   int i,ci;
                   8451:   NODE t,s;
                   8452:   ND m,tp;
                   8453:   ND *pi,*rd;
                   8454:   P c;
                   8455:
                   8456:   r = (PGeoBucket *)MALLOC(nb*sizeof(PGeoBucket));
                   8457:   for ( i = 0; i < nb; i++ )
                   8458:        r[i] = create_pbucket();
                   8459:   for ( t = ti; t; t = NEXT(t) ) {
                   8460:        s = BDY((LIST)BDY(t));
                   8461:     if ( ARG0(s) ) {
                   8462:          m = mdptond((DP)ARG2(s));
                   8463:          ptomp(mod,(P)HCQ(m),&c);
                   8464:          if ( ci = ((MQ)c)->cont ) {
                   8465:            HCM(m) = ci;
                   8466:            pi = p[QTOS((Q)ARG1(s))];
                   8467:            for ( i = 0; i < nb; i++ ) {
                   8468:                  tp = nd_mul_nm(mod,BDY(m),pi[i]);
                   8469:              add_pbucket(mod,r[i],tp);
                   8470:            }
                   8471:          }
                   8472:          ci = 1;
                   8473:     } else {
                   8474:          ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   8475:          ci = invm(ci,mod);
                   8476:        }
                   8477:   }
                   8478:   rd = (ND *)MALLOC(nb*sizeof(ND));
                   8479:   for ( i = 0; i < nb; i++ )
                   8480:        rd[i] = normalize_pbucket(mod,r[i]);
                   8481:   if ( ci != 1 )
                   8482:     for ( i = 0; i < nb; i++ ) nd_mul_c(mod,rd[i],ci);
                   8483:    return rd;
                   8484: }
                   8485:
1.207     noro     8486: ND btog_one(NODE ti,ND *p,int nb,int mod)
1.205     noro     8487: {
                   8488:   PGeoBucket r;
1.206     noro     8489:   int i,ci,j;
1.205     noro     8490:   NODE t,s;
                   8491:   ND m,tp;
                   8492:   ND pi,rd;
                   8493:   P c;
                   8494:
                   8495:   r = create_pbucket();
                   8496:   for ( t = ti; t; t = NEXT(t) ) {
                   8497:        s = BDY((LIST)BDY(t));
                   8498:     if ( ARG0(s) ) {
                   8499:          m = mdptond((DP)ARG2(s));
                   8500:          ptomp(mod,(P)HCQ(m),&c);
                   8501:          if ( ci = ((MQ)c)->cont ) {
                   8502:            HCM(m) = ci;
1.206     noro     8503:            pi = p[j=QTOS((Q)ARG1(s))];
                   8504:                if ( !pi ) {
                   8505:                  pi = nd_load_mod(j);
                   8506:                  tp = nd_mul_nm(mod,BDY(m),pi);
                   8507:                  nd_free(pi);
                   8508:              add_pbucket(mod,r,tp);
                   8509:                } else {
                   8510:                  tp = nd_mul_nm(mod,BDY(m),pi);
                   8511:              add_pbucket(mod,r,tp);
                   8512:            }
1.205     noro     8513:          }
                   8514:          ci = 1;
                   8515:     } else {
                   8516:          ptomp(mod,(P)ARG3(s),&c); ci = ((MQ)c)->cont;
                   8517:          ci = invm(ci,mod);
                   8518:        }
                   8519:   }
                   8520:   rd = normalize_pbucket(mod,r);
1.206     noro     8521:   free_pbucket(r);
1.205     noro     8522:   if ( ci != 1 ) nd_mul_c(mod,rd,ci);
                   8523:   return rd;
                   8524: }
1.204     noro     8525:
                   8526: MAT nd_btog(LIST f,LIST v,int mod,struct order_spec *ord,LIST tlist,MAT *rp)
                   8527: {
                   8528:   int i,j,n,m,nb,pi0,pi1,nvar;
                   8529:   VL fv,tv,vv;
                   8530:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   8531:   ND **p;
                   8532:   ND *c;
                   8533:   ND u;
                   8534:   P inv;
                   8535:   MAT mat;
                   8536:
                   8537:   parse_nd_option(current_option);
                   8538:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   8539:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   8540:   switch ( ord->id ) {
                   8541:     case 1:
                   8542:       if ( ord->nv != nvar )
                   8543:         error("nd_check : invalid order specification");
                   8544:       break;
                   8545:     default:
                   8546:       break;
                   8547:   }
                   8548:   nd_init_ord(ord);
                   8549: #if 0
                   8550:   nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
                   8551: #else
                   8552:   nd_bpe = 32;
                   8553: #endif
                   8554:   nd_setup_parameters(nvar,0);
                   8555:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   8556:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   8557:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   8558:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   8559:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
                   8560:        j = QTOS((Q)BDY(BDY((LIST)BDY(t))));
                   8561:        if ( j > i ) i = j;
                   8562:   }
                   8563:   n = i+1;
                   8564:   nb = length(BDY(f));
                   8565:   p = (ND **)MALLOC(n*sizeof(ND *));
                   8566:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   8567:     pi = BDY((LIST)BDY(t));
                   8568:        pi0 = QTOS((Q)ARG0(pi)); pi1 = QTOS((Q)ARG1(pi));
                   8569:        p[pi0] = c = (ND *)MALLOC(nb*sizeof(ND));
                   8570:        ptomp(mod,(P)ARG2(pi),&inv);
1.218     noro     8571:        ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
1.204     noro     8572:        u = ptond(CO,vv,(P)ONE);
                   8573:        HCM(u) = ((MQ)inv)->cont;
                   8574:        c[pi1] = u;
                   8575:   }
                   8576:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   8577:        printf("%d ",i); fflush(stdout);
                   8578:     ti = BDY((LIST)BDY(t));
1.207     noro     8579:     p[j=QTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.204     noro     8580:   }
                   8581:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   8582:        printf("%d ",i); fflush(stdout);
                   8583:     ti = BDY((LIST)BDY(t));
1.207     noro     8584:     p[j=QTOS((Q)ARG0(ti))] = btog(BDY((LIST)ARG1(ti)),p,nb,mod);
1.204     noro     8585:   }
                   8586:   m = length(ind);
                   8587:   MKMAT(mat,nb,m);
                   8588:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) )
                   8589:     for ( i = 0, c = p[QTOS((Q)BDY(t))]; i < nb; i++ )
                   8590:                BDY(mat)[i][j] = ndtodp(mod,c[i]);
                   8591:   return mat;
                   8592: }
                   8593:
1.205     noro     8594: VECT nd_btog_one(LIST f,LIST v,int mod,struct order_spec *ord,
                   8595:   LIST tlist,int pos,MAT *rp)
                   8596: {
                   8597:   int i,j,n,m,nb,pi0,pi1,nvar;
                   8598:   VL fv,tv,vv;
                   8599:   NODE permtrace,perm,trace,intred,ind,t,pi,ti;
                   8600:   ND *p;
                   8601:   ND *c;
                   8602:   ND u;
                   8603:   P inv;
                   8604:   VECT vect;
                   8605:
                   8606:   parse_nd_option(current_option);
                   8607:   get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   8608:   for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   8609:   switch ( ord->id ) {
                   8610:     case 1:
                   8611:       if ( ord->nv != nvar )
                   8612:         error("nd_check : invalid order specification");
                   8613:       break;
                   8614:     default:
                   8615:       break;
                   8616:   }
                   8617:   nd_init_ord(ord);
                   8618: #if 0
                   8619:   nd_bpe = QTOS((Q)ARG7(BDY(tlist)));
                   8620: #else
                   8621:   nd_bpe = 32;
                   8622: #endif
                   8623:   nd_setup_parameters(nvar,0);
                   8624:   permtrace = BDY((LIST)ARG2(BDY(tlist)));
                   8625:   intred = BDY((LIST)ARG3(BDY(tlist)));
                   8626:   ind = BDY((LIST)ARG4(BDY(tlist)));
                   8627:   perm = BDY((LIST)BDY(permtrace)); trace =NEXT(permtrace);
                   8628:   for ( i = length(perm)-1, t = trace; t; t = NEXT(t) ) {
                   8629:        j = QTOS((Q)BDY(BDY((LIST)BDY(t))));
                   8630:        if ( j > i ) i = j;
                   8631:   }
                   8632:   n = i+1;
                   8633:   nb = length(BDY(f));
                   8634:   p = (ND *)MALLOC(n*sizeof(ND *));
                   8635:   for ( t = perm, i = 0; t; t = NEXT(t), i++ ) {
                   8636:     pi = BDY((LIST)BDY(t));
                   8637:        pi0 = QTOS((Q)ARG0(pi)); pi1 = QTOS((Q)ARG1(pi));
                   8638:        if ( pi1 == pos ) {
1.218     noro     8639:          ptomp(mod,(P)ARG2(pi),&inv);
                   8640:          ((MQ)inv)->cont = invm(((MQ)inv)->cont,mod);
1.205     noro     8641:          u = ptond(CO,vv,(P)ONE);
                   8642:          HCM(u) = ((MQ)inv)->cont;
                   8643:          p[pi0] = u;
                   8644:     }
                   8645:   }
                   8646:   for ( t = trace,i=0; t; t = NEXT(t), i++ ) {
                   8647:        printf("%d ",i); fflush(stdout);
                   8648:     ti = BDY((LIST)BDY(t));
1.207     noro     8649:     p[j=QTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.206     noro     8650:     if ( Demand ) {
                   8651:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   8652:        }
1.205     noro     8653:   }
                   8654:   for ( t = intred, i=0; t; t = NEXT(t), i++ ) {
                   8655:        printf("%d ",i); fflush(stdout);
                   8656:     ti = BDY((LIST)BDY(t));
1.207     noro     8657:     p[j=QTOS((Q)ARG0(ti))] = btog_one(BDY((LIST)ARG1(ti)),p,nb,mod);
1.206     noro     8658:     if ( Demand ) {
                   8659:         nd_save_mod(p[j],j); nd_free(p[j]); p[j] = 0;
                   8660:        }
1.205     noro     8661:   }
                   8662:   m = length(ind);
                   8663:   MKVECT(vect,m);
1.206     noro     8664:   for ( j = 0, t = ind; j < m; j++, t = NEXT(t) ) {
                   8665:        u = p[QTOS((Q)BDY(t))];
                   8666:        if ( !u ) {
                   8667:          u = nd_load_mod(QTOS((Q)BDY(t)));
                   8668:          BDY(vect)[j] = ndtodp(mod,u);
                   8669:          nd_free(u);
                   8670:        } else
                   8671:          BDY(vect)[j] = ndtodp(mod,u);
                   8672:   }
1.205     noro     8673:   return vect;
                   8674: }
1.232     noro     8675:
                   8676: void ndv_print_lf(NDV p)
                   8677: {
                   8678:     NMV m;
                   8679:     int i,len;
                   8680:
                   8681:     if ( !p ) printf("0\n");
                   8682:     else {
                   8683:         len = LEN(p);
                   8684:         for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   8685:             printf("+");
                   8686:             mpz_out_str(asir_out,10,BDY(CZ(m)));
                   8687:             printf("*");
                   8688:             ndl_print(DL(m));
                   8689:         }
                   8690:         printf("\n");
                   8691:     }
                   8692: }
1.234     noro     8693:
                   8694: void nd_f4_lf_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
                   8695: {
                   8696:     VL tv,fv,vv,vc,av;
                   8697:     NODE fd,fd0,in0,in,r,r0,t,s,cand,alist;
                   8698:     int m,nocheck,nvar,mindex,e,max;
                   8699:     NDV c;
                   8700:     NMV a;
                   8701:     P p,zp;
                   8702:     Q dmy;
                   8703:     EPOS oepos;
                   8704:     int obpe,oadv,wmax,i,len,cbpe,ishomo,nalg,mrank,trank,ompos;
                   8705:     Alg alpha,dp;
                   8706:     P poly;
                   8707:     LIST f1,f2,zpl;
                   8708:     Obj obj;
                   8709:     NumberField nf;
                   8710:     struct order_spec *ord1;
                   8711:     struct oEGT eg_check,eg0,eg1;
                   8712:     NODE tr,tl1,tl2,tl3,tl4;
                   8713:     LIST l1,l2,l3,l4,l5;
                   8714:     int *perm;
                   8715:     int j,ret;
                   8716:     Q jq,bpe;
                   8717:
                   8718:     nd_module = 0;
                   8719:     parse_nd_option(current_option);
                   8720:     get_vars((Obj)f,&fv); pltovl(v,&vv); vlminus(fv,vv,&nd_vc);
                   8721:     if ( nd_vc )
                   8722:       error("nd_f4_lf_trace : computation over a rational function field is not implemented");
                   8723:     for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
                   8724:     switch ( ord->id ) {
                   8725:         case 1:
                   8726:             if ( ord->nv != nvar )
                   8727:                 error("nd_f4_lf_trace : invalid order specification");
                   8728:             break;
                   8729:         default:
                   8730:             break;
                   8731:     }
                   8732:
                   8733:     nd_ntrans = nvar;
                   8734:     nd_nalg = 0;
                   8735:
                   8736:     nocheck = 0;
                   8737:     mindex = 0;
                   8738:
1.235   ! noro     8739:     /* do not use on-demand load/save */
        !          8740:     nd_demand = 0;
1.234     noro     8741:     m = trace > 1 ? trace : get_lprime(mindex);
                   8742:     nd_init_ord(ord);
                   8743:     mrank = 0;
                   8744:     for ( t = BDY(f), max = 1; t; t = NEXT(t) )
                   8745:         for ( tv = vv; tv; tv = NEXT(tv) ) {
                   8746:             if ( nd_module ) {
                   8747:                 s = BDY((LIST)BDY(t));
                   8748:                 trank = length(s);
                   8749:                 mrank = MAX(mrank,trank);
                   8750:                 for ( ; s; s = NEXT(s) ) {
                   8751:                     e = getdeg(tv->v,(P)BDY(s));
                   8752:                     max = MAX(e,max);
                   8753:                 }
                   8754:             } else {
                   8755:                 e = getdeg(tv->v,(P)BDY(t));
                   8756:                 max = MAX(e,max);
                   8757:             }
                   8758:         }
                   8759:     nd_setup_parameters(nvar,max);
                   8760:     obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos; ompos = nd_mpos;
                   8761:     ishomo = 1;
                   8762:     /* XXX */
                   8763:     for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   8764:         if ( nd_module ) {
                   8765:             c = (pointer)pltondv(CO,vv,(LIST)BDY(t));
                   8766:         } else {
                   8767:             c = (pointer)ptondv(CO,vv,(P)BDY(t));
                   8768:         }
                   8769:         if ( ishomo )
                   8770:             ishomo = ishomo && ndv_ishomo(c);
                   8771:         if ( c ) {
                   8772:             NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   8773:             NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
                   8774:         }
                   8775:     }
                   8776:     if ( in0 ) NEXT(in) = 0;
                   8777:     if ( fd0 ) NEXT(fd) = 0;
                   8778:     if ( !ishomo && homo ) {
                   8779:         for ( t = in0, wmax = max; t; t = NEXT(t) ) {
                   8780:             c = (NDV)BDY(t); len = LEN(c);
                   8781:             for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   8782:                 wmax = MAX(TD(DL(a)),wmax);
                   8783:         }
                   8784:         homogenize_order(ord,nvar,&ord1);
                   8785:         nd_init_ord(ord1);
                   8786:         nd_setup_parameters(nvar+1,wmax);
                   8787:         for ( t = fd0; t; t = NEXT(t) )
                   8788:             ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos,ompos);
                   8789:     }
                   8790:     if ( MaxDeg > 0 ) nocheck = 1;
1.235   ! noro     8791:     ret = ndv_setup(-2,m,fd0,nd_gbblock?1:0,0);
        !          8792:     if ( ret )
        !          8793:       cand = nd_f4_lf_trace_main(m,&perm);
        !          8794:     if ( !ret || !cand ) {
        !          8795:        *rp = 0; return;
        !          8796:     }
        !          8797:     if ( !ishomo && homo ) {
        !          8798:       /* dehomogenization */
        !          8799:       for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
        !          8800:       nd_init_ord(ord);
        !          8801:       nd_setup_parameters(nvar,0);
1.234     noro     8802:     }
1.235   ! noro     8803:     cand = ndv_reducebase(cand,perm);
        !          8804:     cand = ndv_reduceall(-2,cand);
        !          8805:     cbpe = nd_bpe;
        !          8806:     get_eg(&eg0);
        !          8807:     if ( ret = ndv_check_membership(-2,in0,obpe,oadv,oepos,cand) ) {
        !          8808:       /* gbcheck : cand is a GB of Id(cand) ? */
        !          8809:       ret = nd_f4(-2,0,0);
        !          8810:     }
        !          8811:        if ( !ret ) {
        !          8812:       /* failure */
        !          8813:       *rp = 0; return;
        !          8814:     }
1.234     noro     8815:     get_eg(&eg1); init_eg(&eg_check); add_eg(&eg_check,&eg0,&eg1);
                   8816:     if ( DP_Print )
                   8817:         fprintf(asir_out,"check=%fsec\n",eg_check.exectime+eg_check.gctime);
                   8818:     /* dp->p */
                   8819:     nd_bpe = cbpe;
                   8820:     nd_setup_parameters(nd_nvar,0);
                   8821:     for ( r = cand; r; r = NEXT(r) ) {
                   8822:       if ( nd_module ) BDY(r) = ndvtopl(-2,CO,vv,BDY(r),mrank);
                   8823:       else BDY(r) = (pointer)ndvtop(-2,CO,vv,BDY(r));
                   8824:     }
                   8825:     MKLIST(*rp,cand);
                   8826: }
                   8827:
                   8828: NODE nd_f4_lf_trace_main(int m,int **indp)
                   8829: {
                   8830:     int i,nh,stat,index;
                   8831:     NODE r,rm,g;
                   8832:     ND_pairs d,l,l0,t;
                   8833:     ND spol,red;
                   8834:     NDV nf,redv,nfqv,nfv;
                   8835:     NM s0,s;
                   8836:     NODE rp0,srp0,nflist,nflist_lf;
                   8837:     int nsp,nred,col,rank,len,k,j,a;
                   8838:     UINT c;
                   8839:     UINT **spmat;
                   8840:     UINT *s0vect,*svect,*p,*v;
                   8841:     int *colstat;
                   8842:     IndArray *imat;
                   8843:     int *rhead;
                   8844:     int spcol,sprow;
                   8845:     int sugar;
                   8846:     PGeoBucket bucket;
                   8847:     struct oEGT eg0,eg1,eg_f4;
                   8848:
                   8849:     g = 0; d = 0;
                   8850:     for ( i = 0; i < nd_psn; i++ ) {
                   8851:         d = update_pairs(d,g,i,0);
                   8852:         g = update_base(g,i);
                   8853:     }
                   8854:     while ( d ) {
                   8855:         get_eg(&eg0);
                   8856:         l = nd_minsugarp(d,&d);
                   8857:         sugar = SG(l);
                   8858:         if ( MaxDeg > 0 && sugar > MaxDeg ) break;
                   8859:         bucket = create_pbucket();
                   8860:         stat = nd_sp_f4(m,0,l,bucket);
                   8861:         if ( !stat ) {
                   8862:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8863:             NEXT(t) = d; d = l;
                   8864:             d = nd_reconstruct(1,d);
                   8865:             continue;
                   8866:         }
                   8867:         if ( bucket->m < 0 ) continue;
                   8868:         col = nd_symbolic_preproc(bucket,0,&s0vect,&rp0);
                   8869:         if ( !col ) {
                   8870:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8871:             NEXT(t) = d; d = l;
                   8872:             d = nd_reconstruct(1,d);
                   8873:             continue;
                   8874:         }
                   8875:         get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
                   8876:         if ( DP_Print )
                   8877:             fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   8878:                 sugar,eg_f4.exectime+eg_f4.gctime);
                   8879:         nflist = nd_f4_red(m,l,0,s0vect,col,rp0,&l0);
                   8880:         if ( !l0 ) continue;
                   8881:         l = l0;
                   8882:
                   8883:         /* over LF */
                   8884:         bucket = create_pbucket();
                   8885:         stat = nd_sp_f4(-2,1,l,bucket);
                   8886:         if ( !stat ) {
                   8887:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8888:             NEXT(t) = d; d = l;
                   8889:             d = nd_reconstruct(1,d);
                   8890:             continue;
                   8891:         }
                   8892:         if ( bucket->m < 0 ) continue;
                   8893:         col = nd_symbolic_preproc(bucket,1,&s0vect,&rp0);
                   8894:         if ( !col ) {
                   8895:             for ( t = l; NEXT(t); t = NEXT(t) );
                   8896:             NEXT(t) = d; d = l;
                   8897:             d = nd_reconstruct(1,d);
                   8898:             continue;
                   8899:         }
                   8900:         nflist_lf = nd_f4_red(-2,l,1,s0vect,col,rp0,0);
                   8901:         /* adding new bases */
                   8902:         for ( rm = nflist, r = nflist_lf; r && rm; rm = NEXT(rm), r = NEXT(r) ) {
                   8903:             nfv = (NDV)BDY(rm);
                   8904:             nfqv = (NDV)BDY(r);
                   8905:             if ( DL_COMPARE(HDL(nfv),HDL(nfqv)) ) return 0;
                   8906:             ndv_removecont(m,nfv);
                   8907:             ndv_removecont(-2,nfqv);
                   8908:             nh = ndv_newps(-2,nfv,nfqv,1);
                   8909:             d = update_pairs(d,g,nh,0);
                   8910:             g = update_base(g,nh);
                   8911:         }
                   8912:         if ( r || rm ) return 0;
                   8913:     }
                   8914:        conv_ilist(nd_demand,1,g,indp);
                   8915:     return g;
                   8916: }
                   8917:

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